blob: 222fe84d6ac4b5bdae76b3d9bcb746a1a65717ca [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>
Li Zefan55782132009-06-09 13:43:05 +080035
36#define CREATE_TRACE_POINTS
37#include <trace/events/block.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Jens Axboe8324aa92008-01-29 14:51:59 +010039#include "blk.h"
Tejun Heo5efd6112012-03-05 13:15:12 -080040#include "blk-cgroup.h"
Ming Lei43a5e4e2013-12-26 21:31:35 +080041#include "blk-mq.h"
Jens Axboe8324aa92008-01-29 14:51:59 +010042
Mike Snitzerd07335e2010-11-16 12:52:38 +010043EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_remap);
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +020044EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_remap);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -070045EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_complete);
Keith Busch3291fa52014-04-28 12:30:52 -060046EXPORT_TRACEPOINT_SYMBOL_GPL(block_split);
NeilBrowncbae8d42012-12-14 20:49:27 +010047EXPORT_TRACEPOINT_SYMBOL_GPL(block_unplug);
Ingo Molnar0bfc2452008-11-26 11:59:56 +010048
Tejun Heoa73f7302011-12-14 00:33:37 +010049DEFINE_IDA(blk_queue_ida);
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
52 * For the allocated request tables
53 */
Jens Axboe320ae512013-10-24 09:20:05 +010054struct kmem_cache *request_cachep = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/*
57 * For queue allocation
58 */
Jens Axboe6728cb02008-01-31 13:03:55 +010059struct kmem_cache *blk_requestq_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 * Controlling structure to kblockd
63 */
Jens Axboeff856ba2006-01-09 16:02:34 +010064static struct workqueue_struct *kblockd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Jens Axboe8324aa92008-01-29 14:51:59 +010066void blk_queue_congestion_threshold(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
68 int nr;
69
70 nr = q->nr_requests - (q->nr_requests / 8) + 1;
71 if (nr > q->nr_requests)
72 nr = q->nr_requests;
73 q->nr_congestion_on = nr;
74
75 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
76 if (nr < 1)
77 nr = 1;
78 q->nr_congestion_off = nr;
79}
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/**
82 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
83 * @bdev: device
84 *
85 * Locates the passed device's request queue and returns the address of its
Tejun Heoff9ea322014-09-08 08:03:56 +090086 * backing_dev_info. This function can only be called if @bdev is opened
87 * and the return value is never NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 */
89struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
90{
Jens Axboe165125e2007-07-24 09:28:11 +020091 struct request_queue *q = bdev_get_queue(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Tejun Heoff9ea322014-09-08 08:03:56 +090093 return &q->backing_dev_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
Linus Torvalds1da177e2005-04-16 15:20:36 -070095EXPORT_SYMBOL(blk_get_backing_dev_info);
96
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +020097void blk_rq_init(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +020099 memset(rq, 0, sizeof(*rq));
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 INIT_LIST_HEAD(&rq->queuelist);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700102 INIT_LIST_HEAD(&rq->timeout_list);
Jens Axboec7c22e42008-09-13 20:26:01 +0200103 rq->cpu = -1;
Jens Axboe63a71382008-02-08 12:41:03 +0100104 rq->q = q;
Tejun Heoa2dec7b2009-05-07 22:24:44 +0900105 rq->__sector = (sector_t) -1;
Jens Axboe2e662b62006-07-13 11:55:04 +0200106 INIT_HLIST_NODE(&rq->hash);
107 RB_CLEAR_NODE(&rq->rb_node);
FUJITA Tomonorid7e3c322008-04-29 09:54:39 +0200108 rq->cmd = rq->__cmd;
Li Zefane2494e12009-04-02 13:43:26 +0800109 rq->cmd_len = BLK_MAX_CDB;
Jens Axboe63a71382008-02-08 12:41:03 +0100110 rq->tag = -1;
Tejun Heob243ddc2009-04-23 11:05:18 +0900111 rq->start_time = jiffies;
Divyesh Shah91952912010-04-01 15:01:41 -0700112 set_start_time_ns(rq);
Jerome Marchand09e099d2011-01-05 16:57:38 +0100113 rq->part = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200115EXPORT_SYMBOL(blk_rq_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
NeilBrown5bb23a62007-09-27 12:46:13 +0200117static void req_bio_endio(struct request *rq, struct bio *bio,
118 unsigned int nbytes, int error)
Tejun Heo797e7db2006-01-06 09:51:03 +0100119{
Tejun Heo143a87f2011-01-25 12:43:52 +0100120 if (error)
121 clear_bit(BIO_UPTODATE, &bio->bi_flags);
122 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
123 error = -EIO;
Tejun Heo797e7db2006-01-06 09:51:03 +0100124
Tejun Heo143a87f2011-01-25 12:43:52 +0100125 if (unlikely(rq->cmd_flags & REQ_QUIET))
126 set_bit(BIO_QUIET, &bio->bi_flags);
127
Kent Overstreetf79ea412012-09-20 16:38:30 -0700128 bio_advance(bio, nbytes);
Tejun Heo143a87f2011-01-25 12:43:52 +0100129
130 /* don't actually finish bio if it's part of flush sequence */
Kent Overstreet4f024f32013-10-11 15:44:27 -0700131 if (bio->bi_iter.bi_size == 0 && !(rq->cmd_flags & REQ_FLUSH_SEQ))
Tejun Heo143a87f2011-01-25 12:43:52 +0100132 bio_endio(bio, error);
Tejun Heo797e7db2006-01-06 09:51:03 +0100133}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135void blk_dump_rq_flags(struct request *rq, char *msg)
136{
137 int bit;
138
Jens Axboe59533162013-05-23 12:25:08 +0200139 printk(KERN_INFO "%s: dev %s: type=%x, flags=%llx\n", msg,
Jens Axboe4aff5e22006-08-10 08:44:47 +0200140 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
Jens Axboe59533162013-05-23 12:25:08 +0200141 (unsigned long long) rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Tejun Heo83096eb2009-05-07 22:24:39 +0900143 printk(KERN_INFO " sector %llu, nr/cnr %u/%u\n",
144 (unsigned long long)blk_rq_pos(rq),
145 blk_rq_sectors(rq), blk_rq_cur_sectors(rq));
Jens Axboeb4f42e22014-04-10 09:46:28 -0600146 printk(KERN_INFO " bio %p, biotail %p, len %u\n",
147 rq->bio, rq->biotail, blk_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200149 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
Jens Axboe6728cb02008-01-31 13:03:55 +0100150 printk(KERN_INFO " cdb: ");
FUJITA Tomonorid34c87e2008-04-29 14:37:52 +0200151 for (bit = 0; bit < BLK_MAX_CDB; bit++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 printk("%02x ", rq->cmd[bit]);
153 printk("\n");
154 }
155}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156EXPORT_SYMBOL(blk_dump_rq_flags);
157
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500158static void blk_delay_work(struct work_struct *work)
Jens Axboe6c5e0c42008-08-01 20:31:32 +0200159{
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500160 struct request_queue *q;
Jens Axboe6c5e0c42008-08-01 20:31:32 +0200161
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500162 q = container_of(work, struct request_queue, delay_work.work);
163 spin_lock_irq(q->queue_lock);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200164 __blk_run_queue(q);
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500165 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168/**
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500169 * blk_delay_queue - restart queueing after defined interval
170 * @q: The &struct request_queue in question
171 * @msecs: Delay in msecs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 *
173 * Description:
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500174 * Sometimes queueing needs to be postponed for a little while, to allow
175 * resources to come back. This function will make sure that queueing is
Bart Van Assche70460572012-11-28 13:45:56 +0100176 * restarted around the specified time. Queue lock must be held.
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500177 */
178void blk_delay_queue(struct request_queue *q, unsigned long msecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Bart Van Assche70460572012-11-28 13:45:56 +0100180 if (likely(!blk_queue_dead(q)))
181 queue_delayed_work(kblockd_workqueue, &q->delay_work,
182 msecs_to_jiffies(msecs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500184EXPORT_SYMBOL(blk_delay_queue);
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186/**
187 * blk_start_queue - restart a previously stopped queue
Jens Axboe165125e2007-07-24 09:28:11 +0200188 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 *
190 * Description:
191 * blk_start_queue() will clear the stop flag on the queue, and call
192 * the request_fn for the queue if it was in a stopped state when
193 * entered. Also see blk_stop_queue(). Queue lock must be held.
194 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200195void blk_start_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200197 WARN_ON(!irqs_disabled());
198
Nick Piggin75ad23b2008-04-29 14:48:33 +0200199 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200200 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202EXPORT_SYMBOL(blk_start_queue);
203
204/**
205 * blk_stop_queue - stop a queue
Jens Axboe165125e2007-07-24 09:28:11 +0200206 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 *
208 * Description:
209 * The Linux block layer assumes that a block driver will consume all
210 * entries on the request queue when the request_fn strategy is called.
211 * Often this will not happen, because of hardware limitations (queue
212 * depth settings). If a device driver gets a 'queue full' response,
213 * or if it simply chooses not to queue more I/O at one point, it can
214 * call this function to prevent the request_fn from being called until
215 * the driver has signalled it's ready to go again. This happens by calling
216 * blk_start_queue() to restart queue operations. Queue lock must be held.
217 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200218void blk_stop_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Tejun Heo136b5722012-08-21 13:18:24 -0700220 cancel_delayed_work(&q->delay_work);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200221 queue_flag_set(QUEUE_FLAG_STOPPED, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223EXPORT_SYMBOL(blk_stop_queue);
224
225/**
226 * blk_sync_queue - cancel any pending callbacks on a queue
227 * @q: the queue
228 *
229 * Description:
230 * The block layer may perform asynchronous callback activity
231 * on a queue, such as calling the unplug function after a timeout.
232 * A block device may call blk_sync_queue to ensure that any
233 * such activity is cancelled, thus allowing it to release resources
Michael Opdenacker59c51592007-05-09 08:57:56 +0200234 * that the callbacks might use. The caller must already have made sure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 * that its ->make_request_fn will not re-add plugging prior to calling
236 * this function.
237 *
Vivek Goyalda527772011-03-02 19:05:33 -0500238 * This function does not cancel any asynchronous activity arising
239 * out of elevator or throttling code. That would require elevaotor_exit()
Tejun Heo5efd6112012-03-05 13:15:12 -0800240 * and blkcg_exit_queue() to be called with queue lock initialized.
Vivek Goyalda527772011-03-02 19:05:33 -0500241 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 */
243void blk_sync_queue(struct request_queue *q)
244{
Jens Axboe70ed28b2008-11-19 14:38:39 +0100245 del_timer_sync(&q->timeout);
Ming Leif04c1fe2013-12-26 21:31:36 +0800246
247 if (q->mq_ops) {
248 struct blk_mq_hw_ctx *hctx;
249 int i;
250
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600251 queue_for_each_hw_ctx(q, hctx, i) {
252 cancel_delayed_work_sync(&hctx->run_work);
253 cancel_delayed_work_sync(&hctx->delay_work);
254 }
Ming Leif04c1fe2013-12-26 21:31:36 +0800255 } else {
256 cancel_delayed_work_sync(&q->delay_work);
257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259EXPORT_SYMBOL(blk_sync_queue);
260
261/**
Bart Van Asschec246e802012-12-06 14:32:01 +0100262 * __blk_run_queue_uncond - run a queue whether or not it has been stopped
263 * @q: The queue to run
264 *
265 * Description:
266 * Invoke request handling on a queue if there are any pending requests.
267 * May be used to restart request handling after a request has completed.
268 * This variant runs the queue whether or not the queue has been
269 * stopped. Must be called with the queue lock held and interrupts
270 * disabled. See also @blk_run_queue.
271 */
272inline void __blk_run_queue_uncond(struct request_queue *q)
273{
274 if (unlikely(blk_queue_dead(q)))
275 return;
276
Bart Van Assche24faf6f2012-11-28 13:46:45 +0100277 /*
278 * Some request_fn implementations, e.g. scsi_request_fn(), unlock
279 * the queue lock internally. As a result multiple threads may be
280 * running such a request function concurrently. Keep track of the
281 * number of active request_fn invocations such that blk_drain_queue()
282 * can wait until all these request_fn calls have finished.
283 */
284 q->request_fn_active++;
Bart Van Asschec246e802012-12-06 14:32:01 +0100285 q->request_fn(q);
Bart Van Assche24faf6f2012-11-28 13:46:45 +0100286 q->request_fn_active--;
Bart Van Asschec246e802012-12-06 14:32:01 +0100287}
288
289/**
Jens Axboe80a4b582008-10-14 09:51:06 +0200290 * __blk_run_queue - run a single device queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200292 *
293 * Description:
294 * See @blk_run_queue. This variant must be called with the queue lock
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200295 * held and interrupts disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 */
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200297void __blk_run_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Tejun Heoa538cd02009-04-23 11:05:17 +0900299 if (unlikely(blk_queue_stopped(q)))
300 return;
301
Bart Van Asschec246e802012-12-06 14:32:01 +0100302 __blk_run_queue_uncond(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200303}
304EXPORT_SYMBOL(__blk_run_queue);
Jens Axboedac07ec2006-05-11 08:20:16 +0200305
Nick Piggin75ad23b2008-04-29 14:48:33 +0200306/**
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200307 * blk_run_queue_async - run a single device queue in workqueue context
308 * @q: The queue to run
309 *
310 * Description:
311 * Tells kblockd to perform the equivalent of @blk_run_queue on behalf
Bart Van Assche70460572012-11-28 13:45:56 +0100312 * of us. The caller must hold the queue lock.
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200313 */
314void blk_run_queue_async(struct request_queue *q)
315{
Bart Van Assche70460572012-11-28 13:45:56 +0100316 if (likely(!blk_queue_stopped(q) && !blk_queue_dead(q)))
Tejun Heoe7c2f962012-08-21 13:18:24 -0700317 mod_delayed_work(kblockd_workqueue, &q->delay_work, 0);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200318}
Jens Axboec21e6be2011-04-19 13:32:46 +0200319EXPORT_SYMBOL(blk_run_queue_async);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200320
321/**
Nick Piggin75ad23b2008-04-29 14:48:33 +0200322 * blk_run_queue - run a single device queue
323 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200324 *
325 * Description:
326 * Invoke request handling on this queue, if it has pending work to do.
Tejun Heoa7f55792009-04-23 11:05:17 +0900327 * May be used to restart queueing when a request has completed.
Nick Piggin75ad23b2008-04-29 14:48:33 +0200328 */
329void blk_run_queue(struct request_queue *q)
330{
331 unsigned long flags;
332
333 spin_lock_irqsave(q->queue_lock, flags);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200334 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 spin_unlock_irqrestore(q->queue_lock, flags);
336}
337EXPORT_SYMBOL(blk_run_queue);
338
Jens Axboe165125e2007-07-24 09:28:11 +0200339void blk_put_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500340{
341 kobject_put(&q->kobj);
342}
Jens Axboed86e0e82011-05-27 07:44:43 +0200343EXPORT_SYMBOL(blk_put_queue);
Al Viro483f4af2006-03-18 18:34:37 -0500344
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200345/**
Bart Van Assche807592a2012-11-28 13:43:38 +0100346 * __blk_drain_queue - drain requests from request_queue
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200347 * @q: queue to drain
Tejun Heoc9a929d2011-10-19 14:42:16 +0200348 * @drain_all: whether to drain all requests or only the ones w/ ELVPRIV
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200349 *
Tejun Heoc9a929d2011-10-19 14:42:16 +0200350 * Drain requests from @q. If @drain_all is set, all requests are drained.
351 * If not, only ELVPRIV requests are drained. The caller is responsible
352 * for ensuring that no new requests which need to be drained are queued.
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200353 */
Bart Van Assche807592a2012-11-28 13:43:38 +0100354static void __blk_drain_queue(struct request_queue *q, bool drain_all)
355 __releases(q->queue_lock)
356 __acquires(q->queue_lock)
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200357{
Asias He458f27a2012-06-15 08:45:25 +0200358 int i;
359
Bart Van Assche807592a2012-11-28 13:43:38 +0100360 lockdep_assert_held(q->queue_lock);
361
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200362 while (true) {
Tejun Heo481a7d62011-12-14 00:33:37 +0100363 bool drain = false;
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200364
Tejun Heob855b042012-03-06 21:24:55 +0100365 /*
366 * The caller might be trying to drain @q before its
367 * elevator is initialized.
368 */
369 if (q->elevator)
370 elv_drain_elevator(q);
371
Tejun Heo5efd6112012-03-05 13:15:12 -0800372 blkcg_drain_queue(q);
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200373
Tejun Heo4eabc942011-12-15 20:03:04 +0100374 /*
375 * This function might be called on a queue which failed
Tejun Heob855b042012-03-06 21:24:55 +0100376 * driver init after queue creation or is not yet fully
377 * active yet. Some drivers (e.g. fd and loop) get unhappy
378 * in such cases. Kick queue iff dispatch queue has
379 * something on it and @q has request_fn set.
Tejun Heo4eabc942011-12-15 20:03:04 +0100380 */
Tejun Heob855b042012-03-06 21:24:55 +0100381 if (!list_empty(&q->queue_head) && q->request_fn)
Tejun Heo4eabc942011-12-15 20:03:04 +0100382 __blk_run_queue(q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200383
Tejun Heo8a5ecdd2012-06-04 20:40:58 -0700384 drain |= q->nr_rqs_elvpriv;
Bart Van Assche24faf6f2012-11-28 13:46:45 +0100385 drain |= q->request_fn_active;
Tejun Heo481a7d62011-12-14 00:33:37 +0100386
387 /*
388 * Unfortunately, requests are queued at and tracked from
389 * multiple places and there's no single counter which can
390 * be drained. Check all the queues and counters.
391 */
392 if (drain_all) {
393 drain |= !list_empty(&q->queue_head);
394 for (i = 0; i < 2; i++) {
Tejun Heo8a5ecdd2012-06-04 20:40:58 -0700395 drain |= q->nr_rqs[i];
Tejun Heo481a7d62011-12-14 00:33:37 +0100396 drain |= q->in_flight[i];
397 drain |= !list_empty(&q->flush_queue[i]);
398 }
399 }
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200400
Tejun Heo481a7d62011-12-14 00:33:37 +0100401 if (!drain)
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200402 break;
Bart Van Assche807592a2012-11-28 13:43:38 +0100403
404 spin_unlock_irq(q->queue_lock);
405
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200406 msleep(10);
Bart Van Assche807592a2012-11-28 13:43:38 +0100407
408 spin_lock_irq(q->queue_lock);
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200409 }
Asias He458f27a2012-06-15 08:45:25 +0200410
411 /*
412 * With queue marked dead, any woken up waiter will fail the
413 * allocation path, so the wakeup chaining is lost and we're
414 * left with hung waiters. We need to wake up those waiters.
415 */
416 if (q->request_fn) {
Tejun Heoa0516612012-06-26 15:05:44 -0700417 struct request_list *rl;
418
Tejun Heoa0516612012-06-26 15:05:44 -0700419 blk_queue_for_each_rl(rl, q)
420 for (i = 0; i < ARRAY_SIZE(rl->wait); i++)
421 wake_up_all(&rl->wait[i]);
Asias He458f27a2012-06-15 08:45:25 +0200422 }
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200423}
424
Tejun Heoc9a929d2011-10-19 14:42:16 +0200425/**
Tejun Heod7325802012-03-05 13:14:58 -0800426 * blk_queue_bypass_start - enter queue bypass mode
427 * @q: queue of interest
428 *
429 * In bypass mode, only the dispatch FIFO queue of @q is used. This
430 * function makes @q enter bypass mode and drains all requests which were
Tejun Heo6ecf23a2012-03-05 13:14:59 -0800431 * throttled or issued before. On return, it's guaranteed that no request
Tejun Heo80fd9972012-04-13 14:50:53 -0700432 * is being throttled or has ELVPRIV set and blk_queue_bypass() %true
433 * inside queue or RCU read lock.
Tejun Heod7325802012-03-05 13:14:58 -0800434 */
435void blk_queue_bypass_start(struct request_queue *q)
436{
437 spin_lock_irq(q->queue_lock);
Tejun Heo776687b2014-07-01 10:29:17 -0600438 q->bypass_depth++;
Tejun Heod7325802012-03-05 13:14:58 -0800439 queue_flag_set(QUEUE_FLAG_BYPASS, q);
440 spin_unlock_irq(q->queue_lock);
441
Tejun Heo776687b2014-07-01 10:29:17 -0600442 /*
443 * Queues start drained. Skip actual draining till init is
444 * complete. This avoids lenghty delays during queue init which
445 * can happen many times during boot.
446 */
447 if (blk_queue_init_done(q)) {
Bart Van Assche807592a2012-11-28 13:43:38 +0100448 spin_lock_irq(q->queue_lock);
449 __blk_drain_queue(q, false);
450 spin_unlock_irq(q->queue_lock);
451
Tejun Heob82d4b12012-04-13 13:11:31 -0700452 /* ensure blk_queue_bypass() is %true inside RCU read lock */
453 synchronize_rcu();
454 }
Tejun Heod7325802012-03-05 13:14:58 -0800455}
456EXPORT_SYMBOL_GPL(blk_queue_bypass_start);
457
458/**
459 * blk_queue_bypass_end - leave queue bypass mode
460 * @q: queue of interest
461 *
462 * Leave bypass mode and restore the normal queueing behavior.
463 */
464void blk_queue_bypass_end(struct request_queue *q)
465{
466 spin_lock_irq(q->queue_lock);
467 if (!--q->bypass_depth)
468 queue_flag_clear(QUEUE_FLAG_BYPASS, q);
469 WARN_ON_ONCE(q->bypass_depth < 0);
470 spin_unlock_irq(q->queue_lock);
471}
472EXPORT_SYMBOL_GPL(blk_queue_bypass_end);
473
474/**
Tejun Heoc9a929d2011-10-19 14:42:16 +0200475 * blk_cleanup_queue - shutdown a request queue
476 * @q: request queue to shutdown
477 *
Bart Van Asschec246e802012-12-06 14:32:01 +0100478 * Mark @q DYING, drain all pending requests, mark @q DEAD, destroy and
479 * put it. All future requests will be failed immediately with -ENODEV.
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500480 */
Jens Axboe6728cb02008-01-31 13:03:55 +0100481void blk_cleanup_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500482{
Tejun Heoc9a929d2011-10-19 14:42:16 +0200483 spinlock_t *lock = q->queue_lock;
Jens Axboee3335de92008-09-18 09:22:54 -0700484
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100485 /* mark @q DYING, no new request or merges will be allowed afterwards */
Al Viro483f4af2006-03-18 18:34:37 -0500486 mutex_lock(&q->sysfs_lock);
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100487 queue_flag_set_unlocked(QUEUE_FLAG_DYING, q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200488 spin_lock_irq(lock);
Tejun Heo6ecf23a2012-03-05 13:14:59 -0800489
Tejun Heo80fd9972012-04-13 14:50:53 -0700490 /*
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100491 * A dying queue is permanently in bypass mode till released. Note
Tejun Heo80fd9972012-04-13 14:50:53 -0700492 * that, unlike blk_queue_bypass_start(), we aren't performing
493 * synchronize_rcu() after entering bypass mode to avoid the delay
494 * as some drivers create and destroy a lot of queues while
495 * probing. This is still safe because blk_release_queue() will be
496 * called only after the queue refcnt drops to zero and nothing,
497 * RCU or not, would be traversing the queue by then.
498 */
Tejun Heo6ecf23a2012-03-05 13:14:59 -0800499 q->bypass_depth++;
500 queue_flag_set(QUEUE_FLAG_BYPASS, q);
501
Tejun Heoc9a929d2011-10-19 14:42:16 +0200502 queue_flag_set(QUEUE_FLAG_NOMERGES, q);
503 queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100504 queue_flag_set(QUEUE_FLAG_DYING, q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200505 spin_unlock_irq(lock);
506 mutex_unlock(&q->sysfs_lock);
507
Bart Van Asschec246e802012-12-06 14:32:01 +0100508 /*
509 * Drain all requests queued before DYING marking. Set DEAD flag to
510 * prevent that q->request_fn() gets invoked after draining finished.
511 */
Ming Lei43a5e4e2013-12-26 21:31:35 +0800512 if (q->mq_ops) {
Tejun Heo780db202014-07-01 10:31:13 -0600513 blk_mq_freeze_queue(q);
Ming Lei43a5e4e2013-12-26 21:31:35 +0800514 spin_lock_irq(lock);
515 } else {
516 spin_lock_irq(lock);
517 __blk_drain_queue(q, true);
518 }
Bart Van Asschec246e802012-12-06 14:32:01 +0100519 queue_flag_set(QUEUE_FLAG_DEAD, q);
Bart Van Assche807592a2012-11-28 13:43:38 +0100520 spin_unlock_irq(lock);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200521
522 /* @q won't process any more request, flush async actions */
523 del_timer_sync(&q->backing_dev_info.laptop_mode_wb_timer);
524 blk_sync_queue(q);
525
Asias He5e5cfac2012-05-24 23:28:52 +0800526 spin_lock_irq(lock);
527 if (q->queue_lock != &q->__queue_lock)
528 q->queue_lock = &q->__queue_lock;
529 spin_unlock_irq(lock);
530
Tejun Heoc9a929d2011-10-19 14:42:16 +0200531 /* @q is and will stay empty, shutdown and put */
Al Viro483f4af2006-03-18 18:34:37 -0500532 blk_put_queue(q);
533}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534EXPORT_SYMBOL(blk_cleanup_queue);
535
Tejun Heo5b788ce2012-06-04 20:40:59 -0700536int blk_init_rl(struct request_list *rl, struct request_queue *q,
537 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Mike Snitzer1abec4f2010-05-25 13:15:15 -0400539 if (unlikely(rl->rq_pool))
540 return 0;
541
Tejun Heo5b788ce2012-06-04 20:40:59 -0700542 rl->q = q;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200543 rl->count[BLK_RW_SYNC] = rl->count[BLK_RW_ASYNC] = 0;
544 rl->starved[BLK_RW_SYNC] = rl->starved[BLK_RW_ASYNC] = 0;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200545 init_waitqueue_head(&rl->wait[BLK_RW_SYNC]);
546 init_waitqueue_head(&rl->wait[BLK_RW_ASYNC]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Christoph Lameter19460892005-06-23 00:08:19 -0700548 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
Tejun Heoa91a5ac2012-06-04 20:40:53 -0700549 mempool_free_slab, request_cachep,
Tejun Heo5b788ce2012-06-04 20:40:59 -0700550 gfp_mask, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (!rl->rq_pool)
552 return -ENOMEM;
553
554 return 0;
555}
556
Tejun Heo5b788ce2012-06-04 20:40:59 -0700557void blk_exit_rl(struct request_list *rl)
558{
559 if (rl->rq_pool)
560 mempool_destroy(rl->rq_pool);
561}
562
Jens Axboe165125e2007-07-24 09:28:11 +0200563struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Ezequiel Garciac304a512012-11-10 10:39:44 +0100565 return blk_alloc_queue_node(gfp_mask, NUMA_NO_NODE);
Christoph Lameter19460892005-06-23 00:08:19 -0700566}
567EXPORT_SYMBOL(blk_alloc_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Jens Axboe165125e2007-07-24 09:28:11 +0200569struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -0700570{
Jens Axboe165125e2007-07-24 09:28:11 +0200571 struct request_queue *q;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700572 int err;
Christoph Lameter19460892005-06-23 00:08:19 -0700573
Jens Axboe8324aa92008-01-29 14:51:59 +0100574 q = kmem_cache_alloc_node(blk_requestq_cachep,
Christoph Lameter94f60302007-07-17 04:03:29 -0700575 gfp_mask | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (!q)
577 return NULL;
578
Dan Carpenter00380a42012-03-23 09:58:54 +0100579 q->id = ida_simple_get(&blk_queue_ida, 0, 0, gfp_mask);
Tejun Heoa73f7302011-12-14 00:33:37 +0100580 if (q->id < 0)
Ming Lei3d2936f2014-05-27 23:35:14 +0800581 goto fail_q;
Tejun Heoa73f7302011-12-14 00:33:37 +0100582
Jens Axboe0989a022009-06-12 14:42:56 +0200583 q->backing_dev_info.ra_pages =
584 (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
585 q->backing_dev_info.state = 0;
586 q->backing_dev_info.capabilities = BDI_CAP_MAP_COPY;
Jens Axboed9938312009-06-12 14:45:52 +0200587 q->backing_dev_info.name = "block";
Mike Snitzer51514122011-11-23 10:59:13 +0100588 q->node = node_id;
Jens Axboe0989a022009-06-12 14:42:56 +0200589
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700590 err = bdi_init(&q->backing_dev_info);
Tejun Heoa73f7302011-12-14 00:33:37 +0100591 if (err)
592 goto fail_id;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700593
Matthew Garrett31373d02010-04-06 14:25:14 +0200594 setup_timer(&q->backing_dev_info.laptop_mode_wb_timer,
595 laptop_mode_timer_fn, (unsigned long) q);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700596 setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
Tejun Heob855b042012-03-06 21:24:55 +0100597 INIT_LIST_HEAD(&q->queue_head);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700598 INIT_LIST_HEAD(&q->timeout_list);
Tejun Heoa612fdd2011-12-14 00:33:41 +0100599 INIT_LIST_HEAD(&q->icq_list);
Tejun Heo4eef3042012-03-05 13:15:18 -0800600#ifdef CONFIG_BLK_CGROUP
Tejun Heoe8989fa2012-03-05 13:15:20 -0800601 INIT_LIST_HEAD(&q->blkg_list);
Tejun Heo4eef3042012-03-05 13:15:18 -0800602#endif
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500603 INIT_DELAYED_WORK(&q->delay_work, blk_delay_work);
Al Viro483f4af2006-03-18 18:34:37 -0500604
Jens Axboe8324aa92008-01-29 14:51:59 +0100605 kobject_init(&q->kobj, &blk_queue_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Al Viro483f4af2006-03-18 18:34:37 -0500607 mutex_init(&q->sysfs_lock);
Neil Browne7e72bf2008-05-14 16:05:54 -0700608 spin_lock_init(&q->__queue_lock);
Al Viro483f4af2006-03-18 18:34:37 -0500609
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500610 /*
611 * By default initialize queue_lock to internal lock and driver can
612 * override it later if need be.
613 */
614 q->queue_lock = &q->__queue_lock;
615
Tejun Heob82d4b12012-04-13 13:11:31 -0700616 /*
617 * A queue starts its life with bypass turned on to avoid
618 * unnecessary bypass on/off overhead and nasty surprises during
Tejun Heo749fefe2012-09-20 14:08:52 -0700619 * init. The initial bypass will be finished when the queue is
620 * registered by blk_register_queue().
Tejun Heob82d4b12012-04-13 13:11:31 -0700621 */
622 q->bypass_depth = 1;
623 __set_bit(QUEUE_FLAG_BYPASS, &q->queue_flags);
624
Jens Axboe320ae512013-10-24 09:20:05 +0100625 init_waitqueue_head(&q->mq_freeze_wq);
626
Tejun Heo5efd6112012-03-05 13:15:12 -0800627 if (blkcg_init_queue(q))
Mikulas Patockafff49962013-10-14 12:11:36 -0400628 goto fail_bdi;
Tejun Heof51b8022012-03-05 13:15:05 -0800629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return q;
Tejun Heoa73f7302011-12-14 00:33:37 +0100631
Mikulas Patockafff49962013-10-14 12:11:36 -0400632fail_bdi:
633 bdi_destroy(&q->backing_dev_info);
Tejun Heoa73f7302011-12-14 00:33:37 +0100634fail_id:
635 ida_simple_remove(&blk_queue_ida, q->id);
636fail_q:
637 kmem_cache_free(blk_requestq_cachep, q);
638 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
Christoph Lameter19460892005-06-23 00:08:19 -0700640EXPORT_SYMBOL(blk_alloc_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642/**
643 * blk_init_queue - prepare a request queue for use with a block device
644 * @rfn: The function to be called to process requests that have been
645 * placed on the queue.
646 * @lock: Request queue spin lock
647 *
648 * Description:
649 * If a block device wishes to use the standard request handling procedures,
650 * which sorts requests and coalesces adjacent requests, then it must
651 * call blk_init_queue(). The function @rfn will be called when there
652 * are requests on the queue that need to be processed. If the device
653 * supports plugging, then @rfn may not be called immediately when requests
654 * are available on the queue, but may be called at some time later instead.
655 * Plugged queues are generally unplugged when a buffer belonging to one
656 * of the requests on the queue is needed, or due to memory pressure.
657 *
658 * @rfn is not required, or even expected, to remove all requests off the
659 * queue, but only as many as it can handle at a time. If it does leave
660 * requests on the queue, it is responsible for arranging that the requests
661 * get dealt with eventually.
662 *
663 * The queue spin lock must be held while manipulating the requests on the
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200664 * request queue; this lock will be taken also from interrupt context, so irq
665 * disabling is needed for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 *
Randy Dunlap710027a2008-08-19 20:13:11 +0200667 * Function returns a pointer to the initialized request queue, or %NULL if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 * it didn't succeed.
669 *
670 * Note:
671 * blk_init_queue() must be paired with a blk_cleanup_queue() call
672 * when the block device is deactivated (such as at module unload).
673 **/
Christoph Lameter19460892005-06-23 00:08:19 -0700674
Jens Axboe165125e2007-07-24 09:28:11 +0200675struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Ezequiel Garciac304a512012-11-10 10:39:44 +0100677 return blk_init_queue_node(rfn, lock, NUMA_NO_NODE);
Christoph Lameter19460892005-06-23 00:08:19 -0700678}
679EXPORT_SYMBOL(blk_init_queue);
680
Jens Axboe165125e2007-07-24 09:28:11 +0200681struct request_queue *
Christoph Lameter19460892005-06-23 00:08:19 -0700682blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
683{
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600684 struct request_queue *uninit_q, *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600686 uninit_q = blk_alloc_queue_node(GFP_KERNEL, node_id);
687 if (!uninit_q)
688 return NULL;
689
Mike Snitzer51514122011-11-23 10:59:13 +0100690 q = blk_init_allocated_queue(uninit_q, rfn, lock);
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600691 if (!q)
Mike Snitzer7982e902014-03-08 17:20:01 -0700692 blk_cleanup_queue(uninit_q);
Christoph Hellwig18741982014-02-10 09:29:00 -0700693
Mike Snitzer7982e902014-03-08 17:20:01 -0700694 return q;
Mike Snitzer01effb02010-05-11 08:57:42 +0200695}
696EXPORT_SYMBOL(blk_init_queue_node);
697
698struct request_queue *
699blk_init_allocated_queue(struct request_queue *q, request_fn_proc *rfn,
700 spinlock_t *lock)
701{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (!q)
703 return NULL;
704
Ming Leif3552652014-09-25 23:23:40 +0800705 if (blk_init_flush(q))
Mike Snitzer7982e902014-03-08 17:20:01 -0700706 return NULL;
707
Tejun Heoa0516612012-06-26 15:05:44 -0700708 if (blk_init_rl(&q->root_rl, q, GFP_KERNEL))
Dave Jones708f04d2014-03-20 15:03:58 -0600709 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 q->request_fn = rfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 q->prep_rq_fn = NULL;
James Bottomley28018c22010-07-01 19:49:17 +0900713 q->unprep_rq_fn = NULL;
Tejun Heo60ea8222012-09-20 14:09:30 -0700714 q->queue_flags |= QUEUE_FLAG_DEFAULT;
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500715
716 /* Override internal queue lock with supplied lock pointer */
717 if (lock)
718 q->queue_lock = lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Jens Axboef3b144a2009-03-06 08:48:33 +0100720 /*
721 * This also sets hw/phys segments, boundary and size
722 */
Jens Axboec20e8de2011-09-12 12:03:37 +0200723 blk_queue_make_request(q, blk_queue_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Alan Stern44ec9542007-02-20 11:01:57 -0500725 q->sg_reserved_size = INT_MAX;
726
Tomoki Sekiyamaeb1c1602013-10-15 16:42:16 -0600727 /* Protect q->elevator from elevator_change */
728 mutex_lock(&q->sysfs_lock);
729
Tejun Heob82d4b12012-04-13 13:11:31 -0700730 /* init elevator */
Tomoki Sekiyamaeb1c1602013-10-15 16:42:16 -0600731 if (elevator_init(q, NULL)) {
732 mutex_unlock(&q->sysfs_lock);
Dave Jones708f04d2014-03-20 15:03:58 -0600733 goto fail;
Tomoki Sekiyamaeb1c1602013-10-15 16:42:16 -0600734 }
735
736 mutex_unlock(&q->sysfs_lock);
737
Tejun Heob82d4b12012-04-13 13:11:31 -0700738 return q;
Dave Jones708f04d2014-03-20 15:03:58 -0600739
740fail:
Ming Leif3552652014-09-25 23:23:40 +0800741 blk_exit_flush(q);
Dave Jones708f04d2014-03-20 15:03:58 -0600742 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
Mike Snitzer51514122011-11-23 10:59:13 +0100744EXPORT_SYMBOL(blk_init_allocated_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Tejun Heo09ac46c2011-12-14 00:33:38 +0100746bool blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100748 if (likely(!blk_queue_dying(q))) {
Tejun Heo09ac46c2011-12-14 00:33:38 +0100749 __blk_get_queue(q);
750 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
752
Tejun Heo09ac46c2011-12-14 00:33:38 +0100753 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
Jens Axboed86e0e82011-05-27 07:44:43 +0200755EXPORT_SYMBOL(blk_get_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Tejun Heo5b788ce2012-06-04 20:40:59 -0700757static inline void blk_free_request(struct request_list *rl, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
Tejun Heof1f8cc92011-12-14 00:33:42 +0100759 if (rq->cmd_flags & REQ_ELVPRIV) {
Tejun Heo5b788ce2012-06-04 20:40:59 -0700760 elv_put_request(rl->q, rq);
Tejun Heof1f8cc92011-12-14 00:33:42 +0100761 if (rq->elv.icq)
Tejun Heo11a31222012-02-07 07:51:30 +0100762 put_io_context(rq->elv.icq->ioc);
Tejun Heof1f8cc92011-12-14 00:33:42 +0100763 }
764
Tejun Heo5b788ce2012-06-04 20:40:59 -0700765 mempool_free(rq, rl->rq_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768/*
769 * ioc_batching returns true if the ioc is a valid batching request and
770 * should be given priority access to a request.
771 */
Jens Axboe165125e2007-07-24 09:28:11 +0200772static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
774 if (!ioc)
775 return 0;
776
777 /*
778 * Make sure the process is able to allocate at least 1 request
779 * even if the batch times out, otherwise we could theoretically
780 * lose wakeups.
781 */
782 return ioc->nr_batch_requests == q->nr_batching ||
783 (ioc->nr_batch_requests > 0
784 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
785}
786
787/*
788 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
789 * will cause the process to be a "batcher" on all queues in the system. This
790 * is the behaviour we want though - once it gets a wakeup it should be given
791 * a nice run.
792 */
Jens Axboe165125e2007-07-24 09:28:11 +0200793static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
795 if (!ioc || ioc_batching(q, ioc))
796 return;
797
798 ioc->nr_batch_requests = q->nr_batching;
799 ioc->last_waited = jiffies;
800}
801
Tejun Heo5b788ce2012-06-04 20:40:59 -0700802static void __freed_request(struct request_list *rl, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
Tejun Heo5b788ce2012-06-04 20:40:59 -0700804 struct request_queue *q = rl->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Tejun Heoa0516612012-06-26 15:05:44 -0700806 /*
807 * bdi isn't aware of blkcg yet. As all async IOs end up root
808 * blkcg anyway, just use root blkcg state.
809 */
810 if (rl == &q->root_rl &&
811 rl->count[sync] < queue_congestion_off_threshold(q))
Jens Axboe1faa16d2009-04-06 14:48:01 +0200812 blk_clear_queue_congested(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Jens Axboe1faa16d2009-04-06 14:48:01 +0200814 if (rl->count[sync] + 1 <= q->nr_requests) {
815 if (waitqueue_active(&rl->wait[sync]))
816 wake_up(&rl->wait[sync]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Tejun Heo5b788ce2012-06-04 20:40:59 -0700818 blk_clear_rl_full(rl, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
820}
821
822/*
823 * A request has just been released. Account for it, update the full and
824 * congestion status, wake up any waiters. Called under q->queue_lock.
825 */
Tejun Heo5b788ce2012-06-04 20:40:59 -0700826static void freed_request(struct request_list *rl, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Tejun Heo5b788ce2012-06-04 20:40:59 -0700828 struct request_queue *q = rl->q;
Tejun Heo75eb6c32011-10-19 14:31:22 +0200829 int sync = rw_is_sync(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Tejun Heo8a5ecdd2012-06-04 20:40:58 -0700831 q->nr_rqs[sync]--;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200832 rl->count[sync]--;
Tejun Heo75eb6c32011-10-19 14:31:22 +0200833 if (flags & REQ_ELVPRIV)
Tejun Heo8a5ecdd2012-06-04 20:40:58 -0700834 q->nr_rqs_elvpriv--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Tejun Heo5b788ce2012-06-04 20:40:59 -0700836 __freed_request(rl, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Jens Axboe1faa16d2009-04-06 14:48:01 +0200838 if (unlikely(rl->starved[sync ^ 1]))
Tejun Heo5b788ce2012-06-04 20:40:59 -0700839 __freed_request(rl, sync ^ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
841
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600842int blk_update_nr_requests(struct request_queue *q, unsigned int nr)
843{
844 struct request_list *rl;
845
846 spin_lock_irq(q->queue_lock);
847 q->nr_requests = nr;
848 blk_queue_congestion_threshold(q);
849
850 /* congestion isn't cgroup aware and follows root blkcg for now */
851 rl = &q->root_rl;
852
853 if (rl->count[BLK_RW_SYNC] >= queue_congestion_on_threshold(q))
854 blk_set_queue_congested(q, BLK_RW_SYNC);
855 else if (rl->count[BLK_RW_SYNC] < queue_congestion_off_threshold(q))
856 blk_clear_queue_congested(q, BLK_RW_SYNC);
857
858 if (rl->count[BLK_RW_ASYNC] >= queue_congestion_on_threshold(q))
859 blk_set_queue_congested(q, BLK_RW_ASYNC);
860 else if (rl->count[BLK_RW_ASYNC] < queue_congestion_off_threshold(q))
861 blk_clear_queue_congested(q, BLK_RW_ASYNC);
862
863 blk_queue_for_each_rl(rl, q) {
864 if (rl->count[BLK_RW_SYNC] >= q->nr_requests) {
865 blk_set_rl_full(rl, BLK_RW_SYNC);
866 } else {
867 blk_clear_rl_full(rl, BLK_RW_SYNC);
868 wake_up(&rl->wait[BLK_RW_SYNC]);
869 }
870
871 if (rl->count[BLK_RW_ASYNC] >= q->nr_requests) {
872 blk_set_rl_full(rl, BLK_RW_ASYNC);
873 } else {
874 blk_clear_rl_full(rl, BLK_RW_ASYNC);
875 wake_up(&rl->wait[BLK_RW_ASYNC]);
876 }
877 }
878
879 spin_unlock_irq(q->queue_lock);
880 return 0;
881}
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883/*
Mike Snitzer9d5a4e92011-02-11 11:05:46 +0100884 * Determine if elevator data should be initialized when allocating the
885 * request associated with @bio.
886 */
887static bool blk_rq_should_init_elevator(struct bio *bio)
888{
889 if (!bio)
890 return true;
891
892 /*
893 * Flush requests do not use the elevator so skip initialization.
894 * This allows a request to share the flush and elevator data.
895 */
896 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA))
897 return false;
898
899 return true;
900}
901
Tejun Heoda8303c2011-10-19 14:33:05 +0200902/**
Tejun Heo852c7882012-03-05 13:15:27 -0800903 * rq_ioc - determine io_context for request allocation
904 * @bio: request being allocated is for this bio (can be %NULL)
905 *
906 * Determine io_context to use for request allocation for @bio. May return
907 * %NULL if %current->io_context doesn't exist.
908 */
909static struct io_context *rq_ioc(struct bio *bio)
910{
911#ifdef CONFIG_BLK_CGROUP
912 if (bio && bio->bi_ioc)
913 return bio->bi_ioc;
914#endif
915 return current->io_context;
916}
917
918/**
Tejun Heoa06e05e2012-06-04 20:40:55 -0700919 * __get_request - get a free request
Tejun Heo5b788ce2012-06-04 20:40:59 -0700920 * @rl: request list to allocate from
Tejun Heoda8303c2011-10-19 14:33:05 +0200921 * @rw_flags: RW and SYNC flags
922 * @bio: bio to allocate request for (can be %NULL)
923 * @gfp_mask: allocation mask
924 *
925 * Get a free request from @q. This function may fail under memory
926 * pressure or if @q is dead.
927 *
Joe Lawrencea492f072014-08-28 08:15:21 -0600928 * Must be called with @q->queue_lock held and,
929 * Returns ERR_PTR on failure, with @q->queue_lock held.
930 * Returns request pointer on success, with @q->queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 */
Tejun Heo5b788ce2012-06-04 20:40:59 -0700932static struct request *__get_request(struct request_list *rl, int rw_flags,
Tejun Heoa06e05e2012-06-04 20:40:55 -0700933 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Tejun Heo5b788ce2012-06-04 20:40:59 -0700935 struct request_queue *q = rl->q;
Tejun Heob6792812012-03-05 13:15:23 -0800936 struct request *rq;
Tejun Heo7f4b35d2012-06-04 20:40:56 -0700937 struct elevator_type *et = q->elevator->type;
938 struct io_context *ioc = rq_ioc(bio);
Tejun Heof1f8cc92011-12-14 00:33:42 +0100939 struct io_cq *icq = NULL;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200940 const bool is_sync = rw_is_sync(rw_flags) != 0;
Tejun Heo75eb6c32011-10-19 14:31:22 +0200941 int may_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100943 if (unlikely(blk_queue_dying(q)))
Joe Lawrencea492f072014-08-28 08:15:21 -0600944 return ERR_PTR(-ENODEV);
Tejun Heoda8303c2011-10-19 14:33:05 +0200945
Jens Axboe7749a8d2006-12-13 13:02:26 +0100946 may_queue = elv_may_queue(q, rw_flags);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100947 if (may_queue == ELV_MQUEUE_NO)
948 goto rq_starved;
949
Jens Axboe1faa16d2009-04-06 14:48:01 +0200950 if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) {
951 if (rl->count[is_sync]+1 >= q->nr_requests) {
Tejun Heof2dbd762011-12-14 00:33:40 +0100952 /*
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100953 * The queue will fill after this allocation, so set
954 * it as full, and mark this process as "batching".
955 * This process will be allowed to complete a batch of
956 * requests, others will be blocked.
957 */
Tejun Heo5b788ce2012-06-04 20:40:59 -0700958 if (!blk_rl_full(rl, is_sync)) {
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100959 ioc_set_batching(q, ioc);
Tejun Heo5b788ce2012-06-04 20:40:59 -0700960 blk_set_rl_full(rl, is_sync);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100961 } else {
962 if (may_queue != ELV_MQUEUE_MUST
963 && !ioc_batching(q, ioc)) {
964 /*
965 * The queue is full and the allocating
966 * process is not a "batcher", and not
967 * exempted by the IO scheduler
968 */
Joe Lawrencea492f072014-08-28 08:15:21 -0600969 return ERR_PTR(-ENOMEM);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100970 }
971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
Tejun Heoa0516612012-06-26 15:05:44 -0700973 /*
974 * bdi isn't aware of blkcg yet. As all async IOs end up
975 * root blkcg anyway, just use root blkcg state.
976 */
977 if (rl == &q->root_rl)
978 blk_set_queue_congested(q, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
980
Jens Axboe082cf692005-06-28 16:35:11 +0200981 /*
982 * Only allow batching queuers to allocate up to 50% over the defined
983 * limit of requests, otherwise we could have thousands of requests
984 * allocated with any setting of ->nr_requests
985 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200986 if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
Joe Lawrencea492f072014-08-28 08:15:21 -0600987 return ERR_PTR(-ENOMEM);
Hugh Dickinsfd782a42005-06-29 15:15:40 +0100988
Tejun Heo8a5ecdd2012-06-04 20:40:58 -0700989 q->nr_rqs[is_sync]++;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200990 rl->count[is_sync]++;
991 rl->starved[is_sync] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200992
Tejun Heof1f8cc92011-12-14 00:33:42 +0100993 /*
994 * Decide whether the new request will be managed by elevator. If
995 * so, mark @rw_flags and increment elvpriv. Non-zero elvpriv will
996 * prevent the current elevator from being destroyed until the new
997 * request is freed. This guarantees icq's won't be destroyed and
998 * makes creating new ones safe.
999 *
1000 * Also, lookup icq while holding queue_lock. If it doesn't exist,
1001 * it will be created after releasing queue_lock.
1002 */
Tejun Heod7325802012-03-05 13:14:58 -08001003 if (blk_rq_should_init_elevator(bio) && !blk_queue_bypass(q)) {
Tejun Heo75eb6c32011-10-19 14:31:22 +02001004 rw_flags |= REQ_ELVPRIV;
Tejun Heo8a5ecdd2012-06-04 20:40:58 -07001005 q->nr_rqs_elvpriv++;
Tejun Heof1f8cc92011-12-14 00:33:42 +01001006 if (et->icq_cache && ioc)
1007 icq = ioc_lookup_icq(ioc, q);
Mike Snitzer9d5a4e92011-02-11 11:05:46 +01001008 }
Tejun Heocb98fc82005-10-28 08:29:39 +02001009
Jens Axboef253b862010-10-24 22:06:02 +02001010 if (blk_queue_io_stat(q))
1011 rw_flags |= REQ_IO_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 spin_unlock_irq(q->queue_lock);
1013
Tejun Heo29e2b092012-04-19 16:29:21 -07001014 /* allocate and init request */
Tejun Heo5b788ce2012-06-04 20:40:59 -07001015 rq = mempool_alloc(rl->rq_pool, gfp_mask);
Tejun Heo29e2b092012-04-19 16:29:21 -07001016 if (!rq)
Tejun Heob6792812012-03-05 13:15:23 -08001017 goto fail_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Tejun Heo29e2b092012-04-19 16:29:21 -07001019 blk_rq_init(q, rq);
Tejun Heoa0516612012-06-26 15:05:44 -07001020 blk_rq_set_rl(rq, rl);
Tejun Heo29e2b092012-04-19 16:29:21 -07001021 rq->cmd_flags = rw_flags | REQ_ALLOCED;
1022
Tejun Heoaaf7c682012-04-19 16:29:22 -07001023 /* init elvpriv */
Tejun Heo29e2b092012-04-19 16:29:21 -07001024 if (rw_flags & REQ_ELVPRIV) {
Tejun Heoaaf7c682012-04-19 16:29:22 -07001025 if (unlikely(et->icq_cache && !icq)) {
Tejun Heo7f4b35d2012-06-04 20:40:56 -07001026 if (ioc)
1027 icq = ioc_create_icq(ioc, q, gfp_mask);
Tejun Heoaaf7c682012-04-19 16:29:22 -07001028 if (!icq)
1029 goto fail_elvpriv;
Tejun Heo29e2b092012-04-19 16:29:21 -07001030 }
Tejun Heoaaf7c682012-04-19 16:29:22 -07001031
1032 rq->elv.icq = icq;
1033 if (unlikely(elv_set_request(q, rq, bio, gfp_mask)))
1034 goto fail_elvpriv;
1035
1036 /* @rq->elv.icq holds io_context until @rq is freed */
Tejun Heo29e2b092012-04-19 16:29:21 -07001037 if (icq)
1038 get_io_context(icq->ioc);
1039 }
Tejun Heoaaf7c682012-04-19 16:29:22 -07001040out:
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001041 /*
1042 * ioc may be NULL here, and ioc_batching will be false. That's
1043 * OK, if the queue is under the request limit then requests need
1044 * not count toward the nr_batch_requests limit. There will always
1045 * be some limit enforced by BLK_BATCH_TIME.
1046 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (ioc_batching(q, ioc))
1048 ioc->nr_batch_requests--;
Jens Axboe6728cb02008-01-31 13:03:55 +01001049
Jens Axboe1faa16d2009-04-06 14:48:01 +02001050 trace_block_getrq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 return rq;
Tejun Heob6792812012-03-05 13:15:23 -08001052
Tejun Heoaaf7c682012-04-19 16:29:22 -07001053fail_elvpriv:
1054 /*
1055 * elvpriv init failed. ioc, icq and elvpriv aren't mempool backed
1056 * and may fail indefinitely under memory pressure and thus
1057 * shouldn't stall IO. Treat this request as !elvpriv. This will
1058 * disturb iosched and blkcg but weird is bettern than dead.
1059 */
1060 printk_ratelimited(KERN_WARNING "%s: request aux data allocation failed, iosched may be disturbed\n",
1061 dev_name(q->backing_dev_info.dev));
1062
1063 rq->cmd_flags &= ~REQ_ELVPRIV;
1064 rq->elv.icq = NULL;
1065
1066 spin_lock_irq(q->queue_lock);
Tejun Heo8a5ecdd2012-06-04 20:40:58 -07001067 q->nr_rqs_elvpriv--;
Tejun Heoaaf7c682012-04-19 16:29:22 -07001068 spin_unlock_irq(q->queue_lock);
1069 goto out;
1070
Tejun Heob6792812012-03-05 13:15:23 -08001071fail_alloc:
1072 /*
1073 * Allocation failed presumably due to memory. Undo anything we
1074 * might have messed up.
1075 *
1076 * Allocating task should really be put onto the front of the wait
1077 * queue, but this is pretty rare.
1078 */
1079 spin_lock_irq(q->queue_lock);
Tejun Heo5b788ce2012-06-04 20:40:59 -07001080 freed_request(rl, rw_flags);
Tejun Heob6792812012-03-05 13:15:23 -08001081
1082 /*
1083 * in the very unlikely event that allocation failed and no
1084 * requests for this direction was pending, mark us starved so that
1085 * freeing of a request in the other direction will notice
1086 * us. another possible fix would be to split the rq mempool into
1087 * READ and WRITE
1088 */
1089rq_starved:
1090 if (unlikely(rl->count[is_sync] == 0))
1091 rl->starved[is_sync] = 1;
Joe Lawrencea492f072014-08-28 08:15:21 -06001092 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
1094
Tejun Heoda8303c2011-10-19 14:33:05 +02001095/**
Tejun Heoa06e05e2012-06-04 20:40:55 -07001096 * get_request - get a free request
Tejun Heoda8303c2011-10-19 14:33:05 +02001097 * @q: request_queue to allocate request from
1098 * @rw_flags: RW and SYNC flags
1099 * @bio: bio to allocate request for (can be %NULL)
Tejun Heoa06e05e2012-06-04 20:40:55 -07001100 * @gfp_mask: allocation mask
Nick Piggind6344532005-06-28 20:45:14 -07001101 *
Tejun Heoa06e05e2012-06-04 20:40:55 -07001102 * Get a free request from @q. If %__GFP_WAIT is set in @gfp_mask, this
1103 * function keeps retrying under memory pressure and fails iff @q is dead.
Tejun Heoda8303c2011-10-19 14:33:05 +02001104 *
Joe Lawrencea492f072014-08-28 08:15:21 -06001105 * Must be called with @q->queue_lock held and,
1106 * Returns ERR_PTR on failure, with @q->queue_lock held.
1107 * Returns request pointer on success, with @q->queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 */
Tejun Heoa06e05e2012-06-04 20:40:55 -07001109static struct request *get_request(struct request_queue *q, int rw_flags,
1110 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
Jens Axboe1faa16d2009-04-06 14:48:01 +02001112 const bool is_sync = rw_is_sync(rw_flags) != 0;
Tejun Heoa06e05e2012-06-04 20:40:55 -07001113 DEFINE_WAIT(wait);
Tejun Heoa0516612012-06-26 15:05:44 -07001114 struct request_list *rl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 struct request *rq;
Tejun Heoa0516612012-06-26 15:05:44 -07001116
1117 rl = blk_get_rl(q, bio); /* transferred to @rq on success */
Tejun Heoa06e05e2012-06-04 20:40:55 -07001118retry:
Tejun Heoa0516612012-06-26 15:05:44 -07001119 rq = __get_request(rl, rw_flags, bio, gfp_mask);
Joe Lawrencea492f072014-08-28 08:15:21 -06001120 if (!IS_ERR(rq))
Tejun Heoa06e05e2012-06-04 20:40:55 -07001121 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Bart Van Assche3f3299d2012-11-28 13:42:38 +01001123 if (!(gfp_mask & __GFP_WAIT) || unlikely(blk_queue_dying(q))) {
Tejun Heoa0516612012-06-26 15:05:44 -07001124 blk_put_rl(rl);
Joe Lawrencea492f072014-08-28 08:15:21 -06001125 return rq;
Tejun Heoa0516612012-06-26 15:05:44 -07001126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Tejun Heoa06e05e2012-06-04 20:40:55 -07001128 /* wait on @rl and retry */
1129 prepare_to_wait_exclusive(&rl->wait[is_sync], &wait,
1130 TASK_UNINTERRUPTIBLE);
Tejun Heoda8303c2011-10-19 14:33:05 +02001131
Tejun Heoa06e05e2012-06-04 20:40:55 -07001132 trace_block_sleeprq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Tejun Heoa06e05e2012-06-04 20:40:55 -07001134 spin_unlock_irq(q->queue_lock);
1135 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Tejun Heoa06e05e2012-06-04 20:40:55 -07001137 /*
1138 * After sleeping, we become a "batching" process and will be able
1139 * to allocate at least one request, and up to a big batch of them
1140 * for a small period time. See ioc_batching, ioc_set_batching
1141 */
Tejun Heoa06e05e2012-06-04 20:40:55 -07001142 ioc_set_batching(q, current->io_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Tejun Heoa06e05e2012-06-04 20:40:55 -07001144 spin_lock_irq(q->queue_lock);
1145 finish_wait(&rl->wait[is_sync], &wait);
Jens Axboe2056a782006-03-23 20:00:26 +01001146
Tejun Heoa06e05e2012-06-04 20:40:55 -07001147 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148}
1149
Jens Axboe320ae512013-10-24 09:20:05 +01001150static struct request *blk_old_get_request(struct request_queue *q, int rw,
1151 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
1153 struct request *rq;
1154
1155 BUG_ON(rw != READ && rw != WRITE);
1156
Tejun Heo7f4b35d2012-06-04 20:40:56 -07001157 /* create ioc upfront */
1158 create_io_context(gfp_mask, q->node);
1159
Nick Piggind6344532005-06-28 20:45:14 -07001160 spin_lock_irq(q->queue_lock);
Tejun Heoa06e05e2012-06-04 20:40:55 -07001161 rq = get_request(q, rw, NULL, gfp_mask);
Joe Lawrencea492f072014-08-28 08:15:21 -06001162 if (IS_ERR(rq))
Tejun Heoda8303c2011-10-19 14:33:05 +02001163 spin_unlock_irq(q->queue_lock);
Nick Piggind6344532005-06-28 20:45:14 -07001164 /* q->queue_lock is unlocked at this point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166 return rq;
1167}
Jens Axboe320ae512013-10-24 09:20:05 +01001168
1169struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
1170{
1171 if (q->mq_ops)
Christoph Hellwig4ce01dd2014-05-27 20:59:46 +02001172 return blk_mq_alloc_request(q, rw, gfp_mask, false);
Jens Axboe320ae512013-10-24 09:20:05 +01001173 else
1174 return blk_old_get_request(q, rw, gfp_mask);
1175}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176EXPORT_SYMBOL(blk_get_request);
1177
1178/**
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001179 * blk_make_request - given a bio, allocate a corresponding struct request.
Randy Dunlap8ebf9752009-06-11 20:00:41 -07001180 * @q: target request queue
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001181 * @bio: The bio describing the memory mappings that will be submitted for IO.
1182 * It may be a chained-bio properly constructed by block/bio layer.
Randy Dunlap8ebf9752009-06-11 20:00:41 -07001183 * @gfp_mask: gfp flags to be used for memory allocation
Jens Axboedc72ef42006-07-20 14:54:05 +02001184 *
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001185 * blk_make_request is the parallel of generic_make_request for BLOCK_PC
1186 * type commands. Where the struct request needs to be farther initialized by
1187 * the caller. It is passed a &struct bio, which describes the memory info of
1188 * the I/O transfer.
1189 *
1190 * The caller of blk_make_request must make sure that bi_io_vec
1191 * are set to describe the memory buffers. That bio_data_dir() will return
1192 * the needed direction of the request. (And all bio's in the passed bio-chain
1193 * are properly set accordingly)
1194 *
1195 * If called under none-sleepable conditions, mapped bio buffers must not
1196 * need bouncing, by calling the appropriate masked or flagged allocator,
1197 * suitable for the target device. Otherwise the call to blk_queue_bounce will
1198 * BUG.
Jens Axboe53674ac2009-05-19 19:52:35 +02001199 *
1200 * WARNING: When allocating/cloning a bio-chain, careful consideration should be
1201 * given to how you allocate bios. In particular, you cannot use __GFP_WAIT for
1202 * anything but the first bio in the chain. Otherwise you risk waiting for IO
1203 * completion of a bio that hasn't been submitted yet, thus resulting in a
1204 * deadlock. Alternatively bios should be allocated using bio_kmalloc() instead
1205 * of bio_alloc(), as that avoids the mempool deadlock.
1206 * If possible a big IO should be split into smaller parts when allocation
1207 * fails. Partial allocation should not be an error, or you risk a live-lock.
Jens Axboedc72ef42006-07-20 14:54:05 +02001208 */
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001209struct request *blk_make_request(struct request_queue *q, struct bio *bio,
1210 gfp_t gfp_mask)
Jens Axboedc72ef42006-07-20 14:54:05 +02001211{
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001212 struct request *rq = blk_get_request(q, bio_data_dir(bio), gfp_mask);
1213
Joe Lawrencea492f072014-08-28 08:15:21 -06001214 if (IS_ERR(rq))
1215 return rq;
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001216
Jens Axboef27b0872014-06-06 07:57:37 -06001217 blk_rq_set_block_pc(rq);
1218
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001219 for_each_bio(bio) {
1220 struct bio *bounce_bio = bio;
1221 int ret;
1222
1223 blk_queue_bounce(q, &bounce_bio);
1224 ret = blk_rq_append_bio(q, rq, bounce_bio);
1225 if (unlikely(ret)) {
1226 blk_put_request(rq);
1227 return ERR_PTR(ret);
1228 }
1229 }
1230
1231 return rq;
Jens Axboedc72ef42006-07-20 14:54:05 +02001232}
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001233EXPORT_SYMBOL(blk_make_request);
Jens Axboedc72ef42006-07-20 14:54:05 +02001234
1235/**
Jens Axboef27b0872014-06-06 07:57:37 -06001236 * blk_rq_set_block_pc - initialize a requeest to type BLOCK_PC
1237 * @rq: request to be initialized
1238 *
1239 */
1240void blk_rq_set_block_pc(struct request *rq)
1241{
1242 rq->cmd_type = REQ_TYPE_BLOCK_PC;
1243 rq->__data_len = 0;
1244 rq->__sector = (sector_t) -1;
1245 rq->bio = rq->biotail = NULL;
1246 memset(rq->__cmd, 0, sizeof(rq->__cmd));
Jens Axboef27b0872014-06-06 07:57:37 -06001247}
1248EXPORT_SYMBOL(blk_rq_set_block_pc);
1249
1250/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 * blk_requeue_request - put a request back on queue
1252 * @q: request queue where request should be inserted
1253 * @rq: request to be inserted
1254 *
1255 * Description:
1256 * Drivers often keep queueing requests until the hardware cannot accept
1257 * more, when that condition happens we need to put the request back
1258 * on the queue. Must be called with queue lock held.
1259 */
Jens Axboe165125e2007-07-24 09:28:11 +02001260void blk_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
Jens Axboe242f9dc2008-09-14 05:55:09 -07001262 blk_delete_timer(rq);
1263 blk_clear_rq_complete(rq);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001264 trace_block_rq_requeue(q, rq);
Jens Axboe2056a782006-03-23 20:00:26 +01001265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 if (blk_rq_tagged(rq))
1267 blk_queue_end_tag(q, rq);
1268
James Bottomleyba396a62009-05-27 14:17:08 +02001269 BUG_ON(blk_queued_rq(rq));
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 elv_requeue_request(q, rq);
1272}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273EXPORT_SYMBOL(blk_requeue_request);
1274
Jens Axboe73c10102011-03-08 13:19:51 +01001275static void add_acct_request(struct request_queue *q, struct request *rq,
1276 int where)
1277{
Jens Axboe320ae512013-10-24 09:20:05 +01001278 blk_account_io_start(rq, true);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001279 __elv_add_request(q, rq, where);
Jens Axboe73c10102011-03-08 13:19:51 +01001280}
1281
Tejun Heo074a7ac2008-08-25 19:56:14 +09001282static void part_round_stats_single(int cpu, struct hd_struct *part,
1283 unsigned long now)
1284{
Jens Axboe7276d022014-05-09 15:48:23 -06001285 int inflight;
1286
Tejun Heo074a7ac2008-08-25 19:56:14 +09001287 if (now == part->stamp)
1288 return;
1289
Jens Axboe7276d022014-05-09 15:48:23 -06001290 inflight = part_in_flight(part);
1291 if (inflight) {
Tejun Heo074a7ac2008-08-25 19:56:14 +09001292 __part_stat_add(cpu, part, time_in_queue,
Jens Axboe7276d022014-05-09 15:48:23 -06001293 inflight * (now - part->stamp));
Tejun Heo074a7ac2008-08-25 19:56:14 +09001294 __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
1295 }
1296 part->stamp = now;
1297}
1298
1299/**
Randy Dunlap496aa8a2008-10-16 07:46:23 +02001300 * part_round_stats() - Round off the performance stats on a struct disk_stats.
1301 * @cpu: cpu number for stats access
1302 * @part: target partition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 *
1304 * The average IO queue length and utilisation statistics are maintained
1305 * by observing the current state of the queue length and the amount of
1306 * time it has been in this state for.
1307 *
1308 * Normally, that accounting is done on IO completion, but that can result
1309 * in more than a second's worth of IO being accounted for within any one
1310 * second, leading to >100% utilisation. To deal with that, we call this
1311 * function to do a round-off before returning the results when reading
1312 * /proc/diskstats. This accounts immediately for all queue usage up to
1313 * the current jiffies and restarts the counters again.
1314 */
Tejun Heoc9959052008-08-25 19:47:21 +09001315void part_round_stats(int cpu, struct hd_struct *part)
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001316{
1317 unsigned long now = jiffies;
1318
Tejun Heo074a7ac2008-08-25 19:56:14 +09001319 if (part->partno)
1320 part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
1321 part_round_stats_single(cpu, part, now);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001322}
Tejun Heo074a7ac2008-08-25 19:56:14 +09001323EXPORT_SYMBOL_GPL(part_round_stats);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001324
Lin Mingc8158812013-03-23 11:42:27 +08001325#ifdef CONFIG_PM_RUNTIME
1326static void blk_pm_put_request(struct request *rq)
1327{
1328 if (rq->q->dev && !(rq->cmd_flags & REQ_PM) && !--rq->q->nr_pending)
1329 pm_runtime_mark_last_busy(rq->q->dev);
1330}
1331#else
1332static inline void blk_pm_put_request(struct request *rq) {}
1333#endif
1334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335/*
1336 * queue lock must be held
1337 */
Jens Axboe165125e2007-07-24 09:28:11 +02001338void __blk_put_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 if (unlikely(!q))
1341 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Christoph Hellwig6f5ba582014-02-07 10:22:37 -08001343 if (q->mq_ops) {
1344 blk_mq_free_request(req);
1345 return;
1346 }
1347
Lin Mingc8158812013-03-23 11:42:27 +08001348 blk_pm_put_request(req);
1349
Tejun Heo8922e162005-10-20 16:23:44 +02001350 elv_completed_request(q, req);
1351
Boaz Harrosh1cd96c22009-03-24 12:35:07 +01001352 /* this is a bio leak */
1353 WARN_ON(req->bio != NULL);
1354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 /*
1356 * Request may not have originated from ll_rw_blk. if not,
1357 * it didn't come out of our reserved rq pools
1358 */
Jens Axboe49171e52006-08-10 08:59:11 +02001359 if (req->cmd_flags & REQ_ALLOCED) {
Tejun Heo75eb6c32011-10-19 14:31:22 +02001360 unsigned int flags = req->cmd_flags;
Tejun Heoa0516612012-06-26 15:05:44 -07001361 struct request_list *rl = blk_rq_rl(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 BUG_ON(!list_empty(&req->queuelist));
Jens Axboe360f92c2014-04-09 20:27:01 -06001364 BUG_ON(ELV_ON_HASH(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Tejun Heoa0516612012-06-26 15:05:44 -07001366 blk_free_request(rl, req);
1367 freed_request(rl, flags);
1368 blk_put_rl(rl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 }
1370}
Mike Christie6e39b69e2005-11-11 05:30:24 -06001371EXPORT_SYMBOL_GPL(__blk_put_request);
1372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373void blk_put_request(struct request *req)
1374{
Jens Axboe165125e2007-07-24 09:28:11 +02001375 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Jens Axboe320ae512013-10-24 09:20:05 +01001377 if (q->mq_ops)
1378 blk_mq_free_request(req);
1379 else {
1380 unsigned long flags;
1381
1382 spin_lock_irqsave(q->queue_lock, flags);
1383 __blk_put_request(q, req);
1384 spin_unlock_irqrestore(q->queue_lock, flags);
1385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387EXPORT_SYMBOL(blk_put_request);
1388
Christoph Hellwig66ac0282010-06-18 16:59:42 +02001389/**
1390 * blk_add_request_payload - add a payload to a request
1391 * @rq: request to update
1392 * @page: page backing the payload
1393 * @len: length of the payload.
1394 *
1395 * This allows to later add a payload to an already submitted request by
1396 * a block driver. The driver needs to take care of freeing the payload
1397 * itself.
1398 *
1399 * Note that this is a quite horrible hack and nothing but handling of
1400 * discard requests should ever use it.
1401 */
1402void blk_add_request_payload(struct request *rq, struct page *page,
1403 unsigned int len)
1404{
1405 struct bio *bio = rq->bio;
1406
1407 bio->bi_io_vec->bv_page = page;
1408 bio->bi_io_vec->bv_offset = 0;
1409 bio->bi_io_vec->bv_len = len;
1410
Kent Overstreet4f024f32013-10-11 15:44:27 -07001411 bio->bi_iter.bi_size = len;
Christoph Hellwig66ac0282010-06-18 16:59:42 +02001412 bio->bi_vcnt = 1;
1413 bio->bi_phys_segments = 1;
1414
1415 rq->__data_len = rq->resid_len = len;
1416 rq->nr_phys_segments = 1;
Christoph Hellwig66ac0282010-06-18 16:59:42 +02001417}
1418EXPORT_SYMBOL_GPL(blk_add_request_payload);
1419
Jens Axboe320ae512013-10-24 09:20:05 +01001420bool bio_attempt_back_merge(struct request_queue *q, struct request *req,
1421 struct bio *bio)
Jens Axboe73c10102011-03-08 13:19:51 +01001422{
1423 const int ff = bio->bi_rw & REQ_FAILFAST_MASK;
1424
Jens Axboe73c10102011-03-08 13:19:51 +01001425 if (!ll_back_merge_fn(q, req, bio))
1426 return false;
1427
Tejun Heo8c1cf6b2013-01-11 13:06:34 -08001428 trace_block_bio_backmerge(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001429
1430 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1431 blk_rq_set_mixed_merge(req);
1432
1433 req->biotail->bi_next = bio;
1434 req->biotail = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001435 req->__data_len += bio->bi_iter.bi_size;
Jens Axboe73c10102011-03-08 13:19:51 +01001436 req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
1437
Jens Axboe320ae512013-10-24 09:20:05 +01001438 blk_account_io_start(req, false);
Jens Axboe73c10102011-03-08 13:19:51 +01001439 return true;
1440}
1441
Jens Axboe320ae512013-10-24 09:20:05 +01001442bool bio_attempt_front_merge(struct request_queue *q, struct request *req,
1443 struct bio *bio)
Jens Axboe73c10102011-03-08 13:19:51 +01001444{
1445 const int ff = bio->bi_rw & REQ_FAILFAST_MASK;
Jens Axboe73c10102011-03-08 13:19:51 +01001446
Jens Axboe73c10102011-03-08 13:19:51 +01001447 if (!ll_front_merge_fn(q, req, bio))
1448 return false;
1449
Tejun Heo8c1cf6b2013-01-11 13:06:34 -08001450 trace_block_bio_frontmerge(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001451
1452 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1453 blk_rq_set_mixed_merge(req);
1454
Jens Axboe73c10102011-03-08 13:19:51 +01001455 bio->bi_next = req->bio;
1456 req->bio = bio;
1457
Kent Overstreet4f024f32013-10-11 15:44:27 -07001458 req->__sector = bio->bi_iter.bi_sector;
1459 req->__data_len += bio->bi_iter.bi_size;
Jens Axboe73c10102011-03-08 13:19:51 +01001460 req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
1461
Jens Axboe320ae512013-10-24 09:20:05 +01001462 blk_account_io_start(req, false);
Jens Axboe73c10102011-03-08 13:19:51 +01001463 return true;
1464}
1465
Tejun Heobd87b582011-10-19 14:33:08 +02001466/**
Jens Axboe320ae512013-10-24 09:20:05 +01001467 * blk_attempt_plug_merge - try to merge with %current's plugged list
Tejun Heobd87b582011-10-19 14:33:08 +02001468 * @q: request_queue new bio is being queued at
1469 * @bio: new bio being queued
1470 * @request_count: out parameter for number of traversed plugged requests
1471 *
1472 * Determine whether @bio being queued on @q can be merged with a request
1473 * on %current's plugged list. Returns %true if merge was successful,
1474 * otherwise %false.
1475 *
Tejun Heo07c2bd32012-02-08 09:19:42 +01001476 * Plugging coalesces IOs from the same issuer for the same purpose without
1477 * going through @q->queue_lock. As such it's more of an issuing mechanism
1478 * than scheduling, and the request, while may have elvpriv data, is not
1479 * added on the elevator at this point. In addition, we don't have
1480 * reliable access to the elevator outside queue lock. Only check basic
1481 * merging parameters without querying the elevator.
Robert Elliottda41a582014-05-20 16:46:26 -05001482 *
1483 * Caller must ensure !blk_queue_nomerges(q) beforehand.
Jens Axboe73c10102011-03-08 13:19:51 +01001484 */
Jens Axboe320ae512013-10-24 09:20:05 +01001485bool blk_attempt_plug_merge(struct request_queue *q, struct bio *bio,
1486 unsigned int *request_count)
Jens Axboe73c10102011-03-08 13:19:51 +01001487{
1488 struct blk_plug *plug;
1489 struct request *rq;
1490 bool ret = false;
Shaohua Li92f399c2013-10-29 12:01:03 -06001491 struct list_head *plug_list;
Jens Axboe73c10102011-03-08 13:19:51 +01001492
Tejun Heobd87b582011-10-19 14:33:08 +02001493 plug = current->plug;
Jens Axboe73c10102011-03-08 13:19:51 +01001494 if (!plug)
1495 goto out;
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001496 *request_count = 0;
Jens Axboe73c10102011-03-08 13:19:51 +01001497
Shaohua Li92f399c2013-10-29 12:01:03 -06001498 if (q->mq_ops)
1499 plug_list = &plug->mq_list;
1500 else
1501 plug_list = &plug->list;
1502
1503 list_for_each_entry_reverse(rq, plug_list, queuelist) {
Jens Axboe73c10102011-03-08 13:19:51 +01001504 int el_ret;
1505
Shaohua Li1b2e19f2012-04-06 11:37:47 -06001506 if (rq->q == q)
1507 (*request_count)++;
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001508
Tejun Heo07c2bd32012-02-08 09:19:42 +01001509 if (rq->q != q || !blk_rq_merge_ok(rq, bio))
Jens Axboe73c10102011-03-08 13:19:51 +01001510 continue;
1511
Tejun Heo050c8ea2012-02-08 09:19:38 +01001512 el_ret = blk_try_merge(rq, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001513 if (el_ret == ELEVATOR_BACK_MERGE) {
1514 ret = bio_attempt_back_merge(q, rq, bio);
1515 if (ret)
1516 break;
1517 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
1518 ret = bio_attempt_front_merge(q, rq, bio);
1519 if (ret)
1520 break;
1521 }
1522 }
1523out:
1524 return ret;
1525}
1526
Jens Axboe86db1e22008-01-29 14:53:40 +01001527void init_request_from_bio(struct request *req, struct bio *bio)
Tejun Heo52d9e672006-01-06 09:49:58 +01001528{
Jens Axboe4aff5e22006-08-10 08:44:47 +02001529 req->cmd_type = REQ_TYPE_FS;
Tejun Heo52d9e672006-01-06 09:49:58 +01001530
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001531 req->cmd_flags |= bio->bi_rw & REQ_COMMON_MASK;
1532 if (bio->bi_rw & REQ_RAHEAD)
Tejun Heoa82afdf2009-07-03 17:48:16 +09001533 req->cmd_flags |= REQ_FAILFAST_MASK;
Jens Axboeb31dc662006-06-13 08:26:10 +02001534
Tejun Heo52d9e672006-01-06 09:49:58 +01001535 req->errors = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001536 req->__sector = bio->bi_iter.bi_sector;
Tejun Heo52d9e672006-01-06 09:49:58 +01001537 req->ioprio = bio_prio(bio);
NeilBrownbc1c56f2007-08-16 13:31:30 +02001538 blk_rq_bio_prep(req->q, req, bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001539}
1540
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001541void blk_queue_bio(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
Jiri Slaby5e00d1b2010-08-12 14:31:06 +02001543 const bool sync = !!(bio->bi_rw & REQ_SYNC);
Jens Axboe73c10102011-03-08 13:19:51 +01001544 struct blk_plug *plug;
1545 int el_ret, rw_flags, where = ELEVATOR_INSERT_SORT;
1546 struct request *req;
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001547 unsigned int request_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 /*
1550 * low level driver can indicate that it wants pages above a
1551 * certain limit bounced to low memory (ie for highmem, or even
1552 * ISA dma in theory)
1553 */
1554 blk_queue_bounce(q, &bio);
1555
Darrick J. Wongffecfd12013-02-21 16:42:55 -08001556 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1557 bio_endio(bio, -EIO);
1558 return;
1559 }
1560
Tejun Heo4fed9472010-09-03 11:56:17 +02001561 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) {
Jens Axboe73c10102011-03-08 13:19:51 +01001562 spin_lock_irq(q->queue_lock);
Tejun Heoae1b1532011-01-25 12:43:54 +01001563 where = ELEVATOR_INSERT_FLUSH;
Tejun Heo28e7d182010-09-03 11:56:16 +02001564 goto get_rq;
1565 }
1566
Jens Axboe73c10102011-03-08 13:19:51 +01001567 /*
1568 * Check if we can merge with the plugged list before grabbing
1569 * any locks.
1570 */
Robert Elliottda41a582014-05-20 16:46:26 -05001571 if (!blk_queue_nomerges(q) &&
1572 blk_attempt_plug_merge(q, bio, &request_count))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001573 return;
Jens Axboe73c10102011-03-08 13:19:51 +01001574
1575 spin_lock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577 el_ret = elv_merge(q, &req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001578 if (el_ret == ELEVATOR_BACK_MERGE) {
Jens Axboe73c10102011-03-08 13:19:51 +01001579 if (bio_attempt_back_merge(q, req, bio)) {
Tejun Heo07c2bd32012-02-08 09:19:42 +01001580 elv_bio_merged(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001581 if (!attempt_back_merge(q, req))
1582 elv_merged_request(q, req, el_ret);
1583 goto out_unlock;
Tejun Heo80a761f2009-07-03 17:48:17 +09001584 }
Jens Axboe73c10102011-03-08 13:19:51 +01001585 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
Jens Axboe73c10102011-03-08 13:19:51 +01001586 if (bio_attempt_front_merge(q, req, bio)) {
Tejun Heo07c2bd32012-02-08 09:19:42 +01001587 elv_bio_merged(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001588 if (!attempt_front_merge(q, req))
1589 elv_merged_request(q, req, el_ret);
1590 goto out_unlock;
1591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 }
1593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594get_rq:
Nick Piggin450991b2005-06-28 20:45:13 -07001595 /*
Jens Axboe7749a8d2006-12-13 13:02:26 +01001596 * This sync check and mask will be re-done in init_request_from_bio(),
1597 * but we need to set it earlier to expose the sync flag to the
1598 * rq allocator and io schedulers.
1599 */
1600 rw_flags = bio_data_dir(bio);
1601 if (sync)
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001602 rw_flags |= REQ_SYNC;
Jens Axboe7749a8d2006-12-13 13:02:26 +01001603
1604 /*
Nick Piggin450991b2005-06-28 20:45:13 -07001605 * Grab a free request. This is might sleep but can not fail.
Nick Piggind6344532005-06-28 20:45:14 -07001606 * Returns with the queue unlocked.
Nick Piggin450991b2005-06-28 20:45:13 -07001607 */
Tejun Heoa06e05e2012-06-04 20:40:55 -07001608 req = get_request(q, rw_flags, bio, GFP_NOIO);
Joe Lawrencea492f072014-08-28 08:15:21 -06001609 if (IS_ERR(req)) {
1610 bio_endio(bio, PTR_ERR(req)); /* @q is dead */
Tejun Heoda8303c2011-10-19 14:33:05 +02001611 goto out_unlock;
1612 }
Nick Piggind6344532005-06-28 20:45:14 -07001613
Nick Piggin450991b2005-06-28 20:45:13 -07001614 /*
1615 * After dropping the lock and possibly sleeping here, our request
1616 * may now be mergeable after it had proven unmergeable (above).
1617 * We don't worry about that case for efficiency. It won't happen
1618 * often, and the elevators are able to handle it.
1619 */
Tejun Heo52d9e672006-01-06 09:49:58 +01001620 init_request_from_bio(req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Tao Ma9562ad92011-10-24 16:11:30 +02001622 if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags))
Jens Axboe11ccf112011-07-26 15:01:15 +02001623 req->cpu = raw_smp_processor_id();
Tejun Heodd831002010-09-03 11:56:16 +02001624
Jens Axboe73c10102011-03-08 13:19:51 +01001625 plug = current->plug;
Jens Axboe721a9602011-03-09 11:56:30 +01001626 if (plug) {
Jens Axboedc6d36c2011-04-12 10:28:28 +02001627 /*
1628 * If this is the first request added after a plug, fire
Jianpeng Ma7aef2e72013-09-11 13:21:07 -06001629 * of a plug trace.
Jens Axboedc6d36c2011-04-12 10:28:28 +02001630 */
Jianpeng Ma7aef2e72013-09-11 13:21:07 -06001631 if (!request_count)
Jens Axboedc6d36c2011-04-12 10:28:28 +02001632 trace_block_plug(q);
Shaohua Li3540d5e2011-11-16 09:21:50 +01001633 else {
Shaohua Li019ceb72011-11-16 09:21:50 +01001634 if (request_count >= BLK_MAX_REQUEST_COUNT) {
Shaohua Li3540d5e2011-11-16 09:21:50 +01001635 blk_flush_plug_list(plug, false);
Shaohua Li019ceb72011-11-16 09:21:50 +01001636 trace_block_plug(q);
1637 }
Jens Axboe73c10102011-03-08 13:19:51 +01001638 }
Shaohua Lia6327162011-08-24 16:04:32 +02001639 list_add_tail(&req->queuelist, &plug->list);
Jens Axboe320ae512013-10-24 09:20:05 +01001640 blk_account_io_start(req, true);
Jens Axboe73c10102011-03-08 13:19:51 +01001641 } else {
1642 spin_lock_irq(q->queue_lock);
1643 add_acct_request(q, req, where);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02001644 __blk_run_queue(q);
Jens Axboe73c10102011-03-08 13:19:51 +01001645out_unlock:
1646 spin_unlock_irq(q->queue_lock);
1647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648}
Jens Axboec20e8de2011-09-12 12:03:37 +02001649EXPORT_SYMBOL_GPL(blk_queue_bio); /* for device mapper only */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
1651/*
1652 * If bio->bi_dev is a partition, remap the location
1653 */
1654static inline void blk_partition_remap(struct bio *bio)
1655{
1656 struct block_device *bdev = bio->bi_bdev;
1657
Jens Axboebf2de6f2007-09-27 13:01:25 +02001658 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 struct hd_struct *p = bdev->bd_part;
Jens Axboea3623572005-11-01 09:26:16 +01001660
Kent Overstreet4f024f32013-10-11 15:44:27 -07001661 bio->bi_iter.bi_sector += p->start_sect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 bio->bi_bdev = bdev->bd_contains;
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001663
Mike Snitzerd07335e2010-11-16 12:52:38 +01001664 trace_block_bio_remap(bdev_get_queue(bio->bi_bdev), bio,
1665 bdev->bd_dev,
Kent Overstreet4f024f32013-10-11 15:44:27 -07001666 bio->bi_iter.bi_sector - p->start_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
1668}
1669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670static void handle_bad_sector(struct bio *bio)
1671{
1672 char b[BDEVNAME_SIZE];
1673
1674 printk(KERN_INFO "attempt to access beyond end of device\n");
1675 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1676 bdevname(bio->bi_bdev, b),
1677 bio->bi_rw,
Kent Overstreetf73a1c72012-09-25 15:05:12 -07001678 (unsigned long long)bio_end_sector(bio),
Mike Snitzer77304d22010-11-08 14:39:12 +01001679 (long long)(i_size_read(bio->bi_bdev->bd_inode) >> 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681 set_bit(BIO_EOF, &bio->bi_flags);
1682}
1683
Akinobu Mitac17bb492006-12-08 02:39:46 -08001684#ifdef CONFIG_FAIL_MAKE_REQUEST
1685
1686static DECLARE_FAULT_ATTR(fail_make_request);
1687
1688static int __init setup_fail_make_request(char *str)
1689{
1690 return setup_fault_attr(&fail_make_request, str);
1691}
1692__setup("fail_make_request=", setup_fail_make_request);
1693
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001694static bool should_fail_request(struct hd_struct *part, unsigned int bytes)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001695{
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001696 return part->make_it_fail && should_fail(&fail_make_request, bytes);
Akinobu Mitac17bb492006-12-08 02:39:46 -08001697}
1698
1699static int __init fail_make_request_debugfs(void)
1700{
Akinobu Mitadd48c082011-08-03 16:21:01 -07001701 struct dentry *dir = fault_create_debugfs_attr("fail_make_request",
1702 NULL, &fail_make_request);
1703
Duan Jiong21f9fcd2014-04-11 15:58:56 +08001704 return PTR_ERR_OR_ZERO(dir);
Akinobu Mitac17bb492006-12-08 02:39:46 -08001705}
1706
1707late_initcall(fail_make_request_debugfs);
1708
1709#else /* CONFIG_FAIL_MAKE_REQUEST */
1710
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001711static inline bool should_fail_request(struct hd_struct *part,
1712 unsigned int bytes)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001713{
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001714 return false;
Akinobu Mitac17bb492006-12-08 02:39:46 -08001715}
1716
1717#endif /* CONFIG_FAIL_MAKE_REQUEST */
1718
Jens Axboec07e2b42007-07-18 13:27:58 +02001719/*
1720 * Check whether this bio extends beyond the end of the device.
1721 */
1722static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1723{
1724 sector_t maxsector;
1725
1726 if (!nr_sectors)
1727 return 0;
1728
1729 /* Test device or partition size, when known. */
Mike Snitzer77304d22010-11-08 14:39:12 +01001730 maxsector = i_size_read(bio->bi_bdev->bd_inode) >> 9;
Jens Axboec07e2b42007-07-18 13:27:58 +02001731 if (maxsector) {
Kent Overstreet4f024f32013-10-11 15:44:27 -07001732 sector_t sector = bio->bi_iter.bi_sector;
Jens Axboec07e2b42007-07-18 13:27:58 +02001733
1734 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1735 /*
1736 * This may well happen - the kernel calls bread()
1737 * without checking the size of the device, e.g., when
1738 * mounting a device.
1739 */
1740 handle_bad_sector(bio);
1741 return 1;
1742 }
1743 }
1744
1745 return 0;
1746}
1747
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001748static noinline_for_stack bool
1749generic_make_request_checks(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
Jens Axboe165125e2007-07-24 09:28:11 +02001751 struct request_queue *q;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001752 int nr_sectors = bio_sectors(bio);
Jens Axboe51fd77b2007-11-02 08:49:08 +01001753 int err = -EIO;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001754 char b[BDEVNAME_SIZE];
1755 struct hd_struct *part;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Jens Axboec07e2b42007-07-18 13:27:58 +02001759 if (bio_check_eod(bio, nr_sectors))
1760 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001762 q = bdev_get_queue(bio->bi_bdev);
1763 if (unlikely(!q)) {
1764 printk(KERN_ERR
1765 "generic_make_request: Trying to access "
1766 "nonexistent block-device %s (%Lu)\n",
1767 bdevname(bio->bi_bdev, b),
Kent Overstreet4f024f32013-10-11 15:44:27 -07001768 (long long) bio->bi_iter.bi_sector);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001769 goto end_io;
1770 }
1771
Martin K. Petersene2a60da2012-09-18 12:19:25 -04001772 if (likely(bio_is_rw(bio) &&
1773 nr_sectors > queue_max_hw_sectors(q))) {
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001774 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
1775 bdevname(bio->bi_bdev, b),
1776 bio_sectors(bio),
1777 queue_max_hw_sectors(q));
1778 goto end_io;
1779 }
1780
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001781 part = bio->bi_bdev->bd_part;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001782 if (should_fail_request(part, bio->bi_iter.bi_size) ||
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001783 should_fail_request(&part_to_disk(part)->part0,
Kent Overstreet4f024f32013-10-11 15:44:27 -07001784 bio->bi_iter.bi_size))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001785 goto end_io;
1786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 /*
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001788 * If this device has partitions, remap block n
1789 * of partition p to block n+start(p) of the disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001791 blk_partition_remap(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001793 if (bio_check_eod(bio, nr_sectors))
1794 goto end_io;
1795
1796 /*
1797 * Filter flush bio's early so that make_request based
1798 * drivers without flush support don't have to worry
1799 * about them.
1800 */
1801 if ((bio->bi_rw & (REQ_FLUSH | REQ_FUA)) && !q->flush_flags) {
1802 bio->bi_rw &= ~(REQ_FLUSH | REQ_FUA);
1803 if (!nr_sectors) {
1804 err = 0;
Tejun Heoa7384672008-11-28 13:32:03 +09001805 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 }
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001809 if ((bio->bi_rw & REQ_DISCARD) &&
1810 (!blk_queue_discard(q) ||
Martin K. Petersene2a60da2012-09-18 12:19:25 -04001811 ((bio->bi_rw & REQ_SECURE) && !blk_queue_secdiscard(q)))) {
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001812 err = -EOPNOTSUPP;
1813 goto end_io;
1814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Martin K. Petersen4363ac72012-09-18 12:19:27 -04001816 if (bio->bi_rw & REQ_WRITE_SAME && !bdev_write_same(bio->bi_bdev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 err = -EOPNOTSUPP;
1818 goto end_io;
1819 }
1820
Tejun Heo7f4b35d2012-06-04 20:40:56 -07001821 /*
1822 * Various block parts want %current->io_context and lazy ioc
1823 * allocation ends up trading a lot of pain for a small amount of
1824 * memory. Just allocate it upfront. This may fail and block
1825 * layer knows how to live with it.
1826 */
1827 create_io_context(GFP_ATOMIC, q->node);
1828
Tejun Heobc16a4f2011-10-19 14:33:01 +02001829 if (blk_throtl_bio(q, bio))
1830 return false; /* throttled, will be resubmitted later */
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001831
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001832 trace_block_bio_queue(q, bio);
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001833 return true;
Tejun Heoa7384672008-11-28 13:32:03 +09001834
1835end_io:
1836 bio_endio(bio, err);
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001837 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838}
1839
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001840/**
1841 * generic_make_request - hand a buffer to its device driver for I/O
1842 * @bio: The bio describing the location in memory and on the device.
1843 *
1844 * generic_make_request() is used to make I/O requests of block
1845 * devices. It is passed a &struct bio, which describes the I/O that needs
1846 * to be done.
1847 *
1848 * generic_make_request() does not return any status. The
1849 * success/failure status of the request, along with notification of
1850 * completion, is delivered asynchronously through the bio->bi_end_io
1851 * function described (one day) else where.
1852 *
1853 * The caller of generic_make_request must make sure that bi_io_vec
1854 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1855 * set to describe the device address, and the
1856 * bi_end_io and optionally bi_private are set to describe how
1857 * completion notification should be signaled.
1858 *
1859 * generic_make_request and the drivers it calls may use bi_next if this
1860 * bio happens to be merged with someone else, and may resubmit the bio to
1861 * a lower device by calling into generic_make_request recursively, which
1862 * means the bio should NOT be touched after the call to ->make_request_fn.
Neil Brownd89d8792007-05-01 09:53:42 +02001863 */
1864void generic_make_request(struct bio *bio)
1865{
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001866 struct bio_list bio_list_on_stack;
1867
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001868 if (!generic_make_request_checks(bio))
1869 return;
1870
1871 /*
1872 * We only want one ->make_request_fn to be active at a time, else
1873 * stack usage with stacked devices could be a problem. So use
1874 * current->bio_list to keep a list of requests submited by a
1875 * make_request_fn function. current->bio_list is also used as a
1876 * flag to say if generic_make_request is currently active in this
1877 * task or not. If it is NULL, then no make_request is active. If
1878 * it is non-NULL, then a make_request is active, and new requests
1879 * should be added at the tail
1880 */
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001881 if (current->bio_list) {
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001882 bio_list_add(current->bio_list, bio);
Neil Brownd89d8792007-05-01 09:53:42 +02001883 return;
1884 }
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001885
Neil Brownd89d8792007-05-01 09:53:42 +02001886 /* following loop may be a bit non-obvious, and so deserves some
1887 * explanation.
1888 * Before entering the loop, bio->bi_next is NULL (as all callers
1889 * ensure that) so we have a list with a single bio.
1890 * We pretend that we have just taken it off a longer list, so
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001891 * we assign bio_list to a pointer to the bio_list_on_stack,
1892 * thus initialising the bio_list of new bios to be
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001893 * added. ->make_request() may indeed add some more bios
Neil Brownd89d8792007-05-01 09:53:42 +02001894 * through a recursive call to generic_make_request. If it
1895 * did, we find a non-NULL value in bio_list and re-enter the loop
1896 * from the top. In this case we really did just take the bio
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001897 * of the top of the list (no pretending) and so remove it from
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001898 * bio_list, and call into ->make_request() again.
Neil Brownd89d8792007-05-01 09:53:42 +02001899 */
1900 BUG_ON(bio->bi_next);
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001901 bio_list_init(&bio_list_on_stack);
1902 current->bio_list = &bio_list_on_stack;
Neil Brownd89d8792007-05-01 09:53:42 +02001903 do {
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001904 struct request_queue *q = bdev_get_queue(bio->bi_bdev);
1905
1906 q->make_request_fn(q, bio);
1907
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001908 bio = bio_list_pop(current->bio_list);
Neil Brownd89d8792007-05-01 09:53:42 +02001909 } while (bio);
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001910 current->bio_list = NULL; /* deactivate */
Neil Brownd89d8792007-05-01 09:53:42 +02001911}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912EXPORT_SYMBOL(generic_make_request);
1913
1914/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001915 * submit_bio - submit a bio to the block device layer for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1917 * @bio: The &struct bio which describes the I/O
1918 *
1919 * submit_bio() is very similar in purpose to generic_make_request(), and
1920 * uses that function to do most of the work. Both are fairly rough
Randy Dunlap710027a2008-08-19 20:13:11 +02001921 * interfaces; @bio must be presetup and ready for I/O.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 *
1923 */
1924void submit_bio(int rw, struct bio *bio)
1925{
Jens Axboe22e2c502005-06-27 10:55:12 +02001926 bio->bi_rw |= rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Jens Axboebf2de6f2007-09-27 13:01:25 +02001928 /*
1929 * If it's a regular read/write or a barrier with data attached,
1930 * go through the normal accounting stuff before submission.
1931 */
Martin K. Petersene2a60da2012-09-18 12:19:25 -04001932 if (bio_has_data(bio)) {
Martin K. Petersen4363ac72012-09-18 12:19:27 -04001933 unsigned int count;
1934
1935 if (unlikely(rw & REQ_WRITE_SAME))
1936 count = bdev_logical_block_size(bio->bi_bdev) >> 9;
1937 else
1938 count = bio_sectors(bio);
1939
Jens Axboebf2de6f2007-09-27 13:01:25 +02001940 if (rw & WRITE) {
1941 count_vm_events(PGPGOUT, count);
1942 } else {
Kent Overstreet4f024f32013-10-11 15:44:27 -07001943 task_io_account_read(bio->bi_iter.bi_size);
Jens Axboebf2de6f2007-09-27 13:01:25 +02001944 count_vm_events(PGPGIN, count);
1945 }
1946
1947 if (unlikely(block_dump)) {
1948 char b[BDEVNAME_SIZE];
San Mehat8dcbdc72010-09-14 08:48:01 +02001949 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s (%u sectors)\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001950 current->comm, task_pid_nr(current),
Jens Axboebf2de6f2007-09-27 13:01:25 +02001951 (rw & WRITE) ? "WRITE" : "READ",
Kent Overstreet4f024f32013-10-11 15:44:27 -07001952 (unsigned long long)bio->bi_iter.bi_sector,
San Mehat8dcbdc72010-09-14 08:48:01 +02001953 bdevname(bio->bi_bdev, b),
1954 count);
Jens Axboebf2de6f2007-09-27 13:01:25 +02001955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 }
1957
1958 generic_make_request(bio);
1959}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960EXPORT_SYMBOL(submit_bio);
1961
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001962/**
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001963 * blk_rq_check_limits - Helper function to check a request for the queue limit
1964 * @q: the queue
1965 * @rq: the request being checked
1966 *
1967 * Description:
1968 * @rq may have been made based on weaker limitations of upper-level queues
1969 * in request stacking drivers, and it may violate the limitation of @q.
1970 * Since the block layer and the underlying device driver trust @rq
1971 * after it is inserted to @q, it should be checked against @q before
1972 * the insertion using this generic function.
1973 *
1974 * This function should also be useful for request stacking drivers
Stefan Weileef35c22010-08-06 21:11:15 +02001975 * in some cases below, so export this function.
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001976 * Request stacking drivers like request-based dm may change the queue
1977 * limits while requests are in the queue (e.g. dm's table swapping).
Masanari Iidae2278672014-02-18 22:54:36 +09001978 * Such request stacking drivers should check those requests against
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001979 * the new queue limits again when they dispatch those requests,
1980 * although such checkings are also done against the old queue limits
1981 * when submitting requests.
1982 */
1983int blk_rq_check_limits(struct request_queue *q, struct request *rq)
1984{
Martin K. Petersene2a60da2012-09-18 12:19:25 -04001985 if (!rq_mergeable(rq))
ike Snitzer33839772010-08-08 12:11:33 -04001986 return 0;
1987
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -04001988 if (blk_rq_sectors(rq) > blk_queue_get_max_sectors(q, rq->cmd_flags)) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001989 printk(KERN_ERR "%s: over max size limit.\n", __func__);
1990 return -EIO;
1991 }
1992
1993 /*
1994 * queue's settings related to segment counting like q->bounce_pfn
1995 * may differ from that of other stacking queues.
1996 * Recalculate it to check the request correctly on this queue's
1997 * limitation.
1998 */
1999 blk_recalc_rq_segments(rq);
Martin K. Petersen8a783622010-02-26 00:20:39 -05002000 if (rq->nr_phys_segments > queue_max_segments(q)) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002001 printk(KERN_ERR "%s: over max segments limit.\n", __func__);
2002 return -EIO;
2003 }
2004
2005 return 0;
2006}
2007EXPORT_SYMBOL_GPL(blk_rq_check_limits);
2008
2009/**
2010 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
2011 * @q: the queue to submit the request
2012 * @rq: the request being queued
2013 */
2014int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
2015{
2016 unsigned long flags;
Jeff Moyer4853aba2011-08-15 21:37:25 +02002017 int where = ELEVATOR_INSERT_BACK;
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002018
2019 if (blk_rq_check_limits(q, rq))
2020 return -EIO;
2021
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07002022 if (rq->rq_disk &&
2023 should_fail_request(&rq->rq_disk->part0, blk_rq_bytes(rq)))
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002024 return -EIO;
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002025
2026 spin_lock_irqsave(q->queue_lock, flags);
Bart Van Assche3f3299d2012-11-28 13:42:38 +01002027 if (unlikely(blk_queue_dying(q))) {
Tejun Heo8ba61432011-12-14 00:33:37 +01002028 spin_unlock_irqrestore(q->queue_lock, flags);
2029 return -ENODEV;
2030 }
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002031
2032 /*
2033 * Submitting request must be dequeued before calling this function
2034 * because it will be linked to another request_queue
2035 */
2036 BUG_ON(blk_queued_rq(rq));
2037
Jeff Moyer4853aba2011-08-15 21:37:25 +02002038 if (rq->cmd_flags & (REQ_FLUSH|REQ_FUA))
2039 where = ELEVATOR_INSERT_FLUSH;
2040
2041 add_acct_request(q, rq, where);
Jeff Moyere67b77c2011-10-17 12:57:23 +02002042 if (where == ELEVATOR_INSERT_FLUSH)
2043 __blk_run_queue(q);
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002044 spin_unlock_irqrestore(q->queue_lock, flags);
2045
2046 return 0;
2047}
2048EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
2049
Tejun Heo80a761f2009-07-03 17:48:17 +09002050/**
2051 * blk_rq_err_bytes - determine number of bytes till the next failure boundary
2052 * @rq: request to examine
2053 *
2054 * Description:
2055 * A request could be merge of IOs which require different failure
2056 * handling. This function determines the number of bytes which
2057 * can be failed from the beginning of the request without
2058 * crossing into area which need to be retried further.
2059 *
2060 * Return:
2061 * The number of bytes to fail.
2062 *
2063 * Context:
2064 * queue_lock must be held.
2065 */
2066unsigned int blk_rq_err_bytes(const struct request *rq)
2067{
2068 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
2069 unsigned int bytes = 0;
2070 struct bio *bio;
2071
2072 if (!(rq->cmd_flags & REQ_MIXED_MERGE))
2073 return blk_rq_bytes(rq);
2074
2075 /*
2076 * Currently the only 'mixing' which can happen is between
2077 * different fastfail types. We can safely fail portions
2078 * which have all the failfast bits that the first one has -
2079 * the ones which are at least as eager to fail as the first
2080 * one.
2081 */
2082 for (bio = rq->bio; bio; bio = bio->bi_next) {
2083 if ((bio->bi_rw & ff) != ff)
2084 break;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002085 bytes += bio->bi_iter.bi_size;
Tejun Heo80a761f2009-07-03 17:48:17 +09002086 }
2087
2088 /* this could lead to infinite loop */
2089 BUG_ON(blk_rq_bytes(rq) && !bytes);
2090 return bytes;
2091}
2092EXPORT_SYMBOL_GPL(blk_rq_err_bytes);
2093
Jens Axboe320ae512013-10-24 09:20:05 +01002094void blk_account_io_completion(struct request *req, unsigned int bytes)
Jens Axboebc58ba92009-01-23 10:54:44 +01002095{
Jens Axboec2553b52009-04-24 08:10:11 +02002096 if (blk_do_io_stat(req)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01002097 const int rw = rq_data_dir(req);
2098 struct hd_struct *part;
2099 int cpu;
2100
2101 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +01002102 part = req->part;
Jens Axboebc58ba92009-01-23 10:54:44 +01002103 part_stat_add(cpu, part, sectors[rw], bytes >> 9);
2104 part_stat_unlock();
2105 }
2106}
2107
Jens Axboe320ae512013-10-24 09:20:05 +01002108void blk_account_io_done(struct request *req)
Jens Axboebc58ba92009-01-23 10:54:44 +01002109{
Jens Axboebc58ba92009-01-23 10:54:44 +01002110 /*
Tejun Heodd4c1332010-09-03 11:56:16 +02002111 * Account IO completion. flush_rq isn't accounted as a
2112 * normal IO on queueing nor completion. Accounting the
2113 * containing request is enough.
Jens Axboebc58ba92009-01-23 10:54:44 +01002114 */
Tejun Heo414b4ff2011-01-25 12:43:49 +01002115 if (blk_do_io_stat(req) && !(req->cmd_flags & REQ_FLUSH_SEQ)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01002116 unsigned long duration = jiffies - req->start_time;
2117 const int rw = rq_data_dir(req);
2118 struct hd_struct *part;
2119 int cpu;
2120
2121 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +01002122 part = req->part;
Jens Axboebc58ba92009-01-23 10:54:44 +01002123
2124 part_stat_inc(cpu, part, ios[rw]);
2125 part_stat_add(cpu, part, ticks[rw], duration);
2126 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02002127 part_dec_in_flight(part, rw);
Jens Axboebc58ba92009-01-23 10:54:44 +01002128
Jens Axboe6c23a962011-01-07 08:43:37 +01002129 hd_struct_put(part);
Jens Axboebc58ba92009-01-23 10:54:44 +01002130 part_stat_unlock();
2131 }
2132}
2133
Lin Mingc8158812013-03-23 11:42:27 +08002134#ifdef CONFIG_PM_RUNTIME
2135/*
2136 * Don't process normal requests when queue is suspended
2137 * or in the process of suspending/resuming
2138 */
2139static struct request *blk_pm_peek_request(struct request_queue *q,
2140 struct request *rq)
2141{
2142 if (q->dev && (q->rpm_status == RPM_SUSPENDED ||
2143 (q->rpm_status != RPM_ACTIVE && !(rq->cmd_flags & REQ_PM))))
2144 return NULL;
2145 else
2146 return rq;
2147}
2148#else
2149static inline struct request *blk_pm_peek_request(struct request_queue *q,
2150 struct request *rq)
2151{
2152 return rq;
2153}
2154#endif
2155
Jens Axboe320ae512013-10-24 09:20:05 +01002156void blk_account_io_start(struct request *rq, bool new_io)
2157{
2158 struct hd_struct *part;
2159 int rw = rq_data_dir(rq);
2160 int cpu;
2161
2162 if (!blk_do_io_stat(rq))
2163 return;
2164
2165 cpu = part_stat_lock();
2166
2167 if (!new_io) {
2168 part = rq->part;
2169 part_stat_inc(cpu, part, merges[rw]);
2170 } else {
2171 part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq));
2172 if (!hd_struct_try_get(part)) {
2173 /*
2174 * The partition is already being removed,
2175 * the request will be accounted on the disk only
2176 *
2177 * We take a reference on disk->part0 although that
2178 * partition will never be deleted, so we can treat
2179 * it as any other partition.
2180 */
2181 part = &rq->rq_disk->part0;
2182 hd_struct_get(part);
2183 }
2184 part_round_stats(cpu, part);
2185 part_inc_in_flight(part, rw);
2186 rq->part = part;
2187 }
2188
2189 part_stat_unlock();
2190}
2191
Tejun Heo53a08802008-12-03 12:41:26 +01002192/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09002193 * blk_peek_request - peek at the top of a request queue
2194 * @q: request queue to peek at
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002195 *
2196 * Description:
Tejun Heo9934c8c2009-05-08 11:54:16 +09002197 * Return the request at the top of @q. The returned request
2198 * should be started using blk_start_request() before LLD starts
2199 * processing it.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002200 *
2201 * Return:
Tejun Heo9934c8c2009-05-08 11:54:16 +09002202 * Pointer to the request at the top of @q if available. Null
2203 * otherwise.
2204 *
2205 * Context:
2206 * queue_lock must be held.
2207 */
2208struct request *blk_peek_request(struct request_queue *q)
Tejun Heo158dbda2009-04-23 11:05:18 +09002209{
2210 struct request *rq;
2211 int ret;
2212
2213 while ((rq = __elv_next_request(q)) != NULL) {
Lin Mingc8158812013-03-23 11:42:27 +08002214
2215 rq = blk_pm_peek_request(q, rq);
2216 if (!rq)
2217 break;
2218
Tejun Heo158dbda2009-04-23 11:05:18 +09002219 if (!(rq->cmd_flags & REQ_STARTED)) {
2220 /*
2221 * This is the first time the device driver
2222 * sees this request (possibly after
2223 * requeueing). Notify IO scheduler.
2224 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002225 if (rq->cmd_flags & REQ_SORTED)
Tejun Heo158dbda2009-04-23 11:05:18 +09002226 elv_activate_rq(q, rq);
2227
2228 /*
2229 * just mark as started even if we don't start
2230 * it, a request that has been delayed should
2231 * not be passed by new incoming requests
2232 */
2233 rq->cmd_flags |= REQ_STARTED;
2234 trace_block_rq_issue(q, rq);
2235 }
2236
2237 if (!q->boundary_rq || q->boundary_rq == rq) {
2238 q->end_sector = rq_end_sector(rq);
2239 q->boundary_rq = NULL;
2240 }
2241
2242 if (rq->cmd_flags & REQ_DONTPREP)
2243 break;
2244
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002245 if (q->dma_drain_size && blk_rq_bytes(rq)) {
Tejun Heo158dbda2009-04-23 11:05:18 +09002246 /*
2247 * make sure space for the drain appears we
2248 * know we can do this because max_hw_segments
2249 * has been adjusted to be one fewer than the
2250 * device can handle
2251 */
2252 rq->nr_phys_segments++;
2253 }
2254
2255 if (!q->prep_rq_fn)
2256 break;
2257
2258 ret = q->prep_rq_fn(q, rq);
2259 if (ret == BLKPREP_OK) {
2260 break;
2261 } else if (ret == BLKPREP_DEFER) {
2262 /*
2263 * the request may have been (partially) prepped.
2264 * we need to keep this request in the front to
2265 * avoid resource deadlock. REQ_STARTED will
2266 * prevent other fs requests from passing this one.
2267 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002268 if (q->dma_drain_size && blk_rq_bytes(rq) &&
Tejun Heo158dbda2009-04-23 11:05:18 +09002269 !(rq->cmd_flags & REQ_DONTPREP)) {
2270 /*
2271 * remove the space for the drain we added
2272 * so that we don't add it again
2273 */
2274 --rq->nr_phys_segments;
2275 }
2276
2277 rq = NULL;
2278 break;
2279 } else if (ret == BLKPREP_KILL) {
2280 rq->cmd_flags |= REQ_QUIET;
James Bottomleyc143dc92009-05-30 06:43:49 +02002281 /*
2282 * Mark this request as started so we don't trigger
2283 * any debug logic in the end I/O path.
2284 */
2285 blk_start_request(rq);
Tejun Heo40cbbb72009-04-23 11:05:19 +09002286 __blk_end_request_all(rq, -EIO);
Tejun Heo158dbda2009-04-23 11:05:18 +09002287 } else {
2288 printk(KERN_ERR "%s: bad return=%d\n", __func__, ret);
2289 break;
2290 }
2291 }
2292
2293 return rq;
2294}
Tejun Heo9934c8c2009-05-08 11:54:16 +09002295EXPORT_SYMBOL(blk_peek_request);
Tejun Heo158dbda2009-04-23 11:05:18 +09002296
Tejun Heo9934c8c2009-05-08 11:54:16 +09002297void blk_dequeue_request(struct request *rq)
Tejun Heo158dbda2009-04-23 11:05:18 +09002298{
Tejun Heo9934c8c2009-05-08 11:54:16 +09002299 struct request_queue *q = rq->q;
2300
Tejun Heo158dbda2009-04-23 11:05:18 +09002301 BUG_ON(list_empty(&rq->queuelist));
2302 BUG_ON(ELV_ON_HASH(rq));
2303
2304 list_del_init(&rq->queuelist);
2305
2306 /*
2307 * the time frame between a request being removed from the lists
2308 * and to it is freed is accounted as io that is in progress at
2309 * the driver side.
2310 */
Divyesh Shah91952912010-04-01 15:01:41 -07002311 if (blk_account_rq(rq)) {
Jens Axboe0a7ae2f2009-05-20 08:54:31 +02002312 q->in_flight[rq_is_sync(rq)]++;
Divyesh Shah91952912010-04-01 15:01:41 -07002313 set_io_start_time_ns(rq);
2314 }
Tejun Heo158dbda2009-04-23 11:05:18 +09002315}
2316
Tejun Heo5efccd12009-04-23 11:05:18 +09002317/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09002318 * blk_start_request - start request processing on the driver
2319 * @req: request to dequeue
2320 *
2321 * Description:
2322 * Dequeue @req and start timeout timer on it. This hands off the
2323 * request to the driver.
2324 *
2325 * Block internal functions which don't want to start timer should
2326 * call blk_dequeue_request().
2327 *
2328 * Context:
2329 * queue_lock must be held.
2330 */
2331void blk_start_request(struct request *req)
2332{
2333 blk_dequeue_request(req);
2334
2335 /*
Tejun Heo5f49f632009-05-19 18:33:05 +09002336 * We are now handing the request to the hardware, initialize
2337 * resid_len to full count and add the timeout handler.
Tejun Heo9934c8c2009-05-08 11:54:16 +09002338 */
Tejun Heo5f49f632009-05-19 18:33:05 +09002339 req->resid_len = blk_rq_bytes(req);
FUJITA Tomonoridbb66c42009-06-09 05:47:10 +02002340 if (unlikely(blk_bidi_rq(req)))
2341 req->next_rq->resid_len = blk_rq_bytes(req->next_rq);
2342
Jeff Moyer4912aa62013-10-08 14:36:41 -04002343 BUG_ON(test_bit(REQ_ATOM_COMPLETE, &req->atomic_flags));
Tejun Heo9934c8c2009-05-08 11:54:16 +09002344 blk_add_timer(req);
2345}
2346EXPORT_SYMBOL(blk_start_request);
2347
2348/**
2349 * blk_fetch_request - fetch a request from a request queue
2350 * @q: request queue to fetch a request from
2351 *
2352 * Description:
2353 * Return the request at the top of @q. The request is started on
2354 * return and LLD can start processing it immediately.
2355 *
2356 * Return:
2357 * Pointer to the request at the top of @q if available. Null
2358 * otherwise.
2359 *
2360 * Context:
2361 * queue_lock must be held.
2362 */
2363struct request *blk_fetch_request(struct request_queue *q)
2364{
2365 struct request *rq;
2366
2367 rq = blk_peek_request(q);
2368 if (rq)
2369 blk_start_request(rq);
2370 return rq;
2371}
2372EXPORT_SYMBOL(blk_fetch_request);
2373
2374/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002375 * blk_update_request - Special helper function for request stacking drivers
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002376 * @req: the request being processed
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002377 * @error: %0 for success, < %0 for error
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002378 * @nr_bytes: number of bytes to complete @req
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002379 *
2380 * Description:
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002381 * Ends I/O on a number of bytes attached to @req, but doesn't complete
2382 * the request structure even if @req doesn't have leftover.
2383 * If @req has leftover, sets it up for the next range of segments.
Tejun Heo2e60e022009-04-23 11:05:18 +09002384 *
2385 * This special helper function is only for request stacking drivers
2386 * (e.g. request-based dm) so that they can handle partial completion.
2387 * Actual device drivers should use blk_end_request instead.
2388 *
2389 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
2390 * %false return from this function.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002391 *
2392 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002393 * %false - this request doesn't have any more data
2394 * %true - this request has more data
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002395 **/
Tejun Heo2e60e022009-04-23 11:05:18 +09002396bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397{
Kent Overstreetf79ea412012-09-20 16:38:30 -07002398 int total_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
Tejun Heo2e60e022009-04-23 11:05:18 +09002400 if (!req->bio)
2401 return false;
2402
Roman Penaf5040d2014-03-04 23:13:10 +09002403 trace_block_rq_complete(req->q, req, nr_bytes);
Jens Axboe2056a782006-03-23 20:00:26 +01002404
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 /*
Tejun Heo6f414692009-04-19 07:00:41 +09002406 * For fs requests, rq is just carrier of independent bio's
2407 * and each partial completion should be handled separately.
2408 * Reset per-request error on each partial completion.
2409 *
2410 * TODO: tj: This is too subtle. It would be better to let
2411 * low level drivers do what they see fit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002413 if (req->cmd_type == REQ_TYPE_FS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 req->errors = 0;
2415
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002416 if (error && req->cmd_type == REQ_TYPE_FS &&
2417 !(req->cmd_flags & REQ_QUIET)) {
Hannes Reinecke79775562011-01-18 10:13:13 +01002418 char *error_type;
2419
2420 switch (error) {
2421 case -ENOLINK:
2422 error_type = "recoverable transport";
2423 break;
2424 case -EREMOTEIO:
2425 error_type = "critical target";
2426 break;
2427 case -EBADE:
2428 error_type = "critical nexus";
2429 break;
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002430 case -ETIMEDOUT:
2431 error_type = "timeout";
2432 break;
Hannes Reineckea9d6ceb82013-07-01 15:16:25 +02002433 case -ENOSPC:
2434 error_type = "critical space allocation";
2435 break;
Hannes Reinecke7e782af2013-07-01 15:16:26 +02002436 case -ENODATA:
2437 error_type = "critical medium";
2438 break;
Hannes Reinecke79775562011-01-18 10:13:13 +01002439 case -EIO:
2440 default:
2441 error_type = "I/O";
2442 break;
2443 }
Yi Zou37d7b342012-08-30 16:26:25 -07002444 printk_ratelimited(KERN_ERR "end_request: %s error, dev %s, sector %llu\n",
2445 error_type, req->rq_disk ?
2446 req->rq_disk->disk_name : "?",
2447 (unsigned long long)blk_rq_pos(req));
2448
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 }
2450
Jens Axboebc58ba92009-01-23 10:54:44 +01002451 blk_account_io_completion(req, nr_bytes);
Jens Axboed72d9042005-11-01 08:35:42 +01002452
Kent Overstreetf79ea412012-09-20 16:38:30 -07002453 total_bytes = 0;
2454 while (req->bio) {
2455 struct bio *bio = req->bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002456 unsigned bio_bytes = min(bio->bi_iter.bi_size, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
Kent Overstreet4f024f32013-10-11 15:44:27 -07002458 if (bio_bytes == bio->bi_iter.bi_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 req->bio = bio->bi_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Kent Overstreetf79ea412012-09-20 16:38:30 -07002461 req_bio_endio(req, bio, bio_bytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
Kent Overstreetf79ea412012-09-20 16:38:30 -07002463 total_bytes += bio_bytes;
2464 nr_bytes -= bio_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
Kent Overstreetf79ea412012-09-20 16:38:30 -07002466 if (!nr_bytes)
2467 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 }
2469
2470 /*
2471 * completely done
2472 */
Tejun Heo2e60e022009-04-23 11:05:18 +09002473 if (!req->bio) {
2474 /*
2475 * Reset counters so that the request stacking driver
2476 * can find how many bytes remain in the request
2477 * later.
2478 */
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002479 req->__data_len = 0;
Tejun Heo2e60e022009-04-23 11:05:18 +09002480 return false;
2481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002483 req->__data_len -= total_bytes;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002484
2485 /* update sector only for requests with clear definition of sector */
Martin K. Petersene2a60da2012-09-18 12:19:25 -04002486 if (req->cmd_type == REQ_TYPE_FS)
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002487 req->__sector += total_bytes >> 9;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002488
Tejun Heo80a761f2009-07-03 17:48:17 +09002489 /* mixed attributes always follow the first bio */
2490 if (req->cmd_flags & REQ_MIXED_MERGE) {
2491 req->cmd_flags &= ~REQ_FAILFAST_MASK;
2492 req->cmd_flags |= req->bio->bi_rw & REQ_FAILFAST_MASK;
2493 }
2494
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002495 /*
2496 * If total number of sectors is less than the first segment
2497 * size, something has gone terribly wrong.
2498 */
2499 if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
Jens Axboe81829242011-03-30 09:51:33 +02002500 blk_dump_rq_flags(req, "request botched");
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002501 req->__data_len = blk_rq_cur_bytes(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002502 }
2503
2504 /* recalculate the number of segments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 blk_recalc_rq_segments(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002506
Tejun Heo2e60e022009-04-23 11:05:18 +09002507 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508}
Tejun Heo2e60e022009-04-23 11:05:18 +09002509EXPORT_SYMBOL_GPL(blk_update_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
Tejun Heo2e60e022009-04-23 11:05:18 +09002511static bool blk_update_bidi_request(struct request *rq, int error,
2512 unsigned int nr_bytes,
2513 unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002514{
Tejun Heo2e60e022009-04-23 11:05:18 +09002515 if (blk_update_request(rq, error, nr_bytes))
2516 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002517
Tejun Heo2e60e022009-04-23 11:05:18 +09002518 /* Bidi request must be completed as a whole */
2519 if (unlikely(blk_bidi_rq(rq)) &&
2520 blk_update_request(rq->next_rq, error, bidi_bytes))
2521 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002522
Jens Axboee2e1a142010-06-09 10:42:09 +02002523 if (blk_queue_add_random(rq->q))
2524 add_disk_randomness(rq->rq_disk);
Tejun Heo2e60e022009-04-23 11:05:18 +09002525
2526 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527}
2528
James Bottomley28018c22010-07-01 19:49:17 +09002529/**
2530 * blk_unprep_request - unprepare a request
2531 * @req: the request
2532 *
2533 * This function makes a request ready for complete resubmission (or
2534 * completion). It happens only after all error handling is complete,
2535 * so represents the appropriate moment to deallocate any resources
2536 * that were allocated to the request in the prep_rq_fn. The queue
2537 * lock is held when calling this.
2538 */
2539void blk_unprep_request(struct request *req)
2540{
2541 struct request_queue *q = req->q;
2542
2543 req->cmd_flags &= ~REQ_DONTPREP;
2544 if (q->unprep_rq_fn)
2545 q->unprep_rq_fn(q, req);
2546}
2547EXPORT_SYMBOL_GPL(blk_unprep_request);
2548
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549/*
2550 * queue lock must be held
2551 */
Christoph Hellwig12120072014-04-16 09:44:59 +02002552void blk_finish_request(struct request *req, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553{
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002554 if (blk_rq_tagged(req))
2555 blk_queue_end_tag(req->q, req);
2556
James Bottomleyba396a62009-05-27 14:17:08 +02002557 BUG_ON(blk_queued_rq(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002559 if (unlikely(laptop_mode) && req->cmd_type == REQ_TYPE_FS)
Matthew Garrett31373d02010-04-06 14:25:14 +02002560 laptop_io_completion(&req->q->backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
Mike Andersone78042e2008-10-30 02:16:20 -07002562 blk_delete_timer(req);
2563
James Bottomley28018c22010-07-01 19:49:17 +09002564 if (req->cmd_flags & REQ_DONTPREP)
2565 blk_unprep_request(req);
2566
Jens Axboebc58ba92009-01-23 10:54:44 +01002567 blk_account_io_done(req);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002568
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 if (req->end_io)
Tejun Heo8ffdc652006-01-06 09:49:03 +01002570 req->end_io(req, error);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002571 else {
2572 if (blk_bidi_rq(req))
2573 __blk_put_request(req->next_rq->q, req->next_rq);
2574
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 __blk_put_request(req->q, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 }
2577}
Christoph Hellwig12120072014-04-16 09:44:59 +02002578EXPORT_SYMBOL(blk_finish_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05002580/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002581 * blk_end_bidi_request - Complete a bidi request
2582 * @rq: the request to complete
Randy Dunlap710027a2008-08-19 20:13:11 +02002583 * @error: %0 for success, < %0 for error
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002584 * @nr_bytes: number of bytes to complete @rq
2585 * @bidi_bytes: number of bytes to complete @rq->next_rq
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002586 *
2587 * Description:
2588 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
Tejun Heo2e60e022009-04-23 11:05:18 +09002589 * Drivers that supports bidi can safely call this member for any
2590 * type of request, bidi or uni. In the later case @bidi_bytes is
2591 * just ignored.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002592 *
2593 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002594 * %false - we are done with this request
2595 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002596 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002597static bool blk_end_bidi_request(struct request *rq, int error,
2598 unsigned int nr_bytes, unsigned int bidi_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002599{
2600 struct request_queue *q = rq->q;
Tejun Heo2e60e022009-04-23 11:05:18 +09002601 unsigned long flags;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002602
Tejun Heo2e60e022009-04-23 11:05:18 +09002603 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2604 return true;
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002605
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002606 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo2e60e022009-04-23 11:05:18 +09002607 blk_finish_request(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002608 spin_unlock_irqrestore(q->queue_lock, flags);
2609
Tejun Heo2e60e022009-04-23 11:05:18 +09002610 return false;
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002611}
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002612
2613/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002614 * __blk_end_bidi_request - Complete a bidi request with queue lock held
2615 * @rq: the request to complete
2616 * @error: %0 for success, < %0 for error
2617 * @nr_bytes: number of bytes to complete @rq
2618 * @bidi_bytes: number of bytes to complete @rq->next_rq
Tejun Heo5efccd12009-04-23 11:05:18 +09002619 *
2620 * Description:
Tejun Heo2e60e022009-04-23 11:05:18 +09002621 * Identical to blk_end_bidi_request() except that queue lock is
2622 * assumed to be locked on entry and remains so on return.
Tejun Heo5efccd12009-04-23 11:05:18 +09002623 *
Tejun Heo2e60e022009-04-23 11:05:18 +09002624 * Return:
2625 * %false - we are done with this request
2626 * %true - still buffers pending for this request
Tejun Heo5efccd12009-04-23 11:05:18 +09002627 **/
Jeff Moyer4853aba2011-08-15 21:37:25 +02002628bool __blk_end_bidi_request(struct request *rq, int error,
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002629 unsigned int nr_bytes, unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002630{
Tejun Heo2e60e022009-04-23 11:05:18 +09002631 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2632 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002633
Tejun Heo2e60e022009-04-23 11:05:18 +09002634 blk_finish_request(rq, error);
Tejun Heo5efccd12009-04-23 11:05:18 +09002635
Tejun Heo2e60e022009-04-23 11:05:18 +09002636 return false;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002637}
2638
2639/**
2640 * blk_end_request - Helper function for drivers to complete the request.
2641 * @rq: the request being processed
2642 * @error: %0 for success, < %0 for error
2643 * @nr_bytes: number of bytes to complete
2644 *
2645 * Description:
2646 * Ends I/O on a number of bytes attached to @rq.
2647 * If @rq has leftover, sets it up for the next range of segments.
2648 *
2649 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002650 * %false - we are done with this request
2651 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002652 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002653bool blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002654{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002655 return blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002656}
Jens Axboe56ad1742009-07-28 22:11:24 +02002657EXPORT_SYMBOL(blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002658
2659/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002660 * blk_end_request_all - Helper function for drives to finish the request.
2661 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002662 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002663 *
2664 * Description:
2665 * Completely finish @rq.
2666 */
2667void blk_end_request_all(struct request *rq, int error)
2668{
2669 bool pending;
2670 unsigned int bidi_bytes = 0;
2671
2672 if (unlikely(blk_bidi_rq(rq)))
2673 bidi_bytes = blk_rq_bytes(rq->next_rq);
2674
2675 pending = blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2676 BUG_ON(pending);
2677}
Jens Axboe56ad1742009-07-28 22:11:24 +02002678EXPORT_SYMBOL(blk_end_request_all);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002679
2680/**
2681 * blk_end_request_cur - Helper function to finish the current request chunk.
2682 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002683 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002684 *
2685 * Description:
2686 * Complete the current consecutively mapped chunk from @rq.
2687 *
2688 * Return:
2689 * %false - we are done with this request
2690 * %true - still buffers pending for this request
2691 */
2692bool blk_end_request_cur(struct request *rq, int error)
2693{
2694 return blk_end_request(rq, error, blk_rq_cur_bytes(rq));
2695}
Jens Axboe56ad1742009-07-28 22:11:24 +02002696EXPORT_SYMBOL(blk_end_request_cur);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002697
2698/**
Tejun Heo80a761f2009-07-03 17:48:17 +09002699 * blk_end_request_err - Finish a request till the next failure boundary.
2700 * @rq: the request to finish till the next failure boundary for
2701 * @error: must be negative errno
2702 *
2703 * Description:
2704 * Complete @rq till the next failure boundary.
2705 *
2706 * Return:
2707 * %false - we are done with this request
2708 * %true - still buffers pending for this request
2709 */
2710bool blk_end_request_err(struct request *rq, int error)
2711{
2712 WARN_ON(error >= 0);
2713 return blk_end_request(rq, error, blk_rq_err_bytes(rq));
2714}
2715EXPORT_SYMBOL_GPL(blk_end_request_err);
2716
2717/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002718 * __blk_end_request - Helper function for drivers to complete the request.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002719 * @rq: the request being processed
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002720 * @error: %0 for success, < %0 for error
2721 * @nr_bytes: number of bytes to complete
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002722 *
2723 * Description:
2724 * Must be called with queue lock held unlike blk_end_request().
2725 *
2726 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002727 * %false - we are done with this request
2728 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002729 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002730bool __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002731{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002732 return __blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002733}
Jens Axboe56ad1742009-07-28 22:11:24 +02002734EXPORT_SYMBOL(__blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002735
2736/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002737 * __blk_end_request_all - Helper function for drives to finish the request.
2738 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002739 * @error: %0 for success, < %0 for error
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002740 *
2741 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002742 * Completely finish @rq. Must be called with queue lock held.
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002743 */
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002744void __blk_end_request_all(struct request *rq, int error)
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002745{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002746 bool pending;
2747 unsigned int bidi_bytes = 0;
2748
2749 if (unlikely(blk_bidi_rq(rq)))
2750 bidi_bytes = blk_rq_bytes(rq->next_rq);
2751
2752 pending = __blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2753 BUG_ON(pending);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002754}
Jens Axboe56ad1742009-07-28 22:11:24 +02002755EXPORT_SYMBOL(__blk_end_request_all);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002756
2757/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002758 * __blk_end_request_cur - Helper function to finish the current request chunk.
2759 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002760 * @error: %0 for success, < %0 for error
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002761 *
2762 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002763 * Complete the current consecutively mapped chunk from @rq. Must
2764 * be called with queue lock held.
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002765 *
2766 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002767 * %false - we are done with this request
2768 * %true - still buffers pending for this request
2769 */
2770bool __blk_end_request_cur(struct request *rq, int error)
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002771{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002772 return __blk_end_request(rq, error, blk_rq_cur_bytes(rq));
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002773}
Jens Axboe56ad1742009-07-28 22:11:24 +02002774EXPORT_SYMBOL(__blk_end_request_cur);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002775
Tejun Heo80a761f2009-07-03 17:48:17 +09002776/**
2777 * __blk_end_request_err - Finish a request till the next failure boundary.
2778 * @rq: the request to finish till the next failure boundary for
2779 * @error: must be negative errno
2780 *
2781 * Description:
2782 * Complete @rq till the next failure boundary. Must be called
2783 * with queue lock held.
2784 *
2785 * Return:
2786 * %false - we are done with this request
2787 * %true - still buffers pending for this request
2788 */
2789bool __blk_end_request_err(struct request *rq, int error)
2790{
2791 WARN_ON(error >= 0);
2792 return __blk_end_request(rq, error, blk_rq_err_bytes(rq));
2793}
2794EXPORT_SYMBOL_GPL(__blk_end_request_err);
2795
Jens Axboe86db1e22008-01-29 14:53:40 +01002796void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2797 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798{
Tejun Heoa82afdf2009-07-03 17:48:16 +09002799 /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002800 rq->cmd_flags |= bio->bi_rw & REQ_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
Jens Axboeb4f42e22014-04-10 09:46:28 -06002802 if (bio_has_data(bio))
David Woodhousefb2dce82008-08-05 18:01:53 +01002803 rq->nr_phys_segments = bio_phys_segments(q, bio);
Jens Axboeb4f42e22014-04-10 09:46:28 -06002804
Kent Overstreet4f024f32013-10-11 15:44:27 -07002805 rq->__data_len = bio->bi_iter.bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 rq->bio = rq->biotail = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807
NeilBrown66846572007-08-16 13:31:28 +02002808 if (bio->bi_bdev)
2809 rq->rq_disk = bio->bi_bdev->bd_disk;
2810}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
Ilya Loginov2d4dc892009-11-26 09:16:19 +01002812#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
2813/**
2814 * rq_flush_dcache_pages - Helper function to flush all pages in a request
2815 * @rq: the request to be flushed
2816 *
2817 * Description:
2818 * Flush all pages in @rq.
2819 */
2820void rq_flush_dcache_pages(struct request *rq)
2821{
2822 struct req_iterator iter;
Kent Overstreet79886132013-11-23 17:19:00 -08002823 struct bio_vec bvec;
Ilya Loginov2d4dc892009-11-26 09:16:19 +01002824
2825 rq_for_each_segment(bvec, rq, iter)
Kent Overstreet79886132013-11-23 17:19:00 -08002826 flush_dcache_page(bvec.bv_page);
Ilya Loginov2d4dc892009-11-26 09:16:19 +01002827}
2828EXPORT_SYMBOL_GPL(rq_flush_dcache_pages);
2829#endif
2830
Kiyoshi Uedaef9e3fa2008-10-01 16:12:15 +02002831/**
2832 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
2833 * @q : the queue of the device being checked
2834 *
2835 * Description:
2836 * Check if underlying low-level drivers of a device are busy.
2837 * If the drivers want to export their busy state, they must set own
2838 * exporting function using blk_queue_lld_busy() first.
2839 *
2840 * Basically, this function is used only by request stacking drivers
2841 * to stop dispatching requests to underlying devices when underlying
2842 * devices are busy. This behavior helps more I/O merging on the queue
2843 * of the request stacking driver and prevents I/O throughput regression
2844 * on burst I/O load.
2845 *
2846 * Return:
2847 * 0 - Not busy (The request stacking driver should dispatch request)
2848 * 1 - Busy (The request stacking driver should stop dispatching request)
2849 */
2850int blk_lld_busy(struct request_queue *q)
2851{
2852 if (q->lld_busy_fn)
2853 return q->lld_busy_fn(q);
2854
2855 return 0;
2856}
2857EXPORT_SYMBOL_GPL(blk_lld_busy);
2858
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002859/**
2860 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
2861 * @rq: the clone request to be cleaned up
2862 *
2863 * Description:
2864 * Free all bios in @rq for a cloned request.
2865 */
2866void blk_rq_unprep_clone(struct request *rq)
2867{
2868 struct bio *bio;
2869
2870 while ((bio = rq->bio) != NULL) {
2871 rq->bio = bio->bi_next;
2872
2873 bio_put(bio);
2874 }
2875}
2876EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
2877
2878/*
2879 * Copy attributes of the original request to the clone request.
Jens Axboeb4f42e22014-04-10 09:46:28 -06002880 * The actual data parts (e.g. ->cmd, ->sense) are not copied.
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002881 */
2882static void __blk_rq_prep_clone(struct request *dst, struct request *src)
2883{
2884 dst->cpu = src->cpu;
Tejun Heo3a2edd02010-09-03 11:56:18 +02002885 dst->cmd_flags = (src->cmd_flags & REQ_CLONE_MASK) | REQ_NOMERGE;
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002886 dst->cmd_type = src->cmd_type;
2887 dst->__sector = blk_rq_pos(src);
2888 dst->__data_len = blk_rq_bytes(src);
2889 dst->nr_phys_segments = src->nr_phys_segments;
2890 dst->ioprio = src->ioprio;
2891 dst->extra_len = src->extra_len;
2892}
2893
2894/**
2895 * blk_rq_prep_clone - Helper function to setup clone request
2896 * @rq: the request to be setup
2897 * @rq_src: original request to be cloned
2898 * @bs: bio_set that bios for clone are allocated from
2899 * @gfp_mask: memory allocation mask for bio
2900 * @bio_ctr: setup function to be called for each clone bio.
2901 * Returns %0 for success, non %0 for failure.
2902 * @data: private data to be passed to @bio_ctr
2903 *
2904 * Description:
2905 * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
Jens Axboeb4f42e22014-04-10 09:46:28 -06002906 * The actual data parts of @rq_src (e.g. ->cmd, ->sense)
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002907 * are not copied, and copying such parts is the caller's responsibility.
2908 * Also, pages which the original bios are pointing to are not copied
2909 * and the cloned bios just point same pages.
2910 * So cloned bios must be completed before original bios, which means
2911 * the caller must complete @rq before @rq_src.
2912 */
2913int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
2914 struct bio_set *bs, gfp_t gfp_mask,
2915 int (*bio_ctr)(struct bio *, struct bio *, void *),
2916 void *data)
2917{
2918 struct bio *bio, *bio_src;
2919
2920 if (!bs)
2921 bs = fs_bio_set;
2922
2923 blk_rq_init(NULL, rq);
2924
2925 __rq_for_each_bio(bio_src, rq_src) {
Kent Overstreetbf800ef2012-09-06 15:35:02 -07002926 bio = bio_clone_bioset(bio_src, gfp_mask, bs);
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002927 if (!bio)
2928 goto free_and_out;
2929
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002930 if (bio_ctr && bio_ctr(bio, bio_src, data))
2931 goto free_and_out;
2932
2933 if (rq->bio) {
2934 rq->biotail->bi_next = bio;
2935 rq->biotail = bio;
2936 } else
2937 rq->bio = rq->biotail = bio;
2938 }
2939
2940 __blk_rq_prep_clone(rq, rq_src);
2941
2942 return 0;
2943
2944free_and_out:
2945 if (bio)
Kent Overstreet4254bba2012-09-06 15:35:00 -07002946 bio_put(bio);
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002947 blk_rq_unprep_clone(rq);
2948
2949 return -ENOMEM;
2950}
2951EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
2952
Jens Axboe59c3d452014-04-08 09:15:35 -06002953int kblockd_schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954{
2955 return queue_work(kblockd_workqueue, work);
2956}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957EXPORT_SYMBOL(kblockd_schedule_work);
2958
Jens Axboe59c3d452014-04-08 09:15:35 -06002959int kblockd_schedule_delayed_work(struct delayed_work *dwork,
2960 unsigned long delay)
Vivek Goyale43473b2010-09-15 17:06:35 -04002961{
2962 return queue_delayed_work(kblockd_workqueue, dwork, delay);
2963}
2964EXPORT_SYMBOL(kblockd_schedule_delayed_work);
2965
Jens Axboe8ab14592014-04-08 09:17:40 -06002966int kblockd_schedule_delayed_work_on(int cpu, struct delayed_work *dwork,
2967 unsigned long delay)
2968{
2969 return queue_delayed_work_on(cpu, kblockd_workqueue, dwork, delay);
2970}
2971EXPORT_SYMBOL(kblockd_schedule_delayed_work_on);
2972
Suresh Jayaraman75df7132011-09-21 10:00:16 +02002973/**
2974 * blk_start_plug - initialize blk_plug and track it inside the task_struct
2975 * @plug: The &struct blk_plug that needs to be initialized
2976 *
2977 * Description:
2978 * Tracking blk_plug inside the task_struct will help with auto-flushing the
2979 * pending I/O should the task end up blocking between blk_start_plug() and
2980 * blk_finish_plug(). This is important from a performance perspective, but
2981 * also ensures that we don't deadlock. For instance, if the task is blocking
2982 * for a memory allocation, memory reclaim could end up wanting to free a
2983 * page belonging to that request that is currently residing in our private
2984 * plug. By flushing the pending I/O when the process goes to sleep, we avoid
2985 * this kind of deadlock.
2986 */
Jens Axboe73c10102011-03-08 13:19:51 +01002987void blk_start_plug(struct blk_plug *plug)
2988{
2989 struct task_struct *tsk = current;
2990
Jens Axboe73c10102011-03-08 13:19:51 +01002991 INIT_LIST_HEAD(&plug->list);
Jens Axboe320ae512013-10-24 09:20:05 +01002992 INIT_LIST_HEAD(&plug->mq_list);
NeilBrown048c9372011-04-18 09:52:22 +02002993 INIT_LIST_HEAD(&plug->cb_list);
Jens Axboe73c10102011-03-08 13:19:51 +01002994
2995 /*
2996 * If this is a nested plug, don't actually assign it. It will be
2997 * flushed on its own.
2998 */
2999 if (!tsk->plug) {
3000 /*
3001 * Store ordering should not be needed here, since a potential
3002 * preempt will imply a full memory barrier
3003 */
3004 tsk->plug = plug;
3005 }
3006}
3007EXPORT_SYMBOL(blk_start_plug);
3008
3009static int plug_rq_cmp(void *priv, struct list_head *a, struct list_head *b)
3010{
3011 struct request *rqa = container_of(a, struct request, queuelist);
3012 struct request *rqb = container_of(b, struct request, queuelist);
3013
Jianpeng Ma975927b2012-10-25 21:58:17 +02003014 return !(rqa->q < rqb->q ||
3015 (rqa->q == rqb->q && blk_rq_pos(rqa) < blk_rq_pos(rqb)));
Jens Axboe73c10102011-03-08 13:19:51 +01003016}
3017
Jens Axboe49cac012011-04-16 13:51:05 +02003018/*
3019 * If 'from_schedule' is true, then postpone the dispatch of requests
3020 * until a safe kblockd context. We due this to avoid accidental big
3021 * additional stack usage in driver dispatch, in places where the originally
3022 * plugger did not intend it.
3023 */
Jens Axboef6603782011-04-15 15:49:07 +02003024static void queue_unplugged(struct request_queue *q, unsigned int depth,
Jens Axboe49cac012011-04-16 13:51:05 +02003025 bool from_schedule)
Jens Axboe99e22592011-04-18 09:59:55 +02003026 __releases(q->queue_lock)
Jens Axboe94b5eb22011-04-12 10:12:19 +02003027{
Jens Axboe49cac012011-04-16 13:51:05 +02003028 trace_block_unplug(q, depth, !from_schedule);
Jens Axboe99e22592011-04-18 09:59:55 +02003029
Bart Van Assche70460572012-11-28 13:45:56 +01003030 if (from_schedule)
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003031 blk_run_queue_async(q);
Bart Van Assche70460572012-11-28 13:45:56 +01003032 else
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003033 __blk_run_queue(q);
Bart Van Assche70460572012-11-28 13:45:56 +01003034 spin_unlock(q->queue_lock);
Jens Axboe94b5eb22011-04-12 10:12:19 +02003035}
3036
NeilBrown74018dc2012-07-31 09:08:15 +02003037static void flush_plug_callbacks(struct blk_plug *plug, bool from_schedule)
NeilBrown048c9372011-04-18 09:52:22 +02003038{
3039 LIST_HEAD(callbacks);
3040
Shaohua Li2a7d5552012-07-31 09:08:15 +02003041 while (!list_empty(&plug->cb_list)) {
3042 list_splice_init(&plug->cb_list, &callbacks);
NeilBrown048c9372011-04-18 09:52:22 +02003043
Shaohua Li2a7d5552012-07-31 09:08:15 +02003044 while (!list_empty(&callbacks)) {
3045 struct blk_plug_cb *cb = list_first_entry(&callbacks,
NeilBrown048c9372011-04-18 09:52:22 +02003046 struct blk_plug_cb,
3047 list);
Shaohua Li2a7d5552012-07-31 09:08:15 +02003048 list_del(&cb->list);
NeilBrown74018dc2012-07-31 09:08:15 +02003049 cb->callback(cb, from_schedule);
Shaohua Li2a7d5552012-07-31 09:08:15 +02003050 }
NeilBrown048c9372011-04-18 09:52:22 +02003051 }
3052}
3053
NeilBrown9cbb1752012-07-31 09:08:14 +02003054struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug, void *data,
3055 int size)
3056{
3057 struct blk_plug *plug = current->plug;
3058 struct blk_plug_cb *cb;
3059
3060 if (!plug)
3061 return NULL;
3062
3063 list_for_each_entry(cb, &plug->cb_list, list)
3064 if (cb->callback == unplug && cb->data == data)
3065 return cb;
3066
3067 /* Not currently on the callback list */
3068 BUG_ON(size < sizeof(*cb));
3069 cb = kzalloc(size, GFP_ATOMIC);
3070 if (cb) {
3071 cb->data = data;
3072 cb->callback = unplug;
3073 list_add(&cb->list, &plug->cb_list);
3074 }
3075 return cb;
3076}
3077EXPORT_SYMBOL(blk_check_plugged);
3078
Jens Axboe49cac012011-04-16 13:51:05 +02003079void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
Jens Axboe73c10102011-03-08 13:19:51 +01003080{
3081 struct request_queue *q;
3082 unsigned long flags;
3083 struct request *rq;
NeilBrown109b8122011-04-11 14:13:10 +02003084 LIST_HEAD(list);
Jens Axboe94b5eb22011-04-12 10:12:19 +02003085 unsigned int depth;
Jens Axboe73c10102011-03-08 13:19:51 +01003086
NeilBrown74018dc2012-07-31 09:08:15 +02003087 flush_plug_callbacks(plug, from_schedule);
Jens Axboe320ae512013-10-24 09:20:05 +01003088
3089 if (!list_empty(&plug->mq_list))
3090 blk_mq_flush_plug_list(plug, from_schedule);
3091
Jens Axboe73c10102011-03-08 13:19:51 +01003092 if (list_empty(&plug->list))
3093 return;
3094
NeilBrown109b8122011-04-11 14:13:10 +02003095 list_splice_init(&plug->list, &list);
3096
Jianpeng Ma422765c2013-01-11 14:46:09 +01003097 list_sort(NULL, &list, plug_rq_cmp);
Jens Axboe73c10102011-03-08 13:19:51 +01003098
3099 q = NULL;
Jens Axboe94b5eb22011-04-12 10:12:19 +02003100 depth = 0;
Jens Axboe18811272011-04-12 10:11:24 +02003101
3102 /*
3103 * Save and disable interrupts here, to avoid doing it for every
3104 * queue lock we have to take.
3105 */
Jens Axboe73c10102011-03-08 13:19:51 +01003106 local_irq_save(flags);
NeilBrown109b8122011-04-11 14:13:10 +02003107 while (!list_empty(&list)) {
3108 rq = list_entry_rq(list.next);
Jens Axboe73c10102011-03-08 13:19:51 +01003109 list_del_init(&rq->queuelist);
Jens Axboe73c10102011-03-08 13:19:51 +01003110 BUG_ON(!rq->q);
3111 if (rq->q != q) {
Jens Axboe99e22592011-04-18 09:59:55 +02003112 /*
3113 * This drops the queue lock
3114 */
3115 if (q)
Jens Axboe49cac012011-04-16 13:51:05 +02003116 queue_unplugged(q, depth, from_schedule);
Jens Axboe73c10102011-03-08 13:19:51 +01003117 q = rq->q;
Jens Axboe94b5eb22011-04-12 10:12:19 +02003118 depth = 0;
Jens Axboe73c10102011-03-08 13:19:51 +01003119 spin_lock(q->queue_lock);
3120 }
Tejun Heo8ba61432011-12-14 00:33:37 +01003121
3122 /*
3123 * Short-circuit if @q is dead
3124 */
Bart Van Assche3f3299d2012-11-28 13:42:38 +01003125 if (unlikely(blk_queue_dying(q))) {
Tejun Heo8ba61432011-12-14 00:33:37 +01003126 __blk_end_request_all(rq, -ENODEV);
3127 continue;
3128 }
3129
Jens Axboe73c10102011-03-08 13:19:51 +01003130 /*
3131 * rq is already accounted, so use raw insert
3132 */
Jens Axboe401a18e2011-03-25 16:57:52 +01003133 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA))
3134 __elv_add_request(q, rq, ELEVATOR_INSERT_FLUSH);
3135 else
3136 __elv_add_request(q, rq, ELEVATOR_INSERT_SORT_MERGE);
Jens Axboe94b5eb22011-04-12 10:12:19 +02003137
3138 depth++;
Jens Axboe73c10102011-03-08 13:19:51 +01003139 }
3140
Jens Axboe99e22592011-04-18 09:59:55 +02003141 /*
3142 * This drops the queue lock
3143 */
3144 if (q)
Jens Axboe49cac012011-04-16 13:51:05 +02003145 queue_unplugged(q, depth, from_schedule);
Jens Axboe73c10102011-03-08 13:19:51 +01003146
Jens Axboe73c10102011-03-08 13:19:51 +01003147 local_irq_restore(flags);
3148}
Jens Axboe73c10102011-03-08 13:19:51 +01003149
3150void blk_finish_plug(struct blk_plug *plug)
3151{
Jens Axboef6603782011-04-15 15:49:07 +02003152 blk_flush_plug_list(plug, false);
Christoph Hellwig88b996c2011-04-15 15:20:10 +02003153
3154 if (plug == current->plug)
3155 current->plug = NULL;
Jens Axboe73c10102011-03-08 13:19:51 +01003156}
3157EXPORT_SYMBOL(blk_finish_plug);
3158
Lin Ming6c954662013-03-23 11:42:26 +08003159#ifdef CONFIG_PM_RUNTIME
3160/**
3161 * blk_pm_runtime_init - Block layer runtime PM initialization routine
3162 * @q: the queue of the device
3163 * @dev: the device the queue belongs to
3164 *
3165 * Description:
3166 * Initialize runtime-PM-related fields for @q and start auto suspend for
3167 * @dev. Drivers that want to take advantage of request-based runtime PM
3168 * should call this function after @dev has been initialized, and its
3169 * request queue @q has been allocated, and runtime PM for it can not happen
3170 * yet(either due to disabled/forbidden or its usage_count > 0). In most
3171 * cases, driver should call this function before any I/O has taken place.
3172 *
3173 * This function takes care of setting up using auto suspend for the device,
3174 * the autosuspend delay is set to -1 to make runtime suspend impossible
3175 * until an updated value is either set by user or by driver. Drivers do
3176 * not need to touch other autosuspend settings.
3177 *
3178 * The block layer runtime PM is request based, so only works for drivers
3179 * that use request as their IO unit instead of those directly use bio's.
3180 */
3181void blk_pm_runtime_init(struct request_queue *q, struct device *dev)
3182{
3183 q->dev = dev;
3184 q->rpm_status = RPM_ACTIVE;
3185 pm_runtime_set_autosuspend_delay(q->dev, -1);
3186 pm_runtime_use_autosuspend(q->dev);
3187}
3188EXPORT_SYMBOL(blk_pm_runtime_init);
3189
3190/**
3191 * blk_pre_runtime_suspend - Pre runtime suspend check
3192 * @q: the queue of the device
3193 *
3194 * Description:
3195 * This function will check if runtime suspend is allowed for the device
3196 * by examining if there are any requests pending in the queue. If there
3197 * are requests pending, the device can not be runtime suspended; otherwise,
3198 * the queue's status will be updated to SUSPENDING and the driver can
3199 * proceed to suspend the device.
3200 *
3201 * For the not allowed case, we mark last busy for the device so that
3202 * runtime PM core will try to autosuspend it some time later.
3203 *
3204 * This function should be called near the start of the device's
3205 * runtime_suspend callback.
3206 *
3207 * Return:
3208 * 0 - OK to runtime suspend the device
3209 * -EBUSY - Device should not be runtime suspended
3210 */
3211int blk_pre_runtime_suspend(struct request_queue *q)
3212{
3213 int ret = 0;
3214
3215 spin_lock_irq(q->queue_lock);
3216 if (q->nr_pending) {
3217 ret = -EBUSY;
3218 pm_runtime_mark_last_busy(q->dev);
3219 } else {
3220 q->rpm_status = RPM_SUSPENDING;
3221 }
3222 spin_unlock_irq(q->queue_lock);
3223 return ret;
3224}
3225EXPORT_SYMBOL(blk_pre_runtime_suspend);
3226
3227/**
3228 * blk_post_runtime_suspend - Post runtime suspend processing
3229 * @q: the queue of the device
3230 * @err: return value of the device's runtime_suspend function
3231 *
3232 * Description:
3233 * Update the queue's runtime status according to the return value of the
3234 * device's runtime suspend function and mark last busy for the device so
3235 * that PM core will try to auto suspend the device at a later time.
3236 *
3237 * This function should be called near the end of the device's
3238 * runtime_suspend callback.
3239 */
3240void blk_post_runtime_suspend(struct request_queue *q, int err)
3241{
3242 spin_lock_irq(q->queue_lock);
3243 if (!err) {
3244 q->rpm_status = RPM_SUSPENDED;
3245 } else {
3246 q->rpm_status = RPM_ACTIVE;
3247 pm_runtime_mark_last_busy(q->dev);
3248 }
3249 spin_unlock_irq(q->queue_lock);
3250}
3251EXPORT_SYMBOL(blk_post_runtime_suspend);
3252
3253/**
3254 * blk_pre_runtime_resume - Pre runtime resume processing
3255 * @q: the queue of the device
3256 *
3257 * Description:
3258 * Update the queue's runtime status to RESUMING in preparation for the
3259 * runtime resume of the device.
3260 *
3261 * This function should be called near the start of the device's
3262 * runtime_resume callback.
3263 */
3264void blk_pre_runtime_resume(struct request_queue *q)
3265{
3266 spin_lock_irq(q->queue_lock);
3267 q->rpm_status = RPM_RESUMING;
3268 spin_unlock_irq(q->queue_lock);
3269}
3270EXPORT_SYMBOL(blk_pre_runtime_resume);
3271
3272/**
3273 * blk_post_runtime_resume - Post runtime resume processing
3274 * @q: the queue of the device
3275 * @err: return value of the device's runtime_resume function
3276 *
3277 * Description:
3278 * Update the queue's runtime status according to the return value of the
3279 * device's runtime_resume function. If it is successfully resumed, process
3280 * the requests that are queued into the device's queue when it is resuming
3281 * and then mark last busy and initiate autosuspend for it.
3282 *
3283 * This function should be called near the end of the device's
3284 * runtime_resume callback.
3285 */
3286void blk_post_runtime_resume(struct request_queue *q, int err)
3287{
3288 spin_lock_irq(q->queue_lock);
3289 if (!err) {
3290 q->rpm_status = RPM_ACTIVE;
3291 __blk_run_queue(q);
3292 pm_runtime_mark_last_busy(q->dev);
Aaron Luc60855c2013-05-17 15:47:20 +08003293 pm_request_autosuspend(q->dev);
Lin Ming6c954662013-03-23 11:42:26 +08003294 } else {
3295 q->rpm_status = RPM_SUSPENDED;
3296 }
3297 spin_unlock_irq(q->queue_lock);
3298}
3299EXPORT_SYMBOL(blk_post_runtime_resume);
3300#endif
3301
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302int __init blk_dev_init(void)
3303{
Nikanth Karthikesan9eb55b02009-04-27 14:53:54 +02003304 BUILD_BUG_ON(__REQ_NR_BITS > 8 *
3305 sizeof(((struct request *)0)->cmd_flags));
3306
Tejun Heo89b90be2011-01-03 15:01:47 +01003307 /* used for unplugging and affects IO latency/throughput - HIGHPRI */
3308 kblockd_workqueue = alloc_workqueue("kblockd",
Matias Bjørling28747fc2014-06-11 23:43:54 +02003309 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 if (!kblockd_workqueue)
3311 panic("Failed to create kblockd\n");
3312
3313 request_cachep = kmem_cache_create("blkdev_requests",
Paul Mundt20c2df82007-07-20 10:11:58 +09003314 sizeof(struct request), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315
Jens Axboe8324aa92008-01-29 14:51:59 +01003316 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02003317 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 return 0;
3320}