blob: 04604cfc74516a81be3cecfa951bad3606cd1c0a [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>
Li Zefan55782132009-06-09 13:43:05 +080032
33#define CREATE_TRACE_POINTS
34#include <trace/events/block.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Jens Axboe8324aa92008-01-29 14:51:59 +010036#include "blk.h"
37
Mike Snitzerd07335e2010-11-16 12:52:38 +010038EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_remap);
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +020039EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_remap);
Li Zefan55782132009-06-09 13:43:05 +080040EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_complete);
Ingo Molnar0bfc2452008-11-26 11:59:56 +010041
Tejun Heoa73f7302011-12-14 00:33:37 +010042DEFINE_IDA(blk_queue_ida);
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*
45 * For the allocated request tables
46 */
Adrian Bunk5ece6c52008-02-18 13:45:51 +010047static struct kmem_cache *request_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/*
50 * For queue allocation
51 */
Jens Axboe6728cb02008-01-31 13:03:55 +010052struct kmem_cache *blk_requestq_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 * Controlling structure to kblockd
56 */
Jens Axboeff856ba2006-01-09 16:02:34 +010057static struct workqueue_struct *kblockd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Jens Axboe26b82562008-01-29 13:54:41 +010059static void drive_stat_acct(struct request *rq, int new_io)
60{
Jens Axboe28f13702008-05-07 10:15:46 +020061 struct hd_struct *part;
Jens Axboe26b82562008-01-29 13:54:41 +010062 int rw = rq_data_dir(rq);
Tejun Heoc9959052008-08-25 19:47:21 +090063 int cpu;
Jens Axboe26b82562008-01-29 13:54:41 +010064
Jens Axboec2553b52009-04-24 08:10:11 +020065 if (!blk_do_io_stat(rq))
Jens Axboe26b82562008-01-29 13:54:41 +010066 return;
67
Tejun Heo074a7ac2008-08-25 19:56:14 +090068 cpu = part_stat_lock();
Tejun Heoc9959052008-08-25 19:47:21 +090069
Jerome Marchand09e099d2011-01-05 16:57:38 +010070 if (!new_io) {
71 part = rq->part;
Tejun Heo074a7ac2008-08-25 19:56:14 +090072 part_stat_inc(cpu, part, merges[rw]);
Jerome Marchand09e099d2011-01-05 16:57:38 +010073 } else {
74 part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq));
Jens Axboe6c23a962011-01-07 08:43:37 +010075 if (!hd_struct_try_get(part)) {
Jerome Marchand09e099d2011-01-05 16:57:38 +010076 /*
77 * The partition is already being removed,
78 * the request will be accounted on the disk only
79 *
80 * We take a reference on disk->part0 although that
81 * partition will never be deleted, so we can treat
82 * it as any other partition.
83 */
84 part = &rq->rq_disk->part0;
Jens Axboe6c23a962011-01-07 08:43:37 +010085 hd_struct_get(part);
Jerome Marchand09e099d2011-01-05 16:57:38 +010086 }
Tejun Heo074a7ac2008-08-25 19:56:14 +090087 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +020088 part_inc_in_flight(part, rw);
Jerome Marchand09e099d2011-01-05 16:57:38 +010089 rq->part = part;
Jens Axboe26b82562008-01-29 13:54:41 +010090 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +020091
Tejun Heo074a7ac2008-08-25 19:56:14 +090092 part_stat_unlock();
Jens Axboe26b82562008-01-29 13:54:41 +010093}
94
Jens Axboe8324aa92008-01-29 14:51:59 +010095void blk_queue_congestion_threshold(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 int nr;
98
99 nr = q->nr_requests - (q->nr_requests / 8) + 1;
100 if (nr > q->nr_requests)
101 nr = q->nr_requests;
102 q->nr_congestion_on = nr;
103
104 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
105 if (nr < 1)
106 nr = 1;
107 q->nr_congestion_off = nr;
108}
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110/**
111 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
112 * @bdev: device
113 *
114 * Locates the passed device's request queue and returns the address of its
115 * backing_dev_info
116 *
117 * Will return NULL if the request queue cannot be located.
118 */
119struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
120{
121 struct backing_dev_info *ret = NULL;
Jens Axboe165125e2007-07-24 09:28:11 +0200122 struct request_queue *q = bdev_get_queue(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 if (q)
125 ret = &q->backing_dev_info;
126 return ret;
127}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128EXPORT_SYMBOL(blk_get_backing_dev_info);
129
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200130void blk_rq_init(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200132 memset(rq, 0, sizeof(*rq));
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 INIT_LIST_HEAD(&rq->queuelist);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700135 INIT_LIST_HEAD(&rq->timeout_list);
Jens Axboec7c22e42008-09-13 20:26:01 +0200136 rq->cpu = -1;
Jens Axboe63a71382008-02-08 12:41:03 +0100137 rq->q = q;
Tejun Heoa2dec7b2009-05-07 22:24:44 +0900138 rq->__sector = (sector_t) -1;
Jens Axboe2e662b62006-07-13 11:55:04 +0200139 INIT_HLIST_NODE(&rq->hash);
140 RB_CLEAR_NODE(&rq->rb_node);
FUJITA Tomonorid7e3c322008-04-29 09:54:39 +0200141 rq->cmd = rq->__cmd;
Li Zefane2494e12009-04-02 13:43:26 +0800142 rq->cmd_len = BLK_MAX_CDB;
Jens Axboe63a71382008-02-08 12:41:03 +0100143 rq->tag = -1;
Jens Axboe63a71382008-02-08 12:41:03 +0100144 rq->ref_count = 1;
Tejun Heob243ddc2009-04-23 11:05:18 +0900145 rq->start_time = jiffies;
Divyesh Shah91952912010-04-01 15:01:41 -0700146 set_start_time_ns(rq);
Jerome Marchand09e099d2011-01-05 16:57:38 +0100147 rq->part = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200149EXPORT_SYMBOL(blk_rq_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
NeilBrown5bb23a62007-09-27 12:46:13 +0200151static void req_bio_endio(struct request *rq, struct bio *bio,
152 unsigned int nbytes, int error)
Tejun Heo797e7db2006-01-06 09:51:03 +0100153{
Tejun Heo143a87f2011-01-25 12:43:52 +0100154 if (error)
155 clear_bit(BIO_UPTODATE, &bio->bi_flags);
156 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
157 error = -EIO;
Tejun Heo797e7db2006-01-06 09:51:03 +0100158
Tejun Heo143a87f2011-01-25 12:43:52 +0100159 if (unlikely(nbytes > bio->bi_size)) {
160 printk(KERN_ERR "%s: want %u bytes done, %u left\n",
161 __func__, nbytes, bio->bi_size);
162 nbytes = bio->bi_size;
NeilBrown5bb23a62007-09-27 12:46:13 +0200163 }
Tejun Heo143a87f2011-01-25 12:43:52 +0100164
165 if (unlikely(rq->cmd_flags & REQ_QUIET))
166 set_bit(BIO_QUIET, &bio->bi_flags);
167
168 bio->bi_size -= nbytes;
169 bio->bi_sector += (nbytes >> 9);
170
171 if (bio_integrity(bio))
172 bio_integrity_advance(bio, nbytes);
173
174 /* don't actually finish bio if it's part of flush sequence */
175 if (bio->bi_size == 0 && !(rq->cmd_flags & REQ_FLUSH_SEQ))
176 bio_endio(bio, error);
Tejun Heo797e7db2006-01-06 09:51:03 +0100177}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179void blk_dump_rq_flags(struct request *rq, char *msg)
180{
181 int bit;
182
Jens Axboe6728cb02008-01-31 13:03:55 +0100183 printk(KERN_INFO "%s: dev %s: type=%x, flags=%x\n", msg,
Jens Axboe4aff5e22006-08-10 08:44:47 +0200184 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
185 rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Tejun Heo83096eb2009-05-07 22:24:39 +0900187 printk(KERN_INFO " sector %llu, nr/cnr %u/%u\n",
188 (unsigned long long)blk_rq_pos(rq),
189 blk_rq_sectors(rq), blk_rq_cur_sectors(rq));
Tejun Heo731ec492009-04-23 11:05:20 +0900190 printk(KERN_INFO " bio %p, biotail %p, buffer %p, len %u\n",
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900191 rq->bio, rq->biotail, rq->buffer, blk_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200193 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
Jens Axboe6728cb02008-01-31 13:03:55 +0100194 printk(KERN_INFO " cdb: ");
FUJITA Tomonorid34c87e2008-04-29 14:37:52 +0200195 for (bit = 0; bit < BLK_MAX_CDB; bit++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 printk("%02x ", rq->cmd[bit]);
197 printk("\n");
198 }
199}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200EXPORT_SYMBOL(blk_dump_rq_flags);
201
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500202static void blk_delay_work(struct work_struct *work)
Jens Axboe6c5e0c42008-08-01 20:31:32 +0200203{
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500204 struct request_queue *q;
Jens Axboe6c5e0c42008-08-01 20:31:32 +0200205
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500206 q = container_of(work, struct request_queue, delay_work.work);
207 spin_lock_irq(q->queue_lock);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200208 __blk_run_queue(q);
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500209 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212/**
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500213 * blk_delay_queue - restart queueing after defined interval
214 * @q: The &struct request_queue in question
215 * @msecs: Delay in msecs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 *
217 * Description:
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500218 * Sometimes queueing needs to be postponed for a little while, to allow
219 * resources to come back. This function will make sure that queueing is
220 * restarted around the specified time.
221 */
222void blk_delay_queue(struct request_queue *q, unsigned long msecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Jens Axboe4521cc42011-04-18 11:36:39 +0200224 queue_delayed_work(kblockd_workqueue, &q->delay_work,
225 msecs_to_jiffies(msecs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500227EXPORT_SYMBOL(blk_delay_queue);
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229/**
230 * blk_start_queue - restart a previously stopped queue
Jens Axboe165125e2007-07-24 09:28:11 +0200231 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 *
233 * Description:
234 * blk_start_queue() will clear the stop flag on the queue, and call
235 * the request_fn for the queue if it was in a stopped state when
236 * entered. Also see blk_stop_queue(). Queue lock must be held.
237 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200238void blk_start_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200240 WARN_ON(!irqs_disabled());
241
Nick Piggin75ad23b2008-04-29 14:48:33 +0200242 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200243 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245EXPORT_SYMBOL(blk_start_queue);
246
247/**
248 * blk_stop_queue - stop a queue
Jens Axboe165125e2007-07-24 09:28:11 +0200249 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 *
251 * Description:
252 * The Linux block layer assumes that a block driver will consume all
253 * entries on the request queue when the request_fn strategy is called.
254 * Often this will not happen, because of hardware limitations (queue
255 * depth settings). If a device driver gets a 'queue full' response,
256 * or if it simply chooses not to queue more I/O at one point, it can
257 * call this function to prevent the request_fn from being called until
258 * the driver has signalled it's ready to go again. This happens by calling
259 * blk_start_queue() to restart queue operations. Queue lock must be held.
260 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200261void blk_stop_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Jens Axboead3d9d72011-03-25 16:58:59 +0100263 __cancel_delayed_work(&q->delay_work);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200264 queue_flag_set(QUEUE_FLAG_STOPPED, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266EXPORT_SYMBOL(blk_stop_queue);
267
268/**
269 * blk_sync_queue - cancel any pending callbacks on a queue
270 * @q: the queue
271 *
272 * Description:
273 * The block layer may perform asynchronous callback activity
274 * on a queue, such as calling the unplug function after a timeout.
275 * A block device may call blk_sync_queue to ensure that any
276 * such activity is cancelled, thus allowing it to release resources
Michael Opdenacker59c51592007-05-09 08:57:56 +0200277 * that the callbacks might use. The caller must already have made sure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 * that its ->make_request_fn will not re-add plugging prior to calling
279 * this function.
280 *
Vivek Goyalda527772011-03-02 19:05:33 -0500281 * This function does not cancel any asynchronous activity arising
282 * out of elevator or throttling code. That would require elevaotor_exit()
283 * and blk_throtl_exit() to be called with queue lock initialized.
284 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 */
286void blk_sync_queue(struct request_queue *q)
287{
Jens Axboe70ed28b2008-11-19 14:38:39 +0100288 del_timer_sync(&q->timeout);
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500289 cancel_delayed_work_sync(&q->delay_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291EXPORT_SYMBOL(blk_sync_queue);
292
293/**
Jens Axboe80a4b582008-10-14 09:51:06 +0200294 * __blk_run_queue - run a single device queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200296 *
297 * Description:
298 * See @blk_run_queue. This variant must be called with the queue lock
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200299 * held and interrupts disabled.
Tatyana Brokhman88fd1b42012-12-04 16:04:15 +0200300 * Device driver will be notified of an urgent request
301 * pending under the following conditions:
302 * 1. The driver and the current scheduler support urgent reques handling
303 * 2. There is an urgent request pending in the scheduler
304 * 3. There isn't already an urgent request in flight, meaning previously
305 * notified urgent request completed (!q->notified_urgent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 */
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200307void __blk_run_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Tejun Heoa538cd02009-04-23 11:05:17 +0900309 if (unlikely(blk_queue_stopped(q)))
310 return;
311
Tatyana Brokhman88fd1b42012-12-04 16:04:15 +0200312 if (!q->notified_urgent &&
313 q->elevator->type->ops.elevator_is_urgent_fn &&
314 q->urgent_request_fn &&
315 q->elevator->type->ops.elevator_is_urgent_fn(q)) {
316 q->notified_urgent = true;
317 q->urgent_request_fn(q);
318 } else
319 q->request_fn(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200320}
321EXPORT_SYMBOL(__blk_run_queue);
Jens Axboedac07ec2006-05-11 08:20:16 +0200322
Nick Piggin75ad23b2008-04-29 14:48:33 +0200323/**
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200324 * blk_run_queue_async - run a single device queue in workqueue context
325 * @q: The queue to run
326 *
327 * Description:
328 * Tells kblockd to perform the equivalent of @blk_run_queue on behalf
329 * of us.
330 */
331void blk_run_queue_async(struct request_queue *q)
332{
Shaohua Li3ec717b2011-05-18 11:22:43 +0200333 if (likely(!blk_queue_stopped(q))) {
334 __cancel_delayed_work(&q->delay_work);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200335 queue_delayed_work(kblockd_workqueue, &q->delay_work, 0);
Shaohua Li3ec717b2011-05-18 11:22:43 +0200336 }
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200337}
Jens Axboec21e6be2011-04-19 13:32:46 +0200338EXPORT_SYMBOL(blk_run_queue_async);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200339
340/**
Nick Piggin75ad23b2008-04-29 14:48:33 +0200341 * blk_run_queue - run a single device queue
342 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200343 *
344 * Description:
345 * Invoke request handling on this queue, if it has pending work to do.
Tejun Heoa7f55792009-04-23 11:05:17 +0900346 * May be used to restart queueing when a request has completed.
Nick Piggin75ad23b2008-04-29 14:48:33 +0200347 */
348void blk_run_queue(struct request_queue *q)
349{
350 unsigned long flags;
351
352 spin_lock_irqsave(q->queue_lock, flags);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200353 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 spin_unlock_irqrestore(q->queue_lock, flags);
355}
356EXPORT_SYMBOL(blk_run_queue);
357
Jens Axboe165125e2007-07-24 09:28:11 +0200358void blk_put_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500359{
360 kobject_put(&q->kobj);
361}
Jens Axboed86e0e82011-05-27 07:44:43 +0200362EXPORT_SYMBOL(blk_put_queue);
Al Viro483f4af2006-03-18 18:34:37 -0500363
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200364/**
365 * blk_drain_queue - drain requests from request_queue
366 * @q: queue to drain
Tejun Heoc9a929d2011-10-19 14:42:16 +0200367 * @drain_all: whether to drain all requests or only the ones w/ ELVPRIV
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200368 *
Tejun Heoc9a929d2011-10-19 14:42:16 +0200369 * Drain requests from @q. If @drain_all is set, all requests are drained.
370 * If not, only ELVPRIV requests are drained. The caller is responsible
371 * for ensuring that no new requests which need to be drained are queued.
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200372 */
Tejun Heoc9a929d2011-10-19 14:42:16 +0200373void blk_drain_queue(struct request_queue *q, bool drain_all)
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200374{
375 while (true) {
Tejun Heo481a7d62011-12-14 00:33:37 +0100376 bool drain = false;
377 int i;
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200378
379 spin_lock_irq(q->queue_lock);
380
381 elv_drain_elevator(q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200382 if (drain_all)
383 blk_throtl_drain(q);
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200384
Tejun Heo4eabc942011-12-15 20:03:04 +0100385 /*
386 * This function might be called on a queue which failed
387 * driver init after queue creation. Some drivers
388 * (e.g. fd) get unhappy in such cases. Kick queue iff
389 * dispatch queue has something on it.
390 */
391 if (!list_empty(&q->queue_head))
392 __blk_run_queue(q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200393
Tejun Heo481a7d62011-12-14 00:33:37 +0100394 drain |= q->rq.elvpriv;
395
396 /*
397 * Unfortunately, requests are queued at and tracked from
398 * multiple places and there's no single counter which can
399 * be drained. Check all the queues and counters.
400 */
401 if (drain_all) {
402 drain |= !list_empty(&q->queue_head);
403 for (i = 0; i < 2; i++) {
404 drain |= q->rq.count[i];
405 drain |= q->in_flight[i];
406 drain |= !list_empty(&q->flush_queue[i]);
407 }
408 }
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200409
410 spin_unlock_irq(q->queue_lock);
411
Tejun Heo481a7d62011-12-14 00:33:37 +0100412 if (!drain)
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200413 break;
414 msleep(10);
415 }
416}
417
Tejun Heoc9a929d2011-10-19 14:42:16 +0200418/**
419 * blk_cleanup_queue - shutdown a request queue
420 * @q: request queue to shutdown
421 *
422 * Mark @q DEAD, drain all pending requests, destroy and put it. All
423 * future requests will be failed immediately with -ENODEV.
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500424 */
Jens Axboe6728cb02008-01-31 13:03:55 +0100425void blk_cleanup_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500426{
Tejun Heoc9a929d2011-10-19 14:42:16 +0200427 spinlock_t *lock = q->queue_lock;
Jens Axboee3335de2008-09-18 09:22:54 -0700428
Tejun Heoc9a929d2011-10-19 14:42:16 +0200429 /* mark @q DEAD, no new request or merges will be allowed afterwards */
Al Viro483f4af2006-03-18 18:34:37 -0500430 mutex_lock(&q->sysfs_lock);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200431 queue_flag_set_unlocked(QUEUE_FLAG_DEAD, q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200432
433 spin_lock_irq(lock);
434 queue_flag_set(QUEUE_FLAG_NOMERGES, q);
435 queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
436 queue_flag_set(QUEUE_FLAG_DEAD, q);
Al Viro483f4af2006-03-18 18:34:37 -0500437
Hannes Reinecke777eb1b2011-09-28 08:07:01 -0600438 if (q->queue_lock != &q->__queue_lock)
439 q->queue_lock = &q->__queue_lock;
Vivek Goyalda527772011-03-02 19:05:33 -0500440
Tejun Heoc9a929d2011-10-19 14:42:16 +0200441 spin_unlock_irq(lock);
442 mutex_unlock(&q->sysfs_lock);
443
Tejun Heo6dd9ad72011-11-03 18:52:11 +0100444 /*
445 * Drain all requests queued before DEAD marking. The caller might
446 * be trying to tear down @q before its elevator is initialized, in
447 * which case we don't want to call into draining.
448 */
449 if (q->elevator)
450 blk_drain_queue(q, true);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200451
452 /* @q won't process any more request, flush async actions */
453 del_timer_sync(&q->backing_dev_info.laptop_mode_wb_timer);
454 blk_sync_queue(q);
455
456 /* @q is and will stay empty, shutdown and put */
Al Viro483f4af2006-03-18 18:34:37 -0500457 blk_put_queue(q);
458}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459EXPORT_SYMBOL(blk_cleanup_queue);
460
Jens Axboe165125e2007-07-24 09:28:11 +0200461static int blk_init_free_list(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
463 struct request_list *rl = &q->rq;
464
Mike Snitzer1abec4f2010-05-25 13:15:15 -0400465 if (unlikely(rl->rq_pool))
466 return 0;
467
Jens Axboe1faa16d2009-04-06 14:48:01 +0200468 rl->count[BLK_RW_SYNC] = rl->count[BLK_RW_ASYNC] = 0;
469 rl->starved[BLK_RW_SYNC] = rl->starved[BLK_RW_ASYNC] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200470 rl->elvpriv = 0;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200471 init_waitqueue_head(&rl->wait[BLK_RW_SYNC]);
472 init_waitqueue_head(&rl->wait[BLK_RW_ASYNC]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Christoph Lameter19460892005-06-23 00:08:19 -0700474 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
475 mempool_free_slab, request_cachep, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 if (!rl->rq_pool)
478 return -ENOMEM;
479
480 return 0;
481}
482
Jens Axboe165125e2007-07-24 09:28:11 +0200483struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Christoph Lameter19460892005-06-23 00:08:19 -0700485 return blk_alloc_queue_node(gfp_mask, -1);
486}
487EXPORT_SYMBOL(blk_alloc_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Jens Axboe165125e2007-07-24 09:28:11 +0200489struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -0700490{
Jens Axboe165125e2007-07-24 09:28:11 +0200491 struct request_queue *q;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700492 int err;
Christoph Lameter19460892005-06-23 00:08:19 -0700493
Jens Axboe8324aa92008-01-29 14:51:59 +0100494 q = kmem_cache_alloc_node(blk_requestq_cachep,
Christoph Lameter94f60302007-07-17 04:03:29 -0700495 gfp_mask | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (!q)
497 return NULL;
498
Dan Carpenter00380a42012-03-23 09:58:54 +0100499 q->id = ida_simple_get(&blk_queue_ida, 0, 0, gfp_mask);
Tejun Heoa73f7302011-12-14 00:33:37 +0100500 if (q->id < 0)
501 goto fail_q;
502
Jens Axboe0989a022009-06-12 14:42:56 +0200503 q->backing_dev_info.ra_pages =
504 (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
505 q->backing_dev_info.state = 0;
506 q->backing_dev_info.capabilities = BDI_CAP_MAP_COPY;
Jens Axboed9938312009-06-12 14:45:52 +0200507 q->backing_dev_info.name = "block";
Mike Snitzer51514122011-11-23 10:59:13 +0100508 q->node = node_id;
Jens Axboe0989a022009-06-12 14:42:56 +0200509
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700510 err = bdi_init(&q->backing_dev_info);
Tejun Heoa73f7302011-12-14 00:33:37 +0100511 if (err)
512 goto fail_id;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700513
Tejun Heoa73f7302011-12-14 00:33:37 +0100514 if (blk_throtl_init(q))
515 goto fail_id;
Vivek Goyale43473b2010-09-15 17:06:35 -0400516
Matthew Garrett31373d02010-04-06 14:25:14 +0200517 setup_timer(&q->backing_dev_info.laptop_mode_wb_timer,
518 laptop_mode_timer_fn, (unsigned long) q);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700519 setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
520 INIT_LIST_HEAD(&q->timeout_list);
Tejun Heoa612fdd2011-12-14 00:33:41 +0100521 INIT_LIST_HEAD(&q->icq_list);
Tejun Heoae1b1532011-01-25 12:43:54 +0100522 INIT_LIST_HEAD(&q->flush_queue[0]);
523 INIT_LIST_HEAD(&q->flush_queue[1]);
524 INIT_LIST_HEAD(&q->flush_data_in_flight);
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500525 INIT_DELAYED_WORK(&q->delay_work, blk_delay_work);
Al Viro483f4af2006-03-18 18:34:37 -0500526
Jens Axboe8324aa92008-01-29 14:51:59 +0100527 kobject_init(&q->kobj, &blk_queue_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Al Viro483f4af2006-03-18 18:34:37 -0500529 mutex_init(&q->sysfs_lock);
Neil Browne7e72bf2008-05-14 16:05:54 -0700530 spin_lock_init(&q->__queue_lock);
Al Viro483f4af2006-03-18 18:34:37 -0500531
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500532 /*
533 * By default initialize queue_lock to internal lock and driver can
534 * override it later if need be.
535 */
536 q->queue_lock = &q->__queue_lock;
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return q;
Tejun Heoa73f7302011-12-14 00:33:37 +0100539
540fail_id:
541 ida_simple_remove(&blk_queue_ida, q->id);
542fail_q:
543 kmem_cache_free(blk_requestq_cachep, q);
544 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
Christoph Lameter19460892005-06-23 00:08:19 -0700546EXPORT_SYMBOL(blk_alloc_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548/**
549 * blk_init_queue - prepare a request queue for use with a block device
550 * @rfn: The function to be called to process requests that have been
551 * placed on the queue.
552 * @lock: Request queue spin lock
553 *
554 * Description:
555 * If a block device wishes to use the standard request handling procedures,
556 * which sorts requests and coalesces adjacent requests, then it must
557 * call blk_init_queue(). The function @rfn will be called when there
558 * are requests on the queue that need to be processed. If the device
559 * supports plugging, then @rfn may not be called immediately when requests
560 * are available on the queue, but may be called at some time later instead.
561 * Plugged queues are generally unplugged when a buffer belonging to one
562 * of the requests on the queue is needed, or due to memory pressure.
563 *
564 * @rfn is not required, or even expected, to remove all requests off the
565 * queue, but only as many as it can handle at a time. If it does leave
566 * requests on the queue, it is responsible for arranging that the requests
567 * get dealt with eventually.
568 *
569 * The queue spin lock must be held while manipulating the requests on the
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200570 * request queue; this lock will be taken also from interrupt context, so irq
571 * disabling is needed for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 *
Randy Dunlap710027a2008-08-19 20:13:11 +0200573 * Function returns a pointer to the initialized request queue, or %NULL if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 * it didn't succeed.
575 *
576 * Note:
577 * blk_init_queue() must be paired with a blk_cleanup_queue() call
578 * when the block device is deactivated (such as at module unload).
579 **/
Christoph Lameter19460892005-06-23 00:08:19 -0700580
Jens Axboe165125e2007-07-24 09:28:11 +0200581struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Christoph Lameter19460892005-06-23 00:08:19 -0700583 return blk_init_queue_node(rfn, lock, -1);
584}
585EXPORT_SYMBOL(blk_init_queue);
586
Jens Axboe165125e2007-07-24 09:28:11 +0200587struct request_queue *
Christoph Lameter19460892005-06-23 00:08:19 -0700588blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
589{
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600590 struct request_queue *uninit_q, *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600592 uninit_q = blk_alloc_queue_node(GFP_KERNEL, node_id);
593 if (!uninit_q)
594 return NULL;
595
Mike Snitzer51514122011-11-23 10:59:13 +0100596 q = blk_init_allocated_queue(uninit_q, rfn, lock);
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600597 if (!q)
598 blk_cleanup_queue(uninit_q);
599
600 return q;
Mike Snitzer01effb02010-05-11 08:57:42 +0200601}
602EXPORT_SYMBOL(blk_init_queue_node);
603
604struct request_queue *
605blk_init_allocated_queue(struct request_queue *q, request_fn_proc *rfn,
606 spinlock_t *lock)
607{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (!q)
609 return NULL;
610
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600611 if (blk_init_free_list(q))
Al Viro8669aaf2006-03-18 13:50:00 -0500612 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 q->request_fn = rfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 q->prep_rq_fn = NULL;
James Bottomley28018c22010-07-01 19:49:17 +0900616 q->unprep_rq_fn = NULL;
Jens Axboebc58ba92009-01-23 10:54:44 +0100617 q->queue_flags = QUEUE_FLAG_DEFAULT;
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500618
619 /* Override internal queue lock with supplied lock pointer */
620 if (lock)
621 q->queue_lock = lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Jens Axboef3b144a2009-03-06 08:48:33 +0100623 /*
624 * This also sets hw/phys segments, boundary and size
625 */
Jens Axboec20e8de2011-09-12 12:03:37 +0200626 blk_queue_make_request(q, blk_queue_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Alan Stern44ec9542007-02-20 11:01:57 -0500628 q->sg_reserved_size = INT_MAX;
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 /*
631 * all done
632 */
633 if (!elevator_init(q, NULL)) {
634 blk_queue_congestion_threshold(q);
635 return q;
636 }
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return NULL;
639}
Mike Snitzer51514122011-11-23 10:59:13 +0100640EXPORT_SYMBOL(blk_init_allocated_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Tejun Heo09ac46c2011-12-14 00:33:38 +0100642bool blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
Tejun Heo34f60552011-12-14 00:33:37 +0100644 if (likely(!blk_queue_dead(q))) {
Tejun Heo09ac46c2011-12-14 00:33:38 +0100645 __blk_get_queue(q);
646 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
648
Tejun Heo09ac46c2011-12-14 00:33:38 +0100649 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
Jens Axboed86e0e82011-05-27 07:44:43 +0200651EXPORT_SYMBOL(blk_get_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Jens Axboe165125e2007-07-24 09:28:11 +0200653static inline void blk_free_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
Tejun Heof1f8cc92011-12-14 00:33:42 +0100655 if (rq->cmd_flags & REQ_ELVPRIV) {
Tejun Heocb98fc82005-10-28 08:29:39 +0200656 elv_put_request(q, rq);
Tejun Heof1f8cc92011-12-14 00:33:42 +0100657 if (rq->elv.icq)
Tejun Heo11a31222012-02-07 07:51:30 +0100658 put_io_context(rq->elv.icq->ioc);
Tejun Heof1f8cc92011-12-14 00:33:42 +0100659 }
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 mempool_free(rq, q->rq.rq_pool);
662}
663
Jens Axboe1ea25ec2006-07-18 22:24:11 +0200664static struct request *
Tejun Heof1f8cc92011-12-14 00:33:42 +0100665blk_alloc_request(struct request_queue *q, struct io_cq *icq,
666 unsigned int flags, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
669
670 if (!rq)
671 return NULL;
672
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200673 blk_rq_init(q, rq);
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200674
Jerome Marchand42dad762009-04-22 14:01:49 +0200675 rq->cmd_flags = flags | REQ_ALLOCED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Tejun Heof1f8cc92011-12-14 00:33:42 +0100677 if (flags & REQ_ELVPRIV) {
678 rq->elv.icq = icq;
679 if (unlikely(elv_set_request(q, rq, gfp_mask))) {
680 mempool_free(rq, q->rq.rq_pool);
681 return NULL;
682 }
683 /* @rq->elv.icq holds on to io_context until @rq is freed */
684 if (icq)
685 get_io_context(icq->ioc);
Tejun Heocb98fc82005-10-28 08:29:39 +0200686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Tejun Heocb98fc82005-10-28 08:29:39 +0200688 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
691/*
692 * ioc_batching returns true if the ioc is a valid batching request and
693 * should be given priority access to a request.
694 */
Jens Axboe165125e2007-07-24 09:28:11 +0200695static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
697 if (!ioc)
698 return 0;
699
700 /*
701 * Make sure the process is able to allocate at least 1 request
702 * even if the batch times out, otherwise we could theoretically
703 * lose wakeups.
704 */
705 return ioc->nr_batch_requests == q->nr_batching ||
706 (ioc->nr_batch_requests > 0
707 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
708}
709
710/*
711 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
712 * will cause the process to be a "batcher" on all queues in the system. This
713 * is the behaviour we want though - once it gets a wakeup it should be given
714 * a nice run.
715 */
Jens Axboe165125e2007-07-24 09:28:11 +0200716static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
718 if (!ioc || ioc_batching(q, ioc))
719 return;
720
721 ioc->nr_batch_requests = q->nr_batching;
722 ioc->last_waited = jiffies;
723}
724
Jens Axboe1faa16d2009-04-06 14:48:01 +0200725static void __freed_request(struct request_queue *q, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
727 struct request_list *rl = &q->rq;
728
Jens Axboe1faa16d2009-04-06 14:48:01 +0200729 if (rl->count[sync] < queue_congestion_off_threshold(q))
730 blk_clear_queue_congested(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Jens Axboe1faa16d2009-04-06 14:48:01 +0200732 if (rl->count[sync] + 1 <= q->nr_requests) {
733 if (waitqueue_active(&rl->wait[sync]))
734 wake_up(&rl->wait[sync]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Jens Axboe1faa16d2009-04-06 14:48:01 +0200736 blk_clear_queue_full(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
738}
739
740/*
741 * A request has just been released. Account for it, update the full and
742 * congestion status, wake up any waiters. Called under q->queue_lock.
743 */
Tejun Heo75eb6c32011-10-19 14:31:22 +0200744static void freed_request(struct request_queue *q, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
746 struct request_list *rl = &q->rq;
Tejun Heo75eb6c32011-10-19 14:31:22 +0200747 int sync = rw_is_sync(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Jens Axboe1faa16d2009-04-06 14:48:01 +0200749 rl->count[sync]--;
Tejun Heo75eb6c32011-10-19 14:31:22 +0200750 if (flags & REQ_ELVPRIV)
Tejun Heocb98fc82005-10-28 08:29:39 +0200751 rl->elvpriv--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Jens Axboe1faa16d2009-04-06 14:48:01 +0200753 __freed_request(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Jens Axboe1faa16d2009-04-06 14:48:01 +0200755 if (unlikely(rl->starved[sync ^ 1]))
756 __freed_request(q, sync ^ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759/*
Mike Snitzer9d5a4e92011-02-11 11:05:46 +0100760 * Determine if elevator data should be initialized when allocating the
761 * request associated with @bio.
762 */
763static bool blk_rq_should_init_elevator(struct bio *bio)
764{
765 if (!bio)
766 return true;
767
768 /*
769 * Flush requests do not use the elevator so skip initialization.
770 * This allows a request to share the flush and elevator data.
771 */
772 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA))
773 return false;
774
775 return true;
776}
777
Tejun Heoda8303c2011-10-19 14:33:05 +0200778/**
779 * get_request - get a free request
780 * @q: request_queue to allocate request from
781 * @rw_flags: RW and SYNC flags
782 * @bio: bio to allocate request for (can be %NULL)
783 * @gfp_mask: allocation mask
784 *
785 * Get a free request from @q. This function may fail under memory
786 * pressure or if @q is dead.
787 *
788 * Must be callled with @q->queue_lock held and,
789 * Returns %NULL on failure, with @q->queue_lock held.
790 * Returns !%NULL on success, with @q->queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 */
Jens Axboe165125e2007-07-24 09:28:11 +0200792static struct request *get_request(struct request_queue *q, int rw_flags,
Jens Axboe7749a8d2006-12-13 13:02:26 +0100793 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
795 struct request *rq = NULL;
796 struct request_list *rl = &q->rq;
Tejun Heof1f8cc92011-12-14 00:33:42 +0100797 struct elevator_type *et;
Tejun Heof2dbd762011-12-14 00:33:40 +0100798 struct io_context *ioc;
Tejun Heof1f8cc92011-12-14 00:33:42 +0100799 struct io_cq *icq = NULL;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200800 const bool is_sync = rw_is_sync(rw_flags) != 0;
Tejun Heof2dbd762011-12-14 00:33:40 +0100801 bool retried = false;
Tejun Heo75eb6c32011-10-19 14:31:22 +0200802 int may_queue;
Tejun Heof2dbd762011-12-14 00:33:40 +0100803retry:
Tejun Heof1f8cc92011-12-14 00:33:42 +0100804 et = q->elevator->type;
Tejun Heof2dbd762011-12-14 00:33:40 +0100805 ioc = current->io_context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Tejun Heo34f60552011-12-14 00:33:37 +0100807 if (unlikely(blk_queue_dead(q)))
Tejun Heoda8303c2011-10-19 14:33:05 +0200808 return NULL;
809
Jens Axboe7749a8d2006-12-13 13:02:26 +0100810 may_queue = elv_may_queue(q, rw_flags);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100811 if (may_queue == ELV_MQUEUE_NO)
812 goto rq_starved;
813
Jens Axboe1faa16d2009-04-06 14:48:01 +0200814 if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) {
815 if (rl->count[is_sync]+1 >= q->nr_requests) {
Tejun Heof2dbd762011-12-14 00:33:40 +0100816 /*
817 * We want ioc to record batching state. If it's
818 * not already there, creating a new one requires
819 * dropping queue_lock, which in turn requires
820 * retesting conditions to avoid queue hang.
821 */
822 if (!ioc && !retried) {
823 spin_unlock_irq(q->queue_lock);
824 create_io_context(current, gfp_mask, q->node);
825 spin_lock_irq(q->queue_lock);
826 retried = true;
827 goto retry;
828 }
829
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100830 /*
831 * The queue will fill after this allocation, so set
832 * it as full, and mark this process as "batching".
833 * This process will be allowed to complete a batch of
834 * requests, others will be blocked.
835 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200836 if (!blk_queue_full(q, is_sync)) {
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100837 ioc_set_batching(q, ioc);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200838 blk_set_queue_full(q, is_sync);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100839 } else {
840 if (may_queue != ELV_MQUEUE_MUST
841 && !ioc_batching(q, ioc)) {
842 /*
843 * The queue is full and the allocating
844 * process is not a "batcher", and not
845 * exempted by the IO scheduler
846 */
847 goto out;
848 }
849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 }
Jens Axboe1faa16d2009-04-06 14:48:01 +0200851 blk_set_queue_congested(q, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
853
Jens Axboe082cf692005-06-28 16:35:11 +0200854 /*
855 * Only allow batching queuers to allocate up to 50% over the defined
856 * limit of requests, otherwise we could have thousands of requests
857 * allocated with any setting of ->nr_requests
858 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200859 if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
Jens Axboe082cf692005-06-28 16:35:11 +0200860 goto out;
Hugh Dickinsfd782a42005-06-29 15:15:40 +0100861
Jens Axboe1faa16d2009-04-06 14:48:01 +0200862 rl->count[is_sync]++;
863 rl->starved[is_sync] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200864
Tejun Heof1f8cc92011-12-14 00:33:42 +0100865 /*
866 * Decide whether the new request will be managed by elevator. If
867 * so, mark @rw_flags and increment elvpriv. Non-zero elvpriv will
868 * prevent the current elevator from being destroyed until the new
869 * request is freed. This guarantees icq's won't be destroyed and
870 * makes creating new ones safe.
871 *
872 * Also, lookup icq while holding queue_lock. If it doesn't exist,
873 * it will be created after releasing queue_lock.
874 */
Tejun Heo75eb6c32011-10-19 14:31:22 +0200875 if (blk_rq_should_init_elevator(bio) &&
876 !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags)) {
877 rw_flags |= REQ_ELVPRIV;
878 rl->elvpriv++;
Tejun Heof1f8cc92011-12-14 00:33:42 +0100879 if (et->icq_cache && ioc)
880 icq = ioc_lookup_icq(ioc, q);
Mike Snitzer9d5a4e92011-02-11 11:05:46 +0100881 }
Tejun Heocb98fc82005-10-28 08:29:39 +0200882
Jens Axboef253b862010-10-24 22:06:02 +0200883 if (blk_queue_io_stat(q))
884 rw_flags |= REQ_IO_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 spin_unlock_irq(q->queue_lock);
886
Tejun Heof1f8cc92011-12-14 00:33:42 +0100887 /* create icq if missing */
Shaohua Li05c30b92012-01-19 09:20:10 +0100888 if ((rw_flags & REQ_ELVPRIV) && unlikely(et->icq_cache && !icq)) {
Tejun Heof1f8cc92011-12-14 00:33:42 +0100889 icq = ioc_create_icq(q, gfp_mask);
Shaohua Li05c30b92012-01-19 09:20:10 +0100890 if (!icq)
891 goto fail_icq;
892 }
Tejun Heof1f8cc92011-12-14 00:33:42 +0100893
Shaohua Li05c30b92012-01-19 09:20:10 +0100894 rq = blk_alloc_request(q, icq, rw_flags, gfp_mask);
Tejun Heof1f8cc92011-12-14 00:33:42 +0100895
Shaohua Li05c30b92012-01-19 09:20:10 +0100896fail_icq:
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100897 if (unlikely(!rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 /*
899 * Allocation failed presumably due to memory. Undo anything
900 * we might have messed up.
901 *
902 * Allocating task should really be put onto the front of the
903 * wait queue, but this is pretty rare.
904 */
905 spin_lock_irq(q->queue_lock);
Tejun Heo75eb6c32011-10-19 14:31:22 +0200906 freed_request(q, rw_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 /*
909 * in the very unlikely event that allocation failed and no
910 * requests for this direction was pending, mark us starved
911 * so that freeing of a request in the other direction will
912 * notice us. another possible fix would be to split the
913 * rq mempool into READ and WRITE
914 */
915rq_starved:
Jens Axboe1faa16d2009-04-06 14:48:01 +0200916 if (unlikely(rl->count[is_sync] == 0))
917 rl->starved[is_sync] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 goto out;
920 }
921
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100922 /*
923 * ioc may be NULL here, and ioc_batching will be false. That's
924 * OK, if the queue is under the request limit then requests need
925 * not count toward the nr_batch_requests limit. There will always
926 * be some limit enforced by BLK_BATCH_TIME.
927 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 if (ioc_batching(q, ioc))
929 ioc->nr_batch_requests--;
Jens Axboe6728cb02008-01-31 13:03:55 +0100930
Jens Axboe1faa16d2009-04-06 14:48:01 +0200931 trace_block_getrq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return rq;
934}
935
Tejun Heoda8303c2011-10-19 14:33:05 +0200936/**
937 * get_request_wait - get a free request with retry
938 * @q: request_queue to allocate request from
939 * @rw_flags: RW and SYNC flags
940 * @bio: bio to allocate request for (can be %NULL)
Nick Piggind6344532005-06-28 20:45:14 -0700941 *
Tejun Heoda8303c2011-10-19 14:33:05 +0200942 * Get a free request from @q. This function keeps retrying under memory
943 * pressure and fails iff @q is dead.
944 *
945 * Must be callled with @q->queue_lock held and,
946 * Returns %NULL on failure, with @q->queue_lock held.
947 * Returns !%NULL on success, with @q->queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 */
Jens Axboe165125e2007-07-24 09:28:11 +0200949static struct request *get_request_wait(struct request_queue *q, int rw_flags,
Jens Axboe22e2c502005-06-27 10:55:12 +0200950 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Jens Axboe1faa16d2009-04-06 14:48:01 +0200952 const bool is_sync = rw_is_sync(rw_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 struct request *rq;
954
Jens Axboe7749a8d2006-12-13 13:02:26 +0100955 rq = get_request(q, rw_flags, bio, GFP_NOIO);
Nick Piggin450991b2005-06-28 20:45:13 -0700956 while (!rq) {
957 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 struct request_list *rl = &q->rq;
959
Tejun Heo34f60552011-12-14 00:33:37 +0100960 if (unlikely(blk_queue_dead(q)))
Tejun Heoda8303c2011-10-19 14:33:05 +0200961 return NULL;
962
Jens Axboe1faa16d2009-04-06 14:48:01 +0200963 prepare_to_wait_exclusive(&rl->wait[is_sync], &wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 TASK_UNINTERRUPTIBLE);
965
Jens Axboe1faa16d2009-04-06 14:48:01 +0200966 trace_block_sleeprq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200968 spin_unlock_irq(q->queue_lock);
969 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200971 /*
972 * After sleeping, we become a "batching" process and
973 * will be able to allocate at least one request, and
974 * up to a big batch of them for a small period time.
975 * See ioc_batching, ioc_set_batching
976 */
Tejun Heof2dbd762011-12-14 00:33:40 +0100977 create_io_context(current, GFP_NOIO, q->node);
978 ioc_set_batching(q, current->io_context);
Jens Axboe2056a782006-03-23 20:00:26 +0100979
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200980 spin_lock_irq(q->queue_lock);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200981 finish_wait(&rl->wait[is_sync], &wait);
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200982
983 rq = get_request(q, rw_flags, bio, GFP_NOIO);
984 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 return rq;
987}
988
Jens Axboe165125e2007-07-24 09:28:11 +0200989struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
991 struct request *rq;
992
Nick Piggind6344532005-06-28 20:45:14 -0700993 spin_lock_irq(q->queue_lock);
Tejun Heoda8303c2011-10-19 14:33:05 +0200994 if (gfp_mask & __GFP_WAIT)
Jens Axboe22e2c502005-06-27 10:55:12 +0200995 rq = get_request_wait(q, rw, NULL);
Tejun Heoda8303c2011-10-19 14:33:05 +0200996 else
Jens Axboe22e2c502005-06-27 10:55:12 +0200997 rq = get_request(q, rw, NULL, gfp_mask);
Tejun Heoda8303c2011-10-19 14:33:05 +0200998 if (!rq)
999 spin_unlock_irq(q->queue_lock);
Nick Piggind6344532005-06-28 20:45:14 -07001000 /* q->queue_lock is unlocked at this point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 return rq;
1003}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004EXPORT_SYMBOL(blk_get_request);
1005
1006/**
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001007 * blk_make_request - given a bio, allocate a corresponding struct request.
Randy Dunlap8ebf9752009-06-11 20:00:41 -07001008 * @q: target request queue
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001009 * @bio: The bio describing the memory mappings that will be submitted for IO.
1010 * It may be a chained-bio properly constructed by block/bio layer.
Randy Dunlap8ebf9752009-06-11 20:00:41 -07001011 * @gfp_mask: gfp flags to be used for memory allocation
Jens Axboedc72ef42006-07-20 14:54:05 +02001012 *
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001013 * blk_make_request is the parallel of generic_make_request for BLOCK_PC
1014 * type commands. Where the struct request needs to be farther initialized by
1015 * the caller. It is passed a &struct bio, which describes the memory info of
1016 * the I/O transfer.
1017 *
1018 * The caller of blk_make_request must make sure that bi_io_vec
1019 * are set to describe the memory buffers. That bio_data_dir() will return
1020 * the needed direction of the request. (And all bio's in the passed bio-chain
1021 * are properly set accordingly)
1022 *
1023 * If called under none-sleepable conditions, mapped bio buffers must not
1024 * need bouncing, by calling the appropriate masked or flagged allocator,
1025 * suitable for the target device. Otherwise the call to blk_queue_bounce will
1026 * BUG.
Jens Axboe53674ac2009-05-19 19:52:35 +02001027 *
1028 * WARNING: When allocating/cloning a bio-chain, careful consideration should be
1029 * given to how you allocate bios. In particular, you cannot use __GFP_WAIT for
1030 * anything but the first bio in the chain. Otherwise you risk waiting for IO
1031 * completion of a bio that hasn't been submitted yet, thus resulting in a
1032 * deadlock. Alternatively bios should be allocated using bio_kmalloc() instead
1033 * of bio_alloc(), as that avoids the mempool deadlock.
1034 * If possible a big IO should be split into smaller parts when allocation
1035 * fails. Partial allocation should not be an error, or you risk a live-lock.
Jens Axboedc72ef42006-07-20 14:54:05 +02001036 */
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001037struct request *blk_make_request(struct request_queue *q, struct bio *bio,
1038 gfp_t gfp_mask)
Jens Axboedc72ef42006-07-20 14:54:05 +02001039{
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001040 struct request *rq = blk_get_request(q, bio_data_dir(bio), gfp_mask);
1041
1042 if (unlikely(!rq))
1043 return ERR_PTR(-ENOMEM);
1044
1045 for_each_bio(bio) {
1046 struct bio *bounce_bio = bio;
1047 int ret;
1048
1049 blk_queue_bounce(q, &bounce_bio);
1050 ret = blk_rq_append_bio(q, rq, bounce_bio);
1051 if (unlikely(ret)) {
1052 blk_put_request(rq);
1053 return ERR_PTR(ret);
1054 }
1055 }
1056
1057 return rq;
Jens Axboedc72ef42006-07-20 14:54:05 +02001058}
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001059EXPORT_SYMBOL(blk_make_request);
Jens Axboedc72ef42006-07-20 14:54:05 +02001060
1061/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 * blk_requeue_request - put a request back on queue
1063 * @q: request queue where request should be inserted
1064 * @rq: request to be inserted
1065 *
1066 * Description:
1067 * Drivers often keep queueing requests until the hardware cannot accept
1068 * more, when that condition happens we need to put the request back
1069 * on the queue. Must be called with queue lock held.
1070 */
Jens Axboe165125e2007-07-24 09:28:11 +02001071void blk_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
Jens Axboe242f9dc2008-09-14 05:55:09 -07001073 blk_delete_timer(rq);
1074 blk_clear_rq_complete(rq);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001075 trace_block_rq_requeue(q, rq);
Jens Axboe2056a782006-03-23 20:00:26 +01001076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 if (blk_rq_tagged(rq))
1078 blk_queue_end_tag(q, rq);
1079
James Bottomleyba396a62009-05-27 14:17:08 +02001080 BUG_ON(blk_queued_rq(rq));
1081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 elv_requeue_request(q, rq);
1083}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084EXPORT_SYMBOL(blk_requeue_request);
1085
Tatyana Brokhman57d80192012-12-04 15:54:43 +02001086/**
1087 * blk_reinsert_request() - Insert a request back to the scheduler
1088 * @q: request queue
1089 * @rq: request to be inserted
1090 *
1091 * This function inserts the request back to the scheduler as if
1092 * it was never dispatched.
1093 *
1094 * Return: 0 on success, error code on fail
1095 */
1096int blk_reinsert_request(struct request_queue *q, struct request *rq)
1097{
1098 if (unlikely(!rq) || unlikely(!q))
1099 return -EIO;
1100
1101 blk_delete_timer(rq);
1102 blk_clear_rq_complete(rq);
1103 trace_block_rq_requeue(q, rq);
1104
1105 if (blk_rq_tagged(rq))
1106 blk_queue_end_tag(q, rq);
1107
1108 BUG_ON(blk_queued_rq(rq));
1109
1110 return elv_reinsert_request(q, rq);
1111}
1112EXPORT_SYMBOL(blk_reinsert_request);
1113
1114/**
1115 * blk_reinsert_req_sup() - check whether the scheduler supports
1116 * reinsertion of requests
1117 * @q: request queue
1118 *
1119 * Returns true if the current scheduler supports reinserting
1120 * request. False otherwise
1121 */
1122bool blk_reinsert_req_sup(struct request_queue *q)
1123{
1124 if (unlikely(!q))
1125 return false;
1126 return q->elevator->type->ops.elevator_reinsert_req_fn ? true : false;
1127}
1128EXPORT_SYMBOL(blk_reinsert_req_sup);
1129
Jens Axboe73c10102011-03-08 13:19:51 +01001130static void add_acct_request(struct request_queue *q, struct request *rq,
1131 int where)
1132{
1133 drive_stat_acct(rq, 1);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001134 __elv_add_request(q, rq, where);
Jens Axboe73c10102011-03-08 13:19:51 +01001135}
1136
Tejun Heo074a7ac2008-08-25 19:56:14 +09001137static void part_round_stats_single(int cpu, struct hd_struct *part,
1138 unsigned long now)
1139{
1140 if (now == part->stamp)
1141 return;
1142
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001143 if (part_in_flight(part)) {
Tejun Heo074a7ac2008-08-25 19:56:14 +09001144 __part_stat_add(cpu, part, time_in_queue,
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001145 part_in_flight(part) * (now - part->stamp));
Tejun Heo074a7ac2008-08-25 19:56:14 +09001146 __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
1147 }
1148 part->stamp = now;
1149}
1150
1151/**
Randy Dunlap496aa8a2008-10-16 07:46:23 +02001152 * part_round_stats() - Round off the performance stats on a struct disk_stats.
1153 * @cpu: cpu number for stats access
1154 * @part: target partition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 *
1156 * The average IO queue length and utilisation statistics are maintained
1157 * by observing the current state of the queue length and the amount of
1158 * time it has been in this state for.
1159 *
1160 * Normally, that accounting is done on IO completion, but that can result
1161 * in more than a second's worth of IO being accounted for within any one
1162 * second, leading to >100% utilisation. To deal with that, we call this
1163 * function to do a round-off before returning the results when reading
1164 * /proc/diskstats. This accounts immediately for all queue usage up to
1165 * the current jiffies and restarts the counters again.
1166 */
Tejun Heoc9959052008-08-25 19:47:21 +09001167void part_round_stats(int cpu, struct hd_struct *part)
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001168{
1169 unsigned long now = jiffies;
1170
Tejun Heo074a7ac2008-08-25 19:56:14 +09001171 if (part->partno)
1172 part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
1173 part_round_stats_single(cpu, part, now);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001174}
Tejun Heo074a7ac2008-08-25 19:56:14 +09001175EXPORT_SYMBOL_GPL(part_round_stats);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177/*
1178 * queue lock must be held
1179 */
Jens Axboe165125e2007-07-24 09:28:11 +02001180void __blk_put_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 if (unlikely(!q))
1183 return;
1184 if (unlikely(--req->ref_count))
1185 return;
1186
Tejun Heo8922e162005-10-20 16:23:44 +02001187 elv_completed_request(q, req);
1188
Boaz Harrosh1cd96c22009-03-24 12:35:07 +01001189 /* this is a bio leak */
1190 WARN_ON(req->bio != NULL);
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 /*
1193 * Request may not have originated from ll_rw_blk. if not,
1194 * it didn't come out of our reserved rq pools
1195 */
Jens Axboe49171e52006-08-10 08:59:11 +02001196 if (req->cmd_flags & REQ_ALLOCED) {
Tejun Heo75eb6c32011-10-19 14:31:22 +02001197 unsigned int flags = req->cmd_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 BUG_ON(!list_empty(&req->queuelist));
Jens Axboe98170642006-07-28 09:23:08 +02001200 BUG_ON(!hlist_unhashed(&req->hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 blk_free_request(q, req);
Tejun Heo75eb6c32011-10-19 14:31:22 +02001203 freed_request(q, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 }
1205}
Mike Christie6e39b692005-11-11 05:30:24 -06001206EXPORT_SYMBOL_GPL(__blk_put_request);
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208void blk_put_request(struct request *req)
1209{
Tejun Heo8922e162005-10-20 16:23:44 +02001210 unsigned long flags;
Jens Axboe165125e2007-07-24 09:28:11 +02001211 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
FUJITA Tomonori52a93ba2008-07-15 21:21:45 +02001213 spin_lock_irqsave(q->queue_lock, flags);
1214 __blk_put_request(q, req);
1215 spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217EXPORT_SYMBOL(blk_put_request);
1218
Christoph Hellwig66ac0282010-06-18 16:59:42 +02001219/**
1220 * blk_add_request_payload - add a payload to a request
1221 * @rq: request to update
1222 * @page: page backing the payload
1223 * @len: length of the payload.
1224 *
1225 * This allows to later add a payload to an already submitted request by
1226 * a block driver. The driver needs to take care of freeing the payload
1227 * itself.
1228 *
1229 * Note that this is a quite horrible hack and nothing but handling of
1230 * discard requests should ever use it.
1231 */
1232void blk_add_request_payload(struct request *rq, struct page *page,
1233 unsigned int len)
1234{
1235 struct bio *bio = rq->bio;
1236
1237 bio->bi_io_vec->bv_page = page;
1238 bio->bi_io_vec->bv_offset = 0;
1239 bio->bi_io_vec->bv_len = len;
1240
1241 bio->bi_size = len;
1242 bio->bi_vcnt = 1;
1243 bio->bi_phys_segments = 1;
1244
1245 rq->__data_len = rq->resid_len = len;
1246 rq->nr_phys_segments = 1;
1247 rq->buffer = bio_data(bio);
1248}
1249EXPORT_SYMBOL_GPL(blk_add_request_payload);
1250
Jens Axboe73c10102011-03-08 13:19:51 +01001251static bool bio_attempt_back_merge(struct request_queue *q, struct request *req,
1252 struct bio *bio)
1253{
1254 const int ff = bio->bi_rw & REQ_FAILFAST_MASK;
1255
Jens Axboe73c10102011-03-08 13:19:51 +01001256 if (!ll_back_merge_fn(q, req, bio))
1257 return false;
1258
1259 trace_block_bio_backmerge(q, bio);
1260
1261 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1262 blk_rq_set_mixed_merge(req);
1263
1264 req->biotail->bi_next = bio;
1265 req->biotail = bio;
1266 req->__data_len += bio->bi_size;
1267 req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
1268
1269 drive_stat_acct(req, 0);
1270 return true;
1271}
1272
1273static bool bio_attempt_front_merge(struct request_queue *q,
1274 struct request *req, struct bio *bio)
1275{
1276 const int ff = bio->bi_rw & REQ_FAILFAST_MASK;
Jens Axboe73c10102011-03-08 13:19:51 +01001277
Jens Axboe73c10102011-03-08 13:19:51 +01001278 if (!ll_front_merge_fn(q, req, bio))
1279 return false;
1280
1281 trace_block_bio_frontmerge(q, bio);
1282
1283 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1284 blk_rq_set_mixed_merge(req);
1285
Jens Axboe73c10102011-03-08 13:19:51 +01001286 bio->bi_next = req->bio;
1287 req->bio = bio;
1288
1289 /*
1290 * may not be valid. if the low level driver said
1291 * it didn't need a bounce buffer then it better
1292 * not touch req->buffer either...
1293 */
1294 req->buffer = bio_data(bio);
1295 req->__sector = bio->bi_sector;
1296 req->__data_len += bio->bi_size;
1297 req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
1298
1299 drive_stat_acct(req, 0);
1300 return true;
1301}
1302
Tejun Heobd87b582011-10-19 14:33:08 +02001303/**
1304 * attempt_plug_merge - try to merge with %current's plugged list
1305 * @q: request_queue new bio is being queued at
1306 * @bio: new bio being queued
1307 * @request_count: out parameter for number of traversed plugged requests
1308 *
1309 * Determine whether @bio being queued on @q can be merged with a request
1310 * on %current's plugged list. Returns %true if merge was successful,
1311 * otherwise %false.
1312 *
Tejun Heo07c2bd32012-02-08 09:19:42 +01001313 * Plugging coalesces IOs from the same issuer for the same purpose without
1314 * going through @q->queue_lock. As such it's more of an issuing mechanism
1315 * than scheduling, and the request, while may have elvpriv data, is not
1316 * added on the elevator at this point. In addition, we don't have
1317 * reliable access to the elevator outside queue lock. Only check basic
1318 * merging parameters without querying the elevator.
Jens Axboe73c10102011-03-08 13:19:51 +01001319 */
Tejun Heobd87b582011-10-19 14:33:08 +02001320static bool attempt_plug_merge(struct request_queue *q, struct bio *bio,
1321 unsigned int *request_count)
Jens Axboe73c10102011-03-08 13:19:51 +01001322{
1323 struct blk_plug *plug;
1324 struct request *rq;
1325 bool ret = false;
1326
Tejun Heobd87b582011-10-19 14:33:08 +02001327 plug = current->plug;
Jens Axboe73c10102011-03-08 13:19:51 +01001328 if (!plug)
1329 goto out;
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001330 *request_count = 0;
Jens Axboe73c10102011-03-08 13:19:51 +01001331
1332 list_for_each_entry_reverse(rq, &plug->list, queuelist) {
1333 int el_ret;
1334
Shaohua Li1b2e19f2012-04-06 11:37:47 -06001335 if (rq->q == q)
1336 (*request_count)++;
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001337
Tejun Heo07c2bd32012-02-08 09:19:42 +01001338 if (rq->q != q || !blk_rq_merge_ok(rq, bio))
Jens Axboe73c10102011-03-08 13:19:51 +01001339 continue;
1340
Tejun Heo050c8ea2012-02-08 09:19:38 +01001341 el_ret = blk_try_merge(rq, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001342 if (el_ret == ELEVATOR_BACK_MERGE) {
1343 ret = bio_attempt_back_merge(q, rq, bio);
1344 if (ret)
1345 break;
1346 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
1347 ret = bio_attempt_front_merge(q, rq, bio);
1348 if (ret)
1349 break;
1350 }
1351 }
1352out:
1353 return ret;
1354}
1355
Jens Axboe86db1e22008-01-29 14:53:40 +01001356void init_request_from_bio(struct request *req, struct bio *bio)
Tejun Heo52d9e672006-01-06 09:49:58 +01001357{
Jens Axboe4aff5e22006-08-10 08:44:47 +02001358 req->cmd_type = REQ_TYPE_FS;
Tejun Heo52d9e672006-01-06 09:49:58 +01001359
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001360 req->cmd_flags |= bio->bi_rw & REQ_COMMON_MASK;
1361 if (bio->bi_rw & REQ_RAHEAD)
Tejun Heoa82afdf2009-07-03 17:48:16 +09001362 req->cmd_flags |= REQ_FAILFAST_MASK;
Jens Axboeb31dc662006-06-13 08:26:10 +02001363
Tejun Heo52d9e672006-01-06 09:49:58 +01001364 req->errors = 0;
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001365 req->__sector = bio->bi_sector;
Tejun Heo52d9e672006-01-06 09:49:58 +01001366 req->ioprio = bio_prio(bio);
NeilBrownbc1c56f2007-08-16 13:31:30 +02001367 blk_rq_bio_prep(req->q, req, bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001368}
Maya Erez60181552012-06-27 11:25:26 +03001369EXPORT_SYMBOL(init_request_from_bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001370
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001371void blk_queue_bio(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
Jiri Slaby5e00d1b2010-08-12 14:31:06 +02001373 const bool sync = !!(bio->bi_rw & REQ_SYNC);
Jens Axboe73c10102011-03-08 13:19:51 +01001374 struct blk_plug *plug;
1375 int el_ret, rw_flags, where = ELEVATOR_INSERT_SORT;
1376 struct request *req;
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001377 unsigned int request_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 /*
1380 * low level driver can indicate that it wants pages above a
1381 * certain limit bounced to low memory (ie for highmem, or even
1382 * ISA dma in theory)
1383 */
1384 blk_queue_bounce(q, &bio);
1385
Tejun Heo4fed9472010-09-03 11:56:17 +02001386 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) {
Jens Axboe73c10102011-03-08 13:19:51 +01001387 spin_lock_irq(q->queue_lock);
Tejun Heoae1b1532011-01-25 12:43:54 +01001388 where = ELEVATOR_INSERT_FLUSH;
Tejun Heo28e7d182010-09-03 11:56:16 +02001389 goto get_rq;
1390 }
1391
Jens Axboe73c10102011-03-08 13:19:51 +01001392 /*
1393 * Check if we can merge with the plugged list before grabbing
1394 * any locks.
1395 */
Tejun Heobd87b582011-10-19 14:33:08 +02001396 if (attempt_plug_merge(q, bio, &request_count))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001397 return;
Jens Axboe73c10102011-03-08 13:19:51 +01001398
1399 spin_lock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 el_ret = elv_merge(q, &req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001402 if (el_ret == ELEVATOR_BACK_MERGE) {
Jens Axboe73c10102011-03-08 13:19:51 +01001403 if (bio_attempt_back_merge(q, req, bio)) {
Tejun Heo07c2bd32012-02-08 09:19:42 +01001404 elv_bio_merged(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001405 if (!attempt_back_merge(q, req))
1406 elv_merged_request(q, req, el_ret);
1407 goto out_unlock;
Tejun Heo80a761f2009-07-03 17:48:17 +09001408 }
Jens Axboe73c10102011-03-08 13:19:51 +01001409 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
Jens Axboe73c10102011-03-08 13:19:51 +01001410 if (bio_attempt_front_merge(q, req, bio)) {
Tejun Heo07c2bd32012-02-08 09:19:42 +01001411 elv_bio_merged(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001412 if (!attempt_front_merge(q, req))
1413 elv_merged_request(q, req, el_ret);
1414 goto out_unlock;
1415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 }
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418get_rq:
Nick Piggin450991b2005-06-28 20:45:13 -07001419 /*
Jens Axboe7749a8d2006-12-13 13:02:26 +01001420 * This sync check and mask will be re-done in init_request_from_bio(),
1421 * but we need to set it earlier to expose the sync flag to the
1422 * rq allocator and io schedulers.
1423 */
1424 rw_flags = bio_data_dir(bio);
1425 if (sync)
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001426 rw_flags |= REQ_SYNC;
Jens Axboe7749a8d2006-12-13 13:02:26 +01001427
1428 /*
Nick Piggin450991b2005-06-28 20:45:13 -07001429 * Grab a free request. This is might sleep but can not fail.
Nick Piggind6344532005-06-28 20:45:14 -07001430 * Returns with the queue unlocked.
Nick Piggin450991b2005-06-28 20:45:13 -07001431 */
Jens Axboe7749a8d2006-12-13 13:02:26 +01001432 req = get_request_wait(q, rw_flags, bio);
Tejun Heoda8303c2011-10-19 14:33:05 +02001433 if (unlikely(!req)) {
1434 bio_endio(bio, -ENODEV); /* @q is dead */
1435 goto out_unlock;
1436 }
Nick Piggind6344532005-06-28 20:45:14 -07001437
Nick Piggin450991b2005-06-28 20:45:13 -07001438 /*
1439 * After dropping the lock and possibly sleeping here, our request
1440 * may now be mergeable after it had proven unmergeable (above).
1441 * We don't worry about that case for efficiency. It won't happen
1442 * often, and the elevators are able to handle it.
1443 */
Tejun Heo52d9e672006-01-06 09:49:58 +01001444 init_request_from_bio(req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Tao Ma9562ad92011-10-24 16:11:30 +02001446 if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags))
Jens Axboe11ccf112011-07-26 15:01:15 +02001447 req->cpu = raw_smp_processor_id();
Tejun Heodd831002010-09-03 11:56:16 +02001448
Jens Axboe73c10102011-03-08 13:19:51 +01001449 plug = current->plug;
Jens Axboe721a9602011-03-09 11:56:30 +01001450 if (plug) {
Jens Axboedc6d36c2011-04-12 10:28:28 +02001451 /*
1452 * If this is the first request added after a plug, fire
1453 * of a plug trace. If others have been added before, check
1454 * if we have multiple devices in this plug. If so, make a
1455 * note to sort the list before dispatch.
1456 */
1457 if (list_empty(&plug->list))
1458 trace_block_plug(q);
Shaohua Li3540d5e2011-11-16 09:21:50 +01001459 else {
1460 if (!plug->should_sort) {
1461 struct request *__rq;
Jens Axboe73c10102011-03-08 13:19:51 +01001462
Shaohua Li3540d5e2011-11-16 09:21:50 +01001463 __rq = list_entry_rq(plug->list.prev);
1464 if (__rq->q != q)
1465 plug->should_sort = 1;
1466 }
Shaohua Li019ceb72011-11-16 09:21:50 +01001467 if (request_count >= BLK_MAX_REQUEST_COUNT) {
Shaohua Li3540d5e2011-11-16 09:21:50 +01001468 blk_flush_plug_list(plug, false);
Shaohua Li019ceb72011-11-16 09:21:50 +01001469 trace_block_plug(q);
1470 }
Jens Axboe73c10102011-03-08 13:19:51 +01001471 }
Shaohua Lia6327162011-08-24 16:04:32 +02001472 list_add_tail(&req->queuelist, &plug->list);
1473 drive_stat_acct(req, 1);
Jens Axboe73c10102011-03-08 13:19:51 +01001474 } else {
1475 spin_lock_irq(q->queue_lock);
1476 add_acct_request(q, req, where);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02001477 __blk_run_queue(q);
Jens Axboe73c10102011-03-08 13:19:51 +01001478out_unlock:
1479 spin_unlock_irq(q->queue_lock);
1480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481}
Jens Axboec20e8de2011-09-12 12:03:37 +02001482EXPORT_SYMBOL_GPL(blk_queue_bio); /* for device mapper only */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484/*
1485 * If bio->bi_dev is a partition, remap the location
1486 */
1487static inline void blk_partition_remap(struct bio *bio)
1488{
1489 struct block_device *bdev = bio->bi_bdev;
1490
Jens Axboebf2de6f2007-09-27 13:01:25 +02001491 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 struct hd_struct *p = bdev->bd_part;
Jens Axboea3623572005-11-01 09:26:16 +01001493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 bio->bi_sector += p->start_sect;
1495 bio->bi_bdev = bdev->bd_contains;
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001496
Mike Snitzerd07335e2010-11-16 12:52:38 +01001497 trace_block_bio_remap(bdev_get_queue(bio->bi_bdev), bio,
1498 bdev->bd_dev,
1499 bio->bi_sector - p->start_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 }
1501}
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503static void handle_bad_sector(struct bio *bio)
1504{
1505 char b[BDEVNAME_SIZE];
1506
1507 printk(KERN_INFO "attempt to access beyond end of device\n");
1508 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1509 bdevname(bio->bi_bdev, b),
1510 bio->bi_rw,
1511 (unsigned long long)bio->bi_sector + bio_sectors(bio),
Mike Snitzer77304d22010-11-08 14:39:12 +01001512 (long long)(i_size_read(bio->bi_bdev->bd_inode) >> 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514 set_bit(BIO_EOF, &bio->bi_flags);
1515}
1516
Akinobu Mitac17bb492006-12-08 02:39:46 -08001517#ifdef CONFIG_FAIL_MAKE_REQUEST
1518
1519static DECLARE_FAULT_ATTR(fail_make_request);
1520
1521static int __init setup_fail_make_request(char *str)
1522{
1523 return setup_fault_attr(&fail_make_request, str);
1524}
1525__setup("fail_make_request=", setup_fail_make_request);
1526
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001527static bool should_fail_request(struct hd_struct *part, unsigned int bytes)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001528{
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001529 return part->make_it_fail && should_fail(&fail_make_request, bytes);
Akinobu Mitac17bb492006-12-08 02:39:46 -08001530}
1531
1532static int __init fail_make_request_debugfs(void)
1533{
Akinobu Mitadd48c082011-08-03 16:21:01 -07001534 struct dentry *dir = fault_create_debugfs_attr("fail_make_request",
1535 NULL, &fail_make_request);
1536
1537 return IS_ERR(dir) ? PTR_ERR(dir) : 0;
Akinobu Mitac17bb492006-12-08 02:39:46 -08001538}
1539
1540late_initcall(fail_make_request_debugfs);
1541
1542#else /* CONFIG_FAIL_MAKE_REQUEST */
1543
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001544static inline bool should_fail_request(struct hd_struct *part,
1545 unsigned int bytes)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001546{
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001547 return false;
Akinobu Mitac17bb492006-12-08 02:39:46 -08001548}
1549
1550#endif /* CONFIG_FAIL_MAKE_REQUEST */
1551
Jens Axboec07e2b42007-07-18 13:27:58 +02001552/*
1553 * Check whether this bio extends beyond the end of the device.
1554 */
1555static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1556{
1557 sector_t maxsector;
1558
1559 if (!nr_sectors)
1560 return 0;
1561
1562 /* Test device or partition size, when known. */
Mike Snitzer77304d22010-11-08 14:39:12 +01001563 maxsector = i_size_read(bio->bi_bdev->bd_inode) >> 9;
Jens Axboec07e2b42007-07-18 13:27:58 +02001564 if (maxsector) {
1565 sector_t sector = bio->bi_sector;
1566
1567 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1568 /*
1569 * This may well happen - the kernel calls bread()
1570 * without checking the size of the device, e.g., when
1571 * mounting a device.
1572 */
1573 handle_bad_sector(bio);
1574 return 1;
1575 }
1576 }
1577
1578 return 0;
1579}
1580
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001581static noinline_for_stack bool
1582generic_make_request_checks(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
Jens Axboe165125e2007-07-24 09:28:11 +02001584 struct request_queue *q;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001585 int nr_sectors = bio_sectors(bio);
Jens Axboe51fd77b2007-11-02 08:49:08 +01001586 int err = -EIO;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001587 char b[BDEVNAME_SIZE];
1588 struct hd_struct *part;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
1590 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Jens Axboec07e2b42007-07-18 13:27:58 +02001592 if (bio_check_eod(bio, nr_sectors))
1593 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001595 q = bdev_get_queue(bio->bi_bdev);
1596 if (unlikely(!q)) {
1597 printk(KERN_ERR
1598 "generic_make_request: Trying to access "
1599 "nonexistent block-device %s (%Lu)\n",
1600 bdevname(bio->bi_bdev, b),
1601 (long long) bio->bi_sector);
1602 goto end_io;
1603 }
1604
Steve Mucklef132c6c2012-06-06 18:30:57 -07001605 if (unlikely(!(bio->bi_rw & (REQ_DISCARD | REQ_SANITIZE)) &&
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001606 nr_sectors > queue_max_hw_sectors(q))) {
1607 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
1608 bdevname(bio->bi_bdev, b),
1609 bio_sectors(bio),
1610 queue_max_hw_sectors(q));
1611 goto end_io;
1612 }
1613
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001614 part = bio->bi_bdev->bd_part;
1615 if (should_fail_request(part, bio->bi_size) ||
1616 should_fail_request(&part_to_disk(part)->part0,
1617 bio->bi_size))
1618 goto end_io;
1619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 /*
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001621 * If this device has partitions, remap block n
1622 * of partition p to block n+start(p) of the disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001624 blk_partition_remap(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001626 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio))
1627 goto end_io;
1628
1629 if (bio_check_eod(bio, nr_sectors))
1630 goto end_io;
1631
1632 /*
1633 * Filter flush bio's early so that make_request based
1634 * drivers without flush support don't have to worry
1635 * about them.
1636 */
1637 if ((bio->bi_rw & (REQ_FLUSH | REQ_FUA)) && !q->flush_flags) {
1638 bio->bi_rw &= ~(REQ_FLUSH | REQ_FUA);
1639 if (!nr_sectors) {
1640 err = 0;
Tejun Heoa7384672008-11-28 13:32:03 +09001641 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 }
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001645 if ((bio->bi_rw & REQ_DISCARD) &&
1646 (!blk_queue_discard(q) ||
1647 ((bio->bi_rw & REQ_SECURE) &&
1648 !blk_queue_secdiscard(q)))) {
1649 err = -EOPNOTSUPP;
1650 goto end_io;
1651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Steve Mucklef132c6c2012-06-06 18:30:57 -07001653 if ((bio->bi_rw & REQ_SANITIZE) &&
1654 (!blk_queue_sanitize(q))) {
1655 pr_info("%s - got a SANITIZE request but the queue "
1656 "doesn't support sanitize requests", __func__);
1657 err = -EOPNOTSUPP;
1658 goto end_io;
1659 }
1660
Tejun Heobc16a4f2011-10-19 14:33:01 +02001661 if (blk_throtl_bio(q, bio))
1662 return false; /* throttled, will be resubmitted later */
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001663
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001664 trace_block_bio_queue(q, bio);
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001665 return true;
Tejun Heoa7384672008-11-28 13:32:03 +09001666
1667end_io:
1668 bio_endio(bio, err);
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001669 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670}
1671
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001672/**
1673 * generic_make_request - hand a buffer to its device driver for I/O
1674 * @bio: The bio describing the location in memory and on the device.
1675 *
1676 * generic_make_request() is used to make I/O requests of block
1677 * devices. It is passed a &struct bio, which describes the I/O that needs
1678 * to be done.
1679 *
1680 * generic_make_request() does not return any status. The
1681 * success/failure status of the request, along with notification of
1682 * completion, is delivered asynchronously through the bio->bi_end_io
1683 * function described (one day) else where.
1684 *
1685 * The caller of generic_make_request must make sure that bi_io_vec
1686 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1687 * set to describe the device address, and the
1688 * bi_end_io and optionally bi_private are set to describe how
1689 * completion notification should be signaled.
1690 *
1691 * generic_make_request and the drivers it calls may use bi_next if this
1692 * bio happens to be merged with someone else, and may resubmit the bio to
1693 * a lower device by calling into generic_make_request recursively, which
1694 * means the bio should NOT be touched after the call to ->make_request_fn.
Neil Brownd89d8792007-05-01 09:53:42 +02001695 */
1696void generic_make_request(struct bio *bio)
1697{
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001698 struct bio_list bio_list_on_stack;
1699
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001700 if (!generic_make_request_checks(bio))
1701 return;
1702
1703 /*
1704 * We only want one ->make_request_fn to be active at a time, else
1705 * stack usage with stacked devices could be a problem. So use
1706 * current->bio_list to keep a list of requests submited by a
1707 * make_request_fn function. current->bio_list is also used as a
1708 * flag to say if generic_make_request is currently active in this
1709 * task or not. If it is NULL, then no make_request is active. If
1710 * it is non-NULL, then a make_request is active, and new requests
1711 * should be added at the tail
1712 */
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001713 if (current->bio_list) {
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001714 bio_list_add(current->bio_list, bio);
Neil Brownd89d8792007-05-01 09:53:42 +02001715 return;
1716 }
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001717
Neil Brownd89d8792007-05-01 09:53:42 +02001718 /* following loop may be a bit non-obvious, and so deserves some
1719 * explanation.
1720 * Before entering the loop, bio->bi_next is NULL (as all callers
1721 * ensure that) so we have a list with a single bio.
1722 * We pretend that we have just taken it off a longer list, so
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001723 * we assign bio_list to a pointer to the bio_list_on_stack,
1724 * thus initialising the bio_list of new bios to be
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001725 * added. ->make_request() may indeed add some more bios
Neil Brownd89d8792007-05-01 09:53:42 +02001726 * through a recursive call to generic_make_request. If it
1727 * did, we find a non-NULL value in bio_list and re-enter the loop
1728 * from the top. In this case we really did just take the bio
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001729 * of the top of the list (no pretending) and so remove it from
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001730 * bio_list, and call into ->make_request() again.
Neil Brownd89d8792007-05-01 09:53:42 +02001731 */
1732 BUG_ON(bio->bi_next);
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001733 bio_list_init(&bio_list_on_stack);
1734 current->bio_list = &bio_list_on_stack;
Neil Brownd89d8792007-05-01 09:53:42 +02001735 do {
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001736 struct request_queue *q = bdev_get_queue(bio->bi_bdev);
1737
1738 q->make_request_fn(q, bio);
1739
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001740 bio = bio_list_pop(current->bio_list);
Neil Brownd89d8792007-05-01 09:53:42 +02001741 } while (bio);
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001742 current->bio_list = NULL; /* deactivate */
Neil Brownd89d8792007-05-01 09:53:42 +02001743}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744EXPORT_SYMBOL(generic_make_request);
1745
1746/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001747 * submit_bio - submit a bio to the block device layer for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1749 * @bio: The &struct bio which describes the I/O
1750 *
1751 * submit_bio() is very similar in purpose to generic_make_request(), and
1752 * uses that function to do most of the work. Both are fairly rough
Randy Dunlap710027a2008-08-19 20:13:11 +02001753 * interfaces; @bio must be presetup and ready for I/O.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 *
1755 */
1756void submit_bio(int rw, struct bio *bio)
1757{
1758 int count = bio_sectors(bio);
1759
Jens Axboe22e2c502005-06-27 10:55:12 +02001760 bio->bi_rw |= rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Jens Axboebf2de6f2007-09-27 13:01:25 +02001762 /*
1763 * If it's a regular read/write or a barrier with data attached,
1764 * go through the normal accounting stuff before submission.
1765 */
Maya Erez73937f52012-05-24 23:33:05 +03001766 if (bio_has_data(bio) &&
1767 (!(rw & (REQ_DISCARD | REQ_SANITIZE)))) {
Jens Axboebf2de6f2007-09-27 13:01:25 +02001768 if (rw & WRITE) {
1769 count_vm_events(PGPGOUT, count);
1770 } else {
1771 task_io_account_read(bio->bi_size);
1772 count_vm_events(PGPGIN, count);
1773 }
1774
1775 if (unlikely(block_dump)) {
1776 char b[BDEVNAME_SIZE];
San Mehat8dcbdc72010-09-14 08:48:01 +02001777 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s (%u sectors)\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001778 current->comm, task_pid_nr(current),
Jens Axboebf2de6f2007-09-27 13:01:25 +02001779 (rw & WRITE) ? "WRITE" : "READ",
1780 (unsigned long long)bio->bi_sector,
San Mehat8dcbdc72010-09-14 08:48:01 +02001781 bdevname(bio->bi_bdev, b),
1782 count);
Jens Axboebf2de6f2007-09-27 13:01:25 +02001783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 }
1785
1786 generic_make_request(bio);
1787}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788EXPORT_SYMBOL(submit_bio);
1789
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001790/**
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001791 * blk_rq_check_limits - Helper function to check a request for the queue limit
1792 * @q: the queue
1793 * @rq: the request being checked
1794 *
1795 * Description:
1796 * @rq may have been made based on weaker limitations of upper-level queues
1797 * in request stacking drivers, and it may violate the limitation of @q.
1798 * Since the block layer and the underlying device driver trust @rq
1799 * after it is inserted to @q, it should be checked against @q before
1800 * the insertion using this generic function.
1801 *
1802 * This function should also be useful for request stacking drivers
Stefan Weileef35c22010-08-06 21:11:15 +02001803 * in some cases below, so export this function.
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001804 * Request stacking drivers like request-based dm may change the queue
1805 * limits while requests are in the queue (e.g. dm's table swapping).
1806 * Such request stacking drivers should check those requests agaist
1807 * the new queue limits again when they dispatch those requests,
1808 * although such checkings are also done against the old queue limits
1809 * when submitting requests.
1810 */
1811int blk_rq_check_limits(struct request_queue *q, struct request *rq)
1812{
Maya Erez73937f52012-05-24 23:33:05 +03001813 if (rq->cmd_flags & (REQ_DISCARD | REQ_SANITIZE))
ike Snitzer33839772010-08-08 12:11:33 -04001814 return 0;
1815
Martin K. Petersenae03bf62009-05-22 17:17:50 -04001816 if (blk_rq_sectors(rq) > queue_max_sectors(q) ||
1817 blk_rq_bytes(rq) > queue_max_hw_sectors(q) << 9) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001818 printk(KERN_ERR "%s: over max size limit.\n", __func__);
1819 return -EIO;
1820 }
1821
1822 /*
1823 * queue's settings related to segment counting like q->bounce_pfn
1824 * may differ from that of other stacking queues.
1825 * Recalculate it to check the request correctly on this queue's
1826 * limitation.
1827 */
1828 blk_recalc_rq_segments(rq);
Martin K. Petersen8a783622010-02-26 00:20:39 -05001829 if (rq->nr_phys_segments > queue_max_segments(q)) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001830 printk(KERN_ERR "%s: over max segments limit.\n", __func__);
1831 return -EIO;
1832 }
1833
1834 return 0;
1835}
1836EXPORT_SYMBOL_GPL(blk_rq_check_limits);
1837
1838/**
1839 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
1840 * @q: the queue to submit the request
1841 * @rq: the request being queued
1842 */
1843int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
1844{
1845 unsigned long flags;
Jeff Moyer4853aba2011-08-15 21:37:25 +02001846 int where = ELEVATOR_INSERT_BACK;
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001847
1848 if (blk_rq_check_limits(q, rq))
1849 return -EIO;
1850
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001851 if (rq->rq_disk &&
1852 should_fail_request(&rq->rq_disk->part0, blk_rq_bytes(rq)))
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001853 return -EIO;
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001854
1855 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo8ba61432011-12-14 00:33:37 +01001856 if (unlikely(blk_queue_dead(q))) {
1857 spin_unlock_irqrestore(q->queue_lock, flags);
1858 return -ENODEV;
1859 }
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001860
1861 /*
1862 * Submitting request must be dequeued before calling this function
1863 * because it will be linked to another request_queue
1864 */
1865 BUG_ON(blk_queued_rq(rq));
1866
Jeff Moyer4853aba2011-08-15 21:37:25 +02001867 if (rq->cmd_flags & (REQ_FLUSH|REQ_FUA))
1868 where = ELEVATOR_INSERT_FLUSH;
1869
1870 add_acct_request(q, rq, where);
Jeff Moyere67b77c2011-10-17 12:57:23 +02001871 if (where == ELEVATOR_INSERT_FLUSH)
1872 __blk_run_queue(q);
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001873 spin_unlock_irqrestore(q->queue_lock, flags);
1874
1875 return 0;
1876}
1877EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
1878
Tejun Heo80a761f2009-07-03 17:48:17 +09001879/**
1880 * blk_rq_err_bytes - determine number of bytes till the next failure boundary
1881 * @rq: request to examine
1882 *
1883 * Description:
1884 * A request could be merge of IOs which require different failure
1885 * handling. This function determines the number of bytes which
1886 * can be failed from the beginning of the request without
1887 * crossing into area which need to be retried further.
1888 *
1889 * Return:
1890 * The number of bytes to fail.
1891 *
1892 * Context:
1893 * queue_lock must be held.
1894 */
1895unsigned int blk_rq_err_bytes(const struct request *rq)
1896{
1897 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
1898 unsigned int bytes = 0;
1899 struct bio *bio;
1900
1901 if (!(rq->cmd_flags & REQ_MIXED_MERGE))
1902 return blk_rq_bytes(rq);
1903
1904 /*
1905 * Currently the only 'mixing' which can happen is between
1906 * different fastfail types. We can safely fail portions
1907 * which have all the failfast bits that the first one has -
1908 * the ones which are at least as eager to fail as the first
1909 * one.
1910 */
1911 for (bio = rq->bio; bio; bio = bio->bi_next) {
1912 if ((bio->bi_rw & ff) != ff)
1913 break;
1914 bytes += bio->bi_size;
1915 }
1916
1917 /* this could lead to infinite loop */
1918 BUG_ON(blk_rq_bytes(rq) && !bytes);
1919 return bytes;
1920}
1921EXPORT_SYMBOL_GPL(blk_rq_err_bytes);
1922
Jens Axboebc58ba92009-01-23 10:54:44 +01001923static void blk_account_io_completion(struct request *req, unsigned int bytes)
1924{
Jens Axboec2553b52009-04-24 08:10:11 +02001925 if (blk_do_io_stat(req)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01001926 const int rw = rq_data_dir(req);
1927 struct hd_struct *part;
1928 int cpu;
1929
1930 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +01001931 part = req->part;
Jens Axboebc58ba92009-01-23 10:54:44 +01001932 part_stat_add(cpu, part, sectors[rw], bytes >> 9);
1933 part_stat_unlock();
1934 }
1935}
1936
1937static void blk_account_io_done(struct request *req)
1938{
Jens Axboebc58ba92009-01-23 10:54:44 +01001939 /*
Tejun Heodd4c1332010-09-03 11:56:16 +02001940 * Account IO completion. flush_rq isn't accounted as a
1941 * normal IO on queueing nor completion. Accounting the
1942 * containing request is enough.
Jens Axboebc58ba92009-01-23 10:54:44 +01001943 */
Tejun Heo414b4ff2011-01-25 12:43:49 +01001944 if (blk_do_io_stat(req) && !(req->cmd_flags & REQ_FLUSH_SEQ)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01001945 unsigned long duration = jiffies - req->start_time;
1946 const int rw = rq_data_dir(req);
1947 struct hd_struct *part;
1948 int cpu;
1949
1950 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +01001951 part = req->part;
Jens Axboebc58ba92009-01-23 10:54:44 +01001952
1953 part_stat_inc(cpu, part, ios[rw]);
1954 part_stat_add(cpu, part, ticks[rw], duration);
1955 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001956 part_dec_in_flight(part, rw);
Jens Axboebc58ba92009-01-23 10:54:44 +01001957
Jens Axboe6c23a962011-01-07 08:43:37 +01001958 hd_struct_put(part);
Jens Axboebc58ba92009-01-23 10:54:44 +01001959 part_stat_unlock();
1960 }
1961}
1962
Tejun Heo53a08802008-12-03 12:41:26 +01001963/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09001964 * blk_peek_request - peek at the top of a request queue
1965 * @q: request queue to peek at
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001966 *
1967 * Description:
Tejun Heo9934c8c2009-05-08 11:54:16 +09001968 * Return the request at the top of @q. The returned request
1969 * should be started using blk_start_request() before LLD starts
1970 * processing it.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001971 *
1972 * Return:
Tejun Heo9934c8c2009-05-08 11:54:16 +09001973 * Pointer to the request at the top of @q if available. Null
1974 * otherwise.
1975 *
1976 * Context:
1977 * queue_lock must be held.
1978 */
1979struct request *blk_peek_request(struct request_queue *q)
Tejun Heo158dbda2009-04-23 11:05:18 +09001980{
1981 struct request *rq;
1982 int ret;
1983
1984 while ((rq = __elv_next_request(q)) != NULL) {
1985 if (!(rq->cmd_flags & REQ_STARTED)) {
1986 /*
1987 * This is the first time the device driver
1988 * sees this request (possibly after
1989 * requeueing). Notify IO scheduler.
1990 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001991 if (rq->cmd_flags & REQ_SORTED)
Tejun Heo158dbda2009-04-23 11:05:18 +09001992 elv_activate_rq(q, rq);
1993
1994 /*
1995 * just mark as started even if we don't start
1996 * it, a request that has been delayed should
1997 * not be passed by new incoming requests
1998 */
1999 rq->cmd_flags |= REQ_STARTED;
2000 trace_block_rq_issue(q, rq);
2001 }
2002
2003 if (!q->boundary_rq || q->boundary_rq == rq) {
2004 q->end_sector = rq_end_sector(rq);
2005 q->boundary_rq = NULL;
2006 }
2007
2008 if (rq->cmd_flags & REQ_DONTPREP)
2009 break;
2010
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002011 if (q->dma_drain_size && blk_rq_bytes(rq)) {
Tejun Heo158dbda2009-04-23 11:05:18 +09002012 /*
2013 * make sure space for the drain appears we
2014 * know we can do this because max_hw_segments
2015 * has been adjusted to be one fewer than the
2016 * device can handle
2017 */
2018 rq->nr_phys_segments++;
2019 }
2020
2021 if (!q->prep_rq_fn)
2022 break;
2023
2024 ret = q->prep_rq_fn(q, rq);
2025 if (ret == BLKPREP_OK) {
2026 break;
2027 } else if (ret == BLKPREP_DEFER) {
2028 /*
2029 * the request may have been (partially) prepped.
2030 * we need to keep this request in the front to
2031 * avoid resource deadlock. REQ_STARTED will
2032 * prevent other fs requests from passing this one.
2033 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002034 if (q->dma_drain_size && blk_rq_bytes(rq) &&
Tejun Heo158dbda2009-04-23 11:05:18 +09002035 !(rq->cmd_flags & REQ_DONTPREP)) {
2036 /*
2037 * remove the space for the drain we added
2038 * so that we don't add it again
2039 */
2040 --rq->nr_phys_segments;
2041 }
2042
2043 rq = NULL;
2044 break;
2045 } else if (ret == BLKPREP_KILL) {
2046 rq->cmd_flags |= REQ_QUIET;
James Bottomleyc143dc92009-05-30 06:43:49 +02002047 /*
2048 * Mark this request as started so we don't trigger
2049 * any debug logic in the end I/O path.
2050 */
2051 blk_start_request(rq);
Tejun Heo40cbbb72009-04-23 11:05:19 +09002052 __blk_end_request_all(rq, -EIO);
Tejun Heo158dbda2009-04-23 11:05:18 +09002053 } else {
2054 printk(KERN_ERR "%s: bad return=%d\n", __func__, ret);
2055 break;
2056 }
2057 }
2058
2059 return rq;
2060}
Tejun Heo9934c8c2009-05-08 11:54:16 +09002061EXPORT_SYMBOL(blk_peek_request);
Tejun Heo158dbda2009-04-23 11:05:18 +09002062
Tejun Heo9934c8c2009-05-08 11:54:16 +09002063void blk_dequeue_request(struct request *rq)
Tejun Heo158dbda2009-04-23 11:05:18 +09002064{
Tejun Heo9934c8c2009-05-08 11:54:16 +09002065 struct request_queue *q = rq->q;
2066
Tejun Heo158dbda2009-04-23 11:05:18 +09002067 BUG_ON(list_empty(&rq->queuelist));
2068 BUG_ON(ELV_ON_HASH(rq));
2069
2070 list_del_init(&rq->queuelist);
2071
2072 /*
2073 * the time frame between a request being removed from the lists
2074 * and to it is freed is accounted as io that is in progress at
2075 * the driver side.
2076 */
Divyesh Shah91952912010-04-01 15:01:41 -07002077 if (blk_account_rq(rq)) {
Jens Axboe0a7ae2f2009-05-20 08:54:31 +02002078 q->in_flight[rq_is_sync(rq)]++;
Divyesh Shah91952912010-04-01 15:01:41 -07002079 set_io_start_time_ns(rq);
2080 }
Tejun Heo158dbda2009-04-23 11:05:18 +09002081}
2082
Tejun Heo5efccd12009-04-23 11:05:18 +09002083/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09002084 * blk_start_request - start request processing on the driver
2085 * @req: request to dequeue
2086 *
2087 * Description:
2088 * Dequeue @req and start timeout timer on it. This hands off the
2089 * request to the driver.
2090 *
2091 * Block internal functions which don't want to start timer should
2092 * call blk_dequeue_request().
2093 *
2094 * Context:
2095 * queue_lock must be held.
2096 */
2097void blk_start_request(struct request *req)
2098{
2099 blk_dequeue_request(req);
2100
2101 /*
Tejun Heo5f49f632009-05-19 18:33:05 +09002102 * We are now handing the request to the hardware, initialize
2103 * resid_len to full count and add the timeout handler.
Tejun Heo9934c8c2009-05-08 11:54:16 +09002104 */
Tejun Heo5f49f632009-05-19 18:33:05 +09002105 req->resid_len = blk_rq_bytes(req);
FUJITA Tomonoridbb66c42009-06-09 05:47:10 +02002106 if (unlikely(blk_bidi_rq(req)))
2107 req->next_rq->resid_len = blk_rq_bytes(req->next_rq);
2108
Tejun Heo9934c8c2009-05-08 11:54:16 +09002109 blk_add_timer(req);
2110}
2111EXPORT_SYMBOL(blk_start_request);
2112
2113/**
2114 * blk_fetch_request - fetch a request from a request queue
2115 * @q: request queue to fetch a request from
2116 *
2117 * Description:
2118 * Return the request at the top of @q. The request is started on
2119 * return and LLD can start processing it immediately.
2120 *
2121 * Return:
2122 * Pointer to the request at the top of @q if available. Null
2123 * otherwise.
2124 *
2125 * Context:
2126 * queue_lock must be held.
2127 */
2128struct request *blk_fetch_request(struct request_queue *q)
2129{
2130 struct request *rq;
2131
2132 rq = blk_peek_request(q);
Tatyana Brokhman88fd1b42012-12-04 16:04:15 +02002133 if (rq) {
Tatyana Brokhman2c415182013-03-12 21:02:33 +02002134 if (rq->cmd_flags & REQ_URGENT) {
2135 WARN_ON(q->dispatched_urgent);
Tatyana Brokhman88fd1b42012-12-04 16:04:15 +02002136 q->dispatched_urgent = true;
Tatyana Brokhman88fd1b42012-12-04 16:04:15 +02002137 }
Tejun Heo9934c8c2009-05-08 11:54:16 +09002138 blk_start_request(rq);
Tatyana Brokhman88fd1b42012-12-04 16:04:15 +02002139 }
Tejun Heo9934c8c2009-05-08 11:54:16 +09002140 return rq;
2141}
2142EXPORT_SYMBOL(blk_fetch_request);
2143
2144/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002145 * blk_update_request - Special helper function for request stacking drivers
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002146 * @req: the request being processed
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002147 * @error: %0 for success, < %0 for error
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002148 * @nr_bytes: number of bytes to complete @req
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002149 *
2150 * Description:
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002151 * Ends I/O on a number of bytes attached to @req, but doesn't complete
2152 * the request structure even if @req doesn't have leftover.
2153 * If @req has leftover, sets it up for the next range of segments.
Tejun Heo2e60e022009-04-23 11:05:18 +09002154 *
2155 * This special helper function is only for request stacking drivers
2156 * (e.g. request-based dm) so that they can handle partial completion.
2157 * Actual device drivers should use blk_end_request instead.
2158 *
2159 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
2160 * %false return from this function.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002161 *
2162 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002163 * %false - this request doesn't have any more data
2164 * %true - this request has more data
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002165 **/
Tejun Heo2e60e022009-04-23 11:05:18 +09002166bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167{
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05002168 int total_bytes, bio_nbytes, next_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 struct bio *bio;
2170
Tejun Heo2e60e022009-04-23 11:05:18 +09002171 if (!req->bio)
2172 return false;
2173
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01002174 trace_block_rq_complete(req->q, req);
Jens Axboe2056a782006-03-23 20:00:26 +01002175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 /*
Tejun Heo6f414692009-04-19 07:00:41 +09002177 * For fs requests, rq is just carrier of independent bio's
2178 * and each partial completion should be handled separately.
2179 * Reset per-request error on each partial completion.
2180 *
2181 * TODO: tj: This is too subtle. It would be better to let
2182 * low level drivers do what they see fit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002184 if (req->cmd_type == REQ_TYPE_FS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 req->errors = 0;
2186
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002187 if (error && req->cmd_type == REQ_TYPE_FS &&
2188 !(req->cmd_flags & REQ_QUIET)) {
Hannes Reinecke79775562011-01-18 10:13:13 +01002189 char *error_type;
2190
2191 switch (error) {
2192 case -ENOLINK:
2193 error_type = "recoverable transport";
2194 break;
2195 case -EREMOTEIO:
2196 error_type = "critical target";
2197 break;
2198 case -EBADE:
2199 error_type = "critical nexus";
2200 break;
2201 case -EIO:
2202 default:
2203 error_type = "I/O";
2204 break;
2205 }
2206 printk(KERN_ERR "end_request: %s error, dev %s, sector %llu\n",
2207 error_type, req->rq_disk ? req->rq_disk->disk_name : "?",
2208 (unsigned long long)blk_rq_pos(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 }
2210
Jens Axboebc58ba92009-01-23 10:54:44 +01002211 blk_account_io_completion(req, nr_bytes);
Jens Axboed72d9042005-11-01 08:35:42 +01002212
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 total_bytes = bio_nbytes = 0;
2214 while ((bio = req->bio) != NULL) {
2215 int nbytes;
2216
2217 if (nr_bytes >= bio->bi_size) {
2218 req->bio = bio->bi_next;
2219 nbytes = bio->bi_size;
NeilBrown5bb23a62007-09-27 12:46:13 +02002220 req_bio_endio(req, bio, nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 next_idx = 0;
2222 bio_nbytes = 0;
2223 } else {
2224 int idx = bio->bi_idx + next_idx;
2225
Kazuhisa Ichikawaaf498d72009-05-12 13:27:45 +02002226 if (unlikely(idx >= bio->bi_vcnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 blk_dump_rq_flags(req, "__end_that");
Jens Axboe6728cb02008-01-31 13:03:55 +01002228 printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
Kazuhisa Ichikawaaf498d72009-05-12 13:27:45 +02002229 __func__, idx, bio->bi_vcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 break;
2231 }
2232
2233 nbytes = bio_iovec_idx(bio, idx)->bv_len;
2234 BIO_BUG_ON(nbytes > bio->bi_size);
2235
2236 /*
2237 * not a complete bvec done
2238 */
2239 if (unlikely(nbytes > nr_bytes)) {
2240 bio_nbytes += nr_bytes;
2241 total_bytes += nr_bytes;
2242 break;
2243 }
2244
2245 /*
2246 * advance to the next vector
2247 */
2248 next_idx++;
2249 bio_nbytes += nbytes;
2250 }
2251
2252 total_bytes += nbytes;
2253 nr_bytes -= nbytes;
2254
Jens Axboe6728cb02008-01-31 13:03:55 +01002255 bio = req->bio;
2256 if (bio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 /*
2258 * end more in this run, or just return 'not-done'
2259 */
2260 if (unlikely(nr_bytes <= 0))
2261 break;
2262 }
2263 }
2264
2265 /*
2266 * completely done
2267 */
Tejun Heo2e60e022009-04-23 11:05:18 +09002268 if (!req->bio) {
2269 /*
2270 * Reset counters so that the request stacking driver
2271 * can find how many bytes remain in the request
2272 * later.
2273 */
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002274 req->__data_len = 0;
Tejun Heo2e60e022009-04-23 11:05:18 +09002275 return false;
2276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
2278 /*
2279 * if the request wasn't completed, update state
2280 */
2281 if (bio_nbytes) {
NeilBrown5bb23a62007-09-27 12:46:13 +02002282 req_bio_endio(req, bio, bio_nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 bio->bi_idx += next_idx;
2284 bio_iovec(bio)->bv_offset += nr_bytes;
2285 bio_iovec(bio)->bv_len -= nr_bytes;
2286 }
2287
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002288 req->__data_len -= total_bytes;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002289 req->buffer = bio_data(req->bio);
2290
2291 /* update sector only for requests with clear definition of sector */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002292 if (req->cmd_type == REQ_TYPE_FS || (req->cmd_flags & REQ_DISCARD))
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002293 req->__sector += total_bytes >> 9;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002294
Tejun Heo80a761f2009-07-03 17:48:17 +09002295 /* mixed attributes always follow the first bio */
2296 if (req->cmd_flags & REQ_MIXED_MERGE) {
2297 req->cmd_flags &= ~REQ_FAILFAST_MASK;
2298 req->cmd_flags |= req->bio->bi_rw & REQ_FAILFAST_MASK;
2299 }
2300
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002301 /*
2302 * If total number of sectors is less than the first segment
2303 * size, something has gone terribly wrong.
2304 */
2305 if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
Jens Axboe81829242011-03-30 09:51:33 +02002306 blk_dump_rq_flags(req, "request botched");
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002307 req->__data_len = blk_rq_cur_bytes(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002308 }
2309
2310 /* recalculate the number of segments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 blk_recalc_rq_segments(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002312
Tejun Heo2e60e022009-04-23 11:05:18 +09002313 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314}
Tejun Heo2e60e022009-04-23 11:05:18 +09002315EXPORT_SYMBOL_GPL(blk_update_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Tejun Heo2e60e022009-04-23 11:05:18 +09002317static bool blk_update_bidi_request(struct request *rq, int error,
2318 unsigned int nr_bytes,
2319 unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002320{
Tejun Heo2e60e022009-04-23 11:05:18 +09002321 if (blk_update_request(rq, error, nr_bytes))
2322 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002323
Tejun Heo2e60e022009-04-23 11:05:18 +09002324 /* Bidi request must be completed as a whole */
2325 if (unlikely(blk_bidi_rq(rq)) &&
2326 blk_update_request(rq->next_rq, error, bidi_bytes))
2327 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002328
Jens Axboee2e1a142010-06-09 10:42:09 +02002329 if (blk_queue_add_random(rq->q))
2330 add_disk_randomness(rq->rq_disk);
Tejun Heo2e60e022009-04-23 11:05:18 +09002331
2332 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333}
2334
James Bottomley28018c22010-07-01 19:49:17 +09002335/**
2336 * blk_unprep_request - unprepare a request
2337 * @req: the request
2338 *
2339 * This function makes a request ready for complete resubmission (or
2340 * completion). It happens only after all error handling is complete,
2341 * so represents the appropriate moment to deallocate any resources
2342 * that were allocated to the request in the prep_rq_fn. The queue
2343 * lock is held when calling this.
2344 */
2345void blk_unprep_request(struct request *req)
2346{
2347 struct request_queue *q = req->q;
2348
2349 req->cmd_flags &= ~REQ_DONTPREP;
2350 if (q->unprep_rq_fn)
2351 q->unprep_rq_fn(q, req);
2352}
2353EXPORT_SYMBOL_GPL(blk_unprep_request);
2354
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355/*
2356 * queue lock must be held
2357 */
Tejun Heo2e60e022009-04-23 11:05:18 +09002358static void blk_finish_request(struct request *req, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359{
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002360 if (blk_rq_tagged(req))
2361 blk_queue_end_tag(req->q, req);
2362
James Bottomleyba396a62009-05-27 14:17:08 +02002363 BUG_ON(blk_queued_rq(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002365 if (unlikely(laptop_mode) && req->cmd_type == REQ_TYPE_FS)
Matthew Garrett31373d02010-04-06 14:25:14 +02002366 laptop_io_completion(&req->q->backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
Mike Andersone78042e2008-10-30 02:16:20 -07002368 blk_delete_timer(req);
2369
James Bottomley28018c22010-07-01 19:49:17 +09002370 if (req->cmd_flags & REQ_DONTPREP)
2371 blk_unprep_request(req);
2372
2373
Jens Axboebc58ba92009-01-23 10:54:44 +01002374 blk_account_io_done(req);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002375
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 if (req->end_io)
Tejun Heo8ffdc652006-01-06 09:49:03 +01002377 req->end_io(req, error);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002378 else {
2379 if (blk_bidi_rq(req))
2380 __blk_put_request(req->next_rq->q, req->next_rq);
2381
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 __blk_put_request(req->q, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 }
2384}
2385
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05002386/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002387 * blk_end_bidi_request - Complete a bidi request
2388 * @rq: the request to complete
Randy Dunlap710027a2008-08-19 20:13:11 +02002389 * @error: %0 for success, < %0 for error
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002390 * @nr_bytes: number of bytes to complete @rq
2391 * @bidi_bytes: number of bytes to complete @rq->next_rq
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002392 *
2393 * Description:
2394 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
Tejun Heo2e60e022009-04-23 11:05:18 +09002395 * Drivers that supports bidi can safely call this member for any
2396 * type of request, bidi or uni. In the later case @bidi_bytes is
2397 * just ignored.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002398 *
2399 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002400 * %false - we are done with this request
2401 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002402 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002403static bool blk_end_bidi_request(struct request *rq, int error,
2404 unsigned int nr_bytes, unsigned int bidi_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002405{
2406 struct request_queue *q = rq->q;
Tejun Heo2e60e022009-04-23 11:05:18 +09002407 unsigned long flags;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002408
Tejun Heo2e60e022009-04-23 11:05:18 +09002409 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2410 return true;
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002411
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002412 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo2e60e022009-04-23 11:05:18 +09002413 blk_finish_request(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002414 spin_unlock_irqrestore(q->queue_lock, flags);
2415
Tejun Heo2e60e022009-04-23 11:05:18 +09002416 return false;
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002417}
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002418
2419/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002420 * __blk_end_bidi_request - Complete a bidi request with queue lock held
2421 * @rq: the request to complete
2422 * @error: %0 for success, < %0 for error
2423 * @nr_bytes: number of bytes to complete @rq
2424 * @bidi_bytes: number of bytes to complete @rq->next_rq
Tejun Heo5efccd12009-04-23 11:05:18 +09002425 *
2426 * Description:
Tejun Heo2e60e022009-04-23 11:05:18 +09002427 * Identical to blk_end_bidi_request() except that queue lock is
2428 * assumed to be locked on entry and remains so on return.
Tejun Heo5efccd12009-04-23 11:05:18 +09002429 *
Tejun Heo2e60e022009-04-23 11:05:18 +09002430 * Return:
2431 * %false - we are done with this request
2432 * %true - still buffers pending for this request
Tejun Heo5efccd12009-04-23 11:05:18 +09002433 **/
Jeff Moyer4853aba2011-08-15 21:37:25 +02002434bool __blk_end_bidi_request(struct request *rq, int error,
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002435 unsigned int nr_bytes, unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002436{
Tejun Heo2e60e022009-04-23 11:05:18 +09002437 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2438 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002439
Tejun Heo2e60e022009-04-23 11:05:18 +09002440 blk_finish_request(rq, error);
Tejun Heo5efccd12009-04-23 11:05:18 +09002441
Tejun Heo2e60e022009-04-23 11:05:18 +09002442 return false;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002443}
2444
2445/**
2446 * blk_end_request - Helper function for drivers to complete the request.
2447 * @rq: the request being processed
2448 * @error: %0 for success, < %0 for error
2449 * @nr_bytes: number of bytes to complete
2450 *
2451 * Description:
2452 * Ends I/O on a number of bytes attached to @rq.
2453 * If @rq has leftover, sets it up for the next range of segments.
2454 *
2455 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002456 * %false - we are done with this request
2457 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002458 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002459bool blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002460{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002461 return blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002462}
Jens Axboe56ad1742009-07-28 22:11:24 +02002463EXPORT_SYMBOL(blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002464
2465/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002466 * blk_end_request_all - Helper function for drives to finish the request.
2467 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002468 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002469 *
2470 * Description:
2471 * Completely finish @rq.
2472 */
2473void blk_end_request_all(struct request *rq, int error)
2474{
2475 bool pending;
2476 unsigned int bidi_bytes = 0;
2477
2478 if (unlikely(blk_bidi_rq(rq)))
2479 bidi_bytes = blk_rq_bytes(rq->next_rq);
2480
2481 pending = blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2482 BUG_ON(pending);
2483}
Jens Axboe56ad1742009-07-28 22:11:24 +02002484EXPORT_SYMBOL(blk_end_request_all);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002485
2486/**
2487 * blk_end_request_cur - Helper function to finish the current request chunk.
2488 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002489 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002490 *
2491 * Description:
2492 * Complete the current consecutively mapped chunk from @rq.
2493 *
2494 * Return:
2495 * %false - we are done with this request
2496 * %true - still buffers pending for this request
2497 */
2498bool blk_end_request_cur(struct request *rq, int error)
2499{
2500 return blk_end_request(rq, error, blk_rq_cur_bytes(rq));
2501}
Jens Axboe56ad1742009-07-28 22:11:24 +02002502EXPORT_SYMBOL(blk_end_request_cur);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002503
2504/**
Tejun Heo80a761f2009-07-03 17:48:17 +09002505 * blk_end_request_err - Finish a request till the next failure boundary.
2506 * @rq: the request to finish till the next failure boundary for
2507 * @error: must be negative errno
2508 *
2509 * Description:
2510 * Complete @rq till the next failure boundary.
2511 *
2512 * Return:
2513 * %false - we are done with this request
2514 * %true - still buffers pending for this request
2515 */
2516bool blk_end_request_err(struct request *rq, int error)
2517{
2518 WARN_ON(error >= 0);
2519 return blk_end_request(rq, error, blk_rq_err_bytes(rq));
2520}
2521EXPORT_SYMBOL_GPL(blk_end_request_err);
2522
2523/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002524 * __blk_end_request - Helper function for drivers to complete the request.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002525 * @rq: the request being processed
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002526 * @error: %0 for success, < %0 for error
2527 * @nr_bytes: number of bytes to complete
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002528 *
2529 * Description:
2530 * Must be called with queue lock held unlike blk_end_request().
2531 *
2532 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002533 * %false - we are done with this request
2534 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002535 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002536bool __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002537{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002538 return __blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002539}
Jens Axboe56ad1742009-07-28 22:11:24 +02002540EXPORT_SYMBOL(__blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002541
2542/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002543 * __blk_end_request_all - Helper function for drives to finish the request.
2544 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002545 * @error: %0 for success, < %0 for error
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002546 *
2547 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002548 * Completely finish @rq. Must be called with queue lock held.
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002549 */
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002550void __blk_end_request_all(struct request *rq, int error)
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002551{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002552 bool pending;
2553 unsigned int bidi_bytes = 0;
2554
2555 if (unlikely(blk_bidi_rq(rq)))
2556 bidi_bytes = blk_rq_bytes(rq->next_rq);
2557
2558 pending = __blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2559 BUG_ON(pending);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002560}
Jens Axboe56ad1742009-07-28 22:11:24 +02002561EXPORT_SYMBOL(__blk_end_request_all);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002562
2563/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002564 * __blk_end_request_cur - Helper function to finish the current request chunk.
2565 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002566 * @error: %0 for success, < %0 for error
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002567 *
2568 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002569 * Complete the current consecutively mapped chunk from @rq. Must
2570 * be called with queue lock held.
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002571 *
2572 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002573 * %false - we are done with this request
2574 * %true - still buffers pending for this request
2575 */
2576bool __blk_end_request_cur(struct request *rq, int error)
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002577{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002578 return __blk_end_request(rq, error, blk_rq_cur_bytes(rq));
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002579}
Jens Axboe56ad1742009-07-28 22:11:24 +02002580EXPORT_SYMBOL(__blk_end_request_cur);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002581
Tejun Heo80a761f2009-07-03 17:48:17 +09002582/**
2583 * __blk_end_request_err - Finish a request till the next failure boundary.
2584 * @rq: the request to finish till the next failure boundary for
2585 * @error: must be negative errno
2586 *
2587 * Description:
2588 * Complete @rq till the next failure boundary. Must be called
2589 * with queue lock held.
2590 *
2591 * Return:
2592 * %false - we are done with this request
2593 * %true - still buffers pending for this request
2594 */
2595bool __blk_end_request_err(struct request *rq, int error)
2596{
2597 WARN_ON(error >= 0);
2598 return __blk_end_request(rq, error, blk_rq_err_bytes(rq));
2599}
2600EXPORT_SYMBOL_GPL(__blk_end_request_err);
2601
Jens Axboe86db1e22008-01-29 14:53:40 +01002602void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2603 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604{
Tejun Heoa82afdf2009-07-03 17:48:16 +09002605 /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002606 rq->cmd_flags |= bio->bi_rw & REQ_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
David Woodhousefb2dce82008-08-05 18:01:53 +01002608 if (bio_has_data(bio)) {
2609 rq->nr_phys_segments = bio_phys_segments(q, bio);
David Woodhousefb2dce82008-08-05 18:01:53 +01002610 rq->buffer = bio_data(bio);
2611 }
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002612 rq->__data_len = bio->bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 rq->bio = rq->biotail = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
NeilBrown66846572007-08-16 13:31:28 +02002615 if (bio->bi_bdev)
2616 rq->rq_disk = bio->bi_bdev->bd_disk;
2617}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
Ilya Loginov2d4dc892009-11-26 09:16:19 +01002619#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
2620/**
2621 * rq_flush_dcache_pages - Helper function to flush all pages in a request
2622 * @rq: the request to be flushed
2623 *
2624 * Description:
2625 * Flush all pages in @rq.
2626 */
2627void rq_flush_dcache_pages(struct request *rq)
2628{
2629 struct req_iterator iter;
2630 struct bio_vec *bvec;
2631
2632 rq_for_each_segment(bvec, rq, iter)
2633 flush_dcache_page(bvec->bv_page);
2634}
2635EXPORT_SYMBOL_GPL(rq_flush_dcache_pages);
2636#endif
2637
Kiyoshi Uedaef9e3fa2008-10-01 16:12:15 +02002638/**
2639 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
2640 * @q : the queue of the device being checked
2641 *
2642 * Description:
2643 * Check if underlying low-level drivers of a device are busy.
2644 * If the drivers want to export their busy state, they must set own
2645 * exporting function using blk_queue_lld_busy() first.
2646 *
2647 * Basically, this function is used only by request stacking drivers
2648 * to stop dispatching requests to underlying devices when underlying
2649 * devices are busy. This behavior helps more I/O merging on the queue
2650 * of the request stacking driver and prevents I/O throughput regression
2651 * on burst I/O load.
2652 *
2653 * Return:
2654 * 0 - Not busy (The request stacking driver should dispatch request)
2655 * 1 - Busy (The request stacking driver should stop dispatching request)
2656 */
2657int blk_lld_busy(struct request_queue *q)
2658{
2659 if (q->lld_busy_fn)
2660 return q->lld_busy_fn(q);
2661
2662 return 0;
2663}
2664EXPORT_SYMBOL_GPL(blk_lld_busy);
2665
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002666/**
2667 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
2668 * @rq: the clone request to be cleaned up
2669 *
2670 * Description:
2671 * Free all bios in @rq for a cloned request.
2672 */
2673void blk_rq_unprep_clone(struct request *rq)
2674{
2675 struct bio *bio;
2676
2677 while ((bio = rq->bio) != NULL) {
2678 rq->bio = bio->bi_next;
2679
2680 bio_put(bio);
2681 }
2682}
2683EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
2684
2685/*
2686 * Copy attributes of the original request to the clone request.
2687 * The actual data parts (e.g. ->cmd, ->buffer, ->sense) are not copied.
2688 */
2689static void __blk_rq_prep_clone(struct request *dst, struct request *src)
2690{
2691 dst->cpu = src->cpu;
Tejun Heo3a2edd02010-09-03 11:56:18 +02002692 dst->cmd_flags = (src->cmd_flags & REQ_CLONE_MASK) | REQ_NOMERGE;
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002693 dst->cmd_type = src->cmd_type;
2694 dst->__sector = blk_rq_pos(src);
2695 dst->__data_len = blk_rq_bytes(src);
2696 dst->nr_phys_segments = src->nr_phys_segments;
2697 dst->ioprio = src->ioprio;
2698 dst->extra_len = src->extra_len;
2699}
2700
2701/**
2702 * blk_rq_prep_clone - Helper function to setup clone request
2703 * @rq: the request to be setup
2704 * @rq_src: original request to be cloned
2705 * @bs: bio_set that bios for clone are allocated from
2706 * @gfp_mask: memory allocation mask for bio
2707 * @bio_ctr: setup function to be called for each clone bio.
2708 * Returns %0 for success, non %0 for failure.
2709 * @data: private data to be passed to @bio_ctr
2710 *
2711 * Description:
2712 * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
2713 * The actual data parts of @rq_src (e.g. ->cmd, ->buffer, ->sense)
2714 * are not copied, and copying such parts is the caller's responsibility.
2715 * Also, pages which the original bios are pointing to are not copied
2716 * and the cloned bios just point same pages.
2717 * So cloned bios must be completed before original bios, which means
2718 * the caller must complete @rq before @rq_src.
2719 */
2720int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
2721 struct bio_set *bs, gfp_t gfp_mask,
2722 int (*bio_ctr)(struct bio *, struct bio *, void *),
2723 void *data)
2724{
2725 struct bio *bio, *bio_src;
2726
2727 if (!bs)
2728 bs = fs_bio_set;
2729
2730 blk_rq_init(NULL, rq);
2731
2732 __rq_for_each_bio(bio_src, rq_src) {
2733 bio = bio_alloc_bioset(gfp_mask, bio_src->bi_max_vecs, bs);
2734 if (!bio)
2735 goto free_and_out;
2736
2737 __bio_clone(bio, bio_src);
2738
2739 if (bio_integrity(bio_src) &&
Martin K. Petersen7878cba2009-06-26 15:37:49 +02002740 bio_integrity_clone(bio, bio_src, gfp_mask, bs))
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002741 goto free_and_out;
2742
2743 if (bio_ctr && bio_ctr(bio, bio_src, data))
2744 goto free_and_out;
2745
2746 if (rq->bio) {
2747 rq->biotail->bi_next = bio;
2748 rq->biotail = bio;
2749 } else
2750 rq->bio = rq->biotail = bio;
2751 }
2752
2753 __blk_rq_prep_clone(rq, rq_src);
2754
2755 return 0;
2756
2757free_and_out:
2758 if (bio)
2759 bio_free(bio, bs);
2760 blk_rq_unprep_clone(rq);
2761
2762 return -ENOMEM;
2763}
2764EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
2765
Jens Axboe18887ad2008-07-28 13:08:45 +02002766int kblockd_schedule_work(struct request_queue *q, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767{
2768 return queue_work(kblockd_workqueue, work);
2769}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770EXPORT_SYMBOL(kblockd_schedule_work);
2771
Vivek Goyale43473b2010-09-15 17:06:35 -04002772int kblockd_schedule_delayed_work(struct request_queue *q,
2773 struct delayed_work *dwork, unsigned long delay)
2774{
2775 return queue_delayed_work(kblockd_workqueue, dwork, delay);
2776}
2777EXPORT_SYMBOL(kblockd_schedule_delayed_work);
2778
Jens Axboe73c10102011-03-08 13:19:51 +01002779#define PLUG_MAGIC 0x91827364
2780
Suresh Jayaraman75df7132011-09-21 10:00:16 +02002781/**
2782 * blk_start_plug - initialize blk_plug and track it inside the task_struct
2783 * @plug: The &struct blk_plug that needs to be initialized
2784 *
2785 * Description:
2786 * Tracking blk_plug inside the task_struct will help with auto-flushing the
2787 * pending I/O should the task end up blocking between blk_start_plug() and
2788 * blk_finish_plug(). This is important from a performance perspective, but
2789 * also ensures that we don't deadlock. For instance, if the task is blocking
2790 * for a memory allocation, memory reclaim could end up wanting to free a
2791 * page belonging to that request that is currently residing in our private
2792 * plug. By flushing the pending I/O when the process goes to sleep, we avoid
2793 * this kind of deadlock.
2794 */
Jens Axboe73c10102011-03-08 13:19:51 +01002795void blk_start_plug(struct blk_plug *plug)
2796{
2797 struct task_struct *tsk = current;
2798
2799 plug->magic = PLUG_MAGIC;
2800 INIT_LIST_HEAD(&plug->list);
NeilBrown048c9372011-04-18 09:52:22 +02002801 INIT_LIST_HEAD(&plug->cb_list);
Jens Axboe73c10102011-03-08 13:19:51 +01002802 plug->should_sort = 0;
2803
2804 /*
2805 * If this is a nested plug, don't actually assign it. It will be
2806 * flushed on its own.
2807 */
2808 if (!tsk->plug) {
2809 /*
2810 * Store ordering should not be needed here, since a potential
2811 * preempt will imply a full memory barrier
2812 */
2813 tsk->plug = plug;
2814 }
2815}
2816EXPORT_SYMBOL(blk_start_plug);
2817
2818static int plug_rq_cmp(void *priv, struct list_head *a, struct list_head *b)
2819{
2820 struct request *rqa = container_of(a, struct request, queuelist);
2821 struct request *rqb = container_of(b, struct request, queuelist);
2822
Konstantin Khlebnikovf83e8262011-04-04 00:15:02 +02002823 return !(rqa->q <= rqb->q);
Jens Axboe73c10102011-03-08 13:19:51 +01002824}
2825
Jens Axboe49cac012011-04-16 13:51:05 +02002826/*
2827 * If 'from_schedule' is true, then postpone the dispatch of requests
2828 * until a safe kblockd context. We due this to avoid accidental big
2829 * additional stack usage in driver dispatch, in places where the originally
2830 * plugger did not intend it.
2831 */
Jens Axboef6603782011-04-15 15:49:07 +02002832static void queue_unplugged(struct request_queue *q, unsigned int depth,
Jens Axboe49cac012011-04-16 13:51:05 +02002833 bool from_schedule)
Jens Axboe99e22592011-04-18 09:59:55 +02002834 __releases(q->queue_lock)
Jens Axboe94b5eb22011-04-12 10:12:19 +02002835{
Jens Axboe49cac012011-04-16 13:51:05 +02002836 trace_block_unplug(q, depth, !from_schedule);
Jens Axboe99e22592011-04-18 09:59:55 +02002837
2838 /*
Tejun Heo8ba61432011-12-14 00:33:37 +01002839 * Don't mess with dead queue.
2840 */
2841 if (unlikely(blk_queue_dead(q))) {
2842 spin_unlock(q->queue_lock);
2843 return;
2844 }
2845
2846 /*
Jens Axboe99e22592011-04-18 09:59:55 +02002847 * If we are punting this to kblockd, then we can safely drop
2848 * the queue_lock before waking kblockd (which needs to take
2849 * this lock).
2850 */
2851 if (from_schedule) {
2852 spin_unlock(q->queue_lock);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02002853 blk_run_queue_async(q);
Jens Axboe99e22592011-04-18 09:59:55 +02002854 } else {
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02002855 __blk_run_queue(q);
Jens Axboe99e22592011-04-18 09:59:55 +02002856 spin_unlock(q->queue_lock);
2857 }
2858
Jens Axboe94b5eb22011-04-12 10:12:19 +02002859}
2860
NeilBrown048c9372011-04-18 09:52:22 +02002861static void flush_plug_callbacks(struct blk_plug *plug)
2862{
2863 LIST_HEAD(callbacks);
2864
2865 if (list_empty(&plug->cb_list))
2866 return;
2867
2868 list_splice_init(&plug->cb_list, &callbacks);
2869
2870 while (!list_empty(&callbacks)) {
2871 struct blk_plug_cb *cb = list_first_entry(&callbacks,
2872 struct blk_plug_cb,
2873 list);
2874 list_del(&cb->list);
2875 cb->callback(cb);
2876 }
2877}
2878
Jens Axboe49cac012011-04-16 13:51:05 +02002879void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
Jens Axboe73c10102011-03-08 13:19:51 +01002880{
2881 struct request_queue *q;
2882 unsigned long flags;
2883 struct request *rq;
NeilBrown109b8122011-04-11 14:13:10 +02002884 LIST_HEAD(list);
Jens Axboe94b5eb22011-04-12 10:12:19 +02002885 unsigned int depth;
Jens Axboe73c10102011-03-08 13:19:51 +01002886
2887 BUG_ON(plug->magic != PLUG_MAGIC);
2888
NeilBrown048c9372011-04-18 09:52:22 +02002889 flush_plug_callbacks(plug);
Jens Axboe73c10102011-03-08 13:19:51 +01002890 if (list_empty(&plug->list))
2891 return;
2892
NeilBrown109b8122011-04-11 14:13:10 +02002893 list_splice_init(&plug->list, &list);
2894
2895 if (plug->should_sort) {
2896 list_sort(NULL, &list, plug_rq_cmp);
2897 plug->should_sort = 0;
2898 }
Jens Axboe73c10102011-03-08 13:19:51 +01002899
2900 q = NULL;
Jens Axboe94b5eb22011-04-12 10:12:19 +02002901 depth = 0;
Jens Axboe18811272011-04-12 10:11:24 +02002902
2903 /*
2904 * Save and disable interrupts here, to avoid doing it for every
2905 * queue lock we have to take.
2906 */
Jens Axboe73c10102011-03-08 13:19:51 +01002907 local_irq_save(flags);
NeilBrown109b8122011-04-11 14:13:10 +02002908 while (!list_empty(&list)) {
2909 rq = list_entry_rq(list.next);
Jens Axboe73c10102011-03-08 13:19:51 +01002910 list_del_init(&rq->queuelist);
Jens Axboe73c10102011-03-08 13:19:51 +01002911 BUG_ON(!rq->q);
2912 if (rq->q != q) {
Jens Axboe99e22592011-04-18 09:59:55 +02002913 /*
2914 * This drops the queue lock
2915 */
2916 if (q)
Jens Axboe49cac012011-04-16 13:51:05 +02002917 queue_unplugged(q, depth, from_schedule);
Jens Axboe73c10102011-03-08 13:19:51 +01002918 q = rq->q;
Jens Axboe94b5eb22011-04-12 10:12:19 +02002919 depth = 0;
Jens Axboe73c10102011-03-08 13:19:51 +01002920 spin_lock(q->queue_lock);
2921 }
Tejun Heo8ba61432011-12-14 00:33:37 +01002922
2923 /*
2924 * Short-circuit if @q is dead
2925 */
2926 if (unlikely(blk_queue_dead(q))) {
2927 __blk_end_request_all(rq, -ENODEV);
2928 continue;
2929 }
2930
Jens Axboe73c10102011-03-08 13:19:51 +01002931 /*
2932 * rq is already accounted, so use raw insert
2933 */
Jens Axboe401a18e2011-03-25 16:57:52 +01002934 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA))
2935 __elv_add_request(q, rq, ELEVATOR_INSERT_FLUSH);
2936 else
2937 __elv_add_request(q, rq, ELEVATOR_INSERT_SORT_MERGE);
Jens Axboe94b5eb22011-04-12 10:12:19 +02002938
2939 depth++;
Jens Axboe73c10102011-03-08 13:19:51 +01002940 }
2941
Jens Axboe99e22592011-04-18 09:59:55 +02002942 /*
2943 * This drops the queue lock
2944 */
2945 if (q)
Jens Axboe49cac012011-04-16 13:51:05 +02002946 queue_unplugged(q, depth, from_schedule);
Jens Axboe73c10102011-03-08 13:19:51 +01002947
Jens Axboe73c10102011-03-08 13:19:51 +01002948 local_irq_restore(flags);
2949}
Jens Axboe73c10102011-03-08 13:19:51 +01002950
2951void blk_finish_plug(struct blk_plug *plug)
2952{
Jens Axboef6603782011-04-15 15:49:07 +02002953 blk_flush_plug_list(plug, false);
Christoph Hellwig88b996c2011-04-15 15:20:10 +02002954
2955 if (plug == current->plug)
2956 current->plug = NULL;
Jens Axboe73c10102011-03-08 13:19:51 +01002957}
2958EXPORT_SYMBOL(blk_finish_plug);
2959
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960int __init blk_dev_init(void)
2961{
Nikanth Karthikesan9eb55b02009-04-27 14:53:54 +02002962 BUILD_BUG_ON(__REQ_NR_BITS > 8 *
2963 sizeof(((struct request *)0)->cmd_flags));
2964
Tejun Heo89b90be2011-01-03 15:01:47 +01002965 /* used for unplugging and affects IO latency/throughput - HIGHPRI */
2966 kblockd_workqueue = alloc_workqueue("kblockd",
2967 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 if (!kblockd_workqueue)
2969 panic("Failed to create kblockd\n");
2970
2971 request_cachep = kmem_cache_create("blkdev_requests",
Paul Mundt20c2df82007-07-20 10:11:58 +09002972 sizeof(struct request), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
Jens Axboe8324aa92008-01-29 14:51:59 +01002974 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02002975 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 return 0;
2978}