blob: 63916e1dc569caf1ddc2e09fe85e1b06803ada88 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 2001-2002 Sistina Software (UK) Limited.
3 *
4 * This file is released under the GPL.
5 */
6
7#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/device-mapper.h>
Mikulas Patocka90fa1522009-01-06 03:04:54 +00009#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/fs.h>
11#include <linux/init.h>
12#include <linux/kdev_t.h>
13#include <linux/list.h>
Nikos Tsironisf79ae412019-03-17 14:22:57 +020014#include <linux/list_bl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/mempool.h>
16#include <linux/module.h>
17#include <linux/slab.h>
18#include <linux/vmalloc.h>
vignesh babu6f3c3f02007-10-19 22:38:44 +010019#include <linux/log2.h>
Alasdair G Kergona765e202008-04-24 22:02:01 +010020#include <linux/dm-kcopyd.h>
Nikos Tsironis721b1d92018-10-31 17:53:08 -040021#include <linux/semaphore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Mikulas Patockab735fed2015-02-26 11:40:35 -050023#include "dm.h"
24
Jonathan Brassowaea53d92009-01-06 03:05:15 +000025#include "dm-exception-store.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Alasdair G Kergon72d94862006-06-26 00:27:35 -070027#define DM_MSG_PREFIX "snapshots"
28
Mikulas Patockad698aa42009-12-10 23:52:30 +000029static const char dm_snapshot_merge_target_name[] = "snapshot-merge";
30
31#define dm_target_is_snapshot_merge(ti) \
32 ((ti)->type->name == dm_snapshot_merge_target_name)
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/*
Mikulas Patockacd45daf2008-07-21 12:00:32 +010035 * The size of the mempool used to track chunks in use.
36 */
37#define MIN_IOS 256
38
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010039#define DM_TRACKED_CHUNK_HASH_SIZE 16
40#define DM_TRACKED_CHUNK_HASH(x) ((unsigned long)(x) & \
41 (DM_TRACKED_CHUNK_HASH_SIZE - 1))
42
Jon Brassow191437a2009-12-10 23:52:10 +000043struct dm_exception_table {
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010044 uint32_t hash_mask;
45 unsigned hash_shift;
Nikos Tsironisf79ae412019-03-17 14:22:57 +020046 struct hlist_bl_head *table;
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010047};
48
49struct dm_snapshot {
Nikos Tsironis4ad8d882019-03-17 14:22:56 +020050 struct rw_semaphore lock;
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010051
52 struct dm_dev *origin;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +000053 struct dm_dev *cow;
54
55 struct dm_target *ti;
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010056
57 /* List of snapshots per Origin */
58 struct list_head list;
59
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +000060 /*
61 * You can't use a snapshot if this is 0 (e.g. if full).
62 * A snapshot-merge target never clears this.
63 */
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010064 int valid;
65
Mikulas Patocka76c44f62015-06-21 16:31:33 -040066 /*
67 * The snapshot overflowed because of a write to the snapshot device.
68 * We don't have to invalidate the snapshot in this case, but we need
69 * to prevent further writes.
70 */
71 int snapshot_overflowed;
72
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010073 /* Origin writes don't trigger exceptions until this is set */
74 int active;
75
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010076 atomic_t pending_exceptions_count;
77
Nikos Tsironis3f1637f2019-03-17 14:22:58 +020078 spinlock_t pe_allocation_lock;
79
80 /* Protected by "pe_allocation_lock" */
Mikulas Patocka230c83a2013-11-29 18:13:37 -050081 sector_t exception_start_sequence;
82
83 /* Protected by kcopyd single-threaded callback */
84 sector_t exception_complete_sequence;
85
86 /*
87 * A list of pending exceptions that completed out of order.
88 * Protected by kcopyd single-threaded callback.
89 */
David Jeffery3db27762018-08-07 16:56:00 -040090 struct rb_root out_of_order_tree;
Mikulas Patocka230c83a2013-11-29 18:13:37 -050091
Kent Overstreet6f1c8192018-05-20 18:25:53 -040092 mempool_t pending_pool;
Mike Snitzer924e6002010-03-06 02:32:33 +000093
Jon Brassow191437a2009-12-10 23:52:10 +000094 struct dm_exception_table pending;
95 struct dm_exception_table complete;
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010096
97 /*
98 * pe_lock protects all pending_exception operations and access
99 * as well as the snapshot_bios list.
100 */
101 spinlock_t pe_lock;
102
Mike Snitzer924e6002010-03-06 02:32:33 +0000103 /* Chunks with outstanding reads */
104 spinlock_t tracked_chunk_lock;
Mike Snitzer924e6002010-03-06 02:32:33 +0000105 struct hlist_head tracked_chunk_hash[DM_TRACKED_CHUNK_HASH_SIZE];
106
Jonathan Brassowccc45ea2009-04-02 19:55:34 +0100107 /* The on disk metadata handler */
108 struct dm_exception_store *store;
109
Nikos Tsironis721b1d92018-10-31 17:53:08 -0400110 /* Maximum number of in-flight COW jobs. */
111 struct semaphore cow_count;
112
Jonathan Brassowccc45ea2009-04-02 19:55:34 +0100113 struct dm_kcopyd_client *kcopyd_client;
114
Mike Snitzer924e6002010-03-06 02:32:33 +0000115 /* Wait for events based on state_bits */
116 unsigned long state_bits;
117
118 /* Range of chunks currently being merged. */
119 chunk_t first_merging_chunk;
120 int num_merging_chunks;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000121
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +0000122 /*
123 * The merge operation failed if this flag is set.
124 * Failure modes are handled as follows:
125 * - I/O error reading the header
126 * => don't load the target; abort.
127 * - Header does not have "valid" flag set
128 * => use the origin; forget about the snapshot.
129 * - I/O error when reading exceptions
130 * => don't load the target; abort.
131 * (We can't use the intermediate origin state.)
132 * - I/O error while merging
133 * => stop merging; set merge_failed; process I/O normally.
134 */
Mike Snitzer2e602382019-06-19 17:05:54 -0400135 bool merge_failed:1;
136
137 bool discard_zeroes_cow:1;
138 bool discard_passdown_origin:1;
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +0000139
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000140 /*
141 * Incoming bios that overlap with chunks being merged must wait
142 * for them to be committed.
143 */
144 struct bio_list bios_queued_during_merge;
Jonathan Brassowccc45ea2009-04-02 19:55:34 +0100145};
146
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000147/*
148 * state_bits:
149 * RUNNING_MERGE - Merge operation is in progress.
150 * SHUTDOWN_MERGE - Set to signal that merge needs to be stopped;
151 * cleared afterwards.
152 */
153#define RUNNING_MERGE 0
154#define SHUTDOWN_MERGE 1
155
Nikos Tsironis721b1d92018-10-31 17:53:08 -0400156/*
157 * Maximum number of chunks being copied on write.
158 *
159 * The value was decided experimentally as a trade-off between memory
160 * consumption, stalling the kernel's workqueues and maintaining a high enough
161 * throughput.
162 */
163#define DEFAULT_COW_THRESHOLD 2048
164
165static int cow_threshold = DEFAULT_COW_THRESHOLD;
166module_param_named(snapshot_cow_threshold, cow_threshold, int, 0644);
167MODULE_PARM_DESC(snapshot_cow_threshold, "Maximum number of chunks being copied on write");
168
Mikulas Patockadf5d2e92013-03-01 22:45:49 +0000169DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(snapshot_copy_throttle,
170 "A percentage of time allocated for copy on write");
171
Mikulas Patockac2411042010-08-12 04:13:51 +0100172struct dm_dev *dm_snap_origin(struct dm_snapshot *s)
173{
174 return s->origin;
175}
176EXPORT_SYMBOL(dm_snap_origin);
177
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000178struct dm_dev *dm_snap_cow(struct dm_snapshot *s)
179{
180 return s->cow;
181}
182EXPORT_SYMBOL(dm_snap_cow);
183
Jonathan Brassowccc45ea2009-04-02 19:55:34 +0100184static sector_t chunk_to_sector(struct dm_exception_store *store,
185 chunk_t chunk)
186{
187 return chunk << store->chunk_shift;
188}
189
190static int bdev_equal(struct block_device *lhs, struct block_device *rhs)
191{
192 /*
193 * There is only ever one instance of a particular block
194 * device so we can compare pointers safely.
195 */
196 return lhs == rhs;
197}
198
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100199struct dm_snap_pending_exception {
Jon Brassow1d4989c2009-12-10 23:52:10 +0000200 struct dm_exception e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 /*
203 * Origin buffers waiting for this to complete are held
204 * in a bio list
205 */
206 struct bio_list origin_bios;
207 struct bio_list snapshot_bios;
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 /* Pointer back to snapshot context */
210 struct dm_snapshot *snap;
211
212 /*
213 * 1 indicates the exception has already been sent to
214 * kcopyd.
215 */
216 int started;
Mikulas Patockaa6e50b42011-08-02 12:32:04 +0100217
Mikulas Patocka230c83a2013-11-29 18:13:37 -0500218 /* There was copying error. */
219 int copy_error;
220
221 /* A sequence number, it is used for in-order completion. */
222 sector_t exception_sequence;
223
David Jeffery3db27762018-08-07 16:56:00 -0400224 struct rb_node out_of_order_node;
Mikulas Patocka230c83a2013-11-29 18:13:37 -0500225
Mikulas Patockaa6e50b42011-08-02 12:32:04 +0100226 /*
227 * For writing a complete chunk, bypassing the copy.
228 */
229 struct bio *full_bio;
230 bio_end_io_t *full_bio_end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231};
232
233/*
234 * Hash table mapping origin volumes to lists of snapshots and
235 * a lock to protect it
236 */
Christoph Lametere18b8902006-12-06 20:33:20 -0800237static struct kmem_cache *exception_cache;
238static struct kmem_cache *pending_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100240struct dm_snap_tracked_chunk {
241 struct hlist_node node;
242 chunk_t chunk;
243};
244
Mikulas Patockaee180262012-12-21 20:23:41 +0000245static void init_tracked_chunk(struct bio *bio)
246{
247 struct dm_snap_tracked_chunk *c = dm_per_bio_data(bio, sizeof(struct dm_snap_tracked_chunk));
248 INIT_HLIST_NODE(&c->node);
249}
250
251static bool is_bio_tracked(struct bio *bio)
252{
253 struct dm_snap_tracked_chunk *c = dm_per_bio_data(bio, sizeof(struct dm_snap_tracked_chunk));
254 return !hlist_unhashed(&c->node);
255}
256
257static void track_chunk(struct dm_snapshot *s, struct bio *bio, chunk_t chunk)
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100258{
Mikulas Patocka42bc9542012-12-21 20:23:38 +0000259 struct dm_snap_tracked_chunk *c = dm_per_bio_data(bio, sizeof(struct dm_snap_tracked_chunk));
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100260
261 c->chunk = chunk;
262
Mikulas Patocka9aa0c0e2012-12-21 20:23:33 +0000263 spin_lock_irq(&s->tracked_chunk_lock);
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100264 hlist_add_head(&c->node,
265 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)]);
Mikulas Patocka9aa0c0e2012-12-21 20:23:33 +0000266 spin_unlock_irq(&s->tracked_chunk_lock);
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100267}
268
Mikulas Patockaee180262012-12-21 20:23:41 +0000269static void stop_tracking_chunk(struct dm_snapshot *s, struct bio *bio)
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100270{
Mikulas Patockaee180262012-12-21 20:23:41 +0000271 struct dm_snap_tracked_chunk *c = dm_per_bio_data(bio, sizeof(struct dm_snap_tracked_chunk));
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100272 unsigned long flags;
273
274 spin_lock_irqsave(&s->tracked_chunk_lock, flags);
275 hlist_del(&c->node);
276 spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100277}
278
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100279static int __chunk_is_tracked(struct dm_snapshot *s, chunk_t chunk)
280{
281 struct dm_snap_tracked_chunk *c;
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100282 int found = 0;
283
284 spin_lock_irq(&s->tracked_chunk_lock);
285
Sasha Levinb67bfe02013-02-27 17:06:00 -0800286 hlist_for_each_entry(c,
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100287 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)], node) {
288 if (c->chunk == chunk) {
289 found = 1;
290 break;
291 }
292 }
293
294 spin_unlock_irq(&s->tracked_chunk_lock);
295
296 return found;
297}
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299/*
Mike Snitzer615d1eb2009-12-10 23:52:29 +0000300 * This conflicting I/O is extremely improbable in the caller,
301 * so msleep(1) is sufficient and there is no need for a wait queue.
302 */
303static void __check_for_conflicting_io(struct dm_snapshot *s, chunk_t chunk)
304{
305 while (__chunk_is_tracked(s, chunk))
306 msleep(1);
307}
308
309/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 * One of these per registered origin, held in the snapshot_origins hash
311 */
312struct origin {
313 /* The origin device */
314 struct block_device *bdev;
315
316 struct list_head hash_list;
317
318 /* List of snapshots for this origin */
319 struct list_head snapshots;
320};
321
322/*
Mikulas Patockab735fed2015-02-26 11:40:35 -0500323 * This structure is allocated for each origin target
324 */
325struct dm_origin {
326 struct dm_dev *dev;
327 struct dm_target *ti;
328 unsigned split_boundary;
329 struct list_head hash_list;
330};
331
332/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 * Size of the hash table for origin volumes. If we make this
334 * the size of the minors list then it should be nearly perfect
335 */
336#define ORIGIN_HASH_SIZE 256
337#define ORIGIN_MASK 0xFF
338static struct list_head *_origins;
Mikulas Patockab735fed2015-02-26 11:40:35 -0500339static struct list_head *_dm_origins;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340static struct rw_semaphore _origins_lock;
341
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000342static DECLARE_WAIT_QUEUE_HEAD(_pending_exceptions_done);
343static DEFINE_SPINLOCK(_pending_exceptions_done_spinlock);
344static uint64_t _pending_exceptions_done_count;
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346static int init_origin_hash(void)
347{
348 int i;
349
Kees Cook6da2ec52018-06-12 13:55:00 -0700350 _origins = kmalloc_array(ORIGIN_HASH_SIZE, sizeof(struct list_head),
351 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (!_origins) {
Mikulas Patockab735fed2015-02-26 11:40:35 -0500353 DMERR("unable to allocate memory for _origins");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return -ENOMEM;
355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 for (i = 0; i < ORIGIN_HASH_SIZE; i++)
357 INIT_LIST_HEAD(_origins + i);
Mikulas Patockab735fed2015-02-26 11:40:35 -0500358
Kees Cook6da2ec52018-06-12 13:55:00 -0700359 _dm_origins = kmalloc_array(ORIGIN_HASH_SIZE,
360 sizeof(struct list_head),
361 GFP_KERNEL);
Mikulas Patockab735fed2015-02-26 11:40:35 -0500362 if (!_dm_origins) {
363 DMERR("unable to allocate memory for _dm_origins");
364 kfree(_origins);
365 return -ENOMEM;
366 }
367 for (i = 0; i < ORIGIN_HASH_SIZE; i++)
368 INIT_LIST_HEAD(_dm_origins + i);
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 init_rwsem(&_origins_lock);
371
372 return 0;
373}
374
375static void exit_origin_hash(void)
376{
377 kfree(_origins);
Mikulas Patockab735fed2015-02-26 11:40:35 -0500378 kfree(_dm_origins);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100381static unsigned origin_hash(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
383 return bdev->bd_dev & ORIGIN_MASK;
384}
385
386static struct origin *__lookup_origin(struct block_device *origin)
387{
388 struct list_head *ol;
389 struct origin *o;
390
391 ol = &_origins[origin_hash(origin)];
392 list_for_each_entry (o, ol, hash_list)
393 if (bdev_equal(o->bdev, origin))
394 return o;
395
396 return NULL;
397}
398
399static void __insert_origin(struct origin *o)
400{
401 struct list_head *sl = &_origins[origin_hash(o->bdev)];
402 list_add_tail(&o->hash_list, sl);
403}
404
Mikulas Patockab735fed2015-02-26 11:40:35 -0500405static struct dm_origin *__lookup_dm_origin(struct block_device *origin)
406{
407 struct list_head *ol;
408 struct dm_origin *o;
409
410 ol = &_dm_origins[origin_hash(origin)];
411 list_for_each_entry (o, ol, hash_list)
412 if (bdev_equal(o->dev->bdev, origin))
413 return o;
414
415 return NULL;
416}
417
418static void __insert_dm_origin(struct dm_origin *o)
419{
420 struct list_head *sl = &_dm_origins[origin_hash(o->dev->bdev)];
421 list_add_tail(&o->hash_list, sl);
422}
423
424static void __remove_dm_origin(struct dm_origin *o)
425{
426 list_del(&o->hash_list);
427}
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429/*
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000430 * _origins_lock must be held when calling this function.
431 * Returns number of snapshots registered using the supplied cow device, plus:
432 * snap_src - a snapshot suitable for use as a source of exception handover
433 * snap_dest - a snapshot capable of receiving exception handover.
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000434 * snap_merge - an existing snapshot-merge target linked to the same origin.
435 * There can be at most one snapshot-merge target. The parameter is optional.
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000436 *
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000437 * Possible return values and states of snap_src and snap_dest.
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000438 * 0: NULL, NULL - first new snapshot
439 * 1: snap_src, NULL - normal snapshot
440 * 2: snap_src, snap_dest - waiting for handover
441 * 2: snap_src, NULL - handed over, waiting for old to be deleted
442 * 1: NULL, snap_dest - source got destroyed without handover
443 */
444static int __find_snapshots_sharing_cow(struct dm_snapshot *snap,
445 struct dm_snapshot **snap_src,
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000446 struct dm_snapshot **snap_dest,
447 struct dm_snapshot **snap_merge)
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000448{
449 struct dm_snapshot *s;
450 struct origin *o;
451 int count = 0;
452 int active;
453
454 o = __lookup_origin(snap->origin->bdev);
455 if (!o)
456 goto out;
457
458 list_for_each_entry(s, &o->snapshots, list) {
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000459 if (dm_target_is_snapshot_merge(s->ti) && snap_merge)
460 *snap_merge = s;
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000461 if (!bdev_equal(s->cow->bdev, snap->cow->bdev))
462 continue;
463
Nikos Tsironis4ad8d882019-03-17 14:22:56 +0200464 down_read(&s->lock);
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000465 active = s->active;
Nikos Tsironis4ad8d882019-03-17 14:22:56 +0200466 up_read(&s->lock);
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000467
468 if (active) {
469 if (snap_src)
470 *snap_src = s;
471 } else if (snap_dest)
472 *snap_dest = s;
473
474 count++;
475 }
476
477out:
478 return count;
479}
480
481/*
482 * On success, returns 1 if this snapshot is a handover destination,
483 * otherwise returns 0.
484 */
485static int __validate_exception_handover(struct dm_snapshot *snap)
486{
487 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000488 struct dm_snapshot *snap_merge = NULL;
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000489
490 /* Does snapshot need exceptions handed over to it? */
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000491 if ((__find_snapshots_sharing_cow(snap, &snap_src, &snap_dest,
492 &snap_merge) == 2) ||
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000493 snap_dest) {
494 snap->ti->error = "Snapshot cow pairing for exception "
495 "table handover failed";
496 return -EINVAL;
497 }
498
499 /*
500 * If no snap_src was found, snap cannot become a handover
501 * destination.
502 */
503 if (!snap_src)
504 return 0;
505
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000506 /*
507 * Non-snapshot-merge handover?
508 */
509 if (!dm_target_is_snapshot_merge(snap->ti))
510 return 1;
511
512 /*
513 * Do not allow more than one merging snapshot.
514 */
515 if (snap_merge) {
516 snap->ti->error = "A snapshot is already merging.";
517 return -EINVAL;
518 }
519
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000520 if (!snap_src->store->type->prepare_merge ||
521 !snap_src->store->type->commit_merge) {
522 snap->ti->error = "Snapshot exception store does not "
523 "support snapshot-merge.";
524 return -EINVAL;
525 }
526
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000527 return 1;
528}
529
530static void __insert_snapshot(struct origin *o, struct dm_snapshot *s)
531{
532 struct dm_snapshot *l;
533
534 /* Sort the list according to chunk size, largest-first smallest-last */
535 list_for_each_entry(l, &o->snapshots, list)
536 if (l->store->chunk_size < s->store->chunk_size)
537 break;
538 list_add_tail(&s->list, &l->list);
539}
540
541/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 * Make a note of the snapshot and its origin so we can look it
543 * up when the origin has a write on it.
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000544 *
545 * Also validate snapshot exception store handovers.
546 * On success, returns 1 if this registration is a handover destination,
547 * otherwise returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 */
549static int register_snapshot(struct dm_snapshot *snap)
550{
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000551 struct origin *o, *new_o = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 struct block_device *bdev = snap->origin->bdev;
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000553 int r = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000555 new_o = kmalloc(sizeof(*new_o), GFP_KERNEL);
556 if (!new_o)
557 return -ENOMEM;
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 down_write(&_origins_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000561 r = __validate_exception_handover(snap);
562 if (r < 0) {
563 kfree(new_o);
564 goto out;
565 }
566
567 o = __lookup_origin(bdev);
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000568 if (o)
569 kfree(new_o);
570 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 /* New origin */
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000572 o = new_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 /* Initialise the struct */
575 INIT_LIST_HEAD(&o->snapshots);
576 o->bdev = bdev;
577
578 __insert_origin(o);
579 }
580
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000581 __insert_snapshot(o, snap);
582
583out:
584 up_write(&_origins_lock);
585
586 return r;
587}
588
589/*
590 * Move snapshot to correct place in list according to chunk size.
591 */
592static void reregister_snapshot(struct dm_snapshot *s)
593{
594 struct block_device *bdev = s->origin->bdev;
595
596 down_write(&_origins_lock);
597
598 list_del(&s->list);
599 __insert_snapshot(__lookup_origin(bdev), s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 up_write(&_origins_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603
604static void unregister_snapshot(struct dm_snapshot *s)
605{
606 struct origin *o;
607
608 down_write(&_origins_lock);
609 o = __lookup_origin(s->origin->bdev);
610
611 list_del(&s->list);
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000612 if (o && list_empty(&o->snapshots)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 list_del(&o->hash_list);
614 kfree(o);
615 }
616
617 up_write(&_origins_lock);
618}
619
620/*
621 * Implementation of the exception hash tables.
Milan Brozd74f81f2008-02-08 02:11:27 +0000622 * The lowest hash_shift bits of the chunk number are ignored, allowing
623 * some consecutive chunks to be grouped together.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 */
Nikos Tsironisf79ae412019-03-17 14:22:57 +0200625static uint32_t exception_hash(struct dm_exception_table *et, chunk_t chunk);
626
627/* Lock to protect access to the completed and pending exception hash tables. */
628struct dm_exception_table_lock {
629 struct hlist_bl_head *complete_slot;
630 struct hlist_bl_head *pending_slot;
631};
632
633static void dm_exception_table_lock_init(struct dm_snapshot *s, chunk_t chunk,
634 struct dm_exception_table_lock *lock)
635{
636 struct dm_exception_table *complete = &s->complete;
637 struct dm_exception_table *pending = &s->pending;
638
639 lock->complete_slot = &complete->table[exception_hash(complete, chunk)];
640 lock->pending_slot = &pending->table[exception_hash(pending, chunk)];
641}
642
643static void dm_exception_table_lock(struct dm_exception_table_lock *lock)
644{
645 hlist_bl_lock(lock->complete_slot);
646 hlist_bl_lock(lock->pending_slot);
647}
648
649static void dm_exception_table_unlock(struct dm_exception_table_lock *lock)
650{
651 hlist_bl_unlock(lock->pending_slot);
652 hlist_bl_unlock(lock->complete_slot);
653}
654
Jon Brassow3510cb92009-12-10 23:52:11 +0000655static int dm_exception_table_init(struct dm_exception_table *et,
656 uint32_t size, unsigned hash_shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
658 unsigned int i;
659
Milan Brozd74f81f2008-02-08 02:11:27 +0000660 et->hash_shift = hash_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 et->hash_mask = size - 1;
Nikos Tsironisf79ae412019-03-17 14:22:57 +0200662 et->table = dm_vcalloc(size, sizeof(struct hlist_bl_head));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 if (!et->table)
664 return -ENOMEM;
665
666 for (i = 0; i < size; i++)
Nikos Tsironisf79ae412019-03-17 14:22:57 +0200667 INIT_HLIST_BL_HEAD(et->table + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 return 0;
670}
671
Jon Brassow3510cb92009-12-10 23:52:11 +0000672static void dm_exception_table_exit(struct dm_exception_table *et,
673 struct kmem_cache *mem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Nikos Tsironisf79ae412019-03-17 14:22:57 +0200675 struct hlist_bl_head *slot;
676 struct dm_exception *ex;
677 struct hlist_bl_node *pos, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 int i, size;
679
680 size = et->hash_mask + 1;
681 for (i = 0; i < size; i++) {
682 slot = et->table + i;
683
Nikos Tsironisf79ae412019-03-17 14:22:57 +0200684 hlist_bl_for_each_entry_safe(ex, pos, n, slot, hash_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 kmem_cache_free(mem, ex);
686 }
687
688 vfree(et->table);
689}
690
Jon Brassow191437a2009-12-10 23:52:10 +0000691static uint32_t exception_hash(struct dm_exception_table *et, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
Milan Brozd74f81f2008-02-08 02:11:27 +0000693 return (chunk >> et->hash_shift) & et->hash_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
Jon Brassow3510cb92009-12-10 23:52:11 +0000696static void dm_remove_exception(struct dm_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Nikos Tsironisf79ae412019-03-17 14:22:57 +0200698 hlist_bl_del(&e->hash_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
701/*
702 * Return the exception data for a sector, or NULL if not
703 * remapped.
704 */
Jon Brassow3510cb92009-12-10 23:52:11 +0000705static struct dm_exception *dm_lookup_exception(struct dm_exception_table *et,
706 chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Nikos Tsironisf79ae412019-03-17 14:22:57 +0200708 struct hlist_bl_head *slot;
709 struct hlist_bl_node *pos;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000710 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 slot = &et->table[exception_hash(et, chunk)];
Nikos Tsironisf79ae412019-03-17 14:22:57 +0200713 hlist_bl_for_each_entry(e, pos, slot, hash_list)
Milan Brozd74f81f2008-02-08 02:11:27 +0000714 if (chunk >= e->old_chunk &&
715 chunk <= e->old_chunk + dm_consecutive_chunk_count(e))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return e;
717
718 return NULL;
719}
720
Mikulas Patocka119bc542014-01-13 19:13:36 -0500721static struct dm_exception *alloc_completed_exception(gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Jon Brassow1d4989c2009-12-10 23:52:10 +0000723 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Mikulas Patocka119bc542014-01-13 19:13:36 -0500725 e = kmem_cache_alloc(exception_cache, gfp);
726 if (!e && gfp == GFP_NOIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 e = kmem_cache_alloc(exception_cache, GFP_ATOMIC);
728
729 return e;
730}
731
Jon Brassow3510cb92009-12-10 23:52:11 +0000732static void free_completed_exception(struct dm_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
734 kmem_cache_free(exception_cache, e);
735}
736
Mikulas Patocka92e86812008-07-21 12:00:35 +0100737static struct dm_snap_pending_exception *alloc_pending_exception(struct dm_snapshot *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
Kent Overstreet6f1c8192018-05-20 18:25:53 -0400739 struct dm_snap_pending_exception *pe = mempool_alloc(&s->pending_pool,
Mikulas Patocka92e86812008-07-21 12:00:35 +0100740 GFP_NOIO);
741
Mikulas Patocka879129d22008-10-30 13:33:16 +0000742 atomic_inc(&s->pending_exceptions_count);
Mikulas Patocka92e86812008-07-21 12:00:35 +0100743 pe->snap = s;
744
745 return pe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
747
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100748static void free_pending_exception(struct dm_snap_pending_exception *pe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
Mikulas Patocka879129d22008-10-30 13:33:16 +0000750 struct dm_snapshot *s = pe->snap;
751
Kent Overstreet6f1c8192018-05-20 18:25:53 -0400752 mempool_free(pe, &s->pending_pool);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100753 smp_mb__before_atomic();
Mikulas Patocka879129d22008-10-30 13:33:16 +0000754 atomic_dec(&s->pending_exceptions_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
Jon Brassow3510cb92009-12-10 23:52:11 +0000757static void dm_insert_exception(struct dm_exception_table *eh,
758 struct dm_exception *new_e)
Milan Brozd74f81f2008-02-08 02:11:27 +0000759{
Nikos Tsironisf79ae412019-03-17 14:22:57 +0200760 struct hlist_bl_head *l;
761 struct hlist_bl_node *pos;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000762 struct dm_exception *e = NULL;
Milan Brozd74f81f2008-02-08 02:11:27 +0000763
764 l = &eh->table[exception_hash(eh, new_e->old_chunk)];
765
766 /* Add immediately if this table doesn't support consecutive chunks */
767 if (!eh->hash_shift)
768 goto out;
769
770 /* List is ordered by old_chunk */
Nikos Tsironisf79ae412019-03-17 14:22:57 +0200771 hlist_bl_for_each_entry(e, pos, l, hash_list) {
Milan Brozd74f81f2008-02-08 02:11:27 +0000772 /* Insert after an existing chunk? */
773 if (new_e->old_chunk == (e->old_chunk +
774 dm_consecutive_chunk_count(e) + 1) &&
775 new_e->new_chunk == (dm_chunk_number(e->new_chunk) +
776 dm_consecutive_chunk_count(e) + 1)) {
777 dm_consecutive_chunk_count_inc(e);
Jon Brassow3510cb92009-12-10 23:52:11 +0000778 free_completed_exception(new_e);
Milan Brozd74f81f2008-02-08 02:11:27 +0000779 return;
780 }
781
782 /* Insert before an existing chunk? */
783 if (new_e->old_chunk == (e->old_chunk - 1) &&
784 new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) {
785 dm_consecutive_chunk_count_inc(e);
786 e->old_chunk--;
787 e->new_chunk--;
Jon Brassow3510cb92009-12-10 23:52:11 +0000788 free_completed_exception(new_e);
Milan Brozd74f81f2008-02-08 02:11:27 +0000789 return;
790 }
791
Nikos Tsironisf79ae412019-03-17 14:22:57 +0200792 if (new_e->old_chunk < e->old_chunk)
Milan Brozd74f81f2008-02-08 02:11:27 +0000793 break;
794 }
795
796out:
Nikos Tsironisf79ae412019-03-17 14:22:57 +0200797 if (!e) {
798 /*
799 * Either the table doesn't support consecutive chunks or slot
800 * l is empty.
801 */
802 hlist_bl_add_head(&new_e->hash_list, l);
803 } else if (new_e->old_chunk < e->old_chunk) {
804 /* Add before an existing exception */
805 hlist_bl_add_before(&new_e->hash_list, &e->hash_list);
806 } else {
807 /* Add to l's tail: e is the last exception in this slot */
808 hlist_bl_add_behind(&new_e->hash_list, &e->hash_list);
809 }
Milan Brozd74f81f2008-02-08 02:11:27 +0000810}
811
Jonathan Brassowa159c1a2009-01-06 03:05:19 +0000812/*
813 * Callback used by the exception stores to load exceptions when
814 * initialising.
815 */
816static int dm_add_exception(void *context, chunk_t old, chunk_t new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Nikos Tsironisf79ae412019-03-17 14:22:57 +0200818 struct dm_exception_table_lock lock;
Jonathan Brassowa159c1a2009-01-06 03:05:19 +0000819 struct dm_snapshot *s = context;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000820 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Mikulas Patocka119bc542014-01-13 19:13:36 -0500822 e = alloc_completed_exception(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (!e)
824 return -ENOMEM;
825
826 e->old_chunk = old;
Milan Brozd74f81f2008-02-08 02:11:27 +0000827
828 /* Consecutive_count is implicitly initialised to zero */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 e->new_chunk = new;
Milan Brozd74f81f2008-02-08 02:11:27 +0000830
Nikos Tsironisf79ae412019-03-17 14:22:57 +0200831 /*
832 * Although there is no need to lock access to the exception tables
833 * here, if we don't then hlist_bl_add_head(), called by
834 * dm_insert_exception(), will complain about accessing the
835 * corresponding list without locking it first.
836 */
837 dm_exception_table_lock_init(s, old, &lock);
838
839 dm_exception_table_lock(&lock);
Jon Brassow3510cb92009-12-10 23:52:11 +0000840 dm_insert_exception(&s->complete, e);
Nikos Tsironisf79ae412019-03-17 14:22:57 +0200841 dm_exception_table_unlock(&lock);
Milan Brozd74f81f2008-02-08 02:11:27 +0000842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return 0;
844}
845
Mikulas Patocka7e201b32009-12-10 23:52:08 +0000846/*
847 * Return a minimum chunk size of all snapshots that have the specified origin.
848 * Return zero if the origin has no snapshots.
849 */
Mike Snitzer542f9032012-07-27 15:08:00 +0100850static uint32_t __minimum_chunk_size(struct origin *o)
Mikulas Patocka7e201b32009-12-10 23:52:08 +0000851{
852 struct dm_snapshot *snap;
853 unsigned chunk_size = 0;
854
855 if (o)
856 list_for_each_entry(snap, &o->snapshots, list)
857 chunk_size = min_not_zero(chunk_size,
858 snap->store->chunk_size);
859
Mike Snitzer542f9032012-07-27 15:08:00 +0100860 return (uint32_t) chunk_size;
Mikulas Patocka7e201b32009-12-10 23:52:08 +0000861}
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863/*
864 * Hard coded magic.
865 */
866static int calc_max_buckets(void)
867{
868 /* use a fixed size of 2MB */
869 unsigned long mem = 2 * 1024 * 1024;
Nikos Tsironisf79ae412019-03-17 14:22:57 +0200870 mem /= sizeof(struct hlist_bl_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 return mem;
873}
874
875/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 * Allocate room for a suitable hash table.
877 */
Jonathan Brassowfee19982009-04-02 19:55:34 +0100878static int init_hash_tables(struct dm_snapshot *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
Mikulas Patocka60e356f2013-09-18 19:40:42 -0400880 sector_t hash_size, cow_dev_size, max_buckets;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 /*
883 * Calculate based on the size of the original volume or
884 * the COW volume...
885 */
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000886 cow_dev_size = get_dev_size(s->cow->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 max_buckets = calc_max_buckets();
888
Mikulas Patocka60e356f2013-09-18 19:40:42 -0400889 hash_size = cow_dev_size >> s->store->chunk_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 hash_size = min(hash_size, max_buckets);
891
Mikulas Patocka8e87b9b2009-12-10 23:51:54 +0000892 if (hash_size < 64)
893 hash_size = 64;
Robert P. J. Day8defd832008-02-08 02:10:06 +0000894 hash_size = rounddown_pow_of_two(hash_size);
Jon Brassow3510cb92009-12-10 23:52:11 +0000895 if (dm_exception_table_init(&s->complete, hash_size,
896 DM_CHUNK_CONSECUTIVE_BITS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return -ENOMEM;
898
899 /*
900 * Allocate hash table for in-flight exceptions
901 * Make this smaller than the real hash table
902 */
903 hash_size >>= 3;
904 if (hash_size < 64)
905 hash_size = 64;
906
Jon Brassow3510cb92009-12-10 23:52:11 +0000907 if (dm_exception_table_init(&s->pending, hash_size, 0)) {
908 dm_exception_table_exit(&s->complete, exception_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return -ENOMEM;
910 }
911
912 return 0;
913}
914
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000915static void merge_shutdown(struct dm_snapshot *s)
916{
917 clear_bit_unlock(RUNNING_MERGE, &s->state_bits);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100918 smp_mb__after_atomic();
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000919 wake_up_bit(&s->state_bits, RUNNING_MERGE);
920}
921
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000922static struct bio *__release_queued_bios_after_merge(struct dm_snapshot *s)
923{
924 s->first_merging_chunk = 0;
925 s->num_merging_chunks = 0;
926
927 return bio_list_get(&s->bios_queued_during_merge);
928}
929
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000930/*
931 * Remove one chunk from the index of completed exceptions.
932 */
933static int __remove_single_exception_chunk(struct dm_snapshot *s,
934 chunk_t old_chunk)
935{
936 struct dm_exception *e;
937
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000938 e = dm_lookup_exception(&s->complete, old_chunk);
939 if (!e) {
940 DMERR("Corruption detected: exception for block %llu is "
941 "on disk but not in memory",
942 (unsigned long long)old_chunk);
943 return -EINVAL;
944 }
945
946 /*
947 * If this is the only chunk using this exception, remove exception.
948 */
949 if (!dm_consecutive_chunk_count(e)) {
950 dm_remove_exception(e);
951 free_completed_exception(e);
952 return 0;
953 }
954
955 /*
956 * The chunk may be either at the beginning or the end of a
957 * group of consecutive chunks - never in the middle. We are
958 * removing chunks in the opposite order to that in which they
959 * were added, so this should always be true.
960 * Decrement the consecutive chunk counter and adjust the
961 * starting point if necessary.
962 */
963 if (old_chunk == e->old_chunk) {
964 e->old_chunk++;
965 e->new_chunk++;
966 } else if (old_chunk != e->old_chunk +
967 dm_consecutive_chunk_count(e)) {
968 DMERR("Attempt to merge block %llu from the "
969 "middle of a chunk range [%llu - %llu]",
970 (unsigned long long)old_chunk,
971 (unsigned long long)e->old_chunk,
972 (unsigned long long)
973 e->old_chunk + dm_consecutive_chunk_count(e));
974 return -EINVAL;
975 }
976
977 dm_consecutive_chunk_count_dec(e);
978
979 return 0;
980}
981
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000982static void flush_bios(struct bio *bio);
983
984static int remove_single_exception_chunk(struct dm_snapshot *s)
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000985{
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000986 struct bio *b = NULL;
987 int r;
988 chunk_t old_chunk = s->first_merging_chunk + s->num_merging_chunks - 1;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000989
Nikos Tsironis4ad8d882019-03-17 14:22:56 +0200990 down_write(&s->lock);
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000991
992 /*
993 * Process chunks (and associated exceptions) in reverse order
994 * so that dm_consecutive_chunk_count_dec() accounting works.
995 */
996 do {
997 r = __remove_single_exception_chunk(s, old_chunk);
998 if (r)
999 goto out;
1000 } while (old_chunk-- > s->first_merging_chunk);
1001
1002 b = __release_queued_bios_after_merge(s);
1003
1004out:
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001005 up_write(&s->lock);
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001006 if (b)
1007 flush_bios(b);
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001008
1009 return r;
1010}
1011
Mikulas Patocka73dfd072009-12-10 23:52:34 +00001012static int origin_write_extent(struct dm_snapshot *merging_snap,
1013 sector_t sector, unsigned chunk_size);
1014
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001015static void merge_callback(int read_err, unsigned long write_err,
1016 void *context);
1017
Mikulas Patocka73dfd072009-12-10 23:52:34 +00001018static uint64_t read_pending_exceptions_done_count(void)
1019{
1020 uint64_t pending_exceptions_done;
1021
1022 spin_lock(&_pending_exceptions_done_spinlock);
1023 pending_exceptions_done = _pending_exceptions_done_count;
1024 spin_unlock(&_pending_exceptions_done_spinlock);
1025
1026 return pending_exceptions_done;
1027}
1028
1029static void increment_pending_exceptions_done_count(void)
1030{
1031 spin_lock(&_pending_exceptions_done_spinlock);
1032 _pending_exceptions_done_count++;
1033 spin_unlock(&_pending_exceptions_done_spinlock);
1034
1035 wake_up_all(&_pending_exceptions_done);
1036}
1037
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001038static void snapshot_merge_next_chunks(struct dm_snapshot *s)
1039{
Mike Snitzer8a2d5282009-12-10 23:52:34 +00001040 int i, linear_chunks;
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001041 chunk_t old_chunk, new_chunk;
1042 struct dm_io_region src, dest;
Mike Snitzer8a2d5282009-12-10 23:52:34 +00001043 sector_t io_size;
Mikulas Patocka73dfd072009-12-10 23:52:34 +00001044 uint64_t previous_count;
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001045
1046 BUG_ON(!test_bit(RUNNING_MERGE, &s->state_bits));
1047 if (unlikely(test_bit(SHUTDOWN_MERGE, &s->state_bits)))
1048 goto shut;
1049
1050 /*
1051 * valid flag never changes during merge, so no lock required.
1052 */
1053 if (!s->valid) {
1054 DMERR("Snapshot is invalid: can't merge");
1055 goto shut;
1056 }
1057
Mike Snitzer8a2d5282009-12-10 23:52:34 +00001058 linear_chunks = s->store->type->prepare_merge(s->store, &old_chunk,
1059 &new_chunk);
1060 if (linear_chunks <= 0) {
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +00001061 if (linear_chunks < 0) {
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001062 DMERR("Read error in exception store: "
1063 "shutting down merge");
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001064 down_write(&s->lock);
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +00001065 s->merge_failed = 1;
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001066 up_write(&s->lock);
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +00001067 }
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001068 goto shut;
1069 }
1070
Mike Snitzer8a2d5282009-12-10 23:52:34 +00001071 /* Adjust old_chunk and new_chunk to reflect start of linear region */
1072 old_chunk = old_chunk + 1 - linear_chunks;
1073 new_chunk = new_chunk + 1 - linear_chunks;
1074
1075 /*
1076 * Use one (potentially large) I/O to copy all 'linear_chunks'
1077 * from the exception store to the origin
1078 */
1079 io_size = linear_chunks * s->store->chunk_size;
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001080
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001081 dest.bdev = s->origin->bdev;
1082 dest.sector = chunk_to_sector(s->store, old_chunk);
Mike Snitzer8a2d5282009-12-10 23:52:34 +00001083 dest.count = min(io_size, get_dev_size(dest.bdev) - dest.sector);
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001084
1085 src.bdev = s->cow->bdev;
1086 src.sector = chunk_to_sector(s->store, new_chunk);
1087 src.count = dest.count;
1088
Mikulas Patocka73dfd072009-12-10 23:52:34 +00001089 /*
1090 * Reallocate any exceptions needed in other snapshots then
1091 * wait for the pending exceptions to complete.
1092 * Each time any pending exception (globally on the system)
1093 * completes we are woken and repeat the process to find out
1094 * if we can proceed. While this may not seem a particularly
1095 * efficient algorithm, it is not expected to have any
1096 * significant impact on performance.
1097 */
1098 previous_count = read_pending_exceptions_done_count();
Mike Snitzer8a2d5282009-12-10 23:52:34 +00001099 while (origin_write_extent(s, dest.sector, io_size)) {
Mikulas Patocka73dfd072009-12-10 23:52:34 +00001100 wait_event(_pending_exceptions_done,
1101 (read_pending_exceptions_done_count() !=
1102 previous_count));
1103 /* Retry after the wait, until all exceptions are done. */
1104 previous_count = read_pending_exceptions_done_count();
1105 }
1106
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001107 down_write(&s->lock);
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001108 s->first_merging_chunk = old_chunk;
Mike Snitzer8a2d5282009-12-10 23:52:34 +00001109 s->num_merging_chunks = linear_chunks;
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001110 up_write(&s->lock);
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001111
Mike Snitzer8a2d5282009-12-10 23:52:34 +00001112 /* Wait until writes to all 'linear_chunks' drain */
1113 for (i = 0; i < linear_chunks; i++)
1114 __check_for_conflicting_io(s, old_chunk + i);
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001115
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001116 dm_kcopyd_copy(s->kcopyd_client, &src, 1, &dest, 0, merge_callback, s);
1117 return;
1118
1119shut:
1120 merge_shutdown(s);
1121}
1122
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001123static void error_bios(struct bio *bio);
1124
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001125static void merge_callback(int read_err, unsigned long write_err, void *context)
1126{
1127 struct dm_snapshot *s = context;
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001128 struct bio *b = NULL;
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001129
1130 if (read_err || write_err) {
1131 if (read_err)
1132 DMERR("Read error: shutting down merge.");
1133 else
1134 DMERR("Write error: shutting down merge.");
1135 goto shut;
1136 }
1137
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001138 if (s->store->type->commit_merge(s->store,
1139 s->num_merging_chunks) < 0) {
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001140 DMERR("Write error in exception store: shutting down merge");
1141 goto shut;
1142 }
1143
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001144 if (remove_single_exception_chunk(s) < 0)
1145 goto shut;
1146
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001147 snapshot_merge_next_chunks(s);
1148
1149 return;
1150
1151shut:
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001152 down_write(&s->lock);
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +00001153 s->merge_failed = 1;
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001154 b = __release_queued_bios_after_merge(s);
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001155 up_write(&s->lock);
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001156 error_bios(b);
1157
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001158 merge_shutdown(s);
1159}
1160
1161static void start_merge(struct dm_snapshot *s)
1162{
1163 if (!test_and_set_bit(RUNNING_MERGE, &s->state_bits))
1164 snapshot_merge_next_chunks(s);
1165}
1166
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001167/*
1168 * Stop the merging process and wait until it finishes.
1169 */
1170static void stop_merge(struct dm_snapshot *s)
1171{
1172 set_bit(SHUTDOWN_MERGE, &s->state_bits);
NeilBrown74316202014-07-07 15:16:04 +10001173 wait_on_bit(&s->state_bits, RUNNING_MERGE, TASK_UNINTERRUPTIBLE);
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001174 clear_bit(SHUTDOWN_MERGE, &s->state_bits);
1175}
1176
Mike Snitzer2e602382019-06-19 17:05:54 -04001177static int parse_snapshot_features(struct dm_arg_set *as, struct dm_snapshot *s,
1178 struct dm_target *ti)
1179{
1180 int r;
1181 unsigned argc;
1182 const char *arg_name;
1183
1184 static const struct dm_arg _args[] = {
1185 {0, 2, "Invalid number of feature arguments"},
1186 };
1187
1188 /*
1189 * No feature arguments supplied.
1190 */
1191 if (!as->argc)
1192 return 0;
1193
1194 r = dm_read_arg_group(_args, as, &argc, &ti->error);
1195 if (r)
1196 return -EINVAL;
1197
1198 while (argc && !r) {
1199 arg_name = dm_shift_arg(as);
1200 argc--;
1201
1202 if (!strcasecmp(arg_name, "discard_zeroes_cow"))
1203 s->discard_zeroes_cow = true;
1204
1205 else if (!strcasecmp(arg_name, "discard_passdown_origin"))
1206 s->discard_passdown_origin = true;
1207
1208 else {
1209 ti->error = "Unrecognised feature requested";
1210 r = -EINVAL;
1211 break;
1212 }
1213 }
1214
1215 if (!s->discard_zeroes_cow && s->discard_passdown_origin) {
1216 /*
1217 * TODO: really these are disjoint.. but ti->num_discard_bios
1218 * and dm_bio_get_target_bio_nr() require rigid constraints.
1219 */
1220 ti->error = "discard_passdown_origin feature depends on discard_zeroes_cow";
1221 r = -EINVAL;
1222 }
1223
1224 return r;
1225}
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227/*
Mike Snitzer2e602382019-06-19 17:05:54 -04001228 * Construct a snapshot mapping:
1229 * <origin_dev> <COW-dev> <p|po|n> <chunk-size> [<# feature args> [<arg>]*]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 */
1231static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1232{
1233 struct dm_snapshot *s;
Mike Snitzer2e602382019-06-19 17:05:54 -04001234 struct dm_arg_set as;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001235 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 int r = -EINVAL;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001237 char *origin_path, *cow_path;
DingXiang4df2bf42016-02-02 12:29:18 +08001238 dev_t origin_dev, cow_dev;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001239 unsigned args_used, num_flush_bios = 1;
Mike Snitzer10b81062009-12-10 23:52:31 +00001240 fmode_t origin_mode = FMODE_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Mike Snitzer2e602382019-06-19 17:05:54 -04001242 if (argc < 4) {
1243 ti->error = "requires 4 or more arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 r = -EINVAL;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001245 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
1247
Mike Snitzer10b81062009-12-10 23:52:31 +00001248 if (dm_target_is_snapshot_merge(ti)) {
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001249 num_flush_bios = 2;
Mike Snitzer10b81062009-12-10 23:52:31 +00001250 origin_mode = FMODE_WRITE;
1251 }
1252
Kent Overstreetd3775352018-06-05 05:26:33 -04001253 s = kzalloc(sizeof(*s), GFP_KERNEL);
Jonathan Brassowfee19982009-04-02 19:55:34 +01001254 if (!s) {
Jonathan Brassowa2d2b032011-08-02 12:32:03 +01001255 ti->error = "Cannot allocate private snapshot structure";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 r = -ENOMEM;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001257 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 }
1259
Mike Snitzer2e602382019-06-19 17:05:54 -04001260 as.argc = argc;
1261 as.argv = argv;
1262 dm_consume_args(&as, 4);
1263 r = parse_snapshot_features(&as, s, ti);
1264 if (r)
1265 goto bad_features;
1266
Mikulas Patockac2411042010-08-12 04:13:51 +01001267 origin_path = argv[0];
1268 argv++;
1269 argc--;
1270
1271 r = dm_get_device(ti, origin_path, origin_mode, &s->origin);
1272 if (r) {
1273 ti->error = "Cannot get origin device";
1274 goto bad_origin;
1275 }
DingXiang4df2bf42016-02-02 12:29:18 +08001276 origin_dev = s->origin->bdev->bd_dev;
Mikulas Patockac2411042010-08-12 04:13:51 +01001277
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001278 cow_path = argv[0];
1279 argv++;
1280 argc--;
1281
DingXiang4df2bf42016-02-02 12:29:18 +08001282 cow_dev = dm_get_dev_t(cow_path);
1283 if (cow_dev && cow_dev == origin_dev) {
1284 ti->error = "COW device cannot be the same as origin device";
1285 r = -EINVAL;
1286 goto bad_cow;
1287 }
1288
Milan Broz024d37e2011-03-24 13:52:14 +00001289 r = dm_get_device(ti, cow_path, dm_table_get_mode(ti->table), &s->cow);
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001290 if (r) {
1291 ti->error = "Cannot get COW device";
1292 goto bad_cow;
1293 }
1294
1295 r = dm_exception_store_create(ti, argc, argv, s, &args_used, &s->store);
1296 if (r) {
1297 ti->error = "Couldn't create exception store";
1298 r = -EINVAL;
1299 goto bad_store;
1300 }
1301
1302 argv += args_used;
1303 argc -= args_used;
1304
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001305 s->ti = ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 s->valid = 1;
Mikulas Patocka76c44f62015-06-21 16:31:33 -04001307 s->snapshot_overflowed = 0;
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001308 s->active = 0;
Mikulas Patocka879129d22008-10-30 13:33:16 +00001309 atomic_set(&s->pending_exceptions_count, 0);
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02001310 spin_lock_init(&s->pe_allocation_lock);
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001311 s->exception_start_sequence = 0;
1312 s->exception_complete_sequence = 0;
David Jeffery3db27762018-08-07 16:56:00 -04001313 s->out_of_order_tree = RB_ROOT;
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001314 init_rwsem(&s->lock);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001315 INIT_LIST_HEAD(&s->list);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001316 spin_lock_init(&s->pe_lock);
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001317 s->state_bits = 0;
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +00001318 s->merge_failed = 0;
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001319 s->first_merging_chunk = 0;
1320 s->num_merging_chunks = 0;
1321 bio_list_init(&s->bios_queued_during_merge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 /* Allocate hash table for COW data */
Jonathan Brassowfee19982009-04-02 19:55:34 +01001324 if (init_hash_tables(s)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 ti->error = "Unable to allocate hash table space";
1326 r = -ENOMEM;
Jonathan Brassowfee19982009-04-02 19:55:34 +01001327 goto bad_hash_tables;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 }
1329
Nikos Tsironis721b1d92018-10-31 17:53:08 -04001330 sema_init(&s->cow_count, (cow_threshold > 0) ? cow_threshold : INT_MAX);
1331
Mikulas Patockadf5d2e92013-03-01 22:45:49 +00001332 s->kcopyd_client = dm_kcopyd_client_create(&dm_kcopyd_throttle);
Mikulas Patockafa34ce72011-05-29 13:03:13 +01001333 if (IS_ERR(s->kcopyd_client)) {
1334 r = PTR_ERR(s->kcopyd_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 ti->error = "Could not create kcopyd client";
Jonathan Brassowfee19982009-04-02 19:55:34 +01001336 goto bad_kcopyd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 }
1338
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001339 r = mempool_init_slab_pool(&s->pending_pool, MIN_IOS, pending_cache);
1340 if (r) {
Mikulas Patocka92e86812008-07-21 12:00:35 +01001341 ti->error = "Could not allocate mempool for pending exceptions";
Jonathan Brassowfee19982009-04-02 19:55:34 +01001342 goto bad_pending_pool;
Mikulas Patocka92e86812008-07-21 12:00:35 +01001343 }
1344
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001345 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
1346 INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]);
1347
1348 spin_lock_init(&s->tracked_chunk_lock);
1349
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001350 ti->private = s;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001351 ti->num_flush_bios = num_flush_bios;
Mike Snitzer2e602382019-06-19 17:05:54 -04001352 if (s->discard_zeroes_cow)
1353 ti->num_discard_bios = (s->discard_passdown_origin ? 2 : 1);
Mike Snitzer30187e12016-01-31 13:28:26 -05001354 ti->per_io_data_size = sizeof(struct dm_snap_tracked_chunk);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001355
1356 /* Add snapshot to the list of snapshots for this origin */
1357 /* Exceptions aren't triggered till snapshot_resume() is called */
1358 r = register_snapshot(s);
1359 if (r == -ENOMEM) {
1360 ti->error = "Snapshot origin struct allocation failed";
1361 goto bad_load_and_register;
1362 } else if (r < 0) {
1363 /* invalid handover, register_snapshot has set ti->error */
1364 goto bad_load_and_register;
1365 }
1366
1367 /*
1368 * Metadata must only be loaded into one table at once, so skip this
1369 * if metadata will be handed over during resume.
1370 * Chunk size will be set during the handover - set it to zero to
1371 * ensure it's ignored.
1372 */
1373 if (r > 0) {
1374 s->store->chunk_size = 0;
1375 return 0;
1376 }
1377
Jonathan Brassow493df712009-04-02 19:55:31 +01001378 r = s->store->type->read_metadata(s->store, dm_add_exception,
1379 (void *)s);
Milan Broz07641472007-07-12 17:28:13 +01001380 if (r < 0) {
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -07001381 ti->error = "Failed to read snapshot metadata";
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001382 goto bad_read_metadata;
Milan Broz07641472007-07-12 17:28:13 +01001383 } else if (r > 0) {
1384 s->valid = 0;
1385 DMWARN("Snapshot is marked invalid.");
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -07001386 }
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001387
Mikulas Patocka3f2412d2009-10-16 23:18:16 +01001388 if (!s->store->chunk_size) {
1389 ti->error = "Chunk size not set";
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001390 goto bad_read_metadata;
Mikulas Patocka3f2412d2009-10-16 23:18:16 +01001391 }
Mike Snitzer542f9032012-07-27 15:08:00 +01001392
1393 r = dm_set_target_max_io_len(ti, s->store->chunk_size);
1394 if (r)
1395 goto bad_read_metadata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 return 0;
1398
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001399bad_read_metadata:
1400 unregister_snapshot(s);
Jonathan Brassowfee19982009-04-02 19:55:34 +01001401bad_load_and_register:
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001402 mempool_exit(&s->pending_pool);
Jonathan Brassowfee19982009-04-02 19:55:34 +01001403bad_pending_pool:
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001404 dm_kcopyd_client_destroy(s->kcopyd_client);
Jonathan Brassowfee19982009-04-02 19:55:34 +01001405bad_kcopyd:
Jon Brassow3510cb92009-12-10 23:52:11 +00001406 dm_exception_table_exit(&s->pending, pending_cache);
1407 dm_exception_table_exit(&s->complete, exception_cache);
Jonathan Brassowfee19982009-04-02 19:55:34 +01001408bad_hash_tables:
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001409 dm_exception_store_destroy(s->store);
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001410bad_store:
1411 dm_put_device(ti, s->cow);
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001412bad_cow:
Mikulas Patockac2411042010-08-12 04:13:51 +01001413 dm_put_device(ti, s->origin);
Mikulas Patockac2411042010-08-12 04:13:51 +01001414bad_origin:
Mike Snitzer2e602382019-06-19 17:05:54 -04001415bad_features:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 kfree(s);
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001417bad:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 return r;
1419}
1420
Milan Broz31c93a0c2006-12-08 02:41:11 -08001421static void __free_exceptions(struct dm_snapshot *s)
1422{
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001423 dm_kcopyd_client_destroy(s->kcopyd_client);
Milan Broz31c93a0c2006-12-08 02:41:11 -08001424 s->kcopyd_client = NULL;
1425
Jon Brassow3510cb92009-12-10 23:52:11 +00001426 dm_exception_table_exit(&s->pending, pending_cache);
1427 dm_exception_table_exit(&s->complete, exception_cache);
Milan Broz31c93a0c2006-12-08 02:41:11 -08001428}
1429
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001430static void __handover_exceptions(struct dm_snapshot *snap_src,
1431 struct dm_snapshot *snap_dest)
1432{
1433 union {
1434 struct dm_exception_table table_swap;
1435 struct dm_exception_store *store_swap;
1436 } u;
1437
1438 /*
1439 * Swap all snapshot context information between the two instances.
1440 */
1441 u.table_swap = snap_dest->complete;
1442 snap_dest->complete = snap_src->complete;
1443 snap_src->complete = u.table_swap;
1444
1445 u.store_swap = snap_dest->store;
1446 snap_dest->store = snap_src->store;
Mike Snitzerb0d3cc02015-10-08 18:05:41 -04001447 snap_dest->store->userspace_supports_overflow = u.store_swap->userspace_supports_overflow;
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001448 snap_src->store = u.store_swap;
1449
1450 snap_dest->store->snap = snap_dest;
1451 snap_src->store->snap = snap_src;
1452
Mike Snitzer542f9032012-07-27 15:08:00 +01001453 snap_dest->ti->max_io_len = snap_dest->store->chunk_size;
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001454 snap_dest->valid = snap_src->valid;
Mikulas Patocka76c44f62015-06-21 16:31:33 -04001455 snap_dest->snapshot_overflowed = snap_src->snapshot_overflowed;
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001456
1457 /*
1458 * Set source invalid to ensure it receives no further I/O.
1459 */
1460 snap_src->valid = 0;
1461}
1462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463static void snapshot_dtr(struct dm_target *ti)
1464{
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001465#ifdef CONFIG_DM_DEBUG
1466 int i;
1467#endif
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001468 struct dm_snapshot *s = ti->private;
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001469 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001471 down_read(&_origins_lock);
1472 /* Check whether exception handover must be cancelled */
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +00001473 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001474 if (snap_src && snap_dest && (s == snap_src)) {
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001475 down_write(&snap_dest->lock);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001476 snap_dest->valid = 0;
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001477 up_write(&snap_dest->lock);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001478 DMERR("Cancelling snapshot handover.");
1479 }
1480 up_read(&_origins_lock);
1481
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001482 if (dm_target_is_snapshot_merge(ti))
1483 stop_merge(s);
1484
Alasdair G Kergon138728dc2006-03-27 01:17:50 -08001485 /* Prevent further origin writes from using this snapshot. */
1486 /* After this returns there can be no new kcopyd jobs. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 unregister_snapshot(s);
1488
Mikulas Patocka879129d22008-10-30 13:33:16 +00001489 while (atomic_read(&s->pending_exceptions_count))
Mikulas Patocka90fa1522009-01-06 03:04:54 +00001490 msleep(1);
Mikulas Patocka879129d22008-10-30 13:33:16 +00001491 /*
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001492 * Ensure instructions in mempool_exit aren't reordered
Mikulas Patocka879129d22008-10-30 13:33:16 +00001493 * before atomic_read.
1494 */
1495 smp_mb();
1496
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001497#ifdef CONFIG_DM_DEBUG
1498 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
1499 BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i]));
1500#endif
1501
Milan Broz31c93a0c2006-12-08 02:41:11 -08001502 __free_exceptions(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001504 mempool_exit(&s->pending_pool);
Mikulas Patocka92e86812008-07-21 12:00:35 +01001505
Jonathan Brassowfee19982009-04-02 19:55:34 +01001506 dm_exception_store_destroy(s->store);
Alasdair G Kergon138728dc2006-03-27 01:17:50 -08001507
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001508 dm_put_device(ti, s->cow);
1509
Mikulas Patockac2411042010-08-12 04:13:51 +01001510 dm_put_device(ti, s->origin);
1511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 kfree(s);
1513}
1514
1515/*
1516 * Flush a list of buffers.
1517 */
1518static void flush_bios(struct bio *bio)
1519{
1520 struct bio *n;
1521
1522 while (bio) {
1523 n = bio->bi_next;
1524 bio->bi_next = NULL;
1525 generic_make_request(bio);
1526 bio = n;
1527 }
1528}
1529
Mikulas Patocka515ad662009-12-10 23:52:30 +00001530static int do_origin(struct dm_dev *origin, struct bio *bio);
1531
1532/*
1533 * Flush a list of buffers.
1534 */
1535static void retry_origin_bios(struct dm_snapshot *s, struct bio *bio)
1536{
1537 struct bio *n;
1538 int r;
1539
1540 while (bio) {
1541 n = bio->bi_next;
1542 bio->bi_next = NULL;
1543 r = do_origin(s->origin, bio);
1544 if (r == DM_MAPIO_REMAPPED)
1545 generic_make_request(bio);
1546 bio = n;
1547 }
1548}
1549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550/*
1551 * Error a list of buffers.
1552 */
1553static void error_bios(struct bio *bio)
1554{
1555 struct bio *n;
1556
1557 while (bio) {
1558 n = bio->bi_next;
1559 bio->bi_next = NULL;
NeilBrown6712ecf2007-09-27 12:47:43 +02001560 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 bio = n;
1562 }
1563}
1564
Alasdair G Kergon695368a2006-10-03 01:15:31 -07001565static void __invalidate_snapshot(struct dm_snapshot *s, int err)
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001566{
1567 if (!s->valid)
1568 return;
1569
1570 if (err == -EIO)
1571 DMERR("Invalidating snapshot: Error reading/writing.");
1572 else if (err == -ENOMEM)
1573 DMERR("Invalidating snapshot: Unable to allocate exception.");
1574
Jonathan Brassow493df712009-04-02 19:55:31 +01001575 if (s->store->type->drop_snapshot)
1576 s->store->type->drop_snapshot(s->store);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001577
1578 s->valid = 0;
1579
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001580 dm_table_event(s->ti->table);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001581}
1582
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02001583static void invalidate_snapshot(struct dm_snapshot *s, int err)
1584{
1585 down_write(&s->lock);
1586 __invalidate_snapshot(s, err);
1587 up_write(&s->lock);
1588}
1589
Mikulas Patocka385277b2016-01-08 19:07:55 -05001590static void pending_complete(void *context, int success)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
Mikulas Patocka385277b2016-01-08 19:07:55 -05001592 struct dm_snap_pending_exception *pe = context;
Jon Brassow1d4989c2009-12-10 23:52:10 +00001593 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 struct dm_snapshot *s = pe->snap;
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001595 struct bio *origin_bios = NULL;
1596 struct bio *snapshot_bios = NULL;
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001597 struct bio *full_bio = NULL;
Nikos Tsironisf79ae412019-03-17 14:22:57 +02001598 struct dm_exception_table_lock lock;
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001599 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Nikos Tsironisf79ae412019-03-17 14:22:57 +02001601 dm_exception_table_lock_init(s, pe->e.old_chunk, &lock);
1602
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001603 if (!success) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 /* Read/write error - snapshot is unusable */
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02001605 invalidate_snapshot(s, -EIO);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001606 error = 1;
Nikos Tsironisf79ae412019-03-17 14:22:57 +02001607
1608 dm_exception_table_lock(&lock);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001609 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 }
1611
Mikulas Patocka119bc542014-01-13 19:13:36 -05001612 e = alloc_completed_exception(GFP_NOIO);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001613 if (!e) {
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02001614 invalidate_snapshot(s, -ENOMEM);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001615 error = 1;
Nikos Tsironisf79ae412019-03-17 14:22:57 +02001616
1617 dm_exception_table_lock(&lock);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001618 goto out;
1619 }
1620 *e = pe->e;
1621
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02001622 down_read(&s->lock);
Nikos Tsironisf79ae412019-03-17 14:22:57 +02001623 dm_exception_table_lock(&lock);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001624 if (!s->valid) {
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02001625 up_read(&s->lock);
Jon Brassow3510cb92009-12-10 23:52:11 +00001626 free_completed_exception(e);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001627 error = 1;
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02001628
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001629 goto out;
1630 }
1631
Mikulas Patockaa8d41b52008-07-21 12:00:34 +01001632 /*
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02001633 * Add a proper exception. After inserting the completed exception all
1634 * subsequent snapshot reads to this chunk will be redirected to the
1635 * COW device. This ensures that we do not starve. Moreover, as long
1636 * as the pending exception exists, neither origin writes nor snapshot
1637 * merging can overwrite the chunk in origin.
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001638 */
Jon Brassow3510cb92009-12-10 23:52:11 +00001639 dm_insert_exception(&s->complete, e);
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02001640 up_read(&s->lock);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001641
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02001642 /* Wait for conflicting reads to drain */
1643 if (__chunk_is_tracked(s, pe->e.old_chunk)) {
Nikos Tsironisf79ae412019-03-17 14:22:57 +02001644 dm_exception_table_unlock(&lock);
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02001645 __check_for_conflicting_io(s, pe->e.old_chunk);
Nikos Tsironisf79ae412019-03-17 14:22:57 +02001646 dm_exception_table_lock(&lock);
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02001647 }
1648
Jonathan Brassowa2d2b032011-08-02 12:32:03 +01001649out:
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02001650 /* Remove the in-flight exception from the list */
Jon Brassow3510cb92009-12-10 23:52:11 +00001651 dm_remove_exception(&pe->e);
Nikos Tsironisf79ae412019-03-17 14:22:57 +02001652
1653 dm_exception_table_unlock(&lock);
1654
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001655 snapshot_bios = bio_list_get(&pe->snapshot_bios);
Mikulas Patocka515ad662009-12-10 23:52:30 +00001656 origin_bios = bio_list_get(&pe->origin_bios);
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001657 full_bio = pe->full_bio;
Mikulas Patockafe3265b2015-11-25 16:03:31 -05001658 if (full_bio)
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001659 full_bio->bi_end_io = pe->full_bio_end_io;
Mikulas Patocka73dfd072009-12-10 23:52:34 +00001660 increment_pending_exceptions_done_count();
1661
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001662 /* Submit any pending write bios */
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001663 if (error) {
1664 if (full_bio)
1665 bio_io_error(full_bio);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001666 error_bios(snapshot_bios);
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001667 } else {
1668 if (full_bio)
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001669 bio_endio(full_bio);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001670 flush_bios(snapshot_bios);
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001671 }
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001672
Mikulas Patocka515ad662009-12-10 23:52:30 +00001673 retry_origin_bios(s, origin_bios);
Mikulas Patocka22aa66a2015-02-17 14:34:00 -05001674
1675 free_pending_exception(pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676}
1677
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001678static void complete_exception(struct dm_snap_pending_exception *pe)
1679{
1680 struct dm_snapshot *s = pe->snap;
1681
Mikulas Patocka385277b2016-01-08 19:07:55 -05001682 /* Update the metadata if we are persistent */
1683 s->store->type->commit_exception(s->store, &pe->e, !pe->copy_error,
1684 pending_complete, pe);
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001685}
1686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687/*
1688 * Called when the copy I/O has finished. kcopyd actually runs
1689 * this code so don't block.
1690 */
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -07001691static void copy_callback(int read_err, unsigned long write_err, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001693 struct dm_snap_pending_exception *pe = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 struct dm_snapshot *s = pe->snap;
1695
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001696 pe->copy_error = read_err || write_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001698 if (pe->exception_sequence == s->exception_complete_sequence) {
David Jeffery3db27762018-08-07 16:56:00 -04001699 struct rb_node *next;
1700
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001701 s->exception_complete_sequence++;
1702 complete_exception(pe);
1703
David Jeffery3db27762018-08-07 16:56:00 -04001704 next = rb_first(&s->out_of_order_tree);
1705 while (next) {
1706 pe = rb_entry(next, struct dm_snap_pending_exception,
1707 out_of_order_node);
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001708 if (pe->exception_sequence != s->exception_complete_sequence)
1709 break;
David Jeffery3db27762018-08-07 16:56:00 -04001710 next = rb_next(next);
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001711 s->exception_complete_sequence++;
David Jeffery3db27762018-08-07 16:56:00 -04001712 rb_erase(&pe->out_of_order_node, &s->out_of_order_tree);
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001713 complete_exception(pe);
David Jeffery3db27762018-08-07 16:56:00 -04001714 cond_resched();
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001715 }
1716 } else {
David Jeffery3db27762018-08-07 16:56:00 -04001717 struct rb_node *parent = NULL;
1718 struct rb_node **p = &s->out_of_order_tree.rb_node;
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001719 struct dm_snap_pending_exception *pe2;
1720
David Jeffery3db27762018-08-07 16:56:00 -04001721 while (*p) {
1722 pe2 = rb_entry(*p, struct dm_snap_pending_exception, out_of_order_node);
1723 parent = *p;
1724
1725 BUG_ON(pe->exception_sequence == pe2->exception_sequence);
1726 if (pe->exception_sequence < pe2->exception_sequence)
1727 p = &((*p)->rb_left);
1728 else
1729 p = &((*p)->rb_right);
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001730 }
David Jeffery3db27762018-08-07 16:56:00 -04001731
1732 rb_link_node(&pe->out_of_order_node, parent, p);
1733 rb_insert_color(&pe->out_of_order_node, &s->out_of_order_tree);
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001734 }
Nikos Tsironis721b1d92018-10-31 17:53:08 -04001735 up(&s->cow_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736}
1737
1738/*
1739 * Dispatches the copy operation to kcopyd.
1740 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001741static void start_copy(struct dm_snap_pending_exception *pe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742{
1743 struct dm_snapshot *s = pe->snap;
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +01001744 struct dm_io_region src, dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 struct block_device *bdev = s->origin->bdev;
1746 sector_t dev_size;
1747
1748 dev_size = get_dev_size(bdev);
1749
1750 src.bdev = bdev;
Jonathan Brassow71fab002009-04-02 19:55:33 +01001751 src.sector = chunk_to_sector(s->store, pe->e.old_chunk);
Mikulas Patockadf96eee2009-10-16 23:18:17 +01001752 src.count = min((sector_t)s->store->chunk_size, dev_size - src.sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001754 dest.bdev = s->cow->bdev;
Jonathan Brassow71fab002009-04-02 19:55:33 +01001755 dest.sector = chunk_to_sector(s->store, pe->e.new_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 dest.count = src.count;
1757
1758 /* Hand over to kcopyd */
Nikos Tsironis721b1d92018-10-31 17:53:08 -04001759 down(&s->cow_count);
Jonathan Brassowa2d2b032011-08-02 12:32:03 +01001760 dm_kcopyd_copy(s->kcopyd_client, &src, 1, &dest, 0, copy_callback, pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761}
1762
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001763static void full_bio_end_io(struct bio *bio)
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001764{
1765 void *callback_data = bio->bi_private;
1766
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001767 dm_kcopyd_do_callback(callback_data, 0, bio->bi_status ? 1 : 0);
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001768}
1769
1770static void start_full_bio(struct dm_snap_pending_exception *pe,
1771 struct bio *bio)
1772{
1773 struct dm_snapshot *s = pe->snap;
1774 void *callback_data;
1775
1776 pe->full_bio = bio;
1777 pe->full_bio_end_io = bio->bi_end_io;
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001778
Nikos Tsironis721b1d92018-10-31 17:53:08 -04001779 down(&s->cow_count);
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001780 callback_data = dm_kcopyd_prepare_callback(s->kcopyd_client,
1781 copy_callback, pe);
1782
1783 bio->bi_end_io = full_bio_end_io;
1784 bio->bi_private = callback_data;
1785
1786 generic_make_request(bio);
1787}
1788
Mikulas Patocka29138082009-04-02 19:55:25 +01001789static struct dm_snap_pending_exception *
1790__lookup_pending_exception(struct dm_snapshot *s, chunk_t chunk)
1791{
Jon Brassow3510cb92009-12-10 23:52:11 +00001792 struct dm_exception *e = dm_lookup_exception(&s->pending, chunk);
Mikulas Patocka29138082009-04-02 19:55:25 +01001793
1794 if (!e)
1795 return NULL;
1796
1797 return container_of(e, struct dm_snap_pending_exception, e);
1798}
1799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800/*
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02001801 * Inserts a pending exception into the pending table.
1802 *
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02001803 * NOTE: a write lock must be held on the chunk's pending exception table slot
1804 * before calling this.
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02001805 */
1806static struct dm_snap_pending_exception *
1807__insert_pending_exception(struct dm_snapshot *s,
1808 struct dm_snap_pending_exception *pe, chunk_t chunk)
1809{
1810 pe->e.old_chunk = chunk;
1811 bio_list_init(&pe->origin_bios);
1812 bio_list_init(&pe->snapshot_bios);
1813 pe->started = 0;
1814 pe->full_bio = NULL;
1815
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02001816 spin_lock(&s->pe_allocation_lock);
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02001817 if (s->store->type->prepare_exception(s->store, &pe->e)) {
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02001818 spin_unlock(&s->pe_allocation_lock);
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02001819 free_pending_exception(pe);
1820 return NULL;
1821 }
1822
1823 pe->exception_sequence = s->exception_start_sequence++;
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02001824 spin_unlock(&s->pe_allocation_lock);
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02001825
1826 dm_insert_exception(&s->pending, &pe->e);
1827
1828 return pe;
1829}
1830
1831/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 * Looks to see if this snapshot already has a pending exception
1833 * for this chunk, otherwise it allocates a new one and inserts
1834 * it into the pending table.
1835 *
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02001836 * NOTE: a write lock must be held on the chunk's pending exception table slot
1837 * before calling this.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001839static struct dm_snap_pending_exception *
Mikulas Patockac6621392009-04-02 19:55:25 +01001840__find_pending_exception(struct dm_snapshot *s,
1841 struct dm_snap_pending_exception *pe, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
Mikulas Patockac6621392009-04-02 19:55:25 +01001843 struct dm_snap_pending_exception *pe2;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001844
Mikulas Patocka29138082009-04-02 19:55:25 +01001845 pe2 = __lookup_pending_exception(s, chunk);
1846 if (pe2) {
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001847 free_pending_exception(pe);
Mikulas Patocka29138082009-04-02 19:55:25 +01001848 return pe2;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001849 }
1850
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02001851 return __insert_pending_exception(s, pe, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852}
1853
Jon Brassow1d4989c2009-12-10 23:52:10 +00001854static void remap_exception(struct dm_snapshot *s, struct dm_exception *e,
Milan Brozd74f81f2008-02-08 02:11:27 +00001855 struct bio *bio, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
Christoph Hellwig74d46992017-08-23 19:10:32 +02001857 bio_set_dev(bio, s->cow->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001858 bio->bi_iter.bi_sector =
1859 chunk_to_sector(s->store, dm_chunk_number(e->new_chunk) +
1860 (chunk - e->old_chunk)) +
1861 (bio->bi_iter.bi_sector & s->store->chunk_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862}
1863
Mike Snitzer2e602382019-06-19 17:05:54 -04001864static void zero_callback(int read_err, unsigned long write_err, void *context)
1865{
1866 struct bio *bio = context;
1867 struct dm_snapshot *s = bio->bi_private;
1868
1869 up(&s->cow_count);
1870 bio->bi_status = write_err ? BLK_STS_IOERR : 0;
1871 bio_endio(bio);
1872}
1873
1874static void zero_exception(struct dm_snapshot *s, struct dm_exception *e,
1875 struct bio *bio, chunk_t chunk)
1876{
1877 struct dm_io_region dest;
1878
1879 dest.bdev = s->cow->bdev;
1880 dest.sector = bio->bi_iter.bi_sector;
1881 dest.count = s->store->chunk_size;
1882
1883 down(&s->cow_count);
1884 WARN_ON_ONCE(bio->bi_private);
1885 bio->bi_private = s;
1886 dm_kcopyd_zero(s->kcopyd_client, 1, &dest, 0, zero_callback, bio);
1887}
1888
1889static bool io_overlaps_chunk(struct dm_snapshot *s, struct bio *bio)
1890{
1891 return bio->bi_iter.bi_size ==
1892 (s->store->chunk_size << SECTOR_SHIFT);
1893}
1894
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001895static int snapshot_map(struct dm_target *ti, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896{
Jon Brassow1d4989c2009-12-10 23:52:10 +00001897 struct dm_exception *e;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001898 struct dm_snapshot *s = ti->private;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001899 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 chunk_t chunk;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001901 struct dm_snap_pending_exception *pe = NULL;
Nikos Tsironisf79ae412019-03-17 14:22:57 +02001902 struct dm_exception_table_lock lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Mikulas Patockaee180262012-12-21 20:23:41 +00001904 init_tracked_chunk(bio);
1905
Jens Axboe1eff9d32016-08-05 15:35:16 -06001906 if (bio->bi_opf & REQ_PREFLUSH) {
Christoph Hellwig74d46992017-08-23 19:10:32 +02001907 bio_set_dev(bio, s->cow->bdev);
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01001908 return DM_MAPIO_REMAPPED;
1909 }
1910
Kent Overstreet4f024f32013-10-11 15:44:27 -07001911 chunk = sector_to_chunk(s->store, bio->bi_iter.bi_sector);
Nikos Tsironisf79ae412019-03-17 14:22:57 +02001912 dm_exception_table_lock_init(s, chunk, &lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
1914 /* Full snapshots are not usable */
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001915 /* To get here the table must be live so s->active is always set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 if (!s->valid)
Christoph Hellwig846785e2017-06-03 09:38:02 +02001917 return DM_MAPIO_KILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02001919 down_read(&s->lock);
Nikos Tsironisf79ae412019-03-17 14:22:57 +02001920 dm_exception_table_lock(&lock);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001921
Christoph Hellwig70246282016-07-19 11:28:41 +02001922 if (!s->valid || (unlikely(s->snapshot_overflowed) &&
1923 bio_data_dir(bio) == WRITE)) {
Christoph Hellwig846785e2017-06-03 09:38:02 +02001924 r = DM_MAPIO_KILL;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001925 goto out_unlock;
1926 }
1927
Mike Snitzer2e602382019-06-19 17:05:54 -04001928 if (unlikely(bio_op(bio) == REQ_OP_DISCARD)) {
1929 if (s->discard_passdown_origin && dm_bio_get_target_bio_nr(bio)) {
1930 /*
1931 * passdown discard to origin (without triggering
1932 * snapshot exceptions via do_origin; doing so would
1933 * defeat the goal of freeing space in origin that is
1934 * implied by the "discard_passdown_origin" feature)
1935 */
1936 bio_set_dev(bio, s->origin->bdev);
1937 track_chunk(s, bio, chunk);
1938 goto out_unlock;
1939 }
1940 /* discard to snapshot (target_bio_nr == 0) zeroes exceptions */
1941 }
1942
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001943 /* If the block is already remapped - use that, else remap it */
Jon Brassow3510cb92009-12-10 23:52:11 +00001944 e = dm_lookup_exception(&s->complete, chunk);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001945 if (e) {
Milan Brozd74f81f2008-02-08 02:11:27 +00001946 remap_exception(s, e, bio, chunk);
Mike Snitzer2e602382019-06-19 17:05:54 -04001947 if (unlikely(bio_op(bio) == REQ_OP_DISCARD) &&
1948 io_overlaps_chunk(s, bio)) {
1949 dm_exception_table_unlock(&lock);
1950 up_read(&s->lock);
1951 zero_exception(s, e, bio, chunk);
1952 r = DM_MAPIO_SUBMITTED; /* discard is not issued */
1953 goto out;
1954 }
1955 goto out_unlock;
1956 }
1957
1958 if (unlikely(bio_op(bio) == REQ_OP_DISCARD)) {
1959 /*
1960 * If no exception exists, complete discard immediately
1961 * otherwise it'll trigger copy-out.
1962 */
1963 bio_endio(bio);
1964 r = DM_MAPIO_SUBMITTED;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001965 goto out_unlock;
1966 }
1967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 /*
1969 * Write to snapshot - higher level takes care of RW/RO
1970 * flags so we should only get this if we are
1971 * writeable.
1972 */
Christoph Hellwig70246282016-07-19 11:28:41 +02001973 if (bio_data_dir(bio) == WRITE) {
Mikulas Patocka29138082009-04-02 19:55:25 +01001974 pe = __lookup_pending_exception(s, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001975 if (!pe) {
Nikos Tsironisf79ae412019-03-17 14:22:57 +02001976 dm_exception_table_unlock(&lock);
Mikulas Patockac6621392009-04-02 19:55:25 +01001977 pe = alloc_pending_exception(s);
Nikos Tsironisf79ae412019-03-17 14:22:57 +02001978 dm_exception_table_lock(&lock);
Mikulas Patockac6621392009-04-02 19:55:25 +01001979
Jon Brassow3510cb92009-12-10 23:52:11 +00001980 e = dm_lookup_exception(&s->complete, chunk);
Mikulas Patocka35bf6592009-04-02 19:55:26 +01001981 if (e) {
1982 free_pending_exception(pe);
1983 remap_exception(s, e, bio, chunk);
1984 goto out_unlock;
1985 }
1986
Mikulas Patockac6621392009-04-02 19:55:25 +01001987 pe = __find_pending_exception(s, pe, chunk);
Mikulas Patocka29138082009-04-02 19:55:25 +01001988 if (!pe) {
Nikos Tsironisf79ae412019-03-17 14:22:57 +02001989 dm_exception_table_unlock(&lock);
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02001990 up_read(&s->lock);
1991
1992 down_write(&s->lock);
Nikos Tsironisf79ae412019-03-17 14:22:57 +02001993
Mike Snitzerb0d3cc02015-10-08 18:05:41 -04001994 if (s->store->userspace_supports_overflow) {
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02001995 if (s->valid && !s->snapshot_overflowed) {
1996 s->snapshot_overflowed = 1;
1997 DMERR("Snapshot overflowed: Unable to allocate exception.");
1998 }
Mike Snitzerb0d3cc02015-10-08 18:05:41 -04001999 } else
2000 __invalidate_snapshot(s, -ENOMEM);
Nikos Tsironisf79ae412019-03-17 14:22:57 +02002001 up_write(&s->lock);
2002
Christoph Hellwig846785e2017-06-03 09:38:02 +02002003 r = DM_MAPIO_KILL;
Nikos Tsironisf79ae412019-03-17 14:22:57 +02002004 goto out;
Mikulas Patocka29138082009-04-02 19:55:25 +01002005 }
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002006 }
2007
Milan Brozd74f81f2008-02-08 02:11:27 +00002008 remap_exception(s, &pe->e, bio, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002009
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08002010 r = DM_MAPIO_SUBMITTED;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07002011
Mike Snitzer2e602382019-06-19 17:05:54 -04002012 if (!pe->started && io_overlaps_chunk(s, bio)) {
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01002013 pe->started = 1;
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02002014
Nikos Tsironisf79ae412019-03-17 14:22:57 +02002015 dm_exception_table_unlock(&lock);
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02002016 up_read(&s->lock);
2017
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01002018 start_full_bio(pe, bio);
2019 goto out;
2020 }
2021
2022 bio_list_add(&pe->snapshot_bios, bio);
2023
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002024 if (!pe->started) {
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02002025 /* this is protected by the exception table lock */
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002026 pe->started = 1;
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02002027
Nikos Tsironisf79ae412019-03-17 14:22:57 +02002028 dm_exception_table_unlock(&lock);
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02002029 up_read(&s->lock);
2030
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002031 start_copy(pe);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07002032 goto out;
2033 }
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002034 } else {
Christoph Hellwig74d46992017-08-23 19:10:32 +02002035 bio_set_dev(bio, s->origin->bdev);
Mikulas Patockaee180262012-12-21 20:23:41 +00002036 track_chunk(s, bio, chunk);
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
Jonathan Brassowa2d2b032011-08-02 12:32:03 +01002039out_unlock:
Nikos Tsironisf79ae412019-03-17 14:22:57 +02002040 dm_exception_table_unlock(&lock);
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02002041 up_read(&s->lock);
Jonathan Brassowa2d2b032011-08-02 12:32:03 +01002042out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 return r;
2044}
2045
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00002046/*
2047 * A snapshot-merge target behaves like a combination of a snapshot
2048 * target and a snapshot-origin target. It only generates new
2049 * exceptions in other snapshots and not in the one that is being
2050 * merged.
2051 *
2052 * For each chunk, if there is an existing exception, it is used to
2053 * redirect I/O to the cow device. Otherwise I/O is sent to the origin,
2054 * which in turn might generate exceptions in other snapshots.
Mikulas Patocka9fe862542009-12-10 23:52:33 +00002055 * If merging is currently taking place on the chunk in question, the
2056 * I/O is deferred by adding it to s->bios_queued_during_merge.
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00002057 */
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00002058static int snapshot_merge_map(struct dm_target *ti, struct bio *bio)
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00002059{
2060 struct dm_exception *e;
2061 struct dm_snapshot *s = ti->private;
2062 int r = DM_MAPIO_REMAPPED;
2063 chunk_t chunk;
2064
Mikulas Patockaee180262012-12-21 20:23:41 +00002065 init_tracked_chunk(bio);
2066
Jens Axboe1eff9d32016-08-05 15:35:16 -06002067 if (bio->bi_opf & REQ_PREFLUSH) {
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00002068 if (!dm_bio_get_target_bio_nr(bio))
Christoph Hellwig74d46992017-08-23 19:10:32 +02002069 bio_set_dev(bio, s->origin->bdev);
Mike Snitzer10b81062009-12-10 23:52:31 +00002070 else
Christoph Hellwig74d46992017-08-23 19:10:32 +02002071 bio_set_dev(bio, s->cow->bdev);
Mike Snitzer10b81062009-12-10 23:52:31 +00002072 return DM_MAPIO_REMAPPED;
2073 }
2074
Kent Overstreet4f024f32013-10-11 15:44:27 -07002075 chunk = sector_to_chunk(s->store, bio->bi_iter.bi_sector);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00002076
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02002077 down_write(&s->lock);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00002078
Mikulas Patockad2fdb772009-12-10 23:52:36 +00002079 /* Full merging snapshots are redirected to the origin */
2080 if (!s->valid)
2081 goto redirect_to_origin;
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00002082
2083 /* If the block is already remapped - use that */
2084 e = dm_lookup_exception(&s->complete, chunk);
2085 if (e) {
Mikulas Patocka9fe862542009-12-10 23:52:33 +00002086 /* Queue writes overlapping with chunks being merged */
Christoph Hellwig70246282016-07-19 11:28:41 +02002087 if (bio_data_dir(bio) == WRITE &&
Mikulas Patocka9fe862542009-12-10 23:52:33 +00002088 chunk >= s->first_merging_chunk &&
2089 chunk < (s->first_merging_chunk +
2090 s->num_merging_chunks)) {
Christoph Hellwig74d46992017-08-23 19:10:32 +02002091 bio_set_dev(bio, s->origin->bdev);
Mikulas Patocka9fe862542009-12-10 23:52:33 +00002092 bio_list_add(&s->bios_queued_during_merge, bio);
2093 r = DM_MAPIO_SUBMITTED;
2094 goto out_unlock;
2095 }
Mikulas Patocka17aa0332009-12-10 23:52:33 +00002096
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00002097 remap_exception(s, e, bio, chunk);
Mikulas Patocka17aa0332009-12-10 23:52:33 +00002098
Christoph Hellwig70246282016-07-19 11:28:41 +02002099 if (bio_data_dir(bio) == WRITE)
Mikulas Patockaee180262012-12-21 20:23:41 +00002100 track_chunk(s, bio, chunk);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00002101 goto out_unlock;
2102 }
2103
Mikulas Patockad2fdb772009-12-10 23:52:36 +00002104redirect_to_origin:
Christoph Hellwig74d46992017-08-23 19:10:32 +02002105 bio_set_dev(bio, s->origin->bdev);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00002106
Christoph Hellwig70246282016-07-19 11:28:41 +02002107 if (bio_data_dir(bio) == WRITE) {
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02002108 up_write(&s->lock);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00002109 return do_origin(s->origin, bio);
2110 }
2111
2112out_unlock:
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02002113 up_write(&s->lock);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00002114
2115 return r;
2116}
2117
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002118static int snapshot_end_io(struct dm_target *ti, struct bio *bio,
2119 blk_status_t *error)
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002120{
2121 struct dm_snapshot *s = ti->private;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002122
Mikulas Patockaee180262012-12-21 20:23:41 +00002123 if (is_bio_tracked(bio))
2124 stop_tracking_chunk(s, bio);
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002125
Christoph Hellwig1be56902017-06-03 09:38:03 +02002126 return DM_ENDIO_DONE;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002127}
2128
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002129static void snapshot_merge_presuspend(struct dm_target *ti)
2130{
2131 struct dm_snapshot *s = ti->private;
2132
2133 stop_merge(s);
2134}
2135
Mike Snitzerc1f0c182009-12-10 23:52:24 +00002136static int snapshot_preresume(struct dm_target *ti)
2137{
2138 int r = 0;
2139 struct dm_snapshot *s = ti->private;
2140 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
2141
2142 down_read(&_origins_lock);
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +00002143 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00002144 if (snap_src && snap_dest) {
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02002145 down_read(&snap_src->lock);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00002146 if (s == snap_src) {
2147 DMERR("Unable to resume snapshot source until "
2148 "handover completes.");
2149 r = -EINVAL;
Mike Snitzerb83b2f22011-01-13 19:59:59 +00002150 } else if (!dm_suspended(snap_src->ti)) {
Mike Snitzerc1f0c182009-12-10 23:52:24 +00002151 DMERR("Unable to perform snapshot handover until "
2152 "source is suspended.");
2153 r = -EINVAL;
2154 }
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02002155 up_read(&snap_src->lock);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00002156 }
2157 up_read(&_origins_lock);
2158
2159 return r;
2160}
2161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162static void snapshot_resume(struct dm_target *ti)
2163{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002164 struct dm_snapshot *s = ti->private;
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05002165 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL, *snap_merging = NULL;
Mikulas Patockab735fed2015-02-26 11:40:35 -05002166 struct dm_origin *o;
2167 struct mapped_device *origin_md = NULL;
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05002168 bool must_restart_merging = false;
Mike Snitzerc1f0c182009-12-10 23:52:24 +00002169
2170 down_read(&_origins_lock);
Mikulas Patockab735fed2015-02-26 11:40:35 -05002171
2172 o = __lookup_dm_origin(s->origin->bdev);
2173 if (o)
2174 origin_md = dm_table_get_md(o->ti->table);
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05002175 if (!origin_md) {
2176 (void) __find_snapshots_sharing_cow(s, NULL, NULL, &snap_merging);
2177 if (snap_merging)
2178 origin_md = dm_table_get_md(snap_merging->ti->table);
2179 }
Mikulas Patockab735fed2015-02-26 11:40:35 -05002180 if (origin_md == dm_table_get_md(ti->table))
2181 origin_md = NULL;
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05002182 if (origin_md) {
2183 if (dm_hold(origin_md))
2184 origin_md = NULL;
2185 }
Mikulas Patockab735fed2015-02-26 11:40:35 -05002186
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05002187 up_read(&_origins_lock);
2188
2189 if (origin_md) {
Mikulas Patockab735fed2015-02-26 11:40:35 -05002190 dm_internal_suspend_fast(origin_md);
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05002191 if (snap_merging && test_bit(RUNNING_MERGE, &snap_merging->state_bits)) {
2192 must_restart_merging = true;
2193 stop_merge(snap_merging);
2194 }
2195 }
2196
2197 down_read(&_origins_lock);
Mikulas Patockab735fed2015-02-26 11:40:35 -05002198
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +00002199 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00002200 if (snap_src && snap_dest) {
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02002201 down_write(&snap_src->lock);
2202 down_write_nested(&snap_dest->lock, SINGLE_DEPTH_NESTING);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00002203 __handover_exceptions(snap_src, snap_dest);
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02002204 up_write(&snap_dest->lock);
2205 up_write(&snap_src->lock);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00002206 }
Mikulas Patockab735fed2015-02-26 11:40:35 -05002207
Mike Snitzerc1f0c182009-12-10 23:52:24 +00002208 up_read(&_origins_lock);
2209
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05002210 if (origin_md) {
2211 if (must_restart_merging)
2212 start_merge(snap_merging);
2213 dm_internal_resume_fast(origin_md);
2214 dm_put(origin_md);
2215 }
2216
Mike Snitzerc1f0c182009-12-10 23:52:24 +00002217 /* Now we have correct chunk size, reregister */
2218 reregister_snapshot(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02002220 down_write(&s->lock);
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08002221 s->active = 1;
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02002222 up_write(&s->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223}
2224
Mike Snitzer542f9032012-07-27 15:08:00 +01002225static uint32_t get_origin_minimum_chunksize(struct block_device *bdev)
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002226{
Mike Snitzer542f9032012-07-27 15:08:00 +01002227 uint32_t min_chunksize;
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002228
2229 down_read(&_origins_lock);
2230 min_chunksize = __minimum_chunk_size(__lookup_origin(bdev));
2231 up_read(&_origins_lock);
2232
2233 return min_chunksize;
2234}
2235
2236static void snapshot_merge_resume(struct dm_target *ti)
2237{
2238 struct dm_snapshot *s = ti->private;
2239
2240 /*
2241 * Handover exceptions from existing snapshot.
2242 */
2243 snapshot_resume(ti);
2244
2245 /*
Mike Snitzer542f9032012-07-27 15:08:00 +01002246 * snapshot-merge acts as an origin, so set ti->max_io_len
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002247 */
Mike Snitzer542f9032012-07-27 15:08:00 +01002248 ti->max_io_len = get_origin_minimum_chunksize(s->origin->bdev);
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002249
2250 start_merge(s);
2251}
2252
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002253static void snapshot_status(struct dm_target *ti, status_type_t type,
2254 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255{
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01002256 unsigned sz = 0;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002257 struct dm_snapshot *snap = ti->private;
Mike Snitzer2e602382019-06-19 17:05:54 -04002258 unsigned num_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
2260 switch (type) {
2261 case STATUSTYPE_INFO:
Mikulas Patocka94e765722009-12-10 23:51:53 +00002262
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02002263 down_write(&snap->lock);
Mikulas Patocka94e765722009-12-10 23:51:53 +00002264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 if (!snap->valid)
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01002266 DMEMIT("Invalid");
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +00002267 else if (snap->merge_failed)
2268 DMEMIT("Merge failed");
Mikulas Patocka76c44f62015-06-21 16:31:33 -04002269 else if (snap->snapshot_overflowed)
2270 DMEMIT("Overflow");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 else {
Mike Snitzer985903b2009-12-10 23:52:11 +00002272 if (snap->store->type->usage) {
2273 sector_t total_sectors, sectors_allocated,
2274 metadata_sectors;
2275 snap->store->type->usage(snap->store,
2276 &total_sectors,
2277 &sectors_allocated,
2278 &metadata_sectors);
2279 DMEMIT("%llu/%llu %llu",
2280 (unsigned long long)sectors_allocated,
2281 (unsigned long long)total_sectors,
2282 (unsigned long long)metadata_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 }
2284 else
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01002285 DMEMIT("Unknown");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 }
Mikulas Patocka94e765722009-12-10 23:51:53 +00002287
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02002288 up_write(&snap->lock);
Mikulas Patocka94e765722009-12-10 23:51:53 +00002289
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 break;
2291
2292 case STATUSTYPE_TABLE:
2293 /*
2294 * kdevname returns a static pointer so we need
2295 * to make private copies if the output is to
2296 * make sense.
2297 */
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00002298 DMEMIT("%s %s", snap->origin->name, snap->cow->name);
Mike Snitzer2e602382019-06-19 17:05:54 -04002299 sz += snap->store->type->status(snap->store, type, result + sz,
2300 maxlen - sz);
2301 num_features = snap->discard_zeroes_cow + snap->discard_passdown_origin;
2302 if (num_features) {
2303 DMEMIT(" %u", num_features);
2304 if (snap->discard_zeroes_cow)
2305 DMEMIT(" discard_zeroes_cow");
2306 if (snap->discard_passdown_origin)
2307 DMEMIT(" discard_passdown_origin");
2308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 break;
2310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311}
2312
Mike Snitzer8811f462009-09-04 20:40:19 +01002313static int snapshot_iterate_devices(struct dm_target *ti,
2314 iterate_devices_callout_fn fn, void *data)
2315{
2316 struct dm_snapshot *snap = ti->private;
Mikulas Patocka1e5554c2010-08-12 04:13:50 +01002317 int r;
Mike Snitzer8811f462009-09-04 20:40:19 +01002318
Mikulas Patocka1e5554c2010-08-12 04:13:50 +01002319 r = fn(ti, snap->origin, 0, ti->len, data);
2320
2321 if (!r)
2322 r = fn(ti, snap->cow, 0, get_dev_size(snap->cow->bdev), data);
2323
2324 return r;
Mike Snitzer8811f462009-09-04 20:40:19 +01002325}
2326
Mike Snitzer2e602382019-06-19 17:05:54 -04002327static void snapshot_io_hints(struct dm_target *ti, struct queue_limits *limits)
2328{
2329 struct dm_snapshot *snap = ti->private;
2330
2331 if (snap->discard_zeroes_cow) {
2332 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
2333
2334 (void) __find_snapshots_sharing_cow(snap, &snap_src, &snap_dest, NULL);
2335 if (snap_src && snap_dest)
2336 snap = snap_src;
2337
2338 /* All discards are split on chunk_size boundary */
2339 limits->discard_granularity = snap->store->chunk_size;
2340 limits->max_discard_sectors = snap->store->chunk_size;
2341 }
2342}
Mike Snitzer8811f462009-09-04 20:40:19 +01002343
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344/*-----------------------------------------------------------------
2345 * Origin methods
2346 *---------------------------------------------------------------*/
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00002347
2348/*
2349 * If no exceptions need creating, DM_MAPIO_REMAPPED is returned and any
2350 * supplied bio was ignored. The caller may submit it immediately.
2351 * (No remapping actually occurs as the origin is always a direct linear
2352 * map.)
2353 *
2354 * If further exceptions are required, DM_MAPIO_SUBMITTED is returned
2355 * and any supplied bio is added to a list to be submitted once all
2356 * the necessary exceptions exist.
2357 */
2358static int __origin_write(struct list_head *snapshots, sector_t sector,
2359 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360{
Mikulas Patocka515ad662009-12-10 23:52:30 +00002361 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 struct dm_snapshot *snap;
Jon Brassow1d4989c2009-12-10 23:52:10 +00002363 struct dm_exception *e;
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02002364 struct dm_snap_pending_exception *pe, *pe2;
Mikulas Patocka515ad662009-12-10 23:52:30 +00002365 struct dm_snap_pending_exception *pe_to_start_now = NULL;
2366 struct dm_snap_pending_exception *pe_to_start_last = NULL;
Nikos Tsironisf79ae412019-03-17 14:22:57 +02002367 struct dm_exception_table_lock lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 chunk_t chunk;
2369
2370 /* Do all the snapshots on this origin */
2371 list_for_each_entry (snap, snapshots, list) {
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00002372 /*
2373 * Don't make new exceptions in a merging snapshot
2374 * because it has effectively been deleted
2375 */
2376 if (dm_target_is_snapshot_merge(snap->ti))
2377 continue;
2378
Alasdair G Kergond5e404c2005-07-12 15:53:05 -07002379 /* Nothing to do if writing beyond end of snapshot */
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00002380 if (sector >= dm_table_get_size(snap->ti->table))
Nikos Tsironisf79ae412019-03-17 14:22:57 +02002381 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
2383 /*
2384 * Remember, different snapshots can have
2385 * different chunk sizes.
2386 */
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00002387 chunk = sector_to_chunk(snap->store, sector);
Nikos Tsironisf79ae412019-03-17 14:22:57 +02002388 dm_exception_table_lock_init(snap, chunk, &lock);
2389
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02002390 down_read(&snap->lock);
Nikos Tsironisf79ae412019-03-17 14:22:57 +02002391 dm_exception_table_lock(&lock);
2392
2393 /* Only deal with valid and active snapshots */
2394 if (!snap->valid || !snap->active)
2395 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
Mikulas Patocka29138082009-04-02 19:55:25 +01002397 pe = __lookup_pending_exception(snap, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002398 if (!pe) {
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02002399 /*
2400 * Check exception table to see if block is already
2401 * remapped in this snapshot and trigger an exception
2402 * if not.
2403 */
2404 e = dm_lookup_exception(&snap->complete, chunk);
2405 if (e)
2406 goto next_snapshot;
2407
Nikos Tsironisf79ae412019-03-17 14:22:57 +02002408 dm_exception_table_unlock(&lock);
Mikulas Patockac6621392009-04-02 19:55:25 +01002409 pe = alloc_pending_exception(snap);
Nikos Tsironisf79ae412019-03-17 14:22:57 +02002410 dm_exception_table_lock(&lock);
Mikulas Patockac6621392009-04-02 19:55:25 +01002411
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02002412 pe2 = __lookup_pending_exception(snap, chunk);
Mikulas Patocka35bf6592009-04-02 19:55:26 +01002413
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02002414 if (!pe2) {
2415 e = dm_lookup_exception(&snap->complete, chunk);
2416 if (e) {
2417 free_pending_exception(pe);
2418 goto next_snapshot;
2419 }
2420
2421 pe = __insert_pending_exception(snap, pe, chunk);
2422 if (!pe) {
Nikos Tsironisf79ae412019-03-17 14:22:57 +02002423 dm_exception_table_unlock(&lock);
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02002424 up_read(&snap->lock);
Nikos Tsironisf79ae412019-03-17 14:22:57 +02002425
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02002426 invalidate_snapshot(snap, -ENOMEM);
Nikos Tsironisf79ae412019-03-17 14:22:57 +02002427 continue;
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02002428 }
2429 } else {
2430 free_pending_exception(pe);
2431 pe = pe2;
Mikulas Patocka29138082009-04-02 19:55:25 +01002432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 }
2434
Mikulas Patocka515ad662009-12-10 23:52:30 +00002435 r = DM_MAPIO_SUBMITTED;
2436
2437 /*
2438 * If an origin bio was supplied, queue it to wait for the
2439 * completion of this exception, and start this one last,
2440 * at the end of the function.
2441 */
2442 if (bio) {
2443 bio_list_add(&pe->origin_bios, bio);
2444 bio = NULL;
2445
2446 if (!pe->started) {
2447 pe->started = 1;
2448 pe_to_start_last = pe;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002449 }
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002450 }
2451
2452 if (!pe->started) {
2453 pe->started = 1;
Mikulas Patocka515ad662009-12-10 23:52:30 +00002454 pe_to_start_now = pe;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002455 }
2456
Jonathan Brassowa2d2b032011-08-02 12:32:03 +01002457next_snapshot:
Nikos Tsironisf79ae412019-03-17 14:22:57 +02002458 dm_exception_table_unlock(&lock);
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02002459 up_read(&snap->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Mikulas Patocka515ad662009-12-10 23:52:30 +00002461 if (pe_to_start_now) {
2462 start_copy(pe_to_start_now);
2463 pe_to_start_now = NULL;
2464 }
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08002465 }
2466
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 /*
Mikulas Patocka515ad662009-12-10 23:52:30 +00002468 * Submit the exception against which the bio is queued last,
2469 * to give the other exceptions a head start.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 */
Mikulas Patocka515ad662009-12-10 23:52:30 +00002471 if (pe_to_start_last)
2472 start_copy(pe_to_start_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
2474 return r;
2475}
2476
2477/*
2478 * Called on a write from the origin driver.
2479 */
2480static int do_origin(struct dm_dev *origin, struct bio *bio)
2481{
2482 struct origin *o;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08002483 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
2485 down_read(&_origins_lock);
2486 o = __lookup_origin(origin->bdev);
2487 if (o)
Kent Overstreet4f024f32013-10-11 15:44:27 -07002488 r = __origin_write(&o->snapshots, bio->bi_iter.bi_sector, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 up_read(&_origins_lock);
2490
2491 return r;
2492}
2493
2494/*
Mikulas Patocka73dfd072009-12-10 23:52:34 +00002495 * Trigger exceptions in all non-merging snapshots.
2496 *
2497 * The chunk size of the merging snapshot may be larger than the chunk
2498 * size of some other snapshot so we may need to reallocate multiple
2499 * chunks in other snapshots.
2500 *
2501 * We scan all the overlapping exceptions in the other snapshots.
2502 * Returns 1 if anything was reallocated and must be waited for,
2503 * otherwise returns 0.
2504 *
2505 * size must be a multiple of merging_snap's chunk_size.
2506 */
2507static int origin_write_extent(struct dm_snapshot *merging_snap,
2508 sector_t sector, unsigned size)
2509{
2510 int must_wait = 0;
2511 sector_t n;
2512 struct origin *o;
2513
2514 /*
Mike Snitzer542f9032012-07-27 15:08:00 +01002515 * The origin's __minimum_chunk_size() got stored in max_io_len
Mikulas Patocka73dfd072009-12-10 23:52:34 +00002516 * by snapshot_merge_resume().
2517 */
2518 down_read(&_origins_lock);
2519 o = __lookup_origin(merging_snap->origin->bdev);
Mike Snitzer542f9032012-07-27 15:08:00 +01002520 for (n = 0; n < size; n += merging_snap->ti->max_io_len)
Mikulas Patocka73dfd072009-12-10 23:52:34 +00002521 if (__origin_write(&o->snapshots, sector + n, NULL) ==
2522 DM_MAPIO_SUBMITTED)
2523 must_wait = 1;
2524 up_read(&_origins_lock);
2525
2526 return must_wait;
2527}
2528
2529/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 * Origin: maps a linear range of a device, with hooks for snapshotting.
2531 */
2532
2533/*
2534 * Construct an origin mapping: <dev_path>
2535 * The context for an origin is merely a 'struct dm_dev *'
2536 * pointing to the real device.
2537 */
2538static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
2539{
2540 int r;
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002541 struct dm_origin *o;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
2543 if (argc != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002544 ti->error = "origin: incorrect number of arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 return -EINVAL;
2546 }
2547
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002548 o = kmalloc(sizeof(struct dm_origin), GFP_KERNEL);
2549 if (!o) {
2550 ti->error = "Cannot allocate private origin structure";
2551 r = -ENOMEM;
2552 goto bad_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 }
2554
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002555 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &o->dev);
2556 if (r) {
2557 ti->error = "Cannot get target device";
2558 goto bad_open;
2559 }
2560
Mikulas Patockab735fed2015-02-26 11:40:35 -05002561 o->ti = ti;
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002562 ti->private = o;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00002563 ti->num_flush_bios = 1;
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01002564
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 return 0;
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002566
2567bad_open:
2568 kfree(o);
2569bad_alloc:
2570 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571}
2572
2573static void origin_dtr(struct dm_target *ti)
2574{
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002575 struct dm_origin *o = ti->private;
Mikulas Patockab735fed2015-02-26 11:40:35 -05002576
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002577 dm_put_device(ti, o->dev);
2578 kfree(o);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579}
2580
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00002581static int origin_map(struct dm_target *ti, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582{
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002583 struct dm_origin *o = ti->private;
Mikulas Patocka298eaa82014-03-14 18:43:07 -04002584 unsigned available_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
Christoph Hellwig74d46992017-08-23 19:10:32 +02002586 bio_set_dev(bio, o->dev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
Jens Axboe1eff9d32016-08-05 15:35:16 -06002588 if (unlikely(bio->bi_opf & REQ_PREFLUSH))
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01002589 return DM_MAPIO_REMAPPED;
2590
Christoph Hellwig70246282016-07-19 11:28:41 +02002591 if (bio_data_dir(bio) != WRITE)
Mikulas Patocka298eaa82014-03-14 18:43:07 -04002592 return DM_MAPIO_REMAPPED;
2593
2594 available_sectors = o->split_boundary -
2595 ((unsigned)bio->bi_iter.bi_sector & (o->split_boundary - 1));
2596
2597 if (bio_sectors(bio) > available_sectors)
2598 dm_accept_partial_bio(bio, available_sectors);
2599
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 /* Only tell snapshots if this is a write */
Mikulas Patocka298eaa82014-03-14 18:43:07 -04002601 return do_origin(o->dev, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602}
2603
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604/*
Mike Snitzer542f9032012-07-27 15:08:00 +01002605 * Set the target "max_io_len" field to the minimum of all the snapshots'
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 * chunk sizes.
2607 */
2608static void origin_resume(struct dm_target *ti)
2609{
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002610 struct dm_origin *o = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
Mikulas Patocka298eaa82014-03-14 18:43:07 -04002612 o->split_boundary = get_origin_minimum_chunksize(o->dev->bdev);
Mikulas Patockab735fed2015-02-26 11:40:35 -05002613
2614 down_write(&_origins_lock);
2615 __insert_dm_origin(o);
2616 up_write(&_origins_lock);
2617}
2618
2619static void origin_postsuspend(struct dm_target *ti)
2620{
2621 struct dm_origin *o = ti->private;
2622
2623 down_write(&_origins_lock);
2624 __remove_dm_origin(o);
2625 up_write(&_origins_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626}
2627
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002628static void origin_status(struct dm_target *ti, status_type_t type,
2629 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630{
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002631 struct dm_origin *o = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
2633 switch (type) {
2634 case STATUSTYPE_INFO:
2635 result[0] = '\0';
2636 break;
2637
2638 case STATUSTYPE_TABLE:
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002639 snprintf(result, maxlen, "%s", o->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 break;
2641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642}
2643
Mike Snitzer8811f462009-09-04 20:40:19 +01002644static int origin_iterate_devices(struct dm_target *ti,
2645 iterate_devices_callout_fn fn, void *data)
2646{
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002647 struct dm_origin *o = ti->private;
Mike Snitzer8811f462009-09-04 20:40:19 +01002648
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002649 return fn(ti, o->dev, 0, ti->len, data);
Mike Snitzer8811f462009-09-04 20:40:19 +01002650}
2651
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652static struct target_type origin_target = {
2653 .name = "snapshot-origin",
Mikulas Patockab735fed2015-02-26 11:40:35 -05002654 .version = {1, 9, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 .module = THIS_MODULE,
2656 .ctr = origin_ctr,
2657 .dtr = origin_dtr,
2658 .map = origin_map,
2659 .resume = origin_resume,
Mikulas Patockab735fed2015-02-26 11:40:35 -05002660 .postsuspend = origin_postsuspend,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 .status = origin_status,
Mike Snitzer8811f462009-09-04 20:40:19 +01002662 .iterate_devices = origin_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663};
2664
2665static struct target_type snapshot_target = {
2666 .name = "snapshot",
Mike Snitzer2e602382019-06-19 17:05:54 -04002667 .version = {1, 16, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 .module = THIS_MODULE,
2669 .ctr = snapshot_ctr,
2670 .dtr = snapshot_dtr,
2671 .map = snapshot_map,
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002672 .end_io = snapshot_end_io,
Mike Snitzerc1f0c182009-12-10 23:52:24 +00002673 .preresume = snapshot_preresume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 .resume = snapshot_resume,
2675 .status = snapshot_status,
Mike Snitzer8811f462009-09-04 20:40:19 +01002676 .iterate_devices = snapshot_iterate_devices,
Mike Snitzer2e602382019-06-19 17:05:54 -04002677 .io_hints = snapshot_io_hints,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678};
2679
Mikulas Patockad698aa42009-12-10 23:52:30 +00002680static struct target_type merge_target = {
2681 .name = dm_snapshot_merge_target_name,
Mike Snitzer2e602382019-06-19 17:05:54 -04002682 .version = {1, 5, 0},
Mikulas Patockad698aa42009-12-10 23:52:30 +00002683 .module = THIS_MODULE,
2684 .ctr = snapshot_ctr,
2685 .dtr = snapshot_dtr,
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00002686 .map = snapshot_merge_map,
Mikulas Patockad698aa42009-12-10 23:52:30 +00002687 .end_io = snapshot_end_io,
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002688 .presuspend = snapshot_merge_presuspend,
Mikulas Patockad698aa42009-12-10 23:52:30 +00002689 .preresume = snapshot_preresume,
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002690 .resume = snapshot_merge_resume,
Mikulas Patockad698aa42009-12-10 23:52:30 +00002691 .status = snapshot_status,
2692 .iterate_devices = snapshot_iterate_devices,
Mike Snitzer2e602382019-06-19 17:05:54 -04002693 .io_hints = snapshot_io_hints,
Mikulas Patockad698aa42009-12-10 23:52:30 +00002694};
2695
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696static int __init dm_snapshot_init(void)
2697{
2698 int r;
2699
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00002700 r = dm_exception_store_init();
2701 if (r) {
2702 DMERR("Failed to initialize exception stores");
2703 return r;
2704 }
2705
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 r = init_origin_hash();
2707 if (r) {
2708 DMERR("init_origin_hash failed.");
Mikulas Patockad698aa42009-12-10 23:52:30 +00002709 goto bad_origin_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 }
2711
Jon Brassow1d4989c2009-12-10 23:52:10 +00002712 exception_cache = KMEM_CACHE(dm_exception, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 if (!exception_cache) {
2714 DMERR("Couldn't create exception cache.");
2715 r = -ENOMEM;
Mikulas Patockad698aa42009-12-10 23:52:30 +00002716 goto bad_exception_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 }
2718
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002719 pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 if (!pending_cache) {
2721 DMERR("Couldn't create pending cache.");
2722 r = -ENOMEM;
Mikulas Patockad698aa42009-12-10 23:52:30 +00002723 goto bad_pending_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 }
2725
monty_pavel@sina.com7e6358d2017-11-25 01:43:50 +08002726 r = dm_register_target(&snapshot_target);
2727 if (r < 0) {
2728 DMERR("snapshot target register failed %d", r);
2729 goto bad_register_snapshot_target;
2730 }
2731
2732 r = dm_register_target(&origin_target);
2733 if (r < 0) {
2734 DMERR("Origin target register failed %d", r);
2735 goto bad_register_origin_target;
2736 }
2737
2738 r = dm_register_target(&merge_target);
2739 if (r < 0) {
2740 DMERR("Merge target register failed %d", r);
2741 goto bad_register_merge_target;
2742 }
2743
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 return 0;
2745
Mikulas Patockad698aa42009-12-10 23:52:30 +00002746bad_register_merge_target:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 dm_unregister_target(&origin_target);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002748bad_register_origin_target:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 dm_unregister_target(&snapshot_target);
Jonathan Brassow034a1862009-10-16 23:18:14 +01002750bad_register_snapshot_target:
monty_pavel@sina.com7e6358d2017-11-25 01:43:50 +08002751 kmem_cache_destroy(pending_cache);
2752bad_pending_cache:
2753 kmem_cache_destroy(exception_cache);
2754bad_exception_cache:
2755 exit_origin_hash();
2756bad_origin_hash:
Jonathan Brassow034a1862009-10-16 23:18:14 +01002757 dm_exception_store_exit();
Mikulas Patockad698aa42009-12-10 23:52:30 +00002758
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 return r;
2760}
2761
2762static void __exit dm_snapshot_exit(void)
2763{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00002764 dm_unregister_target(&snapshot_target);
2765 dm_unregister_target(&origin_target);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002766 dm_unregister_target(&merge_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
2768 exit_origin_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 kmem_cache_destroy(pending_cache);
2770 kmem_cache_destroy(exception_cache);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00002771
2772 dm_exception_store_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773}
2774
2775/* Module hooks */
2776module_init(dm_snapshot_init);
2777module_exit(dm_snapshot_exit);
2778
2779MODULE_DESCRIPTION(DM_NAME " snapshot target");
2780MODULE_AUTHOR("Joe Thornber");
2781MODULE_LICENSE("GPL");
Mikulas Patocka23cb2102013-03-01 22:45:47 +00002782MODULE_ALIAS("dm-snapshot-origin");
2783MODULE_ALIAS("dm-snapshot-merge");