blob: 83a47fcf59460d543bc3ef1435bd52a5c3e4018f [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
551 INIT_LIST_HEAD(&q->blkg_list[0]);
552 INIT_LIST_HEAD(&q->blkg_list[1]);
553#endif
Tejun Heoae1b1532011-01-25 12:43:54 +0100554 INIT_LIST_HEAD(&q->flush_queue[0]);
555 INIT_LIST_HEAD(&q->flush_queue[1]);
556 INIT_LIST_HEAD(&q->flush_data_in_flight);
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500557 INIT_DELAYED_WORK(&q->delay_work, blk_delay_work);
Al Viro483f4af2006-03-18 18:34:37 -0500558
Jens Axboe8324aa92008-01-29 14:51:59 +0100559 kobject_init(&q->kobj, &blk_queue_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Al Viro483f4af2006-03-18 18:34:37 -0500561 mutex_init(&q->sysfs_lock);
Neil Browne7e72bf2008-05-14 16:05:54 -0700562 spin_lock_init(&q->__queue_lock);
Al Viro483f4af2006-03-18 18:34:37 -0500563
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500564 /*
565 * By default initialize queue_lock to internal lock and driver can
566 * override it later if need be.
567 */
568 q->queue_lock = &q->__queue_lock;
569
Tejun Heo5efd6112012-03-05 13:15:12 -0800570 if (blkcg_init_queue(q))
Tejun Heof51b8022012-03-05 13:15:05 -0800571 goto fail_id;
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return q;
Tejun Heoa73f7302011-12-14 00:33:37 +0100574
575fail_id:
576 ida_simple_remove(&blk_queue_ida, q->id);
577fail_q:
578 kmem_cache_free(blk_requestq_cachep, q);
579 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
Christoph Lameter19460892005-06-23 00:08:19 -0700581EXPORT_SYMBOL(blk_alloc_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583/**
584 * blk_init_queue - prepare a request queue for use with a block device
585 * @rfn: The function to be called to process requests that have been
586 * placed on the queue.
587 * @lock: Request queue spin lock
588 *
589 * Description:
590 * If a block device wishes to use the standard request handling procedures,
591 * which sorts requests and coalesces adjacent requests, then it must
592 * call blk_init_queue(). The function @rfn will be called when there
593 * are requests on the queue that need to be processed. If the device
594 * supports plugging, then @rfn may not be called immediately when requests
595 * are available on the queue, but may be called at some time later instead.
596 * Plugged queues are generally unplugged when a buffer belonging to one
597 * of the requests on the queue is needed, or due to memory pressure.
598 *
599 * @rfn is not required, or even expected, to remove all requests off the
600 * queue, but only as many as it can handle at a time. If it does leave
601 * requests on the queue, it is responsible for arranging that the requests
602 * get dealt with eventually.
603 *
604 * The queue spin lock must be held while manipulating the requests on the
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200605 * request queue; this lock will be taken also from interrupt context, so irq
606 * disabling is needed for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 *
Randy Dunlap710027a2008-08-19 20:13:11 +0200608 * Function returns a pointer to the initialized request queue, or %NULL if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 * it didn't succeed.
610 *
611 * Note:
612 * blk_init_queue() must be paired with a blk_cleanup_queue() call
613 * when the block device is deactivated (such as at module unload).
614 **/
Christoph Lameter19460892005-06-23 00:08:19 -0700615
Jens Axboe165125e2007-07-24 09:28:11 +0200616struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Christoph Lameter19460892005-06-23 00:08:19 -0700618 return blk_init_queue_node(rfn, lock, -1);
619}
620EXPORT_SYMBOL(blk_init_queue);
621
Jens Axboe165125e2007-07-24 09:28:11 +0200622struct request_queue *
Christoph Lameter19460892005-06-23 00:08:19 -0700623blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
624{
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600625 struct request_queue *uninit_q, *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600627 uninit_q = blk_alloc_queue_node(GFP_KERNEL, node_id);
628 if (!uninit_q)
629 return NULL;
630
Mike Snitzer51514122011-11-23 10:59:13 +0100631 q = blk_init_allocated_queue(uninit_q, rfn, lock);
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600632 if (!q)
633 blk_cleanup_queue(uninit_q);
634
635 return q;
Mike Snitzer01effb02010-05-11 08:57:42 +0200636}
637EXPORT_SYMBOL(blk_init_queue_node);
638
639struct request_queue *
640blk_init_allocated_queue(struct request_queue *q, request_fn_proc *rfn,
641 spinlock_t *lock)
642{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (!q)
644 return NULL;
645
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600646 if (blk_init_free_list(q))
Al Viro8669aaf2006-03-18 13:50:00 -0500647 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 q->request_fn = rfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 q->prep_rq_fn = NULL;
James Bottomley28018c22010-07-01 19:49:17 +0900651 q->unprep_rq_fn = NULL;
Jens Axboebc58ba92009-01-23 10:54:44 +0100652 q->queue_flags = QUEUE_FLAG_DEFAULT;
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500653
654 /* Override internal queue lock with supplied lock pointer */
655 if (lock)
656 q->queue_lock = lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Jens Axboef3b144a2009-03-06 08:48:33 +0100658 /*
659 * This also sets hw/phys segments, boundary and size
660 */
Jens Axboec20e8de2011-09-12 12:03:37 +0200661 blk_queue_make_request(q, blk_queue_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Alan Stern44ec9542007-02-20 11:01:57 -0500663 q->sg_reserved_size = INT_MAX;
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 /*
666 * all done
667 */
668 if (!elevator_init(q, NULL)) {
669 blk_queue_congestion_threshold(q);
670 return q;
671 }
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return NULL;
674}
Mike Snitzer51514122011-11-23 10:59:13 +0100675EXPORT_SYMBOL(blk_init_allocated_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Tejun Heo09ac46c2011-12-14 00:33:38 +0100677bool blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Tejun Heo34f60552011-12-14 00:33:37 +0100679 if (likely(!blk_queue_dead(q))) {
Tejun Heo09ac46c2011-12-14 00:33:38 +0100680 __blk_get_queue(q);
681 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
683
Tejun Heo09ac46c2011-12-14 00:33:38 +0100684 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
Jens Axboed86e0e82011-05-27 07:44:43 +0200686EXPORT_SYMBOL(blk_get_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Jens Axboe165125e2007-07-24 09:28:11 +0200688static inline void blk_free_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Tejun Heof1f8cc92011-12-14 00:33:42 +0100690 if (rq->cmd_flags & REQ_ELVPRIV) {
Tejun Heocb98fc82005-10-28 08:29:39 +0200691 elv_put_request(q, rq);
Tejun Heof1f8cc92011-12-14 00:33:42 +0100692 if (rq->elv.icq)
Tejun Heo11a31222012-02-07 07:51:30 +0100693 put_io_context(rq->elv.icq->ioc);
Tejun Heof1f8cc92011-12-14 00:33:42 +0100694 }
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 mempool_free(rq, q->rq.rq_pool);
697}
698
Jens Axboe1ea25ecb2006-07-18 22:24:11 +0200699static struct request *
Tejun Heof1f8cc92011-12-14 00:33:42 +0100700blk_alloc_request(struct request_queue *q, struct io_cq *icq,
701 unsigned int flags, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
703 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
704
705 if (!rq)
706 return NULL;
707
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200708 blk_rq_init(q, rq);
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200709
Jerome Marchand42dad762009-04-22 14:01:49 +0200710 rq->cmd_flags = flags | REQ_ALLOCED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Tejun Heof1f8cc92011-12-14 00:33:42 +0100712 if (flags & REQ_ELVPRIV) {
713 rq->elv.icq = icq;
714 if (unlikely(elv_set_request(q, rq, gfp_mask))) {
715 mempool_free(rq, q->rq.rq_pool);
716 return NULL;
717 }
718 /* @rq->elv.icq holds on to io_context until @rq is freed */
719 if (icq)
720 get_io_context(icq->ioc);
Tejun Heocb98fc82005-10-28 08:29:39 +0200721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Tejun Heocb98fc82005-10-28 08:29:39 +0200723 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
726/*
727 * ioc_batching returns true if the ioc is a valid batching request and
728 * should be given priority access to a request.
729 */
Jens Axboe165125e2007-07-24 09:28:11 +0200730static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
732 if (!ioc)
733 return 0;
734
735 /*
736 * Make sure the process is able to allocate at least 1 request
737 * even if the batch times out, otherwise we could theoretically
738 * lose wakeups.
739 */
740 return ioc->nr_batch_requests == q->nr_batching ||
741 (ioc->nr_batch_requests > 0
742 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
743}
744
745/*
746 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
747 * will cause the process to be a "batcher" on all queues in the system. This
748 * is the behaviour we want though - once it gets a wakeup it should be given
749 * a nice run.
750 */
Jens Axboe165125e2007-07-24 09:28:11 +0200751static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
753 if (!ioc || ioc_batching(q, ioc))
754 return;
755
756 ioc->nr_batch_requests = q->nr_batching;
757 ioc->last_waited = jiffies;
758}
759
Jens Axboe1faa16d2009-04-06 14:48:01 +0200760static void __freed_request(struct request_queue *q, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
762 struct request_list *rl = &q->rq;
763
Jens Axboe1faa16d2009-04-06 14:48:01 +0200764 if (rl->count[sync] < queue_congestion_off_threshold(q))
765 blk_clear_queue_congested(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Jens Axboe1faa16d2009-04-06 14:48:01 +0200767 if (rl->count[sync] + 1 <= q->nr_requests) {
768 if (waitqueue_active(&rl->wait[sync]))
769 wake_up(&rl->wait[sync]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Jens Axboe1faa16d2009-04-06 14:48:01 +0200771 blk_clear_queue_full(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
773}
774
775/*
776 * A request has just been released. Account for it, update the full and
777 * congestion status, wake up any waiters. Called under q->queue_lock.
778 */
Tejun Heo75eb6c32011-10-19 14:31:22 +0200779static void freed_request(struct request_queue *q, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
781 struct request_list *rl = &q->rq;
Tejun Heo75eb6c32011-10-19 14:31:22 +0200782 int sync = rw_is_sync(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Jens Axboe1faa16d2009-04-06 14:48:01 +0200784 rl->count[sync]--;
Tejun Heo75eb6c32011-10-19 14:31:22 +0200785 if (flags & REQ_ELVPRIV)
Tejun Heocb98fc82005-10-28 08:29:39 +0200786 rl->elvpriv--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Jens Axboe1faa16d2009-04-06 14:48:01 +0200788 __freed_request(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Jens Axboe1faa16d2009-04-06 14:48:01 +0200790 if (unlikely(rl->starved[sync ^ 1]))
791 __freed_request(q, sync ^ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794/*
Mike Snitzer9d5a4e92011-02-11 11:05:46 +0100795 * Determine if elevator data should be initialized when allocating the
796 * request associated with @bio.
797 */
798static bool blk_rq_should_init_elevator(struct bio *bio)
799{
800 if (!bio)
801 return true;
802
803 /*
804 * Flush requests do not use the elevator so skip initialization.
805 * This allows a request to share the flush and elevator data.
806 */
807 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA))
808 return false;
809
810 return true;
811}
812
Tejun Heoda8303c2011-10-19 14:33:05 +0200813/**
814 * get_request - get a free request
815 * @q: request_queue to allocate request from
816 * @rw_flags: RW and SYNC flags
817 * @bio: bio to allocate request for (can be %NULL)
818 * @gfp_mask: allocation mask
819 *
820 * Get a free request from @q. This function may fail under memory
821 * pressure or if @q is dead.
822 *
823 * Must be callled with @q->queue_lock held and,
824 * Returns %NULL on failure, with @q->queue_lock held.
825 * Returns !%NULL on success, with @q->queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 */
Jens Axboe165125e2007-07-24 09:28:11 +0200827static struct request *get_request(struct request_queue *q, int rw_flags,
Jens Axboe7749a8d2006-12-13 13:02:26 +0100828 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
830 struct request *rq = NULL;
831 struct request_list *rl = &q->rq;
Tejun Heof1f8cc92011-12-14 00:33:42 +0100832 struct elevator_type *et;
Tejun Heof2dbd762011-12-14 00:33:40 +0100833 struct io_context *ioc;
Tejun Heof1f8cc92011-12-14 00:33:42 +0100834 struct io_cq *icq = NULL;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200835 const bool is_sync = rw_is_sync(rw_flags) != 0;
Tejun Heof2dbd762011-12-14 00:33:40 +0100836 bool retried = false;
Tejun Heo75eb6c32011-10-19 14:31:22 +0200837 int may_queue;
Tejun Heof2dbd762011-12-14 00:33:40 +0100838retry:
Tejun Heof1f8cc92011-12-14 00:33:42 +0100839 et = q->elevator->type;
Tejun Heof2dbd762011-12-14 00:33:40 +0100840 ioc = current->io_context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Tejun Heo34f60552011-12-14 00:33:37 +0100842 if (unlikely(blk_queue_dead(q)))
Tejun Heoda8303c2011-10-19 14:33:05 +0200843 return NULL;
844
Jens Axboe7749a8d2006-12-13 13:02:26 +0100845 may_queue = elv_may_queue(q, rw_flags);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100846 if (may_queue == ELV_MQUEUE_NO)
847 goto rq_starved;
848
Jens Axboe1faa16d2009-04-06 14:48:01 +0200849 if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) {
850 if (rl->count[is_sync]+1 >= q->nr_requests) {
Tejun Heof2dbd762011-12-14 00:33:40 +0100851 /*
852 * We want ioc to record batching state. If it's
853 * not already there, creating a new one requires
854 * dropping queue_lock, which in turn requires
855 * retesting conditions to avoid queue hang.
856 */
857 if (!ioc && !retried) {
858 spin_unlock_irq(q->queue_lock);
859 create_io_context(current, gfp_mask, q->node);
860 spin_lock_irq(q->queue_lock);
861 retried = true;
862 goto retry;
863 }
864
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100865 /*
866 * The queue will fill after this allocation, so set
867 * it as full, and mark this process as "batching".
868 * This process will be allowed to complete a batch of
869 * requests, others will be blocked.
870 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200871 if (!blk_queue_full(q, is_sync)) {
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100872 ioc_set_batching(q, ioc);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200873 blk_set_queue_full(q, is_sync);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100874 } else {
875 if (may_queue != ELV_MQUEUE_MUST
876 && !ioc_batching(q, ioc)) {
877 /*
878 * The queue is full and the allocating
879 * process is not a "batcher", and not
880 * exempted by the IO scheduler
881 */
882 goto out;
883 }
884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 }
Jens Axboe1faa16d2009-04-06 14:48:01 +0200886 blk_set_queue_congested(q, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
888
Jens Axboe082cf692005-06-28 16:35:11 +0200889 /*
890 * Only allow batching queuers to allocate up to 50% over the defined
891 * limit of requests, otherwise we could have thousands of requests
892 * allocated with any setting of ->nr_requests
893 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200894 if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
Jens Axboe082cf692005-06-28 16:35:11 +0200895 goto out;
Hugh Dickinsfd782a42005-06-29 15:15:40 +0100896
Jens Axboe1faa16d2009-04-06 14:48:01 +0200897 rl->count[is_sync]++;
898 rl->starved[is_sync] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200899
Tejun Heof1f8cc92011-12-14 00:33:42 +0100900 /*
901 * Decide whether the new request will be managed by elevator. If
902 * so, mark @rw_flags and increment elvpriv. Non-zero elvpriv will
903 * prevent the current elevator from being destroyed until the new
904 * request is freed. This guarantees icq's won't be destroyed and
905 * makes creating new ones safe.
906 *
907 * Also, lookup icq while holding queue_lock. If it doesn't exist,
908 * it will be created after releasing queue_lock.
909 */
Tejun Heod7325802012-03-05 13:14:58 -0800910 if (blk_rq_should_init_elevator(bio) && !blk_queue_bypass(q)) {
Tejun Heo75eb6c32011-10-19 14:31:22 +0200911 rw_flags |= REQ_ELVPRIV;
912 rl->elvpriv++;
Tejun Heof1f8cc92011-12-14 00:33:42 +0100913 if (et->icq_cache && ioc)
914 icq = ioc_lookup_icq(ioc, q);
Mike Snitzer9d5a4e92011-02-11 11:05:46 +0100915 }
Tejun Heocb98fc82005-10-28 08:29:39 +0200916
Jens Axboef253b862010-10-24 22:06:02 +0200917 if (blk_queue_io_stat(q))
918 rw_flags |= REQ_IO_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 spin_unlock_irq(q->queue_lock);
920
Tejun Heof1f8cc92011-12-14 00:33:42 +0100921 /* create icq if missing */
Shaohua Li05c30b92012-01-19 09:20:10 +0100922 if ((rw_flags & REQ_ELVPRIV) && unlikely(et->icq_cache && !icq)) {
Tejun Heof1f8cc92011-12-14 00:33:42 +0100923 icq = ioc_create_icq(q, gfp_mask);
Shaohua Li05c30b92012-01-19 09:20:10 +0100924 if (!icq)
925 goto fail_icq;
926 }
Tejun Heof1f8cc92011-12-14 00:33:42 +0100927
Shaohua Li05c30b92012-01-19 09:20:10 +0100928 rq = blk_alloc_request(q, icq, rw_flags, gfp_mask);
Tejun Heof1f8cc92011-12-14 00:33:42 +0100929
Shaohua Li05c30b92012-01-19 09:20:10 +0100930fail_icq:
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100931 if (unlikely(!rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 /*
933 * Allocation failed presumably due to memory. Undo anything
934 * we might have messed up.
935 *
936 * Allocating task should really be put onto the front of the
937 * wait queue, but this is pretty rare.
938 */
939 spin_lock_irq(q->queue_lock);
Tejun Heo75eb6c32011-10-19 14:31:22 +0200940 freed_request(q, rw_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 /*
943 * in the very unlikely event that allocation failed and no
944 * requests for this direction was pending, mark us starved
945 * so that freeing of a request in the other direction will
946 * notice us. another possible fix would be to split the
947 * rq mempool into READ and WRITE
948 */
949rq_starved:
Jens Axboe1faa16d2009-04-06 14:48:01 +0200950 if (unlikely(rl->count[is_sync] == 0))
951 rl->starved[is_sync] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 goto out;
954 }
955
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100956 /*
957 * ioc may be NULL here, and ioc_batching will be false. That's
958 * OK, if the queue is under the request limit then requests need
959 * not count toward the nr_batch_requests limit. There will always
960 * be some limit enforced by BLK_BATCH_TIME.
961 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 if (ioc_batching(q, ioc))
963 ioc->nr_batch_requests--;
Jens Axboe6728cb02008-01-31 13:03:55 +0100964
Jens Axboe1faa16d2009-04-06 14:48:01 +0200965 trace_block_getrq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return rq;
968}
969
Tejun Heoda8303c2011-10-19 14:33:05 +0200970/**
971 * get_request_wait - get a free request with retry
972 * @q: request_queue to allocate request from
973 * @rw_flags: RW and SYNC flags
974 * @bio: bio to allocate request for (can be %NULL)
Nick Piggind6344532005-06-28 20:45:14 -0700975 *
Tejun Heoda8303c2011-10-19 14:33:05 +0200976 * Get a free request from @q. This function keeps retrying under memory
977 * pressure and fails iff @q is dead.
978 *
979 * Must be callled with @q->queue_lock held and,
980 * Returns %NULL on failure, with @q->queue_lock held.
981 * Returns !%NULL on success, with @q->queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 */
Jens Axboe165125e2007-07-24 09:28:11 +0200983static struct request *get_request_wait(struct request_queue *q, int rw_flags,
Jens Axboe22e2c502005-06-27 10:55:12 +0200984 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Jens Axboe1faa16d2009-04-06 14:48:01 +0200986 const bool is_sync = rw_is_sync(rw_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 struct request *rq;
988
Jens Axboe7749a8d2006-12-13 13:02:26 +0100989 rq = get_request(q, rw_flags, bio, GFP_NOIO);
Nick Piggin450991b2005-06-28 20:45:13 -0700990 while (!rq) {
991 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 struct request_list *rl = &q->rq;
993
Tejun Heo34f60552011-12-14 00:33:37 +0100994 if (unlikely(blk_queue_dead(q)))
Tejun Heoda8303c2011-10-19 14:33:05 +0200995 return NULL;
996
Jens Axboe1faa16d2009-04-06 14:48:01 +0200997 prepare_to_wait_exclusive(&rl->wait[is_sync], &wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 TASK_UNINTERRUPTIBLE);
999
Jens Axboe1faa16d2009-04-06 14:48:01 +02001000 trace_block_sleeprq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +02001002 spin_unlock_irq(q->queue_lock);
1003 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +02001005 /*
1006 * After sleeping, we become a "batching" process and
1007 * will be able to allocate at least one request, and
1008 * up to a big batch of them for a small period time.
1009 * See ioc_batching, ioc_set_batching
1010 */
Tejun Heof2dbd762011-12-14 00:33:40 +01001011 create_io_context(current, GFP_NOIO, q->node);
1012 ioc_set_batching(q, current->io_context);
Jens Axboe2056a782006-03-23 20:00:26 +01001013
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +02001014 spin_lock_irq(q->queue_lock);
Jens Axboe1faa16d2009-04-06 14:48:01 +02001015 finish_wait(&rl->wait[is_sync], &wait);
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +02001016
1017 rq = get_request(q, rw_flags, bio, GFP_NOIO);
1018 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 return rq;
1021}
1022
Jens Axboe165125e2007-07-24 09:28:11 +02001023struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
1025 struct request *rq;
1026
1027 BUG_ON(rw != READ && rw != WRITE);
1028
Nick Piggind6344532005-06-28 20:45:14 -07001029 spin_lock_irq(q->queue_lock);
Tejun Heoda8303c2011-10-19 14:33:05 +02001030 if (gfp_mask & __GFP_WAIT)
Jens Axboe22e2c502005-06-27 10:55:12 +02001031 rq = get_request_wait(q, rw, NULL);
Tejun Heoda8303c2011-10-19 14:33:05 +02001032 else
Jens Axboe22e2c502005-06-27 10:55:12 +02001033 rq = get_request(q, rw, NULL, gfp_mask);
Tejun Heoda8303c2011-10-19 14:33:05 +02001034 if (!rq)
1035 spin_unlock_irq(q->queue_lock);
Nick Piggind6344532005-06-28 20:45:14 -07001036 /* q->queue_lock is unlocked at this point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 return rq;
1039}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040EXPORT_SYMBOL(blk_get_request);
1041
1042/**
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001043 * blk_make_request - given a bio, allocate a corresponding struct request.
Randy Dunlap8ebf9752009-06-11 20:00:41 -07001044 * @q: target request queue
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001045 * @bio: The bio describing the memory mappings that will be submitted for IO.
1046 * It may be a chained-bio properly constructed by block/bio layer.
Randy Dunlap8ebf9752009-06-11 20:00:41 -07001047 * @gfp_mask: gfp flags to be used for memory allocation
Jens Axboedc72ef42006-07-20 14:54:05 +02001048 *
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001049 * blk_make_request is the parallel of generic_make_request for BLOCK_PC
1050 * type commands. Where the struct request needs to be farther initialized by
1051 * the caller. It is passed a &struct bio, which describes the memory info of
1052 * the I/O transfer.
1053 *
1054 * The caller of blk_make_request must make sure that bi_io_vec
1055 * are set to describe the memory buffers. That bio_data_dir() will return
1056 * the needed direction of the request. (And all bio's in the passed bio-chain
1057 * are properly set accordingly)
1058 *
1059 * If called under none-sleepable conditions, mapped bio buffers must not
1060 * need bouncing, by calling the appropriate masked or flagged allocator,
1061 * suitable for the target device. Otherwise the call to blk_queue_bounce will
1062 * BUG.
Jens Axboe53674ac2009-05-19 19:52:35 +02001063 *
1064 * WARNING: When allocating/cloning a bio-chain, careful consideration should be
1065 * given to how you allocate bios. In particular, you cannot use __GFP_WAIT for
1066 * anything but the first bio in the chain. Otherwise you risk waiting for IO
1067 * completion of a bio that hasn't been submitted yet, thus resulting in a
1068 * deadlock. Alternatively bios should be allocated using bio_kmalloc() instead
1069 * of bio_alloc(), as that avoids the mempool deadlock.
1070 * If possible a big IO should be split into smaller parts when allocation
1071 * fails. Partial allocation should not be an error, or you risk a live-lock.
Jens Axboedc72ef42006-07-20 14:54:05 +02001072 */
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001073struct request *blk_make_request(struct request_queue *q, struct bio *bio,
1074 gfp_t gfp_mask)
Jens Axboedc72ef42006-07-20 14:54:05 +02001075{
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001076 struct request *rq = blk_get_request(q, bio_data_dir(bio), gfp_mask);
1077
1078 if (unlikely(!rq))
1079 return ERR_PTR(-ENOMEM);
1080
1081 for_each_bio(bio) {
1082 struct bio *bounce_bio = bio;
1083 int ret;
1084
1085 blk_queue_bounce(q, &bounce_bio);
1086 ret = blk_rq_append_bio(q, rq, bounce_bio);
1087 if (unlikely(ret)) {
1088 blk_put_request(rq);
1089 return ERR_PTR(ret);
1090 }
1091 }
1092
1093 return rq;
Jens Axboedc72ef42006-07-20 14:54:05 +02001094}
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001095EXPORT_SYMBOL(blk_make_request);
Jens Axboedc72ef42006-07-20 14:54:05 +02001096
1097/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 * blk_requeue_request - put a request back on queue
1099 * @q: request queue where request should be inserted
1100 * @rq: request to be inserted
1101 *
1102 * Description:
1103 * Drivers often keep queueing requests until the hardware cannot accept
1104 * more, when that condition happens we need to put the request back
1105 * on the queue. Must be called with queue lock held.
1106 */
Jens Axboe165125e2007-07-24 09:28:11 +02001107void blk_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
Jens Axboe242f9dc2008-09-14 05:55:09 -07001109 blk_delete_timer(rq);
1110 blk_clear_rq_complete(rq);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001111 trace_block_rq_requeue(q, rq);
Jens Axboe2056a782006-03-23 20:00:26 +01001112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (blk_rq_tagged(rq))
1114 blk_queue_end_tag(q, rq);
1115
James Bottomleyba396a62009-05-27 14:17:08 +02001116 BUG_ON(blk_queued_rq(rq));
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 elv_requeue_request(q, rq);
1119}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120EXPORT_SYMBOL(blk_requeue_request);
1121
Jens Axboe73c10102011-03-08 13:19:51 +01001122static void add_acct_request(struct request_queue *q, struct request *rq,
1123 int where)
1124{
1125 drive_stat_acct(rq, 1);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001126 __elv_add_request(q, rq, where);
Jens Axboe73c10102011-03-08 13:19:51 +01001127}
1128
Tejun Heo074a7ac2008-08-25 19:56:14 +09001129static void part_round_stats_single(int cpu, struct hd_struct *part,
1130 unsigned long now)
1131{
1132 if (now == part->stamp)
1133 return;
1134
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001135 if (part_in_flight(part)) {
Tejun Heo074a7ac2008-08-25 19:56:14 +09001136 __part_stat_add(cpu, part, time_in_queue,
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001137 part_in_flight(part) * (now - part->stamp));
Tejun Heo074a7ac2008-08-25 19:56:14 +09001138 __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
1139 }
1140 part->stamp = now;
1141}
1142
1143/**
Randy Dunlap496aa8a2008-10-16 07:46:23 +02001144 * part_round_stats() - Round off the performance stats on a struct disk_stats.
1145 * @cpu: cpu number for stats access
1146 * @part: target partition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 *
1148 * The average IO queue length and utilisation statistics are maintained
1149 * by observing the current state of the queue length and the amount of
1150 * time it has been in this state for.
1151 *
1152 * Normally, that accounting is done on IO completion, but that can result
1153 * in more than a second's worth of IO being accounted for within any one
1154 * second, leading to >100% utilisation. To deal with that, we call this
1155 * function to do a round-off before returning the results when reading
1156 * /proc/diskstats. This accounts immediately for all queue usage up to
1157 * the current jiffies and restarts the counters again.
1158 */
Tejun Heoc9959052008-08-25 19:47:21 +09001159void part_round_stats(int cpu, struct hd_struct *part)
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001160{
1161 unsigned long now = jiffies;
1162
Tejun Heo074a7ac2008-08-25 19:56:14 +09001163 if (part->partno)
1164 part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
1165 part_round_stats_single(cpu, part, now);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001166}
Tejun Heo074a7ac2008-08-25 19:56:14 +09001167EXPORT_SYMBOL_GPL(part_round_stats);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169/*
1170 * queue lock must be held
1171 */
Jens Axboe165125e2007-07-24 09:28:11 +02001172void __blk_put_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 if (unlikely(!q))
1175 return;
1176 if (unlikely(--req->ref_count))
1177 return;
1178
Tejun Heo8922e162005-10-20 16:23:44 +02001179 elv_completed_request(q, req);
1180
Boaz Harrosh1cd96c22009-03-24 12:35:07 +01001181 /* this is a bio leak */
1182 WARN_ON(req->bio != NULL);
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 /*
1185 * Request may not have originated from ll_rw_blk. if not,
1186 * it didn't come out of our reserved rq pools
1187 */
Jens Axboe49171e52006-08-10 08:59:11 +02001188 if (req->cmd_flags & REQ_ALLOCED) {
Tejun Heo75eb6c32011-10-19 14:31:22 +02001189 unsigned int flags = req->cmd_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 BUG_ON(!list_empty(&req->queuelist));
Jens Axboe98170642006-07-28 09:23:08 +02001192 BUG_ON(!hlist_unhashed(&req->hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194 blk_free_request(q, req);
Tejun Heo75eb6c32011-10-19 14:31:22 +02001195 freed_request(q, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 }
1197}
Mike Christie6e39b69e2005-11-11 05:30:24 -06001198EXPORT_SYMBOL_GPL(__blk_put_request);
1199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200void blk_put_request(struct request *req)
1201{
Tejun Heo8922e162005-10-20 16:23:44 +02001202 unsigned long flags;
Jens Axboe165125e2007-07-24 09:28:11 +02001203 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
FUJITA Tomonori52a93ba2008-07-15 21:21:45 +02001205 spin_lock_irqsave(q->queue_lock, flags);
1206 __blk_put_request(q, req);
1207 spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209EXPORT_SYMBOL(blk_put_request);
1210
Christoph Hellwig66ac0282010-06-18 16:59:42 +02001211/**
1212 * blk_add_request_payload - add a payload to a request
1213 * @rq: request to update
1214 * @page: page backing the payload
1215 * @len: length of the payload.
1216 *
1217 * This allows to later add a payload to an already submitted request by
1218 * a block driver. The driver needs to take care of freeing the payload
1219 * itself.
1220 *
1221 * Note that this is a quite horrible hack and nothing but handling of
1222 * discard requests should ever use it.
1223 */
1224void blk_add_request_payload(struct request *rq, struct page *page,
1225 unsigned int len)
1226{
1227 struct bio *bio = rq->bio;
1228
1229 bio->bi_io_vec->bv_page = page;
1230 bio->bi_io_vec->bv_offset = 0;
1231 bio->bi_io_vec->bv_len = len;
1232
1233 bio->bi_size = len;
1234 bio->bi_vcnt = 1;
1235 bio->bi_phys_segments = 1;
1236
1237 rq->__data_len = rq->resid_len = len;
1238 rq->nr_phys_segments = 1;
1239 rq->buffer = bio_data(bio);
1240}
1241EXPORT_SYMBOL_GPL(blk_add_request_payload);
1242
Jens Axboe73c10102011-03-08 13:19:51 +01001243static bool bio_attempt_back_merge(struct request_queue *q, struct request *req,
1244 struct bio *bio)
1245{
1246 const int ff = bio->bi_rw & REQ_FAILFAST_MASK;
1247
Jens Axboe73c10102011-03-08 13:19:51 +01001248 if (!ll_back_merge_fn(q, req, bio))
1249 return false;
1250
1251 trace_block_bio_backmerge(q, bio);
1252
1253 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1254 blk_rq_set_mixed_merge(req);
1255
1256 req->biotail->bi_next = bio;
1257 req->biotail = bio;
1258 req->__data_len += bio->bi_size;
1259 req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
1260
1261 drive_stat_acct(req, 0);
1262 return true;
1263}
1264
1265static bool bio_attempt_front_merge(struct request_queue *q,
1266 struct request *req, struct bio *bio)
1267{
1268 const int ff = bio->bi_rw & REQ_FAILFAST_MASK;
Jens Axboe73c10102011-03-08 13:19:51 +01001269
Jens Axboe73c10102011-03-08 13:19:51 +01001270 if (!ll_front_merge_fn(q, req, bio))
1271 return false;
1272
1273 trace_block_bio_frontmerge(q, bio);
1274
1275 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1276 blk_rq_set_mixed_merge(req);
1277
Jens Axboe73c10102011-03-08 13:19:51 +01001278 bio->bi_next = req->bio;
1279 req->bio = bio;
1280
1281 /*
1282 * may not be valid. if the low level driver said
1283 * it didn't need a bounce buffer then it better
1284 * not touch req->buffer either...
1285 */
1286 req->buffer = bio_data(bio);
1287 req->__sector = bio->bi_sector;
1288 req->__data_len += bio->bi_size;
1289 req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
1290
1291 drive_stat_acct(req, 0);
1292 return true;
1293}
1294
Tejun Heobd87b582011-10-19 14:33:08 +02001295/**
1296 * attempt_plug_merge - try to merge with %current's plugged list
1297 * @q: request_queue new bio is being queued at
1298 * @bio: new bio being queued
1299 * @request_count: out parameter for number of traversed plugged requests
1300 *
1301 * Determine whether @bio being queued on @q can be merged with a request
1302 * on %current's plugged list. Returns %true if merge was successful,
1303 * otherwise %false.
1304 *
Tejun Heo07c2bd32012-02-08 09:19:42 +01001305 * Plugging coalesces IOs from the same issuer for the same purpose without
1306 * going through @q->queue_lock. As such it's more of an issuing mechanism
1307 * than scheduling, and the request, while may have elvpriv data, is not
1308 * added on the elevator at this point. In addition, we don't have
1309 * reliable access to the elevator outside queue lock. Only check basic
1310 * merging parameters without querying the elevator.
Jens Axboe73c10102011-03-08 13:19:51 +01001311 */
Tejun Heobd87b582011-10-19 14:33:08 +02001312static bool attempt_plug_merge(struct request_queue *q, struct bio *bio,
1313 unsigned int *request_count)
Jens Axboe73c10102011-03-08 13:19:51 +01001314{
1315 struct blk_plug *plug;
1316 struct request *rq;
1317 bool ret = false;
1318
Tejun Heobd87b582011-10-19 14:33:08 +02001319 plug = current->plug;
Jens Axboe73c10102011-03-08 13:19:51 +01001320 if (!plug)
1321 goto out;
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001322 *request_count = 0;
Jens Axboe73c10102011-03-08 13:19:51 +01001323
1324 list_for_each_entry_reverse(rq, &plug->list, queuelist) {
1325 int el_ret;
1326
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001327 (*request_count)++;
1328
Tejun Heo07c2bd32012-02-08 09:19:42 +01001329 if (rq->q != q || !blk_rq_merge_ok(rq, bio))
Jens Axboe73c10102011-03-08 13:19:51 +01001330 continue;
1331
Tejun Heo050c8ea2012-02-08 09:19:38 +01001332 el_ret = blk_try_merge(rq, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001333 if (el_ret == ELEVATOR_BACK_MERGE) {
1334 ret = bio_attempt_back_merge(q, rq, bio);
1335 if (ret)
1336 break;
1337 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
1338 ret = bio_attempt_front_merge(q, rq, bio);
1339 if (ret)
1340 break;
1341 }
1342 }
1343out:
1344 return ret;
1345}
1346
Jens Axboe86db1e22008-01-29 14:53:40 +01001347void init_request_from_bio(struct request *req, struct bio *bio)
Tejun Heo52d9e672006-01-06 09:49:58 +01001348{
Jens Axboe4aff5e22006-08-10 08:44:47 +02001349 req->cmd_type = REQ_TYPE_FS;
Tejun Heo52d9e672006-01-06 09:49:58 +01001350
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001351 req->cmd_flags |= bio->bi_rw & REQ_COMMON_MASK;
1352 if (bio->bi_rw & REQ_RAHEAD)
Tejun Heoa82afdf2009-07-03 17:48:16 +09001353 req->cmd_flags |= REQ_FAILFAST_MASK;
Jens Axboeb31dc662006-06-13 08:26:10 +02001354
Tejun Heo52d9e672006-01-06 09:49:58 +01001355 req->errors = 0;
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001356 req->__sector = bio->bi_sector;
Tejun Heo52d9e672006-01-06 09:49:58 +01001357 req->ioprio = bio_prio(bio);
NeilBrownbc1c56f2007-08-16 13:31:30 +02001358 blk_rq_bio_prep(req->q, req, bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001359}
1360
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001361void blk_queue_bio(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
Jiri Slaby5e00d1b2010-08-12 14:31:06 +02001363 const bool sync = !!(bio->bi_rw & REQ_SYNC);
Jens Axboe73c10102011-03-08 13:19:51 +01001364 struct blk_plug *plug;
1365 int el_ret, rw_flags, where = ELEVATOR_INSERT_SORT;
1366 struct request *req;
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001367 unsigned int request_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 /*
1370 * low level driver can indicate that it wants pages above a
1371 * certain limit bounced to low memory (ie for highmem, or even
1372 * ISA dma in theory)
1373 */
1374 blk_queue_bounce(q, &bio);
1375
Tejun Heo4fed9472010-09-03 11:56:17 +02001376 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) {
Jens Axboe73c10102011-03-08 13:19:51 +01001377 spin_lock_irq(q->queue_lock);
Tejun Heoae1b1532011-01-25 12:43:54 +01001378 where = ELEVATOR_INSERT_FLUSH;
Tejun Heo28e7d182010-09-03 11:56:16 +02001379 goto get_rq;
1380 }
1381
Jens Axboe73c10102011-03-08 13:19:51 +01001382 /*
1383 * Check if we can merge with the plugged list before grabbing
1384 * any locks.
1385 */
Tejun Heobd87b582011-10-19 14:33:08 +02001386 if (attempt_plug_merge(q, bio, &request_count))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001387 return;
Jens Axboe73c10102011-03-08 13:19:51 +01001388
1389 spin_lock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 el_ret = elv_merge(q, &req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001392 if (el_ret == ELEVATOR_BACK_MERGE) {
Jens Axboe73c10102011-03-08 13:19:51 +01001393 if (bio_attempt_back_merge(q, req, bio)) {
Tejun Heo07c2bd32012-02-08 09:19:42 +01001394 elv_bio_merged(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001395 if (!attempt_back_merge(q, req))
1396 elv_merged_request(q, req, el_ret);
1397 goto out_unlock;
Tejun Heo80a761f2009-07-03 17:48:17 +09001398 }
Jens Axboe73c10102011-03-08 13:19:51 +01001399 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
Jens Axboe73c10102011-03-08 13:19:51 +01001400 if (bio_attempt_front_merge(q, req, bio)) {
Tejun Heo07c2bd32012-02-08 09:19:42 +01001401 elv_bio_merged(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001402 if (!attempt_front_merge(q, req))
1403 elv_merged_request(q, req, el_ret);
1404 goto out_unlock;
1405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
1407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408get_rq:
Nick Piggin450991b2005-06-28 20:45:13 -07001409 /*
Jens Axboe7749a8d2006-12-13 13:02:26 +01001410 * This sync check and mask will be re-done in init_request_from_bio(),
1411 * but we need to set it earlier to expose the sync flag to the
1412 * rq allocator and io schedulers.
1413 */
1414 rw_flags = bio_data_dir(bio);
1415 if (sync)
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001416 rw_flags |= REQ_SYNC;
Jens Axboe7749a8d2006-12-13 13:02:26 +01001417
1418 /*
Nick Piggin450991b2005-06-28 20:45:13 -07001419 * Grab a free request. This is might sleep but can not fail.
Nick Piggind6344532005-06-28 20:45:14 -07001420 * Returns with the queue unlocked.
Nick Piggin450991b2005-06-28 20:45:13 -07001421 */
Jens Axboe7749a8d2006-12-13 13:02:26 +01001422 req = get_request_wait(q, rw_flags, bio);
Tejun Heoda8303c2011-10-19 14:33:05 +02001423 if (unlikely(!req)) {
1424 bio_endio(bio, -ENODEV); /* @q is dead */
1425 goto out_unlock;
1426 }
Nick Piggind6344532005-06-28 20:45:14 -07001427
Nick Piggin450991b2005-06-28 20:45:13 -07001428 /*
1429 * After dropping the lock and possibly sleeping here, our request
1430 * may now be mergeable after it had proven unmergeable (above).
1431 * We don't worry about that case for efficiency. It won't happen
1432 * often, and the elevators are able to handle it.
1433 */
Tejun Heo52d9e672006-01-06 09:49:58 +01001434 init_request_from_bio(req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Tao Ma9562ad92011-10-24 16:11:30 +02001436 if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags))
Jens Axboe11ccf112011-07-26 15:01:15 +02001437 req->cpu = raw_smp_processor_id();
Tejun Heodd831002010-09-03 11:56:16 +02001438
Jens Axboe73c10102011-03-08 13:19:51 +01001439 plug = current->plug;
Jens Axboe721a9602011-03-09 11:56:30 +01001440 if (plug) {
Jens Axboedc6d36c2011-04-12 10:28:28 +02001441 /*
1442 * If this is the first request added after a plug, fire
1443 * of a plug trace. If others have been added before, check
1444 * if we have multiple devices in this plug. If so, make a
1445 * note to sort the list before dispatch.
1446 */
1447 if (list_empty(&plug->list))
1448 trace_block_plug(q);
Shaohua Li3540d5e2011-11-16 09:21:50 +01001449 else {
1450 if (!plug->should_sort) {
1451 struct request *__rq;
Jens Axboe73c10102011-03-08 13:19:51 +01001452
Shaohua Li3540d5e2011-11-16 09:21:50 +01001453 __rq = list_entry_rq(plug->list.prev);
1454 if (__rq->q != q)
1455 plug->should_sort = 1;
1456 }
Shaohua Li019ceb72011-11-16 09:21:50 +01001457 if (request_count >= BLK_MAX_REQUEST_COUNT) {
Shaohua Li3540d5e2011-11-16 09:21:50 +01001458 blk_flush_plug_list(plug, false);
Shaohua Li019ceb72011-11-16 09:21:50 +01001459 trace_block_plug(q);
1460 }
Jens Axboe73c10102011-03-08 13:19:51 +01001461 }
Shaohua Lia6327162011-08-24 16:04:32 +02001462 list_add_tail(&req->queuelist, &plug->list);
1463 drive_stat_acct(req, 1);
Jens Axboe73c10102011-03-08 13:19:51 +01001464 } else {
1465 spin_lock_irq(q->queue_lock);
1466 add_acct_request(q, req, where);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02001467 __blk_run_queue(q);
Jens Axboe73c10102011-03-08 13:19:51 +01001468out_unlock:
1469 spin_unlock_irq(q->queue_lock);
1470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471}
Jens Axboec20e8de2011-09-12 12:03:37 +02001472EXPORT_SYMBOL_GPL(blk_queue_bio); /* for device mapper only */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474/*
1475 * If bio->bi_dev is a partition, remap the location
1476 */
1477static inline void blk_partition_remap(struct bio *bio)
1478{
1479 struct block_device *bdev = bio->bi_bdev;
1480
Jens Axboebf2de6f2007-09-27 13:01:25 +02001481 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 struct hd_struct *p = bdev->bd_part;
Jens Axboea3623572005-11-01 09:26:16 +01001483
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 bio->bi_sector += p->start_sect;
1485 bio->bi_bdev = bdev->bd_contains;
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001486
Mike Snitzerd07335e2010-11-16 12:52:38 +01001487 trace_block_bio_remap(bdev_get_queue(bio->bi_bdev), bio,
1488 bdev->bd_dev,
1489 bio->bi_sector - p->start_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 }
1491}
1492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493static void handle_bad_sector(struct bio *bio)
1494{
1495 char b[BDEVNAME_SIZE];
1496
1497 printk(KERN_INFO "attempt to access beyond end of device\n");
1498 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1499 bdevname(bio->bi_bdev, b),
1500 bio->bi_rw,
1501 (unsigned long long)bio->bi_sector + bio_sectors(bio),
Mike Snitzer77304d22010-11-08 14:39:12 +01001502 (long long)(i_size_read(bio->bi_bdev->bd_inode) >> 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
1504 set_bit(BIO_EOF, &bio->bi_flags);
1505}
1506
Akinobu Mitac17bb492006-12-08 02:39:46 -08001507#ifdef CONFIG_FAIL_MAKE_REQUEST
1508
1509static DECLARE_FAULT_ATTR(fail_make_request);
1510
1511static int __init setup_fail_make_request(char *str)
1512{
1513 return setup_fault_attr(&fail_make_request, str);
1514}
1515__setup("fail_make_request=", setup_fail_make_request);
1516
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001517static bool should_fail_request(struct hd_struct *part, unsigned int bytes)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001518{
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001519 return part->make_it_fail && should_fail(&fail_make_request, bytes);
Akinobu Mitac17bb492006-12-08 02:39:46 -08001520}
1521
1522static int __init fail_make_request_debugfs(void)
1523{
Akinobu Mitadd48c082011-08-03 16:21:01 -07001524 struct dentry *dir = fault_create_debugfs_attr("fail_make_request",
1525 NULL, &fail_make_request);
1526
1527 return IS_ERR(dir) ? PTR_ERR(dir) : 0;
Akinobu Mitac17bb492006-12-08 02:39:46 -08001528}
1529
1530late_initcall(fail_make_request_debugfs);
1531
1532#else /* CONFIG_FAIL_MAKE_REQUEST */
1533
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001534static inline bool should_fail_request(struct hd_struct *part,
1535 unsigned int bytes)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001536{
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001537 return false;
Akinobu Mitac17bb492006-12-08 02:39:46 -08001538}
1539
1540#endif /* CONFIG_FAIL_MAKE_REQUEST */
1541
Jens Axboec07e2b42007-07-18 13:27:58 +02001542/*
1543 * Check whether this bio extends beyond the end of the device.
1544 */
1545static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1546{
1547 sector_t maxsector;
1548
1549 if (!nr_sectors)
1550 return 0;
1551
1552 /* Test device or partition size, when known. */
Mike Snitzer77304d22010-11-08 14:39:12 +01001553 maxsector = i_size_read(bio->bi_bdev->bd_inode) >> 9;
Jens Axboec07e2b42007-07-18 13:27:58 +02001554 if (maxsector) {
1555 sector_t sector = bio->bi_sector;
1556
1557 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1558 /*
1559 * This may well happen - the kernel calls bread()
1560 * without checking the size of the device, e.g., when
1561 * mounting a device.
1562 */
1563 handle_bad_sector(bio);
1564 return 1;
1565 }
1566 }
1567
1568 return 0;
1569}
1570
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001571static noinline_for_stack bool
1572generic_make_request_checks(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573{
Jens Axboe165125e2007-07-24 09:28:11 +02001574 struct request_queue *q;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001575 int nr_sectors = bio_sectors(bio);
Jens Axboe51fd77b2007-11-02 08:49:08 +01001576 int err = -EIO;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001577 char b[BDEVNAME_SIZE];
1578 struct hd_struct *part;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Jens Axboec07e2b42007-07-18 13:27:58 +02001582 if (bio_check_eod(bio, nr_sectors))
1583 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001585 q = bdev_get_queue(bio->bi_bdev);
1586 if (unlikely(!q)) {
1587 printk(KERN_ERR
1588 "generic_make_request: Trying to access "
1589 "nonexistent block-device %s (%Lu)\n",
1590 bdevname(bio->bi_bdev, b),
1591 (long long) bio->bi_sector);
1592 goto end_io;
1593 }
1594
1595 if (unlikely(!(bio->bi_rw & REQ_DISCARD) &&
1596 nr_sectors > queue_max_hw_sectors(q))) {
1597 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
1598 bdevname(bio->bi_bdev, b),
1599 bio_sectors(bio),
1600 queue_max_hw_sectors(q));
1601 goto end_io;
1602 }
1603
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001604 part = bio->bi_bdev->bd_part;
1605 if (should_fail_request(part, bio->bi_size) ||
1606 should_fail_request(&part_to_disk(part)->part0,
1607 bio->bi_size))
1608 goto end_io;
1609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 /*
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001611 * If this device has partitions, remap block n
1612 * of partition p to block n+start(p) of the disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001614 blk_partition_remap(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001616 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio))
1617 goto end_io;
1618
1619 if (bio_check_eod(bio, nr_sectors))
1620 goto end_io;
1621
1622 /*
1623 * Filter flush bio's early so that make_request based
1624 * drivers without flush support don't have to worry
1625 * about them.
1626 */
1627 if ((bio->bi_rw & (REQ_FLUSH | REQ_FUA)) && !q->flush_flags) {
1628 bio->bi_rw &= ~(REQ_FLUSH | REQ_FUA);
1629 if (!nr_sectors) {
1630 err = 0;
Tejun Heoa7384672008-11-28 13:32:03 +09001631 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 }
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001635 if ((bio->bi_rw & REQ_DISCARD) &&
1636 (!blk_queue_discard(q) ||
1637 ((bio->bi_rw & REQ_SECURE) &&
1638 !blk_queue_secdiscard(q)))) {
1639 err = -EOPNOTSUPP;
1640 goto end_io;
1641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Tejun Heobc16a4f2011-10-19 14:33:01 +02001643 if (blk_throtl_bio(q, bio))
1644 return false; /* throttled, will be resubmitted later */
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001645
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001646 trace_block_bio_queue(q, bio);
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001647 return true;
Tejun Heoa7384672008-11-28 13:32:03 +09001648
1649end_io:
1650 bio_endio(bio, err);
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001651 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652}
1653
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001654/**
1655 * generic_make_request - hand a buffer to its device driver for I/O
1656 * @bio: The bio describing the location in memory and on the device.
1657 *
1658 * generic_make_request() is used to make I/O requests of block
1659 * devices. It is passed a &struct bio, which describes the I/O that needs
1660 * to be done.
1661 *
1662 * generic_make_request() does not return any status. The
1663 * success/failure status of the request, along with notification of
1664 * completion, is delivered asynchronously through the bio->bi_end_io
1665 * function described (one day) else where.
1666 *
1667 * The caller of generic_make_request must make sure that bi_io_vec
1668 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1669 * set to describe the device address, and the
1670 * bi_end_io and optionally bi_private are set to describe how
1671 * completion notification should be signaled.
1672 *
1673 * generic_make_request and the drivers it calls may use bi_next if this
1674 * bio happens to be merged with someone else, and may resubmit the bio to
1675 * a lower device by calling into generic_make_request recursively, which
1676 * means the bio should NOT be touched after the call to ->make_request_fn.
Neil Brownd89d8792007-05-01 09:53:42 +02001677 */
1678void generic_make_request(struct bio *bio)
1679{
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001680 struct bio_list bio_list_on_stack;
1681
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001682 if (!generic_make_request_checks(bio))
1683 return;
1684
1685 /*
1686 * We only want one ->make_request_fn to be active at a time, else
1687 * stack usage with stacked devices could be a problem. So use
1688 * current->bio_list to keep a list of requests submited by a
1689 * make_request_fn function. current->bio_list is also used as a
1690 * flag to say if generic_make_request is currently active in this
1691 * task or not. If it is NULL, then no make_request is active. If
1692 * it is non-NULL, then a make_request is active, and new requests
1693 * should be added at the tail
1694 */
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001695 if (current->bio_list) {
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001696 bio_list_add(current->bio_list, bio);
Neil Brownd89d8792007-05-01 09:53:42 +02001697 return;
1698 }
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001699
Neil Brownd89d8792007-05-01 09:53:42 +02001700 /* following loop may be a bit non-obvious, and so deserves some
1701 * explanation.
1702 * Before entering the loop, bio->bi_next is NULL (as all callers
1703 * ensure that) so we have a list with a single bio.
1704 * We pretend that we have just taken it off a longer list, so
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001705 * we assign bio_list to a pointer to the bio_list_on_stack,
1706 * thus initialising the bio_list of new bios to be
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001707 * added. ->make_request() may indeed add some more bios
Neil Brownd89d8792007-05-01 09:53:42 +02001708 * through a recursive call to generic_make_request. If it
1709 * did, we find a non-NULL value in bio_list and re-enter the loop
1710 * from the top. In this case we really did just take the bio
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001711 * of the top of the list (no pretending) and so remove it from
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001712 * bio_list, and call into ->make_request() again.
Neil Brownd89d8792007-05-01 09:53:42 +02001713 */
1714 BUG_ON(bio->bi_next);
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001715 bio_list_init(&bio_list_on_stack);
1716 current->bio_list = &bio_list_on_stack;
Neil Brownd89d8792007-05-01 09:53:42 +02001717 do {
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001718 struct request_queue *q = bdev_get_queue(bio->bi_bdev);
1719
1720 q->make_request_fn(q, bio);
1721
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001722 bio = bio_list_pop(current->bio_list);
Neil Brownd89d8792007-05-01 09:53:42 +02001723 } while (bio);
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001724 current->bio_list = NULL; /* deactivate */
Neil Brownd89d8792007-05-01 09:53:42 +02001725}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726EXPORT_SYMBOL(generic_make_request);
1727
1728/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001729 * submit_bio - submit a bio to the block device layer for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1731 * @bio: The &struct bio which describes the I/O
1732 *
1733 * submit_bio() is very similar in purpose to generic_make_request(), and
1734 * uses that function to do most of the work. Both are fairly rough
Randy Dunlap710027a2008-08-19 20:13:11 +02001735 * interfaces; @bio must be presetup and ready for I/O.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 *
1737 */
1738void submit_bio(int rw, struct bio *bio)
1739{
1740 int count = bio_sectors(bio);
1741
Jens Axboe22e2c502005-06-27 10:55:12 +02001742 bio->bi_rw |= rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Jens Axboebf2de6f2007-09-27 13:01:25 +02001744 /*
1745 * If it's a regular read/write or a barrier with data attached,
1746 * go through the normal accounting stuff before submission.
1747 */
Jens Axboe3ffb52e2010-06-29 13:33:38 +02001748 if (bio_has_data(bio) && !(rw & REQ_DISCARD)) {
Jens Axboebf2de6f2007-09-27 13:01:25 +02001749 if (rw & WRITE) {
1750 count_vm_events(PGPGOUT, count);
1751 } else {
1752 task_io_account_read(bio->bi_size);
1753 count_vm_events(PGPGIN, count);
1754 }
1755
1756 if (unlikely(block_dump)) {
1757 char b[BDEVNAME_SIZE];
San Mehat8dcbdc72010-09-14 08:48:01 +02001758 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s (%u sectors)\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001759 current->comm, task_pid_nr(current),
Jens Axboebf2de6f2007-09-27 13:01:25 +02001760 (rw & WRITE) ? "WRITE" : "READ",
1761 (unsigned long long)bio->bi_sector,
San Mehat8dcbdc72010-09-14 08:48:01 +02001762 bdevname(bio->bi_bdev, b),
1763 count);
Jens Axboebf2de6f2007-09-27 13:01:25 +02001764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 }
1766
1767 generic_make_request(bio);
1768}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769EXPORT_SYMBOL(submit_bio);
1770
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001771/**
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001772 * blk_rq_check_limits - Helper function to check a request for the queue limit
1773 * @q: the queue
1774 * @rq: the request being checked
1775 *
1776 * Description:
1777 * @rq may have been made based on weaker limitations of upper-level queues
1778 * in request stacking drivers, and it may violate the limitation of @q.
1779 * Since the block layer and the underlying device driver trust @rq
1780 * after it is inserted to @q, it should be checked against @q before
1781 * the insertion using this generic function.
1782 *
1783 * This function should also be useful for request stacking drivers
Stefan Weileef35c22010-08-06 21:11:15 +02001784 * in some cases below, so export this function.
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001785 * Request stacking drivers like request-based dm may change the queue
1786 * limits while requests are in the queue (e.g. dm's table swapping).
1787 * Such request stacking drivers should check those requests agaist
1788 * the new queue limits again when they dispatch those requests,
1789 * although such checkings are also done against the old queue limits
1790 * when submitting requests.
1791 */
1792int blk_rq_check_limits(struct request_queue *q, struct request *rq)
1793{
ike Snitzer33839772010-08-08 12:11:33 -04001794 if (rq->cmd_flags & REQ_DISCARD)
1795 return 0;
1796
Martin K. Petersenae03bf62009-05-22 17:17:50 -04001797 if (blk_rq_sectors(rq) > queue_max_sectors(q) ||
1798 blk_rq_bytes(rq) > queue_max_hw_sectors(q) << 9) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001799 printk(KERN_ERR "%s: over max size limit.\n", __func__);
1800 return -EIO;
1801 }
1802
1803 /*
1804 * queue's settings related to segment counting like q->bounce_pfn
1805 * may differ from that of other stacking queues.
1806 * Recalculate it to check the request correctly on this queue's
1807 * limitation.
1808 */
1809 blk_recalc_rq_segments(rq);
Martin K. Petersen8a783622010-02-26 00:20:39 -05001810 if (rq->nr_phys_segments > queue_max_segments(q)) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001811 printk(KERN_ERR "%s: over max segments limit.\n", __func__);
1812 return -EIO;
1813 }
1814
1815 return 0;
1816}
1817EXPORT_SYMBOL_GPL(blk_rq_check_limits);
1818
1819/**
1820 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
1821 * @q: the queue to submit the request
1822 * @rq: the request being queued
1823 */
1824int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
1825{
1826 unsigned long flags;
Jeff Moyer4853aba2011-08-15 21:37:25 +02001827 int where = ELEVATOR_INSERT_BACK;
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001828
1829 if (blk_rq_check_limits(q, rq))
1830 return -EIO;
1831
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001832 if (rq->rq_disk &&
1833 should_fail_request(&rq->rq_disk->part0, blk_rq_bytes(rq)))
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001834 return -EIO;
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001835
1836 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo8ba61432011-12-14 00:33:37 +01001837 if (unlikely(blk_queue_dead(q))) {
1838 spin_unlock_irqrestore(q->queue_lock, flags);
1839 return -ENODEV;
1840 }
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001841
1842 /*
1843 * Submitting request must be dequeued before calling this function
1844 * because it will be linked to another request_queue
1845 */
1846 BUG_ON(blk_queued_rq(rq));
1847
Jeff Moyer4853aba2011-08-15 21:37:25 +02001848 if (rq->cmd_flags & (REQ_FLUSH|REQ_FUA))
1849 where = ELEVATOR_INSERT_FLUSH;
1850
1851 add_acct_request(q, rq, where);
Jeff Moyere67b77c2011-10-17 12:57:23 +02001852 if (where == ELEVATOR_INSERT_FLUSH)
1853 __blk_run_queue(q);
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001854 spin_unlock_irqrestore(q->queue_lock, flags);
1855
1856 return 0;
1857}
1858EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
1859
Tejun Heo80a761f2009-07-03 17:48:17 +09001860/**
1861 * blk_rq_err_bytes - determine number of bytes till the next failure boundary
1862 * @rq: request to examine
1863 *
1864 * Description:
1865 * A request could be merge of IOs which require different failure
1866 * handling. This function determines the number of bytes which
1867 * can be failed from the beginning of the request without
1868 * crossing into area which need to be retried further.
1869 *
1870 * Return:
1871 * The number of bytes to fail.
1872 *
1873 * Context:
1874 * queue_lock must be held.
1875 */
1876unsigned int blk_rq_err_bytes(const struct request *rq)
1877{
1878 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
1879 unsigned int bytes = 0;
1880 struct bio *bio;
1881
1882 if (!(rq->cmd_flags & REQ_MIXED_MERGE))
1883 return blk_rq_bytes(rq);
1884
1885 /*
1886 * Currently the only 'mixing' which can happen is between
1887 * different fastfail types. We can safely fail portions
1888 * which have all the failfast bits that the first one has -
1889 * the ones which are at least as eager to fail as the first
1890 * one.
1891 */
1892 for (bio = rq->bio; bio; bio = bio->bi_next) {
1893 if ((bio->bi_rw & ff) != ff)
1894 break;
1895 bytes += bio->bi_size;
1896 }
1897
1898 /* this could lead to infinite loop */
1899 BUG_ON(blk_rq_bytes(rq) && !bytes);
1900 return bytes;
1901}
1902EXPORT_SYMBOL_GPL(blk_rq_err_bytes);
1903
Jens Axboebc58ba92009-01-23 10:54:44 +01001904static void blk_account_io_completion(struct request *req, unsigned int bytes)
1905{
Jens Axboec2553b52009-04-24 08:10:11 +02001906 if (blk_do_io_stat(req)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01001907 const int rw = rq_data_dir(req);
1908 struct hd_struct *part;
1909 int cpu;
1910
1911 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +01001912 part = req->part;
Jens Axboebc58ba92009-01-23 10:54:44 +01001913 part_stat_add(cpu, part, sectors[rw], bytes >> 9);
1914 part_stat_unlock();
1915 }
1916}
1917
1918static void blk_account_io_done(struct request *req)
1919{
Jens Axboebc58ba92009-01-23 10:54:44 +01001920 /*
Tejun Heodd4c1332010-09-03 11:56:16 +02001921 * Account IO completion. flush_rq isn't accounted as a
1922 * normal IO on queueing nor completion. Accounting the
1923 * containing request is enough.
Jens Axboebc58ba92009-01-23 10:54:44 +01001924 */
Tejun Heo414b4ff2011-01-25 12:43:49 +01001925 if (blk_do_io_stat(req) && !(req->cmd_flags & REQ_FLUSH_SEQ)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01001926 unsigned long duration = jiffies - req->start_time;
1927 const int rw = rq_data_dir(req);
1928 struct hd_struct *part;
1929 int cpu;
1930
1931 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +01001932 part = req->part;
Jens Axboebc58ba92009-01-23 10:54:44 +01001933
1934 part_stat_inc(cpu, part, ios[rw]);
1935 part_stat_add(cpu, part, ticks[rw], duration);
1936 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001937 part_dec_in_flight(part, rw);
Jens Axboebc58ba92009-01-23 10:54:44 +01001938
Jens Axboe6c23a962011-01-07 08:43:37 +01001939 hd_struct_put(part);
Jens Axboebc58ba92009-01-23 10:54:44 +01001940 part_stat_unlock();
1941 }
1942}
1943
Tejun Heo53a08802008-12-03 12:41:26 +01001944/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09001945 * blk_peek_request - peek at the top of a request queue
1946 * @q: request queue to peek at
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001947 *
1948 * Description:
Tejun Heo9934c8c2009-05-08 11:54:16 +09001949 * Return the request at the top of @q. The returned request
1950 * should be started using blk_start_request() before LLD starts
1951 * processing it.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001952 *
1953 * Return:
Tejun Heo9934c8c2009-05-08 11:54:16 +09001954 * Pointer to the request at the top of @q if available. Null
1955 * otherwise.
1956 *
1957 * Context:
1958 * queue_lock must be held.
1959 */
1960struct request *blk_peek_request(struct request_queue *q)
Tejun Heo158dbda2009-04-23 11:05:18 +09001961{
1962 struct request *rq;
1963 int ret;
1964
1965 while ((rq = __elv_next_request(q)) != NULL) {
1966 if (!(rq->cmd_flags & REQ_STARTED)) {
1967 /*
1968 * This is the first time the device driver
1969 * sees this request (possibly after
1970 * requeueing). Notify IO scheduler.
1971 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001972 if (rq->cmd_flags & REQ_SORTED)
Tejun Heo158dbda2009-04-23 11:05:18 +09001973 elv_activate_rq(q, rq);
1974
1975 /*
1976 * just mark as started even if we don't start
1977 * it, a request that has been delayed should
1978 * not be passed by new incoming requests
1979 */
1980 rq->cmd_flags |= REQ_STARTED;
1981 trace_block_rq_issue(q, rq);
1982 }
1983
1984 if (!q->boundary_rq || q->boundary_rq == rq) {
1985 q->end_sector = rq_end_sector(rq);
1986 q->boundary_rq = NULL;
1987 }
1988
1989 if (rq->cmd_flags & REQ_DONTPREP)
1990 break;
1991
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001992 if (q->dma_drain_size && blk_rq_bytes(rq)) {
Tejun Heo158dbda2009-04-23 11:05:18 +09001993 /*
1994 * make sure space for the drain appears we
1995 * know we can do this because max_hw_segments
1996 * has been adjusted to be one fewer than the
1997 * device can handle
1998 */
1999 rq->nr_phys_segments++;
2000 }
2001
2002 if (!q->prep_rq_fn)
2003 break;
2004
2005 ret = q->prep_rq_fn(q, rq);
2006 if (ret == BLKPREP_OK) {
2007 break;
2008 } else if (ret == BLKPREP_DEFER) {
2009 /*
2010 * the request may have been (partially) prepped.
2011 * we need to keep this request in the front to
2012 * avoid resource deadlock. REQ_STARTED will
2013 * prevent other fs requests from passing this one.
2014 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002015 if (q->dma_drain_size && blk_rq_bytes(rq) &&
Tejun Heo158dbda2009-04-23 11:05:18 +09002016 !(rq->cmd_flags & REQ_DONTPREP)) {
2017 /*
2018 * remove the space for the drain we added
2019 * so that we don't add it again
2020 */
2021 --rq->nr_phys_segments;
2022 }
2023
2024 rq = NULL;
2025 break;
2026 } else if (ret == BLKPREP_KILL) {
2027 rq->cmd_flags |= REQ_QUIET;
James Bottomleyc143dc92009-05-30 06:43:49 +02002028 /*
2029 * Mark this request as started so we don't trigger
2030 * any debug logic in the end I/O path.
2031 */
2032 blk_start_request(rq);
Tejun Heo40cbbb72009-04-23 11:05:19 +09002033 __blk_end_request_all(rq, -EIO);
Tejun Heo158dbda2009-04-23 11:05:18 +09002034 } else {
2035 printk(KERN_ERR "%s: bad return=%d\n", __func__, ret);
2036 break;
2037 }
2038 }
2039
2040 return rq;
2041}
Tejun Heo9934c8c2009-05-08 11:54:16 +09002042EXPORT_SYMBOL(blk_peek_request);
Tejun Heo158dbda2009-04-23 11:05:18 +09002043
Tejun Heo9934c8c2009-05-08 11:54:16 +09002044void blk_dequeue_request(struct request *rq)
Tejun Heo158dbda2009-04-23 11:05:18 +09002045{
Tejun Heo9934c8c2009-05-08 11:54:16 +09002046 struct request_queue *q = rq->q;
2047
Tejun Heo158dbda2009-04-23 11:05:18 +09002048 BUG_ON(list_empty(&rq->queuelist));
2049 BUG_ON(ELV_ON_HASH(rq));
2050
2051 list_del_init(&rq->queuelist);
2052
2053 /*
2054 * the time frame between a request being removed from the lists
2055 * and to it is freed is accounted as io that is in progress at
2056 * the driver side.
2057 */
Divyesh Shah91952912010-04-01 15:01:41 -07002058 if (blk_account_rq(rq)) {
Jens Axboe0a7ae2f2009-05-20 08:54:31 +02002059 q->in_flight[rq_is_sync(rq)]++;
Divyesh Shah91952912010-04-01 15:01:41 -07002060 set_io_start_time_ns(rq);
2061 }
Tejun Heo158dbda2009-04-23 11:05:18 +09002062}
2063
Tejun Heo5efccd12009-04-23 11:05:18 +09002064/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09002065 * blk_start_request - start request processing on the driver
2066 * @req: request to dequeue
2067 *
2068 * Description:
2069 * Dequeue @req and start timeout timer on it. This hands off the
2070 * request to the driver.
2071 *
2072 * Block internal functions which don't want to start timer should
2073 * call blk_dequeue_request().
2074 *
2075 * Context:
2076 * queue_lock must be held.
2077 */
2078void blk_start_request(struct request *req)
2079{
2080 blk_dequeue_request(req);
2081
2082 /*
Tejun Heo5f49f632009-05-19 18:33:05 +09002083 * We are now handing the request to the hardware, initialize
2084 * resid_len to full count and add the timeout handler.
Tejun Heo9934c8c2009-05-08 11:54:16 +09002085 */
Tejun Heo5f49f632009-05-19 18:33:05 +09002086 req->resid_len = blk_rq_bytes(req);
FUJITA Tomonoridbb66c42009-06-09 05:47:10 +02002087 if (unlikely(blk_bidi_rq(req)))
2088 req->next_rq->resid_len = blk_rq_bytes(req->next_rq);
2089
Tejun Heo9934c8c2009-05-08 11:54:16 +09002090 blk_add_timer(req);
2091}
2092EXPORT_SYMBOL(blk_start_request);
2093
2094/**
2095 * blk_fetch_request - fetch a request from a request queue
2096 * @q: request queue to fetch a request from
2097 *
2098 * Description:
2099 * Return the request at the top of @q. The request is started on
2100 * return and LLD can start processing it immediately.
2101 *
2102 * Return:
2103 * Pointer to the request at the top of @q if available. Null
2104 * otherwise.
2105 *
2106 * Context:
2107 * queue_lock must be held.
2108 */
2109struct request *blk_fetch_request(struct request_queue *q)
2110{
2111 struct request *rq;
2112
2113 rq = blk_peek_request(q);
2114 if (rq)
2115 blk_start_request(rq);
2116 return rq;
2117}
2118EXPORT_SYMBOL(blk_fetch_request);
2119
2120/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002121 * blk_update_request - Special helper function for request stacking drivers
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002122 * @req: the request being processed
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002123 * @error: %0 for success, < %0 for error
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002124 * @nr_bytes: number of bytes to complete @req
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002125 *
2126 * Description:
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002127 * Ends I/O on a number of bytes attached to @req, but doesn't complete
2128 * the request structure even if @req doesn't have leftover.
2129 * If @req has leftover, sets it up for the next range of segments.
Tejun Heo2e60e022009-04-23 11:05:18 +09002130 *
2131 * This special helper function is only for request stacking drivers
2132 * (e.g. request-based dm) so that they can handle partial completion.
2133 * Actual device drivers should use blk_end_request instead.
2134 *
2135 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
2136 * %false return from this function.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002137 *
2138 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002139 * %false - this request doesn't have any more data
2140 * %true - this request has more data
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002141 **/
Tejun Heo2e60e022009-04-23 11:05:18 +09002142bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143{
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05002144 int total_bytes, bio_nbytes, next_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 struct bio *bio;
2146
Tejun Heo2e60e022009-04-23 11:05:18 +09002147 if (!req->bio)
2148 return false;
2149
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01002150 trace_block_rq_complete(req->q, req);
Jens Axboe2056a782006-03-23 20:00:26 +01002151
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 /*
Tejun Heo6f414692009-04-19 07:00:41 +09002153 * For fs requests, rq is just carrier of independent bio's
2154 * and each partial completion should be handled separately.
2155 * Reset per-request error on each partial completion.
2156 *
2157 * TODO: tj: This is too subtle. It would be better to let
2158 * low level drivers do what they see fit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002160 if (req->cmd_type == REQ_TYPE_FS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 req->errors = 0;
2162
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002163 if (error && req->cmd_type == REQ_TYPE_FS &&
2164 !(req->cmd_flags & REQ_QUIET)) {
Hannes Reinecke79775562011-01-18 10:13:13 +01002165 char *error_type;
2166
2167 switch (error) {
2168 case -ENOLINK:
2169 error_type = "recoverable transport";
2170 break;
2171 case -EREMOTEIO:
2172 error_type = "critical target";
2173 break;
2174 case -EBADE:
2175 error_type = "critical nexus";
2176 break;
2177 case -EIO:
2178 default:
2179 error_type = "I/O";
2180 break;
2181 }
2182 printk(KERN_ERR "end_request: %s error, dev %s, sector %llu\n",
2183 error_type, req->rq_disk ? req->rq_disk->disk_name : "?",
2184 (unsigned long long)blk_rq_pos(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 }
2186
Jens Axboebc58ba92009-01-23 10:54:44 +01002187 blk_account_io_completion(req, nr_bytes);
Jens Axboed72d9042005-11-01 08:35:42 +01002188
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 total_bytes = bio_nbytes = 0;
2190 while ((bio = req->bio) != NULL) {
2191 int nbytes;
2192
2193 if (nr_bytes >= bio->bi_size) {
2194 req->bio = bio->bi_next;
2195 nbytes = bio->bi_size;
NeilBrown5bb23a62007-09-27 12:46:13 +02002196 req_bio_endio(req, bio, nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 next_idx = 0;
2198 bio_nbytes = 0;
2199 } else {
2200 int idx = bio->bi_idx + next_idx;
2201
Kazuhisa Ichikawaaf498d72009-05-12 13:27:45 +02002202 if (unlikely(idx >= bio->bi_vcnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 blk_dump_rq_flags(req, "__end_that");
Jens Axboe6728cb02008-01-31 13:03:55 +01002204 printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
Kazuhisa Ichikawaaf498d72009-05-12 13:27:45 +02002205 __func__, idx, bio->bi_vcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 break;
2207 }
2208
2209 nbytes = bio_iovec_idx(bio, idx)->bv_len;
2210 BIO_BUG_ON(nbytes > bio->bi_size);
2211
2212 /*
2213 * not a complete bvec done
2214 */
2215 if (unlikely(nbytes > nr_bytes)) {
2216 bio_nbytes += nr_bytes;
2217 total_bytes += nr_bytes;
2218 break;
2219 }
2220
2221 /*
2222 * advance to the next vector
2223 */
2224 next_idx++;
2225 bio_nbytes += nbytes;
2226 }
2227
2228 total_bytes += nbytes;
2229 nr_bytes -= nbytes;
2230
Jens Axboe6728cb02008-01-31 13:03:55 +01002231 bio = req->bio;
2232 if (bio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 /*
2234 * end more in this run, or just return 'not-done'
2235 */
2236 if (unlikely(nr_bytes <= 0))
2237 break;
2238 }
2239 }
2240
2241 /*
2242 * completely done
2243 */
Tejun Heo2e60e022009-04-23 11:05:18 +09002244 if (!req->bio) {
2245 /*
2246 * Reset counters so that the request stacking driver
2247 * can find how many bytes remain in the request
2248 * later.
2249 */
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002250 req->__data_len = 0;
Tejun Heo2e60e022009-04-23 11:05:18 +09002251 return false;
2252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
2254 /*
2255 * if the request wasn't completed, update state
2256 */
2257 if (bio_nbytes) {
NeilBrown5bb23a62007-09-27 12:46:13 +02002258 req_bio_endio(req, bio, bio_nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 bio->bi_idx += next_idx;
2260 bio_iovec(bio)->bv_offset += nr_bytes;
2261 bio_iovec(bio)->bv_len -= nr_bytes;
2262 }
2263
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002264 req->__data_len -= total_bytes;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002265 req->buffer = bio_data(req->bio);
2266
2267 /* update sector only for requests with clear definition of sector */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002268 if (req->cmd_type == REQ_TYPE_FS || (req->cmd_flags & REQ_DISCARD))
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002269 req->__sector += total_bytes >> 9;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002270
Tejun Heo80a761f2009-07-03 17:48:17 +09002271 /* mixed attributes always follow the first bio */
2272 if (req->cmd_flags & REQ_MIXED_MERGE) {
2273 req->cmd_flags &= ~REQ_FAILFAST_MASK;
2274 req->cmd_flags |= req->bio->bi_rw & REQ_FAILFAST_MASK;
2275 }
2276
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002277 /*
2278 * If total number of sectors is less than the first segment
2279 * size, something has gone terribly wrong.
2280 */
2281 if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
Jens Axboe81829242011-03-30 09:51:33 +02002282 blk_dump_rq_flags(req, "request botched");
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002283 req->__data_len = blk_rq_cur_bytes(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002284 }
2285
2286 /* recalculate the number of segments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 blk_recalc_rq_segments(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002288
Tejun Heo2e60e022009-04-23 11:05:18 +09002289 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290}
Tejun Heo2e60e022009-04-23 11:05:18 +09002291EXPORT_SYMBOL_GPL(blk_update_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Tejun Heo2e60e022009-04-23 11:05:18 +09002293static bool blk_update_bidi_request(struct request *rq, int error,
2294 unsigned int nr_bytes,
2295 unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002296{
Tejun Heo2e60e022009-04-23 11:05:18 +09002297 if (blk_update_request(rq, error, nr_bytes))
2298 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002299
Tejun Heo2e60e022009-04-23 11:05:18 +09002300 /* Bidi request must be completed as a whole */
2301 if (unlikely(blk_bidi_rq(rq)) &&
2302 blk_update_request(rq->next_rq, error, bidi_bytes))
2303 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002304
Jens Axboee2e1a142010-06-09 10:42:09 +02002305 if (blk_queue_add_random(rq->q))
2306 add_disk_randomness(rq->rq_disk);
Tejun Heo2e60e022009-04-23 11:05:18 +09002307
2308 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309}
2310
James Bottomley28018c22010-07-01 19:49:17 +09002311/**
2312 * blk_unprep_request - unprepare a request
2313 * @req: the request
2314 *
2315 * This function makes a request ready for complete resubmission (or
2316 * completion). It happens only after all error handling is complete,
2317 * so represents the appropriate moment to deallocate any resources
2318 * that were allocated to the request in the prep_rq_fn. The queue
2319 * lock is held when calling this.
2320 */
2321void blk_unprep_request(struct request *req)
2322{
2323 struct request_queue *q = req->q;
2324
2325 req->cmd_flags &= ~REQ_DONTPREP;
2326 if (q->unprep_rq_fn)
2327 q->unprep_rq_fn(q, req);
2328}
2329EXPORT_SYMBOL_GPL(blk_unprep_request);
2330
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331/*
2332 * queue lock must be held
2333 */
Tejun Heo2e60e022009-04-23 11:05:18 +09002334static void blk_finish_request(struct request *req, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335{
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002336 if (blk_rq_tagged(req))
2337 blk_queue_end_tag(req->q, req);
2338
James Bottomleyba396a62009-05-27 14:17:08 +02002339 BUG_ON(blk_queued_rq(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002341 if (unlikely(laptop_mode) && req->cmd_type == REQ_TYPE_FS)
Matthew Garrett31373d02010-04-06 14:25:14 +02002342 laptop_io_completion(&req->q->backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Mike Andersone78042e2008-10-30 02:16:20 -07002344 blk_delete_timer(req);
2345
James Bottomley28018c22010-07-01 19:49:17 +09002346 if (req->cmd_flags & REQ_DONTPREP)
2347 blk_unprep_request(req);
2348
2349
Jens Axboebc58ba92009-01-23 10:54:44 +01002350 blk_account_io_done(req);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002351
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 if (req->end_io)
Tejun Heo8ffdc652006-01-06 09:49:03 +01002353 req->end_io(req, error);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002354 else {
2355 if (blk_bidi_rq(req))
2356 __blk_put_request(req->next_rq->q, req->next_rq);
2357
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 __blk_put_request(req->q, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 }
2360}
2361
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05002362/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002363 * blk_end_bidi_request - Complete a bidi request
2364 * @rq: the request to complete
Randy Dunlap710027a2008-08-19 20:13:11 +02002365 * @error: %0 for success, < %0 for error
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002366 * @nr_bytes: number of bytes to complete @rq
2367 * @bidi_bytes: number of bytes to complete @rq->next_rq
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002368 *
2369 * Description:
2370 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
Tejun Heo2e60e022009-04-23 11:05:18 +09002371 * Drivers that supports bidi can safely call this member for any
2372 * type of request, bidi or uni. In the later case @bidi_bytes is
2373 * just ignored.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002374 *
2375 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002376 * %false - we are done with this request
2377 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002378 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002379static bool blk_end_bidi_request(struct request *rq, int error,
2380 unsigned int nr_bytes, unsigned int bidi_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002381{
2382 struct request_queue *q = rq->q;
Tejun Heo2e60e022009-04-23 11:05:18 +09002383 unsigned long flags;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002384
Tejun Heo2e60e022009-04-23 11:05:18 +09002385 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2386 return true;
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002387
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002388 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo2e60e022009-04-23 11:05:18 +09002389 blk_finish_request(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002390 spin_unlock_irqrestore(q->queue_lock, flags);
2391
Tejun Heo2e60e022009-04-23 11:05:18 +09002392 return false;
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002393}
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002394
2395/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002396 * __blk_end_bidi_request - Complete a bidi request with queue lock held
2397 * @rq: the request to complete
2398 * @error: %0 for success, < %0 for error
2399 * @nr_bytes: number of bytes to complete @rq
2400 * @bidi_bytes: number of bytes to complete @rq->next_rq
Tejun Heo5efccd12009-04-23 11:05:18 +09002401 *
2402 * Description:
Tejun Heo2e60e022009-04-23 11:05:18 +09002403 * Identical to blk_end_bidi_request() except that queue lock is
2404 * assumed to be locked on entry and remains so on return.
Tejun Heo5efccd12009-04-23 11:05:18 +09002405 *
Tejun Heo2e60e022009-04-23 11:05:18 +09002406 * Return:
2407 * %false - we are done with this request
2408 * %true - still buffers pending for this request
Tejun Heo5efccd12009-04-23 11:05:18 +09002409 **/
Jeff Moyer4853aba2011-08-15 21:37:25 +02002410bool __blk_end_bidi_request(struct request *rq, int error,
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002411 unsigned int nr_bytes, unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002412{
Tejun Heo2e60e022009-04-23 11:05:18 +09002413 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2414 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002415
Tejun Heo2e60e022009-04-23 11:05:18 +09002416 blk_finish_request(rq, error);
Tejun Heo5efccd12009-04-23 11:05:18 +09002417
Tejun Heo2e60e022009-04-23 11:05:18 +09002418 return false;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002419}
2420
2421/**
2422 * blk_end_request - Helper function for drivers to complete the request.
2423 * @rq: the request being processed
2424 * @error: %0 for success, < %0 for error
2425 * @nr_bytes: number of bytes to complete
2426 *
2427 * Description:
2428 * Ends I/O on a number of bytes attached to @rq.
2429 * If @rq has leftover, sets it up for the next range of segments.
2430 *
2431 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002432 * %false - we are done with this request
2433 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002434 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002435bool blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002436{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002437 return blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002438}
Jens Axboe56ad1742009-07-28 22:11:24 +02002439EXPORT_SYMBOL(blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002440
2441/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002442 * blk_end_request_all - Helper function for drives to finish the request.
2443 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002444 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002445 *
2446 * Description:
2447 * Completely finish @rq.
2448 */
2449void blk_end_request_all(struct request *rq, int error)
2450{
2451 bool pending;
2452 unsigned int bidi_bytes = 0;
2453
2454 if (unlikely(blk_bidi_rq(rq)))
2455 bidi_bytes = blk_rq_bytes(rq->next_rq);
2456
2457 pending = blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2458 BUG_ON(pending);
2459}
Jens Axboe56ad1742009-07-28 22:11:24 +02002460EXPORT_SYMBOL(blk_end_request_all);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002461
2462/**
2463 * blk_end_request_cur - Helper function to finish the current request chunk.
2464 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002465 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002466 *
2467 * Description:
2468 * Complete the current consecutively mapped chunk from @rq.
2469 *
2470 * Return:
2471 * %false - we are done with this request
2472 * %true - still buffers pending for this request
2473 */
2474bool blk_end_request_cur(struct request *rq, int error)
2475{
2476 return blk_end_request(rq, error, blk_rq_cur_bytes(rq));
2477}
Jens Axboe56ad1742009-07-28 22:11:24 +02002478EXPORT_SYMBOL(blk_end_request_cur);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002479
2480/**
Tejun Heo80a761f2009-07-03 17:48:17 +09002481 * blk_end_request_err - Finish a request till the next failure boundary.
2482 * @rq: the request to finish till the next failure boundary for
2483 * @error: must be negative errno
2484 *
2485 * Description:
2486 * Complete @rq till the next failure boundary.
2487 *
2488 * Return:
2489 * %false - we are done with this request
2490 * %true - still buffers pending for this request
2491 */
2492bool blk_end_request_err(struct request *rq, int error)
2493{
2494 WARN_ON(error >= 0);
2495 return blk_end_request(rq, error, blk_rq_err_bytes(rq));
2496}
2497EXPORT_SYMBOL_GPL(blk_end_request_err);
2498
2499/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002500 * __blk_end_request - Helper function for drivers to complete the request.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002501 * @rq: the request being processed
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002502 * @error: %0 for success, < %0 for error
2503 * @nr_bytes: number of bytes to complete
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002504 *
2505 * Description:
2506 * Must be called with queue lock held unlike blk_end_request().
2507 *
2508 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002509 * %false - we are done with this request
2510 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002511 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002512bool __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002513{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002514 return __blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002515}
Jens Axboe56ad1742009-07-28 22:11:24 +02002516EXPORT_SYMBOL(__blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002517
2518/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002519 * __blk_end_request_all - Helper function for drives to finish the request.
2520 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002521 * @error: %0 for success, < %0 for error
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002522 *
2523 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002524 * Completely finish @rq. Must be called with queue lock held.
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002525 */
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002526void __blk_end_request_all(struct request *rq, int error)
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002527{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002528 bool pending;
2529 unsigned int bidi_bytes = 0;
2530
2531 if (unlikely(blk_bidi_rq(rq)))
2532 bidi_bytes = blk_rq_bytes(rq->next_rq);
2533
2534 pending = __blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2535 BUG_ON(pending);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002536}
Jens Axboe56ad1742009-07-28 22:11:24 +02002537EXPORT_SYMBOL(__blk_end_request_all);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002538
2539/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002540 * __blk_end_request_cur - Helper function to finish the current request chunk.
2541 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002542 * @error: %0 for success, < %0 for error
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002543 *
2544 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002545 * Complete the current consecutively mapped chunk from @rq. Must
2546 * be called with queue lock held.
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002547 *
2548 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002549 * %false - we are done with this request
2550 * %true - still buffers pending for this request
2551 */
2552bool __blk_end_request_cur(struct request *rq, int error)
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002553{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002554 return __blk_end_request(rq, error, blk_rq_cur_bytes(rq));
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002555}
Jens Axboe56ad1742009-07-28 22:11:24 +02002556EXPORT_SYMBOL(__blk_end_request_cur);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002557
Tejun Heo80a761f2009-07-03 17:48:17 +09002558/**
2559 * __blk_end_request_err - Finish a request till the next failure boundary.
2560 * @rq: the request to finish till the next failure boundary for
2561 * @error: must be negative errno
2562 *
2563 * Description:
2564 * Complete @rq till the next failure boundary. Must be called
2565 * with queue lock held.
2566 *
2567 * Return:
2568 * %false - we are done with this request
2569 * %true - still buffers pending for this request
2570 */
2571bool __blk_end_request_err(struct request *rq, int error)
2572{
2573 WARN_ON(error >= 0);
2574 return __blk_end_request(rq, error, blk_rq_err_bytes(rq));
2575}
2576EXPORT_SYMBOL_GPL(__blk_end_request_err);
2577
Jens Axboe86db1e22008-01-29 14:53:40 +01002578void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2579 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580{
Tejun Heoa82afdf2009-07-03 17:48:16 +09002581 /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002582 rq->cmd_flags |= bio->bi_rw & REQ_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
David Woodhousefb2dce82008-08-05 18:01:53 +01002584 if (bio_has_data(bio)) {
2585 rq->nr_phys_segments = bio_phys_segments(q, bio);
David Woodhousefb2dce82008-08-05 18:01:53 +01002586 rq->buffer = bio_data(bio);
2587 }
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002588 rq->__data_len = bio->bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 rq->bio = rq->biotail = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
NeilBrown66846572007-08-16 13:31:28 +02002591 if (bio->bi_bdev)
2592 rq->rq_disk = bio->bi_bdev->bd_disk;
2593}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
Ilya Loginov2d4dc892009-11-26 09:16:19 +01002595#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
2596/**
2597 * rq_flush_dcache_pages - Helper function to flush all pages in a request
2598 * @rq: the request to be flushed
2599 *
2600 * Description:
2601 * Flush all pages in @rq.
2602 */
2603void rq_flush_dcache_pages(struct request *rq)
2604{
2605 struct req_iterator iter;
2606 struct bio_vec *bvec;
2607
2608 rq_for_each_segment(bvec, rq, iter)
2609 flush_dcache_page(bvec->bv_page);
2610}
2611EXPORT_SYMBOL_GPL(rq_flush_dcache_pages);
2612#endif
2613
Kiyoshi Uedaef9e3fa2008-10-01 16:12:15 +02002614/**
2615 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
2616 * @q : the queue of the device being checked
2617 *
2618 * Description:
2619 * Check if underlying low-level drivers of a device are busy.
2620 * If the drivers want to export their busy state, they must set own
2621 * exporting function using blk_queue_lld_busy() first.
2622 *
2623 * Basically, this function is used only by request stacking drivers
2624 * to stop dispatching requests to underlying devices when underlying
2625 * devices are busy. This behavior helps more I/O merging on the queue
2626 * of the request stacking driver and prevents I/O throughput regression
2627 * on burst I/O load.
2628 *
2629 * Return:
2630 * 0 - Not busy (The request stacking driver should dispatch request)
2631 * 1 - Busy (The request stacking driver should stop dispatching request)
2632 */
2633int blk_lld_busy(struct request_queue *q)
2634{
2635 if (q->lld_busy_fn)
2636 return q->lld_busy_fn(q);
2637
2638 return 0;
2639}
2640EXPORT_SYMBOL_GPL(blk_lld_busy);
2641
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002642/**
2643 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
2644 * @rq: the clone request to be cleaned up
2645 *
2646 * Description:
2647 * Free all bios in @rq for a cloned request.
2648 */
2649void blk_rq_unprep_clone(struct request *rq)
2650{
2651 struct bio *bio;
2652
2653 while ((bio = rq->bio) != NULL) {
2654 rq->bio = bio->bi_next;
2655
2656 bio_put(bio);
2657 }
2658}
2659EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
2660
2661/*
2662 * Copy attributes of the original request to the clone request.
2663 * The actual data parts (e.g. ->cmd, ->buffer, ->sense) are not copied.
2664 */
2665static void __blk_rq_prep_clone(struct request *dst, struct request *src)
2666{
2667 dst->cpu = src->cpu;
Tejun Heo3a2edd02010-09-03 11:56:18 +02002668 dst->cmd_flags = (src->cmd_flags & REQ_CLONE_MASK) | REQ_NOMERGE;
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002669 dst->cmd_type = src->cmd_type;
2670 dst->__sector = blk_rq_pos(src);
2671 dst->__data_len = blk_rq_bytes(src);
2672 dst->nr_phys_segments = src->nr_phys_segments;
2673 dst->ioprio = src->ioprio;
2674 dst->extra_len = src->extra_len;
2675}
2676
2677/**
2678 * blk_rq_prep_clone - Helper function to setup clone request
2679 * @rq: the request to be setup
2680 * @rq_src: original request to be cloned
2681 * @bs: bio_set that bios for clone are allocated from
2682 * @gfp_mask: memory allocation mask for bio
2683 * @bio_ctr: setup function to be called for each clone bio.
2684 * Returns %0 for success, non %0 for failure.
2685 * @data: private data to be passed to @bio_ctr
2686 *
2687 * Description:
2688 * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
2689 * The actual data parts of @rq_src (e.g. ->cmd, ->buffer, ->sense)
2690 * are not copied, and copying such parts is the caller's responsibility.
2691 * Also, pages which the original bios are pointing to are not copied
2692 * and the cloned bios just point same pages.
2693 * So cloned bios must be completed before original bios, which means
2694 * the caller must complete @rq before @rq_src.
2695 */
2696int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
2697 struct bio_set *bs, gfp_t gfp_mask,
2698 int (*bio_ctr)(struct bio *, struct bio *, void *),
2699 void *data)
2700{
2701 struct bio *bio, *bio_src;
2702
2703 if (!bs)
2704 bs = fs_bio_set;
2705
2706 blk_rq_init(NULL, rq);
2707
2708 __rq_for_each_bio(bio_src, rq_src) {
2709 bio = bio_alloc_bioset(gfp_mask, bio_src->bi_max_vecs, bs);
2710 if (!bio)
2711 goto free_and_out;
2712
2713 __bio_clone(bio, bio_src);
2714
2715 if (bio_integrity(bio_src) &&
Martin K. Petersen7878cba2009-06-26 15:37:49 +02002716 bio_integrity_clone(bio, bio_src, gfp_mask, bs))
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002717 goto free_and_out;
2718
2719 if (bio_ctr && bio_ctr(bio, bio_src, data))
2720 goto free_and_out;
2721
2722 if (rq->bio) {
2723 rq->biotail->bi_next = bio;
2724 rq->biotail = bio;
2725 } else
2726 rq->bio = rq->biotail = bio;
2727 }
2728
2729 __blk_rq_prep_clone(rq, rq_src);
2730
2731 return 0;
2732
2733free_and_out:
2734 if (bio)
2735 bio_free(bio, bs);
2736 blk_rq_unprep_clone(rq);
2737
2738 return -ENOMEM;
2739}
2740EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
2741
Jens Axboe18887ad2008-07-28 13:08:45 +02002742int kblockd_schedule_work(struct request_queue *q, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743{
2744 return queue_work(kblockd_workqueue, work);
2745}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746EXPORT_SYMBOL(kblockd_schedule_work);
2747
Vivek Goyale43473b2010-09-15 17:06:35 -04002748int kblockd_schedule_delayed_work(struct request_queue *q,
2749 struct delayed_work *dwork, unsigned long delay)
2750{
2751 return queue_delayed_work(kblockd_workqueue, dwork, delay);
2752}
2753EXPORT_SYMBOL(kblockd_schedule_delayed_work);
2754
Jens Axboe73c10102011-03-08 13:19:51 +01002755#define PLUG_MAGIC 0x91827364
2756
Suresh Jayaraman75df7132011-09-21 10:00:16 +02002757/**
2758 * blk_start_plug - initialize blk_plug and track it inside the task_struct
2759 * @plug: The &struct blk_plug that needs to be initialized
2760 *
2761 * Description:
2762 * Tracking blk_plug inside the task_struct will help with auto-flushing the
2763 * pending I/O should the task end up blocking between blk_start_plug() and
2764 * blk_finish_plug(). This is important from a performance perspective, but
2765 * also ensures that we don't deadlock. For instance, if the task is blocking
2766 * for a memory allocation, memory reclaim could end up wanting to free a
2767 * page belonging to that request that is currently residing in our private
2768 * plug. By flushing the pending I/O when the process goes to sleep, we avoid
2769 * this kind of deadlock.
2770 */
Jens Axboe73c10102011-03-08 13:19:51 +01002771void blk_start_plug(struct blk_plug *plug)
2772{
2773 struct task_struct *tsk = current;
2774
2775 plug->magic = PLUG_MAGIC;
2776 INIT_LIST_HEAD(&plug->list);
NeilBrown048c9372011-04-18 09:52:22 +02002777 INIT_LIST_HEAD(&plug->cb_list);
Jens Axboe73c10102011-03-08 13:19:51 +01002778 plug->should_sort = 0;
2779
2780 /*
2781 * If this is a nested plug, don't actually assign it. It will be
2782 * flushed on its own.
2783 */
2784 if (!tsk->plug) {
2785 /*
2786 * Store ordering should not be needed here, since a potential
2787 * preempt will imply a full memory barrier
2788 */
2789 tsk->plug = plug;
2790 }
2791}
2792EXPORT_SYMBOL(blk_start_plug);
2793
2794static int plug_rq_cmp(void *priv, struct list_head *a, struct list_head *b)
2795{
2796 struct request *rqa = container_of(a, struct request, queuelist);
2797 struct request *rqb = container_of(b, struct request, queuelist);
2798
Konstantin Khlebnikovf83e8262011-04-04 00:15:02 +02002799 return !(rqa->q <= rqb->q);
Jens Axboe73c10102011-03-08 13:19:51 +01002800}
2801
Jens Axboe49cac012011-04-16 13:51:05 +02002802/*
2803 * If 'from_schedule' is true, then postpone the dispatch of requests
2804 * until a safe kblockd context. We due this to avoid accidental big
2805 * additional stack usage in driver dispatch, in places where the originally
2806 * plugger did not intend it.
2807 */
Jens Axboef6603782011-04-15 15:49:07 +02002808static void queue_unplugged(struct request_queue *q, unsigned int depth,
Jens Axboe49cac012011-04-16 13:51:05 +02002809 bool from_schedule)
Jens Axboe99e22592011-04-18 09:59:55 +02002810 __releases(q->queue_lock)
Jens Axboe94b5eb22011-04-12 10:12:19 +02002811{
Jens Axboe49cac012011-04-16 13:51:05 +02002812 trace_block_unplug(q, depth, !from_schedule);
Jens Axboe99e22592011-04-18 09:59:55 +02002813
2814 /*
Tejun Heo8ba61432011-12-14 00:33:37 +01002815 * Don't mess with dead queue.
2816 */
2817 if (unlikely(blk_queue_dead(q))) {
2818 spin_unlock(q->queue_lock);
2819 return;
2820 }
2821
2822 /*
Jens Axboe99e22592011-04-18 09:59:55 +02002823 * If we are punting this to kblockd, then we can safely drop
2824 * the queue_lock before waking kblockd (which needs to take
2825 * this lock).
2826 */
2827 if (from_schedule) {
2828 spin_unlock(q->queue_lock);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02002829 blk_run_queue_async(q);
Jens Axboe99e22592011-04-18 09:59:55 +02002830 } else {
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02002831 __blk_run_queue(q);
Jens Axboe99e22592011-04-18 09:59:55 +02002832 spin_unlock(q->queue_lock);
2833 }
2834
Jens Axboe94b5eb22011-04-12 10:12:19 +02002835}
2836
NeilBrown048c9372011-04-18 09:52:22 +02002837static void flush_plug_callbacks(struct blk_plug *plug)
2838{
2839 LIST_HEAD(callbacks);
2840
2841 if (list_empty(&plug->cb_list))
2842 return;
2843
2844 list_splice_init(&plug->cb_list, &callbacks);
2845
2846 while (!list_empty(&callbacks)) {
2847 struct blk_plug_cb *cb = list_first_entry(&callbacks,
2848 struct blk_plug_cb,
2849 list);
2850 list_del(&cb->list);
2851 cb->callback(cb);
2852 }
2853}
2854
Jens Axboe49cac012011-04-16 13:51:05 +02002855void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
Jens Axboe73c10102011-03-08 13:19:51 +01002856{
2857 struct request_queue *q;
2858 unsigned long flags;
2859 struct request *rq;
NeilBrown109b8122011-04-11 14:13:10 +02002860 LIST_HEAD(list);
Jens Axboe94b5eb22011-04-12 10:12:19 +02002861 unsigned int depth;
Jens Axboe73c10102011-03-08 13:19:51 +01002862
2863 BUG_ON(plug->magic != PLUG_MAGIC);
2864
NeilBrown048c9372011-04-18 09:52:22 +02002865 flush_plug_callbacks(plug);
Jens Axboe73c10102011-03-08 13:19:51 +01002866 if (list_empty(&plug->list))
2867 return;
2868
NeilBrown109b8122011-04-11 14:13:10 +02002869 list_splice_init(&plug->list, &list);
2870
2871 if (plug->should_sort) {
2872 list_sort(NULL, &list, plug_rq_cmp);
2873 plug->should_sort = 0;
2874 }
Jens Axboe73c10102011-03-08 13:19:51 +01002875
2876 q = NULL;
Jens Axboe94b5eb22011-04-12 10:12:19 +02002877 depth = 0;
Jens Axboe18811272011-04-12 10:11:24 +02002878
2879 /*
2880 * Save and disable interrupts here, to avoid doing it for every
2881 * queue lock we have to take.
2882 */
Jens Axboe73c10102011-03-08 13:19:51 +01002883 local_irq_save(flags);
NeilBrown109b8122011-04-11 14:13:10 +02002884 while (!list_empty(&list)) {
2885 rq = list_entry_rq(list.next);
Jens Axboe73c10102011-03-08 13:19:51 +01002886 list_del_init(&rq->queuelist);
Jens Axboe73c10102011-03-08 13:19:51 +01002887 BUG_ON(!rq->q);
2888 if (rq->q != q) {
Jens Axboe99e22592011-04-18 09:59:55 +02002889 /*
2890 * This drops the queue lock
2891 */
2892 if (q)
Jens Axboe49cac012011-04-16 13:51:05 +02002893 queue_unplugged(q, depth, from_schedule);
Jens Axboe73c10102011-03-08 13:19:51 +01002894 q = rq->q;
Jens Axboe94b5eb22011-04-12 10:12:19 +02002895 depth = 0;
Jens Axboe73c10102011-03-08 13:19:51 +01002896 spin_lock(q->queue_lock);
2897 }
Tejun Heo8ba61432011-12-14 00:33:37 +01002898
2899 /*
2900 * Short-circuit if @q is dead
2901 */
2902 if (unlikely(blk_queue_dead(q))) {
2903 __blk_end_request_all(rq, -ENODEV);
2904 continue;
2905 }
2906
Jens Axboe73c10102011-03-08 13:19:51 +01002907 /*
2908 * rq is already accounted, so use raw insert
2909 */
Jens Axboe401a18e2011-03-25 16:57:52 +01002910 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA))
2911 __elv_add_request(q, rq, ELEVATOR_INSERT_FLUSH);
2912 else
2913 __elv_add_request(q, rq, ELEVATOR_INSERT_SORT_MERGE);
Jens Axboe94b5eb22011-04-12 10:12:19 +02002914
2915 depth++;
Jens Axboe73c10102011-03-08 13:19:51 +01002916 }
2917
Jens Axboe99e22592011-04-18 09:59:55 +02002918 /*
2919 * This drops the queue lock
2920 */
2921 if (q)
Jens Axboe49cac012011-04-16 13:51:05 +02002922 queue_unplugged(q, depth, from_schedule);
Jens Axboe73c10102011-03-08 13:19:51 +01002923
Jens Axboe73c10102011-03-08 13:19:51 +01002924 local_irq_restore(flags);
2925}
Jens Axboe73c10102011-03-08 13:19:51 +01002926
2927void blk_finish_plug(struct blk_plug *plug)
2928{
Jens Axboef6603782011-04-15 15:49:07 +02002929 blk_flush_plug_list(plug, false);
Christoph Hellwig88b996c2011-04-15 15:20:10 +02002930
2931 if (plug == current->plug)
2932 current->plug = NULL;
Jens Axboe73c10102011-03-08 13:19:51 +01002933}
2934EXPORT_SYMBOL(blk_finish_plug);
2935
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936int __init blk_dev_init(void)
2937{
Nikanth Karthikesan9eb55b02009-04-27 14:53:54 +02002938 BUILD_BUG_ON(__REQ_NR_BITS > 8 *
2939 sizeof(((struct request *)0)->cmd_flags));
2940
Tejun Heo89b90be2011-01-03 15:01:47 +01002941 /* used for unplugging and affects IO latency/throughput - HIGHPRI */
2942 kblockd_workqueue = alloc_workqueue("kblockd",
2943 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 if (!kblockd_workqueue)
2945 panic("Failed to create kblockd\n");
2946
2947 request_cachep = kmem_cache_create("blkdev_requests",
Paul Mundt20c2df82007-07-20 10:11:58 +09002948 sizeof(struct request), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949
Jens Axboe8324aa92008-01-29 14:51:59 +01002950 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02002951 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 return 0;
2954}