dm: export suspended state to targets
[safe/jmp/linux-2.6] / drivers / md / dm-snap.c
index f40331c..b5b9118 100644 (file)
@@ -59,6 +59,9 @@ struct dm_snapshot {
        struct rw_semaphore lock;
 
        struct dm_dev *origin;
+       struct dm_dev *cow;
+
+       struct dm_target *ti;
 
        /* List of snapshots per Origin */
        struct list_head list;
@@ -69,6 +72,9 @@ struct dm_snapshot {
        /* Origin writes don't trigger exceptions until this is set */
        int active;
 
+       /* Whether or not owning mapped_device is suspended */
+       int suspended;
+
        mempool_t *pending_pool;
 
        atomic_t pending_exceptions_count;
@@ -97,6 +103,12 @@ struct dm_snapshot {
        struct hlist_head tracked_chunk_hash[DM_TRACKED_CHUNK_HASH_SIZE];
 };
 
+struct dm_dev *dm_snap_cow(struct dm_snapshot *s)
+{
+       return s->cow;
+}
+EXPORT_SYMBOL(dm_snap_cow);
+
 static struct workqueue_struct *ksnapd;
 static void flush_queued_bios(struct work_struct *work);
 
@@ -291,22 +303,116 @@ static void __insert_origin(struct origin *o)
 }
 
 /*
+ * _origins_lock must be held when calling this function.
+ * Returns number of snapshots registered using the supplied cow device, plus:
+ * snap_src - a snapshot suitable for use as a source of exception handover
+ * snap_dest - a snapshot capable of receiving exception handover.
+ *
+ * Possible return values and states:
+ *   0: NULL, NULL  - first new snapshot
+ *   1: snap_src, NULL - normal snapshot
+ *   2: snap_src, snap_dest  - waiting for handover
+ *   2: snap_src, NULL - handed over, waiting for old to be deleted
+ *   1: NULL, snap_dest - source got destroyed without handover
+ */
+static int __find_snapshots_sharing_cow(struct dm_snapshot *snap,
+                                       struct dm_snapshot **snap_src,
+                                       struct dm_snapshot **snap_dest)
+{
+       struct dm_snapshot *s;
+       struct origin *o;
+       int count = 0;
+       int active;
+
+       o = __lookup_origin(snap->origin->bdev);
+       if (!o)
+               goto out;
+
+       list_for_each_entry(s, &o->snapshots, list) {
+               if (!bdev_equal(s->cow->bdev, snap->cow->bdev))
+                       continue;
+
+               down_read(&s->lock);
+               active = s->active;
+               up_read(&s->lock);
+
+               if (active) {
+                       if (snap_src)
+                               *snap_src = s;
+               } else if (snap_dest)
+                       *snap_dest = s;
+
+               count++;
+       }
+
+out:
+       return count;
+}
+
+/*
+ * On success, returns 1 if this snapshot is a handover destination,
+ * otherwise returns 0.
+ */
+static int __validate_exception_handover(struct dm_snapshot *snap)
+{
+       struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
+
+       /* Does snapshot need exceptions handed over to it? */
+       if ((__find_snapshots_sharing_cow(snap, &snap_src, &snap_dest) == 2) ||
+           snap_dest) {
+               snap->ti->error = "Snapshot cow pairing for exception "
+                                 "table handover failed";
+               return -EINVAL;
+       }
+
+       /*
+        * If no snap_src was found, snap cannot become a handover
+        * destination.
+        */
+       if (!snap_src)
+               return 0;
+
+       return 1;
+}
+
+static void __insert_snapshot(struct origin *o, struct dm_snapshot *s)
+{
+       struct dm_snapshot *l;
+
+       /* Sort the list according to chunk size, largest-first smallest-last */
+       list_for_each_entry(l, &o->snapshots, list)
+               if (l->store->chunk_size < s->store->chunk_size)
+                       break;
+       list_add_tail(&s->list, &l->list);
+}
+
+/*
  * Make a note of the snapshot and its origin so we can look it
  * up when the origin has a write on it.
+ *
+ * Also validate snapshot exception store handovers.
+ * On success, returns 1 if this registration is a handover destination,
+ * otherwise returns 0.
  */
 static int register_snapshot(struct dm_snapshot *snap)
 {
-       struct dm_snapshot *l;
-       struct origin *o, *new_o;
+       struct origin *o, *new_o = NULL;
        struct block_device *bdev = snap->origin->bdev;
+       int r = 0;
 
        new_o = kmalloc(sizeof(*new_o), GFP_KERNEL);
        if (!new_o)
                return -ENOMEM;
 
        down_write(&_origins_lock);
-       o = __lookup_origin(bdev);
 
+       r = __validate_exception_handover(snap);
+       if (r < 0) {
+               kfree(new_o);
+               goto out;
+       }
+
+       o = __lookup_origin(bdev);
        if (o)
                kfree(new_o);
        else {
@@ -320,14 +426,27 @@ static int register_snapshot(struct dm_snapshot *snap)
                __insert_origin(o);
        }
 
-       /* Sort the list according to chunk size, largest-first smallest-last */
-       list_for_each_entry(l, &o->snapshots, list)
-               if (l->store->chunk_size < snap->store->chunk_size)
-                       break;
-       list_add_tail(&snap->list, &l->list);
+       __insert_snapshot(o, snap);
+
+out:
+       up_write(&_origins_lock);
+
+       return r;
+}
+
+/*
+ * Move snapshot to correct place in list according to chunk size.
+ */
+static void reregister_snapshot(struct dm_snapshot *s)
+{
+       struct block_device *bdev = s->origin->bdev;
+
+       down_write(&_origins_lock);
+
+       list_del(&s->list);
+       __insert_snapshot(__lookup_origin(bdev), s);
 
        up_write(&_origins_lock);
-       return 0;
 }
 
 static void unregister_snapshot(struct dm_snapshot *s)
@@ -338,7 +457,7 @@ static void unregister_snapshot(struct dm_snapshot *s)
        o = __lookup_origin(s->origin->bdev);
 
        list_del(&s->list);
-       if (list_empty(&o->snapshots)) {
+       if (o && list_empty(&o->snapshots)) {
                list_del(&o->hash_list);
                kfree(o);
        }
@@ -351,8 +470,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;
 
@@ -368,8 +487,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;
@@ -391,7 +510,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);
 }
@@ -400,8 +519,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;
@@ -415,7 +534,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;
 
@@ -426,7 +545,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);
 }
@@ -451,8 +570,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;
@@ -471,7 +590,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;
                }
 
@@ -481,7 +600,7 @@ static void insert_exception(struct dm_exception_table *eh,
                        dm_consecutive_chunk_count_inc(e);
                        e->old_chunk--;
                        e->new_chunk--;
-                       free_exception(new_e);
+                       free_completed_exception(new_e);
                        return;
                }
 
@@ -502,7 +621,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;
 
@@ -511,7 +630,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;
 }
@@ -558,7 +677,7 @@ static int init_hash_tables(struct dm_snapshot *s)
         * Calculate based on the size of the original volume or
         * the COW volume...
         */
-       cow_dev_size = get_dev_size(s->store->cow->bdev);
+       cow_dev_size = get_dev_size(s->cow->bdev);
        origin_dev_size = get_dev_size(s->origin->bdev);
        max_buckets = calc_max_buckets();
 
@@ -568,8 +687,8 @@ static int init_hash_tables(struct dm_snapshot *s)
        if (hash_size < 64)
                hash_size = 64;
        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;
 
        /*
@@ -580,8 +699,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;
        }
 
@@ -596,49 +715,61 @@ static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
        struct dm_snapshot *s;
        int i;
        int r = -EINVAL;
-       char *origin_path;
-       struct dm_exception_store *store;
+       char *origin_path, *cow_path;
        unsigned args_used;
 
        if (argc != 4) {
                ti->error = "requires exactly 4 arguments";
                r = -EINVAL;
-               goto bad_args;
+               goto bad;
        }
 
        origin_path = argv[0];
        argv++;
        argc--;
 
-       r = dm_exception_store_create(ti, argc, argv, &args_used, &store);
+       s = kmalloc(sizeof(*s), GFP_KERNEL);
+       if (!s) {
+               ti->error = "Cannot allocate snapshot context private "
+                   "structure";
+               r = -ENOMEM;
+               goto bad;
+       }
+
+       cow_path = argv[0];
+       argv++;
+       argc--;
+
+       r = dm_get_device(ti, cow_path, 0, 0,
+                         FMODE_READ | FMODE_WRITE, &s->cow);
+       if (r) {
+               ti->error = "Cannot get COW device";
+               goto bad_cow;
+       }
+
+       r = dm_exception_store_create(ti, argc, argv, s, &args_used, &s->store);
        if (r) {
                ti->error = "Couldn't create exception store";
                r = -EINVAL;
-               goto bad_args;
+               goto bad_store;
        }
 
        argv += args_used;
        argc -= args_used;
 
-       s = kmalloc(sizeof(*s), GFP_KERNEL);
-       if (!s) {
-               ti->error = "Cannot allocate snapshot context private "
-                   "structure";
-               r = -ENOMEM;
-               goto bad_snap;
-       }
-
        r = dm_get_device(ti, origin_path, 0, ti->len, FMODE_READ, &s->origin);
        if (r) {
                ti->error = "Cannot get origin device";
                goto bad_origin;
        }
 
-       s->store = store;
+       s->ti = ti;
        s->valid = 1;
        s->active = 0;
+       s->suspended = 0;
        atomic_set(&s->pending_exceptions_count, 0);
        init_rwsem(&s->lock);
+       INIT_LIST_HEAD(&s->list);
        spin_lock_init(&s->pe_lock);
 
        /* Allocate hash table for COW data */
@@ -673,39 +804,55 @@ static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
 
        spin_lock_init(&s->tracked_chunk_lock);
 
-       /* Metadata must only be loaded into one table at once */
+       bio_list_init(&s->queued_bios);
+       INIT_WORK(&s->queued_bios_work, flush_queued_bios);
+
+       ti->private = s;
+       ti->num_flush_requests = 1;
+
+       /* Add snapshot to the list of snapshots for this origin */
+       /* Exceptions aren't triggered till snapshot_resume() is called */
+       r = register_snapshot(s);
+       if (r == -ENOMEM) {
+               ti->error = "Snapshot origin struct allocation failed";
+               goto bad_load_and_register;
+       } else if (r < 0) {
+               /* invalid handover, register_snapshot has set ti->error */
+               goto bad_load_and_register;
+       }
+
+       /*
+        * Metadata must only be loaded into one table at once, so skip this
+        * if metadata will be handed over during resume.
+        * Chunk size will be set during the handover - set it to zero to
+        * ensure it's ignored.
+        */
+       if (r > 0) {
+               s->store->chunk_size = 0;
+               return 0;
+       }
+
        r = s->store->type->read_metadata(s->store, dm_add_exception,
                                          (void *)s);
        if (r < 0) {
                ti->error = "Failed to read snapshot metadata";
-               goto bad_load_and_register;
+               goto bad_read_metadata;
        } else if (r > 0) {
                s->valid = 0;
                DMWARN("Snapshot is marked invalid.");
        }
 
-       bio_list_init(&s->queued_bios);
-       INIT_WORK(&s->queued_bios_work, flush_queued_bios);
-
        if (!s->store->chunk_size) {
                ti->error = "Chunk size not set";
-               goto bad_load_and_register;
+               goto bad_read_metadata;
        }
-
-       /* Add snapshot to the list of snapshots for this origin */
-       /* Exceptions aren't triggered till snapshot_resume() is called */
-       if (register_snapshot(s)) {
-               r = -EINVAL;
-               ti->error = "Cannot register snapshot origin";
-               goto bad_load_and_register;
-       }
-
-       ti->private = s;
        ti->split_io = s->store->chunk_size;
-       ti->num_flush_requests = 1;
 
        return 0;
 
+bad_read_metadata:
+       unregister_snapshot(s);
+
 bad_load_and_register:
        mempool_destroy(s->tracked_chunk_pool);
 
@@ -716,19 +863,22 @@ 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);
 
 bad_origin:
-       kfree(s);
+       dm_exception_store_destroy(s->store);
+
+bad_store:
+       dm_put_device(ti, s->cow);
 
-bad_snap:
-       dm_exception_store_destroy(store);
+bad_cow:
+       kfree(s);
 
-bad_args:
+bad:
        return r;
 }
 
@@ -737,8 +887,39 @@ 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 __handover_exceptions(struct dm_snapshot *snap_src,
+                                 struct dm_snapshot *snap_dest)
+{
+       union {
+               struct dm_exception_table table_swap;
+               struct dm_exception_store *store_swap;
+       } u;
+
+       /*
+        * Swap all snapshot context information between the two instances.
+        */
+       u.table_swap = snap_dest->complete;
+       snap_dest->complete = snap_src->complete;
+       snap_src->complete = u.table_swap;
+
+       u.store_swap = snap_dest->store;
+       snap_dest->store = snap_src->store;
+       snap_src->store = u.store_swap;
+
+       snap_dest->store->snap = snap_dest;
+       snap_src->store->snap = snap_src;
+
+       snap_dest->ti->split_io = snap_dest->store->chunk_size;
+       snap_dest->valid = snap_src->valid;
+
+       /*
+        * Set source invalid to ensure it receives no further I/O.
+        */
+       snap_src->valid = 0;
 }
 
 static void snapshot_dtr(struct dm_target *ti)
@@ -747,9 +928,21 @@ static void snapshot_dtr(struct dm_target *ti)
        int i;
 #endif
        struct dm_snapshot *s = ti->private;
+       struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
 
        flush_workqueue(ksnapd);
 
+       down_read(&_origins_lock);
+       /* Check whether exception handover must be cancelled */
+       (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest);
+       if (snap_src && snap_dest && (s == snap_src)) {
+               down_write(&snap_dest->lock);
+               snap_dest->valid = 0;
+               up_write(&snap_dest->lock);
+               DMERR("Cancelling snapshot handover.");
+       }
+       up_read(&_origins_lock);
+
        /* Prevent further origin writes from using this snapshot. */
        /* After this returns there can be no new kcopyd jobs. */
        unregister_snapshot(s);
@@ -777,6 +970,8 @@ static void snapshot_dtr(struct dm_target *ti)
 
        dm_exception_store_destroy(s->store);
 
+       dm_put_device(ti, s->cow);
+
        kfree(s);
 }
 
@@ -839,7 +1034,7 @@ static void __invalidate_snapshot(struct dm_snapshot *s, int err)
 
        s->valid = 0;
 
-       dm_table_event(s->store->ti->table);
+       dm_table_event(s->ti->table);
 }
 
 static void get_pending_exception(struct dm_snap_pending_exception *pe)
@@ -891,7 +1086,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);
@@ -902,7 +1097,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;
        }
@@ -918,10 +1113,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);
 
@@ -977,7 +1172,7 @@ static void start_copy(struct dm_snap_pending_exception *pe)
        src.sector = chunk_to_sector(s->store, pe->e.old_chunk);
        src.count = min((sector_t)s->store->chunk_size, dev_size - src.sector);
 
-       dest.bdev = s->store->cow->bdev;
+       dest.bdev = s->cow->bdev;
        dest.sector = chunk_to_sector(s->store, pe->e.new_chunk);
        dest.count = src.count;
 
@@ -989,7 +1184,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;
@@ -1030,7 +1225,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;
 }
@@ -1038,7 +1233,7 @@ __find_pending_exception(struct dm_snapshot *s,
 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;
+       bio->bi_bdev = s->cow->bdev;
        bio->bi_sector = chunk_to_sector(s->store,
                                         dm_chunk_number(e->new_chunk) +
                                         (chunk - e->old_chunk)) +
@@ -1056,7 +1251,7 @@ static int snapshot_map(struct dm_target *ti, struct bio *bio,
        struct dm_snap_pending_exception *pe = NULL;
 
        if (unlikely(bio_empty_barrier(bio))) {
-               bio->bi_bdev = s->store->cow->bdev;
+               bio->bi_bdev = s->cow->bdev;
                return DM_MAPIO_REMAPPED;
        }
 
@@ -1077,7 +1272,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;
@@ -1101,7 +1296,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);
@@ -1151,12 +1346,63 @@ static int snapshot_end_io(struct dm_target *ti, struct bio *bio,
        return 0;
 }
 
+static void snapshot_postsuspend(struct dm_target *ti)
+{
+       struct dm_snapshot *s = ti->private;
+
+       down_write(&s->lock);
+       s->suspended = 1;
+       up_write(&s->lock);
+}
+
+static int snapshot_preresume(struct dm_target *ti)
+{
+       int r = 0;
+       struct dm_snapshot *s = ti->private;
+       struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
+
+       down_read(&_origins_lock);
+       (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest);
+       if (snap_src && snap_dest) {
+               down_read(&snap_src->lock);
+               if (s == snap_src) {
+                       DMERR("Unable to resume snapshot source until "
+                             "handover completes.");
+                       r = -EINVAL;
+               } else if (!snap_src->suspended) {
+                       DMERR("Unable to perform snapshot handover until "
+                             "source is suspended.");
+                       r = -EINVAL;
+               }
+               up_read(&snap_src->lock);
+       }
+       up_read(&_origins_lock);
+
+       return r;
+}
+
 static void snapshot_resume(struct dm_target *ti)
 {
        struct dm_snapshot *s = ti->private;
+       struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
+
+       down_read(&_origins_lock);
+       (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest);
+       if (snap_src && snap_dest) {
+               down_write(&snap_src->lock);
+               down_write_nested(&snap_dest->lock, SINGLE_DEPTH_NESTING);
+               __handover_exceptions(snap_src, snap_dest);
+               up_write(&snap_dest->lock);
+               up_write(&snap_src->lock);
+       }
+       up_read(&_origins_lock);
+
+       /* Now we have correct chunk size, reregister */
+       reregister_snapshot(s);
 
        down_write(&s->lock);
        s->active = 1;
+       s->suspended = 0;
        up_write(&s->lock);
 }
 
@@ -1174,14 +1420,17 @@ static int snapshot_status(struct dm_target *ti, status_type_t type,
                if (!snap->valid)
                        DMEMIT("Invalid");
                else {
-                       if (snap->store->type->fraction_full) {
-                               sector_t numerator, denominator;
-                               snap->store->type->fraction_full(snap->store,
-                                                                &numerator,
-                                                                &denominator);
-                               DMEMIT("%llu/%llu",
-                                      (unsigned long long)numerator,
-                                      (unsigned long long)denominator);
+                       if (snap->store->type->usage) {
+                               sector_t total_sectors, sectors_allocated,
+                                        metadata_sectors;
+                               snap->store->type->usage(snap->store,
+                                                        &total_sectors,
+                                                        &sectors_allocated,
+                                                        &metadata_sectors);
+                               DMEMIT("%llu/%llu %llu",
+                                      (unsigned long long)sectors_allocated,
+                                      (unsigned long long)total_sectors,
+                                      (unsigned long long)metadata_sectors);
                        }
                        else
                                DMEMIT("Unknown");
@@ -1197,7 +1446,7 @@ static int snapshot_status(struct dm_target *ti, status_type_t type,
                 * to make private copies if the output is to
                 * make sense.
                 */
-               DMEMIT("%s", snap->origin->name);
+               DMEMIT("%s %s", snap->origin->name, snap->cow->name);
                snap->store->type->status(snap->store, type, result + sz,
                                          maxlen - sz);
                break;
@@ -1237,7 +1486,7 @@ static int __origin_write(struct list_head *snapshots, struct bio *bio)
                        goto next_snapshot;
 
                /* Nothing to do if writing beyond end of snapshot */
-               if (bio->bi_sector >= dm_table_get_size(snap->store->ti->table))
+               if (bio->bi_sector >= dm_table_get_size(snap->ti->table))
                        goto next_snapshot;
 
                /*
@@ -1254,7 +1503,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;
 
@@ -1269,7 +1518,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;
@@ -1462,12 +1711,14 @@ static struct target_type origin_target = {
 
 static struct target_type snapshot_target = {
        .name    = "snapshot",
-       .version = {1, 7, 0},
+       .version = {1, 9, 0},
        .module  = THIS_MODULE,
        .ctr     = snapshot_ctr,
        .dtr     = snapshot_dtr,
        .map     = snapshot_map,
        .end_io  = snapshot_end_io,
+       .postsuspend = snapshot_postsuspend,
+       .preresume  = snapshot_preresume,
        .resume  = snapshot_resume,
        .status  = snapshot_status,
        .iterate_devices = snapshot_iterate_devices,