2 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
3 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
5 * This file is released under the GPL.
10 #include "dm-uevent.h"
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
15 #include <linux/sched/mm.h>
16 #include <linux/sched/signal.h>
17 #include <linux/blkpg.h>
18 #include <linux/bio.h>
19 #include <linux/mempool.h>
20 #include <linux/dax.h>
21 #include <linux/slab.h>
22 #include <linux/idr.h>
23 #include <linux/uio.h>
24 #include <linux/hdreg.h>
25 #include <linux/delay.h>
26 #include <linux/wait.h>
28 #include <linux/refcount.h>
30 #define DM_MSG_PREFIX "core"
33 * Cookies are numeric values sent with CHANGE and REMOVE
34 * uevents while resuming, removing or renaming the device.
36 #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
37 #define DM_COOKIE_LENGTH 24
39 static const char *_name = DM_NAME;
41 static unsigned int major = 0;
42 static unsigned int _major = 0;
44 static DEFINE_IDR(_minor_idr);
46 static DEFINE_SPINLOCK(_minor_lock);
48 static void do_deferred_remove(struct work_struct *w);
50 static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
52 static struct workqueue_struct *deferred_remove_workqueue;
54 atomic_t dm_global_event_nr = ATOMIC_INIT(0);
55 DECLARE_WAIT_QUEUE_HEAD(dm_global_eventq);
57 void dm_issue_global_event(void)
59 atomic_inc(&dm_global_event_nr);
60 wake_up(&dm_global_eventq);
64 * One of these is allocated (on-stack) per original bio.
71 unsigned sector_count;
75 * One of these is allocated per clone bio.
77 #define DM_TIO_MAGIC 7282014
82 unsigned target_bio_nr;
89 * One of these is allocated per original bio.
90 * It contains the first clone used for that original.
92 #define DM_IO_MAGIC 5191977
95 struct mapped_device *md;
99 unsigned long start_time;
100 spinlock_t endio_lock;
101 struct dm_stats_aux stats_aux;
102 /* last member of dm_target_io is 'struct bio' */
103 struct dm_target_io tio;
106 void *dm_per_bio_data(struct bio *bio, size_t data_size)
108 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
109 if (!tio->inside_dm_io)
110 return (char *)bio - offsetof(struct dm_target_io, clone) - data_size;
111 return (char *)bio - offsetof(struct dm_target_io, clone) - offsetof(struct dm_io, tio) - data_size;
113 EXPORT_SYMBOL_GPL(dm_per_bio_data);
115 struct bio *dm_bio_from_per_bio_data(void *data, size_t data_size)
117 struct dm_io *io = (struct dm_io *)((char *)data + data_size);
118 if (io->magic == DM_IO_MAGIC)
119 return (struct bio *)((char *)io + offsetof(struct dm_io, tio) + offsetof(struct dm_target_io, clone));
120 BUG_ON(io->magic != DM_TIO_MAGIC);
121 return (struct bio *)((char *)io + offsetof(struct dm_target_io, clone));
123 EXPORT_SYMBOL_GPL(dm_bio_from_per_bio_data);
125 unsigned dm_bio_get_target_bio_nr(const struct bio *bio)
127 return container_of(bio, struct dm_target_io, clone)->target_bio_nr;
129 EXPORT_SYMBOL_GPL(dm_bio_get_target_bio_nr);
131 #define MINOR_ALLOCED ((void *)-1)
134 * Bits for the md->flags field.
136 #define DMF_BLOCK_IO_FOR_SUSPEND 0
137 #define DMF_SUSPENDED 1
139 #define DMF_FREEING 3
140 #define DMF_DELETING 4
141 #define DMF_NOFLUSH_SUSPENDING 5
142 #define DMF_DEFERRED_REMOVE 6
143 #define DMF_SUSPENDED_INTERNALLY 7
144 #define DMF_POST_SUSPENDING 8
146 #define DM_NUMA_NODE NUMA_NO_NODE
147 static int dm_numa_node = DM_NUMA_NODE;
149 #define DEFAULT_SWAP_BIOS (8 * 1048576 / PAGE_SIZE)
150 static int swap_bios = DEFAULT_SWAP_BIOS;
151 static int get_swap_bios(void)
153 int latch = READ_ONCE(swap_bios);
154 if (unlikely(latch <= 0))
155 latch = DEFAULT_SWAP_BIOS;
160 * For mempools pre-allocation at the table loading time.
162 struct dm_md_mempools {
164 struct bio_set io_bs;
167 struct table_device {
168 struct list_head list;
170 struct dm_dev dm_dev;
174 * Bio-based DM's mempools' reserved IOs set by the user.
176 #define RESERVED_BIO_BASED_IOS 16
177 static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
179 static int __dm_get_module_param_int(int *module_param, int min, int max)
181 int param = READ_ONCE(*module_param);
182 int modified_param = 0;
183 bool modified = true;
186 modified_param = min;
187 else if (param > max)
188 modified_param = max;
193 (void)cmpxchg(module_param, param, modified_param);
194 param = modified_param;
200 unsigned __dm_get_module_param(unsigned *module_param,
201 unsigned def, unsigned max)
203 unsigned param = READ_ONCE(*module_param);
204 unsigned modified_param = 0;
207 modified_param = def;
208 else if (param > max)
209 modified_param = max;
211 if (modified_param) {
212 (void)cmpxchg(module_param, param, modified_param);
213 param = modified_param;
219 unsigned dm_get_reserved_bio_based_ios(void)
221 return __dm_get_module_param(&reserved_bio_based_ios,
222 RESERVED_BIO_BASED_IOS, DM_RESERVED_MAX_IOS);
224 EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
226 static unsigned dm_get_numa_node(void)
228 return __dm_get_module_param_int(&dm_numa_node,
229 DM_NUMA_NODE, num_online_nodes() - 1);
232 static int __init local_init(void)
236 r = dm_uevent_init();
240 deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
241 if (!deferred_remove_workqueue) {
243 goto out_uevent_exit;
247 r = register_blkdev(_major, _name);
249 goto out_free_workqueue;
257 destroy_workqueue(deferred_remove_workqueue);
264 static void local_exit(void)
266 destroy_workqueue(deferred_remove_workqueue);
268 unregister_blkdev(_major, _name);
273 DMINFO("cleaned up");
276 static int (*_inits[])(void) __initdata = {
287 static void (*_exits[])(void) = {
298 static int __init dm_init(void)
300 const int count = ARRAY_SIZE(_inits);
304 for (i = 0; i < count; i++) {
319 static void __exit dm_exit(void)
321 int i = ARRAY_SIZE(_exits);
327 * Should be empty by this point.
329 idr_destroy(&_minor_idr);
333 * Block device functions
335 int dm_deleting_md(struct mapped_device *md)
337 return test_bit(DMF_DELETING, &md->flags);
340 static int dm_blk_open(struct block_device *bdev, fmode_t mode)
342 struct mapped_device *md;
344 spin_lock(&_minor_lock);
346 md = bdev->bd_disk->private_data;
350 if (test_bit(DMF_FREEING, &md->flags) ||
351 dm_deleting_md(md)) {
357 atomic_inc(&md->open_count);
359 spin_unlock(&_minor_lock);
361 return md ? 0 : -ENXIO;
364 static void dm_blk_close(struct gendisk *disk, fmode_t mode)
366 struct mapped_device *md;
368 spin_lock(&_minor_lock);
370 md = disk->private_data;
374 if (atomic_dec_and_test(&md->open_count) &&
375 (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
376 queue_work(deferred_remove_workqueue, &deferred_remove_work);
380 spin_unlock(&_minor_lock);
383 int dm_open_count(struct mapped_device *md)
385 return atomic_read(&md->open_count);
389 * Guarantees nothing is using the device before it's deleted.
391 int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
395 spin_lock(&_minor_lock);
397 if (dm_open_count(md)) {
400 set_bit(DMF_DEFERRED_REMOVE, &md->flags);
401 } else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
404 set_bit(DMF_DELETING, &md->flags);
406 spin_unlock(&_minor_lock);
411 int dm_cancel_deferred_remove(struct mapped_device *md)
415 spin_lock(&_minor_lock);
417 if (test_bit(DMF_DELETING, &md->flags))
420 clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
422 spin_unlock(&_minor_lock);
427 static void do_deferred_remove(struct work_struct *w)
429 dm_deferred_remove();
432 sector_t dm_get_size(struct mapped_device *md)
434 return get_capacity(md->disk);
437 struct request_queue *dm_get_md_queue(struct mapped_device *md)
442 struct dm_stats *dm_get_stats(struct mapped_device *md)
447 static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
449 struct mapped_device *md = bdev->bd_disk->private_data;
451 return dm_get_geometry(md, geo);
454 static int dm_blk_report_zones(struct gendisk *disk, sector_t sector,
455 struct blk_zone *zones, unsigned int *nr_zones)
457 #ifdef CONFIG_BLK_DEV_ZONED
458 struct mapped_device *md = disk->private_data;
459 struct dm_target *tgt;
460 struct dm_table *map;
463 if (dm_suspended_md(md))
466 map = dm_get_live_table(md, &srcu_idx);
472 tgt = dm_table_find_target(map, sector);
479 * If we are executing this, we already know that the block device
480 * is a zoned device and so each target should have support for that
481 * type of drive. A missing report_zones method means that the target
482 * driver has a problem.
484 if (WARN_ON(!tgt->type->report_zones)) {
490 * blkdev_report_zones() will loop and call this again to cover all the
491 * zones of the target, eventually moving on to the next target.
492 * So there is no need to loop here trying to fill the entire array
495 ret = tgt->type->report_zones(tgt, sector, zones, nr_zones);
498 dm_put_live_table(md, srcu_idx);
505 static int dm_prepare_ioctl(struct mapped_device *md, int *srcu_idx,
506 struct block_device **bdev)
508 struct dm_target *tgt;
509 struct dm_table *map;
514 map = dm_get_live_table(md, srcu_idx);
515 if (!map || !dm_table_get_size(map))
518 /* We only support devices that have a single target */
519 if (dm_table_get_num_targets(map) != 1)
522 tgt = dm_table_get_target(map, 0);
523 if (!tgt->type->prepare_ioctl)
526 if (dm_suspended_md(md))
529 r = tgt->type->prepare_ioctl(tgt, bdev);
530 if (r == -ENOTCONN && !fatal_signal_pending(current)) {
531 dm_put_live_table(md, *srcu_idx);
539 static void dm_unprepare_ioctl(struct mapped_device *md, int srcu_idx)
541 dm_put_live_table(md, srcu_idx);
544 static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
545 unsigned int cmd, unsigned long arg)
547 struct mapped_device *md = bdev->bd_disk->private_data;
550 r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
556 * Target determined this ioctl is being issued against a
557 * subset of the parent bdev; require extra privileges.
559 if (!capable(CAP_SYS_RAWIO)) {
561 "%s: sending ioctl %x to DM device without required privilege.",
568 r = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
570 dm_unprepare_ioctl(md, srcu_idx);
574 static void start_io_acct(struct dm_io *io);
576 static struct dm_io *alloc_io(struct mapped_device *md, struct bio *bio)
579 struct dm_target_io *tio;
582 clone = bio_alloc_bioset(GFP_NOIO, 0, &md->io_bs);
586 tio = container_of(clone, struct dm_target_io, clone);
587 tio->inside_dm_io = true;
590 io = container_of(tio, struct dm_io, tio);
591 io->magic = DM_IO_MAGIC;
593 atomic_set(&io->io_count, 1);
596 spin_lock_init(&io->endio_lock);
603 static void free_io(struct mapped_device *md, struct dm_io *io)
605 bio_put(&io->tio.clone);
608 static struct dm_target_io *alloc_tio(struct clone_info *ci, struct dm_target *ti,
609 unsigned target_bio_nr, gfp_t gfp_mask)
611 struct dm_target_io *tio;
613 if (!ci->io->tio.io) {
614 /* the dm_target_io embedded in ci->io is available */
617 struct bio *clone = bio_alloc_bioset(gfp_mask, 0, &ci->io->md->bs);
621 tio = container_of(clone, struct dm_target_io, clone);
622 tio->inside_dm_io = false;
625 tio->magic = DM_TIO_MAGIC;
628 tio->target_bio_nr = target_bio_nr;
633 static void free_tio(struct dm_target_io *tio)
635 if (tio->inside_dm_io)
637 bio_put(&tio->clone);
640 static bool md_in_flight_bios(struct mapped_device *md)
643 struct hd_struct *part = &dm_disk(md)->part0;
646 for_each_possible_cpu(cpu) {
647 sum += part_stat_local_read_cpu(part, in_flight[0], cpu);
648 sum += part_stat_local_read_cpu(part, in_flight[1], cpu);
654 static bool md_in_flight(struct mapped_device *md)
656 if (queue_is_mq(md->queue))
657 return blk_mq_queue_inflight(md->queue);
659 return md_in_flight_bios(md);
662 static void start_io_acct(struct dm_io *io)
664 struct mapped_device *md = io->md;
665 struct bio *bio = io->orig_bio;
667 io->start_time = jiffies;
669 generic_start_io_acct(md->queue, bio_op(bio), bio_sectors(bio),
670 &dm_disk(md)->part0);
672 if (unlikely(dm_stats_used(&md->stats)))
673 dm_stats_account_io(&md->stats, bio_data_dir(bio),
674 bio->bi_iter.bi_sector, bio_sectors(bio),
675 false, 0, &io->stats_aux);
678 static void end_io_acct(struct mapped_device *md, struct bio *bio,
679 unsigned long start_time, struct dm_stats_aux *stats_aux)
681 unsigned long duration = jiffies - start_time;
683 if (unlikely(dm_stats_used(&md->stats)))
684 dm_stats_account_io(&md->stats, bio_data_dir(bio),
685 bio->bi_iter.bi_sector, bio_sectors(bio),
686 true, duration, stats_aux);
690 generic_end_io_acct(md->queue, bio_op(bio), &dm_disk(md)->part0,
693 /* nudge anyone waiting on suspend queue */
694 if (unlikely(wq_has_sleeper(&md->wait)))
699 * Add the bio to the list of deferred io.
701 static void queue_io(struct mapped_device *md, struct bio *bio)
705 spin_lock_irqsave(&md->deferred_lock, flags);
706 bio_list_add(&md->deferred, bio);
707 spin_unlock_irqrestore(&md->deferred_lock, flags);
708 queue_work(md->wq, &md->work);
712 * Everyone (including functions in this file), should use this
713 * function to access the md->map field, and make sure they call
714 * dm_put_live_table() when finished.
716 struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
718 *srcu_idx = srcu_read_lock(&md->io_barrier);
720 return srcu_dereference(md->map, &md->io_barrier);
723 void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
725 srcu_read_unlock(&md->io_barrier, srcu_idx);
728 void dm_sync_table(struct mapped_device *md)
730 synchronize_srcu(&md->io_barrier);
731 synchronize_rcu_expedited();
735 * A fast alternative to dm_get_live_table/dm_put_live_table.
736 * The caller must not block between these two functions.
738 static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
741 return rcu_dereference(md->map);
744 static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
749 static char *_dm_claim_ptr = "I belong to device-mapper";
752 * Open a table device so we can use it as a map destination.
754 static int open_table_device(struct table_device *td, dev_t dev,
755 struct mapped_device *md)
757 struct block_device *bdev;
761 BUG_ON(td->dm_dev.bdev);
763 bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _dm_claim_ptr);
765 return PTR_ERR(bdev);
767 r = bd_link_disk_holder(bdev, dm_disk(md));
769 blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
773 td->dm_dev.bdev = bdev;
774 td->dm_dev.dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
779 * Close a table device that we've been using.
781 static void close_table_device(struct table_device *td, struct mapped_device *md)
783 if (!td->dm_dev.bdev)
786 bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
787 blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
788 put_dax(td->dm_dev.dax_dev);
789 td->dm_dev.bdev = NULL;
790 td->dm_dev.dax_dev = NULL;
793 static struct table_device *find_table_device(struct list_head *l, dev_t dev,
796 struct table_device *td;
798 list_for_each_entry(td, l, list)
799 if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
805 int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
806 struct dm_dev **result)
809 struct table_device *td;
811 mutex_lock(&md->table_devices_lock);
812 td = find_table_device(&md->table_devices, dev, mode);
814 td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id);
816 mutex_unlock(&md->table_devices_lock);
820 td->dm_dev.mode = mode;
821 td->dm_dev.bdev = NULL;
823 if ((r = open_table_device(td, dev, md))) {
824 mutex_unlock(&md->table_devices_lock);
829 format_dev_t(td->dm_dev.name, dev);
831 refcount_set(&td->count, 1);
832 list_add(&td->list, &md->table_devices);
834 refcount_inc(&td->count);
836 mutex_unlock(&md->table_devices_lock);
838 *result = &td->dm_dev;
841 EXPORT_SYMBOL_GPL(dm_get_table_device);
843 void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
845 struct table_device *td = container_of(d, struct table_device, dm_dev);
847 mutex_lock(&md->table_devices_lock);
848 if (refcount_dec_and_test(&td->count)) {
849 close_table_device(td, md);
853 mutex_unlock(&md->table_devices_lock);
855 EXPORT_SYMBOL(dm_put_table_device);
857 static void free_table_devices(struct list_head *devices)
859 struct list_head *tmp, *next;
861 list_for_each_safe(tmp, next, devices) {
862 struct table_device *td = list_entry(tmp, struct table_device, list);
864 DMWARN("dm_destroy: %s still exists with %d references",
865 td->dm_dev.name, refcount_read(&td->count));
871 * Get the geometry associated with a dm device
873 int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
881 * Set the geometry of a device.
883 int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
885 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
887 if (geo->start > sz) {
888 DMWARN("Start sector is beyond the geometry limits.");
897 static int __noflush_suspending(struct mapped_device *md)
899 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
903 * Decrements the number of outstanding ios that a bio has been
904 * cloned into, completing the original io if necc.
906 static void dec_pending(struct dm_io *io, blk_status_t error)
909 blk_status_t io_error;
911 struct mapped_device *md = io->md;
912 unsigned long start_time = 0;
913 struct dm_stats_aux stats_aux;
915 /* Push-back supersedes any I/O errors */
916 if (unlikely(error)) {
917 spin_lock_irqsave(&io->endio_lock, flags);
918 if (!(io->status == BLK_STS_DM_REQUEUE && __noflush_suspending(md)))
920 spin_unlock_irqrestore(&io->endio_lock, flags);
923 if (atomic_dec_and_test(&io->io_count)) {
924 if (io->status == BLK_STS_DM_REQUEUE) {
926 * Target requested pushing back the I/O.
928 spin_lock_irqsave(&md->deferred_lock, flags);
929 if (__noflush_suspending(md))
930 /* NOTE early return due to BLK_STS_DM_REQUEUE below */
931 bio_list_add_head(&md->deferred, io->orig_bio);
933 /* noflush suspend was interrupted. */
934 io->status = BLK_STS_IOERR;
935 spin_unlock_irqrestore(&md->deferred_lock, flags);
938 io_error = io->status;
940 start_time = io->start_time;
941 stats_aux = io->stats_aux;
943 end_io_acct(md, bio, start_time, &stats_aux);
945 if (io_error == BLK_STS_DM_REQUEUE)
948 if ((bio->bi_opf & REQ_PREFLUSH) && bio->bi_iter.bi_size) {
950 * Preflush done for flush with data, reissue
951 * without REQ_PREFLUSH.
953 bio->bi_opf &= ~REQ_PREFLUSH;
956 /* done with normal IO or empty flush */
958 bio->bi_status = io_error;
964 void disable_discard(struct mapped_device *md)
966 struct queue_limits *limits = dm_get_queue_limits(md);
968 /* device doesn't really support DISCARD, disable it */
969 limits->max_discard_sectors = 0;
970 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, md->queue);
973 void disable_write_same(struct mapped_device *md)
975 struct queue_limits *limits = dm_get_queue_limits(md);
977 /* device doesn't really support WRITE SAME, disable it */
978 limits->max_write_same_sectors = 0;
981 void disable_write_zeroes(struct mapped_device *md)
983 struct queue_limits *limits = dm_get_queue_limits(md);
985 /* device doesn't really support WRITE ZEROES, disable it */
986 limits->max_write_zeroes_sectors = 0;
989 static bool swap_bios_limit(struct dm_target *ti, struct bio *bio)
991 return unlikely((bio->bi_opf & REQ_SWAP) != 0) && unlikely(ti->limit_swap_bios);
994 static void clone_endio(struct bio *bio)
996 blk_status_t error = bio->bi_status;
997 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
998 struct dm_io *io = tio->io;
999 struct mapped_device *md = tio->io->md;
1000 dm_endio_fn endio = tio->ti->type->end_io;
1002 if (unlikely(error == BLK_STS_TARGET)) {
1003 if (bio_op(bio) == REQ_OP_DISCARD &&
1004 !bio->bi_disk->queue->limits.max_discard_sectors)
1005 disable_discard(md);
1006 else if (bio_op(bio) == REQ_OP_WRITE_SAME &&
1007 !bio->bi_disk->queue->limits.max_write_same_sectors)
1008 disable_write_same(md);
1009 else if (bio_op(bio) == REQ_OP_WRITE_ZEROES &&
1010 !bio->bi_disk->queue->limits.max_write_zeroes_sectors)
1011 disable_write_zeroes(md);
1015 int r = endio(tio->ti, bio, &error);
1017 case DM_ENDIO_REQUEUE:
1018 error = BLK_STS_DM_REQUEUE;
1022 case DM_ENDIO_INCOMPLETE:
1023 /* The target will handle the io */
1026 DMWARN("unimplemented target endio return value: %d", r);
1031 if (unlikely(swap_bios_limit(tio->ti, bio))) {
1032 struct mapped_device *md = io->md;
1033 up(&md->swap_bios_semaphore);
1037 dec_pending(io, error);
1041 * Return maximum size of I/O possible at the supplied sector up to the current
1044 static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
1046 sector_t target_offset = dm_target_offset(ti, sector);
1048 return ti->len - target_offset;
1051 static sector_t max_io_len(sector_t sector, struct dm_target *ti)
1053 sector_t len = max_io_len_target_boundary(sector, ti);
1054 sector_t offset, max_len;
1057 * Does the target need to split even further?
1059 if (ti->max_io_len) {
1060 offset = dm_target_offset(ti, sector);
1061 if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
1062 max_len = sector_div(offset, ti->max_io_len);
1064 max_len = offset & (ti->max_io_len - 1);
1065 max_len = ti->max_io_len - max_len;
1074 int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
1076 if (len > UINT_MAX) {
1077 DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1078 (unsigned long long)len, UINT_MAX);
1079 ti->error = "Maximum size of target IO is too large";
1083 ti->max_io_len = (uint32_t) len;
1087 EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
1089 static struct dm_target *dm_dax_get_live_target(struct mapped_device *md,
1090 sector_t sector, int *srcu_idx)
1091 __acquires(md->io_barrier)
1093 struct dm_table *map;
1094 struct dm_target *ti;
1096 map = dm_get_live_table(md, srcu_idx);
1100 ti = dm_table_find_target(map, sector);
1107 static long dm_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
1108 long nr_pages, void **kaddr, pfn_t *pfn)
1110 struct mapped_device *md = dax_get_private(dax_dev);
1111 sector_t sector = pgoff * PAGE_SECTORS;
1112 struct dm_target *ti;
1113 long len, ret = -EIO;
1116 ti = dm_dax_get_live_target(md, sector, &srcu_idx);
1120 if (!ti->type->direct_access)
1122 len = max_io_len(sector, ti) / PAGE_SECTORS;
1125 nr_pages = min(len, nr_pages);
1126 ret = ti->type->direct_access(ti, pgoff, nr_pages, kaddr, pfn);
1129 dm_put_live_table(md, srcu_idx);
1134 static bool dm_dax_supported(struct dax_device *dax_dev, struct block_device *bdev,
1135 int blocksize, sector_t start, sector_t len)
1137 struct mapped_device *md = dax_get_private(dax_dev);
1138 struct dm_table *map;
1142 map = dm_get_live_table(md, &srcu_idx);
1146 ret = dm_table_supports_dax(map, device_not_dax_capable, &blocksize);
1149 dm_put_live_table(md, srcu_idx);
1154 static size_t dm_dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff,
1155 void *addr, size_t bytes, struct iov_iter *i)
1157 struct mapped_device *md = dax_get_private(dax_dev);
1158 sector_t sector = pgoff * PAGE_SECTORS;
1159 struct dm_target *ti;
1163 ti = dm_dax_get_live_target(md, sector, &srcu_idx);
1167 if (!ti->type->dax_copy_from_iter) {
1168 ret = copy_from_iter(addr, bytes, i);
1171 ret = ti->type->dax_copy_from_iter(ti, pgoff, addr, bytes, i);
1173 dm_put_live_table(md, srcu_idx);
1178 static size_t dm_dax_copy_to_iter(struct dax_device *dax_dev, pgoff_t pgoff,
1179 void *addr, size_t bytes, struct iov_iter *i)
1181 struct mapped_device *md = dax_get_private(dax_dev);
1182 sector_t sector = pgoff * PAGE_SECTORS;
1183 struct dm_target *ti;
1187 ti = dm_dax_get_live_target(md, sector, &srcu_idx);
1191 if (!ti->type->dax_copy_to_iter) {
1192 ret = copy_to_iter(addr, bytes, i);
1195 ret = ti->type->dax_copy_to_iter(ti, pgoff, addr, bytes, i);
1197 dm_put_live_table(md, srcu_idx);
1203 * A target may call dm_accept_partial_bio only from the map routine. It is
1204 * allowed for all bio types except REQ_PREFLUSH and REQ_OP_ZONE_RESET.
1206 * dm_accept_partial_bio informs the dm that the target only wants to process
1207 * additional n_sectors sectors of the bio and the rest of the data should be
1208 * sent in a next bio.
1210 * A diagram that explains the arithmetics:
1211 * +--------------------+---------------+-------+
1213 * +--------------------+---------------+-------+
1215 * <-------------- *tio->len_ptr --------------->
1216 * <------- bi_size ------->
1219 * Region 1 was already iterated over with bio_advance or similar function.
1220 * (it may be empty if the target doesn't use bio_advance)
1221 * Region 2 is the remaining bio size that the target wants to process.
1222 * (it may be empty if region 1 is non-empty, although there is no reason
1224 * The target requires that region 3 is to be sent in the next bio.
1226 * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
1227 * the partially processed part (the sum of regions 1+2) must be the same for all
1228 * copies of the bio.
1230 void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
1232 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
1233 unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
1234 BUG_ON(bio->bi_opf & REQ_PREFLUSH);
1235 BUG_ON(bi_size > *tio->len_ptr);
1236 BUG_ON(n_sectors > bi_size);
1237 *tio->len_ptr -= bi_size - n_sectors;
1238 bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
1240 EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
1243 * The zone descriptors obtained with a zone report indicate
1244 * zone positions within the underlying device of the target. The zone
1245 * descriptors must be remapped to match their position within the dm device.
1246 * The caller target should obtain the zones information using
1247 * blkdev_report_zones() to ensure that remapping for partition offset is
1250 void dm_remap_zone_report(struct dm_target *ti, sector_t start,
1251 struct blk_zone *zones, unsigned int *nr_zones)
1253 #ifdef CONFIG_BLK_DEV_ZONED
1254 struct blk_zone *zone;
1255 unsigned int nrz = *nr_zones;
1259 * Remap the start sector and write pointer position of the zones in
1260 * the array. Since we may have obtained from the target underlying
1261 * device more zones that the target size, also adjust the number
1264 for (i = 0; i < nrz; i++) {
1266 if (zone->start >= start + ti->len) {
1267 memset(zone, 0, sizeof(struct blk_zone) * (nrz - i));
1271 zone->start = zone->start + ti->begin - start;
1272 if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL)
1275 if (zone->cond == BLK_ZONE_COND_FULL)
1276 zone->wp = zone->start + zone->len;
1277 else if (zone->cond == BLK_ZONE_COND_EMPTY)
1278 zone->wp = zone->start;
1280 zone->wp = zone->wp + ti->begin - start;
1284 #else /* !CONFIG_BLK_DEV_ZONED */
1288 EXPORT_SYMBOL_GPL(dm_remap_zone_report);
1290 static noinline void __set_swap_bios_limit(struct mapped_device *md, int latch)
1292 mutex_lock(&md->swap_bios_lock);
1293 while (latch < md->swap_bios) {
1295 down(&md->swap_bios_semaphore);
1298 while (latch > md->swap_bios) {
1300 up(&md->swap_bios_semaphore);
1303 mutex_unlock(&md->swap_bios_lock);
1306 static blk_qc_t __map_bio(struct dm_target_io *tio)
1310 struct bio *clone = &tio->clone;
1311 struct dm_io *io = tio->io;
1312 struct mapped_device *md = io->md;
1313 struct dm_target *ti = tio->ti;
1314 blk_qc_t ret = BLK_QC_T_NONE;
1316 clone->bi_end_io = clone_endio;
1319 * Map the clone. If r == 0 we don't need to do
1320 * anything, the target has assumed ownership of
1323 atomic_inc(&io->io_count);
1324 sector = clone->bi_iter.bi_sector;
1326 if (unlikely(swap_bios_limit(ti, clone))) {
1327 int latch = get_swap_bios();
1328 if (unlikely(latch != md->swap_bios))
1329 __set_swap_bios_limit(md, latch);
1330 down(&md->swap_bios_semaphore);
1333 r = ti->type->map(ti, clone);
1335 case DM_MAPIO_SUBMITTED:
1337 case DM_MAPIO_REMAPPED:
1338 /* the bio has been remapped so dispatch it */
1339 trace_block_bio_remap(clone->bi_disk->queue, clone,
1340 bio_dev(io->orig_bio), sector);
1341 ret = generic_make_request(clone);
1344 if (unlikely(swap_bios_limit(ti, clone)))
1345 up(&md->swap_bios_semaphore);
1347 dec_pending(io, BLK_STS_IOERR);
1349 case DM_MAPIO_REQUEUE:
1350 if (unlikely(swap_bios_limit(ti, clone)))
1351 up(&md->swap_bios_semaphore);
1353 dec_pending(io, BLK_STS_DM_REQUEUE);
1356 DMWARN("unimplemented target map return value: %d", r);
1363 static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
1365 bio->bi_iter.bi_sector = sector;
1366 bio->bi_iter.bi_size = to_bytes(len);
1370 * Creates a bio that consists of range of complete bvecs.
1372 static int clone_bio(struct dm_target_io *tio, struct bio *bio,
1373 sector_t sector, unsigned len)
1375 struct bio *clone = &tio->clone;
1377 __bio_clone_fast(clone, bio);
1379 if (bio_integrity(bio)) {
1382 if (unlikely(!dm_target_has_integrity(tio->ti->type) &&
1383 !dm_target_passes_integrity(tio->ti->type))) {
1384 DMWARN("%s: the target %s doesn't support integrity data.",
1385 dm_device_name(tio->io->md),
1386 tio->ti->type->name);
1390 r = bio_integrity_clone(clone, bio, GFP_NOIO);
1395 bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
1396 clone->bi_iter.bi_size = to_bytes(len);
1398 if (bio_integrity(bio))
1399 bio_integrity_trim(clone);
1404 static void alloc_multiple_bios(struct bio_list *blist, struct clone_info *ci,
1405 struct dm_target *ti, unsigned num_bios)
1407 struct dm_target_io *tio;
1413 if (num_bios == 1) {
1414 tio = alloc_tio(ci, ti, 0, GFP_NOIO);
1415 bio_list_add(blist, &tio->clone);
1419 for (try = 0; try < 2; try++) {
1424 mutex_lock(&ci->io->md->table_devices_lock);
1425 for (bio_nr = 0; bio_nr < num_bios; bio_nr++) {
1426 tio = alloc_tio(ci, ti, bio_nr, try ? GFP_NOIO : GFP_NOWAIT);
1430 bio_list_add(blist, &tio->clone);
1433 mutex_unlock(&ci->io->md->table_devices_lock);
1434 if (bio_nr == num_bios)
1437 while ((bio = bio_list_pop(blist))) {
1438 tio = container_of(bio, struct dm_target_io, clone);
1444 static blk_qc_t __clone_and_map_simple_bio(struct clone_info *ci,
1445 struct dm_target_io *tio, unsigned *len)
1447 struct bio *clone = &tio->clone;
1451 __bio_clone_fast(clone, ci->bio);
1453 bio_setup_sector(clone, ci->sector, *len);
1455 return __map_bio(tio);
1458 static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
1459 unsigned num_bios, unsigned *len)
1461 struct bio_list blist = BIO_EMPTY_LIST;
1463 struct dm_target_io *tio;
1465 alloc_multiple_bios(&blist, ci, ti, num_bios);
1467 while ((bio = bio_list_pop(&blist))) {
1468 tio = container_of(bio, struct dm_target_io, clone);
1469 (void) __clone_and_map_simple_bio(ci, tio, len);
1473 static int __send_empty_flush(struct clone_info *ci)
1475 unsigned target_nr = 0;
1476 struct dm_target *ti;
1479 * Empty flush uses a statically initialized bio, as the base for
1480 * cloning. However, blkg association requires that a bdev is
1481 * associated with a gendisk, which doesn't happen until the bdev is
1482 * opened. So, blkg association is done at issue time of the flush
1483 * rather than when the device is created in alloc_dev().
1485 bio_set_dev(ci->bio, ci->io->md->bdev);
1487 BUG_ON(bio_has_data(ci->bio));
1488 while ((ti = dm_table_get_target(ci->map, target_nr++)))
1489 __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
1493 static int __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
1494 sector_t sector, unsigned *len)
1496 struct bio *bio = ci->bio;
1497 struct dm_target_io *tio;
1500 tio = alloc_tio(ci, ti, 0, GFP_NOIO);
1502 r = clone_bio(tio, bio, sector, *len);
1507 (void) __map_bio(tio);
1512 typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
1514 static unsigned get_num_discard_bios(struct dm_target *ti)
1516 return ti->num_discard_bios;
1519 static unsigned get_num_secure_erase_bios(struct dm_target *ti)
1521 return ti->num_secure_erase_bios;
1524 static unsigned get_num_write_same_bios(struct dm_target *ti)
1526 return ti->num_write_same_bios;
1529 static unsigned get_num_write_zeroes_bios(struct dm_target *ti)
1531 return ti->num_write_zeroes_bios;
1534 static int __send_changing_extent_only(struct clone_info *ci, struct dm_target *ti,
1540 * Even though the device advertised support for this type of
1541 * request, that does not mean every target supports it, and
1542 * reconfiguration might also have changed that since the
1543 * check was performed.
1548 len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
1550 __send_duplicate_bios(ci, ti, num_bios, &len);
1553 ci->sector_count -= len;
1558 static int __send_discard(struct clone_info *ci, struct dm_target *ti)
1560 return __send_changing_extent_only(ci, ti, get_num_discard_bios(ti));
1563 static int __send_secure_erase(struct clone_info *ci, struct dm_target *ti)
1565 return __send_changing_extent_only(ci, ti, get_num_secure_erase_bios(ti));
1568 static int __send_write_same(struct clone_info *ci, struct dm_target *ti)
1570 return __send_changing_extent_only(ci, ti, get_num_write_same_bios(ti));
1573 static int __send_write_zeroes(struct clone_info *ci, struct dm_target *ti)
1575 return __send_changing_extent_only(ci, ti, get_num_write_zeroes_bios(ti));
1578 static bool is_abnormal_io(struct bio *bio)
1582 switch (bio_op(bio)) {
1583 case REQ_OP_DISCARD:
1584 case REQ_OP_SECURE_ERASE:
1585 case REQ_OP_WRITE_SAME:
1586 case REQ_OP_WRITE_ZEROES:
1594 static bool __process_abnormal_io(struct clone_info *ci, struct dm_target *ti,
1597 struct bio *bio = ci->bio;
1599 if (bio_op(bio) == REQ_OP_DISCARD)
1600 *result = __send_discard(ci, ti);
1601 else if (bio_op(bio) == REQ_OP_SECURE_ERASE)
1602 *result = __send_secure_erase(ci, ti);
1603 else if (bio_op(bio) == REQ_OP_WRITE_SAME)
1604 *result = __send_write_same(ci, ti);
1605 else if (bio_op(bio) == REQ_OP_WRITE_ZEROES)
1606 *result = __send_write_zeroes(ci, ti);
1614 * Select the correct strategy for processing a non-flush bio.
1616 static int __split_and_process_non_flush(struct clone_info *ci)
1618 struct dm_target *ti;
1622 ti = dm_table_find_target(ci->map, ci->sector);
1626 if (__process_abnormal_io(ci, ti, &r))
1629 len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count);
1631 r = __clone_and_map_data_bio(ci, ti, ci->sector, &len);
1636 ci->sector_count -= len;
1641 static void init_clone_info(struct clone_info *ci, struct mapped_device *md,
1642 struct dm_table *map, struct bio *bio)
1645 ci->io = alloc_io(md, bio);
1646 ci->sector = bio->bi_iter.bi_sector;
1649 #define __dm_part_stat_sub(part, field, subnd) \
1650 (part_stat_get(part, field) -= (subnd))
1653 * Entry point to split a bio into clones and submit them to the targets.
1655 static blk_qc_t __split_and_process_bio(struct mapped_device *md,
1656 struct dm_table *map, struct bio *bio)
1658 struct clone_info ci;
1659 blk_qc_t ret = BLK_QC_T_NONE;
1662 init_clone_info(&ci, md, map, bio);
1664 if (bio->bi_opf & REQ_PREFLUSH) {
1665 struct bio flush_bio;
1668 * Use an on-stack bio for this, it's safe since we don't
1669 * need to reference it after submit. It's just used as
1670 * the basis for the clone(s).
1672 bio_init(&flush_bio, NULL, 0);
1673 flush_bio.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC;
1674 ci.bio = &flush_bio;
1675 ci.sector_count = 0;
1676 error = __send_empty_flush(&ci);
1678 /* dec_pending submits any data associated with flush */
1679 } else if (bio_op(bio) == REQ_OP_ZONE_RESET) {
1681 ci.sector_count = 0;
1682 error = __split_and_process_non_flush(&ci);
1685 ci.sector_count = bio_sectors(bio);
1686 while (ci.sector_count && !error) {
1687 error = __split_and_process_non_flush(&ci);
1688 if (current->bio_list && ci.sector_count && !error) {
1690 * Remainder must be passed to generic_make_request()
1691 * so that it gets handled *after* bios already submitted
1692 * have been completely processed.
1693 * We take a clone of the original to store in
1694 * ci.io->orig_bio to be used by end_io_acct() and
1695 * for dec_pending to use for completion handling.
1697 struct bio *b = bio_split(bio, bio_sectors(bio) - ci.sector_count,
1698 GFP_NOIO, &md->queue->bio_split);
1699 ci.io->orig_bio = b;
1702 * Adjust IO stats for each split, otherwise upon queue
1703 * reentry there will be redundant IO accounting.
1704 * NOTE: this is a stop-gap fix, a proper fix involves
1705 * significant refactoring of DM core's bio splitting
1706 * (by eliminating DM's splitting and just using bio_split)
1709 __dm_part_stat_sub(&dm_disk(md)->part0,
1710 sectors[op_stat_group(bio_op(bio))], ci.sector_count);
1714 trace_block_split(md->queue, b, bio->bi_iter.bi_sector);
1715 ret = generic_make_request(bio);
1721 /* drop the extra reference count */
1722 dec_pending(ci.io, errno_to_blk_status(error));
1726 static blk_qc_t dm_process_bio(struct mapped_device *md,
1727 struct dm_table *map, struct bio *bio)
1729 blk_qc_t ret = BLK_QC_T_NONE;
1730 struct dm_target *ti = md->immutable_target;
1732 if (unlikely(!map)) {
1738 ti = dm_table_find_target(map, bio->bi_iter.bi_sector);
1739 if (unlikely(!ti)) {
1746 * If in ->make_request_fn we need to use blk_queue_split(), otherwise
1747 * queue_limits for abnormal requests (e.g. discard, writesame, etc)
1750 if (current->bio_list) {
1751 if (is_abnormal_io(bio))
1752 blk_queue_split(md->queue, &bio);
1753 /* regular IO is split by __split_and_process_bio */
1756 return __split_and_process_bio(md, map, bio);
1759 static blk_qc_t dm_make_request(struct request_queue *q, struct bio *bio)
1761 struct mapped_device *md = q->queuedata;
1762 blk_qc_t ret = BLK_QC_T_NONE;
1764 struct dm_table *map;
1766 map = dm_get_live_table(md, &srcu_idx);
1768 /* if we're suspended, we have to queue this io for later */
1769 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
1770 dm_put_live_table(md, srcu_idx);
1772 if (!(bio->bi_opf & REQ_RAHEAD))
1779 ret = dm_process_bio(md, map, bio);
1781 dm_put_live_table(md, srcu_idx);
1785 static int dm_any_congested(void *congested_data, int bdi_bits)
1788 struct mapped_device *md = congested_data;
1789 struct dm_table *map;
1791 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
1792 if (dm_request_based(md)) {
1794 * With request-based DM we only need to check the
1795 * top-level queue for congestion.
1797 struct backing_dev_info *bdi = md->queue->backing_dev_info;
1798 r = bdi->wb.congested->state & bdi_bits;
1800 map = dm_get_live_table_fast(md);
1802 r = dm_table_any_congested(map, bdi_bits);
1803 dm_put_live_table_fast(md);
1810 /*-----------------------------------------------------------------
1811 * An IDR is used to keep track of allocated minor numbers.
1812 *---------------------------------------------------------------*/
1813 static void free_minor(int minor)
1815 spin_lock(&_minor_lock);
1816 idr_remove(&_minor_idr, minor);
1817 spin_unlock(&_minor_lock);
1821 * See if the device with a specific minor # is free.
1823 static int specific_minor(int minor)
1827 if (minor >= (1 << MINORBITS))
1830 idr_preload(GFP_KERNEL);
1831 spin_lock(&_minor_lock);
1833 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
1835 spin_unlock(&_minor_lock);
1838 return r == -ENOSPC ? -EBUSY : r;
1842 static int next_free_minor(int *minor)
1846 idr_preload(GFP_KERNEL);
1847 spin_lock(&_minor_lock);
1849 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
1851 spin_unlock(&_minor_lock);
1859 static const struct block_device_operations dm_blk_dops;
1860 static const struct dax_operations dm_dax_ops;
1862 static void dm_wq_work(struct work_struct *work);
1864 static void cleanup_mapped_device(struct mapped_device *md)
1867 destroy_workqueue(md->wq);
1868 bioset_exit(&md->bs);
1869 bioset_exit(&md->io_bs);
1872 kill_dax(md->dax_dev);
1873 put_dax(md->dax_dev);
1878 spin_lock(&_minor_lock);
1879 md->disk->private_data = NULL;
1880 spin_unlock(&_minor_lock);
1881 del_gendisk(md->disk);
1886 blk_cleanup_queue(md->queue);
1888 cleanup_srcu_struct(&md->io_barrier);
1895 mutex_destroy(&md->suspend_lock);
1896 mutex_destroy(&md->type_lock);
1897 mutex_destroy(&md->table_devices_lock);
1898 mutex_destroy(&md->swap_bios_lock);
1900 dm_mq_cleanup_mapped_device(md);
1904 * Allocate and initialise a blank device with a given minor.
1906 static struct mapped_device *alloc_dev(int minor)
1908 int r, numa_node_id = dm_get_numa_node();
1909 struct mapped_device *md;
1912 md = kvzalloc_node(sizeof(*md), GFP_KERNEL, numa_node_id);
1914 DMWARN("unable to allocate device, out of memory.");
1918 if (!try_module_get(THIS_MODULE))
1919 goto bad_module_get;
1921 /* get a minor number for the dev */
1922 if (minor == DM_ANY_MINOR)
1923 r = next_free_minor(&minor);
1925 r = specific_minor(minor);
1929 r = init_srcu_struct(&md->io_barrier);
1931 goto bad_io_barrier;
1933 md->numa_node_id = numa_node_id;
1934 md->init_tio_pdu = false;
1935 md->type = DM_TYPE_NONE;
1936 mutex_init(&md->suspend_lock);
1937 mutex_init(&md->type_lock);
1938 mutex_init(&md->table_devices_lock);
1939 spin_lock_init(&md->deferred_lock);
1940 atomic_set(&md->holders, 1);
1941 atomic_set(&md->open_count, 0);
1942 atomic_set(&md->event_nr, 0);
1943 atomic_set(&md->uevent_seq, 0);
1944 INIT_LIST_HEAD(&md->uevent_list);
1945 INIT_LIST_HEAD(&md->table_devices);
1946 spin_lock_init(&md->uevent_lock);
1948 md->queue = blk_alloc_queue_node(GFP_KERNEL, numa_node_id);
1951 md->queue->queuedata = md;
1953 * default to bio-based required ->make_request_fn until DM
1954 * table is loaded and md->type established. If request-based
1955 * table is loaded: blk-mq will override accordingly.
1957 blk_queue_make_request(md->queue, dm_make_request);
1959 md->disk = alloc_disk_node(1, md->numa_node_id);
1963 init_waitqueue_head(&md->wait);
1964 INIT_WORK(&md->work, dm_wq_work);
1965 init_waitqueue_head(&md->eventq);
1966 init_completion(&md->kobj_holder.completion);
1968 md->swap_bios = get_swap_bios();
1969 sema_init(&md->swap_bios_semaphore, md->swap_bios);
1970 mutex_init(&md->swap_bios_lock);
1972 md->disk->major = _major;
1973 md->disk->first_minor = minor;
1974 md->disk->fops = &dm_blk_dops;
1975 md->disk->queue = md->queue;
1976 md->disk->private_data = md;
1977 sprintf(md->disk->disk_name, "dm-%d", minor);
1979 if (IS_ENABLED(CONFIG_DAX_DRIVER)) {
1980 md->dax_dev = alloc_dax(md, md->disk->disk_name,
1986 add_disk_no_queue_reg(md->disk);
1987 format_dev_t(md->name, MKDEV(_major, minor));
1989 md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
1993 md->bdev = bdget_disk(md->disk, 0);
1997 r = dm_stats_init(&md->stats);
2001 /* Populate the mapping, nobody knows we exist yet */
2002 spin_lock(&_minor_lock);
2003 old_md = idr_replace(&_minor_idr, md, minor);
2004 spin_unlock(&_minor_lock);
2006 BUG_ON(old_md != MINOR_ALLOCED);
2011 cleanup_mapped_device(md);
2015 module_put(THIS_MODULE);
2021 static void unlock_fs(struct mapped_device *md);
2023 static void free_dev(struct mapped_device *md)
2025 int minor = MINOR(disk_devt(md->disk));
2029 cleanup_mapped_device(md);
2031 free_table_devices(&md->table_devices);
2032 dm_stats_cleanup(&md->stats);
2035 module_put(THIS_MODULE);
2039 static int __bind_mempools(struct mapped_device *md, struct dm_table *t)
2041 struct dm_md_mempools *p = dm_table_get_md_mempools(t);
2044 if (dm_table_bio_based(t)) {
2046 * The md may already have mempools that need changing.
2047 * If so, reload bioset because front_pad may have changed
2048 * because a different table was loaded.
2050 bioset_exit(&md->bs);
2051 bioset_exit(&md->io_bs);
2053 } else if (bioset_initialized(&md->bs)) {
2055 * There's no need to reload with request-based dm
2056 * because the size of front_pad doesn't change.
2057 * Note for future: If you are to reload bioset,
2058 * prep-ed requests in the queue may refer
2059 * to bio from the old bioset, so you must walk
2060 * through the queue to unprep.
2066 bioset_initialized(&md->bs) ||
2067 bioset_initialized(&md->io_bs));
2069 ret = bioset_init_from_src(&md->bs, &p->bs);
2072 ret = bioset_init_from_src(&md->io_bs, &p->io_bs);
2074 bioset_exit(&md->bs);
2076 /* mempool bind completed, no longer need any mempools in the table */
2077 dm_table_free_md_mempools(t);
2082 * Bind a table to the device.
2084 static void event_callback(void *context)
2086 unsigned long flags;
2088 struct mapped_device *md = (struct mapped_device *) context;
2090 spin_lock_irqsave(&md->uevent_lock, flags);
2091 list_splice_init(&md->uevent_list, &uevents);
2092 spin_unlock_irqrestore(&md->uevent_lock, flags);
2094 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
2096 atomic_inc(&md->event_nr);
2097 wake_up(&md->eventq);
2098 dm_issue_global_event();
2102 * Protected by md->suspend_lock obtained by dm_swap_table().
2104 static void __set_size(struct mapped_device *md, sector_t size)
2106 lockdep_assert_held(&md->suspend_lock);
2108 set_capacity(md->disk, size);
2110 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
2114 * Returns old map, which caller must destroy.
2116 static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2117 struct queue_limits *limits)
2119 struct dm_table *old_map;
2120 struct request_queue *q = md->queue;
2121 bool request_based = dm_table_request_based(t);
2125 lockdep_assert_held(&md->suspend_lock);
2127 size = dm_table_get_size(t);
2130 * Wipe any geometry if the size of the table changed.
2132 if (size != dm_get_size(md))
2133 memset(&md->geometry, 0, sizeof(md->geometry));
2135 __set_size(md, size);
2137 dm_table_event_callback(t, event_callback, md);
2140 * The queue hasn't been stopped yet, if the old table type wasn't
2141 * for request-based during suspension. So stop it to prevent
2142 * I/O mapping before resume.
2143 * This must be done before setting the queue restrictions,
2144 * because request-based dm may be run just after the setting.
2149 if (request_based) {
2151 * Leverage the fact that request-based DM targets are
2152 * immutable singletons - used to optimize dm_mq_queue_rq.
2154 md->immutable_target = dm_table_get_immutable_target(t);
2157 ret = __bind_mempools(md, t);
2159 old_map = ERR_PTR(ret);
2163 old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2164 rcu_assign_pointer(md->map, (void *)t);
2165 md->immutable_target_type = dm_table_get_immutable_target_type(t);
2167 dm_table_set_restrictions(t, q, limits);
2176 * Returns unbound table for the caller to free.
2178 static struct dm_table *__unbind(struct mapped_device *md)
2180 struct dm_table *map = rcu_dereference_protected(md->map, 1);
2185 dm_table_event_callback(map, NULL, NULL);
2186 RCU_INIT_POINTER(md->map, NULL);
2193 * Constructor for a new device.
2195 int dm_create(int minor, struct mapped_device **result)
2198 struct mapped_device *md;
2200 md = alloc_dev(minor);
2204 r = dm_sysfs_init(md);
2215 * Functions to manage md->type.
2216 * All are required to hold md->type_lock.
2218 void dm_lock_md_type(struct mapped_device *md)
2220 mutex_lock(&md->type_lock);
2223 void dm_unlock_md_type(struct mapped_device *md)
2225 mutex_unlock(&md->type_lock);
2228 void dm_set_md_type(struct mapped_device *md, enum dm_queue_mode type)
2230 BUG_ON(!mutex_is_locked(&md->type_lock));
2234 enum dm_queue_mode dm_get_md_type(struct mapped_device *md)
2239 struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
2241 return md->immutable_target_type;
2245 * The queue_limits are only valid as long as you have a reference
2248 struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2250 BUG_ON(!atomic_read(&md->holders));
2251 return &md->queue->limits;
2253 EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2255 static void dm_init_congested_fn(struct mapped_device *md)
2257 md->queue->backing_dev_info->congested_data = md;
2258 md->queue->backing_dev_info->congested_fn = dm_any_congested;
2262 * Setup the DM device's queue based on md's type
2264 int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
2267 struct queue_limits limits;
2268 enum dm_queue_mode type = dm_get_md_type(md);
2271 case DM_TYPE_REQUEST_BASED:
2272 r = dm_mq_init_request_queue(md, t);
2274 DMERR("Cannot initialize queue for request-based dm-mq mapped device");
2277 dm_init_congested_fn(md);
2279 case DM_TYPE_BIO_BASED:
2280 case DM_TYPE_DAX_BIO_BASED:
2281 dm_init_congested_fn(md);
2288 r = dm_calculate_queue_limits(t, &limits);
2290 DMERR("Cannot calculate initial queue limits");
2293 dm_table_set_restrictions(t, md->queue, &limits);
2294 blk_register_queue(md->disk);
2299 struct mapped_device *dm_get_md(dev_t dev)
2301 struct mapped_device *md;
2302 unsigned minor = MINOR(dev);
2304 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2307 spin_lock(&_minor_lock);
2309 md = idr_find(&_minor_idr, minor);
2310 if (!md || md == MINOR_ALLOCED || (MINOR(disk_devt(dm_disk(md))) != minor) ||
2311 test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) {
2317 spin_unlock(&_minor_lock);
2321 EXPORT_SYMBOL_GPL(dm_get_md);
2323 void *dm_get_mdptr(struct mapped_device *md)
2325 return md->interface_ptr;
2328 void dm_set_mdptr(struct mapped_device *md, void *ptr)
2330 md->interface_ptr = ptr;
2333 void dm_get(struct mapped_device *md)
2335 atomic_inc(&md->holders);
2336 BUG_ON(test_bit(DMF_FREEING, &md->flags));
2339 int dm_hold(struct mapped_device *md)
2341 spin_lock(&_minor_lock);
2342 if (test_bit(DMF_FREEING, &md->flags)) {
2343 spin_unlock(&_minor_lock);
2347 spin_unlock(&_minor_lock);
2350 EXPORT_SYMBOL_GPL(dm_hold);
2352 const char *dm_device_name(struct mapped_device *md)
2356 EXPORT_SYMBOL_GPL(dm_device_name);
2358 static void __dm_destroy(struct mapped_device *md, bool wait)
2360 struct dm_table *map;
2365 spin_lock(&_minor_lock);
2366 idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2367 set_bit(DMF_FREEING, &md->flags);
2368 spin_unlock(&_minor_lock);
2370 blk_set_queue_dying(md->queue);
2373 * Take suspend_lock so that presuspend and postsuspend methods
2374 * do not race with internal suspend.
2376 mutex_lock(&md->suspend_lock);
2377 map = dm_get_live_table(md, &srcu_idx);
2378 if (!dm_suspended_md(md)) {
2379 dm_table_presuspend_targets(map);
2380 set_bit(DMF_SUSPENDED, &md->flags);
2381 set_bit(DMF_POST_SUSPENDING, &md->flags);
2382 dm_table_postsuspend_targets(map);
2384 /* dm_put_live_table must be before msleep, otherwise deadlock is possible */
2385 dm_put_live_table(md, srcu_idx);
2386 mutex_unlock(&md->suspend_lock);
2389 * Rare, but there may be I/O requests still going to complete,
2390 * for example. Wait for all references to disappear.
2391 * No one should increment the reference count of the mapped_device,
2392 * after the mapped_device state becomes DMF_FREEING.
2395 while (atomic_read(&md->holders))
2397 else if (atomic_read(&md->holders))
2398 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2399 dm_device_name(md), atomic_read(&md->holders));
2402 dm_table_destroy(__unbind(md));
2406 void dm_destroy(struct mapped_device *md)
2408 __dm_destroy(md, true);
2411 void dm_destroy_immediate(struct mapped_device *md)
2413 __dm_destroy(md, false);
2416 void dm_put(struct mapped_device *md)
2418 atomic_dec(&md->holders);
2420 EXPORT_SYMBOL_GPL(dm_put);
2422 static int dm_wait_for_completion(struct mapped_device *md, long task_state)
2428 prepare_to_wait(&md->wait, &wait, task_state);
2430 if (!md_in_flight(md))
2433 if (signal_pending_state(task_state, current)) {
2440 finish_wait(&md->wait, &wait);
2448 * Process the deferred bios
2450 static void dm_wq_work(struct work_struct *work)
2452 struct mapped_device *md = container_of(work, struct mapped_device,
2456 struct dm_table *map;
2458 map = dm_get_live_table(md, &srcu_idx);
2460 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2461 spin_lock_irq(&md->deferred_lock);
2462 c = bio_list_pop(&md->deferred);
2463 spin_unlock_irq(&md->deferred_lock);
2468 if (dm_request_based(md))
2469 (void) generic_make_request(c);
2471 (void) dm_process_bio(md, map, c);
2474 dm_put_live_table(md, srcu_idx);
2477 static void dm_queue_flush(struct mapped_device *md)
2479 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2480 smp_mb__after_atomic();
2481 queue_work(md->wq, &md->work);
2485 * Swap in a new table, returning the old one for the caller to destroy.
2487 struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
2489 struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
2490 struct queue_limits limits;
2493 mutex_lock(&md->suspend_lock);
2495 /* device must be suspended */
2496 if (!dm_suspended_md(md))
2500 * If the new table has no data devices, retain the existing limits.
2501 * This helps multipath with queue_if_no_path if all paths disappear,
2502 * then new I/O is queued based on these limits, and then some paths
2505 if (dm_table_has_no_data_devices(table)) {
2506 live_map = dm_get_live_table_fast(md);
2508 limits = md->queue->limits;
2509 dm_put_live_table_fast(md);
2513 r = dm_calculate_queue_limits(table, &limits);
2520 map = __bind(md, table, &limits);
2521 dm_issue_global_event();
2524 mutex_unlock(&md->suspend_lock);
2529 * Functions to lock and unlock any filesystem running on the
2532 static int lock_fs(struct mapped_device *md)
2536 WARN_ON(md->frozen_sb);
2538 md->frozen_sb = freeze_bdev(md->bdev);
2539 if (IS_ERR(md->frozen_sb)) {
2540 r = PTR_ERR(md->frozen_sb);
2541 md->frozen_sb = NULL;
2545 set_bit(DMF_FROZEN, &md->flags);
2550 static void unlock_fs(struct mapped_device *md)
2552 if (!test_bit(DMF_FROZEN, &md->flags))
2555 thaw_bdev(md->bdev, md->frozen_sb);
2556 md->frozen_sb = NULL;
2557 clear_bit(DMF_FROZEN, &md->flags);
2561 * @suspend_flags: DM_SUSPEND_LOCKFS_FLAG and/or DM_SUSPEND_NOFLUSH_FLAG
2562 * @task_state: e.g. TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE
2563 * @dmf_suspended_flag: DMF_SUSPENDED or DMF_SUSPENDED_INTERNALLY
2565 * If __dm_suspend returns 0, the device is completely quiescent
2566 * now. There is no request-processing activity. All new requests
2567 * are being added to md->deferred list.
2569 static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
2570 unsigned suspend_flags, long task_state,
2571 int dmf_suspended_flag)
2573 bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
2574 bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
2577 lockdep_assert_held(&md->suspend_lock);
2580 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2581 * This flag is cleared before dm_suspend returns.
2584 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2586 pr_debug("%s: suspending with flush\n", dm_device_name(md));
2589 * This gets reverted if there's an error later and the targets
2590 * provide the .presuspend_undo hook.
2592 dm_table_presuspend_targets(map);
2595 * Flush I/O to the device.
2596 * Any I/O submitted after lock_fs() may not be flushed.
2597 * noflush takes precedence over do_lockfs.
2598 * (lock_fs() flushes I/Os and waits for them to complete.)
2600 if (!noflush && do_lockfs) {
2603 dm_table_presuspend_undo_targets(map);
2609 * Here we must make sure that no processes are submitting requests
2610 * to target drivers i.e. no one may be executing
2611 * __split_and_process_bio. This is called from dm_request and
2614 * To get all processes out of __split_and_process_bio in dm_request,
2615 * we take the write lock. To prevent any process from reentering
2616 * __split_and_process_bio from dm_request and quiesce the thread
2617 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
2618 * flush_workqueue(md->wq).
2620 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2622 synchronize_srcu(&md->io_barrier);
2625 * Stop md->queue before flushing md->wq in case request-based
2626 * dm defers requests to md->wq from md->queue.
2628 if (dm_request_based(md))
2629 dm_stop_queue(md->queue);
2631 flush_workqueue(md->wq);
2634 * At this point no more requests are entering target request routines.
2635 * We call dm_wait_for_completion to wait for all existing requests
2638 r = dm_wait_for_completion(md, task_state);
2640 set_bit(dmf_suspended_flag, &md->flags);
2643 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2645 synchronize_srcu(&md->io_barrier);
2647 /* were we interrupted ? */
2651 if (dm_request_based(md))
2652 dm_start_queue(md->queue);
2655 dm_table_presuspend_undo_targets(map);
2656 /* pushback list is already flushed, so skip flush */
2663 * We need to be able to change a mapping table under a mounted
2664 * filesystem. For example we might want to move some data in
2665 * the background. Before the table can be swapped with
2666 * dm_bind_table, dm_suspend must be called to flush any in
2667 * flight bios and ensure that any further io gets deferred.
2670 * Suspend mechanism in request-based dm.
2672 * 1. Flush all I/Os by lock_fs() if needed.
2673 * 2. Stop dispatching any I/O by stopping the request_queue.
2674 * 3. Wait for all in-flight I/Os to be completed or requeued.
2676 * To abort suspend, start the request_queue.
2678 int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
2680 struct dm_table *map = NULL;
2684 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2686 if (dm_suspended_md(md)) {
2691 if (dm_suspended_internally_md(md)) {
2692 /* already internally suspended, wait for internal resume */
2693 mutex_unlock(&md->suspend_lock);
2694 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2700 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2702 r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE, DMF_SUSPENDED);
2706 set_bit(DMF_POST_SUSPENDING, &md->flags);
2707 dm_table_postsuspend_targets(map);
2708 clear_bit(DMF_POST_SUSPENDING, &md->flags);
2711 mutex_unlock(&md->suspend_lock);
2715 static int __dm_resume(struct mapped_device *md, struct dm_table *map)
2718 int r = dm_table_resume_targets(map);
2726 * Flushing deferred I/Os must be done after targets are resumed
2727 * so that mapping of targets can work correctly.
2728 * Request-based dm is queueing the deferred I/Os in its request_queue.
2730 if (dm_request_based(md))
2731 dm_start_queue(md->queue);
2738 int dm_resume(struct mapped_device *md)
2741 struct dm_table *map = NULL;
2745 mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2747 if (!dm_suspended_md(md))
2750 if (dm_suspended_internally_md(md)) {
2751 /* already internally suspended, wait for internal resume */
2752 mutex_unlock(&md->suspend_lock);
2753 r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2759 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2760 if (!map || !dm_table_get_size(map))
2763 r = __dm_resume(md, map);
2767 clear_bit(DMF_SUSPENDED, &md->flags);
2769 mutex_unlock(&md->suspend_lock);
2775 * Internal suspend/resume works like userspace-driven suspend. It waits
2776 * until all bios finish and prevents issuing new bios to the target drivers.
2777 * It may be used only from the kernel.
2780 static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
2782 struct dm_table *map = NULL;
2784 lockdep_assert_held(&md->suspend_lock);
2786 if (md->internal_suspend_count++)
2787 return; /* nested internal suspend */
2789 if (dm_suspended_md(md)) {
2790 set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2791 return; /* nest suspend */
2794 map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2797 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
2798 * supported. Properly supporting a TASK_INTERRUPTIBLE internal suspend
2799 * would require changing .presuspend to return an error -- avoid this
2800 * until there is a need for more elaborate variants of internal suspend.
2802 (void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE,
2803 DMF_SUSPENDED_INTERNALLY);
2805 set_bit(DMF_POST_SUSPENDING, &md->flags);
2806 dm_table_postsuspend_targets(map);
2807 clear_bit(DMF_POST_SUSPENDING, &md->flags);
2810 static void __dm_internal_resume(struct mapped_device *md)
2812 BUG_ON(!md->internal_suspend_count);
2814 if (--md->internal_suspend_count)
2815 return; /* resume from nested internal suspend */
2817 if (dm_suspended_md(md))
2818 goto done; /* resume from nested suspend */
2821 * NOTE: existing callers don't need to call dm_table_resume_targets
2822 * (which may fail -- so best to avoid it for now by passing NULL map)
2824 (void) __dm_resume(md, NULL);
2827 clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2828 smp_mb__after_atomic();
2829 wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
2832 void dm_internal_suspend_noflush(struct mapped_device *md)
2834 mutex_lock(&md->suspend_lock);
2835 __dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
2836 mutex_unlock(&md->suspend_lock);
2838 EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
2840 void dm_internal_resume(struct mapped_device *md)
2842 mutex_lock(&md->suspend_lock);
2843 __dm_internal_resume(md);
2844 mutex_unlock(&md->suspend_lock);
2846 EXPORT_SYMBOL_GPL(dm_internal_resume);
2849 * Fast variants of internal suspend/resume hold md->suspend_lock,
2850 * which prevents interaction with userspace-driven suspend.
2853 void dm_internal_suspend_fast(struct mapped_device *md)
2855 mutex_lock(&md->suspend_lock);
2856 if (dm_suspended_md(md) || dm_suspended_internally_md(md))
2859 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2860 synchronize_srcu(&md->io_barrier);
2861 flush_workqueue(md->wq);
2862 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2864 EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
2866 void dm_internal_resume_fast(struct mapped_device *md)
2868 if (dm_suspended_md(md) || dm_suspended_internally_md(md))
2874 mutex_unlock(&md->suspend_lock);
2876 EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
2878 /*-----------------------------------------------------------------
2879 * Event notification.
2880 *---------------------------------------------------------------*/
2881 int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
2886 char udev_cookie[DM_COOKIE_LENGTH];
2887 char *envp[] = { udev_cookie, NULL };
2889 noio_flag = memalloc_noio_save();
2892 r = kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
2894 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
2895 DM_COOKIE_ENV_VAR_NAME, cookie);
2896 r = kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
2900 memalloc_noio_restore(noio_flag);
2905 uint32_t dm_next_uevent_seq(struct mapped_device *md)
2907 return atomic_add_return(1, &md->uevent_seq);
2910 uint32_t dm_get_event_nr(struct mapped_device *md)
2912 return atomic_read(&md->event_nr);
2915 int dm_wait_event(struct mapped_device *md, int event_nr)
2917 return wait_event_interruptible(md->eventq,
2918 (event_nr != atomic_read(&md->event_nr)));
2921 void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
2923 unsigned long flags;
2925 spin_lock_irqsave(&md->uevent_lock, flags);
2926 list_add(elist, &md->uevent_list);
2927 spin_unlock_irqrestore(&md->uevent_lock, flags);
2931 * The gendisk is only valid as long as you have a reference
2934 struct gendisk *dm_disk(struct mapped_device *md)
2938 EXPORT_SYMBOL_GPL(dm_disk);
2940 struct kobject *dm_kobject(struct mapped_device *md)
2942 return &md->kobj_holder.kobj;
2945 struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2947 struct mapped_device *md;
2949 md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
2951 spin_lock(&_minor_lock);
2952 if (test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) {
2958 spin_unlock(&_minor_lock);
2963 int dm_suspended_md(struct mapped_device *md)
2965 return test_bit(DMF_SUSPENDED, &md->flags);
2968 static int dm_post_suspending_md(struct mapped_device *md)
2970 return test_bit(DMF_POST_SUSPENDING, &md->flags);
2973 int dm_suspended_internally_md(struct mapped_device *md)
2975 return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2978 int dm_test_deferred_remove_flag(struct mapped_device *md)
2980 return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
2983 int dm_suspended(struct dm_target *ti)
2985 return dm_suspended_md(dm_table_get_md(ti->table));
2987 EXPORT_SYMBOL_GPL(dm_suspended);
2989 int dm_post_suspending(struct dm_target *ti)
2991 return dm_post_suspending_md(dm_table_get_md(ti->table));
2993 EXPORT_SYMBOL_GPL(dm_post_suspending);
2995 int dm_noflush_suspending(struct dm_target *ti)
2997 return __noflush_suspending(dm_table_get_md(ti->table));
2999 EXPORT_SYMBOL_GPL(dm_noflush_suspending);
3001 struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, enum dm_queue_mode type,
3002 unsigned integrity, unsigned per_io_data_size,
3003 unsigned min_pool_size)
3005 struct dm_md_mempools *pools = kzalloc_node(sizeof(*pools), GFP_KERNEL, md->numa_node_id);
3006 unsigned int pool_size = 0;
3007 unsigned int front_pad, io_front_pad;
3014 case DM_TYPE_BIO_BASED:
3015 case DM_TYPE_DAX_BIO_BASED:
3016 pool_size = max(dm_get_reserved_bio_based_ios(), min_pool_size);
3017 front_pad = roundup(per_io_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
3018 io_front_pad = roundup(front_pad, __alignof__(struct dm_io)) + offsetof(struct dm_io, tio);
3019 ret = bioset_init(&pools->io_bs, pool_size, io_front_pad, 0);
3022 if (integrity && bioset_integrity_create(&pools->io_bs, pool_size))
3025 case DM_TYPE_REQUEST_BASED:
3026 pool_size = max(dm_get_reserved_rq_based_ios(), min_pool_size);
3027 front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
3028 /* per_io_data_size is used for blk-mq pdu at queue allocation */
3034 ret = bioset_init(&pools->bs, pool_size, front_pad, 0);
3038 if (integrity && bioset_integrity_create(&pools->bs, pool_size))
3044 dm_free_md_mempools(pools);
3049 void dm_free_md_mempools(struct dm_md_mempools *pools)
3054 bioset_exit(&pools->bs);
3055 bioset_exit(&pools->io_bs);
3067 static int dm_call_pr(struct block_device *bdev, iterate_devices_callout_fn fn,
3070 struct mapped_device *md = bdev->bd_disk->private_data;
3071 struct dm_table *table;
3072 struct dm_target *ti;
3073 int ret = -ENOTTY, srcu_idx;
3075 table = dm_get_live_table(md, &srcu_idx);
3076 if (!table || !dm_table_get_size(table))
3079 /* We only support devices that have a single target */
3080 if (dm_table_get_num_targets(table) != 1)
3082 ti = dm_table_get_target(table, 0);
3084 if (dm_suspended_md(md)) {
3090 if (!ti->type->iterate_devices)
3093 ret = ti->type->iterate_devices(ti, fn, data);
3095 dm_put_live_table(md, srcu_idx);
3100 * For register / unregister we need to manually call out to every path.
3102 static int __dm_pr_register(struct dm_target *ti, struct dm_dev *dev,
3103 sector_t start, sector_t len, void *data)
3105 struct dm_pr *pr = data;
3106 const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops;
3108 if (!ops || !ops->pr_register)
3110 return ops->pr_register(dev->bdev, pr->old_key, pr->new_key, pr->flags);
3113 static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
3124 ret = dm_call_pr(bdev, __dm_pr_register, &pr);
3125 if (ret && new_key) {
3126 /* unregister all paths if we failed to register any path */
3127 pr.old_key = new_key;
3130 pr.fail_early = false;
3131 dm_call_pr(bdev, __dm_pr_register, &pr);
3137 static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
3140 struct mapped_device *md = bdev->bd_disk->private_data;
3141 const struct pr_ops *ops;
3144 r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
3148 ops = bdev->bd_disk->fops->pr_ops;
3149 if (ops && ops->pr_reserve)
3150 r = ops->pr_reserve(bdev, key, type, flags);
3154 dm_unprepare_ioctl(md, srcu_idx);
3158 static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
3160 struct mapped_device *md = bdev->bd_disk->private_data;
3161 const struct pr_ops *ops;
3164 r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
3168 ops = bdev->bd_disk->fops->pr_ops;
3169 if (ops && ops->pr_release)
3170 r = ops->pr_release(bdev, key, type);
3174 dm_unprepare_ioctl(md, srcu_idx);
3178 static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
3179 enum pr_type type, bool abort)
3181 struct mapped_device *md = bdev->bd_disk->private_data;
3182 const struct pr_ops *ops;
3185 r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
3189 ops = bdev->bd_disk->fops->pr_ops;
3190 if (ops && ops->pr_preempt)
3191 r = ops->pr_preempt(bdev, old_key, new_key, type, abort);
3195 dm_unprepare_ioctl(md, srcu_idx);
3199 static int dm_pr_clear(struct block_device *bdev, u64 key)
3201 struct mapped_device *md = bdev->bd_disk->private_data;
3202 const struct pr_ops *ops;
3205 r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
3209 ops = bdev->bd_disk->fops->pr_ops;
3210 if (ops && ops->pr_clear)
3211 r = ops->pr_clear(bdev, key);
3215 dm_unprepare_ioctl(md, srcu_idx);
3219 static const struct pr_ops dm_pr_ops = {
3220 .pr_register = dm_pr_register,
3221 .pr_reserve = dm_pr_reserve,
3222 .pr_release = dm_pr_release,
3223 .pr_preempt = dm_pr_preempt,
3224 .pr_clear = dm_pr_clear,
3227 static const struct block_device_operations dm_blk_dops = {
3228 .open = dm_blk_open,
3229 .release = dm_blk_close,
3230 .ioctl = dm_blk_ioctl,
3231 .getgeo = dm_blk_getgeo,
3232 .report_zones = dm_blk_report_zones,
3233 .pr_ops = &dm_pr_ops,
3234 .owner = THIS_MODULE
3237 static const struct dax_operations dm_dax_ops = {
3238 .direct_access = dm_dax_direct_access,
3239 .dax_supported = dm_dax_supported,
3240 .copy_from_iter = dm_dax_copy_from_iter,
3241 .copy_to_iter = dm_dax_copy_to_iter,
3247 module_init(dm_init);
3248 module_exit(dm_exit);
3250 module_param(major, uint, 0);
3251 MODULE_PARM_DESC(major, "The major number of the device mapper");
3253 module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
3254 MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
3256 module_param(dm_numa_node, int, S_IRUGO | S_IWUSR);
3257 MODULE_PARM_DESC(dm_numa_node, "NUMA node for DM device memory allocations");
3259 module_param(swap_bios, int, S_IRUGO | S_IWUSR);
3260 MODULE_PARM_DESC(swap_bios, "Maximum allowed inflight swap IOs");
3262 MODULE_DESCRIPTION(DM_NAME " driver");
3263 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
3264 MODULE_LICENSE("GPL");