blob: 7a90fed033fe8f482d437e6df2798709ca9978e9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/ctype.h>
11#include <linux/device-mapper.h>
Mikulas Patocka90fa1522009-01-06 03:04:54 +000012#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/fs.h>
14#include <linux/init.h>
15#include <linux/kdev_t.h>
16#include <linux/list.h>
17#include <linux/mempool.h>
18#include <linux/module.h>
19#include <linux/slab.h>
20#include <linux/vmalloc.h>
vignesh babu6f3c3f02007-10-19 22:38:44 +010021#include <linux/log2.h>
Alasdair G Kergona765e202008-04-24 22:02:01 +010022#include <linux/dm-kcopyd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Jonathan Brassowaea53d92009-01-06 03:05:15 +000024#include "dm-exception-store.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "dm-snap.h"
26#include "dm-bio-list.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Alasdair G Kergon72d94862006-06-26 00:27:35 -070028#define DM_MSG_PREFIX "snapshots"
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030/*
31 * The percentage increment we will wake up users at
32 */
33#define WAKE_UP_PERCENT 5
34
35/*
36 * kcopyd priority of snapshot operations
37 */
38#define SNAPSHOT_COPY_PRIORITY 2
39
40/*
Milan Broz8ee27672008-04-24 21:42:36 +010041 * Reserve 1MB for each snapshot initially (with minimum of 1 page).
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 */
Milan Broz8ee27672008-04-24 21:42:36 +010043#define SNAPSHOT_PAGES (((1UL << 20) >> PAGE_SHIFT) ? : 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Mikulas Patockacd45daf2008-07-21 12:00:32 +010045/*
46 * The size of the mempool used to track chunks in use.
47 */
48#define MIN_IOS 256
49
Adrian Bunkc642f9e2006-12-08 02:41:13 -080050static struct workqueue_struct *ksnapd;
David Howellsc4028952006-11-22 14:57:56 +000051static void flush_queued_bios(struct work_struct *work);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -070052
Alasdair G Kergon028867a2007-07-12 17:26:32 +010053struct dm_snap_pending_exception {
54 struct dm_snap_exception e;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56 /*
57 * Origin buffers waiting for this to complete are held
58 * in a bio list
59 */
60 struct bio_list origin_bios;
61 struct bio_list snapshot_bios;
62
63 /*
Alasdair G Kergoneccf0812006-03-27 01:17:42 -080064 * Short-term queue of pending exceptions prior to submission.
65 */
66 struct list_head list;
67
68 /*
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -080069 * The primary pending_exception is the one that holds
Alasdair G Kergon4b832e82006-10-03 01:15:30 -070070 * the ref_count and the list of origin_bios for a
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -080071 * group of pending_exceptions. It is always last to get freed.
72 * These fields get set up when writing to the origin.
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +010074 struct dm_snap_pending_exception *primary_pe;
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -080075
76 /*
77 * Number of pending_exceptions processing this chunk.
78 * When this drops to zero we must complete the origin bios.
79 * If incrementing or decrementing this, hold pe->snap->lock for
80 * the sibling concerned and not pe->primary_pe->snap->lock unless
81 * they are the same.
82 */
Alasdair G Kergon4b832e82006-10-03 01:15:30 -070083 atomic_t ref_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 /* Pointer back to snapshot context */
86 struct dm_snapshot *snap;
87
88 /*
89 * 1 indicates the exception has already been sent to
90 * kcopyd.
91 */
92 int started;
93};
94
95/*
96 * Hash table mapping origin volumes to lists of snapshots and
97 * a lock to protect it
98 */
Christoph Lametere18b8902006-12-06 20:33:20 -080099static struct kmem_cache *exception_cache;
100static struct kmem_cache *pending_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100102struct dm_snap_tracked_chunk {
103 struct hlist_node node;
104 chunk_t chunk;
105};
106
107static struct kmem_cache *tracked_chunk_cache;
108
109static struct dm_snap_tracked_chunk *track_chunk(struct dm_snapshot *s,
110 chunk_t chunk)
111{
112 struct dm_snap_tracked_chunk *c = mempool_alloc(s->tracked_chunk_pool,
113 GFP_NOIO);
114 unsigned long flags;
115
116 c->chunk = chunk;
117
118 spin_lock_irqsave(&s->tracked_chunk_lock, flags);
119 hlist_add_head(&c->node,
120 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)]);
121 spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
122
123 return c;
124}
125
126static void stop_tracking_chunk(struct dm_snapshot *s,
127 struct dm_snap_tracked_chunk *c)
128{
129 unsigned long flags;
130
131 spin_lock_irqsave(&s->tracked_chunk_lock, flags);
132 hlist_del(&c->node);
133 spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
134
135 mempool_free(c, s->tracked_chunk_pool);
136}
137
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100138static int __chunk_is_tracked(struct dm_snapshot *s, chunk_t chunk)
139{
140 struct dm_snap_tracked_chunk *c;
141 struct hlist_node *hn;
142 int found = 0;
143
144 spin_lock_irq(&s->tracked_chunk_lock);
145
146 hlist_for_each_entry(c, hn,
147 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)], node) {
148 if (c->chunk == chunk) {
149 found = 1;
150 break;
151 }
152 }
153
154 spin_unlock_irq(&s->tracked_chunk_lock);
155
156 return found;
157}
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159/*
160 * One of these per registered origin, held in the snapshot_origins hash
161 */
162struct origin {
163 /* The origin device */
164 struct block_device *bdev;
165
166 struct list_head hash_list;
167
168 /* List of snapshots for this origin */
169 struct list_head snapshots;
170};
171
172/*
173 * Size of the hash table for origin volumes. If we make this
174 * the size of the minors list then it should be nearly perfect
175 */
176#define ORIGIN_HASH_SIZE 256
177#define ORIGIN_MASK 0xFF
178static struct list_head *_origins;
179static struct rw_semaphore _origins_lock;
180
181static int init_origin_hash(void)
182{
183 int i;
184
185 _origins = kmalloc(ORIGIN_HASH_SIZE * sizeof(struct list_head),
186 GFP_KERNEL);
187 if (!_origins) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700188 DMERR("unable to allocate memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return -ENOMEM;
190 }
191
192 for (i = 0; i < ORIGIN_HASH_SIZE; i++)
193 INIT_LIST_HEAD(_origins + i);
194 init_rwsem(&_origins_lock);
195
196 return 0;
197}
198
199static void exit_origin_hash(void)
200{
201 kfree(_origins);
202}
203
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100204static unsigned origin_hash(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 return bdev->bd_dev & ORIGIN_MASK;
207}
208
209static struct origin *__lookup_origin(struct block_device *origin)
210{
211 struct list_head *ol;
212 struct origin *o;
213
214 ol = &_origins[origin_hash(origin)];
215 list_for_each_entry (o, ol, hash_list)
216 if (bdev_equal(o->bdev, origin))
217 return o;
218
219 return NULL;
220}
221
222static void __insert_origin(struct origin *o)
223{
224 struct list_head *sl = &_origins[origin_hash(o->bdev)];
225 list_add_tail(&o->hash_list, sl);
226}
227
228/*
229 * Make a note of the snapshot and its origin so we can look it
230 * up when the origin has a write on it.
231 */
232static int register_snapshot(struct dm_snapshot *snap)
233{
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000234 struct origin *o, *new_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 struct block_device *bdev = snap->origin->bdev;
236
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000237 new_o = kmalloc(sizeof(*new_o), GFP_KERNEL);
238 if (!new_o)
239 return -ENOMEM;
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 down_write(&_origins_lock);
242 o = __lookup_origin(bdev);
243
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000244 if (o)
245 kfree(new_o);
246 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 /* New origin */
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000248 o = new_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 /* Initialise the struct */
251 INIT_LIST_HEAD(&o->snapshots);
252 o->bdev = bdev;
253
254 __insert_origin(o);
255 }
256
257 list_add_tail(&snap->list, &o->snapshots);
258
259 up_write(&_origins_lock);
260 return 0;
261}
262
263static void unregister_snapshot(struct dm_snapshot *s)
264{
265 struct origin *o;
266
267 down_write(&_origins_lock);
268 o = __lookup_origin(s->origin->bdev);
269
270 list_del(&s->list);
271 if (list_empty(&o->snapshots)) {
272 list_del(&o->hash_list);
273 kfree(o);
274 }
275
276 up_write(&_origins_lock);
277}
278
279/*
280 * Implementation of the exception hash tables.
Milan Brozd74f81f2008-02-08 02:11:27 +0000281 * The lowest hash_shift bits of the chunk number are ignored, allowing
282 * some consecutive chunks to be grouped together.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 */
Milan Brozd74f81f2008-02-08 02:11:27 +0000284static int init_exception_table(struct exception_table *et, uint32_t size,
285 unsigned hash_shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 unsigned int i;
288
Milan Brozd74f81f2008-02-08 02:11:27 +0000289 et->hash_shift = hash_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 et->hash_mask = size - 1;
291 et->table = dm_vcalloc(size, sizeof(struct list_head));
292 if (!et->table)
293 return -ENOMEM;
294
295 for (i = 0; i < size; i++)
296 INIT_LIST_HEAD(et->table + i);
297
298 return 0;
299}
300
Christoph Lametere18b8902006-12-06 20:33:20 -0800301static void exit_exception_table(struct exception_table *et, struct kmem_cache *mem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 struct list_head *slot;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100304 struct dm_snap_exception *ex, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 int i, size;
306
307 size = et->hash_mask + 1;
308 for (i = 0; i < size; i++) {
309 slot = et->table + i;
310
311 list_for_each_entry_safe (ex, next, slot, hash_list)
312 kmem_cache_free(mem, ex);
313 }
314
315 vfree(et->table);
316}
317
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100318static uint32_t exception_hash(struct exception_table *et, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
Milan Brozd74f81f2008-02-08 02:11:27 +0000320 return (chunk >> et->hash_shift) & et->hash_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100323static void insert_exception(struct exception_table *eh,
324 struct dm_snap_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 struct list_head *l = &eh->table[exception_hash(eh, e->old_chunk)];
327 list_add(&e->hash_list, l);
328}
329
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100330static void remove_exception(struct dm_snap_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 list_del(&e->hash_list);
333}
334
335/*
336 * Return the exception data for a sector, or NULL if not
337 * remapped.
338 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100339static struct dm_snap_exception *lookup_exception(struct exception_table *et,
340 chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
342 struct list_head *slot;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100343 struct dm_snap_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 slot = &et->table[exception_hash(et, chunk)];
346 list_for_each_entry (e, slot, hash_list)
Milan Brozd74f81f2008-02-08 02:11:27 +0000347 if (chunk >= e->old_chunk &&
348 chunk <= e->old_chunk + dm_consecutive_chunk_count(e))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return e;
350
351 return NULL;
352}
353
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100354static struct dm_snap_exception *alloc_exception(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100356 struct dm_snap_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 e = kmem_cache_alloc(exception_cache, GFP_NOIO);
359 if (!e)
360 e = kmem_cache_alloc(exception_cache, GFP_ATOMIC);
361
362 return e;
363}
364
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100365static void free_exception(struct dm_snap_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
367 kmem_cache_free(exception_cache, e);
368}
369
Mikulas Patocka92e86812008-07-21 12:00:35 +0100370static struct dm_snap_pending_exception *alloc_pending_exception(struct dm_snapshot *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Mikulas Patocka92e86812008-07-21 12:00:35 +0100372 struct dm_snap_pending_exception *pe = mempool_alloc(s->pending_pool,
373 GFP_NOIO);
374
Mikulas Patocka879129d22008-10-30 13:33:16 +0000375 atomic_inc(&s->pending_exceptions_count);
Mikulas Patocka92e86812008-07-21 12:00:35 +0100376 pe->snap = s;
377
378 return pe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100381static void free_pending_exception(struct dm_snap_pending_exception *pe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Mikulas Patocka879129d22008-10-30 13:33:16 +0000383 struct dm_snapshot *s = pe->snap;
384
385 mempool_free(pe, s->pending_pool);
386 smp_mb__before_atomic_dec();
387 atomic_dec(&s->pending_exceptions_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
Milan Brozd74f81f2008-02-08 02:11:27 +0000390static void insert_completed_exception(struct dm_snapshot *s,
391 struct dm_snap_exception *new_e)
392{
393 struct exception_table *eh = &s->complete;
394 struct list_head *l;
395 struct dm_snap_exception *e = NULL;
396
397 l = &eh->table[exception_hash(eh, new_e->old_chunk)];
398
399 /* Add immediately if this table doesn't support consecutive chunks */
400 if (!eh->hash_shift)
401 goto out;
402
403 /* List is ordered by old_chunk */
404 list_for_each_entry_reverse(e, l, hash_list) {
405 /* Insert after an existing chunk? */
406 if (new_e->old_chunk == (e->old_chunk +
407 dm_consecutive_chunk_count(e) + 1) &&
408 new_e->new_chunk == (dm_chunk_number(e->new_chunk) +
409 dm_consecutive_chunk_count(e) + 1)) {
410 dm_consecutive_chunk_count_inc(e);
411 free_exception(new_e);
412 return;
413 }
414
415 /* Insert before an existing chunk? */
416 if (new_e->old_chunk == (e->old_chunk - 1) &&
417 new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) {
418 dm_consecutive_chunk_count_inc(e);
419 e->old_chunk--;
420 e->new_chunk--;
421 free_exception(new_e);
422 return;
423 }
424
425 if (new_e->old_chunk > e->old_chunk)
426 break;
427 }
428
429out:
430 list_add(&new_e->hash_list, e ? &e->hash_list : l);
431}
432
Jonathan Brassowa159c1a2009-01-06 03:05:19 +0000433/*
434 * Callback used by the exception stores to load exceptions when
435 * initialising.
436 */
437static int dm_add_exception(void *context, chunk_t old, chunk_t new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Jonathan Brassowa159c1a2009-01-06 03:05:19 +0000439 struct dm_snapshot *s = context;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100440 struct dm_snap_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 e = alloc_exception();
443 if (!e)
444 return -ENOMEM;
445
446 e->old_chunk = old;
Milan Brozd74f81f2008-02-08 02:11:27 +0000447
448 /* Consecutive_count is implicitly initialised to zero */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 e->new_chunk = new;
Milan Brozd74f81f2008-02-08 02:11:27 +0000450
451 insert_completed_exception(s, e);
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return 0;
454}
455
456/*
457 * Hard coded magic.
458 */
459static int calc_max_buckets(void)
460{
461 /* use a fixed size of 2MB */
462 unsigned long mem = 2 * 1024 * 1024;
463 mem /= sizeof(struct list_head);
464
465 return mem;
466}
467
468/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 * Allocate room for a suitable hash table.
470 */
Jonathan Brassowd0216842009-04-02 19:55:32 +0100471static int init_hash_tables(struct dm_snapshot *s, chunk_t chunk_shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
473 sector_t hash_size, cow_dev_size, origin_dev_size, max_buckets;
474
475 /*
476 * Calculate based on the size of the original volume or
477 * the COW volume...
478 */
479 cow_dev_size = get_dev_size(s->cow->bdev);
480 origin_dev_size = get_dev_size(s->origin->bdev);
481 max_buckets = calc_max_buckets();
482
Jonathan Brassowd0216842009-04-02 19:55:32 +0100483 hash_size = min(origin_dev_size, cow_dev_size) >> chunk_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 hash_size = min(hash_size, max_buckets);
485
Robert P. J. Day8defd832008-02-08 02:10:06 +0000486 hash_size = rounddown_pow_of_two(hash_size);
Milan Brozd74f81f2008-02-08 02:11:27 +0000487 if (init_exception_table(&s->complete, hash_size,
488 DM_CHUNK_CONSECUTIVE_BITS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return -ENOMEM;
490
491 /*
492 * Allocate hash table for in-flight exceptions
493 * Make this smaller than the real hash table
494 */
495 hash_size >>= 3;
496 if (hash_size < 64)
497 hash_size = 64;
498
Milan Brozd74f81f2008-02-08 02:11:27 +0000499 if (init_exception_table(&s->pending, hash_size, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 exit_exception_table(&s->complete, exception_cache);
501 return -ENOMEM;
502 }
503
504 return 0;
505}
506
507/*
508 * Round a number up to the nearest 'size' boundary. size must
509 * be a power of 2.
510 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100511static ulong round_up(ulong n, ulong size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
513 size--;
514 return (n + size) & ~size;
515}
516
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700517static int set_chunk_size(struct dm_snapshot *s, const char *chunk_size_arg,
Jonathan Brassowd0216842009-04-02 19:55:32 +0100518 chunk_t *chunk_size, chunk_t *chunk_mask,
519 chunk_t *chunk_shift, char **error)
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700520{
Jonathan Brassowd0216842009-04-02 19:55:32 +0100521 unsigned long chunk_size_ulong;
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700522 char *value;
523
Jonathan Brassowd0216842009-04-02 19:55:32 +0100524 chunk_size_ulong = simple_strtoul(chunk_size_arg, &value, 10);
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700525 if (*chunk_size_arg == '\0' || *value != '\0') {
526 *error = "Invalid chunk size";
527 return -EINVAL;
528 }
529
Jonathan Brassowd0216842009-04-02 19:55:32 +0100530 if (!chunk_size_ulong) {
531 *chunk_size = *chunk_mask = *chunk_shift = 0;
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700532 return 0;
533 }
534
535 /*
536 * Chunk size must be multiple of page size. Silently
537 * round up if it's not.
538 */
Jonathan Brassowd0216842009-04-02 19:55:32 +0100539 chunk_size_ulong = round_up(chunk_size_ulong, PAGE_SIZE >> 9);
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700540
541 /* Check chunk_size is a power of 2 */
Jonathan Brassowd0216842009-04-02 19:55:32 +0100542 if (!is_power_of_2(chunk_size_ulong)) {
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700543 *error = "Chunk size is not a power of 2";
544 return -EINVAL;
545 }
546
547 /* Validate the chunk size against the device block size */
Jonathan Brassowd0216842009-04-02 19:55:32 +0100548 if (chunk_size_ulong % (bdev_hardsect_size(s->cow->bdev) >> 9)) {
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700549 *error = "Chunk size is not a multiple of device blocksize";
550 return -EINVAL;
551 }
552
Jonathan Brassowd0216842009-04-02 19:55:32 +0100553 *chunk_size = chunk_size_ulong;
554 *chunk_mask = chunk_size_ulong - 1;
555 *chunk_shift = ffs(chunk_size_ulong) - 1;
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700556
557 return 0;
558}
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560/*
561 * Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size>
562 */
563static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
564{
565 struct dm_snapshot *s;
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100566 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 int r = -EINVAL;
568 char persistent;
569 char *origin_path;
570 char *cow_path;
Jonathan Brassowd0216842009-04-02 19:55:32 +0100571 chunk_t chunk_size, chunk_mask, chunk_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700573 if (argc != 4) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700574 ti->error = "requires exactly 4 arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 r = -EINVAL;
576 goto bad1;
577 }
578
579 origin_path = argv[0];
580 cow_path = argv[1];
581 persistent = toupper(*argv[2]);
582
583 if (persistent != 'P' && persistent != 'N') {
584 ti->error = "Persistent flag is not P or N";
585 r = -EINVAL;
586 goto bad1;
587 }
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 s = kmalloc(sizeof(*s), GFP_KERNEL);
590 if (s == NULL) {
591 ti->error = "Cannot allocate snapshot context private "
592 "structure";
593 r = -ENOMEM;
594 goto bad1;
595 }
596
597 r = dm_get_device(ti, origin_path, 0, ti->len, FMODE_READ, &s->origin);
598 if (r) {
599 ti->error = "Cannot get origin device";
600 goto bad2;
601 }
602
603 r = dm_get_device(ti, cow_path, 0, 0,
604 FMODE_READ | FMODE_WRITE, &s->cow);
605 if (r) {
606 dm_put_device(ti, s->origin);
607 ti->error = "Cannot get COW device";
608 goto bad2;
609 }
610
Jonathan Brassowd0216842009-04-02 19:55:32 +0100611 r = set_chunk_size(s, argv[3], &chunk_size, &chunk_mask, &chunk_shift,
612 &ti->error);
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700613 if (r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 goto bad3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 s->valid = 1;
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -0800617 s->active = 0;
Mikulas Patocka879129d22008-10-30 13:33:16 +0000618 atomic_set(&s->pending_exceptions_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 init_rwsem(&s->lock);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700620 spin_lock_init(&s->pe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 /* Allocate hash table for COW data */
Jonathan Brassowd0216842009-04-02 19:55:32 +0100623 if (init_hash_tables(s, chunk_shift)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 ti->error = "Unable to allocate hash table space";
625 r = -ENOMEM;
626 goto bad3;
627 }
628
Jonathan Brassowd0216842009-04-02 19:55:32 +0100629 r = dm_exception_store_create(argv[2], ti, chunk_size, chunk_mask,
630 chunk_shift, &s->store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (r) {
632 ti->error = "Couldn't create exception store";
633 r = -EINVAL;
634 goto bad4;
635 }
636
Jonathan Brassow493df712009-04-02 19:55:31 +0100637 s->store->snap = s;
638
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100639 r = dm_kcopyd_client_create(SNAPSHOT_PAGES, &s->kcopyd_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (r) {
641 ti->error = "Could not create kcopyd client";
642 goto bad5;
643 }
644
Mikulas Patocka92e86812008-07-21 12:00:35 +0100645 s->pending_pool = mempool_create_slab_pool(MIN_IOS, pending_cache);
646 if (!s->pending_pool) {
647 ti->error = "Could not allocate mempool for pending exceptions";
648 goto bad6;
649 }
650
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100651 s->tracked_chunk_pool = mempool_create_slab_pool(MIN_IOS,
652 tracked_chunk_cache);
653 if (!s->tracked_chunk_pool) {
654 ti->error = "Could not allocate tracked_chunk mempool for "
655 "tracking reads";
Mikulas Patocka92e86812008-07-21 12:00:35 +0100656 goto bad_tracked_chunk_pool;
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100657 }
658
659 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
660 INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]);
661
662 spin_lock_init(&s->tracked_chunk_lock);
663
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -0800664 /* Metadata must only be loaded into one table at once */
Jonathan Brassow493df712009-04-02 19:55:31 +0100665 r = s->store->type->read_metadata(s->store, dm_add_exception,
666 (void *)s);
Milan Broz07641472007-07-12 17:28:13 +0100667 if (r < 0) {
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -0700668 ti->error = "Failed to read snapshot metadata";
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100669 goto bad_load_and_register;
Milan Broz07641472007-07-12 17:28:13 +0100670 } else if (r > 0) {
671 s->valid = 0;
672 DMWARN("Snapshot is marked invalid.");
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -0700673 }
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -0800674
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700675 bio_list_init(&s->queued_bios);
David Howellsc4028952006-11-22 14:57:56 +0000676 INIT_WORK(&s->queued_bios_work, flush_queued_bios);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 /* Add snapshot to the list of snapshots for this origin */
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -0800679 /* Exceptions aren't triggered till snapshot_resume() is called */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (register_snapshot(s)) {
681 r = -EINVAL;
682 ti->error = "Cannot register snapshot origin";
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100683 goto bad_load_and_register;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
685
686 ti->private = s;
Jonathan Brassowd0216842009-04-02 19:55:32 +0100687 ti->split_io = s->store->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 return 0;
690
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100691 bad_load_and_register:
692 mempool_destroy(s->tracked_chunk_pool);
693
Mikulas Patocka92e86812008-07-21 12:00:35 +0100694 bad_tracked_chunk_pool:
695 mempool_destroy(s->pending_pool);
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 bad6:
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100698 dm_kcopyd_client_destroy(s->kcopyd_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 bad5:
Jonathan Brassow493df712009-04-02 19:55:31 +0100701 s->store->type->dtr(s->store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 bad4:
704 exit_exception_table(&s->pending, pending_cache);
705 exit_exception_table(&s->complete, exception_cache);
706
707 bad3:
708 dm_put_device(ti, s->cow);
709 dm_put_device(ti, s->origin);
710
711 bad2:
712 kfree(s);
713
714 bad1:
715 return r;
716}
717
Milan Broz31c93a02006-12-08 02:41:11 -0800718static void __free_exceptions(struct dm_snapshot *s)
719{
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100720 dm_kcopyd_client_destroy(s->kcopyd_client);
Milan Broz31c93a02006-12-08 02:41:11 -0800721 s->kcopyd_client = NULL;
722
723 exit_exception_table(&s->pending, pending_cache);
724 exit_exception_table(&s->complete, exception_cache);
725
Jonathan Brassow493df712009-04-02 19:55:31 +0100726 s->store->type->dtr(s->store);
Milan Broz31c93a02006-12-08 02:41:11 -0800727}
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729static void snapshot_dtr(struct dm_target *ti)
730{
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100731#ifdef CONFIG_DM_DEBUG
732 int i;
733#endif
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100734 struct dm_snapshot *s = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700736 flush_workqueue(ksnapd);
737
Alasdair G Kergon138728dc2006-03-27 01:17:50 -0800738 /* Prevent further origin writes from using this snapshot. */
739 /* After this returns there can be no new kcopyd jobs. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 unregister_snapshot(s);
741
Mikulas Patocka879129d22008-10-30 13:33:16 +0000742 while (atomic_read(&s->pending_exceptions_count))
Mikulas Patocka90fa1522009-01-06 03:04:54 +0000743 msleep(1);
Mikulas Patocka879129d22008-10-30 13:33:16 +0000744 /*
745 * Ensure instructions in mempool_destroy aren't reordered
746 * before atomic_read.
747 */
748 smp_mb();
749
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100750#ifdef CONFIG_DM_DEBUG
751 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
752 BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i]));
753#endif
754
755 mempool_destroy(s->tracked_chunk_pool);
756
Milan Broz31c93a02006-12-08 02:41:11 -0800757 __free_exceptions(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Mikulas Patocka92e86812008-07-21 12:00:35 +0100759 mempool_destroy(s->pending_pool);
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 dm_put_device(ti, s->origin);
762 dm_put_device(ti, s->cow);
Alasdair G Kergon138728dc2006-03-27 01:17:50 -0800763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 kfree(s);
765}
766
767/*
768 * Flush a list of buffers.
769 */
770static void flush_bios(struct bio *bio)
771{
772 struct bio *n;
773
774 while (bio) {
775 n = bio->bi_next;
776 bio->bi_next = NULL;
777 generic_make_request(bio);
778 bio = n;
779 }
780}
781
David Howellsc4028952006-11-22 14:57:56 +0000782static void flush_queued_bios(struct work_struct *work)
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700783{
David Howellsc4028952006-11-22 14:57:56 +0000784 struct dm_snapshot *s =
785 container_of(work, struct dm_snapshot, queued_bios_work);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700786 struct bio *queued_bios;
787 unsigned long flags;
788
789 spin_lock_irqsave(&s->pe_lock, flags);
790 queued_bios = bio_list_get(&s->queued_bios);
791 spin_unlock_irqrestore(&s->pe_lock, flags);
792
793 flush_bios(queued_bios);
794}
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796/*
797 * Error a list of buffers.
798 */
799static void error_bios(struct bio *bio)
800{
801 struct bio *n;
802
803 while (bio) {
804 n = bio->bi_next;
805 bio->bi_next = NULL;
NeilBrown6712ecf2007-09-27 12:47:43 +0200806 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 bio = n;
808 }
809}
810
Alasdair G Kergon695368a2006-10-03 01:15:31 -0700811static void __invalidate_snapshot(struct dm_snapshot *s, int err)
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800812{
813 if (!s->valid)
814 return;
815
816 if (err == -EIO)
817 DMERR("Invalidating snapshot: Error reading/writing.");
818 else if (err == -ENOMEM)
819 DMERR("Invalidating snapshot: Unable to allocate exception.");
820
Jonathan Brassow493df712009-04-02 19:55:31 +0100821 if (s->store->type->drop_snapshot)
822 s->store->type->drop_snapshot(s->store);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800823
824 s->valid = 0;
825
Jonathan Brassow0cea9c72009-04-02 19:55:32 +0100826 dm_table_event(s->store->ti->table);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800827}
828
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100829static void get_pending_exception(struct dm_snap_pending_exception *pe)
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700830{
831 atomic_inc(&pe->ref_count);
832}
833
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100834static struct bio *put_pending_exception(struct dm_snap_pending_exception *pe)
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700835{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100836 struct dm_snap_pending_exception *primary_pe;
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700837 struct bio *origin_bios = NULL;
838
839 primary_pe = pe->primary_pe;
840
841 /*
842 * If this pe is involved in a write to the origin and
843 * it is the last sibling to complete then release
844 * the bios for the original write to the origin.
845 */
846 if (primary_pe &&
Mikulas Patocka7c5f78b2008-10-21 17:44:51 +0100847 atomic_dec_and_test(&primary_pe->ref_count)) {
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700848 origin_bios = bio_list_get(&primary_pe->origin_bios);
Mikulas Patocka7c5f78b2008-10-21 17:44:51 +0100849 free_pending_exception(primary_pe);
850 }
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700851
852 /*
853 * Free the pe if it's not linked to an origin write or if
854 * it's not itself a primary pe.
855 */
856 if (!primary_pe || primary_pe != pe)
857 free_pending_exception(pe);
858
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700859 return origin_bios;
860}
861
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100862static void pending_complete(struct dm_snap_pending_exception *pe, int success)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100864 struct dm_snap_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 struct dm_snapshot *s = pe->snap;
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700866 struct bio *origin_bios = NULL;
867 struct bio *snapshot_bios = NULL;
868 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800870 if (!success) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 /* Read/write error - snapshot is unusable */
872 down_write(&s->lock);
Alasdair G Kergon695368a2006-10-03 01:15:31 -0700873 __invalidate_snapshot(s, -EIO);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700874 error = 1;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800875 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
877
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800878 e = alloc_exception();
879 if (!e) {
880 down_write(&s->lock);
Alasdair G Kergon695368a2006-10-03 01:15:31 -0700881 __invalidate_snapshot(s, -ENOMEM);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700882 error = 1;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800883 goto out;
884 }
885 *e = pe->e;
886
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700887 down_write(&s->lock);
888 if (!s->valid) {
889 free_exception(e);
890 error = 1;
891 goto out;
892 }
893
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800894 /*
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100895 * Check for conflicting reads. This is extremely improbable,
Mikulas Patocka90fa1522009-01-06 03:04:54 +0000896 * so msleep(1) is sufficient and there is no need for a wait queue.
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100897 */
898 while (__chunk_is_tracked(s, pe->e.old_chunk))
Mikulas Patocka90fa1522009-01-06 03:04:54 +0000899 msleep(1);
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100900
901 /*
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800902 * Add a proper exception, and remove the
903 * in-flight exception from the list.
904 */
Milan Brozd74f81f2008-02-08 02:11:27 +0000905 insert_completed_exception(s, e);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 out:
Alasdair G Kergon695368a2006-10-03 01:15:31 -0700908 remove_exception(&pe->e);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700909 snapshot_bios = bio_list_get(&pe->snapshot_bios);
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700910 origin_bios = put_pending_exception(pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700912 up_write(&s->lock);
913
914 /* Submit any pending write bios */
915 if (error)
916 error_bios(snapshot_bios);
917 else
918 flush_bios(snapshot_bios);
919
920 flush_bios(origin_bios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922
923static void commit_callback(void *context, int success)
924{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100925 struct dm_snap_pending_exception *pe = context;
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 pending_complete(pe, success);
928}
929
930/*
931 * Called when the copy I/O has finished. kcopyd actually runs
932 * this code so don't block.
933 */
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700934static void copy_callback(int read_err, unsigned long write_err, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100936 struct dm_snap_pending_exception *pe = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 struct dm_snapshot *s = pe->snap;
938
939 if (read_err || write_err)
940 pending_complete(pe, 0);
941
942 else
943 /* Update the metadata if we are persistent */
Jonathan Brassow493df712009-04-02 19:55:31 +0100944 s->store->type->commit_exception(s->store, &pe->e,
945 commit_callback, pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946}
947
948/*
949 * Dispatches the copy operation to kcopyd.
950 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100951static void start_copy(struct dm_snap_pending_exception *pe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
953 struct dm_snapshot *s = pe->snap;
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100954 struct dm_io_region src, dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 struct block_device *bdev = s->origin->bdev;
956 sector_t dev_size;
957
958 dev_size = get_dev_size(bdev);
959
960 src.bdev = bdev;
961 src.sector = chunk_to_sector(s, pe->e.old_chunk);
Jonathan Brassowd0216842009-04-02 19:55:32 +0100962 src.count = min(s->store->chunk_size, dev_size - src.sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 dest.bdev = s->cow->bdev;
965 dest.sector = chunk_to_sector(s, pe->e.new_chunk);
966 dest.count = src.count;
967
968 /* Hand over to kcopyd */
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100969 dm_kcopyd_copy(s->kcopyd_client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 &src, 1, &dest, 0, copy_callback, pe);
971}
972
Mikulas Patocka29138082009-04-02 19:55:25 +0100973static struct dm_snap_pending_exception *
974__lookup_pending_exception(struct dm_snapshot *s, chunk_t chunk)
975{
976 struct dm_snap_exception *e = lookup_exception(&s->pending, chunk);
977
978 if (!e)
979 return NULL;
980
981 return container_of(e, struct dm_snap_pending_exception, e);
982}
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984/*
985 * Looks to see if this snapshot already has a pending exception
986 * for this chunk, otherwise it allocates a new one and inserts
987 * it into the pending table.
988 *
989 * NOTE: a write lock must be held on snap->lock before calling
990 * this.
991 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100992static struct dm_snap_pending_exception *
Mikulas Patockac6621392009-04-02 19:55:25 +0100993__find_pending_exception(struct dm_snapshot *s,
994 struct dm_snap_pending_exception *pe, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
Mikulas Patockac6621392009-04-02 19:55:25 +0100996 struct dm_snap_pending_exception *pe2;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800997
Mikulas Patocka29138082009-04-02 19:55:25 +0100998 pe2 = __lookup_pending_exception(s, chunk);
999 if (pe2) {
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001000 free_pending_exception(pe);
Mikulas Patocka29138082009-04-02 19:55:25 +01001001 return pe2;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001002 }
1003
1004 pe->e.old_chunk = chunk;
1005 bio_list_init(&pe->origin_bios);
1006 bio_list_init(&pe->snapshot_bios);
1007 pe->primary_pe = NULL;
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001008 atomic_set(&pe->ref_count, 0);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001009 pe->started = 0;
1010
Jonathan Brassow493df712009-04-02 19:55:31 +01001011 if (s->store->type->prepare_exception(s->store, &pe->e)) {
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001012 free_pending_exception(pe);
1013 return NULL;
1014 }
1015
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001016 get_pending_exception(pe);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001017 insert_exception(&s->pending, &pe->e);
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return pe;
1020}
1021
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001022static void remap_exception(struct dm_snapshot *s, struct dm_snap_exception *e,
Milan Brozd74f81f2008-02-08 02:11:27 +00001023 struct bio *bio, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
1025 bio->bi_bdev = s->cow->bdev;
Milan Brozd74f81f2008-02-08 02:11:27 +00001026 bio->bi_sector = chunk_to_sector(s, dm_chunk_number(e->new_chunk) +
1027 (chunk - e->old_chunk)) +
Jonathan Brassowd0216842009-04-02 19:55:32 +01001028 (bio->bi_sector & s->store->chunk_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
1030
1031static int snapshot_map(struct dm_target *ti, struct bio *bio,
1032 union map_info *map_context)
1033{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001034 struct dm_snap_exception *e;
1035 struct dm_snapshot *s = ti->private;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001036 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 chunk_t chunk;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001038 struct dm_snap_pending_exception *pe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 chunk = sector_to_chunk(s, bio->bi_sector);
1041
1042 /* Full snapshots are not usable */
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001043 /* To get here the table must be live so s->active is always set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 if (!s->valid)
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -07001045 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001047 /* FIXME: should only take write lock if we need
1048 * to copy an exception */
1049 down_write(&s->lock);
1050
1051 if (!s->valid) {
1052 r = -EIO;
1053 goto out_unlock;
1054 }
1055
1056 /* If the block is already remapped - use that, else remap it */
1057 e = lookup_exception(&s->complete, chunk);
1058 if (e) {
Milan Brozd74f81f2008-02-08 02:11:27 +00001059 remap_exception(s, e, bio, chunk);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001060 goto out_unlock;
1061 }
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 /*
1064 * Write to snapshot - higher level takes care of RW/RO
1065 * flags so we should only get this if we are
1066 * writeable.
1067 */
1068 if (bio_rw(bio) == WRITE) {
Mikulas Patocka29138082009-04-02 19:55:25 +01001069 pe = __lookup_pending_exception(s, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001070 if (!pe) {
Mikulas Patockac6621392009-04-02 19:55:25 +01001071 up_write(&s->lock);
1072 pe = alloc_pending_exception(s);
1073 down_write(&s->lock);
1074
1075 if (!s->valid) {
1076 free_pending_exception(pe);
1077 r = -EIO;
1078 goto out_unlock;
1079 }
1080
Mikulas Patocka35bf6592009-04-02 19:55:26 +01001081 e = lookup_exception(&s->complete, chunk);
1082 if (e) {
1083 free_pending_exception(pe);
1084 remap_exception(s, e, bio, chunk);
1085 goto out_unlock;
1086 }
1087
Mikulas Patockac6621392009-04-02 19:55:25 +01001088 pe = __find_pending_exception(s, pe, chunk);
Mikulas Patocka29138082009-04-02 19:55:25 +01001089 if (!pe) {
1090 __invalidate_snapshot(s, -ENOMEM);
1091 r = -EIO;
1092 goto out_unlock;
1093 }
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001094 }
1095
Milan Brozd74f81f2008-02-08 02:11:27 +00001096 remap_exception(s, &pe->e, bio, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001097 bio_list_add(&pe->snapshot_bios, bio);
1098
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001099 r = DM_MAPIO_SUBMITTED;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001100
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001101 if (!pe->started) {
1102 /* this is protected by snap->lock */
1103 pe->started = 1;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001104 up_write(&s->lock);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001105 start_copy(pe);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001106 goto out;
1107 }
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001108 } else {
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001109 bio->bi_bdev = s->origin->bdev;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001110 map_context->ptr = track_chunk(s, chunk);
1111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001113 out_unlock:
1114 up_write(&s->lock);
1115 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 return r;
1117}
1118
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001119static int snapshot_end_io(struct dm_target *ti, struct bio *bio,
1120 int error, union map_info *map_context)
1121{
1122 struct dm_snapshot *s = ti->private;
1123 struct dm_snap_tracked_chunk *c = map_context->ptr;
1124
1125 if (c)
1126 stop_tracking_chunk(s, c);
1127
1128 return 0;
1129}
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131static void snapshot_resume(struct dm_target *ti)
1132{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001133 struct dm_snapshot *s = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001135 down_write(&s->lock);
1136 s->active = 1;
1137 up_write(&s->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138}
1139
1140static int snapshot_status(struct dm_target *ti, status_type_t type,
1141 char *result, unsigned int maxlen)
1142{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001143 struct dm_snapshot *snap = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
1145 switch (type) {
1146 case STATUSTYPE_INFO:
1147 if (!snap->valid)
1148 snprintf(result, maxlen, "Invalid");
1149 else {
Jonathan Brassow493df712009-04-02 19:55:31 +01001150 if (snap->store->type->fraction_full) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 sector_t numerator, denominator;
Jonathan Brassow493df712009-04-02 19:55:31 +01001152 snap->store->type->fraction_full(snap->store,
1153 &numerator,
1154 &denominator);
Andrew Morton4ee218c2006-03-27 01:17:48 -08001155 snprintf(result, maxlen, "%llu/%llu",
1156 (unsigned long long)numerator,
1157 (unsigned long long)denominator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
1159 else
1160 snprintf(result, maxlen, "Unknown");
1161 }
1162 break;
1163
1164 case STATUSTYPE_TABLE:
1165 /*
1166 * kdevname returns a static pointer so we need
1167 * to make private copies if the output is to
1168 * make sense.
1169 */
Jonathan Brassow493df712009-04-02 19:55:31 +01001170 snprintf(result, maxlen, "%s %s %s %llu",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 snap->origin->name, snap->cow->name,
Jonathan Brassow493df712009-04-02 19:55:31 +01001172 snap->store->type->name,
Jonathan Brassowd0216842009-04-02 19:55:32 +01001173 (unsigned long long)snap->store->chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 break;
1175 }
1176
1177 return 0;
1178}
1179
1180/*-----------------------------------------------------------------
1181 * Origin methods
1182 *---------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183static int __origin_write(struct list_head *snapshots, struct bio *bio)
1184{
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001185 int r = DM_MAPIO_REMAPPED, first = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 struct dm_snapshot *snap;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001187 struct dm_snap_exception *e;
1188 struct dm_snap_pending_exception *pe, *next_pe, *primary_pe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 chunk_t chunk;
Alasdair G Kergoneccf0812006-03-27 01:17:42 -08001190 LIST_HEAD(pe_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 /* Do all the snapshots on this origin */
1193 list_for_each_entry (snap, snapshots, list) {
1194
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001195 down_write(&snap->lock);
1196
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001197 /* Only deal with valid and active snapshots */
1198 if (!snap->valid || !snap->active)
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001199 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Alasdair G Kergond5e404c2005-07-12 15:53:05 -07001201 /* Nothing to do if writing beyond end of snapshot */
Jonathan Brassow0cea9c72009-04-02 19:55:32 +01001202 if (bio->bi_sector >= dm_table_get_size(snap->store->ti->table))
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001203 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 /*
1206 * Remember, different snapshots can have
1207 * different chunk sizes.
1208 */
1209 chunk = sector_to_chunk(snap, bio->bi_sector);
1210
1211 /*
1212 * Check exception table to see if block
1213 * is already remapped in this snapshot
1214 * and trigger an exception if not.
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001215 *
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001216 * ref_count is initialised to 1 so pending_complete()
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001217 * won't destroy the primary_pe while we're inside this loop.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 */
1219 e = lookup_exception(&snap->complete, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001220 if (e)
1221 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Mikulas Patocka29138082009-04-02 19:55:25 +01001223 pe = __lookup_pending_exception(snap, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001224 if (!pe) {
Mikulas Patockac6621392009-04-02 19:55:25 +01001225 up_write(&snap->lock);
1226 pe = alloc_pending_exception(snap);
1227 down_write(&snap->lock);
1228
1229 if (!snap->valid) {
1230 free_pending_exception(pe);
1231 goto next_snapshot;
1232 }
1233
Mikulas Patocka35bf6592009-04-02 19:55:26 +01001234 e = lookup_exception(&snap->complete, chunk);
1235 if (e) {
1236 free_pending_exception(pe);
1237 goto next_snapshot;
1238 }
1239
Mikulas Patockac6621392009-04-02 19:55:25 +01001240 pe = __find_pending_exception(snap, pe, chunk);
Mikulas Patocka29138082009-04-02 19:55:25 +01001241 if (!pe) {
1242 __invalidate_snapshot(snap, -ENOMEM);
1243 goto next_snapshot;
1244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
1246
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001247 if (!primary_pe) {
1248 /*
1249 * Either every pe here has same
1250 * primary_pe or none has one yet.
1251 */
1252 if (pe->primary_pe)
1253 primary_pe = pe->primary_pe;
1254 else {
1255 primary_pe = pe;
1256 first = 1;
1257 }
1258
1259 bio_list_add(&primary_pe->origin_bios, bio);
1260
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001261 r = DM_MAPIO_SUBMITTED;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001262 }
1263
1264 if (!pe->primary_pe) {
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001265 pe->primary_pe = primary_pe;
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001266 get_pending_exception(primary_pe);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001267 }
1268
1269 if (!pe->started) {
1270 pe->started = 1;
1271 list_add_tail(&pe->list, &pe_queue);
1272 }
1273
1274 next_snapshot:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 up_write(&snap->lock);
1276 }
1277
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001278 if (!primary_pe)
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001279 return r;
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001280
1281 /*
1282 * If this is the first time we're processing this chunk and
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001283 * ref_count is now 1 it means all the pending exceptions
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001284 * got completed while we were in the loop above, so it falls to
1285 * us here to remove the primary_pe and submit any origin_bios.
1286 */
1287
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001288 if (first && atomic_dec_and_test(&primary_pe->ref_count)) {
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001289 flush_bios(bio_list_get(&primary_pe->origin_bios));
1290 free_pending_exception(primary_pe);
1291 /* If we got here, pe_queue is necessarily empty. */
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001292 return r;
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001293 }
1294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 /*
1296 * Now that we have a complete pe list we can start the copying.
1297 */
Alasdair G Kergoneccf0812006-03-27 01:17:42 -08001298 list_for_each_entry_safe(pe, next_pe, &pe_queue, list)
1299 start_copy(pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 return r;
1302}
1303
1304/*
1305 * Called on a write from the origin driver.
1306 */
1307static int do_origin(struct dm_dev *origin, struct bio *bio)
1308{
1309 struct origin *o;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001310 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312 down_read(&_origins_lock);
1313 o = __lookup_origin(origin->bdev);
1314 if (o)
1315 r = __origin_write(&o->snapshots, bio);
1316 up_read(&_origins_lock);
1317
1318 return r;
1319}
1320
1321/*
1322 * Origin: maps a linear range of a device, with hooks for snapshotting.
1323 */
1324
1325/*
1326 * Construct an origin mapping: <dev_path>
1327 * The context for an origin is merely a 'struct dm_dev *'
1328 * pointing to the real device.
1329 */
1330static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1331{
1332 int r;
1333 struct dm_dev *dev;
1334
1335 if (argc != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001336 ti->error = "origin: incorrect number of arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 return -EINVAL;
1338 }
1339
1340 r = dm_get_device(ti, argv[0], 0, ti->len,
1341 dm_table_get_mode(ti->table), &dev);
1342 if (r) {
1343 ti->error = "Cannot get target device";
1344 return r;
1345 }
1346
1347 ti->private = dev;
1348 return 0;
1349}
1350
1351static void origin_dtr(struct dm_target *ti)
1352{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001353 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 dm_put_device(ti, dev);
1355}
1356
1357static int origin_map(struct dm_target *ti, struct bio *bio,
1358 union map_info *map_context)
1359{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001360 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 bio->bi_bdev = dev->bdev;
1362
1363 /* Only tell snapshots if this is a write */
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001364 return (bio_rw(bio) == WRITE) ? do_origin(dev, bio) : DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365}
1366
1367#define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
1368
1369/*
1370 * Set the target "split_io" field to the minimum of all the snapshots'
1371 * chunk sizes.
1372 */
1373static void origin_resume(struct dm_target *ti)
1374{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001375 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 struct dm_snapshot *snap;
1377 struct origin *o;
1378 chunk_t chunk_size = 0;
1379
1380 down_read(&_origins_lock);
1381 o = __lookup_origin(dev->bdev);
1382 if (o)
1383 list_for_each_entry (snap, &o->snapshots, list)
Jonathan Brassowd0216842009-04-02 19:55:32 +01001384 chunk_size = min_not_zero(chunk_size,
1385 snap->store->chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 up_read(&_origins_lock);
1387
1388 ti->split_io = chunk_size;
1389}
1390
1391static int origin_status(struct dm_target *ti, status_type_t type, char *result,
1392 unsigned int maxlen)
1393{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001394 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 switch (type) {
1397 case STATUSTYPE_INFO:
1398 result[0] = '\0';
1399 break;
1400
1401 case STATUSTYPE_TABLE:
1402 snprintf(result, maxlen, "%s", dev->name);
1403 break;
1404 }
1405
1406 return 0;
1407}
1408
1409static struct target_type origin_target = {
1410 .name = "snapshot-origin",
Milan Brozd74f81f2008-02-08 02:11:27 +00001411 .version = {1, 6, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 .module = THIS_MODULE,
1413 .ctr = origin_ctr,
1414 .dtr = origin_dtr,
1415 .map = origin_map,
1416 .resume = origin_resume,
1417 .status = origin_status,
1418};
1419
1420static struct target_type snapshot_target = {
1421 .name = "snapshot",
Milan Brozd74f81f2008-02-08 02:11:27 +00001422 .version = {1, 6, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 .module = THIS_MODULE,
1424 .ctr = snapshot_ctr,
1425 .dtr = snapshot_dtr,
1426 .map = snapshot_map,
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001427 .end_io = snapshot_end_io,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 .resume = snapshot_resume,
1429 .status = snapshot_status,
1430};
1431
1432static int __init dm_snapshot_init(void)
1433{
1434 int r;
1435
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00001436 r = dm_exception_store_init();
1437 if (r) {
1438 DMERR("Failed to initialize exception stores");
1439 return r;
1440 }
1441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 r = dm_register_target(&snapshot_target);
1443 if (r) {
1444 DMERR("snapshot target register failed %d", r);
1445 return r;
1446 }
1447
1448 r = dm_register_target(&origin_target);
1449 if (r < 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001450 DMERR("Origin target register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 goto bad1;
1452 }
1453
1454 r = init_origin_hash();
1455 if (r) {
1456 DMERR("init_origin_hash failed.");
1457 goto bad2;
1458 }
1459
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001460 exception_cache = KMEM_CACHE(dm_snap_exception, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 if (!exception_cache) {
1462 DMERR("Couldn't create exception cache.");
1463 r = -ENOMEM;
1464 goto bad3;
1465 }
1466
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001467 pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 if (!pending_cache) {
1469 DMERR("Couldn't create pending cache.");
1470 r = -ENOMEM;
1471 goto bad4;
1472 }
1473
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001474 tracked_chunk_cache = KMEM_CACHE(dm_snap_tracked_chunk, 0);
1475 if (!tracked_chunk_cache) {
1476 DMERR("Couldn't create cache to track chunks in use.");
1477 r = -ENOMEM;
1478 goto bad5;
1479 }
1480
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001481 ksnapd = create_singlethread_workqueue("ksnapd");
1482 if (!ksnapd) {
1483 DMERR("Failed to create ksnapd workqueue.");
1484 r = -ENOMEM;
Mikulas Patocka92e86812008-07-21 12:00:35 +01001485 goto bad_pending_pool;
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001486 }
1487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 return 0;
1489
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00001490bad_pending_pool:
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001491 kmem_cache_destroy(tracked_chunk_cache);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00001492bad5:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 kmem_cache_destroy(pending_cache);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00001494bad4:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 kmem_cache_destroy(exception_cache);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00001496bad3:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 exit_origin_hash();
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00001498bad2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 dm_unregister_target(&origin_target);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00001500bad1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 dm_unregister_target(&snapshot_target);
1502 return r;
1503}
1504
1505static void __exit dm_snapshot_exit(void)
1506{
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001507 destroy_workqueue(ksnapd);
1508
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00001509 dm_unregister_target(&snapshot_target);
1510 dm_unregister_target(&origin_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512 exit_origin_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 kmem_cache_destroy(pending_cache);
1514 kmem_cache_destroy(exception_cache);
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001515 kmem_cache_destroy(tracked_chunk_cache);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00001516
1517 dm_exception_store_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518}
1519
1520/* Module hooks */
1521module_init(dm_snapshot_init);
1522module_exit(dm_snapshot_exit);
1523
1524MODULE_DESCRIPTION(DM_NAME " snapshot target");
1525MODULE_AUTHOR("Joe Thornber");
1526MODULE_LICENSE("GPL");