Skip to contentSkip to conversation

Clusterized exception store preparation

PATCH New Mike Snitzer 1 participant 21 messages Sep 29, 2009 – Sep 30, 2009

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Clusterized exception store preparation

This patch series provides Jon Brassow's refactored DM exceptions, exception tables and more. These patches form the basis upon which Jon's clusterized exception store is built.

Given the fundamental nature of these changes, relative to code movement and abstractions, the snapshot-merge patches must follow these patches (particularly because of the way the hadnover snapshot exception table handover mechanism changes relative to these patches).

Jon Brassow (18):
dm-snap-consolidate-insert_exception-functions dm-exception-store-generalize-table-args dm-snapshot-new-ctr-table-format dm-snapshot-rename-dm_snap_exception-to-dm_exception dm-snapshot-rename-exception_table-to-dm_exception_table dm-snapshot-rename-exception-functions dm-snapshot-use-allocated-exception-tables dm-snapshot-exception-function-changes-1 dm-snapshot-exception-function-changes-2 dm-snapshot-exception-function-changes-3 dm-snapshot-exception-function-changes-4 dm-snapshot-exception-function-changes-5 dm-snapshot-exception-function-changes-6 dm-snapshot-move-exception-code-to-new-file dm-exception-store-create-local-exception-caches dm-exception-store-add-lookup_exception-to-API dm-snapshot-remove-completed-exception-cache dm-exstore-persistent-allow-metadata-reread

 drivers/md/Makefile             |    4 +-
 drivers/md/dm-exception-store.c |   36 ++--
 drivers/md/dm-exception-store.h |   92 ++-------
 drivers/md/dm-exception.c       |  163 +++++++++++++++
 drivers/md/dm-exception.h       |  104 ++++++++++
 drivers/md/dm-snap-persistent.c |  259 ++++++++++++++++++++----
 drivers/md/dm-snap-transient.c  |   88 ++++++++-
 drivers/md/dm-snap.c            |  415 ++++++++++++++-------------------------
 8 files changed, 748 insertions(+), 413 deletions(-)
 create mode 100644 drivers/md/dm-exception.c
 create mode 100644 drivers/md/dm-exception.h
#1 lore raw

PATCH 1/18 dm-snap-consolidate-insert_exception-functions

From: Jon Brassow <[email protected]>

Consolidate the insert_*exception functions. 'insert_completed_exception' already contains all the logic to handle 'insert_exception' (via check for a hash_shift of 0), so remove redundant function.

---
 drivers/md/dm-snap.c |   16 ++++------------
 1 files changed, 4 insertions(+), 12 deletions(-)
drivers/md/dm-snap.c +4 -12
index f3473e8..3734289 100644
@@ -385,13 +385,6 @@ static uint32_t exception_hash(struct exception_table *et, chunk_t chunk)
 	return (chunk >> et->hash_shift) & et->hash_mask;
 }
 
-static void insert_exception(struct exception_table *eh,
-			     struct dm_snap_exception *e)
-{
-	struct list_head *l = &eh->table[exception_hash(eh, e->old_chunk)];
-	list_add(&e->hash_list, l);
-}
-
 static void remove_exception(struct dm_snap_exception *e)
 {
 	list_del(&e->hash_list);
@@ -452,10 +445,9 @@ static void free_pending_exception(struct dm_snap_pending_exception *pe)
 	atomic_dec(&s->pending_exceptions_count);
 }
 
-static void insert_completed_exception(struct dm_snapshot *s,
-				       struct dm_snap_exception *new_e)
+static void insert_exception(struct exception_table *eh,
+			     struct dm_snap_exception *new_e)
 {
-	struct exception_table *eh = &s->complete;
 	struct list_head *l;
 	struct dm_snap_exception *e = NULL;
 
@@ -513,7 +505,7 @@ static int dm_add_exception(void *context, chunk_t old, chunk_t new)
 	/* Consecutive_count is implicitly initialised to zero */
 	e->new_chunk = new;
 
-	insert_completed_exception(s, e);
+	insert_exception(&s->complete, e);
 
 	return 0;
 }
@@ -918,7 +910,7 @@ static void pending_complete(struct dm_snap_pending_exception *pe, int success)
 	 * Add a proper exception, and remove the
 	 * in-flight exception from the list.
 	 */
-	insert_completed_exception(s, e);
+	insert_exception(&s->complete, e);
 
  out:
 	remove_exception(&pe->e);
Signature · 1 line
1.6.2.5
#2 lore raw

PATCH 2/18 dm-exception-store-generalize-table-args

In reply to Mike Snitzer

From: Jon Brassow <[email protected]>

This patch is part of the work to generalize the dm-snapshot/ exception store API - necessary for future exception store implementations.

This patch reworks 'dm_exception_store_create' arguments to make way for cleaner interface when new constructor table format is introduced.

---
 drivers/md/dm-exception-store.c |   36 +++++++++++++++---------------
 drivers/md/dm-exception-store.h |    4 +-
 drivers/md/dm-snap.c            |   47 ++++++++++++++++++++++++++++++++++-----
 3 files changed, 61 insertions(+), 26 deletions(-)
drivers/md/dm-exception-store.c +18 -18
index 8f49f27..674b49b 100644
@@ -187,16 +187,15 @@ int dm_exception_store_set_chunk_size(struct dm_exception_store *store,
 	return 0;
 }
 
-int dm_exception_store_create(struct dm_target *ti, int argc, char **argv,
-			      unsigned *args_used,
+int dm_exception_store_create(const char *type_name, struct dm_target *ti,
+			      int argc, char **argv,
 			      struct dm_exception_store **store)
 {
 	int r = 0;
 	struct dm_exception_store_type *type = NULL;
 	struct dm_exception_store *tmp_store;
-	char persistent;
 
-	if (argc < 3) {
+	if (argc < 2) {
 		ti->error = "Insufficient exception store arguments";
 		return -EINVAL;
 	}
@@ -207,16 +206,7 @@ int dm_exception_store_create(struct dm_target *ti, int argc, char **argv,
 		return -ENOMEM;
 	}
 
-	persistent = toupper(*argv[1]);
-	if (persistent == 'P')
-		type = get_type("P");
-	else if (persistent == 'N')
-		type = get_type("N");
-	else {
-		ti->error = "Persistent flag is not P or N";
-		return -EINVAL;
-	}
-
+	type = get_type(type_name);
 	if (!type) {
 		ti->error = "Exception store type not recognised";
 		r = -EINVAL;
@@ -226,6 +216,12 @@ int dm_exception_store_create(struct dm_target *ti, int argc, char **argv,
 	tmp_store->type = type;
 	tmp_store->ti = ti;
 
+	/*
+	 * COW-dev and chunk_size are common to all types of
+	 * exception stores and are stored directly in the
+	 * dm_exception_store and not passed on to the
+	 * constructor for the dm_exception_store_type
+	 */
 	r = dm_get_device(ti, argv[0], 0, 0,
 			  FMODE_READ | FMODE_WRITE, &tmp_store->cow);
 	if (r) {
@@ -233,17 +229,21 @@ int dm_exception_store_create(struct dm_target *ti, int argc, char **argv,
 		goto bad_cow;
 	}
 
-	r = set_chunk_size(tmp_store, argv[2], &ti->error);
-	if (r)
+	r = set_chunk_size(tmp_store, argv[1], &ti->error);
+	if (r) {
+		ti->error = "Unable to set chunk size";
 		goto bad_cow;
+	}
+
+	argc -= 2;
+	argv += 2;
 
-	r = type->ctr(tmp_store, 0, NULL);
+	r = type->ctr(tmp_store, argc, argv);
 	if (r) {
 		ti->error = "Exception store type constructor failed";
 		goto bad_ctr;
 	}
 
-	*args_used = 3;
 	*store = tmp_store;
 	return 0;
 
drivers/md/dm-exception-store.h +2 -2
index 3e8fa02..2110e3b 100644
@@ -172,8 +172,8 @@ int dm_exception_store_set_chunk_size(struct dm_exception_store *store,
 				      unsigned long chunk_size_ulong,
 				      char **error);
 
-int dm_exception_store_create(struct dm_target *ti, int argc, char **argv,
-			      unsigned *args_used,
+int dm_exception_store_create(const char *type_name, struct dm_target *ti,
+			      int argc, char **argv,
 			      struct dm_exception_store **store);
 void dm_exception_store_destroy(struct dm_exception_store *store);
 
drivers/md/dm-snap.c +41 -6

65 lines not on this page · raw message

Signature · 1 line
1.6.2.5
#3 lore raw

PATCH v2 2/18 dm-exception-store-generalize-table-args

This patch is part of the work to generalize the dm-snapshot/ exception store API - necessary for future exception store implementations.

This patch reworks 'dm_exception_store_create' arguments to make way for cleaner interface when new constructor table format is introduced.

---
v2: adjusted slightly to account for dm-snapshot-32bit-chunk-size.patch

---
 drivers/md/dm-exception-store.c |   36 +++++++++++++++---------------
 drivers/md/dm-exception-store.h |    4 +--
 drivers/md/dm-snap.c            |   47 ++++++++++++++++++++++++++++++++++------
 3 files changed, 61 insertions(+), 26 deletions(-)
drivers/md/dm-exception-store.c +18 -18
Index: linux-2.6/drivers/md/dm-exception-store.c
===================================================================
@@ -186,16 +186,15 @@ int dm_exception_store_set_chunk_size(st
 	return 0;
 }
 
-int dm_exception_store_create(struct dm_target *ti, int argc, char **argv,
-			      unsigned *args_used,
+int dm_exception_store_create(const char *type_name, struct dm_target *ti,
+			      int argc, char **argv,
 			      struct dm_exception_store **store)
 {
 	int r = 0;
 	struct dm_exception_store_type *type = NULL;
 	struct dm_exception_store *tmp_store;
-	char persistent;
 
-	if (argc < 3) {
+	if (argc < 2) {
 		ti->error = "Insufficient exception store arguments";
 		return -EINVAL;
 	}
@@ -206,16 +205,7 @@ int dm_exception_store_create(struct dm_
 		return -ENOMEM;
 	}
 
-	persistent = toupper(*argv[1]);
-	if (persistent == 'P')
-		type = get_type("P");
-	else if (persistent == 'N')
-		type = get_type("N");
-	else {
-		ti->error = "Persistent flag is not P or N";
-		return -EINVAL;
-	}
-
+	type = get_type(type_name);
 	if (!type) {
 		ti->error = "Exception store type not recognised";
 		r = -EINVAL;
@@ -225,6 +215,12 @@ int dm_exception_store_create(struct dm_
 	tmp_store->type = type;
 	tmp_store->ti = ti;
 
+	/*
+	 * COW-dev and chunk_size are common to all types of
+	 * exception stores and are stored directly in the
+	 * dm_exception_store and not passed on to the
+	 * constructor for the dm_exception_store_type
+	 */
 	r = dm_get_device(ti, argv[0], 0, 0,
 			  FMODE_READ | FMODE_WRITE, &tmp_store->cow);
 	if (r) {
@@ -232,17 +228,21 @@ int dm_exception_store_create(struct dm_
 		goto bad_cow;
 	}
 
-	r = set_chunk_size(tmp_store, argv[2], &ti->error);
-	if (r)
+	r = set_chunk_size(tmp_store, argv[1], &ti->error);
+	if (r) {
+		ti->error = "Unable to set chunk size";
 		goto bad_cow;
+	}
+
+	argc -= 2;
+	argv += 2;
 
-	r = type->ctr(tmp_store, 0, NULL);
+	r = type->ctr(tmp_store, argc, argv);
 	if (r) {
 		ti->error = "Exception store type constructor failed";
 		goto bad_ctr;
 	}
 
-	*args_used = 3;
 	*store = tmp_store;
 	return 0;
 
drivers/md/dm-exception-store.h +2 -2
Index: linux-2.6/drivers/md/dm-exception-store.h
===================================================================
@@ -172,8 +172,8 @@ int dm_exception_store_set_chunk_size(st
 				      unsigned chunk_size,
 				      char **error);
 
-int dm_exception_store_create(struct dm_target *ti, int argc, char **argv,
-			      unsigned *args_used,
+int dm_exception_store_create(const char *type_name, struct dm_target *ti,
+			      int argc, char **argv,
 			      struct dm_exception_store **store);
 void dm_exception_store_destroy(struct dm_exception_store *store);
 
drivers/md/dm-snap.c +41 -6

65 lines not on this page · raw message

#4 lore raw

PATCH 3/18 dm-snapshot-new-ctr-table-format

In reply to Mike Snitzer

From: Jon Brassow <[email protected]>

This patch introduces the new constructor table formats for snapshots and exception stores. This allows for new exception store types and allows for easy addition of future features to snapshots.

---
 drivers/md/dm-snap-persistent.c |    9 +++++++--
 drivers/md/dm-snap-transient.c  |    9 +++++++--
 drivers/md/dm-snap.c            |   36 +++++++++++++++++++++++++++++-------
 3 files changed, 43 insertions(+), 11 deletions(-)
drivers/md/dm-snap-persistent.c +7 -2
index e69d632..7887e25 100644
@@ -722,8 +722,13 @@ static unsigned persistent_status(struct dm_exception_store *store,
 	case STATUSTYPE_INFO:
 		break;
 	case STATUSTYPE_TABLE:
-		DMEMIT(" %s P %llu", store->cow->name,
-		       (unsigned long long)store->chunk_size);
+		if (!strcmp("P", store->type->name))
+			DMEMIT(" %s P %llu", store->cow->name,
+			       (unsigned long long)store->chunk_size);
+		else
+			DMEMIT(" %s 2 %s %llu", store->type->name,
+			       store->cow->name,
+			       (unsigned long long)store->chunk_size);
 	}
 
 	return sz;
drivers/md/dm-snap-transient.c +7 -2
index cde5aa5..786d96d 100644
@@ -91,8 +91,13 @@ static unsigned transient_status(struct dm_exception_store *store,
 	case STATUSTYPE_INFO:
 		break;
 	case STATUSTYPE_TABLE:
-		DMEMIT(" %s N %llu", store->cow->name,
-		       (unsigned long long)store->chunk_size);
+		if (!strcmp("N", store->type->name))
+			DMEMIT(" %s N %llu", store->cow->name,
+			       (unsigned long long)store->chunk_size);
+		else
+			DMEMIT(" %s 2 %s %llu", store->type->name,
+			       store->cow->name,
+			       (unsigned long long)store->chunk_size);
 	}
 
 	return sz;
drivers/md/dm-snap.c +29 -7
index 109fbea..b3cb063 100644
@@ -587,7 +587,10 @@ static int init_hash_tables(struct dm_snapshot *s)
  * @store: contains newly allocated dm_exception_store
  *
  * Possible formats for argv::
+ * Backwards compatibility mode:
  *     <COW-dev> p/n <chunk-size>
+ * Current format:
+ *     <store type> <arg count> <COW> <chunk-size> [other args]
  *
  * Returns: 0 on success, -Exxx on error
  */
@@ -595,12 +598,14 @@ static int create_exception_store(struct dm_target *ti, unsigned argc,
 				  char **argv, unsigned *args_used,
 				  struct dm_exception_store **store)
 {
+	unsigned param_count;
 	char *tmp_argv[2];
 	char old_type_name[2];
 
 	*store = NULL;
 
-	if (1 /* less change patch to patch */) {
+	/* Detect old-style table line with type as second arg. */
+	if (!isdigit(*argv[1])) {
 		if (argc < 3) {
 			ti->error = "Insufficient exception store arguments";
 			return -EINVAL;
@@ -616,10 +621,29 @@ static int create_exception_store(struct dm_target *ti, unsigned argc,
 		return dm_exception_store_create(old_type_name, ti, 2,
 						 tmp_argv, store);
 	}
+
+	if (sscanf(argv[1], "%u", &param_count) != 1) {
+		ti->error = "Invalid exception store argument count";
+		return -EINVAL;
+	}
+
+	*args_used = 2 + param_count;
+
+	if (argc < *args_used) {
+		ti->error = "Insufficient exception store arguments";
+		return -EINVAL;
+	}
+
+	return dm_exception_store_create(argv[0], ti, param_count,
+					 argv + 2, store);
 }
 
 /*
- * Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size>
+ * Construct a snapshot mapping.  Possible mapping tables include:
+ *     <origin_dev> <exception store args> <feature args>
+ * See 'create_exception_store' for format of <exception store args>.
+ *
+ * Returns: 0 on success, -XXX on error
  */
 static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
 {
@@ -630,10 +654,9 @@ static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
 	struct dm_exception_store *store;
 	unsigned args_used;
 
-	if (argc != 4) {
-		ti->error = "requires exactly 4 arguments";
-		r = -EINVAL;
-		goto bad_args;
+	if (argc < 4) {
+		ti->error = "too few arguments";
+		return -EINVAL;
 	}
 
 	origin_path = argv[0];
@@ -755,7 +778,6 @@ bad_origin:
 bad_snap:
 	dm_exception_store_destroy(store);
 
-bad_args:
 	return r;
 }
 
Signature · 1 line
1.6.2.5
#5 lore raw

PATCH 4/18 dm-snapshot-rename-dm_snap_exception-to-dm_exception

In reply to Mike Snitzer

From: Jon Brassow <[email protected]>

The exception structure is not necessarily just a snapshot element (especially after we pull it out of dm-snap.c).

Renaming appropriately.

---
 drivers/md/dm-exception-store.h |   14 +++++++-------
 drivers/md/dm-snap-persistent.c |    4 ++--
 drivers/md/dm-snap-transient.c  |    4 ++--
 drivers/md/dm-snap.c            |   34 +++++++++++++++++-----------------
 4 files changed, 28 insertions(+), 28 deletions(-)
drivers/md/dm-exception-store.h +7 -7
index 2110e3b..4a33dd0 100644
@@ -26,7 +26,7 @@ typedef sector_t chunk_t;
  * of chunks that follow contiguously.  Remaining bits hold the number of the
  * chunk within the device.
  */
-struct dm_snap_exception {
+struct dm_exception {
 	struct list_head hash_list;
 
 	chunk_t old_chunk;
@@ -64,13 +64,13 @@ struct dm_exception_store_type {
 	 * Find somewhere to store the next exception.
 	 */
 	int (*prepare_exception) (struct dm_exception_store *store,
-				  struct dm_snap_exception *e);
+				  struct dm_exception *e);
 
 	/*
 	 * Update the metadata with this exception.
 	 */
 	void (*commit_exception) (struct dm_exception_store *store,
-				  struct dm_snap_exception *e,
+				  struct dm_exception *e,
 				  void (*callback) (void *, int success),
 				  void *callback_context);
 
@@ -120,12 +120,12 @@ static inline chunk_t dm_chunk_number(chunk_t chunk)
 	return chunk & (chunk_t)((1ULL << DM_CHUNK_NUMBER_BITS) - 1ULL);
 }
 
-static inline unsigned dm_consecutive_chunk_count(struct dm_snap_exception *e)
+static inline unsigned dm_consecutive_chunk_count(struct dm_exception *e)
 {
 	return e->new_chunk >> DM_CHUNK_NUMBER_BITS;
 }
 
-static inline void dm_consecutive_chunk_count_inc(struct dm_snap_exception *e)
+static inline void dm_consecutive_chunk_count_inc(struct dm_exception *e)
 {
 	e->new_chunk += (1ULL << DM_CHUNK_NUMBER_BITS);
 
@@ -140,12 +140,12 @@ static inline chunk_t dm_chunk_number(chunk_t chunk)
 	return chunk;
 }
 
-static inline unsigned dm_consecutive_chunk_count(struct dm_snap_exception *e)
+static inline unsigned dm_consecutive_chunk_count(struct dm_exception *e)
 {
 	return 0;
 }
 
-static inline void dm_consecutive_chunk_count_inc(struct dm_snap_exception *e)
+static inline void dm_consecutive_chunk_count_inc(struct dm_exception *e)
 {
 }
 
drivers/md/dm-snap-persistent.c +2 -2
index 7887e25..d181957 100644
@@ -580,7 +580,7 @@ static int persistent_read_metadata(struct dm_exception_store *store,
 }
 
 static int persistent_prepare_exception(struct dm_exception_store *store,
-					struct dm_snap_exception *e)
+					struct dm_exception *e)
 {
 	struct pstore *ps = get_info(store);
 	uint32_t stride;
@@ -607,7 +607,7 @@ static int persistent_prepare_exception(struct dm_exception_store *store,
 }
 
 static void persistent_commit_exception(struct dm_exception_store *store,
-					struct dm_snap_exception *e,
+					struct dm_exception *e,
 					void (*callback) (void *, int success),
 					void *callback_context)
 {
drivers/md/dm-snap-transient.c +2 -2
index 786d96d..f2ec79c 100644
@@ -36,7 +36,7 @@ static int transient_read_metadata(struct dm_exception_store *store,
 }
 
 static int transient_prepare_exception(struct dm_exception_store *store,
-				       struct dm_snap_exception *e)
+				       struct dm_exception *e)
 {
 	struct transient_c *tc = store->context;
 	sector_t size = get_dev_size(store->cow->bdev);
@@ -51,7 +51,7 @@ static int transient_prepare_exception(struct dm_exception_store *store,
 }
 
 static void transient_commit_exception(struct dm_exception_store *store,
-				       struct dm_snap_exception *e,
+				       struct dm_exception *e,
 				       void (*callback) (void *, int success),
 				       void *callback_context)
 {
drivers/md/dm-snap.c +17 -17
index b3cb063..2da5b2b 100644
@@ -117,7 +117,7 @@ static int bdev_equal(struct block_device *lhs, struct block_device *rhs)
 }
 
 struct dm_snap_pending_exception {
-	struct dm_snap_exception e;
+	struct dm_exception e;
 
 	/*
 	 * Origin buffers waiting for this to complete are held
@@ -367,7 +367,7 @@ static int init_exception_table(struct exception_table *et, uint32_t size,
 static void exit_exception_table(struct exception_table *et, struct kmem_cache *mem)
 {
 	struct list_head *slot;
-	struct dm_snap_exception *ex, *next;
+	struct dm_exception *ex, *next;
 	int i, size;
 
 	size = et->hash_mask + 1;
@@ -386,7 +386,7 @@ static uint32_t exception_hash(struct exception_table *et, chunk_t chunk)
 	return (chunk >> et->hash_shift) & et->hash_mask;
 }
 
-static void remove_exception(struct dm_snap_exception *e)
+static void remove_exception(struct dm_exception *e)
 {
 	list_del(&e->hash_list);
 }
@@ -395,11 +395,11 @@ static void remove_exception(struct dm_snap_exception *e)
  * Return the exception data for a sector, or NULL if not
  * remapped.
  */
-static struct dm_snap_exception *lookup_exception(struct exception_table *et,
+static struct dm_exception *lookup_exception(struct exception_table *et,
 						  chunk_t chunk)
 {
 	struct list_head *slot;
-	struct dm_snap_exception *e;
+	struct dm_exception *e;
 
 	slot = &et->table[exception_hash(et, chunk)];
 	list_for_each_entry (e, slot, hash_list)
@@ -410,9 +410,9 @@ static struct dm_snap_exception *lookup_exception(struct exception_table *et,
 	return NULL;
 }
 
-static struct dm_snap_exception *alloc_exception(void)
+static struct dm_exception *alloc_exception(void)
 {
-	struct dm_snap_exception *e;
+	struct dm_exception *e;
 
 	e = kmem_cache_alloc(exception_cache, GFP_NOIO);
 	if (!e)
@@ -421,7 +421,7 @@ static struct dm_snap_exception *alloc_exception(void)
 	return e;
 }
 
-static void free_exception(struct dm_snap_exception *e)
+static void free_exception(struct dm_exception *e)
 {
 	kmem_cache_free(exception_cache, e);
 }
@@ -447,10 +447,10 @@ static void free_pending_exception(struct dm_snap_pending_exception *pe)
 }
 
 static void insert_exception(struct exception_table *eh,
-			     struct dm_snap_exception *new_e)
+			     struct dm_exception *new_e)
 {
 	struct list_head *l;
-	struct dm_snap_exception *e = NULL;
+	struct dm_exception *e = NULL;
 
 	l = &eh->table[exception_hash(eh, new_e->old_chunk)];
 
@@ -495,7 +495,7 @@ out:
 static int dm_add_exception(void *context, chunk_t old, chunk_t new)
 {
 	struct dm_snapshot *s = context;
-	struct dm_snap_exception *e;
+	struct dm_exception *e;
 
 	e = alloc_exception();
 	if (!e)
@@ -926,7 +926,7 @@ static struct bio *put_pending_exception(struct dm_snap_pending_exception *pe)
 
 static void pending_complete(struct dm_snap_pending_exception *pe, int success)
 {
-	struct dm_snap_exception *e;
+	struct dm_exception *e;
 	struct dm_snapshot *s = pe->snap;
 	struct bio *origin_bios = NULL;
 	struct bio *snapshot_bios = NULL;
@@ -1038,7 +1038,7 @@ static void start_copy(struct dm_snap_pending_exception *pe)
 static struct dm_snap_pending_exception *
 __lookup_pending_exception(struct dm_snapshot *s, chunk_t chunk)
 {
-	struct dm_snap_exception *e = lookup_exception(&s->pending, chunk);
+	struct dm_exception *e = lookup_exception(&s->pending, chunk);
 
 	if (!e)
 		return NULL;
@@ -1084,7 +1084,7 @@ __find_pending_exception(struct dm_snapshot *s,
 	return pe;
 }
 
-static void remap_exception(struct dm_snapshot *s, struct dm_snap_exception *e,
+static void remap_exception(struct dm_snapshot *s, struct dm_exception *e,
 			    struct bio *bio, chunk_t chunk)
 {
 	bio->bi_bdev = s->store->cow->bdev;
@@ -1098,7 +1098,7 @@ static void remap_exception(struct dm_snapshot *s, struct dm_snap_exception *e,
 static int snapshot_map(struct dm_target *ti, struct bio *bio,
 			union map_info *map_context)
 {
-	struct dm_snap_exception *e;
+	struct dm_exception *e;
 	struct dm_snapshot *s = ti->private;
 	int r = DM_MAPIO_REMAPPED;
 	chunk_t chunk;
@@ -1265,7 +1265,7 @@ static int __origin_write(struct list_head *snapshots, struct bio *bio)
 {
 	int r = DM_MAPIO_REMAPPED, first = 0;
 	struct dm_snapshot *snap;
-	struct dm_snap_exception *e;
+	struct dm_exception *e;
 	struct dm_snap_pending_exception *pe, *next_pe, *primary_pe = NULL;
 	chunk_t chunk;
 	LIST_HEAD(pe_queue);
@@ -1544,7 +1544,7 @@ static int __init dm_snapshot_init(void)
 		goto bad2;
 	}
 
-	exception_cache = KMEM_CACHE(dm_snap_exception, 0);
+	exception_cache = KMEM_CACHE(dm_exception, 0);
 	if (!exception_cache) {
 		DMERR("Couldn't create exception cache.");
 		r = -ENOMEM;
Signature · 1 line
1.6.2.5
#6 lore raw

PATCH 5/18 dm-snapshot-rename-exception_table-to-dm_exception_table

In reply to Mike Snitzer

From: Jon Brassow <[email protected]>

Rename exception_table for broader use outside dm-snap.c

---
 drivers/md/dm-snap.c |   17 +++++++++--------
 1 files changed, 9 insertions(+), 8 deletions(-)
drivers/md/dm-snap.c +9 -8
index 2da5b2b..69fc1f5 100644
@@ -50,7 +50,7 @@
 #define DM_TRACKED_CHUNK_HASH(x)	((unsigned long)(x) & \
 					 (DM_TRACKED_CHUNK_HASH_SIZE - 1))
 
-struct exception_table {
+struct dm_exception_table {
 	uint32_t hash_mask;
 	unsigned hash_shift;
 	struct list_head *table;
@@ -74,8 +74,8 @@ struct dm_snapshot {
 
 	atomic_t pending_exceptions_count;
 
-	struct exception_table pending;
-	struct exception_table complete;
+	struct dm_exception_table pending;
+	struct dm_exception_table complete;
 
 	/*
 	 * pe_lock protects all pending_exception operations and access
@@ -347,7 +347,7 @@ static void unregister_snapshot(struct dm_snapshot *s)
  * The lowest hash_shift bits of the chunk number are ignored, allowing
  * some consecutive chunks to be grouped together.
  */
-static int init_exception_table(struct exception_table *et, uint32_t size,
+static int init_exception_table(struct dm_exception_table *et, uint32_t size,
 				unsigned hash_shift)
 {
 	unsigned int i;
@@ -364,7 +364,8 @@ static int init_exception_table(struct exception_table *et, uint32_t size,
 	return 0;
 }
 
-static void exit_exception_table(struct exception_table *et, struct kmem_cache *mem)
+static void exit_exception_table(struct dm_exception_table *et,
+				 struct kmem_cache *mem)
 {
 	struct list_head *slot;
 	struct dm_exception *ex, *next;
@@ -381,7 +382,7 @@ static void exit_exception_table(struct exception_table *et, struct kmem_cache *
 	vfree(et->table);
 }
 
-static uint32_t exception_hash(struct exception_table *et, chunk_t chunk)
+static uint32_t exception_hash(struct dm_exception_table *et, chunk_t chunk)
 {
 	return (chunk >> et->hash_shift) & et->hash_mask;
 }
@@ -395,7 +396,7 @@ static void remove_exception(struct dm_exception *e)
  * Return the exception data for a sector, or NULL if not
  * remapped.
  */
-static struct dm_exception *lookup_exception(struct exception_table *et,
+static struct dm_exception *lookup_exception(struct dm_exception_table *et,
 						  chunk_t chunk)
 {
 	struct list_head *slot;
@@ -446,7 +447,7 @@ static void free_pending_exception(struct dm_snap_pending_exception *pe)
 	atomic_dec(&s->pending_exceptions_count);
 }
 
-static void insert_exception(struct exception_table *eh,
+static void insert_exception(struct dm_exception_table *eh,
 			     struct dm_exception *new_e)
 {
 	struct list_head *l;
Signature · 1 line
1.6.2.5
#7 lore raw

PATCH 6/18 dm-snapshot-rename-exception-functions

In reply to Mike Snitzer

From: Jon Brassow <[email protected]>

Rename exception functions. Preparing to pull them out of dm-snap.c for broader use.

---
 drivers/md/dm-snap.c |   64 +++++++++++++++++++++++++-------------------------
 1 files changed, 32 insertions(+), 32 deletions(-)
drivers/md/dm-snap.c +32 -32
index 69fc1f5..7d6a02f 100644
@@ -347,8 +347,8 @@ static void unregister_snapshot(struct dm_snapshot *s)
  * The lowest hash_shift bits of the chunk number are ignored, allowing
  * some consecutive chunks to be grouped together.
  */
-static int init_exception_table(struct dm_exception_table *et, uint32_t size,
-				unsigned hash_shift)
+static int dm_exception_table_init(struct dm_exception_table *et,
+				   uint32_t size, unsigned hash_shift)
 {
 	unsigned int i;
 
@@ -364,8 +364,8 @@ static int init_exception_table(struct dm_exception_table *et, uint32_t size,
 	return 0;
 }
 
-static void exit_exception_table(struct dm_exception_table *et,
-				 struct kmem_cache *mem)
+static void dm_exception_table_exit(struct dm_exception_table *et,
+				    struct kmem_cache *mem)
 {
 	struct list_head *slot;
 	struct dm_exception *ex, *next;
@@ -387,7 +387,7 @@ static uint32_t exception_hash(struct dm_exception_table *et, chunk_t chunk)
 	return (chunk >> et->hash_shift) & et->hash_mask;
 }
 
-static void remove_exception(struct dm_exception *e)
+static void dm_remove_exception(struct dm_exception *e)
 {
 	list_del(&e->hash_list);
 }
@@ -396,8 +396,8 @@ static void remove_exception(struct dm_exception *e)
  * Return the exception data for a sector, or NULL if not
  * remapped.
  */
-static struct dm_exception *lookup_exception(struct dm_exception_table *et,
-						  chunk_t chunk)
+static struct dm_exception *dm_lookup_exception(struct dm_exception_table *et,
+						chunk_t chunk)
 {
 	struct list_head *slot;
 	struct dm_exception *e;
@@ -411,7 +411,7 @@ static struct dm_exception *lookup_exception(struct dm_exception_table *et,
 	return NULL;
 }
 
-static struct dm_exception *alloc_exception(void)
+static struct dm_exception *alloc_completed_exception(void)
 {
 	struct dm_exception *e;
 
@@ -422,7 +422,7 @@ static struct dm_exception *alloc_exception(void)
 	return e;
 }
 
-static void free_exception(struct dm_exception *e)
+static void free_completed_exception(struct dm_exception *e)
 {
 	kmem_cache_free(exception_cache, e);
 }
@@ -447,8 +447,8 @@ static void free_pending_exception(struct dm_snap_pending_exception *pe)
 	atomic_dec(&s->pending_exceptions_count);
 }
 
-static void insert_exception(struct dm_exception_table *eh,
-			     struct dm_exception *new_e)
+static void dm_insert_exception(struct dm_exception_table *eh,
+				struct dm_exception *new_e)
 {
 	struct list_head *l;
 	struct dm_exception *e = NULL;
@@ -467,7 +467,7 @@ static void insert_exception(struct dm_exception_table *eh,
 		    new_e->new_chunk == (dm_chunk_number(e->new_chunk) +
 					 dm_consecutive_chunk_count(e) + 1)) {
 			dm_consecutive_chunk_count_inc(e);
-			free_exception(new_e);
+			free_completed_exception(new_e);
 			return;
 		}
 
@@ -498,7 +498,7 @@ static int dm_add_exception(void *context, chunk_t old, chunk_t new)
 	struct dm_snapshot *s = context;
 	struct dm_exception *e;
 
-	e = alloc_exception();
+	e = alloc_completed_exception();
 	if (!e)
 		return -ENOMEM;
 
@@ -507,7 +507,7 @@ static int dm_add_exception(void *context, chunk_t old, chunk_t new)
 	/* Consecutive_count is implicitly initialised to zero */
 	e->new_chunk = new;
 
-	insert_exception(&s->complete, e);
+	dm_insert_exception(&s->complete, e);
 
 	return 0;
 }
@@ -562,8 +562,8 @@ static int init_hash_tables(struct dm_snapshot *s)
 	hash_size = min(hash_size, max_buckets);
 
 	hash_size = rounddown_pow_of_two(hash_size);
-	if (init_exception_table(&s->complete, hash_size,
-				 DM_CHUNK_CONSECUTIVE_BITS))
+	if (dm_exception_table_init(&s->complete, hash_size,
+				    DM_CHUNK_CONSECUTIVE_BITS))
 		return -ENOMEM;
 
 	/*
@@ -574,8 +574,8 @@ static int init_hash_tables(struct dm_snapshot *s)
 	if (hash_size < 64)
 		hash_size = 64;
 
-	if (init_exception_table(&s->pending, hash_size, 0)) {
-		exit_exception_table(&s->complete, exception_cache);
+	if (dm_exception_table_init(&s->pending, hash_size, 0)) {
+		dm_exception_table_exit(&s->complete, exception_cache);
 		return -ENOMEM;
 	}
 
@@ -767,8 +767,8 @@ bad_pending_pool:
 	dm_kcopyd_client_destroy(s->kcopyd_client);
 
 bad_kcopyd:
-	exit_exception_table(&s->pending, pending_cache);
-	exit_exception_table(&s->complete, exception_cache);
+	dm_exception_table_exit(&s->pending, pending_cache);
+	dm_exception_table_exit(&s->complete, exception_cache);
 
 bad_hash_tables:
 	dm_put_device(ti, s->origin);
@@ -787,8 +787,8 @@ static void __free_exceptions(struct dm_snapshot *s)
 	dm_kcopyd_client_destroy(s->kcopyd_client);
 	s->kcopyd_client = NULL;
 
-	exit_exception_table(&s->pending, pending_cache);
-	exit_exception_table(&s->complete, exception_cache);
+	dm_exception_table_exit(&s->pending, pending_cache);
+	dm_exception_table_exit(&s->complete, exception_cache);
 }
 
 static void snapshot_dtr(struct dm_target *ti)
@@ -941,7 +941,7 @@ static void pending_complete(struct dm_snap_pending_exception *pe, int success)
 		goto out;
 	}
 
-	e = alloc_exception();
+	e = alloc_completed_exception();
 	if (!e) {
 		down_write(&s->lock);
 		__invalidate_snapshot(s, -ENOMEM);
@@ -952,7 +952,7 @@ static void pending_complete(struct dm_snap_pending_exception *pe, int success)
 
 	down_write(&s->lock);
 	if (!s->valid) {
-		free_exception(e);
+		free_completed_exception(e);
 		error = 1;
 		goto out;
 	}
@@ -968,10 +968,10 @@ static void pending_complete(struct dm_snap_pending_exception *pe, int success)
 	 * Add a proper exception, and remove the
 	 * in-flight exception from the list.
 	 */
-	insert_exception(&s->complete, e);
+	dm_insert_exception(&s->complete, e);
 
  out:
-	remove_exception(&pe->e);
+	dm_remove_exception(&pe->e);
 	snapshot_bios = bio_list_get(&pe->snapshot_bios);
 	origin_bios = put_pending_exception(pe);
 
@@ -1039,7 +1039,7 @@ static void start_copy(struct dm_snap_pending_exception *pe)
 static struct dm_snap_pending_exception *
 __lookup_pending_exception(struct dm_snapshot *s, chunk_t chunk)
 {
-	struct dm_exception *e = lookup_exception(&s->pending, chunk);
+	struct dm_exception *e = dm_lookup_exception(&s->pending, chunk);
 
 	if (!e)
 		return NULL;
@@ -1080,7 +1080,7 @@ __find_pending_exception(struct dm_snapshot *s,
 	}
 
 	get_pending_exception(pe);
-	insert_exception(&s->pending, &pe->e);
+	dm_insert_exception(&s->pending, &pe->e);
 
 	return pe;
 }
@@ -1127,7 +1127,7 @@ static int snapshot_map(struct dm_target *ti, struct bio *bio,
 	}
 
 	/* If the block is already remapped - use that, else remap it */
-	e = lookup_exception(&s->complete, chunk);
+	e = dm_lookup_exception(&s->complete, chunk);
 	if (e) {
 		remap_exception(s, e, bio, chunk);
 		goto out_unlock;
@@ -1151,7 +1151,7 @@ static int snapshot_map(struct dm_target *ti, struct bio *bio,
 				goto out_unlock;
 			}
 
-			e = lookup_exception(&s->complete, chunk);
+			e = dm_lookup_exception(&s->complete, chunk);
 			if (e) {
 				free_pending_exception(pe);
 				remap_exception(s, e, bio, chunk);
@@ -1298,7 +1298,7 @@ static int __origin_write(struct list_head *snapshots, struct bio *bio)
 		 * ref_count is initialised to 1 so pending_complete()
 		 * won't destroy the primary_pe while we're inside this loop.
 		 */
-		e = lookup_exception(&snap->complete, chunk);
+		e = dm_lookup_exception(&snap->complete, chunk);
 		if (e)
 			goto next_snapshot;
 
@@ -1313,7 +1313,7 @@ static int __origin_write(struct list_head *snapshots, struct bio *bio)
 				goto next_snapshot;
 			}
 
-			e = lookup_exception(&snap->complete, chunk);
+			e = dm_lookup_exception(&snap->complete, chunk);
 			if (e) {
 				free_pending_exception(pe);
 				goto next_snapshot;
Signature · 1 line
1.6.2.5
#8 lore raw

PATCH 7/18 dm-snapshot-use-allocated-exception-tables

In reply to Mike Snitzer

From: Jon Brassow <[email protected]>

Change to using exception table pointers vs. just using the whole struct from within 'stuct dm_snapshot'. Again, this is to facilitate extraction of the exception table/cache code from dm-snap.c.

---
 drivers/md/dm-snap.c |   61 +++++++++++++++++++++++++++++--------------------
 1 files changed, 36 insertions(+), 25 deletions(-)
drivers/md/dm-snap.c +36 -25
index 7d6a02f..cc04760 100644
@@ -74,8 +74,8 @@ struct dm_snapshot {
 
 	atomic_t pending_exceptions_count;
 
-	struct dm_exception_table pending;
-	struct dm_exception_table complete;
+	struct dm_exception_table *pending;
+	struct dm_exception_table *complete;
 
 	/*
 	 * pe_lock protects all pending_exception operations and access
@@ -347,25 +347,32 @@ static void unregister_snapshot(struct dm_snapshot *s)
  * The lowest hash_shift bits of the chunk number are ignored, allowing
  * some consecutive chunks to be grouped together.
  */
-static int dm_exception_table_init(struct dm_exception_table *et,
-				   uint32_t size, unsigned hash_shift)
+static struct dm_exception_table *
+dm_exception_table_create(uint32_t size, unsigned hash_shift)
 {
 	unsigned int i;
+	struct dm_exception_table *et;
+
+	et = kmalloc(sizeof(*et), GFP_KERNEL);
+	if (!et)
+		return NULL;
 
 	et->hash_shift = hash_shift;
 	et->hash_mask = size - 1;
 	et->table = dm_vcalloc(size, sizeof(struct list_head));
-	if (!et->table)
-		return -ENOMEM;
+	if (!et->table) {
+		kfree(et);
+		return NULL;
+	}
 
 	for (i = 0; i < size; i++)
 		INIT_LIST_HEAD(et->table + i);
 
-	return 0;
+	return et;
 }
 
-static void dm_exception_table_exit(struct dm_exception_table *et,
-				    struct kmem_cache *mem)
+static void dm_exception_table_destroy(struct dm_exception_table *et,
+				       struct kmem_cache *mem)
 {
 	struct list_head *slot;
 	struct dm_exception *ex, *next;
@@ -380,6 +387,7 @@ static void dm_exception_table_exit(struct dm_exception_table *et,
 	}
 
 	vfree(et->table);
+	kfree(et);
 }
 
 static uint32_t exception_hash(struct dm_exception_table *et, chunk_t chunk)
@@ -507,7 +515,7 @@ static int dm_add_exception(void *context, chunk_t old, chunk_t new)
 	/* Consecutive_count is implicitly initialised to zero */
 	e->new_chunk = new;
 
-	dm_insert_exception(&s->complete, e);
+	dm_insert_exception(s->complete, e);
 
 	return 0;
 }
@@ -562,8 +570,10 @@ static int init_hash_tables(struct dm_snapshot *s)
 	hash_size = min(hash_size, max_buckets);
 
 	hash_size = rounddown_pow_of_two(hash_size);
-	if (dm_exception_table_init(&s->complete, hash_size,
-				    DM_CHUNK_CONSECUTIVE_BITS))
+
+	s->complete = dm_exception_table_create(hash_size,
+						DM_CHUNK_CONSECUTIVE_BITS);
+	if (!s->complete)
 		return -ENOMEM;
 
 	/*
@@ -574,8 +584,9 @@ static int init_hash_tables(struct dm_snapshot *s)
 	if (hash_size < 64)
 		hash_size = 64;
 
-	if (dm_exception_table_init(&s->pending, hash_size, 0)) {
-		dm_exception_table_exit(&s->complete, exception_cache);
+	s->pending = dm_exception_table_create(hash_size, 0);
+	if (!s->pending) {
+		dm_exception_table_destroy(s->complete, exception_cache);
 		return -ENOMEM;
 	}
 
@@ -767,8 +778,8 @@ bad_pending_pool:
 	dm_kcopyd_client_destroy(s->kcopyd_client);
 
 bad_kcopyd:
-	dm_exception_table_exit(&s->pending, pending_cache);
-	dm_exception_table_exit(&s->complete, exception_cache);
+	dm_exception_table_destroy(s->pending, pending_cache);
+	dm_exception_table_destroy(s->complete, exception_cache);
 
 bad_hash_tables:
 	dm_put_device(ti, s->origin);
@@ -787,8 +798,8 @@ static void __free_exceptions(struct dm_snapshot *s)
 	dm_kcopyd_client_destroy(s->kcopyd_client);
 	s->kcopyd_client = NULL;
 
-	dm_exception_table_exit(&s->pending, pending_cache);
-	dm_exception_table_exit(&s->complete, exception_cache);
+	dm_exception_table_destroy(s->pending, pending_cache);
+	dm_exception_table_destroy(s->complete, exception_cache);
 }
 
 static void snapshot_dtr(struct dm_target *ti)
@@ -968,7 +979,7 @@ static void pending_complete(struct dm_snap_pending_exception *pe, int success)
 	 * Add a proper exception, and remove the
 	 * in-flight exception from the list.
 	 */
-	dm_insert_exception(&s->complete, e);
+	dm_insert_exception(s->complete, e);
 
  out:
 	dm_remove_exception(&pe->e);
@@ -1039,7 +1050,7 @@ static void start_copy(struct dm_snap_pending_exception *pe)
 static struct dm_snap_pending_exception *
 __lookup_pending_exception(struct dm_snapshot *s, chunk_t chunk)
 {
-	struct dm_exception *e = dm_lookup_exception(&s->pending, chunk);
+	struct dm_exception *e = dm_lookup_exception(s->pending, chunk);
 
 	if (!e)
 		return NULL;
@@ -1080,7 +1091,7 @@ __find_pending_exception(struct dm_snapshot *s,
 	}
 
 	get_pending_exception(pe);
-	dm_insert_exception(&s->pending, &pe->e);
+	dm_insert_exception(s->pending, &pe->e);
 
 	return pe;
 }
@@ -1127,7 +1138,7 @@ static int snapshot_map(struct dm_target *ti, struct bio *bio,
 	}
 
 	/* If the block is already remapped - use that, else remap it */
-	e = dm_lookup_exception(&s->complete, chunk);
+	e = dm_lookup_exception(s->complete, chunk);
 	if (e) {
 		remap_exception(s, e, bio, chunk);
 		goto out_unlock;
@@ -1151,7 +1162,7 @@ static int snapshot_map(struct dm_target *ti, struct bio *bio,
 				goto out_unlock;
 			}
 
-			e = dm_lookup_exception(&s->complete, chunk);
+			e = dm_lookup_exception(s->complete, chunk);
 			if (e) {
 				free_pending_exception(pe);
 				remap_exception(s, e, bio, chunk);
@@ -1298,7 +1309,7 @@ static int __origin_write(struct list_head *snapshots, struct bio *bio)
 		 * ref_count is initialised to 1 so pending_complete()
 		 * won't destroy the primary_pe while we're inside this loop.
 		 */
-		e = dm_lookup_exception(&snap->complete, chunk);
+		e = dm_lookup_exception(snap->complete, chunk);
 		if (e)
 			goto next_snapshot;
 
@@ -1313,7 +1324,7 @@ static int __origin_write(struct list_head *snapshots, struct bio *bio)
 				goto next_snapshot;
 			}
 
-			e = dm_lookup_exception(&snap->complete, chunk);
+			e = dm_lookup_exception(snap->complete, chunk);
 			if (e) {
 				free_pending_exception(pe);
 				goto next_snapshot;
Signature · 1 line
1.6.2.5
#9 lore raw

PATCH 8/18 dm-snapshot-exception-function-changes-1

In reply to Mike Snitzer

From: Jon Brassow <[email protected]>

This patch moves towards a generalized interface for functions that allocate and deallocate exceptions. So, we add an additional parameter for context pointers.

---
 drivers/md/dm-snap.c |   36 +++++++++++++++++++-----------------
 1 files changed, 19 insertions(+), 17 deletions(-)
drivers/md/dm-snap.c +19 -17
index cc04760..807447e 100644
@@ -419,7 +419,7 @@ static struct dm_exception *dm_lookup_exception(struct dm_exception_table *et,
 	return NULL;
 }
 
-static struct dm_exception *alloc_completed_exception(void)
+static struct dm_exception *alloc_completed_exception(void *unused)
 {
 	struct dm_exception *e;
 
@@ -430,13 +430,14 @@ static struct dm_exception *alloc_completed_exception(void)
 	return e;
 }
 
-static void free_completed_exception(struct dm_exception *e)
+static void free_completed_exception(struct dm_exception *e, void *unused)
 {
 	kmem_cache_free(exception_cache, e);
 }
 
-static struct dm_snap_pending_exception *alloc_pending_exception(struct dm_snapshot *s)
+static struct dm_snap_pending_exception *alloc_pending_exception(void *context)
 {
+	struct dm_snapshot *s = context;
 	struct dm_snap_pending_exception *pe = mempool_alloc(s->pending_pool,
 							     GFP_NOIO);
 
@@ -446,7 +447,8 @@ static struct dm_snap_pending_exception *alloc_pending_exception(struct dm_snaps
 	return pe;
 }
 
-static void free_pending_exception(struct dm_snap_pending_exception *pe)
+static void free_pending_exception(struct dm_snap_pending_exception *pe,
+				   void *unused)
 {
 	struct dm_snapshot *s = pe->snap;
 
@@ -475,7 +477,7 @@ static void dm_insert_exception(struct dm_exception_table *eh,
 		    new_e->new_chunk == (dm_chunk_number(e->new_chunk) +
 					 dm_consecutive_chunk_count(e) + 1)) {
 			dm_consecutive_chunk_count_inc(e);
-			free_completed_exception(new_e);
+			free_completed_exception(new_e, NULL);
 			return;
 		}
 
@@ -506,7 +508,7 @@ static int dm_add_exception(void *context, chunk_t old, chunk_t new)
 	struct dm_snapshot *s = context;
 	struct dm_exception *e;
 
-	e = alloc_completed_exception();
+	e = alloc_completed_exception(NULL);
 	if (!e)
 		return -ENOMEM;
 
@@ -923,7 +925,7 @@ static struct bio *put_pending_exception(struct dm_snap_pending_exception *pe)
 	if (primary_pe &&
 	    atomic_dec_and_test(&primary_pe->ref_count)) {
 		origin_bios = bio_list_get(&primary_pe->origin_bios);
-		free_pending_exception(primary_pe);
+		free_pending_exception(primary_pe, NULL);
 	}
 
 	/*
@@ -931,7 +933,7 @@ static struct bio *put_pending_exception(struct dm_snap_pending_exception *pe)
 	 * it's not itself a primary pe.
 	 */
 	if (!primary_pe || primary_pe != pe)
-		free_pending_exception(pe);
+		free_pending_exception(pe, NULL);
 
 	return origin_bios;
 }
@@ -952,7 +954,7 @@ static void pending_complete(struct dm_snap_pending_exception *pe, int success)
 		goto out;
 	}
 
-	e = alloc_completed_exception();
+	e = alloc_completed_exception(NULL);
 	if (!e) {
 		down_write(&s->lock);
 		__invalidate_snapshot(s, -ENOMEM);
@@ -963,7 +965,7 @@ static void pending_complete(struct dm_snap_pending_exception *pe, int success)
 
 	down_write(&s->lock);
 	if (!s->valid) {
-		free_completed_exception(e);
+		free_completed_exception(e, NULL);
 		error = 1;
 		goto out;
 	}
@@ -1074,7 +1076,7 @@ __find_pending_exception(struct dm_snapshot *s,
 
 	pe2 = __lookup_pending_exception(s, chunk);
 	if (pe2) {
-		free_pending_exception(pe);
+		free_pending_exception(pe, NULL);
 		return pe2;
 	}
 
@@ -1086,7 +1088,7 @@ __find_pending_exception(struct dm_snapshot *s,
 	pe->started = 0;
 
 	if (s->store->type->prepare_exception(s->store, &pe->e)) {
-		free_pending_exception(pe);
+		free_pending_exception(pe, NULL);
 		return NULL;
 	}
 
@@ -1157,14 +1159,14 @@ static int snapshot_map(struct dm_target *ti, struct bio *bio,
 			down_write(&s->lock);
 
 			if (!s->valid) {
-				free_pending_exception(pe);
+				free_pending_exception(pe, NULL);
 				r = -EIO;
 				goto out_unlock;
 			}
 
 			e = dm_lookup_exception(s->complete, chunk);
 			if (e) {
-				free_pending_exception(pe);
+				free_pending_exception(pe, NULL);
 				remap_exception(s, e, bio, chunk);
 				goto out_unlock;
 			}
@@ -1320,13 +1322,13 @@ static int __origin_write(struct list_head *snapshots, struct bio *bio)
 			down_write(&snap->lock);
 
 			if (!snap->valid) {
-				free_pending_exception(pe);
+				free_pending_exception(pe, NULL);
 				goto next_snapshot;
 			}
 
 			e = dm_lookup_exception(snap->complete, chunk);
 			if (e) {
-				free_pending_exception(pe);
+				free_pending_exception(pe, NULL);
 				goto next_snapshot;
 			}
 
@@ -1380,7 +1382,7 @@ static int __origin_write(struct list_head *snapshots, struct bio *bio)
 
 	if (first && atomic_dec_and_test(&primary_pe->ref_count)) {
 		flush_bios(bio_list_get(&primary_pe->origin_bios));
-		free_pending_exception(primary_pe);
+		free_pending_exception(primary_pe, NULL);
 		/* If we got here, pe_queue is necessarily empty. */
 		return r;
 	}
Signature · 1 line
1.6.2.5
#10 lore raw

PATCH 9/18 dm-snapshot-exception-function-changes-2

In reply to Mike Snitzer

From: Jon Brassow <[email protected]>

This patch makes exception [de]allocation functions deal specifically with the dm_exception type.

---
 drivers/md/dm-snap.c |   43 +++++++++++++++++++++++++------------------
 1 files changed, 25 insertions(+), 18 deletions(-)
drivers/md/dm-snap.c +25 -18
index 807447e..df8cb32 100644
@@ -435,7 +435,7 @@ static void free_completed_exception(struct dm_exception *e, void *unused)
 	kmem_cache_free(exception_cache, e);
 }
 
-static struct dm_snap_pending_exception *alloc_pending_exception(void *context)
+static struct dm_exception *alloc_pending_exception(void *context)
 {
 	struct dm_snapshot *s = context;
 	struct dm_snap_pending_exception *pe = mempool_alloc(s->pending_pool,
@@ -444,13 +444,16 @@ static struct dm_snap_pending_exception *alloc_pending_exception(void *context)
 	atomic_inc(&s->pending_exceptions_count);
 	pe->snap = s;
 
-	return pe;
+	return &pe->e;
 }
 
-static void free_pending_exception(struct dm_snap_pending_exception *pe,
-				   void *unused)
+static void free_pending_exception(struct dm_exception *e, void *unused)
 {
-	struct dm_snapshot *s = pe->snap;
+	struct dm_snap_pending_exception *pe;
+	struct dm_snapshot *s;
+
+	pe = container_of(e, struct dm_snap_pending_exception, e);
+	s = pe->snap;
 
 	mempool_free(pe, s->pending_pool);
 	smp_mb__before_atomic_dec();
@@ -925,7 +928,7 @@ static struct bio *put_pending_exception(struct dm_snap_pending_exception *pe)
 	if (primary_pe &&
 	    atomic_dec_and_test(&primary_pe->ref_count)) {
 		origin_bios = bio_list_get(&primary_pe->origin_bios);
-		free_pending_exception(primary_pe, NULL);
+		free_pending_exception(&primary_pe->e, NULL);
 	}
 
 	/*
@@ -933,7 +936,7 @@ static struct bio *put_pending_exception(struct dm_snap_pending_exception *pe)
 	 * it's not itself a primary pe.
 	 */
 	if (!primary_pe || primary_pe != pe)
-		free_pending_exception(pe, NULL);
+		free_pending_exception(&pe->e, NULL);
 
 	return origin_bios;
 }
@@ -1076,7 +1079,7 @@ __find_pending_exception(struct dm_snapshot *s,
 
 	pe2 = __lookup_pending_exception(s, chunk);
 	if (pe2) {
-		free_pending_exception(pe, NULL);
+		free_pending_exception(&pe->e, NULL);
 		return pe2;
 	}
 
@@ -1088,7 +1091,7 @@ __find_pending_exception(struct dm_snapshot *s,
 	pe->started = 0;
 
 	if (s->store->type->prepare_exception(s->store, &pe->e)) {
-		free_pending_exception(pe, NULL);
+		free_pending_exception(&pe->e, NULL);
 		return NULL;
 	}
 
@@ -1112,7 +1115,7 @@ static void remap_exception(struct dm_snapshot *s, struct dm_exception *e,
 static int snapshot_map(struct dm_target *ti, struct bio *bio,
 			union map_info *map_context)
 {
-	struct dm_exception *e;
+	struct dm_exception *e, *tmp_e;
 	struct dm_snapshot *s = ti->private;
 	int r = DM_MAPIO_REMAPPED;
 	chunk_t chunk;
@@ -1155,18 +1158,20 @@ static int snapshot_map(struct dm_target *ti, struct bio *bio,
 		pe = __lookup_pending_exception(s, chunk);
 		if (!pe) {
 			up_write(&s->lock);
-			pe = alloc_pending_exception(s);
+			tmp_e = alloc_pending_exception(s);
+			pe = container_of(tmp_e,
+					  struct dm_snap_pending_exception, e);
 			down_write(&s->lock);
 
 			if (!s->valid) {
-				free_pending_exception(pe, NULL);
+				free_pending_exception(&pe->e, NULL);
 				r = -EIO;
 				goto out_unlock;
 			}
 
 			e = dm_lookup_exception(s->complete, chunk);
 			if (e) {
-				free_pending_exception(pe, NULL);
+				free_pending_exception(&pe->e, NULL);
 				remap_exception(s, e, bio, chunk);
 				goto out_unlock;
 			}
@@ -1279,7 +1284,7 @@ static int __origin_write(struct list_head *snapshots, struct bio *bio)
 {
 	int r = DM_MAPIO_REMAPPED, first = 0;
 	struct dm_snapshot *snap;
-	struct dm_exception *e;
+	struct dm_exception *e, *tmp_e;
 	struct dm_snap_pending_exception *pe, *next_pe, *primary_pe = NULL;
 	chunk_t chunk;
 	LIST_HEAD(pe_queue);
@@ -1318,17 +1323,19 @@ static int __origin_write(struct list_head *snapshots, struct bio *bio)
 		pe = __lookup_pending_exception(snap, chunk);
 		if (!pe) {
 			up_write(&snap->lock);
-			pe = alloc_pending_exception(snap);
+			tmp_e = alloc_pending_exception(snap);
+			pe = container_of(tmp_e,
+					  struct dm_snap_pending_exception, e);
 			down_write(&snap->lock);
 
 			if (!snap->valid) {
-				free_pending_exception(pe, NULL);
+				free_pending_exception(&pe->e, NULL);
 				goto next_snapshot;
 			}
 
 			e = dm_lookup_exception(snap->complete, chunk);
 			if (e) {
-				free_pending_exception(pe, NULL);
+				free_pending_exception(&pe->e, NULL);
 				goto next_snapshot;
 			}
 
@@ -1382,7 +1389,7 @@ static int __origin_write(struct list_head *snapshots, struct bio *bio)
 
 	if (first && atomic_dec_and_test(&primary_pe->ref_count)) {
 		flush_bios(bio_list_get(&primary_pe->origin_bios));
-		free_pending_exception(primary_pe, NULL);
+		free_pending_exception(&primary_pe->e, NULL);
 		/* If we got here, pe_queue is necessarily empty. */
 		return r;
 	}
Signature · 1 line
1.6.2.5
#11 lore raw

PATCH 10/18 dm-snapshot-exception-function-changes-3

In reply to Mike Snitzer

From: Jon Brassow <[email protected]>

This patch adds the 'dm_exception_table_internal' structure. It is used to track the [de]allocation functions used for dm_exception's. This is necessary because some functions, like dm_insert_exception and dm_exception_table_destroy must access them to do the right thing when freeing exceptions.

---
 drivers/md/dm-snap.c |   22 +++++++++++++++++++---
 1 files changed, 19 insertions(+), 3 deletions(-)
drivers/md/dm-snap.c +19 -3
index df8cb32..1a664ec 100644
@@ -342,6 +342,16 @@ static void unregister_snapshot(struct dm_snapshot *s)
 	up_write(&_origins_lock);
 }
 
+struct dm_exception_table_internal {
+	struct dm_exception_table et;
+
+	struct dm_exception *(*alloc_exception)(void *context);
+	void *alloc_context;
+
+	void (*free_exception)(struct dm_exception *e, void *context);
+	void *free_context;
+};
+
 /*
  * Implementation of the exception hash tables.
  * The lowest hash_shift bits of the chunk number are ignored, allowing
@@ -351,12 +361,15 @@ static struct dm_exception_table *
 dm_exception_table_create(uint32_t size, unsigned hash_shift)
 {
 	unsigned int i;
+	struct dm_exception_table_internal *eti;
 	struct dm_exception_table *et;
 
-	et = kmalloc(sizeof(*et), GFP_KERNEL);
-	if (!et)
+	eti = kmalloc(sizeof(*eti), GFP_KERNEL);
+	if (!eti)
 		return NULL;
 
+	et = &eti->et;
+
 	et->hash_shift = hash_shift;
 	et->hash_mask = size - 1;
 	et->table = dm_vcalloc(size, sizeof(struct list_head));
@@ -374,10 +387,13 @@ dm_exception_table_create(uint32_t size, unsigned hash_shift)
 static void dm_exception_table_destroy(struct dm_exception_table *et,
 				       struct kmem_cache *mem)
 {
+	struct dm_exception_table_internal *eti;
 	struct list_head *slot;
 	struct dm_exception *ex, *next;
 	int i, size;
 
+	eti = container_of(et, struct dm_exception_table_internal, et);
+
 	size = et->hash_mask + 1;
 	for (i = 0; i < size; i++) {
 		slot = et->table + i;
@@ -387,7 +403,7 @@ static void dm_exception_table_destroy(struct dm_exception_table *et,
 	}
 
 	vfree(et->table);
-	kfree(et);
+	kfree(eti);
 }
 
 static uint32_t exception_hash(struct dm_exception_table *et, chunk_t chunk)
Signature · 1 line
1.6.2.5
#12 lore raw

PATCH 11/18 dm-snapshot-exception-function-changes-4

In reply to Mike Snitzer

From: Jon Brassow <[email protected]>

This patch alters the dm_exception_table_create and dm_exception_table_destroy functions to take in the function pointers for the exception [de]allocation functions.

---
 drivers/md/dm-snap.c |   34 +++++++++++++++++++++++-----------
 1 files changed, 23 insertions(+), 11 deletions(-)
drivers/md/dm-snap.c +23 -11
index 1a664ec..2ec51b6 100644
@@ -358,7 +358,11 @@ struct dm_exception_table_internal {
  * some consecutive chunks to be grouped together.
  */
 static struct dm_exception_table *
-dm_exception_table_create(uint32_t size, unsigned hash_shift)
+dm_exception_table_create(uint32_t size, unsigned hash_shift,
+			  struct dm_exception *(*alloc_exception)(void *),
+			  void *alloc_context,
+			  void (*free_exception)(struct dm_exception *e, void *),
+			  void *free_context)
 {
 	unsigned int i;
 	struct dm_exception_table_internal *eti;
@@ -378,14 +382,18 @@ dm_exception_table_create(uint32_t size, unsigned hash_shift)
 		return NULL;
 	}
 
+	eti->alloc_exception = alloc_exception;
+	eti->alloc_context = alloc_context;
+	eti->free_exception = free_exception;
+	eti->free_context = free_context;
+
 	for (i = 0; i < size; i++)
 		INIT_LIST_HEAD(et->table + i);
 
 	return et;
 }
 
-static void dm_exception_table_destroy(struct dm_exception_table *et,
-				       struct kmem_cache *mem)
+static void dm_exception_table_destroy(struct dm_exception_table *et)
 {
 	struct dm_exception_table_internal *eti;
 	struct list_head *slot;
@@ -399,7 +407,7 @@ static void dm_exception_table_destroy(struct dm_exception_table *et,
 		slot = et->table + i;
 
 		list_for_each_entry_safe (ex, next, slot, hash_list)
-			kmem_cache_free(mem, ex);
+			eti->free_exception(ex, eti->free_context);
 	}
 
 	vfree(et->table);
@@ -593,7 +601,9 @@ static int init_hash_tables(struct dm_snapshot *s)
 	hash_size = rounddown_pow_of_two(hash_size);
 
 	s->complete = dm_exception_table_create(hash_size,
-						DM_CHUNK_CONSECUTIVE_BITS);
+						DM_CHUNK_CONSECUTIVE_BITS,
+						alloc_completed_exception, NULL,
+						free_completed_exception, NULL);
 	if (!s->complete)
 		return -ENOMEM;
 
@@ -605,9 +615,11 @@ static int init_hash_tables(struct dm_snapshot *s)
 	if (hash_size < 64)
 		hash_size = 64;
 
-	s->pending = dm_exception_table_create(hash_size, 0);
+	s->pending = dm_exception_table_create(hash_size, 0,
+					       alloc_pending_exception, s,
+					       free_pending_exception, NULL);
 	if (!s->pending) {
-		dm_exception_table_destroy(s->complete, exception_cache);
+		dm_exception_table_destroy(s->complete);
 		return -ENOMEM;
 	}
 
@@ -799,8 +811,8 @@ bad_pending_pool:
 	dm_kcopyd_client_destroy(s->kcopyd_client);
 
 bad_kcopyd:
-	dm_exception_table_destroy(s->pending, pending_cache);
-	dm_exception_table_destroy(s->complete, exception_cache);
+	dm_exception_table_destroy(s->pending);
+	dm_exception_table_destroy(s->complete);
 
 bad_hash_tables:
 	dm_put_device(ti, s->origin);
@@ -819,8 +831,8 @@ static void __free_exceptions(struct dm_snapshot *s)
 	dm_kcopyd_client_destroy(s->kcopyd_client);
 	s->kcopyd_client = NULL;
 
-	dm_exception_table_destroy(s->pending, pending_cache);
-	dm_exception_table_destroy(s->complete, exception_cache);
+	dm_exception_table_destroy(s->pending);
+	dm_exception_table_destroy(s->complete);
 }
 
 static void snapshot_dtr(struct dm_target *ti)
Signature · 1 line
1.6.2.5
#13 lore raw

PATCH 12/18 dm-snapshot-exception-function-changes-5

In reply to Mike Snitzer

From: Jon Brassow <[email protected]>

This patch introduces the dm_alloc_exception and dm_free_exception functions. They are now the official way to [de]allocate exceptions via the new interface. These functions call the [de]allocation functions specified during the creation of the exception table and use the context provided at that time.

---
 drivers/md/dm-snap.c |   27 +++++++++++++++++++++++----
 1 files changed, 23 insertions(+), 4 deletions(-)
drivers/md/dm-snap.c +23 -4
index 2ec51b6..fabf833 100644
@@ -443,6 +443,25 @@ static struct dm_exception *dm_lookup_exception(struct dm_exception_table *et,
 	return NULL;
 }
 
+static struct dm_exception *dm_alloc_exception(struct dm_exception_table *et)
+{
+	struct dm_exception_table_internal *eti;
+
+	eti = container_of(et, struct dm_exception_table_internal, et);
+
+	return eti->alloc_exception(eti->alloc_context);
+}
+
+static void dm_free_exception(struct dm_exception_table *et,
+			      struct dm_exception *e)
+{
+	struct dm_exception_table_internal *eti;
+
+	eti = container_of(et, struct dm_exception_table_internal, et);
+
+	return eti->free_exception(e, eti->free_context);
+}
+
 static struct dm_exception *alloc_completed_exception(void *unused)
 {
 	struct dm_exception *e;
@@ -504,7 +523,7 @@ static void dm_insert_exception(struct dm_exception_table *eh,
 		    new_e->new_chunk == (dm_chunk_number(e->new_chunk) +
 					 dm_consecutive_chunk_count(e) + 1)) {
 			dm_consecutive_chunk_count_inc(e);
-			free_completed_exception(new_e, NULL);
+			dm_free_exception(eh, new_e);
 			return;
 		}
 
@@ -535,7 +554,7 @@ static int dm_add_exception(void *context, chunk_t old, chunk_t new)
 	struct dm_snapshot *s = context;
 	struct dm_exception *e;
 
-	e = alloc_completed_exception(NULL);
+	e = dm_alloc_exception(s->complete);
 	if (!e)
 		return -ENOMEM;
 
@@ -985,7 +1004,7 @@ static void pending_complete(struct dm_snap_pending_exception *pe, int success)
 		goto out;
 	}
 
-	e = alloc_completed_exception(NULL);
+	e = dm_alloc_exception(s->complete);
 	if (!e) {
 		down_write(&s->lock);
 		__invalidate_snapshot(s, -ENOMEM);
@@ -996,7 +1015,7 @@ static void pending_complete(struct dm_snap_pending_exception *pe, int success)
 
 	down_write(&s->lock);
 	if (!s->valid) {
-		free_completed_exception(e, NULL);
+		dm_free_exception(s->complete, e);
 		error = 1;
 		goto out;
 	}
Signature · 1 line
1.6.2.5
#14 lore raw

PATCH 13/18 dm-snapshot-exception-function-changes-6

In reply to Mike Snitzer

From: Jon Brassow <[email protected]>

This patch makes use of the new dm_alloc_exception and dm_free_exception functions.

---
 drivers/md/dm-snap.c |   24 +++++++++++++-----------
 1 files changed, 13 insertions(+), 11 deletions(-)
drivers/md/dm-snap.c +13 -11
index fabf833..4c0a5ed 100644
@@ -963,6 +963,7 @@ static void get_pending_exception(struct dm_snap_pending_exception *pe)
 static struct bio *put_pending_exception(struct dm_snap_pending_exception *pe)
 {
 	struct dm_snap_pending_exception *primary_pe;
+	struct dm_snapshot *s = pe->snap;
 	struct bio *origin_bios = NULL;
 
 	primary_pe = pe->primary_pe;
@@ -975,7 +976,7 @@ static struct bio *put_pending_exception(struct dm_snap_pending_exception *pe)
 	if (primary_pe &&
 	    atomic_dec_and_test(&primary_pe->ref_count)) {
 		origin_bios = bio_list_get(&primary_pe->origin_bios);
-		free_pending_exception(&primary_pe->e, NULL);
+		dm_free_exception(s->pending, &primary_pe->e);
 	}
 
 	/*
@@ -983,7 +984,7 @@ static struct bio *put_pending_exception(struct dm_snap_pending_exception *pe)
 	 * it's not itself a primary pe.
 	 */
 	if (!primary_pe || primary_pe != pe)
-		free_pending_exception(&pe->e, NULL);
+		dm_free_exception(s->pending, &pe->e);
 
 	return origin_bios;
 }
@@ -1126,7 +1127,7 @@ __find_pending_exception(struct dm_snapshot *s,
 
 	pe2 = __lookup_pending_exception(s, chunk);
 	if (pe2) {
-		free_pending_exception(&pe->e, NULL);
+		dm_free_exception(s->pending, &pe->e);
 		return pe2;
 	}
 
@@ -1138,7 +1139,7 @@ __find_pending_exception(struct dm_snapshot *s,
 	pe->started = 0;
 
 	if (s->store->type->prepare_exception(s->store, &pe->e)) {
-		free_pending_exception(&pe->e, NULL);
+		dm_free_exception(s->pending, &pe->e);
 		return NULL;
 	}
 
@@ -1205,20 +1206,20 @@ static int snapshot_map(struct dm_target *ti, struct bio *bio,
 		pe = __lookup_pending_exception(s, chunk);
 		if (!pe) {
 			up_write(&s->lock);
-			tmp_e = alloc_pending_exception(s);
+			tmp_e = dm_alloc_exception(s->pending);
 			pe = container_of(tmp_e,
 					  struct dm_snap_pending_exception, e);
 			down_write(&s->lock);
 
 			if (!s->valid) {
-				free_pending_exception(&pe->e, NULL);
+				dm_free_exception(s->pending, &pe->e);
 				r = -EIO;
 				goto out_unlock;
 			}
 
 			e = dm_lookup_exception(s->complete, chunk);
 			if (e) {
-				free_pending_exception(&pe->e, NULL);
+				dm_free_exception(s->pending, &pe->e);
 				remap_exception(s, e, bio, chunk);
 				goto out_unlock;
 			}
@@ -1370,19 +1371,19 @@ static int __origin_write(struct list_head *snapshots, struct bio *bio)
 		pe = __lookup_pending_exception(snap, chunk);
 		if (!pe) {
 			up_write(&snap->lock);
-			tmp_e = alloc_pending_exception(snap);
+			tmp_e = dm_alloc_exception(snap->pending);
 			pe = container_of(tmp_e,
 					  struct dm_snap_pending_exception, e);
 			down_write(&snap->lock);
 
 			if (!snap->valid) {
-				free_pending_exception(&pe->e, NULL);
+				dm_free_exception(snap->pending, &pe->e);
 				goto next_snapshot;
 			}
 
 			e = dm_lookup_exception(snap->complete, chunk);
 			if (e) {
-				free_pending_exception(&pe->e, NULL);
+				dm_free_exception(snap->pending, &pe->e);
 				goto next_snapshot;
 			}
 
@@ -1436,7 +1437,8 @@ static int __origin_write(struct list_head *snapshots, struct bio *bio)
 
 	if (first && atomic_dec_and_test(&primary_pe->ref_count)) {
 		flush_bios(bio_list_get(&primary_pe->origin_bios));
-		free_pending_exception(&primary_pe->e, NULL);
+		dm_free_exception(primary_pe->snap->pending,
+				       &primary_pe->e);
 		/* If we got here, pe_queue is necessarily empty. */
 		return r;
 	}
Signature · 1 line
1.6.2.5
#15 lore raw

PATCH 14/18 dm-snapshot-move-exception-code-to-new-file

In reply to Mike Snitzer

From: Jon Brassow <[email protected]>

Extract exception table/cache code from dm-snap.c so that it can be used elsewhere. I've created the new files, dm-exception.[ch]. I chose against including this code in this dm-exception-store.[ch] files.

---
 drivers/md/Makefile             |    4 +-
 drivers/md/dm-exception-store.h |   64 +---------------
 drivers/md/dm-exception.c       |  163 +++++++++++++++++++++++++++++++++++++
 drivers/md/dm-exception.h       |  104 ++++++++++++++++++++++++
 drivers/md/dm-snap.c            |  168 ---------------------------------------
 5 files changed, 270 insertions(+), 233 deletions(-)
 create mode 100644 drivers/md/dm-exception.c
 create mode 100644 drivers/md/dm-exception.h
drivers/md/Makefile +2 -2
index 1dc4185..7c3023a 100644
@@ -5,8 +5,8 @@
 dm-mod-y	+= dm.o dm-table.o dm-target.o dm-linear.o dm-stripe.o \
 		   dm-ioctl.o dm-io.o dm-kcopyd.o dm-sysfs.o
 dm-multipath-y	+= dm-path-selector.o dm-mpath.o
-dm-snapshot-y	+= dm-snap.o dm-exception-store.o dm-snap-transient.o \
-		    dm-snap-persistent.o
+dm-snapshot-y	+= dm-snap.o dm-exception.o dm-exception-store.o \
+		   dm-snap-transient.o dm-snap-persistent.o
 dm-mirror-y	+= dm-raid1.o
 dm-log-userspace-y \
 		+= dm-log-userspace-base.o dm-log-userspace-transfer.o
drivers/md/dm-exception-store.h +1 -63

76 lines not on this page · raw message

drivers/md/dm-exception.c new file +163 -0

163 lines not on this page · raw message

drivers/md/dm-exception.h new file +104 -0

104 lines not on this page · raw message

drivers/md/dm-snap.c +0 -168

186 lines not on this page · raw message

Signature · 1 line
1.6.2.5
#16 lore raw

PATCH 15/18 dm-exception-store-create-local-exception-caches

In reply to Mike Snitzer

From: Jon Brassow <[email protected]>

This patch puts the cache for the completed exceptions into the exception store implementations. This will allow us to remove the exception cache from dm-snap.c giving flexibility to the exception store implementations and providing a cleaner lookup interface in up-coming patches.

---
 drivers/md/dm-snap-persistent.c |   87 ++++++++++++++++++++++++++++++++++++++-
 drivers/md/dm-snap-transient.c  |   51 ++++++++++++++++++++++-
 2 files changed, 136 insertions(+), 2 deletions(-)
drivers/md/dm-snap-persistent.c +86 -1
index d181957..03b1b6e 100644
@@ -89,6 +89,7 @@ struct commit_callback {
  */
 struct pstore {
 	struct dm_exception_store *store;
+	struct dm_exception_table *table;
 	int version;
 	int valid;
 	uint32_t exceptions_per_area;
@@ -137,6 +138,24 @@ struct pstore {
 	struct workqueue_struct *metadata_wq;
 };
 
+static struct kmem_cache *exception_cache;
+
+static struct dm_exception *alloc_exception(void *unused)
+{
+	struct dm_exception *e;
+
+	e = kmem_cache_alloc(exception_cache, GFP_NOIO);
+	if (!e)
+		e = kmem_cache_alloc(exception_cache, GFP_ATOMIC);
+
+	return e;
+}
+
+static void free_exception(struct dm_exception *e, void *unused)
+{
+	kmem_cache_free(exception_cache, e);
+}
+
 static unsigned sectors_to_pages(unsigned sectors)
 {
 	return DIV_ROUND_UP(sectors, PAGE_SIZE >> 9);
@@ -408,6 +427,22 @@ static void write_exception(struct pstore *ps,
 	e->new_chunk = cpu_to_le64(de->new_chunk);
 }
 
+static int add_exception(struct pstore *ps, chunk_t old, chunk_t new)
+{
+	struct dm_exception *e;
+
+	e = dm_alloc_exception(ps->table);
+	if (!e)
+		return -ENOMEM;
+
+	e->old_chunk = old;
+	e->new_chunk = new;
+
+	dm_insert_exception(ps->table, e);
+
+	return 0;
+}
+
 /*
  * Registers the exceptions that are present in the current area.
  * 'full' is filled in to indicate if the area has been
@@ -448,7 +483,16 @@ static int insert_exceptions(struct pstore *ps,
 			ps->next_free = de.new_chunk + 1;
 
 		/*
-		 * Otherwise we add the exception to the snapshot.
+		 * Add the exception to our local cache
+		 */
+		r = add_exception(ps, de.old_chunk, de.new_chunk);
+		if (r)
+			return r;
+
+		/*
+		 * Redundant until a follow-up patch pulls this out
+		 * (We leave this in for this patch to maintain working
+		 * version between patches.)
 		 */
 		r = callback(callback_context, de.old_chunk, de.new_chunk);
 		if (r)
@@ -511,6 +555,7 @@ static void persistent_dtr(struct dm_exception_store *store)
 	if (ps->callbacks)
 		vfree(ps->callbacks);
 
+	dm_exception_table_destroy(ps->table);
 	kfree(ps);
 }
 
@@ -621,6 +666,15 @@ static void persistent_commit_exception(struct dm_exception_store *store,
 	write_exception(ps, ps->current_committed++, &de);
 
 	/*
+	 * We are safe to add the exception to our cache before we
+	 * issue the callbacks.  If we fail to allocate the memory
+	 * to put it in the cache though, the callbacks will have to
+	 * report the failure.
+	 */
+	if (add_exception(ps, de.old_chunk, de.new_chunk))
+		ps->valid = 0;
+
+	/*
 	 * Add the callback to the back of the array.  This code
 	 * is the only place where the callback array is
 	 * manipulated, and we know that it will never be called
@@ -681,6 +735,7 @@ static int persistent_ctr(struct dm_exception_store *store,
 			  unsigned argc, char **argv)
 {
 	struct pstore *ps;
+	sector_t hash_size, cow_dev_size, max_buckets;
 
 	/* allocate the pstore */
 	ps = kzalloc(sizeof(*ps), GFP_KERNEL);
@@ -700,8 +755,26 @@ static int persistent_ctr(struct dm_exception_store *store,
 	atomic_set(&ps->pending_count, 0);
 	ps->callbacks = NULL;
 
+	cow_dev_size = get_dev_size(store->cow->bdev);
+	max_buckets = (2 * 1024 * 1024)/sizeof(struct list_head);
+
+	hash_size = cow_dev_size >> store->chunk_shift;
+	hash_size = min(hash_size, max_buckets);
+
+	hash_size = rounddown_pow_of_two(hash_size);
+
+	ps->table = dm_exception_table_create(hash_size,
+					      DM_CHUNK_CONSECUTIVE_BITS,
+					      alloc_exception, NULL,
+					      free_exception, NULL);
+	if (!ps->table) {
+		kfree(ps);
+		return -ENOMEM;
+	}
+
 	ps->metadata_wq = create_singlethread_workqueue("ksnaphd");
 	if (!ps->metadata_wq) {
+		dm_exception_table_destroy(ps->table);
 		kfree(ps);
 		DMERR("couldn't start header metadata update thread");
 		return -ENOMEM;
@@ -764,9 +837,19 @@ int dm_persistent_snapshot_init(void)
 {
 	int r;
 
+	exception_cache = kmem_cache_create("persistent_exception_cache",
+					    sizeof(struct dm_exception),
+					    __alignof__(struct dm_exception),
+					    0, NULL);
+	if (!exception_cache) {
+		DMERR("Couldn't create persistent exception cache.");
+		return -ENOMEM;
+	}
+
 	r = dm_exception_store_type_register(&_persistent_type);
 	if (r) {
 		DMERR("Unable to register persistent exception store type");
+		kmem_cache_destroy(exception_cache);
 		return r;
 	}
 
@@ -775,6 +858,7 @@ int dm_persistent_snapshot_init(void)
 		DMERR("Unable to register old-style persistent exception "
 		      "store type");
 		dm_exception_store_type_unregister(&_persistent_type);
+		kmem_cache_destroy(exception_cache);
 		return r;
 	}
 
@@ -785,4 +869,5 @@ void dm_persistent_snapshot_exit(void)
 {
 	dm_exception_store_type_unregister(&_persistent_type);
 	dm_exception_store_type_unregister(&_persistent_compat_type);
+	kmem_cache_destroy(exception_cache);
 }
drivers/md/dm-snap-transient.c +50 -1

80 lines not on this page · raw message

Signature · 1 line
1.6.2.5
#17 lore raw

PATCH 16/18 dm-exception-store-add-lookup_exception-to-API

In reply to Mike Snitzer

From: Jon Brassow <[email protected]>

Now that the exception store implementations are capable of caching the exceptions on their own, we need a new function added to the API to allow us to lookup the exceptions.

Adding 'lookup_exception' to the exception store API.

---
 drivers/md/dm-exception-store.h |   12 +++++++
 drivers/md/dm-snap-persistent.c |   19 ++++++++++++
 drivers/md/dm-snap-transient.c  |   19 ++++++++++++
 drivers/md/dm-snap.c            |   61 ++++++++++++++++++++++++--------------
 4 files changed, 88 insertions(+), 23 deletions(-)
drivers/md/dm-exception-store.h +12 -0
index db22284..d3e6a58 100644
@@ -56,6 +56,18 @@ struct dm_exception_store_type {
 				  void *callback_context);
 
 	/*
+	 * Look up an exception.  Common errors include:
+	 * -ENOENT     : exception not found
+	 * -EWOULDBLOCK: blocking op required and can_block=0
+	 *
+	 * If 'new_chunk' is NULL, the caller simply wants to
+	 * know if the exception exists (0) or not (-ENOENT).
+	 */
+	int (*lookup_exception) (struct dm_exception_store *store,
+				 chunk_t old, chunk_t *new_chunk,
+				 int can_block);
+
+	/*
 	 * The snapshot is invalid, note this in the metadata.
 	 */
 	void (*drop_snapshot) (struct dm_exception_store *store);
drivers/md/dm-snap-persistent.c +19 -0
index 03b1b6e..fe05bc8 100644
@@ -722,6 +722,23 @@ static void persistent_commit_exception(struct dm_exception_store *store,
 	ps->callback_count = 0;
 }
 
+static int persistent_lookup_exception(struct dm_exception_store *store,
+				       chunk_t old, chunk_t *new_chunk,
+				       int can_block)
+{
+	struct pstore *ps = get_info(store);
+	struct dm_exception *e;
+
+	e = dm_lookup_exception(ps->table, old);
+	if (!e)
+		return -ENOENT;
+
+	if (new_chunk)
+		*new_chunk = e->new_chunk + (old - e->old_chunk);
+
+	return 0;
+}
+
 static void persistent_drop_snapshot(struct dm_exception_store *store)
 {
 	struct pstore *ps = get_info(store);
@@ -815,6 +832,7 @@ static struct dm_exception_store_type _persistent_type = {
 	.read_metadata = persistent_read_metadata,
 	.prepare_exception = persistent_prepare_exception,
 	.commit_exception = persistent_commit_exception,
+	.lookup_exception = persistent_lookup_exception,
 	.drop_snapshot = persistent_drop_snapshot,
 	.fraction_full = persistent_fraction_full,
 	.status = persistent_status,
@@ -828,6 +846,7 @@ static struct dm_exception_store_type _persistent_compat_type = {
 	.read_metadata = persistent_read_metadata,
 	.prepare_exception = persistent_prepare_exception,
 	.commit_exception = persistent_commit_exception,
+	.lookup_exception = persistent_lookup_exception,
 	.drop_snapshot = persistent_drop_snapshot,
 	.fraction_full = persistent_fraction_full,
 	.status = persistent_status,
drivers/md/dm-snap-transient.c +19 -0
index d499083..eabdcc1 100644
@@ -90,6 +90,23 @@ static void transient_commit_exception(struct dm_exception_store *store,
 	callback(callback_context, 1);
 }
 
+static int transient_lookup_exception(struct dm_exception_store *store,
+				      chunk_t old, chunk_t *new_chunk,
+				      int can_block)
+{
+	struct transient_c *tc = store->context;
+	struct dm_exception *e;
+
+	e = dm_lookup_exception(tc->table, old);
+	if (!e)
+		return -ENOENT;
+
+	if (new_chunk)
+		*new_chunk = e->new_chunk + (old - e->old_chunk);
+
+	return 0;
+}
+
 static void transient_fraction_full(struct dm_exception_store *store,
 				    sector_t *numerator, sector_t *denominator)
 {
@@ -160,6 +177,8 @@ static struct dm_exception_store_type _transient_type = {
 	.read_metadata = transient_read_metadata,
 	.prepare_exception = transient_prepare_exception,
 	.commit_exception = transient_commit_exception,
+	.lookup_exception = transient_lookup_exception,
+	.lookup_exception = transient_lookup_exception,
 	.fraction_full = transient_fraction_full,
 	.status = transient_status,
 };
drivers/md/dm-snap.c +38 -23

119 lines not on this page · raw message

Signature · 1 line
1.6.2.5
#18 lore raw

PATCH 17/18 dm-snapshot-remove-completed-exception-cache

In reply to Mike Snitzer

From: Jon Brassow <[email protected]>

No need for snapshots to keep track of completed exceptions anymore, since the exception store implementations can do that (better) now.

---
 drivers/md/dm-exception-store.h |    8 +---
 drivers/md/dm-snap-persistent.c |   29 ++----------
 drivers/md/dm-snap-transient.c  |    5 +--
 drivers/md/dm-snap.c            |   95 ++------------------------------------
 4 files changed, 13 insertions(+), 124 deletions(-)
drivers/md/dm-exception-store.h +2 -6
index d3e6a58..f9a54fa 100644
@@ -33,13 +33,9 @@ struct dm_exception_store_type {
 
 	/*
 	 * The target shouldn't read the COW device until this is
-	 * called.  As exceptions are read from the COW, they are
-	 * reported back via the callback.
+	 * called.
 	 */
-	int (*read_metadata) (struct dm_exception_store *store,
-			      int (*callback)(void *callback_context,
-					      chunk_t old, chunk_t new),
-			      void *callback_context);
+	int (*read_metadata) (struct dm_exception_store *store);
 
 	/*
 	 * Find somewhere to store the next exception.
drivers/md/dm-snap-persistent.c +5 -24
index fe05bc8..d920f69 100644
@@ -448,11 +448,7 @@ static int add_exception(struct pstore *ps, chunk_t old, chunk_t new)
  * 'full' is filled in to indicate if the area has been
  * filled.
  */
-static int insert_exceptions(struct pstore *ps,
-			     int (*callback)(void *callback_context,
-					     chunk_t old, chunk_t new),
-			     void *callback_context,
-			     int *full)
+static int insert_exceptions(struct pstore *ps, int *full)
 {
 	int r;
 	unsigned int i;
@@ -488,24 +484,12 @@ static int insert_exceptions(struct pstore *ps,
 		r = add_exception(ps, de.old_chunk, de.new_chunk);
 		if (r)
 			return r;
-
-		/*
-		 * Redundant until a follow-up patch pulls this out
-		 * (We leave this in for this patch to maintain working
-		 * version between patches.)
-		 */
-		r = callback(callback_context, de.old_chunk, de.new_chunk);
-		if (r)
-			return r;
 	}
 
 	return 0;
 }
 
-static int read_exceptions(struct pstore *ps,
-			   int (*callback)(void *callback_context, chunk_t old,
-					   chunk_t new),
-			   void *callback_context)
+static int read_exceptions(struct pstore *ps)
 {
 	int r, full = 1;
 
@@ -518,7 +502,7 @@ static int read_exceptions(struct pstore *ps,
 		if (r)
 			return r;
 
-		r = insert_exceptions(ps, callback, callback_context, &full);
+		r = insert_exceptions(ps, &full);
 		if (r)
 			return r;
 	}
@@ -559,10 +543,7 @@ static void persistent_dtr(struct dm_exception_store *store)
 	kfree(ps);
 }
 
-static int persistent_read_metadata(struct dm_exception_store *store,
-				    int (*callback)(void *callback_context,
-						    chunk_t old, chunk_t new),
-				    void *callback_context)
+static int persistent_read_metadata(struct dm_exception_store *store)
 {
 	int r, uninitialized_var(new_snapshot);
 	struct pstore *ps = get_info(store);
@@ -619,7 +600,7 @@ static int persistent_read_metadata(struct dm_exception_store *store,
 	/*
 	 * Read the metadata.
 	 */
-	r = read_exceptions(ps, callback, callback_context);
+	r = read_exceptions(ps);
 
 	return r;
 }
drivers/md/dm-snap-transient.c +1 -4
index eabdcc1..e410f9c 100644
@@ -44,10 +44,7 @@ static void transient_dtr(struct dm_exception_store *store)
 	kfree(tc);
 }
 
-static int transient_read_metadata(struct dm_exception_store *store,
-				   int (*callback)(void *callback_context,
-						   chunk_t old, chunk_t new),
-				   void *callback_context)
+static int transient_read_metadata(struct dm_exception_store *store)
 {
 	return 0;
 }
drivers/md/dm-snap.c +5 -90

199 lines not on this page · raw message

Signature · 1 line
1.6.2.5
#19 lore raw

PATCH 18/18 dm-exstore-persistent-allow-metadata-reread

In reply to Mike Snitzer

From: Jon Brassow <[email protected]>

This patch allows the persistent exception store to re-read exception data from disk without adding duplicate entries to the cache. Being able to re-read the metadata is especially useful when trying to pick-up new data that may have been added.

This means the persistent exception store can be used as the starting point for a cluster-aware exception store, which leads to shared-storage cluster-aware snapshots.

---
 drivers/md/dm-snap-persistent.c |  129 +++++++++++++++++++++++++++++++-------
 1 files changed, 105 insertions(+), 24 deletions(-)
drivers/md/dm-snap-persistent.c +105 -24
index d920f69..528832a 100644
@@ -299,7 +299,7 @@ static int zero_disk_area(struct pstore *ps, chunk_t area)
 	return chunk_io(ps, ps->zero_area, area_location(ps, area), WRITE, 0);
 }
 
-static int read_header(struct pstore *ps, int *new_snapshot)
+static int initial_read_header(struct pstore *ps, int *new_snapshot)
 {
 	int r;
 	struct disk_header *dh;
@@ -344,7 +344,6 @@ static int read_header(struct pstore *ps, int *new_snapshot)
 		goto bad;
 	}
 
-	*new_snapshot = 0;
 	ps->valid = le32_to_cpu(dh->valid);
 	ps->version = le32_to_cpu(dh->version);
 	chunk_size = le32_to_cpu(dh->chunk_size);
@@ -382,6 +381,51 @@ bad:
 	return r;
 }
 
+/*
+ * Re-reading the header is all about checking the 'valid'
+ * field to ensure that this exception store has not been
+ * marked invalid.  We can also do some sanity checks while
+ * we are at it.
+ */
+static int reread_header(struct pstore *ps)
+{
+	int r;
+	struct disk_header *dh;
+
+	r = chunk_io(ps, ps->header_area, 0, READ, 1);
+	if (r)
+		return r;
+
+	dh = (struct disk_header *) ps->header_area;
+
+	/*
+	 * If any of these conditions is true it means that the
+	 * exception store underneath us has changed in ways we
+	 * cannot handle.  What's worse, if we proceed, we can
+	 * corrupt the exception store.
+	 */
+	if ((le32_to_cpu(dh->magic) != SNAP_MAGIC) ||
+	    (ps->version != le32_to_cpu(dh->version)) ||
+	    (ps->store->chunk_size != le32_to_cpu(dh->chunk_size))) {
+		DMERR("Exception store parameters changed while in use.");
+		ps->valid = 0;
+		return -EINVAL;
+	}
+
+	ps->valid = le32_to_cpu(dh->valid);
+
+	return 0;
+}
+
+static int read_header(struct pstore *ps, int *new_snapshot)
+{
+	*new_snapshot = 0;
+	if (ps->io_client)
+		return reread_header(ps);
+
+	return initial_read_header(ps, new_snapshot);
+}
+
 static int write_header(struct pstore *ps)
 {
 	struct disk_header *dh;
@@ -457,7 +501,18 @@ static int insert_exceptions(struct pstore *ps, int *full)
 	/* presume the area is full */
 	*full = 1;
 
-	for (i = 0; i < ps->exceptions_per_area; i++) {
+	/*
+	 * We start the loop at ps->current_committed.  If this is a full
+	 * read of the metadata area, it will be zero until we
+	 * are finished reading.  If this is a partial read to catch
+	 * any updates to the metadata area, then we will start at
+	 * the position that was last known to be empty.  (We must reset
+	 * ps->current_committed, otherwise metadata updates involving
+	 * more than one area will skip over some exceptions.
+	 */
+	i = ps->current_committed;
+	ps->current_committed = 0;
+	for (; i < ps->exceptions_per_area; i++) {
 		read_exception(ps, i, &de);
 
 		/*
@@ -494,10 +549,12 @@ static int read_exceptions(struct pstore *ps)
 	int r, full = 1;
 
 	/*
-	 * Keeping reading chunks and inserting exceptions until
-	 * we find a partially full area.
+	 * We start from 'ps->current_area' and keep reading chunks
+	 * and inserting exceptions until we find a partially full
+	 * area.  ('ps->current_area' must be initialized to 0 for the
+	 * first complete read.)
 	 */
-	for (ps->current_area = 0; full; ps->current_area++) {
+	for (; full; ps->current_area++) {
 		r = area_io(ps, READ);
 		if (r)
 			return r;
@@ -543,23 +600,17 @@ static void persistent_dtr(struct dm_exception_store *store)
 	kfree(ps);
 }
 
-static int persistent_read_metadata(struct dm_exception_store *store)
+static int do_initial_metadata_read(struct dm_exception_store *store,
+				    int new_snapshot)
 {
-	int r, uninitialized_var(new_snapshot);
+	int r;
 	struct pstore *ps = get_info(store);
 
 	/*
-	 * Read the snapshot header.
-	 */
-	r = read_header(ps, &new_snapshot);
-	if (r)
-		return r;
-
-	/*
 	 * Now we know correct chunk_size, complete the initialisation.
 	 */
 	ps->exceptions_per_area = (ps->store->chunk_size << SECTOR_SHIFT) /
-				  sizeof(struct disk_exception);
+		sizeof(struct disk_exception);
 	ps->callbacks = dm_vcalloc(ps->exceptions_per_area,
 			sizeof(*ps->callbacks));
 	if (!ps->callbacks)
@@ -572,24 +623,48 @@ static int persistent_read_metadata(struct dm_exception_store *store)
 		r = write_header(ps);
 		if (r) {
 			DMWARN("write_header failed");
-			return r;
+			goto fail;
 		}
 
 		ps->current_area = 0;
 		zero_memory_area(ps);
 		r = zero_disk_area(ps, 0);
-		if (r)
+		if (r) {
 			DMWARN("zero_disk_area(0) failed");
-		return r;
+			goto fail;
+		}
 	}
+
 	/*
 	 * Sanity checks.
 	 */
-	if (ps->version != SNAPSHOT_DISK_VERSION) {
-		DMWARN("unable to handle snapshot disk version %d",
-		       ps->version);
-		return -EINVAL;
-	}
+	if (ps->version == SNAPSHOT_DISK_VERSION)
+		return 0;
+
+	DMWARN("unable to handle snapshot disk version %d",
+	       ps->version);
+	r = -EINVAL;
+
+fail:
+	vfree(ps->callbacks);
+	ps->callbacks = NULL;
+	ps->exceptions_per_area = 0;
+	ps->valid = 0;
+
+	return r;
+}
+
+static int persistent_read_metadata(struct dm_exception_store *store)
+{
+	int r, uninitialized_var(new_snapshot);
+	struct pstore *ps = get_info(store);
+
+	/*
+	 * Read the snapshot header.
+	 */
+	r = read_header(ps, &new_snapshot);
+	if (r)
+		return r;
 
 	/*
 	 * Metadata are valid, but snapshot is invalidated
@@ -597,6 +672,12 @@ static int persistent_read_metadata(struct dm_exception_store *store)
 	if (!ps->valid)
 		return 1;
 
+	if (!ps->callbacks) {
+		r = do_initial_metadata_read(store, new_snapshot);
+		if (r)
+			return r;
+	}
+
 	/*
 	 * Read the metadata.
 	 */
Signature · 1 line
1.6.2.5
#20 lore raw

PATCH v2 18/18 dm-exstore-persistent-allow-metadata-reread

This patch allows the persistent exception store to re-read exception data from disk without adding duplicate entries to the cache. Being able to re-read the metadata is especially useful when trying to pick-up new data that may have been added.

This means the persistent exception store can be used as the starting point for a cluster-aware exception store, which leads to shared-storage cluster-aware snapshots.

---
v2: adjusted slightly to account for dm-snapshot-32bit-chunk-size.patch

---
 drivers/md/dm-snap-persistent.c |  129 ++++++++++++++++++++++++++++++++--------
 1 file changed, 105 insertions(+), 24 deletions(-)
drivers/md/dm-snap-persistent.c +105 -24
Index: linux-2.6/drivers/md/dm-snap-persistent.c
===================================================================
@@ -299,7 +299,7 @@ static int zero_disk_area(struct pstore 
 	return chunk_io(ps, ps->zero_area, area_location(ps, area), WRITE, 0);
 }
 
-static int read_header(struct pstore *ps, int *new_snapshot)
+static int initial_read_header(struct pstore *ps, int *new_snapshot)
 {
 	int r;
 	struct disk_header *dh;
@@ -345,7 +345,6 @@ static int read_header(struct pstore *ps
 		goto bad;
 	}
 
-	*new_snapshot = 0;
 	ps->valid = le32_to_cpu(dh->valid);
 	ps->version = le32_to_cpu(dh->version);
 	chunk_size = le32_to_cpu(dh->chunk_size);
@@ -382,6 +381,51 @@ bad:
 	return r;
 }
 
+/*
+ * Re-reading the header is all about checking the 'valid'
+ * field to ensure that this exception store has not been
+ * marked invalid.  We can also do some sanity checks while
+ * we are at it.
+ */
+static int reread_header(struct pstore *ps)
+{
+	int r;
+	struct disk_header *dh;
+
+	r = chunk_io(ps, ps->header_area, 0, READ, 1);
+	if (r)
+		return r;
+
+	dh = (struct disk_header *) ps->header_area;
+
+	/*
+	 * If any of these conditions is true it means that the
+	 * exception store underneath us has changed in ways we
+	 * cannot handle.  What's worse, if we proceed, we can
+	 * corrupt the exception store.
+	 */
+	if ((le32_to_cpu(dh->magic) != SNAP_MAGIC) ||
+	    (ps->version != le32_to_cpu(dh->version)) ||
+	    (ps->store->chunk_size != le32_to_cpu(dh->chunk_size))) {
+		DMERR("Exception store parameters changed while in use.");
+		ps->valid = 0;
+		return -EINVAL;
+	}
+
+	ps->valid = le32_to_cpu(dh->valid);
+
+	return 0;
+}
+
+static int read_header(struct pstore *ps, int *new_snapshot)
+{
+	*new_snapshot = 0;
+	if (ps->io_client)
+		return reread_header(ps);
+
+	return initial_read_header(ps, new_snapshot);
+}
+
 static int write_header(struct pstore *ps)
 {
 	struct disk_header *dh;
@@ -457,7 +501,18 @@ static int insert_exceptions(struct psto
 	/* presume the area is full */
 	*full = 1;
 
-	for (i = 0; i < ps->exceptions_per_area; i++) {
+	/*
+	 * We start the loop at ps->current_committed.  If this is a full
+	 * read of the metadata area, it will be zero until we
+	 * are finished reading.  If this is a partial read to catch
+	 * any updates to the metadata area, then we will start at
+	 * the position that was last known to be empty.  (We must reset
+	 * ps->current_committed, otherwise metadata updates involving
+	 * more than one area will skip over some exceptions.
+	 */
+	i = ps->current_committed;
+	ps->current_committed = 0;
+	for (; i < ps->exceptions_per_area; i++) {
 		read_exception(ps, i, &de);
 
 		/*
@@ -494,10 +549,12 @@ static int read_exceptions(struct pstore
 	int r, full = 1;
 
 	/*
-	 * Keeping reading chunks and inserting exceptions until
-	 * we find a partially full area.
+	 * We start from 'ps->current_area' and keep reading chunks
+	 * and inserting exceptions until we find a partially full
+	 * area.  ('ps->current_area' must be initialized to 0 for the
+	 * first complete read.)
 	 */
-	for (ps->current_area = 0; full; ps->current_area++) {
+	for (; full; ps->current_area++) {
 		r = area_io(ps, READ);
 		if (r)
 			return r;
@@ -543,23 +600,17 @@ static void persistent_dtr(struct dm_exc
 	kfree(ps);
 }
 
-static int persistent_read_metadata(struct dm_exception_store *store)
+static int do_initial_metadata_read(struct dm_exception_store *store,
+				    int new_snapshot)
 {
-	int r, uninitialized_var(new_snapshot);
+	int r;
 	struct pstore *ps = get_info(store);
 
 	/*
-	 * Read the snapshot header.
-	 */
-	r = read_header(ps, &new_snapshot);
-	if (r)
-		return r;
-
-	/*
 	 * Now we know correct chunk_size, complete the initialisation.
 	 */
 	ps->exceptions_per_area = (ps->store->chunk_size << SECTOR_SHIFT) /
-				  sizeof(struct disk_exception);
+		sizeof(struct disk_exception);
 	ps->callbacks = dm_vcalloc(ps->exceptions_per_area,
 			sizeof(*ps->callbacks));
 	if (!ps->callbacks)
@@ -572,24 +623,48 @@ static int persistent_read_metadata(stru
 		r = write_header(ps);
 		if (r) {
 			DMWARN("write_header failed");
-			return r;
+			goto fail;
 		}
 
 		ps->current_area = 0;
 		zero_memory_area(ps);
 		r = zero_disk_area(ps, 0);
-		if (r)
+		if (r) {
 			DMWARN("zero_disk_area(0) failed");
-		return r;
+			goto fail;
+		}
 	}
+
 	/*
 	 * Sanity checks.
 	 */
-	if (ps->version != SNAPSHOT_DISK_VERSION) {
-		DMWARN("unable to handle snapshot disk version %d",
-		       ps->version);
-		return -EINVAL;
-	}
+	if (ps->version == SNAPSHOT_DISK_VERSION)
+		return 0;
+
+	DMWARN("unable to handle snapshot disk version %d",
+	       ps->version);
+	r = -EINVAL;
+
+fail:
+	vfree(ps->callbacks);
+	ps->callbacks = NULL;
+	ps->exceptions_per_area = 0;
+	ps->valid = 0;
+
+	return r;
+}
+
+static int persistent_read_metadata(struct dm_exception_store *store)
+{
+	int r, uninitialized_var(new_snapshot);
+	struct pstore *ps = get_info(store);
+
+	/*
+	 * Read the snapshot header.
+	 */
+	r = read_header(ps, &new_snapshot);
+	if (r)
+		return r;
 
 	/*
 	 * Metadata are valid, but snapshot is invalidated
@@ -597,6 +672,12 @@ static int persistent_read_metadata(stru
 	if (!ps->valid)
 		return 1;
 
+	if (!ps->callbacks) {
+		r = do_initial_metadata_read(store, new_snapshot);
+		if (r)
+			return r;
+	}
+
 	/*
 	 * Read the metadata.
 	 */