blk-mq: update hctx->nr_active in blk_mq_end_request_batch()
[linux-block.git] / block / blk-core.c
CommitLineData
3dcf60bc 1// SPDX-License-Identifier: GPL-2.0
1da177e4 2/*
1da177e4
LT
3 * Copyright (C) 1991, 1992 Linus Torvalds
4 * Copyright (C) 1994, Karl Keyte: Added support for disk statistics
5 * Elevator latency, (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
6 * Queue request tables / lock, selectable elevator, Jens Axboe <axboe@suse.de>
6728cb0e
JA
7 * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au>
8 * - July2000
1da177e4
LT
9 * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
10 */
11
12/*
13 * This handles all read/write requests to block devices
14 */
1da177e4
LT
15#include <linux/kernel.h>
16#include <linux/module.h>
1da177e4
LT
17#include <linux/bio.h>
18#include <linux/blkdev.h>
320ae51f 19#include <linux/blk-mq.h>
52abca64 20#include <linux/blk-pm.h>
fe45e630 21#include <linux/blk-integrity.h>
1da177e4
LT
22#include <linux/highmem.h>
23#include <linux/mm.h>
cee9a0c4 24#include <linux/pagemap.h>
1da177e4
LT
25#include <linux/kernel_stat.h>
26#include <linux/string.h>
27#include <linux/init.h>
1da177e4
LT
28#include <linux/completion.h>
29#include <linux/slab.h>
30#include <linux/swap.h>
31#include <linux/writeback.h>
faccbd4b 32#include <linux/task_io_accounting_ops.h>
c17bb495 33#include <linux/fault-inject.h>
73c10101 34#include <linux/list_sort.h>
e3c78ca5 35#include <linux/delay.h>
aaf7c680 36#include <linux/ratelimit.h>
6c954667 37#include <linux/pm_runtime.h>
eea8f41c 38#include <linux/blk-cgroup.h>
54d4e6ab 39#include <linux/t10-pi.h>
18fbda91 40#include <linux/debugfs.h>
30abb3a6 41#include <linux/bpf.h>
b8e24a93 42#include <linux/psi.h>
71ac860a 43#include <linux/sched/sysctl.h>
a892c8d5 44#include <linux/blk-crypto.h>
55782138
LZ
45
46#define CREATE_TRACE_POINTS
47#include <trace/events/block.h>
1da177e4 48
8324aa91 49#include "blk.h"
43a5e4e2 50#include "blk-mq.h"
bd166ef1 51#include "blk-mq-sched.h"
bca6b067 52#include "blk-pm.h"
a7b36ee6 53#include "blk-throttle.h"
8324aa91 54
18fbda91 55struct dentry *blk_debugfs_root;
18fbda91 56
d07335e5 57EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_remap);
b0da3f0d 58EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_remap);
0a82a8d1 59EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_complete);
3291fa57 60EXPORT_TRACEPOINT_SYMBOL_GPL(block_split);
cbae8d45 61EXPORT_TRACEPOINT_SYMBOL_GPL(block_unplug);
b357e4a6 62EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_insert);
0bfc2455 63
a73f730d
TH
64DEFINE_IDA(blk_queue_ida);
65
1da177e4
LT
66/*
67 * For queue allocation
68 */
6728cb0e 69struct kmem_cache *blk_requestq_cachep;
1da177e4 70
1da177e4
LT
71/*
72 * Controlling structure to kblockd
73 */
ff856bad 74static struct workqueue_struct *kblockd_workqueue;
1da177e4 75
8814ce8a
BVA
76/**
77 * blk_queue_flag_set - atomically set a queue flag
78 * @flag: flag to be set
79 * @q: request queue
80 */
81void blk_queue_flag_set(unsigned int flag, struct request_queue *q)
82{
57d74df9 83 set_bit(flag, &q->queue_flags);
8814ce8a
BVA
84}
85EXPORT_SYMBOL(blk_queue_flag_set);
86
87/**
88 * blk_queue_flag_clear - atomically clear a queue flag
89 * @flag: flag to be cleared
90 * @q: request queue
91 */
92void blk_queue_flag_clear(unsigned int flag, struct request_queue *q)
93{
57d74df9 94 clear_bit(flag, &q->queue_flags);
8814ce8a
BVA
95}
96EXPORT_SYMBOL(blk_queue_flag_clear);
97
98/**
99 * blk_queue_flag_test_and_set - atomically test and set a queue flag
100 * @flag: flag to be set
101 * @q: request queue
102 *
103 * Returns the previous value of @flag - 0 if the flag was not set and 1 if
104 * the flag was already set.
105 */
106bool blk_queue_flag_test_and_set(unsigned int flag, struct request_queue *q)
107{
57d74df9 108 return test_and_set_bit(flag, &q->queue_flags);
8814ce8a
BVA
109}
110EXPORT_SYMBOL_GPL(blk_queue_flag_test_and_set);
111
2a4aa30c 112void blk_rq_init(struct request_queue *q, struct request *rq)
1da177e4 113{
1afb20f3
FT
114 memset(rq, 0, sizeof(*rq));
115
1da177e4 116 INIT_LIST_HEAD(&rq->queuelist);
63a71386 117 rq->q = q;
a2dec7b3 118 rq->__sector = (sector_t) -1;
2e662b65
JA
119 INIT_HLIST_NODE(&rq->hash);
120 RB_CLEAR_NODE(&rq->rb_node);
e44a6a23
XT
121 rq->tag = BLK_MQ_NO_TAG;
122 rq->internal_tag = BLK_MQ_NO_TAG;
522a7775 123 rq->start_time_ns = ktime_get_ns();
09e099d4 124 rq->part = NULL;
a892c8d5 125 blk_crypto_rq_set_defaults(rq);
1da177e4 126}
2a4aa30c 127EXPORT_SYMBOL(blk_rq_init);
1da177e4 128
e47bc4ed
CK
129#define REQ_OP_NAME(name) [REQ_OP_##name] = #name
130static const char *const blk_op_name[] = {
131 REQ_OP_NAME(READ),
132 REQ_OP_NAME(WRITE),
133 REQ_OP_NAME(FLUSH),
134 REQ_OP_NAME(DISCARD),
135 REQ_OP_NAME(SECURE_ERASE),
136 REQ_OP_NAME(ZONE_RESET),
6e33dbf2 137 REQ_OP_NAME(ZONE_RESET_ALL),
6c1b1da5
AJ
138 REQ_OP_NAME(ZONE_OPEN),
139 REQ_OP_NAME(ZONE_CLOSE),
140 REQ_OP_NAME(ZONE_FINISH),
0512a75b 141 REQ_OP_NAME(ZONE_APPEND),
e47bc4ed
CK
142 REQ_OP_NAME(WRITE_SAME),
143 REQ_OP_NAME(WRITE_ZEROES),
e47bc4ed
CK
144 REQ_OP_NAME(DRV_IN),
145 REQ_OP_NAME(DRV_OUT),
146};
147#undef REQ_OP_NAME
148
149/**
150 * blk_op_str - Return string XXX in the REQ_OP_XXX.
151 * @op: REQ_OP_XXX.
152 *
153 * Description: Centralize block layer function to convert REQ_OP_XXX into
154 * string format. Useful in the debugging and tracing bio or request. For
155 * invalid REQ_OP_XXX it returns string "UNKNOWN".
156 */
157inline const char *blk_op_str(unsigned int op)
158{
159 const char *op_str = "UNKNOWN";
160
161 if (op < ARRAY_SIZE(blk_op_name) && blk_op_name[op])
162 op_str = blk_op_name[op];
163
164 return op_str;
165}
166EXPORT_SYMBOL_GPL(blk_op_str);
167
2a842aca
CH
168static const struct {
169 int errno;
170 const char *name;
171} blk_errors[] = {
172 [BLK_STS_OK] = { 0, "" },
173 [BLK_STS_NOTSUPP] = { -EOPNOTSUPP, "operation not supported" },
174 [BLK_STS_TIMEOUT] = { -ETIMEDOUT, "timeout" },
175 [BLK_STS_NOSPC] = { -ENOSPC, "critical space allocation" },
176 [BLK_STS_TRANSPORT] = { -ENOLINK, "recoverable transport" },
177 [BLK_STS_TARGET] = { -EREMOTEIO, "critical target" },
178 [BLK_STS_NEXUS] = { -EBADE, "critical nexus" },
179 [BLK_STS_MEDIUM] = { -ENODATA, "critical medium" },
180 [BLK_STS_PROTECTION] = { -EILSEQ, "protection" },
181 [BLK_STS_RESOURCE] = { -ENOMEM, "kernel resource" },
86ff7c2a 182 [BLK_STS_DEV_RESOURCE] = { -EBUSY, "device resource" },
03a07c92 183 [BLK_STS_AGAIN] = { -EAGAIN, "nonblocking retry" },
2a842aca 184
4e4cbee9
CH
185 /* device mapper special case, should not leak out: */
186 [BLK_STS_DM_REQUEUE] = { -EREMCHG, "dm internal retry" },
187
3b481d91
KB
188 /* zone device specific errors */
189 [BLK_STS_ZONE_OPEN_RESOURCE] = { -ETOOMANYREFS, "open zones exceeded" },
190 [BLK_STS_ZONE_ACTIVE_RESOURCE] = { -EOVERFLOW, "active zones exceeded" },
191
2a842aca
CH
192 /* everything else not covered above: */
193 [BLK_STS_IOERR] = { -EIO, "I/O" },
194};
195
196blk_status_t errno_to_blk_status(int errno)
197{
198 int i;
199
200 for (i = 0; i < ARRAY_SIZE(blk_errors); i++) {
201 if (blk_errors[i].errno == errno)
202 return (__force blk_status_t)i;
203 }
204
205 return BLK_STS_IOERR;
206}
207EXPORT_SYMBOL_GPL(errno_to_blk_status);
208
209int blk_status_to_errno(blk_status_t status)
210{
211 int idx = (__force int)status;
212
34bd9c1c 213 if (WARN_ON_ONCE(idx >= ARRAY_SIZE(blk_errors)))
2a842aca
CH
214 return -EIO;
215 return blk_errors[idx].errno;
216}
217EXPORT_SYMBOL_GPL(blk_status_to_errno);
218
9be3e06f 219void blk_print_req_error(struct request *req, blk_status_t status)
2a842aca
CH
220{
221 int idx = (__force int)status;
222
34bd9c1c 223 if (WARN_ON_ONCE(idx >= ARRAY_SIZE(blk_errors)))
2a842aca
CH
224 return;
225
178cc590 226 printk_ratelimited(KERN_ERR
c477b797 227 "%s error, dev %s, sector %llu op 0x%x:(%s) flags 0x%x "
b0e5168a 228 "phys_seg %u prio class %u\n",
c477b797 229 blk_errors[idx].name,
b0e5168a
CK
230 req->rq_disk ? req->rq_disk->disk_name : "?",
231 blk_rq_pos(req), req_op(req), blk_op_str(req_op(req)),
232 req->cmd_flags & ~REQ_OP_MASK,
233 req->nr_phys_segments,
234 IOPRIO_PRIO_CLASS(req->ioprio));
2a842aca
CH
235}
236
1da177e4
LT
237void blk_dump_rq_flags(struct request *rq, char *msg)
238{
aebf526b
CH
239 printk(KERN_INFO "%s: dev %s: flags=%llx\n", msg,
240 rq->rq_disk ? rq->rq_disk->disk_name : "?",
5953316d 241 (unsigned long long) rq->cmd_flags);
1da177e4 242
83096ebf
TH
243 printk(KERN_INFO " sector %llu, nr/cnr %u/%u\n",
244 (unsigned long long)blk_rq_pos(rq),
245 blk_rq_sectors(rq), blk_rq_cur_sectors(rq));
b4f42e28
JA
246 printk(KERN_INFO " bio %p, biotail %p, len %u\n",
247 rq->bio, rq->biotail, blk_rq_bytes(rq));
1da177e4 248}
1da177e4
LT
249EXPORT_SYMBOL(blk_dump_rq_flags);
250
1da177e4
LT
251/**
252 * blk_sync_queue - cancel any pending callbacks on a queue
253 * @q: the queue
254 *
255 * Description:
256 * The block layer may perform asynchronous callback activity
257 * on a queue, such as calling the unplug function after a timeout.
258 * A block device may call blk_sync_queue to ensure that any
259 * such activity is cancelled, thus allowing it to release resources
59c51591 260 * that the callbacks might use. The caller must already have made sure
c62b37d9 261 * that its ->submit_bio will not re-add plugging prior to calling
1da177e4
LT
262 * this function.
263 *
da527770 264 * This function does not cancel any asynchronous activity arising
da3dae54 265 * out of elevator or throttling code. That would require elevator_exit()
5efd6113 266 * and blkcg_exit_queue() to be called with queue lock initialized.
da527770 267 *
1da177e4
LT
268 */
269void blk_sync_queue(struct request_queue *q)
270{
70ed28b9 271 del_timer_sync(&q->timeout);
4e9b6f20 272 cancel_work_sync(&q->timeout_work);
1da177e4
LT
273}
274EXPORT_SYMBOL(blk_sync_queue);
275
c9254f2d 276/**
cd84a62e 277 * blk_set_pm_only - increment pm_only counter
c9254f2d 278 * @q: request queue pointer
c9254f2d 279 */
cd84a62e 280void blk_set_pm_only(struct request_queue *q)
c9254f2d 281{
cd84a62e 282 atomic_inc(&q->pm_only);
c9254f2d 283}
cd84a62e 284EXPORT_SYMBOL_GPL(blk_set_pm_only);
c9254f2d 285
cd84a62e 286void blk_clear_pm_only(struct request_queue *q)
c9254f2d 287{
cd84a62e
BVA
288 int pm_only;
289
290 pm_only = atomic_dec_return(&q->pm_only);
291 WARN_ON_ONCE(pm_only < 0);
292 if (pm_only == 0)
293 wake_up_all(&q->mq_freeze_wq);
c9254f2d 294}
cd84a62e 295EXPORT_SYMBOL_GPL(blk_clear_pm_only);
c9254f2d 296
b5bd357c
LC
297/**
298 * blk_put_queue - decrement the request_queue refcount
299 * @q: the request_queue structure to decrement the refcount for
300 *
301 * Decrements the refcount of the request_queue kobject. When this reaches 0
302 * we'll have blk_release_queue() called.
e8c7d14a
LC
303 *
304 * Context: Any context, but the last reference must not be dropped from
305 * atomic context.
b5bd357c 306 */
165125e1 307void blk_put_queue(struct request_queue *q)
483f4afc
AV
308{
309 kobject_put(&q->kobj);
310}
d86e0e83 311EXPORT_SYMBOL(blk_put_queue);
483f4afc 312
8e141f9e 313void blk_queue_start_drain(struct request_queue *q)
aed3ea94 314{
d3cfb2a0
ML
315 /*
316 * When queue DYING flag is set, we need to block new req
317 * entering queue, so we call blk_freeze_queue_start() to
318 * prevent I/O from crossing blk_queue_enter().
319 */
320 blk_freeze_queue_start(q);
344e9ffc 321 if (queue_is_mq(q))
aed3ea94 322 blk_mq_wake_waiters(q);
055f6e18
ML
323 /* Make blk_queue_enter() reexamine the DYING flag. */
324 wake_up_all(&q->mq_freeze_wq);
aed3ea94 325}
8e141f9e
CH
326
327void blk_set_queue_dying(struct request_queue *q)
328{
329 blk_queue_flag_set(QUEUE_FLAG_DYING, q);
330 blk_queue_start_drain(q);
331}
aed3ea94
JA
332EXPORT_SYMBOL_GPL(blk_set_queue_dying);
333
c9a929dd
TH
334/**
335 * blk_cleanup_queue - shutdown a request queue
336 * @q: request queue to shutdown
337 *
c246e80d
BVA
338 * Mark @q DYING, drain all pending requests, mark @q DEAD, destroy and
339 * put it. All future requests will be failed immediately with -ENODEV.
e8c7d14a
LC
340 *
341 * Context: can sleep
c94a96ac 342 */
6728cb0e 343void blk_cleanup_queue(struct request_queue *q)
483f4afc 344{
e8c7d14a
LC
345 /* cannot be called from atomic context */
346 might_sleep();
347
bae85c15
BVA
348 WARN_ON_ONCE(blk_queue_registered(q));
349
3f3299d5 350 /* mark @q DYING, no new request or merges will be allowed afterwards */
aed3ea94 351 blk_set_queue_dying(q);
6ecf23af 352
57d74df9
CH
353 blk_queue_flag_set(QUEUE_FLAG_NOMERGES, q);
354 blk_queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
c9a929dd 355
c246e80d
BVA
356 /*
357 * Drain all requests queued before DYING marking. Set DEAD flag to
67ed8b73
BVA
358 * prevent that blk_mq_run_hw_queues() accesses the hardware queues
359 * after draining finished.
c246e80d 360 */
3ef28e83 361 blk_freeze_queue(q);
c57cdf7a 362
57d74df9 363 blk_queue_flag_set(QUEUE_FLAG_DEAD, q);
c9a929dd 364
c9a929dd 365 blk_sync_queue(q);
344e9ffc 366 if (queue_is_mq(q))
c7e2d94b 367 blk_mq_exit_queue(q);
a1ce35fa 368
c3e22192
ML
369 /*
370 * In theory, request pool of sched_tags belongs to request queue.
371 * However, the current implementation requires tag_set for freeing
372 * requests, so free the pool now.
373 *
374 * Queue has become frozen, there can't be any in-queue requests, so
375 * it is safe to free requests now.
376 */
377 mutex_lock(&q->sysfs_lock);
378 if (q->elevator)
1820f4f0 379 blk_mq_sched_free_rqs(q);
c3e22192
ML
380 mutex_unlock(&q->sysfs_lock);
381
3ef28e83 382 percpu_ref_exit(&q->q_usage_counter);
45a9c9d9 383
c9a929dd 384 /* @q is and will stay empty, shutdown and put */
483f4afc
AV
385 blk_put_queue(q);
386}
1da177e4
LT
387EXPORT_SYMBOL(blk_cleanup_queue);
388
1f14a098
CH
389static bool blk_try_enter_queue(struct request_queue *q, bool pm)
390{
391 rcu_read_lock();
e94f6852 392 if (!percpu_ref_tryget_live_rcu(&q->q_usage_counter))
1f14a098
CH
393 goto fail;
394
395 /*
396 * The code that increments the pm_only counter must ensure that the
397 * counter is globally visible before the queue is unfrozen.
398 */
399 if (blk_queue_pm_only(q) &&
400 (!pm || queue_rpm_status(q) == RPM_SUSPENDED))
401 goto fail_put;
402
403 rcu_read_unlock();
404 return true;
405
406fail_put:
1497a51a 407 blk_queue_exit(q);
1f14a098
CH
408fail:
409 rcu_read_unlock();
410 return false;
411}
412
3a0a5299
BVA
413/**
414 * blk_queue_enter() - try to increase q->q_usage_counter
415 * @q: request queue pointer
a4d34da7 416 * @flags: BLK_MQ_REQ_NOWAIT and/or BLK_MQ_REQ_PM
3a0a5299 417 */
9a95e4ef 418int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags)
3ef28e83 419{
a4d34da7 420 const bool pm = flags & BLK_MQ_REQ_PM;
3a0a5299 421
1f14a098 422 while (!blk_try_enter_queue(q, pm)) {
3a0a5299 423 if (flags & BLK_MQ_REQ_NOWAIT)
3ef28e83
DW
424 return -EBUSY;
425
5ed61d3f 426 /*
1f14a098
CH
427 * read pair of barrier in blk_freeze_queue_start(), we need to
428 * order reading __PERCPU_REF_DEAD flag of .q_usage_counter and
429 * reading .mq_freeze_depth or queue dying flag, otherwise the
430 * following wait may never return if the two reads are
431 * reordered.
5ed61d3f
ML
432 */
433 smp_rmb();
1dc3039b 434 wait_event(q->mq_freeze_wq,
7996a8b5 435 (!q->mq_freeze_depth &&
52abca64 436 blk_pm_resume_queue(pm, q)) ||
1dc3039b 437 blk_queue_dying(q));
3ef28e83
DW
438 if (blk_queue_dying(q))
439 return -ENODEV;
3ef28e83 440 }
1f14a098
CH
441
442 return 0;
3ef28e83
DW
443}
444
accea322
CH
445static inline int bio_queue_enter(struct bio *bio)
446{
eab4e027 447 struct request_queue *q = bdev_get_queue(bio->bi_bdev);
accea322 448
a6741536 449 while (!blk_try_enter_queue(q, false)) {
eab4e027
PB
450 struct gendisk *disk = bio->bi_bdev->bd_disk;
451
a6741536 452 if (bio->bi_opf & REQ_NOWAIT) {
8e141f9e 453 if (test_bit(GD_DEAD, &disk->state))
a6741536 454 goto dead;
accea322 455 bio_wouldblock_error(bio);
a6741536
CH
456 return -EBUSY;
457 }
458
459 /*
460 * read pair of barrier in blk_freeze_queue_start(), we need to
461 * order reading __PERCPU_REF_DEAD flag of .q_usage_counter and
462 * reading .mq_freeze_depth or queue dying flag, otherwise the
463 * following wait may never return if the two reads are
464 * reordered.
465 */
466 smp_rmb();
467 wait_event(q->mq_freeze_wq,
468 (!q->mq_freeze_depth &&
469 blk_pm_resume_queue(false, q)) ||
8e141f9e
CH
470 test_bit(GD_DEAD, &disk->state));
471 if (test_bit(GD_DEAD, &disk->state))
a6741536 472 goto dead;
accea322
CH
473 }
474
a6741536
CH
475 return 0;
476dead:
477 bio_io_error(bio);
478 return -ENODEV;
accea322
CH
479}
480
3ef28e83
DW
481void blk_queue_exit(struct request_queue *q)
482{
483 percpu_ref_put(&q->q_usage_counter);
484}
485
486static void blk_queue_usage_counter_release(struct percpu_ref *ref)
487{
488 struct request_queue *q =
489 container_of(ref, struct request_queue, q_usage_counter);
490
491 wake_up_all(&q->mq_freeze_wq);
492}
493
bca237a5 494static void blk_rq_timed_out_timer(struct timer_list *t)
287922eb 495{
bca237a5 496 struct request_queue *q = from_timer(q, t, timeout);
287922eb
CH
497
498 kblockd_schedule_work(&q->timeout_work);
499}
500
2e3c18d0
TH
501static void blk_timeout_work(struct work_struct *work)
502{
503}
504
c62b37d9 505struct request_queue *blk_alloc_queue(int node_id)
1946089a 506{
165125e1 507 struct request_queue *q;
338aa96d 508 int ret;
1946089a 509
8324aa91 510 q = kmem_cache_alloc_node(blk_requestq_cachep,
3d745ea5 511 GFP_KERNEL | __GFP_ZERO, node_id);
1da177e4
LT
512 if (!q)
513 return NULL;
514
cbf62af3 515 q->last_merge = NULL;
cbf62af3 516
3d745ea5 517 q->id = ida_simple_get(&blk_queue_ida, 0, 0, GFP_KERNEL);
a73f730d 518 if (q->id < 0)
3d2936f4 519 goto fail_q;
a73f730d 520
c495a176 521 ret = bioset_init(&q->bio_split, BIO_POOL_SIZE, 0, 0);
338aa96d 522 if (ret)
54efd50b
KO
523 goto fail_id;
524
a83b576c
JA
525 q->stats = blk_alloc_queue_stats();
526 if (!q->stats)
edb0872f 527 goto fail_split;
a83b576c 528
5151412d 529 q->node = node_id;
0989a025 530
079a2e3e 531 atomic_set(&q->nr_active_requests_shared_tags, 0);
bccf5e26 532
bca237a5 533 timer_setup(&q->timeout, blk_rq_timed_out_timer, 0);
2e3c18d0 534 INIT_WORK(&q->timeout_work, blk_timeout_work);
a612fddf 535 INIT_LIST_HEAD(&q->icq_list);
4eef3049 536#ifdef CONFIG_BLK_CGROUP
e8989fae 537 INIT_LIST_HEAD(&q->blkg_list);
4eef3049 538#endif
483f4afc 539
8324aa91 540 kobject_init(&q->kobj, &blk_queue_ktype);
1da177e4 541
85e0cbbb 542 mutex_init(&q->debugfs_mutex);
483f4afc 543 mutex_init(&q->sysfs_lock);
cecf5d87 544 mutex_init(&q->sysfs_dir_lock);
0d945c1f 545 spin_lock_init(&q->queue_lock);
c94a96ac 546
320ae51f 547 init_waitqueue_head(&q->mq_freeze_wq);
7996a8b5 548 mutex_init(&q->mq_freeze_lock);
320ae51f 549
3ef28e83
DW
550 /*
551 * Init percpu_ref in atomic mode so that it's faster to shutdown.
552 * See blk_register_queue() for details.
553 */
554 if (percpu_ref_init(&q->q_usage_counter,
555 blk_queue_usage_counter_release,
556 PERCPU_REF_INIT_ATOMIC, GFP_KERNEL))
edb0872f 557 goto fail_stats;
f51b802c 558
3ef28e83
DW
559 if (blkcg_init_queue(q))
560 goto fail_ref;
561
3d745ea5
CH
562 blk_queue_dma_alignment(q, 511);
563 blk_set_default_limits(&q->limits);
d2a27964 564 q->nr_requests = BLKDEV_DEFAULT_RQ;
3d745ea5 565
1da177e4 566 return q;
a73f730d 567
3ef28e83
DW
568fail_ref:
569 percpu_ref_exit(&q->q_usage_counter);
a83b576c 570fail_stats:
edb0872f 571 blk_free_queue_stats(q->stats);
54efd50b 572fail_split:
338aa96d 573 bioset_exit(&q->bio_split);
a73f730d
TH
574fail_id:
575 ida_simple_remove(&blk_queue_ida, q->id);
576fail_q:
577 kmem_cache_free(blk_requestq_cachep, q);
578 return NULL;
1da177e4 579}
1da177e4 580
b5bd357c
LC
581/**
582 * blk_get_queue - increment the request_queue refcount
583 * @q: the request_queue structure to increment the refcount for
584 *
585 * Increment the refcount of the request_queue kobject.
763b5892
LC
586 *
587 * Context: Any context.
b5bd357c 588 */
09ac46c4 589bool blk_get_queue(struct request_queue *q)
1da177e4 590{
3f3299d5 591 if (likely(!blk_queue_dying(q))) {
09ac46c4
TH
592 __blk_get_queue(q);
593 return true;
1da177e4
LT
594 }
595
09ac46c4 596 return false;
1da177e4 597}
d86e0e83 598EXPORT_SYMBOL(blk_get_queue);
1da177e4 599
a1ce35fa
JA
600/**
601 * blk_get_request - allocate a request
602 * @q: request queue to allocate a request for
603 * @op: operation (REQ_OP_*) and REQ_* flags, e.g. REQ_SYNC.
604 * @flags: BLK_MQ_REQ_* flags, e.g. BLK_MQ_REQ_NOWAIT.
1da177e4 605 */
a1ce35fa
JA
606struct request *blk_get_request(struct request_queue *q, unsigned int op,
607 blk_mq_req_flags_t flags)
1da177e4 608{
a1ce35fa 609 struct request *req;
1da177e4 610
a1ce35fa 611 WARN_ON_ONCE(op & REQ_NOWAIT);
a4d34da7 612 WARN_ON_ONCE(flags & ~(BLK_MQ_REQ_NOWAIT | BLK_MQ_REQ_PM));
1da177e4 613
a1ce35fa
JA
614 req = blk_mq_alloc_request(q, op, flags);
615 if (!IS_ERR(req) && q->mq_ops->initialize_rq_fn)
616 q->mq_ops->initialize_rq_fn(req);
1da177e4 617
a1ce35fa 618 return req;
1da177e4 619}
a1ce35fa 620EXPORT_SYMBOL(blk_get_request);
1da177e4 621
1da177e4
LT
622void blk_put_request(struct request *req)
623{
a1ce35fa 624 blk_mq_free_request(req);
1da177e4 625}
1da177e4
LT
626EXPORT_SYMBOL(blk_put_request);
627
52c5e62d 628static void handle_bad_sector(struct bio *bio, sector_t maxsector)
1da177e4
LT
629{
630 char b[BDEVNAME_SIZE];
631
8a3ee677 632 pr_info_ratelimited("%s: attempt to access beyond end of device\n"
f4ac712e 633 "%s: rw=%d, want=%llu, limit=%llu\n",
8a3ee677 634 current->comm,
f4ac712e
TH
635 bio_devname(bio, b), bio->bi_opf,
636 bio_end_sector(bio), maxsector);
1da177e4
LT
637}
638
c17bb495
AM
639#ifdef CONFIG_FAIL_MAKE_REQUEST
640
641static DECLARE_FAULT_ATTR(fail_make_request);
642
643static int __init setup_fail_make_request(char *str)
644{
645 return setup_fault_attr(&fail_make_request, str);
646}
647__setup("fail_make_request=", setup_fail_make_request);
648
8446fe92 649static bool should_fail_request(struct block_device *part, unsigned int bytes)
c17bb495 650{
8446fe92 651 return part->bd_make_it_fail && should_fail(&fail_make_request, bytes);
c17bb495
AM
652}
653
654static int __init fail_make_request_debugfs(void)
655{
dd48c085
AM
656 struct dentry *dir = fault_create_debugfs_attr("fail_make_request",
657 NULL, &fail_make_request);
658
21f9fcd8 659 return PTR_ERR_OR_ZERO(dir);
c17bb495
AM
660}
661
662late_initcall(fail_make_request_debugfs);
663
664#else /* CONFIG_FAIL_MAKE_REQUEST */
665
8446fe92 666static inline bool should_fail_request(struct block_device *part,
b2c9cd37 667 unsigned int bytes)
c17bb495 668{
b2c9cd37 669 return false;
c17bb495
AM
670}
671
672#endif /* CONFIG_FAIL_MAKE_REQUEST */
673
2f9f6221 674static inline bool bio_check_ro(struct bio *bio)
721c7fc7 675{
2f9f6221 676 if (op_is_write(bio_op(bio)) && bdev_read_only(bio->bi_bdev)) {
721c7fc7
ID
677 char b[BDEVNAME_SIZE];
678
8b2ded1c
MP
679 if (op_is_flush(bio->bi_opf) && !bio_sectors(bio))
680 return false;
681
a32e236e 682 WARN_ONCE(1,
c8178674 683 "Trying to write to read-only block-device %s (partno %d)\n",
2f9f6221 684 bio_devname(bio, b), bio->bi_bdev->bd_partno);
a32e236e
LT
685 /* Older lvm-tools actually trigger this */
686 return false;
721c7fc7
ID
687 }
688
689 return false;
690}
691
30abb3a6
HM
692static noinline int should_fail_bio(struct bio *bio)
693{
309dca30 694 if (should_fail_request(bdev_whole(bio->bi_bdev), bio->bi_iter.bi_size))
30abb3a6
HM
695 return -EIO;
696 return 0;
697}
698ALLOW_ERROR_INJECTION(should_fail_bio, ERRNO);
699
52c5e62d
CH
700/*
701 * Check whether this bio extends beyond the end of the device or partition.
702 * This may well happen - the kernel calls bread() without checking the size of
703 * the device, e.g., when mounting a file system.
704 */
2f9f6221 705static inline int bio_check_eod(struct bio *bio)
52c5e62d 706{
2f9f6221 707 sector_t maxsector = bdev_nr_sectors(bio->bi_bdev);
52c5e62d
CH
708 unsigned int nr_sectors = bio_sectors(bio);
709
710 if (nr_sectors && maxsector &&
711 (nr_sectors > maxsector ||
712 bio->bi_iter.bi_sector > maxsector - nr_sectors)) {
713 handle_bad_sector(bio, maxsector);
714 return -EIO;
715 }
716 return 0;
717}
718
74d46992
CH
719/*
720 * Remap block n of partition p to block n+start(p) of the disk.
721 */
2f9f6221 722static int blk_partition_remap(struct bio *bio)
74d46992 723{
309dca30 724 struct block_device *p = bio->bi_bdev;
74d46992 725
52c5e62d 726 if (unlikely(should_fail_request(p, bio->bi_iter.bi_size)))
2f9f6221 727 return -EIO;
5eac3eb3 728 if (bio_sectors(bio)) {
8446fe92 729 bio->bi_iter.bi_sector += p->bd_start_sect;
1c02fca6 730 trace_block_bio_remap(bio, p->bd_dev,
29ff57c6 731 bio->bi_iter.bi_sector -
8446fe92 732 p->bd_start_sect);
52c5e62d 733 }
30c5d345 734 bio_set_flag(bio, BIO_REMAPPED);
2f9f6221 735 return 0;
74d46992
CH
736}
737
0512a75b
KB
738/*
739 * Check write append to a zoned block device.
740 */
741static inline blk_status_t blk_check_zone_append(struct request_queue *q,
742 struct bio *bio)
743{
744 sector_t pos = bio->bi_iter.bi_sector;
745 int nr_sectors = bio_sectors(bio);
746
747 /* Only applicable to zoned block devices */
748 if (!blk_queue_is_zoned(q))
749 return BLK_STS_NOTSUPP;
750
751 /* The bio sector must point to the start of a sequential zone */
752 if (pos & (blk_queue_zone_sectors(q) - 1) ||
753 !blk_queue_zone_is_seq(q, pos))
754 return BLK_STS_IOERR;
755
756 /*
757 * Not allowed to cross zone boundaries. Otherwise, the BIO will be
758 * split and could result in non-contiguous sectors being written in
759 * different zones.
760 */
761 if (nr_sectors > q->limits.chunk_sectors)
762 return BLK_STS_IOERR;
763
764 /* Make sure the BIO is small enough and will not get split */
765 if (nr_sectors > q->limits.max_zone_append_sectors)
766 return BLK_STS_IOERR;
767
768 bio->bi_opf |= REQ_NOMERGE;
769
770 return BLK_STS_OK;
771}
772
ed00aabd 773static noinline_for_stack bool submit_bio_checks(struct bio *bio)
1da177e4 774{
309dca30 775 struct block_device *bdev = bio->bi_bdev;
eab4e027 776 struct request_queue *q = bdev_get_queue(bdev);
4e4cbee9 777 blk_status_t status = BLK_STS_IOERR;
5a473e83 778 struct blk_plug *plug;
1da177e4
LT
779
780 might_sleep();
1da177e4 781
5a473e83
JA
782 plug = blk_mq_plug(q, bio);
783 if (plug && plug->nowait)
784 bio->bi_opf |= REQ_NOWAIT;
785
03a07c92 786 /*
b0beb280 787 * For a REQ_NOWAIT based request, return -EOPNOTSUPP
021a2446 788 * if queue does not support NOWAIT.
03a07c92 789 */
021a2446 790 if ((bio->bi_opf & REQ_NOWAIT) && !blk_queue_nowait(q))
b0beb280 791 goto not_supported;
03a07c92 792
30abb3a6 793 if (should_fail_bio(bio))
5a7bbad2 794 goto end_io;
2f9f6221
CH
795 if (unlikely(bio_check_ro(bio)))
796 goto end_io;
3a905c37
CH
797 if (!bio_flagged(bio, BIO_REMAPPED)) {
798 if (unlikely(bio_check_eod(bio)))
799 goto end_io;
800 if (bdev->bd_partno && unlikely(blk_partition_remap(bio)))
801 goto end_io;
802 }
2056a782 803
5a7bbad2 804 /*
ed00aabd
CH
805 * Filter flush bio's early so that bio based drivers without flush
806 * support don't have to worry about them.
5a7bbad2 807 */
f3a8ab7d 808 if (op_is_flush(bio->bi_opf) &&
c888a8f9 809 !test_bit(QUEUE_FLAG_WC, &q->queue_flags)) {
1eff9d32 810 bio->bi_opf &= ~(REQ_PREFLUSH | REQ_FUA);
e439ab71 811 if (!bio_sectors(bio)) {
4e4cbee9 812 status = BLK_STS_OK;
51fd77bd
JA
813 goto end_io;
814 }
5a7bbad2 815 }
5ddfe969 816
d04c406f 817 if (!test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
6ce913fe 818 bio_clear_polled(bio);
d04c406f 819
288dab8a
CH
820 switch (bio_op(bio)) {
821 case REQ_OP_DISCARD:
822 if (!blk_queue_discard(q))
823 goto not_supported;
824 break;
825 case REQ_OP_SECURE_ERASE:
826 if (!blk_queue_secure_erase(q))
827 goto not_supported;
828 break;
829 case REQ_OP_WRITE_SAME:
74d46992 830 if (!q->limits.max_write_same_sectors)
288dab8a 831 goto not_supported;
58886785 832 break;
0512a75b
KB
833 case REQ_OP_ZONE_APPEND:
834 status = blk_check_zone_append(q, bio);
835 if (status != BLK_STS_OK)
836 goto end_io;
837 break;
2d253440 838 case REQ_OP_ZONE_RESET:
6c1b1da5
AJ
839 case REQ_OP_ZONE_OPEN:
840 case REQ_OP_ZONE_CLOSE:
841 case REQ_OP_ZONE_FINISH:
74d46992 842 if (!blk_queue_is_zoned(q))
2d253440 843 goto not_supported;
288dab8a 844 break;
6e33dbf2
CK
845 case REQ_OP_ZONE_RESET_ALL:
846 if (!blk_queue_is_zoned(q) || !blk_queue_zone_resetall(q))
847 goto not_supported;
848 break;
a6f0788e 849 case REQ_OP_WRITE_ZEROES:
74d46992 850 if (!q->limits.max_write_zeroes_sectors)
a6f0788e
CK
851 goto not_supported;
852 break;
288dab8a
CH
853 default:
854 break;
5a7bbad2 855 }
01edede4 856
7f4b35d1 857 /*
3e82c348
CH
858 * Various block parts want %current->io_context, so allocate it up
859 * front rather than dealing with lots of pain to allocate it only
860 * where needed. This may fail and the block layer knows how to live
861 * with it.
7f4b35d1 862 */
3e82c348
CH
863 if (unlikely(!current->io_context))
864 create_task_io_context(current, GFP_ATOMIC, q->node);
7f4b35d1 865
db18a53e
CH
866 if (blk_throtl_bio(bio)) {
867 blkcg_bio_issue_init(bio);
ae118896 868 return false;
db18a53e
CH
869 }
870
871 blk_cgroup_bio_start(bio);
872 blkcg_bio_issue_init(bio);
27a84d54 873
fbbaf700 874 if (!bio_flagged(bio, BIO_TRACE_COMPLETION)) {
e8a676d6 875 trace_block_bio_queue(bio);
fbbaf700
N
876 /* Now that enqueuing has been traced, we need to trace
877 * completion as well.
878 */
879 bio_set_flag(bio, BIO_TRACE_COMPLETION);
880 }
27a84d54 881 return true;
a7384677 882
288dab8a 883not_supported:
4e4cbee9 884 status = BLK_STS_NOTSUPP;
a7384677 885end_io:
4e4cbee9 886 bio->bi_status = status;
4246a0b6 887 bio_endio(bio);
27a84d54 888 return false;
1da177e4
LT
889}
890
3e08773c 891static void __submit_bio(struct bio *bio)
ac7c5675 892{
309dca30 893 struct gendisk *disk = bio->bi_bdev->bd_disk;
ac7c5675 894
cc9c884d 895 if (unlikely(bio_queue_enter(bio) != 0))
3e08773c 896 return;
cc9c884d
CH
897
898 if (!submit_bio_checks(bio) || !blk_crypto_bio_prep(&bio))
899 goto queue_exit;
3e08773c
CH
900 if (!disk->fops->submit_bio) {
901 blk_mq_submit_bio(bio);
902 return;
ac7c5675 903 }
3e08773c 904 disk->fops->submit_bio(bio);
cc9c884d 905queue_exit:
c62b37d9 906 blk_queue_exit(disk->queue);
ac7c5675
CH
907}
908
566acf2d
CH
909/*
910 * The loop in this function may be a bit non-obvious, and so deserves some
911 * explanation:
912 *
913 * - Before entering the loop, bio->bi_next is NULL (as all callers ensure
914 * that), so we have a list with a single bio.
915 * - We pretend that we have just taken it off a longer list, so we assign
916 * bio_list to a pointer to the bio_list_on_stack, thus initialising the
917 * bio_list of new bios to be added. ->submit_bio() may indeed add some more
918 * bios through a recursive call to submit_bio_noacct. If it did, we find a
919 * non-NULL value in bio_list and re-enter the loop from the top.
920 * - In this case we really did just take the bio of the top of the list (no
921 * pretending) and so remove it from bio_list, and call into ->submit_bio()
922 * again.
923 *
924 * bio_list_on_stack[0] contains bios submitted by the current ->submit_bio.
925 * bio_list_on_stack[1] contains bios that were submitted before the current
926 * ->submit_bio_bio, but that haven't been processed yet.
927 */
3e08773c 928static void __submit_bio_noacct(struct bio *bio)
566acf2d
CH
929{
930 struct bio_list bio_list_on_stack[2];
566acf2d
CH
931
932 BUG_ON(bio->bi_next);
933
934 bio_list_init(&bio_list_on_stack[0]);
935 current->bio_list = bio_list_on_stack;
936
937 do {
eab4e027 938 struct request_queue *q = bdev_get_queue(bio->bi_bdev);
566acf2d
CH
939 struct bio_list lower, same;
940
566acf2d
CH
941 /*
942 * Create a fresh bio_list for all subordinate requests.
943 */
944 bio_list_on_stack[1] = bio_list_on_stack[0];
945 bio_list_init(&bio_list_on_stack[0]);
946
3e08773c 947 __submit_bio(bio);
566acf2d
CH
948
949 /*
950 * Sort new bios into those for a lower level and those for the
951 * same level.
952 */
953 bio_list_init(&lower);
954 bio_list_init(&same);
955 while ((bio = bio_list_pop(&bio_list_on_stack[0])) != NULL)
eab4e027 956 if (q == bdev_get_queue(bio->bi_bdev))
566acf2d
CH
957 bio_list_add(&same, bio);
958 else
959 bio_list_add(&lower, bio);
960
961 /*
962 * Now assemble so we handle the lowest level first.
963 */
964 bio_list_merge(&bio_list_on_stack[0], &lower);
965 bio_list_merge(&bio_list_on_stack[0], &same);
966 bio_list_merge(&bio_list_on_stack[0], &bio_list_on_stack[1]);
967 } while ((bio = bio_list_pop(&bio_list_on_stack[0])));
968
969 current->bio_list = NULL;
566acf2d
CH
970}
971
3e08773c 972static void __submit_bio_noacct_mq(struct bio *bio)
ff93ea0c 973{
7c792f33 974 struct bio_list bio_list[2] = { };
ff93ea0c 975
7c792f33 976 current->bio_list = bio_list;
ff93ea0c
CH
977
978 do {
3e08773c 979 __submit_bio(bio);
7c792f33 980 } while ((bio = bio_list_pop(&bio_list[0])));
ff93ea0c
CH
981
982 current->bio_list = NULL;
ff93ea0c
CH
983}
984
27a84d54 985/**
ed00aabd 986 * submit_bio_noacct - re-submit a bio to the block device layer for I/O
27a84d54
CH
987 * @bio: The bio describing the location in memory and on the device.
988 *
3fdd4086
CH
989 * This is a version of submit_bio() that shall only be used for I/O that is
990 * resubmitted to lower level drivers by stacking block drivers. All file
991 * systems and other upper level users of the block layer should use
992 * submit_bio() instead.
d89d8796 993 */
3e08773c 994void submit_bio_noacct(struct bio *bio)
d89d8796 995{
27a84d54 996 /*
566acf2d
CH
997 * We only want one ->submit_bio to be active at a time, else stack
998 * usage with stacked devices could be a problem. Use current->bio_list
999 * to collect a list of requests submited by a ->submit_bio method while
1000 * it is active, and then process them after it returned.
27a84d54 1001 */
3e08773c 1002 if (current->bio_list)
f5fe1b51 1003 bio_list_add(&current->bio_list[0], bio);
3e08773c
CH
1004 else if (!bio->bi_bdev->bd_disk->fops->submit_bio)
1005 __submit_bio_noacct_mq(bio);
1006 else
1007 __submit_bio_noacct(bio);
d89d8796 1008}
ed00aabd 1009EXPORT_SYMBOL(submit_bio_noacct);
1da177e4
LT
1010
1011/**
710027a4 1012 * submit_bio - submit a bio to the block device layer for I/O
1da177e4
LT
1013 * @bio: The &struct bio which describes the I/O
1014 *
3fdd4086
CH
1015 * submit_bio() is used to submit I/O requests to block devices. It is passed a
1016 * fully set up &struct bio that describes the I/O that needs to be done. The
309dca30 1017 * bio will be send to the device described by the bi_bdev field.
1da177e4 1018 *
3fdd4086
CH
1019 * The success/failure status of the request, along with notification of
1020 * completion, is delivered asynchronously through the ->bi_end_io() callback
1021 * in @bio. The bio must NOT be touched by thecaller until ->bi_end_io() has
1022 * been called.
1da177e4 1023 */
3e08773c 1024void submit_bio(struct bio *bio)
1da177e4 1025{
d3f77dfd 1026 if (blkcg_punt_bio_submit(bio))
3e08773c 1027 return;
d3f77dfd 1028
bf2de6f5
JA
1029 /*
1030 * If it's a regular read/write or a barrier with data attached,
1031 * go through the normal accounting stuff before submission.
1032 */
e2a60da7 1033 if (bio_has_data(bio)) {
4363ac7c
MP
1034 unsigned int count;
1035
95fe6c1a 1036 if (unlikely(bio_op(bio) == REQ_OP_WRITE_SAME))
309dca30 1037 count = queue_logical_block_size(
eab4e027 1038 bdev_get_queue(bio->bi_bdev)) >> 9;
4363ac7c
MP
1039 else
1040 count = bio_sectors(bio);
1041
a8ebb056 1042 if (op_is_write(bio_op(bio))) {
bf2de6f5
JA
1043 count_vm_events(PGPGOUT, count);
1044 } else {
4f024f37 1045 task_io_account_read(bio->bi_iter.bi_size);
bf2de6f5
JA
1046 count_vm_events(PGPGIN, count);
1047 }
1da177e4
LT
1048 }
1049
b8e24a93 1050 /*
760f83ea
CH
1051 * If we're reading data that is part of the userspace workingset, count
1052 * submission time as memory stall. When the device is congested, or
1053 * the submitting cgroup IO-throttled, submission can be a significant
1054 * part of overall IO time.
b8e24a93 1055 */
760f83ea
CH
1056 if (unlikely(bio_op(bio) == REQ_OP_READ &&
1057 bio_flagged(bio, BIO_WORKINGSET))) {
1058 unsigned long pflags;
b8e24a93 1059
760f83ea 1060 psi_memstall_enter(&pflags);
3e08773c 1061 submit_bio_noacct(bio);
b8e24a93 1062 psi_memstall_leave(&pflags);
3e08773c 1063 return;
760f83ea
CH
1064 }
1065
3e08773c 1066 submit_bio_noacct(bio);
1da177e4 1067}
1da177e4
LT
1068EXPORT_SYMBOL(submit_bio);
1069
3e08773c
CH
1070/**
1071 * bio_poll - poll for BIO completions
1072 * @bio: bio to poll for
1073 * @flags: BLK_POLL_* flags that control the behavior
1074 *
1075 * Poll for completions on queue associated with the bio. Returns number of
1076 * completed entries found.
1077 *
1078 * Note: the caller must either be the context that submitted @bio, or
1079 * be in a RCU critical section to prevent freeing of @bio.
1080 */
5a72e899 1081int bio_poll(struct bio *bio, struct io_comp_batch *iob, unsigned int flags)
3e08773c 1082{
859897c3 1083 struct request_queue *q = bdev_get_queue(bio->bi_bdev);
3e08773c
CH
1084 blk_qc_t cookie = READ_ONCE(bio->bi_cookie);
1085 int ret;
1086
1087 if (cookie == BLK_QC_T_NONE ||
1088 !test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
1089 return 0;
1090
1091 if (current->plug)
008f75a2 1092 blk_flush_plug(current->plug, false);
3e08773c
CH
1093
1094 if (blk_queue_enter(q, BLK_MQ_REQ_NOWAIT))
1095 return 0;
1096 if (WARN_ON_ONCE(!queue_is_mq(q)))
1097 ret = 0; /* not yet implemented, should not happen */
1098 else
5a72e899 1099 ret = blk_mq_poll(q, cookie, iob, flags);
3e08773c
CH
1100 blk_queue_exit(q);
1101 return ret;
1102}
1103EXPORT_SYMBOL_GPL(bio_poll);
1104
1105/*
1106 * Helper to implement file_operations.iopoll. Requires the bio to be stored
1107 * in iocb->private, and cleared before freeing the bio.
1108 */
5a72e899
JA
1109int iocb_bio_iopoll(struct kiocb *kiocb, struct io_comp_batch *iob,
1110 unsigned int flags)
3e08773c
CH
1111{
1112 struct bio *bio;
1113 int ret = 0;
1114
1115 /*
1116 * Note: the bio cache only uses SLAB_TYPESAFE_BY_RCU, so bio can
1117 * point to a freshly allocated bio at this point. If that happens
1118 * we have a few cases to consider:
1119 *
1120 * 1) the bio is beeing initialized and bi_bdev is NULL. We can just
1121 * simply nothing in this case
1122 * 2) the bio points to a not poll enabled device. bio_poll will catch
1123 * this and return 0
1124 * 3) the bio points to a poll capable device, including but not
1125 * limited to the one that the original bio pointed to. In this
1126 * case we will call into the actual poll method and poll for I/O,
1127 * even if we don't need to, but it won't cause harm either.
1128 *
1129 * For cases 2) and 3) above the RCU grace period ensures that bi_bdev
1130 * is still allocated. Because partitions hold a reference to the whole
1131 * device bdev and thus disk, the disk is also still valid. Grabbing
1132 * a reference to the queue in bio_poll() ensures the hctxs and requests
1133 * are still valid as well.
1134 */
1135 rcu_read_lock();
1136 bio = READ_ONCE(kiocb->private);
1137 if (bio && bio->bi_bdev)
5a72e899 1138 ret = bio_poll(bio, iob, flags);
3e08773c
CH
1139 rcu_read_unlock();
1140
1141 return ret;
1142}
1143EXPORT_SYMBOL_GPL(iocb_bio_iopoll);
1144
82124d60 1145/**
bf4e6b4e 1146 * blk_cloned_rq_check_limits - Helper function to check a cloned request
0d720318 1147 * for the new queue limits
82124d60
KU
1148 * @q: the queue
1149 * @rq: the request being checked
1150 *
1151 * Description:
1152 * @rq may have been made based on weaker limitations of upper-level queues
1153 * in request stacking drivers, and it may violate the limitation of @q.
1154 * Since the block layer and the underlying device driver trust @rq
1155 * after it is inserted to @q, it should be checked against @q before
1156 * the insertion using this generic function.
1157 *
82124d60 1158 * Request stacking drivers like request-based dm may change the queue
bf4e6b4e
HR
1159 * limits when retrying requests on other queues. Those requests need
1160 * to be checked against the new queue limits again during dispatch.
82124d60 1161 */
143d2600 1162static blk_status_t blk_cloned_rq_check_limits(struct request_queue *q,
bf4e6b4e 1163 struct request *rq)
82124d60 1164{
8327cce5
RS
1165 unsigned int max_sectors = blk_queue_get_max_sectors(q, req_op(rq));
1166
1167 if (blk_rq_sectors(rq) > max_sectors) {
1168 /*
1169 * SCSI device does not have a good way to return if
1170 * Write Same/Zero is actually supported. If a device rejects
1171 * a non-read/write command (discard, write same,etc.) the
1172 * low-level device driver will set the relevant queue limit to
1173 * 0 to prevent blk-lib from issuing more of the offending
1174 * operations. Commands queued prior to the queue limit being
1175 * reset need to be completed with BLK_STS_NOTSUPP to avoid I/O
1176 * errors being propagated to upper layers.
1177 */
1178 if (max_sectors == 0)
1179 return BLK_STS_NOTSUPP;
1180
61939b12 1181 printk(KERN_ERR "%s: over max size limit. (%u > %u)\n",
8327cce5 1182 __func__, blk_rq_sectors(rq), max_sectors);
143d2600 1183 return BLK_STS_IOERR;
82124d60
KU
1184 }
1185
1186 /*
9bb33f24
CH
1187 * The queue settings related to segment counting may differ from the
1188 * original queue.
82124d60 1189 */
e9cd19c0 1190 rq->nr_phys_segments = blk_recalc_rq_segments(rq);
8a78362c 1191 if (rq->nr_phys_segments > queue_max_segments(q)) {
61939b12
JP
1192 printk(KERN_ERR "%s: over max segments limit. (%hu > %hu)\n",
1193 __func__, rq->nr_phys_segments, queue_max_segments(q));
143d2600 1194 return BLK_STS_IOERR;
82124d60
KU
1195 }
1196
143d2600 1197 return BLK_STS_OK;
82124d60 1198}
82124d60
KU
1199
1200/**
1201 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
1202 * @q: the queue to submit the request
1203 * @rq: the request being queued
1204 */
2a842aca 1205blk_status_t blk_insert_cloned_request(struct request_queue *q, struct request *rq)
82124d60 1206{
8327cce5
RS
1207 blk_status_t ret;
1208
1209 ret = blk_cloned_rq_check_limits(q, rq);
1210 if (ret != BLK_STS_OK)
1211 return ret;
82124d60 1212
b2c9cd37 1213 if (rq->rq_disk &&
8446fe92 1214 should_fail_request(rq->rq_disk->part0, blk_rq_bytes(rq)))
2a842aca 1215 return BLK_STS_IOERR;
82124d60 1216
a892c8d5
ST
1217 if (blk_crypto_insert_cloned_request(rq))
1218 return BLK_STS_IOERR;
1219
be6bfe36 1220 blk_account_io_start(rq);
82124d60
KU
1221
1222 /*
a1ce35fa
JA
1223 * Since we have a scheduler attached on the top device,
1224 * bypass a potential scheduler on the bottom device for
1225 * insert.
82124d60 1226 */
fd9c40f6 1227 return blk_mq_request_issue_directly(rq, true);
82124d60
KU
1228}
1229EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
1230
80a761fd
TH
1231/**
1232 * blk_rq_err_bytes - determine number of bytes till the next failure boundary
1233 * @rq: request to examine
1234 *
1235 * Description:
1236 * A request could be merge of IOs which require different failure
1237 * handling. This function determines the number of bytes which
1238 * can be failed from the beginning of the request without
1239 * crossing into area which need to be retried further.
1240 *
1241 * Return:
1242 * The number of bytes to fail.
80a761fd
TH
1243 */
1244unsigned int blk_rq_err_bytes(const struct request *rq)
1245{
1246 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
1247 unsigned int bytes = 0;
1248 struct bio *bio;
1249
e8064021 1250 if (!(rq->rq_flags & RQF_MIXED_MERGE))
80a761fd
TH
1251 return blk_rq_bytes(rq);
1252
1253 /*
1254 * Currently the only 'mixing' which can happen is between
1255 * different fastfail types. We can safely fail portions
1256 * which have all the failfast bits that the first one has -
1257 * the ones which are at least as eager to fail as the first
1258 * one.
1259 */
1260 for (bio = rq->bio; bio; bio = bio->bi_next) {
1eff9d32 1261 if ((bio->bi_opf & ff) != ff)
80a761fd 1262 break;
4f024f37 1263 bytes += bio->bi_iter.bi_size;
80a761fd
TH
1264 }
1265
1266 /* this could lead to infinite loop */
1267 BUG_ON(blk_rq_bytes(rq) && !bytes);
1268 return bytes;
1269}
1270EXPORT_SYMBOL_GPL(blk_rq_err_bytes);
1271
8446fe92
CH
1272static void update_io_ticks(struct block_device *part, unsigned long now,
1273 bool end)
9123bf6f
CH
1274{
1275 unsigned long stamp;
1276again:
8446fe92 1277 stamp = READ_ONCE(part->bd_stamp);
d80c228d 1278 if (unlikely(time_after(now, stamp))) {
8446fe92 1279 if (likely(cmpxchg(&part->bd_stamp, stamp, now) == stamp))
9123bf6f
CH
1280 __part_stat_add(part, io_ticks, end ? now - stamp : 1);
1281 }
8446fe92
CH
1282 if (part->bd_partno) {
1283 part = bdev_whole(part);
9123bf6f
CH
1284 goto again;
1285 }
1286}
1287
be6bfe36 1288void __blk_account_io_done(struct request *req, u64 now)
bc58ba94 1289{
be6bfe36 1290 const int sgrp = op_stat_group(req_op(req));
bc58ba94 1291
be6bfe36
PB
1292 part_stat_lock();
1293 update_io_ticks(req->part, jiffies, true);
1294 part_stat_inc(req->part, ios[sgrp]);
1295 part_stat_add(req->part, nsecs[sgrp], now - req->start_time_ns);
1296 part_stat_unlock();
bc58ba94
JA
1297}
1298
be6bfe36 1299void __blk_account_io_start(struct request *rq)
320ae51f 1300{
0b6e522c
CH
1301 /* passthrough requests can hold bios that do not have ->bi_bdev set */
1302 if (rq->bio && rq->bio->bi_bdev)
1303 rq->part = rq->bio->bi_bdev;
1304 else
1305 rq->part = rq->rq_disk->part0;
524f9ffd 1306
112f158f 1307 part_stat_lock();
76268f3a 1308 update_io_ticks(rq->part, jiffies, false);
320ae51f
JA
1309 part_stat_unlock();
1310}
320ae51f 1311
8446fe92 1312static unsigned long __part_start_io_acct(struct block_device *part,
7b26410b 1313 unsigned int sectors, unsigned int op)
956d510e 1314{
956d510e
CH
1315 const int sgrp = op_stat_group(op);
1316 unsigned long now = READ_ONCE(jiffies);
1317
1318 part_stat_lock();
1319 update_io_ticks(part, now, false);
1320 part_stat_inc(part, ios[sgrp]);
1321 part_stat_add(part, sectors[sgrp], sectors);
1322 part_stat_local_inc(part, in_flight[op_is_write(op)]);
1323 part_stat_unlock();
320ae51f 1324
956d510e
CH
1325 return now;
1326}
7b26410b 1327
99dfc43e
CH
1328/**
1329 * bio_start_io_acct - start I/O accounting for bio based drivers
1330 * @bio: bio to start account for
1331 *
1332 * Returns the start time that should be passed back to bio_end_io_acct().
1333 */
1334unsigned long bio_start_io_acct(struct bio *bio)
7b26410b 1335{
99dfc43e 1336 return __part_start_io_acct(bio->bi_bdev, bio_sectors(bio), bio_op(bio));
7b26410b 1337}
99dfc43e 1338EXPORT_SYMBOL_GPL(bio_start_io_acct);
7b26410b
SL
1339
1340unsigned long disk_start_io_acct(struct gendisk *disk, unsigned int sectors,
1341 unsigned int op)
1342{
8446fe92 1343 return __part_start_io_acct(disk->part0, sectors, op);
7b26410b 1344}
956d510e
CH
1345EXPORT_SYMBOL(disk_start_io_acct);
1346
8446fe92 1347static void __part_end_io_acct(struct block_device *part, unsigned int op,
7b26410b 1348 unsigned long start_time)
956d510e 1349{
956d510e
CH
1350 const int sgrp = op_stat_group(op);
1351 unsigned long now = READ_ONCE(jiffies);
1352 unsigned long duration = now - start_time;
5b18b5a7 1353
956d510e
CH
1354 part_stat_lock();
1355 update_io_ticks(part, now, true);
1356 part_stat_add(part, nsecs[sgrp], jiffies_to_nsecs(duration));
1357 part_stat_local_dec(part, in_flight[op_is_write(op)]);
320ae51f
JA
1358 part_stat_unlock();
1359}
7b26410b 1360
99dfc43e
CH
1361void bio_end_io_acct_remapped(struct bio *bio, unsigned long start_time,
1362 struct block_device *orig_bdev)
7b26410b 1363{
99dfc43e 1364 __part_end_io_acct(orig_bdev, bio_op(bio), start_time);
7b26410b 1365}
99dfc43e 1366EXPORT_SYMBOL_GPL(bio_end_io_acct_remapped);
7b26410b
SL
1367
1368void disk_end_io_acct(struct gendisk *disk, unsigned int op,
1369 unsigned long start_time)
1370{
8446fe92 1371 __part_end_io_acct(disk->part0, op, start_time);
7b26410b 1372}
956d510e 1373EXPORT_SYMBOL(disk_end_io_acct);
320ae51f 1374
ef71de8b
CH
1375/*
1376 * Steal bios from a request and add them to a bio list.
1377 * The request must not have been partially completed before.
1378 */
1379void blk_steal_bios(struct bio_list *list, struct request *rq)
1380{
1381 if (rq->bio) {
1382 if (list->tail)
1383 list->tail->bi_next = rq->bio;
1384 else
1385 list->head = rq->bio;
1386 list->tail = rq->biotail;
1387
1388 rq->bio = NULL;
1389 rq->biotail = NULL;
1390 }
1391
1392 rq->__data_len = 0;
1393}
1394EXPORT_SYMBOL_GPL(blk_steal_bios);
1395
2d4dc890
IL
1396#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
1397/**
1398 * rq_flush_dcache_pages - Helper function to flush all pages in a request
1399 * @rq: the request to be flushed
1400 *
1401 * Description:
1402 * Flush all pages in @rq.
1403 */
1404void rq_flush_dcache_pages(struct request *rq)
1405{
1406 struct req_iterator iter;
7988613b 1407 struct bio_vec bvec;
2d4dc890
IL
1408
1409 rq_for_each_segment(bvec, rq, iter)
7988613b 1410 flush_dcache_page(bvec.bv_page);
2d4dc890
IL
1411}
1412EXPORT_SYMBOL_GPL(rq_flush_dcache_pages);
1413#endif
1414
ef9e3fac
KU
1415/**
1416 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
1417 * @q : the queue of the device being checked
1418 *
1419 * Description:
1420 * Check if underlying low-level drivers of a device are busy.
1421 * If the drivers want to export their busy state, they must set own
1422 * exporting function using blk_queue_lld_busy() first.
1423 *
1424 * Basically, this function is used only by request stacking drivers
1425 * to stop dispatching requests to underlying devices when underlying
1426 * devices are busy. This behavior helps more I/O merging on the queue
1427 * of the request stacking driver and prevents I/O throughput regression
1428 * on burst I/O load.
1429 *
1430 * Return:
1431 * 0 - Not busy (The request stacking driver should dispatch request)
1432 * 1 - Busy (The request stacking driver should stop dispatching request)
1433 */
1434int blk_lld_busy(struct request_queue *q)
1435{
344e9ffc 1436 if (queue_is_mq(q) && q->mq_ops->busy)
9ba20527 1437 return q->mq_ops->busy(q);
ef9e3fac
KU
1438
1439 return 0;
1440}
1441EXPORT_SYMBOL_GPL(blk_lld_busy);
1442
78d8e58a
MS
1443/**
1444 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
1445 * @rq: the clone request to be cleaned up
1446 *
1447 * Description:
1448 * Free all bios in @rq for a cloned request.
1449 */
1450void blk_rq_unprep_clone(struct request *rq)
1451{
1452 struct bio *bio;
1453
1454 while ((bio = rq->bio) != NULL) {
1455 rq->bio = bio->bi_next;
1456
1457 bio_put(bio);
1458 }
1459}
1460EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
1461
78d8e58a
MS
1462/**
1463 * blk_rq_prep_clone - Helper function to setup clone request
1464 * @rq: the request to be setup
1465 * @rq_src: original request to be cloned
1466 * @bs: bio_set that bios for clone are allocated from
1467 * @gfp_mask: memory allocation mask for bio
1468 * @bio_ctr: setup function to be called for each clone bio.
1469 * Returns %0 for success, non %0 for failure.
1470 * @data: private data to be passed to @bio_ctr
1471 *
1472 * Description:
1473 * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
78d8e58a
MS
1474 * Also, pages which the original bios are pointing to are not copied
1475 * and the cloned bios just point same pages.
1476 * So cloned bios must be completed before original bios, which means
1477 * the caller must complete @rq before @rq_src.
1478 */
1479int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
1480 struct bio_set *bs, gfp_t gfp_mask,
1481 int (*bio_ctr)(struct bio *, struct bio *, void *),
1482 void *data)
1483{
1484 struct bio *bio, *bio_src;
1485
1486 if (!bs)
f4f8154a 1487 bs = &fs_bio_set;
78d8e58a
MS
1488
1489 __rq_for_each_bio(bio_src, rq_src) {
1490 bio = bio_clone_fast(bio_src, gfp_mask, bs);
1491 if (!bio)
1492 goto free_and_out;
1493
1494 if (bio_ctr && bio_ctr(bio, bio_src, data))
1495 goto free_and_out;
1496
1497 if (rq->bio) {
1498 rq->biotail->bi_next = bio;
1499 rq->biotail = bio;
93f221ae 1500 } else {
78d8e58a 1501 rq->bio = rq->biotail = bio;
93f221ae
EB
1502 }
1503 bio = NULL;
78d8e58a
MS
1504 }
1505
361301a2
GJ
1506 /* Copy attributes of the original request to the clone request. */
1507 rq->__sector = blk_rq_pos(rq_src);
1508 rq->__data_len = blk_rq_bytes(rq_src);
1509 if (rq_src->rq_flags & RQF_SPECIAL_PAYLOAD) {
1510 rq->rq_flags |= RQF_SPECIAL_PAYLOAD;
1511 rq->special_vec = rq_src->special_vec;
1512 }
1513 rq->nr_phys_segments = rq_src->nr_phys_segments;
1514 rq->ioprio = rq_src->ioprio;
78d8e58a 1515
93f221ae
EB
1516 if (rq->bio && blk_crypto_rq_bio_prep(rq, rq->bio, gfp_mask) < 0)
1517 goto free_and_out;
78d8e58a
MS
1518
1519 return 0;
1520
1521free_and_out:
1522 if (bio)
1523 bio_put(bio);
1524 blk_rq_unprep_clone(rq);
1525
1526 return -ENOMEM;
b0fd271d
KU
1527}
1528EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
1529
59c3d45e 1530int kblockd_schedule_work(struct work_struct *work)
1da177e4
LT
1531{
1532 return queue_work(kblockd_workqueue, work);
1533}
1da177e4
LT
1534EXPORT_SYMBOL(kblockd_schedule_work);
1535
818cd1cb
JA
1536int kblockd_mod_delayed_work_on(int cpu, struct delayed_work *dwork,
1537 unsigned long delay)
1538{
1539 return mod_delayed_work_on(cpu, kblockd_workqueue, dwork, delay);
1540}
1541EXPORT_SYMBOL(kblockd_mod_delayed_work_on);
1542
47c122e3
JA
1543void blk_start_plug_nr_ios(struct blk_plug *plug, unsigned short nr_ios)
1544{
1545 struct task_struct *tsk = current;
1546
1547 /*
1548 * If this is a nested plug, don't actually assign it.
1549 */
1550 if (tsk->plug)
1551 return;
1552
bc490f81 1553 plug->mq_list = NULL;
47c122e3
JA
1554 plug->cached_rq = NULL;
1555 plug->nr_ios = min_t(unsigned short, nr_ios, BLK_MAX_REQUEST_COUNT);
1556 plug->rq_count = 0;
1557 plug->multiple_queues = false;
dc5fc361 1558 plug->has_elevator = false;
47c122e3
JA
1559 plug->nowait = false;
1560 INIT_LIST_HEAD(&plug->cb_list);
1561
1562 /*
1563 * Store ordering should not be needed here, since a potential
1564 * preempt will imply a full memory barrier
1565 */
1566 tsk->plug = plug;
1567}
1568
75df7136
SJ
1569/**
1570 * blk_start_plug - initialize blk_plug and track it inside the task_struct
1571 * @plug: The &struct blk_plug that needs to be initialized
1572 *
1573 * Description:
40405851
JM
1574 * blk_start_plug() indicates to the block layer an intent by the caller
1575 * to submit multiple I/O requests in a batch. The block layer may use
1576 * this hint to defer submitting I/Os from the caller until blk_finish_plug()
1577 * is called. However, the block layer may choose to submit requests
1578 * before a call to blk_finish_plug() if the number of queued I/Os
1579 * exceeds %BLK_MAX_REQUEST_COUNT, or if the size of the I/O is larger than
1580 * %BLK_PLUG_FLUSH_SIZE. The queued I/Os may also be submitted early if
1581 * the task schedules (see below).
1582 *
75df7136
SJ
1583 * Tracking blk_plug inside the task_struct will help with auto-flushing the
1584 * pending I/O should the task end up blocking between blk_start_plug() and
1585 * blk_finish_plug(). This is important from a performance perspective, but
1586 * also ensures that we don't deadlock. For instance, if the task is blocking
1587 * for a memory allocation, memory reclaim could end up wanting to free a
1588 * page belonging to that request that is currently residing in our private
1589 * plug. By flushing the pending I/O when the process goes to sleep, we avoid
1590 * this kind of deadlock.
1591 */
73c10101
JA
1592void blk_start_plug(struct blk_plug *plug)
1593{
47c122e3 1594 blk_start_plug_nr_ios(plug, 1);
73c10101
JA
1595}
1596EXPORT_SYMBOL(blk_start_plug);
1597
74018dc3 1598static void flush_plug_callbacks(struct blk_plug *plug, bool from_schedule)
048c9374
N
1599{
1600 LIST_HEAD(callbacks);
1601
2a7d5559
SL
1602 while (!list_empty(&plug->cb_list)) {
1603 list_splice_init(&plug->cb_list, &callbacks);
048c9374 1604
2a7d5559
SL
1605 while (!list_empty(&callbacks)) {
1606 struct blk_plug_cb *cb = list_first_entry(&callbacks,
048c9374
N
1607 struct blk_plug_cb,
1608 list);
2a7d5559 1609 list_del(&cb->list);
74018dc3 1610 cb->callback(cb, from_schedule);
2a7d5559 1611 }
048c9374
N
1612 }
1613}
1614
9cbb1750
N
1615struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug, void *data,
1616 int size)
1617{
1618 struct blk_plug *plug = current->plug;
1619 struct blk_plug_cb *cb;
1620
1621 if (!plug)
1622 return NULL;
1623
1624 list_for_each_entry(cb, &plug->cb_list, list)
1625 if (cb->callback == unplug && cb->data == data)
1626 return cb;
1627
1628 /* Not currently on the callback list */
1629 BUG_ON(size < sizeof(*cb));
1630 cb = kzalloc(size, GFP_ATOMIC);
1631 if (cb) {
1632 cb->data = data;
1633 cb->callback = unplug;
1634 list_add(&cb->list, &plug->cb_list);
1635 }
1636 return cb;
1637}
1638EXPORT_SYMBOL(blk_check_plugged);
1639
008f75a2 1640void blk_flush_plug(struct blk_plug *plug, bool from_schedule)
73c10101 1641{
b600455d
PB
1642 if (!list_empty(&plug->cb_list))
1643 flush_plug_callbacks(plug, from_schedule);
bc490f81 1644 if (!rq_list_empty(plug->mq_list))
320ae51f 1645 blk_mq_flush_plug_list(plug, from_schedule);
47c122e3
JA
1646 if (unlikely(!from_schedule && plug->cached_rq))
1647 blk_mq_free_plug_rqs(plug);
73c10101 1648}
73c10101 1649
40405851
JM
1650/**
1651 * blk_finish_plug - mark the end of a batch of submitted I/O
1652 * @plug: The &struct blk_plug passed to blk_start_plug()
1653 *
1654 * Description:
1655 * Indicate that a batch of I/O submissions is complete. This function
1656 * must be paired with an initial call to blk_start_plug(). The intent
1657 * is to allow the block layer to optimize I/O submission. See the
1658 * documentation for blk_start_plug() for more information.
1659 */
73c10101
JA
1660void blk_finish_plug(struct blk_plug *plug)
1661{
008f75a2
CH
1662 if (plug == current->plug) {
1663 blk_flush_plug(plug, false);
1664 current->plug = NULL;
1665 }
73c10101 1666}
88b996cd 1667EXPORT_SYMBOL(blk_finish_plug);
73c10101 1668
71ac860a
ML
1669void blk_io_schedule(void)
1670{
1671 /* Prevent hang_check timer from firing at us during very long I/O */
1672 unsigned long timeout = sysctl_hung_task_timeout_secs * HZ / 2;
1673
1674 if (timeout)
1675 io_schedule_timeout(timeout);
1676 else
1677 io_schedule();
1678}
1679EXPORT_SYMBOL_GPL(blk_io_schedule);
1680
1da177e4
LT
1681int __init blk_dev_init(void)
1682{
ef295ecf
CH
1683 BUILD_BUG_ON(REQ_OP_LAST >= (1 << REQ_OP_BITS));
1684 BUILD_BUG_ON(REQ_OP_BITS + REQ_FLAG_BITS > 8 *
c593642c 1685 sizeof_field(struct request, cmd_flags));
ef295ecf 1686 BUILD_BUG_ON(REQ_OP_BITS + REQ_FLAG_BITS > 8 *
c593642c 1687 sizeof_field(struct bio, bi_opf));
9eb55b03 1688
89b90be2
TH
1689 /* used for unplugging and affects IO latency/throughput - HIGHPRI */
1690 kblockd_workqueue = alloc_workqueue("kblockd",
28747fcd 1691 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
1da177e4
LT
1692 if (!kblockd_workqueue)
1693 panic("Failed to create kblockd\n");
1694
c2789bd4 1695 blk_requestq_cachep = kmem_cache_create("request_queue",
165125e1 1696 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
1da177e4 1697
18fbda91 1698 blk_debugfs_root = debugfs_create_dir("block", NULL);
18fbda91 1699
d38ecf93 1700 return 0;
1da177e4 1701}