]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - block/blk-core.c
blk-mq: move debugfs declarations to a separate header file
[mirror_ubuntu-bionic-kernel.git] / block / blk-core.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 1994, Karl Keyte: Added support for disk statistics
4 * Elevator latency, (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
5 * Queue request tables / lock, selectable elevator, Jens Axboe <axboe@suse.de>
6728cb0e
JA
6 * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au>
7 * - July2000
1da177e4
LT
8 * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
9 */
10
11/*
12 * This handles all read/write requests to block devices
13 */
1da177e4
LT
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/backing-dev.h>
17#include <linux/bio.h>
18#include <linux/blkdev.h>
320ae51f 19#include <linux/blk-mq.h>
1da177e4
LT
20#include <linux/highmem.h>
21#include <linux/mm.h>
22#include <linux/kernel_stat.h>
23#include <linux/string.h>
24#include <linux/init.h>
1da177e4
LT
25#include <linux/completion.h>
26#include <linux/slab.h>
27#include <linux/swap.h>
28#include <linux/writeback.h>
faccbd4b 29#include <linux/task_io_accounting_ops.h>
c17bb495 30#include <linux/fault-inject.h>
73c10101 31#include <linux/list_sort.h>
e3c78ca5 32#include <linux/delay.h>
aaf7c680 33#include <linux/ratelimit.h>
6c954667 34#include <linux/pm_runtime.h>
eea8f41c 35#include <linux/blk-cgroup.h>
18fbda91 36#include <linux/debugfs.h>
55782138
LZ
37
38#define CREATE_TRACE_POINTS
39#include <trace/events/block.h>
1da177e4 40
8324aa91 41#include "blk.h"
43a5e4e2 42#include "blk-mq.h"
d173a251 43#include "blk-mq-debugfs.h"
bd166ef1 44#include "blk-mq-sched.h"
87760e5e 45#include "blk-wbt.h"
8324aa91 46
18fbda91
OS
47#ifdef CONFIG_DEBUG_FS
48struct dentry *blk_debugfs_root;
49#endif
50
d07335e5 51EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_remap);
b0da3f0d 52EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_remap);
0a82a8d1 53EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_complete);
3291fa57 54EXPORT_TRACEPOINT_SYMBOL_GPL(block_split);
cbae8d45 55EXPORT_TRACEPOINT_SYMBOL_GPL(block_unplug);
0bfc2455 56
a73f730d
TH
57DEFINE_IDA(blk_queue_ida);
58
1da177e4
LT
59/*
60 * For the allocated request tables
61 */
d674d414 62struct kmem_cache *request_cachep;
1da177e4
LT
63
64/*
65 * For queue allocation
66 */
6728cb0e 67struct kmem_cache *blk_requestq_cachep;
1da177e4 68
1da177e4
LT
69/*
70 * Controlling structure to kblockd
71 */
ff856bad 72static struct workqueue_struct *kblockd_workqueue;
1da177e4 73
d40f75a0
TH
74static void blk_clear_congested(struct request_list *rl, int sync)
75{
d40f75a0
TH
76#ifdef CONFIG_CGROUP_WRITEBACK
77 clear_wb_congested(rl->blkg->wb_congested, sync);
78#else
482cf79c
TH
79 /*
80 * If !CGROUP_WRITEBACK, all blkg's map to bdi->wb and we shouldn't
81 * flip its congestion state for events on other blkcgs.
82 */
83 if (rl == &rl->q->root_rl)
dc3b17cc 84 clear_wb_congested(rl->q->backing_dev_info->wb.congested, sync);
d40f75a0
TH
85#endif
86}
87
88static void blk_set_congested(struct request_list *rl, int sync)
89{
d40f75a0
TH
90#ifdef CONFIG_CGROUP_WRITEBACK
91 set_wb_congested(rl->blkg->wb_congested, sync);
92#else
482cf79c
TH
93 /* see blk_clear_congested() */
94 if (rl == &rl->q->root_rl)
dc3b17cc 95 set_wb_congested(rl->q->backing_dev_info->wb.congested, sync);
d40f75a0
TH
96#endif
97}
98
8324aa91 99void blk_queue_congestion_threshold(struct request_queue *q)
1da177e4
LT
100{
101 int nr;
102
103 nr = q->nr_requests - (q->nr_requests / 8) + 1;
104 if (nr > q->nr_requests)
105 nr = q->nr_requests;
106 q->nr_congestion_on = nr;
107
108 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
109 if (nr < 1)
110 nr = 1;
111 q->nr_congestion_off = nr;
112}
113
2a4aa30c 114void blk_rq_init(struct request_queue *q, struct request *rq)
1da177e4 115{
1afb20f3
FT
116 memset(rq, 0, sizeof(*rq));
117
1da177e4 118 INIT_LIST_HEAD(&rq->queuelist);
242f9dcb 119 INIT_LIST_HEAD(&rq->timeout_list);
c7c22e4d 120 rq->cpu = -1;
63a71386 121 rq->q = q;
a2dec7b3 122 rq->__sector = (sector_t) -1;
2e662b65
JA
123 INIT_HLIST_NODE(&rq->hash);
124 RB_CLEAR_NODE(&rq->rb_node);
63a71386 125 rq->tag = -1;
bd166ef1 126 rq->internal_tag = -1;
b243ddcb 127 rq->start_time = jiffies;
9195291e 128 set_start_time_ns(rq);
09e099d4 129 rq->part = NULL;
1da177e4 130}
2a4aa30c 131EXPORT_SYMBOL(blk_rq_init);
1da177e4 132
5bb23a68
N
133static void req_bio_endio(struct request *rq, struct bio *bio,
134 unsigned int nbytes, int error)
1da177e4 135{
78d8e58a 136 if (error)
4246a0b6 137 bio->bi_error = error;
797e7dbb 138
e8064021 139 if (unlikely(rq->rq_flags & RQF_QUIET))
b7c44ed9 140 bio_set_flag(bio, BIO_QUIET);
08bafc03 141
f79ea416 142 bio_advance(bio, nbytes);
7ba1ba12 143
143a87f4 144 /* don't actually finish bio if it's part of flush sequence */
e8064021 145 if (bio->bi_iter.bi_size == 0 && !(rq->rq_flags & RQF_FLUSH_SEQ))
4246a0b6 146 bio_endio(bio);
1da177e4 147}
1da177e4 148
1da177e4
LT
149void blk_dump_rq_flags(struct request *rq, char *msg)
150{
aebf526b
CH
151 printk(KERN_INFO "%s: dev %s: flags=%llx\n", msg,
152 rq->rq_disk ? rq->rq_disk->disk_name : "?",
5953316d 153 (unsigned long long) rq->cmd_flags);
1da177e4 154
83096ebf
TH
155 printk(KERN_INFO " sector %llu, nr/cnr %u/%u\n",
156 (unsigned long long)blk_rq_pos(rq),
157 blk_rq_sectors(rq), blk_rq_cur_sectors(rq));
b4f42e28
JA
158 printk(KERN_INFO " bio %p, biotail %p, len %u\n",
159 rq->bio, rq->biotail, blk_rq_bytes(rq));
1da177e4 160}
1da177e4
LT
161EXPORT_SYMBOL(blk_dump_rq_flags);
162
3cca6dc1 163static void blk_delay_work(struct work_struct *work)
1da177e4 164{
3cca6dc1 165 struct request_queue *q;
1da177e4 166
3cca6dc1
JA
167 q = container_of(work, struct request_queue, delay_work.work);
168 spin_lock_irq(q->queue_lock);
24ecfbe2 169 __blk_run_queue(q);
3cca6dc1 170 spin_unlock_irq(q->queue_lock);
1da177e4 171}
1da177e4
LT
172
173/**
3cca6dc1
JA
174 * blk_delay_queue - restart queueing after defined interval
175 * @q: The &struct request_queue in question
176 * @msecs: Delay in msecs
1da177e4
LT
177 *
178 * Description:
3cca6dc1
JA
179 * Sometimes queueing needs to be postponed for a little while, to allow
180 * resources to come back. This function will make sure that queueing is
70460571 181 * restarted around the specified time. Queue lock must be held.
3cca6dc1
JA
182 */
183void blk_delay_queue(struct request_queue *q, unsigned long msecs)
2ad8b1ef 184{
70460571
BVA
185 if (likely(!blk_queue_dead(q)))
186 queue_delayed_work(kblockd_workqueue, &q->delay_work,
187 msecs_to_jiffies(msecs));
2ad8b1ef 188}
3cca6dc1 189EXPORT_SYMBOL(blk_delay_queue);
2ad8b1ef 190
21491412
JA
191/**
192 * blk_start_queue_async - asynchronously restart a previously stopped queue
193 * @q: The &struct request_queue in question
194 *
195 * Description:
196 * blk_start_queue_async() will clear the stop flag on the queue, and
197 * ensure that the request_fn for the queue is run from an async
198 * context.
199 **/
200void blk_start_queue_async(struct request_queue *q)
201{
202 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
203 blk_run_queue_async(q);
204}
205EXPORT_SYMBOL(blk_start_queue_async);
206
1da177e4
LT
207/**
208 * blk_start_queue - restart a previously stopped queue
165125e1 209 * @q: The &struct request_queue in question
1da177e4
LT
210 *
211 * Description:
212 * blk_start_queue() will clear the stop flag on the queue, and call
213 * the request_fn for the queue if it was in a stopped state when
214 * entered. Also see blk_stop_queue(). Queue lock must be held.
215 **/
165125e1 216void blk_start_queue(struct request_queue *q)
1da177e4 217{
a038e253
PBG
218 WARN_ON(!irqs_disabled());
219
75ad23bc 220 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
24ecfbe2 221 __blk_run_queue(q);
1da177e4 222}
1da177e4
LT
223EXPORT_SYMBOL(blk_start_queue);
224
225/**
226 * blk_stop_queue - stop a queue
165125e1 227 * @q: The &struct request_queue in question
1da177e4
LT
228 *
229 * Description:
230 * The Linux block layer assumes that a block driver will consume all
231 * entries on the request queue when the request_fn strategy is called.
232 * Often this will not happen, because of hardware limitations (queue
233 * depth settings). If a device driver gets a 'queue full' response,
234 * or if it simply chooses not to queue more I/O at one point, it can
235 * call this function to prevent the request_fn from being called until
236 * the driver has signalled it's ready to go again. This happens by calling
237 * blk_start_queue() to restart queue operations. Queue lock must be held.
238 **/
165125e1 239void blk_stop_queue(struct request_queue *q)
1da177e4 240{
136b5721 241 cancel_delayed_work(&q->delay_work);
75ad23bc 242 queue_flag_set(QUEUE_FLAG_STOPPED, q);
1da177e4
LT
243}
244EXPORT_SYMBOL(blk_stop_queue);
245
246/**
247 * blk_sync_queue - cancel any pending callbacks on a queue
248 * @q: the queue
249 *
250 * Description:
251 * The block layer may perform asynchronous callback activity
252 * on a queue, such as calling the unplug function after a timeout.
253 * A block device may call blk_sync_queue to ensure that any
254 * such activity is cancelled, thus allowing it to release resources
59c51591 255 * that the callbacks might use. The caller must already have made sure
1da177e4
LT
256 * that its ->make_request_fn will not re-add plugging prior to calling
257 * this function.
258 *
da527770 259 * This function does not cancel any asynchronous activity arising
da3dae54 260 * out of elevator or throttling code. That would require elevator_exit()
5efd6113 261 * and blkcg_exit_queue() to be called with queue lock initialized.
da527770 262 *
1da177e4
LT
263 */
264void blk_sync_queue(struct request_queue *q)
265{
70ed28b9 266 del_timer_sync(&q->timeout);
f04c1fe7
ML
267
268 if (q->mq_ops) {
269 struct blk_mq_hw_ctx *hctx;
270 int i;
271
21c6e939 272 queue_for_each_hw_ctx(q, hctx, i)
9f993737 273 cancel_delayed_work_sync(&hctx->run_work);
f04c1fe7
ML
274 } else {
275 cancel_delayed_work_sync(&q->delay_work);
276 }
1da177e4
LT
277}
278EXPORT_SYMBOL(blk_sync_queue);
279
c246e80d
BVA
280/**
281 * __blk_run_queue_uncond - run a queue whether or not it has been stopped
282 * @q: The queue to run
283 *
284 * Description:
285 * Invoke request handling on a queue if there are any pending requests.
286 * May be used to restart request handling after a request has completed.
287 * This variant runs the queue whether or not the queue has been
288 * stopped. Must be called with the queue lock held and interrupts
289 * disabled. See also @blk_run_queue.
290 */
291inline void __blk_run_queue_uncond(struct request_queue *q)
292{
293 if (unlikely(blk_queue_dead(q)))
294 return;
295
24faf6f6
BVA
296 /*
297 * Some request_fn implementations, e.g. scsi_request_fn(), unlock
298 * the queue lock internally. As a result multiple threads may be
299 * running such a request function concurrently. Keep track of the
300 * number of active request_fn invocations such that blk_drain_queue()
301 * can wait until all these request_fn calls have finished.
302 */
303 q->request_fn_active++;
c246e80d 304 q->request_fn(q);
24faf6f6 305 q->request_fn_active--;
c246e80d 306}
a7928c15 307EXPORT_SYMBOL_GPL(__blk_run_queue_uncond);
c246e80d 308
1da177e4 309/**
80a4b58e 310 * __blk_run_queue - run a single device queue
1da177e4 311 * @q: The queue to run
80a4b58e
JA
312 *
313 * Description:
314 * See @blk_run_queue. This variant must be called with the queue lock
24ecfbe2 315 * held and interrupts disabled.
1da177e4 316 */
24ecfbe2 317void __blk_run_queue(struct request_queue *q)
1da177e4 318{
a538cd03
TH
319 if (unlikely(blk_queue_stopped(q)))
320 return;
321
c246e80d 322 __blk_run_queue_uncond(q);
75ad23bc
NP
323}
324EXPORT_SYMBOL(__blk_run_queue);
dac07ec1 325
24ecfbe2
CH
326/**
327 * blk_run_queue_async - run a single device queue in workqueue context
328 * @q: The queue to run
329 *
330 * Description:
331 * Tells kblockd to perform the equivalent of @blk_run_queue on behalf
70460571 332 * of us. The caller must hold the queue lock.
24ecfbe2
CH
333 */
334void blk_run_queue_async(struct request_queue *q)
335{
70460571 336 if (likely(!blk_queue_stopped(q) && !blk_queue_dead(q)))
e7c2f967 337 mod_delayed_work(kblockd_workqueue, &q->delay_work, 0);
24ecfbe2 338}
c21e6beb 339EXPORT_SYMBOL(blk_run_queue_async);
24ecfbe2 340
75ad23bc
NP
341/**
342 * blk_run_queue - run a single device queue
343 * @q: The queue to run
80a4b58e
JA
344 *
345 * Description:
346 * Invoke request handling on this queue, if it has pending work to do.
a7f55792 347 * May be used to restart queueing when a request has completed.
75ad23bc
NP
348 */
349void blk_run_queue(struct request_queue *q)
350{
351 unsigned long flags;
352
353 spin_lock_irqsave(q->queue_lock, flags);
24ecfbe2 354 __blk_run_queue(q);
1da177e4
LT
355 spin_unlock_irqrestore(q->queue_lock, flags);
356}
357EXPORT_SYMBOL(blk_run_queue);
358
165125e1 359void blk_put_queue(struct request_queue *q)
483f4afc
AV
360{
361 kobject_put(&q->kobj);
362}
d86e0e83 363EXPORT_SYMBOL(blk_put_queue);
483f4afc 364
e3c78ca5 365/**
807592a4 366 * __blk_drain_queue - drain requests from request_queue
e3c78ca5 367 * @q: queue to drain
c9a929dd 368 * @drain_all: whether to drain all requests or only the ones w/ ELVPRIV
e3c78ca5 369 *
c9a929dd
TH
370 * Drain requests from @q. If @drain_all is set, all requests are drained.
371 * If not, only ELVPRIV requests are drained. The caller is responsible
372 * for ensuring that no new requests which need to be drained are queued.
e3c78ca5 373 */
807592a4
BVA
374static void __blk_drain_queue(struct request_queue *q, bool drain_all)
375 __releases(q->queue_lock)
376 __acquires(q->queue_lock)
e3c78ca5 377{
458f27a9
AH
378 int i;
379
807592a4
BVA
380 lockdep_assert_held(q->queue_lock);
381
e3c78ca5 382 while (true) {
481a7d64 383 bool drain = false;
e3c78ca5 384
b855b04a
TH
385 /*
386 * The caller might be trying to drain @q before its
387 * elevator is initialized.
388 */
389 if (q->elevator)
390 elv_drain_elevator(q);
391
5efd6113 392 blkcg_drain_queue(q);
e3c78ca5 393
4eabc941
TH
394 /*
395 * This function might be called on a queue which failed
b855b04a
TH
396 * driver init after queue creation or is not yet fully
397 * active yet. Some drivers (e.g. fd and loop) get unhappy
398 * in such cases. Kick queue iff dispatch queue has
399 * something on it and @q has request_fn set.
4eabc941 400 */
b855b04a 401 if (!list_empty(&q->queue_head) && q->request_fn)
4eabc941 402 __blk_run_queue(q);
c9a929dd 403
8a5ecdd4 404 drain |= q->nr_rqs_elvpriv;
24faf6f6 405 drain |= q->request_fn_active;
481a7d64
TH
406
407 /*
408 * Unfortunately, requests are queued at and tracked from
409 * multiple places and there's no single counter which can
410 * be drained. Check all the queues and counters.
411 */
412 if (drain_all) {
e97c293c 413 struct blk_flush_queue *fq = blk_get_flush_queue(q, NULL);
481a7d64
TH
414 drain |= !list_empty(&q->queue_head);
415 for (i = 0; i < 2; i++) {
8a5ecdd4 416 drain |= q->nr_rqs[i];
481a7d64 417 drain |= q->in_flight[i];
7c94e1c1
ML
418 if (fq)
419 drain |= !list_empty(&fq->flush_queue[i]);
481a7d64
TH
420 }
421 }
e3c78ca5 422
481a7d64 423 if (!drain)
e3c78ca5 424 break;
807592a4
BVA
425
426 spin_unlock_irq(q->queue_lock);
427
e3c78ca5 428 msleep(10);
807592a4
BVA
429
430 spin_lock_irq(q->queue_lock);
e3c78ca5 431 }
458f27a9
AH
432
433 /*
434 * With queue marked dead, any woken up waiter will fail the
435 * allocation path, so the wakeup chaining is lost and we're
436 * left with hung waiters. We need to wake up those waiters.
437 */
438 if (q->request_fn) {
a051661c
TH
439 struct request_list *rl;
440
a051661c
TH
441 blk_queue_for_each_rl(rl, q)
442 for (i = 0; i < ARRAY_SIZE(rl->wait); i++)
443 wake_up_all(&rl->wait[i]);
458f27a9 444 }
e3c78ca5
TH
445}
446
d732580b
TH
447/**
448 * blk_queue_bypass_start - enter queue bypass mode
449 * @q: queue of interest
450 *
451 * In bypass mode, only the dispatch FIFO queue of @q is used. This
452 * function makes @q enter bypass mode and drains all requests which were
6ecf23af 453 * throttled or issued before. On return, it's guaranteed that no request
80fd9979
TH
454 * is being throttled or has ELVPRIV set and blk_queue_bypass() %true
455 * inside queue or RCU read lock.
d732580b
TH
456 */
457void blk_queue_bypass_start(struct request_queue *q)
458{
459 spin_lock_irq(q->queue_lock);
776687bc 460 q->bypass_depth++;
d732580b
TH
461 queue_flag_set(QUEUE_FLAG_BYPASS, q);
462 spin_unlock_irq(q->queue_lock);
463
776687bc
TH
464 /*
465 * Queues start drained. Skip actual draining till init is
466 * complete. This avoids lenghty delays during queue init which
467 * can happen many times during boot.
468 */
469 if (blk_queue_init_done(q)) {
807592a4
BVA
470 spin_lock_irq(q->queue_lock);
471 __blk_drain_queue(q, false);
472 spin_unlock_irq(q->queue_lock);
473
b82d4b19
TH
474 /* ensure blk_queue_bypass() is %true inside RCU read lock */
475 synchronize_rcu();
476 }
d732580b
TH
477}
478EXPORT_SYMBOL_GPL(blk_queue_bypass_start);
479
480/**
481 * blk_queue_bypass_end - leave queue bypass mode
482 * @q: queue of interest
483 *
484 * Leave bypass mode and restore the normal queueing behavior.
485 */
486void blk_queue_bypass_end(struct request_queue *q)
487{
488 spin_lock_irq(q->queue_lock);
489 if (!--q->bypass_depth)
490 queue_flag_clear(QUEUE_FLAG_BYPASS, q);
491 WARN_ON_ONCE(q->bypass_depth < 0);
492 spin_unlock_irq(q->queue_lock);
493}
494EXPORT_SYMBOL_GPL(blk_queue_bypass_end);
495
aed3ea94
JA
496void blk_set_queue_dying(struct request_queue *q)
497{
1b856086
BVA
498 spin_lock_irq(q->queue_lock);
499 queue_flag_set(QUEUE_FLAG_DYING, q);
500 spin_unlock_irq(q->queue_lock);
aed3ea94 501
d3cfb2a0
ML
502 /*
503 * When queue DYING flag is set, we need to block new req
504 * entering queue, so we call blk_freeze_queue_start() to
505 * prevent I/O from crossing blk_queue_enter().
506 */
507 blk_freeze_queue_start(q);
508
aed3ea94
JA
509 if (q->mq_ops)
510 blk_mq_wake_waiters(q);
511 else {
512 struct request_list *rl;
513
bbfc3c5d 514 spin_lock_irq(q->queue_lock);
aed3ea94
JA
515 blk_queue_for_each_rl(rl, q) {
516 if (rl->rq_pool) {
517 wake_up(&rl->wait[BLK_RW_SYNC]);
518 wake_up(&rl->wait[BLK_RW_ASYNC]);
519 }
520 }
bbfc3c5d 521 spin_unlock_irq(q->queue_lock);
aed3ea94
JA
522 }
523}
524EXPORT_SYMBOL_GPL(blk_set_queue_dying);
525
c9a929dd
TH
526/**
527 * blk_cleanup_queue - shutdown a request queue
528 * @q: request queue to shutdown
529 *
c246e80d
BVA
530 * Mark @q DYING, drain all pending requests, mark @q DEAD, destroy and
531 * put it. All future requests will be failed immediately with -ENODEV.
c94a96ac 532 */
6728cb0e 533void blk_cleanup_queue(struct request_queue *q)
483f4afc 534{
c9a929dd 535 spinlock_t *lock = q->queue_lock;
e3335de9 536
3f3299d5 537 /* mark @q DYING, no new request or merges will be allowed afterwards */
483f4afc 538 mutex_lock(&q->sysfs_lock);
aed3ea94 539 blk_set_queue_dying(q);
c9a929dd 540 spin_lock_irq(lock);
6ecf23af 541
80fd9979 542 /*
3f3299d5 543 * A dying queue is permanently in bypass mode till released. Note
80fd9979
TH
544 * that, unlike blk_queue_bypass_start(), we aren't performing
545 * synchronize_rcu() after entering bypass mode to avoid the delay
546 * as some drivers create and destroy a lot of queues while
547 * probing. This is still safe because blk_release_queue() will be
548 * called only after the queue refcnt drops to zero and nothing,
549 * RCU or not, would be traversing the queue by then.
550 */
6ecf23af
TH
551 q->bypass_depth++;
552 queue_flag_set(QUEUE_FLAG_BYPASS, q);
553
c9a929dd
TH
554 queue_flag_set(QUEUE_FLAG_NOMERGES, q);
555 queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
3f3299d5 556 queue_flag_set(QUEUE_FLAG_DYING, q);
c9a929dd
TH
557 spin_unlock_irq(lock);
558 mutex_unlock(&q->sysfs_lock);
559
c246e80d
BVA
560 /*
561 * Drain all requests queued before DYING marking. Set DEAD flag to
562 * prevent that q->request_fn() gets invoked after draining finished.
563 */
3ef28e83 564 blk_freeze_queue(q);
e869b546
BVA
565 if (!q->mq_ops) {
566 spin_lock_irq(lock);
43a5e4e2 567 __blk_drain_queue(q, true);
e869b546
BVA
568 } else {
569 blk_mq_debugfs_unregister_mq(q);
570 spin_lock_irq(lock);
571 }
c246e80d 572 queue_flag_set(QUEUE_FLAG_DEAD, q);
807592a4 573 spin_unlock_irq(lock);
c9a929dd 574
5a48fc14
DW
575 /* for synchronous bio-based driver finish in-flight integrity i/o */
576 blk_flush_integrity();
577
c9a929dd 578 /* @q won't process any more request, flush async actions */
dc3b17cc 579 del_timer_sync(&q->backing_dev_info->laptop_mode_wb_timer);
c9a929dd
TH
580 blk_sync_queue(q);
581
45a9c9d9
BVA
582 if (q->mq_ops)
583 blk_mq_free_queue(q);
3ef28e83 584 percpu_ref_exit(&q->q_usage_counter);
45a9c9d9 585
5e5cfac0
AH
586 spin_lock_irq(lock);
587 if (q->queue_lock != &q->__queue_lock)
588 q->queue_lock = &q->__queue_lock;
589 spin_unlock_irq(lock);
590
c9a929dd 591 /* @q is and will stay empty, shutdown and put */
483f4afc
AV
592 blk_put_queue(q);
593}
1da177e4
LT
594EXPORT_SYMBOL(blk_cleanup_queue);
595
271508db 596/* Allocate memory local to the request queue */
6d247d7f 597static void *alloc_request_simple(gfp_t gfp_mask, void *data)
271508db 598{
6d247d7f
CH
599 struct request_queue *q = data;
600
601 return kmem_cache_alloc_node(request_cachep, gfp_mask, q->node);
271508db
DR
602}
603
6d247d7f 604static void free_request_simple(void *element, void *data)
271508db
DR
605{
606 kmem_cache_free(request_cachep, element);
607}
608
6d247d7f
CH
609static void *alloc_request_size(gfp_t gfp_mask, void *data)
610{
611 struct request_queue *q = data;
612 struct request *rq;
613
614 rq = kmalloc_node(sizeof(struct request) + q->cmd_size, gfp_mask,
615 q->node);
616 if (rq && q->init_rq_fn && q->init_rq_fn(q, rq, gfp_mask) < 0) {
617 kfree(rq);
618 rq = NULL;
619 }
620 return rq;
621}
622
623static void free_request_size(void *element, void *data)
624{
625 struct request_queue *q = data;
626
627 if (q->exit_rq_fn)
628 q->exit_rq_fn(q, element);
629 kfree(element);
630}
631
5b788ce3
TH
632int blk_init_rl(struct request_list *rl, struct request_queue *q,
633 gfp_t gfp_mask)
1da177e4 634{
1abec4fd
MS
635 if (unlikely(rl->rq_pool))
636 return 0;
637
5b788ce3 638 rl->q = q;
1faa16d2
JA
639 rl->count[BLK_RW_SYNC] = rl->count[BLK_RW_ASYNC] = 0;
640 rl->starved[BLK_RW_SYNC] = rl->starved[BLK_RW_ASYNC] = 0;
1faa16d2
JA
641 init_waitqueue_head(&rl->wait[BLK_RW_SYNC]);
642 init_waitqueue_head(&rl->wait[BLK_RW_ASYNC]);
1da177e4 643
6d247d7f
CH
644 if (q->cmd_size) {
645 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ,
646 alloc_request_size, free_request_size,
647 q, gfp_mask, q->node);
648 } else {
649 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ,
650 alloc_request_simple, free_request_simple,
651 q, gfp_mask, q->node);
652 }
1da177e4
LT
653 if (!rl->rq_pool)
654 return -ENOMEM;
655
656 return 0;
657}
658
5b788ce3
TH
659void blk_exit_rl(struct request_list *rl)
660{
661 if (rl->rq_pool)
662 mempool_destroy(rl->rq_pool);
663}
664
165125e1 665struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
1da177e4 666{
c304a51b 667 return blk_alloc_queue_node(gfp_mask, NUMA_NO_NODE);
1946089a
CL
668}
669EXPORT_SYMBOL(blk_alloc_queue);
1da177e4 670
6f3b0e8b 671int blk_queue_enter(struct request_queue *q, bool nowait)
3ef28e83
DW
672{
673 while (true) {
674 int ret;
675
676 if (percpu_ref_tryget_live(&q->q_usage_counter))
677 return 0;
678
6f3b0e8b 679 if (nowait)
3ef28e83
DW
680 return -EBUSY;
681
5ed61d3f 682 /*
1671d522 683 * read pair of barrier in blk_freeze_queue_start(),
5ed61d3f 684 * we need to order reading __PERCPU_REF_DEAD flag of
d3cfb2a0
ML
685 * .q_usage_counter and reading .mq_freeze_depth or
686 * queue dying flag, otherwise the following wait may
687 * never return if the two reads are reordered.
5ed61d3f
ML
688 */
689 smp_rmb();
690
3ef28e83
DW
691 ret = wait_event_interruptible(q->mq_freeze_wq,
692 !atomic_read(&q->mq_freeze_depth) ||
693 blk_queue_dying(q));
694 if (blk_queue_dying(q))
695 return -ENODEV;
696 if (ret)
697 return ret;
698 }
699}
700
701void blk_queue_exit(struct request_queue *q)
702{
703 percpu_ref_put(&q->q_usage_counter);
704}
705
706static void blk_queue_usage_counter_release(struct percpu_ref *ref)
707{
708 struct request_queue *q =
709 container_of(ref, struct request_queue, q_usage_counter);
710
711 wake_up_all(&q->mq_freeze_wq);
712}
713
287922eb
CH
714static void blk_rq_timed_out_timer(unsigned long data)
715{
716 struct request_queue *q = (struct request_queue *)data;
717
718 kblockd_schedule_work(&q->timeout_work);
719}
720
165125e1 721struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
1946089a 722{
165125e1 723 struct request_queue *q;
1946089a 724
8324aa91 725 q = kmem_cache_alloc_node(blk_requestq_cachep,
94f6030c 726 gfp_mask | __GFP_ZERO, node_id);
1da177e4
LT
727 if (!q)
728 return NULL;
729
00380a40 730 q->id = ida_simple_get(&blk_queue_ida, 0, 0, gfp_mask);
a73f730d 731 if (q->id < 0)
3d2936f4 732 goto fail_q;
a73f730d 733
54efd50b
KO
734 q->bio_split = bioset_create(BIO_POOL_SIZE, 0);
735 if (!q->bio_split)
736 goto fail_id;
737
d03f6cdc
JK
738 q->backing_dev_info = bdi_alloc_node(gfp_mask, node_id);
739 if (!q->backing_dev_info)
740 goto fail_split;
741
a83b576c
JA
742 q->stats = blk_alloc_queue_stats();
743 if (!q->stats)
744 goto fail_stats;
745
dc3b17cc 746 q->backing_dev_info->ra_pages =
09cbfeaf 747 (VM_MAX_READAHEAD * 1024) / PAGE_SIZE;
dc3b17cc
JK
748 q->backing_dev_info->capabilities = BDI_CAP_CGROUP_WRITEBACK;
749 q->backing_dev_info->name = "block";
5151412d 750 q->node = node_id;
0989a025 751
dc3b17cc 752 setup_timer(&q->backing_dev_info->laptop_mode_wb_timer,
31373d09 753 laptop_mode_timer_fn, (unsigned long) q);
242f9dcb 754 setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
b855b04a 755 INIT_LIST_HEAD(&q->queue_head);
242f9dcb 756 INIT_LIST_HEAD(&q->timeout_list);
a612fddf 757 INIT_LIST_HEAD(&q->icq_list);
4eef3049 758#ifdef CONFIG_BLK_CGROUP
e8989fae 759 INIT_LIST_HEAD(&q->blkg_list);
4eef3049 760#endif
3cca6dc1 761 INIT_DELAYED_WORK(&q->delay_work, blk_delay_work);
483f4afc 762
8324aa91 763 kobject_init(&q->kobj, &blk_queue_ktype);
1da177e4 764
483f4afc 765 mutex_init(&q->sysfs_lock);
e7e72bf6 766 spin_lock_init(&q->__queue_lock);
483f4afc 767
c94a96ac
VG
768 /*
769 * By default initialize queue_lock to internal lock and driver can
770 * override it later if need be.
771 */
772 q->queue_lock = &q->__queue_lock;
773
b82d4b19
TH
774 /*
775 * A queue starts its life with bypass turned on to avoid
776 * unnecessary bypass on/off overhead and nasty surprises during
749fefe6
TH
777 * init. The initial bypass will be finished when the queue is
778 * registered by blk_register_queue().
b82d4b19
TH
779 */
780 q->bypass_depth = 1;
781 __set_bit(QUEUE_FLAG_BYPASS, &q->queue_flags);
782
320ae51f
JA
783 init_waitqueue_head(&q->mq_freeze_wq);
784
3ef28e83
DW
785 /*
786 * Init percpu_ref in atomic mode so that it's faster to shutdown.
787 * See blk_register_queue() for details.
788 */
789 if (percpu_ref_init(&q->q_usage_counter,
790 blk_queue_usage_counter_release,
791 PERCPU_REF_INIT_ATOMIC, GFP_KERNEL))
fff4996b 792 goto fail_bdi;
f51b802c 793
3ef28e83
DW
794 if (blkcg_init_queue(q))
795 goto fail_ref;
796
1da177e4 797 return q;
a73f730d 798
3ef28e83
DW
799fail_ref:
800 percpu_ref_exit(&q->q_usage_counter);
fff4996b 801fail_bdi:
a83b576c
JA
802 blk_free_queue_stats(q->stats);
803fail_stats:
d03f6cdc 804 bdi_put(q->backing_dev_info);
54efd50b
KO
805fail_split:
806 bioset_free(q->bio_split);
a73f730d
TH
807fail_id:
808 ida_simple_remove(&blk_queue_ida, q->id);
809fail_q:
810 kmem_cache_free(blk_requestq_cachep, q);
811 return NULL;
1da177e4 812}
1946089a 813EXPORT_SYMBOL(blk_alloc_queue_node);
1da177e4
LT
814
815/**
816 * blk_init_queue - prepare a request queue for use with a block device
817 * @rfn: The function to be called to process requests that have been
818 * placed on the queue.
819 * @lock: Request queue spin lock
820 *
821 * Description:
822 * If a block device wishes to use the standard request handling procedures,
823 * which sorts requests and coalesces adjacent requests, then it must
824 * call blk_init_queue(). The function @rfn will be called when there
825 * are requests on the queue that need to be processed. If the device
826 * supports plugging, then @rfn may not be called immediately when requests
827 * are available on the queue, but may be called at some time later instead.
828 * Plugged queues are generally unplugged when a buffer belonging to one
829 * of the requests on the queue is needed, or due to memory pressure.
830 *
831 * @rfn is not required, or even expected, to remove all requests off the
832 * queue, but only as many as it can handle at a time. If it does leave
833 * requests on the queue, it is responsible for arranging that the requests
834 * get dealt with eventually.
835 *
836 * The queue spin lock must be held while manipulating the requests on the
a038e253
PBG
837 * request queue; this lock will be taken also from interrupt context, so irq
838 * disabling is needed for it.
1da177e4 839 *
710027a4 840 * Function returns a pointer to the initialized request queue, or %NULL if
1da177e4
LT
841 * it didn't succeed.
842 *
843 * Note:
844 * blk_init_queue() must be paired with a blk_cleanup_queue() call
845 * when the block device is deactivated (such as at module unload).
846 **/
1946089a 847
165125e1 848struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
1da177e4 849{
c304a51b 850 return blk_init_queue_node(rfn, lock, NUMA_NO_NODE);
1946089a
CL
851}
852EXPORT_SYMBOL(blk_init_queue);
853
165125e1 854struct request_queue *
1946089a
CL
855blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
856{
5ea708d1 857 struct request_queue *q;
1da177e4 858
5ea708d1
CH
859 q = blk_alloc_queue_node(GFP_KERNEL, node_id);
860 if (!q)
c86d1b8a
MS
861 return NULL;
862
5ea708d1
CH
863 q->request_fn = rfn;
864 if (lock)
865 q->queue_lock = lock;
866 if (blk_init_allocated_queue(q) < 0) {
867 blk_cleanup_queue(q);
868 return NULL;
869 }
18741986 870
7982e90c 871 return q;
01effb0d
MS
872}
873EXPORT_SYMBOL(blk_init_queue_node);
874
dece1635 875static blk_qc_t blk_queue_bio(struct request_queue *q, struct bio *bio);
336b7e1f 876
1da177e4 877
5ea708d1
CH
878int blk_init_allocated_queue(struct request_queue *q)
879{
6d247d7f 880 q->fq = blk_alloc_flush_queue(q, NUMA_NO_NODE, q->cmd_size);
ba483388 881 if (!q->fq)
5ea708d1 882 return -ENOMEM;
7982e90c 883
6d247d7f
CH
884 if (q->init_rq_fn && q->init_rq_fn(q, q->fq->flush_rq, GFP_KERNEL))
885 goto out_free_flush_queue;
7982e90c 886
a051661c 887 if (blk_init_rl(&q->root_rl, q, GFP_KERNEL))
6d247d7f 888 goto out_exit_flush_rq;
1da177e4 889
287922eb 890 INIT_WORK(&q->timeout_work, blk_timeout_work);
60ea8226 891 q->queue_flags |= QUEUE_FLAG_DEFAULT;
c94a96ac 892
f3b144aa
JA
893 /*
894 * This also sets hw/phys segments, boundary and size
895 */
c20e8de2 896 blk_queue_make_request(q, blk_queue_bio);
1da177e4 897
44ec9542
AS
898 q->sg_reserved_size = INT_MAX;
899
eb1c160b
TS
900 /* Protect q->elevator from elevator_change */
901 mutex_lock(&q->sysfs_lock);
902
b82d4b19 903 /* init elevator */
eb1c160b
TS
904 if (elevator_init(q, NULL)) {
905 mutex_unlock(&q->sysfs_lock);
6d247d7f 906 goto out_exit_flush_rq;
eb1c160b
TS
907 }
908
909 mutex_unlock(&q->sysfs_lock);
5ea708d1 910 return 0;
eb1c160b 911
6d247d7f
CH
912out_exit_flush_rq:
913 if (q->exit_rq_fn)
914 q->exit_rq_fn(q, q->fq->flush_rq);
915out_free_flush_queue:
ba483388 916 blk_free_flush_queue(q->fq);
5ea708d1 917 return -ENOMEM;
1da177e4 918}
5151412d 919EXPORT_SYMBOL(blk_init_allocated_queue);
1da177e4 920
09ac46c4 921bool blk_get_queue(struct request_queue *q)
1da177e4 922{
3f3299d5 923 if (likely(!blk_queue_dying(q))) {
09ac46c4
TH
924 __blk_get_queue(q);
925 return true;
1da177e4
LT
926 }
927
09ac46c4 928 return false;
1da177e4 929}
d86e0e83 930EXPORT_SYMBOL(blk_get_queue);
1da177e4 931
5b788ce3 932static inline void blk_free_request(struct request_list *rl, struct request *rq)
1da177e4 933{
e8064021 934 if (rq->rq_flags & RQF_ELVPRIV) {
5b788ce3 935 elv_put_request(rl->q, rq);
f1f8cc94 936 if (rq->elv.icq)
11a3122f 937 put_io_context(rq->elv.icq->ioc);
f1f8cc94
TH
938 }
939
5b788ce3 940 mempool_free(rq, rl->rq_pool);
1da177e4
LT
941}
942
1da177e4
LT
943/*
944 * ioc_batching returns true if the ioc is a valid batching request and
945 * should be given priority access to a request.
946 */
165125e1 947static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
1da177e4
LT
948{
949 if (!ioc)
950 return 0;
951
952 /*
953 * Make sure the process is able to allocate at least 1 request
954 * even if the batch times out, otherwise we could theoretically
955 * lose wakeups.
956 */
957 return ioc->nr_batch_requests == q->nr_batching ||
958 (ioc->nr_batch_requests > 0
959 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
960}
961
962/*
963 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
964 * will cause the process to be a "batcher" on all queues in the system. This
965 * is the behaviour we want though - once it gets a wakeup it should be given
966 * a nice run.
967 */
165125e1 968static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
1da177e4
LT
969{
970 if (!ioc || ioc_batching(q, ioc))
971 return;
972
973 ioc->nr_batch_requests = q->nr_batching;
974 ioc->last_waited = jiffies;
975}
976
5b788ce3 977static void __freed_request(struct request_list *rl, int sync)
1da177e4 978{
5b788ce3 979 struct request_queue *q = rl->q;
1da177e4 980
d40f75a0
TH
981 if (rl->count[sync] < queue_congestion_off_threshold(q))
982 blk_clear_congested(rl, sync);
1da177e4 983
1faa16d2
JA
984 if (rl->count[sync] + 1 <= q->nr_requests) {
985 if (waitqueue_active(&rl->wait[sync]))
986 wake_up(&rl->wait[sync]);
1da177e4 987
5b788ce3 988 blk_clear_rl_full(rl, sync);
1da177e4
LT
989 }
990}
991
992/*
993 * A request has just been released. Account for it, update the full and
994 * congestion status, wake up any waiters. Called under q->queue_lock.
995 */
e8064021
CH
996static void freed_request(struct request_list *rl, bool sync,
997 req_flags_t rq_flags)
1da177e4 998{
5b788ce3 999 struct request_queue *q = rl->q;
1da177e4 1000
8a5ecdd4 1001 q->nr_rqs[sync]--;
1faa16d2 1002 rl->count[sync]--;
e8064021 1003 if (rq_flags & RQF_ELVPRIV)
8a5ecdd4 1004 q->nr_rqs_elvpriv--;
1da177e4 1005
5b788ce3 1006 __freed_request(rl, sync);
1da177e4 1007
1faa16d2 1008 if (unlikely(rl->starved[sync ^ 1]))
5b788ce3 1009 __freed_request(rl, sync ^ 1);
1da177e4
LT
1010}
1011
e3a2b3f9
JA
1012int blk_update_nr_requests(struct request_queue *q, unsigned int nr)
1013{
1014 struct request_list *rl;
d40f75a0 1015 int on_thresh, off_thresh;
e3a2b3f9
JA
1016
1017 spin_lock_irq(q->queue_lock);
1018 q->nr_requests = nr;
1019 blk_queue_congestion_threshold(q);
d40f75a0
TH
1020 on_thresh = queue_congestion_on_threshold(q);
1021 off_thresh = queue_congestion_off_threshold(q);
e3a2b3f9 1022
d40f75a0
TH
1023 blk_queue_for_each_rl(rl, q) {
1024 if (rl->count[BLK_RW_SYNC] >= on_thresh)
1025 blk_set_congested(rl, BLK_RW_SYNC);
1026 else if (rl->count[BLK_RW_SYNC] < off_thresh)
1027 blk_clear_congested(rl, BLK_RW_SYNC);
e3a2b3f9 1028
d40f75a0
TH
1029 if (rl->count[BLK_RW_ASYNC] >= on_thresh)
1030 blk_set_congested(rl, BLK_RW_ASYNC);
1031 else if (rl->count[BLK_RW_ASYNC] < off_thresh)
1032 blk_clear_congested(rl, BLK_RW_ASYNC);
e3a2b3f9 1033
e3a2b3f9
JA
1034 if (rl->count[BLK_RW_SYNC] >= q->nr_requests) {
1035 blk_set_rl_full(rl, BLK_RW_SYNC);
1036 } else {
1037 blk_clear_rl_full(rl, BLK_RW_SYNC);
1038 wake_up(&rl->wait[BLK_RW_SYNC]);
1039 }
1040
1041 if (rl->count[BLK_RW_ASYNC] >= q->nr_requests) {
1042 blk_set_rl_full(rl, BLK_RW_ASYNC);
1043 } else {
1044 blk_clear_rl_full(rl, BLK_RW_ASYNC);
1045 wake_up(&rl->wait[BLK_RW_ASYNC]);
1046 }
1047 }
1048
1049 spin_unlock_irq(q->queue_lock);
1050 return 0;
1051}
1052
da8303c6 1053/**
a06e05e6 1054 * __get_request - get a free request
5b788ce3 1055 * @rl: request list to allocate from
ef295ecf 1056 * @op: operation and flags
da8303c6
TH
1057 * @bio: bio to allocate request for (can be %NULL)
1058 * @gfp_mask: allocation mask
1059 *
1060 * Get a free request from @q. This function may fail under memory
1061 * pressure or if @q is dead.
1062 *
da3dae54 1063 * Must be called with @q->queue_lock held and,
a492f075
JL
1064 * Returns ERR_PTR on failure, with @q->queue_lock held.
1065 * Returns request pointer on success, with @q->queue_lock *not held*.
1da177e4 1066 */
ef295ecf
CH
1067static struct request *__get_request(struct request_list *rl, unsigned int op,
1068 struct bio *bio, gfp_t gfp_mask)
1da177e4 1069{
5b788ce3 1070 struct request_queue *q = rl->q;
b679281a 1071 struct request *rq;
7f4b35d1
TH
1072 struct elevator_type *et = q->elevator->type;
1073 struct io_context *ioc = rq_ioc(bio);
f1f8cc94 1074 struct io_cq *icq = NULL;
ef295ecf 1075 const bool is_sync = op_is_sync(op);
75eb6c37 1076 int may_queue;
e8064021 1077 req_flags_t rq_flags = RQF_ALLOCED;
88ee5ef1 1078
3f3299d5 1079 if (unlikely(blk_queue_dying(q)))
a492f075 1080 return ERR_PTR(-ENODEV);
da8303c6 1081
ef295ecf 1082 may_queue = elv_may_queue(q, op);
88ee5ef1
JA
1083 if (may_queue == ELV_MQUEUE_NO)
1084 goto rq_starved;
1085
1faa16d2
JA
1086 if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) {
1087 if (rl->count[is_sync]+1 >= q->nr_requests) {
88ee5ef1
JA
1088 /*
1089 * The queue will fill after this allocation, so set
1090 * it as full, and mark this process as "batching".
1091 * This process will be allowed to complete a batch of
1092 * requests, others will be blocked.
1093 */
5b788ce3 1094 if (!blk_rl_full(rl, is_sync)) {
88ee5ef1 1095 ioc_set_batching(q, ioc);
5b788ce3 1096 blk_set_rl_full(rl, is_sync);
88ee5ef1
JA
1097 } else {
1098 if (may_queue != ELV_MQUEUE_MUST
1099 && !ioc_batching(q, ioc)) {
1100 /*
1101 * The queue is full and the allocating
1102 * process is not a "batcher", and not
1103 * exempted by the IO scheduler
1104 */
a492f075 1105 return ERR_PTR(-ENOMEM);
88ee5ef1
JA
1106 }
1107 }
1da177e4 1108 }
d40f75a0 1109 blk_set_congested(rl, is_sync);
1da177e4
LT
1110 }
1111
082cf69e
JA
1112 /*
1113 * Only allow batching queuers to allocate up to 50% over the defined
1114 * limit of requests, otherwise we could have thousands of requests
1115 * allocated with any setting of ->nr_requests
1116 */
1faa16d2 1117 if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
a492f075 1118 return ERR_PTR(-ENOMEM);
fd782a4a 1119
8a5ecdd4 1120 q->nr_rqs[is_sync]++;
1faa16d2
JA
1121 rl->count[is_sync]++;
1122 rl->starved[is_sync] = 0;
cb98fc8b 1123
f1f8cc94
TH
1124 /*
1125 * Decide whether the new request will be managed by elevator. If
e8064021 1126 * so, mark @rq_flags and increment elvpriv. Non-zero elvpriv will
f1f8cc94
TH
1127 * prevent the current elevator from being destroyed until the new
1128 * request is freed. This guarantees icq's won't be destroyed and
1129 * makes creating new ones safe.
1130 *
e6f7f93d
CH
1131 * Flush requests do not use the elevator so skip initialization.
1132 * This allows a request to share the flush and elevator data.
1133 *
f1f8cc94
TH
1134 * Also, lookup icq while holding queue_lock. If it doesn't exist,
1135 * it will be created after releasing queue_lock.
1136 */
e6f7f93d 1137 if (!op_is_flush(op) && !blk_queue_bypass(q)) {
e8064021 1138 rq_flags |= RQF_ELVPRIV;
8a5ecdd4 1139 q->nr_rqs_elvpriv++;
f1f8cc94
TH
1140 if (et->icq_cache && ioc)
1141 icq = ioc_lookup_icq(ioc, q);
9d5a4e94 1142 }
cb98fc8b 1143
f253b86b 1144 if (blk_queue_io_stat(q))
e8064021 1145 rq_flags |= RQF_IO_STAT;
1da177e4
LT
1146 spin_unlock_irq(q->queue_lock);
1147
29e2b09a 1148 /* allocate and init request */
5b788ce3 1149 rq = mempool_alloc(rl->rq_pool, gfp_mask);
29e2b09a 1150 if (!rq)
b679281a 1151 goto fail_alloc;
1da177e4 1152
29e2b09a 1153 blk_rq_init(q, rq);
a051661c 1154 blk_rq_set_rl(rq, rl);
ef295ecf 1155 rq->cmd_flags = op;
e8064021 1156 rq->rq_flags = rq_flags;
29e2b09a 1157
aaf7c680 1158 /* init elvpriv */
e8064021 1159 if (rq_flags & RQF_ELVPRIV) {
aaf7c680 1160 if (unlikely(et->icq_cache && !icq)) {
7f4b35d1
TH
1161 if (ioc)
1162 icq = ioc_create_icq(ioc, q, gfp_mask);
aaf7c680
TH
1163 if (!icq)
1164 goto fail_elvpriv;
29e2b09a 1165 }
aaf7c680
TH
1166
1167 rq->elv.icq = icq;
1168 if (unlikely(elv_set_request(q, rq, bio, gfp_mask)))
1169 goto fail_elvpriv;
1170
1171 /* @rq->elv.icq holds io_context until @rq is freed */
29e2b09a
TH
1172 if (icq)
1173 get_io_context(icq->ioc);
1174 }
aaf7c680 1175out:
88ee5ef1
JA
1176 /*
1177 * ioc may be NULL here, and ioc_batching will be false. That's
1178 * OK, if the queue is under the request limit then requests need
1179 * not count toward the nr_batch_requests limit. There will always
1180 * be some limit enforced by BLK_BATCH_TIME.
1181 */
1da177e4
LT
1182 if (ioc_batching(q, ioc))
1183 ioc->nr_batch_requests--;
6728cb0e 1184
e6a40b09 1185 trace_block_getrq(q, bio, op);
1da177e4 1186 return rq;
b679281a 1187
aaf7c680
TH
1188fail_elvpriv:
1189 /*
1190 * elvpriv init failed. ioc, icq and elvpriv aren't mempool backed
1191 * and may fail indefinitely under memory pressure and thus
1192 * shouldn't stall IO. Treat this request as !elvpriv. This will
1193 * disturb iosched and blkcg but weird is bettern than dead.
1194 */
7b2b10e0 1195 printk_ratelimited(KERN_WARNING "%s: dev %s: request aux data allocation failed, iosched may be disturbed\n",
dc3b17cc 1196 __func__, dev_name(q->backing_dev_info->dev));
aaf7c680 1197
e8064021 1198 rq->rq_flags &= ~RQF_ELVPRIV;
aaf7c680
TH
1199 rq->elv.icq = NULL;
1200
1201 spin_lock_irq(q->queue_lock);
8a5ecdd4 1202 q->nr_rqs_elvpriv--;
aaf7c680
TH
1203 spin_unlock_irq(q->queue_lock);
1204 goto out;
1205
b679281a
TH
1206fail_alloc:
1207 /*
1208 * Allocation failed presumably due to memory. Undo anything we
1209 * might have messed up.
1210 *
1211 * Allocating task should really be put onto the front of the wait
1212 * queue, but this is pretty rare.
1213 */
1214 spin_lock_irq(q->queue_lock);
e8064021 1215 freed_request(rl, is_sync, rq_flags);
b679281a
TH
1216
1217 /*
1218 * in the very unlikely event that allocation failed and no
1219 * requests for this direction was pending, mark us starved so that
1220 * freeing of a request in the other direction will notice
1221 * us. another possible fix would be to split the rq mempool into
1222 * READ and WRITE
1223 */
1224rq_starved:
1225 if (unlikely(rl->count[is_sync] == 0))
1226 rl->starved[is_sync] = 1;
a492f075 1227 return ERR_PTR(-ENOMEM);
1da177e4
LT
1228}
1229
da8303c6 1230/**
a06e05e6 1231 * get_request - get a free request
da8303c6 1232 * @q: request_queue to allocate request from
ef295ecf 1233 * @op: operation and flags
da8303c6 1234 * @bio: bio to allocate request for (can be %NULL)
a06e05e6 1235 * @gfp_mask: allocation mask
da8303c6 1236 *
d0164adc
MG
1237 * Get a free request from @q. If %__GFP_DIRECT_RECLAIM is set in @gfp_mask,
1238 * this function keeps retrying under memory pressure and fails iff @q is dead.
d6344532 1239 *
da3dae54 1240 * Must be called with @q->queue_lock held and,
a492f075
JL
1241 * Returns ERR_PTR on failure, with @q->queue_lock held.
1242 * Returns request pointer on success, with @q->queue_lock *not held*.
1da177e4 1243 */
ef295ecf
CH
1244static struct request *get_request(struct request_queue *q, unsigned int op,
1245 struct bio *bio, gfp_t gfp_mask)
1da177e4 1246{
ef295ecf 1247 const bool is_sync = op_is_sync(op);
a06e05e6 1248 DEFINE_WAIT(wait);
a051661c 1249 struct request_list *rl;
1da177e4 1250 struct request *rq;
a051661c
TH
1251
1252 rl = blk_get_rl(q, bio); /* transferred to @rq on success */
a06e05e6 1253retry:
ef295ecf 1254 rq = __get_request(rl, op, bio, gfp_mask);
a492f075 1255 if (!IS_ERR(rq))
a06e05e6 1256 return rq;
1da177e4 1257
d0164adc 1258 if (!gfpflags_allow_blocking(gfp_mask) || unlikely(blk_queue_dying(q))) {
a051661c 1259 blk_put_rl(rl);
a492f075 1260 return rq;
a051661c 1261 }
1da177e4 1262
a06e05e6
TH
1263 /* wait on @rl and retry */
1264 prepare_to_wait_exclusive(&rl->wait[is_sync], &wait,
1265 TASK_UNINTERRUPTIBLE);
1da177e4 1266
e6a40b09 1267 trace_block_sleeprq(q, bio, op);
1da177e4 1268
a06e05e6
TH
1269 spin_unlock_irq(q->queue_lock);
1270 io_schedule();
d6344532 1271
a06e05e6
TH
1272 /*
1273 * After sleeping, we become a "batching" process and will be able
1274 * to allocate at least one request, and up to a big batch of them
1275 * for a small period time. See ioc_batching, ioc_set_batching
1276 */
a06e05e6 1277 ioc_set_batching(q, current->io_context);
05caf8db 1278
a06e05e6
TH
1279 spin_lock_irq(q->queue_lock);
1280 finish_wait(&rl->wait[is_sync], &wait);
1da177e4 1281
a06e05e6 1282 goto retry;
1da177e4
LT
1283}
1284
320ae51f
JA
1285static struct request *blk_old_get_request(struct request_queue *q, int rw,
1286 gfp_t gfp_mask)
1da177e4
LT
1287{
1288 struct request *rq;
1289
7f4b35d1
TH
1290 /* create ioc upfront */
1291 create_io_context(gfp_mask, q->node);
1292
d6344532 1293 spin_lock_irq(q->queue_lock);
ef295ecf 1294 rq = get_request(q, rw, NULL, gfp_mask);
0c4de0f3 1295 if (IS_ERR(rq)) {
da8303c6 1296 spin_unlock_irq(q->queue_lock);
0c4de0f3
CH
1297 return rq;
1298 }
1da177e4 1299
0c4de0f3
CH
1300 /* q->queue_lock is unlocked at this point */
1301 rq->__data_len = 0;
1302 rq->__sector = (sector_t) -1;
1303 rq->bio = rq->biotail = NULL;
1da177e4
LT
1304 return rq;
1305}
320ae51f
JA
1306
1307struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
1308{
1309 if (q->mq_ops)
6f3b0e8b
CH
1310 return blk_mq_alloc_request(q, rw,
1311 (gfp_mask & __GFP_DIRECT_RECLAIM) ?
1312 0 : BLK_MQ_REQ_NOWAIT);
320ae51f
JA
1313 else
1314 return blk_old_get_request(q, rw, gfp_mask);
1315}
1da177e4
LT
1316EXPORT_SYMBOL(blk_get_request);
1317
1318/**
1319 * blk_requeue_request - put a request back on queue
1320 * @q: request queue where request should be inserted
1321 * @rq: request to be inserted
1322 *
1323 * Description:
1324 * Drivers often keep queueing requests until the hardware cannot accept
1325 * more, when that condition happens we need to put the request back
1326 * on the queue. Must be called with queue lock held.
1327 */
165125e1 1328void blk_requeue_request(struct request_queue *q, struct request *rq)
1da177e4 1329{
242f9dcb
JA
1330 blk_delete_timer(rq);
1331 blk_clear_rq_complete(rq);
5f3ea37c 1332 trace_block_rq_requeue(q, rq);
87760e5e 1333 wbt_requeue(q->rq_wb, &rq->issue_stat);
2056a782 1334
e8064021 1335 if (rq->rq_flags & RQF_QUEUED)
1da177e4
LT
1336 blk_queue_end_tag(q, rq);
1337
ba396a6c
JB
1338 BUG_ON(blk_queued_rq(rq));
1339
1da177e4
LT
1340 elv_requeue_request(q, rq);
1341}
1da177e4
LT
1342EXPORT_SYMBOL(blk_requeue_request);
1343
73c10101
JA
1344static void add_acct_request(struct request_queue *q, struct request *rq,
1345 int where)
1346{
320ae51f 1347 blk_account_io_start(rq, true);
7eaceacc 1348 __elv_add_request(q, rq, where);
73c10101
JA
1349}
1350
074a7aca
TH
1351static void part_round_stats_single(int cpu, struct hd_struct *part,
1352 unsigned long now)
1353{
7276d02e
JA
1354 int inflight;
1355
074a7aca
TH
1356 if (now == part->stamp)
1357 return;
1358
7276d02e
JA
1359 inflight = part_in_flight(part);
1360 if (inflight) {
074a7aca 1361 __part_stat_add(cpu, part, time_in_queue,
7276d02e 1362 inflight * (now - part->stamp));
074a7aca
TH
1363 __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
1364 }
1365 part->stamp = now;
1366}
1367
1368/**
496aa8a9
RD
1369 * part_round_stats() - Round off the performance stats on a struct disk_stats.
1370 * @cpu: cpu number for stats access
1371 * @part: target partition
1da177e4
LT
1372 *
1373 * The average IO queue length and utilisation statistics are maintained
1374 * by observing the current state of the queue length and the amount of
1375 * time it has been in this state for.
1376 *
1377 * Normally, that accounting is done on IO completion, but that can result
1378 * in more than a second's worth of IO being accounted for within any one
1379 * second, leading to >100% utilisation. To deal with that, we call this
1380 * function to do a round-off before returning the results when reading
1381 * /proc/diskstats. This accounts immediately for all queue usage up to
1382 * the current jiffies and restarts the counters again.
1383 */
c9959059 1384void part_round_stats(int cpu, struct hd_struct *part)
6f2576af
JM
1385{
1386 unsigned long now = jiffies;
1387
074a7aca
TH
1388 if (part->partno)
1389 part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
1390 part_round_stats_single(cpu, part, now);
6f2576af 1391}
074a7aca 1392EXPORT_SYMBOL_GPL(part_round_stats);
6f2576af 1393
47fafbc7 1394#ifdef CONFIG_PM
c8158819
LM
1395static void blk_pm_put_request(struct request *rq)
1396{
e8064021 1397 if (rq->q->dev && !(rq->rq_flags & RQF_PM) && !--rq->q->nr_pending)
c8158819
LM
1398 pm_runtime_mark_last_busy(rq->q->dev);
1399}
1400#else
1401static inline void blk_pm_put_request(struct request *rq) {}
1402#endif
1403
1da177e4
LT
1404/*
1405 * queue lock must be held
1406 */
165125e1 1407void __blk_put_request(struct request_queue *q, struct request *req)
1da177e4 1408{
e8064021
CH
1409 req_flags_t rq_flags = req->rq_flags;
1410
1da177e4
LT
1411 if (unlikely(!q))
1412 return;
1da177e4 1413
6f5ba581
CH
1414 if (q->mq_ops) {
1415 blk_mq_free_request(req);
1416 return;
1417 }
1418
c8158819
LM
1419 blk_pm_put_request(req);
1420
8922e16c
TH
1421 elv_completed_request(q, req);
1422
1cd96c24
BH
1423 /* this is a bio leak */
1424 WARN_ON(req->bio != NULL);
1425
87760e5e
JA
1426 wbt_done(q->rq_wb, &req->issue_stat);
1427
1da177e4
LT
1428 /*
1429 * Request may not have originated from ll_rw_blk. if not,
1430 * it didn't come out of our reserved rq pools
1431 */
e8064021 1432 if (rq_flags & RQF_ALLOCED) {
a051661c 1433 struct request_list *rl = blk_rq_rl(req);
ef295ecf 1434 bool sync = op_is_sync(req->cmd_flags);
1da177e4 1435
1da177e4 1436 BUG_ON(!list_empty(&req->queuelist));
360f92c2 1437 BUG_ON(ELV_ON_HASH(req));
1da177e4 1438
a051661c 1439 blk_free_request(rl, req);
e8064021 1440 freed_request(rl, sync, rq_flags);
a051661c 1441 blk_put_rl(rl);
1da177e4
LT
1442 }
1443}
6e39b69e
MC
1444EXPORT_SYMBOL_GPL(__blk_put_request);
1445
1da177e4
LT
1446void blk_put_request(struct request *req)
1447{
165125e1 1448 struct request_queue *q = req->q;
8922e16c 1449
320ae51f
JA
1450 if (q->mq_ops)
1451 blk_mq_free_request(req);
1452 else {
1453 unsigned long flags;
1454
1455 spin_lock_irqsave(q->queue_lock, flags);
1456 __blk_put_request(q, req);
1457 spin_unlock_irqrestore(q->queue_lock, flags);
1458 }
1da177e4 1459}
1da177e4
LT
1460EXPORT_SYMBOL(blk_put_request);
1461
320ae51f
JA
1462bool bio_attempt_back_merge(struct request_queue *q, struct request *req,
1463 struct bio *bio)
73c10101 1464{
1eff9d32 1465 const int ff = bio->bi_opf & REQ_FAILFAST_MASK;
73c10101 1466
73c10101
JA
1467 if (!ll_back_merge_fn(q, req, bio))
1468 return false;
1469
8c1cf6bb 1470 trace_block_bio_backmerge(q, req, bio);
73c10101
JA
1471
1472 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1473 blk_rq_set_mixed_merge(req);
1474
1475 req->biotail->bi_next = bio;
1476 req->biotail = bio;
4f024f37 1477 req->__data_len += bio->bi_iter.bi_size;
73c10101
JA
1478 req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
1479
320ae51f 1480 blk_account_io_start(req, false);
73c10101
JA
1481 return true;
1482}
1483
320ae51f
JA
1484bool bio_attempt_front_merge(struct request_queue *q, struct request *req,
1485 struct bio *bio)
73c10101 1486{
1eff9d32 1487 const int ff = bio->bi_opf & REQ_FAILFAST_MASK;
73c10101 1488
73c10101
JA
1489 if (!ll_front_merge_fn(q, req, bio))
1490 return false;
1491
8c1cf6bb 1492 trace_block_bio_frontmerge(q, req, bio);
73c10101
JA
1493
1494 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1495 blk_rq_set_mixed_merge(req);
1496
73c10101
JA
1497 bio->bi_next = req->bio;
1498 req->bio = bio;
1499
4f024f37
KO
1500 req->__sector = bio->bi_iter.bi_sector;
1501 req->__data_len += bio->bi_iter.bi_size;
73c10101
JA
1502 req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
1503
320ae51f 1504 blk_account_io_start(req, false);
73c10101
JA
1505 return true;
1506}
1507
1e739730
CH
1508bool bio_attempt_discard_merge(struct request_queue *q, struct request *req,
1509 struct bio *bio)
1510{
1511 unsigned short segments = blk_rq_nr_discard_segments(req);
1512
1513 if (segments >= queue_max_discard_segments(q))
1514 goto no_merge;
1515 if (blk_rq_sectors(req) + bio_sectors(bio) >
1516 blk_rq_get_max_sectors(req, blk_rq_pos(req)))
1517 goto no_merge;
1518
1519 req->biotail->bi_next = bio;
1520 req->biotail = bio;
1521 req->__data_len += bio->bi_iter.bi_size;
1522 req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
1523 req->nr_phys_segments = segments + 1;
1524
1525 blk_account_io_start(req, false);
1526 return true;
1527no_merge:
1528 req_set_nomerge(q, req);
1529 return false;
1530}
1531
bd87b589 1532/**
320ae51f 1533 * blk_attempt_plug_merge - try to merge with %current's plugged list
bd87b589
TH
1534 * @q: request_queue new bio is being queued at
1535 * @bio: new bio being queued
1536 * @request_count: out parameter for number of traversed plugged requests
ccc2600b
RD
1537 * @same_queue_rq: pointer to &struct request that gets filled in when
1538 * another request associated with @q is found on the plug list
1539 * (optional, may be %NULL)
bd87b589
TH
1540 *
1541 * Determine whether @bio being queued on @q can be merged with a request
1542 * on %current's plugged list. Returns %true if merge was successful,
1543 * otherwise %false.
1544 *
07c2bd37
TH
1545 * Plugging coalesces IOs from the same issuer for the same purpose without
1546 * going through @q->queue_lock. As such it's more of an issuing mechanism
1547 * than scheduling, and the request, while may have elvpriv data, is not
1548 * added on the elevator at this point. In addition, we don't have
1549 * reliable access to the elevator outside queue lock. Only check basic
1550 * merging parameters without querying the elevator.
da41a589
RE
1551 *
1552 * Caller must ensure !blk_queue_nomerges(q) beforehand.
73c10101 1553 */
320ae51f 1554bool blk_attempt_plug_merge(struct request_queue *q, struct bio *bio,
5b3f341f
SL
1555 unsigned int *request_count,
1556 struct request **same_queue_rq)
73c10101
JA
1557{
1558 struct blk_plug *plug;
1559 struct request *rq;
92f399c7 1560 struct list_head *plug_list;
73c10101 1561
bd87b589 1562 plug = current->plug;
73c10101 1563 if (!plug)
34fe7c05 1564 return false;
56ebdaf2 1565 *request_count = 0;
73c10101 1566
92f399c7
SL
1567 if (q->mq_ops)
1568 plug_list = &plug->mq_list;
1569 else
1570 plug_list = &plug->list;
1571
1572 list_for_each_entry_reverse(rq, plug_list, queuelist) {
34fe7c05 1573 bool merged = false;
73c10101 1574
5b3f341f 1575 if (rq->q == q) {
1b2e19f1 1576 (*request_count)++;
5b3f341f
SL
1577 /*
1578 * Only blk-mq multiple hardware queues case checks the
1579 * rq in the same queue, there should be only one such
1580 * rq in a queue
1581 **/
1582 if (same_queue_rq)
1583 *same_queue_rq = rq;
1584 }
56ebdaf2 1585
07c2bd37 1586 if (rq->q != q || !blk_rq_merge_ok(rq, bio))
73c10101
JA
1587 continue;
1588
34fe7c05
CH
1589 switch (blk_try_merge(rq, bio)) {
1590 case ELEVATOR_BACK_MERGE:
1591 merged = bio_attempt_back_merge(q, rq, bio);
1592 break;
1593 case ELEVATOR_FRONT_MERGE:
1594 merged = bio_attempt_front_merge(q, rq, bio);
1595 break;
1e739730
CH
1596 case ELEVATOR_DISCARD_MERGE:
1597 merged = bio_attempt_discard_merge(q, rq, bio);
1598 break;
34fe7c05
CH
1599 default:
1600 break;
73c10101 1601 }
34fe7c05
CH
1602
1603 if (merged)
1604 return true;
73c10101 1605 }
34fe7c05
CH
1606
1607 return false;
73c10101
JA
1608}
1609
0809e3ac
JM
1610unsigned int blk_plug_queued_count(struct request_queue *q)
1611{
1612 struct blk_plug *plug;
1613 struct request *rq;
1614 struct list_head *plug_list;
1615 unsigned int ret = 0;
1616
1617 plug = current->plug;
1618 if (!plug)
1619 goto out;
1620
1621 if (q->mq_ops)
1622 plug_list = &plug->mq_list;
1623 else
1624 plug_list = &plug->list;
1625
1626 list_for_each_entry(rq, plug_list, queuelist) {
1627 if (rq->q == q)
1628 ret++;
1629 }
1630out:
1631 return ret;
1632}
1633
da8d7f07 1634void blk_init_request_from_bio(struct request *req, struct bio *bio)
52d9e675 1635{
0be0dee6
BVA
1636 struct io_context *ioc = rq_ioc(bio);
1637
1eff9d32 1638 if (bio->bi_opf & REQ_RAHEAD)
a82afdfc 1639 req->cmd_flags |= REQ_FAILFAST_MASK;
b31dc66a 1640
4f024f37 1641 req->__sector = bio->bi_iter.bi_sector;
5dc8b362
AM
1642 if (ioprio_valid(bio_prio(bio)))
1643 req->ioprio = bio_prio(bio);
0be0dee6
BVA
1644 else if (ioc)
1645 req->ioprio = ioc->ioprio;
1646 else
1647 req->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, 0);
bc1c56fd 1648 blk_rq_bio_prep(req->q, req, bio);
52d9e675 1649}
da8d7f07 1650EXPORT_SYMBOL_GPL(blk_init_request_from_bio);
52d9e675 1651
dece1635 1652static blk_qc_t blk_queue_bio(struct request_queue *q, struct bio *bio)
1da177e4 1653{
73c10101 1654 struct blk_plug *plug;
34fe7c05 1655 int where = ELEVATOR_INSERT_SORT;
e4d750c9 1656 struct request *req, *free;
56ebdaf2 1657 unsigned int request_count = 0;
87760e5e 1658 unsigned int wb_acct;
1da177e4 1659
1da177e4
LT
1660 /*
1661 * low level driver can indicate that it wants pages above a
1662 * certain limit bounced to low memory (ie for highmem, or even
1663 * ISA dma in theory)
1664 */
1665 blk_queue_bounce(q, &bio);
1666
23688bf4
JN
1667 blk_queue_split(q, &bio, q->bio_split);
1668
ffecfd1a 1669 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
4246a0b6
CH
1670 bio->bi_error = -EIO;
1671 bio_endio(bio);
dece1635 1672 return BLK_QC_T_NONE;
ffecfd1a
DW
1673 }
1674
f73f44eb 1675 if (op_is_flush(bio->bi_opf)) {
73c10101 1676 spin_lock_irq(q->queue_lock);
ae1b1539 1677 where = ELEVATOR_INSERT_FLUSH;
28e7d184
TH
1678 goto get_rq;
1679 }
1680
73c10101
JA
1681 /*
1682 * Check if we can merge with the plugged list before grabbing
1683 * any locks.
1684 */
0809e3ac
JM
1685 if (!blk_queue_nomerges(q)) {
1686 if (blk_attempt_plug_merge(q, bio, &request_count, NULL))
dece1635 1687 return BLK_QC_T_NONE;
0809e3ac
JM
1688 } else
1689 request_count = blk_plug_queued_count(q);
1da177e4 1690
73c10101 1691 spin_lock_irq(q->queue_lock);
2056a782 1692
34fe7c05
CH
1693 switch (elv_merge(q, &req, bio)) {
1694 case ELEVATOR_BACK_MERGE:
1695 if (!bio_attempt_back_merge(q, req, bio))
1696 break;
1697 elv_bio_merged(q, req, bio);
1698 free = attempt_back_merge(q, req);
1699 if (free)
1700 __blk_put_request(q, free);
1701 else
1702 elv_merged_request(q, req, ELEVATOR_BACK_MERGE);
1703 goto out_unlock;
1704 case ELEVATOR_FRONT_MERGE:
1705 if (!bio_attempt_front_merge(q, req, bio))
1706 break;
1707 elv_bio_merged(q, req, bio);
1708 free = attempt_front_merge(q, req);
1709 if (free)
1710 __blk_put_request(q, free);
1711 else
1712 elv_merged_request(q, req, ELEVATOR_FRONT_MERGE);
1713 goto out_unlock;
1714 default:
1715 break;
1da177e4
LT
1716 }
1717
450991bc 1718get_rq:
87760e5e
JA
1719 wb_acct = wbt_wait(q->rq_wb, bio, q->queue_lock);
1720
1da177e4 1721 /*
450991bc 1722 * Grab a free request. This is might sleep but can not fail.
d6344532 1723 * Returns with the queue unlocked.
450991bc 1724 */
ef295ecf 1725 req = get_request(q, bio->bi_opf, bio, GFP_NOIO);
a492f075 1726 if (IS_ERR(req)) {
87760e5e 1727 __wbt_done(q->rq_wb, wb_acct);
4246a0b6
CH
1728 bio->bi_error = PTR_ERR(req);
1729 bio_endio(bio);
da8303c6
TH
1730 goto out_unlock;
1731 }
d6344532 1732
87760e5e
JA
1733 wbt_track(&req->issue_stat, wb_acct);
1734
450991bc
NP
1735 /*
1736 * After dropping the lock and possibly sleeping here, our request
1737 * may now be mergeable after it had proven unmergeable (above).
1738 * We don't worry about that case for efficiency. It won't happen
1739 * often, and the elevators are able to handle it.
1da177e4 1740 */
da8d7f07 1741 blk_init_request_from_bio(req, bio);
1da177e4 1742
9562ad9a 1743 if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags))
11ccf116 1744 req->cpu = raw_smp_processor_id();
73c10101
JA
1745
1746 plug = current->plug;
721a9602 1747 if (plug) {
dc6d36c9
JA
1748 /*
1749 * If this is the first request added after a plug, fire
7aef2e78 1750 * of a plug trace.
0a6219a9
ML
1751 *
1752 * @request_count may become stale because of schedule
1753 * out, so check plug list again.
dc6d36c9 1754 */
0a6219a9 1755 if (!request_count || list_empty(&plug->list))
dc6d36c9 1756 trace_block_plug(q);
3540d5e8 1757 else {
50d24c34
SL
1758 struct request *last = list_entry_rq(plug->list.prev);
1759 if (request_count >= BLK_MAX_REQUEST_COUNT ||
1760 blk_rq_bytes(last) >= BLK_PLUG_FLUSH_SIZE) {
3540d5e8 1761 blk_flush_plug_list(plug, false);
019ceb7d
SL
1762 trace_block_plug(q);
1763 }
73c10101 1764 }
73c10101 1765 list_add_tail(&req->queuelist, &plug->list);
320ae51f 1766 blk_account_io_start(req, true);
73c10101
JA
1767 } else {
1768 spin_lock_irq(q->queue_lock);
1769 add_acct_request(q, req, where);
24ecfbe2 1770 __blk_run_queue(q);
73c10101
JA
1771out_unlock:
1772 spin_unlock_irq(q->queue_lock);
1773 }
dece1635
JA
1774
1775 return BLK_QC_T_NONE;
1da177e4
LT
1776}
1777
1778/*
1779 * If bio->bi_dev is a partition, remap the location
1780 */
1781static inline void blk_partition_remap(struct bio *bio)
1782{
1783 struct block_device *bdev = bio->bi_bdev;
1784
778889d8
ST
1785 /*
1786 * Zone reset does not include bi_size so bio_sectors() is always 0.
1787 * Include a test for the reset op code and perform the remap if needed.
1788 */
1789 if (bdev != bdev->bd_contains &&
1790 (bio_sectors(bio) || bio_op(bio) == REQ_OP_ZONE_RESET)) {
1da177e4
LT
1791 struct hd_struct *p = bdev->bd_part;
1792
4f024f37 1793 bio->bi_iter.bi_sector += p->start_sect;
1da177e4 1794 bio->bi_bdev = bdev->bd_contains;
c7149d6b 1795
d07335e5
MS
1796 trace_block_bio_remap(bdev_get_queue(bio->bi_bdev), bio,
1797 bdev->bd_dev,
4f024f37 1798 bio->bi_iter.bi_sector - p->start_sect);
1da177e4
LT
1799 }
1800}
1801
1da177e4
LT
1802static void handle_bad_sector(struct bio *bio)
1803{
1804 char b[BDEVNAME_SIZE];
1805
1806 printk(KERN_INFO "attempt to access beyond end of device\n");
6296b960 1807 printk(KERN_INFO "%s: rw=%d, want=%Lu, limit=%Lu\n",
1da177e4 1808 bdevname(bio->bi_bdev, b),
1eff9d32 1809 bio->bi_opf,
f73a1c7d 1810 (unsigned long long)bio_end_sector(bio),
77304d2a 1811 (long long)(i_size_read(bio->bi_bdev->bd_inode) >> 9));
1da177e4
LT
1812}
1813
c17bb495
AM
1814#ifdef CONFIG_FAIL_MAKE_REQUEST
1815
1816static DECLARE_FAULT_ATTR(fail_make_request);
1817
1818static int __init setup_fail_make_request(char *str)
1819{
1820 return setup_fault_attr(&fail_make_request, str);
1821}
1822__setup("fail_make_request=", setup_fail_make_request);
1823
b2c9cd37 1824static bool should_fail_request(struct hd_struct *part, unsigned int bytes)
c17bb495 1825{
b2c9cd37 1826 return part->make_it_fail && should_fail(&fail_make_request, bytes);
c17bb495
AM
1827}
1828
1829static int __init fail_make_request_debugfs(void)
1830{
dd48c085
AM
1831 struct dentry *dir = fault_create_debugfs_attr("fail_make_request",
1832 NULL, &fail_make_request);
1833
21f9fcd8 1834 return PTR_ERR_OR_ZERO(dir);
c17bb495
AM
1835}
1836
1837late_initcall(fail_make_request_debugfs);
1838
1839#else /* CONFIG_FAIL_MAKE_REQUEST */
1840
b2c9cd37
AM
1841static inline bool should_fail_request(struct hd_struct *part,
1842 unsigned int bytes)
c17bb495 1843{
b2c9cd37 1844 return false;
c17bb495
AM
1845}
1846
1847#endif /* CONFIG_FAIL_MAKE_REQUEST */
1848
c07e2b41
JA
1849/*
1850 * Check whether this bio extends beyond the end of the device.
1851 */
1852static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1853{
1854 sector_t maxsector;
1855
1856 if (!nr_sectors)
1857 return 0;
1858
1859 /* Test device or partition size, when known. */
77304d2a 1860 maxsector = i_size_read(bio->bi_bdev->bd_inode) >> 9;
c07e2b41 1861 if (maxsector) {
4f024f37 1862 sector_t sector = bio->bi_iter.bi_sector;
c07e2b41
JA
1863
1864 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1865 /*
1866 * This may well happen - the kernel calls bread()
1867 * without checking the size of the device, e.g., when
1868 * mounting a device.
1869 */
1870 handle_bad_sector(bio);
1871 return 1;
1872 }
1873 }
1874
1875 return 0;
1876}
1877
27a84d54
CH
1878static noinline_for_stack bool
1879generic_make_request_checks(struct bio *bio)
1da177e4 1880{
165125e1 1881 struct request_queue *q;
5a7bbad2 1882 int nr_sectors = bio_sectors(bio);
51fd77bd 1883 int err = -EIO;
5a7bbad2
CH
1884 char b[BDEVNAME_SIZE];
1885 struct hd_struct *part;
1da177e4
LT
1886
1887 might_sleep();
1da177e4 1888
c07e2b41
JA
1889 if (bio_check_eod(bio, nr_sectors))
1890 goto end_io;
1da177e4 1891
5a7bbad2
CH
1892 q = bdev_get_queue(bio->bi_bdev);
1893 if (unlikely(!q)) {
1894 printk(KERN_ERR
1895 "generic_make_request: Trying to access "
1896 "nonexistent block-device %s (%Lu)\n",
1897 bdevname(bio->bi_bdev, b),
4f024f37 1898 (long long) bio->bi_iter.bi_sector);
5a7bbad2
CH
1899 goto end_io;
1900 }
c17bb495 1901
5a7bbad2 1902 part = bio->bi_bdev->bd_part;
4f024f37 1903 if (should_fail_request(part, bio->bi_iter.bi_size) ||
5a7bbad2 1904 should_fail_request(&part_to_disk(part)->part0,
4f024f37 1905 bio->bi_iter.bi_size))
5a7bbad2 1906 goto end_io;
2056a782 1907
5a7bbad2
CH
1908 /*
1909 * If this device has partitions, remap block n
1910 * of partition p to block n+start(p) of the disk.
1911 */
1912 blk_partition_remap(bio);
2056a782 1913
5a7bbad2
CH
1914 if (bio_check_eod(bio, nr_sectors))
1915 goto end_io;
1e87901e 1916
5a7bbad2
CH
1917 /*
1918 * Filter flush bio's early so that make_request based
1919 * drivers without flush support don't have to worry
1920 * about them.
1921 */
f3a8ab7d 1922 if (op_is_flush(bio->bi_opf) &&
c888a8f9 1923 !test_bit(QUEUE_FLAG_WC, &q->queue_flags)) {
1eff9d32 1924 bio->bi_opf &= ~(REQ_PREFLUSH | REQ_FUA);
5a7bbad2
CH
1925 if (!nr_sectors) {
1926 err = 0;
51fd77bd
JA
1927 goto end_io;
1928 }
5a7bbad2 1929 }
5ddfe969 1930
288dab8a
CH
1931 switch (bio_op(bio)) {
1932 case REQ_OP_DISCARD:
1933 if (!blk_queue_discard(q))
1934 goto not_supported;
1935 break;
1936 case REQ_OP_SECURE_ERASE:
1937 if (!blk_queue_secure_erase(q))
1938 goto not_supported;
1939 break;
1940 case REQ_OP_WRITE_SAME:
1941 if (!bdev_write_same(bio->bi_bdev))
1942 goto not_supported;
58886785 1943 break;
2d253440
ST
1944 case REQ_OP_ZONE_REPORT:
1945 case REQ_OP_ZONE_RESET:
1946 if (!bdev_is_zoned(bio->bi_bdev))
1947 goto not_supported;
288dab8a 1948 break;
a6f0788e
CK
1949 case REQ_OP_WRITE_ZEROES:
1950 if (!bdev_write_zeroes_sectors(bio->bi_bdev))
1951 goto not_supported;
1952 break;
288dab8a
CH
1953 default:
1954 break;
5a7bbad2 1955 }
01edede4 1956
7f4b35d1
TH
1957 /*
1958 * Various block parts want %current->io_context and lazy ioc
1959 * allocation ends up trading a lot of pain for a small amount of
1960 * memory. Just allocate it upfront. This may fail and block
1961 * layer knows how to live with it.
1962 */
1963 create_io_context(GFP_ATOMIC, q->node);
1964
ae118896
TH
1965 if (!blkcg_bio_issue_check(q, bio))
1966 return false;
27a84d54 1967
fbbaf700
N
1968 if (!bio_flagged(bio, BIO_TRACE_COMPLETION)) {
1969 trace_block_bio_queue(q, bio);
1970 /* Now that enqueuing has been traced, we need to trace
1971 * completion as well.
1972 */
1973 bio_set_flag(bio, BIO_TRACE_COMPLETION);
1974 }
27a84d54 1975 return true;
a7384677 1976
288dab8a
CH
1977not_supported:
1978 err = -EOPNOTSUPP;
a7384677 1979end_io:
4246a0b6
CH
1980 bio->bi_error = err;
1981 bio_endio(bio);
27a84d54 1982 return false;
1da177e4
LT
1983}
1984
27a84d54
CH
1985/**
1986 * generic_make_request - hand a buffer to its device driver for I/O
1987 * @bio: The bio describing the location in memory and on the device.
1988 *
1989 * generic_make_request() is used to make I/O requests of block
1990 * devices. It is passed a &struct bio, which describes the I/O that needs
1991 * to be done.
1992 *
1993 * generic_make_request() does not return any status. The
1994 * success/failure status of the request, along with notification of
1995 * completion, is delivered asynchronously through the bio->bi_end_io
1996 * function described (one day) else where.
1997 *
1998 * The caller of generic_make_request must make sure that bi_io_vec
1999 * are set to describe the memory buffer, and that bi_dev and bi_sector are
2000 * set to describe the device address, and the
2001 * bi_end_io and optionally bi_private are set to describe how
2002 * completion notification should be signaled.
2003 *
2004 * generic_make_request and the drivers it calls may use bi_next if this
2005 * bio happens to be merged with someone else, and may resubmit the bio to
2006 * a lower device by calling into generic_make_request recursively, which
2007 * means the bio should NOT be touched after the call to ->make_request_fn.
d89d8796 2008 */
dece1635 2009blk_qc_t generic_make_request(struct bio *bio)
d89d8796 2010{
f5fe1b51
N
2011 /*
2012 * bio_list_on_stack[0] contains bios submitted by the current
2013 * make_request_fn.
2014 * bio_list_on_stack[1] contains bios that were submitted before
2015 * the current make_request_fn, but that haven't been processed
2016 * yet.
2017 */
2018 struct bio_list bio_list_on_stack[2];
dece1635 2019 blk_qc_t ret = BLK_QC_T_NONE;
bddd87c7 2020
27a84d54 2021 if (!generic_make_request_checks(bio))
dece1635 2022 goto out;
27a84d54
CH
2023
2024 /*
2025 * We only want one ->make_request_fn to be active at a time, else
2026 * stack usage with stacked devices could be a problem. So use
2027 * current->bio_list to keep a list of requests submited by a
2028 * make_request_fn function. current->bio_list is also used as a
2029 * flag to say if generic_make_request is currently active in this
2030 * task or not. If it is NULL, then no make_request is active. If
2031 * it is non-NULL, then a make_request is active, and new requests
2032 * should be added at the tail
2033 */
bddd87c7 2034 if (current->bio_list) {
f5fe1b51 2035 bio_list_add(&current->bio_list[0], bio);
dece1635 2036 goto out;
d89d8796 2037 }
27a84d54 2038
d89d8796
NB
2039 /* following loop may be a bit non-obvious, and so deserves some
2040 * explanation.
2041 * Before entering the loop, bio->bi_next is NULL (as all callers
2042 * ensure that) so we have a list with a single bio.
2043 * We pretend that we have just taken it off a longer list, so
bddd87c7
AM
2044 * we assign bio_list to a pointer to the bio_list_on_stack,
2045 * thus initialising the bio_list of new bios to be
27a84d54 2046 * added. ->make_request() may indeed add some more bios
d89d8796
NB
2047 * through a recursive call to generic_make_request. If it
2048 * did, we find a non-NULL value in bio_list and re-enter the loop
2049 * from the top. In this case we really did just take the bio
bddd87c7 2050 * of the top of the list (no pretending) and so remove it from
27a84d54 2051 * bio_list, and call into ->make_request() again.
d89d8796
NB
2052 */
2053 BUG_ON(bio->bi_next);
f5fe1b51
N
2054 bio_list_init(&bio_list_on_stack[0]);
2055 current->bio_list = bio_list_on_stack;
d89d8796 2056 do {
27a84d54
CH
2057 struct request_queue *q = bdev_get_queue(bio->bi_bdev);
2058
6f3b0e8b 2059 if (likely(blk_queue_enter(q, false) == 0)) {
79bd9959
N
2060 struct bio_list lower, same;
2061
2062 /* Create a fresh bio_list for all subordinate requests */
f5fe1b51
N
2063 bio_list_on_stack[1] = bio_list_on_stack[0];
2064 bio_list_init(&bio_list_on_stack[0]);
dece1635 2065 ret = q->make_request_fn(q, bio);
3ef28e83
DW
2066
2067 blk_queue_exit(q);
27a84d54 2068
79bd9959
N
2069 /* sort new bios into those for a lower level
2070 * and those for the same level
2071 */
2072 bio_list_init(&lower);
2073 bio_list_init(&same);
f5fe1b51 2074 while ((bio = bio_list_pop(&bio_list_on_stack[0])) != NULL)
79bd9959
N
2075 if (q == bdev_get_queue(bio->bi_bdev))
2076 bio_list_add(&same, bio);
2077 else
2078 bio_list_add(&lower, bio);
2079 /* now assemble so we handle the lowest level first */
f5fe1b51
N
2080 bio_list_merge(&bio_list_on_stack[0], &lower);
2081 bio_list_merge(&bio_list_on_stack[0], &same);
2082 bio_list_merge(&bio_list_on_stack[0], &bio_list_on_stack[1]);
3ef28e83 2083 } else {
3ef28e83 2084 bio_io_error(bio);
3ef28e83 2085 }
f5fe1b51 2086 bio = bio_list_pop(&bio_list_on_stack[0]);
d89d8796 2087 } while (bio);
bddd87c7 2088 current->bio_list = NULL; /* deactivate */
dece1635
JA
2089
2090out:
2091 return ret;
d89d8796 2092}
1da177e4
LT
2093EXPORT_SYMBOL(generic_make_request);
2094
2095/**
710027a4 2096 * submit_bio - submit a bio to the block device layer for I/O
1da177e4
LT
2097 * @bio: The &struct bio which describes the I/O
2098 *
2099 * submit_bio() is very similar in purpose to generic_make_request(), and
2100 * uses that function to do most of the work. Both are fairly rough
710027a4 2101 * interfaces; @bio must be presetup and ready for I/O.
1da177e4
LT
2102 *
2103 */
4e49ea4a 2104blk_qc_t submit_bio(struct bio *bio)
1da177e4 2105{
bf2de6f5
JA
2106 /*
2107 * If it's a regular read/write or a barrier with data attached,
2108 * go through the normal accounting stuff before submission.
2109 */
e2a60da7 2110 if (bio_has_data(bio)) {
4363ac7c
MP
2111 unsigned int count;
2112
95fe6c1a 2113 if (unlikely(bio_op(bio) == REQ_OP_WRITE_SAME))
4363ac7c
MP
2114 count = bdev_logical_block_size(bio->bi_bdev) >> 9;
2115 else
2116 count = bio_sectors(bio);
2117
a8ebb056 2118 if (op_is_write(bio_op(bio))) {
bf2de6f5
JA
2119 count_vm_events(PGPGOUT, count);
2120 } else {
4f024f37 2121 task_io_account_read(bio->bi_iter.bi_size);
bf2de6f5
JA
2122 count_vm_events(PGPGIN, count);
2123 }
2124
2125 if (unlikely(block_dump)) {
2126 char b[BDEVNAME_SIZE];
8dcbdc74 2127 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s (%u sectors)\n",
ba25f9dc 2128 current->comm, task_pid_nr(current),
a8ebb056 2129 op_is_write(bio_op(bio)) ? "WRITE" : "READ",
4f024f37 2130 (unsigned long long)bio->bi_iter.bi_sector,
8dcbdc74
SM
2131 bdevname(bio->bi_bdev, b),
2132 count);
bf2de6f5 2133 }
1da177e4
LT
2134 }
2135
dece1635 2136 return generic_make_request(bio);
1da177e4 2137}
1da177e4
LT
2138EXPORT_SYMBOL(submit_bio);
2139
82124d60 2140/**
bf4e6b4e
HR
2141 * blk_cloned_rq_check_limits - Helper function to check a cloned request
2142 * for new the queue limits
82124d60
KU
2143 * @q: the queue
2144 * @rq: the request being checked
2145 *
2146 * Description:
2147 * @rq may have been made based on weaker limitations of upper-level queues
2148 * in request stacking drivers, and it may violate the limitation of @q.
2149 * Since the block layer and the underlying device driver trust @rq
2150 * after it is inserted to @q, it should be checked against @q before
2151 * the insertion using this generic function.
2152 *
82124d60 2153 * Request stacking drivers like request-based dm may change the queue
bf4e6b4e
HR
2154 * limits when retrying requests on other queues. Those requests need
2155 * to be checked against the new queue limits again during dispatch.
82124d60 2156 */
bf4e6b4e
HR
2157static int blk_cloned_rq_check_limits(struct request_queue *q,
2158 struct request *rq)
82124d60 2159{
8fe0d473 2160 if (blk_rq_sectors(rq) > blk_queue_get_max_sectors(q, req_op(rq))) {
82124d60
KU
2161 printk(KERN_ERR "%s: over max size limit.\n", __func__);
2162 return -EIO;
2163 }
2164
2165 /*
2166 * queue's settings related to segment counting like q->bounce_pfn
2167 * may differ from that of other stacking queues.
2168 * Recalculate it to check the request correctly on this queue's
2169 * limitation.
2170 */
2171 blk_recalc_rq_segments(rq);
8a78362c 2172 if (rq->nr_phys_segments > queue_max_segments(q)) {
82124d60
KU
2173 printk(KERN_ERR "%s: over max segments limit.\n", __func__);
2174 return -EIO;
2175 }
2176
2177 return 0;
2178}
82124d60
KU
2179
2180/**
2181 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
2182 * @q: the queue to submit the request
2183 * @rq: the request being queued
2184 */
2185int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
2186{
2187 unsigned long flags;
4853abaa 2188 int where = ELEVATOR_INSERT_BACK;
82124d60 2189
bf4e6b4e 2190 if (blk_cloned_rq_check_limits(q, rq))
82124d60
KU
2191 return -EIO;
2192
b2c9cd37
AM
2193 if (rq->rq_disk &&
2194 should_fail_request(&rq->rq_disk->part0, blk_rq_bytes(rq)))
82124d60 2195 return -EIO;
82124d60 2196
7fb4898e
KB
2197 if (q->mq_ops) {
2198 if (blk_queue_io_stat(q))
2199 blk_account_io_start(rq, true);
bd6737f1 2200 blk_mq_sched_insert_request(rq, false, true, false, false);
7fb4898e
KB
2201 return 0;
2202 }
2203
82124d60 2204 spin_lock_irqsave(q->queue_lock, flags);
3f3299d5 2205 if (unlikely(blk_queue_dying(q))) {
8ba61435
TH
2206 spin_unlock_irqrestore(q->queue_lock, flags);
2207 return -ENODEV;
2208 }
82124d60
KU
2209
2210 /*
2211 * Submitting request must be dequeued before calling this function
2212 * because it will be linked to another request_queue
2213 */
2214 BUG_ON(blk_queued_rq(rq));
2215
f73f44eb 2216 if (op_is_flush(rq->cmd_flags))
4853abaa
JM
2217 where = ELEVATOR_INSERT_FLUSH;
2218
2219 add_acct_request(q, rq, where);
e67b77c7
JM
2220 if (where == ELEVATOR_INSERT_FLUSH)
2221 __blk_run_queue(q);
82124d60
KU
2222 spin_unlock_irqrestore(q->queue_lock, flags);
2223
2224 return 0;
2225}
2226EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
2227
80a761fd
TH
2228/**
2229 * blk_rq_err_bytes - determine number of bytes till the next failure boundary
2230 * @rq: request to examine
2231 *
2232 * Description:
2233 * A request could be merge of IOs which require different failure
2234 * handling. This function determines the number of bytes which
2235 * can be failed from the beginning of the request without
2236 * crossing into area which need to be retried further.
2237 *
2238 * Return:
2239 * The number of bytes to fail.
2240 *
2241 * Context:
2242 * queue_lock must be held.
2243 */
2244unsigned int blk_rq_err_bytes(const struct request *rq)
2245{
2246 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
2247 unsigned int bytes = 0;
2248 struct bio *bio;
2249
e8064021 2250 if (!(rq->rq_flags & RQF_MIXED_MERGE))
80a761fd
TH
2251 return blk_rq_bytes(rq);
2252
2253 /*
2254 * Currently the only 'mixing' which can happen is between
2255 * different fastfail types. We can safely fail portions
2256 * which have all the failfast bits that the first one has -
2257 * the ones which are at least as eager to fail as the first
2258 * one.
2259 */
2260 for (bio = rq->bio; bio; bio = bio->bi_next) {
1eff9d32 2261 if ((bio->bi_opf & ff) != ff)
80a761fd 2262 break;
4f024f37 2263 bytes += bio->bi_iter.bi_size;
80a761fd
TH
2264 }
2265
2266 /* this could lead to infinite loop */
2267 BUG_ON(blk_rq_bytes(rq) && !bytes);
2268 return bytes;
2269}
2270EXPORT_SYMBOL_GPL(blk_rq_err_bytes);
2271
320ae51f 2272void blk_account_io_completion(struct request *req, unsigned int bytes)
bc58ba94 2273{
c2553b58 2274 if (blk_do_io_stat(req)) {
bc58ba94
JA
2275 const int rw = rq_data_dir(req);
2276 struct hd_struct *part;
2277 int cpu;
2278
2279 cpu = part_stat_lock();
09e099d4 2280 part = req->part;
bc58ba94
JA
2281 part_stat_add(cpu, part, sectors[rw], bytes >> 9);
2282 part_stat_unlock();
2283 }
2284}
2285
320ae51f 2286void blk_account_io_done(struct request *req)
bc58ba94 2287{
bc58ba94 2288 /*
dd4c133f
TH
2289 * Account IO completion. flush_rq isn't accounted as a
2290 * normal IO on queueing nor completion. Accounting the
2291 * containing request is enough.
bc58ba94 2292 */
e8064021 2293 if (blk_do_io_stat(req) && !(req->rq_flags & RQF_FLUSH_SEQ)) {
bc58ba94
JA
2294 unsigned long duration = jiffies - req->start_time;
2295 const int rw = rq_data_dir(req);
2296 struct hd_struct *part;
2297 int cpu;
2298
2299 cpu = part_stat_lock();
09e099d4 2300 part = req->part;
bc58ba94
JA
2301
2302 part_stat_inc(cpu, part, ios[rw]);
2303 part_stat_add(cpu, part, ticks[rw], duration);
2304 part_round_stats(cpu, part);
316d315b 2305 part_dec_in_flight(part, rw);
bc58ba94 2306
6c23a968 2307 hd_struct_put(part);
bc58ba94
JA
2308 part_stat_unlock();
2309 }
2310}
2311
47fafbc7 2312#ifdef CONFIG_PM
c8158819
LM
2313/*
2314 * Don't process normal requests when queue is suspended
2315 * or in the process of suspending/resuming
2316 */
2317static struct request *blk_pm_peek_request(struct request_queue *q,
2318 struct request *rq)
2319{
2320 if (q->dev && (q->rpm_status == RPM_SUSPENDED ||
e8064021 2321 (q->rpm_status != RPM_ACTIVE && !(rq->rq_flags & RQF_PM))))
c8158819
LM
2322 return NULL;
2323 else
2324 return rq;
2325}
2326#else
2327static inline struct request *blk_pm_peek_request(struct request_queue *q,
2328 struct request *rq)
2329{
2330 return rq;
2331}
2332#endif
2333
320ae51f
JA
2334void blk_account_io_start(struct request *rq, bool new_io)
2335{
2336 struct hd_struct *part;
2337 int rw = rq_data_dir(rq);
2338 int cpu;
2339
2340 if (!blk_do_io_stat(rq))
2341 return;
2342
2343 cpu = part_stat_lock();
2344
2345 if (!new_io) {
2346 part = rq->part;
2347 part_stat_inc(cpu, part, merges[rw]);
2348 } else {
2349 part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq));
2350 if (!hd_struct_try_get(part)) {
2351 /*
2352 * The partition is already being removed,
2353 * the request will be accounted on the disk only
2354 *
2355 * We take a reference on disk->part0 although that
2356 * partition will never be deleted, so we can treat
2357 * it as any other partition.
2358 */
2359 part = &rq->rq_disk->part0;
2360 hd_struct_get(part);
2361 }
2362 part_round_stats(cpu, part);
2363 part_inc_in_flight(part, rw);
2364 rq->part = part;
2365 }
2366
2367 part_stat_unlock();
2368}
2369
3bcddeac 2370/**
9934c8c0
TH
2371 * blk_peek_request - peek at the top of a request queue
2372 * @q: request queue to peek at
2373 *
2374 * Description:
2375 * Return the request at the top of @q. The returned request
2376 * should be started using blk_start_request() before LLD starts
2377 * processing it.
2378 *
2379 * Return:
2380 * Pointer to the request at the top of @q if available. Null
2381 * otherwise.
2382 *
2383 * Context:
2384 * queue_lock must be held.
2385 */
2386struct request *blk_peek_request(struct request_queue *q)
158dbda0
TH
2387{
2388 struct request *rq;
2389 int ret;
2390
2391 while ((rq = __elv_next_request(q)) != NULL) {
c8158819
LM
2392
2393 rq = blk_pm_peek_request(q, rq);
2394 if (!rq)
2395 break;
2396
e8064021 2397 if (!(rq->rq_flags & RQF_STARTED)) {
158dbda0
TH
2398 /*
2399 * This is the first time the device driver
2400 * sees this request (possibly after
2401 * requeueing). Notify IO scheduler.
2402 */
e8064021 2403 if (rq->rq_flags & RQF_SORTED)
158dbda0
TH
2404 elv_activate_rq(q, rq);
2405
2406 /*
2407 * just mark as started even if we don't start
2408 * it, a request that has been delayed should
2409 * not be passed by new incoming requests
2410 */
e8064021 2411 rq->rq_flags |= RQF_STARTED;
158dbda0
TH
2412 trace_block_rq_issue(q, rq);
2413 }
2414
2415 if (!q->boundary_rq || q->boundary_rq == rq) {
2416 q->end_sector = rq_end_sector(rq);
2417 q->boundary_rq = NULL;
2418 }
2419
e8064021 2420 if (rq->rq_flags & RQF_DONTPREP)
158dbda0
TH
2421 break;
2422
2e46e8b2 2423 if (q->dma_drain_size && blk_rq_bytes(rq)) {
158dbda0
TH
2424 /*
2425 * make sure space for the drain appears we
2426 * know we can do this because max_hw_segments
2427 * has been adjusted to be one fewer than the
2428 * device can handle
2429 */
2430 rq->nr_phys_segments++;
2431 }
2432
2433 if (!q->prep_rq_fn)
2434 break;
2435
2436 ret = q->prep_rq_fn(q, rq);
2437 if (ret == BLKPREP_OK) {
2438 break;
2439 } else if (ret == BLKPREP_DEFER) {
2440 /*
2441 * the request may have been (partially) prepped.
2442 * we need to keep this request in the front to
e8064021 2443 * avoid resource deadlock. RQF_STARTED will
158dbda0
TH
2444 * prevent other fs requests from passing this one.
2445 */
2e46e8b2 2446 if (q->dma_drain_size && blk_rq_bytes(rq) &&
e8064021 2447 !(rq->rq_flags & RQF_DONTPREP)) {
158dbda0
TH
2448 /*
2449 * remove the space for the drain we added
2450 * so that we don't add it again
2451 */
2452 --rq->nr_phys_segments;
2453 }
2454
2455 rq = NULL;
2456 break;
0fb5b1fb
MP
2457 } else if (ret == BLKPREP_KILL || ret == BLKPREP_INVALID) {
2458 int err = (ret == BLKPREP_INVALID) ? -EREMOTEIO : -EIO;
2459
e8064021 2460 rq->rq_flags |= RQF_QUIET;
c143dc90
JB
2461 /*
2462 * Mark this request as started so we don't trigger
2463 * any debug logic in the end I/O path.
2464 */
2465 blk_start_request(rq);
0fb5b1fb 2466 __blk_end_request_all(rq, err);
158dbda0
TH
2467 } else {
2468 printk(KERN_ERR "%s: bad return=%d\n", __func__, ret);
2469 break;
2470 }
2471 }
2472
2473 return rq;
2474}
9934c8c0 2475EXPORT_SYMBOL(blk_peek_request);
158dbda0 2476
9934c8c0 2477void blk_dequeue_request(struct request *rq)
158dbda0 2478{
9934c8c0
TH
2479 struct request_queue *q = rq->q;
2480
158dbda0
TH
2481 BUG_ON(list_empty(&rq->queuelist));
2482 BUG_ON(ELV_ON_HASH(rq));
2483
2484 list_del_init(&rq->queuelist);
2485
2486 /*
2487 * the time frame between a request being removed from the lists
2488 * and to it is freed is accounted as io that is in progress at
2489 * the driver side.
2490 */
9195291e 2491 if (blk_account_rq(rq)) {
0a7ae2ff 2492 q->in_flight[rq_is_sync(rq)]++;
9195291e
DS
2493 set_io_start_time_ns(rq);
2494 }
158dbda0
TH
2495}
2496
9934c8c0
TH
2497/**
2498 * blk_start_request - start request processing on the driver
2499 * @req: request to dequeue
2500 *
2501 * Description:
2502 * Dequeue @req and start timeout timer on it. This hands off the
2503 * request to the driver.
2504 *
2505 * Block internal functions which don't want to start timer should
2506 * call blk_dequeue_request().
2507 *
2508 * Context:
2509 * queue_lock must be held.
2510 */
2511void blk_start_request(struct request *req)
2512{
2513 blk_dequeue_request(req);
2514
cf43e6be 2515 if (test_bit(QUEUE_FLAG_STATS, &req->q->queue_flags)) {
88eeca49 2516 blk_stat_set_issue(&req->issue_stat, blk_rq_sectors(req));
cf43e6be 2517 req->rq_flags |= RQF_STATS;
87760e5e 2518 wbt_issue(req->q->rq_wb, &req->issue_stat);
cf43e6be
JA
2519 }
2520
4912aa6c 2521 BUG_ON(test_bit(REQ_ATOM_COMPLETE, &req->atomic_flags));
9934c8c0
TH
2522 blk_add_timer(req);
2523}
2524EXPORT_SYMBOL(blk_start_request);
2525
2526/**
2527 * blk_fetch_request - fetch a request from a request queue
2528 * @q: request queue to fetch a request from
2529 *
2530 * Description:
2531 * Return the request at the top of @q. The request is started on
2532 * return and LLD can start processing it immediately.
2533 *
2534 * Return:
2535 * Pointer to the request at the top of @q if available. Null
2536 * otherwise.
2537 *
2538 * Context:
2539 * queue_lock must be held.
2540 */
2541struct request *blk_fetch_request(struct request_queue *q)
2542{
2543 struct request *rq;
2544
2545 rq = blk_peek_request(q);
2546 if (rq)
2547 blk_start_request(rq);
2548 return rq;
2549}
2550EXPORT_SYMBOL(blk_fetch_request);
2551
3bcddeac 2552/**
2e60e022 2553 * blk_update_request - Special helper function for request stacking drivers
8ebf9756 2554 * @req: the request being processed
710027a4 2555 * @error: %0 for success, < %0 for error
8ebf9756 2556 * @nr_bytes: number of bytes to complete @req
3bcddeac
KU
2557 *
2558 * Description:
8ebf9756
RD
2559 * Ends I/O on a number of bytes attached to @req, but doesn't complete
2560 * the request structure even if @req doesn't have leftover.
2561 * If @req has leftover, sets it up for the next range of segments.
2e60e022
TH
2562 *
2563 * This special helper function is only for request stacking drivers
2564 * (e.g. request-based dm) so that they can handle partial completion.
2565 * Actual device drivers should use blk_end_request instead.
2566 *
2567 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
2568 * %false return from this function.
3bcddeac
KU
2569 *
2570 * Return:
2e60e022
TH
2571 * %false - this request doesn't have any more data
2572 * %true - this request has more data
3bcddeac 2573 **/
2e60e022 2574bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
1da177e4 2575{
f79ea416 2576 int total_bytes;
1da177e4 2577
caf7df12 2578 trace_block_rq_complete(req, error, nr_bytes);
4a0efdc9 2579
2e60e022
TH
2580 if (!req->bio)
2581 return false;
2582
57292b58 2583 if (error && !blk_rq_is_passthrough(req) &&
e8064021 2584 !(req->rq_flags & RQF_QUIET)) {
79775567
HR
2585 char *error_type;
2586
2587 switch (error) {
2588 case -ENOLINK:
2589 error_type = "recoverable transport";
2590 break;
2591 case -EREMOTEIO:
2592 error_type = "critical target";
2593 break;
2594 case -EBADE:
2595 error_type = "critical nexus";
2596 break;
d1ffc1f8
HR
2597 case -ETIMEDOUT:
2598 error_type = "timeout";
2599 break;
a9d6ceb8
HR
2600 case -ENOSPC:
2601 error_type = "critical space allocation";
2602 break;
7e782af5
HR
2603 case -ENODATA:
2604 error_type = "critical medium";
2605 break;
79775567
HR
2606 case -EIO:
2607 default:
2608 error_type = "I/O";
2609 break;
2610 }
ef3ecb66
RE
2611 printk_ratelimited(KERN_ERR "%s: %s error, dev %s, sector %llu\n",
2612 __func__, error_type, req->rq_disk ?
37d7b34f
YZ
2613 req->rq_disk->disk_name : "?",
2614 (unsigned long long)blk_rq_pos(req));
2615
1da177e4
LT
2616 }
2617
bc58ba94 2618 blk_account_io_completion(req, nr_bytes);
d72d904a 2619
f79ea416
KO
2620 total_bytes = 0;
2621 while (req->bio) {
2622 struct bio *bio = req->bio;
4f024f37 2623 unsigned bio_bytes = min(bio->bi_iter.bi_size, nr_bytes);
1da177e4 2624
4f024f37 2625 if (bio_bytes == bio->bi_iter.bi_size)
1da177e4 2626 req->bio = bio->bi_next;
1da177e4 2627
fbbaf700
N
2628 /* Completion has already been traced */
2629 bio_clear_flag(bio, BIO_TRACE_COMPLETION);
f79ea416 2630 req_bio_endio(req, bio, bio_bytes, error);
1da177e4 2631
f79ea416
KO
2632 total_bytes += bio_bytes;
2633 nr_bytes -= bio_bytes;
1da177e4 2634
f79ea416
KO
2635 if (!nr_bytes)
2636 break;
1da177e4
LT
2637 }
2638
2639 /*
2640 * completely done
2641 */
2e60e022
TH
2642 if (!req->bio) {
2643 /*
2644 * Reset counters so that the request stacking driver
2645 * can find how many bytes remain in the request
2646 * later.
2647 */
a2dec7b3 2648 req->__data_len = 0;
2e60e022
TH
2649 return false;
2650 }
1da177e4 2651
f9d03f96
CH
2652 WARN_ON_ONCE(req->rq_flags & RQF_SPECIAL_PAYLOAD);
2653
a2dec7b3 2654 req->__data_len -= total_bytes;
2e46e8b2
TH
2655
2656 /* update sector only for requests with clear definition of sector */
57292b58 2657 if (!blk_rq_is_passthrough(req))
a2dec7b3 2658 req->__sector += total_bytes >> 9;
2e46e8b2 2659
80a761fd 2660 /* mixed attributes always follow the first bio */
e8064021 2661 if (req->rq_flags & RQF_MIXED_MERGE) {
80a761fd 2662 req->cmd_flags &= ~REQ_FAILFAST_MASK;
1eff9d32 2663 req->cmd_flags |= req->bio->bi_opf & REQ_FAILFAST_MASK;
80a761fd
TH
2664 }
2665
2e46e8b2
TH
2666 /*
2667 * If total number of sectors is less than the first segment
2668 * size, something has gone terribly wrong.
2669 */
2670 if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
8182924b 2671 blk_dump_rq_flags(req, "request botched");
a2dec7b3 2672 req->__data_len = blk_rq_cur_bytes(req);
2e46e8b2
TH
2673 }
2674
2675 /* recalculate the number of segments */
1da177e4 2676 blk_recalc_rq_segments(req);
2e46e8b2 2677
2e60e022 2678 return true;
1da177e4 2679}
2e60e022 2680EXPORT_SYMBOL_GPL(blk_update_request);
1da177e4 2681
2e60e022
TH
2682static bool blk_update_bidi_request(struct request *rq, int error,
2683 unsigned int nr_bytes,
2684 unsigned int bidi_bytes)
5efccd17 2685{
2e60e022
TH
2686 if (blk_update_request(rq, error, nr_bytes))
2687 return true;
5efccd17 2688
2e60e022
TH
2689 /* Bidi request must be completed as a whole */
2690 if (unlikely(blk_bidi_rq(rq)) &&
2691 blk_update_request(rq->next_rq, error, bidi_bytes))
2692 return true;
5efccd17 2693
e2e1a148
JA
2694 if (blk_queue_add_random(rq->q))
2695 add_disk_randomness(rq->rq_disk);
2e60e022
TH
2696
2697 return false;
1da177e4
LT
2698}
2699
28018c24
JB
2700/**
2701 * blk_unprep_request - unprepare a request
2702 * @req: the request
2703 *
2704 * This function makes a request ready for complete resubmission (or
2705 * completion). It happens only after all error handling is complete,
2706 * so represents the appropriate moment to deallocate any resources
2707 * that were allocated to the request in the prep_rq_fn. The queue
2708 * lock is held when calling this.
2709 */
2710void blk_unprep_request(struct request *req)
2711{
2712 struct request_queue *q = req->q;
2713
e8064021 2714 req->rq_flags &= ~RQF_DONTPREP;
28018c24
JB
2715 if (q->unprep_rq_fn)
2716 q->unprep_rq_fn(q, req);
2717}
2718EXPORT_SYMBOL_GPL(blk_unprep_request);
2719
1da177e4
LT
2720/*
2721 * queue lock must be held
2722 */
12120077 2723void blk_finish_request(struct request *req, int error)
1da177e4 2724{
cf43e6be
JA
2725 struct request_queue *q = req->q;
2726
2727 if (req->rq_flags & RQF_STATS)
34dbad5d 2728 blk_stat_add(req);
cf43e6be 2729
e8064021 2730 if (req->rq_flags & RQF_QUEUED)
cf43e6be 2731 blk_queue_end_tag(q, req);
b8286239 2732
ba396a6c 2733 BUG_ON(blk_queued_rq(req));
1da177e4 2734
57292b58 2735 if (unlikely(laptop_mode) && !blk_rq_is_passthrough(req))
dc3b17cc 2736 laptop_io_completion(req->q->backing_dev_info);
1da177e4 2737
e78042e5
MA
2738 blk_delete_timer(req);
2739
e8064021 2740 if (req->rq_flags & RQF_DONTPREP)
28018c24
JB
2741 blk_unprep_request(req);
2742
bc58ba94 2743 blk_account_io_done(req);
b8286239 2744
87760e5e
JA
2745 if (req->end_io) {
2746 wbt_done(req->q->rq_wb, &req->issue_stat);
8ffdc655 2747 req->end_io(req, error);
87760e5e 2748 } else {
b8286239
KU
2749 if (blk_bidi_rq(req))
2750 __blk_put_request(req->next_rq->q, req->next_rq);
2751
cf43e6be 2752 __blk_put_request(q, req);
b8286239 2753 }
1da177e4 2754}
12120077 2755EXPORT_SYMBOL(blk_finish_request);
1da177e4 2756
3b11313a 2757/**
2e60e022
TH
2758 * blk_end_bidi_request - Complete a bidi request
2759 * @rq: the request to complete
2760 * @error: %0 for success, < %0 for error
2761 * @nr_bytes: number of bytes to complete @rq
2762 * @bidi_bytes: number of bytes to complete @rq->next_rq
a0cd1285
JA
2763 *
2764 * Description:
e3a04fe3 2765 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
2e60e022
TH
2766 * Drivers that supports bidi can safely call this member for any
2767 * type of request, bidi or uni. In the later case @bidi_bytes is
2768 * just ignored.
336cdb40
KU
2769 *
2770 * Return:
2e60e022
TH
2771 * %false - we are done with this request
2772 * %true - still buffers pending for this request
a0cd1285 2773 **/
b1f74493 2774static bool blk_end_bidi_request(struct request *rq, int error,
32fab448
KU
2775 unsigned int nr_bytes, unsigned int bidi_bytes)
2776{
336cdb40 2777 struct request_queue *q = rq->q;
2e60e022 2778 unsigned long flags;
32fab448 2779
2e60e022
TH
2780 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2781 return true;
32fab448 2782
336cdb40 2783 spin_lock_irqsave(q->queue_lock, flags);
2e60e022 2784 blk_finish_request(rq, error);
336cdb40
KU
2785 spin_unlock_irqrestore(q->queue_lock, flags);
2786
2e60e022 2787 return false;
32fab448
KU
2788}
2789
336cdb40 2790/**
2e60e022
TH
2791 * __blk_end_bidi_request - Complete a bidi request with queue lock held
2792 * @rq: the request to complete
710027a4 2793 * @error: %0 for success, < %0 for error
e3a04fe3
KU
2794 * @nr_bytes: number of bytes to complete @rq
2795 * @bidi_bytes: number of bytes to complete @rq->next_rq
336cdb40
KU
2796 *
2797 * Description:
2e60e022
TH
2798 * Identical to blk_end_bidi_request() except that queue lock is
2799 * assumed to be locked on entry and remains so on return.
336cdb40
KU
2800 *
2801 * Return:
2e60e022
TH
2802 * %false - we are done with this request
2803 * %true - still buffers pending for this request
336cdb40 2804 **/
d0fac025 2805static bool __blk_end_bidi_request(struct request *rq, int error,
b1f74493 2806 unsigned int nr_bytes, unsigned int bidi_bytes)
336cdb40 2807{
2e60e022
TH
2808 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2809 return true;
336cdb40 2810
2e60e022 2811 blk_finish_request(rq, error);
336cdb40 2812
2e60e022 2813 return false;
336cdb40 2814}
e19a3ab0
KU
2815
2816/**
2817 * blk_end_request - Helper function for drivers to complete the request.
2818 * @rq: the request being processed
710027a4 2819 * @error: %0 for success, < %0 for error
e19a3ab0
KU
2820 * @nr_bytes: number of bytes to complete
2821 *
2822 * Description:
2823 * Ends I/O on a number of bytes attached to @rq.
2824 * If @rq has leftover, sets it up for the next range of segments.
2825 *
2826 * Return:
b1f74493
FT
2827 * %false - we are done with this request
2828 * %true - still buffers pending for this request
e19a3ab0 2829 **/
b1f74493 2830bool blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
e19a3ab0 2831{
b1f74493 2832 return blk_end_bidi_request(rq, error, nr_bytes, 0);
e19a3ab0 2833}
56ad1740 2834EXPORT_SYMBOL(blk_end_request);
336cdb40
KU
2835
2836/**
b1f74493
FT
2837 * blk_end_request_all - Helper function for drives to finish the request.
2838 * @rq: the request to finish
8ebf9756 2839 * @error: %0 for success, < %0 for error
336cdb40
KU
2840 *
2841 * Description:
b1f74493
FT
2842 * Completely finish @rq.
2843 */
2844void blk_end_request_all(struct request *rq, int error)
336cdb40 2845{
b1f74493
FT
2846 bool pending;
2847 unsigned int bidi_bytes = 0;
336cdb40 2848
b1f74493
FT
2849 if (unlikely(blk_bidi_rq(rq)))
2850 bidi_bytes = blk_rq_bytes(rq->next_rq);
336cdb40 2851
b1f74493
FT
2852 pending = blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2853 BUG_ON(pending);
2854}
56ad1740 2855EXPORT_SYMBOL(blk_end_request_all);
336cdb40 2856
e3a04fe3 2857/**
b1f74493
FT
2858 * __blk_end_request - Helper function for drivers to complete the request.
2859 * @rq: the request being processed
2860 * @error: %0 for success, < %0 for error
2861 * @nr_bytes: number of bytes to complete
e3a04fe3
KU
2862 *
2863 * Description:
b1f74493 2864 * Must be called with queue lock held unlike blk_end_request().
e3a04fe3
KU
2865 *
2866 * Return:
b1f74493
FT
2867 * %false - we are done with this request
2868 * %true - still buffers pending for this request
e3a04fe3 2869 **/
b1f74493 2870bool __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
e3a04fe3 2871{
b1f74493 2872 return __blk_end_bidi_request(rq, error, nr_bytes, 0);
e3a04fe3 2873}
56ad1740 2874EXPORT_SYMBOL(__blk_end_request);
e3a04fe3 2875
32fab448 2876/**
b1f74493
FT
2877 * __blk_end_request_all - Helper function for drives to finish the request.
2878 * @rq: the request to finish
8ebf9756 2879 * @error: %0 for success, < %0 for error
32fab448
KU
2880 *
2881 * Description:
b1f74493 2882 * Completely finish @rq. Must be called with queue lock held.
32fab448 2883 */
b1f74493 2884void __blk_end_request_all(struct request *rq, int error)
32fab448 2885{
b1f74493
FT
2886 bool pending;
2887 unsigned int bidi_bytes = 0;
2888
2889 if (unlikely(blk_bidi_rq(rq)))
2890 bidi_bytes = blk_rq_bytes(rq->next_rq);
2891
2892 pending = __blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2893 BUG_ON(pending);
32fab448 2894}
56ad1740 2895EXPORT_SYMBOL(__blk_end_request_all);
32fab448 2896
e19a3ab0 2897/**
b1f74493
FT
2898 * __blk_end_request_cur - Helper function to finish the current request chunk.
2899 * @rq: the request to finish the current chunk for
8ebf9756 2900 * @error: %0 for success, < %0 for error
e19a3ab0
KU
2901 *
2902 * Description:
b1f74493
FT
2903 * Complete the current consecutively mapped chunk from @rq. Must
2904 * be called with queue lock held.
e19a3ab0
KU
2905 *
2906 * Return:
b1f74493
FT
2907 * %false - we are done with this request
2908 * %true - still buffers pending for this request
2909 */
2910bool __blk_end_request_cur(struct request *rq, int error)
e19a3ab0 2911{
b1f74493 2912 return __blk_end_request(rq, error, blk_rq_cur_bytes(rq));
e19a3ab0 2913}
56ad1740 2914EXPORT_SYMBOL(__blk_end_request_cur);
e19a3ab0 2915
86db1e29
JA
2916void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2917 struct bio *bio)
1da177e4 2918{
b4f42e28 2919 if (bio_has_data(bio))
fb2dce86 2920 rq->nr_phys_segments = bio_phys_segments(q, bio);
b4f42e28 2921
4f024f37 2922 rq->__data_len = bio->bi_iter.bi_size;
1da177e4 2923 rq->bio = rq->biotail = bio;
1da177e4 2924
66846572
N
2925 if (bio->bi_bdev)
2926 rq->rq_disk = bio->bi_bdev->bd_disk;
2927}
1da177e4 2928
2d4dc890
IL
2929#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
2930/**
2931 * rq_flush_dcache_pages - Helper function to flush all pages in a request
2932 * @rq: the request to be flushed
2933 *
2934 * Description:
2935 * Flush all pages in @rq.
2936 */
2937void rq_flush_dcache_pages(struct request *rq)
2938{
2939 struct req_iterator iter;
7988613b 2940 struct bio_vec bvec;
2d4dc890
IL
2941
2942 rq_for_each_segment(bvec, rq, iter)
7988613b 2943 flush_dcache_page(bvec.bv_page);
2d4dc890
IL
2944}
2945EXPORT_SYMBOL_GPL(rq_flush_dcache_pages);
2946#endif
2947
ef9e3fac
KU
2948/**
2949 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
2950 * @q : the queue of the device being checked
2951 *
2952 * Description:
2953 * Check if underlying low-level drivers of a device are busy.
2954 * If the drivers want to export their busy state, they must set own
2955 * exporting function using blk_queue_lld_busy() first.
2956 *
2957 * Basically, this function is used only by request stacking drivers
2958 * to stop dispatching requests to underlying devices when underlying
2959 * devices are busy. This behavior helps more I/O merging on the queue
2960 * of the request stacking driver and prevents I/O throughput regression
2961 * on burst I/O load.
2962 *
2963 * Return:
2964 * 0 - Not busy (The request stacking driver should dispatch request)
2965 * 1 - Busy (The request stacking driver should stop dispatching request)
2966 */
2967int blk_lld_busy(struct request_queue *q)
2968{
2969 if (q->lld_busy_fn)
2970 return q->lld_busy_fn(q);
2971
2972 return 0;
2973}
2974EXPORT_SYMBOL_GPL(blk_lld_busy);
2975
78d8e58a
MS
2976/**
2977 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
2978 * @rq: the clone request to be cleaned up
2979 *
2980 * Description:
2981 * Free all bios in @rq for a cloned request.
2982 */
2983void blk_rq_unprep_clone(struct request *rq)
2984{
2985 struct bio *bio;
2986
2987 while ((bio = rq->bio) != NULL) {
2988 rq->bio = bio->bi_next;
2989
2990 bio_put(bio);
2991 }
2992}
2993EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
2994
2995/*
2996 * Copy attributes of the original request to the clone request.
2997 * The actual data parts (e.g. ->cmd, ->sense) are not copied.
2998 */
2999static void __blk_rq_prep_clone(struct request *dst, struct request *src)
b0fd271d
KU
3000{
3001 dst->cpu = src->cpu;
b0fd271d
KU
3002 dst->__sector = blk_rq_pos(src);
3003 dst->__data_len = blk_rq_bytes(src);
3004 dst->nr_phys_segments = src->nr_phys_segments;
3005 dst->ioprio = src->ioprio;
3006 dst->extra_len = src->extra_len;
78d8e58a
MS
3007}
3008
3009/**
3010 * blk_rq_prep_clone - Helper function to setup clone request
3011 * @rq: the request to be setup
3012 * @rq_src: original request to be cloned
3013 * @bs: bio_set that bios for clone are allocated from
3014 * @gfp_mask: memory allocation mask for bio
3015 * @bio_ctr: setup function to be called for each clone bio.
3016 * Returns %0 for success, non %0 for failure.
3017 * @data: private data to be passed to @bio_ctr
3018 *
3019 * Description:
3020 * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
3021 * The actual data parts of @rq_src (e.g. ->cmd, ->sense)
3022 * are not copied, and copying such parts is the caller's responsibility.
3023 * Also, pages which the original bios are pointing to are not copied
3024 * and the cloned bios just point same pages.
3025 * So cloned bios must be completed before original bios, which means
3026 * the caller must complete @rq before @rq_src.
3027 */
3028int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
3029 struct bio_set *bs, gfp_t gfp_mask,
3030 int (*bio_ctr)(struct bio *, struct bio *, void *),
3031 void *data)
3032{
3033 struct bio *bio, *bio_src;
3034
3035 if (!bs)
3036 bs = fs_bio_set;
3037
3038 __rq_for_each_bio(bio_src, rq_src) {
3039 bio = bio_clone_fast(bio_src, gfp_mask, bs);
3040 if (!bio)
3041 goto free_and_out;
3042
3043 if (bio_ctr && bio_ctr(bio, bio_src, data))
3044 goto free_and_out;
3045
3046 if (rq->bio) {
3047 rq->biotail->bi_next = bio;
3048 rq->biotail = bio;
3049 } else
3050 rq->bio = rq->biotail = bio;
3051 }
3052
3053 __blk_rq_prep_clone(rq, rq_src);
3054
3055 return 0;
3056
3057free_and_out:
3058 if (bio)
3059 bio_put(bio);
3060 blk_rq_unprep_clone(rq);
3061
3062 return -ENOMEM;
b0fd271d
KU
3063}
3064EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
3065
59c3d45e 3066int kblockd_schedule_work(struct work_struct *work)
1da177e4
LT
3067{
3068 return queue_work(kblockd_workqueue, work);
3069}
1da177e4
LT
3070EXPORT_SYMBOL(kblockd_schedule_work);
3071
ee63cfa7
JA
3072int kblockd_schedule_work_on(int cpu, struct work_struct *work)
3073{
3074 return queue_work_on(cpu, kblockd_workqueue, work);
3075}
3076EXPORT_SYMBOL(kblockd_schedule_work_on);
3077
818cd1cb
JA
3078int kblockd_mod_delayed_work_on(int cpu, struct delayed_work *dwork,
3079 unsigned long delay)
3080{
3081 return mod_delayed_work_on(cpu, kblockd_workqueue, dwork, delay);
3082}
3083EXPORT_SYMBOL(kblockd_mod_delayed_work_on);
3084
59c3d45e
JA
3085int kblockd_schedule_delayed_work(struct delayed_work *dwork,
3086 unsigned long delay)
e43473b7
VG
3087{
3088 return queue_delayed_work(kblockd_workqueue, dwork, delay);
3089}
3090EXPORT_SYMBOL(kblockd_schedule_delayed_work);
3091
8ab14595
JA
3092int kblockd_schedule_delayed_work_on(int cpu, struct delayed_work *dwork,
3093 unsigned long delay)
3094{
3095 return queue_delayed_work_on(cpu, kblockd_workqueue, dwork, delay);
3096}
3097EXPORT_SYMBOL(kblockd_schedule_delayed_work_on);
3098
75df7136
SJ
3099/**
3100 * blk_start_plug - initialize blk_plug and track it inside the task_struct
3101 * @plug: The &struct blk_plug that needs to be initialized
3102 *
3103 * Description:
3104 * Tracking blk_plug inside the task_struct will help with auto-flushing the
3105 * pending I/O should the task end up blocking between blk_start_plug() and
3106 * blk_finish_plug(). This is important from a performance perspective, but
3107 * also ensures that we don't deadlock. For instance, if the task is blocking
3108 * for a memory allocation, memory reclaim could end up wanting to free a
3109 * page belonging to that request that is currently residing in our private
3110 * plug. By flushing the pending I/O when the process goes to sleep, we avoid
3111 * this kind of deadlock.
3112 */
73c10101
JA
3113void blk_start_plug(struct blk_plug *plug)
3114{
3115 struct task_struct *tsk = current;
3116
dd6cf3e1
SL
3117 /*
3118 * If this is a nested plug, don't actually assign it.
3119 */
3120 if (tsk->plug)
3121 return;
3122
73c10101 3123 INIT_LIST_HEAD(&plug->list);
320ae51f 3124 INIT_LIST_HEAD(&plug->mq_list);
048c9374 3125 INIT_LIST_HEAD(&plug->cb_list);
73c10101 3126 /*
dd6cf3e1
SL
3127 * Store ordering should not be needed here, since a potential
3128 * preempt will imply a full memory barrier
73c10101 3129 */
dd6cf3e1 3130 tsk->plug = plug;
73c10101
JA
3131}
3132EXPORT_SYMBOL(blk_start_plug);
3133
3134static int plug_rq_cmp(void *priv, struct list_head *a, struct list_head *b)
3135{
3136 struct request *rqa = container_of(a, struct request, queuelist);
3137 struct request *rqb = container_of(b, struct request, queuelist);
3138
975927b9
JM
3139 return !(rqa->q < rqb->q ||
3140 (rqa->q == rqb->q && blk_rq_pos(rqa) < blk_rq_pos(rqb)));
73c10101
JA
3141}
3142
49cac01e
JA
3143/*
3144 * If 'from_schedule' is true, then postpone the dispatch of requests
3145 * until a safe kblockd context. We due this to avoid accidental big
3146 * additional stack usage in driver dispatch, in places where the originally
3147 * plugger did not intend it.
3148 */
f6603783 3149static void queue_unplugged(struct request_queue *q, unsigned int depth,
49cac01e 3150 bool from_schedule)
99e22598 3151 __releases(q->queue_lock)
94b5eb28 3152{
49cac01e 3153 trace_block_unplug(q, depth, !from_schedule);
99e22598 3154
70460571 3155 if (from_schedule)
24ecfbe2 3156 blk_run_queue_async(q);
70460571 3157 else
24ecfbe2 3158 __blk_run_queue(q);
70460571 3159 spin_unlock(q->queue_lock);
94b5eb28
JA
3160}
3161
74018dc3 3162static void flush_plug_callbacks(struct blk_plug *plug, bool from_schedule)
048c9374
N
3163{
3164 LIST_HEAD(callbacks);
3165
2a7d5559
SL
3166 while (!list_empty(&plug->cb_list)) {
3167 list_splice_init(&plug->cb_list, &callbacks);
048c9374 3168
2a7d5559
SL
3169 while (!list_empty(&callbacks)) {
3170 struct blk_plug_cb *cb = list_first_entry(&callbacks,
048c9374
N
3171 struct blk_plug_cb,
3172 list);
2a7d5559 3173 list_del(&cb->list);
74018dc3 3174 cb->callback(cb, from_schedule);
2a7d5559 3175 }
048c9374
N
3176 }
3177}
3178
9cbb1750
N
3179struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug, void *data,
3180 int size)
3181{
3182 struct blk_plug *plug = current->plug;
3183 struct blk_plug_cb *cb;
3184
3185 if (!plug)
3186 return NULL;
3187
3188 list_for_each_entry(cb, &plug->cb_list, list)
3189 if (cb->callback == unplug && cb->data == data)
3190 return cb;
3191
3192 /* Not currently on the callback list */
3193 BUG_ON(size < sizeof(*cb));
3194 cb = kzalloc(size, GFP_ATOMIC);
3195 if (cb) {
3196 cb->data = data;
3197 cb->callback = unplug;
3198 list_add(&cb->list, &plug->cb_list);
3199 }
3200 return cb;
3201}
3202EXPORT_SYMBOL(blk_check_plugged);
3203
49cac01e 3204void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
73c10101
JA
3205{
3206 struct request_queue *q;
3207 unsigned long flags;
3208 struct request *rq;
109b8129 3209 LIST_HEAD(list);
94b5eb28 3210 unsigned int depth;
73c10101 3211
74018dc3 3212 flush_plug_callbacks(plug, from_schedule);
320ae51f
JA
3213
3214 if (!list_empty(&plug->mq_list))
3215 blk_mq_flush_plug_list(plug, from_schedule);
3216
73c10101
JA
3217 if (list_empty(&plug->list))
3218 return;
3219
109b8129
N
3220 list_splice_init(&plug->list, &list);
3221
422765c2 3222 list_sort(NULL, &list, plug_rq_cmp);
73c10101
JA
3223
3224 q = NULL;
94b5eb28 3225 depth = 0;
18811272
JA
3226
3227 /*
3228 * Save and disable interrupts here, to avoid doing it for every
3229 * queue lock we have to take.
3230 */
73c10101 3231 local_irq_save(flags);
109b8129
N
3232 while (!list_empty(&list)) {
3233 rq = list_entry_rq(list.next);
73c10101 3234 list_del_init(&rq->queuelist);
73c10101
JA
3235 BUG_ON(!rq->q);
3236 if (rq->q != q) {
99e22598
JA
3237 /*
3238 * This drops the queue lock
3239 */
3240 if (q)
49cac01e 3241 queue_unplugged(q, depth, from_schedule);
73c10101 3242 q = rq->q;
94b5eb28 3243 depth = 0;
73c10101
JA
3244 spin_lock(q->queue_lock);
3245 }
8ba61435
TH
3246
3247 /*
3248 * Short-circuit if @q is dead
3249 */
3f3299d5 3250 if (unlikely(blk_queue_dying(q))) {
8ba61435
TH
3251 __blk_end_request_all(rq, -ENODEV);
3252 continue;
3253 }
3254
73c10101
JA
3255 /*
3256 * rq is already accounted, so use raw insert
3257 */
f73f44eb 3258 if (op_is_flush(rq->cmd_flags))
401a18e9
JA
3259 __elv_add_request(q, rq, ELEVATOR_INSERT_FLUSH);
3260 else
3261 __elv_add_request(q, rq, ELEVATOR_INSERT_SORT_MERGE);
94b5eb28
JA
3262
3263 depth++;
73c10101
JA
3264 }
3265
99e22598
JA
3266 /*
3267 * This drops the queue lock
3268 */
3269 if (q)
49cac01e 3270 queue_unplugged(q, depth, from_schedule);
73c10101 3271
73c10101
JA
3272 local_irq_restore(flags);
3273}
73c10101
JA
3274
3275void blk_finish_plug(struct blk_plug *plug)
3276{
dd6cf3e1
SL
3277 if (plug != current->plug)
3278 return;
f6603783 3279 blk_flush_plug_list(plug, false);
73c10101 3280
dd6cf3e1 3281 current->plug = NULL;
73c10101 3282}
88b996cd 3283EXPORT_SYMBOL(blk_finish_plug);
73c10101 3284
47fafbc7 3285#ifdef CONFIG_PM
6c954667
LM
3286/**
3287 * blk_pm_runtime_init - Block layer runtime PM initialization routine
3288 * @q: the queue of the device
3289 * @dev: the device the queue belongs to
3290 *
3291 * Description:
3292 * Initialize runtime-PM-related fields for @q and start auto suspend for
3293 * @dev. Drivers that want to take advantage of request-based runtime PM
3294 * should call this function after @dev has been initialized, and its
3295 * request queue @q has been allocated, and runtime PM for it can not happen
3296 * yet(either due to disabled/forbidden or its usage_count > 0). In most
3297 * cases, driver should call this function before any I/O has taken place.
3298 *
3299 * This function takes care of setting up using auto suspend for the device,
3300 * the autosuspend delay is set to -1 to make runtime suspend impossible
3301 * until an updated value is either set by user or by driver. Drivers do
3302 * not need to touch other autosuspend settings.
3303 *
3304 * The block layer runtime PM is request based, so only works for drivers
3305 * that use request as their IO unit instead of those directly use bio's.
3306 */
3307void blk_pm_runtime_init(struct request_queue *q, struct device *dev)
3308{
3309 q->dev = dev;
3310 q->rpm_status = RPM_ACTIVE;
3311 pm_runtime_set_autosuspend_delay(q->dev, -1);
3312 pm_runtime_use_autosuspend(q->dev);
3313}
3314EXPORT_SYMBOL(blk_pm_runtime_init);
3315
3316/**
3317 * blk_pre_runtime_suspend - Pre runtime suspend check
3318 * @q: the queue of the device
3319 *
3320 * Description:
3321 * This function will check if runtime suspend is allowed for the device
3322 * by examining if there are any requests pending in the queue. If there
3323 * are requests pending, the device can not be runtime suspended; otherwise,
3324 * the queue's status will be updated to SUSPENDING and the driver can
3325 * proceed to suspend the device.
3326 *
3327 * For the not allowed case, we mark last busy for the device so that
3328 * runtime PM core will try to autosuspend it some time later.
3329 *
3330 * This function should be called near the start of the device's
3331 * runtime_suspend callback.
3332 *
3333 * Return:
3334 * 0 - OK to runtime suspend the device
3335 * -EBUSY - Device should not be runtime suspended
3336 */
3337int blk_pre_runtime_suspend(struct request_queue *q)
3338{
3339 int ret = 0;
3340
4fd41a85
KX
3341 if (!q->dev)
3342 return ret;
3343
6c954667
LM
3344 spin_lock_irq(q->queue_lock);
3345 if (q->nr_pending) {
3346 ret = -EBUSY;
3347 pm_runtime_mark_last_busy(q->dev);
3348 } else {
3349 q->rpm_status = RPM_SUSPENDING;
3350 }
3351 spin_unlock_irq(q->queue_lock);
3352 return ret;
3353}
3354EXPORT_SYMBOL(blk_pre_runtime_suspend);
3355
3356/**
3357 * blk_post_runtime_suspend - Post runtime suspend processing
3358 * @q: the queue of the device
3359 * @err: return value of the device's runtime_suspend function
3360 *
3361 * Description:
3362 * Update the queue's runtime status according to the return value of the
3363 * device's runtime suspend function and mark last busy for the device so
3364 * that PM core will try to auto suspend the device at a later time.
3365 *
3366 * This function should be called near the end of the device's
3367 * runtime_suspend callback.
3368 */
3369void blk_post_runtime_suspend(struct request_queue *q, int err)
3370{
4fd41a85
KX
3371 if (!q->dev)
3372 return;
3373
6c954667
LM
3374 spin_lock_irq(q->queue_lock);
3375 if (!err) {
3376 q->rpm_status = RPM_SUSPENDED;
3377 } else {
3378 q->rpm_status = RPM_ACTIVE;
3379 pm_runtime_mark_last_busy(q->dev);
3380 }
3381 spin_unlock_irq(q->queue_lock);
3382}
3383EXPORT_SYMBOL(blk_post_runtime_suspend);
3384
3385/**
3386 * blk_pre_runtime_resume - Pre runtime resume processing
3387 * @q: the queue of the device
3388 *
3389 * Description:
3390 * Update the queue's runtime status to RESUMING in preparation for the
3391 * runtime resume of the device.
3392 *
3393 * This function should be called near the start of the device's
3394 * runtime_resume callback.
3395 */
3396void blk_pre_runtime_resume(struct request_queue *q)
3397{
4fd41a85
KX
3398 if (!q->dev)
3399 return;
3400
6c954667
LM
3401 spin_lock_irq(q->queue_lock);
3402 q->rpm_status = RPM_RESUMING;
3403 spin_unlock_irq(q->queue_lock);
3404}
3405EXPORT_SYMBOL(blk_pre_runtime_resume);
3406
3407/**
3408 * blk_post_runtime_resume - Post runtime resume processing
3409 * @q: the queue of the device
3410 * @err: return value of the device's runtime_resume function
3411 *
3412 * Description:
3413 * Update the queue's runtime status according to the return value of the
3414 * device's runtime_resume function. If it is successfully resumed, process
3415 * the requests that are queued into the device's queue when it is resuming
3416 * and then mark last busy and initiate autosuspend for it.
3417 *
3418 * This function should be called near the end of the device's
3419 * runtime_resume callback.
3420 */
3421void blk_post_runtime_resume(struct request_queue *q, int err)
3422{
4fd41a85
KX
3423 if (!q->dev)
3424 return;
3425
6c954667
LM
3426 spin_lock_irq(q->queue_lock);
3427 if (!err) {
3428 q->rpm_status = RPM_ACTIVE;
3429 __blk_run_queue(q);
3430 pm_runtime_mark_last_busy(q->dev);
c60855cd 3431 pm_request_autosuspend(q->dev);
6c954667
LM
3432 } else {
3433 q->rpm_status = RPM_SUSPENDED;
3434 }
3435 spin_unlock_irq(q->queue_lock);
3436}
3437EXPORT_SYMBOL(blk_post_runtime_resume);
d07ab6d1
MW
3438
3439/**
3440 * blk_set_runtime_active - Force runtime status of the queue to be active
3441 * @q: the queue of the device
3442 *
3443 * If the device is left runtime suspended during system suspend the resume
3444 * hook typically resumes the device and corrects runtime status
3445 * accordingly. However, that does not affect the queue runtime PM status
3446 * which is still "suspended". This prevents processing requests from the
3447 * queue.
3448 *
3449 * This function can be used in driver's resume hook to correct queue
3450 * runtime PM status and re-enable peeking requests from the queue. It
3451 * should be called before first request is added to the queue.
3452 */
3453void blk_set_runtime_active(struct request_queue *q)
3454{
3455 spin_lock_irq(q->queue_lock);
3456 q->rpm_status = RPM_ACTIVE;
3457 pm_runtime_mark_last_busy(q->dev);
3458 pm_request_autosuspend(q->dev);
3459 spin_unlock_irq(q->queue_lock);
3460}
3461EXPORT_SYMBOL(blk_set_runtime_active);
6c954667
LM
3462#endif
3463
1da177e4
LT
3464int __init blk_dev_init(void)
3465{
ef295ecf
CH
3466 BUILD_BUG_ON(REQ_OP_LAST >= (1 << REQ_OP_BITS));
3467 BUILD_BUG_ON(REQ_OP_BITS + REQ_FLAG_BITS > 8 *
0762b23d 3468 FIELD_SIZEOF(struct request, cmd_flags));
ef295ecf
CH
3469 BUILD_BUG_ON(REQ_OP_BITS + REQ_FLAG_BITS > 8 *
3470 FIELD_SIZEOF(struct bio, bi_opf));
9eb55b03 3471
89b90be2
TH
3472 /* used for unplugging and affects IO latency/throughput - HIGHPRI */
3473 kblockd_workqueue = alloc_workqueue("kblockd",
28747fcd 3474 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
1da177e4
LT
3475 if (!kblockd_workqueue)
3476 panic("Failed to create kblockd\n");
3477
3478 request_cachep = kmem_cache_create("blkdev_requests",
20c2df83 3479 sizeof(struct request), 0, SLAB_PANIC, NULL);
1da177e4 3480
c2789bd4 3481 blk_requestq_cachep = kmem_cache_create("request_queue",
165125e1 3482 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
1da177e4 3483
18fbda91
OS
3484#ifdef CONFIG_DEBUG_FS
3485 blk_debugfs_root = debugfs_create_dir("block", NULL);
3486#endif
3487
d38ecf93 3488 return 0;
1da177e4 3489}