blob: dcf34c6b05ad35763b09bdaa11857f959c50e320 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Mikulas Patockab735fed2015-02-26 11:40:35 -050022#include "dm.h"
23
Jonathan Brassowaea53d92009-01-06 03:05:15 +000024#include "dm-exception-store.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Alasdair G Kergon72d94862006-06-26 00:27:35 -070026#define DM_MSG_PREFIX "snapshots"
27
Mikulas Patockad698aa42009-12-10 23:52:30 +000028static const char dm_snapshot_merge_target_name[] = "snapshot-merge";
29
30#define dm_target_is_snapshot_merge(ti) \
31 ((ti)->type->name == dm_snapshot_merge_target_name)
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/*
Mikulas Patockacd45daf2008-07-21 12:00:32 +010034 * The size of the mempool used to track chunks in use.
35 */
36#define MIN_IOS 256
37
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010038#define DM_TRACKED_CHUNK_HASH_SIZE 16
39#define DM_TRACKED_CHUNK_HASH(x) ((unsigned long)(x) & \
40 (DM_TRACKED_CHUNK_HASH_SIZE - 1))
41
Jon Brassow191437a2009-12-10 23:52:10 +000042struct dm_exception_table {
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010043 uint32_t hash_mask;
44 unsigned hash_shift;
Nikos Tsironisf79ae412019-03-17 14:22:57 +020045 struct hlist_bl_head *table;
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010046};
47
48struct dm_snapshot {
Nikos Tsironis4ad8d882019-03-17 14:22:56 +020049 struct rw_semaphore lock;
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010050
51 struct dm_dev *origin;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +000052 struct dm_dev *cow;
53
54 struct dm_target *ti;
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010055
56 /* List of snapshots per Origin */
57 struct list_head list;
58
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +000059 /*
60 * You can't use a snapshot if this is 0 (e.g. if full).
61 * A snapshot-merge target never clears this.
62 */
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010063 int valid;
64
Mikulas Patocka76c44f62015-06-21 16:31:33 -040065 /*
66 * The snapshot overflowed because of a write to the snapshot device.
67 * We don't have to invalidate the snapshot in this case, but we need
68 * to prevent further writes.
69 */
70 int snapshot_overflowed;
71
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010072 /* Origin writes don't trigger exceptions until this is set */
73 int active;
74
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010075 atomic_t pending_exceptions_count;
76
Nikos Tsironis3f1637f2019-03-17 14:22:58 +020077 spinlock_t pe_allocation_lock;
78
79 /* Protected by "pe_allocation_lock" */
Mikulas Patocka230c83a2013-11-29 18:13:37 -050080 sector_t exception_start_sequence;
81
82 /* Protected by kcopyd single-threaded callback */
83 sector_t exception_complete_sequence;
84
85 /*
86 * A list of pending exceptions that completed out of order.
87 * Protected by kcopyd single-threaded callback.
88 */
David Jeffery3db27762018-08-07 16:56:00 -040089 struct rb_root out_of_order_tree;
Mikulas Patocka230c83a2013-11-29 18:13:37 -050090
Kent Overstreet6f1c8192018-05-20 18:25:53 -040091 mempool_t pending_pool;
Mike Snitzer924e6002010-03-06 02:32:33 +000092
Jon Brassow191437a2009-12-10 23:52:10 +000093 struct dm_exception_table pending;
94 struct dm_exception_table complete;
Jonathan Brassowccc45ea2009-04-02 19:55:34 +010095
96 /*
97 * pe_lock protects all pending_exception operations and access
98 * as well as the snapshot_bios list.
99 */
100 spinlock_t pe_lock;
101
Mike Snitzer924e6002010-03-06 02:32:33 +0000102 /* Chunks with outstanding reads */
103 spinlock_t tracked_chunk_lock;
Mike Snitzer924e6002010-03-06 02:32:33 +0000104 struct hlist_head tracked_chunk_hash[DM_TRACKED_CHUNK_HASH_SIZE];
105
Jonathan Brassowccc45ea2009-04-02 19:55:34 +0100106 /* The on disk metadata handler */
107 struct dm_exception_store *store;
108
Mikulas Patockab2155572019-10-02 06:15:53 -0400109 unsigned in_progress;
110 struct wait_queue_head in_progress_wait;
Nikos Tsironis721b1d92018-10-31 17:53:08 -0400111
Jonathan Brassowccc45ea2009-04-02 19:55:34 +0100112 struct dm_kcopyd_client *kcopyd_client;
113
Mike Snitzer924e6002010-03-06 02:32:33 +0000114 /* Wait for events based on state_bits */
115 unsigned long state_bits;
116
117 /* Range of chunks currently being merged. */
118 chunk_t first_merging_chunk;
119 int num_merging_chunks;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000120
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +0000121 /*
122 * The merge operation failed if this flag is set.
123 * Failure modes are handled as follows:
124 * - I/O error reading the header
125 * => don't load the target; abort.
126 * - Header does not have "valid" flag set
127 * => use the origin; forget about the snapshot.
128 * - I/O error when reading exceptions
129 * => don't load the target; abort.
130 * (We can't use the intermediate origin state.)
131 * - I/O error while merging
132 * => stop merging; set merge_failed; process I/O normally.
133 */
Mike Snitzer2e602382019-06-19 17:05:54 -0400134 bool merge_failed:1;
135
136 bool discard_zeroes_cow:1;
137 bool discard_passdown_origin:1;
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +0000138
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000139 /*
140 * Incoming bios that overlap with chunks being merged must wait
141 * for them to be committed.
142 */
143 struct bio_list bios_queued_during_merge;
Akilesh Kailashfcc42332020-12-28 07:14:07 +0000144
145 /*
146 * Flush data after merge.
147 */
148 struct bio flush_bio;
Jonathan Brassowccc45ea2009-04-02 19:55:34 +0100149};
150
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000151/*
152 * state_bits:
153 * RUNNING_MERGE - Merge operation is in progress.
154 * SHUTDOWN_MERGE - Set to signal that merge needs to be stopped;
155 * cleared afterwards.
156 */
157#define RUNNING_MERGE 0
158#define SHUTDOWN_MERGE 1
159
Nikos Tsironis721b1d92018-10-31 17:53:08 -0400160/*
161 * Maximum number of chunks being copied on write.
162 *
163 * The value was decided experimentally as a trade-off between memory
164 * consumption, stalling the kernel's workqueues and maintaining a high enough
165 * throughput.
166 */
167#define DEFAULT_COW_THRESHOLD 2048
168
Mikulas Patockab2155572019-10-02 06:15:53 -0400169static unsigned cow_threshold = DEFAULT_COW_THRESHOLD;
170module_param_named(snapshot_cow_threshold, cow_threshold, uint, 0644);
Nikos Tsironis721b1d92018-10-31 17:53:08 -0400171MODULE_PARM_DESC(snapshot_cow_threshold, "Maximum number of chunks being copied on write");
172
Mikulas Patockadf5d2e92013-03-01 22:45:49 +0000173DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(snapshot_copy_throttle,
174 "A percentage of time allocated for copy on write");
175
Mikulas Patockac2411042010-08-12 04:13:51 +0100176struct dm_dev *dm_snap_origin(struct dm_snapshot *s)
177{
178 return s->origin;
179}
180EXPORT_SYMBOL(dm_snap_origin);
181
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000182struct dm_dev *dm_snap_cow(struct dm_snapshot *s)
183{
184 return s->cow;
185}
186EXPORT_SYMBOL(dm_snap_cow);
187
Jonathan Brassowccc45ea2009-04-02 19:55:34 +0100188static sector_t chunk_to_sector(struct dm_exception_store *store,
189 chunk_t chunk)
190{
191 return chunk << store->chunk_shift;
192}
193
194static int bdev_equal(struct block_device *lhs, struct block_device *rhs)
195{
196 /*
197 * There is only ever one instance of a particular block
198 * device so we can compare pointers safely.
199 */
200 return lhs == rhs;
201}
202
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100203struct dm_snap_pending_exception {
Jon Brassow1d4989c2009-12-10 23:52:10 +0000204 struct dm_exception e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 /*
207 * Origin buffers waiting for this to complete are held
208 * in a bio list
209 */
210 struct bio_list origin_bios;
211 struct bio_list snapshot_bios;
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /* Pointer back to snapshot context */
214 struct dm_snapshot *snap;
215
216 /*
217 * 1 indicates the exception has already been sent to
218 * kcopyd.
219 */
220 int started;
Mikulas Patockaa6e50b42011-08-02 12:32:04 +0100221
Mikulas Patocka230c83a2013-11-29 18:13:37 -0500222 /* There was copying error. */
223 int copy_error;
224
225 /* A sequence number, it is used for in-order completion. */
226 sector_t exception_sequence;
227
David Jeffery3db27762018-08-07 16:56:00 -0400228 struct rb_node out_of_order_node;
Mikulas Patocka230c83a2013-11-29 18:13:37 -0500229
Mikulas Patockaa6e50b42011-08-02 12:32:04 +0100230 /*
231 * For writing a complete chunk, bypassing the copy.
232 */
233 struct bio *full_bio;
234 bio_end_io_t *full_bio_end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235};
236
237/*
238 * Hash table mapping origin volumes to lists of snapshots and
239 * a lock to protect it
240 */
Christoph Lametere18b8902006-12-06 20:33:20 -0800241static struct kmem_cache *exception_cache;
242static struct kmem_cache *pending_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100244struct dm_snap_tracked_chunk {
245 struct hlist_node node;
246 chunk_t chunk;
247};
248
Mikulas Patockaee180262012-12-21 20:23:41 +0000249static void init_tracked_chunk(struct bio *bio)
250{
251 struct dm_snap_tracked_chunk *c = dm_per_bio_data(bio, sizeof(struct dm_snap_tracked_chunk));
252 INIT_HLIST_NODE(&c->node);
253}
254
255static bool is_bio_tracked(struct bio *bio)
256{
257 struct dm_snap_tracked_chunk *c = dm_per_bio_data(bio, sizeof(struct dm_snap_tracked_chunk));
258 return !hlist_unhashed(&c->node);
259}
260
261static void track_chunk(struct dm_snapshot *s, struct bio *bio, chunk_t chunk)
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100262{
Mikulas Patocka42bc9542012-12-21 20:23:38 +0000263 struct dm_snap_tracked_chunk *c = dm_per_bio_data(bio, sizeof(struct dm_snap_tracked_chunk));
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100264
265 c->chunk = chunk;
266
Mikulas Patocka9aa0c0e2012-12-21 20:23:33 +0000267 spin_lock_irq(&s->tracked_chunk_lock);
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100268 hlist_add_head(&c->node,
269 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)]);
Mikulas Patocka9aa0c0e2012-12-21 20:23:33 +0000270 spin_unlock_irq(&s->tracked_chunk_lock);
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100271}
272
Mikulas Patockaee180262012-12-21 20:23:41 +0000273static void stop_tracking_chunk(struct dm_snapshot *s, struct bio *bio)
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100274{
Mikulas Patockaee180262012-12-21 20:23:41 +0000275 struct dm_snap_tracked_chunk *c = dm_per_bio_data(bio, sizeof(struct dm_snap_tracked_chunk));
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100276 unsigned long flags;
277
278 spin_lock_irqsave(&s->tracked_chunk_lock, flags);
279 hlist_del(&c->node);
280 spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
Mikulas Patockacd45daf2008-07-21 12:00:32 +0100281}
282
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100283static int __chunk_is_tracked(struct dm_snapshot *s, chunk_t chunk)
284{
285 struct dm_snap_tracked_chunk *c;
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100286 int found = 0;
287
288 spin_lock_irq(&s->tracked_chunk_lock);
289
Sasha Levinb67bfe02013-02-27 17:06:00 -0800290 hlist_for_each_entry(c,
Mikulas Patockaa8d41b52008-07-21 12:00:34 +0100291 &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)], node) {
292 if (c->chunk == chunk) {
293 found = 1;
294 break;
295 }
296 }
297
298 spin_unlock_irq(&s->tracked_chunk_lock);
299
300 return found;
301}
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303/*
Mike Snitzer615d1eb2009-12-10 23:52:29 +0000304 * This conflicting I/O is extremely improbable in the caller,
305 * so msleep(1) is sufficient and there is no need for a wait queue.
306 */
307static void __check_for_conflicting_io(struct dm_snapshot *s, chunk_t chunk)
308{
309 while (__chunk_is_tracked(s, chunk))
310 msleep(1);
311}
312
313/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 * One of these per registered origin, held in the snapshot_origins hash
315 */
316struct origin {
317 /* The origin device */
318 struct block_device *bdev;
319
320 struct list_head hash_list;
321
322 /* List of snapshots for this origin */
323 struct list_head snapshots;
324};
325
326/*
Mikulas Patockab735fed2015-02-26 11:40:35 -0500327 * This structure is allocated for each origin target
328 */
329struct dm_origin {
330 struct dm_dev *dev;
331 struct dm_target *ti;
332 unsigned split_boundary;
333 struct list_head hash_list;
334};
335
336/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 * Size of the hash table for origin volumes. If we make this
338 * the size of the minors list then it should be nearly perfect
339 */
340#define ORIGIN_HASH_SIZE 256
341#define ORIGIN_MASK 0xFF
342static struct list_head *_origins;
Mikulas Patockab735fed2015-02-26 11:40:35 -0500343static struct list_head *_dm_origins;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344static struct rw_semaphore _origins_lock;
345
Mikulas Patocka73dfd072009-12-10 23:52:34 +0000346static DECLARE_WAIT_QUEUE_HEAD(_pending_exceptions_done);
347static DEFINE_SPINLOCK(_pending_exceptions_done_spinlock);
348static uint64_t _pending_exceptions_done_count;
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350static int init_origin_hash(void)
351{
352 int i;
353
Kees Cook6da2ec52018-06-12 13:55:00 -0700354 _origins = kmalloc_array(ORIGIN_HASH_SIZE, sizeof(struct list_head),
355 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (!_origins) {
Mikulas Patockab735fed2015-02-26 11:40:35 -0500357 DMERR("unable to allocate memory for _origins");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return -ENOMEM;
359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 for (i = 0; i < ORIGIN_HASH_SIZE; i++)
361 INIT_LIST_HEAD(_origins + i);
Mikulas Patockab735fed2015-02-26 11:40:35 -0500362
Kees Cook6da2ec52018-06-12 13:55:00 -0700363 _dm_origins = kmalloc_array(ORIGIN_HASH_SIZE,
364 sizeof(struct list_head),
365 GFP_KERNEL);
Mikulas Patockab735fed2015-02-26 11:40:35 -0500366 if (!_dm_origins) {
367 DMERR("unable to allocate memory for _dm_origins");
368 kfree(_origins);
369 return -ENOMEM;
370 }
371 for (i = 0; i < ORIGIN_HASH_SIZE; i++)
372 INIT_LIST_HEAD(_dm_origins + i);
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 init_rwsem(&_origins_lock);
375
376 return 0;
377}
378
379static void exit_origin_hash(void)
380{
381 kfree(_origins);
Mikulas Patockab735fed2015-02-26 11:40:35 -0500382 kfree(_dm_origins);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100385static unsigned origin_hash(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
387 return bdev->bd_dev & ORIGIN_MASK;
388}
389
390static struct origin *__lookup_origin(struct block_device *origin)
391{
392 struct list_head *ol;
393 struct origin *o;
394
395 ol = &_origins[origin_hash(origin)];
396 list_for_each_entry (o, ol, hash_list)
397 if (bdev_equal(o->bdev, origin))
398 return o;
399
400 return NULL;
401}
402
403static void __insert_origin(struct origin *o)
404{
405 struct list_head *sl = &_origins[origin_hash(o->bdev)];
406 list_add_tail(&o->hash_list, sl);
407}
408
Mikulas Patockab735fed2015-02-26 11:40:35 -0500409static struct dm_origin *__lookup_dm_origin(struct block_device *origin)
410{
411 struct list_head *ol;
412 struct dm_origin *o;
413
414 ol = &_dm_origins[origin_hash(origin)];
415 list_for_each_entry (o, ol, hash_list)
416 if (bdev_equal(o->dev->bdev, origin))
417 return o;
418
419 return NULL;
420}
421
422static void __insert_dm_origin(struct dm_origin *o)
423{
424 struct list_head *sl = &_dm_origins[origin_hash(o->dev->bdev)];
425 list_add_tail(&o->hash_list, sl);
426}
427
428static void __remove_dm_origin(struct dm_origin *o)
429{
430 list_del(&o->hash_list);
431}
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433/*
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000434 * _origins_lock must be held when calling this function.
435 * Returns number of snapshots registered using the supplied cow device, plus:
436 * snap_src - a snapshot suitable for use as a source of exception handover
437 * snap_dest - a snapshot capable of receiving exception handover.
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000438 * snap_merge - an existing snapshot-merge target linked to the same origin.
439 * There can be at most one snapshot-merge target. The parameter is optional.
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000440 *
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000441 * Possible return values and states of snap_src and snap_dest.
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000442 * 0: NULL, NULL - first new snapshot
443 * 1: snap_src, NULL - normal snapshot
444 * 2: snap_src, snap_dest - waiting for handover
445 * 2: snap_src, NULL - handed over, waiting for old to be deleted
446 * 1: NULL, snap_dest - source got destroyed without handover
447 */
448static int __find_snapshots_sharing_cow(struct dm_snapshot *snap,
449 struct dm_snapshot **snap_src,
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000450 struct dm_snapshot **snap_dest,
451 struct dm_snapshot **snap_merge)
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000452{
453 struct dm_snapshot *s;
454 struct origin *o;
455 int count = 0;
456 int active;
457
458 o = __lookup_origin(snap->origin->bdev);
459 if (!o)
460 goto out;
461
462 list_for_each_entry(s, &o->snapshots, list) {
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000463 if (dm_target_is_snapshot_merge(s->ti) && snap_merge)
464 *snap_merge = s;
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000465 if (!bdev_equal(s->cow->bdev, snap->cow->bdev))
466 continue;
467
Nikos Tsironis4ad8d882019-03-17 14:22:56 +0200468 down_read(&s->lock);
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000469 active = s->active;
Nikos Tsironis4ad8d882019-03-17 14:22:56 +0200470 up_read(&s->lock);
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000471
472 if (active) {
473 if (snap_src)
474 *snap_src = s;
475 } else if (snap_dest)
476 *snap_dest = s;
477
478 count++;
479 }
480
481out:
482 return count;
483}
484
485/*
486 * On success, returns 1 if this snapshot is a handover destination,
487 * otherwise returns 0.
488 */
489static int __validate_exception_handover(struct dm_snapshot *snap)
490{
491 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000492 struct dm_snapshot *snap_merge = NULL;
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000493
494 /* Does snapshot need exceptions handed over to it? */
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000495 if ((__find_snapshots_sharing_cow(snap, &snap_src, &snap_dest,
496 &snap_merge) == 2) ||
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000497 snap_dest) {
498 snap->ti->error = "Snapshot cow pairing for exception "
499 "table handover failed";
500 return -EINVAL;
501 }
502
503 /*
504 * If no snap_src was found, snap cannot become a handover
505 * destination.
506 */
507 if (!snap_src)
508 return 0;
509
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +0000510 /*
511 * Non-snapshot-merge handover?
512 */
513 if (!dm_target_is_snapshot_merge(snap->ti))
514 return 1;
515
516 /*
517 * Do not allow more than one merging snapshot.
518 */
519 if (snap_merge) {
520 snap->ti->error = "A snapshot is already merging.";
521 return -EINVAL;
522 }
523
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000524 if (!snap_src->store->type->prepare_merge ||
525 !snap_src->store->type->commit_merge) {
526 snap->ti->error = "Snapshot exception store does not "
527 "support snapshot-merge.";
528 return -EINVAL;
529 }
530
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000531 return 1;
532}
533
534static void __insert_snapshot(struct origin *o, struct dm_snapshot *s)
535{
536 struct dm_snapshot *l;
537
538 /* Sort the list according to chunk size, largest-first smallest-last */
539 list_for_each_entry(l, &o->snapshots, list)
540 if (l->store->chunk_size < s->store->chunk_size)
541 break;
542 list_add_tail(&s->list, &l->list);
543}
544
545/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 * Make a note of the snapshot and its origin so we can look it
547 * up when the origin has a write on it.
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000548 *
549 * Also validate snapshot exception store handovers.
550 * On success, returns 1 if this registration is a handover destination,
551 * otherwise returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 */
553static int register_snapshot(struct dm_snapshot *snap)
554{
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000555 struct origin *o, *new_o = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 struct block_device *bdev = snap->origin->bdev;
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000557 int r = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000559 new_o = kmalloc(sizeof(*new_o), GFP_KERNEL);
560 if (!new_o)
561 return -ENOMEM;
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 down_write(&_origins_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000565 r = __validate_exception_handover(snap);
566 if (r < 0) {
567 kfree(new_o);
568 goto out;
569 }
570
571 o = __lookup_origin(bdev);
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000572 if (o)
573 kfree(new_o);
574 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 /* New origin */
Mikulas Patocka60c856c82008-10-30 13:33:12 +0000576 o = new_o;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 /* Initialise the struct */
579 INIT_LIST_HEAD(&o->snapshots);
580 o->bdev = bdev;
581
582 __insert_origin(o);
583 }
584
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000585 __insert_snapshot(o, snap);
586
587out:
588 up_write(&_origins_lock);
589
590 return r;
591}
592
593/*
594 * Move snapshot to correct place in list according to chunk size.
595 */
596static void reregister_snapshot(struct dm_snapshot *s)
597{
598 struct block_device *bdev = s->origin->bdev;
599
600 down_write(&_origins_lock);
601
602 list_del(&s->list);
603 __insert_snapshot(__lookup_origin(bdev), s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 up_write(&_origins_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
608static void unregister_snapshot(struct dm_snapshot *s)
609{
610 struct origin *o;
611
612 down_write(&_origins_lock);
613 o = __lookup_origin(s->origin->bdev);
614
615 list_del(&s->list);
Mike Snitzerc1f0c182009-12-10 23:52:24 +0000616 if (o && list_empty(&o->snapshots)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 list_del(&o->hash_list);
618 kfree(o);
619 }
620
621 up_write(&_origins_lock);
622}
623
624/*
625 * Implementation of the exception hash tables.
Milan Brozd74f81f2008-02-08 02:11:27 +0000626 * The lowest hash_shift bits of the chunk number are ignored, allowing
627 * some consecutive chunks to be grouped together.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 */
Nikos Tsironisf79ae412019-03-17 14:22:57 +0200629static uint32_t exception_hash(struct dm_exception_table *et, chunk_t chunk);
630
631/* Lock to protect access to the completed and pending exception hash tables. */
632struct dm_exception_table_lock {
633 struct hlist_bl_head *complete_slot;
634 struct hlist_bl_head *pending_slot;
635};
636
637static void dm_exception_table_lock_init(struct dm_snapshot *s, chunk_t chunk,
638 struct dm_exception_table_lock *lock)
639{
640 struct dm_exception_table *complete = &s->complete;
641 struct dm_exception_table *pending = &s->pending;
642
643 lock->complete_slot = &complete->table[exception_hash(complete, chunk)];
644 lock->pending_slot = &pending->table[exception_hash(pending, chunk)];
645}
646
647static void dm_exception_table_lock(struct dm_exception_table_lock *lock)
648{
649 hlist_bl_lock(lock->complete_slot);
650 hlist_bl_lock(lock->pending_slot);
651}
652
653static void dm_exception_table_unlock(struct dm_exception_table_lock *lock)
654{
655 hlist_bl_unlock(lock->pending_slot);
656 hlist_bl_unlock(lock->complete_slot);
657}
658
Jon Brassow3510cb92009-12-10 23:52:11 +0000659static int dm_exception_table_init(struct dm_exception_table *et,
660 uint32_t size, unsigned hash_shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
662 unsigned int i;
663
Milan Brozd74f81f2008-02-08 02:11:27 +0000664 et->hash_shift = hash_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 et->hash_mask = size - 1;
Matthew Wilcox (Oracle)7a356932021-04-07 14:25:22 +0100666 et->table = kvmalloc_array(size, sizeof(struct hlist_bl_head),
667 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (!et->table)
669 return -ENOMEM;
670
671 for (i = 0; i < size; i++)
Nikos Tsironisf79ae412019-03-17 14:22:57 +0200672 INIT_HLIST_BL_HEAD(et->table + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 return 0;
675}
676
Jon Brassow3510cb92009-12-10 23:52:11 +0000677static void dm_exception_table_exit(struct dm_exception_table *et,
678 struct kmem_cache *mem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Nikos Tsironisf79ae412019-03-17 14:22:57 +0200680 struct hlist_bl_head *slot;
681 struct dm_exception *ex;
682 struct hlist_bl_node *pos, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 int i, size;
684
685 size = et->hash_mask + 1;
686 for (i = 0; i < size; i++) {
687 slot = et->table + i;
688
Nikos Tsironisf79ae412019-03-17 14:22:57 +0200689 hlist_bl_for_each_entry_safe(ex, pos, n, slot, hash_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 kmem_cache_free(mem, ex);
691 }
692
Matthew Wilcox (Oracle)7a356932021-04-07 14:25:22 +0100693 kvfree(et->table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
Jon Brassow191437a2009-12-10 23:52:10 +0000696static uint32_t exception_hash(struct dm_exception_table *et, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Milan Brozd74f81f2008-02-08 02:11:27 +0000698 return (chunk >> et->hash_shift) & et->hash_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
Jon Brassow3510cb92009-12-10 23:52:11 +0000701static void dm_remove_exception(struct dm_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Nikos Tsironisf79ae412019-03-17 14:22:57 +0200703 hlist_bl_del(&e->hash_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
706/*
707 * Return the exception data for a sector, or NULL if not
708 * remapped.
709 */
Jon Brassow3510cb92009-12-10 23:52:11 +0000710static struct dm_exception *dm_lookup_exception(struct dm_exception_table *et,
711 chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Nikos Tsironisf79ae412019-03-17 14:22:57 +0200713 struct hlist_bl_head *slot;
714 struct hlist_bl_node *pos;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000715 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 slot = &et->table[exception_hash(et, chunk)];
Nikos Tsironisf79ae412019-03-17 14:22:57 +0200718 hlist_bl_for_each_entry(e, pos, slot, hash_list)
Milan Brozd74f81f2008-02-08 02:11:27 +0000719 if (chunk >= e->old_chunk &&
720 chunk <= e->old_chunk + dm_consecutive_chunk_count(e))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return e;
722
723 return NULL;
724}
725
Mikulas Patocka119bc542014-01-13 19:13:36 -0500726static struct dm_exception *alloc_completed_exception(gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
Jon Brassow1d4989c2009-12-10 23:52:10 +0000728 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Mikulas Patocka119bc542014-01-13 19:13:36 -0500730 e = kmem_cache_alloc(exception_cache, gfp);
731 if (!e && gfp == GFP_NOIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 e = kmem_cache_alloc(exception_cache, GFP_ATOMIC);
733
734 return e;
735}
736
Jon Brassow3510cb92009-12-10 23:52:11 +0000737static void free_completed_exception(struct dm_exception *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
739 kmem_cache_free(exception_cache, e);
740}
741
Mikulas Patocka92e86812008-07-21 12:00:35 +0100742static struct dm_snap_pending_exception *alloc_pending_exception(struct dm_snapshot *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Kent Overstreet6f1c8192018-05-20 18:25:53 -0400744 struct dm_snap_pending_exception *pe = mempool_alloc(&s->pending_pool,
Mikulas Patocka92e86812008-07-21 12:00:35 +0100745 GFP_NOIO);
746
Mikulas Patocka879129d22008-10-30 13:33:16 +0000747 atomic_inc(&s->pending_exceptions_count);
Mikulas Patocka92e86812008-07-21 12:00:35 +0100748 pe->snap = s;
749
750 return pe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100753static void free_pending_exception(struct dm_snap_pending_exception *pe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
Mikulas Patocka879129d22008-10-30 13:33:16 +0000755 struct dm_snapshot *s = pe->snap;
756
Kent Overstreet6f1c8192018-05-20 18:25:53 -0400757 mempool_free(pe, &s->pending_pool);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100758 smp_mb__before_atomic();
Mikulas Patocka879129d22008-10-30 13:33:16 +0000759 atomic_dec(&s->pending_exceptions_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
Jon Brassow3510cb92009-12-10 23:52:11 +0000762static void dm_insert_exception(struct dm_exception_table *eh,
763 struct dm_exception *new_e)
Milan Brozd74f81f2008-02-08 02:11:27 +0000764{
Nikos Tsironisf79ae412019-03-17 14:22:57 +0200765 struct hlist_bl_head *l;
766 struct hlist_bl_node *pos;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000767 struct dm_exception *e = NULL;
Milan Brozd74f81f2008-02-08 02:11:27 +0000768
769 l = &eh->table[exception_hash(eh, new_e->old_chunk)];
770
771 /* Add immediately if this table doesn't support consecutive chunks */
772 if (!eh->hash_shift)
773 goto out;
774
775 /* List is ordered by old_chunk */
Nikos Tsironisf79ae412019-03-17 14:22:57 +0200776 hlist_bl_for_each_entry(e, pos, l, hash_list) {
Milan Brozd74f81f2008-02-08 02:11:27 +0000777 /* Insert after an existing chunk? */
778 if (new_e->old_chunk == (e->old_chunk +
779 dm_consecutive_chunk_count(e) + 1) &&
780 new_e->new_chunk == (dm_chunk_number(e->new_chunk) +
781 dm_consecutive_chunk_count(e) + 1)) {
782 dm_consecutive_chunk_count_inc(e);
Jon Brassow3510cb92009-12-10 23:52:11 +0000783 free_completed_exception(new_e);
Milan Brozd74f81f2008-02-08 02:11:27 +0000784 return;
785 }
786
787 /* Insert before an existing chunk? */
788 if (new_e->old_chunk == (e->old_chunk - 1) &&
789 new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) {
790 dm_consecutive_chunk_count_inc(e);
791 e->old_chunk--;
792 e->new_chunk--;
Jon Brassow3510cb92009-12-10 23:52:11 +0000793 free_completed_exception(new_e);
Milan Brozd74f81f2008-02-08 02:11:27 +0000794 return;
795 }
796
Nikos Tsironisf79ae412019-03-17 14:22:57 +0200797 if (new_e->old_chunk < e->old_chunk)
Milan Brozd74f81f2008-02-08 02:11:27 +0000798 break;
799 }
800
801out:
Nikos Tsironisf79ae412019-03-17 14:22:57 +0200802 if (!e) {
803 /*
804 * Either the table doesn't support consecutive chunks or slot
805 * l is empty.
806 */
807 hlist_bl_add_head(&new_e->hash_list, l);
808 } else if (new_e->old_chunk < e->old_chunk) {
809 /* Add before an existing exception */
810 hlist_bl_add_before(&new_e->hash_list, &e->hash_list);
811 } else {
812 /* Add to l's tail: e is the last exception in this slot */
813 hlist_bl_add_behind(&new_e->hash_list, &e->hash_list);
814 }
Milan Brozd74f81f2008-02-08 02:11:27 +0000815}
816
Jonathan Brassowa159c1a2009-01-06 03:05:19 +0000817/*
818 * Callback used by the exception stores to load exceptions when
819 * initialising.
820 */
821static int dm_add_exception(void *context, chunk_t old, chunk_t new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
Nikos Tsironisf79ae412019-03-17 14:22:57 +0200823 struct dm_exception_table_lock lock;
Jonathan Brassowa159c1a2009-01-06 03:05:19 +0000824 struct dm_snapshot *s = context;
Jon Brassow1d4989c2009-12-10 23:52:10 +0000825 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Mikulas Patocka119bc542014-01-13 19:13:36 -0500827 e = alloc_completed_exception(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (!e)
829 return -ENOMEM;
830
831 e->old_chunk = old;
Milan Brozd74f81f2008-02-08 02:11:27 +0000832
833 /* Consecutive_count is implicitly initialised to zero */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 e->new_chunk = new;
Milan Brozd74f81f2008-02-08 02:11:27 +0000835
Nikos Tsironisf79ae412019-03-17 14:22:57 +0200836 /*
837 * Although there is no need to lock access to the exception tables
838 * here, if we don't then hlist_bl_add_head(), called by
839 * dm_insert_exception(), will complain about accessing the
840 * corresponding list without locking it first.
841 */
842 dm_exception_table_lock_init(s, old, &lock);
843
844 dm_exception_table_lock(&lock);
Jon Brassow3510cb92009-12-10 23:52:11 +0000845 dm_insert_exception(&s->complete, e);
Nikos Tsironisf79ae412019-03-17 14:22:57 +0200846 dm_exception_table_unlock(&lock);
Milan Brozd74f81f2008-02-08 02:11:27 +0000847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return 0;
849}
850
Mikulas Patocka7e201b32009-12-10 23:52:08 +0000851/*
852 * Return a minimum chunk size of all snapshots that have the specified origin.
853 * Return zero if the origin has no snapshots.
854 */
Mike Snitzer542f9032012-07-27 15:08:00 +0100855static uint32_t __minimum_chunk_size(struct origin *o)
Mikulas Patocka7e201b32009-12-10 23:52:08 +0000856{
857 struct dm_snapshot *snap;
Mikulas Patocka7e768532021-05-25 13:17:19 -0400858 unsigned chunk_size = rounddown_pow_of_two(UINT_MAX);
Mikulas Patocka7e201b32009-12-10 23:52:08 +0000859
860 if (o)
861 list_for_each_entry(snap, &o->snapshots, list)
Mikulas Patockaf16dba52021-05-25 13:16:21 -0400862 chunk_size = min_not_zero(chunk_size,
863 snap->store->chunk_size);
Mikulas Patocka7e201b32009-12-10 23:52:08 +0000864
Mike Snitzer542f9032012-07-27 15:08:00 +0100865 return (uint32_t) chunk_size;
Mikulas Patocka7e201b32009-12-10 23:52:08 +0000866}
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868/*
869 * Hard coded magic.
870 */
871static int calc_max_buckets(void)
872{
873 /* use a fixed size of 2MB */
874 unsigned long mem = 2 * 1024 * 1024;
Nikos Tsironisf79ae412019-03-17 14:22:57 +0200875 mem /= sizeof(struct hlist_bl_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 return mem;
878}
879
880/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 * Allocate room for a suitable hash table.
882 */
Jonathan Brassowfee19982009-04-02 19:55:34 +0100883static int init_hash_tables(struct dm_snapshot *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Mikulas Patocka60e356f2013-09-18 19:40:42 -0400885 sector_t hash_size, cow_dev_size, max_buckets;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 /*
888 * Calculate based on the size of the original volume or
889 * the COW volume...
890 */
Mike Snitzerfc56f6f2009-12-10 23:52:12 +0000891 cow_dev_size = get_dev_size(s->cow->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 max_buckets = calc_max_buckets();
893
Mikulas Patocka60e356f2013-09-18 19:40:42 -0400894 hash_size = cow_dev_size >> s->store->chunk_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 hash_size = min(hash_size, max_buckets);
896
Mikulas Patocka8e87b9b2009-12-10 23:51:54 +0000897 if (hash_size < 64)
898 hash_size = 64;
Robert P. J. Day8defd832008-02-08 02:10:06 +0000899 hash_size = rounddown_pow_of_two(hash_size);
Jon Brassow3510cb92009-12-10 23:52:11 +0000900 if (dm_exception_table_init(&s->complete, hash_size,
901 DM_CHUNK_CONSECUTIVE_BITS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return -ENOMEM;
903
904 /*
905 * Allocate hash table for in-flight exceptions
906 * Make this smaller than the real hash table
907 */
908 hash_size >>= 3;
909 if (hash_size < 64)
910 hash_size = 64;
911
Jon Brassow3510cb92009-12-10 23:52:11 +0000912 if (dm_exception_table_init(&s->pending, hash_size, 0)) {
913 dm_exception_table_exit(&s->complete, exception_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return -ENOMEM;
915 }
916
917 return 0;
918}
919
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000920static void merge_shutdown(struct dm_snapshot *s)
921{
922 clear_bit_unlock(RUNNING_MERGE, &s->state_bits);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100923 smp_mb__after_atomic();
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000924 wake_up_bit(&s->state_bits, RUNNING_MERGE);
925}
926
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000927static struct bio *__release_queued_bios_after_merge(struct dm_snapshot *s)
928{
929 s->first_merging_chunk = 0;
930 s->num_merging_chunks = 0;
931
932 return bio_list_get(&s->bios_queued_during_merge);
933}
934
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000935/*
936 * Remove one chunk from the index of completed exceptions.
937 */
938static int __remove_single_exception_chunk(struct dm_snapshot *s,
939 chunk_t old_chunk)
940{
941 struct dm_exception *e;
942
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000943 e = dm_lookup_exception(&s->complete, old_chunk);
944 if (!e) {
945 DMERR("Corruption detected: exception for block %llu is "
946 "on disk but not in memory",
947 (unsigned long long)old_chunk);
948 return -EINVAL;
949 }
950
951 /*
952 * If this is the only chunk using this exception, remove exception.
953 */
954 if (!dm_consecutive_chunk_count(e)) {
955 dm_remove_exception(e);
956 free_completed_exception(e);
957 return 0;
958 }
959
960 /*
961 * The chunk may be either at the beginning or the end of a
962 * group of consecutive chunks - never in the middle. We are
963 * removing chunks in the opposite order to that in which they
964 * were added, so this should always be true.
965 * Decrement the consecutive chunk counter and adjust the
966 * starting point if necessary.
967 */
968 if (old_chunk == e->old_chunk) {
969 e->old_chunk++;
970 e->new_chunk++;
971 } else if (old_chunk != e->old_chunk +
972 dm_consecutive_chunk_count(e)) {
973 DMERR("Attempt to merge block %llu from the "
974 "middle of a chunk range [%llu - %llu]",
975 (unsigned long long)old_chunk,
976 (unsigned long long)e->old_chunk,
977 (unsigned long long)
978 e->old_chunk + dm_consecutive_chunk_count(e));
979 return -EINVAL;
980 }
981
982 dm_consecutive_chunk_count_dec(e);
983
984 return 0;
985}
986
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000987static void flush_bios(struct bio *bio);
988
989static int remove_single_exception_chunk(struct dm_snapshot *s)
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000990{
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000991 struct bio *b = NULL;
992 int r;
993 chunk_t old_chunk = s->first_merging_chunk + s->num_merging_chunks - 1;
Mikulas Patocka1e03f972009-12-10 23:52:32 +0000994
Nikos Tsironis4ad8d882019-03-17 14:22:56 +0200995 down_write(&s->lock);
Mikulas Patocka9fe862542009-12-10 23:52:33 +0000996
997 /*
998 * Process chunks (and associated exceptions) in reverse order
999 * so that dm_consecutive_chunk_count_dec() accounting works.
1000 */
1001 do {
1002 r = __remove_single_exception_chunk(s, old_chunk);
1003 if (r)
1004 goto out;
1005 } while (old_chunk-- > s->first_merging_chunk);
1006
1007 b = __release_queued_bios_after_merge(s);
1008
1009out:
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001010 up_write(&s->lock);
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001011 if (b)
1012 flush_bios(b);
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001013
1014 return r;
1015}
1016
Mikulas Patocka73dfd072009-12-10 23:52:34 +00001017static int origin_write_extent(struct dm_snapshot *merging_snap,
1018 sector_t sector, unsigned chunk_size);
1019
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001020static void merge_callback(int read_err, unsigned long write_err,
1021 void *context);
1022
Mikulas Patocka73dfd072009-12-10 23:52:34 +00001023static uint64_t read_pending_exceptions_done_count(void)
1024{
1025 uint64_t pending_exceptions_done;
1026
1027 spin_lock(&_pending_exceptions_done_spinlock);
1028 pending_exceptions_done = _pending_exceptions_done_count;
1029 spin_unlock(&_pending_exceptions_done_spinlock);
1030
1031 return pending_exceptions_done;
1032}
1033
1034static void increment_pending_exceptions_done_count(void)
1035{
1036 spin_lock(&_pending_exceptions_done_spinlock);
1037 _pending_exceptions_done_count++;
1038 spin_unlock(&_pending_exceptions_done_spinlock);
1039
1040 wake_up_all(&_pending_exceptions_done);
1041}
1042
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001043static void snapshot_merge_next_chunks(struct dm_snapshot *s)
1044{
Mike Snitzer8a2d5282009-12-10 23:52:34 +00001045 int i, linear_chunks;
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001046 chunk_t old_chunk, new_chunk;
1047 struct dm_io_region src, dest;
Mike Snitzer8a2d5282009-12-10 23:52:34 +00001048 sector_t io_size;
Mikulas Patocka73dfd072009-12-10 23:52:34 +00001049 uint64_t previous_count;
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001050
1051 BUG_ON(!test_bit(RUNNING_MERGE, &s->state_bits));
1052 if (unlikely(test_bit(SHUTDOWN_MERGE, &s->state_bits)))
1053 goto shut;
1054
1055 /*
1056 * valid flag never changes during merge, so no lock required.
1057 */
1058 if (!s->valid) {
1059 DMERR("Snapshot is invalid: can't merge");
1060 goto shut;
1061 }
1062
Mike Snitzer8a2d5282009-12-10 23:52:34 +00001063 linear_chunks = s->store->type->prepare_merge(s->store, &old_chunk,
1064 &new_chunk);
1065 if (linear_chunks <= 0) {
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +00001066 if (linear_chunks < 0) {
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001067 DMERR("Read error in exception store: "
1068 "shutting down merge");
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001069 down_write(&s->lock);
zhengbin1d1dda82019-12-24 14:38:02 +08001070 s->merge_failed = true;
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001071 up_write(&s->lock);
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +00001072 }
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001073 goto shut;
1074 }
1075
Mike Snitzer8a2d5282009-12-10 23:52:34 +00001076 /* Adjust old_chunk and new_chunk to reflect start of linear region */
1077 old_chunk = old_chunk + 1 - linear_chunks;
1078 new_chunk = new_chunk + 1 - linear_chunks;
1079
1080 /*
1081 * Use one (potentially large) I/O to copy all 'linear_chunks'
1082 * from the exception store to the origin
1083 */
1084 io_size = linear_chunks * s->store->chunk_size;
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001085
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001086 dest.bdev = s->origin->bdev;
1087 dest.sector = chunk_to_sector(s->store, old_chunk);
Mike Snitzer8a2d5282009-12-10 23:52:34 +00001088 dest.count = min(io_size, get_dev_size(dest.bdev) - dest.sector);
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001089
1090 src.bdev = s->cow->bdev;
1091 src.sector = chunk_to_sector(s->store, new_chunk);
1092 src.count = dest.count;
1093
Mikulas Patocka73dfd072009-12-10 23:52:34 +00001094 /*
1095 * Reallocate any exceptions needed in other snapshots then
1096 * wait for the pending exceptions to complete.
1097 * Each time any pending exception (globally on the system)
1098 * completes we are woken and repeat the process to find out
1099 * if we can proceed. While this may not seem a particularly
1100 * efficient algorithm, it is not expected to have any
1101 * significant impact on performance.
1102 */
1103 previous_count = read_pending_exceptions_done_count();
Mike Snitzer8a2d5282009-12-10 23:52:34 +00001104 while (origin_write_extent(s, dest.sector, io_size)) {
Mikulas Patocka73dfd072009-12-10 23:52:34 +00001105 wait_event(_pending_exceptions_done,
1106 (read_pending_exceptions_done_count() !=
1107 previous_count));
1108 /* Retry after the wait, until all exceptions are done. */
1109 previous_count = read_pending_exceptions_done_count();
1110 }
1111
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001112 down_write(&s->lock);
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001113 s->first_merging_chunk = old_chunk;
Mike Snitzer8a2d5282009-12-10 23:52:34 +00001114 s->num_merging_chunks = linear_chunks;
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001115 up_write(&s->lock);
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001116
Mike Snitzer8a2d5282009-12-10 23:52:34 +00001117 /* Wait until writes to all 'linear_chunks' drain */
1118 for (i = 0; i < linear_chunks; i++)
1119 __check_for_conflicting_io(s, old_chunk + i);
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001120
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001121 dm_kcopyd_copy(s->kcopyd_client, &src, 1, &dest, 0, merge_callback, s);
1122 return;
1123
1124shut:
1125 merge_shutdown(s);
1126}
1127
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001128static void error_bios(struct bio *bio);
1129
Akilesh Kailashfcc42332020-12-28 07:14:07 +00001130static int flush_data(struct dm_snapshot *s)
1131{
1132 struct bio *flush_bio = &s->flush_bio;
1133
1134 bio_reset(flush_bio);
1135 bio_set_dev(flush_bio, s->origin->bdev);
1136 flush_bio->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
1137
1138 return submit_bio_wait(flush_bio);
1139}
1140
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001141static void merge_callback(int read_err, unsigned long write_err, void *context)
1142{
1143 struct dm_snapshot *s = context;
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001144 struct bio *b = NULL;
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001145
1146 if (read_err || write_err) {
1147 if (read_err)
1148 DMERR("Read error: shutting down merge.");
1149 else
1150 DMERR("Write error: shutting down merge.");
1151 goto shut;
1152 }
1153
Akilesh Kailashfcc42332020-12-28 07:14:07 +00001154 if (flush_data(s) < 0) {
1155 DMERR("Flush after merge failed: shutting down merge");
1156 goto shut;
1157 }
1158
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001159 if (s->store->type->commit_merge(s->store,
1160 s->num_merging_chunks) < 0) {
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001161 DMERR("Write error in exception store: shutting down merge");
1162 goto shut;
1163 }
1164
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001165 if (remove_single_exception_chunk(s) < 0)
1166 goto shut;
1167
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001168 snapshot_merge_next_chunks(s);
1169
1170 return;
1171
1172shut:
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001173 down_write(&s->lock);
zhengbin1d1dda82019-12-24 14:38:02 +08001174 s->merge_failed = true;
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001175 b = __release_queued_bios_after_merge(s);
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001176 up_write(&s->lock);
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001177 error_bios(b);
1178
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001179 merge_shutdown(s);
1180}
1181
1182static void start_merge(struct dm_snapshot *s)
1183{
1184 if (!test_and_set_bit(RUNNING_MERGE, &s->state_bits))
1185 snapshot_merge_next_chunks(s);
1186}
1187
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001188/*
1189 * Stop the merging process and wait until it finishes.
1190 */
1191static void stop_merge(struct dm_snapshot *s)
1192{
1193 set_bit(SHUTDOWN_MERGE, &s->state_bits);
NeilBrown74316202014-07-07 15:16:04 +10001194 wait_on_bit(&s->state_bits, RUNNING_MERGE, TASK_UNINTERRUPTIBLE);
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001195 clear_bit(SHUTDOWN_MERGE, &s->state_bits);
1196}
1197
Mike Snitzer2e602382019-06-19 17:05:54 -04001198static int parse_snapshot_features(struct dm_arg_set *as, struct dm_snapshot *s,
1199 struct dm_target *ti)
1200{
1201 int r;
1202 unsigned argc;
1203 const char *arg_name;
1204
1205 static const struct dm_arg _args[] = {
1206 {0, 2, "Invalid number of feature arguments"},
1207 };
1208
1209 /*
1210 * No feature arguments supplied.
1211 */
1212 if (!as->argc)
1213 return 0;
1214
1215 r = dm_read_arg_group(_args, as, &argc, &ti->error);
1216 if (r)
1217 return -EINVAL;
1218
1219 while (argc && !r) {
1220 arg_name = dm_shift_arg(as);
1221 argc--;
1222
1223 if (!strcasecmp(arg_name, "discard_zeroes_cow"))
1224 s->discard_zeroes_cow = true;
1225
1226 else if (!strcasecmp(arg_name, "discard_passdown_origin"))
1227 s->discard_passdown_origin = true;
1228
1229 else {
1230 ti->error = "Unrecognised feature requested";
1231 r = -EINVAL;
1232 break;
1233 }
1234 }
1235
1236 if (!s->discard_zeroes_cow && s->discard_passdown_origin) {
1237 /*
1238 * TODO: really these are disjoint.. but ti->num_discard_bios
1239 * and dm_bio_get_target_bio_nr() require rigid constraints.
1240 */
1241 ti->error = "discard_passdown_origin feature depends on discard_zeroes_cow";
1242 r = -EINVAL;
1243 }
1244
1245 return r;
1246}
1247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248/*
Mike Snitzer2e602382019-06-19 17:05:54 -04001249 * Construct a snapshot mapping:
1250 * <origin_dev> <COW-dev> <p|po|n> <chunk-size> [<# feature args> [<arg>]*]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 */
1252static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1253{
1254 struct dm_snapshot *s;
Mike Snitzer2e602382019-06-19 17:05:54 -04001255 struct dm_arg_set as;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001256 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 int r = -EINVAL;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001258 char *origin_path, *cow_path;
DingXiang4df2bf42016-02-02 12:29:18 +08001259 dev_t origin_dev, cow_dev;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001260 unsigned args_used, num_flush_bios = 1;
Mike Snitzer10b81062009-12-10 23:52:31 +00001261 fmode_t origin_mode = FMODE_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Mike Snitzer2e602382019-06-19 17:05:54 -04001263 if (argc < 4) {
1264 ti->error = "requires 4 or more arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 r = -EINVAL;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001266 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
1268
Mike Snitzer10b81062009-12-10 23:52:31 +00001269 if (dm_target_is_snapshot_merge(ti)) {
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001270 num_flush_bios = 2;
Mike Snitzer10b81062009-12-10 23:52:31 +00001271 origin_mode = FMODE_WRITE;
1272 }
1273
Kent Overstreetd3775352018-06-05 05:26:33 -04001274 s = kzalloc(sizeof(*s), GFP_KERNEL);
Jonathan Brassowfee19982009-04-02 19:55:34 +01001275 if (!s) {
Jonathan Brassowa2d2b032011-08-02 12:32:03 +01001276 ti->error = "Cannot allocate private snapshot structure";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 r = -ENOMEM;
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001278 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
1280
Mike Snitzer2e602382019-06-19 17:05:54 -04001281 as.argc = argc;
1282 as.argv = argv;
1283 dm_consume_args(&as, 4);
1284 r = parse_snapshot_features(&as, s, ti);
1285 if (r)
1286 goto bad_features;
1287
Mikulas Patockac2411042010-08-12 04:13:51 +01001288 origin_path = argv[0];
1289 argv++;
1290 argc--;
1291
1292 r = dm_get_device(ti, origin_path, origin_mode, &s->origin);
1293 if (r) {
1294 ti->error = "Cannot get origin device";
1295 goto bad_origin;
1296 }
DingXiang4df2bf42016-02-02 12:29:18 +08001297 origin_dev = s->origin->bdev->bd_dev;
Mikulas Patockac2411042010-08-12 04:13:51 +01001298
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001299 cow_path = argv[0];
1300 argv++;
1301 argc--;
1302
DingXiang4df2bf42016-02-02 12:29:18 +08001303 cow_dev = dm_get_dev_t(cow_path);
1304 if (cow_dev && cow_dev == origin_dev) {
1305 ti->error = "COW device cannot be the same as origin device";
1306 r = -EINVAL;
1307 goto bad_cow;
1308 }
1309
Milan Broz024d37e2011-03-24 13:52:14 +00001310 r = dm_get_device(ti, cow_path, dm_table_get_mode(ti->table), &s->cow);
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001311 if (r) {
1312 ti->error = "Cannot get COW device";
1313 goto bad_cow;
1314 }
1315
1316 r = dm_exception_store_create(ti, argc, argv, s, &args_used, &s->store);
1317 if (r) {
1318 ti->error = "Couldn't create exception store";
1319 r = -EINVAL;
1320 goto bad_store;
1321 }
1322
1323 argv += args_used;
1324 argc -= args_used;
1325
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001326 s->ti = ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 s->valid = 1;
Mikulas Patocka76c44f62015-06-21 16:31:33 -04001328 s->snapshot_overflowed = 0;
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001329 s->active = 0;
Mikulas Patocka879129d22008-10-30 13:33:16 +00001330 atomic_set(&s->pending_exceptions_count, 0);
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02001331 spin_lock_init(&s->pe_allocation_lock);
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001332 s->exception_start_sequence = 0;
1333 s->exception_complete_sequence = 0;
David Jeffery3db27762018-08-07 16:56:00 -04001334 s->out_of_order_tree = RB_ROOT;
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001335 init_rwsem(&s->lock);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001336 INIT_LIST_HEAD(&s->list);
Alasdair G Kergonca3a9312006-10-03 01:15:30 -07001337 spin_lock_init(&s->pe_lock);
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001338 s->state_bits = 0;
zhengbin1d1dda82019-12-24 14:38:02 +08001339 s->merge_failed = false;
Mikulas Patocka9fe862542009-12-10 23:52:33 +00001340 s->first_merging_chunk = 0;
1341 s->num_merging_chunks = 0;
1342 bio_list_init(&s->bios_queued_during_merge);
Akilesh Kailashfcc42332020-12-28 07:14:07 +00001343 bio_init(&s->flush_bio, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 /* Allocate hash table for COW data */
Jonathan Brassowfee19982009-04-02 19:55:34 +01001346 if (init_hash_tables(s)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 ti->error = "Unable to allocate hash table space";
1348 r = -ENOMEM;
Jonathan Brassowfee19982009-04-02 19:55:34 +01001349 goto bad_hash_tables;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 }
1351
Mikulas Patockab2155572019-10-02 06:15:53 -04001352 init_waitqueue_head(&s->in_progress_wait);
Nikos Tsironis721b1d92018-10-31 17:53:08 -04001353
Mikulas Patockadf5d2e92013-03-01 22:45:49 +00001354 s->kcopyd_client = dm_kcopyd_client_create(&dm_kcopyd_throttle);
Mikulas Patockafa34ce72011-05-29 13:03:13 +01001355 if (IS_ERR(s->kcopyd_client)) {
1356 r = PTR_ERR(s->kcopyd_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 ti->error = "Could not create kcopyd client";
Jonathan Brassowfee19982009-04-02 19:55:34 +01001358 goto bad_kcopyd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 }
1360
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001361 r = mempool_init_slab_pool(&s->pending_pool, MIN_IOS, pending_cache);
1362 if (r) {
Mikulas Patocka92e86812008-07-21 12:00:35 +01001363 ti->error = "Could not allocate mempool for pending exceptions";
Jonathan Brassowfee19982009-04-02 19:55:34 +01001364 goto bad_pending_pool;
Mikulas Patocka92e86812008-07-21 12:00:35 +01001365 }
1366
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001367 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
1368 INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]);
1369
1370 spin_lock_init(&s->tracked_chunk_lock);
1371
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001372 ti->private = s;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001373 ti->num_flush_bios = num_flush_bios;
Mike Snitzer2e602382019-06-19 17:05:54 -04001374 if (s->discard_zeroes_cow)
1375 ti->num_discard_bios = (s->discard_passdown_origin ? 2 : 1);
Mike Snitzer30187e12016-01-31 13:28:26 -05001376 ti->per_io_data_size = sizeof(struct dm_snap_tracked_chunk);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001377
1378 /* Add snapshot to the list of snapshots for this origin */
1379 /* Exceptions aren't triggered till snapshot_resume() is called */
1380 r = register_snapshot(s);
1381 if (r == -ENOMEM) {
1382 ti->error = "Snapshot origin struct allocation failed";
1383 goto bad_load_and_register;
1384 } else if (r < 0) {
1385 /* invalid handover, register_snapshot has set ti->error */
1386 goto bad_load_and_register;
1387 }
1388
1389 /*
1390 * Metadata must only be loaded into one table at once, so skip this
1391 * if metadata will be handed over during resume.
1392 * Chunk size will be set during the handover - set it to zero to
1393 * ensure it's ignored.
1394 */
1395 if (r > 0) {
1396 s->store->chunk_size = 0;
1397 return 0;
1398 }
1399
Jonathan Brassow493df712009-04-02 19:55:31 +01001400 r = s->store->type->read_metadata(s->store, dm_add_exception,
1401 (void *)s);
Milan Broz07641472007-07-12 17:28:13 +01001402 if (r < 0) {
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -07001403 ti->error = "Failed to read snapshot metadata";
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001404 goto bad_read_metadata;
Milan Broz07641472007-07-12 17:28:13 +01001405 } else if (r > 0) {
1406 s->valid = 0;
1407 DMWARN("Snapshot is marked invalid.");
Mark McLoughlinf9cea4f2006-10-03 01:15:25 -07001408 }
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08001409
Mikulas Patocka3f2412d2009-10-16 23:18:16 +01001410 if (!s->store->chunk_size) {
1411 ti->error = "Chunk size not set";
Mikulas Patockac699a0d2021-05-10 14:49:05 -04001412 r = -EINVAL;
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001413 goto bad_read_metadata;
Mikulas Patocka3f2412d2009-10-16 23:18:16 +01001414 }
Mike Snitzer542f9032012-07-27 15:08:00 +01001415
1416 r = dm_set_target_max_io_len(ti, s->store->chunk_size);
1417 if (r)
1418 goto bad_read_metadata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420 return 0;
1421
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001422bad_read_metadata:
1423 unregister_snapshot(s);
Jonathan Brassowfee19982009-04-02 19:55:34 +01001424bad_load_and_register:
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001425 mempool_exit(&s->pending_pool);
Jonathan Brassowfee19982009-04-02 19:55:34 +01001426bad_pending_pool:
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001427 dm_kcopyd_client_destroy(s->kcopyd_client);
Jonathan Brassowfee19982009-04-02 19:55:34 +01001428bad_kcopyd:
Jon Brassow3510cb92009-12-10 23:52:11 +00001429 dm_exception_table_exit(&s->pending, pending_cache);
1430 dm_exception_table_exit(&s->complete, exception_cache);
Jonathan Brassowfee19982009-04-02 19:55:34 +01001431bad_hash_tables:
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001432 dm_exception_store_destroy(s->store);
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001433bad_store:
1434 dm_put_device(ti, s->cow);
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001435bad_cow:
Mikulas Patockac2411042010-08-12 04:13:51 +01001436 dm_put_device(ti, s->origin);
Mikulas Patockac2411042010-08-12 04:13:51 +01001437bad_origin:
Mike Snitzer2e602382019-06-19 17:05:54 -04001438bad_features:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 kfree(s);
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001440bad:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 return r;
1442}
1443
Milan Broz31c93a0c2006-12-08 02:41:11 -08001444static void __free_exceptions(struct dm_snapshot *s)
1445{
Heinz Mauelshageneb69aca2008-04-24 21:43:19 +01001446 dm_kcopyd_client_destroy(s->kcopyd_client);
Milan Broz31c93a0c2006-12-08 02:41:11 -08001447 s->kcopyd_client = NULL;
1448
Jon Brassow3510cb92009-12-10 23:52:11 +00001449 dm_exception_table_exit(&s->pending, pending_cache);
1450 dm_exception_table_exit(&s->complete, exception_cache);
Milan Broz31c93a0c2006-12-08 02:41:11 -08001451}
1452
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001453static void __handover_exceptions(struct dm_snapshot *snap_src,
1454 struct dm_snapshot *snap_dest)
1455{
1456 union {
1457 struct dm_exception_table table_swap;
1458 struct dm_exception_store *store_swap;
1459 } u;
1460
1461 /*
1462 * Swap all snapshot context information between the two instances.
1463 */
1464 u.table_swap = snap_dest->complete;
1465 snap_dest->complete = snap_src->complete;
1466 snap_src->complete = u.table_swap;
1467
1468 u.store_swap = snap_dest->store;
1469 snap_dest->store = snap_src->store;
Mike Snitzerb0d3cc02015-10-08 18:05:41 -04001470 snap_dest->store->userspace_supports_overflow = u.store_swap->userspace_supports_overflow;
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001471 snap_src->store = u.store_swap;
1472
1473 snap_dest->store->snap = snap_dest;
1474 snap_src->store->snap = snap_src;
1475
Mike Snitzer542f9032012-07-27 15:08:00 +01001476 snap_dest->ti->max_io_len = snap_dest->store->chunk_size;
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001477 snap_dest->valid = snap_src->valid;
Mikulas Patocka76c44f62015-06-21 16:31:33 -04001478 snap_dest->snapshot_overflowed = snap_src->snapshot_overflowed;
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001479
1480 /*
1481 * Set source invalid to ensure it receives no further I/O.
1482 */
1483 snap_src->valid = 0;
1484}
1485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486static void snapshot_dtr(struct dm_target *ti)
1487{
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001488#ifdef CONFIG_DM_DEBUG
1489 int i;
1490#endif
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001491 struct dm_snapshot *s = ti->private;
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001492 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001494 down_read(&_origins_lock);
1495 /* Check whether exception handover must be cancelled */
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +00001496 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001497 if (snap_src && snap_dest && (s == snap_src)) {
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001498 down_write(&snap_dest->lock);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001499 snap_dest->valid = 0;
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02001500 up_write(&snap_dest->lock);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00001501 DMERR("Cancelling snapshot handover.");
1502 }
1503 up_read(&_origins_lock);
1504
Mikulas Patocka1e03f972009-12-10 23:52:32 +00001505 if (dm_target_is_snapshot_merge(ti))
1506 stop_merge(s);
1507
Alasdair G Kergon138728dc2006-03-27 01:17:50 -08001508 /* Prevent further origin writes from using this snapshot. */
1509 /* After this returns there can be no new kcopyd jobs. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 unregister_snapshot(s);
1511
Mikulas Patocka879129d22008-10-30 13:33:16 +00001512 while (atomic_read(&s->pending_exceptions_count))
Mikulas Patocka90fa1522009-01-06 03:04:54 +00001513 msleep(1);
Mikulas Patocka879129d22008-10-30 13:33:16 +00001514 /*
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001515 * Ensure instructions in mempool_exit aren't reordered
Mikulas Patocka879129d22008-10-30 13:33:16 +00001516 * before atomic_read.
1517 */
1518 smp_mb();
1519
Mikulas Patockacd45daf2008-07-21 12:00:32 +01001520#ifdef CONFIG_DM_DEBUG
1521 for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
1522 BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i]));
1523#endif
1524
Milan Broz31c93a0c2006-12-08 02:41:11 -08001525 __free_exceptions(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001527 mempool_exit(&s->pending_pool);
Mikulas Patocka92e86812008-07-21 12:00:35 +01001528
Jonathan Brassowfee19982009-04-02 19:55:34 +01001529 dm_exception_store_destroy(s->store);
Alasdair G Kergon138728dc2006-03-27 01:17:50 -08001530
Akilesh Kailashfcc42332020-12-28 07:14:07 +00001531 bio_uninit(&s->flush_bio);
1532
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001533 dm_put_device(ti, s->cow);
1534
Mikulas Patockac2411042010-08-12 04:13:51 +01001535 dm_put_device(ti, s->origin);
1536
Mikulas Patockab2155572019-10-02 06:15:53 -04001537 WARN_ON(s->in_progress);
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 kfree(s);
1540}
1541
Mikulas Patockaa2f83e82019-10-02 06:14:17 -04001542static void account_start_copy(struct dm_snapshot *s)
1543{
Mikulas Patockab2155572019-10-02 06:15:53 -04001544 spin_lock(&s->in_progress_wait.lock);
1545 s->in_progress++;
1546 spin_unlock(&s->in_progress_wait.lock);
Mikulas Patockaa2f83e82019-10-02 06:14:17 -04001547}
1548
1549static void account_end_copy(struct dm_snapshot *s)
1550{
Mikulas Patockab2155572019-10-02 06:15:53 -04001551 spin_lock(&s->in_progress_wait.lock);
1552 BUG_ON(!s->in_progress);
1553 s->in_progress--;
1554 if (likely(s->in_progress <= cow_threshold) &&
1555 unlikely(waitqueue_active(&s->in_progress_wait)))
1556 wake_up_locked(&s->in_progress_wait);
1557 spin_unlock(&s->in_progress_wait.lock);
1558}
1559
1560static bool wait_for_in_progress(struct dm_snapshot *s, bool unlock_origins)
1561{
1562 if (unlikely(s->in_progress > cow_threshold)) {
1563 spin_lock(&s->in_progress_wait.lock);
1564 if (likely(s->in_progress > cow_threshold)) {
1565 /*
1566 * NOTE: this throttle doesn't account for whether
1567 * the caller is servicing an IO that will trigger a COW
1568 * so excess throttling may result for chunks not required
1569 * to be COW'd. But if cow_threshold was reached, extra
1570 * throttling is unlikely to negatively impact performance.
1571 */
1572 DECLARE_WAITQUEUE(wait, current);
1573 __add_wait_queue(&s->in_progress_wait, &wait);
1574 __set_current_state(TASK_UNINTERRUPTIBLE);
1575 spin_unlock(&s->in_progress_wait.lock);
1576 if (unlock_origins)
1577 up_read(&_origins_lock);
1578 io_schedule();
1579 remove_wait_queue(&s->in_progress_wait, &wait);
1580 return false;
1581 }
1582 spin_unlock(&s->in_progress_wait.lock);
1583 }
1584 return true;
Mikulas Patockaa2f83e82019-10-02 06:14:17 -04001585}
1586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587/*
1588 * Flush a list of buffers.
1589 */
1590static void flush_bios(struct bio *bio)
1591{
1592 struct bio *n;
1593
1594 while (bio) {
1595 n = bio->bi_next;
1596 bio->bi_next = NULL;
Christoph Hellwiged00aab2020-07-01 10:59:44 +02001597 submit_bio_noacct(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 bio = n;
1599 }
1600}
1601
Mikulas Patockab2155572019-10-02 06:15:53 -04001602static int do_origin(struct dm_dev *origin, struct bio *bio, bool limit);
Mikulas Patocka515ad662009-12-10 23:52:30 +00001603
1604/*
1605 * Flush a list of buffers.
1606 */
1607static void retry_origin_bios(struct dm_snapshot *s, struct bio *bio)
1608{
1609 struct bio *n;
1610 int r;
1611
1612 while (bio) {
1613 n = bio->bi_next;
1614 bio->bi_next = NULL;
Mikulas Patockab2155572019-10-02 06:15:53 -04001615 r = do_origin(s->origin, bio, false);
Mikulas Patocka515ad662009-12-10 23:52:30 +00001616 if (r == DM_MAPIO_REMAPPED)
Christoph Hellwiged00aab2020-07-01 10:59:44 +02001617 submit_bio_noacct(bio);
Mikulas Patocka515ad662009-12-10 23:52:30 +00001618 bio = n;
1619 }
1620}
1621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622/*
1623 * Error a list of buffers.
1624 */
1625static void error_bios(struct bio *bio)
1626{
1627 struct bio *n;
1628
1629 while (bio) {
1630 n = bio->bi_next;
1631 bio->bi_next = NULL;
NeilBrown6712ecf2007-09-27 12:47:43 +02001632 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 bio = n;
1634 }
1635}
1636
Alasdair G Kergon695368a2006-10-03 01:15:31 -07001637static void __invalidate_snapshot(struct dm_snapshot *s, int err)
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001638{
1639 if (!s->valid)
1640 return;
1641
1642 if (err == -EIO)
1643 DMERR("Invalidating snapshot: Error reading/writing.");
1644 else if (err == -ENOMEM)
1645 DMERR("Invalidating snapshot: Unable to allocate exception.");
1646
Jonathan Brassow493df712009-04-02 19:55:31 +01001647 if (s->store->type->drop_snapshot)
1648 s->store->type->drop_snapshot(s->store);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001649
1650 s->valid = 0;
1651
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001652 dm_table_event(s->ti->table);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001653}
1654
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02001655static void invalidate_snapshot(struct dm_snapshot *s, int err)
1656{
1657 down_write(&s->lock);
1658 __invalidate_snapshot(s, err);
1659 up_write(&s->lock);
1660}
1661
Mikulas Patocka385277b2016-01-08 19:07:55 -05001662static void pending_complete(void *context, int success)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663{
Mikulas Patocka385277b2016-01-08 19:07:55 -05001664 struct dm_snap_pending_exception *pe = context;
Jon Brassow1d4989c2009-12-10 23:52:10 +00001665 struct dm_exception *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 struct dm_snapshot *s = pe->snap;
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001667 struct bio *origin_bios = NULL;
1668 struct bio *snapshot_bios = NULL;
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001669 struct bio *full_bio = NULL;
Nikos Tsironisf79ae412019-03-17 14:22:57 +02001670 struct dm_exception_table_lock lock;
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001671 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Nikos Tsironisf79ae412019-03-17 14:22:57 +02001673 dm_exception_table_lock_init(s, pe->e.old_chunk, &lock);
1674
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001675 if (!success) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 /* Read/write error - snapshot is unusable */
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02001677 invalidate_snapshot(s, -EIO);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001678 error = 1;
Nikos Tsironisf79ae412019-03-17 14:22:57 +02001679
1680 dm_exception_table_lock(&lock);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001681 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 }
1683
Mikulas Patocka119bc542014-01-13 19:13:36 -05001684 e = alloc_completed_exception(GFP_NOIO);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001685 if (!e) {
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02001686 invalidate_snapshot(s, -ENOMEM);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001687 error = 1;
Nikos Tsironisf79ae412019-03-17 14:22:57 +02001688
1689 dm_exception_table_lock(&lock);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001690 goto out;
1691 }
1692 *e = pe->e;
1693
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02001694 down_read(&s->lock);
Nikos Tsironisf79ae412019-03-17 14:22:57 +02001695 dm_exception_table_lock(&lock);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001696 if (!s->valid) {
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02001697 up_read(&s->lock);
Jon Brassow3510cb92009-12-10 23:52:11 +00001698 free_completed_exception(e);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001699 error = 1;
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02001700
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001701 goto out;
1702 }
1703
Mikulas Patockaa8d41b52008-07-21 12:00:34 +01001704 /*
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02001705 * Add a proper exception. After inserting the completed exception all
1706 * subsequent snapshot reads to this chunk will be redirected to the
1707 * COW device. This ensures that we do not starve. Moreover, as long
1708 * as the pending exception exists, neither origin writes nor snapshot
1709 * merging can overwrite the chunk in origin.
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001710 */
Jon Brassow3510cb92009-12-10 23:52:11 +00001711 dm_insert_exception(&s->complete, e);
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02001712 up_read(&s->lock);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001713
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02001714 /* Wait for conflicting reads to drain */
1715 if (__chunk_is_tracked(s, pe->e.old_chunk)) {
Nikos Tsironisf79ae412019-03-17 14:22:57 +02001716 dm_exception_table_unlock(&lock);
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02001717 __check_for_conflicting_io(s, pe->e.old_chunk);
Nikos Tsironisf79ae412019-03-17 14:22:57 +02001718 dm_exception_table_lock(&lock);
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02001719 }
1720
Jonathan Brassowa2d2b032011-08-02 12:32:03 +01001721out:
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02001722 /* Remove the in-flight exception from the list */
Jon Brassow3510cb92009-12-10 23:52:11 +00001723 dm_remove_exception(&pe->e);
Nikos Tsironisf79ae412019-03-17 14:22:57 +02001724
1725 dm_exception_table_unlock(&lock);
1726
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001727 snapshot_bios = bio_list_get(&pe->snapshot_bios);
Mikulas Patocka515ad662009-12-10 23:52:30 +00001728 origin_bios = bio_list_get(&pe->origin_bios);
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001729 full_bio = pe->full_bio;
Mikulas Patockafe3265b2015-11-25 16:03:31 -05001730 if (full_bio)
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001731 full_bio->bi_end_io = pe->full_bio_end_io;
Mikulas Patocka73dfd072009-12-10 23:52:34 +00001732 increment_pending_exceptions_done_count();
1733
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001734 /* Submit any pending write bios */
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001735 if (error) {
1736 if (full_bio)
1737 bio_io_error(full_bio);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001738 error_bios(snapshot_bios);
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001739 } else {
1740 if (full_bio)
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001741 bio_endio(full_bio);
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001742 flush_bios(snapshot_bios);
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001743 }
Alasdair G Kergon9d493fa2006-10-03 01:15:29 -07001744
Mikulas Patocka515ad662009-12-10 23:52:30 +00001745 retry_origin_bios(s, origin_bios);
Mikulas Patocka22aa66a2015-02-17 14:34:00 -05001746
1747 free_pending_exception(pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748}
1749
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001750static void complete_exception(struct dm_snap_pending_exception *pe)
1751{
1752 struct dm_snapshot *s = pe->snap;
1753
Mikulas Patocka385277b2016-01-08 19:07:55 -05001754 /* Update the metadata if we are persistent */
1755 s->store->type->commit_exception(s->store, &pe->e, !pe->copy_error,
1756 pending_complete, pe);
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001757}
1758
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759/*
1760 * Called when the copy I/O has finished. kcopyd actually runs
1761 * this code so don't block.
1762 */
Alasdair G Kergon4cdc1d12008-03-28 14:16:10 -07001763static void copy_callback(int read_err, unsigned long write_err, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001765 struct dm_snap_pending_exception *pe = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 struct dm_snapshot *s = pe->snap;
1767
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001768 pe->copy_error = read_err || write_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001770 if (pe->exception_sequence == s->exception_complete_sequence) {
David Jeffery3db27762018-08-07 16:56:00 -04001771 struct rb_node *next;
1772
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001773 s->exception_complete_sequence++;
1774 complete_exception(pe);
1775
David Jeffery3db27762018-08-07 16:56:00 -04001776 next = rb_first(&s->out_of_order_tree);
1777 while (next) {
1778 pe = rb_entry(next, struct dm_snap_pending_exception,
1779 out_of_order_node);
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001780 if (pe->exception_sequence != s->exception_complete_sequence)
1781 break;
David Jeffery3db27762018-08-07 16:56:00 -04001782 next = rb_next(next);
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001783 s->exception_complete_sequence++;
David Jeffery3db27762018-08-07 16:56:00 -04001784 rb_erase(&pe->out_of_order_node, &s->out_of_order_tree);
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001785 complete_exception(pe);
David Jeffery3db27762018-08-07 16:56:00 -04001786 cond_resched();
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001787 }
1788 } else {
David Jeffery3db27762018-08-07 16:56:00 -04001789 struct rb_node *parent = NULL;
1790 struct rb_node **p = &s->out_of_order_tree.rb_node;
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001791 struct dm_snap_pending_exception *pe2;
1792
David Jeffery3db27762018-08-07 16:56:00 -04001793 while (*p) {
1794 pe2 = rb_entry(*p, struct dm_snap_pending_exception, out_of_order_node);
1795 parent = *p;
1796
1797 BUG_ON(pe->exception_sequence == pe2->exception_sequence);
1798 if (pe->exception_sequence < pe2->exception_sequence)
1799 p = &((*p)->rb_left);
1800 else
1801 p = &((*p)->rb_right);
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001802 }
David Jeffery3db27762018-08-07 16:56:00 -04001803
1804 rb_link_node(&pe->out_of_order_node, parent, p);
1805 rb_insert_color(&pe->out_of_order_node, &s->out_of_order_tree);
Mikulas Patocka230c83a2013-11-29 18:13:37 -05001806 }
Mikulas Patockaa2f83e82019-10-02 06:14:17 -04001807 account_end_copy(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808}
1809
1810/*
1811 * Dispatches the copy operation to kcopyd.
1812 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001813static void start_copy(struct dm_snap_pending_exception *pe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
1815 struct dm_snapshot *s = pe->snap;
Heinz Mauelshagen22a1ceb2008-04-24 21:43:17 +01001816 struct dm_io_region src, dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 struct block_device *bdev = s->origin->bdev;
1818 sector_t dev_size;
1819
1820 dev_size = get_dev_size(bdev);
1821
1822 src.bdev = bdev;
Jonathan Brassow71fab002009-04-02 19:55:33 +01001823 src.sector = chunk_to_sector(s->store, pe->e.old_chunk);
Mikulas Patockadf96eee2009-10-16 23:18:17 +01001824 src.count = min((sector_t)s->store->chunk_size, dev_size - src.sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00001826 dest.bdev = s->cow->bdev;
Jonathan Brassow71fab002009-04-02 19:55:33 +01001827 dest.sector = chunk_to_sector(s->store, pe->e.new_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 dest.count = src.count;
1829
1830 /* Hand over to kcopyd */
Mikulas Patockaa2f83e82019-10-02 06:14:17 -04001831 account_start_copy(s);
Jonathan Brassowa2d2b032011-08-02 12:32:03 +01001832 dm_kcopyd_copy(s->kcopyd_client, &src, 1, &dest, 0, copy_callback, pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833}
1834
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001835static void full_bio_end_io(struct bio *bio)
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001836{
1837 void *callback_data = bio->bi_private;
1838
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001839 dm_kcopyd_do_callback(callback_data, 0, bio->bi_status ? 1 : 0);
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001840}
1841
1842static void start_full_bio(struct dm_snap_pending_exception *pe,
1843 struct bio *bio)
1844{
1845 struct dm_snapshot *s = pe->snap;
1846 void *callback_data;
1847
1848 pe->full_bio = bio;
1849 pe->full_bio_end_io = bio->bi_end_io;
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001850
Mikulas Patockaa2f83e82019-10-02 06:14:17 -04001851 account_start_copy(s);
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001852 callback_data = dm_kcopyd_prepare_callback(s->kcopyd_client,
1853 copy_callback, pe);
1854
1855 bio->bi_end_io = full_bio_end_io;
1856 bio->bi_private = callback_data;
1857
Christoph Hellwiged00aab2020-07-01 10:59:44 +02001858 submit_bio_noacct(bio);
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01001859}
1860
Mikulas Patocka29138082009-04-02 19:55:25 +01001861static struct dm_snap_pending_exception *
1862__lookup_pending_exception(struct dm_snapshot *s, chunk_t chunk)
1863{
Jon Brassow3510cb92009-12-10 23:52:11 +00001864 struct dm_exception *e = dm_lookup_exception(&s->pending, chunk);
Mikulas Patocka29138082009-04-02 19:55:25 +01001865
1866 if (!e)
1867 return NULL;
1868
1869 return container_of(e, struct dm_snap_pending_exception, e);
1870}
1871
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872/*
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02001873 * Inserts a pending exception into the pending table.
1874 *
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02001875 * NOTE: a write lock must be held on the chunk's pending exception table slot
1876 * before calling this.
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02001877 */
1878static struct dm_snap_pending_exception *
1879__insert_pending_exception(struct dm_snapshot *s,
1880 struct dm_snap_pending_exception *pe, chunk_t chunk)
1881{
1882 pe->e.old_chunk = chunk;
1883 bio_list_init(&pe->origin_bios);
1884 bio_list_init(&pe->snapshot_bios);
1885 pe->started = 0;
1886 pe->full_bio = NULL;
1887
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02001888 spin_lock(&s->pe_allocation_lock);
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02001889 if (s->store->type->prepare_exception(s->store, &pe->e)) {
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02001890 spin_unlock(&s->pe_allocation_lock);
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02001891 free_pending_exception(pe);
1892 return NULL;
1893 }
1894
1895 pe->exception_sequence = s->exception_start_sequence++;
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02001896 spin_unlock(&s->pe_allocation_lock);
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02001897
1898 dm_insert_exception(&s->pending, &pe->e);
1899
1900 return pe;
1901}
1902
1903/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 * Looks to see if this snapshot already has a pending exception
1905 * for this chunk, otherwise it allocates a new one and inserts
1906 * it into the pending table.
1907 *
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02001908 * NOTE: a write lock must be held on the chunk's pending exception table slot
1909 * before calling this.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001911static struct dm_snap_pending_exception *
Mikulas Patockac6621392009-04-02 19:55:25 +01001912__find_pending_exception(struct dm_snapshot *s,
1913 struct dm_snap_pending_exception *pe, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914{
Mikulas Patockac6621392009-04-02 19:55:25 +01001915 struct dm_snap_pending_exception *pe2;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001916
Mikulas Patocka29138082009-04-02 19:55:25 +01001917 pe2 = __lookup_pending_exception(s, chunk);
1918 if (pe2) {
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001919 free_pending_exception(pe);
Mikulas Patocka29138082009-04-02 19:55:25 +01001920 return pe2;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001921 }
1922
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02001923 return __insert_pending_exception(s, pe, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924}
1925
Jon Brassow1d4989c2009-12-10 23:52:10 +00001926static void remap_exception(struct dm_snapshot *s, struct dm_exception *e,
Milan Brozd74f81f2008-02-08 02:11:27 +00001927 struct bio *bio, chunk_t chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
Christoph Hellwig74d46992017-08-23 19:10:32 +02001929 bio_set_dev(bio, s->cow->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001930 bio->bi_iter.bi_sector =
1931 chunk_to_sector(s->store, dm_chunk_number(e->new_chunk) +
1932 (chunk - e->old_chunk)) +
1933 (bio->bi_iter.bi_sector & s->store->chunk_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934}
1935
Mike Snitzer2e602382019-06-19 17:05:54 -04001936static void zero_callback(int read_err, unsigned long write_err, void *context)
1937{
1938 struct bio *bio = context;
1939 struct dm_snapshot *s = bio->bi_private;
1940
Mikulas Patockaa2f83e82019-10-02 06:14:17 -04001941 account_end_copy(s);
Mike Snitzer2e602382019-06-19 17:05:54 -04001942 bio->bi_status = write_err ? BLK_STS_IOERR : 0;
1943 bio_endio(bio);
1944}
1945
1946static void zero_exception(struct dm_snapshot *s, struct dm_exception *e,
1947 struct bio *bio, chunk_t chunk)
1948{
1949 struct dm_io_region dest;
1950
1951 dest.bdev = s->cow->bdev;
1952 dest.sector = bio->bi_iter.bi_sector;
1953 dest.count = s->store->chunk_size;
1954
Mikulas Patockaa2f83e82019-10-02 06:14:17 -04001955 account_start_copy(s);
Mike Snitzer2e602382019-06-19 17:05:54 -04001956 WARN_ON_ONCE(bio->bi_private);
1957 bio->bi_private = s;
1958 dm_kcopyd_zero(s->kcopyd_client, 1, &dest, 0, zero_callback, bio);
1959}
1960
1961static bool io_overlaps_chunk(struct dm_snapshot *s, struct bio *bio)
1962{
1963 return bio->bi_iter.bi_size ==
1964 (s->store->chunk_size << SECTOR_SHIFT);
1965}
1966
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001967static int snapshot_map(struct dm_target *ti, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
Jon Brassow1d4989c2009-12-10 23:52:10 +00001969 struct dm_exception *e;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001970 struct dm_snapshot *s = ti->private;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001971 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 chunk_t chunk;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001973 struct dm_snap_pending_exception *pe = NULL;
Nikos Tsironisf79ae412019-03-17 14:22:57 +02001974 struct dm_exception_table_lock lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Mikulas Patockaee180262012-12-21 20:23:41 +00001976 init_tracked_chunk(bio);
1977
Jens Axboe1eff9d32016-08-05 15:35:16 -06001978 if (bio->bi_opf & REQ_PREFLUSH) {
Christoph Hellwig74d46992017-08-23 19:10:32 +02001979 bio_set_dev(bio, s->cow->bdev);
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01001980 return DM_MAPIO_REMAPPED;
1981 }
1982
Kent Overstreet4f024f32013-10-11 15:44:27 -07001983 chunk = sector_to_chunk(s->store, bio->bi_iter.bi_sector);
Nikos Tsironisf79ae412019-03-17 14:22:57 +02001984 dm_exception_table_lock_init(s, chunk, &lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
1986 /* Full snapshots are not usable */
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08001987 /* To get here the table must be live so s->active is always set. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 if (!s->valid)
Christoph Hellwig846785e2017-06-03 09:38:02 +02001989 return DM_MAPIO_KILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Mikulas Patockab2155572019-10-02 06:15:53 -04001991 if (bio_data_dir(bio) == WRITE) {
1992 while (unlikely(!wait_for_in_progress(s, false)))
1993 ; /* wait_for_in_progress() has slept */
1994 }
1995
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02001996 down_read(&s->lock);
Nikos Tsironisf79ae412019-03-17 14:22:57 +02001997 dm_exception_table_lock(&lock);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07001998
Christoph Hellwig70246282016-07-19 11:28:41 +02001999 if (!s->valid || (unlikely(s->snapshot_overflowed) &&
2000 bio_data_dir(bio) == WRITE)) {
Christoph Hellwig846785e2017-06-03 09:38:02 +02002001 r = DM_MAPIO_KILL;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07002002 goto out_unlock;
2003 }
2004
Mike Snitzer2e602382019-06-19 17:05:54 -04002005 if (unlikely(bio_op(bio) == REQ_OP_DISCARD)) {
2006 if (s->discard_passdown_origin && dm_bio_get_target_bio_nr(bio)) {
2007 /*
2008 * passdown discard to origin (without triggering
2009 * snapshot exceptions via do_origin; doing so would
2010 * defeat the goal of freeing space in origin that is
2011 * implied by the "discard_passdown_origin" feature)
2012 */
2013 bio_set_dev(bio, s->origin->bdev);
2014 track_chunk(s, bio, chunk);
2015 goto out_unlock;
2016 }
2017 /* discard to snapshot (target_bio_nr == 0) zeroes exceptions */
2018 }
2019
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07002020 /* If the block is already remapped - use that, else remap it */
Jon Brassow3510cb92009-12-10 23:52:11 +00002021 e = dm_lookup_exception(&s->complete, chunk);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07002022 if (e) {
Milan Brozd74f81f2008-02-08 02:11:27 +00002023 remap_exception(s, e, bio, chunk);
Mike Snitzer2e602382019-06-19 17:05:54 -04002024 if (unlikely(bio_op(bio) == REQ_OP_DISCARD) &&
2025 io_overlaps_chunk(s, bio)) {
2026 dm_exception_table_unlock(&lock);
2027 up_read(&s->lock);
2028 zero_exception(s, e, bio, chunk);
2029 r = DM_MAPIO_SUBMITTED; /* discard is not issued */
2030 goto out;
2031 }
2032 goto out_unlock;
2033 }
2034
2035 if (unlikely(bio_op(bio) == REQ_OP_DISCARD)) {
2036 /*
2037 * If no exception exists, complete discard immediately
2038 * otherwise it'll trigger copy-out.
2039 */
2040 bio_endio(bio);
2041 r = DM_MAPIO_SUBMITTED;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07002042 goto out_unlock;
2043 }
2044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 /*
2046 * Write to snapshot - higher level takes care of RW/RO
2047 * flags so we should only get this if we are
2048 * writeable.
2049 */
Christoph Hellwig70246282016-07-19 11:28:41 +02002050 if (bio_data_dir(bio) == WRITE) {
Mikulas Patocka29138082009-04-02 19:55:25 +01002051 pe = __lookup_pending_exception(s, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002052 if (!pe) {
Nikos Tsironisf79ae412019-03-17 14:22:57 +02002053 dm_exception_table_unlock(&lock);
Mikulas Patockac6621392009-04-02 19:55:25 +01002054 pe = alloc_pending_exception(s);
Nikos Tsironisf79ae412019-03-17 14:22:57 +02002055 dm_exception_table_lock(&lock);
Mikulas Patockac6621392009-04-02 19:55:25 +01002056
Jon Brassow3510cb92009-12-10 23:52:11 +00002057 e = dm_lookup_exception(&s->complete, chunk);
Mikulas Patocka35bf6592009-04-02 19:55:26 +01002058 if (e) {
2059 free_pending_exception(pe);
2060 remap_exception(s, e, bio, chunk);
2061 goto out_unlock;
2062 }
2063
Mikulas Patockac6621392009-04-02 19:55:25 +01002064 pe = __find_pending_exception(s, pe, chunk);
Mikulas Patocka29138082009-04-02 19:55:25 +01002065 if (!pe) {
Nikos Tsironisf79ae412019-03-17 14:22:57 +02002066 dm_exception_table_unlock(&lock);
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02002067 up_read(&s->lock);
2068
2069 down_write(&s->lock);
Nikos Tsironisf79ae412019-03-17 14:22:57 +02002070
Mike Snitzerb0d3cc02015-10-08 18:05:41 -04002071 if (s->store->userspace_supports_overflow) {
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02002072 if (s->valid && !s->snapshot_overflowed) {
2073 s->snapshot_overflowed = 1;
2074 DMERR("Snapshot overflowed: Unable to allocate exception.");
2075 }
Mike Snitzerb0d3cc02015-10-08 18:05:41 -04002076 } else
2077 __invalidate_snapshot(s, -ENOMEM);
Nikos Tsironisf79ae412019-03-17 14:22:57 +02002078 up_write(&s->lock);
2079
Christoph Hellwig846785e2017-06-03 09:38:02 +02002080 r = DM_MAPIO_KILL;
Nikos Tsironisf79ae412019-03-17 14:22:57 +02002081 goto out;
Mikulas Patocka29138082009-04-02 19:55:25 +01002082 }
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002083 }
2084
Milan Brozd74f81f2008-02-08 02:11:27 +00002085 remap_exception(s, &pe->e, bio, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002086
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08002087 r = DM_MAPIO_SUBMITTED;
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07002088
Mike Snitzer2e602382019-06-19 17:05:54 -04002089 if (!pe->started && io_overlaps_chunk(s, bio)) {
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01002090 pe->started = 1;
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02002091
Nikos Tsironisf79ae412019-03-17 14:22:57 +02002092 dm_exception_table_unlock(&lock);
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02002093 up_read(&s->lock);
2094
Mikulas Patockaa6e50b42011-08-02 12:32:04 +01002095 start_full_bio(pe, bio);
2096 goto out;
2097 }
2098
2099 bio_list_add(&pe->snapshot_bios, bio);
2100
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002101 if (!pe->started) {
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02002102 /* this is protected by the exception table lock */
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002103 pe->started = 1;
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02002104
Nikos Tsironisf79ae412019-03-17 14:22:57 +02002105 dm_exception_table_unlock(&lock);
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02002106 up_read(&s->lock);
2107
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002108 start_copy(pe);
Alasdair G Kergonba40a2a2006-10-03 01:15:28 -07002109 goto out;
2110 }
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002111 } else {
Christoph Hellwig74d46992017-08-23 19:10:32 +02002112 bio_set_dev(bio, s->origin->bdev);
Mikulas Patockaee180262012-12-21 20:23:41 +00002113 track_chunk(s, bio, chunk);
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
Jonathan Brassowa2d2b032011-08-02 12:32:03 +01002116out_unlock:
Nikos Tsironisf79ae412019-03-17 14:22:57 +02002117 dm_exception_table_unlock(&lock);
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02002118 up_read(&s->lock);
Jonathan Brassowa2d2b032011-08-02 12:32:03 +01002119out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 return r;
2121}
2122
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00002123/*
2124 * A snapshot-merge target behaves like a combination of a snapshot
2125 * target and a snapshot-origin target. It only generates new
2126 * exceptions in other snapshots and not in the one that is being
2127 * merged.
2128 *
2129 * For each chunk, if there is an existing exception, it is used to
2130 * redirect I/O to the cow device. Otherwise I/O is sent to the origin,
2131 * which in turn might generate exceptions in other snapshots.
Mikulas Patocka9fe862542009-12-10 23:52:33 +00002132 * If merging is currently taking place on the chunk in question, the
2133 * I/O is deferred by adding it to s->bios_queued_during_merge.
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00002134 */
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00002135static int snapshot_merge_map(struct dm_target *ti, struct bio *bio)
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00002136{
2137 struct dm_exception *e;
2138 struct dm_snapshot *s = ti->private;
2139 int r = DM_MAPIO_REMAPPED;
2140 chunk_t chunk;
2141
Mikulas Patockaee180262012-12-21 20:23:41 +00002142 init_tracked_chunk(bio);
2143
Jens Axboe1eff9d32016-08-05 15:35:16 -06002144 if (bio->bi_opf & REQ_PREFLUSH) {
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00002145 if (!dm_bio_get_target_bio_nr(bio))
Christoph Hellwig74d46992017-08-23 19:10:32 +02002146 bio_set_dev(bio, s->origin->bdev);
Mike Snitzer10b81062009-12-10 23:52:31 +00002147 else
Christoph Hellwig74d46992017-08-23 19:10:32 +02002148 bio_set_dev(bio, s->cow->bdev);
Mike Snitzer10b81062009-12-10 23:52:31 +00002149 return DM_MAPIO_REMAPPED;
2150 }
2151
Mike Snitzer3ee25482019-07-17 11:12:30 -04002152 if (unlikely(bio_op(bio) == REQ_OP_DISCARD)) {
2153 /* Once merging, discards no longer effect change */
2154 bio_endio(bio);
2155 return DM_MAPIO_SUBMITTED;
2156 }
2157
Kent Overstreet4f024f32013-10-11 15:44:27 -07002158 chunk = sector_to_chunk(s->store, bio->bi_iter.bi_sector);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00002159
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02002160 down_write(&s->lock);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00002161
Mikulas Patockad2fdb772009-12-10 23:52:36 +00002162 /* Full merging snapshots are redirected to the origin */
2163 if (!s->valid)
2164 goto redirect_to_origin;
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00002165
2166 /* If the block is already remapped - use that */
2167 e = dm_lookup_exception(&s->complete, chunk);
2168 if (e) {
Mikulas Patocka9fe862542009-12-10 23:52:33 +00002169 /* Queue writes overlapping with chunks being merged */
Christoph Hellwig70246282016-07-19 11:28:41 +02002170 if (bio_data_dir(bio) == WRITE &&
Mikulas Patocka9fe862542009-12-10 23:52:33 +00002171 chunk >= s->first_merging_chunk &&
2172 chunk < (s->first_merging_chunk +
2173 s->num_merging_chunks)) {
Christoph Hellwig74d46992017-08-23 19:10:32 +02002174 bio_set_dev(bio, s->origin->bdev);
Mikulas Patocka9fe862542009-12-10 23:52:33 +00002175 bio_list_add(&s->bios_queued_during_merge, bio);
2176 r = DM_MAPIO_SUBMITTED;
2177 goto out_unlock;
2178 }
Mikulas Patocka17aa0332009-12-10 23:52:33 +00002179
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00002180 remap_exception(s, e, bio, chunk);
Mikulas Patocka17aa0332009-12-10 23:52:33 +00002181
Christoph Hellwig70246282016-07-19 11:28:41 +02002182 if (bio_data_dir(bio) == WRITE)
Mikulas Patockaee180262012-12-21 20:23:41 +00002183 track_chunk(s, bio, chunk);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00002184 goto out_unlock;
2185 }
2186
Mikulas Patockad2fdb772009-12-10 23:52:36 +00002187redirect_to_origin:
Christoph Hellwig74d46992017-08-23 19:10:32 +02002188 bio_set_dev(bio, s->origin->bdev);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00002189
Christoph Hellwig70246282016-07-19 11:28:41 +02002190 if (bio_data_dir(bio) == WRITE) {
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02002191 up_write(&s->lock);
Mikulas Patockab2155572019-10-02 06:15:53 -04002192 return do_origin(s->origin, bio, false);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00002193 }
2194
2195out_unlock:
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02002196 up_write(&s->lock);
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00002197
2198 return r;
2199}
2200
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002201static int snapshot_end_io(struct dm_target *ti, struct bio *bio,
2202 blk_status_t *error)
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002203{
2204 struct dm_snapshot *s = ti->private;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002205
Mikulas Patockaee180262012-12-21 20:23:41 +00002206 if (is_bio_tracked(bio))
2207 stop_tracking_chunk(s, bio);
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002208
Christoph Hellwig1be56902017-06-03 09:38:03 +02002209 return DM_ENDIO_DONE;
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002210}
2211
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002212static void snapshot_merge_presuspend(struct dm_target *ti)
2213{
2214 struct dm_snapshot *s = ti->private;
2215
2216 stop_merge(s);
2217}
2218
Mike Snitzerc1f0c182009-12-10 23:52:24 +00002219static int snapshot_preresume(struct dm_target *ti)
2220{
2221 int r = 0;
2222 struct dm_snapshot *s = ti->private;
2223 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
2224
2225 down_read(&_origins_lock);
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +00002226 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00002227 if (snap_src && snap_dest) {
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02002228 down_read(&snap_src->lock);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00002229 if (s == snap_src) {
2230 DMERR("Unable to resume snapshot source until "
2231 "handover completes.");
2232 r = -EINVAL;
Mike Snitzerb83b2f22011-01-13 19:59:59 +00002233 } else if (!dm_suspended(snap_src->ti)) {
Mike Snitzerc1f0c182009-12-10 23:52:24 +00002234 DMERR("Unable to perform snapshot handover until "
2235 "source is suspended.");
2236 r = -EINVAL;
2237 }
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02002238 up_read(&snap_src->lock);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00002239 }
2240 up_read(&_origins_lock);
2241
2242 return r;
2243}
2244
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245static void snapshot_resume(struct dm_target *ti)
2246{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002247 struct dm_snapshot *s = ti->private;
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05002248 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL, *snap_merging = NULL;
Mikulas Patockab735fed2015-02-26 11:40:35 -05002249 struct dm_origin *o;
2250 struct mapped_device *origin_md = NULL;
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05002251 bool must_restart_merging = false;
Mike Snitzerc1f0c182009-12-10 23:52:24 +00002252
2253 down_read(&_origins_lock);
Mikulas Patockab735fed2015-02-26 11:40:35 -05002254
2255 o = __lookup_dm_origin(s->origin->bdev);
2256 if (o)
2257 origin_md = dm_table_get_md(o->ti->table);
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05002258 if (!origin_md) {
2259 (void) __find_snapshots_sharing_cow(s, NULL, NULL, &snap_merging);
2260 if (snap_merging)
2261 origin_md = dm_table_get_md(snap_merging->ti->table);
2262 }
Mikulas Patockab735fed2015-02-26 11:40:35 -05002263 if (origin_md == dm_table_get_md(ti->table))
2264 origin_md = NULL;
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05002265 if (origin_md) {
2266 if (dm_hold(origin_md))
2267 origin_md = NULL;
2268 }
Mikulas Patockab735fed2015-02-26 11:40:35 -05002269
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05002270 up_read(&_origins_lock);
2271
2272 if (origin_md) {
Mikulas Patockab735fed2015-02-26 11:40:35 -05002273 dm_internal_suspend_fast(origin_md);
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05002274 if (snap_merging && test_bit(RUNNING_MERGE, &snap_merging->state_bits)) {
2275 must_restart_merging = true;
2276 stop_merge(snap_merging);
2277 }
2278 }
2279
2280 down_read(&_origins_lock);
Mikulas Patockab735fed2015-02-26 11:40:35 -05002281
Mikulas Patocka9d3b15c2009-12-10 23:52:32 +00002282 (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00002283 if (snap_src && snap_dest) {
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02002284 down_write(&snap_src->lock);
2285 down_write_nested(&snap_dest->lock, SINGLE_DEPTH_NESTING);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00002286 __handover_exceptions(snap_src, snap_dest);
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02002287 up_write(&snap_dest->lock);
2288 up_write(&snap_src->lock);
Mike Snitzerc1f0c182009-12-10 23:52:24 +00002289 }
Mikulas Patockab735fed2015-02-26 11:40:35 -05002290
Mike Snitzerc1f0c182009-12-10 23:52:24 +00002291 up_read(&_origins_lock);
2292
Mikulas Patocka09ee96b2015-02-26 11:41:28 -05002293 if (origin_md) {
2294 if (must_restart_merging)
2295 start_merge(snap_merging);
2296 dm_internal_resume_fast(origin_md);
2297 dm_put(origin_md);
2298 }
2299
Mike Snitzerc1f0c182009-12-10 23:52:24 +00002300 /* Now we have correct chunk size, reregister */
2301 reregister_snapshot(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02002303 down_write(&s->lock);
Alasdair G Kergonaa14ede2006-02-01 03:04:50 -08002304 s->active = 1;
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02002305 up_write(&s->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306}
2307
Mike Snitzer542f9032012-07-27 15:08:00 +01002308static uint32_t get_origin_minimum_chunksize(struct block_device *bdev)
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002309{
Mike Snitzer542f9032012-07-27 15:08:00 +01002310 uint32_t min_chunksize;
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002311
2312 down_read(&_origins_lock);
2313 min_chunksize = __minimum_chunk_size(__lookup_origin(bdev));
2314 up_read(&_origins_lock);
2315
2316 return min_chunksize;
2317}
2318
2319static void snapshot_merge_resume(struct dm_target *ti)
2320{
2321 struct dm_snapshot *s = ti->private;
2322
2323 /*
2324 * Handover exceptions from existing snapshot.
2325 */
2326 snapshot_resume(ti);
2327
2328 /*
Mike Snitzer542f9032012-07-27 15:08:00 +01002329 * snapshot-merge acts as an origin, so set ti->max_io_len
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002330 */
Mike Snitzer542f9032012-07-27 15:08:00 +01002331 ti->max_io_len = get_origin_minimum_chunksize(s->origin->bdev);
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002332
2333 start_merge(s);
2334}
2335
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002336static void snapshot_status(struct dm_target *ti, status_type_t type,
2337 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338{
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01002339 unsigned sz = 0;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002340 struct dm_snapshot *snap = ti->private;
Mike Snitzer2e602382019-06-19 17:05:54 -04002341 unsigned num_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
2343 switch (type) {
2344 case STATUSTYPE_INFO:
Mikulas Patocka94e765722009-12-10 23:51:53 +00002345
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02002346 down_write(&snap->lock);
Mikulas Patocka94e765722009-12-10 23:51:53 +00002347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 if (!snap->valid)
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01002349 DMEMIT("Invalid");
Mike Snitzerd8ddb1c2009-12-10 23:52:35 +00002350 else if (snap->merge_failed)
2351 DMEMIT("Merge failed");
Mikulas Patocka76c44f62015-06-21 16:31:33 -04002352 else if (snap->snapshot_overflowed)
2353 DMEMIT("Overflow");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 else {
Mike Snitzer985903b2009-12-10 23:52:11 +00002355 if (snap->store->type->usage) {
2356 sector_t total_sectors, sectors_allocated,
2357 metadata_sectors;
2358 snap->store->type->usage(snap->store,
2359 &total_sectors,
2360 &sectors_allocated,
2361 &metadata_sectors);
2362 DMEMIT("%llu/%llu %llu",
2363 (unsigned long long)sectors_allocated,
2364 (unsigned long long)total_sectors,
2365 (unsigned long long)metadata_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 }
2367 else
Jonathan Brassow2e4a31d2009-04-02 19:55:34 +01002368 DMEMIT("Unknown");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 }
Mikulas Patocka94e765722009-12-10 23:51:53 +00002370
Nikos Tsironis4ad8d882019-03-17 14:22:56 +02002371 up_write(&snap->lock);
Mikulas Patocka94e765722009-12-10 23:51:53 +00002372
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 break;
2374
2375 case STATUSTYPE_TABLE:
2376 /*
2377 * kdevname returns a static pointer so we need
2378 * to make private copies if the output is to
2379 * make sense.
2380 */
Mike Snitzerfc56f6f2009-12-10 23:52:12 +00002381 DMEMIT("%s %s", snap->origin->name, snap->cow->name);
Mike Snitzer2e602382019-06-19 17:05:54 -04002382 sz += snap->store->type->status(snap->store, type, result + sz,
2383 maxlen - sz);
2384 num_features = snap->discard_zeroes_cow + snap->discard_passdown_origin;
2385 if (num_features) {
2386 DMEMIT(" %u", num_features);
2387 if (snap->discard_zeroes_cow)
2388 DMEMIT(" discard_zeroes_cow");
2389 if (snap->discard_passdown_origin)
2390 DMEMIT(" discard_passdown_origin");
2391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 break;
Tushar Sugandhi8ec45662021-07-12 17:49:03 -07002393
2394 case STATUSTYPE_IMA:
2395 DMEMIT_TARGET_NAME_VERSION(ti->type);
2396 DMEMIT(",snap_origin_name=%s", snap->origin->name);
2397 DMEMIT(",snap_cow_name=%s", snap->cow->name);
2398 DMEMIT(",snap_valid=%c", snap->valid ? 'y' : 'n');
2399 DMEMIT(",snap_merge_failed=%c", snap->merge_failed ? 'y' : 'n');
2400 DMEMIT(",snapshot_overflowed=%c", snap->snapshot_overflowed ? 'y' : 'n');
2401 DMEMIT(";");
2402 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404}
2405
Mike Snitzer8811f462009-09-04 20:40:19 +01002406static int snapshot_iterate_devices(struct dm_target *ti,
2407 iterate_devices_callout_fn fn, void *data)
2408{
2409 struct dm_snapshot *snap = ti->private;
Mikulas Patocka1e5554c2010-08-12 04:13:50 +01002410 int r;
Mike Snitzer8811f462009-09-04 20:40:19 +01002411
Mikulas Patocka1e5554c2010-08-12 04:13:50 +01002412 r = fn(ti, snap->origin, 0, ti->len, data);
2413
2414 if (!r)
2415 r = fn(ti, snap->cow, 0, get_dev_size(snap->cow->bdev), data);
2416
2417 return r;
Mike Snitzer8811f462009-09-04 20:40:19 +01002418}
2419
Mike Snitzer2e602382019-06-19 17:05:54 -04002420static void snapshot_io_hints(struct dm_target *ti, struct queue_limits *limits)
2421{
2422 struct dm_snapshot *snap = ti->private;
2423
2424 if (snap->discard_zeroes_cow) {
2425 struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
2426
Mike Snitzer3ee25482019-07-17 11:12:30 -04002427 down_read(&_origins_lock);
2428
Mike Snitzer2e602382019-06-19 17:05:54 -04002429 (void) __find_snapshots_sharing_cow(snap, &snap_src, &snap_dest, NULL);
2430 if (snap_src && snap_dest)
2431 snap = snap_src;
2432
2433 /* All discards are split on chunk_size boundary */
2434 limits->discard_granularity = snap->store->chunk_size;
2435 limits->max_discard_sectors = snap->store->chunk_size;
Mike Snitzer3ee25482019-07-17 11:12:30 -04002436
2437 up_read(&_origins_lock);
Mike Snitzer2e602382019-06-19 17:05:54 -04002438 }
2439}
Mike Snitzer8811f462009-09-04 20:40:19 +01002440
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441/*-----------------------------------------------------------------
2442 * Origin methods
2443 *---------------------------------------------------------------*/
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00002444
2445/*
2446 * If no exceptions need creating, DM_MAPIO_REMAPPED is returned and any
2447 * supplied bio was ignored. The caller may submit it immediately.
2448 * (No remapping actually occurs as the origin is always a direct linear
2449 * map.)
2450 *
2451 * If further exceptions are required, DM_MAPIO_SUBMITTED is returned
2452 * and any supplied bio is added to a list to be submitted once all
2453 * the necessary exceptions exist.
2454 */
2455static int __origin_write(struct list_head *snapshots, sector_t sector,
2456 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457{
Mikulas Patocka515ad662009-12-10 23:52:30 +00002458 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 struct dm_snapshot *snap;
Jon Brassow1d4989c2009-12-10 23:52:10 +00002460 struct dm_exception *e;
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02002461 struct dm_snap_pending_exception *pe, *pe2;
Mikulas Patocka515ad662009-12-10 23:52:30 +00002462 struct dm_snap_pending_exception *pe_to_start_now = NULL;
2463 struct dm_snap_pending_exception *pe_to_start_last = NULL;
Nikos Tsironisf79ae412019-03-17 14:22:57 +02002464 struct dm_exception_table_lock lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 chunk_t chunk;
2466
2467 /* Do all the snapshots on this origin */
2468 list_for_each_entry (snap, snapshots, list) {
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00002469 /*
2470 * Don't make new exceptions in a merging snapshot
2471 * because it has effectively been deleted
2472 */
2473 if (dm_target_is_snapshot_merge(snap->ti))
2474 continue;
2475
Alasdair G Kergond5e404c2005-07-12 15:53:05 -07002476 /* Nothing to do if writing beyond end of snapshot */
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00002477 if (sector >= dm_table_get_size(snap->ti->table))
Nikos Tsironisf79ae412019-03-17 14:22:57 +02002478 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
2480 /*
2481 * Remember, different snapshots can have
2482 * different chunk sizes.
2483 */
Mikulas Patocka9eaae8f2009-12-10 23:52:28 +00002484 chunk = sector_to_chunk(snap->store, sector);
Nikos Tsironisf79ae412019-03-17 14:22:57 +02002485 dm_exception_table_lock_init(snap, chunk, &lock);
2486
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02002487 down_read(&snap->lock);
Nikos Tsironisf79ae412019-03-17 14:22:57 +02002488 dm_exception_table_lock(&lock);
2489
2490 /* Only deal with valid and active snapshots */
2491 if (!snap->valid || !snap->active)
2492 goto next_snapshot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
Mikulas Patocka29138082009-04-02 19:55:25 +01002494 pe = __lookup_pending_exception(snap, chunk);
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002495 if (!pe) {
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02002496 /*
2497 * Check exception table to see if block is already
2498 * remapped in this snapshot and trigger an exception
2499 * if not.
2500 */
2501 e = dm_lookup_exception(&snap->complete, chunk);
2502 if (e)
2503 goto next_snapshot;
2504
Nikos Tsironisf79ae412019-03-17 14:22:57 +02002505 dm_exception_table_unlock(&lock);
Mikulas Patockac6621392009-04-02 19:55:25 +01002506 pe = alloc_pending_exception(snap);
Nikos Tsironisf79ae412019-03-17 14:22:57 +02002507 dm_exception_table_lock(&lock);
Mikulas Patockac6621392009-04-02 19:55:25 +01002508
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02002509 pe2 = __lookup_pending_exception(snap, chunk);
Mikulas Patocka35bf6592009-04-02 19:55:26 +01002510
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02002511 if (!pe2) {
2512 e = dm_lookup_exception(&snap->complete, chunk);
2513 if (e) {
2514 free_pending_exception(pe);
2515 goto next_snapshot;
2516 }
2517
2518 pe = __insert_pending_exception(snap, pe, chunk);
2519 if (!pe) {
Nikos Tsironisf79ae412019-03-17 14:22:57 +02002520 dm_exception_table_unlock(&lock);
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02002521 up_read(&snap->lock);
Nikos Tsironisf79ae412019-03-17 14:22:57 +02002522
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02002523 invalidate_snapshot(snap, -ENOMEM);
Nikos Tsironisf79ae412019-03-17 14:22:57 +02002524 continue;
Nikos Tsironis65fc7c32019-03-17 14:22:55 +02002525 }
2526 } else {
2527 free_pending_exception(pe);
2528 pe = pe2;
Mikulas Patocka29138082009-04-02 19:55:25 +01002529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 }
2531
Mikulas Patocka515ad662009-12-10 23:52:30 +00002532 r = DM_MAPIO_SUBMITTED;
2533
2534 /*
2535 * If an origin bio was supplied, queue it to wait for the
2536 * completion of this exception, and start this one last,
2537 * at the end of the function.
2538 */
2539 if (bio) {
2540 bio_list_add(&pe->origin_bios, bio);
2541 bio = NULL;
2542
2543 if (!pe->started) {
2544 pe->started = 1;
2545 pe_to_start_last = pe;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002546 }
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002547 }
2548
2549 if (!pe->started) {
2550 pe->started = 1;
Mikulas Patocka515ad662009-12-10 23:52:30 +00002551 pe_to_start_now = pe;
Alasdair G Kergon76df1c62006-03-27 01:17:45 -08002552 }
2553
Jonathan Brassowa2d2b032011-08-02 12:32:03 +01002554next_snapshot:
Nikos Tsironisf79ae412019-03-17 14:22:57 +02002555 dm_exception_table_unlock(&lock);
Nikos Tsironis3f1637f2019-03-17 14:22:58 +02002556 up_read(&snap->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
Mikulas Patocka515ad662009-12-10 23:52:30 +00002558 if (pe_to_start_now) {
2559 start_copy(pe_to_start_now);
2560 pe_to_start_now = NULL;
2561 }
Alasdair G Kergonb4b610f2006-03-27 01:17:44 -08002562 }
2563
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 /*
Mikulas Patocka515ad662009-12-10 23:52:30 +00002565 * Submit the exception against which the bio is queued last,
2566 * to give the other exceptions a head start.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 */
Mikulas Patocka515ad662009-12-10 23:52:30 +00002568 if (pe_to_start_last)
2569 start_copy(pe_to_start_last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
2571 return r;
2572}
2573
2574/*
2575 * Called on a write from the origin driver.
2576 */
Mikulas Patockab2155572019-10-02 06:15:53 -04002577static int do_origin(struct dm_dev *origin, struct bio *bio, bool limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578{
2579 struct origin *o;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08002580 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
Mikulas Patockab2155572019-10-02 06:15:53 -04002582again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 down_read(&_origins_lock);
2584 o = __lookup_origin(origin->bdev);
Mikulas Patockab2155572019-10-02 06:15:53 -04002585 if (o) {
2586 if (limit) {
2587 struct dm_snapshot *s;
2588 list_for_each_entry(s, &o->snapshots, list)
2589 if (unlikely(!wait_for_in_progress(s, true)))
2590 goto again;
2591 }
2592
Kent Overstreet4f024f32013-10-11 15:44:27 -07002593 r = __origin_write(&o->snapshots, bio->bi_iter.bi_sector, bio);
Mikulas Patockab2155572019-10-02 06:15:53 -04002594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 up_read(&_origins_lock);
2596
2597 return r;
2598}
2599
2600/*
Mikulas Patocka73dfd072009-12-10 23:52:34 +00002601 * Trigger exceptions in all non-merging snapshots.
2602 *
2603 * The chunk size of the merging snapshot may be larger than the chunk
2604 * size of some other snapshot so we may need to reallocate multiple
2605 * chunks in other snapshots.
2606 *
2607 * We scan all the overlapping exceptions in the other snapshots.
2608 * Returns 1 if anything was reallocated and must be waited for,
2609 * otherwise returns 0.
2610 *
2611 * size must be a multiple of merging_snap's chunk_size.
2612 */
2613static int origin_write_extent(struct dm_snapshot *merging_snap,
2614 sector_t sector, unsigned size)
2615{
2616 int must_wait = 0;
2617 sector_t n;
2618 struct origin *o;
2619
2620 /*
Mike Snitzer542f9032012-07-27 15:08:00 +01002621 * The origin's __minimum_chunk_size() got stored in max_io_len
Mikulas Patocka73dfd072009-12-10 23:52:34 +00002622 * by snapshot_merge_resume().
2623 */
2624 down_read(&_origins_lock);
2625 o = __lookup_origin(merging_snap->origin->bdev);
Mike Snitzer542f9032012-07-27 15:08:00 +01002626 for (n = 0; n < size; n += merging_snap->ti->max_io_len)
Mikulas Patocka73dfd072009-12-10 23:52:34 +00002627 if (__origin_write(&o->snapshots, sector + n, NULL) ==
2628 DM_MAPIO_SUBMITTED)
2629 must_wait = 1;
2630 up_read(&_origins_lock);
2631
2632 return must_wait;
2633}
2634
2635/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 * Origin: maps a linear range of a device, with hooks for snapshotting.
2637 */
2638
2639/*
2640 * Construct an origin mapping: <dev_path>
2641 * The context for an origin is merely a 'struct dm_dev *'
2642 * pointing to the real device.
2643 */
2644static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
2645{
2646 int r;
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002647 struct dm_origin *o;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
2649 if (argc != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002650 ti->error = "origin: incorrect number of arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 return -EINVAL;
2652 }
2653
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002654 o = kmalloc(sizeof(struct dm_origin), GFP_KERNEL);
2655 if (!o) {
2656 ti->error = "Cannot allocate private origin structure";
2657 r = -ENOMEM;
2658 goto bad_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 }
2660
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002661 r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &o->dev);
2662 if (r) {
2663 ti->error = "Cannot get target device";
2664 goto bad_open;
2665 }
2666
Mikulas Patockab735fed2015-02-26 11:40:35 -05002667 o->ti = ti;
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002668 ti->private = o;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00002669 ti->num_flush_bios = 1;
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01002670
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 return 0;
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002672
2673bad_open:
2674 kfree(o);
2675bad_alloc:
2676 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677}
2678
2679static void origin_dtr(struct dm_target *ti)
2680{
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002681 struct dm_origin *o = ti->private;
Mikulas Patockab735fed2015-02-26 11:40:35 -05002682
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002683 dm_put_device(ti, o->dev);
2684 kfree(o);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685}
2686
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00002687static int origin_map(struct dm_target *ti, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688{
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002689 struct dm_origin *o = ti->private;
Mikulas Patocka298eaa82014-03-14 18:43:07 -04002690 unsigned available_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691
Christoph Hellwig74d46992017-08-23 19:10:32 +02002692 bio_set_dev(bio, o->dev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693
Jens Axboe1eff9d32016-08-05 15:35:16 -06002694 if (unlikely(bio->bi_opf & REQ_PREFLUSH))
Mikulas Patocka494b3ee2009-06-22 10:12:25 +01002695 return DM_MAPIO_REMAPPED;
2696
Christoph Hellwig70246282016-07-19 11:28:41 +02002697 if (bio_data_dir(bio) != WRITE)
Mikulas Patocka298eaa82014-03-14 18:43:07 -04002698 return DM_MAPIO_REMAPPED;
2699
2700 available_sectors = o->split_boundary -
2701 ((unsigned)bio->bi_iter.bi_sector & (o->split_boundary - 1));
2702
2703 if (bio_sectors(bio) > available_sectors)
2704 dm_accept_partial_bio(bio, available_sectors);
2705
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 /* Only tell snapshots if this is a write */
Mikulas Patockab2155572019-10-02 06:15:53 -04002707 return do_origin(o->dev, bio, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708}
2709
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710/*
Mike Snitzer542f9032012-07-27 15:08:00 +01002711 * Set the target "max_io_len" field to the minimum of all the snapshots'
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 * chunk sizes.
2713 */
2714static void origin_resume(struct dm_target *ti)
2715{
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002716 struct dm_origin *o = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
Mikulas Patocka298eaa82014-03-14 18:43:07 -04002718 o->split_boundary = get_origin_minimum_chunksize(o->dev->bdev);
Mikulas Patockab735fed2015-02-26 11:40:35 -05002719
2720 down_write(&_origins_lock);
2721 __insert_dm_origin(o);
2722 up_write(&_origins_lock);
2723}
2724
2725static void origin_postsuspend(struct dm_target *ti)
2726{
2727 struct dm_origin *o = ti->private;
2728
2729 down_write(&_origins_lock);
2730 __remove_dm_origin(o);
2731 up_write(&_origins_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732}
2733
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002734static void origin_status(struct dm_target *ti, status_type_t type,
2735 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736{
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002737 struct dm_origin *o = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
2739 switch (type) {
2740 case STATUSTYPE_INFO:
2741 result[0] = '\0';
2742 break;
2743
2744 case STATUSTYPE_TABLE:
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002745 snprintf(result, maxlen, "%s", o->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 break;
Tushar Sugandhi8ec45662021-07-12 17:49:03 -07002747 case STATUSTYPE_IMA:
2748 result[0] = '\0';
2749 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751}
2752
Mike Snitzer8811f462009-09-04 20:40:19 +01002753static int origin_iterate_devices(struct dm_target *ti,
2754 iterate_devices_callout_fn fn, void *data)
2755{
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002756 struct dm_origin *o = ti->private;
Mike Snitzer8811f462009-09-04 20:40:19 +01002757
Mikulas Patocka599cdf32014-03-14 18:42:12 -04002758 return fn(ti, o->dev, 0, ti->len, data);
Mike Snitzer8811f462009-09-04 20:40:19 +01002759}
2760
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761static struct target_type origin_target = {
2762 .name = "snapshot-origin",
Mikulas Patockab735fed2015-02-26 11:40:35 -05002763 .version = {1, 9, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 .module = THIS_MODULE,
2765 .ctr = origin_ctr,
2766 .dtr = origin_dtr,
2767 .map = origin_map,
2768 .resume = origin_resume,
Mikulas Patockab735fed2015-02-26 11:40:35 -05002769 .postsuspend = origin_postsuspend,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 .status = origin_status,
Mike Snitzer8811f462009-09-04 20:40:19 +01002771 .iterate_devices = origin_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772};
2773
2774static struct target_type snapshot_target = {
2775 .name = "snapshot",
Mike Snitzer2e602382019-06-19 17:05:54 -04002776 .version = {1, 16, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 .module = THIS_MODULE,
2778 .ctr = snapshot_ctr,
2779 .dtr = snapshot_dtr,
2780 .map = snapshot_map,
Mikulas Patockacd45daf2008-07-21 12:00:32 +01002781 .end_io = snapshot_end_io,
Mike Snitzerc1f0c182009-12-10 23:52:24 +00002782 .preresume = snapshot_preresume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 .resume = snapshot_resume,
2784 .status = snapshot_status,
Mike Snitzer8811f462009-09-04 20:40:19 +01002785 .iterate_devices = snapshot_iterate_devices,
Mike Snitzer2e602382019-06-19 17:05:54 -04002786 .io_hints = snapshot_io_hints,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787};
2788
Mikulas Patockad698aa42009-12-10 23:52:30 +00002789static struct target_type merge_target = {
2790 .name = dm_snapshot_merge_target_name,
Mike Snitzer2e602382019-06-19 17:05:54 -04002791 .version = {1, 5, 0},
Mikulas Patockad698aa42009-12-10 23:52:30 +00002792 .module = THIS_MODULE,
2793 .ctr = snapshot_ctr,
2794 .dtr = snapshot_dtr,
Mikulas Patocka3452c2a2009-12-10 23:52:31 +00002795 .map = snapshot_merge_map,
Mikulas Patockad698aa42009-12-10 23:52:30 +00002796 .end_io = snapshot_end_io,
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002797 .presuspend = snapshot_merge_presuspend,
Mikulas Patockad698aa42009-12-10 23:52:30 +00002798 .preresume = snapshot_preresume,
Mikulas Patocka1e03f972009-12-10 23:52:32 +00002799 .resume = snapshot_merge_resume,
Mikulas Patockad698aa42009-12-10 23:52:30 +00002800 .status = snapshot_status,
2801 .iterate_devices = snapshot_iterate_devices,
Mike Snitzer2e602382019-06-19 17:05:54 -04002802 .io_hints = snapshot_io_hints,
Mikulas Patockad698aa42009-12-10 23:52:30 +00002803};
2804
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805static int __init dm_snapshot_init(void)
2806{
2807 int r;
2808
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00002809 r = dm_exception_store_init();
2810 if (r) {
2811 DMERR("Failed to initialize exception stores");
2812 return r;
2813 }
2814
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 r = init_origin_hash();
2816 if (r) {
2817 DMERR("init_origin_hash failed.");
Mikulas Patockad698aa42009-12-10 23:52:30 +00002818 goto bad_origin_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 }
2820
Jon Brassow1d4989c2009-12-10 23:52:10 +00002821 exception_cache = KMEM_CACHE(dm_exception, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 if (!exception_cache) {
2823 DMERR("Couldn't create exception cache.");
2824 r = -ENOMEM;
Mikulas Patockad698aa42009-12-10 23:52:30 +00002825 goto bad_exception_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 }
2827
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002828 pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 if (!pending_cache) {
2830 DMERR("Couldn't create pending cache.");
2831 r = -ENOMEM;
Mikulas Patockad698aa42009-12-10 23:52:30 +00002832 goto bad_pending_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 }
2834
monty_pavel@sina.com7e6358d2017-11-25 01:43:50 +08002835 r = dm_register_target(&snapshot_target);
2836 if (r < 0) {
2837 DMERR("snapshot target register failed %d", r);
2838 goto bad_register_snapshot_target;
2839 }
2840
2841 r = dm_register_target(&origin_target);
2842 if (r < 0) {
2843 DMERR("Origin target register failed %d", r);
2844 goto bad_register_origin_target;
2845 }
2846
2847 r = dm_register_target(&merge_target);
2848 if (r < 0) {
2849 DMERR("Merge target register failed %d", r);
2850 goto bad_register_merge_target;
2851 }
2852
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 return 0;
2854
Mikulas Patockad698aa42009-12-10 23:52:30 +00002855bad_register_merge_target:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 dm_unregister_target(&origin_target);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002857bad_register_origin_target:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 dm_unregister_target(&snapshot_target);
Jonathan Brassow034a1862009-10-16 23:18:14 +01002859bad_register_snapshot_target:
monty_pavel@sina.com7e6358d2017-11-25 01:43:50 +08002860 kmem_cache_destroy(pending_cache);
2861bad_pending_cache:
2862 kmem_cache_destroy(exception_cache);
2863bad_exception_cache:
2864 exit_origin_hash();
2865bad_origin_hash:
Jonathan Brassow034a1862009-10-16 23:18:14 +01002866 dm_exception_store_exit();
Mikulas Patockad698aa42009-12-10 23:52:30 +00002867
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 return r;
2869}
2870
2871static void __exit dm_snapshot_exit(void)
2872{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00002873 dm_unregister_target(&snapshot_target);
2874 dm_unregister_target(&origin_target);
Mikulas Patockad698aa42009-12-10 23:52:30 +00002875 dm_unregister_target(&merge_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
2877 exit_origin_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 kmem_cache_destroy(pending_cache);
2879 kmem_cache_destroy(exception_cache);
Alasdair G Kergon4db6bfe2009-01-06 03:05:17 +00002880
2881 dm_exception_store_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882}
2883
2884/* Module hooks */
2885module_init(dm_snapshot_init);
2886module_exit(dm_snapshot_exit);
2887
2888MODULE_DESCRIPTION(DM_NAME " snapshot target");
2889MODULE_AUTHOR("Joe Thornber");
2890MODULE_LICENSE("GPL");
Mikulas Patocka23cb2102013-03-01 22:45:47 +00002891MODULE_ALIAS("dm-snapshot-origin");
2892MODULE_ALIAS("dm-snapshot-merge");