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