block: implement and enforce request peek/start/fetch
[safe/jmp/linux-2.6] / include / linux / blkdev.h
index 3c7078e..6e59d3b 100644 (file)
@@ -26,7 +26,6 @@ struct scsi_ioctl_command;
 
 struct request_queue;
 struct elevator_queue;
-typedef struct elevator_queue elevator_t;
 struct request_pm_state;
 struct blk_trace;
 struct request;
@@ -39,6 +38,10 @@ struct request;
 typedef void (rq_end_io_fn)(struct request *, int);
 
 struct request_list {
+       /*
+        * count[], starved[], and wait[] are indexed by
+        * BLK_RW_SYNC/BLK_RW_ASYNC
+        */
        int count[2];
        int starved[2];
        int elvpriv;
@@ -67,6 +70,11 @@ enum rq_cmd_type_bits {
        REQ_TYPE_ATA_PC,
 };
 
+enum {
+       BLK_RW_ASYNC    = 0,
+       BLK_RW_SYNC     = 1,
+};
+
 /*
  * For request of type REQ_TYPE_LINUX_BLOCK, rq->cmd[0] is the opcode being
  * sent down (similar to how REQ_TYPE_BLOCK_PC means that ->cmd[] holds a
@@ -104,11 +112,13 @@ enum rq_flag_bits {
        __REQ_QUIET,            /* don't worry about errors */
        __REQ_PREEMPT,          /* set for "ide_preempt" requests */
        __REQ_ORDERED_COLOR,    /* is before or after barrier */
-       __REQ_RW_SYNC,          /* request is sync (O_DIRECT) */
+       __REQ_RW_SYNC,          /* request is sync (sync write or read) */
        __REQ_ALLOCED,          /* request came from our alloc pool */
        __REQ_RW_META,          /* metadata io request */
        __REQ_COPY_USER,        /* contains copies of user pages */
        __REQ_INTEGRITY,        /* integrity metadata has been remapped */
+       __REQ_NOIDLE,           /* Don't anticipate more IO after this one */
+       __REQ_IO_STAT,          /* account I/O stat */
        __REQ_NR_BITS,          /* stops here */
 };
 
@@ -135,6 +145,8 @@ enum rq_flag_bits {
 #define REQ_RW_META    (1 << __REQ_RW_META)
 #define REQ_COPY_USER  (1 << __REQ_COPY_USER)
 #define REQ_INTEGRITY  (1 << __REQ_INTEGRITY)
+#define REQ_NOIDLE     (1 << __REQ_NOIDLE)
+#define REQ_IO_STAT    (1 << __REQ_IO_STAT)
 
 #define BLK_MAX_CDB    16
 
@@ -154,19 +166,9 @@ struct request {
        enum rq_cmd_type_bits cmd_type;
        unsigned long atomic_flags;
 
-       /* Maintain bio traversal state for part by part I/O submission.
-        * hard_* are block layer internals, no driver should touch them!
-        */
-
-       sector_t sector;                /* next sector to submit */
-       sector_t hard_sector;           /* next sector to complete */
-       unsigned long nr_sectors;       /* no. of sectors left to submit */
-       unsigned long hard_nr_sectors;  /* no. of sectors left to complete */
-       /* no. of sectors left to submit in the current segment */
-       unsigned int current_nr_sectors;
-
-       /* no. of sectors left to complete in the current segment */
-       unsigned int hard_cur_sectors;
+       /* the following two fields are internal, NEVER access directly */
+       sector_t __sector;              /* sector cursor */
+       unsigned int __data_len;        /* total data len */
 
        struct bio *bio;
        struct bio *biotail;
@@ -199,8 +201,8 @@ struct request {
 
        unsigned short ioprio;
 
-       void *special;
-       char *buffer;
+       void *special;          /* opaque pointer available for LLD use */
+       char *buffer;           /* kaddr of the current segment if available */
 
        int tag;
        int errors;
@@ -214,10 +216,9 @@ struct request {
        unsigned char __cmd[BLK_MAX_CDB];
        unsigned char *cmd;
 
-       unsigned int data_len;
        unsigned int extra_len; /* length of alignment and padding */
        unsigned int sense_len;
-       void *data;
+       unsigned int resid_len; /* residual count */
        void *sense;
 
        unsigned long deadline;
@@ -313,7 +314,7 @@ struct request_queue
         */
        struct list_head        queue_head;
        struct request          *last_merge;
-       elevator_t              *elevator;
+       struct elevator_queue   *elevator;
 
        /*
         * the queue request freelist, one for reads and one for writes
@@ -437,8 +438,8 @@ struct request_queue
 #define QUEUE_FLAG_CLUSTER     0       /* cluster several segments into 1 */
 #define QUEUE_FLAG_QUEUED      1       /* uses generic tag queueing */
 #define QUEUE_FLAG_STOPPED     2       /* queue is stopped */
-#define        QUEUE_FLAG_READFULL     3       /* read queue has been filled */
-#define QUEUE_FLAG_WRITEFULL   4       /* write queue has been filled */
+#define        QUEUE_FLAG_SYNCFULL     3       /* read queue has been filled */
+#define QUEUE_FLAG_ASYNCFULL   4       /* write queue has been filled */
 #define QUEUE_FLAG_DEAD                5       /* queue being torn down */
 #define QUEUE_FLAG_REENTER     6       /* Re-entrancy avoidance */
 #define QUEUE_FLAG_PLUGGED     7       /* queue is plugged */
@@ -450,6 +451,11 @@ struct request_queue
 #define QUEUE_FLAG_STACKABLE   13      /* supports request stacking */
 #define QUEUE_FLAG_NONROT      14      /* non-rotational device (SSD) */
 #define QUEUE_FLAG_VIRT        QUEUE_FLAG_NONROT /* paravirt device */
+#define QUEUE_FLAG_IO_STAT     15      /* do IO stats */
+
+#define QUEUE_FLAG_DEFAULT     ((1 << QUEUE_FLAG_IO_STAT) |            \
+                                (1 << QUEUE_FLAG_CLUSTER) |            \
+                                (1 << QUEUE_FLAG_STACKABLE))
 
 static inline int queue_is_locked(struct request_queue *q)
 {
@@ -566,6 +572,7 @@ enum {
 #define blk_queue_stopped(q)   test_bit(QUEUE_FLAG_STOPPED, &(q)->queue_flags)
 #define blk_queue_nomerges(q)  test_bit(QUEUE_FLAG_NOMERGES, &(q)->queue_flags)
 #define blk_queue_nonrot(q)    test_bit(QUEUE_FLAG_NONROT, &(q)->queue_flags)
+#define blk_queue_io_stat(q)   test_bit(QUEUE_FLAG_IO_STAT, &(q)->queue_flags)
 #define blk_queue_flushing(q)  ((q)->ordseq)
 #define blk_queue_stackable(q) \
        test_bit(QUEUE_FLAG_STACKABLE, &(q)->queue_flags)
@@ -582,6 +589,7 @@ enum {
                                 blk_failfast_transport(rq) ||  \
                                 blk_failfast_driver(rq))
 #define blk_rq_started(rq)     ((rq)->cmd_flags & REQ_STARTED)
+#define blk_rq_io_stat(rq)     ((rq)->cmd_flags & REQ_IO_STAT)
 
 #define blk_account_rq(rq)     (blk_rq_started(rq) && (blk_fs_request(rq) || blk_discard_rq(rq))) 
 
@@ -596,7 +604,6 @@ enum {
 #define blk_fua_rq(rq)         ((rq)->cmd_flags & REQ_FUA)
 #define blk_discard_rq(rq)     ((rq)->cmd_flags & REQ_DISCARD)
 #define blk_bidi_rq(rq)                ((rq)->next_rq != NULL)
-#define blk_empty_barrier(rq)  (blk_barrier_rq(rq) && blk_fs_request(rq) && !(rq)->hard_nr_sectors)
 /* rq->queuelist of dequeued request must be list_empty() */
 #define blk_queued_rq(rq)      (!list_empty(&(rq)->queuelist))
 
@@ -605,32 +612,42 @@ enum {
 #define rq_data_dir(rq)                ((rq)->cmd_flags & 1)
 
 /*
- * We regard a request as sync, if it's a READ or a SYNC write.
+ * We regard a request as sync, if either a read or a sync write
  */
-#define rq_is_sync(rq)         (rq_data_dir((rq)) == READ || (rq)->cmd_flags & REQ_RW_SYNC)
+static inline bool rw_is_sync(unsigned int rw_flags)
+{
+       return !(rw_flags & REQ_RW) || (rw_flags & REQ_RW_SYNC);
+}
+
+static inline bool rq_is_sync(struct request *rq)
+{
+       return rw_is_sync(rq->cmd_flags);
+}
+
 #define rq_is_meta(rq)         ((rq)->cmd_flags & REQ_RW_META)
+#define rq_noidle(rq)          ((rq)->cmd_flags & REQ_NOIDLE)
 
-static inline int blk_queue_full(struct request_queue *q, int rw)
+static inline int blk_queue_full(struct request_queue *q, int sync)
 {
-       if (rw == READ)
-               return test_bit(QUEUE_FLAG_READFULL, &q->queue_flags);
-       return test_bit(QUEUE_FLAG_WRITEFULL, &q->queue_flags);
+       if (sync)
+               return test_bit(QUEUE_FLAG_SYNCFULL, &q->queue_flags);
+       return test_bit(QUEUE_FLAG_ASYNCFULL, &q->queue_flags);
 }
 
-static inline void blk_set_queue_full(struct request_queue *q, int rw)
+static inline void blk_set_queue_full(struct request_queue *q, int sync)
 {
-       if (rw == READ)
-               queue_flag_set(QUEUE_FLAG_READFULL, q);
+       if (sync)
+               queue_flag_set(QUEUE_FLAG_SYNCFULL, q);
        else
-               queue_flag_set(QUEUE_FLAG_WRITEFULL, q);
+               queue_flag_set(QUEUE_FLAG_ASYNCFULL, q);
 }
 
-static inline void blk_clear_queue_full(struct request_queue *q, int rw)
+static inline void blk_clear_queue_full(struct request_queue *q, int sync)
 {
-       if (rw == READ)
-               queue_flag_clear(QUEUE_FLAG_READFULL, q);
+       if (sync)
+               queue_flag_clear(QUEUE_FLAG_SYNCFULL, q);
        else
-               queue_flag_clear(QUEUE_FLAG_WRITEFULL, q);
+               queue_flag_clear(QUEUE_FLAG_ASYNCFULL, q);
 }
 
 
@@ -692,6 +709,8 @@ struct rq_map_data {
        struct page **pages;
        int page_order;
        int nr_entries;
+       unsigned long offset;
+       int null_mapped;
 };
 
 struct req_iterator {
@@ -700,6 +719,8 @@ struct req_iterator {
 };
 
 /* This should not be used directly - use rq_for_each_segment */
+#define for_each_bio(_bio)             \
+       for (; _bio; _bio = _bio->bi_next)
 #define __rq_for_each_bio(_bio, rq)    \
        if ((rq->bio))                  \
                for (_bio = (rq)->bio; _bio; _bio = _bio->bi_next)
@@ -765,7 +786,6 @@ extern void blk_sync_queue(struct request_queue *q);
 extern void __blk_stop_queue(struct request_queue *q);
 extern void __blk_run_queue(struct request_queue *);
 extern void blk_run_queue(struct request_queue *);
-extern void blk_start_queueing(struct request_queue *);
 extern int blk_rq_map_user(struct request_queue *, struct request *,
                           struct rq_map_data *, void __user *, unsigned long,
                           gfp_t);
@@ -798,41 +818,177 @@ static inline void blk_run_address_space(struct address_space *mapping)
                blk_run_backing_dev(mapping->backing_dev_info, NULL);
 }
 
-extern void blkdev_dequeue_request(struct request *req);
+/*
+ * blk_rq_pos()                : the current sector
+ * blk_rq_bytes()      : bytes left in the entire request
+ * blk_rq_cur_bytes()  : bytes left in the current segment
+ * blk_rq_sectors()    : sectors left in the entire request
+ * blk_rq_cur_sectors()        : sectors left in the current segment
+ */
+static inline sector_t blk_rq_pos(const struct request *rq)
+{
+       return rq->__sector;
+}
+
+static inline unsigned int blk_rq_bytes(const struct request *rq)
+{
+       return rq->__data_len;
+}
+
+static inline int blk_rq_cur_bytes(const struct request *rq)
+{
+       return rq->bio ? bio_cur_bytes(rq->bio) : 0;
+}
+
+static inline unsigned int blk_rq_sectors(const struct request *rq)
+{
+       return blk_rq_bytes(rq) >> 9;
+}
+
+static inline unsigned int blk_rq_cur_sectors(const struct request *rq)
+{
+       return blk_rq_cur_bytes(rq) >> 9;
+}
+
+/*
+ * Request issue related functions.
+ */
+extern struct request *blk_peek_request(struct request_queue *q);
+extern void blk_start_request(struct request *rq);
+extern struct request *blk_fetch_request(struct request_queue *q);
 
 /*
- * blk_end_request() and friends.
- * __blk_end_request() and end_request() must be called with
- * the request queue spinlock acquired.
+ * Request completion related functions.
+ *
+ * blk_update_request() completes given number of bytes and updates
+ * the request without completing it.
+ *
+ * blk_end_request() and friends.  __blk_end_request() must be called
+ * with the request queue spinlock acquired.
  *
  * Several drivers define their own end_request and call
  * blk_end_request() for parts of the original function.
  * This prevents code duplication in drivers.
  */
-extern int blk_end_request(struct request *rq, int error,
-                               unsigned int nr_bytes);
-extern int __blk_end_request(struct request *rq, int error,
-                               unsigned int nr_bytes);
-extern int blk_end_bidi_request(struct request *rq, int error,
-                               unsigned int nr_bytes, unsigned int bidi_bytes);
-extern void end_request(struct request *, int);
-extern int blk_end_request_callback(struct request *rq, int error,
-                               unsigned int nr_bytes,
-                               int (drv_callback)(struct request *));
+extern bool blk_update_request(struct request *rq, int error,
+                              unsigned int nr_bytes);
+extern bool blk_end_bidi_request(struct request *rq, int error,
+                                unsigned int nr_bytes,
+                                unsigned int bidi_bytes);
+extern bool __blk_end_bidi_request(struct request *rq, int error,
+                                  unsigned int nr_bytes,
+                                  unsigned int bidi_bytes);
+
+/**
+ * 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
+ **/
+static inline bool blk_end_request(struct request *rq, int error,
+                                  unsigned int nr_bytes)
+{
+       return blk_end_bidi_request(rq, error, nr_bytes, 0);
+}
+
+/**
+ * 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.
+ */
+static inline void blk_end_request_all(struct request *rq, int error)
+{
+       bool pending;
+
+       pending = blk_end_request(rq, error, blk_rq_bytes(rq));
+       BUG_ON(pending);
+}
+
+/**
+ * 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
+ */
+static inline bool blk_end_request_cur(struct request *rq, int error)
+{
+       return blk_end_request(rq, error, blk_rq_cur_bytes(rq));
+}
+
+/**
+ * __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
+ **/
+static inline bool __blk_end_request(struct request *rq, int error,
+                                    unsigned int nr_bytes)
+{
+       return __blk_end_bidi_request(rq, error, nr_bytes, 0);
+}
+
+/**
+ * __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.
+ */
+static inline void __blk_end_request_all(struct request *rq, int error)
+{
+       bool pending;
+
+       pending = __blk_end_request(rq, error, blk_rq_bytes(rq));
+       BUG_ON(pending);
+}
+
+/**
+ * __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
+ */
+static inline bool __blk_end_request_cur(struct request *rq, int error)
+{
+       return __blk_end_request(rq, error, blk_rq_cur_bytes(rq));
+}
+
 extern void blk_complete_request(struct request *);
 extern void __blk_complete_request(struct request *);
 extern void blk_abort_request(struct request *);
 extern void blk_abort_queue(struct request_queue *);
-extern void blk_update_request(struct request *rq, int error,
-                              unsigned int nr_bytes);
-
-/*
- * blk_end_request() takes bytes instead of sectors as a complete size.
- * blk_rq_bytes() returns bytes left to complete in the entire request.
- * blk_rq_cur_bytes() returns bytes left to complete in the current segment.
- */
-extern unsigned int blk_rq_bytes(struct request *rq);
-extern unsigned int blk_rq_cur_bytes(struct request *rq);
 
 /*
  * Access functions for manipulating queue properties