<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">From: FUJITA Tomonori &lt;fujita.tomonori@lab.ntt.co.jp&gt;

[checkpatch errors]

Signed-off-by: FUJITA Tomonori &lt;fujita.tomonori@lab.ntt.co.jp&gt;
---
 drivers/md/dm-snap.c |  199 ++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 181 insertions(+), 18 deletions(-)

Index: linux-2.6.28-rc3/drivers/md/dm-snap.c
===================================================================
--- linux-2.6.28-rc3.orig/drivers/md/dm-snap.c	2008-11-07 13:15:53.000000000 +0000
+++ linux-2.6.28-rc3/drivers/md/dm-snap.c	2008-11-07 13:15:55.000000000 +0000
@@ -1156,11 +1156,9 @@ static void snapshot_resume(struct dm_ta
 	up_write(&amp;s-&gt;lock);
 }
 
-static int snapshot_status(struct dm_target *ti, status_type_t type,
-			   char *result, unsigned int maxlen)
+static int do_snapshot_status(struct dm_snapshot *snap, status_type_t type,
+			      char *result, unsigned int maxlen, u64 snapid)
 {
-	struct dm_snapshot *snap = ti-&gt;private;
-
 	switch (type) {
 	case STATUSTYPE_INFO:
 		if (!snap-&gt;valid)
@@ -1186,16 +1184,31 @@ static int snapshot_status(struct dm_tar
 		 * to make private copies if the output is to
 		 * make sense.
 		 */
-		snprintf(result, maxlen, "%s %s %c %llu",
-			 snap-&gt;origin-&gt;name, snap-&gt;cow-&gt;name,
-			 snap-&gt;type,
-			 (unsigned long long)snap-&gt;chunk_size);
+		if (snap-&gt;type == 'S')
+			snprintf(result, maxlen, "%s %s %c %llu %llu",
+				 snap-&gt;origin-&gt;name, snap-&gt;cow-&gt;name,
+				 snap-&gt;type,
+				 (unsigned long long)snap-&gt;chunk_size,
+				 snapid);
+		else
+			snprintf(result, maxlen, "%s %s %c %llu",
+				 snap-&gt;origin-&gt;name, snap-&gt;cow-&gt;name,
+				 snap-&gt;type,
+				 (unsigned long long)snap-&gt;chunk_size);
 		break;
 	}
 
 	return 0;
 }
 
+static int snapshot_status(struct dm_target *ti, status_type_t type,
+			   char *result, unsigned int maxlen)
+{
+	struct dm_snapshot *snap = ti-&gt;private;
+
+	return do_snapshot_status(snap, type, result, maxlen, 0);
+}
+
 /*-----------------------------------------------------------------
  * Origin methods
  *---------------------------------------------------------------*/
@@ -1409,7 +1422,6 @@ static int origin_status(struct dm_targe
 /*
  * shared exception origin code
  */
-
 struct dm_shared_origin {
 	struct dm_dev *origin;
 
@@ -1536,6 +1548,134 @@ static int shared_origin_message(struct 
 	return snap-&gt;store.message(&amp;snap-&gt;store, argc, argv);
 }
 
+/*
+ * shared exception snapshot code
+ */
+
+struct dm_shared_snapshot {
+	struct dm_dev *origin;
+
+	struct dm_snapshot *snap;
+	u64 snapid;
+};
+
+/*
+ * Construct a shared origin mapping: &lt;origin_dev&gt; &lt;snapshot id&gt;
+ */
+static int shared_snapshot_ctr(struct dm_target *ti, unsigned int argc,
+			       char **argv)
+{
+	int r;
+	struct dm_shared_origin *dso;
+	struct dm_shared_snapshot *dss;
+	struct dm_snapshot *origin_snap;
+	struct dm_dev *dev;
+	struct origin *o;
+	u64 id;
+
+	if (argc != 2) {
+		ti-&gt;error = "snapshot: incorrect number of arguments";
+		return -EINVAL;
+	}
+
+	dss = kzalloc(sizeof(*dss), GFP_KERNEL);
+	if (!dss)
+		return -ENOMEM;
+
+	id = simple_strtoul(argv[1], NULL, 10);
+	if (id &gt;= 64) {
+		r = -EINVAL;
+		ti-&gt;error = "Invalid snapid";
+		goto out;
+	}
+
+	r = dm_get_device(ti, argv[0], 0, ti-&gt;len, FMODE_READ, &amp;dev);
+	if (r) {
+		ti-&gt;error = "Cannot get origin device";
+		goto out;
+	}
+
+	r = -EINVAL;
+	down_read(&amp;_origins_lock);
+	o = __lookup_origin(dev-&gt;bdev);
+	if (o &amp;&amp; !list_empty(&amp;o-&gt;snapshots)) {
+		int nr;
+		unsigned long map[DIV_ROUND_UP(MAX_SNAPSHOTS, BITS_PER_LONG)];
+		struct exception_store *store;
+
+		memset(map, 0, sizeof(map));
+
+		origin_snap = list_first_entry(&amp;o-&gt;snapshots,
+					       struct dm_snapshot, list);
+		store = &amp;origin_snap-&gt;store;
+		dso = origin_snap-&gt;ti-&gt;private;
+
+		down_write(&amp;store-&gt;snap-&gt;lock);
+		nr = store-&gt;get_snapshot_info(store, map, sizeof(map));
+		up_write(&amp;store-&gt;snap-&gt;lock);
+
+		if (test_bit(id, map)) {
+			dm_table_get(origin_snap-&gt;ti-&gt;table);
+			ti-&gt;private = dss;
+			dss-&gt;snap = origin_snap;
+			dss-&gt;snapid = id;
+			dss-&gt;origin = dev;
+
+			ti-&gt;split_io = origin_snap-&gt;chunk_size;
+			r = 0;
+
+			printk("%s %d: %lld\n", __FUNCTION__, __LINE__,
+			       (unsigned long long)id);
+
+		} else {
+			ti-&gt;error = "non-exist snapshot";
+			r = -EINVAL;
+		}
+	}
+	up_read(&amp;_origins_lock);
+
+	if (r)
+		dm_put_device(ti, dev);
+out:
+	if (r)
+		kfree(dss);
+
+	return r;
+}
+
+static void shared_snapshot_dtr(struct dm_target *ti)
+{
+	struct dm_shared_snapshot *dss = ti-&gt;private;
+
+	dm_table_put(dss-&gt;snap-&gt;ti-&gt;table);
+	dm_put_device(ti, dss-&gt;origin);
+	kfree(dss);
+}
+
+static int shared_snapshot_map(struct dm_target *ti, struct bio *bio,
+			       union map_info *map_context)
+{
+	struct dm_shared_snapshot *dss = ti-&gt;private;
+
+	return do_snapshot_map(dss-&gt;snap, bio, map_context, dss-&gt;snapid);
+}
+
+static int shared_snapshot_end_io(struct dm_target *ti, struct bio *bio,
+				  int error, union map_info *map_context)
+{
+	struct dm_shared_snapshot *dss = ti-&gt;private;
+
+	return do_snapshot_end_io(dss-&gt;snap, bio, error, map_context);
+}
+
+static int shared_snapshot_status(struct dm_target *ti, status_type_t type,
+				  char *result, unsigned int maxlen)
+{
+	struct dm_shared_snapshot *dss = ti-&gt;private;
+
+	return do_snapshot_status(dss-&gt;snap, type, result, maxlen, dss-&gt;snapid);
+}
+
 static struct target_type origin_target = {
 	.name    = "snapshot-origin",
 	.version = {1, 6, 0},
@@ -1570,6 +1710,17 @@ static struct target_type shared_origin_
 	.message = shared_origin_message,
 };
 
+static struct target_type shared_snapshot_target = {
+	.name    = "s-snap",
+	.version = {1, 0, 0},
+	.module  = THIS_MODULE,
+	.ctr     = shared_snapshot_ctr,
+	.dtr     = shared_snapshot_dtr,
+	.map     = shared_snapshot_map,
+	.end_io  = shared_snapshot_end_io,
+	.status  = shared_snapshot_status,
+};
+
 static int __init dm_snapshot_init(void)
 {
 	int r;
@@ -1586,37 +1737,43 @@ static int __init dm_snapshot_init(void)
 		goto bad1;
 	}
 
+	r = dm_register_target(&amp;shared_snapshot_target);
+	if (r) {
+		DMERR("shared snapshot target register failed %d", r);
+		goto bad2;
+	}
+
 	r = dm_register_target(&amp;shared_origin_target);
 	if (r) {
 		DMERR("shared origin target register failed %d", r);
-		goto bad2;
+		goto bad3;
 	}
 
 	r = init_origin_hash();
 	if (r) {
 		DMERR("init_origin_hash failed.");
-		goto bad3;
+		goto bad4;
 	}
 
 	exception_cache = KMEM_CACHE(dm_snap_exception, 0);
 	if (!exception_cache) {
 		DMERR("Couldn't create exception cache.");
 		r = -ENOMEM;
-		goto bad4;
+		goto bad5;
 	}
 
 	pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0);
 	if (!pending_cache) {
 		DMERR("Couldn't create pending cache.");
 		r = -ENOMEM;
-		goto bad5;
+		goto bad6;
 	}
 
 	tracked_chunk_cache = KMEM_CACHE(dm_snap_tracked_chunk, 0);
 	if (!tracked_chunk_cache) {
 		DMERR("Couldn't create cache to track chunks in use.");
 		r = -ENOMEM;
-		goto bad6;
+		goto bad7;
 	}
 
 	ksnapd = create_singlethread_workqueue("ksnapd");
@@ -1630,14 +1787,16 @@ static int __init dm_snapshot_init(void)
 
       bad_pending_pool:
 	kmem_cache_destroy(tracked_chunk_cache);
-      bad6:
+      bad7:
 	kmem_cache_destroy(pending_cache);
-      bad5:
+      bad6:
 	kmem_cache_destroy(exception_cache);
-      bad4:
+      bad5:
 	exit_origin_hash();
-      bad3:
+      bad4:
 	dm_unregister_target(&amp;shared_origin_target);
+      bad3:
+	dm_unregister_target(&amp;shared_snapshot_target);
       bad2:
 	dm_unregister_target(&amp;origin_target);
       bad1:
@@ -1652,6 +1811,10 @@ static void __exit dm_snapshot_exit(void
 	dm_unregister_target(&amp;snapshot_target);
 	dm_unregister_target(&amp;origin_target);
 
+	r = dm_unregister_target(&amp;shared_snapshot_target);
+	if (r)
+		DMERR("shared snapshot unregister failed %d", r);
+
 	r = dm_unregister_target(&amp;shared_origin_target);
 	if (r)
 		DMERR("shared origin unregister failed %d", r);
</pre></body></html>