blob: ef23788942ba07267b564eba19afeb184ddb1d23 [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
Mohan Srinivasana2766062016-08-25 18:25:05 -070043#include <linux/math64.h>
44
Mike Snitzerd07335e2010-11-16 12:52:38 +010045EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_remap);
Jun'ichi Nomurab0da3f0d2009-10-01 21:16:13 +020046EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_remap);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -070047EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_complete);
Keith Busch3291fa52014-04-28 12:30:52 -060048EXPORT_TRACEPOINT_SYMBOL_GPL(block_split);
NeilBrowncbae8d42012-12-14 20:49:27 +010049EXPORT_TRACEPOINT_SYMBOL_GPL(block_unplug);
Ingo Molnar0bfc2452008-11-26 11:59:56 +010050
Tejun Heoa73f7302011-12-14 00:33:37 +010051DEFINE_IDA(blk_queue_ida);
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/*
54 * For the allocated request tables
55 */
Jens Axboe320ae512013-10-24 09:20:05 +010056struct kmem_cache *request_cachep = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58/*
59 * For queue allocation
60 */
Jens Axboe6728cb02008-01-31 13:03:55 +010061struct kmem_cache *blk_requestq_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 * Controlling structure to kblockd
65 */
Jens Axboeff856ba2006-01-09 16:02:34 +010066static struct workqueue_struct *kblockd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Tejun Heoc18a2202015-05-22 17:13:42 -040068static void blk_clear_congested(struct request_list *rl, int sync)
69{
Tejun Heoc18a2202015-05-22 17:13:42 -040070#ifdef CONFIG_CGROUP_WRITEBACK
71 clear_wb_congested(rl->blkg->wb_congested, sync);
72#else
Tejun Heoe5e48942015-05-22 17:13:43 -040073 /*
74 * If !CGROUP_WRITEBACK, all blkg's map to bdi->wb and we shouldn't
75 * flip its congestion state for events on other blkcgs.
76 */
77 if (rl == &rl->q->root_rl)
78 clear_wb_congested(rl->q->backing_dev_info.wb.congested, sync);
Tejun Heoc18a2202015-05-22 17:13:42 -040079#endif
80}
81
82static void blk_set_congested(struct request_list *rl, int sync)
83{
Tejun Heoc18a2202015-05-22 17:13:42 -040084#ifdef CONFIG_CGROUP_WRITEBACK
85 set_wb_congested(rl->blkg->wb_congested, sync);
86#else
Tejun Heoe5e48942015-05-22 17:13:43 -040087 /* see blk_clear_congested() */
88 if (rl == &rl->q->root_rl)
89 set_wb_congested(rl->q->backing_dev_info.wb.congested, sync);
Tejun Heoc18a2202015-05-22 17:13:42 -040090#endif
91}
92
Jens Axboe8324aa92008-01-29 14:51:59 +010093void blk_queue_congestion_threshold(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
95 int nr;
96
97 nr = q->nr_requests - (q->nr_requests / 8) + 1;
98 if (nr > q->nr_requests)
99 nr = q->nr_requests;
100 q->nr_congestion_on = nr;
101
102 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
103 if (nr < 1)
104 nr = 1;
105 q->nr_congestion_off = nr;
106}
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108/**
109 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
110 * @bdev: device
111 *
112 * Locates the passed device's request queue and returns the address of its
Tejun Heoff9ea322014-09-08 08:03:56 +0900113 * backing_dev_info. This function can only be called if @bdev is opened
114 * and the return value is never NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 */
116struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
117{
Jens Axboe165125e2007-07-24 09:28:11 +0200118 struct request_queue *q = bdev_get_queue(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Tejun Heoff9ea322014-09-08 08:03:56 +0900120 return &q->backing_dev_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122EXPORT_SYMBOL(blk_get_backing_dev_info);
123
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200124void blk_rq_init(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200126 memset(rq, 0, sizeof(*rq));
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 INIT_LIST_HEAD(&rq->queuelist);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700129 INIT_LIST_HEAD(&rq->timeout_list);
Jens Axboec7c22e42008-09-13 20:26:01 +0200130 rq->cpu = -1;
Jens Axboe63a71382008-02-08 12:41:03 +0100131 rq->q = q;
Tejun Heoa2dec7b2009-05-07 22:24:44 +0900132 rq->__sector = (sector_t) -1;
Jens Axboe2e662b62006-07-13 11:55:04 +0200133 INIT_HLIST_NODE(&rq->hash);
134 RB_CLEAR_NODE(&rq->rb_node);
FUJITA Tomonorid7e3c322008-04-29 09:54:39 +0200135 rq->cmd = rq->__cmd;
Li Zefane2494e12009-04-02 13:43:26 +0800136 rq->cmd_len = BLK_MAX_CDB;
Jens Axboe63a71382008-02-08 12:41:03 +0100137 rq->tag = -1;
Tejun Heob243ddc2009-04-23 11:05:18 +0900138 rq->start_time = jiffies;
Divyesh Shah91952912010-04-01 15:01:41 -0700139 set_start_time_ns(rq);
Jerome Marchand09e099d2011-01-05 16:57:38 +0100140 rq->part = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200142EXPORT_SYMBOL(blk_rq_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
NeilBrown5bb23a62007-09-27 12:46:13 +0200144static void req_bio_endio(struct request *rq, struct bio *bio,
145 unsigned int nbytes, int error)
Tejun Heo797e7db2006-01-06 09:51:03 +0100146{
Tejun Heo143a87f2011-01-25 12:43:52 +0100147 if (error)
148 clear_bit(BIO_UPTODATE, &bio->bi_flags);
149 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
150 error = -EIO;
Tejun Heo797e7db2006-01-06 09:51:03 +0100151
Tejun Heo143a87f2011-01-25 12:43:52 +0100152 if (unlikely(rq->cmd_flags & REQ_QUIET))
153 set_bit(BIO_QUIET, &bio->bi_flags);
154
Kent Overstreetf79ea412012-09-20 16:38:30 -0700155 bio_advance(bio, nbytes);
Tejun Heo143a87f2011-01-25 12:43:52 +0100156
157 /* don't actually finish bio if it's part of flush sequence */
Kent Overstreet4f024f32013-10-11 15:44:27 -0700158 if (bio->bi_iter.bi_size == 0 && !(rq->cmd_flags & REQ_FLUSH_SEQ))
Tejun Heo143a87f2011-01-25 12:43:52 +0100159 bio_endio(bio, error);
Tejun Heo797e7db2006-01-06 09:51:03 +0100160}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162void blk_dump_rq_flags(struct request *rq, char *msg)
163{
164 int bit;
165
Jens Axboe59533162013-05-23 12:25:08 +0200166 printk(KERN_INFO "%s: dev %s: type=%x, flags=%llx\n", msg,
Jens Axboe4aff5e22006-08-10 08:44:47 +0200167 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
Jens Axboe59533162013-05-23 12:25:08 +0200168 (unsigned long long) rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Tejun Heo83096eb2009-05-07 22:24:39 +0900170 printk(KERN_INFO " sector %llu, nr/cnr %u/%u\n",
171 (unsigned long long)blk_rq_pos(rq),
172 blk_rq_sectors(rq), blk_rq_cur_sectors(rq));
Jens Axboeb4f42e22014-04-10 09:46:28 -0600173 printk(KERN_INFO " bio %p, biotail %p, len %u\n",
174 rq->bio, rq->biotail, blk_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200176 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
Jens Axboe6728cb02008-01-31 13:03:55 +0100177 printk(KERN_INFO " cdb: ");
FUJITA Tomonorid34c87e2008-04-29 14:37:52 +0200178 for (bit = 0; bit < BLK_MAX_CDB; bit++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 printk("%02x ", rq->cmd[bit]);
180 printk("\n");
181 }
182}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183EXPORT_SYMBOL(blk_dump_rq_flags);
184
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500185static void blk_delay_work(struct work_struct *work)
Jens Axboe6c5e0c42008-08-01 20:31:32 +0200186{
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500187 struct request_queue *q;
Jens Axboe6c5e0c42008-08-01 20:31:32 +0200188
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500189 q = container_of(work, struct request_queue, delay_work.work);
190 spin_lock_irq(q->queue_lock);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200191 __blk_run_queue(q);
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500192 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195/**
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500196 * blk_delay_queue - restart queueing after defined interval
197 * @q: The &struct request_queue in question
198 * @msecs: Delay in msecs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 *
200 * Description:
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500201 * Sometimes queueing needs to be postponed for a little while, to allow
202 * resources to come back. This function will make sure that queueing is
Bart Van Assche70460572012-11-28 13:45:56 +0100203 * restarted around the specified time. Queue lock must be held.
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500204 */
205void blk_delay_queue(struct request_queue *q, unsigned long msecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Bart Van Assche70460572012-11-28 13:45:56 +0100207 if (likely(!blk_queue_dead(q)))
208 queue_delayed_work(kblockd_workqueue, &q->delay_work,
209 msecs_to_jiffies(msecs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500211EXPORT_SYMBOL(blk_delay_queue);
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213/**
214 * blk_start_queue - restart a previously stopped queue
Jens Axboe165125e2007-07-24 09:28:11 +0200215 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 *
217 * Description:
218 * blk_start_queue() will clear the stop flag on the queue, and call
219 * the request_fn for the queue if it was in a stopped state when
220 * entered. Also see blk_stop_queue(). Queue lock must be held.
221 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200222void blk_start_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200224 WARN_ON(!irqs_disabled());
225
Nick Piggin75ad23b2008-04-29 14:48:33 +0200226 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200227 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229EXPORT_SYMBOL(blk_start_queue);
230
231/**
232 * blk_stop_queue - stop a queue
Jens Axboe165125e2007-07-24 09:28:11 +0200233 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 *
235 * Description:
236 * The Linux block layer assumes that a block driver will consume all
237 * entries on the request queue when the request_fn strategy is called.
238 * Often this will not happen, because of hardware limitations (queue
239 * depth settings). If a device driver gets a 'queue full' response,
240 * or if it simply chooses not to queue more I/O at one point, it can
241 * call this function to prevent the request_fn from being called until
242 * the driver has signalled it's ready to go again. This happens by calling
243 * blk_start_queue() to restart queue operations. Queue lock must be held.
244 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200245void blk_stop_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
Tejun Heo136b5722012-08-21 13:18:24 -0700247 cancel_delayed_work(&q->delay_work);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200248 queue_flag_set(QUEUE_FLAG_STOPPED, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250EXPORT_SYMBOL(blk_stop_queue);
251
252/**
253 * blk_sync_queue - cancel any pending callbacks on a queue
254 * @q: the queue
255 *
256 * Description:
257 * The block layer may perform asynchronous callback activity
258 * on a queue, such as calling the unplug function after a timeout.
259 * A block device may call blk_sync_queue to ensure that any
260 * such activity is cancelled, thus allowing it to release resources
Michael Opdenacker59c51592007-05-09 08:57:56 +0200261 * that the callbacks might use. The caller must already have made sure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 * that its ->make_request_fn will not re-add plugging prior to calling
263 * this function.
264 *
Vivek Goyalda527772011-03-02 19:05:33 -0500265 * This function does not cancel any asynchronous activity arising
Masanari Iidada3dae52014-09-09 01:27:23 +0900266 * out of elevator or throttling code. That would require elevator_exit()
Tejun Heo5efd6112012-03-05 13:15:12 -0800267 * and blkcg_exit_queue() to be called with queue lock initialized.
Vivek Goyalda527772011-03-02 19:05:33 -0500268 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 */
270void blk_sync_queue(struct request_queue *q)
271{
Jens Axboe70ed28b2008-11-19 14:38:39 +0100272 del_timer_sync(&q->timeout);
Ming Leif04c1fe2013-12-26 21:31:36 +0800273
274 if (q->mq_ops) {
275 struct blk_mq_hw_ctx *hctx;
276 int i;
277
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600278 queue_for_each_hw_ctx(q, hctx, i) {
279 cancel_delayed_work_sync(&hctx->run_work);
280 cancel_delayed_work_sync(&hctx->delay_work);
281 }
Ming Leif04c1fe2013-12-26 21:31:36 +0800282 } else {
283 cancel_delayed_work_sync(&q->delay_work);
284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286EXPORT_SYMBOL(blk_sync_queue);
287
288/**
Bart Van Asschec246e802012-12-06 14:32:01 +0100289 * __blk_run_queue_uncond - run a queue whether or not it has been stopped
290 * @q: The queue to run
291 *
292 * Description:
293 * Invoke request handling on a queue if there are any pending requests.
294 * May be used to restart request handling after a request has completed.
295 * This variant runs the queue whether or not the queue has been
296 * stopped. Must be called with the queue lock held and interrupts
297 * disabled. See also @blk_run_queue.
298 */
299inline void __blk_run_queue_uncond(struct request_queue *q)
300{
301 if (unlikely(blk_queue_dead(q)))
302 return;
303
Bart Van Assche24faf6f2012-11-28 13:46:45 +0100304 /*
305 * Some request_fn implementations, e.g. scsi_request_fn(), unlock
306 * the queue lock internally. As a result multiple threads may be
307 * running such a request function concurrently. Keep track of the
308 * number of active request_fn invocations such that blk_drain_queue()
309 * can wait until all these request_fn calls have finished.
310 */
311 q->request_fn_active++;
Bart Van Asschec246e802012-12-06 14:32:01 +0100312 q->request_fn(q);
Bart Van Assche24faf6f2012-11-28 13:46:45 +0100313 q->request_fn_active--;
Bart Van Asschec246e802012-12-06 14:32:01 +0100314}
315
316/**
Jens Axboe80a4b582008-10-14 09:51:06 +0200317 * __blk_run_queue - run a single device queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200319 *
320 * Description:
321 * See @blk_run_queue. This variant must be called with the queue lock
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200322 * held and interrupts disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 */
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200324void __blk_run_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Tejun Heoa538cd02009-04-23 11:05:17 +0900326 if (unlikely(blk_queue_stopped(q)))
327 return;
328
Bart Van Asschec246e802012-12-06 14:32:01 +0100329 __blk_run_queue_uncond(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200330}
331EXPORT_SYMBOL(__blk_run_queue);
Jens Axboedac07ec2006-05-11 08:20:16 +0200332
Nick Piggin75ad23b2008-04-29 14:48:33 +0200333/**
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200334 * blk_run_queue_async - run a single device queue in workqueue context
335 * @q: The queue to run
336 *
337 * Description:
338 * Tells kblockd to perform the equivalent of @blk_run_queue on behalf
Bart Van Assche70460572012-11-28 13:45:56 +0100339 * of us. The caller must hold the queue lock.
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200340 */
341void blk_run_queue_async(struct request_queue *q)
342{
Bart Van Assche70460572012-11-28 13:45:56 +0100343 if (likely(!blk_queue_stopped(q) && !blk_queue_dead(q)))
Tejun Heoe7c2f962012-08-21 13:18:24 -0700344 mod_delayed_work(kblockd_workqueue, &q->delay_work, 0);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200345}
Jens Axboec21e6be2011-04-19 13:32:46 +0200346EXPORT_SYMBOL(blk_run_queue_async);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200347
348/**
Nick Piggin75ad23b2008-04-29 14:48:33 +0200349 * blk_run_queue - run a single device queue
350 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200351 *
352 * Description:
353 * Invoke request handling on this queue, if it has pending work to do.
Tejun Heoa7f55792009-04-23 11:05:17 +0900354 * May be used to restart queueing when a request has completed.
Nick Piggin75ad23b2008-04-29 14:48:33 +0200355 */
356void blk_run_queue(struct request_queue *q)
357{
358 unsigned long flags;
359
360 spin_lock_irqsave(q->queue_lock, flags);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200361 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 spin_unlock_irqrestore(q->queue_lock, flags);
363}
364EXPORT_SYMBOL(blk_run_queue);
365
Jens Axboe165125e2007-07-24 09:28:11 +0200366void blk_put_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500367{
368 kobject_put(&q->kobj);
369}
Jens Axboed86e0e82011-05-27 07:44:43 +0200370EXPORT_SYMBOL(blk_put_queue);
Al Viro483f4af2006-03-18 18:34:37 -0500371
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200372/**
Bart Van Assche807592a2012-11-28 13:43:38 +0100373 * __blk_drain_queue - drain requests from request_queue
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200374 * @q: queue to drain
Tejun Heoc9a929d2011-10-19 14:42:16 +0200375 * @drain_all: whether to drain all requests or only the ones w/ ELVPRIV
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200376 *
Tejun Heoc9a929d2011-10-19 14:42:16 +0200377 * Drain requests from @q. If @drain_all is set, all requests are drained.
378 * If not, only ELVPRIV requests are drained. The caller is responsible
379 * for ensuring that no new requests which need to be drained are queued.
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200380 */
Bart Van Assche807592a2012-11-28 13:43:38 +0100381static void __blk_drain_queue(struct request_queue *q, bool drain_all)
382 __releases(q->queue_lock)
383 __acquires(q->queue_lock)
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200384{
Asias He458f27a2012-06-15 08:45:25 +0200385 int i;
386
Bart Van Assche807592a2012-11-28 13:43:38 +0100387 lockdep_assert_held(q->queue_lock);
388
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200389 while (true) {
Tejun Heo481a7d62011-12-14 00:33:37 +0100390 bool drain = false;
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200391
Tejun Heob855b042012-03-06 21:24:55 +0100392 /*
393 * The caller might be trying to drain @q before its
394 * elevator is initialized.
395 */
396 if (q->elevator)
397 elv_drain_elevator(q);
398
Tejun Heo5efd6112012-03-05 13:15:12 -0800399 blkcg_drain_queue(q);
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200400
Tejun Heo4eabc942011-12-15 20:03:04 +0100401 /*
402 * This function might be called on a queue which failed
Tejun Heob855b042012-03-06 21:24:55 +0100403 * driver init after queue creation or is not yet fully
404 * active yet. Some drivers (e.g. fd and loop) get unhappy
405 * in such cases. Kick queue iff dispatch queue has
406 * something on it and @q has request_fn set.
Tejun Heo4eabc942011-12-15 20:03:04 +0100407 */
Tejun Heob855b042012-03-06 21:24:55 +0100408 if (!list_empty(&q->queue_head) && q->request_fn)
Tejun Heo4eabc942011-12-15 20:03:04 +0100409 __blk_run_queue(q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200410
Tejun Heo8a5ecdd2012-06-04 20:40:58 -0700411 drain |= q->nr_rqs_elvpriv;
Bart Van Assche24faf6f2012-11-28 13:46:45 +0100412 drain |= q->request_fn_active;
Tejun Heo481a7d62011-12-14 00:33:37 +0100413
414 /*
415 * Unfortunately, requests are queued at and tracked from
416 * multiple places and there's no single counter which can
417 * be drained. Check all the queues and counters.
418 */
419 if (drain_all) {
Ming Leie97c2932014-09-25 23:23:46 +0800420 struct blk_flush_queue *fq = blk_get_flush_queue(q, NULL);
Tejun Heo481a7d62011-12-14 00:33:37 +0100421 drain |= !list_empty(&q->queue_head);
422 for (i = 0; i < 2; i++) {
Tejun Heo8a5ecdd2012-06-04 20:40:58 -0700423 drain |= q->nr_rqs[i];
Tejun Heo481a7d62011-12-14 00:33:37 +0100424 drain |= q->in_flight[i];
Ming Lei7c94e1c2014-09-25 23:23:43 +0800425 if (fq)
426 drain |= !list_empty(&fq->flush_queue[i]);
Tejun Heo481a7d62011-12-14 00:33:37 +0100427 }
428 }
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200429
Tejun Heo481a7d62011-12-14 00:33:37 +0100430 if (!drain)
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200431 break;
Bart Van Assche807592a2012-11-28 13:43:38 +0100432
433 spin_unlock_irq(q->queue_lock);
434
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200435 msleep(10);
Bart Van Assche807592a2012-11-28 13:43:38 +0100436
437 spin_lock_irq(q->queue_lock);
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200438 }
Asias He458f27a2012-06-15 08:45:25 +0200439
440 /*
441 * With queue marked dead, any woken up waiter will fail the
442 * allocation path, so the wakeup chaining is lost and we're
443 * left with hung waiters. We need to wake up those waiters.
444 */
445 if (q->request_fn) {
Tejun Heoa0516612012-06-26 15:05:44 -0700446 struct request_list *rl;
447
Tejun Heoa0516612012-06-26 15:05:44 -0700448 blk_queue_for_each_rl(rl, q)
449 for (i = 0; i < ARRAY_SIZE(rl->wait); i++)
450 wake_up_all(&rl->wait[i]);
Asias He458f27a2012-06-15 08:45:25 +0200451 }
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200452}
453
Tejun Heoc9a929d2011-10-19 14:42:16 +0200454/**
Tejun Heod7325802012-03-05 13:14:58 -0800455 * blk_queue_bypass_start - enter queue bypass mode
456 * @q: queue of interest
457 *
458 * In bypass mode, only the dispatch FIFO queue of @q is used. This
459 * function makes @q enter bypass mode and drains all requests which were
Tejun Heo6ecf23a2012-03-05 13:14:59 -0800460 * throttled or issued before. On return, it's guaranteed that no request
Tejun Heo80fd9972012-04-13 14:50:53 -0700461 * is being throttled or has ELVPRIV set and blk_queue_bypass() %true
462 * inside queue or RCU read lock.
Tejun Heod7325802012-03-05 13:14:58 -0800463 */
464void blk_queue_bypass_start(struct request_queue *q)
465{
466 spin_lock_irq(q->queue_lock);
Tejun Heo776687b2014-07-01 10:29:17 -0600467 q->bypass_depth++;
Tejun Heod7325802012-03-05 13:14:58 -0800468 queue_flag_set(QUEUE_FLAG_BYPASS, q);
469 spin_unlock_irq(q->queue_lock);
470
Tejun Heo776687b2014-07-01 10:29:17 -0600471 /*
472 * Queues start drained. Skip actual draining till init is
473 * complete. This avoids lenghty delays during queue init which
474 * can happen many times during boot.
475 */
476 if (blk_queue_init_done(q)) {
Bart Van Assche807592a2012-11-28 13:43:38 +0100477 spin_lock_irq(q->queue_lock);
478 __blk_drain_queue(q, false);
479 spin_unlock_irq(q->queue_lock);
480
Tejun Heob82d4b12012-04-13 13:11:31 -0700481 /* ensure blk_queue_bypass() is %true inside RCU read lock */
482 synchronize_rcu();
483 }
Tejun Heod7325802012-03-05 13:14:58 -0800484}
485EXPORT_SYMBOL_GPL(blk_queue_bypass_start);
486
487/**
488 * blk_queue_bypass_end - leave queue bypass mode
489 * @q: queue of interest
490 *
491 * Leave bypass mode and restore the normal queueing behavior.
492 */
493void blk_queue_bypass_end(struct request_queue *q)
494{
495 spin_lock_irq(q->queue_lock);
496 if (!--q->bypass_depth)
497 queue_flag_clear(QUEUE_FLAG_BYPASS, q);
498 WARN_ON_ONCE(q->bypass_depth < 0);
499 spin_unlock_irq(q->queue_lock);
500}
501EXPORT_SYMBOL_GPL(blk_queue_bypass_end);
502
Jens Axboeaed3ea92014-12-22 14:04:42 -0700503void blk_set_queue_dying(struct request_queue *q)
504{
Bart Van Assche26390c72016-08-16 16:48:36 -0700505 spin_lock_irq(q->queue_lock);
506 queue_flag_set(QUEUE_FLAG_DYING, q);
507 spin_unlock_irq(q->queue_lock);
Jens Axboeaed3ea92014-12-22 14:04:42 -0700508
509 if (q->mq_ops)
510 blk_mq_wake_waiters(q);
511 else {
512 struct request_list *rl;
513
514 blk_queue_for_each_rl(rl, q) {
515 if (rl->rq_pool) {
516 wake_up(&rl->wait[BLK_RW_SYNC]);
517 wake_up(&rl->wait[BLK_RW_ASYNC]);
518 }
519 }
520 }
521}
522EXPORT_SYMBOL_GPL(blk_set_queue_dying);
523
Tejun Heod7325802012-03-05 13:14:58 -0800524/**
Tejun Heoc9a929d2011-10-19 14:42:16 +0200525 * blk_cleanup_queue - shutdown a request queue
526 * @q: request queue to shutdown
527 *
Bart Van Asschec246e802012-12-06 14:32:01 +0100528 * Mark @q DYING, drain all pending requests, mark @q DEAD, destroy and
529 * put it. All future requests will be failed immediately with -ENODEV.
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500530 */
Jens Axboe6728cb02008-01-31 13:03:55 +0100531void blk_cleanup_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500532{
Tejun Heoc9a929d2011-10-19 14:42:16 +0200533 spinlock_t *lock = q->queue_lock;
Jens Axboee3335de2008-09-18 09:22:54 -0700534
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100535 /* mark @q DYING, no new request or merges will be allowed afterwards */
Al Viro483f4af2006-03-18 18:34:37 -0500536 mutex_lock(&q->sysfs_lock);
Jens Axboeaed3ea92014-12-22 14:04:42 -0700537 blk_set_queue_dying(q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200538 spin_lock_irq(lock);
Tejun Heo6ecf23a2012-03-05 13:14:59 -0800539
Tejun Heo80fd9972012-04-13 14:50:53 -0700540 /*
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100541 * A dying queue is permanently in bypass mode till released. Note
Tejun Heo80fd9972012-04-13 14:50:53 -0700542 * that, unlike blk_queue_bypass_start(), we aren't performing
543 * synchronize_rcu() after entering bypass mode to avoid the delay
544 * as some drivers create and destroy a lot of queues while
545 * probing. This is still safe because blk_release_queue() will be
546 * called only after the queue refcnt drops to zero and nothing,
547 * RCU or not, would be traversing the queue by then.
548 */
Tejun Heo6ecf23a2012-03-05 13:14:59 -0800549 q->bypass_depth++;
550 queue_flag_set(QUEUE_FLAG_BYPASS, q);
551
Tejun Heoc9a929d2011-10-19 14:42:16 +0200552 queue_flag_set(QUEUE_FLAG_NOMERGES, q);
553 queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100554 queue_flag_set(QUEUE_FLAG_DYING, q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200555 spin_unlock_irq(lock);
556 mutex_unlock(&q->sysfs_lock);
557
Bart Van Asschec246e802012-12-06 14:32:01 +0100558 /*
559 * Drain all requests queued before DYING marking. Set DEAD flag to
560 * prevent that q->request_fn() gets invoked after draining finished.
561 */
Ming Lei43a5e4e2013-12-26 21:31:35 +0800562 if (q->mq_ops) {
Tejun Heo780db202014-07-01 10:31:13 -0600563 blk_mq_freeze_queue(q);
Ming Lei43a5e4e2013-12-26 21:31:35 +0800564 spin_lock_irq(lock);
565 } else {
566 spin_lock_irq(lock);
567 __blk_drain_queue(q, true);
568 }
Bart Van Asschec246e802012-12-06 14:32:01 +0100569 queue_flag_set(QUEUE_FLAG_DEAD, q);
Bart Van Assche807592a2012-11-28 13:43:38 +0100570 spin_unlock_irq(lock);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200571
572 /* @q won't process any more request, flush async actions */
573 del_timer_sync(&q->backing_dev_info.laptop_mode_wb_timer);
574 blk_sync_queue(q);
575
Bart Van Assche45a9c9d2014-12-09 16:57:48 +0100576 if (q->mq_ops)
577 blk_mq_free_queue(q);
578
Asias He5e5cfac2012-05-24 23:28:52 +0800579 spin_lock_irq(lock);
580 if (q->queue_lock != &q->__queue_lock)
581 q->queue_lock = &q->__queue_lock;
582 spin_unlock_irq(lock);
583
Tejun Heo9c580652015-09-08 12:20:22 -0400584 bdi_unregister(&q->backing_dev_info);
NeilBrown6cd18e72015-04-27 14:12:22 +1000585
Tejun Heoc9a929d2011-10-19 14:42:16 +0200586 /* @q is and will stay empty, shutdown and put */
Al Viro483f4af2006-03-18 18:34:37 -0500587 blk_put_queue(q);
588}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589EXPORT_SYMBOL(blk_cleanup_queue);
590
David Rientjes271508d2015-03-24 16:21:16 -0700591/* Allocate memory local to the request queue */
592static void *alloc_request_struct(gfp_t gfp_mask, void *data)
593{
594 int nid = (int)(long)data;
595 return kmem_cache_alloc_node(request_cachep, gfp_mask, nid);
596}
597
598static void free_request_struct(void *element, void *unused)
599{
600 kmem_cache_free(request_cachep, element);
601}
602
Tejun Heo5b788ce2012-06-04 20:40:59 -0700603int blk_init_rl(struct request_list *rl, struct request_queue *q,
604 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Mike Snitzer1abec4f2010-05-25 13:15:15 -0400606 if (unlikely(rl->rq_pool))
607 return 0;
608
Tejun Heo5b788ce2012-06-04 20:40:59 -0700609 rl->q = q;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200610 rl->count[BLK_RW_SYNC] = rl->count[BLK_RW_ASYNC] = 0;
611 rl->starved[BLK_RW_SYNC] = rl->starved[BLK_RW_ASYNC] = 0;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200612 init_waitqueue_head(&rl->wait[BLK_RW_SYNC]);
613 init_waitqueue_head(&rl->wait[BLK_RW_ASYNC]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
David Rientjes271508d2015-03-24 16:21:16 -0700615 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, alloc_request_struct,
616 free_request_struct,
617 (void *)(long)q->node, gfp_mask,
618 q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (!rl->rq_pool)
620 return -ENOMEM;
621
622 return 0;
623}
624
Tejun Heo5b788ce2012-06-04 20:40:59 -0700625void blk_exit_rl(struct request_list *rl)
626{
627 if (rl->rq_pool)
628 mempool_destroy(rl->rq_pool);
629}
630
Jens Axboe165125e2007-07-24 09:28:11 +0200631struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Ezequiel Garciac304a512012-11-10 10:39:44 +0100633 return blk_alloc_queue_node(gfp_mask, NUMA_NO_NODE);
Christoph Lameter19460892005-06-23 00:08:19 -0700634}
635EXPORT_SYMBOL(blk_alloc_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Jens Axboe165125e2007-07-24 09:28:11 +0200637struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -0700638{
Jens Axboe165125e2007-07-24 09:28:11 +0200639 struct request_queue *q;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700640 int err;
Christoph Lameter19460892005-06-23 00:08:19 -0700641
Jens Axboe8324aa92008-01-29 14:51:59 +0100642 q = kmem_cache_alloc_node(blk_requestq_cachep,
Christoph Lameter94f60302007-07-17 04:03:29 -0700643 gfp_mask | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (!q)
645 return NULL;
646
Dan Carpenter00380a42012-03-23 09:58:54 +0100647 q->id = ida_simple_get(&blk_queue_ida, 0, 0, gfp_mask);
Tejun Heoa73f7302011-12-14 00:33:37 +0100648 if (q->id < 0)
Ming Lei3d2936f2014-05-27 23:35:14 +0800649 goto fail_q;
Tejun Heoa73f7302011-12-14 00:33:37 +0100650
Jens Axboe0989a022009-06-12 14:42:56 +0200651 q->backing_dev_info.ra_pages =
652 (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
Tejun Heo0632c4c2015-05-22 17:13:36 -0400653 q->backing_dev_info.capabilities = BDI_CAP_CGROUP_WRITEBACK;
Jens Axboed9938312009-06-12 14:45:52 +0200654 q->backing_dev_info.name = "block";
Mike Snitzer51514122011-11-23 10:59:13 +0100655 q->node = node_id;
Jens Axboe0989a022009-06-12 14:42:56 +0200656
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700657 err = bdi_init(&q->backing_dev_info);
Tejun Heoa73f7302011-12-14 00:33:37 +0100658 if (err)
659 goto fail_id;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700660
Matthew Garrett31373d02010-04-06 14:25:14 +0200661 setup_timer(&q->backing_dev_info.laptop_mode_wb_timer,
662 laptop_mode_timer_fn, (unsigned long) q);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700663 setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
Tejun Heob855b042012-03-06 21:24:55 +0100664 INIT_LIST_HEAD(&q->queue_head);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700665 INIT_LIST_HEAD(&q->timeout_list);
Tejun Heoa612fdd2011-12-14 00:33:41 +0100666 INIT_LIST_HEAD(&q->icq_list);
Tejun Heo4eef3042012-03-05 13:15:18 -0800667#ifdef CONFIG_BLK_CGROUP
Tejun Heoe8989fa2012-03-05 13:15:20 -0800668 INIT_LIST_HEAD(&q->blkg_list);
Tejun Heo4eef3042012-03-05 13:15:18 -0800669#endif
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500670 INIT_DELAYED_WORK(&q->delay_work, blk_delay_work);
Al Viro483f4af2006-03-18 18:34:37 -0500671
Jens Axboe8324aa92008-01-29 14:51:59 +0100672 kobject_init(&q->kobj, &blk_queue_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Al Viro483f4af2006-03-18 18:34:37 -0500674 mutex_init(&q->sysfs_lock);
Neil Browne7e72bf2008-05-14 16:05:54 -0700675 spin_lock_init(&q->__queue_lock);
Al Viro483f4af2006-03-18 18:34:37 -0500676
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500677 /*
678 * By default initialize queue_lock to internal lock and driver can
679 * override it later if need be.
680 */
681 q->queue_lock = &q->__queue_lock;
682
Tejun Heob82d4b12012-04-13 13:11:31 -0700683 /*
684 * A queue starts its life with bypass turned on to avoid
685 * unnecessary bypass on/off overhead and nasty surprises during
Tejun Heo749fefe2012-09-20 14:08:52 -0700686 * init. The initial bypass will be finished when the queue is
687 * registered by blk_register_queue().
Tejun Heob82d4b12012-04-13 13:11:31 -0700688 */
689 q->bypass_depth = 1;
690 __set_bit(QUEUE_FLAG_BYPASS, &q->queue_flags);
691
Jens Axboe320ae512013-10-24 09:20:05 +0100692 init_waitqueue_head(&q->mq_freeze_wq);
693
Tejun Heo5efd6112012-03-05 13:15:12 -0800694 if (blkcg_init_queue(q))
Mikulas Patockafff49962013-10-14 12:11:36 -0400695 goto fail_bdi;
Tejun Heof51b8022012-03-05 13:15:05 -0800696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return q;
Tejun Heoa73f7302011-12-14 00:33:37 +0100698
Mikulas Patockafff49962013-10-14 12:11:36 -0400699fail_bdi:
700 bdi_destroy(&q->backing_dev_info);
Tejun Heoa73f7302011-12-14 00:33:37 +0100701fail_id:
702 ida_simple_remove(&blk_queue_ida, q->id);
703fail_q:
704 kmem_cache_free(blk_requestq_cachep, q);
705 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
Christoph Lameter19460892005-06-23 00:08:19 -0700707EXPORT_SYMBOL(blk_alloc_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709/**
710 * blk_init_queue - prepare a request queue for use with a block device
711 * @rfn: The function to be called to process requests that have been
712 * placed on the queue.
713 * @lock: Request queue spin lock
714 *
715 * Description:
716 * If a block device wishes to use the standard request handling procedures,
717 * which sorts requests and coalesces adjacent requests, then it must
718 * call blk_init_queue(). The function @rfn will be called when there
719 * are requests on the queue that need to be processed. If the device
720 * supports plugging, then @rfn may not be called immediately when requests
721 * are available on the queue, but may be called at some time later instead.
722 * Plugged queues are generally unplugged when a buffer belonging to one
723 * of the requests on the queue is needed, or due to memory pressure.
724 *
725 * @rfn is not required, or even expected, to remove all requests off the
726 * queue, but only as many as it can handle at a time. If it does leave
727 * requests on the queue, it is responsible for arranging that the requests
728 * get dealt with eventually.
729 *
730 * The queue spin lock must be held while manipulating the requests on the
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200731 * request queue; this lock will be taken also from interrupt context, so irq
732 * disabling is needed for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 *
Randy Dunlap710027a2008-08-19 20:13:11 +0200734 * Function returns a pointer to the initialized request queue, or %NULL if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 * it didn't succeed.
736 *
737 * Note:
738 * blk_init_queue() must be paired with a blk_cleanup_queue() call
739 * when the block device is deactivated (such as at module unload).
740 **/
Christoph Lameter19460892005-06-23 00:08:19 -0700741
Jens Axboe165125e2007-07-24 09:28:11 +0200742struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Ezequiel Garciac304a512012-11-10 10:39:44 +0100744 return blk_init_queue_node(rfn, lock, NUMA_NO_NODE);
Christoph Lameter19460892005-06-23 00:08:19 -0700745}
746EXPORT_SYMBOL(blk_init_queue);
747
Jens Axboe165125e2007-07-24 09:28:11 +0200748struct request_queue *
Christoph Lameter19460892005-06-23 00:08:19 -0700749blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
750{
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600751 struct request_queue *uninit_q, *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600753 uninit_q = blk_alloc_queue_node(GFP_KERNEL, node_id);
754 if (!uninit_q)
755 return NULL;
756
Mike Snitzer51514122011-11-23 10:59:13 +0100757 q = blk_init_allocated_queue(uninit_q, rfn, lock);
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600758 if (!q)
Mike Snitzer7982e902014-03-08 17:20:01 -0700759 blk_cleanup_queue(uninit_q);
Christoph Hellwig18741982014-02-10 09:29:00 -0700760
Mike Snitzer7982e902014-03-08 17:20:01 -0700761 return q;
Mike Snitzer01effb02010-05-11 08:57:42 +0200762}
763EXPORT_SYMBOL(blk_init_queue_node);
764
Mike Snitzer336b7e12015-05-11 14:06:32 -0400765static void blk_queue_bio(struct request_queue *q, struct bio *bio);
766
Mike Snitzer01effb02010-05-11 08:57:42 +0200767struct request_queue *
768blk_init_allocated_queue(struct request_queue *q, request_fn_proc *rfn,
769 spinlock_t *lock)
770{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (!q)
772 return NULL;
773
Ming Leif70ced02014-09-25 23:23:47 +0800774 q->fq = blk_alloc_flush_queue(q, NUMA_NO_NODE, 0);
Ming Leiba483382014-09-25 23:23:44 +0800775 if (!q->fq)
Mike Snitzer7982e902014-03-08 17:20:01 -0700776 return NULL;
777
Tejun Heoa0516612012-06-26 15:05:44 -0700778 if (blk_init_rl(&q->root_rl, q, GFP_KERNEL))
Dave Jones708f04d2014-03-20 15:03:58 -0600779 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 q->request_fn = rfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 q->prep_rq_fn = NULL;
James Bottomley28018c22010-07-01 19:49:17 +0900783 q->unprep_rq_fn = NULL;
Tejun Heo60ea8222012-09-20 14:09:30 -0700784 q->queue_flags |= QUEUE_FLAG_DEFAULT;
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500785
786 /* Override internal queue lock with supplied lock pointer */
787 if (lock)
788 q->queue_lock = lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Jens Axboef3b144a2009-03-06 08:48:33 +0100790 /*
791 * This also sets hw/phys segments, boundary and size
792 */
Jens Axboec20e8de2011-09-12 12:03:37 +0200793 blk_queue_make_request(q, blk_queue_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Alan Stern44ec9542007-02-20 11:01:57 -0500795 q->sg_reserved_size = INT_MAX;
796
Tomoki Sekiyamaeb1c1602013-10-15 16:42:16 -0600797 /* Protect q->elevator from elevator_change */
798 mutex_lock(&q->sysfs_lock);
799
Tejun Heob82d4b12012-04-13 13:11:31 -0700800 /* init elevator */
Tomoki Sekiyamaeb1c1602013-10-15 16:42:16 -0600801 if (elevator_init(q, NULL)) {
802 mutex_unlock(&q->sysfs_lock);
Dave Jones708f04d2014-03-20 15:03:58 -0600803 goto fail;
Tomoki Sekiyamaeb1c1602013-10-15 16:42:16 -0600804 }
805
806 mutex_unlock(&q->sysfs_lock);
807
Tejun Heob82d4b12012-04-13 13:11:31 -0700808 return q;
Dave Jones708f04d2014-03-20 15:03:58 -0600809
810fail:
Ming Leiba483382014-09-25 23:23:44 +0800811 blk_free_flush_queue(q->fq);
Dave Jones708f04d2014-03-20 15:03:58 -0600812 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
Mike Snitzer51514122011-11-23 10:59:13 +0100814EXPORT_SYMBOL(blk_init_allocated_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Tejun Heo09ac46c2011-12-14 00:33:38 +0100816bool blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100818 if (likely(!blk_queue_dying(q))) {
Tejun Heo09ac46c2011-12-14 00:33:38 +0100819 __blk_get_queue(q);
820 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
822
Tejun Heo09ac46c2011-12-14 00:33:38 +0100823 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
Jens Axboed86e0e82011-05-27 07:44:43 +0200825EXPORT_SYMBOL(blk_get_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Tejun Heo5b788ce2012-06-04 20:40:59 -0700827static inline void blk_free_request(struct request_list *rl, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
Tejun Heof1f8cc92011-12-14 00:33:42 +0100829 if (rq->cmd_flags & REQ_ELVPRIV) {
Tejun Heo5b788ce2012-06-04 20:40:59 -0700830 elv_put_request(rl->q, rq);
Tejun Heof1f8cc92011-12-14 00:33:42 +0100831 if (rq->elv.icq)
Tejun Heo11a31222012-02-07 07:51:30 +0100832 put_io_context(rq->elv.icq->ioc);
Tejun Heof1f8cc92011-12-14 00:33:42 +0100833 }
834
Tejun Heo5b788ce2012-06-04 20:40:59 -0700835 mempool_free(rq, rl->rq_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838/*
839 * ioc_batching returns true if the ioc is a valid batching request and
840 * should be given priority access to a request.
841 */
Jens Axboe165125e2007-07-24 09:28:11 +0200842static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
844 if (!ioc)
845 return 0;
846
847 /*
848 * Make sure the process is able to allocate at least 1 request
849 * even if the batch times out, otherwise we could theoretically
850 * lose wakeups.
851 */
852 return ioc->nr_batch_requests == q->nr_batching ||
853 (ioc->nr_batch_requests > 0
854 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
855}
856
857/*
858 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
859 * will cause the process to be a "batcher" on all queues in the system. This
860 * is the behaviour we want though - once it gets a wakeup it should be given
861 * a nice run.
862 */
Jens Axboe165125e2007-07-24 09:28:11 +0200863static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
865 if (!ioc || ioc_batching(q, ioc))
866 return;
867
868 ioc->nr_batch_requests = q->nr_batching;
869 ioc->last_waited = jiffies;
870}
871
Tejun Heo5b788ce2012-06-04 20:40:59 -0700872static void __freed_request(struct request_list *rl, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Tejun Heo5b788ce2012-06-04 20:40:59 -0700874 struct request_queue *q = rl->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Tejun Heoc18a2202015-05-22 17:13:42 -0400876 if (rl->count[sync] < queue_congestion_off_threshold(q))
877 blk_clear_congested(rl, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Jens Axboe1faa16d2009-04-06 14:48:01 +0200879 if (rl->count[sync] + 1 <= q->nr_requests) {
880 if (waitqueue_active(&rl->wait[sync]))
881 wake_up(&rl->wait[sync]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Tejun Heo5b788ce2012-06-04 20:40:59 -0700883 blk_clear_rl_full(rl, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
885}
886
887/*
888 * A request has just been released. Account for it, update the full and
889 * congestion status, wake up any waiters. Called under q->queue_lock.
890 */
Tejun Heo5b788ce2012-06-04 20:40:59 -0700891static void freed_request(struct request_list *rl, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Tejun Heo5b788ce2012-06-04 20:40:59 -0700893 struct request_queue *q = rl->q;
Tejun Heo75eb6c32011-10-19 14:31:22 +0200894 int sync = rw_is_sync(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Tejun Heo8a5ecdd2012-06-04 20:40:58 -0700896 q->nr_rqs[sync]--;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200897 rl->count[sync]--;
Tejun Heo75eb6c32011-10-19 14:31:22 +0200898 if (flags & REQ_ELVPRIV)
Tejun Heo8a5ecdd2012-06-04 20:40:58 -0700899 q->nr_rqs_elvpriv--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Tejun Heo5b788ce2012-06-04 20:40:59 -0700901 __freed_request(rl, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Jens Axboe1faa16d2009-04-06 14:48:01 +0200903 if (unlikely(rl->starved[sync ^ 1]))
Tejun Heo5b788ce2012-06-04 20:40:59 -0700904 __freed_request(rl, sync ^ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
906
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600907int blk_update_nr_requests(struct request_queue *q, unsigned int nr)
908{
909 struct request_list *rl;
Tejun Heoc18a2202015-05-22 17:13:42 -0400910 int on_thresh, off_thresh;
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600911
912 spin_lock_irq(q->queue_lock);
913 q->nr_requests = nr;
914 blk_queue_congestion_threshold(q);
Tejun Heoc18a2202015-05-22 17:13:42 -0400915 on_thresh = queue_congestion_on_threshold(q);
916 off_thresh = queue_congestion_off_threshold(q);
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600917
918 blk_queue_for_each_rl(rl, q) {
Tejun Heoc18a2202015-05-22 17:13:42 -0400919 if (rl->count[BLK_RW_SYNC] >= on_thresh)
920 blk_set_congested(rl, BLK_RW_SYNC);
921 else if (rl->count[BLK_RW_SYNC] < off_thresh)
922 blk_clear_congested(rl, BLK_RW_SYNC);
923
924 if (rl->count[BLK_RW_ASYNC] >= on_thresh)
925 blk_set_congested(rl, BLK_RW_ASYNC);
926 else if (rl->count[BLK_RW_ASYNC] < off_thresh)
927 blk_clear_congested(rl, BLK_RW_ASYNC);
928
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600929 if (rl->count[BLK_RW_SYNC] >= q->nr_requests) {
930 blk_set_rl_full(rl, BLK_RW_SYNC);
931 } else {
932 blk_clear_rl_full(rl, BLK_RW_SYNC);
933 wake_up(&rl->wait[BLK_RW_SYNC]);
934 }
935
936 if (rl->count[BLK_RW_ASYNC] >= q->nr_requests) {
937 blk_set_rl_full(rl, BLK_RW_ASYNC);
938 } else {
939 blk_clear_rl_full(rl, BLK_RW_ASYNC);
940 wake_up(&rl->wait[BLK_RW_ASYNC]);
941 }
942 }
943
944 spin_unlock_irq(q->queue_lock);
945 return 0;
946}
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948/*
Mike Snitzer9d5a4e92011-02-11 11:05:46 +0100949 * Determine if elevator data should be initialized when allocating the
950 * request associated with @bio.
951 */
952static bool blk_rq_should_init_elevator(struct bio *bio)
953{
954 if (!bio)
955 return true;
956
957 /*
958 * Flush requests do not use the elevator so skip initialization.
959 * This allows a request to share the flush and elevator data.
960 */
961 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA))
962 return false;
963
964 return true;
965}
966
Tejun Heoda8303c2011-10-19 14:33:05 +0200967/**
Tejun Heo852c7882012-03-05 13:15:27 -0800968 * rq_ioc - determine io_context for request allocation
969 * @bio: request being allocated is for this bio (can be %NULL)
970 *
971 * Determine io_context to use for request allocation for @bio. May return
972 * %NULL if %current->io_context doesn't exist.
973 */
974static struct io_context *rq_ioc(struct bio *bio)
975{
976#ifdef CONFIG_BLK_CGROUP
977 if (bio && bio->bi_ioc)
978 return bio->bi_ioc;
979#endif
980 return current->io_context;
981}
982
983/**
Tejun Heoa06e05e2012-06-04 20:40:55 -0700984 * __get_request - get a free request
Tejun Heo5b788ce2012-06-04 20:40:59 -0700985 * @rl: request list to allocate from
Tejun Heoda8303c2011-10-19 14:33:05 +0200986 * @rw_flags: RW and SYNC flags
987 * @bio: bio to allocate request for (can be %NULL)
988 * @gfp_mask: allocation mask
989 *
990 * Get a free request from @q. This function may fail under memory
991 * pressure or if @q is dead.
992 *
Masanari Iidada3dae52014-09-09 01:27:23 +0900993 * Must be called with @q->queue_lock held and,
Joe Lawrencea492f072014-08-28 08:15:21 -0600994 * Returns ERR_PTR on failure, with @q->queue_lock held.
995 * Returns request pointer on success, with @q->queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 */
Tejun Heo5b788ce2012-06-04 20:40:59 -0700997static struct request *__get_request(struct request_list *rl, int rw_flags,
Tejun Heoa06e05e2012-06-04 20:40:55 -0700998 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
Tejun Heo5b788ce2012-06-04 20:40:59 -07001000 struct request_queue *q = rl->q;
Tejun Heob6792812012-03-05 13:15:23 -08001001 struct request *rq;
Tejun Heo7f4b35d2012-06-04 20:40:56 -07001002 struct elevator_type *et = q->elevator->type;
1003 struct io_context *ioc = rq_ioc(bio);
Tejun Heof1f8cc92011-12-14 00:33:42 +01001004 struct io_cq *icq = NULL;
Jens Axboe1faa16d2009-04-06 14:48:01 +02001005 const bool is_sync = rw_is_sync(rw_flags) != 0;
Tejun Heo75eb6c32011-10-19 14:31:22 +02001006 int may_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Bart Van Assche3f3299d2012-11-28 13:42:38 +01001008 if (unlikely(blk_queue_dying(q)))
Joe Lawrencea492f072014-08-28 08:15:21 -06001009 return ERR_PTR(-ENODEV);
Tejun Heoda8303c2011-10-19 14:33:05 +02001010
Jens Axboe7749a8d2006-12-13 13:02:26 +01001011 may_queue = elv_may_queue(q, rw_flags);
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001012 if (may_queue == ELV_MQUEUE_NO)
1013 goto rq_starved;
1014
Jens Axboe1faa16d2009-04-06 14:48:01 +02001015 if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) {
1016 if (rl->count[is_sync]+1 >= q->nr_requests) {
Tejun Heof2dbd762011-12-14 00:33:40 +01001017 /*
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001018 * The queue will fill after this allocation, so set
1019 * it as full, and mark this process as "batching".
1020 * This process will be allowed to complete a batch of
1021 * requests, others will be blocked.
1022 */
Tejun Heo5b788ce2012-06-04 20:40:59 -07001023 if (!blk_rl_full(rl, is_sync)) {
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001024 ioc_set_batching(q, ioc);
Tejun Heo5b788ce2012-06-04 20:40:59 -07001025 blk_set_rl_full(rl, is_sync);
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001026 } else {
1027 if (may_queue != ELV_MQUEUE_MUST
1028 && !ioc_batching(q, ioc)) {
1029 /*
1030 * The queue is full and the allocating
1031 * process is not a "batcher", and not
1032 * exempted by the IO scheduler
1033 */
Joe Lawrencea492f072014-08-28 08:15:21 -06001034 return ERR_PTR(-ENOMEM);
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001035 }
1036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
Tejun Heoc18a2202015-05-22 17:13:42 -04001038 blk_set_congested(rl, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 }
1040
Jens Axboe082cf692005-06-28 16:35:11 +02001041 /*
1042 * Only allow batching queuers to allocate up to 50% over the defined
1043 * limit of requests, otherwise we could have thousands of requests
1044 * allocated with any setting of ->nr_requests
1045 */
Jens Axboe1faa16d2009-04-06 14:48:01 +02001046 if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
Joe Lawrencea492f072014-08-28 08:15:21 -06001047 return ERR_PTR(-ENOMEM);
Hugh Dickinsfd782a42005-06-29 15:15:40 +01001048
Tejun Heo8a5ecdd2012-06-04 20:40:58 -07001049 q->nr_rqs[is_sync]++;
Jens Axboe1faa16d2009-04-06 14:48:01 +02001050 rl->count[is_sync]++;
1051 rl->starved[is_sync] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +02001052
Tejun Heof1f8cc92011-12-14 00:33:42 +01001053 /*
1054 * Decide whether the new request will be managed by elevator. If
1055 * so, mark @rw_flags and increment elvpriv. Non-zero elvpriv will
1056 * prevent the current elevator from being destroyed until the new
1057 * request is freed. This guarantees icq's won't be destroyed and
1058 * makes creating new ones safe.
1059 *
1060 * Also, lookup icq while holding queue_lock. If it doesn't exist,
1061 * it will be created after releasing queue_lock.
1062 */
Tejun Heod7325802012-03-05 13:14:58 -08001063 if (blk_rq_should_init_elevator(bio) && !blk_queue_bypass(q)) {
Tejun Heo75eb6c32011-10-19 14:31:22 +02001064 rw_flags |= REQ_ELVPRIV;
Tejun Heo8a5ecdd2012-06-04 20:40:58 -07001065 q->nr_rqs_elvpriv++;
Tejun Heof1f8cc92011-12-14 00:33:42 +01001066 if (et->icq_cache && ioc)
1067 icq = ioc_lookup_icq(ioc, q);
Mike Snitzer9d5a4e92011-02-11 11:05:46 +01001068 }
Tejun Heocb98fc82005-10-28 08:29:39 +02001069
Jens Axboef253b862010-10-24 22:06:02 +02001070 if (blk_queue_io_stat(q))
1071 rw_flags |= REQ_IO_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 spin_unlock_irq(q->queue_lock);
1073
Tejun Heo29e2b092012-04-19 16:29:21 -07001074 /* allocate and init request */
Tejun Heo5b788ce2012-06-04 20:40:59 -07001075 rq = mempool_alloc(rl->rq_pool, gfp_mask);
Tejun Heo29e2b092012-04-19 16:29:21 -07001076 if (!rq)
Tejun Heob6792812012-03-05 13:15:23 -08001077 goto fail_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Tejun Heo29e2b092012-04-19 16:29:21 -07001079 blk_rq_init(q, rq);
Tejun Heoa0516612012-06-26 15:05:44 -07001080 blk_rq_set_rl(rq, rl);
Tejun Heo29e2b092012-04-19 16:29:21 -07001081 rq->cmd_flags = rw_flags | REQ_ALLOCED;
1082
Tejun Heoaaf7c682012-04-19 16:29:22 -07001083 /* init elvpriv */
Tejun Heo29e2b092012-04-19 16:29:21 -07001084 if (rw_flags & REQ_ELVPRIV) {
Tejun Heoaaf7c682012-04-19 16:29:22 -07001085 if (unlikely(et->icq_cache && !icq)) {
Tejun Heo7f4b35d2012-06-04 20:40:56 -07001086 if (ioc)
1087 icq = ioc_create_icq(ioc, q, gfp_mask);
Tejun Heoaaf7c682012-04-19 16:29:22 -07001088 if (!icq)
1089 goto fail_elvpriv;
Tejun Heo29e2b092012-04-19 16:29:21 -07001090 }
Tejun Heoaaf7c682012-04-19 16:29:22 -07001091
1092 rq->elv.icq = icq;
1093 if (unlikely(elv_set_request(q, rq, bio, gfp_mask)))
1094 goto fail_elvpriv;
1095
1096 /* @rq->elv.icq holds io_context until @rq is freed */
Tejun Heo29e2b092012-04-19 16:29:21 -07001097 if (icq)
1098 get_io_context(icq->ioc);
1099 }
Tejun Heoaaf7c682012-04-19 16:29:22 -07001100out:
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001101 /*
1102 * ioc may be NULL here, and ioc_batching will be false. That's
1103 * OK, if the queue is under the request limit then requests need
1104 * not count toward the nr_batch_requests limit. There will always
1105 * be some limit enforced by BLK_BATCH_TIME.
1106 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (ioc_batching(q, ioc))
1108 ioc->nr_batch_requests--;
Jens Axboe6728cb02008-01-31 13:03:55 +01001109
Jens Axboe1faa16d2009-04-06 14:48:01 +02001110 trace_block_getrq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 return rq;
Tejun Heob6792812012-03-05 13:15:23 -08001112
Tejun Heoaaf7c682012-04-19 16:29:22 -07001113fail_elvpriv:
1114 /*
1115 * elvpriv init failed. ioc, icq and elvpriv aren't mempool backed
1116 * and may fail indefinitely under memory pressure and thus
1117 * shouldn't stall IO. Treat this request as !elvpriv. This will
1118 * disturb iosched and blkcg but weird is bettern than dead.
1119 */
Robert Elliott7b2b10e2014-08-27 10:50:36 -05001120 printk_ratelimited(KERN_WARNING "%s: dev %s: request aux data allocation failed, iosched may be disturbed\n",
1121 __func__, dev_name(q->backing_dev_info.dev));
Tejun Heoaaf7c682012-04-19 16:29:22 -07001122
1123 rq->cmd_flags &= ~REQ_ELVPRIV;
1124 rq->elv.icq = NULL;
1125
1126 spin_lock_irq(q->queue_lock);
Tejun Heo8a5ecdd2012-06-04 20:40:58 -07001127 q->nr_rqs_elvpriv--;
Tejun Heoaaf7c682012-04-19 16:29:22 -07001128 spin_unlock_irq(q->queue_lock);
1129 goto out;
1130
Tejun Heob6792812012-03-05 13:15:23 -08001131fail_alloc:
1132 /*
1133 * Allocation failed presumably due to memory. Undo anything we
1134 * might have messed up.
1135 *
1136 * Allocating task should really be put onto the front of the wait
1137 * queue, but this is pretty rare.
1138 */
1139 spin_lock_irq(q->queue_lock);
Tejun Heo5b788ce2012-06-04 20:40:59 -07001140 freed_request(rl, rw_flags);
Tejun Heob6792812012-03-05 13:15:23 -08001141
1142 /*
1143 * in the very unlikely event that allocation failed and no
1144 * requests for this direction was pending, mark us starved so that
1145 * freeing of a request in the other direction will notice
1146 * us. another possible fix would be to split the rq mempool into
1147 * READ and WRITE
1148 */
1149rq_starved:
1150 if (unlikely(rl->count[is_sync] == 0))
1151 rl->starved[is_sync] = 1;
Joe Lawrencea492f072014-08-28 08:15:21 -06001152 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
1154
Tejun Heoda8303c2011-10-19 14:33:05 +02001155/**
Tejun Heoa06e05e2012-06-04 20:40:55 -07001156 * get_request - get a free request
Tejun Heoda8303c2011-10-19 14:33:05 +02001157 * @q: request_queue to allocate request from
1158 * @rw_flags: RW and SYNC flags
1159 * @bio: bio to allocate request for (can be %NULL)
Tejun Heoa06e05e2012-06-04 20:40:55 -07001160 * @gfp_mask: allocation mask
Nick Piggind6344532005-06-28 20:45:14 -07001161 *
Tejun Heoa06e05e2012-06-04 20:40:55 -07001162 * Get a free request from @q. If %__GFP_WAIT is set in @gfp_mask, this
1163 * function keeps retrying under memory pressure and fails iff @q is dead.
Tejun Heoda8303c2011-10-19 14:33:05 +02001164 *
Masanari Iidada3dae52014-09-09 01:27:23 +09001165 * Must be called with @q->queue_lock held and,
Joe Lawrencea492f072014-08-28 08:15:21 -06001166 * Returns ERR_PTR on failure, with @q->queue_lock held.
1167 * Returns request pointer on success, with @q->queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 */
Tejun Heoa06e05e2012-06-04 20:40:55 -07001169static struct request *get_request(struct request_queue *q, int rw_flags,
1170 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
Jens Axboe1faa16d2009-04-06 14:48:01 +02001172 const bool is_sync = rw_is_sync(rw_flags) != 0;
Tejun Heoa06e05e2012-06-04 20:40:55 -07001173 DEFINE_WAIT(wait);
Tejun Heoa0516612012-06-26 15:05:44 -07001174 struct request_list *rl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 struct request *rq;
Tejun Heoa0516612012-06-26 15:05:44 -07001176
1177 rl = blk_get_rl(q, bio); /* transferred to @rq on success */
Tejun Heoa06e05e2012-06-04 20:40:55 -07001178retry:
Tejun Heoa0516612012-06-26 15:05:44 -07001179 rq = __get_request(rl, rw_flags, bio, gfp_mask);
Joe Lawrencea492f072014-08-28 08:15:21 -06001180 if (!IS_ERR(rq))
Tejun Heoa06e05e2012-06-04 20:40:55 -07001181 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Bart Van Assche3f3299d2012-11-28 13:42:38 +01001183 if (!(gfp_mask & __GFP_WAIT) || unlikely(blk_queue_dying(q))) {
Tejun Heoa0516612012-06-26 15:05:44 -07001184 blk_put_rl(rl);
Joe Lawrencea492f072014-08-28 08:15:21 -06001185 return rq;
Tejun Heoa0516612012-06-26 15:05:44 -07001186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Tejun Heoa06e05e2012-06-04 20:40:55 -07001188 /* wait on @rl and retry */
1189 prepare_to_wait_exclusive(&rl->wait[is_sync], &wait,
1190 TASK_UNINTERRUPTIBLE);
Tejun Heoda8303c2011-10-19 14:33:05 +02001191
Tejun Heoa06e05e2012-06-04 20:40:55 -07001192 trace_block_sleeprq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Tejun Heoa06e05e2012-06-04 20:40:55 -07001194 spin_unlock_irq(q->queue_lock);
1195 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Tejun Heoa06e05e2012-06-04 20:40:55 -07001197 /*
1198 * After sleeping, we become a "batching" process and will be able
1199 * to allocate at least one request, and up to a big batch of them
1200 * for a small period time. See ioc_batching, ioc_set_batching
1201 */
Tejun Heoa06e05e2012-06-04 20:40:55 -07001202 ioc_set_batching(q, current->io_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Tejun Heoa06e05e2012-06-04 20:40:55 -07001204 spin_lock_irq(q->queue_lock);
1205 finish_wait(&rl->wait[is_sync], &wait);
Jens Axboe2056a782006-03-23 20:00:26 +01001206
Tejun Heoa06e05e2012-06-04 20:40:55 -07001207 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
Jens Axboe320ae512013-10-24 09:20:05 +01001210static struct request *blk_old_get_request(struct request_queue *q, int rw,
1211 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
1213 struct request *rq;
1214
1215 BUG_ON(rw != READ && rw != WRITE);
1216
Tejun Heo7f4b35d2012-06-04 20:40:56 -07001217 /* create ioc upfront */
1218 create_io_context(gfp_mask, q->node);
1219
Nick Piggind6344532005-06-28 20:45:14 -07001220 spin_lock_irq(q->queue_lock);
Tejun Heoa06e05e2012-06-04 20:40:55 -07001221 rq = get_request(q, rw, NULL, gfp_mask);
Joe Lawrencea492f072014-08-28 08:15:21 -06001222 if (IS_ERR(rq))
Tejun Heoda8303c2011-10-19 14:33:05 +02001223 spin_unlock_irq(q->queue_lock);
Nick Piggind6344532005-06-28 20:45:14 -07001224 /* q->queue_lock is unlocked at this point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 return rq;
1227}
Jens Axboe320ae512013-10-24 09:20:05 +01001228
1229struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
1230{
1231 if (q->mq_ops)
Christoph Hellwig4ce01dd2014-05-27 20:59:46 +02001232 return blk_mq_alloc_request(q, rw, gfp_mask, false);
Jens Axboe320ae512013-10-24 09:20:05 +01001233 else
1234 return blk_old_get_request(q, rw, gfp_mask);
1235}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236EXPORT_SYMBOL(blk_get_request);
1237
1238/**
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001239 * blk_make_request - given a bio, allocate a corresponding struct request.
Randy Dunlap8ebf9752009-06-11 20:00:41 -07001240 * @q: target request queue
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001241 * @bio: The bio describing the memory mappings that will be submitted for IO.
1242 * It may be a chained-bio properly constructed by block/bio layer.
Randy Dunlap8ebf9752009-06-11 20:00:41 -07001243 * @gfp_mask: gfp flags to be used for memory allocation
Jens Axboedc72ef42006-07-20 14:54:05 +02001244 *
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001245 * blk_make_request is the parallel of generic_make_request for BLOCK_PC
1246 * type commands. Where the struct request needs to be farther initialized by
1247 * the caller. It is passed a &struct bio, which describes the memory info of
1248 * the I/O transfer.
1249 *
1250 * The caller of blk_make_request must make sure that bi_io_vec
1251 * are set to describe the memory buffers. That bio_data_dir() will return
1252 * the needed direction of the request. (And all bio's in the passed bio-chain
1253 * are properly set accordingly)
1254 *
1255 * If called under none-sleepable conditions, mapped bio buffers must not
1256 * need bouncing, by calling the appropriate masked or flagged allocator,
1257 * suitable for the target device. Otherwise the call to blk_queue_bounce will
1258 * BUG.
Jens Axboe53674ac2009-05-19 19:52:35 +02001259 *
1260 * WARNING: When allocating/cloning a bio-chain, careful consideration should be
1261 * given to how you allocate bios. In particular, you cannot use __GFP_WAIT for
1262 * anything but the first bio in the chain. Otherwise you risk waiting for IO
1263 * completion of a bio that hasn't been submitted yet, thus resulting in a
1264 * deadlock. Alternatively bios should be allocated using bio_kmalloc() instead
1265 * of bio_alloc(), as that avoids the mempool deadlock.
1266 * If possible a big IO should be split into smaller parts when allocation
1267 * fails. Partial allocation should not be an error, or you risk a live-lock.
Jens Axboedc72ef42006-07-20 14:54:05 +02001268 */
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001269struct request *blk_make_request(struct request_queue *q, struct bio *bio,
1270 gfp_t gfp_mask)
Jens Axboedc72ef42006-07-20 14:54:05 +02001271{
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001272 struct request *rq = blk_get_request(q, bio_data_dir(bio), gfp_mask);
1273
Joe Lawrencea492f072014-08-28 08:15:21 -06001274 if (IS_ERR(rq))
1275 return rq;
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001276
Jens Axboef27b0872014-06-06 07:57:37 -06001277 blk_rq_set_block_pc(rq);
1278
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001279 for_each_bio(bio) {
1280 struct bio *bounce_bio = bio;
1281 int ret;
1282
1283 blk_queue_bounce(q, &bounce_bio);
1284 ret = blk_rq_append_bio(q, rq, bounce_bio);
1285 if (unlikely(ret)) {
1286 blk_put_request(rq);
1287 return ERR_PTR(ret);
1288 }
1289 }
1290
1291 return rq;
Jens Axboedc72ef42006-07-20 14:54:05 +02001292}
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001293EXPORT_SYMBOL(blk_make_request);
Jens Axboedc72ef42006-07-20 14:54:05 +02001294
1295/**
Masanari Iidada3dae52014-09-09 01:27:23 +09001296 * blk_rq_set_block_pc - initialize a request to type BLOCK_PC
Jens Axboef27b0872014-06-06 07:57:37 -06001297 * @rq: request to be initialized
1298 *
1299 */
1300void blk_rq_set_block_pc(struct request *rq)
1301{
1302 rq->cmd_type = REQ_TYPE_BLOCK_PC;
1303 rq->__data_len = 0;
1304 rq->__sector = (sector_t) -1;
1305 rq->bio = rq->biotail = NULL;
1306 memset(rq->__cmd, 0, sizeof(rq->__cmd));
Jens Axboef27b0872014-06-06 07:57:37 -06001307}
1308EXPORT_SYMBOL(blk_rq_set_block_pc);
1309
1310/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 * blk_requeue_request - put a request back on queue
1312 * @q: request queue where request should be inserted
1313 * @rq: request to be inserted
1314 *
1315 * Description:
1316 * Drivers often keep queueing requests until the hardware cannot accept
1317 * more, when that condition happens we need to put the request back
1318 * on the queue. Must be called with queue lock held.
1319 */
Jens Axboe165125e2007-07-24 09:28:11 +02001320void blk_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
Jens Axboe242f9dc2008-09-14 05:55:09 -07001322 blk_delete_timer(rq);
1323 blk_clear_rq_complete(rq);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001324 trace_block_rq_requeue(q, rq);
Jens Axboe2056a782006-03-23 20:00:26 +01001325
Christoph Hellwig125c99b2014-11-03 12:47:47 +01001326 if (rq->cmd_flags & REQ_QUEUED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 blk_queue_end_tag(q, rq);
1328
James Bottomleyba396a62009-05-27 14:17:08 +02001329 BUG_ON(blk_queued_rq(rq));
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 elv_requeue_request(q, rq);
1332}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333EXPORT_SYMBOL(blk_requeue_request);
1334
Jens Axboe73c10102011-03-08 13:19:51 +01001335static void add_acct_request(struct request_queue *q, struct request *rq,
1336 int where)
1337{
Jens Axboe320ae512013-10-24 09:20:05 +01001338 blk_account_io_start(rq, true);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001339 __elv_add_request(q, rq, where);
Jens Axboe73c10102011-03-08 13:19:51 +01001340}
1341
Tejun Heo074a7ac2008-08-25 19:56:14 +09001342static void part_round_stats_single(int cpu, struct hd_struct *part,
1343 unsigned long now)
1344{
Jens Axboe7276d022014-05-09 15:48:23 -06001345 int inflight;
1346
Tejun Heo074a7ac2008-08-25 19:56:14 +09001347 if (now == part->stamp)
1348 return;
1349
Jens Axboe7276d022014-05-09 15:48:23 -06001350 inflight = part_in_flight(part);
1351 if (inflight) {
Tejun Heo074a7ac2008-08-25 19:56:14 +09001352 __part_stat_add(cpu, part, time_in_queue,
Jens Axboe7276d022014-05-09 15:48:23 -06001353 inflight * (now - part->stamp));
Tejun Heo074a7ac2008-08-25 19:56:14 +09001354 __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
1355 }
1356 part->stamp = now;
1357}
1358
1359/**
Randy Dunlap496aa8a2008-10-16 07:46:23 +02001360 * part_round_stats() - Round off the performance stats on a struct disk_stats.
1361 * @cpu: cpu number for stats access
1362 * @part: target partition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 *
1364 * The average IO queue length and utilisation statistics are maintained
1365 * by observing the current state of the queue length and the amount of
1366 * time it has been in this state for.
1367 *
1368 * Normally, that accounting is done on IO completion, but that can result
1369 * in more than a second's worth of IO being accounted for within any one
1370 * second, leading to >100% utilisation. To deal with that, we call this
1371 * function to do a round-off before returning the results when reading
1372 * /proc/diskstats. This accounts immediately for all queue usage up to
1373 * the current jiffies and restarts the counters again.
1374 */
Tejun Heoc9959052008-08-25 19:47:21 +09001375void part_round_stats(int cpu, struct hd_struct *part)
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001376{
1377 unsigned long now = jiffies;
1378
Tejun Heo074a7ac2008-08-25 19:56:14 +09001379 if (part->partno)
1380 part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
1381 part_round_stats_single(cpu, part, now);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001382}
Tejun Heo074a7ac2008-08-25 19:56:14 +09001383EXPORT_SYMBOL_GPL(part_round_stats);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001384
Rafael J. Wysocki47fafbc2014-12-04 01:00:23 +01001385#ifdef CONFIG_PM
Lin Mingc8158812013-03-23 11:42:27 +08001386static void blk_pm_put_request(struct request *rq)
1387{
1388 if (rq->q->dev && !(rq->cmd_flags & REQ_PM) && !--rq->q->nr_pending)
1389 pm_runtime_mark_last_busy(rq->q->dev);
1390}
1391#else
1392static inline void blk_pm_put_request(struct request *rq) {}
1393#endif
1394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395/*
1396 * queue lock must be held
1397 */
Jens Axboe165125e2007-07-24 09:28:11 +02001398void __blk_put_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 if (unlikely(!q))
1401 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Christoph Hellwig6f5ba582014-02-07 10:22:37 -08001403 if (q->mq_ops) {
1404 blk_mq_free_request(req);
1405 return;
1406 }
1407
Lin Mingc8158812013-03-23 11:42:27 +08001408 blk_pm_put_request(req);
1409
Tejun Heo8922e162005-10-20 16:23:44 +02001410 elv_completed_request(q, req);
1411
Boaz Harrosh1cd96c22009-03-24 12:35:07 +01001412 /* this is a bio leak */
1413 WARN_ON(req->bio != NULL);
1414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 /*
1416 * Request may not have originated from ll_rw_blk. if not,
1417 * it didn't come out of our reserved rq pools
1418 */
Jens Axboe49171e52006-08-10 08:59:11 +02001419 if (req->cmd_flags & REQ_ALLOCED) {
Tejun Heo75eb6c32011-10-19 14:31:22 +02001420 unsigned int flags = req->cmd_flags;
Tejun Heoa0516612012-06-26 15:05:44 -07001421 struct request_list *rl = blk_rq_rl(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 BUG_ON(!list_empty(&req->queuelist));
Jens Axboe360f92c2014-04-09 20:27:01 -06001424 BUG_ON(ELV_ON_HASH(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Tejun Heoa0516612012-06-26 15:05:44 -07001426 blk_free_request(rl, req);
1427 freed_request(rl, flags);
1428 blk_put_rl(rl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
1430}
Mike Christie6e39b692005-11-11 05:30:24 -06001431EXPORT_SYMBOL_GPL(__blk_put_request);
1432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433void blk_put_request(struct request *req)
1434{
Jens Axboe165125e2007-07-24 09:28:11 +02001435 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Jens Axboe320ae512013-10-24 09:20:05 +01001437 if (q->mq_ops)
1438 blk_mq_free_request(req);
1439 else {
1440 unsigned long flags;
1441
1442 spin_lock_irqsave(q->queue_lock, flags);
1443 __blk_put_request(q, req);
1444 spin_unlock_irqrestore(q->queue_lock, flags);
1445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447EXPORT_SYMBOL(blk_put_request);
1448
Christoph Hellwig66ac0282010-06-18 16:59:42 +02001449/**
1450 * blk_add_request_payload - add a payload to a request
1451 * @rq: request to update
1452 * @page: page backing the payload
1453 * @len: length of the payload.
1454 *
1455 * This allows to later add a payload to an already submitted request by
1456 * a block driver. The driver needs to take care of freeing the payload
1457 * itself.
1458 *
1459 * Note that this is a quite horrible hack and nothing but handling of
1460 * discard requests should ever use it.
1461 */
1462void blk_add_request_payload(struct request *rq, struct page *page,
1463 unsigned int len)
1464{
1465 struct bio *bio = rq->bio;
1466
1467 bio->bi_io_vec->bv_page = page;
1468 bio->bi_io_vec->bv_offset = 0;
1469 bio->bi_io_vec->bv_len = len;
1470
Kent Overstreet4f024f32013-10-11 15:44:27 -07001471 bio->bi_iter.bi_size = len;
Christoph Hellwig66ac0282010-06-18 16:59:42 +02001472 bio->bi_vcnt = 1;
1473 bio->bi_phys_segments = 1;
1474
1475 rq->__data_len = rq->resid_len = len;
1476 rq->nr_phys_segments = 1;
Christoph Hellwig66ac0282010-06-18 16:59:42 +02001477}
1478EXPORT_SYMBOL_GPL(blk_add_request_payload);
1479
Jens Axboe320ae512013-10-24 09:20:05 +01001480bool bio_attempt_back_merge(struct request_queue *q, struct request *req,
1481 struct bio *bio)
Jens Axboe73c10102011-03-08 13:19:51 +01001482{
1483 const int ff = bio->bi_rw & REQ_FAILFAST_MASK;
1484
Jens Axboe73c10102011-03-08 13:19:51 +01001485 if (!ll_back_merge_fn(q, req, bio))
1486 return false;
1487
Tejun Heo8c1cf6b2013-01-11 13:06:34 -08001488 trace_block_bio_backmerge(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001489
1490 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1491 blk_rq_set_mixed_merge(req);
1492
1493 req->biotail->bi_next = bio;
1494 req->biotail = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001495 req->__data_len += bio->bi_iter.bi_size;
Jens Axboe73c10102011-03-08 13:19:51 +01001496 req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
1497
Jens Axboe320ae512013-10-24 09:20:05 +01001498 blk_account_io_start(req, false);
Jens Axboe73c10102011-03-08 13:19:51 +01001499 return true;
1500}
1501
Jens Axboe320ae512013-10-24 09:20:05 +01001502bool bio_attempt_front_merge(struct request_queue *q, struct request *req,
1503 struct bio *bio)
Jens Axboe73c10102011-03-08 13:19:51 +01001504{
1505 const int ff = bio->bi_rw & REQ_FAILFAST_MASK;
Jens Axboe73c10102011-03-08 13:19:51 +01001506
Jens Axboe73c10102011-03-08 13:19:51 +01001507 if (!ll_front_merge_fn(q, req, bio))
1508 return false;
1509
Tejun Heo8c1cf6b2013-01-11 13:06:34 -08001510 trace_block_bio_frontmerge(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001511
1512 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1513 blk_rq_set_mixed_merge(req);
1514
Jens Axboe73c10102011-03-08 13:19:51 +01001515 bio->bi_next = req->bio;
1516 req->bio = bio;
1517
Kent Overstreet4f024f32013-10-11 15:44:27 -07001518 req->__sector = bio->bi_iter.bi_sector;
1519 req->__data_len += bio->bi_iter.bi_size;
Jens Axboe73c10102011-03-08 13:19:51 +01001520 req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
1521
Jens Axboe320ae512013-10-24 09:20:05 +01001522 blk_account_io_start(req, false);
Jens Axboe73c10102011-03-08 13:19:51 +01001523 return true;
1524}
1525
Tejun Heobd87b582011-10-19 14:33:08 +02001526/**
Jens Axboe320ae512013-10-24 09:20:05 +01001527 * blk_attempt_plug_merge - try to merge with %current's plugged list
Tejun Heobd87b582011-10-19 14:33:08 +02001528 * @q: request_queue new bio is being queued at
1529 * @bio: new bio being queued
1530 * @request_count: out parameter for number of traversed plugged requests
1531 *
1532 * Determine whether @bio being queued on @q can be merged with a request
1533 * on %current's plugged list. Returns %true if merge was successful,
1534 * otherwise %false.
1535 *
Tejun Heo07c2bd32012-02-08 09:19:42 +01001536 * Plugging coalesces IOs from the same issuer for the same purpose without
1537 * going through @q->queue_lock. As such it's more of an issuing mechanism
1538 * than scheduling, and the request, while may have elvpriv data, is not
1539 * added on the elevator at this point. In addition, we don't have
1540 * reliable access to the elevator outside queue lock. Only check basic
1541 * merging parameters without querying the elevator.
Robert Elliottda41a582014-05-20 16:46:26 -05001542 *
1543 * Caller must ensure !blk_queue_nomerges(q) beforehand.
Jens Axboe73c10102011-03-08 13:19:51 +01001544 */
Jens Axboe320ae512013-10-24 09:20:05 +01001545bool blk_attempt_plug_merge(struct request_queue *q, struct bio *bio,
1546 unsigned int *request_count)
Jens Axboe73c10102011-03-08 13:19:51 +01001547{
1548 struct blk_plug *plug;
1549 struct request *rq;
1550 bool ret = false;
Shaohua Li92f399c2013-10-29 12:01:03 -06001551 struct list_head *plug_list;
Jens Axboe73c10102011-03-08 13:19:51 +01001552
Tejun Heobd87b582011-10-19 14:33:08 +02001553 plug = current->plug;
Jens Axboe73c10102011-03-08 13:19:51 +01001554 if (!plug)
1555 goto out;
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001556 *request_count = 0;
Jens Axboe73c10102011-03-08 13:19:51 +01001557
Shaohua Li92f399c2013-10-29 12:01:03 -06001558 if (q->mq_ops)
1559 plug_list = &plug->mq_list;
1560 else
1561 plug_list = &plug->list;
1562
1563 list_for_each_entry_reverse(rq, plug_list, queuelist) {
Jens Axboe73c10102011-03-08 13:19:51 +01001564 int el_ret;
1565
Shaohua Li1b2e19f2012-04-06 11:37:47 -06001566 if (rq->q == q)
1567 (*request_count)++;
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001568
Tejun Heo07c2bd32012-02-08 09:19:42 +01001569 if (rq->q != q || !blk_rq_merge_ok(rq, bio))
Jens Axboe73c10102011-03-08 13:19:51 +01001570 continue;
1571
Tejun Heo050c8ea2012-02-08 09:19:38 +01001572 el_ret = blk_try_merge(rq, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001573 if (el_ret == ELEVATOR_BACK_MERGE) {
1574 ret = bio_attempt_back_merge(q, rq, bio);
1575 if (ret)
1576 break;
1577 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
1578 ret = bio_attempt_front_merge(q, rq, bio);
1579 if (ret)
1580 break;
1581 }
1582 }
1583out:
1584 return ret;
1585}
1586
Jens Axboe86db1e22008-01-29 14:53:40 +01001587void init_request_from_bio(struct request *req, struct bio *bio)
Tejun Heo52d9e672006-01-06 09:49:58 +01001588{
Jens Axboe4aff5e22006-08-10 08:44:47 +02001589 req->cmd_type = REQ_TYPE_FS;
Tejun Heo52d9e672006-01-06 09:49:58 +01001590
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001591 req->cmd_flags |= bio->bi_rw & REQ_COMMON_MASK;
1592 if (bio->bi_rw & REQ_RAHEAD)
Tejun Heoa82afdf2009-07-03 17:48:16 +09001593 req->cmd_flags |= REQ_FAILFAST_MASK;
Jens Axboeb31dc662006-06-13 08:26:10 +02001594
Tejun Heo52d9e672006-01-06 09:49:58 +01001595 req->errors = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001596 req->__sector = bio->bi_iter.bi_sector;
Tejun Heo52d9e672006-01-06 09:49:58 +01001597 req->ioprio = bio_prio(bio);
NeilBrownbc1c56f2007-08-16 13:31:30 +02001598 blk_rq_bio_prep(req->q, req, bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001599}
1600
Mike Snitzer336b7e12015-05-11 14:06:32 -04001601static void blk_queue_bio(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
Jiri Slaby5e00d1b2010-08-12 14:31:06 +02001603 const bool sync = !!(bio->bi_rw & REQ_SYNC);
Jens Axboe73c10102011-03-08 13:19:51 +01001604 struct blk_plug *plug;
1605 int el_ret, rw_flags, where = ELEVATOR_INSERT_SORT;
1606 struct request *req;
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001607 unsigned int request_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 /*
1610 * low level driver can indicate that it wants pages above a
1611 * certain limit bounced to low memory (ie for highmem, or even
1612 * ISA dma in theory)
1613 */
1614 blk_queue_bounce(q, &bio);
1615
Darrick J. Wongffecfd12013-02-21 16:42:55 -08001616 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1617 bio_endio(bio, -EIO);
1618 return;
1619 }
1620
Tejun Heo4fed9472010-09-03 11:56:17 +02001621 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) {
Jens Axboe73c10102011-03-08 13:19:51 +01001622 spin_lock_irq(q->queue_lock);
Tejun Heoae1b1532011-01-25 12:43:54 +01001623 where = ELEVATOR_INSERT_FLUSH;
Tejun Heo28e7d182010-09-03 11:56:16 +02001624 goto get_rq;
1625 }
1626
Jens Axboe73c10102011-03-08 13:19:51 +01001627 /*
1628 * Check if we can merge with the plugged list before grabbing
1629 * any locks.
1630 */
Robert Elliottda41a582014-05-20 16:46:26 -05001631 if (!blk_queue_nomerges(q) &&
1632 blk_attempt_plug_merge(q, bio, &request_count))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001633 return;
Jens Axboe73c10102011-03-08 13:19:51 +01001634
1635 spin_lock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637 el_ret = elv_merge(q, &req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001638 if (el_ret == ELEVATOR_BACK_MERGE) {
Jens Axboe73c10102011-03-08 13:19:51 +01001639 if (bio_attempt_back_merge(q, req, bio)) {
Tejun Heo07c2bd32012-02-08 09:19:42 +01001640 elv_bio_merged(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001641 if (!attempt_back_merge(q, req))
1642 elv_merged_request(q, req, el_ret);
1643 goto out_unlock;
Tejun Heo80a761f2009-07-03 17:48:17 +09001644 }
Jens Axboe73c10102011-03-08 13:19:51 +01001645 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
Jens Axboe73c10102011-03-08 13:19:51 +01001646 if (bio_attempt_front_merge(q, req, bio)) {
Tejun Heo07c2bd32012-02-08 09:19:42 +01001647 elv_bio_merged(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001648 if (!attempt_front_merge(q, req))
1649 elv_merged_request(q, req, el_ret);
1650 goto out_unlock;
1651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 }
1653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654get_rq:
Nick Piggin450991b2005-06-28 20:45:13 -07001655 /*
Jens Axboe7749a8d2006-12-13 13:02:26 +01001656 * This sync check and mask will be re-done in init_request_from_bio(),
1657 * but we need to set it earlier to expose the sync flag to the
1658 * rq allocator and io schedulers.
1659 */
1660 rw_flags = bio_data_dir(bio);
1661 if (sync)
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001662 rw_flags |= REQ_SYNC;
Jens Axboe7749a8d2006-12-13 13:02:26 +01001663
1664 /*
Nick Piggin450991b2005-06-28 20:45:13 -07001665 * Grab a free request. This is might sleep but can not fail.
Nick Piggind6344532005-06-28 20:45:14 -07001666 * Returns with the queue unlocked.
Nick Piggin450991b2005-06-28 20:45:13 -07001667 */
Tejun Heoa06e05e2012-06-04 20:40:55 -07001668 req = get_request(q, rw_flags, bio, GFP_NOIO);
Joe Lawrencea492f072014-08-28 08:15:21 -06001669 if (IS_ERR(req)) {
1670 bio_endio(bio, PTR_ERR(req)); /* @q is dead */
Tejun Heoda8303c2011-10-19 14:33:05 +02001671 goto out_unlock;
1672 }
Nick Piggind6344532005-06-28 20:45:14 -07001673
Nick Piggin450991b2005-06-28 20:45:13 -07001674 /*
1675 * After dropping the lock and possibly sleeping here, our request
1676 * may now be mergeable after it had proven unmergeable (above).
1677 * We don't worry about that case for efficiency. It won't happen
1678 * often, and the elevators are able to handle it.
1679 */
Tejun Heo52d9e672006-01-06 09:49:58 +01001680 init_request_from_bio(req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Tao Ma9562ad92011-10-24 16:11:30 +02001682 if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags))
Jens Axboe11ccf112011-07-26 15:01:15 +02001683 req->cpu = raw_smp_processor_id();
Tejun Heodd831002010-09-03 11:56:16 +02001684
Jens Axboe73c10102011-03-08 13:19:51 +01001685 plug = current->plug;
Jens Axboe721a9602011-03-09 11:56:30 +01001686 if (plug) {
Jens Axboedc6d36c2011-04-12 10:28:28 +02001687 /*
1688 * If this is the first request added after a plug, fire
Jianpeng Ma7aef2e72013-09-11 13:21:07 -06001689 * of a plug trace.
Jens Axboedc6d36c2011-04-12 10:28:28 +02001690 */
Jianpeng Ma7aef2e72013-09-11 13:21:07 -06001691 if (!request_count)
Jens Axboedc6d36c2011-04-12 10:28:28 +02001692 trace_block_plug(q);
Shaohua Li3540d5e2011-11-16 09:21:50 +01001693 else {
Shaohua Li019ceb72011-11-16 09:21:50 +01001694 if (request_count >= BLK_MAX_REQUEST_COUNT) {
Shaohua Li3540d5e2011-11-16 09:21:50 +01001695 blk_flush_plug_list(plug, false);
Shaohua Li019ceb72011-11-16 09:21:50 +01001696 trace_block_plug(q);
1697 }
Jens Axboe73c10102011-03-08 13:19:51 +01001698 }
Shaohua Lia6327162011-08-24 16:04:32 +02001699 list_add_tail(&req->queuelist, &plug->list);
Jens Axboe320ae512013-10-24 09:20:05 +01001700 blk_account_io_start(req, true);
Jens Axboe73c10102011-03-08 13:19:51 +01001701 } else {
1702 spin_lock_irq(q->queue_lock);
1703 add_acct_request(q, req, where);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02001704 __blk_run_queue(q);
Jens Axboe73c10102011-03-08 13:19:51 +01001705out_unlock:
1706 spin_unlock_irq(q->queue_lock);
1707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708}
1709
1710/*
1711 * If bio->bi_dev is a partition, remap the location
1712 */
1713static inline void blk_partition_remap(struct bio *bio)
1714{
1715 struct block_device *bdev = bio->bi_bdev;
1716
Jens Axboebf2de6f2007-09-27 13:01:25 +02001717 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 struct hd_struct *p = bdev->bd_part;
Jens Axboea3623572005-11-01 09:26:16 +01001719
Kent Overstreet4f024f32013-10-11 15:44:27 -07001720 bio->bi_iter.bi_sector += p->start_sect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 bio->bi_bdev = bdev->bd_contains;
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001722
Mike Snitzerd07335e2010-11-16 12:52:38 +01001723 trace_block_bio_remap(bdev_get_queue(bio->bi_bdev), bio,
1724 bdev->bd_dev,
Kent Overstreet4f024f32013-10-11 15:44:27 -07001725 bio->bi_iter.bi_sector - p->start_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 }
1727}
1728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729static void handle_bad_sector(struct bio *bio)
1730{
1731 char b[BDEVNAME_SIZE];
1732
1733 printk(KERN_INFO "attempt to access beyond end of device\n");
1734 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1735 bdevname(bio->bi_bdev, b),
1736 bio->bi_rw,
Kent Overstreetf73a1c72012-09-25 15:05:12 -07001737 (unsigned long long)bio_end_sector(bio),
Mike Snitzer77304d22010-11-08 14:39:12 +01001738 (long long)(i_size_read(bio->bi_bdev->bd_inode) >> 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
1740 set_bit(BIO_EOF, &bio->bi_flags);
1741}
1742
Akinobu Mitac17bb492006-12-08 02:39:46 -08001743#ifdef CONFIG_FAIL_MAKE_REQUEST
1744
1745static DECLARE_FAULT_ATTR(fail_make_request);
1746
1747static int __init setup_fail_make_request(char *str)
1748{
1749 return setup_fault_attr(&fail_make_request, str);
1750}
1751__setup("fail_make_request=", setup_fail_make_request);
1752
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001753static bool should_fail_request(struct hd_struct *part, unsigned int bytes)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001754{
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001755 return part->make_it_fail && should_fail(&fail_make_request, bytes);
Akinobu Mitac17bb492006-12-08 02:39:46 -08001756}
1757
1758static int __init fail_make_request_debugfs(void)
1759{
Akinobu Mitadd48c082011-08-03 16:21:01 -07001760 struct dentry *dir = fault_create_debugfs_attr("fail_make_request",
1761 NULL, &fail_make_request);
1762
Duan Jiong21f9fcd2014-04-11 15:58:56 +08001763 return PTR_ERR_OR_ZERO(dir);
Akinobu Mitac17bb492006-12-08 02:39:46 -08001764}
1765
1766late_initcall(fail_make_request_debugfs);
1767
1768#else /* CONFIG_FAIL_MAKE_REQUEST */
1769
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001770static inline bool should_fail_request(struct hd_struct *part,
1771 unsigned int bytes)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001772{
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001773 return false;
Akinobu Mitac17bb492006-12-08 02:39:46 -08001774}
1775
1776#endif /* CONFIG_FAIL_MAKE_REQUEST */
1777
Jens Axboec07e2b42007-07-18 13:27:58 +02001778/*
1779 * Check whether this bio extends beyond the end of the device.
1780 */
1781static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1782{
1783 sector_t maxsector;
1784
1785 if (!nr_sectors)
1786 return 0;
1787
1788 /* Test device or partition size, when known. */
Mike Snitzer77304d22010-11-08 14:39:12 +01001789 maxsector = i_size_read(bio->bi_bdev->bd_inode) >> 9;
Jens Axboec07e2b42007-07-18 13:27:58 +02001790 if (maxsector) {
Kent Overstreet4f024f32013-10-11 15:44:27 -07001791 sector_t sector = bio->bi_iter.bi_sector;
Jens Axboec07e2b42007-07-18 13:27:58 +02001792
1793 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1794 /*
1795 * This may well happen - the kernel calls bread()
1796 * without checking the size of the device, e.g., when
1797 * mounting a device.
1798 */
1799 handle_bad_sector(bio);
1800 return 1;
1801 }
1802 }
1803
1804 return 0;
1805}
1806
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001807static noinline_for_stack bool
1808generic_make_request_checks(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
Jens Axboe165125e2007-07-24 09:28:11 +02001810 struct request_queue *q;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001811 int nr_sectors = bio_sectors(bio);
Jens Axboe51fd77b2007-11-02 08:49:08 +01001812 int err = -EIO;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001813 char b[BDEVNAME_SIZE];
1814 struct hd_struct *part;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
1816 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Jens Axboec07e2b42007-07-18 13:27:58 +02001818 if (bio_check_eod(bio, nr_sectors))
1819 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001821 q = bdev_get_queue(bio->bi_bdev);
1822 if (unlikely(!q)) {
1823 printk(KERN_ERR
1824 "generic_make_request: Trying to access "
1825 "nonexistent block-device %s (%Lu)\n",
1826 bdevname(bio->bi_bdev, b),
Kent Overstreet4f024f32013-10-11 15:44:27 -07001827 (long long) bio->bi_iter.bi_sector);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001828 goto end_io;
1829 }
1830
Martin K. Petersene2a60da2012-09-18 12:19:25 -04001831 if (likely(bio_is_rw(bio) &&
1832 nr_sectors > queue_max_hw_sectors(q))) {
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001833 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
1834 bdevname(bio->bi_bdev, b),
1835 bio_sectors(bio),
1836 queue_max_hw_sectors(q));
1837 goto end_io;
1838 }
1839
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001840 part = bio->bi_bdev->bd_part;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001841 if (should_fail_request(part, bio->bi_iter.bi_size) ||
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001842 should_fail_request(&part_to_disk(part)->part0,
Kent Overstreet4f024f32013-10-11 15:44:27 -07001843 bio->bi_iter.bi_size))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001844 goto end_io;
1845
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 /*
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001847 * If this device has partitions, remap block n
1848 * of partition p to block n+start(p) of the disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001850 blk_partition_remap(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001852 if (bio_check_eod(bio, nr_sectors))
1853 goto end_io;
1854
1855 /*
1856 * Filter flush bio's early so that make_request based
1857 * drivers without flush support don't have to worry
1858 * about them.
1859 */
1860 if ((bio->bi_rw & (REQ_FLUSH | REQ_FUA)) && !q->flush_flags) {
1861 bio->bi_rw &= ~(REQ_FLUSH | REQ_FUA);
1862 if (!nr_sectors) {
1863 err = 0;
Tejun Heoa7384672008-11-28 13:32:03 +09001864 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 }
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001868 if ((bio->bi_rw & REQ_DISCARD) &&
1869 (!blk_queue_discard(q) ||
Martin K. Petersene2a60da2012-09-18 12:19:25 -04001870 ((bio->bi_rw & REQ_SECURE) && !blk_queue_secdiscard(q)))) {
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001871 err = -EOPNOTSUPP;
1872 goto end_io;
1873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Martin K. Petersen4363ac72012-09-18 12:19:27 -04001875 if (bio->bi_rw & REQ_WRITE_SAME && !bdev_write_same(bio->bi_bdev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 err = -EOPNOTSUPP;
1877 goto end_io;
1878 }
1879
Tejun Heo7f4b35d2012-06-04 20:40:56 -07001880 /*
1881 * Various block parts want %current->io_context and lazy ioc
1882 * allocation ends up trading a lot of pain for a small amount of
1883 * memory. Just allocate it upfront. This may fail and block
1884 * layer knows how to live with it.
1885 */
1886 create_io_context(GFP_ATOMIC, q->node);
1887
Tejun Heo927bdd12015-08-18 14:55:20 -07001888 if (!blkcg_bio_issue_check(q, bio))
1889 return false;
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001890
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001891 trace_block_bio_queue(q, bio);
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001892 return true;
Tejun Heoa7384672008-11-28 13:32:03 +09001893
1894end_io:
1895 bio_endio(bio, err);
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001896 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897}
1898
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001899/**
1900 * generic_make_request - hand a buffer to its device driver for I/O
1901 * @bio: The bio describing the location in memory and on the device.
1902 *
1903 * generic_make_request() is used to make I/O requests of block
1904 * devices. It is passed a &struct bio, which describes the I/O that needs
1905 * to be done.
1906 *
1907 * generic_make_request() does not return any status. The
1908 * success/failure status of the request, along with notification of
1909 * completion, is delivered asynchronously through the bio->bi_end_io
1910 * function described (one day) else where.
1911 *
1912 * The caller of generic_make_request must make sure that bi_io_vec
1913 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1914 * set to describe the device address, and the
1915 * bi_end_io and optionally bi_private are set to describe how
1916 * completion notification should be signaled.
1917 *
1918 * generic_make_request and the drivers it calls may use bi_next if this
1919 * bio happens to be merged with someone else, and may resubmit the bio to
1920 * a lower device by calling into generic_make_request recursively, which
1921 * means the bio should NOT be touched after the call to ->make_request_fn.
Neil Brownd89d8792007-05-01 09:53:42 +02001922 */
1923void generic_make_request(struct bio *bio)
1924{
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001925 struct bio_list bio_list_on_stack;
1926
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001927 if (!generic_make_request_checks(bio))
1928 return;
1929
1930 /*
1931 * We only want one ->make_request_fn to be active at a time, else
1932 * stack usage with stacked devices could be a problem. So use
1933 * current->bio_list to keep a list of requests submited by a
1934 * make_request_fn function. current->bio_list is also used as a
1935 * flag to say if generic_make_request is currently active in this
1936 * task or not. If it is NULL, then no make_request is active. If
1937 * it is non-NULL, then a make_request is active, and new requests
1938 * should be added at the tail
1939 */
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001940 if (current->bio_list) {
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001941 bio_list_add(current->bio_list, bio);
Neil Brownd89d8792007-05-01 09:53:42 +02001942 return;
1943 }
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001944
Neil Brownd89d8792007-05-01 09:53:42 +02001945 /* following loop may be a bit non-obvious, and so deserves some
1946 * explanation.
1947 * Before entering the loop, bio->bi_next is NULL (as all callers
1948 * ensure that) so we have a list with a single bio.
1949 * We pretend that we have just taken it off a longer list, so
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001950 * we assign bio_list to a pointer to the bio_list_on_stack,
1951 * thus initialising the bio_list of new bios to be
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001952 * added. ->make_request() may indeed add some more bios
Neil Brownd89d8792007-05-01 09:53:42 +02001953 * through a recursive call to generic_make_request. If it
1954 * did, we find a non-NULL value in bio_list and re-enter the loop
1955 * from the top. In this case we really did just take the bio
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001956 * of the top of the list (no pretending) and so remove it from
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001957 * bio_list, and call into ->make_request() again.
Neil Brownd89d8792007-05-01 09:53:42 +02001958 */
1959 BUG_ON(bio->bi_next);
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001960 bio_list_init(&bio_list_on_stack);
1961 current->bio_list = &bio_list_on_stack;
Neil Brownd89d8792007-05-01 09:53:42 +02001962 do {
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001963 struct request_queue *q = bdev_get_queue(bio->bi_bdev);
1964
1965 q->make_request_fn(q, bio);
1966
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001967 bio = bio_list_pop(current->bio_list);
Neil Brownd89d8792007-05-01 09:53:42 +02001968 } while (bio);
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001969 current->bio_list = NULL; /* deactivate */
Neil Brownd89d8792007-05-01 09:53:42 +02001970}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971EXPORT_SYMBOL(generic_make_request);
1972
1973/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001974 * submit_bio - submit a bio to the block device layer for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1976 * @bio: The &struct bio which describes the I/O
1977 *
1978 * submit_bio() is very similar in purpose to generic_make_request(), and
1979 * uses that function to do most of the work. Both are fairly rough
Randy Dunlap710027a2008-08-19 20:13:11 +02001980 * interfaces; @bio must be presetup and ready for I/O.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 *
1982 */
1983void submit_bio(int rw, struct bio *bio)
1984{
Jens Axboe22e2c502005-06-27 10:55:12 +02001985 bio->bi_rw |= rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Jens Axboebf2de6f2007-09-27 13:01:25 +02001987 /*
1988 * If it's a regular read/write or a barrier with data attached,
1989 * go through the normal accounting stuff before submission.
1990 */
Martin K. Petersene2a60da2012-09-18 12:19:25 -04001991 if (bio_has_data(bio)) {
Martin K. Petersen4363ac72012-09-18 12:19:27 -04001992 unsigned int count;
1993
1994 if (unlikely(rw & REQ_WRITE_SAME))
1995 count = bdev_logical_block_size(bio->bi_bdev) >> 9;
1996 else
1997 count = bio_sectors(bio);
1998
Jens Axboebf2de6f2007-09-27 13:01:25 +02001999 if (rw & WRITE) {
2000 count_vm_events(PGPGOUT, count);
2001 } else {
Kent Overstreet4f024f32013-10-11 15:44:27 -07002002 task_io_account_read(bio->bi_iter.bi_size);
Jens Axboebf2de6f2007-09-27 13:01:25 +02002003 count_vm_events(PGPGIN, count);
2004 }
2005
2006 if (unlikely(block_dump)) {
2007 char b[BDEVNAME_SIZE];
San Mehat8dcbdc72010-09-14 08:48:01 +02002008 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s (%u sectors)\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002009 current->comm, task_pid_nr(current),
Jens Axboebf2de6f2007-09-27 13:01:25 +02002010 (rw & WRITE) ? "WRITE" : "READ",
Kent Overstreet4f024f32013-10-11 15:44:27 -07002011 (unsigned long long)bio->bi_iter.bi_sector,
San Mehat8dcbdc72010-09-14 08:48:01 +02002012 bdevname(bio->bi_bdev, b),
2013 count);
Jens Axboebf2de6f2007-09-27 13:01:25 +02002014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 }
2016
2017 generic_make_request(bio);
2018}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019EXPORT_SYMBOL(submit_bio);
2020
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002021/**
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002022 * blk_rq_check_limits - Helper function to check a request for the queue limit
2023 * @q: the queue
2024 * @rq: the request being checked
2025 *
2026 * Description:
2027 * @rq may have been made based on weaker limitations of upper-level queues
2028 * in request stacking drivers, and it may violate the limitation of @q.
2029 * Since the block layer and the underlying device driver trust @rq
2030 * after it is inserted to @q, it should be checked against @q before
2031 * the insertion using this generic function.
2032 *
2033 * This function should also be useful for request stacking drivers
Stefan Weileef35c22010-08-06 21:11:15 +02002034 * in some cases below, so export this function.
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002035 * Request stacking drivers like request-based dm may change the queue
2036 * limits while requests are in the queue (e.g. dm's table swapping).
Masanari Iidae2278672014-02-18 22:54:36 +09002037 * Such request stacking drivers should check those requests against
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002038 * the new queue limits again when they dispatch those requests,
2039 * although such checkings are also done against the old queue limits
2040 * when submitting requests.
2041 */
2042int blk_rq_check_limits(struct request_queue *q, struct request *rq)
2043{
Martin K. Petersene2a60da2012-09-18 12:19:25 -04002044 if (!rq_mergeable(rq))
ike Snitzer33839772010-08-08 12:11:33 -04002045 return 0;
2046
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -04002047 if (blk_rq_sectors(rq) > blk_queue_get_max_sectors(q, rq->cmd_flags)) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002048 printk(KERN_ERR "%s: over max size limit.\n", __func__);
2049 return -EIO;
2050 }
2051
2052 /*
2053 * queue's settings related to segment counting like q->bounce_pfn
2054 * may differ from that of other stacking queues.
2055 * Recalculate it to check the request correctly on this queue's
2056 * limitation.
2057 */
2058 blk_recalc_rq_segments(rq);
Martin K. Petersen8a783622010-02-26 00:20:39 -05002059 if (rq->nr_phys_segments > queue_max_segments(q)) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002060 printk(KERN_ERR "%s: over max segments limit.\n", __func__);
2061 return -EIO;
2062 }
2063
2064 return 0;
2065}
2066EXPORT_SYMBOL_GPL(blk_rq_check_limits);
2067
2068/**
2069 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
2070 * @q: the queue to submit the request
2071 * @rq: the request being queued
2072 */
2073int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
2074{
2075 unsigned long flags;
Jeff Moyer4853aba2011-08-15 21:37:25 +02002076 int where = ELEVATOR_INSERT_BACK;
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002077
2078 if (blk_rq_check_limits(q, rq))
2079 return -EIO;
2080
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07002081 if (rq->rq_disk &&
2082 should_fail_request(&rq->rq_disk->part0, blk_rq_bytes(rq)))
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002083 return -EIO;
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002084
Keith Busch7fb48982014-10-17 17:46:38 -06002085 if (q->mq_ops) {
2086 if (blk_queue_io_stat(q))
2087 blk_account_io_start(rq, true);
Mike Snitzerfb1840e2016-02-05 08:49:01 -05002088 blk_mq_insert_request(rq, false, true, false);
Keith Busch7fb48982014-10-17 17:46:38 -06002089 return 0;
2090 }
2091
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002092 spin_lock_irqsave(q->queue_lock, flags);
Bart Van Assche3f3299d2012-11-28 13:42:38 +01002093 if (unlikely(blk_queue_dying(q))) {
Tejun Heo8ba61432011-12-14 00:33:37 +01002094 spin_unlock_irqrestore(q->queue_lock, flags);
2095 return -ENODEV;
2096 }
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002097
2098 /*
2099 * Submitting request must be dequeued before calling this function
2100 * because it will be linked to another request_queue
2101 */
2102 BUG_ON(blk_queued_rq(rq));
2103
Jeff Moyer4853aba2011-08-15 21:37:25 +02002104 if (rq->cmd_flags & (REQ_FLUSH|REQ_FUA))
2105 where = ELEVATOR_INSERT_FLUSH;
2106
2107 add_acct_request(q, rq, where);
Jeff Moyere67b77c2011-10-17 12:57:23 +02002108 if (where == ELEVATOR_INSERT_FLUSH)
2109 __blk_run_queue(q);
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002110 spin_unlock_irqrestore(q->queue_lock, flags);
2111
2112 return 0;
2113}
2114EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
2115
Tejun Heo80a761f2009-07-03 17:48:17 +09002116/**
2117 * blk_rq_err_bytes - determine number of bytes till the next failure boundary
2118 * @rq: request to examine
2119 *
2120 * Description:
2121 * A request could be merge of IOs which require different failure
2122 * handling. This function determines the number of bytes which
2123 * can be failed from the beginning of the request without
2124 * crossing into area which need to be retried further.
2125 *
2126 * Return:
2127 * The number of bytes to fail.
2128 *
2129 * Context:
2130 * queue_lock must be held.
2131 */
2132unsigned int blk_rq_err_bytes(const struct request *rq)
2133{
2134 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
2135 unsigned int bytes = 0;
2136 struct bio *bio;
2137
2138 if (!(rq->cmd_flags & REQ_MIXED_MERGE))
2139 return blk_rq_bytes(rq);
2140
2141 /*
2142 * Currently the only 'mixing' which can happen is between
2143 * different fastfail types. We can safely fail portions
2144 * which have all the failfast bits that the first one has -
2145 * the ones which are at least as eager to fail as the first
2146 * one.
2147 */
2148 for (bio = rq->bio; bio; bio = bio->bi_next) {
2149 if ((bio->bi_rw & ff) != ff)
2150 break;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002151 bytes += bio->bi_iter.bi_size;
Tejun Heo80a761f2009-07-03 17:48:17 +09002152 }
2153
2154 /* this could lead to infinite loop */
2155 BUG_ON(blk_rq_bytes(rq) && !bytes);
2156 return bytes;
2157}
2158EXPORT_SYMBOL_GPL(blk_rq_err_bytes);
2159
Jens Axboe320ae512013-10-24 09:20:05 +01002160void blk_account_io_completion(struct request *req, unsigned int bytes)
Jens Axboebc58ba92009-01-23 10:54:44 +01002161{
Jens Axboec2553b52009-04-24 08:10:11 +02002162 if (blk_do_io_stat(req)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01002163 const int rw = rq_data_dir(req);
2164 struct hd_struct *part;
2165 int cpu;
2166
2167 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +01002168 part = req->part;
Jens Axboebc58ba92009-01-23 10:54:44 +01002169 part_stat_add(cpu, part, sectors[rw], bytes >> 9);
2170 part_stat_unlock();
2171 }
2172}
2173
Jens Axboe320ae512013-10-24 09:20:05 +01002174void blk_account_io_done(struct request *req)
Jens Axboebc58ba92009-01-23 10:54:44 +01002175{
Jens Axboebc58ba92009-01-23 10:54:44 +01002176 /*
Tejun Heodd4c1332010-09-03 11:56:16 +02002177 * Account IO completion. flush_rq isn't accounted as a
2178 * normal IO on queueing nor completion. Accounting the
2179 * containing request is enough.
Jens Axboebc58ba92009-01-23 10:54:44 +01002180 */
Tejun Heo414b4ff2011-01-25 12:43:49 +01002181 if (blk_do_io_stat(req) && !(req->cmd_flags & REQ_FLUSH_SEQ)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01002182 unsigned long duration = jiffies - req->start_time;
2183 const int rw = rq_data_dir(req);
2184 struct hd_struct *part;
2185 int cpu;
2186
2187 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +01002188 part = req->part;
Jens Axboebc58ba92009-01-23 10:54:44 +01002189
2190 part_stat_inc(cpu, part, ios[rw]);
2191 part_stat_add(cpu, part, ticks[rw], duration);
2192 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02002193 part_dec_in_flight(part, rw);
Jens Axboebc58ba92009-01-23 10:54:44 +01002194
Jens Axboe6c23a962011-01-07 08:43:37 +01002195 hd_struct_put(part);
Jens Axboebc58ba92009-01-23 10:54:44 +01002196 part_stat_unlock();
2197 }
2198}
2199
Rafael J. Wysocki47fafbc2014-12-04 01:00:23 +01002200#ifdef CONFIG_PM
Lin Mingc8158812013-03-23 11:42:27 +08002201/*
2202 * Don't process normal requests when queue is suspended
2203 * or in the process of suspending/resuming
2204 */
2205static struct request *blk_pm_peek_request(struct request_queue *q,
2206 struct request *rq)
2207{
2208 if (q->dev && (q->rpm_status == RPM_SUSPENDED ||
2209 (q->rpm_status != RPM_ACTIVE && !(rq->cmd_flags & REQ_PM))))
2210 return NULL;
2211 else
2212 return rq;
2213}
2214#else
2215static inline struct request *blk_pm_peek_request(struct request_queue *q,
2216 struct request *rq)
2217{
2218 return rq;
2219}
2220#endif
2221
Jens Axboe320ae512013-10-24 09:20:05 +01002222void blk_account_io_start(struct request *rq, bool new_io)
2223{
2224 struct hd_struct *part;
2225 int rw = rq_data_dir(rq);
2226 int cpu;
2227
2228 if (!blk_do_io_stat(rq))
2229 return;
2230
2231 cpu = part_stat_lock();
2232
2233 if (!new_io) {
2234 part = rq->part;
2235 part_stat_inc(cpu, part, merges[rw]);
2236 } else {
2237 part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq));
2238 if (!hd_struct_try_get(part)) {
2239 /*
2240 * The partition is already being removed,
2241 * the request will be accounted on the disk only
2242 *
2243 * We take a reference on disk->part0 although that
2244 * partition will never be deleted, so we can treat
2245 * it as any other partition.
2246 */
2247 part = &rq->rq_disk->part0;
2248 hd_struct_get(part);
2249 }
2250 part_round_stats(cpu, part);
2251 part_inc_in_flight(part, rw);
2252 rq->part = part;
2253 }
2254
2255 part_stat_unlock();
2256}
2257
Tejun Heo53a08802008-12-03 12:41:26 +01002258/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09002259 * blk_peek_request - peek at the top of a request queue
2260 * @q: request queue to peek at
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002261 *
2262 * Description:
Tejun Heo9934c8c2009-05-08 11:54:16 +09002263 * Return the request at the top of @q. The returned request
2264 * should be started using blk_start_request() before LLD starts
2265 * processing it.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002266 *
2267 * Return:
Tejun Heo9934c8c2009-05-08 11:54:16 +09002268 * Pointer to the request at the top of @q if available. Null
2269 * otherwise.
2270 *
2271 * Context:
2272 * queue_lock must be held.
2273 */
2274struct request *blk_peek_request(struct request_queue *q)
Tejun Heo158dbda2009-04-23 11:05:18 +09002275{
2276 struct request *rq;
2277 int ret;
2278
2279 while ((rq = __elv_next_request(q)) != NULL) {
Lin Mingc8158812013-03-23 11:42:27 +08002280
2281 rq = blk_pm_peek_request(q, rq);
2282 if (!rq)
2283 break;
2284
Tejun Heo158dbda2009-04-23 11:05:18 +09002285 if (!(rq->cmd_flags & REQ_STARTED)) {
2286 /*
2287 * This is the first time the device driver
2288 * sees this request (possibly after
2289 * requeueing). Notify IO scheduler.
2290 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002291 if (rq->cmd_flags & REQ_SORTED)
Tejun Heo158dbda2009-04-23 11:05:18 +09002292 elv_activate_rq(q, rq);
2293
2294 /*
2295 * just mark as started even if we don't start
2296 * it, a request that has been delayed should
2297 * not be passed by new incoming requests
2298 */
2299 rq->cmd_flags |= REQ_STARTED;
2300 trace_block_rq_issue(q, rq);
2301 }
2302
2303 if (!q->boundary_rq || q->boundary_rq == rq) {
2304 q->end_sector = rq_end_sector(rq);
2305 q->boundary_rq = NULL;
2306 }
2307
2308 if (rq->cmd_flags & REQ_DONTPREP)
2309 break;
2310
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002311 if (q->dma_drain_size && blk_rq_bytes(rq)) {
Tejun Heo158dbda2009-04-23 11:05:18 +09002312 /*
2313 * make sure space for the drain appears we
2314 * know we can do this because max_hw_segments
2315 * has been adjusted to be one fewer than the
2316 * device can handle
2317 */
2318 rq->nr_phys_segments++;
2319 }
2320
2321 if (!q->prep_rq_fn)
2322 break;
2323
2324 ret = q->prep_rq_fn(q, rq);
2325 if (ret == BLKPREP_OK) {
2326 break;
2327 } else if (ret == BLKPREP_DEFER) {
2328 /*
2329 * the request may have been (partially) prepped.
2330 * we need to keep this request in the front to
2331 * avoid resource deadlock. REQ_STARTED will
2332 * prevent other fs requests from passing this one.
2333 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002334 if (q->dma_drain_size && blk_rq_bytes(rq) &&
Tejun Heo158dbda2009-04-23 11:05:18 +09002335 !(rq->cmd_flags & REQ_DONTPREP)) {
2336 /*
2337 * remove the space for the drain we added
2338 * so that we don't add it again
2339 */
2340 --rq->nr_phys_segments;
2341 }
2342
2343 rq = NULL;
2344 break;
2345 } else if (ret == BLKPREP_KILL) {
2346 rq->cmd_flags |= REQ_QUIET;
James Bottomleyc143dc92009-05-30 06:43:49 +02002347 /*
2348 * Mark this request as started so we don't trigger
2349 * any debug logic in the end I/O path.
2350 */
2351 blk_start_request(rq);
Tejun Heo40cbbb72009-04-23 11:05:19 +09002352 __blk_end_request_all(rq, -EIO);
Tejun Heo158dbda2009-04-23 11:05:18 +09002353 } else {
2354 printk(KERN_ERR "%s: bad return=%d\n", __func__, ret);
2355 break;
2356 }
2357 }
2358
2359 return rq;
2360}
Tejun Heo9934c8c2009-05-08 11:54:16 +09002361EXPORT_SYMBOL(blk_peek_request);
Tejun Heo158dbda2009-04-23 11:05:18 +09002362
Tejun Heo9934c8c2009-05-08 11:54:16 +09002363void blk_dequeue_request(struct request *rq)
Tejun Heo158dbda2009-04-23 11:05:18 +09002364{
Tejun Heo9934c8c2009-05-08 11:54:16 +09002365 struct request_queue *q = rq->q;
2366
Tejun Heo158dbda2009-04-23 11:05:18 +09002367 BUG_ON(list_empty(&rq->queuelist));
2368 BUG_ON(ELV_ON_HASH(rq));
2369
2370 list_del_init(&rq->queuelist);
2371
2372 /*
2373 * the time frame between a request being removed from the lists
2374 * and to it is freed is accounted as io that is in progress at
2375 * the driver side.
2376 */
Divyesh Shah91952912010-04-01 15:01:41 -07002377 if (blk_account_rq(rq)) {
Jens Axboe0a7ae2f2009-05-20 08:54:31 +02002378 q->in_flight[rq_is_sync(rq)]++;
Divyesh Shah91952912010-04-01 15:01:41 -07002379 set_io_start_time_ns(rq);
2380 }
Tejun Heo158dbda2009-04-23 11:05:18 +09002381}
2382
Tejun Heo5efccd12009-04-23 11:05:18 +09002383/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09002384 * blk_start_request - start request processing on the driver
2385 * @req: request to dequeue
2386 *
2387 * Description:
2388 * Dequeue @req and start timeout timer on it. This hands off the
2389 * request to the driver.
2390 *
2391 * Block internal functions which don't want to start timer should
2392 * call blk_dequeue_request().
2393 *
2394 * Context:
2395 * queue_lock must be held.
2396 */
2397void blk_start_request(struct request *req)
2398{
2399 blk_dequeue_request(req);
2400
2401 /*
Tejun Heo5f49f632009-05-19 18:33:05 +09002402 * We are now handing the request to the hardware, initialize
2403 * resid_len to full count and add the timeout handler.
Tejun Heo9934c8c2009-05-08 11:54:16 +09002404 */
Tejun Heo5f49f632009-05-19 18:33:05 +09002405 req->resid_len = blk_rq_bytes(req);
FUJITA Tomonoridbb66c42009-06-09 05:47:10 +02002406 if (unlikely(blk_bidi_rq(req)))
2407 req->next_rq->resid_len = blk_rq_bytes(req->next_rq);
2408
Jeff Moyer4912aa62013-10-08 14:36:41 -04002409 BUG_ON(test_bit(REQ_ATOM_COMPLETE, &req->atomic_flags));
Tejun Heo9934c8c2009-05-08 11:54:16 +09002410 blk_add_timer(req);
2411}
2412EXPORT_SYMBOL(blk_start_request);
2413
2414/**
2415 * blk_fetch_request - fetch a request from a request queue
2416 * @q: request queue to fetch a request from
2417 *
2418 * Description:
2419 * Return the request at the top of @q. The request is started on
2420 * return and LLD can start processing it immediately.
2421 *
2422 * Return:
2423 * Pointer to the request at the top of @q if available. Null
2424 * otherwise.
2425 *
2426 * Context:
2427 * queue_lock must be held.
2428 */
2429struct request *blk_fetch_request(struct request_queue *q)
2430{
2431 struct request *rq;
2432
2433 rq = blk_peek_request(q);
2434 if (rq)
2435 blk_start_request(rq);
2436 return rq;
2437}
2438EXPORT_SYMBOL(blk_fetch_request);
2439
2440/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002441 * blk_update_request - Special helper function for request stacking drivers
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002442 * @req: the request being processed
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002443 * @error: %0 for success, < %0 for error
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002444 * @nr_bytes: number of bytes to complete @req
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002445 *
2446 * Description:
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002447 * Ends I/O on a number of bytes attached to @req, but doesn't complete
2448 * the request structure even if @req doesn't have leftover.
2449 * If @req has leftover, sets it up for the next range of segments.
Tejun Heo2e60e022009-04-23 11:05:18 +09002450 *
2451 * This special helper function is only for request stacking drivers
2452 * (e.g. request-based dm) so that they can handle partial completion.
2453 * Actual device drivers should use blk_end_request instead.
2454 *
2455 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
2456 * %false return from this function.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002457 *
2458 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002459 * %false - this request doesn't have any more data
2460 * %true - this request has more data
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002461 **/
Tejun Heo2e60e022009-04-23 11:05:18 +09002462bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463{
Kent Overstreetf79ea412012-09-20 16:38:30 -07002464 int total_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
Hannes Reinecke4a0efdc2014-10-01 14:32:31 +02002466 trace_block_rq_complete(req->q, req, nr_bytes);
2467
Tejun Heo2e60e022009-04-23 11:05:18 +09002468 if (!req->bio)
2469 return false;
2470
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 /*
Tejun Heo6f414692009-04-19 07:00:41 +09002472 * For fs requests, rq is just carrier of independent bio's
2473 * and each partial completion should be handled separately.
2474 * Reset per-request error on each partial completion.
2475 *
2476 * TODO: tj: This is too subtle. It would be better to let
2477 * low level drivers do what they see fit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002479 if (req->cmd_type == REQ_TYPE_FS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 req->errors = 0;
2481
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002482 if (error && req->cmd_type == REQ_TYPE_FS &&
2483 !(req->cmd_flags & REQ_QUIET)) {
Hannes Reinecke79775562011-01-18 10:13:13 +01002484 char *error_type;
2485
2486 switch (error) {
2487 case -ENOLINK:
2488 error_type = "recoverable transport";
2489 break;
2490 case -EREMOTEIO:
2491 error_type = "critical target";
2492 break;
2493 case -EBADE:
2494 error_type = "critical nexus";
2495 break;
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002496 case -ETIMEDOUT:
2497 error_type = "timeout";
2498 break;
Hannes Reineckea9d6ceb82013-07-01 15:16:25 +02002499 case -ENOSPC:
2500 error_type = "critical space allocation";
2501 break;
Hannes Reinecke7e782af2013-07-01 15:16:26 +02002502 case -ENODATA:
2503 error_type = "critical medium";
2504 break;
Hannes Reinecke79775562011-01-18 10:13:13 +01002505 case -EIO:
2506 default:
2507 error_type = "I/O";
2508 break;
2509 }
Robert Elliottef3ecb62014-08-27 10:50:31 -05002510 printk_ratelimited(KERN_ERR "%s: %s error, dev %s, sector %llu\n",
2511 __func__, error_type, req->rq_disk ?
Yi Zou37d7b342012-08-30 16:26:25 -07002512 req->rq_disk->disk_name : "?",
2513 (unsigned long long)blk_rq_pos(req));
2514
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 }
2516
Jens Axboebc58ba92009-01-23 10:54:44 +01002517 blk_account_io_completion(req, nr_bytes);
Jens Axboed72d9042005-11-01 08:35:42 +01002518
Kent Overstreetf79ea412012-09-20 16:38:30 -07002519 total_bytes = 0;
2520 while (req->bio) {
2521 struct bio *bio = req->bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002522 unsigned bio_bytes = min(bio->bi_iter.bi_size, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
Kent Overstreet4f024f32013-10-11 15:44:27 -07002524 if (bio_bytes == bio->bi_iter.bi_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 req->bio = bio->bi_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
Kent Overstreetf79ea412012-09-20 16:38:30 -07002527 req_bio_endio(req, bio, bio_bytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
Kent Overstreetf79ea412012-09-20 16:38:30 -07002529 total_bytes += bio_bytes;
2530 nr_bytes -= bio_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
Kent Overstreetf79ea412012-09-20 16:38:30 -07002532 if (!nr_bytes)
2533 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 }
2535
2536 /*
2537 * completely done
2538 */
Tejun Heo2e60e022009-04-23 11:05:18 +09002539 if (!req->bio) {
2540 /*
2541 * Reset counters so that the request stacking driver
2542 * can find how many bytes remain in the request
2543 * later.
2544 */
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002545 req->__data_len = 0;
Tejun Heo2e60e022009-04-23 11:05:18 +09002546 return false;
2547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002549 req->__data_len -= total_bytes;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002550
2551 /* update sector only for requests with clear definition of sector */
Martin K. Petersene2a60da2012-09-18 12:19:25 -04002552 if (req->cmd_type == REQ_TYPE_FS)
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002553 req->__sector += total_bytes >> 9;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002554
Tejun Heo80a761f2009-07-03 17:48:17 +09002555 /* mixed attributes always follow the first bio */
2556 if (req->cmd_flags & REQ_MIXED_MERGE) {
2557 req->cmd_flags &= ~REQ_FAILFAST_MASK;
2558 req->cmd_flags |= req->bio->bi_rw & REQ_FAILFAST_MASK;
2559 }
2560
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002561 /*
2562 * If total number of sectors is less than the first segment
2563 * size, something has gone terribly wrong.
2564 */
2565 if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
Jens Axboe81829242011-03-30 09:51:33 +02002566 blk_dump_rq_flags(req, "request botched");
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002567 req->__data_len = blk_rq_cur_bytes(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002568 }
2569
2570 /* recalculate the number of segments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 blk_recalc_rq_segments(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002572
Tejun Heo2e60e022009-04-23 11:05:18 +09002573 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574}
Tejun Heo2e60e022009-04-23 11:05:18 +09002575EXPORT_SYMBOL_GPL(blk_update_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
Tejun Heo2e60e022009-04-23 11:05:18 +09002577static bool blk_update_bidi_request(struct request *rq, int error,
2578 unsigned int nr_bytes,
2579 unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002580{
Tejun Heo2e60e022009-04-23 11:05:18 +09002581 if (blk_update_request(rq, error, nr_bytes))
2582 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002583
Tejun Heo2e60e022009-04-23 11:05:18 +09002584 /* Bidi request must be completed as a whole */
2585 if (unlikely(blk_bidi_rq(rq)) &&
2586 blk_update_request(rq->next_rq, error, bidi_bytes))
2587 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002588
Jens Axboee2e1a142010-06-09 10:42:09 +02002589 if (blk_queue_add_random(rq->q))
2590 add_disk_randomness(rq->rq_disk);
Tejun Heo2e60e022009-04-23 11:05:18 +09002591
2592 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593}
2594
James Bottomley28018c22010-07-01 19:49:17 +09002595/**
2596 * blk_unprep_request - unprepare a request
2597 * @req: the request
2598 *
2599 * This function makes a request ready for complete resubmission (or
2600 * completion). It happens only after all error handling is complete,
2601 * so represents the appropriate moment to deallocate any resources
2602 * that were allocated to the request in the prep_rq_fn. The queue
2603 * lock is held when calling this.
2604 */
2605void blk_unprep_request(struct request *req)
2606{
2607 struct request_queue *q = req->q;
2608
2609 req->cmd_flags &= ~REQ_DONTPREP;
2610 if (q->unprep_rq_fn)
2611 q->unprep_rq_fn(q, req);
2612}
2613EXPORT_SYMBOL_GPL(blk_unprep_request);
2614
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615/*
2616 * queue lock must be held
2617 */
Christoph Hellwig12120072014-04-16 09:44:59 +02002618void blk_finish_request(struct request *req, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619{
Christoph Hellwig125c99b2014-11-03 12:47:47 +01002620 if (req->cmd_flags & REQ_QUEUED)
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002621 blk_queue_end_tag(req->q, req);
2622
James Bottomleyba396a62009-05-27 14:17:08 +02002623 BUG_ON(blk_queued_rq(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002625 if (unlikely(laptop_mode) && req->cmd_type == REQ_TYPE_FS)
Matthew Garrett31373d02010-04-06 14:25:14 +02002626 laptop_io_completion(&req->q->backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
Mike Andersone78042e2008-10-30 02:16:20 -07002628 blk_delete_timer(req);
2629
James Bottomley28018c22010-07-01 19:49:17 +09002630 if (req->cmd_flags & REQ_DONTPREP)
2631 blk_unprep_request(req);
2632
Jens Axboebc58ba92009-01-23 10:54:44 +01002633 blk_account_io_done(req);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002634
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 if (req->end_io)
Tejun Heo8ffdc652006-01-06 09:49:03 +01002636 req->end_io(req, error);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002637 else {
2638 if (blk_bidi_rq(req))
2639 __blk_put_request(req->next_rq->q, req->next_rq);
2640
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 __blk_put_request(req->q, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 }
2643}
Christoph Hellwig12120072014-04-16 09:44:59 +02002644EXPORT_SYMBOL(blk_finish_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05002646/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002647 * blk_end_bidi_request - Complete a bidi request
2648 * @rq: the request to complete
Randy Dunlap710027a2008-08-19 20:13:11 +02002649 * @error: %0 for success, < %0 for error
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002650 * @nr_bytes: number of bytes to complete @rq
2651 * @bidi_bytes: number of bytes to complete @rq->next_rq
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002652 *
2653 * Description:
2654 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
Tejun Heo2e60e022009-04-23 11:05:18 +09002655 * Drivers that supports bidi can safely call this member for any
2656 * type of request, bidi or uni. In the later case @bidi_bytes is
2657 * just ignored.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002658 *
2659 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002660 * %false - we are done with this request
2661 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002662 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002663static bool blk_end_bidi_request(struct request *rq, int error,
2664 unsigned int nr_bytes, unsigned int bidi_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002665{
2666 struct request_queue *q = rq->q;
Tejun Heo2e60e022009-04-23 11:05:18 +09002667 unsigned long flags;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002668
Tejun Heo2e60e022009-04-23 11:05:18 +09002669 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2670 return true;
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002671
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002672 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo2e60e022009-04-23 11:05:18 +09002673 blk_finish_request(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002674 spin_unlock_irqrestore(q->queue_lock, flags);
2675
Tejun Heo2e60e022009-04-23 11:05:18 +09002676 return false;
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002677}
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002678
2679/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002680 * __blk_end_bidi_request - Complete a bidi request with queue lock held
2681 * @rq: the request to complete
2682 * @error: %0 for success, < %0 for error
2683 * @nr_bytes: number of bytes to complete @rq
2684 * @bidi_bytes: number of bytes to complete @rq->next_rq
Tejun Heo5efccd12009-04-23 11:05:18 +09002685 *
2686 * Description:
Tejun Heo2e60e022009-04-23 11:05:18 +09002687 * Identical to blk_end_bidi_request() except that queue lock is
2688 * assumed to be locked on entry and remains so on return.
Tejun Heo5efccd12009-04-23 11:05:18 +09002689 *
Tejun Heo2e60e022009-04-23 11:05:18 +09002690 * Return:
2691 * %false - we are done with this request
2692 * %true - still buffers pending for this request
Tejun Heo5efccd12009-04-23 11:05:18 +09002693 **/
Jeff Moyer4853aba2011-08-15 21:37:25 +02002694bool __blk_end_bidi_request(struct request *rq, int error,
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002695 unsigned int nr_bytes, unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002696{
Tejun Heo2e60e022009-04-23 11:05:18 +09002697 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2698 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002699
Tejun Heo2e60e022009-04-23 11:05:18 +09002700 blk_finish_request(rq, error);
Tejun Heo5efccd12009-04-23 11:05:18 +09002701
Tejun Heo2e60e022009-04-23 11:05:18 +09002702 return false;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002703}
2704
2705/**
2706 * blk_end_request - Helper function for drivers to complete the request.
2707 * @rq: the request being processed
2708 * @error: %0 for success, < %0 for error
2709 * @nr_bytes: number of bytes to complete
2710 *
2711 * Description:
2712 * Ends I/O on a number of bytes attached to @rq.
2713 * If @rq has leftover, sets it up for the next range of segments.
2714 *
2715 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002716 * %false - we are done with this request
2717 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002718 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002719bool blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002720{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002721 return blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002722}
Jens Axboe56ad1742009-07-28 22:11:24 +02002723EXPORT_SYMBOL(blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002724
2725/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002726 * blk_end_request_all - Helper function for drives to finish the request.
2727 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002728 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002729 *
2730 * Description:
2731 * Completely finish @rq.
2732 */
2733void blk_end_request_all(struct request *rq, int error)
2734{
2735 bool pending;
2736 unsigned int bidi_bytes = 0;
2737
2738 if (unlikely(blk_bidi_rq(rq)))
2739 bidi_bytes = blk_rq_bytes(rq->next_rq);
2740
2741 pending = blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2742 BUG_ON(pending);
2743}
Jens Axboe56ad1742009-07-28 22:11:24 +02002744EXPORT_SYMBOL(blk_end_request_all);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002745
2746/**
2747 * blk_end_request_cur - Helper function to finish the current request chunk.
2748 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002749 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002750 *
2751 * Description:
2752 * Complete the current consecutively mapped chunk from @rq.
2753 *
2754 * Return:
2755 * %false - we are done with this request
2756 * %true - still buffers pending for this request
2757 */
2758bool blk_end_request_cur(struct request *rq, int error)
2759{
2760 return blk_end_request(rq, error, blk_rq_cur_bytes(rq));
2761}
Jens Axboe56ad1742009-07-28 22:11:24 +02002762EXPORT_SYMBOL(blk_end_request_cur);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002763
2764/**
Tejun Heo80a761f2009-07-03 17:48:17 +09002765 * blk_end_request_err - Finish a request till the next failure boundary.
2766 * @rq: the request to finish till the next failure boundary for
2767 * @error: must be negative errno
2768 *
2769 * Description:
2770 * Complete @rq till the next failure boundary.
2771 *
2772 * Return:
2773 * %false - we are done with this request
2774 * %true - still buffers pending for this request
2775 */
2776bool blk_end_request_err(struct request *rq, int error)
2777{
2778 WARN_ON(error >= 0);
2779 return blk_end_request(rq, error, blk_rq_err_bytes(rq));
2780}
2781EXPORT_SYMBOL_GPL(blk_end_request_err);
2782
2783/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002784 * __blk_end_request - Helper function for drivers to complete the request.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002785 * @rq: the request being processed
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002786 * @error: %0 for success, < %0 for error
2787 * @nr_bytes: number of bytes to complete
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002788 *
2789 * Description:
2790 * Must be called with queue lock held unlike blk_end_request().
2791 *
2792 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002793 * %false - we are done with this request
2794 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002795 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002796bool __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002797{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002798 return __blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002799}
Jens Axboe56ad1742009-07-28 22:11:24 +02002800EXPORT_SYMBOL(__blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002801
2802/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002803 * __blk_end_request_all - Helper function for drives to finish the request.
2804 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002805 * @error: %0 for success, < %0 for error
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002806 *
2807 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002808 * Completely finish @rq. Must be called with queue lock held.
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002809 */
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002810void __blk_end_request_all(struct request *rq, int error)
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002811{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002812 bool pending;
2813 unsigned int bidi_bytes = 0;
2814
2815 if (unlikely(blk_bidi_rq(rq)))
2816 bidi_bytes = blk_rq_bytes(rq->next_rq);
2817
2818 pending = __blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2819 BUG_ON(pending);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002820}
Jens Axboe56ad1742009-07-28 22:11:24 +02002821EXPORT_SYMBOL(__blk_end_request_all);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002822
2823/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002824 * __blk_end_request_cur - Helper function to finish the current request chunk.
2825 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002826 * @error: %0 for success, < %0 for error
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002827 *
2828 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002829 * Complete the current consecutively mapped chunk from @rq. Must
2830 * be called with queue lock held.
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002831 *
2832 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002833 * %false - we are done with this request
2834 * %true - still buffers pending for this request
2835 */
2836bool __blk_end_request_cur(struct request *rq, int error)
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002837{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002838 return __blk_end_request(rq, error, blk_rq_cur_bytes(rq));
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002839}
Jens Axboe56ad1742009-07-28 22:11:24 +02002840EXPORT_SYMBOL(__blk_end_request_cur);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002841
Tejun Heo80a761f2009-07-03 17:48:17 +09002842/**
2843 * __blk_end_request_err - Finish a request till the next failure boundary.
2844 * @rq: the request to finish till the next failure boundary for
2845 * @error: must be negative errno
2846 *
2847 * Description:
2848 * Complete @rq till the next failure boundary. Must be called
2849 * with queue lock held.
2850 *
2851 * Return:
2852 * %false - we are done with this request
2853 * %true - still buffers pending for this request
2854 */
2855bool __blk_end_request_err(struct request *rq, int error)
2856{
2857 WARN_ON(error >= 0);
2858 return __blk_end_request(rq, error, blk_rq_err_bytes(rq));
2859}
2860EXPORT_SYMBOL_GPL(__blk_end_request_err);
2861
Jens Axboe86db1e22008-01-29 14:53:40 +01002862void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2863 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864{
Tejun Heoa82afdf2009-07-03 17:48:16 +09002865 /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002866 rq->cmd_flags |= bio->bi_rw & REQ_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
Jens Axboeb4f42e22014-04-10 09:46:28 -06002868 if (bio_has_data(bio))
David Woodhousefb2dce82008-08-05 18:01:53 +01002869 rq->nr_phys_segments = bio_phys_segments(q, bio);
Jens Axboeb4f42e22014-04-10 09:46:28 -06002870
Kent Overstreet4f024f32013-10-11 15:44:27 -07002871 rq->__data_len = bio->bi_iter.bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 rq->bio = rq->biotail = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
NeilBrown66846572007-08-16 13:31:28 +02002874 if (bio->bi_bdev)
2875 rq->rq_disk = bio->bi_bdev->bd_disk;
2876}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
Ilya Loginov2d4dc892009-11-26 09:16:19 +01002878#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
2879/**
2880 * rq_flush_dcache_pages - Helper function to flush all pages in a request
2881 * @rq: the request to be flushed
2882 *
2883 * Description:
2884 * Flush all pages in @rq.
2885 */
2886void rq_flush_dcache_pages(struct request *rq)
2887{
2888 struct req_iterator iter;
Kent Overstreet79886132013-11-23 17:19:00 -08002889 struct bio_vec bvec;
Ilya Loginov2d4dc892009-11-26 09:16:19 +01002890
2891 rq_for_each_segment(bvec, rq, iter)
Kent Overstreet79886132013-11-23 17:19:00 -08002892 flush_dcache_page(bvec.bv_page);
Ilya Loginov2d4dc892009-11-26 09:16:19 +01002893}
2894EXPORT_SYMBOL_GPL(rq_flush_dcache_pages);
2895#endif
2896
Kiyoshi Uedaef9e3fa2008-10-01 16:12:15 +02002897/**
2898 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
2899 * @q : the queue of the device being checked
2900 *
2901 * Description:
2902 * Check if underlying low-level drivers of a device are busy.
2903 * If the drivers want to export their busy state, they must set own
2904 * exporting function using blk_queue_lld_busy() first.
2905 *
2906 * Basically, this function is used only by request stacking drivers
2907 * to stop dispatching requests to underlying devices when underlying
2908 * devices are busy. This behavior helps more I/O merging on the queue
2909 * of the request stacking driver and prevents I/O throughput regression
2910 * on burst I/O load.
2911 *
2912 * Return:
2913 * 0 - Not busy (The request stacking driver should dispatch request)
2914 * 1 - Busy (The request stacking driver should stop dispatching request)
2915 */
2916int blk_lld_busy(struct request_queue *q)
2917{
2918 if (q->lld_busy_fn)
2919 return q->lld_busy_fn(q);
2920
2921 return 0;
2922}
2923EXPORT_SYMBOL_GPL(blk_lld_busy);
2924
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002925/**
2926 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
2927 * @rq: the clone request to be cleaned up
2928 *
2929 * Description:
2930 * Free all bios in @rq for a cloned request.
2931 */
2932void blk_rq_unprep_clone(struct request *rq)
2933{
2934 struct bio *bio;
2935
2936 while ((bio = rq->bio) != NULL) {
2937 rq->bio = bio->bi_next;
2938
2939 bio_put(bio);
2940 }
2941}
2942EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
2943
2944/*
2945 * Copy attributes of the original request to the clone request.
Jens Axboeb4f42e22014-04-10 09:46:28 -06002946 * The actual data parts (e.g. ->cmd, ->sense) are not copied.
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002947 */
2948static void __blk_rq_prep_clone(struct request *dst, struct request *src)
2949{
2950 dst->cpu = src->cpu;
Keith Busch77a08682015-01-12 15:04:06 -05002951 dst->cmd_flags |= (src->cmd_flags & REQ_CLONE_MASK) | REQ_NOMERGE;
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002952 dst->cmd_type = src->cmd_type;
2953 dst->__sector = blk_rq_pos(src);
2954 dst->__data_len = blk_rq_bytes(src);
2955 dst->nr_phys_segments = src->nr_phys_segments;
2956 dst->ioprio = src->ioprio;
2957 dst->extra_len = src->extra_len;
2958}
2959
2960/**
2961 * blk_rq_prep_clone - Helper function to setup clone request
2962 * @rq: the request to be setup
2963 * @rq_src: original request to be cloned
2964 * @bs: bio_set that bios for clone are allocated from
2965 * @gfp_mask: memory allocation mask for bio
2966 * @bio_ctr: setup function to be called for each clone bio.
2967 * Returns %0 for success, non %0 for failure.
2968 * @data: private data to be passed to @bio_ctr
2969 *
2970 * Description:
2971 * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
Jens Axboeb4f42e22014-04-10 09:46:28 -06002972 * The actual data parts of @rq_src (e.g. ->cmd, ->sense)
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002973 * are not copied, and copying such parts is the caller's responsibility.
2974 * Also, pages which the original bios are pointing to are not copied
2975 * and the cloned bios just point same pages.
2976 * So cloned bios must be completed before original bios, which means
2977 * the caller must complete @rq before @rq_src.
2978 */
2979int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
2980 struct bio_set *bs, gfp_t gfp_mask,
2981 int (*bio_ctr)(struct bio *, struct bio *, void *),
2982 void *data)
2983{
2984 struct bio *bio, *bio_src;
2985
2986 if (!bs)
2987 bs = fs_bio_set;
2988
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002989 __rq_for_each_bio(bio_src, rq_src) {
Junichi Nomura11dfce52014-10-03 17:27:11 -04002990 bio = bio_clone_fast(bio_src, gfp_mask, bs);
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002991 if (!bio)
2992 goto free_and_out;
2993
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002994 if (bio_ctr && bio_ctr(bio, bio_src, data))
2995 goto free_and_out;
2996
2997 if (rq->bio) {
2998 rq->biotail->bi_next = bio;
2999 rq->biotail = bio;
3000 } else
3001 rq->bio = rq->biotail = bio;
3002 }
3003
3004 __blk_rq_prep_clone(rq, rq_src);
3005
3006 return 0;
3007
3008free_and_out:
3009 if (bio)
Kent Overstreet4254bba2012-09-06 15:35:00 -07003010 bio_put(bio);
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02003011 blk_rq_unprep_clone(rq);
3012
3013 return -ENOMEM;
3014}
3015EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
3016
Jens Axboe59c3d452014-04-08 09:15:35 -06003017int kblockd_schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018{
3019 return queue_work(kblockd_workqueue, work);
3020}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021EXPORT_SYMBOL(kblockd_schedule_work);
3022
Jens Axboe59c3d452014-04-08 09:15:35 -06003023int kblockd_schedule_delayed_work(struct delayed_work *dwork,
3024 unsigned long delay)
Vivek Goyale43473b2010-09-15 17:06:35 -04003025{
3026 return queue_delayed_work(kblockd_workqueue, dwork, delay);
3027}
3028EXPORT_SYMBOL(kblockd_schedule_delayed_work);
3029
Jens Axboe8ab14592014-04-08 09:17:40 -06003030int kblockd_schedule_delayed_work_on(int cpu, struct delayed_work *dwork,
3031 unsigned long delay)
3032{
3033 return queue_delayed_work_on(cpu, kblockd_workqueue, dwork, delay);
3034}
3035EXPORT_SYMBOL(kblockd_schedule_delayed_work_on);
3036
Suresh Jayaraman75df7132011-09-21 10:00:16 +02003037/**
3038 * blk_start_plug - initialize blk_plug and track it inside the task_struct
3039 * @plug: The &struct blk_plug that needs to be initialized
3040 *
3041 * Description:
3042 * Tracking blk_plug inside the task_struct will help with auto-flushing the
3043 * pending I/O should the task end up blocking between blk_start_plug() and
3044 * blk_finish_plug(). This is important from a performance perspective, but
3045 * also ensures that we don't deadlock. For instance, if the task is blocking
3046 * for a memory allocation, memory reclaim could end up wanting to free a
3047 * page belonging to that request that is currently residing in our private
3048 * plug. By flushing the pending I/O when the process goes to sleep, we avoid
3049 * this kind of deadlock.
3050 */
Jens Axboe73c10102011-03-08 13:19:51 +01003051void blk_start_plug(struct blk_plug *plug)
3052{
3053 struct task_struct *tsk = current;
3054
Jens Axboe73c10102011-03-08 13:19:51 +01003055 INIT_LIST_HEAD(&plug->list);
Jens Axboe320ae512013-10-24 09:20:05 +01003056 INIT_LIST_HEAD(&plug->mq_list);
NeilBrown048c9372011-04-18 09:52:22 +02003057 INIT_LIST_HEAD(&plug->cb_list);
Jens Axboe73c10102011-03-08 13:19:51 +01003058
3059 /*
3060 * If this is a nested plug, don't actually assign it. It will be
3061 * flushed on its own.
3062 */
3063 if (!tsk->plug) {
3064 /*
3065 * Store ordering should not be needed here, since a potential
3066 * preempt will imply a full memory barrier
3067 */
3068 tsk->plug = plug;
3069 }
3070}
3071EXPORT_SYMBOL(blk_start_plug);
3072
3073static int plug_rq_cmp(void *priv, struct list_head *a, struct list_head *b)
3074{
3075 struct request *rqa = container_of(a, struct request, queuelist);
3076 struct request *rqb = container_of(b, struct request, queuelist);
3077
Jianpeng Ma975927b2012-10-25 21:58:17 +02003078 return !(rqa->q < rqb->q ||
3079 (rqa->q == rqb->q && blk_rq_pos(rqa) < blk_rq_pos(rqb)));
Jens Axboe73c10102011-03-08 13:19:51 +01003080}
3081
Jens Axboe49cac012011-04-16 13:51:05 +02003082/*
3083 * If 'from_schedule' is true, then postpone the dispatch of requests
3084 * until a safe kblockd context. We due this to avoid accidental big
3085 * additional stack usage in driver dispatch, in places where the originally
3086 * plugger did not intend it.
3087 */
Jens Axboef6603782011-04-15 15:49:07 +02003088static void queue_unplugged(struct request_queue *q, unsigned int depth,
Jens Axboe49cac012011-04-16 13:51:05 +02003089 bool from_schedule)
Jens Axboe99e22592011-04-18 09:59:55 +02003090 __releases(q->queue_lock)
Jens Axboe94b5eb22011-04-12 10:12:19 +02003091{
Jens Axboe49cac012011-04-16 13:51:05 +02003092 trace_block_unplug(q, depth, !from_schedule);
Jens Axboe99e22592011-04-18 09:59:55 +02003093
Bart Van Assche70460572012-11-28 13:45:56 +01003094 if (from_schedule)
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003095 blk_run_queue_async(q);
Bart Van Assche70460572012-11-28 13:45:56 +01003096 else
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003097 __blk_run_queue(q);
Bart Van Assche70460572012-11-28 13:45:56 +01003098 spin_unlock(q->queue_lock);
Jens Axboe94b5eb22011-04-12 10:12:19 +02003099}
3100
NeilBrown74018dc2012-07-31 09:08:15 +02003101static void flush_plug_callbacks(struct blk_plug *plug, bool from_schedule)
NeilBrown048c9372011-04-18 09:52:22 +02003102{
3103 LIST_HEAD(callbacks);
3104
Shaohua Li2a7d5552012-07-31 09:08:15 +02003105 while (!list_empty(&plug->cb_list)) {
3106 list_splice_init(&plug->cb_list, &callbacks);
NeilBrown048c9372011-04-18 09:52:22 +02003107
Shaohua Li2a7d5552012-07-31 09:08:15 +02003108 while (!list_empty(&callbacks)) {
3109 struct blk_plug_cb *cb = list_first_entry(&callbacks,
NeilBrown048c9372011-04-18 09:52:22 +02003110 struct blk_plug_cb,
3111 list);
Shaohua Li2a7d5552012-07-31 09:08:15 +02003112 list_del(&cb->list);
NeilBrown74018dc2012-07-31 09:08:15 +02003113 cb->callback(cb, from_schedule);
Shaohua Li2a7d5552012-07-31 09:08:15 +02003114 }
NeilBrown048c9372011-04-18 09:52:22 +02003115 }
3116}
3117
NeilBrown9cbb1752012-07-31 09:08:14 +02003118struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug, void *data,
3119 int size)
3120{
3121 struct blk_plug *plug = current->plug;
3122 struct blk_plug_cb *cb;
3123
3124 if (!plug)
3125 return NULL;
3126
3127 list_for_each_entry(cb, &plug->cb_list, list)
3128 if (cb->callback == unplug && cb->data == data)
3129 return cb;
3130
3131 /* Not currently on the callback list */
3132 BUG_ON(size < sizeof(*cb));
3133 cb = kzalloc(size, GFP_ATOMIC);
3134 if (cb) {
3135 cb->data = data;
3136 cb->callback = unplug;
3137 list_add(&cb->list, &plug->cb_list);
3138 }
3139 return cb;
3140}
3141EXPORT_SYMBOL(blk_check_plugged);
3142
Jens Axboe49cac012011-04-16 13:51:05 +02003143void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
Jens Axboe73c10102011-03-08 13:19:51 +01003144{
3145 struct request_queue *q;
3146 unsigned long flags;
3147 struct request *rq;
NeilBrown109b8122011-04-11 14:13:10 +02003148 LIST_HEAD(list);
Jens Axboe94b5eb22011-04-12 10:12:19 +02003149 unsigned int depth;
Jens Axboe73c10102011-03-08 13:19:51 +01003150
NeilBrown74018dc2012-07-31 09:08:15 +02003151 flush_plug_callbacks(plug, from_schedule);
Jens Axboe320ae512013-10-24 09:20:05 +01003152
3153 if (!list_empty(&plug->mq_list))
3154 blk_mq_flush_plug_list(plug, from_schedule);
3155
Jens Axboe73c10102011-03-08 13:19:51 +01003156 if (list_empty(&plug->list))
3157 return;
3158
NeilBrown109b8122011-04-11 14:13:10 +02003159 list_splice_init(&plug->list, &list);
3160
Jianpeng Ma422765c2013-01-11 14:46:09 +01003161 list_sort(NULL, &list, plug_rq_cmp);
Jens Axboe73c10102011-03-08 13:19:51 +01003162
3163 q = NULL;
Jens Axboe94b5eb22011-04-12 10:12:19 +02003164 depth = 0;
Jens Axboe18811272011-04-12 10:11:24 +02003165
3166 /*
3167 * Save and disable interrupts here, to avoid doing it for every
3168 * queue lock we have to take.
3169 */
Jens Axboe73c10102011-03-08 13:19:51 +01003170 local_irq_save(flags);
NeilBrown109b8122011-04-11 14:13:10 +02003171 while (!list_empty(&list)) {
3172 rq = list_entry_rq(list.next);
Jens Axboe73c10102011-03-08 13:19:51 +01003173 list_del_init(&rq->queuelist);
Jens Axboe73c10102011-03-08 13:19:51 +01003174 BUG_ON(!rq->q);
3175 if (rq->q != q) {
Jens Axboe99e22592011-04-18 09:59:55 +02003176 /*
3177 * This drops the queue lock
3178 */
3179 if (q)
Jens Axboe49cac012011-04-16 13:51:05 +02003180 queue_unplugged(q, depth, from_schedule);
Jens Axboe73c10102011-03-08 13:19:51 +01003181 q = rq->q;
Jens Axboe94b5eb22011-04-12 10:12:19 +02003182 depth = 0;
Jens Axboe73c10102011-03-08 13:19:51 +01003183 spin_lock(q->queue_lock);
3184 }
Tejun Heo8ba61432011-12-14 00:33:37 +01003185
3186 /*
3187 * Short-circuit if @q is dead
3188 */
Bart Van Assche3f3299d2012-11-28 13:42:38 +01003189 if (unlikely(blk_queue_dying(q))) {
Tejun Heo8ba61432011-12-14 00:33:37 +01003190 __blk_end_request_all(rq, -ENODEV);
3191 continue;
3192 }
3193
Jens Axboe73c10102011-03-08 13:19:51 +01003194 /*
3195 * rq is already accounted, so use raw insert
3196 */
Jens Axboe401a18e2011-03-25 16:57:52 +01003197 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA))
3198 __elv_add_request(q, rq, ELEVATOR_INSERT_FLUSH);
3199 else
3200 __elv_add_request(q, rq, ELEVATOR_INSERT_SORT_MERGE);
Jens Axboe94b5eb22011-04-12 10:12:19 +02003201
3202 depth++;
Jens Axboe73c10102011-03-08 13:19:51 +01003203 }
3204
Jens Axboe99e22592011-04-18 09:59:55 +02003205 /*
3206 * This drops the queue lock
3207 */
3208 if (q)
Jens Axboe49cac012011-04-16 13:51:05 +02003209 queue_unplugged(q, depth, from_schedule);
Jens Axboe73c10102011-03-08 13:19:51 +01003210
Jens Axboe73c10102011-03-08 13:19:51 +01003211 local_irq_restore(flags);
3212}
Jens Axboe73c10102011-03-08 13:19:51 +01003213
3214void blk_finish_plug(struct blk_plug *plug)
3215{
Jens Axboef6603782011-04-15 15:49:07 +02003216 blk_flush_plug_list(plug, false);
Christoph Hellwig88b996c2011-04-15 15:20:10 +02003217
3218 if (plug == current->plug)
3219 current->plug = NULL;
Jens Axboe73c10102011-03-08 13:19:51 +01003220}
3221EXPORT_SYMBOL(blk_finish_plug);
3222
Rafael J. Wysocki47fafbc2014-12-04 01:00:23 +01003223#ifdef CONFIG_PM
Lin Ming6c954662013-03-23 11:42:26 +08003224/**
3225 * blk_pm_runtime_init - Block layer runtime PM initialization routine
3226 * @q: the queue of the device
3227 * @dev: the device the queue belongs to
3228 *
3229 * Description:
3230 * Initialize runtime-PM-related fields for @q and start auto suspend for
3231 * @dev. Drivers that want to take advantage of request-based runtime PM
3232 * should call this function after @dev has been initialized, and its
3233 * request queue @q has been allocated, and runtime PM for it can not happen
3234 * yet(either due to disabled/forbidden or its usage_count > 0). In most
3235 * cases, driver should call this function before any I/O has taken place.
3236 *
3237 * This function takes care of setting up using auto suspend for the device,
3238 * the autosuspend delay is set to -1 to make runtime suspend impossible
3239 * until an updated value is either set by user or by driver. Drivers do
3240 * not need to touch other autosuspend settings.
3241 *
3242 * The block layer runtime PM is request based, so only works for drivers
3243 * that use request as their IO unit instead of those directly use bio's.
3244 */
3245void blk_pm_runtime_init(struct request_queue *q, struct device *dev)
3246{
3247 q->dev = dev;
3248 q->rpm_status = RPM_ACTIVE;
3249 pm_runtime_set_autosuspend_delay(q->dev, -1);
3250 pm_runtime_use_autosuspend(q->dev);
3251}
3252EXPORT_SYMBOL(blk_pm_runtime_init);
3253
3254/**
3255 * blk_pre_runtime_suspend - Pre runtime suspend check
3256 * @q: the queue of the device
3257 *
3258 * Description:
3259 * This function will check if runtime suspend is allowed for the device
3260 * by examining if there are any requests pending in the queue. If there
3261 * are requests pending, the device can not be runtime suspended; otherwise,
3262 * the queue's status will be updated to SUSPENDING and the driver can
3263 * proceed to suspend the device.
3264 *
3265 * For the not allowed case, we mark last busy for the device so that
3266 * runtime PM core will try to autosuspend it some time later.
3267 *
3268 * This function should be called near the start of the device's
3269 * runtime_suspend callback.
3270 *
3271 * Return:
3272 * 0 - OK to runtime suspend the device
3273 * -EBUSY - Device should not be runtime suspended
3274 */
3275int blk_pre_runtime_suspend(struct request_queue *q)
3276{
3277 int ret = 0;
3278
3279 spin_lock_irq(q->queue_lock);
3280 if (q->nr_pending) {
3281 ret = -EBUSY;
3282 pm_runtime_mark_last_busy(q->dev);
3283 } else {
3284 q->rpm_status = RPM_SUSPENDING;
3285 }
3286 spin_unlock_irq(q->queue_lock);
3287 return ret;
3288}
3289EXPORT_SYMBOL(blk_pre_runtime_suspend);
3290
3291/**
3292 * blk_post_runtime_suspend - Post runtime suspend processing
3293 * @q: the queue of the device
3294 * @err: return value of the device's runtime_suspend function
3295 *
3296 * Description:
3297 * Update the queue's runtime status according to the return value of the
3298 * device's runtime suspend function and mark last busy for the device so
3299 * that PM core will try to auto suspend the device at a later time.
3300 *
3301 * This function should be called near the end of the device's
3302 * runtime_suspend callback.
3303 */
3304void blk_post_runtime_suspend(struct request_queue *q, int err)
3305{
3306 spin_lock_irq(q->queue_lock);
3307 if (!err) {
3308 q->rpm_status = RPM_SUSPENDED;
3309 } else {
3310 q->rpm_status = RPM_ACTIVE;
3311 pm_runtime_mark_last_busy(q->dev);
3312 }
3313 spin_unlock_irq(q->queue_lock);
3314}
3315EXPORT_SYMBOL(blk_post_runtime_suspend);
3316
3317/**
3318 * blk_pre_runtime_resume - Pre runtime resume processing
3319 * @q: the queue of the device
3320 *
3321 * Description:
3322 * Update the queue's runtime status to RESUMING in preparation for the
3323 * runtime resume of the device.
3324 *
3325 * This function should be called near the start of the device's
3326 * runtime_resume callback.
3327 */
3328void blk_pre_runtime_resume(struct request_queue *q)
3329{
3330 spin_lock_irq(q->queue_lock);
3331 q->rpm_status = RPM_RESUMING;
3332 spin_unlock_irq(q->queue_lock);
3333}
3334EXPORT_SYMBOL(blk_pre_runtime_resume);
3335
3336/**
3337 * blk_post_runtime_resume - Post runtime resume processing
3338 * @q: the queue of the device
3339 * @err: return value of the device's runtime_resume function
3340 *
3341 * Description:
3342 * Update the queue's runtime status according to the return value of the
3343 * device's runtime_resume function. If it is successfully resumed, process
3344 * the requests that are queued into the device's queue when it is resuming
3345 * and then mark last busy and initiate autosuspend for it.
3346 *
3347 * This function should be called near the end of the device's
3348 * runtime_resume callback.
3349 */
3350void blk_post_runtime_resume(struct request_queue *q, int err)
3351{
3352 spin_lock_irq(q->queue_lock);
3353 if (!err) {
3354 q->rpm_status = RPM_ACTIVE;
3355 __blk_run_queue(q);
3356 pm_runtime_mark_last_busy(q->dev);
Aaron Luc60855c2013-05-17 15:47:20 +08003357 pm_request_autosuspend(q->dev);
Lin Ming6c954662013-03-23 11:42:26 +08003358 } else {
3359 q->rpm_status = RPM_SUSPENDED;
3360 }
3361 spin_unlock_irq(q->queue_lock);
3362}
3363EXPORT_SYMBOL(blk_post_runtime_resume);
3364#endif
3365
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366int __init blk_dev_init(void)
3367{
Nikanth Karthikesan9eb55b02009-04-27 14:53:54 +02003368 BUILD_BUG_ON(__REQ_NR_BITS > 8 *
3369 sizeof(((struct request *)0)->cmd_flags));
3370
Tejun Heo89b90be2011-01-03 15:01:47 +01003371 /* used for unplugging and affects IO latency/throughput - HIGHPRI */
3372 kblockd_workqueue = alloc_workqueue("kblockd",
Matias Bjørling28747fc2014-06-11 23:43:54 +02003373 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 if (!kblockd_workqueue)
3375 panic("Failed to create kblockd\n");
3376
3377 request_cachep = kmem_cache_create("blkdev_requests",
Paul Mundt20c2df82007-07-20 10:11:58 +09003378 sizeof(struct request), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379
Jens Axboe8324aa92008-01-29 14:51:59 +01003380 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02003381 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 return 0;
3384}
Mohan Srinivasana2766062016-08-25 18:25:05 -07003385
3386/*
3387 * Blk IO latency support. We want this to be as cheap as possible, so doing
3388 * this lockless (and avoiding atomics), a few off by a few errors in this
3389 * code is not harmful, and we don't want to do anything that is
3390 * perf-impactful.
3391 * TODO : If necessary, we can make the histograms per-cpu and aggregate
3392 * them when printing them out.
3393 */
3394void
3395blk_zero_latency_hist(struct io_latency_state *s)
3396{
3397 memset(s->latency_y_axis_read, 0,
3398 sizeof(s->latency_y_axis_read));
3399 memset(s->latency_y_axis_write, 0,
3400 sizeof(s->latency_y_axis_write));
3401 s->latency_reads_elems = 0;
3402 s->latency_writes_elems = 0;
3403}
Mohan Srinivasan5d7691a2016-09-07 17:39:42 -07003404EXPORT_SYMBOL(blk_zero_latency_hist);
Mohan Srinivasana2766062016-08-25 18:25:05 -07003405
3406ssize_t
3407blk_latency_hist_show(struct io_latency_state *s, char *buf)
3408{
3409 int i;
3410 int bytes_written = 0;
3411 u_int64_t num_elem, elem;
3412 int pct;
3413
3414 num_elem = s->latency_reads_elems;
3415 if (num_elem > 0) {
3416 bytes_written += scnprintf(buf + bytes_written,
3417 PAGE_SIZE - bytes_written,
3418 "IO svc_time Read Latency Histogram (n = %llu):\n",
3419 num_elem);
3420 for (i = 0;
3421 i < ARRAY_SIZE(latency_x_axis_us);
3422 i++) {
3423 elem = s->latency_y_axis_read[i];
3424 pct = div64_u64(elem * 100, num_elem);
3425 bytes_written += scnprintf(buf + bytes_written,
3426 PAGE_SIZE - bytes_written,
3427 "\t< %5lluus%15llu%15d%%\n",
3428 latency_x_axis_us[i],
3429 elem, pct);
3430 }
3431 /* Last element in y-axis table is overflow */
3432 elem = s->latency_y_axis_read[i];
3433 pct = div64_u64(elem * 100, num_elem);
3434 bytes_written += scnprintf(buf + bytes_written,
3435 PAGE_SIZE - bytes_written,
3436 "\t> %5dms%15llu%15d%%\n", 10,
3437 elem, pct);
3438 }
3439 num_elem = s->latency_writes_elems;
3440 if (num_elem > 0) {
3441 bytes_written += scnprintf(buf + bytes_written,
3442 PAGE_SIZE - bytes_written,
3443 "IO svc_time Write Latency Histogram (n = %llu):\n",
3444 num_elem);
3445 for (i = 0;
3446 i < ARRAY_SIZE(latency_x_axis_us);
3447 i++) {
3448 elem = s->latency_y_axis_write[i];
3449 pct = div64_u64(elem * 100, num_elem);
3450 bytes_written += scnprintf(buf + bytes_written,
3451 PAGE_SIZE - bytes_written,
3452 "\t< %5lluus%15llu%15d%%\n",
3453 latency_x_axis_us[i],
3454 elem, pct);
3455 }
3456 /* Last element in y-axis table is overflow */
3457 elem = s->latency_y_axis_write[i];
3458 pct = div64_u64(elem * 100, num_elem);
3459 bytes_written += scnprintf(buf + bytes_written,
3460 PAGE_SIZE - bytes_written,
3461 "\t> %5dms%15llu%15d%%\n", 10,
3462 elem, pct);
3463 }
3464 return bytes_written;
3465}
Mohan Srinivasan5d7691a2016-09-07 17:39:42 -07003466EXPORT_SYMBOL(blk_latency_hist_show);