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