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