blob: 01e928ee4dbb87ef8c09f4f5766d9b8bd3679c51 [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>
19#include <linux/highmem.h>
20#include <linux/mm.h>
21#include <linux/kernel_stat.h>
22#include <linux/string.h>
23#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/completion.h>
25#include <linux/slab.h>
26#include <linux/swap.h>
27#include <linux/writeback.h>
Andrew Mortonfaccbd42006-12-10 02:19:35 -080028#include <linux/task_io_accounting_ops.h>
Akinobu Mitac17bb492006-12-08 02:39:46 -080029#include <linux/fault-inject.h>
Jens Axboe73c10102011-03-08 13:19:51 +010030#include <linux/list_sort.h>
Tejun Heoe3c78ca2011-10-19 14:32:38 +020031#include <linux/delay.h>
Asutosh Das75de0c32013-03-07 17:43:35 +053032#include <linux/ratelimit.h>
Lin Mingff70c822013-03-23 11:42:26 +080033#include <linux/pm_runtime.h>
Li Zefan55782132009-06-09 13:43:05 +080034
35#define CREATE_TRACE_POINTS
36#include <trace/events/block.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Jens Axboe8324aa92008-01-29 14:51:59 +010038#include "blk.h"
39
Mike Snitzerd07335e2010-11-16 12:52:38 +010040EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_remap);
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +020041EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_remap);
Li Zefan55782132009-06-09 13:43:05 +080042EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_complete);
Ingo Molnar0bfc2452008-11-26 11:59:56 +010043
Tejun Heoa73f7302011-12-14 00:33:37 +010044DEFINE_IDA(blk_queue_ida);
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/*
47 * For the allocated request tables
48 */
Adrian Bunk5ece6c52008-02-18 13:45:51 +010049static struct kmem_cache *request_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51/*
52 * For queue allocation
53 */
Jens Axboe6728cb02008-01-31 13:03:55 +010054struct kmem_cache *blk_requestq_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 * Controlling structure to kblockd
58 */
Jens Axboeff856ba2006-01-09 16:02:34 +010059static struct workqueue_struct *kblockd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Jens Axboe26b82562008-01-29 13:54:41 +010061static void drive_stat_acct(struct request *rq, int new_io)
62{
Jens Axboe28f13702008-05-07 10:15:46 +020063 struct hd_struct *part;
Jens Axboe26b82562008-01-29 13:54:41 +010064 int rw = rq_data_dir(rq);
Tejun Heoc9959052008-08-25 19:47:21 +090065 int cpu;
Jens Axboe26b82562008-01-29 13:54:41 +010066
Jens Axboec2553b52009-04-24 08:10:11 +020067 if (!blk_do_io_stat(rq))
Jens Axboe26b82562008-01-29 13:54:41 +010068 return;
69
Tejun Heo074a7ac2008-08-25 19:56:14 +090070 cpu = part_stat_lock();
Tejun Heoc9959052008-08-25 19:47:21 +090071
Jerome Marchand09e099d2011-01-05 16:57:38 +010072 if (!new_io) {
73 part = rq->part;
Tejun Heo074a7ac2008-08-25 19:56:14 +090074 part_stat_inc(cpu, part, merges[rw]);
Jerome Marchand09e099d2011-01-05 16:57:38 +010075 } else {
76 part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq));
Jens Axboe6c23a962011-01-07 08:43:37 +010077 if (!hd_struct_try_get(part)) {
Jerome Marchand09e099d2011-01-05 16:57:38 +010078 /*
79 * The partition is already being removed,
80 * the request will be accounted on the disk only
81 *
82 * We take a reference on disk->part0 although that
83 * partition will never be deleted, so we can treat
84 * it as any other partition.
85 */
86 part = &rq->rq_disk->part0;
Jens Axboe6c23a962011-01-07 08:43:37 +010087 hd_struct_get(part);
Jerome Marchand09e099d2011-01-05 16:57:38 +010088 }
Tejun Heo074a7ac2008-08-25 19:56:14 +090089 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +020090 part_inc_in_flight(part, rw);
Jerome Marchand09e099d2011-01-05 16:57:38 +010091 rq->part = part;
Jens Axboe26b82562008-01-29 13:54:41 +010092 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +020093
Tejun Heo074a7ac2008-08-25 19:56:14 +090094 part_stat_unlock();
Jens Axboe26b82562008-01-29 13:54:41 +010095}
96
Jens Axboe8324aa92008-01-29 14:51:59 +010097void blk_queue_congestion_threshold(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 int nr;
100
101 nr = q->nr_requests - (q->nr_requests / 8) + 1;
102 if (nr > q->nr_requests)
103 nr = q->nr_requests;
104 q->nr_congestion_on = nr;
105
106 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
107 if (nr < 1)
108 nr = 1;
109 q->nr_congestion_off = nr;
110}
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/**
113 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
114 * @bdev: device
115 *
116 * Locates the passed device's request queue and returns the address of its
117 * backing_dev_info
118 *
119 * Will return NULL if the request queue cannot be located.
120 */
121struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
122{
123 struct backing_dev_info *ret = NULL;
Jens Axboe165125e2007-07-24 09:28:11 +0200124 struct request_queue *q = bdev_get_queue(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 if (q)
127 ret = &q->backing_dev_info;
128 return ret;
129}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130EXPORT_SYMBOL(blk_get_backing_dev_info);
131
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200132void blk_rq_init(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200134 memset(rq, 0, sizeof(*rq));
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 INIT_LIST_HEAD(&rq->queuelist);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700137 INIT_LIST_HEAD(&rq->timeout_list);
Jens Axboec7c22e42008-09-13 20:26:01 +0200138 rq->cpu = -1;
Jens Axboe63a71382008-02-08 12:41:03 +0100139 rq->q = q;
Tejun Heoa2dec7b2009-05-07 22:24:44 +0900140 rq->__sector = (sector_t) -1;
Jens Axboe2e662b62006-07-13 11:55:04 +0200141 INIT_HLIST_NODE(&rq->hash);
142 RB_CLEAR_NODE(&rq->rb_node);
FUJITA Tomonorid7e3c322008-04-29 09:54:39 +0200143 rq->cmd = rq->__cmd;
Li Zefane2494e12009-04-02 13:43:26 +0800144 rq->cmd_len = BLK_MAX_CDB;
Jens Axboe63a71382008-02-08 12:41:03 +0100145 rq->tag = -1;
Jens Axboe63a71382008-02-08 12:41:03 +0100146 rq->ref_count = 1;
Tejun Heob243ddc2009-04-23 11:05:18 +0900147 rq->start_time = jiffies;
Divyesh Shah91952912010-04-01 15:01:41 -0700148 set_start_time_ns(rq);
Jerome Marchand09e099d2011-01-05 16:57:38 +0100149 rq->part = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200151EXPORT_SYMBOL(blk_rq_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
NeilBrown5bb23a62007-09-27 12:46:13 +0200153static void req_bio_endio(struct request *rq, struct bio *bio,
154 unsigned int nbytes, int error)
Tejun Heo797e7db2006-01-06 09:51:03 +0100155{
Tejun Heo143a87f2011-01-25 12:43:52 +0100156 if (error)
157 clear_bit(BIO_UPTODATE, &bio->bi_flags);
158 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
159 error = -EIO;
Tejun Heo797e7db2006-01-06 09:51:03 +0100160
Tejun Heo143a87f2011-01-25 12:43:52 +0100161 if (unlikely(nbytes > bio->bi_size)) {
162 printk(KERN_ERR "%s: want %u bytes done, %u left\n",
163 __func__, nbytes, bio->bi_size);
164 nbytes = bio->bi_size;
NeilBrown5bb23a62007-09-27 12:46:13 +0200165 }
Tejun Heo143a87f2011-01-25 12:43:52 +0100166
167 if (unlikely(rq->cmd_flags & REQ_QUIET))
168 set_bit(BIO_QUIET, &bio->bi_flags);
169
170 bio->bi_size -= nbytes;
171 bio->bi_sector += (nbytes >> 9);
172
173 if (bio_integrity(bio))
174 bio_integrity_advance(bio, nbytes);
175
176 /* don't actually finish bio if it's part of flush sequence */
177 if (bio->bi_size == 0 && !(rq->cmd_flags & REQ_FLUSH_SEQ))
178 bio_endio(bio, error);
Tejun Heo797e7db2006-01-06 09:51:03 +0100179}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181void blk_dump_rq_flags(struct request *rq, char *msg)
182{
183 int bit;
184
Jens Axboe6728cb02008-01-31 13:03:55 +0100185 printk(KERN_INFO "%s: dev %s: type=%x, flags=%x\n", msg,
Jens Axboe4aff5e22006-08-10 08:44:47 +0200186 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
187 rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Tejun Heo83096eb2009-05-07 22:24:39 +0900189 printk(KERN_INFO " sector %llu, nr/cnr %u/%u\n",
190 (unsigned long long)blk_rq_pos(rq),
191 blk_rq_sectors(rq), blk_rq_cur_sectors(rq));
Tejun Heo731ec492009-04-23 11:05:20 +0900192 printk(KERN_INFO " bio %p, biotail %p, buffer %p, len %u\n",
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900193 rq->bio, rq->biotail, rq->buffer, blk_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200195 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
Jens Axboe6728cb02008-01-31 13:03:55 +0100196 printk(KERN_INFO " cdb: ");
FUJITA Tomonorid34c87e2008-04-29 14:37:52 +0200197 for (bit = 0; bit < BLK_MAX_CDB; bit++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 printk("%02x ", rq->cmd[bit]);
199 printk("\n");
200 }
201}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202EXPORT_SYMBOL(blk_dump_rq_flags);
203
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500204static void blk_delay_work(struct work_struct *work)
Jens Axboe6c5e0c42008-08-01 20:31:32 +0200205{
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500206 struct request_queue *q;
Jens Axboe6c5e0c42008-08-01 20:31:32 +0200207
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500208 q = container_of(work, struct request_queue, delay_work.work);
209 spin_lock_irq(q->queue_lock);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200210 __blk_run_queue(q);
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500211 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214/**
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500215 * blk_delay_queue - restart queueing after defined interval
216 * @q: The &struct request_queue in question
217 * @msecs: Delay in msecs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 *
219 * Description:
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500220 * Sometimes queueing needs to be postponed for a little while, to allow
221 * resources to come back. This function will make sure that queueing is
222 * restarted around the specified time.
223 */
224void blk_delay_queue(struct request_queue *q, unsigned long msecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Jens Axboe4521cc42011-04-18 11:36:39 +0200226 queue_delayed_work(kblockd_workqueue, &q->delay_work,
227 msecs_to_jiffies(msecs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500229EXPORT_SYMBOL(blk_delay_queue);
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231/**
232 * blk_start_queue - restart a previously stopped queue
Jens Axboe165125e2007-07-24 09:28:11 +0200233 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 *
235 * Description:
236 * blk_start_queue() will clear the stop flag on the queue, and call
237 * the request_fn for the queue if it was in a stopped state when
238 * entered. Also see blk_stop_queue(). Queue lock must be held.
239 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200240void blk_start_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200242 WARN_ON(!irqs_disabled());
243
Nick Piggin75ad23b2008-04-29 14:48:33 +0200244 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200245 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247EXPORT_SYMBOL(blk_start_queue);
248
249/**
250 * blk_stop_queue - stop a queue
Jens Axboe165125e2007-07-24 09:28:11 +0200251 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 *
253 * Description:
254 * The Linux block layer assumes that a block driver will consume all
255 * entries on the request queue when the request_fn strategy is called.
256 * Often this will not happen, because of hardware limitations (queue
257 * depth settings). If a device driver gets a 'queue full' response,
258 * or if it simply chooses not to queue more I/O at one point, it can
259 * call this function to prevent the request_fn from being called until
260 * the driver has signalled it's ready to go again. This happens by calling
261 * blk_start_queue() to restart queue operations. Queue lock must be held.
262 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200263void blk_stop_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Jens Axboead3d9d72011-03-25 16:58:59 +0100265 __cancel_delayed_work(&q->delay_work);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200266 queue_flag_set(QUEUE_FLAG_STOPPED, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268EXPORT_SYMBOL(blk_stop_queue);
269
270/**
271 * blk_sync_queue - cancel any pending callbacks on a queue
272 * @q: the queue
273 *
274 * Description:
275 * The block layer may perform asynchronous callback activity
276 * on a queue, such as calling the unplug function after a timeout.
277 * A block device may call blk_sync_queue to ensure that any
278 * such activity is cancelled, thus allowing it to release resources
Michael Opdenacker59c51592007-05-09 08:57:56 +0200279 * that the callbacks might use. The caller must already have made sure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 * that its ->make_request_fn will not re-add plugging prior to calling
281 * this function.
282 *
Vivek Goyalda527772011-03-02 19:05:33 -0500283 * This function does not cancel any asynchronous activity arising
284 * out of elevator or throttling code. That would require elevaotor_exit()
285 * and blk_throtl_exit() to be called with queue lock initialized.
286 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 */
288void blk_sync_queue(struct request_queue *q)
289{
Jens Axboe70ed28b2008-11-19 14:38:39 +0100290 del_timer_sync(&q->timeout);
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500291 cancel_delayed_work_sync(&q->delay_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293EXPORT_SYMBOL(blk_sync_queue);
294
295/**
Jens Axboe80a4b582008-10-14 09:51:06 +0200296 * __blk_run_queue - run a single device queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200298 *
299 * Description:
300 * See @blk_run_queue. This variant must be called with the queue lock
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200301 * held and interrupts disabled.
Tatyana Brokhman88fd1b42012-12-04 16:04:15 +0200302 * Device driver will be notified of an urgent request
303 * pending under the following conditions:
304 * 1. The driver and the current scheduler support urgent reques handling
305 * 2. There is an urgent request pending in the scheduler
306 * 3. There isn't already an urgent request in flight, meaning previously
307 * notified urgent request completed (!q->notified_urgent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 */
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200309void __blk_run_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Tejun Heoa538cd02009-04-23 11:05:17 +0900311 if (unlikely(blk_queue_stopped(q)))
312 return;
313
Tatyana Brokhman88fd1b42012-12-04 16:04:15 +0200314 if (!q->notified_urgent &&
315 q->elevator->type->ops.elevator_is_urgent_fn &&
316 q->urgent_request_fn &&
Tatyana Brokhmana953d602014-01-26 12:05:57 +0200317 q->elevator->type->ops.elevator_is_urgent_fn(q) &&
318 list_empty(&q->flush_data_in_flight)) {
Tatyana Brokhman88fd1b42012-12-04 16:04:15 +0200319 q->notified_urgent = true;
320 q->urgent_request_fn(q);
321 } else
322 q->request_fn(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200323}
324EXPORT_SYMBOL(__blk_run_queue);
Jens Axboedac07ec2006-05-11 08:20:16 +0200325
Nick Piggin75ad23b2008-04-29 14:48:33 +0200326/**
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200327 * blk_run_queue_async - run a single device queue in workqueue context
328 * @q: The queue to run
329 *
330 * Description:
331 * Tells kblockd to perform the equivalent of @blk_run_queue on behalf
332 * of us.
333 */
334void blk_run_queue_async(struct request_queue *q)
335{
Shaohua Li3ec717b2011-05-18 11:22:43 +0200336 if (likely(!blk_queue_stopped(q))) {
337 __cancel_delayed_work(&q->delay_work);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200338 queue_delayed_work(kblockd_workqueue, &q->delay_work, 0);
Shaohua Li3ec717b2011-05-18 11:22:43 +0200339 }
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200340}
Jens Axboec21e6be2011-04-19 13:32:46 +0200341EXPORT_SYMBOL(blk_run_queue_async);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200342
343/**
Nick Piggin75ad23b2008-04-29 14:48:33 +0200344 * blk_run_queue - run a single device queue
345 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200346 *
347 * Description:
348 * Invoke request handling on this queue, if it has pending work to do.
Tejun Heoa7f55792009-04-23 11:05:17 +0900349 * May be used to restart queueing when a request has completed.
Nick Piggin75ad23b2008-04-29 14:48:33 +0200350 */
351void blk_run_queue(struct request_queue *q)
352{
353 unsigned long flags;
354
355 spin_lock_irqsave(q->queue_lock, flags);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200356 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 spin_unlock_irqrestore(q->queue_lock, flags);
358}
359EXPORT_SYMBOL(blk_run_queue);
360
Jens Axboe165125e2007-07-24 09:28:11 +0200361void blk_put_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500362{
363 kobject_put(&q->kobj);
364}
Jens Axboed86e0e82011-05-27 07:44:43 +0200365EXPORT_SYMBOL(blk_put_queue);
Al Viro483f4af2006-03-18 18:34:37 -0500366
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200367/**
368 * blk_drain_queue - drain requests from request_queue
369 * @q: queue to drain
Tejun Heoc9a929d2011-10-19 14:42:16 +0200370 * @drain_all: whether to drain all requests or only the ones w/ ELVPRIV
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200371 *
Tejun Heoc9a929d2011-10-19 14:42:16 +0200372 * Drain requests from @q. If @drain_all is set, all requests are drained.
373 * If not, only ELVPRIV requests are drained. The caller is responsible
374 * for ensuring that no new requests which need to be drained are queued.
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200375 */
Tejun Heoc9a929d2011-10-19 14:42:16 +0200376void blk_drain_queue(struct request_queue *q, bool drain_all)
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200377{
378 while (true) {
Tejun Heo481a7d62011-12-14 00:33:37 +0100379 bool drain = false;
380 int i;
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200381
382 spin_lock_irq(q->queue_lock);
383
384 elv_drain_elevator(q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200385 if (drain_all)
386 blk_throtl_drain(q);
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200387
Tejun Heo4eabc942011-12-15 20:03:04 +0100388 /*
389 * This function might be called on a queue which failed
390 * driver init after queue creation. Some drivers
391 * (e.g. fd) get unhappy in such cases. Kick queue iff
392 * dispatch queue has something on it.
393 */
394 if (!list_empty(&q->queue_head))
395 __blk_run_queue(q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200396
Tejun Heo481a7d62011-12-14 00:33:37 +0100397 drain |= q->rq.elvpriv;
398
399 /*
400 * Unfortunately, requests are queued at and tracked from
401 * multiple places and there's no single counter which can
402 * be drained. Check all the queues and counters.
403 */
404 if (drain_all) {
405 drain |= !list_empty(&q->queue_head);
406 for (i = 0; i < 2; i++) {
407 drain |= q->rq.count[i];
408 drain |= q->in_flight[i];
409 drain |= !list_empty(&q->flush_queue[i]);
410 }
411 }
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200412
413 spin_unlock_irq(q->queue_lock);
414
Tejun Heo481a7d62011-12-14 00:33:37 +0100415 if (!drain)
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200416 break;
417 msleep(10);
418 }
419}
420
Tejun Heoc9a929d2011-10-19 14:42:16 +0200421/**
422 * blk_cleanup_queue - shutdown a request queue
423 * @q: request queue to shutdown
424 *
425 * Mark @q DEAD, drain all pending requests, destroy and put it. All
426 * future requests will be failed immediately with -ENODEV.
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500427 */
Jens Axboe6728cb02008-01-31 13:03:55 +0100428void blk_cleanup_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500429{
Tejun Heoc9a929d2011-10-19 14:42:16 +0200430 spinlock_t *lock = q->queue_lock;
Jens Axboee3335de2008-09-18 09:22:54 -0700431
Tejun Heoc9a929d2011-10-19 14:42:16 +0200432 /* mark @q DEAD, no new request or merges will be allowed afterwards */
Al Viro483f4af2006-03-18 18:34:37 -0500433 mutex_lock(&q->sysfs_lock);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200434 queue_flag_set_unlocked(QUEUE_FLAG_DEAD, q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200435
436 spin_lock_irq(lock);
437 queue_flag_set(QUEUE_FLAG_NOMERGES, q);
438 queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
439 queue_flag_set(QUEUE_FLAG_DEAD, q);
Al Viro483f4af2006-03-18 18:34:37 -0500440
Hannes Reinecke777eb1b2011-09-28 08:07:01 -0600441 if (q->queue_lock != &q->__queue_lock)
442 q->queue_lock = &q->__queue_lock;
Vivek Goyalda527772011-03-02 19:05:33 -0500443
Tejun Heoc9a929d2011-10-19 14:42:16 +0200444 spin_unlock_irq(lock);
445 mutex_unlock(&q->sysfs_lock);
446
Tejun Heo6dd9ad72011-11-03 18:52:11 +0100447 /*
448 * Drain all requests queued before DEAD marking. The caller might
449 * be trying to tear down @q before its elevator is initialized, in
450 * which case we don't want to call into draining.
451 */
452 if (q->elevator)
453 blk_drain_queue(q, true);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200454
455 /* @q won't process any more request, flush async actions */
456 del_timer_sync(&q->backing_dev_info.laptop_mode_wb_timer);
457 blk_sync_queue(q);
458
459 /* @q is and will stay empty, shutdown and put */
Al Viro483f4af2006-03-18 18:34:37 -0500460 blk_put_queue(q);
461}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462EXPORT_SYMBOL(blk_cleanup_queue);
463
Jens Axboe165125e2007-07-24 09:28:11 +0200464static int blk_init_free_list(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 struct request_list *rl = &q->rq;
467
Mike Snitzer1abec4f2010-05-25 13:15:15 -0400468 if (unlikely(rl->rq_pool))
469 return 0;
470
Jens Axboe1faa16d2009-04-06 14:48:01 +0200471 rl->count[BLK_RW_SYNC] = rl->count[BLK_RW_ASYNC] = 0;
472 rl->starved[BLK_RW_SYNC] = rl->starved[BLK_RW_ASYNC] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200473 rl->elvpriv = 0;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200474 init_waitqueue_head(&rl->wait[BLK_RW_SYNC]);
475 init_waitqueue_head(&rl->wait[BLK_RW_ASYNC]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Christoph Lameter19460892005-06-23 00:08:19 -0700477 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
478 mempool_free_slab, request_cachep, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 if (!rl->rq_pool)
481 return -ENOMEM;
482
483 return 0;
484}
485
Jens Axboe165125e2007-07-24 09:28:11 +0200486struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Christoph Lameter19460892005-06-23 00:08:19 -0700488 return blk_alloc_queue_node(gfp_mask, -1);
489}
490EXPORT_SYMBOL(blk_alloc_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Jens Axboe165125e2007-07-24 09:28:11 +0200492struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -0700493{
Jens Axboe165125e2007-07-24 09:28:11 +0200494 struct request_queue *q;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700495 int err;
Christoph Lameter19460892005-06-23 00:08:19 -0700496
Jens Axboe8324aa92008-01-29 14:51:59 +0100497 q = kmem_cache_alloc_node(blk_requestq_cachep,
Christoph Lameter94f60302007-07-17 04:03:29 -0700498 gfp_mask | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (!q)
500 return NULL;
501
Dan Carpenter00380a42012-03-23 09:58:54 +0100502 q->id = ida_simple_get(&blk_queue_ida, 0, 0, gfp_mask);
Tejun Heoa73f7302011-12-14 00:33:37 +0100503 if (q->id < 0)
504 goto fail_q;
505
Jens Axboe0989a022009-06-12 14:42:56 +0200506 q->backing_dev_info.ra_pages =
507 (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
508 q->backing_dev_info.state = 0;
509 q->backing_dev_info.capabilities = BDI_CAP_MAP_COPY;
Jens Axboed9938312009-06-12 14:45:52 +0200510 q->backing_dev_info.name = "block";
Mike Snitzer51514122011-11-23 10:59:13 +0100511 q->node = node_id;
Jens Axboe0989a022009-06-12 14:42:56 +0200512
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700513 err = bdi_init(&q->backing_dev_info);
Tejun Heoa73f7302011-12-14 00:33:37 +0100514 if (err)
515 goto fail_id;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700516
Tejun Heoa73f7302011-12-14 00:33:37 +0100517 if (blk_throtl_init(q))
Mikulas Patocka209fc332013-10-14 12:11:36 -0400518 goto fail_bdi;
Vivek Goyale43473b2010-09-15 17:06:35 -0400519
Matthew Garrett31373d02010-04-06 14:25:14 +0200520 setup_timer(&q->backing_dev_info.laptop_mode_wb_timer,
521 laptop_mode_timer_fn, (unsigned long) q);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700522 setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
523 INIT_LIST_HEAD(&q->timeout_list);
Tejun Heoa612fdd2011-12-14 00:33:41 +0100524 INIT_LIST_HEAD(&q->icq_list);
Tejun Heoae1b1532011-01-25 12:43:54 +0100525 INIT_LIST_HEAD(&q->flush_queue[0]);
526 INIT_LIST_HEAD(&q->flush_queue[1]);
527 INIT_LIST_HEAD(&q->flush_data_in_flight);
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500528 INIT_DELAYED_WORK(&q->delay_work, blk_delay_work);
Al Viro483f4af2006-03-18 18:34:37 -0500529
Jens Axboe8324aa92008-01-29 14:51:59 +0100530 kobject_init(&q->kobj, &blk_queue_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Al Viro483f4af2006-03-18 18:34:37 -0500532 mutex_init(&q->sysfs_lock);
Neil Browne7e72bf2008-05-14 16:05:54 -0700533 spin_lock_init(&q->__queue_lock);
Al Viro483f4af2006-03-18 18:34:37 -0500534
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500535 /*
536 * By default initialize queue_lock to internal lock and driver can
537 * override it later if need be.
538 */
539 q->queue_lock = &q->__queue_lock;
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return q;
Tejun Heoa73f7302011-12-14 00:33:37 +0100542
Mikulas Patocka209fc332013-10-14 12:11:36 -0400543fail_bdi:
544 bdi_destroy(&q->backing_dev_info);
Tejun Heoa73f7302011-12-14 00:33:37 +0100545fail_id:
546 ida_simple_remove(&blk_queue_ida, q->id);
547fail_q:
548 kmem_cache_free(blk_requestq_cachep, q);
549 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
Christoph Lameter19460892005-06-23 00:08:19 -0700551EXPORT_SYMBOL(blk_alloc_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553/**
554 * blk_init_queue - prepare a request queue for use with a block device
555 * @rfn: The function to be called to process requests that have been
556 * placed on the queue.
557 * @lock: Request queue spin lock
558 *
559 * Description:
560 * If a block device wishes to use the standard request handling procedures,
561 * which sorts requests and coalesces adjacent requests, then it must
562 * call blk_init_queue(). The function @rfn will be called when there
563 * are requests on the queue that need to be processed. If the device
564 * supports plugging, then @rfn may not be called immediately when requests
565 * are available on the queue, but may be called at some time later instead.
566 * Plugged queues are generally unplugged when a buffer belonging to one
567 * of the requests on the queue is needed, or due to memory pressure.
568 *
569 * @rfn is not required, or even expected, to remove all requests off the
570 * queue, but only as many as it can handle at a time. If it does leave
571 * requests on the queue, it is responsible for arranging that the requests
572 * get dealt with eventually.
573 *
574 * The queue spin lock must be held while manipulating the requests on the
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200575 * request queue; this lock will be taken also from interrupt context, so irq
576 * disabling is needed for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 *
Randy Dunlap710027a2008-08-19 20:13:11 +0200578 * Function returns a pointer to the initialized request queue, or %NULL if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 * it didn't succeed.
580 *
581 * Note:
582 * blk_init_queue() must be paired with a blk_cleanup_queue() call
583 * when the block device is deactivated (such as at module unload).
584 **/
Christoph Lameter19460892005-06-23 00:08:19 -0700585
Jens Axboe165125e2007-07-24 09:28:11 +0200586struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Christoph Lameter19460892005-06-23 00:08:19 -0700588 return blk_init_queue_node(rfn, lock, -1);
589}
590EXPORT_SYMBOL(blk_init_queue);
591
Jens Axboe165125e2007-07-24 09:28:11 +0200592struct request_queue *
Christoph Lameter19460892005-06-23 00:08:19 -0700593blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
594{
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600595 struct request_queue *uninit_q, *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600597 uninit_q = blk_alloc_queue_node(GFP_KERNEL, node_id);
598 if (!uninit_q)
599 return NULL;
600
Mike Snitzer51514122011-11-23 10:59:13 +0100601 q = blk_init_allocated_queue(uninit_q, rfn, lock);
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600602 if (!q)
603 blk_cleanup_queue(uninit_q);
604
605 return q;
Mike Snitzer01effb02010-05-11 08:57:42 +0200606}
607EXPORT_SYMBOL(blk_init_queue_node);
608
609struct request_queue *
610blk_init_allocated_queue(struct request_queue *q, request_fn_proc *rfn,
611 spinlock_t *lock)
612{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (!q)
614 return NULL;
615
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600616 if (blk_init_free_list(q))
Al Viro8669aaf2006-03-18 13:50:00 -0500617 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 q->request_fn = rfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 q->prep_rq_fn = NULL;
James Bottomley28018c22010-07-01 19:49:17 +0900621 q->unprep_rq_fn = NULL;
Tejun Heof7066332012-09-20 14:09:30 -0700622 q->queue_flags |= QUEUE_FLAG_DEFAULT;
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500623
624 /* Override internal queue lock with supplied lock pointer */
625 if (lock)
626 q->queue_lock = lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Jens Axboef3b144a2009-03-06 08:48:33 +0100628 /*
629 * This also sets hw/phys segments, boundary and size
630 */
Jens Axboec20e8de2011-09-12 12:03:37 +0200631 blk_queue_make_request(q, blk_queue_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Alan Stern44ec9542007-02-20 11:01:57 -0500633 q->sg_reserved_size = INT_MAX;
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 /*
636 * all done
637 */
638 if (!elevator_init(q, NULL)) {
639 blk_queue_congestion_threshold(q);
640 return q;
641 }
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return NULL;
644}
Mike Snitzer51514122011-11-23 10:59:13 +0100645EXPORT_SYMBOL(blk_init_allocated_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Tejun Heo09ac46c2011-12-14 00:33:38 +0100647bool blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Tejun Heo34f60552011-12-14 00:33:37 +0100649 if (likely(!blk_queue_dead(q))) {
Tejun Heo09ac46c2011-12-14 00:33:38 +0100650 __blk_get_queue(q);
651 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
653
Tejun Heo09ac46c2011-12-14 00:33:38 +0100654 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
Jens Axboed86e0e82011-05-27 07:44:43 +0200656EXPORT_SYMBOL(blk_get_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Jens Axboe165125e2007-07-24 09:28:11 +0200658static inline void blk_free_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Tejun Heof1f8cc92011-12-14 00:33:42 +0100660 if (rq->cmd_flags & REQ_ELVPRIV) {
Tejun Heocb98fc82005-10-28 08:29:39 +0200661 elv_put_request(q, rq);
Tejun Heof1f8cc92011-12-14 00:33:42 +0100662 if (rq->elv.icq)
Tejun Heo11a31222012-02-07 07:51:30 +0100663 put_io_context(rq->elv.icq->ioc);
Tejun Heof1f8cc92011-12-14 00:33:42 +0100664 }
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 mempool_free(rq, q->rq.rq_pool);
667}
668
Jens Axboe1ea25ec2006-07-18 22:24:11 +0200669static struct request *
Tejun Heof1f8cc92011-12-14 00:33:42 +0100670blk_alloc_request(struct request_queue *q, struct io_cq *icq,
671 unsigned int flags, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
674
675 if (!rq)
676 return NULL;
677
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200678 blk_rq_init(q, rq);
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200679
Jerome Marchand42dad762009-04-22 14:01:49 +0200680 rq->cmd_flags = flags | REQ_ALLOCED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Tejun Heof1f8cc92011-12-14 00:33:42 +0100682 if (flags & REQ_ELVPRIV) {
683 rq->elv.icq = icq;
684 if (unlikely(elv_set_request(q, rq, gfp_mask))) {
685 mempool_free(rq, q->rq.rq_pool);
686 return NULL;
687 }
688 /* @rq->elv.icq holds on to io_context until @rq is freed */
689 if (icq)
690 get_io_context(icq->ioc);
Tejun Heocb98fc82005-10-28 08:29:39 +0200691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Tejun Heocb98fc82005-10-28 08:29:39 +0200693 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
696/*
697 * ioc_batching returns true if the ioc is a valid batching request and
698 * should be given priority access to a request.
699 */
Jens Axboe165125e2007-07-24 09:28:11 +0200700static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
702 if (!ioc)
703 return 0;
704
705 /*
706 * Make sure the process is able to allocate at least 1 request
707 * even if the batch times out, otherwise we could theoretically
708 * lose wakeups.
709 */
710 return ioc->nr_batch_requests == q->nr_batching ||
711 (ioc->nr_batch_requests > 0
712 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
713}
714
715/*
716 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
717 * will cause the process to be a "batcher" on all queues in the system. This
718 * is the behaviour we want though - once it gets a wakeup it should be given
719 * a nice run.
720 */
Jens Axboe165125e2007-07-24 09:28:11 +0200721static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
723 if (!ioc || ioc_batching(q, ioc))
724 return;
725
726 ioc->nr_batch_requests = q->nr_batching;
727 ioc->last_waited = jiffies;
728}
729
Jens Axboe1faa16d2009-04-06 14:48:01 +0200730static void __freed_request(struct request_queue *q, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
732 struct request_list *rl = &q->rq;
733
Jens Axboe1faa16d2009-04-06 14:48:01 +0200734 if (rl->count[sync] < queue_congestion_off_threshold(q))
735 blk_clear_queue_congested(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Jens Axboe1faa16d2009-04-06 14:48:01 +0200737 if (rl->count[sync] + 1 <= q->nr_requests) {
738 if (waitqueue_active(&rl->wait[sync]))
739 wake_up(&rl->wait[sync]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Jens Axboe1faa16d2009-04-06 14:48:01 +0200741 blk_clear_queue_full(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
743}
744
745/*
746 * A request has just been released. Account for it, update the full and
747 * congestion status, wake up any waiters. Called under q->queue_lock.
748 */
Tejun Heo75eb6c32011-10-19 14:31:22 +0200749static void freed_request(struct request_queue *q, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
751 struct request_list *rl = &q->rq;
Tejun Heo75eb6c32011-10-19 14:31:22 +0200752 int sync = rw_is_sync(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Jens Axboe1faa16d2009-04-06 14:48:01 +0200754 rl->count[sync]--;
Tejun Heo75eb6c32011-10-19 14:31:22 +0200755 if (flags & REQ_ELVPRIV)
Tejun Heocb98fc82005-10-28 08:29:39 +0200756 rl->elvpriv--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Jens Axboe1faa16d2009-04-06 14:48:01 +0200758 __freed_request(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Jens Axboe1faa16d2009-04-06 14:48:01 +0200760 if (unlikely(rl->starved[sync ^ 1]))
761 __freed_request(q, sync ^ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764/*
Mike Snitzer9d5a4e92011-02-11 11:05:46 +0100765 * Determine if elevator data should be initialized when allocating the
766 * request associated with @bio.
767 */
768static bool blk_rq_should_init_elevator(struct bio *bio)
769{
770 if (!bio)
771 return true;
772
773 /*
774 * Flush requests do not use the elevator so skip initialization.
775 * This allows a request to share the flush and elevator data.
776 */
777 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA))
778 return false;
779
780 return true;
781}
782
Tejun Heoda8303c2011-10-19 14:33:05 +0200783/**
784 * get_request - get a free request
785 * @q: request_queue to allocate request from
786 * @rw_flags: RW and SYNC flags
787 * @bio: bio to allocate request for (can be %NULL)
788 * @gfp_mask: allocation mask
789 *
790 * Get a free request from @q. This function may fail under memory
791 * pressure or if @q is dead.
792 *
793 * Must be callled with @q->queue_lock held and,
794 * Returns %NULL on failure, with @q->queue_lock held.
795 * Returns !%NULL on success, with @q->queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 */
Jens Axboe165125e2007-07-24 09:28:11 +0200797static struct request *get_request(struct request_queue *q, int rw_flags,
Jens Axboe7749a8d2006-12-13 13:02:26 +0100798 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
800 struct request *rq = NULL;
801 struct request_list *rl = &q->rq;
Tejun Heof1f8cc92011-12-14 00:33:42 +0100802 struct elevator_type *et;
Tejun Heof2dbd762011-12-14 00:33:40 +0100803 struct io_context *ioc;
Tejun Heof1f8cc92011-12-14 00:33:42 +0100804 struct io_cq *icq = NULL;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200805 const bool is_sync = rw_is_sync(rw_flags) != 0;
Tejun Heof2dbd762011-12-14 00:33:40 +0100806 bool retried = false;
Tejun Heo75eb6c32011-10-19 14:31:22 +0200807 int may_queue;
Tejun Heof2dbd762011-12-14 00:33:40 +0100808retry:
Tejun Heof1f8cc92011-12-14 00:33:42 +0100809 et = q->elevator->type;
Tejun Heof2dbd762011-12-14 00:33:40 +0100810 ioc = current->io_context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Tejun Heo34f60552011-12-14 00:33:37 +0100812 if (unlikely(blk_queue_dead(q)))
Tejun Heoda8303c2011-10-19 14:33:05 +0200813 return NULL;
814
Jens Axboe7749a8d2006-12-13 13:02:26 +0100815 may_queue = elv_may_queue(q, rw_flags);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100816 if (may_queue == ELV_MQUEUE_NO)
817 goto rq_starved;
818
Jens Axboe1faa16d2009-04-06 14:48:01 +0200819 if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) {
820 if (rl->count[is_sync]+1 >= q->nr_requests) {
Tejun Heof2dbd762011-12-14 00:33:40 +0100821 /*
822 * We want ioc to record batching state. If it's
823 * not already there, creating a new one requires
824 * dropping queue_lock, which in turn requires
825 * retesting conditions to avoid queue hang.
826 */
827 if (!ioc && !retried) {
828 spin_unlock_irq(q->queue_lock);
829 create_io_context(current, gfp_mask, q->node);
830 spin_lock_irq(q->queue_lock);
831 retried = true;
832 goto retry;
833 }
834
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100835 /*
836 * The queue will fill after this allocation, so set
837 * it as full, and mark this process as "batching".
838 * This process will be allowed to complete a batch of
839 * requests, others will be blocked.
840 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200841 if (!blk_queue_full(q, is_sync)) {
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100842 ioc_set_batching(q, ioc);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200843 blk_set_queue_full(q, is_sync);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100844 } else {
845 if (may_queue != ELV_MQUEUE_MUST
846 && !ioc_batching(q, ioc)) {
847 /*
848 * The queue is full and the allocating
849 * process is not a "batcher", and not
850 * exempted by the IO scheduler
851 */
852 goto out;
853 }
854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
Jens Axboe1faa16d2009-04-06 14:48:01 +0200856 blk_set_queue_congested(q, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
858
Jens Axboe082cf692005-06-28 16:35:11 +0200859 /*
860 * Only allow batching queuers to allocate up to 50% over the defined
861 * limit of requests, otherwise we could have thousands of requests
862 * allocated with any setting of ->nr_requests
863 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200864 if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
Jens Axboe082cf692005-06-28 16:35:11 +0200865 goto out;
Hugh Dickinsfd782a42005-06-29 15:15:40 +0100866
Jens Axboe1faa16d2009-04-06 14:48:01 +0200867 rl->count[is_sync]++;
868 rl->starved[is_sync] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200869
Tejun Heof1f8cc92011-12-14 00:33:42 +0100870 /*
871 * Decide whether the new request will be managed by elevator. If
872 * so, mark @rw_flags and increment elvpriv. Non-zero elvpriv will
873 * prevent the current elevator from being destroyed until the new
874 * request is freed. This guarantees icq's won't be destroyed and
875 * makes creating new ones safe.
876 *
877 * Also, lookup icq while holding queue_lock. If it doesn't exist,
878 * it will be created after releasing queue_lock.
879 */
Tejun Heo75eb6c32011-10-19 14:31:22 +0200880 if (blk_rq_should_init_elevator(bio) &&
881 !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags)) {
882 rw_flags |= REQ_ELVPRIV;
883 rl->elvpriv++;
Tejun Heof1f8cc92011-12-14 00:33:42 +0100884 if (et->icq_cache && ioc)
885 icq = ioc_lookup_icq(ioc, q);
Mike Snitzer9d5a4e92011-02-11 11:05:46 +0100886 }
Tejun Heocb98fc82005-10-28 08:29:39 +0200887
Jens Axboef253b862010-10-24 22:06:02 +0200888 if (blk_queue_io_stat(q))
889 rw_flags |= REQ_IO_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 spin_unlock_irq(q->queue_lock);
891
Tejun Heof1f8cc92011-12-14 00:33:42 +0100892 /* create icq if missing */
Shaohua Li05c30b92012-01-19 09:20:10 +0100893 if ((rw_flags & REQ_ELVPRIV) && unlikely(et->icq_cache && !icq)) {
Tejun Heof1f8cc92011-12-14 00:33:42 +0100894 icq = ioc_create_icq(q, gfp_mask);
Shaohua Li05c30b92012-01-19 09:20:10 +0100895 if (!icq)
896 goto fail_icq;
897 }
Tejun Heof1f8cc92011-12-14 00:33:42 +0100898
Shaohua Li05c30b92012-01-19 09:20:10 +0100899 rq = blk_alloc_request(q, icq, rw_flags, gfp_mask);
Tejun Heof1f8cc92011-12-14 00:33:42 +0100900
Shaohua Li05c30b92012-01-19 09:20:10 +0100901fail_icq:
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100902 if (unlikely(!rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 /*
904 * Allocation failed presumably due to memory. Undo anything
905 * we might have messed up.
906 *
907 * Allocating task should really be put onto the front of the
908 * wait queue, but this is pretty rare.
909 */
910 spin_lock_irq(q->queue_lock);
Tejun Heo75eb6c32011-10-19 14:31:22 +0200911 freed_request(q, rw_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 /*
914 * in the very unlikely event that allocation failed and no
915 * requests for this direction was pending, mark us starved
916 * so that freeing of a request in the other direction will
917 * notice us. another possible fix would be to split the
918 * rq mempool into READ and WRITE
919 */
920rq_starved:
Jens Axboe1faa16d2009-04-06 14:48:01 +0200921 if (unlikely(rl->count[is_sync] == 0))
922 rl->starved[is_sync] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 goto out;
925 }
926
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100927 /*
928 * ioc may be NULL here, and ioc_batching will be false. That's
929 * OK, if the queue is under the request limit then requests need
930 * not count toward the nr_batch_requests limit. There will always
931 * be some limit enforced by BLK_BATCH_TIME.
932 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if (ioc_batching(q, ioc))
934 ioc->nr_batch_requests--;
Jens Axboe6728cb02008-01-31 13:03:55 +0100935
Jens Axboe1faa16d2009-04-06 14:48:01 +0200936 trace_block_getrq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return rq;
939}
940
Tejun Heoda8303c2011-10-19 14:33:05 +0200941/**
942 * get_request_wait - get a free request with retry
943 * @q: request_queue to allocate request from
944 * @rw_flags: RW and SYNC flags
945 * @bio: bio to allocate request for (can be %NULL)
Nick Piggind6344532005-06-28 20:45:14 -0700946 *
Tejun Heoda8303c2011-10-19 14:33:05 +0200947 * Get a free request from @q. This function keeps retrying under memory
948 * pressure and fails iff @q is dead.
949 *
950 * Must be callled with @q->queue_lock held and,
951 * Returns %NULL on failure, with @q->queue_lock held.
952 * Returns !%NULL on success, with @q->queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 */
Jens Axboe165125e2007-07-24 09:28:11 +0200954static struct request *get_request_wait(struct request_queue *q, int rw_flags,
Jens Axboe22e2c502005-06-27 10:55:12 +0200955 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
Jens Axboe1faa16d2009-04-06 14:48:01 +0200957 const bool is_sync = rw_is_sync(rw_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 struct request *rq;
959
Jens Axboe7749a8d2006-12-13 13:02:26 +0100960 rq = get_request(q, rw_flags, bio, GFP_NOIO);
Nick Piggin450991b2005-06-28 20:45:13 -0700961 while (!rq) {
962 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 struct request_list *rl = &q->rq;
964
Tejun Heo34f60552011-12-14 00:33:37 +0100965 if (unlikely(blk_queue_dead(q)))
Tejun Heoda8303c2011-10-19 14:33:05 +0200966 return NULL;
967
Jens Axboe1faa16d2009-04-06 14:48:01 +0200968 prepare_to_wait_exclusive(&rl->wait[is_sync], &wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 TASK_UNINTERRUPTIBLE);
970
Jens Axboe1faa16d2009-04-06 14:48:01 +0200971 trace_block_sleeprq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200973 spin_unlock_irq(q->queue_lock);
974 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200976 /*
977 * After sleeping, we become a "batching" process and
978 * will be able to allocate at least one request, and
979 * up to a big batch of them for a small period time.
980 * See ioc_batching, ioc_set_batching
981 */
Tejun Heof2dbd762011-12-14 00:33:40 +0100982 create_io_context(current, GFP_NOIO, q->node);
983 ioc_set_batching(q, current->io_context);
Jens Axboe2056a782006-03-23 20:00:26 +0100984
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200985 spin_lock_irq(q->queue_lock);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200986 finish_wait(&rl->wait[is_sync], &wait);
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200987
988 rq = get_request(q, rw_flags, bio, GFP_NOIO);
989 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 return rq;
992}
993
Jens Axboe165125e2007-07-24 09:28:11 +0200994struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
996 struct request *rq;
997
Nick Piggind6344532005-06-28 20:45:14 -0700998 spin_lock_irq(q->queue_lock);
Tejun Heoda8303c2011-10-19 14:33:05 +0200999 if (gfp_mask & __GFP_WAIT)
Jens Axboe22e2c502005-06-27 10:55:12 +02001000 rq = get_request_wait(q, rw, NULL);
Tejun Heoda8303c2011-10-19 14:33:05 +02001001 else
Jens Axboe22e2c502005-06-27 10:55:12 +02001002 rq = get_request(q, rw, NULL, gfp_mask);
Tejun Heoda8303c2011-10-19 14:33:05 +02001003 if (!rq)
1004 spin_unlock_irq(q->queue_lock);
Nick Piggind6344532005-06-28 20:45:14 -07001005 /* q->queue_lock is unlocked at this point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 return rq;
1008}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009EXPORT_SYMBOL(blk_get_request);
1010
1011/**
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001012 * blk_make_request - given a bio, allocate a corresponding struct request.
Randy Dunlap8ebf9752009-06-11 20:00:41 -07001013 * @q: target request queue
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001014 * @bio: The bio describing the memory mappings that will be submitted for IO.
1015 * It may be a chained-bio properly constructed by block/bio layer.
Randy Dunlap8ebf9752009-06-11 20:00:41 -07001016 * @gfp_mask: gfp flags to be used for memory allocation
Jens Axboedc72ef42006-07-20 14:54:05 +02001017 *
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001018 * blk_make_request is the parallel of generic_make_request for BLOCK_PC
1019 * type commands. Where the struct request needs to be farther initialized by
1020 * the caller. It is passed a &struct bio, which describes the memory info of
1021 * the I/O transfer.
1022 *
1023 * The caller of blk_make_request must make sure that bi_io_vec
1024 * are set to describe the memory buffers. That bio_data_dir() will return
1025 * the needed direction of the request. (And all bio's in the passed bio-chain
1026 * are properly set accordingly)
1027 *
1028 * If called under none-sleepable conditions, mapped bio buffers must not
1029 * need bouncing, by calling the appropriate masked or flagged allocator,
1030 * suitable for the target device. Otherwise the call to blk_queue_bounce will
1031 * BUG.
Jens Axboe53674ac2009-05-19 19:52:35 +02001032 *
1033 * WARNING: When allocating/cloning a bio-chain, careful consideration should be
1034 * given to how you allocate bios. In particular, you cannot use __GFP_WAIT for
1035 * anything but the first bio in the chain. Otherwise you risk waiting for IO
1036 * completion of a bio that hasn't been submitted yet, thus resulting in a
1037 * deadlock. Alternatively bios should be allocated using bio_kmalloc() instead
1038 * of bio_alloc(), as that avoids the mempool deadlock.
1039 * If possible a big IO should be split into smaller parts when allocation
1040 * fails. Partial allocation should not be an error, or you risk a live-lock.
Jens Axboedc72ef42006-07-20 14:54:05 +02001041 */
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001042struct request *blk_make_request(struct request_queue *q, struct bio *bio,
1043 gfp_t gfp_mask)
Jens Axboedc72ef42006-07-20 14:54:05 +02001044{
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001045 struct request *rq = blk_get_request(q, bio_data_dir(bio), gfp_mask);
1046
1047 if (unlikely(!rq))
1048 return ERR_PTR(-ENOMEM);
1049
Jens Axboea6c6cb82014-06-06 07:57:37 -06001050 blk_rq_set_block_pc(rq);
1051
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001052 for_each_bio(bio) {
1053 struct bio *bounce_bio = bio;
1054 int ret;
1055
1056 blk_queue_bounce(q, &bounce_bio);
1057 ret = blk_rq_append_bio(q, rq, bounce_bio);
1058 if (unlikely(ret)) {
1059 blk_put_request(rq);
1060 return ERR_PTR(ret);
1061 }
1062 }
1063
1064 return rq;
Jens Axboedc72ef42006-07-20 14:54:05 +02001065}
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001066EXPORT_SYMBOL(blk_make_request);
Jens Axboedc72ef42006-07-20 14:54:05 +02001067
1068/**
Jens Axboea6c6cb82014-06-06 07:57:37 -06001069 * blk_rq_set_block_pc - initialize a requeest to type BLOCK_PC
1070 * @rq: request to be initialized
1071 *
1072 */
1073void blk_rq_set_block_pc(struct request *rq)
1074{
1075 rq->cmd_type = REQ_TYPE_BLOCK_PC;
1076 rq->__data_len = 0;
1077 rq->__sector = (sector_t) -1;
1078 rq->bio = rq->biotail = NULL;
1079 memset(rq->__cmd, 0, sizeof(rq->__cmd));
1080 rq->cmd = rq->__cmd;
1081}
1082EXPORT_SYMBOL(blk_rq_set_block_pc);
1083
1084/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 * blk_requeue_request - put a request back on queue
1086 * @q: request queue where request should be inserted
1087 * @rq: request to be inserted
1088 *
1089 * Description:
1090 * Drivers often keep queueing requests until the hardware cannot accept
1091 * more, when that condition happens we need to put the request back
1092 * on the queue. Must be called with queue lock held.
1093 */
Jens Axboe165125e2007-07-24 09:28:11 +02001094void blk_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
Jens Axboe242f9dc2008-09-14 05:55:09 -07001096 blk_delete_timer(rq);
1097 blk_clear_rq_complete(rq);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001098 trace_block_rq_requeue(q, rq);
Jens Axboe2056a782006-03-23 20:00:26 +01001099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if (blk_rq_tagged(rq))
1101 blk_queue_end_tag(q, rq);
1102
James Bottomleyba396a62009-05-27 14:17:08 +02001103 BUG_ON(blk_queued_rq(rq));
1104
Tatyana Brokhmanfd799022013-04-11 14:57:15 +03001105 if (rq->cmd_flags & REQ_URGENT) {
1106 /*
1107 * It's not compliant with the design to re-insert
1108 * urgent requests. We want to be able to track this
1109 * down.
1110 */
Tatyana Brokhman7f9b9bf2013-05-16 14:36:58 +03001111 pr_debug("%s(): requeueing an URGENT request", __func__);
Tatyana Brokhmanfd799022013-04-11 14:57:15 +03001112 WARN_ON(!q->dispatched_urgent);
1113 q->dispatched_urgent = false;
1114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 elv_requeue_request(q, rq);
1116}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117EXPORT_SYMBOL(blk_requeue_request);
1118
Tatyana Brokhman57d80192012-12-04 15:54:43 +02001119/**
1120 * blk_reinsert_request() - Insert a request back to the scheduler
1121 * @q: request queue
1122 * @rq: request to be inserted
1123 *
1124 * This function inserts the request back to the scheduler as if
1125 * it was never dispatched.
1126 *
1127 * Return: 0 on success, error code on fail
1128 */
1129int blk_reinsert_request(struct request_queue *q, struct request *rq)
1130{
1131 if (unlikely(!rq) || unlikely(!q))
1132 return -EIO;
1133
1134 blk_delete_timer(rq);
1135 blk_clear_rq_complete(rq);
1136 trace_block_rq_requeue(q, rq);
1137
1138 if (blk_rq_tagged(rq))
1139 blk_queue_end_tag(q, rq);
1140
1141 BUG_ON(blk_queued_rq(rq));
Tatyana Brokhmanfd799022013-04-11 14:57:15 +03001142 if (rq->cmd_flags & REQ_URGENT) {
1143 /*
1144 * It's not compliant with the design to re-insert
1145 * urgent requests. We want to be able to track this
1146 * down.
1147 */
Tatyana Brokhman7f9b9bf2013-05-16 14:36:58 +03001148 pr_debug("%s(): reinserting an URGENT request", __func__);
Tatyana Brokhmanfd799022013-04-11 14:57:15 +03001149 WARN_ON(!q->dispatched_urgent);
1150 q->dispatched_urgent = false;
1151 }
Tatyana Brokhman57d80192012-12-04 15:54:43 +02001152
1153 return elv_reinsert_request(q, rq);
1154}
1155EXPORT_SYMBOL(blk_reinsert_request);
1156
1157/**
1158 * blk_reinsert_req_sup() - check whether the scheduler supports
1159 * reinsertion of requests
1160 * @q: request queue
1161 *
1162 * Returns true if the current scheduler supports reinserting
1163 * request. False otherwise
1164 */
1165bool blk_reinsert_req_sup(struct request_queue *q)
1166{
1167 if (unlikely(!q))
1168 return false;
1169 return q->elevator->type->ops.elevator_reinsert_req_fn ? true : false;
1170}
1171EXPORT_SYMBOL(blk_reinsert_req_sup);
1172
Jens Axboe73c10102011-03-08 13:19:51 +01001173static void add_acct_request(struct request_queue *q, struct request *rq,
1174 int where)
1175{
1176 drive_stat_acct(rq, 1);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001177 __elv_add_request(q, rq, where);
Jens Axboe73c10102011-03-08 13:19:51 +01001178}
1179
Tejun Heo074a7ac2008-08-25 19:56:14 +09001180static void part_round_stats_single(int cpu, struct hd_struct *part,
1181 unsigned long now)
1182{
1183 if (now == part->stamp)
1184 return;
1185
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001186 if (part_in_flight(part)) {
Tejun Heo074a7ac2008-08-25 19:56:14 +09001187 __part_stat_add(cpu, part, time_in_queue,
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001188 part_in_flight(part) * (now - part->stamp));
Tejun Heo074a7ac2008-08-25 19:56:14 +09001189 __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
1190 }
1191 part->stamp = now;
1192}
1193
1194/**
Randy Dunlap496aa8a2008-10-16 07:46:23 +02001195 * part_round_stats() - Round off the performance stats on a struct disk_stats.
1196 * @cpu: cpu number for stats access
1197 * @part: target partition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 *
1199 * The average IO queue length and utilisation statistics are maintained
1200 * by observing the current state of the queue length and the amount of
1201 * time it has been in this state for.
1202 *
1203 * Normally, that accounting is done on IO completion, but that can result
1204 * in more than a second's worth of IO being accounted for within any one
1205 * second, leading to >100% utilisation. To deal with that, we call this
1206 * function to do a round-off before returning the results when reading
1207 * /proc/diskstats. This accounts immediately for all queue usage up to
1208 * the current jiffies and restarts the counters again.
1209 */
Tejun Heoc9959052008-08-25 19:47:21 +09001210void part_round_stats(int cpu, struct hd_struct *part)
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001211{
1212 unsigned long now = jiffies;
1213
Tejun Heo074a7ac2008-08-25 19:56:14 +09001214 if (part->partno)
1215 part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
1216 part_round_stats_single(cpu, part, now);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001217}
Tejun Heo074a7ac2008-08-25 19:56:14 +09001218EXPORT_SYMBOL_GPL(part_round_stats);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001219
Lin Ming0fb59e52013-03-23 11:42:27 +08001220#ifdef CONFIG_PM_RUNTIME
1221static void blk_pm_put_request(struct request *rq)
1222{
1223 if (rq->q->dev && !(rq->cmd_flags & REQ_PM) && !--rq->q->nr_pending)
1224 pm_runtime_mark_last_busy(rq->q->dev);
1225}
1226#else
1227static inline void blk_pm_put_request(struct request *rq) {}
1228#endif
1229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230/*
1231 * queue lock must be held
1232 */
Jens Axboe165125e2007-07-24 09:28:11 +02001233void __blk_put_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if (unlikely(!q))
1236 return;
1237 if (unlikely(--req->ref_count))
1238 return;
1239
Lin Ming0fb59e52013-03-23 11:42:27 +08001240 blk_pm_put_request(req);
1241
Tejun Heo8922e162005-10-20 16:23:44 +02001242 elv_completed_request(q, req);
1243
Dinesh K Garg93b3cb02014-01-13 13:55:00 -08001244 /* this is a bio leak if the bio is not tagged with BIO_DONTFREE */
1245 WARN_ON(req->bio && !bio_flagged(req->bio, BIO_DONTFREE));
1246
Boaz Harrosh1cd96c22009-03-24 12:35:07 +01001247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 /*
1249 * Request may not have originated from ll_rw_blk. if not,
1250 * it didn't come out of our reserved rq pools
1251 */
Jens Axboe49171e52006-08-10 08:59:11 +02001252 if (req->cmd_flags & REQ_ALLOCED) {
Tejun Heo75eb6c32011-10-19 14:31:22 +02001253 unsigned int flags = req->cmd_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 BUG_ON(!list_empty(&req->queuelist));
Jens Axboe98170642006-07-28 09:23:08 +02001256 BUG_ON(!hlist_unhashed(&req->hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 blk_free_request(q, req);
Tejun Heo75eb6c32011-10-19 14:31:22 +02001259 freed_request(q, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 }
1261}
Mike Christie6e39b692005-11-11 05:30:24 -06001262EXPORT_SYMBOL_GPL(__blk_put_request);
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264void blk_put_request(struct request *req)
1265{
Tejun Heo8922e162005-10-20 16:23:44 +02001266 unsigned long flags;
Jens Axboe165125e2007-07-24 09:28:11 +02001267 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
FUJITA Tomonori52a93ba2008-07-15 21:21:45 +02001269 spin_lock_irqsave(q->queue_lock, flags);
1270 __blk_put_request(q, req);
1271 spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273EXPORT_SYMBOL(blk_put_request);
1274
Christoph Hellwig66ac0282010-06-18 16:59:42 +02001275/**
1276 * blk_add_request_payload - add a payload to a request
1277 * @rq: request to update
1278 * @page: page backing the payload
1279 * @len: length of the payload.
1280 *
1281 * This allows to later add a payload to an already submitted request by
1282 * a block driver. The driver needs to take care of freeing the payload
1283 * itself.
1284 *
1285 * Note that this is a quite horrible hack and nothing but handling of
1286 * discard requests should ever use it.
1287 */
1288void blk_add_request_payload(struct request *rq, struct page *page,
1289 unsigned int len)
1290{
1291 struct bio *bio = rq->bio;
1292
1293 bio->bi_io_vec->bv_page = page;
1294 bio->bi_io_vec->bv_offset = 0;
1295 bio->bi_io_vec->bv_len = len;
1296
1297 bio->bi_size = len;
1298 bio->bi_vcnt = 1;
1299 bio->bi_phys_segments = 1;
1300
1301 rq->__data_len = rq->resid_len = len;
1302 rq->nr_phys_segments = 1;
1303 rq->buffer = bio_data(bio);
1304}
1305EXPORT_SYMBOL_GPL(blk_add_request_payload);
1306
Jens Axboe73c10102011-03-08 13:19:51 +01001307static bool bio_attempt_back_merge(struct request_queue *q, struct request *req,
1308 struct bio *bio)
1309{
1310 const int ff = bio->bi_rw & REQ_FAILFAST_MASK;
1311
Jens Axboe73c10102011-03-08 13:19:51 +01001312 if (!ll_back_merge_fn(q, req, bio))
1313 return false;
1314
1315 trace_block_bio_backmerge(q, bio);
1316
1317 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1318 blk_rq_set_mixed_merge(req);
1319
1320 req->biotail->bi_next = bio;
1321 req->biotail = bio;
1322 req->__data_len += bio->bi_size;
1323 req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
1324
1325 drive_stat_acct(req, 0);
1326 return true;
1327}
1328
1329static bool bio_attempt_front_merge(struct request_queue *q,
1330 struct request *req, struct bio *bio)
1331{
1332 const int ff = bio->bi_rw & REQ_FAILFAST_MASK;
Jens Axboe73c10102011-03-08 13:19:51 +01001333
Jens Axboe73c10102011-03-08 13:19:51 +01001334 if (!ll_front_merge_fn(q, req, bio))
1335 return false;
1336
1337 trace_block_bio_frontmerge(q, bio);
1338
1339 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1340 blk_rq_set_mixed_merge(req);
1341
Jens Axboe73c10102011-03-08 13:19:51 +01001342 bio->bi_next = req->bio;
1343 req->bio = bio;
1344
1345 /*
1346 * may not be valid. if the low level driver said
1347 * it didn't need a bounce buffer then it better
1348 * not touch req->buffer either...
1349 */
1350 req->buffer = bio_data(bio);
1351 req->__sector = bio->bi_sector;
1352 req->__data_len += bio->bi_size;
1353 req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
1354
1355 drive_stat_acct(req, 0);
1356 return true;
1357}
1358
Tejun Heobd87b582011-10-19 14:33:08 +02001359/**
1360 * attempt_plug_merge - try to merge with %current's plugged list
1361 * @q: request_queue new bio is being queued at
1362 * @bio: new bio being queued
1363 * @request_count: out parameter for number of traversed plugged requests
1364 *
1365 * Determine whether @bio being queued on @q can be merged with a request
1366 * on %current's plugged list. Returns %true if merge was successful,
1367 * otherwise %false.
1368 *
Tejun Heo07c2bd32012-02-08 09:19:42 +01001369 * Plugging coalesces IOs from the same issuer for the same purpose without
1370 * going through @q->queue_lock. As such it's more of an issuing mechanism
1371 * than scheduling, and the request, while may have elvpriv data, is not
1372 * added on the elevator at this point. In addition, we don't have
1373 * reliable access to the elevator outside queue lock. Only check basic
1374 * merging parameters without querying the elevator.
Jens Axboe73c10102011-03-08 13:19:51 +01001375 */
Tejun Heobd87b582011-10-19 14:33:08 +02001376static bool attempt_plug_merge(struct request_queue *q, struct bio *bio,
1377 unsigned int *request_count)
Jens Axboe73c10102011-03-08 13:19:51 +01001378{
1379 struct blk_plug *plug;
1380 struct request *rq;
1381 bool ret = false;
1382
Tejun Heobd87b582011-10-19 14:33:08 +02001383 plug = current->plug;
Jens Axboe73c10102011-03-08 13:19:51 +01001384 if (!plug)
1385 goto out;
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001386 *request_count = 0;
Jens Axboe73c10102011-03-08 13:19:51 +01001387
1388 list_for_each_entry_reverse(rq, &plug->list, queuelist) {
1389 int el_ret;
1390
Shaohua Li1b2e19f2012-04-06 11:37:47 -06001391 if (rq->q == q)
1392 (*request_count)++;
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001393
Tejun Heo07c2bd32012-02-08 09:19:42 +01001394 if (rq->q != q || !blk_rq_merge_ok(rq, bio))
Jens Axboe73c10102011-03-08 13:19:51 +01001395 continue;
1396
Tejun Heo050c8ea2012-02-08 09:19:38 +01001397 el_ret = blk_try_merge(rq, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001398 if (el_ret == ELEVATOR_BACK_MERGE) {
1399 ret = bio_attempt_back_merge(q, rq, bio);
1400 if (ret)
1401 break;
1402 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
1403 ret = bio_attempt_front_merge(q, rq, bio);
1404 if (ret)
1405 break;
1406 }
1407 }
1408out:
1409 return ret;
1410}
1411
Jens Axboe86db1e22008-01-29 14:53:40 +01001412void init_request_from_bio(struct request *req, struct bio *bio)
Tejun Heo52d9e672006-01-06 09:49:58 +01001413{
Jens Axboe4aff5e22006-08-10 08:44:47 +02001414 req->cmd_type = REQ_TYPE_FS;
Tejun Heo52d9e672006-01-06 09:49:58 +01001415
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001416 req->cmd_flags |= bio->bi_rw & REQ_COMMON_MASK;
1417 if (bio->bi_rw & REQ_RAHEAD)
Tejun Heoa82afdf2009-07-03 17:48:16 +09001418 req->cmd_flags |= REQ_FAILFAST_MASK;
Jens Axboeb31dc662006-06-13 08:26:10 +02001419
Tejun Heo52d9e672006-01-06 09:49:58 +01001420 req->errors = 0;
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001421 req->__sector = bio->bi_sector;
Tejun Heo52d9e672006-01-06 09:49:58 +01001422 req->ioprio = bio_prio(bio);
NeilBrownbc1c56f2007-08-16 13:31:30 +02001423 blk_rq_bio_prep(req->q, req, bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001424}
Maya Erez60181552012-06-27 11:25:26 +03001425EXPORT_SYMBOL(init_request_from_bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001426
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001427void blk_queue_bio(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
Jiri Slaby5e00d1b2010-08-12 14:31:06 +02001429 const bool sync = !!(bio->bi_rw & REQ_SYNC);
Jens Axboe73c10102011-03-08 13:19:51 +01001430 struct blk_plug *plug;
1431 int el_ret, rw_flags, where = ELEVATOR_INSERT_SORT;
1432 struct request *req;
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001433 unsigned int request_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 /*
1436 * low level driver can indicate that it wants pages above a
1437 * certain limit bounced to low memory (ie for highmem, or even
1438 * ISA dma in theory)
1439 */
1440 blk_queue_bounce(q, &bio);
1441
Tejun Heo4fed9472010-09-03 11:56:17 +02001442 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) {
Jens Axboe73c10102011-03-08 13:19:51 +01001443 spin_lock_irq(q->queue_lock);
Tejun Heoae1b1532011-01-25 12:43:54 +01001444 where = ELEVATOR_INSERT_FLUSH;
Tejun Heo28e7d182010-09-03 11:56:16 +02001445 goto get_rq;
1446 }
1447
Jens Axboe73c10102011-03-08 13:19:51 +01001448 /*
1449 * Check if we can merge with the plugged list before grabbing
1450 * any locks.
1451 */
Tejun Heobd87b582011-10-19 14:33:08 +02001452 if (attempt_plug_merge(q, bio, &request_count))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001453 return;
Jens Axboe73c10102011-03-08 13:19:51 +01001454
1455 spin_lock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 el_ret = elv_merge(q, &req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001458 if (el_ret == ELEVATOR_BACK_MERGE) {
Jens Axboe73c10102011-03-08 13:19:51 +01001459 if (bio_attempt_back_merge(q, req, bio)) {
Tejun Heo07c2bd32012-02-08 09:19:42 +01001460 elv_bio_merged(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001461 if (!attempt_back_merge(q, req))
1462 elv_merged_request(q, req, el_ret);
1463 goto out_unlock;
Tejun Heo80a761f2009-07-03 17:48:17 +09001464 }
Jens Axboe73c10102011-03-08 13:19:51 +01001465 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
Jens Axboe73c10102011-03-08 13:19:51 +01001466 if (bio_attempt_front_merge(q, req, bio)) {
Tejun Heo07c2bd32012-02-08 09:19:42 +01001467 elv_bio_merged(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001468 if (!attempt_front_merge(q, req))
1469 elv_merged_request(q, req, el_ret);
1470 goto out_unlock;
1471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 }
1473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474get_rq:
Nick Piggin450991b2005-06-28 20:45:13 -07001475 /*
Jens Axboe7749a8d2006-12-13 13:02:26 +01001476 * This sync check and mask will be re-done in init_request_from_bio(),
1477 * but we need to set it earlier to expose the sync flag to the
1478 * rq allocator and io schedulers.
1479 */
1480 rw_flags = bio_data_dir(bio);
1481 if (sync)
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001482 rw_flags |= REQ_SYNC;
Jens Axboe7749a8d2006-12-13 13:02:26 +01001483
1484 /*
Nick Piggin450991b2005-06-28 20:45:13 -07001485 * Grab a free request. This is might sleep but can not fail.
Nick Piggind6344532005-06-28 20:45:14 -07001486 * Returns with the queue unlocked.
Nick Piggin450991b2005-06-28 20:45:13 -07001487 */
Jens Axboe7749a8d2006-12-13 13:02:26 +01001488 req = get_request_wait(q, rw_flags, bio);
Tejun Heoda8303c2011-10-19 14:33:05 +02001489 if (unlikely(!req)) {
1490 bio_endio(bio, -ENODEV); /* @q is dead */
1491 goto out_unlock;
1492 }
Nick Piggind6344532005-06-28 20:45:14 -07001493
Nick Piggin450991b2005-06-28 20:45:13 -07001494 /*
1495 * After dropping the lock and possibly sleeping here, our request
1496 * may now be mergeable after it had proven unmergeable (above).
1497 * We don't worry about that case for efficiency. It won't happen
1498 * often, and the elevators are able to handle it.
1499 */
Tejun Heo52d9e672006-01-06 09:49:58 +01001500 init_request_from_bio(req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Tao Ma9562ad92011-10-24 16:11:30 +02001502 if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags))
Jens Axboe11ccf112011-07-26 15:01:15 +02001503 req->cpu = raw_smp_processor_id();
Tejun Heodd831002010-09-03 11:56:16 +02001504
Jens Axboe73c10102011-03-08 13:19:51 +01001505 plug = current->plug;
Jens Axboe721a9602011-03-09 11:56:30 +01001506 if (plug) {
Jens Axboedc6d36c2011-04-12 10:28:28 +02001507 /*
1508 * If this is the first request added after a plug, fire
1509 * of a plug trace. If others have been added before, check
1510 * if we have multiple devices in this plug. If so, make a
1511 * note to sort the list before dispatch.
1512 */
1513 if (list_empty(&plug->list))
1514 trace_block_plug(q);
Shaohua Li3540d5e2011-11-16 09:21:50 +01001515 else {
1516 if (!plug->should_sort) {
1517 struct request *__rq;
Jens Axboe73c10102011-03-08 13:19:51 +01001518
Shaohua Li3540d5e2011-11-16 09:21:50 +01001519 __rq = list_entry_rq(plug->list.prev);
1520 if (__rq->q != q)
1521 plug->should_sort = 1;
1522 }
Shaohua Li019ceb72011-11-16 09:21:50 +01001523 if (request_count >= BLK_MAX_REQUEST_COUNT) {
Shaohua Li3540d5e2011-11-16 09:21:50 +01001524 blk_flush_plug_list(plug, false);
Shaohua Li019ceb72011-11-16 09:21:50 +01001525 trace_block_plug(q);
1526 }
Jens Axboe73c10102011-03-08 13:19:51 +01001527 }
Shaohua Lia6327162011-08-24 16:04:32 +02001528 list_add_tail(&req->queuelist, &plug->list);
1529 drive_stat_acct(req, 1);
Jens Axboe73c10102011-03-08 13:19:51 +01001530 } else {
1531 spin_lock_irq(q->queue_lock);
1532 add_acct_request(q, req, where);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02001533 __blk_run_queue(q);
Jens Axboe73c10102011-03-08 13:19:51 +01001534out_unlock:
1535 spin_unlock_irq(q->queue_lock);
1536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537}
Jens Axboec20e8de2011-09-12 12:03:37 +02001538EXPORT_SYMBOL_GPL(blk_queue_bio); /* for device mapper only */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
1540/*
1541 * If bio->bi_dev is a partition, remap the location
1542 */
1543static inline void blk_partition_remap(struct bio *bio)
1544{
1545 struct block_device *bdev = bio->bi_bdev;
1546
Jens Axboebf2de6f2007-09-27 13:01:25 +02001547 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 struct hd_struct *p = bdev->bd_part;
Jens Axboea3623572005-11-01 09:26:16 +01001549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 bio->bi_sector += p->start_sect;
1551 bio->bi_bdev = bdev->bd_contains;
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001552
Mike Snitzerd07335e2010-11-16 12:52:38 +01001553 trace_block_bio_remap(bdev_get_queue(bio->bi_bdev), bio,
1554 bdev->bd_dev,
1555 bio->bi_sector - p->start_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 }
1557}
1558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559static void handle_bad_sector(struct bio *bio)
1560{
1561 char b[BDEVNAME_SIZE];
1562
1563 printk(KERN_INFO "attempt to access beyond end of device\n");
1564 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1565 bdevname(bio->bi_bdev, b),
1566 bio->bi_rw,
1567 (unsigned long long)bio->bi_sector + bio_sectors(bio),
Mike Snitzer77304d22010-11-08 14:39:12 +01001568 (long long)(i_size_read(bio->bi_bdev->bd_inode) >> 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
1570 set_bit(BIO_EOF, &bio->bi_flags);
1571}
1572
Akinobu Mitac17bb492006-12-08 02:39:46 -08001573#ifdef CONFIG_FAIL_MAKE_REQUEST
1574
1575static DECLARE_FAULT_ATTR(fail_make_request);
1576
1577static int __init setup_fail_make_request(char *str)
1578{
1579 return setup_fault_attr(&fail_make_request, str);
1580}
1581__setup("fail_make_request=", setup_fail_make_request);
1582
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001583static bool should_fail_request(struct hd_struct *part, unsigned int bytes)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001584{
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001585 return part->make_it_fail && should_fail(&fail_make_request, bytes);
Akinobu Mitac17bb492006-12-08 02:39:46 -08001586}
1587
1588static int __init fail_make_request_debugfs(void)
1589{
Akinobu Mitadd48c082011-08-03 16:21:01 -07001590 struct dentry *dir = fault_create_debugfs_attr("fail_make_request",
1591 NULL, &fail_make_request);
1592
1593 return IS_ERR(dir) ? PTR_ERR(dir) : 0;
Akinobu Mitac17bb492006-12-08 02:39:46 -08001594}
1595
1596late_initcall(fail_make_request_debugfs);
1597
1598#else /* CONFIG_FAIL_MAKE_REQUEST */
1599
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001600static inline bool should_fail_request(struct hd_struct *part,
1601 unsigned int bytes)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001602{
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001603 return false;
Akinobu Mitac17bb492006-12-08 02:39:46 -08001604}
1605
1606#endif /* CONFIG_FAIL_MAKE_REQUEST */
1607
Jens Axboec07e2b42007-07-18 13:27:58 +02001608/*
1609 * Check whether this bio extends beyond the end of the device.
1610 */
1611static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1612{
1613 sector_t maxsector;
1614
1615 if (!nr_sectors)
1616 return 0;
1617
1618 /* Test device or partition size, when known. */
Mike Snitzer77304d22010-11-08 14:39:12 +01001619 maxsector = i_size_read(bio->bi_bdev->bd_inode) >> 9;
Jens Axboec07e2b42007-07-18 13:27:58 +02001620 if (maxsector) {
1621 sector_t sector = bio->bi_sector;
1622
1623 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1624 /*
1625 * This may well happen - the kernel calls bread()
1626 * without checking the size of the device, e.g., when
1627 * mounting a device.
1628 */
1629 handle_bad_sector(bio);
1630 return 1;
1631 }
1632 }
1633
1634 return 0;
1635}
1636
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001637static noinline_for_stack bool
1638generic_make_request_checks(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639{
Jens Axboe165125e2007-07-24 09:28:11 +02001640 struct request_queue *q;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001641 int nr_sectors = bio_sectors(bio);
Jens Axboe51fd77b2007-11-02 08:49:08 +01001642 int err = -EIO;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001643 char b[BDEVNAME_SIZE];
1644 struct hd_struct *part;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Jens Axboec07e2b42007-07-18 13:27:58 +02001648 if (bio_check_eod(bio, nr_sectors))
1649 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001651 q = bdev_get_queue(bio->bi_bdev);
1652 if (unlikely(!q)) {
1653 printk(KERN_ERR
1654 "generic_make_request: Trying to access "
1655 "nonexistent block-device %s (%Lu)\n",
1656 bdevname(bio->bi_bdev, b),
1657 (long long) bio->bi_sector);
1658 goto end_io;
1659 }
1660
Steve Mucklef132c6c2012-06-06 18:30:57 -07001661 if (unlikely(!(bio->bi_rw & (REQ_DISCARD | REQ_SANITIZE)) &&
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001662 nr_sectors > queue_max_hw_sectors(q))) {
1663 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
1664 bdevname(bio->bi_bdev, b),
1665 bio_sectors(bio),
1666 queue_max_hw_sectors(q));
1667 goto end_io;
1668 }
1669
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001670 part = bio->bi_bdev->bd_part;
1671 if (should_fail_request(part, bio->bi_size) ||
1672 should_fail_request(&part_to_disk(part)->part0,
1673 bio->bi_size))
1674 goto end_io;
1675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 /*
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001677 * If this device has partitions, remap block n
1678 * of partition p to block n+start(p) of the disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001680 blk_partition_remap(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001682 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio))
1683 goto end_io;
1684
1685 if (bio_check_eod(bio, nr_sectors))
1686 goto end_io;
1687
1688 /*
1689 * Filter flush bio's early so that make_request based
1690 * drivers without flush support don't have to worry
1691 * about them.
1692 */
1693 if ((bio->bi_rw & (REQ_FLUSH | REQ_FUA)) && !q->flush_flags) {
1694 bio->bi_rw &= ~(REQ_FLUSH | REQ_FUA);
1695 if (!nr_sectors) {
1696 err = 0;
Tejun Heoa7384672008-11-28 13:32:03 +09001697 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 }
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001701 if ((bio->bi_rw & REQ_DISCARD) &&
1702 (!blk_queue_discard(q) ||
1703 ((bio->bi_rw & REQ_SECURE) &&
1704 !blk_queue_secdiscard(q)))) {
1705 err = -EOPNOTSUPP;
1706 goto end_io;
1707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Steve Mucklef132c6c2012-06-06 18:30:57 -07001709 if ((bio->bi_rw & REQ_SANITIZE) &&
1710 (!blk_queue_sanitize(q))) {
1711 pr_info("%s - got a SANITIZE request but the queue "
1712 "doesn't support sanitize requests", __func__);
1713 err = -EOPNOTSUPP;
1714 goto end_io;
1715 }
1716
Tejun Heobc16a4f2011-10-19 14:33:01 +02001717 if (blk_throtl_bio(q, bio))
1718 return false; /* throttled, will be resubmitted later */
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001719
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001720 trace_block_bio_queue(q, bio);
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001721 return true;
Tejun Heoa7384672008-11-28 13:32:03 +09001722
1723end_io:
1724 bio_endio(bio, err);
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001725 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726}
1727
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001728/**
1729 * generic_make_request - hand a buffer to its device driver for I/O
1730 * @bio: The bio describing the location in memory and on the device.
1731 *
1732 * generic_make_request() is used to make I/O requests of block
1733 * devices. It is passed a &struct bio, which describes the I/O that needs
1734 * to be done.
1735 *
1736 * generic_make_request() does not return any status. The
1737 * success/failure status of the request, along with notification of
1738 * completion, is delivered asynchronously through the bio->bi_end_io
1739 * function described (one day) else where.
1740 *
1741 * The caller of generic_make_request must make sure that bi_io_vec
1742 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1743 * set to describe the device address, and the
1744 * bi_end_io and optionally bi_private are set to describe how
1745 * completion notification should be signaled.
1746 *
1747 * generic_make_request and the drivers it calls may use bi_next if this
1748 * bio happens to be merged with someone else, and may resubmit the bio to
1749 * a lower device by calling into generic_make_request recursively, which
1750 * means the bio should NOT be touched after the call to ->make_request_fn.
Neil Brownd89d8792007-05-01 09:53:42 +02001751 */
1752void generic_make_request(struct bio *bio)
1753{
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001754 struct bio_list bio_list_on_stack;
1755
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001756 if (!generic_make_request_checks(bio))
1757 return;
1758
1759 /*
1760 * We only want one ->make_request_fn to be active at a time, else
1761 * stack usage with stacked devices could be a problem. So use
1762 * current->bio_list to keep a list of requests submited by a
1763 * make_request_fn function. current->bio_list is also used as a
1764 * flag to say if generic_make_request is currently active in this
1765 * task or not. If it is NULL, then no make_request is active. If
1766 * it is non-NULL, then a make_request is active, and new requests
1767 * should be added at the tail
1768 */
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001769 if (current->bio_list) {
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001770 bio_list_add(current->bio_list, bio);
Neil Brownd89d8792007-05-01 09:53:42 +02001771 return;
1772 }
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001773
Neil Brownd89d8792007-05-01 09:53:42 +02001774 /* following loop may be a bit non-obvious, and so deserves some
1775 * explanation.
1776 * Before entering the loop, bio->bi_next is NULL (as all callers
1777 * ensure that) so we have a list with a single bio.
1778 * We pretend that we have just taken it off a longer list, so
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001779 * we assign bio_list to a pointer to the bio_list_on_stack,
1780 * thus initialising the bio_list of new bios to be
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001781 * added. ->make_request() may indeed add some more bios
Neil Brownd89d8792007-05-01 09:53:42 +02001782 * through a recursive call to generic_make_request. If it
1783 * did, we find a non-NULL value in bio_list and re-enter the loop
1784 * from the top. In this case we really did just take the bio
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001785 * of the top of the list (no pretending) and so remove it from
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001786 * bio_list, and call into ->make_request() again.
Neil Brownd89d8792007-05-01 09:53:42 +02001787 */
1788 BUG_ON(bio->bi_next);
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001789 bio_list_init(&bio_list_on_stack);
1790 current->bio_list = &bio_list_on_stack;
Neil Brownd89d8792007-05-01 09:53:42 +02001791 do {
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001792 struct request_queue *q = bdev_get_queue(bio->bi_bdev);
1793
1794 q->make_request_fn(q, bio);
1795
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001796 bio = bio_list_pop(current->bio_list);
Neil Brownd89d8792007-05-01 09:53:42 +02001797 } while (bio);
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001798 current->bio_list = NULL; /* deactivate */
Neil Brownd89d8792007-05-01 09:53:42 +02001799}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800EXPORT_SYMBOL(generic_make_request);
1801
1802/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001803 * submit_bio - submit a bio to the block device layer for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1805 * @bio: The &struct bio which describes the I/O
1806 *
1807 * submit_bio() is very similar in purpose to generic_make_request(), and
1808 * uses that function to do most of the work. Both are fairly rough
Randy Dunlap710027a2008-08-19 20:13:11 +02001809 * interfaces; @bio must be presetup and ready for I/O.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 *
1811 */
1812void submit_bio(int rw, struct bio *bio)
1813{
1814 int count = bio_sectors(bio);
1815
Jens Axboe22e2c502005-06-27 10:55:12 +02001816 bio->bi_rw |= rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Jens Axboebf2de6f2007-09-27 13:01:25 +02001818 /*
1819 * If it's a regular read/write or a barrier with data attached,
1820 * go through the normal accounting stuff before submission.
1821 */
Maya Erez73937f52012-05-24 23:33:05 +03001822 if (bio_has_data(bio) &&
1823 (!(rw & (REQ_DISCARD | REQ_SANITIZE)))) {
Jens Axboebf2de6f2007-09-27 13:01:25 +02001824 if (rw & WRITE) {
1825 count_vm_events(PGPGOUT, count);
1826 } else {
1827 task_io_account_read(bio->bi_size);
1828 count_vm_events(PGPGIN, count);
1829 }
1830
1831 if (unlikely(block_dump)) {
1832 char b[BDEVNAME_SIZE];
San Mehat8dcbdc72010-09-14 08:48:01 +02001833 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s (%u sectors)\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001834 current->comm, task_pid_nr(current),
Jens Axboebf2de6f2007-09-27 13:01:25 +02001835 (rw & WRITE) ? "WRITE" : "READ",
1836 (unsigned long long)bio->bi_sector,
San Mehat8dcbdc72010-09-14 08:48:01 +02001837 bdevname(bio->bi_bdev, b),
1838 count);
Jens Axboebf2de6f2007-09-27 13:01:25 +02001839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 }
1841
1842 generic_make_request(bio);
1843}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844EXPORT_SYMBOL(submit_bio);
1845
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001846/**
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001847 * blk_rq_check_limits - Helper function to check a request for the queue limit
1848 * @q: the queue
1849 * @rq: the request being checked
1850 *
1851 * Description:
1852 * @rq may have been made based on weaker limitations of upper-level queues
1853 * in request stacking drivers, and it may violate the limitation of @q.
1854 * Since the block layer and the underlying device driver trust @rq
1855 * after it is inserted to @q, it should be checked against @q before
1856 * the insertion using this generic function.
1857 *
1858 * This function should also be useful for request stacking drivers
Stefan Weileef35c22010-08-06 21:11:15 +02001859 * in some cases below, so export this function.
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001860 * Request stacking drivers like request-based dm may change the queue
1861 * limits while requests are in the queue (e.g. dm's table swapping).
1862 * Such request stacking drivers should check those requests agaist
1863 * the new queue limits again when they dispatch those requests,
1864 * although such checkings are also done against the old queue limits
1865 * when submitting requests.
1866 */
1867int blk_rq_check_limits(struct request_queue *q, struct request *rq)
1868{
Maya Erez73937f52012-05-24 23:33:05 +03001869 if (rq->cmd_flags & (REQ_DISCARD | REQ_SANITIZE))
ike Snitzer33839772010-08-08 12:11:33 -04001870 return 0;
1871
Martin K. Petersenae03bf62009-05-22 17:17:50 -04001872 if (blk_rq_sectors(rq) > queue_max_sectors(q) ||
1873 blk_rq_bytes(rq) > queue_max_hw_sectors(q) << 9) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001874 printk(KERN_ERR "%s: over max size limit.\n", __func__);
1875 return -EIO;
1876 }
1877
1878 /*
1879 * queue's settings related to segment counting like q->bounce_pfn
1880 * may differ from that of other stacking queues.
1881 * Recalculate it to check the request correctly on this queue's
1882 * limitation.
1883 */
1884 blk_recalc_rq_segments(rq);
Martin K. Petersen8a783622010-02-26 00:20:39 -05001885 if (rq->nr_phys_segments > queue_max_segments(q)) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001886 printk(KERN_ERR "%s: over max segments limit.\n", __func__);
1887 return -EIO;
1888 }
1889
1890 return 0;
1891}
1892EXPORT_SYMBOL_GPL(blk_rq_check_limits);
1893
1894/**
1895 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
1896 * @q: the queue to submit the request
1897 * @rq: the request being queued
1898 */
1899int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
1900{
1901 unsigned long flags;
Jeff Moyer4853aba2011-08-15 21:37:25 +02001902 int where = ELEVATOR_INSERT_BACK;
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001903
1904 if (blk_rq_check_limits(q, rq))
1905 return -EIO;
1906
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001907 if (rq->rq_disk &&
1908 should_fail_request(&rq->rq_disk->part0, blk_rq_bytes(rq)))
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001909 return -EIO;
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001910
1911 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo8ba61432011-12-14 00:33:37 +01001912 if (unlikely(blk_queue_dead(q))) {
1913 spin_unlock_irqrestore(q->queue_lock, flags);
1914 return -ENODEV;
1915 }
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001916
1917 /*
1918 * Submitting request must be dequeued before calling this function
1919 * because it will be linked to another request_queue
1920 */
1921 BUG_ON(blk_queued_rq(rq));
1922
Jeff Moyer4853aba2011-08-15 21:37:25 +02001923 if (rq->cmd_flags & (REQ_FLUSH|REQ_FUA))
1924 where = ELEVATOR_INSERT_FLUSH;
1925
1926 add_acct_request(q, rq, where);
Jeff Moyere67b77c2011-10-17 12:57:23 +02001927 if (where == ELEVATOR_INSERT_FLUSH)
1928 __blk_run_queue(q);
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001929 spin_unlock_irqrestore(q->queue_lock, flags);
1930
1931 return 0;
1932}
1933EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
1934
Tejun Heo80a761f2009-07-03 17:48:17 +09001935/**
1936 * blk_rq_err_bytes - determine number of bytes till the next failure boundary
1937 * @rq: request to examine
1938 *
1939 * Description:
1940 * A request could be merge of IOs which require different failure
1941 * handling. This function determines the number of bytes which
1942 * can be failed from the beginning of the request without
1943 * crossing into area which need to be retried further.
1944 *
1945 * Return:
1946 * The number of bytes to fail.
1947 *
1948 * Context:
1949 * queue_lock must be held.
1950 */
1951unsigned int blk_rq_err_bytes(const struct request *rq)
1952{
1953 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
1954 unsigned int bytes = 0;
1955 struct bio *bio;
1956
1957 if (!(rq->cmd_flags & REQ_MIXED_MERGE))
1958 return blk_rq_bytes(rq);
1959
1960 /*
1961 * Currently the only 'mixing' which can happen is between
1962 * different fastfail types. We can safely fail portions
1963 * which have all the failfast bits that the first one has -
1964 * the ones which are at least as eager to fail as the first
1965 * one.
1966 */
1967 for (bio = rq->bio; bio; bio = bio->bi_next) {
1968 if ((bio->bi_rw & ff) != ff)
1969 break;
1970 bytes += bio->bi_size;
1971 }
1972
1973 /* this could lead to infinite loop */
1974 BUG_ON(blk_rq_bytes(rq) && !bytes);
1975 return bytes;
1976}
1977EXPORT_SYMBOL_GPL(blk_rq_err_bytes);
1978
Jens Axboebc58ba92009-01-23 10:54:44 +01001979static void blk_account_io_completion(struct request *req, unsigned int bytes)
1980{
Jens Axboec2553b52009-04-24 08:10:11 +02001981 if (blk_do_io_stat(req)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01001982 const int rw = rq_data_dir(req);
1983 struct hd_struct *part;
1984 int cpu;
1985
1986 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +01001987 part = req->part;
Jens Axboebc58ba92009-01-23 10:54:44 +01001988 part_stat_add(cpu, part, sectors[rw], bytes >> 9);
1989 part_stat_unlock();
1990 }
1991}
1992
1993static void blk_account_io_done(struct request *req)
1994{
Jens Axboebc58ba92009-01-23 10:54:44 +01001995 /*
Tejun Heodd4c1332010-09-03 11:56:16 +02001996 * Account IO completion. flush_rq isn't accounted as a
1997 * normal IO on queueing nor completion. Accounting the
1998 * containing request is enough.
Jens Axboebc58ba92009-01-23 10:54:44 +01001999 */
Tejun Heo414b4ff2011-01-25 12:43:49 +01002000 if (blk_do_io_stat(req) && !(req->cmd_flags & REQ_FLUSH_SEQ)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01002001 unsigned long duration = jiffies - req->start_time;
2002 const int rw = rq_data_dir(req);
2003 struct hd_struct *part;
2004 int cpu;
2005
2006 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +01002007 part = req->part;
Jens Axboebc58ba92009-01-23 10:54:44 +01002008
2009 part_stat_inc(cpu, part, ios[rw]);
2010 part_stat_add(cpu, part, ticks[rw], duration);
2011 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02002012 part_dec_in_flight(part, rw);
Jens Axboebc58ba92009-01-23 10:54:44 +01002013
Jens Axboe6c23a962011-01-07 08:43:37 +01002014 hd_struct_put(part);
Jens Axboebc58ba92009-01-23 10:54:44 +01002015 part_stat_unlock();
2016 }
2017}
2018
Lin Ming0fb59e52013-03-23 11:42:27 +08002019#ifdef CONFIG_PM_RUNTIME
2020/*
2021 * Don't process normal requests when queue is suspended
2022 * or in the process of suspending/resuming
2023 */
2024static struct request *blk_pm_peek_request(struct request_queue *q,
2025 struct request *rq)
2026{
2027 if (q->dev && (q->rpm_status == RPM_SUSPENDED ||
2028 (q->rpm_status != RPM_ACTIVE && !(rq->cmd_flags & REQ_PM))))
2029 return NULL;
2030 else
2031 return rq;
2032}
2033#else
2034static inline struct request *blk_pm_peek_request(struct request_queue *q,
2035 struct request *rq)
2036{
2037 return rq;
2038}
2039#endif
2040
Tejun Heo53a08802008-12-03 12:41:26 +01002041/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09002042 * blk_peek_request - peek at the top of a request queue
2043 * @q: request queue to peek at
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002044 *
2045 * Description:
Tejun Heo9934c8c2009-05-08 11:54:16 +09002046 * Return the request at the top of @q. The returned request
2047 * should be started using blk_start_request() before LLD starts
2048 * processing it.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002049 *
2050 * Return:
Tejun Heo9934c8c2009-05-08 11:54:16 +09002051 * Pointer to the request at the top of @q if available. Null
2052 * otherwise.
2053 *
2054 * Context:
2055 * queue_lock must be held.
2056 */
2057struct request *blk_peek_request(struct request_queue *q)
Tejun Heo158dbda2009-04-23 11:05:18 +09002058{
2059 struct request *rq;
2060 int ret;
2061
2062 while ((rq = __elv_next_request(q)) != NULL) {
Lin Ming0fb59e52013-03-23 11:42:27 +08002063
2064 rq = blk_pm_peek_request(q, rq);
2065 if (!rq)
2066 break;
2067
Tejun Heo158dbda2009-04-23 11:05:18 +09002068 if (!(rq->cmd_flags & REQ_STARTED)) {
2069 /*
2070 * This is the first time the device driver
2071 * sees this request (possibly after
2072 * requeueing). Notify IO scheduler.
2073 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002074 if (rq->cmd_flags & REQ_SORTED)
Tejun Heo158dbda2009-04-23 11:05:18 +09002075 elv_activate_rq(q, rq);
2076
2077 /*
2078 * just mark as started even if we don't start
2079 * it, a request that has been delayed should
2080 * not be passed by new incoming requests
2081 */
2082 rq->cmd_flags |= REQ_STARTED;
Tatyana Brokhman2b3b3232013-09-04 22:13:47 +03002083 if (rq->cmd_flags & REQ_URGENT) {
2084 WARN_ON(q->dispatched_urgent);
Tatyana Brokhman2dd5b262013-05-01 14:35:20 +03002085 q->dispatched_urgent = true;
Tatyana Brokhman2b3b3232013-09-04 22:13:47 +03002086 }
Tejun Heo158dbda2009-04-23 11:05:18 +09002087 trace_block_rq_issue(q, rq);
2088 }
2089
2090 if (!q->boundary_rq || q->boundary_rq == rq) {
2091 q->end_sector = rq_end_sector(rq);
2092 q->boundary_rq = NULL;
2093 }
2094
2095 if (rq->cmd_flags & REQ_DONTPREP)
2096 break;
2097
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002098 if (q->dma_drain_size && blk_rq_bytes(rq)) {
Tejun Heo158dbda2009-04-23 11:05:18 +09002099 /*
2100 * make sure space for the drain appears we
2101 * know we can do this because max_hw_segments
2102 * has been adjusted to be one fewer than the
2103 * device can handle
2104 */
2105 rq->nr_phys_segments++;
2106 }
2107
2108 if (!q->prep_rq_fn)
2109 break;
2110
2111 ret = q->prep_rq_fn(q, rq);
2112 if (ret == BLKPREP_OK) {
2113 break;
2114 } else if (ret == BLKPREP_DEFER) {
2115 /*
2116 * the request may have been (partially) prepped.
2117 * we need to keep this request in the front to
2118 * avoid resource deadlock. REQ_STARTED will
2119 * prevent other fs requests from passing this one.
2120 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002121 if (q->dma_drain_size && blk_rq_bytes(rq) &&
Tejun Heo158dbda2009-04-23 11:05:18 +09002122 !(rq->cmd_flags & REQ_DONTPREP)) {
2123 /*
2124 * remove the space for the drain we added
2125 * so that we don't add it again
2126 */
2127 --rq->nr_phys_segments;
2128 }
2129
2130 rq = NULL;
2131 break;
2132 } else if (ret == BLKPREP_KILL) {
2133 rq->cmd_flags |= REQ_QUIET;
James Bottomleyc143dc92009-05-30 06:43:49 +02002134 /*
2135 * Mark this request as started so we don't trigger
2136 * any debug logic in the end I/O path.
2137 */
2138 blk_start_request(rq);
Tejun Heo40cbbb72009-04-23 11:05:19 +09002139 __blk_end_request_all(rq, -EIO);
Tejun Heo158dbda2009-04-23 11:05:18 +09002140 } else {
2141 printk(KERN_ERR "%s: bad return=%d\n", __func__, ret);
2142 break;
2143 }
2144 }
2145
2146 return rq;
2147}
Tejun Heo9934c8c2009-05-08 11:54:16 +09002148EXPORT_SYMBOL(blk_peek_request);
Tejun Heo158dbda2009-04-23 11:05:18 +09002149
Tejun Heo9934c8c2009-05-08 11:54:16 +09002150void blk_dequeue_request(struct request *rq)
Tejun Heo158dbda2009-04-23 11:05:18 +09002151{
Tejun Heo9934c8c2009-05-08 11:54:16 +09002152 struct request_queue *q = rq->q;
2153
Tejun Heo158dbda2009-04-23 11:05:18 +09002154 BUG_ON(list_empty(&rq->queuelist));
2155 BUG_ON(ELV_ON_HASH(rq));
2156
2157 list_del_init(&rq->queuelist);
2158
2159 /*
2160 * the time frame between a request being removed from the lists
2161 * and to it is freed is accounted as io that is in progress at
2162 * the driver side.
2163 */
Divyesh Shah91952912010-04-01 15:01:41 -07002164 if (blk_account_rq(rq)) {
Jens Axboe0a7ae2f2009-05-20 08:54:31 +02002165 q->in_flight[rq_is_sync(rq)]++;
Divyesh Shah91952912010-04-01 15:01:41 -07002166 set_io_start_time_ns(rq);
2167 }
Tejun Heo158dbda2009-04-23 11:05:18 +09002168}
2169
Tejun Heo5efccd12009-04-23 11:05:18 +09002170/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09002171 * blk_start_request - start request processing on the driver
2172 * @req: request to dequeue
2173 *
2174 * Description:
2175 * Dequeue @req and start timeout timer on it. This hands off the
2176 * request to the driver.
2177 *
2178 * Block internal functions which don't want to start timer should
2179 * call blk_dequeue_request().
2180 *
2181 * Context:
2182 * queue_lock must be held.
2183 */
2184void blk_start_request(struct request *req)
2185{
2186 blk_dequeue_request(req);
2187
2188 /*
Tejun Heo5f49f632009-05-19 18:33:05 +09002189 * We are now handing the request to the hardware, initialize
2190 * resid_len to full count and add the timeout handler.
Tejun Heo9934c8c2009-05-08 11:54:16 +09002191 */
Tejun Heo5f49f632009-05-19 18:33:05 +09002192 req->resid_len = blk_rq_bytes(req);
FUJITA Tomonoridbb66c42009-06-09 05:47:10 +02002193 if (unlikely(blk_bidi_rq(req)))
2194 req->next_rq->resid_len = blk_rq_bytes(req->next_rq);
2195
Jeff Moyere428c162013-10-08 14:36:41 -04002196 BUG_ON(test_bit(REQ_ATOM_COMPLETE, &req->atomic_flags));
Tejun Heo9934c8c2009-05-08 11:54:16 +09002197 blk_add_timer(req);
2198}
2199EXPORT_SYMBOL(blk_start_request);
2200
2201/**
2202 * blk_fetch_request - fetch a request from a request queue
2203 * @q: request queue to fetch a request from
2204 *
2205 * Description:
2206 * Return the request at the top of @q. The request is started on
2207 * return and LLD can start processing it immediately.
2208 *
2209 * Return:
2210 * Pointer to the request at the top of @q if available. Null
2211 * otherwise.
2212 *
2213 * Context:
2214 * queue_lock must be held.
2215 */
2216struct request *blk_fetch_request(struct request_queue *q)
2217{
2218 struct request *rq;
2219
2220 rq = blk_peek_request(q);
Tatyana Brokhman2dd5b262013-05-01 14:35:20 +03002221 if (rq)
Tejun Heo9934c8c2009-05-08 11:54:16 +09002222 blk_start_request(rq);
2223 return rq;
2224}
2225EXPORT_SYMBOL(blk_fetch_request);
2226
2227/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002228 * blk_update_request - Special helper function for request stacking drivers
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002229 * @req: the request being processed
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002230 * @error: %0 for success, < %0 for error
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002231 * @nr_bytes: number of bytes to complete @req
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002232 *
2233 * Description:
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002234 * Ends I/O on a number of bytes attached to @req, but doesn't complete
2235 * the request structure even if @req doesn't have leftover.
2236 * If @req has leftover, sets it up for the next range of segments.
Tejun Heo2e60e022009-04-23 11:05:18 +09002237 *
2238 * This special helper function is only for request stacking drivers
2239 * (e.g. request-based dm) so that they can handle partial completion.
2240 * Actual device drivers should use blk_end_request instead.
2241 *
2242 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
2243 * %false return from this function.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002244 *
2245 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002246 * %false - this request doesn't have any more data
2247 * %true - this request has more data
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002248 **/
Tejun Heo2e60e022009-04-23 11:05:18 +09002249bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250{
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05002251 int total_bytes, bio_nbytes, next_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 struct bio *bio;
2253
Tejun Heo2e60e022009-04-23 11:05:18 +09002254 if (!req->bio)
2255 return false;
2256
Roman Pen08d448e2014-03-04 23:13:10 +09002257 trace_block_rq_complete(req->q, req, nr_bytes);
Jens Axboe2056a782006-03-23 20:00:26 +01002258
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 /*
Tejun Heo6f414692009-04-19 07:00:41 +09002260 * For fs requests, rq is just carrier of independent bio's
2261 * and each partial completion should be handled separately.
2262 * Reset per-request error on each partial completion.
2263 *
2264 * TODO: tj: This is too subtle. It would be better to let
2265 * low level drivers do what they see fit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002267 if (req->cmd_type == REQ_TYPE_FS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 req->errors = 0;
2269
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002270 if (error && req->cmd_type == REQ_TYPE_FS &&
2271 !(req->cmd_flags & REQ_QUIET)) {
Hannes Reinecke79775562011-01-18 10:13:13 +01002272 char *error_type;
2273
2274 switch (error) {
2275 case -ENOLINK:
2276 error_type = "recoverable transport";
2277 break;
2278 case -EREMOTEIO:
2279 error_type = "critical target";
2280 break;
2281 case -EBADE:
2282 error_type = "critical nexus";
2283 break;
2284 case -EIO:
2285 default:
2286 error_type = "I/O";
2287 break;
2288 }
Asutosh Das75de0c32013-03-07 17:43:35 +05302289 printk_ratelimited(
2290 KERN_ERR "end_request: %s error, dev %s, sector %llu\n",
2291 error_type,
2292 req->rq_disk ? req->rq_disk->disk_name : "?",
2293 (unsigned long long)blk_rq_pos(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 }
2295
Jens Axboebc58ba92009-01-23 10:54:44 +01002296 blk_account_io_completion(req, nr_bytes);
Jens Axboed72d9042005-11-01 08:35:42 +01002297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 total_bytes = bio_nbytes = 0;
Dinesh K Garg93b3cb02014-01-13 13:55:00 -08002299
2300 /*
2301 * Check for this if flagged, Req based dm needs to perform
2302 * post processing, hence dont end bios or request.DM
2303 * layer takes care.
2304 */
2305 if (bio_flagged(req->bio, BIO_DONTFREE))
2306 return false;
2307
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 while ((bio = req->bio) != NULL) {
2309 int nbytes;
2310
2311 if (nr_bytes >= bio->bi_size) {
2312 req->bio = bio->bi_next;
2313 nbytes = bio->bi_size;
NeilBrown5bb23a62007-09-27 12:46:13 +02002314 req_bio_endio(req, bio, nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 next_idx = 0;
2316 bio_nbytes = 0;
2317 } else {
2318 int idx = bio->bi_idx + next_idx;
2319
Kazuhisa Ichikawaaf498d72009-05-12 13:27:45 +02002320 if (unlikely(idx >= bio->bi_vcnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 blk_dump_rq_flags(req, "__end_that");
Jens Axboe6728cb02008-01-31 13:03:55 +01002322 printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
Kazuhisa Ichikawaaf498d72009-05-12 13:27:45 +02002323 __func__, idx, bio->bi_vcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 break;
2325 }
2326
2327 nbytes = bio_iovec_idx(bio, idx)->bv_len;
2328 BIO_BUG_ON(nbytes > bio->bi_size);
2329
2330 /*
2331 * not a complete bvec done
2332 */
2333 if (unlikely(nbytes > nr_bytes)) {
2334 bio_nbytes += nr_bytes;
2335 total_bytes += nr_bytes;
2336 break;
2337 }
2338
2339 /*
2340 * advance to the next vector
2341 */
2342 next_idx++;
2343 bio_nbytes += nbytes;
2344 }
2345
2346 total_bytes += nbytes;
2347 nr_bytes -= nbytes;
2348
Jens Axboe6728cb02008-01-31 13:03:55 +01002349 bio = req->bio;
2350 if (bio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 /*
2352 * end more in this run, or just return 'not-done'
2353 */
2354 if (unlikely(nr_bytes <= 0))
2355 break;
2356 }
2357 }
2358
2359 /*
2360 * completely done
2361 */
Tejun Heo2e60e022009-04-23 11:05:18 +09002362 if (!req->bio) {
2363 /*
2364 * Reset counters so that the request stacking driver
2365 * can find how many bytes remain in the request
2366 * later.
2367 */
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002368 req->__data_len = 0;
Tejun Heo2e60e022009-04-23 11:05:18 +09002369 return false;
2370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
2372 /*
2373 * if the request wasn't completed, update state
2374 */
2375 if (bio_nbytes) {
NeilBrown5bb23a62007-09-27 12:46:13 +02002376 req_bio_endio(req, bio, bio_nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 bio->bi_idx += next_idx;
2378 bio_iovec(bio)->bv_offset += nr_bytes;
2379 bio_iovec(bio)->bv_len -= nr_bytes;
2380 }
2381
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002382 req->__data_len -= total_bytes;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002383 req->buffer = bio_data(req->bio);
2384
2385 /* update sector only for requests with clear definition of sector */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002386 if (req->cmd_type == REQ_TYPE_FS || (req->cmd_flags & REQ_DISCARD))
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002387 req->__sector += total_bytes >> 9;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002388
Tejun Heo80a761f2009-07-03 17:48:17 +09002389 /* mixed attributes always follow the first bio */
2390 if (req->cmd_flags & REQ_MIXED_MERGE) {
2391 req->cmd_flags &= ~REQ_FAILFAST_MASK;
2392 req->cmd_flags |= req->bio->bi_rw & REQ_FAILFAST_MASK;
2393 }
2394
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002395 /*
2396 * If total number of sectors is less than the first segment
2397 * size, something has gone terribly wrong.
2398 */
2399 if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
Jens Axboe81829242011-03-30 09:51:33 +02002400 blk_dump_rq_flags(req, "request botched");
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002401 req->__data_len = blk_rq_cur_bytes(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002402 }
2403
2404 /* recalculate the number of segments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 blk_recalc_rq_segments(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002406
Tejun Heo2e60e022009-04-23 11:05:18 +09002407 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408}
Tejun Heo2e60e022009-04-23 11:05:18 +09002409EXPORT_SYMBOL_GPL(blk_update_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
Tejun Heo2e60e022009-04-23 11:05:18 +09002411static bool blk_update_bidi_request(struct request *rq, int error,
2412 unsigned int nr_bytes,
2413 unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002414{
Tejun Heo2e60e022009-04-23 11:05:18 +09002415 if (blk_update_request(rq, error, nr_bytes))
2416 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002417
Tejun Heo2e60e022009-04-23 11:05:18 +09002418 /* Bidi request must be completed as a whole */
2419 if (unlikely(blk_bidi_rq(rq)) &&
2420 blk_update_request(rq->next_rq, error, bidi_bytes))
2421 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002422
Jens Axboee2e1a142010-06-09 10:42:09 +02002423 if (blk_queue_add_random(rq->q))
2424 add_disk_randomness(rq->rq_disk);
Tejun Heo2e60e022009-04-23 11:05:18 +09002425
2426 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427}
2428
James Bottomley28018c22010-07-01 19:49:17 +09002429/**
2430 * blk_unprep_request - unprepare a request
2431 * @req: the request
2432 *
2433 * This function makes a request ready for complete resubmission (or
2434 * completion). It happens only after all error handling is complete,
2435 * so represents the appropriate moment to deallocate any resources
2436 * that were allocated to the request in the prep_rq_fn. The queue
2437 * lock is held when calling this.
2438 */
2439void blk_unprep_request(struct request *req)
2440{
2441 struct request_queue *q = req->q;
2442
2443 req->cmd_flags &= ~REQ_DONTPREP;
2444 if (q->unprep_rq_fn)
2445 q->unprep_rq_fn(q, req);
2446}
2447EXPORT_SYMBOL_GPL(blk_unprep_request);
2448
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449/*
2450 * queue lock must be held
2451 */
Tejun Heo2e60e022009-04-23 11:05:18 +09002452static void blk_finish_request(struct request *req, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453{
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002454 if (blk_rq_tagged(req))
2455 blk_queue_end_tag(req->q, req);
2456
James Bottomleyba396a62009-05-27 14:17:08 +02002457 BUG_ON(blk_queued_rq(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002459 if (unlikely(laptop_mode) && req->cmd_type == REQ_TYPE_FS)
Matthew Garrett31373d02010-04-06 14:25:14 +02002460 laptop_io_completion(&req->q->backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
Mike Andersone78042e2008-10-30 02:16:20 -07002462 blk_delete_timer(req);
2463
James Bottomley28018c22010-07-01 19:49:17 +09002464 if (req->cmd_flags & REQ_DONTPREP)
2465 blk_unprep_request(req);
2466
2467
Jens Axboebc58ba92009-01-23 10:54:44 +01002468 blk_account_io_done(req);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002469
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 if (req->end_io)
Tejun Heo8ffdc652006-01-06 09:49:03 +01002471 req->end_io(req, error);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002472 else {
2473 if (blk_bidi_rq(req))
2474 __blk_put_request(req->next_rq->q, req->next_rq);
2475
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 __blk_put_request(req->q, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 }
2478}
2479
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05002480/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002481 * blk_end_bidi_request - Complete a bidi request
2482 * @rq: the request to complete
Randy Dunlap710027a2008-08-19 20:13:11 +02002483 * @error: %0 for success, < %0 for error
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002484 * @nr_bytes: number of bytes to complete @rq
2485 * @bidi_bytes: number of bytes to complete @rq->next_rq
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002486 *
2487 * Description:
2488 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
Tejun Heo2e60e022009-04-23 11:05:18 +09002489 * Drivers that supports bidi can safely call this member for any
2490 * type of request, bidi or uni. In the later case @bidi_bytes is
2491 * just ignored.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002492 *
2493 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002494 * %false - we are done with this request
2495 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002496 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002497static bool blk_end_bidi_request(struct request *rq, int error,
2498 unsigned int nr_bytes, unsigned int bidi_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002499{
2500 struct request_queue *q = rq->q;
Tejun Heo2e60e022009-04-23 11:05:18 +09002501 unsigned long flags;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002502
Tejun Heo2e60e022009-04-23 11:05:18 +09002503 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2504 return true;
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002505
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002506 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo2e60e022009-04-23 11:05:18 +09002507 blk_finish_request(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002508 spin_unlock_irqrestore(q->queue_lock, flags);
2509
Tejun Heo2e60e022009-04-23 11:05:18 +09002510 return false;
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002511}
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002512
2513/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002514 * __blk_end_bidi_request - Complete a bidi request with queue lock held
2515 * @rq: the request to complete
2516 * @error: %0 for success, < %0 for error
2517 * @nr_bytes: number of bytes to complete @rq
2518 * @bidi_bytes: number of bytes to complete @rq->next_rq
Tejun Heo5efccd12009-04-23 11:05:18 +09002519 *
2520 * Description:
Tejun Heo2e60e022009-04-23 11:05:18 +09002521 * Identical to blk_end_bidi_request() except that queue lock is
2522 * assumed to be locked on entry and remains so on return.
Tejun Heo5efccd12009-04-23 11:05:18 +09002523 *
Tejun Heo2e60e022009-04-23 11:05:18 +09002524 * Return:
2525 * %false - we are done with this request
2526 * %true - still buffers pending for this request
Tejun Heo5efccd12009-04-23 11:05:18 +09002527 **/
Jeff Moyer4853aba2011-08-15 21:37:25 +02002528bool __blk_end_bidi_request(struct request *rq, int error,
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002529 unsigned int nr_bytes, unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002530{
Tejun Heo2e60e022009-04-23 11:05:18 +09002531 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2532 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002533
Tejun Heo2e60e022009-04-23 11:05:18 +09002534 blk_finish_request(rq, error);
Tejun Heo5efccd12009-04-23 11:05:18 +09002535
Tejun Heo2e60e022009-04-23 11:05:18 +09002536 return false;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002537}
2538
2539/**
2540 * blk_end_request - Helper function for drivers to complete the request.
2541 * @rq: the request being processed
2542 * @error: %0 for success, < %0 for error
2543 * @nr_bytes: number of bytes to complete
2544 *
2545 * Description:
2546 * Ends I/O on a number of bytes attached to @rq.
2547 * If @rq has leftover, sets it up for the next range of segments.
2548 *
2549 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002550 * %false - we are done with this request
2551 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002552 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002553bool blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002554{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002555 return blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002556}
Jens Axboe56ad1742009-07-28 22:11:24 +02002557EXPORT_SYMBOL(blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002558
2559/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002560 * blk_end_request_all - Helper function for drives to finish the request.
2561 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002562 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002563 *
2564 * Description:
2565 * Completely finish @rq.
2566 */
2567void blk_end_request_all(struct request *rq, int error)
2568{
2569 bool pending;
2570 unsigned int bidi_bytes = 0;
2571
2572 if (unlikely(blk_bidi_rq(rq)))
2573 bidi_bytes = blk_rq_bytes(rq->next_rq);
2574
2575 pending = blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2576 BUG_ON(pending);
2577}
Jens Axboe56ad1742009-07-28 22:11:24 +02002578EXPORT_SYMBOL(blk_end_request_all);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002579
2580/**
2581 * blk_end_request_cur - Helper function to finish the current request chunk.
2582 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002583 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002584 *
2585 * Description:
2586 * Complete the current consecutively mapped chunk from @rq.
2587 *
2588 * Return:
2589 * %false - we are done with this request
2590 * %true - still buffers pending for this request
2591 */
2592bool blk_end_request_cur(struct request *rq, int error)
2593{
2594 return blk_end_request(rq, error, blk_rq_cur_bytes(rq));
2595}
Jens Axboe56ad1742009-07-28 22:11:24 +02002596EXPORT_SYMBOL(blk_end_request_cur);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002597
2598/**
Tejun Heo80a761f2009-07-03 17:48:17 +09002599 * blk_end_request_err - Finish a request till the next failure boundary.
2600 * @rq: the request to finish till the next failure boundary for
2601 * @error: must be negative errno
2602 *
2603 * Description:
2604 * Complete @rq till the next failure boundary.
2605 *
2606 * Return:
2607 * %false - we are done with this request
2608 * %true - still buffers pending for this request
2609 */
2610bool blk_end_request_err(struct request *rq, int error)
2611{
2612 WARN_ON(error >= 0);
2613 return blk_end_request(rq, error, blk_rq_err_bytes(rq));
2614}
2615EXPORT_SYMBOL_GPL(blk_end_request_err);
2616
2617/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002618 * __blk_end_request - Helper function for drivers to complete the request.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002619 * @rq: the request being processed
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002620 * @error: %0 for success, < %0 for error
2621 * @nr_bytes: number of bytes to complete
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002622 *
2623 * Description:
2624 * Must be called with queue lock held unlike blk_end_request().
2625 *
2626 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002627 * %false - we are done with this request
2628 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002629 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002630bool __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002631{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002632 return __blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002633}
Jens Axboe56ad1742009-07-28 22:11:24 +02002634EXPORT_SYMBOL(__blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002635
2636/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002637 * __blk_end_request_all - Helper function for drives to finish the request.
2638 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002639 * @error: %0 for success, < %0 for error
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002640 *
2641 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002642 * Completely finish @rq. Must be called with queue lock held.
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002643 */
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002644void __blk_end_request_all(struct request *rq, int error)
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002645{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002646 bool pending;
2647 unsigned int bidi_bytes = 0;
2648
2649 if (unlikely(blk_bidi_rq(rq)))
2650 bidi_bytes = blk_rq_bytes(rq->next_rq);
2651
2652 pending = __blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2653 BUG_ON(pending);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002654}
Jens Axboe56ad1742009-07-28 22:11:24 +02002655EXPORT_SYMBOL(__blk_end_request_all);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002656
2657/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002658 * __blk_end_request_cur - Helper function to finish the current request chunk.
2659 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002660 * @error: %0 for success, < %0 for error
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002661 *
2662 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002663 * Complete the current consecutively mapped chunk from @rq. Must
2664 * be called with queue lock held.
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002665 *
2666 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002667 * %false - we are done with this request
2668 * %true - still buffers pending for this request
2669 */
2670bool __blk_end_request_cur(struct request *rq, int error)
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002671{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002672 return __blk_end_request(rq, error, blk_rq_cur_bytes(rq));
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002673}
Jens Axboe56ad1742009-07-28 22:11:24 +02002674EXPORT_SYMBOL(__blk_end_request_cur);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002675
Tejun Heo80a761f2009-07-03 17:48:17 +09002676/**
2677 * __blk_end_request_err - Finish a request till the next failure boundary.
2678 * @rq: the request to finish till the next failure boundary for
2679 * @error: must be negative errno
2680 *
2681 * Description:
2682 * Complete @rq till the next failure boundary. Must be called
2683 * with queue lock held.
2684 *
2685 * Return:
2686 * %false - we are done with this request
2687 * %true - still buffers pending for this request
2688 */
2689bool __blk_end_request_err(struct request *rq, int error)
2690{
2691 WARN_ON(error >= 0);
2692 return __blk_end_request(rq, error, blk_rq_err_bytes(rq));
2693}
2694EXPORT_SYMBOL_GPL(__blk_end_request_err);
2695
Jens Axboe86db1e22008-01-29 14:53:40 +01002696void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2697 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698{
Tejun Heoa82afdf2009-07-03 17:48:16 +09002699 /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002700 rq->cmd_flags |= bio->bi_rw & REQ_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
David Woodhousefb2dce82008-08-05 18:01:53 +01002702 if (bio_has_data(bio)) {
2703 rq->nr_phys_segments = bio_phys_segments(q, bio);
David Woodhousefb2dce82008-08-05 18:01:53 +01002704 rq->buffer = bio_data(bio);
2705 }
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002706 rq->__data_len = bio->bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 rq->bio = rq->biotail = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
NeilBrown66846572007-08-16 13:31:28 +02002709 if (bio->bi_bdev)
2710 rq->rq_disk = bio->bi_bdev->bd_disk;
2711}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
Ilya Loginov2d4dc892009-11-26 09:16:19 +01002713#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
2714/**
2715 * rq_flush_dcache_pages - Helper function to flush all pages in a request
2716 * @rq: the request to be flushed
2717 *
2718 * Description:
2719 * Flush all pages in @rq.
2720 */
2721void rq_flush_dcache_pages(struct request *rq)
2722{
2723 struct req_iterator iter;
2724 struct bio_vec *bvec;
2725
2726 rq_for_each_segment(bvec, rq, iter)
2727 flush_dcache_page(bvec->bv_page);
2728}
2729EXPORT_SYMBOL_GPL(rq_flush_dcache_pages);
2730#endif
2731
Kiyoshi Uedaef9e3fa2008-10-01 16:12:15 +02002732/**
2733 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
2734 * @q : the queue of the device being checked
2735 *
2736 * Description:
2737 * Check if underlying low-level drivers of a device are busy.
2738 * If the drivers want to export their busy state, they must set own
2739 * exporting function using blk_queue_lld_busy() first.
2740 *
2741 * Basically, this function is used only by request stacking drivers
2742 * to stop dispatching requests to underlying devices when underlying
2743 * devices are busy. This behavior helps more I/O merging on the queue
2744 * of the request stacking driver and prevents I/O throughput regression
2745 * on burst I/O load.
2746 *
2747 * Return:
2748 * 0 - Not busy (The request stacking driver should dispatch request)
2749 * 1 - Busy (The request stacking driver should stop dispatching request)
2750 */
2751int blk_lld_busy(struct request_queue *q)
2752{
2753 if (q->lld_busy_fn)
2754 return q->lld_busy_fn(q);
2755
2756 return 0;
2757}
2758EXPORT_SYMBOL_GPL(blk_lld_busy);
2759
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002760/**
2761 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
2762 * @rq: the clone request to be cleaned up
2763 *
2764 * Description:
2765 * Free all bios in @rq for a cloned request.
2766 */
2767void blk_rq_unprep_clone(struct request *rq)
2768{
2769 struct bio *bio;
2770
2771 while ((bio = rq->bio) != NULL) {
2772 rq->bio = bio->bi_next;
2773
2774 bio_put(bio);
2775 }
2776}
2777EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
2778
2779/*
2780 * Copy attributes of the original request to the clone request.
2781 * The actual data parts (e.g. ->cmd, ->buffer, ->sense) are not copied.
2782 */
2783static void __blk_rq_prep_clone(struct request *dst, struct request *src)
2784{
2785 dst->cpu = src->cpu;
Tejun Heo3a2edd02010-09-03 11:56:18 +02002786 dst->cmd_flags = (src->cmd_flags & REQ_CLONE_MASK) | REQ_NOMERGE;
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002787 dst->cmd_type = src->cmd_type;
2788 dst->__sector = blk_rq_pos(src);
2789 dst->__data_len = blk_rq_bytes(src);
2790 dst->nr_phys_segments = src->nr_phys_segments;
2791 dst->ioprio = src->ioprio;
2792 dst->extra_len = src->extra_len;
2793}
2794
2795/**
2796 * blk_rq_prep_clone - Helper function to setup clone request
2797 * @rq: the request to be setup
2798 * @rq_src: original request to be cloned
2799 * @bs: bio_set that bios for clone are allocated from
2800 * @gfp_mask: memory allocation mask for bio
2801 * @bio_ctr: setup function to be called for each clone bio.
2802 * Returns %0 for success, non %0 for failure.
2803 * @data: private data to be passed to @bio_ctr
2804 *
2805 * Description:
2806 * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
2807 * The actual data parts of @rq_src (e.g. ->cmd, ->buffer, ->sense)
2808 * are not copied, and copying such parts is the caller's responsibility.
2809 * Also, pages which the original bios are pointing to are not copied
2810 * and the cloned bios just point same pages.
2811 * So cloned bios must be completed before original bios, which means
2812 * the caller must complete @rq before @rq_src.
2813 */
2814int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
2815 struct bio_set *bs, gfp_t gfp_mask,
2816 int (*bio_ctr)(struct bio *, struct bio *, void *),
2817 void *data)
2818{
2819 struct bio *bio, *bio_src;
2820
2821 if (!bs)
2822 bs = fs_bio_set;
2823
2824 blk_rq_init(NULL, rq);
2825
2826 __rq_for_each_bio(bio_src, rq_src) {
2827 bio = bio_alloc_bioset(gfp_mask, bio_src->bi_max_vecs, bs);
2828 if (!bio)
2829 goto free_and_out;
2830
2831 __bio_clone(bio, bio_src);
2832
2833 if (bio_integrity(bio_src) &&
Martin K. Petersen7878cba2009-06-26 15:37:49 +02002834 bio_integrity_clone(bio, bio_src, gfp_mask, bs))
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002835 goto free_and_out;
2836
2837 if (bio_ctr && bio_ctr(bio, bio_src, data))
2838 goto free_and_out;
2839
2840 if (rq->bio) {
2841 rq->biotail->bi_next = bio;
2842 rq->biotail = bio;
2843 } else
2844 rq->bio = rq->biotail = bio;
2845 }
2846
2847 __blk_rq_prep_clone(rq, rq_src);
2848
2849 return 0;
2850
2851free_and_out:
2852 if (bio)
2853 bio_free(bio, bs);
2854 blk_rq_unprep_clone(rq);
2855
2856 return -ENOMEM;
2857}
2858EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
2859
Jens Axboe18887ad2008-07-28 13:08:45 +02002860int kblockd_schedule_work(struct request_queue *q, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861{
2862 return queue_work(kblockd_workqueue, work);
2863}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864EXPORT_SYMBOL(kblockd_schedule_work);
2865
Vivek Goyale43473b2010-09-15 17:06:35 -04002866int kblockd_schedule_delayed_work(struct request_queue *q,
2867 struct delayed_work *dwork, unsigned long delay)
2868{
2869 return queue_delayed_work(kblockd_workqueue, dwork, delay);
2870}
2871EXPORT_SYMBOL(kblockd_schedule_delayed_work);
2872
Jens Axboe73c10102011-03-08 13:19:51 +01002873#define PLUG_MAGIC 0x91827364
2874
Suresh Jayaraman75df7132011-09-21 10:00:16 +02002875/**
2876 * blk_start_plug - initialize blk_plug and track it inside the task_struct
2877 * @plug: The &struct blk_plug that needs to be initialized
2878 *
2879 * Description:
2880 * Tracking blk_plug inside the task_struct will help with auto-flushing the
2881 * pending I/O should the task end up blocking between blk_start_plug() and
2882 * blk_finish_plug(). This is important from a performance perspective, but
2883 * also ensures that we don't deadlock. For instance, if the task is blocking
2884 * for a memory allocation, memory reclaim could end up wanting to free a
2885 * page belonging to that request that is currently residing in our private
2886 * plug. By flushing the pending I/O when the process goes to sleep, we avoid
2887 * this kind of deadlock.
2888 */
Jens Axboe73c10102011-03-08 13:19:51 +01002889void blk_start_plug(struct blk_plug *plug)
2890{
2891 struct task_struct *tsk = current;
2892
2893 plug->magic = PLUG_MAGIC;
2894 INIT_LIST_HEAD(&plug->list);
NeilBrown048c9372011-04-18 09:52:22 +02002895 INIT_LIST_HEAD(&plug->cb_list);
Jens Axboe73c10102011-03-08 13:19:51 +01002896 plug->should_sort = 0;
2897
2898 /*
2899 * If this is a nested plug, don't actually assign it. It will be
2900 * flushed on its own.
2901 */
2902 if (!tsk->plug) {
2903 /*
2904 * Store ordering should not be needed here, since a potential
2905 * preempt will imply a full memory barrier
2906 */
2907 tsk->plug = plug;
2908 }
2909}
2910EXPORT_SYMBOL(blk_start_plug);
2911
2912static int plug_rq_cmp(void *priv, struct list_head *a, struct list_head *b)
2913{
2914 struct request *rqa = container_of(a, struct request, queuelist);
2915 struct request *rqb = container_of(b, struct request, queuelist);
2916
Konstantin Khlebnikovf83e8262011-04-04 00:15:02 +02002917 return !(rqa->q <= rqb->q);
Jens Axboe73c10102011-03-08 13:19:51 +01002918}
2919
Jens Axboe49cac012011-04-16 13:51:05 +02002920/*
2921 * If 'from_schedule' is true, then postpone the dispatch of requests
2922 * until a safe kblockd context. We due this to avoid accidental big
2923 * additional stack usage in driver dispatch, in places where the originally
2924 * plugger did not intend it.
2925 */
Jens Axboef6603782011-04-15 15:49:07 +02002926static void queue_unplugged(struct request_queue *q, unsigned int depth,
Jens Axboe49cac012011-04-16 13:51:05 +02002927 bool from_schedule)
Jens Axboe99e22592011-04-18 09:59:55 +02002928 __releases(q->queue_lock)
Jens Axboe94b5eb22011-04-12 10:12:19 +02002929{
Jens Axboe49cac012011-04-16 13:51:05 +02002930 trace_block_unplug(q, depth, !from_schedule);
Jens Axboe99e22592011-04-18 09:59:55 +02002931
2932 /*
Tejun Heo8ba61432011-12-14 00:33:37 +01002933 * Don't mess with dead queue.
2934 */
2935 if (unlikely(blk_queue_dead(q))) {
2936 spin_unlock(q->queue_lock);
2937 return;
2938 }
2939
2940 /*
Jens Axboe99e22592011-04-18 09:59:55 +02002941 * If we are punting this to kblockd, then we can safely drop
2942 * the queue_lock before waking kblockd (which needs to take
2943 * this lock).
2944 */
2945 if (from_schedule) {
2946 spin_unlock(q->queue_lock);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02002947 blk_run_queue_async(q);
Jens Axboe99e22592011-04-18 09:59:55 +02002948 } else {
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02002949 __blk_run_queue(q);
Jens Axboe99e22592011-04-18 09:59:55 +02002950 spin_unlock(q->queue_lock);
2951 }
2952
Jens Axboe94b5eb22011-04-12 10:12:19 +02002953}
2954
NeilBrown048c9372011-04-18 09:52:22 +02002955static void flush_plug_callbacks(struct blk_plug *plug)
2956{
2957 LIST_HEAD(callbacks);
2958
2959 if (list_empty(&plug->cb_list))
2960 return;
2961
2962 list_splice_init(&plug->cb_list, &callbacks);
2963
2964 while (!list_empty(&callbacks)) {
2965 struct blk_plug_cb *cb = list_first_entry(&callbacks,
2966 struct blk_plug_cb,
2967 list);
2968 list_del(&cb->list);
2969 cb->callback(cb);
2970 }
2971}
2972
Jens Axboe49cac012011-04-16 13:51:05 +02002973void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
Jens Axboe73c10102011-03-08 13:19:51 +01002974{
2975 struct request_queue *q;
2976 unsigned long flags;
2977 struct request *rq;
NeilBrown109b8122011-04-11 14:13:10 +02002978 LIST_HEAD(list);
Jens Axboe94b5eb22011-04-12 10:12:19 +02002979 unsigned int depth;
Jens Axboe73c10102011-03-08 13:19:51 +01002980
2981 BUG_ON(plug->magic != PLUG_MAGIC);
2982
NeilBrown048c9372011-04-18 09:52:22 +02002983 flush_plug_callbacks(plug);
Jens Axboe73c10102011-03-08 13:19:51 +01002984 if (list_empty(&plug->list))
2985 return;
2986
NeilBrown109b8122011-04-11 14:13:10 +02002987 list_splice_init(&plug->list, &list);
2988
2989 if (plug->should_sort) {
2990 list_sort(NULL, &list, plug_rq_cmp);
2991 plug->should_sort = 0;
2992 }
Jens Axboe73c10102011-03-08 13:19:51 +01002993
2994 q = NULL;
Jens Axboe94b5eb22011-04-12 10:12:19 +02002995 depth = 0;
Jens Axboe18811272011-04-12 10:11:24 +02002996
2997 /*
2998 * Save and disable interrupts here, to avoid doing it for every
2999 * queue lock we have to take.
3000 */
Jens Axboe73c10102011-03-08 13:19:51 +01003001 local_irq_save(flags);
NeilBrown109b8122011-04-11 14:13:10 +02003002 while (!list_empty(&list)) {
3003 rq = list_entry_rq(list.next);
Jens Axboe73c10102011-03-08 13:19:51 +01003004 list_del_init(&rq->queuelist);
Jens Axboe73c10102011-03-08 13:19:51 +01003005 BUG_ON(!rq->q);
3006 if (rq->q != q) {
Jens Axboe99e22592011-04-18 09:59:55 +02003007 /*
3008 * This drops the queue lock
3009 */
3010 if (q)
Jens Axboe49cac012011-04-16 13:51:05 +02003011 queue_unplugged(q, depth, from_schedule);
Jens Axboe73c10102011-03-08 13:19:51 +01003012 q = rq->q;
Jens Axboe94b5eb22011-04-12 10:12:19 +02003013 depth = 0;
Jens Axboe73c10102011-03-08 13:19:51 +01003014 spin_lock(q->queue_lock);
3015 }
Tejun Heo8ba61432011-12-14 00:33:37 +01003016
3017 /*
3018 * Short-circuit if @q is dead
3019 */
3020 if (unlikely(blk_queue_dead(q))) {
3021 __blk_end_request_all(rq, -ENODEV);
3022 continue;
3023 }
3024
Jens Axboe73c10102011-03-08 13:19:51 +01003025 /*
3026 * rq is already accounted, so use raw insert
3027 */
Jens Axboe401a18e2011-03-25 16:57:52 +01003028 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA))
3029 __elv_add_request(q, rq, ELEVATOR_INSERT_FLUSH);
3030 else
3031 __elv_add_request(q, rq, ELEVATOR_INSERT_SORT_MERGE);
Jens Axboe94b5eb22011-04-12 10:12:19 +02003032
3033 depth++;
Jens Axboe73c10102011-03-08 13:19:51 +01003034 }
3035
Jens Axboe99e22592011-04-18 09:59:55 +02003036 /*
3037 * This drops the queue lock
3038 */
3039 if (q)
Jens Axboe49cac012011-04-16 13:51:05 +02003040 queue_unplugged(q, depth, from_schedule);
Jens Axboe73c10102011-03-08 13:19:51 +01003041
Jens Axboe73c10102011-03-08 13:19:51 +01003042 local_irq_restore(flags);
3043}
Jens Axboe73c10102011-03-08 13:19:51 +01003044
3045void blk_finish_plug(struct blk_plug *plug)
3046{
Jens Axboef6603782011-04-15 15:49:07 +02003047 blk_flush_plug_list(plug, false);
Christoph Hellwig88b996c2011-04-15 15:20:10 +02003048
3049 if (plug == current->plug)
3050 current->plug = NULL;
Jens Axboe73c10102011-03-08 13:19:51 +01003051}
3052EXPORT_SYMBOL(blk_finish_plug);
3053
Lin Mingff70c822013-03-23 11:42:26 +08003054#ifdef CONFIG_PM_RUNTIME
3055/**
3056 * blk_pm_runtime_init - Block layer runtime PM initialization routine
3057 * @q: the queue of the device
3058 * @dev: the device the queue belongs to
3059 *
3060 * Description:
3061 * Initialize runtime-PM-related fields for @q and start auto suspend for
3062 * @dev. Drivers that want to take advantage of request-based runtime PM
3063 * should call this function after @dev has been initialized, and its
3064 * request queue @q has been allocated, and runtime PM for it can not happen
3065 * yet(either due to disabled/forbidden or its usage_count > 0). In most
3066 * cases, driver should call this function before any I/O has taken place.
3067 *
3068 * This function takes care of setting up using auto suspend for the device,
3069 * the autosuspend delay is set to -1 to make runtime suspend impossible
3070 * until an updated value is either set by user or by driver. Drivers do
3071 * not need to touch other autosuspend settings.
3072 *
3073 * The block layer runtime PM is request based, so only works for drivers
3074 * that use request as their IO unit instead of those directly use bio's.
3075 */
3076void blk_pm_runtime_init(struct request_queue *q, struct device *dev)
3077{
3078 q->dev = dev;
3079 q->rpm_status = RPM_ACTIVE;
3080 pm_runtime_set_autosuspend_delay(q->dev, -1);
3081 pm_runtime_use_autosuspend(q->dev);
3082}
3083EXPORT_SYMBOL(blk_pm_runtime_init);
3084
3085/**
3086 * blk_pre_runtime_suspend - Pre runtime suspend check
3087 * @q: the queue of the device
3088 *
3089 * Description:
3090 * This function will check if runtime suspend is allowed for the device
3091 * by examining if there are any requests pending in the queue. If there
3092 * are requests pending, the device can not be runtime suspended; otherwise,
3093 * the queue's status will be updated to SUSPENDING and the driver can
3094 * proceed to suspend the device.
3095 *
3096 * For the not allowed case, we mark last busy for the device so that
3097 * runtime PM core will try to autosuspend it some time later.
3098 *
3099 * This function should be called near the start of the device's
3100 * runtime_suspend callback.
3101 *
3102 * Return:
3103 * 0 - OK to runtime suspend the device
3104 * -EBUSY - Device should not be runtime suspended
3105 */
3106int blk_pre_runtime_suspend(struct request_queue *q)
3107{
3108 int ret = 0;
3109
3110 spin_lock_irq(q->queue_lock);
3111 if (q->nr_pending) {
3112 ret = -EBUSY;
3113 pm_runtime_mark_last_busy(q->dev);
3114 } else {
3115 q->rpm_status = RPM_SUSPENDING;
3116 }
3117 spin_unlock_irq(q->queue_lock);
3118 return ret;
3119}
3120EXPORT_SYMBOL(blk_pre_runtime_suspend);
3121
3122/**
3123 * blk_post_runtime_suspend - Post runtime suspend processing
3124 * @q: the queue of the device
3125 * @err: return value of the device's runtime_suspend function
3126 *
3127 * Description:
3128 * Update the queue's runtime status according to the return value of the
3129 * device's runtime suspend function and mark last busy for the device so
3130 * that PM core will try to auto suspend the device at a later time.
3131 *
3132 * This function should be called near the end of the device's
3133 * runtime_suspend callback.
3134 */
3135void blk_post_runtime_suspend(struct request_queue *q, int err)
3136{
3137 spin_lock_irq(q->queue_lock);
3138 if (!err) {
3139 q->rpm_status = RPM_SUSPENDED;
3140 } else {
3141 q->rpm_status = RPM_ACTIVE;
3142 pm_runtime_mark_last_busy(q->dev);
3143 }
3144 spin_unlock_irq(q->queue_lock);
3145}
3146EXPORT_SYMBOL(blk_post_runtime_suspend);
3147
3148/**
3149 * blk_pre_runtime_resume - Pre runtime resume processing
3150 * @q: the queue of the device
3151 *
3152 * Description:
3153 * Update the queue's runtime status to RESUMING in preparation for the
3154 * runtime resume of the device.
3155 *
3156 * This function should be called near the start of the device's
3157 * runtime_resume callback.
3158 */
3159void blk_pre_runtime_resume(struct request_queue *q)
3160{
3161 spin_lock_irq(q->queue_lock);
3162 q->rpm_status = RPM_RESUMING;
3163 spin_unlock_irq(q->queue_lock);
3164}
3165EXPORT_SYMBOL(blk_pre_runtime_resume);
3166
3167/**
3168 * blk_post_runtime_resume - Post runtime resume processing
3169 * @q: the queue of the device
3170 * @err: return value of the device's runtime_resume function
3171 *
3172 * Description:
3173 * Update the queue's runtime status according to the return value of the
3174 * device's runtime_resume function. If it is successfully resumed, process
3175 * the requests that are queued into the device's queue when it is resuming
3176 * and then mark last busy and initiate autosuspend for it.
3177 *
3178 * This function should be called near the end of the device's
3179 * runtime_resume callback.
3180 */
3181void blk_post_runtime_resume(struct request_queue *q, int err)
3182{
3183 spin_lock_irq(q->queue_lock);
3184 if (!err) {
3185 q->rpm_status = RPM_ACTIVE;
3186 __blk_run_queue(q);
3187 pm_runtime_mark_last_busy(q->dev);
Aaron Lu0ed75912013-05-17 15:47:20 +08003188 pm_request_autosuspend(q->dev);
Lin Mingff70c822013-03-23 11:42:26 +08003189 } else {
3190 q->rpm_status = RPM_SUSPENDED;
3191 }
3192 spin_unlock_irq(q->queue_lock);
3193}
3194EXPORT_SYMBOL(blk_post_runtime_resume);
3195#endif
3196
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197int __init blk_dev_init(void)
3198{
Nikanth Karthikesan9eb55b02009-04-27 14:53:54 +02003199 BUILD_BUG_ON(__REQ_NR_BITS > 8 *
3200 sizeof(((struct request *)0)->cmd_flags));
3201
Tejun Heo89b90be2011-01-03 15:01:47 +01003202 /* used for unplugging and affects IO latency/throughput - HIGHPRI */
3203 kblockd_workqueue = alloc_workqueue("kblockd",
3204 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 if (!kblockd_workqueue)
3206 panic("Failed to create kblockd\n");
3207
3208 request_cachep = kmem_cache_create("blkdev_requests",
Paul Mundt20c2df82007-07-20 10:11:58 +09003209 sizeof(struct request), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
Jens Axboe8324aa92008-01-29 14:51:59 +01003211 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02003212 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 return 0;
3215}