block: change the tag sync vs async restriction logic
[safe/jmp/linux-2.6] / block / blk-core.c
index 6dd180c..1c74840 100644 (file)
@@ -68,11 +68,11 @@ static void drive_stat_acct(struct request *rq, int new_io)
        int rw = rq_data_dir(rq);
        int cpu;
 
-       if (!blk_fs_request(rq) || !blk_do_io_stat(rq))
+       if (!blk_do_io_stat(rq))
                return;
 
        cpu = part_stat_lock();
-       part = disk_map_sector_rcu(rq->rq_disk, rq->sector);
+       part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq));
 
        if (!new_io)
                part_stat_inc(cpu, part, merges[rw]);
@@ -127,7 +127,7 @@ void blk_rq_init(struct request_queue *q, struct request *rq)
        INIT_LIST_HEAD(&rq->timeout_list);
        rq->cpu = -1;
        rq->q = q;
-       rq->sector = rq->hard_sector = (sector_t) -1;
+       rq->__sector = (sector_t) -1;
        INIT_HLIST_NODE(&rq->hash);
        RB_CLEAR_NODE(&rq->rb_node);
        rq->cmd = rq->__cmd;
@@ -185,13 +185,11 @@ void blk_dump_rq_flags(struct request *rq, char *msg)
                rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
                rq->cmd_flags);
 
-       printk(KERN_INFO "  sector %llu, nr/cnr %lu/%u\n",
-                                               (unsigned long long)rq->sector,
-                                               rq->nr_sectors,
-                                               rq->current_nr_sectors);
+       printk(KERN_INFO "  sector %llu, nr/cnr %u/%u\n",
+              (unsigned long long)blk_rq_pos(rq),
+              blk_rq_sectors(rq), blk_rq_cur_sectors(rq));
        printk(KERN_INFO "  bio %p, biotail %p, buffer %p, len %u\n",
-                                               rq->bio, rq->biotail,
-                                               rq->buffer, rq->data_len);
+              rq->bio, rq->biotail, rq->buffer, blk_rq_bytes(rq));
 
        if (blk_pc_request(rq)) {
                printk(KERN_INFO "  cdb: ");
@@ -893,6 +891,60 @@ struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
 EXPORT_SYMBOL(blk_get_request);
 
 /**
+ * blk_make_request - given a bio, allocate a corresponding struct request.
+ *
+ * @bio:  The bio describing the memory mappings that will be submitted for IO.
+ *        It may be a chained-bio properly constructed by block/bio layer.
+ *
+ * blk_make_request is the parallel of generic_make_request for BLOCK_PC
+ * type commands. Where the struct request needs to be farther initialized by
+ * the caller. It is passed a &struct bio, which describes the memory info of
+ * the I/O transfer.
+ *
+ * The caller of blk_make_request must make sure that bi_io_vec
+ * are set to describe the memory buffers. That bio_data_dir() will return
+ * the needed direction of the request. (And all bio's in the passed bio-chain
+ * are properly set accordingly)
+ *
+ * If called under none-sleepable conditions, mapped bio buffers must not
+ * need bouncing, by calling the appropriate masked or flagged allocator,
+ * suitable for the target device. Otherwise the call to blk_queue_bounce will
+ * BUG.
+ *
+ * WARNING: When allocating/cloning a bio-chain, careful consideration should be
+ * given to how you allocate bios. In particular, you cannot use __GFP_WAIT for
+ * anything but the first bio in the chain. Otherwise you risk waiting for IO
+ * completion of a bio that hasn't been submitted yet, thus resulting in a
+ * deadlock. Alternatively bios should be allocated using bio_kmalloc() instead
+ * of bio_alloc(), as that avoids the mempool deadlock.
+ * If possible a big IO should be split into smaller parts when allocation
+ * fails. Partial allocation should not be an error, or you risk a live-lock.
+ */
+struct request *blk_make_request(struct request_queue *q, struct bio *bio,
+                                gfp_t gfp_mask)
+{
+       struct request *rq = blk_get_request(q, bio_data_dir(bio), gfp_mask);
+
+       if (unlikely(!rq))
+               return ERR_PTR(-ENOMEM);
+
+       for_each_bio(bio) {
+               struct bio *bounce_bio = bio;
+               int ret;
+
+               blk_queue_bounce(q, &bounce_bio);
+               ret = blk_rq_append_bio(q, rq, bounce_bio);
+               if (unlikely(ret)) {
+                       blk_put_request(rq);
+                       return ERR_PTR(ret);
+               }
+       }
+
+       return rq;
+}
+EXPORT_SYMBOL(blk_make_request);
+
+/**
  * blk_requeue_request - put a request back on queue
  * @q:         request queue where request should be inserted
  * @rq:                request to be inserted
@@ -904,6 +956,8 @@ EXPORT_SYMBOL(blk_get_request);
  */
 void blk_requeue_request(struct request_queue *q, struct request *rq)
 {
+       BUG_ON(blk_queued_rq(rq));
+
        blk_delete_timer(rq);
        blk_clear_rq_complete(rq);
        trace_block_rq_requeue(q, rq);
@@ -1097,7 +1151,7 @@ void init_request_from_bio(struct request *req, struct bio *bio)
                req->cmd_flags |= REQ_NOIDLE;
 
        req->errors = 0;
-       req->hard_sector = req->sector = bio->bi_sector;
+       req->__sector = bio->bi_sector;
        req->ioprio = bio_prio(bio);
        blk_rq_bio_prep(req->q, req, bio);
 }
@@ -1114,14 +1168,13 @@ static inline bool queue_should_plug(struct request_queue *q)
 static int __make_request(struct request_queue *q, struct bio *bio)
 {
        struct request *req;
-       int el_ret, nr_sectors;
+       int el_ret;
+       unsigned int bytes = bio->bi_size;
        const unsigned short prio = bio_prio(bio);
        const int sync = bio_sync(bio);
        const int unplug = bio_unplug(bio);
        int rw_flags;
 
-       nr_sectors = bio_sectors(bio);
-
        /*
         * low level driver can indicate that it wants pages above a
         * certain limit bounced to low memory (ie for highmem, or even
@@ -1146,7 +1199,7 @@ static int __make_request(struct request_queue *q, struct bio *bio)
 
                req->biotail->bi_next = bio;
                req->biotail = bio;
-               req->nr_sectors = req->hard_nr_sectors += nr_sectors;
+               req->__data_len += bytes;
                req->ioprio = ioprio_best(req->ioprio, prio);
                if (!blk_rq_cpu_valid(req))
                        req->cpu = bio->bi_comp_cpu;
@@ -1172,10 +1225,8 @@ static int __make_request(struct request_queue *q, struct bio *bio)
                 * not touch req->buffer either...
                 */
                req->buffer = bio_data(bio);
-               req->current_nr_sectors = bio_cur_sectors(bio);
-               req->hard_cur_sectors = req->current_nr_sectors;
-               req->sector = req->hard_sector = bio->bi_sector;
-               req->nr_sectors = req->hard_nr_sectors += nr_sectors;
+               req->__sector = bio->bi_sector;
+               req->__data_len += bytes;
                req->ioprio = ioprio_best(req->ioprio, prio);
                if (!blk_rq_cpu_valid(req))
                        req->cpu = bio->bi_comp_cpu;
@@ -1557,8 +1608,8 @@ EXPORT_SYMBOL(submit_bio);
  */
 int blk_rq_check_limits(struct request_queue *q, struct request *rq)
 {
-       if (rq->nr_sectors > q->max_sectors ||
-           rq->data_len > q->max_hw_sectors << 9) {
+       if (blk_rq_sectors(rq) > q->max_sectors ||
+           blk_rq_bytes(rq) > q->max_hw_sectors << 9) {
                printk(KERN_ERR "%s: over max size limit.\n", __func__);
                return -EIO;
        }
@@ -1615,40 +1666,15 @@ int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
 }
 EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
 
-/**
- * blkdev_dequeue_request - dequeue request and start timeout timer
- * @req: request to dequeue
- *
- * Dequeue @req and start timeout timer on it.  This hands off the
- * request to the driver.
- *
- * Block internal functions which don't want to start timer should
- * call elv_dequeue_request().
- */
-void blkdev_dequeue_request(struct request *req)
-{
-       elv_dequeue_request(req->q, req);
-
-       /*
-        * We are now handing the request to the hardware, add the
-        * timeout handler.
-        */
-       blk_add_timer(req);
-}
-EXPORT_SYMBOL(blkdev_dequeue_request);
-
 static void blk_account_io_completion(struct request *req, unsigned int bytes)
 {
-       if (!blk_do_io_stat(req))
-               return;
-
-       if (blk_fs_request(req)) {
+       if (blk_do_io_stat(req)) {
                const int rw = rq_data_dir(req);
                struct hd_struct *part;
                int cpu;
 
                cpu = part_stat_lock();
-               part = disk_map_sector_rcu(req->rq_disk, req->sector);
+               part = disk_map_sector_rcu(req->rq_disk, blk_rq_pos(req));
                part_stat_add(cpu, part, sectors[rw], bytes >> 9);
                part_stat_unlock();
        }
@@ -1656,22 +1682,19 @@ static void blk_account_io_completion(struct request *req, unsigned int bytes)
 
 static void blk_account_io_done(struct request *req)
 {
-       if (!blk_do_io_stat(req))
-               return;
-
        /*
         * Account IO completion.  bar_rq isn't accounted as a normal
         * IO on queueing nor completion.  Accounting the containing
         * request is enough.
         */
-       if (blk_fs_request(req) && req != &req->q->bar_rq) {
+       if (blk_do_io_stat(req) && req != &req->q->bar_rq) {
                unsigned long duration = jiffies - req->start_time;
                const int rw = rq_data_dir(req);
                struct hd_struct *part;
                int cpu;
 
                cpu = part_stat_lock();
-               part = disk_map_sector_rcu(req->rq_disk, req->sector);
+               part = disk_map_sector_rcu(req->rq_disk, blk_rq_pos(req));
 
                part_stat_inc(cpu, part, ios[rw]);
                part_stat_add(cpu, part, ticks[rw], duration);
@@ -1683,35 +1706,22 @@ static void blk_account_io_done(struct request *req)
 }
 
 /**
- * blk_rq_bytes - Returns bytes left to complete in the entire request
- * @rq: the request being processed
- **/
-unsigned int blk_rq_bytes(struct request *rq)
-{
-       if (blk_fs_request(rq))
-               return rq->hard_nr_sectors << 9;
-
-       return rq->data_len;
-}
-EXPORT_SYMBOL_GPL(blk_rq_bytes);
-
-/**
- * blk_rq_cur_bytes - Returns bytes left to complete in the current segment
- * @rq: the request being processed
- **/
-unsigned int blk_rq_cur_bytes(struct request *rq)
-{
-       if (blk_fs_request(rq))
-               return rq->current_nr_sectors << 9;
-
-       if (rq->bio)
-               return rq->bio->bi_size;
-
-       return rq->data_len;
-}
-EXPORT_SYMBOL_GPL(blk_rq_cur_bytes);
-
-struct request *elv_next_request(struct request_queue *q)
+ * blk_peek_request - peek at the top of a request queue
+ * @q: request queue to peek at
+ *
+ * Description:
+ *     Return the request at the top of @q.  The returned request
+ *     should be started using blk_start_request() before LLD starts
+ *     processing it.
+ *
+ * Return:
+ *     Pointer to the request at the top of @q if available.  Null
+ *     otherwise.
+ *
+ * Context:
+ *     queue_lock must be held.
+ */
+struct request *blk_peek_request(struct request_queue *q)
 {
        struct request *rq;
        int ret;
@@ -1743,7 +1753,7 @@ struct request *elv_next_request(struct request_queue *q)
                if (rq->cmd_flags & REQ_DONTPREP)
                        break;
 
-               if (q->dma_drain_size && rq->data_len) {
+               if (q->dma_drain_size && blk_rq_bytes(rq)) {
                        /*
                         * make sure space for the drain appears we
                         * know we can do this because max_hw_segments
@@ -1766,7 +1776,7 @@ struct request *elv_next_request(struct request_queue *q)
                         * avoid resource deadlock.  REQ_STARTED will
                         * prevent other fs requests from passing this one.
                         */
-                       if (q->dma_drain_size && rq->data_len &&
+                       if (q->dma_drain_size && blk_rq_bytes(rq) &&
                            !(rq->cmd_flags & REQ_DONTPREP)) {
                                /*
                                 * remove the space for the drain we added
@@ -1788,10 +1798,12 @@ struct request *elv_next_request(struct request_queue *q)
 
        return rq;
 }
-EXPORT_SYMBOL(elv_next_request);
+EXPORT_SYMBOL(blk_peek_request);
 
-void elv_dequeue_request(struct request_queue *q, struct request *rq)
+void blk_dequeue_request(struct request *rq)
 {
+       struct request_queue *q = rq->q;
+
        BUG_ON(list_empty(&rq->queuelist));
        BUG_ON(ELV_ON_HASH(rq));
 
@@ -1803,8 +1815,61 @@ void elv_dequeue_request(struct request_queue *q, struct request *rq)
         * the driver side.
         */
        if (blk_account_rq(rq))
-               q->in_flight++;
+               q->in_flight[rq_is_sync(rq)]++;
+}
+
+/**
+ * blk_start_request - start request processing on the driver
+ * @req: request to dequeue
+ *
+ * Description:
+ *     Dequeue @req and start timeout timer on it.  This hands off the
+ *     request to the driver.
+ *
+ *     Block internal functions which don't want to start timer should
+ *     call blk_dequeue_request().
+ *
+ * Context:
+ *     queue_lock must be held.
+ */
+void blk_start_request(struct request *req)
+{
+       blk_dequeue_request(req);
+
+       /*
+        * 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);
+       blk_add_timer(req);
 }
+EXPORT_SYMBOL(blk_start_request);
+
+/**
+ * blk_fetch_request - fetch a request from a request queue
+ * @q: request queue to fetch a request from
+ *
+ * Description:
+ *     Return the request at the top of @q.  The request is started on
+ *     return and LLD can start processing it immediately.
+ *
+ * Return:
+ *     Pointer to the request at the top of @q if available.  Null
+ *     otherwise.
+ *
+ * Context:
+ *     queue_lock must be held.
+ */
+struct request *blk_fetch_request(struct request_queue *q)
+{
+       struct request *rq;
+
+       rq = blk_peek_request(q);
+       if (rq)
+               blk_start_request(rq);
+       return rq;
+}
+EXPORT_SYMBOL(blk_fetch_request);
 
 /**
  * blk_update_request - Special helper function for request stacking drivers
@@ -1852,7 +1917,7 @@ bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
        if (error && (blk_fs_request(req) && !(req->cmd_flags & REQ_QUIET))) {
                printk(KERN_ERR "end_request: I/O error, dev %s, sector %llu\n",
                                req->rq_disk ? req->rq_disk->disk_name : "?",
-                               (unsigned long long)req->sector);
+                               (unsigned long long)blk_rq_pos(req));
        }
 
        blk_account_io_completion(req, nr_bytes);
@@ -1918,8 +1983,7 @@ bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
                 * can find how many bytes remain in the request
                 * later.
                 */
-               req->nr_sectors = req->hard_nr_sectors = 0;
-               req->current_nr_sectors = req->hard_cur_sectors = 0;
+               req->__data_len = 0;
                return false;
        }
 
@@ -1933,8 +1997,25 @@ bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
                bio_iovec(bio)->bv_len -= nr_bytes;
        }
 
-       blk_recalc_rq_sectors(req, total_bytes >> 9);
+       req->__data_len -= total_bytes;
+       req->buffer = bio_data(req->bio);
+
+       /* update sector only for requests with clear definition of sector */
+       if (blk_fs_request(req) || blk_discard_rq(req))
+               req->__sector += total_bytes >> 9;
+
+       /*
+        * If total number of sectors is less than the first segment
+        * size, something has gone terribly wrong.
+        */
+       if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
+               printk(KERN_ERR "blk: request botched\n");
+               req->__data_len = blk_rq_cur_bytes(req);
+       }
+
+       /* recalculate the number of segments */
        blk_recalc_rq_segments(req);
+
        return true;
 }
 EXPORT_SYMBOL_GPL(blk_update_request);
@@ -1961,12 +2042,11 @@ static bool blk_update_bidi_request(struct request *rq, int error,
  */
 static void blk_finish_request(struct request *req, int error)
 {
+       BUG_ON(blk_queued_rq(req));
+
        if (blk_rq_tagged(req))
                blk_queue_end_tag(req->q, req);
 
-       if (blk_queued_rq(req))
-               elv_dequeue_request(req->q, req);
-
        if (unlikely(laptop_mode) && blk_fs_request(req))
                laptop_io_completion();
 
@@ -2001,8 +2081,8 @@ static void blk_finish_request(struct request *req, int error)
  *     %false - we are done with this request
  *     %true  - still buffers pending for this request
  **/
-bool blk_end_bidi_request(struct request *rq, int error,
-                         unsigned int nr_bytes, unsigned int bidi_bytes)
+static bool blk_end_bidi_request(struct request *rq, int error,
+                                unsigned int nr_bytes, unsigned int bidi_bytes)
 {
        struct request_queue *q = rq->q;
        unsigned long flags;
@@ -2016,7 +2096,6 @@ bool blk_end_bidi_request(struct request *rq, int error,
 
        return false;
 }
-EXPORT_SYMBOL_GPL(blk_end_bidi_request);
 
 /**
  * __blk_end_bidi_request - Complete a bidi request with queue lock held
@@ -2033,8 +2112,8 @@ EXPORT_SYMBOL_GPL(blk_end_bidi_request);
  *     %false - we are done with this request
  *     %true  - still buffers pending for this request
  **/
-bool __blk_end_bidi_request(struct request *rq, int error,
-                           unsigned int nr_bytes, unsigned int bidi_bytes)
+static bool __blk_end_bidi_request(struct request *rq, int error,
+                                  unsigned int nr_bytes, unsigned int bidi_bytes)
 {
        if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
                return true;
@@ -2043,7 +2122,124 @@ bool __blk_end_bidi_request(struct request *rq, int error,
 
        return false;
 }
-EXPORT_SYMBOL_GPL(__blk_end_bidi_request);
+
+/**
+ * blk_end_request - Helper function for drivers to complete the request.
+ * @rq:       the request being processed
+ * @error:    %0 for success, < %0 for error
+ * @nr_bytes: number of bytes to complete
+ *
+ * Description:
+ *     Ends I/O on a number of bytes attached to @rq.
+ *     If @rq has leftover, sets it up for the next range of segments.
+ *
+ * Return:
+ *     %false - we are done with this request
+ *     %true  - still buffers pending for this request
+ **/
+bool blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
+{
+       return blk_end_bidi_request(rq, error, nr_bytes, 0);
+}
+EXPORT_SYMBOL_GPL(blk_end_request);
+
+/**
+ * blk_end_request_all - Helper function for drives to finish the request.
+ * @rq: the request to finish
+ * @err: %0 for success, < %0 for error
+ *
+ * Description:
+ *     Completely finish @rq.
+ */
+void blk_end_request_all(struct request *rq, int error)
+{
+       bool pending;
+       unsigned int bidi_bytes = 0;
+
+       if (unlikely(blk_bidi_rq(rq)))
+               bidi_bytes = blk_rq_bytes(rq->next_rq);
+
+       pending = blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
+       BUG_ON(pending);
+}
+EXPORT_SYMBOL_GPL(blk_end_request_all);
+
+/**
+ * blk_end_request_cur - Helper function to finish the current request chunk.
+ * @rq: the request to finish the current chunk for
+ * @err: %0 for success, < %0 for error
+ *
+ * Description:
+ *     Complete the current consecutively mapped chunk from @rq.
+ *
+ * Return:
+ *     %false - we are done with this request
+ *     %true  - still buffers pending for this request
+ */
+bool blk_end_request_cur(struct request *rq, int error)
+{
+       return blk_end_request(rq, error, blk_rq_cur_bytes(rq));
+}
+EXPORT_SYMBOL_GPL(blk_end_request_cur);
+
+/**
+ * __blk_end_request - Helper function for drivers to complete the request.
+ * @rq:       the request being processed
+ * @error:    %0 for success, < %0 for error
+ * @nr_bytes: number of bytes to complete
+ *
+ * Description:
+ *     Must be called with queue lock held unlike blk_end_request().
+ *
+ * Return:
+ *     %false - we are done with this request
+ *     %true  - still buffers pending for this request
+ **/
+bool __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
+{
+       return __blk_end_bidi_request(rq, error, nr_bytes, 0);
+}
+EXPORT_SYMBOL_GPL(__blk_end_request);
+
+/**
+ * __blk_end_request_all - Helper function for drives to finish the request.
+ * @rq: the request to finish
+ * @err: %0 for success, < %0 for error
+ *
+ * Description:
+ *     Completely finish @rq.  Must be called with queue lock held.
+ */
+void __blk_end_request_all(struct request *rq, int error)
+{
+       bool pending;
+       unsigned int bidi_bytes = 0;
+
+       if (unlikely(blk_bidi_rq(rq)))
+               bidi_bytes = blk_rq_bytes(rq->next_rq);
+
+       pending = __blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
+       BUG_ON(pending);
+}
+EXPORT_SYMBOL_GPL(__blk_end_request_all);
+
+/**
+ * __blk_end_request_cur - Helper function to finish the current request chunk.
+ * @rq: the request to finish the current chunk for
+ * @err: %0 for success, < %0 for error
+ *
+ * Description:
+ *     Complete the current consecutively mapped chunk from @rq.  Must
+ *     be called with queue lock held.
+ *
+ * Return:
+ *     %false - we are done with this request
+ *     %true  - still buffers pending for this request
+ */
+bool __blk_end_request_cur(struct request *rq, int error)
+{
+       return __blk_end_request(rq, error, blk_rq_cur_bytes(rq));
+}
+EXPORT_SYMBOL_GPL(__blk_end_request_cur);
 
 void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
                     struct bio *bio)
@@ -2056,11 +2252,7 @@ void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
                rq->nr_phys_segments = bio_phys_segments(q, bio);
                rq->buffer = bio_data(bio);
        }
-       rq->current_nr_sectors = bio_cur_sectors(bio);
-       rq->hard_cur_sectors = rq->current_nr_sectors;
-       rq->hard_nr_sectors = rq->nr_sectors = bio_sectors(bio);
-       rq->data_len = bio->bi_size;
-
+       rq->__data_len = bio->bi_size;
        rq->bio = rq->biotail = bio;
 
        if (bio->bi_bdev)
@@ -2103,6 +2295,9 @@ EXPORT_SYMBOL(kblockd_schedule_work);
 
 int __init blk_dev_init(void)
 {
+       BUILD_BUG_ON(__REQ_NR_BITS > 8 *
+                       sizeof(((struct request *)0)->cmd_flags));
+
        kblockd_workqueue = create_workqueue("kblockd");
        if (!kblockd_workqueue)
                panic("Failed to create kblockd\n");