blob: b502debc6df3f944a37fd8f8fea754a5627e8f75 [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>
Nikos Tsironis8189ee42018-10-31 17:53:08 -040022#include <linux/semaphore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Mikulas Patockab735fed2015-02-26 11:40:35 -050024#include "dm.h"
25
Jonathan Brassowaea53d92009-01-06 03:05:15 +000026#include "dm-exception-store.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Alasdair G Kergon72d94862006-06-26 00:27:35 -070028#define DM_MSG_PREFIX "snapshots"
29
Mikulas Patockad698aa42009-12-10 23:52:30 +000030static const char dm_snapshot_merge_target_name[] = "snapshot-merge";
31
32#define dm_target_is_snapshot_merge(ti) \
33 ((ti)->type->name == dm_snapshot_merge_target_name)
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035/*
Mikulas Patockacd45daf2008-07-21 12:00:32 +010036 * The size of the mempool used to track chunks in use.
37 */
38#define MIN_IOS 256
39
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010040#define DM_TRACKED_CHUNK_HASH_SIZE 16
41#define DM_TRACKED_CHUNK_HASH(x) ((unsigned long)(x) & \
42 (DM_TRACKED_CHUNK_HASH_SIZE - 1))
43
Jon Brassow191437a2009-12-10 23:52:10 +000044struct dm_exception_table {
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010045 uint32_t hash_mask;
46 unsigned hash_shift;
47 struct list_head *table;
48};
49
50struct dm_snapshot {
51 struct rw_semaphore lock;
52
53 struct dm_dev *origin;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +000054 struct dm_dev *cow;
55
56 struct dm_target *ti;
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010057
58 /* List of snapshots per Origin */
59 struct list_head list;
60
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +000061 /*
62 * You can't use a snapshot if this is 0 (e.g. if full).
63 * A snapshot-merge target never clears this.
64 */
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010065 int valid;
66
Mikulas Patocka76c44f62015-06-21 16:31:33 -040067 /*
68 * The snapshot overflowed because of a write to the snapshot device.
69 * We don't have to invalidate the snapshot in this case, but we need
70 * to prevent further writes.
71 */
72 int snapshot_overflowed;
73
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010074 /* Origin writes don't trigger exceptions until this is set */
75 int active;
76
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010077 atomic_t pending_exceptions_count;
78
Mikulas Patocka230c83a2013-11-29 18:13:37 -050079 /* Protected by "lock" */
80 sector_t exception_start_sequence;
81
82 /* Protected by kcopyd single-threaded callback */
83 sector_t exception_complete_sequence;
84
85 /*
86 * A list of pending exceptions that completed out of order.
87 * Protected by kcopyd single-threaded callback.
88 */
89 struct list_head out_of_order_list;
90
Mike Snitzer924e6002010-03-06 02:32:33 +000091 mempool_t *pending_pool;
92
Jon Brassow191437a2009-12-10 23:52:10 +000093 struct dm_exception_table pending;
94 struct dm_exception_table complete;
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010095
96 /*
97 * pe_lock protects all pending_exception operations and access
98 * as well as the snapshot_bios list.
99 */
100 spinlock_t pe_lock;
101
Mike Snitzer924e6002010-03-06 02:32:33 +0000102 /* Chunks with outstanding reads */
103 spinlock_t tracked_chunk_lock;
Mike Snitzer924e6002010-03-06 02:32:33 +0000104 struct hlist_head tracked_chunk_hash[DM_TRACKED_CHUNK_HASH_SIZE];
105
Jonathan Brassowccc45ea2009-04-02 19:55:34 +0100106 /* The on disk metadata handler */
107 struct dm_exception_store *store;
108
Nikos Tsironis8189ee42018-10-31 17:53:08 -0400109 /* Maximum number of in-flight COW jobs. */
110 struct semaphore cow_count;
111
Jonathan Brassowccc45ea2009-04-02 19:55:34 +0100112 struct dm_kcopyd_client *kcopyd_client;
113
Mike Snitzer924e6002010-03-06 02:32:33 +0000114 /* Wait for events based on state_bits */
115 unsigned long state_bits;
116
117 /* Range of chunks currently being merged. */
118 chunk_t first_merging_chunk;
119 int num_merging_chunks;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000120
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +0000121 /*
122 * The merge operation failed if this flag is set.
123 * Failure modes are handled as follows:
124 * - I/O error reading the header
125 * => don't load the target; abort.
126 * - Header does not have "valid" flag set
127 * => use the origin; forget about the snapshot.
128 * - I/O error when reading exceptions
129 * => don't load the target; abort.
130 * (We can't use the intermediate origin state.)
131 * - I/O error while merging
132 * => stop merging; set merge_failed; process I/O normally.
133 */
134 int merge_failed;
135
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000136 /*
137 * Incoming bios that overlap with chunks being merged must wait
138 * for them to be committed.
139 */
140 struct bio_list bios_queued_during_merge;
Jonathan Brassowccc45ea2009-04-02 19:55:34 +0100141};
142
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000143/*
144 * state_bits:
145 * RUNNING_MERGE - Merge operation is in progress.
146 * SHUTDOWN_MERGE - Set to signal that merge needs to be stopped;
147 * cleared afterwards.
148 */
149#define RUNNING_MERGE 0
150#define SHUTDOWN_MERGE 1
151
Nikos Tsironis8189ee42018-10-31 17:53:08 -0400152/*
153 * Maximum number of chunks being copied on write.
154 *
155 * The value was decided experimentally as a trade-off between memory
156 * consumption, stalling the kernel's workqueues and maintaining a high enough
157 * throughput.
158 */
159#define DEFAULT_COW_THRESHOLD 2048
160
161static int cow_threshold = DEFAULT_COW_THRESHOLD;
162module_param_named(snapshot_cow_threshold, cow_threshold, int, 0644);
163MODULE_PARM_DESC(snapshot_cow_threshold, "Maximum number of chunks being copied on write");
164
Mikulas Patockadf5d2e92013-03-01 22:45:49 +0000165DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(snapshot_copy_throttle,
166 "A percentage of time allocated for copy on write");
167
Mikulas Patockac2411042010-08-12 04:13:51 +0100168struct dm_dev *dm_snap_origin(struct dm_snapshot *s)
169{
170 return s->origin;
171}
172EXPORT_SYMBOL(dm_snap_origin);
173
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000174struct dm_dev *dm_snap_cow(struct dm_snapshot *s)
175{
176 return s->cow;
177}
178EXPORT_SYMBOL(dm_snap_cow);
179
Jonathan Brassowccc45ea2009-04-02 19:55:34 +0100180static sector_t chunk_to_sector(struct dm_exception_store *store,
181 chunk_t chunk)
182{
183 return chunk << store->chunk_shift;
184}
185
186static int bdev_equal(struct block_device *lhs, struct block_device *rhs)
187{
188 /*
189 * There is only ever one instance of a particular block
190 * device so we can compare pointers safely.
191 */
192 return lhs == rhs;
193}
194
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100195struct dm_snap_pending_exception {
Jon Brassow1d4989c2009-12-10 23:52:10 +0000196 struct dm_exception e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 /*
199 * Origin buffers waiting for this to complete are held
200 * in a bio list
201 */
202 struct bio_list origin_bios;
203 struct bio_list snapshot_bios;
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 /* Pointer back to snapshot context */
206 struct dm_snapshot *snap;
207
208 /*
209 * 1 indicates the exception has already been sent to
210 * kcopyd.
211 */
212 int started;
Mikulas Patockaa6e50b42011-08-02 12:32:04 +0100213
Mikulas Patocka230c83a2013-11-29 18:13:37 -0500214 /* There was copying error. */
215 int copy_error;
216
217 /* A sequence number, it is used for in-order completion. */
218 sector_t exception_sequence;
219
220 struct list_head out_of_order_entry;
221
Mikulas Patockaa6e50b42011-08-02 12:32:04 +0100222 /*
223 * For writing a complete chunk, bypassing the copy.
224 */
225 struct bio *full_bio;
226 bio_end_io_t *full_bio_end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227};
228
229/*
230 * Hash table mapping origin volumes to lists of snapshots and
231 * a lock to protect it
232 */
Christoph Lametere18b8902006-12-06 20:33:20 -0800233static struct kmem_cache *exception_cache;
234static struct kmem_cache *pending_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100236struct dm_snap_tracked_chunk {
237 struct hlist_node node;
238 chunk_t chunk;
239};
240
Mikulas Patockaee180262012-12-21 20:23:41 +0000241static void init_tracked_chunk(struct bio *bio)
242{
243 struct dm_snap_tracked_chunk *c = dm_per_bio_data(bio, sizeof(struct dm_snap_tracked_chunk));
244 INIT_HLIST_NODE(&c->node);
245}
246
247static bool is_bio_tracked(struct bio *bio)
248{
249 struct dm_snap_tracked_chunk *c = dm_per_bio_data(bio, sizeof(struct dm_snap_tracked_chunk));
250 return !hlist_unhashed(&c->node);
251}
252
253static void track_chunk(struct dm_snapshot *s, struct bio *bio, chunk_t chunk)
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100254{
Mikulas Patocka42bc9542012-12-21 20:23:38 +0000255 struct dm_snap_tracked_chunk *c = dm_per_bio_data(bio, sizeof(struct dm_snap_tracked_chunk));
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100256
257 c->chunk = chunk;
258
Mikulas Patocka9aa0c0e2012-12-21 20:23:33 +0000259 spin_lock_irq(&s->tracked_chunk_lock);
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100260 hlist_add_head(&c->node,
261 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)]);
Mikulas Patocka9aa0c0e2012-12-21 20:23:33 +0000262 spin_unlock_irq(&s->tracked_chunk_lock);
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100263}
264
Mikulas Patockaee180262012-12-21 20:23:41 +0000265static void stop_tracking_chunk(struct dm_snapshot *s, struct bio *bio)
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100266{
Mikulas Patockaee180262012-12-21 20:23:41 +0000267 struct dm_snap_tracked_chunk *c = dm_per_bio_data(bio, sizeof(struct dm_snap_tracked_chunk));
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100268 unsigned long flags;
269
270 spin_lock_irqsave(&s->tracked_chunk_lock, flags);
271 hlist_del(&c->node);
272 spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100273}
274
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100275static int __chunk_is_tracked(struct dm_snapshot *s, chunk_t chunk)
276{
277 struct dm_snap_tracked_chunk *c;
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100278 int found = 0;
279
280 spin_lock_irq(&s->tracked_chunk_lock);
281
Sasha Levinb67bfe02013-02-27 17:06:00 -0800282 hlist_for_each_entry(c,
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100283 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)], node) {
284 if (c->chunk == chunk) {
285 found = 1;
286 break;
287 }
288 }
289
290 spin_unlock_irq(&s->tracked_chunk_lock);
291
292 return found;
293}
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295/*
Mike Snitzer615d1eb2009-12-10 23:52:29 +0000296 * This conflicting I/O is extremely improbable in the caller,
297 * so msleep(1) is sufficient and there is no need for a wait queue.
298 */
299static void __check_for_conflicting_io(struct dm_snapshot *s, chunk_t chunk)
300{
301 while (__chunk_is_tracked(s, chunk))
302 msleep(1);
303}
304
305/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 * One of these per registered origin, held in the snapshot_origins hash
307 */
308struct origin {
309 /* The origin device */
310 struct block_device *bdev;
311
312 struct list_head hash_list;
313
314 /* List of snapshots for this origin */
315 struct list_head snapshots;
316};
317
318/*
Mikulas Patockab735fed2015-02-26 11:40:35 -0500319 * This structure is allocated for each origin target
320 */
321struct dm_origin {
322 struct dm_dev *dev;
323 struct dm_target *ti;
324 unsigned split_boundary;
325 struct list_head hash_list;
326};
327
328/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 * Size of the hash table for origin volumes. If we make this
330 * the size of the minors list then it should be nearly perfect
331 */
332#define ORIGIN_HASH_SIZE 256
333#define ORIGIN_MASK 0xFF
334static struct list_head *_origins;
Mikulas Patockab735fed2015-02-26 11:40:35 -0500335static struct list_head *_dm_origins;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336static struct rw_semaphore _origins_lock;
337
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000338static DECLARE_WAIT_QUEUE_HEAD(_pending_exceptions_done);
339static DEFINE_SPINLOCK(_pending_exceptions_done_spinlock);
340static uint64_t _pending_exceptions_done_count;
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342static int init_origin_hash(void)
343{
344 int i;
345
346 _origins = kmalloc(ORIGIN_HASH_SIZE * sizeof(struct list_head),
347 GFP_KERNEL);
348 if (!_origins) {
Mikulas Patockab735fed2015-02-26 11:40:35 -0500349 DMERR("unable to allocate memory for _origins");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return -ENOMEM;
351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 for (i = 0; i < ORIGIN_HASH_SIZE; i++)
353 INIT_LIST_HEAD(_origins + i);
Mikulas Patockab735fed2015-02-26 11:40:35 -0500354
355 _dm_origins = kmalloc(ORIGIN_HASH_SIZE * sizeof(struct list_head),
356 GFP_KERNEL);
357 if (!_dm_origins) {
358 DMERR("unable to allocate memory for _dm_origins");
359 kfree(_origins);
360 return -ENOMEM;
361 }
362 for (i = 0; i < ORIGIN_HASH_SIZE; i++)
363 INIT_LIST_HEAD(_dm_origins + i);
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 init_rwsem(&_origins_lock);
366
367 return 0;
368}
369
370static void exit_origin_hash(void)
371{
372 kfree(_origins);
Mikulas Patockab735fed2015-02-26 11:40:35 -0500373 kfree(_dm_origins);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100376static unsigned origin_hash(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
378 return bdev->bd_dev & ORIGIN_MASK;
379}
380
381static struct origin *__lookup_origin(struct block_device *origin)
382{
383 struct list_head *ol;
384 struct origin *o;
385
386 ol = &_origins[origin_hash(origin)];
387 list_for_each_entry (o, ol, hash_list)
388 if (bdev_equal(o->bdev, origin))
389 return o;
390
391 return NULL;
392}
393
394static void __insert_origin(struct origin *o)
395{
396 struct list_head *sl = &_origins[origin_hash(o->bdev)];
397 list_add_tail(&o->hash_list, sl);
398}
399
Mikulas Patockab735fed2015-02-26 11:40:35 -0500400static struct dm_origin *__lookup_dm_origin(struct block_device *origin)
401{
402 struct list_head *ol;
403 struct dm_origin *o;
404
405 ol = &_dm_origins[origin_hash(origin)];
406 list_for_each_entry (o, ol, hash_list)
407 if (bdev_equal(o->dev->bdev, origin))
408 return o;
409
410 return NULL;
411}
412
413static void __insert_dm_origin(struct dm_origin *o)
414{
415 struct list_head *sl = &_dm_origins[origin_hash(o->dev->bdev)];
416 list_add_tail(&o->hash_list, sl);
417}
418
419static void __remove_dm_origin(struct dm_origin *o)
420{
421 list_del(&o->hash_list);
422}
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424/*
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000425 * _origins_lock must be held when calling this function.
426 * Returns number of snapshots registered using the supplied cow device, plus:
427 * snap_src - a snapshot suitable for use as a source of exception handover
428 * snap_dest - a snapshot capable of receiving exception handover.
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000429 * snap_merge - an existing snapshot-merge target linked to the same origin.
430 * There can be at most one snapshot-merge target. The parameter is optional.
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000431 *
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000432 * Possible return values and states of snap_src and snap_dest.
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000433 * 0: NULL, NULL - first new snapshot
434 * 1: snap_src, NULL - normal snapshot
435 * 2: snap_src, snap_dest - waiting for handover
436 * 2: snap_src, NULL - handed over, waiting for old to be deleted
437 * 1: NULL, snap_dest - source got destroyed without handover
438 */
439static int __find_snapshots_sharing_cow(struct dm_snapshot *snap,
440 struct dm_snapshot **snap_src,
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000441 struct dm_snapshot **snap_dest,
442 struct dm_snapshot **snap_merge)
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000443{
444 struct dm_snapshot *s;
445 struct origin *o;
446 int count = 0;
447 int active;
448
449 o = __lookup_origin(snap->origin->bdev);
450 if (!o)
451 goto out;
452
453 list_for_each_entry(s, &o->snapshots, list) {
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000454 if (dm_target_is_snapshot_merge(s->ti) && snap_merge)
455 *snap_merge = s;
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000456 if (!bdev_equal(s->cow->bdev, snap->cow->bdev))
457 continue;
458
459 down_read(&s->lock);
460 active = s->active;
461 up_read(&s->lock);
462
463 if (active) {
464 if (snap_src)
465 *snap_src = s;
466 } else if (snap_dest)
467 *snap_dest = s;
468
469 count++;
470 }
471
472out:
473 return count;
474}
475
476/*
477 * On success, returns 1 if this snapshot is a handover destination,
478 * otherwise returns 0.
479 */
480static int __validate_exception_handover(struct dm_snapshot *snap)
481{
482 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000483 struct dm_snapshot *snap_merge = NULL;
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000484
485 /* Does snapshot need exceptions handed over to it? */
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000486 if ((__find_snapshots_sharing_cow(snap, &snap_src, &snap_dest,
487 &snap_merge) == 2) ||
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000488 snap_dest) {
489 snap->ti->error = "Snapshot cow pairing for exception "
490 "table handover failed";
491 return -EINVAL;
492 }
493
494 /*
495 * If no snap_src was found, snap cannot become a handover
496 * destination.
497 */
498 if (!snap_src)
499 return 0;
500
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000501 /*
502 * Non-snapshot-merge handover?
503 */
504 if (!dm_target_is_snapshot_merge(snap->ti))
505 return 1;
506
507 /*
508 * Do not allow more than one merging snapshot.
509 */
510 if (snap_merge) {
511 snap->ti->error = "A snapshot is already merging.";
512 return -EINVAL;
513 }
514
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000515 if (!snap_src->store->type->prepare_merge ||
516 !snap_src->store->type->commit_merge) {
517 snap->ti->error = "Snapshot exception store does not "
518 "support snapshot-merge.";
519 return -EINVAL;
520 }
521
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000522 return 1;
523}
524
525static void __insert_snapshot(struct origin *o, struct dm_snapshot *s)
526{
527 struct dm_snapshot *l;
528
529 /* Sort the list according to chunk size, largest-first smallest-last */
530 list_for_each_entry(l, &o->snapshots, list)
531 if (l->store->chunk_size < s->store->chunk_size)
532 break;
533 list_add_tail(&s->list, &l->list);
534}
535
536/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 * Make a note of the snapshot and its origin so we can look it
538 * up when the origin has a write on it.
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000539 *
540 * Also validate snapshot exception store handovers.
541 * On success, returns 1 if this registration is a handover destination,
542 * otherwise returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 */
544static int register_snapshot(struct dm_snapshot *snap)
545{
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000546 struct origin *o, *new_o = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 struct block_device *bdev = snap->origin->bdev;
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000548 int r = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000550 new_o = kmalloc(sizeof(*new_o), GFP_KERNEL);
551 if (!new_o)
552 return -ENOMEM;
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 down_write(&_origins_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000556 r = __validate_exception_handover(snap);
557 if (r < 0) {
558 kfree(new_o);
559 goto out;
560 }
561
562 o = __lookup_origin(bdev);
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000563 if (o)
564 kfree(new_o);
565 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 /* New origin */
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000567 o = new_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 /* Initialise the struct */
570 INIT_LIST_HEAD(&o->snapshots);
571 o->bdev = bdev;
572
573 __insert_origin(o);
574 }
575
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000576 __insert_snapshot(o, snap);
577
578out:
579 up_write(&_origins_lock);
580
581 return r;
582}
583
584/*
585 * Move snapshot to correct place in list according to chunk size.
586 */
587static void reregister_snapshot(struct dm_snapshot *s)
588{
589 struct block_device *bdev = s->origin->bdev;
590
591 down_write(&_origins_lock);
592
593 list_del(&s->list);
594 __insert_snapshot(__lookup_origin(bdev), s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 up_write(&_origins_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
599static void unregister_snapshot(struct dm_snapshot *s)
600{
601 struct origin *o;
602
603 down_write(&_origins_lock);
604 o = __lookup_origin(s->origin->bdev);
605
606 list_del(&s->list);
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000607 if (o && list_empty(&o->snapshots)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 list_del(&o->hash_list);
609 kfree(o);
610 }
611
612 up_write(&_origins_lock);
613}
614
615/*
616 * Implementation of the exception hash tables.
Milan Brozd74f81f2008-02-08 02:11:27 +0000617 * The lowest hash_shift bits of the chunk number are ignored, allowing
618 * some consecutive chunks to be grouped together.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 */
Jon Brassow3510cb92009-12-10 23:52:11 +0000620static int dm_exception_table_init(struct dm_exception_table *et,
621 uint32_t size, unsigned hash_shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
623 unsigned int i;
624
Milan Brozd74f81f2008-02-08 02:11:27 +0000625 et->hash_shift = hash_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 et->hash_mask = size - 1;
627 et->table = dm_vcalloc(size, sizeof(struct list_head));
628 if (!et->table)
629 return -ENOMEM;
630
631 for (i = 0; i < size; i++)
632 INIT_LIST_HEAD(et->table + i);
633
634 return 0;
635}
636
Jon Brassow3510cb92009-12-10 23:52:11 +0000637static void dm_exception_table_exit(struct dm_exception_table *et,
638 struct kmem_cache *mem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 struct list_head *slot;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000641 struct dm_exception *ex, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 int i, size;
643
644 size = et->hash_mask + 1;
645 for (i = 0; i < size; i++) {
646 slot = et->table + i;
647
648 list_for_each_entry_safe (ex, next, slot, hash_list)
649 kmem_cache_free(mem, ex);
650 }
651
652 vfree(et->table);
653}
654
Jon Brassow191437a2009-12-10 23:52:10 +0000655static uint32_t exception_hash(struct dm_exception_table *et, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Milan Brozd74f81f2008-02-08 02:11:27 +0000657 return (chunk >> et->hash_shift) & et->hash_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
Jon Brassow3510cb92009-12-10 23:52:11 +0000660static void dm_remove_exception(struct dm_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
662 list_del(&e->hash_list);
663}
664
665/*
666 * Return the exception data for a sector, or NULL if not
667 * remapped.
668 */
Jon Brassow3510cb92009-12-10 23:52:11 +0000669static struct dm_exception *dm_lookup_exception(struct dm_exception_table *et,
670 chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 struct list_head *slot;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000673 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 slot = &et->table[exception_hash(et, chunk)];
676 list_for_each_entry (e, slot, hash_list)
Milan Brozd74f81f2008-02-08 02:11:27 +0000677 if (chunk >= e->old_chunk &&
678 chunk <= e->old_chunk + dm_consecutive_chunk_count(e))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 return e;
680
681 return NULL;
682}
683
Mikulas Patocka119bc542014-01-13 19:13:36 -0500684static struct dm_exception *alloc_completed_exception(gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Jon Brassow1d4989c2009-12-10 23:52:10 +0000686 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Mikulas Patocka119bc542014-01-13 19:13:36 -0500688 e = kmem_cache_alloc(exception_cache, gfp);
689 if (!e && gfp == GFP_NOIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 e = kmem_cache_alloc(exception_cache, GFP_ATOMIC);
691
692 return e;
693}
694
Jon Brassow3510cb92009-12-10 23:52:11 +0000695static void free_completed_exception(struct dm_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
697 kmem_cache_free(exception_cache, e);
698}
699
Mikulas Patocka92e86812008-07-21 12:00:35 +0100700static struct dm_snap_pending_exception *alloc_pending_exception(struct dm_snapshot *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Mikulas Patocka92e86812008-07-21 12:00:35 +0100702 struct dm_snap_pending_exception *pe = mempool_alloc(s->pending_pool,
703 GFP_NOIO);
704
Mikulas Patocka879129d22008-10-30 13:33:16 +0000705 atomic_inc(&s->pending_exceptions_count);
Mikulas Patocka92e86812008-07-21 12:00:35 +0100706 pe->snap = s;
707
708 return pe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100711static void free_pending_exception(struct dm_snap_pending_exception *pe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Mikulas Patocka879129d22008-10-30 13:33:16 +0000713 struct dm_snapshot *s = pe->snap;
714
715 mempool_free(pe, s->pending_pool);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100716 smp_mb__before_atomic();
Mikulas Patocka879129d22008-10-30 13:33:16 +0000717 atomic_dec(&s->pending_exceptions_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
719
Jon Brassow3510cb92009-12-10 23:52:11 +0000720static void dm_insert_exception(struct dm_exception_table *eh,
721 struct dm_exception *new_e)
Milan Brozd74f81f2008-02-08 02:11:27 +0000722{
Milan Brozd74f81f2008-02-08 02:11:27 +0000723 struct list_head *l;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000724 struct dm_exception *e = NULL;
Milan Brozd74f81f2008-02-08 02:11:27 +0000725
726 l = &eh->table[exception_hash(eh, new_e->old_chunk)];
727
728 /* Add immediately if this table doesn't support consecutive chunks */
729 if (!eh->hash_shift)
730 goto out;
731
732 /* List is ordered by old_chunk */
733 list_for_each_entry_reverse(e, l, hash_list) {
734 /* Insert after an existing chunk? */
735 if (new_e->old_chunk == (e->old_chunk +
736 dm_consecutive_chunk_count(e) + 1) &&
737 new_e->new_chunk == (dm_chunk_number(e->new_chunk) +
738 dm_consecutive_chunk_count(e) + 1)) {
739 dm_consecutive_chunk_count_inc(e);
Jon Brassow3510cb92009-12-10 23:52:11 +0000740 free_completed_exception(new_e);
Milan Brozd74f81f2008-02-08 02:11:27 +0000741 return;
742 }
743
744 /* Insert before an existing chunk? */
745 if (new_e->old_chunk == (e->old_chunk - 1) &&
746 new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) {
747 dm_consecutive_chunk_count_inc(e);
748 e->old_chunk--;
749 e->new_chunk--;
Jon Brassow3510cb92009-12-10 23:52:11 +0000750 free_completed_exception(new_e);
Milan Brozd74f81f2008-02-08 02:11:27 +0000751 return;
752 }
753
754 if (new_e->old_chunk > e->old_chunk)
755 break;
756 }
757
758out:
759 list_add(&new_e->hash_list, e ? &e->hash_list : l);
760}
761
Jonathan Brassowa159c1a2009-01-06 03:05:19 +0000762/*
763 * Callback used by the exception stores to load exceptions when
764 * initialising.
765 */
766static int dm_add_exception(void *context, chunk_t old, chunk_t new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Jonathan Brassowa159c1a2009-01-06 03:05:19 +0000768 struct dm_snapshot *s = context;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000769 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Mikulas Patocka119bc542014-01-13 19:13:36 -0500771 e = alloc_completed_exception(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (!e)
773 return -ENOMEM;
774
775 e->old_chunk = old;
Milan Brozd74f81f2008-02-08 02:11:27 +0000776
777 /* Consecutive_count is implicitly initialised to zero */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 e->new_chunk = new;
Milan Brozd74f81f2008-02-08 02:11:27 +0000779
Jon Brassow3510cb92009-12-10 23:52:11 +0000780 dm_insert_exception(&s->complete, e);
Milan Brozd74f81f2008-02-08 02:11:27 +0000781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return 0;
783}
784
Mikulas Patocka7e201b32009-12-10 23:52:08 +0000785/*
786 * Return a minimum chunk size of all snapshots that have the specified origin.
787 * Return zero if the origin has no snapshots.
788 */
Mike Snitzer542f9032012-07-27 15:08:00 +0100789static uint32_t __minimum_chunk_size(struct origin *o)
Mikulas Patocka7e201b32009-12-10 23:52:08 +0000790{
791 struct dm_snapshot *snap;
792 unsigned chunk_size = 0;
793
794 if (o)
795 list_for_each_entry(snap, &o->snapshots, list)
796 chunk_size = min_not_zero(chunk_size,
797 snap->store->chunk_size);
798
Mike Snitzer542f9032012-07-27 15:08:00 +0100799 return (uint32_t) chunk_size;
Mikulas Patocka7e201b32009-12-10 23:52:08 +0000800}
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802/*
803 * Hard coded magic.
804 */
805static int calc_max_buckets(void)
806{
807 /* use a fixed size of 2MB */
808 unsigned long mem = 2 * 1024 * 1024;
809 mem /= sizeof(struct list_head);
810
811 return mem;
812}
813
814/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 * Allocate room for a suitable hash table.
816 */
Jonathan Brassowfee19982009-04-02 19:55:34 +0100817static int init_hash_tables(struct dm_snapshot *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Mikulas Patocka60e356f2013-09-18 19:40:42 -0400819 sector_t hash_size, cow_dev_size, max_buckets;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 /*
822 * Calculate based on the size of the original volume or
823 * the COW volume...
824 */
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000825 cow_dev_size = get_dev_size(s->cow->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 max_buckets = calc_max_buckets();
827
Mikulas Patocka60e356f2013-09-18 19:40:42 -0400828 hash_size = cow_dev_size >> s->store->chunk_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 hash_size = min(hash_size, max_buckets);
830
Mikulas Patocka8e87b9b2009-12-10 23:51:54 +0000831 if (hash_size < 64)
832 hash_size = 64;
Robert P. J. Day8defd832008-02-08 02:10:06 +0000833 hash_size = rounddown_pow_of_two(hash_size);
Jon Brassow3510cb92009-12-10 23:52:11 +0000834 if (dm_exception_table_init(&s->complete, hash_size,
835 DM_CHUNK_CONSECUTIVE_BITS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return -ENOMEM;
837
838 /*
839 * Allocate hash table for in-flight exceptions
840 * Make this smaller than the real hash table
841 */
842 hash_size >>= 3;
843 if (hash_size < 64)
844 hash_size = 64;
845
Jon Brassow3510cb92009-12-10 23:52:11 +0000846 if (dm_exception_table_init(&s->pending, hash_size, 0)) {
847 dm_exception_table_exit(&s->complete, exception_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return -ENOMEM;
849 }
850
851 return 0;
852}
853
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000854static void merge_shutdown(struct dm_snapshot *s)
855{
856 clear_bit_unlock(RUNNING_MERGE, &s->state_bits);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100857 smp_mb__after_atomic();
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000858 wake_up_bit(&s->state_bits, RUNNING_MERGE);
859}
860
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000861static struct bio *__release_queued_bios_after_merge(struct dm_snapshot *s)
862{
863 s->first_merging_chunk = 0;
864 s->num_merging_chunks = 0;
865
866 return bio_list_get(&s->bios_queued_during_merge);
867}
868
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000869/*
870 * Remove one chunk from the index of completed exceptions.
871 */
872static int __remove_single_exception_chunk(struct dm_snapshot *s,
873 chunk_t old_chunk)
874{
875 struct dm_exception *e;
876
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000877 e = dm_lookup_exception(&s->complete, old_chunk);
878 if (!e) {
879 DMERR("Corruption detected: exception for block %llu is "
880 "on disk but not in memory",
881 (unsigned long long)old_chunk);
882 return -EINVAL;
883 }
884
885 /*
886 * If this is the only chunk using this exception, remove exception.
887 */
888 if (!dm_consecutive_chunk_count(e)) {
889 dm_remove_exception(e);
890 free_completed_exception(e);
891 return 0;
892 }
893
894 /*
895 * The chunk may be either at the beginning or the end of a
896 * group of consecutive chunks - never in the middle. We are
897 * removing chunks in the opposite order to that in which they
898 * were added, so this should always be true.
899 * Decrement the consecutive chunk counter and adjust the
900 * starting point if necessary.
901 */
902 if (old_chunk == e->old_chunk) {
903 e->old_chunk++;
904 e->new_chunk++;
905 } else if (old_chunk != e->old_chunk +
906 dm_consecutive_chunk_count(e)) {
907 DMERR("Attempt to merge block %llu from the "
908 "middle of a chunk range [%llu - %llu]",
909 (unsigned long long)old_chunk,
910 (unsigned long long)e->old_chunk,
911 (unsigned long long)
912 e->old_chunk + dm_consecutive_chunk_count(e));
913 return -EINVAL;
914 }
915
916 dm_consecutive_chunk_count_dec(e);
917
918 return 0;
919}
920
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000921static void flush_bios(struct bio *bio);
922
923static int remove_single_exception_chunk(struct dm_snapshot *s)
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000924{
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000925 struct bio *b = NULL;
926 int r;
927 chunk_t old_chunk = s->first_merging_chunk + s->num_merging_chunks - 1;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000928
929 down_write(&s->lock);
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000930
931 /*
932 * Process chunks (and associated exceptions) in reverse order
933 * so that dm_consecutive_chunk_count_dec() accounting works.
934 */
935 do {
936 r = __remove_single_exception_chunk(s, old_chunk);
937 if (r)
938 goto out;
939 } while (old_chunk-- > s->first_merging_chunk);
940
941 b = __release_queued_bios_after_merge(s);
942
943out:
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000944 up_write(&s->lock);
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000945 if (b)
946 flush_bios(b);
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000947
948 return r;
949}
950
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000951static int origin_write_extent(struct dm_snapshot *merging_snap,
952 sector_t sector, unsigned chunk_size);
953
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000954static void merge_callback(int read_err, unsigned long write_err,
955 void *context);
956
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000957static uint64_t read_pending_exceptions_done_count(void)
958{
959 uint64_t pending_exceptions_done;
960
961 spin_lock(&_pending_exceptions_done_spinlock);
962 pending_exceptions_done = _pending_exceptions_done_count;
963 spin_unlock(&_pending_exceptions_done_spinlock);
964
965 return pending_exceptions_done;
966}
967
968static void increment_pending_exceptions_done_count(void)
969{
970 spin_lock(&_pending_exceptions_done_spinlock);
971 _pending_exceptions_done_count++;
972 spin_unlock(&_pending_exceptions_done_spinlock);
973
974 wake_up_all(&_pending_exceptions_done);
975}
976
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000977static void snapshot_merge_next_chunks(struct dm_snapshot *s)
978{
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000979 int i, linear_chunks;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000980 chunk_t old_chunk, new_chunk;
981 struct dm_io_region src, dest;
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000982 sector_t io_size;
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000983 uint64_t previous_count;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000984
985 BUG_ON(!test_bit(RUNNING_MERGE, &s->state_bits));
986 if (unlikely(test_bit(SHUTDOWN_MERGE, &s->state_bits)))
987 goto shut;
988
989 /*
990 * valid flag never changes during merge, so no lock required.
991 */
992 if (!s->valid) {
993 DMERR("Snapshot is invalid: can't merge");
994 goto shut;
995 }
996
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000997 linear_chunks = s->store->type->prepare_merge(s->store, &old_chunk,
998 &new_chunk);
999 if (linear_chunks <= 0) {
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +00001000 if (linear_chunks < 0) {
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001001 DMERR("Read error in exception store: "
1002 "shutting down merge");
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +00001003 down_write(&s->lock);
1004 s->merge_failed = 1;
1005 up_write(&s->lock);
1006 }
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001007 goto shut;
1008 }
1009
Mike Snitzer8a2d5282009-12-10 23:52:34 +00001010 /* Adjust old_chunk and new_chunk to reflect start of linear region */
1011 old_chunk = old_chunk + 1 - linear_chunks;
1012 new_chunk = new_chunk + 1 - linear_chunks;
1013
1014 /*
1015 * Use one (potentially large) I/O to copy all 'linear_chunks'
1016 * from the exception store to the origin
1017 */
1018 io_size = linear_chunks * s->store->chunk_size;
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001019
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001020 dest.bdev = s->origin->bdev;
1021 dest.sector = chunk_to_sector(s->store, old_chunk);
Mike Snitzer8a2d5282009-12-10 23:52:34 +00001022 dest.count = min(io_size, get_dev_size(dest.bdev) - dest.sector);
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001023
1024 src.bdev = s->cow->bdev;
1025 src.sector = chunk_to_sector(s->store, new_chunk);
1026 src.count = dest.count;
1027
Mikulas Patocka73dfd072009-12-10 23:52:34 +00001028 /*
1029 * Reallocate any exceptions needed in other snapshots then
1030 * wait for the pending exceptions to complete.
1031 * Each time any pending exception (globally on the system)
1032 * completes we are woken and repeat the process to find out
1033 * if we can proceed. While this may not seem a particularly
1034 * efficient algorithm, it is not expected to have any
1035 * significant impact on performance.
1036 */
1037 previous_count = read_pending_exceptions_done_count();
Mike Snitzer8a2d5282009-12-10 23:52:34 +00001038 while (origin_write_extent(s, dest.sector, io_size)) {
Mikulas Patocka73dfd072009-12-10 23:52:34 +00001039 wait_event(_pending_exceptions_done,
1040 (read_pending_exceptions_done_count() !=
1041 previous_count));
1042 /* Retry after the wait, until all exceptions are done. */
1043 previous_count = read_pending_exceptions_done_count();
1044 }
1045
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001046 down_write(&s->lock);
1047 s->first_merging_chunk = old_chunk;
Mike Snitzer8a2d5282009-12-10 23:52:34 +00001048 s->num_merging_chunks = linear_chunks;
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001049 up_write(&s->lock);
1050
Mike Snitzer8a2d5282009-12-10 23:52:34 +00001051 /* Wait until writes to all 'linear_chunks' drain */
1052 for (i = 0; i < linear_chunks; i++)
1053 __check_for_conflicting_io(s, old_chunk + i);
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001054
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001055 dm_kcopyd_copy(s->kcopyd_client, &src, 1, &dest, 0, merge_callback, s);
1056 return;
1057
1058shut:
1059 merge_shutdown(s);
1060}
1061
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001062static void error_bios(struct bio *bio);
1063
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001064static void merge_callback(int read_err, unsigned long write_err, void *context)
1065{
1066 struct dm_snapshot *s = context;
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001067 struct bio *b = NULL;
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001068
1069 if (read_err || write_err) {
1070 if (read_err)
1071 DMERR("Read error: shutting down merge.");
1072 else
1073 DMERR("Write error: shutting down merge.");
1074 goto shut;
1075 }
1076
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001077 if (s->store->type->commit_merge(s->store,
1078 s->num_merging_chunks) < 0) {
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001079 DMERR("Write error in exception store: shutting down merge");
1080 goto shut;
1081 }
1082
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001083 if (remove_single_exception_chunk(s) < 0)
1084 goto shut;
1085
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001086 snapshot_merge_next_chunks(s);
1087
1088 return;
1089
1090shut:
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001091 down_write(&s->lock);
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +00001092 s->merge_failed = 1;
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001093 b = __release_queued_bios_after_merge(s);
1094 up_write(&s->lock);
1095 error_bios(b);
1096
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001097 merge_shutdown(s);
1098}
1099
1100static void start_merge(struct dm_snapshot *s)
1101{
1102 if (!test_and_set_bit(RUNNING_MERGE, &s->state_bits))
1103 snapshot_merge_next_chunks(s);
1104}
1105
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001106/*
1107 * Stop the merging process and wait until it finishes.
1108 */
1109static void stop_merge(struct dm_snapshot *s)
1110{
1111 set_bit(SHUTDOWN_MERGE, &s->state_bits);
NeilBrown74316202014-07-07 15:16:04 +10001112 wait_on_bit(&s->state_bits, RUNNING_MERGE, TASK_UNINTERRUPTIBLE);
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001113 clear_bit(SHUTDOWN_MERGE, &s->state_bits);
1114}
1115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116/*
Mike Snitzerb0d3cc02015-10-08 18:05:41 -04001117 * Construct a snapshot mapping: <origin_dev> <COW-dev> <p|po|n> <chunk-size>
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 */
1119static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1120{
1121 struct dm_snapshot *s;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001122 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 int r = -EINVAL;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001124 char *origin_path, *cow_path;
DingXiang4df2bf42016-02-02 12:29:18 +08001125 dev_t origin_dev, cow_dev;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001126 unsigned args_used, num_flush_bios = 1;
Mike Snitzer10b81062009-12-10 23:52:31 +00001127 fmode_t origin_mode = FMODE_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -07001129 if (argc != 4) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001130 ti->error = "requires exactly 4 arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 r = -EINVAL;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001132 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 }
1134
Mike Snitzer10b81062009-12-10 23:52:31 +00001135 if (dm_target_is_snapshot_merge(ti)) {
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001136 num_flush_bios = 2;
Mike Snitzer10b81062009-12-10 23:52:31 +00001137 origin_mode = FMODE_WRITE;
1138 }
1139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 s = kmalloc(sizeof(*s), GFP_KERNEL);
Jonathan Brassowfee19982009-04-02 19:55:34 +01001141 if (!s) {
Jonathan Brassowa2d2b032011-08-02 12:32:03 +01001142 ti->error = "Cannot allocate private snapshot structure";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 r = -ENOMEM;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001144 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 }
1146
Mikulas Patockac2411042010-08-12 04:13:51 +01001147 origin_path = argv[0];
1148 argv++;
1149 argc--;
1150
1151 r = dm_get_device(ti, origin_path, origin_mode, &s->origin);
1152 if (r) {
1153 ti->error = "Cannot get origin device";
1154 goto bad_origin;
1155 }
DingXiang4df2bf42016-02-02 12:29:18 +08001156 origin_dev = s->origin->bdev->bd_dev;
Mikulas Patockac2411042010-08-12 04:13:51 +01001157
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001158 cow_path = argv[0];
1159 argv++;
1160 argc--;
1161
DingXiang4df2bf42016-02-02 12:29:18 +08001162 cow_dev = dm_get_dev_t(cow_path);
1163 if (cow_dev && cow_dev == origin_dev) {
1164 ti->error = "COW device cannot be the same as origin device";
1165 r = -EINVAL;
1166 goto bad_cow;
1167 }
1168
Milan Broz024d37e2011-03-24 13:52:14 +00001169 r = dm_get_device(ti, cow_path, dm_table_get_mode(ti->table), &s->cow);
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001170 if (r) {
1171 ti->error = "Cannot get COW device";
1172 goto bad_cow;
1173 }
1174
1175 r = dm_exception_store_create(ti, argc, argv, s, &args_used, &s->store);
1176 if (r) {
1177 ti->error = "Couldn't create exception store";
1178 r = -EINVAL;
1179 goto bad_store;
1180 }
1181
1182 argv += args_used;
1183 argc -= args_used;
1184
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001185 s->ti = ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 s->valid = 1;
Mikulas Patocka76c44f62015-06-21 16:31:33 -04001187 s->snapshot_overflowed = 0;
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001188 s->active = 0;
Mikulas Patocka879129d22008-10-30 13:33:16 +00001189 atomic_set(&s->pending_exceptions_count, 0);
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001190 s->exception_start_sequence = 0;
1191 s->exception_complete_sequence = 0;
1192 INIT_LIST_HEAD(&s->out_of_order_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 init_rwsem(&s->lock);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001194 INIT_LIST_HEAD(&s->list);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001195 spin_lock_init(&s->pe_lock);
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001196 s->state_bits = 0;
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +00001197 s->merge_failed = 0;
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001198 s->first_merging_chunk = 0;
1199 s->num_merging_chunks = 0;
1200 bio_list_init(&s->bios_queued_during_merge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 /* Allocate hash table for COW data */
Jonathan Brassowfee19982009-04-02 19:55:34 +01001203 if (init_hash_tables(s)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 ti->error = "Unable to allocate hash table space";
1205 r = -ENOMEM;
Jonathan Brassowfee19982009-04-02 19:55:34 +01001206 goto bad_hash_tables;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 }
1208
Nikos Tsironis8189ee42018-10-31 17:53:08 -04001209 sema_init(&s->cow_count, (cow_threshold > 0) ? cow_threshold : INT_MAX);
1210
Mikulas Patockadf5d2e92013-03-01 22:45:49 +00001211 s->kcopyd_client = dm_kcopyd_client_create(&dm_kcopyd_throttle);
Mikulas Patockafa34ce72011-05-29 13:03:13 +01001212 if (IS_ERR(s->kcopyd_client)) {
1213 r = PTR_ERR(s->kcopyd_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 ti->error = "Could not create kcopyd client";
Jonathan Brassowfee19982009-04-02 19:55:34 +01001215 goto bad_kcopyd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
1217
Mikulas Patocka92e86812008-07-21 12:00:35 +01001218 s->pending_pool = mempool_create_slab_pool(MIN_IOS, pending_cache);
1219 if (!s->pending_pool) {
1220 ti->error = "Could not allocate mempool for pending exceptions";
Wei Yongjun09e8b812013-05-10 14:37:15 +01001221 r = -ENOMEM;
Jonathan Brassowfee19982009-04-02 19:55:34 +01001222 goto bad_pending_pool;
Mikulas Patocka92e86812008-07-21 12:00:35 +01001223 }
1224
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001225 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
1226 INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]);
1227
1228 spin_lock_init(&s->tracked_chunk_lock);
1229
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001230 ti->private = s;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001231 ti->num_flush_bios = num_flush_bios;
Mike Snitzer30187e12016-01-31 13:28:26 -05001232 ti->per_io_data_size = sizeof(struct dm_snap_tracked_chunk);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001233
1234 /* Add snapshot to the list of snapshots for this origin */
1235 /* Exceptions aren't triggered till snapshot_resume() is called */
1236 r = register_snapshot(s);
1237 if (r == -ENOMEM) {
1238 ti->error = "Snapshot origin struct allocation failed";
1239 goto bad_load_and_register;
1240 } else if (r < 0) {
1241 /* invalid handover, register_snapshot has set ti->error */
1242 goto bad_load_and_register;
1243 }
1244
1245 /*
1246 * Metadata must only be loaded into one table at once, so skip this
1247 * if metadata will be handed over during resume.
1248 * Chunk size will be set during the handover - set it to zero to
1249 * ensure it's ignored.
1250 */
1251 if (r > 0) {
1252 s->store->chunk_size = 0;
1253 return 0;
1254 }
1255
Jonathan Brassow493df712009-04-02 19:55:31 +01001256 r = s->store->type->read_metadata(s->store, dm_add_exception,
1257 (void *)s);
Milan Broz07641472007-07-12 17:28:13 +01001258 if (r < 0) {
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -07001259 ti->error = "Failed to read snapshot metadata";
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001260 goto bad_read_metadata;
Milan Broz07641472007-07-12 17:28:13 +01001261 } else if (r > 0) {
1262 s->valid = 0;
1263 DMWARN("Snapshot is marked invalid.");
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -07001264 }
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001265
Mikulas Patocka3f2412d2009-10-16 23:18:16 +01001266 if (!s->store->chunk_size) {
1267 ti->error = "Chunk size not set";
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001268 goto bad_read_metadata;
Mikulas Patocka3f2412d2009-10-16 23:18:16 +01001269 }
Mike Snitzer542f9032012-07-27 15:08:00 +01001270
1271 r = dm_set_target_max_io_len(ti, s->store->chunk_size);
1272 if (r)
1273 goto bad_read_metadata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 return 0;
1276
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001277bad_read_metadata:
1278 unregister_snapshot(s);
1279
Jonathan Brassowfee19982009-04-02 19:55:34 +01001280bad_load_and_register:
Mikulas Patocka92e86812008-07-21 12:00:35 +01001281 mempool_destroy(s->pending_pool);
1282
Jonathan Brassowfee19982009-04-02 19:55:34 +01001283bad_pending_pool:
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001284 dm_kcopyd_client_destroy(s->kcopyd_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Jonathan Brassowfee19982009-04-02 19:55:34 +01001286bad_kcopyd:
Jon Brassow3510cb92009-12-10 23:52:11 +00001287 dm_exception_table_exit(&s->pending, pending_cache);
1288 dm_exception_table_exit(&s->complete, exception_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Jonathan Brassowfee19982009-04-02 19:55:34 +01001290bad_hash_tables:
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001291 dm_exception_store_destroy(s->store);
1292
1293bad_store:
1294 dm_put_device(ti, s->cow);
1295
1296bad_cow:
Mikulas Patockac2411042010-08-12 04:13:51 +01001297 dm_put_device(ti, s->origin);
1298
1299bad_origin:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 kfree(s);
1301
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001302bad:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 return r;
1304}
1305
Milan Broz31c93a02006-12-08 02:41:11 -08001306static void __free_exceptions(struct dm_snapshot *s)
1307{
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001308 dm_kcopyd_client_destroy(s->kcopyd_client);
Milan Broz31c93a02006-12-08 02:41:11 -08001309 s->kcopyd_client = NULL;
1310
Jon Brassow3510cb92009-12-10 23:52:11 +00001311 dm_exception_table_exit(&s->pending, pending_cache);
1312 dm_exception_table_exit(&s->complete, exception_cache);
Milan Broz31c93a02006-12-08 02:41:11 -08001313}
1314
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001315static void __handover_exceptions(struct dm_snapshot *snap_src,
1316 struct dm_snapshot *snap_dest)
1317{
1318 union {
1319 struct dm_exception_table table_swap;
1320 struct dm_exception_store *store_swap;
1321 } u;
1322
1323 /*
1324 * Swap all snapshot context information between the two instances.
1325 */
1326 u.table_swap = snap_dest->complete;
1327 snap_dest->complete = snap_src->complete;
1328 snap_src->complete = u.table_swap;
1329
1330 u.store_swap = snap_dest->store;
1331 snap_dest->store = snap_src->store;
Mike Snitzerb0d3cc02015-10-08 18:05:41 -04001332 snap_dest->store->userspace_supports_overflow = u.store_swap->userspace_supports_overflow;
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001333 snap_src->store = u.store_swap;
1334
1335 snap_dest->store->snap = snap_dest;
1336 snap_src->store->snap = snap_src;
1337
Mike Snitzer542f9032012-07-27 15:08:00 +01001338 snap_dest->ti->max_io_len = snap_dest->store->chunk_size;
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001339 snap_dest->valid = snap_src->valid;
Mikulas Patocka76c44f62015-06-21 16:31:33 -04001340 snap_dest->snapshot_overflowed = snap_src->snapshot_overflowed;
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001341
1342 /*
1343 * Set source invalid to ensure it receives no further I/O.
1344 */
1345 snap_src->valid = 0;
1346}
1347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348static void snapshot_dtr(struct dm_target *ti)
1349{
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001350#ifdef CONFIG_DM_DEBUG
1351 int i;
1352#endif
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001353 struct dm_snapshot *s = ti->private;
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001354 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001356 down_read(&_origins_lock);
1357 /* Check whether exception handover must be cancelled */
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +00001358 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001359 if (snap_src && snap_dest && (s == snap_src)) {
1360 down_write(&snap_dest->lock);
1361 snap_dest->valid = 0;
1362 up_write(&snap_dest->lock);
1363 DMERR("Cancelling snapshot handover.");
1364 }
1365 up_read(&_origins_lock);
1366
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001367 if (dm_target_is_snapshot_merge(ti))
1368 stop_merge(s);
1369
Alasdair G Kergon138728dc2006-03-27 01:17:50 -08001370 /* Prevent further origin writes from using this snapshot. */
1371 /* After this returns there can be no new kcopyd jobs. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 unregister_snapshot(s);
1373
Mikulas Patocka879129d22008-10-30 13:33:16 +00001374 while (atomic_read(&s->pending_exceptions_count))
Mikulas Patocka90fa1522009-01-06 03:04:54 +00001375 msleep(1);
Mikulas Patocka879129d22008-10-30 13:33:16 +00001376 /*
1377 * Ensure instructions in mempool_destroy aren't reordered
1378 * before atomic_read.
1379 */
1380 smp_mb();
1381
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001382#ifdef CONFIG_DM_DEBUG
1383 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
1384 BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i]));
1385#endif
1386
Milan Broz31c93a02006-12-08 02:41:11 -08001387 __free_exceptions(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Mikulas Patocka92e86812008-07-21 12:00:35 +01001389 mempool_destroy(s->pending_pool);
1390
Jonathan Brassowfee19982009-04-02 19:55:34 +01001391 dm_exception_store_destroy(s->store);
Alasdair G Kergon138728dc2006-03-27 01:17:50 -08001392
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001393 dm_put_device(ti, s->cow);
1394
Mikulas Patockac2411042010-08-12 04:13:51 +01001395 dm_put_device(ti, s->origin);
1396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 kfree(s);
1398}
1399
1400/*
1401 * Flush a list of buffers.
1402 */
1403static void flush_bios(struct bio *bio)
1404{
1405 struct bio *n;
1406
1407 while (bio) {
1408 n = bio->bi_next;
1409 bio->bi_next = NULL;
1410 generic_make_request(bio);
1411 bio = n;
1412 }
1413}
1414
Mikulas Patocka515ad662009-12-10 23:52:30 +00001415static int do_origin(struct dm_dev *origin, struct bio *bio);
1416
1417/*
1418 * Flush a list of buffers.
1419 */
1420static void retry_origin_bios(struct dm_snapshot *s, struct bio *bio)
1421{
1422 struct bio *n;
1423 int r;
1424
1425 while (bio) {
1426 n = bio->bi_next;
1427 bio->bi_next = NULL;
1428 r = do_origin(s->origin, bio);
1429 if (r == DM_MAPIO_REMAPPED)
1430 generic_make_request(bio);
1431 bio = n;
1432 }
1433}
1434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435/*
1436 * Error a list of buffers.
1437 */
1438static void error_bios(struct bio *bio)
1439{
1440 struct bio *n;
1441
1442 while (bio) {
1443 n = bio->bi_next;
1444 bio->bi_next = NULL;
NeilBrown6712ecf2007-09-27 12:47:43 +02001445 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 bio = n;
1447 }
1448}
1449
Alasdair G Kergon695368a2006-10-03 01:15:31 -07001450static void __invalidate_snapshot(struct dm_snapshot *s, int err)
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001451{
1452 if (!s->valid)
1453 return;
1454
1455 if (err == -EIO)
1456 DMERR("Invalidating snapshot: Error reading/writing.");
1457 else if (err == -ENOMEM)
1458 DMERR("Invalidating snapshot: Unable to allocate exception.");
1459
Jonathan Brassow493df712009-04-02 19:55:31 +01001460 if (s->store->type->drop_snapshot)
1461 s->store->type->drop_snapshot(s->store);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001462
1463 s->valid = 0;
1464
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001465 dm_table_event(s->ti->table);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001466}
1467
Mikulas Patocka385277b2016-01-08 19:07:55 -05001468static void pending_complete(void *context, int success)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
Mikulas Patocka385277b2016-01-08 19:07:55 -05001470 struct dm_snap_pending_exception *pe = context;
Jon Brassow1d4989c2009-12-10 23:52:10 +00001471 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 struct dm_snapshot *s = pe->snap;
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001473 struct bio *origin_bios = NULL;
1474 struct bio *snapshot_bios = NULL;
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001475 struct bio *full_bio = NULL;
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001476 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001478 if (!success) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 /* Read/write error - snapshot is unusable */
1480 down_write(&s->lock);
Alasdair G Kergon695368a2006-10-03 01:15:31 -07001481 __invalidate_snapshot(s, -EIO);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001482 error = 1;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001483 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 }
1485
Mikulas Patocka119bc542014-01-13 19:13:36 -05001486 e = alloc_completed_exception(GFP_NOIO);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001487 if (!e) {
1488 down_write(&s->lock);
Alasdair G Kergon695368a2006-10-03 01:15:31 -07001489 __invalidate_snapshot(s, -ENOMEM);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001490 error = 1;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001491 goto out;
1492 }
1493 *e = pe->e;
1494
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001495 down_write(&s->lock);
1496 if (!s->valid) {
Jon Brassow3510cb92009-12-10 23:52:11 +00001497 free_completed_exception(e);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001498 error = 1;
1499 goto out;
1500 }
1501
Mike Snitzer615d1eb2009-12-10 23:52:29 +00001502 /* Check for conflicting reads */
1503 __check_for_conflicting_io(s, pe->e.old_chunk);
Mikulas Patockaa8d41b52008-07-21 12:00:34 +01001504
1505 /*
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001506 * Add a proper exception, and remove the
1507 * in-flight exception from the list.
1508 */
Jon Brassow3510cb92009-12-10 23:52:11 +00001509 dm_insert_exception(&s->complete, e);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001510
Jonathan Brassowa2d2b032011-08-02 12:32:03 +01001511out:
Jon Brassow3510cb92009-12-10 23:52:11 +00001512 dm_remove_exception(&pe->e);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001513 snapshot_bios = bio_list_get(&pe->snapshot_bios);
Mikulas Patocka515ad662009-12-10 23:52:30 +00001514 origin_bios = bio_list_get(&pe->origin_bios);
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001515 full_bio = pe->full_bio;
Mikulas Patockafe3265b2015-11-25 16:03:31 -05001516 if (full_bio)
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001517 full_bio->bi_end_io = pe->full_bio_end_io;
Mikulas Patocka73dfd072009-12-10 23:52:34 +00001518 increment_pending_exceptions_done_count();
1519
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001520 up_write(&s->lock);
1521
1522 /* Submit any pending write bios */
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001523 if (error) {
1524 if (full_bio)
1525 bio_io_error(full_bio);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001526 error_bios(snapshot_bios);
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001527 } else {
1528 if (full_bio)
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001529 bio_endio(full_bio);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001530 flush_bios(snapshot_bios);
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001531 }
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001532
Mikulas Patocka515ad662009-12-10 23:52:30 +00001533 retry_origin_bios(s, origin_bios);
Mikulas Patocka22aa66a2015-02-17 14:34:00 -05001534
1535 free_pending_exception(pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536}
1537
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001538static void complete_exception(struct dm_snap_pending_exception *pe)
1539{
1540 struct dm_snapshot *s = pe->snap;
1541
Mikulas Patocka385277b2016-01-08 19:07:55 -05001542 /* Update the metadata if we are persistent */
1543 s->store->type->commit_exception(s->store, &pe->e, !pe->copy_error,
1544 pending_complete, pe);
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001545}
1546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547/*
1548 * Called when the copy I/O has finished. kcopyd actually runs
1549 * this code so don't block.
1550 */
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -07001551static void copy_callback(int read_err, unsigned long write_err, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001553 struct dm_snap_pending_exception *pe = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 struct dm_snapshot *s = pe->snap;
1555
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001556 pe->copy_error = read_err || write_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001558 if (pe->exception_sequence == s->exception_complete_sequence) {
1559 s->exception_complete_sequence++;
1560 complete_exception(pe);
1561
1562 while (!list_empty(&s->out_of_order_list)) {
1563 pe = list_entry(s->out_of_order_list.next,
1564 struct dm_snap_pending_exception, out_of_order_entry);
1565 if (pe->exception_sequence != s->exception_complete_sequence)
1566 break;
1567 s->exception_complete_sequence++;
1568 list_del(&pe->out_of_order_entry);
1569 complete_exception(pe);
1570 }
1571 } else {
1572 struct list_head *lh;
1573 struct dm_snap_pending_exception *pe2;
1574
1575 list_for_each_prev(lh, &s->out_of_order_list) {
1576 pe2 = list_entry(lh, struct dm_snap_pending_exception, out_of_order_entry);
1577 if (pe2->exception_sequence < pe->exception_sequence)
1578 break;
1579 }
1580 list_add(&pe->out_of_order_entry, lh);
1581 }
Nikos Tsironis8189ee42018-10-31 17:53:08 -04001582 up(&s->cow_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583}
1584
1585/*
1586 * Dispatches the copy operation to kcopyd.
1587 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001588static void start_copy(struct dm_snap_pending_exception *pe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589{
1590 struct dm_snapshot *s = pe->snap;
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +01001591 struct dm_io_region src, dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 struct block_device *bdev = s->origin->bdev;
1593 sector_t dev_size;
1594
1595 dev_size = get_dev_size(bdev);
1596
1597 src.bdev = bdev;
Jonathan Brassow71fab002009-04-02 19:55:33 +01001598 src.sector = chunk_to_sector(s->store, pe->e.old_chunk);
Mikulas Patockadf96eee2009-10-16 23:18:17 +01001599 src.count = min((sector_t)s->store->chunk_size, dev_size - src.sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001601 dest.bdev = s->cow->bdev;
Jonathan Brassow71fab002009-04-02 19:55:33 +01001602 dest.sector = chunk_to_sector(s->store, pe->e.new_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 dest.count = src.count;
1604
1605 /* Hand over to kcopyd */
Nikos Tsironis8189ee42018-10-31 17:53:08 -04001606 down(&s->cow_count);
Jonathan Brassowa2d2b032011-08-02 12:32:03 +01001607 dm_kcopyd_copy(s->kcopyd_client, &src, 1, &dest, 0, copy_callback, pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608}
1609
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001610static void full_bio_end_io(struct bio *bio)
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001611{
1612 void *callback_data = bio->bi_private;
1613
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001614 dm_kcopyd_do_callback(callback_data, 0, bio->bi_status ? 1 : 0);
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001615}
1616
1617static void start_full_bio(struct dm_snap_pending_exception *pe,
1618 struct bio *bio)
1619{
1620 struct dm_snapshot *s = pe->snap;
1621 void *callback_data;
1622
1623 pe->full_bio = bio;
1624 pe->full_bio_end_io = bio->bi_end_io;
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001625
Nikos Tsironis8189ee42018-10-31 17:53:08 -04001626 down(&s->cow_count);
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001627 callback_data = dm_kcopyd_prepare_callback(s->kcopyd_client,
1628 copy_callback, pe);
1629
1630 bio->bi_end_io = full_bio_end_io;
1631 bio->bi_private = callback_data;
1632
1633 generic_make_request(bio);
1634}
1635
Mikulas Patocka29138082009-04-02 19:55:25 +01001636static struct dm_snap_pending_exception *
1637__lookup_pending_exception(struct dm_snapshot *s, chunk_t chunk)
1638{
Jon Brassow3510cb92009-12-10 23:52:11 +00001639 struct dm_exception *e = dm_lookup_exception(&s->pending, chunk);
Mikulas Patocka29138082009-04-02 19:55:25 +01001640
1641 if (!e)
1642 return NULL;
1643
1644 return container_of(e, struct dm_snap_pending_exception, e);
1645}
1646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647/*
1648 * Looks to see if this snapshot already has a pending exception
1649 * for this chunk, otherwise it allocates a new one and inserts
1650 * it into the pending table.
1651 *
1652 * NOTE: a write lock must be held on snap->lock before calling
1653 * this.
1654 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001655static struct dm_snap_pending_exception *
Mikulas Patockac6621392009-04-02 19:55:25 +01001656__find_pending_exception(struct dm_snapshot *s,
1657 struct dm_snap_pending_exception *pe, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
Mikulas Patockac6621392009-04-02 19:55:25 +01001659 struct dm_snap_pending_exception *pe2;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001660
Mikulas Patocka29138082009-04-02 19:55:25 +01001661 pe2 = __lookup_pending_exception(s, chunk);
1662 if (pe2) {
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001663 free_pending_exception(pe);
Mikulas Patocka29138082009-04-02 19:55:25 +01001664 return pe2;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001665 }
1666
1667 pe->e.old_chunk = chunk;
1668 bio_list_init(&pe->origin_bios);
1669 bio_list_init(&pe->snapshot_bios);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001670 pe->started = 0;
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001671 pe->full_bio = NULL;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001672
Jonathan Brassow493df712009-04-02 19:55:31 +01001673 if (s->store->type->prepare_exception(s->store, &pe->e)) {
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001674 free_pending_exception(pe);
1675 return NULL;
1676 }
1677
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001678 pe->exception_sequence = s->exception_start_sequence++;
1679
Jon Brassow3510cb92009-12-10 23:52:11 +00001680 dm_insert_exception(&s->pending, &pe->e);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001681
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 return pe;
1683}
1684
Jon Brassow1d4989c2009-12-10 23:52:10 +00001685static void remap_exception(struct dm_snapshot *s, struct dm_exception *e,
Milan Brozd74f81f2008-02-08 02:11:27 +00001686 struct bio *bio, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
Christoph Hellwig74d46992017-08-23 19:10:32 +02001688 bio_set_dev(bio, s->cow->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001689 bio->bi_iter.bi_sector =
1690 chunk_to_sector(s->store, dm_chunk_number(e->new_chunk) +
1691 (chunk - e->old_chunk)) +
1692 (bio->bi_iter.bi_sector & s->store->chunk_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693}
1694
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001695static int snapshot_map(struct dm_target *ti, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
Jon Brassow1d4989c2009-12-10 23:52:10 +00001697 struct dm_exception *e;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001698 struct dm_snapshot *s = ti->private;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001699 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 chunk_t chunk;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001701 struct dm_snap_pending_exception *pe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Mikulas Patockaee180262012-12-21 20:23:41 +00001703 init_tracked_chunk(bio);
1704
Jens Axboe1eff9d32016-08-05 15:35:16 -06001705 if (bio->bi_opf & REQ_PREFLUSH) {
Christoph Hellwig74d46992017-08-23 19:10:32 +02001706 bio_set_dev(bio, s->cow->bdev);
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01001707 return DM_MAPIO_REMAPPED;
1708 }
1709
Kent Overstreet4f024f32013-10-11 15:44:27 -07001710 chunk = sector_to_chunk(s->store, bio->bi_iter.bi_sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712 /* Full snapshots are not usable */
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001713 /* To get here the table must be live so s->active is always set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 if (!s->valid)
Christoph Hellwig846785e2017-06-03 09:38:02 +02001715 return DM_MAPIO_KILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001717 /* FIXME: should only take write lock if we need
1718 * to copy an exception */
1719 down_write(&s->lock);
1720
Christoph Hellwig70246282016-07-19 11:28:41 +02001721 if (!s->valid || (unlikely(s->snapshot_overflowed) &&
1722 bio_data_dir(bio) == WRITE)) {
Christoph Hellwig846785e2017-06-03 09:38:02 +02001723 r = DM_MAPIO_KILL;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001724 goto out_unlock;
1725 }
1726
1727 /* If the block is already remapped - use that, else remap it */
Jon Brassow3510cb92009-12-10 23:52:11 +00001728 e = dm_lookup_exception(&s->complete, chunk);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001729 if (e) {
Milan Brozd74f81f2008-02-08 02:11:27 +00001730 remap_exception(s, e, bio, chunk);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001731 goto out_unlock;
1732 }
1733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 /*
1735 * Write to snapshot - higher level takes care of RW/RO
1736 * flags so we should only get this if we are
1737 * writeable.
1738 */
Christoph Hellwig70246282016-07-19 11:28:41 +02001739 if (bio_data_dir(bio) == WRITE) {
Mikulas Patocka29138082009-04-02 19:55:25 +01001740 pe = __lookup_pending_exception(s, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001741 if (!pe) {
Mikulas Patockac6621392009-04-02 19:55:25 +01001742 up_write(&s->lock);
1743 pe = alloc_pending_exception(s);
1744 down_write(&s->lock);
1745
Mikulas Patocka76c44f62015-06-21 16:31:33 -04001746 if (!s->valid || s->snapshot_overflowed) {
Mikulas Patockac6621392009-04-02 19:55:25 +01001747 free_pending_exception(pe);
Christoph Hellwig846785e2017-06-03 09:38:02 +02001748 r = DM_MAPIO_KILL;
Mikulas Patockac6621392009-04-02 19:55:25 +01001749 goto out_unlock;
1750 }
1751
Jon Brassow3510cb92009-12-10 23:52:11 +00001752 e = dm_lookup_exception(&s->complete, chunk);
Mikulas Patocka35bf6592009-04-02 19:55:26 +01001753 if (e) {
1754 free_pending_exception(pe);
1755 remap_exception(s, e, bio, chunk);
1756 goto out_unlock;
1757 }
1758
Mikulas Patockac6621392009-04-02 19:55:25 +01001759 pe = __find_pending_exception(s, pe, chunk);
Mikulas Patocka29138082009-04-02 19:55:25 +01001760 if (!pe) {
Mike Snitzerb0d3cc02015-10-08 18:05:41 -04001761 if (s->store->userspace_supports_overflow) {
1762 s->snapshot_overflowed = 1;
1763 DMERR("Snapshot overflowed: Unable to allocate exception.");
1764 } else
1765 __invalidate_snapshot(s, -ENOMEM);
Christoph Hellwig846785e2017-06-03 09:38:02 +02001766 r = DM_MAPIO_KILL;
Mikulas Patocka29138082009-04-02 19:55:25 +01001767 goto out_unlock;
1768 }
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001769 }
1770
Milan Brozd74f81f2008-02-08 02:11:27 +00001771 remap_exception(s, &pe->e, bio, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001772
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001773 r = DM_MAPIO_SUBMITTED;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001774
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001775 if (!pe->started &&
Kent Overstreet4f024f32013-10-11 15:44:27 -07001776 bio->bi_iter.bi_size ==
1777 (s->store->chunk_size << SECTOR_SHIFT)) {
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001778 pe->started = 1;
1779 up_write(&s->lock);
1780 start_full_bio(pe, bio);
1781 goto out;
1782 }
1783
1784 bio_list_add(&pe->snapshot_bios, bio);
1785
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001786 if (!pe->started) {
1787 /* this is protected by snap->lock */
1788 pe->started = 1;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001789 up_write(&s->lock);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001790 start_copy(pe);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001791 goto out;
1792 }
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001793 } else {
Christoph Hellwig74d46992017-08-23 19:10:32 +02001794 bio_set_dev(bio, s->origin->bdev);
Mikulas Patockaee180262012-12-21 20:23:41 +00001795 track_chunk(s, bio, chunk);
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Jonathan Brassowa2d2b032011-08-02 12:32:03 +01001798out_unlock:
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001799 up_write(&s->lock);
Jonathan Brassowa2d2b032011-08-02 12:32:03 +01001800out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 return r;
1802}
1803
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001804/*
1805 * A snapshot-merge target behaves like a combination of a snapshot
1806 * target and a snapshot-origin target. It only generates new
1807 * exceptions in other snapshots and not in the one that is being
1808 * merged.
1809 *
1810 * For each chunk, if there is an existing exception, it is used to
1811 * redirect I/O to the cow device. Otherwise I/O is sent to the origin,
1812 * which in turn might generate exceptions in other snapshots.
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001813 * If merging is currently taking place on the chunk in question, the
1814 * I/O is deferred by adding it to s->bios_queued_during_merge.
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001815 */
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001816static int snapshot_merge_map(struct dm_target *ti, struct bio *bio)
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001817{
1818 struct dm_exception *e;
1819 struct dm_snapshot *s = ti->private;
1820 int r = DM_MAPIO_REMAPPED;
1821 chunk_t chunk;
1822
Mikulas Patockaee180262012-12-21 20:23:41 +00001823 init_tracked_chunk(bio);
1824
Jens Axboe1eff9d32016-08-05 15:35:16 -06001825 if (bio->bi_opf & REQ_PREFLUSH) {
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001826 if (!dm_bio_get_target_bio_nr(bio))
Christoph Hellwig74d46992017-08-23 19:10:32 +02001827 bio_set_dev(bio, s->origin->bdev);
Mike Snitzer10b81062009-12-10 23:52:31 +00001828 else
Christoph Hellwig74d46992017-08-23 19:10:32 +02001829 bio_set_dev(bio, s->cow->bdev);
Mike Snitzer10b81062009-12-10 23:52:31 +00001830 return DM_MAPIO_REMAPPED;
1831 }
1832
Kent Overstreet4f024f32013-10-11 15:44:27 -07001833 chunk = sector_to_chunk(s->store, bio->bi_iter.bi_sector);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001834
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001835 down_write(&s->lock);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001836
Mikulas Patockad2fdb772009-12-10 23:52:36 +00001837 /* Full merging snapshots are redirected to the origin */
1838 if (!s->valid)
1839 goto redirect_to_origin;
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001840
1841 /* If the block is already remapped - use that */
1842 e = dm_lookup_exception(&s->complete, chunk);
1843 if (e) {
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001844 /* Queue writes overlapping with chunks being merged */
Christoph Hellwig70246282016-07-19 11:28:41 +02001845 if (bio_data_dir(bio) == WRITE &&
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001846 chunk >= s->first_merging_chunk &&
1847 chunk < (s->first_merging_chunk +
1848 s->num_merging_chunks)) {
Christoph Hellwig74d46992017-08-23 19:10:32 +02001849 bio_set_dev(bio, s->origin->bdev);
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001850 bio_list_add(&s->bios_queued_during_merge, bio);
1851 r = DM_MAPIO_SUBMITTED;
1852 goto out_unlock;
1853 }
Mikulas Patocka17aa0332009-12-10 23:52:33 +00001854
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001855 remap_exception(s, e, bio, chunk);
Mikulas Patocka17aa0332009-12-10 23:52:33 +00001856
Christoph Hellwig70246282016-07-19 11:28:41 +02001857 if (bio_data_dir(bio) == WRITE)
Mikulas Patockaee180262012-12-21 20:23:41 +00001858 track_chunk(s, bio, chunk);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001859 goto out_unlock;
1860 }
1861
Mikulas Patockad2fdb772009-12-10 23:52:36 +00001862redirect_to_origin:
Christoph Hellwig74d46992017-08-23 19:10:32 +02001863 bio_set_dev(bio, s->origin->bdev);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001864
Christoph Hellwig70246282016-07-19 11:28:41 +02001865 if (bio_data_dir(bio) == WRITE) {
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001866 up_write(&s->lock);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001867 return do_origin(s->origin, bio);
1868 }
1869
1870out_unlock:
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001871 up_write(&s->lock);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001872
1873 return r;
1874}
1875
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001876static int snapshot_end_io(struct dm_target *ti, struct bio *bio,
1877 blk_status_t *error)
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001878{
1879 struct dm_snapshot *s = ti->private;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001880
Mikulas Patockaee180262012-12-21 20:23:41 +00001881 if (is_bio_tracked(bio))
1882 stop_tracking_chunk(s, bio);
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001883
Christoph Hellwig1be56902017-06-03 09:38:03 +02001884 return DM_ENDIO_DONE;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001885}
1886
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001887static void snapshot_merge_presuspend(struct dm_target *ti)
1888{
1889 struct dm_snapshot *s = ti->private;
1890
1891 stop_merge(s);
1892}
1893
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001894static int snapshot_preresume(struct dm_target *ti)
1895{
1896 int r = 0;
1897 struct dm_snapshot *s = ti->private;
1898 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
1899
1900 down_read(&_origins_lock);
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +00001901 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001902 if (snap_src && snap_dest) {
1903 down_read(&snap_src->lock);
1904 if (s == snap_src) {
1905 DMERR("Unable to resume snapshot source until "
1906 "handover completes.");
1907 r = -EINVAL;
Mike Snitzerb83b2f22011-01-13 19:59:59 +00001908 } else if (!dm_suspended(snap_src->ti)) {
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001909 DMERR("Unable to perform snapshot handover until "
1910 "source is suspended.");
1911 r = -EINVAL;
1912 }
1913 up_read(&snap_src->lock);
1914 }
1915 up_read(&_origins_lock);
1916
1917 return r;
1918}
1919
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920static void snapshot_resume(struct dm_target *ti)
1921{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001922 struct dm_snapshot *s = ti->private;
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05001923 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL, *snap_merging = NULL;
Mikulas Patockab735fed2015-02-26 11:40:35 -05001924 struct dm_origin *o;
1925 struct mapped_device *origin_md = NULL;
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05001926 bool must_restart_merging = false;
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001927
1928 down_read(&_origins_lock);
Mikulas Patockab735fed2015-02-26 11:40:35 -05001929
1930 o = __lookup_dm_origin(s->origin->bdev);
1931 if (o)
1932 origin_md = dm_table_get_md(o->ti->table);
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05001933 if (!origin_md) {
1934 (void) __find_snapshots_sharing_cow(s, NULL, NULL, &snap_merging);
1935 if (snap_merging)
1936 origin_md = dm_table_get_md(snap_merging->ti->table);
1937 }
Mikulas Patockab735fed2015-02-26 11:40:35 -05001938 if (origin_md == dm_table_get_md(ti->table))
1939 origin_md = NULL;
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05001940 if (origin_md) {
1941 if (dm_hold(origin_md))
1942 origin_md = NULL;
1943 }
Mikulas Patockab735fed2015-02-26 11:40:35 -05001944
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05001945 up_read(&_origins_lock);
1946
1947 if (origin_md) {
Mikulas Patockab735fed2015-02-26 11:40:35 -05001948 dm_internal_suspend_fast(origin_md);
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05001949 if (snap_merging && test_bit(RUNNING_MERGE, &snap_merging->state_bits)) {
1950 must_restart_merging = true;
1951 stop_merge(snap_merging);
1952 }
1953 }
1954
1955 down_read(&_origins_lock);
Mikulas Patockab735fed2015-02-26 11:40:35 -05001956
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +00001957 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001958 if (snap_src && snap_dest) {
1959 down_write(&snap_src->lock);
1960 down_write_nested(&snap_dest->lock, SINGLE_DEPTH_NESTING);
1961 __handover_exceptions(snap_src, snap_dest);
1962 up_write(&snap_dest->lock);
1963 up_write(&snap_src->lock);
1964 }
Mikulas Patockab735fed2015-02-26 11:40:35 -05001965
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001966 up_read(&_origins_lock);
1967
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05001968 if (origin_md) {
1969 if (must_restart_merging)
1970 start_merge(snap_merging);
1971 dm_internal_resume_fast(origin_md);
1972 dm_put(origin_md);
1973 }
1974
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001975 /* Now we have correct chunk size, reregister */
1976 reregister_snapshot(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001978 down_write(&s->lock);
1979 s->active = 1;
1980 up_write(&s->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981}
1982
Mike Snitzer542f9032012-07-27 15:08:00 +01001983static uint32_t get_origin_minimum_chunksize(struct block_device *bdev)
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001984{
Mike Snitzer542f9032012-07-27 15:08:00 +01001985 uint32_t min_chunksize;
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001986
1987 down_read(&_origins_lock);
1988 min_chunksize = __minimum_chunk_size(__lookup_origin(bdev));
1989 up_read(&_origins_lock);
1990
1991 return min_chunksize;
1992}
1993
1994static void snapshot_merge_resume(struct dm_target *ti)
1995{
1996 struct dm_snapshot *s = ti->private;
1997
1998 /*
1999 * Handover exceptions from existing snapshot.
2000 */
2001 snapshot_resume(ti);
2002
2003 /*
Mike Snitzer542f9032012-07-27 15:08:00 +01002004 * snapshot-merge acts as an origin, so set ti->max_io_len
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002005 */
Mike Snitzer542f9032012-07-27 15:08:00 +01002006 ti->max_io_len = get_origin_minimum_chunksize(s->origin->bdev);
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002007
2008 start_merge(s);
2009}
2010
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002011static void snapshot_status(struct dm_target *ti, status_type_t type,
2012 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013{
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01002014 unsigned sz = 0;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002015 struct dm_snapshot *snap = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
2017 switch (type) {
2018 case STATUSTYPE_INFO:
Mikulas Patocka94e765722009-12-10 23:51:53 +00002019
2020 down_write(&snap->lock);
2021
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 if (!snap->valid)
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01002023 DMEMIT("Invalid");
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +00002024 else if (snap->merge_failed)
2025 DMEMIT("Merge failed");
Mikulas Patocka76c44f62015-06-21 16:31:33 -04002026 else if (snap->snapshot_overflowed)
2027 DMEMIT("Overflow");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 else {
Mike Snitzer985903b2009-12-10 23:52:11 +00002029 if (snap->store->type->usage) {
2030 sector_t total_sectors, sectors_allocated,
2031 metadata_sectors;
2032 snap->store->type->usage(snap->store,
2033 &total_sectors,
2034 &sectors_allocated,
2035 &metadata_sectors);
2036 DMEMIT("%llu/%llu %llu",
2037 (unsigned long long)sectors_allocated,
2038 (unsigned long long)total_sectors,
2039 (unsigned long long)metadata_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 }
2041 else
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01002042 DMEMIT("Unknown");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 }
Mikulas Patocka94e765722009-12-10 23:51:53 +00002044
2045 up_write(&snap->lock);
2046
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 break;
2048
2049 case STATUSTYPE_TABLE:
2050 /*
2051 * kdevname returns a static pointer so we need
2052 * to make private copies if the output is to
2053 * make sense.
2054 */
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00002055 DMEMIT("%s %s", snap->origin->name, snap->cow->name);
Jonathan Brassow1e302a92009-04-02 19:55:35 +01002056 snap->store->type->status(snap->store, type, result + sz,
2057 maxlen - sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 break;
2059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060}
2061
Mike Snitzer8811f462009-09-04 20:40:19 +01002062static int snapshot_iterate_devices(struct dm_target *ti,
2063 iterate_devices_callout_fn fn, void *data)
2064{
2065 struct dm_snapshot *snap = ti->private;
Mikulas Patocka1e5554c2010-08-12 04:13:50 +01002066 int r;
Mike Snitzer8811f462009-09-04 20:40:19 +01002067
Mikulas Patocka1e5554c2010-08-12 04:13:50 +01002068 r = fn(ti, snap->origin, 0, ti->len, data);
2069
2070 if (!r)
2071 r = fn(ti, snap->cow, 0, get_dev_size(snap->cow->bdev), data);
2072
2073 return r;
Mike Snitzer8811f462009-09-04 20:40:19 +01002074}
2075
2076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077/*-----------------------------------------------------------------
2078 * Origin methods
2079 *---------------------------------------------------------------*/
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00002080
2081/*
2082 * If no exceptions need creating, DM_MAPIO_REMAPPED is returned and any
2083 * supplied bio was ignored. The caller may submit it immediately.
2084 * (No remapping actually occurs as the origin is always a direct linear
2085 * map.)
2086 *
2087 * If further exceptions are required, DM_MAPIO_SUBMITTED is returned
2088 * and any supplied bio is added to a list to be submitted once all
2089 * the necessary exceptions exist.
2090 */
2091static int __origin_write(struct list_head *snapshots, sector_t sector,
2092 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{
Mikulas Patocka515ad662009-12-10 23:52:30 +00002094 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 struct dm_snapshot *snap;
Jon Brassow1d4989c2009-12-10 23:52:10 +00002096 struct dm_exception *e;
Mikulas Patocka515ad662009-12-10 23:52:30 +00002097 struct dm_snap_pending_exception *pe;
2098 struct dm_snap_pending_exception *pe_to_start_now = NULL;
2099 struct dm_snap_pending_exception *pe_to_start_last = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 chunk_t chunk;
2101
2102 /* Do all the snapshots on this origin */
2103 list_for_each_entry (snap, snapshots, list) {
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00002104 /*
2105 * Don't make new exceptions in a merging snapshot
2106 * because it has effectively been deleted
2107 */
2108 if (dm_target_is_snapshot_merge(snap->ti))
2109 continue;
2110
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002111 down_write(&snap->lock);
2112
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08002113 /* Only deal with valid and active snapshots */
2114 if (!snap->valid || !snap->active)
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002115 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Alasdair G Kergond5e404c2005-07-12 15:53:05 -07002117 /* Nothing to do if writing beyond end of snapshot */
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00002118 if (sector >= dm_table_get_size(snap->ti->table))
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002119 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
2121 /*
2122 * Remember, different snapshots can have
2123 * different chunk sizes.
2124 */
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00002125 chunk = sector_to_chunk(snap->store, sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
2127 /*
2128 * Check exception table to see if block
2129 * is already remapped in this snapshot
2130 * and trigger an exception if not.
2131 */
Jon Brassow3510cb92009-12-10 23:52:11 +00002132 e = dm_lookup_exception(&snap->complete, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002133 if (e)
2134 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Mikulas Patocka29138082009-04-02 19:55:25 +01002136 pe = __lookup_pending_exception(snap, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002137 if (!pe) {
Mikulas Patockac6621392009-04-02 19:55:25 +01002138 up_write(&snap->lock);
2139 pe = alloc_pending_exception(snap);
2140 down_write(&snap->lock);
2141
2142 if (!snap->valid) {
2143 free_pending_exception(pe);
2144 goto next_snapshot;
2145 }
2146
Jon Brassow3510cb92009-12-10 23:52:11 +00002147 e = dm_lookup_exception(&snap->complete, chunk);
Mikulas Patocka35bf6592009-04-02 19:55:26 +01002148 if (e) {
2149 free_pending_exception(pe);
2150 goto next_snapshot;
2151 }
2152
Mikulas Patockac6621392009-04-02 19:55:25 +01002153 pe = __find_pending_exception(snap, pe, chunk);
Mikulas Patocka29138082009-04-02 19:55:25 +01002154 if (!pe) {
2155 __invalidate_snapshot(snap, -ENOMEM);
2156 goto next_snapshot;
2157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 }
2159
Mikulas Patocka515ad662009-12-10 23:52:30 +00002160 r = DM_MAPIO_SUBMITTED;
2161
2162 /*
2163 * If an origin bio was supplied, queue it to wait for the
2164 * completion of this exception, and start this one last,
2165 * at the end of the function.
2166 */
2167 if (bio) {
2168 bio_list_add(&pe->origin_bios, bio);
2169 bio = NULL;
2170
2171 if (!pe->started) {
2172 pe->started = 1;
2173 pe_to_start_last = pe;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002174 }
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002175 }
2176
2177 if (!pe->started) {
2178 pe->started = 1;
Mikulas Patocka515ad662009-12-10 23:52:30 +00002179 pe_to_start_now = pe;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002180 }
2181
Jonathan Brassowa2d2b032011-08-02 12:32:03 +01002182next_snapshot:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 up_write(&snap->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Mikulas Patocka515ad662009-12-10 23:52:30 +00002185 if (pe_to_start_now) {
2186 start_copy(pe_to_start_now);
2187 pe_to_start_now = NULL;
2188 }
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08002189 }
2190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 /*
Mikulas Patocka515ad662009-12-10 23:52:30 +00002192 * Submit the exception against which the bio is queued last,
2193 * to give the other exceptions a head start.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 */
Mikulas Patocka515ad662009-12-10 23:52:30 +00002195 if (pe_to_start_last)
2196 start_copy(pe_to_start_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
2198 return r;
2199}
2200
2201/*
2202 * Called on a write from the origin driver.
2203 */
2204static int do_origin(struct dm_dev *origin, struct bio *bio)
2205{
2206 struct origin *o;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08002207 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
2209 down_read(&_origins_lock);
2210 o = __lookup_origin(origin->bdev);
2211 if (o)
Kent Overstreet4f024f32013-10-11 15:44:27 -07002212 r = __origin_write(&o->snapshots, bio->bi_iter.bi_sector, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 up_read(&_origins_lock);
2214
2215 return r;
2216}
2217
2218/*
Mikulas Patocka73dfd072009-12-10 23:52:34 +00002219 * Trigger exceptions in all non-merging snapshots.
2220 *
2221 * The chunk size of the merging snapshot may be larger than the chunk
2222 * size of some other snapshot so we may need to reallocate multiple
2223 * chunks in other snapshots.
2224 *
2225 * We scan all the overlapping exceptions in the other snapshots.
2226 * Returns 1 if anything was reallocated and must be waited for,
2227 * otherwise returns 0.
2228 *
2229 * size must be a multiple of merging_snap's chunk_size.
2230 */
2231static int origin_write_extent(struct dm_snapshot *merging_snap,
2232 sector_t sector, unsigned size)
2233{
2234 int must_wait = 0;
2235 sector_t n;
2236 struct origin *o;
2237
2238 /*
Mike Snitzer542f9032012-07-27 15:08:00 +01002239 * The origin's __minimum_chunk_size() got stored in max_io_len
Mikulas Patocka73dfd072009-12-10 23:52:34 +00002240 * by snapshot_merge_resume().
2241 */
2242 down_read(&_origins_lock);
2243 o = __lookup_origin(merging_snap->origin->bdev);
Mike Snitzer542f9032012-07-27 15:08:00 +01002244 for (n = 0; n < size; n += merging_snap->ti->max_io_len)
Mikulas Patocka73dfd072009-12-10 23:52:34 +00002245 if (__origin_write(&o->snapshots, sector + n, NULL) ==
2246 DM_MAPIO_SUBMITTED)
2247 must_wait = 1;
2248 up_read(&_origins_lock);
2249
2250 return must_wait;
2251}
2252
2253/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 * Origin: maps a linear range of a device, with hooks for snapshotting.
2255 */
2256
2257/*
2258 * Construct an origin mapping: <dev_path>
2259 * The context for an origin is merely a 'struct dm_dev *'
2260 * pointing to the real device.
2261 */
2262static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
2263{
2264 int r;
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002265 struct dm_origin *o;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
2267 if (argc != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002268 ti->error = "origin: incorrect number of arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 return -EINVAL;
2270 }
2271
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002272 o = kmalloc(sizeof(struct dm_origin), GFP_KERNEL);
2273 if (!o) {
2274 ti->error = "Cannot allocate private origin structure";
2275 r = -ENOMEM;
2276 goto bad_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 }
2278
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002279 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &o->dev);
2280 if (r) {
2281 ti->error = "Cannot get target device";
2282 goto bad_open;
2283 }
2284
Mikulas Patockab735fed2015-02-26 11:40:35 -05002285 o->ti = ti;
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002286 ti->private = o;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00002287 ti->num_flush_bios = 1;
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01002288
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 return 0;
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002290
2291bad_open:
2292 kfree(o);
2293bad_alloc:
2294 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295}
2296
2297static void origin_dtr(struct dm_target *ti)
2298{
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002299 struct dm_origin *o = ti->private;
Mikulas Patockab735fed2015-02-26 11:40:35 -05002300
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002301 dm_put_device(ti, o->dev);
2302 kfree(o);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303}
2304
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00002305static int origin_map(struct dm_target *ti, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306{
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002307 struct dm_origin *o = ti->private;
Mikulas Patocka298eaa82014-03-14 18:43:07 -04002308 unsigned available_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
Christoph Hellwig74d46992017-08-23 19:10:32 +02002310 bio_set_dev(bio, o->dev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Jens Axboe1eff9d32016-08-05 15:35:16 -06002312 if (unlikely(bio->bi_opf & REQ_PREFLUSH))
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01002313 return DM_MAPIO_REMAPPED;
2314
Christoph Hellwig70246282016-07-19 11:28:41 +02002315 if (bio_data_dir(bio) != WRITE)
Mikulas Patocka298eaa82014-03-14 18:43:07 -04002316 return DM_MAPIO_REMAPPED;
2317
2318 available_sectors = o->split_boundary -
2319 ((unsigned)bio->bi_iter.bi_sector & (o->split_boundary - 1));
2320
2321 if (bio_sectors(bio) > available_sectors)
2322 dm_accept_partial_bio(bio, available_sectors);
2323
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 /* Only tell snapshots if this is a write */
Mikulas Patocka298eaa82014-03-14 18:43:07 -04002325 return do_origin(o->dev, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326}
2327
Dan Williams817bf402017-04-12 13:37:44 -07002328static long origin_dax_direct_access(struct dm_target *ti, pgoff_t pgoff,
2329 long nr_pages, void **kaddr, pfn_t *pfn)
Toshi Kanif6e629b2016-06-28 13:37:16 -06002330{
2331 DMWARN("device does not support dax.");
2332 return -EIO;
2333}
2334
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335/*
Mike Snitzer542f9032012-07-27 15:08:00 +01002336 * Set the target "max_io_len" field to the minimum of all the snapshots'
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 * chunk sizes.
2338 */
2339static void origin_resume(struct dm_target *ti)
2340{
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002341 struct dm_origin *o = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Mikulas Patocka298eaa82014-03-14 18:43:07 -04002343 o->split_boundary = get_origin_minimum_chunksize(o->dev->bdev);
Mikulas Patockab735fed2015-02-26 11:40:35 -05002344
2345 down_write(&_origins_lock);
2346 __insert_dm_origin(o);
2347 up_write(&_origins_lock);
2348}
2349
2350static void origin_postsuspend(struct dm_target *ti)
2351{
2352 struct dm_origin *o = ti->private;
2353
2354 down_write(&_origins_lock);
2355 __remove_dm_origin(o);
2356 up_write(&_origins_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357}
2358
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002359static void origin_status(struct dm_target *ti, status_type_t type,
2360 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361{
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002362 struct dm_origin *o = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
2364 switch (type) {
2365 case STATUSTYPE_INFO:
2366 result[0] = '\0';
2367 break;
2368
2369 case STATUSTYPE_TABLE:
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002370 snprintf(result, maxlen, "%s", o->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 break;
2372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373}
2374
Mike Snitzer8811f462009-09-04 20:40:19 +01002375static int origin_iterate_devices(struct dm_target *ti,
2376 iterate_devices_callout_fn fn, void *data)
2377{
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002378 struct dm_origin *o = ti->private;
Mike Snitzer8811f462009-09-04 20:40:19 +01002379
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002380 return fn(ti, o->dev, 0, ti->len, data);
Mike Snitzer8811f462009-09-04 20:40:19 +01002381}
2382
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383static struct target_type origin_target = {
2384 .name = "snapshot-origin",
Mikulas Patockab735fed2015-02-26 11:40:35 -05002385 .version = {1, 9, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 .module = THIS_MODULE,
2387 .ctr = origin_ctr,
2388 .dtr = origin_dtr,
2389 .map = origin_map,
2390 .resume = origin_resume,
Mikulas Patockab735fed2015-02-26 11:40:35 -05002391 .postsuspend = origin_postsuspend,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 .status = origin_status,
Mike Snitzer8811f462009-09-04 20:40:19 +01002393 .iterate_devices = origin_iterate_devices,
Dan Williams817bf402017-04-12 13:37:44 -07002394 .direct_access = origin_dax_direct_access,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395};
2396
2397static struct target_type snapshot_target = {
2398 .name = "snapshot",
Mike Snitzerb0d3cc02015-10-08 18:05:41 -04002399 .version = {1, 15, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 .module = THIS_MODULE,
2401 .ctr = snapshot_ctr,
2402 .dtr = snapshot_dtr,
2403 .map = snapshot_map,
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002404 .end_io = snapshot_end_io,
Mike Snitzerc1f0c182009-12-10 23:52:24 +00002405 .preresume = snapshot_preresume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 .resume = snapshot_resume,
2407 .status = snapshot_status,
Mike Snitzer8811f462009-09-04 20:40:19 +01002408 .iterate_devices = snapshot_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409};
2410
Mikulas Patockad698aa42009-12-10 23:52:30 +00002411static struct target_type merge_target = {
2412 .name = dm_snapshot_merge_target_name,
Mike Snitzerb0d3cc02015-10-08 18:05:41 -04002413 .version = {1, 4, 0},
Mikulas Patockad698aa42009-12-10 23:52:30 +00002414 .module = THIS_MODULE,
2415 .ctr = snapshot_ctr,
2416 .dtr = snapshot_dtr,
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00002417 .map = snapshot_merge_map,
Mikulas Patockad698aa42009-12-10 23:52:30 +00002418 .end_io = snapshot_end_io,
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002419 .presuspend = snapshot_merge_presuspend,
Mikulas Patockad698aa42009-12-10 23:52:30 +00002420 .preresume = snapshot_preresume,
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002421 .resume = snapshot_merge_resume,
Mikulas Patockad698aa42009-12-10 23:52:30 +00002422 .status = snapshot_status,
2423 .iterate_devices = snapshot_iterate_devices,
2424};
2425
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426static int __init dm_snapshot_init(void)
2427{
2428 int r;
2429
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00002430 r = dm_exception_store_init();
2431 if (r) {
2432 DMERR("Failed to initialize exception stores");
2433 return r;
2434 }
2435
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 r = init_origin_hash();
2437 if (r) {
2438 DMERR("init_origin_hash failed.");
Mikulas Patockad698aa42009-12-10 23:52:30 +00002439 goto bad_origin_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 }
2441
Jon Brassow1d4989c2009-12-10 23:52:10 +00002442 exception_cache = KMEM_CACHE(dm_exception, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 if (!exception_cache) {
2444 DMERR("Couldn't create exception cache.");
2445 r = -ENOMEM;
Mikulas Patockad698aa42009-12-10 23:52:30 +00002446 goto bad_exception_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 }
2448
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002449 pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 if (!pending_cache) {
2451 DMERR("Couldn't create pending cache.");
2452 r = -ENOMEM;
Mikulas Patockad698aa42009-12-10 23:52:30 +00002453 goto bad_pending_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 }
2455
monty_pavel@sina.comfbce4292017-11-25 01:43:50 +08002456 r = dm_register_target(&snapshot_target);
2457 if (r < 0) {
2458 DMERR("snapshot target register failed %d", r);
2459 goto bad_register_snapshot_target;
2460 }
2461
2462 r = dm_register_target(&origin_target);
2463 if (r < 0) {
2464 DMERR("Origin target register failed %d", r);
2465 goto bad_register_origin_target;
2466 }
2467
2468 r = dm_register_target(&merge_target);
2469 if (r < 0) {
2470 DMERR("Merge target register failed %d", r);
2471 goto bad_register_merge_target;
2472 }
2473
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 return 0;
2475
Mikulas Patockad698aa42009-12-10 23:52:30 +00002476bad_register_merge_target:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 dm_unregister_target(&origin_target);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002478bad_register_origin_target:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 dm_unregister_target(&snapshot_target);
Jonathan Brassow034a1862009-10-16 23:18:14 +01002480bad_register_snapshot_target:
monty_pavel@sina.comfbce4292017-11-25 01:43:50 +08002481 kmem_cache_destroy(pending_cache);
2482bad_pending_cache:
2483 kmem_cache_destroy(exception_cache);
2484bad_exception_cache:
2485 exit_origin_hash();
2486bad_origin_hash:
Jonathan Brassow034a1862009-10-16 23:18:14 +01002487 dm_exception_store_exit();
Mikulas Patockad698aa42009-12-10 23:52:30 +00002488
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 return r;
2490}
2491
2492static void __exit dm_snapshot_exit(void)
2493{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00002494 dm_unregister_target(&snapshot_target);
2495 dm_unregister_target(&origin_target);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002496 dm_unregister_target(&merge_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
2498 exit_origin_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 kmem_cache_destroy(pending_cache);
2500 kmem_cache_destroy(exception_cache);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00002501
2502 dm_exception_store_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503}
2504
2505/* Module hooks */
2506module_init(dm_snapshot_init);
2507module_exit(dm_snapshot_exit);
2508
2509MODULE_DESCRIPTION(DM_NAME " snapshot target");
2510MODULE_AUTHOR("Joe Thornber");
2511MODULE_LICENSE("GPL");
Mikulas Patocka23cb2102013-03-01 22:45:47 +00002512MODULE_ALIAS("dm-snapshot-origin");
2513MODULE_ALIAS("dm-snapshot-merge");