blob: 981a0413068f8028c90e7da039cf5d48fcc27b9b [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/device-mapper.h>
Mikulas Patocka90fa1522009-01-06 03:04:54 +000011#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#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>
vignesh babu6f3c3f02007-10-19 22:38:44 +010020#include <linux/log2.h>
Alasdair G Kergona765e202008-04-24 22:02:01 +010021#include <linux/dm-kcopyd.h>
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010022#include <linux/workqueue.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-bio-list.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Alasdair G Kergon72d94862006-06-26 00:27:35 -070027#define DM_MSG_PREFIX "snapshots"
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/*
30 * The percentage increment we will wake up users at
31 */
32#define WAKE_UP_PERCENT 5
33
34/*
35 * kcopyd priority of snapshot operations
36 */
37#define SNAPSHOT_COPY_PRIORITY 2
38
39/*
Milan Broz8ee27672008-04-24 21:42:36 +010040 * Reserve 1MB for each snapshot initially (with minimum of 1 page).
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 */
Milan Broz8ee27672008-04-24 21:42:36 +010042#define SNAPSHOT_PAGES (((1UL << 20) >> PAGE_SHIFT) ? : 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Mikulas Patockacd45daf2008-07-21 12:00:32 +010044/*
45 * The size of the mempool used to track chunks in use.
46 */
47#define MIN_IOS 256
48
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010049#define DM_TRACKED_CHUNK_HASH_SIZE 16
50#define DM_TRACKED_CHUNK_HASH(x) ((unsigned long)(x) & \
51 (DM_TRACKED_CHUNK_HASH_SIZE - 1))
52
53struct exception_table {
54 uint32_t hash_mask;
55 unsigned hash_shift;
56 struct list_head *table;
57};
58
59struct dm_snapshot {
60 struct rw_semaphore lock;
61
62 struct dm_dev *origin;
63
64 /* List of snapshots per Origin */
65 struct list_head list;
66
67 /* You can't use a snapshot if this is 0 (e.g. if full) */
68 int valid;
69
70 /* Origin writes don't trigger exceptions until this is set */
71 int active;
72
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010073 mempool_t *pending_pool;
74
75 atomic_t pending_exceptions_count;
76
77 struct exception_table pending;
78 struct exception_table complete;
79
80 /*
81 * pe_lock protects all pending_exception operations and access
82 * as well as the snapshot_bios list.
83 */
84 spinlock_t pe_lock;
85
86 /* The on disk metadata handler */
87 struct dm_exception_store *store;
88
89 struct dm_kcopyd_client *kcopyd_client;
90
91 /* Queue of snapshot writes for ksnapd to flush */
92 struct bio_list queued_bios;
93 struct work_struct queued_bios_work;
94
95 /* Chunks with outstanding reads */
96 mempool_t *tracked_chunk_pool;
97 spinlock_t tracked_chunk_lock;
98 struct hlist_head tracked_chunk_hash[DM_TRACKED_CHUNK_HASH_SIZE];
99};
100
Adrian Bunkc642f9e2006-12-08 02:41:13 -0800101static struct workqueue_struct *ksnapd;
David Howellsc4028952006-11-22 14:57:56 +0000102static void flush_queued_bios(struct work_struct *work);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700103
Jonathan Brassowccc45ea2009-04-02 19:55:34 +0100104static sector_t chunk_to_sector(struct dm_exception_store *store,
105 chunk_t chunk)
106{
107 return chunk << store->chunk_shift;
108}
109
110static int bdev_equal(struct block_device *lhs, struct block_device *rhs)
111{
112 /*
113 * There is only ever one instance of a particular block
114 * device so we can compare pointers safely.
115 */
116 return lhs == rhs;
117}
118
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100119struct dm_snap_pending_exception {
120 struct dm_snap_exception e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 /*
123 * Origin buffers waiting for this to complete are held
124 * in a bio list
125 */
126 struct bio_list origin_bios;
127 struct bio_list snapshot_bios;
128
129 /*
Alasdair G Kergoneccf0812006-03-27 01:17:42 -0800130 * Short-term queue of pending exceptions prior to submission.
131 */
132 struct list_head list;
133
134 /*
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -0800135 * The primary pending_exception is the one that holds
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700136 * the ref_count and the list of origin_bios for a
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -0800137 * group of pending_exceptions. It is always last to get freed.
138 * These fields get set up when writing to the origin.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100140 struct dm_snap_pending_exception *primary_pe;
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -0800141
142 /*
143 * Number of pending_exceptions processing this chunk.
144 * When this drops to zero we must complete the origin bios.
145 * If incrementing or decrementing this, hold pe->snap->lock for
146 * the sibling concerned and not pe->primary_pe->snap->lock unless
147 * they are the same.
148 */
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700149 atomic_t ref_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 /* Pointer back to snapshot context */
152 struct dm_snapshot *snap;
153
154 /*
155 * 1 indicates the exception has already been sent to
156 * kcopyd.
157 */
158 int started;
159};
160
161/*
162 * Hash table mapping origin volumes to lists of snapshots and
163 * a lock to protect it
164 */
Christoph Lametere18b8902006-12-06 20:33:20 -0800165static struct kmem_cache *exception_cache;
166static struct kmem_cache *pending_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100168struct dm_snap_tracked_chunk {
169 struct hlist_node node;
170 chunk_t chunk;
171};
172
173static struct kmem_cache *tracked_chunk_cache;
174
175static struct dm_snap_tracked_chunk *track_chunk(struct dm_snapshot *s,
176 chunk_t chunk)
177{
178 struct dm_snap_tracked_chunk *c = mempool_alloc(s->tracked_chunk_pool,
179 GFP_NOIO);
180 unsigned long flags;
181
182 c->chunk = chunk;
183
184 spin_lock_irqsave(&s->tracked_chunk_lock, flags);
185 hlist_add_head(&c->node,
186 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)]);
187 spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
188
189 return c;
190}
191
192static void stop_tracking_chunk(struct dm_snapshot *s,
193 struct dm_snap_tracked_chunk *c)
194{
195 unsigned long flags;
196
197 spin_lock_irqsave(&s->tracked_chunk_lock, flags);
198 hlist_del(&c->node);
199 spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
200
201 mempool_free(c, s->tracked_chunk_pool);
202}
203
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100204static int __chunk_is_tracked(struct dm_snapshot *s, chunk_t chunk)
205{
206 struct dm_snap_tracked_chunk *c;
207 struct hlist_node *hn;
208 int found = 0;
209
210 spin_lock_irq(&s->tracked_chunk_lock);
211
212 hlist_for_each_entry(c, hn,
213 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)], node) {
214 if (c->chunk == chunk) {
215 found = 1;
216 break;
217 }
218 }
219
220 spin_unlock_irq(&s->tracked_chunk_lock);
221
222 return found;
223}
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225/*
226 * One of these per registered origin, held in the snapshot_origins hash
227 */
228struct origin {
229 /* The origin device */
230 struct block_device *bdev;
231
232 struct list_head hash_list;
233
234 /* List of snapshots for this origin */
235 struct list_head snapshots;
236};
237
238/*
239 * Size of the hash table for origin volumes. If we make this
240 * the size of the minors list then it should be nearly perfect
241 */
242#define ORIGIN_HASH_SIZE 256
243#define ORIGIN_MASK 0xFF
244static struct list_head *_origins;
245static struct rw_semaphore _origins_lock;
246
247static int init_origin_hash(void)
248{
249 int i;
250
251 _origins = kmalloc(ORIGIN_HASH_SIZE * sizeof(struct list_head),
252 GFP_KERNEL);
253 if (!_origins) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700254 DMERR("unable to allocate memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return -ENOMEM;
256 }
257
258 for (i = 0; i < ORIGIN_HASH_SIZE; i++)
259 INIT_LIST_HEAD(_origins + i);
260 init_rwsem(&_origins_lock);
261
262 return 0;
263}
264
265static void exit_origin_hash(void)
266{
267 kfree(_origins);
268}
269
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100270static unsigned origin_hash(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
272 return bdev->bd_dev & ORIGIN_MASK;
273}
274
275static struct origin *__lookup_origin(struct block_device *origin)
276{
277 struct list_head *ol;
278 struct origin *o;
279
280 ol = &_origins[origin_hash(origin)];
281 list_for_each_entry (o, ol, hash_list)
282 if (bdev_equal(o->bdev, origin))
283 return o;
284
285 return NULL;
286}
287
288static void __insert_origin(struct origin *o)
289{
290 struct list_head *sl = &_origins[origin_hash(o->bdev)];
291 list_add_tail(&o->hash_list, sl);
292}
293
294/*
295 * Make a note of the snapshot and its origin so we can look it
296 * up when the origin has a write on it.
297 */
298static int register_snapshot(struct dm_snapshot *snap)
299{
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000300 struct origin *o, *new_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 struct block_device *bdev = snap->origin->bdev;
302
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000303 new_o = kmalloc(sizeof(*new_o), GFP_KERNEL);
304 if (!new_o)
305 return -ENOMEM;
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 down_write(&_origins_lock);
308 o = __lookup_origin(bdev);
309
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000310 if (o)
311 kfree(new_o);
312 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 /* New origin */
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000314 o = new_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 /* Initialise the struct */
317 INIT_LIST_HEAD(&o->snapshots);
318 o->bdev = bdev;
319
320 __insert_origin(o);
321 }
322
323 list_add_tail(&snap->list, &o->snapshots);
324
325 up_write(&_origins_lock);
326 return 0;
327}
328
329static void unregister_snapshot(struct dm_snapshot *s)
330{
331 struct origin *o;
332
333 down_write(&_origins_lock);
334 o = __lookup_origin(s->origin->bdev);
335
336 list_del(&s->list);
337 if (list_empty(&o->snapshots)) {
338 list_del(&o->hash_list);
339 kfree(o);
340 }
341
342 up_write(&_origins_lock);
343}
344
345/*
346 * Implementation of the exception hash tables.
Milan Brozd74f81f2008-02-08 02:11:27 +0000347 * The lowest hash_shift bits of the chunk number are ignored, allowing
348 * some consecutive chunks to be grouped together.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 */
Milan Brozd74f81f2008-02-08 02:11:27 +0000350static int init_exception_table(struct exception_table *et, uint32_t size,
351 unsigned hash_shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 unsigned int i;
354
Milan Brozd74f81f2008-02-08 02:11:27 +0000355 et->hash_shift = hash_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 et->hash_mask = size - 1;
357 et->table = dm_vcalloc(size, sizeof(struct list_head));
358 if (!et->table)
359 return -ENOMEM;
360
361 for (i = 0; i < size; i++)
362 INIT_LIST_HEAD(et->table + i);
363
364 return 0;
365}
366
Christoph Lametere18b8902006-12-06 20:33:20 -0800367static void exit_exception_table(struct exception_table *et, struct kmem_cache *mem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 struct list_head *slot;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100370 struct dm_snap_exception *ex, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 int i, size;
372
373 size = et->hash_mask + 1;
374 for (i = 0; i < size; i++) {
375 slot = et->table + i;
376
377 list_for_each_entry_safe (ex, next, slot, hash_list)
378 kmem_cache_free(mem, ex);
379 }
380
381 vfree(et->table);
382}
383
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100384static uint32_t exception_hash(struct exception_table *et, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Milan Brozd74f81f2008-02-08 02:11:27 +0000386 return (chunk >> et->hash_shift) & et->hash_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100389static void insert_exception(struct exception_table *eh,
390 struct dm_snap_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392 struct list_head *l = &eh->table[exception_hash(eh, e->old_chunk)];
393 list_add(&e->hash_list, l);
394}
395
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100396static void remove_exception(struct dm_snap_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 list_del(&e->hash_list);
399}
400
401/*
402 * Return the exception data for a sector, or NULL if not
403 * remapped.
404 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100405static struct dm_snap_exception *lookup_exception(struct exception_table *et,
406 chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
408 struct list_head *slot;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100409 struct dm_snap_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 slot = &et->table[exception_hash(et, chunk)];
412 list_for_each_entry (e, slot, hash_list)
Milan Brozd74f81f2008-02-08 02:11:27 +0000413 if (chunk >= e->old_chunk &&
414 chunk <= e->old_chunk + dm_consecutive_chunk_count(e))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return e;
416
417 return NULL;
418}
419
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100420static struct dm_snap_exception *alloc_exception(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100422 struct dm_snap_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 e = kmem_cache_alloc(exception_cache, GFP_NOIO);
425 if (!e)
426 e = kmem_cache_alloc(exception_cache, GFP_ATOMIC);
427
428 return e;
429}
430
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100431static void free_exception(struct dm_snap_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
433 kmem_cache_free(exception_cache, e);
434}
435
Mikulas Patocka92e86812008-07-21 12:00:35 +0100436static struct dm_snap_pending_exception *alloc_pending_exception(struct dm_snapshot *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Mikulas Patocka92e86812008-07-21 12:00:35 +0100438 struct dm_snap_pending_exception *pe = mempool_alloc(s->pending_pool,
439 GFP_NOIO);
440
Mikulas Patocka879129d22008-10-30 13:33:16 +0000441 atomic_inc(&s->pending_exceptions_count);
Mikulas Patocka92e86812008-07-21 12:00:35 +0100442 pe->snap = s;
443
444 return pe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100447static void free_pending_exception(struct dm_snap_pending_exception *pe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
Mikulas Patocka879129d22008-10-30 13:33:16 +0000449 struct dm_snapshot *s = pe->snap;
450
451 mempool_free(pe, s->pending_pool);
452 smp_mb__before_atomic_dec();
453 atomic_dec(&s->pending_exceptions_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
Milan Brozd74f81f2008-02-08 02:11:27 +0000456static void insert_completed_exception(struct dm_snapshot *s,
457 struct dm_snap_exception *new_e)
458{
459 struct exception_table *eh = &s->complete;
460 struct list_head *l;
461 struct dm_snap_exception *e = NULL;
462
463 l = &eh->table[exception_hash(eh, new_e->old_chunk)];
464
465 /* Add immediately if this table doesn't support consecutive chunks */
466 if (!eh->hash_shift)
467 goto out;
468
469 /* List is ordered by old_chunk */
470 list_for_each_entry_reverse(e, l, hash_list) {
471 /* Insert after an existing chunk? */
472 if (new_e->old_chunk == (e->old_chunk +
473 dm_consecutive_chunk_count(e) + 1) &&
474 new_e->new_chunk == (dm_chunk_number(e->new_chunk) +
475 dm_consecutive_chunk_count(e) + 1)) {
476 dm_consecutive_chunk_count_inc(e);
477 free_exception(new_e);
478 return;
479 }
480
481 /* Insert before an existing chunk? */
482 if (new_e->old_chunk == (e->old_chunk - 1) &&
483 new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) {
484 dm_consecutive_chunk_count_inc(e);
485 e->old_chunk--;
486 e->new_chunk--;
487 free_exception(new_e);
488 return;
489 }
490
491 if (new_e->old_chunk > e->old_chunk)
492 break;
493 }
494
495out:
496 list_add(&new_e->hash_list, e ? &e->hash_list : l);
497}
498
Jonathan Brassowa159c1a2009-01-06 03:05:19 +0000499/*
500 * Callback used by the exception stores to load exceptions when
501 * initialising.
502 */
503static int dm_add_exception(void *context, chunk_t old, chunk_t new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Jonathan Brassowa159c1a2009-01-06 03:05:19 +0000505 struct dm_snapshot *s = context;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100506 struct dm_snap_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 e = alloc_exception();
509 if (!e)
510 return -ENOMEM;
511
512 e->old_chunk = old;
Milan Brozd74f81f2008-02-08 02:11:27 +0000513
514 /* Consecutive_count is implicitly initialised to zero */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 e->new_chunk = new;
Milan Brozd74f81f2008-02-08 02:11:27 +0000516
517 insert_completed_exception(s, e);
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return 0;
520}
521
522/*
523 * Hard coded magic.
524 */
525static int calc_max_buckets(void)
526{
527 /* use a fixed size of 2MB */
528 unsigned long mem = 2 * 1024 * 1024;
529 mem /= sizeof(struct list_head);
530
531 return mem;
532}
533
534/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 * Allocate room for a suitable hash table.
536 */
Jonathan Brassowfee19982009-04-02 19:55:34 +0100537static int init_hash_tables(struct dm_snapshot *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 sector_t hash_size, cow_dev_size, origin_dev_size, max_buckets;
540
541 /*
542 * Calculate based on the size of the original volume or
543 * the COW volume...
544 */
Jonathan Brassowfee19982009-04-02 19:55:34 +0100545 cow_dev_size = get_dev_size(s->store->cow->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 origin_dev_size = get_dev_size(s->origin->bdev);
547 max_buckets = calc_max_buckets();
548
Jonathan Brassowfee19982009-04-02 19:55:34 +0100549 hash_size = min(origin_dev_size, cow_dev_size) >> s->store->chunk_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 hash_size = min(hash_size, max_buckets);
551
Robert P. J. Day8defd832008-02-08 02:10:06 +0000552 hash_size = rounddown_pow_of_two(hash_size);
Milan Brozd74f81f2008-02-08 02:11:27 +0000553 if (init_exception_table(&s->complete, hash_size,
554 DM_CHUNK_CONSECUTIVE_BITS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return -ENOMEM;
556
557 /*
558 * Allocate hash table for in-flight exceptions
559 * Make this smaller than the real hash table
560 */
561 hash_size >>= 3;
562 if (hash_size < 64)
563 hash_size = 64;
564
Milan Brozd74f81f2008-02-08 02:11:27 +0000565 if (init_exception_table(&s->pending, hash_size, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 exit_exception_table(&s->complete, exception_cache);
567 return -ENOMEM;
568 }
569
570 return 0;
571}
572
573/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 * Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size>
575 */
576static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
577{
578 struct dm_snapshot *s;
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100579 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 char *origin_path;
Jonathan Brassowfee19982009-04-02 19:55:34 +0100582 struct dm_exception_store *store;
583 unsigned args_used;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700585 if (argc != 4) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700586 ti->error = "requires exactly 4 arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 r = -EINVAL;
Jonathan Brassowfee19982009-04-02 19:55:34 +0100588 goto bad_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
590
591 origin_path = argv[0];
Jonathan Brassowfee19982009-04-02 19:55:34 +0100592 argv++;
593 argc--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Jonathan Brassowfee19982009-04-02 19:55:34 +0100595 r = dm_exception_store_create(ti, argc, argv, &args_used, &store);
596 if (r) {
597 ti->error = "Couldn't create exception store";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 r = -EINVAL;
Jonathan Brassowfee19982009-04-02 19:55:34 +0100599 goto bad_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
601
Jonathan Brassowfee19982009-04-02 19:55:34 +0100602 argv += args_used;
603 argc -= args_used;
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 s = kmalloc(sizeof(*s), GFP_KERNEL);
Jonathan Brassowfee19982009-04-02 19:55:34 +0100606 if (!s) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 ti->error = "Cannot allocate snapshot context private "
608 "structure";
609 r = -ENOMEM;
Jonathan Brassowfee19982009-04-02 19:55:34 +0100610 goto bad_snap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
612
613 r = dm_get_device(ti, origin_path, 0, ti->len, FMODE_READ, &s->origin);
614 if (r) {
615 ti->error = "Cannot get origin device";
Jonathan Brassowfee19982009-04-02 19:55:34 +0100616 goto bad_origin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
618
Jonathan Brassowfee19982009-04-02 19:55:34 +0100619 s->store = store;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 s->valid = 1;
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -0800621 s->active = 0;
Mikulas Patocka879129d22008-10-30 13:33:16 +0000622 atomic_set(&s->pending_exceptions_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 init_rwsem(&s->lock);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700624 spin_lock_init(&s->pe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 /* Allocate hash table for COW data */
Jonathan Brassowfee19982009-04-02 19:55:34 +0100627 if (init_hash_tables(s)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 ti->error = "Unable to allocate hash table space";
629 r = -ENOMEM;
Jonathan Brassowfee19982009-04-02 19:55:34 +0100630 goto bad_hash_tables;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
632
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100633 r = dm_kcopyd_client_create(SNAPSHOT_PAGES, &s->kcopyd_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (r) {
635 ti->error = "Could not create kcopyd client";
Jonathan Brassowfee19982009-04-02 19:55:34 +0100636 goto bad_kcopyd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
638
Mikulas Patocka92e86812008-07-21 12:00:35 +0100639 s->pending_pool = mempool_create_slab_pool(MIN_IOS, pending_cache);
640 if (!s->pending_pool) {
641 ti->error = "Could not allocate mempool for pending exceptions";
Jonathan Brassowfee19982009-04-02 19:55:34 +0100642 goto bad_pending_pool;
Mikulas Patocka92e86812008-07-21 12:00:35 +0100643 }
644
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100645 s->tracked_chunk_pool = mempool_create_slab_pool(MIN_IOS,
646 tracked_chunk_cache);
647 if (!s->tracked_chunk_pool) {
648 ti->error = "Could not allocate tracked_chunk mempool for "
649 "tracking reads";
Mikulas Patocka92e86812008-07-21 12:00:35 +0100650 goto bad_tracked_chunk_pool;
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100651 }
652
653 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
654 INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]);
655
656 spin_lock_init(&s->tracked_chunk_lock);
657
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -0800658 /* Metadata must only be loaded into one table at once */
Jonathan Brassow493df712009-04-02 19:55:31 +0100659 r = s->store->type->read_metadata(s->store, dm_add_exception,
660 (void *)s);
Milan Broz07641472007-07-12 17:28:13 +0100661 if (r < 0) {
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -0700662 ti->error = "Failed to read snapshot metadata";
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100663 goto bad_load_and_register;
Milan Broz07641472007-07-12 17:28:13 +0100664 } else if (r > 0) {
665 s->valid = 0;
666 DMWARN("Snapshot is marked invalid.");
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -0700667 }
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -0800668
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700669 bio_list_init(&s->queued_bios);
David Howellsc4028952006-11-22 14:57:56 +0000670 INIT_WORK(&s->queued_bios_work, flush_queued_bios);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 /* Add snapshot to the list of snapshots for this origin */
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -0800673 /* Exceptions aren't triggered till snapshot_resume() is called */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (register_snapshot(s)) {
675 r = -EINVAL;
676 ti->error = "Cannot register snapshot origin";
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100677 goto bad_load_and_register;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679
680 ti->private = s;
Jonathan Brassowd0216842009-04-02 19:55:32 +0100681 ti->split_io = s->store->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 return 0;
684
Jonathan Brassowfee19982009-04-02 19:55:34 +0100685bad_load_and_register:
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100686 mempool_destroy(s->tracked_chunk_pool);
687
Jonathan Brassowfee19982009-04-02 19:55:34 +0100688bad_tracked_chunk_pool:
Mikulas Patocka92e86812008-07-21 12:00:35 +0100689 mempool_destroy(s->pending_pool);
690
Jonathan Brassowfee19982009-04-02 19:55:34 +0100691bad_pending_pool:
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100692 dm_kcopyd_client_destroy(s->kcopyd_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Jonathan Brassowfee19982009-04-02 19:55:34 +0100694bad_kcopyd:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 exit_exception_table(&s->pending, pending_cache);
696 exit_exception_table(&s->complete, exception_cache);
697
Jonathan Brassowfee19982009-04-02 19:55:34 +0100698bad_hash_tables:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 dm_put_device(ti, s->origin);
700
Jonathan Brassowfee19982009-04-02 19:55:34 +0100701bad_origin:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 kfree(s);
703
Jonathan Brassowfee19982009-04-02 19:55:34 +0100704bad_snap:
705 dm_exception_store_destroy(store);
706
707bad_args:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 return r;
709}
710
Milan Broz31c93a02006-12-08 02:41:11 -0800711static void __free_exceptions(struct dm_snapshot *s)
712{
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100713 dm_kcopyd_client_destroy(s->kcopyd_client);
Milan Broz31c93a02006-12-08 02:41:11 -0800714 s->kcopyd_client = NULL;
715
716 exit_exception_table(&s->pending, pending_cache);
717 exit_exception_table(&s->complete, exception_cache);
Milan Broz31c93a02006-12-08 02:41:11 -0800718}
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720static void snapshot_dtr(struct dm_target *ti)
721{
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100722#ifdef CONFIG_DM_DEBUG
723 int i;
724#endif
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100725 struct dm_snapshot *s = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700727 flush_workqueue(ksnapd);
728
Alasdair G Kergon138728dc2006-03-27 01:17:50 -0800729 /* Prevent further origin writes from using this snapshot. */
730 /* After this returns there can be no new kcopyd jobs. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 unregister_snapshot(s);
732
Mikulas Patocka879129d22008-10-30 13:33:16 +0000733 while (atomic_read(&s->pending_exceptions_count))
Mikulas Patocka90fa1522009-01-06 03:04:54 +0000734 msleep(1);
Mikulas Patocka879129d22008-10-30 13:33:16 +0000735 /*
736 * Ensure instructions in mempool_destroy aren't reordered
737 * before atomic_read.
738 */
739 smp_mb();
740
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100741#ifdef CONFIG_DM_DEBUG
742 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
743 BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i]));
744#endif
745
746 mempool_destroy(s->tracked_chunk_pool);
747
Milan Broz31c93a02006-12-08 02:41:11 -0800748 __free_exceptions(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Mikulas Patocka92e86812008-07-21 12:00:35 +0100750 mempool_destroy(s->pending_pool);
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 dm_put_device(ti, s->origin);
Jonathan Brassowfee19982009-04-02 19:55:34 +0100753
754 dm_exception_store_destroy(s->store);
Alasdair G Kergon138728dc2006-03-27 01:17:50 -0800755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 kfree(s);
757}
758
759/*
760 * Flush a list of buffers.
761 */
762static void flush_bios(struct bio *bio)
763{
764 struct bio *n;
765
766 while (bio) {
767 n = bio->bi_next;
768 bio->bi_next = NULL;
769 generic_make_request(bio);
770 bio = n;
771 }
772}
773
David Howellsc4028952006-11-22 14:57:56 +0000774static void flush_queued_bios(struct work_struct *work)
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700775{
David Howellsc4028952006-11-22 14:57:56 +0000776 struct dm_snapshot *s =
777 container_of(work, struct dm_snapshot, queued_bios_work);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700778 struct bio *queued_bios;
779 unsigned long flags;
780
781 spin_lock_irqsave(&s->pe_lock, flags);
782 queued_bios = bio_list_get(&s->queued_bios);
783 spin_unlock_irqrestore(&s->pe_lock, flags);
784
785 flush_bios(queued_bios);
786}
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788/*
789 * Error a list of buffers.
790 */
791static void error_bios(struct bio *bio)
792{
793 struct bio *n;
794
795 while (bio) {
796 n = bio->bi_next;
797 bio->bi_next = NULL;
NeilBrown6712ecf2007-09-27 12:47:43 +0200798 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 bio = n;
800 }
801}
802
Alasdair G Kergon695368a2006-10-03 01:15:31 -0700803static void __invalidate_snapshot(struct dm_snapshot *s, int err)
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800804{
805 if (!s->valid)
806 return;
807
808 if (err == -EIO)
809 DMERR("Invalidating snapshot: Error reading/writing.");
810 else if (err == -ENOMEM)
811 DMERR("Invalidating snapshot: Unable to allocate exception.");
812
Jonathan Brassow493df712009-04-02 19:55:31 +0100813 if (s->store->type->drop_snapshot)
814 s->store->type->drop_snapshot(s->store);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800815
816 s->valid = 0;
817
Jonathan Brassow0cea9c72009-04-02 19:55:32 +0100818 dm_table_event(s->store->ti->table);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800819}
820
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100821static void get_pending_exception(struct dm_snap_pending_exception *pe)
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700822{
823 atomic_inc(&pe->ref_count);
824}
825
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100826static struct bio *put_pending_exception(struct dm_snap_pending_exception *pe)
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700827{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100828 struct dm_snap_pending_exception *primary_pe;
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700829 struct bio *origin_bios = NULL;
830
831 primary_pe = pe->primary_pe;
832
833 /*
834 * If this pe is involved in a write to the origin and
835 * it is the last sibling to complete then release
836 * the bios for the original write to the origin.
837 */
838 if (primary_pe &&
Mikulas Patocka7c5f78b2008-10-21 17:44:51 +0100839 atomic_dec_and_test(&primary_pe->ref_count)) {
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700840 origin_bios = bio_list_get(&primary_pe->origin_bios);
Mikulas Patocka7c5f78b2008-10-21 17:44:51 +0100841 free_pending_exception(primary_pe);
842 }
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700843
844 /*
845 * Free the pe if it's not linked to an origin write or if
846 * it's not itself a primary pe.
847 */
848 if (!primary_pe || primary_pe != pe)
849 free_pending_exception(pe);
850
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700851 return origin_bios;
852}
853
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100854static void pending_complete(struct dm_snap_pending_exception *pe, int success)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100856 struct dm_snap_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 struct dm_snapshot *s = pe->snap;
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700858 struct bio *origin_bios = NULL;
859 struct bio *snapshot_bios = NULL;
860 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800862 if (!success) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 /* Read/write error - snapshot is unusable */
864 down_write(&s->lock);
Alasdair G Kergon695368a2006-10-03 01:15:31 -0700865 __invalidate_snapshot(s, -EIO);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700866 error = 1;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800867 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
869
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800870 e = alloc_exception();
871 if (!e) {
872 down_write(&s->lock);
Alasdair G Kergon695368a2006-10-03 01:15:31 -0700873 __invalidate_snapshot(s, -ENOMEM);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700874 error = 1;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800875 goto out;
876 }
877 *e = pe->e;
878
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700879 down_write(&s->lock);
880 if (!s->valid) {
881 free_exception(e);
882 error = 1;
883 goto out;
884 }
885
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800886 /*
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100887 * Check for conflicting reads. This is extremely improbable,
Mikulas Patocka90fa1522009-01-06 03:04:54 +0000888 * so msleep(1) is sufficient and there is no need for a wait queue.
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100889 */
890 while (__chunk_is_tracked(s, pe->e.old_chunk))
Mikulas Patocka90fa1522009-01-06 03:04:54 +0000891 msleep(1);
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100892
893 /*
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800894 * Add a proper exception, and remove the
895 * in-flight exception from the list.
896 */
Milan Brozd74f81f2008-02-08 02:11:27 +0000897 insert_completed_exception(s, e);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 out:
Alasdair G Kergon695368a2006-10-03 01:15:31 -0700900 remove_exception(&pe->e);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700901 snapshot_bios = bio_list_get(&pe->snapshot_bios);
Alasdair G Kergon4b832e82006-10-03 01:15:30 -0700902 origin_bios = put_pending_exception(pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -0700904 up_write(&s->lock);
905
906 /* Submit any pending write bios */
907 if (error)
908 error_bios(snapshot_bios);
909 else
910 flush_bios(snapshot_bios);
911
912 flush_bios(origin_bios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
915static void commit_callback(void *context, int success)
916{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100917 struct dm_snap_pending_exception *pe = context;
918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 pending_complete(pe, success);
920}
921
922/*
923 * Called when the copy I/O has finished. kcopyd actually runs
924 * this code so don't block.
925 */
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -0700926static void copy_callback(int read_err, unsigned long write_err, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100928 struct dm_snap_pending_exception *pe = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 struct dm_snapshot *s = pe->snap;
930
931 if (read_err || write_err)
932 pending_complete(pe, 0);
933
934 else
935 /* Update the metadata if we are persistent */
Jonathan Brassow493df712009-04-02 19:55:31 +0100936 s->store->type->commit_exception(s->store, &pe->e,
937 commit_callback, pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
940/*
941 * Dispatches the copy operation to kcopyd.
942 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100943static void start_copy(struct dm_snap_pending_exception *pe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
945 struct dm_snapshot *s = pe->snap;
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +0100946 struct dm_io_region src, dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 struct block_device *bdev = s->origin->bdev;
948 sector_t dev_size;
949
950 dev_size = get_dev_size(bdev);
951
952 src.bdev = bdev;
Jonathan Brassow71fab002009-04-02 19:55:33 +0100953 src.sector = chunk_to_sector(s->store, pe->e.old_chunk);
Jonathan Brassowd0216842009-04-02 19:55:32 +0100954 src.count = min(s->store->chunk_size, dev_size - src.sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Jonathan Brassow49beb2b2009-04-02 19:55:33 +0100956 dest.bdev = s->store->cow->bdev;
Jonathan Brassow71fab002009-04-02 19:55:33 +0100957 dest.sector = chunk_to_sector(s->store, pe->e.new_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 dest.count = src.count;
959
960 /* Hand over to kcopyd */
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100961 dm_kcopyd_copy(s->kcopyd_client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 &src, 1, &dest, 0, copy_callback, pe);
963}
964
Mikulas Patocka29138082009-04-02 19:55:25 +0100965static struct dm_snap_pending_exception *
966__lookup_pending_exception(struct dm_snapshot *s, chunk_t chunk)
967{
968 struct dm_snap_exception *e = lookup_exception(&s->pending, chunk);
969
970 if (!e)
971 return NULL;
972
973 return container_of(e, struct dm_snap_pending_exception, e);
974}
975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976/*
977 * Looks to see if this snapshot already has a pending exception
978 * for this chunk, otherwise it allocates a new one and inserts
979 * it into the pending table.
980 *
981 * NOTE: a write lock must be held on snap->lock before calling
982 * this.
983 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100984static struct dm_snap_pending_exception *
Mikulas Patockac6621392009-04-02 19:55:25 +0100985__find_pending_exception(struct dm_snapshot *s,
986 struct dm_snap_pending_exception *pe, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
Mikulas Patockac6621392009-04-02 19:55:25 +0100988 struct dm_snap_pending_exception *pe2;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800989
Mikulas Patocka29138082009-04-02 19:55:25 +0100990 pe2 = __lookup_pending_exception(s, chunk);
991 if (pe2) {
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800992 free_pending_exception(pe);
Mikulas Patocka29138082009-04-02 19:55:25 +0100993 return pe2;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -0800994 }
995
996 pe->e.old_chunk = chunk;
997 bio_list_init(&pe->origin_bios);
998 bio_list_init(&pe->snapshot_bios);
999 pe->primary_pe = NULL;
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001000 atomic_set(&pe->ref_count, 0);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001001 pe->started = 0;
1002
Jonathan Brassow493df712009-04-02 19:55:31 +01001003 if (s->store->type->prepare_exception(s->store, &pe->e)) {
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001004 free_pending_exception(pe);
1005 return NULL;
1006 }
1007
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001008 get_pending_exception(pe);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001009 insert_exception(&s->pending, &pe->e);
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 return pe;
1012}
1013
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001014static void remap_exception(struct dm_snapshot *s, struct dm_snap_exception *e,
Milan Brozd74f81f2008-02-08 02:11:27 +00001015 struct bio *bio, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
Jonathan Brassow49beb2b2009-04-02 19:55:33 +01001017 bio->bi_bdev = s->store->cow->bdev;
Jonathan Brassow71fab002009-04-02 19:55:33 +01001018 bio->bi_sector = chunk_to_sector(s->store,
1019 dm_chunk_number(e->new_chunk) +
1020 (chunk - e->old_chunk)) +
1021 (bio->bi_sector &
1022 s->store->chunk_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}
1024
1025static int snapshot_map(struct dm_target *ti, struct bio *bio,
1026 union map_info *map_context)
1027{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001028 struct dm_snap_exception *e;
1029 struct dm_snapshot *s = ti->private;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001030 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 chunk_t chunk;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001032 struct dm_snap_pending_exception *pe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Jonathan Brassow71fab002009-04-02 19:55:33 +01001034 chunk = sector_to_chunk(s->store, bio->bi_sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 /* Full snapshots are not usable */
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001037 /* To get here the table must be live so s->active is always set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 if (!s->valid)
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -07001039 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001041 /* FIXME: should only take write lock if we need
1042 * to copy an exception */
1043 down_write(&s->lock);
1044
1045 if (!s->valid) {
1046 r = -EIO;
1047 goto out_unlock;
1048 }
1049
1050 /* If the block is already remapped - use that, else remap it */
1051 e = lookup_exception(&s->complete, chunk);
1052 if (e) {
Milan Brozd74f81f2008-02-08 02:11:27 +00001053 remap_exception(s, e, bio, chunk);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001054 goto out_unlock;
1055 }
1056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 /*
1058 * Write to snapshot - higher level takes care of RW/RO
1059 * flags so we should only get this if we are
1060 * writeable.
1061 */
1062 if (bio_rw(bio) == WRITE) {
Mikulas Patocka29138082009-04-02 19:55:25 +01001063 pe = __lookup_pending_exception(s, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001064 if (!pe) {
Mikulas Patockac6621392009-04-02 19:55:25 +01001065 up_write(&s->lock);
1066 pe = alloc_pending_exception(s);
1067 down_write(&s->lock);
1068
1069 if (!s->valid) {
1070 free_pending_exception(pe);
1071 r = -EIO;
1072 goto out_unlock;
1073 }
1074
Mikulas Patocka35bf6592009-04-02 19:55:26 +01001075 e = lookup_exception(&s->complete, chunk);
1076 if (e) {
1077 free_pending_exception(pe);
1078 remap_exception(s, e, bio, chunk);
1079 goto out_unlock;
1080 }
1081
Mikulas Patockac6621392009-04-02 19:55:25 +01001082 pe = __find_pending_exception(s, pe, chunk);
Mikulas Patocka29138082009-04-02 19:55:25 +01001083 if (!pe) {
1084 __invalidate_snapshot(s, -ENOMEM);
1085 r = -EIO;
1086 goto out_unlock;
1087 }
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001088 }
1089
Milan Brozd74f81f2008-02-08 02:11:27 +00001090 remap_exception(s, &pe->e, bio, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001091 bio_list_add(&pe->snapshot_bios, bio);
1092
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001093 r = DM_MAPIO_SUBMITTED;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001094
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001095 if (!pe->started) {
1096 /* this is protected by snap->lock */
1097 pe->started = 1;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001098 up_write(&s->lock);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001099 start_copy(pe);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001100 goto out;
1101 }
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001102 } else {
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001103 bio->bi_bdev = s->origin->bdev;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001104 map_context->ptr = track_chunk(s, chunk);
1105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001107 out_unlock:
1108 up_write(&s->lock);
1109 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 return r;
1111}
1112
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001113static int snapshot_end_io(struct dm_target *ti, struct bio *bio,
1114 int error, union map_info *map_context)
1115{
1116 struct dm_snapshot *s = ti->private;
1117 struct dm_snap_tracked_chunk *c = map_context->ptr;
1118
1119 if (c)
1120 stop_tracking_chunk(s, c);
1121
1122 return 0;
1123}
1124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125static void snapshot_resume(struct dm_target *ti)
1126{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001127 struct dm_snapshot *s = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001129 down_write(&s->lock);
1130 s->active = 1;
1131 up_write(&s->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132}
1133
1134static int snapshot_status(struct dm_target *ti, status_type_t type,
1135 char *result, unsigned int maxlen)
1136{
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01001137 unsigned sz = 0;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001138 struct dm_snapshot *snap = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 switch (type) {
1141 case STATUSTYPE_INFO:
1142 if (!snap->valid)
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01001143 DMEMIT("Invalid");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 else {
Jonathan Brassow493df712009-04-02 19:55:31 +01001145 if (snap->store->type->fraction_full) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 sector_t numerator, denominator;
Jonathan Brassow493df712009-04-02 19:55:31 +01001147 snap->store->type->fraction_full(snap->store,
1148 &numerator,
1149 &denominator);
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01001150 DMEMIT("%llu/%llu",
1151 (unsigned long long)numerator,
1152 (unsigned long long)denominator);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 }
1154 else
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01001155 DMEMIT("Unknown");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
1157 break;
1158
1159 case STATUSTYPE_TABLE:
1160 /*
1161 * kdevname returns a static pointer so we need
1162 * to make private copies if the output is to
1163 * make sense.
1164 */
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01001165 DMEMIT("%s", snap->origin->name);
Jonathan Brassow1e302a92009-04-02 19:55:35 +01001166 snap->store->type->status(snap->store, type, result + sz,
1167 maxlen - sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 break;
1169 }
1170
1171 return 0;
1172}
1173
1174/*-----------------------------------------------------------------
1175 * Origin methods
1176 *---------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177static int __origin_write(struct list_head *snapshots, struct bio *bio)
1178{
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001179 int r = DM_MAPIO_REMAPPED, first = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 struct dm_snapshot *snap;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001181 struct dm_snap_exception *e;
1182 struct dm_snap_pending_exception *pe, *next_pe, *primary_pe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 chunk_t chunk;
Alasdair G Kergoneccf0812006-03-27 01:17:42 -08001184 LIST_HEAD(pe_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 /* Do all the snapshots on this origin */
1187 list_for_each_entry (snap, snapshots, list) {
1188
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001189 down_write(&snap->lock);
1190
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001191 /* Only deal with valid and active snapshots */
1192 if (!snap->valid || !snap->active)
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001193 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Alasdair G Kergond5e404c2005-07-12 15:53:05 -07001195 /* Nothing to do if writing beyond end of snapshot */
Jonathan Brassow0cea9c72009-04-02 19:55:32 +01001196 if (bio->bi_sector >= dm_table_get_size(snap->store->ti->table))
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001197 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 /*
1200 * Remember, different snapshots can have
1201 * different chunk sizes.
1202 */
Jonathan Brassow71fab002009-04-02 19:55:33 +01001203 chunk = sector_to_chunk(snap->store, bio->bi_sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 /*
1206 * Check exception table to see if block
1207 * is already remapped in this snapshot
1208 * and trigger an exception if not.
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001209 *
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001210 * ref_count is initialised to 1 so pending_complete()
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001211 * won't destroy the primary_pe while we're inside this loop.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 */
1213 e = lookup_exception(&snap->complete, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001214 if (e)
1215 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Mikulas Patocka29138082009-04-02 19:55:25 +01001217 pe = __lookup_pending_exception(snap, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001218 if (!pe) {
Mikulas Patockac6621392009-04-02 19:55:25 +01001219 up_write(&snap->lock);
1220 pe = alloc_pending_exception(snap);
1221 down_write(&snap->lock);
1222
1223 if (!snap->valid) {
1224 free_pending_exception(pe);
1225 goto next_snapshot;
1226 }
1227
Mikulas Patocka35bf6592009-04-02 19:55:26 +01001228 e = lookup_exception(&snap->complete, chunk);
1229 if (e) {
1230 free_pending_exception(pe);
1231 goto next_snapshot;
1232 }
1233
Mikulas Patockac6621392009-04-02 19:55:25 +01001234 pe = __find_pending_exception(snap, pe, chunk);
Mikulas Patocka29138082009-04-02 19:55:25 +01001235 if (!pe) {
1236 __invalidate_snapshot(snap, -ENOMEM);
1237 goto next_snapshot;
1238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
1240
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001241 if (!primary_pe) {
1242 /*
1243 * Either every pe here has same
1244 * primary_pe or none has one yet.
1245 */
1246 if (pe->primary_pe)
1247 primary_pe = pe->primary_pe;
1248 else {
1249 primary_pe = pe;
1250 first = 1;
1251 }
1252
1253 bio_list_add(&primary_pe->origin_bios, bio);
1254
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001255 r = DM_MAPIO_SUBMITTED;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001256 }
1257
1258 if (!pe->primary_pe) {
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001259 pe->primary_pe = primary_pe;
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001260 get_pending_exception(primary_pe);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001261 }
1262
1263 if (!pe->started) {
1264 pe->started = 1;
1265 list_add_tail(&pe->list, &pe_queue);
1266 }
1267
1268 next_snapshot:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 up_write(&snap->lock);
1270 }
1271
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001272 if (!primary_pe)
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001273 return r;
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001274
1275 /*
1276 * If this is the first time we're processing this chunk and
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001277 * ref_count is now 1 it means all the pending exceptions
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001278 * got completed while we were in the loop above, so it falls to
1279 * us here to remove the primary_pe and submit any origin_bios.
1280 */
1281
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001282 if (first && atomic_dec_and_test(&primary_pe->ref_count)) {
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001283 flush_bios(bio_list_get(&primary_pe->origin_bios));
1284 free_pending_exception(primary_pe);
1285 /* If we got here, pe_queue is necessarily empty. */
Alasdair G Kergon4b832e82006-10-03 01:15:30 -07001286 return r;
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001287 }
1288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 /*
1290 * Now that we have a complete pe list we can start the copying.
1291 */
Alasdair G Kergoneccf0812006-03-27 01:17:42 -08001292 list_for_each_entry_safe(pe, next_pe, &pe_queue, list)
1293 start_copy(pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 return r;
1296}
1297
1298/*
1299 * Called on a write from the origin driver.
1300 */
1301static int do_origin(struct dm_dev *origin, struct bio *bio)
1302{
1303 struct origin *o;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001304 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 down_read(&_origins_lock);
1307 o = __lookup_origin(origin->bdev);
1308 if (o)
1309 r = __origin_write(&o->snapshots, bio);
1310 up_read(&_origins_lock);
1311
1312 return r;
1313}
1314
1315/*
1316 * Origin: maps a linear range of a device, with hooks for snapshotting.
1317 */
1318
1319/*
1320 * Construct an origin mapping: <dev_path>
1321 * The context for an origin is merely a 'struct dm_dev *'
1322 * pointing to the real device.
1323 */
1324static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1325{
1326 int r;
1327 struct dm_dev *dev;
1328
1329 if (argc != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001330 ti->error = "origin: incorrect number of arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 return -EINVAL;
1332 }
1333
1334 r = dm_get_device(ti, argv[0], 0, ti->len,
1335 dm_table_get_mode(ti->table), &dev);
1336 if (r) {
1337 ti->error = "Cannot get target device";
1338 return r;
1339 }
1340
1341 ti->private = dev;
1342 return 0;
1343}
1344
1345static void origin_dtr(struct dm_target *ti)
1346{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001347 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 dm_put_device(ti, dev);
1349}
1350
1351static int origin_map(struct dm_target *ti, struct bio *bio,
1352 union map_info *map_context)
1353{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001354 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 bio->bi_bdev = dev->bdev;
1356
1357 /* Only tell snapshots if this is a write */
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001358 return (bio_rw(bio) == WRITE) ? do_origin(dev, bio) : DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359}
1360
1361#define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
1362
1363/*
1364 * Set the target "split_io" field to the minimum of all the snapshots'
1365 * chunk sizes.
1366 */
1367static void origin_resume(struct dm_target *ti)
1368{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001369 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 struct dm_snapshot *snap;
1371 struct origin *o;
1372 chunk_t chunk_size = 0;
1373
1374 down_read(&_origins_lock);
1375 o = __lookup_origin(dev->bdev);
1376 if (o)
1377 list_for_each_entry (snap, &o->snapshots, list)
Jonathan Brassowd0216842009-04-02 19:55:32 +01001378 chunk_size = min_not_zero(chunk_size,
1379 snap->store->chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 up_read(&_origins_lock);
1381
1382 ti->split_io = chunk_size;
1383}
1384
1385static int origin_status(struct dm_target *ti, status_type_t type, char *result,
1386 unsigned int maxlen)
1387{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001388 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390 switch (type) {
1391 case STATUSTYPE_INFO:
1392 result[0] = '\0';
1393 break;
1394
1395 case STATUSTYPE_TABLE:
1396 snprintf(result, maxlen, "%s", dev->name);
1397 break;
1398 }
1399
1400 return 0;
1401}
1402
1403static struct target_type origin_target = {
1404 .name = "snapshot-origin",
Milan Brozd74f81f2008-02-08 02:11:27 +00001405 .version = {1, 6, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 .module = THIS_MODULE,
1407 .ctr = origin_ctr,
1408 .dtr = origin_dtr,
1409 .map = origin_map,
1410 .resume = origin_resume,
1411 .status = origin_status,
1412};
1413
1414static struct target_type snapshot_target = {
1415 .name = "snapshot",
Milan Brozd74f81f2008-02-08 02:11:27 +00001416 .version = {1, 6, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 .module = THIS_MODULE,
1418 .ctr = snapshot_ctr,
1419 .dtr = snapshot_dtr,
1420 .map = snapshot_map,
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001421 .end_io = snapshot_end_io,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 .resume = snapshot_resume,
1423 .status = snapshot_status,
1424};
1425
1426static int __init dm_snapshot_init(void)
1427{
1428 int r;
1429
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00001430 r = dm_exception_store_init();
1431 if (r) {
1432 DMERR("Failed to initialize exception stores");
1433 return r;
1434 }
1435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 r = dm_register_target(&snapshot_target);
1437 if (r) {
1438 DMERR("snapshot target register failed %d", r);
1439 return r;
1440 }
1441
1442 r = dm_register_target(&origin_target);
1443 if (r < 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001444 DMERR("Origin target register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 goto bad1;
1446 }
1447
1448 r = init_origin_hash();
1449 if (r) {
1450 DMERR("init_origin_hash failed.");
1451 goto bad2;
1452 }
1453
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001454 exception_cache = KMEM_CACHE(dm_snap_exception, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 if (!exception_cache) {
1456 DMERR("Couldn't create exception cache.");
1457 r = -ENOMEM;
1458 goto bad3;
1459 }
1460
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001461 pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 if (!pending_cache) {
1463 DMERR("Couldn't create pending cache.");
1464 r = -ENOMEM;
1465 goto bad4;
1466 }
1467
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001468 tracked_chunk_cache = KMEM_CACHE(dm_snap_tracked_chunk, 0);
1469 if (!tracked_chunk_cache) {
1470 DMERR("Couldn't create cache to track chunks in use.");
1471 r = -ENOMEM;
1472 goto bad5;
1473 }
1474
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001475 ksnapd = create_singlethread_workqueue("ksnapd");
1476 if (!ksnapd) {
1477 DMERR("Failed to create ksnapd workqueue.");
1478 r = -ENOMEM;
Mikulas Patocka92e86812008-07-21 12:00:35 +01001479 goto bad_pending_pool;
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001480 }
1481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 return 0;
1483
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00001484bad_pending_pool:
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001485 kmem_cache_destroy(tracked_chunk_cache);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00001486bad5:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 kmem_cache_destroy(pending_cache);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00001488bad4:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 kmem_cache_destroy(exception_cache);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00001490bad3:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 exit_origin_hash();
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00001492bad2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 dm_unregister_target(&origin_target);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00001494bad1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 dm_unregister_target(&snapshot_target);
1496 return r;
1497}
1498
1499static void __exit dm_snapshot_exit(void)
1500{
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001501 destroy_workqueue(ksnapd);
1502
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00001503 dm_unregister_target(&snapshot_target);
1504 dm_unregister_target(&origin_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
1506 exit_origin_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 kmem_cache_destroy(pending_cache);
1508 kmem_cache_destroy(exception_cache);
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001509 kmem_cache_destroy(tracked_chunk_cache);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00001510
1511 dm_exception_store_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512}
1513
1514/* Module hooks */
1515module_init(dm_snapshot_init);
1516module_exit(dm_snapshot_exit);
1517
1518MODULE_DESCRIPTION(DM_NAME " snapshot target");
1519MODULE_AUTHOR("Joe Thornber");
1520MODULE_LICENSE("GPL");