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/smp_lock.h>
19 #include <linux/mempool.h>
20 #include <linux/slab.h>
21 #include <linux/idr.h>
22 #include <linux/hdreg.h>
23 #include <linux/delay.h>
25 #include <trace/events/block.h>
27 #define DM_MSG_PREFIX "core"
30 * Cookies are numeric values sent with CHANGE and REMOVE
31 * uevents while resuming, removing or renaming the device.
33 #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
34 #define DM_COOKIE_LENGTH 24
36 static const char *_name
= DM_NAME
;
38 static unsigned int major
= 0;
39 static unsigned int _major
= 0;
41 static DEFINE_SPINLOCK(_minor_lock
);
44 * One of these is allocated per bio.
47 struct mapped_device
*md
;
51 unsigned long start_time
;
52 spinlock_t endio_lock
;
57 * One of these is allocated per target within a bio. Hopefully
58 * this will be simplified out one day.
67 * For request-based dm.
68 * One of these is allocated per request.
70 struct dm_rq_target_io
{
71 struct mapped_device
*md
;
73 struct request
*orig
, clone
;
79 * For request-based dm.
80 * One of these is allocated per bio.
82 struct dm_rq_clone_bio_info
{
84 struct dm_rq_target_io
*tio
;
87 union map_info
*dm_get_mapinfo(struct bio
*bio
)
89 if (bio
&& bio
->bi_private
)
90 return &((struct dm_target_io
*)bio
->bi_private
)->info
;
94 union map_info
*dm_get_rq_mapinfo(struct request
*rq
)
96 if (rq
&& rq
->end_io_data
)
97 return &((struct dm_rq_target_io
*)rq
->end_io_data
)->info
;
100 EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo
);
102 #define MINOR_ALLOCED ((void *)-1)
105 * Bits for the md->flags field.
107 #define DMF_BLOCK_IO_FOR_SUSPEND 0
108 #define DMF_SUSPENDED 1
110 #define DMF_FREEING 3
111 #define DMF_DELETING 4
112 #define DMF_NOFLUSH_SUSPENDING 5
113 #define DMF_QUEUE_IO_TO_THREAD 6
116 * Work processed by per-device workqueue.
118 struct mapped_device
{
119 struct rw_semaphore io_lock
;
120 struct mutex suspend_lock
;
127 struct request_queue
*queue
;
129 /* Protect queue and type against concurrent access. */
130 struct mutex type_lock
;
132 struct gendisk
*disk
;
138 * A list of ios that arrived while we were suspended.
141 wait_queue_head_t wait
;
142 struct work_struct work
;
143 struct bio_list deferred
;
144 spinlock_t deferred_lock
;
147 * An error from the flush request currently being processed.
152 * Protect barrier_error from concurrent endio processing
153 * in request-based dm.
155 spinlock_t barrier_error_lock
;
159 * Processing queue (flush/barriers)
161 struct workqueue_struct
*wq
;
162 struct work_struct barrier_work
;
164 /* A pointer to the currently processing pre/post flush request */
165 struct request
*flush_request
;
168 * The current mapping.
170 struct dm_table
*map
;
173 * io objects are allocated from here.
184 wait_queue_head_t eventq
;
186 struct list_head uevent_list
;
187 spinlock_t uevent_lock
; /* Protect access to uevent_list */
190 * freeze/thaw support require holding onto a super block
192 struct super_block
*frozen_sb
;
193 struct block_device
*bdev
;
195 /* forced geometry settings */
196 struct hd_geometry geometry
;
198 /* For saving the address of __make_request for request based dm */
199 make_request_fn
*saved_make_request_fn
;
204 /* zero-length flush that will be cloned and submitted to targets */
205 struct bio flush_bio
;
209 * For mempools pre-allocation at the table loading time.
211 struct dm_md_mempools
{
218 static struct kmem_cache
*_io_cache
;
219 static struct kmem_cache
*_tio_cache
;
220 static struct kmem_cache
*_rq_tio_cache
;
221 static struct kmem_cache
*_rq_bio_info_cache
;
223 static int __init
local_init(void)
227 /* allocate a slab for the dm_ios */
228 _io_cache
= KMEM_CACHE(dm_io
, 0);
232 /* allocate a slab for the target ios */
233 _tio_cache
= KMEM_CACHE(dm_target_io
, 0);
235 goto out_free_io_cache
;
237 _rq_tio_cache
= KMEM_CACHE(dm_rq_target_io
, 0);
239 goto out_free_tio_cache
;
241 _rq_bio_info_cache
= KMEM_CACHE(dm_rq_clone_bio_info
, 0);
242 if (!_rq_bio_info_cache
)
243 goto out_free_rq_tio_cache
;
245 r
= dm_uevent_init();
247 goto out_free_rq_bio_info_cache
;
250 r
= register_blkdev(_major
, _name
);
252 goto out_uevent_exit
;
261 out_free_rq_bio_info_cache
:
262 kmem_cache_destroy(_rq_bio_info_cache
);
263 out_free_rq_tio_cache
:
264 kmem_cache_destroy(_rq_tio_cache
);
266 kmem_cache_destroy(_tio_cache
);
268 kmem_cache_destroy(_io_cache
);
273 static void local_exit(void)
275 kmem_cache_destroy(_rq_bio_info_cache
);
276 kmem_cache_destroy(_rq_tio_cache
);
277 kmem_cache_destroy(_tio_cache
);
278 kmem_cache_destroy(_io_cache
);
279 unregister_blkdev(_major
, _name
);
284 DMINFO("cleaned up");
287 static int (*_inits
[])(void) __initdata
= {
297 static void (*_exits
[])(void) = {
307 static int __init
dm_init(void)
309 const int count
= ARRAY_SIZE(_inits
);
313 for (i
= 0; i
< count
; i
++) {
328 static void __exit
dm_exit(void)
330 int i
= ARRAY_SIZE(_exits
);
337 * Block device functions
339 int dm_deleting_md(struct mapped_device
*md
)
341 return test_bit(DMF_DELETING
, &md
->flags
);
344 static int dm_blk_open(struct block_device
*bdev
, fmode_t mode
)
346 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
);
368 return md
? 0 : -ENXIO
;
371 static int dm_blk_close(struct gendisk
*disk
, fmode_t mode
)
373 struct mapped_device
*md
= disk
->private_data
;
376 atomic_dec(&md
->open_count
);
383 int dm_open_count(struct mapped_device
*md
)
385 return atomic_read(&md
->open_count
);
389 * Guarantees nothing is using the device before it's deleted.
391 int dm_lock_for_deletion(struct mapped_device
*md
)
395 spin_lock(&_minor_lock
);
397 if (dm_open_count(md
))
400 set_bit(DMF_DELETING
, &md
->flags
);
402 spin_unlock(&_minor_lock
);
407 static int dm_blk_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
409 struct mapped_device
*md
= bdev
->bd_disk
->private_data
;
411 return dm_get_geometry(md
, geo
);
414 static int dm_blk_ioctl(struct block_device
*bdev
, fmode_t mode
,
415 unsigned int cmd
, unsigned long arg
)
417 struct mapped_device
*md
= bdev
->bd_disk
->private_data
;
418 struct dm_table
*map
= dm_get_live_table(md
);
419 struct dm_target
*tgt
;
422 if (!map
|| !dm_table_get_size(map
))
425 /* We only support devices that have a single target */
426 if (dm_table_get_num_targets(map
) != 1)
429 tgt
= dm_table_get_target(map
, 0);
431 if (dm_suspended_md(md
)) {
436 if (tgt
->type
->ioctl
)
437 r
= tgt
->type
->ioctl(tgt
, cmd
, arg
);
445 static struct dm_io
*alloc_io(struct mapped_device
*md
)
447 return mempool_alloc(md
->io_pool
, GFP_NOIO
);
450 static void free_io(struct mapped_device
*md
, struct dm_io
*io
)
452 mempool_free(io
, md
->io_pool
);
455 static void free_tio(struct mapped_device
*md
, struct dm_target_io
*tio
)
457 mempool_free(tio
, md
->tio_pool
);
460 static struct dm_rq_target_io
*alloc_rq_tio(struct mapped_device
*md
,
463 return mempool_alloc(md
->tio_pool
, gfp_mask
);
466 static void free_rq_tio(struct dm_rq_target_io
*tio
)
468 mempool_free(tio
, tio
->md
->tio_pool
);
471 static struct dm_rq_clone_bio_info
*alloc_bio_info(struct mapped_device
*md
)
473 return mempool_alloc(md
->io_pool
, GFP_ATOMIC
);
476 static void free_bio_info(struct dm_rq_clone_bio_info
*info
)
478 mempool_free(info
, info
->tio
->md
->io_pool
);
481 static int md_in_flight(struct mapped_device
*md
)
483 return atomic_read(&md
->pending
[READ
]) +
484 atomic_read(&md
->pending
[WRITE
]);
487 static void start_io_acct(struct dm_io
*io
)
489 struct mapped_device
*md
= io
->md
;
491 int rw
= bio_data_dir(io
->bio
);
493 io
->start_time
= jiffies
;
495 cpu
= part_stat_lock();
496 part_round_stats(cpu
, &dm_disk(md
)->part0
);
498 dm_disk(md
)->part0
.in_flight
[rw
] = atomic_inc_return(&md
->pending
[rw
]);
501 static void end_io_acct(struct dm_io
*io
)
503 struct mapped_device
*md
= io
->md
;
504 struct bio
*bio
= io
->bio
;
505 unsigned long duration
= jiffies
- io
->start_time
;
507 int rw
= bio_data_dir(bio
);
509 cpu
= part_stat_lock();
510 part_round_stats(cpu
, &dm_disk(md
)->part0
);
511 part_stat_add(cpu
, &dm_disk(md
)->part0
, ticks
[rw
], duration
);
515 * After this is decremented the bio must not be touched if it is
518 dm_disk(md
)->part0
.in_flight
[rw
] = pending
=
519 atomic_dec_return(&md
->pending
[rw
]);
520 pending
+= atomic_read(&md
->pending
[rw
^0x1]);
522 /* nudge anyone waiting on suspend queue */
528 * Add the bio to the list of deferred io.
530 static void queue_io(struct mapped_device
*md
, struct bio
*bio
)
532 down_write(&md
->io_lock
);
534 spin_lock_irq(&md
->deferred_lock
);
535 bio_list_add(&md
->deferred
, bio
);
536 spin_unlock_irq(&md
->deferred_lock
);
538 if (!test_and_set_bit(DMF_QUEUE_IO_TO_THREAD
, &md
->flags
))
539 queue_work(md
->wq
, &md
->work
);
541 up_write(&md
->io_lock
);
545 * Everyone (including functions in this file), should use this
546 * function to access the md->map field, and make sure they call
547 * dm_table_put() when finished.
549 struct dm_table
*dm_get_live_table(struct mapped_device
*md
)
554 read_lock_irqsave(&md
->map_lock
, flags
);
558 read_unlock_irqrestore(&md
->map_lock
, flags
);
564 * Get the geometry associated with a dm device
566 int dm_get_geometry(struct mapped_device
*md
, struct hd_geometry
*geo
)
574 * Set the geometry of a device.
576 int dm_set_geometry(struct mapped_device
*md
, struct hd_geometry
*geo
)
578 sector_t sz
= (sector_t
)geo
->cylinders
* geo
->heads
* geo
->sectors
;
580 if (geo
->start
> sz
) {
581 DMWARN("Start sector is beyond the geometry limits.");
590 /*-----------------------------------------------------------------
592 * A more elegant soln is in the works that uses the queue
593 * merge fn, unfortunately there are a couple of changes to
594 * the block layer that I want to make for this. So in the
595 * interests of getting something for people to use I give
596 * you this clearly demarcated crap.
597 *---------------------------------------------------------------*/
599 static int __noflush_suspending(struct mapped_device
*md
)
601 return test_bit(DMF_NOFLUSH_SUSPENDING
, &md
->flags
);
605 * Decrements the number of outstanding ios that a bio has been
606 * cloned into, completing the original io if necc.
608 static void dec_pending(struct dm_io
*io
, int error
)
613 struct mapped_device
*md
= io
->md
;
615 /* Push-back supersedes any I/O errors */
616 if (unlikely(error
)) {
617 spin_lock_irqsave(&io
->endio_lock
, flags
);
618 if (!(io
->error
> 0 && __noflush_suspending(md
)))
620 spin_unlock_irqrestore(&io
->endio_lock
, flags
);
623 if (atomic_dec_and_test(&io
->io_count
)) {
624 if (io
->error
== DM_ENDIO_REQUEUE
) {
626 * Target requested pushing back the I/O.
628 spin_lock_irqsave(&md
->deferred_lock
, flags
);
629 if (__noflush_suspending(md
)) {
630 if (!(io
->bio
->bi_rw
& REQ_FLUSH
))
631 bio_list_add_head(&md
->deferred
,
634 /* noflush suspend was interrupted. */
636 spin_unlock_irqrestore(&md
->deferred_lock
, flags
);
639 io_error
= io
->error
;
642 if (bio
->bi_rw
& REQ_FLUSH
) {
644 * There can be just one flush request so we use
645 * a per-device variable for error reporting.
646 * Note that you can't touch the bio after end_io_acct
648 if (!md
->flush_error
)
649 md
->flush_error
= io_error
;
656 if (io_error
!= DM_ENDIO_REQUEUE
) {
657 trace_block_bio_complete(md
->queue
, bio
);
659 bio_endio(bio
, io_error
);
665 static void clone_endio(struct bio
*bio
, int error
)
668 struct dm_target_io
*tio
= bio
->bi_private
;
669 struct dm_io
*io
= tio
->io
;
670 struct mapped_device
*md
= tio
->io
->md
;
671 dm_endio_fn endio
= tio
->ti
->type
->end_io
;
673 if (!bio_flagged(bio
, BIO_UPTODATE
) && !error
)
677 r
= endio(tio
->ti
, bio
, error
, &tio
->info
);
678 if (r
< 0 || r
== DM_ENDIO_REQUEUE
)
680 * error and requeue request are handled
684 else if (r
== DM_ENDIO_INCOMPLETE
)
685 /* The target will handle the io */
688 DMWARN("unimplemented target endio return value: %d", r
);
694 * Store md for cleanup instead of tio which is about to get freed.
696 bio
->bi_private
= md
->bs
;
700 dec_pending(io
, error
);
704 * Partial completion handling for request-based dm
706 static void end_clone_bio(struct bio
*clone
, int error
)
708 struct dm_rq_clone_bio_info
*info
= clone
->bi_private
;
709 struct dm_rq_target_io
*tio
= info
->tio
;
710 struct bio
*bio
= info
->orig
;
711 unsigned int nr_bytes
= info
->orig
->bi_size
;
717 * An error has already been detected on the request.
718 * Once error occurred, just let clone->end_io() handle
724 * Don't notice the error to the upper layer yet.
725 * The error handling decision is made by the target driver,
726 * when the request is completed.
733 * I/O for the bio successfully completed.
734 * Notice the data completion to the upper layer.
738 * bios are processed from the head of the list.
739 * So the completing bio should always be rq->bio.
740 * If it's not, something wrong is happening.
742 if (tio
->orig
->bio
!= bio
)
743 DMERR("bio completion is going in the middle of the request");
746 * Update the original request.
747 * Do not use blk_end_request() here, because it may complete
748 * the original request before the clone, and break the ordering.
750 blk_update_request(tio
->orig
, 0, nr_bytes
);
753 static void store_barrier_error(struct mapped_device
*md
, int error
)
757 spin_lock_irqsave(&md
->barrier_error_lock
, flags
);
759 * Basically, the first error is taken, but:
760 * -EOPNOTSUPP supersedes any I/O error.
761 * Requeue request supersedes any I/O error but -EOPNOTSUPP.
763 if (!md
->barrier_error
|| error
== -EOPNOTSUPP
||
764 (md
->barrier_error
!= -EOPNOTSUPP
&&
765 error
== DM_ENDIO_REQUEUE
))
766 md
->barrier_error
= error
;
767 spin_unlock_irqrestore(&md
->barrier_error_lock
, flags
);
771 * Don't touch any member of the md after calling this function because
772 * the md may be freed in dm_put() at the end of this function.
773 * Or do dm_get() before calling this function and dm_put() later.
775 static void rq_completed(struct mapped_device
*md
, int rw
, int run_queue
)
777 atomic_dec(&md
->pending
[rw
]);
779 /* nudge anyone waiting on suspend queue */
780 if (!md_in_flight(md
))
784 blk_run_queue(md
->queue
);
787 * dm_put() must be at the end of this function. See the comment above
792 static void free_rq_clone(struct request
*clone
)
794 struct dm_rq_target_io
*tio
= clone
->end_io_data
;
796 blk_rq_unprep_clone(clone
);
801 * Complete the clone and the original request.
802 * Must be called without queue lock.
804 static void dm_end_request(struct request
*clone
, int error
)
806 int rw
= rq_data_dir(clone
);
808 bool is_barrier
= clone
->cmd_flags
& REQ_HARDBARRIER
;
809 struct dm_rq_target_io
*tio
= clone
->end_io_data
;
810 struct mapped_device
*md
= tio
->md
;
811 struct request
*rq
= tio
->orig
;
813 if (rq
->cmd_type
== REQ_TYPE_BLOCK_PC
&& !is_barrier
) {
814 rq
->errors
= clone
->errors
;
815 rq
->resid_len
= clone
->resid_len
;
819 * We are using the sense buffer of the original
821 * So setting the length of the sense data is enough.
823 rq
->sense_len
= clone
->sense_len
;
826 free_rq_clone(clone
);
828 if (unlikely(is_barrier
)) {
830 store_barrier_error(md
, error
);
833 blk_end_request_all(rq
, error
);
835 rq_completed(md
, rw
, run_queue
);
838 static void dm_unprep_request(struct request
*rq
)
840 struct request
*clone
= rq
->special
;
843 rq
->cmd_flags
&= ~REQ_DONTPREP
;
845 free_rq_clone(clone
);
849 * Requeue the original request of a clone.
851 void dm_requeue_unmapped_request(struct request
*clone
)
853 int rw
= rq_data_dir(clone
);
854 struct dm_rq_target_io
*tio
= clone
->end_io_data
;
855 struct mapped_device
*md
= tio
->md
;
856 struct request
*rq
= tio
->orig
;
857 struct request_queue
*q
= rq
->q
;
860 if (unlikely(clone
->cmd_flags
& REQ_HARDBARRIER
)) {
862 * Barrier clones share an original request.
863 * Leave it to dm_end_request(), which handles this special
866 dm_end_request(clone
, DM_ENDIO_REQUEUE
);
870 dm_unprep_request(rq
);
872 spin_lock_irqsave(q
->queue_lock
, flags
);
873 if (elv_queue_empty(q
))
875 blk_requeue_request(q
, rq
);
876 spin_unlock_irqrestore(q
->queue_lock
, flags
);
878 rq_completed(md
, rw
, 0);
880 EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request
);
882 static void __stop_queue(struct request_queue
*q
)
887 static void stop_queue(struct request_queue
*q
)
891 spin_lock_irqsave(q
->queue_lock
, flags
);
893 spin_unlock_irqrestore(q
->queue_lock
, flags
);
896 static void __start_queue(struct request_queue
*q
)
898 if (blk_queue_stopped(q
))
902 static void start_queue(struct request_queue
*q
)
906 spin_lock_irqsave(q
->queue_lock
, flags
);
908 spin_unlock_irqrestore(q
->queue_lock
, flags
);
911 static void dm_done(struct request
*clone
, int error
, bool mapped
)
914 struct dm_rq_target_io
*tio
= clone
->end_io_data
;
915 dm_request_endio_fn rq_end_io
= tio
->ti
->type
->rq_end_io
;
917 if (mapped
&& rq_end_io
)
918 r
= rq_end_io(tio
->ti
, clone
, error
, &tio
->info
);
921 /* The target wants to complete the I/O */
922 dm_end_request(clone
, r
);
923 else if (r
== DM_ENDIO_INCOMPLETE
)
924 /* The target will handle the I/O */
926 else if (r
== DM_ENDIO_REQUEUE
)
927 /* The target wants to requeue the I/O */
928 dm_requeue_unmapped_request(clone
);
930 DMWARN("unimplemented target endio return value: %d", r
);
936 * Request completion handler for request-based dm
938 static void dm_softirq_done(struct request
*rq
)
941 struct request
*clone
= rq
->completion_data
;
942 struct dm_rq_target_io
*tio
= clone
->end_io_data
;
944 if (rq
->cmd_flags
& REQ_FAILED
)
947 dm_done(clone
, tio
->error
, mapped
);
951 * Complete the clone and the original request with the error status
952 * through softirq context.
954 static void dm_complete_request(struct request
*clone
, int error
)
956 struct dm_rq_target_io
*tio
= clone
->end_io_data
;
957 struct request
*rq
= tio
->orig
;
959 if (unlikely(clone
->cmd_flags
& REQ_HARDBARRIER
)) {
961 * Barrier clones share an original request. So can't use
962 * softirq_done with the original.
963 * Pass the clone to dm_done() directly in this special case.
964 * It is safe (even if clone->q->queue_lock is held here)
965 * because there is no I/O dispatching during the completion
968 dm_done(clone
, error
, true);
973 rq
->completion_data
= clone
;
974 blk_complete_request(rq
);
978 * Complete the not-mapped clone and the original request with the error status
979 * through softirq context.
980 * Target's rq_end_io() function isn't called.
981 * This may be used when the target's map_rq() function fails.
983 void dm_kill_unmapped_request(struct request
*clone
, int error
)
985 struct dm_rq_target_io
*tio
= clone
->end_io_data
;
986 struct request
*rq
= tio
->orig
;
988 if (unlikely(clone
->cmd_flags
& REQ_HARDBARRIER
)) {
990 * Barrier clones share an original request.
991 * Leave it to dm_end_request(), which handles this special
995 dm_end_request(clone
, error
);
999 rq
->cmd_flags
|= REQ_FAILED
;
1000 dm_complete_request(clone
, error
);
1002 EXPORT_SYMBOL_GPL(dm_kill_unmapped_request
);
1005 * Called with the queue lock held
1007 static void end_clone_request(struct request
*clone
, int error
)
1010 * For just cleaning up the information of the queue in which
1011 * the clone was dispatched.
1012 * The clone is *NOT* freed actually here because it is alloced from
1013 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
1015 __blk_put_request(clone
->q
, clone
);
1018 * Actual request completion is done in a softirq context which doesn't
1019 * hold the queue lock. Otherwise, deadlock could occur because:
1020 * - another request may be submitted by the upper level driver
1021 * of the stacking during the completion
1022 * - the submission which requires queue lock may be done
1023 * against this queue
1025 dm_complete_request(clone
, error
);
1029 * Return maximum size of I/O possible at the supplied sector up to the current
1032 static sector_t
max_io_len_target_boundary(sector_t sector
, struct dm_target
*ti
)
1034 sector_t target_offset
= dm_target_offset(ti
, sector
);
1036 return ti
->len
- target_offset
;
1039 static sector_t
max_io_len(sector_t sector
, struct dm_target
*ti
)
1041 sector_t len
= max_io_len_target_boundary(sector
, ti
);
1044 * Does the target need to split even further ?
1048 sector_t offset
= dm_target_offset(ti
, sector
);
1049 boundary
= ((offset
+ ti
->split_io
) & ~(ti
->split_io
- 1))
1058 static void __map_bio(struct dm_target
*ti
, struct bio
*clone
,
1059 struct dm_target_io
*tio
)
1063 struct mapped_device
*md
;
1065 clone
->bi_end_io
= clone_endio
;
1066 clone
->bi_private
= tio
;
1069 * Map the clone. If r == 0 we don't need to do
1070 * anything, the target has assumed ownership of
1073 atomic_inc(&tio
->io
->io_count
);
1074 sector
= clone
->bi_sector
;
1075 r
= ti
->type
->map(ti
, clone
, &tio
->info
);
1076 if (r
== DM_MAPIO_REMAPPED
) {
1077 /* the bio has been remapped so dispatch it */
1079 trace_block_remap(bdev_get_queue(clone
->bi_bdev
), clone
,
1080 tio
->io
->bio
->bi_bdev
->bd_dev
, sector
);
1082 generic_make_request(clone
);
1083 } else if (r
< 0 || r
== DM_MAPIO_REQUEUE
) {
1084 /* error the io and bail out, or requeue it if needed */
1086 dec_pending(tio
->io
, r
);
1088 * Store bio_set for cleanup.
1090 clone
->bi_private
= md
->bs
;
1094 DMWARN("unimplemented target map return value: %d", r
);
1100 struct mapped_device
*md
;
1101 struct dm_table
*map
;
1105 sector_t sector_count
;
1109 static void dm_bio_destructor(struct bio
*bio
)
1111 struct bio_set
*bs
= bio
->bi_private
;
1117 * Creates a little bio that just does part of a bvec.
1119 static struct bio
*split_bvec(struct bio
*bio
, sector_t sector
,
1120 unsigned short idx
, unsigned int offset
,
1121 unsigned int len
, struct bio_set
*bs
)
1124 struct bio_vec
*bv
= bio
->bi_io_vec
+ idx
;
1126 clone
= bio_alloc_bioset(GFP_NOIO
, 1, bs
);
1127 clone
->bi_destructor
= dm_bio_destructor
;
1128 *clone
->bi_io_vec
= *bv
;
1130 clone
->bi_sector
= sector
;
1131 clone
->bi_bdev
= bio
->bi_bdev
;
1132 clone
->bi_rw
= bio
->bi_rw
;
1134 clone
->bi_size
= to_bytes(len
);
1135 clone
->bi_io_vec
->bv_offset
= offset
;
1136 clone
->bi_io_vec
->bv_len
= clone
->bi_size
;
1137 clone
->bi_flags
|= 1 << BIO_CLONED
;
1139 if (bio_integrity(bio
)) {
1140 bio_integrity_clone(clone
, bio
, GFP_NOIO
, bs
);
1141 bio_integrity_trim(clone
,
1142 bio_sector_offset(bio
, idx
, offset
), len
);
1149 * Creates a bio that consists of range of complete bvecs.
1151 static struct bio
*clone_bio(struct bio
*bio
, sector_t sector
,
1152 unsigned short idx
, unsigned short bv_count
,
1153 unsigned int len
, struct bio_set
*bs
)
1157 clone
= bio_alloc_bioset(GFP_NOIO
, bio
->bi_max_vecs
, bs
);
1158 __bio_clone(clone
, bio
);
1159 clone
->bi_destructor
= dm_bio_destructor
;
1160 clone
->bi_sector
= sector
;
1161 clone
->bi_idx
= idx
;
1162 clone
->bi_vcnt
= idx
+ bv_count
;
1163 clone
->bi_size
= to_bytes(len
);
1164 clone
->bi_flags
&= ~(1 << BIO_SEG_VALID
);
1166 if (bio_integrity(bio
)) {
1167 bio_integrity_clone(clone
, bio
, GFP_NOIO
, bs
);
1169 if (idx
!= bio
->bi_idx
|| clone
->bi_size
< bio
->bi_size
)
1170 bio_integrity_trim(clone
,
1171 bio_sector_offset(bio
, idx
, 0), len
);
1177 static struct dm_target_io
*alloc_tio(struct clone_info
*ci
,
1178 struct dm_target
*ti
)
1180 struct dm_target_io
*tio
= mempool_alloc(ci
->md
->tio_pool
, GFP_NOIO
);
1184 memset(&tio
->info
, 0, sizeof(tio
->info
));
1189 static void __issue_target_request(struct clone_info
*ci
, struct dm_target
*ti
,
1190 unsigned request_nr
, sector_t len
)
1192 struct dm_target_io
*tio
= alloc_tio(ci
, ti
);
1195 tio
->info
.target_request_nr
= request_nr
;
1198 * Discard requests require the bio's inline iovecs be initialized.
1199 * ci->bio->bi_max_vecs is BIO_INLINE_VECS anyway, for both flush
1200 * and discard, so no need for concern about wasted bvec allocations.
1202 clone
= bio_alloc_bioset(GFP_NOIO
, ci
->bio
->bi_max_vecs
, ci
->md
->bs
);
1203 __bio_clone(clone
, ci
->bio
);
1204 clone
->bi_destructor
= dm_bio_destructor
;
1206 clone
->bi_sector
= ci
->sector
;
1207 clone
->bi_size
= to_bytes(len
);
1210 __map_bio(ti
, clone
, tio
);
1213 static void __issue_target_requests(struct clone_info
*ci
, struct dm_target
*ti
,
1214 unsigned num_requests
, sector_t len
)
1216 unsigned request_nr
;
1218 for (request_nr
= 0; request_nr
< num_requests
; request_nr
++)
1219 __issue_target_request(ci
, ti
, request_nr
, len
);
1222 static int __clone_and_map_flush(struct clone_info
*ci
)
1224 unsigned target_nr
= 0;
1225 struct dm_target
*ti
;
1227 while ((ti
= dm_table_get_target(ci
->map
, target_nr
++)))
1228 __issue_target_requests(ci
, ti
, ti
->num_flush_requests
, 0);
1230 ci
->sector_count
= 0;
1236 * Perform all io with a single clone.
1238 static void __clone_and_map_simple(struct clone_info
*ci
, struct dm_target
*ti
)
1240 struct bio
*clone
, *bio
= ci
->bio
;
1241 struct dm_target_io
*tio
;
1243 tio
= alloc_tio(ci
, ti
);
1244 clone
= clone_bio(bio
, ci
->sector
, ci
->idx
,
1245 bio
->bi_vcnt
- ci
->idx
, ci
->sector_count
,
1247 __map_bio(ti
, clone
, tio
);
1248 ci
->sector_count
= 0;
1251 static int __clone_and_map_discard(struct clone_info
*ci
)
1253 struct dm_target
*ti
;
1257 ti
= dm_table_find_target(ci
->map
, ci
->sector
);
1258 if (!dm_target_is_valid(ti
))
1262 * Even though the device advertised discard support,
1263 * reconfiguration might have changed that since the
1264 * check was performed.
1266 if (!ti
->num_discard_requests
)
1269 len
= min(ci
->sector_count
, max_io_len_target_boundary(ci
->sector
, ti
));
1271 __issue_target_requests(ci
, ti
, ti
->num_discard_requests
, len
);
1274 } while (ci
->sector_count
-= len
);
1279 static int __clone_and_map(struct clone_info
*ci
)
1281 struct bio
*clone
, *bio
= ci
->bio
;
1282 struct dm_target
*ti
;
1283 sector_t len
= 0, max
;
1284 struct dm_target_io
*tio
;
1286 if (unlikely(bio
->bi_rw
& REQ_DISCARD
))
1287 return __clone_and_map_discard(ci
);
1289 ti
= dm_table_find_target(ci
->map
, ci
->sector
);
1290 if (!dm_target_is_valid(ti
))
1293 max
= max_io_len(ci
->sector
, ti
);
1295 if (ci
->sector_count
<= max
) {
1297 * Optimise for the simple case where we can do all of
1298 * the remaining io with a single clone.
1300 __clone_and_map_simple(ci
, ti
);
1302 } else if (to_sector(bio
->bi_io_vec
[ci
->idx
].bv_len
) <= max
) {
1304 * There are some bvecs that don't span targets.
1305 * Do as many of these as possible.
1308 sector_t remaining
= max
;
1311 for (i
= ci
->idx
; remaining
&& (i
< bio
->bi_vcnt
); i
++) {
1312 bv_len
= to_sector(bio
->bi_io_vec
[i
].bv_len
);
1314 if (bv_len
> remaining
)
1317 remaining
-= bv_len
;
1321 tio
= alloc_tio(ci
, ti
);
1322 clone
= clone_bio(bio
, ci
->sector
, ci
->idx
, i
- ci
->idx
, len
,
1324 __map_bio(ti
, clone
, tio
);
1327 ci
->sector_count
-= len
;
1332 * Handle a bvec that must be split between two or more targets.
1334 struct bio_vec
*bv
= bio
->bi_io_vec
+ ci
->idx
;
1335 sector_t remaining
= to_sector(bv
->bv_len
);
1336 unsigned int offset
= 0;
1340 ti
= dm_table_find_target(ci
->map
, ci
->sector
);
1341 if (!dm_target_is_valid(ti
))
1344 max
= max_io_len(ci
->sector
, ti
);
1347 len
= min(remaining
, max
);
1349 tio
= alloc_tio(ci
, ti
);
1350 clone
= split_bvec(bio
, ci
->sector
, ci
->idx
,
1351 bv
->bv_offset
+ offset
, len
,
1354 __map_bio(ti
, clone
, tio
);
1357 ci
->sector_count
-= len
;
1358 offset
+= to_bytes(len
);
1359 } while (remaining
-= len
);
1368 * Split the bio into several clones and submit it to targets.
1370 static void __split_and_process_bio(struct mapped_device
*md
, struct bio
*bio
)
1372 struct clone_info ci
;
1375 ci
.map
= dm_get_live_table(md
);
1376 if (unlikely(!ci
.map
)) {
1377 if (!(bio
->bi_rw
& REQ_FLUSH
))
1380 if (!md
->flush_error
)
1381 md
->flush_error
= -EIO
;
1387 ci
.io
= alloc_io(md
);
1389 atomic_set(&ci
.io
->io_count
, 1);
1392 spin_lock_init(&ci
.io
->endio_lock
);
1393 ci
.sector
= bio
->bi_sector
;
1394 if (!(bio
->bi_rw
& REQ_FLUSH
))
1395 ci
.sector_count
= bio_sectors(bio
);
1397 /* all FLUSH bio's reaching here should be empty */
1398 WARN_ON_ONCE(bio_has_data(bio
));
1399 ci
.sector_count
= 1;
1401 ci
.idx
= bio
->bi_idx
;
1403 start_io_acct(ci
.io
);
1404 while (ci
.sector_count
&& !error
) {
1405 if (!(bio
->bi_rw
& REQ_FLUSH
))
1406 error
= __clone_and_map(&ci
);
1408 error
= __clone_and_map_flush(&ci
);
1411 /* drop the extra reference count */
1412 dec_pending(ci
.io
, error
);
1413 dm_table_put(ci
.map
);
1415 /*-----------------------------------------------------------------
1417 *---------------------------------------------------------------*/
1419 static int dm_merge_bvec(struct request_queue
*q
,
1420 struct bvec_merge_data
*bvm
,
1421 struct bio_vec
*biovec
)
1423 struct mapped_device
*md
= q
->queuedata
;
1424 struct dm_table
*map
= dm_get_live_table(md
);
1425 struct dm_target
*ti
;
1426 sector_t max_sectors
;
1432 ti
= dm_table_find_target(map
, bvm
->bi_sector
);
1433 if (!dm_target_is_valid(ti
))
1437 * Find maximum amount of I/O that won't need splitting
1439 max_sectors
= min(max_io_len(bvm
->bi_sector
, ti
),
1440 (sector_t
) BIO_MAX_SECTORS
);
1441 max_size
= (max_sectors
<< SECTOR_SHIFT
) - bvm
->bi_size
;
1446 * merge_bvec_fn() returns number of bytes
1447 * it can accept at this offset
1448 * max is precomputed maximal io size
1450 if (max_size
&& ti
->type
->merge
)
1451 max_size
= ti
->type
->merge(ti
, bvm
, biovec
, max_size
);
1453 * If the target doesn't support merge method and some of the devices
1454 * provided their merge_bvec method (we know this by looking at
1455 * queue_max_hw_sectors), then we can't allow bios with multiple vector
1456 * entries. So always set max_size to 0, and the code below allows
1459 else if (queue_max_hw_sectors(q
) <= PAGE_SIZE
>> 9)
1468 * Always allow an entire first page
1470 if (max_size
<= biovec
->bv_len
&& !(bvm
->bi_size
>> SECTOR_SHIFT
))
1471 max_size
= biovec
->bv_len
;
1477 * The request function that just remaps the bio built up by
1480 static int _dm_request(struct request_queue
*q
, struct bio
*bio
)
1482 int rw
= bio_data_dir(bio
);
1483 struct mapped_device
*md
= q
->queuedata
;
1486 down_read(&md
->io_lock
);
1488 cpu
= part_stat_lock();
1489 part_stat_inc(cpu
, &dm_disk(md
)->part0
, ios
[rw
]);
1490 part_stat_add(cpu
, &dm_disk(md
)->part0
, sectors
[rw
], bio_sectors(bio
));
1494 * If we're suspended or the thread is processing flushes
1495 * we have to queue this io for later.
1497 if (unlikely(test_bit(DMF_QUEUE_IO_TO_THREAD
, &md
->flags
)) ||
1498 (bio
->bi_rw
& REQ_FLUSH
)) {
1499 up_read(&md
->io_lock
);
1501 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND
, &md
->flags
)) &&
1502 bio_rw(bio
) == READA
) {
1512 __split_and_process_bio(md
, bio
);
1513 up_read(&md
->io_lock
);
1517 static int dm_make_request(struct request_queue
*q
, struct bio
*bio
)
1519 struct mapped_device
*md
= q
->queuedata
;
1521 return md
->saved_make_request_fn(q
, bio
); /* call __make_request() */
1524 static int dm_request_based(struct mapped_device
*md
)
1526 return blk_queue_stackable(md
->queue
);
1529 static int dm_request(struct request_queue
*q
, struct bio
*bio
)
1531 struct mapped_device
*md
= q
->queuedata
;
1533 if (dm_request_based(md
))
1534 return dm_make_request(q
, bio
);
1536 return _dm_request(q
, bio
);
1539 static bool dm_rq_is_flush_request(struct request
*rq
)
1541 if (rq
->cmd_flags
& REQ_FLUSH
)
1547 void dm_dispatch_request(struct request
*rq
)
1551 if (blk_queue_io_stat(rq
->q
))
1552 rq
->cmd_flags
|= REQ_IO_STAT
;
1554 rq
->start_time
= jiffies
;
1555 r
= blk_insert_cloned_request(rq
->q
, rq
);
1557 dm_complete_request(rq
, r
);
1559 EXPORT_SYMBOL_GPL(dm_dispatch_request
);
1561 static void dm_rq_bio_destructor(struct bio
*bio
)
1563 struct dm_rq_clone_bio_info
*info
= bio
->bi_private
;
1564 struct mapped_device
*md
= info
->tio
->md
;
1566 free_bio_info(info
);
1567 bio_free(bio
, md
->bs
);
1570 static int dm_rq_bio_constructor(struct bio
*bio
, struct bio
*bio_orig
,
1573 struct dm_rq_target_io
*tio
= data
;
1574 struct mapped_device
*md
= tio
->md
;
1575 struct dm_rq_clone_bio_info
*info
= alloc_bio_info(md
);
1580 info
->orig
= bio_orig
;
1582 bio
->bi_end_io
= end_clone_bio
;
1583 bio
->bi_private
= info
;
1584 bio
->bi_destructor
= dm_rq_bio_destructor
;
1589 static int setup_clone(struct request
*clone
, struct request
*rq
,
1590 struct dm_rq_target_io
*tio
)
1594 if (dm_rq_is_flush_request(rq
)) {
1595 blk_rq_init(NULL
, clone
);
1596 clone
->cmd_type
= REQ_TYPE_FS
;
1597 clone
->cmd_flags
|= (REQ_HARDBARRIER
| WRITE
);
1599 r
= blk_rq_prep_clone(clone
, rq
, tio
->md
->bs
, GFP_ATOMIC
,
1600 dm_rq_bio_constructor
, tio
);
1604 clone
->cmd
= rq
->cmd
;
1605 clone
->cmd_len
= rq
->cmd_len
;
1606 clone
->sense
= rq
->sense
;
1607 clone
->buffer
= rq
->buffer
;
1610 clone
->end_io
= end_clone_request
;
1611 clone
->end_io_data
= tio
;
1616 static struct request
*clone_rq(struct request
*rq
, struct mapped_device
*md
,
1619 struct request
*clone
;
1620 struct dm_rq_target_io
*tio
;
1622 tio
= alloc_rq_tio(md
, gfp_mask
);
1630 memset(&tio
->info
, 0, sizeof(tio
->info
));
1632 clone
= &tio
->clone
;
1633 if (setup_clone(clone
, rq
, tio
)) {
1643 * Called with the queue lock held.
1645 static int dm_prep_fn(struct request_queue
*q
, struct request
*rq
)
1647 struct mapped_device
*md
= q
->queuedata
;
1648 struct request
*clone
;
1650 if (unlikely(dm_rq_is_flush_request(rq
)))
1653 if (unlikely(rq
->special
)) {
1654 DMWARN("Already has something in rq->special.");
1655 return BLKPREP_KILL
;
1658 clone
= clone_rq(rq
, md
, GFP_ATOMIC
);
1660 return BLKPREP_DEFER
;
1662 rq
->special
= clone
;
1663 rq
->cmd_flags
|= REQ_DONTPREP
;
1670 * 0 : the request has been processed (not requeued)
1671 * !0 : the request has been requeued
1673 static int map_request(struct dm_target
*ti
, struct request
*clone
,
1674 struct mapped_device
*md
)
1676 int r
, requeued
= 0;
1677 struct dm_rq_target_io
*tio
= clone
->end_io_data
;
1680 * Hold the md reference here for the in-flight I/O.
1681 * We can't rely on the reference count by device opener,
1682 * because the device may be closed during the request completion
1683 * when all bios are completed.
1684 * See the comment in rq_completed() too.
1689 r
= ti
->type
->map_rq(ti
, clone
, &tio
->info
);
1691 case DM_MAPIO_SUBMITTED
:
1692 /* The target has taken the I/O to submit by itself later */
1694 case DM_MAPIO_REMAPPED
:
1695 /* The target has remapped the I/O so dispatch it */
1696 trace_block_rq_remap(clone
->q
, clone
, disk_devt(dm_disk(md
)),
1697 blk_rq_pos(tio
->orig
));
1698 dm_dispatch_request(clone
);
1700 case DM_MAPIO_REQUEUE
:
1701 /* The target wants to requeue the I/O */
1702 dm_requeue_unmapped_request(clone
);
1707 DMWARN("unimplemented target map return value: %d", r
);
1711 /* The target wants to complete the I/O */
1712 dm_kill_unmapped_request(clone
, r
);
1720 * q->request_fn for request-based dm.
1721 * Called with the queue lock held.
1723 static void dm_request_fn(struct request_queue
*q
)
1725 struct mapped_device
*md
= q
->queuedata
;
1726 struct dm_table
*map
= dm_get_live_table(md
);
1727 struct dm_target
*ti
;
1728 struct request
*rq
, *clone
;
1731 * For suspend, check blk_queue_stopped() and increment
1732 * ->pending within a single queue_lock not to increment the
1733 * number of in-flight I/Os after the queue is stopped in
1736 while (!blk_queue_plugged(q
) && !blk_queue_stopped(q
)) {
1737 rq
= blk_peek_request(q
);
1741 if (unlikely(dm_rq_is_flush_request(rq
))) {
1742 BUG_ON(md
->flush_request
);
1743 md
->flush_request
= rq
;
1744 blk_start_request(rq
);
1745 queue_work(md
->wq
, &md
->barrier_work
);
1749 ti
= dm_table_find_target(map
, blk_rq_pos(rq
));
1750 if (ti
->type
->busy
&& ti
->type
->busy(ti
))
1753 blk_start_request(rq
);
1754 clone
= rq
->special
;
1755 atomic_inc(&md
->pending
[rq_data_dir(clone
)]);
1757 spin_unlock(q
->queue_lock
);
1758 if (map_request(ti
, clone
, md
))
1761 spin_lock_irq(q
->queue_lock
);
1767 spin_lock_irq(q
->queue_lock
);
1770 if (!elv_queue_empty(q
))
1771 /* Some requests still remain, retry later */
1780 int dm_underlying_device_busy(struct request_queue
*q
)
1782 return blk_lld_busy(q
);
1784 EXPORT_SYMBOL_GPL(dm_underlying_device_busy
);
1786 static int dm_lld_busy(struct request_queue
*q
)
1789 struct mapped_device
*md
= q
->queuedata
;
1790 struct dm_table
*map
= dm_get_live_table(md
);
1792 if (!map
|| test_bit(DMF_BLOCK_IO_FOR_SUSPEND
, &md
->flags
))
1795 r
= dm_table_any_busy_target(map
);
1802 static void dm_unplug_all(struct request_queue
*q
)
1804 struct mapped_device
*md
= q
->queuedata
;
1805 struct dm_table
*map
= dm_get_live_table(md
);
1808 if (dm_request_based(md
))
1809 generic_unplug_device(q
);
1811 dm_table_unplug_all(map
);
1816 static int dm_any_congested(void *congested_data
, int bdi_bits
)
1819 struct mapped_device
*md
= congested_data
;
1820 struct dm_table
*map
;
1822 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND
, &md
->flags
)) {
1823 map
= dm_get_live_table(md
);
1826 * Request-based dm cares about only own queue for
1827 * the query about congestion status of request_queue
1829 if (dm_request_based(md
))
1830 r
= md
->queue
->backing_dev_info
.state
&
1833 r
= dm_table_any_congested(map
, bdi_bits
);
1842 /*-----------------------------------------------------------------
1843 * An IDR is used to keep track of allocated minor numbers.
1844 *---------------------------------------------------------------*/
1845 static DEFINE_IDR(_minor_idr
);
1847 static void free_minor(int minor
)
1849 spin_lock(&_minor_lock
);
1850 idr_remove(&_minor_idr
, minor
);
1851 spin_unlock(&_minor_lock
);
1855 * See if the device with a specific minor # is free.
1857 static int specific_minor(int minor
)
1861 if (minor
>= (1 << MINORBITS
))
1864 r
= idr_pre_get(&_minor_idr
, GFP_KERNEL
);
1868 spin_lock(&_minor_lock
);
1870 if (idr_find(&_minor_idr
, minor
)) {
1875 r
= idr_get_new_above(&_minor_idr
, MINOR_ALLOCED
, minor
, &m
);
1880 idr_remove(&_minor_idr
, m
);
1886 spin_unlock(&_minor_lock
);
1890 static int next_free_minor(int *minor
)
1894 r
= idr_pre_get(&_minor_idr
, GFP_KERNEL
);
1898 spin_lock(&_minor_lock
);
1900 r
= idr_get_new(&_minor_idr
, MINOR_ALLOCED
, &m
);
1904 if (m
>= (1 << MINORBITS
)) {
1905 idr_remove(&_minor_idr
, m
);
1913 spin_unlock(&_minor_lock
);
1917 static const struct block_device_operations dm_blk_dops
;
1919 static void dm_wq_work(struct work_struct
*work
);
1920 static void dm_rq_barrier_work(struct work_struct
*work
);
1922 static void dm_init_md_queue(struct mapped_device
*md
)
1925 * Request-based dm devices cannot be stacked on top of bio-based dm
1926 * devices. The type of this dm device has not been decided yet.
1927 * The type is decided at the first table loading time.
1928 * To prevent problematic device stacking, clear the queue flag
1929 * for request stacking support until then.
1931 * This queue is new, so no concurrency on the queue_flags.
1933 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE
, md
->queue
);
1935 md
->queue
->queuedata
= md
;
1936 md
->queue
->backing_dev_info
.congested_fn
= dm_any_congested
;
1937 md
->queue
->backing_dev_info
.congested_data
= md
;
1938 blk_queue_make_request(md
->queue
, dm_request
);
1939 blk_queue_bounce_limit(md
->queue
, BLK_BOUNCE_ANY
);
1940 md
->queue
->unplug_fn
= dm_unplug_all
;
1941 blk_queue_merge_bvec(md
->queue
, dm_merge_bvec
);
1942 blk_queue_flush(md
->queue
, REQ_FLUSH
| REQ_FUA
);
1946 * Allocate and initialise a blank device with a given minor.
1948 static struct mapped_device
*alloc_dev(int minor
)
1951 struct mapped_device
*md
= kzalloc(sizeof(*md
), GFP_KERNEL
);
1955 DMWARN("unable to allocate device, out of memory.");
1959 if (!try_module_get(THIS_MODULE
))
1960 goto bad_module_get
;
1962 /* get a minor number for the dev */
1963 if (minor
== DM_ANY_MINOR
)
1964 r
= next_free_minor(&minor
);
1966 r
= specific_minor(minor
);
1970 md
->type
= DM_TYPE_NONE
;
1971 init_rwsem(&md
->io_lock
);
1972 mutex_init(&md
->suspend_lock
);
1973 mutex_init(&md
->type_lock
);
1974 spin_lock_init(&md
->deferred_lock
);
1975 spin_lock_init(&md
->barrier_error_lock
);
1976 rwlock_init(&md
->map_lock
);
1977 atomic_set(&md
->holders
, 1);
1978 atomic_set(&md
->open_count
, 0);
1979 atomic_set(&md
->event_nr
, 0);
1980 atomic_set(&md
->uevent_seq
, 0);
1981 INIT_LIST_HEAD(&md
->uevent_list
);
1982 spin_lock_init(&md
->uevent_lock
);
1984 md
->queue
= blk_alloc_queue(GFP_KERNEL
);
1988 dm_init_md_queue(md
);
1990 md
->disk
= alloc_disk(1);
1994 atomic_set(&md
->pending
[0], 0);
1995 atomic_set(&md
->pending
[1], 0);
1996 init_waitqueue_head(&md
->wait
);
1997 INIT_WORK(&md
->work
, dm_wq_work
);
1998 INIT_WORK(&md
->barrier_work
, dm_rq_barrier_work
);
1999 init_waitqueue_head(&md
->eventq
);
2001 md
->disk
->major
= _major
;
2002 md
->disk
->first_minor
= minor
;
2003 md
->disk
->fops
= &dm_blk_dops
;
2004 md
->disk
->queue
= md
->queue
;
2005 md
->disk
->private_data
= md
;
2006 sprintf(md
->disk
->disk_name
, "dm-%d", minor
);
2008 format_dev_t(md
->name
, MKDEV(_major
, minor
));
2010 md
->wq
= create_singlethread_workqueue("kdmflush");
2014 md
->bdev
= bdget_disk(md
->disk
, 0);
2018 /* Populate the mapping, nobody knows we exist yet */
2019 spin_lock(&_minor_lock
);
2020 old_md
= idr_replace(&_minor_idr
, md
, minor
);
2021 spin_unlock(&_minor_lock
);
2023 BUG_ON(old_md
!= MINOR_ALLOCED
);
2028 destroy_workqueue(md
->wq
);
2030 del_gendisk(md
->disk
);
2033 blk_cleanup_queue(md
->queue
);
2037 module_put(THIS_MODULE
);
2043 static void unlock_fs(struct mapped_device
*md
);
2045 static void free_dev(struct mapped_device
*md
)
2047 int minor
= MINOR(disk_devt(md
->disk
));
2051 destroy_workqueue(md
->wq
);
2053 mempool_destroy(md
->tio_pool
);
2055 mempool_destroy(md
->io_pool
);
2057 bioset_free(md
->bs
);
2058 blk_integrity_unregister(md
->disk
);
2059 del_gendisk(md
->disk
);
2062 spin_lock(&_minor_lock
);
2063 md
->disk
->private_data
= NULL
;
2064 spin_unlock(&_minor_lock
);
2067 blk_cleanup_queue(md
->queue
);
2068 module_put(THIS_MODULE
);
2072 static void __bind_mempools(struct mapped_device
*md
, struct dm_table
*t
)
2074 struct dm_md_mempools
*p
;
2076 if (md
->io_pool
&& md
->tio_pool
&& md
->bs
)
2077 /* the md already has necessary mempools */
2080 p
= dm_table_get_md_mempools(t
);
2081 BUG_ON(!p
|| md
->io_pool
|| md
->tio_pool
|| md
->bs
);
2083 md
->io_pool
= p
->io_pool
;
2085 md
->tio_pool
= p
->tio_pool
;
2091 /* mempool bind completed, now no need any mempools in the table */
2092 dm_table_free_md_mempools(t
);
2096 * Bind a table to the device.
2098 static void event_callback(void *context
)
2100 unsigned long flags
;
2102 struct mapped_device
*md
= (struct mapped_device
*) context
;
2104 spin_lock_irqsave(&md
->uevent_lock
, flags
);
2105 list_splice_init(&md
->uevent_list
, &uevents
);
2106 spin_unlock_irqrestore(&md
->uevent_lock
, flags
);
2108 dm_send_uevents(&uevents
, &disk_to_dev(md
->disk
)->kobj
);
2110 atomic_inc(&md
->event_nr
);
2111 wake_up(&md
->eventq
);
2114 static void __set_size(struct mapped_device
*md
, sector_t size
)
2116 set_capacity(md
->disk
, size
);
2118 mutex_lock(&md
->bdev
->bd_inode
->i_mutex
);
2119 i_size_write(md
->bdev
->bd_inode
, (loff_t
)size
<< SECTOR_SHIFT
);
2120 mutex_unlock(&md
->bdev
->bd_inode
->i_mutex
);
2124 * Returns old map, which caller must destroy.
2126 static struct dm_table
*__bind(struct mapped_device
*md
, struct dm_table
*t
,
2127 struct queue_limits
*limits
)
2129 struct dm_table
*old_map
;
2130 struct request_queue
*q
= md
->queue
;
2132 unsigned long flags
;
2134 size
= dm_table_get_size(t
);
2137 * Wipe any geometry if the size of the table changed.
2139 if (size
!= get_capacity(md
->disk
))
2140 memset(&md
->geometry
, 0, sizeof(md
->geometry
));
2142 __set_size(md
, size
);
2144 dm_table_event_callback(t
, event_callback
, md
);
2147 * The queue hasn't been stopped yet, if the old table type wasn't
2148 * for request-based during suspension. So stop it to prevent
2149 * I/O mapping before resume.
2150 * This must be done before setting the queue restrictions,
2151 * because request-based dm may be run just after the setting.
2153 if (dm_table_request_based(t
) && !blk_queue_stopped(q
))
2156 __bind_mempools(md
, t
);
2158 write_lock_irqsave(&md
->map_lock
, flags
);
2161 dm_table_set_restrictions(t
, q
, limits
);
2162 write_unlock_irqrestore(&md
->map_lock
, flags
);
2168 * Returns unbound table for the caller to free.
2170 static struct dm_table
*__unbind(struct mapped_device
*md
)
2172 struct dm_table
*map
= md
->map
;
2173 unsigned long flags
;
2178 dm_table_event_callback(map
, NULL
, NULL
);
2179 write_lock_irqsave(&md
->map_lock
, flags
);
2181 write_unlock_irqrestore(&md
->map_lock
, flags
);
2187 * Constructor for a new device.
2189 int dm_create(int minor
, struct mapped_device
**result
)
2191 struct mapped_device
*md
;
2193 md
= alloc_dev(minor
);
2204 * Functions to manage md->type.
2205 * All are required to hold md->type_lock.
2207 void dm_lock_md_type(struct mapped_device
*md
)
2209 mutex_lock(&md
->type_lock
);
2212 void dm_unlock_md_type(struct mapped_device
*md
)
2214 mutex_unlock(&md
->type_lock
);
2217 void dm_set_md_type(struct mapped_device
*md
, unsigned type
)
2222 unsigned dm_get_md_type(struct mapped_device
*md
)
2228 * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
2230 static int dm_init_request_based_queue(struct mapped_device
*md
)
2232 struct request_queue
*q
= NULL
;
2234 if (md
->queue
->elevator
)
2237 /* Fully initialize the queue */
2238 q
= blk_init_allocated_queue(md
->queue
, dm_request_fn
, NULL
);
2243 md
->saved_make_request_fn
= md
->queue
->make_request_fn
;
2244 dm_init_md_queue(md
);
2245 blk_queue_softirq_done(md
->queue
, dm_softirq_done
);
2246 blk_queue_prep_rq(md
->queue
, dm_prep_fn
);
2247 blk_queue_lld_busy(md
->queue
, dm_lld_busy
);
2248 /* no flush support for request based dm yet */
2249 blk_queue_flush(md
->queue
, 0);
2251 elv_register_queue(md
->queue
);
2257 * Setup the DM device's queue based on md's type
2259 int dm_setup_md_queue(struct mapped_device
*md
)
2261 if ((dm_get_md_type(md
) == DM_TYPE_REQUEST_BASED
) &&
2262 !dm_init_request_based_queue(md
)) {
2263 DMWARN("Cannot initialize queue for request-based mapped device");
2270 static struct mapped_device
*dm_find_md(dev_t dev
)
2272 struct mapped_device
*md
;
2273 unsigned minor
= MINOR(dev
);
2275 if (MAJOR(dev
) != _major
|| minor
>= (1 << MINORBITS
))
2278 spin_lock(&_minor_lock
);
2280 md
= idr_find(&_minor_idr
, minor
);
2281 if (md
&& (md
== MINOR_ALLOCED
||
2282 (MINOR(disk_devt(dm_disk(md
))) != minor
) ||
2283 dm_deleting_md(md
) ||
2284 test_bit(DMF_FREEING
, &md
->flags
))) {
2290 spin_unlock(&_minor_lock
);
2295 struct mapped_device
*dm_get_md(dev_t dev
)
2297 struct mapped_device
*md
= dm_find_md(dev
);
2305 void *dm_get_mdptr(struct mapped_device
*md
)
2307 return md
->interface_ptr
;
2310 void dm_set_mdptr(struct mapped_device
*md
, void *ptr
)
2312 md
->interface_ptr
= ptr
;
2315 void dm_get(struct mapped_device
*md
)
2317 atomic_inc(&md
->holders
);
2318 BUG_ON(test_bit(DMF_FREEING
, &md
->flags
));
2321 const char *dm_device_name(struct mapped_device
*md
)
2325 EXPORT_SYMBOL_GPL(dm_device_name
);
2327 static void __dm_destroy(struct mapped_device
*md
, bool wait
)
2329 struct dm_table
*map
;
2333 spin_lock(&_minor_lock
);
2334 map
= dm_get_live_table(md
);
2335 idr_replace(&_minor_idr
, MINOR_ALLOCED
, MINOR(disk_devt(dm_disk(md
))));
2336 set_bit(DMF_FREEING
, &md
->flags
);
2337 spin_unlock(&_minor_lock
);
2339 if (!dm_suspended_md(md
)) {
2340 dm_table_presuspend_targets(map
);
2341 dm_table_postsuspend_targets(map
);
2345 * Rare, but there may be I/O requests still going to complete,
2346 * for example. Wait for all references to disappear.
2347 * No one should increment the reference count of the mapped_device,
2348 * after the mapped_device state becomes DMF_FREEING.
2351 while (atomic_read(&md
->holders
))
2353 else if (atomic_read(&md
->holders
))
2354 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2355 dm_device_name(md
), atomic_read(&md
->holders
));
2359 dm_table_destroy(__unbind(md
));
2363 void dm_destroy(struct mapped_device
*md
)
2365 __dm_destroy(md
, true);
2368 void dm_destroy_immediate(struct mapped_device
*md
)
2370 __dm_destroy(md
, false);
2373 void dm_put(struct mapped_device
*md
)
2375 atomic_dec(&md
->holders
);
2377 EXPORT_SYMBOL_GPL(dm_put
);
2379 static int dm_wait_for_completion(struct mapped_device
*md
, int interruptible
)
2382 DECLARE_WAITQUEUE(wait
, current
);
2384 dm_unplug_all(md
->queue
);
2386 add_wait_queue(&md
->wait
, &wait
);
2389 set_current_state(interruptible
);
2392 if (!md_in_flight(md
))
2395 if (interruptible
== TASK_INTERRUPTIBLE
&&
2396 signal_pending(current
)) {
2403 set_current_state(TASK_RUNNING
);
2405 remove_wait_queue(&md
->wait
, &wait
);
2410 static void process_flush(struct mapped_device
*md
, struct bio
*bio
)
2412 md
->flush_error
= 0;
2414 /* handle REQ_FLUSH */
2415 dm_wait_for_completion(md
, TASK_UNINTERRUPTIBLE
);
2417 bio_init(&md
->flush_bio
);
2418 md
->flush_bio
.bi_bdev
= md
->bdev
;
2419 md
->flush_bio
.bi_rw
= WRITE_FLUSH
;
2420 __split_and_process_bio(md
, &md
->flush_bio
);
2422 dm_wait_for_completion(md
, TASK_UNINTERRUPTIBLE
);
2424 /* if it's an empty flush or the preflush failed, we're done */
2425 if (!bio_has_data(bio
) || md
->flush_error
) {
2426 if (md
->flush_error
!= DM_ENDIO_REQUEUE
)
2427 bio_endio(bio
, md
->flush_error
);
2429 spin_lock_irq(&md
->deferred_lock
);
2430 bio_list_add_head(&md
->deferred
, bio
);
2431 spin_unlock_irq(&md
->deferred_lock
);
2436 /* issue data + REQ_FUA */
2437 bio
->bi_rw
&= ~REQ_FLUSH
;
2438 __split_and_process_bio(md
, bio
);
2442 * Process the deferred bios
2444 static void dm_wq_work(struct work_struct
*work
)
2446 struct mapped_device
*md
= container_of(work
, struct mapped_device
,
2450 down_write(&md
->io_lock
);
2452 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND
, &md
->flags
)) {
2453 spin_lock_irq(&md
->deferred_lock
);
2454 c
= bio_list_pop(&md
->deferred
);
2455 spin_unlock_irq(&md
->deferred_lock
);
2458 clear_bit(DMF_QUEUE_IO_TO_THREAD
, &md
->flags
);
2462 up_write(&md
->io_lock
);
2464 if (dm_request_based(md
))
2465 generic_make_request(c
);
2467 if (c
->bi_rw
& REQ_FLUSH
)
2468 process_flush(md
, c
);
2470 __split_and_process_bio(md
, c
);
2473 down_write(&md
->io_lock
);
2476 up_write(&md
->io_lock
);
2479 static void dm_queue_flush(struct mapped_device
*md
)
2481 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND
, &md
->flags
);
2482 smp_mb__after_clear_bit();
2483 queue_work(md
->wq
, &md
->work
);
2486 static void dm_rq_set_target_request_nr(struct request
*clone
, unsigned request_nr
)
2488 struct dm_rq_target_io
*tio
= clone
->end_io_data
;
2490 tio
->info
.target_request_nr
= request_nr
;
2493 /* Issue barrier requests to targets and wait for their completion. */
2494 static int dm_rq_barrier(struct mapped_device
*md
)
2497 struct dm_table
*map
= dm_get_live_table(md
);
2498 unsigned num_targets
= dm_table_get_num_targets(map
);
2499 struct dm_target
*ti
;
2500 struct request
*clone
;
2502 md
->barrier_error
= 0;
2504 for (i
= 0; i
< num_targets
; i
++) {
2505 ti
= dm_table_get_target(map
, i
);
2506 for (j
= 0; j
< ti
->num_flush_requests
; j
++) {
2507 clone
= clone_rq(md
->flush_request
, md
, GFP_NOIO
);
2508 dm_rq_set_target_request_nr(clone
, j
);
2509 atomic_inc(&md
->pending
[rq_data_dir(clone
)]);
2510 map_request(ti
, clone
, md
);
2514 dm_wait_for_completion(md
, TASK_UNINTERRUPTIBLE
);
2517 return md
->barrier_error
;
2520 static void dm_rq_barrier_work(struct work_struct
*work
)
2523 struct mapped_device
*md
= container_of(work
, struct mapped_device
,
2525 struct request_queue
*q
= md
->queue
;
2527 unsigned long flags
;
2530 * Hold the md reference here and leave it at the last part so that
2531 * the md can't be deleted by device opener when the barrier request
2536 error
= dm_rq_barrier(md
);
2538 rq
= md
->flush_request
;
2539 md
->flush_request
= NULL
;
2541 if (error
== DM_ENDIO_REQUEUE
) {
2542 spin_lock_irqsave(q
->queue_lock
, flags
);
2543 blk_requeue_request(q
, rq
);
2544 spin_unlock_irqrestore(q
->queue_lock
, flags
);
2546 blk_end_request_all(rq
, error
);
2554 * Swap in a new table, returning the old one for the caller to destroy.
2556 struct dm_table
*dm_swap_table(struct mapped_device
*md
, struct dm_table
*table
)
2558 struct dm_table
*map
= ERR_PTR(-EINVAL
);
2559 struct queue_limits limits
;
2562 mutex_lock(&md
->suspend_lock
);
2564 /* device must be suspended */
2565 if (!dm_suspended_md(md
))
2568 r
= dm_calculate_queue_limits(table
, &limits
);
2574 map
= __bind(md
, table
, &limits
);
2577 mutex_unlock(&md
->suspend_lock
);
2582 * Functions to lock and unlock any filesystem running on the
2585 static int lock_fs(struct mapped_device
*md
)
2589 WARN_ON(md
->frozen_sb
);
2591 md
->frozen_sb
= freeze_bdev(md
->bdev
);
2592 if (IS_ERR(md
->frozen_sb
)) {
2593 r
= PTR_ERR(md
->frozen_sb
);
2594 md
->frozen_sb
= NULL
;
2598 set_bit(DMF_FROZEN
, &md
->flags
);
2603 static void unlock_fs(struct mapped_device
*md
)
2605 if (!test_bit(DMF_FROZEN
, &md
->flags
))
2608 thaw_bdev(md
->bdev
, md
->frozen_sb
);
2609 md
->frozen_sb
= NULL
;
2610 clear_bit(DMF_FROZEN
, &md
->flags
);
2614 * We need to be able to change a mapping table under a mounted
2615 * filesystem. For example we might want to move some data in
2616 * the background. Before the table can be swapped with
2617 * dm_bind_table, dm_suspend must be called to flush any in
2618 * flight bios and ensure that any further io gets deferred.
2621 * Suspend mechanism in request-based dm.
2623 * 1. Flush all I/Os by lock_fs() if needed.
2624 * 2. Stop dispatching any I/O by stopping the request_queue.
2625 * 3. Wait for all in-flight I/Os to be completed or requeued.
2627 * To abort suspend, start the request_queue.
2629 int dm_suspend(struct mapped_device
*md
, unsigned suspend_flags
)
2631 struct dm_table
*map
= NULL
;
2633 int do_lockfs
= suspend_flags
& DM_SUSPEND_LOCKFS_FLAG
? 1 : 0;
2634 int noflush
= suspend_flags
& DM_SUSPEND_NOFLUSH_FLAG
? 1 : 0;
2636 mutex_lock(&md
->suspend_lock
);
2638 if (dm_suspended_md(md
)) {
2643 map
= dm_get_live_table(md
);
2646 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2647 * This flag is cleared before dm_suspend returns.
2650 set_bit(DMF_NOFLUSH_SUSPENDING
, &md
->flags
);
2652 /* This does not get reverted if there's an error later. */
2653 dm_table_presuspend_targets(map
);
2656 * Flush I/O to the device.
2657 * Any I/O submitted after lock_fs() may not be flushed.
2658 * noflush takes precedence over do_lockfs.
2659 * (lock_fs() flushes I/Os and waits for them to complete.)
2661 if (!noflush
&& do_lockfs
) {
2668 * Here we must make sure that no processes are submitting requests
2669 * to target drivers i.e. no one may be executing
2670 * __split_and_process_bio. This is called from dm_request and
2673 * To get all processes out of __split_and_process_bio in dm_request,
2674 * we take the write lock. To prevent any process from reentering
2675 * __split_and_process_bio from dm_request, we set
2676 * DMF_QUEUE_IO_TO_THREAD.
2678 * To quiesce the thread (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND
2679 * and call flush_workqueue(md->wq). flush_workqueue will wait until
2680 * dm_wq_work exits and DMF_BLOCK_IO_FOR_SUSPEND will prevent any
2681 * further calls to __split_and_process_bio from dm_wq_work.
2683 down_write(&md
->io_lock
);
2684 set_bit(DMF_BLOCK_IO_FOR_SUSPEND
, &md
->flags
);
2685 set_bit(DMF_QUEUE_IO_TO_THREAD
, &md
->flags
);
2686 up_write(&md
->io_lock
);
2689 * Request-based dm uses md->wq for barrier (dm_rq_barrier_work) which
2690 * can be kicked until md->queue is stopped. So stop md->queue before
2693 if (dm_request_based(md
))
2694 stop_queue(md
->queue
);
2696 flush_workqueue(md
->wq
);
2699 * At this point no more requests are entering target request routines.
2700 * We call dm_wait_for_completion to wait for all existing requests
2703 r
= dm_wait_for_completion(md
, TASK_INTERRUPTIBLE
);
2705 down_write(&md
->io_lock
);
2707 clear_bit(DMF_NOFLUSH_SUSPENDING
, &md
->flags
);
2708 up_write(&md
->io_lock
);
2710 /* were we interrupted ? */
2714 if (dm_request_based(md
))
2715 start_queue(md
->queue
);
2718 goto out
; /* pushback list is already flushed, so skip flush */
2722 * If dm_wait_for_completion returned 0, the device is completely
2723 * quiescent now. There is no request-processing activity. All new
2724 * requests are being added to md->deferred list.
2727 set_bit(DMF_SUSPENDED
, &md
->flags
);
2729 dm_table_postsuspend_targets(map
);
2735 mutex_unlock(&md
->suspend_lock
);
2739 int dm_resume(struct mapped_device
*md
)
2742 struct dm_table
*map
= NULL
;
2744 mutex_lock(&md
->suspend_lock
);
2745 if (!dm_suspended_md(md
))
2748 map
= dm_get_live_table(md
);
2749 if (!map
|| !dm_table_get_size(map
))
2752 r
= dm_table_resume_targets(map
);
2759 * Flushing deferred I/Os must be done after targets are resumed
2760 * so that mapping of targets can work correctly.
2761 * Request-based dm is queueing the deferred I/Os in its request_queue.
2763 if (dm_request_based(md
))
2764 start_queue(md
->queue
);
2768 clear_bit(DMF_SUSPENDED
, &md
->flags
);
2770 dm_table_unplug_all(map
);
2774 mutex_unlock(&md
->suspend_lock
);
2779 /*-----------------------------------------------------------------
2780 * Event notification.
2781 *---------------------------------------------------------------*/
2782 int dm_kobject_uevent(struct mapped_device
*md
, enum kobject_action action
,
2785 char udev_cookie
[DM_COOKIE_LENGTH
];
2786 char *envp
[] = { udev_cookie
, NULL
};
2789 return kobject_uevent(&disk_to_dev(md
->disk
)->kobj
, action
);
2791 snprintf(udev_cookie
, DM_COOKIE_LENGTH
, "%s=%u",
2792 DM_COOKIE_ENV_VAR_NAME
, cookie
);
2793 return kobject_uevent_env(&disk_to_dev(md
->disk
)->kobj
,
2798 uint32_t dm_next_uevent_seq(struct mapped_device
*md
)
2800 return atomic_add_return(1, &md
->uevent_seq
);
2803 uint32_t dm_get_event_nr(struct mapped_device
*md
)
2805 return atomic_read(&md
->event_nr
);
2808 int dm_wait_event(struct mapped_device
*md
, int event_nr
)
2810 return wait_event_interruptible(md
->eventq
,
2811 (event_nr
!= atomic_read(&md
->event_nr
)));
2814 void dm_uevent_add(struct mapped_device
*md
, struct list_head
*elist
)
2816 unsigned long flags
;
2818 spin_lock_irqsave(&md
->uevent_lock
, flags
);
2819 list_add(elist
, &md
->uevent_list
);
2820 spin_unlock_irqrestore(&md
->uevent_lock
, flags
);
2824 * The gendisk is only valid as long as you have a reference
2827 struct gendisk
*dm_disk(struct mapped_device
*md
)
2832 struct kobject
*dm_kobject(struct mapped_device
*md
)
2838 * struct mapped_device should not be exported outside of dm.c
2839 * so use this check to verify that kobj is part of md structure
2841 struct mapped_device
*dm_get_from_kobject(struct kobject
*kobj
)
2843 struct mapped_device
*md
;
2845 md
= container_of(kobj
, struct mapped_device
, kobj
);
2846 if (&md
->kobj
!= kobj
)
2849 if (test_bit(DMF_FREEING
, &md
->flags
) ||
2857 int dm_suspended_md(struct mapped_device
*md
)
2859 return test_bit(DMF_SUSPENDED
, &md
->flags
);
2862 int dm_suspended(struct dm_target
*ti
)
2864 return dm_suspended_md(dm_table_get_md(ti
->table
));
2866 EXPORT_SYMBOL_GPL(dm_suspended
);
2868 int dm_noflush_suspending(struct dm_target
*ti
)
2870 return __noflush_suspending(dm_table_get_md(ti
->table
));
2872 EXPORT_SYMBOL_GPL(dm_noflush_suspending
);
2874 struct dm_md_mempools
*dm_alloc_md_mempools(unsigned type
)
2876 struct dm_md_mempools
*pools
= kmalloc(sizeof(*pools
), GFP_KERNEL
);
2881 pools
->io_pool
= (type
== DM_TYPE_BIO_BASED
) ?
2882 mempool_create_slab_pool(MIN_IOS
, _io_cache
) :
2883 mempool_create_slab_pool(MIN_IOS
, _rq_bio_info_cache
);
2884 if (!pools
->io_pool
)
2885 goto free_pools_and_out
;
2887 pools
->tio_pool
= (type
== DM_TYPE_BIO_BASED
) ?
2888 mempool_create_slab_pool(MIN_IOS
, _tio_cache
) :
2889 mempool_create_slab_pool(MIN_IOS
, _rq_tio_cache
);
2890 if (!pools
->tio_pool
)
2891 goto free_io_pool_and_out
;
2893 pools
->bs
= (type
== DM_TYPE_BIO_BASED
) ?
2894 bioset_create(16, 0) : bioset_create(MIN_IOS
, 0);
2896 goto free_tio_pool_and_out
;
2900 free_tio_pool_and_out
:
2901 mempool_destroy(pools
->tio_pool
);
2903 free_io_pool_and_out
:
2904 mempool_destroy(pools
->io_pool
);
2912 void dm_free_md_mempools(struct dm_md_mempools
*pools
)
2918 mempool_destroy(pools
->io_pool
);
2920 if (pools
->tio_pool
)
2921 mempool_destroy(pools
->tio_pool
);
2924 bioset_free(pools
->bs
);
2929 static const struct block_device_operations dm_blk_dops
= {
2930 .open
= dm_blk_open
,
2931 .release
= dm_blk_close
,
2932 .ioctl
= dm_blk_ioctl
,
2933 .getgeo
= dm_blk_getgeo
,
2934 .owner
= THIS_MODULE
2937 EXPORT_SYMBOL(dm_get_mapinfo
);
2942 module_init(dm_init
);
2943 module_exit(dm_exit
);
2945 module_param(major
, uint
, 0);
2946 MODULE_PARM_DESC(major
, "The major number of the device mapper");
2947 MODULE_DESCRIPTION(DM_NAME
" driver");
2948 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2949 MODULE_LICENSE("GPL");