blob: 991c1d6ef245f758260092033eac9267ebc39dbf [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 Mortonfaccbd4b2006-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"
Tejun Heo5efd6112012-03-05 13:15:12 -080037#include "blk-cgroup.h"
Jens Axboe8324aa92008-01-29 14:51:59 +010038
Mike Snitzerd07335e2010-11-16 12:52:38 +010039EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_remap);
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +020040EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_remap);
Li Zefan55782132009-06-09 13:43:05 +080041EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_complete);
Ingo Molnar0bfc2452008-11-26 11:59:56 +010042
Tejun Heoa73f7302011-12-14 00:33:37 +010043DEFINE_IDA(blk_queue_ida);
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/*
46 * For the allocated request tables
47 */
Adrian Bunk5ece6c52008-02-18 13:45:51 +010048static struct kmem_cache *request_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/*
51 * For queue allocation
52 */
Jens Axboe6728cb02008-01-31 13:03:55 +010053struct kmem_cache *blk_requestq_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 * Controlling structure to kblockd
57 */
Jens Axboeff856ba2006-01-09 16:02:34 +010058static struct workqueue_struct *kblockd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Jens Axboe26b82562008-01-29 13:54:41 +010060static void drive_stat_acct(struct request *rq, int new_io)
61{
Jens Axboe28f13702008-05-07 10:15:46 +020062 struct hd_struct *part;
Jens Axboe26b82562008-01-29 13:54:41 +010063 int rw = rq_data_dir(rq);
Tejun Heoc9959052008-08-25 19:47:21 +090064 int cpu;
Jens Axboe26b82562008-01-29 13:54:41 +010065
Jens Axboec2553b52009-04-24 08:10:11 +020066 if (!blk_do_io_stat(rq))
Jens Axboe26b82562008-01-29 13:54:41 +010067 return;
68
Tejun Heo074a7ac2008-08-25 19:56:14 +090069 cpu = part_stat_lock();
Tejun Heoc9959052008-08-25 19:47:21 +090070
Jerome Marchand09e099d2011-01-05 16:57:38 +010071 if (!new_io) {
72 part = rq->part;
Tejun Heo074a7ac2008-08-25 19:56:14 +090073 part_stat_inc(cpu, part, merges[rw]);
Jerome Marchand09e099d2011-01-05 16:57:38 +010074 } else {
75 part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq));
Jens Axboe6c23a962011-01-07 08:43:37 +010076 if (!hd_struct_try_get(part)) {
Jerome Marchand09e099d2011-01-05 16:57:38 +010077 /*
78 * The partition is already being removed,
79 * the request will be accounted on the disk only
80 *
81 * We take a reference on disk->part0 although that
82 * partition will never be deleted, so we can treat
83 * it as any other partition.
84 */
85 part = &rq->rq_disk->part0;
Jens Axboe6c23a962011-01-07 08:43:37 +010086 hd_struct_get(part);
Jerome Marchand09e099d2011-01-05 16:57:38 +010087 }
Tejun Heo074a7ac2008-08-25 19:56:14 +090088 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +020089 part_inc_in_flight(part, rw);
Jerome Marchand09e099d2011-01-05 16:57:38 +010090 rq->part = part;
Jens Axboe26b82562008-01-29 13:54:41 +010091 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +020092
Tejun Heo074a7ac2008-08-25 19:56:14 +090093 part_stat_unlock();
Jens Axboe26b82562008-01-29 13:54:41 +010094}
95
Jens Axboe8324aa92008-01-29 14:51:59 +010096void blk_queue_congestion_threshold(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
98 int nr;
99
100 nr = q->nr_requests - (q->nr_requests / 8) + 1;
101 if (nr > q->nr_requests)
102 nr = q->nr_requests;
103 q->nr_congestion_on = nr;
104
105 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
106 if (nr < 1)
107 nr = 1;
108 q->nr_congestion_off = nr;
109}
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111/**
112 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
113 * @bdev: device
114 *
115 * Locates the passed device's request queue and returns the address of its
116 * backing_dev_info
117 *
118 * Will return NULL if the request queue cannot be located.
119 */
120struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
121{
122 struct backing_dev_info *ret = NULL;
Jens Axboe165125e2007-07-24 09:28:11 +0200123 struct request_queue *q = bdev_get_queue(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 if (q)
126 ret = &q->backing_dev_info;
127 return ret;
128}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129EXPORT_SYMBOL(blk_get_backing_dev_info);
130
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200131void blk_rq_init(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200133 memset(rq, 0, sizeof(*rq));
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 INIT_LIST_HEAD(&rq->queuelist);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700136 INIT_LIST_HEAD(&rq->timeout_list);
Jens Axboec7c22e42008-09-13 20:26:01 +0200137 rq->cpu = -1;
Jens Axboe63a71382008-02-08 12:41:03 +0100138 rq->q = q;
Tejun Heoa2dec7b2009-05-07 22:24:44 +0900139 rq->__sector = (sector_t) -1;
Jens Axboe2e662b62006-07-13 11:55:04 +0200140 INIT_HLIST_NODE(&rq->hash);
141 RB_CLEAR_NODE(&rq->rb_node);
FUJITA Tomonorid7e3c322008-04-29 09:54:39 +0200142 rq->cmd = rq->__cmd;
Li Zefane2494e12009-04-02 13:43:26 +0800143 rq->cmd_len = BLK_MAX_CDB;
Jens Axboe63a71382008-02-08 12:41:03 +0100144 rq->tag = -1;
Jens Axboe63a71382008-02-08 12:41:03 +0100145 rq->ref_count = 1;
Tejun Heob243ddc2009-04-23 11:05:18 +0900146 rq->start_time = jiffies;
Divyesh Shah91952912010-04-01 15:01:41 -0700147 set_start_time_ns(rq);
Jerome Marchand09e099d2011-01-05 16:57:38 +0100148 rq->part = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200150EXPORT_SYMBOL(blk_rq_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
NeilBrown5bb23a62007-09-27 12:46:13 +0200152static void req_bio_endio(struct request *rq, struct bio *bio,
153 unsigned int nbytes, int error)
Tejun Heo797e7db2006-01-06 09:51:03 +0100154{
Tejun Heo143a87f2011-01-25 12:43:52 +0100155 if (error)
156 clear_bit(BIO_UPTODATE, &bio->bi_flags);
157 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
158 error = -EIO;
Tejun Heo797e7db2006-01-06 09:51:03 +0100159
Tejun Heo143a87f2011-01-25 12:43:52 +0100160 if (unlikely(nbytes > bio->bi_size)) {
161 printk(KERN_ERR "%s: want %u bytes done, %u left\n",
162 __func__, nbytes, bio->bi_size);
163 nbytes = bio->bi_size;
NeilBrown5bb23a62007-09-27 12:46:13 +0200164 }
Tejun Heo143a87f2011-01-25 12:43:52 +0100165
166 if (unlikely(rq->cmd_flags & REQ_QUIET))
167 set_bit(BIO_QUIET, &bio->bi_flags);
168
169 bio->bi_size -= nbytes;
170 bio->bi_sector += (nbytes >> 9);
171
172 if (bio_integrity(bio))
173 bio_integrity_advance(bio, nbytes);
174
175 /* don't actually finish bio if it's part of flush sequence */
176 if (bio->bi_size == 0 && !(rq->cmd_flags & REQ_FLUSH_SEQ))
177 bio_endio(bio, error);
Tejun Heo797e7db2006-01-06 09:51:03 +0100178}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180void blk_dump_rq_flags(struct request *rq, char *msg)
181{
182 int bit;
183
Jens Axboe6728cb02008-01-31 13:03:55 +0100184 printk(KERN_INFO "%s: dev %s: type=%x, flags=%x\n", msg,
Jens Axboe4aff5e22006-08-10 08:44:47 +0200185 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
186 rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Tejun Heo83096eb2009-05-07 22:24:39 +0900188 printk(KERN_INFO " sector %llu, nr/cnr %u/%u\n",
189 (unsigned long long)blk_rq_pos(rq),
190 blk_rq_sectors(rq), blk_rq_cur_sectors(rq));
Tejun Heo731ec492009-04-23 11:05:20 +0900191 printk(KERN_INFO " bio %p, biotail %p, buffer %p, len %u\n",
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900192 rq->bio, rq->biotail, rq->buffer, blk_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200194 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
Jens Axboe6728cb02008-01-31 13:03:55 +0100195 printk(KERN_INFO " cdb: ");
FUJITA Tomonorid34c87e2008-04-29 14:37:52 +0200196 for (bit = 0; bit < BLK_MAX_CDB; bit++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 printk("%02x ", rq->cmd[bit]);
198 printk("\n");
199 }
200}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201EXPORT_SYMBOL(blk_dump_rq_flags);
202
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500203static void blk_delay_work(struct work_struct *work)
Jens Axboe6c5e0c42008-08-01 20:31:32 +0200204{
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500205 struct request_queue *q;
Jens Axboe6c5e0c42008-08-01 20:31:32 +0200206
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500207 q = container_of(work, struct request_queue, delay_work.work);
208 spin_lock_irq(q->queue_lock);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200209 __blk_run_queue(q);
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500210 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213/**
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500214 * blk_delay_queue - restart queueing after defined interval
215 * @q: The &struct request_queue in question
216 * @msecs: Delay in msecs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 *
218 * Description:
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500219 * Sometimes queueing needs to be postponed for a little while, to allow
220 * resources to come back. This function will make sure that queueing is
221 * restarted around the specified time.
222 */
223void blk_delay_queue(struct request_queue *q, unsigned long msecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Jens Axboe4521cc42011-04-18 11:36:39 +0200225 queue_delayed_work(kblockd_workqueue, &q->delay_work,
226 msecs_to_jiffies(msecs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500228EXPORT_SYMBOL(blk_delay_queue);
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230/**
231 * blk_start_queue - restart a previously stopped queue
Jens Axboe165125e2007-07-24 09:28:11 +0200232 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 *
234 * Description:
235 * blk_start_queue() will clear the stop flag on the queue, and call
236 * the request_fn for the queue if it was in a stopped state when
237 * entered. Also see blk_stop_queue(). Queue lock must be held.
238 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200239void blk_start_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200241 WARN_ON(!irqs_disabled());
242
Nick Piggin75ad23b2008-04-29 14:48:33 +0200243 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200244 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246EXPORT_SYMBOL(blk_start_queue);
247
248/**
249 * blk_stop_queue - stop a queue
Jens Axboe165125e2007-07-24 09:28:11 +0200250 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 *
252 * Description:
253 * The Linux block layer assumes that a block driver will consume all
254 * entries on the request queue when the request_fn strategy is called.
255 * Often this will not happen, because of hardware limitations (queue
256 * depth settings). If a device driver gets a 'queue full' response,
257 * or if it simply chooses not to queue more I/O at one point, it can
258 * call this function to prevent the request_fn from being called until
259 * the driver has signalled it's ready to go again. This happens by calling
260 * blk_start_queue() to restart queue operations. Queue lock must be held.
261 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200262void blk_stop_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Jens Axboead3d9d72011-03-25 16:58:59 +0100264 __cancel_delayed_work(&q->delay_work);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200265 queue_flag_set(QUEUE_FLAG_STOPPED, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267EXPORT_SYMBOL(blk_stop_queue);
268
269/**
270 * blk_sync_queue - cancel any pending callbacks on a queue
271 * @q: the queue
272 *
273 * Description:
274 * The block layer may perform asynchronous callback activity
275 * on a queue, such as calling the unplug function after a timeout.
276 * A block device may call blk_sync_queue to ensure that any
277 * such activity is cancelled, thus allowing it to release resources
Michael Opdenacker59c51592007-05-09 08:57:56 +0200278 * that the callbacks might use. The caller must already have made sure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 * that its ->make_request_fn will not re-add plugging prior to calling
280 * this function.
281 *
Vivek Goyalda527772011-03-02 19:05:33 -0500282 * This function does not cancel any asynchronous activity arising
283 * out of elevator or throttling code. That would require elevaotor_exit()
Tejun Heo5efd6112012-03-05 13:15:12 -0800284 * and blkcg_exit_queue() to be called with queue lock initialized.
Vivek Goyalda527772011-03-02 19:05:33 -0500285 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 */
287void blk_sync_queue(struct request_queue *q)
288{
Jens Axboe70ed28b2008-11-19 14:38:39 +0100289 del_timer_sync(&q->timeout);
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500290 cancel_delayed_work_sync(&q->delay_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292EXPORT_SYMBOL(blk_sync_queue);
293
294/**
Jens Axboe80a4b582008-10-14 09:51:06 +0200295 * __blk_run_queue - run a single device queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200297 *
298 * Description:
299 * See @blk_run_queue. This variant must be called with the queue lock
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200300 * held and interrupts disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 */
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200302void __blk_run_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Tejun Heoa538cd02009-04-23 11:05:17 +0900304 if (unlikely(blk_queue_stopped(q)))
305 return;
306
Jens Axboec21e6be2011-04-19 13:32:46 +0200307 q->request_fn(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200308}
309EXPORT_SYMBOL(__blk_run_queue);
Jens Axboedac07ec2006-05-11 08:20:16 +0200310
Nick Piggin75ad23b2008-04-29 14:48:33 +0200311/**
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200312 * blk_run_queue_async - run a single device queue in workqueue context
313 * @q: The queue to run
314 *
315 * Description:
316 * Tells kblockd to perform the equivalent of @blk_run_queue on behalf
317 * of us.
318 */
319void blk_run_queue_async(struct request_queue *q)
320{
Shaohua Li3ec717b2011-05-18 11:22:43 +0200321 if (likely(!blk_queue_stopped(q))) {
322 __cancel_delayed_work(&q->delay_work);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200323 queue_delayed_work(kblockd_workqueue, &q->delay_work, 0);
Shaohua Li3ec717b2011-05-18 11:22:43 +0200324 }
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200325}
Jens Axboec21e6be2011-04-19 13:32:46 +0200326EXPORT_SYMBOL(blk_run_queue_async);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200327
328/**
Nick Piggin75ad23b2008-04-29 14:48:33 +0200329 * blk_run_queue - run a single device queue
330 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200331 *
332 * Description:
333 * Invoke request handling on this queue, if it has pending work to do.
Tejun Heoa7f55792009-04-23 11:05:17 +0900334 * May be used to restart queueing when a request has completed.
Nick Piggin75ad23b2008-04-29 14:48:33 +0200335 */
336void blk_run_queue(struct request_queue *q)
337{
338 unsigned long flags;
339
340 spin_lock_irqsave(q->queue_lock, flags);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200341 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 spin_unlock_irqrestore(q->queue_lock, flags);
343}
344EXPORT_SYMBOL(blk_run_queue);
345
Jens Axboe165125e2007-07-24 09:28:11 +0200346void blk_put_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500347{
348 kobject_put(&q->kobj);
349}
Jens Axboed86e0e82011-05-27 07:44:43 +0200350EXPORT_SYMBOL(blk_put_queue);
Al Viro483f4af2006-03-18 18:34:37 -0500351
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200352/**
353 * blk_drain_queue - drain requests from request_queue
354 * @q: queue to drain
Tejun Heoc9a929d2011-10-19 14:42:16 +0200355 * @drain_all: whether to drain all requests or only the ones w/ ELVPRIV
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200356 *
Tejun Heoc9a929d2011-10-19 14:42:16 +0200357 * Drain requests from @q. If @drain_all is set, all requests are drained.
358 * If not, only ELVPRIV requests are drained. The caller is responsible
359 * for ensuring that no new requests which need to be drained are queued.
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200360 */
Tejun Heoc9a929d2011-10-19 14:42:16 +0200361void blk_drain_queue(struct request_queue *q, bool drain_all)
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200362{
363 while (true) {
Tejun Heo481a7d62011-12-14 00:33:37 +0100364 bool drain = false;
365 int i;
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200366
367 spin_lock_irq(q->queue_lock);
368
Tejun Heob855b042012-03-06 21:24:55 +0100369 /*
370 * The caller might be trying to drain @q before its
371 * elevator is initialized.
372 */
373 if (q->elevator)
374 elv_drain_elevator(q);
375
Tejun Heo5efd6112012-03-05 13:15:12 -0800376 blkcg_drain_queue(q);
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200377
Tejun Heo4eabc942011-12-15 20:03:04 +0100378 /*
379 * This function might be called on a queue which failed
Tejun Heob855b042012-03-06 21:24:55 +0100380 * driver init after queue creation or is not yet fully
381 * active yet. Some drivers (e.g. fd and loop) get unhappy
382 * in such cases. Kick queue iff dispatch queue has
383 * something on it and @q has request_fn set.
Tejun Heo4eabc942011-12-15 20:03:04 +0100384 */
Tejun Heob855b042012-03-06 21:24:55 +0100385 if (!list_empty(&q->queue_head) && q->request_fn)
Tejun Heo4eabc942011-12-15 20:03:04 +0100386 __blk_run_queue(q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200387
Tejun Heo481a7d62011-12-14 00:33:37 +0100388 drain |= q->rq.elvpriv;
389
390 /*
391 * Unfortunately, requests are queued at and tracked from
392 * multiple places and there's no single counter which can
393 * be drained. Check all the queues and counters.
394 */
395 if (drain_all) {
396 drain |= !list_empty(&q->queue_head);
397 for (i = 0; i < 2; i++) {
398 drain |= q->rq.count[i];
399 drain |= q->in_flight[i];
400 drain |= !list_empty(&q->flush_queue[i]);
401 }
402 }
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200403
404 spin_unlock_irq(q->queue_lock);
405
Tejun Heo481a7d62011-12-14 00:33:37 +0100406 if (!drain)
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200407 break;
408 msleep(10);
409 }
410}
411
Tejun Heoc9a929d2011-10-19 14:42:16 +0200412/**
Tejun Heod7325802012-03-05 13:14:58 -0800413 * blk_queue_bypass_start - enter queue bypass mode
414 * @q: queue of interest
415 *
416 * In bypass mode, only the dispatch FIFO queue of @q is used. This
417 * function makes @q enter bypass mode and drains all requests which were
Tejun Heo6ecf23a2012-03-05 13:14:59 -0800418 * throttled or issued before. On return, it's guaranteed that no request
419 * is being throttled or has ELVPRIV set.
Tejun Heod7325802012-03-05 13:14:58 -0800420 */
421void blk_queue_bypass_start(struct request_queue *q)
422{
423 spin_lock_irq(q->queue_lock);
424 q->bypass_depth++;
425 queue_flag_set(QUEUE_FLAG_BYPASS, q);
426 spin_unlock_irq(q->queue_lock);
427
428 blk_drain_queue(q, false);
429}
430EXPORT_SYMBOL_GPL(blk_queue_bypass_start);
431
432/**
433 * blk_queue_bypass_end - leave queue bypass mode
434 * @q: queue of interest
435 *
436 * Leave bypass mode and restore the normal queueing behavior.
437 */
438void blk_queue_bypass_end(struct request_queue *q)
439{
440 spin_lock_irq(q->queue_lock);
441 if (!--q->bypass_depth)
442 queue_flag_clear(QUEUE_FLAG_BYPASS, q);
443 WARN_ON_ONCE(q->bypass_depth < 0);
444 spin_unlock_irq(q->queue_lock);
445}
446EXPORT_SYMBOL_GPL(blk_queue_bypass_end);
447
448/**
Tejun Heoc9a929d2011-10-19 14:42:16 +0200449 * blk_cleanup_queue - shutdown a request queue
450 * @q: request queue to shutdown
451 *
452 * Mark @q DEAD, drain all pending requests, destroy and put it. All
453 * future requests will be failed immediately with -ENODEV.
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500454 */
Jens Axboe6728cb02008-01-31 13:03:55 +0100455void blk_cleanup_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500456{
Tejun Heoc9a929d2011-10-19 14:42:16 +0200457 spinlock_t *lock = q->queue_lock;
Jens Axboee3335de92008-09-18 09:22:54 -0700458
Tejun Heoc9a929d2011-10-19 14:42:16 +0200459 /* mark @q DEAD, no new request or merges will be allowed afterwards */
Al Viro483f4af2006-03-18 18:34:37 -0500460 mutex_lock(&q->sysfs_lock);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200461 queue_flag_set_unlocked(QUEUE_FLAG_DEAD, q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200462
463 spin_lock_irq(lock);
Tejun Heo6ecf23a2012-03-05 13:14:59 -0800464
465 /* dead queue is permanently in bypass mode till released */
466 q->bypass_depth++;
467 queue_flag_set(QUEUE_FLAG_BYPASS, q);
468
Tejun Heoc9a929d2011-10-19 14:42:16 +0200469 queue_flag_set(QUEUE_FLAG_NOMERGES, q);
470 queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
471 queue_flag_set(QUEUE_FLAG_DEAD, q);
Al Viro483f4af2006-03-18 18:34:37 -0500472
Hannes Reinecke777eb1b2011-09-28 08:07:01 -0600473 if (q->queue_lock != &q->__queue_lock)
474 q->queue_lock = &q->__queue_lock;
Vivek Goyalda527772011-03-02 19:05:33 -0500475
Tejun Heoc9a929d2011-10-19 14:42:16 +0200476 spin_unlock_irq(lock);
477 mutex_unlock(&q->sysfs_lock);
478
Tejun Heob855b042012-03-06 21:24:55 +0100479 /* drain all requests queued before DEAD marking */
480 blk_drain_queue(q, true);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200481
482 /* @q won't process any more request, flush async actions */
483 del_timer_sync(&q->backing_dev_info.laptop_mode_wb_timer);
484 blk_sync_queue(q);
485
486 /* @q is and will stay empty, shutdown and put */
Al Viro483f4af2006-03-18 18:34:37 -0500487 blk_put_queue(q);
488}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489EXPORT_SYMBOL(blk_cleanup_queue);
490
Jens Axboe165125e2007-07-24 09:28:11 +0200491static int blk_init_free_list(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 struct request_list *rl = &q->rq;
494
Mike Snitzer1abec4f2010-05-25 13:15:15 -0400495 if (unlikely(rl->rq_pool))
496 return 0;
497
Jens Axboe1faa16d2009-04-06 14:48:01 +0200498 rl->count[BLK_RW_SYNC] = rl->count[BLK_RW_ASYNC] = 0;
499 rl->starved[BLK_RW_SYNC] = rl->starved[BLK_RW_ASYNC] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200500 rl->elvpriv = 0;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200501 init_waitqueue_head(&rl->wait[BLK_RW_SYNC]);
502 init_waitqueue_head(&rl->wait[BLK_RW_ASYNC]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Christoph Lameter19460892005-06-23 00:08:19 -0700504 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
505 mempool_free_slab, request_cachep, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 if (!rl->rq_pool)
508 return -ENOMEM;
509
510 return 0;
511}
512
Jens Axboe165125e2007-07-24 09:28:11 +0200513struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Christoph Lameter19460892005-06-23 00:08:19 -0700515 return blk_alloc_queue_node(gfp_mask, -1);
516}
517EXPORT_SYMBOL(blk_alloc_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Jens Axboe165125e2007-07-24 09:28:11 +0200519struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -0700520{
Jens Axboe165125e2007-07-24 09:28:11 +0200521 struct request_queue *q;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700522 int err;
Christoph Lameter19460892005-06-23 00:08:19 -0700523
Jens Axboe8324aa92008-01-29 14:51:59 +0100524 q = kmem_cache_alloc_node(blk_requestq_cachep,
Christoph Lameter94f60302007-07-17 04:03:29 -0700525 gfp_mask | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (!q)
527 return NULL;
528
Tejun Heoa73f7302011-12-14 00:33:37 +0100529 q->id = ida_simple_get(&blk_queue_ida, 0, 0, GFP_KERNEL);
530 if (q->id < 0)
531 goto fail_q;
532
Jens Axboe0989a022009-06-12 14:42:56 +0200533 q->backing_dev_info.ra_pages =
534 (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
535 q->backing_dev_info.state = 0;
536 q->backing_dev_info.capabilities = BDI_CAP_MAP_COPY;
Jens Axboed9938312009-06-12 14:45:52 +0200537 q->backing_dev_info.name = "block";
Mike Snitzer51514122011-11-23 10:59:13 +0100538 q->node = node_id;
Jens Axboe0989a022009-06-12 14:42:56 +0200539
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700540 err = bdi_init(&q->backing_dev_info);
Tejun Heoa73f7302011-12-14 00:33:37 +0100541 if (err)
542 goto fail_id;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700543
Matthew Garrett31373d02010-04-06 14:25:14 +0200544 setup_timer(&q->backing_dev_info.laptop_mode_wb_timer,
545 laptop_mode_timer_fn, (unsigned long) q);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700546 setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
Tejun Heob855b042012-03-06 21:24:55 +0100547 INIT_LIST_HEAD(&q->queue_head);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700548 INIT_LIST_HEAD(&q->timeout_list);
Tejun Heoa612fdd2011-12-14 00:33:41 +0100549 INIT_LIST_HEAD(&q->icq_list);
Tejun Heo4eef3042012-03-05 13:15:18 -0800550#ifdef CONFIG_BLK_CGROUP
Tejun Heoe8989fa2012-03-05 13:15:20 -0800551 INIT_LIST_HEAD(&q->blkg_list);
Tejun Heo4eef3042012-03-05 13:15:18 -0800552#endif
Tejun Heoae1b1532011-01-25 12:43:54 +0100553 INIT_LIST_HEAD(&q->flush_queue[0]);
554 INIT_LIST_HEAD(&q->flush_queue[1]);
555 INIT_LIST_HEAD(&q->flush_data_in_flight);
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500556 INIT_DELAYED_WORK(&q->delay_work, blk_delay_work);
Al Viro483f4af2006-03-18 18:34:37 -0500557
Jens Axboe8324aa92008-01-29 14:51:59 +0100558 kobject_init(&q->kobj, &blk_queue_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Al Viro483f4af2006-03-18 18:34:37 -0500560 mutex_init(&q->sysfs_lock);
Neil Browne7e72bf2008-05-14 16:05:54 -0700561 spin_lock_init(&q->__queue_lock);
Al Viro483f4af2006-03-18 18:34:37 -0500562
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500563 /*
564 * By default initialize queue_lock to internal lock and driver can
565 * override it later if need be.
566 */
567 q->queue_lock = &q->__queue_lock;
568
Tejun Heo5efd6112012-03-05 13:15:12 -0800569 if (blkcg_init_queue(q))
Tejun Heof51b8022012-03-05 13:15:05 -0800570 goto fail_id;
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 return q;
Tejun Heoa73f7302011-12-14 00:33:37 +0100573
574fail_id:
575 ida_simple_remove(&blk_queue_ida, q->id);
576fail_q:
577 kmem_cache_free(blk_requestq_cachep, q);
578 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
Christoph Lameter19460892005-06-23 00:08:19 -0700580EXPORT_SYMBOL(blk_alloc_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582/**
583 * blk_init_queue - prepare a request queue for use with a block device
584 * @rfn: The function to be called to process requests that have been
585 * placed on the queue.
586 * @lock: Request queue spin lock
587 *
588 * Description:
589 * If a block device wishes to use the standard request handling procedures,
590 * which sorts requests and coalesces adjacent requests, then it must
591 * call blk_init_queue(). The function @rfn will be called when there
592 * are requests on the queue that need to be processed. If the device
593 * supports plugging, then @rfn may not be called immediately when requests
594 * are available on the queue, but may be called at some time later instead.
595 * Plugged queues are generally unplugged when a buffer belonging to one
596 * of the requests on the queue is needed, or due to memory pressure.
597 *
598 * @rfn is not required, or even expected, to remove all requests off the
599 * queue, but only as many as it can handle at a time. If it does leave
600 * requests on the queue, it is responsible for arranging that the requests
601 * get dealt with eventually.
602 *
603 * The queue spin lock must be held while manipulating the requests on the
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200604 * request queue; this lock will be taken also from interrupt context, so irq
605 * disabling is needed for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 *
Randy Dunlap710027a2008-08-19 20:13:11 +0200607 * Function returns a pointer to the initialized request queue, or %NULL if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 * it didn't succeed.
609 *
610 * Note:
611 * blk_init_queue() must be paired with a blk_cleanup_queue() call
612 * when the block device is deactivated (such as at module unload).
613 **/
Christoph Lameter19460892005-06-23 00:08:19 -0700614
Jens Axboe165125e2007-07-24 09:28:11 +0200615struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Christoph Lameter19460892005-06-23 00:08:19 -0700617 return blk_init_queue_node(rfn, lock, -1);
618}
619EXPORT_SYMBOL(blk_init_queue);
620
Jens Axboe165125e2007-07-24 09:28:11 +0200621struct request_queue *
Christoph Lameter19460892005-06-23 00:08:19 -0700622blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
623{
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600624 struct request_queue *uninit_q, *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600626 uninit_q = blk_alloc_queue_node(GFP_KERNEL, node_id);
627 if (!uninit_q)
628 return NULL;
629
Mike Snitzer51514122011-11-23 10:59:13 +0100630 q = blk_init_allocated_queue(uninit_q, rfn, lock);
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600631 if (!q)
632 blk_cleanup_queue(uninit_q);
633
634 return q;
Mike Snitzer01effb02010-05-11 08:57:42 +0200635}
636EXPORT_SYMBOL(blk_init_queue_node);
637
638struct request_queue *
639blk_init_allocated_queue(struct request_queue *q, request_fn_proc *rfn,
640 spinlock_t *lock)
641{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if (!q)
643 return NULL;
644
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600645 if (blk_init_free_list(q))
Al Viro8669aaf2006-03-18 13:50:00 -0500646 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 q->request_fn = rfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 q->prep_rq_fn = NULL;
James Bottomley28018c22010-07-01 19:49:17 +0900650 q->unprep_rq_fn = NULL;
Jens Axboebc58ba92009-01-23 10:54:44 +0100651 q->queue_flags = QUEUE_FLAG_DEFAULT;
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500652
653 /* Override internal queue lock with supplied lock pointer */
654 if (lock)
655 q->queue_lock = lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Jens Axboef3b144a2009-03-06 08:48:33 +0100657 /*
658 * This also sets hw/phys segments, boundary and size
659 */
Jens Axboec20e8de2011-09-12 12:03:37 +0200660 blk_queue_make_request(q, blk_queue_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Alan Stern44ec9542007-02-20 11:01:57 -0500662 q->sg_reserved_size = INT_MAX;
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 /*
665 * all done
666 */
667 if (!elevator_init(q, NULL)) {
668 blk_queue_congestion_threshold(q);
669 return q;
670 }
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return NULL;
673}
Mike Snitzer51514122011-11-23 10:59:13 +0100674EXPORT_SYMBOL(blk_init_allocated_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Tejun Heo09ac46c2011-12-14 00:33:38 +0100676bool blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
Tejun Heo34f60552011-12-14 00:33:37 +0100678 if (likely(!blk_queue_dead(q))) {
Tejun Heo09ac46c2011-12-14 00:33:38 +0100679 __blk_get_queue(q);
680 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682
Tejun Heo09ac46c2011-12-14 00:33:38 +0100683 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
Jens Axboed86e0e82011-05-27 07:44:43 +0200685EXPORT_SYMBOL(blk_get_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Jens Axboe165125e2007-07-24 09:28:11 +0200687static inline void blk_free_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Tejun Heof1f8cc92011-12-14 00:33:42 +0100689 if (rq->cmd_flags & REQ_ELVPRIV) {
Tejun Heocb98fc82005-10-28 08:29:39 +0200690 elv_put_request(q, rq);
Tejun Heof1f8cc92011-12-14 00:33:42 +0100691 if (rq->elv.icq)
Tejun Heo11a31222012-02-07 07:51:30 +0100692 put_io_context(rq->elv.icq->ioc);
Tejun Heof1f8cc92011-12-14 00:33:42 +0100693 }
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 mempool_free(rq, q->rq.rq_pool);
696}
697
Jens Axboe1ea25ecb2006-07-18 22:24:11 +0200698static struct request *
Tejun Heo852c7882012-03-05 13:15:27 -0800699blk_alloc_request(struct request_queue *q, struct bio *bio, struct io_cq *icq,
Tejun Heof1f8cc92011-12-14 00:33:42 +0100700 unsigned int flags, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
702 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
703
704 if (!rq)
705 return NULL;
706
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200707 blk_rq_init(q, rq);
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200708
Jerome Marchand42dad762009-04-22 14:01:49 +0200709 rq->cmd_flags = flags | REQ_ALLOCED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Tejun Heof1f8cc92011-12-14 00:33:42 +0100711 if (flags & REQ_ELVPRIV) {
712 rq->elv.icq = icq;
Tejun Heo852c7882012-03-05 13:15:27 -0800713 if (unlikely(elv_set_request(q, rq, bio, gfp_mask))) {
Tejun Heof1f8cc92011-12-14 00:33:42 +0100714 mempool_free(rq, q->rq.rq_pool);
715 return NULL;
716 }
717 /* @rq->elv.icq holds on to io_context until @rq is freed */
718 if (icq)
719 get_io_context(icq->ioc);
Tejun Heocb98fc82005-10-28 08:29:39 +0200720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Tejun Heocb98fc82005-10-28 08:29:39 +0200722 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
724
725/*
726 * ioc_batching returns true if the ioc is a valid batching request and
727 * should be given priority access to a request.
728 */
Jens Axboe165125e2007-07-24 09:28:11 +0200729static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
731 if (!ioc)
732 return 0;
733
734 /*
735 * Make sure the process is able to allocate at least 1 request
736 * even if the batch times out, otherwise we could theoretically
737 * lose wakeups.
738 */
739 return ioc->nr_batch_requests == q->nr_batching ||
740 (ioc->nr_batch_requests > 0
741 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
742}
743
744/*
745 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
746 * will cause the process to be a "batcher" on all queues in the system. This
747 * is the behaviour we want though - once it gets a wakeup it should be given
748 * a nice run.
749 */
Jens Axboe165125e2007-07-24 09:28:11 +0200750static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
752 if (!ioc || ioc_batching(q, ioc))
753 return;
754
755 ioc->nr_batch_requests = q->nr_batching;
756 ioc->last_waited = jiffies;
757}
758
Jens Axboe1faa16d2009-04-06 14:48:01 +0200759static void __freed_request(struct request_queue *q, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
761 struct request_list *rl = &q->rq;
762
Jens Axboe1faa16d2009-04-06 14:48:01 +0200763 if (rl->count[sync] < queue_congestion_off_threshold(q))
764 blk_clear_queue_congested(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Jens Axboe1faa16d2009-04-06 14:48:01 +0200766 if (rl->count[sync] + 1 <= q->nr_requests) {
767 if (waitqueue_active(&rl->wait[sync]))
768 wake_up(&rl->wait[sync]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Jens Axboe1faa16d2009-04-06 14:48:01 +0200770 blk_clear_queue_full(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
772}
773
774/*
775 * A request has just been released. Account for it, update the full and
776 * congestion status, wake up any waiters. Called under q->queue_lock.
777 */
Tejun Heo75eb6c32011-10-19 14:31:22 +0200778static void freed_request(struct request_queue *q, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 struct request_list *rl = &q->rq;
Tejun Heo75eb6c32011-10-19 14:31:22 +0200781 int sync = rw_is_sync(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Jens Axboe1faa16d2009-04-06 14:48:01 +0200783 rl->count[sync]--;
Tejun Heo75eb6c32011-10-19 14:31:22 +0200784 if (flags & REQ_ELVPRIV)
Tejun Heocb98fc82005-10-28 08:29:39 +0200785 rl->elvpriv--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Jens Axboe1faa16d2009-04-06 14:48:01 +0200787 __freed_request(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Jens Axboe1faa16d2009-04-06 14:48:01 +0200789 if (unlikely(rl->starved[sync ^ 1]))
790 __freed_request(q, sync ^ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793/*
Mike Snitzer9d5a4e92011-02-11 11:05:46 +0100794 * Determine if elevator data should be initialized when allocating the
795 * request associated with @bio.
796 */
797static bool blk_rq_should_init_elevator(struct bio *bio)
798{
799 if (!bio)
800 return true;
801
802 /*
803 * Flush requests do not use the elevator so skip initialization.
804 * This allows a request to share the flush and elevator data.
805 */
806 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA))
807 return false;
808
809 return true;
810}
811
Tejun Heoda8303c2011-10-19 14:33:05 +0200812/**
Tejun Heo852c7882012-03-05 13:15:27 -0800813 * rq_ioc - determine io_context for request allocation
814 * @bio: request being allocated is for this bio (can be %NULL)
815 *
816 * Determine io_context to use for request allocation for @bio. May return
817 * %NULL if %current->io_context doesn't exist.
818 */
819static struct io_context *rq_ioc(struct bio *bio)
820{
821#ifdef CONFIG_BLK_CGROUP
822 if (bio && bio->bi_ioc)
823 return bio->bi_ioc;
824#endif
825 return current->io_context;
826}
827
828/**
Tejun Heoda8303c2011-10-19 14:33:05 +0200829 * get_request - get a free request
830 * @q: request_queue to allocate request from
831 * @rw_flags: RW and SYNC flags
832 * @bio: bio to allocate request for (can be %NULL)
833 * @gfp_mask: allocation mask
834 *
835 * Get a free request from @q. This function may fail under memory
836 * pressure or if @q is dead.
837 *
838 * Must be callled with @q->queue_lock held and,
839 * Returns %NULL on failure, with @q->queue_lock held.
840 * Returns !%NULL on success, with @q->queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 */
Jens Axboe165125e2007-07-24 09:28:11 +0200842static struct request *get_request(struct request_queue *q, int rw_flags,
Jens Axboe7749a8d2006-12-13 13:02:26 +0100843 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Tejun Heob6792812012-03-05 13:15:23 -0800845 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 struct request_list *rl = &q->rq;
Tejun Heof1f8cc92011-12-14 00:33:42 +0100847 struct elevator_type *et;
Tejun Heof2dbd762011-12-14 00:33:40 +0100848 struct io_context *ioc;
Tejun Heof1f8cc92011-12-14 00:33:42 +0100849 struct io_cq *icq = NULL;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200850 const bool is_sync = rw_is_sync(rw_flags) != 0;
Tejun Heof2dbd762011-12-14 00:33:40 +0100851 bool retried = false;
Tejun Heo75eb6c32011-10-19 14:31:22 +0200852 int may_queue;
Tejun Heof2dbd762011-12-14 00:33:40 +0100853retry:
Tejun Heof1f8cc92011-12-14 00:33:42 +0100854 et = q->elevator->type;
Tejun Heo852c7882012-03-05 13:15:27 -0800855 ioc = rq_ioc(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Tejun Heo34f60552011-12-14 00:33:37 +0100857 if (unlikely(blk_queue_dead(q)))
Tejun Heoda8303c2011-10-19 14:33:05 +0200858 return NULL;
859
Jens Axboe7749a8d2006-12-13 13:02:26 +0100860 may_queue = elv_may_queue(q, rw_flags);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100861 if (may_queue == ELV_MQUEUE_NO)
862 goto rq_starved;
863
Jens Axboe1faa16d2009-04-06 14:48:01 +0200864 if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) {
865 if (rl->count[is_sync]+1 >= q->nr_requests) {
Tejun Heof2dbd762011-12-14 00:33:40 +0100866 /*
867 * We want ioc to record batching state. If it's
868 * not already there, creating a new one requires
869 * dropping queue_lock, which in turn requires
870 * retesting conditions to avoid queue hang.
871 */
872 if (!ioc && !retried) {
873 spin_unlock_irq(q->queue_lock);
Tejun Heo24acfc32012-03-05 13:15:24 -0800874 create_io_context(gfp_mask, q->node);
Tejun Heof2dbd762011-12-14 00:33:40 +0100875 spin_lock_irq(q->queue_lock);
876 retried = true;
877 goto retry;
878 }
879
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100880 /*
881 * The queue will fill after this allocation, so set
882 * it as full, and mark this process as "batching".
883 * This process will be allowed to complete a batch of
884 * requests, others will be blocked.
885 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200886 if (!blk_queue_full(q, is_sync)) {
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100887 ioc_set_batching(q, ioc);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200888 blk_set_queue_full(q, is_sync);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100889 } else {
890 if (may_queue != ELV_MQUEUE_MUST
891 && !ioc_batching(q, ioc)) {
892 /*
893 * The queue is full and the allocating
894 * process is not a "batcher", and not
895 * exempted by the IO scheduler
896 */
Tejun Heob6792812012-03-05 13:15:23 -0800897 return NULL;
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100898 }
899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 }
Jens Axboe1faa16d2009-04-06 14:48:01 +0200901 blk_set_queue_congested(q, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903
Jens Axboe082cf692005-06-28 16:35:11 +0200904 /*
905 * Only allow batching queuers to allocate up to 50% over the defined
906 * limit of requests, otherwise we could have thousands of requests
907 * allocated with any setting of ->nr_requests
908 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200909 if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
Tejun Heob6792812012-03-05 13:15:23 -0800910 return NULL;
Hugh Dickinsfd782a42005-06-29 15:15:40 +0100911
Jens Axboe1faa16d2009-04-06 14:48:01 +0200912 rl->count[is_sync]++;
913 rl->starved[is_sync] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200914
Tejun Heof1f8cc92011-12-14 00:33:42 +0100915 /*
916 * Decide whether the new request will be managed by elevator. If
917 * so, mark @rw_flags and increment elvpriv. Non-zero elvpriv will
918 * prevent the current elevator from being destroyed until the new
919 * request is freed. This guarantees icq's won't be destroyed and
920 * makes creating new ones safe.
921 *
922 * Also, lookup icq while holding queue_lock. If it doesn't exist,
923 * it will be created after releasing queue_lock.
924 */
Tejun Heod7325802012-03-05 13:14:58 -0800925 if (blk_rq_should_init_elevator(bio) && !blk_queue_bypass(q)) {
Tejun Heo75eb6c32011-10-19 14:31:22 +0200926 rw_flags |= REQ_ELVPRIV;
927 rl->elvpriv++;
Tejun Heof1f8cc92011-12-14 00:33:42 +0100928 if (et->icq_cache && ioc)
929 icq = ioc_lookup_icq(ioc, q);
Mike Snitzer9d5a4e92011-02-11 11:05:46 +0100930 }
Tejun Heocb98fc82005-10-28 08:29:39 +0200931
Jens Axboef253b862010-10-24 22:06:02 +0200932 if (blk_queue_io_stat(q))
933 rw_flags |= REQ_IO_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 spin_unlock_irq(q->queue_lock);
935
Tejun Heof1f8cc92011-12-14 00:33:42 +0100936 /* create icq if missing */
Shaohua Li05c30b92012-01-19 09:20:10 +0100937 if ((rw_flags & REQ_ELVPRIV) && unlikely(et->icq_cache && !icq)) {
Tejun Heo852c7882012-03-05 13:15:27 -0800938 create_io_context(gfp_mask, q->node);
939 ioc = rq_ioc(bio);
940 if (!ioc)
941 goto fail_alloc;
942 icq = ioc_create_icq(ioc, q, gfp_mask);
Shaohua Li05c30b92012-01-19 09:20:10 +0100943 if (!icq)
Tejun Heob6792812012-03-05 13:15:23 -0800944 goto fail_alloc;
Shaohua Li05c30b92012-01-19 09:20:10 +0100945 }
Tejun Heof1f8cc92011-12-14 00:33:42 +0100946
Tejun Heo852c7882012-03-05 13:15:27 -0800947 rq = blk_alloc_request(q, bio, icq, rw_flags, gfp_mask);
Tejun Heob6792812012-03-05 13:15:23 -0800948 if (unlikely(!rq))
949 goto fail_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100951 /*
952 * ioc may be NULL here, and ioc_batching will be false. That's
953 * OK, if the queue is under the request limit then requests need
954 * not count toward the nr_batch_requests limit. There will always
955 * be some limit enforced by BLK_BATCH_TIME.
956 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 if (ioc_batching(q, ioc))
958 ioc->nr_batch_requests--;
Jens Axboe6728cb02008-01-31 13:03:55 +0100959
Jens Axboe1faa16d2009-04-06 14:48:01 +0200960 trace_block_getrq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return rq;
Tejun Heob6792812012-03-05 13:15:23 -0800962
963fail_alloc:
964 /*
965 * Allocation failed presumably due to memory. Undo anything we
966 * might have messed up.
967 *
968 * Allocating task should really be put onto the front of the wait
969 * queue, but this is pretty rare.
970 */
971 spin_lock_irq(q->queue_lock);
972 freed_request(q, rw_flags);
973
974 /*
975 * in the very unlikely event that allocation failed and no
976 * requests for this direction was pending, mark us starved so that
977 * freeing of a request in the other direction will notice
978 * us. another possible fix would be to split the rq mempool into
979 * READ and WRITE
980 */
981rq_starved:
982 if (unlikely(rl->count[is_sync] == 0))
983 rl->starved[is_sync] = 1;
984 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
Tejun Heoda8303c2011-10-19 14:33:05 +0200987/**
988 * get_request_wait - get a free request with retry
989 * @q: request_queue to allocate request from
990 * @rw_flags: RW and SYNC flags
991 * @bio: bio to allocate request for (can be %NULL)
Nick Piggind6344532005-06-28 20:45:14 -0700992 *
Tejun Heoda8303c2011-10-19 14:33:05 +0200993 * Get a free request from @q. This function keeps retrying under memory
994 * pressure and fails iff @q is dead.
995 *
996 * Must be callled with @q->queue_lock held and,
997 * Returns %NULL on failure, with @q->queue_lock held.
998 * Returns !%NULL on success, with @q->queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 */
Jens Axboe165125e2007-07-24 09:28:11 +02001000static struct request *get_request_wait(struct request_queue *q, int rw_flags,
Jens Axboe22e2c502005-06-27 10:55:12 +02001001 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
Jens Axboe1faa16d2009-04-06 14:48:01 +02001003 const bool is_sync = rw_is_sync(rw_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 struct request *rq;
1005
Jens Axboe7749a8d2006-12-13 13:02:26 +01001006 rq = get_request(q, rw_flags, bio, GFP_NOIO);
Nick Piggin450991b2005-06-28 20:45:13 -07001007 while (!rq) {
1008 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 struct request_list *rl = &q->rq;
1010
Tejun Heo34f60552011-12-14 00:33:37 +01001011 if (unlikely(blk_queue_dead(q)))
Tejun Heoda8303c2011-10-19 14:33:05 +02001012 return NULL;
1013
Jens Axboe1faa16d2009-04-06 14:48:01 +02001014 prepare_to_wait_exclusive(&rl->wait[is_sync], &wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 TASK_UNINTERRUPTIBLE);
1016
Jens Axboe1faa16d2009-04-06 14:48:01 +02001017 trace_block_sleeprq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +02001019 spin_unlock_irq(q->queue_lock);
1020 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +02001022 /*
1023 * After sleeping, we become a "batching" process and
1024 * will be able to allocate at least one request, and
1025 * up to a big batch of them for a small period time.
1026 * See ioc_batching, ioc_set_batching
1027 */
Tejun Heo24acfc32012-03-05 13:15:24 -08001028 create_io_context(GFP_NOIO, q->node);
Tejun Heof2dbd762011-12-14 00:33:40 +01001029 ioc_set_batching(q, current->io_context);
Jens Axboe2056a782006-03-23 20:00:26 +01001030
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +02001031 spin_lock_irq(q->queue_lock);
Jens Axboe1faa16d2009-04-06 14:48:01 +02001032 finish_wait(&rl->wait[is_sync], &wait);
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +02001033
1034 rq = get_request(q, rw_flags, bio, GFP_NOIO);
1035 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037 return rq;
1038}
1039
Jens Axboe165125e2007-07-24 09:28:11 +02001040struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
1042 struct request *rq;
1043
1044 BUG_ON(rw != READ && rw != WRITE);
1045
Nick Piggind6344532005-06-28 20:45:14 -07001046 spin_lock_irq(q->queue_lock);
Tejun Heoda8303c2011-10-19 14:33:05 +02001047 if (gfp_mask & __GFP_WAIT)
Jens Axboe22e2c502005-06-27 10:55:12 +02001048 rq = get_request_wait(q, rw, NULL);
Tejun Heoda8303c2011-10-19 14:33:05 +02001049 else
Jens Axboe22e2c502005-06-27 10:55:12 +02001050 rq = get_request(q, rw, NULL, gfp_mask);
Tejun Heoda8303c2011-10-19 14:33:05 +02001051 if (!rq)
1052 spin_unlock_irq(q->queue_lock);
Nick Piggind6344532005-06-28 20:45:14 -07001053 /* q->queue_lock is unlocked at this point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 return rq;
1056}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057EXPORT_SYMBOL(blk_get_request);
1058
1059/**
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001060 * blk_make_request - given a bio, allocate a corresponding struct request.
Randy Dunlap8ebf9752009-06-11 20:00:41 -07001061 * @q: target request queue
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001062 * @bio: The bio describing the memory mappings that will be submitted for IO.
1063 * It may be a chained-bio properly constructed by block/bio layer.
Randy Dunlap8ebf9752009-06-11 20:00:41 -07001064 * @gfp_mask: gfp flags to be used for memory allocation
Jens Axboedc72ef42006-07-20 14:54:05 +02001065 *
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001066 * blk_make_request is the parallel of generic_make_request for BLOCK_PC
1067 * type commands. Where the struct request needs to be farther initialized by
1068 * the caller. It is passed a &struct bio, which describes the memory info of
1069 * the I/O transfer.
1070 *
1071 * The caller of blk_make_request must make sure that bi_io_vec
1072 * are set to describe the memory buffers. That bio_data_dir() will return
1073 * the needed direction of the request. (And all bio's in the passed bio-chain
1074 * are properly set accordingly)
1075 *
1076 * If called under none-sleepable conditions, mapped bio buffers must not
1077 * need bouncing, by calling the appropriate masked or flagged allocator,
1078 * suitable for the target device. Otherwise the call to blk_queue_bounce will
1079 * BUG.
Jens Axboe53674ac2009-05-19 19:52:35 +02001080 *
1081 * WARNING: When allocating/cloning a bio-chain, careful consideration should be
1082 * given to how you allocate bios. In particular, you cannot use __GFP_WAIT for
1083 * anything but the first bio in the chain. Otherwise you risk waiting for IO
1084 * completion of a bio that hasn't been submitted yet, thus resulting in a
1085 * deadlock. Alternatively bios should be allocated using bio_kmalloc() instead
1086 * of bio_alloc(), as that avoids the mempool deadlock.
1087 * If possible a big IO should be split into smaller parts when allocation
1088 * fails. Partial allocation should not be an error, or you risk a live-lock.
Jens Axboedc72ef42006-07-20 14:54:05 +02001089 */
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001090struct request *blk_make_request(struct request_queue *q, struct bio *bio,
1091 gfp_t gfp_mask)
Jens Axboedc72ef42006-07-20 14:54:05 +02001092{
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001093 struct request *rq = blk_get_request(q, bio_data_dir(bio), gfp_mask);
1094
1095 if (unlikely(!rq))
1096 return ERR_PTR(-ENOMEM);
1097
1098 for_each_bio(bio) {
1099 struct bio *bounce_bio = bio;
1100 int ret;
1101
1102 blk_queue_bounce(q, &bounce_bio);
1103 ret = blk_rq_append_bio(q, rq, bounce_bio);
1104 if (unlikely(ret)) {
1105 blk_put_request(rq);
1106 return ERR_PTR(ret);
1107 }
1108 }
1109
1110 return rq;
Jens Axboedc72ef42006-07-20 14:54:05 +02001111}
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001112EXPORT_SYMBOL(blk_make_request);
Jens Axboedc72ef42006-07-20 14:54:05 +02001113
1114/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 * blk_requeue_request - put a request back on queue
1116 * @q: request queue where request should be inserted
1117 * @rq: request to be inserted
1118 *
1119 * Description:
1120 * Drivers often keep queueing requests until the hardware cannot accept
1121 * more, when that condition happens we need to put the request back
1122 * on the queue. Must be called with queue lock held.
1123 */
Jens Axboe165125e2007-07-24 09:28:11 +02001124void blk_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
Jens Axboe242f9dc2008-09-14 05:55:09 -07001126 blk_delete_timer(rq);
1127 blk_clear_rq_complete(rq);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001128 trace_block_rq_requeue(q, rq);
Jens Axboe2056a782006-03-23 20:00:26 +01001129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 if (blk_rq_tagged(rq))
1131 blk_queue_end_tag(q, rq);
1132
James Bottomleyba396a62009-05-27 14:17:08 +02001133 BUG_ON(blk_queued_rq(rq));
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 elv_requeue_request(q, rq);
1136}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137EXPORT_SYMBOL(blk_requeue_request);
1138
Jens Axboe73c10102011-03-08 13:19:51 +01001139static void add_acct_request(struct request_queue *q, struct request *rq,
1140 int where)
1141{
1142 drive_stat_acct(rq, 1);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001143 __elv_add_request(q, rq, where);
Jens Axboe73c10102011-03-08 13:19:51 +01001144}
1145
Tejun Heo074a7ac2008-08-25 19:56:14 +09001146static void part_round_stats_single(int cpu, struct hd_struct *part,
1147 unsigned long now)
1148{
1149 if (now == part->stamp)
1150 return;
1151
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001152 if (part_in_flight(part)) {
Tejun Heo074a7ac2008-08-25 19:56:14 +09001153 __part_stat_add(cpu, part, time_in_queue,
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001154 part_in_flight(part) * (now - part->stamp));
Tejun Heo074a7ac2008-08-25 19:56:14 +09001155 __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
1156 }
1157 part->stamp = now;
1158}
1159
1160/**
Randy Dunlap496aa8a2008-10-16 07:46:23 +02001161 * part_round_stats() - Round off the performance stats on a struct disk_stats.
1162 * @cpu: cpu number for stats access
1163 * @part: target partition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 *
1165 * The average IO queue length and utilisation statistics are maintained
1166 * by observing the current state of the queue length and the amount of
1167 * time it has been in this state for.
1168 *
1169 * Normally, that accounting is done on IO completion, but that can result
1170 * in more than a second's worth of IO being accounted for within any one
1171 * second, leading to >100% utilisation. To deal with that, we call this
1172 * function to do a round-off before returning the results when reading
1173 * /proc/diskstats. This accounts immediately for all queue usage up to
1174 * the current jiffies and restarts the counters again.
1175 */
Tejun Heoc9959052008-08-25 19:47:21 +09001176void part_round_stats(int cpu, struct hd_struct *part)
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001177{
1178 unsigned long now = jiffies;
1179
Tejun Heo074a7ac2008-08-25 19:56:14 +09001180 if (part->partno)
1181 part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
1182 part_round_stats_single(cpu, part, now);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001183}
Tejun Heo074a7ac2008-08-25 19:56:14 +09001184EXPORT_SYMBOL_GPL(part_round_stats);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186/*
1187 * queue lock must be held
1188 */
Jens Axboe165125e2007-07-24 09:28:11 +02001189void __blk_put_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 if (unlikely(!q))
1192 return;
1193 if (unlikely(--req->ref_count))
1194 return;
1195
Tejun Heo8922e162005-10-20 16:23:44 +02001196 elv_completed_request(q, req);
1197
Boaz Harrosh1cd96c22009-03-24 12:35:07 +01001198 /* this is a bio leak */
1199 WARN_ON(req->bio != NULL);
1200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 /*
1202 * Request may not have originated from ll_rw_blk. if not,
1203 * it didn't come out of our reserved rq pools
1204 */
Jens Axboe49171e52006-08-10 08:59:11 +02001205 if (req->cmd_flags & REQ_ALLOCED) {
Tejun Heo75eb6c32011-10-19 14:31:22 +02001206 unsigned int flags = req->cmd_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 BUG_ON(!list_empty(&req->queuelist));
Jens Axboe98170642006-07-28 09:23:08 +02001209 BUG_ON(!hlist_unhashed(&req->hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 blk_free_request(q, req);
Tejun Heo75eb6c32011-10-19 14:31:22 +02001212 freed_request(q, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
1214}
Mike Christie6e39b69e2005-11-11 05:30:24 -06001215EXPORT_SYMBOL_GPL(__blk_put_request);
1216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217void blk_put_request(struct request *req)
1218{
Tejun Heo8922e162005-10-20 16:23:44 +02001219 unsigned long flags;
Jens Axboe165125e2007-07-24 09:28:11 +02001220 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
FUJITA Tomonori52a93ba2008-07-15 21:21:45 +02001222 spin_lock_irqsave(q->queue_lock, flags);
1223 __blk_put_request(q, req);
1224 spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226EXPORT_SYMBOL(blk_put_request);
1227
Christoph Hellwig66ac0282010-06-18 16:59:42 +02001228/**
1229 * blk_add_request_payload - add a payload to a request
1230 * @rq: request to update
1231 * @page: page backing the payload
1232 * @len: length of the payload.
1233 *
1234 * This allows to later add a payload to an already submitted request by
1235 * a block driver. The driver needs to take care of freeing the payload
1236 * itself.
1237 *
1238 * Note that this is a quite horrible hack and nothing but handling of
1239 * discard requests should ever use it.
1240 */
1241void blk_add_request_payload(struct request *rq, struct page *page,
1242 unsigned int len)
1243{
1244 struct bio *bio = rq->bio;
1245
1246 bio->bi_io_vec->bv_page = page;
1247 bio->bi_io_vec->bv_offset = 0;
1248 bio->bi_io_vec->bv_len = len;
1249
1250 bio->bi_size = len;
1251 bio->bi_vcnt = 1;
1252 bio->bi_phys_segments = 1;
1253
1254 rq->__data_len = rq->resid_len = len;
1255 rq->nr_phys_segments = 1;
1256 rq->buffer = bio_data(bio);
1257}
1258EXPORT_SYMBOL_GPL(blk_add_request_payload);
1259
Jens Axboe73c10102011-03-08 13:19:51 +01001260static bool bio_attempt_back_merge(struct request_queue *q, struct request *req,
1261 struct bio *bio)
1262{
1263 const int ff = bio->bi_rw & REQ_FAILFAST_MASK;
1264
Jens Axboe73c10102011-03-08 13:19:51 +01001265 if (!ll_back_merge_fn(q, req, bio))
1266 return false;
1267
1268 trace_block_bio_backmerge(q, bio);
1269
1270 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1271 blk_rq_set_mixed_merge(req);
1272
1273 req->biotail->bi_next = bio;
1274 req->biotail = bio;
1275 req->__data_len += bio->bi_size;
1276 req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
1277
1278 drive_stat_acct(req, 0);
1279 return true;
1280}
1281
1282static bool bio_attempt_front_merge(struct request_queue *q,
1283 struct request *req, struct bio *bio)
1284{
1285 const int ff = bio->bi_rw & REQ_FAILFAST_MASK;
Jens Axboe73c10102011-03-08 13:19:51 +01001286
Jens Axboe73c10102011-03-08 13:19:51 +01001287 if (!ll_front_merge_fn(q, req, bio))
1288 return false;
1289
1290 trace_block_bio_frontmerge(q, bio);
1291
1292 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1293 blk_rq_set_mixed_merge(req);
1294
Jens Axboe73c10102011-03-08 13:19:51 +01001295 bio->bi_next = req->bio;
1296 req->bio = bio;
1297
1298 /*
1299 * may not be valid. if the low level driver said
1300 * it didn't need a bounce buffer then it better
1301 * not touch req->buffer either...
1302 */
1303 req->buffer = bio_data(bio);
1304 req->__sector = bio->bi_sector;
1305 req->__data_len += bio->bi_size;
1306 req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
1307
1308 drive_stat_acct(req, 0);
1309 return true;
1310}
1311
Tejun Heobd87b582011-10-19 14:33:08 +02001312/**
1313 * attempt_plug_merge - try to merge with %current's plugged list
1314 * @q: request_queue new bio is being queued at
1315 * @bio: new bio being queued
1316 * @request_count: out parameter for number of traversed plugged requests
1317 *
1318 * Determine whether @bio being queued on @q can be merged with a request
1319 * on %current's plugged list. Returns %true if merge was successful,
1320 * otherwise %false.
1321 *
Tejun Heo07c2bd32012-02-08 09:19:42 +01001322 * Plugging coalesces IOs from the same issuer for the same purpose without
1323 * going through @q->queue_lock. As such it's more of an issuing mechanism
1324 * than scheduling, and the request, while may have elvpriv data, is not
1325 * added on the elevator at this point. In addition, we don't have
1326 * reliable access to the elevator outside queue lock. Only check basic
1327 * merging parameters without querying the elevator.
Jens Axboe73c10102011-03-08 13:19:51 +01001328 */
Tejun Heobd87b582011-10-19 14:33:08 +02001329static bool attempt_plug_merge(struct request_queue *q, struct bio *bio,
1330 unsigned int *request_count)
Jens Axboe73c10102011-03-08 13:19:51 +01001331{
1332 struct blk_plug *plug;
1333 struct request *rq;
1334 bool ret = false;
1335
Tejun Heobd87b582011-10-19 14:33:08 +02001336 plug = current->plug;
Jens Axboe73c10102011-03-08 13:19:51 +01001337 if (!plug)
1338 goto out;
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001339 *request_count = 0;
Jens Axboe73c10102011-03-08 13:19:51 +01001340
1341 list_for_each_entry_reverse(rq, &plug->list, queuelist) {
1342 int el_ret;
1343
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001344 (*request_count)++;
1345
Tejun Heo07c2bd32012-02-08 09:19:42 +01001346 if (rq->q != q || !blk_rq_merge_ok(rq, bio))
Jens Axboe73c10102011-03-08 13:19:51 +01001347 continue;
1348
Tejun Heo050c8ea2012-02-08 09:19:38 +01001349 el_ret = blk_try_merge(rq, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001350 if (el_ret == ELEVATOR_BACK_MERGE) {
1351 ret = bio_attempt_back_merge(q, rq, bio);
1352 if (ret)
1353 break;
1354 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
1355 ret = bio_attempt_front_merge(q, rq, bio);
1356 if (ret)
1357 break;
1358 }
1359 }
1360out:
1361 return ret;
1362}
1363
Jens Axboe86db1e22008-01-29 14:53:40 +01001364void init_request_from_bio(struct request *req, struct bio *bio)
Tejun Heo52d9e672006-01-06 09:49:58 +01001365{
Jens Axboe4aff5e22006-08-10 08:44:47 +02001366 req->cmd_type = REQ_TYPE_FS;
Tejun Heo52d9e672006-01-06 09:49:58 +01001367
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001368 req->cmd_flags |= bio->bi_rw & REQ_COMMON_MASK;
1369 if (bio->bi_rw & REQ_RAHEAD)
Tejun Heoa82afdf2009-07-03 17:48:16 +09001370 req->cmd_flags |= REQ_FAILFAST_MASK;
Jens Axboeb31dc662006-06-13 08:26:10 +02001371
Tejun Heo52d9e672006-01-06 09:49:58 +01001372 req->errors = 0;
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001373 req->__sector = bio->bi_sector;
Tejun Heo52d9e672006-01-06 09:49:58 +01001374 req->ioprio = bio_prio(bio);
NeilBrownbc1c56f2007-08-16 13:31:30 +02001375 blk_rq_bio_prep(req->q, req, bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001376}
1377
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001378void blk_queue_bio(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
Jiri Slaby5e00d1b2010-08-12 14:31:06 +02001380 const bool sync = !!(bio->bi_rw & REQ_SYNC);
Jens Axboe73c10102011-03-08 13:19:51 +01001381 struct blk_plug *plug;
1382 int el_ret, rw_flags, where = ELEVATOR_INSERT_SORT;
1383 struct request *req;
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001384 unsigned int request_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 /*
1387 * low level driver can indicate that it wants pages above a
1388 * certain limit bounced to low memory (ie for highmem, or even
1389 * ISA dma in theory)
1390 */
1391 blk_queue_bounce(q, &bio);
1392
Tejun Heo4fed9472010-09-03 11:56:17 +02001393 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) {
Jens Axboe73c10102011-03-08 13:19:51 +01001394 spin_lock_irq(q->queue_lock);
Tejun Heoae1b1532011-01-25 12:43:54 +01001395 where = ELEVATOR_INSERT_FLUSH;
Tejun Heo28e7d182010-09-03 11:56:16 +02001396 goto get_rq;
1397 }
1398
Jens Axboe73c10102011-03-08 13:19:51 +01001399 /*
1400 * Check if we can merge with the plugged list before grabbing
1401 * any locks.
1402 */
Tejun Heobd87b582011-10-19 14:33:08 +02001403 if (attempt_plug_merge(q, bio, &request_count))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001404 return;
Jens Axboe73c10102011-03-08 13:19:51 +01001405
1406 spin_lock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 el_ret = elv_merge(q, &req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001409 if (el_ret == ELEVATOR_BACK_MERGE) {
Jens Axboe73c10102011-03-08 13:19:51 +01001410 if (bio_attempt_back_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_back_merge(q, req))
1413 elv_merged_request(q, req, el_ret);
1414 goto out_unlock;
Tejun Heo80a761f2009-07-03 17:48:17 +09001415 }
Jens Axboe73c10102011-03-08 13:19:51 +01001416 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
Jens Axboe73c10102011-03-08 13:19:51 +01001417 if (bio_attempt_front_merge(q, req, bio)) {
Tejun Heo07c2bd32012-02-08 09:19:42 +01001418 elv_bio_merged(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001419 if (!attempt_front_merge(q, req))
1420 elv_merged_request(q, req, el_ret);
1421 goto out_unlock;
1422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
1424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425get_rq:
Nick Piggin450991b2005-06-28 20:45:13 -07001426 /*
Jens Axboe7749a8d2006-12-13 13:02:26 +01001427 * This sync check and mask will be re-done in init_request_from_bio(),
1428 * but we need to set it earlier to expose the sync flag to the
1429 * rq allocator and io schedulers.
1430 */
1431 rw_flags = bio_data_dir(bio);
1432 if (sync)
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001433 rw_flags |= REQ_SYNC;
Jens Axboe7749a8d2006-12-13 13:02:26 +01001434
1435 /*
Nick Piggin450991b2005-06-28 20:45:13 -07001436 * Grab a free request. This is might sleep but can not fail.
Nick Piggind6344532005-06-28 20:45:14 -07001437 * Returns with the queue unlocked.
Nick Piggin450991b2005-06-28 20:45:13 -07001438 */
Jens Axboe7749a8d2006-12-13 13:02:26 +01001439 req = get_request_wait(q, rw_flags, bio);
Tejun Heoda8303c2011-10-19 14:33:05 +02001440 if (unlikely(!req)) {
1441 bio_endio(bio, -ENODEV); /* @q is dead */
1442 goto out_unlock;
1443 }
Nick Piggind6344532005-06-28 20:45:14 -07001444
Nick Piggin450991b2005-06-28 20:45:13 -07001445 /*
1446 * After dropping the lock and possibly sleeping here, our request
1447 * may now be mergeable after it had proven unmergeable (above).
1448 * We don't worry about that case for efficiency. It won't happen
1449 * often, and the elevators are able to handle it.
1450 */
Tejun Heo52d9e672006-01-06 09:49:58 +01001451 init_request_from_bio(req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Tao Ma9562ad92011-10-24 16:11:30 +02001453 if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags))
Jens Axboe11ccf112011-07-26 15:01:15 +02001454 req->cpu = raw_smp_processor_id();
Tejun Heodd831002010-09-03 11:56:16 +02001455
Jens Axboe73c10102011-03-08 13:19:51 +01001456 plug = current->plug;
Jens Axboe721a9602011-03-09 11:56:30 +01001457 if (plug) {
Jens Axboedc6d36c2011-04-12 10:28:28 +02001458 /*
1459 * If this is the first request added after a plug, fire
1460 * of a plug trace. If others have been added before, check
1461 * if we have multiple devices in this plug. If so, make a
1462 * note to sort the list before dispatch.
1463 */
1464 if (list_empty(&plug->list))
1465 trace_block_plug(q);
Shaohua Li3540d5e2011-11-16 09:21:50 +01001466 else {
1467 if (!plug->should_sort) {
1468 struct request *__rq;
Jens Axboe73c10102011-03-08 13:19:51 +01001469
Shaohua Li3540d5e2011-11-16 09:21:50 +01001470 __rq = list_entry_rq(plug->list.prev);
1471 if (__rq->q != q)
1472 plug->should_sort = 1;
1473 }
Shaohua Li019ceb72011-11-16 09:21:50 +01001474 if (request_count >= BLK_MAX_REQUEST_COUNT) {
Shaohua Li3540d5e2011-11-16 09:21:50 +01001475 blk_flush_plug_list(plug, false);
Shaohua Li019ceb72011-11-16 09:21:50 +01001476 trace_block_plug(q);
1477 }
Jens Axboe73c10102011-03-08 13:19:51 +01001478 }
Shaohua Lia6327162011-08-24 16:04:32 +02001479 list_add_tail(&req->queuelist, &plug->list);
1480 drive_stat_acct(req, 1);
Jens Axboe73c10102011-03-08 13:19:51 +01001481 } else {
1482 spin_lock_irq(q->queue_lock);
1483 add_acct_request(q, req, where);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02001484 __blk_run_queue(q);
Jens Axboe73c10102011-03-08 13:19:51 +01001485out_unlock:
1486 spin_unlock_irq(q->queue_lock);
1487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488}
Jens Axboec20e8de2011-09-12 12:03:37 +02001489EXPORT_SYMBOL_GPL(blk_queue_bio); /* for device mapper only */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
1491/*
1492 * If bio->bi_dev is a partition, remap the location
1493 */
1494static inline void blk_partition_remap(struct bio *bio)
1495{
1496 struct block_device *bdev = bio->bi_bdev;
1497
Jens Axboebf2de6f2007-09-27 13:01:25 +02001498 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 struct hd_struct *p = bdev->bd_part;
Jens Axboea3623572005-11-01 09:26:16 +01001500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 bio->bi_sector += p->start_sect;
1502 bio->bi_bdev = bdev->bd_contains;
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001503
Mike Snitzerd07335e2010-11-16 12:52:38 +01001504 trace_block_bio_remap(bdev_get_queue(bio->bi_bdev), bio,
1505 bdev->bd_dev,
1506 bio->bi_sector - p->start_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 }
1508}
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510static void handle_bad_sector(struct bio *bio)
1511{
1512 char b[BDEVNAME_SIZE];
1513
1514 printk(KERN_INFO "attempt to access beyond end of device\n");
1515 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1516 bdevname(bio->bi_bdev, b),
1517 bio->bi_rw,
1518 (unsigned long long)bio->bi_sector + bio_sectors(bio),
Mike Snitzer77304d22010-11-08 14:39:12 +01001519 (long long)(i_size_read(bio->bi_bdev->bd_inode) >> 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521 set_bit(BIO_EOF, &bio->bi_flags);
1522}
1523
Akinobu Mitac17bb492006-12-08 02:39:46 -08001524#ifdef CONFIG_FAIL_MAKE_REQUEST
1525
1526static DECLARE_FAULT_ATTR(fail_make_request);
1527
1528static int __init setup_fail_make_request(char *str)
1529{
1530 return setup_fault_attr(&fail_make_request, str);
1531}
1532__setup("fail_make_request=", setup_fail_make_request);
1533
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001534static bool should_fail_request(struct hd_struct *part, unsigned int bytes)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001535{
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001536 return part->make_it_fail && should_fail(&fail_make_request, bytes);
Akinobu Mitac17bb492006-12-08 02:39:46 -08001537}
1538
1539static int __init fail_make_request_debugfs(void)
1540{
Akinobu Mitadd48c082011-08-03 16:21:01 -07001541 struct dentry *dir = fault_create_debugfs_attr("fail_make_request",
1542 NULL, &fail_make_request);
1543
1544 return IS_ERR(dir) ? PTR_ERR(dir) : 0;
Akinobu Mitac17bb492006-12-08 02:39:46 -08001545}
1546
1547late_initcall(fail_make_request_debugfs);
1548
1549#else /* CONFIG_FAIL_MAKE_REQUEST */
1550
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001551static inline bool should_fail_request(struct hd_struct *part,
1552 unsigned int bytes)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001553{
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001554 return false;
Akinobu Mitac17bb492006-12-08 02:39:46 -08001555}
1556
1557#endif /* CONFIG_FAIL_MAKE_REQUEST */
1558
Jens Axboec07e2b42007-07-18 13:27:58 +02001559/*
1560 * Check whether this bio extends beyond the end of the device.
1561 */
1562static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1563{
1564 sector_t maxsector;
1565
1566 if (!nr_sectors)
1567 return 0;
1568
1569 /* Test device or partition size, when known. */
Mike Snitzer77304d22010-11-08 14:39:12 +01001570 maxsector = i_size_read(bio->bi_bdev->bd_inode) >> 9;
Jens Axboec07e2b42007-07-18 13:27:58 +02001571 if (maxsector) {
1572 sector_t sector = bio->bi_sector;
1573
1574 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1575 /*
1576 * This may well happen - the kernel calls bread()
1577 * without checking the size of the device, e.g., when
1578 * mounting a device.
1579 */
1580 handle_bad_sector(bio);
1581 return 1;
1582 }
1583 }
1584
1585 return 0;
1586}
1587
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001588static noinline_for_stack bool
1589generic_make_request_checks(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
Jens Axboe165125e2007-07-24 09:28:11 +02001591 struct request_queue *q;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001592 int nr_sectors = bio_sectors(bio);
Jens Axboe51fd77b2007-11-02 08:49:08 +01001593 int err = -EIO;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001594 char b[BDEVNAME_SIZE];
1595 struct hd_struct *part;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
Jens Axboec07e2b42007-07-18 13:27:58 +02001599 if (bio_check_eod(bio, nr_sectors))
1600 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001602 q = bdev_get_queue(bio->bi_bdev);
1603 if (unlikely(!q)) {
1604 printk(KERN_ERR
1605 "generic_make_request: Trying to access "
1606 "nonexistent block-device %s (%Lu)\n",
1607 bdevname(bio->bi_bdev, b),
1608 (long long) bio->bi_sector);
1609 goto end_io;
1610 }
1611
1612 if (unlikely(!(bio->bi_rw & REQ_DISCARD) &&
1613 nr_sectors > queue_max_hw_sectors(q))) {
1614 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
1615 bdevname(bio->bi_bdev, b),
1616 bio_sectors(bio),
1617 queue_max_hw_sectors(q));
1618 goto end_io;
1619 }
1620
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001621 part = bio->bi_bdev->bd_part;
1622 if (should_fail_request(part, bio->bi_size) ||
1623 should_fail_request(&part_to_disk(part)->part0,
1624 bio->bi_size))
1625 goto end_io;
1626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 /*
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001628 * If this device has partitions, remap block n
1629 * of partition p to block n+start(p) of the disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001631 blk_partition_remap(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001633 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio))
1634 goto end_io;
1635
1636 if (bio_check_eod(bio, nr_sectors))
1637 goto end_io;
1638
1639 /*
1640 * Filter flush bio's early so that make_request based
1641 * drivers without flush support don't have to worry
1642 * about them.
1643 */
1644 if ((bio->bi_rw & (REQ_FLUSH | REQ_FUA)) && !q->flush_flags) {
1645 bio->bi_rw &= ~(REQ_FLUSH | REQ_FUA);
1646 if (!nr_sectors) {
1647 err = 0;
Tejun Heoa7384672008-11-28 13:32:03 +09001648 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 }
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001652 if ((bio->bi_rw & REQ_DISCARD) &&
1653 (!blk_queue_discard(q) ||
1654 ((bio->bi_rw & REQ_SECURE) &&
1655 !blk_queue_secdiscard(q)))) {
1656 err = -EOPNOTSUPP;
1657 goto end_io;
1658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Tejun Heobc16a4f2011-10-19 14:33:01 +02001660 if (blk_throtl_bio(q, bio))
1661 return false; /* throttled, will be resubmitted later */
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001662
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001663 trace_block_bio_queue(q, bio);
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001664 return true;
Tejun Heoa7384672008-11-28 13:32:03 +09001665
1666end_io:
1667 bio_endio(bio, err);
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001668 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669}
1670
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001671/**
1672 * generic_make_request - hand a buffer to its device driver for I/O
1673 * @bio: The bio describing the location in memory and on the device.
1674 *
1675 * generic_make_request() is used to make I/O requests of block
1676 * devices. It is passed a &struct bio, which describes the I/O that needs
1677 * to be done.
1678 *
1679 * generic_make_request() does not return any status. The
1680 * success/failure status of the request, along with notification of
1681 * completion, is delivered asynchronously through the bio->bi_end_io
1682 * function described (one day) else where.
1683 *
1684 * The caller of generic_make_request must make sure that bi_io_vec
1685 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1686 * set to describe the device address, and the
1687 * bi_end_io and optionally bi_private are set to describe how
1688 * completion notification should be signaled.
1689 *
1690 * generic_make_request and the drivers it calls may use bi_next if this
1691 * bio happens to be merged with someone else, and may resubmit the bio to
1692 * a lower device by calling into generic_make_request recursively, which
1693 * means the bio should NOT be touched after the call to ->make_request_fn.
Neil Brownd89d8792007-05-01 09:53:42 +02001694 */
1695void generic_make_request(struct bio *bio)
1696{
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001697 struct bio_list bio_list_on_stack;
1698
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001699 if (!generic_make_request_checks(bio))
1700 return;
1701
1702 /*
1703 * We only want one ->make_request_fn to be active at a time, else
1704 * stack usage with stacked devices could be a problem. So use
1705 * current->bio_list to keep a list of requests submited by a
1706 * make_request_fn function. current->bio_list is also used as a
1707 * flag to say if generic_make_request is currently active in this
1708 * task or not. If it is NULL, then no make_request is active. If
1709 * it is non-NULL, then a make_request is active, and new requests
1710 * should be added at the tail
1711 */
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001712 if (current->bio_list) {
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001713 bio_list_add(current->bio_list, bio);
Neil Brownd89d8792007-05-01 09:53:42 +02001714 return;
1715 }
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001716
Neil Brownd89d8792007-05-01 09:53:42 +02001717 /* following loop may be a bit non-obvious, and so deserves some
1718 * explanation.
1719 * Before entering the loop, bio->bi_next is NULL (as all callers
1720 * ensure that) so we have a list with a single bio.
1721 * We pretend that we have just taken it off a longer list, so
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001722 * we assign bio_list to a pointer to the bio_list_on_stack,
1723 * thus initialising the bio_list of new bios to be
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001724 * added. ->make_request() may indeed add some more bios
Neil Brownd89d8792007-05-01 09:53:42 +02001725 * through a recursive call to generic_make_request. If it
1726 * did, we find a non-NULL value in bio_list and re-enter the loop
1727 * from the top. In this case we really did just take the bio
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001728 * of the top of the list (no pretending) and so remove it from
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001729 * bio_list, and call into ->make_request() again.
Neil Brownd89d8792007-05-01 09:53:42 +02001730 */
1731 BUG_ON(bio->bi_next);
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001732 bio_list_init(&bio_list_on_stack);
1733 current->bio_list = &bio_list_on_stack;
Neil Brownd89d8792007-05-01 09:53:42 +02001734 do {
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001735 struct request_queue *q = bdev_get_queue(bio->bi_bdev);
1736
1737 q->make_request_fn(q, bio);
1738
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001739 bio = bio_list_pop(current->bio_list);
Neil Brownd89d8792007-05-01 09:53:42 +02001740 } while (bio);
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001741 current->bio_list = NULL; /* deactivate */
Neil Brownd89d8792007-05-01 09:53:42 +02001742}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743EXPORT_SYMBOL(generic_make_request);
1744
1745/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001746 * submit_bio - submit a bio to the block device layer for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1748 * @bio: The &struct bio which describes the I/O
1749 *
1750 * submit_bio() is very similar in purpose to generic_make_request(), and
1751 * uses that function to do most of the work. Both are fairly rough
Randy Dunlap710027a2008-08-19 20:13:11 +02001752 * interfaces; @bio must be presetup and ready for I/O.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 *
1754 */
1755void submit_bio(int rw, struct bio *bio)
1756{
1757 int count = bio_sectors(bio);
1758
Jens Axboe22e2c502005-06-27 10:55:12 +02001759 bio->bi_rw |= rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Jens Axboebf2de6f2007-09-27 13:01:25 +02001761 /*
1762 * If it's a regular read/write or a barrier with data attached,
1763 * go through the normal accounting stuff before submission.
1764 */
Jens Axboe3ffb52e2010-06-29 13:33:38 +02001765 if (bio_has_data(bio) && !(rw & REQ_DISCARD)) {
Jens Axboebf2de6f2007-09-27 13:01:25 +02001766 if (rw & WRITE) {
1767 count_vm_events(PGPGOUT, count);
1768 } else {
1769 task_io_account_read(bio->bi_size);
1770 count_vm_events(PGPGIN, count);
1771 }
1772
1773 if (unlikely(block_dump)) {
1774 char b[BDEVNAME_SIZE];
San Mehat8dcbdc72010-09-14 08:48:01 +02001775 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s (%u sectors)\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001776 current->comm, task_pid_nr(current),
Jens Axboebf2de6f2007-09-27 13:01:25 +02001777 (rw & WRITE) ? "WRITE" : "READ",
1778 (unsigned long long)bio->bi_sector,
San Mehat8dcbdc72010-09-14 08:48:01 +02001779 bdevname(bio->bi_bdev, b),
1780 count);
Jens Axboebf2de6f2007-09-27 13:01:25 +02001781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 }
1783
1784 generic_make_request(bio);
1785}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786EXPORT_SYMBOL(submit_bio);
1787
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001788/**
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001789 * blk_rq_check_limits - Helper function to check a request for the queue limit
1790 * @q: the queue
1791 * @rq: the request being checked
1792 *
1793 * Description:
1794 * @rq may have been made based on weaker limitations of upper-level queues
1795 * in request stacking drivers, and it may violate the limitation of @q.
1796 * Since the block layer and the underlying device driver trust @rq
1797 * after it is inserted to @q, it should be checked against @q before
1798 * the insertion using this generic function.
1799 *
1800 * This function should also be useful for request stacking drivers
Stefan Weileef35c22010-08-06 21:11:15 +02001801 * in some cases below, so export this function.
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001802 * Request stacking drivers like request-based dm may change the queue
1803 * limits while requests are in the queue (e.g. dm's table swapping).
1804 * Such request stacking drivers should check those requests agaist
1805 * the new queue limits again when they dispatch those requests,
1806 * although such checkings are also done against the old queue limits
1807 * when submitting requests.
1808 */
1809int blk_rq_check_limits(struct request_queue *q, struct request *rq)
1810{
ike Snitzer33839772010-08-08 12:11:33 -04001811 if (rq->cmd_flags & REQ_DISCARD)
1812 return 0;
1813
Martin K. Petersenae03bf62009-05-22 17:17:50 -04001814 if (blk_rq_sectors(rq) > queue_max_sectors(q) ||
1815 blk_rq_bytes(rq) > queue_max_hw_sectors(q) << 9) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001816 printk(KERN_ERR "%s: over max size limit.\n", __func__);
1817 return -EIO;
1818 }
1819
1820 /*
1821 * queue's settings related to segment counting like q->bounce_pfn
1822 * may differ from that of other stacking queues.
1823 * Recalculate it to check the request correctly on this queue's
1824 * limitation.
1825 */
1826 blk_recalc_rq_segments(rq);
Martin K. Petersen8a783622010-02-26 00:20:39 -05001827 if (rq->nr_phys_segments > queue_max_segments(q)) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001828 printk(KERN_ERR "%s: over max segments limit.\n", __func__);
1829 return -EIO;
1830 }
1831
1832 return 0;
1833}
1834EXPORT_SYMBOL_GPL(blk_rq_check_limits);
1835
1836/**
1837 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
1838 * @q: the queue to submit the request
1839 * @rq: the request being queued
1840 */
1841int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
1842{
1843 unsigned long flags;
Jeff Moyer4853aba2011-08-15 21:37:25 +02001844 int where = ELEVATOR_INSERT_BACK;
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001845
1846 if (blk_rq_check_limits(q, rq))
1847 return -EIO;
1848
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001849 if (rq->rq_disk &&
1850 should_fail_request(&rq->rq_disk->part0, blk_rq_bytes(rq)))
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001851 return -EIO;
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001852
1853 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo8ba61432011-12-14 00:33:37 +01001854 if (unlikely(blk_queue_dead(q))) {
1855 spin_unlock_irqrestore(q->queue_lock, flags);
1856 return -ENODEV;
1857 }
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001858
1859 /*
1860 * Submitting request must be dequeued before calling this function
1861 * because it will be linked to another request_queue
1862 */
1863 BUG_ON(blk_queued_rq(rq));
1864
Jeff Moyer4853aba2011-08-15 21:37:25 +02001865 if (rq->cmd_flags & (REQ_FLUSH|REQ_FUA))
1866 where = ELEVATOR_INSERT_FLUSH;
1867
1868 add_acct_request(q, rq, where);
Jeff Moyere67b77c2011-10-17 12:57:23 +02001869 if (where == ELEVATOR_INSERT_FLUSH)
1870 __blk_run_queue(q);
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001871 spin_unlock_irqrestore(q->queue_lock, flags);
1872
1873 return 0;
1874}
1875EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
1876
Tejun Heo80a761f2009-07-03 17:48:17 +09001877/**
1878 * blk_rq_err_bytes - determine number of bytes till the next failure boundary
1879 * @rq: request to examine
1880 *
1881 * Description:
1882 * A request could be merge of IOs which require different failure
1883 * handling. This function determines the number of bytes which
1884 * can be failed from the beginning of the request without
1885 * crossing into area which need to be retried further.
1886 *
1887 * Return:
1888 * The number of bytes to fail.
1889 *
1890 * Context:
1891 * queue_lock must be held.
1892 */
1893unsigned int blk_rq_err_bytes(const struct request *rq)
1894{
1895 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
1896 unsigned int bytes = 0;
1897 struct bio *bio;
1898
1899 if (!(rq->cmd_flags & REQ_MIXED_MERGE))
1900 return blk_rq_bytes(rq);
1901
1902 /*
1903 * Currently the only 'mixing' which can happen is between
1904 * different fastfail types. We can safely fail portions
1905 * which have all the failfast bits that the first one has -
1906 * the ones which are at least as eager to fail as the first
1907 * one.
1908 */
1909 for (bio = rq->bio; bio; bio = bio->bi_next) {
1910 if ((bio->bi_rw & ff) != ff)
1911 break;
1912 bytes += bio->bi_size;
1913 }
1914
1915 /* this could lead to infinite loop */
1916 BUG_ON(blk_rq_bytes(rq) && !bytes);
1917 return bytes;
1918}
1919EXPORT_SYMBOL_GPL(blk_rq_err_bytes);
1920
Jens Axboebc58ba92009-01-23 10:54:44 +01001921static void blk_account_io_completion(struct request *req, unsigned int bytes)
1922{
Jens Axboec2553b52009-04-24 08:10:11 +02001923 if (blk_do_io_stat(req)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01001924 const int rw = rq_data_dir(req);
1925 struct hd_struct *part;
1926 int cpu;
1927
1928 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +01001929 part = req->part;
Jens Axboebc58ba92009-01-23 10:54:44 +01001930 part_stat_add(cpu, part, sectors[rw], bytes >> 9);
1931 part_stat_unlock();
1932 }
1933}
1934
1935static void blk_account_io_done(struct request *req)
1936{
Jens Axboebc58ba92009-01-23 10:54:44 +01001937 /*
Tejun Heodd4c1332010-09-03 11:56:16 +02001938 * Account IO completion. flush_rq isn't accounted as a
1939 * normal IO on queueing nor completion. Accounting the
1940 * containing request is enough.
Jens Axboebc58ba92009-01-23 10:54:44 +01001941 */
Tejun Heo414b4ff2011-01-25 12:43:49 +01001942 if (blk_do_io_stat(req) && !(req->cmd_flags & REQ_FLUSH_SEQ)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01001943 unsigned long duration = jiffies - req->start_time;
1944 const int rw = rq_data_dir(req);
1945 struct hd_struct *part;
1946 int cpu;
1947
1948 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +01001949 part = req->part;
Jens Axboebc58ba92009-01-23 10:54:44 +01001950
1951 part_stat_inc(cpu, part, ios[rw]);
1952 part_stat_add(cpu, part, ticks[rw], duration);
1953 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001954 part_dec_in_flight(part, rw);
Jens Axboebc58ba92009-01-23 10:54:44 +01001955
Jens Axboe6c23a962011-01-07 08:43:37 +01001956 hd_struct_put(part);
Jens Axboebc58ba92009-01-23 10:54:44 +01001957 part_stat_unlock();
1958 }
1959}
1960
Tejun Heo53a08802008-12-03 12:41:26 +01001961/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09001962 * blk_peek_request - peek at the top of a request queue
1963 * @q: request queue to peek at
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001964 *
1965 * Description:
Tejun Heo9934c8c2009-05-08 11:54:16 +09001966 * Return the request at the top of @q. The returned request
1967 * should be started using blk_start_request() before LLD starts
1968 * processing it.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001969 *
1970 * Return:
Tejun Heo9934c8c2009-05-08 11:54:16 +09001971 * Pointer to the request at the top of @q if available. Null
1972 * otherwise.
1973 *
1974 * Context:
1975 * queue_lock must be held.
1976 */
1977struct request *blk_peek_request(struct request_queue *q)
Tejun Heo158dbda2009-04-23 11:05:18 +09001978{
1979 struct request *rq;
1980 int ret;
1981
1982 while ((rq = __elv_next_request(q)) != NULL) {
1983 if (!(rq->cmd_flags & REQ_STARTED)) {
1984 /*
1985 * This is the first time the device driver
1986 * sees this request (possibly after
1987 * requeueing). Notify IO scheduler.
1988 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001989 if (rq->cmd_flags & REQ_SORTED)
Tejun Heo158dbda2009-04-23 11:05:18 +09001990 elv_activate_rq(q, rq);
1991
1992 /*
1993 * just mark as started even if we don't start
1994 * it, a request that has been delayed should
1995 * not be passed by new incoming requests
1996 */
1997 rq->cmd_flags |= REQ_STARTED;
1998 trace_block_rq_issue(q, rq);
1999 }
2000
2001 if (!q->boundary_rq || q->boundary_rq == rq) {
2002 q->end_sector = rq_end_sector(rq);
2003 q->boundary_rq = NULL;
2004 }
2005
2006 if (rq->cmd_flags & REQ_DONTPREP)
2007 break;
2008
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002009 if (q->dma_drain_size && blk_rq_bytes(rq)) {
Tejun Heo158dbda2009-04-23 11:05:18 +09002010 /*
2011 * make sure space for the drain appears we
2012 * know we can do this because max_hw_segments
2013 * has been adjusted to be one fewer than the
2014 * device can handle
2015 */
2016 rq->nr_phys_segments++;
2017 }
2018
2019 if (!q->prep_rq_fn)
2020 break;
2021
2022 ret = q->prep_rq_fn(q, rq);
2023 if (ret == BLKPREP_OK) {
2024 break;
2025 } else if (ret == BLKPREP_DEFER) {
2026 /*
2027 * the request may have been (partially) prepped.
2028 * we need to keep this request in the front to
2029 * avoid resource deadlock. REQ_STARTED will
2030 * prevent other fs requests from passing this one.
2031 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002032 if (q->dma_drain_size && blk_rq_bytes(rq) &&
Tejun Heo158dbda2009-04-23 11:05:18 +09002033 !(rq->cmd_flags & REQ_DONTPREP)) {
2034 /*
2035 * remove the space for the drain we added
2036 * so that we don't add it again
2037 */
2038 --rq->nr_phys_segments;
2039 }
2040
2041 rq = NULL;
2042 break;
2043 } else if (ret == BLKPREP_KILL) {
2044 rq->cmd_flags |= REQ_QUIET;
James Bottomleyc143dc92009-05-30 06:43:49 +02002045 /*
2046 * Mark this request as started so we don't trigger
2047 * any debug logic in the end I/O path.
2048 */
2049 blk_start_request(rq);
Tejun Heo40cbbb72009-04-23 11:05:19 +09002050 __blk_end_request_all(rq, -EIO);
Tejun Heo158dbda2009-04-23 11:05:18 +09002051 } else {
2052 printk(KERN_ERR "%s: bad return=%d\n", __func__, ret);
2053 break;
2054 }
2055 }
2056
2057 return rq;
2058}
Tejun Heo9934c8c2009-05-08 11:54:16 +09002059EXPORT_SYMBOL(blk_peek_request);
Tejun Heo158dbda2009-04-23 11:05:18 +09002060
Tejun Heo9934c8c2009-05-08 11:54:16 +09002061void blk_dequeue_request(struct request *rq)
Tejun Heo158dbda2009-04-23 11:05:18 +09002062{
Tejun Heo9934c8c2009-05-08 11:54:16 +09002063 struct request_queue *q = rq->q;
2064
Tejun Heo158dbda2009-04-23 11:05:18 +09002065 BUG_ON(list_empty(&rq->queuelist));
2066 BUG_ON(ELV_ON_HASH(rq));
2067
2068 list_del_init(&rq->queuelist);
2069
2070 /*
2071 * the time frame between a request being removed from the lists
2072 * and to it is freed is accounted as io that is in progress at
2073 * the driver side.
2074 */
Divyesh Shah91952912010-04-01 15:01:41 -07002075 if (blk_account_rq(rq)) {
Jens Axboe0a7ae2f2009-05-20 08:54:31 +02002076 q->in_flight[rq_is_sync(rq)]++;
Divyesh Shah91952912010-04-01 15:01:41 -07002077 set_io_start_time_ns(rq);
2078 }
Tejun Heo158dbda2009-04-23 11:05:18 +09002079}
2080
Tejun Heo5efccd12009-04-23 11:05:18 +09002081/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09002082 * blk_start_request - start request processing on the driver
2083 * @req: request to dequeue
2084 *
2085 * Description:
2086 * Dequeue @req and start timeout timer on it. This hands off the
2087 * request to the driver.
2088 *
2089 * Block internal functions which don't want to start timer should
2090 * call blk_dequeue_request().
2091 *
2092 * Context:
2093 * queue_lock must be held.
2094 */
2095void blk_start_request(struct request *req)
2096{
2097 blk_dequeue_request(req);
2098
2099 /*
Tejun Heo5f49f632009-05-19 18:33:05 +09002100 * We are now handing the request to the hardware, initialize
2101 * resid_len to full count and add the timeout handler.
Tejun Heo9934c8c2009-05-08 11:54:16 +09002102 */
Tejun Heo5f49f632009-05-19 18:33:05 +09002103 req->resid_len = blk_rq_bytes(req);
FUJITA Tomonoridbb66c42009-06-09 05:47:10 +02002104 if (unlikely(blk_bidi_rq(req)))
2105 req->next_rq->resid_len = blk_rq_bytes(req->next_rq);
2106
Tejun Heo9934c8c2009-05-08 11:54:16 +09002107 blk_add_timer(req);
2108}
2109EXPORT_SYMBOL(blk_start_request);
2110
2111/**
2112 * blk_fetch_request - fetch a request from a request queue
2113 * @q: request queue to fetch a request from
2114 *
2115 * Description:
2116 * Return the request at the top of @q. The request is started on
2117 * return and LLD can start processing it immediately.
2118 *
2119 * Return:
2120 * Pointer to the request at the top of @q if available. Null
2121 * otherwise.
2122 *
2123 * Context:
2124 * queue_lock must be held.
2125 */
2126struct request *blk_fetch_request(struct request_queue *q)
2127{
2128 struct request *rq;
2129
2130 rq = blk_peek_request(q);
2131 if (rq)
2132 blk_start_request(rq);
2133 return rq;
2134}
2135EXPORT_SYMBOL(blk_fetch_request);
2136
2137/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002138 * blk_update_request - Special helper function for request stacking drivers
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002139 * @req: the request being processed
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002140 * @error: %0 for success, < %0 for error
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002141 * @nr_bytes: number of bytes to complete @req
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002142 *
2143 * Description:
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002144 * Ends I/O on a number of bytes attached to @req, but doesn't complete
2145 * the request structure even if @req doesn't have leftover.
2146 * If @req has leftover, sets it up for the next range of segments.
Tejun Heo2e60e022009-04-23 11:05:18 +09002147 *
2148 * This special helper function is only for request stacking drivers
2149 * (e.g. request-based dm) so that they can handle partial completion.
2150 * Actual device drivers should use blk_end_request instead.
2151 *
2152 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
2153 * %false return from this function.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002154 *
2155 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002156 * %false - this request doesn't have any more data
2157 * %true - this request has more data
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002158 **/
Tejun Heo2e60e022009-04-23 11:05:18 +09002159bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160{
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05002161 int total_bytes, bio_nbytes, next_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 struct bio *bio;
2163
Tejun Heo2e60e022009-04-23 11:05:18 +09002164 if (!req->bio)
2165 return false;
2166
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01002167 trace_block_rq_complete(req->q, req);
Jens Axboe2056a782006-03-23 20:00:26 +01002168
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 /*
Tejun Heo6f414692009-04-19 07:00:41 +09002170 * For fs requests, rq is just carrier of independent bio's
2171 * and each partial completion should be handled separately.
2172 * Reset per-request error on each partial completion.
2173 *
2174 * TODO: tj: This is too subtle. It would be better to let
2175 * low level drivers do what they see fit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002177 if (req->cmd_type == REQ_TYPE_FS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 req->errors = 0;
2179
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002180 if (error && req->cmd_type == REQ_TYPE_FS &&
2181 !(req->cmd_flags & REQ_QUIET)) {
Hannes Reinecke79775562011-01-18 10:13:13 +01002182 char *error_type;
2183
2184 switch (error) {
2185 case -ENOLINK:
2186 error_type = "recoverable transport";
2187 break;
2188 case -EREMOTEIO:
2189 error_type = "critical target";
2190 break;
2191 case -EBADE:
2192 error_type = "critical nexus";
2193 break;
2194 case -EIO:
2195 default:
2196 error_type = "I/O";
2197 break;
2198 }
2199 printk(KERN_ERR "end_request: %s error, dev %s, sector %llu\n",
2200 error_type, req->rq_disk ? req->rq_disk->disk_name : "?",
2201 (unsigned long long)blk_rq_pos(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 }
2203
Jens Axboebc58ba92009-01-23 10:54:44 +01002204 blk_account_io_completion(req, nr_bytes);
Jens Axboed72d9042005-11-01 08:35:42 +01002205
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 total_bytes = bio_nbytes = 0;
2207 while ((bio = req->bio) != NULL) {
2208 int nbytes;
2209
2210 if (nr_bytes >= bio->bi_size) {
2211 req->bio = bio->bi_next;
2212 nbytes = bio->bi_size;
NeilBrown5bb23a62007-09-27 12:46:13 +02002213 req_bio_endio(req, bio, nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 next_idx = 0;
2215 bio_nbytes = 0;
2216 } else {
2217 int idx = bio->bi_idx + next_idx;
2218
Kazuhisa Ichikawaaf498d72009-05-12 13:27:45 +02002219 if (unlikely(idx >= bio->bi_vcnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 blk_dump_rq_flags(req, "__end_that");
Jens Axboe6728cb02008-01-31 13:03:55 +01002221 printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
Kazuhisa Ichikawaaf498d72009-05-12 13:27:45 +02002222 __func__, idx, bio->bi_vcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 break;
2224 }
2225
2226 nbytes = bio_iovec_idx(bio, idx)->bv_len;
2227 BIO_BUG_ON(nbytes > bio->bi_size);
2228
2229 /*
2230 * not a complete bvec done
2231 */
2232 if (unlikely(nbytes > nr_bytes)) {
2233 bio_nbytes += nr_bytes;
2234 total_bytes += nr_bytes;
2235 break;
2236 }
2237
2238 /*
2239 * advance to the next vector
2240 */
2241 next_idx++;
2242 bio_nbytes += nbytes;
2243 }
2244
2245 total_bytes += nbytes;
2246 nr_bytes -= nbytes;
2247
Jens Axboe6728cb02008-01-31 13:03:55 +01002248 bio = req->bio;
2249 if (bio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 /*
2251 * end more in this run, or just return 'not-done'
2252 */
2253 if (unlikely(nr_bytes <= 0))
2254 break;
2255 }
2256 }
2257
2258 /*
2259 * completely done
2260 */
Tejun Heo2e60e022009-04-23 11:05:18 +09002261 if (!req->bio) {
2262 /*
2263 * Reset counters so that the request stacking driver
2264 * can find how many bytes remain in the request
2265 * later.
2266 */
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002267 req->__data_len = 0;
Tejun Heo2e60e022009-04-23 11:05:18 +09002268 return false;
2269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
2271 /*
2272 * if the request wasn't completed, update state
2273 */
2274 if (bio_nbytes) {
NeilBrown5bb23a62007-09-27 12:46:13 +02002275 req_bio_endio(req, bio, bio_nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 bio->bi_idx += next_idx;
2277 bio_iovec(bio)->bv_offset += nr_bytes;
2278 bio_iovec(bio)->bv_len -= nr_bytes;
2279 }
2280
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002281 req->__data_len -= total_bytes;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002282 req->buffer = bio_data(req->bio);
2283
2284 /* update sector only for requests with clear definition of sector */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002285 if (req->cmd_type == REQ_TYPE_FS || (req->cmd_flags & REQ_DISCARD))
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002286 req->__sector += total_bytes >> 9;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002287
Tejun Heo80a761f2009-07-03 17:48:17 +09002288 /* mixed attributes always follow the first bio */
2289 if (req->cmd_flags & REQ_MIXED_MERGE) {
2290 req->cmd_flags &= ~REQ_FAILFAST_MASK;
2291 req->cmd_flags |= req->bio->bi_rw & REQ_FAILFAST_MASK;
2292 }
2293
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002294 /*
2295 * If total number of sectors is less than the first segment
2296 * size, something has gone terribly wrong.
2297 */
2298 if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
Jens Axboe81829242011-03-30 09:51:33 +02002299 blk_dump_rq_flags(req, "request botched");
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002300 req->__data_len = blk_rq_cur_bytes(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002301 }
2302
2303 /* recalculate the number of segments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 blk_recalc_rq_segments(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002305
Tejun Heo2e60e022009-04-23 11:05:18 +09002306 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307}
Tejun Heo2e60e022009-04-23 11:05:18 +09002308EXPORT_SYMBOL_GPL(blk_update_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
Tejun Heo2e60e022009-04-23 11:05:18 +09002310static bool blk_update_bidi_request(struct request *rq, int error,
2311 unsigned int nr_bytes,
2312 unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002313{
Tejun Heo2e60e022009-04-23 11:05:18 +09002314 if (blk_update_request(rq, error, nr_bytes))
2315 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002316
Tejun Heo2e60e022009-04-23 11:05:18 +09002317 /* Bidi request must be completed as a whole */
2318 if (unlikely(blk_bidi_rq(rq)) &&
2319 blk_update_request(rq->next_rq, error, bidi_bytes))
2320 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002321
Jens Axboee2e1a142010-06-09 10:42:09 +02002322 if (blk_queue_add_random(rq->q))
2323 add_disk_randomness(rq->rq_disk);
Tejun Heo2e60e022009-04-23 11:05:18 +09002324
2325 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326}
2327
James Bottomley28018c22010-07-01 19:49:17 +09002328/**
2329 * blk_unprep_request - unprepare a request
2330 * @req: the request
2331 *
2332 * This function makes a request ready for complete resubmission (or
2333 * completion). It happens only after all error handling is complete,
2334 * so represents the appropriate moment to deallocate any resources
2335 * that were allocated to the request in the prep_rq_fn. The queue
2336 * lock is held when calling this.
2337 */
2338void blk_unprep_request(struct request *req)
2339{
2340 struct request_queue *q = req->q;
2341
2342 req->cmd_flags &= ~REQ_DONTPREP;
2343 if (q->unprep_rq_fn)
2344 q->unprep_rq_fn(q, req);
2345}
2346EXPORT_SYMBOL_GPL(blk_unprep_request);
2347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348/*
2349 * queue lock must be held
2350 */
Tejun Heo2e60e022009-04-23 11:05:18 +09002351static void blk_finish_request(struct request *req, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352{
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002353 if (blk_rq_tagged(req))
2354 blk_queue_end_tag(req->q, req);
2355
James Bottomleyba396a62009-05-27 14:17:08 +02002356 BUG_ON(blk_queued_rq(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002358 if (unlikely(laptop_mode) && req->cmd_type == REQ_TYPE_FS)
Matthew Garrett31373d02010-04-06 14:25:14 +02002359 laptop_io_completion(&req->q->backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Mike Andersone78042e2008-10-30 02:16:20 -07002361 blk_delete_timer(req);
2362
James Bottomley28018c22010-07-01 19:49:17 +09002363 if (req->cmd_flags & REQ_DONTPREP)
2364 blk_unprep_request(req);
2365
2366
Jens Axboebc58ba92009-01-23 10:54:44 +01002367 blk_account_io_done(req);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002368
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 if (req->end_io)
Tejun Heo8ffdc652006-01-06 09:49:03 +01002370 req->end_io(req, error);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002371 else {
2372 if (blk_bidi_rq(req))
2373 __blk_put_request(req->next_rq->q, req->next_rq);
2374
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 __blk_put_request(req->q, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 }
2377}
2378
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05002379/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002380 * blk_end_bidi_request - Complete a bidi request
2381 * @rq: the request to complete
Randy Dunlap710027a2008-08-19 20:13:11 +02002382 * @error: %0 for success, < %0 for error
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002383 * @nr_bytes: number of bytes to complete @rq
2384 * @bidi_bytes: number of bytes to complete @rq->next_rq
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002385 *
2386 * Description:
2387 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
Tejun Heo2e60e022009-04-23 11:05:18 +09002388 * Drivers that supports bidi can safely call this member for any
2389 * type of request, bidi or uni. In the later case @bidi_bytes is
2390 * just ignored.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002391 *
2392 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002393 * %false - we are done with this request
2394 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002395 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002396static bool blk_end_bidi_request(struct request *rq, int error,
2397 unsigned int nr_bytes, unsigned int bidi_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002398{
2399 struct request_queue *q = rq->q;
Tejun Heo2e60e022009-04-23 11:05:18 +09002400 unsigned long flags;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002401
Tejun Heo2e60e022009-04-23 11:05:18 +09002402 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2403 return true;
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002404
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002405 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo2e60e022009-04-23 11:05:18 +09002406 blk_finish_request(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002407 spin_unlock_irqrestore(q->queue_lock, flags);
2408
Tejun Heo2e60e022009-04-23 11:05:18 +09002409 return false;
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002410}
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002411
2412/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002413 * __blk_end_bidi_request - Complete a bidi request with queue lock held
2414 * @rq: the request to complete
2415 * @error: %0 for success, < %0 for error
2416 * @nr_bytes: number of bytes to complete @rq
2417 * @bidi_bytes: number of bytes to complete @rq->next_rq
Tejun Heo5efccd12009-04-23 11:05:18 +09002418 *
2419 * Description:
Tejun Heo2e60e022009-04-23 11:05:18 +09002420 * Identical to blk_end_bidi_request() except that queue lock is
2421 * assumed to be locked on entry and remains so on return.
Tejun Heo5efccd12009-04-23 11:05:18 +09002422 *
Tejun Heo2e60e022009-04-23 11:05:18 +09002423 * Return:
2424 * %false - we are done with this request
2425 * %true - still buffers pending for this request
Tejun Heo5efccd12009-04-23 11:05:18 +09002426 **/
Jeff Moyer4853aba2011-08-15 21:37:25 +02002427bool __blk_end_bidi_request(struct request *rq, int error,
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002428 unsigned int nr_bytes, unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002429{
Tejun Heo2e60e022009-04-23 11:05:18 +09002430 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2431 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002432
Tejun Heo2e60e022009-04-23 11:05:18 +09002433 blk_finish_request(rq, error);
Tejun Heo5efccd12009-04-23 11:05:18 +09002434
Tejun Heo2e60e022009-04-23 11:05:18 +09002435 return false;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002436}
2437
2438/**
2439 * blk_end_request - Helper function for drivers to complete the request.
2440 * @rq: the request being processed
2441 * @error: %0 for success, < %0 for error
2442 * @nr_bytes: number of bytes to complete
2443 *
2444 * Description:
2445 * Ends I/O on a number of bytes attached to @rq.
2446 * If @rq has leftover, sets it up for the next range of segments.
2447 *
2448 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002449 * %false - we are done with this request
2450 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002451 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002452bool blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002453{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002454 return blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002455}
Jens Axboe56ad1742009-07-28 22:11:24 +02002456EXPORT_SYMBOL(blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002457
2458/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002459 * blk_end_request_all - Helper function for drives to finish the request.
2460 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002461 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002462 *
2463 * Description:
2464 * Completely finish @rq.
2465 */
2466void blk_end_request_all(struct request *rq, int error)
2467{
2468 bool pending;
2469 unsigned int bidi_bytes = 0;
2470
2471 if (unlikely(blk_bidi_rq(rq)))
2472 bidi_bytes = blk_rq_bytes(rq->next_rq);
2473
2474 pending = blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2475 BUG_ON(pending);
2476}
Jens Axboe56ad1742009-07-28 22:11:24 +02002477EXPORT_SYMBOL(blk_end_request_all);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002478
2479/**
2480 * blk_end_request_cur - Helper function to finish the current request chunk.
2481 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002482 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002483 *
2484 * Description:
2485 * Complete the current consecutively mapped chunk from @rq.
2486 *
2487 * Return:
2488 * %false - we are done with this request
2489 * %true - still buffers pending for this request
2490 */
2491bool blk_end_request_cur(struct request *rq, int error)
2492{
2493 return blk_end_request(rq, error, blk_rq_cur_bytes(rq));
2494}
Jens Axboe56ad1742009-07-28 22:11:24 +02002495EXPORT_SYMBOL(blk_end_request_cur);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002496
2497/**
Tejun Heo80a761f2009-07-03 17:48:17 +09002498 * blk_end_request_err - Finish a request till the next failure boundary.
2499 * @rq: the request to finish till the next failure boundary for
2500 * @error: must be negative errno
2501 *
2502 * Description:
2503 * Complete @rq till the next failure boundary.
2504 *
2505 * Return:
2506 * %false - we are done with this request
2507 * %true - still buffers pending for this request
2508 */
2509bool blk_end_request_err(struct request *rq, int error)
2510{
2511 WARN_ON(error >= 0);
2512 return blk_end_request(rq, error, blk_rq_err_bytes(rq));
2513}
2514EXPORT_SYMBOL_GPL(blk_end_request_err);
2515
2516/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002517 * __blk_end_request - Helper function for drivers to complete the request.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002518 * @rq: the request being processed
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002519 * @error: %0 for success, < %0 for error
2520 * @nr_bytes: number of bytes to complete
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002521 *
2522 * Description:
2523 * Must be called with queue lock held unlike blk_end_request().
2524 *
2525 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002526 * %false - we are done with this request
2527 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002528 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002529bool __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002530{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002531 return __blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002532}
Jens Axboe56ad1742009-07-28 22:11:24 +02002533EXPORT_SYMBOL(__blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002534
2535/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002536 * __blk_end_request_all - Helper function for drives to finish the request.
2537 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002538 * @error: %0 for success, < %0 for error
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002539 *
2540 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002541 * Completely finish @rq. Must be called with queue lock held.
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002542 */
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002543void __blk_end_request_all(struct request *rq, int error)
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002544{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002545 bool pending;
2546 unsigned int bidi_bytes = 0;
2547
2548 if (unlikely(blk_bidi_rq(rq)))
2549 bidi_bytes = blk_rq_bytes(rq->next_rq);
2550
2551 pending = __blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2552 BUG_ON(pending);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002553}
Jens Axboe56ad1742009-07-28 22:11:24 +02002554EXPORT_SYMBOL(__blk_end_request_all);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002555
2556/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002557 * __blk_end_request_cur - Helper function to finish the current request chunk.
2558 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002559 * @error: %0 for success, < %0 for error
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002560 *
2561 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002562 * Complete the current consecutively mapped chunk from @rq. Must
2563 * be called with queue lock held.
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002564 *
2565 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002566 * %false - we are done with this request
2567 * %true - still buffers pending for this request
2568 */
2569bool __blk_end_request_cur(struct request *rq, int error)
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002570{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002571 return __blk_end_request(rq, error, blk_rq_cur_bytes(rq));
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002572}
Jens Axboe56ad1742009-07-28 22:11:24 +02002573EXPORT_SYMBOL(__blk_end_request_cur);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002574
Tejun Heo80a761f2009-07-03 17:48:17 +09002575/**
2576 * __blk_end_request_err - Finish a request till the next failure boundary.
2577 * @rq: the request to finish till the next failure boundary for
2578 * @error: must be negative errno
2579 *
2580 * Description:
2581 * Complete @rq till the next failure boundary. Must be called
2582 * with queue lock held.
2583 *
2584 * Return:
2585 * %false - we are done with this request
2586 * %true - still buffers pending for this request
2587 */
2588bool __blk_end_request_err(struct request *rq, int error)
2589{
2590 WARN_ON(error >= 0);
2591 return __blk_end_request(rq, error, blk_rq_err_bytes(rq));
2592}
2593EXPORT_SYMBOL_GPL(__blk_end_request_err);
2594
Jens Axboe86db1e22008-01-29 14:53:40 +01002595void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2596 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597{
Tejun Heoa82afdf2009-07-03 17:48:16 +09002598 /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002599 rq->cmd_flags |= bio->bi_rw & REQ_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
David Woodhousefb2dce82008-08-05 18:01:53 +01002601 if (bio_has_data(bio)) {
2602 rq->nr_phys_segments = bio_phys_segments(q, bio);
David Woodhousefb2dce82008-08-05 18:01:53 +01002603 rq->buffer = bio_data(bio);
2604 }
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002605 rq->__data_len = bio->bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 rq->bio = rq->biotail = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
NeilBrown66846572007-08-16 13:31:28 +02002608 if (bio->bi_bdev)
2609 rq->rq_disk = bio->bi_bdev->bd_disk;
2610}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
Ilya Loginov2d4dc892009-11-26 09:16:19 +01002612#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
2613/**
2614 * rq_flush_dcache_pages - Helper function to flush all pages in a request
2615 * @rq: the request to be flushed
2616 *
2617 * Description:
2618 * Flush all pages in @rq.
2619 */
2620void rq_flush_dcache_pages(struct request *rq)
2621{
2622 struct req_iterator iter;
2623 struct bio_vec *bvec;
2624
2625 rq_for_each_segment(bvec, rq, iter)
2626 flush_dcache_page(bvec->bv_page);
2627}
2628EXPORT_SYMBOL_GPL(rq_flush_dcache_pages);
2629#endif
2630
Kiyoshi Uedaef9e3fa2008-10-01 16:12:15 +02002631/**
2632 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
2633 * @q : the queue of the device being checked
2634 *
2635 * Description:
2636 * Check if underlying low-level drivers of a device are busy.
2637 * If the drivers want to export their busy state, they must set own
2638 * exporting function using blk_queue_lld_busy() first.
2639 *
2640 * Basically, this function is used only by request stacking drivers
2641 * to stop dispatching requests to underlying devices when underlying
2642 * devices are busy. This behavior helps more I/O merging on the queue
2643 * of the request stacking driver and prevents I/O throughput regression
2644 * on burst I/O load.
2645 *
2646 * Return:
2647 * 0 - Not busy (The request stacking driver should dispatch request)
2648 * 1 - Busy (The request stacking driver should stop dispatching request)
2649 */
2650int blk_lld_busy(struct request_queue *q)
2651{
2652 if (q->lld_busy_fn)
2653 return q->lld_busy_fn(q);
2654
2655 return 0;
2656}
2657EXPORT_SYMBOL_GPL(blk_lld_busy);
2658
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002659/**
2660 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
2661 * @rq: the clone request to be cleaned up
2662 *
2663 * Description:
2664 * Free all bios in @rq for a cloned request.
2665 */
2666void blk_rq_unprep_clone(struct request *rq)
2667{
2668 struct bio *bio;
2669
2670 while ((bio = rq->bio) != NULL) {
2671 rq->bio = bio->bi_next;
2672
2673 bio_put(bio);
2674 }
2675}
2676EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
2677
2678/*
2679 * Copy attributes of the original request to the clone request.
2680 * The actual data parts (e.g. ->cmd, ->buffer, ->sense) are not copied.
2681 */
2682static void __blk_rq_prep_clone(struct request *dst, struct request *src)
2683{
2684 dst->cpu = src->cpu;
Tejun Heo3a2edd02010-09-03 11:56:18 +02002685 dst->cmd_flags = (src->cmd_flags & REQ_CLONE_MASK) | REQ_NOMERGE;
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002686 dst->cmd_type = src->cmd_type;
2687 dst->__sector = blk_rq_pos(src);
2688 dst->__data_len = blk_rq_bytes(src);
2689 dst->nr_phys_segments = src->nr_phys_segments;
2690 dst->ioprio = src->ioprio;
2691 dst->extra_len = src->extra_len;
2692}
2693
2694/**
2695 * blk_rq_prep_clone - Helper function to setup clone request
2696 * @rq: the request to be setup
2697 * @rq_src: original request to be cloned
2698 * @bs: bio_set that bios for clone are allocated from
2699 * @gfp_mask: memory allocation mask for bio
2700 * @bio_ctr: setup function to be called for each clone bio.
2701 * Returns %0 for success, non %0 for failure.
2702 * @data: private data to be passed to @bio_ctr
2703 *
2704 * Description:
2705 * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
2706 * The actual data parts of @rq_src (e.g. ->cmd, ->buffer, ->sense)
2707 * are not copied, and copying such parts is the caller's responsibility.
2708 * Also, pages which the original bios are pointing to are not copied
2709 * and the cloned bios just point same pages.
2710 * So cloned bios must be completed before original bios, which means
2711 * the caller must complete @rq before @rq_src.
2712 */
2713int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
2714 struct bio_set *bs, gfp_t gfp_mask,
2715 int (*bio_ctr)(struct bio *, struct bio *, void *),
2716 void *data)
2717{
2718 struct bio *bio, *bio_src;
2719
2720 if (!bs)
2721 bs = fs_bio_set;
2722
2723 blk_rq_init(NULL, rq);
2724
2725 __rq_for_each_bio(bio_src, rq_src) {
2726 bio = bio_alloc_bioset(gfp_mask, bio_src->bi_max_vecs, bs);
2727 if (!bio)
2728 goto free_and_out;
2729
2730 __bio_clone(bio, bio_src);
2731
2732 if (bio_integrity(bio_src) &&
Martin K. Petersen7878cba2009-06-26 15:37:49 +02002733 bio_integrity_clone(bio, bio_src, gfp_mask, bs))
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002734 goto free_and_out;
2735
2736 if (bio_ctr && bio_ctr(bio, bio_src, data))
2737 goto free_and_out;
2738
2739 if (rq->bio) {
2740 rq->biotail->bi_next = bio;
2741 rq->biotail = bio;
2742 } else
2743 rq->bio = rq->biotail = bio;
2744 }
2745
2746 __blk_rq_prep_clone(rq, rq_src);
2747
2748 return 0;
2749
2750free_and_out:
2751 if (bio)
2752 bio_free(bio, bs);
2753 blk_rq_unprep_clone(rq);
2754
2755 return -ENOMEM;
2756}
2757EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
2758
Jens Axboe18887ad2008-07-28 13:08:45 +02002759int kblockd_schedule_work(struct request_queue *q, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760{
2761 return queue_work(kblockd_workqueue, work);
2762}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763EXPORT_SYMBOL(kblockd_schedule_work);
2764
Vivek Goyale43473b2010-09-15 17:06:35 -04002765int kblockd_schedule_delayed_work(struct request_queue *q,
2766 struct delayed_work *dwork, unsigned long delay)
2767{
2768 return queue_delayed_work(kblockd_workqueue, dwork, delay);
2769}
2770EXPORT_SYMBOL(kblockd_schedule_delayed_work);
2771
Jens Axboe73c10102011-03-08 13:19:51 +01002772#define PLUG_MAGIC 0x91827364
2773
Suresh Jayaraman75df7132011-09-21 10:00:16 +02002774/**
2775 * blk_start_plug - initialize blk_plug and track it inside the task_struct
2776 * @plug: The &struct blk_plug that needs to be initialized
2777 *
2778 * Description:
2779 * Tracking blk_plug inside the task_struct will help with auto-flushing the
2780 * pending I/O should the task end up blocking between blk_start_plug() and
2781 * blk_finish_plug(). This is important from a performance perspective, but
2782 * also ensures that we don't deadlock. For instance, if the task is blocking
2783 * for a memory allocation, memory reclaim could end up wanting to free a
2784 * page belonging to that request that is currently residing in our private
2785 * plug. By flushing the pending I/O when the process goes to sleep, we avoid
2786 * this kind of deadlock.
2787 */
Jens Axboe73c10102011-03-08 13:19:51 +01002788void blk_start_plug(struct blk_plug *plug)
2789{
2790 struct task_struct *tsk = current;
2791
2792 plug->magic = PLUG_MAGIC;
2793 INIT_LIST_HEAD(&plug->list);
NeilBrown048c9372011-04-18 09:52:22 +02002794 INIT_LIST_HEAD(&plug->cb_list);
Jens Axboe73c10102011-03-08 13:19:51 +01002795 plug->should_sort = 0;
2796
2797 /*
2798 * If this is a nested plug, don't actually assign it. It will be
2799 * flushed on its own.
2800 */
2801 if (!tsk->plug) {
2802 /*
2803 * Store ordering should not be needed here, since a potential
2804 * preempt will imply a full memory barrier
2805 */
2806 tsk->plug = plug;
2807 }
2808}
2809EXPORT_SYMBOL(blk_start_plug);
2810
2811static int plug_rq_cmp(void *priv, struct list_head *a, struct list_head *b)
2812{
2813 struct request *rqa = container_of(a, struct request, queuelist);
2814 struct request *rqb = container_of(b, struct request, queuelist);
2815
Konstantin Khlebnikovf83e8262011-04-04 00:15:02 +02002816 return !(rqa->q <= rqb->q);
Jens Axboe73c10102011-03-08 13:19:51 +01002817}
2818
Jens Axboe49cac012011-04-16 13:51:05 +02002819/*
2820 * If 'from_schedule' is true, then postpone the dispatch of requests
2821 * until a safe kblockd context. We due this to avoid accidental big
2822 * additional stack usage in driver dispatch, in places where the originally
2823 * plugger did not intend it.
2824 */
Jens Axboef6603782011-04-15 15:49:07 +02002825static void queue_unplugged(struct request_queue *q, unsigned int depth,
Jens Axboe49cac012011-04-16 13:51:05 +02002826 bool from_schedule)
Jens Axboe99e22592011-04-18 09:59:55 +02002827 __releases(q->queue_lock)
Jens Axboe94b5eb22011-04-12 10:12:19 +02002828{
Jens Axboe49cac012011-04-16 13:51:05 +02002829 trace_block_unplug(q, depth, !from_schedule);
Jens Axboe99e22592011-04-18 09:59:55 +02002830
2831 /*
Tejun Heo8ba61432011-12-14 00:33:37 +01002832 * Don't mess with dead queue.
2833 */
2834 if (unlikely(blk_queue_dead(q))) {
2835 spin_unlock(q->queue_lock);
2836 return;
2837 }
2838
2839 /*
Jens Axboe99e22592011-04-18 09:59:55 +02002840 * If we are punting this to kblockd, then we can safely drop
2841 * the queue_lock before waking kblockd (which needs to take
2842 * this lock).
2843 */
2844 if (from_schedule) {
2845 spin_unlock(q->queue_lock);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02002846 blk_run_queue_async(q);
Jens Axboe99e22592011-04-18 09:59:55 +02002847 } else {
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02002848 __blk_run_queue(q);
Jens Axboe99e22592011-04-18 09:59:55 +02002849 spin_unlock(q->queue_lock);
2850 }
2851
Jens Axboe94b5eb22011-04-12 10:12:19 +02002852}
2853
NeilBrown048c9372011-04-18 09:52:22 +02002854static void flush_plug_callbacks(struct blk_plug *plug)
2855{
2856 LIST_HEAD(callbacks);
2857
2858 if (list_empty(&plug->cb_list))
2859 return;
2860
2861 list_splice_init(&plug->cb_list, &callbacks);
2862
2863 while (!list_empty(&callbacks)) {
2864 struct blk_plug_cb *cb = list_first_entry(&callbacks,
2865 struct blk_plug_cb,
2866 list);
2867 list_del(&cb->list);
2868 cb->callback(cb);
2869 }
2870}
2871
Jens Axboe49cac012011-04-16 13:51:05 +02002872void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
Jens Axboe73c10102011-03-08 13:19:51 +01002873{
2874 struct request_queue *q;
2875 unsigned long flags;
2876 struct request *rq;
NeilBrown109b8122011-04-11 14:13:10 +02002877 LIST_HEAD(list);
Jens Axboe94b5eb22011-04-12 10:12:19 +02002878 unsigned int depth;
Jens Axboe73c10102011-03-08 13:19:51 +01002879
2880 BUG_ON(plug->magic != PLUG_MAGIC);
2881
NeilBrown048c9372011-04-18 09:52:22 +02002882 flush_plug_callbacks(plug);
Jens Axboe73c10102011-03-08 13:19:51 +01002883 if (list_empty(&plug->list))
2884 return;
2885
NeilBrown109b8122011-04-11 14:13:10 +02002886 list_splice_init(&plug->list, &list);
2887
2888 if (plug->should_sort) {
2889 list_sort(NULL, &list, plug_rq_cmp);
2890 plug->should_sort = 0;
2891 }
Jens Axboe73c10102011-03-08 13:19:51 +01002892
2893 q = NULL;
Jens Axboe94b5eb22011-04-12 10:12:19 +02002894 depth = 0;
Jens Axboe18811272011-04-12 10:11:24 +02002895
2896 /*
2897 * Save and disable interrupts here, to avoid doing it for every
2898 * queue lock we have to take.
2899 */
Jens Axboe73c10102011-03-08 13:19:51 +01002900 local_irq_save(flags);
NeilBrown109b8122011-04-11 14:13:10 +02002901 while (!list_empty(&list)) {
2902 rq = list_entry_rq(list.next);
Jens Axboe73c10102011-03-08 13:19:51 +01002903 list_del_init(&rq->queuelist);
Jens Axboe73c10102011-03-08 13:19:51 +01002904 BUG_ON(!rq->q);
2905 if (rq->q != q) {
Jens Axboe99e22592011-04-18 09:59:55 +02002906 /*
2907 * This drops the queue lock
2908 */
2909 if (q)
Jens Axboe49cac012011-04-16 13:51:05 +02002910 queue_unplugged(q, depth, from_schedule);
Jens Axboe73c10102011-03-08 13:19:51 +01002911 q = rq->q;
Jens Axboe94b5eb22011-04-12 10:12:19 +02002912 depth = 0;
Jens Axboe73c10102011-03-08 13:19:51 +01002913 spin_lock(q->queue_lock);
2914 }
Tejun Heo8ba61432011-12-14 00:33:37 +01002915
2916 /*
2917 * Short-circuit if @q is dead
2918 */
2919 if (unlikely(blk_queue_dead(q))) {
2920 __blk_end_request_all(rq, -ENODEV);
2921 continue;
2922 }
2923
Jens Axboe73c10102011-03-08 13:19:51 +01002924 /*
2925 * rq is already accounted, so use raw insert
2926 */
Jens Axboe401a18e2011-03-25 16:57:52 +01002927 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA))
2928 __elv_add_request(q, rq, ELEVATOR_INSERT_FLUSH);
2929 else
2930 __elv_add_request(q, rq, ELEVATOR_INSERT_SORT_MERGE);
Jens Axboe94b5eb22011-04-12 10:12:19 +02002931
2932 depth++;
Jens Axboe73c10102011-03-08 13:19:51 +01002933 }
2934
Jens Axboe99e22592011-04-18 09:59:55 +02002935 /*
2936 * This drops the queue lock
2937 */
2938 if (q)
Jens Axboe49cac012011-04-16 13:51:05 +02002939 queue_unplugged(q, depth, from_schedule);
Jens Axboe73c10102011-03-08 13:19:51 +01002940
Jens Axboe73c10102011-03-08 13:19:51 +01002941 local_irq_restore(flags);
2942}
Jens Axboe73c10102011-03-08 13:19:51 +01002943
2944void blk_finish_plug(struct blk_plug *plug)
2945{
Jens Axboef6603782011-04-15 15:49:07 +02002946 blk_flush_plug_list(plug, false);
Christoph Hellwig88b996c2011-04-15 15:20:10 +02002947
2948 if (plug == current->plug)
2949 current->plug = NULL;
Jens Axboe73c10102011-03-08 13:19:51 +01002950}
2951EXPORT_SYMBOL(blk_finish_plug);
2952
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953int __init blk_dev_init(void)
2954{
Nikanth Karthikesan9eb55b02009-04-27 14:53:54 +02002955 BUILD_BUG_ON(__REQ_NR_BITS > 8 *
2956 sizeof(((struct request *)0)->cmd_flags));
2957
Tejun Heo89b90be2011-01-03 15:01:47 +01002958 /* used for unplugging and affects IO latency/throughput - HIGHPRI */
2959 kblockd_workqueue = alloc_workqueue("kblockd",
2960 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 if (!kblockd_workqueue)
2962 panic("Failed to create kblockd\n");
2963
2964 request_cachep = kmem_cache_create("blkdev_requests",
Paul Mundt20c2df82007-07-20 10:11:58 +09002965 sizeof(struct request), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966
Jens Axboe8324aa92008-01-29 14:51:59 +01002967 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02002968 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 return 0;
2971}