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