blob: 10079e07edf48e8cafe8183d01e5808b09304f3b [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/*
Mikulas Patockacd45daf2008-07-21 12:00:32 +010033 * The size of the mempool used to track chunks in use.
34 */
35#define MIN_IOS 256
36
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010037#define DM_TRACKED_CHUNK_HASH_SIZE 16
38#define DM_TRACKED_CHUNK_HASH(x) ((unsigned long)(x) & \
39 (DM_TRACKED_CHUNK_HASH_SIZE - 1))
40
Jon Brassow191437a2009-12-10 23:52:10 +000041struct dm_exception_table {
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010042 uint32_t hash_mask;
43 unsigned hash_shift;
44 struct list_head *table;
45};
46
47struct dm_snapshot {
48 struct rw_semaphore lock;
49
50 struct dm_dev *origin;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +000051 struct dm_dev *cow;
52
53 struct dm_target *ti;
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010054
55 /* List of snapshots per Origin */
56 struct list_head list;
57
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +000058 /*
59 * You can't use a snapshot if this is 0 (e.g. if full).
60 * A snapshot-merge target never clears this.
61 */
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010062 int valid;
63
64 /* Origin writes don't trigger exceptions until this is set */
65 int active;
66
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010067 atomic_t pending_exceptions_count;
68
Mike Snitzer924e6002010-03-06 02:32:33 +000069 mempool_t *pending_pool;
70
Jon Brassow191437a2009-12-10 23:52:10 +000071 struct dm_exception_table pending;
72 struct dm_exception_table complete;
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010073
74 /*
75 * pe_lock protects all pending_exception operations and access
76 * as well as the snapshot_bios list.
77 */
78 spinlock_t pe_lock;
79
Mike Snitzer924e6002010-03-06 02:32:33 +000080 /* Chunks with outstanding reads */
81 spinlock_t tracked_chunk_lock;
Mike Snitzer924e6002010-03-06 02:32:33 +000082 struct hlist_head tracked_chunk_hash[DM_TRACKED_CHUNK_HASH_SIZE];
83
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010084 /* The on disk metadata handler */
85 struct dm_exception_store *store;
86
87 struct dm_kcopyd_client *kcopyd_client;
88
Mike Snitzer924e6002010-03-06 02:32:33 +000089 /* Wait for events based on state_bits */
90 unsigned long state_bits;
91
92 /* Range of chunks currently being merged. */
93 chunk_t first_merging_chunk;
94 int num_merging_chunks;
Mikulas Patocka1e03f972009-12-10 23:52:32 +000095
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +000096 /*
97 * The merge operation failed if this flag is set.
98 * Failure modes are handled as follows:
99 * - I/O error reading the header
100 * => don't load the target; abort.
101 * - Header does not have "valid" flag set
102 * => use the origin; forget about the snapshot.
103 * - I/O error when reading exceptions
104 * => don't load the target; abort.
105 * (We can't use the intermediate origin state.)
106 * - I/O error while merging
107 * => stop merging; set merge_failed; process I/O normally.
108 */
109 int merge_failed;
110
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000111 /*
112 * Incoming bios that overlap with chunks being merged must wait
113 * for them to be committed.
114 */
115 struct bio_list bios_queued_during_merge;
Jonathan Brassowccc45ea2009-04-02 19:55:34 +0100116};
117
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000118/*
119 * state_bits:
120 * RUNNING_MERGE - Merge operation is in progress.
121 * SHUTDOWN_MERGE - Set to signal that merge needs to be stopped;
122 * cleared afterwards.
123 */
124#define RUNNING_MERGE 0
125#define SHUTDOWN_MERGE 1
126
Mikulas Patockac2411042010-08-12 04:13:51 +0100127struct dm_dev *dm_snap_origin(struct dm_snapshot *s)
128{
129 return s->origin;
130}
131EXPORT_SYMBOL(dm_snap_origin);
132
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000133struct dm_dev *dm_snap_cow(struct dm_snapshot *s)
134{
135 return s->cow;
136}
137EXPORT_SYMBOL(dm_snap_cow);
138
Jonathan Brassowccc45ea2009-04-02 19:55:34 +0100139static sector_t chunk_to_sector(struct dm_exception_store *store,
140 chunk_t chunk)
141{
142 return chunk << store->chunk_shift;
143}
144
145static int bdev_equal(struct block_device *lhs, struct block_device *rhs)
146{
147 /*
148 * There is only ever one instance of a particular block
149 * device so we can compare pointers safely.
150 */
151 return lhs == rhs;
152}
153
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100154struct dm_snap_pending_exception {
Jon Brassow1d4989c2009-12-10 23:52:10 +0000155 struct dm_exception e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 /*
158 * Origin buffers waiting for this to complete are held
159 * in a bio list
160 */
161 struct bio_list origin_bios;
162 struct bio_list snapshot_bios;
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 /* Pointer back to snapshot context */
165 struct dm_snapshot *snap;
166
167 /*
168 * 1 indicates the exception has already been sent to
169 * kcopyd.
170 */
171 int started;
Mikulas Patockaa6e50b42011-08-02 12:32:04 +0100172
173 /*
174 * For writing a complete chunk, bypassing the copy.
175 */
176 struct bio *full_bio;
177 bio_end_io_t *full_bio_end_io;
178 void *full_bio_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179};
180
181/*
182 * Hash table mapping origin volumes to lists of snapshots and
183 * a lock to protect it
184 */
Christoph Lametere18b8902006-12-06 20:33:20 -0800185static struct kmem_cache *exception_cache;
186static struct kmem_cache *pending_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100188struct dm_snap_tracked_chunk {
189 struct hlist_node node;
190 chunk_t chunk;
191};
192
Mikulas Patockaee180262012-12-21 20:23:41 +0000193static void init_tracked_chunk(struct bio *bio)
194{
195 struct dm_snap_tracked_chunk *c = dm_per_bio_data(bio, sizeof(struct dm_snap_tracked_chunk));
196 INIT_HLIST_NODE(&c->node);
197}
198
199static bool is_bio_tracked(struct bio *bio)
200{
201 struct dm_snap_tracked_chunk *c = dm_per_bio_data(bio, sizeof(struct dm_snap_tracked_chunk));
202 return !hlist_unhashed(&c->node);
203}
204
205static void track_chunk(struct dm_snapshot *s, struct bio *bio, chunk_t chunk)
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100206{
Mikulas Patocka42bc9542012-12-21 20:23:38 +0000207 struct dm_snap_tracked_chunk *c = dm_per_bio_data(bio, sizeof(struct dm_snap_tracked_chunk));
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100208
209 c->chunk = chunk;
210
Mikulas Patocka9aa0c0e2012-12-21 20:23:33 +0000211 spin_lock_irq(&s->tracked_chunk_lock);
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100212 hlist_add_head(&c->node,
213 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)]);
Mikulas Patocka9aa0c0e2012-12-21 20:23:33 +0000214 spin_unlock_irq(&s->tracked_chunk_lock);
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100215}
216
Mikulas Patockaee180262012-12-21 20:23:41 +0000217static void stop_tracking_chunk(struct dm_snapshot *s, struct bio *bio)
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100218{
Mikulas Patockaee180262012-12-21 20:23:41 +0000219 struct dm_snap_tracked_chunk *c = dm_per_bio_data(bio, sizeof(struct dm_snap_tracked_chunk));
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100220 unsigned long flags;
221
222 spin_lock_irqsave(&s->tracked_chunk_lock, flags);
223 hlist_del(&c->node);
224 spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100225}
226
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100227static int __chunk_is_tracked(struct dm_snapshot *s, chunk_t chunk)
228{
229 struct dm_snap_tracked_chunk *c;
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100230 int found = 0;
231
232 spin_lock_irq(&s->tracked_chunk_lock);
233
Sasha Levinb67bfe02013-02-27 17:06:00 -0800234 hlist_for_each_entry(c,
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100235 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)], node) {
236 if (c->chunk == chunk) {
237 found = 1;
238 break;
239 }
240 }
241
242 spin_unlock_irq(&s->tracked_chunk_lock);
243
244 return found;
245}
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247/*
Mike Snitzer615d1eb2009-12-10 23:52:29 +0000248 * This conflicting I/O is extremely improbable in the caller,
249 * so msleep(1) is sufficient and there is no need for a wait queue.
250 */
251static void __check_for_conflicting_io(struct dm_snapshot *s, chunk_t chunk)
252{
253 while (__chunk_is_tracked(s, chunk))
254 msleep(1);
255}
256
257/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 * One of these per registered origin, held in the snapshot_origins hash
259 */
260struct origin {
261 /* The origin device */
262 struct block_device *bdev;
263
264 struct list_head hash_list;
265
266 /* List of snapshots for this origin */
267 struct list_head snapshots;
268};
269
270/*
271 * Size of the hash table for origin volumes. If we make this
272 * the size of the minors list then it should be nearly perfect
273 */
274#define ORIGIN_HASH_SIZE 256
275#define ORIGIN_MASK 0xFF
276static struct list_head *_origins;
277static struct rw_semaphore _origins_lock;
278
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000279static DECLARE_WAIT_QUEUE_HEAD(_pending_exceptions_done);
280static DEFINE_SPINLOCK(_pending_exceptions_done_spinlock);
281static uint64_t _pending_exceptions_done_count;
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283static int init_origin_hash(void)
284{
285 int i;
286
287 _origins = kmalloc(ORIGIN_HASH_SIZE * sizeof(struct list_head),
288 GFP_KERNEL);
289 if (!_origins) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700290 DMERR("unable to allocate memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return -ENOMEM;
292 }
293
294 for (i = 0; i < ORIGIN_HASH_SIZE; i++)
295 INIT_LIST_HEAD(_origins + i);
296 init_rwsem(&_origins_lock);
297
298 return 0;
299}
300
301static void exit_origin_hash(void)
302{
303 kfree(_origins);
304}
305
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100306static unsigned origin_hash(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
308 return bdev->bd_dev & ORIGIN_MASK;
309}
310
311static struct origin *__lookup_origin(struct block_device *origin)
312{
313 struct list_head *ol;
314 struct origin *o;
315
316 ol = &_origins[origin_hash(origin)];
317 list_for_each_entry (o, ol, hash_list)
318 if (bdev_equal(o->bdev, origin))
319 return o;
320
321 return NULL;
322}
323
324static void __insert_origin(struct origin *o)
325{
326 struct list_head *sl = &_origins[origin_hash(o->bdev)];
327 list_add_tail(&o->hash_list, sl);
328}
329
330/*
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000331 * _origins_lock must be held when calling this function.
332 * Returns number of snapshots registered using the supplied cow device, plus:
333 * snap_src - a snapshot suitable for use as a source of exception handover
334 * snap_dest - a snapshot capable of receiving exception handover.
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000335 * snap_merge - an existing snapshot-merge target linked to the same origin.
336 * There can be at most one snapshot-merge target. The parameter is optional.
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000337 *
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000338 * Possible return values and states of snap_src and snap_dest.
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000339 * 0: NULL, NULL - first new snapshot
340 * 1: snap_src, NULL - normal snapshot
341 * 2: snap_src, snap_dest - waiting for handover
342 * 2: snap_src, NULL - handed over, waiting for old to be deleted
343 * 1: NULL, snap_dest - source got destroyed without handover
344 */
345static int __find_snapshots_sharing_cow(struct dm_snapshot *snap,
346 struct dm_snapshot **snap_src,
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000347 struct dm_snapshot **snap_dest,
348 struct dm_snapshot **snap_merge)
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000349{
350 struct dm_snapshot *s;
351 struct origin *o;
352 int count = 0;
353 int active;
354
355 o = __lookup_origin(snap->origin->bdev);
356 if (!o)
357 goto out;
358
359 list_for_each_entry(s, &o->snapshots, list) {
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000360 if (dm_target_is_snapshot_merge(s->ti) && snap_merge)
361 *snap_merge = s;
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000362 if (!bdev_equal(s->cow->bdev, snap->cow->bdev))
363 continue;
364
365 down_read(&s->lock);
366 active = s->active;
367 up_read(&s->lock);
368
369 if (active) {
370 if (snap_src)
371 *snap_src = s;
372 } else if (snap_dest)
373 *snap_dest = s;
374
375 count++;
376 }
377
378out:
379 return count;
380}
381
382/*
383 * On success, returns 1 if this snapshot is a handover destination,
384 * otherwise returns 0.
385 */
386static int __validate_exception_handover(struct dm_snapshot *snap)
387{
388 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000389 struct dm_snapshot *snap_merge = NULL;
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000390
391 /* Does snapshot need exceptions handed over to it? */
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000392 if ((__find_snapshots_sharing_cow(snap, &snap_src, &snap_dest,
393 &snap_merge) == 2) ||
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000394 snap_dest) {
395 snap->ti->error = "Snapshot cow pairing for exception "
396 "table handover failed";
397 return -EINVAL;
398 }
399
400 /*
401 * If no snap_src was found, snap cannot become a handover
402 * destination.
403 */
404 if (!snap_src)
405 return 0;
406
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000407 /*
408 * Non-snapshot-merge handover?
409 */
410 if (!dm_target_is_snapshot_merge(snap->ti))
411 return 1;
412
413 /*
414 * Do not allow more than one merging snapshot.
415 */
416 if (snap_merge) {
417 snap->ti->error = "A snapshot is already merging.";
418 return -EINVAL;
419 }
420
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000421 if (!snap_src->store->type->prepare_merge ||
422 !snap_src->store->type->commit_merge) {
423 snap->ti->error = "Snapshot exception store does not "
424 "support snapshot-merge.";
425 return -EINVAL;
426 }
427
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000428 return 1;
429}
430
431static void __insert_snapshot(struct origin *o, struct dm_snapshot *s)
432{
433 struct dm_snapshot *l;
434
435 /* Sort the list according to chunk size, largest-first smallest-last */
436 list_for_each_entry(l, &o->snapshots, list)
437 if (l->store->chunk_size < s->store->chunk_size)
438 break;
439 list_add_tail(&s->list, &l->list);
440}
441
442/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 * Make a note of the snapshot and its origin so we can look it
444 * up when the origin has a write on it.
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000445 *
446 * Also validate snapshot exception store handovers.
447 * On success, returns 1 if this registration is a handover destination,
448 * otherwise returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 */
450static int register_snapshot(struct dm_snapshot *snap)
451{
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000452 struct origin *o, *new_o = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 struct block_device *bdev = snap->origin->bdev;
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000454 int r = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000456 new_o = kmalloc(sizeof(*new_o), GFP_KERNEL);
457 if (!new_o)
458 return -ENOMEM;
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 down_write(&_origins_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000462 r = __validate_exception_handover(snap);
463 if (r < 0) {
464 kfree(new_o);
465 goto out;
466 }
467
468 o = __lookup_origin(bdev);
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000469 if (o)
470 kfree(new_o);
471 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 /* New origin */
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000473 o = new_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 /* Initialise the struct */
476 INIT_LIST_HEAD(&o->snapshots);
477 o->bdev = bdev;
478
479 __insert_origin(o);
480 }
481
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000482 __insert_snapshot(o, snap);
483
484out:
485 up_write(&_origins_lock);
486
487 return r;
488}
489
490/*
491 * Move snapshot to correct place in list according to chunk size.
492 */
493static void reregister_snapshot(struct dm_snapshot *s)
494{
495 struct block_device *bdev = s->origin->bdev;
496
497 down_write(&_origins_lock);
498
499 list_del(&s->list);
500 __insert_snapshot(__lookup_origin(bdev), s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 up_write(&_origins_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
505static void unregister_snapshot(struct dm_snapshot *s)
506{
507 struct origin *o;
508
509 down_write(&_origins_lock);
510 o = __lookup_origin(s->origin->bdev);
511
512 list_del(&s->list);
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000513 if (o && list_empty(&o->snapshots)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 list_del(&o->hash_list);
515 kfree(o);
516 }
517
518 up_write(&_origins_lock);
519}
520
521/*
522 * Implementation of the exception hash tables.
Milan Brozd74f81f2008-02-08 02:11:27 +0000523 * The lowest hash_shift bits of the chunk number are ignored, allowing
524 * some consecutive chunks to be grouped together.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 */
Jon Brassow3510cb92009-12-10 23:52:11 +0000526static int dm_exception_table_init(struct dm_exception_table *et,
527 uint32_t size, unsigned hash_shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 unsigned int i;
530
Milan Brozd74f81f2008-02-08 02:11:27 +0000531 et->hash_shift = hash_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 et->hash_mask = size - 1;
533 et->table = dm_vcalloc(size, sizeof(struct list_head));
534 if (!et->table)
535 return -ENOMEM;
536
537 for (i = 0; i < size; i++)
538 INIT_LIST_HEAD(et->table + i);
539
540 return 0;
541}
542
Jon Brassow3510cb92009-12-10 23:52:11 +0000543static void dm_exception_table_exit(struct dm_exception_table *et,
544 struct kmem_cache *mem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
546 struct list_head *slot;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000547 struct dm_exception *ex, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 int i, size;
549
550 size = et->hash_mask + 1;
551 for (i = 0; i < size; i++) {
552 slot = et->table + i;
553
554 list_for_each_entry_safe (ex, next, slot, hash_list)
555 kmem_cache_free(mem, ex);
556 }
557
558 vfree(et->table);
559}
560
Jon Brassow191437a2009-12-10 23:52:10 +0000561static uint32_t exception_hash(struct dm_exception_table *et, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Milan Brozd74f81f2008-02-08 02:11:27 +0000563 return (chunk >> et->hash_shift) & et->hash_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
Jon Brassow3510cb92009-12-10 23:52:11 +0000566static void dm_remove_exception(struct dm_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
568 list_del(&e->hash_list);
569}
570
571/*
572 * Return the exception data for a sector, or NULL if not
573 * remapped.
574 */
Jon Brassow3510cb92009-12-10 23:52:11 +0000575static struct dm_exception *dm_lookup_exception(struct dm_exception_table *et,
576 chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
578 struct list_head *slot;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000579 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 slot = &et->table[exception_hash(et, chunk)];
582 list_for_each_entry (e, slot, hash_list)
Milan Brozd74f81f2008-02-08 02:11:27 +0000583 if (chunk >= e->old_chunk &&
584 chunk <= e->old_chunk + dm_consecutive_chunk_count(e))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return e;
586
587 return NULL;
588}
589
Jon Brassow3510cb92009-12-10 23:52:11 +0000590static struct dm_exception *alloc_completed_exception(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Jon Brassow1d4989c2009-12-10 23:52:10 +0000592 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 e = kmem_cache_alloc(exception_cache, GFP_NOIO);
595 if (!e)
596 e = kmem_cache_alloc(exception_cache, GFP_ATOMIC);
597
598 return e;
599}
600
Jon Brassow3510cb92009-12-10 23:52:11 +0000601static void free_completed_exception(struct dm_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
603 kmem_cache_free(exception_cache, e);
604}
605
Mikulas Patocka92e86812008-07-21 12:00:35 +0100606static struct dm_snap_pending_exception *alloc_pending_exception(struct dm_snapshot *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Mikulas Patocka92e86812008-07-21 12:00:35 +0100608 struct dm_snap_pending_exception *pe = mempool_alloc(s->pending_pool,
609 GFP_NOIO);
610
Mikulas Patocka879129d22008-10-30 13:33:16 +0000611 atomic_inc(&s->pending_exceptions_count);
Mikulas Patocka92e86812008-07-21 12:00:35 +0100612 pe->snap = s;
613
614 return pe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100617static void free_pending_exception(struct dm_snap_pending_exception *pe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Mikulas Patocka879129d22008-10-30 13:33:16 +0000619 struct dm_snapshot *s = pe->snap;
620
621 mempool_free(pe, s->pending_pool);
622 smp_mb__before_atomic_dec();
623 atomic_dec(&s->pending_exceptions_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
Jon Brassow3510cb92009-12-10 23:52:11 +0000626static void dm_insert_exception(struct dm_exception_table *eh,
627 struct dm_exception *new_e)
Milan Brozd74f81f2008-02-08 02:11:27 +0000628{
Milan Brozd74f81f2008-02-08 02:11:27 +0000629 struct list_head *l;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000630 struct dm_exception *e = NULL;
Milan Brozd74f81f2008-02-08 02:11:27 +0000631
632 l = &eh->table[exception_hash(eh, new_e->old_chunk)];
633
634 /* Add immediately if this table doesn't support consecutive chunks */
635 if (!eh->hash_shift)
636 goto out;
637
638 /* List is ordered by old_chunk */
639 list_for_each_entry_reverse(e, l, hash_list) {
640 /* Insert after an existing chunk? */
641 if (new_e->old_chunk == (e->old_chunk +
642 dm_consecutive_chunk_count(e) + 1) &&
643 new_e->new_chunk == (dm_chunk_number(e->new_chunk) +
644 dm_consecutive_chunk_count(e) + 1)) {
645 dm_consecutive_chunk_count_inc(e);
Jon Brassow3510cb92009-12-10 23:52:11 +0000646 free_completed_exception(new_e);
Milan Brozd74f81f2008-02-08 02:11:27 +0000647 return;
648 }
649
650 /* Insert before an existing chunk? */
651 if (new_e->old_chunk == (e->old_chunk - 1) &&
652 new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) {
653 dm_consecutive_chunk_count_inc(e);
654 e->old_chunk--;
655 e->new_chunk--;
Jon Brassow3510cb92009-12-10 23:52:11 +0000656 free_completed_exception(new_e);
Milan Brozd74f81f2008-02-08 02:11:27 +0000657 return;
658 }
659
660 if (new_e->old_chunk > e->old_chunk)
661 break;
662 }
663
664out:
665 list_add(&new_e->hash_list, e ? &e->hash_list : l);
666}
667
Jonathan Brassowa159c1a2009-01-06 03:05:19 +0000668/*
669 * Callback used by the exception stores to load exceptions when
670 * initialising.
671 */
672static int dm_add_exception(void *context, chunk_t old, chunk_t new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Jonathan Brassowa159c1a2009-01-06 03:05:19 +0000674 struct dm_snapshot *s = context;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000675 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Jon Brassow3510cb92009-12-10 23:52:11 +0000677 e = alloc_completed_exception();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (!e)
679 return -ENOMEM;
680
681 e->old_chunk = old;
Milan Brozd74f81f2008-02-08 02:11:27 +0000682
683 /* Consecutive_count is implicitly initialised to zero */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 e->new_chunk = new;
Milan Brozd74f81f2008-02-08 02:11:27 +0000685
Jon Brassow3510cb92009-12-10 23:52:11 +0000686 dm_insert_exception(&s->complete, e);
Milan Brozd74f81f2008-02-08 02:11:27 +0000687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return 0;
689}
690
Mikulas Patocka7e201b32009-12-10 23:52:08 +0000691/*
692 * Return a minimum chunk size of all snapshots that have the specified origin.
693 * Return zero if the origin has no snapshots.
694 */
Mike Snitzer542f9032012-07-27 15:08:00 +0100695static uint32_t __minimum_chunk_size(struct origin *o)
Mikulas Patocka7e201b32009-12-10 23:52:08 +0000696{
697 struct dm_snapshot *snap;
698 unsigned chunk_size = 0;
699
700 if (o)
701 list_for_each_entry(snap, &o->snapshots, list)
702 chunk_size = min_not_zero(chunk_size,
703 snap->store->chunk_size);
704
Mike Snitzer542f9032012-07-27 15:08:00 +0100705 return (uint32_t) chunk_size;
Mikulas Patocka7e201b32009-12-10 23:52:08 +0000706}
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708/*
709 * Hard coded magic.
710 */
711static int calc_max_buckets(void)
712{
713 /* use a fixed size of 2MB */
714 unsigned long mem = 2 * 1024 * 1024;
715 mem /= sizeof(struct list_head);
716
717 return mem;
718}
719
720/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 * Allocate room for a suitable hash table.
722 */
Jonathan Brassowfee19982009-04-02 19:55:34 +0100723static int init_hash_tables(struct dm_snapshot *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
725 sector_t hash_size, cow_dev_size, origin_dev_size, max_buckets;
726
727 /*
728 * Calculate based on the size of the original volume or
729 * the COW volume...
730 */
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000731 cow_dev_size = get_dev_size(s->cow->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 origin_dev_size = get_dev_size(s->origin->bdev);
733 max_buckets = calc_max_buckets();
734
Jonathan Brassowfee19982009-04-02 19:55:34 +0100735 hash_size = min(origin_dev_size, cow_dev_size) >> s->store->chunk_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 hash_size = min(hash_size, max_buckets);
737
Mikulas Patocka8e87b9b2009-12-10 23:51:54 +0000738 if (hash_size < 64)
739 hash_size = 64;
Robert P. J. Day8defd832008-02-08 02:10:06 +0000740 hash_size = rounddown_pow_of_two(hash_size);
Jon Brassow3510cb92009-12-10 23:52:11 +0000741 if (dm_exception_table_init(&s->complete, hash_size,
742 DM_CHUNK_CONSECUTIVE_BITS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return -ENOMEM;
744
745 /*
746 * Allocate hash table for in-flight exceptions
747 * Make this smaller than the real hash table
748 */
749 hash_size >>= 3;
750 if (hash_size < 64)
751 hash_size = 64;
752
Jon Brassow3510cb92009-12-10 23:52:11 +0000753 if (dm_exception_table_init(&s->pending, hash_size, 0)) {
754 dm_exception_table_exit(&s->complete, exception_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return -ENOMEM;
756 }
757
758 return 0;
759}
760
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000761static void merge_shutdown(struct dm_snapshot *s)
762{
763 clear_bit_unlock(RUNNING_MERGE, &s->state_bits);
764 smp_mb__after_clear_bit();
765 wake_up_bit(&s->state_bits, RUNNING_MERGE);
766}
767
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000768static struct bio *__release_queued_bios_after_merge(struct dm_snapshot *s)
769{
770 s->first_merging_chunk = 0;
771 s->num_merging_chunks = 0;
772
773 return bio_list_get(&s->bios_queued_during_merge);
774}
775
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000776/*
777 * Remove one chunk from the index of completed exceptions.
778 */
779static int __remove_single_exception_chunk(struct dm_snapshot *s,
780 chunk_t old_chunk)
781{
782 struct dm_exception *e;
783
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000784 e = dm_lookup_exception(&s->complete, old_chunk);
785 if (!e) {
786 DMERR("Corruption detected: exception for block %llu is "
787 "on disk but not in memory",
788 (unsigned long long)old_chunk);
789 return -EINVAL;
790 }
791
792 /*
793 * If this is the only chunk using this exception, remove exception.
794 */
795 if (!dm_consecutive_chunk_count(e)) {
796 dm_remove_exception(e);
797 free_completed_exception(e);
798 return 0;
799 }
800
801 /*
802 * The chunk may be either at the beginning or the end of a
803 * group of consecutive chunks - never in the middle. We are
804 * removing chunks in the opposite order to that in which they
805 * were added, so this should always be true.
806 * Decrement the consecutive chunk counter and adjust the
807 * starting point if necessary.
808 */
809 if (old_chunk == e->old_chunk) {
810 e->old_chunk++;
811 e->new_chunk++;
812 } else if (old_chunk != e->old_chunk +
813 dm_consecutive_chunk_count(e)) {
814 DMERR("Attempt to merge block %llu from the "
815 "middle of a chunk range [%llu - %llu]",
816 (unsigned long long)old_chunk,
817 (unsigned long long)e->old_chunk,
818 (unsigned long long)
819 e->old_chunk + dm_consecutive_chunk_count(e));
820 return -EINVAL;
821 }
822
823 dm_consecutive_chunk_count_dec(e);
824
825 return 0;
826}
827
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000828static void flush_bios(struct bio *bio);
829
830static int remove_single_exception_chunk(struct dm_snapshot *s)
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000831{
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000832 struct bio *b = NULL;
833 int r;
834 chunk_t old_chunk = s->first_merging_chunk + s->num_merging_chunks - 1;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000835
836 down_write(&s->lock);
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000837
838 /*
839 * Process chunks (and associated exceptions) in reverse order
840 * so that dm_consecutive_chunk_count_dec() accounting works.
841 */
842 do {
843 r = __remove_single_exception_chunk(s, old_chunk);
844 if (r)
845 goto out;
846 } while (old_chunk-- > s->first_merging_chunk);
847
848 b = __release_queued_bios_after_merge(s);
849
850out:
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000851 up_write(&s->lock);
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000852 if (b)
853 flush_bios(b);
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000854
855 return r;
856}
857
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000858static int origin_write_extent(struct dm_snapshot *merging_snap,
859 sector_t sector, unsigned chunk_size);
860
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000861static void merge_callback(int read_err, unsigned long write_err,
862 void *context);
863
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000864static uint64_t read_pending_exceptions_done_count(void)
865{
866 uint64_t pending_exceptions_done;
867
868 spin_lock(&_pending_exceptions_done_spinlock);
869 pending_exceptions_done = _pending_exceptions_done_count;
870 spin_unlock(&_pending_exceptions_done_spinlock);
871
872 return pending_exceptions_done;
873}
874
875static void increment_pending_exceptions_done_count(void)
876{
877 spin_lock(&_pending_exceptions_done_spinlock);
878 _pending_exceptions_done_count++;
879 spin_unlock(&_pending_exceptions_done_spinlock);
880
881 wake_up_all(&_pending_exceptions_done);
882}
883
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000884static void snapshot_merge_next_chunks(struct dm_snapshot *s)
885{
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000886 int i, linear_chunks;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000887 chunk_t old_chunk, new_chunk;
888 struct dm_io_region src, dest;
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000889 sector_t io_size;
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000890 uint64_t previous_count;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000891
892 BUG_ON(!test_bit(RUNNING_MERGE, &s->state_bits));
893 if (unlikely(test_bit(SHUTDOWN_MERGE, &s->state_bits)))
894 goto shut;
895
896 /*
897 * valid flag never changes during merge, so no lock required.
898 */
899 if (!s->valid) {
900 DMERR("Snapshot is invalid: can't merge");
901 goto shut;
902 }
903
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000904 linear_chunks = s->store->type->prepare_merge(s->store, &old_chunk,
905 &new_chunk);
906 if (linear_chunks <= 0) {
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +0000907 if (linear_chunks < 0) {
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000908 DMERR("Read error in exception store: "
909 "shutting down merge");
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +0000910 down_write(&s->lock);
911 s->merge_failed = 1;
912 up_write(&s->lock);
913 }
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000914 goto shut;
915 }
916
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000917 /* Adjust old_chunk and new_chunk to reflect start of linear region */
918 old_chunk = old_chunk + 1 - linear_chunks;
919 new_chunk = new_chunk + 1 - linear_chunks;
920
921 /*
922 * Use one (potentially large) I/O to copy all 'linear_chunks'
923 * from the exception store to the origin
924 */
925 io_size = linear_chunks * s->store->chunk_size;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000926
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000927 dest.bdev = s->origin->bdev;
928 dest.sector = chunk_to_sector(s->store, old_chunk);
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000929 dest.count = min(io_size, get_dev_size(dest.bdev) - dest.sector);
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000930
931 src.bdev = s->cow->bdev;
932 src.sector = chunk_to_sector(s->store, new_chunk);
933 src.count = dest.count;
934
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000935 /*
936 * Reallocate any exceptions needed in other snapshots then
937 * wait for the pending exceptions to complete.
938 * Each time any pending exception (globally on the system)
939 * completes we are woken and repeat the process to find out
940 * if we can proceed. While this may not seem a particularly
941 * efficient algorithm, it is not expected to have any
942 * significant impact on performance.
943 */
944 previous_count = read_pending_exceptions_done_count();
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000945 while (origin_write_extent(s, dest.sector, io_size)) {
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000946 wait_event(_pending_exceptions_done,
947 (read_pending_exceptions_done_count() !=
948 previous_count));
949 /* Retry after the wait, until all exceptions are done. */
950 previous_count = read_pending_exceptions_done_count();
951 }
952
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000953 down_write(&s->lock);
954 s->first_merging_chunk = old_chunk;
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000955 s->num_merging_chunks = linear_chunks;
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000956 up_write(&s->lock);
957
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000958 /* Wait until writes to all 'linear_chunks' drain */
959 for (i = 0; i < linear_chunks; i++)
960 __check_for_conflicting_io(s, old_chunk + i);
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000961
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000962 dm_kcopyd_copy(s->kcopyd_client, &src, 1, &dest, 0, merge_callback, s);
963 return;
964
965shut:
966 merge_shutdown(s);
967}
968
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000969static void error_bios(struct bio *bio);
970
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000971static void merge_callback(int read_err, unsigned long write_err, void *context)
972{
973 struct dm_snapshot *s = context;
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000974 struct bio *b = NULL;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000975
976 if (read_err || write_err) {
977 if (read_err)
978 DMERR("Read error: shutting down merge.");
979 else
980 DMERR("Write error: shutting down merge.");
981 goto shut;
982 }
983
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000984 if (s->store->type->commit_merge(s->store,
985 s->num_merging_chunks) < 0) {
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000986 DMERR("Write error in exception store: shutting down merge");
987 goto shut;
988 }
989
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000990 if (remove_single_exception_chunk(s) < 0)
991 goto shut;
992
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000993 snapshot_merge_next_chunks(s);
994
995 return;
996
997shut:
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000998 down_write(&s->lock);
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +0000999 s->merge_failed = 1;
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001000 b = __release_queued_bios_after_merge(s);
1001 up_write(&s->lock);
1002 error_bios(b);
1003
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001004 merge_shutdown(s);
1005}
1006
1007static void start_merge(struct dm_snapshot *s)
1008{
1009 if (!test_and_set_bit(RUNNING_MERGE, &s->state_bits))
1010 snapshot_merge_next_chunks(s);
1011}
1012
1013static int wait_schedule(void *ptr)
1014{
1015 schedule();
1016
1017 return 0;
1018}
1019
1020/*
1021 * Stop the merging process and wait until it finishes.
1022 */
1023static void stop_merge(struct dm_snapshot *s)
1024{
1025 set_bit(SHUTDOWN_MERGE, &s->state_bits);
1026 wait_on_bit(&s->state_bits, RUNNING_MERGE, wait_schedule,
1027 TASK_UNINTERRUPTIBLE);
1028 clear_bit(SHUTDOWN_MERGE, &s->state_bits);
1029}
1030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 * Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size>
1033 */
1034static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1035{
1036 struct dm_snapshot *s;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001037 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 int r = -EINVAL;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001039 char *origin_path, *cow_path;
Mike Snitzer10b81062009-12-10 23:52:31 +00001040 unsigned args_used, num_flush_requests = 1;
1041 fmode_t origin_mode = FMODE_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -07001043 if (argc != 4) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001044 ti->error = "requires exactly 4 arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 r = -EINVAL;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001046 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
1048
Mike Snitzer10b81062009-12-10 23:52:31 +00001049 if (dm_target_is_snapshot_merge(ti)) {
1050 num_flush_requests = 2;
1051 origin_mode = FMODE_WRITE;
1052 }
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 s = kmalloc(sizeof(*s), GFP_KERNEL);
Jonathan Brassowfee19982009-04-02 19:55:34 +01001055 if (!s) {
Jonathan Brassowa2d2b032011-08-02 12:32:03 +01001056 ti->error = "Cannot allocate private snapshot structure";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 r = -ENOMEM;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001058 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
1060
Mikulas Patockac2411042010-08-12 04:13:51 +01001061 origin_path = argv[0];
1062 argv++;
1063 argc--;
1064
1065 r = dm_get_device(ti, origin_path, origin_mode, &s->origin);
1066 if (r) {
1067 ti->error = "Cannot get origin device";
1068 goto bad_origin;
1069 }
1070
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001071 cow_path = argv[0];
1072 argv++;
1073 argc--;
1074
Milan Broz024d37e2011-03-24 13:52:14 +00001075 r = dm_get_device(ti, cow_path, dm_table_get_mode(ti->table), &s->cow);
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001076 if (r) {
1077 ti->error = "Cannot get COW device";
1078 goto bad_cow;
1079 }
1080
1081 r = dm_exception_store_create(ti, argc, argv, s, &args_used, &s->store);
1082 if (r) {
1083 ti->error = "Couldn't create exception store";
1084 r = -EINVAL;
1085 goto bad_store;
1086 }
1087
1088 argv += args_used;
1089 argc -= args_used;
1090
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001091 s->ti = ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 s->valid = 1;
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001093 s->active = 0;
Mikulas Patocka879129d22008-10-30 13:33:16 +00001094 atomic_set(&s->pending_exceptions_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 init_rwsem(&s->lock);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001096 INIT_LIST_HEAD(&s->list);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001097 spin_lock_init(&s->pe_lock);
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001098 s->state_bits = 0;
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +00001099 s->merge_failed = 0;
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001100 s->first_merging_chunk = 0;
1101 s->num_merging_chunks = 0;
1102 bio_list_init(&s->bios_queued_during_merge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 /* Allocate hash table for COW data */
Jonathan Brassowfee19982009-04-02 19:55:34 +01001105 if (init_hash_tables(s)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 ti->error = "Unable to allocate hash table space";
1107 r = -ENOMEM;
Jonathan Brassowfee19982009-04-02 19:55:34 +01001108 goto bad_hash_tables;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
1110
Mikulas Patockafa34ce72011-05-29 13:03:13 +01001111 s->kcopyd_client = dm_kcopyd_client_create();
1112 if (IS_ERR(s->kcopyd_client)) {
1113 r = PTR_ERR(s->kcopyd_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 ti->error = "Could not create kcopyd client";
Jonathan Brassowfee19982009-04-02 19:55:34 +01001115 goto bad_kcopyd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 }
1117
Mikulas Patocka92e86812008-07-21 12:00:35 +01001118 s->pending_pool = mempool_create_slab_pool(MIN_IOS, pending_cache);
1119 if (!s->pending_pool) {
1120 ti->error = "Could not allocate mempool for pending exceptions";
Jonathan Brassowfee19982009-04-02 19:55:34 +01001121 goto bad_pending_pool;
Mikulas Patocka92e86812008-07-21 12:00:35 +01001122 }
1123
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001124 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
1125 INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]);
1126
1127 spin_lock_init(&s->tracked_chunk_lock);
1128
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001129 ti->private = s;
Mike Snitzer10b81062009-12-10 23:52:31 +00001130 ti->num_flush_requests = num_flush_requests;
Mikulas Patocka42bc9542012-12-21 20:23:38 +00001131 ti->per_bio_data_size = sizeof(struct dm_snap_tracked_chunk);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001132
1133 /* Add snapshot to the list of snapshots for this origin */
1134 /* Exceptions aren't triggered till snapshot_resume() is called */
1135 r = register_snapshot(s);
1136 if (r == -ENOMEM) {
1137 ti->error = "Snapshot origin struct allocation failed";
1138 goto bad_load_and_register;
1139 } else if (r < 0) {
1140 /* invalid handover, register_snapshot has set ti->error */
1141 goto bad_load_and_register;
1142 }
1143
1144 /*
1145 * Metadata must only be loaded into one table at once, so skip this
1146 * if metadata will be handed over during resume.
1147 * Chunk size will be set during the handover - set it to zero to
1148 * ensure it's ignored.
1149 */
1150 if (r > 0) {
1151 s->store->chunk_size = 0;
1152 return 0;
1153 }
1154
Jonathan Brassow493df712009-04-02 19:55:31 +01001155 r = s->store->type->read_metadata(s->store, dm_add_exception,
1156 (void *)s);
Milan Broz07641472007-07-12 17:28:13 +01001157 if (r < 0) {
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -07001158 ti->error = "Failed to read snapshot metadata";
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001159 goto bad_read_metadata;
Milan Broz07641472007-07-12 17:28:13 +01001160 } else if (r > 0) {
1161 s->valid = 0;
1162 DMWARN("Snapshot is marked invalid.");
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -07001163 }
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001164
Mikulas Patocka3f2412d2009-10-16 23:18:16 +01001165 if (!s->store->chunk_size) {
1166 ti->error = "Chunk size not set";
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001167 goto bad_read_metadata;
Mikulas Patocka3f2412d2009-10-16 23:18:16 +01001168 }
Mike Snitzer542f9032012-07-27 15:08:00 +01001169
1170 r = dm_set_target_max_io_len(ti, s->store->chunk_size);
1171 if (r)
1172 goto bad_read_metadata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174 return 0;
1175
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001176bad_read_metadata:
1177 unregister_snapshot(s);
1178
Jonathan Brassowfee19982009-04-02 19:55:34 +01001179bad_load_and_register:
Mikulas Patocka92e86812008-07-21 12:00:35 +01001180 mempool_destroy(s->pending_pool);
1181
Jonathan Brassowfee19982009-04-02 19:55:34 +01001182bad_pending_pool:
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001183 dm_kcopyd_client_destroy(s->kcopyd_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Jonathan Brassowfee19982009-04-02 19:55:34 +01001185bad_kcopyd:
Jon Brassow3510cb92009-12-10 23:52:11 +00001186 dm_exception_table_exit(&s->pending, pending_cache);
1187 dm_exception_table_exit(&s->complete, exception_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Jonathan Brassowfee19982009-04-02 19:55:34 +01001189bad_hash_tables:
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001190 dm_exception_store_destroy(s->store);
1191
1192bad_store:
1193 dm_put_device(ti, s->cow);
1194
1195bad_cow:
Mikulas Patockac2411042010-08-12 04:13:51 +01001196 dm_put_device(ti, s->origin);
1197
1198bad_origin:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 kfree(s);
1200
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001201bad:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 return r;
1203}
1204
Milan Broz31c93a02006-12-08 02:41:11 -08001205static void __free_exceptions(struct dm_snapshot *s)
1206{
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001207 dm_kcopyd_client_destroy(s->kcopyd_client);
Milan Broz31c93a02006-12-08 02:41:11 -08001208 s->kcopyd_client = NULL;
1209
Jon Brassow3510cb92009-12-10 23:52:11 +00001210 dm_exception_table_exit(&s->pending, pending_cache);
1211 dm_exception_table_exit(&s->complete, exception_cache);
Milan Broz31c93a02006-12-08 02:41:11 -08001212}
1213
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001214static void __handover_exceptions(struct dm_snapshot *snap_src,
1215 struct dm_snapshot *snap_dest)
1216{
1217 union {
1218 struct dm_exception_table table_swap;
1219 struct dm_exception_store *store_swap;
1220 } u;
1221
1222 /*
1223 * Swap all snapshot context information between the two instances.
1224 */
1225 u.table_swap = snap_dest->complete;
1226 snap_dest->complete = snap_src->complete;
1227 snap_src->complete = u.table_swap;
1228
1229 u.store_swap = snap_dest->store;
1230 snap_dest->store = snap_src->store;
1231 snap_src->store = u.store_swap;
1232
1233 snap_dest->store->snap = snap_dest;
1234 snap_src->store->snap = snap_src;
1235
Mike Snitzer542f9032012-07-27 15:08:00 +01001236 snap_dest->ti->max_io_len = snap_dest->store->chunk_size;
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001237 snap_dest->valid = snap_src->valid;
1238
1239 /*
1240 * Set source invalid to ensure it receives no further I/O.
1241 */
1242 snap_src->valid = 0;
1243}
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245static void snapshot_dtr(struct dm_target *ti)
1246{
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001247#ifdef CONFIG_DM_DEBUG
1248 int i;
1249#endif
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001250 struct dm_snapshot *s = ti->private;
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001251 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001253 down_read(&_origins_lock);
1254 /* Check whether exception handover must be cancelled */
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +00001255 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001256 if (snap_src && snap_dest && (s == snap_src)) {
1257 down_write(&snap_dest->lock);
1258 snap_dest->valid = 0;
1259 up_write(&snap_dest->lock);
1260 DMERR("Cancelling snapshot handover.");
1261 }
1262 up_read(&_origins_lock);
1263
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001264 if (dm_target_is_snapshot_merge(ti))
1265 stop_merge(s);
1266
Alasdair G Kergon138728dc2006-03-27 01:17:50 -08001267 /* Prevent further origin writes from using this snapshot. */
1268 /* After this returns there can be no new kcopyd jobs. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 unregister_snapshot(s);
1270
Mikulas Patocka879129d22008-10-30 13:33:16 +00001271 while (atomic_read(&s->pending_exceptions_count))
Mikulas Patocka90fa1522009-01-06 03:04:54 +00001272 msleep(1);
Mikulas Patocka879129d22008-10-30 13:33:16 +00001273 /*
1274 * Ensure instructions in mempool_destroy aren't reordered
1275 * before atomic_read.
1276 */
1277 smp_mb();
1278
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001279#ifdef CONFIG_DM_DEBUG
1280 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
1281 BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i]));
1282#endif
1283
Milan Broz31c93a02006-12-08 02:41:11 -08001284 __free_exceptions(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Mikulas Patocka92e86812008-07-21 12:00:35 +01001286 mempool_destroy(s->pending_pool);
1287
Jonathan Brassowfee19982009-04-02 19:55:34 +01001288 dm_exception_store_destroy(s->store);
Alasdair G Kergon138728dc2006-03-27 01:17:50 -08001289
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001290 dm_put_device(ti, s->cow);
1291
Mikulas Patockac2411042010-08-12 04:13:51 +01001292 dm_put_device(ti, s->origin);
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 kfree(s);
1295}
1296
1297/*
1298 * Flush a list of buffers.
1299 */
1300static void flush_bios(struct bio *bio)
1301{
1302 struct bio *n;
1303
1304 while (bio) {
1305 n = bio->bi_next;
1306 bio->bi_next = NULL;
1307 generic_make_request(bio);
1308 bio = n;
1309 }
1310}
1311
Mikulas Patocka515ad662009-12-10 23:52:30 +00001312static int do_origin(struct dm_dev *origin, struct bio *bio);
1313
1314/*
1315 * Flush a list of buffers.
1316 */
1317static void retry_origin_bios(struct dm_snapshot *s, struct bio *bio)
1318{
1319 struct bio *n;
1320 int r;
1321
1322 while (bio) {
1323 n = bio->bi_next;
1324 bio->bi_next = NULL;
1325 r = do_origin(s->origin, bio);
1326 if (r == DM_MAPIO_REMAPPED)
1327 generic_make_request(bio);
1328 bio = n;
1329 }
1330}
1331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332/*
1333 * Error a list of buffers.
1334 */
1335static void error_bios(struct bio *bio)
1336{
1337 struct bio *n;
1338
1339 while (bio) {
1340 n = bio->bi_next;
1341 bio->bi_next = NULL;
NeilBrown6712ecf2007-09-27 12:47:43 +02001342 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 bio = n;
1344 }
1345}
1346
Alasdair G Kergon695368a2006-10-03 01:15:31 -07001347static void __invalidate_snapshot(struct dm_snapshot *s, int err)
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001348{
1349 if (!s->valid)
1350 return;
1351
1352 if (err == -EIO)
1353 DMERR("Invalidating snapshot: Error reading/writing.");
1354 else if (err == -ENOMEM)
1355 DMERR("Invalidating snapshot: Unable to allocate exception.");
1356
Jonathan Brassow493df712009-04-02 19:55:31 +01001357 if (s->store->type->drop_snapshot)
1358 s->store->type->drop_snapshot(s->store);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001359
1360 s->valid = 0;
1361
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001362 dm_table_event(s->ti->table);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001363}
1364
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001365static void pending_complete(struct dm_snap_pending_exception *pe, int success)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
Jon Brassow1d4989c2009-12-10 23:52:10 +00001367 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 struct dm_snapshot *s = pe->snap;
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001369 struct bio *origin_bios = NULL;
1370 struct bio *snapshot_bios = NULL;
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001371 struct bio *full_bio = NULL;
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001372 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001374 if (!success) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 /* Read/write error - snapshot is unusable */
1376 down_write(&s->lock);
Alasdair G Kergon695368a2006-10-03 01:15:31 -07001377 __invalidate_snapshot(s, -EIO);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001378 error = 1;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001379 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 }
1381
Jon Brassow3510cb92009-12-10 23:52:11 +00001382 e = alloc_completed_exception();
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001383 if (!e) {
1384 down_write(&s->lock);
Alasdair G Kergon695368a2006-10-03 01:15:31 -07001385 __invalidate_snapshot(s, -ENOMEM);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001386 error = 1;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001387 goto out;
1388 }
1389 *e = pe->e;
1390
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001391 down_write(&s->lock);
1392 if (!s->valid) {
Jon Brassow3510cb92009-12-10 23:52:11 +00001393 free_completed_exception(e);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001394 error = 1;
1395 goto out;
1396 }
1397
Mike Snitzer615d1eb2009-12-10 23:52:29 +00001398 /* Check for conflicting reads */
1399 __check_for_conflicting_io(s, pe->e.old_chunk);
Mikulas Patockaa8d41b52008-07-21 12:00:34 +01001400
1401 /*
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001402 * Add a proper exception, and remove the
1403 * in-flight exception from the list.
1404 */
Jon Brassow3510cb92009-12-10 23:52:11 +00001405 dm_insert_exception(&s->complete, e);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001406
Jonathan Brassowa2d2b032011-08-02 12:32:03 +01001407out:
Jon Brassow3510cb92009-12-10 23:52:11 +00001408 dm_remove_exception(&pe->e);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001409 snapshot_bios = bio_list_get(&pe->snapshot_bios);
Mikulas Patocka515ad662009-12-10 23:52:30 +00001410 origin_bios = bio_list_get(&pe->origin_bios);
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001411 full_bio = pe->full_bio;
1412 if (full_bio) {
1413 full_bio->bi_end_io = pe->full_bio_end_io;
1414 full_bio->bi_private = pe->full_bio_private;
1415 }
Mikulas Patocka515ad662009-12-10 23:52:30 +00001416 free_pending_exception(pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Mikulas Patocka73dfd072009-12-10 23:52:34 +00001418 increment_pending_exceptions_done_count();
1419
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001420 up_write(&s->lock);
1421
1422 /* Submit any pending write bios */
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001423 if (error) {
1424 if (full_bio)
1425 bio_io_error(full_bio);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001426 error_bios(snapshot_bios);
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001427 } else {
1428 if (full_bio)
1429 bio_endio(full_bio, 0);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001430 flush_bios(snapshot_bios);
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001431 }
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001432
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 */
Jonathan Brassowa2d2b032011-08-02 12:32:03 +01001482 dm_kcopyd_copy(s->kcopyd_client, &src, 1, &dest, 0, copy_callback, pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483}
1484
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001485static void full_bio_end_io(struct bio *bio, int error)
1486{
1487 void *callback_data = bio->bi_private;
1488
1489 dm_kcopyd_do_callback(callback_data, 0, error ? 1 : 0);
1490}
1491
1492static void start_full_bio(struct dm_snap_pending_exception *pe,
1493 struct bio *bio)
1494{
1495 struct dm_snapshot *s = pe->snap;
1496 void *callback_data;
1497
1498 pe->full_bio = bio;
1499 pe->full_bio_end_io = bio->bi_end_io;
1500 pe->full_bio_private = bio->bi_private;
1501
1502 callback_data = dm_kcopyd_prepare_callback(s->kcopyd_client,
1503 copy_callback, pe);
1504
1505 bio->bi_end_io = full_bio_end_io;
1506 bio->bi_private = callback_data;
1507
1508 generic_make_request(bio);
1509}
1510
Mikulas Patocka29138082009-04-02 19:55:25 +01001511static struct dm_snap_pending_exception *
1512__lookup_pending_exception(struct dm_snapshot *s, chunk_t chunk)
1513{
Jon Brassow3510cb92009-12-10 23:52:11 +00001514 struct dm_exception *e = dm_lookup_exception(&s->pending, chunk);
Mikulas Patocka29138082009-04-02 19:55:25 +01001515
1516 if (!e)
1517 return NULL;
1518
1519 return container_of(e, struct dm_snap_pending_exception, e);
1520}
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522/*
1523 * Looks to see if this snapshot already has a pending exception
1524 * for this chunk, otherwise it allocates a new one and inserts
1525 * it into the pending table.
1526 *
1527 * NOTE: a write lock must be held on snap->lock before calling
1528 * this.
1529 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001530static struct dm_snap_pending_exception *
Mikulas Patockac6621392009-04-02 19:55:25 +01001531__find_pending_exception(struct dm_snapshot *s,
1532 struct dm_snap_pending_exception *pe, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
Mikulas Patockac6621392009-04-02 19:55:25 +01001534 struct dm_snap_pending_exception *pe2;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001535
Mikulas Patocka29138082009-04-02 19:55:25 +01001536 pe2 = __lookup_pending_exception(s, chunk);
1537 if (pe2) {
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001538 free_pending_exception(pe);
Mikulas Patocka29138082009-04-02 19:55:25 +01001539 return pe2;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001540 }
1541
1542 pe->e.old_chunk = chunk;
1543 bio_list_init(&pe->origin_bios);
1544 bio_list_init(&pe->snapshot_bios);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001545 pe->started = 0;
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001546 pe->full_bio = NULL;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001547
Jonathan Brassow493df712009-04-02 19:55:31 +01001548 if (s->store->type->prepare_exception(s->store, &pe->e)) {
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001549 free_pending_exception(pe);
1550 return NULL;
1551 }
1552
Jon Brassow3510cb92009-12-10 23:52:11 +00001553 dm_insert_exception(&s->pending, &pe->e);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 return pe;
1556}
1557
Jon Brassow1d4989c2009-12-10 23:52:10 +00001558static void remap_exception(struct dm_snapshot *s, struct dm_exception *e,
Milan Brozd74f81f2008-02-08 02:11:27 +00001559 struct bio *bio, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001561 bio->bi_bdev = s->cow->bdev;
Jonathan Brassow71fab002009-04-02 19:55:33 +01001562 bio->bi_sector = chunk_to_sector(s->store,
1563 dm_chunk_number(e->new_chunk) +
1564 (chunk - e->old_chunk)) +
1565 (bio->bi_sector &
1566 s->store->chunk_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567}
1568
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001569static int snapshot_map(struct dm_target *ti, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
Jon Brassow1d4989c2009-12-10 23:52:10 +00001571 struct dm_exception *e;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001572 struct dm_snapshot *s = ti->private;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001573 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 chunk_t chunk;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001575 struct dm_snap_pending_exception *pe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Mikulas Patockaee180262012-12-21 20:23:41 +00001577 init_tracked_chunk(bio);
1578
Tejun Heod87f4c12010-09-03 11:56:19 +02001579 if (bio->bi_rw & REQ_FLUSH) {
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001580 bio->bi_bdev = s->cow->bdev;
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01001581 return DM_MAPIO_REMAPPED;
1582 }
1583
Jonathan Brassow71fab002009-04-02 19:55:33 +01001584 chunk = sector_to_chunk(s->store, bio->bi_sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 /* Full snapshots are not usable */
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001587 /* To get here the table must be live so s->active is always set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 if (!s->valid)
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -07001589 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001591 /* FIXME: should only take write lock if we need
1592 * to copy an exception */
1593 down_write(&s->lock);
1594
1595 if (!s->valid) {
1596 r = -EIO;
1597 goto out_unlock;
1598 }
1599
1600 /* If the block is already remapped - use that, else remap it */
Jon Brassow3510cb92009-12-10 23:52:11 +00001601 e = dm_lookup_exception(&s->complete, chunk);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001602 if (e) {
Milan Brozd74f81f2008-02-08 02:11:27 +00001603 remap_exception(s, e, bio, chunk);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001604 goto out_unlock;
1605 }
1606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 /*
1608 * Write to snapshot - higher level takes care of RW/RO
1609 * flags so we should only get this if we are
1610 * writeable.
1611 */
1612 if (bio_rw(bio) == WRITE) {
Mikulas Patocka29138082009-04-02 19:55:25 +01001613 pe = __lookup_pending_exception(s, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001614 if (!pe) {
Mikulas Patockac6621392009-04-02 19:55:25 +01001615 up_write(&s->lock);
1616 pe = alloc_pending_exception(s);
1617 down_write(&s->lock);
1618
1619 if (!s->valid) {
1620 free_pending_exception(pe);
1621 r = -EIO;
1622 goto out_unlock;
1623 }
1624
Jon Brassow3510cb92009-12-10 23:52:11 +00001625 e = dm_lookup_exception(&s->complete, chunk);
Mikulas Patocka35bf6592009-04-02 19:55:26 +01001626 if (e) {
1627 free_pending_exception(pe);
1628 remap_exception(s, e, bio, chunk);
1629 goto out_unlock;
1630 }
1631
Mikulas Patockac6621392009-04-02 19:55:25 +01001632 pe = __find_pending_exception(s, pe, chunk);
Mikulas Patocka29138082009-04-02 19:55:25 +01001633 if (!pe) {
1634 __invalidate_snapshot(s, -ENOMEM);
1635 r = -EIO;
1636 goto out_unlock;
1637 }
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001638 }
1639
Milan Brozd74f81f2008-02-08 02:11:27 +00001640 remap_exception(s, &pe->e, bio, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001641
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001642 r = DM_MAPIO_SUBMITTED;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001643
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001644 if (!pe->started &&
1645 bio->bi_size == (s->store->chunk_size << SECTOR_SHIFT)) {
1646 pe->started = 1;
1647 up_write(&s->lock);
1648 start_full_bio(pe, bio);
1649 goto out;
1650 }
1651
1652 bio_list_add(&pe->snapshot_bios, bio);
1653
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001654 if (!pe->started) {
1655 /* this is protected by snap->lock */
1656 pe->started = 1;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001657 up_write(&s->lock);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001658 start_copy(pe);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001659 goto out;
1660 }
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001661 } else {
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001662 bio->bi_bdev = s->origin->bdev;
Mikulas Patockaee180262012-12-21 20:23:41 +00001663 track_chunk(s, bio, chunk);
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Jonathan Brassowa2d2b032011-08-02 12:32:03 +01001666out_unlock:
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001667 up_write(&s->lock);
Jonathan Brassowa2d2b032011-08-02 12:32:03 +01001668out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 return r;
1670}
1671
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001672/*
1673 * A snapshot-merge target behaves like a combination of a snapshot
1674 * target and a snapshot-origin target. It only generates new
1675 * exceptions in other snapshots and not in the one that is being
1676 * merged.
1677 *
1678 * For each chunk, if there is an existing exception, it is used to
1679 * redirect I/O to the cow device. Otherwise I/O is sent to the origin,
1680 * which in turn might generate exceptions in other snapshots.
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001681 * If merging is currently taking place on the chunk in question, the
1682 * I/O is deferred by adding it to s->bios_queued_during_merge.
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001683 */
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001684static int snapshot_merge_map(struct dm_target *ti, struct bio *bio)
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001685{
1686 struct dm_exception *e;
1687 struct dm_snapshot *s = ti->private;
1688 int r = DM_MAPIO_REMAPPED;
1689 chunk_t chunk;
1690
Mikulas Patockaee180262012-12-21 20:23:41 +00001691 init_tracked_chunk(bio);
1692
Tejun Heod87f4c12010-09-03 11:56:19 +02001693 if (bio->bi_rw & REQ_FLUSH) {
Mikulas Patockaddbd6582012-12-21 20:23:39 +00001694 if (!dm_bio_get_target_request_nr(bio))
Mike Snitzer10b81062009-12-10 23:52:31 +00001695 bio->bi_bdev = s->origin->bdev;
1696 else
1697 bio->bi_bdev = s->cow->bdev;
Mike Snitzer10b81062009-12-10 23:52:31 +00001698 return DM_MAPIO_REMAPPED;
1699 }
1700
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001701 chunk = sector_to_chunk(s->store, bio->bi_sector);
1702
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001703 down_write(&s->lock);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001704
Mikulas Patockad2fdb772009-12-10 23:52:36 +00001705 /* Full merging snapshots are redirected to the origin */
1706 if (!s->valid)
1707 goto redirect_to_origin;
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001708
1709 /* If the block is already remapped - use that */
1710 e = dm_lookup_exception(&s->complete, chunk);
1711 if (e) {
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001712 /* Queue writes overlapping with chunks being merged */
1713 if (bio_rw(bio) == WRITE &&
1714 chunk >= s->first_merging_chunk &&
1715 chunk < (s->first_merging_chunk +
1716 s->num_merging_chunks)) {
1717 bio->bi_bdev = s->origin->bdev;
1718 bio_list_add(&s->bios_queued_during_merge, bio);
1719 r = DM_MAPIO_SUBMITTED;
1720 goto out_unlock;
1721 }
Mikulas Patocka17aa0332009-12-10 23:52:33 +00001722
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001723 remap_exception(s, e, bio, chunk);
Mikulas Patocka17aa0332009-12-10 23:52:33 +00001724
1725 if (bio_rw(bio) == WRITE)
Mikulas Patockaee180262012-12-21 20:23:41 +00001726 track_chunk(s, bio, chunk);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001727 goto out_unlock;
1728 }
1729
Mikulas Patockad2fdb772009-12-10 23:52:36 +00001730redirect_to_origin:
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001731 bio->bi_bdev = s->origin->bdev;
1732
1733 if (bio_rw(bio) == WRITE) {
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001734 up_write(&s->lock);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001735 return do_origin(s->origin, bio);
1736 }
1737
1738out_unlock:
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001739 up_write(&s->lock);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001740
1741 return r;
1742}
1743
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001744static int snapshot_end_io(struct dm_target *ti, struct bio *bio, int error)
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001745{
1746 struct dm_snapshot *s = ti->private;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001747
Mikulas Patockaee180262012-12-21 20:23:41 +00001748 if (is_bio_tracked(bio))
1749 stop_tracking_chunk(s, bio);
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001750
1751 return 0;
1752}
1753
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001754static void snapshot_merge_presuspend(struct dm_target *ti)
1755{
1756 struct dm_snapshot *s = ti->private;
1757
1758 stop_merge(s);
1759}
1760
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001761static int snapshot_preresume(struct dm_target *ti)
1762{
1763 int r = 0;
1764 struct dm_snapshot *s = ti->private;
1765 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
1766
1767 down_read(&_origins_lock);
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +00001768 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001769 if (snap_src && snap_dest) {
1770 down_read(&snap_src->lock);
1771 if (s == snap_src) {
1772 DMERR("Unable to resume snapshot source until "
1773 "handover completes.");
1774 r = -EINVAL;
Mike Snitzerb83b2f22011-01-13 19:59:59 +00001775 } else if (!dm_suspended(snap_src->ti)) {
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001776 DMERR("Unable to perform snapshot handover until "
1777 "source is suspended.");
1778 r = -EINVAL;
1779 }
1780 up_read(&snap_src->lock);
1781 }
1782 up_read(&_origins_lock);
1783
1784 return r;
1785}
1786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787static void snapshot_resume(struct dm_target *ti)
1788{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001789 struct dm_snapshot *s = ti->private;
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001790 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
1791
1792 down_read(&_origins_lock);
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +00001793 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001794 if (snap_src && snap_dest) {
1795 down_write(&snap_src->lock);
1796 down_write_nested(&snap_dest->lock, SINGLE_DEPTH_NESTING);
1797 __handover_exceptions(snap_src, snap_dest);
1798 up_write(&snap_dest->lock);
1799 up_write(&snap_src->lock);
1800 }
1801 up_read(&_origins_lock);
1802
1803 /* Now we have correct chunk size, reregister */
1804 reregister_snapshot(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001806 down_write(&s->lock);
1807 s->active = 1;
1808 up_write(&s->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809}
1810
Mike Snitzer542f9032012-07-27 15:08:00 +01001811static uint32_t get_origin_minimum_chunksize(struct block_device *bdev)
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001812{
Mike Snitzer542f9032012-07-27 15:08:00 +01001813 uint32_t min_chunksize;
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001814
1815 down_read(&_origins_lock);
1816 min_chunksize = __minimum_chunk_size(__lookup_origin(bdev));
1817 up_read(&_origins_lock);
1818
1819 return min_chunksize;
1820}
1821
1822static void snapshot_merge_resume(struct dm_target *ti)
1823{
1824 struct dm_snapshot *s = ti->private;
1825
1826 /*
1827 * Handover exceptions from existing snapshot.
1828 */
1829 snapshot_resume(ti);
1830
1831 /*
Mike Snitzer542f9032012-07-27 15:08:00 +01001832 * snapshot-merge acts as an origin, so set ti->max_io_len
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001833 */
Mike Snitzer542f9032012-07-27 15:08:00 +01001834 ti->max_io_len = get_origin_minimum_chunksize(s->origin->bdev);
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001835
1836 start_merge(s);
1837}
1838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839static int snapshot_status(struct dm_target *ti, status_type_t type,
Alasdair G Kergon1f4e0ff2012-07-27 15:08:16 +01001840 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841{
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01001842 unsigned sz = 0;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001843 struct dm_snapshot *snap = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
1845 switch (type) {
1846 case STATUSTYPE_INFO:
Mikulas Patocka94e765722009-12-10 23:51:53 +00001847
1848 down_write(&snap->lock);
1849
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 if (!snap->valid)
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01001851 DMEMIT("Invalid");
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +00001852 else if (snap->merge_failed)
1853 DMEMIT("Merge failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 else {
Mike Snitzer985903b2009-12-10 23:52:11 +00001855 if (snap->store->type->usage) {
1856 sector_t total_sectors, sectors_allocated,
1857 metadata_sectors;
1858 snap->store->type->usage(snap->store,
1859 &total_sectors,
1860 &sectors_allocated,
1861 &metadata_sectors);
1862 DMEMIT("%llu/%llu %llu",
1863 (unsigned long long)sectors_allocated,
1864 (unsigned long long)total_sectors,
1865 (unsigned long long)metadata_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 }
1867 else
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01001868 DMEMIT("Unknown");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 }
Mikulas Patocka94e765722009-12-10 23:51:53 +00001870
1871 up_write(&snap->lock);
1872
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 break;
1874
1875 case STATUSTYPE_TABLE:
1876 /*
1877 * kdevname returns a static pointer so we need
1878 * to make private copies if the output is to
1879 * make sense.
1880 */
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001881 DMEMIT("%s %s", snap->origin->name, snap->cow->name);
Jonathan Brassow1e302a92009-04-02 19:55:35 +01001882 snap->store->type->status(snap->store, type, result + sz,
1883 maxlen - sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 break;
1885 }
1886
1887 return 0;
1888}
1889
Mike Snitzer8811f462009-09-04 20:40:19 +01001890static int snapshot_iterate_devices(struct dm_target *ti,
1891 iterate_devices_callout_fn fn, void *data)
1892{
1893 struct dm_snapshot *snap = ti->private;
Mikulas Patocka1e5554c2010-08-12 04:13:50 +01001894 int r;
Mike Snitzer8811f462009-09-04 20:40:19 +01001895
Mikulas Patocka1e5554c2010-08-12 04:13:50 +01001896 r = fn(ti, snap->origin, 0, ti->len, data);
1897
1898 if (!r)
1899 r = fn(ti, snap->cow, 0, get_dev_size(snap->cow->bdev), data);
1900
1901 return r;
Mike Snitzer8811f462009-09-04 20:40:19 +01001902}
1903
1904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905/*-----------------------------------------------------------------
1906 * Origin methods
1907 *---------------------------------------------------------------*/
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00001908
1909/*
1910 * If no exceptions need creating, DM_MAPIO_REMAPPED is returned and any
1911 * supplied bio was ignored. The caller may submit it immediately.
1912 * (No remapping actually occurs as the origin is always a direct linear
1913 * map.)
1914 *
1915 * If further exceptions are required, DM_MAPIO_SUBMITTED is returned
1916 * and any supplied bio is added to a list to be submitted once all
1917 * the necessary exceptions exist.
1918 */
1919static int __origin_write(struct list_head *snapshots, sector_t sector,
1920 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921{
Mikulas Patocka515ad662009-12-10 23:52:30 +00001922 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 struct dm_snapshot *snap;
Jon Brassow1d4989c2009-12-10 23:52:10 +00001924 struct dm_exception *e;
Mikulas Patocka515ad662009-12-10 23:52:30 +00001925 struct dm_snap_pending_exception *pe;
1926 struct dm_snap_pending_exception *pe_to_start_now = NULL;
1927 struct dm_snap_pending_exception *pe_to_start_last = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 chunk_t chunk;
1929
1930 /* Do all the snapshots on this origin */
1931 list_for_each_entry (snap, snapshots, list) {
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001932 /*
1933 * Don't make new exceptions in a merging snapshot
1934 * because it has effectively been deleted
1935 */
1936 if (dm_target_is_snapshot_merge(snap->ti))
1937 continue;
1938
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001939 down_write(&snap->lock);
1940
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001941 /* Only deal with valid and active snapshots */
1942 if (!snap->valid || !snap->active)
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001943 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Alasdair G Kergond5e404c2005-07-12 15:53:05 -07001945 /* Nothing to do if writing beyond end of snapshot */
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00001946 if (sector >= dm_table_get_size(snap->ti->table))
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001947 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
1949 /*
1950 * Remember, different snapshots can have
1951 * different chunk sizes.
1952 */
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00001953 chunk = sector_to_chunk(snap->store, sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
1955 /*
1956 * Check exception table to see if block
1957 * is already remapped in this snapshot
1958 * and trigger an exception if not.
1959 */
Jon Brassow3510cb92009-12-10 23:52:11 +00001960 e = dm_lookup_exception(&snap->complete, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001961 if (e)
1962 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Mikulas Patocka29138082009-04-02 19:55:25 +01001964 pe = __lookup_pending_exception(snap, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001965 if (!pe) {
Mikulas Patockac6621392009-04-02 19:55:25 +01001966 up_write(&snap->lock);
1967 pe = alloc_pending_exception(snap);
1968 down_write(&snap->lock);
1969
1970 if (!snap->valid) {
1971 free_pending_exception(pe);
1972 goto next_snapshot;
1973 }
1974
Jon Brassow3510cb92009-12-10 23:52:11 +00001975 e = dm_lookup_exception(&snap->complete, chunk);
Mikulas Patocka35bf6592009-04-02 19:55:26 +01001976 if (e) {
1977 free_pending_exception(pe);
1978 goto next_snapshot;
1979 }
1980
Mikulas Patockac6621392009-04-02 19:55:25 +01001981 pe = __find_pending_exception(snap, pe, chunk);
Mikulas Patocka29138082009-04-02 19:55:25 +01001982 if (!pe) {
1983 __invalidate_snapshot(snap, -ENOMEM);
1984 goto next_snapshot;
1985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 }
1987
Mikulas Patocka515ad662009-12-10 23:52:30 +00001988 r = DM_MAPIO_SUBMITTED;
1989
1990 /*
1991 * If an origin bio was supplied, queue it to wait for the
1992 * completion of this exception, and start this one last,
1993 * at the end of the function.
1994 */
1995 if (bio) {
1996 bio_list_add(&pe->origin_bios, bio);
1997 bio = NULL;
1998
1999 if (!pe->started) {
2000 pe->started = 1;
2001 pe_to_start_last = pe;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002002 }
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002003 }
2004
2005 if (!pe->started) {
2006 pe->started = 1;
Mikulas Patocka515ad662009-12-10 23:52:30 +00002007 pe_to_start_now = pe;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002008 }
2009
Jonathan Brassowa2d2b032011-08-02 12:32:03 +01002010next_snapshot:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 up_write(&snap->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Mikulas Patocka515ad662009-12-10 23:52:30 +00002013 if (pe_to_start_now) {
2014 start_copy(pe_to_start_now);
2015 pe_to_start_now = NULL;
2016 }
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08002017 }
2018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 /*
Mikulas Patocka515ad662009-12-10 23:52:30 +00002020 * Submit the exception against which the bio is queued last,
2021 * to give the other exceptions a head start.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 */
Mikulas Patocka515ad662009-12-10 23:52:30 +00002023 if (pe_to_start_last)
2024 start_copy(pe_to_start_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
2026 return r;
2027}
2028
2029/*
2030 * Called on a write from the origin driver.
2031 */
2032static int do_origin(struct dm_dev *origin, struct bio *bio)
2033{
2034 struct origin *o;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08002035 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
2037 down_read(&_origins_lock);
2038 o = __lookup_origin(origin->bdev);
2039 if (o)
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00002040 r = __origin_write(&o->snapshots, bio->bi_sector, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 up_read(&_origins_lock);
2042
2043 return r;
2044}
2045
2046/*
Mikulas Patocka73dfd072009-12-10 23:52:34 +00002047 * Trigger exceptions in all non-merging snapshots.
2048 *
2049 * The chunk size of the merging snapshot may be larger than the chunk
2050 * size of some other snapshot so we may need to reallocate multiple
2051 * chunks in other snapshots.
2052 *
2053 * We scan all the overlapping exceptions in the other snapshots.
2054 * Returns 1 if anything was reallocated and must be waited for,
2055 * otherwise returns 0.
2056 *
2057 * size must be a multiple of merging_snap's chunk_size.
2058 */
2059static int origin_write_extent(struct dm_snapshot *merging_snap,
2060 sector_t sector, unsigned size)
2061{
2062 int must_wait = 0;
2063 sector_t n;
2064 struct origin *o;
2065
2066 /*
Mike Snitzer542f9032012-07-27 15:08:00 +01002067 * The origin's __minimum_chunk_size() got stored in max_io_len
Mikulas Patocka73dfd072009-12-10 23:52:34 +00002068 * by snapshot_merge_resume().
2069 */
2070 down_read(&_origins_lock);
2071 o = __lookup_origin(merging_snap->origin->bdev);
Mike Snitzer542f9032012-07-27 15:08:00 +01002072 for (n = 0; n < size; n += merging_snap->ti->max_io_len)
Mikulas Patocka73dfd072009-12-10 23:52:34 +00002073 if (__origin_write(&o->snapshots, sector + n, NULL) ==
2074 DM_MAPIO_SUBMITTED)
2075 must_wait = 1;
2076 up_read(&_origins_lock);
2077
2078 return must_wait;
2079}
2080
2081/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 * Origin: maps a linear range of a device, with hooks for snapshotting.
2083 */
2084
2085/*
2086 * Construct an origin mapping: <dev_path>
2087 * The context for an origin is merely a 'struct dm_dev *'
2088 * pointing to the real device.
2089 */
2090static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
2091{
2092 int r;
2093 struct dm_dev *dev;
2094
2095 if (argc != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002096 ti->error = "origin: incorrect number of arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 return -EINVAL;
2098 }
2099
Nikanth Karthikesan8215d6e2010-03-06 02:32:27 +00002100 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 if (r) {
2102 ti->error = "Cannot get target device";
2103 return r;
2104 }
2105
2106 ti->private = dev;
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01002107 ti->num_flush_requests = 1;
2108
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 return 0;
2110}
2111
2112static void origin_dtr(struct dm_target *ti)
2113{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002114 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 dm_put_device(ti, dev);
2116}
2117
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00002118static int origin_map(struct dm_target *ti, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002120 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 bio->bi_bdev = dev->bdev;
2122
Tejun Heod87f4c12010-09-03 11:56:19 +02002123 if (bio->bi_rw & REQ_FLUSH)
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01002124 return DM_MAPIO_REMAPPED;
2125
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 /* Only tell snapshots if this is a write */
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08002127 return (bio_rw(bio) == WRITE) ? do_origin(dev, bio) : DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128}
2129
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130/*
Mike Snitzer542f9032012-07-27 15:08:00 +01002131 * Set the target "max_io_len" field to the minimum of all the snapshots'
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 * chunk sizes.
2133 */
2134static void origin_resume(struct dm_target *ti)
2135{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002136 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Mike Snitzer542f9032012-07-27 15:08:00 +01002138 ti->max_io_len = get_origin_minimum_chunksize(dev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139}
2140
Alasdair G Kergon1f4e0ff2012-07-27 15:08:16 +01002141static int origin_status(struct dm_target *ti, status_type_t type,
2142 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002144 struct dm_dev *dev = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
2146 switch (type) {
2147 case STATUSTYPE_INFO:
2148 result[0] = '\0';
2149 break;
2150
2151 case STATUSTYPE_TABLE:
2152 snprintf(result, maxlen, "%s", dev->name);
2153 break;
2154 }
2155
2156 return 0;
2157}
2158
Mikulas Patockab1d55522010-08-12 04:14:02 +01002159static int origin_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
2160 struct bio_vec *biovec, int max_size)
2161{
2162 struct dm_dev *dev = ti->private;
2163 struct request_queue *q = bdev_get_queue(dev->bdev);
2164
2165 if (!q->merge_bvec_fn)
2166 return max_size;
2167
2168 bvm->bi_bdev = dev->bdev;
Mikulas Patockab1d55522010-08-12 04:14:02 +01002169
2170 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
2171}
2172
Mike Snitzer8811f462009-09-04 20:40:19 +01002173static int origin_iterate_devices(struct dm_target *ti,
2174 iterate_devices_callout_fn fn, void *data)
2175{
2176 struct dm_dev *dev = ti->private;
2177
2178 return fn(ti, dev, 0, ti->len, data);
2179}
2180
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181static struct target_type origin_target = {
2182 .name = "snapshot-origin",
Mikulas Patocka42bc9542012-12-21 20:23:38 +00002183 .version = {1, 8, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 .module = THIS_MODULE,
2185 .ctr = origin_ctr,
2186 .dtr = origin_dtr,
2187 .map = origin_map,
2188 .resume = origin_resume,
2189 .status = origin_status,
Mikulas Patockab1d55522010-08-12 04:14:02 +01002190 .merge = origin_merge,
Mike Snitzer8811f462009-09-04 20:40:19 +01002191 .iterate_devices = origin_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192};
2193
2194static struct target_type snapshot_target = {
2195 .name = "snapshot",
Mikulas Patocka42bc9542012-12-21 20:23:38 +00002196 .version = {1, 11, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 .module = THIS_MODULE,
2198 .ctr = snapshot_ctr,
2199 .dtr = snapshot_dtr,
2200 .map = snapshot_map,
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002201 .end_io = snapshot_end_io,
Mike Snitzerc1f0c182009-12-10 23:52:24 +00002202 .preresume = snapshot_preresume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 .resume = snapshot_resume,
2204 .status = snapshot_status,
Mike Snitzer8811f462009-09-04 20:40:19 +01002205 .iterate_devices = snapshot_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206};
2207
Mikulas Patockad698aa42009-12-10 23:52:30 +00002208static struct target_type merge_target = {
2209 .name = dm_snapshot_merge_target_name,
Mikulas Patocka42bc9542012-12-21 20:23:38 +00002210 .version = {1, 2, 0},
Mikulas Patockad698aa42009-12-10 23:52:30 +00002211 .module = THIS_MODULE,
2212 .ctr = snapshot_ctr,
2213 .dtr = snapshot_dtr,
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00002214 .map = snapshot_merge_map,
Mikulas Patockad698aa42009-12-10 23:52:30 +00002215 .end_io = snapshot_end_io,
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002216 .presuspend = snapshot_merge_presuspend,
Mikulas Patockad698aa42009-12-10 23:52:30 +00002217 .preresume = snapshot_preresume,
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002218 .resume = snapshot_merge_resume,
Mikulas Patockad698aa42009-12-10 23:52:30 +00002219 .status = snapshot_status,
2220 .iterate_devices = snapshot_iterate_devices,
2221};
2222
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223static int __init dm_snapshot_init(void)
2224{
2225 int r;
2226
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00002227 r = dm_exception_store_init();
2228 if (r) {
2229 DMERR("Failed to initialize exception stores");
2230 return r;
2231 }
2232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 r = dm_register_target(&snapshot_target);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002234 if (r < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 DMERR("snapshot target register failed %d", r);
Jonathan Brassow034a1862009-10-16 23:18:14 +01002236 goto bad_register_snapshot_target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 }
2238
2239 r = dm_register_target(&origin_target);
2240 if (r < 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002241 DMERR("Origin target register failed %d", r);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002242 goto bad_register_origin_target;
2243 }
2244
2245 r = dm_register_target(&merge_target);
2246 if (r < 0) {
2247 DMERR("Merge target register failed %d", r);
2248 goto bad_register_merge_target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 }
2250
2251 r = init_origin_hash();
2252 if (r) {
2253 DMERR("init_origin_hash failed.");
Mikulas Patockad698aa42009-12-10 23:52:30 +00002254 goto bad_origin_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 }
2256
Jon Brassow1d4989c2009-12-10 23:52:10 +00002257 exception_cache = KMEM_CACHE(dm_exception, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 if (!exception_cache) {
2259 DMERR("Couldn't create exception cache.");
2260 r = -ENOMEM;
Mikulas Patockad698aa42009-12-10 23:52:30 +00002261 goto bad_exception_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 }
2263
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002264 pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 if (!pending_cache) {
2266 DMERR("Couldn't create pending cache.");
2267 r = -ENOMEM;
Mikulas Patockad698aa42009-12-10 23:52:30 +00002268 goto bad_pending_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 }
2270
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 return 0;
2272
Mikulas Patockad698aa42009-12-10 23:52:30 +00002273bad_pending_cache:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 kmem_cache_destroy(exception_cache);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002275bad_exception_cache:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 exit_origin_hash();
Mikulas Patockad698aa42009-12-10 23:52:30 +00002277bad_origin_hash:
2278 dm_unregister_target(&merge_target);
2279bad_register_merge_target:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 dm_unregister_target(&origin_target);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002281bad_register_origin_target:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 dm_unregister_target(&snapshot_target);
Jonathan Brassow034a1862009-10-16 23:18:14 +01002283bad_register_snapshot_target:
2284 dm_exception_store_exit();
Mikulas Patockad698aa42009-12-10 23:52:30 +00002285
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 return r;
2287}
2288
2289static void __exit dm_snapshot_exit(void)
2290{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00002291 dm_unregister_target(&snapshot_target);
2292 dm_unregister_target(&origin_target);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002293 dm_unregister_target(&merge_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
2295 exit_origin_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 kmem_cache_destroy(pending_cache);
2297 kmem_cache_destroy(exception_cache);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00002298
2299 dm_exception_store_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300}
2301
2302/* Module hooks */
2303module_init(dm_snapshot_init);
2304module_exit(dm_snapshot_exit);
2305
2306MODULE_DESCRIPTION(DM_NAME " snapshot target");
2307MODULE_AUTHOR("Joe Thornber");
2308MODULE_LICENSE("GPL");