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