Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * dm-snapshot.c |
| 3 | * |
| 4 | * Copyright (C) 2001-2002 Sistina Software (UK) Limited. |
| 5 | * |
| 6 | * This file is released under the GPL. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/blkdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/device-mapper.h> |
Mikulas Patocka | 90fa152 | 2009-01-06 03:04:54 +0000 | [diff] [blame] | 11 | #include <linux/delay.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/fs.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/kdev_t.h> |
| 15 | #include <linux/list.h> |
| 16 | #include <linux/mempool.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/vmalloc.h> |
vignesh babu | 6f3c3f0 | 2007-10-19 22:38:44 +0100 | [diff] [blame] | 20 | #include <linux/log2.h> |
Alasdair G Kergon | a765e20 | 2008-04-24 22:02:01 +0100 | [diff] [blame] | 21 | #include <linux/dm-kcopyd.h> |
Nikos Tsironis | 721b1d9 | 2018-10-31 17:53:08 -0400 | [diff] [blame] | 22 | #include <linux/semaphore.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Mikulas Patocka | b735fed | 2015-02-26 11:40:35 -0500 | [diff] [blame] | 24 | #include "dm.h" |
| 25 | |
Jonathan Brassow | aea53d9 | 2009-01-06 03:05:15 +0000 | [diff] [blame] | 26 | #include "dm-exception-store.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 28 | #define DM_MSG_PREFIX "snapshots" |
| 29 | |
Mikulas Patocka | d698aa4 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 30 | static const char dm_snapshot_merge_target_name[] = "snapshot-merge"; |
| 31 | |
| 32 | #define dm_target_is_snapshot_merge(ti) \ |
| 33 | ((ti)->type->name == dm_snapshot_merge_target_name) |
| 34 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | /* |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 36 | * The size of the mempool used to track chunks in use. |
| 37 | */ |
| 38 | #define MIN_IOS 256 |
| 39 | |
Jonathan Brassow | ccc45ea | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 40 | #define DM_TRACKED_CHUNK_HASH_SIZE 16 |
| 41 | #define DM_TRACKED_CHUNK_HASH(x) ((unsigned long)(x) & \ |
| 42 | (DM_TRACKED_CHUNK_HASH_SIZE - 1)) |
| 43 | |
Jon Brassow | 191437a | 2009-12-10 23:52:10 +0000 | [diff] [blame] | 44 | struct dm_exception_table { |
Jonathan Brassow | ccc45ea | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 45 | uint32_t hash_mask; |
| 46 | unsigned hash_shift; |
| 47 | struct list_head *table; |
| 48 | }; |
| 49 | |
| 50 | struct dm_snapshot { |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 51 | struct rw_semaphore lock; |
Jonathan Brassow | ccc45ea | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 52 | |
| 53 | struct dm_dev *origin; |
Mike Snitzer | fc56f6f | 2009-12-10 23:52:12 +0000 | [diff] [blame] | 54 | struct dm_dev *cow; |
| 55 | |
| 56 | struct dm_target *ti; |
Jonathan Brassow | ccc45ea | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 57 | |
| 58 | /* List of snapshots per Origin */ |
| 59 | struct list_head list; |
| 60 | |
Mike Snitzer | d8ddb1c | 2009-12-10 23:52:35 +0000 | [diff] [blame] | 61 | /* |
| 62 | * You can't use a snapshot if this is 0 (e.g. if full). |
| 63 | * A snapshot-merge target never clears this. |
| 64 | */ |
Jonathan Brassow | ccc45ea | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 65 | int valid; |
| 66 | |
Mikulas Patocka | 76c44f6 | 2015-06-21 16:31:33 -0400 | [diff] [blame] | 67 | /* |
| 68 | * The snapshot overflowed because of a write to the snapshot device. |
| 69 | * We don't have to invalidate the snapshot in this case, but we need |
| 70 | * to prevent further writes. |
| 71 | */ |
| 72 | int snapshot_overflowed; |
| 73 | |
Jonathan Brassow | ccc45ea | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 74 | /* Origin writes don't trigger exceptions until this is set */ |
| 75 | int active; |
| 76 | |
Jonathan Brassow | ccc45ea | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 77 | atomic_t pending_exceptions_count; |
| 78 | |
Mikulas Patocka | 230c83a | 2013-11-29 18:13:37 -0500 | [diff] [blame] | 79 | /* Protected by "lock" */ |
| 80 | sector_t exception_start_sequence; |
| 81 | |
| 82 | /* Protected by kcopyd single-threaded callback */ |
| 83 | sector_t exception_complete_sequence; |
| 84 | |
| 85 | /* |
| 86 | * A list of pending exceptions that completed out of order. |
| 87 | * Protected by kcopyd single-threaded callback. |
| 88 | */ |
David Jeffery | 3db2776 | 2018-08-07 16:56:00 -0400 | [diff] [blame] | 89 | struct rb_root out_of_order_tree; |
Mikulas Patocka | 230c83a | 2013-11-29 18:13:37 -0500 | [diff] [blame] | 90 | |
Kent Overstreet | 6f1c819 | 2018-05-20 18:25:53 -0400 | [diff] [blame] | 91 | mempool_t pending_pool; |
Mike Snitzer | 924e600 | 2010-03-06 02:32:33 +0000 | [diff] [blame] | 92 | |
Jon Brassow | 191437a | 2009-12-10 23:52:10 +0000 | [diff] [blame] | 93 | struct dm_exception_table pending; |
| 94 | struct dm_exception_table complete; |
Jonathan Brassow | ccc45ea | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 95 | |
| 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 Snitzer | 924e600 | 2010-03-06 02:32:33 +0000 | [diff] [blame] | 102 | /* Chunks with outstanding reads */ |
| 103 | spinlock_t tracked_chunk_lock; |
Mike Snitzer | 924e600 | 2010-03-06 02:32:33 +0000 | [diff] [blame] | 104 | struct hlist_head tracked_chunk_hash[DM_TRACKED_CHUNK_HASH_SIZE]; |
| 105 | |
Jonathan Brassow | ccc45ea | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 106 | /* The on disk metadata handler */ |
| 107 | struct dm_exception_store *store; |
| 108 | |
Nikos Tsironis | 721b1d9 | 2018-10-31 17:53:08 -0400 | [diff] [blame] | 109 | /* Maximum number of in-flight COW jobs. */ |
| 110 | struct semaphore cow_count; |
| 111 | |
Jonathan Brassow | ccc45ea | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 112 | struct dm_kcopyd_client *kcopyd_client; |
| 113 | |
Mike Snitzer | 924e600 | 2010-03-06 02:32:33 +0000 | [diff] [blame] | 114 | /* 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 Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 120 | |
Mike Snitzer | d8ddb1c | 2009-12-10 23:52:35 +0000 | [diff] [blame] | 121 | /* |
| 122 | * The merge operation failed if this flag is set. |
| 123 | * Failure modes are handled as follows: |
| 124 | * - I/O error reading the header |
| 125 | * => don't load the target; abort. |
| 126 | * - Header does not have "valid" flag set |
| 127 | * => use the origin; forget about the snapshot. |
| 128 | * - I/O error when reading exceptions |
| 129 | * => don't load the target; abort. |
| 130 | * (We can't use the intermediate origin state.) |
| 131 | * - I/O error while merging |
| 132 | * => stop merging; set merge_failed; process I/O normally. |
| 133 | */ |
| 134 | int merge_failed; |
| 135 | |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 136 | /* |
| 137 | * Incoming bios that overlap with chunks being merged must wait |
| 138 | * for them to be committed. |
| 139 | */ |
| 140 | struct bio_list bios_queued_during_merge; |
Jonathan Brassow | ccc45ea | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 141 | }; |
| 142 | |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 143 | /* |
| 144 | * state_bits: |
| 145 | * RUNNING_MERGE - Merge operation is in progress. |
| 146 | * SHUTDOWN_MERGE - Set to signal that merge needs to be stopped; |
| 147 | * cleared afterwards. |
| 148 | */ |
| 149 | #define RUNNING_MERGE 0 |
| 150 | #define SHUTDOWN_MERGE 1 |
| 151 | |
Nikos Tsironis | 721b1d9 | 2018-10-31 17:53:08 -0400 | [diff] [blame] | 152 | /* |
| 153 | * Maximum number of chunks being copied on write. |
| 154 | * |
| 155 | * The value was decided experimentally as a trade-off between memory |
| 156 | * consumption, stalling the kernel's workqueues and maintaining a high enough |
| 157 | * throughput. |
| 158 | */ |
| 159 | #define DEFAULT_COW_THRESHOLD 2048 |
| 160 | |
| 161 | static int cow_threshold = DEFAULT_COW_THRESHOLD; |
| 162 | module_param_named(snapshot_cow_threshold, cow_threshold, int, 0644); |
| 163 | MODULE_PARM_DESC(snapshot_cow_threshold, "Maximum number of chunks being copied on write"); |
| 164 | |
Mikulas Patocka | df5d2e9 | 2013-03-01 22:45:49 +0000 | [diff] [blame] | 165 | DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(snapshot_copy_throttle, |
| 166 | "A percentage of time allocated for copy on write"); |
| 167 | |
Mikulas Patocka | c241104 | 2010-08-12 04:13:51 +0100 | [diff] [blame] | 168 | struct dm_dev *dm_snap_origin(struct dm_snapshot *s) |
| 169 | { |
| 170 | return s->origin; |
| 171 | } |
| 172 | EXPORT_SYMBOL(dm_snap_origin); |
| 173 | |
Mike Snitzer | fc56f6f | 2009-12-10 23:52:12 +0000 | [diff] [blame] | 174 | struct dm_dev *dm_snap_cow(struct dm_snapshot *s) |
| 175 | { |
| 176 | return s->cow; |
| 177 | } |
| 178 | EXPORT_SYMBOL(dm_snap_cow); |
| 179 | |
Jonathan Brassow | ccc45ea | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 180 | static sector_t chunk_to_sector(struct dm_exception_store *store, |
| 181 | chunk_t chunk) |
| 182 | { |
| 183 | return chunk << store->chunk_shift; |
| 184 | } |
| 185 | |
| 186 | static int bdev_equal(struct block_device *lhs, struct block_device *rhs) |
| 187 | { |
| 188 | /* |
| 189 | * There is only ever one instance of a particular block |
| 190 | * device so we can compare pointers safely. |
| 191 | */ |
| 192 | return lhs == rhs; |
| 193 | } |
| 194 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 195 | struct dm_snap_pending_exception { |
Jon Brassow | 1d4989c | 2009-12-10 23:52:10 +0000 | [diff] [blame] | 196 | struct dm_exception e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
| 198 | /* |
| 199 | * Origin buffers waiting for this to complete are held |
| 200 | * in a bio list |
| 201 | */ |
| 202 | struct bio_list origin_bios; |
| 203 | struct bio_list snapshot_bios; |
| 204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | /* Pointer back to snapshot context */ |
| 206 | struct dm_snapshot *snap; |
| 207 | |
| 208 | /* |
| 209 | * 1 indicates the exception has already been sent to |
| 210 | * kcopyd. |
| 211 | */ |
| 212 | int started; |
Mikulas Patocka | a6e50b4 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 213 | |
Mikulas Patocka | 230c83a | 2013-11-29 18:13:37 -0500 | [diff] [blame] | 214 | /* There was copying error. */ |
| 215 | int copy_error; |
| 216 | |
| 217 | /* A sequence number, it is used for in-order completion. */ |
| 218 | sector_t exception_sequence; |
| 219 | |
David Jeffery | 3db2776 | 2018-08-07 16:56:00 -0400 | [diff] [blame] | 220 | struct rb_node out_of_order_node; |
Mikulas Patocka | 230c83a | 2013-11-29 18:13:37 -0500 | [diff] [blame] | 221 | |
Mikulas Patocka | a6e50b4 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 222 | /* |
| 223 | * For writing a complete chunk, bypassing the copy. |
| 224 | */ |
| 225 | struct bio *full_bio; |
| 226 | bio_end_io_t *full_bio_end_io; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | }; |
| 228 | |
| 229 | /* |
| 230 | * Hash table mapping origin volumes to lists of snapshots and |
| 231 | * a lock to protect it |
| 232 | */ |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 233 | static struct kmem_cache *exception_cache; |
| 234 | static struct kmem_cache *pending_cache; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 236 | struct dm_snap_tracked_chunk { |
| 237 | struct hlist_node node; |
| 238 | chunk_t chunk; |
| 239 | }; |
| 240 | |
Mikulas Patocka | ee18026 | 2012-12-21 20:23:41 +0000 | [diff] [blame] | 241 | static void init_tracked_chunk(struct bio *bio) |
| 242 | { |
| 243 | struct dm_snap_tracked_chunk *c = dm_per_bio_data(bio, sizeof(struct dm_snap_tracked_chunk)); |
| 244 | INIT_HLIST_NODE(&c->node); |
| 245 | } |
| 246 | |
| 247 | static bool is_bio_tracked(struct bio *bio) |
| 248 | { |
| 249 | struct dm_snap_tracked_chunk *c = dm_per_bio_data(bio, sizeof(struct dm_snap_tracked_chunk)); |
| 250 | return !hlist_unhashed(&c->node); |
| 251 | } |
| 252 | |
| 253 | static void track_chunk(struct dm_snapshot *s, struct bio *bio, chunk_t chunk) |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 254 | { |
Mikulas Patocka | 42bc954 | 2012-12-21 20:23:38 +0000 | [diff] [blame] | 255 | struct dm_snap_tracked_chunk *c = dm_per_bio_data(bio, sizeof(struct dm_snap_tracked_chunk)); |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 256 | |
| 257 | c->chunk = chunk; |
| 258 | |
Mikulas Patocka | 9aa0c0e | 2012-12-21 20:23:33 +0000 | [diff] [blame] | 259 | spin_lock_irq(&s->tracked_chunk_lock); |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 260 | hlist_add_head(&c->node, |
| 261 | &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)]); |
Mikulas Patocka | 9aa0c0e | 2012-12-21 20:23:33 +0000 | [diff] [blame] | 262 | spin_unlock_irq(&s->tracked_chunk_lock); |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 263 | } |
| 264 | |
Mikulas Patocka | ee18026 | 2012-12-21 20:23:41 +0000 | [diff] [blame] | 265 | static void stop_tracking_chunk(struct dm_snapshot *s, struct bio *bio) |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 266 | { |
Mikulas Patocka | ee18026 | 2012-12-21 20:23:41 +0000 | [diff] [blame] | 267 | struct dm_snap_tracked_chunk *c = dm_per_bio_data(bio, sizeof(struct dm_snap_tracked_chunk)); |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 268 | unsigned long flags; |
| 269 | |
| 270 | spin_lock_irqsave(&s->tracked_chunk_lock, flags); |
| 271 | hlist_del(&c->node); |
| 272 | spin_unlock_irqrestore(&s->tracked_chunk_lock, flags); |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 273 | } |
| 274 | |
Mikulas Patocka | a8d41b5 | 2008-07-21 12:00:34 +0100 | [diff] [blame] | 275 | static int __chunk_is_tracked(struct dm_snapshot *s, chunk_t chunk) |
| 276 | { |
| 277 | struct dm_snap_tracked_chunk *c; |
Mikulas Patocka | a8d41b5 | 2008-07-21 12:00:34 +0100 | [diff] [blame] | 278 | int found = 0; |
| 279 | |
| 280 | spin_lock_irq(&s->tracked_chunk_lock); |
| 281 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 282 | hlist_for_each_entry(c, |
Mikulas Patocka | a8d41b5 | 2008-07-21 12:00:34 +0100 | [diff] [blame] | 283 | &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)], node) { |
| 284 | if (c->chunk == chunk) { |
| 285 | found = 1; |
| 286 | break; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | spin_unlock_irq(&s->tracked_chunk_lock); |
| 291 | |
| 292 | return found; |
| 293 | } |
| 294 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | /* |
Mike Snitzer | 615d1eb | 2009-12-10 23:52:29 +0000 | [diff] [blame] | 296 | * This conflicting I/O is extremely improbable in the caller, |
| 297 | * so msleep(1) is sufficient and there is no need for a wait queue. |
| 298 | */ |
| 299 | static void __check_for_conflicting_io(struct dm_snapshot *s, chunk_t chunk) |
| 300 | { |
| 301 | while (__chunk_is_tracked(s, chunk)) |
| 302 | msleep(1); |
| 303 | } |
| 304 | |
| 305 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | * One of these per registered origin, held in the snapshot_origins hash |
| 307 | */ |
| 308 | struct origin { |
| 309 | /* The origin device */ |
| 310 | struct block_device *bdev; |
| 311 | |
| 312 | struct list_head hash_list; |
| 313 | |
| 314 | /* List of snapshots for this origin */ |
| 315 | struct list_head snapshots; |
| 316 | }; |
| 317 | |
| 318 | /* |
Mikulas Patocka | b735fed | 2015-02-26 11:40:35 -0500 | [diff] [blame] | 319 | * This structure is allocated for each origin target |
| 320 | */ |
| 321 | struct dm_origin { |
| 322 | struct dm_dev *dev; |
| 323 | struct dm_target *ti; |
| 324 | unsigned split_boundary; |
| 325 | struct list_head hash_list; |
| 326 | }; |
| 327 | |
| 328 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | * Size of the hash table for origin volumes. If we make this |
| 330 | * the size of the minors list then it should be nearly perfect |
| 331 | */ |
| 332 | #define ORIGIN_HASH_SIZE 256 |
| 333 | #define ORIGIN_MASK 0xFF |
| 334 | static struct list_head *_origins; |
Mikulas Patocka | b735fed | 2015-02-26 11:40:35 -0500 | [diff] [blame] | 335 | static struct list_head *_dm_origins; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | static struct rw_semaphore _origins_lock; |
| 337 | |
Mikulas Patocka | 73dfd07 | 2009-12-10 23:52:34 +0000 | [diff] [blame] | 338 | static DECLARE_WAIT_QUEUE_HEAD(_pending_exceptions_done); |
| 339 | static DEFINE_SPINLOCK(_pending_exceptions_done_spinlock); |
| 340 | static uint64_t _pending_exceptions_done_count; |
| 341 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | static int init_origin_hash(void) |
| 343 | { |
| 344 | int i; |
| 345 | |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 346 | _origins = kmalloc_array(ORIGIN_HASH_SIZE, sizeof(struct list_head), |
| 347 | GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | if (!_origins) { |
Mikulas Patocka | b735fed | 2015-02-26 11:40:35 -0500 | [diff] [blame] | 349 | DMERR("unable to allocate memory for _origins"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | return -ENOMEM; |
| 351 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | for (i = 0; i < ORIGIN_HASH_SIZE; i++) |
| 353 | INIT_LIST_HEAD(_origins + i); |
Mikulas Patocka | b735fed | 2015-02-26 11:40:35 -0500 | [diff] [blame] | 354 | |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 355 | _dm_origins = kmalloc_array(ORIGIN_HASH_SIZE, |
| 356 | sizeof(struct list_head), |
| 357 | GFP_KERNEL); |
Mikulas Patocka | b735fed | 2015-02-26 11:40:35 -0500 | [diff] [blame] | 358 | if (!_dm_origins) { |
| 359 | DMERR("unable to allocate memory for _dm_origins"); |
| 360 | kfree(_origins); |
| 361 | return -ENOMEM; |
| 362 | } |
| 363 | for (i = 0; i < ORIGIN_HASH_SIZE; i++) |
| 364 | INIT_LIST_HEAD(_dm_origins + i); |
| 365 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | init_rwsem(&_origins_lock); |
| 367 | |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | static void exit_origin_hash(void) |
| 372 | { |
| 373 | kfree(_origins); |
Mikulas Patocka | b735fed | 2015-02-26 11:40:35 -0500 | [diff] [blame] | 374 | kfree(_dm_origins); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 377 | static unsigned origin_hash(struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | { |
| 379 | return bdev->bd_dev & ORIGIN_MASK; |
| 380 | } |
| 381 | |
| 382 | static struct origin *__lookup_origin(struct block_device *origin) |
| 383 | { |
| 384 | struct list_head *ol; |
| 385 | struct origin *o; |
| 386 | |
| 387 | ol = &_origins[origin_hash(origin)]; |
| 388 | list_for_each_entry (o, ol, hash_list) |
| 389 | if (bdev_equal(o->bdev, origin)) |
| 390 | return o; |
| 391 | |
| 392 | return NULL; |
| 393 | } |
| 394 | |
| 395 | static void __insert_origin(struct origin *o) |
| 396 | { |
| 397 | struct list_head *sl = &_origins[origin_hash(o->bdev)]; |
| 398 | list_add_tail(&o->hash_list, sl); |
| 399 | } |
| 400 | |
Mikulas Patocka | b735fed | 2015-02-26 11:40:35 -0500 | [diff] [blame] | 401 | static struct dm_origin *__lookup_dm_origin(struct block_device *origin) |
| 402 | { |
| 403 | struct list_head *ol; |
| 404 | struct dm_origin *o; |
| 405 | |
| 406 | ol = &_dm_origins[origin_hash(origin)]; |
| 407 | list_for_each_entry (o, ol, hash_list) |
| 408 | if (bdev_equal(o->dev->bdev, origin)) |
| 409 | return o; |
| 410 | |
| 411 | return NULL; |
| 412 | } |
| 413 | |
| 414 | static void __insert_dm_origin(struct dm_origin *o) |
| 415 | { |
| 416 | struct list_head *sl = &_dm_origins[origin_hash(o->dev->bdev)]; |
| 417 | list_add_tail(&o->hash_list, sl); |
| 418 | } |
| 419 | |
| 420 | static void __remove_dm_origin(struct dm_origin *o) |
| 421 | { |
| 422 | list_del(&o->hash_list); |
| 423 | } |
| 424 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | /* |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 426 | * _origins_lock must be held when calling this function. |
| 427 | * Returns number of snapshots registered using the supplied cow device, plus: |
| 428 | * snap_src - a snapshot suitable for use as a source of exception handover |
| 429 | * snap_dest - a snapshot capable of receiving exception handover. |
Mikulas Patocka | 9d3b15c | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 430 | * snap_merge - an existing snapshot-merge target linked to the same origin. |
| 431 | * There can be at most one snapshot-merge target. The parameter is optional. |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 432 | * |
Mikulas Patocka | 9d3b15c | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 433 | * Possible return values and states of snap_src and snap_dest. |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 434 | * 0: NULL, NULL - first new snapshot |
| 435 | * 1: snap_src, NULL - normal snapshot |
| 436 | * 2: snap_src, snap_dest - waiting for handover |
| 437 | * 2: snap_src, NULL - handed over, waiting for old to be deleted |
| 438 | * 1: NULL, snap_dest - source got destroyed without handover |
| 439 | */ |
| 440 | static int __find_snapshots_sharing_cow(struct dm_snapshot *snap, |
| 441 | struct dm_snapshot **snap_src, |
Mikulas Patocka | 9d3b15c | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 442 | struct dm_snapshot **snap_dest, |
| 443 | struct dm_snapshot **snap_merge) |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 444 | { |
| 445 | struct dm_snapshot *s; |
| 446 | struct origin *o; |
| 447 | int count = 0; |
| 448 | int active; |
| 449 | |
| 450 | o = __lookup_origin(snap->origin->bdev); |
| 451 | if (!o) |
| 452 | goto out; |
| 453 | |
| 454 | list_for_each_entry(s, &o->snapshots, list) { |
Mikulas Patocka | 9d3b15c | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 455 | if (dm_target_is_snapshot_merge(s->ti) && snap_merge) |
| 456 | *snap_merge = s; |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 457 | if (!bdev_equal(s->cow->bdev, snap->cow->bdev)) |
| 458 | continue; |
| 459 | |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 460 | down_read(&s->lock); |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 461 | active = s->active; |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 462 | up_read(&s->lock); |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 463 | |
| 464 | if (active) { |
| 465 | if (snap_src) |
| 466 | *snap_src = s; |
| 467 | } else if (snap_dest) |
| 468 | *snap_dest = s; |
| 469 | |
| 470 | count++; |
| 471 | } |
| 472 | |
| 473 | out: |
| 474 | return count; |
| 475 | } |
| 476 | |
| 477 | /* |
| 478 | * On success, returns 1 if this snapshot is a handover destination, |
| 479 | * otherwise returns 0. |
| 480 | */ |
| 481 | static int __validate_exception_handover(struct dm_snapshot *snap) |
| 482 | { |
| 483 | struct dm_snapshot *snap_src = NULL, *snap_dest = NULL; |
Mikulas Patocka | 9d3b15c | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 484 | struct dm_snapshot *snap_merge = NULL; |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 485 | |
| 486 | /* Does snapshot need exceptions handed over to it? */ |
Mikulas Patocka | 9d3b15c | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 487 | if ((__find_snapshots_sharing_cow(snap, &snap_src, &snap_dest, |
| 488 | &snap_merge) == 2) || |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 489 | snap_dest) { |
| 490 | snap->ti->error = "Snapshot cow pairing for exception " |
| 491 | "table handover failed"; |
| 492 | return -EINVAL; |
| 493 | } |
| 494 | |
| 495 | /* |
| 496 | * If no snap_src was found, snap cannot become a handover |
| 497 | * destination. |
| 498 | */ |
| 499 | if (!snap_src) |
| 500 | return 0; |
| 501 | |
Mikulas Patocka | 9d3b15c | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 502 | /* |
| 503 | * Non-snapshot-merge handover? |
| 504 | */ |
| 505 | if (!dm_target_is_snapshot_merge(snap->ti)) |
| 506 | return 1; |
| 507 | |
| 508 | /* |
| 509 | * Do not allow more than one merging snapshot. |
| 510 | */ |
| 511 | if (snap_merge) { |
| 512 | snap->ti->error = "A snapshot is already merging."; |
| 513 | return -EINVAL; |
| 514 | } |
| 515 | |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 516 | if (!snap_src->store->type->prepare_merge || |
| 517 | !snap_src->store->type->commit_merge) { |
| 518 | snap->ti->error = "Snapshot exception store does not " |
| 519 | "support snapshot-merge."; |
| 520 | return -EINVAL; |
| 521 | } |
| 522 | |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 523 | return 1; |
| 524 | } |
| 525 | |
| 526 | static void __insert_snapshot(struct origin *o, struct dm_snapshot *s) |
| 527 | { |
| 528 | struct dm_snapshot *l; |
| 529 | |
| 530 | /* Sort the list according to chunk size, largest-first smallest-last */ |
| 531 | list_for_each_entry(l, &o->snapshots, list) |
| 532 | if (l->store->chunk_size < s->store->chunk_size) |
| 533 | break; |
| 534 | list_add_tail(&s->list, &l->list); |
| 535 | } |
| 536 | |
| 537 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | * Make a note of the snapshot and its origin so we can look it |
| 539 | * up when the origin has a write on it. |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 540 | * |
| 541 | * Also validate snapshot exception store handovers. |
| 542 | * On success, returns 1 if this registration is a handover destination, |
| 543 | * otherwise returns 0. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | */ |
| 545 | static int register_snapshot(struct dm_snapshot *snap) |
| 546 | { |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 547 | struct origin *o, *new_o = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | struct block_device *bdev = snap->origin->bdev; |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 549 | int r = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | |
Mikulas Patocka | 60c856c8 | 2008-10-30 13:33:12 +0000 | [diff] [blame] | 551 | new_o = kmalloc(sizeof(*new_o), GFP_KERNEL); |
| 552 | if (!new_o) |
| 553 | return -ENOMEM; |
| 554 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | down_write(&_origins_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 557 | r = __validate_exception_handover(snap); |
| 558 | if (r < 0) { |
| 559 | kfree(new_o); |
| 560 | goto out; |
| 561 | } |
| 562 | |
| 563 | o = __lookup_origin(bdev); |
Mikulas Patocka | 60c856c8 | 2008-10-30 13:33:12 +0000 | [diff] [blame] | 564 | if (o) |
| 565 | kfree(new_o); |
| 566 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | /* New origin */ |
Mikulas Patocka | 60c856c8 | 2008-10-30 13:33:12 +0000 | [diff] [blame] | 568 | o = new_o; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | |
| 570 | /* Initialise the struct */ |
| 571 | INIT_LIST_HEAD(&o->snapshots); |
| 572 | o->bdev = bdev; |
| 573 | |
| 574 | __insert_origin(o); |
| 575 | } |
| 576 | |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 577 | __insert_snapshot(o, snap); |
| 578 | |
| 579 | out: |
| 580 | up_write(&_origins_lock); |
| 581 | |
| 582 | return r; |
| 583 | } |
| 584 | |
| 585 | /* |
| 586 | * Move snapshot to correct place in list according to chunk size. |
| 587 | */ |
| 588 | static void reregister_snapshot(struct dm_snapshot *s) |
| 589 | { |
| 590 | struct block_device *bdev = s->origin->bdev; |
| 591 | |
| 592 | down_write(&_origins_lock); |
| 593 | |
| 594 | list_del(&s->list); |
| 595 | __insert_snapshot(__lookup_origin(bdev), s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | |
| 597 | up_write(&_origins_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | static void unregister_snapshot(struct dm_snapshot *s) |
| 601 | { |
| 602 | struct origin *o; |
| 603 | |
| 604 | down_write(&_origins_lock); |
| 605 | o = __lookup_origin(s->origin->bdev); |
| 606 | |
| 607 | list_del(&s->list); |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 608 | if (o && list_empty(&o->snapshots)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | list_del(&o->hash_list); |
| 610 | kfree(o); |
| 611 | } |
| 612 | |
| 613 | up_write(&_origins_lock); |
| 614 | } |
| 615 | |
| 616 | /* |
| 617 | * Implementation of the exception hash tables. |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 618 | * The lowest hash_shift bits of the chunk number are ignored, allowing |
| 619 | * some consecutive chunks to be grouped together. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | */ |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 621 | static int dm_exception_table_init(struct dm_exception_table *et, |
| 622 | uint32_t size, unsigned hash_shift) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | { |
| 624 | unsigned int i; |
| 625 | |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 626 | et->hash_shift = hash_shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | et->hash_mask = size - 1; |
| 628 | et->table = dm_vcalloc(size, sizeof(struct list_head)); |
| 629 | if (!et->table) |
| 630 | return -ENOMEM; |
| 631 | |
| 632 | for (i = 0; i < size; i++) |
| 633 | INIT_LIST_HEAD(et->table + i); |
| 634 | |
| 635 | return 0; |
| 636 | } |
| 637 | |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 638 | static void dm_exception_table_exit(struct dm_exception_table *et, |
| 639 | struct kmem_cache *mem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | { |
| 641 | struct list_head *slot; |
Jon Brassow | 1d4989c | 2009-12-10 23:52:10 +0000 | [diff] [blame] | 642 | struct dm_exception *ex, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | int i, size; |
| 644 | |
| 645 | size = et->hash_mask + 1; |
| 646 | for (i = 0; i < size; i++) { |
| 647 | slot = et->table + i; |
| 648 | |
| 649 | list_for_each_entry_safe (ex, next, slot, hash_list) |
| 650 | kmem_cache_free(mem, ex); |
| 651 | } |
| 652 | |
| 653 | vfree(et->table); |
| 654 | } |
| 655 | |
Jon Brassow | 191437a | 2009-12-10 23:52:10 +0000 | [diff] [blame] | 656 | static uint32_t exception_hash(struct dm_exception_table *et, chunk_t chunk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | { |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 658 | return (chunk >> et->hash_shift) & et->hash_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | } |
| 660 | |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 661 | static void dm_remove_exception(struct dm_exception *e) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | { |
| 663 | list_del(&e->hash_list); |
| 664 | } |
| 665 | |
| 666 | /* |
| 667 | * Return the exception data for a sector, or NULL if not |
| 668 | * remapped. |
| 669 | */ |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 670 | static struct dm_exception *dm_lookup_exception(struct dm_exception_table *et, |
| 671 | chunk_t chunk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | { |
| 673 | struct list_head *slot; |
Jon Brassow | 1d4989c | 2009-12-10 23:52:10 +0000 | [diff] [blame] | 674 | struct dm_exception *e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | |
| 676 | slot = &et->table[exception_hash(et, chunk)]; |
| 677 | list_for_each_entry (e, slot, hash_list) |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 678 | if (chunk >= e->old_chunk && |
| 679 | chunk <= e->old_chunk + dm_consecutive_chunk_count(e)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | return e; |
| 681 | |
| 682 | return NULL; |
| 683 | } |
| 684 | |
Mikulas Patocka | 119bc54 | 2014-01-13 19:13:36 -0500 | [diff] [blame] | 685 | static struct dm_exception *alloc_completed_exception(gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | { |
Jon Brassow | 1d4989c | 2009-12-10 23:52:10 +0000 | [diff] [blame] | 687 | struct dm_exception *e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | |
Mikulas Patocka | 119bc54 | 2014-01-13 19:13:36 -0500 | [diff] [blame] | 689 | e = kmem_cache_alloc(exception_cache, gfp); |
| 690 | if (!e && gfp == GFP_NOIO) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | e = kmem_cache_alloc(exception_cache, GFP_ATOMIC); |
| 692 | |
| 693 | return e; |
| 694 | } |
| 695 | |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 696 | static void free_completed_exception(struct dm_exception *e) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | { |
| 698 | kmem_cache_free(exception_cache, e); |
| 699 | } |
| 700 | |
Mikulas Patocka | 92e8681 | 2008-07-21 12:00:35 +0100 | [diff] [blame] | 701 | static struct dm_snap_pending_exception *alloc_pending_exception(struct dm_snapshot *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | { |
Kent Overstreet | 6f1c819 | 2018-05-20 18:25:53 -0400 | [diff] [blame] | 703 | struct dm_snap_pending_exception *pe = mempool_alloc(&s->pending_pool, |
Mikulas Patocka | 92e8681 | 2008-07-21 12:00:35 +0100 | [diff] [blame] | 704 | GFP_NOIO); |
| 705 | |
Mikulas Patocka | 879129d2 | 2008-10-30 13:33:16 +0000 | [diff] [blame] | 706 | atomic_inc(&s->pending_exceptions_count); |
Mikulas Patocka | 92e8681 | 2008-07-21 12:00:35 +0100 | [diff] [blame] | 707 | pe->snap = s; |
| 708 | |
| 709 | return pe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | } |
| 711 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 712 | static void free_pending_exception(struct dm_snap_pending_exception *pe) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | { |
Mikulas Patocka | 879129d2 | 2008-10-30 13:33:16 +0000 | [diff] [blame] | 714 | struct dm_snapshot *s = pe->snap; |
| 715 | |
Kent Overstreet | 6f1c819 | 2018-05-20 18:25:53 -0400 | [diff] [blame] | 716 | mempool_free(pe, &s->pending_pool); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 717 | smp_mb__before_atomic(); |
Mikulas Patocka | 879129d2 | 2008-10-30 13:33:16 +0000 | [diff] [blame] | 718 | atomic_dec(&s->pending_exceptions_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | } |
| 720 | |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 721 | static void dm_insert_exception(struct dm_exception_table *eh, |
| 722 | struct dm_exception *new_e) |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 723 | { |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 724 | struct list_head *l; |
Jon Brassow | 1d4989c | 2009-12-10 23:52:10 +0000 | [diff] [blame] | 725 | struct dm_exception *e = NULL; |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 726 | |
| 727 | l = &eh->table[exception_hash(eh, new_e->old_chunk)]; |
| 728 | |
| 729 | /* Add immediately if this table doesn't support consecutive chunks */ |
| 730 | if (!eh->hash_shift) |
| 731 | goto out; |
| 732 | |
| 733 | /* List is ordered by old_chunk */ |
| 734 | list_for_each_entry_reverse(e, l, hash_list) { |
| 735 | /* Insert after an existing chunk? */ |
| 736 | if (new_e->old_chunk == (e->old_chunk + |
| 737 | dm_consecutive_chunk_count(e) + 1) && |
| 738 | new_e->new_chunk == (dm_chunk_number(e->new_chunk) + |
| 739 | dm_consecutive_chunk_count(e) + 1)) { |
| 740 | dm_consecutive_chunk_count_inc(e); |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 741 | free_completed_exception(new_e); |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 742 | return; |
| 743 | } |
| 744 | |
| 745 | /* Insert before an existing chunk? */ |
| 746 | if (new_e->old_chunk == (e->old_chunk - 1) && |
| 747 | new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) { |
| 748 | dm_consecutive_chunk_count_inc(e); |
| 749 | e->old_chunk--; |
| 750 | e->new_chunk--; |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 751 | free_completed_exception(new_e); |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 752 | return; |
| 753 | } |
| 754 | |
| 755 | if (new_e->old_chunk > e->old_chunk) |
| 756 | break; |
| 757 | } |
| 758 | |
| 759 | out: |
| 760 | list_add(&new_e->hash_list, e ? &e->hash_list : l); |
| 761 | } |
| 762 | |
Jonathan Brassow | a159c1a | 2009-01-06 03:05:19 +0000 | [diff] [blame] | 763 | /* |
| 764 | * Callback used by the exception stores to load exceptions when |
| 765 | * initialising. |
| 766 | */ |
| 767 | static int dm_add_exception(void *context, chunk_t old, chunk_t new) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | { |
Jonathan Brassow | a159c1a | 2009-01-06 03:05:19 +0000 | [diff] [blame] | 769 | struct dm_snapshot *s = context; |
Jon Brassow | 1d4989c | 2009-12-10 23:52:10 +0000 | [diff] [blame] | 770 | struct dm_exception *e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | |
Mikulas Patocka | 119bc54 | 2014-01-13 19:13:36 -0500 | [diff] [blame] | 772 | e = alloc_completed_exception(GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | if (!e) |
| 774 | return -ENOMEM; |
| 775 | |
| 776 | e->old_chunk = old; |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 777 | |
| 778 | /* Consecutive_count is implicitly initialised to zero */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | e->new_chunk = new; |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 780 | |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 781 | dm_insert_exception(&s->complete, e); |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 782 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | return 0; |
| 784 | } |
| 785 | |
Mikulas Patocka | 7e201b3 | 2009-12-10 23:52:08 +0000 | [diff] [blame] | 786 | /* |
| 787 | * Return a minimum chunk size of all snapshots that have the specified origin. |
| 788 | * Return zero if the origin has no snapshots. |
| 789 | */ |
Mike Snitzer | 542f903 | 2012-07-27 15:08:00 +0100 | [diff] [blame] | 790 | static uint32_t __minimum_chunk_size(struct origin *o) |
Mikulas Patocka | 7e201b3 | 2009-12-10 23:52:08 +0000 | [diff] [blame] | 791 | { |
| 792 | struct dm_snapshot *snap; |
| 793 | unsigned chunk_size = 0; |
| 794 | |
| 795 | if (o) |
| 796 | list_for_each_entry(snap, &o->snapshots, list) |
| 797 | chunk_size = min_not_zero(chunk_size, |
| 798 | snap->store->chunk_size); |
| 799 | |
Mike Snitzer | 542f903 | 2012-07-27 15:08:00 +0100 | [diff] [blame] | 800 | return (uint32_t) chunk_size; |
Mikulas Patocka | 7e201b3 | 2009-12-10 23:52:08 +0000 | [diff] [blame] | 801 | } |
| 802 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | /* |
| 804 | * Hard coded magic. |
| 805 | */ |
| 806 | static int calc_max_buckets(void) |
| 807 | { |
| 808 | /* use a fixed size of 2MB */ |
| 809 | unsigned long mem = 2 * 1024 * 1024; |
| 810 | mem /= sizeof(struct list_head); |
| 811 | |
| 812 | return mem; |
| 813 | } |
| 814 | |
| 815 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | * Allocate room for a suitable hash table. |
| 817 | */ |
Jonathan Brassow | fee1998 | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 818 | static int init_hash_tables(struct dm_snapshot *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | { |
Mikulas Patocka | 60e356f | 2013-09-18 19:40:42 -0400 | [diff] [blame] | 820 | sector_t hash_size, cow_dev_size, max_buckets; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | |
| 822 | /* |
| 823 | * Calculate based on the size of the original volume or |
| 824 | * the COW volume... |
| 825 | */ |
Mike Snitzer | fc56f6f | 2009-12-10 23:52:12 +0000 | [diff] [blame] | 826 | cow_dev_size = get_dev_size(s->cow->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | max_buckets = calc_max_buckets(); |
| 828 | |
Mikulas Patocka | 60e356f | 2013-09-18 19:40:42 -0400 | [diff] [blame] | 829 | hash_size = cow_dev_size >> s->store->chunk_shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | hash_size = min(hash_size, max_buckets); |
| 831 | |
Mikulas Patocka | 8e87b9b | 2009-12-10 23:51:54 +0000 | [diff] [blame] | 832 | if (hash_size < 64) |
| 833 | hash_size = 64; |
Robert P. J. Day | 8defd83 | 2008-02-08 02:10:06 +0000 | [diff] [blame] | 834 | hash_size = rounddown_pow_of_two(hash_size); |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 835 | if (dm_exception_table_init(&s->complete, hash_size, |
| 836 | DM_CHUNK_CONSECUTIVE_BITS)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | return -ENOMEM; |
| 838 | |
| 839 | /* |
| 840 | * Allocate hash table for in-flight exceptions |
| 841 | * Make this smaller than the real hash table |
| 842 | */ |
| 843 | hash_size >>= 3; |
| 844 | if (hash_size < 64) |
| 845 | hash_size = 64; |
| 846 | |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 847 | if (dm_exception_table_init(&s->pending, hash_size, 0)) { |
| 848 | dm_exception_table_exit(&s->complete, exception_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | return -ENOMEM; |
| 850 | } |
| 851 | |
| 852 | return 0; |
| 853 | } |
| 854 | |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 855 | static void merge_shutdown(struct dm_snapshot *s) |
| 856 | { |
| 857 | clear_bit_unlock(RUNNING_MERGE, &s->state_bits); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 858 | smp_mb__after_atomic(); |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 859 | wake_up_bit(&s->state_bits, RUNNING_MERGE); |
| 860 | } |
| 861 | |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 862 | static struct bio *__release_queued_bios_after_merge(struct dm_snapshot *s) |
| 863 | { |
| 864 | s->first_merging_chunk = 0; |
| 865 | s->num_merging_chunks = 0; |
| 866 | |
| 867 | return bio_list_get(&s->bios_queued_during_merge); |
| 868 | } |
| 869 | |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 870 | /* |
| 871 | * Remove one chunk from the index of completed exceptions. |
| 872 | */ |
| 873 | static int __remove_single_exception_chunk(struct dm_snapshot *s, |
| 874 | chunk_t old_chunk) |
| 875 | { |
| 876 | struct dm_exception *e; |
| 877 | |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 878 | e = dm_lookup_exception(&s->complete, old_chunk); |
| 879 | if (!e) { |
| 880 | DMERR("Corruption detected: exception for block %llu is " |
| 881 | "on disk but not in memory", |
| 882 | (unsigned long long)old_chunk); |
| 883 | return -EINVAL; |
| 884 | } |
| 885 | |
| 886 | /* |
| 887 | * If this is the only chunk using this exception, remove exception. |
| 888 | */ |
| 889 | if (!dm_consecutive_chunk_count(e)) { |
| 890 | dm_remove_exception(e); |
| 891 | free_completed_exception(e); |
| 892 | return 0; |
| 893 | } |
| 894 | |
| 895 | /* |
| 896 | * The chunk may be either at the beginning or the end of a |
| 897 | * group of consecutive chunks - never in the middle. We are |
| 898 | * removing chunks in the opposite order to that in which they |
| 899 | * were added, so this should always be true. |
| 900 | * Decrement the consecutive chunk counter and adjust the |
| 901 | * starting point if necessary. |
| 902 | */ |
| 903 | if (old_chunk == e->old_chunk) { |
| 904 | e->old_chunk++; |
| 905 | e->new_chunk++; |
| 906 | } else if (old_chunk != e->old_chunk + |
| 907 | dm_consecutive_chunk_count(e)) { |
| 908 | DMERR("Attempt to merge block %llu from the " |
| 909 | "middle of a chunk range [%llu - %llu]", |
| 910 | (unsigned long long)old_chunk, |
| 911 | (unsigned long long)e->old_chunk, |
| 912 | (unsigned long long) |
| 913 | e->old_chunk + dm_consecutive_chunk_count(e)); |
| 914 | return -EINVAL; |
| 915 | } |
| 916 | |
| 917 | dm_consecutive_chunk_count_dec(e); |
| 918 | |
| 919 | return 0; |
| 920 | } |
| 921 | |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 922 | static void flush_bios(struct bio *bio); |
| 923 | |
| 924 | static int remove_single_exception_chunk(struct dm_snapshot *s) |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 925 | { |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 926 | struct bio *b = NULL; |
| 927 | int r; |
| 928 | chunk_t old_chunk = s->first_merging_chunk + s->num_merging_chunks - 1; |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 929 | |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 930 | down_write(&s->lock); |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 931 | |
| 932 | /* |
| 933 | * Process chunks (and associated exceptions) in reverse order |
| 934 | * so that dm_consecutive_chunk_count_dec() accounting works. |
| 935 | */ |
| 936 | do { |
| 937 | r = __remove_single_exception_chunk(s, old_chunk); |
| 938 | if (r) |
| 939 | goto out; |
| 940 | } while (old_chunk-- > s->first_merging_chunk); |
| 941 | |
| 942 | b = __release_queued_bios_after_merge(s); |
| 943 | |
| 944 | out: |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 945 | up_write(&s->lock); |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 946 | if (b) |
| 947 | flush_bios(b); |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 948 | |
| 949 | return r; |
| 950 | } |
| 951 | |
Mikulas Patocka | 73dfd07 | 2009-12-10 23:52:34 +0000 | [diff] [blame] | 952 | static int origin_write_extent(struct dm_snapshot *merging_snap, |
| 953 | sector_t sector, unsigned chunk_size); |
| 954 | |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 955 | static void merge_callback(int read_err, unsigned long write_err, |
| 956 | void *context); |
| 957 | |
Mikulas Patocka | 73dfd07 | 2009-12-10 23:52:34 +0000 | [diff] [blame] | 958 | static uint64_t read_pending_exceptions_done_count(void) |
| 959 | { |
| 960 | uint64_t pending_exceptions_done; |
| 961 | |
| 962 | spin_lock(&_pending_exceptions_done_spinlock); |
| 963 | pending_exceptions_done = _pending_exceptions_done_count; |
| 964 | spin_unlock(&_pending_exceptions_done_spinlock); |
| 965 | |
| 966 | return pending_exceptions_done; |
| 967 | } |
| 968 | |
| 969 | static void increment_pending_exceptions_done_count(void) |
| 970 | { |
| 971 | spin_lock(&_pending_exceptions_done_spinlock); |
| 972 | _pending_exceptions_done_count++; |
| 973 | spin_unlock(&_pending_exceptions_done_spinlock); |
| 974 | |
| 975 | wake_up_all(&_pending_exceptions_done); |
| 976 | } |
| 977 | |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 978 | static void snapshot_merge_next_chunks(struct dm_snapshot *s) |
| 979 | { |
Mike Snitzer | 8a2d528 | 2009-12-10 23:52:34 +0000 | [diff] [blame] | 980 | int i, linear_chunks; |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 981 | chunk_t old_chunk, new_chunk; |
| 982 | struct dm_io_region src, dest; |
Mike Snitzer | 8a2d528 | 2009-12-10 23:52:34 +0000 | [diff] [blame] | 983 | sector_t io_size; |
Mikulas Patocka | 73dfd07 | 2009-12-10 23:52:34 +0000 | [diff] [blame] | 984 | uint64_t previous_count; |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 985 | |
| 986 | BUG_ON(!test_bit(RUNNING_MERGE, &s->state_bits)); |
| 987 | if (unlikely(test_bit(SHUTDOWN_MERGE, &s->state_bits))) |
| 988 | goto shut; |
| 989 | |
| 990 | /* |
| 991 | * valid flag never changes during merge, so no lock required. |
| 992 | */ |
| 993 | if (!s->valid) { |
| 994 | DMERR("Snapshot is invalid: can't merge"); |
| 995 | goto shut; |
| 996 | } |
| 997 | |
Mike Snitzer | 8a2d528 | 2009-12-10 23:52:34 +0000 | [diff] [blame] | 998 | linear_chunks = s->store->type->prepare_merge(s->store, &old_chunk, |
| 999 | &new_chunk); |
| 1000 | if (linear_chunks <= 0) { |
Mike Snitzer | d8ddb1c | 2009-12-10 23:52:35 +0000 | [diff] [blame] | 1001 | if (linear_chunks < 0) { |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 1002 | DMERR("Read error in exception store: " |
| 1003 | "shutting down merge"); |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 1004 | down_write(&s->lock); |
Mike Snitzer | d8ddb1c | 2009-12-10 23:52:35 +0000 | [diff] [blame] | 1005 | s->merge_failed = 1; |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 1006 | up_write(&s->lock); |
Mike Snitzer | d8ddb1c | 2009-12-10 23:52:35 +0000 | [diff] [blame] | 1007 | } |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 1008 | goto shut; |
| 1009 | } |
| 1010 | |
Mike Snitzer | 8a2d528 | 2009-12-10 23:52:34 +0000 | [diff] [blame] | 1011 | /* Adjust old_chunk and new_chunk to reflect start of linear region */ |
| 1012 | old_chunk = old_chunk + 1 - linear_chunks; |
| 1013 | new_chunk = new_chunk + 1 - linear_chunks; |
| 1014 | |
| 1015 | /* |
| 1016 | * Use one (potentially large) I/O to copy all 'linear_chunks' |
| 1017 | * from the exception store to the origin |
| 1018 | */ |
| 1019 | io_size = linear_chunks * s->store->chunk_size; |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 1020 | |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 1021 | dest.bdev = s->origin->bdev; |
| 1022 | dest.sector = chunk_to_sector(s->store, old_chunk); |
Mike Snitzer | 8a2d528 | 2009-12-10 23:52:34 +0000 | [diff] [blame] | 1023 | dest.count = min(io_size, get_dev_size(dest.bdev) - dest.sector); |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 1024 | |
| 1025 | src.bdev = s->cow->bdev; |
| 1026 | src.sector = chunk_to_sector(s->store, new_chunk); |
| 1027 | src.count = dest.count; |
| 1028 | |
Mikulas Patocka | 73dfd07 | 2009-12-10 23:52:34 +0000 | [diff] [blame] | 1029 | /* |
| 1030 | * Reallocate any exceptions needed in other snapshots then |
| 1031 | * wait for the pending exceptions to complete. |
| 1032 | * Each time any pending exception (globally on the system) |
| 1033 | * completes we are woken and repeat the process to find out |
| 1034 | * if we can proceed. While this may not seem a particularly |
| 1035 | * efficient algorithm, it is not expected to have any |
| 1036 | * significant impact on performance. |
| 1037 | */ |
| 1038 | previous_count = read_pending_exceptions_done_count(); |
Mike Snitzer | 8a2d528 | 2009-12-10 23:52:34 +0000 | [diff] [blame] | 1039 | while (origin_write_extent(s, dest.sector, io_size)) { |
Mikulas Patocka | 73dfd07 | 2009-12-10 23:52:34 +0000 | [diff] [blame] | 1040 | wait_event(_pending_exceptions_done, |
| 1041 | (read_pending_exceptions_done_count() != |
| 1042 | previous_count)); |
| 1043 | /* Retry after the wait, until all exceptions are done. */ |
| 1044 | previous_count = read_pending_exceptions_done_count(); |
| 1045 | } |
| 1046 | |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 1047 | down_write(&s->lock); |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 1048 | s->first_merging_chunk = old_chunk; |
Mike Snitzer | 8a2d528 | 2009-12-10 23:52:34 +0000 | [diff] [blame] | 1049 | s->num_merging_chunks = linear_chunks; |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 1050 | up_write(&s->lock); |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 1051 | |
Mike Snitzer | 8a2d528 | 2009-12-10 23:52:34 +0000 | [diff] [blame] | 1052 | /* Wait until writes to all 'linear_chunks' drain */ |
| 1053 | for (i = 0; i < linear_chunks; i++) |
| 1054 | __check_for_conflicting_io(s, old_chunk + i); |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 1055 | |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 1056 | dm_kcopyd_copy(s->kcopyd_client, &src, 1, &dest, 0, merge_callback, s); |
| 1057 | return; |
| 1058 | |
| 1059 | shut: |
| 1060 | merge_shutdown(s); |
| 1061 | } |
| 1062 | |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 1063 | static void error_bios(struct bio *bio); |
| 1064 | |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 1065 | static void merge_callback(int read_err, unsigned long write_err, void *context) |
| 1066 | { |
| 1067 | struct dm_snapshot *s = context; |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 1068 | struct bio *b = NULL; |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 1069 | |
| 1070 | if (read_err || write_err) { |
| 1071 | if (read_err) |
| 1072 | DMERR("Read error: shutting down merge."); |
| 1073 | else |
| 1074 | DMERR("Write error: shutting down merge."); |
| 1075 | goto shut; |
| 1076 | } |
| 1077 | |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 1078 | if (s->store->type->commit_merge(s->store, |
| 1079 | s->num_merging_chunks) < 0) { |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 1080 | DMERR("Write error in exception store: shutting down merge"); |
| 1081 | goto shut; |
| 1082 | } |
| 1083 | |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 1084 | if (remove_single_exception_chunk(s) < 0) |
| 1085 | goto shut; |
| 1086 | |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 1087 | snapshot_merge_next_chunks(s); |
| 1088 | |
| 1089 | return; |
| 1090 | |
| 1091 | shut: |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 1092 | down_write(&s->lock); |
Mike Snitzer | d8ddb1c | 2009-12-10 23:52:35 +0000 | [diff] [blame] | 1093 | s->merge_failed = 1; |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 1094 | b = __release_queued_bios_after_merge(s); |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 1095 | up_write(&s->lock); |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 1096 | error_bios(b); |
| 1097 | |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 1098 | merge_shutdown(s); |
| 1099 | } |
| 1100 | |
| 1101 | static void start_merge(struct dm_snapshot *s) |
| 1102 | { |
| 1103 | if (!test_and_set_bit(RUNNING_MERGE, &s->state_bits)) |
| 1104 | snapshot_merge_next_chunks(s); |
| 1105 | } |
| 1106 | |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 1107 | /* |
| 1108 | * Stop the merging process and wait until it finishes. |
| 1109 | */ |
| 1110 | static void stop_merge(struct dm_snapshot *s) |
| 1111 | { |
| 1112 | set_bit(SHUTDOWN_MERGE, &s->state_bits); |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 1113 | wait_on_bit(&s->state_bits, RUNNING_MERGE, TASK_UNINTERRUPTIBLE); |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 1114 | clear_bit(SHUTDOWN_MERGE, &s->state_bits); |
| 1115 | } |
| 1116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | /* |
Mike Snitzer | b0d3cc0 | 2015-10-08 18:05:41 -0400 | [diff] [blame] | 1118 | * Construct a snapshot mapping: <origin_dev> <COW-dev> <p|po|n> <chunk-size> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | */ |
| 1120 | static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv) |
| 1121 | { |
| 1122 | struct dm_snapshot *s; |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 1123 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | int r = -EINVAL; |
Mike Snitzer | fc56f6f | 2009-12-10 23:52:12 +0000 | [diff] [blame] | 1125 | char *origin_path, *cow_path; |
DingXiang | 4df2bf4 | 2016-02-02 12:29:18 +0800 | [diff] [blame] | 1126 | dev_t origin_dev, cow_dev; |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1127 | unsigned args_used, num_flush_bios = 1; |
Mike Snitzer | 10b8106 | 2009-12-10 23:52:31 +0000 | [diff] [blame] | 1128 | fmode_t origin_mode = FMODE_READ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | |
Mark McLoughlin | 4c7e3bf | 2006-10-03 01:15:25 -0700 | [diff] [blame] | 1130 | if (argc != 4) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 1131 | ti->error = "requires exactly 4 arguments"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | r = -EINVAL; |
Mike Snitzer | fc56f6f | 2009-12-10 23:52:12 +0000 | [diff] [blame] | 1133 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | } |
| 1135 | |
Mike Snitzer | 10b8106 | 2009-12-10 23:52:31 +0000 | [diff] [blame] | 1136 | if (dm_target_is_snapshot_merge(ti)) { |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1137 | num_flush_bios = 2; |
Mike Snitzer | 10b8106 | 2009-12-10 23:52:31 +0000 | [diff] [blame] | 1138 | origin_mode = FMODE_WRITE; |
| 1139 | } |
| 1140 | |
Kent Overstreet | d377535 | 2018-06-05 05:26:33 -0400 | [diff] [blame] | 1141 | s = kzalloc(sizeof(*s), GFP_KERNEL); |
Jonathan Brassow | fee1998 | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 1142 | if (!s) { |
Jonathan Brassow | a2d2b03 | 2011-08-02 12:32:03 +0100 | [diff] [blame] | 1143 | ti->error = "Cannot allocate private snapshot structure"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | r = -ENOMEM; |
Mike Snitzer | fc56f6f | 2009-12-10 23:52:12 +0000 | [diff] [blame] | 1145 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | } |
| 1147 | |
Mikulas Patocka | c241104 | 2010-08-12 04:13:51 +0100 | [diff] [blame] | 1148 | origin_path = argv[0]; |
| 1149 | argv++; |
| 1150 | argc--; |
| 1151 | |
| 1152 | r = dm_get_device(ti, origin_path, origin_mode, &s->origin); |
| 1153 | if (r) { |
| 1154 | ti->error = "Cannot get origin device"; |
| 1155 | goto bad_origin; |
| 1156 | } |
DingXiang | 4df2bf4 | 2016-02-02 12:29:18 +0800 | [diff] [blame] | 1157 | origin_dev = s->origin->bdev->bd_dev; |
Mikulas Patocka | c241104 | 2010-08-12 04:13:51 +0100 | [diff] [blame] | 1158 | |
Mike Snitzer | fc56f6f | 2009-12-10 23:52:12 +0000 | [diff] [blame] | 1159 | cow_path = argv[0]; |
| 1160 | argv++; |
| 1161 | argc--; |
| 1162 | |
DingXiang | 4df2bf4 | 2016-02-02 12:29:18 +0800 | [diff] [blame] | 1163 | cow_dev = dm_get_dev_t(cow_path); |
| 1164 | if (cow_dev && cow_dev == origin_dev) { |
| 1165 | ti->error = "COW device cannot be the same as origin device"; |
| 1166 | r = -EINVAL; |
| 1167 | goto bad_cow; |
| 1168 | } |
| 1169 | |
Milan Broz | 024d37e | 2011-03-24 13:52:14 +0000 | [diff] [blame] | 1170 | r = dm_get_device(ti, cow_path, dm_table_get_mode(ti->table), &s->cow); |
Mike Snitzer | fc56f6f | 2009-12-10 23:52:12 +0000 | [diff] [blame] | 1171 | if (r) { |
| 1172 | ti->error = "Cannot get COW device"; |
| 1173 | goto bad_cow; |
| 1174 | } |
| 1175 | |
| 1176 | r = dm_exception_store_create(ti, argc, argv, s, &args_used, &s->store); |
| 1177 | if (r) { |
| 1178 | ti->error = "Couldn't create exception store"; |
| 1179 | r = -EINVAL; |
| 1180 | goto bad_store; |
| 1181 | } |
| 1182 | |
| 1183 | argv += args_used; |
| 1184 | argc -= args_used; |
| 1185 | |
Mike Snitzer | fc56f6f | 2009-12-10 23:52:12 +0000 | [diff] [blame] | 1186 | s->ti = ti; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | s->valid = 1; |
Mikulas Patocka | 76c44f6 | 2015-06-21 16:31:33 -0400 | [diff] [blame] | 1188 | s->snapshot_overflowed = 0; |
Alasdair G Kergon | aa14ede | 2006-02-01 03:04:50 -0800 | [diff] [blame] | 1189 | s->active = 0; |
Mikulas Patocka | 879129d2 | 2008-10-30 13:33:16 +0000 | [diff] [blame] | 1190 | atomic_set(&s->pending_exceptions_count, 0); |
Mikulas Patocka | 230c83a | 2013-11-29 18:13:37 -0500 | [diff] [blame] | 1191 | s->exception_start_sequence = 0; |
| 1192 | s->exception_complete_sequence = 0; |
David Jeffery | 3db2776 | 2018-08-07 16:56:00 -0400 | [diff] [blame] | 1193 | s->out_of_order_tree = RB_ROOT; |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 1194 | init_rwsem(&s->lock); |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1195 | INIT_LIST_HEAD(&s->list); |
Alasdair G Kergon | ca3a931 | 2006-10-03 01:15:30 -0700 | [diff] [blame] | 1196 | spin_lock_init(&s->pe_lock); |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 1197 | s->state_bits = 0; |
Mike Snitzer | d8ddb1c | 2009-12-10 23:52:35 +0000 | [diff] [blame] | 1198 | s->merge_failed = 0; |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 1199 | s->first_merging_chunk = 0; |
| 1200 | s->num_merging_chunks = 0; |
| 1201 | bio_list_init(&s->bios_queued_during_merge); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | |
| 1203 | /* Allocate hash table for COW data */ |
Jonathan Brassow | fee1998 | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 1204 | if (init_hash_tables(s)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | ti->error = "Unable to allocate hash table space"; |
| 1206 | r = -ENOMEM; |
Jonathan Brassow | fee1998 | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 1207 | goto bad_hash_tables; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | } |
| 1209 | |
Nikos Tsironis | 721b1d9 | 2018-10-31 17:53:08 -0400 | [diff] [blame] | 1210 | sema_init(&s->cow_count, (cow_threshold > 0) ? cow_threshold : INT_MAX); |
| 1211 | |
Mikulas Patocka | df5d2e9 | 2013-03-01 22:45:49 +0000 | [diff] [blame] | 1212 | s->kcopyd_client = dm_kcopyd_client_create(&dm_kcopyd_throttle); |
Mikulas Patocka | fa34ce7 | 2011-05-29 13:03:13 +0100 | [diff] [blame] | 1213 | if (IS_ERR(s->kcopyd_client)) { |
| 1214 | r = PTR_ERR(s->kcopyd_client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | ti->error = "Could not create kcopyd client"; |
Jonathan Brassow | fee1998 | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 1216 | goto bad_kcopyd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | } |
| 1218 | |
Kent Overstreet | 6f1c819 | 2018-05-20 18:25:53 -0400 | [diff] [blame] | 1219 | r = mempool_init_slab_pool(&s->pending_pool, MIN_IOS, pending_cache); |
| 1220 | if (r) { |
Mikulas Patocka | 92e8681 | 2008-07-21 12:00:35 +0100 | [diff] [blame] | 1221 | ti->error = "Could not allocate mempool for pending exceptions"; |
Jonathan Brassow | fee1998 | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 1222 | goto bad_pending_pool; |
Mikulas Patocka | 92e8681 | 2008-07-21 12:00:35 +0100 | [diff] [blame] | 1223 | } |
| 1224 | |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 1225 | for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++) |
| 1226 | INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]); |
| 1227 | |
| 1228 | spin_lock_init(&s->tracked_chunk_lock); |
| 1229 | |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1230 | ti->private = s; |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1231 | ti->num_flush_bios = num_flush_bios; |
Mike Snitzer | 30187e1 | 2016-01-31 13:28:26 -0500 | [diff] [blame] | 1232 | ti->per_io_data_size = sizeof(struct dm_snap_tracked_chunk); |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1233 | |
| 1234 | /* Add snapshot to the list of snapshots for this origin */ |
| 1235 | /* Exceptions aren't triggered till snapshot_resume() is called */ |
| 1236 | r = register_snapshot(s); |
| 1237 | if (r == -ENOMEM) { |
| 1238 | ti->error = "Snapshot origin struct allocation failed"; |
| 1239 | goto bad_load_and_register; |
| 1240 | } else if (r < 0) { |
| 1241 | /* invalid handover, register_snapshot has set ti->error */ |
| 1242 | goto bad_load_and_register; |
| 1243 | } |
| 1244 | |
| 1245 | /* |
| 1246 | * Metadata must only be loaded into one table at once, so skip this |
| 1247 | * if metadata will be handed over during resume. |
| 1248 | * Chunk size will be set during the handover - set it to zero to |
| 1249 | * ensure it's ignored. |
| 1250 | */ |
| 1251 | if (r > 0) { |
| 1252 | s->store->chunk_size = 0; |
| 1253 | return 0; |
| 1254 | } |
| 1255 | |
Jonathan Brassow | 493df71 | 2009-04-02 19:55:31 +0100 | [diff] [blame] | 1256 | r = s->store->type->read_metadata(s->store, dm_add_exception, |
| 1257 | (void *)s); |
Milan Broz | 0764147 | 2007-07-12 17:28:13 +0100 | [diff] [blame] | 1258 | if (r < 0) { |
Mark McLoughlin | f9cea4f | 2006-10-03 01:15:25 -0700 | [diff] [blame] | 1259 | ti->error = "Failed to read snapshot metadata"; |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1260 | goto bad_read_metadata; |
Milan Broz | 0764147 | 2007-07-12 17:28:13 +0100 | [diff] [blame] | 1261 | } else if (r > 0) { |
| 1262 | s->valid = 0; |
| 1263 | DMWARN("Snapshot is marked invalid."); |
Mark McLoughlin | f9cea4f | 2006-10-03 01:15:25 -0700 | [diff] [blame] | 1264 | } |
Alasdair G Kergon | aa14ede | 2006-02-01 03:04:50 -0800 | [diff] [blame] | 1265 | |
Mikulas Patocka | 3f2412d | 2009-10-16 23:18:16 +0100 | [diff] [blame] | 1266 | if (!s->store->chunk_size) { |
| 1267 | ti->error = "Chunk size not set"; |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1268 | goto bad_read_metadata; |
Mikulas Patocka | 3f2412d | 2009-10-16 23:18:16 +0100 | [diff] [blame] | 1269 | } |
Mike Snitzer | 542f903 | 2012-07-27 15:08:00 +0100 | [diff] [blame] | 1270 | |
| 1271 | r = dm_set_target_max_io_len(ti, s->store->chunk_size); |
| 1272 | if (r) |
| 1273 | goto bad_read_metadata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | |
| 1275 | return 0; |
| 1276 | |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1277 | bad_read_metadata: |
| 1278 | unregister_snapshot(s); |
| 1279 | |
Jonathan Brassow | fee1998 | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 1280 | bad_load_and_register: |
Kent Overstreet | 6f1c819 | 2018-05-20 18:25:53 -0400 | [diff] [blame] | 1281 | mempool_exit(&s->pending_pool); |
Mikulas Patocka | 92e8681 | 2008-07-21 12:00:35 +0100 | [diff] [blame] | 1282 | |
Jonathan Brassow | fee1998 | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 1283 | bad_pending_pool: |
Heinz Mauelshagen | eb69aca | 2008-04-24 21:43:19 +0100 | [diff] [blame] | 1284 | dm_kcopyd_client_destroy(s->kcopyd_client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | |
Jonathan Brassow | fee1998 | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 1286 | bad_kcopyd: |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 1287 | dm_exception_table_exit(&s->pending, pending_cache); |
| 1288 | dm_exception_table_exit(&s->complete, exception_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | |
Jonathan Brassow | fee1998 | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 1290 | bad_hash_tables: |
Mike Snitzer | fc56f6f | 2009-12-10 23:52:12 +0000 | [diff] [blame] | 1291 | dm_exception_store_destroy(s->store); |
| 1292 | |
| 1293 | bad_store: |
| 1294 | dm_put_device(ti, s->cow); |
| 1295 | |
| 1296 | bad_cow: |
Mikulas Patocka | c241104 | 2010-08-12 04:13:51 +0100 | [diff] [blame] | 1297 | dm_put_device(ti, s->origin); |
| 1298 | |
| 1299 | bad_origin: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | kfree(s); |
| 1301 | |
Mike Snitzer | fc56f6f | 2009-12-10 23:52:12 +0000 | [diff] [blame] | 1302 | bad: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | return r; |
| 1304 | } |
| 1305 | |
Milan Broz | 31c93a0c | 2006-12-08 02:41:11 -0800 | [diff] [blame] | 1306 | static void __free_exceptions(struct dm_snapshot *s) |
| 1307 | { |
Heinz Mauelshagen | eb69aca | 2008-04-24 21:43:19 +0100 | [diff] [blame] | 1308 | dm_kcopyd_client_destroy(s->kcopyd_client); |
Milan Broz | 31c93a0c | 2006-12-08 02:41:11 -0800 | [diff] [blame] | 1309 | s->kcopyd_client = NULL; |
| 1310 | |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 1311 | dm_exception_table_exit(&s->pending, pending_cache); |
| 1312 | dm_exception_table_exit(&s->complete, exception_cache); |
Milan Broz | 31c93a0c | 2006-12-08 02:41:11 -0800 | [diff] [blame] | 1313 | } |
| 1314 | |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1315 | static void __handover_exceptions(struct dm_snapshot *snap_src, |
| 1316 | struct dm_snapshot *snap_dest) |
| 1317 | { |
| 1318 | union { |
| 1319 | struct dm_exception_table table_swap; |
| 1320 | struct dm_exception_store *store_swap; |
| 1321 | } u; |
| 1322 | |
| 1323 | /* |
| 1324 | * Swap all snapshot context information between the two instances. |
| 1325 | */ |
| 1326 | u.table_swap = snap_dest->complete; |
| 1327 | snap_dest->complete = snap_src->complete; |
| 1328 | snap_src->complete = u.table_swap; |
| 1329 | |
| 1330 | u.store_swap = snap_dest->store; |
| 1331 | snap_dest->store = snap_src->store; |
Mike Snitzer | b0d3cc0 | 2015-10-08 18:05:41 -0400 | [diff] [blame] | 1332 | snap_dest->store->userspace_supports_overflow = u.store_swap->userspace_supports_overflow; |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1333 | snap_src->store = u.store_swap; |
| 1334 | |
| 1335 | snap_dest->store->snap = snap_dest; |
| 1336 | snap_src->store->snap = snap_src; |
| 1337 | |
Mike Snitzer | 542f903 | 2012-07-27 15:08:00 +0100 | [diff] [blame] | 1338 | snap_dest->ti->max_io_len = snap_dest->store->chunk_size; |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1339 | snap_dest->valid = snap_src->valid; |
Mikulas Patocka | 76c44f6 | 2015-06-21 16:31:33 -0400 | [diff] [blame] | 1340 | snap_dest->snapshot_overflowed = snap_src->snapshot_overflowed; |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1341 | |
| 1342 | /* |
| 1343 | * Set source invalid to ensure it receives no further I/O. |
| 1344 | */ |
| 1345 | snap_src->valid = 0; |
| 1346 | } |
| 1347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | static void snapshot_dtr(struct dm_target *ti) |
| 1349 | { |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 1350 | #ifdef CONFIG_DM_DEBUG |
| 1351 | int i; |
| 1352 | #endif |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1353 | struct dm_snapshot *s = ti->private; |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1354 | struct dm_snapshot *snap_src = NULL, *snap_dest = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1356 | down_read(&_origins_lock); |
| 1357 | /* Check whether exception handover must be cancelled */ |
Mikulas Patocka | 9d3b15c | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 1358 | (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL); |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1359 | if (snap_src && snap_dest && (s == snap_src)) { |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 1360 | down_write(&snap_dest->lock); |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1361 | snap_dest->valid = 0; |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 1362 | up_write(&snap_dest->lock); |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1363 | DMERR("Cancelling snapshot handover."); |
| 1364 | } |
| 1365 | up_read(&_origins_lock); |
| 1366 | |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 1367 | if (dm_target_is_snapshot_merge(ti)) |
| 1368 | stop_merge(s); |
| 1369 | |
Alasdair G Kergon | 138728dc | 2006-03-27 01:17:50 -0800 | [diff] [blame] | 1370 | /* Prevent further origin writes from using this snapshot. */ |
| 1371 | /* After this returns there can be no new kcopyd jobs. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | unregister_snapshot(s); |
| 1373 | |
Mikulas Patocka | 879129d2 | 2008-10-30 13:33:16 +0000 | [diff] [blame] | 1374 | while (atomic_read(&s->pending_exceptions_count)) |
Mikulas Patocka | 90fa152 | 2009-01-06 03:04:54 +0000 | [diff] [blame] | 1375 | msleep(1); |
Mikulas Patocka | 879129d2 | 2008-10-30 13:33:16 +0000 | [diff] [blame] | 1376 | /* |
Kent Overstreet | 6f1c819 | 2018-05-20 18:25:53 -0400 | [diff] [blame] | 1377 | * Ensure instructions in mempool_exit aren't reordered |
Mikulas Patocka | 879129d2 | 2008-10-30 13:33:16 +0000 | [diff] [blame] | 1378 | * before atomic_read. |
| 1379 | */ |
| 1380 | smp_mb(); |
| 1381 | |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 1382 | #ifdef CONFIG_DM_DEBUG |
| 1383 | for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++) |
| 1384 | BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i])); |
| 1385 | #endif |
| 1386 | |
Milan Broz | 31c93a0c | 2006-12-08 02:41:11 -0800 | [diff] [blame] | 1387 | __free_exceptions(s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | |
Kent Overstreet | 6f1c819 | 2018-05-20 18:25:53 -0400 | [diff] [blame] | 1389 | mempool_exit(&s->pending_pool); |
Mikulas Patocka | 92e8681 | 2008-07-21 12:00:35 +0100 | [diff] [blame] | 1390 | |
Jonathan Brassow | fee1998 | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 1391 | dm_exception_store_destroy(s->store); |
Alasdair G Kergon | 138728dc | 2006-03-27 01:17:50 -0800 | [diff] [blame] | 1392 | |
Mike Snitzer | fc56f6f | 2009-12-10 23:52:12 +0000 | [diff] [blame] | 1393 | dm_put_device(ti, s->cow); |
| 1394 | |
Mikulas Patocka | c241104 | 2010-08-12 04:13:51 +0100 | [diff] [blame] | 1395 | dm_put_device(ti, s->origin); |
| 1396 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | kfree(s); |
| 1398 | } |
| 1399 | |
| 1400 | /* |
| 1401 | * Flush a list of buffers. |
| 1402 | */ |
| 1403 | static void flush_bios(struct bio *bio) |
| 1404 | { |
| 1405 | struct bio *n; |
| 1406 | |
| 1407 | while (bio) { |
| 1408 | n = bio->bi_next; |
| 1409 | bio->bi_next = NULL; |
| 1410 | generic_make_request(bio); |
| 1411 | bio = n; |
| 1412 | } |
| 1413 | } |
| 1414 | |
Mikulas Patocka | 515ad66 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 1415 | static int do_origin(struct dm_dev *origin, struct bio *bio); |
| 1416 | |
| 1417 | /* |
| 1418 | * Flush a list of buffers. |
| 1419 | */ |
| 1420 | static void retry_origin_bios(struct dm_snapshot *s, struct bio *bio) |
| 1421 | { |
| 1422 | struct bio *n; |
| 1423 | int r; |
| 1424 | |
| 1425 | while (bio) { |
| 1426 | n = bio->bi_next; |
| 1427 | bio->bi_next = NULL; |
| 1428 | r = do_origin(s->origin, bio); |
| 1429 | if (r == DM_MAPIO_REMAPPED) |
| 1430 | generic_make_request(bio); |
| 1431 | bio = n; |
| 1432 | } |
| 1433 | } |
| 1434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | /* |
| 1436 | * Error a list of buffers. |
| 1437 | */ |
| 1438 | static void error_bios(struct bio *bio) |
| 1439 | { |
| 1440 | struct bio *n; |
| 1441 | |
| 1442 | while (bio) { |
| 1443 | n = bio->bi_next; |
| 1444 | bio->bi_next = NULL; |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1445 | bio_io_error(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | bio = n; |
| 1447 | } |
| 1448 | } |
| 1449 | |
Alasdair G Kergon | 695368a | 2006-10-03 01:15:31 -0700 | [diff] [blame] | 1450 | static void __invalidate_snapshot(struct dm_snapshot *s, int err) |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1451 | { |
| 1452 | if (!s->valid) |
| 1453 | return; |
| 1454 | |
| 1455 | if (err == -EIO) |
| 1456 | DMERR("Invalidating snapshot: Error reading/writing."); |
| 1457 | else if (err == -ENOMEM) |
| 1458 | DMERR("Invalidating snapshot: Unable to allocate exception."); |
| 1459 | |
Jonathan Brassow | 493df71 | 2009-04-02 19:55:31 +0100 | [diff] [blame] | 1460 | if (s->store->type->drop_snapshot) |
| 1461 | s->store->type->drop_snapshot(s->store); |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1462 | |
| 1463 | s->valid = 0; |
| 1464 | |
Mike Snitzer | fc56f6f | 2009-12-10 23:52:12 +0000 | [diff] [blame] | 1465 | dm_table_event(s->ti->table); |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1466 | } |
| 1467 | |
Mikulas Patocka | 385277b | 2016-01-08 19:07:55 -0500 | [diff] [blame] | 1468 | static void pending_complete(void *context, int success) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | { |
Mikulas Patocka | 385277b | 2016-01-08 19:07:55 -0500 | [diff] [blame] | 1470 | struct dm_snap_pending_exception *pe = context; |
Jon Brassow | 1d4989c | 2009-12-10 23:52:10 +0000 | [diff] [blame] | 1471 | struct dm_exception *e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | struct dm_snapshot *s = pe->snap; |
Alasdair G Kergon | 9d493fa | 2006-10-03 01:15:29 -0700 | [diff] [blame] | 1473 | struct bio *origin_bios = NULL; |
| 1474 | struct bio *snapshot_bios = NULL; |
Mikulas Patocka | a6e50b4 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 1475 | struct bio *full_bio = NULL; |
Alasdair G Kergon | 9d493fa | 2006-10-03 01:15:29 -0700 | [diff] [blame] | 1476 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1478 | if (!success) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | /* Read/write error - snapshot is unusable */ |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 1480 | down_write(&s->lock); |
Alasdair G Kergon | 695368a | 2006-10-03 01:15:31 -0700 | [diff] [blame] | 1481 | __invalidate_snapshot(s, -EIO); |
Alasdair G Kergon | 9d493fa | 2006-10-03 01:15:29 -0700 | [diff] [blame] | 1482 | error = 1; |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1483 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | } |
| 1485 | |
Mikulas Patocka | 119bc54 | 2014-01-13 19:13:36 -0500 | [diff] [blame] | 1486 | e = alloc_completed_exception(GFP_NOIO); |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1487 | if (!e) { |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 1488 | down_write(&s->lock); |
Alasdair G Kergon | 695368a | 2006-10-03 01:15:31 -0700 | [diff] [blame] | 1489 | __invalidate_snapshot(s, -ENOMEM); |
Alasdair G Kergon | 9d493fa | 2006-10-03 01:15:29 -0700 | [diff] [blame] | 1490 | error = 1; |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1491 | goto out; |
| 1492 | } |
| 1493 | *e = pe->e; |
| 1494 | |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 1495 | down_write(&s->lock); |
Alasdair G Kergon | 9d493fa | 2006-10-03 01:15:29 -0700 | [diff] [blame] | 1496 | if (!s->valid) { |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 1497 | free_completed_exception(e); |
Alasdair G Kergon | 9d493fa | 2006-10-03 01:15:29 -0700 | [diff] [blame] | 1498 | error = 1; |
| 1499 | goto out; |
| 1500 | } |
| 1501 | |
Mikulas Patocka | a8d41b5 | 2008-07-21 12:00:34 +0100 | [diff] [blame] | 1502 | /* |
Nikos Tsironis | 65fc7c3 | 2019-03-17 14:22:55 +0200 | [diff] [blame] | 1503 | * Add a proper exception. After inserting the completed exception all |
| 1504 | * subsequent snapshot reads to this chunk will be redirected to the |
| 1505 | * COW device. This ensures that we do not starve. Moreover, as long |
| 1506 | * as the pending exception exists, neither origin writes nor snapshot |
| 1507 | * merging can overwrite the chunk in origin. |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1508 | */ |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 1509 | dm_insert_exception(&s->complete, e); |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1510 | |
Nikos Tsironis | 65fc7c3 | 2019-03-17 14:22:55 +0200 | [diff] [blame] | 1511 | /* Wait for conflicting reads to drain */ |
| 1512 | if (__chunk_is_tracked(s, pe->e.old_chunk)) { |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 1513 | up_write(&s->lock); |
Nikos Tsironis | 65fc7c3 | 2019-03-17 14:22:55 +0200 | [diff] [blame] | 1514 | __check_for_conflicting_io(s, pe->e.old_chunk); |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 1515 | down_write(&s->lock); |
Nikos Tsironis | 65fc7c3 | 2019-03-17 14:22:55 +0200 | [diff] [blame] | 1516 | } |
| 1517 | |
Jonathan Brassow | a2d2b03 | 2011-08-02 12:32:03 +0100 | [diff] [blame] | 1518 | out: |
Nikos Tsironis | 65fc7c3 | 2019-03-17 14:22:55 +0200 | [diff] [blame] | 1519 | /* Remove the in-flight exception from the list */ |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 1520 | dm_remove_exception(&pe->e); |
Alasdair G Kergon | 9d493fa | 2006-10-03 01:15:29 -0700 | [diff] [blame] | 1521 | snapshot_bios = bio_list_get(&pe->snapshot_bios); |
Mikulas Patocka | 515ad66 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 1522 | origin_bios = bio_list_get(&pe->origin_bios); |
Mikulas Patocka | a6e50b4 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 1523 | full_bio = pe->full_bio; |
Mikulas Patocka | fe3265b | 2015-11-25 16:03:31 -0500 | [diff] [blame] | 1524 | if (full_bio) |
Mikulas Patocka | a6e50b4 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 1525 | full_bio->bi_end_io = pe->full_bio_end_io; |
Mikulas Patocka | 73dfd07 | 2009-12-10 23:52:34 +0000 | [diff] [blame] | 1526 | increment_pending_exceptions_done_count(); |
| 1527 | |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 1528 | up_write(&s->lock); |
Alasdair G Kergon | 9d493fa | 2006-10-03 01:15:29 -0700 | [diff] [blame] | 1529 | |
| 1530 | /* Submit any pending write bios */ |
Mikulas Patocka | a6e50b4 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 1531 | if (error) { |
| 1532 | if (full_bio) |
| 1533 | bio_io_error(full_bio); |
Alasdair G Kergon | 9d493fa | 2006-10-03 01:15:29 -0700 | [diff] [blame] | 1534 | error_bios(snapshot_bios); |
Mikulas Patocka | a6e50b4 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 1535 | } else { |
| 1536 | if (full_bio) |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 1537 | bio_endio(full_bio); |
Alasdair G Kergon | 9d493fa | 2006-10-03 01:15:29 -0700 | [diff] [blame] | 1538 | flush_bios(snapshot_bios); |
Mikulas Patocka | a6e50b4 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 1539 | } |
Alasdair G Kergon | 9d493fa | 2006-10-03 01:15:29 -0700 | [diff] [blame] | 1540 | |
Mikulas Patocka | 515ad66 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 1541 | retry_origin_bios(s, origin_bios); |
Mikulas Patocka | 22aa66a | 2015-02-17 14:34:00 -0500 | [diff] [blame] | 1542 | |
| 1543 | free_pending_exception(pe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | } |
| 1545 | |
Mikulas Patocka | 230c83a | 2013-11-29 18:13:37 -0500 | [diff] [blame] | 1546 | static void complete_exception(struct dm_snap_pending_exception *pe) |
| 1547 | { |
| 1548 | struct dm_snapshot *s = pe->snap; |
| 1549 | |
Mikulas Patocka | 385277b | 2016-01-08 19:07:55 -0500 | [diff] [blame] | 1550 | /* Update the metadata if we are persistent */ |
| 1551 | s->store->type->commit_exception(s->store, &pe->e, !pe->copy_error, |
| 1552 | pending_complete, pe); |
Mikulas Patocka | 230c83a | 2013-11-29 18:13:37 -0500 | [diff] [blame] | 1553 | } |
| 1554 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | /* |
| 1556 | * Called when the copy I/O has finished. kcopyd actually runs |
| 1557 | * this code so don't block. |
| 1558 | */ |
Alasdair G Kergon | 4cdc1d1 | 2008-03-28 14:16:10 -0700 | [diff] [blame] | 1559 | static void copy_callback(int read_err, unsigned long write_err, void *context) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | { |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1561 | struct dm_snap_pending_exception *pe = context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | struct dm_snapshot *s = pe->snap; |
| 1563 | |
Mikulas Patocka | 230c83a | 2013-11-29 18:13:37 -0500 | [diff] [blame] | 1564 | pe->copy_error = read_err || write_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | |
Mikulas Patocka | 230c83a | 2013-11-29 18:13:37 -0500 | [diff] [blame] | 1566 | if (pe->exception_sequence == s->exception_complete_sequence) { |
David Jeffery | 3db2776 | 2018-08-07 16:56:00 -0400 | [diff] [blame] | 1567 | struct rb_node *next; |
| 1568 | |
Mikulas Patocka | 230c83a | 2013-11-29 18:13:37 -0500 | [diff] [blame] | 1569 | s->exception_complete_sequence++; |
| 1570 | complete_exception(pe); |
| 1571 | |
David Jeffery | 3db2776 | 2018-08-07 16:56:00 -0400 | [diff] [blame] | 1572 | next = rb_first(&s->out_of_order_tree); |
| 1573 | while (next) { |
| 1574 | pe = rb_entry(next, struct dm_snap_pending_exception, |
| 1575 | out_of_order_node); |
Mikulas Patocka | 230c83a | 2013-11-29 18:13:37 -0500 | [diff] [blame] | 1576 | if (pe->exception_sequence != s->exception_complete_sequence) |
| 1577 | break; |
David Jeffery | 3db2776 | 2018-08-07 16:56:00 -0400 | [diff] [blame] | 1578 | next = rb_next(next); |
Mikulas Patocka | 230c83a | 2013-11-29 18:13:37 -0500 | [diff] [blame] | 1579 | s->exception_complete_sequence++; |
David Jeffery | 3db2776 | 2018-08-07 16:56:00 -0400 | [diff] [blame] | 1580 | rb_erase(&pe->out_of_order_node, &s->out_of_order_tree); |
Mikulas Patocka | 230c83a | 2013-11-29 18:13:37 -0500 | [diff] [blame] | 1581 | complete_exception(pe); |
David Jeffery | 3db2776 | 2018-08-07 16:56:00 -0400 | [diff] [blame] | 1582 | cond_resched(); |
Mikulas Patocka | 230c83a | 2013-11-29 18:13:37 -0500 | [diff] [blame] | 1583 | } |
| 1584 | } else { |
David Jeffery | 3db2776 | 2018-08-07 16:56:00 -0400 | [diff] [blame] | 1585 | struct rb_node *parent = NULL; |
| 1586 | struct rb_node **p = &s->out_of_order_tree.rb_node; |
Mikulas Patocka | 230c83a | 2013-11-29 18:13:37 -0500 | [diff] [blame] | 1587 | struct dm_snap_pending_exception *pe2; |
| 1588 | |
David Jeffery | 3db2776 | 2018-08-07 16:56:00 -0400 | [diff] [blame] | 1589 | while (*p) { |
| 1590 | pe2 = rb_entry(*p, struct dm_snap_pending_exception, out_of_order_node); |
| 1591 | parent = *p; |
| 1592 | |
| 1593 | BUG_ON(pe->exception_sequence == pe2->exception_sequence); |
| 1594 | if (pe->exception_sequence < pe2->exception_sequence) |
| 1595 | p = &((*p)->rb_left); |
| 1596 | else |
| 1597 | p = &((*p)->rb_right); |
Mikulas Patocka | 230c83a | 2013-11-29 18:13:37 -0500 | [diff] [blame] | 1598 | } |
David Jeffery | 3db2776 | 2018-08-07 16:56:00 -0400 | [diff] [blame] | 1599 | |
| 1600 | rb_link_node(&pe->out_of_order_node, parent, p); |
| 1601 | rb_insert_color(&pe->out_of_order_node, &s->out_of_order_tree); |
Mikulas Patocka | 230c83a | 2013-11-29 18:13:37 -0500 | [diff] [blame] | 1602 | } |
Nikos Tsironis | 721b1d9 | 2018-10-31 17:53:08 -0400 | [diff] [blame] | 1603 | up(&s->cow_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | } |
| 1605 | |
| 1606 | /* |
| 1607 | * Dispatches the copy operation to kcopyd. |
| 1608 | */ |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1609 | static void start_copy(struct dm_snap_pending_exception *pe) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | { |
| 1611 | struct dm_snapshot *s = pe->snap; |
Heinz Mauelshagen | 22a1ceb | 2008-04-24 21:43:17 +0100 | [diff] [blame] | 1612 | struct dm_io_region src, dest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1613 | struct block_device *bdev = s->origin->bdev; |
| 1614 | sector_t dev_size; |
| 1615 | |
| 1616 | dev_size = get_dev_size(bdev); |
| 1617 | |
| 1618 | src.bdev = bdev; |
Jonathan Brassow | 71fab00 | 2009-04-02 19:55:33 +0100 | [diff] [blame] | 1619 | src.sector = chunk_to_sector(s->store, pe->e.old_chunk); |
Mikulas Patocka | df96eee | 2009-10-16 23:18:17 +0100 | [diff] [blame] | 1620 | src.count = min((sector_t)s->store->chunk_size, dev_size - src.sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | |
Mike Snitzer | fc56f6f | 2009-12-10 23:52:12 +0000 | [diff] [blame] | 1622 | dest.bdev = s->cow->bdev; |
Jonathan Brassow | 71fab00 | 2009-04-02 19:55:33 +0100 | [diff] [blame] | 1623 | dest.sector = chunk_to_sector(s->store, pe->e.new_chunk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | dest.count = src.count; |
| 1625 | |
| 1626 | /* Hand over to kcopyd */ |
Nikos Tsironis | 721b1d9 | 2018-10-31 17:53:08 -0400 | [diff] [blame] | 1627 | down(&s->cow_count); |
Jonathan Brassow | a2d2b03 | 2011-08-02 12:32:03 +0100 | [diff] [blame] | 1628 | dm_kcopyd_copy(s->kcopyd_client, &src, 1, &dest, 0, copy_callback, pe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1629 | } |
| 1630 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 1631 | static void full_bio_end_io(struct bio *bio) |
Mikulas Patocka | a6e50b4 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 1632 | { |
| 1633 | void *callback_data = bio->bi_private; |
| 1634 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1635 | dm_kcopyd_do_callback(callback_data, 0, bio->bi_status ? 1 : 0); |
Mikulas Patocka | a6e50b4 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 1636 | } |
| 1637 | |
| 1638 | static void start_full_bio(struct dm_snap_pending_exception *pe, |
| 1639 | struct bio *bio) |
| 1640 | { |
| 1641 | struct dm_snapshot *s = pe->snap; |
| 1642 | void *callback_data; |
| 1643 | |
| 1644 | pe->full_bio = bio; |
| 1645 | pe->full_bio_end_io = bio->bi_end_io; |
Mikulas Patocka | a6e50b4 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 1646 | |
Nikos Tsironis | 721b1d9 | 2018-10-31 17:53:08 -0400 | [diff] [blame] | 1647 | down(&s->cow_count); |
Mikulas Patocka | a6e50b4 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 1648 | callback_data = dm_kcopyd_prepare_callback(s->kcopyd_client, |
| 1649 | copy_callback, pe); |
| 1650 | |
| 1651 | bio->bi_end_io = full_bio_end_io; |
| 1652 | bio->bi_private = callback_data; |
| 1653 | |
| 1654 | generic_make_request(bio); |
| 1655 | } |
| 1656 | |
Mikulas Patocka | 2913808 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 1657 | static struct dm_snap_pending_exception * |
| 1658 | __lookup_pending_exception(struct dm_snapshot *s, chunk_t chunk) |
| 1659 | { |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 1660 | struct dm_exception *e = dm_lookup_exception(&s->pending, chunk); |
Mikulas Patocka | 2913808 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 1661 | |
| 1662 | if (!e) |
| 1663 | return NULL; |
| 1664 | |
| 1665 | return container_of(e, struct dm_snap_pending_exception, e); |
| 1666 | } |
| 1667 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | /* |
Nikos Tsironis | 65fc7c3 | 2019-03-17 14:22:55 +0200 | [diff] [blame] | 1669 | * Inserts a pending exception into the pending table. |
| 1670 | * |
| 1671 | * NOTE: a write lock must be held on snap->lock before calling |
| 1672 | * this. |
| 1673 | */ |
| 1674 | static struct dm_snap_pending_exception * |
| 1675 | __insert_pending_exception(struct dm_snapshot *s, |
| 1676 | struct dm_snap_pending_exception *pe, chunk_t chunk) |
| 1677 | { |
| 1678 | pe->e.old_chunk = chunk; |
| 1679 | bio_list_init(&pe->origin_bios); |
| 1680 | bio_list_init(&pe->snapshot_bios); |
| 1681 | pe->started = 0; |
| 1682 | pe->full_bio = NULL; |
| 1683 | |
| 1684 | if (s->store->type->prepare_exception(s->store, &pe->e)) { |
| 1685 | free_pending_exception(pe); |
| 1686 | return NULL; |
| 1687 | } |
| 1688 | |
| 1689 | pe->exception_sequence = s->exception_start_sequence++; |
| 1690 | |
| 1691 | dm_insert_exception(&s->pending, &pe->e); |
| 1692 | |
| 1693 | return pe; |
| 1694 | } |
| 1695 | |
| 1696 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | * Looks to see if this snapshot already has a pending exception |
| 1698 | * for this chunk, otherwise it allocates a new one and inserts |
| 1699 | * it into the pending table. |
| 1700 | * |
| 1701 | * NOTE: a write lock must be held on snap->lock before calling |
| 1702 | * this. |
| 1703 | */ |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1704 | static struct dm_snap_pending_exception * |
Mikulas Patocka | c662139 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 1705 | __find_pending_exception(struct dm_snapshot *s, |
| 1706 | struct dm_snap_pending_exception *pe, chunk_t chunk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | { |
Mikulas Patocka | c662139 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 1708 | struct dm_snap_pending_exception *pe2; |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1709 | |
Mikulas Patocka | 2913808 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 1710 | pe2 = __lookup_pending_exception(s, chunk); |
| 1711 | if (pe2) { |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1712 | free_pending_exception(pe); |
Mikulas Patocka | 2913808 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 1713 | return pe2; |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1714 | } |
| 1715 | |
Nikos Tsironis | 65fc7c3 | 2019-03-17 14:22:55 +0200 | [diff] [blame] | 1716 | return __insert_pending_exception(s, pe, chunk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1717 | } |
| 1718 | |
Jon Brassow | 1d4989c | 2009-12-10 23:52:10 +0000 | [diff] [blame] | 1719 | static void remap_exception(struct dm_snapshot *s, struct dm_exception *e, |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 1720 | struct bio *bio, chunk_t chunk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1721 | { |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1722 | bio_set_dev(bio, s->cow->bdev); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1723 | bio->bi_iter.bi_sector = |
| 1724 | chunk_to_sector(s->store, dm_chunk_number(e->new_chunk) + |
| 1725 | (chunk - e->old_chunk)) + |
| 1726 | (bio->bi_iter.bi_sector & s->store->chunk_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | } |
| 1728 | |
Mikulas Patocka | 7de3ee5 | 2012-12-21 20:23:41 +0000 | [diff] [blame] | 1729 | static int snapshot_map(struct dm_target *ti, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | { |
Jon Brassow | 1d4989c | 2009-12-10 23:52:10 +0000 | [diff] [blame] | 1731 | struct dm_exception *e; |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1732 | struct dm_snapshot *s = ti->private; |
Kiyoshi Ueda | d2a7ad2 | 2006-12-08 02:41:06 -0800 | [diff] [blame] | 1733 | int r = DM_MAPIO_REMAPPED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | chunk_t chunk; |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1735 | struct dm_snap_pending_exception *pe = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1736 | |
Mikulas Patocka | ee18026 | 2012-12-21 20:23:41 +0000 | [diff] [blame] | 1737 | init_tracked_chunk(bio); |
| 1738 | |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 1739 | if (bio->bi_opf & REQ_PREFLUSH) { |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1740 | bio_set_dev(bio, s->cow->bdev); |
Mikulas Patocka | 494b3ee | 2009-06-22 10:12:25 +0100 | [diff] [blame] | 1741 | return DM_MAPIO_REMAPPED; |
| 1742 | } |
| 1743 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1744 | chunk = sector_to_chunk(s->store, bio->bi_iter.bi_sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | |
| 1746 | /* Full snapshots are not usable */ |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1747 | /* To get here the table must be live so s->active is always set. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | if (!s->valid) |
Christoph Hellwig | 846785e | 2017-06-03 09:38:02 +0200 | [diff] [blame] | 1749 | return DM_MAPIO_KILL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 1751 | down_write(&s->lock); |
Alasdair G Kergon | ba40a2a | 2006-10-03 01:15:28 -0700 | [diff] [blame] | 1752 | |
Christoph Hellwig | 7024628 | 2016-07-19 11:28:41 +0200 | [diff] [blame] | 1753 | if (!s->valid || (unlikely(s->snapshot_overflowed) && |
| 1754 | bio_data_dir(bio) == WRITE)) { |
Christoph Hellwig | 846785e | 2017-06-03 09:38:02 +0200 | [diff] [blame] | 1755 | r = DM_MAPIO_KILL; |
Alasdair G Kergon | ba40a2a | 2006-10-03 01:15:28 -0700 | [diff] [blame] | 1756 | goto out_unlock; |
| 1757 | } |
| 1758 | |
| 1759 | /* If the block is already remapped - use that, else remap it */ |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 1760 | e = dm_lookup_exception(&s->complete, chunk); |
Alasdair G Kergon | ba40a2a | 2006-10-03 01:15:28 -0700 | [diff] [blame] | 1761 | if (e) { |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 1762 | remap_exception(s, e, bio, chunk); |
Alasdair G Kergon | ba40a2a | 2006-10-03 01:15:28 -0700 | [diff] [blame] | 1763 | goto out_unlock; |
| 1764 | } |
| 1765 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | /* |
| 1767 | * Write to snapshot - higher level takes care of RW/RO |
| 1768 | * flags so we should only get this if we are |
| 1769 | * writeable. |
| 1770 | */ |
Christoph Hellwig | 7024628 | 2016-07-19 11:28:41 +0200 | [diff] [blame] | 1771 | if (bio_data_dir(bio) == WRITE) { |
Mikulas Patocka | 2913808 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 1772 | pe = __lookup_pending_exception(s, chunk); |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1773 | if (!pe) { |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 1774 | up_write(&s->lock); |
Mikulas Patocka | c662139 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 1775 | pe = alloc_pending_exception(s); |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 1776 | down_write(&s->lock); |
Mikulas Patocka | c662139 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 1777 | |
Mikulas Patocka | 76c44f6 | 2015-06-21 16:31:33 -0400 | [diff] [blame] | 1778 | if (!s->valid || s->snapshot_overflowed) { |
Mikulas Patocka | c662139 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 1779 | free_pending_exception(pe); |
Christoph Hellwig | 846785e | 2017-06-03 09:38:02 +0200 | [diff] [blame] | 1780 | r = DM_MAPIO_KILL; |
Mikulas Patocka | c662139 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 1781 | goto out_unlock; |
| 1782 | } |
| 1783 | |
Jon Brassow | 3510cb9 | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 1784 | e = dm_lookup_exception(&s->complete, chunk); |
Mikulas Patocka | 35bf659 | 2009-04-02 19:55:26 +0100 | [diff] [blame] | 1785 | if (e) { |
| 1786 | free_pending_exception(pe); |
| 1787 | remap_exception(s, e, bio, chunk); |
| 1788 | goto out_unlock; |
| 1789 | } |
| 1790 | |
Mikulas Patocka | c662139 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 1791 | pe = __find_pending_exception(s, pe, chunk); |
Mikulas Patocka | 2913808 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 1792 | if (!pe) { |
Mike Snitzer | b0d3cc0 | 2015-10-08 18:05:41 -0400 | [diff] [blame] | 1793 | if (s->store->userspace_supports_overflow) { |
| 1794 | s->snapshot_overflowed = 1; |
| 1795 | DMERR("Snapshot overflowed: Unable to allocate exception."); |
| 1796 | } else |
| 1797 | __invalidate_snapshot(s, -ENOMEM); |
Christoph Hellwig | 846785e | 2017-06-03 09:38:02 +0200 | [diff] [blame] | 1798 | r = DM_MAPIO_KILL; |
Mikulas Patocka | 2913808 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 1799 | goto out_unlock; |
| 1800 | } |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1801 | } |
| 1802 | |
Milan Broz | d74f81f | 2008-02-08 02:11:27 +0000 | [diff] [blame] | 1803 | remap_exception(s, &pe->e, bio, chunk); |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1804 | |
Kiyoshi Ueda | d2a7ad2 | 2006-12-08 02:41:06 -0800 | [diff] [blame] | 1805 | r = DM_MAPIO_SUBMITTED; |
Alasdair G Kergon | ba40a2a | 2006-10-03 01:15:28 -0700 | [diff] [blame] | 1806 | |
Mikulas Patocka | a6e50b4 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 1807 | if (!pe->started && |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1808 | bio->bi_iter.bi_size == |
| 1809 | (s->store->chunk_size << SECTOR_SHIFT)) { |
Mikulas Patocka | a6e50b4 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 1810 | pe->started = 1; |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 1811 | up_write(&s->lock); |
Mikulas Patocka | a6e50b4 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 1812 | start_full_bio(pe, bio); |
| 1813 | goto out; |
| 1814 | } |
| 1815 | |
| 1816 | bio_list_add(&pe->snapshot_bios, bio); |
| 1817 | |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1818 | if (!pe->started) { |
| 1819 | /* this is protected by snap->lock */ |
| 1820 | pe->started = 1; |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 1821 | up_write(&s->lock); |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 1822 | start_copy(pe); |
Alasdair G Kergon | ba40a2a | 2006-10-03 01:15:28 -0700 | [diff] [blame] | 1823 | goto out; |
| 1824 | } |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 1825 | } else { |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1826 | bio_set_dev(bio, s->origin->bdev); |
Mikulas Patocka | ee18026 | 2012-12-21 20:23:41 +0000 | [diff] [blame] | 1827 | track_chunk(s, bio, chunk); |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 1828 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1829 | |
Jonathan Brassow | a2d2b03 | 2011-08-02 12:32:03 +0100 | [diff] [blame] | 1830 | out_unlock: |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 1831 | up_write(&s->lock); |
Jonathan Brassow | a2d2b03 | 2011-08-02 12:32:03 +0100 | [diff] [blame] | 1832 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | return r; |
| 1834 | } |
| 1835 | |
Mikulas Patocka | 3452c2a | 2009-12-10 23:52:31 +0000 | [diff] [blame] | 1836 | /* |
| 1837 | * A snapshot-merge target behaves like a combination of a snapshot |
| 1838 | * target and a snapshot-origin target. It only generates new |
| 1839 | * exceptions in other snapshots and not in the one that is being |
| 1840 | * merged. |
| 1841 | * |
| 1842 | * For each chunk, if there is an existing exception, it is used to |
| 1843 | * redirect I/O to the cow device. Otherwise I/O is sent to the origin, |
| 1844 | * which in turn might generate exceptions in other snapshots. |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 1845 | * If merging is currently taking place on the chunk in question, the |
| 1846 | * I/O is deferred by adding it to s->bios_queued_during_merge. |
Mikulas Patocka | 3452c2a | 2009-12-10 23:52:31 +0000 | [diff] [blame] | 1847 | */ |
Mikulas Patocka | 7de3ee5 | 2012-12-21 20:23:41 +0000 | [diff] [blame] | 1848 | static int snapshot_merge_map(struct dm_target *ti, struct bio *bio) |
Mikulas Patocka | 3452c2a | 2009-12-10 23:52:31 +0000 | [diff] [blame] | 1849 | { |
| 1850 | struct dm_exception *e; |
| 1851 | struct dm_snapshot *s = ti->private; |
| 1852 | int r = DM_MAPIO_REMAPPED; |
| 1853 | chunk_t chunk; |
| 1854 | |
Mikulas Patocka | ee18026 | 2012-12-21 20:23:41 +0000 | [diff] [blame] | 1855 | init_tracked_chunk(bio); |
| 1856 | |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 1857 | if (bio->bi_opf & REQ_PREFLUSH) { |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1858 | if (!dm_bio_get_target_bio_nr(bio)) |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1859 | bio_set_dev(bio, s->origin->bdev); |
Mike Snitzer | 10b8106 | 2009-12-10 23:52:31 +0000 | [diff] [blame] | 1860 | else |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1861 | bio_set_dev(bio, s->cow->bdev); |
Mike Snitzer | 10b8106 | 2009-12-10 23:52:31 +0000 | [diff] [blame] | 1862 | return DM_MAPIO_REMAPPED; |
| 1863 | } |
| 1864 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1865 | chunk = sector_to_chunk(s->store, bio->bi_iter.bi_sector); |
Mikulas Patocka | 3452c2a | 2009-12-10 23:52:31 +0000 | [diff] [blame] | 1866 | |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 1867 | down_write(&s->lock); |
Mikulas Patocka | 3452c2a | 2009-12-10 23:52:31 +0000 | [diff] [blame] | 1868 | |
Mikulas Patocka | d2fdb77 | 2009-12-10 23:52:36 +0000 | [diff] [blame] | 1869 | /* Full merging snapshots are redirected to the origin */ |
| 1870 | if (!s->valid) |
| 1871 | goto redirect_to_origin; |
Mikulas Patocka | 3452c2a | 2009-12-10 23:52:31 +0000 | [diff] [blame] | 1872 | |
| 1873 | /* If the block is already remapped - use that */ |
| 1874 | e = dm_lookup_exception(&s->complete, chunk); |
| 1875 | if (e) { |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 1876 | /* Queue writes overlapping with chunks being merged */ |
Christoph Hellwig | 7024628 | 2016-07-19 11:28:41 +0200 | [diff] [blame] | 1877 | if (bio_data_dir(bio) == WRITE && |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 1878 | chunk >= s->first_merging_chunk && |
| 1879 | chunk < (s->first_merging_chunk + |
| 1880 | s->num_merging_chunks)) { |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1881 | bio_set_dev(bio, s->origin->bdev); |
Mikulas Patocka | 9fe86254 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 1882 | bio_list_add(&s->bios_queued_during_merge, bio); |
| 1883 | r = DM_MAPIO_SUBMITTED; |
| 1884 | goto out_unlock; |
| 1885 | } |
Mikulas Patocka | 17aa033 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 1886 | |
Mikulas Patocka | 3452c2a | 2009-12-10 23:52:31 +0000 | [diff] [blame] | 1887 | remap_exception(s, e, bio, chunk); |
Mikulas Patocka | 17aa033 | 2009-12-10 23:52:33 +0000 | [diff] [blame] | 1888 | |
Christoph Hellwig | 7024628 | 2016-07-19 11:28:41 +0200 | [diff] [blame] | 1889 | if (bio_data_dir(bio) == WRITE) |
Mikulas Patocka | ee18026 | 2012-12-21 20:23:41 +0000 | [diff] [blame] | 1890 | track_chunk(s, bio, chunk); |
Mikulas Patocka | 3452c2a | 2009-12-10 23:52:31 +0000 | [diff] [blame] | 1891 | goto out_unlock; |
| 1892 | } |
| 1893 | |
Mikulas Patocka | d2fdb77 | 2009-12-10 23:52:36 +0000 | [diff] [blame] | 1894 | redirect_to_origin: |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1895 | bio_set_dev(bio, s->origin->bdev); |
Mikulas Patocka | 3452c2a | 2009-12-10 23:52:31 +0000 | [diff] [blame] | 1896 | |
Christoph Hellwig | 7024628 | 2016-07-19 11:28:41 +0200 | [diff] [blame] | 1897 | if (bio_data_dir(bio) == WRITE) { |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 1898 | up_write(&s->lock); |
Mikulas Patocka | 3452c2a | 2009-12-10 23:52:31 +0000 | [diff] [blame] | 1899 | return do_origin(s->origin, bio); |
| 1900 | } |
| 1901 | |
| 1902 | out_unlock: |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 1903 | up_write(&s->lock); |
Mikulas Patocka | 3452c2a | 2009-12-10 23:52:31 +0000 | [diff] [blame] | 1904 | |
| 1905 | return r; |
| 1906 | } |
| 1907 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1908 | static int snapshot_end_io(struct dm_target *ti, struct bio *bio, |
| 1909 | blk_status_t *error) |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 1910 | { |
| 1911 | struct dm_snapshot *s = ti->private; |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 1912 | |
Mikulas Patocka | ee18026 | 2012-12-21 20:23:41 +0000 | [diff] [blame] | 1913 | if (is_bio_tracked(bio)) |
| 1914 | stop_tracking_chunk(s, bio); |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 1915 | |
Christoph Hellwig | 1be5690 | 2017-06-03 09:38:03 +0200 | [diff] [blame] | 1916 | return DM_ENDIO_DONE; |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 1917 | } |
| 1918 | |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 1919 | static void snapshot_merge_presuspend(struct dm_target *ti) |
| 1920 | { |
| 1921 | struct dm_snapshot *s = ti->private; |
| 1922 | |
| 1923 | stop_merge(s); |
| 1924 | } |
| 1925 | |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1926 | static int snapshot_preresume(struct dm_target *ti) |
| 1927 | { |
| 1928 | int r = 0; |
| 1929 | struct dm_snapshot *s = ti->private; |
| 1930 | struct dm_snapshot *snap_src = NULL, *snap_dest = NULL; |
| 1931 | |
| 1932 | down_read(&_origins_lock); |
Mikulas Patocka | 9d3b15c | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 1933 | (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL); |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1934 | if (snap_src && snap_dest) { |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 1935 | down_read(&snap_src->lock); |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1936 | if (s == snap_src) { |
| 1937 | DMERR("Unable to resume snapshot source until " |
| 1938 | "handover completes."); |
| 1939 | r = -EINVAL; |
Mike Snitzer | b83b2f2 | 2011-01-13 19:59:59 +0000 | [diff] [blame] | 1940 | } else if (!dm_suspended(snap_src->ti)) { |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1941 | DMERR("Unable to perform snapshot handover until " |
| 1942 | "source is suspended."); |
| 1943 | r = -EINVAL; |
| 1944 | } |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 1945 | up_read(&snap_src->lock); |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1946 | } |
| 1947 | up_read(&_origins_lock); |
| 1948 | |
| 1949 | return r; |
| 1950 | } |
| 1951 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 | static void snapshot_resume(struct dm_target *ti) |
| 1953 | { |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1954 | struct dm_snapshot *s = ti->private; |
Mikulas Patocka | 09ee96b | 2015-02-26 11:41:28 -0500 | [diff] [blame] | 1955 | struct dm_snapshot *snap_src = NULL, *snap_dest = NULL, *snap_merging = NULL; |
Mikulas Patocka | b735fed | 2015-02-26 11:40:35 -0500 | [diff] [blame] | 1956 | struct dm_origin *o; |
| 1957 | struct mapped_device *origin_md = NULL; |
Mikulas Patocka | 09ee96b | 2015-02-26 11:41:28 -0500 | [diff] [blame] | 1958 | bool must_restart_merging = false; |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1959 | |
| 1960 | down_read(&_origins_lock); |
Mikulas Patocka | b735fed | 2015-02-26 11:40:35 -0500 | [diff] [blame] | 1961 | |
| 1962 | o = __lookup_dm_origin(s->origin->bdev); |
| 1963 | if (o) |
| 1964 | origin_md = dm_table_get_md(o->ti->table); |
Mikulas Patocka | 09ee96b | 2015-02-26 11:41:28 -0500 | [diff] [blame] | 1965 | if (!origin_md) { |
| 1966 | (void) __find_snapshots_sharing_cow(s, NULL, NULL, &snap_merging); |
| 1967 | if (snap_merging) |
| 1968 | origin_md = dm_table_get_md(snap_merging->ti->table); |
| 1969 | } |
Mikulas Patocka | b735fed | 2015-02-26 11:40:35 -0500 | [diff] [blame] | 1970 | if (origin_md == dm_table_get_md(ti->table)) |
| 1971 | origin_md = NULL; |
Mikulas Patocka | 09ee96b | 2015-02-26 11:41:28 -0500 | [diff] [blame] | 1972 | if (origin_md) { |
| 1973 | if (dm_hold(origin_md)) |
| 1974 | origin_md = NULL; |
| 1975 | } |
Mikulas Patocka | b735fed | 2015-02-26 11:40:35 -0500 | [diff] [blame] | 1976 | |
Mikulas Patocka | 09ee96b | 2015-02-26 11:41:28 -0500 | [diff] [blame] | 1977 | up_read(&_origins_lock); |
| 1978 | |
| 1979 | if (origin_md) { |
Mikulas Patocka | b735fed | 2015-02-26 11:40:35 -0500 | [diff] [blame] | 1980 | dm_internal_suspend_fast(origin_md); |
Mikulas Patocka | 09ee96b | 2015-02-26 11:41:28 -0500 | [diff] [blame] | 1981 | if (snap_merging && test_bit(RUNNING_MERGE, &snap_merging->state_bits)) { |
| 1982 | must_restart_merging = true; |
| 1983 | stop_merge(snap_merging); |
| 1984 | } |
| 1985 | } |
| 1986 | |
| 1987 | down_read(&_origins_lock); |
Mikulas Patocka | b735fed | 2015-02-26 11:40:35 -0500 | [diff] [blame] | 1988 | |
Mikulas Patocka | 9d3b15c | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 1989 | (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL); |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1990 | if (snap_src && snap_dest) { |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 1991 | down_write(&snap_src->lock); |
| 1992 | down_write_nested(&snap_dest->lock, SINGLE_DEPTH_NESTING); |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1993 | __handover_exceptions(snap_src, snap_dest); |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 1994 | up_write(&snap_dest->lock); |
| 1995 | up_write(&snap_src->lock); |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1996 | } |
Mikulas Patocka | b735fed | 2015-02-26 11:40:35 -0500 | [diff] [blame] | 1997 | |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 1998 | up_read(&_origins_lock); |
| 1999 | |
Mikulas Patocka | 09ee96b | 2015-02-26 11:41:28 -0500 | [diff] [blame] | 2000 | if (origin_md) { |
| 2001 | if (must_restart_merging) |
| 2002 | start_merge(snap_merging); |
| 2003 | dm_internal_resume_fast(origin_md); |
| 2004 | dm_put(origin_md); |
| 2005 | } |
| 2006 | |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 2007 | /* Now we have correct chunk size, reregister */ |
| 2008 | reregister_snapshot(s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2009 | |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 2010 | down_write(&s->lock); |
Alasdair G Kergon | aa14ede | 2006-02-01 03:04:50 -0800 | [diff] [blame] | 2011 | s->active = 1; |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 2012 | up_write(&s->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2013 | } |
| 2014 | |
Mike Snitzer | 542f903 | 2012-07-27 15:08:00 +0100 | [diff] [blame] | 2015 | static uint32_t get_origin_minimum_chunksize(struct block_device *bdev) |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 2016 | { |
Mike Snitzer | 542f903 | 2012-07-27 15:08:00 +0100 | [diff] [blame] | 2017 | uint32_t min_chunksize; |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 2018 | |
| 2019 | down_read(&_origins_lock); |
| 2020 | min_chunksize = __minimum_chunk_size(__lookup_origin(bdev)); |
| 2021 | up_read(&_origins_lock); |
| 2022 | |
| 2023 | return min_chunksize; |
| 2024 | } |
| 2025 | |
| 2026 | static void snapshot_merge_resume(struct dm_target *ti) |
| 2027 | { |
| 2028 | struct dm_snapshot *s = ti->private; |
| 2029 | |
| 2030 | /* |
| 2031 | * Handover exceptions from existing snapshot. |
| 2032 | */ |
| 2033 | snapshot_resume(ti); |
| 2034 | |
| 2035 | /* |
Mike Snitzer | 542f903 | 2012-07-27 15:08:00 +0100 | [diff] [blame] | 2036 | * snapshot-merge acts as an origin, so set ti->max_io_len |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 2037 | */ |
Mike Snitzer | 542f903 | 2012-07-27 15:08:00 +0100 | [diff] [blame] | 2038 | ti->max_io_len = get_origin_minimum_chunksize(s->origin->bdev); |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 2039 | |
| 2040 | start_merge(s); |
| 2041 | } |
| 2042 | |
Mikulas Patocka | fd7c092 | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 2043 | static void snapshot_status(struct dm_target *ti, status_type_t type, |
| 2044 | unsigned status_flags, char *result, unsigned maxlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2045 | { |
Jonathan Brassow | 2e4a31d | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 2046 | unsigned sz = 0; |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 2047 | struct dm_snapshot *snap = ti->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 | |
| 2049 | switch (type) { |
| 2050 | case STATUSTYPE_INFO: |
Mikulas Patocka | 94e76572 | 2009-12-10 23:51:53 +0000 | [diff] [blame] | 2051 | |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 2052 | down_write(&snap->lock); |
Mikulas Patocka | 94e76572 | 2009-12-10 23:51:53 +0000 | [diff] [blame] | 2053 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2054 | if (!snap->valid) |
Jonathan Brassow | 2e4a31d | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 2055 | DMEMIT("Invalid"); |
Mike Snitzer | d8ddb1c | 2009-12-10 23:52:35 +0000 | [diff] [blame] | 2056 | else if (snap->merge_failed) |
| 2057 | DMEMIT("Merge failed"); |
Mikulas Patocka | 76c44f6 | 2015-06-21 16:31:33 -0400 | [diff] [blame] | 2058 | else if (snap->snapshot_overflowed) |
| 2059 | DMEMIT("Overflow"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2060 | else { |
Mike Snitzer | 985903b | 2009-12-10 23:52:11 +0000 | [diff] [blame] | 2061 | if (snap->store->type->usage) { |
| 2062 | sector_t total_sectors, sectors_allocated, |
| 2063 | metadata_sectors; |
| 2064 | snap->store->type->usage(snap->store, |
| 2065 | &total_sectors, |
| 2066 | §ors_allocated, |
| 2067 | &metadata_sectors); |
| 2068 | DMEMIT("%llu/%llu %llu", |
| 2069 | (unsigned long long)sectors_allocated, |
| 2070 | (unsigned long long)total_sectors, |
| 2071 | (unsigned long long)metadata_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2072 | } |
| 2073 | else |
Jonathan Brassow | 2e4a31d | 2009-04-02 19:55:34 +0100 | [diff] [blame] | 2074 | DMEMIT("Unknown"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2075 | } |
Mikulas Patocka | 94e76572 | 2009-12-10 23:51:53 +0000 | [diff] [blame] | 2076 | |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 2077 | up_write(&snap->lock); |
Mikulas Patocka | 94e76572 | 2009-12-10 23:51:53 +0000 | [diff] [blame] | 2078 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | break; |
| 2080 | |
| 2081 | case STATUSTYPE_TABLE: |
| 2082 | /* |
| 2083 | * kdevname returns a static pointer so we need |
| 2084 | * to make private copies if the output is to |
| 2085 | * make sense. |
| 2086 | */ |
Mike Snitzer | fc56f6f | 2009-12-10 23:52:12 +0000 | [diff] [blame] | 2087 | DMEMIT("%s %s", snap->origin->name, snap->cow->name); |
Jonathan Brassow | 1e302a9 | 2009-04-02 19:55:35 +0100 | [diff] [blame] | 2088 | snap->store->type->status(snap->store, type, result + sz, |
| 2089 | maxlen - sz); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2090 | break; |
| 2091 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | } |
| 2093 | |
Mike Snitzer | 8811f46 | 2009-09-04 20:40:19 +0100 | [diff] [blame] | 2094 | static int snapshot_iterate_devices(struct dm_target *ti, |
| 2095 | iterate_devices_callout_fn fn, void *data) |
| 2096 | { |
| 2097 | struct dm_snapshot *snap = ti->private; |
Mikulas Patocka | 1e5554c | 2010-08-12 04:13:50 +0100 | [diff] [blame] | 2098 | int r; |
Mike Snitzer | 8811f46 | 2009-09-04 20:40:19 +0100 | [diff] [blame] | 2099 | |
Mikulas Patocka | 1e5554c | 2010-08-12 04:13:50 +0100 | [diff] [blame] | 2100 | r = fn(ti, snap->origin, 0, ti->len, data); |
| 2101 | |
| 2102 | if (!r) |
| 2103 | r = fn(ti, snap->cow, 0, get_dev_size(snap->cow->bdev), data); |
| 2104 | |
| 2105 | return r; |
Mike Snitzer | 8811f46 | 2009-09-04 20:40:19 +0100 | [diff] [blame] | 2106 | } |
| 2107 | |
| 2108 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2109 | /*----------------------------------------------------------------- |
| 2110 | * Origin methods |
| 2111 | *---------------------------------------------------------------*/ |
Mikulas Patocka | 9eaae8f | 2009-12-10 23:52:28 +0000 | [diff] [blame] | 2112 | |
| 2113 | /* |
| 2114 | * If no exceptions need creating, DM_MAPIO_REMAPPED is returned and any |
| 2115 | * supplied bio was ignored. The caller may submit it immediately. |
| 2116 | * (No remapping actually occurs as the origin is always a direct linear |
| 2117 | * map.) |
| 2118 | * |
| 2119 | * If further exceptions are required, DM_MAPIO_SUBMITTED is returned |
| 2120 | * and any supplied bio is added to a list to be submitted once all |
| 2121 | * the necessary exceptions exist. |
| 2122 | */ |
| 2123 | static int __origin_write(struct list_head *snapshots, sector_t sector, |
| 2124 | struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2125 | { |
Mikulas Patocka | 515ad66 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2126 | int r = DM_MAPIO_REMAPPED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | struct dm_snapshot *snap; |
Jon Brassow | 1d4989c | 2009-12-10 23:52:10 +0000 | [diff] [blame] | 2128 | struct dm_exception *e; |
Nikos Tsironis | 65fc7c3 | 2019-03-17 14:22:55 +0200 | [diff] [blame] | 2129 | struct dm_snap_pending_exception *pe, *pe2; |
Mikulas Patocka | 515ad66 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2130 | struct dm_snap_pending_exception *pe_to_start_now = NULL; |
| 2131 | struct dm_snap_pending_exception *pe_to_start_last = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2132 | chunk_t chunk; |
| 2133 | |
| 2134 | /* Do all the snapshots on this origin */ |
| 2135 | list_for_each_entry (snap, snapshots, list) { |
Mikulas Patocka | 3452c2a | 2009-12-10 23:52:31 +0000 | [diff] [blame] | 2136 | /* |
| 2137 | * Don't make new exceptions in a merging snapshot |
| 2138 | * because it has effectively been deleted |
| 2139 | */ |
| 2140 | if (dm_target_is_snapshot_merge(snap->ti)) |
| 2141 | continue; |
| 2142 | |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 2143 | down_write(&snap->lock); |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 2144 | |
Alasdair G Kergon | aa14ede | 2006-02-01 03:04:50 -0800 | [diff] [blame] | 2145 | /* Only deal with valid and active snapshots */ |
| 2146 | if (!snap->valid || !snap->active) |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 2147 | goto next_snapshot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | |
Alasdair G Kergon | d5e404c | 2005-07-12 15:53:05 -0700 | [diff] [blame] | 2149 | /* Nothing to do if writing beyond end of snapshot */ |
Mikulas Patocka | 9eaae8f | 2009-12-10 23:52:28 +0000 | [diff] [blame] | 2150 | if (sector >= dm_table_get_size(snap->ti->table)) |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 2151 | goto next_snapshot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2152 | |
| 2153 | /* |
| 2154 | * Remember, different snapshots can have |
| 2155 | * different chunk sizes. |
| 2156 | */ |
Mikulas Patocka | 9eaae8f | 2009-12-10 23:52:28 +0000 | [diff] [blame] | 2157 | chunk = sector_to_chunk(snap->store, sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 | |
Mikulas Patocka | 2913808 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 2159 | pe = __lookup_pending_exception(snap, chunk); |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 2160 | if (!pe) { |
Nikos Tsironis | 65fc7c3 | 2019-03-17 14:22:55 +0200 | [diff] [blame] | 2161 | /* |
| 2162 | * Check exception table to see if block is already |
| 2163 | * remapped in this snapshot and trigger an exception |
| 2164 | * if not. |
| 2165 | */ |
| 2166 | e = dm_lookup_exception(&snap->complete, chunk); |
| 2167 | if (e) |
| 2168 | goto next_snapshot; |
| 2169 | |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 2170 | up_write(&snap->lock); |
Mikulas Patocka | c662139 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 2171 | pe = alloc_pending_exception(snap); |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 2172 | down_write(&snap->lock); |
Mikulas Patocka | c662139 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 2173 | |
| 2174 | if (!snap->valid) { |
| 2175 | free_pending_exception(pe); |
| 2176 | goto next_snapshot; |
| 2177 | } |
| 2178 | |
Nikos Tsironis | 65fc7c3 | 2019-03-17 14:22:55 +0200 | [diff] [blame] | 2179 | pe2 = __lookup_pending_exception(snap, chunk); |
Mikulas Patocka | 35bf659 | 2009-04-02 19:55:26 +0100 | [diff] [blame] | 2180 | |
Nikos Tsironis | 65fc7c3 | 2019-03-17 14:22:55 +0200 | [diff] [blame] | 2181 | if (!pe2) { |
| 2182 | e = dm_lookup_exception(&snap->complete, chunk); |
| 2183 | if (e) { |
| 2184 | free_pending_exception(pe); |
| 2185 | goto next_snapshot; |
| 2186 | } |
| 2187 | |
| 2188 | pe = __insert_pending_exception(snap, pe, chunk); |
| 2189 | if (!pe) { |
| 2190 | __invalidate_snapshot(snap, -ENOMEM); |
| 2191 | goto next_snapshot; |
| 2192 | } |
| 2193 | } else { |
| 2194 | free_pending_exception(pe); |
| 2195 | pe = pe2; |
Mikulas Patocka | 2913808 | 2009-04-02 19:55:25 +0100 | [diff] [blame] | 2196 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | } |
| 2198 | |
Mikulas Patocka | 515ad66 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2199 | r = DM_MAPIO_SUBMITTED; |
| 2200 | |
| 2201 | /* |
| 2202 | * If an origin bio was supplied, queue it to wait for the |
| 2203 | * completion of this exception, and start this one last, |
| 2204 | * at the end of the function. |
| 2205 | */ |
| 2206 | if (bio) { |
| 2207 | bio_list_add(&pe->origin_bios, bio); |
| 2208 | bio = NULL; |
| 2209 | |
| 2210 | if (!pe->started) { |
| 2211 | pe->started = 1; |
| 2212 | pe_to_start_last = pe; |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 2213 | } |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 2214 | } |
| 2215 | |
| 2216 | if (!pe->started) { |
| 2217 | pe->started = 1; |
Mikulas Patocka | 515ad66 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2218 | pe_to_start_now = pe; |
Alasdair G Kergon | 76df1c6 | 2006-03-27 01:17:45 -0800 | [diff] [blame] | 2219 | } |
| 2220 | |
Jonathan Brassow | a2d2b03 | 2011-08-02 12:32:03 +0100 | [diff] [blame] | 2221 | next_snapshot: |
Nikos Tsironis | 4ad8d88 | 2019-03-17 14:22:56 +0200 | [diff] [blame^] | 2222 | up_write(&snap->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2223 | |
Mikulas Patocka | 515ad66 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2224 | if (pe_to_start_now) { |
| 2225 | start_copy(pe_to_start_now); |
| 2226 | pe_to_start_now = NULL; |
| 2227 | } |
Alasdair G Kergon | b4b610f | 2006-03-27 01:17:44 -0800 | [diff] [blame] | 2228 | } |
| 2229 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2230 | /* |
Mikulas Patocka | 515ad66 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2231 | * Submit the exception against which the bio is queued last, |
| 2232 | * to give the other exceptions a head start. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2233 | */ |
Mikulas Patocka | 515ad66 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2234 | if (pe_to_start_last) |
| 2235 | start_copy(pe_to_start_last); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2236 | |
| 2237 | return r; |
| 2238 | } |
| 2239 | |
| 2240 | /* |
| 2241 | * Called on a write from the origin driver. |
| 2242 | */ |
| 2243 | static int do_origin(struct dm_dev *origin, struct bio *bio) |
| 2244 | { |
| 2245 | struct origin *o; |
Kiyoshi Ueda | d2a7ad2 | 2006-12-08 02:41:06 -0800 | [diff] [blame] | 2246 | int r = DM_MAPIO_REMAPPED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2247 | |
| 2248 | down_read(&_origins_lock); |
| 2249 | o = __lookup_origin(origin->bdev); |
| 2250 | if (o) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2251 | r = __origin_write(&o->snapshots, bio->bi_iter.bi_sector, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2252 | up_read(&_origins_lock); |
| 2253 | |
| 2254 | return r; |
| 2255 | } |
| 2256 | |
| 2257 | /* |
Mikulas Patocka | 73dfd07 | 2009-12-10 23:52:34 +0000 | [diff] [blame] | 2258 | * Trigger exceptions in all non-merging snapshots. |
| 2259 | * |
| 2260 | * The chunk size of the merging snapshot may be larger than the chunk |
| 2261 | * size of some other snapshot so we may need to reallocate multiple |
| 2262 | * chunks in other snapshots. |
| 2263 | * |
| 2264 | * We scan all the overlapping exceptions in the other snapshots. |
| 2265 | * Returns 1 if anything was reallocated and must be waited for, |
| 2266 | * otherwise returns 0. |
| 2267 | * |
| 2268 | * size must be a multiple of merging_snap's chunk_size. |
| 2269 | */ |
| 2270 | static int origin_write_extent(struct dm_snapshot *merging_snap, |
| 2271 | sector_t sector, unsigned size) |
| 2272 | { |
| 2273 | int must_wait = 0; |
| 2274 | sector_t n; |
| 2275 | struct origin *o; |
| 2276 | |
| 2277 | /* |
Mike Snitzer | 542f903 | 2012-07-27 15:08:00 +0100 | [diff] [blame] | 2278 | * The origin's __minimum_chunk_size() got stored in max_io_len |
Mikulas Patocka | 73dfd07 | 2009-12-10 23:52:34 +0000 | [diff] [blame] | 2279 | * by snapshot_merge_resume(). |
| 2280 | */ |
| 2281 | down_read(&_origins_lock); |
| 2282 | o = __lookup_origin(merging_snap->origin->bdev); |
Mike Snitzer | 542f903 | 2012-07-27 15:08:00 +0100 | [diff] [blame] | 2283 | for (n = 0; n < size; n += merging_snap->ti->max_io_len) |
Mikulas Patocka | 73dfd07 | 2009-12-10 23:52:34 +0000 | [diff] [blame] | 2284 | if (__origin_write(&o->snapshots, sector + n, NULL) == |
| 2285 | DM_MAPIO_SUBMITTED) |
| 2286 | must_wait = 1; |
| 2287 | up_read(&_origins_lock); |
| 2288 | |
| 2289 | return must_wait; |
| 2290 | } |
| 2291 | |
| 2292 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2293 | * Origin: maps a linear range of a device, with hooks for snapshotting. |
| 2294 | */ |
| 2295 | |
| 2296 | /* |
| 2297 | * Construct an origin mapping: <dev_path> |
| 2298 | * The context for an origin is merely a 'struct dm_dev *' |
| 2299 | * pointing to the real device. |
| 2300 | */ |
| 2301 | static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv) |
| 2302 | { |
| 2303 | int r; |
Mikulas Patocka | 599cdf3 | 2014-03-14 18:42:12 -0400 | [diff] [blame] | 2304 | struct dm_origin *o; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2305 | |
| 2306 | if (argc != 1) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 2307 | ti->error = "origin: incorrect number of arguments"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2308 | return -EINVAL; |
| 2309 | } |
| 2310 | |
Mikulas Patocka | 599cdf3 | 2014-03-14 18:42:12 -0400 | [diff] [blame] | 2311 | o = kmalloc(sizeof(struct dm_origin), GFP_KERNEL); |
| 2312 | if (!o) { |
| 2313 | ti->error = "Cannot allocate private origin structure"; |
| 2314 | r = -ENOMEM; |
| 2315 | goto bad_alloc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2316 | } |
| 2317 | |
Mikulas Patocka | 599cdf3 | 2014-03-14 18:42:12 -0400 | [diff] [blame] | 2318 | r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &o->dev); |
| 2319 | if (r) { |
| 2320 | ti->error = "Cannot get target device"; |
| 2321 | goto bad_open; |
| 2322 | } |
| 2323 | |
Mikulas Patocka | b735fed | 2015-02-26 11:40:35 -0500 | [diff] [blame] | 2324 | o->ti = ti; |
Mikulas Patocka | 599cdf3 | 2014-03-14 18:42:12 -0400 | [diff] [blame] | 2325 | ti->private = o; |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 2326 | ti->num_flush_bios = 1; |
Mikulas Patocka | 494b3ee | 2009-06-22 10:12:25 +0100 | [diff] [blame] | 2327 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2328 | return 0; |
Mikulas Patocka | 599cdf3 | 2014-03-14 18:42:12 -0400 | [diff] [blame] | 2329 | |
| 2330 | bad_open: |
| 2331 | kfree(o); |
| 2332 | bad_alloc: |
| 2333 | return r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2334 | } |
| 2335 | |
| 2336 | static void origin_dtr(struct dm_target *ti) |
| 2337 | { |
Mikulas Patocka | 599cdf3 | 2014-03-14 18:42:12 -0400 | [diff] [blame] | 2338 | struct dm_origin *o = ti->private; |
Mikulas Patocka | b735fed | 2015-02-26 11:40:35 -0500 | [diff] [blame] | 2339 | |
Mikulas Patocka | 599cdf3 | 2014-03-14 18:42:12 -0400 | [diff] [blame] | 2340 | dm_put_device(ti, o->dev); |
| 2341 | kfree(o); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2342 | } |
| 2343 | |
Mikulas Patocka | 7de3ee5 | 2012-12-21 20:23:41 +0000 | [diff] [blame] | 2344 | static int origin_map(struct dm_target *ti, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2345 | { |
Mikulas Patocka | 599cdf3 | 2014-03-14 18:42:12 -0400 | [diff] [blame] | 2346 | struct dm_origin *o = ti->private; |
Mikulas Patocka | 298eaa8 | 2014-03-14 18:43:07 -0400 | [diff] [blame] | 2347 | unsigned available_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2348 | |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 2349 | bio_set_dev(bio, o->dev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2350 | |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 2351 | if (unlikely(bio->bi_opf & REQ_PREFLUSH)) |
Mikulas Patocka | 494b3ee | 2009-06-22 10:12:25 +0100 | [diff] [blame] | 2352 | return DM_MAPIO_REMAPPED; |
| 2353 | |
Christoph Hellwig | 7024628 | 2016-07-19 11:28:41 +0200 | [diff] [blame] | 2354 | if (bio_data_dir(bio) != WRITE) |
Mikulas Patocka | 298eaa8 | 2014-03-14 18:43:07 -0400 | [diff] [blame] | 2355 | return DM_MAPIO_REMAPPED; |
| 2356 | |
| 2357 | available_sectors = o->split_boundary - |
| 2358 | ((unsigned)bio->bi_iter.bi_sector & (o->split_boundary - 1)); |
| 2359 | |
| 2360 | if (bio_sectors(bio) > available_sectors) |
| 2361 | dm_accept_partial_bio(bio, available_sectors); |
| 2362 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | /* Only tell snapshots if this is a write */ |
Mikulas Patocka | 298eaa8 | 2014-03-14 18:43:07 -0400 | [diff] [blame] | 2364 | return do_origin(o->dev, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2365 | } |
| 2366 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2367 | /* |
Mike Snitzer | 542f903 | 2012-07-27 15:08:00 +0100 | [diff] [blame] | 2368 | * Set the target "max_io_len" field to the minimum of all the snapshots' |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2369 | * chunk sizes. |
| 2370 | */ |
| 2371 | static void origin_resume(struct dm_target *ti) |
| 2372 | { |
Mikulas Patocka | 599cdf3 | 2014-03-14 18:42:12 -0400 | [diff] [blame] | 2373 | struct dm_origin *o = ti->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2374 | |
Mikulas Patocka | 298eaa8 | 2014-03-14 18:43:07 -0400 | [diff] [blame] | 2375 | o->split_boundary = get_origin_minimum_chunksize(o->dev->bdev); |
Mikulas Patocka | b735fed | 2015-02-26 11:40:35 -0500 | [diff] [blame] | 2376 | |
| 2377 | down_write(&_origins_lock); |
| 2378 | __insert_dm_origin(o); |
| 2379 | up_write(&_origins_lock); |
| 2380 | } |
| 2381 | |
| 2382 | static void origin_postsuspend(struct dm_target *ti) |
| 2383 | { |
| 2384 | struct dm_origin *o = ti->private; |
| 2385 | |
| 2386 | down_write(&_origins_lock); |
| 2387 | __remove_dm_origin(o); |
| 2388 | up_write(&_origins_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2389 | } |
| 2390 | |
Mikulas Patocka | fd7c092 | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 2391 | static void origin_status(struct dm_target *ti, status_type_t type, |
| 2392 | unsigned status_flags, char *result, unsigned maxlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2393 | { |
Mikulas Patocka | 599cdf3 | 2014-03-14 18:42:12 -0400 | [diff] [blame] | 2394 | struct dm_origin *o = ti->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2395 | |
| 2396 | switch (type) { |
| 2397 | case STATUSTYPE_INFO: |
| 2398 | result[0] = '\0'; |
| 2399 | break; |
| 2400 | |
| 2401 | case STATUSTYPE_TABLE: |
Mikulas Patocka | 599cdf3 | 2014-03-14 18:42:12 -0400 | [diff] [blame] | 2402 | snprintf(result, maxlen, "%s", o->dev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2403 | break; |
| 2404 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2405 | } |
| 2406 | |
Mike Snitzer | 8811f46 | 2009-09-04 20:40:19 +0100 | [diff] [blame] | 2407 | static int origin_iterate_devices(struct dm_target *ti, |
| 2408 | iterate_devices_callout_fn fn, void *data) |
| 2409 | { |
Mikulas Patocka | 599cdf3 | 2014-03-14 18:42:12 -0400 | [diff] [blame] | 2410 | struct dm_origin *o = ti->private; |
Mike Snitzer | 8811f46 | 2009-09-04 20:40:19 +0100 | [diff] [blame] | 2411 | |
Mikulas Patocka | 599cdf3 | 2014-03-14 18:42:12 -0400 | [diff] [blame] | 2412 | return fn(ti, o->dev, 0, ti->len, data); |
Mike Snitzer | 8811f46 | 2009-09-04 20:40:19 +0100 | [diff] [blame] | 2413 | } |
| 2414 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2415 | static struct target_type origin_target = { |
| 2416 | .name = "snapshot-origin", |
Mikulas Patocka | b735fed | 2015-02-26 11:40:35 -0500 | [diff] [blame] | 2417 | .version = {1, 9, 0}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2418 | .module = THIS_MODULE, |
| 2419 | .ctr = origin_ctr, |
| 2420 | .dtr = origin_dtr, |
| 2421 | .map = origin_map, |
| 2422 | .resume = origin_resume, |
Mikulas Patocka | b735fed | 2015-02-26 11:40:35 -0500 | [diff] [blame] | 2423 | .postsuspend = origin_postsuspend, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2424 | .status = origin_status, |
Mike Snitzer | 8811f46 | 2009-09-04 20:40:19 +0100 | [diff] [blame] | 2425 | .iterate_devices = origin_iterate_devices, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2426 | }; |
| 2427 | |
| 2428 | static struct target_type snapshot_target = { |
| 2429 | .name = "snapshot", |
Mike Snitzer | b0d3cc0 | 2015-10-08 18:05:41 -0400 | [diff] [blame] | 2430 | .version = {1, 15, 0}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2431 | .module = THIS_MODULE, |
| 2432 | .ctr = snapshot_ctr, |
| 2433 | .dtr = snapshot_dtr, |
| 2434 | .map = snapshot_map, |
Mikulas Patocka | cd45daf | 2008-07-21 12:00:32 +0100 | [diff] [blame] | 2435 | .end_io = snapshot_end_io, |
Mike Snitzer | c1f0c18 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 2436 | .preresume = snapshot_preresume, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2437 | .resume = snapshot_resume, |
| 2438 | .status = snapshot_status, |
Mike Snitzer | 8811f46 | 2009-09-04 20:40:19 +0100 | [diff] [blame] | 2439 | .iterate_devices = snapshot_iterate_devices, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2440 | }; |
| 2441 | |
Mikulas Patocka | d698aa4 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2442 | static struct target_type merge_target = { |
| 2443 | .name = dm_snapshot_merge_target_name, |
Mike Snitzer | b0d3cc0 | 2015-10-08 18:05:41 -0400 | [diff] [blame] | 2444 | .version = {1, 4, 0}, |
Mikulas Patocka | d698aa4 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2445 | .module = THIS_MODULE, |
| 2446 | .ctr = snapshot_ctr, |
| 2447 | .dtr = snapshot_dtr, |
Mikulas Patocka | 3452c2a | 2009-12-10 23:52:31 +0000 | [diff] [blame] | 2448 | .map = snapshot_merge_map, |
Mikulas Patocka | d698aa4 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2449 | .end_io = snapshot_end_io, |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 2450 | .presuspend = snapshot_merge_presuspend, |
Mikulas Patocka | d698aa4 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2451 | .preresume = snapshot_preresume, |
Mikulas Patocka | 1e03f97 | 2009-12-10 23:52:32 +0000 | [diff] [blame] | 2452 | .resume = snapshot_merge_resume, |
Mikulas Patocka | d698aa4 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2453 | .status = snapshot_status, |
| 2454 | .iterate_devices = snapshot_iterate_devices, |
| 2455 | }; |
| 2456 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2457 | static int __init dm_snapshot_init(void) |
| 2458 | { |
| 2459 | int r; |
| 2460 | |
Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 2461 | r = dm_exception_store_init(); |
| 2462 | if (r) { |
| 2463 | DMERR("Failed to initialize exception stores"); |
| 2464 | return r; |
| 2465 | } |
| 2466 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2467 | r = init_origin_hash(); |
| 2468 | if (r) { |
| 2469 | DMERR("init_origin_hash failed."); |
Mikulas Patocka | d698aa4 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2470 | goto bad_origin_hash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2471 | } |
| 2472 | |
Jon Brassow | 1d4989c | 2009-12-10 23:52:10 +0000 | [diff] [blame] | 2473 | exception_cache = KMEM_CACHE(dm_exception, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2474 | if (!exception_cache) { |
| 2475 | DMERR("Couldn't create exception cache."); |
| 2476 | r = -ENOMEM; |
Mikulas Patocka | d698aa4 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2477 | goto bad_exception_cache; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2478 | } |
| 2479 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 2480 | pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2481 | if (!pending_cache) { |
| 2482 | DMERR("Couldn't create pending cache."); |
| 2483 | r = -ENOMEM; |
Mikulas Patocka | d698aa4 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2484 | goto bad_pending_cache; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2485 | } |
| 2486 | |
monty_pavel@sina.com | 7e6358d | 2017-11-25 01:43:50 +0800 | [diff] [blame] | 2487 | r = dm_register_target(&snapshot_target); |
| 2488 | if (r < 0) { |
| 2489 | DMERR("snapshot target register failed %d", r); |
| 2490 | goto bad_register_snapshot_target; |
| 2491 | } |
| 2492 | |
| 2493 | r = dm_register_target(&origin_target); |
| 2494 | if (r < 0) { |
| 2495 | DMERR("Origin target register failed %d", r); |
| 2496 | goto bad_register_origin_target; |
| 2497 | } |
| 2498 | |
| 2499 | r = dm_register_target(&merge_target); |
| 2500 | if (r < 0) { |
| 2501 | DMERR("Merge target register failed %d", r); |
| 2502 | goto bad_register_merge_target; |
| 2503 | } |
| 2504 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2505 | return 0; |
| 2506 | |
Mikulas Patocka | d698aa4 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2507 | bad_register_merge_target: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2508 | dm_unregister_target(&origin_target); |
Mikulas Patocka | d698aa4 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2509 | bad_register_origin_target: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2510 | dm_unregister_target(&snapshot_target); |
Jonathan Brassow | 034a186 | 2009-10-16 23:18:14 +0100 | [diff] [blame] | 2511 | bad_register_snapshot_target: |
monty_pavel@sina.com | 7e6358d | 2017-11-25 01:43:50 +0800 | [diff] [blame] | 2512 | kmem_cache_destroy(pending_cache); |
| 2513 | bad_pending_cache: |
| 2514 | kmem_cache_destroy(exception_cache); |
| 2515 | bad_exception_cache: |
| 2516 | exit_origin_hash(); |
| 2517 | bad_origin_hash: |
Jonathan Brassow | 034a186 | 2009-10-16 23:18:14 +0100 | [diff] [blame] | 2518 | dm_exception_store_exit(); |
Mikulas Patocka | d698aa4 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2520 | return r; |
| 2521 | } |
| 2522 | |
| 2523 | static void __exit dm_snapshot_exit(void) |
| 2524 | { |
Mikulas Patocka | 10d3bd0 | 2009-01-06 03:04:58 +0000 | [diff] [blame] | 2525 | dm_unregister_target(&snapshot_target); |
| 2526 | dm_unregister_target(&origin_target); |
Mikulas Patocka | d698aa4 | 2009-12-10 23:52:30 +0000 | [diff] [blame] | 2527 | dm_unregister_target(&merge_target); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2528 | |
| 2529 | exit_origin_hash(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2530 | kmem_cache_destroy(pending_cache); |
| 2531 | kmem_cache_destroy(exception_cache); |
Alasdair G Kergon | 4db6bfe | 2009-01-06 03:05:17 +0000 | [diff] [blame] | 2532 | |
| 2533 | dm_exception_store_exit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2534 | } |
| 2535 | |
| 2536 | /* Module hooks */ |
| 2537 | module_init(dm_snapshot_init); |
| 2538 | module_exit(dm_snapshot_exit); |
| 2539 | |
| 2540 | MODULE_DESCRIPTION(DM_NAME " snapshot target"); |
| 2541 | MODULE_AUTHOR("Joe Thornber"); |
| 2542 | MODULE_LICENSE("GPL"); |
Mikulas Patocka | 23cb210 | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 2543 | MODULE_ALIAS("dm-snapshot-origin"); |
| 2544 | MODULE_ALIAS("dm-snapshot-merge"); |