blob: 5a67f408876ece6d12129e482bf89938e3bbad2f [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 Tsironis721b1d92018-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 {
Nikos Tsironis4ad8d882019-03-17 14:22:56 +020051 struct rw_semaphore lock;
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010052
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 */
David Jeffery3db27762018-08-07 16:56:00 -040089 struct rb_root out_of_order_tree;
Mikulas Patocka230c83a2013-11-29 18:13:37 -050090
Kent Overstreet6f1c8192018-05-20 18:25:53 -040091 mempool_t pending_pool;
Mike Snitzer924e6002010-03-06 02:32:33 +000092
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 Tsironis721b1d92018-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 Tsironis721b1d92018-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
David Jeffery3db27762018-08-07 16:56:00 -0400220 struct rb_node out_of_order_node;
Mikulas Patocka230c83a2013-11-29 18:13:37 -0500221
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
Kees Cook6da2ec52018-06-12 13:55:00 -0700346 _origins = kmalloc_array(ORIGIN_HASH_SIZE, sizeof(struct list_head),
347 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 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
Kees Cook6da2ec52018-06-12 13:55:00 -0700355 _dm_origins = kmalloc_array(ORIGIN_HASH_SIZE,
356 sizeof(struct list_head),
357 GFP_KERNEL);
Mikulas Patockab735fed2015-02-26 11:40:35 -0500358 if (!_dm_origins) {
359 DMERR("unable to allocate memory for _dm_origins");
360 kfree(_origins);
361 return -ENOMEM;
362 }
363 for (i = 0; i < ORIGIN_HASH_SIZE; i++)
364 INIT_LIST_HEAD(_dm_origins + i);
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 init_rwsem(&_origins_lock);
367
368 return 0;
369}
370
371static void exit_origin_hash(void)
372{
373 kfree(_origins);
Mikulas Patockab735fed2015-02-26 11:40:35 -0500374 kfree(_dm_origins);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100377static unsigned origin_hash(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
379 return bdev->bd_dev & ORIGIN_MASK;
380}
381
382static struct origin *__lookup_origin(struct block_device *origin)
383{
384 struct list_head *ol;
385 struct origin *o;
386
387 ol = &_origins[origin_hash(origin)];
388 list_for_each_entry (o, ol, hash_list)
389 if (bdev_equal(o->bdev, origin))
390 return o;
391
392 return NULL;
393}
394
395static void __insert_origin(struct origin *o)
396{
397 struct list_head *sl = &_origins[origin_hash(o->bdev)];
398 list_add_tail(&o->hash_list, sl);
399}
400
Mikulas Patockab735fed2015-02-26 11:40:35 -0500401static struct dm_origin *__lookup_dm_origin(struct block_device *origin)
402{
403 struct list_head *ol;
404 struct dm_origin *o;
405
406 ol = &_dm_origins[origin_hash(origin)];
407 list_for_each_entry (o, ol, hash_list)
408 if (bdev_equal(o->dev->bdev, origin))
409 return o;
410
411 return NULL;
412}
413
414static void __insert_dm_origin(struct dm_origin *o)
415{
416 struct list_head *sl = &_dm_origins[origin_hash(o->dev->bdev)];
417 list_add_tail(&o->hash_list, sl);
418}
419
420static void __remove_dm_origin(struct dm_origin *o)
421{
422 list_del(&o->hash_list);
423}
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425/*
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000426 * _origins_lock must be held when calling this function.
427 * Returns number of snapshots registered using the supplied cow device, plus:
428 * snap_src - a snapshot suitable for use as a source of exception handover
429 * snap_dest - a snapshot capable of receiving exception handover.
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000430 * snap_merge - an existing snapshot-merge target linked to the same origin.
431 * There can be at most one snapshot-merge target. The parameter is optional.
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000432 *
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000433 * Possible return values and states of snap_src and snap_dest.
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000434 * 0: NULL, NULL - first new snapshot
435 * 1: snap_src, NULL - normal snapshot
436 * 2: snap_src, snap_dest - waiting for handover
437 * 2: snap_src, NULL - handed over, waiting for old to be deleted
438 * 1: NULL, snap_dest - source got destroyed without handover
439 */
440static int __find_snapshots_sharing_cow(struct dm_snapshot *snap,
441 struct dm_snapshot **snap_src,
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000442 struct dm_snapshot **snap_dest,
443 struct dm_snapshot **snap_merge)
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000444{
445 struct dm_snapshot *s;
446 struct origin *o;
447 int count = 0;
448 int active;
449
450 o = __lookup_origin(snap->origin->bdev);
451 if (!o)
452 goto out;
453
454 list_for_each_entry(s, &o->snapshots, list) {
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000455 if (dm_target_is_snapshot_merge(s->ti) && snap_merge)
456 *snap_merge = s;
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000457 if (!bdev_equal(s->cow->bdev, snap->cow->bdev))
458 continue;
459
Nikos Tsironis4ad8d882019-03-17 14:22:56 +0200460 down_read(&s->lock);
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000461 active = s->active;
Nikos Tsironis4ad8d882019-03-17 14:22:56 +0200462 up_read(&s->lock);
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000463
464 if (active) {
465 if (snap_src)
466 *snap_src = s;
467 } else if (snap_dest)
468 *snap_dest = s;
469
470 count++;
471 }
472
473out:
474 return count;
475}
476
477/*
478 * On success, returns 1 if this snapshot is a handover destination,
479 * otherwise returns 0.
480 */
481static int __validate_exception_handover(struct dm_snapshot *snap)
482{
483 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000484 struct dm_snapshot *snap_merge = NULL;
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000485
486 /* Does snapshot need exceptions handed over to it? */
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000487 if ((__find_snapshots_sharing_cow(snap, &snap_src, &snap_dest,
488 &snap_merge) == 2) ||
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000489 snap_dest) {
490 snap->ti->error = "Snapshot cow pairing for exception "
491 "table handover failed";
492 return -EINVAL;
493 }
494
495 /*
496 * If no snap_src was found, snap cannot become a handover
497 * destination.
498 */
499 if (!snap_src)
500 return 0;
501
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000502 /*
503 * Non-snapshot-merge handover?
504 */
505 if (!dm_target_is_snapshot_merge(snap->ti))
506 return 1;
507
508 /*
509 * Do not allow more than one merging snapshot.
510 */
511 if (snap_merge) {
512 snap->ti->error = "A snapshot is already merging.";
513 return -EINVAL;
514 }
515
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000516 if (!snap_src->store->type->prepare_merge ||
517 !snap_src->store->type->commit_merge) {
518 snap->ti->error = "Snapshot exception store does not "
519 "support snapshot-merge.";
520 return -EINVAL;
521 }
522
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000523 return 1;
524}
525
526static void __insert_snapshot(struct origin *o, struct dm_snapshot *s)
527{
528 struct dm_snapshot *l;
529
530 /* Sort the list according to chunk size, largest-first smallest-last */
531 list_for_each_entry(l, &o->snapshots, list)
532 if (l->store->chunk_size < s->store->chunk_size)
533 break;
534 list_add_tail(&s->list, &l->list);
535}
536
537/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 * Make a note of the snapshot and its origin so we can look it
539 * up when the origin has a write on it.
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000540 *
541 * Also validate snapshot exception store handovers.
542 * On success, returns 1 if this registration is a handover destination,
543 * otherwise returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 */
545static int register_snapshot(struct dm_snapshot *snap)
546{
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000547 struct origin *o, *new_o = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 struct block_device *bdev = snap->origin->bdev;
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000549 int r = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000551 new_o = kmalloc(sizeof(*new_o), GFP_KERNEL);
552 if (!new_o)
553 return -ENOMEM;
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 down_write(&_origins_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000557 r = __validate_exception_handover(snap);
558 if (r < 0) {
559 kfree(new_o);
560 goto out;
561 }
562
563 o = __lookup_origin(bdev);
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000564 if (o)
565 kfree(new_o);
566 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 /* New origin */
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000568 o = new_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 /* Initialise the struct */
571 INIT_LIST_HEAD(&o->snapshots);
572 o->bdev = bdev;
573
574 __insert_origin(o);
575 }
576
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000577 __insert_snapshot(o, snap);
578
579out:
580 up_write(&_origins_lock);
581
582 return r;
583}
584
585/*
586 * Move snapshot to correct place in list according to chunk size.
587 */
588static void reregister_snapshot(struct dm_snapshot *s)
589{
590 struct block_device *bdev = s->origin->bdev;
591
592 down_write(&_origins_lock);
593
594 list_del(&s->list);
595 __insert_snapshot(__lookup_origin(bdev), s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 up_write(&_origins_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
600static void unregister_snapshot(struct dm_snapshot *s)
601{
602 struct origin *o;
603
604 down_write(&_origins_lock);
605 o = __lookup_origin(s->origin->bdev);
606
607 list_del(&s->list);
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000608 if (o && list_empty(&o->snapshots)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 list_del(&o->hash_list);
610 kfree(o);
611 }
612
613 up_write(&_origins_lock);
614}
615
616/*
617 * Implementation of the exception hash tables.
Milan Brozd74f81f2008-02-08 02:11:27 +0000618 * The lowest hash_shift bits of the chunk number are ignored, allowing
619 * some consecutive chunks to be grouped together.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 */
Jon Brassow3510cb92009-12-10 23:52:11 +0000621static int dm_exception_table_init(struct dm_exception_table *et,
622 uint32_t size, unsigned hash_shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
624 unsigned int i;
625
Milan Brozd74f81f2008-02-08 02:11:27 +0000626 et->hash_shift = hash_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 et->hash_mask = size - 1;
628 et->table = dm_vcalloc(size, sizeof(struct list_head));
629 if (!et->table)
630 return -ENOMEM;
631
632 for (i = 0; i < size; i++)
633 INIT_LIST_HEAD(et->table + i);
634
635 return 0;
636}
637
Jon Brassow3510cb92009-12-10 23:52:11 +0000638static void dm_exception_table_exit(struct dm_exception_table *et,
639 struct kmem_cache *mem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
641 struct list_head *slot;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000642 struct dm_exception *ex, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 int i, size;
644
645 size = et->hash_mask + 1;
646 for (i = 0; i < size; i++) {
647 slot = et->table + i;
648
649 list_for_each_entry_safe (ex, next, slot, hash_list)
650 kmem_cache_free(mem, ex);
651 }
652
653 vfree(et->table);
654}
655
Jon Brassow191437a2009-12-10 23:52:10 +0000656static uint32_t exception_hash(struct dm_exception_table *et, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Milan Brozd74f81f2008-02-08 02:11:27 +0000658 return (chunk >> et->hash_shift) & et->hash_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
Jon Brassow3510cb92009-12-10 23:52:11 +0000661static void dm_remove_exception(struct dm_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
663 list_del(&e->hash_list);
664}
665
666/*
667 * Return the exception data for a sector, or NULL if not
668 * remapped.
669 */
Jon Brassow3510cb92009-12-10 23:52:11 +0000670static struct dm_exception *dm_lookup_exception(struct dm_exception_table *et,
671 chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673 struct list_head *slot;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000674 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 slot = &et->table[exception_hash(et, chunk)];
677 list_for_each_entry (e, slot, hash_list)
Milan Brozd74f81f2008-02-08 02:11:27 +0000678 if (chunk >= e->old_chunk &&
679 chunk <= e->old_chunk + dm_consecutive_chunk_count(e))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return e;
681
682 return NULL;
683}
684
Mikulas Patocka119bc542014-01-13 19:13:36 -0500685static struct dm_exception *alloc_completed_exception(gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Jon Brassow1d4989c2009-12-10 23:52:10 +0000687 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Mikulas Patocka119bc542014-01-13 19:13:36 -0500689 e = kmem_cache_alloc(exception_cache, gfp);
690 if (!e && gfp == GFP_NOIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 e = kmem_cache_alloc(exception_cache, GFP_ATOMIC);
692
693 return e;
694}
695
Jon Brassow3510cb92009-12-10 23:52:11 +0000696static void free_completed_exception(struct dm_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
698 kmem_cache_free(exception_cache, e);
699}
700
Mikulas Patocka92e86812008-07-21 12:00:35 +0100701static struct dm_snap_pending_exception *alloc_pending_exception(struct dm_snapshot *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Kent Overstreet6f1c8192018-05-20 18:25:53 -0400703 struct dm_snap_pending_exception *pe = mempool_alloc(&s->pending_pool,
Mikulas Patocka92e86812008-07-21 12:00:35 +0100704 GFP_NOIO);
705
Mikulas Patocka879129d22008-10-30 13:33:16 +0000706 atomic_inc(&s->pending_exceptions_count);
Mikulas Patocka92e86812008-07-21 12:00:35 +0100707 pe->snap = s;
708
709 return pe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100712static void free_pending_exception(struct dm_snap_pending_exception *pe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Mikulas Patocka879129d22008-10-30 13:33:16 +0000714 struct dm_snapshot *s = pe->snap;
715
Kent Overstreet6f1c8192018-05-20 18:25:53 -0400716 mempool_free(pe, &s->pending_pool);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100717 smp_mb__before_atomic();
Mikulas Patocka879129d22008-10-30 13:33:16 +0000718 atomic_dec(&s->pending_exceptions_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
Jon Brassow3510cb92009-12-10 23:52:11 +0000721static void dm_insert_exception(struct dm_exception_table *eh,
722 struct dm_exception *new_e)
Milan Brozd74f81f2008-02-08 02:11:27 +0000723{
Milan Brozd74f81f2008-02-08 02:11:27 +0000724 struct list_head *l;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000725 struct dm_exception *e = NULL;
Milan Brozd74f81f2008-02-08 02:11:27 +0000726
727 l = &eh->table[exception_hash(eh, new_e->old_chunk)];
728
729 /* Add immediately if this table doesn't support consecutive chunks */
730 if (!eh->hash_shift)
731 goto out;
732
733 /* List is ordered by old_chunk */
734 list_for_each_entry_reverse(e, l, hash_list) {
735 /* Insert after an existing chunk? */
736 if (new_e->old_chunk == (e->old_chunk +
737 dm_consecutive_chunk_count(e) + 1) &&
738 new_e->new_chunk == (dm_chunk_number(e->new_chunk) +
739 dm_consecutive_chunk_count(e) + 1)) {
740 dm_consecutive_chunk_count_inc(e);
Jon Brassow3510cb92009-12-10 23:52:11 +0000741 free_completed_exception(new_e);
Milan Brozd74f81f2008-02-08 02:11:27 +0000742 return;
743 }
744
745 /* Insert before an existing chunk? */
746 if (new_e->old_chunk == (e->old_chunk - 1) &&
747 new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) {
748 dm_consecutive_chunk_count_inc(e);
749 e->old_chunk--;
750 e->new_chunk--;
Jon Brassow3510cb92009-12-10 23:52:11 +0000751 free_completed_exception(new_e);
Milan Brozd74f81f2008-02-08 02:11:27 +0000752 return;
753 }
754
755 if (new_e->old_chunk > e->old_chunk)
756 break;
757 }
758
759out:
760 list_add(&new_e->hash_list, e ? &e->hash_list : l);
761}
762
Jonathan Brassowa159c1a2009-01-06 03:05:19 +0000763/*
764 * Callback used by the exception stores to load exceptions when
765 * initialising.
766 */
767static int dm_add_exception(void *context, chunk_t old, chunk_t new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
Jonathan Brassowa159c1a2009-01-06 03:05:19 +0000769 struct dm_snapshot *s = context;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000770 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Mikulas Patocka119bc542014-01-13 19:13:36 -0500772 e = alloc_completed_exception(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (!e)
774 return -ENOMEM;
775
776 e->old_chunk = old;
Milan Brozd74f81f2008-02-08 02:11:27 +0000777
778 /* Consecutive_count is implicitly initialised to zero */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 e->new_chunk = new;
Milan Brozd74f81f2008-02-08 02:11:27 +0000780
Jon Brassow3510cb92009-12-10 23:52:11 +0000781 dm_insert_exception(&s->complete, e);
Milan Brozd74f81f2008-02-08 02:11:27 +0000782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return 0;
784}
785
Mikulas Patocka7e201b32009-12-10 23:52:08 +0000786/*
787 * Return a minimum chunk size of all snapshots that have the specified origin.
788 * Return zero if the origin has no snapshots.
789 */
Mike Snitzer542f9032012-07-27 15:08:00 +0100790static uint32_t __minimum_chunk_size(struct origin *o)
Mikulas Patocka7e201b32009-12-10 23:52:08 +0000791{
792 struct dm_snapshot *snap;
793 unsigned chunk_size = 0;
794
795 if (o)
796 list_for_each_entry(snap, &o->snapshots, list)
797 chunk_size = min_not_zero(chunk_size,
798 snap->store->chunk_size);
799
Mike Snitzer542f9032012-07-27 15:08:00 +0100800 return (uint32_t) chunk_size;
Mikulas Patocka7e201b32009-12-10 23:52:08 +0000801}
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803/*
804 * Hard coded magic.
805 */
806static int calc_max_buckets(void)
807{
808 /* use a fixed size of 2MB */
809 unsigned long mem = 2 * 1024 * 1024;
810 mem /= sizeof(struct list_head);
811
812 return mem;
813}
814
815/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 * Allocate room for a suitable hash table.
817 */
Jonathan Brassowfee19982009-04-02 19:55:34 +0100818static int init_hash_tables(struct dm_snapshot *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
Mikulas Patocka60e356f2013-09-18 19:40:42 -0400820 sector_t hash_size, cow_dev_size, max_buckets;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 /*
823 * Calculate based on the size of the original volume or
824 * the COW volume...
825 */
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000826 cow_dev_size = get_dev_size(s->cow->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 max_buckets = calc_max_buckets();
828
Mikulas Patocka60e356f2013-09-18 19:40:42 -0400829 hash_size = cow_dev_size >> s->store->chunk_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 hash_size = min(hash_size, max_buckets);
831
Mikulas Patocka8e87b9b2009-12-10 23:51:54 +0000832 if (hash_size < 64)
833 hash_size = 64;
Robert P. J. Day8defd832008-02-08 02:10:06 +0000834 hash_size = rounddown_pow_of_two(hash_size);
Jon Brassow3510cb92009-12-10 23:52:11 +0000835 if (dm_exception_table_init(&s->complete, hash_size,
836 DM_CHUNK_CONSECUTIVE_BITS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return -ENOMEM;
838
839 /*
840 * Allocate hash table for in-flight exceptions
841 * Make this smaller than the real hash table
842 */
843 hash_size >>= 3;
844 if (hash_size < 64)
845 hash_size = 64;
846
Jon Brassow3510cb92009-12-10 23:52:11 +0000847 if (dm_exception_table_init(&s->pending, hash_size, 0)) {
848 dm_exception_table_exit(&s->complete, exception_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return -ENOMEM;
850 }
851
852 return 0;
853}
854
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000855static void merge_shutdown(struct dm_snapshot *s)
856{
857 clear_bit_unlock(RUNNING_MERGE, &s->state_bits);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100858 smp_mb__after_atomic();
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000859 wake_up_bit(&s->state_bits, RUNNING_MERGE);
860}
861
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000862static struct bio *__release_queued_bios_after_merge(struct dm_snapshot *s)
863{
864 s->first_merging_chunk = 0;
865 s->num_merging_chunks = 0;
866
867 return bio_list_get(&s->bios_queued_during_merge);
868}
869
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000870/*
871 * Remove one chunk from the index of completed exceptions.
872 */
873static int __remove_single_exception_chunk(struct dm_snapshot *s,
874 chunk_t old_chunk)
875{
876 struct dm_exception *e;
877
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000878 e = dm_lookup_exception(&s->complete, old_chunk);
879 if (!e) {
880 DMERR("Corruption detected: exception for block %llu is "
881 "on disk but not in memory",
882 (unsigned long long)old_chunk);
883 return -EINVAL;
884 }
885
886 /*
887 * If this is the only chunk using this exception, remove exception.
888 */
889 if (!dm_consecutive_chunk_count(e)) {
890 dm_remove_exception(e);
891 free_completed_exception(e);
892 return 0;
893 }
894
895 /*
896 * The chunk may be either at the beginning or the end of a
897 * group of consecutive chunks - never in the middle. We are
898 * removing chunks in the opposite order to that in which they
899 * were added, so this should always be true.
900 * Decrement the consecutive chunk counter and adjust the
901 * starting point if necessary.
902 */
903 if (old_chunk == e->old_chunk) {
904 e->old_chunk++;
905 e->new_chunk++;
906 } else if (old_chunk != e->old_chunk +
907 dm_consecutive_chunk_count(e)) {
908 DMERR("Attempt to merge block %llu from the "
909 "middle of a chunk range [%llu - %llu]",
910 (unsigned long long)old_chunk,
911 (unsigned long long)e->old_chunk,
912 (unsigned long long)
913 e->old_chunk + dm_consecutive_chunk_count(e));
914 return -EINVAL;
915 }
916
917 dm_consecutive_chunk_count_dec(e);
918
919 return 0;
920}
921
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000922static void flush_bios(struct bio *bio);
923
924static int remove_single_exception_chunk(struct dm_snapshot *s)
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000925{
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000926 struct bio *b = NULL;
927 int r;
928 chunk_t old_chunk = s->first_merging_chunk + s->num_merging_chunks - 1;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000929
Nikos Tsironis4ad8d882019-03-17 14:22:56 +0200930 down_write(&s->lock);
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000931
932 /*
933 * Process chunks (and associated exceptions) in reverse order
934 * so that dm_consecutive_chunk_count_dec() accounting works.
935 */
936 do {
937 r = __remove_single_exception_chunk(s, old_chunk);
938 if (r)
939 goto out;
940 } while (old_chunk-- > s->first_merging_chunk);
941
942 b = __release_queued_bios_after_merge(s);
943
944out:
Nikos Tsironis4ad8d882019-03-17 14:22:56 +0200945 up_write(&s->lock);
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000946 if (b)
947 flush_bios(b);
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000948
949 return r;
950}
951
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000952static int origin_write_extent(struct dm_snapshot *merging_snap,
953 sector_t sector, unsigned chunk_size);
954
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000955static void merge_callback(int read_err, unsigned long write_err,
956 void *context);
957
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000958static uint64_t read_pending_exceptions_done_count(void)
959{
960 uint64_t pending_exceptions_done;
961
962 spin_lock(&_pending_exceptions_done_spinlock);
963 pending_exceptions_done = _pending_exceptions_done_count;
964 spin_unlock(&_pending_exceptions_done_spinlock);
965
966 return pending_exceptions_done;
967}
968
969static void increment_pending_exceptions_done_count(void)
970{
971 spin_lock(&_pending_exceptions_done_spinlock);
972 _pending_exceptions_done_count++;
973 spin_unlock(&_pending_exceptions_done_spinlock);
974
975 wake_up_all(&_pending_exceptions_done);
976}
977
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000978static void snapshot_merge_next_chunks(struct dm_snapshot *s)
979{
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000980 int i, linear_chunks;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000981 chunk_t old_chunk, new_chunk;
982 struct dm_io_region src, dest;
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000983 sector_t io_size;
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000984 uint64_t previous_count;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000985
986 BUG_ON(!test_bit(RUNNING_MERGE, &s->state_bits));
987 if (unlikely(test_bit(SHUTDOWN_MERGE, &s->state_bits)))
988 goto shut;
989
990 /*
991 * valid flag never changes during merge, so no lock required.
992 */
993 if (!s->valid) {
994 DMERR("Snapshot is invalid: can't merge");
995 goto shut;
996 }
997
Mike Snitzer8a2d5282009-12-10 23:52:34 +0000998 linear_chunks = s->store->type->prepare_merge(s->store, &old_chunk,
999 &new_chunk);
1000 if (linear_chunks <= 0) {
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +00001001 if (linear_chunks < 0) {
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001002 DMERR("Read error in exception store: "
1003 "shutting down merge");
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001004 down_write(&s->lock);
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +00001005 s->merge_failed = 1;
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001006 up_write(&s->lock);
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +00001007 }
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001008 goto shut;
1009 }
1010
Mike Snitzer8a2d5282009-12-10 23:52:34 +00001011 /* Adjust old_chunk and new_chunk to reflect start of linear region */
1012 old_chunk = old_chunk + 1 - linear_chunks;
1013 new_chunk = new_chunk + 1 - linear_chunks;
1014
1015 /*
1016 * Use one (potentially large) I/O to copy all 'linear_chunks'
1017 * from the exception store to the origin
1018 */
1019 io_size = linear_chunks * s->store->chunk_size;
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001020
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001021 dest.bdev = s->origin->bdev;
1022 dest.sector = chunk_to_sector(s->store, old_chunk);
Mike Snitzer8a2d5282009-12-10 23:52:34 +00001023 dest.count = min(io_size, get_dev_size(dest.bdev) - dest.sector);
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001024
1025 src.bdev = s->cow->bdev;
1026 src.sector = chunk_to_sector(s->store, new_chunk);
1027 src.count = dest.count;
1028
Mikulas Patocka73dfd072009-12-10 23:52:34 +00001029 /*
1030 * Reallocate any exceptions needed in other snapshots then
1031 * wait for the pending exceptions to complete.
1032 * Each time any pending exception (globally on the system)
1033 * completes we are woken and repeat the process to find out
1034 * if we can proceed. While this may not seem a particularly
1035 * efficient algorithm, it is not expected to have any
1036 * significant impact on performance.
1037 */
1038 previous_count = read_pending_exceptions_done_count();
Mike Snitzer8a2d5282009-12-10 23:52:34 +00001039 while (origin_write_extent(s, dest.sector, io_size)) {
Mikulas Patocka73dfd072009-12-10 23:52:34 +00001040 wait_event(_pending_exceptions_done,
1041 (read_pending_exceptions_done_count() !=
1042 previous_count));
1043 /* Retry after the wait, until all exceptions are done. */
1044 previous_count = read_pending_exceptions_done_count();
1045 }
1046
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001047 down_write(&s->lock);
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001048 s->first_merging_chunk = old_chunk;
Mike Snitzer8a2d5282009-12-10 23:52:34 +00001049 s->num_merging_chunks = linear_chunks;
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001050 up_write(&s->lock);
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001051
Mike Snitzer8a2d5282009-12-10 23:52:34 +00001052 /* Wait until writes to all 'linear_chunks' drain */
1053 for (i = 0; i < linear_chunks; i++)
1054 __check_for_conflicting_io(s, old_chunk + i);
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001055
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001056 dm_kcopyd_copy(s->kcopyd_client, &src, 1, &dest, 0, merge_callback, s);
1057 return;
1058
1059shut:
1060 merge_shutdown(s);
1061}
1062
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001063static void error_bios(struct bio *bio);
1064
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001065static void merge_callback(int read_err, unsigned long write_err, void *context)
1066{
1067 struct dm_snapshot *s = context;
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001068 struct bio *b = NULL;
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001069
1070 if (read_err || write_err) {
1071 if (read_err)
1072 DMERR("Read error: shutting down merge.");
1073 else
1074 DMERR("Write error: shutting down merge.");
1075 goto shut;
1076 }
1077
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001078 if (s->store->type->commit_merge(s->store,
1079 s->num_merging_chunks) < 0) {
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001080 DMERR("Write error in exception store: shutting down merge");
1081 goto shut;
1082 }
1083
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001084 if (remove_single_exception_chunk(s) < 0)
1085 goto shut;
1086
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001087 snapshot_merge_next_chunks(s);
1088
1089 return;
1090
1091shut:
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001092 down_write(&s->lock);
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +00001093 s->merge_failed = 1;
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001094 b = __release_queued_bios_after_merge(s);
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001095 up_write(&s->lock);
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001096 error_bios(b);
1097
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001098 merge_shutdown(s);
1099}
1100
1101static void start_merge(struct dm_snapshot *s)
1102{
1103 if (!test_and_set_bit(RUNNING_MERGE, &s->state_bits))
1104 snapshot_merge_next_chunks(s);
1105}
1106
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001107/*
1108 * Stop the merging process and wait until it finishes.
1109 */
1110static void stop_merge(struct dm_snapshot *s)
1111{
1112 set_bit(SHUTDOWN_MERGE, &s->state_bits);
NeilBrown74316202014-07-07 15:16:04 +10001113 wait_on_bit(&s->state_bits, RUNNING_MERGE, TASK_UNINTERRUPTIBLE);
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001114 clear_bit(SHUTDOWN_MERGE, &s->state_bits);
1115}
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117/*
Mike Snitzerb0d3cc02015-10-08 18:05:41 -04001118 * Construct a snapshot mapping: <origin_dev> <COW-dev> <p|po|n> <chunk-size>
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 */
1120static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1121{
1122 struct dm_snapshot *s;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001123 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 int r = -EINVAL;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001125 char *origin_path, *cow_path;
DingXiang4df2bf42016-02-02 12:29:18 +08001126 dev_t origin_dev, cow_dev;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001127 unsigned args_used, num_flush_bios = 1;
Mike Snitzer10b81062009-12-10 23:52:31 +00001128 fmode_t origin_mode = FMODE_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Mark McLoughlin4c7e3bf2006-10-03 01:15:25 -07001130 if (argc != 4) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001131 ti->error = "requires exactly 4 arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 r = -EINVAL;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001133 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 }
1135
Mike Snitzer10b81062009-12-10 23:52:31 +00001136 if (dm_target_is_snapshot_merge(ti)) {
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001137 num_flush_bios = 2;
Mike Snitzer10b81062009-12-10 23:52:31 +00001138 origin_mode = FMODE_WRITE;
1139 }
1140
Kent Overstreetd3775352018-06-05 05:26:33 -04001141 s = kzalloc(sizeof(*s), GFP_KERNEL);
Jonathan Brassowfee19982009-04-02 19:55:34 +01001142 if (!s) {
Jonathan Brassowa2d2b032011-08-02 12:32:03 +01001143 ti->error = "Cannot allocate private snapshot structure";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 r = -ENOMEM;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001145 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
1147
Mikulas Patockac2411042010-08-12 04:13:51 +01001148 origin_path = argv[0];
1149 argv++;
1150 argc--;
1151
1152 r = dm_get_device(ti, origin_path, origin_mode, &s->origin);
1153 if (r) {
1154 ti->error = "Cannot get origin device";
1155 goto bad_origin;
1156 }
DingXiang4df2bf42016-02-02 12:29:18 +08001157 origin_dev = s->origin->bdev->bd_dev;
Mikulas Patockac2411042010-08-12 04:13:51 +01001158
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001159 cow_path = argv[0];
1160 argv++;
1161 argc--;
1162
DingXiang4df2bf42016-02-02 12:29:18 +08001163 cow_dev = dm_get_dev_t(cow_path);
1164 if (cow_dev && cow_dev == origin_dev) {
1165 ti->error = "COW device cannot be the same as origin device";
1166 r = -EINVAL;
1167 goto bad_cow;
1168 }
1169
Milan Broz024d37e2011-03-24 13:52:14 +00001170 r = dm_get_device(ti, cow_path, dm_table_get_mode(ti->table), &s->cow);
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001171 if (r) {
1172 ti->error = "Cannot get COW device";
1173 goto bad_cow;
1174 }
1175
1176 r = dm_exception_store_create(ti, argc, argv, s, &args_used, &s->store);
1177 if (r) {
1178 ti->error = "Couldn't create exception store";
1179 r = -EINVAL;
1180 goto bad_store;
1181 }
1182
1183 argv += args_used;
1184 argc -= args_used;
1185
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001186 s->ti = ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 s->valid = 1;
Mikulas Patocka76c44f62015-06-21 16:31:33 -04001188 s->snapshot_overflowed = 0;
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001189 s->active = 0;
Mikulas Patocka879129d22008-10-30 13:33:16 +00001190 atomic_set(&s->pending_exceptions_count, 0);
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001191 s->exception_start_sequence = 0;
1192 s->exception_complete_sequence = 0;
David Jeffery3db27762018-08-07 16:56:00 -04001193 s->out_of_order_tree = RB_ROOT;
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001194 init_rwsem(&s->lock);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001195 INIT_LIST_HEAD(&s->list);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001196 spin_lock_init(&s->pe_lock);
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001197 s->state_bits = 0;
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +00001198 s->merge_failed = 0;
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001199 s->first_merging_chunk = 0;
1200 s->num_merging_chunks = 0;
1201 bio_list_init(&s->bios_queued_during_merge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 /* Allocate hash table for COW data */
Jonathan Brassowfee19982009-04-02 19:55:34 +01001204 if (init_hash_tables(s)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 ti->error = "Unable to allocate hash table space";
1206 r = -ENOMEM;
Jonathan Brassowfee19982009-04-02 19:55:34 +01001207 goto bad_hash_tables;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 }
1209
Nikos Tsironis721b1d92018-10-31 17:53:08 -04001210 sema_init(&s->cow_count, (cow_threshold > 0) ? cow_threshold : INT_MAX);
1211
Mikulas Patockadf5d2e92013-03-01 22:45:49 +00001212 s->kcopyd_client = dm_kcopyd_client_create(&dm_kcopyd_throttle);
Mikulas Patockafa34ce72011-05-29 13:03:13 +01001213 if (IS_ERR(s->kcopyd_client)) {
1214 r = PTR_ERR(s->kcopyd_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 ti->error = "Could not create kcopyd client";
Jonathan Brassowfee19982009-04-02 19:55:34 +01001216 goto bad_kcopyd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 }
1218
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001219 r = mempool_init_slab_pool(&s->pending_pool, MIN_IOS, pending_cache);
1220 if (r) {
Mikulas Patocka92e86812008-07-21 12:00:35 +01001221 ti->error = "Could not allocate mempool for pending exceptions";
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:
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001281 mempool_exit(&s->pending_pool);
Mikulas Patocka92e86812008-07-21 12:00:35 +01001282
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 Broz31c93a0c2006-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 Broz31c93a0c2006-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 Broz31c93a0c2006-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)) {
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001360 down_write(&snap_dest->lock);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001361 snap_dest->valid = 0;
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001362 up_write(&snap_dest->lock);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001363 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 /*
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001377 * Ensure instructions in mempool_exit aren't reordered
Mikulas Patocka879129d22008-10-30 13:33:16 +00001378 * 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 Broz31c93a0c2006-12-08 02:41:11 -08001387 __free_exceptions(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001389 mempool_exit(&s->pending_pool);
Mikulas Patocka92e86812008-07-21 12:00:35 +01001390
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 */
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001480 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) {
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001488 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
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001495 down_write(&s->lock);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001496 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
Mikulas Patockaa8d41b52008-07-21 12:00:34 +01001502 /*
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02001503 * Add a proper exception. After inserting the completed exception all
1504 * subsequent snapshot reads to this chunk will be redirected to the
1505 * COW device. This ensures that we do not starve. Moreover, as long
1506 * as the pending exception exists, neither origin writes nor snapshot
1507 * merging can overwrite the chunk in origin.
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001508 */
Jon Brassow3510cb92009-12-10 23:52:11 +00001509 dm_insert_exception(&s->complete, e);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001510
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02001511 /* Wait for conflicting reads to drain */
1512 if (__chunk_is_tracked(s, pe->e.old_chunk)) {
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001513 up_write(&s->lock);
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02001514 __check_for_conflicting_io(s, pe->e.old_chunk);
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001515 down_write(&s->lock);
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02001516 }
1517
Jonathan Brassowa2d2b032011-08-02 12:32:03 +01001518out:
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02001519 /* Remove the in-flight exception from the list */
Jon Brassow3510cb92009-12-10 23:52:11 +00001520 dm_remove_exception(&pe->e);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001521 snapshot_bios = bio_list_get(&pe->snapshot_bios);
Mikulas Patocka515ad662009-12-10 23:52:30 +00001522 origin_bios = bio_list_get(&pe->origin_bios);
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001523 full_bio = pe->full_bio;
Mikulas Patockafe3265b2015-11-25 16:03:31 -05001524 if (full_bio)
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001525 full_bio->bi_end_io = pe->full_bio_end_io;
Mikulas Patocka73dfd072009-12-10 23:52:34 +00001526 increment_pending_exceptions_done_count();
1527
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001528 up_write(&s->lock);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001529
1530 /* Submit any pending write bios */
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001531 if (error) {
1532 if (full_bio)
1533 bio_io_error(full_bio);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001534 error_bios(snapshot_bios);
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001535 } else {
1536 if (full_bio)
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001537 bio_endio(full_bio);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001538 flush_bios(snapshot_bios);
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001539 }
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001540
Mikulas Patocka515ad662009-12-10 23:52:30 +00001541 retry_origin_bios(s, origin_bios);
Mikulas Patocka22aa66a2015-02-17 14:34:00 -05001542
1543 free_pending_exception(pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544}
1545
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001546static void complete_exception(struct dm_snap_pending_exception *pe)
1547{
1548 struct dm_snapshot *s = pe->snap;
1549
Mikulas Patocka385277b2016-01-08 19:07:55 -05001550 /* Update the metadata if we are persistent */
1551 s->store->type->commit_exception(s->store, &pe->e, !pe->copy_error,
1552 pending_complete, pe);
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001553}
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555/*
1556 * Called when the copy I/O has finished. kcopyd actually runs
1557 * this code so don't block.
1558 */
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -07001559static void copy_callback(int read_err, unsigned long write_err, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001561 struct dm_snap_pending_exception *pe = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 struct dm_snapshot *s = pe->snap;
1563
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001564 pe->copy_error = read_err || write_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001566 if (pe->exception_sequence == s->exception_complete_sequence) {
David Jeffery3db27762018-08-07 16:56:00 -04001567 struct rb_node *next;
1568
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001569 s->exception_complete_sequence++;
1570 complete_exception(pe);
1571
David Jeffery3db27762018-08-07 16:56:00 -04001572 next = rb_first(&s->out_of_order_tree);
1573 while (next) {
1574 pe = rb_entry(next, struct dm_snap_pending_exception,
1575 out_of_order_node);
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001576 if (pe->exception_sequence != s->exception_complete_sequence)
1577 break;
David Jeffery3db27762018-08-07 16:56:00 -04001578 next = rb_next(next);
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001579 s->exception_complete_sequence++;
David Jeffery3db27762018-08-07 16:56:00 -04001580 rb_erase(&pe->out_of_order_node, &s->out_of_order_tree);
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001581 complete_exception(pe);
David Jeffery3db27762018-08-07 16:56:00 -04001582 cond_resched();
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001583 }
1584 } else {
David Jeffery3db27762018-08-07 16:56:00 -04001585 struct rb_node *parent = NULL;
1586 struct rb_node **p = &s->out_of_order_tree.rb_node;
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001587 struct dm_snap_pending_exception *pe2;
1588
David Jeffery3db27762018-08-07 16:56:00 -04001589 while (*p) {
1590 pe2 = rb_entry(*p, struct dm_snap_pending_exception, out_of_order_node);
1591 parent = *p;
1592
1593 BUG_ON(pe->exception_sequence == pe2->exception_sequence);
1594 if (pe->exception_sequence < pe2->exception_sequence)
1595 p = &((*p)->rb_left);
1596 else
1597 p = &((*p)->rb_right);
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001598 }
David Jeffery3db27762018-08-07 16:56:00 -04001599
1600 rb_link_node(&pe->out_of_order_node, parent, p);
1601 rb_insert_color(&pe->out_of_order_node, &s->out_of_order_tree);
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001602 }
Nikos Tsironis721b1d92018-10-31 17:53:08 -04001603 up(&s->cow_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604}
1605
1606/*
1607 * Dispatches the copy operation to kcopyd.
1608 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001609static void start_copy(struct dm_snap_pending_exception *pe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
1611 struct dm_snapshot *s = pe->snap;
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +01001612 struct dm_io_region src, dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 struct block_device *bdev = s->origin->bdev;
1614 sector_t dev_size;
1615
1616 dev_size = get_dev_size(bdev);
1617
1618 src.bdev = bdev;
Jonathan Brassow71fab002009-04-02 19:55:33 +01001619 src.sector = chunk_to_sector(s->store, pe->e.old_chunk);
Mikulas Patockadf96eee2009-10-16 23:18:17 +01001620 src.count = min((sector_t)s->store->chunk_size, dev_size - src.sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001622 dest.bdev = s->cow->bdev;
Jonathan Brassow71fab002009-04-02 19:55:33 +01001623 dest.sector = chunk_to_sector(s->store, pe->e.new_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 dest.count = src.count;
1625
1626 /* Hand over to kcopyd */
Nikos Tsironis721b1d92018-10-31 17:53:08 -04001627 down(&s->cow_count);
Jonathan Brassowa2d2b032011-08-02 12:32:03 +01001628 dm_kcopyd_copy(s->kcopyd_client, &src, 1, &dest, 0, copy_callback, pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629}
1630
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001631static void full_bio_end_io(struct bio *bio)
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001632{
1633 void *callback_data = bio->bi_private;
1634
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001635 dm_kcopyd_do_callback(callback_data, 0, bio->bi_status ? 1 : 0);
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001636}
1637
1638static void start_full_bio(struct dm_snap_pending_exception *pe,
1639 struct bio *bio)
1640{
1641 struct dm_snapshot *s = pe->snap;
1642 void *callback_data;
1643
1644 pe->full_bio = bio;
1645 pe->full_bio_end_io = bio->bi_end_io;
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001646
Nikos Tsironis721b1d92018-10-31 17:53:08 -04001647 down(&s->cow_count);
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001648 callback_data = dm_kcopyd_prepare_callback(s->kcopyd_client,
1649 copy_callback, pe);
1650
1651 bio->bi_end_io = full_bio_end_io;
1652 bio->bi_private = callback_data;
1653
1654 generic_make_request(bio);
1655}
1656
Mikulas Patocka29138082009-04-02 19:55:25 +01001657static struct dm_snap_pending_exception *
1658__lookup_pending_exception(struct dm_snapshot *s, chunk_t chunk)
1659{
Jon Brassow3510cb92009-12-10 23:52:11 +00001660 struct dm_exception *e = dm_lookup_exception(&s->pending, chunk);
Mikulas Patocka29138082009-04-02 19:55:25 +01001661
1662 if (!e)
1663 return NULL;
1664
1665 return container_of(e, struct dm_snap_pending_exception, e);
1666}
1667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668/*
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02001669 * Inserts a pending exception into the pending table.
1670 *
1671 * NOTE: a write lock must be held on snap->lock before calling
1672 * this.
1673 */
1674static struct dm_snap_pending_exception *
1675__insert_pending_exception(struct dm_snapshot *s,
1676 struct dm_snap_pending_exception *pe, chunk_t chunk)
1677{
1678 pe->e.old_chunk = chunk;
1679 bio_list_init(&pe->origin_bios);
1680 bio_list_init(&pe->snapshot_bios);
1681 pe->started = 0;
1682 pe->full_bio = NULL;
1683
1684 if (s->store->type->prepare_exception(s->store, &pe->e)) {
1685 free_pending_exception(pe);
1686 return NULL;
1687 }
1688
1689 pe->exception_sequence = s->exception_start_sequence++;
1690
1691 dm_insert_exception(&s->pending, &pe->e);
1692
1693 return pe;
1694}
1695
1696/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 * Looks to see if this snapshot already has a pending exception
1698 * for this chunk, otherwise it allocates a new one and inserts
1699 * it into the pending table.
1700 *
1701 * NOTE: a write lock must be held on snap->lock before calling
1702 * this.
1703 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001704static struct dm_snap_pending_exception *
Mikulas Patockac6621392009-04-02 19:55:25 +01001705__find_pending_exception(struct dm_snapshot *s,
1706 struct dm_snap_pending_exception *pe, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
Mikulas Patockac6621392009-04-02 19:55:25 +01001708 struct dm_snap_pending_exception *pe2;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001709
Mikulas Patocka29138082009-04-02 19:55:25 +01001710 pe2 = __lookup_pending_exception(s, chunk);
1711 if (pe2) {
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001712 free_pending_exception(pe);
Mikulas Patocka29138082009-04-02 19:55:25 +01001713 return pe2;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001714 }
1715
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02001716 return __insert_pending_exception(s, pe, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717}
1718
Jon Brassow1d4989c2009-12-10 23:52:10 +00001719static void remap_exception(struct dm_snapshot *s, struct dm_exception *e,
Milan Brozd74f81f2008-02-08 02:11:27 +00001720 struct bio *bio, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721{
Christoph Hellwig74d46992017-08-23 19:10:32 +02001722 bio_set_dev(bio, s->cow->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001723 bio->bi_iter.bi_sector =
1724 chunk_to_sector(s->store, dm_chunk_number(e->new_chunk) +
1725 (chunk - e->old_chunk)) +
1726 (bio->bi_iter.bi_sector & s->store->chunk_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727}
1728
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001729static int snapshot_map(struct dm_target *ti, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730{
Jon Brassow1d4989c2009-12-10 23:52:10 +00001731 struct dm_exception *e;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001732 struct dm_snapshot *s = ti->private;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001733 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 chunk_t chunk;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001735 struct dm_snap_pending_exception *pe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Mikulas Patockaee180262012-12-21 20:23:41 +00001737 init_tracked_chunk(bio);
1738
Jens Axboe1eff9d32016-08-05 15:35:16 -06001739 if (bio->bi_opf & REQ_PREFLUSH) {
Christoph Hellwig74d46992017-08-23 19:10:32 +02001740 bio_set_dev(bio, s->cow->bdev);
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01001741 return DM_MAPIO_REMAPPED;
1742 }
1743
Kent Overstreet4f024f32013-10-11 15:44:27 -07001744 chunk = sector_to_chunk(s->store, bio->bi_iter.bi_sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
1746 /* Full snapshots are not usable */
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001747 /* To get here the table must be live so s->active is always set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 if (!s->valid)
Christoph Hellwig846785e2017-06-03 09:38:02 +02001749 return DM_MAPIO_KILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001751 down_write(&s->lock);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001752
Christoph Hellwig70246282016-07-19 11:28:41 +02001753 if (!s->valid || (unlikely(s->snapshot_overflowed) &&
1754 bio_data_dir(bio) == WRITE)) {
Christoph Hellwig846785e2017-06-03 09:38:02 +02001755 r = DM_MAPIO_KILL;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001756 goto out_unlock;
1757 }
1758
1759 /* If the block is already remapped - use that, else remap it */
Jon Brassow3510cb92009-12-10 23:52:11 +00001760 e = dm_lookup_exception(&s->complete, chunk);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001761 if (e) {
Milan Brozd74f81f2008-02-08 02:11:27 +00001762 remap_exception(s, e, bio, chunk);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001763 goto out_unlock;
1764 }
1765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 /*
1767 * Write to snapshot - higher level takes care of RW/RO
1768 * flags so we should only get this if we are
1769 * writeable.
1770 */
Christoph Hellwig70246282016-07-19 11:28:41 +02001771 if (bio_data_dir(bio) == WRITE) {
Mikulas Patocka29138082009-04-02 19:55:25 +01001772 pe = __lookup_pending_exception(s, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001773 if (!pe) {
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001774 up_write(&s->lock);
Mikulas Patockac6621392009-04-02 19:55:25 +01001775 pe = alloc_pending_exception(s);
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001776 down_write(&s->lock);
Mikulas Patockac6621392009-04-02 19:55:25 +01001777
Mikulas Patocka76c44f62015-06-21 16:31:33 -04001778 if (!s->valid || s->snapshot_overflowed) {
Mikulas Patockac6621392009-04-02 19:55:25 +01001779 free_pending_exception(pe);
Christoph Hellwig846785e2017-06-03 09:38:02 +02001780 r = DM_MAPIO_KILL;
Mikulas Patockac6621392009-04-02 19:55:25 +01001781 goto out_unlock;
1782 }
1783
Jon Brassow3510cb92009-12-10 23:52:11 +00001784 e = dm_lookup_exception(&s->complete, chunk);
Mikulas Patocka35bf6592009-04-02 19:55:26 +01001785 if (e) {
1786 free_pending_exception(pe);
1787 remap_exception(s, e, bio, chunk);
1788 goto out_unlock;
1789 }
1790
Mikulas Patockac6621392009-04-02 19:55:25 +01001791 pe = __find_pending_exception(s, pe, chunk);
Mikulas Patocka29138082009-04-02 19:55:25 +01001792 if (!pe) {
Mike Snitzerb0d3cc02015-10-08 18:05:41 -04001793 if (s->store->userspace_supports_overflow) {
1794 s->snapshot_overflowed = 1;
1795 DMERR("Snapshot overflowed: Unable to allocate exception.");
1796 } else
1797 __invalidate_snapshot(s, -ENOMEM);
Christoph Hellwig846785e2017-06-03 09:38:02 +02001798 r = DM_MAPIO_KILL;
Mikulas Patocka29138082009-04-02 19:55:25 +01001799 goto out_unlock;
1800 }
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001801 }
1802
Milan Brozd74f81f2008-02-08 02:11:27 +00001803 remap_exception(s, &pe->e, bio, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001804
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001805 r = DM_MAPIO_SUBMITTED;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001806
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001807 if (!pe->started &&
Kent Overstreet4f024f32013-10-11 15:44:27 -07001808 bio->bi_iter.bi_size ==
1809 (s->store->chunk_size << SECTOR_SHIFT)) {
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001810 pe->started = 1;
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001811 up_write(&s->lock);
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001812 start_full_bio(pe, bio);
1813 goto out;
1814 }
1815
1816 bio_list_add(&pe->snapshot_bios, bio);
1817
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001818 if (!pe->started) {
1819 /* this is protected by snap->lock */
1820 pe->started = 1;
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001821 up_write(&s->lock);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001822 start_copy(pe);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001823 goto out;
1824 }
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001825 } else {
Christoph Hellwig74d46992017-08-23 19:10:32 +02001826 bio_set_dev(bio, s->origin->bdev);
Mikulas Patockaee180262012-12-21 20:23:41 +00001827 track_chunk(s, bio, chunk);
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Jonathan Brassowa2d2b032011-08-02 12:32:03 +01001830out_unlock:
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001831 up_write(&s->lock);
Jonathan Brassowa2d2b032011-08-02 12:32:03 +01001832out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 return r;
1834}
1835
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001836/*
1837 * A snapshot-merge target behaves like a combination of a snapshot
1838 * target and a snapshot-origin target. It only generates new
1839 * exceptions in other snapshots and not in the one that is being
1840 * merged.
1841 *
1842 * For each chunk, if there is an existing exception, it is used to
1843 * redirect I/O to the cow device. Otherwise I/O is sent to the origin,
1844 * which in turn might generate exceptions in other snapshots.
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001845 * If merging is currently taking place on the chunk in question, the
1846 * I/O is deferred by adding it to s->bios_queued_during_merge.
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001847 */
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001848static int snapshot_merge_map(struct dm_target *ti, struct bio *bio)
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001849{
1850 struct dm_exception *e;
1851 struct dm_snapshot *s = ti->private;
1852 int r = DM_MAPIO_REMAPPED;
1853 chunk_t chunk;
1854
Mikulas Patockaee180262012-12-21 20:23:41 +00001855 init_tracked_chunk(bio);
1856
Jens Axboe1eff9d32016-08-05 15:35:16 -06001857 if (bio->bi_opf & REQ_PREFLUSH) {
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001858 if (!dm_bio_get_target_bio_nr(bio))
Christoph Hellwig74d46992017-08-23 19:10:32 +02001859 bio_set_dev(bio, s->origin->bdev);
Mike Snitzer10b81062009-12-10 23:52:31 +00001860 else
Christoph Hellwig74d46992017-08-23 19:10:32 +02001861 bio_set_dev(bio, s->cow->bdev);
Mike Snitzer10b81062009-12-10 23:52:31 +00001862 return DM_MAPIO_REMAPPED;
1863 }
1864
Kent Overstreet4f024f32013-10-11 15:44:27 -07001865 chunk = sector_to_chunk(s->store, bio->bi_iter.bi_sector);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001866
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001867 down_write(&s->lock);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001868
Mikulas Patockad2fdb772009-12-10 23:52:36 +00001869 /* Full merging snapshots are redirected to the origin */
1870 if (!s->valid)
1871 goto redirect_to_origin;
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001872
1873 /* If the block is already remapped - use that */
1874 e = dm_lookup_exception(&s->complete, chunk);
1875 if (e) {
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001876 /* Queue writes overlapping with chunks being merged */
Christoph Hellwig70246282016-07-19 11:28:41 +02001877 if (bio_data_dir(bio) == WRITE &&
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001878 chunk >= s->first_merging_chunk &&
1879 chunk < (s->first_merging_chunk +
1880 s->num_merging_chunks)) {
Christoph Hellwig74d46992017-08-23 19:10:32 +02001881 bio_set_dev(bio, s->origin->bdev);
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001882 bio_list_add(&s->bios_queued_during_merge, bio);
1883 r = DM_MAPIO_SUBMITTED;
1884 goto out_unlock;
1885 }
Mikulas Patocka17aa0332009-12-10 23:52:33 +00001886
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001887 remap_exception(s, e, bio, chunk);
Mikulas Patocka17aa0332009-12-10 23:52:33 +00001888
Christoph Hellwig70246282016-07-19 11:28:41 +02001889 if (bio_data_dir(bio) == WRITE)
Mikulas Patockaee180262012-12-21 20:23:41 +00001890 track_chunk(s, bio, chunk);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001891 goto out_unlock;
1892 }
1893
Mikulas Patockad2fdb772009-12-10 23:52:36 +00001894redirect_to_origin:
Christoph Hellwig74d46992017-08-23 19:10:32 +02001895 bio_set_dev(bio, s->origin->bdev);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001896
Christoph Hellwig70246282016-07-19 11:28:41 +02001897 if (bio_data_dir(bio) == WRITE) {
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001898 up_write(&s->lock);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001899 return do_origin(s->origin, bio);
1900 }
1901
1902out_unlock:
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001903 up_write(&s->lock);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00001904
1905 return r;
1906}
1907
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001908static int snapshot_end_io(struct dm_target *ti, struct bio *bio,
1909 blk_status_t *error)
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001910{
1911 struct dm_snapshot *s = ti->private;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001912
Mikulas Patockaee180262012-12-21 20:23:41 +00001913 if (is_bio_tracked(bio))
1914 stop_tracking_chunk(s, bio);
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001915
Christoph Hellwig1be56902017-06-03 09:38:03 +02001916 return DM_ENDIO_DONE;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001917}
1918
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001919static void snapshot_merge_presuspend(struct dm_target *ti)
1920{
1921 struct dm_snapshot *s = ti->private;
1922
1923 stop_merge(s);
1924}
1925
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001926static int snapshot_preresume(struct dm_target *ti)
1927{
1928 int r = 0;
1929 struct dm_snapshot *s = ti->private;
1930 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
1931
1932 down_read(&_origins_lock);
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +00001933 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001934 if (snap_src && snap_dest) {
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001935 down_read(&snap_src->lock);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001936 if (s == snap_src) {
1937 DMERR("Unable to resume snapshot source until "
1938 "handover completes.");
1939 r = -EINVAL;
Mike Snitzerb83b2f22011-01-13 19:59:59 +00001940 } else if (!dm_suspended(snap_src->ti)) {
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001941 DMERR("Unable to perform snapshot handover until "
1942 "source is suspended.");
1943 r = -EINVAL;
1944 }
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001945 up_read(&snap_src->lock);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001946 }
1947 up_read(&_origins_lock);
1948
1949 return r;
1950}
1951
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952static void snapshot_resume(struct dm_target *ti)
1953{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001954 struct dm_snapshot *s = ti->private;
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05001955 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL, *snap_merging = NULL;
Mikulas Patockab735fed2015-02-26 11:40:35 -05001956 struct dm_origin *o;
1957 struct mapped_device *origin_md = NULL;
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05001958 bool must_restart_merging = false;
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001959
1960 down_read(&_origins_lock);
Mikulas Patockab735fed2015-02-26 11:40:35 -05001961
1962 o = __lookup_dm_origin(s->origin->bdev);
1963 if (o)
1964 origin_md = dm_table_get_md(o->ti->table);
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05001965 if (!origin_md) {
1966 (void) __find_snapshots_sharing_cow(s, NULL, NULL, &snap_merging);
1967 if (snap_merging)
1968 origin_md = dm_table_get_md(snap_merging->ti->table);
1969 }
Mikulas Patockab735fed2015-02-26 11:40:35 -05001970 if (origin_md == dm_table_get_md(ti->table))
1971 origin_md = NULL;
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05001972 if (origin_md) {
1973 if (dm_hold(origin_md))
1974 origin_md = NULL;
1975 }
Mikulas Patockab735fed2015-02-26 11:40:35 -05001976
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05001977 up_read(&_origins_lock);
1978
1979 if (origin_md) {
Mikulas Patockab735fed2015-02-26 11:40:35 -05001980 dm_internal_suspend_fast(origin_md);
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05001981 if (snap_merging && test_bit(RUNNING_MERGE, &snap_merging->state_bits)) {
1982 must_restart_merging = true;
1983 stop_merge(snap_merging);
1984 }
1985 }
1986
1987 down_read(&_origins_lock);
Mikulas Patockab735fed2015-02-26 11:40:35 -05001988
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +00001989 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001990 if (snap_src && snap_dest) {
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001991 down_write(&snap_src->lock);
1992 down_write_nested(&snap_dest->lock, SINGLE_DEPTH_NESTING);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001993 __handover_exceptions(snap_src, snap_dest);
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001994 up_write(&snap_dest->lock);
1995 up_write(&snap_src->lock);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001996 }
Mikulas Patockab735fed2015-02-26 11:40:35 -05001997
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001998 up_read(&_origins_lock);
1999
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05002000 if (origin_md) {
2001 if (must_restart_merging)
2002 start_merge(snap_merging);
2003 dm_internal_resume_fast(origin_md);
2004 dm_put(origin_md);
2005 }
2006
Mike Snitzerc1f0c182009-12-10 23:52:24 +00002007 /* Now we have correct chunk size, reregister */
2008 reregister_snapshot(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02002010 down_write(&s->lock);
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08002011 s->active = 1;
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02002012 up_write(&s->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013}
2014
Mike Snitzer542f9032012-07-27 15:08:00 +01002015static uint32_t get_origin_minimum_chunksize(struct block_device *bdev)
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002016{
Mike Snitzer542f9032012-07-27 15:08:00 +01002017 uint32_t min_chunksize;
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002018
2019 down_read(&_origins_lock);
2020 min_chunksize = __minimum_chunk_size(__lookup_origin(bdev));
2021 up_read(&_origins_lock);
2022
2023 return min_chunksize;
2024}
2025
2026static void snapshot_merge_resume(struct dm_target *ti)
2027{
2028 struct dm_snapshot *s = ti->private;
2029
2030 /*
2031 * Handover exceptions from existing snapshot.
2032 */
2033 snapshot_resume(ti);
2034
2035 /*
Mike Snitzer542f9032012-07-27 15:08:00 +01002036 * snapshot-merge acts as an origin, so set ti->max_io_len
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002037 */
Mike Snitzer542f9032012-07-27 15:08:00 +01002038 ti->max_io_len = get_origin_minimum_chunksize(s->origin->bdev);
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002039
2040 start_merge(s);
2041}
2042
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002043static void snapshot_status(struct dm_target *ti, status_type_t type,
2044 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01002046 unsigned sz = 0;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002047 struct dm_snapshot *snap = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
2049 switch (type) {
2050 case STATUSTYPE_INFO:
Mikulas Patocka94e765722009-12-10 23:51:53 +00002051
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02002052 down_write(&snap->lock);
Mikulas Patocka94e765722009-12-10 23:51:53 +00002053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 if (!snap->valid)
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01002055 DMEMIT("Invalid");
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +00002056 else if (snap->merge_failed)
2057 DMEMIT("Merge failed");
Mikulas Patocka76c44f62015-06-21 16:31:33 -04002058 else if (snap->snapshot_overflowed)
2059 DMEMIT("Overflow");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 else {
Mike Snitzer985903b2009-12-10 23:52:11 +00002061 if (snap->store->type->usage) {
2062 sector_t total_sectors, sectors_allocated,
2063 metadata_sectors;
2064 snap->store->type->usage(snap->store,
2065 &total_sectors,
2066 &sectors_allocated,
2067 &metadata_sectors);
2068 DMEMIT("%llu/%llu %llu",
2069 (unsigned long long)sectors_allocated,
2070 (unsigned long long)total_sectors,
2071 (unsigned long long)metadata_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 }
2073 else
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01002074 DMEMIT("Unknown");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 }
Mikulas Patocka94e765722009-12-10 23:51:53 +00002076
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02002077 up_write(&snap->lock);
Mikulas Patocka94e765722009-12-10 23:51:53 +00002078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 break;
2080
2081 case STATUSTYPE_TABLE:
2082 /*
2083 * kdevname returns a static pointer so we need
2084 * to make private copies if the output is to
2085 * make sense.
2086 */
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00002087 DMEMIT("%s %s", snap->origin->name, snap->cow->name);
Jonathan Brassow1e302a92009-04-02 19:55:35 +01002088 snap->store->type->status(snap->store, type, result + sz,
2089 maxlen - sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 break;
2091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092}
2093
Mike Snitzer8811f462009-09-04 20:40:19 +01002094static int snapshot_iterate_devices(struct dm_target *ti,
2095 iterate_devices_callout_fn fn, void *data)
2096{
2097 struct dm_snapshot *snap = ti->private;
Mikulas Patocka1e5554c2010-08-12 04:13:50 +01002098 int r;
Mike Snitzer8811f462009-09-04 20:40:19 +01002099
Mikulas Patocka1e5554c2010-08-12 04:13:50 +01002100 r = fn(ti, snap->origin, 0, ti->len, data);
2101
2102 if (!r)
2103 r = fn(ti, snap->cow, 0, get_dev_size(snap->cow->bdev), data);
2104
2105 return r;
Mike Snitzer8811f462009-09-04 20:40:19 +01002106}
2107
2108
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109/*-----------------------------------------------------------------
2110 * Origin methods
2111 *---------------------------------------------------------------*/
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00002112
2113/*
2114 * If no exceptions need creating, DM_MAPIO_REMAPPED is returned and any
2115 * supplied bio was ignored. The caller may submit it immediately.
2116 * (No remapping actually occurs as the origin is always a direct linear
2117 * map.)
2118 *
2119 * If further exceptions are required, DM_MAPIO_SUBMITTED is returned
2120 * and any supplied bio is added to a list to be submitted once all
2121 * the necessary exceptions exist.
2122 */
2123static int __origin_write(struct list_head *snapshots, sector_t sector,
2124 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
Mikulas Patocka515ad662009-12-10 23:52:30 +00002126 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 struct dm_snapshot *snap;
Jon Brassow1d4989c2009-12-10 23:52:10 +00002128 struct dm_exception *e;
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02002129 struct dm_snap_pending_exception *pe, *pe2;
Mikulas Patocka515ad662009-12-10 23:52:30 +00002130 struct dm_snap_pending_exception *pe_to_start_now = NULL;
2131 struct dm_snap_pending_exception *pe_to_start_last = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 chunk_t chunk;
2133
2134 /* Do all the snapshots on this origin */
2135 list_for_each_entry (snap, snapshots, list) {
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00002136 /*
2137 * Don't make new exceptions in a merging snapshot
2138 * because it has effectively been deleted
2139 */
2140 if (dm_target_is_snapshot_merge(snap->ti))
2141 continue;
2142
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02002143 down_write(&snap->lock);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002144
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08002145 /* Only deal with valid and active snapshots */
2146 if (!snap->valid || !snap->active)
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002147 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
Alasdair G Kergond5e404c2005-07-12 15:53:05 -07002149 /* Nothing to do if writing beyond end of snapshot */
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00002150 if (sector >= dm_table_get_size(snap->ti->table))
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002151 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
2153 /*
2154 * Remember, different snapshots can have
2155 * different chunk sizes.
2156 */
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00002157 chunk = sector_to_chunk(snap->store, sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Mikulas Patocka29138082009-04-02 19:55:25 +01002159 pe = __lookup_pending_exception(snap, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002160 if (!pe) {
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02002161 /*
2162 * Check exception table to see if block is already
2163 * remapped in this snapshot and trigger an exception
2164 * if not.
2165 */
2166 e = dm_lookup_exception(&snap->complete, chunk);
2167 if (e)
2168 goto next_snapshot;
2169
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02002170 up_write(&snap->lock);
Mikulas Patockac6621392009-04-02 19:55:25 +01002171 pe = alloc_pending_exception(snap);
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02002172 down_write(&snap->lock);
Mikulas Patockac6621392009-04-02 19:55:25 +01002173
2174 if (!snap->valid) {
2175 free_pending_exception(pe);
2176 goto next_snapshot;
2177 }
2178
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02002179 pe2 = __lookup_pending_exception(snap, chunk);
Mikulas Patocka35bf6592009-04-02 19:55:26 +01002180
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02002181 if (!pe2) {
2182 e = dm_lookup_exception(&snap->complete, chunk);
2183 if (e) {
2184 free_pending_exception(pe);
2185 goto next_snapshot;
2186 }
2187
2188 pe = __insert_pending_exception(snap, pe, chunk);
2189 if (!pe) {
2190 __invalidate_snapshot(snap, -ENOMEM);
2191 goto next_snapshot;
2192 }
2193 } else {
2194 free_pending_exception(pe);
2195 pe = pe2;
Mikulas Patocka29138082009-04-02 19:55:25 +01002196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 }
2198
Mikulas Patocka515ad662009-12-10 23:52:30 +00002199 r = DM_MAPIO_SUBMITTED;
2200
2201 /*
2202 * If an origin bio was supplied, queue it to wait for the
2203 * completion of this exception, and start this one last,
2204 * at the end of the function.
2205 */
2206 if (bio) {
2207 bio_list_add(&pe->origin_bios, bio);
2208 bio = NULL;
2209
2210 if (!pe->started) {
2211 pe->started = 1;
2212 pe_to_start_last = pe;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002213 }
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002214 }
2215
2216 if (!pe->started) {
2217 pe->started = 1;
Mikulas Patocka515ad662009-12-10 23:52:30 +00002218 pe_to_start_now = pe;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002219 }
2220
Jonathan Brassowa2d2b032011-08-02 12:32:03 +01002221next_snapshot:
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02002222 up_write(&snap->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
Mikulas Patocka515ad662009-12-10 23:52:30 +00002224 if (pe_to_start_now) {
2225 start_copy(pe_to_start_now);
2226 pe_to_start_now = NULL;
2227 }
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08002228 }
2229
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 /*
Mikulas Patocka515ad662009-12-10 23:52:30 +00002231 * Submit the exception against which the bio is queued last,
2232 * to give the other exceptions a head start.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 */
Mikulas Patocka515ad662009-12-10 23:52:30 +00002234 if (pe_to_start_last)
2235 start_copy(pe_to_start_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
2237 return r;
2238}
2239
2240/*
2241 * Called on a write from the origin driver.
2242 */
2243static int do_origin(struct dm_dev *origin, struct bio *bio)
2244{
2245 struct origin *o;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08002246 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
2248 down_read(&_origins_lock);
2249 o = __lookup_origin(origin->bdev);
2250 if (o)
Kent Overstreet4f024f32013-10-11 15:44:27 -07002251 r = __origin_write(&o->snapshots, bio->bi_iter.bi_sector, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 up_read(&_origins_lock);
2253
2254 return r;
2255}
2256
2257/*
Mikulas Patocka73dfd072009-12-10 23:52:34 +00002258 * Trigger exceptions in all non-merging snapshots.
2259 *
2260 * The chunk size of the merging snapshot may be larger than the chunk
2261 * size of some other snapshot so we may need to reallocate multiple
2262 * chunks in other snapshots.
2263 *
2264 * We scan all the overlapping exceptions in the other snapshots.
2265 * Returns 1 if anything was reallocated and must be waited for,
2266 * otherwise returns 0.
2267 *
2268 * size must be a multiple of merging_snap's chunk_size.
2269 */
2270static int origin_write_extent(struct dm_snapshot *merging_snap,
2271 sector_t sector, unsigned size)
2272{
2273 int must_wait = 0;
2274 sector_t n;
2275 struct origin *o;
2276
2277 /*
Mike Snitzer542f9032012-07-27 15:08:00 +01002278 * The origin's __minimum_chunk_size() got stored in max_io_len
Mikulas Patocka73dfd072009-12-10 23:52:34 +00002279 * by snapshot_merge_resume().
2280 */
2281 down_read(&_origins_lock);
2282 o = __lookup_origin(merging_snap->origin->bdev);
Mike Snitzer542f9032012-07-27 15:08:00 +01002283 for (n = 0; n < size; n += merging_snap->ti->max_io_len)
Mikulas Patocka73dfd072009-12-10 23:52:34 +00002284 if (__origin_write(&o->snapshots, sector + n, NULL) ==
2285 DM_MAPIO_SUBMITTED)
2286 must_wait = 1;
2287 up_read(&_origins_lock);
2288
2289 return must_wait;
2290}
2291
2292/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 * Origin: maps a linear range of a device, with hooks for snapshotting.
2294 */
2295
2296/*
2297 * Construct an origin mapping: <dev_path>
2298 * The context for an origin is merely a 'struct dm_dev *'
2299 * pointing to the real device.
2300 */
2301static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
2302{
2303 int r;
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002304 struct dm_origin *o;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
2306 if (argc != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002307 ti->error = "origin: incorrect number of arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 return -EINVAL;
2309 }
2310
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002311 o = kmalloc(sizeof(struct dm_origin), GFP_KERNEL);
2312 if (!o) {
2313 ti->error = "Cannot allocate private origin structure";
2314 r = -ENOMEM;
2315 goto bad_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 }
2317
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002318 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &o->dev);
2319 if (r) {
2320 ti->error = "Cannot get target device";
2321 goto bad_open;
2322 }
2323
Mikulas Patockab735fed2015-02-26 11:40:35 -05002324 o->ti = ti;
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002325 ti->private = o;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00002326 ti->num_flush_bios = 1;
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01002327
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 return 0;
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002329
2330bad_open:
2331 kfree(o);
2332bad_alloc:
2333 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334}
2335
2336static void origin_dtr(struct dm_target *ti)
2337{
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002338 struct dm_origin *o = ti->private;
Mikulas Patockab735fed2015-02-26 11:40:35 -05002339
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002340 dm_put_device(ti, o->dev);
2341 kfree(o);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342}
2343
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00002344static int origin_map(struct dm_target *ti, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345{
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002346 struct dm_origin *o = ti->private;
Mikulas Patocka298eaa82014-03-14 18:43:07 -04002347 unsigned available_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Christoph Hellwig74d46992017-08-23 19:10:32 +02002349 bio_set_dev(bio, o->dev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
Jens Axboe1eff9d32016-08-05 15:35:16 -06002351 if (unlikely(bio->bi_opf & REQ_PREFLUSH))
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01002352 return DM_MAPIO_REMAPPED;
2353
Christoph Hellwig70246282016-07-19 11:28:41 +02002354 if (bio_data_dir(bio) != WRITE)
Mikulas Patocka298eaa82014-03-14 18:43:07 -04002355 return DM_MAPIO_REMAPPED;
2356
2357 available_sectors = o->split_boundary -
2358 ((unsigned)bio->bi_iter.bi_sector & (o->split_boundary - 1));
2359
2360 if (bio_sectors(bio) > available_sectors)
2361 dm_accept_partial_bio(bio, available_sectors);
2362
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 /* Only tell snapshots if this is a write */
Mikulas Patocka298eaa82014-03-14 18:43:07 -04002364 return do_origin(o->dev, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365}
2366
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367/*
Mike Snitzer542f9032012-07-27 15:08:00 +01002368 * Set the target "max_io_len" field to the minimum of all the snapshots'
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 * chunk sizes.
2370 */
2371static void origin_resume(struct dm_target *ti)
2372{
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002373 struct dm_origin *o = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Mikulas Patocka298eaa82014-03-14 18:43:07 -04002375 o->split_boundary = get_origin_minimum_chunksize(o->dev->bdev);
Mikulas Patockab735fed2015-02-26 11:40:35 -05002376
2377 down_write(&_origins_lock);
2378 __insert_dm_origin(o);
2379 up_write(&_origins_lock);
2380}
2381
2382static void origin_postsuspend(struct dm_target *ti)
2383{
2384 struct dm_origin *o = ti->private;
2385
2386 down_write(&_origins_lock);
2387 __remove_dm_origin(o);
2388 up_write(&_origins_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389}
2390
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002391static void origin_status(struct dm_target *ti, status_type_t type,
2392 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393{
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002394 struct dm_origin *o = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
2396 switch (type) {
2397 case STATUSTYPE_INFO:
2398 result[0] = '\0';
2399 break;
2400
2401 case STATUSTYPE_TABLE:
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002402 snprintf(result, maxlen, "%s", o->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 break;
2404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405}
2406
Mike Snitzer8811f462009-09-04 20:40:19 +01002407static int origin_iterate_devices(struct dm_target *ti,
2408 iterate_devices_callout_fn fn, void *data)
2409{
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002410 struct dm_origin *o = ti->private;
Mike Snitzer8811f462009-09-04 20:40:19 +01002411
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002412 return fn(ti, o->dev, 0, ti->len, data);
Mike Snitzer8811f462009-09-04 20:40:19 +01002413}
2414
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415static struct target_type origin_target = {
2416 .name = "snapshot-origin",
Mikulas Patockab735fed2015-02-26 11:40:35 -05002417 .version = {1, 9, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 .module = THIS_MODULE,
2419 .ctr = origin_ctr,
2420 .dtr = origin_dtr,
2421 .map = origin_map,
2422 .resume = origin_resume,
Mikulas Patockab735fed2015-02-26 11:40:35 -05002423 .postsuspend = origin_postsuspend,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 .status = origin_status,
Mike Snitzer8811f462009-09-04 20:40:19 +01002425 .iterate_devices = origin_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426};
2427
2428static struct target_type snapshot_target = {
2429 .name = "snapshot",
Mike Snitzerb0d3cc02015-10-08 18:05:41 -04002430 .version = {1, 15, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 .module = THIS_MODULE,
2432 .ctr = snapshot_ctr,
2433 .dtr = snapshot_dtr,
2434 .map = snapshot_map,
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002435 .end_io = snapshot_end_io,
Mike Snitzerc1f0c182009-12-10 23:52:24 +00002436 .preresume = snapshot_preresume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 .resume = snapshot_resume,
2438 .status = snapshot_status,
Mike Snitzer8811f462009-09-04 20:40:19 +01002439 .iterate_devices = snapshot_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440};
2441
Mikulas Patockad698aa42009-12-10 23:52:30 +00002442static struct target_type merge_target = {
2443 .name = dm_snapshot_merge_target_name,
Mike Snitzerb0d3cc02015-10-08 18:05:41 -04002444 .version = {1, 4, 0},
Mikulas Patockad698aa42009-12-10 23:52:30 +00002445 .module = THIS_MODULE,
2446 .ctr = snapshot_ctr,
2447 .dtr = snapshot_dtr,
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00002448 .map = snapshot_merge_map,
Mikulas Patockad698aa42009-12-10 23:52:30 +00002449 .end_io = snapshot_end_io,
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002450 .presuspend = snapshot_merge_presuspend,
Mikulas Patockad698aa42009-12-10 23:52:30 +00002451 .preresume = snapshot_preresume,
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002452 .resume = snapshot_merge_resume,
Mikulas Patockad698aa42009-12-10 23:52:30 +00002453 .status = snapshot_status,
2454 .iterate_devices = snapshot_iterate_devices,
2455};
2456
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457static int __init dm_snapshot_init(void)
2458{
2459 int r;
2460
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00002461 r = dm_exception_store_init();
2462 if (r) {
2463 DMERR("Failed to initialize exception stores");
2464 return r;
2465 }
2466
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 r = init_origin_hash();
2468 if (r) {
2469 DMERR("init_origin_hash failed.");
Mikulas Patockad698aa42009-12-10 23:52:30 +00002470 goto bad_origin_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 }
2472
Jon Brassow1d4989c2009-12-10 23:52:10 +00002473 exception_cache = KMEM_CACHE(dm_exception, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 if (!exception_cache) {
2475 DMERR("Couldn't create exception cache.");
2476 r = -ENOMEM;
Mikulas Patockad698aa42009-12-10 23:52:30 +00002477 goto bad_exception_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 }
2479
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002480 pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 if (!pending_cache) {
2482 DMERR("Couldn't create pending cache.");
2483 r = -ENOMEM;
Mikulas Patockad698aa42009-12-10 23:52:30 +00002484 goto bad_pending_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 }
2486
monty_pavel@sina.com7e6358d2017-11-25 01:43:50 +08002487 r = dm_register_target(&snapshot_target);
2488 if (r < 0) {
2489 DMERR("snapshot target register failed %d", r);
2490 goto bad_register_snapshot_target;
2491 }
2492
2493 r = dm_register_target(&origin_target);
2494 if (r < 0) {
2495 DMERR("Origin target register failed %d", r);
2496 goto bad_register_origin_target;
2497 }
2498
2499 r = dm_register_target(&merge_target);
2500 if (r < 0) {
2501 DMERR("Merge target register failed %d", r);
2502 goto bad_register_merge_target;
2503 }
2504
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 return 0;
2506
Mikulas Patockad698aa42009-12-10 23:52:30 +00002507bad_register_merge_target:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 dm_unregister_target(&origin_target);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002509bad_register_origin_target:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 dm_unregister_target(&snapshot_target);
Jonathan Brassow034a1862009-10-16 23:18:14 +01002511bad_register_snapshot_target:
monty_pavel@sina.com7e6358d2017-11-25 01:43:50 +08002512 kmem_cache_destroy(pending_cache);
2513bad_pending_cache:
2514 kmem_cache_destroy(exception_cache);
2515bad_exception_cache:
2516 exit_origin_hash();
2517bad_origin_hash:
Jonathan Brassow034a1862009-10-16 23:18:14 +01002518 dm_exception_store_exit();
Mikulas Patockad698aa42009-12-10 23:52:30 +00002519
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 return r;
2521}
2522
2523static void __exit dm_snapshot_exit(void)
2524{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00002525 dm_unregister_target(&snapshot_target);
2526 dm_unregister_target(&origin_target);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002527 dm_unregister_target(&merge_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
2529 exit_origin_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 kmem_cache_destroy(pending_cache);
2531 kmem_cache_destroy(exception_cache);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00002532
2533 dm_exception_store_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534}
2535
2536/* Module hooks */
2537module_init(dm_snapshot_init);
2538module_exit(dm_snapshot_exit);
2539
2540MODULE_DESCRIPTION(DM_NAME " snapshot target");
2541MODULE_AUTHOR("Joe Thornber");
2542MODULE_LICENSE("GPL");
Mikulas Patocka23cb2102013-03-01 22:45:47 +00002543MODULE_ALIAS("dm-snapshot-origin");
2544MODULE_ALIAS("dm-snapshot-merge");