dm: fix idr leak on module removal
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / md / dm.c
blobc988ac2d8c96f33e03332ff74458b3637abdd7c7
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/mempool.h>
19 #include <linux/slab.h>
20 #include <linux/idr.h>
21 #include <linux/hdreg.h>
23 #include <trace/events/block.h>
25 #define DM_MSG_PREFIX "core"
28 * Cookies are numeric values sent with CHANGE and REMOVE
29 * uevents while resuming, removing or renaming the device.
31 #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
32 #define DM_COOKIE_LENGTH 24
34 static const char *_name = DM_NAME;
36 static unsigned int major = 0;
37 static unsigned int _major = 0;
39 static DEFINE_IDR(_minor_idr);
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 struct gendisk *disk;
129 char name[16];
131 void *interface_ptr;
134 * A list of ios that arrived while we were suspended.
136 atomic_t pending[2];
137 wait_queue_head_t wait;
138 struct work_struct work;
139 struct bio_list deferred;
140 spinlock_t deferred_lock;
143 * An error from the barrier request currently being processed.
145 int barrier_error;
148 * Processing queue (flush/barriers)
150 struct workqueue_struct *wq;
153 * The current mapping.
155 struct dm_table *map;
158 * io objects are allocated from here.
160 mempool_t *io_pool;
161 mempool_t *tio_pool;
163 struct bio_set *bs;
166 * Event handling.
168 atomic_t event_nr;
169 wait_queue_head_t eventq;
170 atomic_t uevent_seq;
171 struct list_head uevent_list;
172 spinlock_t uevent_lock; /* Protect access to uevent_list */
175 * freeze/thaw support require holding onto a super block
177 struct super_block *frozen_sb;
178 struct block_device *bdev;
180 /* forced geometry settings */
181 struct hd_geometry geometry;
183 /* marker of flush suspend for request-based dm */
184 struct request suspend_rq;
186 /* For saving the address of __make_request for request based dm */
187 make_request_fn *saved_make_request_fn;
189 /* sysfs handle */
190 struct kobject kobj;
192 /* zero-length barrier that will be cloned and submitted to targets */
193 struct bio barrier_bio;
197 * For mempools pre-allocation at the table loading time.
199 struct dm_md_mempools {
200 mempool_t *io_pool;
201 mempool_t *tio_pool;
202 struct bio_set *bs;
205 #define MIN_IOS 256
206 static struct kmem_cache *_io_cache;
207 static struct kmem_cache *_tio_cache;
208 static struct kmem_cache *_rq_tio_cache;
209 static struct kmem_cache *_rq_bio_info_cache;
211 static int __init local_init(void)
213 int r = -ENOMEM;
215 /* allocate a slab for the dm_ios */
216 _io_cache = KMEM_CACHE(dm_io, 0);
217 if (!_io_cache)
218 return r;
220 /* allocate a slab for the target ios */
221 _tio_cache = KMEM_CACHE(dm_target_io, 0);
222 if (!_tio_cache)
223 goto out_free_io_cache;
225 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
226 if (!_rq_tio_cache)
227 goto out_free_tio_cache;
229 _rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0);
230 if (!_rq_bio_info_cache)
231 goto out_free_rq_tio_cache;
233 r = dm_uevent_init();
234 if (r)
235 goto out_free_rq_bio_info_cache;
237 _major = major;
238 r = register_blkdev(_major, _name);
239 if (r < 0)
240 goto out_uevent_exit;
242 if (!_major)
243 _major = r;
245 return 0;
247 out_uevent_exit:
248 dm_uevent_exit();
249 out_free_rq_bio_info_cache:
250 kmem_cache_destroy(_rq_bio_info_cache);
251 out_free_rq_tio_cache:
252 kmem_cache_destroy(_rq_tio_cache);
253 out_free_tio_cache:
254 kmem_cache_destroy(_tio_cache);
255 out_free_io_cache:
256 kmem_cache_destroy(_io_cache);
258 return r;
261 static void local_exit(void)
263 kmem_cache_destroy(_rq_bio_info_cache);
264 kmem_cache_destroy(_rq_tio_cache);
265 kmem_cache_destroy(_tio_cache);
266 kmem_cache_destroy(_io_cache);
267 unregister_blkdev(_major, _name);
268 dm_uevent_exit();
270 _major = 0;
272 DMINFO("cleaned up");
275 static int (*_inits[])(void) __initdata = {
276 local_init,
277 dm_target_init,
278 dm_linear_init,
279 dm_stripe_init,
280 dm_kcopyd_init,
281 dm_interface_init,
284 static void (*_exits[])(void) = {
285 local_exit,
286 dm_target_exit,
287 dm_linear_exit,
288 dm_stripe_exit,
289 dm_kcopyd_exit,
290 dm_interface_exit,
293 static int __init dm_init(void)
295 const int count = ARRAY_SIZE(_inits);
297 int r, i;
299 for (i = 0; i < count; i++) {
300 r = _inits[i]();
301 if (r)
302 goto bad;
305 return 0;
307 bad:
308 while (i--)
309 _exits[i]();
311 return r;
314 static void __exit dm_exit(void)
316 int i = ARRAY_SIZE(_exits);
318 while (i--)
319 _exits[i]();
322 * Should be empty by this point.
324 idr_remove_all(&_minor_idr);
325 idr_destroy(&_minor_idr);
329 * Block device functions
331 static int dm_blk_open(struct block_device *bdev, fmode_t mode)
333 struct mapped_device *md;
335 spin_lock(&_minor_lock);
337 md = bdev->bd_disk->private_data;
338 if (!md)
339 goto out;
341 if (test_bit(DMF_FREEING, &md->flags) ||
342 test_bit(DMF_DELETING, &md->flags)) {
343 md = NULL;
344 goto out;
347 dm_get(md);
348 atomic_inc(&md->open_count);
350 out:
351 spin_unlock(&_minor_lock);
353 return md ? 0 : -ENXIO;
356 static int dm_blk_close(struct gendisk *disk, fmode_t mode)
358 struct mapped_device *md = disk->private_data;
359 atomic_dec(&md->open_count);
360 dm_put(md);
361 return 0;
364 int dm_open_count(struct mapped_device *md)
366 return atomic_read(&md->open_count);
370 * Guarantees nothing is using the device before it's deleted.
372 int dm_lock_for_deletion(struct mapped_device *md)
374 int r = 0;
376 spin_lock(&_minor_lock);
378 if (dm_open_count(md))
379 r = -EBUSY;
380 else
381 set_bit(DMF_DELETING, &md->flags);
383 spin_unlock(&_minor_lock);
385 return r;
388 static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
390 struct mapped_device *md = bdev->bd_disk->private_data;
392 return dm_get_geometry(md, geo);
395 static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
396 unsigned int cmd, unsigned long arg)
398 struct mapped_device *md = bdev->bd_disk->private_data;
399 struct dm_table *map = dm_get_table(md);
400 struct dm_target *tgt;
401 int r = -ENOTTY;
403 if (!map || !dm_table_get_size(map))
404 goto out;
406 /* We only support devices that have a single target */
407 if (dm_table_get_num_targets(map) != 1)
408 goto out;
410 tgt = dm_table_get_target(map, 0);
412 if (dm_suspended(md)) {
413 r = -EAGAIN;
414 goto out;
417 if (tgt->type->ioctl)
418 r = tgt->type->ioctl(tgt, cmd, arg);
420 out:
421 dm_table_put(map);
423 return r;
426 static struct dm_io *alloc_io(struct mapped_device *md)
428 return mempool_alloc(md->io_pool, GFP_NOIO);
431 static void free_io(struct mapped_device *md, struct dm_io *io)
433 mempool_free(io, md->io_pool);
436 static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
438 mempool_free(tio, md->tio_pool);
441 static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md)
443 return mempool_alloc(md->tio_pool, GFP_ATOMIC);
446 static void free_rq_tio(struct dm_rq_target_io *tio)
448 mempool_free(tio, tio->md->tio_pool);
451 static struct dm_rq_clone_bio_info *alloc_bio_info(struct mapped_device *md)
453 return mempool_alloc(md->io_pool, GFP_ATOMIC);
456 static void free_bio_info(struct dm_rq_clone_bio_info *info)
458 mempool_free(info, info->tio->md->io_pool);
461 static void start_io_acct(struct dm_io *io)
463 struct mapped_device *md = io->md;
464 int cpu;
465 int rw = bio_data_dir(io->bio);
467 io->start_time = jiffies;
469 cpu = part_stat_lock();
470 part_round_stats(cpu, &dm_disk(md)->part0);
471 part_stat_unlock();
472 dm_disk(md)->part0.in_flight[rw] = atomic_inc_return(&md->pending[rw]);
475 static void end_io_acct(struct dm_io *io)
477 struct mapped_device *md = io->md;
478 struct bio *bio = io->bio;
479 unsigned long duration = jiffies - io->start_time;
480 int pending, cpu;
481 int rw = bio_data_dir(bio);
483 cpu = part_stat_lock();
484 part_round_stats(cpu, &dm_disk(md)->part0);
485 part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
486 part_stat_unlock();
489 * After this is decremented the bio must not be touched if it is
490 * a barrier.
492 dm_disk(md)->part0.in_flight[rw] = pending =
493 atomic_dec_return(&md->pending[rw]);
494 pending += atomic_read(&md->pending[rw^0x1]);
496 /* nudge anyone waiting on suspend queue */
497 if (!pending)
498 wake_up(&md->wait);
502 * Add the bio to the list of deferred io.
504 static void queue_io(struct mapped_device *md, struct bio *bio)
506 down_write(&md->io_lock);
508 spin_lock_irq(&md->deferred_lock);
509 bio_list_add(&md->deferred, bio);
510 spin_unlock_irq(&md->deferred_lock);
512 if (!test_and_set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags))
513 queue_work(md->wq, &md->work);
515 up_write(&md->io_lock);
519 * Everyone (including functions in this file), should use this
520 * function to access the md->map field, and make sure they call
521 * dm_table_put() when finished.
523 struct dm_table *dm_get_table(struct mapped_device *md)
525 struct dm_table *t;
526 unsigned long flags;
528 read_lock_irqsave(&md->map_lock, flags);
529 t = md->map;
530 if (t)
531 dm_table_get(t);
532 read_unlock_irqrestore(&md->map_lock, flags);
534 return t;
538 * Get the geometry associated with a dm device
540 int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
542 *geo = md->geometry;
544 return 0;
548 * Set the geometry of a device.
550 int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
552 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
554 if (geo->start > sz) {
555 DMWARN("Start sector is beyond the geometry limits.");
556 return -EINVAL;
559 md->geometry = *geo;
561 return 0;
564 /*-----------------------------------------------------------------
565 * CRUD START:
566 * A more elegant soln is in the works that uses the queue
567 * merge fn, unfortunately there are a couple of changes to
568 * the block layer that I want to make for this. So in the
569 * interests of getting something for people to use I give
570 * you this clearly demarcated crap.
571 *---------------------------------------------------------------*/
573 static int __noflush_suspending(struct mapped_device *md)
575 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
579 * Decrements the number of outstanding ios that a bio has been
580 * cloned into, completing the original io if necc.
582 static void dec_pending(struct dm_io *io, int error)
584 unsigned long flags;
585 int io_error;
586 struct bio *bio;
587 struct mapped_device *md = io->md;
589 /* Push-back supersedes any I/O errors */
590 if (unlikely(error)) {
591 spin_lock_irqsave(&io->endio_lock, flags);
592 if (!(io->error > 0 && __noflush_suspending(md)))
593 io->error = error;
594 spin_unlock_irqrestore(&io->endio_lock, flags);
597 if (atomic_dec_and_test(&io->io_count)) {
598 if (io->error == DM_ENDIO_REQUEUE) {
600 * Target requested pushing back the I/O.
602 spin_lock_irqsave(&md->deferred_lock, flags);
603 if (__noflush_suspending(md)) {
604 if (!bio_rw_flagged(io->bio, BIO_RW_BARRIER))
605 bio_list_add_head(&md->deferred,
606 io->bio);
607 } else
608 /* noflush suspend was interrupted. */
609 io->error = -EIO;
610 spin_unlock_irqrestore(&md->deferred_lock, flags);
613 io_error = io->error;
614 bio = io->bio;
616 if (bio_rw_flagged(bio, BIO_RW_BARRIER)) {
618 * There can be just one barrier request so we use
619 * a per-device variable for error reporting.
620 * Note that you can't touch the bio after end_io_acct
622 if (!md->barrier_error && io_error != -EOPNOTSUPP)
623 md->barrier_error = io_error;
624 end_io_acct(io);
625 free_io(md, io);
626 } else {
627 end_io_acct(io);
628 free_io(md, io);
630 if (io_error != DM_ENDIO_REQUEUE) {
631 trace_block_bio_complete(md->queue, bio);
633 bio_endio(bio, io_error);
639 static void clone_endio(struct bio *bio, int error)
641 int r = 0;
642 struct dm_target_io *tio = bio->bi_private;
643 struct dm_io *io = tio->io;
644 struct mapped_device *md = tio->io->md;
645 dm_endio_fn endio = tio->ti->type->end_io;
647 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
648 error = -EIO;
650 if (endio) {
651 r = endio(tio->ti, bio, error, &tio->info);
652 if (r < 0 || r == DM_ENDIO_REQUEUE)
654 * error and requeue request are handled
655 * in dec_pending().
657 error = r;
658 else if (r == DM_ENDIO_INCOMPLETE)
659 /* The target will handle the io */
660 return;
661 else if (r) {
662 DMWARN("unimplemented target endio return value: %d", r);
663 BUG();
668 * Store md for cleanup instead of tio which is about to get freed.
670 bio->bi_private = md->bs;
672 free_tio(md, tio);
673 bio_put(bio);
674 dec_pending(io, error);
678 * Partial completion handling for request-based dm
680 static void end_clone_bio(struct bio *clone, int error)
682 struct dm_rq_clone_bio_info *info = clone->bi_private;
683 struct dm_rq_target_io *tio = info->tio;
684 struct bio *bio = info->orig;
685 unsigned int nr_bytes = info->orig->bi_size;
687 bio_put(clone);
689 if (tio->error)
691 * An error has already been detected on the request.
692 * Once error occurred, just let clone->end_io() handle
693 * the remainder.
695 return;
696 else if (error) {
698 * Don't notice the error to the upper layer yet.
699 * The error handling decision is made by the target driver,
700 * when the request is completed.
702 tio->error = error;
703 return;
707 * I/O for the bio successfully completed.
708 * Notice the data completion to the upper layer.
712 * bios are processed from the head of the list.
713 * So the completing bio should always be rq->bio.
714 * If it's not, something wrong is happening.
716 if (tio->orig->bio != bio)
717 DMERR("bio completion is going in the middle of the request");
720 * Update the original request.
721 * Do not use blk_end_request() here, because it may complete
722 * the original request before the clone, and break the ordering.
724 blk_update_request(tio->orig, 0, nr_bytes);
728 * Don't touch any member of the md after calling this function because
729 * the md may be freed in dm_put() at the end of this function.
730 * Or do dm_get() before calling this function and dm_put() later.
732 static void rq_completed(struct mapped_device *md, int run_queue)
734 int wakeup_waiters = 0;
735 struct request_queue *q = md->queue;
736 unsigned long flags;
738 spin_lock_irqsave(q->queue_lock, flags);
739 if (!queue_in_flight(q))
740 wakeup_waiters = 1;
741 spin_unlock_irqrestore(q->queue_lock, flags);
743 /* nudge anyone waiting on suspend queue */
744 if (wakeup_waiters)
745 wake_up(&md->wait);
747 if (run_queue)
748 blk_run_queue(q);
751 * dm_put() must be at the end of this function. See the comment above
753 dm_put(md);
756 static void free_rq_clone(struct request *clone)
758 struct dm_rq_target_io *tio = clone->end_io_data;
760 blk_rq_unprep_clone(clone);
761 free_rq_tio(tio);
764 static void dm_unprep_request(struct request *rq)
766 struct request *clone = rq->special;
768 rq->special = NULL;
769 rq->cmd_flags &= ~REQ_DONTPREP;
771 free_rq_clone(clone);
775 * Requeue the original request of a clone.
777 void dm_requeue_unmapped_request(struct request *clone)
779 struct dm_rq_target_io *tio = clone->end_io_data;
780 struct mapped_device *md = tio->md;
781 struct request *rq = tio->orig;
782 struct request_queue *q = rq->q;
783 unsigned long flags;
785 dm_unprep_request(rq);
787 spin_lock_irqsave(q->queue_lock, flags);
788 if (elv_queue_empty(q))
789 blk_plug_device(q);
790 blk_requeue_request(q, rq);
791 spin_unlock_irqrestore(q->queue_lock, flags);
793 rq_completed(md, 0);
795 EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
797 static void __stop_queue(struct request_queue *q)
799 blk_stop_queue(q);
802 static void stop_queue(struct request_queue *q)
804 unsigned long flags;
806 spin_lock_irqsave(q->queue_lock, flags);
807 __stop_queue(q);
808 spin_unlock_irqrestore(q->queue_lock, flags);
811 static void __start_queue(struct request_queue *q)
813 if (blk_queue_stopped(q))
814 blk_start_queue(q);
817 static void start_queue(struct request_queue *q)
819 unsigned long flags;
821 spin_lock_irqsave(q->queue_lock, flags);
822 __start_queue(q);
823 spin_unlock_irqrestore(q->queue_lock, flags);
827 * Complete the clone and the original request.
828 * Must be called without queue lock.
830 static void dm_end_request(struct request *clone, int error)
832 struct dm_rq_target_io *tio = clone->end_io_data;
833 struct mapped_device *md = tio->md;
834 struct request *rq = tio->orig;
836 if (blk_pc_request(rq)) {
837 rq->errors = clone->errors;
838 rq->resid_len = clone->resid_len;
840 if (rq->sense)
842 * We are using the sense buffer of the original
843 * request.
844 * So setting the length of the sense data is enough.
846 rq->sense_len = clone->sense_len;
849 free_rq_clone(clone);
851 blk_end_request_all(rq, error);
853 rq_completed(md, 1);
857 * Request completion handler for request-based dm
859 static void dm_softirq_done(struct request *rq)
861 struct request *clone = rq->completion_data;
862 struct dm_rq_target_io *tio = clone->end_io_data;
863 dm_request_endio_fn rq_end_io = tio->ti->type->rq_end_io;
864 int error = tio->error;
866 if (!(rq->cmd_flags & REQ_FAILED) && rq_end_io)
867 error = rq_end_io(tio->ti, clone, error, &tio->info);
869 if (error <= 0)
870 /* The target wants to complete the I/O */
871 dm_end_request(clone, error);
872 else if (error == DM_ENDIO_INCOMPLETE)
873 /* The target will handle the I/O */
874 return;
875 else if (error == DM_ENDIO_REQUEUE)
876 /* The target wants to requeue the I/O */
877 dm_requeue_unmapped_request(clone);
878 else {
879 DMWARN("unimplemented target endio return value: %d", error);
880 BUG();
885 * Complete the clone and the original request with the error status
886 * through softirq context.
888 static void dm_complete_request(struct request *clone, int error)
890 struct dm_rq_target_io *tio = clone->end_io_data;
891 struct request *rq = tio->orig;
893 tio->error = error;
894 rq->completion_data = clone;
895 blk_complete_request(rq);
899 * Complete the not-mapped clone and the original request with the error status
900 * through softirq context.
901 * Target's rq_end_io() function isn't called.
902 * This may be used when the target's map_rq() function fails.
904 void dm_kill_unmapped_request(struct request *clone, int error)
906 struct dm_rq_target_io *tio = clone->end_io_data;
907 struct request *rq = tio->orig;
909 rq->cmd_flags |= REQ_FAILED;
910 dm_complete_request(clone, error);
912 EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
915 * Called with the queue lock held
917 static void end_clone_request(struct request *clone, int error)
920 * For just cleaning up the information of the queue in which
921 * the clone was dispatched.
922 * The clone is *NOT* freed actually here because it is alloced from
923 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
925 __blk_put_request(clone->q, clone);
928 * Actual request completion is done in a softirq context which doesn't
929 * hold the queue lock. Otherwise, deadlock could occur because:
930 * - another request may be submitted by the upper level driver
931 * of the stacking during the completion
932 * - the submission which requires queue lock may be done
933 * against this queue
935 dm_complete_request(clone, error);
938 static sector_t max_io_len(struct mapped_device *md,
939 sector_t sector, struct dm_target *ti)
941 sector_t offset = sector - ti->begin;
942 sector_t len = ti->len - offset;
945 * Does the target need to split even further ?
947 if (ti->split_io) {
948 sector_t boundary;
949 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
950 - offset;
951 if (len > boundary)
952 len = boundary;
955 return len;
958 static void __map_bio(struct dm_target *ti, struct bio *clone,
959 struct dm_target_io *tio)
961 int r;
962 sector_t sector;
963 struct mapped_device *md;
965 clone->bi_end_io = clone_endio;
966 clone->bi_private = tio;
969 * Map the clone. If r == 0 we don't need to do
970 * anything, the target has assumed ownership of
971 * this io.
973 atomic_inc(&tio->io->io_count);
974 sector = clone->bi_sector;
975 r = ti->type->map(ti, clone, &tio->info);
976 if (r == DM_MAPIO_REMAPPED) {
977 /* the bio has been remapped so dispatch it */
979 trace_block_remap(bdev_get_queue(clone->bi_bdev), clone,
980 tio->io->bio->bi_bdev->bd_dev, sector);
982 generic_make_request(clone);
983 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
984 /* error the io and bail out, or requeue it if needed */
985 md = tio->io->md;
986 dec_pending(tio->io, r);
988 * Store bio_set for cleanup.
990 clone->bi_private = md->bs;
991 bio_put(clone);
992 free_tio(md, tio);
993 } else if (r) {
994 DMWARN("unimplemented target map return value: %d", r);
995 BUG();
999 struct clone_info {
1000 struct mapped_device *md;
1001 struct dm_table *map;
1002 struct bio *bio;
1003 struct dm_io *io;
1004 sector_t sector;
1005 sector_t sector_count;
1006 unsigned short idx;
1009 static void dm_bio_destructor(struct bio *bio)
1011 struct bio_set *bs = bio->bi_private;
1013 bio_free(bio, bs);
1017 * Creates a little bio that is just does part of a bvec.
1019 static struct bio *split_bvec(struct bio *bio, sector_t sector,
1020 unsigned short idx, unsigned int offset,
1021 unsigned int len, struct bio_set *bs)
1023 struct bio *clone;
1024 struct bio_vec *bv = bio->bi_io_vec + idx;
1026 clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
1027 clone->bi_destructor = dm_bio_destructor;
1028 *clone->bi_io_vec = *bv;
1030 clone->bi_sector = sector;
1031 clone->bi_bdev = bio->bi_bdev;
1032 clone->bi_rw = bio->bi_rw & ~(1 << BIO_RW_BARRIER);
1033 clone->bi_vcnt = 1;
1034 clone->bi_size = to_bytes(len);
1035 clone->bi_io_vec->bv_offset = offset;
1036 clone->bi_io_vec->bv_len = clone->bi_size;
1037 clone->bi_flags |= 1 << BIO_CLONED;
1039 if (bio_integrity(bio)) {
1040 bio_integrity_clone(clone, bio, GFP_NOIO, bs);
1041 bio_integrity_trim(clone,
1042 bio_sector_offset(bio, idx, offset), len);
1045 return clone;
1049 * Creates a bio that consists of range of complete bvecs.
1051 static struct bio *clone_bio(struct bio *bio, sector_t sector,
1052 unsigned short idx, unsigned short bv_count,
1053 unsigned int len, struct bio_set *bs)
1055 struct bio *clone;
1057 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
1058 __bio_clone(clone, bio);
1059 clone->bi_rw &= ~(1 << BIO_RW_BARRIER);
1060 clone->bi_destructor = dm_bio_destructor;
1061 clone->bi_sector = sector;
1062 clone->bi_idx = idx;
1063 clone->bi_vcnt = idx + bv_count;
1064 clone->bi_size = to_bytes(len);
1065 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
1067 if (bio_integrity(bio)) {
1068 bio_integrity_clone(clone, bio, GFP_NOIO, bs);
1070 if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
1071 bio_integrity_trim(clone,
1072 bio_sector_offset(bio, idx, 0), len);
1075 return clone;
1078 static struct dm_target_io *alloc_tio(struct clone_info *ci,
1079 struct dm_target *ti)
1081 struct dm_target_io *tio = mempool_alloc(ci->md->tio_pool, GFP_NOIO);
1083 tio->io = ci->io;
1084 tio->ti = ti;
1085 memset(&tio->info, 0, sizeof(tio->info));
1087 return tio;
1090 static void __flush_target(struct clone_info *ci, struct dm_target *ti,
1091 unsigned flush_nr)
1093 struct dm_target_io *tio = alloc_tio(ci, ti);
1094 struct bio *clone;
1096 tio->info.flush_request = flush_nr;
1098 clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
1099 __bio_clone(clone, ci->bio);
1100 clone->bi_destructor = dm_bio_destructor;
1102 __map_bio(ti, clone, tio);
1105 static int __clone_and_map_empty_barrier(struct clone_info *ci)
1107 unsigned target_nr = 0, flush_nr;
1108 struct dm_target *ti;
1110 while ((ti = dm_table_get_target(ci->map, target_nr++)))
1111 for (flush_nr = 0; flush_nr < ti->num_flush_requests;
1112 flush_nr++)
1113 __flush_target(ci, ti, flush_nr);
1115 ci->sector_count = 0;
1117 return 0;
1120 static int __clone_and_map(struct clone_info *ci)
1122 struct bio *clone, *bio = ci->bio;
1123 struct dm_target *ti;
1124 sector_t len = 0, max;
1125 struct dm_target_io *tio;
1127 if (unlikely(bio_empty_barrier(bio)))
1128 return __clone_and_map_empty_barrier(ci);
1130 ti = dm_table_find_target(ci->map, ci->sector);
1131 if (!dm_target_is_valid(ti))
1132 return -EIO;
1134 max = max_io_len(ci->md, ci->sector, ti);
1137 * Allocate a target io object.
1139 tio = alloc_tio(ci, ti);
1141 if (ci->sector_count <= max) {
1143 * Optimise for the simple case where we can do all of
1144 * the remaining io with a single clone.
1146 clone = clone_bio(bio, ci->sector, ci->idx,
1147 bio->bi_vcnt - ci->idx, ci->sector_count,
1148 ci->md->bs);
1149 __map_bio(ti, clone, tio);
1150 ci->sector_count = 0;
1152 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
1154 * There are some bvecs that don't span targets.
1155 * Do as many of these as possible.
1157 int i;
1158 sector_t remaining = max;
1159 sector_t bv_len;
1161 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
1162 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
1164 if (bv_len > remaining)
1165 break;
1167 remaining -= bv_len;
1168 len += bv_len;
1171 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
1172 ci->md->bs);
1173 __map_bio(ti, clone, tio);
1175 ci->sector += len;
1176 ci->sector_count -= len;
1177 ci->idx = i;
1179 } else {
1181 * Handle a bvec that must be split between two or more targets.
1183 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
1184 sector_t remaining = to_sector(bv->bv_len);
1185 unsigned int offset = 0;
1187 do {
1188 if (offset) {
1189 ti = dm_table_find_target(ci->map, ci->sector);
1190 if (!dm_target_is_valid(ti))
1191 return -EIO;
1193 max = max_io_len(ci->md, ci->sector, ti);
1195 tio = alloc_tio(ci, ti);
1198 len = min(remaining, max);
1200 clone = split_bvec(bio, ci->sector, ci->idx,
1201 bv->bv_offset + offset, len,
1202 ci->md->bs);
1204 __map_bio(ti, clone, tio);
1206 ci->sector += len;
1207 ci->sector_count -= len;
1208 offset += to_bytes(len);
1209 } while (remaining -= len);
1211 ci->idx++;
1214 return 0;
1218 * Split the bio into several clones and submit it to targets.
1220 static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
1222 struct clone_info ci;
1223 int error = 0;
1225 ci.map = dm_get_table(md);
1226 if (unlikely(!ci.map)) {
1227 if (!bio_rw_flagged(bio, BIO_RW_BARRIER))
1228 bio_io_error(bio);
1229 else
1230 if (!md->barrier_error)
1231 md->barrier_error = -EIO;
1232 return;
1235 ci.md = md;
1236 ci.bio = bio;
1237 ci.io = alloc_io(md);
1238 ci.io->error = 0;
1239 atomic_set(&ci.io->io_count, 1);
1240 ci.io->bio = bio;
1241 ci.io->md = md;
1242 spin_lock_init(&ci.io->endio_lock);
1243 ci.sector = bio->bi_sector;
1244 ci.sector_count = bio_sectors(bio);
1245 if (unlikely(bio_empty_barrier(bio)))
1246 ci.sector_count = 1;
1247 ci.idx = bio->bi_idx;
1249 start_io_acct(ci.io);
1250 while (ci.sector_count && !error)
1251 error = __clone_and_map(&ci);
1253 /* drop the extra reference count */
1254 dec_pending(ci.io, error);
1255 dm_table_put(ci.map);
1257 /*-----------------------------------------------------------------
1258 * CRUD END
1259 *---------------------------------------------------------------*/
1261 static int dm_merge_bvec(struct request_queue *q,
1262 struct bvec_merge_data *bvm,
1263 struct bio_vec *biovec)
1265 struct mapped_device *md = q->queuedata;
1266 struct dm_table *map = dm_get_table(md);
1267 struct dm_target *ti;
1268 sector_t max_sectors;
1269 int max_size = 0;
1271 if (unlikely(!map))
1272 goto out;
1274 ti = dm_table_find_target(map, bvm->bi_sector);
1275 if (!dm_target_is_valid(ti))
1276 goto out_table;
1279 * Find maximum amount of I/O that won't need splitting
1281 max_sectors = min(max_io_len(md, bvm->bi_sector, ti),
1282 (sector_t) BIO_MAX_SECTORS);
1283 max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1284 if (max_size < 0)
1285 max_size = 0;
1288 * merge_bvec_fn() returns number of bytes
1289 * it can accept at this offset
1290 * max is precomputed maximal io size
1292 if (max_size && ti->type->merge)
1293 max_size = ti->type->merge(ti, bvm, biovec, max_size);
1295 * If the target doesn't support merge method and some of the devices
1296 * provided their merge_bvec method (we know this by looking at
1297 * queue_max_hw_sectors), then we can't allow bios with multiple vector
1298 * entries. So always set max_size to 0, and the code below allows
1299 * just one page.
1301 else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
1303 max_size = 0;
1305 out_table:
1306 dm_table_put(map);
1308 out:
1310 * Always allow an entire first page
1312 if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1313 max_size = biovec->bv_len;
1315 return max_size;
1319 * The request function that just remaps the bio built up by
1320 * dm_merge_bvec.
1322 static int _dm_request(struct request_queue *q, struct bio *bio)
1324 int rw = bio_data_dir(bio);
1325 struct mapped_device *md = q->queuedata;
1326 int cpu;
1328 down_read(&md->io_lock);
1330 cpu = part_stat_lock();
1331 part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1332 part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1333 part_stat_unlock();
1336 * If we're suspended or the thread is processing barriers
1337 * we have to queue this io for later.
1339 if (unlikely(test_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags)) ||
1340 unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) {
1341 up_read(&md->io_lock);
1343 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) &&
1344 bio_rw(bio) == READA) {
1345 bio_io_error(bio);
1346 return 0;
1349 queue_io(md, bio);
1351 return 0;
1354 __split_and_process_bio(md, bio);
1355 up_read(&md->io_lock);
1356 return 0;
1359 static int dm_make_request(struct request_queue *q, struct bio *bio)
1361 struct mapped_device *md = q->queuedata;
1363 if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) {
1364 bio_endio(bio, -EOPNOTSUPP);
1365 return 0;
1368 return md->saved_make_request_fn(q, bio); /* call __make_request() */
1371 static int dm_request_based(struct mapped_device *md)
1373 return blk_queue_stackable(md->queue);
1376 static int dm_request(struct request_queue *q, struct bio *bio)
1378 struct mapped_device *md = q->queuedata;
1380 if (dm_request_based(md))
1381 return dm_make_request(q, bio);
1383 return _dm_request(q, bio);
1386 void dm_dispatch_request(struct request *rq)
1388 int r;
1390 if (blk_queue_io_stat(rq->q))
1391 rq->cmd_flags |= REQ_IO_STAT;
1393 rq->start_time = jiffies;
1394 r = blk_insert_cloned_request(rq->q, rq);
1395 if (r)
1396 dm_complete_request(rq, r);
1398 EXPORT_SYMBOL_GPL(dm_dispatch_request);
1400 static void dm_rq_bio_destructor(struct bio *bio)
1402 struct dm_rq_clone_bio_info *info = bio->bi_private;
1403 struct mapped_device *md = info->tio->md;
1405 free_bio_info(info);
1406 bio_free(bio, md->bs);
1409 static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1410 void *data)
1412 struct dm_rq_target_io *tio = data;
1413 struct mapped_device *md = tio->md;
1414 struct dm_rq_clone_bio_info *info = alloc_bio_info(md);
1416 if (!info)
1417 return -ENOMEM;
1419 info->orig = bio_orig;
1420 info->tio = tio;
1421 bio->bi_end_io = end_clone_bio;
1422 bio->bi_private = info;
1423 bio->bi_destructor = dm_rq_bio_destructor;
1425 return 0;
1428 static int setup_clone(struct request *clone, struct request *rq,
1429 struct dm_rq_target_io *tio)
1431 int r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1432 dm_rq_bio_constructor, tio);
1434 if (r)
1435 return r;
1437 clone->cmd = rq->cmd;
1438 clone->cmd_len = rq->cmd_len;
1439 clone->sense = rq->sense;
1440 clone->buffer = rq->buffer;
1441 clone->end_io = end_clone_request;
1442 clone->end_io_data = tio;
1444 return 0;
1447 static int dm_rq_flush_suspending(struct mapped_device *md)
1449 return !md->suspend_rq.special;
1453 * Called with the queue lock held.
1455 static int dm_prep_fn(struct request_queue *q, struct request *rq)
1457 struct mapped_device *md = q->queuedata;
1458 struct dm_rq_target_io *tio;
1459 struct request *clone;
1461 if (unlikely(rq == &md->suspend_rq)) {
1462 if (dm_rq_flush_suspending(md))
1463 return BLKPREP_OK;
1464 else
1465 /* The flush suspend was interrupted */
1466 return BLKPREP_KILL;
1469 if (unlikely(rq->special)) {
1470 DMWARN("Already has something in rq->special.");
1471 return BLKPREP_KILL;
1474 tio = alloc_rq_tio(md); /* Only one for each original request */
1475 if (!tio)
1476 /* -ENOMEM */
1477 return BLKPREP_DEFER;
1479 tio->md = md;
1480 tio->ti = NULL;
1481 tio->orig = rq;
1482 tio->error = 0;
1483 memset(&tio->info, 0, sizeof(tio->info));
1485 clone = &tio->clone;
1486 if (setup_clone(clone, rq, tio)) {
1487 /* -ENOMEM */
1488 free_rq_tio(tio);
1489 return BLKPREP_DEFER;
1492 rq->special = clone;
1493 rq->cmd_flags |= REQ_DONTPREP;
1495 return BLKPREP_OK;
1499 * Returns:
1500 * 0 : the request has been processed (not requeued)
1501 * !0 : the request has been requeued
1503 static int map_request(struct dm_target *ti, struct request *rq,
1504 struct mapped_device *md)
1506 int r, requeued = 0;
1507 struct request *clone = rq->special;
1508 struct dm_rq_target_io *tio = clone->end_io_data;
1511 * Hold the md reference here for the in-flight I/O.
1512 * We can't rely on the reference count by device opener,
1513 * because the device may be closed during the request completion
1514 * when all bios are completed.
1515 * See the comment in rq_completed() too.
1517 dm_get(md);
1519 tio->ti = ti;
1520 r = ti->type->map_rq(ti, clone, &tio->info);
1521 switch (r) {
1522 case DM_MAPIO_SUBMITTED:
1523 /* The target has taken the I/O to submit by itself later */
1524 break;
1525 case DM_MAPIO_REMAPPED:
1526 /* The target has remapped the I/O so dispatch it */
1527 dm_dispatch_request(clone);
1528 break;
1529 case DM_MAPIO_REQUEUE:
1530 /* The target wants to requeue the I/O */
1531 dm_requeue_unmapped_request(clone);
1532 requeued = 1;
1533 break;
1534 default:
1535 if (r > 0) {
1536 DMWARN("unimplemented target map return value: %d", r);
1537 BUG();
1540 /* The target wants to complete the I/O */
1541 dm_kill_unmapped_request(clone, r);
1542 break;
1545 return requeued;
1549 * q->request_fn for request-based dm.
1550 * Called with the queue lock held.
1552 static void dm_request_fn(struct request_queue *q)
1554 struct mapped_device *md = q->queuedata;
1555 struct dm_table *map = dm_get_table(md);
1556 struct dm_target *ti;
1557 struct request *rq;
1560 * For noflush suspend, check blk_queue_stopped() to immediately
1561 * quit I/O dispatching.
1563 while (!blk_queue_plugged(q) && !blk_queue_stopped(q)) {
1564 rq = blk_peek_request(q);
1565 if (!rq)
1566 goto plug_and_out;
1568 if (unlikely(rq == &md->suspend_rq)) { /* Flush suspend maker */
1569 if (queue_in_flight(q))
1570 /* Not quiet yet. Wait more */
1571 goto plug_and_out;
1573 /* This device should be quiet now */
1574 __stop_queue(q);
1575 blk_start_request(rq);
1576 __blk_end_request_all(rq, 0);
1577 wake_up(&md->wait);
1578 goto out;
1581 ti = dm_table_find_target(map, blk_rq_pos(rq));
1582 if (ti->type->busy && ti->type->busy(ti))
1583 goto plug_and_out;
1585 blk_start_request(rq);
1586 spin_unlock(q->queue_lock);
1587 if (map_request(ti, rq, md))
1588 goto requeued;
1590 spin_lock_irq(q->queue_lock);
1593 goto out;
1595 requeued:
1596 spin_lock_irq(q->queue_lock);
1598 plug_and_out:
1599 if (!elv_queue_empty(q))
1600 /* Some requests still remain, retry later */
1601 blk_plug_device(q);
1603 out:
1604 dm_table_put(map);
1606 return;
1609 int dm_underlying_device_busy(struct request_queue *q)
1611 return blk_lld_busy(q);
1613 EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1615 static int dm_lld_busy(struct request_queue *q)
1617 int r;
1618 struct mapped_device *md = q->queuedata;
1619 struct dm_table *map = dm_get_table(md);
1621 if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1622 r = 1;
1623 else
1624 r = dm_table_any_busy_target(map);
1626 dm_table_put(map);
1628 return r;
1631 static void dm_unplug_all(struct request_queue *q)
1633 struct mapped_device *md = q->queuedata;
1634 struct dm_table *map = dm_get_table(md);
1636 if (map) {
1637 if (dm_request_based(md))
1638 generic_unplug_device(q);
1640 dm_table_unplug_all(map);
1641 dm_table_put(map);
1645 static int dm_any_congested(void *congested_data, int bdi_bits)
1647 int r = bdi_bits;
1648 struct mapped_device *md = congested_data;
1649 struct dm_table *map;
1651 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
1652 map = dm_get_table(md);
1653 if (map) {
1655 * Request-based dm cares about only own queue for
1656 * the query about congestion status of request_queue
1658 if (dm_request_based(md))
1659 r = md->queue->backing_dev_info.state &
1660 bdi_bits;
1661 else
1662 r = dm_table_any_congested(map, bdi_bits);
1664 dm_table_put(map);
1668 return r;
1671 /*-----------------------------------------------------------------
1672 * An IDR is used to keep track of allocated minor numbers.
1673 *---------------------------------------------------------------*/
1674 static void free_minor(int minor)
1676 spin_lock(&_minor_lock);
1677 idr_remove(&_minor_idr, minor);
1678 spin_unlock(&_minor_lock);
1682 * See if the device with a specific minor # is free.
1684 static int specific_minor(int minor)
1686 int r, m;
1688 if (minor >= (1 << MINORBITS))
1689 return -EINVAL;
1691 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
1692 if (!r)
1693 return -ENOMEM;
1695 spin_lock(&_minor_lock);
1697 if (idr_find(&_minor_idr, minor)) {
1698 r = -EBUSY;
1699 goto out;
1702 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
1703 if (r)
1704 goto out;
1706 if (m != minor) {
1707 idr_remove(&_minor_idr, m);
1708 r = -EBUSY;
1709 goto out;
1712 out:
1713 spin_unlock(&_minor_lock);
1714 return r;
1717 static int next_free_minor(int *minor)
1719 int r, m;
1721 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
1722 if (!r)
1723 return -ENOMEM;
1725 spin_lock(&_minor_lock);
1727 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
1728 if (r)
1729 goto out;
1731 if (m >= (1 << MINORBITS)) {
1732 idr_remove(&_minor_idr, m);
1733 r = -ENOSPC;
1734 goto out;
1737 *minor = m;
1739 out:
1740 spin_unlock(&_minor_lock);
1741 return r;
1744 static const struct block_device_operations dm_blk_dops;
1746 static void dm_wq_work(struct work_struct *work);
1749 * Allocate and initialise a blank device with a given minor.
1751 static struct mapped_device *alloc_dev(int minor)
1753 int r;
1754 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
1755 void *old_md;
1757 if (!md) {
1758 DMWARN("unable to allocate device, out of memory.");
1759 return NULL;
1762 if (!try_module_get(THIS_MODULE))
1763 goto bad_module_get;
1765 /* get a minor number for the dev */
1766 if (minor == DM_ANY_MINOR)
1767 r = next_free_minor(&minor);
1768 else
1769 r = specific_minor(minor);
1770 if (r < 0)
1771 goto bad_minor;
1773 init_rwsem(&md->io_lock);
1774 mutex_init(&md->suspend_lock);
1775 spin_lock_init(&md->deferred_lock);
1776 rwlock_init(&md->map_lock);
1777 atomic_set(&md->holders, 1);
1778 atomic_set(&md->open_count, 0);
1779 atomic_set(&md->event_nr, 0);
1780 atomic_set(&md->uevent_seq, 0);
1781 INIT_LIST_HEAD(&md->uevent_list);
1782 spin_lock_init(&md->uevent_lock);
1784 md->queue = blk_init_queue(dm_request_fn, NULL);
1785 if (!md->queue)
1786 goto bad_queue;
1789 * Request-based dm devices cannot be stacked on top of bio-based dm
1790 * devices. The type of this dm device has not been decided yet,
1791 * although we initialized the queue using blk_init_queue().
1792 * The type is decided at the first table loading time.
1793 * To prevent problematic device stacking, clear the queue flag
1794 * for request stacking support until then.
1796 * This queue is new, so no concurrency on the queue_flags.
1798 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
1799 md->saved_make_request_fn = md->queue->make_request_fn;
1800 md->queue->queuedata = md;
1801 md->queue->backing_dev_info.congested_fn = dm_any_congested;
1802 md->queue->backing_dev_info.congested_data = md;
1803 blk_queue_make_request(md->queue, dm_request);
1804 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
1805 md->queue->unplug_fn = dm_unplug_all;
1806 blk_queue_merge_bvec(md->queue, dm_merge_bvec);
1807 blk_queue_softirq_done(md->queue, dm_softirq_done);
1808 blk_queue_prep_rq(md->queue, dm_prep_fn);
1809 blk_queue_lld_busy(md->queue, dm_lld_busy);
1811 md->disk = alloc_disk(1);
1812 if (!md->disk)
1813 goto bad_disk;
1815 atomic_set(&md->pending[0], 0);
1816 atomic_set(&md->pending[1], 0);
1817 init_waitqueue_head(&md->wait);
1818 INIT_WORK(&md->work, dm_wq_work);
1819 init_waitqueue_head(&md->eventq);
1821 md->disk->major = _major;
1822 md->disk->first_minor = minor;
1823 md->disk->fops = &dm_blk_dops;
1824 md->disk->queue = md->queue;
1825 md->disk->private_data = md;
1826 sprintf(md->disk->disk_name, "dm-%d", minor);
1827 add_disk(md->disk);
1828 format_dev_t(md->name, MKDEV(_major, minor));
1830 md->wq = create_singlethread_workqueue("kdmflush");
1831 if (!md->wq)
1832 goto bad_thread;
1834 md->bdev = bdget_disk(md->disk, 0);
1835 if (!md->bdev)
1836 goto bad_bdev;
1838 /* Populate the mapping, nobody knows we exist yet */
1839 spin_lock(&_minor_lock);
1840 old_md = idr_replace(&_minor_idr, md, minor);
1841 spin_unlock(&_minor_lock);
1843 BUG_ON(old_md != MINOR_ALLOCED);
1845 return md;
1847 bad_bdev:
1848 destroy_workqueue(md->wq);
1849 bad_thread:
1850 del_gendisk(md->disk);
1851 put_disk(md->disk);
1852 bad_disk:
1853 blk_cleanup_queue(md->queue);
1854 bad_queue:
1855 free_minor(minor);
1856 bad_minor:
1857 module_put(THIS_MODULE);
1858 bad_module_get:
1859 kfree(md);
1860 return NULL;
1863 static void unlock_fs(struct mapped_device *md);
1865 static void free_dev(struct mapped_device *md)
1867 int minor = MINOR(disk_devt(md->disk));
1869 unlock_fs(md);
1870 bdput(md->bdev);
1871 destroy_workqueue(md->wq);
1872 if (md->tio_pool)
1873 mempool_destroy(md->tio_pool);
1874 if (md->io_pool)
1875 mempool_destroy(md->io_pool);
1876 if (md->bs)
1877 bioset_free(md->bs);
1878 blk_integrity_unregister(md->disk);
1879 del_gendisk(md->disk);
1880 free_minor(minor);
1882 spin_lock(&_minor_lock);
1883 md->disk->private_data = NULL;
1884 spin_unlock(&_minor_lock);
1886 put_disk(md->disk);
1887 blk_cleanup_queue(md->queue);
1888 module_put(THIS_MODULE);
1889 kfree(md);
1892 static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
1894 struct dm_md_mempools *p;
1896 if (md->io_pool && md->tio_pool && md->bs)
1897 /* the md already has necessary mempools */
1898 goto out;
1900 p = dm_table_get_md_mempools(t);
1901 BUG_ON(!p || md->io_pool || md->tio_pool || md->bs);
1903 md->io_pool = p->io_pool;
1904 p->io_pool = NULL;
1905 md->tio_pool = p->tio_pool;
1906 p->tio_pool = NULL;
1907 md->bs = p->bs;
1908 p->bs = NULL;
1910 out:
1911 /* mempool bind completed, now no need any mempools in the table */
1912 dm_table_free_md_mempools(t);
1916 * Bind a table to the device.
1918 static void event_callback(void *context)
1920 unsigned long flags;
1921 LIST_HEAD(uevents);
1922 struct mapped_device *md = (struct mapped_device *) context;
1924 spin_lock_irqsave(&md->uevent_lock, flags);
1925 list_splice_init(&md->uevent_list, &uevents);
1926 spin_unlock_irqrestore(&md->uevent_lock, flags);
1928 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
1930 atomic_inc(&md->event_nr);
1931 wake_up(&md->eventq);
1935 * Protected by md->suspend_lock obtained by dm_swap_table().
1937 static void __set_size(struct mapped_device *md, sector_t size)
1939 set_capacity(md->disk, size);
1941 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
1944 static int __bind(struct mapped_device *md, struct dm_table *t,
1945 struct queue_limits *limits)
1947 struct request_queue *q = md->queue;
1948 sector_t size;
1949 unsigned long flags;
1951 size = dm_table_get_size(t);
1954 * Wipe any geometry if the size of the table changed.
1956 if (size != get_capacity(md->disk))
1957 memset(&md->geometry, 0, sizeof(md->geometry));
1959 __set_size(md, size);
1961 if (!size) {
1962 dm_table_destroy(t);
1963 return 0;
1966 dm_table_event_callback(t, event_callback, md);
1969 * The queue hasn't been stopped yet, if the old table type wasn't
1970 * for request-based during suspension. So stop it to prevent
1971 * I/O mapping before resume.
1972 * This must be done before setting the queue restrictions,
1973 * because request-based dm may be run just after the setting.
1975 if (dm_table_request_based(t) && !blk_queue_stopped(q))
1976 stop_queue(q);
1978 __bind_mempools(md, t);
1980 write_lock_irqsave(&md->map_lock, flags);
1981 md->map = t;
1982 dm_table_set_restrictions(t, q, limits);
1983 write_unlock_irqrestore(&md->map_lock, flags);
1985 return 0;
1988 static void __unbind(struct mapped_device *md)
1990 struct dm_table *map = md->map;
1991 unsigned long flags;
1993 if (!map)
1994 return;
1996 dm_table_event_callback(map, NULL, NULL);
1997 write_lock_irqsave(&md->map_lock, flags);
1998 md->map = NULL;
1999 write_unlock_irqrestore(&md->map_lock, flags);
2000 dm_table_destroy(map);
2004 * Constructor for a new device.
2006 int dm_create(int minor, struct mapped_device **result)
2008 struct mapped_device *md;
2010 md = alloc_dev(minor);
2011 if (!md)
2012 return -ENXIO;
2014 dm_sysfs_init(md);
2016 *result = md;
2017 return 0;
2020 static struct mapped_device *dm_find_md(dev_t dev)
2022 struct mapped_device *md;
2023 unsigned minor = MINOR(dev);
2025 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
2026 return NULL;
2028 spin_lock(&_minor_lock);
2030 md = idr_find(&_minor_idr, minor);
2031 if (md && (md == MINOR_ALLOCED ||
2032 (MINOR(disk_devt(dm_disk(md))) != minor) ||
2033 test_bit(DMF_FREEING, &md->flags))) {
2034 md = NULL;
2035 goto out;
2038 out:
2039 spin_unlock(&_minor_lock);
2041 return md;
2044 struct mapped_device *dm_get_md(dev_t dev)
2046 struct mapped_device *md = dm_find_md(dev);
2048 if (md)
2049 dm_get(md);
2051 return md;
2054 void *dm_get_mdptr(struct mapped_device *md)
2056 return md->interface_ptr;
2059 void dm_set_mdptr(struct mapped_device *md, void *ptr)
2061 md->interface_ptr = ptr;
2064 void dm_get(struct mapped_device *md)
2066 atomic_inc(&md->holders);
2069 const char *dm_device_name(struct mapped_device *md)
2071 return md->name;
2073 EXPORT_SYMBOL_GPL(dm_device_name);
2075 void dm_put(struct mapped_device *md)
2077 struct dm_table *map;
2079 BUG_ON(test_bit(DMF_FREEING, &md->flags));
2081 if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
2082 map = dm_get_table(md);
2083 idr_replace(&_minor_idr, MINOR_ALLOCED,
2084 MINOR(disk_devt(dm_disk(md))));
2085 set_bit(DMF_FREEING, &md->flags);
2086 spin_unlock(&_minor_lock);
2087 if (!dm_suspended(md)) {
2088 dm_table_presuspend_targets(map);
2089 dm_table_postsuspend_targets(map);
2091 dm_sysfs_exit(md);
2092 dm_table_put(map);
2093 __unbind(md);
2094 free_dev(md);
2097 EXPORT_SYMBOL_GPL(dm_put);
2099 static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
2101 int r = 0;
2102 DECLARE_WAITQUEUE(wait, current);
2103 struct request_queue *q = md->queue;
2104 unsigned long flags;
2106 dm_unplug_all(md->queue);
2108 add_wait_queue(&md->wait, &wait);
2110 while (1) {
2111 set_current_state(interruptible);
2113 smp_mb();
2114 if (dm_request_based(md)) {
2115 spin_lock_irqsave(q->queue_lock, flags);
2116 if (!queue_in_flight(q) && blk_queue_stopped(q)) {
2117 spin_unlock_irqrestore(q->queue_lock, flags);
2118 break;
2120 spin_unlock_irqrestore(q->queue_lock, flags);
2121 } else if (!atomic_read(&md->pending[0]) &&
2122 !atomic_read(&md->pending[1]))
2123 break;
2125 if (interruptible == TASK_INTERRUPTIBLE &&
2126 signal_pending(current)) {
2127 r = -EINTR;
2128 break;
2131 io_schedule();
2133 set_current_state(TASK_RUNNING);
2135 remove_wait_queue(&md->wait, &wait);
2137 return r;
2140 static void dm_flush(struct mapped_device *md)
2142 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2144 bio_init(&md->barrier_bio);
2145 md->barrier_bio.bi_bdev = md->bdev;
2146 md->barrier_bio.bi_rw = WRITE_BARRIER;
2147 __split_and_process_bio(md, &md->barrier_bio);
2149 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2152 static void process_barrier(struct mapped_device *md, struct bio *bio)
2154 md->barrier_error = 0;
2156 dm_flush(md);
2158 if (!bio_empty_barrier(bio)) {
2159 __split_and_process_bio(md, bio);
2160 dm_flush(md);
2163 if (md->barrier_error != DM_ENDIO_REQUEUE)
2164 bio_endio(bio, md->barrier_error);
2165 else {
2166 spin_lock_irq(&md->deferred_lock);
2167 bio_list_add_head(&md->deferred, bio);
2168 spin_unlock_irq(&md->deferred_lock);
2173 * Process the deferred bios
2175 static void dm_wq_work(struct work_struct *work)
2177 struct mapped_device *md = container_of(work, struct mapped_device,
2178 work);
2179 struct bio *c;
2181 down_write(&md->io_lock);
2183 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2184 spin_lock_irq(&md->deferred_lock);
2185 c = bio_list_pop(&md->deferred);
2186 spin_unlock_irq(&md->deferred_lock);
2188 if (!c) {
2189 clear_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
2190 break;
2193 up_write(&md->io_lock);
2195 if (dm_request_based(md))
2196 generic_make_request(c);
2197 else {
2198 if (bio_rw_flagged(c, BIO_RW_BARRIER))
2199 process_barrier(md, c);
2200 else
2201 __split_and_process_bio(md, c);
2204 down_write(&md->io_lock);
2207 up_write(&md->io_lock);
2210 static void dm_queue_flush(struct mapped_device *md)
2212 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2213 smp_mb__after_clear_bit();
2214 queue_work(md->wq, &md->work);
2218 * Swap in a new table (destroying old one).
2220 int dm_swap_table(struct mapped_device *md, struct dm_table *table)
2222 struct queue_limits limits;
2223 int r = -EINVAL;
2225 mutex_lock(&md->suspend_lock);
2227 /* device must be suspended */
2228 if (!dm_suspended(md))
2229 goto out;
2231 r = dm_calculate_queue_limits(table, &limits);
2232 if (r)
2233 goto out;
2235 /* cannot change the device type, once a table is bound */
2236 if (md->map &&
2237 (dm_table_get_type(md->map) != dm_table_get_type(table))) {
2238 DMWARN("can't change the device type after a table is bound");
2239 goto out;
2242 __unbind(md);
2243 r = __bind(md, table, &limits);
2245 out:
2246 mutex_unlock(&md->suspend_lock);
2247 return r;
2250 static void dm_rq_invalidate_suspend_marker(struct mapped_device *md)
2252 md->suspend_rq.special = (void *)0x1;
2255 static void dm_rq_abort_suspend(struct mapped_device *md, int noflush)
2257 struct request_queue *q = md->queue;
2258 unsigned long flags;
2260 spin_lock_irqsave(q->queue_lock, flags);
2261 if (!noflush)
2262 dm_rq_invalidate_suspend_marker(md);
2263 __start_queue(q);
2264 spin_unlock_irqrestore(q->queue_lock, flags);
2267 static void dm_rq_start_suspend(struct mapped_device *md, int noflush)
2269 struct request *rq = &md->suspend_rq;
2270 struct request_queue *q = md->queue;
2272 if (noflush)
2273 stop_queue(q);
2274 else {
2275 blk_rq_init(q, rq);
2276 blk_insert_request(q, rq, 0, NULL);
2280 static int dm_rq_suspend_available(struct mapped_device *md, int noflush)
2282 int r = 1;
2283 struct request *rq = &md->suspend_rq;
2284 struct request_queue *q = md->queue;
2285 unsigned long flags;
2287 if (noflush)
2288 return r;
2290 /* The marker must be protected by queue lock if it is in use */
2291 spin_lock_irqsave(q->queue_lock, flags);
2292 if (unlikely(rq->ref_count)) {
2294 * This can happen, when the previous flush suspend was
2295 * interrupted, the marker is still in the queue and
2296 * this flush suspend has been invoked, because we don't
2297 * remove the marker at the time of suspend interruption.
2298 * We have only one marker per mapped_device, so we can't
2299 * start another flush suspend while it is in use.
2301 BUG_ON(!rq->special); /* The marker should be invalidated */
2302 DMWARN("Invalidating the previous flush suspend is still in"
2303 " progress. Please retry later.");
2304 r = 0;
2306 spin_unlock_irqrestore(q->queue_lock, flags);
2308 return r;
2312 * Functions to lock and unlock any filesystem running on the
2313 * device.
2315 static int lock_fs(struct mapped_device *md)
2317 int r;
2319 WARN_ON(md->frozen_sb);
2321 md->frozen_sb = freeze_bdev(md->bdev);
2322 if (IS_ERR(md->frozen_sb)) {
2323 r = PTR_ERR(md->frozen_sb);
2324 md->frozen_sb = NULL;
2325 return r;
2328 set_bit(DMF_FROZEN, &md->flags);
2330 return 0;
2333 static void unlock_fs(struct mapped_device *md)
2335 if (!test_bit(DMF_FROZEN, &md->flags))
2336 return;
2338 thaw_bdev(md->bdev, md->frozen_sb);
2339 md->frozen_sb = NULL;
2340 clear_bit(DMF_FROZEN, &md->flags);
2344 * We need to be able to change a mapping table under a mounted
2345 * filesystem. For example we might want to move some data in
2346 * the background. Before the table can be swapped with
2347 * dm_bind_table, dm_suspend must be called to flush any in
2348 * flight bios and ensure that any further io gets deferred.
2351 * Suspend mechanism in request-based dm.
2353 * After the suspend starts, further incoming requests are kept in
2354 * the request_queue and deferred.
2355 * Remaining requests in the request_queue at the start of suspend are flushed
2356 * if it is flush suspend.
2357 * The suspend completes when the following conditions have been satisfied,
2358 * so wait for it:
2359 * 1. q->in_flight is 0 (which means no in_flight request)
2360 * 2. queue has been stopped (which means no request dispatching)
2363 * Noflush suspend
2364 * ---------------
2365 * Noflush suspend doesn't need to dispatch remaining requests.
2366 * So stop the queue immediately. Then, wait for all in_flight requests
2367 * to be completed or requeued.
2369 * To abort noflush suspend, start the queue.
2372 * Flush suspend
2373 * -------------
2374 * Flush suspend needs to dispatch remaining requests. So stop the queue
2375 * after the remaining requests are completed. (Requeued request must be also
2376 * re-dispatched and completed. Until then, we can't stop the queue.)
2378 * During flushing the remaining requests, further incoming requests are also
2379 * inserted to the same queue. To distinguish which requests are to be
2380 * flushed, we insert a marker request to the queue at the time of starting
2381 * flush suspend, like a barrier.
2382 * The dispatching is blocked when the marker is found on the top of the queue.
2383 * And the queue is stopped when all in_flight requests are completed, since
2384 * that means the remaining requests are completely flushed.
2385 * Then, the marker is removed from the queue.
2387 * To abort flush suspend, we also need to take care of the marker, not only
2388 * starting the queue.
2389 * We don't remove the marker forcibly from the queue since it's against
2390 * the block-layer manner. Instead, we put a invalidated mark on the marker.
2391 * When the invalidated marker is found on the top of the queue, it is
2392 * immediately removed from the queue, so it doesn't block dispatching.
2393 * Because we have only one marker per mapped_device, we can't start another
2394 * flush suspend until the invalidated marker is removed from the queue.
2395 * So fail and return with -EBUSY in such a case.
2397 int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
2399 struct dm_table *map = NULL;
2400 int r = 0;
2401 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
2402 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
2404 mutex_lock(&md->suspend_lock);
2406 if (dm_suspended(md)) {
2407 r = -EINVAL;
2408 goto out_unlock;
2411 if (dm_request_based(md) && !dm_rq_suspend_available(md, noflush)) {
2412 r = -EBUSY;
2413 goto out_unlock;
2416 map = dm_get_table(md);
2419 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2420 * This flag is cleared before dm_suspend returns.
2422 if (noflush)
2423 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2425 /* This does not get reverted if there's an error later. */
2426 dm_table_presuspend_targets(map);
2429 * Flush I/O to the device. noflush supersedes do_lockfs,
2430 * because lock_fs() needs to flush I/Os.
2432 if (!noflush && do_lockfs) {
2433 r = lock_fs(md);
2434 if (r)
2435 goto out;
2439 * Here we must make sure that no processes are submitting requests
2440 * to target drivers i.e. no one may be executing
2441 * __split_and_process_bio. This is called from dm_request and
2442 * dm_wq_work.
2444 * To get all processes out of __split_and_process_bio in dm_request,
2445 * we take the write lock. To prevent any process from reentering
2446 * __split_and_process_bio from dm_request, we set
2447 * DMF_QUEUE_IO_TO_THREAD.
2449 * To quiesce the thread (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND
2450 * and call flush_workqueue(md->wq). flush_workqueue will wait until
2451 * dm_wq_work exits and DMF_BLOCK_IO_FOR_SUSPEND will prevent any
2452 * further calls to __split_and_process_bio from dm_wq_work.
2454 down_write(&md->io_lock);
2455 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2456 set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
2457 up_write(&md->io_lock);
2459 flush_workqueue(md->wq);
2461 if (dm_request_based(md))
2462 dm_rq_start_suspend(md, noflush);
2465 * At this point no more requests are entering target request routines.
2466 * We call dm_wait_for_completion to wait for all existing requests
2467 * to finish.
2469 r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
2471 down_write(&md->io_lock);
2472 if (noflush)
2473 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2474 up_write(&md->io_lock);
2476 /* were we interrupted ? */
2477 if (r < 0) {
2478 dm_queue_flush(md);
2480 if (dm_request_based(md))
2481 dm_rq_abort_suspend(md, noflush);
2483 unlock_fs(md);
2484 goto out; /* pushback list is already flushed, so skip flush */
2488 * If dm_wait_for_completion returned 0, the device is completely
2489 * quiescent now. There is no request-processing activity. All new
2490 * requests are being added to md->deferred list.
2493 dm_table_postsuspend_targets(map);
2495 set_bit(DMF_SUSPENDED, &md->flags);
2497 out:
2498 dm_table_put(map);
2500 out_unlock:
2501 mutex_unlock(&md->suspend_lock);
2502 return r;
2505 int dm_resume(struct mapped_device *md)
2507 int r = -EINVAL;
2508 struct dm_table *map = NULL;
2510 mutex_lock(&md->suspend_lock);
2511 if (!dm_suspended(md))
2512 goto out;
2514 map = dm_get_table(md);
2515 if (!map || !dm_table_get_size(map))
2516 goto out;
2518 r = dm_table_resume_targets(map);
2519 if (r)
2520 goto out;
2522 dm_queue_flush(md);
2525 * Flushing deferred I/Os must be done after targets are resumed
2526 * so that mapping of targets can work correctly.
2527 * Request-based dm is queueing the deferred I/Os in its request_queue.
2529 if (dm_request_based(md))
2530 start_queue(md->queue);
2532 unlock_fs(md);
2534 clear_bit(DMF_SUSPENDED, &md->flags);
2536 dm_table_unplug_all(map);
2537 r = 0;
2538 out:
2539 dm_table_put(map);
2540 mutex_unlock(&md->suspend_lock);
2542 return r;
2545 /*-----------------------------------------------------------------
2546 * Event notification.
2547 *---------------------------------------------------------------*/
2548 void dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
2549 unsigned cookie)
2551 char udev_cookie[DM_COOKIE_LENGTH];
2552 char *envp[] = { udev_cookie, NULL };
2554 if (!cookie)
2555 kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
2556 else {
2557 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
2558 DM_COOKIE_ENV_VAR_NAME, cookie);
2559 kobject_uevent_env(&disk_to_dev(md->disk)->kobj, action, envp);
2563 uint32_t dm_next_uevent_seq(struct mapped_device *md)
2565 return atomic_add_return(1, &md->uevent_seq);
2568 uint32_t dm_get_event_nr(struct mapped_device *md)
2570 return atomic_read(&md->event_nr);
2573 int dm_wait_event(struct mapped_device *md, int event_nr)
2575 return wait_event_interruptible(md->eventq,
2576 (event_nr != atomic_read(&md->event_nr)));
2579 void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
2581 unsigned long flags;
2583 spin_lock_irqsave(&md->uevent_lock, flags);
2584 list_add(elist, &md->uevent_list);
2585 spin_unlock_irqrestore(&md->uevent_lock, flags);
2589 * The gendisk is only valid as long as you have a reference
2590 * count on 'md'.
2592 struct gendisk *dm_disk(struct mapped_device *md)
2594 return md->disk;
2597 struct kobject *dm_kobject(struct mapped_device *md)
2599 return &md->kobj;
2603 * struct mapped_device should not be exported outside of dm.c
2604 * so use this check to verify that kobj is part of md structure
2606 struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2608 struct mapped_device *md;
2610 md = container_of(kobj, struct mapped_device, kobj);
2611 if (&md->kobj != kobj)
2612 return NULL;
2614 if (test_bit(DMF_FREEING, &md->flags) ||
2615 test_bit(DMF_DELETING, &md->flags))
2616 return NULL;
2618 dm_get(md);
2619 return md;
2622 int dm_suspended(struct mapped_device *md)
2624 return test_bit(DMF_SUSPENDED, &md->flags);
2627 int dm_noflush_suspending(struct dm_target *ti)
2629 struct mapped_device *md = dm_table_get_md(ti->table);
2630 int r = __noflush_suspending(md);
2632 dm_put(md);
2634 return r;
2636 EXPORT_SYMBOL_GPL(dm_noflush_suspending);
2638 struct dm_md_mempools *dm_alloc_md_mempools(unsigned type)
2640 struct dm_md_mempools *pools = kmalloc(sizeof(*pools), GFP_KERNEL);
2642 if (!pools)
2643 return NULL;
2645 pools->io_pool = (type == DM_TYPE_BIO_BASED) ?
2646 mempool_create_slab_pool(MIN_IOS, _io_cache) :
2647 mempool_create_slab_pool(MIN_IOS, _rq_bio_info_cache);
2648 if (!pools->io_pool)
2649 goto free_pools_and_out;
2651 pools->tio_pool = (type == DM_TYPE_BIO_BASED) ?
2652 mempool_create_slab_pool(MIN_IOS, _tio_cache) :
2653 mempool_create_slab_pool(MIN_IOS, _rq_tio_cache);
2654 if (!pools->tio_pool)
2655 goto free_io_pool_and_out;
2657 pools->bs = (type == DM_TYPE_BIO_BASED) ?
2658 bioset_create(16, 0) : bioset_create(MIN_IOS, 0);
2659 if (!pools->bs)
2660 goto free_tio_pool_and_out;
2662 return pools;
2664 free_tio_pool_and_out:
2665 mempool_destroy(pools->tio_pool);
2667 free_io_pool_and_out:
2668 mempool_destroy(pools->io_pool);
2670 free_pools_and_out:
2671 kfree(pools);
2673 return NULL;
2676 void dm_free_md_mempools(struct dm_md_mempools *pools)
2678 if (!pools)
2679 return;
2681 if (pools->io_pool)
2682 mempool_destroy(pools->io_pool);
2684 if (pools->tio_pool)
2685 mempool_destroy(pools->tio_pool);
2687 if (pools->bs)
2688 bioset_free(pools->bs);
2690 kfree(pools);
2693 static const struct block_device_operations dm_blk_dops = {
2694 .open = dm_blk_open,
2695 .release = dm_blk_close,
2696 .ioctl = dm_blk_ioctl,
2697 .getgeo = dm_blk_getgeo,
2698 .owner = THIS_MODULE
2701 EXPORT_SYMBOL(dm_get_mapinfo);
2704 * module hooks
2706 module_init(dm_init);
2707 module_exit(dm_exit);
2709 module_param(major, uint, 0);
2710 MODULE_PARM_DESC(major, "The major number of the device mapper");
2711 MODULE_DESCRIPTION(DM_NAME " driver");
2712 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2713 MODULE_LICENSE("GPL");