dm snapshot: track suspended state in target
[sfrench/cifs-2.6.git] / drivers / md / dm-snap.c
1 /*
2  * dm-snapshot.c
3  *
4  * Copyright (C) 2001-2002 Sistina Software (UK) Limited.
5  *
6  * This file is released under the GPL.
7  */
8
9 #include <linux/blkdev.h>
10 #include <linux/device-mapper.h>
11 #include <linux/delay.h>
12 #include <linux/fs.h>
13 #include <linux/init.h>
14 #include <linux/kdev_t.h>
15 #include <linux/list.h>
16 #include <linux/mempool.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/log2.h>
21 #include <linux/dm-kcopyd.h>
22 #include <linux/workqueue.h>
23
24 #include "dm-exception-store.h"
25
26 #define DM_MSG_PREFIX "snapshots"
27
28 /*
29  * The percentage increment we will wake up users at
30  */
31 #define WAKE_UP_PERCENT 5
32
33 /*
34  * kcopyd priority of snapshot operations
35  */
36 #define SNAPSHOT_COPY_PRIORITY 2
37
38 /*
39  * Reserve 1MB for each snapshot initially (with minimum of 1 page).
40  */
41 #define SNAPSHOT_PAGES (((1UL << 20) >> PAGE_SHIFT) ? : 1)
42
43 /*
44  * The size of the mempool used to track chunks in use.
45  */
46 #define MIN_IOS 256
47
48 #define DM_TRACKED_CHUNK_HASH_SIZE      16
49 #define DM_TRACKED_CHUNK_HASH(x)        ((unsigned long)(x) & \
50                                          (DM_TRACKED_CHUNK_HASH_SIZE - 1))
51
52 struct dm_exception_table {
53         uint32_t hash_mask;
54         unsigned hash_shift;
55         struct list_head *table;
56 };
57
58 struct dm_snapshot {
59         struct rw_semaphore lock;
60
61         struct dm_dev *origin;
62         struct dm_dev *cow;
63
64         struct dm_target *ti;
65
66         /* List of snapshots per Origin */
67         struct list_head list;
68
69         /* You can't use a snapshot if this is 0 (e.g. if full) */
70         int valid;
71
72         /* Origin writes don't trigger exceptions until this is set */
73         int active;
74
75         /* Whether or not owning mapped_device is suspended */
76         int suspended;
77
78         mempool_t *pending_pool;
79
80         atomic_t pending_exceptions_count;
81
82         struct dm_exception_table pending;
83         struct dm_exception_table complete;
84
85         /*
86          * pe_lock protects all pending_exception operations and access
87          * as well as the snapshot_bios list.
88          */
89         spinlock_t pe_lock;
90
91         /* The on disk metadata handler */
92         struct dm_exception_store *store;
93
94         struct dm_kcopyd_client *kcopyd_client;
95
96         /* Queue of snapshot writes for ksnapd to flush */
97         struct bio_list queued_bios;
98         struct work_struct queued_bios_work;
99
100         /* Chunks with outstanding reads */
101         mempool_t *tracked_chunk_pool;
102         spinlock_t tracked_chunk_lock;
103         struct hlist_head tracked_chunk_hash[DM_TRACKED_CHUNK_HASH_SIZE];
104 };
105
106 struct dm_dev *dm_snap_cow(struct dm_snapshot *s)
107 {
108         return s->cow;
109 }
110 EXPORT_SYMBOL(dm_snap_cow);
111
112 static struct workqueue_struct *ksnapd;
113 static void flush_queued_bios(struct work_struct *work);
114
115 static sector_t chunk_to_sector(struct dm_exception_store *store,
116                                 chunk_t chunk)
117 {
118         return chunk << store->chunk_shift;
119 }
120
121 static int bdev_equal(struct block_device *lhs, struct block_device *rhs)
122 {
123         /*
124          * There is only ever one instance of a particular block
125          * device so we can compare pointers safely.
126          */
127         return lhs == rhs;
128 }
129
130 struct dm_snap_pending_exception {
131         struct dm_exception e;
132
133         /*
134          * Origin buffers waiting for this to complete are held
135          * in a bio list
136          */
137         struct bio_list origin_bios;
138         struct bio_list snapshot_bios;
139
140         /*
141          * Short-term queue of pending exceptions prior to submission.
142          */
143         struct list_head list;
144
145         /*
146          * The primary pending_exception is the one that holds
147          * the ref_count and the list of origin_bios for a
148          * group of pending_exceptions.  It is always last to get freed.
149          * These fields get set up when writing to the origin.
150          */
151         struct dm_snap_pending_exception *primary_pe;
152
153         /*
154          * Number of pending_exceptions processing this chunk.
155          * When this drops to zero we must complete the origin bios.
156          * If incrementing or decrementing this, hold pe->snap->lock for
157          * the sibling concerned and not pe->primary_pe->snap->lock unless
158          * they are the same.
159          */
160         atomic_t ref_count;
161
162         /* Pointer back to snapshot context */
163         struct dm_snapshot *snap;
164
165         /*
166          * 1 indicates the exception has already been sent to
167          * kcopyd.
168          */
169         int started;
170 };
171
172 /*
173  * Hash table mapping origin volumes to lists of snapshots and
174  * a lock to protect it
175  */
176 static struct kmem_cache *exception_cache;
177 static struct kmem_cache *pending_cache;
178
179 struct dm_snap_tracked_chunk {
180         struct hlist_node node;
181         chunk_t chunk;
182 };
183
184 static struct kmem_cache *tracked_chunk_cache;
185
186 static struct dm_snap_tracked_chunk *track_chunk(struct dm_snapshot *s,
187                                                  chunk_t chunk)
188 {
189         struct dm_snap_tracked_chunk *c = mempool_alloc(s->tracked_chunk_pool,
190                                                         GFP_NOIO);
191         unsigned long flags;
192
193         c->chunk = chunk;
194
195         spin_lock_irqsave(&s->tracked_chunk_lock, flags);
196         hlist_add_head(&c->node,
197                        &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)]);
198         spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
199
200         return c;
201 }
202
203 static void stop_tracking_chunk(struct dm_snapshot *s,
204                                 struct dm_snap_tracked_chunk *c)
205 {
206         unsigned long flags;
207
208         spin_lock_irqsave(&s->tracked_chunk_lock, flags);
209         hlist_del(&c->node);
210         spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
211
212         mempool_free(c, s->tracked_chunk_pool);
213 }
214
215 static int __chunk_is_tracked(struct dm_snapshot *s, chunk_t chunk)
216 {
217         struct dm_snap_tracked_chunk *c;
218         struct hlist_node *hn;
219         int found = 0;
220
221         spin_lock_irq(&s->tracked_chunk_lock);
222
223         hlist_for_each_entry(c, hn,
224             &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)], node) {
225                 if (c->chunk == chunk) {
226                         found = 1;
227                         break;
228                 }
229         }
230
231         spin_unlock_irq(&s->tracked_chunk_lock);
232
233         return found;
234 }
235
236 /*
237  * One of these per registered origin, held in the snapshot_origins hash
238  */
239 struct origin {
240         /* The origin device */
241         struct block_device *bdev;
242
243         struct list_head hash_list;
244
245         /* List of snapshots for this origin */
246         struct list_head snapshots;
247 };
248
249 /*
250  * Size of the hash table for origin volumes. If we make this
251  * the size of the minors list then it should be nearly perfect
252  */
253 #define ORIGIN_HASH_SIZE 256
254 #define ORIGIN_MASK      0xFF
255 static struct list_head *_origins;
256 static struct rw_semaphore _origins_lock;
257
258 static int init_origin_hash(void)
259 {
260         int i;
261
262         _origins = kmalloc(ORIGIN_HASH_SIZE * sizeof(struct list_head),
263                            GFP_KERNEL);
264         if (!_origins) {
265                 DMERR("unable to allocate memory");
266                 return -ENOMEM;
267         }
268
269         for (i = 0; i < ORIGIN_HASH_SIZE; i++)
270                 INIT_LIST_HEAD(_origins + i);
271         init_rwsem(&_origins_lock);
272
273         return 0;
274 }
275
276 static void exit_origin_hash(void)
277 {
278         kfree(_origins);
279 }
280
281 static unsigned origin_hash(struct block_device *bdev)
282 {
283         return bdev->bd_dev & ORIGIN_MASK;
284 }
285
286 static struct origin *__lookup_origin(struct block_device *origin)
287 {
288         struct list_head *ol;
289         struct origin *o;
290
291         ol = &_origins[origin_hash(origin)];
292         list_for_each_entry (o, ol, hash_list)
293                 if (bdev_equal(o->bdev, origin))
294                         return o;
295
296         return NULL;
297 }
298
299 static void __insert_origin(struct origin *o)
300 {
301         struct list_head *sl = &_origins[origin_hash(o->bdev)];
302         list_add_tail(&o->hash_list, sl);
303 }
304
305 /*
306  * Make a note of the snapshot and its origin so we can look it
307  * up when the origin has a write on it.
308  */
309 static int register_snapshot(struct dm_snapshot *snap)
310 {
311         struct dm_snapshot *l;
312         struct origin *o, *new_o;
313         struct block_device *bdev = snap->origin->bdev;
314
315         new_o = kmalloc(sizeof(*new_o), GFP_KERNEL);
316         if (!new_o)
317                 return -ENOMEM;
318
319         down_write(&_origins_lock);
320         o = __lookup_origin(bdev);
321
322         if (o)
323                 kfree(new_o);
324         else {
325                 /* New origin */
326                 o = new_o;
327
328                 /* Initialise the struct */
329                 INIT_LIST_HEAD(&o->snapshots);
330                 o->bdev = bdev;
331
332                 __insert_origin(o);
333         }
334
335         /* Sort the list according to chunk size, largest-first smallest-last */
336         list_for_each_entry(l, &o->snapshots, list)
337                 if (l->store->chunk_size < snap->store->chunk_size)
338                         break;
339         list_add_tail(&snap->list, &l->list);
340
341         up_write(&_origins_lock);
342         return 0;
343 }
344
345 static void unregister_snapshot(struct dm_snapshot *s)
346 {
347         struct origin *o;
348
349         down_write(&_origins_lock);
350         o = __lookup_origin(s->origin->bdev);
351
352         list_del(&s->list);
353         if (list_empty(&o->snapshots)) {
354                 list_del(&o->hash_list);
355                 kfree(o);
356         }
357
358         up_write(&_origins_lock);
359 }
360
361 /*
362  * Implementation of the exception hash tables.
363  * The lowest hash_shift bits of the chunk number are ignored, allowing
364  * some consecutive chunks to be grouped together.
365  */
366 static int dm_exception_table_init(struct dm_exception_table *et,
367                                    uint32_t size, unsigned hash_shift)
368 {
369         unsigned int i;
370
371         et->hash_shift = hash_shift;
372         et->hash_mask = size - 1;
373         et->table = dm_vcalloc(size, sizeof(struct list_head));
374         if (!et->table)
375                 return -ENOMEM;
376
377         for (i = 0; i < size; i++)
378                 INIT_LIST_HEAD(et->table + i);
379
380         return 0;
381 }
382
383 static void dm_exception_table_exit(struct dm_exception_table *et,
384                                     struct kmem_cache *mem)
385 {
386         struct list_head *slot;
387         struct dm_exception *ex, *next;
388         int i, size;
389
390         size = et->hash_mask + 1;
391         for (i = 0; i < size; i++) {
392                 slot = et->table + i;
393
394                 list_for_each_entry_safe (ex, next, slot, hash_list)
395                         kmem_cache_free(mem, ex);
396         }
397
398         vfree(et->table);
399 }
400
401 static uint32_t exception_hash(struct dm_exception_table *et, chunk_t chunk)
402 {
403         return (chunk >> et->hash_shift) & et->hash_mask;
404 }
405
406 static void dm_remove_exception(struct dm_exception *e)
407 {
408         list_del(&e->hash_list);
409 }
410
411 /*
412  * Return the exception data for a sector, or NULL if not
413  * remapped.
414  */
415 static struct dm_exception *dm_lookup_exception(struct dm_exception_table *et,
416                                                 chunk_t chunk)
417 {
418         struct list_head *slot;
419         struct dm_exception *e;
420
421         slot = &et->table[exception_hash(et, chunk)];
422         list_for_each_entry (e, slot, hash_list)
423                 if (chunk >= e->old_chunk &&
424                     chunk <= e->old_chunk + dm_consecutive_chunk_count(e))
425                         return e;
426
427         return NULL;
428 }
429
430 static struct dm_exception *alloc_completed_exception(void)
431 {
432         struct dm_exception *e;
433
434         e = kmem_cache_alloc(exception_cache, GFP_NOIO);
435         if (!e)
436                 e = kmem_cache_alloc(exception_cache, GFP_ATOMIC);
437
438         return e;
439 }
440
441 static void free_completed_exception(struct dm_exception *e)
442 {
443         kmem_cache_free(exception_cache, e);
444 }
445
446 static struct dm_snap_pending_exception *alloc_pending_exception(struct dm_snapshot *s)
447 {
448         struct dm_snap_pending_exception *pe = mempool_alloc(s->pending_pool,
449                                                              GFP_NOIO);
450
451         atomic_inc(&s->pending_exceptions_count);
452         pe->snap = s;
453
454         return pe;
455 }
456
457 static void free_pending_exception(struct dm_snap_pending_exception *pe)
458 {
459         struct dm_snapshot *s = pe->snap;
460
461         mempool_free(pe, s->pending_pool);
462         smp_mb__before_atomic_dec();
463         atomic_dec(&s->pending_exceptions_count);
464 }
465
466 static void dm_insert_exception(struct dm_exception_table *eh,
467                                 struct dm_exception *new_e)
468 {
469         struct list_head *l;
470         struct dm_exception *e = NULL;
471
472         l = &eh->table[exception_hash(eh, new_e->old_chunk)];
473
474         /* Add immediately if this table doesn't support consecutive chunks */
475         if (!eh->hash_shift)
476                 goto out;
477
478         /* List is ordered by old_chunk */
479         list_for_each_entry_reverse(e, l, hash_list) {
480                 /* Insert after an existing chunk? */
481                 if (new_e->old_chunk == (e->old_chunk +
482                                          dm_consecutive_chunk_count(e) + 1) &&
483                     new_e->new_chunk == (dm_chunk_number(e->new_chunk) +
484                                          dm_consecutive_chunk_count(e) + 1)) {
485                         dm_consecutive_chunk_count_inc(e);
486                         free_completed_exception(new_e);
487                         return;
488                 }
489
490                 /* Insert before an existing chunk? */
491                 if (new_e->old_chunk == (e->old_chunk - 1) &&
492                     new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) {
493                         dm_consecutive_chunk_count_inc(e);
494                         e->old_chunk--;
495                         e->new_chunk--;
496                         free_completed_exception(new_e);
497                         return;
498                 }
499
500                 if (new_e->old_chunk > e->old_chunk)
501                         break;
502         }
503
504 out:
505         list_add(&new_e->hash_list, e ? &e->hash_list : l);
506 }
507
508 /*
509  * Callback used by the exception stores to load exceptions when
510  * initialising.
511  */
512 static int dm_add_exception(void *context, chunk_t old, chunk_t new)
513 {
514         struct dm_snapshot *s = context;
515         struct dm_exception *e;
516
517         e = alloc_completed_exception();
518         if (!e)
519                 return -ENOMEM;
520
521         e->old_chunk = old;
522
523         /* Consecutive_count is implicitly initialised to zero */
524         e->new_chunk = new;
525
526         dm_insert_exception(&s->complete, e);
527
528         return 0;
529 }
530
531 #define min_not_zero(l, r) (((l) == 0) ? (r) : (((r) == 0) ? (l) : min(l, r)))
532
533 /*
534  * Return a minimum chunk size of all snapshots that have the specified origin.
535  * Return zero if the origin has no snapshots.
536  */
537 static sector_t __minimum_chunk_size(struct origin *o)
538 {
539         struct dm_snapshot *snap;
540         unsigned chunk_size = 0;
541
542         if (o)
543                 list_for_each_entry(snap, &o->snapshots, list)
544                         chunk_size = min_not_zero(chunk_size,
545                                                   snap->store->chunk_size);
546
547         return chunk_size;
548 }
549
550 /*
551  * Hard coded magic.
552  */
553 static int calc_max_buckets(void)
554 {
555         /* use a fixed size of 2MB */
556         unsigned long mem = 2 * 1024 * 1024;
557         mem /= sizeof(struct list_head);
558
559         return mem;
560 }
561
562 /*
563  * Allocate room for a suitable hash table.
564  */
565 static int init_hash_tables(struct dm_snapshot *s)
566 {
567         sector_t hash_size, cow_dev_size, origin_dev_size, max_buckets;
568
569         /*
570          * Calculate based on the size of the original volume or
571          * the COW volume...
572          */
573         cow_dev_size = get_dev_size(s->cow->bdev);
574         origin_dev_size = get_dev_size(s->origin->bdev);
575         max_buckets = calc_max_buckets();
576
577         hash_size = min(origin_dev_size, cow_dev_size) >> s->store->chunk_shift;
578         hash_size = min(hash_size, max_buckets);
579
580         if (hash_size < 64)
581                 hash_size = 64;
582         hash_size = rounddown_pow_of_two(hash_size);
583         if (dm_exception_table_init(&s->complete, hash_size,
584                                     DM_CHUNK_CONSECUTIVE_BITS))
585                 return -ENOMEM;
586
587         /*
588          * Allocate hash table for in-flight exceptions
589          * Make this smaller than the real hash table
590          */
591         hash_size >>= 3;
592         if (hash_size < 64)
593                 hash_size = 64;
594
595         if (dm_exception_table_init(&s->pending, hash_size, 0)) {
596                 dm_exception_table_exit(&s->complete, exception_cache);
597                 return -ENOMEM;
598         }
599
600         return 0;
601 }
602
603 /*
604  * Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size>
605  */
606 static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
607 {
608         struct dm_snapshot *s;
609         int i;
610         int r = -EINVAL;
611         char *origin_path, *cow_path;
612         unsigned args_used;
613
614         if (argc != 4) {
615                 ti->error = "requires exactly 4 arguments";
616                 r = -EINVAL;
617                 goto bad;
618         }
619
620         origin_path = argv[0];
621         argv++;
622         argc--;
623
624         s = kmalloc(sizeof(*s), GFP_KERNEL);
625         if (!s) {
626                 ti->error = "Cannot allocate snapshot context private "
627                     "structure";
628                 r = -ENOMEM;
629                 goto bad;
630         }
631
632         cow_path = argv[0];
633         argv++;
634         argc--;
635
636         r = dm_get_device(ti, cow_path, 0, 0,
637                           FMODE_READ | FMODE_WRITE, &s->cow);
638         if (r) {
639                 ti->error = "Cannot get COW device";
640                 goto bad_cow;
641         }
642
643         r = dm_exception_store_create(ti, argc, argv, s, &args_used, &s->store);
644         if (r) {
645                 ti->error = "Couldn't create exception store";
646                 r = -EINVAL;
647                 goto bad_store;
648         }
649
650         argv += args_used;
651         argc -= args_used;
652
653         r = dm_get_device(ti, origin_path, 0, ti->len, FMODE_READ, &s->origin);
654         if (r) {
655                 ti->error = "Cannot get origin device";
656                 goto bad_origin;
657         }
658
659         s->ti = ti;
660         s->valid = 1;
661         s->active = 0;
662         s->suspended = 0;
663         atomic_set(&s->pending_exceptions_count, 0);
664         init_rwsem(&s->lock);
665         spin_lock_init(&s->pe_lock);
666
667         /* Allocate hash table for COW data */
668         if (init_hash_tables(s)) {
669                 ti->error = "Unable to allocate hash table space";
670                 r = -ENOMEM;
671                 goto bad_hash_tables;
672         }
673
674         r = dm_kcopyd_client_create(SNAPSHOT_PAGES, &s->kcopyd_client);
675         if (r) {
676                 ti->error = "Could not create kcopyd client";
677                 goto bad_kcopyd;
678         }
679
680         s->pending_pool = mempool_create_slab_pool(MIN_IOS, pending_cache);
681         if (!s->pending_pool) {
682                 ti->error = "Could not allocate mempool for pending exceptions";
683                 goto bad_pending_pool;
684         }
685
686         s->tracked_chunk_pool = mempool_create_slab_pool(MIN_IOS,
687                                                          tracked_chunk_cache);
688         if (!s->tracked_chunk_pool) {
689                 ti->error = "Could not allocate tracked_chunk mempool for "
690                             "tracking reads";
691                 goto bad_tracked_chunk_pool;
692         }
693
694         for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
695                 INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]);
696
697         spin_lock_init(&s->tracked_chunk_lock);
698
699         /* Metadata must only be loaded into one table at once */
700         r = s->store->type->read_metadata(s->store, dm_add_exception,
701                                           (void *)s);
702         if (r < 0) {
703                 ti->error = "Failed to read snapshot metadata";
704                 goto bad_load_and_register;
705         } else if (r > 0) {
706                 s->valid = 0;
707                 DMWARN("Snapshot is marked invalid.");
708         }
709
710         bio_list_init(&s->queued_bios);
711         INIT_WORK(&s->queued_bios_work, flush_queued_bios);
712
713         if (!s->store->chunk_size) {
714                 ti->error = "Chunk size not set";
715                 goto bad_load_and_register;
716         }
717
718         /* Add snapshot to the list of snapshots for this origin */
719         /* Exceptions aren't triggered till snapshot_resume() is called */
720         if (register_snapshot(s)) {
721                 r = -EINVAL;
722                 ti->error = "Cannot register snapshot origin";
723                 goto bad_load_and_register;
724         }
725
726         ti->private = s;
727         ti->split_io = s->store->chunk_size;
728         ti->num_flush_requests = 1;
729
730         return 0;
731
732 bad_load_and_register:
733         mempool_destroy(s->tracked_chunk_pool);
734
735 bad_tracked_chunk_pool:
736         mempool_destroy(s->pending_pool);
737
738 bad_pending_pool:
739         dm_kcopyd_client_destroy(s->kcopyd_client);
740
741 bad_kcopyd:
742         dm_exception_table_exit(&s->pending, pending_cache);
743         dm_exception_table_exit(&s->complete, exception_cache);
744
745 bad_hash_tables:
746         dm_put_device(ti, s->origin);
747
748 bad_origin:
749         dm_exception_store_destroy(s->store);
750
751 bad_store:
752         dm_put_device(ti, s->cow);
753
754 bad_cow:
755         kfree(s);
756
757 bad:
758         return r;
759 }
760
761 static void __free_exceptions(struct dm_snapshot *s)
762 {
763         dm_kcopyd_client_destroy(s->kcopyd_client);
764         s->kcopyd_client = NULL;
765
766         dm_exception_table_exit(&s->pending, pending_cache);
767         dm_exception_table_exit(&s->complete, exception_cache);
768 }
769
770 static void snapshot_dtr(struct dm_target *ti)
771 {
772 #ifdef CONFIG_DM_DEBUG
773         int i;
774 #endif
775         struct dm_snapshot *s = ti->private;
776
777         flush_workqueue(ksnapd);
778
779         /* Prevent further origin writes from using this snapshot. */
780         /* After this returns there can be no new kcopyd jobs. */
781         unregister_snapshot(s);
782
783         while (atomic_read(&s->pending_exceptions_count))
784                 msleep(1);
785         /*
786          * Ensure instructions in mempool_destroy aren't reordered
787          * before atomic_read.
788          */
789         smp_mb();
790
791 #ifdef CONFIG_DM_DEBUG
792         for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
793                 BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i]));
794 #endif
795
796         mempool_destroy(s->tracked_chunk_pool);
797
798         __free_exceptions(s);
799
800         mempool_destroy(s->pending_pool);
801
802         dm_put_device(ti, s->origin);
803
804         dm_exception_store_destroy(s->store);
805
806         dm_put_device(ti, s->cow);
807
808         kfree(s);
809 }
810
811 /*
812  * Flush a list of buffers.
813  */
814 static void flush_bios(struct bio *bio)
815 {
816         struct bio *n;
817
818         while (bio) {
819                 n = bio->bi_next;
820                 bio->bi_next = NULL;
821                 generic_make_request(bio);
822                 bio = n;
823         }
824 }
825
826 static void flush_queued_bios(struct work_struct *work)
827 {
828         struct dm_snapshot *s =
829                 container_of(work, struct dm_snapshot, queued_bios_work);
830         struct bio *queued_bios;
831         unsigned long flags;
832
833         spin_lock_irqsave(&s->pe_lock, flags);
834         queued_bios = bio_list_get(&s->queued_bios);
835         spin_unlock_irqrestore(&s->pe_lock, flags);
836
837         flush_bios(queued_bios);
838 }
839
840 /*
841  * Error a list of buffers.
842  */
843 static void error_bios(struct bio *bio)
844 {
845         struct bio *n;
846
847         while (bio) {
848                 n = bio->bi_next;
849                 bio->bi_next = NULL;
850                 bio_io_error(bio);
851                 bio = n;
852         }
853 }
854
855 static void __invalidate_snapshot(struct dm_snapshot *s, int err)
856 {
857         if (!s->valid)
858                 return;
859
860         if (err == -EIO)
861                 DMERR("Invalidating snapshot: Error reading/writing.");
862         else if (err == -ENOMEM)
863                 DMERR("Invalidating snapshot: Unable to allocate exception.");
864
865         if (s->store->type->drop_snapshot)
866                 s->store->type->drop_snapshot(s->store);
867
868         s->valid = 0;
869
870         dm_table_event(s->ti->table);
871 }
872
873 static void get_pending_exception(struct dm_snap_pending_exception *pe)
874 {
875         atomic_inc(&pe->ref_count);
876 }
877
878 static struct bio *put_pending_exception(struct dm_snap_pending_exception *pe)
879 {
880         struct dm_snap_pending_exception *primary_pe;
881         struct bio *origin_bios = NULL;
882
883         primary_pe = pe->primary_pe;
884
885         /*
886          * If this pe is involved in a write to the origin and
887          * it is the last sibling to complete then release
888          * the bios for the original write to the origin.
889          */
890         if (primary_pe &&
891             atomic_dec_and_test(&primary_pe->ref_count)) {
892                 origin_bios = bio_list_get(&primary_pe->origin_bios);
893                 free_pending_exception(primary_pe);
894         }
895
896         /*
897          * Free the pe if it's not linked to an origin write or if
898          * it's not itself a primary pe.
899          */
900         if (!primary_pe || primary_pe != pe)
901                 free_pending_exception(pe);
902
903         return origin_bios;
904 }
905
906 static void pending_complete(struct dm_snap_pending_exception *pe, int success)
907 {
908         struct dm_exception *e;
909         struct dm_snapshot *s = pe->snap;
910         struct bio *origin_bios = NULL;
911         struct bio *snapshot_bios = NULL;
912         int error = 0;
913
914         if (!success) {
915                 /* Read/write error - snapshot is unusable */
916                 down_write(&s->lock);
917                 __invalidate_snapshot(s, -EIO);
918                 error = 1;
919                 goto out;
920         }
921
922         e = alloc_completed_exception();
923         if (!e) {
924                 down_write(&s->lock);
925                 __invalidate_snapshot(s, -ENOMEM);
926                 error = 1;
927                 goto out;
928         }
929         *e = pe->e;
930
931         down_write(&s->lock);
932         if (!s->valid) {
933                 free_completed_exception(e);
934                 error = 1;
935                 goto out;
936         }
937
938         /*
939          * Check for conflicting reads. This is extremely improbable,
940          * so msleep(1) is sufficient and there is no need for a wait queue.
941          */
942         while (__chunk_is_tracked(s, pe->e.old_chunk))
943                 msleep(1);
944
945         /*
946          * Add a proper exception, and remove the
947          * in-flight exception from the list.
948          */
949         dm_insert_exception(&s->complete, e);
950
951  out:
952         dm_remove_exception(&pe->e);
953         snapshot_bios = bio_list_get(&pe->snapshot_bios);
954         origin_bios = put_pending_exception(pe);
955
956         up_write(&s->lock);
957
958         /* Submit any pending write bios */
959         if (error)
960                 error_bios(snapshot_bios);
961         else
962                 flush_bios(snapshot_bios);
963
964         flush_bios(origin_bios);
965 }
966
967 static void commit_callback(void *context, int success)
968 {
969         struct dm_snap_pending_exception *pe = context;
970
971         pending_complete(pe, success);
972 }
973
974 /*
975  * Called when the copy I/O has finished.  kcopyd actually runs
976  * this code so don't block.
977  */
978 static void copy_callback(int read_err, unsigned long write_err, void *context)
979 {
980         struct dm_snap_pending_exception *pe = context;
981         struct dm_snapshot *s = pe->snap;
982
983         if (read_err || write_err)
984                 pending_complete(pe, 0);
985
986         else
987                 /* Update the metadata if we are persistent */
988                 s->store->type->commit_exception(s->store, &pe->e,
989                                                  commit_callback, pe);
990 }
991
992 /*
993  * Dispatches the copy operation to kcopyd.
994  */
995 static void start_copy(struct dm_snap_pending_exception *pe)
996 {
997         struct dm_snapshot *s = pe->snap;
998         struct dm_io_region src, dest;
999         struct block_device *bdev = s->origin->bdev;
1000         sector_t dev_size;
1001
1002         dev_size = get_dev_size(bdev);
1003
1004         src.bdev = bdev;
1005         src.sector = chunk_to_sector(s->store, pe->e.old_chunk);
1006         src.count = min((sector_t)s->store->chunk_size, dev_size - src.sector);
1007
1008         dest.bdev = s->cow->bdev;
1009         dest.sector = chunk_to_sector(s->store, pe->e.new_chunk);
1010         dest.count = src.count;
1011
1012         /* Hand over to kcopyd */
1013         dm_kcopyd_copy(s->kcopyd_client,
1014                     &src, 1, &dest, 0, copy_callback, pe);
1015 }
1016
1017 static struct dm_snap_pending_exception *
1018 __lookup_pending_exception(struct dm_snapshot *s, chunk_t chunk)
1019 {
1020         struct dm_exception *e = dm_lookup_exception(&s->pending, chunk);
1021
1022         if (!e)
1023                 return NULL;
1024
1025         return container_of(e, struct dm_snap_pending_exception, e);
1026 }
1027
1028 /*
1029  * Looks to see if this snapshot already has a pending exception
1030  * for this chunk, otherwise it allocates a new one and inserts
1031  * it into the pending table.
1032  *
1033  * NOTE: a write lock must be held on snap->lock before calling
1034  * this.
1035  */
1036 static struct dm_snap_pending_exception *
1037 __find_pending_exception(struct dm_snapshot *s,
1038                          struct dm_snap_pending_exception *pe, chunk_t chunk)
1039 {
1040         struct dm_snap_pending_exception *pe2;
1041
1042         pe2 = __lookup_pending_exception(s, chunk);
1043         if (pe2) {
1044                 free_pending_exception(pe);
1045                 return pe2;
1046         }
1047
1048         pe->e.old_chunk = chunk;
1049         bio_list_init(&pe->origin_bios);
1050         bio_list_init(&pe->snapshot_bios);
1051         pe->primary_pe = NULL;
1052         atomic_set(&pe->ref_count, 0);
1053         pe->started = 0;
1054
1055         if (s->store->type->prepare_exception(s->store, &pe->e)) {
1056                 free_pending_exception(pe);
1057                 return NULL;
1058         }
1059
1060         get_pending_exception(pe);
1061         dm_insert_exception(&s->pending, &pe->e);
1062
1063         return pe;
1064 }
1065
1066 static void remap_exception(struct dm_snapshot *s, struct dm_exception *e,
1067                             struct bio *bio, chunk_t chunk)
1068 {
1069         bio->bi_bdev = s->cow->bdev;
1070         bio->bi_sector = chunk_to_sector(s->store,
1071                                          dm_chunk_number(e->new_chunk) +
1072                                          (chunk - e->old_chunk)) +
1073                                          (bio->bi_sector &
1074                                           s->store->chunk_mask);
1075 }
1076
1077 static int snapshot_map(struct dm_target *ti, struct bio *bio,
1078                         union map_info *map_context)
1079 {
1080         struct dm_exception *e;
1081         struct dm_snapshot *s = ti->private;
1082         int r = DM_MAPIO_REMAPPED;
1083         chunk_t chunk;
1084         struct dm_snap_pending_exception *pe = NULL;
1085
1086         if (unlikely(bio_empty_barrier(bio))) {
1087                 bio->bi_bdev = s->cow->bdev;
1088                 return DM_MAPIO_REMAPPED;
1089         }
1090
1091         chunk = sector_to_chunk(s->store, bio->bi_sector);
1092
1093         /* Full snapshots are not usable */
1094         /* To get here the table must be live so s->active is always set. */
1095         if (!s->valid)
1096                 return -EIO;
1097
1098         /* FIXME: should only take write lock if we need
1099          * to copy an exception */
1100         down_write(&s->lock);
1101
1102         if (!s->valid) {
1103                 r = -EIO;
1104                 goto out_unlock;
1105         }
1106
1107         /* If the block is already remapped - use that, else remap it */
1108         e = dm_lookup_exception(&s->complete, chunk);
1109         if (e) {
1110                 remap_exception(s, e, bio, chunk);
1111                 goto out_unlock;
1112         }
1113
1114         /*
1115          * Write to snapshot - higher level takes care of RW/RO
1116          * flags so we should only get this if we are
1117          * writeable.
1118          */
1119         if (bio_rw(bio) == WRITE) {
1120                 pe = __lookup_pending_exception(s, chunk);
1121                 if (!pe) {
1122                         up_write(&s->lock);
1123                         pe = alloc_pending_exception(s);
1124                         down_write(&s->lock);
1125
1126                         if (!s->valid) {
1127                                 free_pending_exception(pe);
1128                                 r = -EIO;
1129                                 goto out_unlock;
1130                         }
1131
1132                         e = dm_lookup_exception(&s->complete, chunk);
1133                         if (e) {
1134                                 free_pending_exception(pe);
1135                                 remap_exception(s, e, bio, chunk);
1136                                 goto out_unlock;
1137                         }
1138
1139                         pe = __find_pending_exception(s, pe, chunk);
1140                         if (!pe) {
1141                                 __invalidate_snapshot(s, -ENOMEM);
1142                                 r = -EIO;
1143                                 goto out_unlock;
1144                         }
1145                 }
1146
1147                 remap_exception(s, &pe->e, bio, chunk);
1148                 bio_list_add(&pe->snapshot_bios, bio);
1149
1150                 r = DM_MAPIO_SUBMITTED;
1151
1152                 if (!pe->started) {
1153                         /* this is protected by snap->lock */
1154                         pe->started = 1;
1155                         up_write(&s->lock);
1156                         start_copy(pe);
1157                         goto out;
1158                 }
1159         } else {
1160                 bio->bi_bdev = s->origin->bdev;
1161                 map_context->ptr = track_chunk(s, chunk);
1162         }
1163
1164  out_unlock:
1165         up_write(&s->lock);
1166  out:
1167         return r;
1168 }
1169
1170 static int snapshot_end_io(struct dm_target *ti, struct bio *bio,
1171                            int error, union map_info *map_context)
1172 {
1173         struct dm_snapshot *s = ti->private;
1174         struct dm_snap_tracked_chunk *c = map_context->ptr;
1175
1176         if (c)
1177                 stop_tracking_chunk(s, c);
1178
1179         return 0;
1180 }
1181
1182 static void snapshot_postsuspend(struct dm_target *ti)
1183 {
1184         struct dm_snapshot *s = ti->private;
1185
1186         down_write(&s->lock);
1187         s->suspended = 1;
1188         up_write(&s->lock);
1189 }
1190
1191 static void snapshot_resume(struct dm_target *ti)
1192 {
1193         struct dm_snapshot *s = ti->private;
1194
1195         down_write(&s->lock);
1196         s->active = 1;
1197         s->suspended = 0;
1198         up_write(&s->lock);
1199 }
1200
1201 static int snapshot_status(struct dm_target *ti, status_type_t type,
1202                            char *result, unsigned int maxlen)
1203 {
1204         unsigned sz = 0;
1205         struct dm_snapshot *snap = ti->private;
1206
1207         switch (type) {
1208         case STATUSTYPE_INFO:
1209
1210                 down_write(&snap->lock);
1211
1212                 if (!snap->valid)
1213                         DMEMIT("Invalid");
1214                 else {
1215                         if (snap->store->type->usage) {
1216                                 sector_t total_sectors, sectors_allocated,
1217                                          metadata_sectors;
1218                                 snap->store->type->usage(snap->store,
1219                                                          &total_sectors,
1220                                                          &sectors_allocated,
1221                                                          &metadata_sectors);
1222                                 DMEMIT("%llu/%llu %llu",
1223                                        (unsigned long long)sectors_allocated,
1224                                        (unsigned long long)total_sectors,
1225                                        (unsigned long long)metadata_sectors);
1226                         }
1227                         else
1228                                 DMEMIT("Unknown");
1229                 }
1230
1231                 up_write(&snap->lock);
1232
1233                 break;
1234
1235         case STATUSTYPE_TABLE:
1236                 /*
1237                  * kdevname returns a static pointer so we need
1238                  * to make private copies if the output is to
1239                  * make sense.
1240                  */
1241                 DMEMIT("%s %s", snap->origin->name, snap->cow->name);
1242                 snap->store->type->status(snap->store, type, result + sz,
1243                                           maxlen - sz);
1244                 break;
1245         }
1246
1247         return 0;
1248 }
1249
1250 static int snapshot_iterate_devices(struct dm_target *ti,
1251                                     iterate_devices_callout_fn fn, void *data)
1252 {
1253         struct dm_snapshot *snap = ti->private;
1254
1255         return fn(ti, snap->origin, 0, ti->len, data);
1256 }
1257
1258
1259 /*-----------------------------------------------------------------
1260  * Origin methods
1261  *---------------------------------------------------------------*/
1262 static int __origin_write(struct list_head *snapshots, struct bio *bio)
1263 {
1264         int r = DM_MAPIO_REMAPPED, first = 0;
1265         struct dm_snapshot *snap;
1266         struct dm_exception *e;
1267         struct dm_snap_pending_exception *pe, *next_pe, *primary_pe = NULL;
1268         chunk_t chunk;
1269         LIST_HEAD(pe_queue);
1270
1271         /* Do all the snapshots on this origin */
1272         list_for_each_entry (snap, snapshots, list) {
1273
1274                 down_write(&snap->lock);
1275
1276                 /* Only deal with valid and active snapshots */
1277                 if (!snap->valid || !snap->active)
1278                         goto next_snapshot;
1279
1280                 /* Nothing to do if writing beyond end of snapshot */
1281                 if (bio->bi_sector >= dm_table_get_size(snap->ti->table))
1282                         goto next_snapshot;
1283
1284                 /*
1285                  * Remember, different snapshots can have
1286                  * different chunk sizes.
1287                  */
1288                 chunk = sector_to_chunk(snap->store, bio->bi_sector);
1289
1290                 /*
1291                  * Check exception table to see if block
1292                  * is already remapped in this snapshot
1293                  * and trigger an exception if not.
1294                  *
1295                  * ref_count is initialised to 1 so pending_complete()
1296                  * won't destroy the primary_pe while we're inside this loop.
1297                  */
1298                 e = dm_lookup_exception(&snap->complete, chunk);
1299                 if (e)
1300                         goto next_snapshot;
1301
1302                 pe = __lookup_pending_exception(snap, chunk);
1303                 if (!pe) {
1304                         up_write(&snap->lock);
1305                         pe = alloc_pending_exception(snap);
1306                         down_write(&snap->lock);
1307
1308                         if (!snap->valid) {
1309                                 free_pending_exception(pe);
1310                                 goto next_snapshot;
1311                         }
1312
1313                         e = dm_lookup_exception(&snap->complete, chunk);
1314                         if (e) {
1315                                 free_pending_exception(pe);
1316                                 goto next_snapshot;
1317                         }
1318
1319                         pe = __find_pending_exception(snap, pe, chunk);
1320                         if (!pe) {
1321                                 __invalidate_snapshot(snap, -ENOMEM);
1322                                 goto next_snapshot;
1323                         }
1324                 }
1325
1326                 if (!primary_pe) {
1327                         /*
1328                          * Either every pe here has same
1329                          * primary_pe or none has one yet.
1330                          */
1331                         if (pe->primary_pe)
1332                                 primary_pe = pe->primary_pe;
1333                         else {
1334                                 primary_pe = pe;
1335                                 first = 1;
1336                         }
1337
1338                         bio_list_add(&primary_pe->origin_bios, bio);
1339
1340                         r = DM_MAPIO_SUBMITTED;
1341                 }
1342
1343                 if (!pe->primary_pe) {
1344                         pe->primary_pe = primary_pe;
1345                         get_pending_exception(primary_pe);
1346                 }
1347
1348                 if (!pe->started) {
1349                         pe->started = 1;
1350                         list_add_tail(&pe->list, &pe_queue);
1351                 }
1352
1353  next_snapshot:
1354                 up_write(&snap->lock);
1355         }
1356
1357         if (!primary_pe)
1358                 return r;
1359
1360         /*
1361          * If this is the first time we're processing this chunk and
1362          * ref_count is now 1 it means all the pending exceptions
1363          * got completed while we were in the loop above, so it falls to
1364          * us here to remove the primary_pe and submit any origin_bios.
1365          */
1366
1367         if (first && atomic_dec_and_test(&primary_pe->ref_count)) {
1368                 flush_bios(bio_list_get(&primary_pe->origin_bios));
1369                 free_pending_exception(primary_pe);
1370                 /* If we got here, pe_queue is necessarily empty. */
1371                 return r;
1372         }
1373
1374         /*
1375          * Now that we have a complete pe list we can start the copying.
1376          */
1377         list_for_each_entry_safe(pe, next_pe, &pe_queue, list)
1378                 start_copy(pe);
1379
1380         return r;
1381 }
1382
1383 /*
1384  * Called on a write from the origin driver.
1385  */
1386 static int do_origin(struct dm_dev *origin, struct bio *bio)
1387 {
1388         struct origin *o;
1389         int r = DM_MAPIO_REMAPPED;
1390
1391         down_read(&_origins_lock);
1392         o = __lookup_origin(origin->bdev);
1393         if (o)
1394                 r = __origin_write(&o->snapshots, bio);
1395         up_read(&_origins_lock);
1396
1397         return r;
1398 }
1399
1400 /*
1401  * Origin: maps a linear range of a device, with hooks for snapshotting.
1402  */
1403
1404 /*
1405  * Construct an origin mapping: <dev_path>
1406  * The context for an origin is merely a 'struct dm_dev *'
1407  * pointing to the real device.
1408  */
1409 static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1410 {
1411         int r;
1412         struct dm_dev *dev;
1413
1414         if (argc != 1) {
1415                 ti->error = "origin: incorrect number of arguments";
1416                 return -EINVAL;
1417         }
1418
1419         r = dm_get_device(ti, argv[0], 0, ti->len,
1420                           dm_table_get_mode(ti->table), &dev);
1421         if (r) {
1422                 ti->error = "Cannot get target device";
1423                 return r;
1424         }
1425
1426         ti->private = dev;
1427         ti->num_flush_requests = 1;
1428
1429         return 0;
1430 }
1431
1432 static void origin_dtr(struct dm_target *ti)
1433 {
1434         struct dm_dev *dev = ti->private;
1435         dm_put_device(ti, dev);
1436 }
1437
1438 static int origin_map(struct dm_target *ti, struct bio *bio,
1439                       union map_info *map_context)
1440 {
1441         struct dm_dev *dev = ti->private;
1442         bio->bi_bdev = dev->bdev;
1443
1444         if (unlikely(bio_empty_barrier(bio)))
1445                 return DM_MAPIO_REMAPPED;
1446
1447         /* Only tell snapshots if this is a write */
1448         return (bio_rw(bio) == WRITE) ? do_origin(dev, bio) : DM_MAPIO_REMAPPED;
1449 }
1450
1451 /*
1452  * Set the target "split_io" field to the minimum of all the snapshots'
1453  * chunk sizes.
1454  */
1455 static void origin_resume(struct dm_target *ti)
1456 {
1457         struct dm_dev *dev = ti->private;
1458
1459         down_read(&_origins_lock);
1460
1461         ti->split_io = __minimum_chunk_size(__lookup_origin(dev->bdev));
1462
1463         up_read(&_origins_lock);
1464 }
1465
1466 static int origin_status(struct dm_target *ti, status_type_t type, char *result,
1467                          unsigned int maxlen)
1468 {
1469         struct dm_dev *dev = ti->private;
1470
1471         switch (type) {
1472         case STATUSTYPE_INFO:
1473                 result[0] = '\0';
1474                 break;
1475
1476         case STATUSTYPE_TABLE:
1477                 snprintf(result, maxlen, "%s", dev->name);
1478                 break;
1479         }
1480
1481         return 0;
1482 }
1483
1484 static int origin_iterate_devices(struct dm_target *ti,
1485                                   iterate_devices_callout_fn fn, void *data)
1486 {
1487         struct dm_dev *dev = ti->private;
1488
1489         return fn(ti, dev, 0, ti->len, data);
1490 }
1491
1492 static struct target_type origin_target = {
1493         .name    = "snapshot-origin",
1494         .version = {1, 7, 0},
1495         .module  = THIS_MODULE,
1496         .ctr     = origin_ctr,
1497         .dtr     = origin_dtr,
1498         .map     = origin_map,
1499         .resume  = origin_resume,
1500         .status  = origin_status,
1501         .iterate_devices = origin_iterate_devices,
1502 };
1503
1504 static struct target_type snapshot_target = {
1505         .name    = "snapshot",
1506         .version = {1, 9, 0},
1507         .module  = THIS_MODULE,
1508         .ctr     = snapshot_ctr,
1509         .dtr     = snapshot_dtr,
1510         .map     = snapshot_map,
1511         .end_io  = snapshot_end_io,
1512         .postsuspend = snapshot_postsuspend,
1513         .resume  = snapshot_resume,
1514         .status  = snapshot_status,
1515         .iterate_devices = snapshot_iterate_devices,
1516 };
1517
1518 static int __init dm_snapshot_init(void)
1519 {
1520         int r;
1521
1522         r = dm_exception_store_init();
1523         if (r) {
1524                 DMERR("Failed to initialize exception stores");
1525                 return r;
1526         }
1527
1528         r = dm_register_target(&snapshot_target);
1529         if (r) {
1530                 DMERR("snapshot target register failed %d", r);
1531                 goto bad_register_snapshot_target;
1532         }
1533
1534         r = dm_register_target(&origin_target);
1535         if (r < 0) {
1536                 DMERR("Origin target register failed %d", r);
1537                 goto bad1;
1538         }
1539
1540         r = init_origin_hash();
1541         if (r) {
1542                 DMERR("init_origin_hash failed.");
1543                 goto bad2;
1544         }
1545
1546         exception_cache = KMEM_CACHE(dm_exception, 0);
1547         if (!exception_cache) {
1548                 DMERR("Couldn't create exception cache.");
1549                 r = -ENOMEM;
1550                 goto bad3;
1551         }
1552
1553         pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0);
1554         if (!pending_cache) {
1555                 DMERR("Couldn't create pending cache.");
1556                 r = -ENOMEM;
1557                 goto bad4;
1558         }
1559
1560         tracked_chunk_cache = KMEM_CACHE(dm_snap_tracked_chunk, 0);
1561         if (!tracked_chunk_cache) {
1562                 DMERR("Couldn't create cache to track chunks in use.");
1563                 r = -ENOMEM;
1564                 goto bad5;
1565         }
1566
1567         ksnapd = create_singlethread_workqueue("ksnapd");
1568         if (!ksnapd) {
1569                 DMERR("Failed to create ksnapd workqueue.");
1570                 r = -ENOMEM;
1571                 goto bad_pending_pool;
1572         }
1573
1574         return 0;
1575
1576 bad_pending_pool:
1577         kmem_cache_destroy(tracked_chunk_cache);
1578 bad5:
1579         kmem_cache_destroy(pending_cache);
1580 bad4:
1581         kmem_cache_destroy(exception_cache);
1582 bad3:
1583         exit_origin_hash();
1584 bad2:
1585         dm_unregister_target(&origin_target);
1586 bad1:
1587         dm_unregister_target(&snapshot_target);
1588
1589 bad_register_snapshot_target:
1590         dm_exception_store_exit();
1591         return r;
1592 }
1593
1594 static void __exit dm_snapshot_exit(void)
1595 {
1596         destroy_workqueue(ksnapd);
1597
1598         dm_unregister_target(&snapshot_target);
1599         dm_unregister_target(&origin_target);
1600
1601         exit_origin_hash();
1602         kmem_cache_destroy(pending_cache);
1603         kmem_cache_destroy(exception_cache);
1604         kmem_cache_destroy(tracked_chunk_cache);
1605
1606         dm_exception_store_exit();
1607 }
1608
1609 /* Module hooks */
1610 module_init(dm_snapshot_init);
1611 module_exit(dm_snapshot_exit);
1612
1613 MODULE_DESCRIPTION(DM_NAME " snapshot target");
1614 MODULE_AUTHOR("Joe Thornber");
1615 MODULE_LICENSE("GPL");