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