blob: accb7fc6ec944a59651e1e8a43a4319eb2df1cca [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);
NeilBrowncbae8d42012-12-14 20:49:27 +010046EXPORT_TRACEPOINT_SYMBOL_GPL(block_unplug);
Ingo Molnar0bfc2452008-11-26 11:59:56 +010047
Tejun Heoa73f7302011-12-14 00:33:37 +010048DEFINE_IDA(blk_queue_ida);
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050/*
51 * For the allocated request tables
52 */
Jens Axboe320ae512013-10-24 09:20:05 +010053struct kmem_cache *request_cachep = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/*
56 * For queue allocation
57 */
Jens Axboe6728cb02008-01-31 13:03:55 +010058struct kmem_cache *blk_requestq_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 * Controlling structure to kblockd
62 */
Jens Axboeff856ba2006-01-09 16:02:34 +010063static struct workqueue_struct *kblockd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Jens Axboe8324aa92008-01-29 14:51:59 +010065void blk_queue_congestion_threshold(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
67 int nr;
68
69 nr = q->nr_requests - (q->nr_requests / 8) + 1;
70 if (nr > q->nr_requests)
71 nr = q->nr_requests;
72 q->nr_congestion_on = nr;
73
74 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
75 if (nr < 1)
76 nr = 1;
77 q->nr_congestion_off = nr;
78}
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/**
81 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
82 * @bdev: device
83 *
84 * Locates the passed device's request queue and returns the address of its
85 * backing_dev_info
86 *
87 * Will return NULL if the request queue cannot be located.
88 */
89struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
90{
91 struct backing_dev_info *ret = NULL;
Jens Axboe165125e2007-07-24 09:28:11 +020092 struct request_queue *q = bdev_get_queue(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 if (q)
95 ret = &q->backing_dev_info;
96 return ret;
97}
Linus Torvalds1da177e2005-04-16 15:20:36 -070098EXPORT_SYMBOL(blk_get_backing_dev_info);
99
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200100void blk_rq_init(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200102 memset(rq, 0, sizeof(*rq));
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 INIT_LIST_HEAD(&rq->queuelist);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700105 INIT_LIST_HEAD(&rq->timeout_list);
Jens Axboec7c22e42008-09-13 20:26:01 +0200106 rq->cpu = -1;
Jens Axboe63a71382008-02-08 12:41:03 +0100107 rq->q = q;
Tejun Heoa2dec7b2009-05-07 22:24:44 +0900108 rq->__sector = (sector_t) -1;
Jens Axboe2e662b62006-07-13 11:55:04 +0200109 INIT_HLIST_NODE(&rq->hash);
110 RB_CLEAR_NODE(&rq->rb_node);
FUJITA Tomonorid7e3c322008-04-29 09:54:39 +0200111 rq->cmd = rq->__cmd;
Li Zefane2494e12009-04-02 13:43:26 +0800112 rq->cmd_len = BLK_MAX_CDB;
Jens Axboe63a71382008-02-08 12:41:03 +0100113 rq->tag = -1;
Tejun Heob243ddc2009-04-23 11:05:18 +0900114 rq->start_time = jiffies;
Divyesh Shah91952912010-04-01 15:01:41 -0700115 set_start_time_ns(rq);
Jerome Marchand09e099d2011-01-05 16:57:38 +0100116 rq->part = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200118EXPORT_SYMBOL(blk_rq_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
NeilBrown5bb23a62007-09-27 12:46:13 +0200120static void req_bio_endio(struct request *rq, struct bio *bio,
121 unsigned int nbytes, int error)
Tejun Heo797e7db2006-01-06 09:51:03 +0100122{
Tejun Heo143a87f2011-01-25 12:43:52 +0100123 if (error)
124 clear_bit(BIO_UPTODATE, &bio->bi_flags);
125 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
126 error = -EIO;
Tejun Heo797e7db2006-01-06 09:51:03 +0100127
Tejun Heo143a87f2011-01-25 12:43:52 +0100128 if (unlikely(rq->cmd_flags & REQ_QUIET))
129 set_bit(BIO_QUIET, &bio->bi_flags);
130
Kent Overstreetf79ea412012-09-20 16:38:30 -0700131 bio_advance(bio, nbytes);
Tejun Heo143a87f2011-01-25 12:43:52 +0100132
133 /* don't actually finish bio if it's part of flush sequence */
Kent Overstreet4f024f32013-10-11 15:44:27 -0700134 if (bio->bi_iter.bi_size == 0 && !(rq->cmd_flags & REQ_FLUSH_SEQ))
Tejun Heo143a87f2011-01-25 12:43:52 +0100135 bio_endio(bio, error);
Tejun Heo797e7db2006-01-06 09:51:03 +0100136}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138void blk_dump_rq_flags(struct request *rq, char *msg)
139{
140 int bit;
141
Jens Axboe59533162013-05-23 12:25:08 +0200142 printk(KERN_INFO "%s: dev %s: type=%x, flags=%llx\n", msg,
Jens Axboe4aff5e22006-08-10 08:44:47 +0200143 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
Jens Axboe59533162013-05-23 12:25:08 +0200144 (unsigned long long) rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Tejun Heo83096eb2009-05-07 22:24:39 +0900146 printk(KERN_INFO " sector %llu, nr/cnr %u/%u\n",
147 (unsigned long long)blk_rq_pos(rq),
148 blk_rq_sectors(rq), blk_rq_cur_sectors(rq));
Tejun Heo731ec492009-04-23 11:05:20 +0900149 printk(KERN_INFO " bio %p, biotail %p, buffer %p, len %u\n",
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900150 rq->bio, rq->biotail, rq->buffer, blk_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200152 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
Jens Axboe6728cb02008-01-31 13:03:55 +0100153 printk(KERN_INFO " cdb: ");
FUJITA Tomonorid34c87e2008-04-29 14:37:52 +0200154 for (bit = 0; bit < BLK_MAX_CDB; bit++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 printk("%02x ", rq->cmd[bit]);
156 printk("\n");
157 }
158}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159EXPORT_SYMBOL(blk_dump_rq_flags);
160
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500161static void blk_delay_work(struct work_struct *work)
Jens Axboe6c5e0c42008-08-01 20:31:32 +0200162{
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500163 struct request_queue *q;
Jens Axboe6c5e0c42008-08-01 20:31:32 +0200164
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500165 q = container_of(work, struct request_queue, delay_work.work);
166 spin_lock_irq(q->queue_lock);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200167 __blk_run_queue(q);
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500168 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171/**
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500172 * blk_delay_queue - restart queueing after defined interval
173 * @q: The &struct request_queue in question
174 * @msecs: Delay in msecs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 *
176 * Description:
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500177 * Sometimes queueing needs to be postponed for a little while, to allow
178 * resources to come back. This function will make sure that queueing is
Bart Van Assche70460572012-11-28 13:45:56 +0100179 * restarted around the specified time. Queue lock must be held.
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500180 */
181void blk_delay_queue(struct request_queue *q, unsigned long msecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Bart Van Assche70460572012-11-28 13:45:56 +0100183 if (likely(!blk_queue_dead(q)))
184 queue_delayed_work(kblockd_workqueue, &q->delay_work,
185 msecs_to_jiffies(msecs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500187EXPORT_SYMBOL(blk_delay_queue);
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/**
190 * blk_start_queue - restart a previously stopped queue
Jens Axboe165125e2007-07-24 09:28:11 +0200191 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 *
193 * Description:
194 * blk_start_queue() will clear the stop flag on the queue, and call
195 * the request_fn for the queue if it was in a stopped state when
196 * entered. Also see blk_stop_queue(). Queue lock must be held.
197 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200198void blk_start_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200200 WARN_ON(!irqs_disabled());
201
Nick Piggin75ad23b2008-04-29 14:48:33 +0200202 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200203 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205EXPORT_SYMBOL(blk_start_queue);
206
207/**
208 * blk_stop_queue - stop a queue
Jens Axboe165125e2007-07-24 09:28:11 +0200209 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 *
211 * Description:
212 * The Linux block layer assumes that a block driver will consume all
213 * entries on the request queue when the request_fn strategy is called.
214 * Often this will not happen, because of hardware limitations (queue
215 * depth settings). If a device driver gets a 'queue full' response,
216 * or if it simply chooses not to queue more I/O at one point, it can
217 * call this function to prevent the request_fn from being called until
218 * the driver has signalled it's ready to go again. This happens by calling
219 * blk_start_queue() to restart queue operations. Queue lock must be held.
220 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200221void blk_stop_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
Tejun Heo136b5722012-08-21 13:18:24 -0700223 cancel_delayed_work(&q->delay_work);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200224 queue_flag_set(QUEUE_FLAG_STOPPED, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226EXPORT_SYMBOL(blk_stop_queue);
227
228/**
229 * blk_sync_queue - cancel any pending callbacks on a queue
230 * @q: the queue
231 *
232 * Description:
233 * The block layer may perform asynchronous callback activity
234 * on a queue, such as calling the unplug function after a timeout.
235 * A block device may call blk_sync_queue to ensure that any
236 * such activity is cancelled, thus allowing it to release resources
Michael Opdenacker59c51592007-05-09 08:57:56 +0200237 * that the callbacks might use. The caller must already have made sure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 * that its ->make_request_fn will not re-add plugging prior to calling
239 * this function.
240 *
Vivek Goyalda527772011-03-02 19:05:33 -0500241 * This function does not cancel any asynchronous activity arising
242 * out of elevator or throttling code. That would require elevaotor_exit()
Tejun Heo5efd6112012-03-05 13:15:12 -0800243 * and blkcg_exit_queue() to be called with queue lock initialized.
Vivek Goyalda527772011-03-02 19:05:33 -0500244 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 */
246void blk_sync_queue(struct request_queue *q)
247{
Jens Axboe70ed28b2008-11-19 14:38:39 +0100248 del_timer_sync(&q->timeout);
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500249 cancel_delayed_work_sync(&q->delay_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251EXPORT_SYMBOL(blk_sync_queue);
252
253/**
Bart Van Asschec246e802012-12-06 14:32:01 +0100254 * __blk_run_queue_uncond - run a queue whether or not it has been stopped
255 * @q: The queue to run
256 *
257 * Description:
258 * Invoke request handling on a queue if there are any pending requests.
259 * May be used to restart request handling after a request has completed.
260 * This variant runs the queue whether or not the queue has been
261 * stopped. Must be called with the queue lock held and interrupts
262 * disabled. See also @blk_run_queue.
263 */
264inline void __blk_run_queue_uncond(struct request_queue *q)
265{
266 if (unlikely(blk_queue_dead(q)))
267 return;
268
Bart Van Assche24faf6f2012-11-28 13:46:45 +0100269 /*
270 * Some request_fn implementations, e.g. scsi_request_fn(), unlock
271 * the queue lock internally. As a result multiple threads may be
272 * running such a request function concurrently. Keep track of the
273 * number of active request_fn invocations such that blk_drain_queue()
274 * can wait until all these request_fn calls have finished.
275 */
276 q->request_fn_active++;
Bart Van Asschec246e802012-12-06 14:32:01 +0100277 q->request_fn(q);
Bart Van Assche24faf6f2012-11-28 13:46:45 +0100278 q->request_fn_active--;
Bart Van Asschec246e802012-12-06 14:32:01 +0100279}
280
281/**
Jens Axboe80a4b582008-10-14 09:51:06 +0200282 * __blk_run_queue - run a single device queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200284 *
285 * Description:
286 * See @blk_run_queue. This variant must be called with the queue lock
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200287 * held and interrupts disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 */
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200289void __blk_run_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Tejun Heoa538cd02009-04-23 11:05:17 +0900291 if (unlikely(blk_queue_stopped(q)))
292 return;
293
Bart Van Asschec246e802012-12-06 14:32:01 +0100294 __blk_run_queue_uncond(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200295}
296EXPORT_SYMBOL(__blk_run_queue);
Jens Axboedac07ec2006-05-11 08:20:16 +0200297
Nick Piggin75ad23b2008-04-29 14:48:33 +0200298/**
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200299 * blk_run_queue_async - run a single device queue in workqueue context
300 * @q: The queue to run
301 *
302 * Description:
303 * Tells kblockd to perform the equivalent of @blk_run_queue on behalf
Bart Van Assche70460572012-11-28 13:45:56 +0100304 * of us. The caller must hold the queue lock.
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200305 */
306void blk_run_queue_async(struct request_queue *q)
307{
Bart Van Assche70460572012-11-28 13:45:56 +0100308 if (likely(!blk_queue_stopped(q) && !blk_queue_dead(q)))
Tejun Heoe7c2f962012-08-21 13:18:24 -0700309 mod_delayed_work(kblockd_workqueue, &q->delay_work, 0);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200310}
Jens Axboec21e6be2011-04-19 13:32:46 +0200311EXPORT_SYMBOL(blk_run_queue_async);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200312
313/**
Nick Piggin75ad23b2008-04-29 14:48:33 +0200314 * blk_run_queue - run a single device queue
315 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200316 *
317 * Description:
318 * Invoke request handling on this queue, if it has pending work to do.
Tejun Heoa7f55792009-04-23 11:05:17 +0900319 * May be used to restart queueing when a request has completed.
Nick Piggin75ad23b2008-04-29 14:48:33 +0200320 */
321void blk_run_queue(struct request_queue *q)
322{
323 unsigned long flags;
324
325 spin_lock_irqsave(q->queue_lock, flags);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200326 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 spin_unlock_irqrestore(q->queue_lock, flags);
328}
329EXPORT_SYMBOL(blk_run_queue);
330
Jens Axboe165125e2007-07-24 09:28:11 +0200331void blk_put_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500332{
333 kobject_put(&q->kobj);
334}
Jens Axboed86e0e82011-05-27 07:44:43 +0200335EXPORT_SYMBOL(blk_put_queue);
Al Viro483f4af2006-03-18 18:34:37 -0500336
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200337/**
Bart Van Assche807592a2012-11-28 13:43:38 +0100338 * __blk_drain_queue - drain requests from request_queue
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200339 * @q: queue to drain
Tejun Heoc9a929d2011-10-19 14:42:16 +0200340 * @drain_all: whether to drain all requests or only the ones w/ ELVPRIV
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200341 *
Tejun Heoc9a929d2011-10-19 14:42:16 +0200342 * Drain requests from @q. If @drain_all is set, all requests are drained.
343 * If not, only ELVPRIV requests are drained. The caller is responsible
344 * for ensuring that no new requests which need to be drained are queued.
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200345 */
Bart Van Assche807592a2012-11-28 13:43:38 +0100346static void __blk_drain_queue(struct request_queue *q, bool drain_all)
347 __releases(q->queue_lock)
348 __acquires(q->queue_lock)
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200349{
Asias He458f27a2012-06-15 08:45:25 +0200350 int i;
351
Bart Van Assche807592a2012-11-28 13:43:38 +0100352 lockdep_assert_held(q->queue_lock);
353
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200354 while (true) {
Tejun Heo481a7d62011-12-14 00:33:37 +0100355 bool drain = false;
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200356
Tejun Heob855b042012-03-06 21:24:55 +0100357 /*
358 * The caller might be trying to drain @q before its
359 * elevator is initialized.
360 */
361 if (q->elevator)
362 elv_drain_elevator(q);
363
Tejun Heo5efd6112012-03-05 13:15:12 -0800364 blkcg_drain_queue(q);
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200365
Tejun Heo4eabc942011-12-15 20:03:04 +0100366 /*
367 * This function might be called on a queue which failed
Tejun Heob855b042012-03-06 21:24:55 +0100368 * driver init after queue creation or is not yet fully
369 * active yet. Some drivers (e.g. fd and loop) get unhappy
370 * in such cases. Kick queue iff dispatch queue has
371 * something on it and @q has request_fn set.
Tejun Heo4eabc942011-12-15 20:03:04 +0100372 */
Tejun Heob855b042012-03-06 21:24:55 +0100373 if (!list_empty(&q->queue_head) && q->request_fn)
Tejun Heo4eabc942011-12-15 20:03:04 +0100374 __blk_run_queue(q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200375
Tejun Heo8a5ecdd2012-06-04 20:40:58 -0700376 drain |= q->nr_rqs_elvpriv;
Bart Van Assche24faf6f2012-11-28 13:46:45 +0100377 drain |= q->request_fn_active;
Tejun Heo481a7d62011-12-14 00:33:37 +0100378
379 /*
380 * Unfortunately, requests are queued at and tracked from
381 * multiple places and there's no single counter which can
382 * be drained. Check all the queues and counters.
383 */
384 if (drain_all) {
385 drain |= !list_empty(&q->queue_head);
386 for (i = 0; i < 2; i++) {
Tejun Heo8a5ecdd2012-06-04 20:40:58 -0700387 drain |= q->nr_rqs[i];
Tejun Heo481a7d62011-12-14 00:33:37 +0100388 drain |= q->in_flight[i];
389 drain |= !list_empty(&q->flush_queue[i]);
390 }
391 }
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200392
Tejun Heo481a7d62011-12-14 00:33:37 +0100393 if (!drain)
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200394 break;
Bart Van Assche807592a2012-11-28 13:43:38 +0100395
396 spin_unlock_irq(q->queue_lock);
397
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200398 msleep(10);
Bart Van Assche807592a2012-11-28 13:43:38 +0100399
400 spin_lock_irq(q->queue_lock);
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200401 }
Asias He458f27a2012-06-15 08:45:25 +0200402
403 /*
404 * With queue marked dead, any woken up waiter will fail the
405 * allocation path, so the wakeup chaining is lost and we're
406 * left with hung waiters. We need to wake up those waiters.
407 */
408 if (q->request_fn) {
Tejun Heoa0516612012-06-26 15:05:44 -0700409 struct request_list *rl;
410
Tejun Heoa0516612012-06-26 15:05:44 -0700411 blk_queue_for_each_rl(rl, q)
412 for (i = 0; i < ARRAY_SIZE(rl->wait); i++)
413 wake_up_all(&rl->wait[i]);
Asias He458f27a2012-06-15 08:45:25 +0200414 }
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200415}
416
Tejun Heoc9a929d2011-10-19 14:42:16 +0200417/**
Tejun Heod7325802012-03-05 13:14:58 -0800418 * blk_queue_bypass_start - enter queue bypass mode
419 * @q: queue of interest
420 *
421 * In bypass mode, only the dispatch FIFO queue of @q is used. This
422 * function makes @q enter bypass mode and drains all requests which were
Tejun Heo6ecf23a2012-03-05 13:14:59 -0800423 * throttled or issued before. On return, it's guaranteed that no request
Tejun Heo80fd9972012-04-13 14:50:53 -0700424 * is being throttled or has ELVPRIV set and blk_queue_bypass() %true
425 * inside queue or RCU read lock.
Tejun Heod7325802012-03-05 13:14:58 -0800426 */
427void blk_queue_bypass_start(struct request_queue *q)
428{
Tejun Heob82d4b12012-04-13 13:11:31 -0700429 bool drain;
430
Tejun Heod7325802012-03-05 13:14:58 -0800431 spin_lock_irq(q->queue_lock);
Tejun Heob82d4b12012-04-13 13:11:31 -0700432 drain = !q->bypass_depth++;
Tejun Heod7325802012-03-05 13:14:58 -0800433 queue_flag_set(QUEUE_FLAG_BYPASS, q);
434 spin_unlock_irq(q->queue_lock);
435
Tejun Heob82d4b12012-04-13 13:11:31 -0700436 if (drain) {
Bart Van Assche807592a2012-11-28 13:43:38 +0100437 spin_lock_irq(q->queue_lock);
438 __blk_drain_queue(q, false);
439 spin_unlock_irq(q->queue_lock);
440
Tejun Heob82d4b12012-04-13 13:11:31 -0700441 /* ensure blk_queue_bypass() is %true inside RCU read lock */
442 synchronize_rcu();
443 }
Tejun Heod7325802012-03-05 13:14:58 -0800444}
445EXPORT_SYMBOL_GPL(blk_queue_bypass_start);
446
447/**
448 * blk_queue_bypass_end - leave queue bypass mode
449 * @q: queue of interest
450 *
451 * Leave bypass mode and restore the normal queueing behavior.
452 */
453void blk_queue_bypass_end(struct request_queue *q)
454{
455 spin_lock_irq(q->queue_lock);
456 if (!--q->bypass_depth)
457 queue_flag_clear(QUEUE_FLAG_BYPASS, q);
458 WARN_ON_ONCE(q->bypass_depth < 0);
459 spin_unlock_irq(q->queue_lock);
460}
461EXPORT_SYMBOL_GPL(blk_queue_bypass_end);
462
463/**
Tejun Heoc9a929d2011-10-19 14:42:16 +0200464 * blk_cleanup_queue - shutdown a request queue
465 * @q: request queue to shutdown
466 *
Bart Van Asschec246e802012-12-06 14:32:01 +0100467 * Mark @q DYING, drain all pending requests, mark @q DEAD, destroy and
468 * put it. All future requests will be failed immediately with -ENODEV.
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500469 */
Jens Axboe6728cb02008-01-31 13:03:55 +0100470void blk_cleanup_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500471{
Tejun Heoc9a929d2011-10-19 14:42:16 +0200472 spinlock_t *lock = q->queue_lock;
Jens Axboee3335de92008-09-18 09:22:54 -0700473
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100474 /* mark @q DYING, no new request or merges will be allowed afterwards */
Al Viro483f4af2006-03-18 18:34:37 -0500475 mutex_lock(&q->sysfs_lock);
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100476 queue_flag_set_unlocked(QUEUE_FLAG_DYING, q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200477 spin_lock_irq(lock);
Tejun Heo6ecf23a2012-03-05 13:14:59 -0800478
Tejun Heo80fd9972012-04-13 14:50:53 -0700479 /*
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100480 * A dying queue is permanently in bypass mode till released. Note
Tejun Heo80fd9972012-04-13 14:50:53 -0700481 * that, unlike blk_queue_bypass_start(), we aren't performing
482 * synchronize_rcu() after entering bypass mode to avoid the delay
483 * as some drivers create and destroy a lot of queues while
484 * probing. This is still safe because blk_release_queue() will be
485 * called only after the queue refcnt drops to zero and nothing,
486 * RCU or not, would be traversing the queue by then.
487 */
Tejun Heo6ecf23a2012-03-05 13:14:59 -0800488 q->bypass_depth++;
489 queue_flag_set(QUEUE_FLAG_BYPASS, q);
490
Tejun Heoc9a929d2011-10-19 14:42:16 +0200491 queue_flag_set(QUEUE_FLAG_NOMERGES, q);
492 queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100493 queue_flag_set(QUEUE_FLAG_DYING, q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200494 spin_unlock_irq(lock);
495 mutex_unlock(&q->sysfs_lock);
496
Bart Van Asschec246e802012-12-06 14:32:01 +0100497 /*
498 * Drain all requests queued before DYING marking. Set DEAD flag to
499 * prevent that q->request_fn() gets invoked after draining finished.
500 */
Ming Lei43a5e4e2013-12-26 21:31:35 +0800501 if (q->mq_ops) {
502 blk_mq_drain_queue(q);
503 spin_lock_irq(lock);
504 } else {
505 spin_lock_irq(lock);
506 __blk_drain_queue(q, true);
507 }
Bart Van Asschec246e802012-12-06 14:32:01 +0100508 queue_flag_set(QUEUE_FLAG_DEAD, q);
Bart Van Assche807592a2012-11-28 13:43:38 +0100509 spin_unlock_irq(lock);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200510
511 /* @q won't process any more request, flush async actions */
512 del_timer_sync(&q->backing_dev_info.laptop_mode_wb_timer);
513 blk_sync_queue(q);
514
Asias He5e5cfac2012-05-24 23:28:52 +0800515 spin_lock_irq(lock);
516 if (q->queue_lock != &q->__queue_lock)
517 q->queue_lock = &q->__queue_lock;
518 spin_unlock_irq(lock);
519
Tejun Heoc9a929d2011-10-19 14:42:16 +0200520 /* @q is and will stay empty, shutdown and put */
Al Viro483f4af2006-03-18 18:34:37 -0500521 blk_put_queue(q);
522}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523EXPORT_SYMBOL(blk_cleanup_queue);
524
Tejun Heo5b788ce2012-06-04 20:40:59 -0700525int blk_init_rl(struct request_list *rl, struct request_queue *q,
526 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Mike Snitzer1abec4f2010-05-25 13:15:15 -0400528 if (unlikely(rl->rq_pool))
529 return 0;
530
Tejun Heo5b788ce2012-06-04 20:40:59 -0700531 rl->q = q;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200532 rl->count[BLK_RW_SYNC] = rl->count[BLK_RW_ASYNC] = 0;
533 rl->starved[BLK_RW_SYNC] = rl->starved[BLK_RW_ASYNC] = 0;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200534 init_waitqueue_head(&rl->wait[BLK_RW_SYNC]);
535 init_waitqueue_head(&rl->wait[BLK_RW_ASYNC]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Christoph Lameter19460892005-06-23 00:08:19 -0700537 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
Tejun Heoa91a5ac2012-06-04 20:40:53 -0700538 mempool_free_slab, request_cachep,
Tejun Heo5b788ce2012-06-04 20:40:59 -0700539 gfp_mask, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (!rl->rq_pool)
541 return -ENOMEM;
542
543 return 0;
544}
545
Tejun Heo5b788ce2012-06-04 20:40:59 -0700546void blk_exit_rl(struct request_list *rl)
547{
548 if (rl->rq_pool)
549 mempool_destroy(rl->rq_pool);
550}
551
Jens Axboe165125e2007-07-24 09:28:11 +0200552struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Ezequiel Garciac304a512012-11-10 10:39:44 +0100554 return blk_alloc_queue_node(gfp_mask, NUMA_NO_NODE);
Christoph Lameter19460892005-06-23 00:08:19 -0700555}
556EXPORT_SYMBOL(blk_alloc_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Jens Axboe165125e2007-07-24 09:28:11 +0200558struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -0700559{
Jens Axboe165125e2007-07-24 09:28:11 +0200560 struct request_queue *q;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700561 int err;
Christoph Lameter19460892005-06-23 00:08:19 -0700562
Jens Axboe8324aa92008-01-29 14:51:59 +0100563 q = kmem_cache_alloc_node(blk_requestq_cachep,
Christoph Lameter94f60302007-07-17 04:03:29 -0700564 gfp_mask | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (!q)
566 return NULL;
567
Jens Axboe320ae512013-10-24 09:20:05 +0100568 if (percpu_counter_init(&q->mq_usage_counter, 0))
569 goto fail_q;
570
Dan Carpenter00380a42012-03-23 09:58:54 +0100571 q->id = ida_simple_get(&blk_queue_ida, 0, 0, gfp_mask);
Tejun Heoa73f7302011-12-14 00:33:37 +0100572 if (q->id < 0)
Jens Axboe320ae512013-10-24 09:20:05 +0100573 goto fail_c;
Tejun Heoa73f7302011-12-14 00:33:37 +0100574
Jens Axboe0989a022009-06-12 14:42:56 +0200575 q->backing_dev_info.ra_pages =
576 (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
577 q->backing_dev_info.state = 0;
578 q->backing_dev_info.capabilities = BDI_CAP_MAP_COPY;
Jens Axboed9938312009-06-12 14:45:52 +0200579 q->backing_dev_info.name = "block";
Mike Snitzer51514122011-11-23 10:59:13 +0100580 q->node = node_id;
Jens Axboe0989a022009-06-12 14:42:56 +0200581
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700582 err = bdi_init(&q->backing_dev_info);
Tejun Heoa73f7302011-12-14 00:33:37 +0100583 if (err)
584 goto fail_id;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700585
Matthew Garrett31373d02010-04-06 14:25:14 +0200586 setup_timer(&q->backing_dev_info.laptop_mode_wb_timer,
587 laptop_mode_timer_fn, (unsigned long) q);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700588 setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
Tejun Heob855b042012-03-06 21:24:55 +0100589 INIT_LIST_HEAD(&q->queue_head);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700590 INIT_LIST_HEAD(&q->timeout_list);
Tejun Heoa612fdd2011-12-14 00:33:41 +0100591 INIT_LIST_HEAD(&q->icq_list);
Tejun Heo4eef3042012-03-05 13:15:18 -0800592#ifdef CONFIG_BLK_CGROUP
Tejun Heoe8989fa2012-03-05 13:15:20 -0800593 INIT_LIST_HEAD(&q->blkg_list);
Tejun Heo4eef3042012-03-05 13:15:18 -0800594#endif
Tejun Heoae1b1532011-01-25 12:43:54 +0100595 INIT_LIST_HEAD(&q->flush_queue[0]);
596 INIT_LIST_HEAD(&q->flush_queue[1]);
597 INIT_LIST_HEAD(&q->flush_data_in_flight);
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500598 INIT_DELAYED_WORK(&q->delay_work, blk_delay_work);
Al Viro483f4af2006-03-18 18:34:37 -0500599
Jens Axboe8324aa92008-01-29 14:51:59 +0100600 kobject_init(&q->kobj, &blk_queue_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Al Viro483f4af2006-03-18 18:34:37 -0500602 mutex_init(&q->sysfs_lock);
Neil Browne7e72bf2008-05-14 16:05:54 -0700603 spin_lock_init(&q->__queue_lock);
Al Viro483f4af2006-03-18 18:34:37 -0500604
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500605 /*
606 * By default initialize queue_lock to internal lock and driver can
607 * override it later if need be.
608 */
609 q->queue_lock = &q->__queue_lock;
610
Tejun Heob82d4b12012-04-13 13:11:31 -0700611 /*
612 * A queue starts its life with bypass turned on to avoid
613 * unnecessary bypass on/off overhead and nasty surprises during
Tejun Heo749fefe2012-09-20 14:08:52 -0700614 * init. The initial bypass will be finished when the queue is
615 * registered by blk_register_queue().
Tejun Heob82d4b12012-04-13 13:11:31 -0700616 */
617 q->bypass_depth = 1;
618 __set_bit(QUEUE_FLAG_BYPASS, &q->queue_flags);
619
Jens Axboe320ae512013-10-24 09:20:05 +0100620 init_waitqueue_head(&q->mq_freeze_wq);
621
Tejun Heo5efd6112012-03-05 13:15:12 -0800622 if (blkcg_init_queue(q))
Mikulas Patockafff49962013-10-14 12:11:36 -0400623 goto fail_bdi;
Tejun Heof51b8022012-03-05 13:15:05 -0800624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return q;
Tejun Heoa73f7302011-12-14 00:33:37 +0100626
Mikulas Patockafff49962013-10-14 12:11:36 -0400627fail_bdi:
628 bdi_destroy(&q->backing_dev_info);
Tejun Heoa73f7302011-12-14 00:33:37 +0100629fail_id:
630 ida_simple_remove(&blk_queue_ida, q->id);
Jens Axboe320ae512013-10-24 09:20:05 +0100631fail_c:
632 percpu_counter_destroy(&q->mq_usage_counter);
Tejun Heoa73f7302011-12-14 00:33:37 +0100633fail_q:
634 kmem_cache_free(blk_requestq_cachep, q);
635 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
Christoph Lameter19460892005-06-23 00:08:19 -0700637EXPORT_SYMBOL(blk_alloc_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639/**
640 * blk_init_queue - prepare a request queue for use with a block device
641 * @rfn: The function to be called to process requests that have been
642 * placed on the queue.
643 * @lock: Request queue spin lock
644 *
645 * Description:
646 * If a block device wishes to use the standard request handling procedures,
647 * which sorts requests and coalesces adjacent requests, then it must
648 * call blk_init_queue(). The function @rfn will be called when there
649 * are requests on the queue that need to be processed. If the device
650 * supports plugging, then @rfn may not be called immediately when requests
651 * are available on the queue, but may be called at some time later instead.
652 * Plugged queues are generally unplugged when a buffer belonging to one
653 * of the requests on the queue is needed, or due to memory pressure.
654 *
655 * @rfn is not required, or even expected, to remove all requests off the
656 * queue, but only as many as it can handle at a time. If it does leave
657 * requests on the queue, it is responsible for arranging that the requests
658 * get dealt with eventually.
659 *
660 * The queue spin lock must be held while manipulating the requests on the
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200661 * request queue; this lock will be taken also from interrupt context, so irq
662 * disabling is needed for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 *
Randy Dunlap710027a2008-08-19 20:13:11 +0200664 * Function returns a pointer to the initialized request queue, or %NULL if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 * it didn't succeed.
666 *
667 * Note:
668 * blk_init_queue() must be paired with a blk_cleanup_queue() call
669 * when the block device is deactivated (such as at module unload).
670 **/
Christoph Lameter19460892005-06-23 00:08:19 -0700671
Jens Axboe165125e2007-07-24 09:28:11 +0200672struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Ezequiel Garciac304a512012-11-10 10:39:44 +0100674 return blk_init_queue_node(rfn, lock, NUMA_NO_NODE);
Christoph Lameter19460892005-06-23 00:08:19 -0700675}
676EXPORT_SYMBOL(blk_init_queue);
677
Jens Axboe165125e2007-07-24 09:28:11 +0200678struct request_queue *
Christoph Lameter19460892005-06-23 00:08:19 -0700679blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
680{
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600681 struct request_queue *uninit_q, *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600683 uninit_q = blk_alloc_queue_node(GFP_KERNEL, node_id);
684 if (!uninit_q)
685 return NULL;
686
Mike Snitzer51514122011-11-23 10:59:13 +0100687 q = blk_init_allocated_queue(uninit_q, rfn, lock);
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600688 if (!q)
689 blk_cleanup_queue(uninit_q);
690
691 return q;
Mike Snitzer01effb02010-05-11 08:57:42 +0200692}
693EXPORT_SYMBOL(blk_init_queue_node);
694
695struct request_queue *
696blk_init_allocated_queue(struct request_queue *q, request_fn_proc *rfn,
697 spinlock_t *lock)
698{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if (!q)
700 return NULL;
701
Tejun Heoa0516612012-06-26 15:05:44 -0700702 if (blk_init_rl(&q->root_rl, q, GFP_KERNEL))
Al Viro8669aaf2006-03-18 13:50:00 -0500703 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 q->request_fn = rfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 q->prep_rq_fn = NULL;
James Bottomley28018c22010-07-01 19:49:17 +0900707 q->unprep_rq_fn = NULL;
Tejun Heo60ea8222012-09-20 14:09:30 -0700708 q->queue_flags |= QUEUE_FLAG_DEFAULT;
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500709
710 /* Override internal queue lock with supplied lock pointer */
711 if (lock)
712 q->queue_lock = lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Jens Axboef3b144a2009-03-06 08:48:33 +0100714 /*
715 * This also sets hw/phys segments, boundary and size
716 */
Jens Axboec20e8de2011-09-12 12:03:37 +0200717 blk_queue_make_request(q, blk_queue_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Alan Stern44ec9542007-02-20 11:01:57 -0500719 q->sg_reserved_size = INT_MAX;
720
Tomoki Sekiyamaeb1c1602013-10-15 16:42:16 -0600721 /* Protect q->elevator from elevator_change */
722 mutex_lock(&q->sysfs_lock);
723
Tejun Heob82d4b12012-04-13 13:11:31 -0700724 /* init elevator */
Tomoki Sekiyamaeb1c1602013-10-15 16:42:16 -0600725 if (elevator_init(q, NULL)) {
726 mutex_unlock(&q->sysfs_lock);
Tejun Heob82d4b12012-04-13 13:11:31 -0700727 return NULL;
Tomoki Sekiyamaeb1c1602013-10-15 16:42:16 -0600728 }
729
730 mutex_unlock(&q->sysfs_lock);
731
Tejun Heob82d4b12012-04-13 13:11:31 -0700732 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
Mike Snitzer51514122011-11-23 10:59:13 +0100734EXPORT_SYMBOL(blk_init_allocated_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Tejun Heo09ac46c2011-12-14 00:33:38 +0100736bool blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100738 if (likely(!blk_queue_dying(q))) {
Tejun Heo09ac46c2011-12-14 00:33:38 +0100739 __blk_get_queue(q);
740 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
742
Tejun Heo09ac46c2011-12-14 00:33:38 +0100743 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
Jens Axboed86e0e82011-05-27 07:44:43 +0200745EXPORT_SYMBOL(blk_get_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Tejun Heo5b788ce2012-06-04 20:40:59 -0700747static inline void blk_free_request(struct request_list *rl, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Tejun Heof1f8cc92011-12-14 00:33:42 +0100749 if (rq->cmd_flags & REQ_ELVPRIV) {
Tejun Heo5b788ce2012-06-04 20:40:59 -0700750 elv_put_request(rl->q, rq);
Tejun Heof1f8cc92011-12-14 00:33:42 +0100751 if (rq->elv.icq)
Tejun Heo11a31222012-02-07 07:51:30 +0100752 put_io_context(rq->elv.icq->ioc);
Tejun Heof1f8cc92011-12-14 00:33:42 +0100753 }
754
Tejun Heo5b788ce2012-06-04 20:40:59 -0700755 mempool_free(rq, rl->rq_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758/*
759 * ioc_batching returns true if the ioc is a valid batching request and
760 * should be given priority access to a request.
761 */
Jens Axboe165125e2007-07-24 09:28:11 +0200762static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
764 if (!ioc)
765 return 0;
766
767 /*
768 * Make sure the process is able to allocate at least 1 request
769 * even if the batch times out, otherwise we could theoretically
770 * lose wakeups.
771 */
772 return ioc->nr_batch_requests == q->nr_batching ||
773 (ioc->nr_batch_requests > 0
774 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
775}
776
777/*
778 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
779 * will cause the process to be a "batcher" on all queues in the system. This
780 * is the behaviour we want though - once it gets a wakeup it should be given
781 * a nice run.
782 */
Jens Axboe165125e2007-07-24 09:28:11 +0200783static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
785 if (!ioc || ioc_batching(q, ioc))
786 return;
787
788 ioc->nr_batch_requests = q->nr_batching;
789 ioc->last_waited = jiffies;
790}
791
Tejun Heo5b788ce2012-06-04 20:40:59 -0700792static void __freed_request(struct request_list *rl, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Tejun Heo5b788ce2012-06-04 20:40:59 -0700794 struct request_queue *q = rl->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Tejun Heoa0516612012-06-26 15:05:44 -0700796 /*
797 * bdi isn't aware of blkcg yet. As all async IOs end up root
798 * blkcg anyway, just use root blkcg state.
799 */
800 if (rl == &q->root_rl &&
801 rl->count[sync] < queue_congestion_off_threshold(q))
Jens Axboe1faa16d2009-04-06 14:48:01 +0200802 blk_clear_queue_congested(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Jens Axboe1faa16d2009-04-06 14:48:01 +0200804 if (rl->count[sync] + 1 <= q->nr_requests) {
805 if (waitqueue_active(&rl->wait[sync]))
806 wake_up(&rl->wait[sync]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Tejun Heo5b788ce2012-06-04 20:40:59 -0700808 blk_clear_rl_full(rl, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
810}
811
812/*
813 * A request has just been released. Account for it, update the full and
814 * congestion status, wake up any waiters. Called under q->queue_lock.
815 */
Tejun Heo5b788ce2012-06-04 20:40:59 -0700816static void freed_request(struct request_list *rl, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Tejun Heo5b788ce2012-06-04 20:40:59 -0700818 struct request_queue *q = rl->q;
Tejun Heo75eb6c32011-10-19 14:31:22 +0200819 int sync = rw_is_sync(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Tejun Heo8a5ecdd2012-06-04 20:40:58 -0700821 q->nr_rqs[sync]--;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200822 rl->count[sync]--;
Tejun Heo75eb6c32011-10-19 14:31:22 +0200823 if (flags & REQ_ELVPRIV)
Tejun Heo8a5ecdd2012-06-04 20:40:58 -0700824 q->nr_rqs_elvpriv--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Tejun Heo5b788ce2012-06-04 20:40:59 -0700826 __freed_request(rl, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Jens Axboe1faa16d2009-04-06 14:48:01 +0200828 if (unlikely(rl->starved[sync ^ 1]))
Tejun Heo5b788ce2012-06-04 20:40:59 -0700829 __freed_request(rl, sync ^ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832/*
Mike Snitzer9d5a4e92011-02-11 11:05:46 +0100833 * Determine if elevator data should be initialized when allocating the
834 * request associated with @bio.
835 */
836static bool blk_rq_should_init_elevator(struct bio *bio)
837{
838 if (!bio)
839 return true;
840
841 /*
842 * Flush requests do not use the elevator so skip initialization.
843 * This allows a request to share the flush and elevator data.
844 */
845 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA))
846 return false;
847
848 return true;
849}
850
Tejun Heoda8303c2011-10-19 14:33:05 +0200851/**
Tejun Heo852c7882012-03-05 13:15:27 -0800852 * rq_ioc - determine io_context for request allocation
853 * @bio: request being allocated is for this bio (can be %NULL)
854 *
855 * Determine io_context to use for request allocation for @bio. May return
856 * %NULL if %current->io_context doesn't exist.
857 */
858static struct io_context *rq_ioc(struct bio *bio)
859{
860#ifdef CONFIG_BLK_CGROUP
861 if (bio && bio->bi_ioc)
862 return bio->bi_ioc;
863#endif
864 return current->io_context;
865}
866
867/**
Tejun Heoa06e05e2012-06-04 20:40:55 -0700868 * __get_request - get a free request
Tejun Heo5b788ce2012-06-04 20:40:59 -0700869 * @rl: request list to allocate from
Tejun Heoda8303c2011-10-19 14:33:05 +0200870 * @rw_flags: RW and SYNC flags
871 * @bio: bio to allocate request for (can be %NULL)
872 * @gfp_mask: allocation mask
873 *
874 * Get a free request from @q. This function may fail under memory
875 * pressure or if @q is dead.
876 *
877 * Must be callled with @q->queue_lock held and,
878 * Returns %NULL on failure, with @q->queue_lock held.
879 * Returns !%NULL on success, with @q->queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 */
Tejun Heo5b788ce2012-06-04 20:40:59 -0700881static struct request *__get_request(struct request_list *rl, int rw_flags,
Tejun Heoa06e05e2012-06-04 20:40:55 -0700882 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Tejun Heo5b788ce2012-06-04 20:40:59 -0700884 struct request_queue *q = rl->q;
Tejun Heob6792812012-03-05 13:15:23 -0800885 struct request *rq;
Tejun Heo7f4b35d2012-06-04 20:40:56 -0700886 struct elevator_type *et = q->elevator->type;
887 struct io_context *ioc = rq_ioc(bio);
Tejun Heof1f8cc92011-12-14 00:33:42 +0100888 struct io_cq *icq = NULL;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200889 const bool is_sync = rw_is_sync(rw_flags) != 0;
Tejun Heo75eb6c32011-10-19 14:31:22 +0200890 int may_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100892 if (unlikely(blk_queue_dying(q)))
Tejun Heoda8303c2011-10-19 14:33:05 +0200893 return NULL;
894
Jens Axboe7749a8d2006-12-13 13:02:26 +0100895 may_queue = elv_may_queue(q, rw_flags);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100896 if (may_queue == ELV_MQUEUE_NO)
897 goto rq_starved;
898
Jens Axboe1faa16d2009-04-06 14:48:01 +0200899 if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) {
900 if (rl->count[is_sync]+1 >= q->nr_requests) {
Tejun Heof2dbd762011-12-14 00:33:40 +0100901 /*
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100902 * The queue will fill after this allocation, so set
903 * it as full, and mark this process as "batching".
904 * This process will be allowed to complete a batch of
905 * requests, others will be blocked.
906 */
Tejun Heo5b788ce2012-06-04 20:40:59 -0700907 if (!blk_rl_full(rl, is_sync)) {
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100908 ioc_set_batching(q, ioc);
Tejun Heo5b788ce2012-06-04 20:40:59 -0700909 blk_set_rl_full(rl, is_sync);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100910 } else {
911 if (may_queue != ELV_MQUEUE_MUST
912 && !ioc_batching(q, ioc)) {
913 /*
914 * The queue is full and the allocating
915 * process is not a "batcher", and not
916 * exempted by the IO scheduler
917 */
Tejun Heob6792812012-03-05 13:15:23 -0800918 return NULL;
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100919 }
920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
Tejun Heoa0516612012-06-26 15:05:44 -0700922 /*
923 * bdi isn't aware of blkcg yet. As all async IOs end up
924 * root blkcg anyway, just use root blkcg state.
925 */
926 if (rl == &q->root_rl)
927 blk_set_queue_congested(q, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
929
Jens Axboe082cf692005-06-28 16:35:11 +0200930 /*
931 * Only allow batching queuers to allocate up to 50% over the defined
932 * limit of requests, otherwise we could have thousands of requests
933 * allocated with any setting of ->nr_requests
934 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200935 if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
Tejun Heob6792812012-03-05 13:15:23 -0800936 return NULL;
Hugh Dickinsfd782a42005-06-29 15:15:40 +0100937
Tejun Heo8a5ecdd2012-06-04 20:40:58 -0700938 q->nr_rqs[is_sync]++;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200939 rl->count[is_sync]++;
940 rl->starved[is_sync] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200941
Tejun Heof1f8cc92011-12-14 00:33:42 +0100942 /*
943 * Decide whether the new request will be managed by elevator. If
944 * so, mark @rw_flags and increment elvpriv. Non-zero elvpriv will
945 * prevent the current elevator from being destroyed until the new
946 * request is freed. This guarantees icq's won't be destroyed and
947 * makes creating new ones safe.
948 *
949 * Also, lookup icq while holding queue_lock. If it doesn't exist,
950 * it will be created after releasing queue_lock.
951 */
Tejun Heod7325802012-03-05 13:14:58 -0800952 if (blk_rq_should_init_elevator(bio) && !blk_queue_bypass(q)) {
Tejun Heo75eb6c32011-10-19 14:31:22 +0200953 rw_flags |= REQ_ELVPRIV;
Tejun Heo8a5ecdd2012-06-04 20:40:58 -0700954 q->nr_rqs_elvpriv++;
Tejun Heof1f8cc92011-12-14 00:33:42 +0100955 if (et->icq_cache && ioc)
956 icq = ioc_lookup_icq(ioc, q);
Mike Snitzer9d5a4e92011-02-11 11:05:46 +0100957 }
Tejun Heocb98fc82005-10-28 08:29:39 +0200958
Jens Axboef253b862010-10-24 22:06:02 +0200959 if (blk_queue_io_stat(q))
960 rw_flags |= REQ_IO_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 spin_unlock_irq(q->queue_lock);
962
Tejun Heo29e2b092012-04-19 16:29:21 -0700963 /* allocate and init request */
Tejun Heo5b788ce2012-06-04 20:40:59 -0700964 rq = mempool_alloc(rl->rq_pool, gfp_mask);
Tejun Heo29e2b092012-04-19 16:29:21 -0700965 if (!rq)
Tejun Heob6792812012-03-05 13:15:23 -0800966 goto fail_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Tejun Heo29e2b092012-04-19 16:29:21 -0700968 blk_rq_init(q, rq);
Tejun Heoa0516612012-06-26 15:05:44 -0700969 blk_rq_set_rl(rq, rl);
Tejun Heo29e2b092012-04-19 16:29:21 -0700970 rq->cmd_flags = rw_flags | REQ_ALLOCED;
971
Tejun Heoaaf7c682012-04-19 16:29:22 -0700972 /* init elvpriv */
Tejun Heo29e2b092012-04-19 16:29:21 -0700973 if (rw_flags & REQ_ELVPRIV) {
Tejun Heoaaf7c682012-04-19 16:29:22 -0700974 if (unlikely(et->icq_cache && !icq)) {
Tejun Heo7f4b35d2012-06-04 20:40:56 -0700975 if (ioc)
976 icq = ioc_create_icq(ioc, q, gfp_mask);
Tejun Heoaaf7c682012-04-19 16:29:22 -0700977 if (!icq)
978 goto fail_elvpriv;
Tejun Heo29e2b092012-04-19 16:29:21 -0700979 }
Tejun Heoaaf7c682012-04-19 16:29:22 -0700980
981 rq->elv.icq = icq;
982 if (unlikely(elv_set_request(q, rq, bio, gfp_mask)))
983 goto fail_elvpriv;
984
985 /* @rq->elv.icq holds io_context until @rq is freed */
Tejun Heo29e2b092012-04-19 16:29:21 -0700986 if (icq)
987 get_io_context(icq->ioc);
988 }
Tejun Heoaaf7c682012-04-19 16:29:22 -0700989out:
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100990 /*
991 * ioc may be NULL here, and ioc_batching will be false. That's
992 * OK, if the queue is under the request limit then requests need
993 * not count toward the nr_batch_requests limit. There will always
994 * be some limit enforced by BLK_BATCH_TIME.
995 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (ioc_batching(q, ioc))
997 ioc->nr_batch_requests--;
Jens Axboe6728cb02008-01-31 13:03:55 +0100998
Jens Axboe1faa16d2009-04-06 14:48:01 +0200999 trace_block_getrq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return rq;
Tejun Heob6792812012-03-05 13:15:23 -08001001
Tejun Heoaaf7c682012-04-19 16:29:22 -07001002fail_elvpriv:
1003 /*
1004 * elvpriv init failed. ioc, icq and elvpriv aren't mempool backed
1005 * and may fail indefinitely under memory pressure and thus
1006 * shouldn't stall IO. Treat this request as !elvpriv. This will
1007 * disturb iosched and blkcg but weird is bettern than dead.
1008 */
1009 printk_ratelimited(KERN_WARNING "%s: request aux data allocation failed, iosched may be disturbed\n",
1010 dev_name(q->backing_dev_info.dev));
1011
1012 rq->cmd_flags &= ~REQ_ELVPRIV;
1013 rq->elv.icq = NULL;
1014
1015 spin_lock_irq(q->queue_lock);
Tejun Heo8a5ecdd2012-06-04 20:40:58 -07001016 q->nr_rqs_elvpriv--;
Tejun Heoaaf7c682012-04-19 16:29:22 -07001017 spin_unlock_irq(q->queue_lock);
1018 goto out;
1019
Tejun Heob6792812012-03-05 13:15:23 -08001020fail_alloc:
1021 /*
1022 * Allocation failed presumably due to memory. Undo anything we
1023 * might have messed up.
1024 *
1025 * Allocating task should really be put onto the front of the wait
1026 * queue, but this is pretty rare.
1027 */
1028 spin_lock_irq(q->queue_lock);
Tejun Heo5b788ce2012-06-04 20:40:59 -07001029 freed_request(rl, rw_flags);
Tejun Heob6792812012-03-05 13:15:23 -08001030
1031 /*
1032 * in the very unlikely event that allocation failed and no
1033 * requests for this direction was pending, mark us starved so that
1034 * freeing of a request in the other direction will notice
1035 * us. another possible fix would be to split the rq mempool into
1036 * READ and WRITE
1037 */
1038rq_starved:
1039 if (unlikely(rl->count[is_sync] == 0))
1040 rl->starved[is_sync] = 1;
1041 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}
1043
Tejun Heoda8303c2011-10-19 14:33:05 +02001044/**
Tejun Heoa06e05e2012-06-04 20:40:55 -07001045 * get_request - get a free request
Tejun Heoda8303c2011-10-19 14:33:05 +02001046 * @q: request_queue to allocate request from
1047 * @rw_flags: RW and SYNC flags
1048 * @bio: bio to allocate request for (can be %NULL)
Tejun Heoa06e05e2012-06-04 20:40:55 -07001049 * @gfp_mask: allocation mask
Nick Piggind6344532005-06-28 20:45:14 -07001050 *
Tejun Heoa06e05e2012-06-04 20:40:55 -07001051 * Get a free request from @q. If %__GFP_WAIT is set in @gfp_mask, this
1052 * function keeps retrying under memory pressure and fails iff @q is dead.
Tejun Heoda8303c2011-10-19 14:33:05 +02001053 *
1054 * Must be callled with @q->queue_lock held and,
1055 * Returns %NULL on failure, with @q->queue_lock held.
1056 * Returns !%NULL on success, with @q->queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 */
Tejun Heoa06e05e2012-06-04 20:40:55 -07001058static struct request *get_request(struct request_queue *q, int rw_flags,
1059 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
Jens Axboe1faa16d2009-04-06 14:48:01 +02001061 const bool is_sync = rw_is_sync(rw_flags) != 0;
Tejun Heoa06e05e2012-06-04 20:40:55 -07001062 DEFINE_WAIT(wait);
Tejun Heoa0516612012-06-26 15:05:44 -07001063 struct request_list *rl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 struct request *rq;
Tejun Heoa0516612012-06-26 15:05:44 -07001065
1066 rl = blk_get_rl(q, bio); /* transferred to @rq on success */
Tejun Heoa06e05e2012-06-04 20:40:55 -07001067retry:
Tejun Heoa0516612012-06-26 15:05:44 -07001068 rq = __get_request(rl, rw_flags, bio, gfp_mask);
Tejun Heoa06e05e2012-06-04 20:40:55 -07001069 if (rq)
1070 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Bart Van Assche3f3299d2012-11-28 13:42:38 +01001072 if (!(gfp_mask & __GFP_WAIT) || unlikely(blk_queue_dying(q))) {
Tejun Heoa0516612012-06-26 15:05:44 -07001073 blk_put_rl(rl);
Tejun Heoa06e05e2012-06-04 20:40:55 -07001074 return NULL;
Tejun Heoa0516612012-06-26 15:05:44 -07001075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Tejun Heoa06e05e2012-06-04 20:40:55 -07001077 /* wait on @rl and retry */
1078 prepare_to_wait_exclusive(&rl->wait[is_sync], &wait,
1079 TASK_UNINTERRUPTIBLE);
Tejun Heoda8303c2011-10-19 14:33:05 +02001080
Tejun Heoa06e05e2012-06-04 20:40:55 -07001081 trace_block_sleeprq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Tejun Heoa06e05e2012-06-04 20:40:55 -07001083 spin_unlock_irq(q->queue_lock);
1084 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Tejun Heoa06e05e2012-06-04 20:40:55 -07001086 /*
1087 * After sleeping, we become a "batching" process and will be able
1088 * to allocate at least one request, and up to a big batch of them
1089 * for a small period time. See ioc_batching, ioc_set_batching
1090 */
Tejun Heoa06e05e2012-06-04 20:40:55 -07001091 ioc_set_batching(q, current->io_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Tejun Heoa06e05e2012-06-04 20:40:55 -07001093 spin_lock_irq(q->queue_lock);
1094 finish_wait(&rl->wait[is_sync], &wait);
Jens Axboe2056a782006-03-23 20:00:26 +01001095
Tejun Heoa06e05e2012-06-04 20:40:55 -07001096 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097}
1098
Jens Axboe320ae512013-10-24 09:20:05 +01001099static struct request *blk_old_get_request(struct request_queue *q, int rw,
1100 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
1102 struct request *rq;
1103
1104 BUG_ON(rw != READ && rw != WRITE);
1105
Tejun Heo7f4b35d2012-06-04 20:40:56 -07001106 /* create ioc upfront */
1107 create_io_context(gfp_mask, q->node);
1108
Nick Piggind6344532005-06-28 20:45:14 -07001109 spin_lock_irq(q->queue_lock);
Tejun Heoa06e05e2012-06-04 20:40:55 -07001110 rq = get_request(q, rw, NULL, gfp_mask);
Tejun Heoda8303c2011-10-19 14:33:05 +02001111 if (!rq)
1112 spin_unlock_irq(q->queue_lock);
Nick Piggind6344532005-06-28 20:45:14 -07001113 /* q->queue_lock is unlocked at this point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 return rq;
1116}
Jens Axboe320ae512013-10-24 09:20:05 +01001117
1118struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
1119{
1120 if (q->mq_ops)
Christoph Hellwig3228f482013-10-28 13:33:58 -06001121 return blk_mq_alloc_request(q, rw, gfp_mask, false);
Jens Axboe320ae512013-10-24 09:20:05 +01001122 else
1123 return blk_old_get_request(q, rw, gfp_mask);
1124}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125EXPORT_SYMBOL(blk_get_request);
1126
1127/**
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001128 * blk_make_request - given a bio, allocate a corresponding struct request.
Randy Dunlap8ebf9752009-06-11 20:00:41 -07001129 * @q: target request queue
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001130 * @bio: The bio describing the memory mappings that will be submitted for IO.
1131 * It may be a chained-bio properly constructed by block/bio layer.
Randy Dunlap8ebf9752009-06-11 20:00:41 -07001132 * @gfp_mask: gfp flags to be used for memory allocation
Jens Axboedc72ef42006-07-20 14:54:05 +02001133 *
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001134 * blk_make_request is the parallel of generic_make_request for BLOCK_PC
1135 * type commands. Where the struct request needs to be farther initialized by
1136 * the caller. It is passed a &struct bio, which describes the memory info of
1137 * the I/O transfer.
1138 *
1139 * The caller of blk_make_request must make sure that bi_io_vec
1140 * are set to describe the memory buffers. That bio_data_dir() will return
1141 * the needed direction of the request. (And all bio's in the passed bio-chain
1142 * are properly set accordingly)
1143 *
1144 * If called under none-sleepable conditions, mapped bio buffers must not
1145 * need bouncing, by calling the appropriate masked or flagged allocator,
1146 * suitable for the target device. Otherwise the call to blk_queue_bounce will
1147 * BUG.
Jens Axboe53674ac2009-05-19 19:52:35 +02001148 *
1149 * WARNING: When allocating/cloning a bio-chain, careful consideration should be
1150 * given to how you allocate bios. In particular, you cannot use __GFP_WAIT for
1151 * anything but the first bio in the chain. Otherwise you risk waiting for IO
1152 * completion of a bio that hasn't been submitted yet, thus resulting in a
1153 * deadlock. Alternatively bios should be allocated using bio_kmalloc() instead
1154 * of bio_alloc(), as that avoids the mempool deadlock.
1155 * If possible a big IO should be split into smaller parts when allocation
1156 * fails. Partial allocation should not be an error, or you risk a live-lock.
Jens Axboedc72ef42006-07-20 14:54:05 +02001157 */
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001158struct request *blk_make_request(struct request_queue *q, struct bio *bio,
1159 gfp_t gfp_mask)
Jens Axboedc72ef42006-07-20 14:54:05 +02001160{
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001161 struct request *rq = blk_get_request(q, bio_data_dir(bio), gfp_mask);
1162
1163 if (unlikely(!rq))
1164 return ERR_PTR(-ENOMEM);
1165
1166 for_each_bio(bio) {
1167 struct bio *bounce_bio = bio;
1168 int ret;
1169
1170 blk_queue_bounce(q, &bounce_bio);
1171 ret = blk_rq_append_bio(q, rq, bounce_bio);
1172 if (unlikely(ret)) {
1173 blk_put_request(rq);
1174 return ERR_PTR(ret);
1175 }
1176 }
1177
1178 return rq;
Jens Axboedc72ef42006-07-20 14:54:05 +02001179}
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001180EXPORT_SYMBOL(blk_make_request);
Jens Axboedc72ef42006-07-20 14:54:05 +02001181
1182/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 * blk_requeue_request - put a request back on queue
1184 * @q: request queue where request should be inserted
1185 * @rq: request to be inserted
1186 *
1187 * Description:
1188 * Drivers often keep queueing requests until the hardware cannot accept
1189 * more, when that condition happens we need to put the request back
1190 * on the queue. Must be called with queue lock held.
1191 */
Jens Axboe165125e2007-07-24 09:28:11 +02001192void blk_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
Jens Axboe242f9dc2008-09-14 05:55:09 -07001194 blk_delete_timer(rq);
1195 blk_clear_rq_complete(rq);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001196 trace_block_rq_requeue(q, rq);
Jens Axboe2056a782006-03-23 20:00:26 +01001197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 if (blk_rq_tagged(rq))
1199 blk_queue_end_tag(q, rq);
1200
James Bottomleyba396a62009-05-27 14:17:08 +02001201 BUG_ON(blk_queued_rq(rq));
1202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 elv_requeue_request(q, rq);
1204}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205EXPORT_SYMBOL(blk_requeue_request);
1206
Jens Axboe73c10102011-03-08 13:19:51 +01001207static void add_acct_request(struct request_queue *q, struct request *rq,
1208 int where)
1209{
Jens Axboe320ae512013-10-24 09:20:05 +01001210 blk_account_io_start(rq, true);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001211 __elv_add_request(q, rq, where);
Jens Axboe73c10102011-03-08 13:19:51 +01001212}
1213
Tejun Heo074a7ac2008-08-25 19:56:14 +09001214static void part_round_stats_single(int cpu, struct hd_struct *part,
1215 unsigned long now)
1216{
1217 if (now == part->stamp)
1218 return;
1219
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001220 if (part_in_flight(part)) {
Tejun Heo074a7ac2008-08-25 19:56:14 +09001221 __part_stat_add(cpu, part, time_in_queue,
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001222 part_in_flight(part) * (now - part->stamp));
Tejun Heo074a7ac2008-08-25 19:56:14 +09001223 __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
1224 }
1225 part->stamp = now;
1226}
1227
1228/**
Randy Dunlap496aa8a2008-10-16 07:46:23 +02001229 * part_round_stats() - Round off the performance stats on a struct disk_stats.
1230 * @cpu: cpu number for stats access
1231 * @part: target partition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 *
1233 * The average IO queue length and utilisation statistics are maintained
1234 * by observing the current state of the queue length and the amount of
1235 * time it has been in this state for.
1236 *
1237 * Normally, that accounting is done on IO completion, but that can result
1238 * in more than a second's worth of IO being accounted for within any one
1239 * second, leading to >100% utilisation. To deal with that, we call this
1240 * function to do a round-off before returning the results when reading
1241 * /proc/diskstats. This accounts immediately for all queue usage up to
1242 * the current jiffies and restarts the counters again.
1243 */
Tejun Heoc9959052008-08-25 19:47:21 +09001244void part_round_stats(int cpu, struct hd_struct *part)
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001245{
1246 unsigned long now = jiffies;
1247
Tejun Heo074a7ac2008-08-25 19:56:14 +09001248 if (part->partno)
1249 part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
1250 part_round_stats_single(cpu, part, now);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001251}
Tejun Heo074a7ac2008-08-25 19:56:14 +09001252EXPORT_SYMBOL_GPL(part_round_stats);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001253
Lin Mingc8158812013-03-23 11:42:27 +08001254#ifdef CONFIG_PM_RUNTIME
1255static void blk_pm_put_request(struct request *rq)
1256{
1257 if (rq->q->dev && !(rq->cmd_flags & REQ_PM) && !--rq->q->nr_pending)
1258 pm_runtime_mark_last_busy(rq->q->dev);
1259}
1260#else
1261static inline void blk_pm_put_request(struct request *rq) {}
1262#endif
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264/*
1265 * queue lock must be held
1266 */
Jens Axboe165125e2007-07-24 09:28:11 +02001267void __blk_put_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if (unlikely(!q))
1270 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Lin Mingc8158812013-03-23 11:42:27 +08001272 blk_pm_put_request(req);
1273
Tejun Heo8922e162005-10-20 16:23:44 +02001274 elv_completed_request(q, req);
1275
Boaz Harrosh1cd96c22009-03-24 12:35:07 +01001276 /* this is a bio leak */
1277 WARN_ON(req->bio != NULL);
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 /*
1280 * Request may not have originated from ll_rw_blk. if not,
1281 * it didn't come out of our reserved rq pools
1282 */
Jens Axboe49171e52006-08-10 08:59:11 +02001283 if (req->cmd_flags & REQ_ALLOCED) {
Tejun Heo75eb6c32011-10-19 14:31:22 +02001284 unsigned int flags = req->cmd_flags;
Tejun Heoa0516612012-06-26 15:05:44 -07001285 struct request_list *rl = blk_rq_rl(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 BUG_ON(!list_empty(&req->queuelist));
Jens Axboe98170642006-07-28 09:23:08 +02001288 BUG_ON(!hlist_unhashed(&req->hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Tejun Heoa0516612012-06-26 15:05:44 -07001290 blk_free_request(rl, req);
1291 freed_request(rl, flags);
1292 blk_put_rl(rl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
1294}
Mike Christie6e39b69e2005-11-11 05:30:24 -06001295EXPORT_SYMBOL_GPL(__blk_put_request);
1296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297void blk_put_request(struct request *req)
1298{
Jens Axboe165125e2007-07-24 09:28:11 +02001299 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Jens Axboe320ae512013-10-24 09:20:05 +01001301 if (q->mq_ops)
1302 blk_mq_free_request(req);
1303 else {
1304 unsigned long flags;
1305
1306 spin_lock_irqsave(q->queue_lock, flags);
1307 __blk_put_request(q, req);
1308 spin_unlock_irqrestore(q->queue_lock, flags);
1309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311EXPORT_SYMBOL(blk_put_request);
1312
Christoph Hellwig66ac0282010-06-18 16:59:42 +02001313/**
1314 * blk_add_request_payload - add a payload to a request
1315 * @rq: request to update
1316 * @page: page backing the payload
1317 * @len: length of the payload.
1318 *
1319 * This allows to later add a payload to an already submitted request by
1320 * a block driver. The driver needs to take care of freeing the payload
1321 * itself.
1322 *
1323 * Note that this is a quite horrible hack and nothing but handling of
1324 * discard requests should ever use it.
1325 */
1326void blk_add_request_payload(struct request *rq, struct page *page,
1327 unsigned int len)
1328{
1329 struct bio *bio = rq->bio;
1330
1331 bio->bi_io_vec->bv_page = page;
1332 bio->bi_io_vec->bv_offset = 0;
1333 bio->bi_io_vec->bv_len = len;
1334
Kent Overstreet4f024f32013-10-11 15:44:27 -07001335 bio->bi_iter.bi_size = len;
Christoph Hellwig66ac0282010-06-18 16:59:42 +02001336 bio->bi_vcnt = 1;
1337 bio->bi_phys_segments = 1;
1338
1339 rq->__data_len = rq->resid_len = len;
1340 rq->nr_phys_segments = 1;
1341 rq->buffer = bio_data(bio);
1342}
1343EXPORT_SYMBOL_GPL(blk_add_request_payload);
1344
Jens Axboe320ae512013-10-24 09:20:05 +01001345bool bio_attempt_back_merge(struct request_queue *q, struct request *req,
1346 struct bio *bio)
Jens Axboe73c10102011-03-08 13:19:51 +01001347{
1348 const int ff = bio->bi_rw & REQ_FAILFAST_MASK;
1349
Jens Axboe73c10102011-03-08 13:19:51 +01001350 if (!ll_back_merge_fn(q, req, bio))
1351 return false;
1352
Tejun Heo8c1cf6b2013-01-11 13:06:34 -08001353 trace_block_bio_backmerge(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001354
1355 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1356 blk_rq_set_mixed_merge(req);
1357
1358 req->biotail->bi_next = bio;
1359 req->biotail = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001360 req->__data_len += bio->bi_iter.bi_size;
Jens Axboe73c10102011-03-08 13:19:51 +01001361 req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
1362
Jens Axboe320ae512013-10-24 09:20:05 +01001363 blk_account_io_start(req, false);
Jens Axboe73c10102011-03-08 13:19:51 +01001364 return true;
1365}
1366
Jens Axboe320ae512013-10-24 09:20:05 +01001367bool bio_attempt_front_merge(struct request_queue *q, struct request *req,
1368 struct bio *bio)
Jens Axboe73c10102011-03-08 13:19:51 +01001369{
1370 const int ff = bio->bi_rw & REQ_FAILFAST_MASK;
Jens Axboe73c10102011-03-08 13:19:51 +01001371
Jens Axboe73c10102011-03-08 13:19:51 +01001372 if (!ll_front_merge_fn(q, req, bio))
1373 return false;
1374
Tejun Heo8c1cf6b2013-01-11 13:06:34 -08001375 trace_block_bio_frontmerge(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001376
1377 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1378 blk_rq_set_mixed_merge(req);
1379
Jens Axboe73c10102011-03-08 13:19:51 +01001380 bio->bi_next = req->bio;
1381 req->bio = bio;
1382
1383 /*
1384 * may not be valid. if the low level driver said
1385 * it didn't need a bounce buffer then it better
1386 * not touch req->buffer either...
1387 */
1388 req->buffer = bio_data(bio);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001389 req->__sector = bio->bi_iter.bi_sector;
1390 req->__data_len += bio->bi_iter.bi_size;
Jens Axboe73c10102011-03-08 13:19:51 +01001391 req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
1392
Jens Axboe320ae512013-10-24 09:20:05 +01001393 blk_account_io_start(req, false);
Jens Axboe73c10102011-03-08 13:19:51 +01001394 return true;
1395}
1396
Tejun Heobd87b582011-10-19 14:33:08 +02001397/**
Jens Axboe320ae512013-10-24 09:20:05 +01001398 * blk_attempt_plug_merge - try to merge with %current's plugged list
Tejun Heobd87b582011-10-19 14:33:08 +02001399 * @q: request_queue new bio is being queued at
1400 * @bio: new bio being queued
1401 * @request_count: out parameter for number of traversed plugged requests
1402 *
1403 * Determine whether @bio being queued on @q can be merged with a request
1404 * on %current's plugged list. Returns %true if merge was successful,
1405 * otherwise %false.
1406 *
Tejun Heo07c2bd32012-02-08 09:19:42 +01001407 * Plugging coalesces IOs from the same issuer for the same purpose without
1408 * going through @q->queue_lock. As such it's more of an issuing mechanism
1409 * than scheduling, and the request, while may have elvpriv data, is not
1410 * added on the elevator at this point. In addition, we don't have
1411 * reliable access to the elevator outside queue lock. Only check basic
1412 * merging parameters without querying the elevator.
Jens Axboe73c10102011-03-08 13:19:51 +01001413 */
Jens Axboe320ae512013-10-24 09:20:05 +01001414bool blk_attempt_plug_merge(struct request_queue *q, struct bio *bio,
1415 unsigned int *request_count)
Jens Axboe73c10102011-03-08 13:19:51 +01001416{
1417 struct blk_plug *plug;
1418 struct request *rq;
1419 bool ret = false;
Shaohua Li92f399c2013-10-29 12:01:03 -06001420 struct list_head *plug_list;
Jens Axboe73c10102011-03-08 13:19:51 +01001421
Alireza Haghdoost23779fb2013-10-23 17:08:16 +01001422 if (blk_queue_nomerges(q))
1423 goto out;
1424
Tejun Heobd87b582011-10-19 14:33:08 +02001425 plug = current->plug;
Jens Axboe73c10102011-03-08 13:19:51 +01001426 if (!plug)
1427 goto out;
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001428 *request_count = 0;
Jens Axboe73c10102011-03-08 13:19:51 +01001429
Shaohua Li92f399c2013-10-29 12:01:03 -06001430 if (q->mq_ops)
1431 plug_list = &plug->mq_list;
1432 else
1433 plug_list = &plug->list;
1434
1435 list_for_each_entry_reverse(rq, plug_list, queuelist) {
Jens Axboe73c10102011-03-08 13:19:51 +01001436 int el_ret;
1437
Shaohua Li1b2e19f2012-04-06 11:37:47 -06001438 if (rq->q == q)
1439 (*request_count)++;
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001440
Tejun Heo07c2bd32012-02-08 09:19:42 +01001441 if (rq->q != q || !blk_rq_merge_ok(rq, bio))
Jens Axboe73c10102011-03-08 13:19:51 +01001442 continue;
1443
Tejun Heo050c8ea2012-02-08 09:19:38 +01001444 el_ret = blk_try_merge(rq, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001445 if (el_ret == ELEVATOR_BACK_MERGE) {
1446 ret = bio_attempt_back_merge(q, rq, bio);
1447 if (ret)
1448 break;
1449 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
1450 ret = bio_attempt_front_merge(q, rq, bio);
1451 if (ret)
1452 break;
1453 }
1454 }
1455out:
1456 return ret;
1457}
1458
Jens Axboe86db1e22008-01-29 14:53:40 +01001459void init_request_from_bio(struct request *req, struct bio *bio)
Tejun Heo52d9e672006-01-06 09:49:58 +01001460{
Jens Axboe4aff5e22006-08-10 08:44:47 +02001461 req->cmd_type = REQ_TYPE_FS;
Tejun Heo52d9e672006-01-06 09:49:58 +01001462
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001463 req->cmd_flags |= bio->bi_rw & REQ_COMMON_MASK;
1464 if (bio->bi_rw & REQ_RAHEAD)
Tejun Heoa82afdf2009-07-03 17:48:16 +09001465 req->cmd_flags |= REQ_FAILFAST_MASK;
Jens Axboeb31dc662006-06-13 08:26:10 +02001466
Tejun Heo52d9e672006-01-06 09:49:58 +01001467 req->errors = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001468 req->__sector = bio->bi_iter.bi_sector;
Tejun Heo52d9e672006-01-06 09:49:58 +01001469 req->ioprio = bio_prio(bio);
NeilBrownbc1c56f2007-08-16 13:31:30 +02001470 blk_rq_bio_prep(req->q, req, bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001471}
1472
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001473void blk_queue_bio(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
Jiri Slaby5e00d1b2010-08-12 14:31:06 +02001475 const bool sync = !!(bio->bi_rw & REQ_SYNC);
Jens Axboe73c10102011-03-08 13:19:51 +01001476 struct blk_plug *plug;
1477 int el_ret, rw_flags, where = ELEVATOR_INSERT_SORT;
1478 struct request *req;
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001479 unsigned int request_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 /*
1482 * low level driver can indicate that it wants pages above a
1483 * certain limit bounced to low memory (ie for highmem, or even
1484 * ISA dma in theory)
1485 */
1486 blk_queue_bounce(q, &bio);
1487
Darrick J. Wongffecfd12013-02-21 16:42:55 -08001488 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1489 bio_endio(bio, -EIO);
1490 return;
1491 }
1492
Tejun Heo4fed9472010-09-03 11:56:17 +02001493 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) {
Jens Axboe73c10102011-03-08 13:19:51 +01001494 spin_lock_irq(q->queue_lock);
Tejun Heoae1b1532011-01-25 12:43:54 +01001495 where = ELEVATOR_INSERT_FLUSH;
Tejun Heo28e7d182010-09-03 11:56:16 +02001496 goto get_rq;
1497 }
1498
Jens Axboe73c10102011-03-08 13:19:51 +01001499 /*
1500 * Check if we can merge with the plugged list before grabbing
1501 * any locks.
1502 */
Jens Axboe320ae512013-10-24 09:20:05 +01001503 if (blk_attempt_plug_merge(q, bio, &request_count))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001504 return;
Jens Axboe73c10102011-03-08 13:19:51 +01001505
1506 spin_lock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
1508 el_ret = elv_merge(q, &req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001509 if (el_ret == ELEVATOR_BACK_MERGE) {
Jens Axboe73c10102011-03-08 13:19:51 +01001510 if (bio_attempt_back_merge(q, req, bio)) {
Tejun Heo07c2bd32012-02-08 09:19:42 +01001511 elv_bio_merged(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001512 if (!attempt_back_merge(q, req))
1513 elv_merged_request(q, req, el_ret);
1514 goto out_unlock;
Tejun Heo80a761f2009-07-03 17:48:17 +09001515 }
Jens Axboe73c10102011-03-08 13:19:51 +01001516 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
Jens Axboe73c10102011-03-08 13:19:51 +01001517 if (bio_attempt_front_merge(q, req, bio)) {
Tejun Heo07c2bd32012-02-08 09:19:42 +01001518 elv_bio_merged(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001519 if (!attempt_front_merge(q, req))
1520 elv_merged_request(q, req, el_ret);
1521 goto out_unlock;
1522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 }
1524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525get_rq:
Nick Piggin450991b2005-06-28 20:45:13 -07001526 /*
Jens Axboe7749a8d2006-12-13 13:02:26 +01001527 * This sync check and mask will be re-done in init_request_from_bio(),
1528 * but we need to set it earlier to expose the sync flag to the
1529 * rq allocator and io schedulers.
1530 */
1531 rw_flags = bio_data_dir(bio);
1532 if (sync)
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001533 rw_flags |= REQ_SYNC;
Jens Axboe7749a8d2006-12-13 13:02:26 +01001534
1535 /*
Nick Piggin450991b2005-06-28 20:45:13 -07001536 * Grab a free request. This is might sleep but can not fail.
Nick Piggind6344532005-06-28 20:45:14 -07001537 * Returns with the queue unlocked.
Nick Piggin450991b2005-06-28 20:45:13 -07001538 */
Tejun Heoa06e05e2012-06-04 20:40:55 -07001539 req = get_request(q, rw_flags, bio, GFP_NOIO);
Tejun Heoda8303c2011-10-19 14:33:05 +02001540 if (unlikely(!req)) {
1541 bio_endio(bio, -ENODEV); /* @q is dead */
1542 goto out_unlock;
1543 }
Nick Piggind6344532005-06-28 20:45:14 -07001544
Nick Piggin450991b2005-06-28 20:45:13 -07001545 /*
1546 * After dropping the lock and possibly sleeping here, our request
1547 * may now be mergeable after it had proven unmergeable (above).
1548 * We don't worry about that case for efficiency. It won't happen
1549 * often, and the elevators are able to handle it.
1550 */
Tejun Heo52d9e672006-01-06 09:49:58 +01001551 init_request_from_bio(req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Tao Ma9562ad92011-10-24 16:11:30 +02001553 if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags))
Jens Axboe11ccf112011-07-26 15:01:15 +02001554 req->cpu = raw_smp_processor_id();
Tejun Heodd831002010-09-03 11:56:16 +02001555
Jens Axboe73c10102011-03-08 13:19:51 +01001556 plug = current->plug;
Jens Axboe721a9602011-03-09 11:56:30 +01001557 if (plug) {
Jens Axboedc6d36c2011-04-12 10:28:28 +02001558 /*
1559 * If this is the first request added after a plug, fire
Jianpeng Ma7aef2e72013-09-11 13:21:07 -06001560 * of a plug trace.
Jens Axboedc6d36c2011-04-12 10:28:28 +02001561 */
Jianpeng Ma7aef2e72013-09-11 13:21:07 -06001562 if (!request_count)
Jens Axboedc6d36c2011-04-12 10:28:28 +02001563 trace_block_plug(q);
Shaohua Li3540d5e2011-11-16 09:21:50 +01001564 else {
Shaohua Li019ceb72011-11-16 09:21:50 +01001565 if (request_count >= BLK_MAX_REQUEST_COUNT) {
Shaohua Li3540d5e2011-11-16 09:21:50 +01001566 blk_flush_plug_list(plug, false);
Shaohua Li019ceb72011-11-16 09:21:50 +01001567 trace_block_plug(q);
1568 }
Jens Axboe73c10102011-03-08 13:19:51 +01001569 }
Shaohua Lia6327162011-08-24 16:04:32 +02001570 list_add_tail(&req->queuelist, &plug->list);
Jens Axboe320ae512013-10-24 09:20:05 +01001571 blk_account_io_start(req, true);
Jens Axboe73c10102011-03-08 13:19:51 +01001572 } else {
1573 spin_lock_irq(q->queue_lock);
1574 add_acct_request(q, req, where);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02001575 __blk_run_queue(q);
Jens Axboe73c10102011-03-08 13:19:51 +01001576out_unlock:
1577 spin_unlock_irq(q->queue_lock);
1578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579}
Jens Axboec20e8de2011-09-12 12:03:37 +02001580EXPORT_SYMBOL_GPL(blk_queue_bio); /* for device mapper only */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582/*
1583 * If bio->bi_dev is a partition, remap the location
1584 */
1585static inline void blk_partition_remap(struct bio *bio)
1586{
1587 struct block_device *bdev = bio->bi_bdev;
1588
Jens Axboebf2de6f2007-09-27 13:01:25 +02001589 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 struct hd_struct *p = bdev->bd_part;
Jens Axboea3623572005-11-01 09:26:16 +01001591
Kent Overstreet4f024f32013-10-11 15:44:27 -07001592 bio->bi_iter.bi_sector += p->start_sect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 bio->bi_bdev = bdev->bd_contains;
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001594
Mike Snitzerd07335e2010-11-16 12:52:38 +01001595 trace_block_bio_remap(bdev_get_queue(bio->bi_bdev), bio,
1596 bdev->bd_dev,
Kent Overstreet4f024f32013-10-11 15:44:27 -07001597 bio->bi_iter.bi_sector - p->start_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 }
1599}
1600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601static void handle_bad_sector(struct bio *bio)
1602{
1603 char b[BDEVNAME_SIZE];
1604
1605 printk(KERN_INFO "attempt to access beyond end of device\n");
1606 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1607 bdevname(bio->bi_bdev, b),
1608 bio->bi_rw,
Kent Overstreetf73a1c72012-09-25 15:05:12 -07001609 (unsigned long long)bio_end_sector(bio),
Mike Snitzer77304d22010-11-08 14:39:12 +01001610 (long long)(i_size_read(bio->bi_bdev->bd_inode) >> 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
1612 set_bit(BIO_EOF, &bio->bi_flags);
1613}
1614
Akinobu Mitac17bb492006-12-08 02:39:46 -08001615#ifdef CONFIG_FAIL_MAKE_REQUEST
1616
1617static DECLARE_FAULT_ATTR(fail_make_request);
1618
1619static int __init setup_fail_make_request(char *str)
1620{
1621 return setup_fault_attr(&fail_make_request, str);
1622}
1623__setup("fail_make_request=", setup_fail_make_request);
1624
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001625static bool should_fail_request(struct hd_struct *part, unsigned int bytes)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001626{
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001627 return part->make_it_fail && should_fail(&fail_make_request, bytes);
Akinobu Mitac17bb492006-12-08 02:39:46 -08001628}
1629
1630static int __init fail_make_request_debugfs(void)
1631{
Akinobu Mitadd48c082011-08-03 16:21:01 -07001632 struct dentry *dir = fault_create_debugfs_attr("fail_make_request",
1633 NULL, &fail_make_request);
1634
1635 return IS_ERR(dir) ? PTR_ERR(dir) : 0;
Akinobu Mitac17bb492006-12-08 02:39:46 -08001636}
1637
1638late_initcall(fail_make_request_debugfs);
1639
1640#else /* CONFIG_FAIL_MAKE_REQUEST */
1641
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001642static inline bool should_fail_request(struct hd_struct *part,
1643 unsigned int bytes)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001644{
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001645 return false;
Akinobu Mitac17bb492006-12-08 02:39:46 -08001646}
1647
1648#endif /* CONFIG_FAIL_MAKE_REQUEST */
1649
Jens Axboec07e2b42007-07-18 13:27:58 +02001650/*
1651 * Check whether this bio extends beyond the end of the device.
1652 */
1653static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1654{
1655 sector_t maxsector;
1656
1657 if (!nr_sectors)
1658 return 0;
1659
1660 /* Test device or partition size, when known. */
Mike Snitzer77304d22010-11-08 14:39:12 +01001661 maxsector = i_size_read(bio->bi_bdev->bd_inode) >> 9;
Jens Axboec07e2b42007-07-18 13:27:58 +02001662 if (maxsector) {
Kent Overstreet4f024f32013-10-11 15:44:27 -07001663 sector_t sector = bio->bi_iter.bi_sector;
Jens Axboec07e2b42007-07-18 13:27:58 +02001664
1665 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1666 /*
1667 * This may well happen - the kernel calls bread()
1668 * without checking the size of the device, e.g., when
1669 * mounting a device.
1670 */
1671 handle_bad_sector(bio);
1672 return 1;
1673 }
1674 }
1675
1676 return 0;
1677}
1678
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001679static noinline_for_stack bool
1680generic_make_request_checks(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
Jens Axboe165125e2007-07-24 09:28:11 +02001682 struct request_queue *q;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001683 int nr_sectors = bio_sectors(bio);
Jens Axboe51fd77b2007-11-02 08:49:08 +01001684 int err = -EIO;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001685 char b[BDEVNAME_SIZE];
1686 struct hd_struct *part;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
1688 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Jens Axboec07e2b42007-07-18 13:27:58 +02001690 if (bio_check_eod(bio, nr_sectors))
1691 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001693 q = bdev_get_queue(bio->bi_bdev);
1694 if (unlikely(!q)) {
1695 printk(KERN_ERR
1696 "generic_make_request: Trying to access "
1697 "nonexistent block-device %s (%Lu)\n",
1698 bdevname(bio->bi_bdev, b),
Kent Overstreet4f024f32013-10-11 15:44:27 -07001699 (long long) bio->bi_iter.bi_sector);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001700 goto end_io;
1701 }
1702
Martin K. Petersene2a60da2012-09-18 12:19:25 -04001703 if (likely(bio_is_rw(bio) &&
1704 nr_sectors > queue_max_hw_sectors(q))) {
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001705 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
1706 bdevname(bio->bi_bdev, b),
1707 bio_sectors(bio),
1708 queue_max_hw_sectors(q));
1709 goto end_io;
1710 }
1711
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001712 part = bio->bi_bdev->bd_part;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001713 if (should_fail_request(part, bio->bi_iter.bi_size) ||
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001714 should_fail_request(&part_to_disk(part)->part0,
Kent Overstreet4f024f32013-10-11 15:44:27 -07001715 bio->bi_iter.bi_size))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001716 goto end_io;
1717
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 /*
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001719 * If this device has partitions, remap block n
1720 * of partition p to block n+start(p) of the disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001722 blk_partition_remap(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001724 if (bio_check_eod(bio, nr_sectors))
1725 goto end_io;
1726
1727 /*
1728 * Filter flush bio's early so that make_request based
1729 * drivers without flush support don't have to worry
1730 * about them.
1731 */
1732 if ((bio->bi_rw & (REQ_FLUSH | REQ_FUA)) && !q->flush_flags) {
1733 bio->bi_rw &= ~(REQ_FLUSH | REQ_FUA);
1734 if (!nr_sectors) {
1735 err = 0;
Tejun Heoa7384672008-11-28 13:32:03 +09001736 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 }
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001740 if ((bio->bi_rw & REQ_DISCARD) &&
1741 (!blk_queue_discard(q) ||
Martin K. Petersene2a60da2012-09-18 12:19:25 -04001742 ((bio->bi_rw & REQ_SECURE) && !blk_queue_secdiscard(q)))) {
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001743 err = -EOPNOTSUPP;
1744 goto end_io;
1745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Martin K. Petersen4363ac72012-09-18 12:19:27 -04001747 if (bio->bi_rw & REQ_WRITE_SAME && !bdev_write_same(bio->bi_bdev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 err = -EOPNOTSUPP;
1749 goto end_io;
1750 }
1751
Tejun Heo7f4b35d2012-06-04 20:40:56 -07001752 /*
1753 * Various block parts want %current->io_context and lazy ioc
1754 * allocation ends up trading a lot of pain for a small amount of
1755 * memory. Just allocate it upfront. This may fail and block
1756 * layer knows how to live with it.
1757 */
1758 create_io_context(GFP_ATOMIC, q->node);
1759
Tejun Heobc16a4f2011-10-19 14:33:01 +02001760 if (blk_throtl_bio(q, bio))
1761 return false; /* throttled, will be resubmitted later */
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001762
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001763 trace_block_bio_queue(q, bio);
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001764 return true;
Tejun Heoa7384672008-11-28 13:32:03 +09001765
1766end_io:
1767 bio_endio(bio, err);
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001768 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769}
1770
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001771/**
1772 * generic_make_request - hand a buffer to its device driver for I/O
1773 * @bio: The bio describing the location in memory and on the device.
1774 *
1775 * generic_make_request() is used to make I/O requests of block
1776 * devices. It is passed a &struct bio, which describes the I/O that needs
1777 * to be done.
1778 *
1779 * generic_make_request() does not return any status. The
1780 * success/failure status of the request, along with notification of
1781 * completion, is delivered asynchronously through the bio->bi_end_io
1782 * function described (one day) else where.
1783 *
1784 * The caller of generic_make_request must make sure that bi_io_vec
1785 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1786 * set to describe the device address, and the
1787 * bi_end_io and optionally bi_private are set to describe how
1788 * completion notification should be signaled.
1789 *
1790 * generic_make_request and the drivers it calls may use bi_next if this
1791 * bio happens to be merged with someone else, and may resubmit the bio to
1792 * a lower device by calling into generic_make_request recursively, which
1793 * means the bio should NOT be touched after the call to ->make_request_fn.
Neil Brownd89d8792007-05-01 09:53:42 +02001794 */
1795void generic_make_request(struct bio *bio)
1796{
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001797 struct bio_list bio_list_on_stack;
1798
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001799 if (!generic_make_request_checks(bio))
1800 return;
1801
1802 /*
1803 * We only want one ->make_request_fn to be active at a time, else
1804 * stack usage with stacked devices could be a problem. So use
1805 * current->bio_list to keep a list of requests submited by a
1806 * make_request_fn function. current->bio_list is also used as a
1807 * flag to say if generic_make_request is currently active in this
1808 * task or not. If it is NULL, then no make_request is active. If
1809 * it is non-NULL, then a make_request is active, and new requests
1810 * should be added at the tail
1811 */
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001812 if (current->bio_list) {
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001813 bio_list_add(current->bio_list, bio);
Neil Brownd89d8792007-05-01 09:53:42 +02001814 return;
1815 }
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001816
Neil Brownd89d8792007-05-01 09:53:42 +02001817 /* following loop may be a bit non-obvious, and so deserves some
1818 * explanation.
1819 * Before entering the loop, bio->bi_next is NULL (as all callers
1820 * ensure that) so we have a list with a single bio.
1821 * We pretend that we have just taken it off a longer list, so
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001822 * we assign bio_list to a pointer to the bio_list_on_stack,
1823 * thus initialising the bio_list of new bios to be
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001824 * added. ->make_request() may indeed add some more bios
Neil Brownd89d8792007-05-01 09:53:42 +02001825 * through a recursive call to generic_make_request. If it
1826 * did, we find a non-NULL value in bio_list and re-enter the loop
1827 * from the top. In this case we really did just take the bio
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001828 * of the top of the list (no pretending) and so remove it from
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001829 * bio_list, and call into ->make_request() again.
Neil Brownd89d8792007-05-01 09:53:42 +02001830 */
1831 BUG_ON(bio->bi_next);
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001832 bio_list_init(&bio_list_on_stack);
1833 current->bio_list = &bio_list_on_stack;
Neil Brownd89d8792007-05-01 09:53:42 +02001834 do {
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001835 struct request_queue *q = bdev_get_queue(bio->bi_bdev);
1836
1837 q->make_request_fn(q, bio);
1838
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001839 bio = bio_list_pop(current->bio_list);
Neil Brownd89d8792007-05-01 09:53:42 +02001840 } while (bio);
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001841 current->bio_list = NULL; /* deactivate */
Neil Brownd89d8792007-05-01 09:53:42 +02001842}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843EXPORT_SYMBOL(generic_make_request);
1844
1845/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001846 * submit_bio - submit a bio to the block device layer for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1848 * @bio: The &struct bio which describes the I/O
1849 *
1850 * submit_bio() is very similar in purpose to generic_make_request(), and
1851 * uses that function to do most of the work. Both are fairly rough
Randy Dunlap710027a2008-08-19 20:13:11 +02001852 * interfaces; @bio must be presetup and ready for I/O.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 *
1854 */
1855void submit_bio(int rw, struct bio *bio)
1856{
Jens Axboe22e2c502005-06-27 10:55:12 +02001857 bio->bi_rw |= rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Jens Axboebf2de6f2007-09-27 13:01:25 +02001859 /*
1860 * If it's a regular read/write or a barrier with data attached,
1861 * go through the normal accounting stuff before submission.
1862 */
Martin K. Petersene2a60da2012-09-18 12:19:25 -04001863 if (bio_has_data(bio)) {
Martin K. Petersen4363ac72012-09-18 12:19:27 -04001864 unsigned int count;
1865
1866 if (unlikely(rw & REQ_WRITE_SAME))
1867 count = bdev_logical_block_size(bio->bi_bdev) >> 9;
1868 else
1869 count = bio_sectors(bio);
1870
Jens Axboebf2de6f2007-09-27 13:01:25 +02001871 if (rw & WRITE) {
1872 count_vm_events(PGPGOUT, count);
1873 } else {
Kent Overstreet4f024f32013-10-11 15:44:27 -07001874 task_io_account_read(bio->bi_iter.bi_size);
Jens Axboebf2de6f2007-09-27 13:01:25 +02001875 count_vm_events(PGPGIN, count);
1876 }
1877
1878 if (unlikely(block_dump)) {
1879 char b[BDEVNAME_SIZE];
San Mehat8dcbdc72010-09-14 08:48:01 +02001880 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s (%u sectors)\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001881 current->comm, task_pid_nr(current),
Jens Axboebf2de6f2007-09-27 13:01:25 +02001882 (rw & WRITE) ? "WRITE" : "READ",
Kent Overstreet4f024f32013-10-11 15:44:27 -07001883 (unsigned long long)bio->bi_iter.bi_sector,
San Mehat8dcbdc72010-09-14 08:48:01 +02001884 bdevname(bio->bi_bdev, b),
1885 count);
Jens Axboebf2de6f2007-09-27 13:01:25 +02001886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 }
1888
1889 generic_make_request(bio);
1890}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891EXPORT_SYMBOL(submit_bio);
1892
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001893/**
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001894 * blk_rq_check_limits - Helper function to check a request for the queue limit
1895 * @q: the queue
1896 * @rq: the request being checked
1897 *
1898 * Description:
1899 * @rq may have been made based on weaker limitations of upper-level queues
1900 * in request stacking drivers, and it may violate the limitation of @q.
1901 * Since the block layer and the underlying device driver trust @rq
1902 * after it is inserted to @q, it should be checked against @q before
1903 * the insertion using this generic function.
1904 *
1905 * This function should also be useful for request stacking drivers
Stefan Weileef35c22010-08-06 21:11:15 +02001906 * in some cases below, so export this function.
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001907 * Request stacking drivers like request-based dm may change the queue
1908 * limits while requests are in the queue (e.g. dm's table swapping).
1909 * Such request stacking drivers should check those requests agaist
1910 * the new queue limits again when they dispatch those requests,
1911 * although such checkings are also done against the old queue limits
1912 * when submitting requests.
1913 */
1914int blk_rq_check_limits(struct request_queue *q, struct request *rq)
1915{
Martin K. Petersene2a60da2012-09-18 12:19:25 -04001916 if (!rq_mergeable(rq))
ike Snitzer33839772010-08-08 12:11:33 -04001917 return 0;
1918
Martin K. Petersenf31dc1c2012-09-18 12:19:26 -04001919 if (blk_rq_sectors(rq) > blk_queue_get_max_sectors(q, rq->cmd_flags)) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001920 printk(KERN_ERR "%s: over max size limit.\n", __func__);
1921 return -EIO;
1922 }
1923
1924 /*
1925 * queue's settings related to segment counting like q->bounce_pfn
1926 * may differ from that of other stacking queues.
1927 * Recalculate it to check the request correctly on this queue's
1928 * limitation.
1929 */
1930 blk_recalc_rq_segments(rq);
Martin K. Petersen8a783622010-02-26 00:20:39 -05001931 if (rq->nr_phys_segments > queue_max_segments(q)) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001932 printk(KERN_ERR "%s: over max segments limit.\n", __func__);
1933 return -EIO;
1934 }
1935
1936 return 0;
1937}
1938EXPORT_SYMBOL_GPL(blk_rq_check_limits);
1939
1940/**
1941 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
1942 * @q: the queue to submit the request
1943 * @rq: the request being queued
1944 */
1945int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
1946{
1947 unsigned long flags;
Jeff Moyer4853aba2011-08-15 21:37:25 +02001948 int where = ELEVATOR_INSERT_BACK;
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001949
1950 if (blk_rq_check_limits(q, rq))
1951 return -EIO;
1952
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001953 if (rq->rq_disk &&
1954 should_fail_request(&rq->rq_disk->part0, blk_rq_bytes(rq)))
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001955 return -EIO;
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001956
1957 spin_lock_irqsave(q->queue_lock, flags);
Bart Van Assche3f3299d2012-11-28 13:42:38 +01001958 if (unlikely(blk_queue_dying(q))) {
Tejun Heo8ba61432011-12-14 00:33:37 +01001959 spin_unlock_irqrestore(q->queue_lock, flags);
1960 return -ENODEV;
1961 }
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001962
1963 /*
1964 * Submitting request must be dequeued before calling this function
1965 * because it will be linked to another request_queue
1966 */
1967 BUG_ON(blk_queued_rq(rq));
1968
Jeff Moyer4853aba2011-08-15 21:37:25 +02001969 if (rq->cmd_flags & (REQ_FLUSH|REQ_FUA))
1970 where = ELEVATOR_INSERT_FLUSH;
1971
1972 add_acct_request(q, rq, where);
Jeff Moyere67b77c2011-10-17 12:57:23 +02001973 if (where == ELEVATOR_INSERT_FLUSH)
1974 __blk_run_queue(q);
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001975 spin_unlock_irqrestore(q->queue_lock, flags);
1976
1977 return 0;
1978}
1979EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
1980
Tejun Heo80a761f2009-07-03 17:48:17 +09001981/**
1982 * blk_rq_err_bytes - determine number of bytes till the next failure boundary
1983 * @rq: request to examine
1984 *
1985 * Description:
1986 * A request could be merge of IOs which require different failure
1987 * handling. This function determines the number of bytes which
1988 * can be failed from the beginning of the request without
1989 * crossing into area which need to be retried further.
1990 *
1991 * Return:
1992 * The number of bytes to fail.
1993 *
1994 * Context:
1995 * queue_lock must be held.
1996 */
1997unsigned int blk_rq_err_bytes(const struct request *rq)
1998{
1999 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
2000 unsigned int bytes = 0;
2001 struct bio *bio;
2002
2003 if (!(rq->cmd_flags & REQ_MIXED_MERGE))
2004 return blk_rq_bytes(rq);
2005
2006 /*
2007 * Currently the only 'mixing' which can happen is between
2008 * different fastfail types. We can safely fail portions
2009 * which have all the failfast bits that the first one has -
2010 * the ones which are at least as eager to fail as the first
2011 * one.
2012 */
2013 for (bio = rq->bio; bio; bio = bio->bi_next) {
2014 if ((bio->bi_rw & ff) != ff)
2015 break;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002016 bytes += bio->bi_iter.bi_size;
Tejun Heo80a761f2009-07-03 17:48:17 +09002017 }
2018
2019 /* this could lead to infinite loop */
2020 BUG_ON(blk_rq_bytes(rq) && !bytes);
2021 return bytes;
2022}
2023EXPORT_SYMBOL_GPL(blk_rq_err_bytes);
2024
Jens Axboe320ae512013-10-24 09:20:05 +01002025void blk_account_io_completion(struct request *req, unsigned int bytes)
Jens Axboebc58ba92009-01-23 10:54:44 +01002026{
Jens Axboec2553b52009-04-24 08:10:11 +02002027 if (blk_do_io_stat(req)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01002028 const int rw = rq_data_dir(req);
2029 struct hd_struct *part;
2030 int cpu;
2031
2032 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +01002033 part = req->part;
Jens Axboebc58ba92009-01-23 10:54:44 +01002034 part_stat_add(cpu, part, sectors[rw], bytes >> 9);
2035 part_stat_unlock();
2036 }
2037}
2038
Jens Axboe320ae512013-10-24 09:20:05 +01002039void blk_account_io_done(struct request *req)
Jens Axboebc58ba92009-01-23 10:54:44 +01002040{
Jens Axboebc58ba92009-01-23 10:54:44 +01002041 /*
Tejun Heodd4c1332010-09-03 11:56:16 +02002042 * Account IO completion. flush_rq isn't accounted as a
2043 * normal IO on queueing nor completion. Accounting the
2044 * containing request is enough.
Jens Axboebc58ba92009-01-23 10:54:44 +01002045 */
Tejun Heo414b4ff2011-01-25 12:43:49 +01002046 if (blk_do_io_stat(req) && !(req->cmd_flags & REQ_FLUSH_SEQ)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01002047 unsigned long duration = jiffies - req->start_time;
2048 const int rw = rq_data_dir(req);
2049 struct hd_struct *part;
2050 int cpu;
2051
2052 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +01002053 part = req->part;
Jens Axboebc58ba92009-01-23 10:54:44 +01002054
2055 part_stat_inc(cpu, part, ios[rw]);
2056 part_stat_add(cpu, part, ticks[rw], duration);
2057 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02002058 part_dec_in_flight(part, rw);
Jens Axboebc58ba92009-01-23 10:54:44 +01002059
Jens Axboe6c23a962011-01-07 08:43:37 +01002060 hd_struct_put(part);
Jens Axboebc58ba92009-01-23 10:54:44 +01002061 part_stat_unlock();
2062 }
2063}
2064
Lin Mingc8158812013-03-23 11:42:27 +08002065#ifdef CONFIG_PM_RUNTIME
2066/*
2067 * Don't process normal requests when queue is suspended
2068 * or in the process of suspending/resuming
2069 */
2070static struct request *blk_pm_peek_request(struct request_queue *q,
2071 struct request *rq)
2072{
2073 if (q->dev && (q->rpm_status == RPM_SUSPENDED ||
2074 (q->rpm_status != RPM_ACTIVE && !(rq->cmd_flags & REQ_PM))))
2075 return NULL;
2076 else
2077 return rq;
2078}
2079#else
2080static inline struct request *blk_pm_peek_request(struct request_queue *q,
2081 struct request *rq)
2082{
2083 return rq;
2084}
2085#endif
2086
Jens Axboe320ae512013-10-24 09:20:05 +01002087void blk_account_io_start(struct request *rq, bool new_io)
2088{
2089 struct hd_struct *part;
2090 int rw = rq_data_dir(rq);
2091 int cpu;
2092
2093 if (!blk_do_io_stat(rq))
2094 return;
2095
2096 cpu = part_stat_lock();
2097
2098 if (!new_io) {
2099 part = rq->part;
2100 part_stat_inc(cpu, part, merges[rw]);
2101 } else {
2102 part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq));
2103 if (!hd_struct_try_get(part)) {
2104 /*
2105 * The partition is already being removed,
2106 * the request will be accounted on the disk only
2107 *
2108 * We take a reference on disk->part0 although that
2109 * partition will never be deleted, so we can treat
2110 * it as any other partition.
2111 */
2112 part = &rq->rq_disk->part0;
2113 hd_struct_get(part);
2114 }
2115 part_round_stats(cpu, part);
2116 part_inc_in_flight(part, rw);
2117 rq->part = part;
2118 }
2119
2120 part_stat_unlock();
2121}
2122
Tejun Heo53a08802008-12-03 12:41:26 +01002123/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09002124 * blk_peek_request - peek at the top of a request queue
2125 * @q: request queue to peek at
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002126 *
2127 * Description:
Tejun Heo9934c8c2009-05-08 11:54:16 +09002128 * Return the request at the top of @q. The returned request
2129 * should be started using blk_start_request() before LLD starts
2130 * processing it.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002131 *
2132 * Return:
Tejun Heo9934c8c2009-05-08 11:54:16 +09002133 * Pointer to the request at the top of @q if available. Null
2134 * otherwise.
2135 *
2136 * Context:
2137 * queue_lock must be held.
2138 */
2139struct request *blk_peek_request(struct request_queue *q)
Tejun Heo158dbda2009-04-23 11:05:18 +09002140{
2141 struct request *rq;
2142 int ret;
2143
2144 while ((rq = __elv_next_request(q)) != NULL) {
Lin Mingc8158812013-03-23 11:42:27 +08002145
2146 rq = blk_pm_peek_request(q, rq);
2147 if (!rq)
2148 break;
2149
Tejun Heo158dbda2009-04-23 11:05:18 +09002150 if (!(rq->cmd_flags & REQ_STARTED)) {
2151 /*
2152 * This is the first time the device driver
2153 * sees this request (possibly after
2154 * requeueing). Notify IO scheduler.
2155 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002156 if (rq->cmd_flags & REQ_SORTED)
Tejun Heo158dbda2009-04-23 11:05:18 +09002157 elv_activate_rq(q, rq);
2158
2159 /*
2160 * just mark as started even if we don't start
2161 * it, a request that has been delayed should
2162 * not be passed by new incoming requests
2163 */
2164 rq->cmd_flags |= REQ_STARTED;
2165 trace_block_rq_issue(q, rq);
2166 }
2167
2168 if (!q->boundary_rq || q->boundary_rq == rq) {
2169 q->end_sector = rq_end_sector(rq);
2170 q->boundary_rq = NULL;
2171 }
2172
2173 if (rq->cmd_flags & REQ_DONTPREP)
2174 break;
2175
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002176 if (q->dma_drain_size && blk_rq_bytes(rq)) {
Tejun Heo158dbda2009-04-23 11:05:18 +09002177 /*
2178 * make sure space for the drain appears we
2179 * know we can do this because max_hw_segments
2180 * has been adjusted to be one fewer than the
2181 * device can handle
2182 */
2183 rq->nr_phys_segments++;
2184 }
2185
2186 if (!q->prep_rq_fn)
2187 break;
2188
2189 ret = q->prep_rq_fn(q, rq);
2190 if (ret == BLKPREP_OK) {
2191 break;
2192 } else if (ret == BLKPREP_DEFER) {
2193 /*
2194 * the request may have been (partially) prepped.
2195 * we need to keep this request in the front to
2196 * avoid resource deadlock. REQ_STARTED will
2197 * prevent other fs requests from passing this one.
2198 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002199 if (q->dma_drain_size && blk_rq_bytes(rq) &&
Tejun Heo158dbda2009-04-23 11:05:18 +09002200 !(rq->cmd_flags & REQ_DONTPREP)) {
2201 /*
2202 * remove the space for the drain we added
2203 * so that we don't add it again
2204 */
2205 --rq->nr_phys_segments;
2206 }
2207
2208 rq = NULL;
2209 break;
2210 } else if (ret == BLKPREP_KILL) {
2211 rq->cmd_flags |= REQ_QUIET;
James Bottomleyc143dc92009-05-30 06:43:49 +02002212 /*
2213 * Mark this request as started so we don't trigger
2214 * any debug logic in the end I/O path.
2215 */
2216 blk_start_request(rq);
Tejun Heo40cbbb72009-04-23 11:05:19 +09002217 __blk_end_request_all(rq, -EIO);
Tejun Heo158dbda2009-04-23 11:05:18 +09002218 } else {
2219 printk(KERN_ERR "%s: bad return=%d\n", __func__, ret);
2220 break;
2221 }
2222 }
2223
2224 return rq;
2225}
Tejun Heo9934c8c2009-05-08 11:54:16 +09002226EXPORT_SYMBOL(blk_peek_request);
Tejun Heo158dbda2009-04-23 11:05:18 +09002227
Tejun Heo9934c8c2009-05-08 11:54:16 +09002228void blk_dequeue_request(struct request *rq)
Tejun Heo158dbda2009-04-23 11:05:18 +09002229{
Tejun Heo9934c8c2009-05-08 11:54:16 +09002230 struct request_queue *q = rq->q;
2231
Tejun Heo158dbda2009-04-23 11:05:18 +09002232 BUG_ON(list_empty(&rq->queuelist));
2233 BUG_ON(ELV_ON_HASH(rq));
2234
2235 list_del_init(&rq->queuelist);
2236
2237 /*
2238 * the time frame between a request being removed from the lists
2239 * and to it is freed is accounted as io that is in progress at
2240 * the driver side.
2241 */
Divyesh Shah91952912010-04-01 15:01:41 -07002242 if (blk_account_rq(rq)) {
Jens Axboe0a7ae2f2009-05-20 08:54:31 +02002243 q->in_flight[rq_is_sync(rq)]++;
Divyesh Shah91952912010-04-01 15:01:41 -07002244 set_io_start_time_ns(rq);
2245 }
Tejun Heo158dbda2009-04-23 11:05:18 +09002246}
2247
Tejun Heo5efccd12009-04-23 11:05:18 +09002248/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09002249 * blk_start_request - start request processing on the driver
2250 * @req: request to dequeue
2251 *
2252 * Description:
2253 * Dequeue @req and start timeout timer on it. This hands off the
2254 * request to the driver.
2255 *
2256 * Block internal functions which don't want to start timer should
2257 * call blk_dequeue_request().
2258 *
2259 * Context:
2260 * queue_lock must be held.
2261 */
2262void blk_start_request(struct request *req)
2263{
2264 blk_dequeue_request(req);
2265
2266 /*
Tejun Heo5f49f632009-05-19 18:33:05 +09002267 * We are now handing the request to the hardware, initialize
2268 * resid_len to full count and add the timeout handler.
Tejun Heo9934c8c2009-05-08 11:54:16 +09002269 */
Tejun Heo5f49f632009-05-19 18:33:05 +09002270 req->resid_len = blk_rq_bytes(req);
FUJITA Tomonoridbb66c42009-06-09 05:47:10 +02002271 if (unlikely(blk_bidi_rq(req)))
2272 req->next_rq->resid_len = blk_rq_bytes(req->next_rq);
2273
Jeff Moyer4912aa62013-10-08 14:36:41 -04002274 BUG_ON(test_bit(REQ_ATOM_COMPLETE, &req->atomic_flags));
Tejun Heo9934c8c2009-05-08 11:54:16 +09002275 blk_add_timer(req);
2276}
2277EXPORT_SYMBOL(blk_start_request);
2278
2279/**
2280 * blk_fetch_request - fetch a request from a request queue
2281 * @q: request queue to fetch a request from
2282 *
2283 * Description:
2284 * Return the request at the top of @q. The request is started on
2285 * return and LLD can start processing it immediately.
2286 *
2287 * Return:
2288 * Pointer to the request at the top of @q if available. Null
2289 * otherwise.
2290 *
2291 * Context:
2292 * queue_lock must be held.
2293 */
2294struct request *blk_fetch_request(struct request_queue *q)
2295{
2296 struct request *rq;
2297
2298 rq = blk_peek_request(q);
2299 if (rq)
2300 blk_start_request(rq);
2301 return rq;
2302}
2303EXPORT_SYMBOL(blk_fetch_request);
2304
2305/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002306 * blk_update_request - Special helper function for request stacking drivers
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002307 * @req: the request being processed
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002308 * @error: %0 for success, < %0 for error
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002309 * @nr_bytes: number of bytes to complete @req
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002310 *
2311 * Description:
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002312 * Ends I/O on a number of bytes attached to @req, but doesn't complete
2313 * the request structure even if @req doesn't have leftover.
2314 * If @req has leftover, sets it up for the next range of segments.
Tejun Heo2e60e022009-04-23 11:05:18 +09002315 *
2316 * This special helper function is only for request stacking drivers
2317 * (e.g. request-based dm) so that they can handle partial completion.
2318 * Actual device drivers should use blk_end_request instead.
2319 *
2320 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
2321 * %false return from this function.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002322 *
2323 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002324 * %false - this request doesn't have any more data
2325 * %true - this request has more data
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002326 **/
Tejun Heo2e60e022009-04-23 11:05:18 +09002327bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328{
Kent Overstreetf79ea412012-09-20 16:38:30 -07002329 int total_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
Tejun Heo2e60e022009-04-23 11:05:18 +09002331 if (!req->bio)
2332 return false;
2333
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01002334 trace_block_rq_complete(req->q, req);
Jens Axboe2056a782006-03-23 20:00:26 +01002335
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 /*
Tejun Heo6f414692009-04-19 07:00:41 +09002337 * For fs requests, rq is just carrier of independent bio's
2338 * and each partial completion should be handled separately.
2339 * Reset per-request error on each partial completion.
2340 *
2341 * TODO: tj: This is too subtle. It would be better to let
2342 * low level drivers do what they see fit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002344 if (req->cmd_type == REQ_TYPE_FS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 req->errors = 0;
2346
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002347 if (error && req->cmd_type == REQ_TYPE_FS &&
2348 !(req->cmd_flags & REQ_QUIET)) {
Hannes Reinecke79775562011-01-18 10:13:13 +01002349 char *error_type;
2350
2351 switch (error) {
2352 case -ENOLINK:
2353 error_type = "recoverable transport";
2354 break;
2355 case -EREMOTEIO:
2356 error_type = "critical target";
2357 break;
2358 case -EBADE:
2359 error_type = "critical nexus";
2360 break;
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002361 case -ETIMEDOUT:
2362 error_type = "timeout";
2363 break;
Hannes Reineckea9d6ceb82013-07-01 15:16:25 +02002364 case -ENOSPC:
2365 error_type = "critical space allocation";
2366 break;
Hannes Reinecke7e782af2013-07-01 15:16:26 +02002367 case -ENODATA:
2368 error_type = "critical medium";
2369 break;
Hannes Reinecke79775562011-01-18 10:13:13 +01002370 case -EIO:
2371 default:
2372 error_type = "I/O";
2373 break;
2374 }
Yi Zou37d7b342012-08-30 16:26:25 -07002375 printk_ratelimited(KERN_ERR "end_request: %s error, dev %s, sector %llu\n",
2376 error_type, req->rq_disk ?
2377 req->rq_disk->disk_name : "?",
2378 (unsigned long long)blk_rq_pos(req));
2379
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 }
2381
Jens Axboebc58ba92009-01-23 10:54:44 +01002382 blk_account_io_completion(req, nr_bytes);
Jens Axboed72d9042005-11-01 08:35:42 +01002383
Kent Overstreetf79ea412012-09-20 16:38:30 -07002384 total_bytes = 0;
2385 while (req->bio) {
2386 struct bio *bio = req->bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002387 unsigned bio_bytes = min(bio->bi_iter.bi_size, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
Kent Overstreet4f024f32013-10-11 15:44:27 -07002389 if (bio_bytes == bio->bi_iter.bi_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 req->bio = bio->bi_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
Kent Overstreetf79ea412012-09-20 16:38:30 -07002392 req_bio_endio(req, bio, bio_bytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
Kent Overstreetf79ea412012-09-20 16:38:30 -07002394 total_bytes += bio_bytes;
2395 nr_bytes -= bio_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
Kent Overstreetf79ea412012-09-20 16:38:30 -07002397 if (!nr_bytes)
2398 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 }
2400
2401 /*
2402 * completely done
2403 */
Tejun Heo2e60e022009-04-23 11:05:18 +09002404 if (!req->bio) {
2405 /*
2406 * Reset counters so that the request stacking driver
2407 * can find how many bytes remain in the request
2408 * later.
2409 */
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002410 req->__data_len = 0;
Tejun Heo2e60e022009-04-23 11:05:18 +09002411 return false;
2412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002414 req->__data_len -= total_bytes;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002415 req->buffer = bio_data(req->bio);
2416
2417 /* update sector only for requests with clear definition of sector */
Martin K. Petersene2a60da2012-09-18 12:19:25 -04002418 if (req->cmd_type == REQ_TYPE_FS)
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002419 req->__sector += total_bytes >> 9;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002420
Tejun Heo80a761f2009-07-03 17:48:17 +09002421 /* mixed attributes always follow the first bio */
2422 if (req->cmd_flags & REQ_MIXED_MERGE) {
2423 req->cmd_flags &= ~REQ_FAILFAST_MASK;
2424 req->cmd_flags |= req->bio->bi_rw & REQ_FAILFAST_MASK;
2425 }
2426
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002427 /*
2428 * If total number of sectors is less than the first segment
2429 * size, something has gone terribly wrong.
2430 */
2431 if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
Jens Axboe81829242011-03-30 09:51:33 +02002432 blk_dump_rq_flags(req, "request botched");
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002433 req->__data_len = blk_rq_cur_bytes(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002434 }
2435
2436 /* recalculate the number of segments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 blk_recalc_rq_segments(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002438
Tejun Heo2e60e022009-04-23 11:05:18 +09002439 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440}
Tejun Heo2e60e022009-04-23 11:05:18 +09002441EXPORT_SYMBOL_GPL(blk_update_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
Tejun Heo2e60e022009-04-23 11:05:18 +09002443static bool blk_update_bidi_request(struct request *rq, int error,
2444 unsigned int nr_bytes,
2445 unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002446{
Tejun Heo2e60e022009-04-23 11:05:18 +09002447 if (blk_update_request(rq, error, nr_bytes))
2448 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002449
Tejun Heo2e60e022009-04-23 11:05:18 +09002450 /* Bidi request must be completed as a whole */
2451 if (unlikely(blk_bidi_rq(rq)) &&
2452 blk_update_request(rq->next_rq, error, bidi_bytes))
2453 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002454
Jens Axboee2e1a142010-06-09 10:42:09 +02002455 if (blk_queue_add_random(rq->q))
2456 add_disk_randomness(rq->rq_disk);
Tejun Heo2e60e022009-04-23 11:05:18 +09002457
2458 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459}
2460
James Bottomley28018c22010-07-01 19:49:17 +09002461/**
2462 * blk_unprep_request - unprepare a request
2463 * @req: the request
2464 *
2465 * This function makes a request ready for complete resubmission (or
2466 * completion). It happens only after all error handling is complete,
2467 * so represents the appropriate moment to deallocate any resources
2468 * that were allocated to the request in the prep_rq_fn. The queue
2469 * lock is held when calling this.
2470 */
2471void blk_unprep_request(struct request *req)
2472{
2473 struct request_queue *q = req->q;
2474
2475 req->cmd_flags &= ~REQ_DONTPREP;
2476 if (q->unprep_rq_fn)
2477 q->unprep_rq_fn(q, req);
2478}
2479EXPORT_SYMBOL_GPL(blk_unprep_request);
2480
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481/*
2482 * queue lock must be held
2483 */
Tejun Heo2e60e022009-04-23 11:05:18 +09002484static void blk_finish_request(struct request *req, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485{
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002486 if (blk_rq_tagged(req))
2487 blk_queue_end_tag(req->q, req);
2488
James Bottomleyba396a62009-05-27 14:17:08 +02002489 BUG_ON(blk_queued_rq(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002491 if (unlikely(laptop_mode) && req->cmd_type == REQ_TYPE_FS)
Matthew Garrett31373d02010-04-06 14:25:14 +02002492 laptop_io_completion(&req->q->backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
Mike Andersone78042e2008-10-30 02:16:20 -07002494 blk_delete_timer(req);
2495
James Bottomley28018c22010-07-01 19:49:17 +09002496 if (req->cmd_flags & REQ_DONTPREP)
2497 blk_unprep_request(req);
2498
Jens Axboebc58ba92009-01-23 10:54:44 +01002499 blk_account_io_done(req);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002500
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 if (req->end_io)
Tejun Heo8ffdc652006-01-06 09:49:03 +01002502 req->end_io(req, error);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002503 else {
2504 if (blk_bidi_rq(req))
2505 __blk_put_request(req->next_rq->q, req->next_rq);
2506
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 __blk_put_request(req->q, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 }
2509}
2510
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05002511/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002512 * blk_end_bidi_request - Complete a bidi request
2513 * @rq: the request to complete
Randy Dunlap710027a2008-08-19 20:13:11 +02002514 * @error: %0 for success, < %0 for error
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002515 * @nr_bytes: number of bytes to complete @rq
2516 * @bidi_bytes: number of bytes to complete @rq->next_rq
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002517 *
2518 * Description:
2519 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
Tejun Heo2e60e022009-04-23 11:05:18 +09002520 * Drivers that supports bidi can safely call this member for any
2521 * type of request, bidi or uni. In the later case @bidi_bytes is
2522 * just ignored.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002523 *
2524 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002525 * %false - we are done with this request
2526 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002527 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002528static bool blk_end_bidi_request(struct request *rq, int error,
2529 unsigned int nr_bytes, unsigned int bidi_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002530{
2531 struct request_queue *q = rq->q;
Tejun Heo2e60e022009-04-23 11:05:18 +09002532 unsigned long flags;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002533
Tejun Heo2e60e022009-04-23 11:05:18 +09002534 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2535 return true;
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002536
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002537 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo2e60e022009-04-23 11:05:18 +09002538 blk_finish_request(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002539 spin_unlock_irqrestore(q->queue_lock, flags);
2540
Tejun Heo2e60e022009-04-23 11:05:18 +09002541 return false;
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002542}
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002543
2544/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002545 * __blk_end_bidi_request - Complete a bidi request with queue lock held
2546 * @rq: the request to complete
2547 * @error: %0 for success, < %0 for error
2548 * @nr_bytes: number of bytes to complete @rq
2549 * @bidi_bytes: number of bytes to complete @rq->next_rq
Tejun Heo5efccd12009-04-23 11:05:18 +09002550 *
2551 * Description:
Tejun Heo2e60e022009-04-23 11:05:18 +09002552 * Identical to blk_end_bidi_request() except that queue lock is
2553 * assumed to be locked on entry and remains so on return.
Tejun Heo5efccd12009-04-23 11:05:18 +09002554 *
Tejun Heo2e60e022009-04-23 11:05:18 +09002555 * Return:
2556 * %false - we are done with this request
2557 * %true - still buffers pending for this request
Tejun Heo5efccd12009-04-23 11:05:18 +09002558 **/
Jeff Moyer4853aba2011-08-15 21:37:25 +02002559bool __blk_end_bidi_request(struct request *rq, int error,
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002560 unsigned int nr_bytes, unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002561{
Tejun Heo2e60e022009-04-23 11:05:18 +09002562 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2563 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002564
Tejun Heo2e60e022009-04-23 11:05:18 +09002565 blk_finish_request(rq, error);
Tejun Heo5efccd12009-04-23 11:05:18 +09002566
Tejun Heo2e60e022009-04-23 11:05:18 +09002567 return false;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002568}
2569
2570/**
2571 * blk_end_request - Helper function for drivers to complete the request.
2572 * @rq: the request being processed
2573 * @error: %0 for success, < %0 for error
2574 * @nr_bytes: number of bytes to complete
2575 *
2576 * Description:
2577 * Ends I/O on a number of bytes attached to @rq.
2578 * If @rq has leftover, sets it up for the next range of segments.
2579 *
2580 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002581 * %false - we are done with this request
2582 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002583 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002584bool blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002585{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002586 return blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002587}
Jens Axboe56ad1742009-07-28 22:11:24 +02002588EXPORT_SYMBOL(blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002589
2590/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002591 * blk_end_request_all - Helper function for drives to finish the request.
2592 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002593 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002594 *
2595 * Description:
2596 * Completely finish @rq.
2597 */
2598void blk_end_request_all(struct request *rq, int error)
2599{
2600 bool pending;
2601 unsigned int bidi_bytes = 0;
2602
2603 if (unlikely(blk_bidi_rq(rq)))
2604 bidi_bytes = blk_rq_bytes(rq->next_rq);
2605
2606 pending = blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2607 BUG_ON(pending);
2608}
Jens Axboe56ad1742009-07-28 22:11:24 +02002609EXPORT_SYMBOL(blk_end_request_all);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002610
2611/**
2612 * blk_end_request_cur - Helper function to finish the current request chunk.
2613 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002614 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002615 *
2616 * Description:
2617 * Complete the current consecutively mapped chunk from @rq.
2618 *
2619 * Return:
2620 * %false - we are done with this request
2621 * %true - still buffers pending for this request
2622 */
2623bool blk_end_request_cur(struct request *rq, int error)
2624{
2625 return blk_end_request(rq, error, blk_rq_cur_bytes(rq));
2626}
Jens Axboe56ad1742009-07-28 22:11:24 +02002627EXPORT_SYMBOL(blk_end_request_cur);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002628
2629/**
Tejun Heo80a761f2009-07-03 17:48:17 +09002630 * blk_end_request_err - Finish a request till the next failure boundary.
2631 * @rq: the request to finish till the next failure boundary for
2632 * @error: must be negative errno
2633 *
2634 * Description:
2635 * Complete @rq till the next failure boundary.
2636 *
2637 * Return:
2638 * %false - we are done with this request
2639 * %true - still buffers pending for this request
2640 */
2641bool blk_end_request_err(struct request *rq, int error)
2642{
2643 WARN_ON(error >= 0);
2644 return blk_end_request(rq, error, blk_rq_err_bytes(rq));
2645}
2646EXPORT_SYMBOL_GPL(blk_end_request_err);
2647
2648/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002649 * __blk_end_request - Helper function for drivers to complete the request.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002650 * @rq: the request being processed
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002651 * @error: %0 for success, < %0 for error
2652 * @nr_bytes: number of bytes to complete
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002653 *
2654 * Description:
2655 * Must be called with queue lock held unlike blk_end_request().
2656 *
2657 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002658 * %false - we are done with this request
2659 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002660 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002661bool __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002662{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002663 return __blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002664}
Jens Axboe56ad1742009-07-28 22:11:24 +02002665EXPORT_SYMBOL(__blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002666
2667/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002668 * __blk_end_request_all - Helper function for drives to finish the request.
2669 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002670 * @error: %0 for success, < %0 for error
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002671 *
2672 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002673 * Completely finish @rq. Must be called with queue lock held.
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002674 */
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002675void __blk_end_request_all(struct request *rq, int error)
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002676{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002677 bool pending;
2678 unsigned int bidi_bytes = 0;
2679
2680 if (unlikely(blk_bidi_rq(rq)))
2681 bidi_bytes = blk_rq_bytes(rq->next_rq);
2682
2683 pending = __blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2684 BUG_ON(pending);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002685}
Jens Axboe56ad1742009-07-28 22:11:24 +02002686EXPORT_SYMBOL(__blk_end_request_all);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002687
2688/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002689 * __blk_end_request_cur - Helper function to finish the current request chunk.
2690 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002691 * @error: %0 for success, < %0 for error
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002692 *
2693 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002694 * Complete the current consecutively mapped chunk from @rq. Must
2695 * be called with queue lock held.
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002696 *
2697 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002698 * %false - we are done with this request
2699 * %true - still buffers pending for this request
2700 */
2701bool __blk_end_request_cur(struct request *rq, int error)
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002702{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002703 return __blk_end_request(rq, error, blk_rq_cur_bytes(rq));
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002704}
Jens Axboe56ad1742009-07-28 22:11:24 +02002705EXPORT_SYMBOL(__blk_end_request_cur);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002706
Tejun Heo80a761f2009-07-03 17:48:17 +09002707/**
2708 * __blk_end_request_err - Finish a request till the next failure boundary.
2709 * @rq: the request to finish till the next failure boundary for
2710 * @error: must be negative errno
2711 *
2712 * Description:
2713 * Complete @rq till the next failure boundary. Must be called
2714 * with queue lock held.
2715 *
2716 * Return:
2717 * %false - we are done with this request
2718 * %true - still buffers pending for this request
2719 */
2720bool __blk_end_request_err(struct request *rq, int error)
2721{
2722 WARN_ON(error >= 0);
2723 return __blk_end_request(rq, error, blk_rq_err_bytes(rq));
2724}
2725EXPORT_SYMBOL_GPL(__blk_end_request_err);
2726
Jens Axboe86db1e22008-01-29 14:53:40 +01002727void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2728 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729{
Tejun Heoa82afdf2009-07-03 17:48:16 +09002730 /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002731 rq->cmd_flags |= bio->bi_rw & REQ_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732
David Woodhousefb2dce82008-08-05 18:01:53 +01002733 if (bio_has_data(bio)) {
2734 rq->nr_phys_segments = bio_phys_segments(q, bio);
David Woodhousefb2dce82008-08-05 18:01:53 +01002735 rq->buffer = bio_data(bio);
2736 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07002737 rq->__data_len = bio->bi_iter.bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 rq->bio = rq->biotail = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
NeilBrown66846572007-08-16 13:31:28 +02002740 if (bio->bi_bdev)
2741 rq->rq_disk = bio->bi_bdev->bd_disk;
2742}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
Ilya Loginov2d4dc892009-11-26 09:16:19 +01002744#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
2745/**
2746 * rq_flush_dcache_pages - Helper function to flush all pages in a request
2747 * @rq: the request to be flushed
2748 *
2749 * Description:
2750 * Flush all pages in @rq.
2751 */
2752void rq_flush_dcache_pages(struct request *rq)
2753{
2754 struct req_iterator iter;
Kent Overstreet79886132013-11-23 17:19:00 -08002755 struct bio_vec bvec;
Ilya Loginov2d4dc892009-11-26 09:16:19 +01002756
2757 rq_for_each_segment(bvec, rq, iter)
Kent Overstreet79886132013-11-23 17:19:00 -08002758 flush_dcache_page(bvec.bv_page);
Ilya Loginov2d4dc892009-11-26 09:16:19 +01002759}
2760EXPORT_SYMBOL_GPL(rq_flush_dcache_pages);
2761#endif
2762
Kiyoshi Uedaef9e3fa2008-10-01 16:12:15 +02002763/**
2764 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
2765 * @q : the queue of the device being checked
2766 *
2767 * Description:
2768 * Check if underlying low-level drivers of a device are busy.
2769 * If the drivers want to export their busy state, they must set own
2770 * exporting function using blk_queue_lld_busy() first.
2771 *
2772 * Basically, this function is used only by request stacking drivers
2773 * to stop dispatching requests to underlying devices when underlying
2774 * devices are busy. This behavior helps more I/O merging on the queue
2775 * of the request stacking driver and prevents I/O throughput regression
2776 * on burst I/O load.
2777 *
2778 * Return:
2779 * 0 - Not busy (The request stacking driver should dispatch request)
2780 * 1 - Busy (The request stacking driver should stop dispatching request)
2781 */
2782int blk_lld_busy(struct request_queue *q)
2783{
2784 if (q->lld_busy_fn)
2785 return q->lld_busy_fn(q);
2786
2787 return 0;
2788}
2789EXPORT_SYMBOL_GPL(blk_lld_busy);
2790
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002791/**
2792 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
2793 * @rq: the clone request to be cleaned up
2794 *
2795 * Description:
2796 * Free all bios in @rq for a cloned request.
2797 */
2798void blk_rq_unprep_clone(struct request *rq)
2799{
2800 struct bio *bio;
2801
2802 while ((bio = rq->bio) != NULL) {
2803 rq->bio = bio->bi_next;
2804
2805 bio_put(bio);
2806 }
2807}
2808EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
2809
2810/*
2811 * Copy attributes of the original request to the clone request.
2812 * The actual data parts (e.g. ->cmd, ->buffer, ->sense) are not copied.
2813 */
2814static void __blk_rq_prep_clone(struct request *dst, struct request *src)
2815{
2816 dst->cpu = src->cpu;
Tejun Heo3a2edd02010-09-03 11:56:18 +02002817 dst->cmd_flags = (src->cmd_flags & REQ_CLONE_MASK) | REQ_NOMERGE;
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002818 dst->cmd_type = src->cmd_type;
2819 dst->__sector = blk_rq_pos(src);
2820 dst->__data_len = blk_rq_bytes(src);
2821 dst->nr_phys_segments = src->nr_phys_segments;
2822 dst->ioprio = src->ioprio;
2823 dst->extra_len = src->extra_len;
2824}
2825
2826/**
2827 * blk_rq_prep_clone - Helper function to setup clone request
2828 * @rq: the request to be setup
2829 * @rq_src: original request to be cloned
2830 * @bs: bio_set that bios for clone are allocated from
2831 * @gfp_mask: memory allocation mask for bio
2832 * @bio_ctr: setup function to be called for each clone bio.
2833 * Returns %0 for success, non %0 for failure.
2834 * @data: private data to be passed to @bio_ctr
2835 *
2836 * Description:
2837 * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
2838 * The actual data parts of @rq_src (e.g. ->cmd, ->buffer, ->sense)
2839 * are not copied, and copying such parts is the caller's responsibility.
2840 * Also, pages which the original bios are pointing to are not copied
2841 * and the cloned bios just point same pages.
2842 * So cloned bios must be completed before original bios, which means
2843 * the caller must complete @rq before @rq_src.
2844 */
2845int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
2846 struct bio_set *bs, gfp_t gfp_mask,
2847 int (*bio_ctr)(struct bio *, struct bio *, void *),
2848 void *data)
2849{
2850 struct bio *bio, *bio_src;
2851
2852 if (!bs)
2853 bs = fs_bio_set;
2854
2855 blk_rq_init(NULL, rq);
2856
2857 __rq_for_each_bio(bio_src, rq_src) {
Kent Overstreetbf800ef2012-09-06 15:35:02 -07002858 bio = bio_clone_bioset(bio_src, gfp_mask, bs);
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002859 if (!bio)
2860 goto free_and_out;
2861
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002862 if (bio_ctr && bio_ctr(bio, bio_src, data))
2863 goto free_and_out;
2864
2865 if (rq->bio) {
2866 rq->biotail->bi_next = bio;
2867 rq->biotail = bio;
2868 } else
2869 rq->bio = rq->biotail = bio;
2870 }
2871
2872 __blk_rq_prep_clone(rq, rq_src);
2873
2874 return 0;
2875
2876free_and_out:
2877 if (bio)
Kent Overstreet4254bba2012-09-06 15:35:00 -07002878 bio_put(bio);
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002879 blk_rq_unprep_clone(rq);
2880
2881 return -ENOMEM;
2882}
2883EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
2884
Jens Axboe18887ad2008-07-28 13:08:45 +02002885int kblockd_schedule_work(struct request_queue *q, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886{
2887 return queue_work(kblockd_workqueue, work);
2888}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889EXPORT_SYMBOL(kblockd_schedule_work);
2890
Vivek Goyale43473b2010-09-15 17:06:35 -04002891int kblockd_schedule_delayed_work(struct request_queue *q,
2892 struct delayed_work *dwork, unsigned long delay)
2893{
2894 return queue_delayed_work(kblockd_workqueue, dwork, delay);
2895}
2896EXPORT_SYMBOL(kblockd_schedule_delayed_work);
2897
Jens Axboe73c10102011-03-08 13:19:51 +01002898#define PLUG_MAGIC 0x91827364
2899
Suresh Jayaraman75df7132011-09-21 10:00:16 +02002900/**
2901 * blk_start_plug - initialize blk_plug and track it inside the task_struct
2902 * @plug: The &struct blk_plug that needs to be initialized
2903 *
2904 * Description:
2905 * Tracking blk_plug inside the task_struct will help with auto-flushing the
2906 * pending I/O should the task end up blocking between blk_start_plug() and
2907 * blk_finish_plug(). This is important from a performance perspective, but
2908 * also ensures that we don't deadlock. For instance, if the task is blocking
2909 * for a memory allocation, memory reclaim could end up wanting to free a
2910 * page belonging to that request that is currently residing in our private
2911 * plug. By flushing the pending I/O when the process goes to sleep, we avoid
2912 * this kind of deadlock.
2913 */
Jens Axboe73c10102011-03-08 13:19:51 +01002914void blk_start_plug(struct blk_plug *plug)
2915{
2916 struct task_struct *tsk = current;
2917
2918 plug->magic = PLUG_MAGIC;
2919 INIT_LIST_HEAD(&plug->list);
Jens Axboe320ae512013-10-24 09:20:05 +01002920 INIT_LIST_HEAD(&plug->mq_list);
NeilBrown048c9372011-04-18 09:52:22 +02002921 INIT_LIST_HEAD(&plug->cb_list);
Jens Axboe73c10102011-03-08 13:19:51 +01002922
2923 /*
2924 * If this is a nested plug, don't actually assign it. It will be
2925 * flushed on its own.
2926 */
2927 if (!tsk->plug) {
2928 /*
2929 * Store ordering should not be needed here, since a potential
2930 * preempt will imply a full memory barrier
2931 */
2932 tsk->plug = plug;
2933 }
2934}
2935EXPORT_SYMBOL(blk_start_plug);
2936
2937static int plug_rq_cmp(void *priv, struct list_head *a, struct list_head *b)
2938{
2939 struct request *rqa = container_of(a, struct request, queuelist);
2940 struct request *rqb = container_of(b, struct request, queuelist);
2941
Jianpeng Ma975927b2012-10-25 21:58:17 +02002942 return !(rqa->q < rqb->q ||
2943 (rqa->q == rqb->q && blk_rq_pos(rqa) < blk_rq_pos(rqb)));
Jens Axboe73c10102011-03-08 13:19:51 +01002944}
2945
Jens Axboe49cac012011-04-16 13:51:05 +02002946/*
2947 * If 'from_schedule' is true, then postpone the dispatch of requests
2948 * until a safe kblockd context. We due this to avoid accidental big
2949 * additional stack usage in driver dispatch, in places where the originally
2950 * plugger did not intend it.
2951 */
Jens Axboef6603782011-04-15 15:49:07 +02002952static void queue_unplugged(struct request_queue *q, unsigned int depth,
Jens Axboe49cac012011-04-16 13:51:05 +02002953 bool from_schedule)
Jens Axboe99e22592011-04-18 09:59:55 +02002954 __releases(q->queue_lock)
Jens Axboe94b5eb22011-04-12 10:12:19 +02002955{
Jens Axboe49cac012011-04-16 13:51:05 +02002956 trace_block_unplug(q, depth, !from_schedule);
Jens Axboe99e22592011-04-18 09:59:55 +02002957
Bart Van Assche70460572012-11-28 13:45:56 +01002958 if (from_schedule)
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02002959 blk_run_queue_async(q);
Bart Van Assche70460572012-11-28 13:45:56 +01002960 else
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02002961 __blk_run_queue(q);
Bart Van Assche70460572012-11-28 13:45:56 +01002962 spin_unlock(q->queue_lock);
Jens Axboe94b5eb22011-04-12 10:12:19 +02002963}
2964
NeilBrown74018dc2012-07-31 09:08:15 +02002965static void flush_plug_callbacks(struct blk_plug *plug, bool from_schedule)
NeilBrown048c9372011-04-18 09:52:22 +02002966{
2967 LIST_HEAD(callbacks);
2968
Shaohua Li2a7d5552012-07-31 09:08:15 +02002969 while (!list_empty(&plug->cb_list)) {
2970 list_splice_init(&plug->cb_list, &callbacks);
NeilBrown048c9372011-04-18 09:52:22 +02002971
Shaohua Li2a7d5552012-07-31 09:08:15 +02002972 while (!list_empty(&callbacks)) {
2973 struct blk_plug_cb *cb = list_first_entry(&callbacks,
NeilBrown048c9372011-04-18 09:52:22 +02002974 struct blk_plug_cb,
2975 list);
Shaohua Li2a7d5552012-07-31 09:08:15 +02002976 list_del(&cb->list);
NeilBrown74018dc2012-07-31 09:08:15 +02002977 cb->callback(cb, from_schedule);
Shaohua Li2a7d5552012-07-31 09:08:15 +02002978 }
NeilBrown048c9372011-04-18 09:52:22 +02002979 }
2980}
2981
NeilBrown9cbb1752012-07-31 09:08:14 +02002982struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug, void *data,
2983 int size)
2984{
2985 struct blk_plug *plug = current->plug;
2986 struct blk_plug_cb *cb;
2987
2988 if (!plug)
2989 return NULL;
2990
2991 list_for_each_entry(cb, &plug->cb_list, list)
2992 if (cb->callback == unplug && cb->data == data)
2993 return cb;
2994
2995 /* Not currently on the callback list */
2996 BUG_ON(size < sizeof(*cb));
2997 cb = kzalloc(size, GFP_ATOMIC);
2998 if (cb) {
2999 cb->data = data;
3000 cb->callback = unplug;
3001 list_add(&cb->list, &plug->cb_list);
3002 }
3003 return cb;
3004}
3005EXPORT_SYMBOL(blk_check_plugged);
3006
Jens Axboe49cac012011-04-16 13:51:05 +02003007void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
Jens Axboe73c10102011-03-08 13:19:51 +01003008{
3009 struct request_queue *q;
3010 unsigned long flags;
3011 struct request *rq;
NeilBrown109b8122011-04-11 14:13:10 +02003012 LIST_HEAD(list);
Jens Axboe94b5eb22011-04-12 10:12:19 +02003013 unsigned int depth;
Jens Axboe73c10102011-03-08 13:19:51 +01003014
3015 BUG_ON(plug->magic != PLUG_MAGIC);
3016
NeilBrown74018dc2012-07-31 09:08:15 +02003017 flush_plug_callbacks(plug, from_schedule);
Jens Axboe320ae512013-10-24 09:20:05 +01003018
3019 if (!list_empty(&plug->mq_list))
3020 blk_mq_flush_plug_list(plug, from_schedule);
3021
Jens Axboe73c10102011-03-08 13:19:51 +01003022 if (list_empty(&plug->list))
3023 return;
3024
NeilBrown109b8122011-04-11 14:13:10 +02003025 list_splice_init(&plug->list, &list);
3026
Jianpeng Ma422765c2013-01-11 14:46:09 +01003027 list_sort(NULL, &list, plug_rq_cmp);
Jens Axboe73c10102011-03-08 13:19:51 +01003028
3029 q = NULL;
Jens Axboe94b5eb22011-04-12 10:12:19 +02003030 depth = 0;
Jens Axboe18811272011-04-12 10:11:24 +02003031
3032 /*
3033 * Save and disable interrupts here, to avoid doing it for every
3034 * queue lock we have to take.
3035 */
Jens Axboe73c10102011-03-08 13:19:51 +01003036 local_irq_save(flags);
NeilBrown109b8122011-04-11 14:13:10 +02003037 while (!list_empty(&list)) {
3038 rq = list_entry_rq(list.next);
Jens Axboe73c10102011-03-08 13:19:51 +01003039 list_del_init(&rq->queuelist);
Jens Axboe73c10102011-03-08 13:19:51 +01003040 BUG_ON(!rq->q);
3041 if (rq->q != q) {
Jens Axboe99e22592011-04-18 09:59:55 +02003042 /*
3043 * This drops the queue lock
3044 */
3045 if (q)
Jens Axboe49cac012011-04-16 13:51:05 +02003046 queue_unplugged(q, depth, from_schedule);
Jens Axboe73c10102011-03-08 13:19:51 +01003047 q = rq->q;
Jens Axboe94b5eb22011-04-12 10:12:19 +02003048 depth = 0;
Jens Axboe73c10102011-03-08 13:19:51 +01003049 spin_lock(q->queue_lock);
3050 }
Tejun Heo8ba61432011-12-14 00:33:37 +01003051
3052 /*
3053 * Short-circuit if @q is dead
3054 */
Bart Van Assche3f3299d2012-11-28 13:42:38 +01003055 if (unlikely(blk_queue_dying(q))) {
Tejun Heo8ba61432011-12-14 00:33:37 +01003056 __blk_end_request_all(rq, -ENODEV);
3057 continue;
3058 }
3059
Jens Axboe73c10102011-03-08 13:19:51 +01003060 /*
3061 * rq is already accounted, so use raw insert
3062 */
Jens Axboe401a18e2011-03-25 16:57:52 +01003063 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA))
3064 __elv_add_request(q, rq, ELEVATOR_INSERT_FLUSH);
3065 else
3066 __elv_add_request(q, rq, ELEVATOR_INSERT_SORT_MERGE);
Jens Axboe94b5eb22011-04-12 10:12:19 +02003067
3068 depth++;
Jens Axboe73c10102011-03-08 13:19:51 +01003069 }
3070
Jens Axboe99e22592011-04-18 09:59:55 +02003071 /*
3072 * This drops the queue lock
3073 */
3074 if (q)
Jens Axboe49cac012011-04-16 13:51:05 +02003075 queue_unplugged(q, depth, from_schedule);
Jens Axboe73c10102011-03-08 13:19:51 +01003076
Jens Axboe73c10102011-03-08 13:19:51 +01003077 local_irq_restore(flags);
3078}
Jens Axboe73c10102011-03-08 13:19:51 +01003079
3080void blk_finish_plug(struct blk_plug *plug)
3081{
Jens Axboef6603782011-04-15 15:49:07 +02003082 blk_flush_plug_list(plug, false);
Christoph Hellwig88b996c2011-04-15 15:20:10 +02003083
3084 if (plug == current->plug)
3085 current->plug = NULL;
Jens Axboe73c10102011-03-08 13:19:51 +01003086}
3087EXPORT_SYMBOL(blk_finish_plug);
3088
Lin Ming6c954662013-03-23 11:42:26 +08003089#ifdef CONFIG_PM_RUNTIME
3090/**
3091 * blk_pm_runtime_init - Block layer runtime PM initialization routine
3092 * @q: the queue of the device
3093 * @dev: the device the queue belongs to
3094 *
3095 * Description:
3096 * Initialize runtime-PM-related fields for @q and start auto suspend for
3097 * @dev. Drivers that want to take advantage of request-based runtime PM
3098 * should call this function after @dev has been initialized, and its
3099 * request queue @q has been allocated, and runtime PM for it can not happen
3100 * yet(either due to disabled/forbidden or its usage_count > 0). In most
3101 * cases, driver should call this function before any I/O has taken place.
3102 *
3103 * This function takes care of setting up using auto suspend for the device,
3104 * the autosuspend delay is set to -1 to make runtime suspend impossible
3105 * until an updated value is either set by user or by driver. Drivers do
3106 * not need to touch other autosuspend settings.
3107 *
3108 * The block layer runtime PM is request based, so only works for drivers
3109 * that use request as their IO unit instead of those directly use bio's.
3110 */
3111void blk_pm_runtime_init(struct request_queue *q, struct device *dev)
3112{
3113 q->dev = dev;
3114 q->rpm_status = RPM_ACTIVE;
3115 pm_runtime_set_autosuspend_delay(q->dev, -1);
3116 pm_runtime_use_autosuspend(q->dev);
3117}
3118EXPORT_SYMBOL(blk_pm_runtime_init);
3119
3120/**
3121 * blk_pre_runtime_suspend - Pre runtime suspend check
3122 * @q: the queue of the device
3123 *
3124 * Description:
3125 * This function will check if runtime suspend is allowed for the device
3126 * by examining if there are any requests pending in the queue. If there
3127 * are requests pending, the device can not be runtime suspended; otherwise,
3128 * the queue's status will be updated to SUSPENDING and the driver can
3129 * proceed to suspend the device.
3130 *
3131 * For the not allowed case, we mark last busy for the device so that
3132 * runtime PM core will try to autosuspend it some time later.
3133 *
3134 * This function should be called near the start of the device's
3135 * runtime_suspend callback.
3136 *
3137 * Return:
3138 * 0 - OK to runtime suspend the device
3139 * -EBUSY - Device should not be runtime suspended
3140 */
3141int blk_pre_runtime_suspend(struct request_queue *q)
3142{
3143 int ret = 0;
3144
3145 spin_lock_irq(q->queue_lock);
3146 if (q->nr_pending) {
3147 ret = -EBUSY;
3148 pm_runtime_mark_last_busy(q->dev);
3149 } else {
3150 q->rpm_status = RPM_SUSPENDING;
3151 }
3152 spin_unlock_irq(q->queue_lock);
3153 return ret;
3154}
3155EXPORT_SYMBOL(blk_pre_runtime_suspend);
3156
3157/**
3158 * blk_post_runtime_suspend - Post runtime suspend processing
3159 * @q: the queue of the device
3160 * @err: return value of the device's runtime_suspend function
3161 *
3162 * Description:
3163 * Update the queue's runtime status according to the return value of the
3164 * device's runtime suspend function and mark last busy for the device so
3165 * that PM core will try to auto suspend the device at a later time.
3166 *
3167 * This function should be called near the end of the device's
3168 * runtime_suspend callback.
3169 */
3170void blk_post_runtime_suspend(struct request_queue *q, int err)
3171{
3172 spin_lock_irq(q->queue_lock);
3173 if (!err) {
3174 q->rpm_status = RPM_SUSPENDED;
3175 } else {
3176 q->rpm_status = RPM_ACTIVE;
3177 pm_runtime_mark_last_busy(q->dev);
3178 }
3179 spin_unlock_irq(q->queue_lock);
3180}
3181EXPORT_SYMBOL(blk_post_runtime_suspend);
3182
3183/**
3184 * blk_pre_runtime_resume - Pre runtime resume processing
3185 * @q: the queue of the device
3186 *
3187 * Description:
3188 * Update the queue's runtime status to RESUMING in preparation for the
3189 * runtime resume of the device.
3190 *
3191 * This function should be called near the start of the device's
3192 * runtime_resume callback.
3193 */
3194void blk_pre_runtime_resume(struct request_queue *q)
3195{
3196 spin_lock_irq(q->queue_lock);
3197 q->rpm_status = RPM_RESUMING;
3198 spin_unlock_irq(q->queue_lock);
3199}
3200EXPORT_SYMBOL(blk_pre_runtime_resume);
3201
3202/**
3203 * blk_post_runtime_resume - Post runtime resume processing
3204 * @q: the queue of the device
3205 * @err: return value of the device's runtime_resume function
3206 *
3207 * Description:
3208 * Update the queue's runtime status according to the return value of the
3209 * device's runtime_resume function. If it is successfully resumed, process
3210 * the requests that are queued into the device's queue when it is resuming
3211 * and then mark last busy and initiate autosuspend for it.
3212 *
3213 * This function should be called near the end of the device's
3214 * runtime_resume callback.
3215 */
3216void blk_post_runtime_resume(struct request_queue *q, int err)
3217{
3218 spin_lock_irq(q->queue_lock);
3219 if (!err) {
3220 q->rpm_status = RPM_ACTIVE;
3221 __blk_run_queue(q);
3222 pm_runtime_mark_last_busy(q->dev);
Aaron Luc60855c2013-05-17 15:47:20 +08003223 pm_request_autosuspend(q->dev);
Lin Ming6c954662013-03-23 11:42:26 +08003224 } else {
3225 q->rpm_status = RPM_SUSPENDED;
3226 }
3227 spin_unlock_irq(q->queue_lock);
3228}
3229EXPORT_SYMBOL(blk_post_runtime_resume);
3230#endif
3231
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232int __init blk_dev_init(void)
3233{
Nikanth Karthikesan9eb55b02009-04-27 14:53:54 +02003234 BUILD_BUG_ON(__REQ_NR_BITS > 8 *
3235 sizeof(((struct request *)0)->cmd_flags));
3236
Tejun Heo89b90be2011-01-03 15:01:47 +01003237 /* used for unplugging and affects IO latency/throughput - HIGHPRI */
3238 kblockd_workqueue = alloc_workqueue("kblockd",
Viresh Kumar695588f2013-04-24 17:12:56 +05303239 WQ_MEM_RECLAIM | WQ_HIGHPRI |
3240 WQ_POWER_EFFICIENT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 if (!kblockd_workqueue)
3242 panic("Failed to create kblockd\n");
3243
3244 request_cachep = kmem_cache_create("blkdev_requests",
Paul Mundt20c2df82007-07-20 10:11:58 +09003245 sizeof(struct request), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246
Jens Axboe8324aa92008-01-29 14:51:59 +01003247 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02003248 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 return 0;
3251}