blob: a2d330942cb29f824c029973ae12a98a2d39efec [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
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010082 atomic_t pending_exceptions_count;
83
Mike Snitzer924e6002010-03-06 02:32:33 +000084 mempool_t *pending_pool;
85
Jon Brassow191437a2009-12-10 23:52:10 +000086 struct dm_exception_table pending;
87 struct dm_exception_table complete;
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010088
89 /*
90 * pe_lock protects all pending_exception operations and access
91 * as well as the snapshot_bios list.
92 */
93 spinlock_t pe_lock;
94
Mike Snitzer924e6002010-03-06 02:32:33 +000095 /* Chunks with outstanding reads */
96 spinlock_t tracked_chunk_lock;
97 mempool_t *tracked_chunk_pool;
98 struct hlist_head tracked_chunk_hash[DM_TRACKED_CHUNK_HASH_SIZE];
99
Jonathan Brassowccc45ea2009-04-02 19:55:34 +0100100 /* The on disk metadata handler */
101 struct dm_exception_store *store;
102
103 struct dm_kcopyd_client *kcopyd_client;
104
Mike Snitzer924e6002010-03-06 02:32:33 +0000105 /* Wait for events based on state_bits */
106 unsigned long state_bits;
107
108 /* Range of chunks currently being merged. */
109 chunk_t first_merging_chunk;
110 int num_merging_chunks;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000111
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +0000112 /*
113 * The merge operation failed if this flag is set.
114 * Failure modes are handled as follows:
115 * - I/O error reading the header
116 * => don't load the target; abort.
117 * - Header does not have "valid" flag set
118 * => use the origin; forget about the snapshot.
119 * - I/O error when reading exceptions
120 * => don't load the target; abort.
121 * (We can't use the intermediate origin state.)
122 * - I/O error while merging
123 * => stop merging; set merge_failed; process I/O normally.
124 */
125 int merge_failed;
126
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000127 /*
128 * Incoming bios that overlap with chunks being merged must wait
129 * for them to be committed.
130 */
131 struct bio_list bios_queued_during_merge;
Jonathan Brassowccc45ea2009-04-02 19:55:34 +0100132};
133
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000134/*
135 * state_bits:
136 * RUNNING_MERGE - Merge operation is in progress.
137 * SHUTDOWN_MERGE - Set to signal that merge needs to be stopped;
138 * cleared afterwards.
139 */
140#define RUNNING_MERGE 0
141#define SHUTDOWN_MERGE 1
142
Mikulas Patockac2411042010-08-12 04:13:51 +0100143struct dm_dev *dm_snap_origin(struct dm_snapshot *s)
144{
145 return s->origin;
146}
147EXPORT_SYMBOL(dm_snap_origin);
148
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000149struct dm_dev *dm_snap_cow(struct dm_snapshot *s)
150{
151 return s->cow;
152}
153EXPORT_SYMBOL(dm_snap_cow);
154
Jonathan Brassowccc45ea2009-04-02 19:55:34 +0100155static sector_t chunk_to_sector(struct dm_exception_store *store,
156 chunk_t chunk)
157{
158 return chunk << store->chunk_shift;
159}
160
161static int bdev_equal(struct block_device *lhs, struct block_device *rhs)
162{
163 /*
164 * There is only ever one instance of a particular block
165 * device so we can compare pointers safely.
166 */
167 return lhs == rhs;
168}
169
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100170struct dm_snap_pending_exception {
Jon Brassow1d4989c2009-12-10 23:52:10 +0000171 struct dm_exception e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 /*
174 * Origin buffers waiting for this to complete are held
175 * in a bio list
176 */
177 struct bio_list origin_bios;
178 struct bio_list snapshot_bios;
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 /* Pointer back to snapshot context */
181 struct dm_snapshot *snap;
182
183 /*
184 * 1 indicates the exception has already been sent to
185 * kcopyd.
186 */
187 int started;
188};
189
190/*
191 * Hash table mapping origin volumes to lists of snapshots and
192 * a lock to protect it
193 */
Christoph Lametere18b8902006-12-06 20:33:20 -0800194static struct kmem_cache *exception_cache;
195static struct kmem_cache *pending_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100197struct dm_snap_tracked_chunk {
198 struct hlist_node node;
199 chunk_t chunk;
200};
201
202static struct kmem_cache *tracked_chunk_cache;
203
204static struct dm_snap_tracked_chunk *track_chunk(struct dm_snapshot *s,
205 chunk_t chunk)
206{
207 struct dm_snap_tracked_chunk *c = mempool_alloc(s->tracked_chunk_pool,
208 GFP_NOIO);
209 unsigned long flags;
210
211 c->chunk = chunk;
212
213 spin_lock_irqsave(&s->tracked_chunk_lock, flags);
214 hlist_add_head(&c->node,
215 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)]);
216 spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
217
218 return c;
219}
220
221static void stop_tracking_chunk(struct dm_snapshot *s,
222 struct dm_snap_tracked_chunk *c)
223{
224 unsigned long flags;
225
226 spin_lock_irqsave(&s->tracked_chunk_lock, flags);
227 hlist_del(&c->node);
228 spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
229
230 mempool_free(c, s->tracked_chunk_pool);
231}
232
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100233static int __chunk_is_tracked(struct dm_snapshot *s, chunk_t chunk)
234{
235 struct dm_snap_tracked_chunk *c;
236 struct hlist_node *hn;
237 int found = 0;
238
239 spin_lock_irq(&s->tracked_chunk_lock);
240
241 hlist_for_each_entry(c, hn,
242 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)], node) {
243 if (c->chunk == chunk) {
244 found = 1;
245 break;
246 }
247 }
248
249 spin_unlock_irq(&s->tracked_chunk_lock);
250
251 return found;
252}
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254/*
Mike Snitzer615d1eb2009-12-10 23:52:29 +0000255 * This conflicting I/O is extremely improbable in the caller,
256 * so msleep(1) is sufficient and there is no need for a wait queue.
257 */
258static void __check_for_conflicting_io(struct dm_snapshot *s, chunk_t chunk)
259{
260 while (__chunk_is_tracked(s, chunk))
261 msleep(1);
262}
263
264/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 * One of these per registered origin, held in the snapshot_origins hash
266 */
267struct origin {
268 /* The origin device */
269 struct block_device *bdev;
270
271 struct list_head hash_list;
272
273 /* List of snapshots for this origin */
274 struct list_head snapshots;
275};
276
277/*
278 * Size of the hash table for origin volumes. If we make this
279 * the size of the minors list then it should be nearly perfect
280 */
281#define ORIGIN_HASH_SIZE 256
282#define ORIGIN_MASK 0xFF
283static struct list_head *_origins;
284static struct rw_semaphore _origins_lock;
285
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000286static DECLARE_WAIT_QUEUE_HEAD(_pending_exceptions_done);
287static DEFINE_SPINLOCK(_pending_exceptions_done_spinlock);
288static uint64_t _pending_exceptions_done_count;
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290static int init_origin_hash(void)
291{
292 int i;
293
294 _origins = kmalloc(ORIGIN_HASH_SIZE * sizeof(struct list_head),
295 GFP_KERNEL);
296 if (!_origins) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700297 DMERR("unable to allocate memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 return -ENOMEM;
299 }
300
301 for (i = 0; i < ORIGIN_HASH_SIZE; i++)
302 INIT_LIST_HEAD(_origins + i);
303 init_rwsem(&_origins_lock);
304
305 return 0;
306}
307
308static void exit_origin_hash(void)
309{
310 kfree(_origins);
311}
312
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100313static unsigned origin_hash(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
315 return bdev->bd_dev & ORIGIN_MASK;
316}
317
318static struct origin *__lookup_origin(struct block_device *origin)
319{
320 struct list_head *ol;
321 struct origin *o;
322
323 ol = &_origins[origin_hash(origin)];
324 list_for_each_entry (o, ol, hash_list)
325 if (bdev_equal(o->bdev, origin))
326 return o;
327
328 return NULL;
329}
330
331static void __insert_origin(struct origin *o)
332{
333 struct list_head *sl = &_origins[origin_hash(o->bdev)];
334 list_add_tail(&o->hash_list, sl);
335}
336
337/*
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000338 * _origins_lock must be held when calling this function.
339 * Returns number of snapshots registered using the supplied cow device, plus:
340 * snap_src - a snapshot suitable for use as a source of exception handover
341 * snap_dest - a snapshot capable of receiving exception handover.
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000342 * snap_merge - an existing snapshot-merge target linked to the same origin.
343 * There can be at most one snapshot-merge target. The parameter is optional.
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000344 *
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000345 * Possible return values and states of snap_src and snap_dest.
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000346 * 0: NULL, NULL - first new snapshot
347 * 1: snap_src, NULL - normal snapshot
348 * 2: snap_src, snap_dest - waiting for handover
349 * 2: snap_src, NULL - handed over, waiting for old to be deleted
350 * 1: NULL, snap_dest - source got destroyed without handover
351 */
352static int __find_snapshots_sharing_cow(struct dm_snapshot *snap,
353 struct dm_snapshot **snap_src,
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000354 struct dm_snapshot **snap_dest,
355 struct dm_snapshot **snap_merge)
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000356{
357 struct dm_snapshot *s;
358 struct origin *o;
359 int count = 0;
360 int active;
361
362 o = __lookup_origin(snap->origin->bdev);
363 if (!o)
364 goto out;
365
366 list_for_each_entry(s, &o->snapshots, list) {
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000367 if (dm_target_is_snapshot_merge(s->ti) && snap_merge)
368 *snap_merge = s;
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000369 if (!bdev_equal(s->cow->bdev, snap->cow->bdev))
370 continue;
371
372 down_read(&s->lock);
373 active = s->active;
374 up_read(&s->lock);
375
376 if (active) {
377 if (snap_src)
378 *snap_src = s;
379 } else if (snap_dest)
380 *snap_dest = s;
381
382 count++;
383 }
384
385out:
386 return count;
387}
388
389/*
390 * On success, returns 1 if this snapshot is a handover destination,
391 * otherwise returns 0.
392 */
393static int __validate_exception_handover(struct dm_snapshot *snap)
394{
395 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000396 struct dm_snapshot *snap_merge = NULL;
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000397
398 /* Does snapshot need exceptions handed over to it? */
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000399 if ((__find_snapshots_sharing_cow(snap, &snap_src, &snap_dest,
400 &snap_merge) == 2) ||
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000401 snap_dest) {
402 snap->ti->error = "Snapshot cow pairing for exception "
403 "table handover failed";
404 return -EINVAL;
405 }
406
407 /*
408 * If no snap_src was found, snap cannot become a handover
409 * destination.
410 */
411 if (!snap_src)
412 return 0;
413
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000414 /*
415 * Non-snapshot-merge handover?
416 */
417 if (!dm_target_is_snapshot_merge(snap->ti))
418 return 1;
419
420 /*
421 * Do not allow more than one merging snapshot.
422 */
423 if (snap_merge) {
424 snap->ti->error = "A snapshot is already merging.";
425 return -EINVAL;
426 }
427
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000428 if (!snap_src->store->type->prepare_merge ||
429 !snap_src->store->type->commit_merge) {
430 snap->ti->error = "Snapshot exception store does not "
431 "support snapshot-merge.";
432 return -EINVAL;
433 }
434
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000435 return 1;
436}
437
438static void __insert_snapshot(struct origin *o, struct dm_snapshot *s)
439{
440 struct dm_snapshot *l;
441
442 /* Sort the list according to chunk size, largest-first smallest-last */
443 list_for_each_entry(l, &o->snapshots, list)
444 if (l->store->chunk_size < s->store->chunk_size)
445 break;
446 list_add_tail(&s->list, &l->list);
447}
448
449/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 * Make a note of the snapshot and its origin so we can look it
451 * up when the origin has a write on it.
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000452 *
453 * Also validate snapshot exception store handovers.
454 * On success, returns 1 if this registration is a handover destination,
455 * otherwise returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 */
457static int register_snapshot(struct dm_snapshot *snap)
458{
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000459 struct origin *o, *new_o = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 struct block_device *bdev = snap->origin->bdev;
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000461 int r = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000463 new_o = kmalloc(sizeof(*new_o), GFP_KERNEL);
464 if (!new_o)
465 return -ENOMEM;
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 down_write(&_origins_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000469 r = __validate_exception_handover(snap);
470 if (r < 0) {
471 kfree(new_o);
472 goto out;
473 }
474
475 o = __lookup_origin(bdev);
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000476 if (o)
477 kfree(new_o);
478 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 /* New origin */
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000480 o = new_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 /* Initialise the struct */
483 INIT_LIST_HEAD(&o->snapshots);
484 o->bdev = bdev;
485
486 __insert_origin(o);
487 }
488
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000489 __insert_snapshot(o, snap);
490
491out:
492 up_write(&_origins_lock);
493
494 return r;
495}
496
497/*
498 * Move snapshot to correct place in list according to chunk size.
499 */
500static void reregister_snapshot(struct dm_snapshot *s)
501{
502 struct block_device *bdev = s->origin->bdev;
503
504 down_write(&_origins_lock);
505
506 list_del(&s->list);
507 __insert_snapshot(__lookup_origin(bdev), s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 up_write(&_origins_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
512static void unregister_snapshot(struct dm_snapshot *s)
513{
514 struct origin *o;
515
516 down_write(&_origins_lock);
517 o = __lookup_origin(s->origin->bdev);
518
519 list_del(&s->list);
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000520 if (o && list_empty(&o->snapshots)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 list_del(&o->hash_list);
522 kfree(o);
523 }
524
525 up_write(&_origins_lock);
526}
527
528/*
529 * Implementation of the exception hash tables.
Milan Brozd74f81f2008-02-08 02:11:27 +0000530 * The lowest hash_shift bits of the chunk number are ignored, allowing
531 * some consecutive chunks to be grouped together.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 */
Jon Brassow3510cb92009-12-10 23:52:11 +0000533static int dm_exception_table_init(struct dm_exception_table *et,
534 uint32_t size, unsigned hash_shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
536 unsigned int i;
537
Milan Brozd74f81f2008-02-08 02:11:27 +0000538 et->hash_shift = hash_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 et->hash_mask = size - 1;
540 et->table = dm_vcalloc(size, sizeof(struct list_head));
541 if (!et->table)
542 return -ENOMEM;
543
544 for (i = 0; i < size; i++)
545 INIT_LIST_HEAD(et->table + i);
546
547 return 0;
548}
549
Jon Brassow3510cb92009-12-10 23:52:11 +0000550static void dm_exception_table_exit(struct dm_exception_table *et,
551 struct kmem_cache *mem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 struct list_head *slot;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000554 struct dm_exception *ex, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 int i, size;
556
557 size = et->hash_mask + 1;
558 for (i = 0; i < size; i++) {
559 slot = et->table + i;
560
561 list_for_each_entry_safe (ex, next, slot, hash_list)
562 kmem_cache_free(mem, ex);
563 }
564
565 vfree(et->table);
566}
567
Jon Brassow191437a2009-12-10 23:52:10 +0000568static uint32_t exception_hash(struct dm_exception_table *et, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Milan Brozd74f81f2008-02-08 02:11:27 +0000570 return (chunk >> et->hash_shift) & et->hash_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
Jon Brassow3510cb92009-12-10 23:52:11 +0000573static void dm_remove_exception(struct dm_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
575 list_del(&e->hash_list);
576}
577
578/*
579 * Return the exception data for a sector, or NULL if not
580 * remapped.
581 */
Jon Brassow3510cb92009-12-10 23:52:11 +0000582static struct dm_exception *dm_lookup_exception(struct dm_exception_table *et,
583 chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
585 struct list_head *slot;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000586 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 slot = &et->table[exception_hash(et, chunk)];
589 list_for_each_entry (e, slot, hash_list)
Milan Brozd74f81f2008-02-08 02:11:27 +0000590 if (chunk >= e->old_chunk &&
591 chunk <= e->old_chunk + dm_consecutive_chunk_count(e))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 return e;
593
594 return NULL;
595}
596
Jon Brassow3510cb92009-12-10 23:52:11 +0000597static struct dm_exception *alloc_completed_exception(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Jon Brassow1d4989c2009-12-10 23:52:10 +0000599 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 e = kmem_cache_alloc(exception_cache, GFP_NOIO);
602 if (!e)
603 e = kmem_cache_alloc(exception_cache, GFP_ATOMIC);
604
605 return e;
606}
607
Jon Brassow3510cb92009-12-10 23:52:11 +0000608static void free_completed_exception(struct dm_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
610 kmem_cache_free(exception_cache, e);
611}
612
Mikulas Patocka92e86812008-07-21 12:00:35 +0100613static struct dm_snap_pending_exception *alloc_pending_exception(struct dm_snapshot *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Mikulas Patocka92e86812008-07-21 12:00:35 +0100615 struct dm_snap_pending_exception *pe = mempool_alloc(s->pending_pool,
616 GFP_NOIO);
617
Mikulas Patocka879129d22008-10-30 13:33:16 +0000618 atomic_inc(&s->pending_exceptions_count);
Mikulas Patocka92e86812008-07-21 12:00:35 +0100619 pe->snap = s;
620
621 return pe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100624static void free_pending_exception(struct dm_snap_pending_exception *pe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Mikulas Patocka879129d22008-10-30 13:33:16 +0000626 struct dm_snapshot *s = pe->snap;
627
628 mempool_free(pe, s->pending_pool);
629 smp_mb__before_atomic_dec();
630 atomic_dec(&s->pending_exceptions_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
Jon Brassow3510cb92009-12-10 23:52:11 +0000633static void dm_insert_exception(struct dm_exception_table *eh,
634 struct dm_exception *new_e)
Milan Brozd74f81f2008-02-08 02:11:27 +0000635{
Milan Brozd74f81f2008-02-08 02:11:27 +0000636 struct list_head *l;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000637 struct dm_exception *e = NULL;
Milan Brozd74f81f2008-02-08 02:11:27 +0000638
639 l = &eh->table[exception_hash(eh, new_e->old_chunk)];
640
641 /* Add immediately if this table doesn't support consecutive chunks */
642 if (!eh->hash_shift)
643 goto out;
644
645 /* List is ordered by old_chunk */
646 list_for_each_entry_reverse(e, l, hash_list) {
647 /* Insert after an existing chunk? */
648 if (new_e->old_chunk == (e->old_chunk +
649 dm_consecutive_chunk_count(e) + 1) &&
650 new_e->new_chunk == (dm_chunk_number(e->new_chunk) +
651 dm_consecutive_chunk_count(e) + 1)) {
652 dm_consecutive_chunk_count_inc(e);
Jon Brassow3510cb92009-12-10 23:52:11 +0000653 free_completed_exception(new_e);
Milan Brozd74f81f2008-02-08 02:11:27 +0000654 return;
655 }
656
657 /* Insert before an existing chunk? */
658 if (new_e->old_chunk == (e->old_chunk - 1) &&
659 new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) {
660 dm_consecutive_chunk_count_inc(e);
661 e->old_chunk--;
662 e->new_chunk--;
Jon Brassow3510cb92009-12-10 23:52:11 +0000663 free_completed_exception(new_e);
Milan Brozd74f81f2008-02-08 02:11:27 +0000664 return;
665 }
666
667 if (new_e->old_chunk > e->old_chunk)
668 break;
669 }
670
671out:
672 list_add(&new_e->hash_list, e ? &e->hash_list : l);
673}
674
Jonathan Brassowa159c1a2009-01-06 03:05:19 +0000675/*
676 * Callback used by the exception stores to load exceptions when
677 * initialising.
678 */
679static int dm_add_exception(void *context, chunk_t old, chunk_t new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Jonathan Brassowa159c1a2009-01-06 03:05:19 +0000681 struct dm_snapshot *s = context;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000682 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Jon Brassow3510cb92009-12-10 23:52:11 +0000684 e = alloc_completed_exception();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (!e)
686 return -ENOMEM;
687
688 e->old_chunk = old;
Milan Brozd74f81f2008-02-08 02:11:27 +0000689
690 /* Consecutive_count is implicitly initialised to zero */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 e->new_chunk = new;
Milan Brozd74f81f2008-02-08 02:11:27 +0000692
Jon Brassow3510cb92009-12-10 23:52:11 +0000693 dm_insert_exception(&s->complete, e);
Milan Brozd74f81f2008-02-08 02:11:27 +0000694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return 0;
696}
697
Mikulas Patocka7e201b32009-12-10 23:52:08 +0000698/*
699 * Return a minimum chunk size of all snapshots that have the specified origin.
700 * Return zero if the origin has no snapshots.
701 */
702static sector_t __minimum_chunk_size(struct origin *o)
703{
704 struct dm_snapshot *snap;
705 unsigned chunk_size = 0;
706
707 if (o)
708 list_for_each_entry(snap, &o->snapshots, list)
709 chunk_size = min_not_zero(chunk_size,
710 snap->store->chunk_size);
711
712 return chunk_size;
713}
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715/*
716 * Hard coded magic.
717 */
718static int calc_max_buckets(void)
719{
720 /* use a fixed size of 2MB */
721 unsigned long mem = 2 * 1024 * 1024;
722 mem /= sizeof(struct list_head);
723
724 return mem;
725}
726
727/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 * Allocate room for a suitable hash table.
729 */
Jonathan Brassowfee19982009-04-02 19:55:34 +0100730static int init_hash_tables(struct dm_snapshot *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
732 sector_t hash_size, cow_dev_size, origin_dev_size, max_buckets;
733
734 /*
735 * Calculate based on the size of the original volume or
736 * the COW volume...
737 */
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000738 cow_dev_size = get_dev_size(s->cow->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 origin_dev_size = get_dev_size(s->origin->bdev);
740 max_buckets = calc_max_buckets();
741
Jonathan Brassowfee19982009-04-02 19:55:34 +0100742 hash_size = min(origin_dev_size, cow_dev_size) >> s->store->chunk_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 hash_size = min(hash_size, max_buckets);
744
Mikulas Patocka8e87b9b2009-12-10 23:51:54 +0000745 if (hash_size < 64)
746 hash_size = 64;
Robert P. J. Day8defd832008-02-08 02:10:06 +0000747 hash_size = rounddown_pow_of_two(hash_size);
Jon Brassow3510cb92009-12-10 23:52:11 +0000748 if (dm_exception_table_init(&s->complete, hash_size,
749 DM_CHUNK_CONSECUTIVE_BITS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return -ENOMEM;
751
752 /*
753 * Allocate hash table for in-flight exceptions
754 * Make this smaller than the real hash table
755 */
756 hash_size >>= 3;
757 if (hash_size < 64)
758 hash_size = 64;
759
Jon Brassow3510cb92009-12-10 23:52:11 +0000760 if (dm_exception_table_init(&s->pending, hash_size, 0)) {
761 dm_exception_table_exit(&s->complete, exception_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return -ENOMEM;
763 }
764
765 return 0;
766}
767
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000768static void merge_shutdown(struct dm_snapshot *s)
769{
770 clear_bit_unlock(RUNNING_MERGE, &s->state_bits);
771 smp_mb__after_clear_bit();
772 wake_up_bit(&s->state_bits, RUNNING_MERGE);
773}
774
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000775static struct bio *__release_queued_bios_after_merge(struct dm_snapshot *s)
776{
777 s->first_merging_chunk = 0;
778 s->num_merging_chunks = 0;
779
780 return bio_list_get(&s->bios_queued_during_merge);
781}
782
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000783/*
784 * Remove one chunk from the index of completed exceptions.
785 */
786static int __remove_single_exception_chunk(struct dm_snapshot *s,
787 chunk_t old_chunk)
788{
789 struct dm_exception *e;
790
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000791 e = dm_lookup_exception(&s->complete, old_chunk);
792 if (!e) {
793 DMERR("Corruption detected: exception for block %llu is "
794 "on disk but not in memory",
795 (unsigned long long)old_chunk);
796 return -EINVAL;
797 }
798
799 /*
800 * If this is the only chunk using this exception, remove exception.
801 */
802 if (!dm_consecutive_chunk_count(e)) {
803 dm_remove_exception(e);
804 free_completed_exception(e);
805 return 0;
806 }
807
808 /*
809 * The chunk may be either at the beginning or the end of a
810 * group of consecutive chunks - never in the middle. We are
811 * removing chunks in the opposite order to that in which they
812 * were added, so this should always be true.
813 * Decrement the consecutive chunk counter and adjust the
814 * starting point if necessary.
815 */
816 if (old_chunk == e->old_chunk) {
817 e->old_chunk++;
818 e->new_chunk++;
819 } else if (old_chunk != e->old_chunk +
820 dm_consecutive_chunk_count(e)) {
821 DMERR("Attempt to merge block %llu from the "
822 "middle of a chunk range [%llu - %llu]",
823 (unsigned long long)old_chunk,
824 (unsigned long long)e->old_chunk,
825 (unsigned long long)
826 e->old_chunk + dm_consecutive_chunk_count(e));
827 return -EINVAL;
828 }
829
830 dm_consecutive_chunk_count_dec(e);
831
832 return 0;
833}
834
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000835static void flush_bios(struct bio *bio);
836
837static int remove_single_exception_chunk(struct dm_snapshot *s)
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000838{
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000839 struct bio *b = NULL;
840 int r;
841 chunk_t old_chunk = s->first_merging_chunk + s->num_merging_chunks - 1;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000842
843 down_write(&s->lock);
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000844
845 /*
846 * Process chunks (and associated exceptions) in reverse order
847 * so that dm_consecutive_chunk_count_dec() accounting works.
848 */
849 do {
850 r = __remove_single_exception_chunk(s, old_chunk);
851 if (r)
852 goto out;
853 } while (old_chunk-- > s->first_merging_chunk);
854
855 b = __release_queued_bios_after_merge(s);
856
857out:
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000858 up_write(&s->lock);
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000859 if (b)
860 flush_bios(b);
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000861
862 return r;
863}
864
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000865static int origin_write_extent(struct dm_snapshot *merging_snap,
866 sector_t sector, unsigned chunk_size);
867
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000868static void merge_callback(int read_err, unsigned long write_err,
869 void *context);
870
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000871static uint64_t read_pending_exceptions_done_count(void)
872{
873 uint64_t pending_exceptions_done;
874
875 spin_lock(&_pending_exceptions_done_spinlock);
876 pending_exceptions_done = _pending_exceptions_done_count;
877 spin_unlock(&_pending_exceptions_done_spinlock);
878
879 return pending_exceptions_done;
880}
881
882static void increment_pending_exceptions_done_count(void)
883{
884 spin_lock(&_pending_exceptions_done_spinlock);
885 _pending_exceptions_done_count++;
886 spin_unlock(&_pending_exceptions_done_spinlock);
887
888 wake_up_all(&_pending_exceptions_done);
889}
890
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000891static void snapshot_merge_next_chunks(struct dm_snapshot *s)
892{
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000893 int i, linear_chunks;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000894 chunk_t old_chunk, new_chunk;
895 struct dm_io_region src, dest;
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000896 sector_t io_size;
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000897 uint64_t previous_count;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000898
899 BUG_ON(!test_bit(RUNNING_MERGE, &s->state_bits));
900 if (unlikely(test_bit(SHUTDOWN_MERGE, &s->state_bits)))
901 goto shut;
902
903 /*
904 * valid flag never changes during merge, so no lock required.
905 */
906 if (!s->valid) {
907 DMERR("Snapshot is invalid: can't merge");
908 goto shut;
909 }
910
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000911 linear_chunks = s->store->type->prepare_merge(s->store, &old_chunk,
912 &new_chunk);
913 if (linear_chunks <= 0) {
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +0000914 if (linear_chunks < 0) {
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000915 DMERR("Read error in exception store: "
916 "shutting down merge");
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +0000917 down_write(&s->lock);
918 s->merge_failed = 1;
919 up_write(&s->lock);
920 }
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000921 goto shut;
922 }
923
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000924 /* Adjust old_chunk and new_chunk to reflect start of linear region */
925 old_chunk = old_chunk + 1 - linear_chunks;
926 new_chunk = new_chunk + 1 - linear_chunks;
927
928 /*
929 * Use one (potentially large) I/O to copy all 'linear_chunks'
930 * from the exception store to the origin
931 */
932 io_size = linear_chunks * s->store->chunk_size;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000933
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000934 dest.bdev = s->origin->bdev;
935 dest.sector = chunk_to_sector(s->store, old_chunk);
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000936 dest.count = min(io_size, get_dev_size(dest.bdev) - dest.sector);
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000937
938 src.bdev = s->cow->bdev;
939 src.sector = chunk_to_sector(s->store, new_chunk);
940 src.count = dest.count;
941
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000942 /*
943 * Reallocate any exceptions needed in other snapshots then
944 * wait for the pending exceptions to complete.
945 * Each time any pending exception (globally on the system)
946 * completes we are woken and repeat the process to find out
947 * if we can proceed. While this may not seem a particularly
948 * efficient algorithm, it is not expected to have any
949 * significant impact on performance.
950 */
951 previous_count = read_pending_exceptions_done_count();
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000952 while (origin_write_extent(s, dest.sector, io_size)) {
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000953 wait_event(_pending_exceptions_done,
954 (read_pending_exceptions_done_count() !=
955 previous_count));
956 /* Retry after the wait, until all exceptions are done. */
957 previous_count = read_pending_exceptions_done_count();
958 }
959
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000960 down_write(&s->lock);
961 s->first_merging_chunk = old_chunk;
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000962 s->num_merging_chunks = linear_chunks;
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000963 up_write(&s->lock);
964
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000965 /* Wait until writes to all 'linear_chunks' drain */
966 for (i = 0; i < linear_chunks; i++)
967 __check_for_conflicting_io(s, old_chunk + i);
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000968
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000969 dm_kcopyd_copy(s->kcopyd_client, &src, 1, &dest, 0, merge_callback, s);
970 return;
971
972shut:
973 merge_shutdown(s);
974}
975
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000976static void error_bios(struct bio *bio);
977
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000978static void merge_callback(int read_err, unsigned long write_err, void *context)
979{
980 struct dm_snapshot *s = context;
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000981 struct bio *b = NULL;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000982
983 if (read_err || write_err) {
984 if (read_err)
985 DMERR("Read error: shutting down merge.");
986 else
987 DMERR("Write error: shutting down merge.");
988 goto shut;
989 }
990
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000991 if (s->store->type->commit_merge(s->store,
992 s->num_merging_chunks) < 0) {
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000993 DMERR("Write error in exception store: shutting down merge");
994 goto shut;
995 }
996
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000997 if (remove_single_exception_chunk(s) < 0)
998 goto shut;
999
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001000 snapshot_merge_next_chunks(s);
1001
1002 return;
1003
1004shut:
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001005 down_write(&s->lock);
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +00001006 s->merge_failed = 1;
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001007 b = __release_queued_bios_after_merge(s);
1008 up_write(&s->lock);
1009 error_bios(b);
1010
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001011 merge_shutdown(s);
1012}
1013
1014static void start_merge(struct dm_snapshot *s)
1015{
1016 if (!test_and_set_bit(RUNNING_MERGE, &s->state_bits))
1017 snapshot_merge_next_chunks(s);
1018}
1019
1020static int wait_schedule(void *ptr)
1021{
1022 schedule();
1023
1024 return 0;
1025}
1026
1027/*
1028 * Stop the merging process and wait until it finishes.
1029 */
1030static void stop_merge(struct dm_snapshot *s)
1031{
1032 set_bit(SHUTDOWN_MERGE, &s->state_bits);
1033 wait_on_bit(&s->state_bits, RUNNING_MERGE, wait_schedule,
1034 TASK_UNINTERRUPTIBLE);
1035 clear_bit(SHUTDOWN_MERGE, &s->state_bits);
1036}
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 * Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size>
1040 */
1041static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1042{
1043 struct dm_snapshot *s;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001044 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 int r = -EINVAL;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001046 char *origin_path, *cow_path;
Mike Snitzer10b81062009-12-10 23:52:31 +00001047 unsigned args_used, num_flush_requests = 1;
1048 fmode_t origin_mode = FMODE_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -07001050 if (argc != 4) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001051 ti->error = "requires exactly 4 arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 r = -EINVAL;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001053 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 }
1055
Mike Snitzer10b81062009-12-10 23:52:31 +00001056 if (dm_target_is_snapshot_merge(ti)) {
1057 num_flush_requests = 2;
1058 origin_mode = FMODE_WRITE;
1059 }
1060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 s = kmalloc(sizeof(*s), GFP_KERNEL);
Jonathan Brassowfee19982009-04-02 19:55:34 +01001062 if (!s) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 ti->error = "Cannot allocate snapshot context private "
1064 "structure";
1065 r = -ENOMEM;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001066 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068
Mikulas Patockac2411042010-08-12 04:13:51 +01001069 origin_path = argv[0];
1070 argv++;
1071 argc--;
1072
1073 r = dm_get_device(ti, origin_path, origin_mode, &s->origin);
1074 if (r) {
1075 ti->error = "Cannot get origin device";
1076 goto bad_origin;
1077 }
1078
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001079 cow_path = argv[0];
1080 argv++;
1081 argc--;
1082
Milan Broz024d37e2011-03-24 13:52:14 +00001083 r = dm_get_device(ti, cow_path, dm_table_get_mode(ti->table), &s->cow);
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001084 if (r) {
1085 ti->error = "Cannot get COW device";
1086 goto bad_cow;
1087 }
1088
1089 r = dm_exception_store_create(ti, argc, argv, s, &args_used, &s->store);
1090 if (r) {
1091 ti->error = "Couldn't create exception store";
1092 r = -EINVAL;
1093 goto bad_store;
1094 }
1095
1096 argv += args_used;
1097 argc -= args_used;
1098
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001099 s->ti = ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 s->valid = 1;
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001101 s->active = 0;
Mikulas Patocka879129d22008-10-30 13:33:16 +00001102 atomic_set(&s->pending_exceptions_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 init_rwsem(&s->lock);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001104 INIT_LIST_HEAD(&s->list);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001105 spin_lock_init(&s->pe_lock);
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001106 s->state_bits = 0;
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +00001107 s->merge_failed = 0;
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001108 s->first_merging_chunk = 0;
1109 s->num_merging_chunks = 0;
1110 bio_list_init(&s->bios_queued_during_merge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 /* Allocate hash table for COW data */
Jonathan Brassowfee19982009-04-02 19:55:34 +01001113 if (init_hash_tables(s)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 ti->error = "Unable to allocate hash table space";
1115 r = -ENOMEM;
Jonathan Brassowfee19982009-04-02 19:55:34 +01001116 goto bad_hash_tables;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
1118
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001119 r = dm_kcopyd_client_create(SNAPSHOT_PAGES, &s->kcopyd_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (r) {
1121 ti->error = "Could not create kcopyd client";
Jonathan Brassowfee19982009-04-02 19:55:34 +01001122 goto bad_kcopyd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
1124
Mikulas Patocka92e86812008-07-21 12:00:35 +01001125 s->pending_pool = mempool_create_slab_pool(MIN_IOS, pending_cache);
1126 if (!s->pending_pool) {
1127 ti->error = "Could not allocate mempool for pending exceptions";
Jonathan Brassowfee19982009-04-02 19:55:34 +01001128 goto bad_pending_pool;
Mikulas Patocka92e86812008-07-21 12:00:35 +01001129 }
1130
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001131 s->tracked_chunk_pool = mempool_create_slab_pool(MIN_IOS,
1132 tracked_chunk_cache);
1133 if (!s->tracked_chunk_pool) {
1134 ti->error = "Could not allocate tracked_chunk mempool for "
1135 "tracking reads";
Mikulas Patocka92e86812008-07-21 12:00:35 +01001136 goto bad_tracked_chunk_pool;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001137 }
1138
1139 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
1140 INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]);
1141
1142 spin_lock_init(&s->tracked_chunk_lock);
1143
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001144 ti->private = s;
Mike Snitzer10b81062009-12-10 23:52:31 +00001145 ti->num_flush_requests = num_flush_requests;
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001146
1147 /* Add snapshot to the list of snapshots for this origin */
1148 /* Exceptions aren't triggered till snapshot_resume() is called */
1149 r = register_snapshot(s);
1150 if (r == -ENOMEM) {
1151 ti->error = "Snapshot origin struct allocation failed";
1152 goto bad_load_and_register;
1153 } else if (r < 0) {
1154 /* invalid handover, register_snapshot has set ti->error */
1155 goto bad_load_and_register;
1156 }
1157
1158 /*
1159 * Metadata must only be loaded into one table at once, so skip this
1160 * if metadata will be handed over during resume.
1161 * Chunk size will be set during the handover - set it to zero to
1162 * ensure it's ignored.
1163 */
1164 if (r > 0) {
1165 s->store->chunk_size = 0;
1166 return 0;
1167 }
1168
Jonathan Brassow493df712009-04-02 19:55:31 +01001169 r = s->store->type->read_metadata(s->store, dm_add_exception,
1170 (void *)s);
Milan Broz07641472007-07-12 17:28:13 +01001171 if (r < 0) {
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -07001172 ti->error = "Failed to read snapshot metadata";
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001173 goto bad_read_metadata;
Milan Broz07641472007-07-12 17:28:13 +01001174 } else if (r > 0) {
1175 s->valid = 0;
1176 DMWARN("Snapshot is marked invalid.");
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -07001177 }
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001178
Mikulas Patocka3f2412d2009-10-16 23:18:16 +01001179 if (!s->store->chunk_size) {
1180 ti->error = "Chunk size not set";
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001181 goto bad_read_metadata;
Mikulas Patocka3f2412d2009-10-16 23:18:16 +01001182 }
Jonathan Brassowd0216842009-04-02 19:55:32 +01001183 ti->split_io = s->store->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 return 0;
1186
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001187bad_read_metadata:
1188 unregister_snapshot(s);
1189
Jonathan Brassowfee19982009-04-02 19:55:34 +01001190bad_load_and_register:
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001191 mempool_destroy(s->tracked_chunk_pool);
1192
Jonathan Brassowfee19982009-04-02 19:55:34 +01001193bad_tracked_chunk_pool:
Mikulas Patocka92e86812008-07-21 12:00:35 +01001194 mempool_destroy(s->pending_pool);
1195
Jonathan Brassowfee19982009-04-02 19:55:34 +01001196bad_pending_pool:
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001197 dm_kcopyd_client_destroy(s->kcopyd_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Jonathan Brassowfee19982009-04-02 19:55:34 +01001199bad_kcopyd:
Jon Brassow3510cb92009-12-10 23:52:11 +00001200 dm_exception_table_exit(&s->pending, pending_cache);
1201 dm_exception_table_exit(&s->complete, exception_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Jonathan Brassowfee19982009-04-02 19:55:34 +01001203bad_hash_tables:
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001204 dm_exception_store_destroy(s->store);
1205
1206bad_store:
1207 dm_put_device(ti, s->cow);
1208
1209bad_cow:
Mikulas Patockac2411042010-08-12 04:13:51 +01001210 dm_put_device(ti, s->origin);
1211
1212bad_origin:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 kfree(s);
1214
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001215bad:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 return r;
1217}
1218
Milan Broz31c93a02006-12-08 02:41:11 -08001219static void __free_exceptions(struct dm_snapshot *s)
1220{
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001221 dm_kcopyd_client_destroy(s->kcopyd_client);
Milan Broz31c93a02006-12-08 02:41:11 -08001222 s->kcopyd_client = NULL;
1223
Jon Brassow3510cb92009-12-10 23:52:11 +00001224 dm_exception_table_exit(&s->pending, pending_cache);
1225 dm_exception_table_exit(&s->complete, exception_cache);
Milan Broz31c93a02006-12-08 02:41:11 -08001226}
1227
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001228static void __handover_exceptions(struct dm_snapshot *snap_src,
1229 struct dm_snapshot *snap_dest)
1230{
1231 union {
1232 struct dm_exception_table table_swap;
1233 struct dm_exception_store *store_swap;
1234 } u;
1235
1236 /*
1237 * Swap all snapshot context information between the two instances.
1238 */
1239 u.table_swap = snap_dest->complete;
1240 snap_dest->complete = snap_src->complete;
1241 snap_src->complete = u.table_swap;
1242
1243 u.store_swap = snap_dest->store;
1244 snap_dest->store = snap_src->store;
1245 snap_src->store = u.store_swap;
1246
1247 snap_dest->store->snap = snap_dest;
1248 snap_src->store->snap = snap_src;
1249
1250 snap_dest->ti->split_io = snap_dest->store->chunk_size;
1251 snap_dest->valid = snap_src->valid;
1252
1253 /*
1254 * Set source invalid to ensure it receives no further I/O.
1255 */
1256 snap_src->valid = 0;
1257}
1258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259static void snapshot_dtr(struct dm_target *ti)
1260{
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001261#ifdef CONFIG_DM_DEBUG
1262 int i;
1263#endif
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001264 struct dm_snapshot *s = ti->private;
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001265 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001267 down_read(&_origins_lock);
1268 /* Check whether exception handover must be cancelled */
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +00001269 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001270 if (snap_src && snap_dest && (s == snap_src)) {
1271 down_write(&snap_dest->lock);
1272 snap_dest->valid = 0;
1273 up_write(&snap_dest->lock);
1274 DMERR("Cancelling snapshot handover.");
1275 }
1276 up_read(&_origins_lock);
1277
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001278 if (dm_target_is_snapshot_merge(ti))
1279 stop_merge(s);
1280
Alasdair G Kergon138728dc2006-03-27 01:17:50 -08001281 /* Prevent further origin writes from using this snapshot. */
1282 /* After this returns there can be no new kcopyd jobs. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 unregister_snapshot(s);
1284
Mikulas Patocka879129d22008-10-30 13:33:16 +00001285 while (atomic_read(&s->pending_exceptions_count))
Mikulas Patocka90fa1522009-01-06 03:04:54 +00001286 msleep(1);
Mikulas Patocka879129d22008-10-30 13:33:16 +00001287 /*
1288 * Ensure instructions in mempool_destroy aren't reordered
1289 * before atomic_read.
1290 */
1291 smp_mb();
1292
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001293#ifdef CONFIG_DM_DEBUG
1294 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
1295 BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i]));
1296#endif
1297
1298 mempool_destroy(s->tracked_chunk_pool);
1299
Milan Broz31c93a02006-12-08 02:41:11 -08001300 __free_exceptions(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Mikulas Patocka92e86812008-07-21 12:00:35 +01001302 mempool_destroy(s->pending_pool);
1303
Jonathan Brassowfee19982009-04-02 19:55:34 +01001304 dm_exception_store_destroy(s->store);
Alasdair G Kergon138728dc2006-03-27 01:17:50 -08001305
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001306 dm_put_device(ti, s->cow);
1307
Mikulas Patockac2411042010-08-12 04:13:51 +01001308 dm_put_device(ti, s->origin);
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 kfree(s);
1311}
1312
1313/*
1314 * Flush a list of buffers.
1315 */
1316static void flush_bios(struct bio *bio)
1317{
1318 struct bio *n;
1319
1320 while (bio) {
1321 n = bio->bi_next;
1322 bio->bi_next = NULL;
1323 generic_make_request(bio);
1324 bio = n;
1325 }
1326}
1327
Mikulas Patocka515ad662009-12-10 23:52:30 +00001328static int do_origin(struct dm_dev *origin, struct bio *bio);
1329
1330/*
1331 * Flush a list of buffers.
1332 */
1333static void retry_origin_bios(struct dm_snapshot *s, struct bio *bio)
1334{
1335 struct bio *n;
1336 int r;
1337
1338 while (bio) {
1339 n = bio->bi_next;
1340 bio->bi_next = NULL;
1341 r = do_origin(s->origin, bio);
1342 if (r == DM_MAPIO_REMAPPED)
1343 generic_make_request(bio);
1344 bio = n;
1345 }
1346}
1347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348/*
1349 * Error a list of buffers.
1350 */
1351static void error_bios(struct bio *bio)
1352{
1353 struct bio *n;
1354
1355 while (bio) {
1356 n = bio->bi_next;
1357 bio->bi_next = NULL;
NeilBrown6712ecf2007-09-27 12:47:43 +02001358 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 bio = n;
1360 }
1361}
1362
Alasdair G Kergon695368a2006-10-03 01:15:31 -07001363static void __invalidate_snapshot(struct dm_snapshot *s, int err)
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001364{
1365 if (!s->valid)
1366 return;
1367
1368 if (err == -EIO)
1369 DMERR("Invalidating snapshot: Error reading/writing.");
1370 else if (err == -ENOMEM)
1371 DMERR("Invalidating snapshot: Unable to allocate exception.");
1372
Jonathan Brassow493df712009-04-02 19:55:31 +01001373 if (s->store->type->drop_snapshot)
1374 s->store->type->drop_snapshot(s->store);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001375
1376 s->valid = 0;
1377
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001378 dm_table_event(s->ti->table);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001379}
1380
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001381static void pending_complete(struct dm_snap_pending_exception *pe, int success)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
Jon Brassow1d4989c2009-12-10 23:52:10 +00001383 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 struct dm_snapshot *s = pe->snap;
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001385 struct bio *origin_bios = NULL;
1386 struct bio *snapshot_bios = NULL;
1387 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001389 if (!success) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 /* Read/write error - snapshot is unusable */
1391 down_write(&s->lock);
Alasdair G Kergon695368a2006-10-03 01:15:31 -07001392 __invalidate_snapshot(s, -EIO);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001393 error = 1;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001394 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 }
1396
Jon Brassow3510cb92009-12-10 23:52:11 +00001397 e = alloc_completed_exception();
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001398 if (!e) {
1399 down_write(&s->lock);
Alasdair G Kergon695368a2006-10-03 01:15:31 -07001400 __invalidate_snapshot(s, -ENOMEM);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001401 error = 1;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001402 goto out;
1403 }
1404 *e = pe->e;
1405
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001406 down_write(&s->lock);
1407 if (!s->valid) {
Jon Brassow3510cb92009-12-10 23:52:11 +00001408 free_completed_exception(e);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001409 error = 1;
1410 goto out;
1411 }
1412
Mike Snitzer615d1eb2009-12-10 23:52:29 +00001413 /* Check for conflicting reads */
1414 __check_for_conflicting_io(s, pe->e.old_chunk);
Mikulas Patockaa8d41b52008-07-21 12:00:34 +01001415
1416 /*
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001417 * Add a proper exception, and remove the
1418 * in-flight exception from the list.
1419 */
Jon Brassow3510cb92009-12-10 23:52:11 +00001420 dm_insert_exception(&s->complete, e);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 out:
Jon Brassow3510cb92009-12-10 23:52:11 +00001423 dm_remove_exception(&pe->e);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001424 snapshot_bios = bio_list_get(&pe->snapshot_bios);
Mikulas Patocka515ad662009-12-10 23:52:30 +00001425 origin_bios = bio_list_get(&pe->origin_bios);
1426 free_pending_exception(pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Mikulas Patocka73dfd072009-12-10 23:52:34 +00001428 increment_pending_exceptions_done_count();
1429
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001430 up_write(&s->lock);
1431
1432 /* Submit any pending write bios */
1433 if (error)
1434 error_bios(snapshot_bios);
1435 else
1436 flush_bios(snapshot_bios);
1437
Mikulas Patocka515ad662009-12-10 23:52:30 +00001438 retry_origin_bios(s, origin_bios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439}
1440
1441static void commit_callback(void *context, int success)
1442{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001443 struct dm_snap_pending_exception *pe = context;
1444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 pending_complete(pe, success);
1446}
1447
1448/*
1449 * Called when the copy I/O has finished. kcopyd actually runs
1450 * this code so don't block.
1451 */
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -07001452static void copy_callback(int read_err, unsigned long write_err, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001454 struct dm_snap_pending_exception *pe = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 struct dm_snapshot *s = pe->snap;
1456
1457 if (read_err || write_err)
1458 pending_complete(pe, 0);
1459
1460 else
1461 /* Update the metadata if we are persistent */
Jonathan Brassow493df712009-04-02 19:55:31 +01001462 s->store->type->commit_exception(s->store, &pe->e,
1463 commit_callback, pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
1466/*
1467 * Dispatches the copy operation to kcopyd.
1468 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001469static void start_copy(struct dm_snap_pending_exception *pe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
1471 struct dm_snapshot *s = pe->snap;
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +01001472 struct dm_io_region src, dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 struct block_device *bdev = s->origin->bdev;
1474 sector_t dev_size;
1475
1476 dev_size = get_dev_size(bdev);
1477
1478 src.bdev = bdev;
Jonathan Brassow71fab002009-04-02 19:55:33 +01001479 src.sector = chunk_to_sector(s->store, pe->e.old_chunk);
Mikulas Patockadf96eee2009-10-16 23:18:17 +01001480 src.count = min((sector_t)s->store->chunk_size, dev_size - src.sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001482 dest.bdev = s->cow->bdev;
Jonathan Brassow71fab002009-04-02 19:55:33 +01001483 dest.sector = chunk_to_sector(s->store, pe->e.new_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 dest.count = src.count;
1485
1486 /* Hand over to kcopyd */
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001487 dm_kcopyd_copy(s->kcopyd_client,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 &src, 1, &dest, 0, copy_callback, pe);
1489}
1490
Mikulas Patocka29138082009-04-02 19:55:25 +01001491static struct dm_snap_pending_exception *
1492__lookup_pending_exception(struct dm_snapshot *s, chunk_t chunk)
1493{
Jon Brassow3510cb92009-12-10 23:52:11 +00001494 struct dm_exception *e = dm_lookup_exception(&s->pending, chunk);
Mikulas Patocka29138082009-04-02 19:55:25 +01001495
1496 if (!e)
1497 return NULL;
1498
1499 return container_of(e, struct dm_snap_pending_exception, e);
1500}
1501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502/*
1503 * Looks to see if this snapshot already has a pending exception
1504 * for this chunk, otherwise it allocates a new one and inserts
1505 * it into the pending table.
1506 *
1507 * NOTE: a write lock must be held on snap->lock before calling
1508 * this.
1509 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001510static struct dm_snap_pending_exception *
Mikulas Patockac6621392009-04-02 19:55:25 +01001511__find_pending_exception(struct dm_snapshot *s,
1512 struct dm_snap_pending_exception *pe, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
Mikulas Patockac6621392009-04-02 19:55:25 +01001514 struct dm_snap_pending_exception *pe2;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001515
Mikulas Patocka29138082009-04-02 19:55:25 +01001516 pe2 = __lookup_pending_exception(s, chunk);
1517 if (pe2) {
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001518 free_pending_exception(pe);
Mikulas Patocka29138082009-04-02 19:55:25 +01001519 return pe2;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001520 }
1521
1522 pe->e.old_chunk = chunk;
1523 bio_list_init(&pe->origin_bios);
1524 bio_list_init(&pe->snapshot_bios);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001525 pe->started = 0;
1526
Jonathan Brassow493df712009-04-02 19:55:31 +01001527 if (s->store->type->prepare_exception(s->store, &pe->e)) {
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001528 free_pending_exception(pe);
1529 return NULL;
1530 }
1531
Jon Brassow3510cb92009-12-10 23:52:11 +00001532 dm_insert_exception(&s->pending, &pe->e);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 return pe;
1535}
1536
Jon Brassow1d4989c2009-12-10 23:52:10 +00001537static void remap_exception(struct dm_snapshot *s, struct dm_exception *e,
Milan Brozd74f81f2008-02-08 02:11:27 +00001538 struct bio *bio, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001540 bio->bi_bdev = s->cow->bdev;
Jonathan Brassow71fab002009-04-02 19:55:33 +01001541 bio->bi_sector = chunk_to_sector(s->store,
1542 dm_chunk_number(e->new_chunk) +
1543 (chunk - e->old_chunk)) +
1544 (bio->bi_sector &
1545 s->store->chunk_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546}
1547
1548static int snapshot_map(struct dm_target *ti, struct bio *bio,
1549 union map_info *map_context)
1550{
Jon Brassow1d4989c2009-12-10 23:52:10 +00001551 struct dm_exception *e;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001552 struct dm_snapshot *s = ti->private;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001553 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 chunk_t chunk;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001555 struct dm_snap_pending_exception *pe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Tejun Heod87f4c12010-09-03 11:56:19 +02001557 if (bio->bi_rw & REQ_FLUSH) {
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001558 bio->bi_bdev = s->cow->bdev;
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01001559 return DM_MAPIO_REMAPPED;
1560 }
1561
Jonathan Brassow71fab002009-04-02 19:55:33 +01001562 chunk = sector_to_chunk(s->store, bio->bi_sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
1564 /* Full snapshots are not usable */
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001565 /* To get here the table must be live so s->active is always set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 if (!s->valid)
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -07001567 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001569 /* FIXME: should only take write lock if we need
1570 * to copy an exception */
1571 down_write(&s->lock);
1572
1573 if (!s->valid) {
1574 r = -EIO;
1575 goto out_unlock;
1576 }
1577
1578 /* If the block is already remapped - use that, else remap it */
Jon Brassow3510cb92009-12-10 23:52:11 +00001579 e = dm_lookup_exception(&s->complete, chunk);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001580 if (e) {
Milan Brozd74f81f2008-02-08 02:11:27 +00001581 remap_exception(s, e, bio, chunk);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001582 goto out_unlock;
1583 }
1584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 /*
1586 * Write to snapshot - higher level takes care of RW/RO
1587 * flags so we should only get this if we are
1588 * writeable.
1589 */
1590 if (bio_rw(bio) == WRITE) {
Mikulas Patocka29138082009-04-02 19:55:25 +01001591 pe = __lookup_pending_exception(s, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001592 if (!pe) {
Mikulas Patockac6621392009-04-02 19:55:25 +01001593 up_write(&s->lock);
1594 pe = alloc_pending_exception(s);
1595 down_write(&s->lock);
1596
1597 if (!s->valid) {
1598 free_pending_exception(pe);
1599 r = -EIO;
1600 goto out_unlock;
1601 }
1602
Jon Brassow3510cb92009-12-10 23:52:11 +00001603 e = dm_lookup_exception(&s->complete, chunk);
Mikulas Patocka35bf6592009-04-02 19:55:26 +01001604 if (e) {
1605 free_pending_exception(pe);
1606 remap_exception(s, e, bio, chunk);
1607 goto out_unlock;
1608 }
1609
Mikulas Patockac6621392009-04-02 19:55:25 +01001610 pe = __find_pending_exception(s, pe, chunk);
Mikulas Patocka29138082009-04-02 19:55:25 +01001611 if (!pe) {
1612 __invalidate_snapshot(s, -ENOMEM);
1613 r = -EIO;
1614 goto out_unlock;
1615 }
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001616 }
1617
Milan Brozd74f81f2008-02-08 02:11:27 +00001618 remap_exception(s, &pe->e, bio, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001619 bio_list_add(&pe->snapshot_bios, bio);
1620
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001621 r = DM_MAPIO_SUBMITTED;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001622
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001623 if (!pe->started) {
1624 /* this is protected by snap->lock */
1625 pe->started = 1;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001626 up_write(&s->lock);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001627 start_copy(pe);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001628 goto out;
1629 }
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001630 } else {
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001631 bio->bi_bdev = s->origin->bdev;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001632 map_context->ptr = track_chunk(s, chunk);
1633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001635 out_unlock:
1636 up_write(&s->lock);
1637 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 return r;
1639}
1640
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001641/*
1642 * A snapshot-merge target behaves like a combination of a snapshot
1643 * target and a snapshot-origin target. It only generates new
1644 * exceptions in other snapshots and not in the one that is being
1645 * merged.
1646 *
1647 * For each chunk, if there is an existing exception, it is used to
1648 * redirect I/O to the cow device. Otherwise I/O is sent to the origin,
1649 * which in turn might generate exceptions in other snapshots.
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001650 * If merging is currently taking place on the chunk in question, the
1651 * I/O is deferred by adding it to s->bios_queued_during_merge.
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001652 */
1653static int snapshot_merge_map(struct dm_target *ti, struct bio *bio,
1654 union map_info *map_context)
1655{
1656 struct dm_exception *e;
1657 struct dm_snapshot *s = ti->private;
1658 int r = DM_MAPIO_REMAPPED;
1659 chunk_t chunk;
1660
Tejun Heod87f4c12010-09-03 11:56:19 +02001661 if (bio->bi_rw & REQ_FLUSH) {
Mike Snitzer57cba5d2010-08-12 04:14:04 +01001662 if (!map_context->target_request_nr)
Mike Snitzer10b81062009-12-10 23:52:31 +00001663 bio->bi_bdev = s->origin->bdev;
1664 else
1665 bio->bi_bdev = s->cow->bdev;
1666 map_context->ptr = NULL;
1667 return DM_MAPIO_REMAPPED;
1668 }
1669
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001670 chunk = sector_to_chunk(s->store, bio->bi_sector);
1671
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001672 down_write(&s->lock);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001673
Mikulas Patockad2fdb772009-12-10 23:52:36 +00001674 /* Full merging snapshots are redirected to the origin */
1675 if (!s->valid)
1676 goto redirect_to_origin;
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001677
1678 /* If the block is already remapped - use that */
1679 e = dm_lookup_exception(&s->complete, chunk);
1680 if (e) {
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001681 /* Queue writes overlapping with chunks being merged */
1682 if (bio_rw(bio) == WRITE &&
1683 chunk >= s->first_merging_chunk &&
1684 chunk < (s->first_merging_chunk +
1685 s->num_merging_chunks)) {
1686 bio->bi_bdev = s->origin->bdev;
1687 bio_list_add(&s->bios_queued_during_merge, bio);
1688 r = DM_MAPIO_SUBMITTED;
1689 goto out_unlock;
1690 }
Mikulas Patocka17aa0332009-12-10 23:52:33 +00001691
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001692 remap_exception(s, e, bio, chunk);
Mikulas Patocka17aa0332009-12-10 23:52:33 +00001693
1694 if (bio_rw(bio) == WRITE)
1695 map_context->ptr = track_chunk(s, chunk);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001696 goto out_unlock;
1697 }
1698
Mikulas Patockad2fdb772009-12-10 23:52:36 +00001699redirect_to_origin:
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001700 bio->bi_bdev = s->origin->bdev;
1701
1702 if (bio_rw(bio) == WRITE) {
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001703 up_write(&s->lock);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001704 return do_origin(s->origin, bio);
1705 }
1706
1707out_unlock:
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001708 up_write(&s->lock);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001709
1710 return r;
1711}
1712
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001713static int snapshot_end_io(struct dm_target *ti, struct bio *bio,
1714 int error, union map_info *map_context)
1715{
1716 struct dm_snapshot *s = ti->private;
1717 struct dm_snap_tracked_chunk *c = map_context->ptr;
1718
1719 if (c)
1720 stop_tracking_chunk(s, c);
1721
1722 return 0;
1723}
1724
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001725static void snapshot_merge_presuspend(struct dm_target *ti)
1726{
1727 struct dm_snapshot *s = ti->private;
1728
1729 stop_merge(s);
1730}
1731
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001732static int snapshot_preresume(struct dm_target *ti)
1733{
1734 int r = 0;
1735 struct dm_snapshot *s = ti->private;
1736 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
1737
1738 down_read(&_origins_lock);
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +00001739 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001740 if (snap_src && snap_dest) {
1741 down_read(&snap_src->lock);
1742 if (s == snap_src) {
1743 DMERR("Unable to resume snapshot source until "
1744 "handover completes.");
1745 r = -EINVAL;
Mike Snitzerb83b2f22011-01-13 19:59:59 +00001746 } else if (!dm_suspended(snap_src->ti)) {
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001747 DMERR("Unable to perform snapshot handover until "
1748 "source is suspended.");
1749 r = -EINVAL;
1750 }
1751 up_read(&snap_src->lock);
1752 }
1753 up_read(&_origins_lock);
1754
1755 return r;
1756}
1757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758static void snapshot_resume(struct dm_target *ti)
1759{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001760 struct dm_snapshot *s = ti->private;
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001761 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
1762
1763 down_read(&_origins_lock);
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +00001764 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001765 if (snap_src && snap_dest) {
1766 down_write(&snap_src->lock);
1767 down_write_nested(&snap_dest->lock, SINGLE_DEPTH_NESTING);
1768 __handover_exceptions(snap_src, snap_dest);
1769 up_write(&snap_dest->lock);
1770 up_write(&snap_src->lock);
1771 }
1772 up_read(&_origins_lock);
1773
1774 /* Now we have correct chunk size, reregister */
1775 reregister_snapshot(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001777 down_write(&s->lock);
1778 s->active = 1;
1779 up_write(&s->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780}
1781
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001782static sector_t get_origin_minimum_chunksize(struct block_device *bdev)
1783{
1784 sector_t min_chunksize;
1785
1786 down_read(&_origins_lock);
1787 min_chunksize = __minimum_chunk_size(__lookup_origin(bdev));
1788 up_read(&_origins_lock);
1789
1790 return min_chunksize;
1791}
1792
1793static void snapshot_merge_resume(struct dm_target *ti)
1794{
1795 struct dm_snapshot *s = ti->private;
1796
1797 /*
1798 * Handover exceptions from existing snapshot.
1799 */
1800 snapshot_resume(ti);
1801
1802 /*
1803 * snapshot-merge acts as an origin, so set ti->split_io
1804 */
1805 ti->split_io = get_origin_minimum_chunksize(s->origin->bdev);
1806
1807 start_merge(s);
1808}
1809
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810static int snapshot_status(struct dm_target *ti, status_type_t type,
1811 char *result, unsigned int maxlen)
1812{
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01001813 unsigned sz = 0;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001814 struct dm_snapshot *snap = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
1816 switch (type) {
1817 case STATUSTYPE_INFO:
Mikulas Patocka94e765722009-12-10 23:51:53 +00001818
1819 down_write(&snap->lock);
1820
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 if (!snap->valid)
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01001822 DMEMIT("Invalid");
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +00001823 else if (snap->merge_failed)
1824 DMEMIT("Merge failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 else {
Mike Snitzer985903b2009-12-10 23:52:11 +00001826 if (snap->store->type->usage) {
1827 sector_t total_sectors, sectors_allocated,
1828 metadata_sectors;
1829 snap->store->type->usage(snap->store,
1830 &total_sectors,
1831 &sectors_allocated,
1832 &metadata_sectors);
1833 DMEMIT("%llu/%llu %llu",
1834 (unsigned long long)sectors_allocated,
1835 (unsigned long long)total_sectors,
1836 (unsigned long long)metadata_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 }
1838 else
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01001839 DMEMIT("Unknown");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 }
Mikulas Patocka94e765722009-12-10 23:51:53 +00001841
1842 up_write(&snap->lock);
1843
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 break;
1845
1846 case STATUSTYPE_TABLE:
1847 /*
1848 * kdevname returns a static pointer so we need
1849 * to make private copies if the output is to
1850 * make sense.
1851 */
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001852 DMEMIT("%s %s", snap->origin->name, snap->cow->name);
Jonathan Brassow1e302a92009-04-02 19:55:35 +01001853 snap->store->type->status(snap->store, type, result + sz,
1854 maxlen - sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 break;
1856 }
1857
1858 return 0;
1859}
1860
Mike Snitzer8811f462009-09-04 20:40:19 +01001861static int snapshot_iterate_devices(struct dm_target *ti,
1862 iterate_devices_callout_fn fn, void *data)
1863{
1864 struct dm_snapshot *snap = ti->private;
Mikulas Patocka1e5554c2010-08-12 04:13:50 +01001865 int r;
Mike Snitzer8811f462009-09-04 20:40:19 +01001866
Mikulas Patocka1e5554c2010-08-12 04:13:50 +01001867 r = fn(ti, snap->origin, 0, ti->len, data);
1868
1869 if (!r)
1870 r = fn(ti, snap->cow, 0, get_dev_size(snap->cow->bdev), data);
1871
1872 return r;
Mike Snitzer8811f462009-09-04 20:40:19 +01001873}
1874
1875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876/*-----------------------------------------------------------------
1877 * Origin methods
1878 *---------------------------------------------------------------*/
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00001879
1880/*
1881 * If no exceptions need creating, DM_MAPIO_REMAPPED is returned and any
1882 * supplied bio was ignored. The caller may submit it immediately.
1883 * (No remapping actually occurs as the origin is always a direct linear
1884 * map.)
1885 *
1886 * If further exceptions are required, DM_MAPIO_SUBMITTED is returned
1887 * and any supplied bio is added to a list to be submitted once all
1888 * the necessary exceptions exist.
1889 */
1890static int __origin_write(struct list_head *snapshots, sector_t sector,
1891 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
Mikulas Patocka515ad662009-12-10 23:52:30 +00001893 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 struct dm_snapshot *snap;
Jon Brassow1d4989c2009-12-10 23:52:10 +00001895 struct dm_exception *e;
Mikulas Patocka515ad662009-12-10 23:52:30 +00001896 struct dm_snap_pending_exception *pe;
1897 struct dm_snap_pending_exception *pe_to_start_now = NULL;
1898 struct dm_snap_pending_exception *pe_to_start_last = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 chunk_t chunk;
1900
1901 /* Do all the snapshots on this origin */
1902 list_for_each_entry (snap, snapshots, list) {
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001903 /*
1904 * Don't make new exceptions in a merging snapshot
1905 * because it has effectively been deleted
1906 */
1907 if (dm_target_is_snapshot_merge(snap->ti))
1908 continue;
1909
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001910 down_write(&snap->lock);
1911
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001912 /* Only deal with valid and active snapshots */
1913 if (!snap->valid || !snap->active)
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001914 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Alasdair G Kergond5e404c2005-07-12 15:53:05 -07001916 /* Nothing to do if writing beyond end of snapshot */
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00001917 if (sector >= dm_table_get_size(snap->ti->table))
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001918 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
1920 /*
1921 * Remember, different snapshots can have
1922 * different chunk sizes.
1923 */
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00001924 chunk = sector_to_chunk(snap->store, sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
1926 /*
1927 * Check exception table to see if block
1928 * is already remapped in this snapshot
1929 * and trigger an exception if not.
1930 */
Jon Brassow3510cb92009-12-10 23:52:11 +00001931 e = dm_lookup_exception(&snap->complete, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001932 if (e)
1933 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Mikulas Patocka29138082009-04-02 19:55:25 +01001935 pe = __lookup_pending_exception(snap, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001936 if (!pe) {
Mikulas Patockac6621392009-04-02 19:55:25 +01001937 up_write(&snap->lock);
1938 pe = alloc_pending_exception(snap);
1939 down_write(&snap->lock);
1940
1941 if (!snap->valid) {
1942 free_pending_exception(pe);
1943 goto next_snapshot;
1944 }
1945
Jon Brassow3510cb92009-12-10 23:52:11 +00001946 e = dm_lookup_exception(&snap->complete, chunk);
Mikulas Patocka35bf6592009-04-02 19:55:26 +01001947 if (e) {
1948 free_pending_exception(pe);
1949 goto next_snapshot;
1950 }
1951
Mikulas Patockac6621392009-04-02 19:55:25 +01001952 pe = __find_pending_exception(snap, pe, chunk);
Mikulas Patocka29138082009-04-02 19:55:25 +01001953 if (!pe) {
1954 __invalidate_snapshot(snap, -ENOMEM);
1955 goto next_snapshot;
1956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 }
1958
Mikulas Patocka515ad662009-12-10 23:52:30 +00001959 r = DM_MAPIO_SUBMITTED;
1960
1961 /*
1962 * If an origin bio was supplied, queue it to wait for the
1963 * completion of this exception, and start this one last,
1964 * at the end of the function.
1965 */
1966 if (bio) {
1967 bio_list_add(&pe->origin_bios, bio);
1968 bio = NULL;
1969
1970 if (!pe->started) {
1971 pe->started = 1;
1972 pe_to_start_last = pe;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001973 }
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001974 }
1975
1976 if (!pe->started) {
1977 pe->started = 1;
Mikulas Patocka515ad662009-12-10 23:52:30 +00001978 pe_to_start_now = pe;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001979 }
1980
1981 next_snapshot:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 up_write(&snap->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
Mikulas Patocka515ad662009-12-10 23:52:30 +00001984 if (pe_to_start_now) {
1985 start_copy(pe_to_start_now);
1986 pe_to_start_now = NULL;
1987 }
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08001988 }
1989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 /*
Mikulas Patocka515ad662009-12-10 23:52:30 +00001991 * Submit the exception against which the bio is queued last,
1992 * to give the other exceptions a head start.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 */
Mikulas Patocka515ad662009-12-10 23:52:30 +00001994 if (pe_to_start_last)
1995 start_copy(pe_to_start_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
1997 return r;
1998}
1999
2000/*
2001 * Called on a write from the origin driver.
2002 */
2003static int do_origin(struct dm_dev *origin, struct bio *bio)
2004{
2005 struct origin *o;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08002006 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
2008 down_read(&_origins_lock);
2009 o = __lookup_origin(origin->bdev);
2010 if (o)
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00002011 r = __origin_write(&o->snapshots, bio->bi_sector, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 up_read(&_origins_lock);
2013
2014 return r;
2015}
2016
2017/*
Mikulas Patocka73dfd072009-12-10 23:52:34 +00002018 * Trigger exceptions in all non-merging snapshots.
2019 *
2020 * The chunk size of the merging snapshot may be larger than the chunk
2021 * size of some other snapshot so we may need to reallocate multiple
2022 * chunks in other snapshots.
2023 *
2024 * We scan all the overlapping exceptions in the other snapshots.
2025 * Returns 1 if anything was reallocated and must be waited for,
2026 * otherwise returns 0.
2027 *
2028 * size must be a multiple of merging_snap's chunk_size.
2029 */
2030static int origin_write_extent(struct dm_snapshot *merging_snap,
2031 sector_t sector, unsigned size)
2032{
2033 int must_wait = 0;
2034 sector_t n;
2035 struct origin *o;
2036
2037 /*
2038 * The origin's __minimum_chunk_size() got stored in split_io
2039 * by snapshot_merge_resume().
2040 */
2041 down_read(&_origins_lock);
2042 o = __lookup_origin(merging_snap->origin->bdev);
2043 for (n = 0; n < size; n += merging_snap->ti->split_io)
2044 if (__origin_write(&o->snapshots, sector + n, NULL) ==
2045 DM_MAPIO_SUBMITTED)
2046 must_wait = 1;
2047 up_read(&_origins_lock);
2048
2049 return must_wait;
2050}
2051
2052/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 * Origin: maps a linear range of a device, with hooks for snapshotting.
2054 */
2055
2056/*
2057 * Construct an origin mapping: <dev_path>
2058 * The context for an origin is merely a 'struct dm_dev *'
2059 * pointing to the real device.
2060 */
2061static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
2062{
2063 int r;
2064 struct dm_dev *dev;
2065
2066 if (argc != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002067 ti->error = "origin: incorrect number of arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 return -EINVAL;
2069 }
2070
Nikanth Karthikesan8215d6e2010-03-06 02:32:27 +00002071 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 if (r) {
2073 ti->error = "Cannot get target device";
2074 return r;
2075 }
2076
2077 ti->private = dev;
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01002078 ti->num_flush_requests = 1;
2079
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 return 0;
2081}
2082
2083static void origin_dtr(struct dm_target *ti)
2084{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002085 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 dm_put_device(ti, dev);
2087}
2088
2089static int origin_map(struct dm_target *ti, struct bio *bio,
2090 union map_info *map_context)
2091{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002092 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 bio->bi_bdev = dev->bdev;
2094
Tejun Heod87f4c12010-09-03 11:56:19 +02002095 if (bio->bi_rw & REQ_FLUSH)
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01002096 return DM_MAPIO_REMAPPED;
2097
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 /* Only tell snapshots if this is a write */
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08002099 return (bio_rw(bio) == WRITE) ? do_origin(dev, bio) : DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100}
2101
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102/*
2103 * Set the target "split_io" field to the minimum of all the snapshots'
2104 * chunk sizes.
2105 */
2106static void origin_resume(struct dm_target *ti)
2107{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002108 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002110 ti->split_io = get_origin_minimum_chunksize(dev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111}
2112
2113static int origin_status(struct dm_target *ti, status_type_t type, char *result,
2114 unsigned int maxlen)
2115{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002116 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
2118 switch (type) {
2119 case STATUSTYPE_INFO:
2120 result[0] = '\0';
2121 break;
2122
2123 case STATUSTYPE_TABLE:
2124 snprintf(result, maxlen, "%s", dev->name);
2125 break;
2126 }
2127
2128 return 0;
2129}
2130
Mikulas Patockab1d55522010-08-12 04:14:02 +01002131static int origin_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
2132 struct bio_vec *biovec, int max_size)
2133{
2134 struct dm_dev *dev = ti->private;
2135 struct request_queue *q = bdev_get_queue(dev->bdev);
2136
2137 if (!q->merge_bvec_fn)
2138 return max_size;
2139
2140 bvm->bi_bdev = dev->bdev;
2141 bvm->bi_sector = bvm->bi_sector;
2142
2143 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
2144}
2145
Mike Snitzer8811f462009-09-04 20:40:19 +01002146static int origin_iterate_devices(struct dm_target *ti,
2147 iterate_devices_callout_fn fn, void *data)
2148{
2149 struct dm_dev *dev = ti->private;
2150
2151 return fn(ti, dev, 0, ti->len, data);
2152}
2153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154static struct target_type origin_target = {
2155 .name = "snapshot-origin",
Mike Snitzerb83b2f22011-01-13 19:59:59 +00002156 .version = {1, 7, 1},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 .module = THIS_MODULE,
2158 .ctr = origin_ctr,
2159 .dtr = origin_dtr,
2160 .map = origin_map,
2161 .resume = origin_resume,
2162 .status = origin_status,
Mikulas Patockab1d55522010-08-12 04:14:02 +01002163 .merge = origin_merge,
Mike Snitzer8811f462009-09-04 20:40:19 +01002164 .iterate_devices = origin_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165};
2166
2167static struct target_type snapshot_target = {
2168 .name = "snapshot",
Mike Snitzerb83b2f22011-01-13 19:59:59 +00002169 .version = {1, 10, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 .module = THIS_MODULE,
2171 .ctr = snapshot_ctr,
2172 .dtr = snapshot_dtr,
2173 .map = snapshot_map,
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002174 .end_io = snapshot_end_io,
Mike Snitzerc1f0c182009-12-10 23:52:24 +00002175 .preresume = snapshot_preresume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 .resume = snapshot_resume,
2177 .status = snapshot_status,
Mike Snitzer8811f462009-09-04 20:40:19 +01002178 .iterate_devices = snapshot_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179};
2180
Mikulas Patockad698aa42009-12-10 23:52:30 +00002181static struct target_type merge_target = {
2182 .name = dm_snapshot_merge_target_name,
Mike Snitzerb83b2f22011-01-13 19:59:59 +00002183 .version = {1, 1, 0},
Mikulas Patockad698aa42009-12-10 23:52:30 +00002184 .module = THIS_MODULE,
2185 .ctr = snapshot_ctr,
2186 .dtr = snapshot_dtr,
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00002187 .map = snapshot_merge_map,
Mikulas Patockad698aa42009-12-10 23:52:30 +00002188 .end_io = snapshot_end_io,
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002189 .presuspend = snapshot_merge_presuspend,
Mikulas Patockad698aa42009-12-10 23:52:30 +00002190 .preresume = snapshot_preresume,
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002191 .resume = snapshot_merge_resume,
Mikulas Patockad698aa42009-12-10 23:52:30 +00002192 .status = snapshot_status,
2193 .iterate_devices = snapshot_iterate_devices,
2194};
2195
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196static int __init dm_snapshot_init(void)
2197{
2198 int r;
2199
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00002200 r = dm_exception_store_init();
2201 if (r) {
2202 DMERR("Failed to initialize exception stores");
2203 return r;
2204 }
2205
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 r = dm_register_target(&snapshot_target);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002207 if (r < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 DMERR("snapshot target register failed %d", r);
Jonathan Brassow034a1862009-10-16 23:18:14 +01002209 goto bad_register_snapshot_target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 }
2211
2212 r = dm_register_target(&origin_target);
2213 if (r < 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002214 DMERR("Origin target register failed %d", r);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002215 goto bad_register_origin_target;
2216 }
2217
2218 r = dm_register_target(&merge_target);
2219 if (r < 0) {
2220 DMERR("Merge target register failed %d", r);
2221 goto bad_register_merge_target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 }
2223
2224 r = init_origin_hash();
2225 if (r) {
2226 DMERR("init_origin_hash failed.");
Mikulas Patockad698aa42009-12-10 23:52:30 +00002227 goto bad_origin_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 }
2229
Jon Brassow1d4989c2009-12-10 23:52:10 +00002230 exception_cache = KMEM_CACHE(dm_exception, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 if (!exception_cache) {
2232 DMERR("Couldn't create exception cache.");
2233 r = -ENOMEM;
Mikulas Patockad698aa42009-12-10 23:52:30 +00002234 goto bad_exception_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 }
2236
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002237 pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 if (!pending_cache) {
2239 DMERR("Couldn't create pending cache.");
2240 r = -ENOMEM;
Mikulas Patockad698aa42009-12-10 23:52:30 +00002241 goto bad_pending_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 }
2243
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002244 tracked_chunk_cache = KMEM_CACHE(dm_snap_tracked_chunk, 0);
2245 if (!tracked_chunk_cache) {
2246 DMERR("Couldn't create cache to track chunks in use.");
2247 r = -ENOMEM;
Mikulas Patockad698aa42009-12-10 23:52:30 +00002248 goto bad_tracked_chunk_cache;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002249 }
2250
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 return 0;
2252
Mikulas Patockad698aa42009-12-10 23:52:30 +00002253bad_tracked_chunk_cache:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 kmem_cache_destroy(pending_cache);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002255bad_pending_cache:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 kmem_cache_destroy(exception_cache);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002257bad_exception_cache:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 exit_origin_hash();
Mikulas Patockad698aa42009-12-10 23:52:30 +00002259bad_origin_hash:
2260 dm_unregister_target(&merge_target);
2261bad_register_merge_target:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 dm_unregister_target(&origin_target);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002263bad_register_origin_target:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 dm_unregister_target(&snapshot_target);
Jonathan Brassow034a1862009-10-16 23:18:14 +01002265bad_register_snapshot_target:
2266 dm_exception_store_exit();
Mikulas Patockad698aa42009-12-10 23:52:30 +00002267
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 return r;
2269}
2270
2271static void __exit dm_snapshot_exit(void)
2272{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00002273 dm_unregister_target(&snapshot_target);
2274 dm_unregister_target(&origin_target);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002275 dm_unregister_target(&merge_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
2277 exit_origin_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 kmem_cache_destroy(pending_cache);
2279 kmem_cache_destroy(exception_cache);
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002280 kmem_cache_destroy(tracked_chunk_cache);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00002281
2282 dm_exception_store_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283}
2284
2285/* Module hooks */
2286module_init(dm_snapshot_init);
2287module_exit(dm_snapshot_exit);
2288
2289MODULE_DESCRIPTION(DM_NAME " snapshot target");
2290MODULE_AUTHOR("Joe Thornber");
2291MODULE_LICENSE("GPL");