dm: rename map_info flush_request to target_request_nr
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / md / dm.c
blob0d4710175885931530599a7534904e89317fa1cf
1 /*
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.
6 */
8 #include "dm.h"
9 #include "dm-uevent.h"
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);
43 * For bio-based dm.
44 * One of these is allocated per bio.
46 struct dm_io {
47 struct mapped_device *md;
48 int error;
49 atomic_t io_count;
50 struct bio *bio;
51 unsigned long start_time;
52 spinlock_t endio_lock;
56 * For bio-based dm.
57 * One of these is allocated per target within a bio. Hopefully
58 * this will be simplified out one day.
60 struct dm_target_io {
61 struct dm_io *io;
62 struct dm_target *ti;
63 union map_info info;
67 * For request-based dm.
68 * One of these is allocated per request.
70 struct dm_rq_target_io {
71 struct mapped_device *md;
72 struct dm_target *ti;
73 struct request *orig, clone;
74 int error;
75 union map_info info;
79 * For request-based dm.
80 * One of these is allocated per bio.
82 struct dm_rq_clone_bio_info {
83 struct bio *orig;
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;
91 return NULL;
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;
98 return NULL;
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
109 #define DMF_FROZEN 2
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;
121 rwlock_t map_lock;
122 atomic_t holders;
123 atomic_t open_count;
125 unsigned long flags;
127 struct request_queue *queue;
128 unsigned type;
129 /* Protect queue and type against concurrent access. */
130 struct mutex type_lock;
132 struct gendisk *disk;
133 char name[16];
135 void *interface_ptr;
138 * A list of ios that arrived while we were suspended.
140 atomic_t pending[2];
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 barrier request currently being processed.
149 int barrier_error;
152 * Protect barrier_error from concurrent endio processing
153 * in request-based dm.
155 spinlock_t barrier_error_lock;
158 * Processing queue (flush/barriers)
160 struct workqueue_struct *wq;
161 struct work_struct barrier_work;
163 /* A pointer to the currently processing pre/post flush request */
164 struct request *flush_request;
167 * The current mapping.
169 struct dm_table *map;
172 * io objects are allocated from here.
174 mempool_t *io_pool;
175 mempool_t *tio_pool;
177 struct bio_set *bs;
180 * Event handling.
182 atomic_t event_nr;
183 wait_queue_head_t eventq;
184 atomic_t uevent_seq;
185 struct list_head uevent_list;
186 spinlock_t uevent_lock; /* Protect access to uevent_list */
189 * freeze/thaw support require holding onto a super block
191 struct super_block *frozen_sb;
192 struct block_device *bdev;
194 /* forced geometry settings */
195 struct hd_geometry geometry;
197 /* For saving the address of __make_request for request based dm */
198 make_request_fn *saved_make_request_fn;
200 /* sysfs handle */
201 struct kobject kobj;
203 /* zero-length barrier that will be cloned and submitted to targets */
204 struct bio barrier_bio;
208 * For mempools pre-allocation at the table loading time.
210 struct dm_md_mempools {
211 mempool_t *io_pool;
212 mempool_t *tio_pool;
213 struct bio_set *bs;
216 #define MIN_IOS 256
217 static struct kmem_cache *_io_cache;
218 static struct kmem_cache *_tio_cache;
219 static struct kmem_cache *_rq_tio_cache;
220 static struct kmem_cache *_rq_bio_info_cache;
222 static int __init local_init(void)
224 int r = -ENOMEM;
226 /* allocate a slab for the dm_ios */
227 _io_cache = KMEM_CACHE(dm_io, 0);
228 if (!_io_cache)
229 return r;
231 /* allocate a slab for the target ios */
232 _tio_cache = KMEM_CACHE(dm_target_io, 0);
233 if (!_tio_cache)
234 goto out_free_io_cache;
236 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
237 if (!_rq_tio_cache)
238 goto out_free_tio_cache;
240 _rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0);
241 if (!_rq_bio_info_cache)
242 goto out_free_rq_tio_cache;
244 r = dm_uevent_init();
245 if (r)
246 goto out_free_rq_bio_info_cache;
248 _major = major;
249 r = register_blkdev(_major, _name);
250 if (r < 0)
251 goto out_uevent_exit;
253 if (!_major)
254 _major = r;
256 return 0;
258 out_uevent_exit:
259 dm_uevent_exit();
260 out_free_rq_bio_info_cache:
261 kmem_cache_destroy(_rq_bio_info_cache);
262 out_free_rq_tio_cache:
263 kmem_cache_destroy(_rq_tio_cache);
264 out_free_tio_cache:
265 kmem_cache_destroy(_tio_cache);
266 out_free_io_cache:
267 kmem_cache_destroy(_io_cache);
269 return r;
272 static void local_exit(void)
274 kmem_cache_destroy(_rq_bio_info_cache);
275 kmem_cache_destroy(_rq_tio_cache);
276 kmem_cache_destroy(_tio_cache);
277 kmem_cache_destroy(_io_cache);
278 unregister_blkdev(_major, _name);
279 dm_uevent_exit();
281 _major = 0;
283 DMINFO("cleaned up");
286 static int (*_inits[])(void) __initdata = {
287 local_init,
288 dm_target_init,
289 dm_linear_init,
290 dm_stripe_init,
291 dm_io_init,
292 dm_kcopyd_init,
293 dm_interface_init,
296 static void (*_exits[])(void) = {
297 local_exit,
298 dm_target_exit,
299 dm_linear_exit,
300 dm_stripe_exit,
301 dm_io_exit,
302 dm_kcopyd_exit,
303 dm_interface_exit,
306 static int __init dm_init(void)
308 const int count = ARRAY_SIZE(_inits);
310 int r, i;
312 for (i = 0; i < count; i++) {
313 r = _inits[i]();
314 if (r)
315 goto bad;
318 return 0;
320 bad:
321 while (i--)
322 _exits[i]();
324 return r;
327 static void __exit dm_exit(void)
329 int i = ARRAY_SIZE(_exits);
331 while (i--)
332 _exits[i]();
336 * Block device functions
338 int dm_deleting_md(struct mapped_device *md)
340 return test_bit(DMF_DELETING, &md->flags);
343 static int dm_blk_open(struct block_device *bdev, fmode_t mode)
345 struct mapped_device *md;
347 lock_kernel();
348 spin_lock(&_minor_lock);
350 md = bdev->bd_disk->private_data;
351 if (!md)
352 goto out;
354 if (test_bit(DMF_FREEING, &md->flags) ||
355 dm_deleting_md(md)) {
356 md = NULL;
357 goto out;
360 dm_get(md);
361 atomic_inc(&md->open_count);
363 out:
364 spin_unlock(&_minor_lock);
365 unlock_kernel();
367 return md ? 0 : -ENXIO;
370 static int dm_blk_close(struct gendisk *disk, fmode_t mode)
372 struct mapped_device *md = disk->private_data;
374 lock_kernel();
375 atomic_dec(&md->open_count);
376 dm_put(md);
377 unlock_kernel();
379 return 0;
382 int dm_open_count(struct mapped_device *md)
384 return atomic_read(&md->open_count);
388 * Guarantees nothing is using the device before it's deleted.
390 int dm_lock_for_deletion(struct mapped_device *md)
392 int r = 0;
394 spin_lock(&_minor_lock);
396 if (dm_open_count(md))
397 r = -EBUSY;
398 else
399 set_bit(DMF_DELETING, &md->flags);
401 spin_unlock(&_minor_lock);
403 return r;
406 static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
408 struct mapped_device *md = bdev->bd_disk->private_data;
410 return dm_get_geometry(md, geo);
413 static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
414 unsigned int cmd, unsigned long arg)
416 struct mapped_device *md = bdev->bd_disk->private_data;
417 struct dm_table *map = dm_get_live_table(md);
418 struct dm_target *tgt;
419 int r = -ENOTTY;
421 if (!map || !dm_table_get_size(map))
422 goto out;
424 /* We only support devices that have a single target */
425 if (dm_table_get_num_targets(map) != 1)
426 goto out;
428 tgt = dm_table_get_target(map, 0);
430 if (dm_suspended_md(md)) {
431 r = -EAGAIN;
432 goto out;
435 if (tgt->type->ioctl)
436 r = tgt->type->ioctl(tgt, cmd, arg);
438 out:
439 dm_table_put(map);
441 return r;
444 static struct dm_io *alloc_io(struct mapped_device *md)
446 return mempool_alloc(md->io_pool, GFP_NOIO);
449 static void free_io(struct mapped_device *md, struct dm_io *io)
451 mempool_free(io, md->io_pool);
454 static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
456 mempool_free(tio, md->tio_pool);
459 static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
460 gfp_t gfp_mask)
462 return mempool_alloc(md->tio_pool, gfp_mask);
465 static void free_rq_tio(struct dm_rq_target_io *tio)
467 mempool_free(tio, tio->md->tio_pool);
470 static struct dm_rq_clone_bio_info *alloc_bio_info(struct mapped_device *md)
472 return mempool_alloc(md->io_pool, GFP_ATOMIC);
475 static void free_bio_info(struct dm_rq_clone_bio_info *info)
477 mempool_free(info, info->tio->md->io_pool);
480 static int md_in_flight(struct mapped_device *md)
482 return atomic_read(&md->pending[READ]) +
483 atomic_read(&md->pending[WRITE]);
486 static void start_io_acct(struct dm_io *io)
488 struct mapped_device *md = io->md;
489 int cpu;
490 int rw = bio_data_dir(io->bio);
492 io->start_time = jiffies;
494 cpu = part_stat_lock();
495 part_round_stats(cpu, &dm_disk(md)->part0);
496 part_stat_unlock();
497 dm_disk(md)->part0.in_flight[rw] = atomic_inc_return(&md->pending[rw]);
500 static void end_io_acct(struct dm_io *io)
502 struct mapped_device *md = io->md;
503 struct bio *bio = io->bio;
504 unsigned long duration = jiffies - io->start_time;
505 int pending, cpu;
506 int rw = bio_data_dir(bio);
508 cpu = part_stat_lock();
509 part_round_stats(cpu, &dm_disk(md)->part0);
510 part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
511 part_stat_unlock();
514 * After this is decremented the bio must not be touched if it is
515 * a barrier.
517 dm_disk(md)->part0.in_flight[rw] = pending =
518 atomic_dec_return(&md->pending[rw]);
519 pending += atomic_read(&md->pending[rw^0x1]);
521 /* nudge anyone waiting on suspend queue */
522 if (!pending)
523 wake_up(&md->wait);
527 * Add the bio to the list of deferred io.
529 static void queue_io(struct mapped_device *md, struct bio *bio)
531 down_write(&md->io_lock);
533 spin_lock_irq(&md->deferred_lock);
534 bio_list_add(&md->deferred, bio);
535 spin_unlock_irq(&md->deferred_lock);
537 if (!test_and_set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags))
538 queue_work(md->wq, &md->work);
540 up_write(&md->io_lock);
544 * Everyone (including functions in this file), should use this
545 * function to access the md->map field, and make sure they call
546 * dm_table_put() when finished.
548 struct dm_table *dm_get_live_table(struct mapped_device *md)
550 struct dm_table *t;
551 unsigned long flags;
553 read_lock_irqsave(&md->map_lock, flags);
554 t = md->map;
555 if (t)
556 dm_table_get(t);
557 read_unlock_irqrestore(&md->map_lock, flags);
559 return t;
563 * Get the geometry associated with a dm device
565 int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
567 *geo = md->geometry;
569 return 0;
573 * Set the geometry of a device.
575 int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
577 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
579 if (geo->start > sz) {
580 DMWARN("Start sector is beyond the geometry limits.");
581 return -EINVAL;
584 md->geometry = *geo;
586 return 0;
589 /*-----------------------------------------------------------------
590 * CRUD START:
591 * A more elegant soln is in the works that uses the queue
592 * merge fn, unfortunately there are a couple of changes to
593 * the block layer that I want to make for this. So in the
594 * interests of getting something for people to use I give
595 * you this clearly demarcated crap.
596 *---------------------------------------------------------------*/
598 static int __noflush_suspending(struct mapped_device *md)
600 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
604 * Decrements the number of outstanding ios that a bio has been
605 * cloned into, completing the original io if necc.
607 static void dec_pending(struct dm_io *io, int error)
609 unsigned long flags;
610 int io_error;
611 struct bio *bio;
612 struct mapped_device *md = io->md;
614 /* Push-back supersedes any I/O errors */
615 if (unlikely(error)) {
616 spin_lock_irqsave(&io->endio_lock, flags);
617 if (!(io->error > 0 && __noflush_suspending(md)))
618 io->error = error;
619 spin_unlock_irqrestore(&io->endio_lock, flags);
622 if (atomic_dec_and_test(&io->io_count)) {
623 if (io->error == DM_ENDIO_REQUEUE) {
625 * Target requested pushing back the I/O.
627 spin_lock_irqsave(&md->deferred_lock, flags);
628 if (__noflush_suspending(md)) {
629 if (!(io->bio->bi_rw & REQ_HARDBARRIER))
630 bio_list_add_head(&md->deferred,
631 io->bio);
632 } else
633 /* noflush suspend was interrupted. */
634 io->error = -EIO;
635 spin_unlock_irqrestore(&md->deferred_lock, flags);
638 io_error = io->error;
639 bio = io->bio;
641 if (bio->bi_rw & REQ_HARDBARRIER) {
643 * There can be just one barrier request so we use
644 * a per-device variable for error reporting.
645 * Note that you can't touch the bio after end_io_acct
647 * We ignore -EOPNOTSUPP for empty flush reported by
648 * underlying devices. We assume that if the device
649 * doesn't support empty barriers, it doesn't need
650 * cache flushing commands.
652 if (!md->barrier_error &&
653 !(bio_empty_barrier(bio) && io_error == -EOPNOTSUPP))
654 md->barrier_error = io_error;
655 end_io_acct(io);
656 free_io(md, io);
657 } else {
658 end_io_acct(io);
659 free_io(md, io);
661 if (io_error != DM_ENDIO_REQUEUE) {
662 trace_block_bio_complete(md->queue, bio);
664 bio_endio(bio, io_error);
670 static void clone_endio(struct bio *bio, int error)
672 int r = 0;
673 struct dm_target_io *tio = bio->bi_private;
674 struct dm_io *io = tio->io;
675 struct mapped_device *md = tio->io->md;
676 dm_endio_fn endio = tio->ti->type->end_io;
678 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
679 error = -EIO;
681 if (endio) {
682 r = endio(tio->ti, bio, error, &tio->info);
683 if (r < 0 || r == DM_ENDIO_REQUEUE)
685 * error and requeue request are handled
686 * in dec_pending().
688 error = r;
689 else if (r == DM_ENDIO_INCOMPLETE)
690 /* The target will handle the io */
691 return;
692 else if (r) {
693 DMWARN("unimplemented target endio return value: %d", r);
694 BUG();
699 * Store md for cleanup instead of tio which is about to get freed.
701 bio->bi_private = md->bs;
703 free_tio(md, tio);
704 bio_put(bio);
705 dec_pending(io, error);
709 * Partial completion handling for request-based dm
711 static void end_clone_bio(struct bio *clone, int error)
713 struct dm_rq_clone_bio_info *info = clone->bi_private;
714 struct dm_rq_target_io *tio = info->tio;
715 struct bio *bio = info->orig;
716 unsigned int nr_bytes = info->orig->bi_size;
718 bio_put(clone);
720 if (tio->error)
722 * An error has already been detected on the request.
723 * Once error occurred, just let clone->end_io() handle
724 * the remainder.
726 return;
727 else if (error) {
729 * Don't notice the error to the upper layer yet.
730 * The error handling decision is made by the target driver,
731 * when the request is completed.
733 tio->error = error;
734 return;
738 * I/O for the bio successfully completed.
739 * Notice the data completion to the upper layer.
743 * bios are processed from the head of the list.
744 * So the completing bio should always be rq->bio.
745 * If it's not, something wrong is happening.
747 if (tio->orig->bio != bio)
748 DMERR("bio completion is going in the middle of the request");
751 * Update the original request.
752 * Do not use blk_end_request() here, because it may complete
753 * the original request before the clone, and break the ordering.
755 blk_update_request(tio->orig, 0, nr_bytes);
758 static void store_barrier_error(struct mapped_device *md, int error)
760 unsigned long flags;
762 spin_lock_irqsave(&md->barrier_error_lock, flags);
764 * Basically, the first error is taken, but:
765 * -EOPNOTSUPP supersedes any I/O error.
766 * Requeue request supersedes any I/O error but -EOPNOTSUPP.
768 if (!md->barrier_error || error == -EOPNOTSUPP ||
769 (md->barrier_error != -EOPNOTSUPP &&
770 error == DM_ENDIO_REQUEUE))
771 md->barrier_error = error;
772 spin_unlock_irqrestore(&md->barrier_error_lock, flags);
776 * Don't touch any member of the md after calling this function because
777 * the md may be freed in dm_put() at the end of this function.
778 * Or do dm_get() before calling this function and dm_put() later.
780 static void rq_completed(struct mapped_device *md, int rw, int run_queue)
782 atomic_dec(&md->pending[rw]);
784 /* nudge anyone waiting on suspend queue */
785 if (!md_in_flight(md))
786 wake_up(&md->wait);
788 if (run_queue)
789 blk_run_queue(md->queue);
792 * dm_put() must be at the end of this function. See the comment above
794 dm_put(md);
797 static void free_rq_clone(struct request *clone)
799 struct dm_rq_target_io *tio = clone->end_io_data;
801 blk_rq_unprep_clone(clone);
802 free_rq_tio(tio);
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 int rw = rq_data_dir(clone);
812 int run_queue = 1;
813 bool is_barrier = clone->cmd_flags & REQ_HARDBARRIER;
814 struct dm_rq_target_io *tio = clone->end_io_data;
815 struct mapped_device *md = tio->md;
816 struct request *rq = tio->orig;
818 if (rq->cmd_type == REQ_TYPE_BLOCK_PC && !is_barrier) {
819 rq->errors = clone->errors;
820 rq->resid_len = clone->resid_len;
822 if (rq->sense)
824 * We are using the sense buffer of the original
825 * request.
826 * So setting the length of the sense data is enough.
828 rq->sense_len = clone->sense_len;
831 free_rq_clone(clone);
833 if (unlikely(is_barrier)) {
834 if (unlikely(error))
835 store_barrier_error(md, error);
836 run_queue = 0;
837 } else
838 blk_end_request_all(rq, error);
840 rq_completed(md, rw, run_queue);
843 static void dm_unprep_request(struct request *rq)
845 struct request *clone = rq->special;
847 rq->special = NULL;
848 rq->cmd_flags &= ~REQ_DONTPREP;
850 free_rq_clone(clone);
854 * Requeue the original request of a clone.
856 void dm_requeue_unmapped_request(struct request *clone)
858 int rw = rq_data_dir(clone);
859 struct dm_rq_target_io *tio = clone->end_io_data;
860 struct mapped_device *md = tio->md;
861 struct request *rq = tio->orig;
862 struct request_queue *q = rq->q;
863 unsigned long flags;
865 if (unlikely(clone->cmd_flags & REQ_HARDBARRIER)) {
867 * Barrier clones share an original request.
868 * Leave it to dm_end_request(), which handles this special
869 * case.
871 dm_end_request(clone, DM_ENDIO_REQUEUE);
872 return;
875 dm_unprep_request(rq);
877 spin_lock_irqsave(q->queue_lock, flags);
878 if (elv_queue_empty(q))
879 blk_plug_device(q);
880 blk_requeue_request(q, rq);
881 spin_unlock_irqrestore(q->queue_lock, flags);
883 rq_completed(md, rw, 0);
885 EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
887 static void __stop_queue(struct request_queue *q)
889 blk_stop_queue(q);
892 static void stop_queue(struct request_queue *q)
894 unsigned long flags;
896 spin_lock_irqsave(q->queue_lock, flags);
897 __stop_queue(q);
898 spin_unlock_irqrestore(q->queue_lock, flags);
901 static void __start_queue(struct request_queue *q)
903 if (blk_queue_stopped(q))
904 blk_start_queue(q);
907 static void start_queue(struct request_queue *q)
909 unsigned long flags;
911 spin_lock_irqsave(q->queue_lock, flags);
912 __start_queue(q);
913 spin_unlock_irqrestore(q->queue_lock, flags);
916 static void dm_done(struct request *clone, int error, bool mapped)
918 int r = error;
919 struct dm_rq_target_io *tio = clone->end_io_data;
920 dm_request_endio_fn rq_end_io = tio->ti->type->rq_end_io;
922 if (mapped && rq_end_io)
923 r = rq_end_io(tio->ti, clone, error, &tio->info);
925 if (r <= 0)
926 /* The target wants to complete the I/O */
927 dm_end_request(clone, r);
928 else if (r == DM_ENDIO_INCOMPLETE)
929 /* The target will handle the I/O */
930 return;
931 else if (r == DM_ENDIO_REQUEUE)
932 /* The target wants to requeue the I/O */
933 dm_requeue_unmapped_request(clone);
934 else {
935 DMWARN("unimplemented target endio return value: %d", r);
936 BUG();
941 * Request completion handler for request-based dm
943 static void dm_softirq_done(struct request *rq)
945 bool mapped = true;
946 struct request *clone = rq->completion_data;
947 struct dm_rq_target_io *tio = clone->end_io_data;
949 if (rq->cmd_flags & REQ_FAILED)
950 mapped = false;
952 dm_done(clone, tio->error, mapped);
956 * Complete the clone and the original request with the error status
957 * through softirq context.
959 static void dm_complete_request(struct request *clone, int error)
961 struct dm_rq_target_io *tio = clone->end_io_data;
962 struct request *rq = tio->orig;
964 if (unlikely(clone->cmd_flags & REQ_HARDBARRIER)) {
966 * Barrier clones share an original request. So can't use
967 * softirq_done with the original.
968 * Pass the clone to dm_done() directly in this special case.
969 * It is safe (even if clone->q->queue_lock is held here)
970 * because there is no I/O dispatching during the completion
971 * of barrier clone.
973 dm_done(clone, error, true);
974 return;
977 tio->error = error;
978 rq->completion_data = clone;
979 blk_complete_request(rq);
983 * Complete the not-mapped clone and the original request with the error status
984 * through softirq context.
985 * Target's rq_end_io() function isn't called.
986 * This may be used when the target's map_rq() function fails.
988 void dm_kill_unmapped_request(struct request *clone, int error)
990 struct dm_rq_target_io *tio = clone->end_io_data;
991 struct request *rq = tio->orig;
993 if (unlikely(clone->cmd_flags & REQ_HARDBARRIER)) {
995 * Barrier clones share an original request.
996 * Leave it to dm_end_request(), which handles this special
997 * case.
999 BUG_ON(error > 0);
1000 dm_end_request(clone, error);
1001 return;
1004 rq->cmd_flags |= REQ_FAILED;
1005 dm_complete_request(clone, error);
1007 EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
1010 * Called with the queue lock held
1012 static void end_clone_request(struct request *clone, int error)
1015 * For just cleaning up the information of the queue in which
1016 * the clone was dispatched.
1017 * The clone is *NOT* freed actually here because it is alloced from
1018 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
1020 __blk_put_request(clone->q, clone);
1023 * Actual request completion is done in a softirq context which doesn't
1024 * hold the queue lock. Otherwise, deadlock could occur because:
1025 * - another request may be submitted by the upper level driver
1026 * of the stacking during the completion
1027 * - the submission which requires queue lock may be done
1028 * against this queue
1030 dm_complete_request(clone, error);
1033 static sector_t max_io_len(struct mapped_device *md,
1034 sector_t sector, struct dm_target *ti)
1036 sector_t offset = sector - ti->begin;
1037 sector_t len = ti->len - offset;
1040 * Does the target need to split even further ?
1042 if (ti->split_io) {
1043 sector_t boundary;
1044 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
1045 - offset;
1046 if (len > boundary)
1047 len = boundary;
1050 return len;
1053 static void __map_bio(struct dm_target *ti, struct bio *clone,
1054 struct dm_target_io *tio)
1056 int r;
1057 sector_t sector;
1058 struct mapped_device *md;
1060 clone->bi_end_io = clone_endio;
1061 clone->bi_private = tio;
1064 * Map the clone. If r == 0 we don't need to do
1065 * anything, the target has assumed ownership of
1066 * this io.
1068 atomic_inc(&tio->io->io_count);
1069 sector = clone->bi_sector;
1070 r = ti->type->map(ti, clone, &tio->info);
1071 if (r == DM_MAPIO_REMAPPED) {
1072 /* the bio has been remapped so dispatch it */
1074 trace_block_remap(bdev_get_queue(clone->bi_bdev), clone,
1075 tio->io->bio->bi_bdev->bd_dev, sector);
1077 generic_make_request(clone);
1078 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
1079 /* error the io and bail out, or requeue it if needed */
1080 md = tio->io->md;
1081 dec_pending(tio->io, r);
1083 * Store bio_set for cleanup.
1085 clone->bi_private = md->bs;
1086 bio_put(clone);
1087 free_tio(md, tio);
1088 } else if (r) {
1089 DMWARN("unimplemented target map return value: %d", r);
1090 BUG();
1094 struct clone_info {
1095 struct mapped_device *md;
1096 struct dm_table *map;
1097 struct bio *bio;
1098 struct dm_io *io;
1099 sector_t sector;
1100 sector_t sector_count;
1101 unsigned short idx;
1104 static void dm_bio_destructor(struct bio *bio)
1106 struct bio_set *bs = bio->bi_private;
1108 bio_free(bio, bs);
1112 * Creates a little bio that is just does part of a bvec.
1114 static struct bio *split_bvec(struct bio *bio, sector_t sector,
1115 unsigned short idx, unsigned int offset,
1116 unsigned int len, struct bio_set *bs)
1118 struct bio *clone;
1119 struct bio_vec *bv = bio->bi_io_vec + idx;
1121 clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
1122 clone->bi_destructor = dm_bio_destructor;
1123 *clone->bi_io_vec = *bv;
1125 clone->bi_sector = sector;
1126 clone->bi_bdev = bio->bi_bdev;
1127 clone->bi_rw = bio->bi_rw & ~REQ_HARDBARRIER;
1128 clone->bi_vcnt = 1;
1129 clone->bi_size = to_bytes(len);
1130 clone->bi_io_vec->bv_offset = offset;
1131 clone->bi_io_vec->bv_len = clone->bi_size;
1132 clone->bi_flags |= 1 << BIO_CLONED;
1134 if (bio_integrity(bio)) {
1135 bio_integrity_clone(clone, bio, GFP_NOIO, bs);
1136 bio_integrity_trim(clone,
1137 bio_sector_offset(bio, idx, offset), len);
1140 return clone;
1144 * Creates a bio that consists of range of complete bvecs.
1146 static struct bio *clone_bio(struct bio *bio, sector_t sector,
1147 unsigned short idx, unsigned short bv_count,
1148 unsigned int len, struct bio_set *bs)
1150 struct bio *clone;
1152 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
1153 __bio_clone(clone, bio);
1154 clone->bi_rw &= ~REQ_HARDBARRIER;
1155 clone->bi_destructor = dm_bio_destructor;
1156 clone->bi_sector = sector;
1157 clone->bi_idx = idx;
1158 clone->bi_vcnt = idx + bv_count;
1159 clone->bi_size = to_bytes(len);
1160 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
1162 if (bio_integrity(bio)) {
1163 bio_integrity_clone(clone, bio, GFP_NOIO, bs);
1165 if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
1166 bio_integrity_trim(clone,
1167 bio_sector_offset(bio, idx, 0), len);
1170 return clone;
1173 static struct dm_target_io *alloc_tio(struct clone_info *ci,
1174 struct dm_target *ti)
1176 struct dm_target_io *tio = mempool_alloc(ci->md->tio_pool, GFP_NOIO);
1178 tio->io = ci->io;
1179 tio->ti = ti;
1180 memset(&tio->info, 0, sizeof(tio->info));
1182 return tio;
1185 static void __flush_target(struct clone_info *ci, struct dm_target *ti,
1186 unsigned request_nr)
1188 struct dm_target_io *tio = alloc_tio(ci, ti);
1189 struct bio *clone;
1191 tio->info.target_request_nr = request_nr;
1193 clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
1194 __bio_clone(clone, ci->bio);
1195 clone->bi_destructor = dm_bio_destructor;
1197 __map_bio(ti, clone, tio);
1200 static int __clone_and_map_empty_barrier(struct clone_info *ci)
1202 unsigned target_nr = 0, request_nr;
1203 struct dm_target *ti;
1205 while ((ti = dm_table_get_target(ci->map, target_nr++)))
1206 for (request_nr = 0; request_nr < ti->num_flush_requests;
1207 request_nr++)
1208 __flush_target(ci, ti, request_nr);
1210 ci->sector_count = 0;
1212 return 0;
1215 static int __clone_and_map(struct clone_info *ci)
1217 struct bio *clone, *bio = ci->bio;
1218 struct dm_target *ti;
1219 sector_t len = 0, max;
1220 struct dm_target_io *tio;
1222 if (unlikely(bio_empty_barrier(bio)))
1223 return __clone_and_map_empty_barrier(ci);
1225 ti = dm_table_find_target(ci->map, ci->sector);
1226 if (!dm_target_is_valid(ti))
1227 return -EIO;
1229 max = max_io_len(ci->md, ci->sector, ti);
1232 * Allocate a target io object.
1234 tio = alloc_tio(ci, ti);
1236 if (ci->sector_count <= max) {
1238 * Optimise for the simple case where we can do all of
1239 * the remaining io with a single clone.
1241 clone = clone_bio(bio, ci->sector, ci->idx,
1242 bio->bi_vcnt - ci->idx, ci->sector_count,
1243 ci->md->bs);
1244 __map_bio(ti, clone, tio);
1245 ci->sector_count = 0;
1247 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
1249 * There are some bvecs that don't span targets.
1250 * Do as many of these as possible.
1252 int i;
1253 sector_t remaining = max;
1254 sector_t bv_len;
1256 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
1257 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
1259 if (bv_len > remaining)
1260 break;
1262 remaining -= bv_len;
1263 len += bv_len;
1266 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
1267 ci->md->bs);
1268 __map_bio(ti, clone, tio);
1270 ci->sector += len;
1271 ci->sector_count -= len;
1272 ci->idx = i;
1274 } else {
1276 * Handle a bvec that must be split between two or more targets.
1278 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
1279 sector_t remaining = to_sector(bv->bv_len);
1280 unsigned int offset = 0;
1282 do {
1283 if (offset) {
1284 ti = dm_table_find_target(ci->map, ci->sector);
1285 if (!dm_target_is_valid(ti))
1286 return -EIO;
1288 max = max_io_len(ci->md, ci->sector, ti);
1290 tio = alloc_tio(ci, ti);
1293 len = min(remaining, max);
1295 clone = split_bvec(bio, ci->sector, ci->idx,
1296 bv->bv_offset + offset, len,
1297 ci->md->bs);
1299 __map_bio(ti, clone, tio);
1301 ci->sector += len;
1302 ci->sector_count -= len;
1303 offset += to_bytes(len);
1304 } while (remaining -= len);
1306 ci->idx++;
1309 return 0;
1313 * Split the bio into several clones and submit it to targets.
1315 static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
1317 struct clone_info ci;
1318 int error = 0;
1320 ci.map = dm_get_live_table(md);
1321 if (unlikely(!ci.map)) {
1322 if (!(bio->bi_rw & REQ_HARDBARRIER))
1323 bio_io_error(bio);
1324 else
1325 if (!md->barrier_error)
1326 md->barrier_error = -EIO;
1327 return;
1330 ci.md = md;
1331 ci.bio = bio;
1332 ci.io = alloc_io(md);
1333 ci.io->error = 0;
1334 atomic_set(&ci.io->io_count, 1);
1335 ci.io->bio = bio;
1336 ci.io->md = md;
1337 spin_lock_init(&ci.io->endio_lock);
1338 ci.sector = bio->bi_sector;
1339 ci.sector_count = bio_sectors(bio);
1340 if (unlikely(bio_empty_barrier(bio)))
1341 ci.sector_count = 1;
1342 ci.idx = bio->bi_idx;
1344 start_io_acct(ci.io);
1345 while (ci.sector_count && !error)
1346 error = __clone_and_map(&ci);
1348 /* drop the extra reference count */
1349 dec_pending(ci.io, error);
1350 dm_table_put(ci.map);
1352 /*-----------------------------------------------------------------
1353 * CRUD END
1354 *---------------------------------------------------------------*/
1356 static int dm_merge_bvec(struct request_queue *q,
1357 struct bvec_merge_data *bvm,
1358 struct bio_vec *biovec)
1360 struct mapped_device *md = q->queuedata;
1361 struct dm_table *map = dm_get_live_table(md);
1362 struct dm_target *ti;
1363 sector_t max_sectors;
1364 int max_size = 0;
1366 if (unlikely(!map))
1367 goto out;
1369 ti = dm_table_find_target(map, bvm->bi_sector);
1370 if (!dm_target_is_valid(ti))
1371 goto out_table;
1374 * Find maximum amount of I/O that won't need splitting
1376 max_sectors = min(max_io_len(md, bvm->bi_sector, ti),
1377 (sector_t) BIO_MAX_SECTORS);
1378 max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1379 if (max_size < 0)
1380 max_size = 0;
1383 * merge_bvec_fn() returns number of bytes
1384 * it can accept at this offset
1385 * max is precomputed maximal io size
1387 if (max_size && ti->type->merge)
1388 max_size = ti->type->merge(ti, bvm, biovec, max_size);
1390 * If the target doesn't support merge method and some of the devices
1391 * provided their merge_bvec method (we know this by looking at
1392 * queue_max_hw_sectors), then we can't allow bios with multiple vector
1393 * entries. So always set max_size to 0, and the code below allows
1394 * just one page.
1396 else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
1398 max_size = 0;
1400 out_table:
1401 dm_table_put(map);
1403 out:
1405 * Always allow an entire first page
1407 if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1408 max_size = biovec->bv_len;
1410 return max_size;
1414 * The request function that just remaps the bio built up by
1415 * dm_merge_bvec.
1417 static int _dm_request(struct request_queue *q, struct bio *bio)
1419 int rw = bio_data_dir(bio);
1420 struct mapped_device *md = q->queuedata;
1421 int cpu;
1423 down_read(&md->io_lock);
1425 cpu = part_stat_lock();
1426 part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1427 part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1428 part_stat_unlock();
1431 * If we're suspended or the thread is processing barriers
1432 * we have to queue this io for later.
1434 if (unlikely(test_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags)) ||
1435 unlikely(bio->bi_rw & REQ_HARDBARRIER)) {
1436 up_read(&md->io_lock);
1438 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) &&
1439 bio_rw(bio) == READA) {
1440 bio_io_error(bio);
1441 return 0;
1444 queue_io(md, bio);
1446 return 0;
1449 __split_and_process_bio(md, bio);
1450 up_read(&md->io_lock);
1451 return 0;
1454 static int dm_make_request(struct request_queue *q, struct bio *bio)
1456 struct mapped_device *md = q->queuedata;
1458 return md->saved_make_request_fn(q, bio); /* call __make_request() */
1461 static int dm_request_based(struct mapped_device *md)
1463 return blk_queue_stackable(md->queue);
1466 static int dm_request(struct request_queue *q, struct bio *bio)
1468 struct mapped_device *md = q->queuedata;
1470 if (dm_request_based(md))
1471 return dm_make_request(q, bio);
1473 return _dm_request(q, bio);
1476 static bool dm_rq_is_flush_request(struct request *rq)
1478 if (rq->cmd_flags & REQ_FLUSH)
1479 return true;
1480 else
1481 return false;
1484 void dm_dispatch_request(struct request *rq)
1486 int r;
1488 if (blk_queue_io_stat(rq->q))
1489 rq->cmd_flags |= REQ_IO_STAT;
1491 rq->start_time = jiffies;
1492 r = blk_insert_cloned_request(rq->q, rq);
1493 if (r)
1494 dm_complete_request(rq, r);
1496 EXPORT_SYMBOL_GPL(dm_dispatch_request);
1498 static void dm_rq_bio_destructor(struct bio *bio)
1500 struct dm_rq_clone_bio_info *info = bio->bi_private;
1501 struct mapped_device *md = info->tio->md;
1503 free_bio_info(info);
1504 bio_free(bio, md->bs);
1507 static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1508 void *data)
1510 struct dm_rq_target_io *tio = data;
1511 struct mapped_device *md = tio->md;
1512 struct dm_rq_clone_bio_info *info = alloc_bio_info(md);
1514 if (!info)
1515 return -ENOMEM;
1517 info->orig = bio_orig;
1518 info->tio = tio;
1519 bio->bi_end_io = end_clone_bio;
1520 bio->bi_private = info;
1521 bio->bi_destructor = dm_rq_bio_destructor;
1523 return 0;
1526 static int setup_clone(struct request *clone, struct request *rq,
1527 struct dm_rq_target_io *tio)
1529 int r;
1531 if (dm_rq_is_flush_request(rq)) {
1532 blk_rq_init(NULL, clone);
1533 clone->cmd_type = REQ_TYPE_FS;
1534 clone->cmd_flags |= (REQ_HARDBARRIER | WRITE);
1535 } else {
1536 r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1537 dm_rq_bio_constructor, tio);
1538 if (r)
1539 return r;
1541 clone->cmd = rq->cmd;
1542 clone->cmd_len = rq->cmd_len;
1543 clone->sense = rq->sense;
1544 clone->buffer = rq->buffer;
1547 clone->end_io = end_clone_request;
1548 clone->end_io_data = tio;
1550 return 0;
1553 static struct request *clone_rq(struct request *rq, struct mapped_device *md,
1554 gfp_t gfp_mask)
1556 struct request *clone;
1557 struct dm_rq_target_io *tio;
1559 tio = alloc_rq_tio(md, gfp_mask);
1560 if (!tio)
1561 return NULL;
1563 tio->md = md;
1564 tio->ti = NULL;
1565 tio->orig = rq;
1566 tio->error = 0;
1567 memset(&tio->info, 0, sizeof(tio->info));
1569 clone = &tio->clone;
1570 if (setup_clone(clone, rq, tio)) {
1571 /* -ENOMEM */
1572 free_rq_tio(tio);
1573 return NULL;
1576 return clone;
1580 * Called with the queue lock held.
1582 static int dm_prep_fn(struct request_queue *q, struct request *rq)
1584 struct mapped_device *md = q->queuedata;
1585 struct request *clone;
1587 if (unlikely(dm_rq_is_flush_request(rq)))
1588 return BLKPREP_OK;
1590 if (unlikely(rq->special)) {
1591 DMWARN("Already has something in rq->special.");
1592 return BLKPREP_KILL;
1595 clone = clone_rq(rq, md, GFP_ATOMIC);
1596 if (!clone)
1597 return BLKPREP_DEFER;
1599 rq->special = clone;
1600 rq->cmd_flags |= REQ_DONTPREP;
1602 return BLKPREP_OK;
1606 * Returns:
1607 * 0 : the request has been processed (not requeued)
1608 * !0 : the request has been requeued
1610 static int map_request(struct dm_target *ti, struct request *clone,
1611 struct mapped_device *md)
1613 int r, requeued = 0;
1614 struct dm_rq_target_io *tio = clone->end_io_data;
1617 * Hold the md reference here for the in-flight I/O.
1618 * We can't rely on the reference count by device opener,
1619 * because the device may be closed during the request completion
1620 * when all bios are completed.
1621 * See the comment in rq_completed() too.
1623 dm_get(md);
1625 tio->ti = ti;
1626 r = ti->type->map_rq(ti, clone, &tio->info);
1627 switch (r) {
1628 case DM_MAPIO_SUBMITTED:
1629 /* The target has taken the I/O to submit by itself later */
1630 break;
1631 case DM_MAPIO_REMAPPED:
1632 /* The target has remapped the I/O so dispatch it */
1633 trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
1634 blk_rq_pos(tio->orig));
1635 dm_dispatch_request(clone);
1636 break;
1637 case DM_MAPIO_REQUEUE:
1638 /* The target wants to requeue the I/O */
1639 dm_requeue_unmapped_request(clone);
1640 requeued = 1;
1641 break;
1642 default:
1643 if (r > 0) {
1644 DMWARN("unimplemented target map return value: %d", r);
1645 BUG();
1648 /* The target wants to complete the I/O */
1649 dm_kill_unmapped_request(clone, r);
1650 break;
1653 return requeued;
1657 * q->request_fn for request-based dm.
1658 * Called with the queue lock held.
1660 static void dm_request_fn(struct request_queue *q)
1662 struct mapped_device *md = q->queuedata;
1663 struct dm_table *map = dm_get_live_table(md);
1664 struct dm_target *ti;
1665 struct request *rq, *clone;
1668 * For suspend, check blk_queue_stopped() and increment
1669 * ->pending within a single queue_lock not to increment the
1670 * number of in-flight I/Os after the queue is stopped in
1671 * dm_suspend().
1673 while (!blk_queue_plugged(q) && !blk_queue_stopped(q)) {
1674 rq = blk_peek_request(q);
1675 if (!rq)
1676 goto plug_and_out;
1678 if (unlikely(dm_rq_is_flush_request(rq))) {
1679 BUG_ON(md->flush_request);
1680 md->flush_request = rq;
1681 blk_start_request(rq);
1682 queue_work(md->wq, &md->barrier_work);
1683 goto out;
1686 ti = dm_table_find_target(map, blk_rq_pos(rq));
1687 if (ti->type->busy && ti->type->busy(ti))
1688 goto plug_and_out;
1690 blk_start_request(rq);
1691 clone = rq->special;
1692 atomic_inc(&md->pending[rq_data_dir(clone)]);
1694 spin_unlock(q->queue_lock);
1695 if (map_request(ti, clone, md))
1696 goto requeued;
1698 spin_lock_irq(q->queue_lock);
1701 goto out;
1703 requeued:
1704 spin_lock_irq(q->queue_lock);
1706 plug_and_out:
1707 if (!elv_queue_empty(q))
1708 /* Some requests still remain, retry later */
1709 blk_plug_device(q);
1711 out:
1712 dm_table_put(map);
1714 return;
1717 int dm_underlying_device_busy(struct request_queue *q)
1719 return blk_lld_busy(q);
1721 EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1723 static int dm_lld_busy(struct request_queue *q)
1725 int r;
1726 struct mapped_device *md = q->queuedata;
1727 struct dm_table *map = dm_get_live_table(md);
1729 if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1730 r = 1;
1731 else
1732 r = dm_table_any_busy_target(map);
1734 dm_table_put(map);
1736 return r;
1739 static void dm_unplug_all(struct request_queue *q)
1741 struct mapped_device *md = q->queuedata;
1742 struct dm_table *map = dm_get_live_table(md);
1744 if (map) {
1745 if (dm_request_based(md))
1746 generic_unplug_device(q);
1748 dm_table_unplug_all(map);
1749 dm_table_put(map);
1753 static int dm_any_congested(void *congested_data, int bdi_bits)
1755 int r = bdi_bits;
1756 struct mapped_device *md = congested_data;
1757 struct dm_table *map;
1759 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
1760 map = dm_get_live_table(md);
1761 if (map) {
1763 * Request-based dm cares about only own queue for
1764 * the query about congestion status of request_queue
1766 if (dm_request_based(md))
1767 r = md->queue->backing_dev_info.state &
1768 bdi_bits;
1769 else
1770 r = dm_table_any_congested(map, bdi_bits);
1772 dm_table_put(map);
1776 return r;
1779 /*-----------------------------------------------------------------
1780 * An IDR is used to keep track of allocated minor numbers.
1781 *---------------------------------------------------------------*/
1782 static DEFINE_IDR(_minor_idr);
1784 static void free_minor(int minor)
1786 spin_lock(&_minor_lock);
1787 idr_remove(&_minor_idr, minor);
1788 spin_unlock(&_minor_lock);
1792 * See if the device with a specific minor # is free.
1794 static int specific_minor(int minor)
1796 int r, m;
1798 if (minor >= (1 << MINORBITS))
1799 return -EINVAL;
1801 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
1802 if (!r)
1803 return -ENOMEM;
1805 spin_lock(&_minor_lock);
1807 if (idr_find(&_minor_idr, minor)) {
1808 r = -EBUSY;
1809 goto out;
1812 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
1813 if (r)
1814 goto out;
1816 if (m != minor) {
1817 idr_remove(&_minor_idr, m);
1818 r = -EBUSY;
1819 goto out;
1822 out:
1823 spin_unlock(&_minor_lock);
1824 return r;
1827 static int next_free_minor(int *minor)
1829 int r, m;
1831 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
1832 if (!r)
1833 return -ENOMEM;
1835 spin_lock(&_minor_lock);
1837 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
1838 if (r)
1839 goto out;
1841 if (m >= (1 << MINORBITS)) {
1842 idr_remove(&_minor_idr, m);
1843 r = -ENOSPC;
1844 goto out;
1847 *minor = m;
1849 out:
1850 spin_unlock(&_minor_lock);
1851 return r;
1854 static const struct block_device_operations dm_blk_dops;
1856 static void dm_wq_work(struct work_struct *work);
1857 static void dm_rq_barrier_work(struct work_struct *work);
1859 static void dm_init_md_queue(struct mapped_device *md)
1862 * Request-based dm devices cannot be stacked on top of bio-based dm
1863 * devices. The type of this dm device has not been decided yet.
1864 * The type is decided at the first table loading time.
1865 * To prevent problematic device stacking, clear the queue flag
1866 * for request stacking support until then.
1868 * This queue is new, so no concurrency on the queue_flags.
1870 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
1872 md->queue->queuedata = md;
1873 md->queue->backing_dev_info.congested_fn = dm_any_congested;
1874 md->queue->backing_dev_info.congested_data = md;
1875 blk_queue_make_request(md->queue, dm_request);
1876 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
1877 md->queue->unplug_fn = dm_unplug_all;
1878 blk_queue_merge_bvec(md->queue, dm_merge_bvec);
1882 * Allocate and initialise a blank device with a given minor.
1884 static struct mapped_device *alloc_dev(int minor)
1886 int r;
1887 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
1888 void *old_md;
1890 if (!md) {
1891 DMWARN("unable to allocate device, out of memory.");
1892 return NULL;
1895 if (!try_module_get(THIS_MODULE))
1896 goto bad_module_get;
1898 /* get a minor number for the dev */
1899 if (minor == DM_ANY_MINOR)
1900 r = next_free_minor(&minor);
1901 else
1902 r = specific_minor(minor);
1903 if (r < 0)
1904 goto bad_minor;
1906 md->type = DM_TYPE_NONE;
1907 init_rwsem(&md->io_lock);
1908 mutex_init(&md->suspend_lock);
1909 mutex_init(&md->type_lock);
1910 spin_lock_init(&md->deferred_lock);
1911 spin_lock_init(&md->barrier_error_lock);
1912 rwlock_init(&md->map_lock);
1913 atomic_set(&md->holders, 1);
1914 atomic_set(&md->open_count, 0);
1915 atomic_set(&md->event_nr, 0);
1916 atomic_set(&md->uevent_seq, 0);
1917 INIT_LIST_HEAD(&md->uevent_list);
1918 spin_lock_init(&md->uevent_lock);
1920 md->queue = blk_alloc_queue(GFP_KERNEL);
1921 if (!md->queue)
1922 goto bad_queue;
1924 dm_init_md_queue(md);
1926 md->disk = alloc_disk(1);
1927 if (!md->disk)
1928 goto bad_disk;
1930 atomic_set(&md->pending[0], 0);
1931 atomic_set(&md->pending[1], 0);
1932 init_waitqueue_head(&md->wait);
1933 INIT_WORK(&md->work, dm_wq_work);
1934 INIT_WORK(&md->barrier_work, dm_rq_barrier_work);
1935 init_waitqueue_head(&md->eventq);
1937 md->disk->major = _major;
1938 md->disk->first_minor = minor;
1939 md->disk->fops = &dm_blk_dops;
1940 md->disk->queue = md->queue;
1941 md->disk->private_data = md;
1942 sprintf(md->disk->disk_name, "dm-%d", minor);
1943 add_disk(md->disk);
1944 format_dev_t(md->name, MKDEV(_major, minor));
1946 md->wq = create_singlethread_workqueue("kdmflush");
1947 if (!md->wq)
1948 goto bad_thread;
1950 md->bdev = bdget_disk(md->disk, 0);
1951 if (!md->bdev)
1952 goto bad_bdev;
1954 /* Populate the mapping, nobody knows we exist yet */
1955 spin_lock(&_minor_lock);
1956 old_md = idr_replace(&_minor_idr, md, minor);
1957 spin_unlock(&_minor_lock);
1959 BUG_ON(old_md != MINOR_ALLOCED);
1961 return md;
1963 bad_bdev:
1964 destroy_workqueue(md->wq);
1965 bad_thread:
1966 del_gendisk(md->disk);
1967 put_disk(md->disk);
1968 bad_disk:
1969 blk_cleanup_queue(md->queue);
1970 bad_queue:
1971 free_minor(minor);
1972 bad_minor:
1973 module_put(THIS_MODULE);
1974 bad_module_get:
1975 kfree(md);
1976 return NULL;
1979 static void unlock_fs(struct mapped_device *md);
1981 static void free_dev(struct mapped_device *md)
1983 int minor = MINOR(disk_devt(md->disk));
1985 unlock_fs(md);
1986 bdput(md->bdev);
1987 destroy_workqueue(md->wq);
1988 if (md->tio_pool)
1989 mempool_destroy(md->tio_pool);
1990 if (md->io_pool)
1991 mempool_destroy(md->io_pool);
1992 if (md->bs)
1993 bioset_free(md->bs);
1994 blk_integrity_unregister(md->disk);
1995 del_gendisk(md->disk);
1996 free_minor(minor);
1998 spin_lock(&_minor_lock);
1999 md->disk->private_data = NULL;
2000 spin_unlock(&_minor_lock);
2002 put_disk(md->disk);
2003 blk_cleanup_queue(md->queue);
2004 module_put(THIS_MODULE);
2005 kfree(md);
2008 static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
2010 struct dm_md_mempools *p;
2012 if (md->io_pool && md->tio_pool && md->bs)
2013 /* the md already has necessary mempools */
2014 goto out;
2016 p = dm_table_get_md_mempools(t);
2017 BUG_ON(!p || md->io_pool || md->tio_pool || md->bs);
2019 md->io_pool = p->io_pool;
2020 p->io_pool = NULL;
2021 md->tio_pool = p->tio_pool;
2022 p->tio_pool = NULL;
2023 md->bs = p->bs;
2024 p->bs = NULL;
2026 out:
2027 /* mempool bind completed, now no need any mempools in the table */
2028 dm_table_free_md_mempools(t);
2032 * Bind a table to the device.
2034 static void event_callback(void *context)
2036 unsigned long flags;
2037 LIST_HEAD(uevents);
2038 struct mapped_device *md = (struct mapped_device *) context;
2040 spin_lock_irqsave(&md->uevent_lock, flags);
2041 list_splice_init(&md->uevent_list, &uevents);
2042 spin_unlock_irqrestore(&md->uevent_lock, flags);
2044 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
2046 atomic_inc(&md->event_nr);
2047 wake_up(&md->eventq);
2050 static void __set_size(struct mapped_device *md, sector_t size)
2052 set_capacity(md->disk, size);
2054 mutex_lock(&md->bdev->bd_inode->i_mutex);
2055 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
2056 mutex_unlock(&md->bdev->bd_inode->i_mutex);
2060 * Returns old map, which caller must destroy.
2062 static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2063 struct queue_limits *limits)
2065 struct dm_table *old_map;
2066 struct request_queue *q = md->queue;
2067 sector_t size;
2068 unsigned long flags;
2070 size = dm_table_get_size(t);
2073 * Wipe any geometry if the size of the table changed.
2075 if (size != get_capacity(md->disk))
2076 memset(&md->geometry, 0, sizeof(md->geometry));
2078 __set_size(md, size);
2080 dm_table_event_callback(t, event_callback, md);
2083 * The queue hasn't been stopped yet, if the old table type wasn't
2084 * for request-based during suspension. So stop it to prevent
2085 * I/O mapping before resume.
2086 * This must be done before setting the queue restrictions,
2087 * because request-based dm may be run just after the setting.
2089 if (dm_table_request_based(t) && !blk_queue_stopped(q))
2090 stop_queue(q);
2092 __bind_mempools(md, t);
2094 write_lock_irqsave(&md->map_lock, flags);
2095 old_map = md->map;
2096 md->map = t;
2097 dm_table_set_restrictions(t, q, limits);
2098 write_unlock_irqrestore(&md->map_lock, flags);
2100 return old_map;
2104 * Returns unbound table for the caller to free.
2106 static struct dm_table *__unbind(struct mapped_device *md)
2108 struct dm_table *map = md->map;
2109 unsigned long flags;
2111 if (!map)
2112 return NULL;
2114 dm_table_event_callback(map, NULL, NULL);
2115 write_lock_irqsave(&md->map_lock, flags);
2116 md->map = NULL;
2117 write_unlock_irqrestore(&md->map_lock, flags);
2119 return map;
2123 * Constructor for a new device.
2125 int dm_create(int minor, struct mapped_device **result)
2127 struct mapped_device *md;
2129 md = alloc_dev(minor);
2130 if (!md)
2131 return -ENXIO;
2133 dm_sysfs_init(md);
2135 *result = md;
2136 return 0;
2140 * Functions to manage md->type.
2141 * All are required to hold md->type_lock.
2143 void dm_lock_md_type(struct mapped_device *md)
2145 mutex_lock(&md->type_lock);
2148 void dm_unlock_md_type(struct mapped_device *md)
2150 mutex_unlock(&md->type_lock);
2153 void dm_set_md_type(struct mapped_device *md, unsigned type)
2155 md->type = type;
2158 unsigned dm_get_md_type(struct mapped_device *md)
2160 return md->type;
2164 * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
2166 static int dm_init_request_based_queue(struct mapped_device *md)
2168 struct request_queue *q = NULL;
2170 if (md->queue->elevator)
2171 return 1;
2173 /* Fully initialize the queue */
2174 q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
2175 if (!q)
2176 return 0;
2178 md->queue = q;
2179 md->saved_make_request_fn = md->queue->make_request_fn;
2180 dm_init_md_queue(md);
2181 blk_queue_softirq_done(md->queue, dm_softirq_done);
2182 blk_queue_prep_rq(md->queue, dm_prep_fn);
2183 blk_queue_lld_busy(md->queue, dm_lld_busy);
2184 blk_queue_ordered(md->queue, QUEUE_ORDERED_DRAIN_FLUSH);
2186 elv_register_queue(md->queue);
2188 return 1;
2192 * Setup the DM device's queue based on md's type
2194 int dm_setup_md_queue(struct mapped_device *md)
2196 if ((dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) &&
2197 !dm_init_request_based_queue(md)) {
2198 DMWARN("Cannot initialize queue for request-based mapped device");
2199 return -EINVAL;
2202 return 0;
2205 static struct mapped_device *dm_find_md(dev_t dev)
2207 struct mapped_device *md;
2208 unsigned minor = MINOR(dev);
2210 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2211 return NULL;
2213 spin_lock(&_minor_lock);
2215 md = idr_find(&_minor_idr, minor);
2216 if (md && (md == MINOR_ALLOCED ||
2217 (MINOR(disk_devt(dm_disk(md))) != minor) ||
2218 dm_deleting_md(md) ||
2219 test_bit(DMF_FREEING, &md->flags))) {
2220 md = NULL;
2221 goto out;
2224 out:
2225 spin_unlock(&_minor_lock);
2227 return md;
2230 struct mapped_device *dm_get_md(dev_t dev)
2232 struct mapped_device *md = dm_find_md(dev);
2234 if (md)
2235 dm_get(md);
2237 return md;
2240 void *dm_get_mdptr(struct mapped_device *md)
2242 return md->interface_ptr;
2245 void dm_set_mdptr(struct mapped_device *md, void *ptr)
2247 md->interface_ptr = ptr;
2250 void dm_get(struct mapped_device *md)
2252 atomic_inc(&md->holders);
2253 BUG_ON(test_bit(DMF_FREEING, &md->flags));
2256 const char *dm_device_name(struct mapped_device *md)
2258 return md->name;
2260 EXPORT_SYMBOL_GPL(dm_device_name);
2262 static void __dm_destroy(struct mapped_device *md, bool wait)
2264 struct dm_table *map;
2266 might_sleep();
2268 spin_lock(&_minor_lock);
2269 map = dm_get_live_table(md);
2270 idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2271 set_bit(DMF_FREEING, &md->flags);
2272 spin_unlock(&_minor_lock);
2274 if (!dm_suspended_md(md)) {
2275 dm_table_presuspend_targets(map);
2276 dm_table_postsuspend_targets(map);
2280 * Rare, but there may be I/O requests still going to complete,
2281 * for example. Wait for all references to disappear.
2282 * No one should increment the reference count of the mapped_device,
2283 * after the mapped_device state becomes DMF_FREEING.
2285 if (wait)
2286 while (atomic_read(&md->holders))
2287 msleep(1);
2288 else if (atomic_read(&md->holders))
2289 DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
2290 dm_device_name(md), atomic_read(&md->holders));
2292 dm_sysfs_exit(md);
2293 dm_table_put(map);
2294 dm_table_destroy(__unbind(md));
2295 free_dev(md);
2298 void dm_destroy(struct mapped_device *md)
2300 __dm_destroy(md, true);
2303 void dm_destroy_immediate(struct mapped_device *md)
2305 __dm_destroy(md, false);
2308 void dm_put(struct mapped_device *md)
2310 atomic_dec(&md->holders);
2312 EXPORT_SYMBOL_GPL(dm_put);
2314 static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
2316 int r = 0;
2317 DECLARE_WAITQUEUE(wait, current);
2319 dm_unplug_all(md->queue);
2321 add_wait_queue(&md->wait, &wait);
2323 while (1) {
2324 set_current_state(interruptible);
2326 smp_mb();
2327 if (!md_in_flight(md))
2328 break;
2330 if (interruptible == TASK_INTERRUPTIBLE &&
2331 signal_pending(current)) {
2332 r = -EINTR;
2333 break;
2336 io_schedule();
2338 set_current_state(TASK_RUNNING);
2340 remove_wait_queue(&md->wait, &wait);
2342 return r;
2345 static void dm_flush(struct mapped_device *md)
2347 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2349 bio_init(&md->barrier_bio);
2350 md->barrier_bio.bi_bdev = md->bdev;
2351 md->barrier_bio.bi_rw = WRITE_BARRIER;
2352 __split_and_process_bio(md, &md->barrier_bio);
2354 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2357 static void process_barrier(struct mapped_device *md, struct bio *bio)
2359 md->barrier_error = 0;
2361 dm_flush(md);
2363 if (!bio_empty_barrier(bio)) {
2364 __split_and_process_bio(md, bio);
2366 * If the request isn't supported, don't waste time with
2367 * the second flush.
2369 if (md->barrier_error != -EOPNOTSUPP)
2370 dm_flush(md);
2373 if (md->barrier_error != DM_ENDIO_REQUEUE)
2374 bio_endio(bio, md->barrier_error);
2375 else {
2376 spin_lock_irq(&md->deferred_lock);
2377 bio_list_add_head(&md->deferred, bio);
2378 spin_unlock_irq(&md->deferred_lock);
2383 * Process the deferred bios
2385 static void dm_wq_work(struct work_struct *work)
2387 struct mapped_device *md = container_of(work, struct mapped_device,
2388 work);
2389 struct bio *c;
2391 down_write(&md->io_lock);
2393 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2394 spin_lock_irq(&md->deferred_lock);
2395 c = bio_list_pop(&md->deferred);
2396 spin_unlock_irq(&md->deferred_lock);
2398 if (!c) {
2399 clear_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
2400 break;
2403 up_write(&md->io_lock);
2405 if (dm_request_based(md))
2406 generic_make_request(c);
2407 else {
2408 if (c->bi_rw & REQ_HARDBARRIER)
2409 process_barrier(md, c);
2410 else
2411 __split_and_process_bio(md, c);
2414 down_write(&md->io_lock);
2417 up_write(&md->io_lock);
2420 static void dm_queue_flush(struct mapped_device *md)
2422 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2423 smp_mb__after_clear_bit();
2424 queue_work(md->wq, &md->work);
2427 static void dm_rq_set_target_request_nr(struct request *clone, unsigned request_nr)
2429 struct dm_rq_target_io *tio = clone->end_io_data;
2431 tio->info.target_request_nr = request_nr;
2434 /* Issue barrier requests to targets and wait for their completion. */
2435 static int dm_rq_barrier(struct mapped_device *md)
2437 int i, j;
2438 struct dm_table *map = dm_get_live_table(md);
2439 unsigned num_targets = dm_table_get_num_targets(map);
2440 struct dm_target *ti;
2441 struct request *clone;
2443 md->barrier_error = 0;
2445 for (i = 0; i < num_targets; i++) {
2446 ti = dm_table_get_target(map, i);
2447 for (j = 0; j < ti->num_flush_requests; j++) {
2448 clone = clone_rq(md->flush_request, md, GFP_NOIO);
2449 dm_rq_set_target_request_nr(clone, j);
2450 atomic_inc(&md->pending[rq_data_dir(clone)]);
2451 map_request(ti, clone, md);
2455 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2456 dm_table_put(map);
2458 return md->barrier_error;
2461 static void dm_rq_barrier_work(struct work_struct *work)
2463 int error;
2464 struct mapped_device *md = container_of(work, struct mapped_device,
2465 barrier_work);
2466 struct request_queue *q = md->queue;
2467 struct request *rq;
2468 unsigned long flags;
2471 * Hold the md reference here and leave it at the last part so that
2472 * the md can't be deleted by device opener when the barrier request
2473 * completes.
2475 dm_get(md);
2477 error = dm_rq_barrier(md);
2479 rq = md->flush_request;
2480 md->flush_request = NULL;
2482 if (error == DM_ENDIO_REQUEUE) {
2483 spin_lock_irqsave(q->queue_lock, flags);
2484 blk_requeue_request(q, rq);
2485 spin_unlock_irqrestore(q->queue_lock, flags);
2486 } else
2487 blk_end_request_all(rq, error);
2489 blk_run_queue(q);
2491 dm_put(md);
2495 * Swap in a new table, returning the old one for the caller to destroy.
2497 struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
2499 struct dm_table *map = ERR_PTR(-EINVAL);
2500 struct queue_limits limits;
2501 int r;
2503 mutex_lock(&md->suspend_lock);
2505 /* device must be suspended */
2506 if (!dm_suspended_md(md))
2507 goto out;
2509 r = dm_calculate_queue_limits(table, &limits);
2510 if (r) {
2511 map = ERR_PTR(r);
2512 goto out;
2515 map = __bind(md, table, &limits);
2517 out:
2518 mutex_unlock(&md->suspend_lock);
2519 return map;
2523 * Functions to lock and unlock any filesystem running on the
2524 * device.
2526 static int lock_fs(struct mapped_device *md)
2528 int r;
2530 WARN_ON(md->frozen_sb);
2532 md->frozen_sb = freeze_bdev(md->bdev);
2533 if (IS_ERR(md->frozen_sb)) {
2534 r = PTR_ERR(md->frozen_sb);
2535 md->frozen_sb = NULL;
2536 return r;
2539 set_bit(DMF_FROZEN, &md->flags);
2541 return 0;
2544 static void unlock_fs(struct mapped_device *md)
2546 if (!test_bit(DMF_FROZEN, &md->flags))
2547 return;
2549 thaw_bdev(md->bdev, md->frozen_sb);
2550 md->frozen_sb = NULL;
2551 clear_bit(DMF_FROZEN, &md->flags);
2555 * We need to be able to change a mapping table under a mounted
2556 * filesystem. For example we might want to move some data in
2557 * the background. Before the table can be swapped with
2558 * dm_bind_table, dm_suspend must be called to flush any in
2559 * flight bios and ensure that any further io gets deferred.
2562 * Suspend mechanism in request-based dm.
2564 * 1. Flush all I/Os by lock_fs() if needed.
2565 * 2. Stop dispatching any I/O by stopping the request_queue.
2566 * 3. Wait for all in-flight I/Os to be completed or requeued.
2568 * To abort suspend, start the request_queue.
2570 int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
2572 struct dm_table *map = NULL;
2573 int r = 0;
2574 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
2575 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
2577 mutex_lock(&md->suspend_lock);
2579 if (dm_suspended_md(md)) {
2580 r = -EINVAL;
2581 goto out_unlock;
2584 map = dm_get_live_table(md);
2587 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2588 * This flag is cleared before dm_suspend returns.
2590 if (noflush)
2591 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2593 /* This does not get reverted if there's an error later. */
2594 dm_table_presuspend_targets(map);
2597 * Flush I/O to the device.
2598 * Any I/O submitted after lock_fs() may not be flushed.
2599 * noflush takes precedence over do_lockfs.
2600 * (lock_fs() flushes I/Os and waits for them to complete.)
2602 if (!noflush && do_lockfs) {
2603 r = lock_fs(md);
2604 if (r)
2605 goto out;
2609 * Here we must make sure that no processes are submitting requests
2610 * to target drivers i.e. no one may be executing
2611 * __split_and_process_bio. This is called from dm_request and
2612 * dm_wq_work.
2614 * To get all processes out of __split_and_process_bio in dm_request,
2615 * we take the write lock. To prevent any process from reentering
2616 * __split_and_process_bio from dm_request, we set
2617 * DMF_QUEUE_IO_TO_THREAD.
2619 * To quiesce the thread (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND
2620 * and call flush_workqueue(md->wq). flush_workqueue will wait until
2621 * dm_wq_work exits and DMF_BLOCK_IO_FOR_SUSPEND will prevent any
2622 * further calls to __split_and_process_bio from dm_wq_work.
2624 down_write(&md->io_lock);
2625 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2626 set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
2627 up_write(&md->io_lock);
2630 * Request-based dm uses md->wq for barrier (dm_rq_barrier_work) which
2631 * can be kicked until md->queue is stopped. So stop md->queue before
2632 * flushing md->wq.
2634 if (dm_request_based(md))
2635 stop_queue(md->queue);
2637 flush_workqueue(md->wq);
2640 * At this point no more requests are entering target request routines.
2641 * We call dm_wait_for_completion to wait for all existing requests
2642 * to finish.
2644 r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
2646 down_write(&md->io_lock);
2647 if (noflush)
2648 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2649 up_write(&md->io_lock);
2651 /* were we interrupted ? */
2652 if (r < 0) {
2653 dm_queue_flush(md);
2655 if (dm_request_based(md))
2656 start_queue(md->queue);
2658 unlock_fs(md);
2659 goto out; /* pushback list is already flushed, so skip flush */
2663 * If dm_wait_for_completion returned 0, the device is completely
2664 * quiescent now. There is no request-processing activity. All new
2665 * requests are being added to md->deferred list.
2668 set_bit(DMF_SUSPENDED, &md->flags);
2670 dm_table_postsuspend_targets(map);
2672 out:
2673 dm_table_put(map);
2675 out_unlock:
2676 mutex_unlock(&md->suspend_lock);
2677 return r;
2680 int dm_resume(struct mapped_device *md)
2682 int r = -EINVAL;
2683 struct dm_table *map = NULL;
2685 mutex_lock(&md->suspend_lock);
2686 if (!dm_suspended_md(md))
2687 goto out;
2689 map = dm_get_live_table(md);
2690 if (!map || !dm_table_get_size(map))
2691 goto out;
2693 r = dm_table_resume_targets(map);
2694 if (r)
2695 goto out;
2697 dm_queue_flush(md);
2700 * Flushing deferred I/Os must be done after targets are resumed
2701 * so that mapping of targets can work correctly.
2702 * Request-based dm is queueing the deferred I/Os in its request_queue.
2704 if (dm_request_based(md))
2705 start_queue(md->queue);
2707 unlock_fs(md);
2709 clear_bit(DMF_SUSPENDED, &md->flags);
2711 dm_table_unplug_all(map);
2712 r = 0;
2713 out:
2714 dm_table_put(map);
2715 mutex_unlock(&md->suspend_lock);
2717 return r;
2720 /*-----------------------------------------------------------------
2721 * Event notification.
2722 *---------------------------------------------------------------*/
2723 int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
2724 unsigned cookie)
2726 char udev_cookie[DM_COOKIE_LENGTH];
2727 char *envp[] = { udev_cookie, NULL };
2729 if (!cookie)
2730 return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
2731 else {
2732 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
2733 DM_COOKIE_ENV_VAR_NAME, cookie);
2734 return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
2735 action, envp);
2739 uint32_t dm_next_uevent_seq(struct mapped_device *md)
2741 return atomic_add_return(1, &md->uevent_seq);
2744 uint32_t dm_get_event_nr(struct mapped_device *md)
2746 return atomic_read(&md->event_nr);
2749 int dm_wait_event(struct mapped_device *md, int event_nr)
2751 return wait_event_interruptible(md->eventq,
2752 (event_nr != atomic_read(&md->event_nr)));
2755 void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
2757 unsigned long flags;
2759 spin_lock_irqsave(&md->uevent_lock, flags);
2760 list_add(elist, &md->uevent_list);
2761 spin_unlock_irqrestore(&md->uevent_lock, flags);
2765 * The gendisk is only valid as long as you have a reference
2766 * count on 'md'.
2768 struct gendisk *dm_disk(struct mapped_device *md)
2770 return md->disk;
2773 struct kobject *dm_kobject(struct mapped_device *md)
2775 return &md->kobj;
2779 * struct mapped_device should not be exported outside of dm.c
2780 * so use this check to verify that kobj is part of md structure
2782 struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2784 struct mapped_device *md;
2786 md = container_of(kobj, struct mapped_device, kobj);
2787 if (&md->kobj != kobj)
2788 return NULL;
2790 if (test_bit(DMF_FREEING, &md->flags) ||
2791 dm_deleting_md(md))
2792 return NULL;
2794 dm_get(md);
2795 return md;
2798 int dm_suspended_md(struct mapped_device *md)
2800 return test_bit(DMF_SUSPENDED, &md->flags);
2803 int dm_suspended(struct dm_target *ti)
2805 return dm_suspended_md(dm_table_get_md(ti->table));
2807 EXPORT_SYMBOL_GPL(dm_suspended);
2809 int dm_noflush_suspending(struct dm_target *ti)
2811 return __noflush_suspending(dm_table_get_md(ti->table));
2813 EXPORT_SYMBOL_GPL(dm_noflush_suspending);
2815 struct dm_md_mempools *dm_alloc_md_mempools(unsigned type)
2817 struct dm_md_mempools *pools = kmalloc(sizeof(*pools), GFP_KERNEL);
2819 if (!pools)
2820 return NULL;
2822 pools->io_pool = (type == DM_TYPE_BIO_BASED) ?
2823 mempool_create_slab_pool(MIN_IOS, _io_cache) :
2824 mempool_create_slab_pool(MIN_IOS, _rq_bio_info_cache);
2825 if (!pools->io_pool)
2826 goto free_pools_and_out;
2828 pools->tio_pool = (type == DM_TYPE_BIO_BASED) ?
2829 mempool_create_slab_pool(MIN_IOS, _tio_cache) :
2830 mempool_create_slab_pool(MIN_IOS, _rq_tio_cache);
2831 if (!pools->tio_pool)
2832 goto free_io_pool_and_out;
2834 pools->bs = (type == DM_TYPE_BIO_BASED) ?
2835 bioset_create(16, 0) : bioset_create(MIN_IOS, 0);
2836 if (!pools->bs)
2837 goto free_tio_pool_and_out;
2839 return pools;
2841 free_tio_pool_and_out:
2842 mempool_destroy(pools->tio_pool);
2844 free_io_pool_and_out:
2845 mempool_destroy(pools->io_pool);
2847 free_pools_and_out:
2848 kfree(pools);
2850 return NULL;
2853 void dm_free_md_mempools(struct dm_md_mempools *pools)
2855 if (!pools)
2856 return;
2858 if (pools->io_pool)
2859 mempool_destroy(pools->io_pool);
2861 if (pools->tio_pool)
2862 mempool_destroy(pools->tio_pool);
2864 if (pools->bs)
2865 bioset_free(pools->bs);
2867 kfree(pools);
2870 static const struct block_device_operations dm_blk_dops = {
2871 .open = dm_blk_open,
2872 .release = dm_blk_close,
2873 .ioctl = dm_blk_ioctl,
2874 .getgeo = dm_blk_getgeo,
2875 .owner = THIS_MODULE
2878 EXPORT_SYMBOL(dm_get_mapinfo);
2881 * module hooks
2883 module_init(dm_init);
2884 module_exit(dm_exit);
2886 module_param(major, uint, 0);
2887 MODULE_PARM_DESC(major, "The major number of the device mapper");
2888 MODULE_DESCRIPTION(DM_NAME " driver");
2889 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2890 MODULE_LICENSE("GPL");