blob: 6bd4d1754d294a1ff4f6e9a7ebdc011580794e72 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 1994, Karl Keyte: Added support for disk statistics
4 * Elevator latency, (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
5 * Queue request tables / lock, selectable elevator, Jens Axboe <axboe@suse.de>
Jens Axboe6728cb02008-01-31 13:03:55 +01006 * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au>
7 * - July2000
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
9 */
10
11/*
12 * This handles all read/write requests to block devices
13 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/backing-dev.h>
17#include <linux/bio.h>
18#include <linux/blkdev.h>
Jens Axboe320ae512013-10-24 09:20:05 +010019#include <linux/blk-mq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/highmem.h>
21#include <linux/mm.h>
22#include <linux/kernel_stat.h>
23#include <linux/string.h>
24#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/completion.h>
26#include <linux/slab.h>
27#include <linux/swap.h>
28#include <linux/writeback.h>
Andrew Mortonfaccbd4b2006-12-10 02:19:35 -080029#include <linux/task_io_accounting_ops.h>
Akinobu Mitac17bb492006-12-08 02:39:46 -080030#include <linux/fault-inject.h>
Jens Axboe73c10102011-03-08 13:19:51 +010031#include <linux/list_sort.h>
Tejun Heoe3c78ca2011-10-19 14:32:38 +020032#include <linux/delay.h>
Tejun Heoaaf7c682012-04-19 16:29:22 -070033#include <linux/ratelimit.h>
Lin Ming6c954662013-03-23 11:42:26 +080034#include <linux/pm_runtime.h>
Tejun Heoeea8f412015-05-22 17:13:17 -040035#include <linux/blk-cgroup.h>
Omar Sandoval18fbda92017-01-31 14:53:20 -080036#include <linux/debugfs.h>
Li Zefan55782132009-06-09 13:43:05 +080037
38#define CREATE_TRACE_POINTS
39#include <trace/events/block.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Jens Axboe8324aa92008-01-29 14:51:59 +010041#include "blk.h"
Ming Lei43a5e4e2013-12-26 21:31:35 +080042#include "blk-mq.h"
Jens Axboebd166ef2017-01-17 06:03:22 -070043#include "blk-mq-sched.h"
Jens Axboe87760e52016-11-09 12:38:14 -070044#include "blk-wbt.h"
Jens Axboe8324aa92008-01-29 14:51:59 +010045
Omar Sandoval18fbda92017-01-31 14:53:20 -080046#ifdef CONFIG_DEBUG_FS
47struct dentry *blk_debugfs_root;
48#endif
49
Mike Snitzerd07335e2010-11-16 12:52:38 +010050EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_remap);
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +020051EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_remap);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -070052EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_complete);
Keith Busch3291fa52014-04-28 12:30:52 -060053EXPORT_TRACEPOINT_SYMBOL_GPL(block_split);
NeilBrowncbae8d42012-12-14 20:49:27 +010054EXPORT_TRACEPOINT_SYMBOL_GPL(block_unplug);
Ingo Molnar0bfc2452008-11-26 11:59:56 +010055
Tejun Heoa73f7302011-12-14 00:33:37 +010056DEFINE_IDA(blk_queue_ida);
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/*
59 * For the allocated request tables
60 */
Wei Tangd674d412015-11-24 09:58:45 +080061struct kmem_cache *request_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63/*
64 * For queue allocation
65 */
Jens Axboe6728cb02008-01-31 13:03:55 +010066struct kmem_cache *blk_requestq_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 * Controlling structure to kblockd
70 */
Jens Axboeff856ba2006-01-09 16:02:34 +010071static struct workqueue_struct *kblockd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Tejun Heod40f75a2015-05-22 17:13:42 -040073static void blk_clear_congested(struct request_list *rl, int sync)
74{
Tejun Heod40f75a2015-05-22 17:13:42 -040075#ifdef CONFIG_CGROUP_WRITEBACK
76 clear_wb_congested(rl->blkg->wb_congested, sync);
77#else
Tejun Heo482cf792015-05-22 17:13:43 -040078 /*
79 * If !CGROUP_WRITEBACK, all blkg's map to bdi->wb and we shouldn't
80 * flip its congestion state for events on other blkcgs.
81 */
82 if (rl == &rl->q->root_rl)
Jan Karadc3b17c2017-02-02 15:56:50 +010083 clear_wb_congested(rl->q->backing_dev_info->wb.congested, sync);
Tejun Heod40f75a2015-05-22 17:13:42 -040084#endif
85}
86
87static void blk_set_congested(struct request_list *rl, int sync)
88{
Tejun Heod40f75a2015-05-22 17:13:42 -040089#ifdef CONFIG_CGROUP_WRITEBACK
90 set_wb_congested(rl->blkg->wb_congested, sync);
91#else
Tejun Heo482cf792015-05-22 17:13:43 -040092 /* see blk_clear_congested() */
93 if (rl == &rl->q->root_rl)
Jan Karadc3b17c2017-02-02 15:56:50 +010094 set_wb_congested(rl->q->backing_dev_info->wb.congested, sync);
Tejun Heod40f75a2015-05-22 17:13:42 -040095#endif
96}
97
Jens Axboe8324aa92008-01-29 14:51:59 +010098void blk_queue_congestion_threshold(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
100 int nr;
101
102 nr = q->nr_requests - (q->nr_requests / 8) + 1;
103 if (nr > q->nr_requests)
104 nr = q->nr_requests;
105 q->nr_congestion_on = nr;
106
107 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
108 if (nr < 1)
109 nr = 1;
110 q->nr_congestion_off = nr;
111}
112
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200113void blk_rq_init(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200115 memset(rq, 0, sizeof(*rq));
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 INIT_LIST_HEAD(&rq->queuelist);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700118 INIT_LIST_HEAD(&rq->timeout_list);
Jens Axboec7c22e42008-09-13 20:26:01 +0200119 rq->cpu = -1;
Jens Axboe63a71382008-02-08 12:41:03 +0100120 rq->q = q;
Tejun Heoa2dec7b2009-05-07 22:24:44 +0900121 rq->__sector = (sector_t) -1;
Jens Axboe2e662b62006-07-13 11:55:04 +0200122 INIT_HLIST_NODE(&rq->hash);
123 RB_CLEAR_NODE(&rq->rb_node);
Jens Axboe63a71382008-02-08 12:41:03 +0100124 rq->tag = -1;
Jens Axboebd166ef2017-01-17 06:03:22 -0700125 rq->internal_tag = -1;
Tejun Heob243ddc2009-04-23 11:05:18 +0900126 rq->start_time = jiffies;
Divyesh Shah91952912010-04-01 15:01:41 -0700127 set_start_time_ns(rq);
Jerome Marchand09e099d2011-01-05 16:57:38 +0100128 rq->part = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200130EXPORT_SYMBOL(blk_rq_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
NeilBrown5bb23a62007-09-27 12:46:13 +0200132static void req_bio_endio(struct request *rq, struct bio *bio,
133 unsigned int nbytes, int error)
Tejun Heo797e7db2006-01-06 09:51:03 +0100134{
Mike Snitzer78d8e582015-06-26 10:01:13 -0400135 if (error)
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200136 bio->bi_error = error;
Tejun Heo797e7db2006-01-06 09:51:03 +0100137
Christoph Hellwige8064022016-10-20 15:12:13 +0200138 if (unlikely(rq->rq_flags & RQF_QUIET))
Jens Axboeb7c44ed2015-07-24 12:37:59 -0600139 bio_set_flag(bio, BIO_QUIET);
Tejun Heo143a87f2011-01-25 12:43:52 +0100140
Kent Overstreetf79ea412012-09-20 16:38:30 -0700141 bio_advance(bio, nbytes);
Tejun Heo143a87f2011-01-25 12:43:52 +0100142
143 /* don't actually finish bio if it's part of flush sequence */
Christoph Hellwige8064022016-10-20 15:12:13 +0200144 if (bio->bi_iter.bi_size == 0 && !(rq->rq_flags & RQF_FLUSH_SEQ))
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200145 bio_endio(bio);
Tejun Heo797e7db2006-01-06 09:51:03 +0100146}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148void blk_dump_rq_flags(struct request *rq, char *msg)
149{
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100150 printk(KERN_INFO "%s: dev %s: flags=%llx\n", msg,
151 rq->rq_disk ? rq->rq_disk->disk_name : "?",
Jens Axboe59533162013-05-23 12:25:08 +0200152 (unsigned long long) rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Tejun Heo83096eb2009-05-07 22:24:39 +0900154 printk(KERN_INFO " sector %llu, nr/cnr %u/%u\n",
155 (unsigned long long)blk_rq_pos(rq),
156 blk_rq_sectors(rq), blk_rq_cur_sectors(rq));
Jens Axboeb4f42e22014-04-10 09:46:28 -0600157 printk(KERN_INFO " bio %p, biotail %p, len %u\n",
158 rq->bio, rq->biotail, blk_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160EXPORT_SYMBOL(blk_dump_rq_flags);
161
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500162static void blk_delay_work(struct work_struct *work)
Jens Axboe6c5e0c42008-08-01 20:31:32 +0200163{
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500164 struct request_queue *q;
Jens Axboe6c5e0c42008-08-01 20:31:32 +0200165
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500166 q = container_of(work, struct request_queue, delay_work.work);
167 spin_lock_irq(q->queue_lock);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200168 __blk_run_queue(q);
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500169 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172/**
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500173 * blk_delay_queue - restart queueing after defined interval
174 * @q: The &struct request_queue in question
175 * @msecs: Delay in msecs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 *
177 * Description:
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500178 * Sometimes queueing needs to be postponed for a little while, to allow
179 * resources to come back. This function will make sure that queueing is
Bart Van Assche70460572012-11-28 13:45:56 +0100180 * restarted around the specified time. Queue lock must be held.
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500181 */
182void blk_delay_queue(struct request_queue *q, unsigned long msecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Bart Van Assche70460572012-11-28 13:45:56 +0100184 if (likely(!blk_queue_dead(q)))
185 queue_delayed_work(kblockd_workqueue, &q->delay_work,
186 msecs_to_jiffies(msecs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500188EXPORT_SYMBOL(blk_delay_queue);
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/**
Jens Axboe21491412015-12-28 13:01:22 -0700191 * blk_start_queue_async - asynchronously restart a previously stopped queue
192 * @q: The &struct request_queue in question
193 *
194 * Description:
195 * blk_start_queue_async() will clear the stop flag on the queue, and
196 * ensure that the request_fn for the queue is run from an async
197 * context.
198 **/
199void blk_start_queue_async(struct request_queue *q)
200{
201 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
202 blk_run_queue_async(q);
203}
204EXPORT_SYMBOL(blk_start_queue_async);
205
206/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 * blk_start_queue - restart a previously stopped queue
Jens Axboe165125e2007-07-24 09:28:11 +0200208 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 *
210 * Description:
211 * blk_start_queue() will clear the stop flag on the queue, and call
212 * the request_fn for the queue if it was in a stopped state when
213 * entered. Also see blk_stop_queue(). Queue lock must be held.
214 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200215void blk_start_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200217 WARN_ON(!irqs_disabled());
218
Nick Piggin75ad23b2008-04-29 14:48:33 +0200219 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200220 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222EXPORT_SYMBOL(blk_start_queue);
223
224/**
225 * blk_stop_queue - stop a queue
Jens Axboe165125e2007-07-24 09:28:11 +0200226 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 *
228 * Description:
229 * The Linux block layer assumes that a block driver will consume all
230 * entries on the request queue when the request_fn strategy is called.
231 * Often this will not happen, because of hardware limitations (queue
232 * depth settings). If a device driver gets a 'queue full' response,
233 * or if it simply chooses not to queue more I/O at one point, it can
234 * call this function to prevent the request_fn from being called until
235 * the driver has signalled it's ready to go again. This happens by calling
236 * blk_start_queue() to restart queue operations. Queue lock must be held.
237 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200238void blk_stop_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Tejun Heo136b5722012-08-21 13:18:24 -0700240 cancel_delayed_work(&q->delay_work);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200241 queue_flag_set(QUEUE_FLAG_STOPPED, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243EXPORT_SYMBOL(blk_stop_queue);
244
245/**
246 * blk_sync_queue - cancel any pending callbacks on a queue
247 * @q: the queue
248 *
249 * Description:
250 * The block layer may perform asynchronous callback activity
251 * on a queue, such as calling the unplug function after a timeout.
252 * A block device may call blk_sync_queue to ensure that any
253 * such activity is cancelled, thus allowing it to release resources
Michael Opdenacker59c51592007-05-09 08:57:56 +0200254 * that the callbacks might use. The caller must already have made sure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 * that its ->make_request_fn will not re-add plugging prior to calling
256 * this function.
257 *
Vivek Goyalda527772011-03-02 19:05:33 -0500258 * This function does not cancel any asynchronous activity arising
Masanari Iidada3dae52014-09-09 01:27:23 +0900259 * out of elevator or throttling code. That would require elevator_exit()
Tejun Heo5efd6112012-03-05 13:15:12 -0800260 * and blkcg_exit_queue() to be called with queue lock initialized.
Vivek Goyalda527772011-03-02 19:05:33 -0500261 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 */
263void blk_sync_queue(struct request_queue *q)
264{
Jens Axboe70ed28b2008-11-19 14:38:39 +0100265 del_timer_sync(&q->timeout);
Ming Leif04c1fe2013-12-26 21:31:36 +0800266
267 if (q->mq_ops) {
268 struct blk_mq_hw_ctx *hctx;
269 int i;
270
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600271 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe27489a32016-08-24 15:54:25 -0600272 cancel_work_sync(&hctx->run_work);
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600273 cancel_delayed_work_sync(&hctx->delay_work);
274 }
Ming Leif04c1fe2013-12-26 21:31:36 +0800275 } else {
276 cancel_delayed_work_sync(&q->delay_work);
277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279EXPORT_SYMBOL(blk_sync_queue);
280
281/**
Bart Van Asschec246e802012-12-06 14:32:01 +0100282 * __blk_run_queue_uncond - run a queue whether or not it has been stopped
283 * @q: The queue to run
284 *
285 * Description:
286 * Invoke request handling on a queue if there are any pending requests.
287 * May be used to restart request handling after a request has completed.
288 * This variant runs the queue whether or not the queue has been
289 * stopped. Must be called with the queue lock held and interrupts
290 * disabled. See also @blk_run_queue.
291 */
292inline void __blk_run_queue_uncond(struct request_queue *q)
293{
294 if (unlikely(blk_queue_dead(q)))
295 return;
296
Bart Van Assche24faf6f2012-11-28 13:46:45 +0100297 /*
298 * Some request_fn implementations, e.g. scsi_request_fn(), unlock
299 * the queue lock internally. As a result multiple threads may be
300 * running such a request function concurrently. Keep track of the
301 * number of active request_fn invocations such that blk_drain_queue()
302 * can wait until all these request_fn calls have finished.
303 */
304 q->request_fn_active++;
Bart Van Asschec246e802012-12-06 14:32:01 +0100305 q->request_fn(q);
Bart Van Assche24faf6f2012-11-28 13:46:45 +0100306 q->request_fn_active--;
Bart Van Asschec246e802012-12-06 14:32:01 +0100307}
Christoph Hellwiga7928c12015-04-17 22:37:20 +0200308EXPORT_SYMBOL_GPL(__blk_run_queue_uncond);
Bart Van Asschec246e802012-12-06 14:32:01 +0100309
310/**
Jens Axboe80a4b582008-10-14 09:51:06 +0200311 * __blk_run_queue - run a single device queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200313 *
314 * Description:
315 * See @blk_run_queue. This variant must be called with the queue lock
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200316 * held and interrupts disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 */
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200318void __blk_run_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
Tejun Heoa538cd02009-04-23 11:05:17 +0900320 if (unlikely(blk_queue_stopped(q)))
321 return;
322
Bart Van Asschec246e802012-12-06 14:32:01 +0100323 __blk_run_queue_uncond(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200324}
325EXPORT_SYMBOL(__blk_run_queue);
Jens Axboedac07ec2006-05-11 08:20:16 +0200326
Nick Piggin75ad23b2008-04-29 14:48:33 +0200327/**
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200328 * blk_run_queue_async - run a single device queue in workqueue context
329 * @q: The queue to run
330 *
331 * Description:
332 * Tells kblockd to perform the equivalent of @blk_run_queue on behalf
Bart Van Assche70460572012-11-28 13:45:56 +0100333 * of us. The caller must hold the queue lock.
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200334 */
335void blk_run_queue_async(struct request_queue *q)
336{
Bart Van Assche70460572012-11-28 13:45:56 +0100337 if (likely(!blk_queue_stopped(q) && !blk_queue_dead(q)))
Tejun Heoe7c2f962012-08-21 13:18:24 -0700338 mod_delayed_work(kblockd_workqueue, &q->delay_work, 0);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200339}
Jens Axboec21e6be2011-04-19 13:32:46 +0200340EXPORT_SYMBOL(blk_run_queue_async);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200341
342/**
Nick Piggin75ad23b2008-04-29 14:48:33 +0200343 * blk_run_queue - run a single device queue
344 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200345 *
346 * Description:
347 * Invoke request handling on this queue, if it has pending work to do.
Tejun Heoa7f55792009-04-23 11:05:17 +0900348 * May be used to restart queueing when a request has completed.
Nick Piggin75ad23b2008-04-29 14:48:33 +0200349 */
350void blk_run_queue(struct request_queue *q)
351{
352 unsigned long flags;
353
354 spin_lock_irqsave(q->queue_lock, flags);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200355 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 spin_unlock_irqrestore(q->queue_lock, flags);
357}
358EXPORT_SYMBOL(blk_run_queue);
359
Jens Axboe165125e2007-07-24 09:28:11 +0200360void blk_put_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500361{
362 kobject_put(&q->kobj);
363}
Jens Axboed86e0e82011-05-27 07:44:43 +0200364EXPORT_SYMBOL(blk_put_queue);
Al Viro483f4af2006-03-18 18:34:37 -0500365
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200366/**
Bart Van Assche807592a2012-11-28 13:43:38 +0100367 * __blk_drain_queue - drain requests from request_queue
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200368 * @q: queue to drain
Tejun Heoc9a929d2011-10-19 14:42:16 +0200369 * @drain_all: whether to drain all requests or only the ones w/ ELVPRIV
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200370 *
Tejun Heoc9a929d2011-10-19 14:42:16 +0200371 * Drain requests from @q. If @drain_all is set, all requests are drained.
372 * If not, only ELVPRIV requests are drained. The caller is responsible
373 * for ensuring that no new requests which need to be drained are queued.
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200374 */
Bart Van Assche807592a2012-11-28 13:43:38 +0100375static void __blk_drain_queue(struct request_queue *q, bool drain_all)
376 __releases(q->queue_lock)
377 __acquires(q->queue_lock)
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200378{
Asias He458f27a2012-06-15 08:45:25 +0200379 int i;
380
Bart Van Assche807592a2012-11-28 13:43:38 +0100381 lockdep_assert_held(q->queue_lock);
382
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200383 while (true) {
Tejun Heo481a7d62011-12-14 00:33:37 +0100384 bool drain = false;
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200385
Tejun Heob855b042012-03-06 21:24:55 +0100386 /*
387 * The caller might be trying to drain @q before its
388 * elevator is initialized.
389 */
390 if (q->elevator)
391 elv_drain_elevator(q);
392
Tejun Heo5efd6112012-03-05 13:15:12 -0800393 blkcg_drain_queue(q);
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200394
Tejun Heo4eabc942011-12-15 20:03:04 +0100395 /*
396 * This function might be called on a queue which failed
Tejun Heob855b042012-03-06 21:24:55 +0100397 * driver init after queue creation or is not yet fully
398 * active yet. Some drivers (e.g. fd and loop) get unhappy
399 * in such cases. Kick queue iff dispatch queue has
400 * something on it and @q has request_fn set.
Tejun Heo4eabc942011-12-15 20:03:04 +0100401 */
Tejun Heob855b042012-03-06 21:24:55 +0100402 if (!list_empty(&q->queue_head) && q->request_fn)
Tejun Heo4eabc942011-12-15 20:03:04 +0100403 __blk_run_queue(q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200404
Tejun Heo8a5ecdd2012-06-04 20:40:58 -0700405 drain |= q->nr_rqs_elvpriv;
Bart Van Assche24faf6f2012-11-28 13:46:45 +0100406 drain |= q->request_fn_active;
Tejun Heo481a7d62011-12-14 00:33:37 +0100407
408 /*
409 * Unfortunately, requests are queued at and tracked from
410 * multiple places and there's no single counter which can
411 * be drained. Check all the queues and counters.
412 */
413 if (drain_all) {
Ming Leie97c2932014-09-25 23:23:46 +0800414 struct blk_flush_queue *fq = blk_get_flush_queue(q, NULL);
Tejun Heo481a7d62011-12-14 00:33:37 +0100415 drain |= !list_empty(&q->queue_head);
416 for (i = 0; i < 2; i++) {
Tejun Heo8a5ecdd2012-06-04 20:40:58 -0700417 drain |= q->nr_rqs[i];
Tejun Heo481a7d62011-12-14 00:33:37 +0100418 drain |= q->in_flight[i];
Ming Lei7c94e1c2014-09-25 23:23:43 +0800419 if (fq)
420 drain |= !list_empty(&fq->flush_queue[i]);
Tejun Heo481a7d62011-12-14 00:33:37 +0100421 }
422 }
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200423
Tejun Heo481a7d62011-12-14 00:33:37 +0100424 if (!drain)
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200425 break;
Bart Van Assche807592a2012-11-28 13:43:38 +0100426
427 spin_unlock_irq(q->queue_lock);
428
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200429 msleep(10);
Bart Van Assche807592a2012-11-28 13:43:38 +0100430
431 spin_lock_irq(q->queue_lock);
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200432 }
Asias He458f27a2012-06-15 08:45:25 +0200433
434 /*
435 * With queue marked dead, any woken up waiter will fail the
436 * allocation path, so the wakeup chaining is lost and we're
437 * left with hung waiters. We need to wake up those waiters.
438 */
439 if (q->request_fn) {
Tejun Heoa0516612012-06-26 15:05:44 -0700440 struct request_list *rl;
441
Tejun Heoa0516612012-06-26 15:05:44 -0700442 blk_queue_for_each_rl(rl, q)
443 for (i = 0; i < ARRAY_SIZE(rl->wait); i++)
444 wake_up_all(&rl->wait[i]);
Asias He458f27a2012-06-15 08:45:25 +0200445 }
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200446}
447
Tejun Heoc9a929d2011-10-19 14:42:16 +0200448/**
Tejun Heod7325802012-03-05 13:14:58 -0800449 * blk_queue_bypass_start - enter queue bypass mode
450 * @q: queue of interest
451 *
452 * In bypass mode, only the dispatch FIFO queue of @q is used. This
453 * function makes @q enter bypass mode and drains all requests which were
Tejun Heo6ecf23a2012-03-05 13:14:59 -0800454 * throttled or issued before. On return, it's guaranteed that no request
Tejun Heo80fd9972012-04-13 14:50:53 -0700455 * is being throttled or has ELVPRIV set and blk_queue_bypass() %true
456 * inside queue or RCU read lock.
Tejun Heod7325802012-03-05 13:14:58 -0800457 */
458void blk_queue_bypass_start(struct request_queue *q)
459{
460 spin_lock_irq(q->queue_lock);
Tejun Heo776687b2014-07-01 10:29:17 -0600461 q->bypass_depth++;
Tejun Heod7325802012-03-05 13:14:58 -0800462 queue_flag_set(QUEUE_FLAG_BYPASS, q);
463 spin_unlock_irq(q->queue_lock);
464
Tejun Heo776687b2014-07-01 10:29:17 -0600465 /*
466 * Queues start drained. Skip actual draining till init is
467 * complete. This avoids lenghty delays during queue init which
468 * can happen many times during boot.
469 */
470 if (blk_queue_init_done(q)) {
Bart Van Assche807592a2012-11-28 13:43:38 +0100471 spin_lock_irq(q->queue_lock);
472 __blk_drain_queue(q, false);
473 spin_unlock_irq(q->queue_lock);
474
Tejun Heob82d4b12012-04-13 13:11:31 -0700475 /* ensure blk_queue_bypass() is %true inside RCU read lock */
476 synchronize_rcu();
477 }
Tejun Heod7325802012-03-05 13:14:58 -0800478}
479EXPORT_SYMBOL_GPL(blk_queue_bypass_start);
480
481/**
482 * blk_queue_bypass_end - leave queue bypass mode
483 * @q: queue of interest
484 *
485 * Leave bypass mode and restore the normal queueing behavior.
486 */
487void blk_queue_bypass_end(struct request_queue *q)
488{
489 spin_lock_irq(q->queue_lock);
490 if (!--q->bypass_depth)
491 queue_flag_clear(QUEUE_FLAG_BYPASS, q);
492 WARN_ON_ONCE(q->bypass_depth < 0);
493 spin_unlock_irq(q->queue_lock);
494}
495EXPORT_SYMBOL_GPL(blk_queue_bypass_end);
496
Jens Axboeaed3ea92014-12-22 14:04:42 -0700497void blk_set_queue_dying(struct request_queue *q)
498{
Bart Van Assche1b856082016-08-16 16:48:36 -0700499 spin_lock_irq(q->queue_lock);
500 queue_flag_set(QUEUE_FLAG_DYING, q);
501 spin_unlock_irq(q->queue_lock);
Jens Axboeaed3ea92014-12-22 14:04:42 -0700502
Ming Leid3cfb2a2017-03-27 20:06:58 +0800503 /*
504 * When queue DYING flag is set, we need to block new req
505 * entering queue, so we call blk_freeze_queue_start() to
506 * prevent I/O from crossing blk_queue_enter().
507 */
508 blk_freeze_queue_start(q);
509
Jens Axboeaed3ea92014-12-22 14:04:42 -0700510 if (q->mq_ops)
511 blk_mq_wake_waiters(q);
512 else {
513 struct request_list *rl;
514
Tahsin Erdoganbbfc3c52017-01-31 22:36:50 -0800515 spin_lock_irq(q->queue_lock);
Jens Axboeaed3ea92014-12-22 14:04:42 -0700516 blk_queue_for_each_rl(rl, q) {
517 if (rl->rq_pool) {
518 wake_up(&rl->wait[BLK_RW_SYNC]);
519 wake_up(&rl->wait[BLK_RW_ASYNC]);
520 }
521 }
Tahsin Erdoganbbfc3c52017-01-31 22:36:50 -0800522 spin_unlock_irq(q->queue_lock);
Jens Axboeaed3ea92014-12-22 14:04:42 -0700523 }
524}
525EXPORT_SYMBOL_GPL(blk_set_queue_dying);
526
Tejun Heod7325802012-03-05 13:14:58 -0800527/**
Tejun Heoc9a929d2011-10-19 14:42:16 +0200528 * blk_cleanup_queue - shutdown a request queue
529 * @q: request queue to shutdown
530 *
Bart Van Asschec246e802012-12-06 14:32:01 +0100531 * Mark @q DYING, drain all pending requests, mark @q DEAD, destroy and
532 * put it. All future requests will be failed immediately with -ENODEV.
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500533 */
Jens Axboe6728cb02008-01-31 13:03:55 +0100534void blk_cleanup_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500535{
Tejun Heoc9a929d2011-10-19 14:42:16 +0200536 spinlock_t *lock = q->queue_lock;
Jens Axboee3335de2008-09-18 09:22:54 -0700537
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100538 /* mark @q DYING, no new request or merges will be allowed afterwards */
Al Viro483f4af2006-03-18 18:34:37 -0500539 mutex_lock(&q->sysfs_lock);
Jens Axboeaed3ea92014-12-22 14:04:42 -0700540 blk_set_queue_dying(q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200541 spin_lock_irq(lock);
Tejun Heo6ecf23a2012-03-05 13:14:59 -0800542
Tejun Heo80fd9972012-04-13 14:50:53 -0700543 /*
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100544 * A dying queue is permanently in bypass mode till released. Note
Tejun Heo80fd9972012-04-13 14:50:53 -0700545 * that, unlike blk_queue_bypass_start(), we aren't performing
546 * synchronize_rcu() after entering bypass mode to avoid the delay
547 * as some drivers create and destroy a lot of queues while
548 * probing. This is still safe because blk_release_queue() will be
549 * called only after the queue refcnt drops to zero and nothing,
550 * RCU or not, would be traversing the queue by then.
551 */
Tejun Heo6ecf23a2012-03-05 13:14:59 -0800552 q->bypass_depth++;
553 queue_flag_set(QUEUE_FLAG_BYPASS, q);
554
Tejun Heoc9a929d2011-10-19 14:42:16 +0200555 queue_flag_set(QUEUE_FLAG_NOMERGES, q);
556 queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100557 queue_flag_set(QUEUE_FLAG_DYING, q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200558 spin_unlock_irq(lock);
559 mutex_unlock(&q->sysfs_lock);
560
Bart Van Asschec246e802012-12-06 14:32:01 +0100561 /*
562 * Drain all requests queued before DYING marking. Set DEAD flag to
563 * prevent that q->request_fn() gets invoked after draining finished.
564 */
Dan Williams3ef28e82015-10-21 13:20:12 -0400565 blk_freeze_queue(q);
Bart Van Asschee869b5462017-04-26 13:47:52 -0700566 if (!q->mq_ops) {
567 spin_lock_irq(lock);
Ming Lei43a5e4e2013-12-26 21:31:35 +0800568 __blk_drain_queue(q, true);
Bart Van Asschee869b5462017-04-26 13:47:52 -0700569 } else {
570 blk_mq_debugfs_unregister_mq(q);
571 spin_lock_irq(lock);
572 }
Bart Van Asschec246e802012-12-06 14:32:01 +0100573 queue_flag_set(QUEUE_FLAG_DEAD, q);
Bart Van Assche807592a2012-11-28 13:43:38 +0100574 spin_unlock_irq(lock);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200575
Dan Williams5a48fc12015-10-21 13:20:23 -0400576 /* for synchronous bio-based driver finish in-flight integrity i/o */
577 blk_flush_integrity();
578
Tejun Heoc9a929d2011-10-19 14:42:16 +0200579 /* @q won't process any more request, flush async actions */
Jan Karadc3b17c2017-02-02 15:56:50 +0100580 del_timer_sync(&q->backing_dev_info->laptop_mode_wb_timer);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200581 blk_sync_queue(q);
582
Bart Van Assche45a9c9d2014-12-09 16:57:48 +0100583 if (q->mq_ops)
584 blk_mq_free_queue(q);
Dan Williams3ef28e82015-10-21 13:20:12 -0400585 percpu_ref_exit(&q->q_usage_counter);
Bart Van Assche45a9c9d2014-12-09 16:57:48 +0100586
Asias He5e5cfac2012-05-24 23:28:52 +0800587 spin_lock_irq(lock);
588 if (q->queue_lock != &q->__queue_lock)
589 q->queue_lock = &q->__queue_lock;
590 spin_unlock_irq(lock);
591
Tejun Heoc9a929d2011-10-19 14:42:16 +0200592 /* @q is and will stay empty, shutdown and put */
Al Viro483f4af2006-03-18 18:34:37 -0500593 blk_put_queue(q);
594}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595EXPORT_SYMBOL(blk_cleanup_queue);
596
David Rientjes271508d2015-03-24 16:21:16 -0700597/* Allocate memory local to the request queue */
Christoph Hellwig6d247d72017-01-27 09:51:45 -0700598static void *alloc_request_simple(gfp_t gfp_mask, void *data)
David Rientjes271508d2015-03-24 16:21:16 -0700599{
Christoph Hellwig6d247d72017-01-27 09:51:45 -0700600 struct request_queue *q = data;
601
602 return kmem_cache_alloc_node(request_cachep, gfp_mask, q->node);
David Rientjes271508d2015-03-24 16:21:16 -0700603}
604
Christoph Hellwig6d247d72017-01-27 09:51:45 -0700605static void free_request_simple(void *element, void *data)
David Rientjes271508d2015-03-24 16:21:16 -0700606{
607 kmem_cache_free(request_cachep, element);
608}
609
Christoph Hellwig6d247d72017-01-27 09:51:45 -0700610static void *alloc_request_size(gfp_t gfp_mask, void *data)
611{
612 struct request_queue *q = data;
613 struct request *rq;
614
615 rq = kmalloc_node(sizeof(struct request) + q->cmd_size, gfp_mask,
616 q->node);
617 if (rq && q->init_rq_fn && q->init_rq_fn(q, rq, gfp_mask) < 0) {
618 kfree(rq);
619 rq = NULL;
620 }
621 return rq;
622}
623
624static void free_request_size(void *element, void *data)
625{
626 struct request_queue *q = data;
627
628 if (q->exit_rq_fn)
629 q->exit_rq_fn(q, element);
630 kfree(element);
631}
632
Tejun Heo5b788ce2012-06-04 20:40:59 -0700633int blk_init_rl(struct request_list *rl, struct request_queue *q,
634 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Mike Snitzer1abec4f2010-05-25 13:15:15 -0400636 if (unlikely(rl->rq_pool))
637 return 0;
638
Tejun Heo5b788ce2012-06-04 20:40:59 -0700639 rl->q = q;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200640 rl->count[BLK_RW_SYNC] = rl->count[BLK_RW_ASYNC] = 0;
641 rl->starved[BLK_RW_SYNC] = rl->starved[BLK_RW_ASYNC] = 0;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200642 init_waitqueue_head(&rl->wait[BLK_RW_SYNC]);
643 init_waitqueue_head(&rl->wait[BLK_RW_ASYNC]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Christoph Hellwig6d247d72017-01-27 09:51:45 -0700645 if (q->cmd_size) {
646 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ,
647 alloc_request_size, free_request_size,
648 q, gfp_mask, q->node);
649 } else {
650 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ,
651 alloc_request_simple, free_request_simple,
652 q, gfp_mask, q->node);
653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (!rl->rq_pool)
655 return -ENOMEM;
656
657 return 0;
658}
659
Tejun Heo5b788ce2012-06-04 20:40:59 -0700660void blk_exit_rl(struct request_list *rl)
661{
662 if (rl->rq_pool)
663 mempool_destroy(rl->rq_pool);
664}
665
Jens Axboe165125e2007-07-24 09:28:11 +0200666struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Ezequiel Garciac304a512012-11-10 10:39:44 +0100668 return blk_alloc_queue_node(gfp_mask, NUMA_NO_NODE);
Christoph Lameter19460892005-06-23 00:08:19 -0700669}
670EXPORT_SYMBOL(blk_alloc_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100672int blk_queue_enter(struct request_queue *q, bool nowait)
Dan Williams3ef28e82015-10-21 13:20:12 -0400673{
674 while (true) {
675 int ret;
676
677 if (percpu_ref_tryget_live(&q->q_usage_counter))
678 return 0;
679
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100680 if (nowait)
Dan Williams3ef28e82015-10-21 13:20:12 -0400681 return -EBUSY;
682
Ming Lei5ed61d32017-03-27 20:06:56 +0800683 /*
Ming Lei1671d522017-03-27 20:06:57 +0800684 * read pair of barrier in blk_freeze_queue_start(),
Ming Lei5ed61d32017-03-27 20:06:56 +0800685 * we need to order reading __PERCPU_REF_DEAD flag of
Ming Leid3cfb2a2017-03-27 20:06:58 +0800686 * .q_usage_counter and reading .mq_freeze_depth or
687 * queue dying flag, otherwise the following wait may
688 * never return if the two reads are reordered.
Ming Lei5ed61d32017-03-27 20:06:56 +0800689 */
690 smp_rmb();
691
Dan Williams3ef28e82015-10-21 13:20:12 -0400692 ret = wait_event_interruptible(q->mq_freeze_wq,
693 !atomic_read(&q->mq_freeze_depth) ||
694 blk_queue_dying(q));
695 if (blk_queue_dying(q))
696 return -ENODEV;
697 if (ret)
698 return ret;
699 }
700}
701
702void blk_queue_exit(struct request_queue *q)
703{
704 percpu_ref_put(&q->q_usage_counter);
705}
706
707static void blk_queue_usage_counter_release(struct percpu_ref *ref)
708{
709 struct request_queue *q =
710 container_of(ref, struct request_queue, q_usage_counter);
711
712 wake_up_all(&q->mq_freeze_wq);
713}
714
Christoph Hellwig287922e2015-10-30 20:57:30 +0800715static void blk_rq_timed_out_timer(unsigned long data)
716{
717 struct request_queue *q = (struct request_queue *)data;
718
719 kblockd_schedule_work(&q->timeout_work);
720}
721
Jens Axboe165125e2007-07-24 09:28:11 +0200722struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -0700723{
Jens Axboe165125e2007-07-24 09:28:11 +0200724 struct request_queue *q;
Christoph Lameter19460892005-06-23 00:08:19 -0700725
Jens Axboe8324aa92008-01-29 14:51:59 +0100726 q = kmem_cache_alloc_node(blk_requestq_cachep,
Christoph Lameter94f60302007-07-17 04:03:29 -0700727 gfp_mask | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (!q)
729 return NULL;
730
Dan Carpenter00380a42012-03-23 09:58:54 +0100731 q->id = ida_simple_get(&blk_queue_ida, 0, 0, gfp_mask);
Tejun Heoa73f7302011-12-14 00:33:37 +0100732 if (q->id < 0)
Ming Lei3d2936f2014-05-27 23:35:14 +0800733 goto fail_q;
Tejun Heoa73f7302011-12-14 00:33:37 +0100734
Kent Overstreet54efd502015-04-23 22:37:18 -0700735 q->bio_split = bioset_create(BIO_POOL_SIZE, 0);
736 if (!q->bio_split)
737 goto fail_id;
738
Jan Karad03f6cd2017-02-02 15:56:51 +0100739 q->backing_dev_info = bdi_alloc_node(gfp_mask, node_id);
740 if (!q->backing_dev_info)
Kent Overstreet54efd502015-04-23 22:37:18 -0700741 goto fail_split;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700742
Jens Axboea83b5762017-03-21 17:20:01 -0600743 q->stats = blk_alloc_queue_stats();
744 if (!q->stats)
745 goto fail_stats;
746
Jan Karadc3b17c2017-02-02 15:56:50 +0100747 q->backing_dev_info->ra_pages =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 (VM_MAX_READAHEAD * 1024) / PAGE_SIZE;
Jan Karadc3b17c2017-02-02 15:56:50 +0100749 q->backing_dev_info->capabilities = BDI_CAP_CGROUP_WRITEBACK;
750 q->backing_dev_info->name = "block";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 q->node = node_id;
752
Jan Karadc3b17c2017-02-02 15:56:50 +0100753 setup_timer(&q->backing_dev_info->laptop_mode_wb_timer,
Matthew Garrett31373d02010-04-06 14:25:14 +0200754 laptop_mode_timer_fn, (unsigned long) q);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700755 setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
Tejun Heob855b042012-03-06 21:24:55 +0100756 INIT_LIST_HEAD(&q->queue_head);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700757 INIT_LIST_HEAD(&q->timeout_list);
Tejun Heoa612fdd2011-12-14 00:33:41 +0100758 INIT_LIST_HEAD(&q->icq_list);
Tejun Heo4eef3042012-03-05 13:15:18 -0800759#ifdef CONFIG_BLK_CGROUP
Tejun Heoe8989fa2012-03-05 13:15:20 -0800760 INIT_LIST_HEAD(&q->blkg_list);
Tejun Heo4eef3042012-03-05 13:15:18 -0800761#endif
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500762 INIT_DELAYED_WORK(&q->delay_work, blk_delay_work);
Al Viro483f4af2006-03-18 18:34:37 -0500763
Jens Axboe8324aa92008-01-29 14:51:59 +0100764 kobject_init(&q->kobj, &blk_queue_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Al Viro483f4af2006-03-18 18:34:37 -0500766 mutex_init(&q->sysfs_lock);
Neil Browne7e72bf2008-05-14 16:05:54 -0700767 spin_lock_init(&q->__queue_lock);
Al Viro483f4af2006-03-18 18:34:37 -0500768
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500769 /*
770 * By default initialize queue_lock to internal lock and driver can
771 * override it later if need be.
772 */
773 q->queue_lock = &q->__queue_lock;
774
Tejun Heob82d4b12012-04-13 13:11:31 -0700775 /*
776 * A queue starts its life with bypass turned on to avoid
777 * unnecessary bypass on/off overhead and nasty surprises during
Tejun Heo749fefe2012-09-20 14:08:52 -0700778 * init. The initial bypass will be finished when the queue is
779 * registered by blk_register_queue().
Tejun Heob82d4b12012-04-13 13:11:31 -0700780 */
781 q->bypass_depth = 1;
782 __set_bit(QUEUE_FLAG_BYPASS, &q->queue_flags);
783
Jens Axboe320ae512013-10-24 09:20:05 +0100784 init_waitqueue_head(&q->mq_freeze_wq);
785
Dan Williams3ef28e82015-10-21 13:20:12 -0400786 /*
787 * Init percpu_ref in atomic mode so that it's faster to shutdown.
788 * See blk_register_queue() for details.
789 */
790 if (percpu_ref_init(&q->q_usage_counter,
791 blk_queue_usage_counter_release,
792 PERCPU_REF_INIT_ATOMIC, GFP_KERNEL))
Mikulas Patockafff49962013-10-14 12:11:36 -0400793 goto fail_bdi;
Tejun Heof51b8022012-03-05 13:15:05 -0800794
Dan Williams3ef28e82015-10-21 13:20:12 -0400795 if (blkcg_init_queue(q))
796 goto fail_ref;
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return q;
Tejun Heoa73f7302011-12-14 00:33:37 +0100799
Dan Williams3ef28e82015-10-21 13:20:12 -0400800fail_ref:
801 percpu_ref_exit(&q->q_usage_counter);
Mikulas Patockafff49962013-10-14 12:11:36 -0400802fail_bdi:
Jens Axboea83b5762017-03-21 17:20:01 -0600803 blk_free_queue_stats(q->stats);
804fail_stats:
Jan Karad03f6cd2017-02-02 15:56:51 +0100805 bdi_put(q->backing_dev_info);
Kent Overstreet54efd502015-04-23 22:37:18 -0700806fail_split:
807 bioset_free(q->bio_split);
Tejun Heoa73f7302011-12-14 00:33:37 +0100808fail_id:
809 ida_simple_remove(&blk_queue_ida, q->id);
810fail_q:
811 kmem_cache_free(blk_requestq_cachep, q);
812 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
Christoph Lameter19460892005-06-23 00:08:19 -0700814EXPORT_SYMBOL(blk_alloc_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816/**
817 * blk_init_queue - prepare a request queue for use with a block device
818 * @rfn: The function to be called to process requests that have been
819 * placed on the queue.
820 * @lock: Request queue spin lock
821 *
822 * Description:
823 * If a block device wishes to use the standard request handling procedures,
824 * which sorts requests and coalesces adjacent requests, then it must
825 * call blk_init_queue(). The function @rfn will be called when there
826 * are requests on the queue that need to be processed. If the device
827 * supports plugging, then @rfn may not be called immediately when requests
828 * are available on the queue, but may be called at some time later instead.
829 * Plugged queues are generally unplugged when a buffer belonging to one
830 * of the requests on the queue is needed, or due to memory pressure.
831 *
832 * @rfn is not required, or even expected, to remove all requests off the
833 * queue, but only as many as it can handle at a time. If it does leave
834 * requests on the queue, it is responsible for arranging that the requests
835 * get dealt with eventually.
836 *
837 * The queue spin lock must be held while manipulating the requests on the
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200838 * request queue; this lock will be taken also from interrupt context, so irq
839 * disabling is needed for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 *
Randy Dunlap710027a2008-08-19 20:13:11 +0200841 * Function returns a pointer to the initialized request queue, or %NULL if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 * it didn't succeed.
843 *
844 * Note:
845 * blk_init_queue() must be paired with a blk_cleanup_queue() call
846 * when the block device is deactivated (such as at module unload).
847 **/
Christoph Lameter19460892005-06-23 00:08:19 -0700848
Jens Axboe165125e2007-07-24 09:28:11 +0200849struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Ezequiel Garciac304a512012-11-10 10:39:44 +0100851 return blk_init_queue_node(rfn, lock, NUMA_NO_NODE);
Christoph Lameter19460892005-06-23 00:08:19 -0700852}
853EXPORT_SYMBOL(blk_init_queue);
854
Jens Axboe165125e2007-07-24 09:28:11 +0200855struct request_queue *
Christoph Lameter19460892005-06-23 00:08:19 -0700856blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
857{
Christoph Hellwig5ea708d2017-01-03 14:52:44 +0300858 struct request_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Christoph Hellwig5ea708d2017-01-03 14:52:44 +0300860 q = blk_alloc_queue_node(GFP_KERNEL, node_id);
861 if (!q)
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600862 return NULL;
863
Christoph Hellwig5ea708d2017-01-03 14:52:44 +0300864 q->request_fn = rfn;
865 if (lock)
866 q->queue_lock = lock;
867 if (blk_init_allocated_queue(q) < 0) {
868 blk_cleanup_queue(q);
869 return NULL;
870 }
Christoph Hellwig18741982014-02-10 09:29:00 -0700871
Mike Snitzer7982e902014-03-08 17:20:01 -0700872 return q;
Mike Snitzer01effb02010-05-11 08:57:42 +0200873}
874EXPORT_SYMBOL(blk_init_queue_node);
875
Jens Axboedece1632015-11-05 10:41:16 -0700876static blk_qc_t blk_queue_bio(struct request_queue *q, struct bio *bio);
Mike Snitzer336b7e12015-05-11 14:06:32 -0400877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Christoph Hellwig5ea708d2017-01-03 14:52:44 +0300879int blk_init_allocated_queue(struct request_queue *q)
880{
Christoph Hellwig6d247d72017-01-27 09:51:45 -0700881 q->fq = blk_alloc_flush_queue(q, NUMA_NO_NODE, q->cmd_size);
Ming Leiba483382014-09-25 23:23:44 +0800882 if (!q->fq)
Christoph Hellwig5ea708d2017-01-03 14:52:44 +0300883 return -ENOMEM;
Mike Snitzer7982e902014-03-08 17:20:01 -0700884
Christoph Hellwig6d247d72017-01-27 09:51:45 -0700885 if (q->init_rq_fn && q->init_rq_fn(q, q->fq->flush_rq, GFP_KERNEL))
886 goto out_free_flush_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 if (blk_init_rl(&q->root_rl, q, GFP_KERNEL))
Christoph Hellwig6d247d72017-01-27 09:51:45 -0700889 goto out_exit_flush_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Christoph Hellwig287922e2015-10-30 20:57:30 +0800891 INIT_WORK(&q->timeout_work, blk_timeout_work);
Tejun Heo60ea8222012-09-20 14:09:30 -0700892 q->queue_flags |= QUEUE_FLAG_DEFAULT;
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500893
Jens Axboef3b144a2009-03-06 08:48:33 +0100894 /*
895 * This also sets hw/phys segments, boundary and size
896 */
Jens Axboec20e8de2011-09-12 12:03:37 +0200897 blk_queue_make_request(q, blk_queue_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Alan Stern44ec9542007-02-20 11:01:57 -0500899 q->sg_reserved_size = INT_MAX;
900
Tomoki Sekiyamaeb1c1602013-10-15 16:42:16 -0600901 /* Protect q->elevator from elevator_change */
902 mutex_lock(&q->sysfs_lock);
903
Tejun Heob82d4b12012-04-13 13:11:31 -0700904 /* init elevator */
Tomoki Sekiyamaeb1c1602013-10-15 16:42:16 -0600905 if (elevator_init(q, NULL)) {
906 mutex_unlock(&q->sysfs_lock);
Christoph Hellwig6d247d72017-01-27 09:51:45 -0700907 goto out_exit_flush_rq;
Tomoki Sekiyamaeb1c1602013-10-15 16:42:16 -0600908 }
909
910 mutex_unlock(&q->sysfs_lock);
Christoph Hellwig5ea708d2017-01-03 14:52:44 +0300911 return 0;
Tomoki Sekiyamaeb1c1602013-10-15 16:42:16 -0600912
Christoph Hellwig6d247d72017-01-27 09:51:45 -0700913out_exit_flush_rq:
914 if (q->exit_rq_fn)
915 q->exit_rq_fn(q, q->fq->flush_rq);
916out_free_flush_queue:
Ming Leiba483382014-09-25 23:23:44 +0800917 blk_free_flush_queue(q->fq);
Christoph Hellwig5ea708d2017-01-03 14:52:44 +0300918 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
Mike Snitzer51514122011-11-23 10:59:13 +0100920EXPORT_SYMBOL(blk_init_allocated_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Tejun Heo09ac46c2011-12-14 00:33:38 +0100922bool blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100924 if (likely(!blk_queue_dying(q))) {
Tejun Heo09ac46c2011-12-14 00:33:38 +0100925 __blk_get_queue(q);
926 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
928
Tejun Heo09ac46c2011-12-14 00:33:38 +0100929 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
Jens Axboed86e0e82011-05-27 07:44:43 +0200931EXPORT_SYMBOL(blk_get_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Tejun Heo5b788ce2012-06-04 20:40:59 -0700933static inline void blk_free_request(struct request_list *rl, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Christoph Hellwige8064022016-10-20 15:12:13 +0200935 if (rq->rq_flags & RQF_ELVPRIV) {
Tejun Heo5b788ce2012-06-04 20:40:59 -0700936 elv_put_request(rl->q, rq);
Tejun Heof1f8cc92011-12-14 00:33:42 +0100937 if (rq->elv.icq)
Tejun Heo11a31222012-02-07 07:51:30 +0100938 put_io_context(rq->elv.icq->ioc);
Tejun Heof1f8cc92011-12-14 00:33:42 +0100939 }
940
Tejun Heo5b788ce2012-06-04 20:40:59 -0700941 mempool_free(rq, rl->rq_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944/*
945 * ioc_batching returns true if the ioc is a valid batching request and
946 * should be given priority access to a request.
947 */
Jens Axboe165125e2007-07-24 09:28:11 +0200948static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
950 if (!ioc)
951 return 0;
952
953 /*
954 * Make sure the process is able to allocate at least 1 request
955 * even if the batch times out, otherwise we could theoretically
956 * lose wakeups.
957 */
958 return ioc->nr_batch_requests == q->nr_batching ||
959 (ioc->nr_batch_requests > 0
960 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
961}
962
963/*
964 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
965 * will cause the process to be a "batcher" on all queues in the system. This
966 * is the behaviour we want though - once it gets a wakeup it should be given
967 * a nice run.
968 */
Jens Axboe165125e2007-07-24 09:28:11 +0200969static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
971 if (!ioc || ioc_batching(q, ioc))
972 return;
973
974 ioc->nr_batch_requests = q->nr_batching;
975 ioc->last_waited = jiffies;
976}
977
Tejun Heo5b788ce2012-06-04 20:40:59 -0700978static void __freed_request(struct request_list *rl, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
Tejun Heo5b788ce2012-06-04 20:40:59 -0700980 struct request_queue *q = rl->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Tejun Heod40f75a2015-05-22 17:13:42 -0400982 if (rl->count[sync] < queue_congestion_off_threshold(q))
983 blk_clear_congested(rl, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Jens Axboe1faa16d2009-04-06 14:48:01 +0200985 if (rl->count[sync] + 1 <= q->nr_requests) {
986 if (waitqueue_active(&rl->wait[sync]))
987 wake_up(&rl->wait[sync]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Tejun Heo5b788ce2012-06-04 20:40:59 -0700989 blk_clear_rl_full(rl, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
991}
992
993/*
994 * A request has just been released. Account for it, update the full and
995 * congestion status, wake up any waiters. Called under q->queue_lock.
996 */
Christoph Hellwige8064022016-10-20 15:12:13 +0200997static void freed_request(struct request_list *rl, bool sync,
998 req_flags_t rq_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
Tejun Heo5b788ce2012-06-04 20:40:59 -07001000 struct request_queue *q = rl->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Tejun Heo8a5ecdd2012-06-04 20:40:58 -07001002 q->nr_rqs[sync]--;
Jens Axboe1faa16d2009-04-06 14:48:01 +02001003 rl->count[sync]--;
Christoph Hellwige8064022016-10-20 15:12:13 +02001004 if (rq_flags & RQF_ELVPRIV)
Tejun Heo8a5ecdd2012-06-04 20:40:58 -07001005 q->nr_rqs_elvpriv--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Tejun Heo5b788ce2012-06-04 20:40:59 -07001007 __freed_request(rl, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Jens Axboe1faa16d2009-04-06 14:48:01 +02001009 if (unlikely(rl->starved[sync ^ 1]))
Tejun Heo5b788ce2012-06-04 20:40:59 -07001010 __freed_request(rl, sync ^ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
1012
Jens Axboee3a2b3f2014-05-20 11:49:02 -06001013int blk_update_nr_requests(struct request_queue *q, unsigned int nr)
1014{
1015 struct request_list *rl;
Tejun Heod40f75a2015-05-22 17:13:42 -04001016 int on_thresh, off_thresh;
Jens Axboee3a2b3f2014-05-20 11:49:02 -06001017
1018 spin_lock_irq(q->queue_lock);
1019 q->nr_requests = nr;
1020 blk_queue_congestion_threshold(q);
Tejun Heod40f75a2015-05-22 17:13:42 -04001021 on_thresh = queue_congestion_on_threshold(q);
1022 off_thresh = queue_congestion_off_threshold(q);
Jens Axboee3a2b3f2014-05-20 11:49:02 -06001023
1024 blk_queue_for_each_rl(rl, q) {
Tejun Heod40f75a2015-05-22 17:13:42 -04001025 if (rl->count[BLK_RW_SYNC] >= on_thresh)
1026 blk_set_congested(rl, BLK_RW_SYNC);
1027 else if (rl->count[BLK_RW_SYNC] < off_thresh)
1028 blk_clear_congested(rl, BLK_RW_SYNC);
1029
1030 if (rl->count[BLK_RW_ASYNC] >= on_thresh)
1031 blk_set_congested(rl, BLK_RW_ASYNC);
1032 else if (rl->count[BLK_RW_ASYNC] < off_thresh)
1033 blk_clear_congested(rl, BLK_RW_ASYNC);
1034
Jens Axboee3a2b3f2014-05-20 11:49:02 -06001035 if (rl->count[BLK_RW_SYNC] >= q->nr_requests) {
1036 blk_set_rl_full(rl, BLK_RW_SYNC);
1037 } else {
1038 blk_clear_rl_full(rl, BLK_RW_SYNC);
1039 wake_up(&rl->wait[BLK_RW_SYNC]);
1040 }
1041
1042 if (rl->count[BLK_RW_ASYNC] >= q->nr_requests) {
1043 blk_set_rl_full(rl, BLK_RW_ASYNC);
1044 } else {
1045 blk_clear_rl_full(rl, BLK_RW_ASYNC);
1046 wake_up(&rl->wait[BLK_RW_ASYNC]);
1047 }
1048 }
1049
1050 spin_unlock_irq(q->queue_lock);
1051 return 0;
1052}
1053
Tejun Heoda8303c2011-10-19 14:33:05 +02001054/**
Tejun Heoa06e05e2012-06-04 20:40:55 -07001055 * __get_request - get a free request
Tejun Heo5b788ce2012-06-04 20:40:59 -07001056 * @rl: request list to allocate from
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001057 * @op: operation and flags
Tejun Heoda8303c2011-10-19 14:33:05 +02001058 * @bio: bio to allocate request for (can be %NULL)
1059 * @gfp_mask: allocation mask
1060 *
1061 * Get a free request from @q. This function may fail under memory
1062 * pressure or if @q is dead.
1063 *
Masanari Iidada3dae52014-09-09 01:27:23 +09001064 * Must be called with @q->queue_lock held and,
Joe Lawrencea492f072014-08-28 08:15:21 -06001065 * Returns ERR_PTR on failure, with @q->queue_lock held.
1066 * Returns request pointer on success, with @q->queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 */
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001068static struct request *__get_request(struct request_list *rl, unsigned int op,
1069 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
Tejun Heo5b788ce2012-06-04 20:40:59 -07001071 struct request_queue *q = rl->q;
Tejun Heob6792812012-03-05 13:15:23 -08001072 struct request *rq;
Tejun Heo7f4b35d2012-06-04 20:40:56 -07001073 struct elevator_type *et = q->elevator->type;
1074 struct io_context *ioc = rq_ioc(bio);
Tejun Heof1f8cc92011-12-14 00:33:42 +01001075 struct io_cq *icq = NULL;
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001076 const bool is_sync = op_is_sync(op);
Tejun Heo75eb6c32011-10-19 14:31:22 +02001077 int may_queue;
Christoph Hellwige8064022016-10-20 15:12:13 +02001078 req_flags_t rq_flags = RQF_ALLOCED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Bart Van Assche3f3299d2012-11-28 13:42:38 +01001080 if (unlikely(blk_queue_dying(q)))
Joe Lawrencea492f072014-08-28 08:15:21 -06001081 return ERR_PTR(-ENODEV);
Tejun Heoda8303c2011-10-19 14:33:05 +02001082
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001083 may_queue = elv_may_queue(q, op);
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001084 if (may_queue == ELV_MQUEUE_NO)
1085 goto rq_starved;
1086
Jens Axboe1faa16d2009-04-06 14:48:01 +02001087 if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) {
1088 if (rl->count[is_sync]+1 >= q->nr_requests) {
Tejun Heof2dbd762011-12-14 00:33:40 +01001089 /*
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001090 * The queue will fill after this allocation, so set
1091 * it as full, and mark this process as "batching".
1092 * This process will be allowed to complete a batch of
1093 * requests, others will be blocked.
1094 */
Tejun Heo5b788ce2012-06-04 20:40:59 -07001095 if (!blk_rl_full(rl, is_sync)) {
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001096 ioc_set_batching(q, ioc);
Tejun Heo5b788ce2012-06-04 20:40:59 -07001097 blk_set_rl_full(rl, is_sync);
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001098 } else {
1099 if (may_queue != ELV_MQUEUE_MUST
1100 && !ioc_batching(q, ioc)) {
1101 /*
1102 * The queue is full and the allocating
1103 * process is not a "batcher", and not
1104 * exempted by the IO scheduler
1105 */
Joe Lawrencea492f072014-08-28 08:15:21 -06001106 return ERR_PTR(-ENOMEM);
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001107 }
1108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
Tejun Heod40f75a2015-05-22 17:13:42 -04001110 blk_set_congested(rl, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 }
1112
Jens Axboe082cf692005-06-28 16:35:11 +02001113 /*
1114 * Only allow batching queuers to allocate up to 50% over the defined
1115 * limit of requests, otherwise we could have thousands of requests
1116 * allocated with any setting of ->nr_requests
1117 */
Jens Axboe1faa16d2009-04-06 14:48:01 +02001118 if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
Joe Lawrencea492f072014-08-28 08:15:21 -06001119 return ERR_PTR(-ENOMEM);
Hugh Dickinsfd782a42005-06-29 15:15:40 +01001120
Tejun Heo8a5ecdd2012-06-04 20:40:58 -07001121 q->nr_rqs[is_sync]++;
Jens Axboe1faa16d2009-04-06 14:48:01 +02001122 rl->count[is_sync]++;
1123 rl->starved[is_sync] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +02001124
Tejun Heof1f8cc92011-12-14 00:33:42 +01001125 /*
1126 * Decide whether the new request will be managed by elevator. If
Christoph Hellwige8064022016-10-20 15:12:13 +02001127 * so, mark @rq_flags and increment elvpriv. Non-zero elvpriv will
Tejun Heof1f8cc92011-12-14 00:33:42 +01001128 * prevent the current elevator from being destroyed until the new
1129 * request is freed. This guarantees icq's won't be destroyed and
1130 * makes creating new ones safe.
1131 *
Christoph Hellwige6f7f932017-01-25 11:17:11 +01001132 * Flush requests do not use the elevator so skip initialization.
1133 * This allows a request to share the flush and elevator data.
1134 *
Tejun Heof1f8cc92011-12-14 00:33:42 +01001135 * Also, lookup icq while holding queue_lock. If it doesn't exist,
1136 * it will be created after releasing queue_lock.
1137 */
Christoph Hellwige6f7f932017-01-25 11:17:11 +01001138 if (!op_is_flush(op) && !blk_queue_bypass(q)) {
Christoph Hellwige8064022016-10-20 15:12:13 +02001139 rq_flags |= RQF_ELVPRIV;
Tejun Heo8a5ecdd2012-06-04 20:40:58 -07001140 q->nr_rqs_elvpriv++;
Tejun Heof1f8cc92011-12-14 00:33:42 +01001141 if (et->icq_cache && ioc)
1142 icq = ioc_lookup_icq(ioc, q);
Mike Snitzer9d5a4e92011-02-11 11:05:46 +01001143 }
Tejun Heocb98fc82005-10-28 08:29:39 +02001144
Jens Axboef253b862010-10-24 22:06:02 +02001145 if (blk_queue_io_stat(q))
Christoph Hellwige8064022016-10-20 15:12:13 +02001146 rq_flags |= RQF_IO_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 spin_unlock_irq(q->queue_lock);
1148
Tejun Heo29e2b092012-04-19 16:29:21 -07001149 /* allocate and init request */
Tejun Heo5b788ce2012-06-04 20:40:59 -07001150 rq = mempool_alloc(rl->rq_pool, gfp_mask);
Tejun Heo29e2b092012-04-19 16:29:21 -07001151 if (!rq)
Tejun Heob6792812012-03-05 13:15:23 -08001152 goto fail_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Tejun Heo29e2b092012-04-19 16:29:21 -07001154 blk_rq_init(q, rq);
Tejun Heoa0516612012-06-26 15:05:44 -07001155 blk_rq_set_rl(rq, rl);
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001156 rq->cmd_flags = op;
Christoph Hellwige8064022016-10-20 15:12:13 +02001157 rq->rq_flags = rq_flags;
Tejun Heo29e2b092012-04-19 16:29:21 -07001158
Tejun Heoaaf7c682012-04-19 16:29:22 -07001159 /* init elvpriv */
Christoph Hellwige8064022016-10-20 15:12:13 +02001160 if (rq_flags & RQF_ELVPRIV) {
Tejun Heoaaf7c682012-04-19 16:29:22 -07001161 if (unlikely(et->icq_cache && !icq)) {
Tejun Heo7f4b35d2012-06-04 20:40:56 -07001162 if (ioc)
1163 icq = ioc_create_icq(ioc, q, gfp_mask);
Tejun Heoaaf7c682012-04-19 16:29:22 -07001164 if (!icq)
1165 goto fail_elvpriv;
Tejun Heo29e2b092012-04-19 16:29:21 -07001166 }
Tejun Heoaaf7c682012-04-19 16:29:22 -07001167
1168 rq->elv.icq = icq;
1169 if (unlikely(elv_set_request(q, rq, bio, gfp_mask)))
1170 goto fail_elvpriv;
1171
1172 /* @rq->elv.icq holds io_context until @rq is freed */
Tejun Heo29e2b092012-04-19 16:29:21 -07001173 if (icq)
1174 get_io_context(icq->ioc);
1175 }
Tejun Heoaaf7c682012-04-19 16:29:22 -07001176out:
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001177 /*
1178 * ioc may be NULL here, and ioc_batching will be false. That's
1179 * OK, if the queue is under the request limit then requests need
1180 * not count toward the nr_batch_requests limit. There will always
1181 * be some limit enforced by BLK_BATCH_TIME.
1182 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (ioc_batching(q, ioc))
1184 ioc->nr_batch_requests--;
Jens Axboe6728cb02008-01-31 13:03:55 +01001185
Mike Christiee6a40b02016-06-05 14:32:11 -05001186 trace_block_getrq(q, bio, op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return rq;
Tejun Heob6792812012-03-05 13:15:23 -08001188
Tejun Heoaaf7c682012-04-19 16:29:22 -07001189fail_elvpriv:
1190 /*
1191 * elvpriv init failed. ioc, icq and elvpriv aren't mempool backed
1192 * and may fail indefinitely under memory pressure and thus
1193 * shouldn't stall IO. Treat this request as !elvpriv. This will
1194 * disturb iosched and blkcg but weird is bettern than dead.
1195 */
Robert Elliott7b2b10e2014-08-27 10:50:36 -05001196 printk_ratelimited(KERN_WARNING "%s: dev %s: request aux data allocation failed, iosched may be disturbed\n",
Jan Karadc3b17c2017-02-02 15:56:50 +01001197 __func__, dev_name(q->backing_dev_info->dev));
Tejun Heoaaf7c682012-04-19 16:29:22 -07001198
Christoph Hellwige8064022016-10-20 15:12:13 +02001199 rq->rq_flags &= ~RQF_ELVPRIV;
Tejun Heoaaf7c682012-04-19 16:29:22 -07001200 rq->elv.icq = NULL;
1201
1202 spin_lock_irq(q->queue_lock);
Tejun Heo8a5ecdd2012-06-04 20:40:58 -07001203 q->nr_rqs_elvpriv--;
Tejun Heoaaf7c682012-04-19 16:29:22 -07001204 spin_unlock_irq(q->queue_lock);
1205 goto out;
1206
Tejun Heob6792812012-03-05 13:15:23 -08001207fail_alloc:
1208 /*
1209 * Allocation failed presumably due to memory. Undo anything we
1210 * might have messed up.
1211 *
1212 * Allocating task should really be put onto the front of the wait
1213 * queue, but this is pretty rare.
1214 */
1215 spin_lock_irq(q->queue_lock);
Christoph Hellwige8064022016-10-20 15:12:13 +02001216 freed_request(rl, is_sync, rq_flags);
Tejun Heob6792812012-03-05 13:15:23 -08001217
1218 /*
1219 * in the very unlikely event that allocation failed and no
1220 * requests for this direction was pending, mark us starved so that
1221 * freeing of a request in the other direction will notice
1222 * us. another possible fix would be to split the rq mempool into
1223 * READ and WRITE
1224 */
1225rq_starved:
1226 if (unlikely(rl->count[is_sync] == 0))
1227 rl->starved[is_sync] = 1;
Joe Lawrencea492f072014-08-28 08:15:21 -06001228 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229}
1230
Tejun Heoda8303c2011-10-19 14:33:05 +02001231/**
Tejun Heoa06e05e2012-06-04 20:40:55 -07001232 * get_request - get a free request
Tejun Heoda8303c2011-10-19 14:33:05 +02001233 * @q: request_queue to allocate request from
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001234 * @op: operation and flags
Tejun Heoda8303c2011-10-19 14:33:05 +02001235 * @bio: bio to allocate request for (can be %NULL)
Tejun Heoa06e05e2012-06-04 20:40:55 -07001236 * @gfp_mask: allocation mask
Nick Piggind6344532005-06-28 20:45:14 -07001237 *
Mel Gormand0164ad2015-11-06 16:28:21 -08001238 * Get a free request from @q. If %__GFP_DIRECT_RECLAIM is set in @gfp_mask,
1239 * this function keeps retrying under memory pressure and fails iff @q is dead.
Tejun Heoda8303c2011-10-19 14:33:05 +02001240 *
Masanari Iidada3dae52014-09-09 01:27:23 +09001241 * Must be called with @q->queue_lock held and,
Joe Lawrencea492f072014-08-28 08:15:21 -06001242 * Returns ERR_PTR on failure, with @q->queue_lock held.
1243 * Returns request pointer on success, with @q->queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 */
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001245static struct request *get_request(struct request_queue *q, unsigned int op,
1246 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001248 const bool is_sync = op_is_sync(op);
Tejun Heoa06e05e2012-06-04 20:40:55 -07001249 DEFINE_WAIT(wait);
Tejun Heoa0516612012-06-26 15:05:44 -07001250 struct request_list *rl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 struct request *rq;
Tejun Heoa0516612012-06-26 15:05:44 -07001252
1253 rl = blk_get_rl(q, bio); /* transferred to @rq on success */
Tejun Heoa06e05e2012-06-04 20:40:55 -07001254retry:
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001255 rq = __get_request(rl, op, bio, gfp_mask);
Joe Lawrencea492f072014-08-28 08:15:21 -06001256 if (!IS_ERR(rq))
Tejun Heoa06e05e2012-06-04 20:40:55 -07001257 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Mel Gormand0164ad2015-11-06 16:28:21 -08001259 if (!gfpflags_allow_blocking(gfp_mask) || unlikely(blk_queue_dying(q))) {
Tejun Heoa0516612012-06-26 15:05:44 -07001260 blk_put_rl(rl);
Joe Lawrencea492f072014-08-28 08:15:21 -06001261 return rq;
Tejun Heoa0516612012-06-26 15:05:44 -07001262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Tejun Heoa06e05e2012-06-04 20:40:55 -07001264 /* wait on @rl and retry */
1265 prepare_to_wait_exclusive(&rl->wait[is_sync], &wait,
1266 TASK_UNINTERRUPTIBLE);
Tejun Heoda8303c2011-10-19 14:33:05 +02001267
Mike Christiee6a40b02016-06-05 14:32:11 -05001268 trace_block_sleeprq(q, bio, op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Tejun Heoa06e05e2012-06-04 20:40:55 -07001270 spin_unlock_irq(q->queue_lock);
1271 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Tejun Heoa06e05e2012-06-04 20:40:55 -07001273 /*
1274 * After sleeping, we become a "batching" process and will be able
1275 * to allocate at least one request, and up to a big batch of them
1276 * for a small period time. See ioc_batching, ioc_set_batching
1277 */
Tejun Heoa06e05e2012-06-04 20:40:55 -07001278 ioc_set_batching(q, current->io_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Tejun Heoa06e05e2012-06-04 20:40:55 -07001280 spin_lock_irq(q->queue_lock);
1281 finish_wait(&rl->wait[is_sync], &wait);
Jens Axboe2056a782006-03-23 20:00:26 +01001282
Tejun Heoa06e05e2012-06-04 20:40:55 -07001283 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284}
1285
Jens Axboe320ae512013-10-24 09:20:05 +01001286static struct request *blk_old_get_request(struct request_queue *q, int rw,
1287 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
1289 struct request *rq;
1290
Tejun Heo7f4b35d2012-06-04 20:40:56 -07001291 /* create ioc upfront */
1292 create_io_context(gfp_mask, q->node);
1293
Nick Piggind6344532005-06-28 20:45:14 -07001294 spin_lock_irq(q->queue_lock);
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001295 rq = get_request(q, rw, NULL, gfp_mask);
Christoph Hellwig0c4de0f2016-07-19 11:31:50 +02001296 if (IS_ERR(rq)) {
Tejun Heoda8303c2011-10-19 14:33:05 +02001297 spin_unlock_irq(q->queue_lock);
Christoph Hellwig0c4de0f2016-07-19 11:31:50 +02001298 return rq;
1299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Christoph Hellwig0c4de0f2016-07-19 11:31:50 +02001301 /* q->queue_lock is unlocked at this point */
1302 rq->__data_len = 0;
1303 rq->__sector = (sector_t) -1;
1304 rq->bio = rq->biotail = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 return rq;
1306}
Jens Axboe320ae512013-10-24 09:20:05 +01001307
1308struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
1309{
1310 if (q->mq_ops)
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +01001311 return blk_mq_alloc_request(q, rw,
1312 (gfp_mask & __GFP_DIRECT_RECLAIM) ?
1313 0 : BLK_MQ_REQ_NOWAIT);
Jens Axboe320ae512013-10-24 09:20:05 +01001314 else
1315 return blk_old_get_request(q, rw, gfp_mask);
1316}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317EXPORT_SYMBOL(blk_get_request);
1318
1319/**
1320 * blk_requeue_request - put a request back on queue
1321 * @q: request queue where request should be inserted
1322 * @rq: request to be inserted
1323 *
1324 * Description:
1325 * Drivers often keep queueing requests until the hardware cannot accept
1326 * more, when that condition happens we need to put the request back
1327 * on the queue. Must be called with queue lock held.
1328 */
Jens Axboe165125e2007-07-24 09:28:11 +02001329void blk_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
Jens Axboe242f9dc2008-09-14 05:55:09 -07001331 blk_delete_timer(rq);
1332 blk_clear_rq_complete(rq);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001333 trace_block_rq_requeue(q, rq);
Jens Axboe87760e52016-11-09 12:38:14 -07001334 wbt_requeue(q->rq_wb, &rq->issue_stat);
Jens Axboe2056a782006-03-23 20:00:26 +01001335
Christoph Hellwige8064022016-10-20 15:12:13 +02001336 if (rq->rq_flags & RQF_QUEUED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 blk_queue_end_tag(q, rq);
1338
James Bottomleyba396a62009-05-27 14:17:08 +02001339 BUG_ON(blk_queued_rq(rq));
1340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 elv_requeue_request(q, rq);
1342}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343EXPORT_SYMBOL(blk_requeue_request);
1344
Jens Axboe73c10102011-03-08 13:19:51 +01001345static void add_acct_request(struct request_queue *q, struct request *rq,
1346 int where)
1347{
Jens Axboe320ae512013-10-24 09:20:05 +01001348 blk_account_io_start(rq, true);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001349 __elv_add_request(q, rq, where);
Jens Axboe73c10102011-03-08 13:19:51 +01001350}
1351
Tejun Heo074a7ac2008-08-25 19:56:14 +09001352static void part_round_stats_single(int cpu, struct hd_struct *part,
1353 unsigned long now)
1354{
Jens Axboe7276d022014-05-09 15:48:23 -06001355 int inflight;
1356
Tejun Heo074a7ac2008-08-25 19:56:14 +09001357 if (now == part->stamp)
1358 return;
1359
Jens Axboe7276d022014-05-09 15:48:23 -06001360 inflight = part_in_flight(part);
1361 if (inflight) {
Tejun Heo074a7ac2008-08-25 19:56:14 +09001362 __part_stat_add(cpu, part, time_in_queue,
Jens Axboe7276d022014-05-09 15:48:23 -06001363 inflight * (now - part->stamp));
Tejun Heo074a7ac2008-08-25 19:56:14 +09001364 __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
1365 }
1366 part->stamp = now;
1367}
1368
1369/**
Randy Dunlap496aa8a2008-10-16 07:46:23 +02001370 * part_round_stats() - Round off the performance stats on a struct disk_stats.
1371 * @cpu: cpu number for stats access
1372 * @part: target partition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 *
1374 * The average IO queue length and utilisation statistics are maintained
1375 * by observing the current state of the queue length and the amount of
1376 * time it has been in this state for.
1377 *
1378 * Normally, that accounting is done on IO completion, but that can result
1379 * in more than a second's worth of IO being accounted for within any one
1380 * second, leading to >100% utilisation. To deal with that, we call this
1381 * function to do a round-off before returning the results when reading
1382 * /proc/diskstats. This accounts immediately for all queue usage up to
1383 * the current jiffies and restarts the counters again.
1384 */
Tejun Heoc9959052008-08-25 19:47:21 +09001385void part_round_stats(int cpu, struct hd_struct *part)
Jerome Marchand6f2576af2008-02-08 11:04:35 +01001386{
1387 unsigned long now = jiffies;
1388
Tejun Heo074a7ac2008-08-25 19:56:14 +09001389 if (part->partno)
1390 part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
1391 part_round_stats_single(cpu, part, now);
Jerome Marchand6f2576af2008-02-08 11:04:35 +01001392}
Tejun Heo074a7ac2008-08-25 19:56:14 +09001393EXPORT_SYMBOL_GPL(part_round_stats);
Jerome Marchand6f2576af2008-02-08 11:04:35 +01001394
Rafael J. Wysocki47fafbc2014-12-04 01:00:23 +01001395#ifdef CONFIG_PM
Lin Mingc8158812013-03-23 11:42:27 +08001396static void blk_pm_put_request(struct request *rq)
1397{
Christoph Hellwige8064022016-10-20 15:12:13 +02001398 if (rq->q->dev && !(rq->rq_flags & RQF_PM) && !--rq->q->nr_pending)
Lin Mingc8158812013-03-23 11:42:27 +08001399 pm_runtime_mark_last_busy(rq->q->dev);
1400}
1401#else
1402static inline void blk_pm_put_request(struct request *rq) {}
1403#endif
1404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405/*
1406 * queue lock must be held
1407 */
Jens Axboe165125e2007-07-24 09:28:11 +02001408void __blk_put_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
Christoph Hellwige8064022016-10-20 15:12:13 +02001410 req_flags_t rq_flags = req->rq_flags;
1411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 if (unlikely(!q))
1413 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Christoph Hellwig6f5ba582014-02-07 10:22:37 -08001415 if (q->mq_ops) {
1416 blk_mq_free_request(req);
1417 return;
1418 }
1419
Lin Mingc8158812013-03-23 11:42:27 +08001420 blk_pm_put_request(req);
1421
Tejun Heo8922e162005-10-20 16:23:44 +02001422 elv_completed_request(q, req);
1423
Boaz Harrosh1cd96c22009-03-24 12:35:07 +01001424 /* this is a bio leak */
1425 WARN_ON(req->bio != NULL);
1426
Jens Axboe87760e52016-11-09 12:38:14 -07001427 wbt_done(q->rq_wb, &req->issue_stat);
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 /*
1430 * Request may not have originated from ll_rw_blk. if not,
1431 * it didn't come out of our reserved rq pools
1432 */
Christoph Hellwige8064022016-10-20 15:12:13 +02001433 if (rq_flags & RQF_ALLOCED) {
Tejun Heoa0516612012-06-26 15:05:44 -07001434 struct request_list *rl = blk_rq_rl(req);
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001435 bool sync = op_is_sync(req->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 BUG_ON(!list_empty(&req->queuelist));
Jens Axboe360f92c2014-04-09 20:27:01 -06001438 BUG_ON(ELV_ON_HASH(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Tejun Heoa0516612012-06-26 15:05:44 -07001440 blk_free_request(rl, req);
Christoph Hellwige8064022016-10-20 15:12:13 +02001441 freed_request(rl, sync, rq_flags);
Tejun Heoa0516612012-06-26 15:05:44 -07001442 blk_put_rl(rl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 }
1444}
Mike Christie6e39b692005-11-11 05:30:24 -06001445EXPORT_SYMBOL_GPL(__blk_put_request);
1446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447void blk_put_request(struct request *req)
1448{
Jens Axboe165125e2007-07-24 09:28:11 +02001449 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Jens Axboe320ae512013-10-24 09:20:05 +01001451 if (q->mq_ops)
1452 blk_mq_free_request(req);
1453 else {
1454 unsigned long flags;
1455
1456 spin_lock_irqsave(q->queue_lock, flags);
1457 __blk_put_request(q, req);
1458 spin_unlock_irqrestore(q->queue_lock, flags);
1459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461EXPORT_SYMBOL(blk_put_request);
1462
Jens Axboe320ae512013-10-24 09:20:05 +01001463bool bio_attempt_back_merge(struct request_queue *q, struct request *req,
1464 struct bio *bio)
Jens Axboe73c10102011-03-08 13:19:51 +01001465{
Jens Axboe1eff9d32016-08-05 15:35:16 -06001466 const int ff = bio->bi_opf & REQ_FAILFAST_MASK;
Jens Axboe73c10102011-03-08 13:19:51 +01001467
Jens Axboe73c10102011-03-08 13:19:51 +01001468 if (!ll_back_merge_fn(q, req, bio))
1469 return false;
1470
Tejun Heo8c1cf6b2013-01-11 13:06:34 -08001471 trace_block_bio_backmerge(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001472
1473 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1474 blk_rq_set_mixed_merge(req);
1475
1476 req->biotail->bi_next = bio;
1477 req->biotail = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001478 req->__data_len += bio->bi_iter.bi_size;
Jens Axboe73c10102011-03-08 13:19:51 +01001479 req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
1480
Jens Axboe320ae512013-10-24 09:20:05 +01001481 blk_account_io_start(req, false);
Jens Axboe73c10102011-03-08 13:19:51 +01001482 return true;
1483}
1484
Jens Axboe320ae512013-10-24 09:20:05 +01001485bool bio_attempt_front_merge(struct request_queue *q, struct request *req,
1486 struct bio *bio)
Jens Axboe73c10102011-03-08 13:19:51 +01001487{
Jens Axboe1eff9d32016-08-05 15:35:16 -06001488 const int ff = bio->bi_opf & REQ_FAILFAST_MASK;
Jens Axboe73c10102011-03-08 13:19:51 +01001489
Jens Axboe73c10102011-03-08 13:19:51 +01001490 if (!ll_front_merge_fn(q, req, bio))
1491 return false;
1492
Tejun Heo8c1cf6b2013-01-11 13:06:34 -08001493 trace_block_bio_frontmerge(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001494
1495 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1496 blk_rq_set_mixed_merge(req);
1497
Jens Axboe73c10102011-03-08 13:19:51 +01001498 bio->bi_next = req->bio;
1499 req->bio = bio;
1500
Kent Overstreet4f024f32013-10-11 15:44:27 -07001501 req->__sector = bio->bi_iter.bi_sector;
1502 req->__data_len += bio->bi_iter.bi_size;
Jens Axboe73c10102011-03-08 13:19:51 +01001503 req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
1504
Jens Axboe320ae512013-10-24 09:20:05 +01001505 blk_account_io_start(req, false);
Jens Axboe73c10102011-03-08 13:19:51 +01001506 return true;
1507}
1508
Christoph Hellwig1e739732017-02-08 14:46:49 +01001509bool bio_attempt_discard_merge(struct request_queue *q, struct request *req,
1510 struct bio *bio)
1511{
1512 unsigned short segments = blk_rq_nr_discard_segments(req);
1513
1514 if (segments >= queue_max_discard_segments(q))
1515 goto no_merge;
1516 if (blk_rq_sectors(req) + bio_sectors(bio) >
1517 blk_rq_get_max_sectors(req, blk_rq_pos(req)))
1518 goto no_merge;
1519
1520 req->biotail->bi_next = bio;
1521 req->biotail = bio;
1522 req->__data_len += bio->bi_iter.bi_size;
1523 req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
1524 req->nr_phys_segments = segments + 1;
1525
1526 blk_account_io_start(req, false);
1527 return true;
1528no_merge:
1529 req_set_nomerge(q, req);
1530 return false;
1531}
1532
Tejun Heobd87b582011-10-19 14:33:08 +02001533/**
Jens Axboe320ae512013-10-24 09:20:05 +01001534 * blk_attempt_plug_merge - try to merge with %current's plugged list
Tejun Heobd87b582011-10-19 14:33:08 +02001535 * @q: request_queue new bio is being queued at
1536 * @bio: new bio being queued
1537 * @request_count: out parameter for number of traversed plugged requests
Randy Dunlapccc26002015-10-30 18:36:16 -07001538 * @same_queue_rq: pointer to &struct request that gets filled in when
1539 * another request associated with @q is found on the plug list
1540 * (optional, may be %NULL)
Tejun Heobd87b582011-10-19 14:33:08 +02001541 *
1542 * Determine whether @bio being queued on @q can be merged with a request
1543 * on %current's plugged list. Returns %true if merge was successful,
1544 * otherwise %false.
1545 *
Tejun Heo07c2bd32012-02-08 09:19:42 +01001546 * Plugging coalesces IOs from the same issuer for the same purpose without
1547 * going through @q->queue_lock. As such it's more of an issuing mechanism
1548 * than scheduling, and the request, while may have elvpriv data, is not
1549 * added on the elevator at this point. In addition, we don't have
1550 * reliable access to the elevator outside queue lock. Only check basic
1551 * merging parameters without querying the elevator.
Robert Elliottda41a582014-05-20 16:46:26 -05001552 *
1553 * Caller must ensure !blk_queue_nomerges(q) beforehand.
Jens Axboe73c10102011-03-08 13:19:51 +01001554 */
Jens Axboe320ae512013-10-24 09:20:05 +01001555bool blk_attempt_plug_merge(struct request_queue *q, struct bio *bio,
Shaohua Li5b3f3412015-05-08 10:51:33 -07001556 unsigned int *request_count,
1557 struct request **same_queue_rq)
Jens Axboe73c10102011-03-08 13:19:51 +01001558{
1559 struct blk_plug *plug;
1560 struct request *rq;
Shaohua Li92f399c2013-10-29 12:01:03 -06001561 struct list_head *plug_list;
Jens Axboe73c10102011-03-08 13:19:51 +01001562
Tejun Heobd87b582011-10-19 14:33:08 +02001563 plug = current->plug;
Jens Axboe73c10102011-03-08 13:19:51 +01001564 if (!plug)
Christoph Hellwig34fe7c02017-02-08 14:46:48 +01001565 return false;
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001566 *request_count = 0;
Jens Axboe73c10102011-03-08 13:19:51 +01001567
Shaohua Li92f399c2013-10-29 12:01:03 -06001568 if (q->mq_ops)
1569 plug_list = &plug->mq_list;
1570 else
1571 plug_list = &plug->list;
1572
1573 list_for_each_entry_reverse(rq, plug_list, queuelist) {
Christoph Hellwig34fe7c02017-02-08 14:46:48 +01001574 bool merged = false;
Jens Axboe73c10102011-03-08 13:19:51 +01001575
Shaohua Li5b3f3412015-05-08 10:51:33 -07001576 if (rq->q == q) {
Shaohua Li1b2e19f2012-04-06 11:37:47 -06001577 (*request_count)++;
Shaohua Li5b3f3412015-05-08 10:51:33 -07001578 /*
1579 * Only blk-mq multiple hardware queues case checks the
1580 * rq in the same queue, there should be only one such
1581 * rq in a queue
1582 **/
1583 if (same_queue_rq)
1584 *same_queue_rq = rq;
1585 }
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001586
Tejun Heo07c2bd32012-02-08 09:19:42 +01001587 if (rq->q != q || !blk_rq_merge_ok(rq, bio))
Jens Axboe73c10102011-03-08 13:19:51 +01001588 continue;
1589
Christoph Hellwig34fe7c02017-02-08 14:46:48 +01001590 switch (blk_try_merge(rq, bio)) {
1591 case ELEVATOR_BACK_MERGE:
1592 merged = bio_attempt_back_merge(q, rq, bio);
1593 break;
1594 case ELEVATOR_FRONT_MERGE:
1595 merged = bio_attempt_front_merge(q, rq, bio);
1596 break;
Christoph Hellwig1e739732017-02-08 14:46:49 +01001597 case ELEVATOR_DISCARD_MERGE:
1598 merged = bio_attempt_discard_merge(q, rq, bio);
1599 break;
Christoph Hellwig34fe7c02017-02-08 14:46:48 +01001600 default:
1601 break;
Jens Axboe73c10102011-03-08 13:19:51 +01001602 }
Christoph Hellwig34fe7c02017-02-08 14:46:48 +01001603
1604 if (merged)
1605 return true;
Jens Axboe73c10102011-03-08 13:19:51 +01001606 }
Christoph Hellwig34fe7c02017-02-08 14:46:48 +01001607
1608 return false;
Jens Axboe73c10102011-03-08 13:19:51 +01001609}
1610
Jeff Moyer0809e3a2015-10-20 23:13:51 +08001611unsigned int blk_plug_queued_count(struct request_queue *q)
1612{
1613 struct blk_plug *plug;
1614 struct request *rq;
1615 struct list_head *plug_list;
1616 unsigned int ret = 0;
1617
1618 plug = current->plug;
1619 if (!plug)
1620 goto out;
1621
1622 if (q->mq_ops)
1623 plug_list = &plug->mq_list;
1624 else
1625 plug_list = &plug->list;
1626
1627 list_for_each_entry(rq, plug_list, queuelist) {
1628 if (rq->q == q)
1629 ret++;
1630 }
1631out:
1632 return ret;
1633}
1634
Bart Van Asscheda8d7f02017-04-19 14:01:24 -07001635void blk_init_request_from_bio(struct request *req, struct bio *bio)
Tejun Heo52d9e672006-01-06 09:49:58 +01001636{
Bart Van Assche0be0dee2017-04-19 14:01:27 -07001637 struct io_context *ioc = rq_ioc(bio);
1638
Jens Axboe1eff9d32016-08-05 15:35:16 -06001639 if (bio->bi_opf & REQ_RAHEAD)
Tejun Heoa82afdf2009-07-03 17:48:16 +09001640 req->cmd_flags |= REQ_FAILFAST_MASK;
Jens Axboeb31dc662006-06-13 08:26:10 +02001641
Kent Overstreet4f024f32013-10-11 15:44:27 -07001642 req->__sector = bio->bi_iter.bi_sector;
Adam Manzanares5dc8b362016-10-17 11:27:28 -07001643 if (ioprio_valid(bio_prio(bio)))
1644 req->ioprio = bio_prio(bio);
Bart Van Assche0be0dee2017-04-19 14:01:27 -07001645 else if (ioc)
1646 req->ioprio = ioc->ioprio;
1647 else
1648 req->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, 0);
NeilBrownbc1c56f2007-08-16 13:31:30 +02001649 blk_rq_bio_prep(req->q, req, bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001650}
Bart Van Asscheda8d7f02017-04-19 14:01:24 -07001651EXPORT_SYMBOL_GPL(blk_init_request_from_bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001652
Jens Axboedece1632015-11-05 10:41:16 -07001653static blk_qc_t blk_queue_bio(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
Jens Axboe73c10102011-03-08 13:19:51 +01001655 struct blk_plug *plug;
Christoph Hellwig34fe7c02017-02-08 14:46:48 +01001656 int where = ELEVATOR_INSERT_SORT;
Jens Axboee4d750c2017-02-03 09:48:28 -07001657 struct request *req, *free;
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001658 unsigned int request_count = 0;
Jens Axboe87760e52016-11-09 12:38:14 -07001659 unsigned int wb_acct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 /*
1662 * low level driver can indicate that it wants pages above a
1663 * certain limit bounced to low memory (ie for highmem, or even
1664 * ISA dma in theory)
1665 */
1666 blk_queue_bounce(q, &bio);
1667
Junichi Nomura23688bf2015-12-22 10:23:44 -07001668 blk_queue_split(q, &bio, q->bio_split);
1669
Darrick J. Wongffecfd12013-02-21 16:42:55 -08001670 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001671 bio->bi_error = -EIO;
1672 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -07001673 return BLK_QC_T_NONE;
Darrick J. Wongffecfd12013-02-21 16:42:55 -08001674 }
1675
Christoph Hellwigf73f44e2017-01-27 08:30:47 -07001676 if (op_is_flush(bio->bi_opf)) {
Jens Axboe73c10102011-03-08 13:19:51 +01001677 spin_lock_irq(q->queue_lock);
Tejun Heoae1b1532011-01-25 12:43:54 +01001678 where = ELEVATOR_INSERT_FLUSH;
Tejun Heo28e7d182010-09-03 11:56:16 +02001679 goto get_rq;
1680 }
1681
Jens Axboe73c10102011-03-08 13:19:51 +01001682 /*
1683 * Check if we can merge with the plugged list before grabbing
1684 * any locks.
1685 */
Jeff Moyer0809e3a2015-10-20 23:13:51 +08001686 if (!blk_queue_nomerges(q)) {
1687 if (blk_attempt_plug_merge(q, bio, &request_count, NULL))
Jens Axboedece1632015-11-05 10:41:16 -07001688 return BLK_QC_T_NONE;
Jeff Moyer0809e3a2015-10-20 23:13:51 +08001689 } else
1690 request_count = blk_plug_queued_count(q);
Jens Axboe73c10102011-03-08 13:19:51 +01001691
1692 spin_lock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Christoph Hellwig34fe7c02017-02-08 14:46:48 +01001694 switch (elv_merge(q, &req, bio)) {
1695 case ELEVATOR_BACK_MERGE:
1696 if (!bio_attempt_back_merge(q, req, bio))
1697 break;
1698 elv_bio_merged(q, req, bio);
1699 free = attempt_back_merge(q, req);
1700 if (free)
1701 __blk_put_request(q, free);
1702 else
1703 elv_merged_request(q, req, ELEVATOR_BACK_MERGE);
1704 goto out_unlock;
1705 case ELEVATOR_FRONT_MERGE:
1706 if (!bio_attempt_front_merge(q, req, bio))
1707 break;
1708 elv_bio_merged(q, req, bio);
1709 free = attempt_front_merge(q, req);
1710 if (free)
1711 __blk_put_request(q, free);
1712 else
1713 elv_merged_request(q, req, ELEVATOR_FRONT_MERGE);
1714 goto out_unlock;
1715 default:
1716 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 }
1718
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719get_rq:
Jens Axboe87760e52016-11-09 12:38:14 -07001720 wb_acct = wbt_wait(q->rq_wb, bio, q->queue_lock);
1721
Nick Piggin450991b2005-06-28 20:45:13 -07001722 /*
1723 * Grab a free request. This is might sleep but can not fail.
Nick Piggind6344532005-06-28 20:45:14 -07001724 * Returns with the queue unlocked.
Nick Piggin450991b2005-06-28 20:45:13 -07001725 */
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001726 req = get_request(q, bio->bi_opf, bio, GFP_NOIO);
Joe Lawrencea492f072014-08-28 08:15:21 -06001727 if (IS_ERR(req)) {
Jens Axboe87760e52016-11-09 12:38:14 -07001728 __wbt_done(q->rq_wb, wb_acct);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001729 bio->bi_error = PTR_ERR(req);
1730 bio_endio(bio);
Tejun Heoda8303c2011-10-19 14:33:05 +02001731 goto out_unlock;
1732 }
Nick Piggind6344532005-06-28 20:45:14 -07001733
Jens Axboe87760e52016-11-09 12:38:14 -07001734 wbt_track(&req->issue_stat, wb_acct);
1735
Nick Piggin450991b2005-06-28 20:45:13 -07001736 /*
1737 * After dropping the lock and possibly sleeping here, our request
1738 * may now be mergeable after it had proven unmergeable (above).
1739 * We don't worry about that case for efficiency. It won't happen
1740 * often, and the elevators are able to handle it.
1741 */
Bart Van Asscheda8d7f02017-04-19 14:01:24 -07001742 blk_init_request_from_bio(req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Tao Ma9562ad92011-10-24 16:11:30 +02001744 if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags))
Jens Axboe11ccf112011-07-26 15:01:15 +02001745 req->cpu = raw_smp_processor_id();
Tejun Heodd831002010-09-03 11:56:16 +02001746
Jens Axboe73c10102011-03-08 13:19:51 +01001747 plug = current->plug;
Jens Axboe721a9602011-03-09 11:56:30 +01001748 if (plug) {
Jens Axboedc6d36c2011-04-12 10:28:28 +02001749 /*
1750 * If this is the first request added after a plug, fire
Jianpeng Ma7aef2e72013-09-11 13:21:07 -06001751 * of a plug trace.
Ming Lei0a6219a2016-11-16 18:07:05 +08001752 *
1753 * @request_count may become stale because of schedule
1754 * out, so check plug list again.
Jens Axboedc6d36c2011-04-12 10:28:28 +02001755 */
Ming Lei0a6219a2016-11-16 18:07:05 +08001756 if (!request_count || list_empty(&plug->list))
Jens Axboedc6d36c2011-04-12 10:28:28 +02001757 trace_block_plug(q);
Shaohua Li3540d5e2011-11-16 09:21:50 +01001758 else {
Shaohua Li50d24c32016-11-03 17:03:53 -07001759 struct request *last = list_entry_rq(plug->list.prev);
1760 if (request_count >= BLK_MAX_REQUEST_COUNT ||
1761 blk_rq_bytes(last) >= BLK_PLUG_FLUSH_SIZE) {
Shaohua Li3540d5e2011-11-16 09:21:50 +01001762 blk_flush_plug_list(plug, false);
Shaohua Li019ceb72011-11-16 09:21:50 +01001763 trace_block_plug(q);
1764 }
Jens Axboe73c10102011-03-08 13:19:51 +01001765 }
Shaohua Lia6327162011-08-24 16:04:32 +02001766 list_add_tail(&req->queuelist, &plug->list);
Jens Axboe320ae512013-10-24 09:20:05 +01001767 blk_account_io_start(req, true);
Jens Axboe73c10102011-03-08 13:19:51 +01001768 } else {
1769 spin_lock_irq(q->queue_lock);
1770 add_acct_request(q, req, where);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02001771 __blk_run_queue(q);
Jens Axboe73c10102011-03-08 13:19:51 +01001772out_unlock:
1773 spin_unlock_irq(q->queue_lock);
1774 }
Jens Axboedece1632015-11-05 10:41:16 -07001775
1776 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777}
1778
1779/*
1780 * If bio->bi_dev is a partition, remap the location
1781 */
1782static inline void blk_partition_remap(struct bio *bio)
1783{
1784 struct block_device *bdev = bio->bi_bdev;
1785
Shaun Tancheff778889d2016-11-21 15:52:23 -06001786 /*
1787 * Zone reset does not include bi_size so bio_sectors() is always 0.
1788 * Include a test for the reset op code and perform the remap if needed.
1789 */
1790 if (bdev != bdev->bd_contains &&
1791 (bio_sectors(bio) || bio_op(bio) == REQ_OP_ZONE_RESET)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 struct hd_struct *p = bdev->bd_part;
Jens Axboea3623572005-11-01 09:26:16 +01001793
Kent Overstreet4f024f32013-10-11 15:44:27 -07001794 bio->bi_iter.bi_sector += p->start_sect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 bio->bi_bdev = bdev->bd_contains;
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001796
Mike Snitzerd07335e2010-11-16 12:52:38 +01001797 trace_block_bio_remap(bdev_get_queue(bio->bi_bdev), bio,
1798 bdev->bd_dev,
Kent Overstreet4f024f32013-10-11 15:44:27 -07001799 bio->bi_iter.bi_sector - p->start_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 }
1801}
1802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803static void handle_bad_sector(struct bio *bio)
1804{
1805 char b[BDEVNAME_SIZE];
1806
1807 printk(KERN_INFO "attempt to access beyond end of device\n");
Mike Christie6296b962016-06-05 14:32:21 -05001808 printk(KERN_INFO "%s: rw=%d, want=%Lu, limit=%Lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 bdevname(bio->bi_bdev, b),
Jens Axboe1eff9d32016-08-05 15:35:16 -06001810 bio->bi_opf,
Kent Overstreetf73a1c72012-09-25 15:05:12 -07001811 (unsigned long long)bio_end_sector(bio),
Mike Snitzer77304d22010-11-08 14:39:12 +01001812 (long long)(i_size_read(bio->bi_bdev->bd_inode) >> 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813}
1814
Akinobu Mitac17bb492006-12-08 02:39:46 -08001815#ifdef CONFIG_FAIL_MAKE_REQUEST
1816
1817static DECLARE_FAULT_ATTR(fail_make_request);
1818
1819static int __init setup_fail_make_request(char *str)
1820{
1821 return setup_fault_attr(&fail_make_request, str);
1822}
1823__setup("fail_make_request=", setup_fail_make_request);
1824
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001825static bool should_fail_request(struct hd_struct *part, unsigned int bytes)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001826{
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001827 return part->make_it_fail && should_fail(&fail_make_request, bytes);
Akinobu Mitac17bb492006-12-08 02:39:46 -08001828}
1829
1830static int __init fail_make_request_debugfs(void)
1831{
Akinobu Mitadd48c082011-08-03 16:21:01 -07001832 struct dentry *dir = fault_create_debugfs_attr("fail_make_request",
1833 NULL, &fail_make_request);
1834
Duan Jiong21f9fcd2014-04-11 15:58:56 +08001835 return PTR_ERR_OR_ZERO(dir);
Akinobu Mitac17bb492006-12-08 02:39:46 -08001836}
1837
1838late_initcall(fail_make_request_debugfs);
1839
1840#else /* CONFIG_FAIL_MAKE_REQUEST */
1841
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001842static inline bool should_fail_request(struct hd_struct *part,
1843 unsigned int bytes)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001844{
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001845 return false;
Akinobu Mitac17bb492006-12-08 02:39:46 -08001846}
1847
1848#endif /* CONFIG_FAIL_MAKE_REQUEST */
1849
Jens Axboec07e2b42007-07-18 13:27:58 +02001850/*
1851 * Check whether this bio extends beyond the end of the device.
1852 */
1853static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1854{
1855 sector_t maxsector;
1856
1857 if (!nr_sectors)
1858 return 0;
1859
1860 /* Test device or partition size, when known. */
Mike Snitzer77304d22010-11-08 14:39:12 +01001861 maxsector = i_size_read(bio->bi_bdev->bd_inode) >> 9;
Jens Axboec07e2b42007-07-18 13:27:58 +02001862 if (maxsector) {
Kent Overstreet4f024f32013-10-11 15:44:27 -07001863 sector_t sector = bio->bi_iter.bi_sector;
Jens Axboec07e2b42007-07-18 13:27:58 +02001864
1865 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1866 /*
1867 * This may well happen - the kernel calls bread()
1868 * without checking the size of the device, e.g., when
1869 * mounting a device.
1870 */
1871 handle_bad_sector(bio);
1872 return 1;
1873 }
1874 }
1875
1876 return 0;
1877}
1878
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001879static noinline_for_stack bool
1880generic_make_request_checks(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
Jens Axboe165125e2007-07-24 09:28:11 +02001882 struct request_queue *q;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001883 int nr_sectors = bio_sectors(bio);
Jens Axboe51fd77b2007-11-02 08:49:08 +01001884 int err = -EIO;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001885 char b[BDEVNAME_SIZE];
1886 struct hd_struct *part;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
1888 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Jens Axboec07e2b42007-07-18 13:27:58 +02001890 if (bio_check_eod(bio, nr_sectors))
1891 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001893 q = bdev_get_queue(bio->bi_bdev);
1894 if (unlikely(!q)) {
1895 printk(KERN_ERR
1896 "generic_make_request: Trying to access "
1897 "nonexistent block-device %s (%Lu)\n",
1898 bdevname(bio->bi_bdev, b),
Kent Overstreet4f024f32013-10-11 15:44:27 -07001899 (long long) bio->bi_iter.bi_sector);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001900 goto end_io;
1901 }
1902
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001903 part = bio->bi_bdev->bd_part;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001904 if (should_fail_request(part, bio->bi_iter.bi_size) ||
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001905 should_fail_request(&part_to_disk(part)->part0,
Kent Overstreet4f024f32013-10-11 15:44:27 -07001906 bio->bi_iter.bi_size))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001907 goto end_io;
1908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 /*
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001910 * If this device has partitions, remap block n
1911 * of partition p to block n+start(p) of the disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001913 blk_partition_remap(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001915 if (bio_check_eod(bio, nr_sectors))
1916 goto end_io;
1917
1918 /*
1919 * Filter flush bio's early so that make_request based
1920 * drivers without flush support don't have to worry
1921 * about them.
1922 */
Jens Axboef3a8ab72017-01-27 09:08:23 -07001923 if (op_is_flush(bio->bi_opf) &&
Jens Axboec888a8f2016-04-13 13:33:19 -06001924 !test_bit(QUEUE_FLAG_WC, &q->queue_flags)) {
Jens Axboe1eff9d32016-08-05 15:35:16 -06001925 bio->bi_opf &= ~(REQ_PREFLUSH | REQ_FUA);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001926 if (!nr_sectors) {
1927 err = 0;
Tejun Heoa7384672008-11-28 13:32:03 +09001928 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 }
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Christoph Hellwig288dab82016-06-09 16:00:36 +02001932 switch (bio_op(bio)) {
1933 case REQ_OP_DISCARD:
1934 if (!blk_queue_discard(q))
1935 goto not_supported;
1936 break;
1937 case REQ_OP_SECURE_ERASE:
1938 if (!blk_queue_secure_erase(q))
1939 goto not_supported;
1940 break;
1941 case REQ_OP_WRITE_SAME:
1942 if (!bdev_write_same(bio->bi_bdev))
1943 goto not_supported;
Nicolai Stange58886782016-12-04 14:56:39 +01001944 break;
Shaun Tancheff2d253442016-10-18 15:40:32 +09001945 case REQ_OP_ZONE_REPORT:
1946 case REQ_OP_ZONE_RESET:
1947 if (!bdev_is_zoned(bio->bi_bdev))
1948 goto not_supported;
Christoph Hellwig288dab82016-06-09 16:00:36 +02001949 break;
Chaitanya Kulkarnia6f07882016-11-30 12:28:59 -08001950 case REQ_OP_WRITE_ZEROES:
1951 if (!bdev_write_zeroes_sectors(bio->bi_bdev))
1952 goto not_supported;
1953 break;
Christoph Hellwig288dab82016-06-09 16:00:36 +02001954 default:
1955 break;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Tejun Heo7f4b35d2012-06-04 20:40:56 -07001958 /*
1959 * Various block parts want %current->io_context and lazy ioc
1960 * allocation ends up trading a lot of pain for a small amount of
1961 * memory. Just allocate it upfront. This may fail and block
1962 * layer knows how to live with it.
1963 */
1964 create_io_context(GFP_ATOMIC, q->node);
1965
Tejun Heoae118892015-08-18 14:55:20 -07001966 if (!blkcg_bio_issue_check(q, bio))
1967 return false;
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001968
NeilBrownfbbaf702017-04-07 09:40:52 -06001969 if (!bio_flagged(bio, BIO_TRACE_COMPLETION)) {
1970 trace_block_bio_queue(q, bio);
1971 /* Now that enqueuing has been traced, we need to trace
1972 * completion as well.
1973 */
1974 bio_set_flag(bio, BIO_TRACE_COMPLETION);
1975 }
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001976 return true;
Tejun Heoa7384672008-11-28 13:32:03 +09001977
Christoph Hellwig288dab82016-06-09 16:00:36 +02001978not_supported:
1979 err = -EOPNOTSUPP;
Tejun Heoa7384672008-11-28 13:32:03 +09001980end_io:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001981 bio->bi_error = err;
1982 bio_endio(bio);
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001983 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984}
1985
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001986/**
1987 * generic_make_request - hand a buffer to its device driver for I/O
1988 * @bio: The bio describing the location in memory and on the device.
1989 *
1990 * generic_make_request() is used to make I/O requests of block
1991 * devices. It is passed a &struct bio, which describes the I/O that needs
1992 * to be done.
1993 *
1994 * generic_make_request() does not return any status. The
1995 * success/failure status of the request, along with notification of
1996 * completion, is delivered asynchronously through the bio->bi_end_io
1997 * function described (one day) else where.
1998 *
1999 * The caller of generic_make_request must make sure that bi_io_vec
2000 * are set to describe the memory buffer, and that bi_dev and bi_sector are
2001 * set to describe the device address, and the
2002 * bi_end_io and optionally bi_private are set to describe how
2003 * completion notification should be signaled.
2004 *
2005 * generic_make_request and the drivers it calls may use bi_next if this
2006 * bio happens to be merged with someone else, and may resubmit the bio to
2007 * a lower device by calling into generic_make_request recursively, which
2008 * means the bio should NOT be touched after the call to ->make_request_fn.
Neil Brownd89d8792007-05-01 09:53:42 +02002009 */
Jens Axboedece1632015-11-05 10:41:16 -07002010blk_qc_t generic_make_request(struct bio *bio)
Neil Brownd89d8792007-05-01 09:53:42 +02002011{
NeilBrownf5fe1b52017-03-10 17:00:47 +11002012 /*
2013 * bio_list_on_stack[0] contains bios submitted by the current
2014 * make_request_fn.
2015 * bio_list_on_stack[1] contains bios that were submitted before
2016 * the current make_request_fn, but that haven't been processed
2017 * yet.
2018 */
2019 struct bio_list bio_list_on_stack[2];
Jens Axboedece1632015-11-05 10:41:16 -07002020 blk_qc_t ret = BLK_QC_T_NONE;
Akinobu Mitabddd87c2010-02-23 08:55:42 +01002021
Christoph Hellwig27a84d52011-09-15 14:01:40 +02002022 if (!generic_make_request_checks(bio))
Jens Axboedece1632015-11-05 10:41:16 -07002023 goto out;
Christoph Hellwig27a84d52011-09-15 14:01:40 +02002024
2025 /*
2026 * We only want one ->make_request_fn to be active at a time, else
2027 * stack usage with stacked devices could be a problem. So use
2028 * current->bio_list to keep a list of requests submited by a
2029 * make_request_fn function. current->bio_list is also used as a
2030 * flag to say if generic_make_request is currently active in this
2031 * task or not. If it is NULL, then no make_request is active. If
2032 * it is non-NULL, then a make_request is active, and new requests
2033 * should be added at the tail
2034 */
Akinobu Mitabddd87c2010-02-23 08:55:42 +01002035 if (current->bio_list) {
NeilBrownf5fe1b52017-03-10 17:00:47 +11002036 bio_list_add(&current->bio_list[0], bio);
Jens Axboedece1632015-11-05 10:41:16 -07002037 goto out;
Neil Brownd89d8792007-05-01 09:53:42 +02002038 }
Christoph Hellwig27a84d52011-09-15 14:01:40 +02002039
Neil Brownd89d8792007-05-01 09:53:42 +02002040 /* following loop may be a bit non-obvious, and so deserves some
2041 * explanation.
2042 * Before entering the loop, bio->bi_next is NULL (as all callers
2043 * ensure that) so we have a list with a single bio.
2044 * We pretend that we have just taken it off a longer list, so
Akinobu Mitabddd87c2010-02-23 08:55:42 +01002045 * we assign bio_list to a pointer to the bio_list_on_stack,
2046 * thus initialising the bio_list of new bios to be
Christoph Hellwig27a84d52011-09-15 14:01:40 +02002047 * added. ->make_request() may indeed add some more bios
Neil Brownd89d8792007-05-01 09:53:42 +02002048 * through a recursive call to generic_make_request. If it
2049 * did, we find a non-NULL value in bio_list and re-enter the loop
2050 * from the top. In this case we really did just take the bio
Akinobu Mitabddd87c2010-02-23 08:55:42 +01002051 * of the top of the list (no pretending) and so remove it from
Christoph Hellwig27a84d52011-09-15 14:01:40 +02002052 * bio_list, and call into ->make_request() again.
Neil Brownd89d8792007-05-01 09:53:42 +02002053 */
2054 BUG_ON(bio->bi_next);
NeilBrownf5fe1b52017-03-10 17:00:47 +11002055 bio_list_init(&bio_list_on_stack[0]);
2056 current->bio_list = bio_list_on_stack;
Neil Brownd89d8792007-05-01 09:53:42 +02002057 do {
Christoph Hellwig27a84d52011-09-15 14:01:40 +02002058 struct request_queue *q = bdev_get_queue(bio->bi_bdev);
2059
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +01002060 if (likely(blk_queue_enter(q, false) == 0)) {
NeilBrown79bd9952017-03-08 07:38:05 +11002061 struct bio_list lower, same;
2062
2063 /* Create a fresh bio_list for all subordinate requests */
NeilBrownf5fe1b52017-03-10 17:00:47 +11002064 bio_list_on_stack[1] = bio_list_on_stack[0];
2065 bio_list_init(&bio_list_on_stack[0]);
Jens Axboedece1632015-11-05 10:41:16 -07002066 ret = q->make_request_fn(q, bio);
Dan Williams3ef28e82015-10-21 13:20:12 -04002067
2068 blk_queue_exit(q);
2069
NeilBrown79bd9952017-03-08 07:38:05 +11002070 /* sort new bios into those for a lower level
2071 * and those for the same level
2072 */
2073 bio_list_init(&lower);
2074 bio_list_init(&same);
NeilBrownf5fe1b52017-03-10 17:00:47 +11002075 while ((bio = bio_list_pop(&bio_list_on_stack[0])) != NULL)
NeilBrown79bd9952017-03-08 07:38:05 +11002076 if (q == bdev_get_queue(bio->bi_bdev))
2077 bio_list_add(&same, bio);
2078 else
2079 bio_list_add(&lower, bio);
2080 /* now assemble so we handle the lowest level first */
NeilBrownf5fe1b52017-03-10 17:00:47 +11002081 bio_list_merge(&bio_list_on_stack[0], &lower);
2082 bio_list_merge(&bio_list_on_stack[0], &same);
2083 bio_list_merge(&bio_list_on_stack[0], &bio_list_on_stack[1]);
Dan Williams3ef28e82015-10-21 13:20:12 -04002084 } else {
Dan Williams3ef28e82015-10-21 13:20:12 -04002085 bio_io_error(bio);
Dan Williams3ef28e82015-10-21 13:20:12 -04002086 }
NeilBrownf5fe1b52017-03-10 17:00:47 +11002087 bio = bio_list_pop(&bio_list_on_stack[0]);
Neil Brownd89d8792007-05-01 09:53:42 +02002088 } while (bio);
Akinobu Mitabddd87c2010-02-23 08:55:42 +01002089 current->bio_list = NULL; /* deactivate */
Jens Axboedece1632015-11-05 10:41:16 -07002090
2091out:
2092 return ret;
Neil Brownd89d8792007-05-01 09:53:42 +02002093}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094EXPORT_SYMBOL(generic_make_request);
2095
2096/**
Randy Dunlap710027a2008-08-19 20:13:11 +02002097 * submit_bio - submit a bio to the block device layer for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 * @bio: The &struct bio which describes the I/O
2099 *
2100 * submit_bio() is very similar in purpose to generic_make_request(), and
2101 * uses that function to do most of the work. Both are fairly rough
Randy Dunlap710027a2008-08-19 20:13:11 +02002102 * interfaces; @bio must be presetup and ready for I/O.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 *
2104 */
Mike Christie4e49ea42016-06-05 14:31:41 -05002105blk_qc_t submit_bio(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106{
Jens Axboebf2de6f2007-09-27 13:01:25 +02002107 /*
2108 * If it's a regular read/write or a barrier with data attached,
2109 * go through the normal accounting stuff before submission.
2110 */
Martin K. Petersene2a60da2012-09-18 12:19:25 -04002111 if (bio_has_data(bio)) {
Martin K. Petersen4363ac72012-09-18 12:19:27 -04002112 unsigned int count;
2113
Mike Christie95fe6c12016-06-05 14:31:48 -05002114 if (unlikely(bio_op(bio) == REQ_OP_WRITE_SAME))
Martin K. Petersen4363ac72012-09-18 12:19:27 -04002115 count = bdev_logical_block_size(bio->bi_bdev) >> 9;
2116 else
2117 count = bio_sectors(bio);
2118
Mike Christiea8ebb052016-06-05 14:31:45 -05002119 if (op_is_write(bio_op(bio))) {
Jens Axboebf2de6f2007-09-27 13:01:25 +02002120 count_vm_events(PGPGOUT, count);
2121 } else {
Kent Overstreet4f024f32013-10-11 15:44:27 -07002122 task_io_account_read(bio->bi_iter.bi_size);
Jens Axboebf2de6f2007-09-27 13:01:25 +02002123 count_vm_events(PGPGIN, count);
2124 }
2125
2126 if (unlikely(block_dump)) {
2127 char b[BDEVNAME_SIZE];
San Mehat8dcbdc72010-09-14 08:48:01 +02002128 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s (%u sectors)\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002129 current->comm, task_pid_nr(current),
Mike Christiea8ebb052016-06-05 14:31:45 -05002130 op_is_write(bio_op(bio)) ? "WRITE" : "READ",
Kent Overstreet4f024f32013-10-11 15:44:27 -07002131 (unsigned long long)bio->bi_iter.bi_sector,
San Mehat8dcbdc72010-09-14 08:48:01 +02002132 bdevname(bio->bi_bdev, b),
2133 count);
Jens Axboebf2de6f2007-09-27 13:01:25 +02002134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 }
2136
Jens Axboedece1632015-11-05 10:41:16 -07002137 return generic_make_request(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139EXPORT_SYMBOL(submit_bio);
2140
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002141/**
Hannes Reineckebf4e6b42015-11-26 08:46:57 +01002142 * blk_cloned_rq_check_limits - Helper function to check a cloned request
2143 * for new the queue limits
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002144 * @q: the queue
2145 * @rq: the request being checked
2146 *
2147 * Description:
2148 * @rq may have been made based on weaker limitations of upper-level queues
2149 * in request stacking drivers, and it may violate the limitation of @q.
2150 * Since the block layer and the underlying device driver trust @rq
2151 * after it is inserted to @q, it should be checked against @q before
2152 * the insertion using this generic function.
2153 *
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002154 * Request stacking drivers like request-based dm may change the queue
Hannes Reineckebf4e6b42015-11-26 08:46:57 +01002155 * limits when retrying requests on other queues. Those requests need
2156 * to be checked against the new queue limits again during dispatch.
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002157 */
Hannes Reineckebf4e6b42015-11-26 08:46:57 +01002158static int blk_cloned_rq_check_limits(struct request_queue *q,
2159 struct request *rq)
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002160{
Mike Christie8fe0d472016-06-05 14:32:15 -05002161 if (blk_rq_sectors(rq) > blk_queue_get_max_sectors(q, req_op(rq))) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002162 printk(KERN_ERR "%s: over max size limit.\n", __func__);
2163 return -EIO;
2164 }
2165
2166 /*
2167 * queue's settings related to segment counting like q->bounce_pfn
2168 * may differ from that of other stacking queues.
2169 * Recalculate it to check the request correctly on this queue's
2170 * limitation.
2171 */
2172 blk_recalc_rq_segments(rq);
Martin K. Petersen8a783622010-02-26 00:20:39 -05002173 if (rq->nr_phys_segments > queue_max_segments(q)) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002174 printk(KERN_ERR "%s: over max segments limit.\n", __func__);
2175 return -EIO;
2176 }
2177
2178 return 0;
2179}
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002180
2181/**
2182 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
2183 * @q: the queue to submit the request
2184 * @rq: the request being queued
2185 */
2186int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
2187{
2188 unsigned long flags;
Jeff Moyer4853aba2011-08-15 21:37:25 +02002189 int where = ELEVATOR_INSERT_BACK;
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002190
Hannes Reineckebf4e6b42015-11-26 08:46:57 +01002191 if (blk_cloned_rq_check_limits(q, rq))
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002192 return -EIO;
2193
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07002194 if (rq->rq_disk &&
2195 should_fail_request(&rq->rq_disk->part0, blk_rq_bytes(rq)))
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002196 return -EIO;
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002197
Keith Busch7fb48982014-10-17 17:46:38 -06002198 if (q->mq_ops) {
2199 if (blk_queue_io_stat(q))
2200 blk_account_io_start(rq, true);
Jens Axboebd6737f2017-01-27 01:00:47 -07002201 blk_mq_sched_insert_request(rq, false, true, false, false);
Keith Busch7fb48982014-10-17 17:46:38 -06002202 return 0;
2203 }
2204
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002205 spin_lock_irqsave(q->queue_lock, flags);
Bart Van Assche3f3299d2012-11-28 13:42:38 +01002206 if (unlikely(blk_queue_dying(q))) {
Tejun Heo8ba61432011-12-14 00:33:37 +01002207 spin_unlock_irqrestore(q->queue_lock, flags);
2208 return -ENODEV;
2209 }
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002210
2211 /*
2212 * Submitting request must be dequeued before calling this function
2213 * because it will be linked to another request_queue
2214 */
2215 BUG_ON(blk_queued_rq(rq));
2216
Christoph Hellwigf73f44e2017-01-27 08:30:47 -07002217 if (op_is_flush(rq->cmd_flags))
Jeff Moyer4853aba2011-08-15 21:37:25 +02002218 where = ELEVATOR_INSERT_FLUSH;
2219
2220 add_acct_request(q, rq, where);
Jeff Moyere67b77c2011-10-17 12:57:23 +02002221 if (where == ELEVATOR_INSERT_FLUSH)
2222 __blk_run_queue(q);
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002223 spin_unlock_irqrestore(q->queue_lock, flags);
2224
2225 return 0;
2226}
2227EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
2228
Tejun Heo80a761f2009-07-03 17:48:17 +09002229/**
2230 * blk_rq_err_bytes - determine number of bytes till the next failure boundary
2231 * @rq: request to examine
2232 *
2233 * Description:
2234 * A request could be merge of IOs which require different failure
2235 * handling. This function determines the number of bytes which
2236 * can be failed from the beginning of the request without
2237 * crossing into area which need to be retried further.
2238 *
2239 * Return:
2240 * The number of bytes to fail.
2241 *
2242 * Context:
2243 * queue_lock must be held.
2244 */
2245unsigned int blk_rq_err_bytes(const struct request *rq)
2246{
2247 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
2248 unsigned int bytes = 0;
2249 struct bio *bio;
2250
Christoph Hellwige8064022016-10-20 15:12:13 +02002251 if (!(rq->rq_flags & RQF_MIXED_MERGE))
Tejun Heo80a761f2009-07-03 17:48:17 +09002252 return blk_rq_bytes(rq);
2253
2254 /*
2255 * Currently the only 'mixing' which can happen is between
2256 * different fastfail types. We can safely fail portions
2257 * which have all the failfast bits that the first one has -
2258 * the ones which are at least as eager to fail as the first
2259 * one.
2260 */
2261 for (bio = rq->bio; bio; bio = bio->bi_next) {
Jens Axboe1eff9d32016-08-05 15:35:16 -06002262 if ((bio->bi_opf & ff) != ff)
Tejun Heo80a761f2009-07-03 17:48:17 +09002263 break;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002264 bytes += bio->bi_iter.bi_size;
Tejun Heo80a761f2009-07-03 17:48:17 +09002265 }
2266
2267 /* this could lead to infinite loop */
2268 BUG_ON(blk_rq_bytes(rq) && !bytes);
2269 return bytes;
2270}
2271EXPORT_SYMBOL_GPL(blk_rq_err_bytes);
2272
Jens Axboe320ae512013-10-24 09:20:05 +01002273void blk_account_io_completion(struct request *req, unsigned int bytes)
Jens Axboebc58ba92009-01-23 10:54:44 +01002274{
Jens Axboec2553b52009-04-24 08:10:11 +02002275 if (blk_do_io_stat(req)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01002276 const int rw = rq_data_dir(req);
2277 struct hd_struct *part;
2278 int cpu;
2279
2280 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +01002281 part = req->part;
Jens Axboebc58ba92009-01-23 10:54:44 +01002282 part_stat_add(cpu, part, sectors[rw], bytes >> 9);
2283 part_stat_unlock();
2284 }
2285}
2286
Jens Axboe320ae512013-10-24 09:20:05 +01002287void blk_account_io_done(struct request *req)
Jens Axboebc58ba92009-01-23 10:54:44 +01002288{
Jens Axboebc58ba92009-01-23 10:54:44 +01002289 /*
Tejun Heodd4c1332010-09-03 11:56:16 +02002290 * Account IO completion. flush_rq isn't accounted as a
2291 * normal IO on queueing nor completion. Accounting the
2292 * containing request is enough.
Jens Axboebc58ba92009-01-23 10:54:44 +01002293 */
Christoph Hellwige8064022016-10-20 15:12:13 +02002294 if (blk_do_io_stat(req) && !(req->rq_flags & RQF_FLUSH_SEQ)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01002295 unsigned long duration = jiffies - req->start_time;
2296 const int rw = rq_data_dir(req);
2297 struct hd_struct *part;
2298 int cpu;
2299
2300 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +01002301 part = req->part;
Jens Axboebc58ba92009-01-23 10:54:44 +01002302
2303 part_stat_inc(cpu, part, ios[rw]);
2304 part_stat_add(cpu, part, ticks[rw], duration);
2305 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02002306 part_dec_in_flight(part, rw);
Jens Axboebc58ba92009-01-23 10:54:44 +01002307
Jens Axboe6c23a962011-01-07 08:43:37 +01002308 hd_struct_put(part);
Jens Axboebc58ba92009-01-23 10:54:44 +01002309 part_stat_unlock();
2310 }
2311}
2312
Rafael J. Wysocki47fafbc2014-12-04 01:00:23 +01002313#ifdef CONFIG_PM
Lin Mingc8158812013-03-23 11:42:27 +08002314/*
2315 * Don't process normal requests when queue is suspended
2316 * or in the process of suspending/resuming
2317 */
2318static struct request *blk_pm_peek_request(struct request_queue *q,
2319 struct request *rq)
2320{
2321 if (q->dev && (q->rpm_status == RPM_SUSPENDED ||
Christoph Hellwige8064022016-10-20 15:12:13 +02002322 (q->rpm_status != RPM_ACTIVE && !(rq->rq_flags & RQF_PM))))
Lin Mingc8158812013-03-23 11:42:27 +08002323 return NULL;
2324 else
2325 return rq;
2326}
2327#else
2328static inline struct request *blk_pm_peek_request(struct request_queue *q,
2329 struct request *rq)
2330{
2331 return rq;
2332}
2333#endif
2334
Jens Axboe320ae512013-10-24 09:20:05 +01002335void blk_account_io_start(struct request *rq, bool new_io)
2336{
2337 struct hd_struct *part;
2338 int rw = rq_data_dir(rq);
2339 int cpu;
2340
2341 if (!blk_do_io_stat(rq))
2342 return;
2343
2344 cpu = part_stat_lock();
2345
2346 if (!new_io) {
2347 part = rq->part;
2348 part_stat_inc(cpu, part, merges[rw]);
2349 } else {
2350 part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq));
2351 if (!hd_struct_try_get(part)) {
2352 /*
2353 * The partition is already being removed,
2354 * the request will be accounted on the disk only
2355 *
2356 * We take a reference on disk->part0 although that
2357 * partition will never be deleted, so we can treat
2358 * it as any other partition.
2359 */
2360 part = &rq->rq_disk->part0;
2361 hd_struct_get(part);
2362 }
2363 part_round_stats(cpu, part);
2364 part_inc_in_flight(part, rw);
2365 rq->part = part;
2366 }
2367
2368 part_stat_unlock();
2369}
2370
Tejun Heo53a08802008-12-03 12:41:26 +01002371/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09002372 * blk_peek_request - peek at the top of a request queue
2373 * @q: request queue to peek at
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002374 *
2375 * Description:
Tejun Heo9934c8c2009-05-08 11:54:16 +09002376 * Return the request at the top of @q. The returned request
2377 * should be started using blk_start_request() before LLD starts
2378 * processing it.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002379 *
2380 * Return:
Tejun Heo9934c8c2009-05-08 11:54:16 +09002381 * Pointer to the request at the top of @q if available. Null
2382 * otherwise.
2383 *
2384 * Context:
2385 * queue_lock must be held.
2386 */
2387struct request *blk_peek_request(struct request_queue *q)
Tejun Heo158dbda2009-04-23 11:05:18 +09002388{
2389 struct request *rq;
2390 int ret;
2391
2392 while ((rq = __elv_next_request(q)) != NULL) {
Lin Mingc8158812013-03-23 11:42:27 +08002393
2394 rq = blk_pm_peek_request(q, rq);
2395 if (!rq)
2396 break;
2397
Christoph Hellwige8064022016-10-20 15:12:13 +02002398 if (!(rq->rq_flags & RQF_STARTED)) {
Tejun Heo158dbda2009-04-23 11:05:18 +09002399 /*
2400 * This is the first time the device driver
2401 * sees this request (possibly after
2402 * requeueing). Notify IO scheduler.
2403 */
Christoph Hellwige8064022016-10-20 15:12:13 +02002404 if (rq->rq_flags & RQF_SORTED)
Tejun Heo158dbda2009-04-23 11:05:18 +09002405 elv_activate_rq(q, rq);
2406
2407 /*
2408 * just mark as started even if we don't start
2409 * it, a request that has been delayed should
2410 * not be passed by new incoming requests
2411 */
Christoph Hellwige8064022016-10-20 15:12:13 +02002412 rq->rq_flags |= RQF_STARTED;
Tejun Heo158dbda2009-04-23 11:05:18 +09002413 trace_block_rq_issue(q, rq);
2414 }
2415
2416 if (!q->boundary_rq || q->boundary_rq == rq) {
2417 q->end_sector = rq_end_sector(rq);
2418 q->boundary_rq = NULL;
2419 }
2420
Christoph Hellwige8064022016-10-20 15:12:13 +02002421 if (rq->rq_flags & RQF_DONTPREP)
Tejun Heo158dbda2009-04-23 11:05:18 +09002422 break;
2423
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002424 if (q->dma_drain_size && blk_rq_bytes(rq)) {
Tejun Heo158dbda2009-04-23 11:05:18 +09002425 /*
2426 * make sure space for the drain appears we
2427 * know we can do this because max_hw_segments
2428 * has been adjusted to be one fewer than the
2429 * device can handle
2430 */
2431 rq->nr_phys_segments++;
2432 }
2433
2434 if (!q->prep_rq_fn)
2435 break;
2436
2437 ret = q->prep_rq_fn(q, rq);
2438 if (ret == BLKPREP_OK) {
2439 break;
2440 } else if (ret == BLKPREP_DEFER) {
2441 /*
2442 * the request may have been (partially) prepped.
2443 * we need to keep this request in the front to
Christoph Hellwige8064022016-10-20 15:12:13 +02002444 * avoid resource deadlock. RQF_STARTED will
Tejun Heo158dbda2009-04-23 11:05:18 +09002445 * prevent other fs requests from passing this one.
2446 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002447 if (q->dma_drain_size && blk_rq_bytes(rq) &&
Christoph Hellwige8064022016-10-20 15:12:13 +02002448 !(rq->rq_flags & RQF_DONTPREP)) {
Tejun Heo158dbda2009-04-23 11:05:18 +09002449 /*
2450 * remove the space for the drain we added
2451 * so that we don't add it again
2452 */
2453 --rq->nr_phys_segments;
2454 }
2455
2456 rq = NULL;
2457 break;
Martin K. Petersen0fb5b1f2016-02-04 00:52:12 -05002458 } else if (ret == BLKPREP_KILL || ret == BLKPREP_INVALID) {
2459 int err = (ret == BLKPREP_INVALID) ? -EREMOTEIO : -EIO;
2460
Christoph Hellwige8064022016-10-20 15:12:13 +02002461 rq->rq_flags |= RQF_QUIET;
James Bottomleyc143dc92009-05-30 06:43:49 +02002462 /*
2463 * Mark this request as started so we don't trigger
2464 * any debug logic in the end I/O path.
2465 */
2466 blk_start_request(rq);
Martin K. Petersen0fb5b1f2016-02-04 00:52:12 -05002467 __blk_end_request_all(rq, err);
Tejun Heo158dbda2009-04-23 11:05:18 +09002468 } else {
2469 printk(KERN_ERR "%s: bad return=%d\n", __func__, ret);
2470 break;
2471 }
2472 }
2473
2474 return rq;
2475}
Tejun Heo9934c8c2009-05-08 11:54:16 +09002476EXPORT_SYMBOL(blk_peek_request);
Tejun Heo158dbda2009-04-23 11:05:18 +09002477
Tejun Heo9934c8c2009-05-08 11:54:16 +09002478void blk_dequeue_request(struct request *rq)
Tejun Heo158dbda2009-04-23 11:05:18 +09002479{
Tejun Heo9934c8c2009-05-08 11:54:16 +09002480 struct request_queue *q = rq->q;
2481
Tejun Heo158dbda2009-04-23 11:05:18 +09002482 BUG_ON(list_empty(&rq->queuelist));
2483 BUG_ON(ELV_ON_HASH(rq));
2484
2485 list_del_init(&rq->queuelist);
2486
2487 /*
2488 * the time frame between a request being removed from the lists
2489 * and to it is freed is accounted as io that is in progress at
2490 * the driver side.
2491 */
Divyesh Shah91952912010-04-01 15:01:41 -07002492 if (blk_account_rq(rq)) {
Jens Axboe0a7ae2f2009-05-20 08:54:31 +02002493 q->in_flight[rq_is_sync(rq)]++;
Divyesh Shah91952912010-04-01 15:01:41 -07002494 set_io_start_time_ns(rq);
2495 }
Tejun Heo158dbda2009-04-23 11:05:18 +09002496}
2497
Tejun Heo5efccd12009-04-23 11:05:18 +09002498/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09002499 * blk_start_request - start request processing on the driver
2500 * @req: request to dequeue
2501 *
2502 * Description:
2503 * Dequeue @req and start timeout timer on it. This hands off the
2504 * request to the driver.
2505 *
2506 * Block internal functions which don't want to start timer should
2507 * call blk_dequeue_request().
2508 *
2509 * Context:
2510 * queue_lock must be held.
2511 */
2512void blk_start_request(struct request *req)
2513{
2514 blk_dequeue_request(req);
2515
Jens Axboecf43e6b2016-11-07 21:32:37 -07002516 if (test_bit(QUEUE_FLAG_STATS, &req->q->queue_flags)) {
Shaohua Li88eeca42017-03-27 15:19:41 -07002517 blk_stat_set_issue(&req->issue_stat, blk_rq_sectors(req));
Jens Axboecf43e6b2016-11-07 21:32:37 -07002518 req->rq_flags |= RQF_STATS;
Jens Axboe87760e52016-11-09 12:38:14 -07002519 wbt_issue(req->q->rq_wb, &req->issue_stat);
Jens Axboecf43e6b2016-11-07 21:32:37 -07002520 }
2521
Jeff Moyer4912aa62013-10-08 14:36:41 -04002522 BUG_ON(test_bit(REQ_ATOM_COMPLETE, &req->atomic_flags));
Tejun Heo9934c8c2009-05-08 11:54:16 +09002523 blk_add_timer(req);
2524}
2525EXPORT_SYMBOL(blk_start_request);
2526
2527/**
2528 * blk_fetch_request - fetch a request from a request queue
2529 * @q: request queue to fetch a request from
2530 *
2531 * Description:
2532 * Return the request at the top of @q. The request is started on
2533 * return and LLD can start processing it immediately.
2534 *
2535 * Return:
2536 * Pointer to the request at the top of @q if available. Null
2537 * otherwise.
2538 *
2539 * Context:
2540 * queue_lock must be held.
2541 */
2542struct request *blk_fetch_request(struct request_queue *q)
2543{
2544 struct request *rq;
2545
2546 rq = blk_peek_request(q);
2547 if (rq)
2548 blk_start_request(rq);
2549 return rq;
2550}
2551EXPORT_SYMBOL(blk_fetch_request);
2552
2553/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002554 * blk_update_request - Special helper function for request stacking drivers
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002555 * @req: the request being processed
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002556 * @error: %0 for success, < %0 for error
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002557 * @nr_bytes: number of bytes to complete @req
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002558 *
2559 * Description:
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002560 * Ends I/O on a number of bytes attached to @req, but doesn't complete
2561 * the request structure even if @req doesn't have leftover.
2562 * If @req has leftover, sets it up for the next range of segments.
Tejun Heo2e60e022009-04-23 11:05:18 +09002563 *
2564 * This special helper function is only for request stacking drivers
2565 * (e.g. request-based dm) so that they can handle partial completion.
2566 * Actual device drivers should use blk_end_request instead.
2567 *
2568 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
2569 * %false return from this function.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002570 *
2571 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002572 * %false - this request doesn't have any more data
2573 * %true - this request has more data
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002574 **/
Tejun Heo2e60e022009-04-23 11:05:18 +09002575bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576{
Kent Overstreetf79ea412012-09-20 16:38:30 -07002577 int total_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Christoph Hellwigcaf7df12017-04-20 16:03:16 +02002579 trace_block_rq_complete(req, error, nr_bytes);
Hannes Reinecke4a0efdc2014-10-01 14:32:31 +02002580
Tejun Heo2e60e022009-04-23 11:05:18 +09002581 if (!req->bio)
2582 return false;
2583
Christoph Hellwig57292b52017-01-31 16:57:29 +01002584 if (error && !blk_rq_is_passthrough(req) &&
Christoph Hellwige8064022016-10-20 15:12:13 +02002585 !(req->rq_flags & RQF_QUIET)) {
Hannes Reinecke79775562011-01-18 10:13:13 +01002586 char *error_type;
2587
2588 switch (error) {
2589 case -ENOLINK:
2590 error_type = "recoverable transport";
2591 break;
2592 case -EREMOTEIO:
2593 error_type = "critical target";
2594 break;
2595 case -EBADE:
2596 error_type = "critical nexus";
2597 break;
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002598 case -ETIMEDOUT:
2599 error_type = "timeout";
2600 break;
Hannes Reineckea9d6ceb82013-07-01 15:16:25 +02002601 case -ENOSPC:
2602 error_type = "critical space allocation";
2603 break;
Hannes Reinecke7e782af2013-07-01 15:16:26 +02002604 case -ENODATA:
2605 error_type = "critical medium";
2606 break;
Hannes Reinecke79775562011-01-18 10:13:13 +01002607 case -EIO:
2608 default:
2609 error_type = "I/O";
2610 break;
2611 }
Robert Elliottef3ecb62014-08-27 10:50:31 -05002612 printk_ratelimited(KERN_ERR "%s: %s error, dev %s, sector %llu\n",
2613 __func__, error_type, req->rq_disk ?
Yi Zou37d7b342012-08-30 16:26:25 -07002614 req->rq_disk->disk_name : "?",
2615 (unsigned long long)blk_rq_pos(req));
2616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 }
2618
Jens Axboebc58ba92009-01-23 10:54:44 +01002619 blk_account_io_completion(req, nr_bytes);
Jens Axboed72d9042005-11-01 08:35:42 +01002620
Kent Overstreetf79ea412012-09-20 16:38:30 -07002621 total_bytes = 0;
2622 while (req->bio) {
2623 struct bio *bio = req->bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002624 unsigned bio_bytes = min(bio->bi_iter.bi_size, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
Kent Overstreet4f024f32013-10-11 15:44:27 -07002626 if (bio_bytes == bio->bi_iter.bi_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 req->bio = bio->bi_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
NeilBrownfbbaf702017-04-07 09:40:52 -06002629 /* Completion has already been traced */
2630 bio_clear_flag(bio, BIO_TRACE_COMPLETION);
Kent Overstreetf79ea412012-09-20 16:38:30 -07002631 req_bio_endio(req, bio, bio_bytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
Kent Overstreetf79ea412012-09-20 16:38:30 -07002633 total_bytes += bio_bytes;
2634 nr_bytes -= bio_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
Kent Overstreetf79ea412012-09-20 16:38:30 -07002636 if (!nr_bytes)
2637 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 }
2639
2640 /*
2641 * completely done
2642 */
Tejun Heo2e60e022009-04-23 11:05:18 +09002643 if (!req->bio) {
2644 /*
2645 * Reset counters so that the request stacking driver
2646 * can find how many bytes remain in the request
2647 * later.
2648 */
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002649 req->__data_len = 0;
Tejun Heo2e60e022009-04-23 11:05:18 +09002650 return false;
2651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07002653 WARN_ON_ONCE(req->rq_flags & RQF_SPECIAL_PAYLOAD);
2654
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002655 req->__data_len -= total_bytes;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002656
2657 /* update sector only for requests with clear definition of sector */
Christoph Hellwig57292b52017-01-31 16:57:29 +01002658 if (!blk_rq_is_passthrough(req))
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002659 req->__sector += total_bytes >> 9;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002660
Tejun Heo80a761f2009-07-03 17:48:17 +09002661 /* mixed attributes always follow the first bio */
Christoph Hellwige8064022016-10-20 15:12:13 +02002662 if (req->rq_flags & RQF_MIXED_MERGE) {
Tejun Heo80a761f2009-07-03 17:48:17 +09002663 req->cmd_flags &= ~REQ_FAILFAST_MASK;
Jens Axboe1eff9d32016-08-05 15:35:16 -06002664 req->cmd_flags |= req->bio->bi_opf & REQ_FAILFAST_MASK;
Tejun Heo80a761f2009-07-03 17:48:17 +09002665 }
2666
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002667 /*
2668 * If total number of sectors is less than the first segment
2669 * size, something has gone terribly wrong.
2670 */
2671 if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
Jens Axboe81829242011-03-30 09:51:33 +02002672 blk_dump_rq_flags(req, "request botched");
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002673 req->__data_len = blk_rq_cur_bytes(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002674 }
2675
2676 /* recalculate the number of segments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 blk_recalc_rq_segments(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002678
Tejun Heo2e60e022009-04-23 11:05:18 +09002679 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680}
Tejun Heo2e60e022009-04-23 11:05:18 +09002681EXPORT_SYMBOL_GPL(blk_update_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
Tejun Heo2e60e022009-04-23 11:05:18 +09002683static bool blk_update_bidi_request(struct request *rq, int error,
2684 unsigned int nr_bytes,
2685 unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002686{
Tejun Heo2e60e022009-04-23 11:05:18 +09002687 if (blk_update_request(rq, error, nr_bytes))
2688 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002689
Tejun Heo2e60e022009-04-23 11:05:18 +09002690 /* Bidi request must be completed as a whole */
2691 if (unlikely(blk_bidi_rq(rq)) &&
2692 blk_update_request(rq->next_rq, error, bidi_bytes))
2693 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002694
Jens Axboee2e1a142010-06-09 10:42:09 +02002695 if (blk_queue_add_random(rq->q))
2696 add_disk_randomness(rq->rq_disk);
Tejun Heo2e60e022009-04-23 11:05:18 +09002697
2698 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699}
2700
James Bottomley28018c22010-07-01 19:49:17 +09002701/**
2702 * blk_unprep_request - unprepare a request
2703 * @req: the request
2704 *
2705 * This function makes a request ready for complete resubmission (or
2706 * completion). It happens only after all error handling is complete,
2707 * so represents the appropriate moment to deallocate any resources
2708 * that were allocated to the request in the prep_rq_fn. The queue
2709 * lock is held when calling this.
2710 */
2711void blk_unprep_request(struct request *req)
2712{
2713 struct request_queue *q = req->q;
2714
Christoph Hellwige8064022016-10-20 15:12:13 +02002715 req->rq_flags &= ~RQF_DONTPREP;
James Bottomley28018c22010-07-01 19:49:17 +09002716 if (q->unprep_rq_fn)
2717 q->unprep_rq_fn(q, req);
2718}
2719EXPORT_SYMBOL_GPL(blk_unprep_request);
2720
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721/*
2722 * queue lock must be held
2723 */
Christoph Hellwig12120072014-04-16 09:44:59 +02002724void blk_finish_request(struct request *req, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725{
Jens Axboecf43e6b2016-11-07 21:32:37 -07002726 struct request_queue *q = req->q;
2727
2728 if (req->rq_flags & RQF_STATS)
Omar Sandoval34dbad52017-03-21 08:56:08 -07002729 blk_stat_add(req);
Jens Axboecf43e6b2016-11-07 21:32:37 -07002730
Christoph Hellwige8064022016-10-20 15:12:13 +02002731 if (req->rq_flags & RQF_QUEUED)
Jens Axboecf43e6b2016-11-07 21:32:37 -07002732 blk_queue_end_tag(q, req);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002733
James Bottomleyba396a62009-05-27 14:17:08 +02002734 BUG_ON(blk_queued_rq(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
Christoph Hellwig57292b52017-01-31 16:57:29 +01002736 if (unlikely(laptop_mode) && !blk_rq_is_passthrough(req))
Jan Karadc3b17c2017-02-02 15:56:50 +01002737 laptop_io_completion(req->q->backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
Mike Andersone78042e52008-10-30 02:16:20 -07002739 blk_delete_timer(req);
2740
Christoph Hellwige8064022016-10-20 15:12:13 +02002741 if (req->rq_flags & RQF_DONTPREP)
James Bottomley28018c22010-07-01 19:49:17 +09002742 blk_unprep_request(req);
2743
Jens Axboebc58ba92009-01-23 10:54:44 +01002744 blk_account_io_done(req);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002745
Jens Axboe87760e52016-11-09 12:38:14 -07002746 if (req->end_io) {
2747 wbt_done(req->q->rq_wb, &req->issue_stat);
Tejun Heo8ffdc652006-01-06 09:49:03 +01002748 req->end_io(req, error);
Jens Axboe87760e52016-11-09 12:38:14 -07002749 } else {
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002750 if (blk_bidi_rq(req))
2751 __blk_put_request(req->next_rq->q, req->next_rq);
2752
Jens Axboecf43e6b2016-11-07 21:32:37 -07002753 __blk_put_request(q, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 }
2755}
Christoph Hellwig12120072014-04-16 09:44:59 +02002756EXPORT_SYMBOL(blk_finish_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05002758/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002759 * blk_end_bidi_request - Complete a bidi request
2760 * @rq: the request to complete
Randy Dunlap710027a2008-08-19 20:13:11 +02002761 * @error: %0 for success, < %0 for error
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002762 * @nr_bytes: number of bytes to complete @rq
2763 * @bidi_bytes: number of bytes to complete @rq->next_rq
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002764 *
2765 * Description:
2766 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
Tejun Heo2e60e022009-04-23 11:05:18 +09002767 * Drivers that supports bidi can safely call this member for any
2768 * type of request, bidi or uni. In the later case @bidi_bytes is
2769 * just ignored.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002770 *
2771 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002772 * %false - we are done with this request
2773 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002774 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002775static bool blk_end_bidi_request(struct request *rq, int error,
2776 unsigned int nr_bytes, unsigned int bidi_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002777{
2778 struct request_queue *q = rq->q;
Tejun Heo2e60e022009-04-23 11:05:18 +09002779 unsigned long flags;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002780
Tejun Heo2e60e022009-04-23 11:05:18 +09002781 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2782 return true;
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002783
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002784 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo2e60e022009-04-23 11:05:18 +09002785 blk_finish_request(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002786 spin_unlock_irqrestore(q->queue_lock, flags);
2787
Tejun Heo2e60e022009-04-23 11:05:18 +09002788 return false;
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002789}
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002790
2791/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002792 * __blk_end_bidi_request - Complete a bidi request with queue lock held
2793 * @rq: the request to complete
2794 * @error: %0 for success, < %0 for error
2795 * @nr_bytes: number of bytes to complete @rq
2796 * @bidi_bytes: number of bytes to complete @rq->next_rq
Tejun Heo5efccd12009-04-23 11:05:18 +09002797 *
2798 * Description:
Tejun Heo2e60e022009-04-23 11:05:18 +09002799 * Identical to blk_end_bidi_request() except that queue lock is
2800 * assumed to be locked on entry and remains so on return.
Tejun Heo5efccd12009-04-23 11:05:18 +09002801 *
Tejun Heo2e60e022009-04-23 11:05:18 +09002802 * Return:
2803 * %false - we are done with this request
2804 * %true - still buffers pending for this request
Tejun Heo5efccd12009-04-23 11:05:18 +09002805 **/
Christoph Hellwigd0fac022017-04-12 12:13:59 +02002806static bool __blk_end_bidi_request(struct request *rq, int error,
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002807 unsigned int nr_bytes, unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002808{
Tejun Heo2e60e022009-04-23 11:05:18 +09002809 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2810 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002811
Tejun Heo2e60e022009-04-23 11:05:18 +09002812 blk_finish_request(rq, error);
Tejun Heo5efccd12009-04-23 11:05:18 +09002813
Tejun Heo2e60e022009-04-23 11:05:18 +09002814 return false;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002815}
2816
2817/**
2818 * blk_end_request - Helper function for drivers to complete the request.
2819 * @rq: the request being processed
2820 * @error: %0 for success, < %0 for error
2821 * @nr_bytes: number of bytes to complete
2822 *
2823 * Description:
2824 * Ends I/O on a number of bytes attached to @rq.
2825 * If @rq has leftover, sets it up for the next range of segments.
2826 *
2827 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002828 * %false - we are done with this request
2829 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002830 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002831bool blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002832{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002833 return blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002834}
Jens Axboe56ad1742009-07-28 22:11:24 +02002835EXPORT_SYMBOL(blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002836
2837/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002838 * blk_end_request_all - Helper function for drives to finish the request.
2839 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002840 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002841 *
2842 * Description:
2843 * Completely finish @rq.
2844 */
2845void blk_end_request_all(struct request *rq, int error)
2846{
2847 bool pending;
2848 unsigned int bidi_bytes = 0;
2849
2850 if (unlikely(blk_bidi_rq(rq)))
2851 bidi_bytes = blk_rq_bytes(rq->next_rq);
2852
2853 pending = blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2854 BUG_ON(pending);
2855}
Jens Axboe56ad1742009-07-28 22:11:24 +02002856EXPORT_SYMBOL(blk_end_request_all);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002857
2858/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002859 * __blk_end_request - Helper function for drivers to complete the request.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002860 * @rq: the request being processed
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002861 * @error: %0 for success, < %0 for error
2862 * @nr_bytes: number of bytes to complete
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002863 *
2864 * Description:
2865 * Must be called with queue lock held unlike blk_end_request().
2866 *
2867 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002868 * %false - we are done with this request
2869 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002870 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002871bool __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002872{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002873 return __blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002874}
Jens Axboe56ad1742009-07-28 22:11:24 +02002875EXPORT_SYMBOL(__blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002876
2877/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002878 * __blk_end_request_all - Helper function for drives to finish the request.
2879 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002880 * @error: %0 for success, < %0 for error
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002881 *
2882 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002883 * Completely finish @rq. Must be called with queue lock held.
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002884 */
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002885void __blk_end_request_all(struct request *rq, int error)
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002886{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002887 bool pending;
2888 unsigned int bidi_bytes = 0;
2889
2890 if (unlikely(blk_bidi_rq(rq)))
2891 bidi_bytes = blk_rq_bytes(rq->next_rq);
2892
2893 pending = __blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2894 BUG_ON(pending);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002895}
Jens Axboe56ad1742009-07-28 22:11:24 +02002896EXPORT_SYMBOL(__blk_end_request_all);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002897
2898/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002899 * __blk_end_request_cur - Helper function to finish the current request chunk.
2900 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002901 * @error: %0 for success, < %0 for error
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002902 *
2903 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002904 * Complete the current consecutively mapped chunk from @rq. Must
2905 * be called with queue lock held.
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002906 *
2907 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002908 * %false - we are done with this request
2909 * %true - still buffers pending for this request
2910 */
2911bool __blk_end_request_cur(struct request *rq, int error)
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002912{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002913 return __blk_end_request(rq, error, blk_rq_cur_bytes(rq));
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002914}
Jens Axboe56ad1742009-07-28 22:11:24 +02002915EXPORT_SYMBOL(__blk_end_request_cur);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002916
Jens Axboe86db1e22008-01-29 14:53:40 +01002917void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2918 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919{
Jens Axboeb4f42e22014-04-10 09:46:28 -06002920 if (bio_has_data(bio))
David Woodhousefb2dce82008-08-05 18:01:53 +01002921 rq->nr_phys_segments = bio_phys_segments(q, bio);
Jens Axboeb4f42e22014-04-10 09:46:28 -06002922
Kent Overstreet4f024f32013-10-11 15:44:27 -07002923 rq->__data_len = bio->bi_iter.bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 rq->bio = rq->biotail = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
NeilBrown66846572007-08-16 13:31:28 +02002926 if (bio->bi_bdev)
2927 rq->rq_disk = bio->bi_bdev->bd_disk;
2928}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
Ilya Loginov2d4dc892009-11-26 09:16:19 +01002930#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
2931/**
2932 * rq_flush_dcache_pages - Helper function to flush all pages in a request
2933 * @rq: the request to be flushed
2934 *
2935 * Description:
2936 * Flush all pages in @rq.
2937 */
2938void rq_flush_dcache_pages(struct request *rq)
2939{
2940 struct req_iterator iter;
Kent Overstreet79886132013-11-23 17:19:00 -08002941 struct bio_vec bvec;
Ilya Loginov2d4dc892009-11-26 09:16:19 +01002942
2943 rq_for_each_segment(bvec, rq, iter)
Kent Overstreet79886132013-11-23 17:19:00 -08002944 flush_dcache_page(bvec.bv_page);
Ilya Loginov2d4dc892009-11-26 09:16:19 +01002945}
2946EXPORT_SYMBOL_GPL(rq_flush_dcache_pages);
2947#endif
2948
Kiyoshi Uedaef9e3fa2008-10-01 16:12:15 +02002949/**
2950 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
2951 * @q : the queue of the device being checked
2952 *
2953 * Description:
2954 * Check if underlying low-level drivers of a device are busy.
2955 * If the drivers want to export their busy state, they must set own
2956 * exporting function using blk_queue_lld_busy() first.
2957 *
2958 * Basically, this function is used only by request stacking drivers
2959 * to stop dispatching requests to underlying devices when underlying
2960 * devices are busy. This behavior helps more I/O merging on the queue
2961 * of the request stacking driver and prevents I/O throughput regression
2962 * on burst I/O load.
2963 *
2964 * Return:
2965 * 0 - Not busy (The request stacking driver should dispatch request)
2966 * 1 - Busy (The request stacking driver should stop dispatching request)
2967 */
2968int blk_lld_busy(struct request_queue *q)
2969{
2970 if (q->lld_busy_fn)
2971 return q->lld_busy_fn(q);
2972
2973 return 0;
2974}
2975EXPORT_SYMBOL_GPL(blk_lld_busy);
2976
Mike Snitzer78d8e582015-06-26 10:01:13 -04002977/**
2978 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
2979 * @rq: the clone request to be cleaned up
2980 *
2981 * Description:
2982 * Free all bios in @rq for a cloned request.
2983 */
2984void blk_rq_unprep_clone(struct request *rq)
2985{
2986 struct bio *bio;
2987
2988 while ((bio = rq->bio) != NULL) {
2989 rq->bio = bio->bi_next;
2990
2991 bio_put(bio);
2992 }
2993}
2994EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
2995
2996/*
2997 * Copy attributes of the original request to the clone request.
2998 * The actual data parts (e.g. ->cmd, ->sense) are not copied.
2999 */
3000static void __blk_rq_prep_clone(struct request *dst, struct request *src)
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02003001{
3002 dst->cpu = src->cpu;
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02003003 dst->__sector = blk_rq_pos(src);
3004 dst->__data_len = blk_rq_bytes(src);
3005 dst->nr_phys_segments = src->nr_phys_segments;
3006 dst->ioprio = src->ioprio;
3007 dst->extra_len = src->extra_len;
Mike Snitzer78d8e582015-06-26 10:01:13 -04003008}
3009
3010/**
3011 * blk_rq_prep_clone - Helper function to setup clone request
3012 * @rq: the request to be setup
3013 * @rq_src: original request to be cloned
3014 * @bs: bio_set that bios for clone are allocated from
3015 * @gfp_mask: memory allocation mask for bio
3016 * @bio_ctr: setup function to be called for each clone bio.
3017 * Returns %0 for success, non %0 for failure.
3018 * @data: private data to be passed to @bio_ctr
3019 *
3020 * Description:
3021 * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
3022 * The actual data parts of @rq_src (e.g. ->cmd, ->sense)
3023 * are not copied, and copying such parts is the caller's responsibility.
3024 * Also, pages which the original bios are pointing to are not copied
3025 * and the cloned bios just point same pages.
3026 * So cloned bios must be completed before original bios, which means
3027 * the caller must complete @rq before @rq_src.
3028 */
3029int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
3030 struct bio_set *bs, gfp_t gfp_mask,
3031 int (*bio_ctr)(struct bio *, struct bio *, void *),
3032 void *data)
3033{
3034 struct bio *bio, *bio_src;
3035
3036 if (!bs)
3037 bs = fs_bio_set;
3038
3039 __rq_for_each_bio(bio_src, rq_src) {
3040 bio = bio_clone_fast(bio_src, gfp_mask, bs);
3041 if (!bio)
3042 goto free_and_out;
3043
3044 if (bio_ctr && bio_ctr(bio, bio_src, data))
3045 goto free_and_out;
3046
3047 if (rq->bio) {
3048 rq->biotail->bi_next = bio;
3049 rq->biotail = bio;
3050 } else
3051 rq->bio = rq->biotail = bio;
3052 }
3053
3054 __blk_rq_prep_clone(rq, rq_src);
3055
3056 return 0;
3057
3058free_and_out:
3059 if (bio)
3060 bio_put(bio);
3061 blk_rq_unprep_clone(rq);
3062
3063 return -ENOMEM;
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02003064}
3065EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
3066
Jens Axboe59c3d452014-04-08 09:15:35 -06003067int kblockd_schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068{
3069 return queue_work(kblockd_workqueue, work);
3070}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071EXPORT_SYMBOL(kblockd_schedule_work);
3072
Jens Axboeee63cfa2016-08-24 15:52:48 -06003073int kblockd_schedule_work_on(int cpu, struct work_struct *work)
3074{
3075 return queue_work_on(cpu, kblockd_workqueue, work);
3076}
3077EXPORT_SYMBOL(kblockd_schedule_work_on);
3078
Jens Axboe59c3d452014-04-08 09:15:35 -06003079int kblockd_schedule_delayed_work(struct delayed_work *dwork,
3080 unsigned long delay)
Vivek Goyale43473b2010-09-15 17:06:35 -04003081{
3082 return queue_delayed_work(kblockd_workqueue, dwork, delay);
3083}
3084EXPORT_SYMBOL(kblockd_schedule_delayed_work);
3085
Jens Axboe8ab14592014-04-08 09:17:40 -06003086int kblockd_schedule_delayed_work_on(int cpu, struct delayed_work *dwork,
3087 unsigned long delay)
3088{
3089 return queue_delayed_work_on(cpu, kblockd_workqueue, dwork, delay);
3090}
3091EXPORT_SYMBOL(kblockd_schedule_delayed_work_on);
3092
Suresh Jayaraman75df7132011-09-21 10:00:16 +02003093/**
3094 * blk_start_plug - initialize blk_plug and track it inside the task_struct
3095 * @plug: The &struct blk_plug that needs to be initialized
3096 *
3097 * Description:
3098 * Tracking blk_plug inside the task_struct will help with auto-flushing the
3099 * pending I/O should the task end up blocking between blk_start_plug() and
3100 * blk_finish_plug(). This is important from a performance perspective, but
3101 * also ensures that we don't deadlock. For instance, if the task is blocking
3102 * for a memory allocation, memory reclaim could end up wanting to free a
3103 * page belonging to that request that is currently residing in our private
3104 * plug. By flushing the pending I/O when the process goes to sleep, we avoid
3105 * this kind of deadlock.
3106 */
Jens Axboe73c10102011-03-08 13:19:51 +01003107void blk_start_plug(struct blk_plug *plug)
3108{
3109 struct task_struct *tsk = current;
3110
Shaohua Lidd6cf3e2015-05-08 10:51:28 -07003111 /*
3112 * If this is a nested plug, don't actually assign it.
3113 */
3114 if (tsk->plug)
3115 return;
3116
Jens Axboe73c10102011-03-08 13:19:51 +01003117 INIT_LIST_HEAD(&plug->list);
Jens Axboe320ae512013-10-24 09:20:05 +01003118 INIT_LIST_HEAD(&plug->mq_list);
NeilBrown048c9372011-04-18 09:52:22 +02003119 INIT_LIST_HEAD(&plug->cb_list);
Jens Axboe73c10102011-03-08 13:19:51 +01003120 /*
Shaohua Lidd6cf3e2015-05-08 10:51:28 -07003121 * Store ordering should not be needed here, since a potential
3122 * preempt will imply a full memory barrier
Jens Axboe73c10102011-03-08 13:19:51 +01003123 */
Shaohua Lidd6cf3e2015-05-08 10:51:28 -07003124 tsk->plug = plug;
Jens Axboe73c10102011-03-08 13:19:51 +01003125}
3126EXPORT_SYMBOL(blk_start_plug);
3127
3128static int plug_rq_cmp(void *priv, struct list_head *a, struct list_head *b)
3129{
3130 struct request *rqa = container_of(a, struct request, queuelist);
3131 struct request *rqb = container_of(b, struct request, queuelist);
3132
Jianpeng Ma975927b2012-10-25 21:58:17 +02003133 return !(rqa->q < rqb->q ||
3134 (rqa->q == rqb->q && blk_rq_pos(rqa) < blk_rq_pos(rqb)));
Jens Axboe73c10102011-03-08 13:19:51 +01003135}
3136
Jens Axboe49cac012011-04-16 13:51:05 +02003137/*
3138 * If 'from_schedule' is true, then postpone the dispatch of requests
3139 * until a safe kblockd context. We due this to avoid accidental big
3140 * additional stack usage in driver dispatch, in places where the originally
3141 * plugger did not intend it.
3142 */
Jens Axboef6603782011-04-15 15:49:07 +02003143static void queue_unplugged(struct request_queue *q, unsigned int depth,
Jens Axboe49cac012011-04-16 13:51:05 +02003144 bool from_schedule)
Jens Axboe99e22592011-04-18 09:59:55 +02003145 __releases(q->queue_lock)
Jens Axboe94b5eb22011-04-12 10:12:19 +02003146{
Jens Axboe49cac012011-04-16 13:51:05 +02003147 trace_block_unplug(q, depth, !from_schedule);
Jens Axboe99e22592011-04-18 09:59:55 +02003148
Bart Van Assche70460572012-11-28 13:45:56 +01003149 if (from_schedule)
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003150 blk_run_queue_async(q);
Bart Van Assche70460572012-11-28 13:45:56 +01003151 else
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003152 __blk_run_queue(q);
Bart Van Assche70460572012-11-28 13:45:56 +01003153 spin_unlock(q->queue_lock);
Jens Axboe94b5eb22011-04-12 10:12:19 +02003154}
3155
NeilBrown74018dc2012-07-31 09:08:15 +02003156static void flush_plug_callbacks(struct blk_plug *plug, bool from_schedule)
NeilBrown048c9372011-04-18 09:52:22 +02003157{
3158 LIST_HEAD(callbacks);
3159
Shaohua Li2a7d5552012-07-31 09:08:15 +02003160 while (!list_empty(&plug->cb_list)) {
3161 list_splice_init(&plug->cb_list, &callbacks);
NeilBrown048c9372011-04-18 09:52:22 +02003162
Shaohua Li2a7d5552012-07-31 09:08:15 +02003163 while (!list_empty(&callbacks)) {
3164 struct blk_plug_cb *cb = list_first_entry(&callbacks,
NeilBrown048c9372011-04-18 09:52:22 +02003165 struct blk_plug_cb,
3166 list);
Shaohua Li2a7d5552012-07-31 09:08:15 +02003167 list_del(&cb->list);
NeilBrown74018dc2012-07-31 09:08:15 +02003168 cb->callback(cb, from_schedule);
Shaohua Li2a7d5552012-07-31 09:08:15 +02003169 }
NeilBrown048c9372011-04-18 09:52:22 +02003170 }
3171}
3172
NeilBrown9cbb1752012-07-31 09:08:14 +02003173struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug, void *data,
3174 int size)
3175{
3176 struct blk_plug *plug = current->plug;
3177 struct blk_plug_cb *cb;
3178
3179 if (!plug)
3180 return NULL;
3181
3182 list_for_each_entry(cb, &plug->cb_list, list)
3183 if (cb->callback == unplug && cb->data == data)
3184 return cb;
3185
3186 /* Not currently on the callback list */
3187 BUG_ON(size < sizeof(*cb));
3188 cb = kzalloc(size, GFP_ATOMIC);
3189 if (cb) {
3190 cb->data = data;
3191 cb->callback = unplug;
3192 list_add(&cb->list, &plug->cb_list);
3193 }
3194 return cb;
3195}
3196EXPORT_SYMBOL(blk_check_plugged);
3197
Jens Axboe49cac012011-04-16 13:51:05 +02003198void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
Jens Axboe73c10102011-03-08 13:19:51 +01003199{
3200 struct request_queue *q;
3201 unsigned long flags;
3202 struct request *rq;
NeilBrown109b8122011-04-11 14:13:10 +02003203 LIST_HEAD(list);
Jens Axboe94b5eb22011-04-12 10:12:19 +02003204 unsigned int depth;
Jens Axboe73c10102011-03-08 13:19:51 +01003205
NeilBrown74018dc2012-07-31 09:08:15 +02003206 flush_plug_callbacks(plug, from_schedule);
Jens Axboe320ae512013-10-24 09:20:05 +01003207
3208 if (!list_empty(&plug->mq_list))
3209 blk_mq_flush_plug_list(plug, from_schedule);
3210
Jens Axboe73c10102011-03-08 13:19:51 +01003211 if (list_empty(&plug->list))
3212 return;
3213
NeilBrown109b8122011-04-11 14:13:10 +02003214 list_splice_init(&plug->list, &list);
3215
Jianpeng Ma422765c2013-01-11 14:46:09 +01003216 list_sort(NULL, &list, plug_rq_cmp);
Jens Axboe73c10102011-03-08 13:19:51 +01003217
3218 q = NULL;
Jens Axboe94b5eb22011-04-12 10:12:19 +02003219 depth = 0;
Jens Axboe188112722011-04-12 10:11:24 +02003220
3221 /*
3222 * Save and disable interrupts here, to avoid doing it for every
3223 * queue lock we have to take.
3224 */
Jens Axboe73c10102011-03-08 13:19:51 +01003225 local_irq_save(flags);
NeilBrown109b8122011-04-11 14:13:10 +02003226 while (!list_empty(&list)) {
3227 rq = list_entry_rq(list.next);
Jens Axboe73c10102011-03-08 13:19:51 +01003228 list_del_init(&rq->queuelist);
Jens Axboe73c10102011-03-08 13:19:51 +01003229 BUG_ON(!rq->q);
3230 if (rq->q != q) {
Jens Axboe99e22592011-04-18 09:59:55 +02003231 /*
3232 * This drops the queue lock
3233 */
3234 if (q)
Jens Axboe49cac012011-04-16 13:51:05 +02003235 queue_unplugged(q, depth, from_schedule);
Jens Axboe73c10102011-03-08 13:19:51 +01003236 q = rq->q;
Jens Axboe94b5eb22011-04-12 10:12:19 +02003237 depth = 0;
Jens Axboe73c10102011-03-08 13:19:51 +01003238 spin_lock(q->queue_lock);
3239 }
Tejun Heo8ba61432011-12-14 00:33:37 +01003240
3241 /*
3242 * Short-circuit if @q is dead
3243 */
Bart Van Assche3f3299d2012-11-28 13:42:38 +01003244 if (unlikely(blk_queue_dying(q))) {
Tejun Heo8ba61432011-12-14 00:33:37 +01003245 __blk_end_request_all(rq, -ENODEV);
3246 continue;
3247 }
3248
Jens Axboe73c10102011-03-08 13:19:51 +01003249 /*
3250 * rq is already accounted, so use raw insert
3251 */
Christoph Hellwigf73f44e2017-01-27 08:30:47 -07003252 if (op_is_flush(rq->cmd_flags))
Jens Axboe401a18e2011-03-25 16:57:52 +01003253 __elv_add_request(q, rq, ELEVATOR_INSERT_FLUSH);
3254 else
3255 __elv_add_request(q, rq, ELEVATOR_INSERT_SORT_MERGE);
Jens Axboe94b5eb22011-04-12 10:12:19 +02003256
3257 depth++;
Jens Axboe73c10102011-03-08 13:19:51 +01003258 }
3259
Jens Axboe99e22592011-04-18 09:59:55 +02003260 /*
3261 * This drops the queue lock
3262 */
3263 if (q)
Jens Axboe49cac012011-04-16 13:51:05 +02003264 queue_unplugged(q, depth, from_schedule);
Jens Axboe73c10102011-03-08 13:19:51 +01003265
Jens Axboe73c10102011-03-08 13:19:51 +01003266 local_irq_restore(flags);
3267}
Jens Axboe73c10102011-03-08 13:19:51 +01003268
3269void blk_finish_plug(struct blk_plug *plug)
3270{
Shaohua Lidd6cf3e2015-05-08 10:51:28 -07003271 if (plug != current->plug)
3272 return;
Jens Axboef6603782011-04-15 15:49:07 +02003273 blk_flush_plug_list(plug, false);
Christoph Hellwig88b996c2011-04-15 15:20:10 +02003274
Shaohua Lidd6cf3e2015-05-08 10:51:28 -07003275 current->plug = NULL;
Jens Axboe73c10102011-03-08 13:19:51 +01003276}
3277EXPORT_SYMBOL(blk_finish_plug);
3278
Rafael J. Wysocki47fafbc2014-12-04 01:00:23 +01003279#ifdef CONFIG_PM
Lin Ming6c954662013-03-23 11:42:26 +08003280/**
3281 * blk_pm_runtime_init - Block layer runtime PM initialization routine
3282 * @q: the queue of the device
3283 * @dev: the device the queue belongs to
3284 *
3285 * Description:
3286 * Initialize runtime-PM-related fields for @q and start auto suspend for
3287 * @dev. Drivers that want to take advantage of request-based runtime PM
3288 * should call this function after @dev has been initialized, and its
3289 * request queue @q has been allocated, and runtime PM for it can not happen
3290 * yet(either due to disabled/forbidden or its usage_count > 0). In most
3291 * cases, driver should call this function before any I/O has taken place.
3292 *
3293 * This function takes care of setting up using auto suspend for the device,
3294 * the autosuspend delay is set to -1 to make runtime suspend impossible
3295 * until an updated value is either set by user or by driver. Drivers do
3296 * not need to touch other autosuspend settings.
3297 *
3298 * The block layer runtime PM is request based, so only works for drivers
3299 * that use request as their IO unit instead of those directly use bio's.
3300 */
3301void blk_pm_runtime_init(struct request_queue *q, struct device *dev)
3302{
3303 q->dev = dev;
3304 q->rpm_status = RPM_ACTIVE;
3305 pm_runtime_set_autosuspend_delay(q->dev, -1);
3306 pm_runtime_use_autosuspend(q->dev);
3307}
3308EXPORT_SYMBOL(blk_pm_runtime_init);
3309
3310/**
3311 * blk_pre_runtime_suspend - Pre runtime suspend check
3312 * @q: the queue of the device
3313 *
3314 * Description:
3315 * This function will check if runtime suspend is allowed for the device
3316 * by examining if there are any requests pending in the queue. If there
3317 * are requests pending, the device can not be runtime suspended; otherwise,
3318 * the queue's status will be updated to SUSPENDING and the driver can
3319 * proceed to suspend the device.
3320 *
3321 * For the not allowed case, we mark last busy for the device so that
3322 * runtime PM core will try to autosuspend it some time later.
3323 *
3324 * This function should be called near the start of the device's
3325 * runtime_suspend callback.
3326 *
3327 * Return:
3328 * 0 - OK to runtime suspend the device
3329 * -EBUSY - Device should not be runtime suspended
3330 */
3331int blk_pre_runtime_suspend(struct request_queue *q)
3332{
3333 int ret = 0;
3334
Ken Xue4fd41a852015-12-01 14:45:46 +08003335 if (!q->dev)
3336 return ret;
3337
Lin Ming6c954662013-03-23 11:42:26 +08003338 spin_lock_irq(q->queue_lock);
3339 if (q->nr_pending) {
3340 ret = -EBUSY;
3341 pm_runtime_mark_last_busy(q->dev);
3342 } else {
3343 q->rpm_status = RPM_SUSPENDING;
3344 }
3345 spin_unlock_irq(q->queue_lock);
3346 return ret;
3347}
3348EXPORT_SYMBOL(blk_pre_runtime_suspend);
3349
3350/**
3351 * blk_post_runtime_suspend - Post runtime suspend processing
3352 * @q: the queue of the device
3353 * @err: return value of the device's runtime_suspend function
3354 *
3355 * Description:
3356 * Update the queue's runtime status according to the return value of the
3357 * device's runtime suspend function and mark last busy for the device so
3358 * that PM core will try to auto suspend the device at a later time.
3359 *
3360 * This function should be called near the end of the device's
3361 * runtime_suspend callback.
3362 */
3363void blk_post_runtime_suspend(struct request_queue *q, int err)
3364{
Ken Xue4fd41a852015-12-01 14:45:46 +08003365 if (!q->dev)
3366 return;
3367
Lin Ming6c954662013-03-23 11:42:26 +08003368 spin_lock_irq(q->queue_lock);
3369 if (!err) {
3370 q->rpm_status = RPM_SUSPENDED;
3371 } else {
3372 q->rpm_status = RPM_ACTIVE;
3373 pm_runtime_mark_last_busy(q->dev);
3374 }
3375 spin_unlock_irq(q->queue_lock);
3376}
3377EXPORT_SYMBOL(blk_post_runtime_suspend);
3378
3379/**
3380 * blk_pre_runtime_resume - Pre runtime resume processing
3381 * @q: the queue of the device
3382 *
3383 * Description:
3384 * Update the queue's runtime status to RESUMING in preparation for the
3385 * runtime resume of the device.
3386 *
3387 * This function should be called near the start of the device's
3388 * runtime_resume callback.
3389 */
3390void blk_pre_runtime_resume(struct request_queue *q)
3391{
Ken Xue4fd41a852015-12-01 14:45:46 +08003392 if (!q->dev)
3393 return;
3394
Lin Ming6c954662013-03-23 11:42:26 +08003395 spin_lock_irq(q->queue_lock);
3396 q->rpm_status = RPM_RESUMING;
3397 spin_unlock_irq(q->queue_lock);
3398}
3399EXPORT_SYMBOL(blk_pre_runtime_resume);
3400
3401/**
3402 * blk_post_runtime_resume - Post runtime resume processing
3403 * @q: the queue of the device
3404 * @err: return value of the device's runtime_resume function
3405 *
3406 * Description:
3407 * Update the queue's runtime status according to the return value of the
3408 * device's runtime_resume function. If it is successfully resumed, process
3409 * the requests that are queued into the device's queue when it is resuming
3410 * and then mark last busy and initiate autosuspend for it.
3411 *
3412 * This function should be called near the end of the device's
3413 * runtime_resume callback.
3414 */
3415void blk_post_runtime_resume(struct request_queue *q, int err)
3416{
Ken Xue4fd41a852015-12-01 14:45:46 +08003417 if (!q->dev)
3418 return;
3419
Lin Ming6c954662013-03-23 11:42:26 +08003420 spin_lock_irq(q->queue_lock);
3421 if (!err) {
3422 q->rpm_status = RPM_ACTIVE;
3423 __blk_run_queue(q);
3424 pm_runtime_mark_last_busy(q->dev);
Aaron Luc60855c2013-05-17 15:47:20 +08003425 pm_request_autosuspend(q->dev);
Lin Ming6c954662013-03-23 11:42:26 +08003426 } else {
3427 q->rpm_status = RPM_SUSPENDED;
3428 }
3429 spin_unlock_irq(q->queue_lock);
3430}
3431EXPORT_SYMBOL(blk_post_runtime_resume);
Mika Westerbergd07ab6d2016-02-18 10:54:11 +02003432
3433/**
3434 * blk_set_runtime_active - Force runtime status of the queue to be active
3435 * @q: the queue of the device
3436 *
3437 * If the device is left runtime suspended during system suspend the resume
3438 * hook typically resumes the device and corrects runtime status
3439 * accordingly. However, that does not affect the queue runtime PM status
3440 * which is still "suspended". This prevents processing requests from the
3441 * queue.
3442 *
3443 * This function can be used in driver's resume hook to correct queue
3444 * runtime PM status and re-enable peeking requests from the queue. It
3445 * should be called before first request is added to the queue.
3446 */
3447void blk_set_runtime_active(struct request_queue *q)
3448{
3449 spin_lock_irq(q->queue_lock);
3450 q->rpm_status = RPM_ACTIVE;
3451 pm_runtime_mark_last_busy(q->dev);
3452 pm_request_autosuspend(q->dev);
3453 spin_unlock_irq(q->queue_lock);
3454}
3455EXPORT_SYMBOL(blk_set_runtime_active);
Lin Ming6c954662013-03-23 11:42:26 +08003456#endif
3457
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458int __init blk_dev_init(void)
3459{
Christoph Hellwigef295ec2016-10-28 08:48:16 -06003460 BUILD_BUG_ON(REQ_OP_LAST >= (1 << REQ_OP_BITS));
3461 BUILD_BUG_ON(REQ_OP_BITS + REQ_FLAG_BITS > 8 *
Maninder Singh0762b232015-07-07 12:41:07 +05303462 FIELD_SIZEOF(struct request, cmd_flags));
Christoph Hellwigef295ec2016-10-28 08:48:16 -06003463 BUILD_BUG_ON(REQ_OP_BITS + REQ_FLAG_BITS > 8 *
3464 FIELD_SIZEOF(struct bio, bi_opf));
Nikanth Karthikesan9eb55b02009-04-27 14:53:54 +02003465
Tejun Heo89b90be2011-01-03 15:01:47 +01003466 /* used for unplugging and affects IO latency/throughput - HIGHPRI */
3467 kblockd_workqueue = alloc_workqueue("kblockd",
Matias Bjørling28747fc2014-06-11 23:43:54 +02003468 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 if (!kblockd_workqueue)
3470 panic("Failed to create kblockd\n");
3471
3472 request_cachep = kmem_cache_create("blkdev_requests",
Paul Mundt20c2df82007-07-20 10:11:58 +09003473 sizeof(struct request), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474
Ilya Dryomovc2789bd2015-11-20 22:16:46 +01003475 blk_requestq_cachep = kmem_cache_create("request_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02003476 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477
Omar Sandoval18fbda92017-01-31 14:53:20 -08003478#ifdef CONFIG_DEBUG_FS
3479 blk_debugfs_root = debugfs_create_dir("block", NULL);
3480#endif
3481
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 return 0;
3483}