dm: eliminate unused "bioset" process for each bio-based DM device
[linux-2.6-block.git] / drivers / md / dm.c
index 0d7ab20c58dffc40d5c56c9427b7dd7f090c8bd3..64b50b71400f3531999225cf2e2714d30711dec6 100644 (file)
@@ -24,6 +24,7 @@
 #include <linux/ktime.h>
 #include <linux/elevator.h> /* for rq_end_sector() */
 #include <linux/blk-mq.h>
+#include <linux/pr.h>
 
 #include <trace/events/block.h>
 
@@ -124,9 +125,8 @@ EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
 #define DMF_FREEING 3
 #define DMF_DELETING 4
 #define DMF_NOFLUSH_SUSPENDING 5
-#define DMF_MERGE_IS_OPTIONAL 6
-#define DMF_DEFERRED_REMOVE 7
-#define DMF_SUSPENDED_INTERNALLY 8
+#define DMF_DEFERRED_REMOVE 6
+#define DMF_SUSPENDED_INTERNALLY 7
 
 /*
  * A dummy definition to make RCU happy.
@@ -556,18 +556,16 @@ static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
        return dm_get_geometry(md, geo);
 }
 
-static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
-                       unsigned int cmd, unsigned long arg)
+static int dm_get_live_table_for_ioctl(struct mapped_device *md,
+               struct dm_target **tgt, struct block_device **bdev,
+               fmode_t *mode, int *srcu_idx)
 {
-       struct mapped_device *md = bdev->bd_disk->private_data;
-       int srcu_idx;
        struct dm_table *map;
-       struct dm_target *tgt;
-       int r = -ENOTTY;
+       int r;
 
 retry:
-       map = dm_get_live_table(md, &srcu_idx);
-
+       r = -ENOTTY;
+       map = dm_get_live_table(md, srcu_idx);
        if (!map || !dm_table_get_size(map))
                goto out;
 
@@ -575,8 +573,9 @@ retry:
        if (dm_table_get_num_targets(map) != 1)
                goto out;
 
-       tgt = dm_table_get_target(map, 0);
-       if (!tgt->type->ioctl)
+       *tgt = dm_table_get_target(map, 0);
+
+       if (!(*tgt)->type->prepare_ioctl)
                goto out;
 
        if (dm_suspended_md(md)) {
@@ -584,16 +583,46 @@ retry:
                goto out;
        }
 
-       r = tgt->type->ioctl(tgt, cmd, arg);
+       r = (*tgt)->type->prepare_ioctl(*tgt, bdev, mode);
+       if (r < 0)
+               goto out;
 
-out:
-       dm_put_live_table(md, srcu_idx);
+       return r;
 
+out:
+       dm_put_live_table(md, *srcu_idx);
        if (r == -ENOTCONN) {
                msleep(10);
                goto retry;
        }
+       return r;
+}
 
+static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
+                       unsigned int cmd, unsigned long arg)
+{
+       struct mapped_device *md = bdev->bd_disk->private_data;
+       struct dm_target *tgt;
+       int srcu_idx, r;
+
+       r = dm_get_live_table_for_ioctl(md, &tgt, &bdev, &mode, &srcu_idx);
+       if (r < 0)
+               return r;
+
+       if (r > 0) {
+               /*
+                * Target determined this ioctl is being issued against
+                * a logical partition of the parent bdev; so extra
+                * validation is needed.
+                */
+               r = scsi_verify_blk_ioctl(NULL, cmd);
+               if (r)
+                       goto out;
+       }
+
+       r =  __blkdev_driver_ioctl(bdev, mode, cmd, arg);
+out:
+       dm_put_live_table(md, srcu_idx);
        return r;
 }
 
@@ -944,7 +973,8 @@ static void dec_pending(struct dm_io *io, int error)
                } else {
                        /* done with normal IO or empty flush */
                        trace_block_bio_complete(md->queue, bio, io_error);
-                       bio_endio(bio, io_error);
+                       bio->bi_error = io_error;
+                       bio_endio(bio);
                }
        }
 }
@@ -957,17 +987,15 @@ static void disable_write_same(struct mapped_device *md)
        limits->max_write_same_sectors = 0;
 }
 
-static void clone_endio(struct bio *bio, int error)
+static void clone_endio(struct bio *bio)
 {
+       int error = bio->bi_error;
        int r = error;
        struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
        struct dm_io *io = tio->io;
        struct mapped_device *md = tio->io->md;
        dm_endio_fn endio = tio->ti->type->end_io;
 
-       if (!bio_flagged(bio, BIO_UPTODATE) && !error)
-               error = -EIO;
-
        if (endio) {
                r = endio(tio->ti, bio, error);
                if (r < 0 || r == DM_ENDIO_REQUEUE)
@@ -996,7 +1024,7 @@ static void clone_endio(struct bio *bio, int error)
 /*
  * Partial completion handling for request-based dm
  */
-static void end_clone_bio(struct bio *clone, int error)
+static void end_clone_bio(struct bio *clone)
 {
        struct dm_rq_clone_bio_info *info =
                container_of(clone, struct dm_rq_clone_bio_info, clone);
@@ -1013,13 +1041,13 @@ static void end_clone_bio(struct bio *clone, int error)
                 * the remainder.
                 */
                return;
-       else if (error) {
+       else if (bio->bi_error) {
                /*
                 * Don't notice the error to the upper layer yet.
                 * The error handling decision is made by the target driver,
                 * when the request is completed.
                 */
-               tio->error = error;
+               tio->error = bio->bi_error;
                return;
        }
 
@@ -1466,7 +1494,7 @@ static void __map_bio(struct dm_target_io *tio)
                md = tio->io->md;
                dec_pending(tio->io, r);
                free_tio(md, tio);
-       } else if (r) {
+       } else if (r != DM_MAPIO_SUBMITTED) {
                DMWARN("unimplemented target map return value: %d", r);
                BUG();
        }
@@ -1722,60 +1750,6 @@ static void __split_and_process_bio(struct mapped_device *md,
  * CRUD END
  *---------------------------------------------------------------*/
 
-static int dm_merge_bvec(struct request_queue *q,
-                        struct bvec_merge_data *bvm,
-                        struct bio_vec *biovec)
-{
-       struct mapped_device *md = q->queuedata;
-       struct dm_table *map = dm_get_live_table_fast(md);
-       struct dm_target *ti;
-       sector_t max_sectors;
-       int max_size = 0;
-
-       if (unlikely(!map))
-               goto out;
-
-       ti = dm_table_find_target(map, bvm->bi_sector);
-       if (!dm_target_is_valid(ti))
-               goto out;
-
-       /*
-        * Find maximum amount of I/O that won't need splitting
-        */
-       max_sectors = min(max_io_len(bvm->bi_sector, ti),
-                         (sector_t) BIO_MAX_SECTORS);
-       max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
-       if (max_size < 0)
-               max_size = 0;
-
-       /*
-        * merge_bvec_fn() returns number of bytes
-        * it can accept at this offset
-        * max is precomputed maximal io size
-        */
-       if (max_size && ti->type->merge)
-               max_size = ti->type->merge(ti, bvm, biovec, max_size);
-       /*
-        * If the target doesn't support merge method and some of the devices
-        * provided their merge_bvec method (we know this by looking at
-        * queue_max_hw_sectors), then we can't allow bios with multiple vector
-        * entries.  So always set max_size to 0, and the code below allows
-        * just one page.
-        */
-       else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
-               max_size = 0;
-
-out:
-       dm_put_live_table_fast(md);
-       /*
-        * Always allow an entire first page
-        */
-       if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
-               max_size = biovec->bv_len;
-
-       return max_size;
-}
-
 /*
  * The request function that just remaps the bio built up by
  * dm_merge_bvec.
@@ -2251,6 +2225,13 @@ static void dm_init_md_queue(struct mapped_device *md)
         * This queue is new, so no concurrency on the queue_flags.
         */
        queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
+
+       /*
+        * Initialize data that will only be used by a non-blk-mq DM queue
+        * - must do so here (in alloc_dev callchain) before queue is used
+        */
+       md->queue->queuedata = md;
+       md->queue->backing_dev_info.congested_data = md;
 }
 
 static void dm_init_old_md_queue(struct mapped_device *md)
@@ -2261,10 +2242,7 @@ static void dm_init_old_md_queue(struct mapped_device *md)
        /*
         * Initialize aspects of queue that aren't relevant for blk-mq
         */
-       md->queue->queuedata = md;
        md->queue->backing_dev_info.congested_fn = dm_any_congested;
-       md->queue->backing_dev_info.congested_data = md;
-
        blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
 }
 
@@ -2274,10 +2252,8 @@ static void cleanup_mapped_device(struct mapped_device *md)
                destroy_workqueue(md->wq);
        if (md->kworker_task)
                kthread_stop(md->kworker_task);
-       if (md->io_pool)
-               mempool_destroy(md->io_pool);
-       if (md->rq_pool)
-               mempool_destroy(md->rq_pool);
+       mempool_destroy(md->io_pool);
+       mempool_destroy(md->rq_pool);
        if (md->bs)
                bioset_free(md->bs);
 
@@ -2495,59 +2471,6 @@ static void __set_size(struct mapped_device *md, sector_t size)
        i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
 }
 
-/*
- * Return 1 if the queue has a compulsory merge_bvec_fn function.
- *
- * If this function returns 0, then the device is either a non-dm
- * device without a merge_bvec_fn, or it is a dm device that is
- * able to split any bios it receives that are too big.
- */
-int dm_queue_merge_is_compulsory(struct request_queue *q)
-{
-       struct mapped_device *dev_md;
-
-       if (!q->merge_bvec_fn)
-               return 0;
-
-       if (q->make_request_fn == dm_make_request) {
-               dev_md = q->queuedata;
-               if (test_bit(DMF_MERGE_IS_OPTIONAL, &dev_md->flags))
-                       return 0;
-       }
-
-       return 1;
-}
-
-static int dm_device_merge_is_compulsory(struct dm_target *ti,
-                                        struct dm_dev *dev, sector_t start,
-                                        sector_t len, void *data)
-{
-       struct block_device *bdev = dev->bdev;
-       struct request_queue *q = bdev_get_queue(bdev);
-
-       return dm_queue_merge_is_compulsory(q);
-}
-
-/*
- * Return 1 if it is acceptable to ignore merge_bvec_fn based
- * on the properties of the underlying devices.
- */
-static int dm_table_merge_is_optional(struct dm_table *table)
-{
-       unsigned i = 0;
-       struct dm_target *ti;
-
-       while (i < dm_table_get_num_targets(table)) {
-               ti = dm_table_get_target(table, i++);
-
-               if (ti->type->iterate_devices &&
-                   ti->type->iterate_devices(ti, dm_device_merge_is_compulsory, NULL))
-                       return 0;
-       }
-
-       return 1;
-}
-
 /*
  * Returns old map, which caller must destroy.
  */
@@ -2557,7 +2480,6 @@ static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
        struct dm_table *old_map;
        struct request_queue *q = md->queue;
        sector_t size;
-       int merge_is_optional;
 
        size = dm_table_get_size(t);
 
@@ -2583,17 +2505,11 @@ static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
 
        __bind_mempools(md, t);
 
-       merge_is_optional = dm_table_merge_is_optional(t);
-
        old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
        rcu_assign_pointer(md->map, t);
        md->immutable_target_type = dm_table_get_immutable_target_type(t);
 
        dm_table_set_restrictions(t, q, limits);
-       if (merge_is_optional)
-               set_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
-       else
-               clear_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
        if (old_map)
                dm_sync_table(md);
 
@@ -2874,7 +2790,12 @@ int dm_setup_md_queue(struct mapped_device *md)
        case DM_TYPE_BIO_BASED:
                dm_init_old_md_queue(md);
                blk_queue_make_request(md->queue, dm_make_request);
-               blk_queue_merge_bvec(md->queue, dm_merge_bvec);
+               /*
+                * DM handles splitting bios as needed.  Free the bio_split bioset
+                * since it won't be used (saves 1 process per bio-based DM device).
+                */
+               bioset_free(md->queue->bio_split);
+               md->queue->bio_split = NULL;
                break;
        }
 
@@ -3623,11 +3544,8 @@ void dm_free_md_mempools(struct dm_md_mempools *pools)
        if (!pools)
                return;
 
-       if (pools->io_pool)
-               mempool_destroy(pools->io_pool);
-
-       if (pools->rq_pool)
-               mempool_destroy(pools->rq_pool);
+       mempool_destroy(pools->io_pool);
+       mempool_destroy(pools->rq_pool);
 
        if (pools->bs)
                bioset_free(pools->bs);
@@ -3635,11 +3553,133 @@ void dm_free_md_mempools(struct dm_md_mempools *pools)
        kfree(pools);
 }
 
+static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
+               u32 flags)
+{
+       struct mapped_device *md = bdev->bd_disk->private_data;
+       const struct pr_ops *ops;
+       struct dm_target *tgt;
+       fmode_t mode;
+       int srcu_idx, r;
+
+       r = dm_get_live_table_for_ioctl(md, &tgt, &bdev, &mode, &srcu_idx);
+       if (r < 0)
+               return r;
+
+       ops = bdev->bd_disk->fops->pr_ops;
+       if (ops && ops->pr_register)
+               r = ops->pr_register(bdev, old_key, new_key, flags);
+       else
+               r = -EOPNOTSUPP;
+
+       dm_put_live_table(md, srcu_idx);
+       return r;
+}
+
+static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
+               u32 flags)
+{
+       struct mapped_device *md = bdev->bd_disk->private_data;
+       const struct pr_ops *ops;
+       struct dm_target *tgt;
+       fmode_t mode;
+       int srcu_idx, r;
+
+       r = dm_get_live_table_for_ioctl(md, &tgt, &bdev, &mode, &srcu_idx);
+       if (r < 0)
+               return r;
+
+       ops = bdev->bd_disk->fops->pr_ops;
+       if (ops && ops->pr_reserve)
+               r = ops->pr_reserve(bdev, key, type, flags);
+       else
+               r = -EOPNOTSUPP;
+
+       dm_put_live_table(md, srcu_idx);
+       return r;
+}
+
+static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
+{
+       struct mapped_device *md = bdev->bd_disk->private_data;
+       const struct pr_ops *ops;
+       struct dm_target *tgt;
+       fmode_t mode;
+       int srcu_idx, r;
+
+       r = dm_get_live_table_for_ioctl(md, &tgt, &bdev, &mode, &srcu_idx);
+       if (r < 0)
+               return r;
+
+       ops = bdev->bd_disk->fops->pr_ops;
+       if (ops && ops->pr_release)
+               r = ops->pr_release(bdev, key, type);
+       else
+               r = -EOPNOTSUPP;
+
+       dm_put_live_table(md, srcu_idx);
+       return r;
+}
+
+static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
+               enum pr_type type, bool abort)
+{
+       struct mapped_device *md = bdev->bd_disk->private_data;
+       const struct pr_ops *ops;
+       struct dm_target *tgt;
+       fmode_t mode;
+       int srcu_idx, r;
+
+       r = dm_get_live_table_for_ioctl(md, &tgt, &bdev, &mode, &srcu_idx);
+       if (r < 0)
+               return r;
+
+       ops = bdev->bd_disk->fops->pr_ops;
+       if (ops && ops->pr_preempt)
+               r = ops->pr_preempt(bdev, old_key, new_key, type, abort);
+       else
+               r = -EOPNOTSUPP;
+
+       dm_put_live_table(md, srcu_idx);
+       return r;
+}
+
+static int dm_pr_clear(struct block_device *bdev, u64 key)
+{
+       struct mapped_device *md = bdev->bd_disk->private_data;
+       const struct pr_ops *ops;
+       struct dm_target *tgt;
+       fmode_t mode;
+       int srcu_idx, r;
+
+       r = dm_get_live_table_for_ioctl(md, &tgt, &bdev, &mode, &srcu_idx);
+       if (r < 0)
+               return r;
+
+       ops = bdev->bd_disk->fops->pr_ops;
+       if (ops && ops->pr_clear)
+               r = ops->pr_clear(bdev, key);
+       else
+               r = -EOPNOTSUPP;
+
+       dm_put_live_table(md, srcu_idx);
+       return r;
+}
+
+static const struct pr_ops dm_pr_ops = {
+       .pr_register    = dm_pr_register,
+       .pr_reserve     = dm_pr_reserve,
+       .pr_release     = dm_pr_release,
+       .pr_preempt     = dm_pr_preempt,
+       .pr_clear       = dm_pr_clear,
+};
+
 static const struct block_device_operations dm_blk_dops = {
        .open = dm_blk_open,
        .release = dm_blk_close,
        .ioctl = dm_blk_ioctl,
        .getgeo = dm_blk_getgeo,
+       .pr_ops = &dm_pr_ops,
        .owner = THIS_MODULE
 };