[ARM] pxa: Gumstix Verdex PCMCIA support
[linux-2.6/verdex.git] / drivers / md / dm.c
blob376f1ab48a245fcdce521bd55a7ceb109f7f8c9e
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_SPINLOCK(_minor_lock);
41 * For bio-based dm.
42 * One of these is allocated per bio.
44 struct dm_io {
45 struct mapped_device *md;
46 int error;
47 atomic_t io_count;
48 struct bio *bio;
49 unsigned long start_time;
53 * For bio-based dm.
54 * One of these is allocated per target within a bio. Hopefully
55 * this will be simplified out one day.
57 struct dm_target_io {
58 struct dm_io *io;
59 struct dm_target *ti;
60 union map_info info;
64 * For request-based dm.
65 * One of these is allocated per request.
67 struct dm_rq_target_io {
68 struct mapped_device *md;
69 struct dm_target *ti;
70 struct request *orig, clone;
71 int error;
72 union map_info info;
76 * For request-based dm.
77 * One of these is allocated per bio.
79 struct dm_rq_clone_bio_info {
80 struct bio *orig;
81 struct dm_rq_target_io *tio;
84 union map_info *dm_get_mapinfo(struct bio *bio)
86 if (bio && bio->bi_private)
87 return &((struct dm_target_io *)bio->bi_private)->info;
88 return NULL;
91 union map_info *dm_get_rq_mapinfo(struct request *rq)
93 if (rq && rq->end_io_data)
94 return &((struct dm_rq_target_io *)rq->end_io_data)->info;
95 return NULL;
97 EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
99 #define MINOR_ALLOCED ((void *)-1)
102 * Bits for the md->flags field.
104 #define DMF_BLOCK_IO_FOR_SUSPEND 0
105 #define DMF_SUSPENDED 1
106 #define DMF_FROZEN 2
107 #define DMF_FREEING 3
108 #define DMF_DELETING 4
109 #define DMF_NOFLUSH_SUSPENDING 5
110 #define DMF_QUEUE_IO_TO_THREAD 6
113 * Work processed by per-device workqueue.
115 struct mapped_device {
116 struct rw_semaphore io_lock;
117 struct mutex suspend_lock;
118 rwlock_t map_lock;
119 atomic_t holders;
120 atomic_t open_count;
122 unsigned long flags;
124 struct request_queue *queue;
125 struct gendisk *disk;
126 char name[16];
128 void *interface_ptr;
131 * A list of ios that arrived while we were suspended.
133 atomic_t pending[2];
134 wait_queue_head_t wait;
135 struct work_struct work;
136 struct bio_list deferred;
137 spinlock_t deferred_lock;
140 * An error from the barrier request currently being processed.
142 int barrier_error;
145 * Processing queue (flush/barriers)
147 struct workqueue_struct *wq;
150 * The current mapping.
152 struct dm_table *map;
155 * io objects are allocated from here.
157 mempool_t *io_pool;
158 mempool_t *tio_pool;
160 struct bio_set *bs;
163 * Event handling.
165 atomic_t event_nr;
166 wait_queue_head_t eventq;
167 atomic_t uevent_seq;
168 struct list_head uevent_list;
169 spinlock_t uevent_lock; /* Protect access to uevent_list */
172 * freeze/thaw support require holding onto a super block
174 struct super_block *frozen_sb;
175 struct block_device *bdev;
177 /* forced geometry settings */
178 struct hd_geometry geometry;
180 /* marker of flush suspend for request-based dm */
181 struct request suspend_rq;
183 /* For saving the address of __make_request for request based dm */
184 make_request_fn *saved_make_request_fn;
186 /* sysfs handle */
187 struct kobject kobj;
189 /* zero-length barrier that will be cloned and submitted to targets */
190 struct bio barrier_bio;
194 * For mempools pre-allocation at the table loading time.
196 struct dm_md_mempools {
197 mempool_t *io_pool;
198 mempool_t *tio_pool;
199 struct bio_set *bs;
202 #define MIN_IOS 256
203 static struct kmem_cache *_io_cache;
204 static struct kmem_cache *_tio_cache;
205 static struct kmem_cache *_rq_tio_cache;
206 static struct kmem_cache *_rq_bio_info_cache;
208 static int __init local_init(void)
210 int r = -ENOMEM;
212 /* allocate a slab for the dm_ios */
213 _io_cache = KMEM_CACHE(dm_io, 0);
214 if (!_io_cache)
215 return r;
217 /* allocate a slab for the target ios */
218 _tio_cache = KMEM_CACHE(dm_target_io, 0);
219 if (!_tio_cache)
220 goto out_free_io_cache;
222 _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
223 if (!_rq_tio_cache)
224 goto out_free_tio_cache;
226 _rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0);
227 if (!_rq_bio_info_cache)
228 goto out_free_rq_tio_cache;
230 r = dm_uevent_init();
231 if (r)
232 goto out_free_rq_bio_info_cache;
234 _major = major;
235 r = register_blkdev(_major, _name);
236 if (r < 0)
237 goto out_uevent_exit;
239 if (!_major)
240 _major = r;
242 return 0;
244 out_uevent_exit:
245 dm_uevent_exit();
246 out_free_rq_bio_info_cache:
247 kmem_cache_destroy(_rq_bio_info_cache);
248 out_free_rq_tio_cache:
249 kmem_cache_destroy(_rq_tio_cache);
250 out_free_tio_cache:
251 kmem_cache_destroy(_tio_cache);
252 out_free_io_cache:
253 kmem_cache_destroy(_io_cache);
255 return r;
258 static void local_exit(void)
260 kmem_cache_destroy(_rq_bio_info_cache);
261 kmem_cache_destroy(_rq_tio_cache);
262 kmem_cache_destroy(_tio_cache);
263 kmem_cache_destroy(_io_cache);
264 unregister_blkdev(_major, _name);
265 dm_uevent_exit();
267 _major = 0;
269 DMINFO("cleaned up");
272 static int (*_inits[])(void) __initdata = {
273 local_init,
274 dm_target_init,
275 dm_linear_init,
276 dm_stripe_init,
277 dm_kcopyd_init,
278 dm_interface_init,
281 static void (*_exits[])(void) = {
282 local_exit,
283 dm_target_exit,
284 dm_linear_exit,
285 dm_stripe_exit,
286 dm_kcopyd_exit,
287 dm_interface_exit,
290 static int __init dm_init(void)
292 const int count = ARRAY_SIZE(_inits);
294 int r, i;
296 for (i = 0; i < count; i++) {
297 r = _inits[i]();
298 if (r)
299 goto bad;
302 return 0;
304 bad:
305 while (i--)
306 _exits[i]();
308 return r;
311 static void __exit dm_exit(void)
313 int i = ARRAY_SIZE(_exits);
315 while (i--)
316 _exits[i]();
320 * Block device functions
322 static int dm_blk_open(struct block_device *bdev, fmode_t mode)
324 struct mapped_device *md;
326 spin_lock(&_minor_lock);
328 md = bdev->bd_disk->private_data;
329 if (!md)
330 goto out;
332 if (test_bit(DMF_FREEING, &md->flags) ||
333 test_bit(DMF_DELETING, &md->flags)) {
334 md = NULL;
335 goto out;
338 dm_get(md);
339 atomic_inc(&md->open_count);
341 out:
342 spin_unlock(&_minor_lock);
344 return md ? 0 : -ENXIO;
347 static int dm_blk_close(struct gendisk *disk, fmode_t mode)
349 struct mapped_device *md = disk->private_data;
350 atomic_dec(&md->open_count);
351 dm_put(md);
352 return 0;
355 int dm_open_count(struct mapped_device *md)
357 return atomic_read(&md->open_count);
361 * Guarantees nothing is using the device before it's deleted.
363 int dm_lock_for_deletion(struct mapped_device *md)
365 int r = 0;
367 spin_lock(&_minor_lock);
369 if (dm_open_count(md))
370 r = -EBUSY;
371 else
372 set_bit(DMF_DELETING, &md->flags);
374 spin_unlock(&_minor_lock);
376 return r;
379 static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
381 struct mapped_device *md = bdev->bd_disk->private_data;
383 return dm_get_geometry(md, geo);
386 static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
387 unsigned int cmd, unsigned long arg)
389 struct mapped_device *md = bdev->bd_disk->private_data;
390 struct dm_table *map = dm_get_table(md);
391 struct dm_target *tgt;
392 int r = -ENOTTY;
394 if (!map || !dm_table_get_size(map))
395 goto out;
397 /* We only support devices that have a single target */
398 if (dm_table_get_num_targets(map) != 1)
399 goto out;
401 tgt = dm_table_get_target(map, 0);
403 if (dm_suspended(md)) {
404 r = -EAGAIN;
405 goto out;
408 if (tgt->type->ioctl)
409 r = tgt->type->ioctl(tgt, cmd, arg);
411 out:
412 dm_table_put(map);
414 return r;
417 static struct dm_io *alloc_io(struct mapped_device *md)
419 return mempool_alloc(md->io_pool, GFP_NOIO);
422 static void free_io(struct mapped_device *md, struct dm_io *io)
424 mempool_free(io, md->io_pool);
427 static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
429 mempool_free(tio, md->tio_pool);
432 static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md)
434 return mempool_alloc(md->tio_pool, GFP_ATOMIC);
437 static void free_rq_tio(struct dm_rq_target_io *tio)
439 mempool_free(tio, tio->md->tio_pool);
442 static struct dm_rq_clone_bio_info *alloc_bio_info(struct mapped_device *md)
444 return mempool_alloc(md->io_pool, GFP_ATOMIC);
447 static void free_bio_info(struct dm_rq_clone_bio_info *info)
449 mempool_free(info, info->tio->md->io_pool);
452 static void start_io_acct(struct dm_io *io)
454 struct mapped_device *md = io->md;
455 int cpu;
456 int rw = bio_data_dir(io->bio);
458 io->start_time = jiffies;
460 cpu = part_stat_lock();
461 part_round_stats(cpu, &dm_disk(md)->part0);
462 part_stat_unlock();
463 dm_disk(md)->part0.in_flight[rw] = atomic_inc_return(&md->pending[rw]);
466 static void end_io_acct(struct dm_io *io)
468 struct mapped_device *md = io->md;
469 struct bio *bio = io->bio;
470 unsigned long duration = jiffies - io->start_time;
471 int pending, cpu;
472 int rw = bio_data_dir(bio);
474 cpu = part_stat_lock();
475 part_round_stats(cpu, &dm_disk(md)->part0);
476 part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
477 part_stat_unlock();
480 * After this is decremented the bio must not be touched if it is
481 * a barrier.
483 dm_disk(md)->part0.in_flight[rw] = pending =
484 atomic_dec_return(&md->pending[rw]);
485 pending += atomic_read(&md->pending[rw^0x1]);
487 /* nudge anyone waiting on suspend queue */
488 if (!pending)
489 wake_up(&md->wait);
493 * Add the bio to the list of deferred io.
495 static void queue_io(struct mapped_device *md, struct bio *bio)
497 down_write(&md->io_lock);
499 spin_lock_irq(&md->deferred_lock);
500 bio_list_add(&md->deferred, bio);
501 spin_unlock_irq(&md->deferred_lock);
503 if (!test_and_set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags))
504 queue_work(md->wq, &md->work);
506 up_write(&md->io_lock);
510 * Everyone (including functions in this file), should use this
511 * function to access the md->map field, and make sure they call
512 * dm_table_put() when finished.
514 struct dm_table *dm_get_table(struct mapped_device *md)
516 struct dm_table *t;
517 unsigned long flags;
519 read_lock_irqsave(&md->map_lock, flags);
520 t = md->map;
521 if (t)
522 dm_table_get(t);
523 read_unlock_irqrestore(&md->map_lock, flags);
525 return t;
529 * Get the geometry associated with a dm device
531 int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
533 *geo = md->geometry;
535 return 0;
539 * Set the geometry of a device.
541 int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
543 sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
545 if (geo->start > sz) {
546 DMWARN("Start sector is beyond the geometry limits.");
547 return -EINVAL;
550 md->geometry = *geo;
552 return 0;
555 /*-----------------------------------------------------------------
556 * CRUD START:
557 * A more elegant soln is in the works that uses the queue
558 * merge fn, unfortunately there are a couple of changes to
559 * the block layer that I want to make for this. So in the
560 * interests of getting something for people to use I give
561 * you this clearly demarcated crap.
562 *---------------------------------------------------------------*/
564 static int __noflush_suspending(struct mapped_device *md)
566 return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
570 * Decrements the number of outstanding ios that a bio has been
571 * cloned into, completing the original io if necc.
573 static void dec_pending(struct dm_io *io, int error)
575 unsigned long flags;
576 int io_error;
577 struct bio *bio;
578 struct mapped_device *md = io->md;
580 /* Push-back supersedes any I/O errors */
581 if (error && !(io->error > 0 && __noflush_suspending(md)))
582 io->error = error;
584 if (atomic_dec_and_test(&io->io_count)) {
585 if (io->error == DM_ENDIO_REQUEUE) {
587 * Target requested pushing back the I/O.
589 spin_lock_irqsave(&md->deferred_lock, flags);
590 if (__noflush_suspending(md)) {
591 if (!bio_rw_flagged(io->bio, BIO_RW_BARRIER))
592 bio_list_add_head(&md->deferred,
593 io->bio);
594 } else
595 /* noflush suspend was interrupted. */
596 io->error = -EIO;
597 spin_unlock_irqrestore(&md->deferred_lock, flags);
600 io_error = io->error;
601 bio = io->bio;
603 if (bio_rw_flagged(bio, BIO_RW_BARRIER)) {
605 * There can be just one barrier request so we use
606 * a per-device variable for error reporting.
607 * Note that you can't touch the bio after end_io_acct
609 if (!md->barrier_error && io_error != -EOPNOTSUPP)
610 md->barrier_error = io_error;
611 end_io_acct(io);
612 } else {
613 end_io_acct(io);
615 if (io_error != DM_ENDIO_REQUEUE) {
616 trace_block_bio_complete(md->queue, bio);
618 bio_endio(bio, io_error);
622 free_io(md, io);
626 static void clone_endio(struct bio *bio, int error)
628 int r = 0;
629 struct dm_target_io *tio = bio->bi_private;
630 struct dm_io *io = tio->io;
631 struct mapped_device *md = tio->io->md;
632 dm_endio_fn endio = tio->ti->type->end_io;
634 if (!bio_flagged(bio, BIO_UPTODATE) && !error)
635 error = -EIO;
637 if (endio) {
638 r = endio(tio->ti, bio, error, &tio->info);
639 if (r < 0 || r == DM_ENDIO_REQUEUE)
641 * error and requeue request are handled
642 * in dec_pending().
644 error = r;
645 else if (r == DM_ENDIO_INCOMPLETE)
646 /* The target will handle the io */
647 return;
648 else if (r) {
649 DMWARN("unimplemented target endio return value: %d", r);
650 BUG();
655 * Store md for cleanup instead of tio which is about to get freed.
657 bio->bi_private = md->bs;
659 free_tio(md, tio);
660 bio_put(bio);
661 dec_pending(io, error);
665 * Partial completion handling for request-based dm
667 static void end_clone_bio(struct bio *clone, int error)
669 struct dm_rq_clone_bio_info *info = clone->bi_private;
670 struct dm_rq_target_io *tio = info->tio;
671 struct bio *bio = info->orig;
672 unsigned int nr_bytes = info->orig->bi_size;
674 bio_put(clone);
676 if (tio->error)
678 * An error has already been detected on the request.
679 * Once error occurred, just let clone->end_io() handle
680 * the remainder.
682 return;
683 else if (error) {
685 * Don't notice the error to the upper layer yet.
686 * The error handling decision is made by the target driver,
687 * when the request is completed.
689 tio->error = error;
690 return;
694 * I/O for the bio successfully completed.
695 * Notice the data completion to the upper layer.
699 * bios are processed from the head of the list.
700 * So the completing bio should always be rq->bio.
701 * If it's not, something wrong is happening.
703 if (tio->orig->bio != bio)
704 DMERR("bio completion is going in the middle of the request");
707 * Update the original request.
708 * Do not use blk_end_request() here, because it may complete
709 * the original request before the clone, and break the ordering.
711 blk_update_request(tio->orig, 0, nr_bytes);
715 * Don't touch any member of the md after calling this function because
716 * the md may be freed in dm_put() at the end of this function.
717 * Or do dm_get() before calling this function and dm_put() later.
719 static void rq_completed(struct mapped_device *md, int run_queue)
721 int wakeup_waiters = 0;
722 struct request_queue *q = md->queue;
723 unsigned long flags;
725 spin_lock_irqsave(q->queue_lock, flags);
726 if (!queue_in_flight(q))
727 wakeup_waiters = 1;
728 spin_unlock_irqrestore(q->queue_lock, flags);
730 /* nudge anyone waiting on suspend queue */
731 if (wakeup_waiters)
732 wake_up(&md->wait);
734 if (run_queue)
735 blk_run_queue(q);
738 * dm_put() must be at the end of this function. See the comment above
740 dm_put(md);
743 static void free_rq_clone(struct request *clone)
745 struct dm_rq_target_io *tio = clone->end_io_data;
747 blk_rq_unprep_clone(clone);
748 free_rq_tio(tio);
751 static void dm_unprep_request(struct request *rq)
753 struct request *clone = rq->special;
755 rq->special = NULL;
756 rq->cmd_flags &= ~REQ_DONTPREP;
758 free_rq_clone(clone);
762 * Requeue the original request of a clone.
764 void dm_requeue_unmapped_request(struct request *clone)
766 struct dm_rq_target_io *tio = clone->end_io_data;
767 struct mapped_device *md = tio->md;
768 struct request *rq = tio->orig;
769 struct request_queue *q = rq->q;
770 unsigned long flags;
772 dm_unprep_request(rq);
774 spin_lock_irqsave(q->queue_lock, flags);
775 if (elv_queue_empty(q))
776 blk_plug_device(q);
777 blk_requeue_request(q, rq);
778 spin_unlock_irqrestore(q->queue_lock, flags);
780 rq_completed(md, 0);
782 EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
784 static void __stop_queue(struct request_queue *q)
786 blk_stop_queue(q);
789 static void stop_queue(struct request_queue *q)
791 unsigned long flags;
793 spin_lock_irqsave(q->queue_lock, flags);
794 __stop_queue(q);
795 spin_unlock_irqrestore(q->queue_lock, flags);
798 static void __start_queue(struct request_queue *q)
800 if (blk_queue_stopped(q))
801 blk_start_queue(q);
804 static void start_queue(struct request_queue *q)
806 unsigned long flags;
808 spin_lock_irqsave(q->queue_lock, flags);
809 __start_queue(q);
810 spin_unlock_irqrestore(q->queue_lock, flags);
814 * Complete the clone and the original request.
815 * Must be called without queue lock.
817 static void dm_end_request(struct request *clone, int error)
819 struct dm_rq_target_io *tio = clone->end_io_data;
820 struct mapped_device *md = tio->md;
821 struct request *rq = tio->orig;
823 if (blk_pc_request(rq)) {
824 rq->errors = clone->errors;
825 rq->resid_len = clone->resid_len;
827 if (rq->sense)
829 * We are using the sense buffer of the original
830 * request.
831 * So setting the length of the sense data is enough.
833 rq->sense_len = clone->sense_len;
836 free_rq_clone(clone);
838 blk_end_request_all(rq, error);
840 rq_completed(md, 1);
844 * Request completion handler for request-based dm
846 static void dm_softirq_done(struct request *rq)
848 struct request *clone = rq->completion_data;
849 struct dm_rq_target_io *tio = clone->end_io_data;
850 dm_request_endio_fn rq_end_io = tio->ti->type->rq_end_io;
851 int error = tio->error;
853 if (!(rq->cmd_flags & REQ_FAILED) && rq_end_io)
854 error = rq_end_io(tio->ti, clone, error, &tio->info);
856 if (error <= 0)
857 /* The target wants to complete the I/O */
858 dm_end_request(clone, error);
859 else if (error == DM_ENDIO_INCOMPLETE)
860 /* The target will handle the I/O */
861 return;
862 else if (error == DM_ENDIO_REQUEUE)
863 /* The target wants to requeue the I/O */
864 dm_requeue_unmapped_request(clone);
865 else {
866 DMWARN("unimplemented target endio return value: %d", error);
867 BUG();
872 * Complete the clone and the original request with the error status
873 * through softirq context.
875 static void dm_complete_request(struct request *clone, int error)
877 struct dm_rq_target_io *tio = clone->end_io_data;
878 struct request *rq = tio->orig;
880 tio->error = error;
881 rq->completion_data = clone;
882 blk_complete_request(rq);
886 * Complete the not-mapped clone and the original request with the error status
887 * through softirq context.
888 * Target's rq_end_io() function isn't called.
889 * This may be used when the target's map_rq() function fails.
891 void dm_kill_unmapped_request(struct request *clone, int error)
893 struct dm_rq_target_io *tio = clone->end_io_data;
894 struct request *rq = tio->orig;
896 rq->cmd_flags |= REQ_FAILED;
897 dm_complete_request(clone, error);
899 EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
902 * Called with the queue lock held
904 static void end_clone_request(struct request *clone, int error)
907 * For just cleaning up the information of the queue in which
908 * the clone was dispatched.
909 * The clone is *NOT* freed actually here because it is alloced from
910 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
912 __blk_put_request(clone->q, clone);
915 * Actual request completion is done in a softirq context which doesn't
916 * hold the queue lock. Otherwise, deadlock could occur because:
917 * - another request may be submitted by the upper level driver
918 * of the stacking during the completion
919 * - the submission which requires queue lock may be done
920 * against this queue
922 dm_complete_request(clone, error);
925 static sector_t max_io_len(struct mapped_device *md,
926 sector_t sector, struct dm_target *ti)
928 sector_t offset = sector - ti->begin;
929 sector_t len = ti->len - offset;
932 * Does the target need to split even further ?
934 if (ti->split_io) {
935 sector_t boundary;
936 boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
937 - offset;
938 if (len > boundary)
939 len = boundary;
942 return len;
945 static void __map_bio(struct dm_target *ti, struct bio *clone,
946 struct dm_target_io *tio)
948 int r;
949 sector_t sector;
950 struct mapped_device *md;
952 clone->bi_end_io = clone_endio;
953 clone->bi_private = tio;
956 * Map the clone. If r == 0 we don't need to do
957 * anything, the target has assumed ownership of
958 * this io.
960 atomic_inc(&tio->io->io_count);
961 sector = clone->bi_sector;
962 r = ti->type->map(ti, clone, &tio->info);
963 if (r == DM_MAPIO_REMAPPED) {
964 /* the bio has been remapped so dispatch it */
966 trace_block_remap(bdev_get_queue(clone->bi_bdev), clone,
967 tio->io->bio->bi_bdev->bd_dev, sector);
969 generic_make_request(clone);
970 } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
971 /* error the io and bail out, or requeue it if needed */
972 md = tio->io->md;
973 dec_pending(tio->io, r);
975 * Store bio_set for cleanup.
977 clone->bi_private = md->bs;
978 bio_put(clone);
979 free_tio(md, tio);
980 } else if (r) {
981 DMWARN("unimplemented target map return value: %d", r);
982 BUG();
986 struct clone_info {
987 struct mapped_device *md;
988 struct dm_table *map;
989 struct bio *bio;
990 struct dm_io *io;
991 sector_t sector;
992 sector_t sector_count;
993 unsigned short idx;
996 static void dm_bio_destructor(struct bio *bio)
998 struct bio_set *bs = bio->bi_private;
1000 bio_free(bio, bs);
1004 * Creates a little bio that is just does part of a bvec.
1006 static struct bio *split_bvec(struct bio *bio, sector_t sector,
1007 unsigned short idx, unsigned int offset,
1008 unsigned int len, struct bio_set *bs)
1010 struct bio *clone;
1011 struct bio_vec *bv = bio->bi_io_vec + idx;
1013 clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
1014 clone->bi_destructor = dm_bio_destructor;
1015 *clone->bi_io_vec = *bv;
1017 clone->bi_sector = sector;
1018 clone->bi_bdev = bio->bi_bdev;
1019 clone->bi_rw = bio->bi_rw & ~(1 << BIO_RW_BARRIER);
1020 clone->bi_vcnt = 1;
1021 clone->bi_size = to_bytes(len);
1022 clone->bi_io_vec->bv_offset = offset;
1023 clone->bi_io_vec->bv_len = clone->bi_size;
1024 clone->bi_flags |= 1 << BIO_CLONED;
1026 if (bio_integrity(bio)) {
1027 bio_integrity_clone(clone, bio, GFP_NOIO, bs);
1028 bio_integrity_trim(clone,
1029 bio_sector_offset(bio, idx, offset), len);
1032 return clone;
1036 * Creates a bio that consists of range of complete bvecs.
1038 static struct bio *clone_bio(struct bio *bio, sector_t sector,
1039 unsigned short idx, unsigned short bv_count,
1040 unsigned int len, struct bio_set *bs)
1042 struct bio *clone;
1044 clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
1045 __bio_clone(clone, bio);
1046 clone->bi_rw &= ~(1 << BIO_RW_BARRIER);
1047 clone->bi_destructor = dm_bio_destructor;
1048 clone->bi_sector = sector;
1049 clone->bi_idx = idx;
1050 clone->bi_vcnt = idx + bv_count;
1051 clone->bi_size = to_bytes(len);
1052 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
1054 if (bio_integrity(bio)) {
1055 bio_integrity_clone(clone, bio, GFP_NOIO, bs);
1057 if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
1058 bio_integrity_trim(clone,
1059 bio_sector_offset(bio, idx, 0), len);
1062 return clone;
1065 static struct dm_target_io *alloc_tio(struct clone_info *ci,
1066 struct dm_target *ti)
1068 struct dm_target_io *tio = mempool_alloc(ci->md->tio_pool, GFP_NOIO);
1070 tio->io = ci->io;
1071 tio->ti = ti;
1072 memset(&tio->info, 0, sizeof(tio->info));
1074 return tio;
1077 static void __flush_target(struct clone_info *ci, struct dm_target *ti,
1078 unsigned flush_nr)
1080 struct dm_target_io *tio = alloc_tio(ci, ti);
1081 struct bio *clone;
1083 tio->info.flush_request = flush_nr;
1085 clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
1086 __bio_clone(clone, ci->bio);
1087 clone->bi_destructor = dm_bio_destructor;
1089 __map_bio(ti, clone, tio);
1092 static int __clone_and_map_empty_barrier(struct clone_info *ci)
1094 unsigned target_nr = 0, flush_nr;
1095 struct dm_target *ti;
1097 while ((ti = dm_table_get_target(ci->map, target_nr++)))
1098 for (flush_nr = 0; flush_nr < ti->num_flush_requests;
1099 flush_nr++)
1100 __flush_target(ci, ti, flush_nr);
1102 ci->sector_count = 0;
1104 return 0;
1107 static int __clone_and_map(struct clone_info *ci)
1109 struct bio *clone, *bio = ci->bio;
1110 struct dm_target *ti;
1111 sector_t len = 0, max;
1112 struct dm_target_io *tio;
1114 if (unlikely(bio_empty_barrier(bio)))
1115 return __clone_and_map_empty_barrier(ci);
1117 ti = dm_table_find_target(ci->map, ci->sector);
1118 if (!dm_target_is_valid(ti))
1119 return -EIO;
1121 max = max_io_len(ci->md, ci->sector, ti);
1124 * Allocate a target io object.
1126 tio = alloc_tio(ci, ti);
1128 if (ci->sector_count <= max) {
1130 * Optimise for the simple case where we can do all of
1131 * the remaining io with a single clone.
1133 clone = clone_bio(bio, ci->sector, ci->idx,
1134 bio->bi_vcnt - ci->idx, ci->sector_count,
1135 ci->md->bs);
1136 __map_bio(ti, clone, tio);
1137 ci->sector_count = 0;
1139 } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
1141 * There are some bvecs that don't span targets.
1142 * Do as many of these as possible.
1144 int i;
1145 sector_t remaining = max;
1146 sector_t bv_len;
1148 for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
1149 bv_len = to_sector(bio->bi_io_vec[i].bv_len);
1151 if (bv_len > remaining)
1152 break;
1154 remaining -= bv_len;
1155 len += bv_len;
1158 clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
1159 ci->md->bs);
1160 __map_bio(ti, clone, tio);
1162 ci->sector += len;
1163 ci->sector_count -= len;
1164 ci->idx = i;
1166 } else {
1168 * Handle a bvec that must be split between two or more targets.
1170 struct bio_vec *bv = bio->bi_io_vec + ci->idx;
1171 sector_t remaining = to_sector(bv->bv_len);
1172 unsigned int offset = 0;
1174 do {
1175 if (offset) {
1176 ti = dm_table_find_target(ci->map, ci->sector);
1177 if (!dm_target_is_valid(ti))
1178 return -EIO;
1180 max = max_io_len(ci->md, ci->sector, ti);
1182 tio = alloc_tio(ci, ti);
1185 len = min(remaining, max);
1187 clone = split_bvec(bio, ci->sector, ci->idx,
1188 bv->bv_offset + offset, len,
1189 ci->md->bs);
1191 __map_bio(ti, clone, tio);
1193 ci->sector += len;
1194 ci->sector_count -= len;
1195 offset += to_bytes(len);
1196 } while (remaining -= len);
1198 ci->idx++;
1201 return 0;
1205 * Split the bio into several clones and submit it to targets.
1207 static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
1209 struct clone_info ci;
1210 int error = 0;
1212 ci.map = dm_get_table(md);
1213 if (unlikely(!ci.map)) {
1214 if (!bio_rw_flagged(bio, BIO_RW_BARRIER))
1215 bio_io_error(bio);
1216 else
1217 if (!md->barrier_error)
1218 md->barrier_error = -EIO;
1219 return;
1222 ci.md = md;
1223 ci.bio = bio;
1224 ci.io = alloc_io(md);
1225 ci.io->error = 0;
1226 atomic_set(&ci.io->io_count, 1);
1227 ci.io->bio = bio;
1228 ci.io->md = md;
1229 ci.sector = bio->bi_sector;
1230 ci.sector_count = bio_sectors(bio);
1231 if (unlikely(bio_empty_barrier(bio)))
1232 ci.sector_count = 1;
1233 ci.idx = bio->bi_idx;
1235 start_io_acct(ci.io);
1236 while (ci.sector_count && !error)
1237 error = __clone_and_map(&ci);
1239 /* drop the extra reference count */
1240 dec_pending(ci.io, error);
1241 dm_table_put(ci.map);
1243 /*-----------------------------------------------------------------
1244 * CRUD END
1245 *---------------------------------------------------------------*/
1247 static int dm_merge_bvec(struct request_queue *q,
1248 struct bvec_merge_data *bvm,
1249 struct bio_vec *biovec)
1251 struct mapped_device *md = q->queuedata;
1252 struct dm_table *map = dm_get_table(md);
1253 struct dm_target *ti;
1254 sector_t max_sectors;
1255 int max_size = 0;
1257 if (unlikely(!map))
1258 goto out;
1260 ti = dm_table_find_target(map, bvm->bi_sector);
1261 if (!dm_target_is_valid(ti))
1262 goto out_table;
1265 * Find maximum amount of I/O that won't need splitting
1267 max_sectors = min(max_io_len(md, bvm->bi_sector, ti),
1268 (sector_t) BIO_MAX_SECTORS);
1269 max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1270 if (max_size < 0)
1271 max_size = 0;
1274 * merge_bvec_fn() returns number of bytes
1275 * it can accept at this offset
1276 * max is precomputed maximal io size
1278 if (max_size && ti->type->merge)
1279 max_size = ti->type->merge(ti, bvm, biovec, max_size);
1281 * If the target doesn't support merge method and some of the devices
1282 * provided their merge_bvec method (we know this by looking at
1283 * queue_max_hw_sectors), then we can't allow bios with multiple vector
1284 * entries. So always set max_size to 0, and the code below allows
1285 * just one page.
1287 else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
1289 max_size = 0;
1291 out_table:
1292 dm_table_put(map);
1294 out:
1296 * Always allow an entire first page
1298 if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1299 max_size = biovec->bv_len;
1301 return max_size;
1305 * The request function that just remaps the bio built up by
1306 * dm_merge_bvec.
1308 static int _dm_request(struct request_queue *q, struct bio *bio)
1310 int rw = bio_data_dir(bio);
1311 struct mapped_device *md = q->queuedata;
1312 int cpu;
1314 down_read(&md->io_lock);
1316 cpu = part_stat_lock();
1317 part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1318 part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1319 part_stat_unlock();
1322 * If we're suspended or the thread is processing barriers
1323 * we have to queue this io for later.
1325 if (unlikely(test_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags)) ||
1326 unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) {
1327 up_read(&md->io_lock);
1329 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) &&
1330 bio_rw(bio) == READA) {
1331 bio_io_error(bio);
1332 return 0;
1335 queue_io(md, bio);
1337 return 0;
1340 __split_and_process_bio(md, bio);
1341 up_read(&md->io_lock);
1342 return 0;
1345 static int dm_make_request(struct request_queue *q, struct bio *bio)
1347 struct mapped_device *md = q->queuedata;
1349 if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) {
1350 bio_endio(bio, -EOPNOTSUPP);
1351 return 0;
1354 return md->saved_make_request_fn(q, bio); /* call __make_request() */
1357 static int dm_request_based(struct mapped_device *md)
1359 return blk_queue_stackable(md->queue);
1362 static int dm_request(struct request_queue *q, struct bio *bio)
1364 struct mapped_device *md = q->queuedata;
1366 if (dm_request_based(md))
1367 return dm_make_request(q, bio);
1369 return _dm_request(q, bio);
1372 void dm_dispatch_request(struct request *rq)
1374 int r;
1376 if (blk_queue_io_stat(rq->q))
1377 rq->cmd_flags |= REQ_IO_STAT;
1379 rq->start_time = jiffies;
1380 r = blk_insert_cloned_request(rq->q, rq);
1381 if (r)
1382 dm_complete_request(rq, r);
1384 EXPORT_SYMBOL_GPL(dm_dispatch_request);
1386 static void dm_rq_bio_destructor(struct bio *bio)
1388 struct dm_rq_clone_bio_info *info = bio->bi_private;
1389 struct mapped_device *md = info->tio->md;
1391 free_bio_info(info);
1392 bio_free(bio, md->bs);
1395 static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1396 void *data)
1398 struct dm_rq_target_io *tio = data;
1399 struct mapped_device *md = tio->md;
1400 struct dm_rq_clone_bio_info *info = alloc_bio_info(md);
1402 if (!info)
1403 return -ENOMEM;
1405 info->orig = bio_orig;
1406 info->tio = tio;
1407 bio->bi_end_io = end_clone_bio;
1408 bio->bi_private = info;
1409 bio->bi_destructor = dm_rq_bio_destructor;
1411 return 0;
1414 static int setup_clone(struct request *clone, struct request *rq,
1415 struct dm_rq_target_io *tio)
1417 int r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1418 dm_rq_bio_constructor, tio);
1420 if (r)
1421 return r;
1423 clone->cmd = rq->cmd;
1424 clone->cmd_len = rq->cmd_len;
1425 clone->sense = rq->sense;
1426 clone->buffer = rq->buffer;
1427 clone->end_io = end_clone_request;
1428 clone->end_io_data = tio;
1430 return 0;
1433 static int dm_rq_flush_suspending(struct mapped_device *md)
1435 return !md->suspend_rq.special;
1439 * Called with the queue lock held.
1441 static int dm_prep_fn(struct request_queue *q, struct request *rq)
1443 struct mapped_device *md = q->queuedata;
1444 struct dm_rq_target_io *tio;
1445 struct request *clone;
1447 if (unlikely(rq == &md->suspend_rq)) {
1448 if (dm_rq_flush_suspending(md))
1449 return BLKPREP_OK;
1450 else
1451 /* The flush suspend was interrupted */
1452 return BLKPREP_KILL;
1455 if (unlikely(rq->special)) {
1456 DMWARN("Already has something in rq->special.");
1457 return BLKPREP_KILL;
1460 tio = alloc_rq_tio(md); /* Only one for each original request */
1461 if (!tio)
1462 /* -ENOMEM */
1463 return BLKPREP_DEFER;
1465 tio->md = md;
1466 tio->ti = NULL;
1467 tio->orig = rq;
1468 tio->error = 0;
1469 memset(&tio->info, 0, sizeof(tio->info));
1471 clone = &tio->clone;
1472 if (setup_clone(clone, rq, tio)) {
1473 /* -ENOMEM */
1474 free_rq_tio(tio);
1475 return BLKPREP_DEFER;
1478 rq->special = clone;
1479 rq->cmd_flags |= REQ_DONTPREP;
1481 return BLKPREP_OK;
1484 static void map_request(struct dm_target *ti, struct request *rq,
1485 struct mapped_device *md)
1487 int r;
1488 struct request *clone = rq->special;
1489 struct dm_rq_target_io *tio = clone->end_io_data;
1492 * Hold the md reference here for the in-flight I/O.
1493 * We can't rely on the reference count by device opener,
1494 * because the device may be closed during the request completion
1495 * when all bios are completed.
1496 * See the comment in rq_completed() too.
1498 dm_get(md);
1500 tio->ti = ti;
1501 r = ti->type->map_rq(ti, clone, &tio->info);
1502 switch (r) {
1503 case DM_MAPIO_SUBMITTED:
1504 /* The target has taken the I/O to submit by itself later */
1505 break;
1506 case DM_MAPIO_REMAPPED:
1507 /* The target has remapped the I/O so dispatch it */
1508 dm_dispatch_request(clone);
1509 break;
1510 case DM_MAPIO_REQUEUE:
1511 /* The target wants to requeue the I/O */
1512 dm_requeue_unmapped_request(clone);
1513 break;
1514 default:
1515 if (r > 0) {
1516 DMWARN("unimplemented target map return value: %d", r);
1517 BUG();
1520 /* The target wants to complete the I/O */
1521 dm_kill_unmapped_request(clone, r);
1522 break;
1527 * q->request_fn for request-based dm.
1528 * Called with the queue lock held.
1530 static void dm_request_fn(struct request_queue *q)
1532 struct mapped_device *md = q->queuedata;
1533 struct dm_table *map = dm_get_table(md);
1534 struct dm_target *ti;
1535 struct request *rq;
1538 * For noflush suspend, check blk_queue_stopped() to immediately
1539 * quit I/O dispatching.
1541 while (!blk_queue_plugged(q) && !blk_queue_stopped(q)) {
1542 rq = blk_peek_request(q);
1543 if (!rq)
1544 goto plug_and_out;
1546 if (unlikely(rq == &md->suspend_rq)) { /* Flush suspend maker */
1547 if (queue_in_flight(q))
1548 /* Not quiet yet. Wait more */
1549 goto plug_and_out;
1551 /* This device should be quiet now */
1552 __stop_queue(q);
1553 blk_start_request(rq);
1554 __blk_end_request_all(rq, 0);
1555 wake_up(&md->wait);
1556 goto out;
1559 ti = dm_table_find_target(map, blk_rq_pos(rq));
1560 if (ti->type->busy && ti->type->busy(ti))
1561 goto plug_and_out;
1563 blk_start_request(rq);
1564 spin_unlock(q->queue_lock);
1565 map_request(ti, rq, md);
1566 spin_lock_irq(q->queue_lock);
1569 goto out;
1571 plug_and_out:
1572 if (!elv_queue_empty(q))
1573 /* Some requests still remain, retry later */
1574 blk_plug_device(q);
1576 out:
1577 dm_table_put(map);
1579 return;
1582 int dm_underlying_device_busy(struct request_queue *q)
1584 return blk_lld_busy(q);
1586 EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1588 static int dm_lld_busy(struct request_queue *q)
1590 int r;
1591 struct mapped_device *md = q->queuedata;
1592 struct dm_table *map = dm_get_table(md);
1594 if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1595 r = 1;
1596 else
1597 r = dm_table_any_busy_target(map);
1599 dm_table_put(map);
1601 return r;
1604 static void dm_unplug_all(struct request_queue *q)
1606 struct mapped_device *md = q->queuedata;
1607 struct dm_table *map = dm_get_table(md);
1609 if (map) {
1610 if (dm_request_based(md))
1611 generic_unplug_device(q);
1613 dm_table_unplug_all(map);
1614 dm_table_put(map);
1618 static int dm_any_congested(void *congested_data, int bdi_bits)
1620 int r = bdi_bits;
1621 struct mapped_device *md = congested_data;
1622 struct dm_table *map;
1624 if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
1625 map = dm_get_table(md);
1626 if (map) {
1628 * Request-based dm cares about only own queue for
1629 * the query about congestion status of request_queue
1631 if (dm_request_based(md))
1632 r = md->queue->backing_dev_info.state &
1633 bdi_bits;
1634 else
1635 r = dm_table_any_congested(map, bdi_bits);
1637 dm_table_put(map);
1641 return r;
1644 /*-----------------------------------------------------------------
1645 * An IDR is used to keep track of allocated minor numbers.
1646 *---------------------------------------------------------------*/
1647 static DEFINE_IDR(_minor_idr);
1649 static void free_minor(int minor)
1651 spin_lock(&_minor_lock);
1652 idr_remove(&_minor_idr, minor);
1653 spin_unlock(&_minor_lock);
1657 * See if the device with a specific minor # is free.
1659 static int specific_minor(int minor)
1661 int r, m;
1663 if (minor >= (1 << MINORBITS))
1664 return -EINVAL;
1666 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
1667 if (!r)
1668 return -ENOMEM;
1670 spin_lock(&_minor_lock);
1672 if (idr_find(&_minor_idr, minor)) {
1673 r = -EBUSY;
1674 goto out;
1677 r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
1678 if (r)
1679 goto out;
1681 if (m != minor) {
1682 idr_remove(&_minor_idr, m);
1683 r = -EBUSY;
1684 goto out;
1687 out:
1688 spin_unlock(&_minor_lock);
1689 return r;
1692 static int next_free_minor(int *minor)
1694 int r, m;
1696 r = idr_pre_get(&_minor_idr, GFP_KERNEL);
1697 if (!r)
1698 return -ENOMEM;
1700 spin_lock(&_minor_lock);
1702 r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
1703 if (r)
1704 goto out;
1706 if (m >= (1 << MINORBITS)) {
1707 idr_remove(&_minor_idr, m);
1708 r = -ENOSPC;
1709 goto out;
1712 *minor = m;
1714 out:
1715 spin_unlock(&_minor_lock);
1716 return r;
1719 static const struct block_device_operations dm_blk_dops;
1721 static void dm_wq_work(struct work_struct *work);
1724 * Allocate and initialise a blank device with a given minor.
1726 static struct mapped_device *alloc_dev(int minor)
1728 int r;
1729 struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
1730 void *old_md;
1732 if (!md) {
1733 DMWARN("unable to allocate device, out of memory.");
1734 return NULL;
1737 if (!try_module_get(THIS_MODULE))
1738 goto bad_module_get;
1740 /* get a minor number for the dev */
1741 if (minor == DM_ANY_MINOR)
1742 r = next_free_minor(&minor);
1743 else
1744 r = specific_minor(minor);
1745 if (r < 0)
1746 goto bad_minor;
1748 init_rwsem(&md->io_lock);
1749 mutex_init(&md->suspend_lock);
1750 spin_lock_init(&md->deferred_lock);
1751 rwlock_init(&md->map_lock);
1752 atomic_set(&md->holders, 1);
1753 atomic_set(&md->open_count, 0);
1754 atomic_set(&md->event_nr, 0);
1755 atomic_set(&md->uevent_seq, 0);
1756 INIT_LIST_HEAD(&md->uevent_list);
1757 spin_lock_init(&md->uevent_lock);
1759 md->queue = blk_init_queue(dm_request_fn, NULL);
1760 if (!md->queue)
1761 goto bad_queue;
1764 * Request-based dm devices cannot be stacked on top of bio-based dm
1765 * devices. The type of this dm device has not been decided yet,
1766 * although we initialized the queue using blk_init_queue().
1767 * The type is decided at the first table loading time.
1768 * To prevent problematic device stacking, clear the queue flag
1769 * for request stacking support until then.
1771 * This queue is new, so no concurrency on the queue_flags.
1773 queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
1774 md->saved_make_request_fn = md->queue->make_request_fn;
1775 md->queue->queuedata = md;
1776 md->queue->backing_dev_info.congested_fn = dm_any_congested;
1777 md->queue->backing_dev_info.congested_data = md;
1778 blk_queue_make_request(md->queue, dm_request);
1779 blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
1780 md->queue->unplug_fn = dm_unplug_all;
1781 blk_queue_merge_bvec(md->queue, dm_merge_bvec);
1782 blk_queue_softirq_done(md->queue, dm_softirq_done);
1783 blk_queue_prep_rq(md->queue, dm_prep_fn);
1784 blk_queue_lld_busy(md->queue, dm_lld_busy);
1786 md->disk = alloc_disk(1);
1787 if (!md->disk)
1788 goto bad_disk;
1790 atomic_set(&md->pending[0], 0);
1791 atomic_set(&md->pending[1], 0);
1792 init_waitqueue_head(&md->wait);
1793 INIT_WORK(&md->work, dm_wq_work);
1794 init_waitqueue_head(&md->eventq);
1796 md->disk->major = _major;
1797 md->disk->first_minor = minor;
1798 md->disk->fops = &dm_blk_dops;
1799 md->disk->queue = md->queue;
1800 md->disk->private_data = md;
1801 sprintf(md->disk->disk_name, "dm-%d", minor);
1802 add_disk(md->disk);
1803 format_dev_t(md->name, MKDEV(_major, minor));
1805 md->wq = create_singlethread_workqueue("kdmflush");
1806 if (!md->wq)
1807 goto bad_thread;
1809 md->bdev = bdget_disk(md->disk, 0);
1810 if (!md->bdev)
1811 goto bad_bdev;
1813 /* Populate the mapping, nobody knows we exist yet */
1814 spin_lock(&_minor_lock);
1815 old_md = idr_replace(&_minor_idr, md, minor);
1816 spin_unlock(&_minor_lock);
1818 BUG_ON(old_md != MINOR_ALLOCED);
1820 return md;
1822 bad_bdev:
1823 destroy_workqueue(md->wq);
1824 bad_thread:
1825 put_disk(md->disk);
1826 bad_disk:
1827 blk_cleanup_queue(md->queue);
1828 bad_queue:
1829 free_minor(minor);
1830 bad_minor:
1831 module_put(THIS_MODULE);
1832 bad_module_get:
1833 kfree(md);
1834 return NULL;
1837 static void unlock_fs(struct mapped_device *md);
1839 static void free_dev(struct mapped_device *md)
1841 int minor = MINOR(disk_devt(md->disk));
1843 unlock_fs(md);
1844 bdput(md->bdev);
1845 destroy_workqueue(md->wq);
1846 if (md->tio_pool)
1847 mempool_destroy(md->tio_pool);
1848 if (md->io_pool)
1849 mempool_destroy(md->io_pool);
1850 if (md->bs)
1851 bioset_free(md->bs);
1852 blk_integrity_unregister(md->disk);
1853 del_gendisk(md->disk);
1854 free_minor(minor);
1856 spin_lock(&_minor_lock);
1857 md->disk->private_data = NULL;
1858 spin_unlock(&_minor_lock);
1860 put_disk(md->disk);
1861 blk_cleanup_queue(md->queue);
1862 module_put(THIS_MODULE);
1863 kfree(md);
1866 static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
1868 struct dm_md_mempools *p;
1870 if (md->io_pool && md->tio_pool && md->bs)
1871 /* the md already has necessary mempools */
1872 goto out;
1874 p = dm_table_get_md_mempools(t);
1875 BUG_ON(!p || md->io_pool || md->tio_pool || md->bs);
1877 md->io_pool = p->io_pool;
1878 p->io_pool = NULL;
1879 md->tio_pool = p->tio_pool;
1880 p->tio_pool = NULL;
1881 md->bs = p->bs;
1882 p->bs = NULL;
1884 out:
1885 /* mempool bind completed, now no need any mempools in the table */
1886 dm_table_free_md_mempools(t);
1890 * Bind a table to the device.
1892 static void event_callback(void *context)
1894 unsigned long flags;
1895 LIST_HEAD(uevents);
1896 struct mapped_device *md = (struct mapped_device *) context;
1898 spin_lock_irqsave(&md->uevent_lock, flags);
1899 list_splice_init(&md->uevent_list, &uevents);
1900 spin_unlock_irqrestore(&md->uevent_lock, flags);
1902 dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
1904 atomic_inc(&md->event_nr);
1905 wake_up(&md->eventq);
1908 static void __set_size(struct mapped_device *md, sector_t size)
1910 set_capacity(md->disk, size);
1912 mutex_lock(&md->bdev->bd_inode->i_mutex);
1913 i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
1914 mutex_unlock(&md->bdev->bd_inode->i_mutex);
1917 static int __bind(struct mapped_device *md, struct dm_table *t,
1918 struct queue_limits *limits)
1920 struct request_queue *q = md->queue;
1921 sector_t size;
1922 unsigned long flags;
1924 size = dm_table_get_size(t);
1927 * Wipe any geometry if the size of the table changed.
1929 if (size != get_capacity(md->disk))
1930 memset(&md->geometry, 0, sizeof(md->geometry));
1932 __set_size(md, size);
1934 if (!size) {
1935 dm_table_destroy(t);
1936 return 0;
1939 dm_table_event_callback(t, event_callback, md);
1942 * The queue hasn't been stopped yet, if the old table type wasn't
1943 * for request-based during suspension. So stop it to prevent
1944 * I/O mapping before resume.
1945 * This must be done before setting the queue restrictions,
1946 * because request-based dm may be run just after the setting.
1948 if (dm_table_request_based(t) && !blk_queue_stopped(q))
1949 stop_queue(q);
1951 __bind_mempools(md, t);
1953 write_lock_irqsave(&md->map_lock, flags);
1954 md->map = t;
1955 dm_table_set_restrictions(t, q, limits);
1956 write_unlock_irqrestore(&md->map_lock, flags);
1958 return 0;
1961 static void __unbind(struct mapped_device *md)
1963 struct dm_table *map = md->map;
1964 unsigned long flags;
1966 if (!map)
1967 return;
1969 dm_table_event_callback(map, NULL, NULL);
1970 write_lock_irqsave(&md->map_lock, flags);
1971 md->map = NULL;
1972 write_unlock_irqrestore(&md->map_lock, flags);
1973 dm_table_destroy(map);
1977 * Constructor for a new device.
1979 int dm_create(int minor, struct mapped_device **result)
1981 struct mapped_device *md;
1983 md = alloc_dev(minor);
1984 if (!md)
1985 return -ENXIO;
1987 dm_sysfs_init(md);
1989 *result = md;
1990 return 0;
1993 static struct mapped_device *dm_find_md(dev_t dev)
1995 struct mapped_device *md;
1996 unsigned minor = MINOR(dev);
1998 if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
1999 return NULL;
2001 spin_lock(&_minor_lock);
2003 md = idr_find(&_minor_idr, minor);
2004 if (md && (md == MINOR_ALLOCED ||
2005 (MINOR(disk_devt(dm_disk(md))) != minor) ||
2006 test_bit(DMF_FREEING, &md->flags))) {
2007 md = NULL;
2008 goto out;
2011 out:
2012 spin_unlock(&_minor_lock);
2014 return md;
2017 struct mapped_device *dm_get_md(dev_t dev)
2019 struct mapped_device *md = dm_find_md(dev);
2021 if (md)
2022 dm_get(md);
2024 return md;
2027 void *dm_get_mdptr(struct mapped_device *md)
2029 return md->interface_ptr;
2032 void dm_set_mdptr(struct mapped_device *md, void *ptr)
2034 md->interface_ptr = ptr;
2037 void dm_get(struct mapped_device *md)
2039 atomic_inc(&md->holders);
2042 const char *dm_device_name(struct mapped_device *md)
2044 return md->name;
2046 EXPORT_SYMBOL_GPL(dm_device_name);
2048 void dm_put(struct mapped_device *md)
2050 struct dm_table *map;
2052 BUG_ON(test_bit(DMF_FREEING, &md->flags));
2054 if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
2055 map = dm_get_table(md);
2056 idr_replace(&_minor_idr, MINOR_ALLOCED,
2057 MINOR(disk_devt(dm_disk(md))));
2058 set_bit(DMF_FREEING, &md->flags);
2059 spin_unlock(&_minor_lock);
2060 if (!dm_suspended(md)) {
2061 dm_table_presuspend_targets(map);
2062 dm_table_postsuspend_targets(map);
2064 dm_sysfs_exit(md);
2065 dm_table_put(map);
2066 __unbind(md);
2067 free_dev(md);
2070 EXPORT_SYMBOL_GPL(dm_put);
2072 static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
2074 int r = 0;
2075 DECLARE_WAITQUEUE(wait, current);
2076 struct request_queue *q = md->queue;
2077 unsigned long flags;
2079 dm_unplug_all(md->queue);
2081 add_wait_queue(&md->wait, &wait);
2083 while (1) {
2084 set_current_state(interruptible);
2086 smp_mb();
2087 if (dm_request_based(md)) {
2088 spin_lock_irqsave(q->queue_lock, flags);
2089 if (!queue_in_flight(q) && blk_queue_stopped(q)) {
2090 spin_unlock_irqrestore(q->queue_lock, flags);
2091 break;
2093 spin_unlock_irqrestore(q->queue_lock, flags);
2094 } else if (!atomic_read(&md->pending[0]) &&
2095 !atomic_read(&md->pending[1]))
2096 break;
2098 if (interruptible == TASK_INTERRUPTIBLE &&
2099 signal_pending(current)) {
2100 r = -EINTR;
2101 break;
2104 io_schedule();
2106 set_current_state(TASK_RUNNING);
2108 remove_wait_queue(&md->wait, &wait);
2110 return r;
2113 static void dm_flush(struct mapped_device *md)
2115 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2117 bio_init(&md->barrier_bio);
2118 md->barrier_bio.bi_bdev = md->bdev;
2119 md->barrier_bio.bi_rw = WRITE_BARRIER;
2120 __split_and_process_bio(md, &md->barrier_bio);
2122 dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2125 static void process_barrier(struct mapped_device *md, struct bio *bio)
2127 md->barrier_error = 0;
2129 dm_flush(md);
2131 if (!bio_empty_barrier(bio)) {
2132 __split_and_process_bio(md, bio);
2133 dm_flush(md);
2136 if (md->barrier_error != DM_ENDIO_REQUEUE)
2137 bio_endio(bio, md->barrier_error);
2138 else {
2139 spin_lock_irq(&md->deferred_lock);
2140 bio_list_add_head(&md->deferred, bio);
2141 spin_unlock_irq(&md->deferred_lock);
2146 * Process the deferred bios
2148 static void dm_wq_work(struct work_struct *work)
2150 struct mapped_device *md = container_of(work, struct mapped_device,
2151 work);
2152 struct bio *c;
2154 down_write(&md->io_lock);
2156 while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2157 spin_lock_irq(&md->deferred_lock);
2158 c = bio_list_pop(&md->deferred);
2159 spin_unlock_irq(&md->deferred_lock);
2161 if (!c) {
2162 clear_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
2163 break;
2166 up_write(&md->io_lock);
2168 if (dm_request_based(md))
2169 generic_make_request(c);
2170 else {
2171 if (bio_rw_flagged(c, BIO_RW_BARRIER))
2172 process_barrier(md, c);
2173 else
2174 __split_and_process_bio(md, c);
2177 down_write(&md->io_lock);
2180 up_write(&md->io_lock);
2183 static void dm_queue_flush(struct mapped_device *md)
2185 clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2186 smp_mb__after_clear_bit();
2187 queue_work(md->wq, &md->work);
2191 * Swap in a new table (destroying old one).
2193 int dm_swap_table(struct mapped_device *md, struct dm_table *table)
2195 struct queue_limits limits;
2196 int r = -EINVAL;
2198 mutex_lock(&md->suspend_lock);
2200 /* device must be suspended */
2201 if (!dm_suspended(md))
2202 goto out;
2204 r = dm_calculate_queue_limits(table, &limits);
2205 if (r)
2206 goto out;
2208 /* cannot change the device type, once a table is bound */
2209 if (md->map &&
2210 (dm_table_get_type(md->map) != dm_table_get_type(table))) {
2211 DMWARN("can't change the device type after a table is bound");
2212 goto out;
2215 __unbind(md);
2216 r = __bind(md, table, &limits);
2218 out:
2219 mutex_unlock(&md->suspend_lock);
2220 return r;
2223 static void dm_rq_invalidate_suspend_marker(struct mapped_device *md)
2225 md->suspend_rq.special = (void *)0x1;
2228 static void dm_rq_abort_suspend(struct mapped_device *md, int noflush)
2230 struct request_queue *q = md->queue;
2231 unsigned long flags;
2233 spin_lock_irqsave(q->queue_lock, flags);
2234 if (!noflush)
2235 dm_rq_invalidate_suspend_marker(md);
2236 __start_queue(q);
2237 spin_unlock_irqrestore(q->queue_lock, flags);
2240 static void dm_rq_start_suspend(struct mapped_device *md, int noflush)
2242 struct request *rq = &md->suspend_rq;
2243 struct request_queue *q = md->queue;
2245 if (noflush)
2246 stop_queue(q);
2247 else {
2248 blk_rq_init(q, rq);
2249 blk_insert_request(q, rq, 0, NULL);
2253 static int dm_rq_suspend_available(struct mapped_device *md, int noflush)
2255 int r = 1;
2256 struct request *rq = &md->suspend_rq;
2257 struct request_queue *q = md->queue;
2258 unsigned long flags;
2260 if (noflush)
2261 return r;
2263 /* The marker must be protected by queue lock if it is in use */
2264 spin_lock_irqsave(q->queue_lock, flags);
2265 if (unlikely(rq->ref_count)) {
2267 * This can happen, when the previous flush suspend was
2268 * interrupted, the marker is still in the queue and
2269 * this flush suspend has been invoked, because we don't
2270 * remove the marker at the time of suspend interruption.
2271 * We have only one marker per mapped_device, so we can't
2272 * start another flush suspend while it is in use.
2274 BUG_ON(!rq->special); /* The marker should be invalidated */
2275 DMWARN("Invalidating the previous flush suspend is still in"
2276 " progress. Please retry later.");
2277 r = 0;
2279 spin_unlock_irqrestore(q->queue_lock, flags);
2281 return r;
2285 * Functions to lock and unlock any filesystem running on the
2286 * device.
2288 static int lock_fs(struct mapped_device *md)
2290 int r;
2292 WARN_ON(md->frozen_sb);
2294 md->frozen_sb = freeze_bdev(md->bdev);
2295 if (IS_ERR(md->frozen_sb)) {
2296 r = PTR_ERR(md->frozen_sb);
2297 md->frozen_sb = NULL;
2298 return r;
2301 set_bit(DMF_FROZEN, &md->flags);
2303 return 0;
2306 static void unlock_fs(struct mapped_device *md)
2308 if (!test_bit(DMF_FROZEN, &md->flags))
2309 return;
2311 thaw_bdev(md->bdev, md->frozen_sb);
2312 md->frozen_sb = NULL;
2313 clear_bit(DMF_FROZEN, &md->flags);
2317 * We need to be able to change a mapping table under a mounted
2318 * filesystem. For example we might want to move some data in
2319 * the background. Before the table can be swapped with
2320 * dm_bind_table, dm_suspend must be called to flush any in
2321 * flight bios and ensure that any further io gets deferred.
2324 * Suspend mechanism in request-based dm.
2326 * After the suspend starts, further incoming requests are kept in
2327 * the request_queue and deferred.
2328 * Remaining requests in the request_queue at the start of suspend are flushed
2329 * if it is flush suspend.
2330 * The suspend completes when the following conditions have been satisfied,
2331 * so wait for it:
2332 * 1. q->in_flight is 0 (which means no in_flight request)
2333 * 2. queue has been stopped (which means no request dispatching)
2336 * Noflush suspend
2337 * ---------------
2338 * Noflush suspend doesn't need to dispatch remaining requests.
2339 * So stop the queue immediately. Then, wait for all in_flight requests
2340 * to be completed or requeued.
2342 * To abort noflush suspend, start the queue.
2345 * Flush suspend
2346 * -------------
2347 * Flush suspend needs to dispatch remaining requests. So stop the queue
2348 * after the remaining requests are completed. (Requeued request must be also
2349 * re-dispatched and completed. Until then, we can't stop the queue.)
2351 * During flushing the remaining requests, further incoming requests are also
2352 * inserted to the same queue. To distinguish which requests are to be
2353 * flushed, we insert a marker request to the queue at the time of starting
2354 * flush suspend, like a barrier.
2355 * The dispatching is blocked when the marker is found on the top of the queue.
2356 * And the queue is stopped when all in_flight requests are completed, since
2357 * that means the remaining requests are completely flushed.
2358 * Then, the marker is removed from the queue.
2360 * To abort flush suspend, we also need to take care of the marker, not only
2361 * starting the queue.
2362 * We don't remove the marker forcibly from the queue since it's against
2363 * the block-layer manner. Instead, we put a invalidated mark on the marker.
2364 * When the invalidated marker is found on the top of the queue, it is
2365 * immediately removed from the queue, so it doesn't block dispatching.
2366 * Because we have only one marker per mapped_device, we can't start another
2367 * flush suspend until the invalidated marker is removed from the queue.
2368 * So fail and return with -EBUSY in such a case.
2370 int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
2372 struct dm_table *map = NULL;
2373 int r = 0;
2374 int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
2375 int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
2377 mutex_lock(&md->suspend_lock);
2379 if (dm_suspended(md)) {
2380 r = -EINVAL;
2381 goto out_unlock;
2384 if (dm_request_based(md) && !dm_rq_suspend_available(md, noflush)) {
2385 r = -EBUSY;
2386 goto out_unlock;
2389 map = dm_get_table(md);
2392 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
2393 * This flag is cleared before dm_suspend returns.
2395 if (noflush)
2396 set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2398 /* This does not get reverted if there's an error later. */
2399 dm_table_presuspend_targets(map);
2402 * Flush I/O to the device. noflush supersedes do_lockfs,
2403 * because lock_fs() needs to flush I/Os.
2405 if (!noflush && do_lockfs) {
2406 r = lock_fs(md);
2407 if (r)
2408 goto out;
2412 * Here we must make sure that no processes are submitting requests
2413 * to target drivers i.e. no one may be executing
2414 * __split_and_process_bio. This is called from dm_request and
2415 * dm_wq_work.
2417 * To get all processes out of __split_and_process_bio in dm_request,
2418 * we take the write lock. To prevent any process from reentering
2419 * __split_and_process_bio from dm_request, we set
2420 * DMF_QUEUE_IO_TO_THREAD.
2422 * To quiesce the thread (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND
2423 * and call flush_workqueue(md->wq). flush_workqueue will wait until
2424 * dm_wq_work exits and DMF_BLOCK_IO_FOR_SUSPEND will prevent any
2425 * further calls to __split_and_process_bio from dm_wq_work.
2427 down_write(&md->io_lock);
2428 set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2429 set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
2430 up_write(&md->io_lock);
2432 flush_workqueue(md->wq);
2434 if (dm_request_based(md))
2435 dm_rq_start_suspend(md, noflush);
2438 * At this point no more requests are entering target request routines.
2439 * We call dm_wait_for_completion to wait for all existing requests
2440 * to finish.
2442 r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
2444 down_write(&md->io_lock);
2445 if (noflush)
2446 clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
2447 up_write(&md->io_lock);
2449 /* were we interrupted ? */
2450 if (r < 0) {
2451 dm_queue_flush(md);
2453 if (dm_request_based(md))
2454 dm_rq_abort_suspend(md, noflush);
2456 unlock_fs(md);
2457 goto out; /* pushback list is already flushed, so skip flush */
2461 * If dm_wait_for_completion returned 0, the device is completely
2462 * quiescent now. There is no request-processing activity. All new
2463 * requests are being added to md->deferred list.
2466 dm_table_postsuspend_targets(map);
2468 set_bit(DMF_SUSPENDED, &md->flags);
2470 out:
2471 dm_table_put(map);
2473 out_unlock:
2474 mutex_unlock(&md->suspend_lock);
2475 return r;
2478 int dm_resume(struct mapped_device *md)
2480 int r = -EINVAL;
2481 struct dm_table *map = NULL;
2483 mutex_lock(&md->suspend_lock);
2484 if (!dm_suspended(md))
2485 goto out;
2487 map = dm_get_table(md);
2488 if (!map || !dm_table_get_size(map))
2489 goto out;
2491 r = dm_table_resume_targets(map);
2492 if (r)
2493 goto out;
2495 dm_queue_flush(md);
2498 * Flushing deferred I/Os must be done after targets are resumed
2499 * so that mapping of targets can work correctly.
2500 * Request-based dm is queueing the deferred I/Os in its request_queue.
2502 if (dm_request_based(md))
2503 start_queue(md->queue);
2505 unlock_fs(md);
2507 clear_bit(DMF_SUSPENDED, &md->flags);
2509 dm_table_unplug_all(map);
2510 r = 0;
2511 out:
2512 dm_table_put(map);
2513 mutex_unlock(&md->suspend_lock);
2515 return r;
2518 /*-----------------------------------------------------------------
2519 * Event notification.
2520 *---------------------------------------------------------------*/
2521 void dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
2522 unsigned cookie)
2524 char udev_cookie[DM_COOKIE_LENGTH];
2525 char *envp[] = { udev_cookie, NULL };
2527 if (!cookie)
2528 kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
2529 else {
2530 snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
2531 DM_COOKIE_ENV_VAR_NAME, cookie);
2532 kobject_uevent_env(&disk_to_dev(md->disk)->kobj, action, envp);
2536 uint32_t dm_next_uevent_seq(struct mapped_device *md)
2538 return atomic_add_return(1, &md->uevent_seq);
2541 uint32_t dm_get_event_nr(struct mapped_device *md)
2543 return atomic_read(&md->event_nr);
2546 int dm_wait_event(struct mapped_device *md, int event_nr)
2548 return wait_event_interruptible(md->eventq,
2549 (event_nr != atomic_read(&md->event_nr)));
2552 void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
2554 unsigned long flags;
2556 spin_lock_irqsave(&md->uevent_lock, flags);
2557 list_add(elist, &md->uevent_list);
2558 spin_unlock_irqrestore(&md->uevent_lock, flags);
2562 * The gendisk is only valid as long as you have a reference
2563 * count on 'md'.
2565 struct gendisk *dm_disk(struct mapped_device *md)
2567 return md->disk;
2570 struct kobject *dm_kobject(struct mapped_device *md)
2572 return &md->kobj;
2576 * struct mapped_device should not be exported outside of dm.c
2577 * so use this check to verify that kobj is part of md structure
2579 struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2581 struct mapped_device *md;
2583 md = container_of(kobj, struct mapped_device, kobj);
2584 if (&md->kobj != kobj)
2585 return NULL;
2587 if (test_bit(DMF_FREEING, &md->flags) ||
2588 test_bit(DMF_DELETING, &md->flags))
2589 return NULL;
2591 dm_get(md);
2592 return md;
2595 int dm_suspended(struct mapped_device *md)
2597 return test_bit(DMF_SUSPENDED, &md->flags);
2600 int dm_noflush_suspending(struct dm_target *ti)
2602 struct mapped_device *md = dm_table_get_md(ti->table);
2603 int r = __noflush_suspending(md);
2605 dm_put(md);
2607 return r;
2609 EXPORT_SYMBOL_GPL(dm_noflush_suspending);
2611 struct dm_md_mempools *dm_alloc_md_mempools(unsigned type)
2613 struct dm_md_mempools *pools = kmalloc(sizeof(*pools), GFP_KERNEL);
2615 if (!pools)
2616 return NULL;
2618 pools->io_pool = (type == DM_TYPE_BIO_BASED) ?
2619 mempool_create_slab_pool(MIN_IOS, _io_cache) :
2620 mempool_create_slab_pool(MIN_IOS, _rq_bio_info_cache);
2621 if (!pools->io_pool)
2622 goto free_pools_and_out;
2624 pools->tio_pool = (type == DM_TYPE_BIO_BASED) ?
2625 mempool_create_slab_pool(MIN_IOS, _tio_cache) :
2626 mempool_create_slab_pool(MIN_IOS, _rq_tio_cache);
2627 if (!pools->tio_pool)
2628 goto free_io_pool_and_out;
2630 pools->bs = (type == DM_TYPE_BIO_BASED) ?
2631 bioset_create(16, 0) : bioset_create(MIN_IOS, 0);
2632 if (!pools->bs)
2633 goto free_tio_pool_and_out;
2635 return pools;
2637 free_tio_pool_and_out:
2638 mempool_destroy(pools->tio_pool);
2640 free_io_pool_and_out:
2641 mempool_destroy(pools->io_pool);
2643 free_pools_and_out:
2644 kfree(pools);
2646 return NULL;
2649 void dm_free_md_mempools(struct dm_md_mempools *pools)
2651 if (!pools)
2652 return;
2654 if (pools->io_pool)
2655 mempool_destroy(pools->io_pool);
2657 if (pools->tio_pool)
2658 mempool_destroy(pools->tio_pool);
2660 if (pools->bs)
2661 bioset_free(pools->bs);
2663 kfree(pools);
2666 static const struct block_device_operations dm_blk_dops = {
2667 .open = dm_blk_open,
2668 .release = dm_blk_close,
2669 .ioctl = dm_blk_ioctl,
2670 .getgeo = dm_blk_getgeo,
2671 .owner = THIS_MODULE
2674 EXPORT_SYMBOL(dm_get_mapinfo);
2677 * module hooks
2679 module_init(dm_init);
2680 module_exit(dm_exit);
2682 module_param(major, uint, 0);
2683 MODULE_PARM_DESC(major, "The major number of the device mapper");
2684 MODULE_DESCRIPTION(DM_NAME " driver");
2685 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
2686 MODULE_LICENSE("GPL");