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>
22 #include <linux/wait.h>
23 #include <linux/kthread.h>
24 #include <linux/ktime.h>
25 #include <linux/elevator.h> /* for rq_end_sector() */
27 #include <trace/events/block.h>
29 #define DM_MSG_PREFIX "core"
33 * ratelimit state to be used in DMXXX_LIMIT().
35 DEFINE_RATELIMIT_STATE(dm_ratelimit_state
,
36 DEFAULT_RATELIMIT_INTERVAL
,
37 DEFAULT_RATELIMIT_BURST
);
38 EXPORT_SYMBOL(dm_ratelimit_state
);
42 * Cookies are numeric values sent with CHANGE and REMOVE
43 * uevents while resuming, removing or renaming the device.
45 #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
46 #define DM_COOKIE_LENGTH 24
48 static const char *_name
= DM_NAME
;
50 static unsigned int major
= 0;
51 static unsigned int _major
= 0;
53 static DEFINE_IDR(_minor_idr
);
55 static DEFINE_SPINLOCK(_minor_lock
);
57 static void do_deferred_remove(struct work_struct
*w
);
59 static DECLARE_WORK(deferred_remove_work
, do_deferred_remove
);
61 static struct workqueue_struct
*deferred_remove_workqueue
;
65 * One of these is allocated per bio.
68 struct mapped_device
*md
;
72 unsigned long start_time
;
73 spinlock_t endio_lock
;
74 struct dm_stats_aux stats_aux
;
78 * For request-based dm.
79 * One of these is allocated per request.
81 struct dm_rq_target_io
{
82 struct mapped_device
*md
;
84 struct request
*orig
, *clone
;
85 struct kthread_work work
;
91 * For request-based dm - the bio clones we allocate are embedded in these
94 * We allocate these with bio_alloc_bioset, using the front_pad parameter when
95 * the bioset is created - this means the bio has to come at the end of the
98 struct dm_rq_clone_bio_info
{
100 struct dm_rq_target_io
*tio
;
104 union map_info
*dm_get_rq_mapinfo(struct request
*rq
)
106 if (rq
&& rq
->end_io_data
)
107 return &((struct dm_rq_target_io
*)rq
->end_io_data
)->info
;
110 EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo
);
112 #define MINOR_ALLOCED ((void *)-1)
115 * Bits for the md->flags field.
117 #define DMF_BLOCK_IO_FOR_SUSPEND 0
118 #define DMF_SUSPENDED 1
120 #define DMF_FREEING 3
121 #define DMF_DELETING 4
122 #define DMF_NOFLUSH_SUSPENDING 5
123 #define DMF_MERGE_IS_OPTIONAL 6
124 #define DMF_DEFERRED_REMOVE 7
125 #define DMF_SUSPENDED_INTERNALLY 8
128 * A dummy definition to make RCU happy.
129 * struct dm_table should never be dereferenced in this file.
136 * Work processed by per-device workqueue.
138 struct mapped_device
{
139 struct srcu_struct io_barrier
;
140 struct mutex suspend_lock
;
145 * The current mapping.
146 * Use dm_get_live_table{_fast} or take suspend_lock for
149 struct dm_table __rcu
*map
;
151 struct list_head table_devices
;
152 struct mutex table_devices_lock
;
156 struct request_queue
*queue
;
158 /* Protect queue and type against concurrent access. */
159 struct mutex type_lock
;
161 struct target_type
*immutable_target_type
;
163 struct gendisk
*disk
;
169 * A list of ios that arrived while we were suspended.
172 wait_queue_head_t wait
;
173 struct work_struct work
;
174 struct bio_list deferred
;
175 spinlock_t deferred_lock
;
178 * Processing queue (flush)
180 struct workqueue_struct
*wq
;
183 * io objects are allocated from here.
194 wait_queue_head_t eventq
;
196 struct list_head uevent_list
;
197 spinlock_t uevent_lock
; /* Protect access to uevent_list */
200 * freeze/thaw support require holding onto a super block
202 struct super_block
*frozen_sb
;
203 struct block_device
*bdev
;
205 /* forced geometry settings */
206 struct hd_geometry geometry
;
208 /* kobject and completion */
209 struct dm_kobject_holder kobj_holder
;
211 /* zero-length flush that will be cloned and submitted to targets */
212 struct bio flush_bio
;
214 /* the number of internal suspends */
215 unsigned internal_suspend_count
;
217 struct dm_stats stats
;
219 struct kthread_worker kworker
;
220 struct task_struct
*kworker_task
;
222 /* for request-based merge heuristic in dm_request_fn() */
223 unsigned seq_rq_merge_deadline_usecs
;
225 sector_t last_rq_pos
;
226 ktime_t last_rq_start_time
;
230 * For mempools pre-allocation at the table loading time.
232 struct dm_md_mempools
{
238 struct table_device
{
239 struct list_head list
;
241 struct dm_dev dm_dev
;
244 #define RESERVED_BIO_BASED_IOS 16
245 #define RESERVED_REQUEST_BASED_IOS 256
246 #define RESERVED_MAX_IOS 1024
247 static struct kmem_cache
*_io_cache
;
248 static struct kmem_cache
*_rq_tio_cache
;
249 static struct kmem_cache
*_rq_cache
;
252 * Bio-based DM's mempools' reserved IOs set by the user.
254 static unsigned reserved_bio_based_ios
= RESERVED_BIO_BASED_IOS
;
257 * Request-based DM's mempools' reserved IOs set by the user.
259 static unsigned reserved_rq_based_ios
= RESERVED_REQUEST_BASED_IOS
;
261 static unsigned __dm_get_module_param(unsigned *module_param
,
262 unsigned def
, unsigned max
)
264 unsigned param
= ACCESS_ONCE(*module_param
);
265 unsigned modified_param
= 0;
268 modified_param
= def
;
269 else if (param
> max
)
270 modified_param
= max
;
272 if (modified_param
) {
273 (void)cmpxchg(module_param
, param
, modified_param
);
274 param
= modified_param
;
280 unsigned dm_get_reserved_bio_based_ios(void)
282 return __dm_get_module_param(&reserved_bio_based_ios
,
283 RESERVED_BIO_BASED_IOS
, RESERVED_MAX_IOS
);
285 EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios
);
287 unsigned dm_get_reserved_rq_based_ios(void)
289 return __dm_get_module_param(&reserved_rq_based_ios
,
290 RESERVED_REQUEST_BASED_IOS
, RESERVED_MAX_IOS
);
292 EXPORT_SYMBOL_GPL(dm_get_reserved_rq_based_ios
);
294 static int __init
local_init(void)
298 /* allocate a slab for the dm_ios */
299 _io_cache
= KMEM_CACHE(dm_io
, 0);
303 _rq_tio_cache
= KMEM_CACHE(dm_rq_target_io
, 0);
305 goto out_free_io_cache
;
307 _rq_cache
= kmem_cache_create("dm_clone_request", sizeof(struct request
),
308 __alignof__(struct request
), 0, NULL
);
310 goto out_free_rq_tio_cache
;
312 r
= dm_uevent_init();
314 goto out_free_rq_cache
;
316 deferred_remove_workqueue
= alloc_workqueue("kdmremove", WQ_UNBOUND
, 1);
317 if (!deferred_remove_workqueue
) {
319 goto out_uevent_exit
;
323 r
= register_blkdev(_major
, _name
);
325 goto out_free_workqueue
;
333 destroy_workqueue(deferred_remove_workqueue
);
337 kmem_cache_destroy(_rq_cache
);
338 out_free_rq_tio_cache
:
339 kmem_cache_destroy(_rq_tio_cache
);
341 kmem_cache_destroy(_io_cache
);
346 static void local_exit(void)
348 flush_scheduled_work();
349 destroy_workqueue(deferred_remove_workqueue
);
351 kmem_cache_destroy(_rq_cache
);
352 kmem_cache_destroy(_rq_tio_cache
);
353 kmem_cache_destroy(_io_cache
);
354 unregister_blkdev(_major
, _name
);
359 DMINFO("cleaned up");
362 static int (*_inits
[])(void) __initdata
= {
373 static void (*_exits
[])(void) = {
384 static int __init
dm_init(void)
386 const int count
= ARRAY_SIZE(_inits
);
390 for (i
= 0; i
< count
; i
++) {
405 static void __exit
dm_exit(void)
407 int i
= ARRAY_SIZE(_exits
);
413 * Should be empty by this point.
415 idr_destroy(&_minor_idr
);
419 * Block device functions
421 int dm_deleting_md(struct mapped_device
*md
)
423 return test_bit(DMF_DELETING
, &md
->flags
);
426 static int dm_blk_open(struct block_device
*bdev
, fmode_t mode
)
428 struct mapped_device
*md
;
430 spin_lock(&_minor_lock
);
432 md
= bdev
->bd_disk
->private_data
;
436 if (test_bit(DMF_FREEING
, &md
->flags
) ||
437 dm_deleting_md(md
)) {
443 atomic_inc(&md
->open_count
);
445 spin_unlock(&_minor_lock
);
447 return md
? 0 : -ENXIO
;
450 static void dm_blk_close(struct gendisk
*disk
, fmode_t mode
)
452 struct mapped_device
*md
;
454 spin_lock(&_minor_lock
);
456 md
= disk
->private_data
;
460 if (atomic_dec_and_test(&md
->open_count
) &&
461 (test_bit(DMF_DEFERRED_REMOVE
, &md
->flags
)))
462 queue_work(deferred_remove_workqueue
, &deferred_remove_work
);
466 spin_unlock(&_minor_lock
);
469 int dm_open_count(struct mapped_device
*md
)
471 return atomic_read(&md
->open_count
);
475 * Guarantees nothing is using the device before it's deleted.
477 int dm_lock_for_deletion(struct mapped_device
*md
, bool mark_deferred
, bool only_deferred
)
481 spin_lock(&_minor_lock
);
483 if (dm_open_count(md
)) {
486 set_bit(DMF_DEFERRED_REMOVE
, &md
->flags
);
487 } else if (only_deferred
&& !test_bit(DMF_DEFERRED_REMOVE
, &md
->flags
))
490 set_bit(DMF_DELETING
, &md
->flags
);
492 spin_unlock(&_minor_lock
);
497 int dm_cancel_deferred_remove(struct mapped_device
*md
)
501 spin_lock(&_minor_lock
);
503 if (test_bit(DMF_DELETING
, &md
->flags
))
506 clear_bit(DMF_DEFERRED_REMOVE
, &md
->flags
);
508 spin_unlock(&_minor_lock
);
513 static void do_deferred_remove(struct work_struct
*w
)
515 dm_deferred_remove();
518 sector_t
dm_get_size(struct mapped_device
*md
)
520 return get_capacity(md
->disk
);
523 struct request_queue
*dm_get_md_queue(struct mapped_device
*md
)
528 struct dm_stats
*dm_get_stats(struct mapped_device
*md
)
533 static int dm_blk_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
535 struct mapped_device
*md
= bdev
->bd_disk
->private_data
;
537 return dm_get_geometry(md
, geo
);
540 static int dm_blk_ioctl(struct block_device
*bdev
, fmode_t mode
,
541 unsigned int cmd
, unsigned long arg
)
543 struct mapped_device
*md
= bdev
->bd_disk
->private_data
;
545 struct dm_table
*map
;
546 struct dm_target
*tgt
;
550 map
= dm_get_live_table(md
, &srcu_idx
);
552 if (!map
|| !dm_table_get_size(map
))
555 /* We only support devices that have a single target */
556 if (dm_table_get_num_targets(map
) != 1)
559 tgt
= dm_table_get_target(map
, 0);
560 if (!tgt
->type
->ioctl
)
563 if (dm_suspended_md(md
)) {
568 r
= tgt
->type
->ioctl(tgt
, cmd
, arg
);
571 dm_put_live_table(md
, srcu_idx
);
573 if (r
== -ENOTCONN
) {
581 static struct dm_io
*alloc_io(struct mapped_device
*md
)
583 return mempool_alloc(md
->io_pool
, GFP_NOIO
);
586 static void free_io(struct mapped_device
*md
, struct dm_io
*io
)
588 mempool_free(io
, md
->io_pool
);
591 static void free_tio(struct mapped_device
*md
, struct dm_target_io
*tio
)
593 bio_put(&tio
->clone
);
596 static struct dm_rq_target_io
*alloc_rq_tio(struct mapped_device
*md
,
599 return mempool_alloc(md
->io_pool
, gfp_mask
);
602 static void free_rq_tio(struct dm_rq_target_io
*tio
)
604 mempool_free(tio
, tio
->md
->io_pool
);
607 static struct request
*alloc_clone_request(struct mapped_device
*md
,
610 return mempool_alloc(md
->rq_pool
, gfp_mask
);
613 static void free_clone_request(struct mapped_device
*md
, struct request
*rq
)
615 mempool_free(rq
, md
->rq_pool
);
618 static int md_in_flight(struct mapped_device
*md
)
620 return atomic_read(&md
->pending
[READ
]) +
621 atomic_read(&md
->pending
[WRITE
]);
624 static void start_io_acct(struct dm_io
*io
)
626 struct mapped_device
*md
= io
->md
;
627 struct bio
*bio
= io
->bio
;
629 int rw
= bio_data_dir(bio
);
631 io
->start_time
= jiffies
;
633 cpu
= part_stat_lock();
634 part_round_stats(cpu
, &dm_disk(md
)->part0
);
636 atomic_set(&dm_disk(md
)->part0
.in_flight
[rw
],
637 atomic_inc_return(&md
->pending
[rw
]));
639 if (unlikely(dm_stats_used(&md
->stats
)))
640 dm_stats_account_io(&md
->stats
, bio
->bi_rw
, bio
->bi_iter
.bi_sector
,
641 bio_sectors(bio
), false, 0, &io
->stats_aux
);
644 static void end_io_acct(struct dm_io
*io
)
646 struct mapped_device
*md
= io
->md
;
647 struct bio
*bio
= io
->bio
;
648 unsigned long duration
= jiffies
- io
->start_time
;
650 int rw
= bio_data_dir(bio
);
652 generic_end_io_acct(rw
, &dm_disk(md
)->part0
, io
->start_time
);
654 if (unlikely(dm_stats_used(&md
->stats
)))
655 dm_stats_account_io(&md
->stats
, bio
->bi_rw
, bio
->bi_iter
.bi_sector
,
656 bio_sectors(bio
), true, duration
, &io
->stats_aux
);
659 * After this is decremented the bio must not be touched if it is
662 pending
= atomic_dec_return(&md
->pending
[rw
]);
663 atomic_set(&dm_disk(md
)->part0
.in_flight
[rw
], pending
);
664 pending
+= atomic_read(&md
->pending
[rw
^0x1]);
666 /* nudge anyone waiting on suspend queue */
672 * Add the bio to the list of deferred io.
674 static void queue_io(struct mapped_device
*md
, struct bio
*bio
)
678 spin_lock_irqsave(&md
->deferred_lock
, flags
);
679 bio_list_add(&md
->deferred
, bio
);
680 spin_unlock_irqrestore(&md
->deferred_lock
, flags
);
681 queue_work(md
->wq
, &md
->work
);
685 * Everyone (including functions in this file), should use this
686 * function to access the md->map field, and make sure they call
687 * dm_put_live_table() when finished.
689 struct dm_table
*dm_get_live_table(struct mapped_device
*md
, int *srcu_idx
) __acquires(md
->io_barrier
)
691 *srcu_idx
= srcu_read_lock(&md
->io_barrier
);
693 return srcu_dereference(md
->map
, &md
->io_barrier
);
696 void dm_put_live_table(struct mapped_device
*md
, int srcu_idx
) __releases(md
->io_barrier
)
698 srcu_read_unlock(&md
->io_barrier
, srcu_idx
);
701 void dm_sync_table(struct mapped_device
*md
)
703 synchronize_srcu(&md
->io_barrier
);
704 synchronize_rcu_expedited();
708 * A fast alternative to dm_get_live_table/dm_put_live_table.
709 * The caller must not block between these two functions.
711 static struct dm_table
*dm_get_live_table_fast(struct mapped_device
*md
) __acquires(RCU
)
714 return rcu_dereference(md
->map
);
717 static void dm_put_live_table_fast(struct mapped_device
*md
) __releases(RCU
)
723 * Open a table device so we can use it as a map destination.
725 static int open_table_device(struct table_device
*td
, dev_t dev
,
726 struct mapped_device
*md
)
728 static char *_claim_ptr
= "I belong to device-mapper";
729 struct block_device
*bdev
;
733 BUG_ON(td
->dm_dev
.bdev
);
735 bdev
= blkdev_get_by_dev(dev
, td
->dm_dev
.mode
| FMODE_EXCL
, _claim_ptr
);
737 return PTR_ERR(bdev
);
739 r
= bd_link_disk_holder(bdev
, dm_disk(md
));
741 blkdev_put(bdev
, td
->dm_dev
.mode
| FMODE_EXCL
);
745 td
->dm_dev
.bdev
= bdev
;
750 * Close a table device that we've been using.
752 static void close_table_device(struct table_device
*td
, struct mapped_device
*md
)
754 if (!td
->dm_dev
.bdev
)
757 bd_unlink_disk_holder(td
->dm_dev
.bdev
, dm_disk(md
));
758 blkdev_put(td
->dm_dev
.bdev
, td
->dm_dev
.mode
| FMODE_EXCL
);
759 td
->dm_dev
.bdev
= NULL
;
762 static struct table_device
*find_table_device(struct list_head
*l
, dev_t dev
,
764 struct table_device
*td
;
766 list_for_each_entry(td
, l
, list
)
767 if (td
->dm_dev
.bdev
->bd_dev
== dev
&& td
->dm_dev
.mode
== mode
)
773 int dm_get_table_device(struct mapped_device
*md
, dev_t dev
, fmode_t mode
,
774 struct dm_dev
**result
) {
776 struct table_device
*td
;
778 mutex_lock(&md
->table_devices_lock
);
779 td
= find_table_device(&md
->table_devices
, dev
, mode
);
781 td
= kmalloc(sizeof(*td
), GFP_KERNEL
);
783 mutex_unlock(&md
->table_devices_lock
);
787 td
->dm_dev
.mode
= mode
;
788 td
->dm_dev
.bdev
= NULL
;
790 if ((r
= open_table_device(td
, dev
, md
))) {
791 mutex_unlock(&md
->table_devices_lock
);
796 format_dev_t(td
->dm_dev
.name
, dev
);
798 atomic_set(&td
->count
, 0);
799 list_add(&td
->list
, &md
->table_devices
);
801 atomic_inc(&td
->count
);
802 mutex_unlock(&md
->table_devices_lock
);
804 *result
= &td
->dm_dev
;
807 EXPORT_SYMBOL_GPL(dm_get_table_device
);
809 void dm_put_table_device(struct mapped_device
*md
, struct dm_dev
*d
)
811 struct table_device
*td
= container_of(d
, struct table_device
, dm_dev
);
813 mutex_lock(&md
->table_devices_lock
);
814 if (atomic_dec_and_test(&td
->count
)) {
815 close_table_device(td
, md
);
819 mutex_unlock(&md
->table_devices_lock
);
821 EXPORT_SYMBOL(dm_put_table_device
);
823 static void free_table_devices(struct list_head
*devices
)
825 struct list_head
*tmp
, *next
;
827 list_for_each_safe(tmp
, next
, devices
) {
828 struct table_device
*td
= list_entry(tmp
, struct table_device
, list
);
830 DMWARN("dm_destroy: %s still exists with %d references",
831 td
->dm_dev
.name
, atomic_read(&td
->count
));
837 * Get the geometry associated with a dm device
839 int dm_get_geometry(struct mapped_device
*md
, struct hd_geometry
*geo
)
847 * Set the geometry of a device.
849 int dm_set_geometry(struct mapped_device
*md
, struct hd_geometry
*geo
)
851 sector_t sz
= (sector_t
)geo
->cylinders
* geo
->heads
* geo
->sectors
;
853 if (geo
->start
> sz
) {
854 DMWARN("Start sector is beyond the geometry limits.");
863 /*-----------------------------------------------------------------
865 * A more elegant soln is in the works that uses the queue
866 * merge fn, unfortunately there are a couple of changes to
867 * the block layer that I want to make for this. So in the
868 * interests of getting something for people to use I give
869 * you this clearly demarcated crap.
870 *---------------------------------------------------------------*/
872 static int __noflush_suspending(struct mapped_device
*md
)
874 return test_bit(DMF_NOFLUSH_SUSPENDING
, &md
->flags
);
878 * Decrements the number of outstanding ios that a bio has been
879 * cloned into, completing the original io if necc.
881 static void dec_pending(struct dm_io
*io
, int error
)
886 struct mapped_device
*md
= io
->md
;
888 /* Push-back supersedes any I/O errors */
889 if (unlikely(error
)) {
890 spin_lock_irqsave(&io
->endio_lock
, flags
);
891 if (!(io
->error
> 0 && __noflush_suspending(md
)))
893 spin_unlock_irqrestore(&io
->endio_lock
, flags
);
896 if (atomic_dec_and_test(&io
->io_count
)) {
897 if (io
->error
== DM_ENDIO_REQUEUE
) {
899 * Target requested pushing back the I/O.
901 spin_lock_irqsave(&md
->deferred_lock
, flags
);
902 if (__noflush_suspending(md
))
903 bio_list_add_head(&md
->deferred
, io
->bio
);
905 /* noflush suspend was interrupted. */
907 spin_unlock_irqrestore(&md
->deferred_lock
, flags
);
910 io_error
= io
->error
;
915 if (io_error
== DM_ENDIO_REQUEUE
)
918 if ((bio
->bi_rw
& REQ_FLUSH
) && bio
->bi_iter
.bi_size
) {
920 * Preflush done for flush with data, reissue
923 bio
->bi_rw
&= ~REQ_FLUSH
;
926 /* done with normal IO or empty flush */
927 trace_block_bio_complete(md
->queue
, bio
, io_error
);
928 bio_endio(bio
, io_error
);
933 static void disable_write_same(struct mapped_device
*md
)
935 struct queue_limits
*limits
= dm_get_queue_limits(md
);
937 /* device doesn't really support WRITE SAME, disable it */
938 limits
->max_write_same_sectors
= 0;
941 static void clone_endio(struct bio
*bio
, int error
)
944 struct dm_target_io
*tio
= container_of(bio
, struct dm_target_io
, clone
);
945 struct dm_io
*io
= tio
->io
;
946 struct mapped_device
*md
= tio
->io
->md
;
947 dm_endio_fn endio
= tio
->ti
->type
->end_io
;
949 if (!bio_flagged(bio
, BIO_UPTODATE
) && !error
)
953 r
= endio(tio
->ti
, bio
, error
);
954 if (r
< 0 || r
== DM_ENDIO_REQUEUE
)
956 * error and requeue request are handled
960 else if (r
== DM_ENDIO_INCOMPLETE
)
961 /* The target will handle the io */
964 DMWARN("unimplemented target endio return value: %d", r
);
969 if (unlikely(r
== -EREMOTEIO
&& (bio
->bi_rw
& REQ_WRITE_SAME
) &&
970 !bdev_get_queue(bio
->bi_bdev
)->limits
.max_write_same_sectors
))
971 disable_write_same(md
);
974 dec_pending(io
, error
);
978 * Partial completion handling for request-based dm
980 static void end_clone_bio(struct bio
*clone
, int error
)
982 struct dm_rq_clone_bio_info
*info
=
983 container_of(clone
, struct dm_rq_clone_bio_info
, clone
);
984 struct dm_rq_target_io
*tio
= info
->tio
;
985 struct bio
*bio
= info
->orig
;
986 unsigned int nr_bytes
= info
->orig
->bi_iter
.bi_size
;
992 * An error has already been detected on the request.
993 * Once error occurred, just let clone->end_io() handle
999 * Don't notice the error to the upper layer yet.
1000 * The error handling decision is made by the target driver,
1001 * when the request is completed.
1008 * I/O for the bio successfully completed.
1009 * Notice the data completion to the upper layer.
1013 * bios are processed from the head of the list.
1014 * So the completing bio should always be rq->bio.
1015 * If it's not, something wrong is happening.
1017 if (tio
->orig
->bio
!= bio
)
1018 DMERR("bio completion is going in the middle of the request");
1021 * Update the original request.
1022 * Do not use blk_end_request() here, because it may complete
1023 * the original request before the clone, and break the ordering.
1025 blk_update_request(tio
->orig
, 0, nr_bytes
);
1029 * Don't touch any member of the md after calling this function because
1030 * the md may be freed in dm_put() at the end of this function.
1031 * Or do dm_get() before calling this function and dm_put() later.
1033 static void rq_completed(struct mapped_device
*md
, int rw
, bool run_queue
)
1035 int nr_requests_pending
;
1037 atomic_dec(&md
->pending
[rw
]);
1039 /* nudge anyone waiting on suspend queue */
1040 nr_requests_pending
= md_in_flight(md
);
1041 if (!nr_requests_pending
)
1045 * Run this off this callpath, as drivers could invoke end_io while
1046 * inside their request_fn (and holding the queue lock). Calling
1047 * back into ->request_fn() could deadlock attempting to grab the
1051 if (!nr_requests_pending
||
1052 (nr_requests_pending
>= md
->queue
->nr_congestion_on
))
1053 blk_run_queue_async(md
->queue
);
1057 * dm_put() must be at the end of this function. See the comment above
1062 static void free_rq_clone(struct request
*clone
)
1064 struct dm_rq_target_io
*tio
= clone
->end_io_data
;
1066 blk_rq_unprep_clone(clone
);
1067 if (clone
->q
&& clone
->q
->mq_ops
)
1068 tio
->ti
->type
->release_clone_rq(clone
);
1070 free_clone_request(tio
->md
, clone
);
1075 * Complete the clone and the original request.
1076 * Must be called without clone's queue lock held,
1077 * see end_clone_request() for more details.
1079 static void dm_end_request(struct request
*clone
, int error
)
1081 int rw
= rq_data_dir(clone
);
1082 struct dm_rq_target_io
*tio
= clone
->end_io_data
;
1083 struct mapped_device
*md
= tio
->md
;
1084 struct request
*rq
= tio
->orig
;
1086 if (rq
->cmd_type
== REQ_TYPE_BLOCK_PC
) {
1087 rq
->errors
= clone
->errors
;
1088 rq
->resid_len
= clone
->resid_len
;
1092 * We are using the sense buffer of the original
1094 * So setting the length of the sense data is enough.
1096 rq
->sense_len
= clone
->sense_len
;
1099 free_rq_clone(clone
);
1100 blk_end_request_all(rq
, error
);
1101 rq_completed(md
, rw
, true);
1104 static void dm_unprep_request(struct request
*rq
)
1106 struct dm_rq_target_io
*tio
= rq
->special
;
1107 struct request
*clone
= tio
->clone
;
1110 rq
->cmd_flags
&= ~REQ_DONTPREP
;
1113 free_rq_clone(clone
);
1117 * Requeue the original request of a clone.
1119 static void dm_requeue_unmapped_original_request(struct mapped_device
*md
,
1122 int rw
= rq_data_dir(rq
);
1123 struct request_queue
*q
= rq
->q
;
1124 unsigned long flags
;
1126 dm_unprep_request(rq
);
1128 spin_lock_irqsave(q
->queue_lock
, flags
);
1129 blk_requeue_request(q
, rq
);
1130 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1132 rq_completed(md
, rw
, false);
1135 static void dm_requeue_unmapped_request(struct request
*clone
)
1137 struct dm_rq_target_io
*tio
= clone
->end_io_data
;
1139 dm_requeue_unmapped_original_request(tio
->md
, tio
->orig
);
1142 static void __stop_queue(struct request_queue
*q
)
1147 static void stop_queue(struct request_queue
*q
)
1149 unsigned long flags
;
1151 spin_lock_irqsave(q
->queue_lock
, flags
);
1153 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1156 static void __start_queue(struct request_queue
*q
)
1158 if (blk_queue_stopped(q
))
1162 static void start_queue(struct request_queue
*q
)
1164 unsigned long flags
;
1166 spin_lock_irqsave(q
->queue_lock
, flags
);
1168 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1171 static void dm_done(struct request
*clone
, int error
, bool mapped
)
1174 struct dm_rq_target_io
*tio
= clone
->end_io_data
;
1175 dm_request_endio_fn rq_end_io
= NULL
;
1178 rq_end_io
= tio
->ti
->type
->rq_end_io
;
1180 if (mapped
&& rq_end_io
)
1181 r
= rq_end_io(tio
->ti
, clone
, error
, &tio
->info
);
1184 if (unlikely(r
== -EREMOTEIO
&& (clone
->cmd_flags
& REQ_WRITE_SAME
) &&
1185 !clone
->q
->limits
.max_write_same_sectors
))
1186 disable_write_same(tio
->md
);
1189 /* The target wants to complete the I/O */
1190 dm_end_request(clone
, r
);
1191 else if (r
== DM_ENDIO_INCOMPLETE
)
1192 /* The target will handle the I/O */
1194 else if (r
== DM_ENDIO_REQUEUE
)
1195 /* The target wants to requeue the I/O */
1196 dm_requeue_unmapped_request(clone
);
1198 DMWARN("unimplemented target endio return value: %d", r
);
1204 * Request completion handler for request-based dm
1206 static void dm_softirq_done(struct request
*rq
)
1209 struct dm_rq_target_io
*tio
= rq
->special
;
1210 struct request
*clone
= tio
->clone
;
1213 blk_end_request_all(rq
, tio
->error
);
1214 rq_completed(tio
->md
, rq_data_dir(rq
), false);
1219 if (rq
->cmd_flags
& REQ_FAILED
)
1222 dm_done(clone
, tio
->error
, mapped
);
1226 * Complete the clone and the original request with the error status
1227 * through softirq context.
1229 static void dm_complete_request(struct request
*rq
, int error
)
1231 struct dm_rq_target_io
*tio
= rq
->special
;
1234 blk_complete_request(rq
);
1238 * Complete the not-mapped clone and the original request with the error status
1239 * through softirq context.
1240 * Target's rq_end_io() function isn't called.
1241 * This may be used when the target's map_rq() or clone_and_map_rq() functions fail.
1243 static void dm_kill_unmapped_request(struct request
*rq
, int error
)
1245 rq
->cmd_flags
|= REQ_FAILED
;
1246 dm_complete_request(rq
, error
);
1250 * Called with the clone's queue lock held
1252 static void end_clone_request(struct request
*clone
, int error
)
1254 struct dm_rq_target_io
*tio
= clone
->end_io_data
;
1256 if (!clone
->q
->mq_ops
) {
1258 * For just cleaning up the information of the queue in which
1259 * the clone was dispatched.
1260 * The clone is *NOT* freed actually here because it is alloced
1261 * from dm own mempool (REQ_ALLOCED isn't set).
1263 __blk_put_request(clone
->q
, clone
);
1267 * Actual request completion is done in a softirq context which doesn't
1268 * hold the clone's queue lock. Otherwise, deadlock could occur because:
1269 * - another request may be submitted by the upper level driver
1270 * of the stacking during the completion
1271 * - the submission which requires queue lock may be done
1272 * against this clone's queue
1274 dm_complete_request(tio
->orig
, error
);
1278 * Return maximum size of I/O possible at the supplied sector up to the current
1281 static sector_t
max_io_len_target_boundary(sector_t sector
, struct dm_target
*ti
)
1283 sector_t target_offset
= dm_target_offset(ti
, sector
);
1285 return ti
->len
- target_offset
;
1288 static sector_t
max_io_len(sector_t sector
, struct dm_target
*ti
)
1290 sector_t len
= max_io_len_target_boundary(sector
, ti
);
1291 sector_t offset
, max_len
;
1294 * Does the target need to split even further?
1296 if (ti
->max_io_len
) {
1297 offset
= dm_target_offset(ti
, sector
);
1298 if (unlikely(ti
->max_io_len
& (ti
->max_io_len
- 1)))
1299 max_len
= sector_div(offset
, ti
->max_io_len
);
1301 max_len
= offset
& (ti
->max_io_len
- 1);
1302 max_len
= ti
->max_io_len
- max_len
;
1311 int dm_set_target_max_io_len(struct dm_target
*ti
, sector_t len
)
1313 if (len
> UINT_MAX
) {
1314 DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1315 (unsigned long long)len
, UINT_MAX
);
1316 ti
->error
= "Maximum size of target IO is too large";
1320 ti
->max_io_len
= (uint32_t) len
;
1324 EXPORT_SYMBOL_GPL(dm_set_target_max_io_len
);
1327 * A target may call dm_accept_partial_bio only from the map routine. It is
1328 * allowed for all bio types except REQ_FLUSH.
1330 * dm_accept_partial_bio informs the dm that the target only wants to process
1331 * additional n_sectors sectors of the bio and the rest of the data should be
1332 * sent in a next bio.
1334 * A diagram that explains the arithmetics:
1335 * +--------------------+---------------+-------+
1337 * +--------------------+---------------+-------+
1339 * <-------------- *tio->len_ptr --------------->
1340 * <------- bi_size ------->
1343 * Region 1 was already iterated over with bio_advance or similar function.
1344 * (it may be empty if the target doesn't use bio_advance)
1345 * Region 2 is the remaining bio size that the target wants to process.
1346 * (it may be empty if region 1 is non-empty, although there is no reason
1348 * The target requires that region 3 is to be sent in the next bio.
1350 * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
1351 * the partially processed part (the sum of regions 1+2) must be the same for all
1352 * copies of the bio.
1354 void dm_accept_partial_bio(struct bio
*bio
, unsigned n_sectors
)
1356 struct dm_target_io
*tio
= container_of(bio
, struct dm_target_io
, clone
);
1357 unsigned bi_size
= bio
->bi_iter
.bi_size
>> SECTOR_SHIFT
;
1358 BUG_ON(bio
->bi_rw
& REQ_FLUSH
);
1359 BUG_ON(bi_size
> *tio
->len_ptr
);
1360 BUG_ON(n_sectors
> bi_size
);
1361 *tio
->len_ptr
-= bi_size
- n_sectors
;
1362 bio
->bi_iter
.bi_size
= n_sectors
<< SECTOR_SHIFT
;
1364 EXPORT_SYMBOL_GPL(dm_accept_partial_bio
);
1366 static void __map_bio(struct dm_target_io
*tio
)
1370 struct mapped_device
*md
;
1371 struct bio
*clone
= &tio
->clone
;
1372 struct dm_target
*ti
= tio
->ti
;
1374 clone
->bi_end_io
= clone_endio
;
1377 * Map the clone. If r == 0 we don't need to do
1378 * anything, the target has assumed ownership of
1381 atomic_inc(&tio
->io
->io_count
);
1382 sector
= clone
->bi_iter
.bi_sector
;
1383 r
= ti
->type
->map(ti
, clone
);
1384 if (r
== DM_MAPIO_REMAPPED
) {
1385 /* the bio has been remapped so dispatch it */
1387 trace_block_bio_remap(bdev_get_queue(clone
->bi_bdev
), clone
,
1388 tio
->io
->bio
->bi_bdev
->bd_dev
, sector
);
1390 generic_make_request(clone
);
1391 } else if (r
< 0 || r
== DM_MAPIO_REQUEUE
) {
1392 /* error the io and bail out, or requeue it if needed */
1394 dec_pending(tio
->io
, r
);
1397 DMWARN("unimplemented target map return value: %d", r
);
1403 struct mapped_device
*md
;
1404 struct dm_table
*map
;
1408 unsigned sector_count
;
1411 static void bio_setup_sector(struct bio
*bio
, sector_t sector
, unsigned len
)
1413 bio
->bi_iter
.bi_sector
= sector
;
1414 bio
->bi_iter
.bi_size
= to_bytes(len
);
1418 * Creates a bio that consists of range of complete bvecs.
1420 static void clone_bio(struct dm_target_io
*tio
, struct bio
*bio
,
1421 sector_t sector
, unsigned len
)
1423 struct bio
*clone
= &tio
->clone
;
1425 __bio_clone_fast(clone
, bio
);
1427 if (bio_integrity(bio
))
1428 bio_integrity_clone(clone
, bio
, GFP_NOIO
);
1430 bio_advance(clone
, to_bytes(sector
- clone
->bi_iter
.bi_sector
));
1431 clone
->bi_iter
.bi_size
= to_bytes(len
);
1433 if (bio_integrity(bio
))
1434 bio_integrity_trim(clone
, 0, len
);
1437 static struct dm_target_io
*alloc_tio(struct clone_info
*ci
,
1438 struct dm_target
*ti
,
1439 unsigned target_bio_nr
)
1441 struct dm_target_io
*tio
;
1444 clone
= bio_alloc_bioset(GFP_NOIO
, 0, ci
->md
->bs
);
1445 tio
= container_of(clone
, struct dm_target_io
, clone
);
1449 tio
->target_bio_nr
= target_bio_nr
;
1454 static void __clone_and_map_simple_bio(struct clone_info
*ci
,
1455 struct dm_target
*ti
,
1456 unsigned target_bio_nr
, unsigned *len
)
1458 struct dm_target_io
*tio
= alloc_tio(ci
, ti
, target_bio_nr
);
1459 struct bio
*clone
= &tio
->clone
;
1463 __bio_clone_fast(clone
, ci
->bio
);
1465 bio_setup_sector(clone
, ci
->sector
, *len
);
1470 static void __send_duplicate_bios(struct clone_info
*ci
, struct dm_target
*ti
,
1471 unsigned num_bios
, unsigned *len
)
1473 unsigned target_bio_nr
;
1475 for (target_bio_nr
= 0; target_bio_nr
< num_bios
; target_bio_nr
++)
1476 __clone_and_map_simple_bio(ci
, ti
, target_bio_nr
, len
);
1479 static int __send_empty_flush(struct clone_info
*ci
)
1481 unsigned target_nr
= 0;
1482 struct dm_target
*ti
;
1484 BUG_ON(bio_has_data(ci
->bio
));
1485 while ((ti
= dm_table_get_target(ci
->map
, target_nr
++)))
1486 __send_duplicate_bios(ci
, ti
, ti
->num_flush_bios
, NULL
);
1491 static void __clone_and_map_data_bio(struct clone_info
*ci
, struct dm_target
*ti
,
1492 sector_t sector
, unsigned *len
)
1494 struct bio
*bio
= ci
->bio
;
1495 struct dm_target_io
*tio
;
1496 unsigned target_bio_nr
;
1497 unsigned num_target_bios
= 1;
1500 * Does the target want to receive duplicate copies of the bio?
1502 if (bio_data_dir(bio
) == WRITE
&& ti
->num_write_bios
)
1503 num_target_bios
= ti
->num_write_bios(ti
, bio
);
1505 for (target_bio_nr
= 0; target_bio_nr
< num_target_bios
; target_bio_nr
++) {
1506 tio
= alloc_tio(ci
, ti
, target_bio_nr
);
1508 clone_bio(tio
, bio
, sector
, *len
);
1513 typedef unsigned (*get_num_bios_fn
)(struct dm_target
*ti
);
1515 static unsigned get_num_discard_bios(struct dm_target
*ti
)
1517 return ti
->num_discard_bios
;
1520 static unsigned get_num_write_same_bios(struct dm_target
*ti
)
1522 return ti
->num_write_same_bios
;
1525 typedef bool (*is_split_required_fn
)(struct dm_target
*ti
);
1527 static bool is_split_required_for_discard(struct dm_target
*ti
)
1529 return ti
->split_discard_bios
;
1532 static int __send_changing_extent_only(struct clone_info
*ci
,
1533 get_num_bios_fn get_num_bios
,
1534 is_split_required_fn is_split_required
)
1536 struct dm_target
*ti
;
1541 ti
= dm_table_find_target(ci
->map
, ci
->sector
);
1542 if (!dm_target_is_valid(ti
))
1546 * Even though the device advertised support for this type of
1547 * request, that does not mean every target supports it, and
1548 * reconfiguration might also have changed that since the
1549 * check was performed.
1551 num_bios
= get_num_bios
? get_num_bios(ti
) : 0;
1555 if (is_split_required
&& !is_split_required(ti
))
1556 len
= min((sector_t
)ci
->sector_count
, max_io_len_target_boundary(ci
->sector
, ti
));
1558 len
= min((sector_t
)ci
->sector_count
, max_io_len(ci
->sector
, ti
));
1560 __send_duplicate_bios(ci
, ti
, num_bios
, &len
);
1563 } while (ci
->sector_count
-= len
);
1568 static int __send_discard(struct clone_info
*ci
)
1570 return __send_changing_extent_only(ci
, get_num_discard_bios
,
1571 is_split_required_for_discard
);
1574 static int __send_write_same(struct clone_info
*ci
)
1576 return __send_changing_extent_only(ci
, get_num_write_same_bios
, NULL
);
1580 * Select the correct strategy for processing a non-flush bio.
1582 static int __split_and_process_non_flush(struct clone_info
*ci
)
1584 struct bio
*bio
= ci
->bio
;
1585 struct dm_target
*ti
;
1588 if (unlikely(bio
->bi_rw
& REQ_DISCARD
))
1589 return __send_discard(ci
);
1590 else if (unlikely(bio
->bi_rw
& REQ_WRITE_SAME
))
1591 return __send_write_same(ci
);
1593 ti
= dm_table_find_target(ci
->map
, ci
->sector
);
1594 if (!dm_target_is_valid(ti
))
1597 len
= min_t(sector_t
, max_io_len(ci
->sector
, ti
), ci
->sector_count
);
1599 __clone_and_map_data_bio(ci
, ti
, ci
->sector
, &len
);
1602 ci
->sector_count
-= len
;
1608 * Entry point to split a bio into clones and submit them to the targets.
1610 static void __split_and_process_bio(struct mapped_device
*md
,
1611 struct dm_table
*map
, struct bio
*bio
)
1613 struct clone_info ci
;
1616 if (unlikely(!map
)) {
1623 ci
.io
= alloc_io(md
);
1625 atomic_set(&ci
.io
->io_count
, 1);
1628 spin_lock_init(&ci
.io
->endio_lock
);
1629 ci
.sector
= bio
->bi_iter
.bi_sector
;
1631 start_io_acct(ci
.io
);
1633 if (bio
->bi_rw
& REQ_FLUSH
) {
1634 ci
.bio
= &ci
.md
->flush_bio
;
1635 ci
.sector_count
= 0;
1636 error
= __send_empty_flush(&ci
);
1637 /* dec_pending submits any data associated with flush */
1640 ci
.sector_count
= bio_sectors(bio
);
1641 while (ci
.sector_count
&& !error
)
1642 error
= __split_and_process_non_flush(&ci
);
1645 /* drop the extra reference count */
1646 dec_pending(ci
.io
, error
);
1648 /*-----------------------------------------------------------------
1650 *---------------------------------------------------------------*/
1652 static int dm_merge_bvec(struct request_queue
*q
,
1653 struct bvec_merge_data
*bvm
,
1654 struct bio_vec
*biovec
)
1656 struct mapped_device
*md
= q
->queuedata
;
1657 struct dm_table
*map
= dm_get_live_table_fast(md
);
1658 struct dm_target
*ti
;
1659 sector_t max_sectors
;
1665 ti
= dm_table_find_target(map
, bvm
->bi_sector
);
1666 if (!dm_target_is_valid(ti
))
1670 * Find maximum amount of I/O that won't need splitting
1672 max_sectors
= min(max_io_len(bvm
->bi_sector
, ti
),
1673 (sector_t
) queue_max_sectors(q
));
1674 max_size
= (max_sectors
<< SECTOR_SHIFT
) - bvm
->bi_size
;
1675 if (unlikely(max_size
< 0)) /* this shouldn't _ever_ happen */
1679 * merge_bvec_fn() returns number of bytes
1680 * it can accept at this offset
1681 * max is precomputed maximal io size
1683 if (max_size
&& ti
->type
->merge
)
1684 max_size
= ti
->type
->merge(ti
, bvm
, biovec
, max_size
);
1686 * If the target doesn't support merge method and some of the devices
1687 * provided their merge_bvec method (we know this by looking for the
1688 * max_hw_sectors that dm_set_device_limits may set), then we can't
1689 * allow bios with multiple vector entries. So always set max_size
1690 * to 0, and the code below allows just one page.
1692 else if (queue_max_hw_sectors(q
) <= PAGE_SIZE
>> 9)
1696 dm_put_live_table_fast(md
);
1698 * Always allow an entire first page
1700 if (max_size
<= biovec
->bv_len
&& !(bvm
->bi_size
>> SECTOR_SHIFT
))
1701 max_size
= biovec
->bv_len
;
1707 * The request function that just remaps the bio built up by
1710 static void dm_make_request(struct request_queue
*q
, struct bio
*bio
)
1712 int rw
= bio_data_dir(bio
);
1713 struct mapped_device
*md
= q
->queuedata
;
1715 struct dm_table
*map
;
1717 map
= dm_get_live_table(md
, &srcu_idx
);
1719 generic_start_io_acct(rw
, bio_sectors(bio
), &dm_disk(md
)->part0
);
1721 /* if we're suspended, we have to queue this io for later */
1722 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND
, &md
->flags
))) {
1723 dm_put_live_table(md
, srcu_idx
);
1725 if (bio_rw(bio
) != READA
)
1732 __split_and_process_bio(md
, map
, bio
);
1733 dm_put_live_table(md
, srcu_idx
);
1737 int dm_request_based(struct mapped_device
*md
)
1739 return blk_queue_stackable(md
->queue
);
1742 static void dm_dispatch_clone_request(struct request
*clone
, struct request
*rq
)
1746 if (blk_queue_io_stat(clone
->q
))
1747 clone
->cmd_flags
|= REQ_IO_STAT
;
1749 clone
->start_time
= jiffies
;
1750 r
= blk_insert_cloned_request(clone
->q
, clone
);
1752 /* must complete clone in terms of original request */
1753 dm_complete_request(rq
, r
);
1756 static int dm_rq_bio_constructor(struct bio
*bio
, struct bio
*bio_orig
,
1759 struct dm_rq_target_io
*tio
= data
;
1760 struct dm_rq_clone_bio_info
*info
=
1761 container_of(bio
, struct dm_rq_clone_bio_info
, clone
);
1763 info
->orig
= bio_orig
;
1765 bio
->bi_end_io
= end_clone_bio
;
1770 static int setup_clone(struct request
*clone
, struct request
*rq
,
1771 struct dm_rq_target_io
*tio
, gfp_t gfp_mask
)
1775 r
= blk_rq_prep_clone(clone
, rq
, tio
->md
->bs
, gfp_mask
,
1776 dm_rq_bio_constructor
, tio
);
1780 clone
->cmd
= rq
->cmd
;
1781 clone
->cmd_len
= rq
->cmd_len
;
1782 clone
->sense
= rq
->sense
;
1783 clone
->end_io
= end_clone_request
;
1784 clone
->end_io_data
= tio
;
1791 static struct request
*clone_rq(struct request
*rq
, struct mapped_device
*md
,
1792 struct dm_rq_target_io
*tio
, gfp_t gfp_mask
)
1794 struct request
*clone
= alloc_clone_request(md
, gfp_mask
);
1799 blk_rq_init(NULL
, clone
);
1800 if (setup_clone(clone
, rq
, tio
, gfp_mask
)) {
1802 free_clone_request(md
, clone
);
1809 static void map_tio_request(struct kthread_work
*work
);
1811 static struct dm_rq_target_io
*prep_tio(struct request
*rq
,
1812 struct mapped_device
*md
, gfp_t gfp_mask
)
1814 struct dm_rq_target_io
*tio
;
1816 struct dm_table
*table
;
1818 tio
= alloc_rq_tio(md
, gfp_mask
);
1827 memset(&tio
->info
, 0, sizeof(tio
->info
));
1828 init_kthread_work(&tio
->work
, map_tio_request
);
1830 table
= dm_get_live_table(md
, &srcu_idx
);
1831 if (!dm_table_mq_request_based(table
)) {
1832 if (!clone_rq(rq
, md
, tio
, gfp_mask
)) {
1833 dm_put_live_table(md
, srcu_idx
);
1838 dm_put_live_table(md
, srcu_idx
);
1844 * Called with the queue lock held.
1846 static int dm_prep_fn(struct request_queue
*q
, struct request
*rq
)
1848 struct mapped_device
*md
= q
->queuedata
;
1849 struct dm_rq_target_io
*tio
;
1851 if (unlikely(rq
->special
)) {
1852 DMWARN("Already has something in rq->special.");
1853 return BLKPREP_KILL
;
1856 tio
= prep_tio(rq
, md
, GFP_ATOMIC
);
1858 return BLKPREP_DEFER
;
1861 rq
->cmd_flags
|= REQ_DONTPREP
;
1868 * 0 : the request has been processed
1869 * DM_MAPIO_REQUEUE : the original request needs to be requeued
1870 * < 0 : the request was completed due to failure
1872 static int map_request(struct dm_target
*ti
, struct request
*rq
,
1873 struct mapped_device
*md
)
1876 struct dm_rq_target_io
*tio
= rq
->special
;
1877 struct request
*clone
= NULL
;
1881 r
= ti
->type
->map_rq(ti
, clone
, &tio
->info
);
1883 r
= ti
->type
->clone_and_map_rq(ti
, rq
, &tio
->info
, &clone
);
1885 /* The target wants to complete the I/O */
1886 dm_kill_unmapped_request(rq
, r
);
1890 return DM_MAPIO_REQUEUE
;
1891 if (setup_clone(clone
, rq
, tio
, GFP_KERNEL
)) {
1893 ti
->type
->release_clone_rq(clone
);
1894 return DM_MAPIO_REQUEUE
;
1899 case DM_MAPIO_SUBMITTED
:
1900 /* The target has taken the I/O to submit by itself later */
1902 case DM_MAPIO_REMAPPED
:
1903 /* The target has remapped the I/O so dispatch it */
1904 trace_block_rq_remap(clone
->q
, clone
, disk_devt(dm_disk(md
)),
1906 dm_dispatch_clone_request(clone
, rq
);
1908 case DM_MAPIO_REQUEUE
:
1909 /* The target wants to requeue the I/O */
1910 dm_requeue_unmapped_request(clone
);
1914 DMWARN("unimplemented target map return value: %d", r
);
1918 /* The target wants to complete the I/O */
1919 dm_kill_unmapped_request(rq
, r
);
1926 static void map_tio_request(struct kthread_work
*work
)
1928 struct dm_rq_target_io
*tio
= container_of(work
, struct dm_rq_target_io
, work
);
1929 struct request
*rq
= tio
->orig
;
1930 struct mapped_device
*md
= tio
->md
;
1932 if (map_request(tio
->ti
, rq
, md
) == DM_MAPIO_REQUEUE
)
1933 dm_requeue_unmapped_original_request(md
, rq
);
1936 static void dm_start_request(struct mapped_device
*md
, struct request
*orig
)
1938 blk_start_request(orig
);
1939 atomic_inc(&md
->pending
[rq_data_dir(orig
)]);
1941 if (md
->seq_rq_merge_deadline_usecs
) {
1942 md
->last_rq_pos
= rq_end_sector(orig
);
1943 md
->last_rq_rw
= rq_data_dir(orig
);
1944 md
->last_rq_start_time
= ktime_get();
1948 * Hold the md reference here for the in-flight I/O.
1949 * We can't rely on the reference count by device opener,
1950 * because the device may be closed during the request completion
1951 * when all bios are completed.
1952 * See the comment in rq_completed() too.
1957 #define MAX_SEQ_RQ_MERGE_DEADLINE_USECS 100000
1959 ssize_t
dm_attr_rq_based_seq_io_merge_deadline_show(struct mapped_device
*md
, char *buf
)
1961 return sprintf(buf
, "%u\n", md
->seq_rq_merge_deadline_usecs
);
1964 ssize_t
dm_attr_rq_based_seq_io_merge_deadline_store(struct mapped_device
*md
,
1965 const char *buf
, size_t count
)
1969 if (!dm_request_based(md
))
1972 if (kstrtouint(buf
, 10, &deadline
))
1975 if (deadline
> MAX_SEQ_RQ_MERGE_DEADLINE_USECS
)
1976 deadline
= MAX_SEQ_RQ_MERGE_DEADLINE_USECS
;
1978 md
->seq_rq_merge_deadline_usecs
= deadline
;
1983 static bool dm_request_peeked_before_merge_deadline(struct mapped_device
*md
)
1985 ktime_t kt_deadline
;
1987 if (!md
->seq_rq_merge_deadline_usecs
)
1990 kt_deadline
= ns_to_ktime((u64
)md
->seq_rq_merge_deadline_usecs
* NSEC_PER_USEC
);
1991 kt_deadline
= ktime_add_safe(md
->last_rq_start_time
, kt_deadline
);
1993 return !ktime_after(ktime_get(), kt_deadline
);
1997 * q->request_fn for request-based dm.
1998 * Called with the queue lock held.
2000 static void dm_request_fn(struct request_queue
*q
)
2002 struct mapped_device
*md
= q
->queuedata
;
2004 struct dm_table
*map
= dm_get_live_table(md
, &srcu_idx
);
2005 struct dm_target
*ti
;
2007 struct dm_rq_target_io
*tio
;
2011 * For suspend, check blk_queue_stopped() and increment
2012 * ->pending within a single queue_lock not to increment the
2013 * number of in-flight I/Os after the queue is stopped in
2016 while (!blk_queue_stopped(q
)) {
2017 rq
= blk_peek_request(q
);
2021 /* always use block 0 to find the target for flushes for now */
2023 if (!(rq
->cmd_flags
& REQ_FLUSH
))
2024 pos
= blk_rq_pos(rq
);
2026 ti
= dm_table_find_target(map
, pos
);
2027 if (!dm_target_is_valid(ti
)) {
2029 * Must perform setup, that rq_completed() requires,
2030 * before calling dm_kill_unmapped_request
2032 DMERR_LIMIT("request attempted access beyond the end of device");
2033 dm_start_request(md
, rq
);
2034 dm_kill_unmapped_request(rq
, -EIO
);
2038 if (dm_request_peeked_before_merge_deadline(md
) &&
2039 md_in_flight(md
) && rq
->bio
&& rq
->bio
->bi_vcnt
== 1 &&
2040 md
->last_rq_pos
== pos
&& md
->last_rq_rw
== rq_data_dir(rq
))
2043 if (ti
->type
->busy
&& ti
->type
->busy(ti
))
2046 dm_start_request(md
, rq
);
2049 /* Establish tio->ti before queuing work (map_tio_request) */
2051 queue_kthread_work(&md
->kworker
, &tio
->work
);
2052 BUG_ON(!irqs_disabled());
2058 blk_delay_queue(q
, HZ
/ 100);
2060 dm_put_live_table(md
, srcu_idx
);
2063 static int dm_any_congested(void *congested_data
, int bdi_bits
)
2066 struct mapped_device
*md
= congested_data
;
2067 struct dm_table
*map
;
2069 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND
, &md
->flags
)) {
2070 map
= dm_get_live_table_fast(md
);
2073 * Request-based dm cares about only own queue for
2074 * the query about congestion status of request_queue
2076 if (dm_request_based(md
))
2077 r
= md
->queue
->backing_dev_info
.state
&
2080 r
= dm_table_any_congested(map
, bdi_bits
);
2082 dm_put_live_table_fast(md
);
2088 /*-----------------------------------------------------------------
2089 * An IDR is used to keep track of allocated minor numbers.
2090 *---------------------------------------------------------------*/
2091 static void free_minor(int minor
)
2093 spin_lock(&_minor_lock
);
2094 idr_remove(&_minor_idr
, minor
);
2095 spin_unlock(&_minor_lock
);
2099 * See if the device with a specific minor # is free.
2101 static int specific_minor(int minor
)
2105 if (minor
>= (1 << MINORBITS
))
2108 idr_preload(GFP_KERNEL
);
2109 spin_lock(&_minor_lock
);
2111 r
= idr_alloc(&_minor_idr
, MINOR_ALLOCED
, minor
, minor
+ 1, GFP_NOWAIT
);
2113 spin_unlock(&_minor_lock
);
2116 return r
== -ENOSPC
? -EBUSY
: r
;
2120 static int next_free_minor(int *minor
)
2124 idr_preload(GFP_KERNEL
);
2125 spin_lock(&_minor_lock
);
2127 r
= idr_alloc(&_minor_idr
, MINOR_ALLOCED
, 0, 1 << MINORBITS
, GFP_NOWAIT
);
2129 spin_unlock(&_minor_lock
);
2137 static const struct block_device_operations dm_blk_dops
;
2139 static void dm_wq_work(struct work_struct
*work
);
2141 static void dm_init_md_queue(struct mapped_device
*md
)
2144 * Request-based dm devices cannot be stacked on top of bio-based dm
2145 * devices. The type of this dm device has not been decided yet.
2146 * The type is decided at the first table loading time.
2147 * To prevent problematic device stacking, clear the queue flag
2148 * for request stacking support until then.
2150 * This queue is new, so no concurrency on the queue_flags.
2152 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE
, md
->queue
);
2154 md
->queue
->queuedata
= md
;
2155 md
->queue
->backing_dev_info
.congested_fn
= dm_any_congested
;
2156 md
->queue
->backing_dev_info
.congested_data
= md
;
2158 blk_queue_bounce_limit(md
->queue
, BLK_BOUNCE_ANY
);
2162 * Allocate and initialise a blank device with a given minor.
2164 static struct mapped_device
*alloc_dev(int minor
)
2167 struct mapped_device
*md
= kzalloc(sizeof(*md
), GFP_KERNEL
);
2171 DMWARN("unable to allocate device, out of memory.");
2175 if (!try_module_get(THIS_MODULE
))
2176 goto bad_module_get
;
2178 /* get a minor number for the dev */
2179 if (minor
== DM_ANY_MINOR
)
2180 r
= next_free_minor(&minor
);
2182 r
= specific_minor(minor
);
2186 r
= init_srcu_struct(&md
->io_barrier
);
2188 goto bad_io_barrier
;
2190 md
->type
= DM_TYPE_NONE
;
2191 mutex_init(&md
->suspend_lock
);
2192 mutex_init(&md
->type_lock
);
2193 mutex_init(&md
->table_devices_lock
);
2194 spin_lock_init(&md
->deferred_lock
);
2195 atomic_set(&md
->holders
, 1);
2196 atomic_set(&md
->open_count
, 0);
2197 atomic_set(&md
->event_nr
, 0);
2198 atomic_set(&md
->uevent_seq
, 0);
2199 INIT_LIST_HEAD(&md
->uevent_list
);
2200 INIT_LIST_HEAD(&md
->table_devices
);
2201 spin_lock_init(&md
->uevent_lock
);
2203 md
->queue
= blk_alloc_queue(GFP_KERNEL
);
2207 dm_init_md_queue(md
);
2209 md
->disk
= alloc_disk(1);
2213 atomic_set(&md
->pending
[0], 0);
2214 atomic_set(&md
->pending
[1], 0);
2215 init_waitqueue_head(&md
->wait
);
2216 INIT_WORK(&md
->work
, dm_wq_work
);
2217 init_waitqueue_head(&md
->eventq
);
2218 init_completion(&md
->kobj_holder
.completion
);
2219 md
->kworker_task
= NULL
;
2221 md
->disk
->major
= _major
;
2222 md
->disk
->first_minor
= minor
;
2223 md
->disk
->fops
= &dm_blk_dops
;
2224 md
->disk
->queue
= md
->queue
;
2225 md
->disk
->private_data
= md
;
2226 sprintf(md
->disk
->disk_name
, "dm-%d", minor
);
2228 format_dev_t(md
->name
, MKDEV(_major
, minor
));
2230 md
->wq
= alloc_workqueue("kdmflush", WQ_MEM_RECLAIM
, 0);
2234 md
->bdev
= bdget_disk(md
->disk
, 0);
2238 bio_init(&md
->flush_bio
);
2239 md
->flush_bio
.bi_bdev
= md
->bdev
;
2240 md
->flush_bio
.bi_rw
= WRITE_FLUSH
;
2242 dm_stats_init(&md
->stats
);
2244 /* Populate the mapping, nobody knows we exist yet */
2245 spin_lock(&_minor_lock
);
2246 old_md
= idr_replace(&_minor_idr
, md
, minor
);
2247 spin_unlock(&_minor_lock
);
2249 BUG_ON(old_md
!= MINOR_ALLOCED
);
2254 destroy_workqueue(md
->wq
);
2256 del_gendisk(md
->disk
);
2259 blk_cleanup_queue(md
->queue
);
2261 cleanup_srcu_struct(&md
->io_barrier
);
2265 module_put(THIS_MODULE
);
2271 static void unlock_fs(struct mapped_device
*md
);
2273 static void free_dev(struct mapped_device
*md
)
2275 int minor
= MINOR(disk_devt(md
->disk
));
2278 destroy_workqueue(md
->wq
);
2280 if (md
->kworker_task
)
2281 kthread_stop(md
->kworker_task
);
2283 mempool_destroy(md
->io_pool
);
2285 mempool_destroy(md
->rq_pool
);
2287 bioset_free(md
->bs
);
2289 cleanup_srcu_struct(&md
->io_barrier
);
2290 free_table_devices(&md
->table_devices
);
2291 dm_stats_cleanup(&md
->stats
);
2293 spin_lock(&_minor_lock
);
2294 md
->disk
->private_data
= NULL
;
2295 spin_unlock(&_minor_lock
);
2296 if (blk_get_integrity(md
->disk
))
2297 blk_integrity_unregister(md
->disk
);
2298 del_gendisk(md
->disk
);
2300 blk_cleanup_queue(md
->queue
);
2304 module_put(THIS_MODULE
);
2308 static void __bind_mempools(struct mapped_device
*md
, struct dm_table
*t
)
2310 struct dm_md_mempools
*p
= dm_table_get_md_mempools(t
);
2312 if (md
->io_pool
&& md
->bs
) {
2313 /* The md already has necessary mempools. */
2314 if (dm_table_get_type(t
) == DM_TYPE_BIO_BASED
) {
2316 * Reload bioset because front_pad may have changed
2317 * because a different table was loaded.
2319 bioset_free(md
->bs
);
2324 * There's no need to reload with request-based dm
2325 * because the size of front_pad doesn't change.
2326 * Note for future: If you are to reload bioset,
2327 * prep-ed requests in the queue may refer
2328 * to bio from the old bioset, so you must walk
2329 * through the queue to unprep.
2334 BUG_ON(!p
|| md
->io_pool
|| md
->rq_pool
|| md
->bs
);
2336 md
->io_pool
= p
->io_pool
;
2338 md
->rq_pool
= p
->rq_pool
;
2344 /* mempool bind completed, now no need any mempools in the table */
2345 dm_table_free_md_mempools(t
);
2349 * Bind a table to the device.
2351 static void event_callback(void *context
)
2353 unsigned long flags
;
2355 struct mapped_device
*md
= (struct mapped_device
*) context
;
2357 spin_lock_irqsave(&md
->uevent_lock
, flags
);
2358 list_splice_init(&md
->uevent_list
, &uevents
);
2359 spin_unlock_irqrestore(&md
->uevent_lock
, flags
);
2361 dm_send_uevents(&uevents
, &disk_to_dev(md
->disk
)->kobj
);
2363 atomic_inc(&md
->event_nr
);
2364 wake_up(&md
->eventq
);
2368 * Protected by md->suspend_lock obtained by dm_swap_table().
2370 static void __set_size(struct mapped_device
*md
, sector_t size
)
2372 set_capacity(md
->disk
, size
);
2374 i_size_write(md
->bdev
->bd_inode
, (loff_t
)size
<< SECTOR_SHIFT
);
2378 * Return 1 if the queue has a compulsory merge_bvec_fn function.
2380 * If this function returns 0, then the device is either a non-dm
2381 * device without a merge_bvec_fn, or it is a dm device that is
2382 * able to split any bios it receives that are too big.
2384 int dm_queue_merge_is_compulsory(struct request_queue
*q
)
2386 struct mapped_device
*dev_md
;
2388 if (!q
->merge_bvec_fn
)
2391 if (q
->make_request_fn
== dm_make_request
) {
2392 dev_md
= q
->queuedata
;
2393 if (test_bit(DMF_MERGE_IS_OPTIONAL
, &dev_md
->flags
))
2400 static int dm_device_merge_is_compulsory(struct dm_target
*ti
,
2401 struct dm_dev
*dev
, sector_t start
,
2402 sector_t len
, void *data
)
2404 struct block_device
*bdev
= dev
->bdev
;
2405 struct request_queue
*q
= bdev_get_queue(bdev
);
2407 return dm_queue_merge_is_compulsory(q
);
2411 * Return 1 if it is acceptable to ignore merge_bvec_fn based
2412 * on the properties of the underlying devices.
2414 static int dm_table_merge_is_optional(struct dm_table
*table
)
2417 struct dm_target
*ti
;
2419 while (i
< dm_table_get_num_targets(table
)) {
2420 ti
= dm_table_get_target(table
, i
++);
2422 if (ti
->type
->iterate_devices
&&
2423 ti
->type
->iterate_devices(ti
, dm_device_merge_is_compulsory
, NULL
))
2431 * Returns old map, which caller must destroy.
2433 static struct dm_table
*__bind(struct mapped_device
*md
, struct dm_table
*t
,
2434 struct queue_limits
*limits
)
2436 struct dm_table
*old_map
;
2437 struct request_queue
*q
= md
->queue
;
2439 int merge_is_optional
;
2441 size
= dm_table_get_size(t
);
2444 * Wipe any geometry if the size of the table changed.
2446 if (size
!= dm_get_size(md
))
2447 memset(&md
->geometry
, 0, sizeof(md
->geometry
));
2449 __set_size(md
, size
);
2451 dm_table_event_callback(t
, event_callback
, md
);
2454 * The queue hasn't been stopped yet, if the old table type wasn't
2455 * for request-based during suspension. So stop it to prevent
2456 * I/O mapping before resume.
2457 * This must be done before setting the queue restrictions,
2458 * because request-based dm may be run just after the setting.
2460 if (dm_table_request_based(t
) && !blk_queue_stopped(q
))
2463 __bind_mempools(md
, t
);
2465 merge_is_optional
= dm_table_merge_is_optional(t
);
2467 old_map
= rcu_dereference_protected(md
->map
, lockdep_is_held(&md
->suspend_lock
));
2468 rcu_assign_pointer(md
->map
, t
);
2469 md
->immutable_target_type
= dm_table_get_immutable_target_type(t
);
2471 dm_table_set_restrictions(t
, q
, limits
);
2472 if (merge_is_optional
)
2473 set_bit(DMF_MERGE_IS_OPTIONAL
, &md
->flags
);
2475 clear_bit(DMF_MERGE_IS_OPTIONAL
, &md
->flags
);
2483 * Returns unbound table for the caller to free.
2485 static struct dm_table
*__unbind(struct mapped_device
*md
)
2487 struct dm_table
*map
= rcu_dereference_protected(md
->map
, 1);
2492 dm_table_event_callback(map
, NULL
, NULL
);
2493 RCU_INIT_POINTER(md
->map
, NULL
);
2500 * Constructor for a new device.
2502 int dm_create(int minor
, struct mapped_device
**result
)
2504 struct mapped_device
*md
;
2506 md
= alloc_dev(minor
);
2517 * Functions to manage md->type.
2518 * All are required to hold md->type_lock.
2520 void dm_lock_md_type(struct mapped_device
*md
)
2522 mutex_lock(&md
->type_lock
);
2525 void dm_unlock_md_type(struct mapped_device
*md
)
2527 mutex_unlock(&md
->type_lock
);
2530 void dm_set_md_type(struct mapped_device
*md
, unsigned type
)
2532 BUG_ON(!mutex_is_locked(&md
->type_lock
));
2536 unsigned dm_get_md_type(struct mapped_device
*md
)
2538 BUG_ON(!mutex_is_locked(&md
->type_lock
));
2542 static bool dm_md_type_request_based(struct mapped_device
*md
)
2544 unsigned table_type
= dm_get_md_type(md
);
2546 return (table_type
== DM_TYPE_REQUEST_BASED
||
2547 table_type
== DM_TYPE_MQ_REQUEST_BASED
);
2550 struct target_type
*dm_get_immutable_target_type(struct mapped_device
*md
)
2552 return md
->immutable_target_type
;
2556 * The queue_limits are only valid as long as you have a reference
2559 struct queue_limits
*dm_get_queue_limits(struct mapped_device
*md
)
2561 BUG_ON(!atomic_read(&md
->holders
));
2562 return &md
->queue
->limits
;
2564 EXPORT_SYMBOL_GPL(dm_get_queue_limits
);
2567 * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
2569 static int dm_init_request_based_queue(struct mapped_device
*md
)
2571 struct request_queue
*q
= NULL
;
2573 if (md
->queue
->elevator
)
2576 /* Fully initialize the queue */
2577 q
= blk_init_allocated_queue(md
->queue
, dm_request_fn
, NULL
);
2581 /* disable dm_request_fn's merge heuristic by default */
2582 md
->seq_rq_merge_deadline_usecs
= 0;
2585 dm_init_md_queue(md
);
2586 blk_queue_softirq_done(md
->queue
, dm_softirq_done
);
2587 blk_queue_prep_rq(md
->queue
, dm_prep_fn
);
2589 /* Also initialize the request-based DM worker thread */
2590 init_kthread_worker(&md
->kworker
);
2591 md
->kworker_task
= kthread_run(kthread_worker_fn
, &md
->kworker
,
2592 "kdmwork-%s", dm_device_name(md
));
2594 elv_register_queue(md
->queue
);
2600 * Setup the DM device's queue based on md's type
2602 int dm_setup_md_queue(struct mapped_device
*md
)
2604 if (dm_md_type_request_based(md
)) {
2605 if (!dm_init_request_based_queue(md
)) {
2606 DMWARN("Cannot initialize queue for request-based mapped device");
2610 /* bio-based specific initialization */
2611 blk_queue_make_request(md
->queue
, dm_make_request
);
2612 blk_queue_merge_bvec(md
->queue
, dm_merge_bvec
);
2618 struct mapped_device
*dm_get_md(dev_t dev
)
2620 struct mapped_device
*md
;
2621 unsigned minor
= MINOR(dev
);
2623 if (MAJOR(dev
) != _major
|| minor
>= (1 << MINORBITS
))
2626 spin_lock(&_minor_lock
);
2628 md
= idr_find(&_minor_idr
, minor
);
2630 if ((md
== MINOR_ALLOCED
||
2631 (MINOR(disk_devt(dm_disk(md
))) != minor
) ||
2632 dm_deleting_md(md
) ||
2633 test_bit(DMF_FREEING
, &md
->flags
))) {
2641 spin_unlock(&_minor_lock
);
2645 EXPORT_SYMBOL_GPL(dm_get_md
);
2647 void *dm_get_mdptr(struct mapped_device
*md
)
2649 return md
->interface_ptr
;
2652 void dm_set_mdptr(struct mapped_device
*md
, void *ptr
)
2654 md
->interface_ptr
= ptr
;
2657 void dm_get(struct mapped_device
*md
)
2659 atomic_inc(&md
->holders
);
2660 BUG_ON(test_bit(DMF_FREEING
, &md
->flags
));
2663 int dm_hold(struct mapped_device
*md
)
2665 spin_lock(&_minor_lock
);
2666 if (test_bit(DMF_FREEING
, &md
->flags
)) {
2667 spin_unlock(&_minor_lock
);
2671 spin_unlock(&_minor_lock
);
2674 EXPORT_SYMBOL_GPL(dm_hold
);
2676 const char *dm_device_name(struct mapped_device
*md
)
2680 EXPORT_SYMBOL_GPL(dm_device_name
);
2682 static void __dm_destroy(struct mapped_device
*md
, bool wait
)
2684 struct dm_table
*map
;
2689 map
= dm_get_live_table(md
, &srcu_idx
);
2691 spin_lock(&_minor_lock
);
2692 idr_replace(&_minor_idr
, MINOR_ALLOCED
, MINOR(disk_devt(dm_disk(md
))));
2693 set_bit(DMF_FREEING
, &md
->flags
);
2694 spin_unlock(&_minor_lock
);
2696 if (dm_request_based(md
))
2697 flush_kthread_worker(&md
->kworker
);
2700 * Take suspend_lock so that presuspend and postsuspend methods
2701 * do not race with internal suspend.
2703 mutex_lock(&md
->suspend_lock
);
2704 if (!dm_suspended_md(md
)) {
2705 dm_table_presuspend_targets(map
);
2706 dm_table_postsuspend_targets(map
);
2708 mutex_unlock(&md
->suspend_lock
);
2710 /* dm_put_live_table must be before msleep, otherwise deadlock is possible */
2711 dm_put_live_table(md
, srcu_idx
);
2714 * Rare, but there may be I/O requests still going to complete,
2715 * for example. Wait for all references to disappear.
2716 * No one should increment the reference count of the mapped_device,
2717 * after the mapped_device state becomes DMF_FREEING.
2720 while (atomic_read(&md
->holders
))
2722 else if (atomic_read(&md
->holders
))
2723 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2724 dm_device_name(md
), atomic_read(&md
->holders
));
2727 dm_table_destroy(__unbind(md
));
2731 void dm_destroy(struct mapped_device
*md
)
2733 __dm_destroy(md
, true);
2736 void dm_destroy_immediate(struct mapped_device
*md
)
2738 __dm_destroy(md
, false);
2741 void dm_put(struct mapped_device
*md
)
2743 atomic_dec(&md
->holders
);
2745 EXPORT_SYMBOL_GPL(dm_put
);
2747 static int dm_wait_for_completion(struct mapped_device
*md
, int interruptible
)
2750 DECLARE_WAITQUEUE(wait
, current
);
2752 add_wait_queue(&md
->wait
, &wait
);
2755 set_current_state(interruptible
);
2757 if (!md_in_flight(md
))
2760 if (interruptible
== TASK_INTERRUPTIBLE
&&
2761 signal_pending(current
)) {
2768 set_current_state(TASK_RUNNING
);
2770 remove_wait_queue(&md
->wait
, &wait
);
2776 * Process the deferred bios
2778 static void dm_wq_work(struct work_struct
*work
)
2780 struct mapped_device
*md
= container_of(work
, struct mapped_device
,
2784 struct dm_table
*map
;
2786 map
= dm_get_live_table(md
, &srcu_idx
);
2788 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND
, &md
->flags
)) {
2789 spin_lock_irq(&md
->deferred_lock
);
2790 c
= bio_list_pop(&md
->deferred
);
2791 spin_unlock_irq(&md
->deferred_lock
);
2796 if (dm_request_based(md
))
2797 generic_make_request(c
);
2799 __split_and_process_bio(md
, map
, c
);
2802 dm_put_live_table(md
, srcu_idx
);
2805 static void dm_queue_flush(struct mapped_device
*md
)
2807 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND
, &md
->flags
);
2808 smp_mb__after_atomic();
2809 queue_work(md
->wq
, &md
->work
);
2813 * Swap in a new table, returning the old one for the caller to destroy.
2815 struct dm_table
*dm_swap_table(struct mapped_device
*md
, struct dm_table
*table
)
2817 struct dm_table
*live_map
= NULL
, *map
= ERR_PTR(-EINVAL
);
2818 struct queue_limits limits
;
2821 mutex_lock(&md
->suspend_lock
);
2823 /* device must be suspended */
2824 if (!dm_suspended_md(md
))
2828 * If the new table has no data devices, retain the existing limits.
2829 * This helps multipath with queue_if_no_path if all paths disappear,
2830 * then new I/O is queued based on these limits, and then some paths
2833 if (dm_table_has_no_data_devices(table
)) {
2834 live_map
= dm_get_live_table_fast(md
);
2836 limits
= md
->queue
->limits
;
2837 dm_put_live_table_fast(md
);
2841 r
= dm_calculate_queue_limits(table
, &limits
);
2848 map
= __bind(md
, table
, &limits
);
2851 mutex_unlock(&md
->suspend_lock
);
2856 * Functions to lock and unlock any filesystem running on the
2859 static int lock_fs(struct mapped_device
*md
)
2863 WARN_ON(md
->frozen_sb
);
2865 md
->frozen_sb
= freeze_bdev(md
->bdev
);
2866 if (IS_ERR(md
->frozen_sb
)) {
2867 r
= PTR_ERR(md
->frozen_sb
);
2868 md
->frozen_sb
= NULL
;
2872 set_bit(DMF_FROZEN
, &md
->flags
);
2877 static void unlock_fs(struct mapped_device
*md
)
2879 if (!test_bit(DMF_FROZEN
, &md
->flags
))
2882 thaw_bdev(md
->bdev
, md
->frozen_sb
);
2883 md
->frozen_sb
= NULL
;
2884 clear_bit(DMF_FROZEN
, &md
->flags
);
2888 * If __dm_suspend returns 0, the device is completely quiescent
2889 * now. There is no request-processing activity. All new requests
2890 * are being added to md->deferred list.
2892 * Caller must hold md->suspend_lock
2894 static int __dm_suspend(struct mapped_device
*md
, struct dm_table
*map
,
2895 unsigned suspend_flags
, int interruptible
)
2897 bool do_lockfs
= suspend_flags
& DM_SUSPEND_LOCKFS_FLAG
;
2898 bool noflush
= suspend_flags
& DM_SUSPEND_NOFLUSH_FLAG
;
2902 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2903 * This flag is cleared before dm_suspend returns.
2906 set_bit(DMF_NOFLUSH_SUSPENDING
, &md
->flags
);
2909 * This gets reverted if there's an error later and the targets
2910 * provide the .presuspend_undo hook.
2912 dm_table_presuspend_targets(map
);
2915 * Flush I/O to the device.
2916 * Any I/O submitted after lock_fs() may not be flushed.
2917 * noflush takes precedence over do_lockfs.
2918 * (lock_fs() flushes I/Os and waits for them to complete.)
2920 if (!noflush
&& do_lockfs
) {
2923 dm_table_presuspend_undo_targets(map
);
2929 * Here we must make sure that no processes are submitting requests
2930 * to target drivers i.e. no one may be executing
2931 * __split_and_process_bio. This is called from dm_request and
2934 * To get all processes out of __split_and_process_bio in dm_request,
2935 * we take the write lock. To prevent any process from reentering
2936 * __split_and_process_bio from dm_request and quiesce the thread
2937 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
2938 * flush_workqueue(md->wq).
2940 set_bit(DMF_BLOCK_IO_FOR_SUSPEND
, &md
->flags
);
2942 synchronize_srcu(&md
->io_barrier
);
2945 * Stop md->queue before flushing md->wq in case request-based
2946 * dm defers requests to md->wq from md->queue.
2948 if (dm_request_based(md
)) {
2949 stop_queue(md
->queue
);
2950 flush_kthread_worker(&md
->kworker
);
2953 flush_workqueue(md
->wq
);
2956 * At this point no more requests are entering target request routines.
2957 * We call dm_wait_for_completion to wait for all existing requests
2960 r
= dm_wait_for_completion(md
, interruptible
);
2963 clear_bit(DMF_NOFLUSH_SUSPENDING
, &md
->flags
);
2965 synchronize_srcu(&md
->io_barrier
);
2967 /* were we interrupted ? */
2971 if (dm_request_based(md
))
2972 start_queue(md
->queue
);
2975 dm_table_presuspend_undo_targets(map
);
2976 /* pushback list is already flushed, so skip flush */
2983 * We need to be able to change a mapping table under a mounted
2984 * filesystem. For example we might want to move some data in
2985 * the background. Before the table can be swapped with
2986 * dm_bind_table, dm_suspend must be called to flush any in
2987 * flight bios and ensure that any further io gets deferred.
2990 * Suspend mechanism in request-based dm.
2992 * 1. Flush all I/Os by lock_fs() if needed.
2993 * 2. Stop dispatching any I/O by stopping the request_queue.
2994 * 3. Wait for all in-flight I/Os to be completed or requeued.
2996 * To abort suspend, start the request_queue.
2998 int dm_suspend(struct mapped_device
*md
, unsigned suspend_flags
)
3000 struct dm_table
*map
= NULL
;
3004 mutex_lock_nested(&md
->suspend_lock
, SINGLE_DEPTH_NESTING
);
3006 if (dm_suspended_md(md
)) {
3011 if (dm_suspended_internally_md(md
)) {
3012 /* already internally suspended, wait for internal resume */
3013 mutex_unlock(&md
->suspend_lock
);
3014 r
= wait_on_bit(&md
->flags
, DMF_SUSPENDED_INTERNALLY
, TASK_INTERRUPTIBLE
);
3020 map
= rcu_dereference_protected(md
->map
, lockdep_is_held(&md
->suspend_lock
));
3022 r
= __dm_suspend(md
, map
, suspend_flags
, TASK_INTERRUPTIBLE
);
3026 set_bit(DMF_SUSPENDED
, &md
->flags
);
3028 dm_table_postsuspend_targets(map
);
3031 mutex_unlock(&md
->suspend_lock
);
3035 static int __dm_resume(struct mapped_device
*md
, struct dm_table
*map
)
3038 int r
= dm_table_resume_targets(map
);
3046 * Flushing deferred I/Os must be done after targets are resumed
3047 * so that mapping of targets can work correctly.
3048 * Request-based dm is queueing the deferred I/Os in its request_queue.
3050 if (dm_request_based(md
))
3051 start_queue(md
->queue
);
3058 int dm_resume(struct mapped_device
*md
)
3061 struct dm_table
*map
= NULL
;
3064 mutex_lock_nested(&md
->suspend_lock
, SINGLE_DEPTH_NESTING
);
3066 if (!dm_suspended_md(md
))
3069 if (dm_suspended_internally_md(md
)) {
3070 /* already internally suspended, wait for internal resume */
3071 mutex_unlock(&md
->suspend_lock
);
3072 r
= wait_on_bit(&md
->flags
, DMF_SUSPENDED_INTERNALLY
, TASK_INTERRUPTIBLE
);
3078 map
= rcu_dereference_protected(md
->map
, lockdep_is_held(&md
->suspend_lock
));
3079 if (!map
|| !dm_table_get_size(map
))
3082 r
= __dm_resume(md
, map
);
3086 clear_bit(DMF_SUSPENDED
, &md
->flags
);
3090 mutex_unlock(&md
->suspend_lock
);
3096 * Internal suspend/resume works like userspace-driven suspend. It waits
3097 * until all bios finish and prevents issuing new bios to the target drivers.
3098 * It may be used only from the kernel.
3101 static void __dm_internal_suspend(struct mapped_device
*md
, unsigned suspend_flags
)
3103 struct dm_table
*map
= NULL
;
3105 if (md
->internal_suspend_count
++)
3106 return; /* nested internal suspend */
3108 if (dm_suspended_md(md
)) {
3109 set_bit(DMF_SUSPENDED_INTERNALLY
, &md
->flags
);
3110 return; /* nest suspend */
3113 map
= rcu_dereference_protected(md
->map
, lockdep_is_held(&md
->suspend_lock
));
3116 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
3117 * supported. Properly supporting a TASK_INTERRUPTIBLE internal suspend
3118 * would require changing .presuspend to return an error -- avoid this
3119 * until there is a need for more elaborate variants of internal suspend.
3121 (void) __dm_suspend(md
, map
, suspend_flags
, TASK_UNINTERRUPTIBLE
);
3123 set_bit(DMF_SUSPENDED_INTERNALLY
, &md
->flags
);
3125 dm_table_postsuspend_targets(map
);
3128 static void __dm_internal_resume(struct mapped_device
*md
)
3130 BUG_ON(!md
->internal_suspend_count
);
3132 if (--md
->internal_suspend_count
)
3133 return; /* resume from nested internal suspend */
3135 if (dm_suspended_md(md
))
3136 goto done
; /* resume from nested suspend */
3139 * NOTE: existing callers don't need to call dm_table_resume_targets
3140 * (which may fail -- so best to avoid it for now by passing NULL map)
3142 (void) __dm_resume(md
, NULL
);
3145 clear_bit(DMF_SUSPENDED_INTERNALLY
, &md
->flags
);
3146 smp_mb__after_atomic();
3147 wake_up_bit(&md
->flags
, DMF_SUSPENDED_INTERNALLY
);
3150 void dm_internal_suspend_noflush(struct mapped_device
*md
)
3152 mutex_lock(&md
->suspend_lock
);
3153 __dm_internal_suspend(md
, DM_SUSPEND_NOFLUSH_FLAG
);
3154 mutex_unlock(&md
->suspend_lock
);
3156 EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush
);
3158 void dm_internal_resume(struct mapped_device
*md
)
3160 mutex_lock(&md
->suspend_lock
);
3161 __dm_internal_resume(md
);
3162 mutex_unlock(&md
->suspend_lock
);
3164 EXPORT_SYMBOL_GPL(dm_internal_resume
);
3167 * Fast variants of internal suspend/resume hold md->suspend_lock,
3168 * which prevents interaction with userspace-driven suspend.
3171 void dm_internal_suspend_fast(struct mapped_device
*md
)
3173 mutex_lock(&md
->suspend_lock
);
3174 if (dm_suspended_md(md
) || dm_suspended_internally_md(md
))
3177 set_bit(DMF_BLOCK_IO_FOR_SUSPEND
, &md
->flags
);
3178 synchronize_srcu(&md
->io_barrier
);
3179 flush_workqueue(md
->wq
);
3180 dm_wait_for_completion(md
, TASK_UNINTERRUPTIBLE
);
3182 EXPORT_SYMBOL_GPL(dm_internal_suspend_fast
);
3184 void dm_internal_resume_fast(struct mapped_device
*md
)
3186 if (dm_suspended_md(md
) || dm_suspended_internally_md(md
))
3192 mutex_unlock(&md
->suspend_lock
);
3194 EXPORT_SYMBOL_GPL(dm_internal_resume_fast
);
3196 /*-----------------------------------------------------------------
3197 * Event notification.
3198 *---------------------------------------------------------------*/
3199 int dm_kobject_uevent(struct mapped_device
*md
, enum kobject_action action
,
3202 char udev_cookie
[DM_COOKIE_LENGTH
];
3203 char *envp
[] = { udev_cookie
, NULL
};
3206 return kobject_uevent(&disk_to_dev(md
->disk
)->kobj
, action
);
3208 snprintf(udev_cookie
, DM_COOKIE_LENGTH
, "%s=%u",
3209 DM_COOKIE_ENV_VAR_NAME
, cookie
);
3210 return kobject_uevent_env(&disk_to_dev(md
->disk
)->kobj
,
3215 uint32_t dm_next_uevent_seq(struct mapped_device
*md
)
3217 return atomic_add_return(1, &md
->uevent_seq
);
3220 uint32_t dm_get_event_nr(struct mapped_device
*md
)
3222 return atomic_read(&md
->event_nr
);
3225 int dm_wait_event(struct mapped_device
*md
, int event_nr
)
3227 return wait_event_interruptible(md
->eventq
,
3228 (event_nr
!= atomic_read(&md
->event_nr
)));
3231 void dm_uevent_add(struct mapped_device
*md
, struct list_head
*elist
)
3233 unsigned long flags
;
3235 spin_lock_irqsave(&md
->uevent_lock
, flags
);
3236 list_add(elist
, &md
->uevent_list
);
3237 spin_unlock_irqrestore(&md
->uevent_lock
, flags
);
3241 * The gendisk is only valid as long as you have a reference
3244 struct gendisk
*dm_disk(struct mapped_device
*md
)
3249 struct kobject
*dm_kobject(struct mapped_device
*md
)
3251 return &md
->kobj_holder
.kobj
;
3254 struct mapped_device
*dm_get_from_kobject(struct kobject
*kobj
)
3256 struct mapped_device
*md
;
3258 md
= container_of(kobj
, struct mapped_device
, kobj_holder
.kobj
);
3260 if (test_bit(DMF_FREEING
, &md
->flags
) ||
3268 int dm_suspended_md(struct mapped_device
*md
)
3270 return test_bit(DMF_SUSPENDED
, &md
->flags
);
3273 int dm_suspended_internally_md(struct mapped_device
*md
)
3275 return test_bit(DMF_SUSPENDED_INTERNALLY
, &md
->flags
);
3278 int dm_test_deferred_remove_flag(struct mapped_device
*md
)
3280 return test_bit(DMF_DEFERRED_REMOVE
, &md
->flags
);
3283 int dm_suspended(struct dm_target
*ti
)
3285 return dm_suspended_md(dm_table_get_md(ti
->table
));
3287 EXPORT_SYMBOL_GPL(dm_suspended
);
3289 int dm_noflush_suspending(struct dm_target
*ti
)
3291 return __noflush_suspending(dm_table_get_md(ti
->table
));
3293 EXPORT_SYMBOL_GPL(dm_noflush_suspending
);
3295 struct dm_md_mempools
*dm_alloc_md_mempools(unsigned type
, unsigned integrity
, unsigned per_bio_data_size
)
3297 struct dm_md_mempools
*pools
= kzalloc(sizeof(*pools
), GFP_KERNEL
);
3298 struct kmem_cache
*cachep
;
3299 unsigned int pool_size
= 0;
3300 unsigned int front_pad
;
3306 case DM_TYPE_BIO_BASED
:
3308 pool_size
= dm_get_reserved_bio_based_ios();
3309 front_pad
= roundup(per_bio_data_size
, __alignof__(struct dm_target_io
)) + offsetof(struct dm_target_io
, clone
);
3311 case DM_TYPE_REQUEST_BASED
:
3312 pool_size
= dm_get_reserved_rq_based_ios();
3313 pools
->rq_pool
= mempool_create_slab_pool(pool_size
, _rq_cache
);
3314 if (!pools
->rq_pool
)
3316 /* fall through to setup remaining rq-based pools */
3317 case DM_TYPE_MQ_REQUEST_BASED
:
3318 cachep
= _rq_tio_cache
;
3320 pool_size
= dm_get_reserved_rq_based_ios();
3321 front_pad
= offsetof(struct dm_rq_clone_bio_info
, clone
);
3322 /* per_bio_data_size is not used. See __bind_mempools(). */
3323 WARN_ON(per_bio_data_size
!= 0);
3329 pools
->io_pool
= mempool_create_slab_pool(pool_size
, cachep
);
3330 if (!pools
->io_pool
)
3333 pools
->bs
= bioset_create_nobvec(pool_size
, front_pad
);
3337 if (integrity
&& bioset_integrity_create(pools
->bs
, pool_size
))
3343 dm_free_md_mempools(pools
);
3348 void dm_free_md_mempools(struct dm_md_mempools
*pools
)
3354 mempool_destroy(pools
->io_pool
);
3357 mempool_destroy(pools
->rq_pool
);
3360 bioset_free(pools
->bs
);
3365 static const struct block_device_operations dm_blk_dops
= {
3366 .open
= dm_blk_open
,
3367 .release
= dm_blk_close
,
3368 .ioctl
= dm_blk_ioctl
,
3369 .getgeo
= dm_blk_getgeo
,
3370 .owner
= THIS_MODULE
3376 module_init(dm_init
);
3377 module_exit(dm_exit
);
3379 module_param(major
, uint
, 0);
3380 MODULE_PARM_DESC(major
, "The major number of the device mapper");
3382 module_param(reserved_bio_based_ios
, uint
, S_IRUGO
| S_IWUSR
);
3383 MODULE_PARM_DESC(reserved_bio_based_ios
, "Reserved IOs in bio-based mempools");
3385 module_param(reserved_rq_based_ios
, uint
, S_IRUGO
| S_IWUSR
);
3386 MODULE_PARM_DESC(reserved_rq_based_ios
, "Reserved IOs in request-based mempools");
3388 MODULE_DESCRIPTION(DM_NAME
" driver");
3389 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
3390 MODULE_LICENSE("GPL");