blob: 5a2296de84a3d900db6e176a8ac049cbd5ee04f6 [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/*
Mikulas Patockacd45daf2008-07-21 12:00:32 +010043 * The size of the mempool used to track chunks in use.
44 */
45#define MIN_IOS 256
46
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010047#define DM_TRACKED_CHUNK_HASH_SIZE 16
48#define DM_TRACKED_CHUNK_HASH(x) ((unsigned long)(x) & \
49 (DM_TRACKED_CHUNK_HASH_SIZE - 1))
50
Jon Brassow191437a2009-12-10 23:52:10 +000051struct dm_exception_table {
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010052 uint32_t hash_mask;
53 unsigned hash_shift;
54 struct list_head *table;
55};
56
57struct dm_snapshot {
58 struct rw_semaphore lock;
59
60 struct dm_dev *origin;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +000061 struct dm_dev *cow;
62
63 struct dm_target *ti;
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010064
65 /* List of snapshots per Origin */
66 struct list_head list;
67
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +000068 /*
69 * You can't use a snapshot if this is 0 (e.g. if full).
70 * A snapshot-merge target never clears this.
71 */
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010072 int valid;
73
74 /* Origin writes don't trigger exceptions until this is set */
75 int active;
76
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010077 atomic_t pending_exceptions_count;
78
Mike Snitzer924e6002010-03-06 02:32:33 +000079 mempool_t *pending_pool;
80
Jon Brassow191437a2009-12-10 23:52:10 +000081 struct dm_exception_table pending;
82 struct dm_exception_table complete;
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010083
84 /*
85 * pe_lock protects all pending_exception operations and access
86 * as well as the snapshot_bios list.
87 */
88 spinlock_t pe_lock;
89
Mike Snitzer924e6002010-03-06 02:32:33 +000090 /* Chunks with outstanding reads */
91 spinlock_t tracked_chunk_lock;
92 mempool_t *tracked_chunk_pool;
93 struct hlist_head tracked_chunk_hash[DM_TRACKED_CHUNK_HASH_SIZE];
94
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010095 /* The on disk metadata handler */
96 struct dm_exception_store *store;
97
98 struct dm_kcopyd_client *kcopyd_client;
99
Mike Snitzer924e6002010-03-06 02:32:33 +0000100 /* Wait for events based on state_bits */
101 unsigned long state_bits;
102
103 /* Range of chunks currently being merged. */
104 chunk_t first_merging_chunk;
105 int num_merging_chunks;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000106
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +0000107 /*
108 * The merge operation failed if this flag is set.
109 * Failure modes are handled as follows:
110 * - I/O error reading the header
111 * => don't load the target; abort.
112 * - Header does not have "valid" flag set
113 * => use the origin; forget about the snapshot.
114 * - I/O error when reading exceptions
115 * => don't load the target; abort.
116 * (We can't use the intermediate origin state.)
117 * - I/O error while merging
118 * => stop merging; set merge_failed; process I/O normally.
119 */
120 int merge_failed;
121
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000122 /*
123 * Incoming bios that overlap with chunks being merged must wait
124 * for them to be committed.
125 */
126 struct bio_list bios_queued_during_merge;
Jonathan Brassowccc45ea2009-04-02 19:55:34 +0100127};
128
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000129/*
130 * state_bits:
131 * RUNNING_MERGE - Merge operation is in progress.
132 * SHUTDOWN_MERGE - Set to signal that merge needs to be stopped;
133 * cleared afterwards.
134 */
135#define RUNNING_MERGE 0
136#define SHUTDOWN_MERGE 1
137
Mikulas Patockac2411042010-08-12 04:13:51 +0100138struct dm_dev *dm_snap_origin(struct dm_snapshot *s)
139{
140 return s->origin;
141}
142EXPORT_SYMBOL(dm_snap_origin);
143
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000144struct dm_dev *dm_snap_cow(struct dm_snapshot *s)
145{
146 return s->cow;
147}
148EXPORT_SYMBOL(dm_snap_cow);
149
Jonathan Brassowccc45ea2009-04-02 19:55:34 +0100150static sector_t chunk_to_sector(struct dm_exception_store *store,
151 chunk_t chunk)
152{
153 return chunk << store->chunk_shift;
154}
155
156static int bdev_equal(struct block_device *lhs, struct block_device *rhs)
157{
158 /*
159 * There is only ever one instance of a particular block
160 * device so we can compare pointers safely.
161 */
162 return lhs == rhs;
163}
164
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100165struct dm_snap_pending_exception {
Jon Brassow1d4989c2009-12-10 23:52:10 +0000166 struct dm_exception e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 /*
169 * Origin buffers waiting for this to complete are held
170 * in a bio list
171 */
172 struct bio_list origin_bios;
173 struct bio_list snapshot_bios;
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 /* Pointer back to snapshot context */
176 struct dm_snapshot *snap;
177
178 /*
179 * 1 indicates the exception has already been sent to
180 * kcopyd.
181 */
182 int started;
183};
184
185/*
186 * Hash table mapping origin volumes to lists of snapshots and
187 * a lock to protect it
188 */
Christoph Lametere18b8902006-12-06 20:33:20 -0800189static struct kmem_cache *exception_cache;
190static struct kmem_cache *pending_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100192struct dm_snap_tracked_chunk {
193 struct hlist_node node;
194 chunk_t chunk;
195};
196
197static struct kmem_cache *tracked_chunk_cache;
198
199static struct dm_snap_tracked_chunk *track_chunk(struct dm_snapshot *s,
200 chunk_t chunk)
201{
202 struct dm_snap_tracked_chunk *c = mempool_alloc(s->tracked_chunk_pool,
203 GFP_NOIO);
204 unsigned long flags;
205
206 c->chunk = chunk;
207
208 spin_lock_irqsave(&s->tracked_chunk_lock, flags);
209 hlist_add_head(&c->node,
210 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)]);
211 spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
212
213 return c;
214}
215
216static void stop_tracking_chunk(struct dm_snapshot *s,
217 struct dm_snap_tracked_chunk *c)
218{
219 unsigned long flags;
220
221 spin_lock_irqsave(&s->tracked_chunk_lock, flags);
222 hlist_del(&c->node);
223 spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
224
225 mempool_free(c, s->tracked_chunk_pool);
226}
227
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100228static int __chunk_is_tracked(struct dm_snapshot *s, chunk_t chunk)
229{
230 struct dm_snap_tracked_chunk *c;
231 struct hlist_node *hn;
232 int found = 0;
233
234 spin_lock_irq(&s->tracked_chunk_lock);
235
236 hlist_for_each_entry(c, hn,
237 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)], node) {
238 if (c->chunk == chunk) {
239 found = 1;
240 break;
241 }
242 }
243
244 spin_unlock_irq(&s->tracked_chunk_lock);
245
246 return found;
247}
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249/*
Mike Snitzer615d1eb2009-12-10 23:52:29 +0000250 * This conflicting I/O is extremely improbable in the caller,
251 * so msleep(1) is sufficient and there is no need for a wait queue.
252 */
253static void __check_for_conflicting_io(struct dm_snapshot *s, chunk_t chunk)
254{
255 while (__chunk_is_tracked(s, chunk))
256 msleep(1);
257}
258
259/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 * One of these per registered origin, held in the snapshot_origins hash
261 */
262struct origin {
263 /* The origin device */
264 struct block_device *bdev;
265
266 struct list_head hash_list;
267
268 /* List of snapshots for this origin */
269 struct list_head snapshots;
270};
271
272/*
273 * Size of the hash table for origin volumes. If we make this
274 * the size of the minors list then it should be nearly perfect
275 */
276#define ORIGIN_HASH_SIZE 256
277#define ORIGIN_MASK 0xFF
278static struct list_head *_origins;
279static struct rw_semaphore _origins_lock;
280
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000281static DECLARE_WAIT_QUEUE_HEAD(_pending_exceptions_done);
282static DEFINE_SPINLOCK(_pending_exceptions_done_spinlock);
283static uint64_t _pending_exceptions_done_count;
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285static int init_origin_hash(void)
286{
287 int i;
288
289 _origins = kmalloc(ORIGIN_HASH_SIZE * sizeof(struct list_head),
290 GFP_KERNEL);
291 if (!_origins) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700292 DMERR("unable to allocate memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return -ENOMEM;
294 }
295
296 for (i = 0; i < ORIGIN_HASH_SIZE; i++)
297 INIT_LIST_HEAD(_origins + i);
298 init_rwsem(&_origins_lock);
299
300 return 0;
301}
302
303static void exit_origin_hash(void)
304{
305 kfree(_origins);
306}
307
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100308static unsigned origin_hash(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 return bdev->bd_dev & ORIGIN_MASK;
311}
312
313static struct origin *__lookup_origin(struct block_device *origin)
314{
315 struct list_head *ol;
316 struct origin *o;
317
318 ol = &_origins[origin_hash(origin)];
319 list_for_each_entry (o, ol, hash_list)
320 if (bdev_equal(o->bdev, origin))
321 return o;
322
323 return NULL;
324}
325
326static void __insert_origin(struct origin *o)
327{
328 struct list_head *sl = &_origins[origin_hash(o->bdev)];
329 list_add_tail(&o->hash_list, sl);
330}
331
332/*
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000333 * _origins_lock must be held when calling this function.
334 * Returns number of snapshots registered using the supplied cow device, plus:
335 * snap_src - a snapshot suitable for use as a source of exception handover
336 * snap_dest - a snapshot capable of receiving exception handover.
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000337 * snap_merge - an existing snapshot-merge target linked to the same origin.
338 * There can be at most one snapshot-merge target. The parameter is optional.
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000339 *
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000340 * Possible return values and states of snap_src and snap_dest.
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000341 * 0: NULL, NULL - first new snapshot
342 * 1: snap_src, NULL - normal snapshot
343 * 2: snap_src, snap_dest - waiting for handover
344 * 2: snap_src, NULL - handed over, waiting for old to be deleted
345 * 1: NULL, snap_dest - source got destroyed without handover
346 */
347static int __find_snapshots_sharing_cow(struct dm_snapshot *snap,
348 struct dm_snapshot **snap_src,
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000349 struct dm_snapshot **snap_dest,
350 struct dm_snapshot **snap_merge)
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000351{
352 struct dm_snapshot *s;
353 struct origin *o;
354 int count = 0;
355 int active;
356
357 o = __lookup_origin(snap->origin->bdev);
358 if (!o)
359 goto out;
360
361 list_for_each_entry(s, &o->snapshots, list) {
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000362 if (dm_target_is_snapshot_merge(s->ti) && snap_merge)
363 *snap_merge = s;
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000364 if (!bdev_equal(s->cow->bdev, snap->cow->bdev))
365 continue;
366
367 down_read(&s->lock);
368 active = s->active;
369 up_read(&s->lock);
370
371 if (active) {
372 if (snap_src)
373 *snap_src = s;
374 } else if (snap_dest)
375 *snap_dest = s;
376
377 count++;
378 }
379
380out:
381 return count;
382}
383
384/*
385 * On success, returns 1 if this snapshot is a handover destination,
386 * otherwise returns 0.
387 */
388static int __validate_exception_handover(struct dm_snapshot *snap)
389{
390 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000391 struct dm_snapshot *snap_merge = NULL;
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000392
393 /* Does snapshot need exceptions handed over to it? */
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000394 if ((__find_snapshots_sharing_cow(snap, &snap_src, &snap_dest,
395 &snap_merge) == 2) ||
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000396 snap_dest) {
397 snap->ti->error = "Snapshot cow pairing for exception "
398 "table handover failed";
399 return -EINVAL;
400 }
401
402 /*
403 * If no snap_src was found, snap cannot become a handover
404 * destination.
405 */
406 if (!snap_src)
407 return 0;
408
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000409 /*
410 * Non-snapshot-merge handover?
411 */
412 if (!dm_target_is_snapshot_merge(snap->ti))
413 return 1;
414
415 /*
416 * Do not allow more than one merging snapshot.
417 */
418 if (snap_merge) {
419 snap->ti->error = "A snapshot is already merging.";
420 return -EINVAL;
421 }
422
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000423 if (!snap_src->store->type->prepare_merge ||
424 !snap_src->store->type->commit_merge) {
425 snap->ti->error = "Snapshot exception store does not "
426 "support snapshot-merge.";
427 return -EINVAL;
428 }
429
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000430 return 1;
431}
432
433static void __insert_snapshot(struct origin *o, struct dm_snapshot *s)
434{
435 struct dm_snapshot *l;
436
437 /* Sort the list according to chunk size, largest-first smallest-last */
438 list_for_each_entry(l, &o->snapshots, list)
439 if (l->store->chunk_size < s->store->chunk_size)
440 break;
441 list_add_tail(&s->list, &l->list);
442}
443
444/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 * Make a note of the snapshot and its origin so we can look it
446 * up when the origin has a write on it.
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000447 *
448 * Also validate snapshot exception store handovers.
449 * On success, returns 1 if this registration is a handover destination,
450 * otherwise returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 */
452static int register_snapshot(struct dm_snapshot *snap)
453{
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000454 struct origin *o, *new_o = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 struct block_device *bdev = snap->origin->bdev;
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000456 int r = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000458 new_o = kmalloc(sizeof(*new_o), GFP_KERNEL);
459 if (!new_o)
460 return -ENOMEM;
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 down_write(&_origins_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000464 r = __validate_exception_handover(snap);
465 if (r < 0) {
466 kfree(new_o);
467 goto out;
468 }
469
470 o = __lookup_origin(bdev);
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000471 if (o)
472 kfree(new_o);
473 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 /* New origin */
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000475 o = new_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 /* Initialise the struct */
478 INIT_LIST_HEAD(&o->snapshots);
479 o->bdev = bdev;
480
481 __insert_origin(o);
482 }
483
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000484 __insert_snapshot(o, snap);
485
486out:
487 up_write(&_origins_lock);
488
489 return r;
490}
491
492/*
493 * Move snapshot to correct place in list according to chunk size.
494 */
495static void reregister_snapshot(struct dm_snapshot *s)
496{
497 struct block_device *bdev = s->origin->bdev;
498
499 down_write(&_origins_lock);
500
501 list_del(&s->list);
502 __insert_snapshot(__lookup_origin(bdev), s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 up_write(&_origins_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
507static void unregister_snapshot(struct dm_snapshot *s)
508{
509 struct origin *o;
510
511 down_write(&_origins_lock);
512 o = __lookup_origin(s->origin->bdev);
513
514 list_del(&s->list);
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000515 if (o && list_empty(&o->snapshots)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 list_del(&o->hash_list);
517 kfree(o);
518 }
519
520 up_write(&_origins_lock);
521}
522
523/*
524 * Implementation of the exception hash tables.
Milan Brozd74f81f2008-02-08 02:11:27 +0000525 * The lowest hash_shift bits of the chunk number are ignored, allowing
526 * some consecutive chunks to be grouped together.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 */
Jon Brassow3510cb92009-12-10 23:52:11 +0000528static int dm_exception_table_init(struct dm_exception_table *et,
529 uint32_t size, unsigned hash_shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
531 unsigned int i;
532
Milan Brozd74f81f2008-02-08 02:11:27 +0000533 et->hash_shift = hash_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 et->hash_mask = size - 1;
535 et->table = dm_vcalloc(size, sizeof(struct list_head));
536 if (!et->table)
537 return -ENOMEM;
538
539 for (i = 0; i < size; i++)
540 INIT_LIST_HEAD(et->table + i);
541
542 return 0;
543}
544
Jon Brassow3510cb92009-12-10 23:52:11 +0000545static void dm_exception_table_exit(struct dm_exception_table *et,
546 struct kmem_cache *mem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
548 struct list_head *slot;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000549 struct dm_exception *ex, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 int i, size;
551
552 size = et->hash_mask + 1;
553 for (i = 0; i < size; i++) {
554 slot = et->table + i;
555
556 list_for_each_entry_safe (ex, next, slot, hash_list)
557 kmem_cache_free(mem, ex);
558 }
559
560 vfree(et->table);
561}
562
Jon Brassow191437a2009-12-10 23:52:10 +0000563static uint32_t exception_hash(struct dm_exception_table *et, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Milan Brozd74f81f2008-02-08 02:11:27 +0000565 return (chunk >> et->hash_shift) & et->hash_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
Jon Brassow3510cb92009-12-10 23:52:11 +0000568static void dm_remove_exception(struct dm_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
570 list_del(&e->hash_list);
571}
572
573/*
574 * Return the exception data for a sector, or NULL if not
575 * remapped.
576 */
Jon Brassow3510cb92009-12-10 23:52:11 +0000577static struct dm_exception *dm_lookup_exception(struct dm_exception_table *et,
578 chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580 struct list_head *slot;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000581 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 slot = &et->table[exception_hash(et, chunk)];
584 list_for_each_entry (e, slot, hash_list)
Milan Brozd74f81f2008-02-08 02:11:27 +0000585 if (chunk >= e->old_chunk &&
586 chunk <= e->old_chunk + dm_consecutive_chunk_count(e))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return e;
588
589 return NULL;
590}
591
Jon Brassow3510cb92009-12-10 23:52:11 +0000592static struct dm_exception *alloc_completed_exception(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Jon Brassow1d4989c2009-12-10 23:52:10 +0000594 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 e = kmem_cache_alloc(exception_cache, GFP_NOIO);
597 if (!e)
598 e = kmem_cache_alloc(exception_cache, GFP_ATOMIC);
599
600 return e;
601}
602
Jon Brassow3510cb92009-12-10 23:52:11 +0000603static void free_completed_exception(struct dm_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
605 kmem_cache_free(exception_cache, e);
606}
607
Mikulas Patocka92e86812008-07-21 12:00:35 +0100608static struct dm_snap_pending_exception *alloc_pending_exception(struct dm_snapshot *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Mikulas Patocka92e86812008-07-21 12:00:35 +0100610 struct dm_snap_pending_exception *pe = mempool_alloc(s->pending_pool,
611 GFP_NOIO);
612
Mikulas Patocka879129d22008-10-30 13:33:16 +0000613 atomic_inc(&s->pending_exceptions_count);
Mikulas Patocka92e86812008-07-21 12:00:35 +0100614 pe->snap = s;
615
616 return pe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100619static void free_pending_exception(struct dm_snap_pending_exception *pe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Mikulas Patocka879129d22008-10-30 13:33:16 +0000621 struct dm_snapshot *s = pe->snap;
622
623 mempool_free(pe, s->pending_pool);
624 smp_mb__before_atomic_dec();
625 atomic_dec(&s->pending_exceptions_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
Jon Brassow3510cb92009-12-10 23:52:11 +0000628static void dm_insert_exception(struct dm_exception_table *eh,
629 struct dm_exception *new_e)
Milan Brozd74f81f2008-02-08 02:11:27 +0000630{
Milan Brozd74f81f2008-02-08 02:11:27 +0000631 struct list_head *l;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000632 struct dm_exception *e = NULL;
Milan Brozd74f81f2008-02-08 02:11:27 +0000633
634 l = &eh->table[exception_hash(eh, new_e->old_chunk)];
635
636 /* Add immediately if this table doesn't support consecutive chunks */
637 if (!eh->hash_shift)
638 goto out;
639
640 /* List is ordered by old_chunk */
641 list_for_each_entry_reverse(e, l, hash_list) {
642 /* Insert after an existing chunk? */
643 if (new_e->old_chunk == (e->old_chunk +
644 dm_consecutive_chunk_count(e) + 1) &&
645 new_e->new_chunk == (dm_chunk_number(e->new_chunk) +
646 dm_consecutive_chunk_count(e) + 1)) {
647 dm_consecutive_chunk_count_inc(e);
Jon Brassow3510cb92009-12-10 23:52:11 +0000648 free_completed_exception(new_e);
Milan Brozd74f81f2008-02-08 02:11:27 +0000649 return;
650 }
651
652 /* Insert before an existing chunk? */
653 if (new_e->old_chunk == (e->old_chunk - 1) &&
654 new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) {
655 dm_consecutive_chunk_count_inc(e);
656 e->old_chunk--;
657 e->new_chunk--;
Jon Brassow3510cb92009-12-10 23:52:11 +0000658 free_completed_exception(new_e);
Milan Brozd74f81f2008-02-08 02:11:27 +0000659 return;
660 }
661
662 if (new_e->old_chunk > e->old_chunk)
663 break;
664 }
665
666out:
667 list_add(&new_e->hash_list, e ? &e->hash_list : l);
668}
669
Jonathan Brassowa159c1a2009-01-06 03:05:19 +0000670/*
671 * Callback used by the exception stores to load exceptions when
672 * initialising.
673 */
674static int dm_add_exception(void *context, chunk_t old, chunk_t new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Jonathan Brassowa159c1a2009-01-06 03:05:19 +0000676 struct dm_snapshot *s = context;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000677 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Jon Brassow3510cb92009-12-10 23:52:11 +0000679 e = alloc_completed_exception();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (!e)
681 return -ENOMEM;
682
683 e->old_chunk = old;
Milan Brozd74f81f2008-02-08 02:11:27 +0000684
685 /* Consecutive_count is implicitly initialised to zero */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 e->new_chunk = new;
Milan Brozd74f81f2008-02-08 02:11:27 +0000687
Jon Brassow3510cb92009-12-10 23:52:11 +0000688 dm_insert_exception(&s->complete, e);
Milan Brozd74f81f2008-02-08 02:11:27 +0000689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return 0;
691}
692
Mikulas Patocka7e201b32009-12-10 23:52:08 +0000693/*
694 * Return a minimum chunk size of all snapshots that have the specified origin.
695 * Return zero if the origin has no snapshots.
696 */
697static sector_t __minimum_chunk_size(struct origin *o)
698{
699 struct dm_snapshot *snap;
700 unsigned chunk_size = 0;
701
702 if (o)
703 list_for_each_entry(snap, &o->snapshots, list)
704 chunk_size = min_not_zero(chunk_size,
705 snap->store->chunk_size);
706
707 return chunk_size;
708}
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710/*
711 * Hard coded magic.
712 */
713static int calc_max_buckets(void)
714{
715 /* use a fixed size of 2MB */
716 unsigned long mem = 2 * 1024 * 1024;
717 mem /= sizeof(struct list_head);
718
719 return mem;
720}
721
722/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 * Allocate room for a suitable hash table.
724 */
Jonathan Brassowfee19982009-04-02 19:55:34 +0100725static int init_hash_tables(struct dm_snapshot *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
727 sector_t hash_size, cow_dev_size, origin_dev_size, max_buckets;
728
729 /*
730 * Calculate based on the size of the original volume or
731 * the COW volume...
732 */
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000733 cow_dev_size = get_dev_size(s->cow->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 origin_dev_size = get_dev_size(s->origin->bdev);
735 max_buckets = calc_max_buckets();
736
Jonathan Brassowfee19982009-04-02 19:55:34 +0100737 hash_size = min(origin_dev_size, cow_dev_size) >> s->store->chunk_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 hash_size = min(hash_size, max_buckets);
739
Mikulas Patocka8e87b9b2009-12-10 23:51:54 +0000740 if (hash_size < 64)
741 hash_size = 64;
Robert P. J. Day8defd832008-02-08 02:10:06 +0000742 hash_size = rounddown_pow_of_two(hash_size);
Jon Brassow3510cb92009-12-10 23:52:11 +0000743 if (dm_exception_table_init(&s->complete, hash_size,
744 DM_CHUNK_CONSECUTIVE_BITS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return -ENOMEM;
746
747 /*
748 * Allocate hash table for in-flight exceptions
749 * Make this smaller than the real hash table
750 */
751 hash_size >>= 3;
752 if (hash_size < 64)
753 hash_size = 64;
754
Jon Brassow3510cb92009-12-10 23:52:11 +0000755 if (dm_exception_table_init(&s->pending, hash_size, 0)) {
756 dm_exception_table_exit(&s->complete, exception_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return -ENOMEM;
758 }
759
760 return 0;
761}
762
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000763static void merge_shutdown(struct dm_snapshot *s)
764{
765 clear_bit_unlock(RUNNING_MERGE, &s->state_bits);
766 smp_mb__after_clear_bit();
767 wake_up_bit(&s->state_bits, RUNNING_MERGE);
768}
769
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000770static struct bio *__release_queued_bios_after_merge(struct dm_snapshot *s)
771{
772 s->first_merging_chunk = 0;
773 s->num_merging_chunks = 0;
774
775 return bio_list_get(&s->bios_queued_during_merge);
776}
777
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000778/*
779 * Remove one chunk from the index of completed exceptions.
780 */
781static int __remove_single_exception_chunk(struct dm_snapshot *s,
782 chunk_t old_chunk)
783{
784 struct dm_exception *e;
785
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000786 e = dm_lookup_exception(&s->complete, old_chunk);
787 if (!e) {
788 DMERR("Corruption detected: exception for block %llu is "
789 "on disk but not in memory",
790 (unsigned long long)old_chunk);
791 return -EINVAL;
792 }
793
794 /*
795 * If this is the only chunk using this exception, remove exception.
796 */
797 if (!dm_consecutive_chunk_count(e)) {
798 dm_remove_exception(e);
799 free_completed_exception(e);
800 return 0;
801 }
802
803 /*
804 * The chunk may be either at the beginning or the end of a
805 * group of consecutive chunks - never in the middle. We are
806 * removing chunks in the opposite order to that in which they
807 * were added, so this should always be true.
808 * Decrement the consecutive chunk counter and adjust the
809 * starting point if necessary.
810 */
811 if (old_chunk == e->old_chunk) {
812 e->old_chunk++;
813 e->new_chunk++;
814 } else if (old_chunk != e->old_chunk +
815 dm_consecutive_chunk_count(e)) {
816 DMERR("Attempt to merge block %llu from the "
817 "middle of a chunk range [%llu - %llu]",
818 (unsigned long long)old_chunk,
819 (unsigned long long)e->old_chunk,
820 (unsigned long long)
821 e->old_chunk + dm_consecutive_chunk_count(e));
822 return -EINVAL;
823 }
824
825 dm_consecutive_chunk_count_dec(e);
826
827 return 0;
828}
829
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000830static void flush_bios(struct bio *bio);
831
832static int remove_single_exception_chunk(struct dm_snapshot *s)
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000833{
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000834 struct bio *b = NULL;
835 int r;
836 chunk_t old_chunk = s->first_merging_chunk + s->num_merging_chunks - 1;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000837
838 down_write(&s->lock);
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000839
840 /*
841 * Process chunks (and associated exceptions) in reverse order
842 * so that dm_consecutive_chunk_count_dec() accounting works.
843 */
844 do {
845 r = __remove_single_exception_chunk(s, old_chunk);
846 if (r)
847 goto out;
848 } while (old_chunk-- > s->first_merging_chunk);
849
850 b = __release_queued_bios_after_merge(s);
851
852out:
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000853 up_write(&s->lock);
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000854 if (b)
855 flush_bios(b);
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000856
857 return r;
858}
859
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000860static int origin_write_extent(struct dm_snapshot *merging_snap,
861 sector_t sector, unsigned chunk_size);
862
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000863static void merge_callback(int read_err, unsigned long write_err,
864 void *context);
865
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000866static uint64_t read_pending_exceptions_done_count(void)
867{
868 uint64_t pending_exceptions_done;
869
870 spin_lock(&_pending_exceptions_done_spinlock);
871 pending_exceptions_done = _pending_exceptions_done_count;
872 spin_unlock(&_pending_exceptions_done_spinlock);
873
874 return pending_exceptions_done;
875}
876
877static void increment_pending_exceptions_done_count(void)
878{
879 spin_lock(&_pending_exceptions_done_spinlock);
880 _pending_exceptions_done_count++;
881 spin_unlock(&_pending_exceptions_done_spinlock);
882
883 wake_up_all(&_pending_exceptions_done);
884}
885
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000886static void snapshot_merge_next_chunks(struct dm_snapshot *s)
887{
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000888 int i, linear_chunks;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000889 chunk_t old_chunk, new_chunk;
890 struct dm_io_region src, dest;
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000891 sector_t io_size;
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000892 uint64_t previous_count;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000893
894 BUG_ON(!test_bit(RUNNING_MERGE, &s->state_bits));
895 if (unlikely(test_bit(SHUTDOWN_MERGE, &s->state_bits)))
896 goto shut;
897
898 /*
899 * valid flag never changes during merge, so no lock required.
900 */
901 if (!s->valid) {
902 DMERR("Snapshot is invalid: can't merge");
903 goto shut;
904 }
905
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000906 linear_chunks = s->store->type->prepare_merge(s->store, &old_chunk,
907 &new_chunk);
908 if (linear_chunks <= 0) {
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +0000909 if (linear_chunks < 0) {
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000910 DMERR("Read error in exception store: "
911 "shutting down merge");
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +0000912 down_write(&s->lock);
913 s->merge_failed = 1;
914 up_write(&s->lock);
915 }
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000916 goto shut;
917 }
918
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000919 /* Adjust old_chunk and new_chunk to reflect start of linear region */
920 old_chunk = old_chunk + 1 - linear_chunks;
921 new_chunk = new_chunk + 1 - linear_chunks;
922
923 /*
924 * Use one (potentially large) I/O to copy all 'linear_chunks'
925 * from the exception store to the origin
926 */
927 io_size = linear_chunks * s->store->chunk_size;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000928
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000929 dest.bdev = s->origin->bdev;
930 dest.sector = chunk_to_sector(s->store, old_chunk);
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000931 dest.count = min(io_size, get_dev_size(dest.bdev) - dest.sector);
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000932
933 src.bdev = s->cow->bdev;
934 src.sector = chunk_to_sector(s->store, new_chunk);
935 src.count = dest.count;
936
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000937 /*
938 * Reallocate any exceptions needed in other snapshots then
939 * wait for the pending exceptions to complete.
940 * Each time any pending exception (globally on the system)
941 * completes we are woken and repeat the process to find out
942 * if we can proceed. While this may not seem a particularly
943 * efficient algorithm, it is not expected to have any
944 * significant impact on performance.
945 */
946 previous_count = read_pending_exceptions_done_count();
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000947 while (origin_write_extent(s, dest.sector, io_size)) {
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000948 wait_event(_pending_exceptions_done,
949 (read_pending_exceptions_done_count() !=
950 previous_count));
951 /* Retry after the wait, until all exceptions are done. */
952 previous_count = read_pending_exceptions_done_count();
953 }
954
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000955 down_write(&s->lock);
956 s->first_merging_chunk = old_chunk;
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000957 s->num_merging_chunks = linear_chunks;
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000958 up_write(&s->lock);
959
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000960 /* Wait until writes to all 'linear_chunks' drain */
961 for (i = 0; i < linear_chunks; i++)
962 __check_for_conflicting_io(s, old_chunk + i);
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000963
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000964 dm_kcopyd_copy(s->kcopyd_client, &src, 1, &dest, 0, merge_callback, s);
965 return;
966
967shut:
968 merge_shutdown(s);
969}
970
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000971static void error_bios(struct bio *bio);
972
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000973static void merge_callback(int read_err, unsigned long write_err, void *context)
974{
975 struct dm_snapshot *s = context;
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000976 struct bio *b = NULL;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000977
978 if (read_err || write_err) {
979 if (read_err)
980 DMERR("Read error: shutting down merge.");
981 else
982 DMERR("Write error: shutting down merge.");
983 goto shut;
984 }
985
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000986 if (s->store->type->commit_merge(s->store,
987 s->num_merging_chunks) < 0) {
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000988 DMERR("Write error in exception store: shutting down merge");
989 goto shut;
990 }
991
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000992 if (remove_single_exception_chunk(s) < 0)
993 goto shut;
994
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000995 snapshot_merge_next_chunks(s);
996
997 return;
998
999shut:
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001000 down_write(&s->lock);
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +00001001 s->merge_failed = 1;
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001002 b = __release_queued_bios_after_merge(s);
1003 up_write(&s->lock);
1004 error_bios(b);
1005
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001006 merge_shutdown(s);
1007}
1008
1009static void start_merge(struct dm_snapshot *s)
1010{
1011 if (!test_and_set_bit(RUNNING_MERGE, &s->state_bits))
1012 snapshot_merge_next_chunks(s);
1013}
1014
1015static int wait_schedule(void *ptr)
1016{
1017 schedule();
1018
1019 return 0;
1020}
1021
1022/*
1023 * Stop the merging process and wait until it finishes.
1024 */
1025static void stop_merge(struct dm_snapshot *s)
1026{
1027 set_bit(SHUTDOWN_MERGE, &s->state_bits);
1028 wait_on_bit(&s->state_bits, RUNNING_MERGE, wait_schedule,
1029 TASK_UNINTERRUPTIBLE);
1030 clear_bit(SHUTDOWN_MERGE, &s->state_bits);
1031}
1032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 * Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size>
1035 */
1036static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1037{
1038 struct dm_snapshot *s;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001039 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 int r = -EINVAL;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001041 char *origin_path, *cow_path;
Mike Snitzer10b81062009-12-10 23:52:31 +00001042 unsigned args_used, num_flush_requests = 1;
1043 fmode_t origin_mode = FMODE_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -07001045 if (argc != 4) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001046 ti->error = "requires exactly 4 arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 r = -EINVAL;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001048 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 }
1050
Mike Snitzer10b81062009-12-10 23:52:31 +00001051 if (dm_target_is_snapshot_merge(ti)) {
1052 num_flush_requests = 2;
1053 origin_mode = FMODE_WRITE;
1054 }
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 s = kmalloc(sizeof(*s), GFP_KERNEL);
Jonathan Brassowfee19982009-04-02 19:55:34 +01001057 if (!s) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 ti->error = "Cannot allocate snapshot context private "
1059 "structure";
1060 r = -ENOMEM;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001061 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
1063
Mikulas Patockac2411042010-08-12 04:13:51 +01001064 origin_path = argv[0];
1065 argv++;
1066 argc--;
1067
1068 r = dm_get_device(ti, origin_path, origin_mode, &s->origin);
1069 if (r) {
1070 ti->error = "Cannot get origin device";
1071 goto bad_origin;
1072 }
1073
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001074 cow_path = argv[0];
1075 argv++;
1076 argc--;
1077
Milan Broz024d37e2011-03-24 13:52:14 +00001078 r = dm_get_device(ti, cow_path, dm_table_get_mode(ti->table), &s->cow);
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001079 if (r) {
1080 ti->error = "Cannot get COW device";
1081 goto bad_cow;
1082 }
1083
1084 r = dm_exception_store_create(ti, argc, argv, s, &args_used, &s->store);
1085 if (r) {
1086 ti->error = "Couldn't create exception store";
1087 r = -EINVAL;
1088 goto bad_store;
1089 }
1090
1091 argv += args_used;
1092 argc -= args_used;
1093
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001094 s->ti = ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 s->valid = 1;
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001096 s->active = 0;
Mikulas Patocka879129d22008-10-30 13:33:16 +00001097 atomic_set(&s->pending_exceptions_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 init_rwsem(&s->lock);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001099 INIT_LIST_HEAD(&s->list);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001100 spin_lock_init(&s->pe_lock);
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001101 s->state_bits = 0;
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +00001102 s->merge_failed = 0;
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001103 s->first_merging_chunk = 0;
1104 s->num_merging_chunks = 0;
1105 bio_list_init(&s->bios_queued_during_merge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107 /* Allocate hash table for COW data */
Jonathan Brassowfee19982009-04-02 19:55:34 +01001108 if (init_hash_tables(s)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 ti->error = "Unable to allocate hash table space";
1110 r = -ENOMEM;
Jonathan Brassowfee19982009-04-02 19:55:34 +01001111 goto bad_hash_tables;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 }
1113
Mikulas Patocka5f43ba22011-05-29 13:03:11 +01001114 r = dm_kcopyd_client_create(&s->kcopyd_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (r) {
1116 ti->error = "Could not create kcopyd client";
Jonathan Brassowfee19982009-04-02 19:55:34 +01001117 goto bad_kcopyd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
1119
Mikulas Patocka92e86812008-07-21 12:00:35 +01001120 s->pending_pool = mempool_create_slab_pool(MIN_IOS, pending_cache);
1121 if (!s->pending_pool) {
1122 ti->error = "Could not allocate mempool for pending exceptions";
Jonathan Brassowfee19982009-04-02 19:55:34 +01001123 goto bad_pending_pool;
Mikulas Patocka92e86812008-07-21 12:00:35 +01001124 }
1125
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001126 s->tracked_chunk_pool = mempool_create_slab_pool(MIN_IOS,
1127 tracked_chunk_cache);
1128 if (!s->tracked_chunk_pool) {
1129 ti->error = "Could not allocate tracked_chunk mempool for "
1130 "tracking reads";
Mikulas Patocka92e86812008-07-21 12:00:35 +01001131 goto bad_tracked_chunk_pool;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001132 }
1133
1134 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
1135 INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]);
1136
1137 spin_lock_init(&s->tracked_chunk_lock);
1138
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001139 ti->private = s;
Mike Snitzer10b81062009-12-10 23:52:31 +00001140 ti->num_flush_requests = num_flush_requests;
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001141
1142 /* Add snapshot to the list of snapshots for this origin */
1143 /* Exceptions aren't triggered till snapshot_resume() is called */
1144 r = register_snapshot(s);
1145 if (r == -ENOMEM) {
1146 ti->error = "Snapshot origin struct allocation failed";
1147 goto bad_load_and_register;
1148 } else if (r < 0) {
1149 /* invalid handover, register_snapshot has set ti->error */
1150 goto bad_load_and_register;
1151 }
1152
1153 /*
1154 * Metadata must only be loaded into one table at once, so skip this
1155 * if metadata will be handed over during resume.
1156 * Chunk size will be set during the handover - set it to zero to
1157 * ensure it's ignored.
1158 */
1159 if (r > 0) {
1160 s->store->chunk_size = 0;
1161 return 0;
1162 }
1163
Jonathan Brassow493df712009-04-02 19:55:31 +01001164 r = s->store->type->read_metadata(s->store, dm_add_exception,
1165 (void *)s);
Milan Broz07641472007-07-12 17:28:13 +01001166 if (r < 0) {
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -07001167 ti->error = "Failed to read snapshot metadata";
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001168 goto bad_read_metadata;
Milan Broz07641472007-07-12 17:28:13 +01001169 } else if (r > 0) {
1170 s->valid = 0;
1171 DMWARN("Snapshot is marked invalid.");
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -07001172 }
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001173
Mikulas Patocka3f2412d2009-10-16 23:18:16 +01001174 if (!s->store->chunk_size) {
1175 ti->error = "Chunk size not set";
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001176 goto bad_read_metadata;
Mikulas Patocka3f2412d2009-10-16 23:18:16 +01001177 }
Jonathan Brassowd0216842009-04-02 19:55:32 +01001178 ti->split_io = s->store->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180 return 0;
1181
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001182bad_read_metadata:
1183 unregister_snapshot(s);
1184
Jonathan Brassowfee19982009-04-02 19:55:34 +01001185bad_load_and_register:
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001186 mempool_destroy(s->tracked_chunk_pool);
1187
Jonathan Brassowfee19982009-04-02 19:55:34 +01001188bad_tracked_chunk_pool:
Mikulas Patocka92e86812008-07-21 12:00:35 +01001189 mempool_destroy(s->pending_pool);
1190
Jonathan Brassowfee19982009-04-02 19:55:34 +01001191bad_pending_pool:
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001192 dm_kcopyd_client_destroy(s->kcopyd_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Jonathan Brassowfee19982009-04-02 19:55:34 +01001194bad_kcopyd:
Jon Brassow3510cb92009-12-10 23:52:11 +00001195 dm_exception_table_exit(&s->pending, pending_cache);
1196 dm_exception_table_exit(&s->complete, exception_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Jonathan Brassowfee19982009-04-02 19:55:34 +01001198bad_hash_tables:
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001199 dm_exception_store_destroy(s->store);
1200
1201bad_store:
1202 dm_put_device(ti, s->cow);
1203
1204bad_cow:
Mikulas Patockac2411042010-08-12 04:13:51 +01001205 dm_put_device(ti, s->origin);
1206
1207bad_origin:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 kfree(s);
1209
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001210bad:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 return r;
1212}
1213
Milan Broz31c93a02006-12-08 02:41:11 -08001214static void __free_exceptions(struct dm_snapshot *s)
1215{
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001216 dm_kcopyd_client_destroy(s->kcopyd_client);
Milan Broz31c93a02006-12-08 02:41:11 -08001217 s->kcopyd_client = NULL;
1218
Jon Brassow3510cb92009-12-10 23:52:11 +00001219 dm_exception_table_exit(&s->pending, pending_cache);
1220 dm_exception_table_exit(&s->complete, exception_cache);
Milan Broz31c93a02006-12-08 02:41:11 -08001221}
1222
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001223static void __handover_exceptions(struct dm_snapshot *snap_src,
1224 struct dm_snapshot *snap_dest)
1225{
1226 union {
1227 struct dm_exception_table table_swap;
1228 struct dm_exception_store *store_swap;
1229 } u;
1230
1231 /*
1232 * Swap all snapshot context information between the two instances.
1233 */
1234 u.table_swap = snap_dest->complete;
1235 snap_dest->complete = snap_src->complete;
1236 snap_src->complete = u.table_swap;
1237
1238 u.store_swap = snap_dest->store;
1239 snap_dest->store = snap_src->store;
1240 snap_src->store = u.store_swap;
1241
1242 snap_dest->store->snap = snap_dest;
1243 snap_src->store->snap = snap_src;
1244
1245 snap_dest->ti->split_io = snap_dest->store->chunk_size;
1246 snap_dest->valid = snap_src->valid;
1247
1248 /*
1249 * Set source invalid to ensure it receives no further I/O.
1250 */
1251 snap_src->valid = 0;
1252}
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254static void snapshot_dtr(struct dm_target *ti)
1255{
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001256#ifdef CONFIG_DM_DEBUG
1257 int i;
1258#endif
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001259 struct dm_snapshot *s = ti->private;
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001260 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001262 down_read(&_origins_lock);
1263 /* Check whether exception handover must be cancelled */
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +00001264 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001265 if (snap_src && snap_dest && (s == snap_src)) {
1266 down_write(&snap_dest->lock);
1267 snap_dest->valid = 0;
1268 up_write(&snap_dest->lock);
1269 DMERR("Cancelling snapshot handover.");
1270 }
1271 up_read(&_origins_lock);
1272
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001273 if (dm_target_is_snapshot_merge(ti))
1274 stop_merge(s);
1275
Alasdair G Kergon138728dc2006-03-27 01:17:50 -08001276 /* Prevent further origin writes from using this snapshot. */
1277 /* After this returns there can be no new kcopyd jobs. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 unregister_snapshot(s);
1279
Mikulas Patocka879129d22008-10-30 13:33:16 +00001280 while (atomic_read(&s->pending_exceptions_count))
Mikulas Patocka90fa1522009-01-06 03:04:54 +00001281 msleep(1);
Mikulas Patocka879129d22008-10-30 13:33:16 +00001282 /*
1283 * Ensure instructions in mempool_destroy aren't reordered
1284 * before atomic_read.
1285 */
1286 smp_mb();
1287
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001288#ifdef CONFIG_DM_DEBUG
1289 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
1290 BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i]));
1291#endif
1292
1293 mempool_destroy(s->tracked_chunk_pool);
1294
Milan Broz31c93a02006-12-08 02:41:11 -08001295 __free_exceptions(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Mikulas Patocka92e86812008-07-21 12:00:35 +01001297 mempool_destroy(s->pending_pool);
1298
Jonathan Brassowfee19982009-04-02 19:55:34 +01001299 dm_exception_store_destroy(s->store);
Alasdair G Kergon138728dc2006-03-27 01:17:50 -08001300
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001301 dm_put_device(ti, s->cow);
1302
Mikulas Patockac2411042010-08-12 04:13:51 +01001303 dm_put_device(ti, s->origin);
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 kfree(s);
1306}
1307
1308/*
1309 * Flush a list of buffers.
1310 */
1311static void flush_bios(struct bio *bio)
1312{
1313 struct bio *n;
1314
1315 while (bio) {
1316 n = bio->bi_next;
1317 bio->bi_next = NULL;
1318 generic_make_request(bio);
1319 bio = n;
1320 }
1321}
1322
Mikulas Patocka515ad662009-12-10 23:52:30 +00001323static int do_origin(struct dm_dev *origin, struct bio *bio);
1324
1325/*
1326 * Flush a list of buffers.
1327 */
1328static void retry_origin_bios(struct dm_snapshot *s, struct bio *bio)
1329{
1330 struct bio *n;
1331 int r;
1332
1333 while (bio) {
1334 n = bio->bi_next;
1335 bio->bi_next = NULL;
1336 r = do_origin(s->origin, bio);
1337 if (r == DM_MAPIO_REMAPPED)
1338 generic_make_request(bio);
1339 bio = n;
1340 }
1341}
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343/*
1344 * Error a list of buffers.
1345 */
1346static void error_bios(struct bio *bio)
1347{
1348 struct bio *n;
1349
1350 while (bio) {
1351 n = bio->bi_next;
1352 bio->bi_next = NULL;
NeilBrown6712ecf2007-09-27 12:47:43 +02001353 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 bio = n;
1355 }
1356}
1357
Alasdair G Kergon695368a2006-10-03 01:15:31 -07001358static void __invalidate_snapshot(struct dm_snapshot *s, int err)
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001359{
1360 if (!s->valid)
1361 return;
1362
1363 if (err == -EIO)
1364 DMERR("Invalidating snapshot: Error reading/writing.");
1365 else if (err == -ENOMEM)
1366 DMERR("Invalidating snapshot: Unable to allocate exception.");
1367
Jonathan Brassow493df712009-04-02 19:55:31 +01001368 if (s->store->type->drop_snapshot)
1369 s->store->type->drop_snapshot(s->store);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001370
1371 s->valid = 0;
1372
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001373 dm_table_event(s->ti->table);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001374}
1375
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001376static void pending_complete(struct dm_snap_pending_exception *pe, int success)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
Jon Brassow1d4989c2009-12-10 23:52:10 +00001378 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 struct dm_snapshot *s = pe->snap;
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001380 struct bio *origin_bios = NULL;
1381 struct bio *snapshot_bios = NULL;
1382 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001384 if (!success) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 /* Read/write error - snapshot is unusable */
1386 down_write(&s->lock);
Alasdair G Kergon695368a2006-10-03 01:15:31 -07001387 __invalidate_snapshot(s, -EIO);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001388 error = 1;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001389 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
1391
Jon Brassow3510cb92009-12-10 23:52:11 +00001392 e = alloc_completed_exception();
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001393 if (!e) {
1394 down_write(&s->lock);
Alasdair G Kergon695368a2006-10-03 01:15:31 -07001395 __invalidate_snapshot(s, -ENOMEM);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001396 error = 1;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001397 goto out;
1398 }
1399 *e = pe->e;
1400
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001401 down_write(&s->lock);
1402 if (!s->valid) {
Jon Brassow3510cb92009-12-10 23:52:11 +00001403 free_completed_exception(e);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001404 error = 1;
1405 goto out;
1406 }
1407
Mike Snitzer615d1eb2009-12-10 23:52:29 +00001408 /* Check for conflicting reads */
1409 __check_for_conflicting_io(s, pe->e.old_chunk);
Mikulas Patockaa8d41b52008-07-21 12:00:34 +01001410
1411 /*
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001412 * Add a proper exception, and remove the
1413 * in-flight exception from the list.
1414 */
Jon Brassow3510cb92009-12-10 23:52:11 +00001415 dm_insert_exception(&s->complete, e);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 out:
Jon Brassow3510cb92009-12-10 23:52:11 +00001418 dm_remove_exception(&pe->e);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001419 snapshot_bios = bio_list_get(&pe->snapshot_bios);
Mikulas Patocka515ad662009-12-10 23:52:30 +00001420 origin_bios = bio_list_get(&pe->origin_bios);
1421 free_pending_exception(pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Mikulas Patocka73dfd072009-12-10 23:52:34 +00001423 increment_pending_exceptions_done_count();
1424
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001425 up_write(&s->lock);
1426
1427 /* Submit any pending write bios */
1428 if (error)
1429 error_bios(snapshot_bios);
1430 else
1431 flush_bios(snapshot_bios);
1432
Mikulas Patocka515ad662009-12-10 23:52:30 +00001433 retry_origin_bios(s, origin_bios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434}
1435
1436static void commit_callback(void *context, int success)
1437{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001438 struct dm_snap_pending_exception *pe = context;
1439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 pending_complete(pe, success);
1441}
1442
1443/*
1444 * Called when the copy I/O has finished. kcopyd actually runs
1445 * this code so don't block.
1446 */
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -07001447static void copy_callback(int read_err, unsigned long write_err, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001449 struct dm_snap_pending_exception *pe = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 struct dm_snapshot *s = pe->snap;
1451
1452 if (read_err || write_err)
1453 pending_complete(pe, 0);
1454
1455 else
1456 /* Update the metadata if we are persistent */
Jonathan Brassow493df712009-04-02 19:55:31 +01001457 s->store->type->commit_exception(s->store, &pe->e,
1458 commit_callback, pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459}
1460
1461/*
1462 * Dispatches the copy operation to kcopyd.
1463 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001464static void start_copy(struct dm_snap_pending_exception *pe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
1466 struct dm_snapshot *s = pe->snap;
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +01001467 struct dm_io_region src, dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 struct block_device *bdev = s->origin->bdev;
1469 sector_t dev_size;
1470
1471 dev_size = get_dev_size(bdev);
1472
1473 src.bdev = bdev;
Jonathan Brassow71fab002009-04-02 19:55:33 +01001474 src.sector = chunk_to_sector(s->store, pe->e.old_chunk);
Mikulas Patockadf96eee2009-10-16 23:18:17 +01001475 src.count = min((sector_t)s->store->chunk_size, dev_size - src.sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001477 dest.bdev = s->cow->bdev;
Jonathan Brassow71fab002009-04-02 19:55:33 +01001478 dest.sector = chunk_to_sector(s->store, pe->e.new_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 dest.count = src.count;
1480
1481 /* Hand over to kcopyd */
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001482 dm_kcopyd_copy(s->kcopyd_client,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 &src, 1, &dest, 0, copy_callback, pe);
1484}
1485
Mikulas Patocka29138082009-04-02 19:55:25 +01001486static struct dm_snap_pending_exception *
1487__lookup_pending_exception(struct dm_snapshot *s, chunk_t chunk)
1488{
Jon Brassow3510cb92009-12-10 23:52:11 +00001489 struct dm_exception *e = dm_lookup_exception(&s->pending, chunk);
Mikulas Patocka29138082009-04-02 19:55:25 +01001490
1491 if (!e)
1492 return NULL;
1493
1494 return container_of(e, struct dm_snap_pending_exception, e);
1495}
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497/*
1498 * Looks to see if this snapshot already has a pending exception
1499 * for this chunk, otherwise it allocates a new one and inserts
1500 * it into the pending table.
1501 *
1502 * NOTE: a write lock must be held on snap->lock before calling
1503 * this.
1504 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001505static struct dm_snap_pending_exception *
Mikulas Patockac6621392009-04-02 19:55:25 +01001506__find_pending_exception(struct dm_snapshot *s,
1507 struct dm_snap_pending_exception *pe, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
Mikulas Patockac6621392009-04-02 19:55:25 +01001509 struct dm_snap_pending_exception *pe2;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001510
Mikulas Patocka29138082009-04-02 19:55:25 +01001511 pe2 = __lookup_pending_exception(s, chunk);
1512 if (pe2) {
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001513 free_pending_exception(pe);
Mikulas Patocka29138082009-04-02 19:55:25 +01001514 return pe2;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001515 }
1516
1517 pe->e.old_chunk = chunk;
1518 bio_list_init(&pe->origin_bios);
1519 bio_list_init(&pe->snapshot_bios);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001520 pe->started = 0;
1521
Jonathan Brassow493df712009-04-02 19:55:31 +01001522 if (s->store->type->prepare_exception(s->store, &pe->e)) {
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001523 free_pending_exception(pe);
1524 return NULL;
1525 }
1526
Jon Brassow3510cb92009-12-10 23:52:11 +00001527 dm_insert_exception(&s->pending, &pe->e);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 return pe;
1530}
1531
Jon Brassow1d4989c2009-12-10 23:52:10 +00001532static void remap_exception(struct dm_snapshot *s, struct dm_exception *e,
Milan Brozd74f81f2008-02-08 02:11:27 +00001533 struct bio *bio, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001535 bio->bi_bdev = s->cow->bdev;
Jonathan Brassow71fab002009-04-02 19:55:33 +01001536 bio->bi_sector = chunk_to_sector(s->store,
1537 dm_chunk_number(e->new_chunk) +
1538 (chunk - e->old_chunk)) +
1539 (bio->bi_sector &
1540 s->store->chunk_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541}
1542
1543static int snapshot_map(struct dm_target *ti, struct bio *bio,
1544 union map_info *map_context)
1545{
Jon Brassow1d4989c2009-12-10 23:52:10 +00001546 struct dm_exception *e;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001547 struct dm_snapshot *s = ti->private;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001548 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 chunk_t chunk;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001550 struct dm_snap_pending_exception *pe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Tejun Heod87f4c12010-09-03 11:56:19 +02001552 if (bio->bi_rw & REQ_FLUSH) {
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001553 bio->bi_bdev = s->cow->bdev;
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01001554 return DM_MAPIO_REMAPPED;
1555 }
1556
Jonathan Brassow71fab002009-04-02 19:55:33 +01001557 chunk = sector_to_chunk(s->store, bio->bi_sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
1559 /* Full snapshots are not usable */
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001560 /* To get here the table must be live so s->active is always set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 if (!s->valid)
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -07001562 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001564 /* FIXME: should only take write lock if we need
1565 * to copy an exception */
1566 down_write(&s->lock);
1567
1568 if (!s->valid) {
1569 r = -EIO;
1570 goto out_unlock;
1571 }
1572
1573 /* If the block is already remapped - use that, else remap it */
Jon Brassow3510cb92009-12-10 23:52:11 +00001574 e = dm_lookup_exception(&s->complete, chunk);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001575 if (e) {
Milan Brozd74f81f2008-02-08 02:11:27 +00001576 remap_exception(s, e, bio, chunk);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001577 goto out_unlock;
1578 }
1579
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 /*
1581 * Write to snapshot - higher level takes care of RW/RO
1582 * flags so we should only get this if we are
1583 * writeable.
1584 */
1585 if (bio_rw(bio) == WRITE) {
Mikulas Patocka29138082009-04-02 19:55:25 +01001586 pe = __lookup_pending_exception(s, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001587 if (!pe) {
Mikulas Patockac6621392009-04-02 19:55:25 +01001588 up_write(&s->lock);
1589 pe = alloc_pending_exception(s);
1590 down_write(&s->lock);
1591
1592 if (!s->valid) {
1593 free_pending_exception(pe);
1594 r = -EIO;
1595 goto out_unlock;
1596 }
1597
Jon Brassow3510cb92009-12-10 23:52:11 +00001598 e = dm_lookup_exception(&s->complete, chunk);
Mikulas Patocka35bf6592009-04-02 19:55:26 +01001599 if (e) {
1600 free_pending_exception(pe);
1601 remap_exception(s, e, bio, chunk);
1602 goto out_unlock;
1603 }
1604
Mikulas Patockac6621392009-04-02 19:55:25 +01001605 pe = __find_pending_exception(s, pe, chunk);
Mikulas Patocka29138082009-04-02 19:55:25 +01001606 if (!pe) {
1607 __invalidate_snapshot(s, -ENOMEM);
1608 r = -EIO;
1609 goto out_unlock;
1610 }
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001611 }
1612
Milan Brozd74f81f2008-02-08 02:11:27 +00001613 remap_exception(s, &pe->e, bio, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001614 bio_list_add(&pe->snapshot_bios, bio);
1615
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001616 r = DM_MAPIO_SUBMITTED;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001617
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001618 if (!pe->started) {
1619 /* this is protected by snap->lock */
1620 pe->started = 1;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001621 up_write(&s->lock);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001622 start_copy(pe);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001623 goto out;
1624 }
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001625 } else {
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001626 bio->bi_bdev = s->origin->bdev;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001627 map_context->ptr = track_chunk(s, chunk);
1628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001630 out_unlock:
1631 up_write(&s->lock);
1632 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 return r;
1634}
1635
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001636/*
1637 * A snapshot-merge target behaves like a combination of a snapshot
1638 * target and a snapshot-origin target. It only generates new
1639 * exceptions in other snapshots and not in the one that is being
1640 * merged.
1641 *
1642 * For each chunk, if there is an existing exception, it is used to
1643 * redirect I/O to the cow device. Otherwise I/O is sent to the origin,
1644 * which in turn might generate exceptions in other snapshots.
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001645 * If merging is currently taking place on the chunk in question, the
1646 * I/O is deferred by adding it to s->bios_queued_during_merge.
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001647 */
1648static int snapshot_merge_map(struct dm_target *ti, struct bio *bio,
1649 union map_info *map_context)
1650{
1651 struct dm_exception *e;
1652 struct dm_snapshot *s = ti->private;
1653 int r = DM_MAPIO_REMAPPED;
1654 chunk_t chunk;
1655
Tejun Heod87f4c12010-09-03 11:56:19 +02001656 if (bio->bi_rw & REQ_FLUSH) {
Mike Snitzer57cba5d2010-08-12 04:14:04 +01001657 if (!map_context->target_request_nr)
Mike Snitzer10b81062009-12-10 23:52:31 +00001658 bio->bi_bdev = s->origin->bdev;
1659 else
1660 bio->bi_bdev = s->cow->bdev;
1661 map_context->ptr = NULL;
1662 return DM_MAPIO_REMAPPED;
1663 }
1664
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001665 chunk = sector_to_chunk(s->store, bio->bi_sector);
1666
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001667 down_write(&s->lock);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001668
Mikulas Patockad2fdb772009-12-10 23:52:36 +00001669 /* Full merging snapshots are redirected to the origin */
1670 if (!s->valid)
1671 goto redirect_to_origin;
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001672
1673 /* If the block is already remapped - use that */
1674 e = dm_lookup_exception(&s->complete, chunk);
1675 if (e) {
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001676 /* Queue writes overlapping with chunks being merged */
1677 if (bio_rw(bio) == WRITE &&
1678 chunk >= s->first_merging_chunk &&
1679 chunk < (s->first_merging_chunk +
1680 s->num_merging_chunks)) {
1681 bio->bi_bdev = s->origin->bdev;
1682 bio_list_add(&s->bios_queued_during_merge, bio);
1683 r = DM_MAPIO_SUBMITTED;
1684 goto out_unlock;
1685 }
Mikulas Patocka17aa0332009-12-10 23:52:33 +00001686
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001687 remap_exception(s, e, bio, chunk);
Mikulas Patocka17aa0332009-12-10 23:52:33 +00001688
1689 if (bio_rw(bio) == WRITE)
1690 map_context->ptr = track_chunk(s, chunk);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001691 goto out_unlock;
1692 }
1693
Mikulas Patockad2fdb772009-12-10 23:52:36 +00001694redirect_to_origin:
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001695 bio->bi_bdev = s->origin->bdev;
1696
1697 if (bio_rw(bio) == WRITE) {
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001698 up_write(&s->lock);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001699 return do_origin(s->origin, bio);
1700 }
1701
1702out_unlock:
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001703 up_write(&s->lock);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001704
1705 return r;
1706}
1707
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001708static int snapshot_end_io(struct dm_target *ti, struct bio *bio,
1709 int error, union map_info *map_context)
1710{
1711 struct dm_snapshot *s = ti->private;
1712 struct dm_snap_tracked_chunk *c = map_context->ptr;
1713
1714 if (c)
1715 stop_tracking_chunk(s, c);
1716
1717 return 0;
1718}
1719
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001720static void snapshot_merge_presuspend(struct dm_target *ti)
1721{
1722 struct dm_snapshot *s = ti->private;
1723
1724 stop_merge(s);
1725}
1726
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001727static int snapshot_preresume(struct dm_target *ti)
1728{
1729 int r = 0;
1730 struct dm_snapshot *s = ti->private;
1731 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
1732
1733 down_read(&_origins_lock);
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +00001734 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001735 if (snap_src && snap_dest) {
1736 down_read(&snap_src->lock);
1737 if (s == snap_src) {
1738 DMERR("Unable to resume snapshot source until "
1739 "handover completes.");
1740 r = -EINVAL;
Mike Snitzerb83b2f22011-01-13 19:59:59 +00001741 } else if (!dm_suspended(snap_src->ti)) {
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001742 DMERR("Unable to perform snapshot handover until "
1743 "source is suspended.");
1744 r = -EINVAL;
1745 }
1746 up_read(&snap_src->lock);
1747 }
1748 up_read(&_origins_lock);
1749
1750 return r;
1751}
1752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753static void snapshot_resume(struct dm_target *ti)
1754{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001755 struct dm_snapshot *s = ti->private;
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001756 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
1757
1758 down_read(&_origins_lock);
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +00001759 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001760 if (snap_src && snap_dest) {
1761 down_write(&snap_src->lock);
1762 down_write_nested(&snap_dest->lock, SINGLE_DEPTH_NESTING);
1763 __handover_exceptions(snap_src, snap_dest);
1764 up_write(&snap_dest->lock);
1765 up_write(&snap_src->lock);
1766 }
1767 up_read(&_origins_lock);
1768
1769 /* Now we have correct chunk size, reregister */
1770 reregister_snapshot(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001772 down_write(&s->lock);
1773 s->active = 1;
1774 up_write(&s->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775}
1776
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001777static sector_t get_origin_minimum_chunksize(struct block_device *bdev)
1778{
1779 sector_t min_chunksize;
1780
1781 down_read(&_origins_lock);
1782 min_chunksize = __minimum_chunk_size(__lookup_origin(bdev));
1783 up_read(&_origins_lock);
1784
1785 return min_chunksize;
1786}
1787
1788static void snapshot_merge_resume(struct dm_target *ti)
1789{
1790 struct dm_snapshot *s = ti->private;
1791
1792 /*
1793 * Handover exceptions from existing snapshot.
1794 */
1795 snapshot_resume(ti);
1796
1797 /*
1798 * snapshot-merge acts as an origin, so set ti->split_io
1799 */
1800 ti->split_io = get_origin_minimum_chunksize(s->origin->bdev);
1801
1802 start_merge(s);
1803}
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805static int snapshot_status(struct dm_target *ti, status_type_t type,
1806 char *result, unsigned int maxlen)
1807{
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01001808 unsigned sz = 0;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001809 struct dm_snapshot *snap = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
1811 switch (type) {
1812 case STATUSTYPE_INFO:
Mikulas Patocka94e765722009-12-10 23:51:53 +00001813
1814 down_write(&snap->lock);
1815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 if (!snap->valid)
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01001817 DMEMIT("Invalid");
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +00001818 else if (snap->merge_failed)
1819 DMEMIT("Merge failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 else {
Mike Snitzer985903b2009-12-10 23:52:11 +00001821 if (snap->store->type->usage) {
1822 sector_t total_sectors, sectors_allocated,
1823 metadata_sectors;
1824 snap->store->type->usage(snap->store,
1825 &total_sectors,
1826 &sectors_allocated,
1827 &metadata_sectors);
1828 DMEMIT("%llu/%llu %llu",
1829 (unsigned long long)sectors_allocated,
1830 (unsigned long long)total_sectors,
1831 (unsigned long long)metadata_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 }
1833 else
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01001834 DMEMIT("Unknown");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 }
Mikulas Patocka94e765722009-12-10 23:51:53 +00001836
1837 up_write(&snap->lock);
1838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 break;
1840
1841 case STATUSTYPE_TABLE:
1842 /*
1843 * kdevname returns a static pointer so we need
1844 * to make private copies if the output is to
1845 * make sense.
1846 */
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001847 DMEMIT("%s %s", snap->origin->name, snap->cow->name);
Jonathan Brassow1e302a92009-04-02 19:55:35 +01001848 snap->store->type->status(snap->store, type, result + sz,
1849 maxlen - sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 break;
1851 }
1852
1853 return 0;
1854}
1855
Mike Snitzer8811f462009-09-04 20:40:19 +01001856static int snapshot_iterate_devices(struct dm_target *ti,
1857 iterate_devices_callout_fn fn, void *data)
1858{
1859 struct dm_snapshot *snap = ti->private;
Mikulas Patocka1e5554c2010-08-12 04:13:50 +01001860 int r;
Mike Snitzer8811f462009-09-04 20:40:19 +01001861
Mikulas Patocka1e5554c2010-08-12 04:13:50 +01001862 r = fn(ti, snap->origin, 0, ti->len, data);
1863
1864 if (!r)
1865 r = fn(ti, snap->cow, 0, get_dev_size(snap->cow->bdev), data);
1866
1867 return r;
Mike Snitzer8811f462009-09-04 20:40:19 +01001868}
1869
1870
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871/*-----------------------------------------------------------------
1872 * Origin methods
1873 *---------------------------------------------------------------*/
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00001874
1875/*
1876 * If no exceptions need creating, DM_MAPIO_REMAPPED is returned and any
1877 * supplied bio was ignored. The caller may submit it immediately.
1878 * (No remapping actually occurs as the origin is always a direct linear
1879 * map.)
1880 *
1881 * If further exceptions are required, DM_MAPIO_SUBMITTED is returned
1882 * and any supplied bio is added to a list to be submitted once all
1883 * the necessary exceptions exist.
1884 */
1885static int __origin_write(struct list_head *snapshots, sector_t sector,
1886 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887{
Mikulas Patocka515ad662009-12-10 23:52:30 +00001888 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 struct dm_snapshot *snap;
Jon Brassow1d4989c2009-12-10 23:52:10 +00001890 struct dm_exception *e;
Mikulas Patocka515ad662009-12-10 23:52:30 +00001891 struct dm_snap_pending_exception *pe;
1892 struct dm_snap_pending_exception *pe_to_start_now = NULL;
1893 struct dm_snap_pending_exception *pe_to_start_last = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 chunk_t chunk;
1895
1896 /* Do all the snapshots on this origin */
1897 list_for_each_entry (snap, snapshots, list) {
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001898 /*
1899 * Don't make new exceptions in a merging snapshot
1900 * because it has effectively been deleted
1901 */
1902 if (dm_target_is_snapshot_merge(snap->ti))
1903 continue;
1904
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001905 down_write(&snap->lock);
1906
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001907 /* Only deal with valid and active snapshots */
1908 if (!snap->valid || !snap->active)
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001909 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Alasdair G Kergond5e404c2005-07-12 15:53:05 -07001911 /* Nothing to do if writing beyond end of snapshot */
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00001912 if (sector >= dm_table_get_size(snap->ti->table))
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001913 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
1915 /*
1916 * Remember, different snapshots can have
1917 * different chunk sizes.
1918 */
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00001919 chunk = sector_to_chunk(snap->store, sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
1921 /*
1922 * Check exception table to see if block
1923 * is already remapped in this snapshot
1924 * and trigger an exception if not.
1925 */
Jon Brassow3510cb92009-12-10 23:52:11 +00001926 e = dm_lookup_exception(&snap->complete, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001927 if (e)
1928 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
Mikulas Patocka29138082009-04-02 19:55:25 +01001930 pe = __lookup_pending_exception(snap, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001931 if (!pe) {
Mikulas Patockac6621392009-04-02 19:55:25 +01001932 up_write(&snap->lock);
1933 pe = alloc_pending_exception(snap);
1934 down_write(&snap->lock);
1935
1936 if (!snap->valid) {
1937 free_pending_exception(pe);
1938 goto next_snapshot;
1939 }
1940
Jon Brassow3510cb92009-12-10 23:52:11 +00001941 e = dm_lookup_exception(&snap->complete, chunk);
Mikulas Patocka35bf6592009-04-02 19:55:26 +01001942 if (e) {
1943 free_pending_exception(pe);
1944 goto next_snapshot;
1945 }
1946
Mikulas Patockac6621392009-04-02 19:55:25 +01001947 pe = __find_pending_exception(snap, pe, chunk);
Mikulas Patocka29138082009-04-02 19:55:25 +01001948 if (!pe) {
1949 __invalidate_snapshot(snap, -ENOMEM);
1950 goto next_snapshot;
1951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 }
1953
Mikulas Patocka515ad662009-12-10 23:52:30 +00001954 r = DM_MAPIO_SUBMITTED;
1955
1956 /*
1957 * If an origin bio was supplied, queue it to wait for the
1958 * completion of this exception, and start this one last,
1959 * at the end of the function.
1960 */
1961 if (bio) {
1962 bio_list_add(&pe->origin_bios, bio);
1963 bio = NULL;
1964
1965 if (!pe->started) {
1966 pe->started = 1;
1967 pe_to_start_last = pe;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001968 }
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001969 }
1970
1971 if (!pe->started) {
1972 pe->started = 1;
Mikulas Patocka515ad662009-12-10 23:52:30 +00001973 pe_to_start_now = pe;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001974 }
1975
1976 next_snapshot:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 up_write(&snap->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Mikulas Patocka515ad662009-12-10 23:52:30 +00001979 if (pe_to_start_now) {
1980 start_copy(pe_to_start_now);
1981 pe_to_start_now = NULL;
1982 }
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001983 }
1984
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 /*
Mikulas Patocka515ad662009-12-10 23:52:30 +00001986 * Submit the exception against which the bio is queued last,
1987 * to give the other exceptions a head start.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 */
Mikulas Patocka515ad662009-12-10 23:52:30 +00001989 if (pe_to_start_last)
1990 start_copy(pe_to_start_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
1992 return r;
1993}
1994
1995/*
1996 * Called on a write from the origin driver.
1997 */
1998static int do_origin(struct dm_dev *origin, struct bio *bio)
1999{
2000 struct origin *o;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08002001 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
2003 down_read(&_origins_lock);
2004 o = __lookup_origin(origin->bdev);
2005 if (o)
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00002006 r = __origin_write(&o->snapshots, bio->bi_sector, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 up_read(&_origins_lock);
2008
2009 return r;
2010}
2011
2012/*
Mikulas Patocka73dfd072009-12-10 23:52:34 +00002013 * Trigger exceptions in all non-merging snapshots.
2014 *
2015 * The chunk size of the merging snapshot may be larger than the chunk
2016 * size of some other snapshot so we may need to reallocate multiple
2017 * chunks in other snapshots.
2018 *
2019 * We scan all the overlapping exceptions in the other snapshots.
2020 * Returns 1 if anything was reallocated and must be waited for,
2021 * otherwise returns 0.
2022 *
2023 * size must be a multiple of merging_snap's chunk_size.
2024 */
2025static int origin_write_extent(struct dm_snapshot *merging_snap,
2026 sector_t sector, unsigned size)
2027{
2028 int must_wait = 0;
2029 sector_t n;
2030 struct origin *o;
2031
2032 /*
2033 * The origin's __minimum_chunk_size() got stored in split_io
2034 * by snapshot_merge_resume().
2035 */
2036 down_read(&_origins_lock);
2037 o = __lookup_origin(merging_snap->origin->bdev);
2038 for (n = 0; n < size; n += merging_snap->ti->split_io)
2039 if (__origin_write(&o->snapshots, sector + n, NULL) ==
2040 DM_MAPIO_SUBMITTED)
2041 must_wait = 1;
2042 up_read(&_origins_lock);
2043
2044 return must_wait;
2045}
2046
2047/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 * Origin: maps a linear range of a device, with hooks for snapshotting.
2049 */
2050
2051/*
2052 * Construct an origin mapping: <dev_path>
2053 * The context for an origin is merely a 'struct dm_dev *'
2054 * pointing to the real device.
2055 */
2056static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
2057{
2058 int r;
2059 struct dm_dev *dev;
2060
2061 if (argc != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002062 ti->error = "origin: incorrect number of arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 return -EINVAL;
2064 }
2065
Nikanth Karthikesan8215d6e2010-03-06 02:32:27 +00002066 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 if (r) {
2068 ti->error = "Cannot get target device";
2069 return r;
2070 }
2071
2072 ti->private = dev;
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01002073 ti->num_flush_requests = 1;
2074
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 return 0;
2076}
2077
2078static void origin_dtr(struct dm_target *ti)
2079{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002080 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 dm_put_device(ti, dev);
2082}
2083
2084static int origin_map(struct dm_target *ti, struct bio *bio,
2085 union map_info *map_context)
2086{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002087 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 bio->bi_bdev = dev->bdev;
2089
Tejun Heod87f4c12010-09-03 11:56:19 +02002090 if (bio->bi_rw & REQ_FLUSH)
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01002091 return DM_MAPIO_REMAPPED;
2092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 /* Only tell snapshots if this is a write */
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08002094 return (bio_rw(bio) == WRITE) ? do_origin(dev, bio) : DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095}
2096
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097/*
2098 * Set the target "split_io" field to the minimum of all the snapshots'
2099 * chunk sizes.
2100 */
2101static void origin_resume(struct dm_target *ti)
2102{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002103 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002105 ti->split_io = get_origin_minimum_chunksize(dev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106}
2107
2108static int origin_status(struct dm_target *ti, status_type_t type, char *result,
2109 unsigned int maxlen)
2110{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002111 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
2113 switch (type) {
2114 case STATUSTYPE_INFO:
2115 result[0] = '\0';
2116 break;
2117
2118 case STATUSTYPE_TABLE:
2119 snprintf(result, maxlen, "%s", dev->name);
2120 break;
2121 }
2122
2123 return 0;
2124}
2125
Mikulas Patockab1d55522010-08-12 04:14:02 +01002126static int origin_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
2127 struct bio_vec *biovec, int max_size)
2128{
2129 struct dm_dev *dev = ti->private;
2130 struct request_queue *q = bdev_get_queue(dev->bdev);
2131
2132 if (!q->merge_bvec_fn)
2133 return max_size;
2134
2135 bvm->bi_bdev = dev->bdev;
2136 bvm->bi_sector = bvm->bi_sector;
2137
2138 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
2139}
2140
Mike Snitzer8811f462009-09-04 20:40:19 +01002141static int origin_iterate_devices(struct dm_target *ti,
2142 iterate_devices_callout_fn fn, void *data)
2143{
2144 struct dm_dev *dev = ti->private;
2145
2146 return fn(ti, dev, 0, ti->len, data);
2147}
2148
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149static struct target_type origin_target = {
2150 .name = "snapshot-origin",
Mike Snitzerb83b2f22011-01-13 19:59:59 +00002151 .version = {1, 7, 1},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 .module = THIS_MODULE,
2153 .ctr = origin_ctr,
2154 .dtr = origin_dtr,
2155 .map = origin_map,
2156 .resume = origin_resume,
2157 .status = origin_status,
Mikulas Patockab1d55522010-08-12 04:14:02 +01002158 .merge = origin_merge,
Mike Snitzer8811f462009-09-04 20:40:19 +01002159 .iterate_devices = origin_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160};
2161
2162static struct target_type snapshot_target = {
2163 .name = "snapshot",
Mike Snitzerb83b2f22011-01-13 19:59:59 +00002164 .version = {1, 10, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 .module = THIS_MODULE,
2166 .ctr = snapshot_ctr,
2167 .dtr = snapshot_dtr,
2168 .map = snapshot_map,
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002169 .end_io = snapshot_end_io,
Mike Snitzerc1f0c182009-12-10 23:52:24 +00002170 .preresume = snapshot_preresume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 .resume = snapshot_resume,
2172 .status = snapshot_status,
Mike Snitzer8811f462009-09-04 20:40:19 +01002173 .iterate_devices = snapshot_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174};
2175
Mikulas Patockad698aa42009-12-10 23:52:30 +00002176static struct target_type merge_target = {
2177 .name = dm_snapshot_merge_target_name,
Mike Snitzerb83b2f22011-01-13 19:59:59 +00002178 .version = {1, 1, 0},
Mikulas Patockad698aa42009-12-10 23:52:30 +00002179 .module = THIS_MODULE,
2180 .ctr = snapshot_ctr,
2181 .dtr = snapshot_dtr,
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00002182 .map = snapshot_merge_map,
Mikulas Patockad698aa42009-12-10 23:52:30 +00002183 .end_io = snapshot_end_io,
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002184 .presuspend = snapshot_merge_presuspend,
Mikulas Patockad698aa42009-12-10 23:52:30 +00002185 .preresume = snapshot_preresume,
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002186 .resume = snapshot_merge_resume,
Mikulas Patockad698aa42009-12-10 23:52:30 +00002187 .status = snapshot_status,
2188 .iterate_devices = snapshot_iterate_devices,
2189};
2190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191static int __init dm_snapshot_init(void)
2192{
2193 int r;
2194
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00002195 r = dm_exception_store_init();
2196 if (r) {
2197 DMERR("Failed to initialize exception stores");
2198 return r;
2199 }
2200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 r = dm_register_target(&snapshot_target);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002202 if (r < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 DMERR("snapshot target register failed %d", r);
Jonathan Brassow034a1862009-10-16 23:18:14 +01002204 goto bad_register_snapshot_target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 }
2206
2207 r = dm_register_target(&origin_target);
2208 if (r < 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002209 DMERR("Origin target register failed %d", r);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002210 goto bad_register_origin_target;
2211 }
2212
2213 r = dm_register_target(&merge_target);
2214 if (r < 0) {
2215 DMERR("Merge target register failed %d", r);
2216 goto bad_register_merge_target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 }
2218
2219 r = init_origin_hash();
2220 if (r) {
2221 DMERR("init_origin_hash failed.");
Mikulas Patockad698aa42009-12-10 23:52:30 +00002222 goto bad_origin_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 }
2224
Jon Brassow1d4989c2009-12-10 23:52:10 +00002225 exception_cache = KMEM_CACHE(dm_exception, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 if (!exception_cache) {
2227 DMERR("Couldn't create exception cache.");
2228 r = -ENOMEM;
Mikulas Patockad698aa42009-12-10 23:52:30 +00002229 goto bad_exception_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 }
2231
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002232 pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 if (!pending_cache) {
2234 DMERR("Couldn't create pending cache.");
2235 r = -ENOMEM;
Mikulas Patockad698aa42009-12-10 23:52:30 +00002236 goto bad_pending_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 }
2238
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002239 tracked_chunk_cache = KMEM_CACHE(dm_snap_tracked_chunk, 0);
2240 if (!tracked_chunk_cache) {
2241 DMERR("Couldn't create cache to track chunks in use.");
2242 r = -ENOMEM;
Mikulas Patockad698aa42009-12-10 23:52:30 +00002243 goto bad_tracked_chunk_cache;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002244 }
2245
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 return 0;
2247
Mikulas Patockad698aa42009-12-10 23:52:30 +00002248bad_tracked_chunk_cache:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 kmem_cache_destroy(pending_cache);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002250bad_pending_cache:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 kmem_cache_destroy(exception_cache);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002252bad_exception_cache:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 exit_origin_hash();
Mikulas Patockad698aa42009-12-10 23:52:30 +00002254bad_origin_hash:
2255 dm_unregister_target(&merge_target);
2256bad_register_merge_target:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 dm_unregister_target(&origin_target);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002258bad_register_origin_target:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 dm_unregister_target(&snapshot_target);
Jonathan Brassow034a1862009-10-16 23:18:14 +01002260bad_register_snapshot_target:
2261 dm_exception_store_exit();
Mikulas Patockad698aa42009-12-10 23:52:30 +00002262
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 return r;
2264}
2265
2266static void __exit dm_snapshot_exit(void)
2267{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00002268 dm_unregister_target(&snapshot_target);
2269 dm_unregister_target(&origin_target);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002270 dm_unregister_target(&merge_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
2272 exit_origin_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 kmem_cache_destroy(pending_cache);
2274 kmem_cache_destroy(exception_cache);
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002275 kmem_cache_destroy(tracked_chunk_cache);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00002276
2277 dm_exception_store_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278}
2279
2280/* Module hooks */
2281module_init(dm_snapshot_init);
2282module_exit(dm_snapshot_exit);
2283
2284MODULE_DESCRIPTION(DM_NAME " snapshot target");
2285MODULE_AUTHOR("Joe Thornber");
2286MODULE_LICENSE("GPL");