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/buffer_head.h>
18 #include <linux/mempool.h>
19 #include <linux/slab.h>
20 #include <linux/idr.h>
21 #include <linux/hdreg.h>
22 #include <linux/delay.h>
24 #include <trace/events/block.h>
26 #define DM_MSG_PREFIX "core"
30 * ratelimit state to be used in DMXXX_LIMIT().
32 DEFINE_RATELIMIT_STATE(dm_ratelimit_state
,
33 DEFAULT_RATELIMIT_INTERVAL
,
34 DEFAULT_RATELIMIT_BURST
);
35 EXPORT_SYMBOL(dm_ratelimit_state
);
39 * Cookies are numeric values sent with CHANGE and REMOVE
40 * uevents while resuming, removing or renaming the device.
42 #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
43 #define DM_COOKIE_LENGTH 24
45 static const char *_name
= DM_NAME
;
47 static unsigned int major
= 0;
48 static unsigned int _major
= 0;
50 static DEFINE_IDR(_minor_idr
);
52 static DEFINE_SPINLOCK(_minor_lock
);
55 * One of these is allocated per bio.
58 struct mapped_device
*md
;
62 unsigned long start_time
;
63 spinlock_t endio_lock
;
68 * One of these is allocated per target within a bio. Hopefully
69 * this will be simplified out one day.
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
;
90 * For request-based dm.
91 * One of these is allocated per bio.
93 struct dm_rq_clone_bio_info
{
95 struct dm_rq_target_io
*tio
;
98 union map_info
*dm_get_mapinfo(struct bio
*bio
)
100 if (bio
&& bio
->bi_private
)
101 return &((struct dm_target_io
*)bio
->bi_private
)->info
;
105 union map_info
*dm_get_rq_mapinfo(struct request
*rq
)
107 if (rq
&& rq
->end_io_data
)
108 return &((struct dm_rq_target_io
*)rq
->end_io_data
)->info
;
111 EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo
);
113 #define MINOR_ALLOCED ((void *)-1)
116 * Bits for the md->flags field.
118 #define DMF_BLOCK_IO_FOR_SUSPEND 0
119 #define DMF_SUSPENDED 1
121 #define DMF_FREEING 3
122 #define DMF_DELETING 4
123 #define DMF_NOFLUSH_SUSPENDING 5
124 #define DMF_MERGE_IS_OPTIONAL 6
127 * Work processed by per-device workqueue.
129 struct mapped_device
{
130 struct rw_semaphore io_lock
;
131 struct mutex suspend_lock
;
138 struct request_queue
*queue
;
140 /* Protect queue and type against concurrent access. */
141 struct mutex type_lock
;
143 struct gendisk
*disk
;
149 * A list of ios that arrived while we were suspended.
152 wait_queue_head_t wait
;
153 struct work_struct work
;
154 struct bio_list deferred
;
155 spinlock_t deferred_lock
;
158 * Processing queue (flush)
160 struct workqueue_struct
*wq
;
163 * The current mapping.
165 struct dm_table
*map
;
168 * io objects are allocated from here.
179 wait_queue_head_t eventq
;
181 struct list_head uevent_list
;
182 spinlock_t uevent_lock
; /* Protect access to uevent_list */
185 * freeze/thaw support require holding onto a super block
187 struct super_block
*frozen_sb
;
188 struct block_device
*bdev
;
190 /* forced geometry settings */
191 struct hd_geometry geometry
;
193 /* For saving the address of __make_request for request based dm */
194 make_request_fn
*saved_make_request_fn
;
199 /* zero-length flush that will be cloned and submitted to targets */
200 struct bio flush_bio
;
204 * For mempools pre-allocation at the table loading time.
206 struct dm_md_mempools
{
213 static struct kmem_cache
*_io_cache
;
214 static struct kmem_cache
*_tio_cache
;
215 static struct kmem_cache
*_rq_tio_cache
;
216 static struct kmem_cache
*_rq_bio_info_cache
;
218 static int __init
local_init(void)
222 /* allocate a slab for the dm_ios */
223 _io_cache
= KMEM_CACHE(dm_io
, 0);
227 /* allocate a slab for the target ios */
228 _tio_cache
= KMEM_CACHE(dm_target_io
, 0);
230 goto out_free_io_cache
;
232 _rq_tio_cache
= KMEM_CACHE(dm_rq_target_io
, 0);
234 goto out_free_tio_cache
;
236 _rq_bio_info_cache
= KMEM_CACHE(dm_rq_clone_bio_info
, 0);
237 if (!_rq_bio_info_cache
)
238 goto out_free_rq_tio_cache
;
240 r
= dm_uevent_init();
242 goto out_free_rq_bio_info_cache
;
245 r
= register_blkdev(_major
, _name
);
247 goto out_uevent_exit
;
256 out_free_rq_bio_info_cache
:
257 kmem_cache_destroy(_rq_bio_info_cache
);
258 out_free_rq_tio_cache
:
259 kmem_cache_destroy(_rq_tio_cache
);
261 kmem_cache_destroy(_tio_cache
);
263 kmem_cache_destroy(_io_cache
);
268 static void local_exit(void)
270 kmem_cache_destroy(_rq_bio_info_cache
);
271 kmem_cache_destroy(_rq_tio_cache
);
272 kmem_cache_destroy(_tio_cache
);
273 kmem_cache_destroy(_io_cache
);
274 unregister_blkdev(_major
, _name
);
279 DMINFO("cleaned up");
282 static int (*_inits
[])(void) __initdata
= {
292 static void (*_exits
[])(void) = {
302 static int __init
dm_init(void)
304 const int count
= ARRAY_SIZE(_inits
);
308 for (i
= 0; i
< count
; i
++) {
323 static void __exit
dm_exit(void)
325 int i
= ARRAY_SIZE(_exits
);
331 * Should be empty by this point.
333 idr_remove_all(&_minor_idr
);
334 idr_destroy(&_minor_idr
);
338 * Block device functions
340 int dm_deleting_md(struct mapped_device
*md
)
342 return test_bit(DMF_DELETING
, &md
->flags
);
345 static int dm_blk_open(struct block_device
*bdev
, fmode_t mode
)
347 struct mapped_device
*md
;
349 spin_lock(&_minor_lock
);
351 md
= bdev
->bd_disk
->private_data
;
355 if (test_bit(DMF_FREEING
, &md
->flags
) ||
356 dm_deleting_md(md
)) {
362 atomic_inc(&md
->open_count
);
365 spin_unlock(&_minor_lock
);
367 return md
? 0 : -ENXIO
;
370 static int dm_blk_close(struct gendisk
*disk
, fmode_t mode
)
372 struct mapped_device
*md
= disk
->private_data
;
374 spin_lock(&_minor_lock
);
376 atomic_dec(&md
->open_count
);
379 spin_unlock(&_minor_lock
);
384 int dm_open_count(struct mapped_device
*md
)
386 return atomic_read(&md
->open_count
);
390 * Guarantees nothing is using the device before it's deleted.
392 int dm_lock_for_deletion(struct mapped_device
*md
)
396 spin_lock(&_minor_lock
);
398 if (dm_open_count(md
))
401 set_bit(DMF_DELETING
, &md
->flags
);
403 spin_unlock(&_minor_lock
);
408 static int dm_blk_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
410 struct mapped_device
*md
= bdev
->bd_disk
->private_data
;
412 return dm_get_geometry(md
, geo
);
415 static int dm_blk_ioctl(struct block_device
*bdev
, fmode_t mode
,
416 unsigned int cmd
, unsigned long arg
)
418 struct mapped_device
*md
= bdev
->bd_disk
->private_data
;
419 struct dm_table
*map
= dm_get_live_table(md
);
420 struct dm_target
*tgt
;
423 if (!map
|| !dm_table_get_size(map
))
426 /* We only support devices that have a single target */
427 if (dm_table_get_num_targets(map
) != 1)
430 tgt
= dm_table_get_target(map
, 0);
432 if (dm_suspended_md(md
)) {
437 if (tgt
->type
->ioctl
)
438 r
= tgt
->type
->ioctl(tgt
, cmd
, arg
);
446 static struct dm_io
*alloc_io(struct mapped_device
*md
)
448 return mempool_alloc(md
->io_pool
, GFP_NOIO
);
451 static void free_io(struct mapped_device
*md
, struct dm_io
*io
)
453 mempool_free(io
, md
->io_pool
);
456 static void free_tio(struct mapped_device
*md
, struct dm_target_io
*tio
)
458 mempool_free(tio
, md
->tio_pool
);
461 static struct dm_rq_target_io
*alloc_rq_tio(struct mapped_device
*md
,
464 return mempool_alloc(md
->tio_pool
, gfp_mask
);
467 static void free_rq_tio(struct dm_rq_target_io
*tio
)
469 mempool_free(tio
, tio
->md
->tio_pool
);
472 static struct dm_rq_clone_bio_info
*alloc_bio_info(struct mapped_device
*md
)
474 return mempool_alloc(md
->io_pool
, GFP_ATOMIC
);
477 static void free_bio_info(struct dm_rq_clone_bio_info
*info
)
479 mempool_free(info
, info
->tio
->md
->io_pool
);
482 static int md_in_flight(struct mapped_device
*md
)
484 return atomic_read(&md
->pending
[READ
]) +
485 atomic_read(&md
->pending
[WRITE
]);
488 static void start_io_acct(struct dm_io
*io
)
490 struct mapped_device
*md
= io
->md
;
492 int rw
= bio_data_dir(io
->bio
);
494 io
->start_time
= jiffies
;
496 cpu
= part_stat_lock();
497 part_round_stats(cpu
, &dm_disk(md
)->part0
);
499 atomic_set(&dm_disk(md
)->part0
.in_flight
[rw
],
500 atomic_inc_return(&md
->pending
[rw
]));
503 static void end_io_acct(struct dm_io
*io
)
505 struct mapped_device
*md
= io
->md
;
506 struct bio
*bio
= io
->bio
;
507 unsigned long duration
= jiffies
- io
->start_time
;
509 int rw
= bio_data_dir(bio
);
511 cpu
= part_stat_lock();
512 part_round_stats(cpu
, &dm_disk(md
)->part0
);
513 part_stat_add(cpu
, &dm_disk(md
)->part0
, ticks
[rw
], duration
);
517 * After this is decremented the bio must not be touched if it is
520 pending
= atomic_dec_return(&md
->pending
[rw
]);
521 atomic_set(&dm_disk(md
)->part0
.in_flight
[rw
], pending
);
522 pending
+= atomic_read(&md
->pending
[rw
^0x1]);
524 /* nudge anyone waiting on suspend queue */
530 * Add the bio to the list of deferred io.
532 static void queue_io(struct mapped_device
*md
, struct bio
*bio
)
536 spin_lock_irqsave(&md
->deferred_lock
, flags
);
537 bio_list_add(&md
->deferred
, bio
);
538 spin_unlock_irqrestore(&md
->deferred_lock
, flags
);
539 queue_work(md
->wq
, &md
->work
);
543 * Everyone (including functions in this file), should use this
544 * function to access the md->map field, and make sure they call
545 * dm_table_put() when finished.
547 struct dm_table
*dm_get_live_table(struct mapped_device
*md
)
552 read_lock_irqsave(&md
->map_lock
, flags
);
556 read_unlock_irqrestore(&md
->map_lock
, flags
);
562 * Get the geometry associated with a dm device
564 int dm_get_geometry(struct mapped_device
*md
, struct hd_geometry
*geo
)
572 * Set the geometry of a device.
574 int dm_set_geometry(struct mapped_device
*md
, struct hd_geometry
*geo
)
576 sector_t sz
= (sector_t
)geo
->cylinders
* geo
->heads
* geo
->sectors
;
578 if (geo
->start
> sz
) {
579 DMWARN("Start sector is beyond the geometry limits.");
588 /*-----------------------------------------------------------------
590 * A more elegant soln is in the works that uses the queue
591 * merge fn, unfortunately there are a couple of changes to
592 * the block layer that I want to make for this. So in the
593 * interests of getting something for people to use I give
594 * you this clearly demarcated crap.
595 *---------------------------------------------------------------*/
597 static int __noflush_suspending(struct mapped_device
*md
)
599 return test_bit(DMF_NOFLUSH_SUSPENDING
, &md
->flags
);
603 * Decrements the number of outstanding ios that a bio has been
604 * cloned into, completing the original io if necc.
606 static void dec_pending(struct dm_io
*io
, int error
)
611 struct mapped_device
*md
= io
->md
;
613 /* Push-back supersedes any I/O errors */
614 if (unlikely(error
)) {
615 spin_lock_irqsave(&io
->endio_lock
, flags
);
616 if (!(io
->error
> 0 && __noflush_suspending(md
)))
618 spin_unlock_irqrestore(&io
->endio_lock
, flags
);
621 if (atomic_dec_and_test(&io
->io_count
)) {
622 if (io
->error
== DM_ENDIO_REQUEUE
) {
624 * Target requested pushing back the I/O.
626 spin_lock_irqsave(&md
->deferred_lock
, flags
);
627 if (__noflush_suspending(md
))
628 bio_list_add_head(&md
->deferred
, io
->bio
);
630 /* noflush suspend was interrupted. */
632 spin_unlock_irqrestore(&md
->deferred_lock
, flags
);
635 io_error
= io
->error
;
640 if (io_error
== DM_ENDIO_REQUEUE
)
643 if ((bio
->bi_rw
& REQ_FLUSH
) && bio
->bi_size
) {
645 * Preflush done for flush with data, reissue
648 bio
->bi_rw
&= ~REQ_FLUSH
;
651 /* done with normal IO or empty flush */
652 trace_block_bio_complete(md
->queue
, bio
, io_error
);
653 bio_endio(bio
, io_error
);
658 static void clone_endio(struct bio
*bio
, int error
)
661 struct dm_target_io
*tio
= bio
->bi_private
;
662 struct dm_io
*io
= tio
->io
;
663 struct mapped_device
*md
= tio
->io
->md
;
664 dm_endio_fn endio
= tio
->ti
->type
->end_io
;
666 if (!bio_flagged(bio
, BIO_UPTODATE
) && !error
)
670 r
= endio(tio
->ti
, bio
, error
, &tio
->info
);
671 if (r
< 0 || r
== DM_ENDIO_REQUEUE
)
673 * error and requeue request are handled
677 else if (r
== DM_ENDIO_INCOMPLETE
)
678 /* The target will handle the io */
681 DMWARN("unimplemented target endio return value: %d", r
);
687 * Store md for cleanup instead of tio which is about to get freed.
689 bio
->bi_private
= md
->bs
;
693 dec_pending(io
, error
);
697 * Partial completion handling for request-based dm
699 static void end_clone_bio(struct bio
*clone
, int error
)
701 struct dm_rq_clone_bio_info
*info
= clone
->bi_private
;
702 struct dm_rq_target_io
*tio
= info
->tio
;
703 struct bio
*bio
= info
->orig
;
704 unsigned int nr_bytes
= info
->orig
->bi_size
;
710 * An error has already been detected on the request.
711 * Once error occurred, just let clone->end_io() handle
717 * Don't notice the error to the upper layer yet.
718 * The error handling decision is made by the target driver,
719 * when the request is completed.
726 * I/O for the bio successfully completed.
727 * Notice the data completion to the upper layer.
731 * bios are processed from the head of the list.
732 * So the completing bio should always be rq->bio.
733 * If it's not, something wrong is happening.
735 if (tio
->orig
->bio
!= bio
)
736 DMERR("bio completion is going in the middle of the request");
739 * Update the original request.
740 * Do not use blk_end_request() here, because it may complete
741 * the original request before the clone, and break the ordering.
743 blk_update_request(tio
->orig
, 0, nr_bytes
);
747 * Don't touch any member of the md after calling this function because
748 * the md may be freed in dm_put() at the end of this function.
749 * Or do dm_get() before calling this function and dm_put() later.
751 static void rq_completed(struct mapped_device
*md
, int rw
, int run_queue
)
753 atomic_dec(&md
->pending
[rw
]);
755 /* nudge anyone waiting on suspend queue */
756 if (!md_in_flight(md
))
760 blk_run_queue(md
->queue
);
763 * dm_put() must be at the end of this function. See the comment above
768 static void free_rq_clone(struct request
*clone
)
770 struct dm_rq_target_io
*tio
= clone
->end_io_data
;
772 blk_rq_unprep_clone(clone
);
777 * Complete the clone and the original request.
778 * Must be called without queue lock.
780 static void dm_end_request(struct request
*clone
, int error
)
782 int rw
= rq_data_dir(clone
);
783 struct dm_rq_target_io
*tio
= clone
->end_io_data
;
784 struct mapped_device
*md
= tio
->md
;
785 struct request
*rq
= tio
->orig
;
787 if (rq
->cmd_type
== REQ_TYPE_BLOCK_PC
) {
788 rq
->errors
= clone
->errors
;
789 rq
->resid_len
= clone
->resid_len
;
793 * We are using the sense buffer of the original
795 * So setting the length of the sense data is enough.
797 rq
->sense_len
= clone
->sense_len
;
800 free_rq_clone(clone
);
801 blk_end_request_all(rq
, error
);
802 rq_completed(md
, rw
, true);
805 static void dm_unprep_request(struct request
*rq
)
807 struct request
*clone
= rq
->special
;
810 rq
->cmd_flags
&= ~REQ_DONTPREP
;
812 free_rq_clone(clone
);
816 * Requeue the original request of a clone.
818 void dm_requeue_unmapped_request(struct request
*clone
)
820 int rw
= rq_data_dir(clone
);
821 struct dm_rq_target_io
*tio
= clone
->end_io_data
;
822 struct mapped_device
*md
= tio
->md
;
823 struct request
*rq
= tio
->orig
;
824 struct request_queue
*q
= rq
->q
;
827 dm_unprep_request(rq
);
829 spin_lock_irqsave(q
->queue_lock
, flags
);
830 blk_requeue_request(q
, rq
);
831 spin_unlock_irqrestore(q
->queue_lock
, flags
);
833 rq_completed(md
, rw
, 0);
835 EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request
);
837 static void __stop_queue(struct request_queue
*q
)
842 static void stop_queue(struct request_queue
*q
)
846 spin_lock_irqsave(q
->queue_lock
, flags
);
848 spin_unlock_irqrestore(q
->queue_lock
, flags
);
851 static void __start_queue(struct request_queue
*q
)
853 if (blk_queue_stopped(q
))
857 static void start_queue(struct request_queue
*q
)
861 spin_lock_irqsave(q
->queue_lock
, flags
);
863 spin_unlock_irqrestore(q
->queue_lock
, flags
);
866 static void dm_done(struct request
*clone
, int error
, bool mapped
)
869 struct dm_rq_target_io
*tio
= clone
->end_io_data
;
870 dm_request_endio_fn rq_end_io
= tio
->ti
->type
->rq_end_io
;
872 if (mapped
&& rq_end_io
)
873 r
= rq_end_io(tio
->ti
, clone
, error
, &tio
->info
);
876 /* The target wants to complete the I/O */
877 dm_end_request(clone
, r
);
878 else if (r
== DM_ENDIO_INCOMPLETE
)
879 /* The target will handle the I/O */
881 else if (r
== DM_ENDIO_REQUEUE
)
882 /* The target wants to requeue the I/O */
883 dm_requeue_unmapped_request(clone
);
885 DMWARN("unimplemented target endio return value: %d", r
);
891 * Request completion handler for request-based dm
893 static void dm_softirq_done(struct request
*rq
)
896 struct request
*clone
= rq
->completion_data
;
897 struct dm_rq_target_io
*tio
= clone
->end_io_data
;
899 if (rq
->cmd_flags
& REQ_FAILED
)
902 dm_done(clone
, tio
->error
, mapped
);
906 * Complete the clone and the original request with the error status
907 * through softirq context.
909 static void dm_complete_request(struct request
*clone
, int error
)
911 struct dm_rq_target_io
*tio
= clone
->end_io_data
;
912 struct request
*rq
= tio
->orig
;
915 rq
->completion_data
= clone
;
916 blk_complete_request(rq
);
920 * Complete the not-mapped clone and the original request with the error status
921 * through softirq context.
922 * Target's rq_end_io() function isn't called.
923 * This may be used when the target's map_rq() function fails.
925 void dm_kill_unmapped_request(struct request
*clone
, int error
)
927 struct dm_rq_target_io
*tio
= clone
->end_io_data
;
928 struct request
*rq
= tio
->orig
;
930 rq
->cmd_flags
|= REQ_FAILED
;
931 dm_complete_request(clone
, error
);
933 EXPORT_SYMBOL_GPL(dm_kill_unmapped_request
);
936 * Called with the queue lock held
938 static void end_clone_request(struct request
*clone
, int error
)
941 * For just cleaning up the information of the queue in which
942 * the clone was dispatched.
943 * The clone is *NOT* freed actually here because it is alloced from
944 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
946 __blk_put_request(clone
->q
, clone
);
949 * Actual request completion is done in a softirq context which doesn't
950 * hold the queue lock. Otherwise, deadlock could occur because:
951 * - another request may be submitted by the upper level driver
952 * of the stacking during the completion
953 * - the submission which requires queue lock may be done
956 dm_complete_request(clone
, error
);
960 * Return maximum size of I/O possible at the supplied sector up to the current
963 static sector_t
max_io_len_target_boundary(sector_t sector
, struct dm_target
*ti
)
965 sector_t target_offset
= dm_target_offset(ti
, sector
);
967 return ti
->len
- target_offset
;
970 static sector_t
max_io_len(sector_t sector
, struct dm_target
*ti
)
972 sector_t len
= max_io_len_target_boundary(sector
, ti
);
975 * Does the target need to split even further ?
979 sector_t offset
= dm_target_offset(ti
, sector
);
980 boundary
= ((offset
+ ti
->split_io
) & ~(ti
->split_io
- 1))
989 static void __map_bio(struct dm_target
*ti
, struct bio
*clone
,
990 struct dm_target_io
*tio
)
994 struct mapped_device
*md
;
996 clone
->bi_end_io
= clone_endio
;
997 clone
->bi_private
= tio
;
1000 * Map the clone. If r == 0 we don't need to do
1001 * anything, the target has assumed ownership of
1004 atomic_inc(&tio
->io
->io_count
);
1005 sector
= clone
->bi_sector
;
1006 r
= ti
->type
->map(ti
, clone
, &tio
->info
);
1007 if (r
== DM_MAPIO_REMAPPED
) {
1008 /* the bio has been remapped so dispatch it */
1010 trace_block_bio_remap(bdev_get_queue(clone
->bi_bdev
), clone
,
1011 tio
->io
->bio
->bi_bdev
->bd_dev
, sector
);
1013 generic_make_request(clone
);
1014 } else if (r
< 0 || r
== DM_MAPIO_REQUEUE
) {
1015 /* error the io and bail out, or requeue it if needed */
1017 dec_pending(tio
->io
, r
);
1019 * Store bio_set for cleanup.
1021 clone
->bi_private
= md
->bs
;
1025 DMWARN("unimplemented target map return value: %d", r
);
1031 struct mapped_device
*md
;
1032 struct dm_table
*map
;
1036 sector_t sector_count
;
1040 static void dm_bio_destructor(struct bio
*bio
)
1042 struct bio_set
*bs
= bio
->bi_private
;
1048 * Creates a little bio that just does part of a bvec.
1050 static struct bio
*split_bvec(struct bio
*bio
, sector_t sector
,
1051 unsigned short idx
, unsigned int offset
,
1052 unsigned int len
, struct bio_set
*bs
)
1055 struct bio_vec
*bv
= bio
->bi_io_vec
+ idx
;
1057 clone
= bio_alloc_bioset(GFP_NOIO
, 1, bs
);
1058 clone
->bi_destructor
= dm_bio_destructor
;
1059 *clone
->bi_io_vec
= *bv
;
1061 clone
->bi_sector
= sector
;
1062 clone
->bi_bdev
= bio
->bi_bdev
;
1063 clone
->bi_rw
= bio
->bi_rw
;
1065 clone
->bi_size
= to_bytes(len
);
1066 clone
->bi_io_vec
->bv_offset
= offset
;
1067 clone
->bi_io_vec
->bv_len
= clone
->bi_size
;
1068 clone
->bi_flags
|= 1 << BIO_CLONED
;
1070 if (bio_integrity(bio
)) {
1071 bio_integrity_clone(clone
, bio
, GFP_NOIO
, bs
);
1072 bio_integrity_trim(clone
,
1073 bio_sector_offset(bio
, idx
, offset
), len
);
1080 * Creates a bio that consists of range of complete bvecs.
1082 static struct bio
*clone_bio(struct bio
*bio
, sector_t sector
,
1083 unsigned short idx
, unsigned short bv_count
,
1084 unsigned int len
, struct bio_set
*bs
)
1088 clone
= bio_alloc_bioset(GFP_NOIO
, bio
->bi_max_vecs
, bs
);
1089 __bio_clone(clone
, bio
);
1090 clone
->bi_destructor
= dm_bio_destructor
;
1091 clone
->bi_sector
= sector
;
1092 clone
->bi_idx
= idx
;
1093 clone
->bi_vcnt
= idx
+ bv_count
;
1094 clone
->bi_size
= to_bytes(len
);
1095 clone
->bi_flags
&= ~(1 << BIO_SEG_VALID
);
1097 if (bio_integrity(bio
)) {
1098 bio_integrity_clone(clone
, bio
, GFP_NOIO
, bs
);
1100 if (idx
!= bio
->bi_idx
|| clone
->bi_size
< bio
->bi_size
)
1101 bio_integrity_trim(clone
,
1102 bio_sector_offset(bio
, idx
, 0), len
);
1108 static struct dm_target_io
*alloc_tio(struct clone_info
*ci
,
1109 struct dm_target
*ti
)
1111 struct dm_target_io
*tio
= mempool_alloc(ci
->md
->tio_pool
, GFP_NOIO
);
1115 memset(&tio
->info
, 0, sizeof(tio
->info
));
1120 static void __issue_target_request(struct clone_info
*ci
, struct dm_target
*ti
,
1121 unsigned request_nr
, sector_t len
)
1123 struct dm_target_io
*tio
= alloc_tio(ci
, ti
);
1126 tio
->info
.target_request_nr
= request_nr
;
1129 * Discard requests require the bio's inline iovecs be initialized.
1130 * ci->bio->bi_max_vecs is BIO_INLINE_VECS anyway, for both flush
1131 * and discard, so no need for concern about wasted bvec allocations.
1133 clone
= bio_alloc_bioset(GFP_NOIO
, ci
->bio
->bi_max_vecs
, ci
->md
->bs
);
1134 __bio_clone(clone
, ci
->bio
);
1135 clone
->bi_destructor
= dm_bio_destructor
;
1137 clone
->bi_sector
= ci
->sector
;
1138 clone
->bi_size
= to_bytes(len
);
1141 __map_bio(ti
, clone
, tio
);
1144 static void __issue_target_requests(struct clone_info
*ci
, struct dm_target
*ti
,
1145 unsigned num_requests
, sector_t len
)
1147 unsigned request_nr
;
1149 for (request_nr
= 0; request_nr
< num_requests
; request_nr
++)
1150 __issue_target_request(ci
, ti
, request_nr
, len
);
1153 static int __clone_and_map_empty_flush(struct clone_info
*ci
)
1155 unsigned target_nr
= 0;
1156 struct dm_target
*ti
;
1158 BUG_ON(bio_has_data(ci
->bio
));
1159 while ((ti
= dm_table_get_target(ci
->map
, target_nr
++)))
1160 __issue_target_requests(ci
, ti
, ti
->num_flush_requests
, 0);
1166 * Perform all io with a single clone.
1168 static void __clone_and_map_simple(struct clone_info
*ci
, struct dm_target
*ti
)
1170 struct bio
*clone
, *bio
= ci
->bio
;
1171 struct dm_target_io
*tio
;
1173 tio
= alloc_tio(ci
, ti
);
1174 clone
= clone_bio(bio
, ci
->sector
, ci
->idx
,
1175 bio
->bi_vcnt
- ci
->idx
, ci
->sector_count
,
1177 __map_bio(ti
, clone
, tio
);
1178 ci
->sector_count
= 0;
1181 static int __clone_and_map_discard(struct clone_info
*ci
)
1183 struct dm_target
*ti
;
1187 ti
= dm_table_find_target(ci
->map
, ci
->sector
);
1188 if (!dm_target_is_valid(ti
))
1192 * Even though the device advertised discard support,
1193 * that does not mean every target supports it, and
1194 * reconfiguration might also have changed that since the
1195 * check was performed.
1197 if (!ti
->num_discard_requests
)
1200 len
= min(ci
->sector_count
, max_io_len_target_boundary(ci
->sector
, ti
));
1202 __issue_target_requests(ci
, ti
, ti
->num_discard_requests
, len
);
1205 } while (ci
->sector_count
-= len
);
1210 static int __clone_and_map(struct clone_info
*ci
)
1212 struct bio
*clone
, *bio
= ci
->bio
;
1213 struct dm_target
*ti
;
1214 sector_t len
= 0, max
;
1215 struct dm_target_io
*tio
;
1217 if (unlikely(bio
->bi_rw
& REQ_DISCARD
))
1218 return __clone_and_map_discard(ci
);
1220 ti
= dm_table_find_target(ci
->map
, ci
->sector
);
1221 if (!dm_target_is_valid(ti
))
1224 max
= max_io_len(ci
->sector
, ti
);
1226 if (ci
->sector_count
<= max
) {
1228 * Optimise for the simple case where we can do all of
1229 * the remaining io with a single clone.
1231 __clone_and_map_simple(ci
, ti
);
1233 } else if (to_sector(bio
->bi_io_vec
[ci
->idx
].bv_len
) <= max
) {
1235 * There are some bvecs that don't span targets.
1236 * Do as many of these as possible.
1239 sector_t remaining
= max
;
1242 for (i
= ci
->idx
; remaining
&& (i
< bio
->bi_vcnt
); i
++) {
1243 bv_len
= to_sector(bio
->bi_io_vec
[i
].bv_len
);
1245 if (bv_len
> remaining
)
1248 remaining
-= bv_len
;
1252 tio
= alloc_tio(ci
, ti
);
1253 clone
= clone_bio(bio
, ci
->sector
, ci
->idx
, i
- ci
->idx
, len
,
1255 __map_bio(ti
, clone
, tio
);
1258 ci
->sector_count
-= len
;
1263 * Handle a bvec that must be split between two or more targets.
1265 struct bio_vec
*bv
= bio
->bi_io_vec
+ ci
->idx
;
1266 sector_t remaining
= to_sector(bv
->bv_len
);
1267 unsigned int offset
= 0;
1271 ti
= dm_table_find_target(ci
->map
, ci
->sector
);
1272 if (!dm_target_is_valid(ti
))
1275 max
= max_io_len(ci
->sector
, ti
);
1278 len
= min(remaining
, max
);
1280 tio
= alloc_tio(ci
, ti
);
1281 clone
= split_bvec(bio
, ci
->sector
, ci
->idx
,
1282 bv
->bv_offset
+ offset
, len
,
1285 __map_bio(ti
, clone
, tio
);
1288 ci
->sector_count
-= len
;
1289 offset
+= to_bytes(len
);
1290 } while (remaining
-= len
);
1299 * Split the bio into several clones and submit it to targets.
1301 static void __split_and_process_bio(struct mapped_device
*md
, struct bio
*bio
)
1303 struct clone_info ci
;
1306 ci
.map
= dm_get_live_table(md
);
1307 if (unlikely(!ci
.map
)) {
1313 ci
.io
= alloc_io(md
);
1315 atomic_set(&ci
.io
->io_count
, 1);
1318 spin_lock_init(&ci
.io
->endio_lock
);
1319 ci
.sector
= bio
->bi_sector
;
1320 ci
.idx
= bio
->bi_idx
;
1322 start_io_acct(ci
.io
);
1323 if (bio
->bi_rw
& REQ_FLUSH
) {
1324 ci
.bio
= &ci
.md
->flush_bio
;
1325 ci
.sector_count
= 0;
1326 error
= __clone_and_map_empty_flush(&ci
);
1327 /* dec_pending submits any data associated with flush */
1330 ci
.sector_count
= bio_sectors(bio
);
1331 while (ci
.sector_count
&& !error
)
1332 error
= __clone_and_map(&ci
);
1335 /* drop the extra reference count */
1336 dec_pending(ci
.io
, error
);
1337 dm_table_put(ci
.map
);
1339 /*-----------------------------------------------------------------
1341 *---------------------------------------------------------------*/
1343 static int dm_merge_bvec(struct request_queue
*q
,
1344 struct bvec_merge_data
*bvm
,
1345 struct bio_vec
*biovec
)
1347 struct mapped_device
*md
= q
->queuedata
;
1348 struct dm_table
*map
= dm_get_live_table(md
);
1349 struct dm_target
*ti
;
1350 sector_t max_sectors
;
1356 ti
= dm_table_find_target(map
, bvm
->bi_sector
);
1357 if (!dm_target_is_valid(ti
))
1361 * Find maximum amount of I/O that won't need splitting
1363 max_sectors
= min(max_io_len(bvm
->bi_sector
, ti
),
1364 (sector_t
) BIO_MAX_SECTORS
);
1365 max_size
= (max_sectors
<< SECTOR_SHIFT
) - bvm
->bi_size
;
1370 * merge_bvec_fn() returns number of bytes
1371 * it can accept at this offset
1372 * max is precomputed maximal io size
1374 if (max_size
&& ti
->type
->merge
)
1375 max_size
= ti
->type
->merge(ti
, bvm
, biovec
, max_size
);
1377 * If the target doesn't support merge method and some of the devices
1378 * provided their merge_bvec method (we know this by looking at
1379 * queue_max_hw_sectors), then we can't allow bios with multiple vector
1380 * entries. So always set max_size to 0, and the code below allows
1383 else if (queue_max_hw_sectors(q
) <= PAGE_SIZE
>> 9)
1392 * Always allow an entire first page
1394 if (max_size
<= biovec
->bv_len
&& !(bvm
->bi_size
>> SECTOR_SHIFT
))
1395 max_size
= biovec
->bv_len
;
1401 * The request function that just remaps the bio built up by
1404 static int _dm_request(struct request_queue
*q
, struct bio
*bio
)
1406 int rw
= bio_data_dir(bio
);
1407 struct mapped_device
*md
= q
->queuedata
;
1410 down_read(&md
->io_lock
);
1412 cpu
= part_stat_lock();
1413 part_stat_inc(cpu
, &dm_disk(md
)->part0
, ios
[rw
]);
1414 part_stat_add(cpu
, &dm_disk(md
)->part0
, sectors
[rw
], bio_sectors(bio
));
1417 /* if we're suspended, we have to queue this io for later */
1418 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND
, &md
->flags
))) {
1419 up_read(&md
->io_lock
);
1421 if (bio_rw(bio
) != READA
)
1428 __split_and_process_bio(md
, bio
);
1429 up_read(&md
->io_lock
);
1433 static int dm_make_request(struct request_queue
*q
, struct bio
*bio
)
1435 struct mapped_device
*md
= q
->queuedata
;
1437 return md
->saved_make_request_fn(q
, bio
); /* call __make_request() */
1440 static int dm_request_based(struct mapped_device
*md
)
1442 return blk_queue_stackable(md
->queue
);
1445 static int dm_request(struct request_queue
*q
, struct bio
*bio
)
1447 struct mapped_device
*md
= q
->queuedata
;
1449 if (dm_request_based(md
))
1450 return dm_make_request(q
, bio
);
1452 return _dm_request(q
, bio
);
1455 void dm_dispatch_request(struct request
*rq
)
1459 if (blk_queue_io_stat(rq
->q
))
1460 rq
->cmd_flags
|= REQ_IO_STAT
;
1462 rq
->start_time
= jiffies
;
1463 r
= blk_insert_cloned_request(rq
->q
, rq
);
1465 dm_complete_request(rq
, r
);
1467 EXPORT_SYMBOL_GPL(dm_dispatch_request
);
1469 static void dm_rq_bio_destructor(struct bio
*bio
)
1471 struct dm_rq_clone_bio_info
*info
= bio
->bi_private
;
1472 struct mapped_device
*md
= info
->tio
->md
;
1474 free_bio_info(info
);
1475 bio_free(bio
, md
->bs
);
1478 static int dm_rq_bio_constructor(struct bio
*bio
, struct bio
*bio_orig
,
1481 struct dm_rq_target_io
*tio
= data
;
1482 struct mapped_device
*md
= tio
->md
;
1483 struct dm_rq_clone_bio_info
*info
= alloc_bio_info(md
);
1488 info
->orig
= bio_orig
;
1490 bio
->bi_end_io
= end_clone_bio
;
1491 bio
->bi_private
= info
;
1492 bio
->bi_destructor
= dm_rq_bio_destructor
;
1497 static int setup_clone(struct request
*clone
, struct request
*rq
,
1498 struct dm_rq_target_io
*tio
)
1502 r
= blk_rq_prep_clone(clone
, rq
, tio
->md
->bs
, GFP_ATOMIC
,
1503 dm_rq_bio_constructor
, tio
);
1507 clone
->cmd
= rq
->cmd
;
1508 clone
->cmd_len
= rq
->cmd_len
;
1509 clone
->sense
= rq
->sense
;
1510 clone
->buffer
= rq
->buffer
;
1511 clone
->end_io
= end_clone_request
;
1512 clone
->end_io_data
= tio
;
1517 static struct request
*clone_rq(struct request
*rq
, struct mapped_device
*md
,
1520 struct request
*clone
;
1521 struct dm_rq_target_io
*tio
;
1523 tio
= alloc_rq_tio(md
, gfp_mask
);
1531 memset(&tio
->info
, 0, sizeof(tio
->info
));
1533 clone
= &tio
->clone
;
1534 if (setup_clone(clone
, rq
, tio
)) {
1544 * Called with the queue lock held.
1546 static int dm_prep_fn(struct request_queue
*q
, struct request
*rq
)
1548 struct mapped_device
*md
= q
->queuedata
;
1549 struct request
*clone
;
1551 if (unlikely(rq
->special
)) {
1552 DMWARN("Already has something in rq->special.");
1553 return BLKPREP_KILL
;
1556 clone
= clone_rq(rq
, md
, GFP_ATOMIC
);
1558 return BLKPREP_DEFER
;
1560 rq
->special
= clone
;
1561 rq
->cmd_flags
|= REQ_DONTPREP
;
1568 * 0 : the request has been processed (not requeued)
1569 * !0 : the request has been requeued
1571 static int map_request(struct dm_target
*ti
, struct request
*clone
,
1572 struct mapped_device
*md
)
1574 int r
, requeued
= 0;
1575 struct dm_rq_target_io
*tio
= clone
->end_io_data
;
1578 * Hold the md reference here for the in-flight I/O.
1579 * We can't rely on the reference count by device opener,
1580 * because the device may be closed during the request completion
1581 * when all bios are completed.
1582 * See the comment in rq_completed() too.
1587 r
= ti
->type
->map_rq(ti
, clone
, &tio
->info
);
1589 case DM_MAPIO_SUBMITTED
:
1590 /* The target has taken the I/O to submit by itself later */
1592 case DM_MAPIO_REMAPPED
:
1593 /* The target has remapped the I/O so dispatch it */
1594 trace_block_rq_remap(clone
->q
, clone
, disk_devt(dm_disk(md
)),
1595 blk_rq_pos(tio
->orig
));
1596 dm_dispatch_request(clone
);
1598 case DM_MAPIO_REQUEUE
:
1599 /* The target wants to requeue the I/O */
1600 dm_requeue_unmapped_request(clone
);
1605 DMWARN("unimplemented target map return value: %d", r
);
1609 /* The target wants to complete the I/O */
1610 dm_kill_unmapped_request(clone
, r
);
1618 * q->request_fn for request-based dm.
1619 * Called with the queue lock held.
1621 static void dm_request_fn(struct request_queue
*q
)
1623 struct mapped_device
*md
= q
->queuedata
;
1624 struct dm_table
*map
= dm_get_live_table(md
);
1625 struct dm_target
*ti
;
1626 struct request
*rq
, *clone
;
1630 * For suspend, check blk_queue_stopped() and increment
1631 * ->pending within a single queue_lock not to increment the
1632 * number of in-flight I/Os after the queue is stopped in
1635 while (!blk_queue_stopped(q
)) {
1636 rq
= blk_peek_request(q
);
1640 /* always use block 0 to find the target for flushes for now */
1642 if (!(rq
->cmd_flags
& REQ_FLUSH
))
1643 pos
= blk_rq_pos(rq
);
1645 ti
= dm_table_find_target(map
, pos
);
1646 BUG_ON(!dm_target_is_valid(ti
));
1648 if (ti
->type
->busy
&& ti
->type
->busy(ti
))
1651 blk_start_request(rq
);
1652 clone
= rq
->special
;
1653 atomic_inc(&md
->pending
[rq_data_dir(clone
)]);
1655 spin_unlock(q
->queue_lock
);
1656 if (map_request(ti
, clone
, md
))
1659 BUG_ON(!irqs_disabled());
1660 spin_lock(q
->queue_lock
);
1666 BUG_ON(!irqs_disabled());
1667 spin_lock(q
->queue_lock
);
1670 blk_delay_queue(q
, HZ
/ 10);
1677 int dm_underlying_device_busy(struct request_queue
*q
)
1679 return blk_lld_busy(q
);
1681 EXPORT_SYMBOL_GPL(dm_underlying_device_busy
);
1683 static int dm_lld_busy(struct request_queue
*q
)
1686 struct mapped_device
*md
= q
->queuedata
;
1687 struct dm_table
*map
= dm_get_live_table(md
);
1689 if (!map
|| test_bit(DMF_BLOCK_IO_FOR_SUSPEND
, &md
->flags
))
1692 r
= dm_table_any_busy_target(map
);
1699 static int dm_any_congested(void *congested_data
, int bdi_bits
)
1702 struct mapped_device
*md
= congested_data
;
1703 struct dm_table
*map
;
1705 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND
, &md
->flags
)) {
1706 map
= dm_get_live_table(md
);
1709 * Request-based dm cares about only own queue for
1710 * the query about congestion status of request_queue
1712 if (dm_request_based(md
))
1713 r
= md
->queue
->backing_dev_info
.state
&
1716 r
= dm_table_any_congested(map
, bdi_bits
);
1725 /*-----------------------------------------------------------------
1726 * An IDR is used to keep track of allocated minor numbers.
1727 *---------------------------------------------------------------*/
1728 static void free_minor(int minor
)
1730 spin_lock(&_minor_lock
);
1731 idr_remove(&_minor_idr
, minor
);
1732 spin_unlock(&_minor_lock
);
1736 * See if the device with a specific minor # is free.
1738 static int specific_minor(int minor
)
1742 if (minor
>= (1 << MINORBITS
))
1745 r
= idr_pre_get(&_minor_idr
, GFP_KERNEL
);
1749 spin_lock(&_minor_lock
);
1751 if (idr_find(&_minor_idr
, minor
)) {
1756 r
= idr_get_new_above(&_minor_idr
, MINOR_ALLOCED
, minor
, &m
);
1761 idr_remove(&_minor_idr
, m
);
1767 spin_unlock(&_minor_lock
);
1771 static int next_free_minor(int *minor
)
1775 r
= idr_pre_get(&_minor_idr
, GFP_KERNEL
);
1779 spin_lock(&_minor_lock
);
1781 r
= idr_get_new(&_minor_idr
, MINOR_ALLOCED
, &m
);
1785 if (m
>= (1 << MINORBITS
)) {
1786 idr_remove(&_minor_idr
, m
);
1794 spin_unlock(&_minor_lock
);
1798 static const struct block_device_operations dm_blk_dops
;
1800 static void dm_wq_work(struct work_struct
*work
);
1802 static void dm_init_md_queue(struct mapped_device
*md
)
1805 * Request-based dm devices cannot be stacked on top of bio-based dm
1806 * devices. The type of this dm device has not been decided yet.
1807 * The type is decided at the first table loading time.
1808 * To prevent problematic device stacking, clear the queue flag
1809 * for request stacking support until then.
1811 * This queue is new, so no concurrency on the queue_flags.
1813 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE
, md
->queue
);
1815 md
->queue
->queuedata
= md
;
1816 md
->queue
->backing_dev_info
.congested_fn
= dm_any_congested
;
1817 md
->queue
->backing_dev_info
.congested_data
= md
;
1818 blk_queue_make_request(md
->queue
, dm_request
);
1819 blk_queue_bounce_limit(md
->queue
, BLK_BOUNCE_ANY
);
1820 blk_queue_merge_bvec(md
->queue
, dm_merge_bvec
);
1824 * Allocate and initialise a blank device with a given minor.
1826 static struct mapped_device
*alloc_dev(int minor
)
1829 struct mapped_device
*md
= kzalloc(sizeof(*md
), GFP_KERNEL
);
1833 DMWARN("unable to allocate device, out of memory.");
1837 if (!try_module_get(THIS_MODULE
))
1838 goto bad_module_get
;
1840 /* get a minor number for the dev */
1841 if (minor
== DM_ANY_MINOR
)
1842 r
= next_free_minor(&minor
);
1844 r
= specific_minor(minor
);
1848 md
->type
= DM_TYPE_NONE
;
1849 init_rwsem(&md
->io_lock
);
1850 mutex_init(&md
->suspend_lock
);
1851 mutex_init(&md
->type_lock
);
1852 spin_lock_init(&md
->deferred_lock
);
1853 rwlock_init(&md
->map_lock
);
1854 atomic_set(&md
->holders
, 1);
1855 atomic_set(&md
->open_count
, 0);
1856 atomic_set(&md
->event_nr
, 0);
1857 atomic_set(&md
->uevent_seq
, 0);
1858 INIT_LIST_HEAD(&md
->uevent_list
);
1859 spin_lock_init(&md
->uevent_lock
);
1861 md
->queue
= blk_alloc_queue(GFP_KERNEL
);
1865 dm_init_md_queue(md
);
1867 md
->disk
= alloc_disk(1);
1871 atomic_set(&md
->pending
[0], 0);
1872 atomic_set(&md
->pending
[1], 0);
1873 init_waitqueue_head(&md
->wait
);
1874 INIT_WORK(&md
->work
, dm_wq_work
);
1875 init_waitqueue_head(&md
->eventq
);
1877 md
->disk
->major
= _major
;
1878 md
->disk
->first_minor
= minor
;
1879 md
->disk
->fops
= &dm_blk_dops
;
1880 md
->disk
->queue
= md
->queue
;
1881 md
->disk
->private_data
= md
;
1882 sprintf(md
->disk
->disk_name
, "dm-%d", minor
);
1884 format_dev_t(md
->name
, MKDEV(_major
, minor
));
1886 md
->wq
= alloc_workqueue("kdmflush",
1887 WQ_NON_REENTRANT
| WQ_MEM_RECLAIM
, 0);
1891 md
->bdev
= bdget_disk(md
->disk
, 0);
1895 bio_init(&md
->flush_bio
);
1896 md
->flush_bio
.bi_bdev
= md
->bdev
;
1897 md
->flush_bio
.bi_rw
= WRITE_FLUSH
;
1899 /* Populate the mapping, nobody knows we exist yet */
1900 spin_lock(&_minor_lock
);
1901 old_md
= idr_replace(&_minor_idr
, md
, minor
);
1902 spin_unlock(&_minor_lock
);
1904 BUG_ON(old_md
!= MINOR_ALLOCED
);
1909 destroy_workqueue(md
->wq
);
1911 del_gendisk(md
->disk
);
1914 blk_cleanup_queue(md
->queue
);
1918 module_put(THIS_MODULE
);
1924 static void unlock_fs(struct mapped_device
*md
);
1926 static void free_dev(struct mapped_device
*md
)
1928 int minor
= MINOR(disk_devt(md
->disk
));
1932 destroy_workqueue(md
->wq
);
1934 mempool_destroy(md
->tio_pool
);
1936 mempool_destroy(md
->io_pool
);
1938 bioset_free(md
->bs
);
1939 blk_integrity_unregister(md
->disk
);
1940 del_gendisk(md
->disk
);
1943 spin_lock(&_minor_lock
);
1944 md
->disk
->private_data
= NULL
;
1945 spin_unlock(&_minor_lock
);
1948 blk_cleanup_queue(md
->queue
);
1949 module_put(THIS_MODULE
);
1953 static void __bind_mempools(struct mapped_device
*md
, struct dm_table
*t
)
1955 struct dm_md_mempools
*p
;
1957 if (md
->io_pool
&& md
->tio_pool
&& md
->bs
)
1958 /* the md already has necessary mempools */
1961 p
= dm_table_get_md_mempools(t
);
1962 BUG_ON(!p
|| md
->io_pool
|| md
->tio_pool
|| md
->bs
);
1964 md
->io_pool
= p
->io_pool
;
1966 md
->tio_pool
= p
->tio_pool
;
1972 /* mempool bind completed, now no need any mempools in the table */
1973 dm_table_free_md_mempools(t
);
1977 * Bind a table to the device.
1979 static void event_callback(void *context
)
1981 unsigned long flags
;
1983 struct mapped_device
*md
= (struct mapped_device
*) context
;
1985 spin_lock_irqsave(&md
->uevent_lock
, flags
);
1986 list_splice_init(&md
->uevent_list
, &uevents
);
1987 spin_unlock_irqrestore(&md
->uevent_lock
, flags
);
1989 dm_send_uevents(&uevents
, &disk_to_dev(md
->disk
)->kobj
);
1991 atomic_inc(&md
->event_nr
);
1992 wake_up(&md
->eventq
);
1996 * Protected by md->suspend_lock obtained by dm_swap_table().
1998 static void __set_size(struct mapped_device
*md
, sector_t size
)
2000 set_capacity(md
->disk
, size
);
2002 i_size_write(md
->bdev
->bd_inode
, (loff_t
)size
<< SECTOR_SHIFT
);
2006 * Return 1 if the queue has a compulsory merge_bvec_fn function.
2008 * If this function returns 0, then the device is either a non-dm
2009 * device without a merge_bvec_fn, or it is a dm device that is
2010 * able to split any bios it receives that are too big.
2012 int dm_queue_merge_is_compulsory(struct request_queue
*q
)
2014 struct mapped_device
*dev_md
;
2016 if (!q
->merge_bvec_fn
)
2019 if (q
->make_request_fn
== dm_request
) {
2020 dev_md
= q
->queuedata
;
2021 if (test_bit(DMF_MERGE_IS_OPTIONAL
, &dev_md
->flags
))
2028 static int dm_device_merge_is_compulsory(struct dm_target
*ti
,
2029 struct dm_dev
*dev
, sector_t start
,
2030 sector_t len
, void *data
)
2032 struct block_device
*bdev
= dev
->bdev
;
2033 struct request_queue
*q
= bdev_get_queue(bdev
);
2035 return dm_queue_merge_is_compulsory(q
);
2039 * Return 1 if it is acceptable to ignore merge_bvec_fn based
2040 * on the properties of the underlying devices.
2042 static int dm_table_merge_is_optional(struct dm_table
*table
)
2045 struct dm_target
*ti
;
2047 while (i
< dm_table_get_num_targets(table
)) {
2048 ti
= dm_table_get_target(table
, i
++);
2050 if (ti
->type
->iterate_devices
&&
2051 ti
->type
->iterate_devices(ti
, dm_device_merge_is_compulsory
, NULL
))
2059 * Returns old map, which caller must destroy.
2061 static struct dm_table
*__bind(struct mapped_device
*md
, struct dm_table
*t
,
2062 struct queue_limits
*limits
)
2064 struct dm_table
*old_map
;
2065 struct request_queue
*q
= md
->queue
;
2067 unsigned long flags
;
2068 int merge_is_optional
;
2070 size
= dm_table_get_size(t
);
2073 * Wipe any geometry if the size of the table changed.
2075 if (size
!= get_capacity(md
->disk
))
2076 memset(&md
->geometry
, 0, sizeof(md
->geometry
));
2078 __set_size(md
, size
);
2080 dm_table_event_callback(t
, event_callback
, md
);
2083 * The queue hasn't been stopped yet, if the old table type wasn't
2084 * for request-based during suspension. So stop it to prevent
2085 * I/O mapping before resume.
2086 * This must be done before setting the queue restrictions,
2087 * because request-based dm may be run just after the setting.
2089 if (dm_table_request_based(t
) && !blk_queue_stopped(q
))
2092 __bind_mempools(md
, t
);
2094 merge_is_optional
= dm_table_merge_is_optional(t
);
2096 write_lock_irqsave(&md
->map_lock
, flags
);
2099 dm_table_set_restrictions(t
, q
, limits
);
2100 if (merge_is_optional
)
2101 set_bit(DMF_MERGE_IS_OPTIONAL
, &md
->flags
);
2103 clear_bit(DMF_MERGE_IS_OPTIONAL
, &md
->flags
);
2104 write_unlock_irqrestore(&md
->map_lock
, flags
);
2110 * Returns unbound table for the caller to free.
2112 static struct dm_table
*__unbind(struct mapped_device
*md
)
2114 struct dm_table
*map
= md
->map
;
2115 unsigned long flags
;
2120 dm_table_event_callback(map
, NULL
, NULL
);
2121 write_lock_irqsave(&md
->map_lock
, flags
);
2123 write_unlock_irqrestore(&md
->map_lock
, flags
);
2129 * Constructor for a new device.
2131 int dm_create(int minor
, struct mapped_device
**result
)
2133 struct mapped_device
*md
;
2135 md
= alloc_dev(minor
);
2146 * Functions to manage md->type.
2147 * All are required to hold md->type_lock.
2149 void dm_lock_md_type(struct mapped_device
*md
)
2151 mutex_lock(&md
->type_lock
);
2154 void dm_unlock_md_type(struct mapped_device
*md
)
2156 mutex_unlock(&md
->type_lock
);
2159 void dm_set_md_type(struct mapped_device
*md
, unsigned type
)
2164 unsigned dm_get_md_type(struct mapped_device
*md
)
2170 * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
2172 static int dm_init_request_based_queue(struct mapped_device
*md
)
2174 struct request_queue
*q
= NULL
;
2176 if (md
->queue
->elevator
)
2179 /* Fully initialize the queue */
2180 q
= blk_init_allocated_queue(md
->queue
, dm_request_fn
, NULL
);
2185 md
->saved_make_request_fn
= md
->queue
->make_request_fn
;
2186 dm_init_md_queue(md
);
2187 blk_queue_softirq_done(md
->queue
, dm_softirq_done
);
2188 blk_queue_prep_rq(md
->queue
, dm_prep_fn
);
2189 blk_queue_lld_busy(md
->queue
, dm_lld_busy
);
2191 elv_register_queue(md
->queue
);
2197 * Setup the DM device's queue based on md's type
2199 int dm_setup_md_queue(struct mapped_device
*md
)
2201 if ((dm_get_md_type(md
) == DM_TYPE_REQUEST_BASED
) &&
2202 !dm_init_request_based_queue(md
)) {
2203 DMWARN("Cannot initialize queue for request-based mapped device");
2210 static struct mapped_device
*dm_find_md(dev_t dev
)
2212 struct mapped_device
*md
;
2213 unsigned minor
= MINOR(dev
);
2215 if (MAJOR(dev
) != _major
|| minor
>= (1 << MINORBITS
))
2218 spin_lock(&_minor_lock
);
2220 md
= idr_find(&_minor_idr
, minor
);
2221 if (md
&& (md
== MINOR_ALLOCED
||
2222 (MINOR(disk_devt(dm_disk(md
))) != minor
) ||
2223 dm_deleting_md(md
) ||
2224 test_bit(DMF_FREEING
, &md
->flags
))) {
2230 spin_unlock(&_minor_lock
);
2235 struct mapped_device
*dm_get_md(dev_t dev
)
2237 struct mapped_device
*md
= dm_find_md(dev
);
2244 EXPORT_SYMBOL_GPL(dm_get_md
);
2246 void *dm_get_mdptr(struct mapped_device
*md
)
2248 return md
->interface_ptr
;
2251 void dm_set_mdptr(struct mapped_device
*md
, void *ptr
)
2253 md
->interface_ptr
= ptr
;
2256 void dm_get(struct mapped_device
*md
)
2258 atomic_inc(&md
->holders
);
2259 BUG_ON(test_bit(DMF_FREEING
, &md
->flags
));
2262 const char *dm_device_name(struct mapped_device
*md
)
2266 EXPORT_SYMBOL_GPL(dm_device_name
);
2268 static void __dm_destroy(struct mapped_device
*md
, bool wait
)
2270 struct dm_table
*map
;
2274 spin_lock(&_minor_lock
);
2275 map
= dm_get_live_table(md
);
2276 idr_replace(&_minor_idr
, MINOR_ALLOCED
, MINOR(disk_devt(dm_disk(md
))));
2277 set_bit(DMF_FREEING
, &md
->flags
);
2278 spin_unlock(&_minor_lock
);
2280 if (!dm_suspended_md(md
)) {
2281 dm_table_presuspend_targets(map
);
2282 dm_table_postsuspend_targets(map
);
2286 * Rare, but there may be I/O requests still going to complete,
2287 * for example. Wait for all references to disappear.
2288 * No one should increment the reference count of the mapped_device,
2289 * after the mapped_device state becomes DMF_FREEING.
2292 while (atomic_read(&md
->holders
))
2294 else if (atomic_read(&md
->holders
))
2295 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2296 dm_device_name(md
), atomic_read(&md
->holders
));
2300 dm_table_destroy(__unbind(md
));
2304 void dm_destroy(struct mapped_device
*md
)
2306 __dm_destroy(md
, true);
2309 void dm_destroy_immediate(struct mapped_device
*md
)
2311 __dm_destroy(md
, false);
2314 void dm_put(struct mapped_device
*md
)
2316 atomic_dec(&md
->holders
);
2318 EXPORT_SYMBOL_GPL(dm_put
);
2320 static int dm_wait_for_completion(struct mapped_device
*md
, int interruptible
)
2323 DECLARE_WAITQUEUE(wait
, current
);
2325 add_wait_queue(&md
->wait
, &wait
);
2328 set_current_state(interruptible
);
2330 if (!md_in_flight(md
))
2333 if (interruptible
== TASK_INTERRUPTIBLE
&&
2334 signal_pending(current
)) {
2341 set_current_state(TASK_RUNNING
);
2343 remove_wait_queue(&md
->wait
, &wait
);
2349 * Process the deferred bios
2351 static void dm_wq_work(struct work_struct
*work
)
2353 struct mapped_device
*md
= container_of(work
, struct mapped_device
,
2357 down_read(&md
->io_lock
);
2359 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND
, &md
->flags
)) {
2360 spin_lock_irq(&md
->deferred_lock
);
2361 c
= bio_list_pop(&md
->deferred
);
2362 spin_unlock_irq(&md
->deferred_lock
);
2367 up_read(&md
->io_lock
);
2369 if (dm_request_based(md
))
2370 generic_make_request(c
);
2372 __split_and_process_bio(md
, c
);
2374 down_read(&md
->io_lock
);
2377 up_read(&md
->io_lock
);
2380 static void dm_queue_flush(struct mapped_device
*md
)
2382 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND
, &md
->flags
);
2383 smp_mb__after_clear_bit();
2384 queue_work(md
->wq
, &md
->work
);
2388 * Swap in a new table, returning the old one for the caller to destroy.
2390 struct dm_table
*dm_swap_table(struct mapped_device
*md
, struct dm_table
*table
)
2392 struct dm_table
*map
= ERR_PTR(-EINVAL
);
2393 struct queue_limits limits
;
2396 mutex_lock(&md
->suspend_lock
);
2398 /* device must be suspended */
2399 if (!dm_suspended_md(md
))
2402 r
= dm_calculate_queue_limits(table
, &limits
);
2408 map
= __bind(md
, table
, &limits
);
2411 mutex_unlock(&md
->suspend_lock
);
2416 * Functions to lock and unlock any filesystem running on the
2419 static int lock_fs(struct mapped_device
*md
)
2423 WARN_ON(md
->frozen_sb
);
2425 md
->frozen_sb
= freeze_bdev(md
->bdev
);
2426 if (IS_ERR(md
->frozen_sb
)) {
2427 r
= PTR_ERR(md
->frozen_sb
);
2428 md
->frozen_sb
= NULL
;
2432 set_bit(DMF_FROZEN
, &md
->flags
);
2437 static void unlock_fs(struct mapped_device
*md
)
2439 if (!test_bit(DMF_FROZEN
, &md
->flags
))
2442 thaw_bdev(md
->bdev
, md
->frozen_sb
);
2443 md
->frozen_sb
= NULL
;
2444 clear_bit(DMF_FROZEN
, &md
->flags
);
2448 * We need to be able to change a mapping table under a mounted
2449 * filesystem. For example we might want to move some data in
2450 * the background. Before the table can be swapped with
2451 * dm_bind_table, dm_suspend must be called to flush any in
2452 * flight bios and ensure that any further io gets deferred.
2455 * Suspend mechanism in request-based dm.
2457 * 1. Flush all I/Os by lock_fs() if needed.
2458 * 2. Stop dispatching any I/O by stopping the request_queue.
2459 * 3. Wait for all in-flight I/Os to be completed or requeued.
2461 * To abort suspend, start the request_queue.
2463 int dm_suspend(struct mapped_device
*md
, unsigned suspend_flags
)
2465 struct dm_table
*map
= NULL
;
2467 int do_lockfs
= suspend_flags
& DM_SUSPEND_LOCKFS_FLAG
? 1 : 0;
2468 int noflush
= suspend_flags
& DM_SUSPEND_NOFLUSH_FLAG
? 1 : 0;
2470 mutex_lock(&md
->suspend_lock
);
2472 if (dm_suspended_md(md
)) {
2477 map
= dm_get_live_table(md
);
2480 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2481 * This flag is cleared before dm_suspend returns.
2484 set_bit(DMF_NOFLUSH_SUSPENDING
, &md
->flags
);
2486 /* This does not get reverted if there's an error later. */
2487 dm_table_presuspend_targets(map
);
2490 * Flush I/O to the device.
2491 * Any I/O submitted after lock_fs() may not be flushed.
2492 * noflush takes precedence over do_lockfs.
2493 * (lock_fs() flushes I/Os and waits for them to complete.)
2495 if (!noflush
&& do_lockfs
) {
2502 * Here we must make sure that no processes are submitting requests
2503 * to target drivers i.e. no one may be executing
2504 * __split_and_process_bio. This is called from dm_request and
2507 * To get all processes out of __split_and_process_bio in dm_request,
2508 * we take the write lock. To prevent any process from reentering
2509 * __split_and_process_bio from dm_request and quiesce the thread
2510 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
2511 * flush_workqueue(md->wq).
2513 down_write(&md
->io_lock
);
2514 set_bit(DMF_BLOCK_IO_FOR_SUSPEND
, &md
->flags
);
2515 up_write(&md
->io_lock
);
2518 * Stop md->queue before flushing md->wq in case request-based
2519 * dm defers requests to md->wq from md->queue.
2521 if (dm_request_based(md
))
2522 stop_queue(md
->queue
);
2524 flush_workqueue(md
->wq
);
2527 * At this point no more requests are entering target request routines.
2528 * We call dm_wait_for_completion to wait for all existing requests
2531 r
= dm_wait_for_completion(md
, TASK_INTERRUPTIBLE
);
2533 down_write(&md
->io_lock
);
2535 clear_bit(DMF_NOFLUSH_SUSPENDING
, &md
->flags
);
2536 up_write(&md
->io_lock
);
2538 /* were we interrupted ? */
2542 if (dm_request_based(md
))
2543 start_queue(md
->queue
);
2546 goto out
; /* pushback list is already flushed, so skip flush */
2550 * If dm_wait_for_completion returned 0, the device is completely
2551 * quiescent now. There is no request-processing activity. All new
2552 * requests are being added to md->deferred list.
2555 set_bit(DMF_SUSPENDED
, &md
->flags
);
2557 dm_table_postsuspend_targets(map
);
2563 mutex_unlock(&md
->suspend_lock
);
2567 int dm_resume(struct mapped_device
*md
)
2570 struct dm_table
*map
= NULL
;
2572 mutex_lock(&md
->suspend_lock
);
2573 if (!dm_suspended_md(md
))
2576 map
= dm_get_live_table(md
);
2577 if (!map
|| !dm_table_get_size(map
))
2580 r
= dm_table_resume_targets(map
);
2587 * Flushing deferred I/Os must be done after targets are resumed
2588 * so that mapping of targets can work correctly.
2589 * Request-based dm is queueing the deferred I/Os in its request_queue.
2591 if (dm_request_based(md
))
2592 start_queue(md
->queue
);
2596 clear_bit(DMF_SUSPENDED
, &md
->flags
);
2601 mutex_unlock(&md
->suspend_lock
);
2606 /*-----------------------------------------------------------------
2607 * Event notification.
2608 *---------------------------------------------------------------*/
2609 int dm_kobject_uevent(struct mapped_device
*md
, enum kobject_action action
,
2612 char udev_cookie
[DM_COOKIE_LENGTH
];
2613 char *envp
[] = { udev_cookie
, NULL
};
2616 return kobject_uevent(&disk_to_dev(md
->disk
)->kobj
, action
);
2618 snprintf(udev_cookie
, DM_COOKIE_LENGTH
, "%s=%u",
2619 DM_COOKIE_ENV_VAR_NAME
, cookie
);
2620 return kobject_uevent_env(&disk_to_dev(md
->disk
)->kobj
,
2625 uint32_t dm_next_uevent_seq(struct mapped_device
*md
)
2627 return atomic_add_return(1, &md
->uevent_seq
);
2630 uint32_t dm_get_event_nr(struct mapped_device
*md
)
2632 return atomic_read(&md
->event_nr
);
2635 int dm_wait_event(struct mapped_device
*md
, int event_nr
)
2637 return wait_event_interruptible(md
->eventq
,
2638 (event_nr
!= atomic_read(&md
->event_nr
)));
2641 void dm_uevent_add(struct mapped_device
*md
, struct list_head
*elist
)
2643 unsigned long flags
;
2645 spin_lock_irqsave(&md
->uevent_lock
, flags
);
2646 list_add(elist
, &md
->uevent_list
);
2647 spin_unlock_irqrestore(&md
->uevent_lock
, flags
);
2651 * The gendisk is only valid as long as you have a reference
2654 struct gendisk
*dm_disk(struct mapped_device
*md
)
2659 struct kobject
*dm_kobject(struct mapped_device
*md
)
2665 * struct mapped_device should not be exported outside of dm.c
2666 * so use this check to verify that kobj is part of md structure
2668 struct mapped_device
*dm_get_from_kobject(struct kobject
*kobj
)
2670 struct mapped_device
*md
;
2672 md
= container_of(kobj
, struct mapped_device
, kobj
);
2673 if (&md
->kobj
!= kobj
)
2676 if (test_bit(DMF_FREEING
, &md
->flags
) ||
2684 int dm_suspended_md(struct mapped_device
*md
)
2686 return test_bit(DMF_SUSPENDED
, &md
->flags
);
2689 int dm_suspended(struct dm_target
*ti
)
2691 return dm_suspended_md(dm_table_get_md(ti
->table
));
2693 EXPORT_SYMBOL_GPL(dm_suspended
);
2695 int dm_noflush_suspending(struct dm_target
*ti
)
2697 return __noflush_suspending(dm_table_get_md(ti
->table
));
2699 EXPORT_SYMBOL_GPL(dm_noflush_suspending
);
2701 struct dm_md_mempools
*dm_alloc_md_mempools(unsigned type
, unsigned integrity
)
2703 struct dm_md_mempools
*pools
= kmalloc(sizeof(*pools
), GFP_KERNEL
);
2704 unsigned int pool_size
= (type
== DM_TYPE_BIO_BASED
) ? 16 : MIN_IOS
;
2709 pools
->io_pool
= (type
== DM_TYPE_BIO_BASED
) ?
2710 mempool_create_slab_pool(MIN_IOS
, _io_cache
) :
2711 mempool_create_slab_pool(MIN_IOS
, _rq_bio_info_cache
);
2712 if (!pools
->io_pool
)
2713 goto free_pools_and_out
;
2715 pools
->tio_pool
= (type
== DM_TYPE_BIO_BASED
) ?
2716 mempool_create_slab_pool(MIN_IOS
, _tio_cache
) :
2717 mempool_create_slab_pool(MIN_IOS
, _rq_tio_cache
);
2718 if (!pools
->tio_pool
)
2719 goto free_io_pool_and_out
;
2721 pools
->bs
= bioset_create(pool_size
, 0);
2723 goto free_tio_pool_and_out
;
2725 if (integrity
&& bioset_integrity_create(pools
->bs
, pool_size
))
2726 goto free_bioset_and_out
;
2730 free_bioset_and_out
:
2731 bioset_free(pools
->bs
);
2733 free_tio_pool_and_out
:
2734 mempool_destroy(pools
->tio_pool
);
2736 free_io_pool_and_out
:
2737 mempool_destroy(pools
->io_pool
);
2745 void dm_free_md_mempools(struct dm_md_mempools
*pools
)
2751 mempool_destroy(pools
->io_pool
);
2753 if (pools
->tio_pool
)
2754 mempool_destroy(pools
->tio_pool
);
2757 bioset_free(pools
->bs
);
2762 static const struct block_device_operations dm_blk_dops
= {
2763 .open
= dm_blk_open
,
2764 .release
= dm_blk_close
,
2765 .ioctl
= dm_blk_ioctl
,
2766 .getgeo
= dm_blk_getgeo
,
2767 .owner
= THIS_MODULE
2770 EXPORT_SYMBOL(dm_get_mapinfo
);
2775 module_init(dm_init
);
2776 module_exit(dm_exit
);
2778 module_param(major
, uint
, 0);
2779 MODULE_PARM_DESC(major
, "The major number of the device mapper");
2780 MODULE_DESCRIPTION(DM_NAME
" driver");
2781 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2782 MODULE_LICENSE("GPL");