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
);
59 * One of these is allocated per bio.
62 struct mapped_device
*md
;
66 unsigned long start_time
;
67 spinlock_t endio_lock
;
68 struct dm_stats_aux stats_aux
;
72 * For request-based dm.
73 * One of these is allocated per request.
75 struct dm_rq_target_io
{
76 struct mapped_device
*md
;
78 struct request
*orig
, clone
;
84 * For request-based dm - the bio clones we allocate are embedded in these
87 * We allocate these with bio_alloc_bioset, using the front_pad parameter when
88 * the bioset is created - this means the bio has to come at the end of the
91 struct dm_rq_clone_bio_info
{
93 struct dm_rq_target_io
*tio
;
97 union map_info
*dm_get_mapinfo(struct bio
*bio
)
99 if (bio
&& bio
->bi_private
)
100 return &((struct dm_target_io
*)bio
->bi_private
)->info
;
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
127 * A dummy definition to make RCU happy.
128 * struct dm_table should never be dereferenced in this file.
135 * Work processed by per-device workqueue.
137 struct mapped_device
{
138 struct srcu_struct io_barrier
;
139 struct mutex suspend_lock
;
144 * The current mapping.
145 * Use dm_get_live_table{_fast} or take suspend_lock for
148 struct dm_table
*map
;
152 struct request_queue
*queue
;
154 /* Protect queue and type against concurrent access. */
155 struct mutex type_lock
;
157 struct target_type
*immutable_target_type
;
159 struct gendisk
*disk
;
165 * A list of ios that arrived while we were suspended.
168 wait_queue_head_t wait
;
169 struct work_struct work
;
170 struct bio_list deferred
;
171 spinlock_t deferred_lock
;
174 * Processing queue (flush)
176 struct workqueue_struct
*wq
;
179 * io objects are allocated from here.
189 wait_queue_head_t eventq
;
191 struct list_head uevent_list
;
192 spinlock_t uevent_lock
; /* Protect access to uevent_list */
195 * freeze/thaw support require holding onto a super block
197 struct super_block
*frozen_sb
;
198 struct block_device
*bdev
;
200 /* forced geometry settings */
201 struct hd_geometry geometry
;
203 /* kobject and completion */
204 struct dm_kobject_holder kobj_holder
;
206 /* zero-length flush that will be cloned and submitted to targets */
207 struct bio flush_bio
;
209 struct dm_stats stats
;
213 * For mempools pre-allocation at the table loading time.
215 struct dm_md_mempools
{
220 #define RESERVED_BIO_BASED_IOS 16
221 #define RESERVED_REQUEST_BASED_IOS 256
222 #define RESERVED_MAX_IOS 1024
223 static struct kmem_cache
*_io_cache
;
224 static struct kmem_cache
*_rq_tio_cache
;
227 * Bio-based DM's mempools' reserved IOs set by the user.
229 static unsigned reserved_bio_based_ios
= RESERVED_BIO_BASED_IOS
;
232 * Request-based DM's mempools' reserved IOs set by the user.
234 static unsigned reserved_rq_based_ios
= RESERVED_REQUEST_BASED_IOS
;
236 static unsigned __dm_get_reserved_ios(unsigned *reserved_ios
,
237 unsigned def
, unsigned max
)
239 unsigned ios
= ACCESS_ONCE(*reserved_ios
);
240 unsigned modified_ios
= 0;
248 (void)cmpxchg(reserved_ios
, ios
, modified_ios
);
255 unsigned dm_get_reserved_bio_based_ios(void)
257 return __dm_get_reserved_ios(&reserved_bio_based_ios
,
258 RESERVED_BIO_BASED_IOS
, RESERVED_MAX_IOS
);
260 EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios
);
262 unsigned dm_get_reserved_rq_based_ios(void)
264 return __dm_get_reserved_ios(&reserved_rq_based_ios
,
265 RESERVED_REQUEST_BASED_IOS
, RESERVED_MAX_IOS
);
267 EXPORT_SYMBOL_GPL(dm_get_reserved_rq_based_ios
);
269 static int __init
local_init(void)
273 /* allocate a slab for the dm_ios */
274 _io_cache
= KMEM_CACHE(dm_io
, 0);
278 _rq_tio_cache
= KMEM_CACHE(dm_rq_target_io
, 0);
280 goto out_free_io_cache
;
282 r
= dm_uevent_init();
284 goto out_free_rq_tio_cache
;
287 r
= register_blkdev(_major
, _name
);
289 goto out_uevent_exit
;
298 out_free_rq_tio_cache
:
299 kmem_cache_destroy(_rq_tio_cache
);
301 kmem_cache_destroy(_io_cache
);
306 static void local_exit(void)
308 flush_scheduled_work();
310 kmem_cache_destroy(_rq_tio_cache
);
311 kmem_cache_destroy(_io_cache
);
312 unregister_blkdev(_major
, _name
);
317 DMINFO("cleaned up");
320 static int (*_inits
[])(void) __initdata
= {
331 static void (*_exits
[])(void) = {
342 static int __init
dm_init(void)
344 const int count
= ARRAY_SIZE(_inits
);
348 for (i
= 0; i
< count
; i
++) {
363 static void __exit
dm_exit(void)
365 int i
= ARRAY_SIZE(_exits
);
371 * Should be empty by this point.
373 idr_destroy(&_minor_idr
);
377 * Block device functions
379 int dm_deleting_md(struct mapped_device
*md
)
381 return test_bit(DMF_DELETING
, &md
->flags
);
384 static int dm_blk_open(struct block_device
*bdev
, fmode_t mode
)
386 struct mapped_device
*md
;
388 spin_lock(&_minor_lock
);
390 md
= bdev
->bd_disk
->private_data
;
394 if (test_bit(DMF_FREEING
, &md
->flags
) ||
395 dm_deleting_md(md
)) {
401 atomic_inc(&md
->open_count
);
404 spin_unlock(&_minor_lock
);
406 return md
? 0 : -ENXIO
;
409 static void dm_blk_close(struct gendisk
*disk
, fmode_t mode
)
411 struct mapped_device
*md
= disk
->private_data
;
413 spin_lock(&_minor_lock
);
415 if (atomic_dec_and_test(&md
->open_count
) &&
416 (test_bit(DMF_DEFERRED_REMOVE
, &md
->flags
)))
417 schedule_work(&deferred_remove_work
);
421 spin_unlock(&_minor_lock
);
424 int dm_open_count(struct mapped_device
*md
)
426 return atomic_read(&md
->open_count
);
430 * Guarantees nothing is using the device before it's deleted.
432 int dm_lock_for_deletion(struct mapped_device
*md
, bool mark_deferred
, bool only_deferred
)
436 spin_lock(&_minor_lock
);
438 if (dm_open_count(md
)) {
441 set_bit(DMF_DEFERRED_REMOVE
, &md
->flags
);
442 } else if (only_deferred
&& !test_bit(DMF_DEFERRED_REMOVE
, &md
->flags
))
445 set_bit(DMF_DELETING
, &md
->flags
);
447 spin_unlock(&_minor_lock
);
452 int dm_cancel_deferred_remove(struct mapped_device
*md
)
456 spin_lock(&_minor_lock
);
458 if (test_bit(DMF_DELETING
, &md
->flags
))
461 clear_bit(DMF_DEFERRED_REMOVE
, &md
->flags
);
463 spin_unlock(&_minor_lock
);
468 static void do_deferred_remove(struct work_struct
*w
)
470 dm_deferred_remove();
473 sector_t
dm_get_size(struct mapped_device
*md
)
475 return get_capacity(md
->disk
);
478 struct dm_stats
*dm_get_stats(struct mapped_device
*md
)
483 static int dm_blk_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
485 struct mapped_device
*md
= bdev
->bd_disk
->private_data
;
487 return dm_get_geometry(md
, geo
);
490 static int dm_blk_ioctl(struct block_device
*bdev
, fmode_t mode
,
491 unsigned int cmd
, unsigned long arg
)
493 struct mapped_device
*md
= bdev
->bd_disk
->private_data
;
495 struct dm_table
*map
;
496 struct dm_target
*tgt
;
500 map
= dm_get_live_table(md
, &srcu_idx
);
502 if (!map
|| !dm_table_get_size(map
))
505 /* We only support devices that have a single target */
506 if (dm_table_get_num_targets(map
) != 1)
509 tgt
= dm_table_get_target(map
, 0);
511 if (dm_suspended_md(md
)) {
516 if (tgt
->type
->ioctl
)
517 r
= tgt
->type
->ioctl(tgt
, cmd
, arg
);
520 dm_put_live_table(md
, srcu_idx
);
522 if (r
== -ENOTCONN
) {
530 static struct dm_io
*alloc_io(struct mapped_device
*md
)
532 return mempool_alloc(md
->io_pool
, GFP_NOIO
);
535 static void free_io(struct mapped_device
*md
, struct dm_io
*io
)
537 mempool_free(io
, md
->io_pool
);
540 static void free_tio(struct mapped_device
*md
, struct dm_target_io
*tio
)
542 bio_put(&tio
->clone
);
545 static struct dm_rq_target_io
*alloc_rq_tio(struct mapped_device
*md
,
548 return mempool_alloc(md
->io_pool
, gfp_mask
);
551 static void free_rq_tio(struct dm_rq_target_io
*tio
)
553 mempool_free(tio
, tio
->md
->io_pool
);
556 static int md_in_flight(struct mapped_device
*md
)
558 return atomic_read(&md
->pending
[READ
]) +
559 atomic_read(&md
->pending
[WRITE
]);
562 static void start_io_acct(struct dm_io
*io
)
564 struct mapped_device
*md
= io
->md
;
565 struct bio
*bio
= io
->bio
;
567 int rw
= bio_data_dir(bio
);
569 io
->start_time
= jiffies
;
571 cpu
= part_stat_lock();
572 part_round_stats(cpu
, &dm_disk(md
)->part0
);
574 atomic_set(&dm_disk(md
)->part0
.in_flight
[rw
],
575 atomic_inc_return(&md
->pending
[rw
]));
577 if (unlikely(dm_stats_used(&md
->stats
)))
578 dm_stats_account_io(&md
->stats
, bio
->bi_rw
, bio
->bi_sector
,
579 bio_sectors(bio
), false, 0, &io
->stats_aux
);
582 static void end_io_acct(struct dm_io
*io
)
584 struct mapped_device
*md
= io
->md
;
585 struct bio
*bio
= io
->bio
;
586 unsigned long duration
= jiffies
- io
->start_time
;
588 int rw
= bio_data_dir(bio
);
590 cpu
= part_stat_lock();
591 part_round_stats(cpu
, &dm_disk(md
)->part0
);
592 part_stat_add(cpu
, &dm_disk(md
)->part0
, ticks
[rw
], duration
);
595 if (unlikely(dm_stats_used(&md
->stats
)))
596 dm_stats_account_io(&md
->stats
, bio
->bi_rw
, bio
->bi_sector
,
597 bio_sectors(bio
), true, duration
, &io
->stats_aux
);
600 * After this is decremented the bio must not be touched if it is
603 pending
= atomic_dec_return(&md
->pending
[rw
]);
604 atomic_set(&dm_disk(md
)->part0
.in_flight
[rw
], pending
);
605 pending
+= atomic_read(&md
->pending
[rw
^0x1]);
607 /* nudge anyone waiting on suspend queue */
613 * Add the bio to the list of deferred io.
615 static void queue_io(struct mapped_device
*md
, struct bio
*bio
)
619 spin_lock_irqsave(&md
->deferred_lock
, flags
);
620 bio_list_add(&md
->deferred
, bio
);
621 spin_unlock_irqrestore(&md
->deferred_lock
, flags
);
622 queue_work(md
->wq
, &md
->work
);
626 * Everyone (including functions in this file), should use this
627 * function to access the md->map field, and make sure they call
628 * dm_put_live_table() when finished.
630 struct dm_table
*dm_get_live_table(struct mapped_device
*md
, int *srcu_idx
) __acquires(md
->io_barrier
)
632 *srcu_idx
= srcu_read_lock(&md
->io_barrier
);
634 return srcu_dereference(md
->map
, &md
->io_barrier
);
637 void dm_put_live_table(struct mapped_device
*md
, int srcu_idx
) __releases(md
->io_barrier
)
639 srcu_read_unlock(&md
->io_barrier
, srcu_idx
);
642 void dm_sync_table(struct mapped_device
*md
)
644 synchronize_srcu(&md
->io_barrier
);
645 synchronize_rcu_expedited();
649 * A fast alternative to dm_get_live_table/dm_put_live_table.
650 * The caller must not block between these two functions.
652 static struct dm_table
*dm_get_live_table_fast(struct mapped_device
*md
) __acquires(RCU
)
655 return rcu_dereference(md
->map
);
658 static void dm_put_live_table_fast(struct mapped_device
*md
) __releases(RCU
)
664 * Get the geometry associated with a dm device
666 int dm_get_geometry(struct mapped_device
*md
, struct hd_geometry
*geo
)
674 * Set the geometry of a device.
676 int dm_set_geometry(struct mapped_device
*md
, struct hd_geometry
*geo
)
678 sector_t sz
= (sector_t
)geo
->cylinders
* geo
->heads
* geo
->sectors
;
680 if (geo
->start
> sz
) {
681 DMWARN("Start sector is beyond the geometry limits.");
690 /*-----------------------------------------------------------------
692 * A more elegant soln is in the works that uses the queue
693 * merge fn, unfortunately there are a couple of changes to
694 * the block layer that I want to make for this. So in the
695 * interests of getting something for people to use I give
696 * you this clearly demarcated crap.
697 *---------------------------------------------------------------*/
699 static int __noflush_suspending(struct mapped_device
*md
)
701 return test_bit(DMF_NOFLUSH_SUSPENDING
, &md
->flags
);
705 * Decrements the number of outstanding ios that a bio has been
706 * cloned into, completing the original io if necc.
708 static void dec_pending(struct dm_io
*io
, int error
)
713 struct mapped_device
*md
= io
->md
;
715 /* Push-back supersedes any I/O errors */
716 if (unlikely(error
)) {
717 spin_lock_irqsave(&io
->endio_lock
, flags
);
718 if (!(io
->error
> 0 && __noflush_suspending(md
)))
720 spin_unlock_irqrestore(&io
->endio_lock
, flags
);
723 if (atomic_dec_and_test(&io
->io_count
)) {
724 if (io
->error
== DM_ENDIO_REQUEUE
) {
726 * Target requested pushing back the I/O.
728 spin_lock_irqsave(&md
->deferred_lock
, flags
);
729 if (__noflush_suspending(md
))
730 bio_list_add_head(&md
->deferred
, io
->bio
);
732 /* noflush suspend was interrupted. */
734 spin_unlock_irqrestore(&md
->deferred_lock
, flags
);
737 io_error
= io
->error
;
742 if (io_error
== DM_ENDIO_REQUEUE
)
745 if ((bio
->bi_rw
& REQ_FLUSH
) && bio
->bi_size
) {
747 * Preflush done for flush with data, reissue
750 bio
->bi_rw
&= ~REQ_FLUSH
;
753 /* done with normal IO or empty flush */
754 trace_block_bio_complete(md
->queue
, bio
, io_error
);
755 bio_endio(bio
, io_error
);
760 static void clone_endio(struct bio
*bio
, int error
)
763 struct dm_target_io
*tio
= bio
->bi_private
;
764 struct dm_io
*io
= tio
->io
;
765 struct mapped_device
*md
= tio
->io
->md
;
766 dm_endio_fn endio
= tio
->ti
->type
->end_io
;
768 if (!bio_flagged(bio
, BIO_UPTODATE
) && !error
)
772 r
= endio(tio
->ti
, bio
, error
);
773 if (r
< 0 || r
== DM_ENDIO_REQUEUE
)
775 * error and requeue request are handled
779 else if (r
== DM_ENDIO_INCOMPLETE
)
780 /* The target will handle the io */
783 DMWARN("unimplemented target endio return value: %d", r
);
789 dec_pending(io
, error
);
793 * Partial completion handling for request-based dm
795 static void end_clone_bio(struct bio
*clone
, int error
)
797 struct dm_rq_clone_bio_info
*info
= clone
->bi_private
;
798 struct dm_rq_target_io
*tio
= info
->tio
;
799 struct bio
*bio
= info
->orig
;
800 unsigned int nr_bytes
= info
->orig
->bi_size
;
806 * An error has already been detected on the request.
807 * Once error occurred, just let clone->end_io() handle
813 * Don't notice the error to the upper layer yet.
814 * The error handling decision is made by the target driver,
815 * when the request is completed.
822 * I/O for the bio successfully completed.
823 * Notice the data completion to the upper layer.
827 * bios are processed from the head of the list.
828 * So the completing bio should always be rq->bio.
829 * If it's not, something wrong is happening.
831 if (tio
->orig
->bio
!= bio
)
832 DMERR("bio completion is going in the middle of the request");
835 * Update the original request.
836 * Do not use blk_end_request() here, because it may complete
837 * the original request before the clone, and break the ordering.
839 blk_update_request(tio
->orig
, 0, nr_bytes
);
843 * Don't touch any member of the md after calling this function because
844 * the md may be freed in dm_put() at the end of this function.
845 * Or do dm_get() before calling this function and dm_put() later.
847 static void rq_completed(struct mapped_device
*md
, int rw
, int run_queue
)
849 atomic_dec(&md
->pending
[rw
]);
851 /* nudge anyone waiting on suspend queue */
852 if (!md_in_flight(md
))
856 * Run this off this callpath, as drivers could invoke end_io while
857 * inside their request_fn (and holding the queue lock). Calling
858 * back into ->request_fn() could deadlock attempting to grab the
862 blk_run_queue_async(md
->queue
);
865 * dm_put() must be at the end of this function. See the comment above
870 static void free_rq_clone(struct request
*clone
)
872 struct dm_rq_target_io
*tio
= clone
->end_io_data
;
874 blk_rq_unprep_clone(clone
);
879 * Complete the clone and the original request.
880 * Must be called without queue lock.
882 static void dm_end_request(struct request
*clone
, int error
)
884 int rw
= rq_data_dir(clone
);
885 struct dm_rq_target_io
*tio
= clone
->end_io_data
;
886 struct mapped_device
*md
= tio
->md
;
887 struct request
*rq
= tio
->orig
;
889 if (rq
->cmd_type
== REQ_TYPE_BLOCK_PC
) {
890 rq
->errors
= clone
->errors
;
891 rq
->resid_len
= clone
->resid_len
;
895 * We are using the sense buffer of the original
897 * So setting the length of the sense data is enough.
899 rq
->sense_len
= clone
->sense_len
;
902 free_rq_clone(clone
);
903 blk_end_request_all(rq
, error
);
904 rq_completed(md
, rw
, true);
907 static void dm_unprep_request(struct request
*rq
)
909 struct request
*clone
= rq
->special
;
912 rq
->cmd_flags
&= ~REQ_DONTPREP
;
914 free_rq_clone(clone
);
918 * Requeue the original request of a clone.
920 void dm_requeue_unmapped_request(struct request
*clone
)
922 int rw
= rq_data_dir(clone
);
923 struct dm_rq_target_io
*tio
= clone
->end_io_data
;
924 struct mapped_device
*md
= tio
->md
;
925 struct request
*rq
= tio
->orig
;
926 struct request_queue
*q
= rq
->q
;
929 dm_unprep_request(rq
);
931 spin_lock_irqsave(q
->queue_lock
, flags
);
932 blk_requeue_request(q
, rq
);
933 spin_unlock_irqrestore(q
->queue_lock
, flags
);
935 rq_completed(md
, rw
, 0);
937 EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request
);
939 static void __stop_queue(struct request_queue
*q
)
944 static void stop_queue(struct request_queue
*q
)
948 spin_lock_irqsave(q
->queue_lock
, flags
);
950 spin_unlock_irqrestore(q
->queue_lock
, flags
);
953 static void __start_queue(struct request_queue
*q
)
955 if (blk_queue_stopped(q
))
959 static void start_queue(struct request_queue
*q
)
963 spin_lock_irqsave(q
->queue_lock
, flags
);
965 spin_unlock_irqrestore(q
->queue_lock
, flags
);
968 static void dm_done(struct request
*clone
, int error
, bool mapped
)
971 struct dm_rq_target_io
*tio
= clone
->end_io_data
;
972 dm_request_endio_fn rq_end_io
= NULL
;
975 rq_end_io
= tio
->ti
->type
->rq_end_io
;
977 if (mapped
&& rq_end_io
)
978 r
= rq_end_io(tio
->ti
, clone
, error
, &tio
->info
);
982 /* The target wants to complete the I/O */
983 dm_end_request(clone
, r
);
984 else if (r
== DM_ENDIO_INCOMPLETE
)
985 /* The target will handle the I/O */
987 else if (r
== DM_ENDIO_REQUEUE
)
988 /* The target wants to requeue the I/O */
989 dm_requeue_unmapped_request(clone
);
991 DMWARN("unimplemented target endio return value: %d", r
);
997 * Request completion handler for request-based dm
999 static void dm_softirq_done(struct request
*rq
)
1002 struct request
*clone
= rq
->completion_data
;
1003 struct dm_rq_target_io
*tio
= clone
->end_io_data
;
1005 if (rq
->cmd_flags
& REQ_FAILED
)
1008 dm_done(clone
, tio
->error
, mapped
);
1012 * Complete the clone and the original request with the error status
1013 * through softirq context.
1015 static void dm_complete_request(struct request
*clone
, int error
)
1017 struct dm_rq_target_io
*tio
= clone
->end_io_data
;
1018 struct request
*rq
= tio
->orig
;
1021 rq
->completion_data
= clone
;
1022 blk_complete_request(rq
);
1026 * Complete the not-mapped clone and the original request with the error status
1027 * through softirq context.
1028 * Target's rq_end_io() function isn't called.
1029 * This may be used when the target's map_rq() function fails.
1031 void dm_kill_unmapped_request(struct request
*clone
, int error
)
1033 struct dm_rq_target_io
*tio
= clone
->end_io_data
;
1034 struct request
*rq
= tio
->orig
;
1036 rq
->cmd_flags
|= REQ_FAILED
;
1037 dm_complete_request(clone
, error
);
1039 EXPORT_SYMBOL_GPL(dm_kill_unmapped_request
);
1042 * Called with the queue lock held
1044 static void end_clone_request(struct request
*clone
, int error
)
1047 * For just cleaning up the information of the queue in which
1048 * the clone was dispatched.
1049 * The clone is *NOT* freed actually here because it is alloced from
1050 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
1052 __blk_put_request(clone
->q
, clone
);
1055 * Actual request completion is done in a softirq context which doesn't
1056 * hold the queue lock. Otherwise, deadlock could occur because:
1057 * - another request may be submitted by the upper level driver
1058 * of the stacking during the completion
1059 * - the submission which requires queue lock may be done
1060 * against this queue
1062 dm_complete_request(clone
, error
);
1066 * Return maximum size of I/O possible at the supplied sector up to the current
1069 static sector_t
max_io_len_target_boundary(sector_t sector
, struct dm_target
*ti
)
1071 sector_t target_offset
= dm_target_offset(ti
, sector
);
1073 return ti
->len
- target_offset
;
1076 static sector_t
max_io_len(sector_t sector
, struct dm_target
*ti
)
1078 sector_t len
= max_io_len_target_boundary(sector
, ti
);
1079 sector_t offset
, max_len
;
1082 * Does the target need to split even further?
1084 if (ti
->max_io_len
) {
1085 offset
= dm_target_offset(ti
, sector
);
1086 if (unlikely(ti
->max_io_len
& (ti
->max_io_len
- 1)))
1087 max_len
= sector_div(offset
, ti
->max_io_len
);
1089 max_len
= offset
& (ti
->max_io_len
- 1);
1090 max_len
= ti
->max_io_len
- max_len
;
1099 int dm_set_target_max_io_len(struct dm_target
*ti
, sector_t len
)
1101 if (len
> UINT_MAX
) {
1102 DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1103 (unsigned long long)len
, UINT_MAX
);
1104 ti
->error
= "Maximum size of target IO is too large";
1108 ti
->max_io_len
= (uint32_t) len
;
1112 EXPORT_SYMBOL_GPL(dm_set_target_max_io_len
);
1114 static void __map_bio(struct dm_target_io
*tio
)
1118 struct mapped_device
*md
;
1119 struct bio
*clone
= &tio
->clone
;
1120 struct dm_target
*ti
= tio
->ti
;
1122 clone
->bi_end_io
= clone_endio
;
1123 clone
->bi_private
= tio
;
1126 * Map the clone. If r == 0 we don't need to do
1127 * anything, the target has assumed ownership of
1130 atomic_inc(&tio
->io
->io_count
);
1131 sector
= clone
->bi_sector
;
1132 r
= ti
->type
->map(ti
, clone
);
1133 if (r
== DM_MAPIO_REMAPPED
) {
1134 /* the bio has been remapped so dispatch it */
1136 trace_block_bio_remap(bdev_get_queue(clone
->bi_bdev
), clone
,
1137 tio
->io
->bio
->bi_bdev
->bd_dev
, sector
);
1139 generic_make_request(clone
);
1140 } else if (r
< 0 || r
== DM_MAPIO_REQUEUE
) {
1141 /* error the io and bail out, or requeue it if needed */
1143 dec_pending(tio
->io
, r
);
1146 DMWARN("unimplemented target map return value: %d", r
);
1152 struct mapped_device
*md
;
1153 struct dm_table
*map
;
1157 sector_t sector_count
;
1161 static void bio_setup_sector(struct bio
*bio
, sector_t sector
, sector_t len
)
1163 bio
->bi_sector
= sector
;
1164 bio
->bi_size
= to_bytes(len
);
1167 static void bio_setup_bv(struct bio
*bio
, unsigned short idx
, unsigned short bv_count
)
1170 bio
->bi_vcnt
= idx
+ bv_count
;
1171 bio
->bi_flags
&= ~(1 << BIO_SEG_VALID
);
1174 static void clone_bio_integrity(struct bio
*bio
, struct bio
*clone
,
1175 unsigned short idx
, unsigned len
, unsigned offset
,
1178 if (!bio_integrity(bio
))
1181 bio_integrity_clone(clone
, bio
, GFP_NOIO
);
1184 bio_integrity_trim(clone
, bio_sector_offset(bio
, idx
, offset
), len
);
1188 * Creates a little bio that just does part of a bvec.
1190 static void clone_split_bio(struct dm_target_io
*tio
, struct bio
*bio
,
1191 sector_t sector
, unsigned short idx
,
1192 unsigned offset
, unsigned len
)
1194 struct bio
*clone
= &tio
->clone
;
1195 struct bio_vec
*bv
= bio
->bi_io_vec
+ idx
;
1197 *clone
->bi_io_vec
= *bv
;
1199 bio_setup_sector(clone
, sector
, len
);
1201 clone
->bi_bdev
= bio
->bi_bdev
;
1202 clone
->bi_rw
= bio
->bi_rw
;
1204 clone
->bi_io_vec
->bv_offset
= offset
;
1205 clone
->bi_io_vec
->bv_len
= clone
->bi_size
;
1206 clone
->bi_flags
|= 1 << BIO_CLONED
;
1208 clone_bio_integrity(bio
, clone
, idx
, len
, offset
, 1);
1212 * Creates a bio that consists of range of complete bvecs.
1214 static void clone_bio(struct dm_target_io
*tio
, struct bio
*bio
,
1215 sector_t sector
, unsigned short idx
,
1216 unsigned short bv_count
, unsigned len
)
1218 struct bio
*clone
= &tio
->clone
;
1221 __bio_clone(clone
, bio
);
1222 bio_setup_sector(clone
, sector
, len
);
1223 bio_setup_bv(clone
, idx
, bv_count
);
1225 if (idx
!= bio
->bi_idx
|| clone
->bi_size
< bio
->bi_size
)
1227 clone_bio_integrity(bio
, clone
, idx
, len
, 0, trim
);
1230 static struct dm_target_io
*alloc_tio(struct clone_info
*ci
,
1231 struct dm_target
*ti
, int nr_iovecs
,
1232 unsigned target_bio_nr
)
1234 struct dm_target_io
*tio
;
1237 clone
= bio_alloc_bioset(GFP_NOIO
, nr_iovecs
, ci
->md
->bs
);
1238 tio
= container_of(clone
, struct dm_target_io
, clone
);
1242 memset(&tio
->info
, 0, sizeof(tio
->info
));
1243 tio
->target_bio_nr
= target_bio_nr
;
1248 static void __clone_and_map_simple_bio(struct clone_info
*ci
,
1249 struct dm_target
*ti
,
1250 unsigned target_bio_nr
, sector_t len
)
1252 struct dm_target_io
*tio
= alloc_tio(ci
, ti
, ci
->bio
->bi_max_vecs
, target_bio_nr
);
1253 struct bio
*clone
= &tio
->clone
;
1256 * Discard requests require the bio's inline iovecs be initialized.
1257 * ci->bio->bi_max_vecs is BIO_INLINE_VECS anyway, for both flush
1258 * and discard, so no need for concern about wasted bvec allocations.
1260 __bio_clone(clone
, ci
->bio
);
1262 bio_setup_sector(clone
, ci
->sector
, len
);
1267 static void __send_duplicate_bios(struct clone_info
*ci
, struct dm_target
*ti
,
1268 unsigned num_bios
, sector_t len
)
1270 unsigned target_bio_nr
;
1272 for (target_bio_nr
= 0; target_bio_nr
< num_bios
; target_bio_nr
++)
1273 __clone_and_map_simple_bio(ci
, ti
, target_bio_nr
, len
);
1276 static int __send_empty_flush(struct clone_info
*ci
)
1278 unsigned target_nr
= 0;
1279 struct dm_target
*ti
;
1281 BUG_ON(bio_has_data(ci
->bio
));
1282 while ((ti
= dm_table_get_target(ci
->map
, target_nr
++)))
1283 __send_duplicate_bios(ci
, ti
, ti
->num_flush_bios
, 0);
1288 static void __clone_and_map_data_bio(struct clone_info
*ci
, struct dm_target
*ti
,
1289 sector_t sector
, int nr_iovecs
,
1290 unsigned short idx
, unsigned short bv_count
,
1291 unsigned offset
, unsigned len
,
1292 unsigned split_bvec
)
1294 struct bio
*bio
= ci
->bio
;
1295 struct dm_target_io
*tio
;
1296 unsigned target_bio_nr
;
1297 unsigned num_target_bios
= 1;
1300 * Does the target want to receive duplicate copies of the bio?
1302 if (bio_data_dir(bio
) == WRITE
&& ti
->num_write_bios
)
1303 num_target_bios
= ti
->num_write_bios(ti
, bio
);
1305 for (target_bio_nr
= 0; target_bio_nr
< num_target_bios
; target_bio_nr
++) {
1306 tio
= alloc_tio(ci
, ti
, nr_iovecs
, target_bio_nr
);
1308 clone_split_bio(tio
, bio
, sector
, idx
, offset
, len
);
1310 clone_bio(tio
, bio
, sector
, idx
, bv_count
, len
);
1315 typedef unsigned (*get_num_bios_fn
)(struct dm_target
*ti
);
1317 static unsigned get_num_discard_bios(struct dm_target
*ti
)
1319 return ti
->num_discard_bios
;
1322 static unsigned get_num_write_same_bios(struct dm_target
*ti
)
1324 return ti
->num_write_same_bios
;
1327 typedef bool (*is_split_required_fn
)(struct dm_target
*ti
);
1329 static bool is_split_required_for_discard(struct dm_target
*ti
)
1331 return ti
->split_discard_bios
;
1334 static int __send_changing_extent_only(struct clone_info
*ci
,
1335 get_num_bios_fn get_num_bios
,
1336 is_split_required_fn is_split_required
)
1338 struct dm_target
*ti
;
1343 ti
= dm_table_find_target(ci
->map
, ci
->sector
);
1344 if (!dm_target_is_valid(ti
))
1348 * Even though the device advertised support for this type of
1349 * request, that does not mean every target supports it, and
1350 * reconfiguration might also have changed that since the
1351 * check was performed.
1353 num_bios
= get_num_bios
? get_num_bios(ti
) : 0;
1357 if (is_split_required
&& !is_split_required(ti
))
1358 len
= min(ci
->sector_count
, max_io_len_target_boundary(ci
->sector
, ti
));
1360 len
= min(ci
->sector_count
, max_io_len(ci
->sector
, ti
));
1362 __send_duplicate_bios(ci
, ti
, num_bios
, len
);
1365 } while (ci
->sector_count
-= len
);
1370 static int __send_discard(struct clone_info
*ci
)
1372 return __send_changing_extent_only(ci
, get_num_discard_bios
,
1373 is_split_required_for_discard
);
1376 static int __send_write_same(struct clone_info
*ci
)
1378 return __send_changing_extent_only(ci
, get_num_write_same_bios
, NULL
);
1382 * Find maximum number of sectors / bvecs we can process with a single bio.
1384 static sector_t
__len_within_target(struct clone_info
*ci
, sector_t max
, int *idx
)
1386 struct bio
*bio
= ci
->bio
;
1387 sector_t bv_len
, total_len
= 0;
1389 for (*idx
= ci
->idx
; max
&& (*idx
< bio
->bi_vcnt
); (*idx
)++) {
1390 bv_len
= to_sector(bio
->bi_io_vec
[*idx
].bv_len
);
1396 total_len
+= bv_len
;
1402 static int __split_bvec_across_targets(struct clone_info
*ci
,
1403 struct dm_target
*ti
, sector_t max
)
1405 struct bio
*bio
= ci
->bio
;
1406 struct bio_vec
*bv
= bio
->bi_io_vec
+ ci
->idx
;
1407 sector_t remaining
= to_sector(bv
->bv_len
);
1408 unsigned offset
= 0;
1413 ti
= dm_table_find_target(ci
->map
, ci
->sector
);
1414 if (!dm_target_is_valid(ti
))
1417 max
= max_io_len(ci
->sector
, ti
);
1420 len
= min(remaining
, max
);
1422 __clone_and_map_data_bio(ci
, ti
, ci
->sector
, 1, ci
->idx
, 0,
1423 bv
->bv_offset
+ offset
, len
, 1);
1426 ci
->sector_count
-= len
;
1427 offset
+= to_bytes(len
);
1428 } while (remaining
-= len
);
1436 * Select the correct strategy for processing a non-flush bio.
1438 static int __split_and_process_non_flush(struct clone_info
*ci
)
1440 struct bio
*bio
= ci
->bio
;
1441 struct dm_target
*ti
;
1445 if (unlikely(bio
->bi_rw
& REQ_DISCARD
))
1446 return __send_discard(ci
);
1447 else if (unlikely(bio
->bi_rw
& REQ_WRITE_SAME
))
1448 return __send_write_same(ci
);
1450 ti
= dm_table_find_target(ci
->map
, ci
->sector
);
1451 if (!dm_target_is_valid(ti
))
1454 max
= max_io_len(ci
->sector
, ti
);
1457 * Optimise for the simple case where we can do all of
1458 * the remaining io with a single clone.
1460 if (ci
->sector_count
<= max
) {
1461 __clone_and_map_data_bio(ci
, ti
, ci
->sector
, bio
->bi_max_vecs
,
1462 ci
->idx
, bio
->bi_vcnt
- ci
->idx
, 0,
1463 ci
->sector_count
, 0);
1464 ci
->sector_count
= 0;
1469 * There are some bvecs that don't span targets.
1470 * Do as many of these as possible.
1472 if (to_sector(bio
->bi_io_vec
[ci
->idx
].bv_len
) <= max
) {
1473 len
= __len_within_target(ci
, max
, &idx
);
1475 __clone_and_map_data_bio(ci
, ti
, ci
->sector
, bio
->bi_max_vecs
,
1476 ci
->idx
, idx
- ci
->idx
, 0, len
, 0);
1479 ci
->sector_count
-= len
;
1486 * Handle a bvec that must be split between two or more targets.
1488 return __split_bvec_across_targets(ci
, ti
, max
);
1492 * Entry point to split a bio into clones and submit them to the targets.
1494 static void __split_and_process_bio(struct mapped_device
*md
,
1495 struct dm_table
*map
, struct bio
*bio
)
1497 struct clone_info ci
;
1500 if (unlikely(!map
)) {
1507 ci
.io
= alloc_io(md
);
1509 atomic_set(&ci
.io
->io_count
, 1);
1512 spin_lock_init(&ci
.io
->endio_lock
);
1513 ci
.sector
= bio
->bi_sector
;
1514 ci
.idx
= bio
->bi_idx
;
1516 start_io_acct(ci
.io
);
1518 if (bio
->bi_rw
& REQ_FLUSH
) {
1519 ci
.bio
= &ci
.md
->flush_bio
;
1520 ci
.sector_count
= 0;
1521 error
= __send_empty_flush(&ci
);
1522 /* dec_pending submits any data associated with flush */
1525 ci
.sector_count
= bio_sectors(bio
);
1526 while (ci
.sector_count
&& !error
)
1527 error
= __split_and_process_non_flush(&ci
);
1530 /* drop the extra reference count */
1531 dec_pending(ci
.io
, error
);
1533 /*-----------------------------------------------------------------
1535 *---------------------------------------------------------------*/
1537 static int dm_merge_bvec(struct request_queue
*q
,
1538 struct bvec_merge_data
*bvm
,
1539 struct bio_vec
*biovec
)
1541 struct mapped_device
*md
= q
->queuedata
;
1542 struct dm_table
*map
= dm_get_live_table_fast(md
);
1543 struct dm_target
*ti
;
1544 sector_t max_sectors
;
1550 ti
= dm_table_find_target(map
, bvm
->bi_sector
);
1551 if (!dm_target_is_valid(ti
))
1555 * Find maximum amount of I/O that won't need splitting
1557 max_sectors
= min(max_io_len(bvm
->bi_sector
, ti
),
1558 (sector_t
) BIO_MAX_SECTORS
);
1559 max_size
= (max_sectors
<< SECTOR_SHIFT
) - bvm
->bi_size
;
1564 * merge_bvec_fn() returns number of bytes
1565 * it can accept at this offset
1566 * max is precomputed maximal io size
1568 if (max_size
&& ti
->type
->merge
)
1569 max_size
= ti
->type
->merge(ti
, bvm
, biovec
, max_size
);
1571 * If the target doesn't support merge method and some of the devices
1572 * provided their merge_bvec method (we know this by looking at
1573 * queue_max_hw_sectors), then we can't allow bios with multiple vector
1574 * entries. So always set max_size to 0, and the code below allows
1577 else if (queue_max_hw_sectors(q
) <= PAGE_SIZE
>> 9)
1582 dm_put_live_table_fast(md
);
1584 * Always allow an entire first page
1586 if (max_size
<= biovec
->bv_len
&& !(bvm
->bi_size
>> SECTOR_SHIFT
))
1587 max_size
= biovec
->bv_len
;
1593 * The request function that just remaps the bio built up by
1596 static void _dm_request(struct request_queue
*q
, struct bio
*bio
)
1598 int rw
= bio_data_dir(bio
);
1599 struct mapped_device
*md
= q
->queuedata
;
1602 struct dm_table
*map
;
1604 map
= dm_get_live_table(md
, &srcu_idx
);
1606 cpu
= part_stat_lock();
1607 part_stat_inc(cpu
, &dm_disk(md
)->part0
, ios
[rw
]);
1608 part_stat_add(cpu
, &dm_disk(md
)->part0
, sectors
[rw
], bio_sectors(bio
));
1611 /* if we're suspended, we have to queue this io for later */
1612 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND
, &md
->flags
))) {
1613 dm_put_live_table(md
, srcu_idx
);
1615 if (bio_rw(bio
) != READA
)
1622 __split_and_process_bio(md
, map
, bio
);
1623 dm_put_live_table(md
, srcu_idx
);
1627 int dm_request_based(struct mapped_device
*md
)
1629 return blk_queue_stackable(md
->queue
);
1632 static void dm_request(struct request_queue
*q
, struct bio
*bio
)
1634 struct mapped_device
*md
= q
->queuedata
;
1636 if (dm_request_based(md
))
1637 blk_queue_bio(q
, bio
);
1639 _dm_request(q
, bio
);
1642 void dm_dispatch_request(struct request
*rq
)
1646 if (blk_queue_io_stat(rq
->q
))
1647 rq
->cmd_flags
|= REQ_IO_STAT
;
1649 rq
->start_time
= jiffies
;
1650 r
= blk_insert_cloned_request(rq
->q
, rq
);
1652 dm_complete_request(rq
, r
);
1654 EXPORT_SYMBOL_GPL(dm_dispatch_request
);
1656 static int dm_rq_bio_constructor(struct bio
*bio
, struct bio
*bio_orig
,
1659 struct dm_rq_target_io
*tio
= data
;
1660 struct dm_rq_clone_bio_info
*info
=
1661 container_of(bio
, struct dm_rq_clone_bio_info
, clone
);
1663 info
->orig
= bio_orig
;
1665 bio
->bi_end_io
= end_clone_bio
;
1666 bio
->bi_private
= info
;
1671 static int setup_clone(struct request
*clone
, struct request
*rq
,
1672 struct dm_rq_target_io
*tio
)
1676 r
= blk_rq_prep_clone(clone
, rq
, tio
->md
->bs
, GFP_ATOMIC
,
1677 dm_rq_bio_constructor
, tio
);
1681 clone
->cmd
= rq
->cmd
;
1682 clone
->cmd_len
= rq
->cmd_len
;
1683 clone
->sense
= rq
->sense
;
1684 clone
->buffer
= rq
->buffer
;
1685 clone
->end_io
= end_clone_request
;
1686 clone
->end_io_data
= tio
;
1691 static struct request
*clone_rq(struct request
*rq
, struct mapped_device
*md
,
1694 struct request
*clone
;
1695 struct dm_rq_target_io
*tio
;
1697 tio
= alloc_rq_tio(md
, gfp_mask
);
1705 memset(&tio
->info
, 0, sizeof(tio
->info
));
1707 clone
= &tio
->clone
;
1708 if (setup_clone(clone
, rq
, tio
)) {
1718 * Called with the queue lock held.
1720 static int dm_prep_fn(struct request_queue
*q
, struct request
*rq
)
1722 struct mapped_device
*md
= q
->queuedata
;
1723 struct request
*clone
;
1725 if (unlikely(rq
->special
)) {
1726 DMWARN("Already has something in rq->special.");
1727 return BLKPREP_KILL
;
1730 clone
= clone_rq(rq
, md
, GFP_ATOMIC
);
1732 return BLKPREP_DEFER
;
1734 rq
->special
= clone
;
1735 rq
->cmd_flags
|= REQ_DONTPREP
;
1742 * 0 : the request has been processed (not requeued)
1743 * !0 : the request has been requeued
1745 static int map_request(struct dm_target
*ti
, struct request
*clone
,
1746 struct mapped_device
*md
)
1748 int r
, requeued
= 0;
1749 struct dm_rq_target_io
*tio
= clone
->end_io_data
;
1752 r
= ti
->type
->map_rq(ti
, clone
, &tio
->info
);
1754 case DM_MAPIO_SUBMITTED
:
1755 /* The target has taken the I/O to submit by itself later */
1757 case DM_MAPIO_REMAPPED
:
1758 /* The target has remapped the I/O so dispatch it */
1759 trace_block_rq_remap(clone
->q
, clone
, disk_devt(dm_disk(md
)),
1760 blk_rq_pos(tio
->orig
));
1761 dm_dispatch_request(clone
);
1763 case DM_MAPIO_REQUEUE
:
1764 /* The target wants to requeue the I/O */
1765 dm_requeue_unmapped_request(clone
);
1770 DMWARN("unimplemented target map return value: %d", r
);
1774 /* The target wants to complete the I/O */
1775 dm_kill_unmapped_request(clone
, r
);
1782 static struct request
*dm_start_request(struct mapped_device
*md
, struct request
*orig
)
1784 struct request
*clone
;
1786 blk_start_request(orig
);
1787 clone
= orig
->special
;
1788 atomic_inc(&md
->pending
[rq_data_dir(clone
)]);
1791 * Hold the md reference here for the in-flight I/O.
1792 * We can't rely on the reference count by device opener,
1793 * because the device may be closed during the request completion
1794 * when all bios are completed.
1795 * See the comment in rq_completed() too.
1803 * q->request_fn for request-based dm.
1804 * Called with the queue lock held.
1806 static void dm_request_fn(struct request_queue
*q
)
1808 struct mapped_device
*md
= q
->queuedata
;
1810 struct dm_table
*map
= dm_get_live_table(md
, &srcu_idx
);
1811 struct dm_target
*ti
;
1812 struct request
*rq
, *clone
;
1816 * For suspend, check blk_queue_stopped() and increment
1817 * ->pending within a single queue_lock not to increment the
1818 * number of in-flight I/Os after the queue is stopped in
1821 while (!blk_queue_stopped(q
)) {
1822 rq
= blk_peek_request(q
);
1826 /* always use block 0 to find the target for flushes for now */
1828 if (!(rq
->cmd_flags
& REQ_FLUSH
))
1829 pos
= blk_rq_pos(rq
);
1831 ti
= dm_table_find_target(map
, pos
);
1832 if (!dm_target_is_valid(ti
)) {
1834 * Must perform setup, that dm_done() requires,
1835 * before calling dm_kill_unmapped_request
1837 DMERR_LIMIT("request attempted access beyond the end of device");
1838 clone
= dm_start_request(md
, rq
);
1839 dm_kill_unmapped_request(clone
, -EIO
);
1843 if (ti
->type
->busy
&& ti
->type
->busy(ti
))
1846 clone
= dm_start_request(md
, rq
);
1848 spin_unlock(q
->queue_lock
);
1849 if (map_request(ti
, clone
, md
))
1852 BUG_ON(!irqs_disabled());
1853 spin_lock(q
->queue_lock
);
1859 BUG_ON(!irqs_disabled());
1860 spin_lock(q
->queue_lock
);
1863 blk_delay_queue(q
, HZ
/ 10);
1865 dm_put_live_table(md
, srcu_idx
);
1868 int dm_underlying_device_busy(struct request_queue
*q
)
1870 return blk_lld_busy(q
);
1872 EXPORT_SYMBOL_GPL(dm_underlying_device_busy
);
1874 static int dm_lld_busy(struct request_queue
*q
)
1877 struct mapped_device
*md
= q
->queuedata
;
1878 struct dm_table
*map
= dm_get_live_table_fast(md
);
1880 if (!map
|| test_bit(DMF_BLOCK_IO_FOR_SUSPEND
, &md
->flags
))
1883 r
= dm_table_any_busy_target(map
);
1885 dm_put_live_table_fast(md
);
1890 static int dm_any_congested(void *congested_data
, int bdi_bits
)
1893 struct mapped_device
*md
= congested_data
;
1894 struct dm_table
*map
;
1896 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND
, &md
->flags
)) {
1897 map
= dm_get_live_table_fast(md
);
1900 * Request-based dm cares about only own queue for
1901 * the query about congestion status of request_queue
1903 if (dm_request_based(md
))
1904 r
= md
->queue
->backing_dev_info
.state
&
1907 r
= dm_table_any_congested(map
, bdi_bits
);
1909 dm_put_live_table_fast(md
);
1915 /*-----------------------------------------------------------------
1916 * An IDR is used to keep track of allocated minor numbers.
1917 *---------------------------------------------------------------*/
1918 static void free_minor(int minor
)
1920 spin_lock(&_minor_lock
);
1921 idr_remove(&_minor_idr
, minor
);
1922 spin_unlock(&_minor_lock
);
1926 * See if the device with a specific minor # is free.
1928 static int specific_minor(int minor
)
1932 if (minor
>= (1 << MINORBITS
))
1935 idr_preload(GFP_KERNEL
);
1936 spin_lock(&_minor_lock
);
1938 r
= idr_alloc(&_minor_idr
, MINOR_ALLOCED
, minor
, minor
+ 1, GFP_NOWAIT
);
1940 spin_unlock(&_minor_lock
);
1943 return r
== -ENOSPC
? -EBUSY
: r
;
1947 static int next_free_minor(int *minor
)
1951 idr_preload(GFP_KERNEL
);
1952 spin_lock(&_minor_lock
);
1954 r
= idr_alloc(&_minor_idr
, MINOR_ALLOCED
, 0, 1 << MINORBITS
, GFP_NOWAIT
);
1956 spin_unlock(&_minor_lock
);
1964 static const struct block_device_operations dm_blk_dops
;
1966 static void dm_wq_work(struct work_struct
*work
);
1968 static void dm_init_md_queue(struct mapped_device
*md
)
1971 * Request-based dm devices cannot be stacked on top of bio-based dm
1972 * devices. The type of this dm device has not been decided yet.
1973 * The type is decided at the first table loading time.
1974 * To prevent problematic device stacking, clear the queue flag
1975 * for request stacking support until then.
1977 * This queue is new, so no concurrency on the queue_flags.
1979 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE
, md
->queue
);
1981 md
->queue
->queuedata
= md
;
1982 md
->queue
->backing_dev_info
.congested_fn
= dm_any_congested
;
1983 md
->queue
->backing_dev_info
.congested_data
= md
;
1984 blk_queue_make_request(md
->queue
, dm_request
);
1985 blk_queue_bounce_limit(md
->queue
, BLK_BOUNCE_ANY
);
1986 blk_queue_merge_bvec(md
->queue
, dm_merge_bvec
);
1990 * Allocate and initialise a blank device with a given minor.
1992 static struct mapped_device
*alloc_dev(int minor
)
1995 struct mapped_device
*md
= kzalloc(sizeof(*md
), GFP_KERNEL
);
1999 DMWARN("unable to allocate device, out of memory.");
2003 if (!try_module_get(THIS_MODULE
))
2004 goto bad_module_get
;
2006 /* get a minor number for the dev */
2007 if (minor
== DM_ANY_MINOR
)
2008 r
= next_free_minor(&minor
);
2010 r
= specific_minor(minor
);
2014 r
= init_srcu_struct(&md
->io_barrier
);
2016 goto bad_io_barrier
;
2018 md
->type
= DM_TYPE_NONE
;
2019 mutex_init(&md
->suspend_lock
);
2020 mutex_init(&md
->type_lock
);
2021 spin_lock_init(&md
->deferred_lock
);
2022 atomic_set(&md
->holders
, 1);
2023 atomic_set(&md
->open_count
, 0);
2024 atomic_set(&md
->event_nr
, 0);
2025 atomic_set(&md
->uevent_seq
, 0);
2026 INIT_LIST_HEAD(&md
->uevent_list
);
2027 spin_lock_init(&md
->uevent_lock
);
2029 md
->queue
= blk_alloc_queue(GFP_KERNEL
);
2033 dm_init_md_queue(md
);
2035 md
->disk
= alloc_disk(1);
2039 atomic_set(&md
->pending
[0], 0);
2040 atomic_set(&md
->pending
[1], 0);
2041 init_waitqueue_head(&md
->wait
);
2042 INIT_WORK(&md
->work
, dm_wq_work
);
2043 init_waitqueue_head(&md
->eventq
);
2044 init_completion(&md
->kobj_holder
.completion
);
2046 md
->disk
->major
= _major
;
2047 md
->disk
->first_minor
= minor
;
2048 md
->disk
->fops
= &dm_blk_dops
;
2049 md
->disk
->queue
= md
->queue
;
2050 md
->disk
->private_data
= md
;
2051 sprintf(md
->disk
->disk_name
, "dm-%d", minor
);
2053 format_dev_t(md
->name
, MKDEV(_major
, minor
));
2055 md
->wq
= alloc_workqueue("kdmflush", WQ_MEM_RECLAIM
, 0);
2059 md
->bdev
= bdget_disk(md
->disk
, 0);
2063 bio_init(&md
->flush_bio
);
2064 md
->flush_bio
.bi_bdev
= md
->bdev
;
2065 md
->flush_bio
.bi_rw
= WRITE_FLUSH
;
2067 dm_stats_init(&md
->stats
);
2069 /* Populate the mapping, nobody knows we exist yet */
2070 spin_lock(&_minor_lock
);
2071 old_md
= idr_replace(&_minor_idr
, md
, minor
);
2072 spin_unlock(&_minor_lock
);
2074 BUG_ON(old_md
!= MINOR_ALLOCED
);
2079 destroy_workqueue(md
->wq
);
2081 del_gendisk(md
->disk
);
2084 blk_cleanup_queue(md
->queue
);
2086 cleanup_srcu_struct(&md
->io_barrier
);
2090 module_put(THIS_MODULE
);
2096 static void unlock_fs(struct mapped_device
*md
);
2098 static void free_dev(struct mapped_device
*md
)
2100 int minor
= MINOR(disk_devt(md
->disk
));
2104 destroy_workqueue(md
->wq
);
2106 mempool_destroy(md
->io_pool
);
2108 bioset_free(md
->bs
);
2109 blk_integrity_unregister(md
->disk
);
2110 del_gendisk(md
->disk
);
2111 cleanup_srcu_struct(&md
->io_barrier
);
2114 spin_lock(&_minor_lock
);
2115 md
->disk
->private_data
= NULL
;
2116 spin_unlock(&_minor_lock
);
2119 blk_cleanup_queue(md
->queue
);
2120 dm_stats_cleanup(&md
->stats
);
2121 module_put(THIS_MODULE
);
2125 static void __bind_mempools(struct mapped_device
*md
, struct dm_table
*t
)
2127 struct dm_md_mempools
*p
= dm_table_get_md_mempools(t
);
2129 if (md
->io_pool
&& md
->bs
) {
2130 /* The md already has necessary mempools. */
2131 if (dm_table_get_type(t
) == DM_TYPE_BIO_BASED
) {
2133 * Reload bioset because front_pad may have changed
2134 * because a different table was loaded.
2136 bioset_free(md
->bs
);
2139 } else if (dm_table_get_type(t
) == DM_TYPE_REQUEST_BASED
) {
2141 * There's no need to reload with request-based dm
2142 * because the size of front_pad doesn't change.
2143 * Note for future: If you are to reload bioset,
2144 * prep-ed requests in the queue may refer
2145 * to bio from the old bioset, so you must walk
2146 * through the queue to unprep.
2152 BUG_ON(!p
|| md
->io_pool
|| md
->bs
);
2154 md
->io_pool
= p
->io_pool
;
2160 /* mempool bind completed, now no need any mempools in the table */
2161 dm_table_free_md_mempools(t
);
2165 * Bind a table to the device.
2167 static void event_callback(void *context
)
2169 unsigned long flags
;
2171 struct mapped_device
*md
= (struct mapped_device
*) context
;
2173 spin_lock_irqsave(&md
->uevent_lock
, flags
);
2174 list_splice_init(&md
->uevent_list
, &uevents
);
2175 spin_unlock_irqrestore(&md
->uevent_lock
, flags
);
2177 dm_send_uevents(&uevents
, &disk_to_dev(md
->disk
)->kobj
);
2179 atomic_inc(&md
->event_nr
);
2180 wake_up(&md
->eventq
);
2184 * Protected by md->suspend_lock obtained by dm_swap_table().
2186 static void __set_size(struct mapped_device
*md
, sector_t size
)
2188 set_capacity(md
->disk
, size
);
2190 i_size_write(md
->bdev
->bd_inode
, (loff_t
)size
<< SECTOR_SHIFT
);
2194 * Return 1 if the queue has a compulsory merge_bvec_fn function.
2196 * If this function returns 0, then the device is either a non-dm
2197 * device without a merge_bvec_fn, or it is a dm device that is
2198 * able to split any bios it receives that are too big.
2200 int dm_queue_merge_is_compulsory(struct request_queue
*q
)
2202 struct mapped_device
*dev_md
;
2204 if (!q
->merge_bvec_fn
)
2207 if (q
->make_request_fn
== dm_request
) {
2208 dev_md
= q
->queuedata
;
2209 if (test_bit(DMF_MERGE_IS_OPTIONAL
, &dev_md
->flags
))
2216 static int dm_device_merge_is_compulsory(struct dm_target
*ti
,
2217 struct dm_dev
*dev
, sector_t start
,
2218 sector_t len
, void *data
)
2220 struct block_device
*bdev
= dev
->bdev
;
2221 struct request_queue
*q
= bdev_get_queue(bdev
);
2223 return dm_queue_merge_is_compulsory(q
);
2227 * Return 1 if it is acceptable to ignore merge_bvec_fn based
2228 * on the properties of the underlying devices.
2230 static int dm_table_merge_is_optional(struct dm_table
*table
)
2233 struct dm_target
*ti
;
2235 while (i
< dm_table_get_num_targets(table
)) {
2236 ti
= dm_table_get_target(table
, i
++);
2238 if (ti
->type
->iterate_devices
&&
2239 ti
->type
->iterate_devices(ti
, dm_device_merge_is_compulsory
, NULL
))
2247 * Returns old map, which caller must destroy.
2249 static struct dm_table
*__bind(struct mapped_device
*md
, struct dm_table
*t
,
2250 struct queue_limits
*limits
)
2252 struct dm_table
*old_map
;
2253 struct request_queue
*q
= md
->queue
;
2255 int merge_is_optional
;
2257 size
= dm_table_get_size(t
);
2260 * Wipe any geometry if the size of the table changed.
2262 if (size
!= dm_get_size(md
))
2263 memset(&md
->geometry
, 0, sizeof(md
->geometry
));
2265 __set_size(md
, size
);
2267 dm_table_event_callback(t
, event_callback
, md
);
2270 * The queue hasn't been stopped yet, if the old table type wasn't
2271 * for request-based during suspension. So stop it to prevent
2272 * I/O mapping before resume.
2273 * This must be done before setting the queue restrictions,
2274 * because request-based dm may be run just after the setting.
2276 if (dm_table_request_based(t
) && !blk_queue_stopped(q
))
2279 __bind_mempools(md
, t
);
2281 merge_is_optional
= dm_table_merge_is_optional(t
);
2284 rcu_assign_pointer(md
->map
, t
);
2285 md
->immutable_target_type
= dm_table_get_immutable_target_type(t
);
2287 dm_table_set_restrictions(t
, q
, limits
);
2288 if (merge_is_optional
)
2289 set_bit(DMF_MERGE_IS_OPTIONAL
, &md
->flags
);
2291 clear_bit(DMF_MERGE_IS_OPTIONAL
, &md
->flags
);
2298 * Returns unbound table for the caller to free.
2300 static struct dm_table
*__unbind(struct mapped_device
*md
)
2302 struct dm_table
*map
= md
->map
;
2307 dm_table_event_callback(map
, NULL
, NULL
);
2308 rcu_assign_pointer(md
->map
, NULL
);
2315 * Constructor for a new device.
2317 int dm_create(int minor
, struct mapped_device
**result
)
2319 struct mapped_device
*md
;
2321 md
= alloc_dev(minor
);
2332 * Functions to manage md->type.
2333 * All are required to hold md->type_lock.
2335 void dm_lock_md_type(struct mapped_device
*md
)
2337 mutex_lock(&md
->type_lock
);
2340 void dm_unlock_md_type(struct mapped_device
*md
)
2342 mutex_unlock(&md
->type_lock
);
2345 void dm_set_md_type(struct mapped_device
*md
, unsigned type
)
2347 BUG_ON(!mutex_is_locked(&md
->type_lock
));
2351 unsigned dm_get_md_type(struct mapped_device
*md
)
2353 BUG_ON(!mutex_is_locked(&md
->type_lock
));
2357 struct target_type
*dm_get_immutable_target_type(struct mapped_device
*md
)
2359 return md
->immutable_target_type
;
2363 * The queue_limits are only valid as long as you have a reference
2366 struct queue_limits
*dm_get_queue_limits(struct mapped_device
*md
)
2368 BUG_ON(!atomic_read(&md
->holders
));
2369 return &md
->queue
->limits
;
2371 EXPORT_SYMBOL_GPL(dm_get_queue_limits
);
2374 * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
2376 static int dm_init_request_based_queue(struct mapped_device
*md
)
2378 struct request_queue
*q
= NULL
;
2380 if (md
->queue
->elevator
)
2383 /* Fully initialize the queue */
2384 q
= blk_init_allocated_queue(md
->queue
, dm_request_fn
, NULL
);
2389 dm_init_md_queue(md
);
2390 blk_queue_softirq_done(md
->queue
, dm_softirq_done
);
2391 blk_queue_prep_rq(md
->queue
, dm_prep_fn
);
2392 blk_queue_lld_busy(md
->queue
, dm_lld_busy
);
2394 elv_register_queue(md
->queue
);
2400 * Setup the DM device's queue based on md's type
2402 int dm_setup_md_queue(struct mapped_device
*md
)
2404 if ((dm_get_md_type(md
) == DM_TYPE_REQUEST_BASED
) &&
2405 !dm_init_request_based_queue(md
)) {
2406 DMWARN("Cannot initialize queue for request-based mapped device");
2413 static struct mapped_device
*dm_find_md(dev_t dev
)
2415 struct mapped_device
*md
;
2416 unsigned minor
= MINOR(dev
);
2418 if (MAJOR(dev
) != _major
|| minor
>= (1 << MINORBITS
))
2421 spin_lock(&_minor_lock
);
2423 md
= idr_find(&_minor_idr
, minor
);
2424 if (md
&& (md
== MINOR_ALLOCED
||
2425 (MINOR(disk_devt(dm_disk(md
))) != minor
) ||
2426 dm_deleting_md(md
) ||
2427 test_bit(DMF_FREEING
, &md
->flags
))) {
2433 spin_unlock(&_minor_lock
);
2438 struct mapped_device
*dm_get_md(dev_t dev
)
2440 struct mapped_device
*md
= dm_find_md(dev
);
2447 EXPORT_SYMBOL_GPL(dm_get_md
);
2449 void *dm_get_mdptr(struct mapped_device
*md
)
2451 return md
->interface_ptr
;
2454 void dm_set_mdptr(struct mapped_device
*md
, void *ptr
)
2456 md
->interface_ptr
= ptr
;
2459 void dm_get(struct mapped_device
*md
)
2461 atomic_inc(&md
->holders
);
2462 BUG_ON(test_bit(DMF_FREEING
, &md
->flags
));
2465 const char *dm_device_name(struct mapped_device
*md
)
2469 EXPORT_SYMBOL_GPL(dm_device_name
);
2471 static void __dm_destroy(struct mapped_device
*md
, bool wait
)
2473 struct dm_table
*map
;
2478 spin_lock(&_minor_lock
);
2479 map
= dm_get_live_table(md
, &srcu_idx
);
2480 idr_replace(&_minor_idr
, MINOR_ALLOCED
, MINOR(disk_devt(dm_disk(md
))));
2481 set_bit(DMF_FREEING
, &md
->flags
);
2482 spin_unlock(&_minor_lock
);
2484 if (!dm_suspended_md(md
)) {
2485 dm_table_presuspend_targets(map
);
2486 dm_table_postsuspend_targets(map
);
2489 /* dm_put_live_table must be before msleep, otherwise deadlock is possible */
2490 dm_put_live_table(md
, srcu_idx
);
2493 * Rare, but there may be I/O requests still going to complete,
2494 * for example. Wait for all references to disappear.
2495 * No one should increment the reference count of the mapped_device,
2496 * after the mapped_device state becomes DMF_FREEING.
2499 while (atomic_read(&md
->holders
))
2501 else if (atomic_read(&md
->holders
))
2502 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2503 dm_device_name(md
), atomic_read(&md
->holders
));
2506 dm_table_destroy(__unbind(md
));
2510 void dm_destroy(struct mapped_device
*md
)
2512 __dm_destroy(md
, true);
2515 void dm_destroy_immediate(struct mapped_device
*md
)
2517 __dm_destroy(md
, false);
2520 void dm_put(struct mapped_device
*md
)
2522 atomic_dec(&md
->holders
);
2524 EXPORT_SYMBOL_GPL(dm_put
);
2526 static int dm_wait_for_completion(struct mapped_device
*md
, int interruptible
)
2529 DECLARE_WAITQUEUE(wait
, current
);
2531 add_wait_queue(&md
->wait
, &wait
);
2534 set_current_state(interruptible
);
2536 if (!md_in_flight(md
))
2539 if (interruptible
== TASK_INTERRUPTIBLE
&&
2540 signal_pending(current
)) {
2547 set_current_state(TASK_RUNNING
);
2549 remove_wait_queue(&md
->wait
, &wait
);
2555 * Process the deferred bios
2557 static void dm_wq_work(struct work_struct
*work
)
2559 struct mapped_device
*md
= container_of(work
, struct mapped_device
,
2563 struct dm_table
*map
;
2565 map
= dm_get_live_table(md
, &srcu_idx
);
2567 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND
, &md
->flags
)) {
2568 spin_lock_irq(&md
->deferred_lock
);
2569 c
= bio_list_pop(&md
->deferred
);
2570 spin_unlock_irq(&md
->deferred_lock
);
2575 if (dm_request_based(md
))
2576 generic_make_request(c
);
2578 __split_and_process_bio(md
, map
, c
);
2581 dm_put_live_table(md
, srcu_idx
);
2584 static void dm_queue_flush(struct mapped_device
*md
)
2586 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND
, &md
->flags
);
2587 smp_mb__after_clear_bit();
2588 queue_work(md
->wq
, &md
->work
);
2592 * Swap in a new table, returning the old one for the caller to destroy.
2594 struct dm_table
*dm_swap_table(struct mapped_device
*md
, struct dm_table
*table
)
2596 struct dm_table
*live_map
= NULL
, *map
= ERR_PTR(-EINVAL
);
2597 struct queue_limits limits
;
2600 mutex_lock(&md
->suspend_lock
);
2602 /* device must be suspended */
2603 if (!dm_suspended_md(md
))
2607 * If the new table has no data devices, retain the existing limits.
2608 * This helps multipath with queue_if_no_path if all paths disappear,
2609 * then new I/O is queued based on these limits, and then some paths
2612 if (dm_table_has_no_data_devices(table
)) {
2613 live_map
= dm_get_live_table_fast(md
);
2615 limits
= md
->queue
->limits
;
2616 dm_put_live_table_fast(md
);
2620 r
= dm_calculate_queue_limits(table
, &limits
);
2627 map
= __bind(md
, table
, &limits
);
2630 mutex_unlock(&md
->suspend_lock
);
2635 * Functions to lock and unlock any filesystem running on the
2638 static int lock_fs(struct mapped_device
*md
)
2642 WARN_ON(md
->frozen_sb
);
2644 md
->frozen_sb
= freeze_bdev(md
->bdev
);
2645 if (IS_ERR(md
->frozen_sb
)) {
2646 r
= PTR_ERR(md
->frozen_sb
);
2647 md
->frozen_sb
= NULL
;
2651 set_bit(DMF_FROZEN
, &md
->flags
);
2656 static void unlock_fs(struct mapped_device
*md
)
2658 if (!test_bit(DMF_FROZEN
, &md
->flags
))
2661 thaw_bdev(md
->bdev
, md
->frozen_sb
);
2662 md
->frozen_sb
= NULL
;
2663 clear_bit(DMF_FROZEN
, &md
->flags
);
2667 * We need to be able to change a mapping table under a mounted
2668 * filesystem. For example we might want to move some data in
2669 * the background. Before the table can be swapped with
2670 * dm_bind_table, dm_suspend must be called to flush any in
2671 * flight bios and ensure that any further io gets deferred.
2674 * Suspend mechanism in request-based dm.
2676 * 1. Flush all I/Os by lock_fs() if needed.
2677 * 2. Stop dispatching any I/O by stopping the request_queue.
2678 * 3. Wait for all in-flight I/Os to be completed or requeued.
2680 * To abort suspend, start the request_queue.
2682 int dm_suspend(struct mapped_device
*md
, unsigned suspend_flags
)
2684 struct dm_table
*map
= NULL
;
2686 int do_lockfs
= suspend_flags
& DM_SUSPEND_LOCKFS_FLAG
? 1 : 0;
2687 int noflush
= suspend_flags
& DM_SUSPEND_NOFLUSH_FLAG
? 1 : 0;
2689 mutex_lock(&md
->suspend_lock
);
2691 if (dm_suspended_md(md
)) {
2699 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2700 * This flag is cleared before dm_suspend returns.
2703 set_bit(DMF_NOFLUSH_SUSPENDING
, &md
->flags
);
2705 /* This does not get reverted if there's an error later. */
2706 dm_table_presuspend_targets(map
);
2709 * Flush I/O to the device.
2710 * Any I/O submitted after lock_fs() may not be flushed.
2711 * noflush takes precedence over do_lockfs.
2712 * (lock_fs() flushes I/Os and waits for them to complete.)
2714 if (!noflush
&& do_lockfs
) {
2721 * Here we must make sure that no processes are submitting requests
2722 * to target drivers i.e. no one may be executing
2723 * __split_and_process_bio. This is called from dm_request and
2726 * To get all processes out of __split_and_process_bio in dm_request,
2727 * we take the write lock. To prevent any process from reentering
2728 * __split_and_process_bio from dm_request and quiesce the thread
2729 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
2730 * flush_workqueue(md->wq).
2732 set_bit(DMF_BLOCK_IO_FOR_SUSPEND
, &md
->flags
);
2733 synchronize_srcu(&md
->io_barrier
);
2736 * Stop md->queue before flushing md->wq in case request-based
2737 * dm defers requests to md->wq from md->queue.
2739 if (dm_request_based(md
))
2740 stop_queue(md
->queue
);
2742 flush_workqueue(md
->wq
);
2745 * At this point no more requests are entering target request routines.
2746 * We call dm_wait_for_completion to wait for all existing requests
2749 r
= dm_wait_for_completion(md
, TASK_INTERRUPTIBLE
);
2752 clear_bit(DMF_NOFLUSH_SUSPENDING
, &md
->flags
);
2753 synchronize_srcu(&md
->io_barrier
);
2755 /* were we interrupted ? */
2759 if (dm_request_based(md
))
2760 start_queue(md
->queue
);
2763 goto out_unlock
; /* pushback list is already flushed, so skip flush */
2767 * If dm_wait_for_completion returned 0, the device is completely
2768 * quiescent now. There is no request-processing activity. All new
2769 * requests are being added to md->deferred list.
2772 set_bit(DMF_SUSPENDED
, &md
->flags
);
2774 dm_table_postsuspend_targets(map
);
2777 mutex_unlock(&md
->suspend_lock
);
2781 int dm_resume(struct mapped_device
*md
)
2784 struct dm_table
*map
= NULL
;
2786 mutex_lock(&md
->suspend_lock
);
2787 if (!dm_suspended_md(md
))
2791 if (!map
|| !dm_table_get_size(map
))
2794 r
= dm_table_resume_targets(map
);
2801 * Flushing deferred I/Os must be done after targets are resumed
2802 * so that mapping of targets can work correctly.
2803 * Request-based dm is queueing the deferred I/Os in its request_queue.
2805 if (dm_request_based(md
))
2806 start_queue(md
->queue
);
2810 clear_bit(DMF_SUSPENDED
, &md
->flags
);
2814 mutex_unlock(&md
->suspend_lock
);
2820 * Internal suspend/resume works like userspace-driven suspend. It waits
2821 * until all bios finish and prevents issuing new bios to the target drivers.
2822 * It may be used only from the kernel.
2824 * Internal suspend holds md->suspend_lock, which prevents interaction with
2825 * userspace-driven suspend.
2828 void dm_internal_suspend(struct mapped_device
*md
)
2830 mutex_lock(&md
->suspend_lock
);
2831 if (dm_suspended_md(md
))
2834 set_bit(DMF_BLOCK_IO_FOR_SUSPEND
, &md
->flags
);
2835 synchronize_srcu(&md
->io_barrier
);
2836 flush_workqueue(md
->wq
);
2837 dm_wait_for_completion(md
, TASK_UNINTERRUPTIBLE
);
2840 void dm_internal_resume(struct mapped_device
*md
)
2842 if (dm_suspended_md(md
))
2848 mutex_unlock(&md
->suspend_lock
);
2851 /*-----------------------------------------------------------------
2852 * Event notification.
2853 *---------------------------------------------------------------*/
2854 int dm_kobject_uevent(struct mapped_device
*md
, enum kobject_action action
,
2857 char udev_cookie
[DM_COOKIE_LENGTH
];
2858 char *envp
[] = { udev_cookie
, NULL
};
2861 return kobject_uevent(&disk_to_dev(md
->disk
)->kobj
, action
);
2863 snprintf(udev_cookie
, DM_COOKIE_LENGTH
, "%s=%u",
2864 DM_COOKIE_ENV_VAR_NAME
, cookie
);
2865 return kobject_uevent_env(&disk_to_dev(md
->disk
)->kobj
,
2870 uint32_t dm_next_uevent_seq(struct mapped_device
*md
)
2872 return atomic_add_return(1, &md
->uevent_seq
);
2875 uint32_t dm_get_event_nr(struct mapped_device
*md
)
2877 return atomic_read(&md
->event_nr
);
2880 int dm_wait_event(struct mapped_device
*md
, int event_nr
)
2882 return wait_event_interruptible(md
->eventq
,
2883 (event_nr
!= atomic_read(&md
->event_nr
)));
2886 void dm_uevent_add(struct mapped_device
*md
, struct list_head
*elist
)
2888 unsigned long flags
;
2890 spin_lock_irqsave(&md
->uevent_lock
, flags
);
2891 list_add(elist
, &md
->uevent_list
);
2892 spin_unlock_irqrestore(&md
->uevent_lock
, flags
);
2896 * The gendisk is only valid as long as you have a reference
2899 struct gendisk
*dm_disk(struct mapped_device
*md
)
2904 struct kobject
*dm_kobject(struct mapped_device
*md
)
2906 return &md
->kobj_holder
.kobj
;
2909 struct mapped_device
*dm_get_from_kobject(struct kobject
*kobj
)
2911 struct mapped_device
*md
;
2913 md
= container_of(kobj
, struct mapped_device
, kobj_holder
.kobj
);
2915 if (test_bit(DMF_FREEING
, &md
->flags
) ||
2923 int dm_suspended_md(struct mapped_device
*md
)
2925 return test_bit(DMF_SUSPENDED
, &md
->flags
);
2928 int dm_test_deferred_remove_flag(struct mapped_device
*md
)
2930 return test_bit(DMF_DEFERRED_REMOVE
, &md
->flags
);
2933 int dm_suspended(struct dm_target
*ti
)
2935 return dm_suspended_md(dm_table_get_md(ti
->table
));
2937 EXPORT_SYMBOL_GPL(dm_suspended
);
2939 int dm_noflush_suspending(struct dm_target
*ti
)
2941 return __noflush_suspending(dm_table_get_md(ti
->table
));
2943 EXPORT_SYMBOL_GPL(dm_noflush_suspending
);
2945 struct dm_md_mempools
*dm_alloc_md_mempools(unsigned type
, unsigned integrity
, unsigned per_bio_data_size
)
2947 struct dm_md_mempools
*pools
= kzalloc(sizeof(*pools
), GFP_KERNEL
);
2948 struct kmem_cache
*cachep
;
2949 unsigned int pool_size
;
2950 unsigned int front_pad
;
2955 if (type
== DM_TYPE_BIO_BASED
) {
2957 pool_size
= dm_get_reserved_bio_based_ios();
2958 front_pad
= roundup(per_bio_data_size
, __alignof__(struct dm_target_io
)) + offsetof(struct dm_target_io
, clone
);
2959 } else if (type
== DM_TYPE_REQUEST_BASED
) {
2960 cachep
= _rq_tio_cache
;
2961 pool_size
= dm_get_reserved_rq_based_ios();
2962 front_pad
= offsetof(struct dm_rq_clone_bio_info
, clone
);
2963 /* per_bio_data_size is not used. See __bind_mempools(). */
2964 WARN_ON(per_bio_data_size
!= 0);
2968 pools
->io_pool
= mempool_create_slab_pool(pool_size
, cachep
);
2969 if (!pools
->io_pool
)
2972 pools
->bs
= bioset_create(pool_size
, front_pad
);
2976 if (integrity
&& bioset_integrity_create(pools
->bs
, pool_size
))
2982 dm_free_md_mempools(pools
);
2987 void dm_free_md_mempools(struct dm_md_mempools
*pools
)
2993 mempool_destroy(pools
->io_pool
);
2996 bioset_free(pools
->bs
);
3001 static const struct block_device_operations dm_blk_dops
= {
3002 .open
= dm_blk_open
,
3003 .release
= dm_blk_close
,
3004 .ioctl
= dm_blk_ioctl
,
3005 .getgeo
= dm_blk_getgeo
,
3006 .owner
= THIS_MODULE
3009 EXPORT_SYMBOL(dm_get_mapinfo
);
3014 module_init(dm_init
);
3015 module_exit(dm_exit
);
3017 module_param(major
, uint
, 0);
3018 MODULE_PARM_DESC(major
, "The major number of the device mapper");
3020 module_param(reserved_bio_based_ios
, uint
, S_IRUGO
| S_IWUSR
);
3021 MODULE_PARM_DESC(reserved_bio_based_ios
, "Reserved IOs in bio-based mempools");
3023 module_param(reserved_rq_based_ios
, uint
, S_IRUGO
| S_IWUSR
);
3024 MODULE_PARM_DESC(reserved_rq_based_ios
, "Reserved IOs in request-based mempools");
3026 MODULE_DESCRIPTION(DM_NAME
" driver");
3027 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
3028 MODULE_LICENSE("GPL");