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