<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">From: Alasdair G Kergon &lt;agk@redhat.com&gt;

Use a separate buffer for writing zeroes to the on-disk snapshot
exception store, make the updating of ps-&gt;current_area explicit and
refactor the code in preparation for the fix in the next patch.

No functional change.

Signed-off-by: Alasdair G Kergon &lt;agk@redhat.com&gt;
Signed-off-by: Mikulas Patocka &lt;mpatocka@redhat.com&gt;
Cc: stable@kernel.org
---
 drivers/md/dm-exception-store.c |  104 ++++++++++++++++++++++++++--------------
 1 files changed, 69 insertions(+), 35 deletions(-)

Index: linux-2.6.27/drivers/md/dm-exception-store.c
===================================================================
--- linux-2.6.27.orig/drivers/md/dm-exception-store.c	2008-10-21 17:32:24.000000000 +0100
+++ linux-2.6.27/drivers/md/dm-exception-store.c	2008-10-21 17:33:41.000000000 +0100
@@ -105,6 +105,11 @@ struct pstore {
 	void *area;
 
 	/*
+	 * An area of zeros used to clear the next area.
+	 */
+	void *zero_area;
+
+	/*
 	 * Used to keep track of which metadata area the data in
 	 * 'chunk' refers to.
 	 */
@@ -149,6 +154,13 @@ static int alloc_area(struct pstore *ps)
 	if (!ps-&gt;area)
 		return r;
 
+	ps-&gt;zero_area = vmalloc(len);
+	if (!ps-&gt;zero_area) {
+		vfree(ps-&gt;area);
+		return r;
+	}
+	memset(ps-&gt;zero_area, 0, len);
+
 	return 0;
 }
 
@@ -156,6 +168,8 @@ static void free_area(struct pstore *ps)
 {
 	vfree(ps-&gt;area);
 	ps-&gt;area = NULL;
+	vfree(ps-&gt;zero_area);
+	ps-&gt;zero_area = NULL;
 }
 
 struct mdata_req {
@@ -220,25 +234,41 @@ static chunk_t area_location(struct psto
  * Read or write a metadata area.  Remembering to skip the first
  * chunk which holds the header.
  */
-static int area_io(struct pstore *ps, chunk_t area, int rw)
+static int area_io(struct pstore *ps, int rw)
 {
 	int r;
 	chunk_t chunk;
 
-	chunk = area_location(ps, area);
+	chunk = area_location(ps, ps-&gt;current_area);
 
 	r = chunk_io(ps, chunk, rw, 0);
 	if (r)
 		return r;
 
-	ps-&gt;current_area = area;
 	return 0;
 }
 
-static int zero_area(struct pstore *ps, chunk_t area)
+static void zero_memory_area(struct pstore *ps)
 {
 	memset(ps-&gt;area, 0, ps-&gt;snap-&gt;chunk_size &lt;&lt; SECTOR_SHIFT);
-	return area_io(ps, area, WRITE);
+}
+
+static int zero_disk_area(struct pstore *ps, chunk_t area)
+{
+	struct dm_io_region where = {
+		.bdev = ps-&gt;snap-&gt;cow-&gt;bdev,
+		.sector = ps-&gt;snap-&gt;chunk_size * area_location(ps, area),
+		.count = ps-&gt;snap-&gt;chunk_size,
+	};
+	struct dm_io_request io_req = {
+		.bi_rw = WRITE,
+		.mem.type = DM_IO_VMA,
+		.mem.ptr.vma = ps-&gt;zero_area,
+		.client = ps-&gt;io_client,
+		.notify.fn = NULL,
+	};
+
+	return dm_io(&amp;io_req, 1, &amp;where, NULL);
 }
 
 static int read_header(struct pstore *ps, int *new_snapshot)
@@ -411,15 +441,14 @@ static int insert_exceptions(struct psto
 
 static int read_exceptions(struct pstore *ps)
 {
-	chunk_t area;
 	int r, full = 1;
 
 	/*
 	 * Keeping reading chunks and inserting exceptions until
 	 * we find a partially full area.
 	 */
-	for (area = 0; full; area++) {
-		r = area_io(ps, area, READ);
+	for (ps-&gt;current_area = 0; full; ps-&gt;current_area++) {
+		r = area_io(ps, READ);
 		if (r)
 			return r;
 
@@ -428,6 +457,8 @@ static int read_exceptions(struct pstore
 			return r;
 	}
 
+	ps-&gt;current_area--;
+
 	return 0;
 }
 
@@ -486,12 +517,13 @@ static int persistent_read_metadata(stru
 			return r;
 		}
 
-		r = zero_area(ps, 0);
+		ps-&gt;current_area = 0;
+		zero_memory_area(ps);
+		r = zero_disk_area(ps, 0);
 		if (r) {
-			DMWARN("zero_area(0) failed");
+			DMWARN("zero_disk_area(0) failed");
 			return r;
 		}
-
 	} else {
 		/*
 		 * Sanity checks.
@@ -551,7 +583,6 @@ static void persistent_commit(struct exc
 			      void (*callback) (void *, int success),
 			      void *callback_context)
 {
-	int r;
 	unsigned int i;
 	struct pstore *ps = get_info(store);
 	struct disk_exception de;
@@ -572,33 +603,36 @@ static void persistent_commit(struct exc
 	cb-&gt;context = callback_context;
 
 	/*
-	 * If there are no more exceptions in flight, or we have
-	 * filled this metadata area we commit the exceptions to
-	 * disk.
-	 */
-	if (atomic_dec_and_test(&amp;ps-&gt;pending_count) ||
-	    (ps-&gt;current_committed == ps-&gt;exceptions_per_area)) {
-		r = area_io(ps, ps-&gt;current_area, WRITE);
-		if (r)
-			ps-&gt;valid = 0;
+	 * If there are exceptions in flight and we have not yet
+	 * filled this metadata area there's nothing more to do.
+	 */
+	if (!atomic_dec_and_test(&amp;ps-&gt;pending_count) &amp;&amp;
+	    (ps-&gt;current_committed != ps-&gt;exceptions_per_area))
+		return;
 
-		/*
-		 * Have we completely filled the current area ?
-		 */
-		if (ps-&gt;current_committed == ps-&gt;exceptions_per_area) {
-			ps-&gt;current_committed = 0;
-			r = zero_area(ps, ps-&gt;current_area + 1);
-			if (r)
-				ps-&gt;valid = 0;
-		}
+	/*
+	 * Commit exceptions to disk.
+	 */
+	if (area_io(ps, WRITE))
+		ps-&gt;valid = 0;
 
-		for (i = 0; i &lt; ps-&gt;callback_count; i++) {
-			cb = ps-&gt;callbacks + i;
-			cb-&gt;callback(cb-&gt;context, r == 0 ? 1 : 0);
-		}
+	/*
+	 * Advance to the next area if this one is full.
+	 */
+	if (ps-&gt;current_committed == ps-&gt;exceptions_per_area) {
+		if (zero_disk_area(ps, ps-&gt;current_area + 1))
+			ps-&gt;valid = 0;
+		ps-&gt;current_committed = 0;
+		ps-&gt;current_area++;
+		zero_memory_area(ps);
+	}
 
-		ps-&gt;callback_count = 0;
+	for (i = 0; i &lt; ps-&gt;callback_count; i++) {
+		cb = ps-&gt;callbacks + i;
+		cb-&gt;callback(cb-&gt;context, ps-&gt;valid);
 	}
+
+	ps-&gt;callback_count = 0;
 }
 
 static void persistent_drop(struct exception_store *store)
</pre></body></html>