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

Permit in-use snapshot exception data to be 'handed over' from one
snapshot instance to another.  This is a pre-requisite for patches
that allow the changes made in a snapshot device to be merged back into
its origin device and also allows device resizing.

The basic call sequence is:

  dmsetup load new_snapshot (referencing the existing in-use cow device)
     - the ctr code detects that the cow is already in use and links the
       two snapshot target instances together
  dmsetup suspend original_snapshot
  dmsetup resume new_snapshot
     - the new_snapshot becomes live, and if anything now tries to access
       the original one it will receive EIO
  dmsetup remove original_snapshot

(There can only be two snapshot targets referencing the same cow device
simultaneously.)

Signed-off-by: Mike Snitzer &lt;snitzer@redhat.com&gt;
Cc: Mikulas Patocka &lt;mpatocka@redhat.com&gt;

---
 drivers/md/dm-snap.c |  236 +++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 228 insertions(+), 8 deletions(-)

Index: linux-2.6.32-rc6/drivers/md/dm-snap.c
===================================================================
--- linux-2.6.32-rc6.orig/drivers/md/dm-snap.c
+++ linux-2.6.32-rc6/drivers/md/dm-snap.c
@@ -75,6 +75,27 @@ struct dm_snapshot {
 	/* Whether or not owning mapped_device is suspended */
 	int suspended;
 
+	/*
+	 * 'is_handover_destination' denotes another snapshot with the same
+	 * cow block device (as identified with find_snapshot_using_cow)
+	 * will hand over its exception store to this snapshot.
+	 *
+	 * 'is_handover_destination' is set in snapshot_ctr if an existing
+	 * snapshot has the same cow device. The handover is performed,
+	 * and 'is_handover_destination' is cleared, when one of the
+	 * following occurs:
+	 * - src (old) snapshot, that is handing over, is destructed
+	 * - src (old) snapshot, that is handing over, is resumed
+	 * - dest (new) snapshot, that is accepting the handover, is resumed
+	 */
+	int is_handover_destination;
+
+	/*
+	 * reference to the other snapshot that will participate in the
+	 * exception store handover; src references dest, dest references src
+	 */
+	struct dm_snapshot *handover_snap;
+
 	mempool_t *pending_pool;
 
 	atomic_t pending_exceptions_count;
@@ -528,6 +549,31 @@ static int dm_add_exception(void *contex
 	return 0;
 }
 
+static struct dm_snapshot *find_snapshot_using_cow(struct dm_snapshot *snap)
+{
+	struct dm_snapshot *s, *handover_snap = NULL;
+	struct origin *o;
+
+	down_read(&amp;_origins_lock);
+
+	o = __lookup_origin(snap-&gt;origin-&gt;bdev);
+	if (!o)
+		goto out;
+
+	list_for_each_entry(s, &amp;o-&gt;snapshots, list) {
+		if (s == snap || !bdev_equal(s-&gt;cow-&gt;bdev, snap-&gt;cow-&gt;bdev))
+			continue;
+
+		handover_snap = s;
+		break;
+	}
+
+out:
+	up_read(&amp;_origins_lock);
+
+	return handover_snap;
+}
+
 #define min_not_zero(l, r) (((l) == 0) ? (r) : (((r) == 0) ? (l) : min(l, r)))
 
 /*
@@ -599,11 +645,64 @@ static int init_hash_tables(struct dm_sn
 }
 
 /*
+ * Reserve snap_src for handover to snap_dest.
+ */
+static int link_snapshots_for_handover(struct dm_snapshot *snap_src,
+				       struct dm_snapshot *snap_dest)
+{
+	int r = -EINVAL;
+
+	down_write(&amp;snap_src-&gt;lock);
+
+	/* Another handover already set? */
+	if (snap_src-&gt;handover_snap)
+		goto out;
+
+	snap_src-&gt;handover_snap = snap_dest;
+
+	snap_dest-&gt;handover_snap = snap_src;
+	snap_dest-&gt;is_handover_destination = 1;
+
+	r = 0;
+
+out:
+	up_write(&amp;snap_src-&gt;lock);
+	return r;
+}
+
+/*
+ * Unreserve snap_src for handover to snap_dest.
+ */
+static int unlink_snapshots_for_handover(struct dm_snapshot *snap_src,
+					 struct dm_snapshot *snap_dest)
+{
+	int r = -EINVAL;
+
+	down_write(&amp;snap_src-&gt;lock);
+
+	/* make sure these snapshots are already linked */
+	if ((snap_src-&gt;handover_snap != snap_dest) ||
+	    (snap_dest-&gt;handover_snap != snap_src))
+		goto out;
+
+	snap_src-&gt;handover_snap = NULL;
+
+	snap_dest-&gt;handover_snap = NULL;
+	snap_dest-&gt;is_handover_destination = 0;
+
+	r = 0;
+
+out:
+	up_write(&amp;snap_src-&gt;lock);
+	return r;
+}
+
+/*
  * Construct a snapshot mapping: &lt;origin_dev&gt; &lt;COW-dev&gt; &lt;p/n&gt; &lt;chunk-size&gt;
  */
 static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
 {
-	struct dm_snapshot *s;
+	struct dm_snapshot *s, *handover_snap;
 	int i;
 	int r = -EINVAL;
 	char *origin_path, *cow_path;
@@ -659,6 +758,8 @@ static int snapshot_ctr(struct dm_target
 	s-&gt;active = 0;
 	s-&gt;suspended = 0;
 	atomic_set(&amp;s-&gt;pending_exceptions_count, 0);
+	s-&gt;is_handover_destination = 0;
+	s-&gt;handover_snap = NULL;
 	init_rwsem(&amp;s-&gt;lock);
 	spin_lock_init(&amp;s-&gt;pe_lock);
 
@@ -694,6 +795,27 @@ static int snapshot_ctr(struct dm_target
 
 	spin_lock_init(&amp;s-&gt;tracked_chunk_lock);
 
+	/* Does snapshot need exceptions handing over to it? */
+	handover_snap = find_snapshot_using_cow(s);
+	if (handover_snap) {
+		r = link_snapshots_for_handover(handover_snap, s);
+		if (r) {
+			ti-&gt;error = "Unable to handover snapshot to "
+				    "two devices at once.";
+			goto bad_load_and_register;
+		}
+	}
+
+	bio_list_init(&amp;s-&gt;queued_bios);
+	INIT_WORK(&amp;s-&gt;queued_bios_work, flush_queued_bios);
+
+	ti-&gt;private = s;
+	ti-&gt;num_flush_requests = 1;
+
+	if (handover_snap)
+		/* defer register_snapshot until after handover_exceptions */
+		return 0;
+
 	/* Metadata must only be loaded into one table at once */
 	r = s-&gt;store-&gt;type-&gt;read_metadata(s-&gt;store, dm_add_exception,
 					  (void *)s);
@@ -705,13 +827,11 @@ static int snapshot_ctr(struct dm_target
 		DMWARN("Snapshot is marked invalid.");
 	}
 
-	bio_list_init(&amp;s-&gt;queued_bios);
-	INIT_WORK(&amp;s-&gt;queued_bios_work, flush_queued_bios);
-
 	if (!s-&gt;store-&gt;chunk_size) {
 		ti-&gt;error = "Chunk size not set";
 		goto bad_load_and_register;
 	}
+	ti-&gt;split_io = s-&gt;store-&gt;chunk_size;
 
 	/* Add snapshot to the list of snapshots for this origin */
 	/* Exceptions aren't triggered till snapshot_resume() is called */
@@ -721,10 +841,6 @@ static int snapshot_ctr(struct dm_target
 		goto bad_load_and_register;
 	}
 
-	ti-&gt;private = s;
-	ti-&gt;split_io = s-&gt;store-&gt;chunk_size;
-	ti-&gt;num_flush_requests = 1;
-
 	return 0;
 
 bad_load_and_register:
@@ -765,15 +881,90 @@ static void __free_exceptions(struct dm_
 	dm_exception_table_exit(&amp;s-&gt;complete, exception_cache);
 }
 
+static void handover_exceptions(struct dm_snapshot *old,
+				struct dm_snapshot *new)
+{
+	union {
+		struct dm_exception_table table_swap;
+		struct dm_exception_store *store_swap;
+	} u;
+
+	BUG_ON((old-&gt;handover_snap != new) ||
+	       (new-&gt;handover_snap != old));
+	BUG_ON((old-&gt;is_handover_destination != 0) ||
+	       (new-&gt;is_handover_destination != 1));
+	BUG_ON(!old-&gt;suspended);
+
+	/* make sure old snapshot is still valid */
+	if (!old-&gt;valid) {
+		new-&gt;valid = 0;
+		DMERR("Unable to handover exceptions "
+		      "from an invalid snapshot.");
+		return;
+	}
+
+	/* swap exceptions tables and stores */
+	u.table_swap = new-&gt;complete;
+	new-&gt;complete = old-&gt;complete;
+	old-&gt;complete = u.table_swap;
+	u.store_swap = new-&gt;store;
+	new-&gt;store = old-&gt;store;
+	old-&gt;store = u.store_swap;
+
+	new-&gt;store-&gt;snap = new;
+	old-&gt;store-&gt;snap = old;
+
+	/* reset split_io to store's chunk_size */
+	if (new-&gt;ti-&gt;split_io != new-&gt;store-&gt;chunk_size)
+		new-&gt;ti-&gt;split_io = new-&gt;store-&gt;chunk_size;
+
+	/* Mark old snapshot invalid and inactive */
+	old-&gt;valid = 0;
+	old-&gt;active = 0;
+
+	/* Reset handover_snap references */
+	old-&gt;handover_snap = NULL;
+	new-&gt;handover_snap = NULL;
+
+	new-&gt;is_handover_destination = 0;
+}
+
 static void snapshot_dtr(struct dm_target *ti)
 {
 #ifdef CONFIG_DM_DEBUG
 	int i;
 #endif
 	struct dm_snapshot *s = ti-&gt;private;
+	struct dm_snapshot *new_snap = NULL;
 
 	flush_workqueue(ksnapd);
 
+	/* This snapshot may need to handover its exception store */
+	down_write(&amp;s-&gt;lock);
+	if (s-&gt;handover_snap) {
+		if (!s-&gt;is_handover_destination) {
+			/* Handover exceptions to another snapshot */
+			new_snap = s-&gt;handover_snap;
+
+			down_write_nested(&amp;new_snap-&gt;lock,
+					  SINGLE_DEPTH_NESTING);
+			handover_exceptions(s, new_snap);
+			up_write(&amp;new_snap-&gt;lock);
+		} else {
+			/* allow table_clear to cancel handover */
+			unlink_snapshots_for_handover(s-&gt;handover_snap, s);
+		}
+	}
+	/* An incomplete exception handover is not allowed */
+	BUG_ON(s-&gt;handover_snap);
+	up_write(&amp;s-&gt;lock);
+
+	if (new_snap &amp;&amp; register_snapshot(new_snap)) {
+		DMERR("Unable to register snapshot "
+		      "after exception handover.");
+		new_snap-&gt;valid = 0;
+	}
+
 	/* Prevent further origin writes from using this snapshot. */
 	/* After this returns there can be no new kcopyd jobs. */
 	unregister_snapshot(s);
@@ -1189,11 +1380,40 @@ static void snapshot_presuspend(struct d
 static void snapshot_resume(struct dm_target *ti)
 {
 	struct dm_snapshot *s = ti-&gt;private;
+	struct dm_snapshot *lock_snap, *old_snap, *new_snap = NULL;
 
 	down_write(&amp;s-&gt;lock);
+	if (s-&gt;handover_snap) {
+		/*
+		 * Initially assumes this snapshot will get
+		 * exception store from another snapshot
+		 */
+		old_snap = s-&gt;handover_snap;
+		new_snap = s;
+		lock_snap = old_snap;
+
+		if (!s-&gt;is_handover_destination) {
+			/* Handover exceptions to another snapshot */
+			old_snap = s;
+			new_snap = s-&gt;handover_snap;
+			lock_snap = new_snap;
+		}
+		down_write_nested(&amp;lock_snap-&gt;lock,
+				  SINGLE_DEPTH_NESTING);
+		handover_exceptions(old_snap, new_snap);
+		up_write(&amp;lock_snap-&gt;lock);
+	}
+	/* An incomplete exception handover is not allowed */
+	BUG_ON(s-&gt;handover_snap);
 	s-&gt;active = 1;
 	s-&gt;suspended = 0;
 	up_write(&amp;s-&gt;lock);
+
+	if (new_snap &amp;&amp; register_snapshot(new_snap)) {
+		DMERR("Unable to register snapshot "
+		      "after exception handover.");
+		new_snap-&gt;valid = 0;
+	}
 }
 
 static int snapshot_status(struct dm_target *ti, status_type_t type,
</pre></body></html>