blob: dc2412e6c5cf2eea11ed94549a5f100f7fd3ecb9 [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
Alasdair G Kergon72d94862006-06-26 00:27:35 -070026#define DM_MSG_PREFIX "snapshots"
27
Mikulas Patockad698aa42009-12-10 23:52:30 +000028static const char dm_snapshot_merge_target_name[] = "snapshot-merge";
29
30#define dm_target_is_snapshot_merge(ti) \
31 ((ti)->type->name == dm_snapshot_merge_target_name)
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/*
34 * The percentage increment we will wake up users at
35 */
36#define WAKE_UP_PERCENT 5
37
38/*
39 * kcopyd priority of snapshot operations
40 */
41#define SNAPSHOT_COPY_PRIORITY 2
42
43/*
Milan Broz8ee27672008-04-24 21:42:36 +010044 * Reserve 1MB for each snapshot initially (with minimum of 1 page).
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 */
Milan Broz8ee27672008-04-24 21:42:36 +010046#define SNAPSHOT_PAGES (((1UL << 20) >> PAGE_SHIFT) ? : 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Mikulas Patockacd45daf2008-07-21 12:00:32 +010048/*
49 * The size of the mempool used to track chunks in use.
50 */
51#define MIN_IOS 256
52
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010053#define DM_TRACKED_CHUNK_HASH_SIZE 16
54#define DM_TRACKED_CHUNK_HASH(x) ((unsigned long)(x) & \
55 (DM_TRACKED_CHUNK_HASH_SIZE - 1))
56
Jon Brassow191437a2009-12-10 23:52:10 +000057struct dm_exception_table {
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010058 uint32_t hash_mask;
59 unsigned hash_shift;
60 struct list_head *table;
61};
62
63struct dm_snapshot {
64 struct rw_semaphore lock;
65
66 struct dm_dev *origin;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +000067 struct dm_dev *cow;
68
69 struct dm_target *ti;
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010070
71 /* List of snapshots per Origin */
72 struct list_head list;
73
74 /* You can't use a snapshot if this is 0 (e.g. if full) */
75 int valid;
76
77 /* Origin writes don't trigger exceptions until this is set */
78 int active;
79
Mike Snitzerc26655c2009-12-10 23:52:12 +000080 /* Whether or not owning mapped_device is suspended */
81 int suspended;
82
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010083 mempool_t *pending_pool;
84
85 atomic_t pending_exceptions_count;
86
Jon Brassow191437a2009-12-10 23:52:10 +000087 struct dm_exception_table pending;
88 struct dm_exception_table complete;
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010089
90 /*
91 * pe_lock protects all pending_exception operations and access
92 * as well as the snapshot_bios list.
93 */
94 spinlock_t pe_lock;
95
96 /* The on disk metadata handler */
97 struct dm_exception_store *store;
98
99 struct dm_kcopyd_client *kcopyd_client;
100
101 /* Queue of snapshot writes for ksnapd to flush */
102 struct bio_list queued_bios;
103 struct work_struct queued_bios_work;
104
105 /* Chunks with outstanding reads */
106 mempool_t *tracked_chunk_pool;
107 spinlock_t tracked_chunk_lock;
108 struct hlist_head tracked_chunk_hash[DM_TRACKED_CHUNK_HASH_SIZE];
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000109
110 /* Wait for events based on state_bits */
111 unsigned long state_bits;
Jonathan Brassowccc45ea2009-04-02 19:55:34 +0100112};
113
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000114/*
115 * state_bits:
116 * RUNNING_MERGE - Merge operation is in progress.
117 * SHUTDOWN_MERGE - Set to signal that merge needs to be stopped;
118 * cleared afterwards.
119 */
120#define RUNNING_MERGE 0
121#define SHUTDOWN_MERGE 1
122
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000123struct dm_dev *dm_snap_cow(struct dm_snapshot *s)
124{
125 return s->cow;
126}
127EXPORT_SYMBOL(dm_snap_cow);
128
Adrian Bunkc642f9e2006-12-08 02:41:13 -0800129static struct workqueue_struct *ksnapd;
David Howellsc4028952006-11-22 14:57:56 +0000130static void flush_queued_bios(struct work_struct *work);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700131
Jonathan Brassowccc45ea2009-04-02 19:55:34 +0100132static sector_t chunk_to_sector(struct dm_exception_store *store,
133 chunk_t chunk)
134{
135 return chunk << store->chunk_shift;
136}
137
138static int bdev_equal(struct block_device *lhs, struct block_device *rhs)
139{
140 /*
141 * There is only ever one instance of a particular block
142 * device so we can compare pointers safely.
143 */
144 return lhs == rhs;
145}
146
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100147struct dm_snap_pending_exception {
Jon Brassow1d4989c2009-12-10 23:52:10 +0000148 struct dm_exception e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 /*
151 * Origin buffers waiting for this to complete are held
152 * in a bio list
153 */
154 struct bio_list origin_bios;
155 struct bio_list snapshot_bios;
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 /* Pointer back to snapshot context */
158 struct dm_snapshot *snap;
159
160 /*
161 * 1 indicates the exception has already been sent to
162 * kcopyd.
163 */
164 int started;
165};
166
167/*
168 * Hash table mapping origin volumes to lists of snapshots and
169 * a lock to protect it
170 */
Christoph Lametere18b8902006-12-06 20:33:20 -0800171static struct kmem_cache *exception_cache;
172static struct kmem_cache *pending_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100174struct dm_snap_tracked_chunk {
175 struct hlist_node node;
176 chunk_t chunk;
177};
178
179static struct kmem_cache *tracked_chunk_cache;
180
181static struct dm_snap_tracked_chunk *track_chunk(struct dm_snapshot *s,
182 chunk_t chunk)
183{
184 struct dm_snap_tracked_chunk *c = mempool_alloc(s->tracked_chunk_pool,
185 GFP_NOIO);
186 unsigned long flags;
187
188 c->chunk = chunk;
189
190 spin_lock_irqsave(&s->tracked_chunk_lock, flags);
191 hlist_add_head(&c->node,
192 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)]);
193 spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
194
195 return c;
196}
197
198static void stop_tracking_chunk(struct dm_snapshot *s,
199 struct dm_snap_tracked_chunk *c)
200{
201 unsigned long flags;
202
203 spin_lock_irqsave(&s->tracked_chunk_lock, flags);
204 hlist_del(&c->node);
205 spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
206
207 mempool_free(c, s->tracked_chunk_pool);
208}
209
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100210static int __chunk_is_tracked(struct dm_snapshot *s, chunk_t chunk)
211{
212 struct dm_snap_tracked_chunk *c;
213 struct hlist_node *hn;
214 int found = 0;
215
216 spin_lock_irq(&s->tracked_chunk_lock);
217
218 hlist_for_each_entry(c, hn,
219 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)], node) {
220 if (c->chunk == chunk) {
221 found = 1;
222 break;
223 }
224 }
225
226 spin_unlock_irq(&s->tracked_chunk_lock);
227
228 return found;
229}
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231/*
Mike Snitzer615d1eb2009-12-10 23:52:29 +0000232 * This conflicting I/O is extremely improbable in the caller,
233 * so msleep(1) is sufficient and there is no need for a wait queue.
234 */
235static void __check_for_conflicting_io(struct dm_snapshot *s, chunk_t chunk)
236{
237 while (__chunk_is_tracked(s, chunk))
238 msleep(1);
239}
240
241/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 * One of these per registered origin, held in the snapshot_origins hash
243 */
244struct origin {
245 /* The origin device */
246 struct block_device *bdev;
247
248 struct list_head hash_list;
249
250 /* List of snapshots for this origin */
251 struct list_head snapshots;
252};
253
254/*
255 * Size of the hash table for origin volumes. If we make this
256 * the size of the minors list then it should be nearly perfect
257 */
258#define ORIGIN_HASH_SIZE 256
259#define ORIGIN_MASK 0xFF
260static struct list_head *_origins;
261static struct rw_semaphore _origins_lock;
262
263static int init_origin_hash(void)
264{
265 int i;
266
267 _origins = kmalloc(ORIGIN_HASH_SIZE * sizeof(struct list_head),
268 GFP_KERNEL);
269 if (!_origins) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700270 DMERR("unable to allocate memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return -ENOMEM;
272 }
273
274 for (i = 0; i < ORIGIN_HASH_SIZE; i++)
275 INIT_LIST_HEAD(_origins + i);
276 init_rwsem(&_origins_lock);
277
278 return 0;
279}
280
281static void exit_origin_hash(void)
282{
283 kfree(_origins);
284}
285
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100286static unsigned origin_hash(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 return bdev->bd_dev & ORIGIN_MASK;
289}
290
291static struct origin *__lookup_origin(struct block_device *origin)
292{
293 struct list_head *ol;
294 struct origin *o;
295
296 ol = &_origins[origin_hash(origin)];
297 list_for_each_entry (o, ol, hash_list)
298 if (bdev_equal(o->bdev, origin))
299 return o;
300
301 return NULL;
302}
303
304static void __insert_origin(struct origin *o)
305{
306 struct list_head *sl = &_origins[origin_hash(o->bdev)];
307 list_add_tail(&o->hash_list, sl);
308}
309
310/*
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000311 * _origins_lock must be held when calling this function.
312 * Returns number of snapshots registered using the supplied cow device, plus:
313 * snap_src - a snapshot suitable for use as a source of exception handover
314 * snap_dest - a snapshot capable of receiving exception handover.
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000315 * snap_merge - an existing snapshot-merge target linked to the same origin.
316 * There can be at most one snapshot-merge target. The parameter is optional.
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000317 *
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000318 * Possible return values and states of snap_src and snap_dest.
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000319 * 0: NULL, NULL - first new snapshot
320 * 1: snap_src, NULL - normal snapshot
321 * 2: snap_src, snap_dest - waiting for handover
322 * 2: snap_src, NULL - handed over, waiting for old to be deleted
323 * 1: NULL, snap_dest - source got destroyed without handover
324 */
325static int __find_snapshots_sharing_cow(struct dm_snapshot *snap,
326 struct dm_snapshot **snap_src,
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000327 struct dm_snapshot **snap_dest,
328 struct dm_snapshot **snap_merge)
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000329{
330 struct dm_snapshot *s;
331 struct origin *o;
332 int count = 0;
333 int active;
334
335 o = __lookup_origin(snap->origin->bdev);
336 if (!o)
337 goto out;
338
339 list_for_each_entry(s, &o->snapshots, list) {
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000340 if (dm_target_is_snapshot_merge(s->ti) && snap_merge)
341 *snap_merge = s;
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000342 if (!bdev_equal(s->cow->bdev, snap->cow->bdev))
343 continue;
344
345 down_read(&s->lock);
346 active = s->active;
347 up_read(&s->lock);
348
349 if (active) {
350 if (snap_src)
351 *snap_src = s;
352 } else if (snap_dest)
353 *snap_dest = s;
354
355 count++;
356 }
357
358out:
359 return count;
360}
361
362/*
363 * On success, returns 1 if this snapshot is a handover destination,
364 * otherwise returns 0.
365 */
366static int __validate_exception_handover(struct dm_snapshot *snap)
367{
368 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000369 struct dm_snapshot *snap_merge = NULL;
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000370
371 /* Does snapshot need exceptions handed over to it? */
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000372 if ((__find_snapshots_sharing_cow(snap, &snap_src, &snap_dest,
373 &snap_merge) == 2) ||
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000374 snap_dest) {
375 snap->ti->error = "Snapshot cow pairing for exception "
376 "table handover failed";
377 return -EINVAL;
378 }
379
380 /*
381 * If no snap_src was found, snap cannot become a handover
382 * destination.
383 */
384 if (!snap_src)
385 return 0;
386
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000387 /*
388 * Non-snapshot-merge handover?
389 */
390 if (!dm_target_is_snapshot_merge(snap->ti))
391 return 1;
392
393 /*
394 * Do not allow more than one merging snapshot.
395 */
396 if (snap_merge) {
397 snap->ti->error = "A snapshot is already merging.";
398 return -EINVAL;
399 }
400
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000401 if (!snap_src->store->type->prepare_merge ||
402 !snap_src->store->type->commit_merge) {
403 snap->ti->error = "Snapshot exception store does not "
404 "support snapshot-merge.";
405 return -EINVAL;
406 }
407
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000408 return 1;
409}
410
411static void __insert_snapshot(struct origin *o, struct dm_snapshot *s)
412{
413 struct dm_snapshot *l;
414
415 /* Sort the list according to chunk size, largest-first smallest-last */
416 list_for_each_entry(l, &o->snapshots, list)
417 if (l->store->chunk_size < s->store->chunk_size)
418 break;
419 list_add_tail(&s->list, &l->list);
420}
421
422/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 * Make a note of the snapshot and its origin so we can look it
424 * up when the origin has a write on it.
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000425 *
426 * Also validate snapshot exception store handovers.
427 * On success, returns 1 if this registration is a handover destination,
428 * otherwise returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 */
430static int register_snapshot(struct dm_snapshot *snap)
431{
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000432 struct origin *o, *new_o = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 struct block_device *bdev = snap->origin->bdev;
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000434 int r = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000436 new_o = kmalloc(sizeof(*new_o), GFP_KERNEL);
437 if (!new_o)
438 return -ENOMEM;
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 down_write(&_origins_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000442 r = __validate_exception_handover(snap);
443 if (r < 0) {
444 kfree(new_o);
445 goto out;
446 }
447
448 o = __lookup_origin(bdev);
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000449 if (o)
450 kfree(new_o);
451 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 /* New origin */
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000453 o = new_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 /* Initialise the struct */
456 INIT_LIST_HEAD(&o->snapshots);
457 o->bdev = bdev;
458
459 __insert_origin(o);
460 }
461
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000462 __insert_snapshot(o, snap);
463
464out:
465 up_write(&_origins_lock);
466
467 return r;
468}
469
470/*
471 * Move snapshot to correct place in list according to chunk size.
472 */
473static void reregister_snapshot(struct dm_snapshot *s)
474{
475 struct block_device *bdev = s->origin->bdev;
476
477 down_write(&_origins_lock);
478
479 list_del(&s->list);
480 __insert_snapshot(__lookup_origin(bdev), s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 up_write(&_origins_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
485static void unregister_snapshot(struct dm_snapshot *s)
486{
487 struct origin *o;
488
489 down_write(&_origins_lock);
490 o = __lookup_origin(s->origin->bdev);
491
492 list_del(&s->list);
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000493 if (o && list_empty(&o->snapshots)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 list_del(&o->hash_list);
495 kfree(o);
496 }
497
498 up_write(&_origins_lock);
499}
500
501/*
502 * Implementation of the exception hash tables.
Milan Brozd74f81f2008-02-08 02:11:27 +0000503 * The lowest hash_shift bits of the chunk number are ignored, allowing
504 * some consecutive chunks to be grouped together.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 */
Jon Brassow3510cb92009-12-10 23:52:11 +0000506static int dm_exception_table_init(struct dm_exception_table *et,
507 uint32_t size, unsigned hash_shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 unsigned int i;
510
Milan Brozd74f81f2008-02-08 02:11:27 +0000511 et->hash_shift = hash_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 et->hash_mask = size - 1;
513 et->table = dm_vcalloc(size, sizeof(struct list_head));
514 if (!et->table)
515 return -ENOMEM;
516
517 for (i = 0; i < size; i++)
518 INIT_LIST_HEAD(et->table + i);
519
520 return 0;
521}
522
Jon Brassow3510cb92009-12-10 23:52:11 +0000523static void dm_exception_table_exit(struct dm_exception_table *et,
524 struct kmem_cache *mem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
526 struct list_head *slot;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000527 struct dm_exception *ex, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 int i, size;
529
530 size = et->hash_mask + 1;
531 for (i = 0; i < size; i++) {
532 slot = et->table + i;
533
534 list_for_each_entry_safe (ex, next, slot, hash_list)
535 kmem_cache_free(mem, ex);
536 }
537
538 vfree(et->table);
539}
540
Jon Brassow191437a2009-12-10 23:52:10 +0000541static uint32_t exception_hash(struct dm_exception_table *et, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Milan Brozd74f81f2008-02-08 02:11:27 +0000543 return (chunk >> et->hash_shift) & et->hash_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
Jon Brassow3510cb92009-12-10 23:52:11 +0000546static void dm_remove_exception(struct dm_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
548 list_del(&e->hash_list);
549}
550
551/*
552 * Return the exception data for a sector, or NULL if not
553 * remapped.
554 */
Jon Brassow3510cb92009-12-10 23:52:11 +0000555static struct dm_exception *dm_lookup_exception(struct dm_exception_table *et,
556 chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 struct list_head *slot;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000559 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 slot = &et->table[exception_hash(et, chunk)];
562 list_for_each_entry (e, slot, hash_list)
Milan Brozd74f81f2008-02-08 02:11:27 +0000563 if (chunk >= e->old_chunk &&
564 chunk <= e->old_chunk + dm_consecutive_chunk_count(e))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return e;
566
567 return NULL;
568}
569
Jon Brassow3510cb92009-12-10 23:52:11 +0000570static struct dm_exception *alloc_completed_exception(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Jon Brassow1d4989c2009-12-10 23:52:10 +0000572 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 e = kmem_cache_alloc(exception_cache, GFP_NOIO);
575 if (!e)
576 e = kmem_cache_alloc(exception_cache, GFP_ATOMIC);
577
578 return e;
579}
580
Jon Brassow3510cb92009-12-10 23:52:11 +0000581static void free_completed_exception(struct dm_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 kmem_cache_free(exception_cache, e);
584}
585
Mikulas Patocka92e86812008-07-21 12:00:35 +0100586static struct dm_snap_pending_exception *alloc_pending_exception(struct dm_snapshot *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Mikulas Patocka92e86812008-07-21 12:00:35 +0100588 struct dm_snap_pending_exception *pe = mempool_alloc(s->pending_pool,
589 GFP_NOIO);
590
Mikulas Patocka879129d22008-10-30 13:33:16 +0000591 atomic_inc(&s->pending_exceptions_count);
Mikulas Patocka92e86812008-07-21 12:00:35 +0100592 pe->snap = s;
593
594 return pe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100597static void free_pending_exception(struct dm_snap_pending_exception *pe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Mikulas Patocka879129d22008-10-30 13:33:16 +0000599 struct dm_snapshot *s = pe->snap;
600
601 mempool_free(pe, s->pending_pool);
602 smp_mb__before_atomic_dec();
603 atomic_dec(&s->pending_exceptions_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
Jon Brassow3510cb92009-12-10 23:52:11 +0000606static void dm_insert_exception(struct dm_exception_table *eh,
607 struct dm_exception *new_e)
Milan Brozd74f81f2008-02-08 02:11:27 +0000608{
Milan Brozd74f81f2008-02-08 02:11:27 +0000609 struct list_head *l;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000610 struct dm_exception *e = NULL;
Milan Brozd74f81f2008-02-08 02:11:27 +0000611
612 l = &eh->table[exception_hash(eh, new_e->old_chunk)];
613
614 /* Add immediately if this table doesn't support consecutive chunks */
615 if (!eh->hash_shift)
616 goto out;
617
618 /* List is ordered by old_chunk */
619 list_for_each_entry_reverse(e, l, hash_list) {
620 /* Insert after an existing chunk? */
621 if (new_e->old_chunk == (e->old_chunk +
622 dm_consecutive_chunk_count(e) + 1) &&
623 new_e->new_chunk == (dm_chunk_number(e->new_chunk) +
624 dm_consecutive_chunk_count(e) + 1)) {
625 dm_consecutive_chunk_count_inc(e);
Jon Brassow3510cb92009-12-10 23:52:11 +0000626 free_completed_exception(new_e);
Milan Brozd74f81f2008-02-08 02:11:27 +0000627 return;
628 }
629
630 /* Insert before an existing chunk? */
631 if (new_e->old_chunk == (e->old_chunk - 1) &&
632 new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) {
633 dm_consecutive_chunk_count_inc(e);
634 e->old_chunk--;
635 e->new_chunk--;
Jon Brassow3510cb92009-12-10 23:52:11 +0000636 free_completed_exception(new_e);
Milan Brozd74f81f2008-02-08 02:11:27 +0000637 return;
638 }
639
640 if (new_e->old_chunk > e->old_chunk)
641 break;
642 }
643
644out:
645 list_add(&new_e->hash_list, e ? &e->hash_list : l);
646}
647
Jonathan Brassowa159c1a2009-01-06 03:05:19 +0000648/*
649 * Callback used by the exception stores to load exceptions when
650 * initialising.
651 */
652static int dm_add_exception(void *context, chunk_t old, chunk_t new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Jonathan Brassowa159c1a2009-01-06 03:05:19 +0000654 struct dm_snapshot *s = context;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000655 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Jon Brassow3510cb92009-12-10 23:52:11 +0000657 e = alloc_completed_exception();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (!e)
659 return -ENOMEM;
660
661 e->old_chunk = old;
Milan Brozd74f81f2008-02-08 02:11:27 +0000662
663 /* Consecutive_count is implicitly initialised to zero */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 e->new_chunk = new;
Milan Brozd74f81f2008-02-08 02:11:27 +0000665
Jon Brassow3510cb92009-12-10 23:52:11 +0000666 dm_insert_exception(&s->complete, e);
Milan Brozd74f81f2008-02-08 02:11:27 +0000667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return 0;
669}
670
Mikulas Patocka7e201b32009-12-10 23:52:08 +0000671#define min_not_zero(l, r) (((l) == 0) ? (r) : (((r) == 0) ? (l) : min(l, r)))
672
673/*
674 * Return a minimum chunk size of all snapshots that have the specified origin.
675 * Return zero if the origin has no snapshots.
676 */
677static sector_t __minimum_chunk_size(struct origin *o)
678{
679 struct dm_snapshot *snap;
680 unsigned chunk_size = 0;
681
682 if (o)
683 list_for_each_entry(snap, &o->snapshots, list)
684 chunk_size = min_not_zero(chunk_size,
685 snap->store->chunk_size);
686
687 return chunk_size;
688}
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690/*
691 * Hard coded magic.
692 */
693static int calc_max_buckets(void)
694{
695 /* use a fixed size of 2MB */
696 unsigned long mem = 2 * 1024 * 1024;
697 mem /= sizeof(struct list_head);
698
699 return mem;
700}
701
702/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 * Allocate room for a suitable hash table.
704 */
Jonathan Brassowfee19982009-04-02 19:55:34 +0100705static int init_hash_tables(struct dm_snapshot *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
707 sector_t hash_size, cow_dev_size, origin_dev_size, max_buckets;
708
709 /*
710 * Calculate based on the size of the original volume or
711 * the COW volume...
712 */
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000713 cow_dev_size = get_dev_size(s->cow->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 origin_dev_size = get_dev_size(s->origin->bdev);
715 max_buckets = calc_max_buckets();
716
Jonathan Brassowfee19982009-04-02 19:55:34 +0100717 hash_size = min(origin_dev_size, cow_dev_size) >> s->store->chunk_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 hash_size = min(hash_size, max_buckets);
719
Mikulas Patocka8e87b9b2009-12-10 23:51:54 +0000720 if (hash_size < 64)
721 hash_size = 64;
Robert P. J. Day8defd832008-02-08 02:10:06 +0000722 hash_size = rounddown_pow_of_two(hash_size);
Jon Brassow3510cb92009-12-10 23:52:11 +0000723 if (dm_exception_table_init(&s->complete, hash_size,
724 DM_CHUNK_CONSECUTIVE_BITS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return -ENOMEM;
726
727 /*
728 * Allocate hash table for in-flight exceptions
729 * Make this smaller than the real hash table
730 */
731 hash_size >>= 3;
732 if (hash_size < 64)
733 hash_size = 64;
734
Jon Brassow3510cb92009-12-10 23:52:11 +0000735 if (dm_exception_table_init(&s->pending, hash_size, 0)) {
736 dm_exception_table_exit(&s->complete, exception_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return -ENOMEM;
738 }
739
740 return 0;
741}
742
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000743static void merge_shutdown(struct dm_snapshot *s)
744{
745 clear_bit_unlock(RUNNING_MERGE, &s->state_bits);
746 smp_mb__after_clear_bit();
747 wake_up_bit(&s->state_bits, RUNNING_MERGE);
748}
749
750/*
751 * Remove one chunk from the index of completed exceptions.
752 */
753static int __remove_single_exception_chunk(struct dm_snapshot *s,
754 chunk_t old_chunk)
755{
756 struct dm_exception *e;
757
758 /* FIXME: interlock writes to this chunk */
759
760 e = dm_lookup_exception(&s->complete, old_chunk);
761 if (!e) {
762 DMERR("Corruption detected: exception for block %llu is "
763 "on disk but not in memory",
764 (unsigned long long)old_chunk);
765 return -EINVAL;
766 }
767
768 /*
769 * If this is the only chunk using this exception, remove exception.
770 */
771 if (!dm_consecutive_chunk_count(e)) {
772 dm_remove_exception(e);
773 free_completed_exception(e);
774 return 0;
775 }
776
777 /*
778 * The chunk may be either at the beginning or the end of a
779 * group of consecutive chunks - never in the middle. We are
780 * removing chunks in the opposite order to that in which they
781 * were added, so this should always be true.
782 * Decrement the consecutive chunk counter and adjust the
783 * starting point if necessary.
784 */
785 if (old_chunk == e->old_chunk) {
786 e->old_chunk++;
787 e->new_chunk++;
788 } else if (old_chunk != e->old_chunk +
789 dm_consecutive_chunk_count(e)) {
790 DMERR("Attempt to merge block %llu from the "
791 "middle of a chunk range [%llu - %llu]",
792 (unsigned long long)old_chunk,
793 (unsigned long long)e->old_chunk,
794 (unsigned long long)
795 e->old_chunk + dm_consecutive_chunk_count(e));
796 return -EINVAL;
797 }
798
799 dm_consecutive_chunk_count_dec(e);
800
801 return 0;
802}
803
804static int remove_single_exception_chunk(struct dm_snapshot *s,
805 chunk_t old_chunk)
806{
807 int r = 0;
808
809 down_write(&s->lock);
810 r = __remove_single_exception_chunk(s, old_chunk);
811 up_write(&s->lock);
812
813 return r;
814}
815
816static void merge_callback(int read_err, unsigned long write_err,
817 void *context);
818
819static void snapshot_merge_next_chunks(struct dm_snapshot *s)
820{
821 int r;
822 chunk_t old_chunk, new_chunk;
823 struct dm_io_region src, dest;
824
825 BUG_ON(!test_bit(RUNNING_MERGE, &s->state_bits));
826 if (unlikely(test_bit(SHUTDOWN_MERGE, &s->state_bits)))
827 goto shut;
828
829 /*
830 * valid flag never changes during merge, so no lock required.
831 */
832 if (!s->valid) {
833 DMERR("Snapshot is invalid: can't merge");
834 goto shut;
835 }
836
837 r = s->store->type->prepare_merge(s->store, &old_chunk, &new_chunk);
838 if (r <= 0) {
839 if (r < 0)
840 DMERR("Read error in exception store: "
841 "shutting down merge");
842 goto shut;
843 }
844
845 /* TODO: use larger I/O size once we verify that kcopyd handles it */
846
847 if (remove_single_exception_chunk(s, old_chunk) < 0)
848 goto shut;
849
850 dest.bdev = s->origin->bdev;
851 dest.sector = chunk_to_sector(s->store, old_chunk);
852 dest.count = min((sector_t)s->store->chunk_size,
853 get_dev_size(dest.bdev) - dest.sector);
854
855 src.bdev = s->cow->bdev;
856 src.sector = chunk_to_sector(s->store, new_chunk);
857 src.count = dest.count;
858
859 dm_kcopyd_copy(s->kcopyd_client, &src, 1, &dest, 0, merge_callback, s);
860 return;
861
862shut:
863 merge_shutdown(s);
864}
865
866static void merge_callback(int read_err, unsigned long write_err, void *context)
867{
868 struct dm_snapshot *s = context;
869
870 if (read_err || write_err) {
871 if (read_err)
872 DMERR("Read error: shutting down merge.");
873 else
874 DMERR("Write error: shutting down merge.");
875 goto shut;
876 }
877
878 if (s->store->type->commit_merge(s->store, 1) < 0) {
879 DMERR("Write error in exception store: shutting down merge");
880 goto shut;
881 }
882
883 snapshot_merge_next_chunks(s);
884
885 return;
886
887shut:
888 merge_shutdown(s);
889}
890
891static void start_merge(struct dm_snapshot *s)
892{
893 if (!test_and_set_bit(RUNNING_MERGE, &s->state_bits))
894 snapshot_merge_next_chunks(s);
895}
896
897static int wait_schedule(void *ptr)
898{
899 schedule();
900
901 return 0;
902}
903
904/*
905 * Stop the merging process and wait until it finishes.
906 */
907static void stop_merge(struct dm_snapshot *s)
908{
909 set_bit(SHUTDOWN_MERGE, &s->state_bits);
910 wait_on_bit(&s->state_bits, RUNNING_MERGE, wait_schedule,
911 TASK_UNINTERRUPTIBLE);
912 clear_bit(SHUTDOWN_MERGE, &s->state_bits);
913}
914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 * Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size>
917 */
918static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
919{
920 struct dm_snapshot *s;
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100921 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 int r = -EINVAL;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000923 char *origin_path, *cow_path;
Mike Snitzer10b81062009-12-10 23:52:31 +0000924 unsigned args_used, num_flush_requests = 1;
925 fmode_t origin_mode = FMODE_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -0700927 if (argc != 4) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700928 ti->error = "requires exactly 4 arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 r = -EINVAL;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000930 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932
Mike Snitzer10b81062009-12-10 23:52:31 +0000933 if (dm_target_is_snapshot_merge(ti)) {
934 num_flush_requests = 2;
935 origin_mode = FMODE_WRITE;
936 }
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 origin_path = argv[0];
Jonathan Brassowfee19982009-04-02 19:55:34 +0100939 argv++;
940 argc--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 s = kmalloc(sizeof(*s), GFP_KERNEL);
Jonathan Brassowfee19982009-04-02 19:55:34 +0100943 if (!s) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 ti->error = "Cannot allocate snapshot context private "
945 "structure";
946 r = -ENOMEM;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000947 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
949
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000950 cow_path = argv[0];
951 argv++;
952 argc--;
953
954 r = dm_get_device(ti, cow_path, 0, 0,
955 FMODE_READ | FMODE_WRITE, &s->cow);
956 if (r) {
957 ti->error = "Cannot get COW device";
958 goto bad_cow;
959 }
960
961 r = dm_exception_store_create(ti, argc, argv, s, &args_used, &s->store);
962 if (r) {
963 ti->error = "Couldn't create exception store";
964 r = -EINVAL;
965 goto bad_store;
966 }
967
968 argv += args_used;
969 argc -= args_used;
970
Mike Snitzer10b81062009-12-10 23:52:31 +0000971 r = dm_get_device(ti, origin_path, 0, ti->len, origin_mode, &s->origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (r) {
973 ti->error = "Cannot get origin device";
Jonathan Brassowfee19982009-04-02 19:55:34 +0100974 goto bad_origin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
976
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000977 s->ti = ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 s->valid = 1;
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -0800979 s->active = 0;
Mike Snitzerc26655c2009-12-10 23:52:12 +0000980 s->suspended = 0;
Mikulas Patocka879129d22008-10-30 13:33:16 +0000981 atomic_set(&s->pending_exceptions_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 init_rwsem(&s->lock);
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000983 INIT_LIST_HEAD(&s->list);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -0700984 spin_lock_init(&s->pe_lock);
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000985 s->state_bits = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 /* Allocate hash table for COW data */
Jonathan Brassowfee19982009-04-02 19:55:34 +0100988 if (init_hash_tables(s)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 ti->error = "Unable to allocate hash table space";
990 r = -ENOMEM;
Jonathan Brassowfee19982009-04-02 19:55:34 +0100991 goto bad_hash_tables;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
993
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +0100994 r = dm_kcopyd_client_create(SNAPSHOT_PAGES, &s->kcopyd_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (r) {
996 ti->error = "Could not create kcopyd client";
Jonathan Brassowfee19982009-04-02 19:55:34 +0100997 goto bad_kcopyd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
999
Mikulas Patocka92e86812008-07-21 12:00:35 +01001000 s->pending_pool = mempool_create_slab_pool(MIN_IOS, pending_cache);
1001 if (!s->pending_pool) {
1002 ti->error = "Could not allocate mempool for pending exceptions";
Jonathan Brassowfee19982009-04-02 19:55:34 +01001003 goto bad_pending_pool;
Mikulas Patocka92e86812008-07-21 12:00:35 +01001004 }
1005
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001006 s->tracked_chunk_pool = mempool_create_slab_pool(MIN_IOS,
1007 tracked_chunk_cache);
1008 if (!s->tracked_chunk_pool) {
1009 ti->error = "Could not allocate tracked_chunk mempool for "
1010 "tracking reads";
Mikulas Patocka92e86812008-07-21 12:00:35 +01001011 goto bad_tracked_chunk_pool;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001012 }
1013
1014 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
1015 INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]);
1016
1017 spin_lock_init(&s->tracked_chunk_lock);
1018
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001019 bio_list_init(&s->queued_bios);
1020 INIT_WORK(&s->queued_bios_work, flush_queued_bios);
1021
1022 ti->private = s;
Mike Snitzer10b81062009-12-10 23:52:31 +00001023 ti->num_flush_requests = num_flush_requests;
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001024
1025 /* Add snapshot to the list of snapshots for this origin */
1026 /* Exceptions aren't triggered till snapshot_resume() is called */
1027 r = register_snapshot(s);
1028 if (r == -ENOMEM) {
1029 ti->error = "Snapshot origin struct allocation failed";
1030 goto bad_load_and_register;
1031 } else if (r < 0) {
1032 /* invalid handover, register_snapshot has set ti->error */
1033 goto bad_load_and_register;
1034 }
1035
1036 /*
1037 * Metadata must only be loaded into one table at once, so skip this
1038 * if metadata will be handed over during resume.
1039 * Chunk size will be set during the handover - set it to zero to
1040 * ensure it's ignored.
1041 */
1042 if (r > 0) {
1043 s->store->chunk_size = 0;
1044 return 0;
1045 }
1046
Jonathan Brassow493df712009-04-02 19:55:31 +01001047 r = s->store->type->read_metadata(s->store, dm_add_exception,
1048 (void *)s);
Milan Broz07641472007-07-12 17:28:13 +01001049 if (r < 0) {
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -07001050 ti->error = "Failed to read snapshot metadata";
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001051 goto bad_read_metadata;
Milan Broz07641472007-07-12 17:28:13 +01001052 } else if (r > 0) {
1053 s->valid = 0;
1054 DMWARN("Snapshot is marked invalid.");
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -07001055 }
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001056
Mikulas Patocka3f2412d2009-10-16 23:18:16 +01001057 if (!s->store->chunk_size) {
1058 ti->error = "Chunk size not set";
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001059 goto bad_read_metadata;
Mikulas Patocka3f2412d2009-10-16 23:18:16 +01001060 }
Jonathan Brassowd0216842009-04-02 19:55:32 +01001061 ti->split_io = s->store->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 return 0;
1064
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001065bad_read_metadata:
1066 unregister_snapshot(s);
1067
Jonathan Brassowfee19982009-04-02 19:55:34 +01001068bad_load_and_register:
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001069 mempool_destroy(s->tracked_chunk_pool);
1070
Jonathan Brassowfee19982009-04-02 19:55:34 +01001071bad_tracked_chunk_pool:
Mikulas Patocka92e86812008-07-21 12:00:35 +01001072 mempool_destroy(s->pending_pool);
1073
Jonathan Brassowfee19982009-04-02 19:55:34 +01001074bad_pending_pool:
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001075 dm_kcopyd_client_destroy(s->kcopyd_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Jonathan Brassowfee19982009-04-02 19:55:34 +01001077bad_kcopyd:
Jon Brassow3510cb92009-12-10 23:52:11 +00001078 dm_exception_table_exit(&s->pending, pending_cache);
1079 dm_exception_table_exit(&s->complete, exception_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Jonathan Brassowfee19982009-04-02 19:55:34 +01001081bad_hash_tables:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 dm_put_device(ti, s->origin);
1083
Jonathan Brassowfee19982009-04-02 19:55:34 +01001084bad_origin:
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001085 dm_exception_store_destroy(s->store);
1086
1087bad_store:
1088 dm_put_device(ti, s->cow);
1089
1090bad_cow:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 kfree(s);
1092
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001093bad:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 return r;
1095}
1096
Milan Broz31c93a02006-12-08 02:41:11 -08001097static void __free_exceptions(struct dm_snapshot *s)
1098{
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001099 dm_kcopyd_client_destroy(s->kcopyd_client);
Milan Broz31c93a02006-12-08 02:41:11 -08001100 s->kcopyd_client = NULL;
1101
Jon Brassow3510cb92009-12-10 23:52:11 +00001102 dm_exception_table_exit(&s->pending, pending_cache);
1103 dm_exception_table_exit(&s->complete, exception_cache);
Milan Broz31c93a02006-12-08 02:41:11 -08001104}
1105
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001106static void __handover_exceptions(struct dm_snapshot *snap_src,
1107 struct dm_snapshot *snap_dest)
1108{
1109 union {
1110 struct dm_exception_table table_swap;
1111 struct dm_exception_store *store_swap;
1112 } u;
1113
1114 /*
1115 * Swap all snapshot context information between the two instances.
1116 */
1117 u.table_swap = snap_dest->complete;
1118 snap_dest->complete = snap_src->complete;
1119 snap_src->complete = u.table_swap;
1120
1121 u.store_swap = snap_dest->store;
1122 snap_dest->store = snap_src->store;
1123 snap_src->store = u.store_swap;
1124
1125 snap_dest->store->snap = snap_dest;
1126 snap_src->store->snap = snap_src;
1127
1128 snap_dest->ti->split_io = snap_dest->store->chunk_size;
1129 snap_dest->valid = snap_src->valid;
1130
1131 /*
1132 * Set source invalid to ensure it receives no further I/O.
1133 */
1134 snap_src->valid = 0;
1135}
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137static void snapshot_dtr(struct dm_target *ti)
1138{
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001139#ifdef CONFIG_DM_DEBUG
1140 int i;
1141#endif
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001142 struct dm_snapshot *s = ti->private;
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001143 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001145 flush_workqueue(ksnapd);
1146
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001147 down_read(&_origins_lock);
1148 /* Check whether exception handover must be cancelled */
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +00001149 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001150 if (snap_src && snap_dest && (s == snap_src)) {
1151 down_write(&snap_dest->lock);
1152 snap_dest->valid = 0;
1153 up_write(&snap_dest->lock);
1154 DMERR("Cancelling snapshot handover.");
1155 }
1156 up_read(&_origins_lock);
1157
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001158 if (dm_target_is_snapshot_merge(ti))
1159 stop_merge(s);
1160
Alasdair G Kergon138728dc2006-03-27 01:17:50 -08001161 /* Prevent further origin writes from using this snapshot. */
1162 /* After this returns there can be no new kcopyd jobs. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 unregister_snapshot(s);
1164
Mikulas Patocka879129d22008-10-30 13:33:16 +00001165 while (atomic_read(&s->pending_exceptions_count))
Mikulas Patocka90fa1522009-01-06 03:04:54 +00001166 msleep(1);
Mikulas Patocka879129d22008-10-30 13:33:16 +00001167 /*
1168 * Ensure instructions in mempool_destroy aren't reordered
1169 * before atomic_read.
1170 */
1171 smp_mb();
1172
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001173#ifdef CONFIG_DM_DEBUG
1174 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
1175 BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i]));
1176#endif
1177
1178 mempool_destroy(s->tracked_chunk_pool);
1179
Milan Broz31c93a02006-12-08 02:41:11 -08001180 __free_exceptions(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Mikulas Patocka92e86812008-07-21 12:00:35 +01001182 mempool_destroy(s->pending_pool);
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 dm_put_device(ti, s->origin);
Jonathan Brassowfee19982009-04-02 19:55:34 +01001185
1186 dm_exception_store_destroy(s->store);
Alasdair G Kergon138728dc2006-03-27 01:17:50 -08001187
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001188 dm_put_device(ti, s->cow);
1189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 kfree(s);
1191}
1192
1193/*
1194 * Flush a list of buffers.
1195 */
1196static void flush_bios(struct bio *bio)
1197{
1198 struct bio *n;
1199
1200 while (bio) {
1201 n = bio->bi_next;
1202 bio->bi_next = NULL;
1203 generic_make_request(bio);
1204 bio = n;
1205 }
1206}
1207
David Howellsc4028952006-11-22 14:57:56 +00001208static void flush_queued_bios(struct work_struct *work)
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001209{
David Howellsc4028952006-11-22 14:57:56 +00001210 struct dm_snapshot *s =
1211 container_of(work, struct dm_snapshot, queued_bios_work);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001212 struct bio *queued_bios;
1213 unsigned long flags;
1214
1215 spin_lock_irqsave(&s->pe_lock, flags);
1216 queued_bios = bio_list_get(&s->queued_bios);
1217 spin_unlock_irqrestore(&s->pe_lock, flags);
1218
1219 flush_bios(queued_bios);
1220}
1221
Mikulas Patocka515ad662009-12-10 23:52:30 +00001222static int do_origin(struct dm_dev *origin, struct bio *bio);
1223
1224/*
1225 * Flush a list of buffers.
1226 */
1227static void retry_origin_bios(struct dm_snapshot *s, struct bio *bio)
1228{
1229 struct bio *n;
1230 int r;
1231
1232 while (bio) {
1233 n = bio->bi_next;
1234 bio->bi_next = NULL;
1235 r = do_origin(s->origin, bio);
1236 if (r == DM_MAPIO_REMAPPED)
1237 generic_make_request(bio);
1238 bio = n;
1239 }
1240}
1241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242/*
1243 * Error a list of buffers.
1244 */
1245static void error_bios(struct bio *bio)
1246{
1247 struct bio *n;
1248
1249 while (bio) {
1250 n = bio->bi_next;
1251 bio->bi_next = NULL;
NeilBrown6712ecf2007-09-27 12:47:43 +02001252 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 bio = n;
1254 }
1255}
1256
Alasdair G Kergon695368a2006-10-03 01:15:31 -07001257static void __invalidate_snapshot(struct dm_snapshot *s, int err)
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001258{
1259 if (!s->valid)
1260 return;
1261
1262 if (err == -EIO)
1263 DMERR("Invalidating snapshot: Error reading/writing.");
1264 else if (err == -ENOMEM)
1265 DMERR("Invalidating snapshot: Unable to allocate exception.");
1266
Jonathan Brassow493df712009-04-02 19:55:31 +01001267 if (s->store->type->drop_snapshot)
1268 s->store->type->drop_snapshot(s->store);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001269
1270 s->valid = 0;
1271
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001272 dm_table_event(s->ti->table);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001273}
1274
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001275static void pending_complete(struct dm_snap_pending_exception *pe, int success)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
Jon Brassow1d4989c2009-12-10 23:52:10 +00001277 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 struct dm_snapshot *s = pe->snap;
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001279 struct bio *origin_bios = NULL;
1280 struct bio *snapshot_bios = NULL;
1281 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001283 if (!success) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 /* Read/write error - snapshot is unusable */
1285 down_write(&s->lock);
Alasdair G Kergon695368a2006-10-03 01:15:31 -07001286 __invalidate_snapshot(s, -EIO);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001287 error = 1;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001288 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
1290
Jon Brassow3510cb92009-12-10 23:52:11 +00001291 e = alloc_completed_exception();
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001292 if (!e) {
1293 down_write(&s->lock);
Alasdair G Kergon695368a2006-10-03 01:15:31 -07001294 __invalidate_snapshot(s, -ENOMEM);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001295 error = 1;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001296 goto out;
1297 }
1298 *e = pe->e;
1299
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001300 down_write(&s->lock);
1301 if (!s->valid) {
Jon Brassow3510cb92009-12-10 23:52:11 +00001302 free_completed_exception(e);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001303 error = 1;
1304 goto out;
1305 }
1306
Mike Snitzer615d1eb2009-12-10 23:52:29 +00001307 /* Check for conflicting reads */
1308 __check_for_conflicting_io(s, pe->e.old_chunk);
Mikulas Patockaa8d41b52008-07-21 12:00:34 +01001309
1310 /*
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001311 * Add a proper exception, and remove the
1312 * in-flight exception from the list.
1313 */
Jon Brassow3510cb92009-12-10 23:52:11 +00001314 dm_insert_exception(&s->complete, e);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 out:
Jon Brassow3510cb92009-12-10 23:52:11 +00001317 dm_remove_exception(&pe->e);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001318 snapshot_bios = bio_list_get(&pe->snapshot_bios);
Mikulas Patocka515ad662009-12-10 23:52:30 +00001319 origin_bios = bio_list_get(&pe->origin_bios);
1320 free_pending_exception(pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001322 up_write(&s->lock);
1323
1324 /* Submit any pending write bios */
1325 if (error)
1326 error_bios(snapshot_bios);
1327 else
1328 flush_bios(snapshot_bios);
1329
Mikulas Patocka515ad662009-12-10 23:52:30 +00001330 retry_origin_bios(s, origin_bios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331}
1332
1333static void commit_callback(void *context, int success)
1334{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001335 struct dm_snap_pending_exception *pe = context;
1336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 pending_complete(pe, success);
1338}
1339
1340/*
1341 * Called when the copy I/O has finished. kcopyd actually runs
1342 * this code so don't block.
1343 */
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -07001344static void copy_callback(int read_err, unsigned long write_err, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001346 struct dm_snap_pending_exception *pe = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 struct dm_snapshot *s = pe->snap;
1348
1349 if (read_err || write_err)
1350 pending_complete(pe, 0);
1351
1352 else
1353 /* Update the metadata if we are persistent */
Jonathan Brassow493df712009-04-02 19:55:31 +01001354 s->store->type->commit_exception(s->store, &pe->e,
1355 commit_callback, pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356}
1357
1358/*
1359 * Dispatches the copy operation to kcopyd.
1360 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001361static void start_copy(struct dm_snap_pending_exception *pe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
1363 struct dm_snapshot *s = pe->snap;
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +01001364 struct dm_io_region src, dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 struct block_device *bdev = s->origin->bdev;
1366 sector_t dev_size;
1367
1368 dev_size = get_dev_size(bdev);
1369
1370 src.bdev = bdev;
Jonathan Brassow71fab002009-04-02 19:55:33 +01001371 src.sector = chunk_to_sector(s->store, pe->e.old_chunk);
Mikulas Patockadf96eee2009-10-16 23:18:17 +01001372 src.count = min((sector_t)s->store->chunk_size, dev_size - src.sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001374 dest.bdev = s->cow->bdev;
Jonathan Brassow71fab002009-04-02 19:55:33 +01001375 dest.sector = chunk_to_sector(s->store, pe->e.new_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 dest.count = src.count;
1377
1378 /* Hand over to kcopyd */
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001379 dm_kcopyd_copy(s->kcopyd_client,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 &src, 1, &dest, 0, copy_callback, pe);
1381}
1382
Mikulas Patocka29138082009-04-02 19:55:25 +01001383static struct dm_snap_pending_exception *
1384__lookup_pending_exception(struct dm_snapshot *s, chunk_t chunk)
1385{
Jon Brassow3510cb92009-12-10 23:52:11 +00001386 struct dm_exception *e = dm_lookup_exception(&s->pending, chunk);
Mikulas Patocka29138082009-04-02 19:55:25 +01001387
1388 if (!e)
1389 return NULL;
1390
1391 return container_of(e, struct dm_snap_pending_exception, e);
1392}
1393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394/*
1395 * Looks to see if this snapshot already has a pending exception
1396 * for this chunk, otherwise it allocates a new one and inserts
1397 * it into the pending table.
1398 *
1399 * NOTE: a write lock must be held on snap->lock before calling
1400 * this.
1401 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001402static struct dm_snap_pending_exception *
Mikulas Patockac6621392009-04-02 19:55:25 +01001403__find_pending_exception(struct dm_snapshot *s,
1404 struct dm_snap_pending_exception *pe, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
Mikulas Patockac6621392009-04-02 19:55:25 +01001406 struct dm_snap_pending_exception *pe2;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001407
Mikulas Patocka29138082009-04-02 19:55:25 +01001408 pe2 = __lookup_pending_exception(s, chunk);
1409 if (pe2) {
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001410 free_pending_exception(pe);
Mikulas Patocka29138082009-04-02 19:55:25 +01001411 return pe2;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001412 }
1413
1414 pe->e.old_chunk = chunk;
1415 bio_list_init(&pe->origin_bios);
1416 bio_list_init(&pe->snapshot_bios);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001417 pe->started = 0;
1418
Jonathan Brassow493df712009-04-02 19:55:31 +01001419 if (s->store->type->prepare_exception(s->store, &pe->e)) {
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001420 free_pending_exception(pe);
1421 return NULL;
1422 }
1423
Jon Brassow3510cb92009-12-10 23:52:11 +00001424 dm_insert_exception(&s->pending, &pe->e);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 return pe;
1427}
1428
Jon Brassow1d4989c2009-12-10 23:52:10 +00001429static void remap_exception(struct dm_snapshot *s, struct dm_exception *e,
Milan Brozd74f81f2008-02-08 02:11:27 +00001430 struct bio *bio, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001432 bio->bi_bdev = s->cow->bdev;
Jonathan Brassow71fab002009-04-02 19:55:33 +01001433 bio->bi_sector = chunk_to_sector(s->store,
1434 dm_chunk_number(e->new_chunk) +
1435 (chunk - e->old_chunk)) +
1436 (bio->bi_sector &
1437 s->store->chunk_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438}
1439
1440static int snapshot_map(struct dm_target *ti, struct bio *bio,
1441 union map_info *map_context)
1442{
Jon Brassow1d4989c2009-12-10 23:52:10 +00001443 struct dm_exception *e;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001444 struct dm_snapshot *s = ti->private;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001445 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 chunk_t chunk;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001447 struct dm_snap_pending_exception *pe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01001449 if (unlikely(bio_empty_barrier(bio))) {
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001450 bio->bi_bdev = s->cow->bdev;
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01001451 return DM_MAPIO_REMAPPED;
1452 }
1453
Jonathan Brassow71fab002009-04-02 19:55:33 +01001454 chunk = sector_to_chunk(s->store, bio->bi_sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456 /* Full snapshots are not usable */
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001457 /* To get here the table must be live so s->active is always set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 if (!s->valid)
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -07001459 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001461 /* FIXME: should only take write lock if we need
1462 * to copy an exception */
1463 down_write(&s->lock);
1464
1465 if (!s->valid) {
1466 r = -EIO;
1467 goto out_unlock;
1468 }
1469
1470 /* If the block is already remapped - use that, else remap it */
Jon Brassow3510cb92009-12-10 23:52:11 +00001471 e = dm_lookup_exception(&s->complete, chunk);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001472 if (e) {
Milan Brozd74f81f2008-02-08 02:11:27 +00001473 remap_exception(s, e, bio, chunk);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001474 goto out_unlock;
1475 }
1476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 /*
1478 * Write to snapshot - higher level takes care of RW/RO
1479 * flags so we should only get this if we are
1480 * writeable.
1481 */
1482 if (bio_rw(bio) == WRITE) {
Mikulas Patocka29138082009-04-02 19:55:25 +01001483 pe = __lookup_pending_exception(s, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001484 if (!pe) {
Mikulas Patockac6621392009-04-02 19:55:25 +01001485 up_write(&s->lock);
1486 pe = alloc_pending_exception(s);
1487 down_write(&s->lock);
1488
1489 if (!s->valid) {
1490 free_pending_exception(pe);
1491 r = -EIO;
1492 goto out_unlock;
1493 }
1494
Jon Brassow3510cb92009-12-10 23:52:11 +00001495 e = dm_lookup_exception(&s->complete, chunk);
Mikulas Patocka35bf6592009-04-02 19:55:26 +01001496 if (e) {
1497 free_pending_exception(pe);
1498 remap_exception(s, e, bio, chunk);
1499 goto out_unlock;
1500 }
1501
Mikulas Patockac6621392009-04-02 19:55:25 +01001502 pe = __find_pending_exception(s, pe, chunk);
Mikulas Patocka29138082009-04-02 19:55:25 +01001503 if (!pe) {
1504 __invalidate_snapshot(s, -ENOMEM);
1505 r = -EIO;
1506 goto out_unlock;
1507 }
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001508 }
1509
Milan Brozd74f81f2008-02-08 02:11:27 +00001510 remap_exception(s, &pe->e, bio, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001511 bio_list_add(&pe->snapshot_bios, bio);
1512
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001513 r = DM_MAPIO_SUBMITTED;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001514
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001515 if (!pe->started) {
1516 /* this is protected by snap->lock */
1517 pe->started = 1;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001518 up_write(&s->lock);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001519 start_copy(pe);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001520 goto out;
1521 }
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001522 } else {
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001523 bio->bi_bdev = s->origin->bdev;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001524 map_context->ptr = track_chunk(s, chunk);
1525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001527 out_unlock:
1528 up_write(&s->lock);
1529 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 return r;
1531}
1532
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001533/*
1534 * A snapshot-merge target behaves like a combination of a snapshot
1535 * target and a snapshot-origin target. It only generates new
1536 * exceptions in other snapshots and not in the one that is being
1537 * merged.
1538 *
1539 * For each chunk, if there is an existing exception, it is used to
1540 * redirect I/O to the cow device. Otherwise I/O is sent to the origin,
1541 * which in turn might generate exceptions in other snapshots.
1542 */
1543static int snapshot_merge_map(struct dm_target *ti, struct bio *bio,
1544 union map_info *map_context)
1545{
1546 struct dm_exception *e;
1547 struct dm_snapshot *s = ti->private;
1548 int r = DM_MAPIO_REMAPPED;
1549 chunk_t chunk;
1550
Mike Snitzer10b81062009-12-10 23:52:31 +00001551 if (unlikely(bio_empty_barrier(bio))) {
1552 if (!map_context->flush_request)
1553 bio->bi_bdev = s->origin->bdev;
1554 else
1555 bio->bi_bdev = s->cow->bdev;
1556 map_context->ptr = NULL;
1557 return DM_MAPIO_REMAPPED;
1558 }
1559
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001560 chunk = sector_to_chunk(s->store, bio->bi_sector);
1561
1562 down_read(&s->lock);
1563
1564 /* Full snapshots are not usable */
1565 if (!s->valid) {
1566 r = -EIO;
1567 goto out_unlock;
1568 }
1569
1570 /* If the block is already remapped - use that */
1571 e = dm_lookup_exception(&s->complete, chunk);
1572 if (e) {
1573 remap_exception(s, e, bio, chunk);
1574 goto out_unlock;
1575 }
1576
1577 bio->bi_bdev = s->origin->bdev;
1578
1579 if (bio_rw(bio) == WRITE) {
1580 up_read(&s->lock);
1581 return do_origin(s->origin, bio);
1582 }
1583
1584out_unlock:
1585 up_read(&s->lock);
1586
1587 return r;
1588}
1589
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001590static int snapshot_end_io(struct dm_target *ti, struct bio *bio,
1591 int error, union map_info *map_context)
1592{
1593 struct dm_snapshot *s = ti->private;
1594 struct dm_snap_tracked_chunk *c = map_context->ptr;
1595
1596 if (c)
1597 stop_tracking_chunk(s, c);
1598
1599 return 0;
1600}
1601
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001602static void snapshot_merge_presuspend(struct dm_target *ti)
1603{
1604 struct dm_snapshot *s = ti->private;
1605
1606 stop_merge(s);
1607}
1608
Mike Snitzerc26655c2009-12-10 23:52:12 +00001609static void snapshot_postsuspend(struct dm_target *ti)
1610{
1611 struct dm_snapshot *s = ti->private;
1612
1613 down_write(&s->lock);
1614 s->suspended = 1;
1615 up_write(&s->lock);
1616}
1617
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001618static int snapshot_preresume(struct dm_target *ti)
1619{
1620 int r = 0;
1621 struct dm_snapshot *s = ti->private;
1622 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
1623
1624 down_read(&_origins_lock);
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +00001625 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001626 if (snap_src && snap_dest) {
1627 down_read(&snap_src->lock);
1628 if (s == snap_src) {
1629 DMERR("Unable to resume snapshot source until "
1630 "handover completes.");
1631 r = -EINVAL;
1632 } else if (!snap_src->suspended) {
1633 DMERR("Unable to perform snapshot handover until "
1634 "source is suspended.");
1635 r = -EINVAL;
1636 }
1637 up_read(&snap_src->lock);
1638 }
1639 up_read(&_origins_lock);
1640
1641 return r;
1642}
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644static void snapshot_resume(struct dm_target *ti)
1645{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001646 struct dm_snapshot *s = ti->private;
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001647 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
1648
1649 down_read(&_origins_lock);
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +00001650 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001651 if (snap_src && snap_dest) {
1652 down_write(&snap_src->lock);
1653 down_write_nested(&snap_dest->lock, SINGLE_DEPTH_NESTING);
1654 __handover_exceptions(snap_src, snap_dest);
1655 up_write(&snap_dest->lock);
1656 up_write(&snap_src->lock);
1657 }
1658 up_read(&_origins_lock);
1659
1660 /* Now we have correct chunk size, reregister */
1661 reregister_snapshot(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001663 down_write(&s->lock);
1664 s->active = 1;
Mike Snitzerc26655c2009-12-10 23:52:12 +00001665 s->suspended = 0;
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001666 up_write(&s->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667}
1668
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001669static sector_t get_origin_minimum_chunksize(struct block_device *bdev)
1670{
1671 sector_t min_chunksize;
1672
1673 down_read(&_origins_lock);
1674 min_chunksize = __minimum_chunk_size(__lookup_origin(bdev));
1675 up_read(&_origins_lock);
1676
1677 return min_chunksize;
1678}
1679
1680static void snapshot_merge_resume(struct dm_target *ti)
1681{
1682 struct dm_snapshot *s = ti->private;
1683
1684 /*
1685 * Handover exceptions from existing snapshot.
1686 */
1687 snapshot_resume(ti);
1688
1689 /*
1690 * snapshot-merge acts as an origin, so set ti->split_io
1691 */
1692 ti->split_io = get_origin_minimum_chunksize(s->origin->bdev);
1693
1694 start_merge(s);
1695}
1696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697static int snapshot_status(struct dm_target *ti, status_type_t type,
1698 char *result, unsigned int maxlen)
1699{
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01001700 unsigned sz = 0;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001701 struct dm_snapshot *snap = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
1703 switch (type) {
1704 case STATUSTYPE_INFO:
Mikulas Patocka94e765722009-12-10 23:51:53 +00001705
1706 down_write(&snap->lock);
1707
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 if (!snap->valid)
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01001709 DMEMIT("Invalid");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 else {
Mike Snitzer985903b2009-12-10 23:52:11 +00001711 if (snap->store->type->usage) {
1712 sector_t total_sectors, sectors_allocated,
1713 metadata_sectors;
1714 snap->store->type->usage(snap->store,
1715 &total_sectors,
1716 &sectors_allocated,
1717 &metadata_sectors);
1718 DMEMIT("%llu/%llu %llu",
1719 (unsigned long long)sectors_allocated,
1720 (unsigned long long)total_sectors,
1721 (unsigned long long)metadata_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 }
1723 else
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01001724 DMEMIT("Unknown");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 }
Mikulas Patocka94e765722009-12-10 23:51:53 +00001726
1727 up_write(&snap->lock);
1728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 break;
1730
1731 case STATUSTYPE_TABLE:
1732 /*
1733 * kdevname returns a static pointer so we need
1734 * to make private copies if the output is to
1735 * make sense.
1736 */
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001737 DMEMIT("%s %s", snap->origin->name, snap->cow->name);
Jonathan Brassow1e302a92009-04-02 19:55:35 +01001738 snap->store->type->status(snap->store, type, result + sz,
1739 maxlen - sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 break;
1741 }
1742
1743 return 0;
1744}
1745
Mike Snitzer8811f462009-09-04 20:40:19 +01001746static int snapshot_iterate_devices(struct dm_target *ti,
1747 iterate_devices_callout_fn fn, void *data)
1748{
1749 struct dm_snapshot *snap = ti->private;
1750
1751 return fn(ti, snap->origin, 0, ti->len, data);
1752}
1753
1754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755/*-----------------------------------------------------------------
1756 * Origin methods
1757 *---------------------------------------------------------------*/
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00001758
1759/*
1760 * If no exceptions need creating, DM_MAPIO_REMAPPED is returned and any
1761 * supplied bio was ignored. The caller may submit it immediately.
1762 * (No remapping actually occurs as the origin is always a direct linear
1763 * map.)
1764 *
1765 * If further exceptions are required, DM_MAPIO_SUBMITTED is returned
1766 * and any supplied bio is added to a list to be submitted once all
1767 * the necessary exceptions exist.
1768 */
1769static int __origin_write(struct list_head *snapshots, sector_t sector,
1770 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771{
Mikulas Patocka515ad662009-12-10 23:52:30 +00001772 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 struct dm_snapshot *snap;
Jon Brassow1d4989c2009-12-10 23:52:10 +00001774 struct dm_exception *e;
Mikulas Patocka515ad662009-12-10 23:52:30 +00001775 struct dm_snap_pending_exception *pe;
1776 struct dm_snap_pending_exception *pe_to_start_now = NULL;
1777 struct dm_snap_pending_exception *pe_to_start_last = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 chunk_t chunk;
1779
1780 /* Do all the snapshots on this origin */
1781 list_for_each_entry (snap, snapshots, list) {
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001782 /*
1783 * Don't make new exceptions in a merging snapshot
1784 * because it has effectively been deleted
1785 */
1786 if (dm_target_is_snapshot_merge(snap->ti))
1787 continue;
1788
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001789 down_write(&snap->lock);
1790
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001791 /* Only deal with valid and active snapshots */
1792 if (!snap->valid || !snap->active)
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001793 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
Alasdair G Kergond5e404c2005-07-12 15:53:05 -07001795 /* Nothing to do if writing beyond end of snapshot */
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00001796 if (sector >= dm_table_get_size(snap->ti->table))
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001797 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
1799 /*
1800 * Remember, different snapshots can have
1801 * different chunk sizes.
1802 */
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00001803 chunk = sector_to_chunk(snap->store, sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
1805 /*
1806 * Check exception table to see if block
1807 * is already remapped in this snapshot
1808 * and trigger an exception if not.
1809 */
Jon Brassow3510cb92009-12-10 23:52:11 +00001810 e = dm_lookup_exception(&snap->complete, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001811 if (e)
1812 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Mikulas Patocka29138082009-04-02 19:55:25 +01001814 pe = __lookup_pending_exception(snap, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001815 if (!pe) {
Mikulas Patockac6621392009-04-02 19:55:25 +01001816 up_write(&snap->lock);
1817 pe = alloc_pending_exception(snap);
1818 down_write(&snap->lock);
1819
1820 if (!snap->valid) {
1821 free_pending_exception(pe);
1822 goto next_snapshot;
1823 }
1824
Jon Brassow3510cb92009-12-10 23:52:11 +00001825 e = dm_lookup_exception(&snap->complete, chunk);
Mikulas Patocka35bf6592009-04-02 19:55:26 +01001826 if (e) {
1827 free_pending_exception(pe);
1828 goto next_snapshot;
1829 }
1830
Mikulas Patockac6621392009-04-02 19:55:25 +01001831 pe = __find_pending_exception(snap, pe, chunk);
Mikulas Patocka29138082009-04-02 19:55:25 +01001832 if (!pe) {
1833 __invalidate_snapshot(snap, -ENOMEM);
1834 goto next_snapshot;
1835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 }
1837
Mikulas Patocka515ad662009-12-10 23:52:30 +00001838 r = DM_MAPIO_SUBMITTED;
1839
1840 /*
1841 * If an origin bio was supplied, queue it to wait for the
1842 * completion of this exception, and start this one last,
1843 * at the end of the function.
1844 */
1845 if (bio) {
1846 bio_list_add(&pe->origin_bios, bio);
1847 bio = NULL;
1848
1849 if (!pe->started) {
1850 pe->started = 1;
1851 pe_to_start_last = pe;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001852 }
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001853 }
1854
1855 if (!pe->started) {
1856 pe->started = 1;
Mikulas Patocka515ad662009-12-10 23:52:30 +00001857 pe_to_start_now = pe;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001858 }
1859
1860 next_snapshot:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 up_write(&snap->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Mikulas Patocka515ad662009-12-10 23:52:30 +00001863 if (pe_to_start_now) {
1864 start_copy(pe_to_start_now);
1865 pe_to_start_now = NULL;
1866 }
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001867 }
1868
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 /*
Mikulas Patocka515ad662009-12-10 23:52:30 +00001870 * Submit the exception against which the bio is queued last,
1871 * to give the other exceptions a head start.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 */
Mikulas Patocka515ad662009-12-10 23:52:30 +00001873 if (pe_to_start_last)
1874 start_copy(pe_to_start_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
1876 return r;
1877}
1878
1879/*
1880 * Called on a write from the origin driver.
1881 */
1882static int do_origin(struct dm_dev *origin, struct bio *bio)
1883{
1884 struct origin *o;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001885 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
1887 down_read(&_origins_lock);
1888 o = __lookup_origin(origin->bdev);
1889 if (o)
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00001890 r = __origin_write(&o->snapshots, bio->bi_sector, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 up_read(&_origins_lock);
1892
1893 return r;
1894}
1895
1896/*
1897 * Origin: maps a linear range of a device, with hooks for snapshotting.
1898 */
1899
1900/*
1901 * Construct an origin mapping: <dev_path>
1902 * The context for an origin is merely a 'struct dm_dev *'
1903 * pointing to the real device.
1904 */
1905static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1906{
1907 int r;
1908 struct dm_dev *dev;
1909
1910 if (argc != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001911 ti->error = "origin: incorrect number of arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 return -EINVAL;
1913 }
1914
1915 r = dm_get_device(ti, argv[0], 0, ti->len,
1916 dm_table_get_mode(ti->table), &dev);
1917 if (r) {
1918 ti->error = "Cannot get target device";
1919 return r;
1920 }
1921
1922 ti->private = dev;
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01001923 ti->num_flush_requests = 1;
1924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 return 0;
1926}
1927
1928static void origin_dtr(struct dm_target *ti)
1929{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001930 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 dm_put_device(ti, dev);
1932}
1933
1934static int origin_map(struct dm_target *ti, struct bio *bio,
1935 union map_info *map_context)
1936{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001937 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 bio->bi_bdev = dev->bdev;
1939
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01001940 if (unlikely(bio_empty_barrier(bio)))
1941 return DM_MAPIO_REMAPPED;
1942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 /* Only tell snapshots if this is a write */
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001944 return (bio_rw(bio) == WRITE) ? do_origin(dev, bio) : DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945}
1946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947/*
1948 * Set the target "split_io" field to the minimum of all the snapshots'
1949 * chunk sizes.
1950 */
1951static void origin_resume(struct dm_target *ti)
1952{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001953 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001955 ti->split_io = get_origin_minimum_chunksize(dev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956}
1957
1958static int origin_status(struct dm_target *ti, status_type_t type, char *result,
1959 unsigned int maxlen)
1960{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001961 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
1963 switch (type) {
1964 case STATUSTYPE_INFO:
1965 result[0] = '\0';
1966 break;
1967
1968 case STATUSTYPE_TABLE:
1969 snprintf(result, maxlen, "%s", dev->name);
1970 break;
1971 }
1972
1973 return 0;
1974}
1975
Mike Snitzer8811f462009-09-04 20:40:19 +01001976static int origin_iterate_devices(struct dm_target *ti,
1977 iterate_devices_callout_fn fn, void *data)
1978{
1979 struct dm_dev *dev = ti->private;
1980
1981 return fn(ti, dev, 0, ti->len, data);
1982}
1983
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984static struct target_type origin_target = {
1985 .name = "snapshot-origin",
Mike Snitzer8811f462009-09-04 20:40:19 +01001986 .version = {1, 7, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 .module = THIS_MODULE,
1988 .ctr = origin_ctr,
1989 .dtr = origin_dtr,
1990 .map = origin_map,
1991 .resume = origin_resume,
1992 .status = origin_status,
Mike Snitzer8811f462009-09-04 20:40:19 +01001993 .iterate_devices = origin_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994};
1995
1996static struct target_type snapshot_target = {
1997 .name = "snapshot",
Mike Snitzerc26655c2009-12-10 23:52:12 +00001998 .version = {1, 9, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 .module = THIS_MODULE,
2000 .ctr = snapshot_ctr,
2001 .dtr = snapshot_dtr,
2002 .map = snapshot_map,
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002003 .end_io = snapshot_end_io,
Mike Snitzerc26655c2009-12-10 23:52:12 +00002004 .postsuspend = snapshot_postsuspend,
Mike Snitzerc1f0c182009-12-10 23:52:24 +00002005 .preresume = snapshot_preresume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 .resume = snapshot_resume,
2007 .status = snapshot_status,
Mike Snitzer8811f462009-09-04 20:40:19 +01002008 .iterate_devices = snapshot_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009};
2010
Mikulas Patockad698aa42009-12-10 23:52:30 +00002011static struct target_type merge_target = {
2012 .name = dm_snapshot_merge_target_name,
2013 .version = {1, 0, 0},
2014 .module = THIS_MODULE,
2015 .ctr = snapshot_ctr,
2016 .dtr = snapshot_dtr,
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00002017 .map = snapshot_merge_map,
Mikulas Patockad698aa42009-12-10 23:52:30 +00002018 .end_io = snapshot_end_io,
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002019 .presuspend = snapshot_merge_presuspend,
Mikulas Patockad698aa42009-12-10 23:52:30 +00002020 .postsuspend = snapshot_postsuspend,
2021 .preresume = snapshot_preresume,
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002022 .resume = snapshot_merge_resume,
Mikulas Patockad698aa42009-12-10 23:52:30 +00002023 .status = snapshot_status,
2024 .iterate_devices = snapshot_iterate_devices,
2025};
2026
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027static int __init dm_snapshot_init(void)
2028{
2029 int r;
2030
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00002031 r = dm_exception_store_init();
2032 if (r) {
2033 DMERR("Failed to initialize exception stores");
2034 return r;
2035 }
2036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 r = dm_register_target(&snapshot_target);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002038 if (r < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 DMERR("snapshot target register failed %d", r);
Jonathan Brassow034a1862009-10-16 23:18:14 +01002040 goto bad_register_snapshot_target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 }
2042
2043 r = dm_register_target(&origin_target);
2044 if (r < 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002045 DMERR("Origin target register failed %d", r);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002046 goto bad_register_origin_target;
2047 }
2048
2049 r = dm_register_target(&merge_target);
2050 if (r < 0) {
2051 DMERR("Merge target register failed %d", r);
2052 goto bad_register_merge_target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 }
2054
2055 r = init_origin_hash();
2056 if (r) {
2057 DMERR("init_origin_hash failed.");
Mikulas Patockad698aa42009-12-10 23:52:30 +00002058 goto bad_origin_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 }
2060
Jon Brassow1d4989c2009-12-10 23:52:10 +00002061 exception_cache = KMEM_CACHE(dm_exception, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 if (!exception_cache) {
2063 DMERR("Couldn't create exception cache.");
2064 r = -ENOMEM;
Mikulas Patockad698aa42009-12-10 23:52:30 +00002065 goto bad_exception_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 }
2067
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002068 pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 if (!pending_cache) {
2070 DMERR("Couldn't create pending cache.");
2071 r = -ENOMEM;
Mikulas Patockad698aa42009-12-10 23:52:30 +00002072 goto bad_pending_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 }
2074
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002075 tracked_chunk_cache = KMEM_CACHE(dm_snap_tracked_chunk, 0);
2076 if (!tracked_chunk_cache) {
2077 DMERR("Couldn't create cache to track chunks in use.");
2078 r = -ENOMEM;
Mikulas Patockad698aa42009-12-10 23:52:30 +00002079 goto bad_tracked_chunk_cache;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002080 }
2081
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07002082 ksnapd = create_singlethread_workqueue("ksnapd");
2083 if (!ksnapd) {
2084 DMERR("Failed to create ksnapd workqueue.");
2085 r = -ENOMEM;
Mikulas Patocka92e86812008-07-21 12:00:35 +01002086 goto bad_pending_pool;
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07002087 }
2088
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 return 0;
2090
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00002091bad_pending_pool:
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002092 kmem_cache_destroy(tracked_chunk_cache);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002093bad_tracked_chunk_cache:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 kmem_cache_destroy(pending_cache);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002095bad_pending_cache:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 kmem_cache_destroy(exception_cache);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002097bad_exception_cache:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 exit_origin_hash();
Mikulas Patockad698aa42009-12-10 23:52:30 +00002099bad_origin_hash:
2100 dm_unregister_target(&merge_target);
2101bad_register_merge_target:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 dm_unregister_target(&origin_target);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002103bad_register_origin_target:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 dm_unregister_target(&snapshot_target);
Jonathan Brassow034a1862009-10-16 23:18:14 +01002105bad_register_snapshot_target:
2106 dm_exception_store_exit();
Mikulas Patockad698aa42009-12-10 23:52:30 +00002107
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 return r;
2109}
2110
2111static void __exit dm_snapshot_exit(void)
2112{
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07002113 destroy_workqueue(ksnapd);
2114
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00002115 dm_unregister_target(&snapshot_target);
2116 dm_unregister_target(&origin_target);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002117 dm_unregister_target(&merge_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
2119 exit_origin_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 kmem_cache_destroy(pending_cache);
2121 kmem_cache_destroy(exception_cache);
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002122 kmem_cache_destroy(tracked_chunk_cache);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00002123
2124 dm_exception_store_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125}
2126
2127/* Module hooks */
2128module_init(dm_snapshot_init);
2129module_exit(dm_snapshot_exit);
2130
2131MODULE_DESCRIPTION(DM_NAME " snapshot target");
2132MODULE_AUTHOR("Joe Thornber");
2133MODULE_LICENSE("GPL");