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>
23 #include <trace/events/block.h>
25 #define DM_MSG_PREFIX "core"
28 * Cookies are numeric values sent with CHANGE and REMOVE
29 * uevents while resuming, removing or renaming the device.
31 #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
32 #define DM_COOKIE_LENGTH 24
34 static const char *_name
= DM_NAME
;
36 static unsigned int major
= 0;
37 static unsigned int _major
= 0;
39 static DEFINE_SPINLOCK(_minor_lock
);
42 * One of these is allocated per bio.
45 struct mapped_device
*md
;
49 unsigned long start_time
;
54 * One of these is allocated per target within a bio. Hopefully
55 * this will be simplified out one day.
64 * For request-based dm.
65 * One of these is allocated per request.
67 struct dm_rq_target_io
{
68 struct mapped_device
*md
;
70 struct request
*orig
, clone
;
76 * For request-based dm.
77 * One of these is allocated per bio.
79 struct dm_rq_clone_bio_info
{
81 struct dm_rq_target_io
*tio
;
84 union map_info
*dm_get_mapinfo(struct bio
*bio
)
86 if (bio
&& bio
->bi_private
)
87 return &((struct dm_target_io
*)bio
->bi_private
)->info
;
91 union map_info
*dm_get_rq_mapinfo(struct request
*rq
)
93 if (rq
&& rq
->end_io_data
)
94 return &((struct dm_rq_target_io
*)rq
->end_io_data
)->info
;
97 EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo
);
99 #define MINOR_ALLOCED ((void *)-1)
102 * Bits for the md->flags field.
104 #define DMF_BLOCK_IO_FOR_SUSPEND 0
105 #define DMF_SUSPENDED 1
107 #define DMF_FREEING 3
108 #define DMF_DELETING 4
109 #define DMF_NOFLUSH_SUSPENDING 5
110 #define DMF_QUEUE_IO_TO_THREAD 6
113 * Work processed by per-device workqueue.
115 struct mapped_device
{
116 struct rw_semaphore io_lock
;
117 struct mutex suspend_lock
;
124 struct request_queue
*queue
;
125 struct gendisk
*disk
;
131 * A list of ios that arrived while we were suspended.
134 wait_queue_head_t wait
;
135 struct work_struct work
;
136 struct bio_list deferred
;
137 spinlock_t deferred_lock
;
140 * An error from the barrier request currently being processed.
145 * Processing queue (flush/barriers)
147 struct workqueue_struct
*wq
;
150 * The current mapping.
152 struct dm_table
*map
;
155 * io objects are allocated from here.
166 wait_queue_head_t eventq
;
168 struct list_head uevent_list
;
169 spinlock_t uevent_lock
; /* Protect access to uevent_list */
172 * freeze/thaw support require holding onto a super block
174 struct super_block
*frozen_sb
;
175 struct block_device
*bdev
;
177 /* forced geometry settings */
178 struct hd_geometry geometry
;
180 /* marker of flush suspend for request-based dm */
181 struct request suspend_rq
;
183 /* For saving the address of __make_request for request based dm */
184 make_request_fn
*saved_make_request_fn
;
189 /* zero-length barrier that will be cloned and submitted to targets */
190 struct bio barrier_bio
;
194 * For mempools pre-allocation at the table loading time.
196 struct dm_md_mempools
{
203 static struct kmem_cache
*_io_cache
;
204 static struct kmem_cache
*_tio_cache
;
205 static struct kmem_cache
*_rq_tio_cache
;
206 static struct kmem_cache
*_rq_bio_info_cache
;
208 static int __init
local_init(void)
212 /* allocate a slab for the dm_ios */
213 _io_cache
= KMEM_CACHE(dm_io
, 0);
217 /* allocate a slab for the target ios */
218 _tio_cache
= KMEM_CACHE(dm_target_io
, 0);
220 goto out_free_io_cache
;
222 _rq_tio_cache
= KMEM_CACHE(dm_rq_target_io
, 0);
224 goto out_free_tio_cache
;
226 _rq_bio_info_cache
= KMEM_CACHE(dm_rq_clone_bio_info
, 0);
227 if (!_rq_bio_info_cache
)
228 goto out_free_rq_tio_cache
;
230 r
= dm_uevent_init();
232 goto out_free_rq_bio_info_cache
;
235 r
= register_blkdev(_major
, _name
);
237 goto out_uevent_exit
;
246 out_free_rq_bio_info_cache
:
247 kmem_cache_destroy(_rq_bio_info_cache
);
248 out_free_rq_tio_cache
:
249 kmem_cache_destroy(_rq_tio_cache
);
251 kmem_cache_destroy(_tio_cache
);
253 kmem_cache_destroy(_io_cache
);
258 static void local_exit(void)
260 kmem_cache_destroy(_rq_bio_info_cache
);
261 kmem_cache_destroy(_rq_tio_cache
);
262 kmem_cache_destroy(_tio_cache
);
263 kmem_cache_destroy(_io_cache
);
264 unregister_blkdev(_major
, _name
);
269 DMINFO("cleaned up");
272 static int (*_inits
[])(void) __initdata
= {
281 static void (*_exits
[])(void) = {
290 static int __init
dm_init(void)
292 const int count
= ARRAY_SIZE(_inits
);
296 for (i
= 0; i
< count
; i
++) {
311 static void __exit
dm_exit(void)
313 int i
= ARRAY_SIZE(_exits
);
320 * Block device functions
322 static int dm_blk_open(struct block_device
*bdev
, fmode_t mode
)
324 struct mapped_device
*md
;
326 spin_lock(&_minor_lock
);
328 md
= bdev
->bd_disk
->private_data
;
332 if (test_bit(DMF_FREEING
, &md
->flags
) ||
333 test_bit(DMF_DELETING
, &md
->flags
)) {
339 atomic_inc(&md
->open_count
);
342 spin_unlock(&_minor_lock
);
344 return md
? 0 : -ENXIO
;
347 static int dm_blk_close(struct gendisk
*disk
, fmode_t mode
)
349 struct mapped_device
*md
= disk
->private_data
;
350 atomic_dec(&md
->open_count
);
355 int dm_open_count(struct mapped_device
*md
)
357 return atomic_read(&md
->open_count
);
361 * Guarantees nothing is using the device before it's deleted.
363 int dm_lock_for_deletion(struct mapped_device
*md
)
367 spin_lock(&_minor_lock
);
369 if (dm_open_count(md
))
372 set_bit(DMF_DELETING
, &md
->flags
);
374 spin_unlock(&_minor_lock
);
379 static int dm_blk_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
381 struct mapped_device
*md
= bdev
->bd_disk
->private_data
;
383 return dm_get_geometry(md
, geo
);
386 static int dm_blk_ioctl(struct block_device
*bdev
, fmode_t mode
,
387 unsigned int cmd
, unsigned long arg
)
389 struct mapped_device
*md
= bdev
->bd_disk
->private_data
;
390 struct dm_table
*map
= dm_get_table(md
);
391 struct dm_target
*tgt
;
394 if (!map
|| !dm_table_get_size(map
))
397 /* We only support devices that have a single target */
398 if (dm_table_get_num_targets(map
) != 1)
401 tgt
= dm_table_get_target(map
, 0);
403 if (dm_suspended(md
)) {
408 if (tgt
->type
->ioctl
)
409 r
= tgt
->type
->ioctl(tgt
, cmd
, arg
);
417 static struct dm_io
*alloc_io(struct mapped_device
*md
)
419 return mempool_alloc(md
->io_pool
, GFP_NOIO
);
422 static void free_io(struct mapped_device
*md
, struct dm_io
*io
)
424 mempool_free(io
, md
->io_pool
);
427 static void free_tio(struct mapped_device
*md
, struct dm_target_io
*tio
)
429 mempool_free(tio
, md
->tio_pool
);
432 static struct dm_rq_target_io
*alloc_rq_tio(struct mapped_device
*md
)
434 return mempool_alloc(md
->tio_pool
, GFP_ATOMIC
);
437 static void free_rq_tio(struct dm_rq_target_io
*tio
)
439 mempool_free(tio
, tio
->md
->tio_pool
);
442 static struct dm_rq_clone_bio_info
*alloc_bio_info(struct mapped_device
*md
)
444 return mempool_alloc(md
->io_pool
, GFP_ATOMIC
);
447 static void free_bio_info(struct dm_rq_clone_bio_info
*info
)
449 mempool_free(info
, info
->tio
->md
->io_pool
);
452 static void start_io_acct(struct dm_io
*io
)
454 struct mapped_device
*md
= io
->md
;
457 io
->start_time
= jiffies
;
459 cpu
= part_stat_lock();
460 part_round_stats(cpu
, &dm_disk(md
)->part0
);
462 dm_disk(md
)->part0
.in_flight
= atomic_inc_return(&md
->pending
);
465 static void end_io_acct(struct dm_io
*io
)
467 struct mapped_device
*md
= io
->md
;
468 struct bio
*bio
= io
->bio
;
469 unsigned long duration
= jiffies
- io
->start_time
;
471 int rw
= bio_data_dir(bio
);
473 cpu
= part_stat_lock();
474 part_round_stats(cpu
, &dm_disk(md
)->part0
);
475 part_stat_add(cpu
, &dm_disk(md
)->part0
, ticks
[rw
], duration
);
479 * After this is decremented the bio must not be touched if it is
482 dm_disk(md
)->part0
.in_flight
= pending
=
483 atomic_dec_return(&md
->pending
);
485 /* nudge anyone waiting on suspend queue */
491 * Add the bio to the list of deferred io.
493 static void queue_io(struct mapped_device
*md
, struct bio
*bio
)
495 down_write(&md
->io_lock
);
497 spin_lock_irq(&md
->deferred_lock
);
498 bio_list_add(&md
->deferred
, bio
);
499 spin_unlock_irq(&md
->deferred_lock
);
501 if (!test_and_set_bit(DMF_QUEUE_IO_TO_THREAD
, &md
->flags
))
502 queue_work(md
->wq
, &md
->work
);
504 up_write(&md
->io_lock
);
508 * Everyone (including functions in this file), should use this
509 * function to access the md->map field, and make sure they call
510 * dm_table_put() when finished.
512 struct dm_table
*dm_get_table(struct mapped_device
*md
)
517 read_lock_irqsave(&md
->map_lock
, flags
);
521 read_unlock_irqrestore(&md
->map_lock
, flags
);
527 * Get the geometry associated with a dm device
529 int dm_get_geometry(struct mapped_device
*md
, struct hd_geometry
*geo
)
537 * Set the geometry of a device.
539 int dm_set_geometry(struct mapped_device
*md
, struct hd_geometry
*geo
)
541 sector_t sz
= (sector_t
)geo
->cylinders
* geo
->heads
* geo
->sectors
;
543 if (geo
->start
> sz
) {
544 DMWARN("Start sector is beyond the geometry limits.");
553 /*-----------------------------------------------------------------
555 * A more elegant soln is in the works that uses the queue
556 * merge fn, unfortunately there are a couple of changes to
557 * the block layer that I want to make for this. So in the
558 * interests of getting something for people to use I give
559 * you this clearly demarcated crap.
560 *---------------------------------------------------------------*/
562 static int __noflush_suspending(struct mapped_device
*md
)
564 return test_bit(DMF_NOFLUSH_SUSPENDING
, &md
->flags
);
568 * Decrements the number of outstanding ios that a bio has been
569 * cloned into, completing the original io if necc.
571 static void dec_pending(struct dm_io
*io
, int error
)
576 struct mapped_device
*md
= io
->md
;
578 /* Push-back supersedes any I/O errors */
579 if (error
&& !(io
->error
> 0 && __noflush_suspending(md
)))
582 if (atomic_dec_and_test(&io
->io_count
)) {
583 if (io
->error
== DM_ENDIO_REQUEUE
) {
585 * Target requested pushing back the I/O.
587 spin_lock_irqsave(&md
->deferred_lock
, flags
);
588 if (__noflush_suspending(md
)) {
589 if (!bio_barrier(io
->bio
))
590 bio_list_add_head(&md
->deferred
,
593 /* noflush suspend was interrupted. */
595 spin_unlock_irqrestore(&md
->deferred_lock
, flags
);
598 io_error
= io
->error
;
601 if (bio_barrier(bio
)) {
603 * There can be just one barrier request so we use
604 * a per-device variable for error reporting.
605 * Note that you can't touch the bio after end_io_acct
607 if (!md
->barrier_error
&& io_error
!= -EOPNOTSUPP
)
608 md
->barrier_error
= io_error
;
613 if (io_error
!= DM_ENDIO_REQUEUE
) {
614 trace_block_bio_complete(md
->queue
, bio
);
616 bio_endio(bio
, io_error
);
624 static void clone_endio(struct bio
*bio
, int error
)
627 struct dm_target_io
*tio
= bio
->bi_private
;
628 struct dm_io
*io
= tio
->io
;
629 struct mapped_device
*md
= tio
->io
->md
;
630 dm_endio_fn endio
= tio
->ti
->type
->end_io
;
632 if (!bio_flagged(bio
, BIO_UPTODATE
) && !error
)
636 r
= endio(tio
->ti
, bio
, error
, &tio
->info
);
637 if (r
< 0 || r
== DM_ENDIO_REQUEUE
)
639 * error and requeue request are handled
643 else if (r
== DM_ENDIO_INCOMPLETE
)
644 /* The target will handle the io */
647 DMWARN("unimplemented target endio return value: %d", r
);
653 * Store md for cleanup instead of tio which is about to get freed.
655 bio
->bi_private
= md
->bs
;
659 dec_pending(io
, error
);
663 * Partial completion handling for request-based dm
665 static void end_clone_bio(struct bio
*clone
, int error
)
667 struct dm_rq_clone_bio_info
*info
= clone
->bi_private
;
668 struct dm_rq_target_io
*tio
= info
->tio
;
669 struct bio
*bio
= info
->orig
;
670 unsigned int nr_bytes
= info
->orig
->bi_size
;
676 * An error has already been detected on the request.
677 * Once error occurred, just let clone->end_io() handle
683 * Don't notice the error to the upper layer yet.
684 * The error handling decision is made by the target driver,
685 * when the request is completed.
692 * I/O for the bio successfully completed.
693 * Notice the data completion to the upper layer.
697 * bios are processed from the head of the list.
698 * So the completing bio should always be rq->bio.
699 * If it's not, something wrong is happening.
701 if (tio
->orig
->bio
!= bio
)
702 DMERR("bio completion is going in the middle of the request");
705 * Update the original request.
706 * Do not use blk_end_request() here, because it may complete
707 * the original request before the clone, and break the ordering.
709 blk_update_request(tio
->orig
, 0, nr_bytes
);
713 * Don't touch any member of the md after calling this function because
714 * the md may be freed in dm_put() at the end of this function.
715 * Or do dm_get() before calling this function and dm_put() later.
717 static void rq_completed(struct mapped_device
*md
, int run_queue
)
719 int wakeup_waiters
= 0;
720 struct request_queue
*q
= md
->queue
;
723 spin_lock_irqsave(q
->queue_lock
, flags
);
724 if (!queue_in_flight(q
))
726 spin_unlock_irqrestore(q
->queue_lock
, flags
);
728 /* nudge anyone waiting on suspend queue */
736 * dm_put() must be at the end of this function. See the comment above
741 static void dm_unprep_request(struct request
*rq
)
743 struct request
*clone
= rq
->special
;
744 struct dm_rq_target_io
*tio
= clone
->end_io_data
;
747 rq
->cmd_flags
&= ~REQ_DONTPREP
;
749 blk_rq_unprep_clone(clone
);
754 * Requeue the original request of a clone.
756 void dm_requeue_unmapped_request(struct request
*clone
)
758 struct dm_rq_target_io
*tio
= clone
->end_io_data
;
759 struct mapped_device
*md
= tio
->md
;
760 struct request
*rq
= tio
->orig
;
761 struct request_queue
*q
= rq
->q
;
764 dm_unprep_request(rq
);
766 spin_lock_irqsave(q
->queue_lock
, flags
);
767 if (elv_queue_empty(q
))
769 blk_requeue_request(q
, rq
);
770 spin_unlock_irqrestore(q
->queue_lock
, flags
);
774 EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request
);
776 static void __stop_queue(struct request_queue
*q
)
781 static void stop_queue(struct request_queue
*q
)
785 spin_lock_irqsave(q
->queue_lock
, flags
);
787 spin_unlock_irqrestore(q
->queue_lock
, flags
);
790 static void __start_queue(struct request_queue
*q
)
792 if (blk_queue_stopped(q
))
796 static void start_queue(struct request_queue
*q
)
800 spin_lock_irqsave(q
->queue_lock
, flags
);
802 spin_unlock_irqrestore(q
->queue_lock
, flags
);
806 * Complete the clone and the original request.
807 * Must be called without queue lock.
809 static void dm_end_request(struct request
*clone
, int error
)
811 struct dm_rq_target_io
*tio
= clone
->end_io_data
;
812 struct mapped_device
*md
= tio
->md
;
813 struct request
*rq
= tio
->orig
;
815 if (blk_pc_request(rq
)) {
816 rq
->errors
= clone
->errors
;
817 rq
->resid_len
= clone
->resid_len
;
821 * We are using the sense buffer of the original
823 * So setting the length of the sense data is enough.
825 rq
->sense_len
= clone
->sense_len
;
831 blk_end_request_all(rq
, error
);
837 * Request completion handler for request-based dm
839 static void dm_softirq_done(struct request
*rq
)
841 struct request
*clone
= rq
->completion_data
;
842 struct dm_rq_target_io
*tio
= clone
->end_io_data
;
843 dm_request_endio_fn rq_end_io
= tio
->ti
->type
->rq_end_io
;
844 int error
= tio
->error
;
846 if (!(rq
->cmd_flags
& REQ_FAILED
) && rq_end_io
)
847 error
= rq_end_io(tio
->ti
, clone
, error
, &tio
->info
);
850 /* The target wants to complete the I/O */
851 dm_end_request(clone
, error
);
852 else if (error
== DM_ENDIO_INCOMPLETE
)
853 /* The target will handle the I/O */
855 else if (error
== DM_ENDIO_REQUEUE
)
856 /* The target wants to requeue the I/O */
857 dm_requeue_unmapped_request(clone
);
859 DMWARN("unimplemented target endio return value: %d", error
);
865 * Complete the clone and the original request with the error status
866 * through softirq context.
868 static void dm_complete_request(struct request
*clone
, int error
)
870 struct dm_rq_target_io
*tio
= clone
->end_io_data
;
871 struct request
*rq
= tio
->orig
;
874 rq
->completion_data
= clone
;
875 blk_complete_request(rq
);
879 * Complete the not-mapped clone and the original request with the error status
880 * through softirq context.
881 * Target's rq_end_io() function isn't called.
882 * This may be used when the target's map_rq() function fails.
884 void dm_kill_unmapped_request(struct request
*clone
, int error
)
886 struct dm_rq_target_io
*tio
= clone
->end_io_data
;
887 struct request
*rq
= tio
->orig
;
889 rq
->cmd_flags
|= REQ_FAILED
;
890 dm_complete_request(clone
, error
);
892 EXPORT_SYMBOL_GPL(dm_kill_unmapped_request
);
895 * Called with the queue lock held
897 static void end_clone_request(struct request
*clone
, int error
)
900 * For just cleaning up the information of the queue in which
901 * the clone was dispatched.
902 * The clone is *NOT* freed actually here because it is alloced from
903 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
905 __blk_put_request(clone
->q
, clone
);
908 * Actual request completion is done in a softirq context which doesn't
909 * hold the queue lock. Otherwise, deadlock could occur because:
910 * - another request may be submitted by the upper level driver
911 * of the stacking during the completion
912 * - the submission which requires queue lock may be done
915 dm_complete_request(clone
, error
);
918 static sector_t
max_io_len(struct mapped_device
*md
,
919 sector_t sector
, struct dm_target
*ti
)
921 sector_t offset
= sector
- ti
->begin
;
922 sector_t len
= ti
->len
- offset
;
925 * Does the target need to split even further ?
929 boundary
= ((offset
+ ti
->split_io
) & ~(ti
->split_io
- 1))
938 static void __map_bio(struct dm_target
*ti
, struct bio
*clone
,
939 struct dm_target_io
*tio
)
943 struct mapped_device
*md
;
945 clone
->bi_end_io
= clone_endio
;
946 clone
->bi_private
= tio
;
949 * Map the clone. If r == 0 we don't need to do
950 * anything, the target has assumed ownership of
953 atomic_inc(&tio
->io
->io_count
);
954 sector
= clone
->bi_sector
;
955 r
= ti
->type
->map(ti
, clone
, &tio
->info
);
956 if (r
== DM_MAPIO_REMAPPED
) {
957 /* the bio has been remapped so dispatch it */
959 trace_block_remap(bdev_get_queue(clone
->bi_bdev
), clone
,
960 tio
->io
->bio
->bi_bdev
->bd_dev
, sector
);
962 generic_make_request(clone
);
963 } else if (r
< 0 || r
== DM_MAPIO_REQUEUE
) {
964 /* error the io and bail out, or requeue it if needed */
966 dec_pending(tio
->io
, r
);
968 * Store bio_set for cleanup.
970 clone
->bi_private
= md
->bs
;
974 DMWARN("unimplemented target map return value: %d", r
);
980 struct mapped_device
*md
;
981 struct dm_table
*map
;
985 sector_t sector_count
;
989 static void dm_bio_destructor(struct bio
*bio
)
991 struct bio_set
*bs
= bio
->bi_private
;
997 * Creates a little bio that is just does part of a bvec.
999 static struct bio
*split_bvec(struct bio
*bio
, sector_t sector
,
1000 unsigned short idx
, unsigned int offset
,
1001 unsigned int len
, struct bio_set
*bs
)
1004 struct bio_vec
*bv
= bio
->bi_io_vec
+ idx
;
1006 clone
= bio_alloc_bioset(GFP_NOIO
, 1, bs
);
1007 clone
->bi_destructor
= dm_bio_destructor
;
1008 *clone
->bi_io_vec
= *bv
;
1010 clone
->bi_sector
= sector
;
1011 clone
->bi_bdev
= bio
->bi_bdev
;
1012 clone
->bi_rw
= bio
->bi_rw
& ~(1 << BIO_RW_BARRIER
);
1014 clone
->bi_size
= to_bytes(len
);
1015 clone
->bi_io_vec
->bv_offset
= offset
;
1016 clone
->bi_io_vec
->bv_len
= clone
->bi_size
;
1017 clone
->bi_flags
|= 1 << BIO_CLONED
;
1019 if (bio_integrity(bio
)) {
1020 bio_integrity_clone(clone
, bio
, GFP_NOIO
, bs
);
1021 bio_integrity_trim(clone
,
1022 bio_sector_offset(bio
, idx
, offset
), len
);
1029 * Creates a bio that consists of range of complete bvecs.
1031 static struct bio
*clone_bio(struct bio
*bio
, sector_t sector
,
1032 unsigned short idx
, unsigned short bv_count
,
1033 unsigned int len
, struct bio_set
*bs
)
1037 clone
= bio_alloc_bioset(GFP_NOIO
, bio
->bi_max_vecs
, bs
);
1038 __bio_clone(clone
, bio
);
1039 clone
->bi_rw
&= ~(1 << BIO_RW_BARRIER
);
1040 clone
->bi_destructor
= dm_bio_destructor
;
1041 clone
->bi_sector
= sector
;
1042 clone
->bi_idx
= idx
;
1043 clone
->bi_vcnt
= idx
+ bv_count
;
1044 clone
->bi_size
= to_bytes(len
);
1045 clone
->bi_flags
&= ~(1 << BIO_SEG_VALID
);
1047 if (bio_integrity(bio
)) {
1048 bio_integrity_clone(clone
, bio
, GFP_NOIO
, bs
);
1050 if (idx
!= bio
->bi_idx
|| clone
->bi_size
< bio
->bi_size
)
1051 bio_integrity_trim(clone
,
1052 bio_sector_offset(bio
, idx
, 0), len
);
1058 static struct dm_target_io
*alloc_tio(struct clone_info
*ci
,
1059 struct dm_target
*ti
)
1061 struct dm_target_io
*tio
= mempool_alloc(ci
->md
->tio_pool
, GFP_NOIO
);
1065 memset(&tio
->info
, 0, sizeof(tio
->info
));
1070 static void __flush_target(struct clone_info
*ci
, struct dm_target
*ti
,
1073 struct dm_target_io
*tio
= alloc_tio(ci
, ti
);
1076 tio
->info
.flush_request
= flush_nr
;
1078 clone
= bio_alloc_bioset(GFP_NOIO
, 0, ci
->md
->bs
);
1079 __bio_clone(clone
, ci
->bio
);
1080 clone
->bi_destructor
= dm_bio_destructor
;
1082 __map_bio(ti
, clone
, tio
);
1085 static int __clone_and_map_empty_barrier(struct clone_info
*ci
)
1087 unsigned target_nr
= 0, flush_nr
;
1088 struct dm_target
*ti
;
1090 while ((ti
= dm_table_get_target(ci
->map
, target_nr
++)))
1091 for (flush_nr
= 0; flush_nr
< ti
->num_flush_requests
;
1093 __flush_target(ci
, ti
, flush_nr
);
1095 ci
->sector_count
= 0;
1100 static int __clone_and_map(struct clone_info
*ci
)
1102 struct bio
*clone
, *bio
= ci
->bio
;
1103 struct dm_target
*ti
;
1104 sector_t len
= 0, max
;
1105 struct dm_target_io
*tio
;
1107 if (unlikely(bio_empty_barrier(bio
)))
1108 return __clone_and_map_empty_barrier(ci
);
1110 ti
= dm_table_find_target(ci
->map
, ci
->sector
);
1111 if (!dm_target_is_valid(ti
))
1114 max
= max_io_len(ci
->md
, ci
->sector
, ti
);
1117 * Allocate a target io object.
1119 tio
= alloc_tio(ci
, ti
);
1121 if (ci
->sector_count
<= max
) {
1123 * Optimise for the simple case where we can do all of
1124 * the remaining io with a single clone.
1126 clone
= clone_bio(bio
, ci
->sector
, ci
->idx
,
1127 bio
->bi_vcnt
- ci
->idx
, ci
->sector_count
,
1129 __map_bio(ti
, clone
, tio
);
1130 ci
->sector_count
= 0;
1132 } else if (to_sector(bio
->bi_io_vec
[ci
->idx
].bv_len
) <= max
) {
1134 * There are some bvecs that don't span targets.
1135 * Do as many of these as possible.
1138 sector_t remaining
= max
;
1141 for (i
= ci
->idx
; remaining
&& (i
< bio
->bi_vcnt
); i
++) {
1142 bv_len
= to_sector(bio
->bi_io_vec
[i
].bv_len
);
1144 if (bv_len
> remaining
)
1147 remaining
-= bv_len
;
1151 clone
= clone_bio(bio
, ci
->sector
, ci
->idx
, i
- ci
->idx
, len
,
1153 __map_bio(ti
, clone
, tio
);
1156 ci
->sector_count
-= len
;
1161 * Handle a bvec that must be split between two or more targets.
1163 struct bio_vec
*bv
= bio
->bi_io_vec
+ ci
->idx
;
1164 sector_t remaining
= to_sector(bv
->bv_len
);
1165 unsigned int offset
= 0;
1169 ti
= dm_table_find_target(ci
->map
, ci
->sector
);
1170 if (!dm_target_is_valid(ti
))
1173 max
= max_io_len(ci
->md
, ci
->sector
, ti
);
1175 tio
= alloc_tio(ci
, ti
);
1178 len
= min(remaining
, max
);
1180 clone
= split_bvec(bio
, ci
->sector
, ci
->idx
,
1181 bv
->bv_offset
+ offset
, len
,
1184 __map_bio(ti
, clone
, tio
);
1187 ci
->sector_count
-= len
;
1188 offset
+= to_bytes(len
);
1189 } while (remaining
-= len
);
1198 * Split the bio into several clones and submit it to targets.
1200 static void __split_and_process_bio(struct mapped_device
*md
, struct bio
*bio
)
1202 struct clone_info ci
;
1205 ci
.map
= dm_get_table(md
);
1206 if (unlikely(!ci
.map
)) {
1207 if (!bio_barrier(bio
))
1210 if (!md
->barrier_error
)
1211 md
->barrier_error
= -EIO
;
1217 ci
.io
= alloc_io(md
);
1219 atomic_set(&ci
.io
->io_count
, 1);
1222 ci
.sector
= bio
->bi_sector
;
1223 ci
.sector_count
= bio_sectors(bio
);
1224 if (unlikely(bio_empty_barrier(bio
)))
1225 ci
.sector_count
= 1;
1226 ci
.idx
= bio
->bi_idx
;
1228 start_io_acct(ci
.io
);
1229 while (ci
.sector_count
&& !error
)
1230 error
= __clone_and_map(&ci
);
1232 /* drop the extra reference count */
1233 dec_pending(ci
.io
, error
);
1234 dm_table_put(ci
.map
);
1236 /*-----------------------------------------------------------------
1238 *---------------------------------------------------------------*/
1240 static int dm_merge_bvec(struct request_queue
*q
,
1241 struct bvec_merge_data
*bvm
,
1242 struct bio_vec
*biovec
)
1244 struct mapped_device
*md
= q
->queuedata
;
1245 struct dm_table
*map
= dm_get_table(md
);
1246 struct dm_target
*ti
;
1247 sector_t max_sectors
;
1253 ti
= dm_table_find_target(map
, bvm
->bi_sector
);
1254 if (!dm_target_is_valid(ti
))
1258 * Find maximum amount of I/O that won't need splitting
1260 max_sectors
= min(max_io_len(md
, bvm
->bi_sector
, ti
),
1261 (sector_t
) BIO_MAX_SECTORS
);
1262 max_size
= (max_sectors
<< SECTOR_SHIFT
) - bvm
->bi_size
;
1267 * merge_bvec_fn() returns number of bytes
1268 * it can accept at this offset
1269 * max is precomputed maximal io size
1271 if (max_size
&& ti
->type
->merge
)
1272 max_size
= ti
->type
->merge(ti
, bvm
, biovec
, max_size
);
1274 * If the target doesn't support merge method and some of the devices
1275 * provided their merge_bvec method (we know this by looking at
1276 * queue_max_hw_sectors), then we can't allow bios with multiple vector
1277 * entries. So always set max_size to 0, and the code below allows
1280 else if (queue_max_hw_sectors(q
) <= PAGE_SIZE
>> 9)
1289 * Always allow an entire first page
1291 if (max_size
<= biovec
->bv_len
&& !(bvm
->bi_size
>> SECTOR_SHIFT
))
1292 max_size
= biovec
->bv_len
;
1298 * The request function that just remaps the bio built up by
1301 static int _dm_request(struct request_queue
*q
, struct bio
*bio
)
1303 int rw
= bio_data_dir(bio
);
1304 struct mapped_device
*md
= q
->queuedata
;
1307 down_read(&md
->io_lock
);
1309 cpu
= part_stat_lock();
1310 part_stat_inc(cpu
, &dm_disk(md
)->part0
, ios
[rw
]);
1311 part_stat_add(cpu
, &dm_disk(md
)->part0
, sectors
[rw
], bio_sectors(bio
));
1315 * If we're suspended or the thread is processing barriers
1316 * we have to queue this io for later.
1318 if (unlikely(test_bit(DMF_QUEUE_IO_TO_THREAD
, &md
->flags
)) ||
1319 unlikely(bio_barrier(bio
))) {
1320 up_read(&md
->io_lock
);
1322 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND
, &md
->flags
)) &&
1323 bio_rw(bio
) == READA
) {
1333 __split_and_process_bio(md
, bio
);
1334 up_read(&md
->io_lock
);
1338 static int dm_make_request(struct request_queue
*q
, struct bio
*bio
)
1340 struct mapped_device
*md
= q
->queuedata
;
1342 if (unlikely(bio_barrier(bio
))) {
1343 bio_endio(bio
, -EOPNOTSUPP
);
1347 return md
->saved_make_request_fn(q
, bio
); /* call __make_request() */
1350 static int dm_request_based(struct mapped_device
*md
)
1352 return blk_queue_stackable(md
->queue
);
1355 static int dm_request(struct request_queue
*q
, struct bio
*bio
)
1357 struct mapped_device
*md
= q
->queuedata
;
1359 if (dm_request_based(md
))
1360 return dm_make_request(q
, bio
);
1362 return _dm_request(q
, bio
);
1365 void dm_dispatch_request(struct request
*rq
)
1369 if (blk_queue_io_stat(rq
->q
))
1370 rq
->cmd_flags
|= REQ_IO_STAT
;
1372 rq
->start_time
= jiffies
;
1373 r
= blk_insert_cloned_request(rq
->q
, rq
);
1375 dm_complete_request(rq
, r
);
1377 EXPORT_SYMBOL_GPL(dm_dispatch_request
);
1379 static void dm_rq_bio_destructor(struct bio
*bio
)
1381 struct dm_rq_clone_bio_info
*info
= bio
->bi_private
;
1382 struct mapped_device
*md
= info
->tio
->md
;
1384 free_bio_info(info
);
1385 bio_free(bio
, md
->bs
);
1388 static int dm_rq_bio_constructor(struct bio
*bio
, struct bio
*bio_orig
,
1391 struct dm_rq_target_io
*tio
= data
;
1392 struct mapped_device
*md
= tio
->md
;
1393 struct dm_rq_clone_bio_info
*info
= alloc_bio_info(md
);
1398 info
->orig
= bio_orig
;
1400 bio
->bi_end_io
= end_clone_bio
;
1401 bio
->bi_private
= info
;
1402 bio
->bi_destructor
= dm_rq_bio_destructor
;
1407 static int setup_clone(struct request
*clone
, struct request
*rq
,
1408 struct dm_rq_target_io
*tio
)
1410 int r
= blk_rq_prep_clone(clone
, rq
, tio
->md
->bs
, GFP_ATOMIC
,
1411 dm_rq_bio_constructor
, tio
);
1416 clone
->cmd
= rq
->cmd
;
1417 clone
->cmd_len
= rq
->cmd_len
;
1418 clone
->sense
= rq
->sense
;
1419 clone
->buffer
= rq
->buffer
;
1420 clone
->end_io
= end_clone_request
;
1421 clone
->end_io_data
= tio
;
1426 static int dm_rq_flush_suspending(struct mapped_device
*md
)
1428 return !md
->suspend_rq
.special
;
1432 * Called with the queue lock held.
1434 static int dm_prep_fn(struct request_queue
*q
, struct request
*rq
)
1436 struct mapped_device
*md
= q
->queuedata
;
1437 struct dm_rq_target_io
*tio
;
1438 struct request
*clone
;
1440 if (unlikely(rq
== &md
->suspend_rq
)) {
1441 if (dm_rq_flush_suspending(md
))
1444 /* The flush suspend was interrupted */
1445 return BLKPREP_KILL
;
1448 if (unlikely(rq
->special
)) {
1449 DMWARN("Already has something in rq->special.");
1450 return BLKPREP_KILL
;
1453 tio
= alloc_rq_tio(md
); /* Only one for each original request */
1456 return BLKPREP_DEFER
;
1462 memset(&tio
->info
, 0, sizeof(tio
->info
));
1464 clone
= &tio
->clone
;
1465 if (setup_clone(clone
, rq
, tio
)) {
1468 return BLKPREP_DEFER
;
1471 rq
->special
= clone
;
1472 rq
->cmd_flags
|= REQ_DONTPREP
;
1477 static void map_request(struct dm_target
*ti
, struct request
*rq
,
1478 struct mapped_device
*md
)
1481 struct request
*clone
= rq
->special
;
1482 struct dm_rq_target_io
*tio
= clone
->end_io_data
;
1485 * Hold the md reference here for the in-flight I/O.
1486 * We can't rely on the reference count by device opener,
1487 * because the device may be closed during the request completion
1488 * when all bios are completed.
1489 * See the comment in rq_completed() too.
1494 r
= ti
->type
->map_rq(ti
, clone
, &tio
->info
);
1496 case DM_MAPIO_SUBMITTED
:
1497 /* The target has taken the I/O to submit by itself later */
1499 case DM_MAPIO_REMAPPED
:
1500 /* The target has remapped the I/O so dispatch it */
1501 dm_dispatch_request(clone
);
1503 case DM_MAPIO_REQUEUE
:
1504 /* The target wants to requeue the I/O */
1505 dm_requeue_unmapped_request(clone
);
1509 DMWARN("unimplemented target map return value: %d", r
);
1513 /* The target wants to complete the I/O */
1514 dm_kill_unmapped_request(clone
, r
);
1520 * q->request_fn for request-based dm.
1521 * Called with the queue lock held.
1523 static void dm_request_fn(struct request_queue
*q
)
1525 struct mapped_device
*md
= q
->queuedata
;
1526 struct dm_table
*map
= dm_get_table(md
);
1527 struct dm_target
*ti
;
1531 * For noflush suspend, check blk_queue_stopped() to immediately
1532 * quit I/O dispatching.
1534 while (!blk_queue_plugged(q
) && !blk_queue_stopped(q
)) {
1535 rq
= blk_peek_request(q
);
1539 if (unlikely(rq
== &md
->suspend_rq
)) { /* Flush suspend maker */
1540 if (queue_in_flight(q
))
1541 /* Not quiet yet. Wait more */
1544 /* This device should be quiet now */
1546 blk_start_request(rq
);
1547 __blk_end_request_all(rq
, 0);
1552 ti
= dm_table_find_target(map
, blk_rq_pos(rq
));
1553 if (ti
->type
->busy
&& ti
->type
->busy(ti
))
1556 blk_start_request(rq
);
1557 spin_unlock(q
->queue_lock
);
1558 map_request(ti
, rq
, md
);
1559 spin_lock_irq(q
->queue_lock
);
1565 if (!elv_queue_empty(q
))
1566 /* Some requests still remain, retry later */
1575 int dm_underlying_device_busy(struct request_queue
*q
)
1577 return blk_lld_busy(q
);
1579 EXPORT_SYMBOL_GPL(dm_underlying_device_busy
);
1581 static int dm_lld_busy(struct request_queue
*q
)
1584 struct mapped_device
*md
= q
->queuedata
;
1585 struct dm_table
*map
= dm_get_table(md
);
1587 if (!map
|| test_bit(DMF_BLOCK_IO_FOR_SUSPEND
, &md
->flags
))
1590 r
= dm_table_any_busy_target(map
);
1597 static void dm_unplug_all(struct request_queue
*q
)
1599 struct mapped_device
*md
= q
->queuedata
;
1600 struct dm_table
*map
= dm_get_table(md
);
1603 if (dm_request_based(md
))
1604 generic_unplug_device(q
);
1606 dm_table_unplug_all(map
);
1611 static int dm_any_congested(void *congested_data
, int bdi_bits
)
1614 struct mapped_device
*md
= congested_data
;
1615 struct dm_table
*map
;
1617 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND
, &md
->flags
)) {
1618 map
= dm_get_table(md
);
1621 * Request-based dm cares about only own queue for
1622 * the query about congestion status of request_queue
1624 if (dm_request_based(md
))
1625 r
= md
->queue
->backing_dev_info
.state
&
1628 r
= dm_table_any_congested(map
, bdi_bits
);
1637 /*-----------------------------------------------------------------
1638 * An IDR is used to keep track of allocated minor numbers.
1639 *---------------------------------------------------------------*/
1640 static DEFINE_IDR(_minor_idr
);
1642 static void free_minor(int minor
)
1644 spin_lock(&_minor_lock
);
1645 idr_remove(&_minor_idr
, minor
);
1646 spin_unlock(&_minor_lock
);
1650 * See if the device with a specific minor # is free.
1652 static int specific_minor(int minor
)
1656 if (minor
>= (1 << MINORBITS
))
1659 r
= idr_pre_get(&_minor_idr
, GFP_KERNEL
);
1663 spin_lock(&_minor_lock
);
1665 if (idr_find(&_minor_idr
, minor
)) {
1670 r
= idr_get_new_above(&_minor_idr
, MINOR_ALLOCED
, minor
, &m
);
1675 idr_remove(&_minor_idr
, m
);
1681 spin_unlock(&_minor_lock
);
1685 static int next_free_minor(int *minor
)
1689 r
= idr_pre_get(&_minor_idr
, GFP_KERNEL
);
1693 spin_lock(&_minor_lock
);
1695 r
= idr_get_new(&_minor_idr
, MINOR_ALLOCED
, &m
);
1699 if (m
>= (1 << MINORBITS
)) {
1700 idr_remove(&_minor_idr
, m
);
1708 spin_unlock(&_minor_lock
);
1712 static struct block_device_operations dm_blk_dops
;
1714 static void dm_wq_work(struct work_struct
*work
);
1717 * Allocate and initialise a blank device with a given minor.
1719 static struct mapped_device
*alloc_dev(int minor
)
1722 struct mapped_device
*md
= kzalloc(sizeof(*md
), GFP_KERNEL
);
1726 DMWARN("unable to allocate device, out of memory.");
1730 if (!try_module_get(THIS_MODULE
))
1731 goto bad_module_get
;
1733 /* get a minor number for the dev */
1734 if (minor
== DM_ANY_MINOR
)
1735 r
= next_free_minor(&minor
);
1737 r
= specific_minor(minor
);
1741 init_rwsem(&md
->io_lock
);
1742 mutex_init(&md
->suspend_lock
);
1743 spin_lock_init(&md
->deferred_lock
);
1744 rwlock_init(&md
->map_lock
);
1745 atomic_set(&md
->holders
, 1);
1746 atomic_set(&md
->open_count
, 0);
1747 atomic_set(&md
->event_nr
, 0);
1748 atomic_set(&md
->uevent_seq
, 0);
1749 INIT_LIST_HEAD(&md
->uevent_list
);
1750 spin_lock_init(&md
->uevent_lock
);
1752 md
->queue
= blk_init_queue(dm_request_fn
, NULL
);
1757 * Request-based dm devices cannot be stacked on top of bio-based dm
1758 * devices. The type of this dm device has not been decided yet,
1759 * although we initialized the queue using blk_init_queue().
1760 * The type is decided at the first table loading time.
1761 * To prevent problematic device stacking, clear the queue flag
1762 * for request stacking support until then.
1764 * This queue is new, so no concurrency on the queue_flags.
1766 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE
, md
->queue
);
1767 md
->saved_make_request_fn
= md
->queue
->make_request_fn
;
1768 md
->queue
->queuedata
= md
;
1769 md
->queue
->backing_dev_info
.congested_fn
= dm_any_congested
;
1770 md
->queue
->backing_dev_info
.congested_data
= md
;
1771 blk_queue_make_request(md
->queue
, dm_request
);
1772 blk_queue_bounce_limit(md
->queue
, BLK_BOUNCE_ANY
);
1773 md
->queue
->unplug_fn
= dm_unplug_all
;
1774 blk_queue_merge_bvec(md
->queue
, dm_merge_bvec
);
1775 blk_queue_softirq_done(md
->queue
, dm_softirq_done
);
1776 blk_queue_prep_rq(md
->queue
, dm_prep_fn
);
1777 blk_queue_lld_busy(md
->queue
, dm_lld_busy
);
1779 md
->disk
= alloc_disk(1);
1783 atomic_set(&md
->pending
, 0);
1784 init_waitqueue_head(&md
->wait
);
1785 INIT_WORK(&md
->work
, dm_wq_work
);
1786 init_waitqueue_head(&md
->eventq
);
1788 md
->disk
->major
= _major
;
1789 md
->disk
->first_minor
= minor
;
1790 md
->disk
->fops
= &dm_blk_dops
;
1791 md
->disk
->queue
= md
->queue
;
1792 md
->disk
->private_data
= md
;
1793 sprintf(md
->disk
->disk_name
, "dm-%d", minor
);
1795 format_dev_t(md
->name
, MKDEV(_major
, minor
));
1797 md
->wq
= create_singlethread_workqueue("kdmflush");
1801 md
->bdev
= bdget_disk(md
->disk
, 0);
1805 /* Populate the mapping, nobody knows we exist yet */
1806 spin_lock(&_minor_lock
);
1807 old_md
= idr_replace(&_minor_idr
, md
, minor
);
1808 spin_unlock(&_minor_lock
);
1810 BUG_ON(old_md
!= MINOR_ALLOCED
);
1815 destroy_workqueue(md
->wq
);
1819 blk_cleanup_queue(md
->queue
);
1823 module_put(THIS_MODULE
);
1829 static void unlock_fs(struct mapped_device
*md
);
1831 static void free_dev(struct mapped_device
*md
)
1833 int minor
= MINOR(disk_devt(md
->disk
));
1837 destroy_workqueue(md
->wq
);
1839 mempool_destroy(md
->tio_pool
);
1841 mempool_destroy(md
->io_pool
);
1843 bioset_free(md
->bs
);
1844 blk_integrity_unregister(md
->disk
);
1845 del_gendisk(md
->disk
);
1848 spin_lock(&_minor_lock
);
1849 md
->disk
->private_data
= NULL
;
1850 spin_unlock(&_minor_lock
);
1853 blk_cleanup_queue(md
->queue
);
1854 module_put(THIS_MODULE
);
1858 static void __bind_mempools(struct mapped_device
*md
, struct dm_table
*t
)
1860 struct dm_md_mempools
*p
;
1862 if (md
->io_pool
&& md
->tio_pool
&& md
->bs
)
1863 /* the md already has necessary mempools */
1866 p
= dm_table_get_md_mempools(t
);
1867 BUG_ON(!p
|| md
->io_pool
|| md
->tio_pool
|| md
->bs
);
1869 md
->io_pool
= p
->io_pool
;
1871 md
->tio_pool
= p
->tio_pool
;
1877 /* mempool bind completed, now no need any mempools in the table */
1878 dm_table_free_md_mempools(t
);
1882 * Bind a table to the device.
1884 static void event_callback(void *context
)
1886 unsigned long flags
;
1888 struct mapped_device
*md
= (struct mapped_device
*) context
;
1890 spin_lock_irqsave(&md
->uevent_lock
, flags
);
1891 list_splice_init(&md
->uevent_list
, &uevents
);
1892 spin_unlock_irqrestore(&md
->uevent_lock
, flags
);
1894 dm_send_uevents(&uevents
, &disk_to_dev(md
->disk
)->kobj
);
1896 atomic_inc(&md
->event_nr
);
1897 wake_up(&md
->eventq
);
1900 static void __set_size(struct mapped_device
*md
, sector_t size
)
1902 set_capacity(md
->disk
, size
);
1904 mutex_lock(&md
->bdev
->bd_inode
->i_mutex
);
1905 i_size_write(md
->bdev
->bd_inode
, (loff_t
)size
<< SECTOR_SHIFT
);
1906 mutex_unlock(&md
->bdev
->bd_inode
->i_mutex
);
1909 static int __bind(struct mapped_device
*md
, struct dm_table
*t
,
1910 struct queue_limits
*limits
)
1912 struct request_queue
*q
= md
->queue
;
1914 unsigned long flags
;
1916 size
= dm_table_get_size(t
);
1919 * Wipe any geometry if the size of the table changed.
1921 if (size
!= get_capacity(md
->disk
))
1922 memset(&md
->geometry
, 0, sizeof(md
->geometry
));
1924 __set_size(md
, size
);
1927 dm_table_destroy(t
);
1931 dm_table_event_callback(t
, event_callback
, md
);
1934 * The queue hasn't been stopped yet, if the old table type wasn't
1935 * for request-based during suspension. So stop it to prevent
1936 * I/O mapping before resume.
1937 * This must be done before setting the queue restrictions,
1938 * because request-based dm may be run just after the setting.
1940 if (dm_table_request_based(t
) && !blk_queue_stopped(q
))
1943 __bind_mempools(md
, t
);
1945 write_lock_irqsave(&md
->map_lock
, flags
);
1947 dm_table_set_restrictions(t
, q
, limits
);
1948 write_unlock_irqrestore(&md
->map_lock
, flags
);
1953 static void __unbind(struct mapped_device
*md
)
1955 struct dm_table
*map
= md
->map
;
1956 unsigned long flags
;
1961 dm_table_event_callback(map
, NULL
, NULL
);
1962 write_lock_irqsave(&md
->map_lock
, flags
);
1964 write_unlock_irqrestore(&md
->map_lock
, flags
);
1965 dm_table_destroy(map
);
1969 * Constructor for a new device.
1971 int dm_create(int minor
, struct mapped_device
**result
)
1973 struct mapped_device
*md
;
1975 md
= alloc_dev(minor
);
1985 static struct mapped_device
*dm_find_md(dev_t dev
)
1987 struct mapped_device
*md
;
1988 unsigned minor
= MINOR(dev
);
1990 if (MAJOR(dev
) != _major
|| minor
>= (1 << MINORBITS
))
1993 spin_lock(&_minor_lock
);
1995 md
= idr_find(&_minor_idr
, minor
);
1996 if (md
&& (md
== MINOR_ALLOCED
||
1997 (MINOR(disk_devt(dm_disk(md
))) != minor
) ||
1998 test_bit(DMF_FREEING
, &md
->flags
))) {
2004 spin_unlock(&_minor_lock
);
2009 struct mapped_device
*dm_get_md(dev_t dev
)
2011 struct mapped_device
*md
= dm_find_md(dev
);
2019 void *dm_get_mdptr(struct mapped_device
*md
)
2021 return md
->interface_ptr
;
2024 void dm_set_mdptr(struct mapped_device
*md
, void *ptr
)
2026 md
->interface_ptr
= ptr
;
2029 void dm_get(struct mapped_device
*md
)
2031 atomic_inc(&md
->holders
);
2034 const char *dm_device_name(struct mapped_device
*md
)
2038 EXPORT_SYMBOL_GPL(dm_device_name
);
2040 void dm_put(struct mapped_device
*md
)
2042 struct dm_table
*map
;
2044 BUG_ON(test_bit(DMF_FREEING
, &md
->flags
));
2046 if (atomic_dec_and_lock(&md
->holders
, &_minor_lock
)) {
2047 map
= dm_get_table(md
);
2048 idr_replace(&_minor_idr
, MINOR_ALLOCED
,
2049 MINOR(disk_devt(dm_disk(md
))));
2050 set_bit(DMF_FREEING
, &md
->flags
);
2051 spin_unlock(&_minor_lock
);
2052 if (!dm_suspended(md
)) {
2053 dm_table_presuspend_targets(map
);
2054 dm_table_postsuspend_targets(map
);
2062 EXPORT_SYMBOL_GPL(dm_put
);
2064 static int dm_wait_for_completion(struct mapped_device
*md
, int interruptible
)
2067 DECLARE_WAITQUEUE(wait
, current
);
2068 struct request_queue
*q
= md
->queue
;
2069 unsigned long flags
;
2071 dm_unplug_all(md
->queue
);
2073 add_wait_queue(&md
->wait
, &wait
);
2076 set_current_state(interruptible
);
2079 if (dm_request_based(md
)) {
2080 spin_lock_irqsave(q
->queue_lock
, flags
);
2081 if (!queue_in_flight(q
) && blk_queue_stopped(q
)) {
2082 spin_unlock_irqrestore(q
->queue_lock
, flags
);
2085 spin_unlock_irqrestore(q
->queue_lock
, flags
);
2086 } else if (!atomic_read(&md
->pending
))
2089 if (interruptible
== TASK_INTERRUPTIBLE
&&
2090 signal_pending(current
)) {
2097 set_current_state(TASK_RUNNING
);
2099 remove_wait_queue(&md
->wait
, &wait
);
2104 static void dm_flush(struct mapped_device
*md
)
2106 dm_wait_for_completion(md
, TASK_UNINTERRUPTIBLE
);
2108 bio_init(&md
->barrier_bio
);
2109 md
->barrier_bio
.bi_bdev
= md
->bdev
;
2110 md
->barrier_bio
.bi_rw
= WRITE_BARRIER
;
2111 __split_and_process_bio(md
, &md
->barrier_bio
);
2113 dm_wait_for_completion(md
, TASK_UNINTERRUPTIBLE
);
2116 static void process_barrier(struct mapped_device
*md
, struct bio
*bio
)
2118 md
->barrier_error
= 0;
2122 if (!bio_empty_barrier(bio
)) {
2123 __split_and_process_bio(md
, bio
);
2127 if (md
->barrier_error
!= DM_ENDIO_REQUEUE
)
2128 bio_endio(bio
, md
->barrier_error
);
2130 spin_lock_irq(&md
->deferred_lock
);
2131 bio_list_add_head(&md
->deferred
, bio
);
2132 spin_unlock_irq(&md
->deferred_lock
);
2137 * Process the deferred bios
2139 static void dm_wq_work(struct work_struct
*work
)
2141 struct mapped_device
*md
= container_of(work
, struct mapped_device
,
2145 down_write(&md
->io_lock
);
2147 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND
, &md
->flags
)) {
2148 spin_lock_irq(&md
->deferred_lock
);
2149 c
= bio_list_pop(&md
->deferred
);
2150 spin_unlock_irq(&md
->deferred_lock
);
2153 clear_bit(DMF_QUEUE_IO_TO_THREAD
, &md
->flags
);
2157 up_write(&md
->io_lock
);
2159 if (dm_request_based(md
))
2160 generic_make_request(c
);
2163 process_barrier(md
, c
);
2165 __split_and_process_bio(md
, c
);
2168 down_write(&md
->io_lock
);
2171 up_write(&md
->io_lock
);
2174 static void dm_queue_flush(struct mapped_device
*md
)
2176 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND
, &md
->flags
);
2177 smp_mb__after_clear_bit();
2178 queue_work(md
->wq
, &md
->work
);
2182 * Swap in a new table (destroying old one).
2184 int dm_swap_table(struct mapped_device
*md
, struct dm_table
*table
)
2186 struct queue_limits limits
;
2189 mutex_lock(&md
->suspend_lock
);
2191 /* device must be suspended */
2192 if (!dm_suspended(md
))
2195 r
= dm_calculate_queue_limits(table
, &limits
);
2199 /* cannot change the device type, once a table is bound */
2201 (dm_table_get_type(md
->map
) != dm_table_get_type(table
))) {
2202 DMWARN("can't change the device type after a table is bound");
2207 * It is enought that blk_queue_ordered() is called only once when
2208 * the first bio-based table is bound.
2210 * This setting should be moved to alloc_dev() when request-based dm
2213 if (!md
->map
&& dm_table_bio_based(table
))
2214 blk_queue_ordered(md
->queue
, QUEUE_ORDERED_DRAIN
, NULL
);
2217 r
= __bind(md
, table
, &limits
);
2220 mutex_unlock(&md
->suspend_lock
);
2224 static void dm_rq_invalidate_suspend_marker(struct mapped_device
*md
)
2226 md
->suspend_rq
.special
= (void *)0x1;
2229 static void dm_rq_abort_suspend(struct mapped_device
*md
, int noflush
)
2231 struct request_queue
*q
= md
->queue
;
2232 unsigned long flags
;
2234 spin_lock_irqsave(q
->queue_lock
, flags
);
2236 dm_rq_invalidate_suspend_marker(md
);
2238 spin_unlock_irqrestore(q
->queue_lock
, flags
);
2241 static void dm_rq_start_suspend(struct mapped_device
*md
, int noflush
)
2243 struct request
*rq
= &md
->suspend_rq
;
2244 struct request_queue
*q
= md
->queue
;
2250 blk_insert_request(q
, rq
, 0, NULL
);
2254 static int dm_rq_suspend_available(struct mapped_device
*md
, int noflush
)
2257 struct request
*rq
= &md
->suspend_rq
;
2258 struct request_queue
*q
= md
->queue
;
2259 unsigned long flags
;
2264 /* The marker must be protected by queue lock if it is in use */
2265 spin_lock_irqsave(q
->queue_lock
, flags
);
2266 if (unlikely(rq
->ref_count
)) {
2268 * This can happen, when the previous flush suspend was
2269 * interrupted, the marker is still in the queue and
2270 * this flush suspend has been invoked, because we don't
2271 * remove the marker at the time of suspend interruption.
2272 * We have only one marker per mapped_device, so we can't
2273 * start another flush suspend while it is in use.
2275 BUG_ON(!rq
->special
); /* The marker should be invalidated */
2276 DMWARN("Invalidating the previous flush suspend is still in"
2277 " progress. Please retry later.");
2280 spin_unlock_irqrestore(q
->queue_lock
, flags
);
2286 * Functions to lock and unlock any filesystem running on the
2289 static int lock_fs(struct mapped_device
*md
)
2293 WARN_ON(md
->frozen_sb
);
2295 md
->frozen_sb
= freeze_bdev(md
->bdev
);
2296 if (IS_ERR(md
->frozen_sb
)) {
2297 r
= PTR_ERR(md
->frozen_sb
);
2298 md
->frozen_sb
= NULL
;
2302 set_bit(DMF_FROZEN
, &md
->flags
);
2307 static void unlock_fs(struct mapped_device
*md
)
2309 if (!test_bit(DMF_FROZEN
, &md
->flags
))
2312 thaw_bdev(md
->bdev
, md
->frozen_sb
);
2313 md
->frozen_sb
= NULL
;
2314 clear_bit(DMF_FROZEN
, &md
->flags
);
2318 * We need to be able to change a mapping table under a mounted
2319 * filesystem. For example we might want to move some data in
2320 * the background. Before the table can be swapped with
2321 * dm_bind_table, dm_suspend must be called to flush any in
2322 * flight bios and ensure that any further io gets deferred.
2325 * Suspend mechanism in request-based dm.
2327 * After the suspend starts, further incoming requests are kept in
2328 * the request_queue and deferred.
2329 * Remaining requests in the request_queue at the start of suspend are flushed
2330 * if it is flush suspend.
2331 * The suspend completes when the following conditions have been satisfied,
2333 * 1. q->in_flight is 0 (which means no in_flight request)
2334 * 2. queue has been stopped (which means no request dispatching)
2339 * Noflush suspend doesn't need to dispatch remaining requests.
2340 * So stop the queue immediately. Then, wait for all in_flight requests
2341 * to be completed or requeued.
2343 * To abort noflush suspend, start the queue.
2348 * Flush suspend needs to dispatch remaining requests. So stop the queue
2349 * after the remaining requests are completed. (Requeued request must be also
2350 * re-dispatched and completed. Until then, we can't stop the queue.)
2352 * During flushing the remaining requests, further incoming requests are also
2353 * inserted to the same queue. To distinguish which requests are to be
2354 * flushed, we insert a marker request to the queue at the time of starting
2355 * flush suspend, like a barrier.
2356 * The dispatching is blocked when the marker is found on the top of the queue.
2357 * And the queue is stopped when all in_flight requests are completed, since
2358 * that means the remaining requests are completely flushed.
2359 * Then, the marker is removed from the queue.
2361 * To abort flush suspend, we also need to take care of the marker, not only
2362 * starting the queue.
2363 * We don't remove the marker forcibly from the queue since it's against
2364 * the block-layer manner. Instead, we put a invalidated mark on the marker.
2365 * When the invalidated marker is found on the top of the queue, it is
2366 * immediately removed from the queue, so it doesn't block dispatching.
2367 * Because we have only one marker per mapped_device, we can't start another
2368 * flush suspend until the invalidated marker is removed from the queue.
2369 * So fail and return with -EBUSY in such a case.
2371 int dm_suspend(struct mapped_device
*md
, unsigned suspend_flags
)
2373 struct dm_table
*map
= NULL
;
2375 int do_lockfs
= suspend_flags
& DM_SUSPEND_LOCKFS_FLAG
? 1 : 0;
2376 int noflush
= suspend_flags
& DM_SUSPEND_NOFLUSH_FLAG
? 1 : 0;
2378 mutex_lock(&md
->suspend_lock
);
2380 if (dm_suspended(md
)) {
2385 if (dm_request_based(md
) && !dm_rq_suspend_available(md
, noflush
)) {
2390 map
= dm_get_table(md
);
2393 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2394 * This flag is cleared before dm_suspend returns.
2397 set_bit(DMF_NOFLUSH_SUSPENDING
, &md
->flags
);
2399 /* This does not get reverted if there's an error later. */
2400 dm_table_presuspend_targets(map
);
2403 * Flush I/O to the device. noflush supersedes do_lockfs,
2404 * because lock_fs() needs to flush I/Os.
2406 if (!noflush
&& do_lockfs
) {
2413 * Here we must make sure that no processes are submitting requests
2414 * to target drivers i.e. no one may be executing
2415 * __split_and_process_bio. This is called from dm_request and
2418 * To get all processes out of __split_and_process_bio in dm_request,
2419 * we take the write lock. To prevent any process from reentering
2420 * __split_and_process_bio from dm_request, we set
2421 * DMF_QUEUE_IO_TO_THREAD.
2423 * To quiesce the thread (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND
2424 * and call flush_workqueue(md->wq). flush_workqueue will wait until
2425 * dm_wq_work exits and DMF_BLOCK_IO_FOR_SUSPEND will prevent any
2426 * further calls to __split_and_process_bio from dm_wq_work.
2428 down_write(&md
->io_lock
);
2429 set_bit(DMF_BLOCK_IO_FOR_SUSPEND
, &md
->flags
);
2430 set_bit(DMF_QUEUE_IO_TO_THREAD
, &md
->flags
);
2431 up_write(&md
->io_lock
);
2433 flush_workqueue(md
->wq
);
2435 if (dm_request_based(md
))
2436 dm_rq_start_suspend(md
, noflush
);
2439 * At this point no more requests are entering target request routines.
2440 * We call dm_wait_for_completion to wait for all existing requests
2443 r
= dm_wait_for_completion(md
, TASK_INTERRUPTIBLE
);
2445 down_write(&md
->io_lock
);
2447 clear_bit(DMF_NOFLUSH_SUSPENDING
, &md
->flags
);
2448 up_write(&md
->io_lock
);
2450 /* were we interrupted ? */
2454 if (dm_request_based(md
))
2455 dm_rq_abort_suspend(md
, noflush
);
2458 goto out
; /* pushback list is already flushed, so skip flush */
2462 * If dm_wait_for_completion returned 0, the device is completely
2463 * quiescent now. There is no request-processing activity. All new
2464 * requests are being added to md->deferred list.
2467 dm_table_postsuspend_targets(map
);
2469 set_bit(DMF_SUSPENDED
, &md
->flags
);
2475 mutex_unlock(&md
->suspend_lock
);
2479 int dm_resume(struct mapped_device
*md
)
2482 struct dm_table
*map
= NULL
;
2484 mutex_lock(&md
->suspend_lock
);
2485 if (!dm_suspended(md
))
2488 map
= dm_get_table(md
);
2489 if (!map
|| !dm_table_get_size(map
))
2492 r
= dm_table_resume_targets(map
);
2499 * Flushing deferred I/Os must be done after targets are resumed
2500 * so that mapping of targets can work correctly.
2501 * Request-based dm is queueing the deferred I/Os in its request_queue.
2503 if (dm_request_based(md
))
2504 start_queue(md
->queue
);
2508 clear_bit(DMF_SUSPENDED
, &md
->flags
);
2510 dm_table_unplug_all(map
);
2514 mutex_unlock(&md
->suspend_lock
);
2519 /*-----------------------------------------------------------------
2520 * Event notification.
2521 *---------------------------------------------------------------*/
2522 void dm_kobject_uevent(struct mapped_device
*md
, enum kobject_action action
,
2525 char udev_cookie
[DM_COOKIE_LENGTH
];
2526 char *envp
[] = { udev_cookie
, NULL
};
2529 kobject_uevent(&disk_to_dev(md
->disk
)->kobj
, action
);
2531 snprintf(udev_cookie
, DM_COOKIE_LENGTH
, "%s=%u",
2532 DM_COOKIE_ENV_VAR_NAME
, cookie
);
2533 kobject_uevent_env(&disk_to_dev(md
->disk
)->kobj
, action
, envp
);
2537 uint32_t dm_next_uevent_seq(struct mapped_device
*md
)
2539 return atomic_add_return(1, &md
->uevent_seq
);
2542 uint32_t dm_get_event_nr(struct mapped_device
*md
)
2544 return atomic_read(&md
->event_nr
);
2547 int dm_wait_event(struct mapped_device
*md
, int event_nr
)
2549 return wait_event_interruptible(md
->eventq
,
2550 (event_nr
!= atomic_read(&md
->event_nr
)));
2553 void dm_uevent_add(struct mapped_device
*md
, struct list_head
*elist
)
2555 unsigned long flags
;
2557 spin_lock_irqsave(&md
->uevent_lock
, flags
);
2558 list_add(elist
, &md
->uevent_list
);
2559 spin_unlock_irqrestore(&md
->uevent_lock
, flags
);
2563 * The gendisk is only valid as long as you have a reference
2566 struct gendisk
*dm_disk(struct mapped_device
*md
)
2571 struct kobject
*dm_kobject(struct mapped_device
*md
)
2577 * struct mapped_device should not be exported outside of dm.c
2578 * so use this check to verify that kobj is part of md structure
2580 struct mapped_device
*dm_get_from_kobject(struct kobject
*kobj
)
2582 struct mapped_device
*md
;
2584 md
= container_of(kobj
, struct mapped_device
, kobj
);
2585 if (&md
->kobj
!= kobj
)
2588 if (test_bit(DMF_FREEING
, &md
->flags
) ||
2589 test_bit(DMF_DELETING
, &md
->flags
))
2596 int dm_suspended(struct mapped_device
*md
)
2598 return test_bit(DMF_SUSPENDED
, &md
->flags
);
2601 int dm_noflush_suspending(struct dm_target
*ti
)
2603 struct mapped_device
*md
= dm_table_get_md(ti
->table
);
2604 int r
= __noflush_suspending(md
);
2610 EXPORT_SYMBOL_GPL(dm_noflush_suspending
);
2612 struct dm_md_mempools
*dm_alloc_md_mempools(unsigned type
)
2614 struct dm_md_mempools
*pools
= kmalloc(sizeof(*pools
), GFP_KERNEL
);
2619 pools
->io_pool
= (type
== DM_TYPE_BIO_BASED
) ?
2620 mempool_create_slab_pool(MIN_IOS
, _io_cache
) :
2621 mempool_create_slab_pool(MIN_IOS
, _rq_bio_info_cache
);
2622 if (!pools
->io_pool
)
2623 goto free_pools_and_out
;
2625 pools
->tio_pool
= (type
== DM_TYPE_BIO_BASED
) ?
2626 mempool_create_slab_pool(MIN_IOS
, _tio_cache
) :
2627 mempool_create_slab_pool(MIN_IOS
, _rq_tio_cache
);
2628 if (!pools
->tio_pool
)
2629 goto free_io_pool_and_out
;
2631 pools
->bs
= (type
== DM_TYPE_BIO_BASED
) ?
2632 bioset_create(16, 0) : bioset_create(MIN_IOS
, 0);
2634 goto free_tio_pool_and_out
;
2638 free_tio_pool_and_out
:
2639 mempool_destroy(pools
->tio_pool
);
2641 free_io_pool_and_out
:
2642 mempool_destroy(pools
->io_pool
);
2650 void dm_free_md_mempools(struct dm_md_mempools
*pools
)
2656 mempool_destroy(pools
->io_pool
);
2658 if (pools
->tio_pool
)
2659 mempool_destroy(pools
->tio_pool
);
2662 bioset_free(pools
->bs
);
2667 static struct block_device_operations dm_blk_dops
= {
2668 .open
= dm_blk_open
,
2669 .release
= dm_blk_close
,
2670 .ioctl
= dm_blk_ioctl
,
2671 .getgeo
= dm_blk_getgeo
,
2672 .owner
= THIS_MODULE
2675 EXPORT_SYMBOL(dm_get_mapinfo
);
2680 module_init(dm_init
);
2681 module_exit(dm_exit
);
2683 module_param(major
, uint
, 0);
2684 MODULE_PARM_DESC(major
, "The major number of the device mapper");
2685 MODULE_DESCRIPTION(DM_NAME
" driver");
2686 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2687 MODULE_LICENSE("GPL");