blob: 8bf8d520982dd5bb25dc8a317b3fd7749a6e99f9 [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 Heofcd58022015-05-22 17:13:17 -040035#include <linux/blk-cgroup.h>
Li Zefan55782132009-06-09 13:43:05 +080036
37#define CREATE_TRACE_POINTS
38#include <trace/events/block.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Jens Axboe8324aa92008-01-29 14:51:59 +010040#include "blk.h"
Ming Lei43a5e4e2013-12-26 21:31:35 +080041#include "blk-mq.h"
Jens Axboe8324aa92008-01-29 14:51:59 +010042
Mike Snitzerd07335e2010-11-16 12:52:38 +010043EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_remap);
Jun'ichi Nomurab0da3f0d2009-10-01 21:16:13 +020044EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_remap);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -070045EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_complete);
Keith Busch3291fa52014-04-28 12:30:52 -060046EXPORT_TRACEPOINT_SYMBOL_GPL(block_split);
NeilBrowncbae8d42012-12-14 20:49:27 +010047EXPORT_TRACEPOINT_SYMBOL_GPL(block_unplug);
Ingo Molnar0bfc2452008-11-26 11:59:56 +010048
Tejun Heoa73f7302011-12-14 00:33:37 +010049DEFINE_IDA(blk_queue_ida);
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
52 * For the allocated request tables
53 */
Jens Axboe320ae512013-10-24 09:20:05 +010054struct kmem_cache *request_cachep = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/*
57 * For queue allocation
58 */
Jens Axboe6728cb02008-01-31 13:03:55 +010059struct kmem_cache *blk_requestq_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 * Controlling structure to kblockd
63 */
Jens Axboeff856ba2006-01-09 16:02:34 +010064static struct workqueue_struct *kblockd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Tejun Heoc18a2202015-05-22 17:13:42 -040066static void blk_clear_congested(struct request_list *rl, int sync)
67{
Tejun Heoc18a2202015-05-22 17:13:42 -040068#ifdef CONFIG_CGROUP_WRITEBACK
69 clear_wb_congested(rl->blkg->wb_congested, sync);
70#else
Tejun Heoe5e48942015-05-22 17:13:43 -040071 /*
72 * If !CGROUP_WRITEBACK, all blkg's map to bdi->wb and we shouldn't
73 * flip its congestion state for events on other blkcgs.
74 */
75 if (rl == &rl->q->root_rl)
76 clear_wb_congested(rl->q->backing_dev_info.wb.congested, sync);
Tejun Heoc18a2202015-05-22 17:13:42 -040077#endif
78}
79
80static void blk_set_congested(struct request_list *rl, int sync)
81{
Tejun Heoc18a2202015-05-22 17:13:42 -040082#ifdef CONFIG_CGROUP_WRITEBACK
83 set_wb_congested(rl->blkg->wb_congested, sync);
84#else
Tejun Heoe5e48942015-05-22 17:13:43 -040085 /* see blk_clear_congested() */
86 if (rl == &rl->q->root_rl)
87 set_wb_congested(rl->q->backing_dev_info.wb.congested, sync);
Tejun Heoc18a2202015-05-22 17:13:42 -040088#endif
89}
90
Jens Axboe8324aa92008-01-29 14:51:59 +010091void blk_queue_congestion_threshold(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 int nr;
94
95 nr = q->nr_requests - (q->nr_requests / 8) + 1;
96 if (nr > q->nr_requests)
97 nr = q->nr_requests;
98 q->nr_congestion_on = nr;
99
100 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
101 if (nr < 1)
102 nr = 1;
103 q->nr_congestion_off = nr;
104}
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106/**
107 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
108 * @bdev: device
109 *
110 * Locates the passed device's request queue and returns the address of its
Tejun Heoff9ea322014-09-08 08:03:56 +0900111 * backing_dev_info. This function can only be called if @bdev is opened
112 * and the return value is never NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 */
114struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
115{
Jens Axboe165125e2007-07-24 09:28:11 +0200116 struct request_queue *q = bdev_get_queue(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Tejun Heoff9ea322014-09-08 08:03:56 +0900118 return &q->backing_dev_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120EXPORT_SYMBOL(blk_get_backing_dev_info);
121
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200122void blk_rq_init(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200124 memset(rq, 0, sizeof(*rq));
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 INIT_LIST_HEAD(&rq->queuelist);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700127 INIT_LIST_HEAD(&rq->timeout_list);
Jens Axboec7c22e42008-09-13 20:26:01 +0200128 rq->cpu = -1;
Jens Axboe63a71382008-02-08 12:41:03 +0100129 rq->q = q;
Tejun Heoa2dec7b2009-05-07 22:24:44 +0900130 rq->__sector = (sector_t) -1;
Jens Axboe2e662b62006-07-13 11:55:04 +0200131 INIT_HLIST_NODE(&rq->hash);
132 RB_CLEAR_NODE(&rq->rb_node);
FUJITA Tomonorid7e3c322008-04-29 09:54:39 +0200133 rq->cmd = rq->__cmd;
Li Zefane2494e12009-04-02 13:43:26 +0800134 rq->cmd_len = BLK_MAX_CDB;
Jens Axboe63a71382008-02-08 12:41:03 +0100135 rq->tag = -1;
Tejun Heob243ddc2009-04-23 11:05:18 +0900136 rq->start_time = jiffies;
Divyesh Shah91952912010-04-01 15:01:41 -0700137 set_start_time_ns(rq);
Jerome Marchand09e099d2011-01-05 16:57:38 +0100138 rq->part = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200140EXPORT_SYMBOL(blk_rq_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
NeilBrown5bb23a62007-09-27 12:46:13 +0200142static void req_bio_endio(struct request *rq, struct bio *bio,
143 unsigned int nbytes, int error)
Tejun Heo797e7db2006-01-06 09:51:03 +0100144{
Tejun Heo143a87f2011-01-25 12:43:52 +0100145 if (error)
146 clear_bit(BIO_UPTODATE, &bio->bi_flags);
147 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
148 error = -EIO;
Tejun Heo797e7db2006-01-06 09:51:03 +0100149
Tejun Heo143a87f2011-01-25 12:43:52 +0100150 if (unlikely(rq->cmd_flags & REQ_QUIET))
151 set_bit(BIO_QUIET, &bio->bi_flags);
152
Kent Overstreetf79ea412012-09-20 16:38:30 -0700153 bio_advance(bio, nbytes);
Tejun Heo143a87f2011-01-25 12:43:52 +0100154
155 /* don't actually finish bio if it's part of flush sequence */
Kent Overstreet4f024f32013-10-11 15:44:27 -0700156 if (bio->bi_iter.bi_size == 0 && !(rq->cmd_flags & REQ_FLUSH_SEQ))
Tejun Heo143a87f2011-01-25 12:43:52 +0100157 bio_endio(bio, error);
Tejun Heo797e7db2006-01-06 09:51:03 +0100158}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160void blk_dump_rq_flags(struct request *rq, char *msg)
161{
162 int bit;
163
Jens Axboe59533162013-05-23 12:25:08 +0200164 printk(KERN_INFO "%s: dev %s: type=%x, flags=%llx\n", msg,
Jens Axboe4aff5e22006-08-10 08:44:47 +0200165 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
Jens Axboe59533162013-05-23 12:25:08 +0200166 (unsigned long long) rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Tejun Heo83096eb2009-05-07 22:24:39 +0900168 printk(KERN_INFO " sector %llu, nr/cnr %u/%u\n",
169 (unsigned long long)blk_rq_pos(rq),
170 blk_rq_sectors(rq), blk_rq_cur_sectors(rq));
Jens Axboeb4f42e22014-04-10 09:46:28 -0600171 printk(KERN_INFO " bio %p, biotail %p, len %u\n",
172 rq->bio, rq->biotail, blk_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200174 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
Jens Axboe6728cb02008-01-31 13:03:55 +0100175 printk(KERN_INFO " cdb: ");
FUJITA Tomonorid34c87e2008-04-29 14:37:52 +0200176 for (bit = 0; bit < BLK_MAX_CDB; bit++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 printk("%02x ", rq->cmd[bit]);
178 printk("\n");
179 }
180}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181EXPORT_SYMBOL(blk_dump_rq_flags);
182
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500183static void blk_delay_work(struct work_struct *work)
Jens Axboe6c5e0c42008-08-01 20:31:32 +0200184{
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500185 struct request_queue *q;
Jens Axboe6c5e0c42008-08-01 20:31:32 +0200186
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500187 q = container_of(work, struct request_queue, delay_work.work);
188 spin_lock_irq(q->queue_lock);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200189 __blk_run_queue(q);
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500190 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193/**
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500194 * blk_delay_queue - restart queueing after defined interval
195 * @q: The &struct request_queue in question
196 * @msecs: Delay in msecs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 *
198 * Description:
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500199 * Sometimes queueing needs to be postponed for a little while, to allow
200 * resources to come back. This function will make sure that queueing is
Bart Van Assche70460572012-11-28 13:45:56 +0100201 * restarted around the specified time. Queue lock must be held.
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500202 */
203void blk_delay_queue(struct request_queue *q, unsigned long msecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Bart Van Assche70460572012-11-28 13:45:56 +0100205 if (likely(!blk_queue_dead(q)))
206 queue_delayed_work(kblockd_workqueue, &q->delay_work,
207 msecs_to_jiffies(msecs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500209EXPORT_SYMBOL(blk_delay_queue);
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/**
212 * blk_start_queue - restart a previously stopped queue
Jens Axboe165125e2007-07-24 09:28:11 +0200213 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 *
215 * Description:
216 * blk_start_queue() will clear the stop flag on the queue, and call
217 * the request_fn for the queue if it was in a stopped state when
218 * entered. Also see blk_stop_queue(). Queue lock must be held.
219 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200220void blk_start_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200222 WARN_ON(!irqs_disabled());
223
Nick Piggin75ad23b2008-04-29 14:48:33 +0200224 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200225 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227EXPORT_SYMBOL(blk_start_queue);
228
229/**
230 * blk_stop_queue - stop a queue
Jens Axboe165125e2007-07-24 09:28:11 +0200231 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 *
233 * Description:
234 * The Linux block layer assumes that a block driver will consume all
235 * entries on the request queue when the request_fn strategy is called.
236 * Often this will not happen, because of hardware limitations (queue
237 * depth settings). If a device driver gets a 'queue full' response,
238 * or if it simply chooses not to queue more I/O at one point, it can
239 * call this function to prevent the request_fn from being called until
240 * the driver has signalled it's ready to go again. This happens by calling
241 * blk_start_queue() to restart queue operations. Queue lock must be held.
242 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200243void blk_stop_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Tejun Heo136b5722012-08-21 13:18:24 -0700245 cancel_delayed_work(&q->delay_work);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200246 queue_flag_set(QUEUE_FLAG_STOPPED, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248EXPORT_SYMBOL(blk_stop_queue);
249
250/**
251 * blk_sync_queue - cancel any pending callbacks on a queue
252 * @q: the queue
253 *
254 * Description:
255 * The block layer may perform asynchronous callback activity
256 * on a queue, such as calling the unplug function after a timeout.
257 * A block device may call blk_sync_queue to ensure that any
258 * such activity is cancelled, thus allowing it to release resources
Michael Opdenacker59c51592007-05-09 08:57:56 +0200259 * that the callbacks might use. The caller must already have made sure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 * that its ->make_request_fn will not re-add plugging prior to calling
261 * this function.
262 *
Vivek Goyalda527772011-03-02 19:05:33 -0500263 * This function does not cancel any asynchronous activity arising
Masanari Iidada3dae52014-09-09 01:27:23 +0900264 * out of elevator or throttling code. That would require elevator_exit()
Tejun Heo5efd6112012-03-05 13:15:12 -0800265 * and blkcg_exit_queue() to be called with queue lock initialized.
Vivek Goyalda527772011-03-02 19:05:33 -0500266 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 */
268void blk_sync_queue(struct request_queue *q)
269{
Jens Axboe70ed28b2008-11-19 14:38:39 +0100270 del_timer_sync(&q->timeout);
Ming Leif04c1fe2013-12-26 21:31:36 +0800271
272 if (q->mq_ops) {
273 struct blk_mq_hw_ctx *hctx;
274 int i;
275
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600276 queue_for_each_hw_ctx(q, hctx, i) {
277 cancel_delayed_work_sync(&hctx->run_work);
278 cancel_delayed_work_sync(&hctx->delay_work);
279 }
Ming Leif04c1fe2013-12-26 21:31:36 +0800280 } else {
281 cancel_delayed_work_sync(&q->delay_work);
282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284EXPORT_SYMBOL(blk_sync_queue);
285
286/**
Bart Van Asschec246e802012-12-06 14:32:01 +0100287 * __blk_run_queue_uncond - run a queue whether or not it has been stopped
288 * @q: The queue to run
289 *
290 * Description:
291 * Invoke request handling on a queue if there are any pending requests.
292 * May be used to restart request handling after a request has completed.
293 * This variant runs the queue whether or not the queue has been
294 * stopped. Must be called with the queue lock held and interrupts
295 * disabled. See also @blk_run_queue.
296 */
297inline void __blk_run_queue_uncond(struct request_queue *q)
298{
299 if (unlikely(blk_queue_dead(q)))
300 return;
301
Bart Van Assche24faf6f2012-11-28 13:46:45 +0100302 /*
303 * Some request_fn implementations, e.g. scsi_request_fn(), unlock
304 * the queue lock internally. As a result multiple threads may be
305 * running such a request function concurrently. Keep track of the
306 * number of active request_fn invocations such that blk_drain_queue()
307 * can wait until all these request_fn calls have finished.
308 */
309 q->request_fn_active++;
Bart Van Asschec246e802012-12-06 14:32:01 +0100310 q->request_fn(q);
Bart Van Assche24faf6f2012-11-28 13:46:45 +0100311 q->request_fn_active--;
Bart Van Asschec246e802012-12-06 14:32:01 +0100312}
313
314/**
Jens Axboe80a4b582008-10-14 09:51:06 +0200315 * __blk_run_queue - run a single device queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200317 *
318 * Description:
319 * See @blk_run_queue. This variant must be called with the queue lock
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200320 * held and interrupts disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 */
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200322void __blk_run_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Tejun Heoa538cd02009-04-23 11:05:17 +0900324 if (unlikely(blk_queue_stopped(q)))
325 return;
326
Bart Van Asschec246e802012-12-06 14:32:01 +0100327 __blk_run_queue_uncond(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200328}
329EXPORT_SYMBOL(__blk_run_queue);
Jens Axboedac07ec2006-05-11 08:20:16 +0200330
Nick Piggin75ad23b2008-04-29 14:48:33 +0200331/**
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200332 * blk_run_queue_async - run a single device queue in workqueue context
333 * @q: The queue to run
334 *
335 * Description:
336 * Tells kblockd to perform the equivalent of @blk_run_queue on behalf
Bart Van Assche70460572012-11-28 13:45:56 +0100337 * of us. The caller must hold the queue lock.
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200338 */
339void blk_run_queue_async(struct request_queue *q)
340{
Bart Van Assche70460572012-11-28 13:45:56 +0100341 if (likely(!blk_queue_stopped(q) && !blk_queue_dead(q)))
Tejun Heoe7c2f962012-08-21 13:18:24 -0700342 mod_delayed_work(kblockd_workqueue, &q->delay_work, 0);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200343}
Jens Axboec21e6be2011-04-19 13:32:46 +0200344EXPORT_SYMBOL(blk_run_queue_async);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200345
346/**
Nick Piggin75ad23b2008-04-29 14:48:33 +0200347 * blk_run_queue - run a single device queue
348 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200349 *
350 * Description:
351 * Invoke request handling on this queue, if it has pending work to do.
Tejun Heoa7f55792009-04-23 11:05:17 +0900352 * May be used to restart queueing when a request has completed.
Nick Piggin75ad23b2008-04-29 14:48:33 +0200353 */
354void blk_run_queue(struct request_queue *q)
355{
356 unsigned long flags;
357
358 spin_lock_irqsave(q->queue_lock, flags);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200359 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 spin_unlock_irqrestore(q->queue_lock, flags);
361}
362EXPORT_SYMBOL(blk_run_queue);
363
Jens Axboe165125e2007-07-24 09:28:11 +0200364void blk_put_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500365{
366 kobject_put(&q->kobj);
367}
Jens Axboed86e0e82011-05-27 07:44:43 +0200368EXPORT_SYMBOL(blk_put_queue);
Al Viro483f4af2006-03-18 18:34:37 -0500369
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200370/**
Bart Van Assche807592a2012-11-28 13:43:38 +0100371 * __blk_drain_queue - drain requests from request_queue
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200372 * @q: queue to drain
Tejun Heoc9a929d2011-10-19 14:42:16 +0200373 * @drain_all: whether to drain all requests or only the ones w/ ELVPRIV
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200374 *
Tejun Heoc9a929d2011-10-19 14:42:16 +0200375 * Drain requests from @q. If @drain_all is set, all requests are drained.
376 * If not, only ELVPRIV requests are drained. The caller is responsible
377 * for ensuring that no new requests which need to be drained are queued.
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200378 */
Bart Van Assche807592a2012-11-28 13:43:38 +0100379static void __blk_drain_queue(struct request_queue *q, bool drain_all)
380 __releases(q->queue_lock)
381 __acquires(q->queue_lock)
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200382{
Asias He458f27a2012-06-15 08:45:25 +0200383 int i;
384
Bart Van Assche807592a2012-11-28 13:43:38 +0100385 lockdep_assert_held(q->queue_lock);
386
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200387 while (true) {
Tejun Heo481a7d62011-12-14 00:33:37 +0100388 bool drain = false;
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200389
Tejun Heob855b042012-03-06 21:24:55 +0100390 /*
391 * The caller might be trying to drain @q before its
392 * elevator is initialized.
393 */
394 if (q->elevator)
395 elv_drain_elevator(q);
396
Tejun Heo5efd6112012-03-05 13:15:12 -0800397 blkcg_drain_queue(q);
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200398
Tejun Heo4eabc942011-12-15 20:03:04 +0100399 /*
400 * This function might be called on a queue which failed
Tejun Heob855b042012-03-06 21:24:55 +0100401 * driver init after queue creation or is not yet fully
402 * active yet. Some drivers (e.g. fd and loop) get unhappy
403 * in such cases. Kick queue iff dispatch queue has
404 * something on it and @q has request_fn set.
Tejun Heo4eabc942011-12-15 20:03:04 +0100405 */
Tejun Heob855b042012-03-06 21:24:55 +0100406 if (!list_empty(&q->queue_head) && q->request_fn)
Tejun Heo4eabc942011-12-15 20:03:04 +0100407 __blk_run_queue(q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200408
Tejun Heo8a5ecdd2012-06-04 20:40:58 -0700409 drain |= q->nr_rqs_elvpriv;
Bart Van Assche24faf6f2012-11-28 13:46:45 +0100410 drain |= q->request_fn_active;
Tejun Heo481a7d62011-12-14 00:33:37 +0100411
412 /*
413 * Unfortunately, requests are queued at and tracked from
414 * multiple places and there's no single counter which can
415 * be drained. Check all the queues and counters.
416 */
417 if (drain_all) {
Ming Leie97c2932014-09-25 23:23:46 +0800418 struct blk_flush_queue *fq = blk_get_flush_queue(q, NULL);
Tejun Heo481a7d62011-12-14 00:33:37 +0100419 drain |= !list_empty(&q->queue_head);
420 for (i = 0; i < 2; i++) {
Tejun Heo8a5ecdd2012-06-04 20:40:58 -0700421 drain |= q->nr_rqs[i];
Tejun Heo481a7d62011-12-14 00:33:37 +0100422 drain |= q->in_flight[i];
Ming Lei7c94e1c2014-09-25 23:23:43 +0800423 if (fq)
424 drain |= !list_empty(&fq->flush_queue[i]);
Tejun Heo481a7d62011-12-14 00:33:37 +0100425 }
426 }
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200427
Tejun Heo481a7d62011-12-14 00:33:37 +0100428 if (!drain)
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200429 break;
Bart Van Assche807592a2012-11-28 13:43:38 +0100430
431 spin_unlock_irq(q->queue_lock);
432
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200433 msleep(10);
Bart Van Assche807592a2012-11-28 13:43:38 +0100434
435 spin_lock_irq(q->queue_lock);
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200436 }
Asias He458f27a2012-06-15 08:45:25 +0200437
438 /*
439 * With queue marked dead, any woken up waiter will fail the
440 * allocation path, so the wakeup chaining is lost and we're
441 * left with hung waiters. We need to wake up those waiters.
442 */
443 if (q->request_fn) {
Tejun Heoa0516612012-06-26 15:05:44 -0700444 struct request_list *rl;
445
Tejun Heoa0516612012-06-26 15:05:44 -0700446 blk_queue_for_each_rl(rl, q)
447 for (i = 0; i < ARRAY_SIZE(rl->wait); i++)
448 wake_up_all(&rl->wait[i]);
Asias He458f27a2012-06-15 08:45:25 +0200449 }
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200450}
451
Tejun Heoc9a929d2011-10-19 14:42:16 +0200452/**
Tejun Heod7325802012-03-05 13:14:58 -0800453 * blk_queue_bypass_start - enter queue bypass mode
454 * @q: queue of interest
455 *
456 * In bypass mode, only the dispatch FIFO queue of @q is used. This
457 * function makes @q enter bypass mode and drains all requests which were
Tejun Heo6ecf23a2012-03-05 13:14:59 -0800458 * throttled or issued before. On return, it's guaranteed that no request
Tejun Heo80fd9972012-04-13 14:50:53 -0700459 * is being throttled or has ELVPRIV set and blk_queue_bypass() %true
460 * inside queue or RCU read lock.
Tejun Heod7325802012-03-05 13:14:58 -0800461 */
462void blk_queue_bypass_start(struct request_queue *q)
463{
464 spin_lock_irq(q->queue_lock);
Tejun Heo776687b2014-07-01 10:29:17 -0600465 q->bypass_depth++;
Tejun Heod7325802012-03-05 13:14:58 -0800466 queue_flag_set(QUEUE_FLAG_BYPASS, q);
467 spin_unlock_irq(q->queue_lock);
468
Tejun Heo776687b2014-07-01 10:29:17 -0600469 /*
470 * Queues start drained. Skip actual draining till init is
471 * complete. This avoids lenghty delays during queue init which
472 * can happen many times during boot.
473 */
474 if (blk_queue_init_done(q)) {
Bart Van Assche807592a2012-11-28 13:43:38 +0100475 spin_lock_irq(q->queue_lock);
476 __blk_drain_queue(q, false);
477 spin_unlock_irq(q->queue_lock);
478
Tejun Heob82d4b12012-04-13 13:11:31 -0700479 /* ensure blk_queue_bypass() is %true inside RCU read lock */
480 synchronize_rcu();
481 }
Tejun Heod7325802012-03-05 13:14:58 -0800482}
483EXPORT_SYMBOL_GPL(blk_queue_bypass_start);
484
485/**
486 * blk_queue_bypass_end - leave queue bypass mode
487 * @q: queue of interest
488 *
489 * Leave bypass mode and restore the normal queueing behavior.
490 */
491void blk_queue_bypass_end(struct request_queue *q)
492{
493 spin_lock_irq(q->queue_lock);
494 if (!--q->bypass_depth)
495 queue_flag_clear(QUEUE_FLAG_BYPASS, q);
496 WARN_ON_ONCE(q->bypass_depth < 0);
497 spin_unlock_irq(q->queue_lock);
498}
499EXPORT_SYMBOL_GPL(blk_queue_bypass_end);
500
Jens Axboeaed3ea92014-12-22 14:04:42 -0700501void blk_set_queue_dying(struct request_queue *q)
502{
503 queue_flag_set_unlocked(QUEUE_FLAG_DYING, q);
504
505 if (q->mq_ops)
506 blk_mq_wake_waiters(q);
507 else {
508 struct request_list *rl;
509
510 blk_queue_for_each_rl(rl, q) {
511 if (rl->rq_pool) {
512 wake_up(&rl->wait[BLK_RW_SYNC]);
513 wake_up(&rl->wait[BLK_RW_ASYNC]);
514 }
515 }
516 }
517}
518EXPORT_SYMBOL_GPL(blk_set_queue_dying);
519
Tejun Heod7325802012-03-05 13:14:58 -0800520/**
Tejun Heoc9a929d2011-10-19 14:42:16 +0200521 * blk_cleanup_queue - shutdown a request queue
522 * @q: request queue to shutdown
523 *
Bart Van Asschec246e802012-12-06 14:32:01 +0100524 * Mark @q DYING, drain all pending requests, mark @q DEAD, destroy and
525 * put it. All future requests will be failed immediately with -ENODEV.
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500526 */
Jens Axboe6728cb02008-01-31 13:03:55 +0100527void blk_cleanup_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500528{
Tejun Heoc9a929d2011-10-19 14:42:16 +0200529 spinlock_t *lock = q->queue_lock;
Jens Axboee3335de2008-09-18 09:22:54 -0700530
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100531 /* mark @q DYING, no new request or merges will be allowed afterwards */
Al Viro483f4af2006-03-18 18:34:37 -0500532 mutex_lock(&q->sysfs_lock);
Jens Axboeaed3ea92014-12-22 14:04:42 -0700533 blk_set_queue_dying(q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200534 spin_lock_irq(lock);
Tejun Heo6ecf23a2012-03-05 13:14:59 -0800535
Tejun Heo80fd9972012-04-13 14:50:53 -0700536 /*
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100537 * A dying queue is permanently in bypass mode till released. Note
Tejun Heo80fd9972012-04-13 14:50:53 -0700538 * that, unlike blk_queue_bypass_start(), we aren't performing
539 * synchronize_rcu() after entering bypass mode to avoid the delay
540 * as some drivers create and destroy a lot of queues while
541 * probing. This is still safe because blk_release_queue() will be
542 * called only after the queue refcnt drops to zero and nothing,
543 * RCU or not, would be traversing the queue by then.
544 */
Tejun Heo6ecf23a2012-03-05 13:14:59 -0800545 q->bypass_depth++;
546 queue_flag_set(QUEUE_FLAG_BYPASS, q);
547
Tejun Heoc9a929d2011-10-19 14:42:16 +0200548 queue_flag_set(QUEUE_FLAG_NOMERGES, q);
549 queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100550 queue_flag_set(QUEUE_FLAG_DYING, q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200551 spin_unlock_irq(lock);
552 mutex_unlock(&q->sysfs_lock);
553
Bart Van Asschec246e802012-12-06 14:32:01 +0100554 /*
555 * Drain all requests queued before DYING marking. Set DEAD flag to
556 * prevent that q->request_fn() gets invoked after draining finished.
557 */
Ming Lei43a5e4e2013-12-26 21:31:35 +0800558 if (q->mq_ops) {
Tejun Heo780db202014-07-01 10:31:13 -0600559 blk_mq_freeze_queue(q);
Ming Lei43a5e4e2013-12-26 21:31:35 +0800560 spin_lock_irq(lock);
561 } else {
562 spin_lock_irq(lock);
563 __blk_drain_queue(q, true);
564 }
Bart Van Asschec246e802012-12-06 14:32:01 +0100565 queue_flag_set(QUEUE_FLAG_DEAD, q);
Bart Van Assche807592a2012-11-28 13:43:38 +0100566 spin_unlock_irq(lock);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200567
568 /* @q won't process any more request, flush async actions */
569 del_timer_sync(&q->backing_dev_info.laptop_mode_wb_timer);
570 blk_sync_queue(q);
571
Bart Van Assche45a9c9d2014-12-09 16:57:48 +0100572 if (q->mq_ops)
573 blk_mq_free_queue(q);
574
Asias He5e5cfac2012-05-24 23:28:52 +0800575 spin_lock_irq(lock);
576 if (q->queue_lock != &q->__queue_lock)
577 q->queue_lock = &q->__queue_lock;
578 spin_unlock_irq(lock);
579
NeilBrown6cd18e72015-04-27 14:12:22 +1000580 bdi_destroy(&q->backing_dev_info);
581
Tejun Heoc9a929d2011-10-19 14:42:16 +0200582 /* @q is and will stay empty, shutdown and put */
Al Viro483f4af2006-03-18 18:34:37 -0500583 blk_put_queue(q);
584}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585EXPORT_SYMBOL(blk_cleanup_queue);
586
David Rientjes271508d2015-03-24 16:21:16 -0700587/* Allocate memory local to the request queue */
588static void *alloc_request_struct(gfp_t gfp_mask, void *data)
589{
590 int nid = (int)(long)data;
591 return kmem_cache_alloc_node(request_cachep, gfp_mask, nid);
592}
593
594static void free_request_struct(void *element, void *unused)
595{
596 kmem_cache_free(request_cachep, element);
597}
598
Tejun Heo5b788ce2012-06-04 20:40:59 -0700599int blk_init_rl(struct request_list *rl, struct request_queue *q,
600 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Mike Snitzer1abec4f2010-05-25 13:15:15 -0400602 if (unlikely(rl->rq_pool))
603 return 0;
604
Tejun Heo5b788ce2012-06-04 20:40:59 -0700605 rl->q = q;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200606 rl->count[BLK_RW_SYNC] = rl->count[BLK_RW_ASYNC] = 0;
607 rl->starved[BLK_RW_SYNC] = rl->starved[BLK_RW_ASYNC] = 0;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200608 init_waitqueue_head(&rl->wait[BLK_RW_SYNC]);
609 init_waitqueue_head(&rl->wait[BLK_RW_ASYNC]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
David Rientjes271508d2015-03-24 16:21:16 -0700611 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, alloc_request_struct,
612 free_request_struct,
613 (void *)(long)q->node, gfp_mask,
614 q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (!rl->rq_pool)
616 return -ENOMEM;
617
618 return 0;
619}
620
Tejun Heo5b788ce2012-06-04 20:40:59 -0700621void blk_exit_rl(struct request_list *rl)
622{
623 if (rl->rq_pool)
624 mempool_destroy(rl->rq_pool);
625}
626
Jens Axboe165125e2007-07-24 09:28:11 +0200627struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Ezequiel Garciac304a512012-11-10 10:39:44 +0100629 return blk_alloc_queue_node(gfp_mask, NUMA_NO_NODE);
Christoph Lameter19460892005-06-23 00:08:19 -0700630}
631EXPORT_SYMBOL(blk_alloc_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Jens Axboe165125e2007-07-24 09:28:11 +0200633struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -0700634{
Jens Axboe165125e2007-07-24 09:28:11 +0200635 struct request_queue *q;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700636 int err;
Christoph Lameter19460892005-06-23 00:08:19 -0700637
Jens Axboe8324aa92008-01-29 14:51:59 +0100638 q = kmem_cache_alloc_node(blk_requestq_cachep,
Christoph Lameter94f60302007-07-17 04:03:29 -0700639 gfp_mask | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (!q)
641 return NULL;
642
Dan Carpenter00380a42012-03-23 09:58:54 +0100643 q->id = ida_simple_get(&blk_queue_ida, 0, 0, gfp_mask);
Tejun Heoa73f7302011-12-14 00:33:37 +0100644 if (q->id < 0)
Ming Lei3d2936f2014-05-27 23:35:14 +0800645 goto fail_q;
Tejun Heoa73f7302011-12-14 00:33:37 +0100646
Jens Axboe0989a022009-06-12 14:42:56 +0200647 q->backing_dev_info.ra_pages =
648 (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
Tejun Heo0632c4c2015-05-22 17:13:36 -0400649 q->backing_dev_info.capabilities = BDI_CAP_CGROUP_WRITEBACK;
Jens Axboed9938312009-06-12 14:45:52 +0200650 q->backing_dev_info.name = "block";
Mike Snitzer51514122011-11-23 10:59:13 +0100651 q->node = node_id;
Jens Axboe0989a022009-06-12 14:42:56 +0200652
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700653 err = bdi_init(&q->backing_dev_info);
Tejun Heoa73f7302011-12-14 00:33:37 +0100654 if (err)
655 goto fail_id;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700656
Matthew Garrett31373d02010-04-06 14:25:14 +0200657 setup_timer(&q->backing_dev_info.laptop_mode_wb_timer,
658 laptop_mode_timer_fn, (unsigned long) q);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700659 setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
Tejun Heob855b042012-03-06 21:24:55 +0100660 INIT_LIST_HEAD(&q->queue_head);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700661 INIT_LIST_HEAD(&q->timeout_list);
Tejun Heoa612fdd2011-12-14 00:33:41 +0100662 INIT_LIST_HEAD(&q->icq_list);
Tejun Heo4eef3042012-03-05 13:15:18 -0800663#ifdef CONFIG_BLK_CGROUP
Tejun Heoe8989fa2012-03-05 13:15:20 -0800664 INIT_LIST_HEAD(&q->blkg_list);
Tejun Heo4eef3042012-03-05 13:15:18 -0800665#endif
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500666 INIT_DELAYED_WORK(&q->delay_work, blk_delay_work);
Al Viro483f4af2006-03-18 18:34:37 -0500667
Jens Axboe8324aa92008-01-29 14:51:59 +0100668 kobject_init(&q->kobj, &blk_queue_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Al Viro483f4af2006-03-18 18:34:37 -0500670 mutex_init(&q->sysfs_lock);
Neil Browne7e72bf2008-05-14 16:05:54 -0700671 spin_lock_init(&q->__queue_lock);
Al Viro483f4af2006-03-18 18:34:37 -0500672
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500673 /*
674 * By default initialize queue_lock to internal lock and driver can
675 * override it later if need be.
676 */
677 q->queue_lock = &q->__queue_lock;
678
Tejun Heob82d4b12012-04-13 13:11:31 -0700679 /*
680 * A queue starts its life with bypass turned on to avoid
681 * unnecessary bypass on/off overhead and nasty surprises during
Tejun Heo749fefe2012-09-20 14:08:52 -0700682 * init. The initial bypass will be finished when the queue is
683 * registered by blk_register_queue().
Tejun Heob82d4b12012-04-13 13:11:31 -0700684 */
685 q->bypass_depth = 1;
686 __set_bit(QUEUE_FLAG_BYPASS, &q->queue_flags);
687
Jens Axboe320ae512013-10-24 09:20:05 +0100688 init_waitqueue_head(&q->mq_freeze_wq);
689
Tejun Heo5efd6112012-03-05 13:15:12 -0800690 if (blkcg_init_queue(q))
Mikulas Patockafff49962013-10-14 12:11:36 -0400691 goto fail_bdi;
Tejun Heof51b8022012-03-05 13:15:05 -0800692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return q;
Tejun Heoa73f7302011-12-14 00:33:37 +0100694
Mikulas Patockafff49962013-10-14 12:11:36 -0400695fail_bdi:
696 bdi_destroy(&q->backing_dev_info);
Tejun Heoa73f7302011-12-14 00:33:37 +0100697fail_id:
698 ida_simple_remove(&blk_queue_ida, q->id);
699fail_q:
700 kmem_cache_free(blk_requestq_cachep, q);
701 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
Christoph Lameter19460892005-06-23 00:08:19 -0700703EXPORT_SYMBOL(blk_alloc_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705/**
706 * blk_init_queue - prepare a request queue for use with a block device
707 * @rfn: The function to be called to process requests that have been
708 * placed on the queue.
709 * @lock: Request queue spin lock
710 *
711 * Description:
712 * If a block device wishes to use the standard request handling procedures,
713 * which sorts requests and coalesces adjacent requests, then it must
714 * call blk_init_queue(). The function @rfn will be called when there
715 * are requests on the queue that need to be processed. If the device
716 * supports plugging, then @rfn may not be called immediately when requests
717 * are available on the queue, but may be called at some time later instead.
718 * Plugged queues are generally unplugged when a buffer belonging to one
719 * of the requests on the queue is needed, or due to memory pressure.
720 *
721 * @rfn is not required, or even expected, to remove all requests off the
722 * queue, but only as many as it can handle at a time. If it does leave
723 * requests on the queue, it is responsible for arranging that the requests
724 * get dealt with eventually.
725 *
726 * The queue spin lock must be held while manipulating the requests on the
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200727 * request queue; this lock will be taken also from interrupt context, so irq
728 * disabling is needed for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 *
Randy Dunlap710027a2008-08-19 20:13:11 +0200730 * Function returns a pointer to the initialized request queue, or %NULL if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 * it didn't succeed.
732 *
733 * Note:
734 * blk_init_queue() must be paired with a blk_cleanup_queue() call
735 * when the block device is deactivated (such as at module unload).
736 **/
Christoph Lameter19460892005-06-23 00:08:19 -0700737
Jens Axboe165125e2007-07-24 09:28:11 +0200738struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Ezequiel Garciac304a512012-11-10 10:39:44 +0100740 return blk_init_queue_node(rfn, lock, NUMA_NO_NODE);
Christoph Lameter19460892005-06-23 00:08:19 -0700741}
742EXPORT_SYMBOL(blk_init_queue);
743
Jens Axboe165125e2007-07-24 09:28:11 +0200744struct request_queue *
Christoph Lameter19460892005-06-23 00:08:19 -0700745blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
746{
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600747 struct request_queue *uninit_q, *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600749 uninit_q = blk_alloc_queue_node(GFP_KERNEL, node_id);
750 if (!uninit_q)
751 return NULL;
752
Mike Snitzer51514122011-11-23 10:59:13 +0100753 q = blk_init_allocated_queue(uninit_q, rfn, lock);
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600754 if (!q)
Mike Snitzer7982e902014-03-08 17:20:01 -0700755 blk_cleanup_queue(uninit_q);
Christoph Hellwig18741982014-02-10 09:29:00 -0700756
Mike Snitzer7982e902014-03-08 17:20:01 -0700757 return q;
Mike Snitzer01effb02010-05-11 08:57:42 +0200758}
759EXPORT_SYMBOL(blk_init_queue_node);
760
Mike Snitzer336b7e12015-05-11 14:06:32 -0400761static void blk_queue_bio(struct request_queue *q, struct bio *bio);
762
Mike Snitzer01effb02010-05-11 08:57:42 +0200763struct request_queue *
764blk_init_allocated_queue(struct request_queue *q, request_fn_proc *rfn,
765 spinlock_t *lock)
766{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (!q)
768 return NULL;
769
Ming Leif70ced02014-09-25 23:23:47 +0800770 q->fq = blk_alloc_flush_queue(q, NUMA_NO_NODE, 0);
Ming Leiba483382014-09-25 23:23:44 +0800771 if (!q->fq)
Mike Snitzer7982e902014-03-08 17:20:01 -0700772 return NULL;
773
Tejun Heoa0516612012-06-26 15:05:44 -0700774 if (blk_init_rl(&q->root_rl, q, GFP_KERNEL))
Dave Jones708f04d2014-03-20 15:03:58 -0600775 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 q->request_fn = rfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 q->prep_rq_fn = NULL;
James Bottomley28018c22010-07-01 19:49:17 +0900779 q->unprep_rq_fn = NULL;
Tejun Heo60ea8222012-09-20 14:09:30 -0700780 q->queue_flags |= QUEUE_FLAG_DEFAULT;
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500781
782 /* Override internal queue lock with supplied lock pointer */
783 if (lock)
784 q->queue_lock = lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Jens Axboef3b144a2009-03-06 08:48:33 +0100786 /*
787 * This also sets hw/phys segments, boundary and size
788 */
Jens Axboec20e8de2011-09-12 12:03:37 +0200789 blk_queue_make_request(q, blk_queue_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Alan Stern44ec9542007-02-20 11:01:57 -0500791 q->sg_reserved_size = INT_MAX;
792
Tomoki Sekiyamaeb1c1602013-10-15 16:42:16 -0600793 /* Protect q->elevator from elevator_change */
794 mutex_lock(&q->sysfs_lock);
795
Tejun Heob82d4b12012-04-13 13:11:31 -0700796 /* init elevator */
Tomoki Sekiyamaeb1c1602013-10-15 16:42:16 -0600797 if (elevator_init(q, NULL)) {
798 mutex_unlock(&q->sysfs_lock);
Dave Jones708f04d2014-03-20 15:03:58 -0600799 goto fail;
Tomoki Sekiyamaeb1c1602013-10-15 16:42:16 -0600800 }
801
802 mutex_unlock(&q->sysfs_lock);
803
Tejun Heob82d4b12012-04-13 13:11:31 -0700804 return q;
Dave Jones708f04d2014-03-20 15:03:58 -0600805
806fail:
Ming Leiba483382014-09-25 23:23:44 +0800807 blk_free_flush_queue(q->fq);
Dave Jones708f04d2014-03-20 15:03:58 -0600808 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
Mike Snitzer51514122011-11-23 10:59:13 +0100810EXPORT_SYMBOL(blk_init_allocated_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Tejun Heo09ac46c2011-12-14 00:33:38 +0100812bool blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100814 if (likely(!blk_queue_dying(q))) {
Tejun Heo09ac46c2011-12-14 00:33:38 +0100815 __blk_get_queue(q);
816 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818
Tejun Heo09ac46c2011-12-14 00:33:38 +0100819 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
Jens Axboed86e0e82011-05-27 07:44:43 +0200821EXPORT_SYMBOL(blk_get_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Tejun Heo5b788ce2012-06-04 20:40:59 -0700823static inline void blk_free_request(struct request_list *rl, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Tejun Heof1f8cc92011-12-14 00:33:42 +0100825 if (rq->cmd_flags & REQ_ELVPRIV) {
Tejun Heo5b788ce2012-06-04 20:40:59 -0700826 elv_put_request(rl->q, rq);
Tejun Heof1f8cc92011-12-14 00:33:42 +0100827 if (rq->elv.icq)
Tejun Heo11a31222012-02-07 07:51:30 +0100828 put_io_context(rq->elv.icq->ioc);
Tejun Heof1f8cc92011-12-14 00:33:42 +0100829 }
830
Tejun Heo5b788ce2012-06-04 20:40:59 -0700831 mempool_free(rq, rl->rq_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834/*
835 * ioc_batching returns true if the ioc is a valid batching request and
836 * should be given priority access to a request.
837 */
Jens Axboe165125e2007-07-24 09:28:11 +0200838static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
840 if (!ioc)
841 return 0;
842
843 /*
844 * Make sure the process is able to allocate at least 1 request
845 * even if the batch times out, otherwise we could theoretically
846 * lose wakeups.
847 */
848 return ioc->nr_batch_requests == q->nr_batching ||
849 (ioc->nr_batch_requests > 0
850 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
851}
852
853/*
854 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
855 * will cause the process to be a "batcher" on all queues in the system. This
856 * is the behaviour we want though - once it gets a wakeup it should be given
857 * a nice run.
858 */
Jens Axboe165125e2007-07-24 09:28:11 +0200859static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
861 if (!ioc || ioc_batching(q, ioc))
862 return;
863
864 ioc->nr_batch_requests = q->nr_batching;
865 ioc->last_waited = jiffies;
866}
867
Tejun Heo5b788ce2012-06-04 20:40:59 -0700868static void __freed_request(struct request_list *rl, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Tejun Heo5b788ce2012-06-04 20:40:59 -0700870 struct request_queue *q = rl->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Tejun Heoc18a2202015-05-22 17:13:42 -0400872 if (rl->count[sync] < queue_congestion_off_threshold(q))
873 blk_clear_congested(rl, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Jens Axboe1faa16d2009-04-06 14:48:01 +0200875 if (rl->count[sync] + 1 <= q->nr_requests) {
876 if (waitqueue_active(&rl->wait[sync]))
877 wake_up(&rl->wait[sync]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Tejun Heo5b788ce2012-06-04 20:40:59 -0700879 blk_clear_rl_full(rl, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
881}
882
883/*
884 * A request has just been released. Account for it, update the full and
885 * congestion status, wake up any waiters. Called under q->queue_lock.
886 */
Tejun Heo5b788ce2012-06-04 20:40:59 -0700887static void freed_request(struct request_list *rl, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Tejun Heo5b788ce2012-06-04 20:40:59 -0700889 struct request_queue *q = rl->q;
Tejun Heo75eb6c32011-10-19 14:31:22 +0200890 int sync = rw_is_sync(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Tejun Heo8a5ecdd2012-06-04 20:40:58 -0700892 q->nr_rqs[sync]--;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200893 rl->count[sync]--;
Tejun Heo75eb6c32011-10-19 14:31:22 +0200894 if (flags & REQ_ELVPRIV)
Tejun Heo8a5ecdd2012-06-04 20:40:58 -0700895 q->nr_rqs_elvpriv--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Tejun Heo5b788ce2012-06-04 20:40:59 -0700897 __freed_request(rl, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Jens Axboe1faa16d2009-04-06 14:48:01 +0200899 if (unlikely(rl->starved[sync ^ 1]))
Tejun Heo5b788ce2012-06-04 20:40:59 -0700900 __freed_request(rl, sync ^ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
902
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600903int blk_update_nr_requests(struct request_queue *q, unsigned int nr)
904{
905 struct request_list *rl;
Tejun Heoc18a2202015-05-22 17:13:42 -0400906 int on_thresh, off_thresh;
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600907
908 spin_lock_irq(q->queue_lock);
909 q->nr_requests = nr;
910 blk_queue_congestion_threshold(q);
Tejun Heoc18a2202015-05-22 17:13:42 -0400911 on_thresh = queue_congestion_on_threshold(q);
912 off_thresh = queue_congestion_off_threshold(q);
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600913
914 blk_queue_for_each_rl(rl, q) {
Tejun Heoc18a2202015-05-22 17:13:42 -0400915 if (rl->count[BLK_RW_SYNC] >= on_thresh)
916 blk_set_congested(rl, BLK_RW_SYNC);
917 else if (rl->count[BLK_RW_SYNC] < off_thresh)
918 blk_clear_congested(rl, BLK_RW_SYNC);
919
920 if (rl->count[BLK_RW_ASYNC] >= on_thresh)
921 blk_set_congested(rl, BLK_RW_ASYNC);
922 else if (rl->count[BLK_RW_ASYNC] < off_thresh)
923 blk_clear_congested(rl, BLK_RW_ASYNC);
924
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600925 if (rl->count[BLK_RW_SYNC] >= q->nr_requests) {
926 blk_set_rl_full(rl, BLK_RW_SYNC);
927 } else {
928 blk_clear_rl_full(rl, BLK_RW_SYNC);
929 wake_up(&rl->wait[BLK_RW_SYNC]);
930 }
931
932 if (rl->count[BLK_RW_ASYNC] >= q->nr_requests) {
933 blk_set_rl_full(rl, BLK_RW_ASYNC);
934 } else {
935 blk_clear_rl_full(rl, BLK_RW_ASYNC);
936 wake_up(&rl->wait[BLK_RW_ASYNC]);
937 }
938 }
939
940 spin_unlock_irq(q->queue_lock);
941 return 0;
942}
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944/*
Mike Snitzer9d5a4e92011-02-11 11:05:46 +0100945 * Determine if elevator data should be initialized when allocating the
946 * request associated with @bio.
947 */
948static bool blk_rq_should_init_elevator(struct bio *bio)
949{
950 if (!bio)
951 return true;
952
953 /*
954 * Flush requests do not use the elevator so skip initialization.
955 * This allows a request to share the flush and elevator data.
956 */
957 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA))
958 return false;
959
960 return true;
961}
962
Tejun Heoda8303c2011-10-19 14:33:05 +0200963/**
Tejun Heo852c7882012-03-05 13:15:27 -0800964 * rq_ioc - determine io_context for request allocation
965 * @bio: request being allocated is for this bio (can be %NULL)
966 *
967 * Determine io_context to use for request allocation for @bio. May return
968 * %NULL if %current->io_context doesn't exist.
969 */
970static struct io_context *rq_ioc(struct bio *bio)
971{
972#ifdef CONFIG_BLK_CGROUP
973 if (bio && bio->bi_ioc)
974 return bio->bi_ioc;
975#endif
976 return current->io_context;
977}
978
979/**
Tejun Heoa06e05e2012-06-04 20:40:55 -0700980 * __get_request - get a free request
Tejun Heo5b788ce2012-06-04 20:40:59 -0700981 * @rl: request list to allocate from
Tejun Heoda8303c2011-10-19 14:33:05 +0200982 * @rw_flags: RW and SYNC flags
983 * @bio: bio to allocate request for (can be %NULL)
984 * @gfp_mask: allocation mask
985 *
986 * Get a free request from @q. This function may fail under memory
987 * pressure or if @q is dead.
988 *
Masanari Iidada3dae52014-09-09 01:27:23 +0900989 * Must be called with @q->queue_lock held and,
Joe Lawrencea492f072014-08-28 08:15:21 -0600990 * Returns ERR_PTR on failure, with @q->queue_lock held.
991 * Returns request pointer on success, with @q->queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 */
Tejun Heo5b788ce2012-06-04 20:40:59 -0700993static struct request *__get_request(struct request_list *rl, int rw_flags,
Tejun Heoa06e05e2012-06-04 20:40:55 -0700994 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
Tejun Heo5b788ce2012-06-04 20:40:59 -0700996 struct request_queue *q = rl->q;
Tejun Heob6792812012-03-05 13:15:23 -0800997 struct request *rq;
Tejun Heo7f4b35d2012-06-04 20:40:56 -0700998 struct elevator_type *et = q->elevator->type;
999 struct io_context *ioc = rq_ioc(bio);
Tejun Heof1f8cc92011-12-14 00:33:42 +01001000 struct io_cq *icq = NULL;
Jens Axboe1faa16d2009-04-06 14:48:01 +02001001 const bool is_sync = rw_is_sync(rw_flags) != 0;
Tejun Heo75eb6c32011-10-19 14:31:22 +02001002 int may_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Bart Van Assche3f3299d2012-11-28 13:42:38 +01001004 if (unlikely(blk_queue_dying(q)))
Joe Lawrencea492f072014-08-28 08:15:21 -06001005 return ERR_PTR(-ENODEV);
Tejun Heoda8303c2011-10-19 14:33:05 +02001006
Jens Axboe7749a8d2006-12-13 13:02:26 +01001007 may_queue = elv_may_queue(q, rw_flags);
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001008 if (may_queue == ELV_MQUEUE_NO)
1009 goto rq_starved;
1010
Jens Axboe1faa16d2009-04-06 14:48:01 +02001011 if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) {
1012 if (rl->count[is_sync]+1 >= q->nr_requests) {
Tejun Heof2dbd762011-12-14 00:33:40 +01001013 /*
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001014 * The queue will fill after this allocation, so set
1015 * it as full, and mark this process as "batching".
1016 * This process will be allowed to complete a batch of
1017 * requests, others will be blocked.
1018 */
Tejun Heo5b788ce2012-06-04 20:40:59 -07001019 if (!blk_rl_full(rl, is_sync)) {
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001020 ioc_set_batching(q, ioc);
Tejun Heo5b788ce2012-06-04 20:40:59 -07001021 blk_set_rl_full(rl, is_sync);
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001022 } else {
1023 if (may_queue != ELV_MQUEUE_MUST
1024 && !ioc_batching(q, ioc)) {
1025 /*
1026 * The queue is full and the allocating
1027 * process is not a "batcher", and not
1028 * exempted by the IO scheduler
1029 */
Joe Lawrencea492f072014-08-28 08:15:21 -06001030 return ERR_PTR(-ENOMEM);
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001031 }
1032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
Tejun Heoc18a2202015-05-22 17:13:42 -04001034 blk_set_congested(rl, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
1036
Jens Axboe082cf692005-06-28 16:35:11 +02001037 /*
1038 * Only allow batching queuers to allocate up to 50% over the defined
1039 * limit of requests, otherwise we could have thousands of requests
1040 * allocated with any setting of ->nr_requests
1041 */
Jens Axboe1faa16d2009-04-06 14:48:01 +02001042 if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
Joe Lawrencea492f072014-08-28 08:15:21 -06001043 return ERR_PTR(-ENOMEM);
Hugh Dickinsfd782a42005-06-29 15:15:40 +01001044
Tejun Heo8a5ecdd2012-06-04 20:40:58 -07001045 q->nr_rqs[is_sync]++;
Jens Axboe1faa16d2009-04-06 14:48:01 +02001046 rl->count[is_sync]++;
1047 rl->starved[is_sync] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +02001048
Tejun Heof1f8cc92011-12-14 00:33:42 +01001049 /*
1050 * Decide whether the new request will be managed by elevator. If
1051 * so, mark @rw_flags and increment elvpriv. Non-zero elvpriv will
1052 * prevent the current elevator from being destroyed until the new
1053 * request is freed. This guarantees icq's won't be destroyed and
1054 * makes creating new ones safe.
1055 *
1056 * Also, lookup icq while holding queue_lock. If it doesn't exist,
1057 * it will be created after releasing queue_lock.
1058 */
Tejun Heod7325802012-03-05 13:14:58 -08001059 if (blk_rq_should_init_elevator(bio) && !blk_queue_bypass(q)) {
Tejun Heo75eb6c32011-10-19 14:31:22 +02001060 rw_flags |= REQ_ELVPRIV;
Tejun Heo8a5ecdd2012-06-04 20:40:58 -07001061 q->nr_rqs_elvpriv++;
Tejun Heof1f8cc92011-12-14 00:33:42 +01001062 if (et->icq_cache && ioc)
1063 icq = ioc_lookup_icq(ioc, q);
Mike Snitzer9d5a4e92011-02-11 11:05:46 +01001064 }
Tejun Heocb98fc82005-10-28 08:29:39 +02001065
Jens Axboef253b862010-10-24 22:06:02 +02001066 if (blk_queue_io_stat(q))
1067 rw_flags |= REQ_IO_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 spin_unlock_irq(q->queue_lock);
1069
Tejun Heo29e2b092012-04-19 16:29:21 -07001070 /* allocate and init request */
Tejun Heo5b788ce2012-06-04 20:40:59 -07001071 rq = mempool_alloc(rl->rq_pool, gfp_mask);
Tejun Heo29e2b092012-04-19 16:29:21 -07001072 if (!rq)
Tejun Heob6792812012-03-05 13:15:23 -08001073 goto fail_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Tejun Heo29e2b092012-04-19 16:29:21 -07001075 blk_rq_init(q, rq);
Tejun Heoa0516612012-06-26 15:05:44 -07001076 blk_rq_set_rl(rq, rl);
Tejun Heo29e2b092012-04-19 16:29:21 -07001077 rq->cmd_flags = rw_flags | REQ_ALLOCED;
1078
Tejun Heoaaf7c682012-04-19 16:29:22 -07001079 /* init elvpriv */
Tejun Heo29e2b092012-04-19 16:29:21 -07001080 if (rw_flags & REQ_ELVPRIV) {
Tejun Heoaaf7c682012-04-19 16:29:22 -07001081 if (unlikely(et->icq_cache && !icq)) {
Tejun Heo7f4b35d2012-06-04 20:40:56 -07001082 if (ioc)
1083 icq = ioc_create_icq(ioc, q, gfp_mask);
Tejun Heoaaf7c682012-04-19 16:29:22 -07001084 if (!icq)
1085 goto fail_elvpriv;
Tejun Heo29e2b092012-04-19 16:29:21 -07001086 }
Tejun Heoaaf7c682012-04-19 16:29:22 -07001087
1088 rq->elv.icq = icq;
1089 if (unlikely(elv_set_request(q, rq, bio, gfp_mask)))
1090 goto fail_elvpriv;
1091
1092 /* @rq->elv.icq holds io_context until @rq is freed */
Tejun Heo29e2b092012-04-19 16:29:21 -07001093 if (icq)
1094 get_io_context(icq->ioc);
1095 }
Tejun Heoaaf7c682012-04-19 16:29:22 -07001096out:
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001097 /*
1098 * ioc may be NULL here, and ioc_batching will be false. That's
1099 * OK, if the queue is under the request limit then requests need
1100 * not count toward the nr_batch_requests limit. There will always
1101 * be some limit enforced by BLK_BATCH_TIME.
1102 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (ioc_batching(q, ioc))
1104 ioc->nr_batch_requests--;
Jens Axboe6728cb02008-01-31 13:03:55 +01001105
Jens Axboe1faa16d2009-04-06 14:48:01 +02001106 trace_block_getrq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return rq;
Tejun Heob6792812012-03-05 13:15:23 -08001108
Tejun Heoaaf7c682012-04-19 16:29:22 -07001109fail_elvpriv:
1110 /*
1111 * elvpriv init failed. ioc, icq and elvpriv aren't mempool backed
1112 * and may fail indefinitely under memory pressure and thus
1113 * shouldn't stall IO. Treat this request as !elvpriv. This will
1114 * disturb iosched and blkcg but weird is bettern than dead.
1115 */
Robert Elliott7b2b10e2014-08-27 10:50:36 -05001116 printk_ratelimited(KERN_WARNING "%s: dev %s: request aux data allocation failed, iosched may be disturbed\n",
1117 __func__, dev_name(q->backing_dev_info.dev));
Tejun Heoaaf7c682012-04-19 16:29:22 -07001118
1119 rq->cmd_flags &= ~REQ_ELVPRIV;
1120 rq->elv.icq = NULL;
1121
1122 spin_lock_irq(q->queue_lock);
Tejun Heo8a5ecdd2012-06-04 20:40:58 -07001123 q->nr_rqs_elvpriv--;
Tejun Heoaaf7c682012-04-19 16:29:22 -07001124 spin_unlock_irq(q->queue_lock);
1125 goto out;
1126
Tejun Heob6792812012-03-05 13:15:23 -08001127fail_alloc:
1128 /*
1129 * Allocation failed presumably due to memory. Undo anything we
1130 * might have messed up.
1131 *
1132 * Allocating task should really be put onto the front of the wait
1133 * queue, but this is pretty rare.
1134 */
1135 spin_lock_irq(q->queue_lock);
Tejun Heo5b788ce2012-06-04 20:40:59 -07001136 freed_request(rl, rw_flags);
Tejun Heob6792812012-03-05 13:15:23 -08001137
1138 /*
1139 * in the very unlikely event that allocation failed and no
1140 * requests for this direction was pending, mark us starved so that
1141 * freeing of a request in the other direction will notice
1142 * us. another possible fix would be to split the rq mempool into
1143 * READ and WRITE
1144 */
1145rq_starved:
1146 if (unlikely(rl->count[is_sync] == 0))
1147 rl->starved[is_sync] = 1;
Joe Lawrencea492f072014-08-28 08:15:21 -06001148 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
1150
Tejun Heoda8303c2011-10-19 14:33:05 +02001151/**
Tejun Heoa06e05e2012-06-04 20:40:55 -07001152 * get_request - get a free request
Tejun Heoda8303c2011-10-19 14:33:05 +02001153 * @q: request_queue to allocate request from
1154 * @rw_flags: RW and SYNC flags
1155 * @bio: bio to allocate request for (can be %NULL)
Tejun Heoa06e05e2012-06-04 20:40:55 -07001156 * @gfp_mask: allocation mask
Nick Piggind6344532005-06-28 20:45:14 -07001157 *
Tejun Heoa06e05e2012-06-04 20:40:55 -07001158 * Get a free request from @q. If %__GFP_WAIT is set in @gfp_mask, this
1159 * function keeps retrying under memory pressure and fails iff @q is dead.
Tejun Heoda8303c2011-10-19 14:33:05 +02001160 *
Masanari Iidada3dae52014-09-09 01:27:23 +09001161 * Must be called with @q->queue_lock held and,
Joe Lawrencea492f072014-08-28 08:15:21 -06001162 * Returns ERR_PTR on failure, with @q->queue_lock held.
1163 * Returns request pointer on success, with @q->queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 */
Tejun Heoa06e05e2012-06-04 20:40:55 -07001165static struct request *get_request(struct request_queue *q, int rw_flags,
1166 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
Jens Axboe1faa16d2009-04-06 14:48:01 +02001168 const bool is_sync = rw_is_sync(rw_flags) != 0;
Tejun Heoa06e05e2012-06-04 20:40:55 -07001169 DEFINE_WAIT(wait);
Tejun Heoa0516612012-06-26 15:05:44 -07001170 struct request_list *rl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 struct request *rq;
Tejun Heoa0516612012-06-26 15:05:44 -07001172
1173 rl = blk_get_rl(q, bio); /* transferred to @rq on success */
Tejun Heoa06e05e2012-06-04 20:40:55 -07001174retry:
Tejun Heoa0516612012-06-26 15:05:44 -07001175 rq = __get_request(rl, rw_flags, bio, gfp_mask);
Joe Lawrencea492f072014-08-28 08:15:21 -06001176 if (!IS_ERR(rq))
Tejun Heoa06e05e2012-06-04 20:40:55 -07001177 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Bart Van Assche3f3299d2012-11-28 13:42:38 +01001179 if (!(gfp_mask & __GFP_WAIT) || unlikely(blk_queue_dying(q))) {
Tejun Heoa0516612012-06-26 15:05:44 -07001180 blk_put_rl(rl);
Joe Lawrencea492f072014-08-28 08:15:21 -06001181 return rq;
Tejun Heoa0516612012-06-26 15:05:44 -07001182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Tejun Heoa06e05e2012-06-04 20:40:55 -07001184 /* wait on @rl and retry */
1185 prepare_to_wait_exclusive(&rl->wait[is_sync], &wait,
1186 TASK_UNINTERRUPTIBLE);
Tejun Heoda8303c2011-10-19 14:33:05 +02001187
Tejun Heoa06e05e2012-06-04 20:40:55 -07001188 trace_block_sleeprq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Tejun Heoa06e05e2012-06-04 20:40:55 -07001190 spin_unlock_irq(q->queue_lock);
1191 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Tejun Heoa06e05e2012-06-04 20:40:55 -07001193 /*
1194 * After sleeping, we become a "batching" process and will be able
1195 * to allocate at least one request, and up to a big batch of them
1196 * for a small period time. See ioc_batching, ioc_set_batching
1197 */
Tejun Heoa06e05e2012-06-04 20:40:55 -07001198 ioc_set_batching(q, current->io_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Tejun Heoa06e05e2012-06-04 20:40:55 -07001200 spin_lock_irq(q->queue_lock);
1201 finish_wait(&rl->wait[is_sync], &wait);
Jens Axboe2056a782006-03-23 20:00:26 +01001202
Tejun Heoa06e05e2012-06-04 20:40:55 -07001203 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204}
1205
Jens Axboe320ae512013-10-24 09:20:05 +01001206static struct request *blk_old_get_request(struct request_queue *q, int rw,
1207 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
1209 struct request *rq;
1210
1211 BUG_ON(rw != READ && rw != WRITE);
1212
Tejun Heo7f4b35d2012-06-04 20:40:56 -07001213 /* create ioc upfront */
1214 create_io_context(gfp_mask, q->node);
1215
Nick Piggind6344532005-06-28 20:45:14 -07001216 spin_lock_irq(q->queue_lock);
Tejun Heoa06e05e2012-06-04 20:40:55 -07001217 rq = get_request(q, rw, NULL, gfp_mask);
Joe Lawrencea492f072014-08-28 08:15:21 -06001218 if (IS_ERR(rq))
Tejun Heoda8303c2011-10-19 14:33:05 +02001219 spin_unlock_irq(q->queue_lock);
Nick Piggind6344532005-06-28 20:45:14 -07001220 /* q->queue_lock is unlocked at this point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222 return rq;
1223}
Jens Axboe320ae512013-10-24 09:20:05 +01001224
1225struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
1226{
1227 if (q->mq_ops)
Christoph Hellwig4ce01dd2014-05-27 20:59:46 +02001228 return blk_mq_alloc_request(q, rw, gfp_mask, false);
Jens Axboe320ae512013-10-24 09:20:05 +01001229 else
1230 return blk_old_get_request(q, rw, gfp_mask);
1231}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232EXPORT_SYMBOL(blk_get_request);
1233
1234/**
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001235 * blk_make_request - given a bio, allocate a corresponding struct request.
Randy Dunlap8ebf9752009-06-11 20:00:41 -07001236 * @q: target request queue
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001237 * @bio: The bio describing the memory mappings that will be submitted for IO.
1238 * It may be a chained-bio properly constructed by block/bio layer.
Randy Dunlap8ebf9752009-06-11 20:00:41 -07001239 * @gfp_mask: gfp flags to be used for memory allocation
Jens Axboedc72ef42006-07-20 14:54:05 +02001240 *
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001241 * blk_make_request is the parallel of generic_make_request for BLOCK_PC
1242 * type commands. Where the struct request needs to be farther initialized by
1243 * the caller. It is passed a &struct bio, which describes the memory info of
1244 * the I/O transfer.
1245 *
1246 * The caller of blk_make_request must make sure that bi_io_vec
1247 * are set to describe the memory buffers. That bio_data_dir() will return
1248 * the needed direction of the request. (And all bio's in the passed bio-chain
1249 * are properly set accordingly)
1250 *
1251 * If called under none-sleepable conditions, mapped bio buffers must not
1252 * need bouncing, by calling the appropriate masked or flagged allocator,
1253 * suitable for the target device. Otherwise the call to blk_queue_bounce will
1254 * BUG.
Jens Axboe53674ac2009-05-19 19:52:35 +02001255 *
1256 * WARNING: When allocating/cloning a bio-chain, careful consideration should be
1257 * given to how you allocate bios. In particular, you cannot use __GFP_WAIT for
1258 * anything but the first bio in the chain. Otherwise you risk waiting for IO
1259 * completion of a bio that hasn't been submitted yet, thus resulting in a
1260 * deadlock. Alternatively bios should be allocated using bio_kmalloc() instead
1261 * of bio_alloc(), as that avoids the mempool deadlock.
1262 * If possible a big IO should be split into smaller parts when allocation
1263 * fails. Partial allocation should not be an error, or you risk a live-lock.
Jens Axboedc72ef42006-07-20 14:54:05 +02001264 */
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001265struct request *blk_make_request(struct request_queue *q, struct bio *bio,
1266 gfp_t gfp_mask)
Jens Axboedc72ef42006-07-20 14:54:05 +02001267{
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001268 struct request *rq = blk_get_request(q, bio_data_dir(bio), gfp_mask);
1269
Joe Lawrencea492f072014-08-28 08:15:21 -06001270 if (IS_ERR(rq))
1271 return rq;
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001272
Jens Axboef27b0872014-06-06 07:57:37 -06001273 blk_rq_set_block_pc(rq);
1274
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001275 for_each_bio(bio) {
1276 struct bio *bounce_bio = bio;
1277 int ret;
1278
1279 blk_queue_bounce(q, &bounce_bio);
1280 ret = blk_rq_append_bio(q, rq, bounce_bio);
1281 if (unlikely(ret)) {
1282 blk_put_request(rq);
1283 return ERR_PTR(ret);
1284 }
1285 }
1286
1287 return rq;
Jens Axboedc72ef42006-07-20 14:54:05 +02001288}
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001289EXPORT_SYMBOL(blk_make_request);
Jens Axboedc72ef42006-07-20 14:54:05 +02001290
1291/**
Masanari Iidada3dae52014-09-09 01:27:23 +09001292 * blk_rq_set_block_pc - initialize a request to type BLOCK_PC
Jens Axboef27b0872014-06-06 07:57:37 -06001293 * @rq: request to be initialized
1294 *
1295 */
1296void blk_rq_set_block_pc(struct request *rq)
1297{
1298 rq->cmd_type = REQ_TYPE_BLOCK_PC;
1299 rq->__data_len = 0;
1300 rq->__sector = (sector_t) -1;
1301 rq->bio = rq->biotail = NULL;
1302 memset(rq->__cmd, 0, sizeof(rq->__cmd));
Jens Axboef27b0872014-06-06 07:57:37 -06001303}
1304EXPORT_SYMBOL(blk_rq_set_block_pc);
1305
1306/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 * blk_requeue_request - put a request back on queue
1308 * @q: request queue where request should be inserted
1309 * @rq: request to be inserted
1310 *
1311 * Description:
1312 * Drivers often keep queueing requests until the hardware cannot accept
1313 * more, when that condition happens we need to put the request back
1314 * on the queue. Must be called with queue lock held.
1315 */
Jens Axboe165125e2007-07-24 09:28:11 +02001316void blk_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
Jens Axboe242f9dc2008-09-14 05:55:09 -07001318 blk_delete_timer(rq);
1319 blk_clear_rq_complete(rq);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001320 trace_block_rq_requeue(q, rq);
Jens Axboe2056a782006-03-23 20:00:26 +01001321
Christoph Hellwig125c99b2014-11-03 12:47:47 +01001322 if (rq->cmd_flags & REQ_QUEUED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 blk_queue_end_tag(q, rq);
1324
James Bottomleyba396a62009-05-27 14:17:08 +02001325 BUG_ON(blk_queued_rq(rq));
1326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 elv_requeue_request(q, rq);
1328}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329EXPORT_SYMBOL(blk_requeue_request);
1330
Jens Axboe73c10102011-03-08 13:19:51 +01001331static void add_acct_request(struct request_queue *q, struct request *rq,
1332 int where)
1333{
Jens Axboe320ae512013-10-24 09:20:05 +01001334 blk_account_io_start(rq, true);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001335 __elv_add_request(q, rq, where);
Jens Axboe73c10102011-03-08 13:19:51 +01001336}
1337
Tejun Heo074a7ac2008-08-25 19:56:14 +09001338static void part_round_stats_single(int cpu, struct hd_struct *part,
1339 unsigned long now)
1340{
Jens Axboe7276d022014-05-09 15:48:23 -06001341 int inflight;
1342
Tejun Heo074a7ac2008-08-25 19:56:14 +09001343 if (now == part->stamp)
1344 return;
1345
Jens Axboe7276d022014-05-09 15:48:23 -06001346 inflight = part_in_flight(part);
1347 if (inflight) {
Tejun Heo074a7ac2008-08-25 19:56:14 +09001348 __part_stat_add(cpu, part, time_in_queue,
Jens Axboe7276d022014-05-09 15:48:23 -06001349 inflight * (now - part->stamp));
Tejun Heo074a7ac2008-08-25 19:56:14 +09001350 __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
1351 }
1352 part->stamp = now;
1353}
1354
1355/**
Randy Dunlap496aa8a2008-10-16 07:46:23 +02001356 * part_round_stats() - Round off the performance stats on a struct disk_stats.
1357 * @cpu: cpu number for stats access
1358 * @part: target partition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 *
1360 * The average IO queue length and utilisation statistics are maintained
1361 * by observing the current state of the queue length and the amount of
1362 * time it has been in this state for.
1363 *
1364 * Normally, that accounting is done on IO completion, but that can result
1365 * in more than a second's worth of IO being accounted for within any one
1366 * second, leading to >100% utilisation. To deal with that, we call this
1367 * function to do a round-off before returning the results when reading
1368 * /proc/diskstats. This accounts immediately for all queue usage up to
1369 * the current jiffies and restarts the counters again.
1370 */
Tejun Heoc9959052008-08-25 19:47:21 +09001371void part_round_stats(int cpu, struct hd_struct *part)
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001372{
1373 unsigned long now = jiffies;
1374
Tejun Heo074a7ac2008-08-25 19:56:14 +09001375 if (part->partno)
1376 part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
1377 part_round_stats_single(cpu, part, now);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001378}
Tejun Heo074a7ac2008-08-25 19:56:14 +09001379EXPORT_SYMBOL_GPL(part_round_stats);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001380
Rafael J. Wysocki47fafbc2014-12-04 01:00:23 +01001381#ifdef CONFIG_PM
Lin Mingc8158812013-03-23 11:42:27 +08001382static void blk_pm_put_request(struct request *rq)
1383{
1384 if (rq->q->dev && !(rq->cmd_flags & REQ_PM) && !--rq->q->nr_pending)
1385 pm_runtime_mark_last_busy(rq->q->dev);
1386}
1387#else
1388static inline void blk_pm_put_request(struct request *rq) {}
1389#endif
1390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391/*
1392 * queue lock must be held
1393 */
Jens Axboe165125e2007-07-24 09:28:11 +02001394void __blk_put_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 if (unlikely(!q))
1397 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Christoph Hellwig6f5ba582014-02-07 10:22:37 -08001399 if (q->mq_ops) {
1400 blk_mq_free_request(req);
1401 return;
1402 }
1403
Lin Mingc8158812013-03-23 11:42:27 +08001404 blk_pm_put_request(req);
1405
Tejun Heo8922e162005-10-20 16:23:44 +02001406 elv_completed_request(q, req);
1407
Boaz Harrosh1cd96c22009-03-24 12:35:07 +01001408 /* this is a bio leak */
1409 WARN_ON(req->bio != NULL);
1410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 /*
1412 * Request may not have originated from ll_rw_blk. if not,
1413 * it didn't come out of our reserved rq pools
1414 */
Jens Axboe49171e52006-08-10 08:59:11 +02001415 if (req->cmd_flags & REQ_ALLOCED) {
Tejun Heo75eb6c32011-10-19 14:31:22 +02001416 unsigned int flags = req->cmd_flags;
Tejun Heoa0516612012-06-26 15:05:44 -07001417 struct request_list *rl = blk_rq_rl(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 BUG_ON(!list_empty(&req->queuelist));
Jens Axboe360f92c2014-04-09 20:27:01 -06001420 BUG_ON(ELV_ON_HASH(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Tejun Heoa0516612012-06-26 15:05:44 -07001422 blk_free_request(rl, req);
1423 freed_request(rl, flags);
1424 blk_put_rl(rl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 }
1426}
Mike Christie6e39b692005-11-11 05:30:24 -06001427EXPORT_SYMBOL_GPL(__blk_put_request);
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429void blk_put_request(struct request *req)
1430{
Jens Axboe165125e2007-07-24 09:28:11 +02001431 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Jens Axboe320ae512013-10-24 09:20:05 +01001433 if (q->mq_ops)
1434 blk_mq_free_request(req);
1435 else {
1436 unsigned long flags;
1437
1438 spin_lock_irqsave(q->queue_lock, flags);
1439 __blk_put_request(q, req);
1440 spin_unlock_irqrestore(q->queue_lock, flags);
1441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443EXPORT_SYMBOL(blk_put_request);
1444
Christoph Hellwig66ac0282010-06-18 16:59:42 +02001445/**
1446 * blk_add_request_payload - add a payload to a request
1447 * @rq: request to update
1448 * @page: page backing the payload
1449 * @len: length of the payload.
1450 *
1451 * This allows to later add a payload to an already submitted request by
1452 * a block driver. The driver needs to take care of freeing the payload
1453 * itself.
1454 *
1455 * Note that this is a quite horrible hack and nothing but handling of
1456 * discard requests should ever use it.
1457 */
1458void blk_add_request_payload(struct request *rq, struct page *page,
1459 unsigned int len)
1460{
1461 struct bio *bio = rq->bio;
1462
1463 bio->bi_io_vec->bv_page = page;
1464 bio->bi_io_vec->bv_offset = 0;
1465 bio->bi_io_vec->bv_len = len;
1466
Kent Overstreet4f024f32013-10-11 15:44:27 -07001467 bio->bi_iter.bi_size = len;
Christoph Hellwig66ac0282010-06-18 16:59:42 +02001468 bio->bi_vcnt = 1;
1469 bio->bi_phys_segments = 1;
1470
1471 rq->__data_len = rq->resid_len = len;
1472 rq->nr_phys_segments = 1;
Christoph Hellwig66ac0282010-06-18 16:59:42 +02001473}
1474EXPORT_SYMBOL_GPL(blk_add_request_payload);
1475
Jens Axboe320ae512013-10-24 09:20:05 +01001476bool bio_attempt_back_merge(struct request_queue *q, struct request *req,
1477 struct bio *bio)
Jens Axboe73c10102011-03-08 13:19:51 +01001478{
1479 const int ff = bio->bi_rw & REQ_FAILFAST_MASK;
1480
Jens Axboe73c10102011-03-08 13:19:51 +01001481 if (!ll_back_merge_fn(q, req, bio))
1482 return false;
1483
Tejun Heo8c1cf6b2013-01-11 13:06:34 -08001484 trace_block_bio_backmerge(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001485
1486 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1487 blk_rq_set_mixed_merge(req);
1488
1489 req->biotail->bi_next = bio;
1490 req->biotail = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001491 req->__data_len += bio->bi_iter.bi_size;
Jens Axboe73c10102011-03-08 13:19:51 +01001492 req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
1493
Jens Axboe320ae512013-10-24 09:20:05 +01001494 blk_account_io_start(req, false);
Jens Axboe73c10102011-03-08 13:19:51 +01001495 return true;
1496}
1497
Jens Axboe320ae512013-10-24 09:20:05 +01001498bool bio_attempt_front_merge(struct request_queue *q, struct request *req,
1499 struct bio *bio)
Jens Axboe73c10102011-03-08 13:19:51 +01001500{
1501 const int ff = bio->bi_rw & REQ_FAILFAST_MASK;
Jens Axboe73c10102011-03-08 13:19:51 +01001502
Jens Axboe73c10102011-03-08 13:19:51 +01001503 if (!ll_front_merge_fn(q, req, bio))
1504 return false;
1505
Tejun Heo8c1cf6b2013-01-11 13:06:34 -08001506 trace_block_bio_frontmerge(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001507
1508 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1509 blk_rq_set_mixed_merge(req);
1510
Jens Axboe73c10102011-03-08 13:19:51 +01001511 bio->bi_next = req->bio;
1512 req->bio = bio;
1513
Kent Overstreet4f024f32013-10-11 15:44:27 -07001514 req->__sector = bio->bi_iter.bi_sector;
1515 req->__data_len += bio->bi_iter.bi_size;
Jens Axboe73c10102011-03-08 13:19:51 +01001516 req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
1517
Jens Axboe320ae512013-10-24 09:20:05 +01001518 blk_account_io_start(req, false);
Jens Axboe73c10102011-03-08 13:19:51 +01001519 return true;
1520}
1521
Tejun Heobd87b582011-10-19 14:33:08 +02001522/**
Jens Axboe320ae512013-10-24 09:20:05 +01001523 * blk_attempt_plug_merge - try to merge with %current's plugged list
Tejun Heobd87b582011-10-19 14:33:08 +02001524 * @q: request_queue new bio is being queued at
1525 * @bio: new bio being queued
1526 * @request_count: out parameter for number of traversed plugged requests
1527 *
1528 * Determine whether @bio being queued on @q can be merged with a request
1529 * on %current's plugged list. Returns %true if merge was successful,
1530 * otherwise %false.
1531 *
Tejun Heo07c2bd32012-02-08 09:19:42 +01001532 * Plugging coalesces IOs from the same issuer for the same purpose without
1533 * going through @q->queue_lock. As such it's more of an issuing mechanism
1534 * than scheduling, and the request, while may have elvpriv data, is not
1535 * added on the elevator at this point. In addition, we don't have
1536 * reliable access to the elevator outside queue lock. Only check basic
1537 * merging parameters without querying the elevator.
Robert Elliottda41a582014-05-20 16:46:26 -05001538 *
1539 * Caller must ensure !blk_queue_nomerges(q) beforehand.
Jens Axboe73c10102011-03-08 13:19:51 +01001540 */
Jens Axboe320ae512013-10-24 09:20:05 +01001541bool blk_attempt_plug_merge(struct request_queue *q, struct bio *bio,
1542 unsigned int *request_count)
Jens Axboe73c10102011-03-08 13:19:51 +01001543{
1544 struct blk_plug *plug;
1545 struct request *rq;
1546 bool ret = false;
Shaohua Li92f399c2013-10-29 12:01:03 -06001547 struct list_head *plug_list;
Jens Axboe73c10102011-03-08 13:19:51 +01001548
Tejun Heobd87b582011-10-19 14:33:08 +02001549 plug = current->plug;
Jens Axboe73c10102011-03-08 13:19:51 +01001550 if (!plug)
1551 goto out;
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001552 *request_count = 0;
Jens Axboe73c10102011-03-08 13:19:51 +01001553
Shaohua Li92f399c2013-10-29 12:01:03 -06001554 if (q->mq_ops)
1555 plug_list = &plug->mq_list;
1556 else
1557 plug_list = &plug->list;
1558
1559 list_for_each_entry_reverse(rq, plug_list, queuelist) {
Jens Axboe73c10102011-03-08 13:19:51 +01001560 int el_ret;
1561
Shaohua Li1b2e19f2012-04-06 11:37:47 -06001562 if (rq->q == q)
1563 (*request_count)++;
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001564
Tejun Heo07c2bd32012-02-08 09:19:42 +01001565 if (rq->q != q || !blk_rq_merge_ok(rq, bio))
Jens Axboe73c10102011-03-08 13:19:51 +01001566 continue;
1567
Tejun Heo050c8ea2012-02-08 09:19:38 +01001568 el_ret = blk_try_merge(rq, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001569 if (el_ret == ELEVATOR_BACK_MERGE) {
1570 ret = bio_attempt_back_merge(q, rq, bio);
1571 if (ret)
1572 break;
1573 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
1574 ret = bio_attempt_front_merge(q, rq, bio);
1575 if (ret)
1576 break;
1577 }
1578 }
1579out:
1580 return ret;
1581}
1582
Jens Axboe86db1e22008-01-29 14:53:40 +01001583void init_request_from_bio(struct request *req, struct bio *bio)
Tejun Heo52d9e672006-01-06 09:49:58 +01001584{
Jens Axboe4aff5e22006-08-10 08:44:47 +02001585 req->cmd_type = REQ_TYPE_FS;
Tejun Heo52d9e672006-01-06 09:49:58 +01001586
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001587 req->cmd_flags |= bio->bi_rw & REQ_COMMON_MASK;
1588 if (bio->bi_rw & REQ_RAHEAD)
Tejun Heoa82afdf2009-07-03 17:48:16 +09001589 req->cmd_flags |= REQ_FAILFAST_MASK;
Jens Axboeb31dc662006-06-13 08:26:10 +02001590
Tejun Heo52d9e672006-01-06 09:49:58 +01001591 req->errors = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001592 req->__sector = bio->bi_iter.bi_sector;
Tejun Heo52d9e672006-01-06 09:49:58 +01001593 req->ioprio = bio_prio(bio);
NeilBrownbc1c56f2007-08-16 13:31:30 +02001594 blk_rq_bio_prep(req->q, req, bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001595}
1596
Mike Snitzer336b7e12015-05-11 14:06:32 -04001597static void blk_queue_bio(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598{
Jiri Slaby5e00d1b2010-08-12 14:31:06 +02001599 const bool sync = !!(bio->bi_rw & REQ_SYNC);
Jens Axboe73c10102011-03-08 13:19:51 +01001600 struct blk_plug *plug;
1601 int el_ret, rw_flags, where = ELEVATOR_INSERT_SORT;
1602 struct request *req;
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001603 unsigned int request_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 /*
1606 * low level driver can indicate that it wants pages above a
1607 * certain limit bounced to low memory (ie for highmem, or even
1608 * ISA dma in theory)
1609 */
1610 blk_queue_bounce(q, &bio);
1611
Darrick J. Wongffecfd12013-02-21 16:42:55 -08001612 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1613 bio_endio(bio, -EIO);
1614 return;
1615 }
1616
Tejun Heo4fed9472010-09-03 11:56:17 +02001617 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) {
Jens Axboe73c10102011-03-08 13:19:51 +01001618 spin_lock_irq(q->queue_lock);
Tejun Heoae1b1532011-01-25 12:43:54 +01001619 where = ELEVATOR_INSERT_FLUSH;
Tejun Heo28e7d182010-09-03 11:56:16 +02001620 goto get_rq;
1621 }
1622
Jens Axboe73c10102011-03-08 13:19:51 +01001623 /*
1624 * Check if we can merge with the plugged list before grabbing
1625 * any locks.
1626 */
Robert Elliottda41a582014-05-20 16:46:26 -05001627 if (!blk_queue_nomerges(q) &&
1628 blk_attempt_plug_merge(q, bio, &request_count))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001629 return;
Jens Axboe73c10102011-03-08 13:19:51 +01001630
1631 spin_lock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633 el_ret = elv_merge(q, &req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001634 if (el_ret == ELEVATOR_BACK_MERGE) {
Jens Axboe73c10102011-03-08 13:19:51 +01001635 if (bio_attempt_back_merge(q, req, bio)) {
Tejun Heo07c2bd32012-02-08 09:19:42 +01001636 elv_bio_merged(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001637 if (!attempt_back_merge(q, req))
1638 elv_merged_request(q, req, el_ret);
1639 goto out_unlock;
Tejun Heo80a761f2009-07-03 17:48:17 +09001640 }
Jens Axboe73c10102011-03-08 13:19:51 +01001641 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
Jens Axboe73c10102011-03-08 13:19:51 +01001642 if (bio_attempt_front_merge(q, req, bio)) {
Tejun Heo07c2bd32012-02-08 09:19:42 +01001643 elv_bio_merged(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001644 if (!attempt_front_merge(q, req))
1645 elv_merged_request(q, req, el_ret);
1646 goto out_unlock;
1647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 }
1649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650get_rq:
Nick Piggin450991b2005-06-28 20:45:13 -07001651 /*
Jens Axboe7749a8d2006-12-13 13:02:26 +01001652 * This sync check and mask will be re-done in init_request_from_bio(),
1653 * but we need to set it earlier to expose the sync flag to the
1654 * rq allocator and io schedulers.
1655 */
1656 rw_flags = bio_data_dir(bio);
1657 if (sync)
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001658 rw_flags |= REQ_SYNC;
Jens Axboe7749a8d2006-12-13 13:02:26 +01001659
1660 /*
Nick Piggin450991b2005-06-28 20:45:13 -07001661 * Grab a free request. This is might sleep but can not fail.
Nick Piggind6344532005-06-28 20:45:14 -07001662 * Returns with the queue unlocked.
Nick Piggin450991b2005-06-28 20:45:13 -07001663 */
Tejun Heoa06e05e2012-06-04 20:40:55 -07001664 req = get_request(q, rw_flags, bio, GFP_NOIO);
Joe Lawrencea492f072014-08-28 08:15:21 -06001665 if (IS_ERR(req)) {
1666 bio_endio(bio, PTR_ERR(req)); /* @q is dead */
Tejun Heoda8303c2011-10-19 14:33:05 +02001667 goto out_unlock;
1668 }
Nick Piggind6344532005-06-28 20:45:14 -07001669
Nick Piggin450991b2005-06-28 20:45:13 -07001670 /*
1671 * After dropping the lock and possibly sleeping here, our request
1672 * may now be mergeable after it had proven unmergeable (above).
1673 * We don't worry about that case for efficiency. It won't happen
1674 * often, and the elevators are able to handle it.
1675 */
Tejun Heo52d9e672006-01-06 09:49:58 +01001676 init_request_from_bio(req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Tao Ma9562ad92011-10-24 16:11:30 +02001678 if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags))
Jens Axboe11ccf112011-07-26 15:01:15 +02001679 req->cpu = raw_smp_processor_id();
Tejun Heodd831002010-09-03 11:56:16 +02001680
Jens Axboe73c10102011-03-08 13:19:51 +01001681 plug = current->plug;
Jens Axboe721a9602011-03-09 11:56:30 +01001682 if (plug) {
Jens Axboedc6d36c2011-04-12 10:28:28 +02001683 /*
1684 * If this is the first request added after a plug, fire
Jianpeng Ma7aef2e72013-09-11 13:21:07 -06001685 * of a plug trace.
Jens Axboedc6d36c2011-04-12 10:28:28 +02001686 */
Jianpeng Ma7aef2e72013-09-11 13:21:07 -06001687 if (!request_count)
Jens Axboedc6d36c2011-04-12 10:28:28 +02001688 trace_block_plug(q);
Shaohua Li3540d5e2011-11-16 09:21:50 +01001689 else {
Shaohua Li019ceb72011-11-16 09:21:50 +01001690 if (request_count >= BLK_MAX_REQUEST_COUNT) {
Shaohua Li3540d5e2011-11-16 09:21:50 +01001691 blk_flush_plug_list(plug, false);
Shaohua Li019ceb72011-11-16 09:21:50 +01001692 trace_block_plug(q);
1693 }
Jens Axboe73c10102011-03-08 13:19:51 +01001694 }
Shaohua Lia6327162011-08-24 16:04:32 +02001695 list_add_tail(&req->queuelist, &plug->list);
Jens Axboe320ae512013-10-24 09:20:05 +01001696 blk_account_io_start(req, true);
Jens Axboe73c10102011-03-08 13:19:51 +01001697 } else {
1698 spin_lock_irq(q->queue_lock);
1699 add_acct_request(q, req, where);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02001700 __blk_run_queue(q);
Jens Axboe73c10102011-03-08 13:19:51 +01001701out_unlock:
1702 spin_unlock_irq(q->queue_lock);
1703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
1705
1706/*
1707 * If bio->bi_dev is a partition, remap the location
1708 */
1709static inline void blk_partition_remap(struct bio *bio)
1710{
1711 struct block_device *bdev = bio->bi_bdev;
1712
Jens Axboebf2de6f2007-09-27 13:01:25 +02001713 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 struct hd_struct *p = bdev->bd_part;
Jens Axboea3623572005-11-01 09:26:16 +01001715
Kent Overstreet4f024f32013-10-11 15:44:27 -07001716 bio->bi_iter.bi_sector += p->start_sect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 bio->bi_bdev = bdev->bd_contains;
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001718
Mike Snitzerd07335e2010-11-16 12:52:38 +01001719 trace_block_bio_remap(bdev_get_queue(bio->bi_bdev), bio,
1720 bdev->bd_dev,
Kent Overstreet4f024f32013-10-11 15:44:27 -07001721 bio->bi_iter.bi_sector - p->start_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 }
1723}
1724
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725static void handle_bad_sector(struct bio *bio)
1726{
1727 char b[BDEVNAME_SIZE];
1728
1729 printk(KERN_INFO "attempt to access beyond end of device\n");
1730 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1731 bdevname(bio->bi_bdev, b),
1732 bio->bi_rw,
Kent Overstreetf73a1c72012-09-25 15:05:12 -07001733 (unsigned long long)bio_end_sector(bio),
Mike Snitzer77304d22010-11-08 14:39:12 +01001734 (long long)(i_size_read(bio->bi_bdev->bd_inode) >> 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
1736 set_bit(BIO_EOF, &bio->bi_flags);
1737}
1738
Akinobu Mitac17bb492006-12-08 02:39:46 -08001739#ifdef CONFIG_FAIL_MAKE_REQUEST
1740
1741static DECLARE_FAULT_ATTR(fail_make_request);
1742
1743static int __init setup_fail_make_request(char *str)
1744{
1745 return setup_fault_attr(&fail_make_request, str);
1746}
1747__setup("fail_make_request=", setup_fail_make_request);
1748
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001749static bool should_fail_request(struct hd_struct *part, unsigned int bytes)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001750{
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001751 return part->make_it_fail && should_fail(&fail_make_request, bytes);
Akinobu Mitac17bb492006-12-08 02:39:46 -08001752}
1753
1754static int __init fail_make_request_debugfs(void)
1755{
Akinobu Mitadd48c082011-08-03 16:21:01 -07001756 struct dentry *dir = fault_create_debugfs_attr("fail_make_request",
1757 NULL, &fail_make_request);
1758
Duan Jiong21f9fcd2014-04-11 15:58:56 +08001759 return PTR_ERR_OR_ZERO(dir);
Akinobu Mitac17bb492006-12-08 02:39:46 -08001760}
1761
1762late_initcall(fail_make_request_debugfs);
1763
1764#else /* CONFIG_FAIL_MAKE_REQUEST */
1765
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001766static inline bool should_fail_request(struct hd_struct *part,
1767 unsigned int bytes)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001768{
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001769 return false;
Akinobu Mitac17bb492006-12-08 02:39:46 -08001770}
1771
1772#endif /* CONFIG_FAIL_MAKE_REQUEST */
1773
Jens Axboec07e2b42007-07-18 13:27:58 +02001774/*
1775 * Check whether this bio extends beyond the end of the device.
1776 */
1777static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1778{
1779 sector_t maxsector;
1780
1781 if (!nr_sectors)
1782 return 0;
1783
1784 /* Test device or partition size, when known. */
Mike Snitzer77304d22010-11-08 14:39:12 +01001785 maxsector = i_size_read(bio->bi_bdev->bd_inode) >> 9;
Jens Axboec07e2b42007-07-18 13:27:58 +02001786 if (maxsector) {
Kent Overstreet4f024f32013-10-11 15:44:27 -07001787 sector_t sector = bio->bi_iter.bi_sector;
Jens Axboec07e2b42007-07-18 13:27:58 +02001788
1789 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1790 /*
1791 * This may well happen - the kernel calls bread()
1792 * without checking the size of the device, e.g., when
1793 * mounting a device.
1794 */
1795 handle_bad_sector(bio);
1796 return 1;
1797 }
1798 }
1799
1800 return 0;
1801}
1802
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001803static noinline_for_stack bool
1804generic_make_request_checks(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
Jens Axboe165125e2007-07-24 09:28:11 +02001806 struct request_queue *q;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001807 int nr_sectors = bio_sectors(bio);
Jens Axboe51fd77b2007-11-02 08:49:08 +01001808 int err = -EIO;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001809 char b[BDEVNAME_SIZE];
1810 struct hd_struct *part;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
1812 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Jens Axboec07e2b42007-07-18 13:27:58 +02001814 if (bio_check_eod(bio, nr_sectors))
1815 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001817 q = bdev_get_queue(bio->bi_bdev);
1818 if (unlikely(!q)) {
1819 printk(KERN_ERR
1820 "generic_make_request: Trying to access "
1821 "nonexistent block-device %s (%Lu)\n",
1822 bdevname(bio->bi_bdev, b),
Kent Overstreet4f024f32013-10-11 15:44:27 -07001823 (long long) bio->bi_iter.bi_sector);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001824 goto end_io;
1825 }
1826
Martin K. Petersene2a60da2012-09-18 12:19:25 -04001827 if (likely(bio_is_rw(bio) &&
1828 nr_sectors > queue_max_hw_sectors(q))) {
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001829 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
1830 bdevname(bio->bi_bdev, b),
1831 bio_sectors(bio),
1832 queue_max_hw_sectors(q));
1833 goto end_io;
1834 }
1835
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001836 part = bio->bi_bdev->bd_part;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001837 if (should_fail_request(part, bio->bi_iter.bi_size) ||
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001838 should_fail_request(&part_to_disk(part)->part0,
Kent Overstreet4f024f32013-10-11 15:44:27 -07001839 bio->bi_iter.bi_size))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001840 goto end_io;
1841
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 /*
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001843 * If this device has partitions, remap block n
1844 * of partition p to block n+start(p) of the disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001846 blk_partition_remap(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001848 if (bio_check_eod(bio, nr_sectors))
1849 goto end_io;
1850
1851 /*
1852 * Filter flush bio's early so that make_request based
1853 * drivers without flush support don't have to worry
1854 * about them.
1855 */
1856 if ((bio->bi_rw & (REQ_FLUSH | REQ_FUA)) && !q->flush_flags) {
1857 bio->bi_rw &= ~(REQ_FLUSH | REQ_FUA);
1858 if (!nr_sectors) {
1859 err = 0;
Tejun Heoa7384672008-11-28 13:32:03 +09001860 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 }
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001864 if ((bio->bi_rw & REQ_DISCARD) &&
1865 (!blk_queue_discard(q) ||
Martin K. Petersene2a60da2012-09-18 12:19:25 -04001866 ((bio->bi_rw & REQ_SECURE) && !blk_queue_secdiscard(q)))) {
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001867 err = -EOPNOTSUPP;
1868 goto end_io;
1869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Martin K. Petersen4363ac72012-09-18 12:19:27 -04001871 if (bio->bi_rw & REQ_WRITE_SAME && !bdev_write_same(bio->bi_bdev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 err = -EOPNOTSUPP;
1873 goto end_io;
1874 }
1875
Tejun Heo7f4b35d2012-06-04 20:40:56 -07001876 /*
1877 * Various block parts want %current->io_context and lazy ioc
1878 * allocation ends up trading a lot of pain for a small amount of
1879 * memory. Just allocate it upfront. This may fail and block
1880 * layer knows how to live with it.
1881 */
1882 create_io_context(GFP_ATOMIC, q->node);
1883
Tejun Heo927bdd12015-08-18 14:55:20 -07001884 if (!blkcg_bio_issue_check(q, bio))
1885 return false;
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001886
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001887 trace_block_bio_queue(q, bio);
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001888 return true;
Tejun Heoa7384672008-11-28 13:32:03 +09001889
1890end_io:
1891 bio_endio(bio, err);
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001892 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893}
1894
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001895/**
1896 * generic_make_request - hand a buffer to its device driver for I/O
1897 * @bio: The bio describing the location in memory and on the device.
1898 *
1899 * generic_make_request() is used to make I/O requests of block
1900 * devices. It is passed a &struct bio, which describes the I/O that needs
1901 * to be done.
1902 *
1903 * generic_make_request() does not return any status. The
1904 * success/failure status of the request, along with notification of
1905 * completion, is delivered asynchronously through the bio->bi_end_io
1906 * function described (one day) else where.
1907 *
1908 * The caller of generic_make_request must make sure that bi_io_vec
1909 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1910 * set to describe the device address, and the
1911 * bi_end_io and optionally bi_private are set to describe how
1912 * completion notification should be signaled.
1913 *
1914 * generic_make_request and the drivers it calls may use bi_next if this
1915 * bio happens to be merged with someone else, and may resubmit the bio to
1916 * a lower device by calling into generic_make_request recursively, which
1917 * means the bio should NOT be touched after the call to ->make_request_fn.
Neil Brownd89d8792007-05-01 09:53:42 +02001918 */
1919void generic_make_request(struct bio *bio)
1920{
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001921 struct bio_list bio_list_on_stack;
1922
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001923 if (!generic_make_request_checks(bio))
1924 return;
1925
1926 /*
1927 * We only want one ->make_request_fn to be active at a time, else
1928 * stack usage with stacked devices could be a problem. So use
1929 * current->bio_list to keep a list of requests submited by a
1930 * make_request_fn function. current->bio_list is also used as a
1931 * flag to say if generic_make_request is currently active in this
1932 * task or not. If it is NULL, then no make_request is active. If
1933 * it is non-NULL, then a make_request is active, and new requests
1934 * should be added at the tail
1935 */
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001936 if (current->bio_list) {
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001937 bio_list_add(current->bio_list, bio);
Neil Brownd89d8792007-05-01 09:53:42 +02001938 return;
1939 }
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001940
Neil Brownd89d8792007-05-01 09:53:42 +02001941 /* following loop may be a bit non-obvious, and so deserves some
1942 * explanation.
1943 * Before entering the loop, bio->bi_next is NULL (as all callers
1944 * ensure that) so we have a list with a single bio.
1945 * We pretend that we have just taken it off a longer list, so
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001946 * we assign bio_list to a pointer to the bio_list_on_stack,
1947 * thus initialising the bio_list of new bios to be
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001948 * added. ->make_request() may indeed add some more bios
Neil Brownd89d8792007-05-01 09:53:42 +02001949 * through a recursive call to generic_make_request. If it
1950 * did, we find a non-NULL value in bio_list and re-enter the loop
1951 * from the top. In this case we really did just take the bio
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001952 * of the top of the list (no pretending) and so remove it from
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001953 * bio_list, and call into ->make_request() again.
Neil Brownd89d8792007-05-01 09:53:42 +02001954 */
1955 BUG_ON(bio->bi_next);
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001956 bio_list_init(&bio_list_on_stack);
1957 current->bio_list = &bio_list_on_stack;
Neil Brownd89d8792007-05-01 09:53:42 +02001958 do {
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001959 struct request_queue *q = bdev_get_queue(bio->bi_bdev);
1960
1961 q->make_request_fn(q, bio);
1962
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001963 bio = bio_list_pop(current->bio_list);
Neil Brownd89d8792007-05-01 09:53:42 +02001964 } while (bio);
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001965 current->bio_list = NULL; /* deactivate */
Neil Brownd89d8792007-05-01 09:53:42 +02001966}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967EXPORT_SYMBOL(generic_make_request);
1968
1969/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001970 * submit_bio - submit a bio to the block device layer for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1972 * @bio: The &struct bio which describes the I/O
1973 *
1974 * submit_bio() is very similar in purpose to generic_make_request(), and
1975 * uses that function to do most of the work. Both are fairly rough
Randy Dunlap710027a2008-08-19 20:13:11 +02001976 * interfaces; @bio must be presetup and ready for I/O.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 *
1978 */
1979void submit_bio(int rw, struct bio *bio)
1980{
Jens Axboe22e2c502005-06-27 10:55:12 +02001981 bio->bi_rw |= rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
Jens Axboebf2de6f2007-09-27 13:01:25 +02001983 /*
1984 * If it's a regular read/write or a barrier with data attached,
1985 * go through the normal accounting stuff before submission.
1986 */
Martin K. Petersene2a60da2012-09-18 12:19:25 -04001987 if (bio_has_data(bio)) {
Martin K. Petersen4363ac72012-09-18 12:19:27 -04001988 unsigned int count;
1989
1990 if (unlikely(rw & REQ_WRITE_SAME))
1991 count = bdev_logical_block_size(bio->bi_bdev) >> 9;
1992 else
1993 count = bio_sectors(bio);
1994
Jens Axboebf2de6f2007-09-27 13:01:25 +02001995 if (rw & WRITE) {
1996 count_vm_events(PGPGOUT, count);
1997 } else {
Kent Overstreet4f024f32013-10-11 15:44:27 -07001998 task_io_account_read(bio->bi_iter.bi_size);
Jens Axboebf2de6f2007-09-27 13:01:25 +02001999 count_vm_events(PGPGIN, count);
2000 }
2001
2002 if (unlikely(block_dump)) {
2003 char b[BDEVNAME_SIZE];
San Mehat8dcbdc72010-09-14 08:48:01 +02002004 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s (%u sectors)\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002005 current->comm, task_pid_nr(current),
Jens Axboebf2de6f2007-09-27 13:01:25 +02002006 (rw & WRITE) ? "WRITE" : "READ",
Kent Overstreet4f024f32013-10-11 15:44:27 -07002007 (unsigned long long)bio->bi_iter.bi_sector,
San Mehat8dcbdc72010-09-14 08:48:01 +02002008 bdevname(bio->bi_bdev, b),
2009 count);
Jens Axboebf2de6f2007-09-27 13:01:25 +02002010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 }
2012
2013 generic_make_request(bio);
2014}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015EXPORT_SYMBOL(submit_bio);
2016
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002017/**
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002018 * blk_rq_check_limits - Helper function to check a request for the queue limit
2019 * @q: the queue
2020 * @rq: the request being checked
2021 *
2022 * Description:
2023 * @rq may have been made based on weaker limitations of upper-level queues
2024 * in request stacking drivers, and it may violate the limitation of @q.
2025 * Since the block layer and the underlying device driver trust @rq
2026 * after it is inserted to @q, it should be checked against @q before
2027 * the insertion using this generic function.
2028 *
2029 * This function should also be useful for request stacking drivers
Stefan Weileef35c22010-08-06 21:11:15 +02002030 * in some cases below, so export this function.
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002031 * Request stacking drivers like request-based dm may change the queue
2032 * limits while requests are in the queue (e.g. dm's table swapping).
Masanari Iidae2278672014-02-18 22:54:36 +09002033 * Such request stacking drivers should check those requests against
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002034 * the new queue limits again when they dispatch those requests,
2035 * although such checkings are also done against the old queue limits
2036 * when submitting requests.
2037 */
2038int blk_rq_check_limits(struct request_queue *q, struct request *rq)
2039{
Martin K. Petersene2a60da2012-09-18 12:19:25 -04002040 if (!rq_mergeable(rq))
ike Snitzer33839772010-08-08 12:11:33 -04002041 return 0;
2042
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -04002043 if (blk_rq_sectors(rq) > blk_queue_get_max_sectors(q, rq->cmd_flags)) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002044 printk(KERN_ERR "%s: over max size limit.\n", __func__);
2045 return -EIO;
2046 }
2047
2048 /*
2049 * queue's settings related to segment counting like q->bounce_pfn
2050 * may differ from that of other stacking queues.
2051 * Recalculate it to check the request correctly on this queue's
2052 * limitation.
2053 */
2054 blk_recalc_rq_segments(rq);
Martin K. Petersen8a783622010-02-26 00:20:39 -05002055 if (rq->nr_phys_segments > queue_max_segments(q)) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002056 printk(KERN_ERR "%s: over max segments limit.\n", __func__);
2057 return -EIO;
2058 }
2059
2060 return 0;
2061}
2062EXPORT_SYMBOL_GPL(blk_rq_check_limits);
2063
2064/**
2065 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
2066 * @q: the queue to submit the request
2067 * @rq: the request being queued
2068 */
2069int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
2070{
2071 unsigned long flags;
Jeff Moyer4853aba2011-08-15 21:37:25 +02002072 int where = ELEVATOR_INSERT_BACK;
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002073
2074 if (blk_rq_check_limits(q, rq))
2075 return -EIO;
2076
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07002077 if (rq->rq_disk &&
2078 should_fail_request(&rq->rq_disk->part0, blk_rq_bytes(rq)))
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002079 return -EIO;
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002080
Keith Busch7fb48982014-10-17 17:46:38 -06002081 if (q->mq_ops) {
2082 if (blk_queue_io_stat(q))
2083 blk_account_io_start(rq, true);
2084 blk_mq_insert_request(rq, false, true, true);
2085 return 0;
2086 }
2087
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002088 spin_lock_irqsave(q->queue_lock, flags);
Bart Van Assche3f3299d2012-11-28 13:42:38 +01002089 if (unlikely(blk_queue_dying(q))) {
Tejun Heo8ba61432011-12-14 00:33:37 +01002090 spin_unlock_irqrestore(q->queue_lock, flags);
2091 return -ENODEV;
2092 }
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002093
2094 /*
2095 * Submitting request must be dequeued before calling this function
2096 * because it will be linked to another request_queue
2097 */
2098 BUG_ON(blk_queued_rq(rq));
2099
Jeff Moyer4853aba2011-08-15 21:37:25 +02002100 if (rq->cmd_flags & (REQ_FLUSH|REQ_FUA))
2101 where = ELEVATOR_INSERT_FLUSH;
2102
2103 add_acct_request(q, rq, where);
Jeff Moyere67b77c2011-10-17 12:57:23 +02002104 if (where == ELEVATOR_INSERT_FLUSH)
2105 __blk_run_queue(q);
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002106 spin_unlock_irqrestore(q->queue_lock, flags);
2107
2108 return 0;
2109}
2110EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
2111
Tejun Heo80a761f2009-07-03 17:48:17 +09002112/**
2113 * blk_rq_err_bytes - determine number of bytes till the next failure boundary
2114 * @rq: request to examine
2115 *
2116 * Description:
2117 * A request could be merge of IOs which require different failure
2118 * handling. This function determines the number of bytes which
2119 * can be failed from the beginning of the request without
2120 * crossing into area which need to be retried further.
2121 *
2122 * Return:
2123 * The number of bytes to fail.
2124 *
2125 * Context:
2126 * queue_lock must be held.
2127 */
2128unsigned int blk_rq_err_bytes(const struct request *rq)
2129{
2130 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
2131 unsigned int bytes = 0;
2132 struct bio *bio;
2133
2134 if (!(rq->cmd_flags & REQ_MIXED_MERGE))
2135 return blk_rq_bytes(rq);
2136
2137 /*
2138 * Currently the only 'mixing' which can happen is between
2139 * different fastfail types. We can safely fail portions
2140 * which have all the failfast bits that the first one has -
2141 * the ones which are at least as eager to fail as the first
2142 * one.
2143 */
2144 for (bio = rq->bio; bio; bio = bio->bi_next) {
2145 if ((bio->bi_rw & ff) != ff)
2146 break;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002147 bytes += bio->bi_iter.bi_size;
Tejun Heo80a761f2009-07-03 17:48:17 +09002148 }
2149
2150 /* this could lead to infinite loop */
2151 BUG_ON(blk_rq_bytes(rq) && !bytes);
2152 return bytes;
2153}
2154EXPORT_SYMBOL_GPL(blk_rq_err_bytes);
2155
Jens Axboe320ae512013-10-24 09:20:05 +01002156void blk_account_io_completion(struct request *req, unsigned int bytes)
Jens Axboebc58ba92009-01-23 10:54:44 +01002157{
Jens Axboec2553b52009-04-24 08:10:11 +02002158 if (blk_do_io_stat(req)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01002159 const int rw = rq_data_dir(req);
2160 struct hd_struct *part;
2161 int cpu;
2162
2163 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +01002164 part = req->part;
Jens Axboebc58ba92009-01-23 10:54:44 +01002165 part_stat_add(cpu, part, sectors[rw], bytes >> 9);
2166 part_stat_unlock();
2167 }
2168}
2169
Jens Axboe320ae512013-10-24 09:20:05 +01002170void blk_account_io_done(struct request *req)
Jens Axboebc58ba92009-01-23 10:54:44 +01002171{
Jens Axboebc58ba92009-01-23 10:54:44 +01002172 /*
Tejun Heodd4c1332010-09-03 11:56:16 +02002173 * Account IO completion. flush_rq isn't accounted as a
2174 * normal IO on queueing nor completion. Accounting the
2175 * containing request is enough.
Jens Axboebc58ba92009-01-23 10:54:44 +01002176 */
Tejun Heo414b4ff2011-01-25 12:43:49 +01002177 if (blk_do_io_stat(req) && !(req->cmd_flags & REQ_FLUSH_SEQ)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01002178 unsigned long duration = jiffies - req->start_time;
2179 const int rw = rq_data_dir(req);
2180 struct hd_struct *part;
2181 int cpu;
2182
2183 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +01002184 part = req->part;
Jens Axboebc58ba92009-01-23 10:54:44 +01002185
2186 part_stat_inc(cpu, part, ios[rw]);
2187 part_stat_add(cpu, part, ticks[rw], duration);
2188 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02002189 part_dec_in_flight(part, rw);
Jens Axboebc58ba92009-01-23 10:54:44 +01002190
Jens Axboe6c23a962011-01-07 08:43:37 +01002191 hd_struct_put(part);
Jens Axboebc58ba92009-01-23 10:54:44 +01002192 part_stat_unlock();
2193 }
2194}
2195
Rafael J. Wysocki47fafbc2014-12-04 01:00:23 +01002196#ifdef CONFIG_PM
Lin Mingc8158812013-03-23 11:42:27 +08002197/*
2198 * Don't process normal requests when queue is suspended
2199 * or in the process of suspending/resuming
2200 */
2201static struct request *blk_pm_peek_request(struct request_queue *q,
2202 struct request *rq)
2203{
2204 if (q->dev && (q->rpm_status == RPM_SUSPENDED ||
2205 (q->rpm_status != RPM_ACTIVE && !(rq->cmd_flags & REQ_PM))))
2206 return NULL;
2207 else
2208 return rq;
2209}
2210#else
2211static inline struct request *blk_pm_peek_request(struct request_queue *q,
2212 struct request *rq)
2213{
2214 return rq;
2215}
2216#endif
2217
Jens Axboe320ae512013-10-24 09:20:05 +01002218void blk_account_io_start(struct request *rq, bool new_io)
2219{
2220 struct hd_struct *part;
2221 int rw = rq_data_dir(rq);
2222 int cpu;
2223
2224 if (!blk_do_io_stat(rq))
2225 return;
2226
2227 cpu = part_stat_lock();
2228
2229 if (!new_io) {
2230 part = rq->part;
2231 part_stat_inc(cpu, part, merges[rw]);
2232 } else {
2233 part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq));
2234 if (!hd_struct_try_get(part)) {
2235 /*
2236 * The partition is already being removed,
2237 * the request will be accounted on the disk only
2238 *
2239 * We take a reference on disk->part0 although that
2240 * partition will never be deleted, so we can treat
2241 * it as any other partition.
2242 */
2243 part = &rq->rq_disk->part0;
2244 hd_struct_get(part);
2245 }
2246 part_round_stats(cpu, part);
2247 part_inc_in_flight(part, rw);
2248 rq->part = part;
2249 }
2250
2251 part_stat_unlock();
2252}
2253
Tejun Heo53a08802008-12-03 12:41:26 +01002254/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09002255 * blk_peek_request - peek at the top of a request queue
2256 * @q: request queue to peek at
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002257 *
2258 * Description:
Tejun Heo9934c8c2009-05-08 11:54:16 +09002259 * Return the request at the top of @q. The returned request
2260 * should be started using blk_start_request() before LLD starts
2261 * processing it.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002262 *
2263 * Return:
Tejun Heo9934c8c2009-05-08 11:54:16 +09002264 * Pointer to the request at the top of @q if available. Null
2265 * otherwise.
2266 *
2267 * Context:
2268 * queue_lock must be held.
2269 */
2270struct request *blk_peek_request(struct request_queue *q)
Tejun Heo158dbda2009-04-23 11:05:18 +09002271{
2272 struct request *rq;
2273 int ret;
2274
2275 while ((rq = __elv_next_request(q)) != NULL) {
Lin Mingc8158812013-03-23 11:42:27 +08002276
2277 rq = blk_pm_peek_request(q, rq);
2278 if (!rq)
2279 break;
2280
Tejun Heo158dbda2009-04-23 11:05:18 +09002281 if (!(rq->cmd_flags & REQ_STARTED)) {
2282 /*
2283 * This is the first time the device driver
2284 * sees this request (possibly after
2285 * requeueing). Notify IO scheduler.
2286 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002287 if (rq->cmd_flags & REQ_SORTED)
Tejun Heo158dbda2009-04-23 11:05:18 +09002288 elv_activate_rq(q, rq);
2289
2290 /*
2291 * just mark as started even if we don't start
2292 * it, a request that has been delayed should
2293 * not be passed by new incoming requests
2294 */
2295 rq->cmd_flags |= REQ_STARTED;
2296 trace_block_rq_issue(q, rq);
2297 }
2298
2299 if (!q->boundary_rq || q->boundary_rq == rq) {
2300 q->end_sector = rq_end_sector(rq);
2301 q->boundary_rq = NULL;
2302 }
2303
2304 if (rq->cmd_flags & REQ_DONTPREP)
2305 break;
2306
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002307 if (q->dma_drain_size && blk_rq_bytes(rq)) {
Tejun Heo158dbda2009-04-23 11:05:18 +09002308 /*
2309 * make sure space for the drain appears we
2310 * know we can do this because max_hw_segments
2311 * has been adjusted to be one fewer than the
2312 * device can handle
2313 */
2314 rq->nr_phys_segments++;
2315 }
2316
2317 if (!q->prep_rq_fn)
2318 break;
2319
2320 ret = q->prep_rq_fn(q, rq);
2321 if (ret == BLKPREP_OK) {
2322 break;
2323 } else if (ret == BLKPREP_DEFER) {
2324 /*
2325 * the request may have been (partially) prepped.
2326 * we need to keep this request in the front to
2327 * avoid resource deadlock. REQ_STARTED will
2328 * prevent other fs requests from passing this one.
2329 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002330 if (q->dma_drain_size && blk_rq_bytes(rq) &&
Tejun Heo158dbda2009-04-23 11:05:18 +09002331 !(rq->cmd_flags & REQ_DONTPREP)) {
2332 /*
2333 * remove the space for the drain we added
2334 * so that we don't add it again
2335 */
2336 --rq->nr_phys_segments;
2337 }
2338
2339 rq = NULL;
2340 break;
2341 } else if (ret == BLKPREP_KILL) {
2342 rq->cmd_flags |= REQ_QUIET;
James Bottomleyc143dc92009-05-30 06:43:49 +02002343 /*
2344 * Mark this request as started so we don't trigger
2345 * any debug logic in the end I/O path.
2346 */
2347 blk_start_request(rq);
Tejun Heo40cbbb72009-04-23 11:05:19 +09002348 __blk_end_request_all(rq, -EIO);
Tejun Heo158dbda2009-04-23 11:05:18 +09002349 } else {
2350 printk(KERN_ERR "%s: bad return=%d\n", __func__, ret);
2351 break;
2352 }
2353 }
2354
2355 return rq;
2356}
Tejun Heo9934c8c2009-05-08 11:54:16 +09002357EXPORT_SYMBOL(blk_peek_request);
Tejun Heo158dbda2009-04-23 11:05:18 +09002358
Tejun Heo9934c8c2009-05-08 11:54:16 +09002359void blk_dequeue_request(struct request *rq)
Tejun Heo158dbda2009-04-23 11:05:18 +09002360{
Tejun Heo9934c8c2009-05-08 11:54:16 +09002361 struct request_queue *q = rq->q;
2362
Tejun Heo158dbda2009-04-23 11:05:18 +09002363 BUG_ON(list_empty(&rq->queuelist));
2364 BUG_ON(ELV_ON_HASH(rq));
2365
2366 list_del_init(&rq->queuelist);
2367
2368 /*
2369 * the time frame between a request being removed from the lists
2370 * and to it is freed is accounted as io that is in progress at
2371 * the driver side.
2372 */
Divyesh Shah91952912010-04-01 15:01:41 -07002373 if (blk_account_rq(rq)) {
Jens Axboe0a7ae2f2009-05-20 08:54:31 +02002374 q->in_flight[rq_is_sync(rq)]++;
Divyesh Shah91952912010-04-01 15:01:41 -07002375 set_io_start_time_ns(rq);
2376 }
Tejun Heo158dbda2009-04-23 11:05:18 +09002377}
2378
Tejun Heo5efccd12009-04-23 11:05:18 +09002379/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09002380 * blk_start_request - start request processing on the driver
2381 * @req: request to dequeue
2382 *
2383 * Description:
2384 * Dequeue @req and start timeout timer on it. This hands off the
2385 * request to the driver.
2386 *
2387 * Block internal functions which don't want to start timer should
2388 * call blk_dequeue_request().
2389 *
2390 * Context:
2391 * queue_lock must be held.
2392 */
2393void blk_start_request(struct request *req)
2394{
2395 blk_dequeue_request(req);
2396
2397 /*
Tejun Heo5f49f632009-05-19 18:33:05 +09002398 * We are now handing the request to the hardware, initialize
2399 * resid_len to full count and add the timeout handler.
Tejun Heo9934c8c2009-05-08 11:54:16 +09002400 */
Tejun Heo5f49f632009-05-19 18:33:05 +09002401 req->resid_len = blk_rq_bytes(req);
FUJITA Tomonoridbb66c42009-06-09 05:47:10 +02002402 if (unlikely(blk_bidi_rq(req)))
2403 req->next_rq->resid_len = blk_rq_bytes(req->next_rq);
2404
Jeff Moyer4912aa62013-10-08 14:36:41 -04002405 BUG_ON(test_bit(REQ_ATOM_COMPLETE, &req->atomic_flags));
Tejun Heo9934c8c2009-05-08 11:54:16 +09002406 blk_add_timer(req);
2407}
2408EXPORT_SYMBOL(blk_start_request);
2409
2410/**
2411 * blk_fetch_request - fetch a request from a request queue
2412 * @q: request queue to fetch a request from
2413 *
2414 * Description:
2415 * Return the request at the top of @q. The request is started on
2416 * return and LLD can start processing it immediately.
2417 *
2418 * Return:
2419 * Pointer to the request at the top of @q if available. Null
2420 * otherwise.
2421 *
2422 * Context:
2423 * queue_lock must be held.
2424 */
2425struct request *blk_fetch_request(struct request_queue *q)
2426{
2427 struct request *rq;
2428
2429 rq = blk_peek_request(q);
2430 if (rq)
2431 blk_start_request(rq);
2432 return rq;
2433}
2434EXPORT_SYMBOL(blk_fetch_request);
2435
2436/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002437 * blk_update_request - Special helper function for request stacking drivers
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002438 * @req: the request being processed
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002439 * @error: %0 for success, < %0 for error
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002440 * @nr_bytes: number of bytes to complete @req
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002441 *
2442 * Description:
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002443 * Ends I/O on a number of bytes attached to @req, but doesn't complete
2444 * the request structure even if @req doesn't have leftover.
2445 * If @req has leftover, sets it up for the next range of segments.
Tejun Heo2e60e022009-04-23 11:05:18 +09002446 *
2447 * This special helper function is only for request stacking drivers
2448 * (e.g. request-based dm) so that they can handle partial completion.
2449 * Actual device drivers should use blk_end_request instead.
2450 *
2451 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
2452 * %false return from this function.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002453 *
2454 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002455 * %false - this request doesn't have any more data
2456 * %true - this request has more data
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002457 **/
Tejun Heo2e60e022009-04-23 11:05:18 +09002458bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459{
Kent Overstreetf79ea412012-09-20 16:38:30 -07002460 int total_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
Hannes Reinecke4a0efdc2014-10-01 14:32:31 +02002462 trace_block_rq_complete(req->q, req, nr_bytes);
2463
Tejun Heo2e60e022009-04-23 11:05:18 +09002464 if (!req->bio)
2465 return false;
2466
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 /*
Tejun Heo6f414692009-04-19 07:00:41 +09002468 * For fs requests, rq is just carrier of independent bio's
2469 * and each partial completion should be handled separately.
2470 * Reset per-request error on each partial completion.
2471 *
2472 * TODO: tj: This is too subtle. It would be better to let
2473 * low level drivers do what they see fit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002475 if (req->cmd_type == REQ_TYPE_FS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 req->errors = 0;
2477
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002478 if (error && req->cmd_type == REQ_TYPE_FS &&
2479 !(req->cmd_flags & REQ_QUIET)) {
Hannes Reinecke79775562011-01-18 10:13:13 +01002480 char *error_type;
2481
2482 switch (error) {
2483 case -ENOLINK:
2484 error_type = "recoverable transport";
2485 break;
2486 case -EREMOTEIO:
2487 error_type = "critical target";
2488 break;
2489 case -EBADE:
2490 error_type = "critical nexus";
2491 break;
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002492 case -ETIMEDOUT:
2493 error_type = "timeout";
2494 break;
Hannes Reineckea9d6ceb82013-07-01 15:16:25 +02002495 case -ENOSPC:
2496 error_type = "critical space allocation";
2497 break;
Hannes Reinecke7e782af2013-07-01 15:16:26 +02002498 case -ENODATA:
2499 error_type = "critical medium";
2500 break;
Hannes Reinecke79775562011-01-18 10:13:13 +01002501 case -EIO:
2502 default:
2503 error_type = "I/O";
2504 break;
2505 }
Robert Elliottef3ecb62014-08-27 10:50:31 -05002506 printk_ratelimited(KERN_ERR "%s: %s error, dev %s, sector %llu\n",
2507 __func__, error_type, req->rq_disk ?
Yi Zou37d7b342012-08-30 16:26:25 -07002508 req->rq_disk->disk_name : "?",
2509 (unsigned long long)blk_rq_pos(req));
2510
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 }
2512
Jens Axboebc58ba92009-01-23 10:54:44 +01002513 blk_account_io_completion(req, nr_bytes);
Jens Axboed72d9042005-11-01 08:35:42 +01002514
Kent Overstreetf79ea412012-09-20 16:38:30 -07002515 total_bytes = 0;
2516 while (req->bio) {
2517 struct bio *bio = req->bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002518 unsigned bio_bytes = min(bio->bi_iter.bi_size, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
Kent Overstreet4f024f32013-10-11 15:44:27 -07002520 if (bio_bytes == bio->bi_iter.bi_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 req->bio = bio->bi_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
Kent Overstreetf79ea412012-09-20 16:38:30 -07002523 req_bio_endio(req, bio, bio_bytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Kent Overstreetf79ea412012-09-20 16:38:30 -07002525 total_bytes += bio_bytes;
2526 nr_bytes -= bio_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
Kent Overstreetf79ea412012-09-20 16:38:30 -07002528 if (!nr_bytes)
2529 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 }
2531
2532 /*
2533 * completely done
2534 */
Tejun Heo2e60e022009-04-23 11:05:18 +09002535 if (!req->bio) {
2536 /*
2537 * Reset counters so that the request stacking driver
2538 * can find how many bytes remain in the request
2539 * later.
2540 */
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002541 req->__data_len = 0;
Tejun Heo2e60e022009-04-23 11:05:18 +09002542 return false;
2543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002545 req->__data_len -= total_bytes;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002546
2547 /* update sector only for requests with clear definition of sector */
Martin K. Petersene2a60da2012-09-18 12:19:25 -04002548 if (req->cmd_type == REQ_TYPE_FS)
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002549 req->__sector += total_bytes >> 9;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002550
Tejun Heo80a761f2009-07-03 17:48:17 +09002551 /* mixed attributes always follow the first bio */
2552 if (req->cmd_flags & REQ_MIXED_MERGE) {
2553 req->cmd_flags &= ~REQ_FAILFAST_MASK;
2554 req->cmd_flags |= req->bio->bi_rw & REQ_FAILFAST_MASK;
2555 }
2556
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002557 /*
2558 * If total number of sectors is less than the first segment
2559 * size, something has gone terribly wrong.
2560 */
2561 if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
Jens Axboe81829242011-03-30 09:51:33 +02002562 blk_dump_rq_flags(req, "request botched");
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002563 req->__data_len = blk_rq_cur_bytes(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002564 }
2565
2566 /* recalculate the number of segments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 blk_recalc_rq_segments(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002568
Tejun Heo2e60e022009-04-23 11:05:18 +09002569 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570}
Tejun Heo2e60e022009-04-23 11:05:18 +09002571EXPORT_SYMBOL_GPL(blk_update_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
Tejun Heo2e60e022009-04-23 11:05:18 +09002573static bool blk_update_bidi_request(struct request *rq, int error,
2574 unsigned int nr_bytes,
2575 unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002576{
Tejun Heo2e60e022009-04-23 11:05:18 +09002577 if (blk_update_request(rq, error, nr_bytes))
2578 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002579
Tejun Heo2e60e022009-04-23 11:05:18 +09002580 /* Bidi request must be completed as a whole */
2581 if (unlikely(blk_bidi_rq(rq)) &&
2582 blk_update_request(rq->next_rq, error, bidi_bytes))
2583 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002584
Jens Axboee2e1a142010-06-09 10:42:09 +02002585 if (blk_queue_add_random(rq->q))
2586 add_disk_randomness(rq->rq_disk);
Tejun Heo2e60e022009-04-23 11:05:18 +09002587
2588 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589}
2590
James Bottomley28018c22010-07-01 19:49:17 +09002591/**
2592 * blk_unprep_request - unprepare a request
2593 * @req: the request
2594 *
2595 * This function makes a request ready for complete resubmission (or
2596 * completion). It happens only after all error handling is complete,
2597 * so represents the appropriate moment to deallocate any resources
2598 * that were allocated to the request in the prep_rq_fn. The queue
2599 * lock is held when calling this.
2600 */
2601void blk_unprep_request(struct request *req)
2602{
2603 struct request_queue *q = req->q;
2604
2605 req->cmd_flags &= ~REQ_DONTPREP;
2606 if (q->unprep_rq_fn)
2607 q->unprep_rq_fn(q, req);
2608}
2609EXPORT_SYMBOL_GPL(blk_unprep_request);
2610
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611/*
2612 * queue lock must be held
2613 */
Christoph Hellwig12120072014-04-16 09:44:59 +02002614void blk_finish_request(struct request *req, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615{
Christoph Hellwig125c99b2014-11-03 12:47:47 +01002616 if (req->cmd_flags & REQ_QUEUED)
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002617 blk_queue_end_tag(req->q, req);
2618
James Bottomleyba396a62009-05-27 14:17:08 +02002619 BUG_ON(blk_queued_rq(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002621 if (unlikely(laptop_mode) && req->cmd_type == REQ_TYPE_FS)
Matthew Garrett31373d02010-04-06 14:25:14 +02002622 laptop_io_completion(&req->q->backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
Mike Andersone78042e2008-10-30 02:16:20 -07002624 blk_delete_timer(req);
2625
James Bottomley28018c22010-07-01 19:49:17 +09002626 if (req->cmd_flags & REQ_DONTPREP)
2627 blk_unprep_request(req);
2628
Jens Axboebc58ba92009-01-23 10:54:44 +01002629 blk_account_io_done(req);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002630
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 if (req->end_io)
Tejun Heo8ffdc652006-01-06 09:49:03 +01002632 req->end_io(req, error);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002633 else {
2634 if (blk_bidi_rq(req))
2635 __blk_put_request(req->next_rq->q, req->next_rq);
2636
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 __blk_put_request(req->q, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 }
2639}
Christoph Hellwig12120072014-04-16 09:44:59 +02002640EXPORT_SYMBOL(blk_finish_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05002642/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002643 * blk_end_bidi_request - Complete a bidi request
2644 * @rq: the request to complete
Randy Dunlap710027a2008-08-19 20:13:11 +02002645 * @error: %0 for success, < %0 for error
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002646 * @nr_bytes: number of bytes to complete @rq
2647 * @bidi_bytes: number of bytes to complete @rq->next_rq
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002648 *
2649 * Description:
2650 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
Tejun Heo2e60e022009-04-23 11:05:18 +09002651 * Drivers that supports bidi can safely call this member for any
2652 * type of request, bidi or uni. In the later case @bidi_bytes is
2653 * just ignored.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002654 *
2655 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002656 * %false - we are done with this request
2657 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002658 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002659static bool blk_end_bidi_request(struct request *rq, int error,
2660 unsigned int nr_bytes, unsigned int bidi_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002661{
2662 struct request_queue *q = rq->q;
Tejun Heo2e60e022009-04-23 11:05:18 +09002663 unsigned long flags;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002664
Tejun Heo2e60e022009-04-23 11:05:18 +09002665 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2666 return true;
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002667
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002668 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo2e60e022009-04-23 11:05:18 +09002669 blk_finish_request(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002670 spin_unlock_irqrestore(q->queue_lock, flags);
2671
Tejun Heo2e60e022009-04-23 11:05:18 +09002672 return false;
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002673}
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002674
2675/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002676 * __blk_end_bidi_request - Complete a bidi request with queue lock held
2677 * @rq: the request to complete
2678 * @error: %0 for success, < %0 for error
2679 * @nr_bytes: number of bytes to complete @rq
2680 * @bidi_bytes: number of bytes to complete @rq->next_rq
Tejun Heo5efccd12009-04-23 11:05:18 +09002681 *
2682 * Description:
Tejun Heo2e60e022009-04-23 11:05:18 +09002683 * Identical to blk_end_bidi_request() except that queue lock is
2684 * assumed to be locked on entry and remains so on return.
Tejun Heo5efccd12009-04-23 11:05:18 +09002685 *
Tejun Heo2e60e022009-04-23 11:05:18 +09002686 * Return:
2687 * %false - we are done with this request
2688 * %true - still buffers pending for this request
Tejun Heo5efccd12009-04-23 11:05:18 +09002689 **/
Jeff Moyer4853aba2011-08-15 21:37:25 +02002690bool __blk_end_bidi_request(struct request *rq, int error,
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002691 unsigned int nr_bytes, unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002692{
Tejun Heo2e60e022009-04-23 11:05:18 +09002693 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2694 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002695
Tejun Heo2e60e022009-04-23 11:05:18 +09002696 blk_finish_request(rq, error);
Tejun Heo5efccd12009-04-23 11:05:18 +09002697
Tejun Heo2e60e022009-04-23 11:05:18 +09002698 return false;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002699}
2700
2701/**
2702 * blk_end_request - Helper function for drivers to complete the request.
2703 * @rq: the request being processed
2704 * @error: %0 for success, < %0 for error
2705 * @nr_bytes: number of bytes to complete
2706 *
2707 * Description:
2708 * Ends I/O on a number of bytes attached to @rq.
2709 * If @rq has leftover, sets it up for the next range of segments.
2710 *
2711 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002712 * %false - we are done with this request
2713 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002714 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002715bool blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002716{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002717 return blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002718}
Jens Axboe56ad1742009-07-28 22:11:24 +02002719EXPORT_SYMBOL(blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002720
2721/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002722 * blk_end_request_all - Helper function for drives to finish the request.
2723 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002724 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002725 *
2726 * Description:
2727 * Completely finish @rq.
2728 */
2729void blk_end_request_all(struct request *rq, int error)
2730{
2731 bool pending;
2732 unsigned int bidi_bytes = 0;
2733
2734 if (unlikely(blk_bidi_rq(rq)))
2735 bidi_bytes = blk_rq_bytes(rq->next_rq);
2736
2737 pending = blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2738 BUG_ON(pending);
2739}
Jens Axboe56ad1742009-07-28 22:11:24 +02002740EXPORT_SYMBOL(blk_end_request_all);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002741
2742/**
2743 * blk_end_request_cur - Helper function to finish the current request chunk.
2744 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002745 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002746 *
2747 * Description:
2748 * Complete the current consecutively mapped chunk from @rq.
2749 *
2750 * Return:
2751 * %false - we are done with this request
2752 * %true - still buffers pending for this request
2753 */
2754bool blk_end_request_cur(struct request *rq, int error)
2755{
2756 return blk_end_request(rq, error, blk_rq_cur_bytes(rq));
2757}
Jens Axboe56ad1742009-07-28 22:11:24 +02002758EXPORT_SYMBOL(blk_end_request_cur);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002759
2760/**
Tejun Heo80a761f2009-07-03 17:48:17 +09002761 * blk_end_request_err - Finish a request till the next failure boundary.
2762 * @rq: the request to finish till the next failure boundary for
2763 * @error: must be negative errno
2764 *
2765 * Description:
2766 * Complete @rq till the next failure boundary.
2767 *
2768 * Return:
2769 * %false - we are done with this request
2770 * %true - still buffers pending for this request
2771 */
2772bool blk_end_request_err(struct request *rq, int error)
2773{
2774 WARN_ON(error >= 0);
2775 return blk_end_request(rq, error, blk_rq_err_bytes(rq));
2776}
2777EXPORT_SYMBOL_GPL(blk_end_request_err);
2778
2779/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002780 * __blk_end_request - Helper function for drivers to complete the request.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002781 * @rq: the request being processed
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002782 * @error: %0 for success, < %0 for error
2783 * @nr_bytes: number of bytes to complete
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002784 *
2785 * Description:
2786 * Must be called with queue lock held unlike blk_end_request().
2787 *
2788 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002789 * %false - we are done with this request
2790 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002791 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002792bool __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002793{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002794 return __blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002795}
Jens Axboe56ad1742009-07-28 22:11:24 +02002796EXPORT_SYMBOL(__blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002797
2798/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002799 * __blk_end_request_all - Helper function for drives to finish the request.
2800 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002801 * @error: %0 for success, < %0 for error
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002802 *
2803 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002804 * Completely finish @rq. Must be called with queue lock held.
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002805 */
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002806void __blk_end_request_all(struct request *rq, int error)
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002807{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002808 bool pending;
2809 unsigned int bidi_bytes = 0;
2810
2811 if (unlikely(blk_bidi_rq(rq)))
2812 bidi_bytes = blk_rq_bytes(rq->next_rq);
2813
2814 pending = __blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2815 BUG_ON(pending);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002816}
Jens Axboe56ad1742009-07-28 22:11:24 +02002817EXPORT_SYMBOL(__blk_end_request_all);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002818
2819/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002820 * __blk_end_request_cur - Helper function to finish the current request chunk.
2821 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002822 * @error: %0 for success, < %0 for error
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002823 *
2824 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002825 * Complete the current consecutively mapped chunk from @rq. Must
2826 * be called with queue lock held.
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002827 *
2828 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002829 * %false - we are done with this request
2830 * %true - still buffers pending for this request
2831 */
2832bool __blk_end_request_cur(struct request *rq, int error)
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002833{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002834 return __blk_end_request(rq, error, blk_rq_cur_bytes(rq));
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002835}
Jens Axboe56ad1742009-07-28 22:11:24 +02002836EXPORT_SYMBOL(__blk_end_request_cur);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002837
Tejun Heo80a761f2009-07-03 17:48:17 +09002838/**
2839 * __blk_end_request_err - Finish a request till the next failure boundary.
2840 * @rq: the request to finish till the next failure boundary for
2841 * @error: must be negative errno
2842 *
2843 * Description:
2844 * Complete @rq till the next failure boundary. Must be called
2845 * with queue lock held.
2846 *
2847 * Return:
2848 * %false - we are done with this request
2849 * %true - still buffers pending for this request
2850 */
2851bool __blk_end_request_err(struct request *rq, int error)
2852{
2853 WARN_ON(error >= 0);
2854 return __blk_end_request(rq, error, blk_rq_err_bytes(rq));
2855}
2856EXPORT_SYMBOL_GPL(__blk_end_request_err);
2857
Jens Axboe86db1e22008-01-29 14:53:40 +01002858void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2859 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860{
Tejun Heoa82afdf2009-07-03 17:48:16 +09002861 /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002862 rq->cmd_flags |= bio->bi_rw & REQ_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
Jens Axboeb4f42e22014-04-10 09:46:28 -06002864 if (bio_has_data(bio))
David Woodhousefb2dce82008-08-05 18:01:53 +01002865 rq->nr_phys_segments = bio_phys_segments(q, bio);
Jens Axboeb4f42e22014-04-10 09:46:28 -06002866
Kent Overstreet4f024f32013-10-11 15:44:27 -07002867 rq->__data_len = bio->bi_iter.bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 rq->bio = rq->biotail = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
NeilBrown66846572007-08-16 13:31:28 +02002870 if (bio->bi_bdev)
2871 rq->rq_disk = bio->bi_bdev->bd_disk;
2872}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
Ilya Loginov2d4dc892009-11-26 09:16:19 +01002874#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
2875/**
2876 * rq_flush_dcache_pages - Helper function to flush all pages in a request
2877 * @rq: the request to be flushed
2878 *
2879 * Description:
2880 * Flush all pages in @rq.
2881 */
2882void rq_flush_dcache_pages(struct request *rq)
2883{
2884 struct req_iterator iter;
Kent Overstreet79886132013-11-23 17:19:00 -08002885 struct bio_vec bvec;
Ilya Loginov2d4dc892009-11-26 09:16:19 +01002886
2887 rq_for_each_segment(bvec, rq, iter)
Kent Overstreet79886132013-11-23 17:19:00 -08002888 flush_dcache_page(bvec.bv_page);
Ilya Loginov2d4dc892009-11-26 09:16:19 +01002889}
2890EXPORT_SYMBOL_GPL(rq_flush_dcache_pages);
2891#endif
2892
Kiyoshi Uedaef9e3fa2008-10-01 16:12:15 +02002893/**
2894 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
2895 * @q : the queue of the device being checked
2896 *
2897 * Description:
2898 * Check if underlying low-level drivers of a device are busy.
2899 * If the drivers want to export their busy state, they must set own
2900 * exporting function using blk_queue_lld_busy() first.
2901 *
2902 * Basically, this function is used only by request stacking drivers
2903 * to stop dispatching requests to underlying devices when underlying
2904 * devices are busy. This behavior helps more I/O merging on the queue
2905 * of the request stacking driver and prevents I/O throughput regression
2906 * on burst I/O load.
2907 *
2908 * Return:
2909 * 0 - Not busy (The request stacking driver should dispatch request)
2910 * 1 - Busy (The request stacking driver should stop dispatching request)
2911 */
2912int blk_lld_busy(struct request_queue *q)
2913{
2914 if (q->lld_busy_fn)
2915 return q->lld_busy_fn(q);
2916
2917 return 0;
2918}
2919EXPORT_SYMBOL_GPL(blk_lld_busy);
2920
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002921/**
2922 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
2923 * @rq: the clone request to be cleaned up
2924 *
2925 * Description:
2926 * Free all bios in @rq for a cloned request.
2927 */
2928void blk_rq_unprep_clone(struct request *rq)
2929{
2930 struct bio *bio;
2931
2932 while ((bio = rq->bio) != NULL) {
2933 rq->bio = bio->bi_next;
2934
2935 bio_put(bio);
2936 }
2937}
2938EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
2939
2940/*
2941 * Copy attributes of the original request to the clone request.
Jens Axboeb4f42e22014-04-10 09:46:28 -06002942 * The actual data parts (e.g. ->cmd, ->sense) are not copied.
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002943 */
2944static void __blk_rq_prep_clone(struct request *dst, struct request *src)
2945{
2946 dst->cpu = src->cpu;
Keith Busch77a08682015-01-12 15:04:06 -05002947 dst->cmd_flags |= (src->cmd_flags & REQ_CLONE_MASK) | REQ_NOMERGE;
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002948 dst->cmd_type = src->cmd_type;
2949 dst->__sector = blk_rq_pos(src);
2950 dst->__data_len = blk_rq_bytes(src);
2951 dst->nr_phys_segments = src->nr_phys_segments;
2952 dst->ioprio = src->ioprio;
2953 dst->extra_len = src->extra_len;
2954}
2955
2956/**
2957 * blk_rq_prep_clone - Helper function to setup clone request
2958 * @rq: the request to be setup
2959 * @rq_src: original request to be cloned
2960 * @bs: bio_set that bios for clone are allocated from
2961 * @gfp_mask: memory allocation mask for bio
2962 * @bio_ctr: setup function to be called for each clone bio.
2963 * Returns %0 for success, non %0 for failure.
2964 * @data: private data to be passed to @bio_ctr
2965 *
2966 * Description:
2967 * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
Jens Axboeb4f42e22014-04-10 09:46:28 -06002968 * The actual data parts of @rq_src (e.g. ->cmd, ->sense)
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002969 * are not copied, and copying such parts is the caller's responsibility.
2970 * Also, pages which the original bios are pointing to are not copied
2971 * and the cloned bios just point same pages.
2972 * So cloned bios must be completed before original bios, which means
2973 * the caller must complete @rq before @rq_src.
2974 */
2975int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
2976 struct bio_set *bs, gfp_t gfp_mask,
2977 int (*bio_ctr)(struct bio *, struct bio *, void *),
2978 void *data)
2979{
2980 struct bio *bio, *bio_src;
2981
2982 if (!bs)
2983 bs = fs_bio_set;
2984
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002985 __rq_for_each_bio(bio_src, rq_src) {
Junichi Nomura11dfce52014-10-03 17:27:11 -04002986 bio = bio_clone_fast(bio_src, gfp_mask, bs);
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002987 if (!bio)
2988 goto free_and_out;
2989
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002990 if (bio_ctr && bio_ctr(bio, bio_src, data))
2991 goto free_and_out;
2992
2993 if (rq->bio) {
2994 rq->biotail->bi_next = bio;
2995 rq->biotail = bio;
2996 } else
2997 rq->bio = rq->biotail = bio;
2998 }
2999
3000 __blk_rq_prep_clone(rq, rq_src);
3001
3002 return 0;
3003
3004free_and_out:
3005 if (bio)
Kent Overstreet4254bba2012-09-06 15:35:00 -07003006 bio_put(bio);
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02003007 blk_rq_unprep_clone(rq);
3008
3009 return -ENOMEM;
3010}
3011EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
3012
Jens Axboe59c3d452014-04-08 09:15:35 -06003013int kblockd_schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014{
3015 return queue_work(kblockd_workqueue, work);
3016}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017EXPORT_SYMBOL(kblockd_schedule_work);
3018
Jens Axboe59c3d452014-04-08 09:15:35 -06003019int kblockd_schedule_delayed_work(struct delayed_work *dwork,
3020 unsigned long delay)
Vivek Goyale43473b2010-09-15 17:06:35 -04003021{
3022 return queue_delayed_work(kblockd_workqueue, dwork, delay);
3023}
3024EXPORT_SYMBOL(kblockd_schedule_delayed_work);
3025
Jens Axboe8ab14592014-04-08 09:17:40 -06003026int kblockd_schedule_delayed_work_on(int cpu, struct delayed_work *dwork,
3027 unsigned long delay)
3028{
3029 return queue_delayed_work_on(cpu, kblockd_workqueue, dwork, delay);
3030}
3031EXPORT_SYMBOL(kblockd_schedule_delayed_work_on);
3032
Suresh Jayaraman75df7132011-09-21 10:00:16 +02003033/**
3034 * blk_start_plug - initialize blk_plug and track it inside the task_struct
3035 * @plug: The &struct blk_plug that needs to be initialized
3036 *
3037 * Description:
3038 * Tracking blk_plug inside the task_struct will help with auto-flushing the
3039 * pending I/O should the task end up blocking between blk_start_plug() and
3040 * blk_finish_plug(). This is important from a performance perspective, but
3041 * also ensures that we don't deadlock. For instance, if the task is blocking
3042 * for a memory allocation, memory reclaim could end up wanting to free a
3043 * page belonging to that request that is currently residing in our private
3044 * plug. By flushing the pending I/O when the process goes to sleep, we avoid
3045 * this kind of deadlock.
3046 */
Jens Axboe73c10102011-03-08 13:19:51 +01003047void blk_start_plug(struct blk_plug *plug)
3048{
3049 struct task_struct *tsk = current;
3050
Jens Axboe73c10102011-03-08 13:19:51 +01003051 INIT_LIST_HEAD(&plug->list);
Jens Axboe320ae512013-10-24 09:20:05 +01003052 INIT_LIST_HEAD(&plug->mq_list);
NeilBrown048c9372011-04-18 09:52:22 +02003053 INIT_LIST_HEAD(&plug->cb_list);
Jens Axboe73c10102011-03-08 13:19:51 +01003054
3055 /*
3056 * If this is a nested plug, don't actually assign it. It will be
3057 * flushed on its own.
3058 */
3059 if (!tsk->plug) {
3060 /*
3061 * Store ordering should not be needed here, since a potential
3062 * preempt will imply a full memory barrier
3063 */
3064 tsk->plug = plug;
3065 }
3066}
3067EXPORT_SYMBOL(blk_start_plug);
3068
3069static int plug_rq_cmp(void *priv, struct list_head *a, struct list_head *b)
3070{
3071 struct request *rqa = container_of(a, struct request, queuelist);
3072 struct request *rqb = container_of(b, struct request, queuelist);
3073
Jianpeng Ma975927b2012-10-25 21:58:17 +02003074 return !(rqa->q < rqb->q ||
3075 (rqa->q == rqb->q && blk_rq_pos(rqa) < blk_rq_pos(rqb)));
Jens Axboe73c10102011-03-08 13:19:51 +01003076}
3077
Jens Axboe49cac012011-04-16 13:51:05 +02003078/*
3079 * If 'from_schedule' is true, then postpone the dispatch of requests
3080 * until a safe kblockd context. We due this to avoid accidental big
3081 * additional stack usage in driver dispatch, in places where the originally
3082 * plugger did not intend it.
3083 */
Jens Axboef6603782011-04-15 15:49:07 +02003084static void queue_unplugged(struct request_queue *q, unsigned int depth,
Jens Axboe49cac012011-04-16 13:51:05 +02003085 bool from_schedule)
Jens Axboe99e22592011-04-18 09:59:55 +02003086 __releases(q->queue_lock)
Jens Axboe94b5eb22011-04-12 10:12:19 +02003087{
Jens Axboe49cac012011-04-16 13:51:05 +02003088 trace_block_unplug(q, depth, !from_schedule);
Jens Axboe99e22592011-04-18 09:59:55 +02003089
Bart Van Assche70460572012-11-28 13:45:56 +01003090 if (from_schedule)
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003091 blk_run_queue_async(q);
Bart Van Assche70460572012-11-28 13:45:56 +01003092 else
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003093 __blk_run_queue(q);
Bart Van Assche70460572012-11-28 13:45:56 +01003094 spin_unlock(q->queue_lock);
Jens Axboe94b5eb22011-04-12 10:12:19 +02003095}
3096
NeilBrown74018dc2012-07-31 09:08:15 +02003097static void flush_plug_callbacks(struct blk_plug *plug, bool from_schedule)
NeilBrown048c9372011-04-18 09:52:22 +02003098{
3099 LIST_HEAD(callbacks);
3100
Shaohua Li2a7d5552012-07-31 09:08:15 +02003101 while (!list_empty(&plug->cb_list)) {
3102 list_splice_init(&plug->cb_list, &callbacks);
NeilBrown048c9372011-04-18 09:52:22 +02003103
Shaohua Li2a7d5552012-07-31 09:08:15 +02003104 while (!list_empty(&callbacks)) {
3105 struct blk_plug_cb *cb = list_first_entry(&callbacks,
NeilBrown048c9372011-04-18 09:52:22 +02003106 struct blk_plug_cb,
3107 list);
Shaohua Li2a7d5552012-07-31 09:08:15 +02003108 list_del(&cb->list);
NeilBrown74018dc2012-07-31 09:08:15 +02003109 cb->callback(cb, from_schedule);
Shaohua Li2a7d5552012-07-31 09:08:15 +02003110 }
NeilBrown048c9372011-04-18 09:52:22 +02003111 }
3112}
3113
NeilBrown9cbb1752012-07-31 09:08:14 +02003114struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug, void *data,
3115 int size)
3116{
3117 struct blk_plug *plug = current->plug;
3118 struct blk_plug_cb *cb;
3119
3120 if (!plug)
3121 return NULL;
3122
3123 list_for_each_entry(cb, &plug->cb_list, list)
3124 if (cb->callback == unplug && cb->data == data)
3125 return cb;
3126
3127 /* Not currently on the callback list */
3128 BUG_ON(size < sizeof(*cb));
3129 cb = kzalloc(size, GFP_ATOMIC);
3130 if (cb) {
3131 cb->data = data;
3132 cb->callback = unplug;
3133 list_add(&cb->list, &plug->cb_list);
3134 }
3135 return cb;
3136}
3137EXPORT_SYMBOL(blk_check_plugged);
3138
Jens Axboe49cac012011-04-16 13:51:05 +02003139void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
Jens Axboe73c10102011-03-08 13:19:51 +01003140{
3141 struct request_queue *q;
3142 unsigned long flags;
3143 struct request *rq;
NeilBrown109b8122011-04-11 14:13:10 +02003144 LIST_HEAD(list);
Jens Axboe94b5eb22011-04-12 10:12:19 +02003145 unsigned int depth;
Jens Axboe73c10102011-03-08 13:19:51 +01003146
NeilBrown74018dc2012-07-31 09:08:15 +02003147 flush_plug_callbacks(plug, from_schedule);
Jens Axboe320ae512013-10-24 09:20:05 +01003148
3149 if (!list_empty(&plug->mq_list))
3150 blk_mq_flush_plug_list(plug, from_schedule);
3151
Jens Axboe73c10102011-03-08 13:19:51 +01003152 if (list_empty(&plug->list))
3153 return;
3154
NeilBrown109b8122011-04-11 14:13:10 +02003155 list_splice_init(&plug->list, &list);
3156
Jianpeng Ma422765c2013-01-11 14:46:09 +01003157 list_sort(NULL, &list, plug_rq_cmp);
Jens Axboe73c10102011-03-08 13:19:51 +01003158
3159 q = NULL;
Jens Axboe94b5eb22011-04-12 10:12:19 +02003160 depth = 0;
Jens Axboe18811272011-04-12 10:11:24 +02003161
3162 /*
3163 * Save and disable interrupts here, to avoid doing it for every
3164 * queue lock we have to take.
3165 */
Jens Axboe73c10102011-03-08 13:19:51 +01003166 local_irq_save(flags);
NeilBrown109b8122011-04-11 14:13:10 +02003167 while (!list_empty(&list)) {
3168 rq = list_entry_rq(list.next);
Jens Axboe73c10102011-03-08 13:19:51 +01003169 list_del_init(&rq->queuelist);
Jens Axboe73c10102011-03-08 13:19:51 +01003170 BUG_ON(!rq->q);
3171 if (rq->q != q) {
Jens Axboe99e22592011-04-18 09:59:55 +02003172 /*
3173 * This drops the queue lock
3174 */
3175 if (q)
Jens Axboe49cac012011-04-16 13:51:05 +02003176 queue_unplugged(q, depth, from_schedule);
Jens Axboe73c10102011-03-08 13:19:51 +01003177 q = rq->q;
Jens Axboe94b5eb22011-04-12 10:12:19 +02003178 depth = 0;
Jens Axboe73c10102011-03-08 13:19:51 +01003179 spin_lock(q->queue_lock);
3180 }
Tejun Heo8ba61432011-12-14 00:33:37 +01003181
3182 /*
3183 * Short-circuit if @q is dead
3184 */
Bart Van Assche3f3299d2012-11-28 13:42:38 +01003185 if (unlikely(blk_queue_dying(q))) {
Tejun Heo8ba61432011-12-14 00:33:37 +01003186 __blk_end_request_all(rq, -ENODEV);
3187 continue;
3188 }
3189
Jens Axboe73c10102011-03-08 13:19:51 +01003190 /*
3191 * rq is already accounted, so use raw insert
3192 */
Jens Axboe401a18e2011-03-25 16:57:52 +01003193 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA))
3194 __elv_add_request(q, rq, ELEVATOR_INSERT_FLUSH);
3195 else
3196 __elv_add_request(q, rq, ELEVATOR_INSERT_SORT_MERGE);
Jens Axboe94b5eb22011-04-12 10:12:19 +02003197
3198 depth++;
Jens Axboe73c10102011-03-08 13:19:51 +01003199 }
3200
Jens Axboe99e22592011-04-18 09:59:55 +02003201 /*
3202 * This drops the queue lock
3203 */
3204 if (q)
Jens Axboe49cac012011-04-16 13:51:05 +02003205 queue_unplugged(q, depth, from_schedule);
Jens Axboe73c10102011-03-08 13:19:51 +01003206
Jens Axboe73c10102011-03-08 13:19:51 +01003207 local_irq_restore(flags);
3208}
Jens Axboe73c10102011-03-08 13:19:51 +01003209
3210void blk_finish_plug(struct blk_plug *plug)
3211{
Jens Axboef6603782011-04-15 15:49:07 +02003212 blk_flush_plug_list(plug, false);
Christoph Hellwig88b996c2011-04-15 15:20:10 +02003213
3214 if (plug == current->plug)
3215 current->plug = NULL;
Jens Axboe73c10102011-03-08 13:19:51 +01003216}
3217EXPORT_SYMBOL(blk_finish_plug);
3218
Rafael J. Wysocki47fafbc2014-12-04 01:00:23 +01003219#ifdef CONFIG_PM
Lin Ming6c954662013-03-23 11:42:26 +08003220/**
3221 * blk_pm_runtime_init - Block layer runtime PM initialization routine
3222 * @q: the queue of the device
3223 * @dev: the device the queue belongs to
3224 *
3225 * Description:
3226 * Initialize runtime-PM-related fields for @q and start auto suspend for
3227 * @dev. Drivers that want to take advantage of request-based runtime PM
3228 * should call this function after @dev has been initialized, and its
3229 * request queue @q has been allocated, and runtime PM for it can not happen
3230 * yet(either due to disabled/forbidden or its usage_count > 0). In most
3231 * cases, driver should call this function before any I/O has taken place.
3232 *
3233 * This function takes care of setting up using auto suspend for the device,
3234 * the autosuspend delay is set to -1 to make runtime suspend impossible
3235 * until an updated value is either set by user or by driver. Drivers do
3236 * not need to touch other autosuspend settings.
3237 *
3238 * The block layer runtime PM is request based, so only works for drivers
3239 * that use request as their IO unit instead of those directly use bio's.
3240 */
3241void blk_pm_runtime_init(struct request_queue *q, struct device *dev)
3242{
3243 q->dev = dev;
3244 q->rpm_status = RPM_ACTIVE;
3245 pm_runtime_set_autosuspend_delay(q->dev, -1);
3246 pm_runtime_use_autosuspend(q->dev);
3247}
3248EXPORT_SYMBOL(blk_pm_runtime_init);
3249
3250/**
3251 * blk_pre_runtime_suspend - Pre runtime suspend check
3252 * @q: the queue of the device
3253 *
3254 * Description:
3255 * This function will check if runtime suspend is allowed for the device
3256 * by examining if there are any requests pending in the queue. If there
3257 * are requests pending, the device can not be runtime suspended; otherwise,
3258 * the queue's status will be updated to SUSPENDING and the driver can
3259 * proceed to suspend the device.
3260 *
3261 * For the not allowed case, we mark last busy for the device so that
3262 * runtime PM core will try to autosuspend it some time later.
3263 *
3264 * This function should be called near the start of the device's
3265 * runtime_suspend callback.
3266 *
3267 * Return:
3268 * 0 - OK to runtime suspend the device
3269 * -EBUSY - Device should not be runtime suspended
3270 */
3271int blk_pre_runtime_suspend(struct request_queue *q)
3272{
3273 int ret = 0;
3274
3275 spin_lock_irq(q->queue_lock);
3276 if (q->nr_pending) {
3277 ret = -EBUSY;
3278 pm_runtime_mark_last_busy(q->dev);
3279 } else {
3280 q->rpm_status = RPM_SUSPENDING;
3281 }
3282 spin_unlock_irq(q->queue_lock);
3283 return ret;
3284}
3285EXPORT_SYMBOL(blk_pre_runtime_suspend);
3286
3287/**
3288 * blk_post_runtime_suspend - Post runtime suspend processing
3289 * @q: the queue of the device
3290 * @err: return value of the device's runtime_suspend function
3291 *
3292 * Description:
3293 * Update the queue's runtime status according to the return value of the
3294 * device's runtime suspend function and mark last busy for the device so
3295 * that PM core will try to auto suspend the device at a later time.
3296 *
3297 * This function should be called near the end of the device's
3298 * runtime_suspend callback.
3299 */
3300void blk_post_runtime_suspend(struct request_queue *q, int err)
3301{
3302 spin_lock_irq(q->queue_lock);
3303 if (!err) {
3304 q->rpm_status = RPM_SUSPENDED;
3305 } else {
3306 q->rpm_status = RPM_ACTIVE;
3307 pm_runtime_mark_last_busy(q->dev);
3308 }
3309 spin_unlock_irq(q->queue_lock);
3310}
3311EXPORT_SYMBOL(blk_post_runtime_suspend);
3312
3313/**
3314 * blk_pre_runtime_resume - Pre runtime resume processing
3315 * @q: the queue of the device
3316 *
3317 * Description:
3318 * Update the queue's runtime status to RESUMING in preparation for the
3319 * runtime resume of the device.
3320 *
3321 * This function should be called near the start of the device's
3322 * runtime_resume callback.
3323 */
3324void blk_pre_runtime_resume(struct request_queue *q)
3325{
3326 spin_lock_irq(q->queue_lock);
3327 q->rpm_status = RPM_RESUMING;
3328 spin_unlock_irq(q->queue_lock);
3329}
3330EXPORT_SYMBOL(blk_pre_runtime_resume);
3331
3332/**
3333 * blk_post_runtime_resume - Post runtime resume processing
3334 * @q: the queue of the device
3335 * @err: return value of the device's runtime_resume function
3336 *
3337 * Description:
3338 * Update the queue's runtime status according to the return value of the
3339 * device's runtime_resume function. If it is successfully resumed, process
3340 * the requests that are queued into the device's queue when it is resuming
3341 * and then mark last busy and initiate autosuspend for it.
3342 *
3343 * This function should be called near the end of the device's
3344 * runtime_resume callback.
3345 */
3346void blk_post_runtime_resume(struct request_queue *q, int err)
3347{
3348 spin_lock_irq(q->queue_lock);
3349 if (!err) {
3350 q->rpm_status = RPM_ACTIVE;
3351 __blk_run_queue(q);
3352 pm_runtime_mark_last_busy(q->dev);
Aaron Luc60855c2013-05-17 15:47:20 +08003353 pm_request_autosuspend(q->dev);
Lin Ming6c954662013-03-23 11:42:26 +08003354 } else {
3355 q->rpm_status = RPM_SUSPENDED;
3356 }
3357 spin_unlock_irq(q->queue_lock);
3358}
3359EXPORT_SYMBOL(blk_post_runtime_resume);
3360#endif
3361
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362int __init blk_dev_init(void)
3363{
Nikanth Karthikesan9eb55b02009-04-27 14:53:54 +02003364 BUILD_BUG_ON(__REQ_NR_BITS > 8 *
3365 sizeof(((struct request *)0)->cmd_flags));
3366
Tejun Heo89b90be2011-01-03 15:01:47 +01003367 /* used for unplugging and affects IO latency/throughput - HIGHPRI */
3368 kblockd_workqueue = alloc_workqueue("kblockd",
Matias Bjørling28747fc2014-06-11 23:43:54 +02003369 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 if (!kblockd_workqueue)
3371 panic("Failed to create kblockd\n");
3372
3373 request_cachep = kmem_cache_create("blkdev_requests",
Paul Mundt20c2df82007-07-20 10:11:58 +09003374 sizeof(struct request), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375
Jens Axboe8324aa92008-01-29 14:51:59 +01003376 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02003377 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 return 0;
3380}