blob: f68398cb2ef6102e67dcf135eed140ec1e704f72 [file] [log] [blame]
Christoph Hellwig3dcf60b2019-04-30 14:42:43 -04001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * 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>
Jens Axboe6728cb02008-01-31 13:03:55 +01007 * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au>
8 * - July2000
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/backing-dev.h>
18#include <linux/bio.h>
19#include <linux/blkdev.h>
Jens Axboe320ae512013-10-24 09:20:05 +010020#include <linux/blk-mq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/highmem.h>
22#include <linux/mm.h>
Matthew Wilcox (Oracle)cee9a0c2020-06-01 21:46:07 -070023#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel_stat.h>
25#include <linux/string.h>
26#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/completion.h>
28#include <linux/slab.h>
29#include <linux/swap.h>
30#include <linux/writeback.h>
Andrew Mortonfaccbd4b2006-12-10 02:19:35 -080031#include <linux/task_io_accounting_ops.h>
Akinobu Mitac17bb492006-12-08 02:39:46 -080032#include <linux/fault-inject.h>
Jens Axboe73c10102011-03-08 13:19:51 +010033#include <linux/list_sort.h>
Tejun Heoe3c78ca2011-10-19 14:32:38 +020034#include <linux/delay.h>
Tejun Heoaaf7c682012-04-19 16:29:22 -070035#include <linux/ratelimit.h>
Lin Ming6c954662013-03-23 11:42:26 +080036#include <linux/pm_runtime.h>
Tejun Heoeea8f412015-05-22 17:13:17 -040037#include <linux/blk-cgroup.h>
Max Gurtovoy54d4e6a2019-09-16 18:44:29 +030038#include <linux/t10-pi.h>
Omar Sandoval18fbda92017-01-31 14:53:20 -080039#include <linux/debugfs.h>
Howard McLauchlan30abb3a2018-02-06 14:05:39 -080040#include <linux/bpf.h>
Johannes Weinerb8e24a92019-08-08 15:03:00 -040041#include <linux/psi.h>
Ming Lei71ac8602020-05-14 16:45:09 +080042#include <linux/sched/sysctl.h>
Satya Tangiralaa892c8d2020-05-14 00:37:18 +000043#include <linux/blk-crypto.h>
Li Zefan55782132009-06-09 13:43:05 +080044
45#define CREATE_TRACE_POINTS
46#include <trace/events/block.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Jens Axboe8324aa92008-01-29 14:51:59 +010048#include "blk.h"
Ming Lei43a5e4e2013-12-26 21:31:35 +080049#include "blk-mq.h"
Jens Axboebd166ef2017-01-17 06:03:22 -070050#include "blk-mq-sched.h"
Bart Van Asschebca6b062018-09-26 14:01:03 -070051#include "blk-pm.h"
Josef Bacikc1c80382018-07-03 11:14:59 -040052#include "blk-rq-qos.h"
Jens Axboe8324aa92008-01-29 14:51:59 +010053
Omar Sandoval18fbda92017-01-31 14:53:20 -080054#ifdef CONFIG_DEBUG_FS
55struct dentry *blk_debugfs_root;
56#endif
57
Mike Snitzerd07335e2010-11-16 12:52:38 +010058EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_remap);
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +020059EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_remap);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -070060EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_complete);
Keith Busch3291fa52014-04-28 12:30:52 -060061EXPORT_TRACEPOINT_SYMBOL_GPL(block_split);
NeilBrowncbae8d42012-12-14 20:49:27 +010062EXPORT_TRACEPOINT_SYMBOL_GPL(block_unplug);
Ingo Molnar0bfc2452008-11-26 11:59:56 +010063
Tejun Heoa73f7302011-12-14 00:33:37 +010064DEFINE_IDA(blk_queue_ida);
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 * For queue allocation
68 */
Jens Axboe6728cb02008-01-31 13:03:55 +010069struct kmem_cache *blk_requestq_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * Controlling structure to kblockd
73 */
Jens Axboeff856ba2006-01-09 16:02:34 +010074static struct workqueue_struct *kblockd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Bart Van Assche8814ce82018-03-07 17:10:04 -080076/**
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{
Christoph Hellwig57d74df2018-11-14 17:02:07 +010083 set_bit(flag, &q->queue_flags);
Bart Van Assche8814ce82018-03-07 17:10:04 -080084}
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{
Christoph Hellwig57d74df2018-11-14 17:02:07 +010094 clear_bit(flag, &q->queue_flags);
Bart Van Assche8814ce82018-03-07 17:10:04 -080095}
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{
Christoph Hellwig57d74df2018-11-14 17:02:07 +0100108 return test_and_set_bit(flag, &q->queue_flags);
Bart Van Assche8814ce82018-03-07 17:10:04 -0800109}
110EXPORT_SYMBOL_GPL(blk_queue_flag_test_and_set);
111
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200112void blk_rq_init(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200114 memset(rq, 0, sizeof(*rq));
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 INIT_LIST_HEAD(&rq->queuelist);
Jens Axboe63a71382008-02-08 12:41:03 +0100117 rq->q = q;
Tejun Heoa2dec7b2009-05-07 22:24:44 +0900118 rq->__sector = (sector_t) -1;
Jens Axboe2e662b62006-07-13 11:55:04 +0200119 INIT_HLIST_NODE(&rq->hash);
120 RB_CLEAR_NODE(&rq->rb_node);
Jens Axboe63a71382008-02-08 12:41:03 +0100121 rq->tag = -1;
Jens Axboebd166ef2017-01-17 06:03:22 -0700122 rq->internal_tag = -1;
Omar Sandoval522a7772018-05-09 02:08:53 -0700123 rq->start_time_ns = ktime_get_ns();
Jerome Marchand09e099d2011-01-05 16:57:38 +0100124 rq->part = NULL;
Josef Bacikb554db12019-03-07 21:37:18 +0000125 refcount_set(&rq->ref, 1);
Satya Tangiralaa892c8d2020-05-14 00:37:18 +0000126 blk_crypto_rq_set_defaults(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200128EXPORT_SYMBOL(blk_rq_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Chaitanya Kulkarnie47bc4e2019-06-20 10:59:16 -0700130#define REQ_OP_NAME(name) [REQ_OP_##name] = #name
131static const char *const blk_op_name[] = {
132 REQ_OP_NAME(READ),
133 REQ_OP_NAME(WRITE),
134 REQ_OP_NAME(FLUSH),
135 REQ_OP_NAME(DISCARD),
136 REQ_OP_NAME(SECURE_ERASE),
137 REQ_OP_NAME(ZONE_RESET),
Chaitanya Kulkarni6e33dbf2019-08-01 10:26:36 -0700138 REQ_OP_NAME(ZONE_RESET_ALL),
Ajay Joshi6c1b1da2019-10-27 23:05:45 +0900139 REQ_OP_NAME(ZONE_OPEN),
140 REQ_OP_NAME(ZONE_CLOSE),
141 REQ_OP_NAME(ZONE_FINISH),
Keith Busch0512a752020-05-12 17:55:47 +0900142 REQ_OP_NAME(ZONE_APPEND),
Chaitanya Kulkarnie47bc4e2019-06-20 10:59:16 -0700143 REQ_OP_NAME(WRITE_SAME),
144 REQ_OP_NAME(WRITE_ZEROES),
145 REQ_OP_NAME(SCSI_IN),
146 REQ_OP_NAME(SCSI_OUT),
147 REQ_OP_NAME(DRV_IN),
148 REQ_OP_NAME(DRV_OUT),
149};
150#undef REQ_OP_NAME
151
152/**
153 * blk_op_str - Return string XXX in the REQ_OP_XXX.
154 * @op: REQ_OP_XXX.
155 *
156 * Description: Centralize block layer function to convert REQ_OP_XXX into
157 * string format. Useful in the debugging and tracing bio or request. For
158 * invalid REQ_OP_XXX it returns string "UNKNOWN".
159 */
160inline const char *blk_op_str(unsigned int op)
161{
162 const char *op_str = "UNKNOWN";
163
164 if (op < ARRAY_SIZE(blk_op_name) && blk_op_name[op])
165 op_str = blk_op_name[op];
166
167 return op_str;
168}
169EXPORT_SYMBOL_GPL(blk_op_str);
170
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200171static const struct {
172 int errno;
173 const char *name;
174} blk_errors[] = {
175 [BLK_STS_OK] = { 0, "" },
176 [BLK_STS_NOTSUPP] = { -EOPNOTSUPP, "operation not supported" },
177 [BLK_STS_TIMEOUT] = { -ETIMEDOUT, "timeout" },
178 [BLK_STS_NOSPC] = { -ENOSPC, "critical space allocation" },
179 [BLK_STS_TRANSPORT] = { -ENOLINK, "recoverable transport" },
180 [BLK_STS_TARGET] = { -EREMOTEIO, "critical target" },
181 [BLK_STS_NEXUS] = { -EBADE, "critical nexus" },
182 [BLK_STS_MEDIUM] = { -ENODATA, "critical medium" },
183 [BLK_STS_PROTECTION] = { -EILSEQ, "protection" },
184 [BLK_STS_RESOURCE] = { -ENOMEM, "kernel resource" },
Ming Lei86ff7c22018-01-30 22:04:57 -0500185 [BLK_STS_DEV_RESOURCE] = { -EBUSY, "device resource" },
Goldwyn Rodrigues03a07c92017-06-20 07:05:46 -0500186 [BLK_STS_AGAIN] = { -EAGAIN, "nonblocking retry" },
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200187
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200188 /* device mapper special case, should not leak out: */
189 [BLK_STS_DM_REQUEUE] = { -EREMCHG, "dm internal retry" },
190
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200191 /* everything else not covered above: */
192 [BLK_STS_IOERR] = { -EIO, "I/O" },
193};
194
195blk_status_t errno_to_blk_status(int errno)
196{
197 int i;
198
199 for (i = 0; i < ARRAY_SIZE(blk_errors); i++) {
200 if (blk_errors[i].errno == errno)
201 return (__force blk_status_t)i;
202 }
203
204 return BLK_STS_IOERR;
205}
206EXPORT_SYMBOL_GPL(errno_to_blk_status);
207
208int blk_status_to_errno(blk_status_t status)
209{
210 int idx = (__force int)status;
211
Bart Van Assche34bd9c12017-06-21 10:55:46 -0700212 if (WARN_ON_ONCE(idx >= ARRAY_SIZE(blk_errors)))
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200213 return -EIO;
214 return blk_errors[idx].errno;
215}
216EXPORT_SYMBOL_GPL(blk_status_to_errno);
217
Christoph Hellwig178cc592019-06-20 10:59:15 -0700218static void print_req_error(struct request *req, blk_status_t status,
219 const char *caller)
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200220{
221 int idx = (__force int)status;
222
Bart Van Assche34bd9c12017-06-21 10:55:46 -0700223 if (WARN_ON_ONCE(idx >= ARRAY_SIZE(blk_errors)))
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200224 return;
225
Christoph Hellwig178cc592019-06-20 10:59:15 -0700226 printk_ratelimited(KERN_ERR
Chaitanya Kulkarnib0e51682019-06-20 10:59:18 -0700227 "%s: %s error, dev %s, sector %llu op 0x%x:(%s) flags 0x%x "
228 "phys_seg %u prio class %u\n",
Christoph Hellwig178cc592019-06-20 10:59:15 -0700229 caller, blk_errors[idx].name,
Chaitanya Kulkarnib0e51682019-06-20 10:59:18 -0700230 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));
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200235}
236
NeilBrown5bb23a62007-09-27 12:46:13 +0200237static void req_bio_endio(struct request *rq, struct bio *bio,
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200238 unsigned int nbytes, blk_status_t error)
Tejun Heo797e7db2006-01-06 09:51:03 +0100239{
Mike Snitzer78d8e582015-06-26 10:01:13 -0400240 if (error)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200241 bio->bi_status = error;
Tejun Heo797e7db2006-01-06 09:51:03 +0100242
Christoph Hellwige8064022016-10-20 15:12:13 +0200243 if (unlikely(rq->rq_flags & RQF_QUIET))
Jens Axboeb7c44ed2015-07-24 12:37:59 -0600244 bio_set_flag(bio, BIO_QUIET);
Tejun Heo143a87f2011-01-25 12:43:52 +0100245
Kent Overstreetf79ea412012-09-20 16:38:30 -0700246 bio_advance(bio, nbytes);
Tejun Heo143a87f2011-01-25 12:43:52 +0100247
Keith Busch0512a752020-05-12 17:55:47 +0900248 if (req_op(rq) == REQ_OP_ZONE_APPEND && error == BLK_STS_OK) {
249 /*
250 * Partial zone append completions cannot be supported as the
251 * BIO fragments may end up not being written sequentially.
252 */
253 if (bio->bi_iter.bi_size)
254 bio->bi_status = BLK_STS_IOERR;
255 else
256 bio->bi_iter.bi_sector = rq->__sector;
257 }
258
Tejun Heo143a87f2011-01-25 12:43:52 +0100259 /* don't actually finish bio if it's part of flush sequence */
Christoph Hellwige8064022016-10-20 15:12:13 +0200260 if (bio->bi_iter.bi_size == 0 && !(rq->rq_flags & RQF_FLUSH_SEQ))
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200261 bio_endio(bio);
Tejun Heo797e7db2006-01-06 09:51:03 +0100262}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264void blk_dump_rq_flags(struct request *rq, char *msg)
265{
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100266 printk(KERN_INFO "%s: dev %s: flags=%llx\n", msg,
267 rq->rq_disk ? rq->rq_disk->disk_name : "?",
Jens Axboe59533162013-05-23 12:25:08 +0200268 (unsigned long long) rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Tejun Heo83096eb2009-05-07 22:24:39 +0900270 printk(KERN_INFO " sector %llu, nr/cnr %u/%u\n",
271 (unsigned long long)blk_rq_pos(rq),
272 blk_rq_sectors(rq), blk_rq_cur_sectors(rq));
Jens Axboeb4f42e22014-04-10 09:46:28 -0600273 printk(KERN_INFO " bio %p, biotail %p, len %u\n",
274 rq->bio, rq->biotail, blk_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276EXPORT_SYMBOL(blk_dump_rq_flags);
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278/**
279 * blk_sync_queue - cancel any pending callbacks on a queue
280 * @q: the queue
281 *
282 * Description:
283 * The block layer may perform asynchronous callback activity
284 * on a queue, such as calling the unplug function after a timeout.
285 * A block device may call blk_sync_queue to ensure that any
286 * such activity is cancelled, thus allowing it to release resources
Michael Opdenacker59c51592007-05-09 08:57:56 +0200287 * that the callbacks might use. The caller must already have made sure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 * that its ->make_request_fn will not re-add plugging prior to calling
289 * this function.
290 *
Vivek Goyalda527772011-03-02 19:05:33 -0500291 * This function does not cancel any asynchronous activity arising
Masanari Iidada3dae52014-09-09 01:27:23 +0900292 * out of elevator or throttling code. That would require elevator_exit()
Tejun Heo5efd6112012-03-05 13:15:12 -0800293 * and blkcg_exit_queue() to be called with queue lock initialized.
Vivek Goyalda527772011-03-02 19:05:33 -0500294 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 */
296void blk_sync_queue(struct request_queue *q)
297{
Jens Axboe70ed28b2008-11-19 14:38:39 +0100298 del_timer_sync(&q->timeout);
Bart Van Assche4e9b6f22017-10-19 10:00:48 -0700299 cancel_work_sync(&q->timeout_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301EXPORT_SYMBOL(blk_sync_queue);
302
303/**
Bart Van Asschecd84a622018-09-26 14:01:04 -0700304 * blk_set_pm_only - increment pm_only counter
Bart Van Asschec9254f22017-11-09 10:49:57 -0800305 * @q: request queue pointer
Bart Van Asschec9254f22017-11-09 10:49:57 -0800306 */
Bart Van Asschecd84a622018-09-26 14:01:04 -0700307void blk_set_pm_only(struct request_queue *q)
Bart Van Asschec9254f22017-11-09 10:49:57 -0800308{
Bart Van Asschecd84a622018-09-26 14:01:04 -0700309 atomic_inc(&q->pm_only);
Bart Van Asschec9254f22017-11-09 10:49:57 -0800310}
Bart Van Asschecd84a622018-09-26 14:01:04 -0700311EXPORT_SYMBOL_GPL(blk_set_pm_only);
Bart Van Asschec9254f22017-11-09 10:49:57 -0800312
Bart Van Asschecd84a622018-09-26 14:01:04 -0700313void blk_clear_pm_only(struct request_queue *q)
Bart Van Asschec9254f22017-11-09 10:49:57 -0800314{
Bart Van Asschecd84a622018-09-26 14:01:04 -0700315 int pm_only;
316
317 pm_only = atomic_dec_return(&q->pm_only);
318 WARN_ON_ONCE(pm_only < 0);
319 if (pm_only == 0)
320 wake_up_all(&q->mq_freeze_wq);
Bart Van Asschec9254f22017-11-09 10:49:57 -0800321}
Bart Van Asschecd84a622018-09-26 14:01:04 -0700322EXPORT_SYMBOL_GPL(blk_clear_pm_only);
Bart Van Asschec9254f22017-11-09 10:49:57 -0800323
Luis Chamberlainb5bd3572020-06-19 20:47:23 +0000324/**
325 * blk_put_queue - decrement the request_queue refcount
326 * @q: the request_queue structure to decrement the refcount for
327 *
328 * Decrements the refcount of the request_queue kobject. When this reaches 0
329 * we'll have blk_release_queue() called.
330 */
Jens Axboe165125e2007-07-24 09:28:11 +0200331void blk_put_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500332{
333 kobject_put(&q->kobj);
334}
Jens Axboed86e0e82011-05-27 07:44:43 +0200335EXPORT_SYMBOL(blk_put_queue);
Al Viro483f4af2006-03-18 18:34:37 -0500336
Jens Axboeaed3ea92014-12-22 14:04:42 -0700337void blk_set_queue_dying(struct request_queue *q)
338{
Bart Van Assche8814ce82018-03-07 17:10:04 -0800339 blk_queue_flag_set(QUEUE_FLAG_DYING, q);
Jens Axboeaed3ea92014-12-22 14:04:42 -0700340
Ming Leid3cfb2a2017-03-27 20:06:58 +0800341 /*
342 * When queue DYING flag is set, we need to block new req
343 * entering queue, so we call blk_freeze_queue_start() to
344 * prevent I/O from crossing blk_queue_enter().
345 */
346 blk_freeze_queue_start(q);
347
Jens Axboe344e9ff2018-11-15 12:22:51 -0700348 if (queue_is_mq(q))
Jens Axboeaed3ea92014-12-22 14:04:42 -0700349 blk_mq_wake_waiters(q);
Ming Lei055f6e12017-11-09 10:49:53 -0800350
351 /* Make blk_queue_enter() reexamine the DYING flag. */
352 wake_up_all(&q->mq_freeze_wq);
Jens Axboeaed3ea92014-12-22 14:04:42 -0700353}
354EXPORT_SYMBOL_GPL(blk_set_queue_dying);
355
Tejun Heod7325802012-03-05 13:14:58 -0800356/**
Tejun Heoc9a929d2011-10-19 14:42:16 +0200357 * blk_cleanup_queue - shutdown a request queue
358 * @q: request queue to shutdown
359 *
Bart Van Asschec246e802012-12-06 14:32:01 +0100360 * Mark @q DYING, drain all pending requests, mark @q DEAD, destroy and
361 * put it. All future requests will be failed immediately with -ENODEV.
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500362 */
Jens Axboe6728cb02008-01-31 13:03:55 +0100363void blk_cleanup_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500364{
Bart Van Asschebae85c12019-09-30 16:00:43 -0700365 WARN_ON_ONCE(blk_queue_registered(q));
366
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100367 /* mark @q DYING, no new request or merges will be allowed afterwards */
Jens Axboeaed3ea92014-12-22 14:04:42 -0700368 blk_set_queue_dying(q);
Tejun Heo6ecf23a2012-03-05 13:14:59 -0800369
Christoph Hellwig57d74df2018-11-14 17:02:07 +0100370 blk_queue_flag_set(QUEUE_FLAG_NOMERGES, q);
371 blk_queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200372
Bart Van Asschec246e802012-12-06 14:32:01 +0100373 /*
374 * Drain all requests queued before DYING marking. Set DEAD flag to
Bart Van Assche67ed8b72019-08-01 15:39:55 -0700375 * prevent that blk_mq_run_hw_queues() accesses the hardware queues
376 * after draining finished.
Bart Van Asschec246e802012-12-06 14:32:01 +0100377 */
Dan Williams3ef28e82015-10-21 13:20:12 -0400378 blk_freeze_queue(q);
Ming Leic57cdf72018-10-24 21:18:09 +0800379
380 rq_qos_exit(q);
381
Christoph Hellwig57d74df2018-11-14 17:02:07 +0100382 blk_queue_flag_set(QUEUE_FLAG_DEAD, q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200383
Dan Williams5a48fc12015-10-21 13:20:23 -0400384 /* for synchronous bio-based driver finish in-flight integrity i/o */
385 blk_flush_integrity();
386
Tejun Heoc9a929d2011-10-19 14:42:16 +0200387 /* @q won't process any more request, flush async actions */
Jan Karadc3b17c2017-02-02 15:56:50 +0100388 del_timer_sync(&q->backing_dev_info->laptop_mode_wb_timer);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200389 blk_sync_queue(q);
390
Jens Axboe344e9ff2018-11-15 12:22:51 -0700391 if (queue_is_mq(q))
Ming Leic7e2d942019-04-30 09:52:25 +0800392 blk_mq_exit_queue(q);
Jens Axboea1ce35f2018-10-29 10:23:51 -0600393
Ming Leic3e22192019-06-04 21:08:02 +0800394 /*
395 * In theory, request pool of sched_tags belongs to request queue.
396 * However, the current implementation requires tag_set for freeing
397 * requests, so free the pool now.
398 *
399 * Queue has become frozen, there can't be any in-queue requests, so
400 * it is safe to free requests now.
401 */
402 mutex_lock(&q->sysfs_lock);
403 if (q->elevator)
404 blk_mq_sched_free_requests(q);
405 mutex_unlock(&q->sysfs_lock);
406
Dan Williams3ef28e82015-10-21 13:20:12 -0400407 percpu_ref_exit(&q->q_usage_counter);
Bart Van Assche45a9c9d2014-12-09 16:57:48 +0100408
Tejun Heoc9a929d2011-10-19 14:42:16 +0200409 /* @q is and will stay empty, shutdown and put */
Al Viro483f4af2006-03-18 18:34:37 -0500410 blk_put_queue(q);
411}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412EXPORT_SYMBOL(blk_cleanup_queue);
413
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800414/**
415 * blk_queue_enter() - try to increase q->q_usage_counter
416 * @q: request queue pointer
417 * @flags: BLK_MQ_REQ_NOWAIT and/or BLK_MQ_REQ_PREEMPT
418 */
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800419int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags)
Dan Williams3ef28e82015-10-21 13:20:12 -0400420{
Bart Van Asschecd84a622018-09-26 14:01:04 -0700421 const bool pm = flags & BLK_MQ_REQ_PREEMPT;
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800422
Dan Williams3ef28e82015-10-21 13:20:12 -0400423 while (true) {
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800424 bool success = false;
Dan Williams3ef28e82015-10-21 13:20:12 -0400425
Bart Van Assche818e0fa2018-03-19 11:46:13 -0700426 rcu_read_lock();
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800427 if (percpu_ref_tryget_live(&q->q_usage_counter)) {
428 /*
Bart Van Asschecd84a622018-09-26 14:01:04 -0700429 * The code that increments the pm_only counter is
430 * responsible for ensuring that that counter is
431 * globally visible before the queue is unfrozen.
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800432 */
Bart Van Asschecd84a622018-09-26 14:01:04 -0700433 if (pm || !blk_queue_pm_only(q)) {
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800434 success = true;
435 } else {
436 percpu_ref_put(&q->q_usage_counter);
437 }
438 }
Bart Van Assche818e0fa2018-03-19 11:46:13 -0700439 rcu_read_unlock();
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800440
441 if (success)
Dan Williams3ef28e82015-10-21 13:20:12 -0400442 return 0;
443
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800444 if (flags & BLK_MQ_REQ_NOWAIT)
Dan Williams3ef28e82015-10-21 13:20:12 -0400445 return -EBUSY;
446
Ming Lei5ed61d32017-03-27 20:06:56 +0800447 /*
Ming Lei1671d522017-03-27 20:06:57 +0800448 * read pair of barrier in blk_freeze_queue_start(),
Ming Lei5ed61d32017-03-27 20:06:56 +0800449 * we need to order reading __PERCPU_REF_DEAD flag of
Ming Leid3cfb2a2017-03-27 20:06:58 +0800450 * .q_usage_counter and reading .mq_freeze_depth or
451 * queue dying flag, otherwise the following wait may
452 * never return if the two reads are reordered.
Ming Lei5ed61d32017-03-27 20:06:56 +0800453 */
454 smp_rmb();
455
Alan Jenkins1dc30392018-04-12 19:11:58 +0100456 wait_event(q->mq_freeze_wq,
Bob Liu7996a8b2019-05-21 11:25:55 +0800457 (!q->mq_freeze_depth &&
Bart Van Assche0d25bd02018-09-26 14:01:06 -0700458 (pm || (blk_pm_request_resume(q),
459 !blk_queue_pm_only(q)))) ||
Alan Jenkins1dc30392018-04-12 19:11:58 +0100460 blk_queue_dying(q));
Dan Williams3ef28e82015-10-21 13:20:12 -0400461 if (blk_queue_dying(q))
462 return -ENODEV;
Dan Williams3ef28e82015-10-21 13:20:12 -0400463 }
464}
465
Christoph Hellwigaccea322020-04-28 13:27:56 +0200466static inline int bio_queue_enter(struct bio *bio)
467{
468 struct request_queue *q = bio->bi_disk->queue;
469 bool nowait = bio->bi_opf & REQ_NOWAIT;
470 int ret;
471
472 ret = blk_queue_enter(q, nowait ? BLK_MQ_REQ_NOWAIT : 0);
473 if (unlikely(ret)) {
474 if (nowait && !blk_queue_dying(q))
475 bio_wouldblock_error(bio);
476 else
477 bio_io_error(bio);
478 }
479
480 return ret;
481}
482
Dan Williams3ef28e82015-10-21 13:20:12 -0400483void blk_queue_exit(struct request_queue *q)
484{
485 percpu_ref_put(&q->q_usage_counter);
486}
487
488static void blk_queue_usage_counter_release(struct percpu_ref *ref)
489{
490 struct request_queue *q =
491 container_of(ref, struct request_queue, q_usage_counter);
492
493 wake_up_all(&q->mq_freeze_wq);
494}
495
Kees Cookbca237a2017-08-28 15:03:41 -0700496static void blk_rq_timed_out_timer(struct timer_list *t)
Christoph Hellwig287922e2015-10-30 20:57:30 +0800497{
Kees Cookbca237a2017-08-28 15:03:41 -0700498 struct request_queue *q = from_timer(q, t, timeout);
Christoph Hellwig287922e2015-10-30 20:57:30 +0800499
500 kblockd_schedule_work(&q->timeout_work);
501}
502
Tetsuo Handa2e3c18d2019-01-30 22:21:45 +0900503static void blk_timeout_work(struct work_struct *work)
504{
505}
506
Christoph Hellwig3d745ea2020-03-27 09:30:11 +0100507struct request_queue *__blk_alloc_queue(int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -0700508{
Jens Axboe165125e2007-07-24 09:28:11 +0200509 struct request_queue *q;
Kent Overstreet338aa962018-05-20 18:25:47 -0400510 int ret;
Christoph Lameter19460892005-06-23 00:08:19 -0700511
Jens Axboe8324aa92008-01-29 14:51:59 +0100512 q = kmem_cache_alloc_node(blk_requestq_cachep,
Christoph Hellwig3d745ea2020-03-27 09:30:11 +0100513 GFP_KERNEL | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (!q)
515 return NULL;
516
Christoph Hellwigcbf62af2018-05-31 19:11:36 +0200517 q->last_merge = NULL;
Christoph Hellwigcbf62af2018-05-31 19:11:36 +0200518
Christoph Hellwig3d745ea2020-03-27 09:30:11 +0100519 q->id = ida_simple_get(&blk_queue_ida, 0, 0, GFP_KERNEL);
Tejun Heoa73f7302011-12-14 00:33:37 +0100520 if (q->id < 0)
Ming Lei3d2936f2014-05-27 23:35:14 +0800521 goto fail_q;
Tejun Heoa73f7302011-12-14 00:33:37 +0100522
Kent Overstreet338aa962018-05-20 18:25:47 -0400523 ret = bioset_init(&q->bio_split, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
524 if (ret)
Kent Overstreet54efd502015-04-23 22:37:18 -0700525 goto fail_id;
526
Christoph Hellwigaef33c22020-05-04 14:48:00 +0200527 q->backing_dev_info = bdi_alloc(node_id);
Jan Karad03f6cd2017-02-02 15:56:51 +0100528 if (!q->backing_dev_info)
Kent Overstreet54efd502015-04-23 22:37:18 -0700529 goto fail_split;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700530
Jens Axboea83b5762017-03-21 17:20:01 -0600531 q->stats = blk_alloc_queue_stats();
532 if (!q->stats)
533 goto fail_stats;
534
Nikolay Borisovb5420232019-03-11 23:28:13 -0700535 q->backing_dev_info->ra_pages = VM_READAHEAD_PAGES;
Jan Karadc3b17c2017-02-02 15:56:50 +0100536 q->backing_dev_info->capabilities = BDI_CAP_CGROUP_WRITEBACK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 q->node = node_id;
538
Kees Cookbca237a2017-08-28 15:03:41 -0700539 timer_setup(&q->backing_dev_info->laptop_mode_wb_timer,
540 laptop_mode_timer_fn, 0);
541 timer_setup(&q->timeout, blk_rq_timed_out_timer, 0);
Tetsuo Handa2e3c18d2019-01-30 22:21:45 +0900542 INIT_WORK(&q->timeout_work, blk_timeout_work);
Tejun Heoa612fdd2011-12-14 00:33:41 +0100543 INIT_LIST_HEAD(&q->icq_list);
Tejun Heo4eef3042012-03-05 13:15:18 -0800544#ifdef CONFIG_BLK_CGROUP
Tejun Heoe8989fa2012-03-05 13:15:20 -0800545 INIT_LIST_HEAD(&q->blkg_list);
Tejun Heo4eef3042012-03-05 13:15:18 -0800546#endif
Al Viro483f4af2006-03-18 18:34:37 -0500547
Jens Axboe8324aa92008-01-29 14:51:59 +0100548 kobject_init(&q->kobj, &blk_queue_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Waiman Long5acb3cc2017-09-20 13:12:20 -0600550#ifdef CONFIG_BLK_DEV_IO_TRACE
551 mutex_init(&q->blk_trace_mutex);
552#endif
Al Viro483f4af2006-03-18 18:34:37 -0500553 mutex_init(&q->sysfs_lock);
Ming Leicecf5d82019-08-27 19:01:48 +0800554 mutex_init(&q->sysfs_dir_lock);
Christoph Hellwig0d945c12018-11-15 12:17:28 -0700555 spin_lock_init(&q->queue_lock);
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500556
Jens Axboe320ae512013-10-24 09:20:05 +0100557 init_waitqueue_head(&q->mq_freeze_wq);
Bob Liu7996a8b2019-05-21 11:25:55 +0800558 mutex_init(&q->mq_freeze_lock);
Jens Axboe320ae512013-10-24 09:20:05 +0100559
Dan Williams3ef28e82015-10-21 13:20:12 -0400560 /*
561 * Init percpu_ref in atomic mode so that it's faster to shutdown.
562 * See blk_register_queue() for details.
563 */
564 if (percpu_ref_init(&q->q_usage_counter,
565 blk_queue_usage_counter_release,
566 PERCPU_REF_INIT_ATOMIC, GFP_KERNEL))
Mikulas Patockafff49962013-10-14 12:11:36 -0400567 goto fail_bdi;
Tejun Heof51b8022012-03-05 13:15:05 -0800568
Dan Williams3ef28e82015-10-21 13:20:12 -0400569 if (blkcg_init_queue(q))
570 goto fail_ref;
571
Christoph Hellwig3d745ea2020-03-27 09:30:11 +0100572 blk_queue_dma_alignment(q, 511);
573 blk_set_default_limits(&q->limits);
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return q;
Tejun Heoa73f7302011-12-14 00:33:37 +0100576
Dan Williams3ef28e82015-10-21 13:20:12 -0400577fail_ref:
578 percpu_ref_exit(&q->q_usage_counter);
Mikulas Patockafff49962013-10-14 12:11:36 -0400579fail_bdi:
Jens Axboea83b5762017-03-21 17:20:01 -0600580 blk_free_queue_stats(q->stats);
581fail_stats:
Jan Karad03f6cd2017-02-02 15:56:51 +0100582 bdi_put(q->backing_dev_info);
Kent Overstreet54efd502015-04-23 22:37:18 -0700583fail_split:
Kent Overstreet338aa962018-05-20 18:25:47 -0400584 bioset_exit(&q->bio_split);
Tejun Heoa73f7302011-12-14 00:33:37 +0100585fail_id:
586 ida_simple_remove(&blk_queue_ida, q->id);
587fail_q:
588 kmem_cache_free(blk_requestq_cachep, q);
589 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
Christoph Hellwig3d745ea2020-03-27 09:30:11 +0100591
592struct request_queue *blk_alloc_queue(make_request_fn make_request, int node_id)
593{
594 struct request_queue *q;
595
596 if (WARN_ON_ONCE(!make_request))
Chaitanya Kulkarni654a3662020-03-29 10:08:26 -0600597 return NULL;
Christoph Hellwig3d745ea2020-03-27 09:30:11 +0100598
599 q = __blk_alloc_queue(node_id);
600 if (!q)
601 return NULL;
602 q->make_request_fn = make_request;
603 q->nr_requests = BLKDEV_MAX_RQ;
604 return q;
605}
606EXPORT_SYMBOL(blk_alloc_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Luis Chamberlainb5bd3572020-06-19 20:47:23 +0000608/**
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.
Luis Chamberlain763b5892020-06-19 20:47:24 +0000613 *
614 * Context: Any context.
Luis Chamberlainb5bd3572020-06-19 20:47:23 +0000615 */
Tejun Heo09ac46c2011-12-14 00:33:38 +0100616bool blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100618 if (likely(!blk_queue_dying(q))) {
Tejun Heo09ac46c2011-12-14 00:33:38 +0100619 __blk_get_queue(q);
620 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
622
Tejun Heo09ac46c2011-12-14 00:33:38 +0100623 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
Jens Axboed86e0e82011-05-27 07:44:43 +0200625EXPORT_SYMBOL(blk_get_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Bart Van Assche6a156742017-11-09 10:49:54 -0800627/**
Christoph Hellwigff005a02018-05-09 09:54:05 +0200628 * blk_get_request - allocate a request
Bart Van Assche6a156742017-11-09 10:49:54 -0800629 * @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.
632 */
Christoph Hellwigff005a02018-05-09 09:54:05 +0200633struct request *blk_get_request(struct request_queue *q, unsigned int op,
634 blk_mq_req_flags_t flags)
Jens Axboe320ae512013-10-24 09:20:05 +0100635{
Bart Van Assched280bab2017-06-20 11:15:40 -0700636 struct request *req;
637
Bart Van Assche6a156742017-11-09 10:49:54 -0800638 WARN_ON_ONCE(op & REQ_NOWAIT);
Bart Van Assche1b6d65a2017-11-09 10:49:55 -0800639 WARN_ON_ONCE(flags & ~(BLK_MQ_REQ_NOWAIT | BLK_MQ_REQ_PREEMPT));
Bart Van Assche6a156742017-11-09 10:49:54 -0800640
Jens Axboea1ce35f2018-10-29 10:23:51 -0600641 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);
Bart Van Assched280bab2017-06-20 11:15:40 -0700644
645 return req;
Jens Axboe320ae512013-10-24 09:20:05 +0100646}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647EXPORT_SYMBOL(blk_get_request);
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649void blk_put_request(struct request *req)
650{
Jens Axboea1ce35f2018-10-29 10:23:51 -0600651 blk_mq_free_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653EXPORT_SYMBOL(blk_put_request);
654
Konstantin Khlebnikovb5af37a2020-05-27 07:24:16 +0200655static void blk_account_io_merge_bio(struct request *req)
656{
657 if (!blk_do_io_stat(req))
658 return;
659
660 part_stat_lock();
661 part_stat_inc(req->part, merges[op_stat_group(req_op(req))]);
662 part_stat_unlock();
663}
664
Christoph Hellwig14ccb662019-06-06 12:29:01 +0200665bool bio_attempt_back_merge(struct request *req, struct bio *bio,
666 unsigned int nr_segs)
Jens Axboe73c10102011-03-08 13:19:51 +0100667{
Jens Axboe1eff9d32016-08-05 15:35:16 -0600668 const int ff = bio->bi_opf & REQ_FAILFAST_MASK;
Jens Axboe73c10102011-03-08 13:19:51 +0100669
Christoph Hellwig14ccb662019-06-06 12:29:01 +0200670 if (!ll_back_merge_fn(req, bio, nr_segs))
Jens Axboe73c10102011-03-08 13:19:51 +0100671 return false;
672
Christoph Hellwig14ccb662019-06-06 12:29:01 +0200673 trace_block_bio_backmerge(req->q, req, bio);
Tejun Heod3e65ff2019-08-28 15:05:54 -0700674 rq_qos_merge(req->q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +0100675
676 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
677 blk_rq_set_mixed_merge(req);
678
679 req->biotail->bi_next = bio;
680 req->biotail = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700681 req->__data_len += bio->bi_iter.bi_size;
Jens Axboe73c10102011-03-08 13:19:51 +0100682
Satya Tangiralaa892c8d2020-05-14 00:37:18 +0000683 bio_crypt_free_ctx(bio);
684
Konstantin Khlebnikovb5af37a2020-05-27 07:24:16 +0200685 blk_account_io_merge_bio(req);
Jens Axboe73c10102011-03-08 13:19:51 +0100686 return true;
687}
688
Christoph Hellwig14ccb662019-06-06 12:29:01 +0200689bool bio_attempt_front_merge(struct request *req, struct bio *bio,
690 unsigned int nr_segs)
Jens Axboe73c10102011-03-08 13:19:51 +0100691{
Jens Axboe1eff9d32016-08-05 15:35:16 -0600692 const int ff = bio->bi_opf & REQ_FAILFAST_MASK;
Jens Axboe73c10102011-03-08 13:19:51 +0100693
Christoph Hellwig14ccb662019-06-06 12:29:01 +0200694 if (!ll_front_merge_fn(req, bio, nr_segs))
Jens Axboe73c10102011-03-08 13:19:51 +0100695 return false;
696
Christoph Hellwig14ccb662019-06-06 12:29:01 +0200697 trace_block_bio_frontmerge(req->q, req, bio);
Tejun Heod3e65ff2019-08-28 15:05:54 -0700698 rq_qos_merge(req->q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +0100699
700 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
701 blk_rq_set_mixed_merge(req);
702
Jens Axboe73c10102011-03-08 13:19:51 +0100703 bio->bi_next = req->bio;
704 req->bio = bio;
705
Kent Overstreet4f024f32013-10-11 15:44:27 -0700706 req->__sector = bio->bi_iter.bi_sector;
707 req->__data_len += bio->bi_iter.bi_size;
Jens Axboe73c10102011-03-08 13:19:51 +0100708
Satya Tangiralaa892c8d2020-05-14 00:37:18 +0000709 bio_crypt_do_front_merge(req, bio);
710
Konstantin Khlebnikovb5af37a2020-05-27 07:24:16 +0200711 blk_account_io_merge_bio(req);
Jens Axboe73c10102011-03-08 13:19:51 +0100712 return true;
713}
714
Christoph Hellwig1e739732017-02-08 14:46:49 +0100715bool bio_attempt_discard_merge(struct request_queue *q, struct request *req,
716 struct bio *bio)
717{
718 unsigned short segments = blk_rq_nr_discard_segments(req);
719
720 if (segments >= queue_max_discard_segments(q))
721 goto no_merge;
722 if (blk_rq_sectors(req) + bio_sectors(bio) >
723 blk_rq_get_max_sectors(req, blk_rq_pos(req)))
724 goto no_merge;
725
Tejun Heod3e65ff2019-08-28 15:05:54 -0700726 rq_qos_merge(q, req, bio);
727
Christoph Hellwig1e739732017-02-08 14:46:49 +0100728 req->biotail->bi_next = bio;
729 req->biotail = bio;
730 req->__data_len += bio->bi_iter.bi_size;
Christoph Hellwig1e739732017-02-08 14:46:49 +0100731 req->nr_phys_segments = segments + 1;
732
Konstantin Khlebnikovb5af37a2020-05-27 07:24:16 +0200733 blk_account_io_merge_bio(req);
Christoph Hellwig1e739732017-02-08 14:46:49 +0100734 return true;
735no_merge:
736 req_set_nomerge(q, req);
737 return false;
738}
739
Tejun Heobd87b582011-10-19 14:33:08 +0200740/**
Jens Axboe320ae512013-10-24 09:20:05 +0100741 * blk_attempt_plug_merge - try to merge with %current's plugged list
Tejun Heobd87b582011-10-19 14:33:08 +0200742 * @q: request_queue new bio is being queued at
743 * @bio: new bio being queued
Christoph Hellwig14ccb662019-06-06 12:29:01 +0200744 * @nr_segs: number of segments in @bio
Randy Dunlapccc26002015-10-30 18:36:16 -0700745 * @same_queue_rq: pointer to &struct request that gets filled in when
746 * another request associated with @q is found on the plug list
747 * (optional, may be %NULL)
Tejun Heobd87b582011-10-19 14:33:08 +0200748 *
749 * Determine whether @bio being queued on @q can be merged with a request
750 * on %current's plugged list. Returns %true if merge was successful,
751 * otherwise %false.
752 *
Tejun Heo07c2bd32012-02-08 09:19:42 +0100753 * Plugging coalesces IOs from the same issuer for the same purpose without
754 * going through @q->queue_lock. As such it's more of an issuing mechanism
755 * than scheduling, and the request, while may have elvpriv data, is not
756 * added on the elevator at this point. In addition, we don't have
757 * reliable access to the elevator outside queue lock. Only check basic
758 * merging parameters without querying the elevator.
Robert Elliottda41a582014-05-20 16:46:26 -0500759 *
760 * Caller must ensure !blk_queue_nomerges(q) beforehand.
Jens Axboe73c10102011-03-08 13:19:51 +0100761 */
Jens Axboe320ae512013-10-24 09:20:05 +0100762bool blk_attempt_plug_merge(struct request_queue *q, struct bio *bio,
Christoph Hellwig14ccb662019-06-06 12:29:01 +0200763 unsigned int nr_segs, struct request **same_queue_rq)
Jens Axboe73c10102011-03-08 13:19:51 +0100764{
765 struct blk_plug *plug;
766 struct request *rq;
Shaohua Li92f399c2013-10-29 12:01:03 -0600767 struct list_head *plug_list;
Jens Axboe73c10102011-03-08 13:19:51 +0100768
Damien Le Moalb49773e72019-07-11 01:18:31 +0900769 plug = blk_mq_plug(q, bio);
Jens Axboe73c10102011-03-08 13:19:51 +0100770 if (!plug)
Christoph Hellwig34fe7c02017-02-08 14:46:48 +0100771 return false;
Jens Axboe73c10102011-03-08 13:19:51 +0100772
Jens Axboea1ce35f2018-10-29 10:23:51 -0600773 plug_list = &plug->mq_list;
Shaohua Li92f399c2013-10-29 12:01:03 -0600774
775 list_for_each_entry_reverse(rq, plug_list, queuelist) {
Christoph Hellwig34fe7c02017-02-08 14:46:48 +0100776 bool merged = false;
Jens Axboe73c10102011-03-08 13:19:51 +0100777
Jens Axboe5f0ed772018-11-23 22:04:33 -0700778 if (rq->q == q && same_queue_rq) {
Shaohua Li5b3f3412015-05-08 10:51:33 -0700779 /*
780 * Only blk-mq multiple hardware queues case checks the
781 * rq in the same queue, there should be only one such
782 * rq in a queue
783 **/
Jens Axboe5f0ed772018-11-23 22:04:33 -0700784 *same_queue_rq = rq;
Shaohua Li5b3f3412015-05-08 10:51:33 -0700785 }
Shaohua Li56ebdaf2011-08-24 16:04:34 +0200786
Tejun Heo07c2bd32012-02-08 09:19:42 +0100787 if (rq->q != q || !blk_rq_merge_ok(rq, bio))
Jens Axboe73c10102011-03-08 13:19:51 +0100788 continue;
789
Christoph Hellwig34fe7c02017-02-08 14:46:48 +0100790 switch (blk_try_merge(rq, bio)) {
791 case ELEVATOR_BACK_MERGE:
Christoph Hellwig14ccb662019-06-06 12:29:01 +0200792 merged = bio_attempt_back_merge(rq, bio, nr_segs);
Christoph Hellwig34fe7c02017-02-08 14:46:48 +0100793 break;
794 case ELEVATOR_FRONT_MERGE:
Christoph Hellwig14ccb662019-06-06 12:29:01 +0200795 merged = bio_attempt_front_merge(rq, bio, nr_segs);
Christoph Hellwig34fe7c02017-02-08 14:46:48 +0100796 break;
Christoph Hellwig1e739732017-02-08 14:46:49 +0100797 case ELEVATOR_DISCARD_MERGE:
798 merged = bio_attempt_discard_merge(q, rq, bio);
799 break;
Christoph Hellwig34fe7c02017-02-08 14:46:48 +0100800 default:
801 break;
Jens Axboe73c10102011-03-08 13:19:51 +0100802 }
Christoph Hellwig34fe7c02017-02-08 14:46:48 +0100803
804 if (merged)
805 return true;
Jens Axboe73c10102011-03-08 13:19:51 +0100806 }
Christoph Hellwig34fe7c02017-02-08 14:46:48 +0100807
808 return false;
Jens Axboe73c10102011-03-08 13:19:51 +0100809}
810
Christoph Hellwig52c5e622018-03-14 16:56:53 +0100811static void handle_bad_sector(struct bio *bio, sector_t maxsector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
813 char b[BDEVNAME_SIZE];
814
815 printk(KERN_INFO "attempt to access beyond end of device\n");
Mike Christie6296b962016-06-05 14:32:21 -0500816 printk(KERN_INFO "%s: rw=%d, want=%Lu, limit=%Lu\n",
Christoph Hellwig74d46992017-08-23 19:10:32 +0200817 bio_devname(bio, b), bio->bi_opf,
Kent Overstreetf73a1c72012-09-25 15:05:12 -0700818 (unsigned long long)bio_end_sector(bio),
Christoph Hellwig52c5e622018-03-14 16:56:53 +0100819 (long long)maxsector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
821
Akinobu Mitac17bb492006-12-08 02:39:46 -0800822#ifdef CONFIG_FAIL_MAKE_REQUEST
823
824static DECLARE_FAULT_ATTR(fail_make_request);
825
826static int __init setup_fail_make_request(char *str)
827{
828 return setup_fault_attr(&fail_make_request, str);
829}
830__setup("fail_make_request=", setup_fail_make_request);
831
Akinobu Mitab2c9cd32011-07-26 16:09:03 -0700832static bool should_fail_request(struct hd_struct *part, unsigned int bytes)
Akinobu Mitac17bb492006-12-08 02:39:46 -0800833{
Akinobu Mitab2c9cd32011-07-26 16:09:03 -0700834 return part->make_it_fail && should_fail(&fail_make_request, bytes);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800835}
836
837static int __init fail_make_request_debugfs(void)
838{
Akinobu Mitadd48c082011-08-03 16:21:01 -0700839 struct dentry *dir = fault_create_debugfs_attr("fail_make_request",
840 NULL, &fail_make_request);
841
Duan Jiong21f9fcd2014-04-11 15:58:56 +0800842 return PTR_ERR_OR_ZERO(dir);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800843}
844
845late_initcall(fail_make_request_debugfs);
846
847#else /* CONFIG_FAIL_MAKE_REQUEST */
848
Akinobu Mitab2c9cd32011-07-26 16:09:03 -0700849static inline bool should_fail_request(struct hd_struct *part,
850 unsigned int bytes)
Akinobu Mitac17bb492006-12-08 02:39:46 -0800851{
Akinobu Mitab2c9cd32011-07-26 16:09:03 -0700852 return false;
Akinobu Mitac17bb492006-12-08 02:39:46 -0800853}
854
855#endif /* CONFIG_FAIL_MAKE_REQUEST */
856
Ilya Dryomov721c7fc2018-01-11 14:09:11 +0100857static inline bool bio_check_ro(struct bio *bio, struct hd_struct *part)
858{
Jens Axboeb089cfd2018-08-14 10:52:40 -0600859 const int op = bio_op(bio);
860
Mikulas Patocka8b2ded12018-09-05 16:14:36 -0600861 if (part->policy && op_is_write(op)) {
Ilya Dryomov721c7fc2018-01-11 14:09:11 +0100862 char b[BDEVNAME_SIZE];
863
Mikulas Patocka8b2ded12018-09-05 16:14:36 -0600864 if (op_is_flush(bio->bi_opf) && !bio_sectors(bio))
865 return false;
866
Linus Torvaldsa32e2362018-08-03 12:22:09 -0700867 WARN_ONCE(1,
Ilya Dryomov721c7fc2018-01-11 14:09:11 +0100868 "generic_make_request: Trying to write "
869 "to read-only block-device %s (partno %d)\n",
870 bio_devname(bio, b), part->partno);
Linus Torvaldsa32e2362018-08-03 12:22:09 -0700871 /* Older lvm-tools actually trigger this */
872 return false;
Ilya Dryomov721c7fc2018-01-11 14:09:11 +0100873 }
874
875 return false;
876}
877
Howard McLauchlan30abb3a2018-02-06 14:05:39 -0800878static noinline int should_fail_bio(struct bio *bio)
879{
880 if (should_fail_request(&bio->bi_disk->part0, bio->bi_iter.bi_size))
881 return -EIO;
882 return 0;
883}
884ALLOW_ERROR_INJECTION(should_fail_bio, ERRNO);
885
Jens Axboec07e2b42007-07-18 13:27:58 +0200886/*
Christoph Hellwig52c5e622018-03-14 16:56:53 +0100887 * Check whether this bio extends beyond the end of the device or partition.
888 * This may well happen - the kernel calls bread() without checking the size of
889 * the device, e.g., when mounting a file system.
890 */
891static inline int bio_check_eod(struct bio *bio, sector_t maxsector)
892{
893 unsigned int nr_sectors = bio_sectors(bio);
894
895 if (nr_sectors && maxsector &&
896 (nr_sectors > maxsector ||
897 bio->bi_iter.bi_sector > maxsector - nr_sectors)) {
898 handle_bad_sector(bio, maxsector);
899 return -EIO;
900 }
901 return 0;
902}
903
904/*
Christoph Hellwig74d46992017-08-23 19:10:32 +0200905 * Remap block n of partition p to block n+start(p) of the disk.
906 */
907static inline int blk_partition_remap(struct bio *bio)
908{
909 struct hd_struct *p;
Christoph Hellwig52c5e622018-03-14 16:56:53 +0100910 int ret = -EIO;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200911
Ilya Dryomov721c7fc2018-01-11 14:09:11 +0100912 rcu_read_lock();
913 p = __disk_get_part(bio->bi_disk, bio->bi_partno);
Christoph Hellwig52c5e622018-03-14 16:56:53 +0100914 if (unlikely(!p))
Ilya Dryomov721c7fc2018-01-11 14:09:11 +0100915 goto out;
Christoph Hellwig52c5e622018-03-14 16:56:53 +0100916 if (unlikely(should_fail_request(p, bio->bi_iter.bi_size)))
917 goto out;
918 if (unlikely(bio_check_ro(bio, p)))
919 goto out;
Ilya Dryomov721c7fc2018-01-11 14:09:11 +0100920
Damien Le Moal5eac3eb2019-11-11 11:39:25 +0900921 if (bio_sectors(bio)) {
Christoph Hellwig52c5e622018-03-14 16:56:53 +0100922 if (bio_check_eod(bio, part_nr_sects_read(p)))
923 goto out;
924 bio->bi_iter.bi_sector += p->start_sect;
Christoph Hellwig52c5e622018-03-14 16:56:53 +0100925 trace_block_bio_remap(bio->bi_disk->queue, bio, part_devt(p),
926 bio->bi_iter.bi_sector - p->start_sect);
927 }
Hannes Reineckec04fa442018-06-07 10:29:44 +0200928 bio->bi_partno = 0;
Christoph Hellwig52c5e622018-03-14 16:56:53 +0100929 ret = 0;
Ilya Dryomov721c7fc2018-01-11 14:09:11 +0100930out:
Christoph Hellwig74d46992017-08-23 19:10:32 +0200931 rcu_read_unlock();
Christoph Hellwig74d46992017-08-23 19:10:32 +0200932 return ret;
933}
934
Keith Busch0512a752020-05-12 17:55:47 +0900935/*
936 * Check write append to a zoned block device.
937 */
938static inline blk_status_t blk_check_zone_append(struct request_queue *q,
939 struct bio *bio)
940{
941 sector_t pos = bio->bi_iter.bi_sector;
942 int nr_sectors = bio_sectors(bio);
943
944 /* Only applicable to zoned block devices */
945 if (!blk_queue_is_zoned(q))
946 return BLK_STS_NOTSUPP;
947
948 /* The bio sector must point to the start of a sequential zone */
949 if (pos & (blk_queue_zone_sectors(q) - 1) ||
950 !blk_queue_zone_is_seq(q, pos))
951 return BLK_STS_IOERR;
952
953 /*
954 * Not allowed to cross zone boundaries. Otherwise, the BIO will be
955 * split and could result in non-contiguous sectors being written in
956 * different zones.
957 */
958 if (nr_sectors > q->limits.chunk_sectors)
959 return BLK_STS_IOERR;
960
961 /* Make sure the BIO is small enough and will not get split */
962 if (nr_sectors > q->limits.max_zone_append_sectors)
963 return BLK_STS_IOERR;
964
965 bio->bi_opf |= REQ_NOMERGE;
966
967 return BLK_STS_OK;
968}
969
Christoph Hellwig27a84d52011-09-15 14:01:40 +0200970static noinline_for_stack bool
971generic_make_request_checks(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Jens Axboe165125e2007-07-24 09:28:11 +0200973 struct request_queue *q;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200974 int nr_sectors = bio_sectors(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200975 blk_status_t status = BLK_STS_IOERR;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200976 char b[BDEVNAME_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Christoph Hellwig74d46992017-08-23 19:10:32 +0200980 q = bio->bi_disk->queue;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200981 if (unlikely(!q)) {
982 printk(KERN_ERR
983 "generic_make_request: Trying to access "
984 "nonexistent block-device %s (%Lu)\n",
Christoph Hellwig74d46992017-08-23 19:10:32 +0200985 bio_devname(bio, b), (long long)bio->bi_iter.bi_sector);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200986 goto end_io;
987 }
988
Goldwyn Rodrigues03a07c92017-06-20 07:05:46 -0500989 /*
Jens Axboeb0beb282020-05-28 13:19:29 -0600990 * For a REQ_NOWAIT based request, return -EOPNOTSUPP
991 * if queue is not a request based queue.
Goldwyn Rodrigues03a07c92017-06-20 07:05:46 -0500992 */
Jens Axboeb0beb282020-05-28 13:19:29 -0600993 if ((bio->bi_opf & REQ_NOWAIT) && !queue_is_mq(q))
994 goto not_supported;
Goldwyn Rodrigues03a07c92017-06-20 07:05:46 -0500995
Howard McLauchlan30abb3a2018-02-06 14:05:39 -0800996 if (should_fail_bio(bio))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200997 goto end_io;
998
Christoph Hellwig52c5e622018-03-14 16:56:53 +0100999 if (bio->bi_partno) {
1000 if (unlikely(blk_partition_remap(bio)))
Ilya Dryomov721c7fc2018-01-11 14:09:11 +01001001 goto end_io;
1002 } else {
Christoph Hellwig52c5e622018-03-14 16:56:53 +01001003 if (unlikely(bio_check_ro(bio, &bio->bi_disk->part0)))
1004 goto end_io;
1005 if (unlikely(bio_check_eod(bio, get_capacity(bio->bi_disk))))
Ilya Dryomov721c7fc2018-01-11 14:09:11 +01001006 goto end_io;
1007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001009 /*
1010 * Filter flush bio's early so that make_request based
1011 * drivers without flush support don't have to worry
1012 * about them.
1013 */
Jens Axboef3a8ab72017-01-27 09:08:23 -07001014 if (op_is_flush(bio->bi_opf) &&
Jens Axboec888a8f2016-04-13 13:33:19 -06001015 !test_bit(QUEUE_FLAG_WC, &q->queue_flags)) {
Jens Axboe1eff9d32016-08-05 15:35:16 -06001016 bio->bi_opf &= ~(REQ_PREFLUSH | REQ_FUA);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001017 if (!nr_sectors) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001018 status = BLK_STS_OK;
Tejun Heoa7384672008-11-28 13:32:03 +09001019 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Christoph Hellwigd04c4062018-12-14 17:21:22 +01001023 if (!test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
1024 bio->bi_opf &= ~REQ_HIPRI;
1025
Christoph Hellwig288dab82016-06-09 16:00:36 +02001026 switch (bio_op(bio)) {
1027 case REQ_OP_DISCARD:
1028 if (!blk_queue_discard(q))
1029 goto not_supported;
1030 break;
1031 case REQ_OP_SECURE_ERASE:
1032 if (!blk_queue_secure_erase(q))
1033 goto not_supported;
1034 break;
1035 case REQ_OP_WRITE_SAME:
Christoph Hellwig74d46992017-08-23 19:10:32 +02001036 if (!q->limits.max_write_same_sectors)
Christoph Hellwig288dab82016-06-09 16:00:36 +02001037 goto not_supported;
Nicolai Stange58886782016-12-04 14:56:39 +01001038 break;
Keith Busch0512a752020-05-12 17:55:47 +09001039 case REQ_OP_ZONE_APPEND:
1040 status = blk_check_zone_append(q, bio);
1041 if (status != BLK_STS_OK)
1042 goto end_io;
1043 break;
Shaun Tancheff2d253442016-10-18 15:40:32 +09001044 case REQ_OP_ZONE_RESET:
Ajay Joshi6c1b1da2019-10-27 23:05:45 +09001045 case REQ_OP_ZONE_OPEN:
1046 case REQ_OP_ZONE_CLOSE:
1047 case REQ_OP_ZONE_FINISH:
Christoph Hellwig74d46992017-08-23 19:10:32 +02001048 if (!blk_queue_is_zoned(q))
Shaun Tancheff2d253442016-10-18 15:40:32 +09001049 goto not_supported;
Christoph Hellwig288dab82016-06-09 16:00:36 +02001050 break;
Chaitanya Kulkarni6e33dbf2019-08-01 10:26:36 -07001051 case REQ_OP_ZONE_RESET_ALL:
1052 if (!blk_queue_is_zoned(q) || !blk_queue_zone_resetall(q))
1053 goto not_supported;
1054 break;
Chaitanya Kulkarnia6f07882016-11-30 12:28:59 -08001055 case REQ_OP_WRITE_ZEROES:
Christoph Hellwig74d46992017-08-23 19:10:32 +02001056 if (!q->limits.max_write_zeroes_sectors)
Chaitanya Kulkarnia6f07882016-11-30 12:28:59 -08001057 goto not_supported;
1058 break;
Christoph Hellwig288dab82016-06-09 16:00:36 +02001059 default:
1060 break;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Tejun Heo7f4b35d2012-06-04 20:40:56 -07001063 /*
Christoph Hellwig3e82c342020-04-25 09:55:51 +02001064 * Various block parts want %current->io_context, so allocate it up
1065 * front rather than dealing with lots of pain to allocate it only
1066 * where needed. This may fail and the block layer knows how to live
1067 * with it.
Tejun Heo7f4b35d2012-06-04 20:40:56 -07001068 */
Christoph Hellwig3e82c342020-04-25 09:55:51 +02001069 if (unlikely(!current->io_context))
1070 create_task_io_context(current, GFP_ATOMIC, q->node);
Tejun Heo7f4b35d2012-06-04 20:40:56 -07001071
Tejun Heoae118892015-08-18 14:55:20 -07001072 if (!blkcg_bio_issue_check(q, bio))
1073 return false;
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001074
NeilBrownfbbaf702017-04-07 09:40:52 -06001075 if (!bio_flagged(bio, BIO_TRACE_COMPLETION)) {
1076 trace_block_bio_queue(q, bio);
1077 /* Now that enqueuing has been traced, we need to trace
1078 * completion as well.
1079 */
1080 bio_set_flag(bio, BIO_TRACE_COMPLETION);
1081 }
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001082 return true;
Tejun Heoa7384672008-11-28 13:32:03 +09001083
Christoph Hellwig288dab82016-06-09 16:00:36 +02001084not_supported:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001085 status = BLK_STS_NOTSUPP;
Tejun Heoa7384672008-11-28 13:32:03 +09001086end_io:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001087 bio->bi_status = status;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001088 bio_endio(bio);
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001089 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
1091
Christoph Hellwigac7c5672020-05-16 20:28:01 +02001092static blk_qc_t do_make_request(struct bio *bio)
1093{
1094 struct request_queue *q = bio->bi_disk->queue;
1095 blk_qc_t ret = BLK_QC_T_NONE;
1096
1097 if (blk_crypto_bio_prep(&bio)) {
1098 if (!q->make_request_fn)
1099 return blk_mq_make_request(q, bio);
1100 ret = q->make_request_fn(q, bio);
1101 }
1102 blk_queue_exit(q);
1103 return ret;
1104}
1105
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001106/**
Christoph Hellwig3fdd4082020-04-28 13:27:53 +02001107 * generic_make_request - re-submit a bio to the block device layer for I/O
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001108 * @bio: The bio describing the location in memory and on the device.
1109 *
Christoph Hellwig3fdd4082020-04-28 13:27:53 +02001110 * This is a version of submit_bio() that shall only be used for I/O that is
1111 * resubmitted to lower level drivers by stacking block drivers. All file
1112 * systems and other upper level users of the block layer should use
1113 * submit_bio() instead.
Neil Brownd89d8792007-05-01 09:53:42 +02001114 */
Jens Axboedece1632015-11-05 10:41:16 -07001115blk_qc_t generic_make_request(struct bio *bio)
Neil Brownd89d8792007-05-01 09:53:42 +02001116{
NeilBrownf5fe1b52017-03-10 17:00:47 +11001117 /*
1118 * bio_list_on_stack[0] contains bios submitted by the current
1119 * make_request_fn.
1120 * bio_list_on_stack[1] contains bios that were submitted before
1121 * the current make_request_fn, but that haven't been processed
1122 * yet.
1123 */
1124 struct bio_list bio_list_on_stack[2];
Jens Axboedece1632015-11-05 10:41:16 -07001125 blk_qc_t ret = BLK_QC_T_NONE;
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001126
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001127 if (!generic_make_request_checks(bio))
Jens Axboedece1632015-11-05 10:41:16 -07001128 goto out;
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001129
1130 /*
1131 * We only want one ->make_request_fn to be active at a time, else
1132 * stack usage with stacked devices could be a problem. So use
1133 * current->bio_list to keep a list of requests submited by a
1134 * make_request_fn function. current->bio_list is also used as a
1135 * flag to say if generic_make_request is currently active in this
1136 * task or not. If it is NULL, then no make_request is active. If
1137 * it is non-NULL, then a make_request is active, and new requests
1138 * should be added at the tail
1139 */
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001140 if (current->bio_list) {
NeilBrownf5fe1b52017-03-10 17:00:47 +11001141 bio_list_add(&current->bio_list[0], bio);
Jens Axboedece1632015-11-05 10:41:16 -07001142 goto out;
Neil Brownd89d8792007-05-01 09:53:42 +02001143 }
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001144
Neil Brownd89d8792007-05-01 09:53:42 +02001145 /* following loop may be a bit non-obvious, and so deserves some
1146 * explanation.
1147 * Before entering the loop, bio->bi_next is NULL (as all callers
1148 * ensure that) so we have a list with a single bio.
1149 * We pretend that we have just taken it off a longer list, so
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001150 * we assign bio_list to a pointer to the bio_list_on_stack,
1151 * thus initialising the bio_list of new bios to be
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001152 * added. ->make_request() may indeed add some more bios
Neil Brownd89d8792007-05-01 09:53:42 +02001153 * through a recursive call to generic_make_request. If it
1154 * did, we find a non-NULL value in bio_list and re-enter the loop
1155 * from the top. In this case we really did just take the bio
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001156 * of the top of the list (no pretending) and so remove it from
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001157 * bio_list, and call into ->make_request() again.
Neil Brownd89d8792007-05-01 09:53:42 +02001158 */
1159 BUG_ON(bio->bi_next);
NeilBrownf5fe1b52017-03-10 17:00:47 +11001160 bio_list_init(&bio_list_on_stack[0]);
1161 current->bio_list = bio_list_on_stack;
Neil Brownd89d8792007-05-01 09:53:42 +02001162 do {
Ming Leife200862019-05-15 11:03:09 +08001163 struct request_queue *q = bio->bi_disk->queue;
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001164
Christoph Hellwigaccea322020-04-28 13:27:56 +02001165 if (likely(bio_queue_enter(bio) == 0)) {
NeilBrown79bd9952017-03-08 07:38:05 +11001166 struct bio_list lower, same;
1167
1168 /* Create a fresh bio_list for all subordinate requests */
NeilBrownf5fe1b52017-03-10 17:00:47 +11001169 bio_list_on_stack[1] = bio_list_on_stack[0];
1170 bio_list_init(&bio_list_on_stack[0]);
Christoph Hellwigac7c5672020-05-16 20:28:01 +02001171 ret = do_make_request(bio);
Ming Leife200862019-05-15 11:03:09 +08001172
NeilBrown79bd9952017-03-08 07:38:05 +11001173 /* sort new bios into those for a lower level
1174 * and those for the same level
1175 */
1176 bio_list_init(&lower);
1177 bio_list_init(&same);
NeilBrownf5fe1b52017-03-10 17:00:47 +11001178 while ((bio = bio_list_pop(&bio_list_on_stack[0])) != NULL)
Christoph Hellwig74d46992017-08-23 19:10:32 +02001179 if (q == bio->bi_disk->queue)
NeilBrown79bd9952017-03-08 07:38:05 +11001180 bio_list_add(&same, bio);
1181 else
1182 bio_list_add(&lower, bio);
1183 /* now assemble so we handle the lowest level first */
NeilBrownf5fe1b52017-03-10 17:00:47 +11001184 bio_list_merge(&bio_list_on_stack[0], &lower);
1185 bio_list_merge(&bio_list_on_stack[0], &same);
1186 bio_list_merge(&bio_list_on_stack[0], &bio_list_on_stack[1]);
Dan Williams3ef28e82015-10-21 13:20:12 -04001187 }
NeilBrownf5fe1b52017-03-10 17:00:47 +11001188 bio = bio_list_pop(&bio_list_on_stack[0]);
Neil Brownd89d8792007-05-01 09:53:42 +02001189 } while (bio);
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001190 current->bio_list = NULL; /* deactivate */
Jens Axboedece1632015-11-05 10:41:16 -07001191
1192out:
1193 return ret;
Neil Brownd89d8792007-05-01 09:53:42 +02001194}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195EXPORT_SYMBOL(generic_make_request);
1196
1197/**
Christoph Hellwigf421e1d2017-11-02 21:29:50 +03001198 * direct_make_request - hand a buffer directly to its device driver for I/O
1199 * @bio: The bio describing the location in memory and on the device.
1200 *
1201 * This function behaves like generic_make_request(), but does not protect
1202 * against recursion. Must only be used if the called driver is known
Christoph Hellwig8cf79612020-04-25 09:53:36 +02001203 * to be blk-mq based.
Christoph Hellwigf421e1d2017-11-02 21:29:50 +03001204 */
1205blk_qc_t direct_make_request(struct bio *bio)
1206{
1207 struct request_queue *q = bio->bi_disk->queue;
Christoph Hellwigf421e1d2017-11-02 21:29:50 +03001208
Christoph Hellwigaccea322020-04-28 13:27:56 +02001209 if (WARN_ON_ONCE(q->make_request_fn)) {
1210 bio_io_error(bio);
Christoph Hellwigf421e1d2017-11-02 21:29:50 +03001211 return BLK_QC_T_NONE;
1212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 if (!generic_make_request_checks(bio))
Mike Christie4e49ea42016-06-05 14:31:41 -05001214 return BLK_QC_T_NONE;
Christoph Hellwigaccea322020-04-28 13:27:56 +02001215 if (unlikely(bio_queue_enter(bio)))
1216 return BLK_QC_T_NONE;
Christoph Hellwigac7c5672020-05-16 20:28:01 +02001217 if (!blk_crypto_bio_prep(&bio)) {
1218 blk_queue_exit(q);
1219 return BLK_QC_T_NONE;
1220 }
1221 return blk_mq_make_request(q, bio);
Jens Axboebf2de6f2007-09-27 13:01:25 +02001222}
San Mehat8dcbdc72010-09-14 08:48:01 +02001223EXPORT_SYMBOL_GPL(direct_make_request);
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001224
Mike Christiea8ebb052016-06-05 14:31:45 -05001225/**
Kent Overstreet4f024f32013-10-11 15:44:27 -07001226 * submit_bio - submit a bio to the block device layer for I/O
Christoph Hellwig74d46992017-08-23 19:10:32 +02001227 * @bio: The &struct bio which describes the I/O
Jens Axboebf2de6f2007-09-27 13:01:25 +02001228 *
Christoph Hellwig3fdd4082020-04-28 13:27:53 +02001229 * submit_bio() is used to submit I/O requests to block devices. It is passed a
1230 * fully set up &struct bio that describes the I/O that needs to be done. The
1231 * bio will be send to the device described by the bi_disk and bi_partno fields.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 *
Christoph Hellwig3fdd4082020-04-28 13:27:53 +02001233 * The success/failure status of the request, along with notification of
1234 * completion, is delivered asynchronously through the ->bi_end_io() callback
1235 * in @bio. The bio must NOT be touched by thecaller until ->bi_end_io() has
1236 * been called.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 */
1238blk_qc_t submit_bio(struct bio *bio)
1239{
Tejun Heod3f77df2019-06-27 13:39:52 -07001240 if (blkcg_punt_bio_submit(bio))
1241 return BLK_QC_T_NONE;
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 /*
1244 * If it's a regular read/write or a barrier with data attached,
1245 * go through the normal accounting stuff before submission.
1246 */
1247 if (bio_has_data(bio)) {
1248 unsigned int count;
1249
1250 if (unlikely(bio_op(bio) == REQ_OP_WRITE_SAME))
Jiufei Xue7c5a0dc2018-02-27 20:10:03 +08001251 count = queue_logical_block_size(bio->bi_disk->queue) >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 else
1253 count = bio_sectors(bio);
1254
1255 if (op_is_write(bio_op(bio))) {
1256 count_vm_events(PGPGOUT, count);
1257 } else {
1258 task_io_account_read(bio->bi_iter.bi_size);
1259 count_vm_events(PGPGIN, count);
1260 }
1261
1262 if (unlikely(block_dump)) {
1263 char b[BDEVNAME_SIZE];
1264 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s (%u sectors)\n",
1265 current->comm, task_pid_nr(current),
1266 op_is_write(bio_op(bio)) ? "WRITE" : "READ",
1267 (unsigned long long)bio->bi_iter.bi_sector,
1268 bio_devname(bio, b), count);
1269 }
1270 }
1271
Johannes Weinerb8e24a92019-08-08 15:03:00 -04001272 /*
Christoph Hellwig760f83e2020-04-28 13:27:54 +02001273 * If we're reading data that is part of the userspace workingset, count
1274 * submission time as memory stall. When the device is congested, or
1275 * the submitting cgroup IO-throttled, submission can be a significant
1276 * part of overall IO time.
Johannes Weinerb8e24a92019-08-08 15:03:00 -04001277 */
Christoph Hellwig760f83e2020-04-28 13:27:54 +02001278 if (unlikely(bio_op(bio) == REQ_OP_READ &&
1279 bio_flagged(bio, BIO_WORKINGSET))) {
1280 unsigned long pflags;
1281 blk_qc_t ret;
1282
Johannes Weinerb8e24a92019-08-08 15:03:00 -04001283 psi_memstall_enter(&pflags);
Christoph Hellwig760f83e2020-04-28 13:27:54 +02001284 ret = generic_make_request(bio);
Johannes Weinerb8e24a92019-08-08 15:03:00 -04001285 psi_memstall_leave(&pflags);
1286
Christoph Hellwig760f83e2020-04-28 13:27:54 +02001287 return ret;
1288 }
1289
1290 return generic_make_request(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292EXPORT_SYMBOL(submit_bio);
1293
Jens Axboe1052b8a2018-11-26 08:21:49 -07001294/**
Hannes Reineckebf4e6b42015-11-26 08:46:57 +01001295 * blk_cloned_rq_check_limits - Helper function to check a cloned request
Guoqing Jiang0d720312020-03-09 22:41:33 +01001296 * for the new queue limits
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001297 * @q: the queue
1298 * @rq: the request being checked
1299 *
1300 * Description:
1301 * @rq may have been made based on weaker limitations of upper-level queues
1302 * in request stacking drivers, and it may violate the limitation of @q.
1303 * Since the block layer and the underlying device driver trust @rq
1304 * after it is inserted to @q, it should be checked against @q before
1305 * the insertion using this generic function.
1306 *
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001307 * Request stacking drivers like request-based dm may change the queue
Hannes Reineckebf4e6b42015-11-26 08:46:57 +01001308 * limits when retrying requests on other queues. Those requests need
1309 * to be checked against the new queue limits again during dispatch.
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001310 */
Hannes Reineckebf4e6b42015-11-26 08:46:57 +01001311static int blk_cloned_rq_check_limits(struct request_queue *q,
1312 struct request *rq)
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001313{
Mike Christie8fe0d472016-06-05 14:32:15 -05001314 if (blk_rq_sectors(rq) > blk_queue_get_max_sectors(q, req_op(rq))) {
John Pittman61939b12019-05-23 17:49:39 -04001315 printk(KERN_ERR "%s: over max size limit. (%u > %u)\n",
1316 __func__, blk_rq_sectors(rq),
1317 blk_queue_get_max_sectors(q, req_op(rq)));
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001318 return -EIO;
1319 }
1320
1321 /*
1322 * queue's settings related to segment counting like q->bounce_pfn
1323 * may differ from that of other stacking queues.
1324 * Recalculate it to check the request correctly on this queue's
1325 * limitation.
1326 */
Christoph Hellwige9cd19c2019-06-06 12:29:02 +02001327 rq->nr_phys_segments = blk_recalc_rq_segments(rq);
Martin K. Petersen8a783622010-02-26 00:20:39 -05001328 if (rq->nr_phys_segments > queue_max_segments(q)) {
John Pittman61939b12019-05-23 17:49:39 -04001329 printk(KERN_ERR "%s: over max segments limit. (%hu > %hu)\n",
1330 __func__, rq->nr_phys_segments, queue_max_segments(q));
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001331 return -EIO;
1332 }
1333
1334 return 0;
1335}
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001336
1337/**
1338 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
1339 * @q: the queue to submit the request
1340 * @rq: the request being queued
1341 */
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001342blk_status_t blk_insert_cloned_request(struct request_queue *q, struct request *rq)
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001343{
Hannes Reineckebf4e6b42015-11-26 08:46:57 +01001344 if (blk_cloned_rq_check_limits(q, rq))
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001345 return BLK_STS_IOERR;
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001346
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001347 if (rq->rq_disk &&
1348 should_fail_request(&rq->rq_disk->part0, blk_rq_bytes(rq)))
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001349 return BLK_STS_IOERR;
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001350
Satya Tangiralaa892c8d2020-05-14 00:37:18 +00001351 if (blk_crypto_insert_cloned_request(rq))
1352 return BLK_STS_IOERR;
1353
Jens Axboea1ce35f2018-10-29 10:23:51 -06001354 if (blk_queue_io_stat(q))
Konstantin Khlebnikovb5af37a2020-05-27 07:24:16 +02001355 blk_account_io_start(rq);
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001356
1357 /*
Jens Axboea1ce35f2018-10-29 10:23:51 -06001358 * Since we have a scheduler attached on the top device,
1359 * bypass a potential scheduler on the bottom device for
1360 * insert.
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001361 */
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001362 return blk_mq_request_issue_directly(rq, true);
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001363}
1364EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
1365
Tejun Heo80a761f2009-07-03 17:48:17 +09001366/**
1367 * blk_rq_err_bytes - determine number of bytes till the next failure boundary
1368 * @rq: request to examine
1369 *
1370 * Description:
1371 * A request could be merge of IOs which require different failure
1372 * handling. This function determines the number of bytes which
1373 * can be failed from the beginning of the request without
1374 * crossing into area which need to be retried further.
1375 *
1376 * Return:
1377 * The number of bytes to fail.
Tejun Heo80a761f2009-07-03 17:48:17 +09001378 */
1379unsigned int blk_rq_err_bytes(const struct request *rq)
1380{
1381 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
1382 unsigned int bytes = 0;
1383 struct bio *bio;
1384
Christoph Hellwige8064022016-10-20 15:12:13 +02001385 if (!(rq->rq_flags & RQF_MIXED_MERGE))
Tejun Heo80a761f2009-07-03 17:48:17 +09001386 return blk_rq_bytes(rq);
1387
1388 /*
1389 * Currently the only 'mixing' which can happen is between
1390 * different fastfail types. We can safely fail portions
1391 * which have all the failfast bits that the first one has -
1392 * the ones which are at least as eager to fail as the first
1393 * one.
1394 */
1395 for (bio = rq->bio; bio; bio = bio->bi_next) {
Jens Axboe1eff9d32016-08-05 15:35:16 -06001396 if ((bio->bi_opf & ff) != ff)
Tejun Heo80a761f2009-07-03 17:48:17 +09001397 break;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001398 bytes += bio->bi_iter.bi_size;
Tejun Heo80a761f2009-07-03 17:48:17 +09001399 }
1400
1401 /* this could lead to infinite loop */
1402 BUG_ON(blk_rq_bytes(rq) && !bytes);
1403 return bytes;
1404}
1405EXPORT_SYMBOL_GPL(blk_rq_err_bytes);
1406
Christoph Hellwig9123bf62020-05-27 07:24:13 +02001407static void update_io_ticks(struct hd_struct *part, unsigned long now, bool end)
1408{
1409 unsigned long stamp;
1410again:
1411 stamp = READ_ONCE(part->stamp);
1412 if (unlikely(stamp != now)) {
1413 if (likely(cmpxchg(&part->stamp, stamp, now) == stamp))
1414 __part_stat_add(part, io_ticks, end ? now - stamp : 1);
1415 }
1416 if (part->partno) {
1417 part = &part_to_disk(part)->part0;
1418 goto again;
1419 }
1420}
1421
Christoph Hellwigf1394b792020-05-13 12:49:32 +02001422static void blk_account_io_completion(struct request *req, unsigned int bytes)
Jens Axboebc58ba92009-01-23 10:54:44 +01001423{
Logan Gunthorpeecb61862019-12-10 11:47:04 -07001424 if (req->part && blk_do_io_stat(req)) {
Michael Callahanddcf35d2018-07-18 04:47:39 -07001425 const int sgrp = op_stat_group(req_op(req));
Jens Axboebc58ba92009-01-23 10:54:44 +01001426 struct hd_struct *part;
Jens Axboebc58ba92009-01-23 10:54:44 +01001427
Mike Snitzer112f1582018-12-06 11:41:18 -05001428 part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +01001429 part = req->part;
Mike Snitzer112f1582018-12-06 11:41:18 -05001430 part_stat_add(part, sectors[sgrp], bytes >> 9);
Jens Axboebc58ba92009-01-23 10:54:44 +01001431 part_stat_unlock();
1432 }
1433}
1434
Omar Sandoval522a7772018-05-09 02:08:53 -07001435void blk_account_io_done(struct request *req, u64 now)
Jens Axboebc58ba92009-01-23 10:54:44 +01001436{
Jens Axboebc58ba92009-01-23 10:54:44 +01001437 /*
Tejun Heodd4c1332010-09-03 11:56:16 +02001438 * Account IO completion. flush_rq isn't accounted as a
1439 * normal IO on queueing nor completion. Accounting the
1440 * containing request is enough.
Jens Axboebc58ba92009-01-23 10:54:44 +01001441 */
Logan Gunthorpeecb61862019-12-10 11:47:04 -07001442 if (req->part && blk_do_io_stat(req) &&
1443 !(req->rq_flags & RQF_FLUSH_SEQ)) {
Michael Callahanddcf35d2018-07-18 04:47:39 -07001444 const int sgrp = op_stat_group(req_op(req));
Jens Axboebc58ba92009-01-23 10:54:44 +01001445 struct hd_struct *part;
Jens Axboebc58ba92009-01-23 10:54:44 +01001446
Mike Snitzer112f1582018-12-06 11:41:18 -05001447 part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +01001448 part = req->part;
Jens Axboebc58ba92009-01-23 10:54:44 +01001449
Konstantin Khlebnikov2b8bd422020-03-25 16:07:04 +03001450 update_io_ticks(part, jiffies, true);
Mike Snitzer112f1582018-12-06 11:41:18 -05001451 part_stat_inc(part, ios[sgrp]);
1452 part_stat_add(part, nsecs[sgrp], now - req->start_time_ns);
Christoph Hellwig524f9ff2020-05-27 07:24:19 +02001453 part_stat_unlock();
Jens Axboebc58ba92009-01-23 10:54:44 +01001454
Jens Axboe6c23a962011-01-07 08:43:37 +01001455 hd_struct_put(part);
Jens Axboebc58ba92009-01-23 10:54:44 +01001456 }
1457}
1458
Konstantin Khlebnikovb5af37a2020-05-27 07:24:16 +02001459void blk_account_io_start(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +01001460{
Jens Axboe320ae512013-10-24 09:20:05 +01001461 if (!blk_do_io_stat(rq))
1462 return;
1463
Konstantin Khlebnikovb5af37a2020-05-27 07:24:16 +02001464 rq->part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq));
Christoph Hellwig524f9ff2020-05-27 07:24:19 +02001465
Mike Snitzer112f1582018-12-06 11:41:18 -05001466 part_stat_lock();
Christoph Hellwig76268f32020-05-13 12:49:34 +02001467 update_io_ticks(rq->part, jiffies, false);
Jens Axboe320ae512013-10-24 09:20:05 +01001468 part_stat_unlock();
1469}
1470
Christoph Hellwig956d5102020-05-27 07:24:04 +02001471unsigned long disk_start_io_acct(struct gendisk *disk, unsigned int sectors,
1472 unsigned int op)
1473{
1474 struct hd_struct *part = &disk->part0;
1475 const int sgrp = op_stat_group(op);
1476 unsigned long now = READ_ONCE(jiffies);
1477
1478 part_stat_lock();
1479 update_io_ticks(part, now, false);
1480 part_stat_inc(part, ios[sgrp]);
1481 part_stat_add(part, sectors[sgrp], sectors);
1482 part_stat_local_inc(part, in_flight[op_is_write(op)]);
1483 part_stat_unlock();
1484
1485 return now;
1486}
1487EXPORT_SYMBOL(disk_start_io_acct);
1488
1489void disk_end_io_acct(struct gendisk *disk, unsigned int op,
1490 unsigned long start_time)
1491{
1492 struct hd_struct *part = &disk->part0;
1493 const int sgrp = op_stat_group(op);
1494 unsigned long now = READ_ONCE(jiffies);
1495 unsigned long duration = now - start_time;
1496
1497 part_stat_lock();
1498 update_io_ticks(part, now, true);
1499 part_stat_add(part, nsecs[sgrp], jiffies_to_nsecs(duration));
1500 part_stat_local_dec(part, in_flight[op_is_write(op)]);
1501 part_stat_unlock();
1502}
1503EXPORT_SYMBOL(disk_end_io_acct);
1504
Christoph Hellwigef71de82017-11-02 21:29:51 +03001505/*
1506 * Steal bios from a request and add them to a bio list.
1507 * The request must not have been partially completed before.
1508 */
1509void blk_steal_bios(struct bio_list *list, struct request *rq)
1510{
1511 if (rq->bio) {
1512 if (list->tail)
1513 list->tail->bi_next = rq->bio;
1514 else
1515 list->head = rq->bio;
1516 list->tail = rq->biotail;
1517
1518 rq->bio = NULL;
1519 rq->biotail = NULL;
1520 }
1521
1522 rq->__data_len = 0;
1523}
1524EXPORT_SYMBOL_GPL(blk_steal_bios);
1525
Tejun Heo9934c8c2009-05-08 11:54:16 +09001526/**
Tejun Heo2e60e022009-04-23 11:05:18 +09001527 * blk_update_request - Special helper function for request stacking drivers
Randy Dunlap8ebf9752009-06-11 20:00:41 -07001528 * @req: the request being processed
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001529 * @error: block status code
Randy Dunlap8ebf9752009-06-11 20:00:41 -07001530 * @nr_bytes: number of bytes to complete @req
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001531 *
1532 * Description:
Randy Dunlap8ebf9752009-06-11 20:00:41 -07001533 * Ends I/O on a number of bytes attached to @req, but doesn't complete
1534 * the request structure even if @req doesn't have leftover.
1535 * If @req has leftover, sets it up for the next range of segments.
Tejun Heo2e60e022009-04-23 11:05:18 +09001536 *
1537 * This special helper function is only for request stacking drivers
1538 * (e.g. request-based dm) so that they can handle partial completion.
Pavel Begunkov3a211b72019-05-23 18:43:11 +03001539 * Actual device drivers should use blk_mq_end_request instead.
Tejun Heo2e60e022009-04-23 11:05:18 +09001540 *
1541 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
1542 * %false return from this function.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001543 *
Bart Van Assche1954e9a2018-06-27 13:09:05 -07001544 * Note:
1545 * The RQF_SPECIAL_PAYLOAD flag is ignored on purpose in both
1546 * blk_rq_bytes() and in blk_update_request().
1547 *
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001548 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09001549 * %false - this request doesn't have any more data
1550 * %true - this request has more data
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001551 **/
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001552bool blk_update_request(struct request *req, blk_status_t error,
1553 unsigned int nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
Kent Overstreetf79ea412012-09-20 16:38:30 -07001555 int total_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001557 trace_block_rq_complete(req, blk_status_to_errno(error), nr_bytes);
Hannes Reinecke4a0efdc2014-10-01 14:32:31 +02001558
Tejun Heo2e60e022009-04-23 11:05:18 +09001559 if (!req->bio)
1560 return false;
1561
Max Gurtovoy54d4e6a2019-09-16 18:44:29 +03001562#ifdef CONFIG_BLK_DEV_INTEGRITY
1563 if (blk_integrity_rq(req) && req_op(req) == REQ_OP_READ &&
1564 error == BLK_STS_OK)
1565 req->q->integrity.profile->complete_fn(req, nr_bytes);
1566#endif
1567
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001568 if (unlikely(error && !blk_rq_is_passthrough(req) &&
1569 !(req->rq_flags & RQF_QUIET)))
Christoph Hellwig178cc592019-06-20 10:59:15 -07001570 print_req_error(req, error, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Jens Axboebc58ba92009-01-23 10:54:44 +01001572 blk_account_io_completion(req, nr_bytes);
Jens Axboed72d9042005-11-01 08:35:42 +01001573
Kent Overstreetf79ea412012-09-20 16:38:30 -07001574 total_bytes = 0;
1575 while (req->bio) {
1576 struct bio *bio = req->bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001577 unsigned bio_bytes = min(bio->bi_iter.bi_size, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Bart Van Assche9c24c102018-06-19 10:26:40 -07001579 if (bio_bytes == bio->bi_iter.bi_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 req->bio = bio->bi_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
NeilBrownfbbaf702017-04-07 09:40:52 -06001582 /* Completion has already been traced */
1583 bio_clear_flag(bio, BIO_TRACE_COMPLETION);
Kent Overstreetf79ea412012-09-20 16:38:30 -07001584 req_bio_endio(req, bio, bio_bytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Kent Overstreetf79ea412012-09-20 16:38:30 -07001586 total_bytes += bio_bytes;
1587 nr_bytes -= bio_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Kent Overstreetf79ea412012-09-20 16:38:30 -07001589 if (!nr_bytes)
1590 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 }
1592
1593 /*
1594 * completely done
1595 */
Tejun Heo2e60e022009-04-23 11:05:18 +09001596 if (!req->bio) {
1597 /*
1598 * Reset counters so that the request stacking driver
1599 * can find how many bytes remain in the request
1600 * later.
1601 */
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001602 req->__data_len = 0;
Tejun Heo2e60e022009-04-23 11:05:18 +09001603 return false;
1604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001606 req->__data_len -= total_bytes;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001607
1608 /* update sector only for requests with clear definition of sector */
Christoph Hellwig57292b52017-01-31 16:57:29 +01001609 if (!blk_rq_is_passthrough(req))
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001610 req->__sector += total_bytes >> 9;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001611
Tejun Heo80a761f2009-07-03 17:48:17 +09001612 /* mixed attributes always follow the first bio */
Christoph Hellwige8064022016-10-20 15:12:13 +02001613 if (req->rq_flags & RQF_MIXED_MERGE) {
Tejun Heo80a761f2009-07-03 17:48:17 +09001614 req->cmd_flags &= ~REQ_FAILFAST_MASK;
Jens Axboe1eff9d32016-08-05 15:35:16 -06001615 req->cmd_flags |= req->bio->bi_opf & REQ_FAILFAST_MASK;
Tejun Heo80a761f2009-07-03 17:48:17 +09001616 }
1617
Christoph Hellwiged6565e2017-05-11 12:34:38 +02001618 if (!(req->rq_flags & RQF_SPECIAL_PAYLOAD)) {
1619 /*
1620 * If total number of sectors is less than the first segment
1621 * size, something has gone terribly wrong.
1622 */
1623 if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
1624 blk_dump_rq_flags(req, "request botched");
1625 req->__data_len = blk_rq_cur_bytes(req);
1626 }
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001627
Christoph Hellwiged6565e2017-05-11 12:34:38 +02001628 /* recalculate the number of segments */
Christoph Hellwige9cd19c2019-06-06 12:29:02 +02001629 req->nr_phys_segments = blk_recalc_rq_segments(req);
Christoph Hellwiged6565e2017-05-11 12:34:38 +02001630 }
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001631
Tejun Heo2e60e022009-04-23 11:05:18 +09001632 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633}
Tejun Heo2e60e022009-04-23 11:05:18 +09001634EXPORT_SYMBOL_GPL(blk_update_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Ilya Loginov2d4dc892009-11-26 09:16:19 +01001636#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
1637/**
1638 * rq_flush_dcache_pages - Helper function to flush all pages in a request
1639 * @rq: the request to be flushed
1640 *
1641 * Description:
1642 * Flush all pages in @rq.
1643 */
1644void rq_flush_dcache_pages(struct request *rq)
1645{
1646 struct req_iterator iter;
Kent Overstreet79886132013-11-23 17:19:00 -08001647 struct bio_vec bvec;
Ilya Loginov2d4dc892009-11-26 09:16:19 +01001648
1649 rq_for_each_segment(bvec, rq, iter)
Kent Overstreet79886132013-11-23 17:19:00 -08001650 flush_dcache_page(bvec.bv_page);
Ilya Loginov2d4dc892009-11-26 09:16:19 +01001651}
1652EXPORT_SYMBOL_GPL(rq_flush_dcache_pages);
1653#endif
1654
Kiyoshi Uedaef9e3fa2008-10-01 16:12:15 +02001655/**
1656 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
1657 * @q : the queue of the device being checked
1658 *
1659 * Description:
1660 * Check if underlying low-level drivers of a device are busy.
1661 * If the drivers want to export their busy state, they must set own
1662 * exporting function using blk_queue_lld_busy() first.
1663 *
1664 * Basically, this function is used only by request stacking drivers
1665 * to stop dispatching requests to underlying devices when underlying
1666 * devices are busy. This behavior helps more I/O merging on the queue
1667 * of the request stacking driver and prevents I/O throughput regression
1668 * on burst I/O load.
1669 *
1670 * Return:
1671 * 0 - Not busy (The request stacking driver should dispatch request)
1672 * 1 - Busy (The request stacking driver should stop dispatching request)
1673 */
1674int blk_lld_busy(struct request_queue *q)
1675{
Jens Axboe344e9ff2018-11-15 12:22:51 -07001676 if (queue_is_mq(q) && q->mq_ops->busy)
Jens Axboe9ba20522018-10-29 10:15:10 -06001677 return q->mq_ops->busy(q);
Kiyoshi Uedaef9e3fa2008-10-01 16:12:15 +02001678
1679 return 0;
1680}
1681EXPORT_SYMBOL_GPL(blk_lld_busy);
1682
Mike Snitzer78d8e582015-06-26 10:01:13 -04001683/**
1684 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
1685 * @rq: the clone request to be cleaned up
1686 *
1687 * Description:
1688 * Free all bios in @rq for a cloned request.
1689 */
1690void blk_rq_unprep_clone(struct request *rq)
1691{
1692 struct bio *bio;
1693
1694 while ((bio = rq->bio) != NULL) {
1695 rq->bio = bio->bi_next;
1696
1697 bio_put(bio);
1698 }
1699}
1700EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
1701
Mike Snitzer78d8e582015-06-26 10:01:13 -04001702/**
1703 * blk_rq_prep_clone - Helper function to setup clone request
1704 * @rq: the request to be setup
1705 * @rq_src: original request to be cloned
1706 * @bs: bio_set that bios for clone are allocated from
1707 * @gfp_mask: memory allocation mask for bio
1708 * @bio_ctr: setup function to be called for each clone bio.
1709 * Returns %0 for success, non %0 for failure.
1710 * @data: private data to be passed to @bio_ctr
1711 *
1712 * Description:
1713 * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
Mike Snitzer78d8e582015-06-26 10:01:13 -04001714 * Also, pages which the original bios are pointing to are not copied
1715 * and the cloned bios just point same pages.
1716 * So cloned bios must be completed before original bios, which means
1717 * the caller must complete @rq before @rq_src.
1718 */
1719int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
1720 struct bio_set *bs, gfp_t gfp_mask,
1721 int (*bio_ctr)(struct bio *, struct bio *, void *),
1722 void *data)
1723{
1724 struct bio *bio, *bio_src;
1725
1726 if (!bs)
Kent Overstreetf4f81542018-05-08 21:33:52 -04001727 bs = &fs_bio_set;
Mike Snitzer78d8e582015-06-26 10:01:13 -04001728
1729 __rq_for_each_bio(bio_src, rq_src) {
1730 bio = bio_clone_fast(bio_src, gfp_mask, bs);
1731 if (!bio)
1732 goto free_and_out;
1733
1734 if (bio_ctr && bio_ctr(bio, bio_src, data))
1735 goto free_and_out;
1736
1737 if (rq->bio) {
1738 rq->biotail->bi_next = bio;
1739 rq->biotail = bio;
1740 } else
1741 rq->bio = rq->biotail = bio;
1742 }
1743
Guoqing Jiang361301a2020-03-09 22:41:36 +01001744 /* Copy attributes of the original request to the clone request. */
1745 rq->__sector = blk_rq_pos(rq_src);
1746 rq->__data_len = blk_rq_bytes(rq_src);
1747 if (rq_src->rq_flags & RQF_SPECIAL_PAYLOAD) {
1748 rq->rq_flags |= RQF_SPECIAL_PAYLOAD;
1749 rq->special_vec = rq_src->special_vec;
1750 }
1751 rq->nr_phys_segments = rq_src->nr_phys_segments;
1752 rq->ioprio = rq_src->ioprio;
Mike Snitzer78d8e582015-06-26 10:01:13 -04001753
Satya Tangiralaa892c8d2020-05-14 00:37:18 +00001754 if (rq->bio)
1755 blk_crypto_rq_bio_prep(rq, rq->bio, gfp_mask);
Mike Snitzer78d8e582015-06-26 10:01:13 -04001756
1757 return 0;
1758
1759free_and_out:
1760 if (bio)
1761 bio_put(bio);
1762 blk_rq_unprep_clone(rq);
1763
1764 return -ENOMEM;
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02001765}
1766EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
1767
Jens Axboe59c3d452014-04-08 09:15:35 -06001768int kblockd_schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769{
1770 return queue_work(kblockd_workqueue, work);
1771}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772EXPORT_SYMBOL(kblockd_schedule_work);
1773
Jens Axboe818cd1c2017-04-10 09:54:55 -06001774int kblockd_mod_delayed_work_on(int cpu, struct delayed_work *dwork,
1775 unsigned long delay)
1776{
1777 return mod_delayed_work_on(cpu, kblockd_workqueue, dwork, delay);
1778}
1779EXPORT_SYMBOL(kblockd_mod_delayed_work_on);
1780
Suresh Jayaraman75df7132011-09-21 10:00:16 +02001781/**
1782 * blk_start_plug - initialize blk_plug and track it inside the task_struct
1783 * @plug: The &struct blk_plug that needs to be initialized
1784 *
1785 * Description:
Jeff Moyer40405852019-01-08 16:57:34 -05001786 * blk_start_plug() indicates to the block layer an intent by the caller
1787 * to submit multiple I/O requests in a batch. The block layer may use
1788 * this hint to defer submitting I/Os from the caller until blk_finish_plug()
1789 * is called. However, the block layer may choose to submit requests
1790 * before a call to blk_finish_plug() if the number of queued I/Os
1791 * exceeds %BLK_MAX_REQUEST_COUNT, or if the size of the I/O is larger than
1792 * %BLK_PLUG_FLUSH_SIZE. The queued I/Os may also be submitted early if
1793 * the task schedules (see below).
1794 *
Suresh Jayaraman75df7132011-09-21 10:00:16 +02001795 * Tracking blk_plug inside the task_struct will help with auto-flushing the
1796 * pending I/O should the task end up blocking between blk_start_plug() and
1797 * blk_finish_plug(). This is important from a performance perspective, but
1798 * also ensures that we don't deadlock. For instance, if the task is blocking
1799 * for a memory allocation, memory reclaim could end up wanting to free a
1800 * page belonging to that request that is currently residing in our private
1801 * plug. By flushing the pending I/O when the process goes to sleep, we avoid
1802 * this kind of deadlock.
1803 */
Jens Axboe73c10102011-03-08 13:19:51 +01001804void blk_start_plug(struct blk_plug *plug)
1805{
1806 struct task_struct *tsk = current;
1807
Shaohua Lidd6cf3e2015-05-08 10:51:28 -07001808 /*
1809 * If this is a nested plug, don't actually assign it.
1810 */
1811 if (tsk->plug)
1812 return;
1813
Jens Axboe320ae512013-10-24 09:20:05 +01001814 INIT_LIST_HEAD(&plug->mq_list);
NeilBrown048c9372011-04-18 09:52:22 +02001815 INIT_LIST_HEAD(&plug->cb_list);
Jens Axboe5f0ed772018-11-23 22:04:33 -07001816 plug->rq_count = 0;
Jens Axboece5b0092018-11-27 17:13:56 -07001817 plug->multiple_queues = false;
Jens Axboe5f0ed772018-11-23 22:04:33 -07001818
Jens Axboe73c10102011-03-08 13:19:51 +01001819 /*
Shaohua Lidd6cf3e2015-05-08 10:51:28 -07001820 * Store ordering should not be needed here, since a potential
1821 * preempt will imply a full memory barrier
Jens Axboe73c10102011-03-08 13:19:51 +01001822 */
Shaohua Lidd6cf3e2015-05-08 10:51:28 -07001823 tsk->plug = plug;
Jens Axboe73c10102011-03-08 13:19:51 +01001824}
1825EXPORT_SYMBOL(blk_start_plug);
1826
NeilBrown74018dc2012-07-31 09:08:15 +02001827static void flush_plug_callbacks(struct blk_plug *plug, bool from_schedule)
NeilBrown048c9372011-04-18 09:52:22 +02001828{
1829 LIST_HEAD(callbacks);
1830
Shaohua Li2a7d5552012-07-31 09:08:15 +02001831 while (!list_empty(&plug->cb_list)) {
1832 list_splice_init(&plug->cb_list, &callbacks);
NeilBrown048c9372011-04-18 09:52:22 +02001833
Shaohua Li2a7d5552012-07-31 09:08:15 +02001834 while (!list_empty(&callbacks)) {
1835 struct blk_plug_cb *cb = list_first_entry(&callbacks,
NeilBrown048c9372011-04-18 09:52:22 +02001836 struct blk_plug_cb,
1837 list);
Shaohua Li2a7d5552012-07-31 09:08:15 +02001838 list_del(&cb->list);
NeilBrown74018dc2012-07-31 09:08:15 +02001839 cb->callback(cb, from_schedule);
Shaohua Li2a7d5552012-07-31 09:08:15 +02001840 }
NeilBrown048c9372011-04-18 09:52:22 +02001841 }
1842}
1843
NeilBrown9cbb1752012-07-31 09:08:14 +02001844struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug, void *data,
1845 int size)
1846{
1847 struct blk_plug *plug = current->plug;
1848 struct blk_plug_cb *cb;
1849
1850 if (!plug)
1851 return NULL;
1852
1853 list_for_each_entry(cb, &plug->cb_list, list)
1854 if (cb->callback == unplug && cb->data == data)
1855 return cb;
1856
1857 /* Not currently on the callback list */
1858 BUG_ON(size < sizeof(*cb));
1859 cb = kzalloc(size, GFP_ATOMIC);
1860 if (cb) {
1861 cb->data = data;
1862 cb->callback = unplug;
1863 list_add(&cb->list, &plug->cb_list);
1864 }
1865 return cb;
1866}
1867EXPORT_SYMBOL(blk_check_plugged);
1868
Jens Axboe49cac012011-04-16 13:51:05 +02001869void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
Jens Axboe73c10102011-03-08 13:19:51 +01001870{
NeilBrown74018dc2012-07-31 09:08:15 +02001871 flush_plug_callbacks(plug, from_schedule);
Jens Axboe320ae512013-10-24 09:20:05 +01001872
1873 if (!list_empty(&plug->mq_list))
1874 blk_mq_flush_plug_list(plug, from_schedule);
Jens Axboe73c10102011-03-08 13:19:51 +01001875}
Jens Axboe73c10102011-03-08 13:19:51 +01001876
Jeff Moyer40405852019-01-08 16:57:34 -05001877/**
1878 * blk_finish_plug - mark the end of a batch of submitted I/O
1879 * @plug: The &struct blk_plug passed to blk_start_plug()
1880 *
1881 * Description:
1882 * Indicate that a batch of I/O submissions is complete. This function
1883 * must be paired with an initial call to blk_start_plug(). The intent
1884 * is to allow the block layer to optimize I/O submission. See the
1885 * documentation for blk_start_plug() for more information.
1886 */
Jens Axboe73c10102011-03-08 13:19:51 +01001887void blk_finish_plug(struct blk_plug *plug)
1888{
Shaohua Lidd6cf3e2015-05-08 10:51:28 -07001889 if (plug != current->plug)
1890 return;
Jens Axboef6603782011-04-15 15:49:07 +02001891 blk_flush_plug_list(plug, false);
Christoph Hellwig88b996c2011-04-15 15:20:10 +02001892
Shaohua Lidd6cf3e2015-05-08 10:51:28 -07001893 current->plug = NULL;
Jens Axboe73c10102011-03-08 13:19:51 +01001894}
1895EXPORT_SYMBOL(blk_finish_plug);
1896
Ming Lei71ac8602020-05-14 16:45:09 +08001897void blk_io_schedule(void)
1898{
1899 /* Prevent hang_check timer from firing at us during very long I/O */
1900 unsigned long timeout = sysctl_hung_task_timeout_secs * HZ / 2;
1901
1902 if (timeout)
1903 io_schedule_timeout(timeout);
1904 else
1905 io_schedule();
1906}
1907EXPORT_SYMBOL_GPL(blk_io_schedule);
1908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909int __init blk_dev_init(void)
1910{
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001911 BUILD_BUG_ON(REQ_OP_LAST >= (1 << REQ_OP_BITS));
1912 BUILD_BUG_ON(REQ_OP_BITS + REQ_FLAG_BITS > 8 *
Pankaj Bharadiyac5936422019-12-09 10:31:43 -08001913 sizeof_field(struct request, cmd_flags));
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001914 BUILD_BUG_ON(REQ_OP_BITS + REQ_FLAG_BITS > 8 *
Pankaj Bharadiyac5936422019-12-09 10:31:43 -08001915 sizeof_field(struct bio, bi_opf));
Nikanth Karthikesan9eb55b02009-04-27 14:53:54 +02001916
Tejun Heo89b90be2011-01-03 15:01:47 +01001917 /* used for unplugging and affects IO latency/throughput - HIGHPRI */
1918 kblockd_workqueue = alloc_workqueue("kblockd",
Matias Bjørling28747fc2014-06-11 23:43:54 +02001919 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 if (!kblockd_workqueue)
1921 panic("Failed to create kblockd\n");
1922
Ilya Dryomovc2789bd2015-11-20 22:16:46 +01001923 blk_requestq_cachep = kmem_cache_create("request_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02001924 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Omar Sandoval18fbda92017-01-31 14:53:20 -08001926#ifdef CONFIG_DEBUG_FS
1927 blk_debugfs_root = debugfs_create_dir("block", NULL);
1928#endif
1929
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 return 0;
1931}