1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2019 Arrikto, Inc. All Rights Reserved.
9 #include <linux/hash.h>
10 #include <linux/list.h>
11 #include <linux/log2.h>
12 #include <linux/init.h>
13 #include <linux/slab.h>
14 #include <linux/wait.h>
15 #include <linux/dm-io.h>
16 #include <linux/mutex.h>
17 #include <linux/atomic.h>
18 #include <linux/bitops.h>
19 #include <linux/blkdev.h>
20 #include <linux/kdev_t.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/jiffies.h>
24 #include <linux/mempool.h>
25 #include <linux/spinlock.h>
26 #include <linux/blk_types.h>
27 #include <linux/dm-kcopyd.h>
28 #include <linux/workqueue.h>
29 #include <linux/backing-dev.h>
30 #include <linux/device-mapper.h>
33 #include "dm-clone-metadata.h"
35 #define DM_MSG_PREFIX "clone"
38 * Minimum and maximum allowed region sizes
40 #define MIN_REGION_SIZE (1 << 3) /* 4KB */
41 #define MAX_REGION_SIZE (1 << 21) /* 1GB */
43 #define MIN_HYDRATIONS 256 /* Size of hydration mempool */
44 #define DEFAULT_HYDRATION_THRESHOLD 1 /* 1 region */
45 #define DEFAULT_HYDRATION_BATCH_SIZE 1 /* Hydrate in batches of 1 region */
47 #define COMMIT_PERIOD HZ /* 1 sec */
50 * Hydration hash table size: 1 << HASH_TABLE_BITS
52 #define HASH_TABLE_BITS 15
54 DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(clone_hydration_throttle,
55 "A percentage of time allocated for hydrating regions");
57 /* Slab cache for struct dm_clone_region_hydration */
58 static struct kmem_cache *_hydration_cache;
60 /* dm-clone metadata modes */
61 enum clone_metadata_mode {
62 CM_WRITE, /* metadata may be changed */
63 CM_READ_ONLY, /* metadata may not be changed */
64 CM_FAIL, /* all metadata I/O fails */
67 struct hash_table_bucket;
71 struct dm_target_callbacks callbacks;
73 struct dm_dev *metadata_dev;
74 struct dm_dev *dest_dev;
75 struct dm_dev *source_dev;
77 unsigned long nr_regions;
79 unsigned int region_shift;
82 * A metadata commit and the actions taken in case it fails should run
83 * as a single atomic step.
85 struct mutex commit_lock;
87 struct dm_clone_metadata *cmd;
89 /* Region hydration hash table */
90 struct hash_table_bucket *ht;
92 atomic_t ios_in_flight;
94 wait_queue_head_t hydration_stopped;
96 mempool_t hydration_pool;
98 unsigned long last_commit_jiffies;
101 * We defer incoming WRITE bios for regions that are not hydrated,
102 * until after these regions have been hydrated.
104 * Also, we defer REQ_FUA and REQ_PREFLUSH bios, until after the
105 * metadata have been committed.
108 struct bio_list deferred_bios;
109 struct bio_list deferred_discard_bios;
110 struct bio_list deferred_flush_bios;
111 struct bio_list deferred_flush_completions;
113 /* Maximum number of regions being copied during background hydration. */
114 unsigned int hydration_threshold;
116 /* Number of regions to batch together during background hydration. */
117 unsigned int hydration_batch_size;
119 /* Which region to hydrate next */
120 unsigned long hydration_offset;
122 atomic_t hydrations_in_flight;
125 * Save a copy of the table line rather than reconstructing it for the
128 unsigned int nr_ctr_args;
129 const char **ctr_args;
131 struct workqueue_struct *wq;
132 struct work_struct worker;
133 struct delayed_work waker;
135 struct dm_kcopyd_client *kcopyd_client;
137 enum clone_metadata_mode mode;
144 #define DM_CLONE_DISCARD_PASSDOWN 0
145 #define DM_CLONE_HYDRATION_ENABLED 1
146 #define DM_CLONE_HYDRATION_SUSPENDED 2
148 /*---------------------------------------------------------------------------*/
151 * Metadata failure handling.
153 static enum clone_metadata_mode get_clone_mode(struct clone *clone)
155 return READ_ONCE(clone->mode);
158 static const char *clone_device_name(struct clone *clone)
160 return dm_table_device_name(clone->ti->table);
163 static void __set_clone_mode(struct clone *clone, enum clone_metadata_mode new_mode)
165 const char *descs[] = {
171 enum clone_metadata_mode old_mode = get_clone_mode(clone);
173 /* Never move out of fail mode */
174 if (old_mode == CM_FAIL)
180 dm_clone_metadata_set_read_only(clone->cmd);
184 dm_clone_metadata_set_read_write(clone->cmd);
188 WRITE_ONCE(clone->mode, new_mode);
190 if (new_mode != old_mode) {
191 dm_table_event(clone->ti->table);
192 DMINFO("%s: Switching to %s mode", clone_device_name(clone),
193 descs[(int)new_mode]);
197 static void __abort_transaction(struct clone *clone)
199 const char *dev_name = clone_device_name(clone);
201 if (get_clone_mode(clone) >= CM_READ_ONLY)
204 DMERR("%s: Aborting current metadata transaction", dev_name);
205 if (dm_clone_metadata_abort(clone->cmd)) {
206 DMERR("%s: Failed to abort metadata transaction", dev_name);
207 __set_clone_mode(clone, CM_FAIL);
211 static void __reload_in_core_bitset(struct clone *clone)
213 const char *dev_name = clone_device_name(clone);
215 if (get_clone_mode(clone) == CM_FAIL)
218 /* Reload the on-disk bitset */
219 DMINFO("%s: Reloading on-disk bitmap", dev_name);
220 if (dm_clone_reload_in_core_bitset(clone->cmd)) {
221 DMERR("%s: Failed to reload on-disk bitmap", dev_name);
222 __set_clone_mode(clone, CM_FAIL);
226 static void __metadata_operation_failed(struct clone *clone, const char *op, int r)
228 DMERR("%s: Metadata operation `%s' failed: error = %d",
229 clone_device_name(clone), op, r);
231 __abort_transaction(clone);
232 __set_clone_mode(clone, CM_READ_ONLY);
235 * dm_clone_reload_in_core_bitset() may run concurrently with either
236 * dm_clone_set_region_hydrated() or dm_clone_cond_set_range(), but
237 * it's safe as we have already set the metadata to read-only mode.
239 __reload_in_core_bitset(clone);
242 /*---------------------------------------------------------------------------*/
244 /* Wake up anyone waiting for region hydrations to stop */
245 static inline void wakeup_hydration_waiters(struct clone *clone)
247 wake_up_all(&clone->hydration_stopped);
250 static inline void wake_worker(struct clone *clone)
252 queue_work(clone->wq, &clone->worker);
255 /*---------------------------------------------------------------------------*/
258 * bio helper functions.
260 static inline void remap_to_source(struct clone *clone, struct bio *bio)
262 bio_set_dev(bio, clone->source_dev->bdev);
265 static inline void remap_to_dest(struct clone *clone, struct bio *bio)
267 bio_set_dev(bio, clone->dest_dev->bdev);
270 static bool bio_triggers_commit(struct clone *clone, struct bio *bio)
272 return op_is_flush(bio->bi_opf) &&
273 dm_clone_changed_this_transaction(clone->cmd);
276 /* Get the address of the region in sectors */
277 static inline sector_t region_to_sector(struct clone *clone, unsigned long region_nr)
279 return (region_nr << clone->region_shift);
282 /* Get the region number of the bio */
283 static inline unsigned long bio_to_region(struct clone *clone, struct bio *bio)
285 return (bio->bi_iter.bi_sector >> clone->region_shift);
288 /* Get the region range covered by the bio */
289 static void bio_region_range(struct clone *clone, struct bio *bio,
290 unsigned long *rs, unsigned long *re)
292 *rs = dm_sector_div_up(bio->bi_iter.bi_sector, clone->region_size);
293 *re = bio_end_sector(bio) >> clone->region_shift;
296 /* Check whether a bio overwrites a region */
297 static inline bool is_overwrite_bio(struct clone *clone, struct bio *bio)
299 return (bio_data_dir(bio) == WRITE && bio_sectors(bio) == clone->region_size);
302 static void fail_bios(struct bio_list *bios, blk_status_t status)
306 while ((bio = bio_list_pop(bios))) {
307 bio->bi_status = status;
312 static void submit_bios(struct bio_list *bios)
315 struct blk_plug plug;
317 blk_start_plug(&plug);
319 while ((bio = bio_list_pop(bios)))
320 generic_make_request(bio);
322 blk_finish_plug(&plug);
326 * Submit bio to the underlying device.
328 * If the bio triggers a commit, delay it, until after the metadata have been
331 * NOTE: The bio remapping must be performed by the caller.
333 static void issue_bio(struct clone *clone, struct bio *bio)
335 if (!bio_triggers_commit(clone, bio)) {
336 generic_make_request(bio);
341 * If the metadata mode is RO or FAIL we won't be able to commit the
342 * metadata, so we complete the bio with an error.
344 if (unlikely(get_clone_mode(clone) >= CM_READ_ONLY)) {
350 * Batch together any bios that trigger commits and then issue a single
351 * commit for them in process_deferred_flush_bios().
353 spin_lock_irq(&clone->lock);
354 bio_list_add(&clone->deferred_flush_bios, bio);
355 spin_unlock_irq(&clone->lock);
361 * Remap bio to the destination device and submit it.
363 * If the bio triggers a commit, delay it, until after the metadata have been
366 static void remap_and_issue(struct clone *clone, struct bio *bio)
368 remap_to_dest(clone, bio);
369 issue_bio(clone, bio);
373 * Issue bios that have been deferred until after their region has finished
376 * We delegate the bio submission to the worker thread, so this is safe to call
377 * from interrupt context.
379 static void issue_deferred_bios(struct clone *clone, struct bio_list *bios)
383 struct bio_list flush_bios = BIO_EMPTY_LIST;
384 struct bio_list normal_bios = BIO_EMPTY_LIST;
386 if (bio_list_empty(bios))
389 while ((bio = bio_list_pop(bios))) {
390 if (bio_triggers_commit(clone, bio))
391 bio_list_add(&flush_bios, bio);
393 bio_list_add(&normal_bios, bio);
396 spin_lock_irqsave(&clone->lock, flags);
397 bio_list_merge(&clone->deferred_bios, &normal_bios);
398 bio_list_merge(&clone->deferred_flush_bios, &flush_bios);
399 spin_unlock_irqrestore(&clone->lock, flags);
404 static void complete_overwrite_bio(struct clone *clone, struct bio *bio)
409 * If the bio has the REQ_FUA flag set we must commit the metadata
410 * before signaling its completion.
412 * complete_overwrite_bio() is only called by hydration_complete(),
413 * after having successfully updated the metadata. This means we don't
414 * need to call dm_clone_changed_this_transaction() to check if the
415 * metadata has changed and thus we can avoid taking the metadata spin
418 if (!(bio->bi_opf & REQ_FUA)) {
424 * If the metadata mode is RO or FAIL we won't be able to commit the
425 * metadata, so we complete the bio with an error.
427 if (unlikely(get_clone_mode(clone) >= CM_READ_ONLY)) {
433 * Batch together any bios that trigger commits and then issue a single
434 * commit for them in process_deferred_flush_bios().
436 spin_lock_irqsave(&clone->lock, flags);
437 bio_list_add(&clone->deferred_flush_completions, bio);
438 spin_unlock_irqrestore(&clone->lock, flags);
443 static void trim_bio(struct bio *bio, sector_t sector, unsigned int len)
445 bio->bi_iter.bi_sector = sector;
446 bio->bi_iter.bi_size = to_bytes(len);
449 static void complete_discard_bio(struct clone *clone, struct bio *bio, bool success)
451 unsigned long rs, re;
454 * If the destination device supports discards, remap and trim the
455 * discard bio and pass it down. Otherwise complete the bio
458 if (test_bit(DM_CLONE_DISCARD_PASSDOWN, &clone->flags) && success) {
459 remap_to_dest(clone, bio);
460 bio_region_range(clone, bio, &rs, &re);
461 trim_bio(bio, rs << clone->region_shift,
462 (re - rs) << clone->region_shift);
463 generic_make_request(bio);
468 static void process_discard_bio(struct clone *clone, struct bio *bio)
470 unsigned long rs, re;
472 bio_region_range(clone, bio, &rs, &re);
473 BUG_ON(re > clone->nr_regions);
475 if (unlikely(rs == re)) {
481 * The covered regions are already hydrated so we just need to pass
484 if (dm_clone_is_range_hydrated(clone->cmd, rs, re - rs)) {
485 complete_discard_bio(clone, bio, true);
490 * If the metadata mode is RO or FAIL we won't be able to update the
491 * metadata for the regions covered by the discard so we just ignore
494 if (unlikely(get_clone_mode(clone) >= CM_READ_ONLY)) {
500 * Defer discard processing.
502 spin_lock_irq(&clone->lock);
503 bio_list_add(&clone->deferred_discard_bios, bio);
504 spin_unlock_irq(&clone->lock);
509 /*---------------------------------------------------------------------------*/
512 * dm-clone region hydrations.
514 struct dm_clone_region_hydration {
516 unsigned long region_nr;
518 struct bio *overwrite_bio;
519 bio_end_io_t *overwrite_bio_end_io;
521 struct bio_list deferred_bios;
525 /* Used by hydration batching */
526 struct list_head list;
528 /* Used by hydration hash table */
533 * Hydration hash table implementation.
535 * Ideally we would like to use list_bl, which uses bit spin locks and employs
536 * the least significant bit of the list head to lock the corresponding bucket,
537 * reducing the memory overhead for the locks. But, currently, list_bl and bit
538 * spin locks don't support IRQ safe versions. Since we have to take the lock
539 * in both process and interrupt context, we must fall back to using regular
540 * spin locks; one per hash table bucket.
542 struct hash_table_bucket {
543 struct hlist_head head;
545 /* Spinlock protecting the bucket */
549 #define bucket_lock_irqsave(bucket, flags) \
550 spin_lock_irqsave(&(bucket)->lock, flags)
552 #define bucket_unlock_irqrestore(bucket, flags) \
553 spin_unlock_irqrestore(&(bucket)->lock, flags)
555 #define bucket_lock_irq(bucket) \
556 spin_lock_irq(&(bucket)->lock)
558 #define bucket_unlock_irq(bucket) \
559 spin_unlock_irq(&(bucket)->lock)
561 static int hash_table_init(struct clone *clone)
564 struct hash_table_bucket *bucket;
566 sz = 1 << HASH_TABLE_BITS;
568 clone->ht = kvmalloc(sz * sizeof(struct hash_table_bucket), GFP_KERNEL);
572 for (i = 0; i < sz; i++) {
573 bucket = clone->ht + i;
575 INIT_HLIST_HEAD(&bucket->head);
576 spin_lock_init(&bucket->lock);
582 static void hash_table_exit(struct clone *clone)
587 static struct hash_table_bucket *get_hash_table_bucket(struct clone *clone,
588 unsigned long region_nr)
590 return &clone->ht[hash_long(region_nr, HASH_TABLE_BITS)];
594 * Search hash table for a hydration with hd->region_nr == region_nr
596 * NOTE: Must be called with the bucket lock held
598 static struct dm_clone_region_hydration *__hash_find(struct hash_table_bucket *bucket,
599 unsigned long region_nr)
601 struct dm_clone_region_hydration *hd;
603 hlist_for_each_entry(hd, &bucket->head, h) {
604 if (hd->region_nr == region_nr)
612 * Insert a hydration into the hash table.
614 * NOTE: Must be called with the bucket lock held.
616 static inline void __insert_region_hydration(struct hash_table_bucket *bucket,
617 struct dm_clone_region_hydration *hd)
619 hlist_add_head(&hd->h, &bucket->head);
623 * This function inserts a hydration into the hash table, unless someone else
624 * managed to insert a hydration for the same region first. In the latter case
625 * it returns the existing hydration descriptor for this region.
627 * NOTE: Must be called with the hydration hash table lock held.
629 static struct dm_clone_region_hydration *
630 __find_or_insert_region_hydration(struct hash_table_bucket *bucket,
631 struct dm_clone_region_hydration *hd)
633 struct dm_clone_region_hydration *hd2;
635 hd2 = __hash_find(bucket, hd->region_nr);
639 __insert_region_hydration(bucket, hd);
644 /*---------------------------------------------------------------------------*/
646 /* Allocate a hydration */
647 static struct dm_clone_region_hydration *alloc_hydration(struct clone *clone)
649 struct dm_clone_region_hydration *hd;
652 * Allocate a hydration from the hydration mempool.
653 * This might block but it can't fail.
655 hd = mempool_alloc(&clone->hydration_pool, GFP_NOIO);
661 static inline void free_hydration(struct dm_clone_region_hydration *hd)
663 mempool_free(hd, &hd->clone->hydration_pool);
666 /* Initialize a hydration */
667 static void hydration_init(struct dm_clone_region_hydration *hd, unsigned long region_nr)
669 hd->region_nr = region_nr;
670 hd->overwrite_bio = NULL;
671 bio_list_init(&hd->deferred_bios);
674 INIT_LIST_HEAD(&hd->list);
675 INIT_HLIST_NODE(&hd->h);
678 /*---------------------------------------------------------------------------*/
681 * Update dm-clone's metadata after a region has finished hydrating and remove
682 * hydration from the hash table.
684 static int hydration_update_metadata(struct dm_clone_region_hydration *hd)
688 struct hash_table_bucket *bucket;
689 struct clone *clone = hd->clone;
691 if (unlikely(get_clone_mode(clone) >= CM_READ_ONLY))
694 /* Update the metadata */
695 if (likely(!r) && hd->status == BLK_STS_OK)
696 r = dm_clone_set_region_hydrated(clone->cmd, hd->region_nr);
698 bucket = get_hash_table_bucket(clone, hd->region_nr);
700 /* Remove hydration from hash table */
701 bucket_lock_irqsave(bucket, flags);
703 bucket_unlock_irqrestore(bucket, flags);
709 * Complete a region's hydration:
711 * 1. Update dm-clone's metadata.
712 * 2. Remove hydration from hash table.
713 * 3. Complete overwrite bio.
714 * 4. Issue deferred bios.
715 * 5. If this was the last hydration, wake up anyone waiting for
716 * hydrations to finish.
718 static void hydration_complete(struct dm_clone_region_hydration *hd)
722 struct clone *clone = hd->clone;
724 r = hydration_update_metadata(hd);
726 if (hd->status == BLK_STS_OK && likely(!r)) {
727 if (hd->overwrite_bio)
728 complete_overwrite_bio(clone, hd->overwrite_bio);
730 issue_deferred_bios(clone, &hd->deferred_bios);
732 status = r ? BLK_STS_IOERR : hd->status;
734 if (hd->overwrite_bio)
735 bio_list_add(&hd->deferred_bios, hd->overwrite_bio);
737 fail_bios(&hd->deferred_bios, status);
742 if (atomic_dec_and_test(&clone->hydrations_in_flight))
743 wakeup_hydration_waiters(clone);
746 static void hydration_kcopyd_callback(int read_err, unsigned long write_err, void *context)
750 struct dm_clone_region_hydration *tmp, *hd = context;
751 struct clone *clone = hd->clone;
753 LIST_HEAD(batched_hydrations);
755 if (read_err || write_err) {
756 DMERR_LIMIT("%s: hydration failed", clone_device_name(clone));
757 status = BLK_STS_IOERR;
761 list_splice_tail(&hd->list, &batched_hydrations);
764 hydration_complete(hd);
766 /* Complete batched hydrations */
767 list_for_each_entry_safe(hd, tmp, &batched_hydrations, list) {
769 hydration_complete(hd);
772 /* Continue background hydration, if there is no I/O in-flight */
773 if (test_bit(DM_CLONE_HYDRATION_ENABLED, &clone->flags) &&
774 !atomic_read(&clone->ios_in_flight))
778 static void hydration_copy(struct dm_clone_region_hydration *hd, unsigned int nr_regions)
780 unsigned long region_start, region_end;
781 sector_t tail_size, region_size, total_size;
782 struct dm_io_region from, to;
783 struct clone *clone = hd->clone;
785 region_size = clone->region_size;
786 region_start = hd->region_nr;
787 region_end = region_start + nr_regions - 1;
789 total_size = (nr_regions - 1) << clone->region_shift;
791 if (region_end == clone->nr_regions - 1) {
793 * The last region of the target might be smaller than
796 tail_size = clone->ti->len & (region_size - 1);
798 tail_size = region_size;
800 tail_size = region_size;
803 total_size += tail_size;
805 from.bdev = clone->source_dev->bdev;
806 from.sector = region_to_sector(clone, region_start);
807 from.count = total_size;
809 to.bdev = clone->dest_dev->bdev;
810 to.sector = from.sector;
811 to.count = from.count;
814 atomic_add(nr_regions, &clone->hydrations_in_flight);
815 dm_kcopyd_copy(clone->kcopyd_client, &from, 1, &to, 0,
816 hydration_kcopyd_callback, hd);
819 static void overwrite_endio(struct bio *bio)
821 struct dm_clone_region_hydration *hd = bio->bi_private;
823 bio->bi_end_io = hd->overwrite_bio_end_io;
824 hd->status = bio->bi_status;
826 hydration_complete(hd);
829 static void hydration_overwrite(struct dm_clone_region_hydration *hd, struct bio *bio)
832 * We don't need to save and restore bio->bi_private because device
833 * mapper core generates a new bio for us to use, with clean
836 hd->overwrite_bio = bio;
837 hd->overwrite_bio_end_io = bio->bi_end_io;
839 bio->bi_end_io = overwrite_endio;
840 bio->bi_private = hd;
842 atomic_inc(&hd->clone->hydrations_in_flight);
843 generic_make_request(bio);
847 * Hydrate bio's region.
849 * This function starts the hydration of the bio's region and puts the bio in
850 * the list of deferred bios for this region. In case, by the time this
851 * function is called, the region has finished hydrating it's submitted to the
852 * destination device.
854 * NOTE: The bio remapping must be performed by the caller.
856 static void hydrate_bio_region(struct clone *clone, struct bio *bio)
858 unsigned long region_nr;
859 struct hash_table_bucket *bucket;
860 struct dm_clone_region_hydration *hd, *hd2;
862 region_nr = bio_to_region(clone, bio);
863 bucket = get_hash_table_bucket(clone, region_nr);
865 bucket_lock_irq(bucket);
867 hd = __hash_find(bucket, region_nr);
869 /* Someone else is hydrating the region */
870 bio_list_add(&hd->deferred_bios, bio);
871 bucket_unlock_irq(bucket);
875 if (dm_clone_is_region_hydrated(clone->cmd, region_nr)) {
876 /* The region has been hydrated */
877 bucket_unlock_irq(bucket);
878 issue_bio(clone, bio);
883 * We must allocate a hydration descriptor and start the hydration of
884 * the corresponding region.
886 bucket_unlock_irq(bucket);
888 hd = alloc_hydration(clone);
889 hydration_init(hd, region_nr);
891 bucket_lock_irq(bucket);
893 /* Check if the region has been hydrated in the meantime. */
894 if (dm_clone_is_region_hydrated(clone->cmd, region_nr)) {
895 bucket_unlock_irq(bucket);
897 issue_bio(clone, bio);
901 hd2 = __find_or_insert_region_hydration(bucket, hd);
903 /* Someone else started the region's hydration. */
904 bio_list_add(&hd2->deferred_bios, bio);
905 bucket_unlock_irq(bucket);
911 * If the metadata mode is RO or FAIL then there is no point starting a
912 * hydration, since we will not be able to update the metadata when the
913 * hydration finishes.
915 if (unlikely(get_clone_mode(clone) >= CM_READ_ONLY)) {
917 bucket_unlock_irq(bucket);
924 * Start region hydration.
926 * If a bio overwrites a region, i.e., its size is equal to the
927 * region's size, then we don't need to copy the region from the source
928 * to the destination device.
930 if (is_overwrite_bio(clone, bio)) {
931 bucket_unlock_irq(bucket);
932 hydration_overwrite(hd, bio);
934 bio_list_add(&hd->deferred_bios, bio);
935 bucket_unlock_irq(bucket);
936 hydration_copy(hd, 1);
940 /*---------------------------------------------------------------------------*/
943 * Background hydrations.
947 * Batch region hydrations.
949 * To better utilize device bandwidth we batch together the hydration of
950 * adjacent regions. This allows us to use small region sizes, e.g., 4KB, which
951 * is good for small, random write performance (because of the overwriting of
952 * un-hydrated regions) and at the same time issue big copy requests to kcopyd
953 * to achieve high hydration bandwidth.
956 struct dm_clone_region_hydration *head;
957 unsigned int nr_batched_regions;
960 static void __batch_hydration(struct batch_info *batch,
961 struct dm_clone_region_hydration *hd)
963 struct clone *clone = hd->clone;
964 unsigned int max_batch_size = READ_ONCE(clone->hydration_batch_size);
967 /* Try to extend the current batch */
968 if (batch->nr_batched_regions < max_batch_size &&
969 (batch->head->region_nr + batch->nr_batched_regions) == hd->region_nr) {
970 list_add_tail(&hd->list, &batch->head->list);
971 batch->nr_batched_regions++;
975 /* Check if we should issue the current batch */
976 if (batch->nr_batched_regions >= max_batch_size || hd) {
977 hydration_copy(batch->head, batch->nr_batched_regions);
979 batch->nr_batched_regions = 0;
986 /* We treat max batch sizes of zero and one equivalently */
987 if (max_batch_size <= 1) {
988 hydration_copy(hd, 1);
992 /* Start a new batch */
993 BUG_ON(!list_empty(&hd->list));
995 batch->nr_batched_regions = 1;
998 static unsigned long __start_next_hydration(struct clone *clone,
999 unsigned long offset,
1000 struct batch_info *batch)
1002 struct hash_table_bucket *bucket;
1003 struct dm_clone_region_hydration *hd;
1004 unsigned long nr_regions = clone->nr_regions;
1006 hd = alloc_hydration(clone);
1008 /* Try to find a region to hydrate. */
1010 offset = dm_clone_find_next_unhydrated_region(clone->cmd, offset);
1011 if (offset == nr_regions)
1014 bucket = get_hash_table_bucket(clone, offset);
1015 bucket_lock_irq(bucket);
1017 if (!dm_clone_is_region_hydrated(clone->cmd, offset) &&
1018 !__hash_find(bucket, offset)) {
1019 hydration_init(hd, offset);
1020 __insert_region_hydration(bucket, hd);
1021 bucket_unlock_irq(bucket);
1023 /* Batch hydration */
1024 __batch_hydration(batch, hd);
1026 return (offset + 1);
1029 bucket_unlock_irq(bucket);
1031 } while (++offset < nr_regions);
1040 * This function searches for regions that still reside in the source device
1041 * and starts their hydration.
1043 static void do_hydration(struct clone *clone)
1045 unsigned int current_volume;
1046 unsigned long offset, nr_regions = clone->nr_regions;
1048 struct batch_info batch = {
1050 .nr_batched_regions = 0,
1053 if (unlikely(get_clone_mode(clone) >= CM_READ_ONLY))
1056 if (dm_clone_is_hydration_done(clone->cmd))
1060 * Avoid race with device suspension.
1062 atomic_inc(&clone->hydrations_in_flight);
1065 * Make sure atomic_inc() is ordered before test_bit(), otherwise we
1066 * might race with clone_postsuspend() and start a region hydration
1067 * after the target has been suspended.
1069 * This is paired with the smp_mb__after_atomic() in
1070 * clone_postsuspend().
1072 smp_mb__after_atomic();
1074 offset = clone->hydration_offset;
1075 while (likely(!test_bit(DM_CLONE_HYDRATION_SUSPENDED, &clone->flags)) &&
1076 !atomic_read(&clone->ios_in_flight) &&
1077 test_bit(DM_CLONE_HYDRATION_ENABLED, &clone->flags) &&
1078 offset < nr_regions) {
1079 current_volume = atomic_read(&clone->hydrations_in_flight);
1080 current_volume += batch.nr_batched_regions;
1082 if (current_volume > READ_ONCE(clone->hydration_threshold))
1085 offset = __start_next_hydration(clone, offset, &batch);
1089 hydration_copy(batch.head, batch.nr_batched_regions);
1091 if (offset >= nr_regions)
1094 clone->hydration_offset = offset;
1096 if (atomic_dec_and_test(&clone->hydrations_in_flight))
1097 wakeup_hydration_waiters(clone);
1100 /*---------------------------------------------------------------------------*/
1102 static bool need_commit_due_to_time(struct clone *clone)
1104 return !time_in_range(jiffies, clone->last_commit_jiffies,
1105 clone->last_commit_jiffies + COMMIT_PERIOD);
1109 * A non-zero return indicates read-only or fail mode.
1111 static int commit_metadata(struct clone *clone)
1115 mutex_lock(&clone->commit_lock);
1117 if (!dm_clone_changed_this_transaction(clone->cmd))
1120 if (unlikely(get_clone_mode(clone) >= CM_READ_ONLY)) {
1125 r = dm_clone_metadata_commit(clone->cmd);
1128 __metadata_operation_failed(clone, "dm_clone_metadata_commit", r);
1132 if (dm_clone_is_hydration_done(clone->cmd))
1133 dm_table_event(clone->ti->table);
1135 mutex_unlock(&clone->commit_lock);
1140 static void process_deferred_discards(struct clone *clone)
1144 struct blk_plug plug;
1145 unsigned long rs, re;
1146 struct bio_list discards = BIO_EMPTY_LIST;
1148 spin_lock_irq(&clone->lock);
1149 bio_list_merge(&discards, &clone->deferred_discard_bios);
1150 bio_list_init(&clone->deferred_discard_bios);
1151 spin_unlock_irq(&clone->lock);
1153 if (bio_list_empty(&discards))
1156 if (unlikely(get_clone_mode(clone) >= CM_READ_ONLY))
1159 /* Update the metadata */
1160 bio_list_for_each(bio, &discards) {
1161 bio_region_range(clone, bio, &rs, &re);
1163 * A discard request might cover regions that have been already
1164 * hydrated. There is no need to update the metadata for these
1167 r = dm_clone_cond_set_range(clone->cmd, rs, re - rs);
1173 blk_start_plug(&plug);
1174 while ((bio = bio_list_pop(&discards)))
1175 complete_discard_bio(clone, bio, r == 0);
1176 blk_finish_plug(&plug);
1179 static void process_deferred_bios(struct clone *clone)
1181 struct bio_list bios = BIO_EMPTY_LIST;
1183 spin_lock_irq(&clone->lock);
1184 bio_list_merge(&bios, &clone->deferred_bios);
1185 bio_list_init(&clone->deferred_bios);
1186 spin_unlock_irq(&clone->lock);
1188 if (bio_list_empty(&bios))
1194 static void process_deferred_flush_bios(struct clone *clone)
1197 struct bio_list bios = BIO_EMPTY_LIST;
1198 struct bio_list bio_completions = BIO_EMPTY_LIST;
1201 * If there are any deferred flush bios, we must commit the metadata
1202 * before issuing them or signaling their completion.
1204 spin_lock_irq(&clone->lock);
1205 bio_list_merge(&bios, &clone->deferred_flush_bios);
1206 bio_list_init(&clone->deferred_flush_bios);
1208 bio_list_merge(&bio_completions, &clone->deferred_flush_completions);
1209 bio_list_init(&clone->deferred_flush_completions);
1210 spin_unlock_irq(&clone->lock);
1212 if (bio_list_empty(&bios) && bio_list_empty(&bio_completions) &&
1213 !(dm_clone_changed_this_transaction(clone->cmd) && need_commit_due_to_time(clone)))
1216 if (commit_metadata(clone)) {
1217 bio_list_merge(&bios, &bio_completions);
1219 while ((bio = bio_list_pop(&bios)))
1225 clone->last_commit_jiffies = jiffies;
1227 while ((bio = bio_list_pop(&bio_completions)))
1230 while ((bio = bio_list_pop(&bios)))
1231 generic_make_request(bio);
1234 static void do_worker(struct work_struct *work)
1236 struct clone *clone = container_of(work, typeof(*clone), worker);
1238 process_deferred_bios(clone);
1239 process_deferred_discards(clone);
1242 * process_deferred_flush_bios():
1246 * - Process deferred REQ_FUA completions
1248 * - Process deferred REQ_PREFLUSH bios
1250 process_deferred_flush_bios(clone);
1252 /* Background hydration */
1253 do_hydration(clone);
1257 * Commit periodically so that not too much unwritten data builds up.
1259 * Also, restart background hydration, if it has been stopped by in-flight I/O.
1261 static void do_waker(struct work_struct *work)
1263 struct clone *clone = container_of(to_delayed_work(work), struct clone, waker);
1266 queue_delayed_work(clone->wq, &clone->waker, COMMIT_PERIOD);
1269 /*---------------------------------------------------------------------------*/
1274 static int clone_map(struct dm_target *ti, struct bio *bio)
1276 struct clone *clone = ti->private;
1277 unsigned long region_nr;
1279 atomic_inc(&clone->ios_in_flight);
1281 if (unlikely(get_clone_mode(clone) == CM_FAIL))
1282 return DM_MAPIO_KILL;
1285 * REQ_PREFLUSH bios carry no data:
1287 * - Commit metadata, if changed
1289 * - Pass down to destination device
1291 if (bio->bi_opf & REQ_PREFLUSH) {
1292 remap_and_issue(clone, bio);
1293 return DM_MAPIO_SUBMITTED;
1296 bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
1299 * dm-clone interprets discards and performs a fast hydration of the
1300 * discarded regions, i.e., we skip the copy from the source device and
1301 * just mark the regions as hydrated.
1303 if (bio_op(bio) == REQ_OP_DISCARD) {
1304 process_discard_bio(clone, bio);
1305 return DM_MAPIO_SUBMITTED;
1309 * If the bio's region is hydrated, redirect it to the destination
1312 * If the region is not hydrated and the bio is a READ, redirect it to
1313 * the source device.
1315 * Else, defer WRITE bio until after its region has been hydrated and
1316 * start the region's hydration immediately.
1318 region_nr = bio_to_region(clone, bio);
1319 if (dm_clone_is_region_hydrated(clone->cmd, region_nr)) {
1320 remap_and_issue(clone, bio);
1321 return DM_MAPIO_SUBMITTED;
1322 } else if (bio_data_dir(bio) == READ) {
1323 remap_to_source(clone, bio);
1324 return DM_MAPIO_REMAPPED;
1327 remap_to_dest(clone, bio);
1328 hydrate_bio_region(clone, bio);
1330 return DM_MAPIO_SUBMITTED;
1333 static int clone_endio(struct dm_target *ti, struct bio *bio, blk_status_t *error)
1335 struct clone *clone = ti->private;
1337 atomic_dec(&clone->ios_in_flight);
1339 return DM_ENDIO_DONE;
1342 static void emit_flags(struct clone *clone, char *result, unsigned int maxlen,
1345 ssize_t sz = *sz_ptr;
1348 count = !test_bit(DM_CLONE_HYDRATION_ENABLED, &clone->flags);
1349 count += !test_bit(DM_CLONE_DISCARD_PASSDOWN, &clone->flags);
1351 DMEMIT("%u ", count);
1353 if (!test_bit(DM_CLONE_HYDRATION_ENABLED, &clone->flags))
1354 DMEMIT("no_hydration ");
1356 if (!test_bit(DM_CLONE_DISCARD_PASSDOWN, &clone->flags))
1357 DMEMIT("no_discard_passdown ");
1362 static void emit_core_args(struct clone *clone, char *result,
1363 unsigned int maxlen, ssize_t *sz_ptr)
1365 ssize_t sz = *sz_ptr;
1366 unsigned int count = 4;
1368 DMEMIT("%u hydration_threshold %u hydration_batch_size %u ", count,
1369 READ_ONCE(clone->hydration_threshold),
1370 READ_ONCE(clone->hydration_batch_size));
1378 * <metadata block size> <#used metadata blocks>/<#total metadata blocks>
1379 * <clone region size> <#hydrated regions>/<#total regions> <#hydrating regions>
1380 * <#features> <features>* <#core args> <core args>* <clone metadata mode>
1382 static void clone_status(struct dm_target *ti, status_type_t type,
1383 unsigned int status_flags, char *result,
1384 unsigned int maxlen)
1389 dm_block_t nr_free_metadata_blocks = 0;
1390 dm_block_t nr_metadata_blocks = 0;
1391 char buf[BDEVNAME_SIZE];
1392 struct clone *clone = ti->private;
1395 case STATUSTYPE_INFO:
1396 if (get_clone_mode(clone) == CM_FAIL) {
1401 /* Commit to ensure statistics aren't out-of-date */
1402 if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti))
1403 (void) commit_metadata(clone);
1405 r = dm_clone_get_free_metadata_block_count(clone->cmd, &nr_free_metadata_blocks);
1408 DMERR("%s: dm_clone_get_free_metadata_block_count returned %d",
1409 clone_device_name(clone), r);
1413 r = dm_clone_get_metadata_dev_size(clone->cmd, &nr_metadata_blocks);
1416 DMERR("%s: dm_clone_get_metadata_dev_size returned %d",
1417 clone_device_name(clone), r);
1421 DMEMIT("%u %llu/%llu %llu %lu/%lu %u ",
1422 DM_CLONE_METADATA_BLOCK_SIZE,
1423 (unsigned long long)(nr_metadata_blocks - nr_free_metadata_blocks),
1424 (unsigned long long)nr_metadata_blocks,
1425 (unsigned long long)clone->region_size,
1426 dm_clone_nr_of_hydrated_regions(clone->cmd),
1428 atomic_read(&clone->hydrations_in_flight));
1430 emit_flags(clone, result, maxlen, &sz);
1431 emit_core_args(clone, result, maxlen, &sz);
1433 switch (get_clone_mode(clone)) {
1446 case STATUSTYPE_TABLE:
1447 format_dev_t(buf, clone->metadata_dev->bdev->bd_dev);
1450 format_dev_t(buf, clone->dest_dev->bdev->bd_dev);
1453 format_dev_t(buf, clone->source_dev->bdev->bd_dev);
1456 for (i = 0; i < clone->nr_ctr_args; i++)
1457 DMEMIT(" %s", clone->ctr_args[i]);
1466 static int clone_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
1468 struct request_queue *dest_q, *source_q;
1469 struct clone *clone = container_of(cb, struct clone, callbacks);
1471 source_q = bdev_get_queue(clone->source_dev->bdev);
1472 dest_q = bdev_get_queue(clone->dest_dev->bdev);
1474 return (bdi_congested(dest_q->backing_dev_info, bdi_bits) |
1475 bdi_congested(source_q->backing_dev_info, bdi_bits));
1478 static sector_t get_dev_size(struct dm_dev *dev)
1480 return i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT;
1483 /*---------------------------------------------------------------------------*/
1486 * Construct a clone device mapping:
1488 * clone <metadata dev> <destination dev> <source dev> <region size>
1489 * [<#feature args> [<feature arg>]* [<#core args> [key value]*]]
1491 * metadata dev: Fast device holding the persistent metadata
1492 * destination dev: The destination device, which will become a clone of the
1494 * source dev: The read-only source device that gets cloned
1495 * region size: dm-clone unit size in sectors
1497 * #feature args: Number of feature arguments passed
1498 * feature args: E.g. no_hydration, no_discard_passdown
1500 * #core arguments: An even number of core arguments
1501 * core arguments: Key/value pairs for tuning the core
1502 * E.g. 'hydration_threshold 256'
1504 static int parse_feature_args(struct dm_arg_set *as, struct clone *clone)
1508 const char *arg_name;
1509 struct dm_target *ti = clone->ti;
1511 const struct dm_arg args = {
1514 .error = "Invalid number of feature arguments"
1517 /* No feature arguments supplied */
1521 r = dm_read_arg_group(&args, as, &argc, &ti->error);
1526 arg_name = dm_shift_arg(as);
1529 if (!strcasecmp(arg_name, "no_hydration")) {
1530 __clear_bit(DM_CLONE_HYDRATION_ENABLED, &clone->flags);
1531 } else if (!strcasecmp(arg_name, "no_discard_passdown")) {
1532 __clear_bit(DM_CLONE_DISCARD_PASSDOWN, &clone->flags);
1534 ti->error = "Invalid feature argument";
1542 static int parse_core_args(struct dm_arg_set *as, struct clone *clone)
1547 const char *arg_name;
1548 struct dm_target *ti = clone->ti;
1550 const struct dm_arg args = {
1553 .error = "Invalid number of core arguments"
1556 /* Initialize core arguments */
1557 clone->hydration_batch_size = DEFAULT_HYDRATION_BATCH_SIZE;
1558 clone->hydration_threshold = DEFAULT_HYDRATION_THRESHOLD;
1560 /* No core arguments supplied */
1564 r = dm_read_arg_group(&args, as, &argc, &ti->error);
1569 ti->error = "Number of core arguments must be even";
1574 arg_name = dm_shift_arg(as);
1577 if (!strcasecmp(arg_name, "hydration_threshold")) {
1578 if (kstrtouint(dm_shift_arg(as), 10, &value)) {
1579 ti->error = "Invalid value for argument `hydration_threshold'";
1582 clone->hydration_threshold = value;
1583 } else if (!strcasecmp(arg_name, "hydration_batch_size")) {
1584 if (kstrtouint(dm_shift_arg(as), 10, &value)) {
1585 ti->error = "Invalid value for argument `hydration_batch_size'";
1588 clone->hydration_batch_size = value;
1590 ti->error = "Invalid core argument";
1598 static int parse_region_size(struct clone *clone, struct dm_arg_set *as, char **error)
1601 unsigned int region_size;
1604 arg.min = MIN_REGION_SIZE;
1605 arg.max = MAX_REGION_SIZE;
1606 arg.error = "Invalid region size";
1608 r = dm_read_arg(&arg, as, ®ion_size, error);
1612 /* Check region size is a power of 2 */
1613 if (!is_power_of_2(region_size)) {
1614 *error = "Region size is not a power of 2";
1618 /* Validate the region size against the device logical block size */
1619 if (region_size % (bdev_logical_block_size(clone->source_dev->bdev) >> 9) ||
1620 region_size % (bdev_logical_block_size(clone->dest_dev->bdev) >> 9)) {
1621 *error = "Region size is not a multiple of device logical block size";
1625 clone->region_size = region_size;
1630 static int validate_nr_regions(unsigned long n, char **error)
1633 * dm_bitset restricts us to 2^32 regions. test_bit & co. restrict us
1634 * further to 2^31 regions.
1636 if (n > (1UL << 31)) {
1637 *error = "Too many regions. Consider increasing the region size";
1644 static int parse_metadata_dev(struct clone *clone, struct dm_arg_set *as, char **error)
1647 sector_t metadata_dev_size;
1648 char b[BDEVNAME_SIZE];
1650 r = dm_get_device(clone->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
1651 &clone->metadata_dev);
1653 *error = "Error opening metadata device";
1657 metadata_dev_size = get_dev_size(clone->metadata_dev);
1658 if (metadata_dev_size > DM_CLONE_METADATA_MAX_SECTORS_WARNING)
1659 DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
1660 bdevname(clone->metadata_dev->bdev, b), DM_CLONE_METADATA_MAX_SECTORS);
1665 static int parse_dest_dev(struct clone *clone, struct dm_arg_set *as, char **error)
1668 sector_t dest_dev_size;
1670 r = dm_get_device(clone->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
1673 *error = "Error opening destination device";
1677 dest_dev_size = get_dev_size(clone->dest_dev);
1678 if (dest_dev_size < clone->ti->len) {
1679 dm_put_device(clone->ti, clone->dest_dev);
1680 *error = "Device size larger than destination device";
1687 static int parse_source_dev(struct clone *clone, struct dm_arg_set *as, char **error)
1690 sector_t source_dev_size;
1692 r = dm_get_device(clone->ti, dm_shift_arg(as), FMODE_READ,
1693 &clone->source_dev);
1695 *error = "Error opening source device";
1699 source_dev_size = get_dev_size(clone->source_dev);
1700 if (source_dev_size < clone->ti->len) {
1701 dm_put_device(clone->ti, clone->source_dev);
1702 *error = "Device size larger than source device";
1709 static int copy_ctr_args(struct clone *clone, int argc, const char **argv, char **error)
1714 copy = kcalloc(argc, sizeof(*copy), GFP_KERNEL);
1718 for (i = 0; i < argc; i++) {
1719 copy[i] = kstrdup(argv[i], GFP_KERNEL);
1729 clone->nr_ctr_args = argc;
1730 clone->ctr_args = copy;
1734 *error = "Failed to allocate memory for table line";
1738 static int clone_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1741 struct clone *clone;
1742 struct dm_arg_set as;
1745 ti->error = "Invalid number of arguments";
1752 clone = kzalloc(sizeof(*clone), GFP_KERNEL);
1754 ti->error = "Failed to allocate clone structure";
1760 /* Initialize dm-clone flags */
1761 __set_bit(DM_CLONE_HYDRATION_ENABLED, &clone->flags);
1762 __set_bit(DM_CLONE_HYDRATION_SUSPENDED, &clone->flags);
1763 __set_bit(DM_CLONE_DISCARD_PASSDOWN, &clone->flags);
1765 r = parse_metadata_dev(clone, &as, &ti->error);
1767 goto out_with_clone;
1769 r = parse_dest_dev(clone, &as, &ti->error);
1771 goto out_with_meta_dev;
1773 r = parse_source_dev(clone, &as, &ti->error);
1775 goto out_with_dest_dev;
1777 r = parse_region_size(clone, &as, &ti->error);
1779 goto out_with_source_dev;
1781 clone->region_shift = __ffs(clone->region_size);
1782 clone->nr_regions = dm_sector_div_up(ti->len, clone->region_size);
1784 r = validate_nr_regions(clone->nr_regions, &ti->error);
1786 goto out_with_source_dev;
1788 r = dm_set_target_max_io_len(ti, clone->region_size);
1790 ti->error = "Failed to set max io len";
1791 goto out_with_source_dev;
1794 r = parse_feature_args(&as, clone);
1796 goto out_with_source_dev;
1798 r = parse_core_args(&as, clone);
1800 goto out_with_source_dev;
1803 clone->cmd = dm_clone_metadata_open(clone->metadata_dev->bdev, ti->len,
1804 clone->region_size);
1805 if (IS_ERR(clone->cmd)) {
1806 ti->error = "Failed to load metadata";
1807 r = PTR_ERR(clone->cmd);
1808 goto out_with_source_dev;
1811 __set_clone_mode(clone, CM_WRITE);
1813 if (get_clone_mode(clone) != CM_WRITE) {
1814 ti->error = "Unable to get write access to metadata, please check/repair metadata";
1816 goto out_with_metadata;
1819 clone->last_commit_jiffies = jiffies;
1821 /* Allocate hydration hash table */
1822 r = hash_table_init(clone);
1824 ti->error = "Failed to allocate hydration hash table";
1825 goto out_with_metadata;
1828 atomic_set(&clone->ios_in_flight, 0);
1829 init_waitqueue_head(&clone->hydration_stopped);
1830 spin_lock_init(&clone->lock);
1831 bio_list_init(&clone->deferred_bios);
1832 bio_list_init(&clone->deferred_discard_bios);
1833 bio_list_init(&clone->deferred_flush_bios);
1834 bio_list_init(&clone->deferred_flush_completions);
1835 clone->hydration_offset = 0;
1836 atomic_set(&clone->hydrations_in_flight, 0);
1838 clone->wq = alloc_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM, 0);
1840 ti->error = "Failed to allocate workqueue";
1845 INIT_WORK(&clone->worker, do_worker);
1846 INIT_DELAYED_WORK(&clone->waker, do_waker);
1848 clone->kcopyd_client = dm_kcopyd_client_create(&dm_kcopyd_throttle);
1849 if (IS_ERR(clone->kcopyd_client)) {
1850 r = PTR_ERR(clone->kcopyd_client);
1854 r = mempool_init_slab_pool(&clone->hydration_pool, MIN_HYDRATIONS,
1857 ti->error = "Failed to create dm_clone_region_hydration memory pool";
1858 goto out_with_kcopyd;
1861 /* Save a copy of the table line */
1862 r = copy_ctr_args(clone, argc - 3, (const char **)argv + 3, &ti->error);
1864 goto out_with_mempool;
1866 mutex_init(&clone->commit_lock);
1867 clone->callbacks.congested_fn = clone_is_congested;
1868 dm_table_add_target_callbacks(ti->table, &clone->callbacks);
1870 /* Enable flushes */
1871 ti->num_flush_bios = 1;
1872 ti->flush_supported = true;
1874 /* Enable discards */
1875 ti->discards_supported = true;
1876 ti->num_discard_bios = 1;
1878 ti->private = clone;
1883 mempool_exit(&clone->hydration_pool);
1885 dm_kcopyd_client_destroy(clone->kcopyd_client);
1887 destroy_workqueue(clone->wq);
1889 hash_table_exit(clone);
1891 dm_clone_metadata_close(clone->cmd);
1892 out_with_source_dev:
1893 dm_put_device(ti, clone->source_dev);
1895 dm_put_device(ti, clone->dest_dev);
1897 dm_put_device(ti, clone->metadata_dev);
1904 static void clone_dtr(struct dm_target *ti)
1907 struct clone *clone = ti->private;
1909 mutex_destroy(&clone->commit_lock);
1911 for (i = 0; i < clone->nr_ctr_args; i++)
1912 kfree(clone->ctr_args[i]);
1913 kfree(clone->ctr_args);
1915 mempool_exit(&clone->hydration_pool);
1916 dm_kcopyd_client_destroy(clone->kcopyd_client);
1917 destroy_workqueue(clone->wq);
1918 hash_table_exit(clone);
1919 dm_clone_metadata_close(clone->cmd);
1920 dm_put_device(ti, clone->source_dev);
1921 dm_put_device(ti, clone->dest_dev);
1922 dm_put_device(ti, clone->metadata_dev);
1927 /*---------------------------------------------------------------------------*/
1929 static void clone_postsuspend(struct dm_target *ti)
1931 struct clone *clone = ti->private;
1934 * To successfully suspend the device:
1936 * - We cancel the delayed work for periodic commits and wait for
1939 * - We stop the background hydration, i.e. we prevent new region
1940 * hydrations from starting.
1942 * - We wait for any in-flight hydrations to finish.
1944 * - We flush the workqueue.
1946 * - We commit the metadata.
1948 cancel_delayed_work_sync(&clone->waker);
1950 set_bit(DM_CLONE_HYDRATION_SUSPENDED, &clone->flags);
1953 * Make sure set_bit() is ordered before atomic_read(), otherwise we
1954 * might race with do_hydration() and miss some started region
1957 * This is paired with smp_mb__after_atomic() in do_hydration().
1959 smp_mb__after_atomic();
1961 wait_event(clone->hydration_stopped, !atomic_read(&clone->hydrations_in_flight));
1962 flush_workqueue(clone->wq);
1964 (void) commit_metadata(clone);
1967 static void clone_resume(struct dm_target *ti)
1969 struct clone *clone = ti->private;
1971 clear_bit(DM_CLONE_HYDRATION_SUSPENDED, &clone->flags);
1972 do_waker(&clone->waker.work);
1975 static bool bdev_supports_discards(struct block_device *bdev)
1977 struct request_queue *q = bdev_get_queue(bdev);
1979 return (q && blk_queue_discard(q));
1983 * If discard_passdown was enabled verify that the destination device supports
1984 * discards. Disable discard_passdown if not.
1986 static void disable_passdown_if_not_supported(struct clone *clone)
1988 struct block_device *dest_dev = clone->dest_dev->bdev;
1989 struct queue_limits *dest_limits = &bdev_get_queue(dest_dev)->limits;
1990 const char *reason = NULL;
1991 char buf[BDEVNAME_SIZE];
1993 if (!test_bit(DM_CLONE_DISCARD_PASSDOWN, &clone->flags))
1996 if (!bdev_supports_discards(dest_dev))
1997 reason = "discard unsupported";
1998 else if (dest_limits->max_discard_sectors < clone->region_size)
1999 reason = "max discard sectors smaller than a region";
2002 DMWARN("Destination device (%s) %s: Disabling discard passdown.",
2003 bdevname(dest_dev, buf), reason);
2004 clear_bit(DM_CLONE_DISCARD_PASSDOWN, &clone->flags);
2008 static void set_discard_limits(struct clone *clone, struct queue_limits *limits)
2010 struct block_device *dest_bdev = clone->dest_dev->bdev;
2011 struct queue_limits *dest_limits = &bdev_get_queue(dest_bdev)->limits;
2013 if (!test_bit(DM_CLONE_DISCARD_PASSDOWN, &clone->flags)) {
2014 /* No passdown is done so we set our own virtual limits */
2015 limits->discard_granularity = clone->region_size << SECTOR_SHIFT;
2016 limits->max_discard_sectors = round_down(UINT_MAX >> SECTOR_SHIFT, clone->region_size);
2021 * clone_iterate_devices() is stacking both the source and destination
2022 * device limits but discards aren't passed to the source device, so
2023 * inherit destination's limits.
2025 limits->max_discard_sectors = dest_limits->max_discard_sectors;
2026 limits->max_hw_discard_sectors = dest_limits->max_hw_discard_sectors;
2027 limits->discard_granularity = dest_limits->discard_granularity;
2028 limits->discard_alignment = dest_limits->discard_alignment;
2029 limits->discard_misaligned = dest_limits->discard_misaligned;
2030 limits->max_discard_segments = dest_limits->max_discard_segments;
2033 static void clone_io_hints(struct dm_target *ti, struct queue_limits *limits)
2035 struct clone *clone = ti->private;
2036 u64 io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
2039 * If the system-determined stacked limits are compatible with
2040 * dm-clone's region size (io_opt is a factor) do not override them.
2042 if (io_opt_sectors < clone->region_size ||
2043 do_div(io_opt_sectors, clone->region_size)) {
2044 blk_limits_io_min(limits, clone->region_size << SECTOR_SHIFT);
2045 blk_limits_io_opt(limits, clone->region_size << SECTOR_SHIFT);
2048 disable_passdown_if_not_supported(clone);
2049 set_discard_limits(clone, limits);
2052 static int clone_iterate_devices(struct dm_target *ti,
2053 iterate_devices_callout_fn fn, void *data)
2056 struct clone *clone = ti->private;
2057 struct dm_dev *dest_dev = clone->dest_dev;
2058 struct dm_dev *source_dev = clone->source_dev;
2060 ret = fn(ti, source_dev, 0, ti->len, data);
2062 ret = fn(ti, dest_dev, 0, ti->len, data);
2067 * dm-clone message functions.
2069 static void set_hydration_threshold(struct clone *clone, unsigned int nr_regions)
2071 WRITE_ONCE(clone->hydration_threshold, nr_regions);
2074 * If user space sets hydration_threshold to zero then the hydration
2075 * will stop. If at a later time the hydration_threshold is increased
2076 * we must restart the hydration process by waking up the worker.
2081 static void set_hydration_batch_size(struct clone *clone, unsigned int nr_regions)
2083 WRITE_ONCE(clone->hydration_batch_size, nr_regions);
2086 static void enable_hydration(struct clone *clone)
2088 if (!test_and_set_bit(DM_CLONE_HYDRATION_ENABLED, &clone->flags))
2092 static void disable_hydration(struct clone *clone)
2094 clear_bit(DM_CLONE_HYDRATION_ENABLED, &clone->flags);
2097 static int clone_message(struct dm_target *ti, unsigned int argc, char **argv,
2098 char *result, unsigned int maxlen)
2100 struct clone *clone = ti->private;
2106 if (!strcasecmp(argv[0], "enable_hydration")) {
2107 enable_hydration(clone);
2111 if (!strcasecmp(argv[0], "disable_hydration")) {
2112 disable_hydration(clone);
2119 if (!strcasecmp(argv[0], "hydration_threshold")) {
2120 if (kstrtouint(argv[1], 10, &value))
2123 set_hydration_threshold(clone, value);
2128 if (!strcasecmp(argv[0], "hydration_batch_size")) {
2129 if (kstrtouint(argv[1], 10, &value))
2132 set_hydration_batch_size(clone, value);
2137 DMERR("%s: Unsupported message `%s'", clone_device_name(clone), argv[0]);
2141 static struct target_type clone_target = {
2143 .version = {1, 0, 0},
2144 .module = THIS_MODULE,
2148 .end_io = clone_endio,
2149 .postsuspend = clone_postsuspend,
2150 .resume = clone_resume,
2151 .status = clone_status,
2152 .message = clone_message,
2153 .io_hints = clone_io_hints,
2154 .iterate_devices = clone_iterate_devices,
2157 /*---------------------------------------------------------------------------*/
2159 /* Module functions */
2160 static int __init dm_clone_init(void)
2164 _hydration_cache = KMEM_CACHE(dm_clone_region_hydration, 0);
2165 if (!_hydration_cache)
2168 r = dm_register_target(&clone_target);
2170 DMERR("Failed to register clone target");
2177 static void __exit dm_clone_exit(void)
2179 dm_unregister_target(&clone_target);
2181 kmem_cache_destroy(_hydration_cache);
2182 _hydration_cache = NULL;
2186 module_init(dm_clone_init);
2187 module_exit(dm_clone_exit);
2189 MODULE_DESCRIPTION(DM_NAME " clone target");
2190 MODULE_AUTHOR("Nikos Tsironis <ntsironis@arrikto.com>");
2191 MODULE_LICENSE("GPL");