blob: c37e9e7c9d0785baf67b39a28ba0882a83d969c0 [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"
37
Mike Snitzerd07335e2010-11-16 12:52:38 +010038EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_remap);
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +020039EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_remap);
Li Zefan55782132009-06-09 13:43:05 +080040EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_complete);
Ingo Molnar0bfc2452008-11-26 11:59:56 +010041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/*
43 * For the allocated request tables
44 */
Adrian Bunk5ece6c52008-02-18 13:45:51 +010045static struct kmem_cache *request_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47/*
48 * For queue allocation
49 */
Jens Axboe6728cb02008-01-31 13:03:55 +010050struct kmem_cache *blk_requestq_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 * Controlling structure to kblockd
54 */
Jens Axboeff856ba2006-01-09 16:02:34 +010055static struct workqueue_struct *kblockd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Jens Axboe26b82562008-01-29 13:54:41 +010057static void drive_stat_acct(struct request *rq, int new_io)
58{
Jens Axboe28f13702008-05-07 10:15:46 +020059 struct hd_struct *part;
Jens Axboe26b82562008-01-29 13:54:41 +010060 int rw = rq_data_dir(rq);
Tejun Heoc9959052008-08-25 19:47:21 +090061 int cpu;
Jens Axboe26b82562008-01-29 13:54:41 +010062
Jens Axboec2553b52009-04-24 08:10:11 +020063 if (!blk_do_io_stat(rq))
Jens Axboe26b82562008-01-29 13:54:41 +010064 return;
65
Tejun Heo074a7ac2008-08-25 19:56:14 +090066 cpu = part_stat_lock();
Tejun Heoc9959052008-08-25 19:47:21 +090067
Jerome Marchand09e099d2011-01-05 16:57:38 +010068 if (!new_io) {
69 part = rq->part;
Tejun Heo074a7ac2008-08-25 19:56:14 +090070 part_stat_inc(cpu, part, merges[rw]);
Jerome Marchand09e099d2011-01-05 16:57:38 +010071 } else {
72 part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq));
Jens Axboe6c23a962011-01-07 08:43:37 +010073 if (!hd_struct_try_get(part)) {
Jerome Marchand09e099d2011-01-05 16:57:38 +010074 /*
75 * The partition is already being removed,
76 * the request will be accounted on the disk only
77 *
78 * We take a reference on disk->part0 although that
79 * partition will never be deleted, so we can treat
80 * it as any other partition.
81 */
82 part = &rq->rq_disk->part0;
Jens Axboe6c23a962011-01-07 08:43:37 +010083 hd_struct_get(part);
Jerome Marchand09e099d2011-01-05 16:57:38 +010084 }
Tejun Heo074a7ac2008-08-25 19:56:14 +090085 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +020086 part_inc_in_flight(part, rw);
Jerome Marchand09e099d2011-01-05 16:57:38 +010087 rq->part = part;
Jens Axboe26b82562008-01-29 13:54:41 +010088 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +020089
Tejun Heo074a7ac2008-08-25 19:56:14 +090090 part_stat_unlock();
Jens Axboe26b82562008-01-29 13:54:41 +010091}
92
Jens Axboe8324aa92008-01-29 14:51:59 +010093void blk_queue_congestion_threshold(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
95 int nr;
96
97 nr = q->nr_requests - (q->nr_requests / 8) + 1;
98 if (nr > q->nr_requests)
99 nr = q->nr_requests;
100 q->nr_congestion_on = nr;
101
102 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
103 if (nr < 1)
104 nr = 1;
105 q->nr_congestion_off = nr;
106}
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108/**
109 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
110 * @bdev: device
111 *
112 * Locates the passed device's request queue and returns the address of its
113 * backing_dev_info
114 *
115 * Will return NULL if the request queue cannot be located.
116 */
117struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
118{
119 struct backing_dev_info *ret = NULL;
Jens Axboe165125e2007-07-24 09:28:11 +0200120 struct request_queue *q = bdev_get_queue(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 if (q)
123 ret = &q->backing_dev_info;
124 return ret;
125}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126EXPORT_SYMBOL(blk_get_backing_dev_info);
127
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200128void blk_rq_init(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200130 memset(rq, 0, sizeof(*rq));
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 INIT_LIST_HEAD(&rq->queuelist);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700133 INIT_LIST_HEAD(&rq->timeout_list);
Jens Axboec7c22e42008-09-13 20:26:01 +0200134 rq->cpu = -1;
Jens Axboe63a71382008-02-08 12:41:03 +0100135 rq->q = q;
Tejun Heoa2dec7b2009-05-07 22:24:44 +0900136 rq->__sector = (sector_t) -1;
Jens Axboe2e662b62006-07-13 11:55:04 +0200137 INIT_HLIST_NODE(&rq->hash);
138 RB_CLEAR_NODE(&rq->rb_node);
FUJITA Tomonorid7e3c322008-04-29 09:54:39 +0200139 rq->cmd = rq->__cmd;
Li Zefane2494e12009-04-02 13:43:26 +0800140 rq->cmd_len = BLK_MAX_CDB;
Jens Axboe63a71382008-02-08 12:41:03 +0100141 rq->tag = -1;
Jens Axboe63a71382008-02-08 12:41:03 +0100142 rq->ref_count = 1;
Tejun Heob243ddc2009-04-23 11:05:18 +0900143 rq->start_time = jiffies;
Divyesh Shah91952912010-04-01 15:01:41 -0700144 set_start_time_ns(rq);
Jerome Marchand09e099d2011-01-05 16:57:38 +0100145 rq->part = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200147EXPORT_SYMBOL(blk_rq_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
NeilBrown5bb23a62007-09-27 12:46:13 +0200149static void req_bio_endio(struct request *rq, struct bio *bio,
150 unsigned int nbytes, int error)
Tejun Heo797e7db2006-01-06 09:51:03 +0100151{
Tejun Heo143a87f2011-01-25 12:43:52 +0100152 if (error)
153 clear_bit(BIO_UPTODATE, &bio->bi_flags);
154 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
155 error = -EIO;
Tejun Heo797e7db2006-01-06 09:51:03 +0100156
Tejun Heo143a87f2011-01-25 12:43:52 +0100157 if (unlikely(nbytes > bio->bi_size)) {
158 printk(KERN_ERR "%s: want %u bytes done, %u left\n",
159 __func__, nbytes, bio->bi_size);
160 nbytes = bio->bi_size;
NeilBrown5bb23a62007-09-27 12:46:13 +0200161 }
Tejun Heo143a87f2011-01-25 12:43:52 +0100162
163 if (unlikely(rq->cmd_flags & REQ_QUIET))
164 set_bit(BIO_QUIET, &bio->bi_flags);
165
166 bio->bi_size -= nbytes;
167 bio->bi_sector += (nbytes >> 9);
168
169 if (bio_integrity(bio))
170 bio_integrity_advance(bio, nbytes);
171
172 /* don't actually finish bio if it's part of flush sequence */
173 if (bio->bi_size == 0 && !(rq->cmd_flags & REQ_FLUSH_SEQ))
174 bio_endio(bio, error);
Tejun Heo797e7db2006-01-06 09:51:03 +0100175}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177void blk_dump_rq_flags(struct request *rq, char *msg)
178{
179 int bit;
180
Jens Axboe6728cb02008-01-31 13:03:55 +0100181 printk(KERN_INFO "%s: dev %s: type=%x, flags=%x\n", msg,
Jens Axboe4aff5e22006-08-10 08:44:47 +0200182 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
183 rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Tejun Heo83096eb2009-05-07 22:24:39 +0900185 printk(KERN_INFO " sector %llu, nr/cnr %u/%u\n",
186 (unsigned long long)blk_rq_pos(rq),
187 blk_rq_sectors(rq), blk_rq_cur_sectors(rq));
Tejun Heo731ec492009-04-23 11:05:20 +0900188 printk(KERN_INFO " bio %p, biotail %p, buffer %p, len %u\n",
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900189 rq->bio, rq->biotail, rq->buffer, blk_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200191 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
Jens Axboe6728cb02008-01-31 13:03:55 +0100192 printk(KERN_INFO " cdb: ");
FUJITA Tomonorid34c87e2008-04-29 14:37:52 +0200193 for (bit = 0; bit < BLK_MAX_CDB; bit++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 printk("%02x ", rq->cmd[bit]);
195 printk("\n");
196 }
197}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198EXPORT_SYMBOL(blk_dump_rq_flags);
199
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500200static void blk_delay_work(struct work_struct *work)
Jens Axboe6c5e0c42008-08-01 20:31:32 +0200201{
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500202 struct request_queue *q;
Jens Axboe6c5e0c42008-08-01 20:31:32 +0200203
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500204 q = container_of(work, struct request_queue, delay_work.work);
205 spin_lock_irq(q->queue_lock);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200206 __blk_run_queue(q);
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500207 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210/**
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500211 * blk_delay_queue - restart queueing after defined interval
212 * @q: The &struct request_queue in question
213 * @msecs: Delay in msecs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 *
215 * Description:
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500216 * Sometimes queueing needs to be postponed for a little while, to allow
217 * resources to come back. This function will make sure that queueing is
218 * restarted around the specified time.
219 */
220void blk_delay_queue(struct request_queue *q, unsigned long msecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Jens Axboe4521cc42011-04-18 11:36:39 +0200222 queue_delayed_work(kblockd_workqueue, &q->delay_work,
223 msecs_to_jiffies(msecs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500225EXPORT_SYMBOL(blk_delay_queue);
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227/**
228 * blk_start_queue - restart a previously stopped queue
Jens Axboe165125e2007-07-24 09:28:11 +0200229 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 *
231 * Description:
232 * blk_start_queue() will clear the stop flag on the queue, and call
233 * the request_fn for the queue if it was in a stopped state when
234 * entered. Also see blk_stop_queue(). Queue lock must be held.
235 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200236void blk_start_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200238 WARN_ON(!irqs_disabled());
239
Nick Piggin75ad23b2008-04-29 14:48:33 +0200240 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200241 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243EXPORT_SYMBOL(blk_start_queue);
244
245/**
246 * blk_stop_queue - stop a queue
Jens Axboe165125e2007-07-24 09:28:11 +0200247 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 *
249 * Description:
250 * The Linux block layer assumes that a block driver will consume all
251 * entries on the request queue when the request_fn strategy is called.
252 * Often this will not happen, because of hardware limitations (queue
253 * depth settings). If a device driver gets a 'queue full' response,
254 * or if it simply chooses not to queue more I/O at one point, it can
255 * call this function to prevent the request_fn from being called until
256 * the driver has signalled it's ready to go again. This happens by calling
257 * blk_start_queue() to restart queue operations. Queue lock must be held.
258 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200259void blk_stop_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Jens Axboead3d9d72011-03-25 16:58:59 +0100261 __cancel_delayed_work(&q->delay_work);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200262 queue_flag_set(QUEUE_FLAG_STOPPED, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264EXPORT_SYMBOL(blk_stop_queue);
265
266/**
267 * blk_sync_queue - cancel any pending callbacks on a queue
268 * @q: the queue
269 *
270 * Description:
271 * The block layer may perform asynchronous callback activity
272 * on a queue, such as calling the unplug function after a timeout.
273 * A block device may call blk_sync_queue to ensure that any
274 * such activity is cancelled, thus allowing it to release resources
Michael Opdenacker59c51592007-05-09 08:57:56 +0200275 * that the callbacks might use. The caller must already have made sure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 * that its ->make_request_fn will not re-add plugging prior to calling
277 * this function.
278 *
Vivek Goyalda527772011-03-02 19:05:33 -0500279 * This function does not cancel any asynchronous activity arising
280 * out of elevator or throttling code. That would require elevaotor_exit()
281 * and blk_throtl_exit() to be called with queue lock initialized.
282 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 */
284void blk_sync_queue(struct request_queue *q)
285{
Jens Axboe70ed28b2008-11-19 14:38:39 +0100286 del_timer_sync(&q->timeout);
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500287 cancel_delayed_work_sync(&q->delay_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289EXPORT_SYMBOL(blk_sync_queue);
290
291/**
Jens Axboe80a4b582008-10-14 09:51:06 +0200292 * __blk_run_queue - run a single device queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200294 *
295 * Description:
296 * See @blk_run_queue. This variant must be called with the queue lock
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200297 * held and interrupts disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 */
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200299void __blk_run_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Tejun Heoa538cd02009-04-23 11:05:17 +0900301 if (unlikely(blk_queue_stopped(q)))
302 return;
303
Jens Axboec21e6be2011-04-19 13:32:46 +0200304 q->request_fn(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200305}
306EXPORT_SYMBOL(__blk_run_queue);
Jens Axboedac07ec2006-05-11 08:20:16 +0200307
Nick Piggin75ad23b2008-04-29 14:48:33 +0200308/**
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200309 * blk_run_queue_async - run a single device queue in workqueue context
310 * @q: The queue to run
311 *
312 * Description:
313 * Tells kblockd to perform the equivalent of @blk_run_queue on behalf
314 * of us.
315 */
316void blk_run_queue_async(struct request_queue *q)
317{
Shaohua Li3ec717b2011-05-18 11:22:43 +0200318 if (likely(!blk_queue_stopped(q))) {
319 __cancel_delayed_work(&q->delay_work);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200320 queue_delayed_work(kblockd_workqueue, &q->delay_work, 0);
Shaohua Li3ec717b2011-05-18 11:22:43 +0200321 }
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200322}
Jens Axboec21e6be2011-04-19 13:32:46 +0200323EXPORT_SYMBOL(blk_run_queue_async);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200324
325/**
Nick Piggin75ad23b2008-04-29 14:48:33 +0200326 * blk_run_queue - run a single device queue
327 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200328 *
329 * Description:
330 * Invoke request handling on this queue, if it has pending work to do.
Tejun Heoa7f55792009-04-23 11:05:17 +0900331 * May be used to restart queueing when a request has completed.
Nick Piggin75ad23b2008-04-29 14:48:33 +0200332 */
333void blk_run_queue(struct request_queue *q)
334{
335 unsigned long flags;
336
337 spin_lock_irqsave(q->queue_lock, flags);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200338 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 spin_unlock_irqrestore(q->queue_lock, flags);
340}
341EXPORT_SYMBOL(blk_run_queue);
342
Jens Axboe165125e2007-07-24 09:28:11 +0200343void blk_put_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500344{
345 kobject_put(&q->kobj);
346}
Jens Axboed86e0e82011-05-27 07:44:43 +0200347EXPORT_SYMBOL(blk_put_queue);
Al Viro483f4af2006-03-18 18:34:37 -0500348
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200349/**
350 * blk_drain_queue - drain requests from request_queue
351 * @q: queue to drain
Tejun Heoc9a929d2011-10-19 14:42:16 +0200352 * @drain_all: whether to drain all requests or only the ones w/ ELVPRIV
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200353 *
Tejun Heoc9a929d2011-10-19 14:42:16 +0200354 * Drain requests from @q. If @drain_all is set, all requests are drained.
355 * If not, only ELVPRIV requests are drained. The caller is responsible
356 * for ensuring that no new requests which need to be drained are queued.
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200357 */
Tejun Heoc9a929d2011-10-19 14:42:16 +0200358void blk_drain_queue(struct request_queue *q, bool drain_all)
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200359{
360 while (true) {
Tejun Heo481a7d62011-12-14 00:33:37 +0100361 bool drain = false;
362 int i;
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200363
364 spin_lock_irq(q->queue_lock);
365
366 elv_drain_elevator(q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200367 if (drain_all)
368 blk_throtl_drain(q);
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200369
370 __blk_run_queue(q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200371
Tejun Heo481a7d62011-12-14 00:33:37 +0100372 drain |= q->rq.elvpriv;
373
374 /*
375 * Unfortunately, requests are queued at and tracked from
376 * multiple places and there's no single counter which can
377 * be drained. Check all the queues and counters.
378 */
379 if (drain_all) {
380 drain |= !list_empty(&q->queue_head);
381 for (i = 0; i < 2; i++) {
382 drain |= q->rq.count[i];
383 drain |= q->in_flight[i];
384 drain |= !list_empty(&q->flush_queue[i]);
385 }
386 }
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200387
388 spin_unlock_irq(q->queue_lock);
389
Tejun Heo481a7d62011-12-14 00:33:37 +0100390 if (!drain)
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200391 break;
392 msleep(10);
393 }
394}
395
Tejun Heoc9a929d2011-10-19 14:42:16 +0200396/**
397 * blk_cleanup_queue - shutdown a request queue
398 * @q: request queue to shutdown
399 *
400 * Mark @q DEAD, drain all pending requests, destroy and put it. All
401 * future requests will be failed immediately with -ENODEV.
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500402 */
Jens Axboe6728cb02008-01-31 13:03:55 +0100403void blk_cleanup_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500404{
Tejun Heoc9a929d2011-10-19 14:42:16 +0200405 spinlock_t *lock = q->queue_lock;
Jens Axboee3335de92008-09-18 09:22:54 -0700406
Tejun Heoc9a929d2011-10-19 14:42:16 +0200407 /* mark @q DEAD, no new request or merges will be allowed afterwards */
Al Viro483f4af2006-03-18 18:34:37 -0500408 mutex_lock(&q->sysfs_lock);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200409 queue_flag_set_unlocked(QUEUE_FLAG_DEAD, q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200410
411 spin_lock_irq(lock);
412 queue_flag_set(QUEUE_FLAG_NOMERGES, q);
413 queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
414 queue_flag_set(QUEUE_FLAG_DEAD, q);
Al Viro483f4af2006-03-18 18:34:37 -0500415
Hannes Reinecke777eb1b2011-09-28 08:07:01 -0600416 if (q->queue_lock != &q->__queue_lock)
417 q->queue_lock = &q->__queue_lock;
Vivek Goyalda527772011-03-02 19:05:33 -0500418
Tejun Heoc9a929d2011-10-19 14:42:16 +0200419 spin_unlock_irq(lock);
420 mutex_unlock(&q->sysfs_lock);
421
Tejun Heo6dd9ad72011-11-03 18:52:11 +0100422 /*
423 * Drain all requests queued before DEAD marking. The caller might
424 * be trying to tear down @q before its elevator is initialized, in
425 * which case we don't want to call into draining.
426 */
427 if (q->elevator)
428 blk_drain_queue(q, true);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200429
430 /* @q won't process any more request, flush async actions */
431 del_timer_sync(&q->backing_dev_info.laptop_mode_wb_timer);
432 blk_sync_queue(q);
433
434 /* @q is and will stay empty, shutdown and put */
Al Viro483f4af2006-03-18 18:34:37 -0500435 blk_put_queue(q);
436}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437EXPORT_SYMBOL(blk_cleanup_queue);
438
Jens Axboe165125e2007-07-24 09:28:11 +0200439static int blk_init_free_list(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
441 struct request_list *rl = &q->rq;
442
Mike Snitzer1abec4f2010-05-25 13:15:15 -0400443 if (unlikely(rl->rq_pool))
444 return 0;
445
Jens Axboe1faa16d2009-04-06 14:48:01 +0200446 rl->count[BLK_RW_SYNC] = rl->count[BLK_RW_ASYNC] = 0;
447 rl->starved[BLK_RW_SYNC] = rl->starved[BLK_RW_ASYNC] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200448 rl->elvpriv = 0;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200449 init_waitqueue_head(&rl->wait[BLK_RW_SYNC]);
450 init_waitqueue_head(&rl->wait[BLK_RW_ASYNC]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Christoph Lameter19460892005-06-23 00:08:19 -0700452 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
453 mempool_free_slab, request_cachep, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 if (!rl->rq_pool)
456 return -ENOMEM;
457
458 return 0;
459}
460
Jens Axboe165125e2007-07-24 09:28:11 +0200461struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Christoph Lameter19460892005-06-23 00:08:19 -0700463 return blk_alloc_queue_node(gfp_mask, -1);
464}
465EXPORT_SYMBOL(blk_alloc_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Jens Axboe165125e2007-07-24 09:28:11 +0200467struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -0700468{
Jens Axboe165125e2007-07-24 09:28:11 +0200469 struct request_queue *q;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700470 int err;
Christoph Lameter19460892005-06-23 00:08:19 -0700471
Jens Axboe8324aa92008-01-29 14:51:59 +0100472 q = kmem_cache_alloc_node(blk_requestq_cachep,
Christoph Lameter94f60302007-07-17 04:03:29 -0700473 gfp_mask | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (!q)
475 return NULL;
476
Jens Axboe0989a022009-06-12 14:42:56 +0200477 q->backing_dev_info.ra_pages =
478 (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
479 q->backing_dev_info.state = 0;
480 q->backing_dev_info.capabilities = BDI_CAP_MAP_COPY;
Jens Axboed9938312009-06-12 14:45:52 +0200481 q->backing_dev_info.name = "block";
Jens Axboe0989a022009-06-12 14:42:56 +0200482
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700483 err = bdi_init(&q->backing_dev_info);
484 if (err) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100485 kmem_cache_free(blk_requestq_cachep, q);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700486 return NULL;
487 }
488
Vivek Goyale43473b2010-09-15 17:06:35 -0400489 if (blk_throtl_init(q)) {
490 kmem_cache_free(blk_requestq_cachep, q);
491 return NULL;
492 }
493
Matthew Garrett31373d02010-04-06 14:25:14 +0200494 setup_timer(&q->backing_dev_info.laptop_mode_wb_timer,
495 laptop_mode_timer_fn, (unsigned long) q);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700496 setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
497 INIT_LIST_HEAD(&q->timeout_list);
Tejun Heoae1b1532011-01-25 12:43:54 +0100498 INIT_LIST_HEAD(&q->flush_queue[0]);
499 INIT_LIST_HEAD(&q->flush_queue[1]);
500 INIT_LIST_HEAD(&q->flush_data_in_flight);
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500501 INIT_DELAYED_WORK(&q->delay_work, blk_delay_work);
Al Viro483f4af2006-03-18 18:34:37 -0500502
Jens Axboe8324aa92008-01-29 14:51:59 +0100503 kobject_init(&q->kobj, &blk_queue_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Al Viro483f4af2006-03-18 18:34:37 -0500505 mutex_init(&q->sysfs_lock);
Neil Browne7e72bf2008-05-14 16:05:54 -0700506 spin_lock_init(&q->__queue_lock);
Al Viro483f4af2006-03-18 18:34:37 -0500507
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500508 /*
509 * By default initialize queue_lock to internal lock and driver can
510 * override it later if need be.
511 */
512 q->queue_lock = &q->__queue_lock;
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return q;
515}
Christoph Lameter19460892005-06-23 00:08:19 -0700516EXPORT_SYMBOL(blk_alloc_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518/**
519 * blk_init_queue - prepare a request queue for use with a block device
520 * @rfn: The function to be called to process requests that have been
521 * placed on the queue.
522 * @lock: Request queue spin lock
523 *
524 * Description:
525 * If a block device wishes to use the standard request handling procedures,
526 * which sorts requests and coalesces adjacent requests, then it must
527 * call blk_init_queue(). The function @rfn will be called when there
528 * are requests on the queue that need to be processed. If the device
529 * supports plugging, then @rfn may not be called immediately when requests
530 * are available on the queue, but may be called at some time later instead.
531 * Plugged queues are generally unplugged when a buffer belonging to one
532 * of the requests on the queue is needed, or due to memory pressure.
533 *
534 * @rfn is not required, or even expected, to remove all requests off the
535 * queue, but only as many as it can handle at a time. If it does leave
536 * requests on the queue, it is responsible for arranging that the requests
537 * get dealt with eventually.
538 *
539 * The queue spin lock must be held while manipulating the requests on the
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200540 * request queue; this lock will be taken also from interrupt context, so irq
541 * disabling is needed for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 *
Randy Dunlap710027a2008-08-19 20:13:11 +0200543 * Function returns a pointer to the initialized request queue, or %NULL if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 * it didn't succeed.
545 *
546 * Note:
547 * blk_init_queue() must be paired with a blk_cleanup_queue() call
548 * when the block device is deactivated (such as at module unload).
549 **/
Christoph Lameter19460892005-06-23 00:08:19 -0700550
Jens Axboe165125e2007-07-24 09:28:11 +0200551struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Christoph Lameter19460892005-06-23 00:08:19 -0700553 return blk_init_queue_node(rfn, lock, -1);
554}
555EXPORT_SYMBOL(blk_init_queue);
556
Jens Axboe165125e2007-07-24 09:28:11 +0200557struct request_queue *
Christoph Lameter19460892005-06-23 00:08:19 -0700558blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
559{
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600560 struct request_queue *uninit_q, *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600562 uninit_q = blk_alloc_queue_node(GFP_KERNEL, node_id);
563 if (!uninit_q)
564 return NULL;
565
566 q = blk_init_allocated_queue_node(uninit_q, rfn, lock, node_id);
567 if (!q)
568 blk_cleanup_queue(uninit_q);
569
570 return q;
Mike Snitzer01effb02010-05-11 08:57:42 +0200571}
572EXPORT_SYMBOL(blk_init_queue_node);
573
574struct request_queue *
575blk_init_allocated_queue(struct request_queue *q, request_fn_proc *rfn,
576 spinlock_t *lock)
577{
578 return blk_init_allocated_queue_node(q, rfn, lock, -1);
579}
580EXPORT_SYMBOL(blk_init_allocated_queue);
581
582struct request_queue *
583blk_init_allocated_queue_node(struct request_queue *q, request_fn_proc *rfn,
584 spinlock_t *lock, int node_id)
585{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (!q)
587 return NULL;
588
Christoph Lameter19460892005-06-23 00:08:19 -0700589 q->node = node_id;
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600590 if (blk_init_free_list(q))
Al Viro8669aaf2006-03-18 13:50:00 -0500591 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 q->request_fn = rfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 q->prep_rq_fn = NULL;
James Bottomley28018c22010-07-01 19:49:17 +0900595 q->unprep_rq_fn = NULL;
Jens Axboebc58ba92009-01-23 10:54:44 +0100596 q->queue_flags = QUEUE_FLAG_DEFAULT;
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500597
598 /* Override internal queue lock with supplied lock pointer */
599 if (lock)
600 q->queue_lock = lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Jens Axboef3b144a2009-03-06 08:48:33 +0100602 /*
603 * This also sets hw/phys segments, boundary and size
604 */
Jens Axboec20e8de2011-09-12 12:03:37 +0200605 blk_queue_make_request(q, blk_queue_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Alan Stern44ec9542007-02-20 11:01:57 -0500607 q->sg_reserved_size = INT_MAX;
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 /*
610 * all done
611 */
612 if (!elevator_init(q, NULL)) {
613 blk_queue_congestion_threshold(q);
614 return q;
615 }
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return NULL;
618}
Mike Snitzer01effb02010-05-11 08:57:42 +0200619EXPORT_SYMBOL(blk_init_allocated_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Jens Axboe165125e2007-07-24 09:28:11 +0200621int blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Tejun Heo34f60552011-12-14 00:33:37 +0100623 if (likely(!blk_queue_dead(q))) {
Al Viro483f4af2006-03-18 18:34:37 -0500624 kobject_get(&q->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return 0;
626 }
627
628 return 1;
629}
Jens Axboed86e0e82011-05-27 07:44:43 +0200630EXPORT_SYMBOL(blk_get_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Jens Axboe165125e2007-07-24 09:28:11 +0200632static inline void blk_free_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Jens Axboe4aff5e22006-08-10 08:44:47 +0200634 if (rq->cmd_flags & REQ_ELVPRIV)
Tejun Heocb98fc82005-10-28 08:29:39 +0200635 elv_put_request(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 mempool_free(rq, q->rq.rq_pool);
637}
638
Jens Axboe1ea25ecb2006-07-18 22:24:11 +0200639static struct request *
Tejun Heo75eb6c32011-10-19 14:31:22 +0200640blk_alloc_request(struct request_queue *q, unsigned int flags, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
642 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
643
644 if (!rq)
645 return NULL;
646
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200647 blk_rq_init(q, rq);
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200648
Jerome Marchand42dad762009-04-22 14:01:49 +0200649 rq->cmd_flags = flags | REQ_ALLOCED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Tejun Heo75eb6c32011-10-19 14:31:22 +0200651 if ((flags & REQ_ELVPRIV) &&
652 unlikely(elv_set_request(q, rq, gfp_mask))) {
653 mempool_free(rq, q->rq.rq_pool);
654 return NULL;
Tejun Heocb98fc82005-10-28 08:29:39 +0200655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Tejun Heocb98fc82005-10-28 08:29:39 +0200657 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
660/*
661 * ioc_batching returns true if the ioc is a valid batching request and
662 * should be given priority access to a request.
663 */
Jens Axboe165125e2007-07-24 09:28:11 +0200664static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
666 if (!ioc)
667 return 0;
668
669 /*
670 * Make sure the process is able to allocate at least 1 request
671 * even if the batch times out, otherwise we could theoretically
672 * lose wakeups.
673 */
674 return ioc->nr_batch_requests == q->nr_batching ||
675 (ioc->nr_batch_requests > 0
676 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
677}
678
679/*
680 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
681 * will cause the process to be a "batcher" on all queues in the system. This
682 * is the behaviour we want though - once it gets a wakeup it should be given
683 * a nice run.
684 */
Jens Axboe165125e2007-07-24 09:28:11 +0200685static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
687 if (!ioc || ioc_batching(q, ioc))
688 return;
689
690 ioc->nr_batch_requests = q->nr_batching;
691 ioc->last_waited = jiffies;
692}
693
Jens Axboe1faa16d2009-04-06 14:48:01 +0200694static void __freed_request(struct request_queue *q, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
696 struct request_list *rl = &q->rq;
697
Jens Axboe1faa16d2009-04-06 14:48:01 +0200698 if (rl->count[sync] < queue_congestion_off_threshold(q))
699 blk_clear_queue_congested(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Jens Axboe1faa16d2009-04-06 14:48:01 +0200701 if (rl->count[sync] + 1 <= q->nr_requests) {
702 if (waitqueue_active(&rl->wait[sync]))
703 wake_up(&rl->wait[sync]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Jens Axboe1faa16d2009-04-06 14:48:01 +0200705 blk_clear_queue_full(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
707}
708
709/*
710 * A request has just been released. Account for it, update the full and
711 * congestion status, wake up any waiters. Called under q->queue_lock.
712 */
Tejun Heo75eb6c32011-10-19 14:31:22 +0200713static void freed_request(struct request_queue *q, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
715 struct request_list *rl = &q->rq;
Tejun Heo75eb6c32011-10-19 14:31:22 +0200716 int sync = rw_is_sync(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Jens Axboe1faa16d2009-04-06 14:48:01 +0200718 rl->count[sync]--;
Tejun Heo75eb6c32011-10-19 14:31:22 +0200719 if (flags & REQ_ELVPRIV)
Tejun Heocb98fc82005-10-28 08:29:39 +0200720 rl->elvpriv--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Jens Axboe1faa16d2009-04-06 14:48:01 +0200722 __freed_request(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Jens Axboe1faa16d2009-04-06 14:48:01 +0200724 if (unlikely(rl->starved[sync ^ 1]))
725 __freed_request(q, sync ^ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728/*
Mike Snitzer9d5a4e92011-02-11 11:05:46 +0100729 * Determine if elevator data should be initialized when allocating the
730 * request associated with @bio.
731 */
732static bool blk_rq_should_init_elevator(struct bio *bio)
733{
734 if (!bio)
735 return true;
736
737 /*
738 * Flush requests do not use the elevator so skip initialization.
739 * This allows a request to share the flush and elevator data.
740 */
741 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA))
742 return false;
743
744 return true;
745}
746
Tejun Heoda8303c2011-10-19 14:33:05 +0200747/**
748 * get_request - get a free request
749 * @q: request_queue to allocate request from
750 * @rw_flags: RW and SYNC flags
751 * @bio: bio to allocate request for (can be %NULL)
752 * @gfp_mask: allocation mask
753 *
754 * Get a free request from @q. This function may fail under memory
755 * pressure or if @q is dead.
756 *
757 * Must be callled with @q->queue_lock held and,
758 * Returns %NULL on failure, with @q->queue_lock held.
759 * Returns !%NULL on success, with @q->queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 */
Jens Axboe165125e2007-07-24 09:28:11 +0200761static struct request *get_request(struct request_queue *q, int rw_flags,
Jens Axboe7749a8d2006-12-13 13:02:26 +0100762 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
764 struct request *rq = NULL;
765 struct request_list *rl = &q->rq;
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100766 struct io_context *ioc = NULL;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200767 const bool is_sync = rw_is_sync(rw_flags) != 0;
Tejun Heo75eb6c32011-10-19 14:31:22 +0200768 int may_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Tejun Heo34f60552011-12-14 00:33:37 +0100770 if (unlikely(blk_queue_dead(q)))
Tejun Heoda8303c2011-10-19 14:33:05 +0200771 return NULL;
772
Jens Axboe7749a8d2006-12-13 13:02:26 +0100773 may_queue = elv_may_queue(q, rw_flags);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100774 if (may_queue == ELV_MQUEUE_NO)
775 goto rq_starved;
776
Jens Axboe1faa16d2009-04-06 14:48:01 +0200777 if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) {
778 if (rl->count[is_sync]+1 >= q->nr_requests) {
Jens Axboeb5deef92006-07-19 23:39:40 +0200779 ioc = current_io_context(GFP_ATOMIC, q->node);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100780 /*
781 * The queue will fill after this allocation, so set
782 * it as full, and mark this process as "batching".
783 * This process will be allowed to complete a batch of
784 * requests, others will be blocked.
785 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200786 if (!blk_queue_full(q, is_sync)) {
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100787 ioc_set_batching(q, ioc);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200788 blk_set_queue_full(q, is_sync);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100789 } else {
790 if (may_queue != ELV_MQUEUE_MUST
791 && !ioc_batching(q, ioc)) {
792 /*
793 * The queue is full and the allocating
794 * process is not a "batcher", and not
795 * exempted by the IO scheduler
796 */
797 goto out;
798 }
799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
Jens Axboe1faa16d2009-04-06 14:48:01 +0200801 blk_set_queue_congested(q, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
803
Jens Axboe082cf692005-06-28 16:35:11 +0200804 /*
805 * Only allow batching queuers to allocate up to 50% over the defined
806 * limit of requests, otherwise we could have thousands of requests
807 * allocated with any setting of ->nr_requests
808 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200809 if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
Jens Axboe082cf692005-06-28 16:35:11 +0200810 goto out;
Hugh Dickinsfd782a42005-06-29 15:15:40 +0100811
Jens Axboe1faa16d2009-04-06 14:48:01 +0200812 rl->count[is_sync]++;
813 rl->starved[is_sync] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200814
Tejun Heo75eb6c32011-10-19 14:31:22 +0200815 if (blk_rq_should_init_elevator(bio) &&
816 !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags)) {
817 rw_flags |= REQ_ELVPRIV;
818 rl->elvpriv++;
Mike Snitzer9d5a4e92011-02-11 11:05:46 +0100819 }
Tejun Heocb98fc82005-10-28 08:29:39 +0200820
Jens Axboef253b862010-10-24 22:06:02 +0200821 if (blk_queue_io_stat(q))
822 rw_flags |= REQ_IO_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 spin_unlock_irq(q->queue_lock);
824
Tejun Heo75eb6c32011-10-19 14:31:22 +0200825 rq = blk_alloc_request(q, rw_flags, gfp_mask);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100826 if (unlikely(!rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 /*
828 * Allocation failed presumably due to memory. Undo anything
829 * we might have messed up.
830 *
831 * Allocating task should really be put onto the front of the
832 * wait queue, but this is pretty rare.
833 */
834 spin_lock_irq(q->queue_lock);
Tejun Heo75eb6c32011-10-19 14:31:22 +0200835 freed_request(q, rw_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 /*
838 * in the very unlikely event that allocation failed and no
839 * requests for this direction was pending, mark us starved
840 * so that freeing of a request in the other direction will
841 * notice us. another possible fix would be to split the
842 * rq mempool into READ and WRITE
843 */
844rq_starved:
Jens Axboe1faa16d2009-04-06 14:48:01 +0200845 if (unlikely(rl->count[is_sync] == 0))
846 rl->starved[is_sync] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 goto out;
849 }
850
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100851 /*
852 * ioc may be NULL here, and ioc_batching will be false. That's
853 * OK, if the queue is under the request limit then requests need
854 * not count toward the nr_batch_requests limit. There will always
855 * be some limit enforced by BLK_BATCH_TIME.
856 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (ioc_batching(q, ioc))
858 ioc->nr_batch_requests--;
Jens Axboe6728cb02008-01-31 13:03:55 +0100859
Jens Axboe1faa16d2009-04-06 14:48:01 +0200860 trace_block_getrq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return rq;
863}
864
Tejun Heoda8303c2011-10-19 14:33:05 +0200865/**
866 * get_request_wait - get a free request with retry
867 * @q: request_queue to allocate request from
868 * @rw_flags: RW and SYNC flags
869 * @bio: bio to allocate request for (can be %NULL)
Nick Piggind6344532005-06-28 20:45:14 -0700870 *
Tejun Heoda8303c2011-10-19 14:33:05 +0200871 * Get a free request from @q. This function keeps retrying under memory
872 * pressure and fails iff @q is dead.
873 *
874 * Must be callled with @q->queue_lock held and,
875 * Returns %NULL on failure, with @q->queue_lock held.
876 * Returns !%NULL on success, with @q->queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 */
Jens Axboe165125e2007-07-24 09:28:11 +0200878static struct request *get_request_wait(struct request_queue *q, int rw_flags,
Jens Axboe22e2c502005-06-27 10:55:12 +0200879 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Jens Axboe1faa16d2009-04-06 14:48:01 +0200881 const bool is_sync = rw_is_sync(rw_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 struct request *rq;
883
Jens Axboe7749a8d2006-12-13 13:02:26 +0100884 rq = get_request(q, rw_flags, bio, GFP_NOIO);
Nick Piggin450991b2005-06-28 20:45:13 -0700885 while (!rq) {
886 DEFINE_WAIT(wait);
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200887 struct io_context *ioc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 struct request_list *rl = &q->rq;
889
Tejun Heo34f60552011-12-14 00:33:37 +0100890 if (unlikely(blk_queue_dead(q)))
Tejun Heoda8303c2011-10-19 14:33:05 +0200891 return NULL;
892
Jens Axboe1faa16d2009-04-06 14:48:01 +0200893 prepare_to_wait_exclusive(&rl->wait[is_sync], &wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 TASK_UNINTERRUPTIBLE);
895
Jens Axboe1faa16d2009-04-06 14:48:01 +0200896 trace_block_sleeprq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200898 spin_unlock_irq(q->queue_lock);
899 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200901 /*
902 * After sleeping, we become a "batching" process and
903 * will be able to allocate at least one request, and
904 * up to a big batch of them for a small period time.
905 * See ioc_batching, ioc_set_batching
906 */
907 ioc = current_io_context(GFP_NOIO, q->node);
908 ioc_set_batching(q, ioc);
Jens Axboe2056a782006-03-23 20:00:26 +0100909
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200910 spin_lock_irq(q->queue_lock);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200911 finish_wait(&rl->wait[is_sync], &wait);
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200912
913 rq = get_request(q, rw_flags, bio, GFP_NOIO);
914 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 return rq;
917}
918
Jens Axboe165125e2007-07-24 09:28:11 +0200919struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
921 struct request *rq;
922
923 BUG_ON(rw != READ && rw != WRITE);
924
Nick Piggind6344532005-06-28 20:45:14 -0700925 spin_lock_irq(q->queue_lock);
Tejun Heoda8303c2011-10-19 14:33:05 +0200926 if (gfp_mask & __GFP_WAIT)
Jens Axboe22e2c502005-06-27 10:55:12 +0200927 rq = get_request_wait(q, rw, NULL);
Tejun Heoda8303c2011-10-19 14:33:05 +0200928 else
Jens Axboe22e2c502005-06-27 10:55:12 +0200929 rq = get_request(q, rw, NULL, gfp_mask);
Tejun Heoda8303c2011-10-19 14:33:05 +0200930 if (!rq)
931 spin_unlock_irq(q->queue_lock);
Nick Piggind6344532005-06-28 20:45:14 -0700932 /* q->queue_lock is unlocked at this point */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 return rq;
935}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936EXPORT_SYMBOL(blk_get_request);
937
938/**
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300939 * blk_make_request - given a bio, allocate a corresponding struct request.
Randy Dunlap8ebf9752009-06-11 20:00:41 -0700940 * @q: target request queue
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300941 * @bio: The bio describing the memory mappings that will be submitted for IO.
942 * It may be a chained-bio properly constructed by block/bio layer.
Randy Dunlap8ebf9752009-06-11 20:00:41 -0700943 * @gfp_mask: gfp flags to be used for memory allocation
Jens Axboedc72ef42006-07-20 14:54:05 +0200944 *
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300945 * blk_make_request is the parallel of generic_make_request for BLOCK_PC
946 * type commands. Where the struct request needs to be farther initialized by
947 * the caller. It is passed a &struct bio, which describes the memory info of
948 * the I/O transfer.
949 *
950 * The caller of blk_make_request must make sure that bi_io_vec
951 * are set to describe the memory buffers. That bio_data_dir() will return
952 * the needed direction of the request. (And all bio's in the passed bio-chain
953 * are properly set accordingly)
954 *
955 * If called under none-sleepable conditions, mapped bio buffers must not
956 * need bouncing, by calling the appropriate masked or flagged allocator,
957 * suitable for the target device. Otherwise the call to blk_queue_bounce will
958 * BUG.
Jens Axboe53674ac2009-05-19 19:52:35 +0200959 *
960 * WARNING: When allocating/cloning a bio-chain, careful consideration should be
961 * given to how you allocate bios. In particular, you cannot use __GFP_WAIT for
962 * anything but the first bio in the chain. Otherwise you risk waiting for IO
963 * completion of a bio that hasn't been submitted yet, thus resulting in a
964 * deadlock. Alternatively bios should be allocated using bio_kmalloc() instead
965 * of bio_alloc(), as that avoids the mempool deadlock.
966 * If possible a big IO should be split into smaller parts when allocation
967 * fails. Partial allocation should not be an error, or you risk a live-lock.
Jens Axboedc72ef42006-07-20 14:54:05 +0200968 */
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300969struct request *blk_make_request(struct request_queue *q, struct bio *bio,
970 gfp_t gfp_mask)
Jens Axboedc72ef42006-07-20 14:54:05 +0200971{
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300972 struct request *rq = blk_get_request(q, bio_data_dir(bio), gfp_mask);
973
974 if (unlikely(!rq))
975 return ERR_PTR(-ENOMEM);
976
977 for_each_bio(bio) {
978 struct bio *bounce_bio = bio;
979 int ret;
980
981 blk_queue_bounce(q, &bounce_bio);
982 ret = blk_rq_append_bio(q, rq, bounce_bio);
983 if (unlikely(ret)) {
984 blk_put_request(rq);
985 return ERR_PTR(ret);
986 }
987 }
988
989 return rq;
Jens Axboedc72ef42006-07-20 14:54:05 +0200990}
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300991EXPORT_SYMBOL(blk_make_request);
Jens Axboedc72ef42006-07-20 14:54:05 +0200992
993/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 * blk_requeue_request - put a request back on queue
995 * @q: request queue where request should be inserted
996 * @rq: request to be inserted
997 *
998 * Description:
999 * Drivers often keep queueing requests until the hardware cannot accept
1000 * more, when that condition happens we need to put the request back
1001 * on the queue. Must be called with queue lock held.
1002 */
Jens Axboe165125e2007-07-24 09:28:11 +02001003void blk_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Jens Axboe242f9dc2008-09-14 05:55:09 -07001005 blk_delete_timer(rq);
1006 blk_clear_rq_complete(rq);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001007 trace_block_rq_requeue(q, rq);
Jens Axboe2056a782006-03-23 20:00:26 +01001008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 if (blk_rq_tagged(rq))
1010 blk_queue_end_tag(q, rq);
1011
James Bottomleyba396a62009-05-27 14:17:08 +02001012 BUG_ON(blk_queued_rq(rq));
1013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 elv_requeue_request(q, rq);
1015}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016EXPORT_SYMBOL(blk_requeue_request);
1017
Jens Axboe73c10102011-03-08 13:19:51 +01001018static void add_acct_request(struct request_queue *q, struct request *rq,
1019 int where)
1020{
1021 drive_stat_acct(rq, 1);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001022 __elv_add_request(q, rq, where);
Jens Axboe73c10102011-03-08 13:19:51 +01001023}
1024
Tejun Heo074a7ac2008-08-25 19:56:14 +09001025static void part_round_stats_single(int cpu, struct hd_struct *part,
1026 unsigned long now)
1027{
1028 if (now == part->stamp)
1029 return;
1030
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001031 if (part_in_flight(part)) {
Tejun Heo074a7ac2008-08-25 19:56:14 +09001032 __part_stat_add(cpu, part, time_in_queue,
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001033 part_in_flight(part) * (now - part->stamp));
Tejun Heo074a7ac2008-08-25 19:56:14 +09001034 __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
1035 }
1036 part->stamp = now;
1037}
1038
1039/**
Randy Dunlap496aa8a2008-10-16 07:46:23 +02001040 * part_round_stats() - Round off the performance stats on a struct disk_stats.
1041 * @cpu: cpu number for stats access
1042 * @part: target partition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 *
1044 * The average IO queue length and utilisation statistics are maintained
1045 * by observing the current state of the queue length and the amount of
1046 * time it has been in this state for.
1047 *
1048 * Normally, that accounting is done on IO completion, but that can result
1049 * in more than a second's worth of IO being accounted for within any one
1050 * second, leading to >100% utilisation. To deal with that, we call this
1051 * function to do a round-off before returning the results when reading
1052 * /proc/diskstats. This accounts immediately for all queue usage up to
1053 * the current jiffies and restarts the counters again.
1054 */
Tejun Heoc9959052008-08-25 19:47:21 +09001055void part_round_stats(int cpu, struct hd_struct *part)
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001056{
1057 unsigned long now = jiffies;
1058
Tejun Heo074a7ac2008-08-25 19:56:14 +09001059 if (part->partno)
1060 part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
1061 part_round_stats_single(cpu, part, now);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001062}
Tejun Heo074a7ac2008-08-25 19:56:14 +09001063EXPORT_SYMBOL_GPL(part_round_stats);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065/*
1066 * queue lock must be held
1067 */
Jens Axboe165125e2007-07-24 09:28:11 +02001068void __blk_put_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if (unlikely(!q))
1071 return;
1072 if (unlikely(--req->ref_count))
1073 return;
1074
Tejun Heo8922e162005-10-20 16:23:44 +02001075 elv_completed_request(q, req);
1076
Boaz Harrosh1cd96c22009-03-24 12:35:07 +01001077 /* this is a bio leak */
1078 WARN_ON(req->bio != NULL);
1079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 /*
1081 * Request may not have originated from ll_rw_blk. if not,
1082 * it didn't come out of our reserved rq pools
1083 */
Jens Axboe49171e52006-08-10 08:59:11 +02001084 if (req->cmd_flags & REQ_ALLOCED) {
Tejun Heo75eb6c32011-10-19 14:31:22 +02001085 unsigned int flags = req->cmd_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 BUG_ON(!list_empty(&req->queuelist));
Jens Axboe98170642006-07-28 09:23:08 +02001088 BUG_ON(!hlist_unhashed(&req->hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090 blk_free_request(q, req);
Tejun Heo75eb6c32011-10-19 14:31:22 +02001091 freed_request(q, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
1093}
Mike Christie6e39b69e2005-11-11 05:30:24 -06001094EXPORT_SYMBOL_GPL(__blk_put_request);
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096void blk_put_request(struct request *req)
1097{
Tejun Heo8922e162005-10-20 16:23:44 +02001098 unsigned long flags;
Jens Axboe165125e2007-07-24 09:28:11 +02001099 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
FUJITA Tomonori52a93ba2008-07-15 21:21:45 +02001101 spin_lock_irqsave(q->queue_lock, flags);
1102 __blk_put_request(q, req);
1103 spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105EXPORT_SYMBOL(blk_put_request);
1106
Christoph Hellwig66ac0282010-06-18 16:59:42 +02001107/**
1108 * blk_add_request_payload - add a payload to a request
1109 * @rq: request to update
1110 * @page: page backing the payload
1111 * @len: length of the payload.
1112 *
1113 * This allows to later add a payload to an already submitted request by
1114 * a block driver. The driver needs to take care of freeing the payload
1115 * itself.
1116 *
1117 * Note that this is a quite horrible hack and nothing but handling of
1118 * discard requests should ever use it.
1119 */
1120void blk_add_request_payload(struct request *rq, struct page *page,
1121 unsigned int len)
1122{
1123 struct bio *bio = rq->bio;
1124
1125 bio->bi_io_vec->bv_page = page;
1126 bio->bi_io_vec->bv_offset = 0;
1127 bio->bi_io_vec->bv_len = len;
1128
1129 bio->bi_size = len;
1130 bio->bi_vcnt = 1;
1131 bio->bi_phys_segments = 1;
1132
1133 rq->__data_len = rq->resid_len = len;
1134 rq->nr_phys_segments = 1;
1135 rq->buffer = bio_data(bio);
1136}
1137EXPORT_SYMBOL_GPL(blk_add_request_payload);
1138
Jens Axboe73c10102011-03-08 13:19:51 +01001139static bool bio_attempt_back_merge(struct request_queue *q, struct request *req,
1140 struct bio *bio)
1141{
1142 const int ff = bio->bi_rw & REQ_FAILFAST_MASK;
1143
Jens Axboe73c10102011-03-08 13:19:51 +01001144 if (!ll_back_merge_fn(q, req, bio))
1145 return false;
1146
1147 trace_block_bio_backmerge(q, bio);
1148
1149 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1150 blk_rq_set_mixed_merge(req);
1151
1152 req->biotail->bi_next = bio;
1153 req->biotail = bio;
1154 req->__data_len += bio->bi_size;
1155 req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
1156
1157 drive_stat_acct(req, 0);
Vivek Goyal95cf3dd2011-05-23 10:02:19 +02001158 elv_bio_merged(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001159 return true;
1160}
1161
1162static bool bio_attempt_front_merge(struct request_queue *q,
1163 struct request *req, struct bio *bio)
1164{
1165 const int ff = bio->bi_rw & REQ_FAILFAST_MASK;
Jens Axboe73c10102011-03-08 13:19:51 +01001166
Jens Axboe73c10102011-03-08 13:19:51 +01001167 if (!ll_front_merge_fn(q, req, bio))
1168 return false;
1169
1170 trace_block_bio_frontmerge(q, bio);
1171
1172 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1173 blk_rq_set_mixed_merge(req);
1174
Jens Axboe73c10102011-03-08 13:19:51 +01001175 bio->bi_next = req->bio;
1176 req->bio = bio;
1177
1178 /*
1179 * may not be valid. if the low level driver said
1180 * it didn't need a bounce buffer then it better
1181 * not touch req->buffer either...
1182 */
1183 req->buffer = bio_data(bio);
1184 req->__sector = bio->bi_sector;
1185 req->__data_len += bio->bi_size;
1186 req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
1187
1188 drive_stat_acct(req, 0);
Vivek Goyal95cf3dd2011-05-23 10:02:19 +02001189 elv_bio_merged(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001190 return true;
1191}
1192
Tejun Heobd87b582011-10-19 14:33:08 +02001193/**
1194 * attempt_plug_merge - try to merge with %current's plugged list
1195 * @q: request_queue new bio is being queued at
1196 * @bio: new bio being queued
1197 * @request_count: out parameter for number of traversed plugged requests
1198 *
1199 * Determine whether @bio being queued on @q can be merged with a request
1200 * on %current's plugged list. Returns %true if merge was successful,
1201 * otherwise %false.
1202 *
1203 * This function is called without @q->queue_lock; however, elevator is
1204 * accessed iff there already are requests on the plugged list which in
1205 * turn guarantees validity of the elevator.
1206 *
1207 * Note that, on successful merge, elevator operation
1208 * elevator_bio_merged_fn() will be called without queue lock. Elevator
1209 * must be ready for this.
Jens Axboe73c10102011-03-08 13:19:51 +01001210 */
Tejun Heobd87b582011-10-19 14:33:08 +02001211static bool attempt_plug_merge(struct request_queue *q, struct bio *bio,
1212 unsigned int *request_count)
Jens Axboe73c10102011-03-08 13:19:51 +01001213{
1214 struct blk_plug *plug;
1215 struct request *rq;
1216 bool ret = false;
1217
Tejun Heobd87b582011-10-19 14:33:08 +02001218 plug = current->plug;
Jens Axboe73c10102011-03-08 13:19:51 +01001219 if (!plug)
1220 goto out;
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001221 *request_count = 0;
Jens Axboe73c10102011-03-08 13:19:51 +01001222
1223 list_for_each_entry_reverse(rq, &plug->list, queuelist) {
1224 int el_ret;
1225
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001226 (*request_count)++;
1227
Jens Axboe73c10102011-03-08 13:19:51 +01001228 if (rq->q != q)
1229 continue;
1230
1231 el_ret = elv_try_merge(rq, bio);
1232 if (el_ret == ELEVATOR_BACK_MERGE) {
1233 ret = bio_attempt_back_merge(q, rq, bio);
1234 if (ret)
1235 break;
1236 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
1237 ret = bio_attempt_front_merge(q, rq, bio);
1238 if (ret)
1239 break;
1240 }
1241 }
1242out:
1243 return ret;
1244}
1245
Jens Axboe86db1e22008-01-29 14:53:40 +01001246void init_request_from_bio(struct request *req, struct bio *bio)
Tejun Heo52d9e672006-01-06 09:49:58 +01001247{
Jens Axboe4aff5e22006-08-10 08:44:47 +02001248 req->cmd_type = REQ_TYPE_FS;
Tejun Heo52d9e672006-01-06 09:49:58 +01001249
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001250 req->cmd_flags |= bio->bi_rw & REQ_COMMON_MASK;
1251 if (bio->bi_rw & REQ_RAHEAD)
Tejun Heoa82afdf2009-07-03 17:48:16 +09001252 req->cmd_flags |= REQ_FAILFAST_MASK;
Jens Axboeb31dc662006-06-13 08:26:10 +02001253
Tejun Heo52d9e672006-01-06 09:49:58 +01001254 req->errors = 0;
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001255 req->__sector = bio->bi_sector;
Tejun Heo52d9e672006-01-06 09:49:58 +01001256 req->ioprio = bio_prio(bio);
NeilBrownbc1c56f2007-08-16 13:31:30 +02001257 blk_rq_bio_prep(req->q, req, bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001258}
1259
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001260void blk_queue_bio(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
Jiri Slaby5e00d1b2010-08-12 14:31:06 +02001262 const bool sync = !!(bio->bi_rw & REQ_SYNC);
Jens Axboe73c10102011-03-08 13:19:51 +01001263 struct blk_plug *plug;
1264 int el_ret, rw_flags, where = ELEVATOR_INSERT_SORT;
1265 struct request *req;
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001266 unsigned int request_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 /*
1269 * low level driver can indicate that it wants pages above a
1270 * certain limit bounced to low memory (ie for highmem, or even
1271 * ISA dma in theory)
1272 */
1273 blk_queue_bounce(q, &bio);
1274
Tejun Heo4fed9472010-09-03 11:56:17 +02001275 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) {
Jens Axboe73c10102011-03-08 13:19:51 +01001276 spin_lock_irq(q->queue_lock);
Tejun Heoae1b1532011-01-25 12:43:54 +01001277 where = ELEVATOR_INSERT_FLUSH;
Tejun Heo28e7d182010-09-03 11:56:16 +02001278 goto get_rq;
1279 }
1280
Jens Axboe73c10102011-03-08 13:19:51 +01001281 /*
1282 * Check if we can merge with the plugged list before grabbing
1283 * any locks.
1284 */
Tejun Heobd87b582011-10-19 14:33:08 +02001285 if (attempt_plug_merge(q, bio, &request_count))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001286 return;
Jens Axboe73c10102011-03-08 13:19:51 +01001287
1288 spin_lock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290 el_ret = elv_merge(q, &req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001291 if (el_ret == ELEVATOR_BACK_MERGE) {
Jens Axboe73c10102011-03-08 13:19:51 +01001292 if (bio_attempt_back_merge(q, req, bio)) {
1293 if (!attempt_back_merge(q, req))
1294 elv_merged_request(q, req, el_ret);
1295 goto out_unlock;
Tejun Heo80a761f2009-07-03 17:48:17 +09001296 }
Jens Axboe73c10102011-03-08 13:19:51 +01001297 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
Jens Axboe73c10102011-03-08 13:19:51 +01001298 if (bio_attempt_front_merge(q, req, bio)) {
1299 if (!attempt_front_merge(q, req))
1300 elv_merged_request(q, req, el_ret);
1301 goto out_unlock;
1302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 }
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305get_rq:
Nick Piggin450991b2005-06-28 20:45:13 -07001306 /*
Jens Axboe7749a8d2006-12-13 13:02:26 +01001307 * This sync check and mask will be re-done in init_request_from_bio(),
1308 * but we need to set it earlier to expose the sync flag to the
1309 * rq allocator and io schedulers.
1310 */
1311 rw_flags = bio_data_dir(bio);
1312 if (sync)
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001313 rw_flags |= REQ_SYNC;
Jens Axboe7749a8d2006-12-13 13:02:26 +01001314
1315 /*
Nick Piggin450991b2005-06-28 20:45:13 -07001316 * Grab a free request. This is might sleep but can not fail.
Nick Piggind6344532005-06-28 20:45:14 -07001317 * Returns with the queue unlocked.
Nick Piggin450991b2005-06-28 20:45:13 -07001318 */
Jens Axboe7749a8d2006-12-13 13:02:26 +01001319 req = get_request_wait(q, rw_flags, bio);
Tejun Heoda8303c2011-10-19 14:33:05 +02001320 if (unlikely(!req)) {
1321 bio_endio(bio, -ENODEV); /* @q is dead */
1322 goto out_unlock;
1323 }
Nick Piggind6344532005-06-28 20:45:14 -07001324
Nick Piggin450991b2005-06-28 20:45:13 -07001325 /*
1326 * After dropping the lock and possibly sleeping here, our request
1327 * may now be mergeable after it had proven unmergeable (above).
1328 * We don't worry about that case for efficiency. It won't happen
1329 * often, and the elevators are able to handle it.
1330 */
Tejun Heo52d9e672006-01-06 09:49:58 +01001331 init_request_from_bio(req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Tao Ma9562ad92011-10-24 16:11:30 +02001333 if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags))
Jens Axboe11ccf112011-07-26 15:01:15 +02001334 req->cpu = raw_smp_processor_id();
Tejun Heodd831002010-09-03 11:56:16 +02001335
Jens Axboe73c10102011-03-08 13:19:51 +01001336 plug = current->plug;
Jens Axboe721a9602011-03-09 11:56:30 +01001337 if (plug) {
Jens Axboedc6d36c2011-04-12 10:28:28 +02001338 /*
1339 * If this is the first request added after a plug, fire
1340 * of a plug trace. If others have been added before, check
1341 * if we have multiple devices in this plug. If so, make a
1342 * note to sort the list before dispatch.
1343 */
1344 if (list_empty(&plug->list))
1345 trace_block_plug(q);
Shaohua Li3540d5e2011-11-16 09:21:50 +01001346 else {
1347 if (!plug->should_sort) {
1348 struct request *__rq;
Jens Axboe73c10102011-03-08 13:19:51 +01001349
Shaohua Li3540d5e2011-11-16 09:21:50 +01001350 __rq = list_entry_rq(plug->list.prev);
1351 if (__rq->q != q)
1352 plug->should_sort = 1;
1353 }
Shaohua Li019ceb72011-11-16 09:21:50 +01001354 if (request_count >= BLK_MAX_REQUEST_COUNT) {
Shaohua Li3540d5e2011-11-16 09:21:50 +01001355 blk_flush_plug_list(plug, false);
Shaohua Li019ceb72011-11-16 09:21:50 +01001356 trace_block_plug(q);
1357 }
Jens Axboe73c10102011-03-08 13:19:51 +01001358 }
Shaohua Lia6327162011-08-24 16:04:32 +02001359 list_add_tail(&req->queuelist, &plug->list);
1360 drive_stat_acct(req, 1);
Jens Axboe73c10102011-03-08 13:19:51 +01001361 } else {
1362 spin_lock_irq(q->queue_lock);
1363 add_acct_request(q, req, where);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02001364 __blk_run_queue(q);
Jens Axboe73c10102011-03-08 13:19:51 +01001365out_unlock:
1366 spin_unlock_irq(q->queue_lock);
1367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368}
Jens Axboec20e8de2011-09-12 12:03:37 +02001369EXPORT_SYMBOL_GPL(blk_queue_bio); /* for device mapper only */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371/*
1372 * If bio->bi_dev is a partition, remap the location
1373 */
1374static inline void blk_partition_remap(struct bio *bio)
1375{
1376 struct block_device *bdev = bio->bi_bdev;
1377
Jens Axboebf2de6f2007-09-27 13:01:25 +02001378 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 struct hd_struct *p = bdev->bd_part;
Jens Axboea3623572005-11-01 09:26:16 +01001380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 bio->bi_sector += p->start_sect;
1382 bio->bi_bdev = bdev->bd_contains;
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001383
Mike Snitzerd07335e2010-11-16 12:52:38 +01001384 trace_block_bio_remap(bdev_get_queue(bio->bi_bdev), bio,
1385 bdev->bd_dev,
1386 bio->bi_sector - p->start_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 }
1388}
1389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390static void handle_bad_sector(struct bio *bio)
1391{
1392 char b[BDEVNAME_SIZE];
1393
1394 printk(KERN_INFO "attempt to access beyond end of device\n");
1395 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1396 bdevname(bio->bi_bdev, b),
1397 bio->bi_rw,
1398 (unsigned long long)bio->bi_sector + bio_sectors(bio),
Mike Snitzer77304d22010-11-08 14:39:12 +01001399 (long long)(i_size_read(bio->bi_bdev->bd_inode) >> 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 set_bit(BIO_EOF, &bio->bi_flags);
1402}
1403
Akinobu Mitac17bb492006-12-08 02:39:46 -08001404#ifdef CONFIG_FAIL_MAKE_REQUEST
1405
1406static DECLARE_FAULT_ATTR(fail_make_request);
1407
1408static int __init setup_fail_make_request(char *str)
1409{
1410 return setup_fault_attr(&fail_make_request, str);
1411}
1412__setup("fail_make_request=", setup_fail_make_request);
1413
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001414static bool should_fail_request(struct hd_struct *part, unsigned int bytes)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001415{
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001416 return part->make_it_fail && should_fail(&fail_make_request, bytes);
Akinobu Mitac17bb492006-12-08 02:39:46 -08001417}
1418
1419static int __init fail_make_request_debugfs(void)
1420{
Akinobu Mitadd48c082011-08-03 16:21:01 -07001421 struct dentry *dir = fault_create_debugfs_attr("fail_make_request",
1422 NULL, &fail_make_request);
1423
1424 return IS_ERR(dir) ? PTR_ERR(dir) : 0;
Akinobu Mitac17bb492006-12-08 02:39:46 -08001425}
1426
1427late_initcall(fail_make_request_debugfs);
1428
1429#else /* CONFIG_FAIL_MAKE_REQUEST */
1430
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001431static inline bool should_fail_request(struct hd_struct *part,
1432 unsigned int bytes)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001433{
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001434 return false;
Akinobu Mitac17bb492006-12-08 02:39:46 -08001435}
1436
1437#endif /* CONFIG_FAIL_MAKE_REQUEST */
1438
Jens Axboec07e2b42007-07-18 13:27:58 +02001439/*
1440 * Check whether this bio extends beyond the end of the device.
1441 */
1442static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1443{
1444 sector_t maxsector;
1445
1446 if (!nr_sectors)
1447 return 0;
1448
1449 /* Test device or partition size, when known. */
Mike Snitzer77304d22010-11-08 14:39:12 +01001450 maxsector = i_size_read(bio->bi_bdev->bd_inode) >> 9;
Jens Axboec07e2b42007-07-18 13:27:58 +02001451 if (maxsector) {
1452 sector_t sector = bio->bi_sector;
1453
1454 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1455 /*
1456 * This may well happen - the kernel calls bread()
1457 * without checking the size of the device, e.g., when
1458 * mounting a device.
1459 */
1460 handle_bad_sector(bio);
1461 return 1;
1462 }
1463 }
1464
1465 return 0;
1466}
1467
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001468static noinline_for_stack bool
1469generic_make_request_checks(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
Jens Axboe165125e2007-07-24 09:28:11 +02001471 struct request_queue *q;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001472 int nr_sectors = bio_sectors(bio);
Jens Axboe51fd77b2007-11-02 08:49:08 +01001473 int err = -EIO;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001474 char b[BDEVNAME_SIZE];
1475 struct hd_struct *part;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Jens Axboec07e2b42007-07-18 13:27:58 +02001479 if (bio_check_eod(bio, nr_sectors))
1480 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001482 q = bdev_get_queue(bio->bi_bdev);
1483 if (unlikely(!q)) {
1484 printk(KERN_ERR
1485 "generic_make_request: Trying to access "
1486 "nonexistent block-device %s (%Lu)\n",
1487 bdevname(bio->bi_bdev, b),
1488 (long long) bio->bi_sector);
1489 goto end_io;
1490 }
1491
1492 if (unlikely(!(bio->bi_rw & REQ_DISCARD) &&
1493 nr_sectors > queue_max_hw_sectors(q))) {
1494 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
1495 bdevname(bio->bi_bdev, b),
1496 bio_sectors(bio),
1497 queue_max_hw_sectors(q));
1498 goto end_io;
1499 }
1500
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001501 part = bio->bi_bdev->bd_part;
1502 if (should_fail_request(part, bio->bi_size) ||
1503 should_fail_request(&part_to_disk(part)->part0,
1504 bio->bi_size))
1505 goto end_io;
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 /*
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001508 * If this device has partitions, remap block n
1509 * of partition p to block n+start(p) of the disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001511 blk_partition_remap(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001513 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio))
1514 goto end_io;
1515
1516 if (bio_check_eod(bio, nr_sectors))
1517 goto end_io;
1518
1519 /*
1520 * Filter flush bio's early so that make_request based
1521 * drivers without flush support don't have to worry
1522 * about them.
1523 */
1524 if ((bio->bi_rw & (REQ_FLUSH | REQ_FUA)) && !q->flush_flags) {
1525 bio->bi_rw &= ~(REQ_FLUSH | REQ_FUA);
1526 if (!nr_sectors) {
1527 err = 0;
Tejun Heoa7384672008-11-28 13:32:03 +09001528 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 }
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001532 if ((bio->bi_rw & REQ_DISCARD) &&
1533 (!blk_queue_discard(q) ||
1534 ((bio->bi_rw & REQ_SECURE) &&
1535 !blk_queue_secdiscard(q)))) {
1536 err = -EOPNOTSUPP;
1537 goto end_io;
1538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Tejun Heobc16a4f2011-10-19 14:33:01 +02001540 if (blk_throtl_bio(q, bio))
1541 return false; /* throttled, will be resubmitted later */
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001542
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001543 trace_block_bio_queue(q, bio);
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001544 return true;
Tejun Heoa7384672008-11-28 13:32:03 +09001545
1546end_io:
1547 bio_endio(bio, err);
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001548 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549}
1550
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001551/**
1552 * generic_make_request - hand a buffer to its device driver for I/O
1553 * @bio: The bio describing the location in memory and on the device.
1554 *
1555 * generic_make_request() is used to make I/O requests of block
1556 * devices. It is passed a &struct bio, which describes the I/O that needs
1557 * to be done.
1558 *
1559 * generic_make_request() does not return any status. The
1560 * success/failure status of the request, along with notification of
1561 * completion, is delivered asynchronously through the bio->bi_end_io
1562 * function described (one day) else where.
1563 *
1564 * The caller of generic_make_request must make sure that bi_io_vec
1565 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1566 * set to describe the device address, and the
1567 * bi_end_io and optionally bi_private are set to describe how
1568 * completion notification should be signaled.
1569 *
1570 * generic_make_request and the drivers it calls may use bi_next if this
1571 * bio happens to be merged with someone else, and may resubmit the bio to
1572 * a lower device by calling into generic_make_request recursively, which
1573 * means the bio should NOT be touched after the call to ->make_request_fn.
Neil Brownd89d8792007-05-01 09:53:42 +02001574 */
1575void generic_make_request(struct bio *bio)
1576{
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001577 struct bio_list bio_list_on_stack;
1578
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001579 if (!generic_make_request_checks(bio))
1580 return;
1581
1582 /*
1583 * We only want one ->make_request_fn to be active at a time, else
1584 * stack usage with stacked devices could be a problem. So use
1585 * current->bio_list to keep a list of requests submited by a
1586 * make_request_fn function. current->bio_list is also used as a
1587 * flag to say if generic_make_request is currently active in this
1588 * task or not. If it is NULL, then no make_request is active. If
1589 * it is non-NULL, then a make_request is active, and new requests
1590 * should be added at the tail
1591 */
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001592 if (current->bio_list) {
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001593 bio_list_add(current->bio_list, bio);
Neil Brownd89d8792007-05-01 09:53:42 +02001594 return;
1595 }
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001596
Neil Brownd89d8792007-05-01 09:53:42 +02001597 /* following loop may be a bit non-obvious, and so deserves some
1598 * explanation.
1599 * Before entering the loop, bio->bi_next is NULL (as all callers
1600 * ensure that) so we have a list with a single bio.
1601 * We pretend that we have just taken it off a longer list, so
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001602 * we assign bio_list to a pointer to the bio_list_on_stack,
1603 * thus initialising the bio_list of new bios to be
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001604 * added. ->make_request() may indeed add some more bios
Neil Brownd89d8792007-05-01 09:53:42 +02001605 * through a recursive call to generic_make_request. If it
1606 * did, we find a non-NULL value in bio_list and re-enter the loop
1607 * from the top. In this case we really did just take the bio
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001608 * of the top of the list (no pretending) and so remove it from
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001609 * bio_list, and call into ->make_request() again.
Neil Brownd89d8792007-05-01 09:53:42 +02001610 */
1611 BUG_ON(bio->bi_next);
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001612 bio_list_init(&bio_list_on_stack);
1613 current->bio_list = &bio_list_on_stack;
Neil Brownd89d8792007-05-01 09:53:42 +02001614 do {
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001615 struct request_queue *q = bdev_get_queue(bio->bi_bdev);
1616
1617 q->make_request_fn(q, bio);
1618
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001619 bio = bio_list_pop(current->bio_list);
Neil Brownd89d8792007-05-01 09:53:42 +02001620 } while (bio);
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001621 current->bio_list = NULL; /* deactivate */
Neil Brownd89d8792007-05-01 09:53:42 +02001622}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623EXPORT_SYMBOL(generic_make_request);
1624
1625/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001626 * submit_bio - submit a bio to the block device layer for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1628 * @bio: The &struct bio which describes the I/O
1629 *
1630 * submit_bio() is very similar in purpose to generic_make_request(), and
1631 * uses that function to do most of the work. Both are fairly rough
Randy Dunlap710027a2008-08-19 20:13:11 +02001632 * interfaces; @bio must be presetup and ready for I/O.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 *
1634 */
1635void submit_bio(int rw, struct bio *bio)
1636{
1637 int count = bio_sectors(bio);
1638
Jens Axboe22e2c502005-06-27 10:55:12 +02001639 bio->bi_rw |= rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Jens Axboebf2de6f2007-09-27 13:01:25 +02001641 /*
1642 * If it's a regular read/write or a barrier with data attached,
1643 * go through the normal accounting stuff before submission.
1644 */
Jens Axboe3ffb52e2010-06-29 13:33:38 +02001645 if (bio_has_data(bio) && !(rw & REQ_DISCARD)) {
Jens Axboebf2de6f2007-09-27 13:01:25 +02001646 if (rw & WRITE) {
1647 count_vm_events(PGPGOUT, count);
1648 } else {
1649 task_io_account_read(bio->bi_size);
1650 count_vm_events(PGPGIN, count);
1651 }
1652
1653 if (unlikely(block_dump)) {
1654 char b[BDEVNAME_SIZE];
San Mehat8dcbdc72010-09-14 08:48:01 +02001655 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s (%u sectors)\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001656 current->comm, task_pid_nr(current),
Jens Axboebf2de6f2007-09-27 13:01:25 +02001657 (rw & WRITE) ? "WRITE" : "READ",
1658 (unsigned long long)bio->bi_sector,
San Mehat8dcbdc72010-09-14 08:48:01 +02001659 bdevname(bio->bi_bdev, b),
1660 count);
Jens Axboebf2de6f2007-09-27 13:01:25 +02001661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 }
1663
1664 generic_make_request(bio);
1665}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666EXPORT_SYMBOL(submit_bio);
1667
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001668/**
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001669 * blk_rq_check_limits - Helper function to check a request for the queue limit
1670 * @q: the queue
1671 * @rq: the request being checked
1672 *
1673 * Description:
1674 * @rq may have been made based on weaker limitations of upper-level queues
1675 * in request stacking drivers, and it may violate the limitation of @q.
1676 * Since the block layer and the underlying device driver trust @rq
1677 * after it is inserted to @q, it should be checked against @q before
1678 * the insertion using this generic function.
1679 *
1680 * This function should also be useful for request stacking drivers
Stefan Weileef35c22010-08-06 21:11:15 +02001681 * in some cases below, so export this function.
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001682 * Request stacking drivers like request-based dm may change the queue
1683 * limits while requests are in the queue (e.g. dm's table swapping).
1684 * Such request stacking drivers should check those requests agaist
1685 * the new queue limits again when they dispatch those requests,
1686 * although such checkings are also done against the old queue limits
1687 * when submitting requests.
1688 */
1689int blk_rq_check_limits(struct request_queue *q, struct request *rq)
1690{
ike Snitzer33839772010-08-08 12:11:33 -04001691 if (rq->cmd_flags & REQ_DISCARD)
1692 return 0;
1693
Martin K. Petersenae03bf62009-05-22 17:17:50 -04001694 if (blk_rq_sectors(rq) > queue_max_sectors(q) ||
1695 blk_rq_bytes(rq) > queue_max_hw_sectors(q) << 9) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001696 printk(KERN_ERR "%s: over max size limit.\n", __func__);
1697 return -EIO;
1698 }
1699
1700 /*
1701 * queue's settings related to segment counting like q->bounce_pfn
1702 * may differ from that of other stacking queues.
1703 * Recalculate it to check the request correctly on this queue's
1704 * limitation.
1705 */
1706 blk_recalc_rq_segments(rq);
Martin K. Petersen8a783622010-02-26 00:20:39 -05001707 if (rq->nr_phys_segments > queue_max_segments(q)) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001708 printk(KERN_ERR "%s: over max segments limit.\n", __func__);
1709 return -EIO;
1710 }
1711
1712 return 0;
1713}
1714EXPORT_SYMBOL_GPL(blk_rq_check_limits);
1715
1716/**
1717 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
1718 * @q: the queue to submit the request
1719 * @rq: the request being queued
1720 */
1721int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
1722{
1723 unsigned long flags;
Jeff Moyer4853aba2011-08-15 21:37:25 +02001724 int where = ELEVATOR_INSERT_BACK;
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001725
1726 if (blk_rq_check_limits(q, rq))
1727 return -EIO;
1728
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001729 if (rq->rq_disk &&
1730 should_fail_request(&rq->rq_disk->part0, blk_rq_bytes(rq)))
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001731 return -EIO;
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001732
1733 spin_lock_irqsave(q->queue_lock, flags);
1734
1735 /*
1736 * Submitting request must be dequeued before calling this function
1737 * because it will be linked to another request_queue
1738 */
1739 BUG_ON(blk_queued_rq(rq));
1740
Jeff Moyer4853aba2011-08-15 21:37:25 +02001741 if (rq->cmd_flags & (REQ_FLUSH|REQ_FUA))
1742 where = ELEVATOR_INSERT_FLUSH;
1743
1744 add_acct_request(q, rq, where);
Jeff Moyere67b77c2011-10-17 12:57:23 +02001745 if (where == ELEVATOR_INSERT_FLUSH)
1746 __blk_run_queue(q);
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001747 spin_unlock_irqrestore(q->queue_lock, flags);
1748
1749 return 0;
1750}
1751EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
1752
Tejun Heo80a761f2009-07-03 17:48:17 +09001753/**
1754 * blk_rq_err_bytes - determine number of bytes till the next failure boundary
1755 * @rq: request to examine
1756 *
1757 * Description:
1758 * A request could be merge of IOs which require different failure
1759 * handling. This function determines the number of bytes which
1760 * can be failed from the beginning of the request without
1761 * crossing into area which need to be retried further.
1762 *
1763 * Return:
1764 * The number of bytes to fail.
1765 *
1766 * Context:
1767 * queue_lock must be held.
1768 */
1769unsigned int blk_rq_err_bytes(const struct request *rq)
1770{
1771 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
1772 unsigned int bytes = 0;
1773 struct bio *bio;
1774
1775 if (!(rq->cmd_flags & REQ_MIXED_MERGE))
1776 return blk_rq_bytes(rq);
1777
1778 /*
1779 * Currently the only 'mixing' which can happen is between
1780 * different fastfail types. We can safely fail portions
1781 * which have all the failfast bits that the first one has -
1782 * the ones which are at least as eager to fail as the first
1783 * one.
1784 */
1785 for (bio = rq->bio; bio; bio = bio->bi_next) {
1786 if ((bio->bi_rw & ff) != ff)
1787 break;
1788 bytes += bio->bi_size;
1789 }
1790
1791 /* this could lead to infinite loop */
1792 BUG_ON(blk_rq_bytes(rq) && !bytes);
1793 return bytes;
1794}
1795EXPORT_SYMBOL_GPL(blk_rq_err_bytes);
1796
Jens Axboebc58ba92009-01-23 10:54:44 +01001797static void blk_account_io_completion(struct request *req, unsigned int bytes)
1798{
Jens Axboec2553b52009-04-24 08:10:11 +02001799 if (blk_do_io_stat(req)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01001800 const int rw = rq_data_dir(req);
1801 struct hd_struct *part;
1802 int cpu;
1803
1804 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +01001805 part = req->part;
Jens Axboebc58ba92009-01-23 10:54:44 +01001806 part_stat_add(cpu, part, sectors[rw], bytes >> 9);
1807 part_stat_unlock();
1808 }
1809}
1810
1811static void blk_account_io_done(struct request *req)
1812{
Jens Axboebc58ba92009-01-23 10:54:44 +01001813 /*
Tejun Heodd4c1332010-09-03 11:56:16 +02001814 * Account IO completion. flush_rq isn't accounted as a
1815 * normal IO on queueing nor completion. Accounting the
1816 * containing request is enough.
Jens Axboebc58ba92009-01-23 10:54:44 +01001817 */
Tejun Heo414b4ff2011-01-25 12:43:49 +01001818 if (blk_do_io_stat(req) && !(req->cmd_flags & REQ_FLUSH_SEQ)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01001819 unsigned long duration = jiffies - req->start_time;
1820 const int rw = rq_data_dir(req);
1821 struct hd_struct *part;
1822 int cpu;
1823
1824 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +01001825 part = req->part;
Jens Axboebc58ba92009-01-23 10:54:44 +01001826
1827 part_stat_inc(cpu, part, ios[rw]);
1828 part_stat_add(cpu, part, ticks[rw], duration);
1829 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001830 part_dec_in_flight(part, rw);
Jens Axboebc58ba92009-01-23 10:54:44 +01001831
Jens Axboe6c23a962011-01-07 08:43:37 +01001832 hd_struct_put(part);
Jens Axboebc58ba92009-01-23 10:54:44 +01001833 part_stat_unlock();
1834 }
1835}
1836
Tejun Heo53a08802008-12-03 12:41:26 +01001837/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09001838 * blk_peek_request - peek at the top of a request queue
1839 * @q: request queue to peek at
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001840 *
1841 * Description:
Tejun Heo9934c8c2009-05-08 11:54:16 +09001842 * Return the request at the top of @q. The returned request
1843 * should be started using blk_start_request() before LLD starts
1844 * processing it.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001845 *
1846 * Return:
Tejun Heo9934c8c2009-05-08 11:54:16 +09001847 * Pointer to the request at the top of @q if available. Null
1848 * otherwise.
1849 *
1850 * Context:
1851 * queue_lock must be held.
1852 */
1853struct request *blk_peek_request(struct request_queue *q)
Tejun Heo158dbda2009-04-23 11:05:18 +09001854{
1855 struct request *rq;
1856 int ret;
1857
1858 while ((rq = __elv_next_request(q)) != NULL) {
1859 if (!(rq->cmd_flags & REQ_STARTED)) {
1860 /*
1861 * This is the first time the device driver
1862 * sees this request (possibly after
1863 * requeueing). Notify IO scheduler.
1864 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001865 if (rq->cmd_flags & REQ_SORTED)
Tejun Heo158dbda2009-04-23 11:05:18 +09001866 elv_activate_rq(q, rq);
1867
1868 /*
1869 * just mark as started even if we don't start
1870 * it, a request that has been delayed should
1871 * not be passed by new incoming requests
1872 */
1873 rq->cmd_flags |= REQ_STARTED;
1874 trace_block_rq_issue(q, rq);
1875 }
1876
1877 if (!q->boundary_rq || q->boundary_rq == rq) {
1878 q->end_sector = rq_end_sector(rq);
1879 q->boundary_rq = NULL;
1880 }
1881
1882 if (rq->cmd_flags & REQ_DONTPREP)
1883 break;
1884
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001885 if (q->dma_drain_size && blk_rq_bytes(rq)) {
Tejun Heo158dbda2009-04-23 11:05:18 +09001886 /*
1887 * make sure space for the drain appears we
1888 * know we can do this because max_hw_segments
1889 * has been adjusted to be one fewer than the
1890 * device can handle
1891 */
1892 rq->nr_phys_segments++;
1893 }
1894
1895 if (!q->prep_rq_fn)
1896 break;
1897
1898 ret = q->prep_rq_fn(q, rq);
1899 if (ret == BLKPREP_OK) {
1900 break;
1901 } else if (ret == BLKPREP_DEFER) {
1902 /*
1903 * the request may have been (partially) prepped.
1904 * we need to keep this request in the front to
1905 * avoid resource deadlock. REQ_STARTED will
1906 * prevent other fs requests from passing this one.
1907 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001908 if (q->dma_drain_size && blk_rq_bytes(rq) &&
Tejun Heo158dbda2009-04-23 11:05:18 +09001909 !(rq->cmd_flags & REQ_DONTPREP)) {
1910 /*
1911 * remove the space for the drain we added
1912 * so that we don't add it again
1913 */
1914 --rq->nr_phys_segments;
1915 }
1916
1917 rq = NULL;
1918 break;
1919 } else if (ret == BLKPREP_KILL) {
1920 rq->cmd_flags |= REQ_QUIET;
James Bottomleyc143dc92009-05-30 06:43:49 +02001921 /*
1922 * Mark this request as started so we don't trigger
1923 * any debug logic in the end I/O path.
1924 */
1925 blk_start_request(rq);
Tejun Heo40cbbb72009-04-23 11:05:19 +09001926 __blk_end_request_all(rq, -EIO);
Tejun Heo158dbda2009-04-23 11:05:18 +09001927 } else {
1928 printk(KERN_ERR "%s: bad return=%d\n", __func__, ret);
1929 break;
1930 }
1931 }
1932
1933 return rq;
1934}
Tejun Heo9934c8c2009-05-08 11:54:16 +09001935EXPORT_SYMBOL(blk_peek_request);
Tejun Heo158dbda2009-04-23 11:05:18 +09001936
Tejun Heo9934c8c2009-05-08 11:54:16 +09001937void blk_dequeue_request(struct request *rq)
Tejun Heo158dbda2009-04-23 11:05:18 +09001938{
Tejun Heo9934c8c2009-05-08 11:54:16 +09001939 struct request_queue *q = rq->q;
1940
Tejun Heo158dbda2009-04-23 11:05:18 +09001941 BUG_ON(list_empty(&rq->queuelist));
1942 BUG_ON(ELV_ON_HASH(rq));
1943
1944 list_del_init(&rq->queuelist);
1945
1946 /*
1947 * the time frame between a request being removed from the lists
1948 * and to it is freed is accounted as io that is in progress at
1949 * the driver side.
1950 */
Divyesh Shah91952912010-04-01 15:01:41 -07001951 if (blk_account_rq(rq)) {
Jens Axboe0a7ae2f2009-05-20 08:54:31 +02001952 q->in_flight[rq_is_sync(rq)]++;
Divyesh Shah91952912010-04-01 15:01:41 -07001953 set_io_start_time_ns(rq);
1954 }
Tejun Heo158dbda2009-04-23 11:05:18 +09001955}
1956
Tejun Heo5efccd12009-04-23 11:05:18 +09001957/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09001958 * blk_start_request - start request processing on the driver
1959 * @req: request to dequeue
1960 *
1961 * Description:
1962 * Dequeue @req and start timeout timer on it. This hands off the
1963 * request to the driver.
1964 *
1965 * Block internal functions which don't want to start timer should
1966 * call blk_dequeue_request().
1967 *
1968 * Context:
1969 * queue_lock must be held.
1970 */
1971void blk_start_request(struct request *req)
1972{
1973 blk_dequeue_request(req);
1974
1975 /*
Tejun Heo5f49f632009-05-19 18:33:05 +09001976 * We are now handing the request to the hardware, initialize
1977 * resid_len to full count and add the timeout handler.
Tejun Heo9934c8c2009-05-08 11:54:16 +09001978 */
Tejun Heo5f49f632009-05-19 18:33:05 +09001979 req->resid_len = blk_rq_bytes(req);
FUJITA Tomonoridbb66c42009-06-09 05:47:10 +02001980 if (unlikely(blk_bidi_rq(req)))
1981 req->next_rq->resid_len = blk_rq_bytes(req->next_rq);
1982
Tejun Heo9934c8c2009-05-08 11:54:16 +09001983 blk_add_timer(req);
1984}
1985EXPORT_SYMBOL(blk_start_request);
1986
1987/**
1988 * blk_fetch_request - fetch a request from a request queue
1989 * @q: request queue to fetch a request from
1990 *
1991 * Description:
1992 * Return the request at the top of @q. The request is started on
1993 * return and LLD can start processing it immediately.
1994 *
1995 * Return:
1996 * Pointer to the request at the top of @q if available. Null
1997 * otherwise.
1998 *
1999 * Context:
2000 * queue_lock must be held.
2001 */
2002struct request *blk_fetch_request(struct request_queue *q)
2003{
2004 struct request *rq;
2005
2006 rq = blk_peek_request(q);
2007 if (rq)
2008 blk_start_request(rq);
2009 return rq;
2010}
2011EXPORT_SYMBOL(blk_fetch_request);
2012
2013/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002014 * blk_update_request - Special helper function for request stacking drivers
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002015 * @req: the request being processed
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002016 * @error: %0 for success, < %0 for error
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002017 * @nr_bytes: number of bytes to complete @req
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002018 *
2019 * Description:
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002020 * Ends I/O on a number of bytes attached to @req, but doesn't complete
2021 * the request structure even if @req doesn't have leftover.
2022 * If @req has leftover, sets it up for the next range of segments.
Tejun Heo2e60e022009-04-23 11:05:18 +09002023 *
2024 * This special helper function is only for request stacking drivers
2025 * (e.g. request-based dm) so that they can handle partial completion.
2026 * Actual device drivers should use blk_end_request instead.
2027 *
2028 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
2029 * %false return from this function.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002030 *
2031 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002032 * %false - this request doesn't have any more data
2033 * %true - this request has more data
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002034 **/
Tejun Heo2e60e022009-04-23 11:05:18 +09002035bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036{
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05002037 int total_bytes, bio_nbytes, next_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 struct bio *bio;
2039
Tejun Heo2e60e022009-04-23 11:05:18 +09002040 if (!req->bio)
2041 return false;
2042
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01002043 trace_block_rq_complete(req->q, req);
Jens Axboe2056a782006-03-23 20:00:26 +01002044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 /*
Tejun Heo6f414692009-04-19 07:00:41 +09002046 * For fs requests, rq is just carrier of independent bio's
2047 * and each partial completion should be handled separately.
2048 * Reset per-request error on each partial completion.
2049 *
2050 * TODO: tj: This is too subtle. It would be better to let
2051 * low level drivers do what they see fit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002053 if (req->cmd_type == REQ_TYPE_FS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 req->errors = 0;
2055
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002056 if (error && req->cmd_type == REQ_TYPE_FS &&
2057 !(req->cmd_flags & REQ_QUIET)) {
Hannes Reinecke79775562011-01-18 10:13:13 +01002058 char *error_type;
2059
2060 switch (error) {
2061 case -ENOLINK:
2062 error_type = "recoverable transport";
2063 break;
2064 case -EREMOTEIO:
2065 error_type = "critical target";
2066 break;
2067 case -EBADE:
2068 error_type = "critical nexus";
2069 break;
2070 case -EIO:
2071 default:
2072 error_type = "I/O";
2073 break;
2074 }
2075 printk(KERN_ERR "end_request: %s error, dev %s, sector %llu\n",
2076 error_type, req->rq_disk ? req->rq_disk->disk_name : "?",
2077 (unsigned long long)blk_rq_pos(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 }
2079
Jens Axboebc58ba92009-01-23 10:54:44 +01002080 blk_account_io_completion(req, nr_bytes);
Jens Axboed72d9042005-11-01 08:35:42 +01002081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 total_bytes = bio_nbytes = 0;
2083 while ((bio = req->bio) != NULL) {
2084 int nbytes;
2085
2086 if (nr_bytes >= bio->bi_size) {
2087 req->bio = bio->bi_next;
2088 nbytes = bio->bi_size;
NeilBrown5bb23a62007-09-27 12:46:13 +02002089 req_bio_endio(req, bio, nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 next_idx = 0;
2091 bio_nbytes = 0;
2092 } else {
2093 int idx = bio->bi_idx + next_idx;
2094
Kazuhisa Ichikawaaf498d72009-05-12 13:27:45 +02002095 if (unlikely(idx >= bio->bi_vcnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 blk_dump_rq_flags(req, "__end_that");
Jens Axboe6728cb02008-01-31 13:03:55 +01002097 printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
Kazuhisa Ichikawaaf498d72009-05-12 13:27:45 +02002098 __func__, idx, bio->bi_vcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 break;
2100 }
2101
2102 nbytes = bio_iovec_idx(bio, idx)->bv_len;
2103 BIO_BUG_ON(nbytes > bio->bi_size);
2104
2105 /*
2106 * not a complete bvec done
2107 */
2108 if (unlikely(nbytes > nr_bytes)) {
2109 bio_nbytes += nr_bytes;
2110 total_bytes += nr_bytes;
2111 break;
2112 }
2113
2114 /*
2115 * advance to the next vector
2116 */
2117 next_idx++;
2118 bio_nbytes += nbytes;
2119 }
2120
2121 total_bytes += nbytes;
2122 nr_bytes -= nbytes;
2123
Jens Axboe6728cb02008-01-31 13:03:55 +01002124 bio = req->bio;
2125 if (bio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 /*
2127 * end more in this run, or just return 'not-done'
2128 */
2129 if (unlikely(nr_bytes <= 0))
2130 break;
2131 }
2132 }
2133
2134 /*
2135 * completely done
2136 */
Tejun Heo2e60e022009-04-23 11:05:18 +09002137 if (!req->bio) {
2138 /*
2139 * Reset counters so that the request stacking driver
2140 * can find how many bytes remain in the request
2141 * later.
2142 */
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002143 req->__data_len = 0;
Tejun Heo2e60e022009-04-23 11:05:18 +09002144 return false;
2145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
2147 /*
2148 * if the request wasn't completed, update state
2149 */
2150 if (bio_nbytes) {
NeilBrown5bb23a62007-09-27 12:46:13 +02002151 req_bio_endio(req, bio, bio_nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 bio->bi_idx += next_idx;
2153 bio_iovec(bio)->bv_offset += nr_bytes;
2154 bio_iovec(bio)->bv_len -= nr_bytes;
2155 }
2156
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002157 req->__data_len -= total_bytes;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002158 req->buffer = bio_data(req->bio);
2159
2160 /* update sector only for requests with clear definition of sector */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002161 if (req->cmd_type == REQ_TYPE_FS || (req->cmd_flags & REQ_DISCARD))
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002162 req->__sector += total_bytes >> 9;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002163
Tejun Heo80a761f2009-07-03 17:48:17 +09002164 /* mixed attributes always follow the first bio */
2165 if (req->cmd_flags & REQ_MIXED_MERGE) {
2166 req->cmd_flags &= ~REQ_FAILFAST_MASK;
2167 req->cmd_flags |= req->bio->bi_rw & REQ_FAILFAST_MASK;
2168 }
2169
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002170 /*
2171 * If total number of sectors is less than the first segment
2172 * size, something has gone terribly wrong.
2173 */
2174 if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
Jens Axboe81829242011-03-30 09:51:33 +02002175 blk_dump_rq_flags(req, "request botched");
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002176 req->__data_len = blk_rq_cur_bytes(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002177 }
2178
2179 /* recalculate the number of segments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 blk_recalc_rq_segments(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002181
Tejun Heo2e60e022009-04-23 11:05:18 +09002182 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183}
Tejun Heo2e60e022009-04-23 11:05:18 +09002184EXPORT_SYMBOL_GPL(blk_update_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
Tejun Heo2e60e022009-04-23 11:05:18 +09002186static bool blk_update_bidi_request(struct request *rq, int error,
2187 unsigned int nr_bytes,
2188 unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002189{
Tejun Heo2e60e022009-04-23 11:05:18 +09002190 if (blk_update_request(rq, error, nr_bytes))
2191 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002192
Tejun Heo2e60e022009-04-23 11:05:18 +09002193 /* Bidi request must be completed as a whole */
2194 if (unlikely(blk_bidi_rq(rq)) &&
2195 blk_update_request(rq->next_rq, error, bidi_bytes))
2196 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002197
Jens Axboee2e1a142010-06-09 10:42:09 +02002198 if (blk_queue_add_random(rq->q))
2199 add_disk_randomness(rq->rq_disk);
Tejun Heo2e60e022009-04-23 11:05:18 +09002200
2201 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202}
2203
James Bottomley28018c22010-07-01 19:49:17 +09002204/**
2205 * blk_unprep_request - unprepare a request
2206 * @req: the request
2207 *
2208 * This function makes a request ready for complete resubmission (or
2209 * completion). It happens only after all error handling is complete,
2210 * so represents the appropriate moment to deallocate any resources
2211 * that were allocated to the request in the prep_rq_fn. The queue
2212 * lock is held when calling this.
2213 */
2214void blk_unprep_request(struct request *req)
2215{
2216 struct request_queue *q = req->q;
2217
2218 req->cmd_flags &= ~REQ_DONTPREP;
2219 if (q->unprep_rq_fn)
2220 q->unprep_rq_fn(q, req);
2221}
2222EXPORT_SYMBOL_GPL(blk_unprep_request);
2223
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224/*
2225 * queue lock must be held
2226 */
Tejun Heo2e60e022009-04-23 11:05:18 +09002227static void blk_finish_request(struct request *req, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228{
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002229 if (blk_rq_tagged(req))
2230 blk_queue_end_tag(req->q, req);
2231
James Bottomleyba396a62009-05-27 14:17:08 +02002232 BUG_ON(blk_queued_rq(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002234 if (unlikely(laptop_mode) && req->cmd_type == REQ_TYPE_FS)
Matthew Garrett31373d02010-04-06 14:25:14 +02002235 laptop_io_completion(&req->q->backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Mike Andersone78042e2008-10-30 02:16:20 -07002237 blk_delete_timer(req);
2238
James Bottomley28018c22010-07-01 19:49:17 +09002239 if (req->cmd_flags & REQ_DONTPREP)
2240 blk_unprep_request(req);
2241
2242
Jens Axboebc58ba92009-01-23 10:54:44 +01002243 blk_account_io_done(req);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002244
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 if (req->end_io)
Tejun Heo8ffdc652006-01-06 09:49:03 +01002246 req->end_io(req, error);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002247 else {
2248 if (blk_bidi_rq(req))
2249 __blk_put_request(req->next_rq->q, req->next_rq);
2250
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 __blk_put_request(req->q, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 }
2253}
2254
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05002255/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002256 * blk_end_bidi_request - Complete a bidi request
2257 * @rq: the request to complete
Randy Dunlap710027a2008-08-19 20:13:11 +02002258 * @error: %0 for success, < %0 for error
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002259 * @nr_bytes: number of bytes to complete @rq
2260 * @bidi_bytes: number of bytes to complete @rq->next_rq
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002261 *
2262 * Description:
2263 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
Tejun Heo2e60e022009-04-23 11:05:18 +09002264 * Drivers that supports bidi can safely call this member for any
2265 * type of request, bidi or uni. In the later case @bidi_bytes is
2266 * just ignored.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002267 *
2268 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002269 * %false - we are done with this request
2270 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002271 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002272static bool blk_end_bidi_request(struct request *rq, int error,
2273 unsigned int nr_bytes, unsigned int bidi_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002274{
2275 struct request_queue *q = rq->q;
Tejun Heo2e60e022009-04-23 11:05:18 +09002276 unsigned long flags;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002277
Tejun Heo2e60e022009-04-23 11:05:18 +09002278 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2279 return true;
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002280
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002281 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo2e60e022009-04-23 11:05:18 +09002282 blk_finish_request(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002283 spin_unlock_irqrestore(q->queue_lock, flags);
2284
Tejun Heo2e60e022009-04-23 11:05:18 +09002285 return false;
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002286}
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002287
2288/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002289 * __blk_end_bidi_request - Complete a bidi request with queue lock held
2290 * @rq: the request to complete
2291 * @error: %0 for success, < %0 for error
2292 * @nr_bytes: number of bytes to complete @rq
2293 * @bidi_bytes: number of bytes to complete @rq->next_rq
Tejun Heo5efccd12009-04-23 11:05:18 +09002294 *
2295 * Description:
Tejun Heo2e60e022009-04-23 11:05:18 +09002296 * Identical to blk_end_bidi_request() except that queue lock is
2297 * assumed to be locked on entry and remains so on return.
Tejun Heo5efccd12009-04-23 11:05:18 +09002298 *
Tejun Heo2e60e022009-04-23 11:05:18 +09002299 * Return:
2300 * %false - we are done with this request
2301 * %true - still buffers pending for this request
Tejun Heo5efccd12009-04-23 11:05:18 +09002302 **/
Jeff Moyer4853aba2011-08-15 21:37:25 +02002303bool __blk_end_bidi_request(struct request *rq, int error,
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002304 unsigned int nr_bytes, unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002305{
Tejun Heo2e60e022009-04-23 11:05:18 +09002306 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2307 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002308
Tejun Heo2e60e022009-04-23 11:05:18 +09002309 blk_finish_request(rq, error);
Tejun Heo5efccd12009-04-23 11:05:18 +09002310
Tejun Heo2e60e022009-04-23 11:05:18 +09002311 return false;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002312}
2313
2314/**
2315 * blk_end_request - Helper function for drivers to complete the request.
2316 * @rq: the request being processed
2317 * @error: %0 for success, < %0 for error
2318 * @nr_bytes: number of bytes to complete
2319 *
2320 * Description:
2321 * Ends I/O on a number of bytes attached to @rq.
2322 * If @rq has leftover, sets it up for the next range of segments.
2323 *
2324 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002325 * %false - we are done with this request
2326 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002327 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002328bool blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002329{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002330 return blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002331}
Jens Axboe56ad1742009-07-28 22:11:24 +02002332EXPORT_SYMBOL(blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002333
2334/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002335 * blk_end_request_all - Helper function for drives to finish the request.
2336 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002337 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002338 *
2339 * Description:
2340 * Completely finish @rq.
2341 */
2342void blk_end_request_all(struct request *rq, int error)
2343{
2344 bool pending;
2345 unsigned int bidi_bytes = 0;
2346
2347 if (unlikely(blk_bidi_rq(rq)))
2348 bidi_bytes = blk_rq_bytes(rq->next_rq);
2349
2350 pending = blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2351 BUG_ON(pending);
2352}
Jens Axboe56ad1742009-07-28 22:11:24 +02002353EXPORT_SYMBOL(blk_end_request_all);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002354
2355/**
2356 * blk_end_request_cur - Helper function to finish the current request chunk.
2357 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002358 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002359 *
2360 * Description:
2361 * Complete the current consecutively mapped chunk from @rq.
2362 *
2363 * Return:
2364 * %false - we are done with this request
2365 * %true - still buffers pending for this request
2366 */
2367bool blk_end_request_cur(struct request *rq, int error)
2368{
2369 return blk_end_request(rq, error, blk_rq_cur_bytes(rq));
2370}
Jens Axboe56ad1742009-07-28 22:11:24 +02002371EXPORT_SYMBOL(blk_end_request_cur);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002372
2373/**
Tejun Heo80a761f2009-07-03 17:48:17 +09002374 * blk_end_request_err - Finish a request till the next failure boundary.
2375 * @rq: the request to finish till the next failure boundary for
2376 * @error: must be negative errno
2377 *
2378 * Description:
2379 * Complete @rq till the next failure boundary.
2380 *
2381 * Return:
2382 * %false - we are done with this request
2383 * %true - still buffers pending for this request
2384 */
2385bool blk_end_request_err(struct request *rq, int error)
2386{
2387 WARN_ON(error >= 0);
2388 return blk_end_request(rq, error, blk_rq_err_bytes(rq));
2389}
2390EXPORT_SYMBOL_GPL(blk_end_request_err);
2391
2392/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002393 * __blk_end_request - Helper function for drivers to complete the request.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002394 * @rq: the request being processed
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002395 * @error: %0 for success, < %0 for error
2396 * @nr_bytes: number of bytes to complete
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002397 *
2398 * Description:
2399 * Must be called with queue lock held unlike blk_end_request().
2400 *
2401 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002402 * %false - we are done with this request
2403 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002404 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002405bool __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002406{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002407 return __blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002408}
Jens Axboe56ad1742009-07-28 22:11:24 +02002409EXPORT_SYMBOL(__blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002410
2411/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002412 * __blk_end_request_all - Helper function for drives to finish the request.
2413 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002414 * @error: %0 for success, < %0 for error
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002415 *
2416 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002417 * Completely finish @rq. Must be called with queue lock held.
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002418 */
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002419void __blk_end_request_all(struct request *rq, int error)
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002420{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002421 bool pending;
2422 unsigned int bidi_bytes = 0;
2423
2424 if (unlikely(blk_bidi_rq(rq)))
2425 bidi_bytes = blk_rq_bytes(rq->next_rq);
2426
2427 pending = __blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2428 BUG_ON(pending);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002429}
Jens Axboe56ad1742009-07-28 22:11:24 +02002430EXPORT_SYMBOL(__blk_end_request_all);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002431
2432/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002433 * __blk_end_request_cur - Helper function to finish the current request chunk.
2434 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002435 * @error: %0 for success, < %0 for error
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002436 *
2437 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002438 * Complete the current consecutively mapped chunk from @rq. Must
2439 * be called with queue lock held.
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002440 *
2441 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002442 * %false - we are done with this request
2443 * %true - still buffers pending for this request
2444 */
2445bool __blk_end_request_cur(struct request *rq, int error)
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002446{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002447 return __blk_end_request(rq, error, blk_rq_cur_bytes(rq));
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002448}
Jens Axboe56ad1742009-07-28 22:11:24 +02002449EXPORT_SYMBOL(__blk_end_request_cur);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002450
Tejun Heo80a761f2009-07-03 17:48:17 +09002451/**
2452 * __blk_end_request_err - Finish a request till the next failure boundary.
2453 * @rq: the request to finish till the next failure boundary for
2454 * @error: must be negative errno
2455 *
2456 * Description:
2457 * Complete @rq till the next failure boundary. Must be called
2458 * with queue lock held.
2459 *
2460 * Return:
2461 * %false - we are done with this request
2462 * %true - still buffers pending for this request
2463 */
2464bool __blk_end_request_err(struct request *rq, int error)
2465{
2466 WARN_ON(error >= 0);
2467 return __blk_end_request(rq, error, blk_rq_err_bytes(rq));
2468}
2469EXPORT_SYMBOL_GPL(__blk_end_request_err);
2470
Jens Axboe86db1e22008-01-29 14:53:40 +01002471void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2472 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473{
Tejun Heoa82afdf2009-07-03 17:48:16 +09002474 /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002475 rq->cmd_flags |= bio->bi_rw & REQ_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
David Woodhousefb2dce82008-08-05 18:01:53 +01002477 if (bio_has_data(bio)) {
2478 rq->nr_phys_segments = bio_phys_segments(q, bio);
David Woodhousefb2dce82008-08-05 18:01:53 +01002479 rq->buffer = bio_data(bio);
2480 }
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002481 rq->__data_len = bio->bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 rq->bio = rq->biotail = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
NeilBrown66846572007-08-16 13:31:28 +02002484 if (bio->bi_bdev)
2485 rq->rq_disk = bio->bi_bdev->bd_disk;
2486}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
Ilya Loginov2d4dc892009-11-26 09:16:19 +01002488#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
2489/**
2490 * rq_flush_dcache_pages - Helper function to flush all pages in a request
2491 * @rq: the request to be flushed
2492 *
2493 * Description:
2494 * Flush all pages in @rq.
2495 */
2496void rq_flush_dcache_pages(struct request *rq)
2497{
2498 struct req_iterator iter;
2499 struct bio_vec *bvec;
2500
2501 rq_for_each_segment(bvec, rq, iter)
2502 flush_dcache_page(bvec->bv_page);
2503}
2504EXPORT_SYMBOL_GPL(rq_flush_dcache_pages);
2505#endif
2506
Kiyoshi Uedaef9e3fa2008-10-01 16:12:15 +02002507/**
2508 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
2509 * @q : the queue of the device being checked
2510 *
2511 * Description:
2512 * Check if underlying low-level drivers of a device are busy.
2513 * If the drivers want to export their busy state, they must set own
2514 * exporting function using blk_queue_lld_busy() first.
2515 *
2516 * Basically, this function is used only by request stacking drivers
2517 * to stop dispatching requests to underlying devices when underlying
2518 * devices are busy. This behavior helps more I/O merging on the queue
2519 * of the request stacking driver and prevents I/O throughput regression
2520 * on burst I/O load.
2521 *
2522 * Return:
2523 * 0 - Not busy (The request stacking driver should dispatch request)
2524 * 1 - Busy (The request stacking driver should stop dispatching request)
2525 */
2526int blk_lld_busy(struct request_queue *q)
2527{
2528 if (q->lld_busy_fn)
2529 return q->lld_busy_fn(q);
2530
2531 return 0;
2532}
2533EXPORT_SYMBOL_GPL(blk_lld_busy);
2534
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002535/**
2536 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
2537 * @rq: the clone request to be cleaned up
2538 *
2539 * Description:
2540 * Free all bios in @rq for a cloned request.
2541 */
2542void blk_rq_unprep_clone(struct request *rq)
2543{
2544 struct bio *bio;
2545
2546 while ((bio = rq->bio) != NULL) {
2547 rq->bio = bio->bi_next;
2548
2549 bio_put(bio);
2550 }
2551}
2552EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
2553
2554/*
2555 * Copy attributes of the original request to the clone request.
2556 * The actual data parts (e.g. ->cmd, ->buffer, ->sense) are not copied.
2557 */
2558static void __blk_rq_prep_clone(struct request *dst, struct request *src)
2559{
2560 dst->cpu = src->cpu;
Tejun Heo3a2edd02010-09-03 11:56:18 +02002561 dst->cmd_flags = (src->cmd_flags & REQ_CLONE_MASK) | REQ_NOMERGE;
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002562 dst->cmd_type = src->cmd_type;
2563 dst->__sector = blk_rq_pos(src);
2564 dst->__data_len = blk_rq_bytes(src);
2565 dst->nr_phys_segments = src->nr_phys_segments;
2566 dst->ioprio = src->ioprio;
2567 dst->extra_len = src->extra_len;
2568}
2569
2570/**
2571 * blk_rq_prep_clone - Helper function to setup clone request
2572 * @rq: the request to be setup
2573 * @rq_src: original request to be cloned
2574 * @bs: bio_set that bios for clone are allocated from
2575 * @gfp_mask: memory allocation mask for bio
2576 * @bio_ctr: setup function to be called for each clone bio.
2577 * Returns %0 for success, non %0 for failure.
2578 * @data: private data to be passed to @bio_ctr
2579 *
2580 * Description:
2581 * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
2582 * The actual data parts of @rq_src (e.g. ->cmd, ->buffer, ->sense)
2583 * are not copied, and copying such parts is the caller's responsibility.
2584 * Also, pages which the original bios are pointing to are not copied
2585 * and the cloned bios just point same pages.
2586 * So cloned bios must be completed before original bios, which means
2587 * the caller must complete @rq before @rq_src.
2588 */
2589int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
2590 struct bio_set *bs, gfp_t gfp_mask,
2591 int (*bio_ctr)(struct bio *, struct bio *, void *),
2592 void *data)
2593{
2594 struct bio *bio, *bio_src;
2595
2596 if (!bs)
2597 bs = fs_bio_set;
2598
2599 blk_rq_init(NULL, rq);
2600
2601 __rq_for_each_bio(bio_src, rq_src) {
2602 bio = bio_alloc_bioset(gfp_mask, bio_src->bi_max_vecs, bs);
2603 if (!bio)
2604 goto free_and_out;
2605
2606 __bio_clone(bio, bio_src);
2607
2608 if (bio_integrity(bio_src) &&
Martin K. Petersen7878cba2009-06-26 15:37:49 +02002609 bio_integrity_clone(bio, bio_src, gfp_mask, bs))
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002610 goto free_and_out;
2611
2612 if (bio_ctr && bio_ctr(bio, bio_src, data))
2613 goto free_and_out;
2614
2615 if (rq->bio) {
2616 rq->biotail->bi_next = bio;
2617 rq->biotail = bio;
2618 } else
2619 rq->bio = rq->biotail = bio;
2620 }
2621
2622 __blk_rq_prep_clone(rq, rq_src);
2623
2624 return 0;
2625
2626free_and_out:
2627 if (bio)
2628 bio_free(bio, bs);
2629 blk_rq_unprep_clone(rq);
2630
2631 return -ENOMEM;
2632}
2633EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
2634
Jens Axboe18887ad2008-07-28 13:08:45 +02002635int kblockd_schedule_work(struct request_queue *q, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636{
2637 return queue_work(kblockd_workqueue, work);
2638}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639EXPORT_SYMBOL(kblockd_schedule_work);
2640
Vivek Goyale43473b2010-09-15 17:06:35 -04002641int kblockd_schedule_delayed_work(struct request_queue *q,
2642 struct delayed_work *dwork, unsigned long delay)
2643{
2644 return queue_delayed_work(kblockd_workqueue, dwork, delay);
2645}
2646EXPORT_SYMBOL(kblockd_schedule_delayed_work);
2647
Jens Axboe73c10102011-03-08 13:19:51 +01002648#define PLUG_MAGIC 0x91827364
2649
Suresh Jayaraman75df7132011-09-21 10:00:16 +02002650/**
2651 * blk_start_plug - initialize blk_plug and track it inside the task_struct
2652 * @plug: The &struct blk_plug that needs to be initialized
2653 *
2654 * Description:
2655 * Tracking blk_plug inside the task_struct will help with auto-flushing the
2656 * pending I/O should the task end up blocking between blk_start_plug() and
2657 * blk_finish_plug(). This is important from a performance perspective, but
2658 * also ensures that we don't deadlock. For instance, if the task is blocking
2659 * for a memory allocation, memory reclaim could end up wanting to free a
2660 * page belonging to that request that is currently residing in our private
2661 * plug. By flushing the pending I/O when the process goes to sleep, we avoid
2662 * this kind of deadlock.
2663 */
Jens Axboe73c10102011-03-08 13:19:51 +01002664void blk_start_plug(struct blk_plug *plug)
2665{
2666 struct task_struct *tsk = current;
2667
2668 plug->magic = PLUG_MAGIC;
2669 INIT_LIST_HEAD(&plug->list);
NeilBrown048c9372011-04-18 09:52:22 +02002670 INIT_LIST_HEAD(&plug->cb_list);
Jens Axboe73c10102011-03-08 13:19:51 +01002671 plug->should_sort = 0;
2672
2673 /*
2674 * If this is a nested plug, don't actually assign it. It will be
2675 * flushed on its own.
2676 */
2677 if (!tsk->plug) {
2678 /*
2679 * Store ordering should not be needed here, since a potential
2680 * preempt will imply a full memory barrier
2681 */
2682 tsk->plug = plug;
2683 }
2684}
2685EXPORT_SYMBOL(blk_start_plug);
2686
2687static int plug_rq_cmp(void *priv, struct list_head *a, struct list_head *b)
2688{
2689 struct request *rqa = container_of(a, struct request, queuelist);
2690 struct request *rqb = container_of(b, struct request, queuelist);
2691
Konstantin Khlebnikovf83e8262011-04-04 00:15:02 +02002692 return !(rqa->q <= rqb->q);
Jens Axboe73c10102011-03-08 13:19:51 +01002693}
2694
Jens Axboe49cac012011-04-16 13:51:05 +02002695/*
2696 * If 'from_schedule' is true, then postpone the dispatch of requests
2697 * until a safe kblockd context. We due this to avoid accidental big
2698 * additional stack usage in driver dispatch, in places where the originally
2699 * plugger did not intend it.
2700 */
Jens Axboef6603782011-04-15 15:49:07 +02002701static void queue_unplugged(struct request_queue *q, unsigned int depth,
Jens Axboe49cac012011-04-16 13:51:05 +02002702 bool from_schedule)
Jens Axboe99e22592011-04-18 09:59:55 +02002703 __releases(q->queue_lock)
Jens Axboe94b5eb22011-04-12 10:12:19 +02002704{
Jens Axboe49cac012011-04-16 13:51:05 +02002705 trace_block_unplug(q, depth, !from_schedule);
Jens Axboe99e22592011-04-18 09:59:55 +02002706
2707 /*
2708 * If we are punting this to kblockd, then we can safely drop
2709 * the queue_lock before waking kblockd (which needs to take
2710 * this lock).
2711 */
2712 if (from_schedule) {
2713 spin_unlock(q->queue_lock);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02002714 blk_run_queue_async(q);
Jens Axboe99e22592011-04-18 09:59:55 +02002715 } else {
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02002716 __blk_run_queue(q);
Jens Axboe99e22592011-04-18 09:59:55 +02002717 spin_unlock(q->queue_lock);
2718 }
2719
Jens Axboe94b5eb22011-04-12 10:12:19 +02002720}
2721
NeilBrown048c9372011-04-18 09:52:22 +02002722static void flush_plug_callbacks(struct blk_plug *plug)
2723{
2724 LIST_HEAD(callbacks);
2725
2726 if (list_empty(&plug->cb_list))
2727 return;
2728
2729 list_splice_init(&plug->cb_list, &callbacks);
2730
2731 while (!list_empty(&callbacks)) {
2732 struct blk_plug_cb *cb = list_first_entry(&callbacks,
2733 struct blk_plug_cb,
2734 list);
2735 list_del(&cb->list);
2736 cb->callback(cb);
2737 }
2738}
2739
Jens Axboe49cac012011-04-16 13:51:05 +02002740void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
Jens Axboe73c10102011-03-08 13:19:51 +01002741{
2742 struct request_queue *q;
2743 unsigned long flags;
2744 struct request *rq;
NeilBrown109b8122011-04-11 14:13:10 +02002745 LIST_HEAD(list);
Jens Axboe94b5eb22011-04-12 10:12:19 +02002746 unsigned int depth;
Jens Axboe73c10102011-03-08 13:19:51 +01002747
2748 BUG_ON(plug->magic != PLUG_MAGIC);
2749
NeilBrown048c9372011-04-18 09:52:22 +02002750 flush_plug_callbacks(plug);
Jens Axboe73c10102011-03-08 13:19:51 +01002751 if (list_empty(&plug->list))
2752 return;
2753
NeilBrown109b8122011-04-11 14:13:10 +02002754 list_splice_init(&plug->list, &list);
2755
2756 if (plug->should_sort) {
2757 list_sort(NULL, &list, plug_rq_cmp);
2758 plug->should_sort = 0;
2759 }
Jens Axboe73c10102011-03-08 13:19:51 +01002760
2761 q = NULL;
Jens Axboe94b5eb22011-04-12 10:12:19 +02002762 depth = 0;
Jens Axboe18811272011-04-12 10:11:24 +02002763
2764 /*
2765 * Save and disable interrupts here, to avoid doing it for every
2766 * queue lock we have to take.
2767 */
Jens Axboe73c10102011-03-08 13:19:51 +01002768 local_irq_save(flags);
NeilBrown109b8122011-04-11 14:13:10 +02002769 while (!list_empty(&list)) {
2770 rq = list_entry_rq(list.next);
Jens Axboe73c10102011-03-08 13:19:51 +01002771 list_del_init(&rq->queuelist);
Jens Axboe73c10102011-03-08 13:19:51 +01002772 BUG_ON(!rq->q);
2773 if (rq->q != q) {
Jens Axboe99e22592011-04-18 09:59:55 +02002774 /*
2775 * This drops the queue lock
2776 */
2777 if (q)
Jens Axboe49cac012011-04-16 13:51:05 +02002778 queue_unplugged(q, depth, from_schedule);
Jens Axboe73c10102011-03-08 13:19:51 +01002779 q = rq->q;
Jens Axboe94b5eb22011-04-12 10:12:19 +02002780 depth = 0;
Jens Axboe73c10102011-03-08 13:19:51 +01002781 spin_lock(q->queue_lock);
2782 }
Jens Axboe73c10102011-03-08 13:19:51 +01002783 /*
2784 * rq is already accounted, so use raw insert
2785 */
Jens Axboe401a18e2011-03-25 16:57:52 +01002786 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA))
2787 __elv_add_request(q, rq, ELEVATOR_INSERT_FLUSH);
2788 else
2789 __elv_add_request(q, rq, ELEVATOR_INSERT_SORT_MERGE);
Jens Axboe94b5eb22011-04-12 10:12:19 +02002790
2791 depth++;
Jens Axboe73c10102011-03-08 13:19:51 +01002792 }
2793
Jens Axboe99e22592011-04-18 09:59:55 +02002794 /*
2795 * This drops the queue lock
2796 */
2797 if (q)
Jens Axboe49cac012011-04-16 13:51:05 +02002798 queue_unplugged(q, depth, from_schedule);
Jens Axboe73c10102011-03-08 13:19:51 +01002799
Jens Axboe73c10102011-03-08 13:19:51 +01002800 local_irq_restore(flags);
2801}
Jens Axboe73c10102011-03-08 13:19:51 +01002802
2803void blk_finish_plug(struct blk_plug *plug)
2804{
Jens Axboef6603782011-04-15 15:49:07 +02002805 blk_flush_plug_list(plug, false);
Christoph Hellwig88b996c2011-04-15 15:20:10 +02002806
2807 if (plug == current->plug)
2808 current->plug = NULL;
Jens Axboe73c10102011-03-08 13:19:51 +01002809}
2810EXPORT_SYMBOL(blk_finish_plug);
2811
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812int __init blk_dev_init(void)
2813{
Nikanth Karthikesan9eb55b02009-04-27 14:53:54 +02002814 BUILD_BUG_ON(__REQ_NR_BITS > 8 *
2815 sizeof(((struct request *)0)->cmd_flags));
2816
Tejun Heo89b90be2011-01-03 15:01:47 +01002817 /* used for unplugging and affects IO latency/throughput - HIGHPRI */
2818 kblockd_workqueue = alloc_workqueue("kblockd",
2819 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 if (!kblockd_workqueue)
2821 panic("Failed to create kblockd\n");
2822
2823 request_cachep = kmem_cache_create("blkdev_requests",
Paul Mundt20c2df82007-07-20 10:11:58 +09002824 sizeof(struct request), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
Jens Axboe8324aa92008-01-29 14:51:59 +01002826 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02002827 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 return 0;
2830}