block: split scsi_request out of struct request
[linux-block.git] / block / blk-core.c
index 61ba08c58b649b54fdfbc4e06c7e1b11b015512a..f3bc083d4c1484dcf444dba88d5e19cfb57118ae 100644 (file)
@@ -39,6 +39,7 @@
 
 #include "blk.h"
 #include "blk-mq.h"
+#include "blk-mq-sched.h"
 #include "blk-wbt.h"
 
 EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_remap);
@@ -131,9 +132,8 @@ void blk_rq_init(struct request_queue *q, struct request *rq)
        rq->__sector = (sector_t) -1;
        INIT_HLIST_NODE(&rq->hash);
        RB_CLEAR_NODE(&rq->rb_node);
-       rq->cmd = rq->__cmd;
-       rq->cmd_len = BLK_MAX_CDB;
        rq->tag = -1;
+       rq->internal_tag = -1;
        rq->start_time = jiffies;
        set_start_time_ns(rq);
        rq->part = NULL;
@@ -158,8 +158,6 @@ static void req_bio_endio(struct request *rq, struct bio *bio,
 
 void blk_dump_rq_flags(struct request *rq, char *msg)
 {
-       int bit;
-
        printk(KERN_INFO "%s: dev %s: type=%x, flags=%llx\n", msg,
                rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
                (unsigned long long) rq->cmd_flags);
@@ -169,13 +167,6 @@ void blk_dump_rq_flags(struct request *rq, char *msg)
               blk_rq_sectors(rq), blk_rq_cur_sectors(rq));
        printk(KERN_INFO "  bio %p, biotail %p, len %u\n",
               rq->bio, rq->biotail, blk_rq_bytes(rq));
-
-       if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
-               printk(KERN_INFO "  cdb: ");
-               for (bit = 0; bit < BLK_MAX_CDB; bit++)
-                       printk("%02x ", rq->cmd[bit]);
-               printk("\n");
-       }
 }
 EXPORT_SYMBOL(blk_dump_rq_flags);
 
@@ -604,17 +595,41 @@ void blk_cleanup_queue(struct request_queue *q)
 EXPORT_SYMBOL(blk_cleanup_queue);
 
 /* Allocate memory local to the request queue */
-static void *alloc_request_struct(gfp_t gfp_mask, void *data)
+static void *alloc_request_simple(gfp_t gfp_mask, void *data)
 {
-       int nid = (int)(long)data;
-       return kmem_cache_alloc_node(request_cachep, gfp_mask, nid);
+       struct request_queue *q = data;
+
+       return kmem_cache_alloc_node(request_cachep, gfp_mask, q->node);
 }
 
-static void free_request_struct(void *element, void *unused)
+static void free_request_simple(void *element, void *data)
 {
        kmem_cache_free(request_cachep, element);
 }
 
+static void *alloc_request_size(gfp_t gfp_mask, void *data)
+{
+       struct request_queue *q = data;
+       struct request *rq;
+
+       rq = kmalloc_node(sizeof(struct request) + q->cmd_size, gfp_mask,
+                       q->node);
+       if (rq && q->init_rq_fn && q->init_rq_fn(q, rq, gfp_mask) < 0) {
+               kfree(rq);
+               rq = NULL;
+       }
+       return rq;
+}
+
+static void free_request_size(void *element, void *data)
+{
+       struct request_queue *q = data;
+
+       if (q->exit_rq_fn)
+               q->exit_rq_fn(q, element);
+       kfree(element);
+}
+
 int blk_init_rl(struct request_list *rl, struct request_queue *q,
                gfp_t gfp_mask)
 {
@@ -627,10 +642,15 @@ int blk_init_rl(struct request_list *rl, struct request_queue *q,
        init_waitqueue_head(&rl->wait[BLK_RW_SYNC]);
        init_waitqueue_head(&rl->wait[BLK_RW_ASYNC]);
 
-       rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, alloc_request_struct,
-                                         free_request_struct,
-                                         (void *)(long)q->node, gfp_mask,
-                                         q->node);
+       if (q->cmd_size) {
+               rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ,
+                               alloc_request_size, free_request_size,
+                               q, gfp_mask, q->node);
+       } else {
+               rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ,
+                               alloc_request_simple, free_request_simple,
+                               q, gfp_mask, q->node);
+       }
        if (!rl->rq_pool)
                return -ENOMEM;
 
@@ -821,15 +841,19 @@ EXPORT_SYMBOL(blk_init_queue);
 struct request_queue *
 blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
 {
-       struct request_queue *uninit_q, *q;
+       struct request_queue *q;
 
-       uninit_q = blk_alloc_queue_node(GFP_KERNEL, node_id);
-       if (!uninit_q)
+       q = blk_alloc_queue_node(GFP_KERNEL, node_id);
+       if (!q)
                return NULL;
 
-       q = blk_init_allocated_queue(uninit_q, rfn, lock);
-       if (!q)
-               blk_cleanup_queue(uninit_q);
+       q->request_fn = rfn;
+       if (lock)
+               q->queue_lock = lock;
+       if (blk_init_allocated_queue(q) < 0) {
+               blk_cleanup_queue(q);
+               return NULL;
+       }
 
        return q;
 }
@@ -837,30 +861,22 @@ EXPORT_SYMBOL(blk_init_queue_node);
 
 static blk_qc_t blk_queue_bio(struct request_queue *q, struct bio *bio);
 
-struct request_queue *
-blk_init_allocated_queue(struct request_queue *q, request_fn_proc *rfn,
-                        spinlock_t *lock)
-{
-       if (!q)
-               return NULL;
 
-       q->fq = blk_alloc_flush_queue(q, NUMA_NO_NODE, 0);
+int blk_init_allocated_queue(struct request_queue *q)
+{
+       q->fq = blk_alloc_flush_queue(q, NUMA_NO_NODE, q->cmd_size);
        if (!q->fq)
-               return NULL;
+               return -ENOMEM;
+
+       if (q->init_rq_fn && q->init_rq_fn(q, q->fq->flush_rq, GFP_KERNEL))
+               goto out_free_flush_queue;
 
        if (blk_init_rl(&q->root_rl, q, GFP_KERNEL))
-               goto fail;
+               goto out_exit_flush_rq;
 
        INIT_WORK(&q->timeout_work, blk_timeout_work);
-       q->request_fn           = rfn;
-       q->prep_rq_fn           = NULL;
-       q->unprep_rq_fn         = NULL;
        q->queue_flags          |= QUEUE_FLAG_DEFAULT;
 
-       /* Override internal queue lock with supplied lock pointer */
-       if (lock)
-               q->queue_lock           = lock;
-
        /*
         * This also sets hw/phys segments, boundary and size
         */
@@ -874,17 +890,19 @@ blk_init_allocated_queue(struct request_queue *q, request_fn_proc *rfn,
        /* init elevator */
        if (elevator_init(q, NULL)) {
                mutex_unlock(&q->sysfs_lock);
-               goto fail;
+               goto out_exit_flush_rq;
        }
 
        mutex_unlock(&q->sysfs_lock);
+       return 0;
 
-       return q;
-
-fail:
+out_exit_flush_rq:
+       if (q->exit_rq_fn)
+               q->exit_rq_fn(q, q->fq->flush_rq);
+out_free_flush_queue:
        blk_free_flush_queue(q->fq);
        wbt_exit(q);
-       return NULL;
+       return -ENOMEM;
 }
 EXPORT_SYMBOL(blk_init_allocated_queue);
 
@@ -1020,41 +1038,6 @@ int blk_update_nr_requests(struct request_queue *q, unsigned int nr)
        return 0;
 }
 
-/*
- * Determine if elevator data should be initialized when allocating the
- * request associated with @bio.
- */
-static bool blk_rq_should_init_elevator(struct bio *bio)
-{
-       if (!bio)
-               return true;
-
-       /*
-        * Flush requests do not use the elevator so skip initialization.
-        * This allows a request to share the flush and elevator data.
-        */
-       if (bio->bi_opf & (REQ_PREFLUSH | REQ_FUA))
-               return false;
-
-       return true;
-}
-
-/**
- * rq_ioc - determine io_context for request allocation
- * @bio: request being allocated is for this bio (can be %NULL)
- *
- * Determine io_context to use for request allocation for @bio.  May return
- * %NULL if %current->io_context doesn't exist.
- */
-static struct io_context *rq_ioc(struct bio *bio)
-{
-#ifdef CONFIG_BLK_CGROUP
-       if (bio && bio->bi_ioc)
-               return bio->bi_ioc;
-#endif
-       return current->io_context;
-}
-
 /**
  * __get_request - get a free request
  * @rl: request list to allocate from
@@ -1133,10 +1116,13 @@ static struct request *__get_request(struct request_list *rl, unsigned int op,
         * request is freed.  This guarantees icq's won't be destroyed and
         * makes creating new ones safe.
         *
+        * Flush requests do not use the elevator so skip initialization.
+        * This allows a request to share the flush and elevator data.
+        *
         * Also, lookup icq while holding queue_lock.  If it doesn't exist,
         * it will be created after releasing queue_lock.
         */
-       if (blk_rq_should_init_elevator(bio) && !blk_queue_bypass(q)) {
+       if (!op_is_flush(op) && !blk_queue_bypass(q)) {
                rq_flags |= RQF_ELVPRIV;
                q->nr_rqs_elvpriv++;
                if (et->icq_cache && ioc)
@@ -1290,8 +1276,6 @@ static struct request *blk_old_get_request(struct request_queue *q, int rw,
 {
        struct request *rq;
 
-       BUG_ON(rw != READ && rw != WRITE);
-
        /* create ioc upfront */
        create_io_context(gfp_mask, q->node);
 
@@ -1320,18 +1304,6 @@ struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
 }
 EXPORT_SYMBOL(blk_get_request);
 
-/**
- * blk_rq_set_block_pc - initialize a request to type BLOCK_PC
- * @rq:                request to be initialized
- *
- */
-void blk_rq_set_block_pc(struct request *rq)
-{
-       rq->cmd_type = REQ_TYPE_BLOCK_PC;
-       memset(rq->__cmd, 0, sizeof(rq->__cmd));
-}
-EXPORT_SYMBOL(blk_rq_set_block_pc);
-
 /**
  * blk_requeue_request - put a request back on queue
  * @q:         request queue where request should be inserted
@@ -1655,7 +1627,7 @@ static blk_qc_t blk_queue_bio(struct request_queue *q, struct bio *bio)
                return BLK_QC_T_NONE;
        }
 
-       if (bio->bi_opf & (REQ_PREFLUSH | REQ_FUA)) {
+       if (op_is_flush(bio->bi_opf)) {
                spin_lock_irq(q->queue_lock);
                where = ELEVATOR_INSERT_FLUSH;
                goto get_rq;
@@ -1894,7 +1866,7 @@ generic_make_request_checks(struct bio *bio)
         * drivers without flush support don't have to worry
         * about them.
         */
-       if ((bio->bi_opf & (REQ_PREFLUSH | REQ_FUA)) &&
+       if (op_is_flush(bio->bi_opf) &&
            !test_bit(QUEUE_FLAG_WC, &q->queue_flags)) {
                bio->bi_opf &= ~(REQ_PREFLUSH | REQ_FUA);
                if (!nr_sectors) {
@@ -2143,7 +2115,7 @@ int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
        if (q->mq_ops) {
                if (blk_queue_io_stat(q))
                        blk_account_io_start(rq, true);
-               blk_mq_insert_request(rq, false, true, false);
+               blk_mq_sched_insert_request(rq, false, true, false, false);
                return 0;
        }
 
@@ -2159,7 +2131,7 @@ int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
         */
        BUG_ON(blk_queued_rq(rq));
 
-       if (rq->cmd_flags & (REQ_PREFLUSH | REQ_FUA))
+       if (op_is_flush(rq->cmd_flags))
                where = ELEVATOR_INSERT_FLUSH;
 
        add_acct_request(q, rq, where);
@@ -2464,14 +2436,6 @@ void blk_start_request(struct request *req)
                wbt_issue(req->q->rq_wb, &req->issue_stat);
        }
 
-       /*
-        * We are now handing the request to the hardware, initialize
-        * resid_len to full count and add the timeout handler.
-        */
-       req->resid_len = blk_rq_bytes(req);
-       if (unlikely(blk_bidi_rq(req)))
-               req->next_rq->resid_len = blk_rq_bytes(req->next_rq);
-
        BUG_ON(test_bit(REQ_ATOM_COMPLETE, &req->atomic_flags));
        blk_add_timer(req);
 }
@@ -3270,7 +3234,7 @@ void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
                /*
                 * rq is already accounted, so use raw insert
                 */
-               if (rq->cmd_flags & (REQ_PREFLUSH | REQ_FUA))
+               if (op_is_flush(rq->cmd_flags))
                        __elv_add_request(q, rq, ELEVATOR_INSERT_FLUSH);
                else
                        __elv_add_request(q, rq, ELEVATOR_INSERT_SORT_MERGE);