blob: 0f47698beafa179e84c50324ea6191db134f6b49 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Jonathan Brassowaea53d92009-01-06 03:05:15 +000023#include "dm-exception-store.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Alasdair G Kergon72d94862006-06-26 00:27:35 -070025#define DM_MSG_PREFIX "snapshots"
26
Mikulas Patockad698aa42009-12-10 23:52:30 +000027static const char dm_snapshot_merge_target_name[] = "snapshot-merge";
28
29#define dm_target_is_snapshot_merge(ti) \
30 ((ti)->type->name == dm_snapshot_merge_target_name)
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032/*
33 * The percentage increment we will wake up users at
34 */
35#define WAKE_UP_PERCENT 5
36
37/*
38 * kcopyd priority of snapshot operations
39 */
40#define SNAPSHOT_COPY_PRIORITY 2
41
42/*
Milan Broz8ee27672008-04-24 21:42:36 +010043 * Reserve 1MB for each snapshot initially (with minimum of 1 page).
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 */
Milan Broz8ee27672008-04-24 21:42:36 +010045#define SNAPSHOT_PAGES (((1UL << 20) >> PAGE_SHIFT) ? : 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Mikulas Patockacd45daf2008-07-21 12:00:32 +010047/*
48 * The size of the mempool used to track chunks in use.
49 */
50#define MIN_IOS 256
51
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010052#define DM_TRACKED_CHUNK_HASH_SIZE 16
53#define DM_TRACKED_CHUNK_HASH(x) ((unsigned long)(x) & \
54 (DM_TRACKED_CHUNK_HASH_SIZE - 1))
55
Jon Brassow191437a2009-12-10 23:52:10 +000056struct dm_exception_table {
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010057 uint32_t hash_mask;
58 unsigned hash_shift;
59 struct list_head *table;
60};
61
62struct dm_snapshot {
63 struct rw_semaphore lock;
64
65 struct dm_dev *origin;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +000066 struct dm_dev *cow;
67
68 struct dm_target *ti;
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010069
70 /* List of snapshots per Origin */
71 struct list_head list;
72
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +000073 /*
74 * You can't use a snapshot if this is 0 (e.g. if full).
75 * A snapshot-merge target never clears this.
76 */
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010077 int valid;
78
79 /* Origin writes don't trigger exceptions until this is set */
80 int active;
81
Mike Snitzerc26655c2009-12-10 23:52:12 +000082 /* Whether or not owning mapped_device is suspended */
83 int suspended;
84
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010085 atomic_t pending_exceptions_count;
86
Mike Snitzer924e6002010-03-06 02:32:33 +000087 mempool_t *pending_pool;
88
Jon Brassow191437a2009-12-10 23:52:10 +000089 struct dm_exception_table pending;
90 struct dm_exception_table complete;
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010091
92 /*
93 * pe_lock protects all pending_exception operations and access
94 * as well as the snapshot_bios list.
95 */
96 spinlock_t pe_lock;
97
Mike Snitzer924e6002010-03-06 02:32:33 +000098 /* Chunks with outstanding reads */
99 spinlock_t tracked_chunk_lock;
100 mempool_t *tracked_chunk_pool;
101 struct hlist_head tracked_chunk_hash[DM_TRACKED_CHUNK_HASH_SIZE];
102
Jonathan Brassowccc45ea2009-04-02 19:55:34 +0100103 /* The on disk metadata handler */
104 struct dm_exception_store *store;
105
106 struct dm_kcopyd_client *kcopyd_client;
107
Mike Snitzer924e6002010-03-06 02:32:33 +0000108 /* Wait for events based on state_bits */
109 unsigned long state_bits;
110
111 /* Range of chunks currently being merged. */
112 chunk_t first_merging_chunk;
113 int num_merging_chunks;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000114
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +0000115 /*
116 * The merge operation failed if this flag is set.
117 * Failure modes are handled as follows:
118 * - I/O error reading the header
119 * => don't load the target; abort.
120 * - Header does not have "valid" flag set
121 * => use the origin; forget about the snapshot.
122 * - I/O error when reading exceptions
123 * => don't load the target; abort.
124 * (We can't use the intermediate origin state.)
125 * - I/O error while merging
126 * => stop merging; set merge_failed; process I/O normally.
127 */
128 int merge_failed;
129
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000130 /*
131 * Incoming bios that overlap with chunks being merged must wait
132 * for them to be committed.
133 */
134 struct bio_list bios_queued_during_merge;
Jonathan Brassowccc45ea2009-04-02 19:55:34 +0100135};
136
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000137/*
138 * state_bits:
139 * RUNNING_MERGE - Merge operation is in progress.
140 * SHUTDOWN_MERGE - Set to signal that merge needs to be stopped;
141 * cleared afterwards.
142 */
143#define RUNNING_MERGE 0
144#define SHUTDOWN_MERGE 1
145
Mikulas Patockac2411042010-08-12 04:13:51 +0100146struct dm_dev *dm_snap_origin(struct dm_snapshot *s)
147{
148 return s->origin;
149}
150EXPORT_SYMBOL(dm_snap_origin);
151
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000152struct dm_dev *dm_snap_cow(struct dm_snapshot *s)
153{
154 return s->cow;
155}
156EXPORT_SYMBOL(dm_snap_cow);
157
Jonathan Brassowccc45ea2009-04-02 19:55:34 +0100158static sector_t chunk_to_sector(struct dm_exception_store *store,
159 chunk_t chunk)
160{
161 return chunk << store->chunk_shift;
162}
163
164static int bdev_equal(struct block_device *lhs, struct block_device *rhs)
165{
166 /*
167 * There is only ever one instance of a particular block
168 * device so we can compare pointers safely.
169 */
170 return lhs == rhs;
171}
172
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100173struct dm_snap_pending_exception {
Jon Brassow1d4989c2009-12-10 23:52:10 +0000174 struct dm_exception e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 /*
177 * Origin buffers waiting for this to complete are held
178 * in a bio list
179 */
180 struct bio_list origin_bios;
181 struct bio_list snapshot_bios;
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 /* Pointer back to snapshot context */
184 struct dm_snapshot *snap;
185
186 /*
187 * 1 indicates the exception has already been sent to
188 * kcopyd.
189 */
190 int started;
191};
192
193/*
194 * Hash table mapping origin volumes to lists of snapshots and
195 * a lock to protect it
196 */
Christoph Lametere18b8902006-12-06 20:33:20 -0800197static struct kmem_cache *exception_cache;
198static struct kmem_cache *pending_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100200struct dm_snap_tracked_chunk {
201 struct hlist_node node;
202 chunk_t chunk;
203};
204
205static struct kmem_cache *tracked_chunk_cache;
206
207static struct dm_snap_tracked_chunk *track_chunk(struct dm_snapshot *s,
208 chunk_t chunk)
209{
210 struct dm_snap_tracked_chunk *c = mempool_alloc(s->tracked_chunk_pool,
211 GFP_NOIO);
212 unsigned long flags;
213
214 c->chunk = chunk;
215
216 spin_lock_irqsave(&s->tracked_chunk_lock, flags);
217 hlist_add_head(&c->node,
218 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)]);
219 spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
220
221 return c;
222}
223
224static void stop_tracking_chunk(struct dm_snapshot *s,
225 struct dm_snap_tracked_chunk *c)
226{
227 unsigned long flags;
228
229 spin_lock_irqsave(&s->tracked_chunk_lock, flags);
230 hlist_del(&c->node);
231 spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
232
233 mempool_free(c, s->tracked_chunk_pool);
234}
235
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100236static int __chunk_is_tracked(struct dm_snapshot *s, chunk_t chunk)
237{
238 struct dm_snap_tracked_chunk *c;
239 struct hlist_node *hn;
240 int found = 0;
241
242 spin_lock_irq(&s->tracked_chunk_lock);
243
244 hlist_for_each_entry(c, hn,
245 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)], node) {
246 if (c->chunk == chunk) {
247 found = 1;
248 break;
249 }
250 }
251
252 spin_unlock_irq(&s->tracked_chunk_lock);
253
254 return found;
255}
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257/*
Mike Snitzer615d1eb2009-12-10 23:52:29 +0000258 * This conflicting I/O is extremely improbable in the caller,
259 * so msleep(1) is sufficient and there is no need for a wait queue.
260 */
261static void __check_for_conflicting_io(struct dm_snapshot *s, chunk_t chunk)
262{
263 while (__chunk_is_tracked(s, chunk))
264 msleep(1);
265}
266
267/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 * One of these per registered origin, held in the snapshot_origins hash
269 */
270struct origin {
271 /* The origin device */
272 struct block_device *bdev;
273
274 struct list_head hash_list;
275
276 /* List of snapshots for this origin */
277 struct list_head snapshots;
278};
279
280/*
281 * Size of the hash table for origin volumes. If we make this
282 * the size of the minors list then it should be nearly perfect
283 */
284#define ORIGIN_HASH_SIZE 256
285#define ORIGIN_MASK 0xFF
286static struct list_head *_origins;
287static struct rw_semaphore _origins_lock;
288
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000289static DECLARE_WAIT_QUEUE_HEAD(_pending_exceptions_done);
290static DEFINE_SPINLOCK(_pending_exceptions_done_spinlock);
291static uint64_t _pending_exceptions_done_count;
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293static int init_origin_hash(void)
294{
295 int i;
296
297 _origins = kmalloc(ORIGIN_HASH_SIZE * sizeof(struct list_head),
298 GFP_KERNEL);
299 if (!_origins) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700300 DMERR("unable to allocate memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return -ENOMEM;
302 }
303
304 for (i = 0; i < ORIGIN_HASH_SIZE; i++)
305 INIT_LIST_HEAD(_origins + i);
306 init_rwsem(&_origins_lock);
307
308 return 0;
309}
310
311static void exit_origin_hash(void)
312{
313 kfree(_origins);
314}
315
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100316static unsigned origin_hash(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
318 return bdev->bd_dev & ORIGIN_MASK;
319}
320
321static struct origin *__lookup_origin(struct block_device *origin)
322{
323 struct list_head *ol;
324 struct origin *o;
325
326 ol = &_origins[origin_hash(origin)];
327 list_for_each_entry (o, ol, hash_list)
328 if (bdev_equal(o->bdev, origin))
329 return o;
330
331 return NULL;
332}
333
334static void __insert_origin(struct origin *o)
335{
336 struct list_head *sl = &_origins[origin_hash(o->bdev)];
337 list_add_tail(&o->hash_list, sl);
338}
339
340/*
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000341 * _origins_lock must be held when calling this function.
342 * Returns number of snapshots registered using the supplied cow device, plus:
343 * snap_src - a snapshot suitable for use as a source of exception handover
344 * snap_dest - a snapshot capable of receiving exception handover.
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000345 * snap_merge - an existing snapshot-merge target linked to the same origin.
346 * There can be at most one snapshot-merge target. The parameter is optional.
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000347 *
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000348 * Possible return values and states of snap_src and snap_dest.
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000349 * 0: NULL, NULL - first new snapshot
350 * 1: snap_src, NULL - normal snapshot
351 * 2: snap_src, snap_dest - waiting for handover
352 * 2: snap_src, NULL - handed over, waiting for old to be deleted
353 * 1: NULL, snap_dest - source got destroyed without handover
354 */
355static int __find_snapshots_sharing_cow(struct dm_snapshot *snap,
356 struct dm_snapshot **snap_src,
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000357 struct dm_snapshot **snap_dest,
358 struct dm_snapshot **snap_merge)
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000359{
360 struct dm_snapshot *s;
361 struct origin *o;
362 int count = 0;
363 int active;
364
365 o = __lookup_origin(snap->origin->bdev);
366 if (!o)
367 goto out;
368
369 list_for_each_entry(s, &o->snapshots, list) {
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000370 if (dm_target_is_snapshot_merge(s->ti) && snap_merge)
371 *snap_merge = s;
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000372 if (!bdev_equal(s->cow->bdev, snap->cow->bdev))
373 continue;
374
375 down_read(&s->lock);
376 active = s->active;
377 up_read(&s->lock);
378
379 if (active) {
380 if (snap_src)
381 *snap_src = s;
382 } else if (snap_dest)
383 *snap_dest = s;
384
385 count++;
386 }
387
388out:
389 return count;
390}
391
392/*
393 * On success, returns 1 if this snapshot is a handover destination,
394 * otherwise returns 0.
395 */
396static int __validate_exception_handover(struct dm_snapshot *snap)
397{
398 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000399 struct dm_snapshot *snap_merge = NULL;
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000400
401 /* Does snapshot need exceptions handed over to it? */
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000402 if ((__find_snapshots_sharing_cow(snap, &snap_src, &snap_dest,
403 &snap_merge) == 2) ||
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000404 snap_dest) {
405 snap->ti->error = "Snapshot cow pairing for exception "
406 "table handover failed";
407 return -EINVAL;
408 }
409
410 /*
411 * If no snap_src was found, snap cannot become a handover
412 * destination.
413 */
414 if (!snap_src)
415 return 0;
416
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000417 /*
418 * Non-snapshot-merge handover?
419 */
420 if (!dm_target_is_snapshot_merge(snap->ti))
421 return 1;
422
423 /*
424 * Do not allow more than one merging snapshot.
425 */
426 if (snap_merge) {
427 snap->ti->error = "A snapshot is already merging.";
428 return -EINVAL;
429 }
430
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000431 if (!snap_src->store->type->prepare_merge ||
432 !snap_src->store->type->commit_merge) {
433 snap->ti->error = "Snapshot exception store does not "
434 "support snapshot-merge.";
435 return -EINVAL;
436 }
437
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000438 return 1;
439}
440
441static void __insert_snapshot(struct origin *o, struct dm_snapshot *s)
442{
443 struct dm_snapshot *l;
444
445 /* Sort the list according to chunk size, largest-first smallest-last */
446 list_for_each_entry(l, &o->snapshots, list)
447 if (l->store->chunk_size < s->store->chunk_size)
448 break;
449 list_add_tail(&s->list, &l->list);
450}
451
452/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 * Make a note of the snapshot and its origin so we can look it
454 * up when the origin has a write on it.
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000455 *
456 * Also validate snapshot exception store handovers.
457 * On success, returns 1 if this registration is a handover destination,
458 * otherwise returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 */
460static int register_snapshot(struct dm_snapshot *snap)
461{
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000462 struct origin *o, *new_o = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 struct block_device *bdev = snap->origin->bdev;
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000464 int r = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000466 new_o = kmalloc(sizeof(*new_o), GFP_KERNEL);
467 if (!new_o)
468 return -ENOMEM;
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 down_write(&_origins_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000472 r = __validate_exception_handover(snap);
473 if (r < 0) {
474 kfree(new_o);
475 goto out;
476 }
477
478 o = __lookup_origin(bdev);
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000479 if (o)
480 kfree(new_o);
481 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 /* New origin */
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000483 o = new_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 /* Initialise the struct */
486 INIT_LIST_HEAD(&o->snapshots);
487 o->bdev = bdev;
488
489 __insert_origin(o);
490 }
491
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000492 __insert_snapshot(o, snap);
493
494out:
495 up_write(&_origins_lock);
496
497 return r;
498}
499
500/*
501 * Move snapshot to correct place in list according to chunk size.
502 */
503static void reregister_snapshot(struct dm_snapshot *s)
504{
505 struct block_device *bdev = s->origin->bdev;
506
507 down_write(&_origins_lock);
508
509 list_del(&s->list);
510 __insert_snapshot(__lookup_origin(bdev), s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 up_write(&_origins_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
515static void unregister_snapshot(struct dm_snapshot *s)
516{
517 struct origin *o;
518
519 down_write(&_origins_lock);
520 o = __lookup_origin(s->origin->bdev);
521
522 list_del(&s->list);
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000523 if (o && list_empty(&o->snapshots)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 list_del(&o->hash_list);
525 kfree(o);
526 }
527
528 up_write(&_origins_lock);
529}
530
531/*
532 * Implementation of the exception hash tables.
Milan Brozd74f81f2008-02-08 02:11:27 +0000533 * The lowest hash_shift bits of the chunk number are ignored, allowing
534 * some consecutive chunks to be grouped together.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 */
Jon Brassow3510cb92009-12-10 23:52:11 +0000536static int dm_exception_table_init(struct dm_exception_table *et,
537 uint32_t size, unsigned hash_shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 unsigned int i;
540
Milan Brozd74f81f2008-02-08 02:11:27 +0000541 et->hash_shift = hash_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 et->hash_mask = size - 1;
543 et->table = dm_vcalloc(size, sizeof(struct list_head));
544 if (!et->table)
545 return -ENOMEM;
546
547 for (i = 0; i < size; i++)
548 INIT_LIST_HEAD(et->table + i);
549
550 return 0;
551}
552
Jon Brassow3510cb92009-12-10 23:52:11 +0000553static void dm_exception_table_exit(struct dm_exception_table *et,
554 struct kmem_cache *mem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
556 struct list_head *slot;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000557 struct dm_exception *ex, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 int i, size;
559
560 size = et->hash_mask + 1;
561 for (i = 0; i < size; i++) {
562 slot = et->table + i;
563
564 list_for_each_entry_safe (ex, next, slot, hash_list)
565 kmem_cache_free(mem, ex);
566 }
567
568 vfree(et->table);
569}
570
Jon Brassow191437a2009-12-10 23:52:10 +0000571static uint32_t exception_hash(struct dm_exception_table *et, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Milan Brozd74f81f2008-02-08 02:11:27 +0000573 return (chunk >> et->hash_shift) & et->hash_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
Jon Brassow3510cb92009-12-10 23:52:11 +0000576static void dm_remove_exception(struct dm_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
578 list_del(&e->hash_list);
579}
580
581/*
582 * Return the exception data for a sector, or NULL if not
583 * remapped.
584 */
Jon Brassow3510cb92009-12-10 23:52:11 +0000585static struct dm_exception *dm_lookup_exception(struct dm_exception_table *et,
586 chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
588 struct list_head *slot;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000589 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 slot = &et->table[exception_hash(et, chunk)];
592 list_for_each_entry (e, slot, hash_list)
Milan Brozd74f81f2008-02-08 02:11:27 +0000593 if (chunk >= e->old_chunk &&
594 chunk <= e->old_chunk + dm_consecutive_chunk_count(e))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return e;
596
597 return NULL;
598}
599
Jon Brassow3510cb92009-12-10 23:52:11 +0000600static struct dm_exception *alloc_completed_exception(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Jon Brassow1d4989c2009-12-10 23:52:10 +0000602 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 e = kmem_cache_alloc(exception_cache, GFP_NOIO);
605 if (!e)
606 e = kmem_cache_alloc(exception_cache, GFP_ATOMIC);
607
608 return e;
609}
610
Jon Brassow3510cb92009-12-10 23:52:11 +0000611static void free_completed_exception(struct dm_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
613 kmem_cache_free(exception_cache, e);
614}
615
Mikulas Patocka92e86812008-07-21 12:00:35 +0100616static struct dm_snap_pending_exception *alloc_pending_exception(struct dm_snapshot *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Mikulas Patocka92e86812008-07-21 12:00:35 +0100618 struct dm_snap_pending_exception *pe = mempool_alloc(s->pending_pool,
619 GFP_NOIO);
620
Mikulas Patocka879129d22008-10-30 13:33:16 +0000621 atomic_inc(&s->pending_exceptions_count);
Mikulas Patocka92e86812008-07-21 12:00:35 +0100622 pe->snap = s;
623
624 return pe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100627static void free_pending_exception(struct dm_snap_pending_exception *pe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Mikulas Patocka879129d22008-10-30 13:33:16 +0000629 struct dm_snapshot *s = pe->snap;
630
631 mempool_free(pe, s->pending_pool);
632 smp_mb__before_atomic_dec();
633 atomic_dec(&s->pending_exceptions_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634}
635
Jon Brassow3510cb92009-12-10 23:52:11 +0000636static void dm_insert_exception(struct dm_exception_table *eh,
637 struct dm_exception *new_e)
Milan Brozd74f81f2008-02-08 02:11:27 +0000638{
Milan Brozd74f81f2008-02-08 02:11:27 +0000639 struct list_head *l;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000640 struct dm_exception *e = NULL;
Milan Brozd74f81f2008-02-08 02:11:27 +0000641
642 l = &eh->table[exception_hash(eh, new_e->old_chunk)];
643
644 /* Add immediately if this table doesn't support consecutive chunks */
645 if (!eh->hash_shift)
646 goto out;
647
648 /* List is ordered by old_chunk */
649 list_for_each_entry_reverse(e, l, hash_list) {
650 /* Insert after an existing chunk? */
651 if (new_e->old_chunk == (e->old_chunk +
652 dm_consecutive_chunk_count(e) + 1) &&
653 new_e->new_chunk == (dm_chunk_number(e->new_chunk) +
654 dm_consecutive_chunk_count(e) + 1)) {
655 dm_consecutive_chunk_count_inc(e);
Jon Brassow3510cb92009-12-10 23:52:11 +0000656 free_completed_exception(new_e);
Milan Brozd74f81f2008-02-08 02:11:27 +0000657 return;
658 }
659
660 /* Insert before an existing chunk? */
661 if (new_e->old_chunk == (e->old_chunk - 1) &&
662 new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) {
663 dm_consecutive_chunk_count_inc(e);
664 e->old_chunk--;
665 e->new_chunk--;
Jon Brassow3510cb92009-12-10 23:52:11 +0000666 free_completed_exception(new_e);
Milan Brozd74f81f2008-02-08 02:11:27 +0000667 return;
668 }
669
670 if (new_e->old_chunk > e->old_chunk)
671 break;
672 }
673
674out:
675 list_add(&new_e->hash_list, e ? &e->hash_list : l);
676}
677
Jonathan Brassowa159c1a2009-01-06 03:05:19 +0000678/*
679 * Callback used by the exception stores to load exceptions when
680 * initialising.
681 */
682static int dm_add_exception(void *context, chunk_t old, chunk_t new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Jonathan Brassowa159c1a2009-01-06 03:05:19 +0000684 struct dm_snapshot *s = context;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000685 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Jon Brassow3510cb92009-12-10 23:52:11 +0000687 e = alloc_completed_exception();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (!e)
689 return -ENOMEM;
690
691 e->old_chunk = old;
Milan Brozd74f81f2008-02-08 02:11:27 +0000692
693 /* Consecutive_count is implicitly initialised to zero */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 e->new_chunk = new;
Milan Brozd74f81f2008-02-08 02:11:27 +0000695
Jon Brassow3510cb92009-12-10 23:52:11 +0000696 dm_insert_exception(&s->complete, e);
Milan Brozd74f81f2008-02-08 02:11:27 +0000697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return 0;
699}
700
Mikulas Patocka7e201b32009-12-10 23:52:08 +0000701/*
702 * Return a minimum chunk size of all snapshots that have the specified origin.
703 * Return zero if the origin has no snapshots.
704 */
705static sector_t __minimum_chunk_size(struct origin *o)
706{
707 struct dm_snapshot *snap;
708 unsigned chunk_size = 0;
709
710 if (o)
711 list_for_each_entry(snap, &o->snapshots, list)
712 chunk_size = min_not_zero(chunk_size,
713 snap->store->chunk_size);
714
715 return chunk_size;
716}
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718/*
719 * Hard coded magic.
720 */
721static int calc_max_buckets(void)
722{
723 /* use a fixed size of 2MB */
724 unsigned long mem = 2 * 1024 * 1024;
725 mem /= sizeof(struct list_head);
726
727 return mem;
728}
729
730/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 * Allocate room for a suitable hash table.
732 */
Jonathan Brassowfee19982009-04-02 19:55:34 +0100733static int init_hash_tables(struct dm_snapshot *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
735 sector_t hash_size, cow_dev_size, origin_dev_size, max_buckets;
736
737 /*
738 * Calculate based on the size of the original volume or
739 * the COW volume...
740 */
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000741 cow_dev_size = get_dev_size(s->cow->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 origin_dev_size = get_dev_size(s->origin->bdev);
743 max_buckets = calc_max_buckets();
744
Jonathan Brassowfee19982009-04-02 19:55:34 +0100745 hash_size = min(origin_dev_size, cow_dev_size) >> s->store->chunk_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 hash_size = min(hash_size, max_buckets);
747
Mikulas Patocka8e87b9b2009-12-10 23:51:54 +0000748 if (hash_size < 64)
749 hash_size = 64;
Robert P. J. Day8defd832008-02-08 02:10:06 +0000750 hash_size = rounddown_pow_of_two(hash_size);
Jon Brassow3510cb92009-12-10 23:52:11 +0000751 if (dm_exception_table_init(&s->complete, hash_size,
752 DM_CHUNK_CONSECUTIVE_BITS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 return -ENOMEM;
754
755 /*
756 * Allocate hash table for in-flight exceptions
757 * Make this smaller than the real hash table
758 */
759 hash_size >>= 3;
760 if (hash_size < 64)
761 hash_size = 64;
762
Jon Brassow3510cb92009-12-10 23:52:11 +0000763 if (dm_exception_table_init(&s->pending, hash_size, 0)) {
764 dm_exception_table_exit(&s->complete, exception_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 return -ENOMEM;
766 }
767
768 return 0;
769}
770
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000771static void merge_shutdown(struct dm_snapshot *s)
772{
773 clear_bit_unlock(RUNNING_MERGE, &s->state_bits);
774 smp_mb__after_clear_bit();
775 wake_up_bit(&s->state_bits, RUNNING_MERGE);
776}
777
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000778static struct bio *__release_queued_bios_after_merge(struct dm_snapshot *s)
779{
780 s->first_merging_chunk = 0;
781 s->num_merging_chunks = 0;
782
783 return bio_list_get(&s->bios_queued_during_merge);
784}
785
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000786/*
787 * Remove one chunk from the index of completed exceptions.
788 */
789static int __remove_single_exception_chunk(struct dm_snapshot *s,
790 chunk_t old_chunk)
791{
792 struct dm_exception *e;
793
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000794 e = dm_lookup_exception(&s->complete, old_chunk);
795 if (!e) {
796 DMERR("Corruption detected: exception for block %llu is "
797 "on disk but not in memory",
798 (unsigned long long)old_chunk);
799 return -EINVAL;
800 }
801
802 /*
803 * If this is the only chunk using this exception, remove exception.
804 */
805 if (!dm_consecutive_chunk_count(e)) {
806 dm_remove_exception(e);
807 free_completed_exception(e);
808 return 0;
809 }
810
811 /*
812 * The chunk may be either at the beginning or the end of a
813 * group of consecutive chunks - never in the middle. We are
814 * removing chunks in the opposite order to that in which they
815 * were added, so this should always be true.
816 * Decrement the consecutive chunk counter and adjust the
817 * starting point if necessary.
818 */
819 if (old_chunk == e->old_chunk) {
820 e->old_chunk++;
821 e->new_chunk++;
822 } else if (old_chunk != e->old_chunk +
823 dm_consecutive_chunk_count(e)) {
824 DMERR("Attempt to merge block %llu from the "
825 "middle of a chunk range [%llu - %llu]",
826 (unsigned long long)old_chunk,
827 (unsigned long long)e->old_chunk,
828 (unsigned long long)
829 e->old_chunk + dm_consecutive_chunk_count(e));
830 return -EINVAL;
831 }
832
833 dm_consecutive_chunk_count_dec(e);
834
835 return 0;
836}
837
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000838static void flush_bios(struct bio *bio);
839
840static int remove_single_exception_chunk(struct dm_snapshot *s)
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000841{
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000842 struct bio *b = NULL;
843 int r;
844 chunk_t old_chunk = s->first_merging_chunk + s->num_merging_chunks - 1;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000845
846 down_write(&s->lock);
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000847
848 /*
849 * Process chunks (and associated exceptions) in reverse order
850 * so that dm_consecutive_chunk_count_dec() accounting works.
851 */
852 do {
853 r = __remove_single_exception_chunk(s, old_chunk);
854 if (r)
855 goto out;
856 } while (old_chunk-- > s->first_merging_chunk);
857
858 b = __release_queued_bios_after_merge(s);
859
860out:
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000861 up_write(&s->lock);
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000862 if (b)
863 flush_bios(b);
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000864
865 return r;
866}
867
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000868static int origin_write_extent(struct dm_snapshot *merging_snap,
869 sector_t sector, unsigned chunk_size);
870
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000871static void merge_callback(int read_err, unsigned long write_err,
872 void *context);
873
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000874static uint64_t read_pending_exceptions_done_count(void)
875{
876 uint64_t pending_exceptions_done;
877
878 spin_lock(&_pending_exceptions_done_spinlock);
879 pending_exceptions_done = _pending_exceptions_done_count;
880 spin_unlock(&_pending_exceptions_done_spinlock);
881
882 return pending_exceptions_done;
883}
884
885static void increment_pending_exceptions_done_count(void)
886{
887 spin_lock(&_pending_exceptions_done_spinlock);
888 _pending_exceptions_done_count++;
889 spin_unlock(&_pending_exceptions_done_spinlock);
890
891 wake_up_all(&_pending_exceptions_done);
892}
893
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000894static void snapshot_merge_next_chunks(struct dm_snapshot *s)
895{
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000896 int i, linear_chunks;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000897 chunk_t old_chunk, new_chunk;
898 struct dm_io_region src, dest;
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000899 sector_t io_size;
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000900 uint64_t previous_count;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000901
902 BUG_ON(!test_bit(RUNNING_MERGE, &s->state_bits));
903 if (unlikely(test_bit(SHUTDOWN_MERGE, &s->state_bits)))
904 goto shut;
905
906 /*
907 * valid flag never changes during merge, so no lock required.
908 */
909 if (!s->valid) {
910 DMERR("Snapshot is invalid: can't merge");
911 goto shut;
912 }
913
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000914 linear_chunks = s->store->type->prepare_merge(s->store, &old_chunk,
915 &new_chunk);
916 if (linear_chunks <= 0) {
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +0000917 if (linear_chunks < 0) {
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000918 DMERR("Read error in exception store: "
919 "shutting down merge");
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +0000920 down_write(&s->lock);
921 s->merge_failed = 1;
922 up_write(&s->lock);
923 }
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000924 goto shut;
925 }
926
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000927 /* Adjust old_chunk and new_chunk to reflect start of linear region */
928 old_chunk = old_chunk + 1 - linear_chunks;
929 new_chunk = new_chunk + 1 - linear_chunks;
930
931 /*
932 * Use one (potentially large) I/O to copy all 'linear_chunks'
933 * from the exception store to the origin
934 */
935 io_size = linear_chunks * s->store->chunk_size;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000936
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000937 dest.bdev = s->origin->bdev;
938 dest.sector = chunk_to_sector(s->store, old_chunk);
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000939 dest.count = min(io_size, get_dev_size(dest.bdev) - dest.sector);
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000940
941 src.bdev = s->cow->bdev;
942 src.sector = chunk_to_sector(s->store, new_chunk);
943 src.count = dest.count;
944
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000945 /*
946 * Reallocate any exceptions needed in other snapshots then
947 * wait for the pending exceptions to complete.
948 * Each time any pending exception (globally on the system)
949 * completes we are woken and repeat the process to find out
950 * if we can proceed. While this may not seem a particularly
951 * efficient algorithm, it is not expected to have any
952 * significant impact on performance.
953 */
954 previous_count = read_pending_exceptions_done_count();
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000955 while (origin_write_extent(s, dest.sector, io_size)) {
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000956 wait_event(_pending_exceptions_done,
957 (read_pending_exceptions_done_count() !=
958 previous_count));
959 /* Retry after the wait, until all exceptions are done. */
960 previous_count = read_pending_exceptions_done_count();
961 }
962
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000963 down_write(&s->lock);
964 s->first_merging_chunk = old_chunk;
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000965 s->num_merging_chunks = linear_chunks;
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000966 up_write(&s->lock);
967
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000968 /* Wait until writes to all 'linear_chunks' drain */
969 for (i = 0; i < linear_chunks; i++)
970 __check_for_conflicting_io(s, old_chunk + i);
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000971
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000972 dm_kcopyd_copy(s->kcopyd_client, &src, 1, &dest, 0, merge_callback, s);
973 return;
974
975shut:
976 merge_shutdown(s);
977}
978
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000979static void error_bios(struct bio *bio);
980
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000981static void merge_callback(int read_err, unsigned long write_err, void *context)
982{
983 struct dm_snapshot *s = context;
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000984 struct bio *b = NULL;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000985
986 if (read_err || write_err) {
987 if (read_err)
988 DMERR("Read error: shutting down merge.");
989 else
990 DMERR("Write error: shutting down merge.");
991 goto shut;
992 }
993
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000994 if (s->store->type->commit_merge(s->store,
995 s->num_merging_chunks) < 0) {
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000996 DMERR("Write error in exception store: shutting down merge");
997 goto shut;
998 }
999
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001000 if (remove_single_exception_chunk(s) < 0)
1001 goto shut;
1002
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001003 snapshot_merge_next_chunks(s);
1004
1005 return;
1006
1007shut:
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001008 down_write(&s->lock);
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +00001009 s->merge_failed = 1;
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001010 b = __release_queued_bios_after_merge(s);
1011 up_write(&s->lock);
1012 error_bios(b);
1013
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001014 merge_shutdown(s);
1015}
1016
1017static void start_merge(struct dm_snapshot *s)
1018{
1019 if (!test_and_set_bit(RUNNING_MERGE, &s->state_bits))
1020 snapshot_merge_next_chunks(s);
1021}
1022
1023static int wait_schedule(void *ptr)
1024{
1025 schedule();
1026
1027 return 0;
1028}
1029
1030/*
1031 * Stop the merging process and wait until it finishes.
1032 */
1033static void stop_merge(struct dm_snapshot *s)
1034{
1035 set_bit(SHUTDOWN_MERGE, &s->state_bits);
1036 wait_on_bit(&s->state_bits, RUNNING_MERGE, wait_schedule,
1037 TASK_UNINTERRUPTIBLE);
1038 clear_bit(SHUTDOWN_MERGE, &s->state_bits);
1039}
1040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 * Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size>
1043 */
1044static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1045{
1046 struct dm_snapshot *s;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001047 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 int r = -EINVAL;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001049 char *origin_path, *cow_path;
Mike Snitzer10b81062009-12-10 23:52:31 +00001050 unsigned args_used, num_flush_requests = 1;
1051 fmode_t origin_mode = FMODE_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -07001053 if (argc != 4) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001054 ti->error = "requires exactly 4 arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 r = -EINVAL;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001056 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
1058
Mike Snitzer10b81062009-12-10 23:52:31 +00001059 if (dm_target_is_snapshot_merge(ti)) {
1060 num_flush_requests = 2;
1061 origin_mode = FMODE_WRITE;
1062 }
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 s = kmalloc(sizeof(*s), GFP_KERNEL);
Jonathan Brassowfee19982009-04-02 19:55:34 +01001065 if (!s) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 ti->error = "Cannot allocate snapshot context private "
1067 "structure";
1068 r = -ENOMEM;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001069 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 }
1071
Mikulas Patockac2411042010-08-12 04:13:51 +01001072 origin_path = argv[0];
1073 argv++;
1074 argc--;
1075
1076 r = dm_get_device(ti, origin_path, origin_mode, &s->origin);
1077 if (r) {
1078 ti->error = "Cannot get origin device";
1079 goto bad_origin;
1080 }
1081
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001082 cow_path = argv[0];
1083 argv++;
1084 argc--;
1085
Nikanth Karthikesan8215d6e2010-03-06 02:32:27 +00001086 r = dm_get_device(ti, cow_path, FMODE_READ | FMODE_WRITE, &s->cow);
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001087 if (r) {
1088 ti->error = "Cannot get COW device";
1089 goto bad_cow;
1090 }
1091
1092 r = dm_exception_store_create(ti, argc, argv, s, &args_used, &s->store);
1093 if (r) {
1094 ti->error = "Couldn't create exception store";
1095 r = -EINVAL;
1096 goto bad_store;
1097 }
1098
1099 argv += args_used;
1100 argc -= args_used;
1101
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001102 s->ti = ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 s->valid = 1;
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001104 s->active = 0;
Mike Snitzerc26655c2009-12-10 23:52:12 +00001105 s->suspended = 0;
Mikulas Patocka879129d22008-10-30 13:33:16 +00001106 atomic_set(&s->pending_exceptions_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 init_rwsem(&s->lock);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001108 INIT_LIST_HEAD(&s->list);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001109 spin_lock_init(&s->pe_lock);
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001110 s->state_bits = 0;
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +00001111 s->merge_failed = 0;
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001112 s->first_merging_chunk = 0;
1113 s->num_merging_chunks = 0;
1114 bio_list_init(&s->bios_queued_during_merge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 /* Allocate hash table for COW data */
Jonathan Brassowfee19982009-04-02 19:55:34 +01001117 if (init_hash_tables(s)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 ti->error = "Unable to allocate hash table space";
1119 r = -ENOMEM;
Jonathan Brassowfee19982009-04-02 19:55:34 +01001120 goto bad_hash_tables;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 }
1122
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001123 r = dm_kcopyd_client_create(SNAPSHOT_PAGES, &s->kcopyd_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 if (r) {
1125 ti->error = "Could not create kcopyd client";
Jonathan Brassowfee19982009-04-02 19:55:34 +01001126 goto bad_kcopyd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
1128
Mikulas Patocka92e86812008-07-21 12:00:35 +01001129 s->pending_pool = mempool_create_slab_pool(MIN_IOS, pending_cache);
1130 if (!s->pending_pool) {
1131 ti->error = "Could not allocate mempool for pending exceptions";
Jonathan Brassowfee19982009-04-02 19:55:34 +01001132 goto bad_pending_pool;
Mikulas Patocka92e86812008-07-21 12:00:35 +01001133 }
1134
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001135 s->tracked_chunk_pool = mempool_create_slab_pool(MIN_IOS,
1136 tracked_chunk_cache);
1137 if (!s->tracked_chunk_pool) {
1138 ti->error = "Could not allocate tracked_chunk mempool for "
1139 "tracking reads";
Mikulas Patocka92e86812008-07-21 12:00:35 +01001140 goto bad_tracked_chunk_pool;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001141 }
1142
1143 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
1144 INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]);
1145
1146 spin_lock_init(&s->tracked_chunk_lock);
1147
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001148 ti->private = s;
Mike Snitzer10b81062009-12-10 23:52:31 +00001149 ti->num_flush_requests = num_flush_requests;
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001150
1151 /* Add snapshot to the list of snapshots for this origin */
1152 /* Exceptions aren't triggered till snapshot_resume() is called */
1153 r = register_snapshot(s);
1154 if (r == -ENOMEM) {
1155 ti->error = "Snapshot origin struct allocation failed";
1156 goto bad_load_and_register;
1157 } else if (r < 0) {
1158 /* invalid handover, register_snapshot has set ti->error */
1159 goto bad_load_and_register;
1160 }
1161
1162 /*
1163 * Metadata must only be loaded into one table at once, so skip this
1164 * if metadata will be handed over during resume.
1165 * Chunk size will be set during the handover - set it to zero to
1166 * ensure it's ignored.
1167 */
1168 if (r > 0) {
1169 s->store->chunk_size = 0;
1170 return 0;
1171 }
1172
Jonathan Brassow493df712009-04-02 19:55:31 +01001173 r = s->store->type->read_metadata(s->store, dm_add_exception,
1174 (void *)s);
Milan Broz07641472007-07-12 17:28:13 +01001175 if (r < 0) {
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -07001176 ti->error = "Failed to read snapshot metadata";
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001177 goto bad_read_metadata;
Milan Broz07641472007-07-12 17:28:13 +01001178 } else if (r > 0) {
1179 s->valid = 0;
1180 DMWARN("Snapshot is marked invalid.");
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -07001181 }
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001182
Mikulas Patocka3f2412d2009-10-16 23:18:16 +01001183 if (!s->store->chunk_size) {
1184 ti->error = "Chunk size not set";
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001185 goto bad_read_metadata;
Mikulas Patocka3f2412d2009-10-16 23:18:16 +01001186 }
Jonathan Brassowd0216842009-04-02 19:55:32 +01001187 ti->split_io = s->store->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 return 0;
1190
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001191bad_read_metadata:
1192 unregister_snapshot(s);
1193
Jonathan Brassowfee19982009-04-02 19:55:34 +01001194bad_load_and_register:
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001195 mempool_destroy(s->tracked_chunk_pool);
1196
Jonathan Brassowfee19982009-04-02 19:55:34 +01001197bad_tracked_chunk_pool:
Mikulas Patocka92e86812008-07-21 12:00:35 +01001198 mempool_destroy(s->pending_pool);
1199
Jonathan Brassowfee19982009-04-02 19:55:34 +01001200bad_pending_pool:
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001201 dm_kcopyd_client_destroy(s->kcopyd_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Jonathan Brassowfee19982009-04-02 19:55:34 +01001203bad_kcopyd:
Jon Brassow3510cb92009-12-10 23:52:11 +00001204 dm_exception_table_exit(&s->pending, pending_cache);
1205 dm_exception_table_exit(&s->complete, exception_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Jonathan Brassowfee19982009-04-02 19:55:34 +01001207bad_hash_tables:
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001208 dm_exception_store_destroy(s->store);
1209
1210bad_store:
1211 dm_put_device(ti, s->cow);
1212
1213bad_cow:
Mikulas Patockac2411042010-08-12 04:13:51 +01001214 dm_put_device(ti, s->origin);
1215
1216bad_origin:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 kfree(s);
1218
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001219bad:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 return r;
1221}
1222
Milan Broz31c93a02006-12-08 02:41:11 -08001223static void __free_exceptions(struct dm_snapshot *s)
1224{
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001225 dm_kcopyd_client_destroy(s->kcopyd_client);
Milan Broz31c93a02006-12-08 02:41:11 -08001226 s->kcopyd_client = NULL;
1227
Jon Brassow3510cb92009-12-10 23:52:11 +00001228 dm_exception_table_exit(&s->pending, pending_cache);
1229 dm_exception_table_exit(&s->complete, exception_cache);
Milan Broz31c93a02006-12-08 02:41:11 -08001230}
1231
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001232static void __handover_exceptions(struct dm_snapshot *snap_src,
1233 struct dm_snapshot *snap_dest)
1234{
1235 union {
1236 struct dm_exception_table table_swap;
1237 struct dm_exception_store *store_swap;
1238 } u;
1239
1240 /*
1241 * Swap all snapshot context information between the two instances.
1242 */
1243 u.table_swap = snap_dest->complete;
1244 snap_dest->complete = snap_src->complete;
1245 snap_src->complete = u.table_swap;
1246
1247 u.store_swap = snap_dest->store;
1248 snap_dest->store = snap_src->store;
1249 snap_src->store = u.store_swap;
1250
1251 snap_dest->store->snap = snap_dest;
1252 snap_src->store->snap = snap_src;
1253
1254 snap_dest->ti->split_io = snap_dest->store->chunk_size;
1255 snap_dest->valid = snap_src->valid;
1256
1257 /*
1258 * Set source invalid to ensure it receives no further I/O.
1259 */
1260 snap_src->valid = 0;
1261}
1262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263static void snapshot_dtr(struct dm_target *ti)
1264{
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001265#ifdef CONFIG_DM_DEBUG
1266 int i;
1267#endif
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001268 struct dm_snapshot *s = ti->private;
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001269 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001271 down_read(&_origins_lock);
1272 /* Check whether exception handover must be cancelled */
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +00001273 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001274 if (snap_src && snap_dest && (s == snap_src)) {
1275 down_write(&snap_dest->lock);
1276 snap_dest->valid = 0;
1277 up_write(&snap_dest->lock);
1278 DMERR("Cancelling snapshot handover.");
1279 }
1280 up_read(&_origins_lock);
1281
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001282 if (dm_target_is_snapshot_merge(ti))
1283 stop_merge(s);
1284
Alasdair G Kergon138728dc2006-03-27 01:17:50 -08001285 /* Prevent further origin writes from using this snapshot. */
1286 /* After this returns there can be no new kcopyd jobs. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 unregister_snapshot(s);
1288
Mikulas Patocka879129d22008-10-30 13:33:16 +00001289 while (atomic_read(&s->pending_exceptions_count))
Mikulas Patocka90fa1522009-01-06 03:04:54 +00001290 msleep(1);
Mikulas Patocka879129d22008-10-30 13:33:16 +00001291 /*
1292 * Ensure instructions in mempool_destroy aren't reordered
1293 * before atomic_read.
1294 */
1295 smp_mb();
1296
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001297#ifdef CONFIG_DM_DEBUG
1298 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
1299 BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i]));
1300#endif
1301
1302 mempool_destroy(s->tracked_chunk_pool);
1303
Milan Broz31c93a02006-12-08 02:41:11 -08001304 __free_exceptions(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Mikulas Patocka92e86812008-07-21 12:00:35 +01001306 mempool_destroy(s->pending_pool);
1307
Jonathan Brassowfee19982009-04-02 19:55:34 +01001308 dm_exception_store_destroy(s->store);
Alasdair G Kergon138728dc2006-03-27 01:17:50 -08001309
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001310 dm_put_device(ti, s->cow);
1311
Mikulas Patockac2411042010-08-12 04:13:51 +01001312 dm_put_device(ti, s->origin);
1313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 kfree(s);
1315}
1316
1317/*
1318 * Flush a list of buffers.
1319 */
1320static void flush_bios(struct bio *bio)
1321{
1322 struct bio *n;
1323
1324 while (bio) {
1325 n = bio->bi_next;
1326 bio->bi_next = NULL;
1327 generic_make_request(bio);
1328 bio = n;
1329 }
1330}
1331
Mikulas Patocka515ad662009-12-10 23:52:30 +00001332static int do_origin(struct dm_dev *origin, struct bio *bio);
1333
1334/*
1335 * Flush a list of buffers.
1336 */
1337static void retry_origin_bios(struct dm_snapshot *s, struct bio *bio)
1338{
1339 struct bio *n;
1340 int r;
1341
1342 while (bio) {
1343 n = bio->bi_next;
1344 bio->bi_next = NULL;
1345 r = do_origin(s->origin, bio);
1346 if (r == DM_MAPIO_REMAPPED)
1347 generic_make_request(bio);
1348 bio = n;
1349 }
1350}
1351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352/*
1353 * Error a list of buffers.
1354 */
1355static void error_bios(struct bio *bio)
1356{
1357 struct bio *n;
1358
1359 while (bio) {
1360 n = bio->bi_next;
1361 bio->bi_next = NULL;
NeilBrown6712ecf2007-09-27 12:47:43 +02001362 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 bio = n;
1364 }
1365}
1366
Alasdair G Kergon695368a2006-10-03 01:15:31 -07001367static void __invalidate_snapshot(struct dm_snapshot *s, int err)
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001368{
1369 if (!s->valid)
1370 return;
1371
1372 if (err == -EIO)
1373 DMERR("Invalidating snapshot: Error reading/writing.");
1374 else if (err == -ENOMEM)
1375 DMERR("Invalidating snapshot: Unable to allocate exception.");
1376
Jonathan Brassow493df712009-04-02 19:55:31 +01001377 if (s->store->type->drop_snapshot)
1378 s->store->type->drop_snapshot(s->store);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001379
1380 s->valid = 0;
1381
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001382 dm_table_event(s->ti->table);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001383}
1384
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001385static void pending_complete(struct dm_snap_pending_exception *pe, int success)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
Jon Brassow1d4989c2009-12-10 23:52:10 +00001387 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 struct dm_snapshot *s = pe->snap;
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001389 struct bio *origin_bios = NULL;
1390 struct bio *snapshot_bios = NULL;
1391 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001393 if (!success) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 /* Read/write error - snapshot is unusable */
1395 down_write(&s->lock);
Alasdair G Kergon695368a2006-10-03 01:15:31 -07001396 __invalidate_snapshot(s, -EIO);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001397 error = 1;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001398 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 }
1400
Jon Brassow3510cb92009-12-10 23:52:11 +00001401 e = alloc_completed_exception();
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001402 if (!e) {
1403 down_write(&s->lock);
Alasdair G Kergon695368a2006-10-03 01:15:31 -07001404 __invalidate_snapshot(s, -ENOMEM);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001405 error = 1;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001406 goto out;
1407 }
1408 *e = pe->e;
1409
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001410 down_write(&s->lock);
1411 if (!s->valid) {
Jon Brassow3510cb92009-12-10 23:52:11 +00001412 free_completed_exception(e);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001413 error = 1;
1414 goto out;
1415 }
1416
Mike Snitzer615d1eb2009-12-10 23:52:29 +00001417 /* Check for conflicting reads */
1418 __check_for_conflicting_io(s, pe->e.old_chunk);
Mikulas Patockaa8d41b52008-07-21 12:00:34 +01001419
1420 /*
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001421 * Add a proper exception, and remove the
1422 * in-flight exception from the list.
1423 */
Jon Brassow3510cb92009-12-10 23:52:11 +00001424 dm_insert_exception(&s->complete, e);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 out:
Jon Brassow3510cb92009-12-10 23:52:11 +00001427 dm_remove_exception(&pe->e);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001428 snapshot_bios = bio_list_get(&pe->snapshot_bios);
Mikulas Patocka515ad662009-12-10 23:52:30 +00001429 origin_bios = bio_list_get(&pe->origin_bios);
1430 free_pending_exception(pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Mikulas Patocka73dfd072009-12-10 23:52:34 +00001432 increment_pending_exceptions_done_count();
1433
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001434 up_write(&s->lock);
1435
1436 /* Submit any pending write bios */
1437 if (error)
1438 error_bios(snapshot_bios);
1439 else
1440 flush_bios(snapshot_bios);
1441
Mikulas Patocka515ad662009-12-10 23:52:30 +00001442 retry_origin_bios(s, origin_bios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443}
1444
1445static void commit_callback(void *context, int success)
1446{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001447 struct dm_snap_pending_exception *pe = context;
1448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 pending_complete(pe, success);
1450}
1451
1452/*
1453 * Called when the copy I/O has finished. kcopyd actually runs
1454 * this code so don't block.
1455 */
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -07001456static void copy_callback(int read_err, unsigned long write_err, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001458 struct dm_snap_pending_exception *pe = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 struct dm_snapshot *s = pe->snap;
1460
1461 if (read_err || write_err)
1462 pending_complete(pe, 0);
1463
1464 else
1465 /* Update the metadata if we are persistent */
Jonathan Brassow493df712009-04-02 19:55:31 +01001466 s->store->type->commit_exception(s->store, &pe->e,
1467 commit_callback, pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
1470/*
1471 * Dispatches the copy operation to kcopyd.
1472 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001473static void start_copy(struct dm_snap_pending_exception *pe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
1475 struct dm_snapshot *s = pe->snap;
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +01001476 struct dm_io_region src, dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 struct block_device *bdev = s->origin->bdev;
1478 sector_t dev_size;
1479
1480 dev_size = get_dev_size(bdev);
1481
1482 src.bdev = bdev;
Jonathan Brassow71fab002009-04-02 19:55:33 +01001483 src.sector = chunk_to_sector(s->store, pe->e.old_chunk);
Mikulas Patockadf96eee2009-10-16 23:18:17 +01001484 src.count = min((sector_t)s->store->chunk_size, dev_size - src.sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001486 dest.bdev = s->cow->bdev;
Jonathan Brassow71fab002009-04-02 19:55:33 +01001487 dest.sector = chunk_to_sector(s->store, pe->e.new_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 dest.count = src.count;
1489
1490 /* Hand over to kcopyd */
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001491 dm_kcopyd_copy(s->kcopyd_client,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 &src, 1, &dest, 0, copy_callback, pe);
1493}
1494
Mikulas Patocka29138082009-04-02 19:55:25 +01001495static struct dm_snap_pending_exception *
1496__lookup_pending_exception(struct dm_snapshot *s, chunk_t chunk)
1497{
Jon Brassow3510cb92009-12-10 23:52:11 +00001498 struct dm_exception *e = dm_lookup_exception(&s->pending, chunk);
Mikulas Patocka29138082009-04-02 19:55:25 +01001499
1500 if (!e)
1501 return NULL;
1502
1503 return container_of(e, struct dm_snap_pending_exception, e);
1504}
1505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506/*
1507 * Looks to see if this snapshot already has a pending exception
1508 * for this chunk, otherwise it allocates a new one and inserts
1509 * it into the pending table.
1510 *
1511 * NOTE: a write lock must be held on snap->lock before calling
1512 * this.
1513 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001514static struct dm_snap_pending_exception *
Mikulas Patockac6621392009-04-02 19:55:25 +01001515__find_pending_exception(struct dm_snapshot *s,
1516 struct dm_snap_pending_exception *pe, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
Mikulas Patockac6621392009-04-02 19:55:25 +01001518 struct dm_snap_pending_exception *pe2;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001519
Mikulas Patocka29138082009-04-02 19:55:25 +01001520 pe2 = __lookup_pending_exception(s, chunk);
1521 if (pe2) {
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001522 free_pending_exception(pe);
Mikulas Patocka29138082009-04-02 19:55:25 +01001523 return pe2;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001524 }
1525
1526 pe->e.old_chunk = chunk;
1527 bio_list_init(&pe->origin_bios);
1528 bio_list_init(&pe->snapshot_bios);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001529 pe->started = 0;
1530
Jonathan Brassow493df712009-04-02 19:55:31 +01001531 if (s->store->type->prepare_exception(s->store, &pe->e)) {
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001532 free_pending_exception(pe);
1533 return NULL;
1534 }
1535
Jon Brassow3510cb92009-12-10 23:52:11 +00001536 dm_insert_exception(&s->pending, &pe->e);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 return pe;
1539}
1540
Jon Brassow1d4989c2009-12-10 23:52:10 +00001541static void remap_exception(struct dm_snapshot *s, struct dm_exception *e,
Milan Brozd74f81f2008-02-08 02:11:27 +00001542 struct bio *bio, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001544 bio->bi_bdev = s->cow->bdev;
Jonathan Brassow71fab002009-04-02 19:55:33 +01001545 bio->bi_sector = chunk_to_sector(s->store,
1546 dm_chunk_number(e->new_chunk) +
1547 (chunk - e->old_chunk)) +
1548 (bio->bi_sector &
1549 s->store->chunk_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550}
1551
1552static int snapshot_map(struct dm_target *ti, struct bio *bio,
1553 union map_info *map_context)
1554{
Jon Brassow1d4989c2009-12-10 23:52:10 +00001555 struct dm_exception *e;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001556 struct dm_snapshot *s = ti->private;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001557 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 chunk_t chunk;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001559 struct dm_snap_pending_exception *pe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Tejun Heod87f4c12010-09-03 11:56:19 +02001561 if (bio->bi_rw & REQ_FLUSH) {
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001562 bio->bi_bdev = s->cow->bdev;
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01001563 return DM_MAPIO_REMAPPED;
1564 }
1565
Jonathan Brassow71fab002009-04-02 19:55:33 +01001566 chunk = sector_to_chunk(s->store, bio->bi_sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
1568 /* Full snapshots are not usable */
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001569 /* To get here the table must be live so s->active is always set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 if (!s->valid)
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -07001571 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001573 /* FIXME: should only take write lock if we need
1574 * to copy an exception */
1575 down_write(&s->lock);
1576
1577 if (!s->valid) {
1578 r = -EIO;
1579 goto out_unlock;
1580 }
1581
1582 /* If the block is already remapped - use that, else remap it */
Jon Brassow3510cb92009-12-10 23:52:11 +00001583 e = dm_lookup_exception(&s->complete, chunk);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001584 if (e) {
Milan Brozd74f81f2008-02-08 02:11:27 +00001585 remap_exception(s, e, bio, chunk);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001586 goto out_unlock;
1587 }
1588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 /*
1590 * Write to snapshot - higher level takes care of RW/RO
1591 * flags so we should only get this if we are
1592 * writeable.
1593 */
1594 if (bio_rw(bio) == WRITE) {
Mikulas Patocka29138082009-04-02 19:55:25 +01001595 pe = __lookup_pending_exception(s, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001596 if (!pe) {
Mikulas Patockac6621392009-04-02 19:55:25 +01001597 up_write(&s->lock);
1598 pe = alloc_pending_exception(s);
1599 down_write(&s->lock);
1600
1601 if (!s->valid) {
1602 free_pending_exception(pe);
1603 r = -EIO;
1604 goto out_unlock;
1605 }
1606
Jon Brassow3510cb92009-12-10 23:52:11 +00001607 e = dm_lookup_exception(&s->complete, chunk);
Mikulas Patocka35bf6592009-04-02 19:55:26 +01001608 if (e) {
1609 free_pending_exception(pe);
1610 remap_exception(s, e, bio, chunk);
1611 goto out_unlock;
1612 }
1613
Mikulas Patockac6621392009-04-02 19:55:25 +01001614 pe = __find_pending_exception(s, pe, chunk);
Mikulas Patocka29138082009-04-02 19:55:25 +01001615 if (!pe) {
1616 __invalidate_snapshot(s, -ENOMEM);
1617 r = -EIO;
1618 goto out_unlock;
1619 }
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001620 }
1621
Milan Brozd74f81f2008-02-08 02:11:27 +00001622 remap_exception(s, &pe->e, bio, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001623 bio_list_add(&pe->snapshot_bios, bio);
1624
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001625 r = DM_MAPIO_SUBMITTED;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001626
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001627 if (!pe->started) {
1628 /* this is protected by snap->lock */
1629 pe->started = 1;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001630 up_write(&s->lock);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001631 start_copy(pe);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001632 goto out;
1633 }
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001634 } else {
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001635 bio->bi_bdev = s->origin->bdev;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001636 map_context->ptr = track_chunk(s, chunk);
1637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001639 out_unlock:
1640 up_write(&s->lock);
1641 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 return r;
1643}
1644
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001645/*
1646 * A snapshot-merge target behaves like a combination of a snapshot
1647 * target and a snapshot-origin target. It only generates new
1648 * exceptions in other snapshots and not in the one that is being
1649 * merged.
1650 *
1651 * For each chunk, if there is an existing exception, it is used to
1652 * redirect I/O to the cow device. Otherwise I/O is sent to the origin,
1653 * which in turn might generate exceptions in other snapshots.
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001654 * If merging is currently taking place on the chunk in question, the
1655 * I/O is deferred by adding it to s->bios_queued_during_merge.
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001656 */
1657static int snapshot_merge_map(struct dm_target *ti, struct bio *bio,
1658 union map_info *map_context)
1659{
1660 struct dm_exception *e;
1661 struct dm_snapshot *s = ti->private;
1662 int r = DM_MAPIO_REMAPPED;
1663 chunk_t chunk;
1664
Tejun Heod87f4c12010-09-03 11:56:19 +02001665 if (bio->bi_rw & REQ_FLUSH) {
Mike Snitzer57cba5d2010-08-12 04:14:04 +01001666 if (!map_context->target_request_nr)
Mike Snitzer10b81062009-12-10 23:52:31 +00001667 bio->bi_bdev = s->origin->bdev;
1668 else
1669 bio->bi_bdev = s->cow->bdev;
1670 map_context->ptr = NULL;
1671 return DM_MAPIO_REMAPPED;
1672 }
1673
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001674 chunk = sector_to_chunk(s->store, bio->bi_sector);
1675
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001676 down_write(&s->lock);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001677
Mikulas Patockad2fdb772009-12-10 23:52:36 +00001678 /* Full merging snapshots are redirected to the origin */
1679 if (!s->valid)
1680 goto redirect_to_origin;
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001681
1682 /* If the block is already remapped - use that */
1683 e = dm_lookup_exception(&s->complete, chunk);
1684 if (e) {
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001685 /* Queue writes overlapping with chunks being merged */
1686 if (bio_rw(bio) == WRITE &&
1687 chunk >= s->first_merging_chunk &&
1688 chunk < (s->first_merging_chunk +
1689 s->num_merging_chunks)) {
1690 bio->bi_bdev = s->origin->bdev;
1691 bio_list_add(&s->bios_queued_during_merge, bio);
1692 r = DM_MAPIO_SUBMITTED;
1693 goto out_unlock;
1694 }
Mikulas Patocka17aa0332009-12-10 23:52:33 +00001695
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001696 remap_exception(s, e, bio, chunk);
Mikulas Patocka17aa0332009-12-10 23:52:33 +00001697
1698 if (bio_rw(bio) == WRITE)
1699 map_context->ptr = track_chunk(s, chunk);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001700 goto out_unlock;
1701 }
1702
Mikulas Patockad2fdb772009-12-10 23:52:36 +00001703redirect_to_origin:
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001704 bio->bi_bdev = s->origin->bdev;
1705
1706 if (bio_rw(bio) == WRITE) {
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001707 up_write(&s->lock);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001708 return do_origin(s->origin, bio);
1709 }
1710
1711out_unlock:
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001712 up_write(&s->lock);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001713
1714 return r;
1715}
1716
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001717static int snapshot_end_io(struct dm_target *ti, struct bio *bio,
1718 int error, union map_info *map_context)
1719{
1720 struct dm_snapshot *s = ti->private;
1721 struct dm_snap_tracked_chunk *c = map_context->ptr;
1722
1723 if (c)
1724 stop_tracking_chunk(s, c);
1725
1726 return 0;
1727}
1728
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001729static void snapshot_merge_presuspend(struct dm_target *ti)
1730{
1731 struct dm_snapshot *s = ti->private;
1732
1733 stop_merge(s);
1734}
1735
Mike Snitzerc26655c2009-12-10 23:52:12 +00001736static void snapshot_postsuspend(struct dm_target *ti)
1737{
1738 struct dm_snapshot *s = ti->private;
1739
1740 down_write(&s->lock);
1741 s->suspended = 1;
1742 up_write(&s->lock);
1743}
1744
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001745static int snapshot_preresume(struct dm_target *ti)
1746{
1747 int r = 0;
1748 struct dm_snapshot *s = ti->private;
1749 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
1750
1751 down_read(&_origins_lock);
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +00001752 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001753 if (snap_src && snap_dest) {
1754 down_read(&snap_src->lock);
1755 if (s == snap_src) {
1756 DMERR("Unable to resume snapshot source until "
1757 "handover completes.");
1758 r = -EINVAL;
1759 } else if (!snap_src->suspended) {
1760 DMERR("Unable to perform snapshot handover until "
1761 "source is suspended.");
1762 r = -EINVAL;
1763 }
1764 up_read(&snap_src->lock);
1765 }
1766 up_read(&_origins_lock);
1767
1768 return r;
1769}
1770
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771static void snapshot_resume(struct dm_target *ti)
1772{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001773 struct dm_snapshot *s = ti->private;
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001774 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
1775
1776 down_read(&_origins_lock);
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +00001777 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001778 if (snap_src && snap_dest) {
1779 down_write(&snap_src->lock);
1780 down_write_nested(&snap_dest->lock, SINGLE_DEPTH_NESTING);
1781 __handover_exceptions(snap_src, snap_dest);
1782 up_write(&snap_dest->lock);
1783 up_write(&snap_src->lock);
1784 }
1785 up_read(&_origins_lock);
1786
1787 /* Now we have correct chunk size, reregister */
1788 reregister_snapshot(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001790 down_write(&s->lock);
1791 s->active = 1;
Mike Snitzerc26655c2009-12-10 23:52:12 +00001792 s->suspended = 0;
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001793 up_write(&s->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794}
1795
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001796static sector_t get_origin_minimum_chunksize(struct block_device *bdev)
1797{
1798 sector_t min_chunksize;
1799
1800 down_read(&_origins_lock);
1801 min_chunksize = __minimum_chunk_size(__lookup_origin(bdev));
1802 up_read(&_origins_lock);
1803
1804 return min_chunksize;
1805}
1806
1807static void snapshot_merge_resume(struct dm_target *ti)
1808{
1809 struct dm_snapshot *s = ti->private;
1810
1811 /*
1812 * Handover exceptions from existing snapshot.
1813 */
1814 snapshot_resume(ti);
1815
1816 /*
1817 * snapshot-merge acts as an origin, so set ti->split_io
1818 */
1819 ti->split_io = get_origin_minimum_chunksize(s->origin->bdev);
1820
1821 start_merge(s);
1822}
1823
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824static int snapshot_status(struct dm_target *ti, status_type_t type,
1825 char *result, unsigned int maxlen)
1826{
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01001827 unsigned sz = 0;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001828 struct dm_snapshot *snap = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
1830 switch (type) {
1831 case STATUSTYPE_INFO:
Mikulas Patocka94e765722009-12-10 23:51:53 +00001832
1833 down_write(&snap->lock);
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 if (!snap->valid)
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01001836 DMEMIT("Invalid");
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +00001837 else if (snap->merge_failed)
1838 DMEMIT("Merge failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 else {
Mike Snitzer985903b2009-12-10 23:52:11 +00001840 if (snap->store->type->usage) {
1841 sector_t total_sectors, sectors_allocated,
1842 metadata_sectors;
1843 snap->store->type->usage(snap->store,
1844 &total_sectors,
1845 &sectors_allocated,
1846 &metadata_sectors);
1847 DMEMIT("%llu/%llu %llu",
1848 (unsigned long long)sectors_allocated,
1849 (unsigned long long)total_sectors,
1850 (unsigned long long)metadata_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 }
1852 else
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01001853 DMEMIT("Unknown");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 }
Mikulas Patocka94e765722009-12-10 23:51:53 +00001855
1856 up_write(&snap->lock);
1857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 break;
1859
1860 case STATUSTYPE_TABLE:
1861 /*
1862 * kdevname returns a static pointer so we need
1863 * to make private copies if the output is to
1864 * make sense.
1865 */
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001866 DMEMIT("%s %s", snap->origin->name, snap->cow->name);
Jonathan Brassow1e302a92009-04-02 19:55:35 +01001867 snap->store->type->status(snap->store, type, result + sz,
1868 maxlen - sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 break;
1870 }
1871
1872 return 0;
1873}
1874
Mike Snitzer8811f462009-09-04 20:40:19 +01001875static int snapshot_iterate_devices(struct dm_target *ti,
1876 iterate_devices_callout_fn fn, void *data)
1877{
1878 struct dm_snapshot *snap = ti->private;
Mikulas Patocka1e5554c2010-08-12 04:13:50 +01001879 int r;
Mike Snitzer8811f462009-09-04 20:40:19 +01001880
Mikulas Patocka1e5554c2010-08-12 04:13:50 +01001881 r = fn(ti, snap->origin, 0, ti->len, data);
1882
1883 if (!r)
1884 r = fn(ti, snap->cow, 0, get_dev_size(snap->cow->bdev), data);
1885
1886 return r;
Mike Snitzer8811f462009-09-04 20:40:19 +01001887}
1888
1889
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890/*-----------------------------------------------------------------
1891 * Origin methods
1892 *---------------------------------------------------------------*/
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00001893
1894/*
1895 * If no exceptions need creating, DM_MAPIO_REMAPPED is returned and any
1896 * supplied bio was ignored. The caller may submit it immediately.
1897 * (No remapping actually occurs as the origin is always a direct linear
1898 * map.)
1899 *
1900 * If further exceptions are required, DM_MAPIO_SUBMITTED is returned
1901 * and any supplied bio is added to a list to be submitted once all
1902 * the necessary exceptions exist.
1903 */
1904static int __origin_write(struct list_head *snapshots, sector_t sector,
1905 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906{
Mikulas Patocka515ad662009-12-10 23:52:30 +00001907 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 struct dm_snapshot *snap;
Jon Brassow1d4989c2009-12-10 23:52:10 +00001909 struct dm_exception *e;
Mikulas Patocka515ad662009-12-10 23:52:30 +00001910 struct dm_snap_pending_exception *pe;
1911 struct dm_snap_pending_exception *pe_to_start_now = NULL;
1912 struct dm_snap_pending_exception *pe_to_start_last = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 chunk_t chunk;
1914
1915 /* Do all the snapshots on this origin */
1916 list_for_each_entry (snap, snapshots, list) {
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001917 /*
1918 * Don't make new exceptions in a merging snapshot
1919 * because it has effectively been deleted
1920 */
1921 if (dm_target_is_snapshot_merge(snap->ti))
1922 continue;
1923
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001924 down_write(&snap->lock);
1925
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001926 /* Only deal with valid and active snapshots */
1927 if (!snap->valid || !snap->active)
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001928 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
Alasdair G Kergond5e404c2005-07-12 15:53:05 -07001930 /* Nothing to do if writing beyond end of snapshot */
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00001931 if (sector >= dm_table_get_size(snap->ti->table))
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001932 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
1934 /*
1935 * Remember, different snapshots can have
1936 * different chunk sizes.
1937 */
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00001938 chunk = sector_to_chunk(snap->store, sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
1940 /*
1941 * Check exception table to see if block
1942 * is already remapped in this snapshot
1943 * and trigger an exception if not.
1944 */
Jon Brassow3510cb92009-12-10 23:52:11 +00001945 e = dm_lookup_exception(&snap->complete, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001946 if (e)
1947 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Mikulas Patocka29138082009-04-02 19:55:25 +01001949 pe = __lookup_pending_exception(snap, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001950 if (!pe) {
Mikulas Patockac6621392009-04-02 19:55:25 +01001951 up_write(&snap->lock);
1952 pe = alloc_pending_exception(snap);
1953 down_write(&snap->lock);
1954
1955 if (!snap->valid) {
1956 free_pending_exception(pe);
1957 goto next_snapshot;
1958 }
1959
Jon Brassow3510cb92009-12-10 23:52:11 +00001960 e = dm_lookup_exception(&snap->complete, chunk);
Mikulas Patocka35bf6592009-04-02 19:55:26 +01001961 if (e) {
1962 free_pending_exception(pe);
1963 goto next_snapshot;
1964 }
1965
Mikulas Patockac6621392009-04-02 19:55:25 +01001966 pe = __find_pending_exception(snap, pe, chunk);
Mikulas Patocka29138082009-04-02 19:55:25 +01001967 if (!pe) {
1968 __invalidate_snapshot(snap, -ENOMEM);
1969 goto next_snapshot;
1970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 }
1972
Mikulas Patocka515ad662009-12-10 23:52:30 +00001973 r = DM_MAPIO_SUBMITTED;
1974
1975 /*
1976 * If an origin bio was supplied, queue it to wait for the
1977 * completion of this exception, and start this one last,
1978 * at the end of the function.
1979 */
1980 if (bio) {
1981 bio_list_add(&pe->origin_bios, bio);
1982 bio = NULL;
1983
1984 if (!pe->started) {
1985 pe->started = 1;
1986 pe_to_start_last = pe;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001987 }
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001988 }
1989
1990 if (!pe->started) {
1991 pe->started = 1;
Mikulas Patocka515ad662009-12-10 23:52:30 +00001992 pe_to_start_now = pe;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001993 }
1994
1995 next_snapshot:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 up_write(&snap->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Mikulas Patocka515ad662009-12-10 23:52:30 +00001998 if (pe_to_start_now) {
1999 start_copy(pe_to_start_now);
2000 pe_to_start_now = NULL;
2001 }
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08002002 }
2003
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 /*
Mikulas Patocka515ad662009-12-10 23:52:30 +00002005 * Submit the exception against which the bio is queued last,
2006 * to give the other exceptions a head start.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 */
Mikulas Patocka515ad662009-12-10 23:52:30 +00002008 if (pe_to_start_last)
2009 start_copy(pe_to_start_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
2011 return r;
2012}
2013
2014/*
2015 * Called on a write from the origin driver.
2016 */
2017static int do_origin(struct dm_dev *origin, struct bio *bio)
2018{
2019 struct origin *o;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08002020 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
2022 down_read(&_origins_lock);
2023 o = __lookup_origin(origin->bdev);
2024 if (o)
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00002025 r = __origin_write(&o->snapshots, bio->bi_sector, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 up_read(&_origins_lock);
2027
2028 return r;
2029}
2030
2031/*
Mikulas Patocka73dfd072009-12-10 23:52:34 +00002032 * Trigger exceptions in all non-merging snapshots.
2033 *
2034 * The chunk size of the merging snapshot may be larger than the chunk
2035 * size of some other snapshot so we may need to reallocate multiple
2036 * chunks in other snapshots.
2037 *
2038 * We scan all the overlapping exceptions in the other snapshots.
2039 * Returns 1 if anything was reallocated and must be waited for,
2040 * otherwise returns 0.
2041 *
2042 * size must be a multiple of merging_snap's chunk_size.
2043 */
2044static int origin_write_extent(struct dm_snapshot *merging_snap,
2045 sector_t sector, unsigned size)
2046{
2047 int must_wait = 0;
2048 sector_t n;
2049 struct origin *o;
2050
2051 /*
2052 * The origin's __minimum_chunk_size() got stored in split_io
2053 * by snapshot_merge_resume().
2054 */
2055 down_read(&_origins_lock);
2056 o = __lookup_origin(merging_snap->origin->bdev);
2057 for (n = 0; n < size; n += merging_snap->ti->split_io)
2058 if (__origin_write(&o->snapshots, sector + n, NULL) ==
2059 DM_MAPIO_SUBMITTED)
2060 must_wait = 1;
2061 up_read(&_origins_lock);
2062
2063 return must_wait;
2064}
2065
2066/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 * Origin: maps a linear range of a device, with hooks for snapshotting.
2068 */
2069
2070/*
2071 * Construct an origin mapping: <dev_path>
2072 * The context for an origin is merely a 'struct dm_dev *'
2073 * pointing to the real device.
2074 */
2075static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
2076{
2077 int r;
2078 struct dm_dev *dev;
2079
2080 if (argc != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002081 ti->error = "origin: incorrect number of arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 return -EINVAL;
2083 }
2084
Nikanth Karthikesan8215d6e2010-03-06 02:32:27 +00002085 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 if (r) {
2087 ti->error = "Cannot get target device";
2088 return r;
2089 }
2090
2091 ti->private = dev;
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01002092 ti->num_flush_requests = 1;
2093
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 return 0;
2095}
2096
2097static void origin_dtr(struct dm_target *ti)
2098{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002099 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 dm_put_device(ti, dev);
2101}
2102
2103static int origin_map(struct dm_target *ti, struct bio *bio,
2104 union map_info *map_context)
2105{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002106 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 bio->bi_bdev = dev->bdev;
2108
Tejun Heod87f4c12010-09-03 11:56:19 +02002109 if (bio->bi_rw & REQ_FLUSH)
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01002110 return DM_MAPIO_REMAPPED;
2111
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 /* Only tell snapshots if this is a write */
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08002113 return (bio_rw(bio) == WRITE) ? do_origin(dev, bio) : DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114}
2115
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116/*
2117 * Set the target "split_io" field to the minimum of all the snapshots'
2118 * chunk sizes.
2119 */
2120static void origin_resume(struct dm_target *ti)
2121{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002122 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002124 ti->split_io = get_origin_minimum_chunksize(dev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125}
2126
2127static int origin_status(struct dm_target *ti, status_type_t type, char *result,
2128 unsigned int maxlen)
2129{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002130 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
2132 switch (type) {
2133 case STATUSTYPE_INFO:
2134 result[0] = '\0';
2135 break;
2136
2137 case STATUSTYPE_TABLE:
2138 snprintf(result, maxlen, "%s", dev->name);
2139 break;
2140 }
2141
2142 return 0;
2143}
2144
Mikulas Patockab1d55522010-08-12 04:14:02 +01002145static int origin_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
2146 struct bio_vec *biovec, int max_size)
2147{
2148 struct dm_dev *dev = ti->private;
2149 struct request_queue *q = bdev_get_queue(dev->bdev);
2150
2151 if (!q->merge_bvec_fn)
2152 return max_size;
2153
2154 bvm->bi_bdev = dev->bdev;
2155 bvm->bi_sector = bvm->bi_sector;
2156
2157 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
2158}
2159
Mike Snitzer8811f462009-09-04 20:40:19 +01002160static int origin_iterate_devices(struct dm_target *ti,
2161 iterate_devices_callout_fn fn, void *data)
2162{
2163 struct dm_dev *dev = ti->private;
2164
2165 return fn(ti, dev, 0, ti->len, data);
2166}
2167
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168static struct target_type origin_target = {
2169 .name = "snapshot-origin",
Mike Snitzer8811f462009-09-04 20:40:19 +01002170 .version = {1, 7, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 .module = THIS_MODULE,
2172 .ctr = origin_ctr,
2173 .dtr = origin_dtr,
2174 .map = origin_map,
2175 .resume = origin_resume,
2176 .status = origin_status,
Mikulas Patockab1d55522010-08-12 04:14:02 +01002177 .merge = origin_merge,
Mike Snitzer8811f462009-09-04 20:40:19 +01002178 .iterate_devices = origin_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179};
2180
2181static struct target_type snapshot_target = {
2182 .name = "snapshot",
Mike Snitzerc26655c2009-12-10 23:52:12 +00002183 .version = {1, 9, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 .module = THIS_MODULE,
2185 .ctr = snapshot_ctr,
2186 .dtr = snapshot_dtr,
2187 .map = snapshot_map,
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002188 .end_io = snapshot_end_io,
Mike Snitzerc26655c2009-12-10 23:52:12 +00002189 .postsuspend = snapshot_postsuspend,
Mike Snitzerc1f0c182009-12-10 23:52:24 +00002190 .preresume = snapshot_preresume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 .resume = snapshot_resume,
2192 .status = snapshot_status,
Mike Snitzer8811f462009-09-04 20:40:19 +01002193 .iterate_devices = snapshot_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194};
2195
Mikulas Patockad698aa42009-12-10 23:52:30 +00002196static struct target_type merge_target = {
2197 .name = dm_snapshot_merge_target_name,
2198 .version = {1, 0, 0},
2199 .module = THIS_MODULE,
2200 .ctr = snapshot_ctr,
2201 .dtr = snapshot_dtr,
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00002202 .map = snapshot_merge_map,
Mikulas Patockad698aa42009-12-10 23:52:30 +00002203 .end_io = snapshot_end_io,
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002204 .presuspend = snapshot_merge_presuspend,
Mikulas Patockad698aa42009-12-10 23:52:30 +00002205 .postsuspend = snapshot_postsuspend,
2206 .preresume = snapshot_preresume,
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002207 .resume = snapshot_merge_resume,
Mikulas Patockad698aa42009-12-10 23:52:30 +00002208 .status = snapshot_status,
2209 .iterate_devices = snapshot_iterate_devices,
2210};
2211
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212static int __init dm_snapshot_init(void)
2213{
2214 int r;
2215
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00002216 r = dm_exception_store_init();
2217 if (r) {
2218 DMERR("Failed to initialize exception stores");
2219 return r;
2220 }
2221
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 r = dm_register_target(&snapshot_target);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002223 if (r < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 DMERR("snapshot target register failed %d", r);
Jonathan Brassow034a1862009-10-16 23:18:14 +01002225 goto bad_register_snapshot_target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 }
2227
2228 r = dm_register_target(&origin_target);
2229 if (r < 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002230 DMERR("Origin target register failed %d", r);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002231 goto bad_register_origin_target;
2232 }
2233
2234 r = dm_register_target(&merge_target);
2235 if (r < 0) {
2236 DMERR("Merge target register failed %d", r);
2237 goto bad_register_merge_target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 }
2239
2240 r = init_origin_hash();
2241 if (r) {
2242 DMERR("init_origin_hash failed.");
Mikulas Patockad698aa42009-12-10 23:52:30 +00002243 goto bad_origin_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 }
2245
Jon Brassow1d4989c2009-12-10 23:52:10 +00002246 exception_cache = KMEM_CACHE(dm_exception, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 if (!exception_cache) {
2248 DMERR("Couldn't create exception cache.");
2249 r = -ENOMEM;
Mikulas Patockad698aa42009-12-10 23:52:30 +00002250 goto bad_exception_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 }
2252
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002253 pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 if (!pending_cache) {
2255 DMERR("Couldn't create pending cache.");
2256 r = -ENOMEM;
Mikulas Patockad698aa42009-12-10 23:52:30 +00002257 goto bad_pending_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 }
2259
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002260 tracked_chunk_cache = KMEM_CACHE(dm_snap_tracked_chunk, 0);
2261 if (!tracked_chunk_cache) {
2262 DMERR("Couldn't create cache to track chunks in use.");
2263 r = -ENOMEM;
Mikulas Patockad698aa42009-12-10 23:52:30 +00002264 goto bad_tracked_chunk_cache;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002265 }
2266
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 return 0;
2268
Mikulas Patockad698aa42009-12-10 23:52:30 +00002269bad_tracked_chunk_cache:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 kmem_cache_destroy(pending_cache);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002271bad_pending_cache:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 kmem_cache_destroy(exception_cache);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002273bad_exception_cache:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 exit_origin_hash();
Mikulas Patockad698aa42009-12-10 23:52:30 +00002275bad_origin_hash:
2276 dm_unregister_target(&merge_target);
2277bad_register_merge_target:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 dm_unregister_target(&origin_target);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002279bad_register_origin_target:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 dm_unregister_target(&snapshot_target);
Jonathan Brassow034a1862009-10-16 23:18:14 +01002281bad_register_snapshot_target:
2282 dm_exception_store_exit();
Mikulas Patockad698aa42009-12-10 23:52:30 +00002283
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 return r;
2285}
2286
2287static void __exit dm_snapshot_exit(void)
2288{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00002289 dm_unregister_target(&snapshot_target);
2290 dm_unregister_target(&origin_target);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002291 dm_unregister_target(&merge_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
2293 exit_origin_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 kmem_cache_destroy(pending_cache);
2295 kmem_cache_destroy(exception_cache);
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002296 kmem_cache_destroy(tracked_chunk_cache);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00002297
2298 dm_exception_store_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299}
2300
2301/* Module hooks */
2302module_init(dm_snapshot_init);
2303module_exit(dm_snapshot_exit);
2304
2305MODULE_DESCRIPTION(DM_NAME " snapshot target");
2306MODULE_AUTHOR("Joe Thornber");
2307MODULE_LICENSE("GPL");