Back to home page

LXR

 
 

    


0001 #ifndef BLK_INTERNAL_H
0002 #define BLK_INTERNAL_H
0003 
0004 #include <linux/idr.h>
0005 #include <linux/blk-mq.h>
0006 #include "blk-mq.h"
0007 
0008 /* Amount of time in which a process may batch requests */
0009 #define BLK_BATCH_TIME  (HZ/50UL)
0010 
0011 /* Number of requests a "batching" process may submit */
0012 #define BLK_BATCH_REQ   32
0013 
0014 /* Max future timer expiry for timeouts */
0015 #define BLK_MAX_TIMEOUT     (5 * HZ)
0016 
0017 struct blk_flush_queue {
0018     unsigned int        flush_queue_delayed:1;
0019     unsigned int        flush_pending_idx:1;
0020     unsigned int        flush_running_idx:1;
0021     unsigned long       flush_pending_since;
0022     struct list_head    flush_queue[2];
0023     struct list_head    flush_data_in_flight;
0024     struct request      *flush_rq;
0025 
0026     /*
0027      * flush_rq shares tag with this rq, both can't be active
0028      * at the same time
0029      */
0030     struct request      *orig_rq;
0031     spinlock_t      mq_flush_lock;
0032 };
0033 
0034 extern struct kmem_cache *blk_requestq_cachep;
0035 extern struct kmem_cache *request_cachep;
0036 extern struct kobj_type blk_queue_ktype;
0037 extern struct ida blk_queue_ida;
0038 
0039 static inline struct blk_flush_queue *blk_get_flush_queue(
0040         struct request_queue *q, struct blk_mq_ctx *ctx)
0041 {
0042     if (q->mq_ops)
0043         return blk_mq_map_queue(q, ctx->cpu)->fq;
0044     return q->fq;
0045 }
0046 
0047 static inline void __blk_get_queue(struct request_queue *q)
0048 {
0049     kobject_get(&q->kobj);
0050 }
0051 
0052 struct blk_flush_queue *blk_alloc_flush_queue(struct request_queue *q,
0053         int node, int cmd_size);
0054 void blk_free_flush_queue(struct blk_flush_queue *q);
0055 
0056 int blk_init_rl(struct request_list *rl, struct request_queue *q,
0057         gfp_t gfp_mask);
0058 void blk_exit_rl(struct request_list *rl);
0059 void init_request_from_bio(struct request *req, struct bio *bio);
0060 void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
0061             struct bio *bio);
0062 void blk_queue_bypass_start(struct request_queue *q);
0063 void blk_queue_bypass_end(struct request_queue *q);
0064 void blk_dequeue_request(struct request *rq);
0065 void __blk_queue_free_tags(struct request_queue *q);
0066 bool __blk_end_bidi_request(struct request *rq, int error,
0067                 unsigned int nr_bytes, unsigned int bidi_bytes);
0068 void blk_freeze_queue(struct request_queue *q);
0069 
0070 static inline void blk_queue_enter_live(struct request_queue *q)
0071 {
0072     /*
0073      * Given that running in generic_make_request() context
0074      * guarantees that a live reference against q_usage_counter has
0075      * been established, further references under that same context
0076      * need not check that the queue has been frozen (marked dead).
0077      */
0078     percpu_ref_get(&q->q_usage_counter);
0079 }
0080 
0081 #ifdef CONFIG_BLK_DEV_INTEGRITY
0082 void blk_flush_integrity(void);
0083 #else
0084 static inline void blk_flush_integrity(void)
0085 {
0086 }
0087 #endif
0088 
0089 void blk_timeout_work(struct work_struct *work);
0090 unsigned long blk_rq_timeout(unsigned long timeout);
0091 void blk_add_timer(struct request *req);
0092 void blk_delete_timer(struct request *);
0093 
0094 
0095 bool bio_attempt_front_merge(struct request_queue *q, struct request *req,
0096                  struct bio *bio);
0097 bool bio_attempt_back_merge(struct request_queue *q, struct request *req,
0098                 struct bio *bio);
0099 bool blk_attempt_plug_merge(struct request_queue *q, struct bio *bio,
0100                 unsigned int *request_count,
0101                 struct request **same_queue_rq);
0102 unsigned int blk_plug_queued_count(struct request_queue *q);
0103 
0104 void blk_account_io_start(struct request *req, bool new_io);
0105 void blk_account_io_completion(struct request *req, unsigned int bytes);
0106 void blk_account_io_done(struct request *req);
0107 
0108 /*
0109  * Internal atomic flags for request handling
0110  */
0111 enum rq_atomic_flags {
0112     REQ_ATOM_COMPLETE = 0,
0113     REQ_ATOM_STARTED,
0114     REQ_ATOM_POLL_SLEPT,
0115 };
0116 
0117 /*
0118  * EH timer and IO completion will both attempt to 'grab' the request, make
0119  * sure that only one of them succeeds
0120  */
0121 static inline int blk_mark_rq_complete(struct request *rq)
0122 {
0123     return test_and_set_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags);
0124 }
0125 
0126 static inline void blk_clear_rq_complete(struct request *rq)
0127 {
0128     clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags);
0129 }
0130 
0131 /*
0132  * Internal elevator interface
0133  */
0134 #define ELV_ON_HASH(rq) ((rq)->rq_flags & RQF_HASHED)
0135 
0136 void blk_insert_flush(struct request *rq);
0137 
0138 static inline struct request *__elv_next_request(struct request_queue *q)
0139 {
0140     struct request *rq;
0141     struct blk_flush_queue *fq = blk_get_flush_queue(q, NULL);
0142 
0143     while (1) {
0144         if (!list_empty(&q->queue_head)) {
0145             rq = list_entry_rq(q->queue_head.next);
0146             return rq;
0147         }
0148 
0149         /*
0150          * Flush request is running and flush request isn't queueable
0151          * in the drive, we can hold the queue till flush request is
0152          * finished. Even we don't do this, driver can't dispatch next
0153          * requests and will requeue them. And this can improve
0154          * throughput too. For example, we have request flush1, write1,
0155          * flush 2. flush1 is dispatched, then queue is hold, write1
0156          * isn't inserted to queue. After flush1 is finished, flush2
0157          * will be dispatched. Since disk cache is already clean,
0158          * flush2 will be finished very soon, so looks like flush2 is
0159          * folded to flush1.
0160          * Since the queue is hold, a flag is set to indicate the queue
0161          * should be restarted later. Please see flush_end_io() for
0162          * details.
0163          */
0164         if (fq->flush_pending_idx != fq->flush_running_idx &&
0165                 !queue_flush_queueable(q)) {
0166             fq->flush_queue_delayed = 1;
0167             return NULL;
0168         }
0169         if (unlikely(blk_queue_bypass(q)) ||
0170             !q->elevator->type->ops.elevator_dispatch_fn(q, 0))
0171             return NULL;
0172     }
0173 }
0174 
0175 static inline void elv_activate_rq(struct request_queue *q, struct request *rq)
0176 {
0177     struct elevator_queue *e = q->elevator;
0178 
0179     if (e->type->ops.elevator_activate_req_fn)
0180         e->type->ops.elevator_activate_req_fn(q, rq);
0181 }
0182 
0183 static inline void elv_deactivate_rq(struct request_queue *q, struct request *rq)
0184 {
0185     struct elevator_queue *e = q->elevator;
0186 
0187     if (e->type->ops.elevator_deactivate_req_fn)
0188         e->type->ops.elevator_deactivate_req_fn(q, rq);
0189 }
0190 
0191 #ifdef CONFIG_FAIL_IO_TIMEOUT
0192 int blk_should_fake_timeout(struct request_queue *);
0193 ssize_t part_timeout_show(struct device *, struct device_attribute *, char *);
0194 ssize_t part_timeout_store(struct device *, struct device_attribute *,
0195                 const char *, size_t);
0196 #else
0197 static inline int blk_should_fake_timeout(struct request_queue *q)
0198 {
0199     return 0;
0200 }
0201 #endif
0202 
0203 int ll_back_merge_fn(struct request_queue *q, struct request *req,
0204              struct bio *bio);
0205 int ll_front_merge_fn(struct request_queue *q, struct request *req, 
0206               struct bio *bio);
0207 int attempt_back_merge(struct request_queue *q, struct request *rq);
0208 int attempt_front_merge(struct request_queue *q, struct request *rq);
0209 int blk_attempt_req_merge(struct request_queue *q, struct request *rq,
0210                 struct request *next);
0211 void blk_recalc_rq_segments(struct request *rq);
0212 void blk_rq_set_mixed_merge(struct request *rq);
0213 bool blk_rq_merge_ok(struct request *rq, struct bio *bio);
0214 int blk_try_merge(struct request *rq, struct bio *bio);
0215 
0216 void blk_queue_congestion_threshold(struct request_queue *q);
0217 
0218 int blk_dev_init(void);
0219 
0220 
0221 /*
0222  * Return the threshold (number of used requests) at which the queue is
0223  * considered to be congested.  It include a little hysteresis to keep the
0224  * context switch rate down.
0225  */
0226 static inline int queue_congestion_on_threshold(struct request_queue *q)
0227 {
0228     return q->nr_congestion_on;
0229 }
0230 
0231 /*
0232  * The threshold at which a queue is considered to be uncongested
0233  */
0234 static inline int queue_congestion_off_threshold(struct request_queue *q)
0235 {
0236     return q->nr_congestion_off;
0237 }
0238 
0239 extern int blk_update_nr_requests(struct request_queue *, unsigned int);
0240 
0241 /*
0242  * Contribute to IO statistics IFF:
0243  *
0244  *  a) it's attached to a gendisk, and
0245  *  b) the queue had IO stats enabled when this request was started, and
0246  *  c) it's a file system request
0247  */
0248 static inline int blk_do_io_stat(struct request *rq)
0249 {
0250     return rq->rq_disk &&
0251            (rq->rq_flags & RQF_IO_STAT) &&
0252         (rq->cmd_type == REQ_TYPE_FS);
0253 }
0254 
0255 /*
0256  * Internal io_context interface
0257  */
0258 void get_io_context(struct io_context *ioc);
0259 struct io_cq *ioc_lookup_icq(struct io_context *ioc, struct request_queue *q);
0260 struct io_cq *ioc_create_icq(struct io_context *ioc, struct request_queue *q,
0261                  gfp_t gfp_mask);
0262 void ioc_clear_queue(struct request_queue *q);
0263 
0264 int create_task_io_context(struct task_struct *task, gfp_t gfp_mask, int node);
0265 
0266 /**
0267  * create_io_context - try to create task->io_context
0268  * @gfp_mask: allocation mask
0269  * @node: allocation node
0270  *
0271  * If %current->io_context is %NULL, allocate a new io_context and install
0272  * it.  Returns the current %current->io_context which may be %NULL if
0273  * allocation failed.
0274  *
0275  * Note that this function can't be called with IRQ disabled because
0276  * task_lock which protects %current->io_context is IRQ-unsafe.
0277  */
0278 static inline struct io_context *create_io_context(gfp_t gfp_mask, int node)
0279 {
0280     WARN_ON_ONCE(irqs_disabled());
0281     if (unlikely(!current->io_context))
0282         create_task_io_context(current, gfp_mask, node);
0283     return current->io_context;
0284 }
0285 
0286 /*
0287  * Internal throttling interface
0288  */
0289 #ifdef CONFIG_BLK_DEV_THROTTLING
0290 extern void blk_throtl_drain(struct request_queue *q);
0291 extern int blk_throtl_init(struct request_queue *q);
0292 extern void blk_throtl_exit(struct request_queue *q);
0293 #else /* CONFIG_BLK_DEV_THROTTLING */
0294 static inline void blk_throtl_drain(struct request_queue *q) { }
0295 static inline int blk_throtl_init(struct request_queue *q) { return 0; }
0296 static inline void blk_throtl_exit(struct request_queue *q) { }
0297 #endif /* CONFIG_BLK_DEV_THROTTLING */
0298 
0299 #endif /* BLK_INTERNAL_H */