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.
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/mutex.h>
14 #include <linux/moduleparam.h>
15 #include <linux/blkpg.h>
16 #include <linux/bio.h>
17 #include <linux/mempool.h>
18 #include <linux/slab.h>
19 #include <linux/idr.h>
20 #include <linux/hdreg.h>
21 #include <linux/delay.h>
23 #include <trace/events/block.h>
25 #define DM_MSG_PREFIX "core"
29 * ratelimit state to be used in DMXXX_LIMIT().
31 DEFINE_RATELIMIT_STATE(dm_ratelimit_state,
32 DEFAULT_RATELIMIT_INTERVAL,
33 DEFAULT_RATELIMIT_BURST);
34 EXPORT_SYMBOL(dm_ratelimit_state);
38 * Cookies are numeric values sent with CHANGE and REMOVE
39 * uevents while resuming, removing or renaming the device.
41 #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
42 #define DM_COOKIE_LENGTH 24
44 static const char *_name = DM_NAME;
46 static unsigned int major = 0;
47 static unsigned int _major = 0;
49 static DEFINE_IDR(_minor_idr);
51 static DEFINE_SPINLOCK(_minor_lock);
53 static void do_deferred_remove(struct work_struct *w);
55 static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
57 static struct workqueue_struct *deferred_remove_workqueue;
61 * One of these is allocated per bio.
64 struct mapped_device *md;
68 unsigned long start_time;
69 spinlock_t endio_lock;
70 struct dm_stats_aux stats_aux;
74 * For request-based dm.
75 * One of these is allocated per request.
77 struct dm_rq_target_io {
78 struct mapped_device *md;
80 struct request *orig, clone;
86 * For request-based dm - the bio clones we allocate are embedded in these
89 * We allocate these with bio_alloc_bioset, using the front_pad parameter when
90 * the bioset is created - this means the bio has to come at the end of the
93 struct dm_rq_clone_bio_info {
95 struct dm_rq_target_io *tio;
99 union map_info *dm_get_rq_mapinfo(struct request *rq)
101 if (rq && rq->end_io_data)
102 return &((struct dm_rq_target_io *)rq->end_io_data)->info;
105 EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
107 #define MINOR_ALLOCED ((void *)-1)
110 * Bits for the md->flags field.
112 #define DMF_BLOCK_IO_FOR_SUSPEND 0
113 #define DMF_SUSPENDED 1
115 #define DMF_FREEING 3
116 #define DMF_DELETING 4
117 #define DMF_NOFLUSH_SUSPENDING 5
118 #define DMF_MERGE_IS_OPTIONAL 6
119 #define DMF_DEFERRED_REMOVE 7
122 * A dummy definition to make RCU happy.
123 * struct dm_table should never be dereferenced in this file.
130 * Work processed by per-device workqueue.
132 struct mapped_device {
133 struct srcu_struct io_barrier;
134 struct mutex suspend_lock;
139 * The current mapping.
140 * Use dm_get_live_table{_fast} or take suspend_lock for
143 struct dm_table __rcu *map;
145 struct list_head table_devices;
146 struct mutex table_devices_lock;
150 struct request_queue *queue;
152 /* Protect queue and type against concurrent access. */
153 struct mutex type_lock;
155 struct target_type *immutable_target_type;
157 struct gendisk *disk;
163 * A list of ios that arrived while we were suspended.
166 wait_queue_head_t wait;
167 struct work_struct work;
168 struct bio_list deferred;
169 spinlock_t deferred_lock;
172 * Processing queue (flush)
174 struct workqueue_struct *wq;
177 * io objects are allocated from here.
187 wait_queue_head_t eventq;
189 struct list_head uevent_list;
190 spinlock_t uevent_lock; /* Protect access to uevent_list */
193 * freeze/thaw support require holding onto a super block
195 struct super_block *frozen_sb;
196 struct block_device *bdev;
198 /* forced geometry settings */
199 struct hd_geometry geometry;
201 /* kobject and completion */
202 struct dm_kobject_holder kobj_holder;
204 /* zero-length flush that will be cloned and submitted to targets */
205 struct bio flush_bio;
207 struct dm_stats stats;
211 * For mempools pre-allocation at the table loading time.
213 struct dm_md_mempools {
218 struct table_device {
219 struct list_head list;
221 struct dm_dev dm_dev;
224 #define RESERVED_BIO_BASED_IOS 16
225 #define RESERVED_REQUEST_BASED_IOS 256
226 #define RESERVED_MAX_IOS 1024
227 static struct kmem_cache *_io_cache;
228 static struct kmem_cache *_rq_tio_cache;
231 * Bio-based DM's mempools' reserved IOs set by the user.
233 static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
236 * Request-based DM's mempools' reserved IOs set by the user.
238 static unsigned reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS;
240 static unsigned __dm_get_reserved_ios(unsigned *reserved_ios,
241 unsigned def, unsigned max)
243 unsigned ios = ACCESS_ONCE(*reserved_ios);
244 unsigned modified_ios = 0;
252 (void)cmpxchg(reserved_ios, ios, modified_ios);
259 unsigned dm_get_reserved_bio_based_ios(void)
261 return __dm_get_reserved_ios(&reserved_bio_based_ios,
262 RESERVED_BIO_BASED_IOS, RESERVED_MAX_IOS);
264 EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
266 unsigned dm_get_reserved_rq_based_ios(void)
268 return __dm_get_reserved_ios(&reserved_rq_based_ios,
269 RESERVED_REQUEST_BASED_IOS, RESERVED_MAX_IOS);
271 EXPORT_SYMBOL_GPL(dm_get_reserved_rq_based_ios);
273 static int __init local_init(void)
277 /* allocate a slab for the dm_ios */
278 _io_cache = KMEM_CACHE(dm_io, 0);
282 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
284 goto out_free_io_cache;
286 r = dm_uevent_init();
288 goto out_free_rq_tio_cache;
290 deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
291 if (!deferred_remove_workqueue) {
293 goto out_uevent_exit;
297 r = register_blkdev(_major, _name);
299 goto out_free_workqueue;
307 destroy_workqueue(deferred_remove_workqueue);
310 out_free_rq_tio_cache:
311 kmem_cache_destroy(_rq_tio_cache);
313 kmem_cache_destroy(_io_cache);
318 static void local_exit(void)
320 flush_scheduled_work();
321 destroy_workqueue(deferred_remove_workqueue);
323 kmem_cache_destroy(_rq_tio_cache);
324 kmem_cache_destroy(_io_cache);
325 unregister_blkdev(_major, _name);
330 DMINFO("cleaned up");
333 static int (*_inits[])(void) __initdata = {
344 static void (*_exits[])(void) = {
355 static int __init dm_init(void)
357 const int count = ARRAY_SIZE(_inits);
361 for (i = 0; i < count; i++) {
376 static void __exit dm_exit(void)
378 int i = ARRAY_SIZE(_exits);
384 * Should be empty by this point.
386 idr_destroy(&_minor_idr);
390 * Block device functions
392 int dm_deleting_md(struct mapped_device *md)
394 return test_bit(DMF_DELETING, &md->flags);
397 static int dm_blk_open(struct block_device *bdev, fmode_t mode)
399 struct mapped_device *md;
401 spin_lock(&_minor_lock);
403 md = bdev->bd_disk->private_data;
407 if (test_bit(DMF_FREEING, &md->flags) ||
408 dm_deleting_md(md)) {
414 atomic_inc(&md->open_count);
417 spin_unlock(&_minor_lock);
419 return md ? 0 : -ENXIO;
422 static void dm_blk_close(struct gendisk *disk, fmode_t mode)
424 struct mapped_device *md = disk->private_data;
426 spin_lock(&_minor_lock);
428 if (atomic_dec_and_test(&md->open_count) &&
429 (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
430 queue_work(deferred_remove_workqueue, &deferred_remove_work);
434 spin_unlock(&_minor_lock);
437 int dm_open_count(struct mapped_device *md)
439 return atomic_read(&md->open_count);
443 * Guarantees nothing is using the device before it's deleted.
445 int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
449 spin_lock(&_minor_lock);
451 if (dm_open_count(md)) {
454 set_bit(DMF_DEFERRED_REMOVE, &md->flags);
455 } else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
458 set_bit(DMF_DELETING, &md->flags);
460 spin_unlock(&_minor_lock);
465 int dm_cancel_deferred_remove(struct mapped_device *md)
469 spin_lock(&_minor_lock);
471 if (test_bit(DMF_DELETING, &md->flags))
474 clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
476 spin_unlock(&_minor_lock);
481 static void do_deferred_remove(struct work_struct *w)
483 dm_deferred_remove();
486 sector_t dm_get_size(struct mapped_device *md)
488 return get_capacity(md->disk);
491 struct request_queue *dm_get_md_queue(struct mapped_device *md)
496 struct dm_stats *dm_get_stats(struct mapped_device *md)
501 static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
503 struct mapped_device *md = bdev->bd_disk->private_data;
505 return dm_get_geometry(md, geo);
508 static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
509 unsigned int cmd, unsigned long arg)
511 struct mapped_device *md = bdev->bd_disk->private_data;
513 struct dm_table *map;
514 struct dm_target *tgt;
518 map = dm_get_live_table(md, &srcu_idx);
520 if (!map || !dm_table_get_size(map))
523 /* We only support devices that have a single target */
524 if (dm_table_get_num_targets(map) != 1)
527 tgt = dm_table_get_target(map, 0);
528 if (!tgt->type->ioctl)
531 if (dm_suspended_md(md)) {
536 r = tgt->type->ioctl(tgt, cmd, arg);
539 dm_put_live_table(md, srcu_idx);
541 if (r == -ENOTCONN) {
549 static struct dm_io *alloc_io(struct mapped_device *md)
551 return mempool_alloc(md->io_pool, GFP_NOIO);
554 static void free_io(struct mapped_device *md, struct dm_io *io)
556 mempool_free(io, md->io_pool);
559 static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
561 bio_put(&tio->clone);
564 static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
567 return mempool_alloc(md->io_pool, gfp_mask);
570 static void free_rq_tio(struct dm_rq_target_io *tio)
572 mempool_free(tio, tio->md->io_pool);
575 static int md_in_flight(struct mapped_device *md)
577 return atomic_read(&md->pending[READ]) +
578 atomic_read(&md->pending[WRITE]);
581 static void start_io_acct(struct dm_io *io)
583 struct mapped_device *md = io->md;
584 struct bio *bio = io->bio;
586 int rw = bio_data_dir(bio);
588 io->start_time = jiffies;
590 cpu = part_stat_lock();
591 part_round_stats(cpu, &dm_disk(md)->part0);
593 atomic_set(&dm_disk(md)->part0.in_flight[rw],
594 atomic_inc_return(&md->pending[rw]));
596 if (unlikely(dm_stats_used(&md->stats)))
597 dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
598 bio_sectors(bio), false, 0, &io->stats_aux);
601 static void end_io_acct(struct dm_io *io)
603 struct mapped_device *md = io->md;
604 struct bio *bio = io->bio;
605 unsigned long duration = jiffies - io->start_time;
607 int rw = bio_data_dir(bio);
609 cpu = part_stat_lock();
610 part_round_stats(cpu, &dm_disk(md)->part0);
611 part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
614 if (unlikely(dm_stats_used(&md->stats)))
615 dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
616 bio_sectors(bio), true, duration, &io->stats_aux);
619 * After this is decremented the bio must not be touched if it is
622 pending = atomic_dec_return(&md->pending[rw]);
623 atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
624 pending += atomic_read(&md->pending[rw^0x1]);
626 /* nudge anyone waiting on suspend queue */
632 * Add the bio to the list of deferred io.
634 static void queue_io(struct mapped_device *md, struct bio *bio)
638 spin_lock_irqsave(&md->deferred_lock, flags);
639 bio_list_add(&md->deferred, bio);
640 spin_unlock_irqrestore(&md->deferred_lock, flags);
641 queue_work(md->wq, &md->work);
645 * Everyone (including functions in this file), should use this
646 * function to access the md->map field, and make sure they call
647 * dm_put_live_table() when finished.
649 struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
651 *srcu_idx = srcu_read_lock(&md->io_barrier);
653 return srcu_dereference(md->map, &md->io_barrier);
656 void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
658 srcu_read_unlock(&md->io_barrier, srcu_idx);
661 void dm_sync_table(struct mapped_device *md)
663 synchronize_srcu(&md->io_barrier);
664 synchronize_rcu_expedited();
668 * A fast alternative to dm_get_live_table/dm_put_live_table.
669 * The caller must not block between these two functions.
671 static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
674 return rcu_dereference(md->map);
677 static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
683 * Open a table device so we can use it as a map destination.
685 static int open_table_device(struct table_device *td, dev_t dev,
686 struct mapped_device *md)
688 static char *_claim_ptr = "I belong to device-mapper";
689 struct block_device *bdev;
693 BUG_ON(td->dm_dev.bdev);
695 bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _claim_ptr);
697 return PTR_ERR(bdev);
699 r = bd_link_disk_holder(bdev, dm_disk(md));
701 blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
705 td->dm_dev.bdev = bdev;
710 * Close a table device that we've been using.
712 static void close_table_device(struct table_device *td, struct mapped_device *md)
714 if (!td->dm_dev.bdev)
717 bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
718 blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
719 td->dm_dev.bdev = NULL;
722 static struct table_device *find_table_device(struct list_head *l, dev_t dev,
724 struct table_device *td;
726 list_for_each_entry(td, l, list)
727 if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
733 int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
734 struct dm_dev **result) {
736 struct table_device *td;
738 mutex_lock(&md->table_devices_lock);
739 td = find_table_device(&md->table_devices, dev, mode);
741 td = kmalloc(sizeof(*td), GFP_KERNEL);
743 mutex_unlock(&md->table_devices_lock);
747 td->dm_dev.mode = mode;
748 td->dm_dev.bdev = NULL;
750 if ((r = open_table_device(td, dev, md))) {
751 mutex_unlock(&md->table_devices_lock);
756 format_dev_t(td->dm_dev.name, dev);
758 atomic_set(&td->count, 0);
759 list_add(&td->list, &md->table_devices);
761 atomic_inc(&td->count);
762 mutex_unlock(&md->table_devices_lock);
764 *result = &td->dm_dev;
767 EXPORT_SYMBOL_GPL(dm_get_table_device);
769 void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
771 struct table_device *td = container_of(d, struct table_device, dm_dev);
773 mutex_lock(&md->table_devices_lock);
774 if (atomic_dec_and_test(&td->count)) {
775 close_table_device(td, md);
779 mutex_unlock(&md->table_devices_lock);
781 EXPORT_SYMBOL(dm_put_table_device);
783 static void free_table_devices(struct list_head *devices)
785 struct list_head *tmp, *next;
787 list_for_each_safe(tmp, next, devices) {
788 struct table_device *td = list_entry(tmp, struct table_device, list);
790 DMWARN("dm_destroy: %s still exists with %d references",
791 td->dm_dev.name, atomic_read(&td->count));
797 * Get the geometry associated with a dm device
799 int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
807 * Set the geometry of a device.
809 int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
811 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
813 if (geo->start > sz) {
814 DMWARN("Start sector is beyond the geometry limits.");
823 /*-----------------------------------------------------------------
825 * A more elegant soln is in the works that uses the queue
826 * merge fn, unfortunately there are a couple of changes to
827 * the block layer that I want to make for this. So in the
828 * interests of getting something for people to use I give
829 * you this clearly demarcated crap.
830 *---------------------------------------------------------------*/
832 static int __noflush_suspending(struct mapped_device *md)
834 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
838 * Decrements the number of outstanding ios that a bio has been
839 * cloned into, completing the original io if necc.
841 static void dec_pending(struct dm_io *io, int error)
846 struct mapped_device *md = io->md;
848 /* Push-back supersedes any I/O errors */
849 if (unlikely(error)) {
850 spin_lock_irqsave(&io->endio_lock, flags);
851 if (!(io->error > 0 && __noflush_suspending(md)))
853 spin_unlock_irqrestore(&io->endio_lock, flags);
856 if (atomic_dec_and_test(&io->io_count)) {
857 if (io->error == DM_ENDIO_REQUEUE) {
859 * Target requested pushing back the I/O.
861 spin_lock_irqsave(&md->deferred_lock, flags);
862 if (__noflush_suspending(md))
863 bio_list_add_head(&md->deferred, io->bio);
865 /* noflush suspend was interrupted. */
867 spin_unlock_irqrestore(&md->deferred_lock, flags);
870 io_error = io->error;
875 if (io_error == DM_ENDIO_REQUEUE)
878 if ((bio->bi_rw & REQ_FLUSH) && bio->bi_iter.bi_size) {
880 * Preflush done for flush with data, reissue
883 bio->bi_rw &= ~REQ_FLUSH;
886 /* done with normal IO or empty flush */
887 trace_block_bio_complete(md->queue, bio, io_error);
888 bio_endio(bio, io_error);
893 static void disable_write_same(struct mapped_device *md)
895 struct queue_limits *limits = dm_get_queue_limits(md);
897 /* device doesn't really support WRITE SAME, disable it */
898 limits->max_write_same_sectors = 0;
901 static void clone_endio(struct bio *bio, int error)
904 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
905 struct dm_io *io = tio->io;
906 struct mapped_device *md = tio->io->md;
907 dm_endio_fn endio = tio->ti->type->end_io;
909 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
913 r = endio(tio->ti, bio, error);
914 if (r < 0 || r == DM_ENDIO_REQUEUE)
916 * error and requeue request are handled
920 else if (r == DM_ENDIO_INCOMPLETE)
921 /* The target will handle the io */
924 DMWARN("unimplemented target endio return value: %d", r);
929 if (unlikely(r == -EREMOTEIO && (bio->bi_rw & REQ_WRITE_SAME) &&
930 !bdev_get_queue(bio->bi_bdev)->limits.max_write_same_sectors))
931 disable_write_same(md);
934 dec_pending(io, error);
938 * Partial completion handling for request-based dm
940 static void end_clone_bio(struct bio *clone, int error)
942 struct dm_rq_clone_bio_info *info =
943 container_of(clone, struct dm_rq_clone_bio_info, clone);
944 struct dm_rq_target_io *tio = info->tio;
945 struct bio *bio = info->orig;
946 unsigned int nr_bytes = info->orig->bi_iter.bi_size;
952 * An error has already been detected on the request.
953 * Once error occurred, just let clone->end_io() handle
959 * Don't notice the error to the upper layer yet.
960 * The error handling decision is made by the target driver,
961 * when the request is completed.
968 * I/O for the bio successfully completed.
969 * Notice the data completion to the upper layer.
973 * bios are processed from the head of the list.
974 * So the completing bio should always be rq->bio.
975 * If it's not, something wrong is happening.
977 if (tio->orig->bio != bio)
978 DMERR("bio completion is going in the middle of the request");
981 * Update the original request.
982 * Do not use blk_end_request() here, because it may complete
983 * the original request before the clone, and break the ordering.
985 blk_update_request(tio->orig, 0, nr_bytes);
989 * Don't touch any member of the md after calling this function because
990 * the md may be freed in dm_put() at the end of this function.
991 * Or do dm_get() before calling this function and dm_put() later.
993 static void rq_completed(struct mapped_device *md, int rw, int run_queue)
995 atomic_dec(&md->pending[rw]);
997 /* nudge anyone waiting on suspend queue */
998 if (!md_in_flight(md))
1002 * Run this off this callpath, as drivers could invoke end_io while
1003 * inside their request_fn (and holding the queue lock). Calling
1004 * back into ->request_fn() could deadlock attempting to grab the
1008 blk_run_queue_async(md->queue);
1011 * dm_put() must be at the end of this function. See the comment above
1016 static void free_rq_clone(struct request *clone)
1018 struct dm_rq_target_io *tio = clone->end_io_data;
1020 blk_rq_unprep_clone(clone);
1025 * Complete the clone and the original request.
1026 * Must be called without queue lock.
1028 static void dm_end_request(struct request *clone, int error)
1030 int rw = rq_data_dir(clone);
1031 struct dm_rq_target_io *tio = clone->end_io_data;
1032 struct mapped_device *md = tio->md;
1033 struct request *rq = tio->orig;
1035 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
1036 rq->errors = clone->errors;
1037 rq->resid_len = clone->resid_len;
1041 * We are using the sense buffer of the original
1043 * So setting the length of the sense data is enough.
1045 rq->sense_len = clone->sense_len;
1048 free_rq_clone(clone);
1049 blk_end_request_all(rq, error);
1050 rq_completed(md, rw, true);
1053 static void dm_unprep_request(struct request *rq)
1055 struct request *clone = rq->special;
1058 rq->cmd_flags &= ~REQ_DONTPREP;
1060 free_rq_clone(clone);
1064 * Requeue the original request of a clone.
1066 void dm_requeue_unmapped_request(struct request *clone)
1068 int rw = rq_data_dir(clone);
1069 struct dm_rq_target_io *tio = clone->end_io_data;
1070 struct mapped_device *md = tio->md;
1071 struct request *rq = tio->orig;
1072 struct request_queue *q = rq->q;
1073 unsigned long flags;
1075 dm_unprep_request(rq);
1077 spin_lock_irqsave(q->queue_lock, flags);
1078 blk_requeue_request(q, rq);
1079 spin_unlock_irqrestore(q->queue_lock, flags);
1081 rq_completed(md, rw, 0);
1083 EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
1085 static void __stop_queue(struct request_queue *q)
1090 static void stop_queue(struct request_queue *q)
1092 unsigned long flags;
1094 spin_lock_irqsave(q->queue_lock, flags);
1096 spin_unlock_irqrestore(q->queue_lock, flags);
1099 static void __start_queue(struct request_queue *q)
1101 if (blk_queue_stopped(q))
1105 static void start_queue(struct request_queue *q)
1107 unsigned long flags;
1109 spin_lock_irqsave(q->queue_lock, flags);
1111 spin_unlock_irqrestore(q->queue_lock, flags);
1114 static void dm_done(struct request *clone, int error, bool mapped)
1117 struct dm_rq_target_io *tio = clone->end_io_data;
1118 dm_request_endio_fn rq_end_io = NULL;
1121 rq_end_io = tio->ti->type->rq_end_io;
1123 if (mapped && rq_end_io)
1124 r = rq_end_io(tio->ti, clone, error, &tio->info);
1127 if (unlikely(r == -EREMOTEIO && (clone->cmd_flags & REQ_WRITE_SAME) &&
1128 !clone->q->limits.max_write_same_sectors))
1129 disable_write_same(tio->md);
1132 /* The target wants to complete the I/O */
1133 dm_end_request(clone, r);
1134 else if (r == DM_ENDIO_INCOMPLETE)
1135 /* The target will handle the I/O */
1137 else if (r == DM_ENDIO_REQUEUE)
1138 /* The target wants to requeue the I/O */
1139 dm_requeue_unmapped_request(clone);
1141 DMWARN("unimplemented target endio return value: %d", r);
1147 * Request completion handler for request-based dm
1149 static void dm_softirq_done(struct request *rq)
1152 struct request *clone = rq->completion_data;
1153 struct dm_rq_target_io *tio = clone->end_io_data;
1155 if (rq->cmd_flags & REQ_FAILED)
1158 dm_done(clone, tio->error, mapped);
1162 * Complete the clone and the original request with the error status
1163 * through softirq context.
1165 static void dm_complete_request(struct request *clone, int error)
1167 struct dm_rq_target_io *tio = clone->end_io_data;
1168 struct request *rq = tio->orig;
1171 rq->completion_data = clone;
1172 blk_complete_request(rq);
1176 * Complete the not-mapped clone and the original request with the error status
1177 * through softirq context.
1178 * Target's rq_end_io() function isn't called.
1179 * This may be used when the target's map_rq() function fails.
1181 void dm_kill_unmapped_request(struct request *clone, int error)
1183 struct dm_rq_target_io *tio = clone->end_io_data;
1184 struct request *rq = tio->orig;
1186 rq->cmd_flags |= REQ_FAILED;
1187 dm_complete_request(clone, error);
1189 EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
1192 * Called with the queue lock held
1194 static void end_clone_request(struct request *clone, int error)
1197 * For just cleaning up the information of the queue in which
1198 * the clone was dispatched.
1199 * The clone is *NOT* freed actually here because it is alloced from
1200 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
1202 __blk_put_request(clone->q, clone);
1205 * Actual request completion is done in a softirq context which doesn't
1206 * hold the queue lock. Otherwise, deadlock could occur because:
1207 * - another request may be submitted by the upper level driver
1208 * of the stacking during the completion
1209 * - the submission which requires queue lock may be done
1210 * against this queue
1212 dm_complete_request(clone, error);
1216 * Return maximum size of I/O possible at the supplied sector up to the current
1219 static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
1221 sector_t target_offset = dm_target_offset(ti, sector);
1223 return ti->len - target_offset;
1226 static sector_t max_io_len(sector_t sector, struct dm_target *ti)
1228 sector_t len = max_io_len_target_boundary(sector, ti);
1229 sector_t offset, max_len;
1232 * Does the target need to split even further?
1234 if (ti->max_io_len) {
1235 offset = dm_target_offset(ti, sector);
1236 if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
1237 max_len = sector_div(offset, ti->max_io_len);
1239 max_len = offset & (ti->max_io_len - 1);
1240 max_len = ti->max_io_len - max_len;
1249 int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
1251 if (len > UINT_MAX) {
1252 DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1253 (unsigned long long)len, UINT_MAX);
1254 ti->error = "Maximum size of target IO is too large";
1258 ti->max_io_len = (uint32_t) len;
1262 EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
1265 * A target may call dm_accept_partial_bio only from the map routine. It is
1266 * allowed for all bio types except REQ_FLUSH.
1268 * dm_accept_partial_bio informs the dm that the target only wants to process
1269 * additional n_sectors sectors of the bio and the rest of the data should be
1270 * sent in a next bio.
1272 * A diagram that explains the arithmetics:
1273 * +--------------------+---------------+-------+
1275 * +--------------------+---------------+-------+
1277 * <-------------- *tio->len_ptr --------------->
1278 * <------- bi_size ------->
1281 * Region 1 was already iterated over with bio_advance or similar function.
1282 * (it may be empty if the target doesn't use bio_advance)
1283 * Region 2 is the remaining bio size that the target wants to process.
1284 * (it may be empty if region 1 is non-empty, although there is no reason
1286 * The target requires that region 3 is to be sent in the next bio.
1288 * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
1289 * the partially processed part (the sum of regions 1+2) must be the same for all
1290 * copies of the bio.
1292 void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
1294 struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
1295 unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
1296 BUG_ON(bio->bi_rw & REQ_FLUSH);
1297 BUG_ON(bi_size > *tio->len_ptr);
1298 BUG_ON(n_sectors > bi_size);
1299 *tio->len_ptr -= bi_size - n_sectors;
1300 bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
1302 EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
1304 static void __map_bio(struct dm_target_io *tio)
1308 struct mapped_device *md;
1309 struct bio *clone = &tio->clone;
1310 struct dm_target *ti = tio->ti;
1312 clone->bi_end_io = clone_endio;
1315 * Map the clone. If r == 0 we don't need to do
1316 * anything, the target has assumed ownership of
1319 atomic_inc(&tio->io->io_count);
1320 sector = clone->bi_iter.bi_sector;
1321 r = ti->type->map(ti, clone);
1322 if (r == DM_MAPIO_REMAPPED) {
1323 /* the bio has been remapped so dispatch it */
1325 trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
1326 tio->io->bio->bi_bdev->bd_dev, sector);
1328 generic_make_request(clone);
1329 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
1330 /* error the io and bail out, or requeue it if needed */
1332 dec_pending(tio->io, r);
1335 DMWARN("unimplemented target map return value: %d", r);
1341 struct mapped_device *md;
1342 struct dm_table *map;
1346 unsigned sector_count;
1349 static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
1351 bio->bi_iter.bi_sector = sector;
1352 bio->bi_iter.bi_size = to_bytes(len);
1356 * Creates a bio that consists of range of complete bvecs.
1358 static void clone_bio(struct dm_target_io *tio, struct bio *bio,
1359 sector_t sector, unsigned len)
1361 struct bio *clone = &tio->clone;
1363 __bio_clone_fast(clone, bio);
1365 if (bio_integrity(bio))
1366 bio_integrity_clone(clone, bio, GFP_NOIO);
1368 bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
1369 clone->bi_iter.bi_size = to_bytes(len);
1371 if (bio_integrity(bio))
1372 bio_integrity_trim(clone, 0, len);
1375 static struct dm_target_io *alloc_tio(struct clone_info *ci,
1376 struct dm_target *ti,
1377 unsigned target_bio_nr)
1379 struct dm_target_io *tio;
1382 clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
1383 tio = container_of(clone, struct dm_target_io, clone);
1387 tio->target_bio_nr = target_bio_nr;
1392 static void __clone_and_map_simple_bio(struct clone_info *ci,
1393 struct dm_target *ti,
1394 unsigned target_bio_nr, unsigned *len)
1396 struct dm_target_io *tio = alloc_tio(ci, ti, target_bio_nr);
1397 struct bio *clone = &tio->clone;
1401 __bio_clone_fast(clone, ci->bio);
1403 bio_setup_sector(clone, ci->sector, *len);
1408 static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
1409 unsigned num_bios, unsigned *len)
1411 unsigned target_bio_nr;
1413 for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
1414 __clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
1417 static int __send_empty_flush(struct clone_info *ci)
1419 unsigned target_nr = 0;
1420 struct dm_target *ti;
1422 BUG_ON(bio_has_data(ci->bio));
1423 while ((ti = dm_table_get_target(ci->map, target_nr++)))
1424 __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
1429 static void __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
1430 sector_t sector, unsigned *len)
1432 struct bio *bio = ci->bio;
1433 struct dm_target_io *tio;
1434 unsigned target_bio_nr;
1435 unsigned num_target_bios = 1;
1438 * Does the target want to receive duplicate copies of the bio?
1440 if (bio_data_dir(bio) == WRITE && ti->num_write_bios)
1441 num_target_bios = ti->num_write_bios(ti, bio);
1443 for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) {
1444 tio = alloc_tio(ci, ti, target_bio_nr);
1446 clone_bio(tio, bio, sector, *len);
1451 typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
1453 static unsigned get_num_discard_bios(struct dm_target *ti)
1455 return ti->num_discard_bios;
1458 static unsigned get_num_write_same_bios(struct dm_target *ti)
1460 return ti->num_write_same_bios;
1463 typedef bool (*is_split_required_fn)(struct dm_target *ti);
1465 static bool is_split_required_for_discard(struct dm_target *ti)
1467 return ti->split_discard_bios;
1470 static int __send_changing_extent_only(struct clone_info *ci,
1471 get_num_bios_fn get_num_bios,
1472 is_split_required_fn is_split_required)
1474 struct dm_target *ti;
1479 ti = dm_table_find_target(ci->map, ci->sector);
1480 if (!dm_target_is_valid(ti))
1484 * Even though the device advertised support for this type of
1485 * request, that does not mean every target supports it, and
1486 * reconfiguration might also have changed that since the
1487 * check was performed.
1489 num_bios = get_num_bios ? get_num_bios(ti) : 0;
1493 if (is_split_required && !is_split_required(ti))
1494 len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
1496 len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti));
1498 __send_duplicate_bios(ci, ti, num_bios, &len);
1501 } while (ci->sector_count -= len);
1506 static int __send_discard(struct clone_info *ci)
1508 return __send_changing_extent_only(ci, get_num_discard_bios,
1509 is_split_required_for_discard);
1512 static int __send_write_same(struct clone_info *ci)
1514 return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
1518 * Select the correct strategy for processing a non-flush bio.
1520 static int __split_and_process_non_flush(struct clone_info *ci)
1522 struct bio *bio = ci->bio;
1523 struct dm_target *ti;
1526 if (unlikely(bio->bi_rw & REQ_DISCARD))
1527 return __send_discard(ci);
1528 else if (unlikely(bio->bi_rw & REQ_WRITE_SAME))
1529 return __send_write_same(ci);
1531 ti = dm_table_find_target(ci->map, ci->sector);
1532 if (!dm_target_is_valid(ti))
1535 len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count);
1537 __clone_and_map_data_bio(ci, ti, ci->sector, &len);
1540 ci->sector_count -= len;
1546 * Entry point to split a bio into clones and submit them to the targets.
1548 static void __split_and_process_bio(struct mapped_device *md,
1549 struct dm_table *map, struct bio *bio)
1551 struct clone_info ci;
1554 if (unlikely(!map)) {
1561 ci.io = alloc_io(md);
1563 atomic_set(&ci.io->io_count, 1);
1566 spin_lock_init(&ci.io->endio_lock);
1567 ci.sector = bio->bi_iter.bi_sector;
1569 start_io_acct(ci.io);
1571 if (bio->bi_rw & REQ_FLUSH) {
1572 ci.bio = &ci.md->flush_bio;
1573 ci.sector_count = 0;
1574 error = __send_empty_flush(&ci);
1575 /* dec_pending submits any data associated with flush */
1578 ci.sector_count = bio_sectors(bio);
1579 while (ci.sector_count && !error)
1580 error = __split_and_process_non_flush(&ci);
1583 /* drop the extra reference count */
1584 dec_pending(ci.io, error);
1586 /*-----------------------------------------------------------------
1588 *---------------------------------------------------------------*/
1590 static int dm_merge_bvec(struct request_queue *q,
1591 struct bvec_merge_data *bvm,
1592 struct bio_vec *biovec)
1594 struct mapped_device *md = q->queuedata;
1595 struct dm_table *map = dm_get_live_table_fast(md);
1596 struct dm_target *ti;
1597 sector_t max_sectors;
1603 ti = dm_table_find_target(map, bvm->bi_sector);
1604 if (!dm_target_is_valid(ti))
1608 * Find maximum amount of I/O that won't need splitting
1610 max_sectors = min(max_io_len(bvm->bi_sector, ti),
1611 (sector_t) queue_max_sectors(q));
1612 max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1613 if (unlikely(max_size < 0)) /* this shouldn't _ever_ happen */
1617 * merge_bvec_fn() returns number of bytes
1618 * it can accept at this offset
1619 * max is precomputed maximal io size
1621 if (max_size && ti->type->merge)
1622 max_size = ti->type->merge(ti, bvm, biovec, max_size);
1624 * If the target doesn't support merge method and some of the devices
1625 * provided their merge_bvec method (we know this by looking for the
1626 * max_hw_sectors that dm_set_device_limits may set), then we can't
1627 * allow bios with multiple vector entries. So always set max_size
1628 * to 0, and the code below allows just one page.
1630 else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
1634 dm_put_live_table_fast(md);
1636 * Always allow an entire first page
1638 if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1639 max_size = biovec->bv_len;
1645 * The request function that just remaps the bio built up by
1648 static void _dm_request(struct request_queue *q, struct bio *bio)
1650 int rw = bio_data_dir(bio);
1651 struct mapped_device *md = q->queuedata;
1654 struct dm_table *map;
1656 map = dm_get_live_table(md, &srcu_idx);
1658 cpu = part_stat_lock();
1659 part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1660 part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1663 /* if we're suspended, we have to queue this io for later */
1664 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
1665 dm_put_live_table(md, srcu_idx);
1667 if (bio_rw(bio) != READA)
1674 __split_and_process_bio(md, map, bio);
1675 dm_put_live_table(md, srcu_idx);
1679 int dm_request_based(struct mapped_device *md)
1681 return blk_queue_stackable(md->queue);
1684 static void dm_request(struct request_queue *q, struct bio *bio)
1686 struct mapped_device *md = q->queuedata;
1688 if (dm_request_based(md))
1689 blk_queue_bio(q, bio);
1691 _dm_request(q, bio);
1694 void dm_dispatch_request(struct request *rq)
1698 if (blk_queue_io_stat(rq->q))
1699 rq->cmd_flags |= REQ_IO_STAT;
1701 rq->start_time = jiffies;
1702 r = blk_insert_cloned_request(rq->q, rq);
1704 dm_complete_request(rq, r);
1706 EXPORT_SYMBOL_GPL(dm_dispatch_request);
1708 static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1711 struct dm_rq_target_io *tio = data;
1712 struct dm_rq_clone_bio_info *info =
1713 container_of(bio, struct dm_rq_clone_bio_info, clone);
1715 info->orig = bio_orig;
1717 bio->bi_end_io = end_clone_bio;
1722 static int setup_clone(struct request *clone, struct request *rq,
1723 struct dm_rq_target_io *tio)
1727 r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1728 dm_rq_bio_constructor, tio);
1732 clone->cmd = rq->cmd;
1733 clone->cmd_len = rq->cmd_len;
1734 clone->sense = rq->sense;
1735 clone->end_io = end_clone_request;
1736 clone->end_io_data = tio;
1741 static struct request *clone_rq(struct request *rq, struct mapped_device *md,
1744 struct request *clone;
1745 struct dm_rq_target_io *tio;
1747 tio = alloc_rq_tio(md, gfp_mask);
1755 memset(&tio->info, 0, sizeof(tio->info));
1757 clone = &tio->clone;
1758 if (setup_clone(clone, rq, tio)) {
1768 * Called with the queue lock held.
1770 static int dm_prep_fn(struct request_queue *q, struct request *rq)
1772 struct mapped_device *md = q->queuedata;
1773 struct request *clone;
1775 if (unlikely(rq->special)) {
1776 DMWARN("Already has something in rq->special.");
1777 return BLKPREP_KILL;
1780 clone = clone_rq(rq, md, GFP_ATOMIC);
1782 return BLKPREP_DEFER;
1784 rq->special = clone;
1785 rq->cmd_flags |= REQ_DONTPREP;
1792 * 0 : the request has been processed (not requeued)
1793 * !0 : the request has been requeued
1795 static int map_request(struct dm_target *ti, struct request *clone,
1796 struct mapped_device *md)
1798 int r, requeued = 0;
1799 struct dm_rq_target_io *tio = clone->end_io_data;
1802 r = ti->type->map_rq(ti, clone, &tio->info);
1804 case DM_MAPIO_SUBMITTED:
1805 /* The target has taken the I/O to submit by itself later */
1807 case DM_MAPIO_REMAPPED:
1808 /* The target has remapped the I/O so dispatch it */
1809 trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
1810 blk_rq_pos(tio->orig));
1811 dm_dispatch_request(clone);
1813 case DM_MAPIO_REQUEUE:
1814 /* The target wants to requeue the I/O */
1815 dm_requeue_unmapped_request(clone);
1820 DMWARN("unimplemented target map return value: %d", r);
1824 /* The target wants to complete the I/O */
1825 dm_kill_unmapped_request(clone, r);
1832 static struct request *dm_start_request(struct mapped_device *md, struct request *orig)
1834 struct request *clone;
1836 blk_start_request(orig);
1837 clone = orig->special;
1838 atomic_inc(&md->pending[rq_data_dir(clone)]);
1841 * Hold the md reference here for the in-flight I/O.
1842 * We can't rely on the reference count by device opener,
1843 * because the device may be closed during the request completion
1844 * when all bios are completed.
1845 * See the comment in rq_completed() too.
1853 * q->request_fn for request-based dm.
1854 * Called with the queue lock held.
1856 static void dm_request_fn(struct request_queue *q)
1858 struct mapped_device *md = q->queuedata;
1860 struct dm_table *map = dm_get_live_table(md, &srcu_idx);
1861 struct dm_target *ti;
1862 struct request *rq, *clone;
1866 * For suspend, check blk_queue_stopped() and increment
1867 * ->pending within a single queue_lock not to increment the
1868 * number of in-flight I/Os after the queue is stopped in
1871 while (!blk_queue_stopped(q)) {
1872 rq = blk_peek_request(q);
1876 /* always use block 0 to find the target for flushes for now */
1878 if (!(rq->cmd_flags & REQ_FLUSH))
1879 pos = blk_rq_pos(rq);
1881 ti = dm_table_find_target(map, pos);
1882 if (!dm_target_is_valid(ti)) {
1884 * Must perform setup, that dm_done() requires,
1885 * before calling dm_kill_unmapped_request
1887 DMERR_LIMIT("request attempted access beyond the end of device");
1888 clone = dm_start_request(md, rq);
1889 dm_kill_unmapped_request(clone, -EIO);
1893 if (ti->type->busy && ti->type->busy(ti))
1896 clone = dm_start_request(md, rq);
1898 spin_unlock(q->queue_lock);
1899 if (map_request(ti, clone, md))
1902 BUG_ON(!irqs_disabled());
1903 spin_lock(q->queue_lock);
1909 BUG_ON(!irqs_disabled());
1910 spin_lock(q->queue_lock);
1913 blk_delay_queue(q, HZ / 10);
1915 dm_put_live_table(md, srcu_idx);
1918 int dm_underlying_device_busy(struct request_queue *q)
1920 return blk_lld_busy(q);
1922 EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1924 static int dm_lld_busy(struct request_queue *q)
1927 struct mapped_device *md = q->queuedata;
1928 struct dm_table *map = dm_get_live_table_fast(md);
1930 if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1933 r = dm_table_any_busy_target(map);
1935 dm_put_live_table_fast(md);
1940 static int dm_any_congested(void *congested_data, int bdi_bits)
1943 struct mapped_device *md = congested_data;
1944 struct dm_table *map;
1946 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
1947 map = dm_get_live_table_fast(md);
1950 * Request-based dm cares about only own queue for
1951 * the query about congestion status of request_queue
1953 if (dm_request_based(md))
1954 r = md->queue->backing_dev_info.state &
1957 r = dm_table_any_congested(map, bdi_bits);
1959 dm_put_live_table_fast(md);
1965 /*-----------------------------------------------------------------
1966 * An IDR is used to keep track of allocated minor numbers.
1967 *---------------------------------------------------------------*/
1968 static void free_minor(int minor)
1970 spin_lock(&_minor_lock);
1971 idr_remove(&_minor_idr, minor);
1972 spin_unlock(&_minor_lock);
1976 * See if the device with a specific minor # is free.
1978 static int specific_minor(int minor)
1982 if (minor >= (1 << MINORBITS))
1985 idr_preload(GFP_KERNEL);
1986 spin_lock(&_minor_lock);
1988 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
1990 spin_unlock(&_minor_lock);
1993 return r == -ENOSPC ? -EBUSY : r;
1997 static int next_free_minor(int *minor)
2001 idr_preload(GFP_KERNEL);
2002 spin_lock(&_minor_lock);
2004 r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
2006 spin_unlock(&_minor_lock);
2014 static const struct block_device_operations dm_blk_dops;
2016 static void dm_wq_work(struct work_struct *work);
2018 static void dm_init_md_queue(struct mapped_device *md)
2021 * Request-based dm devices cannot be stacked on top of bio-based dm
2022 * devices. The type of this dm device has not been decided yet.
2023 * The type is decided at the first table loading time.
2024 * To prevent problematic device stacking, clear the queue flag
2025 * for request stacking support until then.
2027 * This queue is new, so no concurrency on the queue_flags.
2029 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
2031 md->queue->queuedata = md;
2032 md->queue->backing_dev_info.congested_fn = dm_any_congested;
2033 md->queue->backing_dev_info.congested_data = md;
2034 blk_queue_make_request(md->queue, dm_request);
2035 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
2036 blk_queue_merge_bvec(md->queue, dm_merge_bvec);
2040 * Allocate and initialise a blank device with a given minor.
2042 static struct mapped_device *alloc_dev(int minor)
2045 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
2049 DMWARN("unable to allocate device, out of memory.");
2053 if (!try_module_get(THIS_MODULE))
2054 goto bad_module_get;
2056 /* get a minor number for the dev */
2057 if (minor == DM_ANY_MINOR)
2058 r = next_free_minor(&minor);
2060 r = specific_minor(minor);
2064 r = init_srcu_struct(&md->io_barrier);
2066 goto bad_io_barrier;
2068 md->type = DM_TYPE_NONE;
2069 mutex_init(&md->suspend_lock);
2070 mutex_init(&md->type_lock);
2071 mutex_init(&md->table_devices_lock);
2072 spin_lock_init(&md->deferred_lock);
2073 atomic_set(&md->holders, 1);
2074 atomic_set(&md->open_count, 0);
2075 atomic_set(&md->event_nr, 0);
2076 atomic_set(&md->uevent_seq, 0);
2077 INIT_LIST_HEAD(&md->uevent_list);
2078 INIT_LIST_HEAD(&md->table_devices);
2079 spin_lock_init(&md->uevent_lock);
2081 md->queue = blk_alloc_queue(GFP_KERNEL);
2085 dm_init_md_queue(md);
2087 md->disk = alloc_disk(1);
2091 atomic_set(&md->pending[0], 0);
2092 atomic_set(&md->pending[1], 0);
2093 init_waitqueue_head(&md->wait);
2094 INIT_WORK(&md->work, dm_wq_work);
2095 init_waitqueue_head(&md->eventq);
2096 init_completion(&md->kobj_holder.completion);
2098 md->disk->major = _major;
2099 md->disk->first_minor = minor;
2100 md->disk->fops = &dm_blk_dops;
2101 md->disk->queue = md->queue;
2102 md->disk->private_data = md;
2103 sprintf(md->disk->disk_name, "dm-%d", minor);
2105 format_dev_t(md->name, MKDEV(_major, minor));
2107 md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
2111 md->bdev = bdget_disk(md->disk, 0);
2115 bio_init(&md->flush_bio);
2116 md->flush_bio.bi_bdev = md->bdev;
2117 md->flush_bio.bi_rw = WRITE_FLUSH;
2119 dm_stats_init(&md->stats);
2121 /* Populate the mapping, nobody knows we exist yet */
2122 spin_lock(&_minor_lock);
2123 old_md = idr_replace(&_minor_idr, md, minor);
2124 spin_unlock(&_minor_lock);
2126 BUG_ON(old_md != MINOR_ALLOCED);
2131 destroy_workqueue(md->wq);
2133 del_gendisk(md->disk);
2136 blk_cleanup_queue(md->queue);
2138 cleanup_srcu_struct(&md->io_barrier);
2142 module_put(THIS_MODULE);
2148 static void unlock_fs(struct mapped_device *md);
2150 static void free_dev(struct mapped_device *md)
2152 int minor = MINOR(disk_devt(md->disk));
2156 destroy_workqueue(md->wq);
2158 mempool_destroy(md->io_pool);
2160 bioset_free(md->bs);
2161 blk_integrity_unregister(md->disk);
2162 del_gendisk(md->disk);
2163 cleanup_srcu_struct(&md->io_barrier);
2164 free_table_devices(&md->table_devices);
2167 spin_lock(&_minor_lock);
2168 md->disk->private_data = NULL;
2169 spin_unlock(&_minor_lock);
2172 blk_cleanup_queue(md->queue);
2173 dm_stats_cleanup(&md->stats);
2174 module_put(THIS_MODULE);
2178 static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
2180 struct dm_md_mempools *p = dm_table_get_md_mempools(t);
2182 if (md->io_pool && md->bs) {
2183 /* The md already has necessary mempools. */
2184 if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) {
2186 * Reload bioset because front_pad may have changed
2187 * because a different table was loaded.
2189 bioset_free(md->bs);
2192 } else if (dm_table_get_type(t) == DM_TYPE_REQUEST_BASED) {
2194 * There's no need to reload with request-based dm
2195 * because the size of front_pad doesn't change.
2196 * Note for future: If you are to reload bioset,
2197 * prep-ed requests in the queue may refer
2198 * to bio from the old bioset, so you must walk
2199 * through the queue to unprep.
2205 BUG_ON(!p || md->io_pool || md->bs);
2207 md->io_pool = p->io_pool;
2213 /* mempool bind completed, now no need any mempools in the table */
2214 dm_table_free_md_mempools(t);
2218 * Bind a table to the device.
2220 static void event_callback(void *context)
2222 unsigned long flags;
2224 struct mapped_device *md = (struct mapped_device *) context;
2226 spin_lock_irqsave(&md->uevent_lock, flags);
2227 list_splice_init(&md->uevent_list, &uevents);
2228 spin_unlock_irqrestore(&md->uevent_lock, flags);
2230 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
2232 atomic_inc(&md->event_nr);
2233 wake_up(&md->eventq);
2237 * Protected by md->suspend_lock obtained by dm_swap_table().
2239 static void __set_size(struct mapped_device *md, sector_t size)
2241 set_capacity(md->disk, size);
2243 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
2247 * Return 1 if the queue has a compulsory merge_bvec_fn function.
2249 * If this function returns 0, then the device is either a non-dm
2250 * device without a merge_bvec_fn, or it is a dm device that is
2251 * able to split any bios it receives that are too big.
2253 int dm_queue_merge_is_compulsory(struct request_queue *q)
2255 struct mapped_device *dev_md;
2257 if (!q->merge_bvec_fn)
2260 if (q->make_request_fn == dm_request) {
2261 dev_md = q->queuedata;
2262 if (test_bit(DMF_MERGE_IS_OPTIONAL, &dev_md->flags))
2269 static int dm_device_merge_is_compulsory(struct dm_target *ti,
2270 struct dm_dev *dev, sector_t start,
2271 sector_t len, void *data)
2273 struct block_device *bdev = dev->bdev;
2274 struct request_queue *q = bdev_get_queue(bdev);
2276 return dm_queue_merge_is_compulsory(q);
2280 * Return 1 if it is acceptable to ignore merge_bvec_fn based
2281 * on the properties of the underlying devices.
2283 static int dm_table_merge_is_optional(struct dm_table *table)
2286 struct dm_target *ti;
2288 while (i < dm_table_get_num_targets(table)) {
2289 ti = dm_table_get_target(table, i++);
2291 if (ti->type->iterate_devices &&
2292 ti->type->iterate_devices(ti, dm_device_merge_is_compulsory, NULL))
2300 * Returns old map, which caller must destroy.
2302 static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2303 struct queue_limits *limits)
2305 struct dm_table *old_map;
2306 struct request_queue *q = md->queue;
2308 int merge_is_optional;
2310 size = dm_table_get_size(t);
2313 * Wipe any geometry if the size of the table changed.
2315 if (size != dm_get_size(md))
2316 memset(&md->geometry, 0, sizeof(md->geometry));
2318 __set_size(md, size);
2320 dm_table_event_callback(t, event_callback, md);
2323 * The queue hasn't been stopped yet, if the old table type wasn't
2324 * for request-based during suspension. So stop it to prevent
2325 * I/O mapping before resume.
2326 * This must be done before setting the queue restrictions,
2327 * because request-based dm may be run just after the setting.
2329 if (dm_table_request_based(t) && !blk_queue_stopped(q))
2332 __bind_mempools(md, t);
2334 merge_is_optional = dm_table_merge_is_optional(t);
2336 old_map = rcu_dereference(md->map);
2337 rcu_assign_pointer(md->map, t);
2338 md->immutable_target_type = dm_table_get_immutable_target_type(t);
2340 dm_table_set_restrictions(t, q, limits);
2341 if (merge_is_optional)
2342 set_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
2344 clear_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
2352 * Returns unbound table for the caller to free.
2354 static struct dm_table *__unbind(struct mapped_device *md)
2356 struct dm_table *map = rcu_dereference(md->map);
2361 dm_table_event_callback(map, NULL, NULL);
2362 RCU_INIT_POINTER(md->map, NULL);
2369 * Constructor for a new device.
2371 int dm_create(int minor, struct mapped_device **result)
2373 struct mapped_device *md;
2375 md = alloc_dev(minor);
2386 * Functions to manage md->type.
2387 * All are required to hold md->type_lock.
2389 void dm_lock_md_type(struct mapped_device *md)
2391 mutex_lock(&md->type_lock);
2394 void dm_unlock_md_type(struct mapped_device *md)
2396 mutex_unlock(&md->type_lock);
2399 void dm_set_md_type(struct mapped_device *md, unsigned type)
2401 BUG_ON(!mutex_is_locked(&md->type_lock));
2405 unsigned dm_get_md_type(struct mapped_device *md)
2407 BUG_ON(!mutex_is_locked(&md->type_lock));
2411 struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
2413 return md->immutable_target_type;
2417 * The queue_limits are only valid as long as you have a reference
2420 struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2422 BUG_ON(!atomic_read(&md->holders));
2423 return &md->queue->limits;
2425 EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2428 * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
2430 static int dm_init_request_based_queue(struct mapped_device *md)
2432 struct request_queue *q = NULL;
2434 if (md->queue->elevator)
2437 /* Fully initialize the queue */
2438 q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
2443 dm_init_md_queue(md);
2444 blk_queue_softirq_done(md->queue, dm_softirq_done);
2445 blk_queue_prep_rq(md->queue, dm_prep_fn);
2446 blk_queue_lld_busy(md->queue, dm_lld_busy);
2448 elv_register_queue(md->queue);
2454 * Setup the DM device's queue based on md's type
2456 int dm_setup_md_queue(struct mapped_device *md)
2458 if ((dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) &&
2459 !dm_init_request_based_queue(md)) {
2460 DMWARN("Cannot initialize queue for request-based mapped device");
2467 static struct mapped_device *dm_find_md(dev_t dev)
2469 struct mapped_device *md;
2470 unsigned minor = MINOR(dev);
2472 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2475 spin_lock(&_minor_lock);
2477 md = idr_find(&_minor_idr, minor);
2478 if (md && (md == MINOR_ALLOCED ||
2479 (MINOR(disk_devt(dm_disk(md))) != minor) ||
2480 dm_deleting_md(md) ||
2481 test_bit(DMF_FREEING, &md->flags))) {
2487 spin_unlock(&_minor_lock);
2492 struct mapped_device *dm_get_md(dev_t dev)
2494 struct mapped_device *md = dm_find_md(dev);
2501 EXPORT_SYMBOL_GPL(dm_get_md);
2503 void *dm_get_mdptr(struct mapped_device *md)
2505 return md->interface_ptr;
2508 void dm_set_mdptr(struct mapped_device *md, void *ptr)
2510 md->interface_ptr = ptr;
2513 void dm_get(struct mapped_device *md)
2515 atomic_inc(&md->holders);
2516 BUG_ON(test_bit(DMF_FREEING, &md->flags));
2519 const char *dm_device_name(struct mapped_device *md)
2523 EXPORT_SYMBOL_GPL(dm_device_name);
2525 static void __dm_destroy(struct mapped_device *md, bool wait)
2527 struct dm_table *map;
2532 spin_lock(&_minor_lock);
2533 map = dm_get_live_table(md, &srcu_idx);
2534 idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2535 set_bit(DMF_FREEING, &md->flags);
2536 spin_unlock(&_minor_lock);
2538 if (!dm_suspended_md(md)) {
2539 dm_table_presuspend_targets(map);
2540 dm_table_postsuspend_targets(map);
2543 /* dm_put_live_table must be before msleep, otherwise deadlock is possible */
2544 dm_put_live_table(md, srcu_idx);
2547 * Rare, but there may be I/O requests still going to complete,
2548 * for example. Wait for all references to disappear.
2549 * No one should increment the reference count of the mapped_device,
2550 * after the mapped_device state becomes DMF_FREEING.
2553 while (atomic_read(&md->holders))
2555 else if (atomic_read(&md->holders))
2556 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2557 dm_device_name(md), atomic_read(&md->holders));
2560 dm_table_destroy(__unbind(md));
2564 void dm_destroy(struct mapped_device *md)
2566 __dm_destroy(md, true);
2569 void dm_destroy_immediate(struct mapped_device *md)
2571 __dm_destroy(md, false);
2574 void dm_put(struct mapped_device *md)
2576 atomic_dec(&md->holders);
2578 EXPORT_SYMBOL_GPL(dm_put);
2580 static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
2583 DECLARE_WAITQUEUE(wait, current);
2585 add_wait_queue(&md->wait, &wait);
2588 set_current_state(interruptible);
2590 if (!md_in_flight(md))
2593 if (interruptible == TASK_INTERRUPTIBLE &&
2594 signal_pending(current)) {
2601 set_current_state(TASK_RUNNING);
2603 remove_wait_queue(&md->wait, &wait);
2609 * Process the deferred bios
2611 static void dm_wq_work(struct work_struct *work)
2613 struct mapped_device *md = container_of(work, struct mapped_device,
2617 struct dm_table *map;
2619 map = dm_get_live_table(md, &srcu_idx);
2621 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2622 spin_lock_irq(&md->deferred_lock);
2623 c = bio_list_pop(&md->deferred);
2624 spin_unlock_irq(&md->deferred_lock);
2629 if (dm_request_based(md))
2630 generic_make_request(c);
2632 __split_and_process_bio(md, map, c);
2635 dm_put_live_table(md, srcu_idx);
2638 static void dm_queue_flush(struct mapped_device *md)
2640 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2641 smp_mb__after_atomic();
2642 queue_work(md->wq, &md->work);
2646 * Swap in a new table, returning the old one for the caller to destroy.
2648 struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
2650 struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
2651 struct queue_limits limits;
2654 mutex_lock(&md->suspend_lock);
2656 /* device must be suspended */
2657 if (!dm_suspended_md(md))
2661 * If the new table has no data devices, retain the existing limits.
2662 * This helps multipath with queue_if_no_path if all paths disappear,
2663 * then new I/O is queued based on these limits, and then some paths
2666 if (dm_table_has_no_data_devices(table)) {
2667 live_map = dm_get_live_table_fast(md);
2669 limits = md->queue->limits;
2670 dm_put_live_table_fast(md);
2674 r = dm_calculate_queue_limits(table, &limits);
2681 map = __bind(md, table, &limits);
2684 mutex_unlock(&md->suspend_lock);
2689 * Functions to lock and unlock any filesystem running on the
2692 static int lock_fs(struct mapped_device *md)
2696 WARN_ON(md->frozen_sb);
2698 md->frozen_sb = freeze_bdev(md->bdev);
2699 if (IS_ERR(md->frozen_sb)) {
2700 r = PTR_ERR(md->frozen_sb);
2701 md->frozen_sb = NULL;
2705 set_bit(DMF_FROZEN, &md->flags);
2710 static void unlock_fs(struct mapped_device *md)
2712 if (!test_bit(DMF_FROZEN, &md->flags))
2715 thaw_bdev(md->bdev, md->frozen_sb);
2716 md->frozen_sb = NULL;
2717 clear_bit(DMF_FROZEN, &md->flags);
2721 * We need to be able to change a mapping table under a mounted
2722 * filesystem. For example we might want to move some data in
2723 * the background. Before the table can be swapped with
2724 * dm_bind_table, dm_suspend must be called to flush any in
2725 * flight bios and ensure that any further io gets deferred.
2728 * Suspend mechanism in request-based dm.
2730 * 1. Flush all I/Os by lock_fs() if needed.
2731 * 2. Stop dispatching any I/O by stopping the request_queue.
2732 * 3. Wait for all in-flight I/Os to be completed or requeued.
2734 * To abort suspend, start the request_queue.
2736 int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
2738 struct dm_table *map = NULL;
2740 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
2741 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
2743 mutex_lock(&md->suspend_lock);
2745 if (dm_suspended_md(md)) {
2750 map = rcu_dereference(md->map);
2753 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2754 * This flag is cleared before dm_suspend returns.
2757 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2759 /* This does not get reverted if there's an error later. */
2760 dm_table_presuspend_targets(map);
2763 * Flush I/O to the device.
2764 * Any I/O submitted after lock_fs() may not be flushed.
2765 * noflush takes precedence over do_lockfs.
2766 * (lock_fs() flushes I/Os and waits for them to complete.)
2768 if (!noflush && do_lockfs) {
2775 * Here we must make sure that no processes are submitting requests
2776 * to target drivers i.e. no one may be executing
2777 * __split_and_process_bio. This is called from dm_request and
2780 * To get all processes out of __split_and_process_bio in dm_request,
2781 * we take the write lock. To prevent any process from reentering
2782 * __split_and_process_bio from dm_request and quiesce the thread
2783 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
2784 * flush_workqueue(md->wq).
2786 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2788 synchronize_srcu(&md->io_barrier);
2791 * Stop md->queue before flushing md->wq in case request-based
2792 * dm defers requests to md->wq from md->queue.
2794 if (dm_request_based(md))
2795 stop_queue(md->queue);
2797 flush_workqueue(md->wq);
2800 * At this point no more requests are entering target request routines.
2801 * We call dm_wait_for_completion to wait for all existing requests
2804 r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
2807 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2809 synchronize_srcu(&md->io_barrier);
2811 /* were we interrupted ? */
2815 if (dm_request_based(md))
2816 start_queue(md->queue);
2819 goto out_unlock; /* pushback list is already flushed, so skip flush */
2823 * If dm_wait_for_completion returned 0, the device is completely
2824 * quiescent now. There is no request-processing activity. All new
2825 * requests are being added to md->deferred list.
2828 set_bit(DMF_SUSPENDED, &md->flags);
2830 dm_table_postsuspend_targets(map);
2833 mutex_unlock(&md->suspend_lock);
2837 int dm_resume(struct mapped_device *md)
2840 struct dm_table *map = NULL;
2842 mutex_lock(&md->suspend_lock);
2843 if (!dm_suspended_md(md))
2846 map = rcu_dereference(md->map);
2847 if (!map || !dm_table_get_size(map))
2850 r = dm_table_resume_targets(map);
2857 * Flushing deferred I/Os must be done after targets are resumed
2858 * so that mapping of targets can work correctly.
2859 * Request-based dm is queueing the deferred I/Os in its request_queue.
2861 if (dm_request_based(md))
2862 start_queue(md->queue);
2866 clear_bit(DMF_SUSPENDED, &md->flags);
2870 mutex_unlock(&md->suspend_lock);
2876 * Internal suspend/resume works like userspace-driven suspend. It waits
2877 * until all bios finish and prevents issuing new bios to the target drivers.
2878 * It may be used only from the kernel.
2880 * Internal suspend holds md->suspend_lock, which prevents interaction with
2881 * userspace-driven suspend.
2884 void dm_internal_suspend(struct mapped_device *md)
2886 mutex_lock(&md->suspend_lock);
2887 if (dm_suspended_md(md))
2890 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2891 synchronize_srcu(&md->io_barrier);
2892 flush_workqueue(md->wq);
2893 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2896 void dm_internal_resume(struct mapped_device *md)
2898 if (dm_suspended_md(md))
2904 mutex_unlock(&md->suspend_lock);
2907 /*-----------------------------------------------------------------
2908 * Event notification.
2909 *---------------------------------------------------------------*/
2910 int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
2913 char udev_cookie[DM_COOKIE_LENGTH];
2914 char *envp[] = { udev_cookie, NULL };
2917 return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
2919 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
2920 DM_COOKIE_ENV_VAR_NAME, cookie);
2921 return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
2926 uint32_t dm_next_uevent_seq(struct mapped_device *md)
2928 return atomic_add_return(1, &md->uevent_seq);
2931 uint32_t dm_get_event_nr(struct mapped_device *md)
2933 return atomic_read(&md->event_nr);
2936 int dm_wait_event(struct mapped_device *md, int event_nr)
2938 return wait_event_interruptible(md->eventq,
2939 (event_nr != atomic_read(&md->event_nr)));
2942 void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
2944 unsigned long flags;
2946 spin_lock_irqsave(&md->uevent_lock, flags);
2947 list_add(elist, &md->uevent_list);
2948 spin_unlock_irqrestore(&md->uevent_lock, flags);
2952 * The gendisk is only valid as long as you have a reference
2955 struct gendisk *dm_disk(struct mapped_device *md)
2960 struct kobject *dm_kobject(struct mapped_device *md)
2962 return &md->kobj_holder.kobj;
2965 struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2967 struct mapped_device *md;
2969 md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
2971 if (test_bit(DMF_FREEING, &md->flags) ||
2979 int dm_suspended_md(struct mapped_device *md)
2981 return test_bit(DMF_SUSPENDED, &md->flags);
2984 int dm_test_deferred_remove_flag(struct mapped_device *md)
2986 return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
2989 int dm_suspended(struct dm_target *ti)
2991 return dm_suspended_md(dm_table_get_md(ti->table));
2993 EXPORT_SYMBOL_GPL(dm_suspended);
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(unsigned type, unsigned integrity, unsigned per_bio_data_size)
3003 struct dm_md_mempools *pools = kzalloc(sizeof(*pools), GFP_KERNEL);
3004 struct kmem_cache *cachep;
3005 unsigned int pool_size;
3006 unsigned int front_pad;
3011 if (type == DM_TYPE_BIO_BASED) {
3013 pool_size = dm_get_reserved_bio_based_ios();
3014 front_pad = roundup(per_bio_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
3015 } else if (type == DM_TYPE_REQUEST_BASED) {
3016 cachep = _rq_tio_cache;
3017 pool_size = dm_get_reserved_rq_based_ios();
3018 front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
3019 /* per_bio_data_size is not used. See __bind_mempools(). */
3020 WARN_ON(per_bio_data_size != 0);
3024 pools->io_pool = mempool_create_slab_pool(pool_size, cachep);
3025 if (!pools->io_pool)
3028 pools->bs = bioset_create_nobvec(pool_size, front_pad);
3032 if (integrity && bioset_integrity_create(pools->bs, pool_size))
3038 dm_free_md_mempools(pools);
3043 void dm_free_md_mempools(struct dm_md_mempools *pools)
3049 mempool_destroy(pools->io_pool);
3052 bioset_free(pools->bs);
3057 static const struct block_device_operations dm_blk_dops = {
3058 .open = dm_blk_open,
3059 .release = dm_blk_close,
3060 .ioctl = dm_blk_ioctl,
3061 .getgeo = dm_blk_getgeo,
3062 .owner = THIS_MODULE
3068 module_init(dm_init);
3069 module_exit(dm_exit);
3071 module_param(major, uint, 0);
3072 MODULE_PARM_DESC(major, "The major number of the device mapper");
3074 module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
3075 MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
3077 module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR);
3078 MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools");
3080 MODULE_DESCRIPTION(DM_NAME " driver");
3081 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
3082 MODULE_LICENSE("GPL");