xen/blkback: Cleanup move the code a bit around.
[firefly-linux-kernel-4.4.55.git] / drivers / xen / blkback / blkback.c
index 8a4b1e8eeb62537990800bc8758c0d642fd30a3b..f282463d7b5ce0f5d63938a5c02eccff4528a6d3 100644 (file)
@@ -63,8 +63,8 @@ module_param_named(reqs, blkif_reqs, int, 0);
 MODULE_PARM_DESC(reqs, "Number of blkback requests to allocate");
 
 /* Run-time switchable: /sys/module/blkback/parameters/ */
-static unsigned int log_stats = 0;
-static unsigned int debug_lvl = 0;
+static unsigned int log_stats;
+static unsigned int debug_lvl;
 module_param(log_stats, int, 0644);
 module_param(debug_lvl, int, 0644);
 
@@ -74,20 +74,20 @@ module_param(debug_lvl, int, 0644);
  * the pendcnt towards zero. When it hits zero, the specified domain has a
  * response queued for it, with the saved 'id' passed back.
  */
-typedef struct {
-       blkif_t       *blkif;
+struct pending_req {
+       struct blkif_st       *blkif;
        u64            id;
        int            nr_pages;
        atomic_t       pendcnt;
        unsigned short operation;
        int            status;
        struct list_head free_list;
-} pending_req_t;
+};
 
 #define BLKBACK_INVALID_HANDLE (~0)
 
 struct xen_blkbk {
-       pending_req_t           *pending_reqs;
+       struct pending_req      *pending_reqs;
        /* List of all 'pending_req' available */
        struct list_head        pending_free;
        /* And its spinlock. */
@@ -106,14 +106,15 @@ static struct xen_blkbk *blkbk;
  * pending_pages[..]. For each 'pending_req' we have have up to
  * BLKIF_MAX_SEGMENTS_PER_REQUEST (11) pages. The seg would be from 0 through
  * 10 and would index in the pending_pages[..]. */
-static inline int vaddr_pagenr(pending_req_t *req, int seg)
+static inline int vaddr_pagenr(struct pending_req *req, int seg)
 {
-       return (req - blkbk->pending_reqs) * BLKIF_MAX_SEGMENTS_PER_REQUEST + seg;
+       return (req - blkbk->pending_reqs) *
+               BLKIF_MAX_SEGMENTS_PER_REQUEST + seg;
 }
 
 #define pending_page(req, seg) pending_pages[vaddr_pagenr(req, seg)]
 
-static inline unsigned long vaddr(pending_req_t *req, int seg)
+static inline unsigned long vaddr(struct pending_req *req, int seg)
 {
        unsigned long pfn = page_to_pfn(blkbk->pending_page(req, seg));
        return (unsigned long)pfn_to_kaddr(pfn);
@@ -123,24 +124,25 @@ static inline unsigned long vaddr(pending_req_t *req, int seg)
        (blkbk->pending_grant_handles[vaddr_pagenr(_req, _seg)])
 
 
-static int do_block_io_op(blkif_t *blkif);
-static void dispatch_rw_block_io(blkif_t *blkif,
+static int do_block_io_op(struct blkif_st *blkif);
+static void dispatch_rw_block_io(struct blkif_st *blkif,
                                 struct blkif_request *req,
-                                pending_req_t *pending_req);
-static void make_response(blkif_t *blkif, u64 id,
+                                struct pending_req *pending_req);
+static void make_response(struct blkif_st *blkif, u64 id,
                          unsigned short op, int st);
 
 /*
  * Retrieve from the 'pending_reqs' a free pending_req structure to be used.
  */
-static pending_req_t* alloc_req(void)
+static struct pending_req *alloc_req(void)
 {
-       pending_req_t *req = NULL;
+       struct pending_req *req = NULL;
        unsigned long flags;
 
        spin_lock_irqsave(&blkbk->pending_free_lock, flags);
        if (!list_empty(&blkbk->pending_free)) {
-               req = list_entry(blkbk->pending_free.next, pending_req_t, free_list);
+               req = list_entry(blkbk->pending_free.next, struct pending_req,
+                                free_list);
                list_del(&req->free_list);
        }
        spin_unlock_irqrestore(&blkbk->pending_free_lock, flags);
@@ -151,7 +153,7 @@ static pending_req_t* alloc_req(void)
  * Return the 'pending_req' structure back to the freepool. We also
  * wake up the thread if it was waiting for a free page.
  */
-static void free_req(pending_req_t *req)
+static void free_req(struct pending_req *req)
 {
        unsigned long flags;
        int was_empty;
@@ -165,79 +167,25 @@ static void free_req(pending_req_t *req)
 }
 
 /*
- * Give back a reference count on the underlaying storage.
- * It is OK to make multiple calls in this function as it
- * resets the plug to NULL when it is done on the first call.
+ * Notification from the guest OS.
  */
-static void unplug_queue(blkif_t *blkif)
+static void blkif_notify_work(struct blkif_st *blkif)
 {
-       if (blkif->plug == NULL)
-               return;
-       if (blkif->plug->unplug_fn)
-               blkif->plug->unplug_fn(blkif->plug);
-       blk_put_queue(blkif->plug);
-       blkif->plug = NULL;
+       blkif->waiting_reqs = 1;
+       wake_up(&blkif->wq);
 }
 
-/*
- * Take a reference count on the underlaying storage.
- * It is OK to call this multiple times as we check to make sure
- * not to double reference. We also give back a reference count
- * if it corresponds to another queue.
- */
-static void plug_queue(blkif_t *blkif, struct block_device *bdev)
+irqreturn_t blkif_be_int(int irq, void *dev_id)
 {
-       struct request_queue *q = bdev_get_queue(bdev);
-
-       if (q == blkif->plug)
-               return;
-       unplug_queue(blkif);
-       blk_get_queue(q);
-       blkif->plug = q;
+       blkif_notify_work(dev_id);
+       return IRQ_HANDLED;
 }
 
 /*
- * Unmap the grant references, and also remove the M2P over-rides
- * used in the 'pending_req'.
-*/
-static void fast_flush_area(pending_req_t *req)
-{
-       struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
-       unsigned int i, invcount = 0;
-       grant_handle_t handle;
-       int ret;
-
-       for (i = 0; i < req->nr_pages; i++) {
-               handle = pending_handle(req, i);
-               if (handle == BLKBACK_INVALID_HANDLE)
-                       continue;
-               gnttab_set_unmap_op(&unmap[invcount], vaddr(req, i),
-                                   GNTMAP_host_map, handle);
-               pending_handle(req, i) = BLKBACK_INVALID_HANDLE;
-               invcount++;
-       }
-
-       ret = HYPERVISOR_grant_table_op(
-               GNTTABOP_unmap_grant_ref, unmap, invcount);
-       BUG_ON(ret);
-       /* Note, we use invcount, so nr->pages, so we can't index
-        * using vaddr(req, i). */
-       for (i = 0; i < invcount; i++) {
-               ret = m2p_remove_override(
-                       virt_to_page(unmap[i].host_addr), false);
-               if (ret) {
-                       printk(KERN_ALERT "Failed to remove M2P override for " \
-                               "%lx\n", (unsigned long)unmap[i].host_addr);
-                       continue;
-               }
-       }
-}
-
-/******************************************************************
  * SCHEDULER FUNCTIONS
  */
 
-static void print_stats(blkif_t *blkif)
+static void print_stats(struct blkif_st *blkif)
 {
        printk(KERN_DEBUG "%s: oo %3d  |  rd %4d  |  wr %4d  |  br %4d\n",
               current->comm, blkif->st_oo_req,
@@ -250,7 +198,7 @@ static void print_stats(blkif_t *blkif)
 
 int blkif_schedule(void *arg)
 {
-       blkif_t *blkif = arg;
+       struct blkif_st *blkif = arg;
        struct vbd *vbd = &blkif->vbd;
 
        blkif_get(blkif);
@@ -269,14 +217,14 @@ int blkif_schedule(void *arg)
                        blkif->waiting_reqs || kthread_should_stop());
                wait_event_interruptible(
                        blkbk->pending_free_wq,
-                       !list_empty(&blkbk->pending_free) || kthread_should_stop());
+                       !list_empty(&blkbk->pending_free) ||
+                       kthread_should_stop());
 
                blkif->waiting_reqs = 0;
                smp_mb(); /* clear flag *before* checking for work */
 
                if (do_block_io_op(blkif))
                        blkif->waiting_reqs = 1;
-               unplug_queue(blkif);
 
                if (log_stats && time_after(jiffies, blkif->st_print))
                        print_stats(blkif);
@@ -293,11 +241,48 @@ int blkif_schedule(void *arg)
        return 0;
 }
 
+/*
+ * Unmap the grant references, and also remove the M2P over-rides
+ * used in the 'pending_req'.
+*/
+static void fast_flush_area(struct pending_req *req)
+{
+       struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
+       unsigned int i, invcount = 0;
+       grant_handle_t handle;
+       int ret;
+
+       for (i = 0; i < req->nr_pages; i++) {
+               handle = pending_handle(req, i);
+               if (handle == BLKBACK_INVALID_HANDLE)
+                       continue;
+               gnttab_set_unmap_op(&unmap[invcount], vaddr(req, i),
+                                   GNTMAP_host_map, handle);
+               pending_handle(req, i) = BLKBACK_INVALID_HANDLE;
+               invcount++;
+       }
+
+       ret = HYPERVISOR_grant_table_op(
+               GNTTABOP_unmap_grant_ref, unmap, invcount);
+       BUG_ON(ret);
+       /* Note, we use invcount, so nr->pages, so we can't index
+        * using vaddr(req, i).
+        */
+       for (i = 0; i < invcount; i++) {
+               ret = m2p_remove_override(
+                       virt_to_page(unmap[i].host_addr), false);
+               if (ret) {
+                       printk(KERN_ALERT "Failed to remove M2P override for " \
+                               "%lx\n", (unsigned long)unmap[i].host_addr);
+                       continue;
+               }
+       }
+}
 /*
  * Completion callback on the bio's. Called as bh->b_end_io()
  */
 
-static void __end_block_io_op(pending_req_t *pending_req, int error)
+static void __end_block_io_op(struct pending_req *pending_req, int error)
 {
        /* An error fails the entire request. */
        if ((pending_req->operation == BLKIF_OP_WRITE_BARRIER) &&
@@ -313,7 +298,8 @@ static void __end_block_io_op(pending_req_t *pending_req, int error)
 
        /* If all of the bio's have completed it is time to unmap
         * the grant references associated with 'request' and provide
-        * the proper response on the ring. */
+        * the proper response on the ring.
+        */
        if (atomic_dec_and_test(&pending_req->pendcnt)) {
                fast_flush_area(pending_req);
                make_response(pending_req->blkif, pending_req->id,
@@ -333,34 +319,17 @@ static void end_block_io_op(struct bio *bio, int error)
 }
 
 
-/*
- * Notification from the guest OS.
- */
-
-static void blkif_notify_work(blkif_t *blkif)
-{
-       blkif->waiting_reqs = 1;
-       wake_up(&blkif->wq);
-}
-
-irqreturn_t blkif_be_int(int irq, void *dev_id)
-{
-       blkif_notify_work(dev_id);
-       return IRQ_HANDLED;
-}
-
-
 
 /*
  * Function to copy the from the ring buffer the 'struct blkif_request'
  * (which has the sectors we want, number of them, grant references, etc),
  * and transmute  it to the block API to hand it over to the proper block disk.
  */
-static int do_block_io_op(blkif_t *blkif)
+static int do_block_io_op(struct blkif_st *blkif)
 {
        union blkif_back_rings *blk_rings = &blkif->blk_rings;
        struct blkif_request req;
-       pending_req_t *pending_req;
+       struct pending_req *pending_req;
        RING_IDX rc, rp;
        int more_to_do = 0;
 
@@ -438,9 +407,9 @@ static int do_block_io_op(blkif_t *blkif)
  * Transumation of the 'struct blkif_request' to a proper 'struct bio'
  * and call the 'submit_bio' to pass it to the underlaying storage.
  */
-static void dispatch_rw_block_io(blkif_t *blkif,
+static void dispatch_rw_block_io(struct blkif_st *blkif,
                                 struct blkif_request *req,
-                                pending_req_t *pending_req)
+                                struct pending_req *pending_req)
 {
        struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST];
        struct phys_req preq;
@@ -449,8 +418,11 @@ static void dispatch_rw_block_io(blkif_t *blkif,
        } seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
        unsigned int nseg;
        struct bio *bio = NULL;
-       int ret, i;
+       struct bio *biolist[BLKIF_MAX_SEGMENTS_PER_REQUEST];
+       int ret, i, nbio = 0;
        int operation;
+       struct blk_plug plug;
+       struct request_queue *q;
 
        switch (req->operation) {
        case BLKIF_OP_READ:
@@ -487,7 +459,8 @@ static void dispatch_rw_block_io(blkif_t *blkif,
 
        /* Fill out preq.nr_sects with proper amount of sectors, and setup
         * assign map[..] with the PFN of the page in our domain with the
-        * corresponding grant reference for each page.*/
+        * corresponding grant reference for each page.
+        */
        for (i = 0; i < nseg; i++) {
                uint32_t flags;
 
@@ -509,8 +482,9 @@ static void dispatch_rw_block_io(blkif_t *blkif,
        BUG_ON(ret);
 
        /* Now swizzel the MFN in our domain with the MFN from the other domain
-        * so that when we access vaddr(pending_req,i) it has the contents of the
-        * page from the other domain. */
+        * so that when we access vaddr(pending_req,i) it has the contents of
+        * the page from the other domain.
+        */
        for (i = 0; i < nseg; i++) {
                if (unlikely(map[i].status != 0)) {
                        DPRINTK("invalid buffer -- could not remap it\n");
@@ -522,12 +496,13 @@ static void dispatch_rw_block_io(blkif_t *blkif,
 
                if (ret)
                        continue;
-               
+
                ret = m2p_add_override(PFN_DOWN(map[i].dev_bus_addr),
                        blkbk->pending_page(pending_req, i), false);
                if (ret) {
                        printk(KERN_ALERT "Failed to install M2P override for"\
-                               " %lx (ret: %d)\n", (unsigned long)map[i].dev_bus_addr, ret);
+                               " %lx (ret: %d)\n", (unsigned long)
+                               map[i].dev_bus_addr, ret);
                        /* We could switch over to GNTTABOP_copy */
                        continue;
                }
@@ -536,9 +511,11 @@ static void dispatch_rw_block_io(blkif_t *blkif,
                        (req->u.rw.seg[i].first_sect << 9);
        }
 
-       /* If we have failed at this point, we need to undo the M2P override, set
-        * gnttab_set_unmap_op on all of the grant references and perform the
-        * hypercall to unmap the grants - that is all done in fast_flush_area. */
+       /* If we have failed at this point, we need to undo the M2P override,
+        * set gnttab_set_unmap_op on all of the grant references and perform
+        * the hypercall to unmap the grants - that is all done in
+        * fast_flush_area.
+        */
        if (ret)
                goto fail_flush;
 
@@ -550,12 +527,7 @@ static void dispatch_rw_block_io(blkif_t *blkif,
                goto fail_flush;
        }
 
-       /* Get a reference count for the disk queue and start sending I/O */
-       plug_queue(blkif, preq.bdev);
-
-       /* We set it one so that the last submit_bio does not have to call
-        * atomic_inc. */
-       atomic_set(&pending_req->pendcnt, 1);
+       /* This corresponding blkif_put is done in __end_block_io_op */
        blkif_get(blkif);
 
        for (i = 0; i < nseg; i++) {
@@ -571,12 +543,8 @@ static void dispatch_rw_block_io(blkif_t *blkif,
                                     blkbk->pending_page(pending_req, i),
                                     seg[i].nsec << 9,
                                     seg[i].buf & ~PAGE_MASK) == 0)) {
-                       if (bio) {
-                               atomic_inc(&pending_req->pendcnt);
-                               submit_bio(operation, bio);
-                       }
-                        
-                       bio = bio_alloc(GFP_KERNEL, nseg-i);
+
+                       bio = biolist[nbio++] = bio_alloc(GFP_KERNEL, nseg-i);
                        if (unlikely(bio == NULL))
                                goto fail_put_bio;
 
@@ -592,7 +560,7 @@ static void dispatch_rw_block_io(blkif_t *blkif,
        /* This will be hit if the operation was a barrier. */
        if (!bio) {
                BUG_ON(operation != WRITE_BARRIER);
-               bio = bio_alloc(GFP_KERNEL, 0);
+               bio = biolist[nbio++] = bio_alloc(GFP_KERNEL, 0);
                if (unlikely(bio == NULL))
                        goto fail_put_bio;
 
@@ -602,7 +570,22 @@ static void dispatch_rw_block_io(blkif_t *blkif,
                bio->bi_sector  = -1;
        }
 
-       submit_bio(operation, bio);
+
+       /* We set it one so that the last submit_bio does not have to call
+        * atomic_inc.
+        */
+       atomic_set(&pending_req->pendcnt, nbio);
+
+       /* Get a reference count for the disk queue and start sending I/O */
+       blk_get_queue(q);
+       blk_start_plug(&plug);
+
+       for (i = 0; i < nbio; i++)
+               submit_bio(operation, biolist[i]);
+
+       blk_finish_plug(&plug);
+       /* Let the I/Os go.. */
+       blk_put_queue(q);
 
        if (operation == READ)
                blkif->st_rd_sect += preq.nr_sects;
@@ -614,16 +597,16 @@ static void dispatch_rw_block_io(blkif_t *blkif,
  fail_flush:
        fast_flush_area(pending_req);
  fail_response:
+       /* Haven't submitted any bio's yet. */
        make_response(blkif, req->id, req->operation, BLKIF_RSP_ERROR);
        free_req(pending_req);
        msleep(1); /* back off a bit */
        return;
 
  fail_put_bio:
+       for (i = 0; i < (nbio-1); i++)
+               bio_put(biolist[i]);
        __end_block_io_op(pending_req, -EINVAL);
-       if (bio)
-               bio_put(bio);
-       unplug_queue(blkif);
        msleep(1); /* back off a bit */
        return;
 }
@@ -633,7 +616,7 @@ static void dispatch_rw_block_io(blkif_t *blkif,
 /*
  * Put a response on the ring on how the operation fared.
  */
-static void make_response(blkif_t *blkif, u64 id,
+static void make_response(struct blkif_st *blkif, u64 id,
                          unsigned short op, int st)
 {
        struct blkif_response  resp;
@@ -694,7 +677,7 @@ static int __init blkif_init(void)
        if (!xen_pv_domain())
                return -ENODEV;
 
-       blkbk = (struct xen_blkbk *)kzalloc(sizeof(struct xen_blkbk), GFP_KERNEL);
+       blkbk = kzalloc(sizeof(struct xen_blkbk), GFP_KERNEL);
        if (!blkbk) {
                printk(KERN_ALERT "%s: out of memory!\n", __func__);
                return -ENOMEM;
@@ -709,7 +692,8 @@ static int __init blkif_init(void)
        blkbk->pending_pages         = kzalloc(sizeof(blkbk->pending_pages[0]) *
                                        mmap_pages, GFP_KERNEL);
 
-       if (!blkbk->pending_reqs || !blkbk->pending_grant_handles || !blkbk->pending_pages) {
+       if (!blkbk->pending_reqs || !blkbk->pending_grant_handles ||
+           !blkbk->pending_pages) {
                rc = -ENOMEM;
                goto out_of_memory;
        }
@@ -733,7 +717,8 @@ static int __init blkif_init(void)
        init_waitqueue_head(&blkbk->pending_free_wq);
 
        for (i = 0; i < blkif_reqs; i++)
-               list_add_tail(&blkbk->pending_reqs[i].free_list, &blkbk->pending_free);
+               list_add_tail(&blkbk->pending_reqs[i].free_list,
+                             &blkbk->pending_free);
 
        rc = blkif_xenbus_init();
        if (rc)