blob: 718897e6d37f2293615a858ea880b8e245ad7050 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 1994, Karl Keyte: Added support for disk statistics
4 * Elevator latency, (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
5 * Queue request tables / lock, selectable elevator, Jens Axboe <axboe@suse.de>
Jens Axboe6728cb02008-01-31 13:03:55 +01006 * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au>
7 * - July2000
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
9 */
10
11/*
12 * This handles all read/write requests to block devices
13 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/backing-dev.h>
17#include <linux/bio.h>
18#include <linux/blkdev.h>
19#include <linux/highmem.h>
20#include <linux/mm.h>
21#include <linux/kernel_stat.h>
22#include <linux/string.h>
23#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/completion.h>
25#include <linux/slab.h>
26#include <linux/swap.h>
27#include <linux/writeback.h>
Andrew Mortonfaccbd42006-12-10 02:19:35 -080028#include <linux/task_io_accounting_ops.h>
Akinobu Mitac17bb492006-12-08 02:39:46 -080029#include <linux/fault-inject.h>
Li Zefan55782132009-06-09 13:43:05 +080030
31#define CREATE_TRACE_POINTS
32#include <trace/events/block.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Jens Axboe8324aa92008-01-29 14:51:59 +010034#include "blk.h"
35
Ingo Molnar0bfc2452008-11-26 11:59:56 +010036EXPORT_TRACEPOINT_SYMBOL_GPL(block_remap);
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +020037EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_remap);
Li Zefan55782132009-06-09 13:43:05 +080038EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_complete);
Ingo Molnar0bfc2452008-11-26 11:59:56 +010039
Jens Axboe165125e2007-07-24 09:28:11 +020040static int __make_request(struct request_queue *q, struct bio *bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/*
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 Heo83096eb2009-05-07 22:24:39 +090067 part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq));
Tejun Heoc9959052008-08-25 19:47:21 +090068
Jens Axboe28f13702008-05-07 10:15:46 +020069 if (!new_io)
Tejun Heo074a7ac2008-08-25 19:56:14 +090070 part_stat_inc(cpu, part, merges[rw]);
Jens Axboe28f13702008-05-07 10:15:46 +020071 else {
Tejun Heo074a7ac2008-08-25 19:56:14 +090072 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +020073 part_inc_in_flight(part, rw);
Jens Axboe26b82562008-01-29 13:54:41 +010074 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +020075
Tejun Heo074a7ac2008-08-25 19:56:14 +090076 part_stat_unlock();
Jens Axboe26b82562008-01-29 13:54:41 +010077}
78
Jens Axboe8324aa92008-01-29 14:51:59 +010079void blk_queue_congestion_threshold(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
81 int nr;
82
83 nr = q->nr_requests - (q->nr_requests / 8) + 1;
84 if (nr > q->nr_requests)
85 nr = q->nr_requests;
86 q->nr_congestion_on = nr;
87
88 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
89 if (nr < 1)
90 nr = 1;
91 q->nr_congestion_off = nr;
92}
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/**
95 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
96 * @bdev: device
97 *
98 * Locates the passed device's request queue and returns the address of its
99 * backing_dev_info
100 *
101 * Will return NULL if the request queue cannot be located.
102 */
103struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
104{
105 struct backing_dev_info *ret = NULL;
Jens Axboe165125e2007-07-24 09:28:11 +0200106 struct request_queue *q = bdev_get_queue(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 if (q)
109 ret = &q->backing_dev_info;
110 return ret;
111}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112EXPORT_SYMBOL(blk_get_backing_dev_info);
113
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200114void blk_rq_init(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200116 memset(rq, 0, sizeof(*rq));
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 INIT_LIST_HEAD(&rq->queuelist);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700119 INIT_LIST_HEAD(&rq->timeout_list);
Jens Axboec7c22e42008-09-13 20:26:01 +0200120 rq->cpu = -1;
Jens Axboe63a71382008-02-08 12:41:03 +0100121 rq->q = q;
Tejun Heoa2dec7b2009-05-07 22:24:44 +0900122 rq->__sector = (sector_t) -1;
Jens Axboe2e662b62006-07-13 11:55:04 +0200123 INIT_HLIST_NODE(&rq->hash);
124 RB_CLEAR_NODE(&rq->rb_node);
FUJITA Tomonorid7e3c322008-04-29 09:54:39 +0200125 rq->cmd = rq->__cmd;
Li Zefane2494e12009-04-02 13:43:26 +0800126 rq->cmd_len = BLK_MAX_CDB;
Jens Axboe63a71382008-02-08 12:41:03 +0100127 rq->tag = -1;
Jens Axboe63a71382008-02-08 12:41:03 +0100128 rq->ref_count = 1;
Tejun Heob243ddc2009-04-23 11:05:18 +0900129 rq->start_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200131EXPORT_SYMBOL(blk_rq_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
NeilBrown5bb23a62007-09-27 12:46:13 +0200133static void req_bio_endio(struct request *rq, struct bio *bio,
134 unsigned int nbytes, int error)
Tejun Heo797e7db2006-01-06 09:51:03 +0100135{
Jens Axboe165125e2007-07-24 09:28:11 +0200136 struct request_queue *q = rq->q;
Tejun Heo797e7db2006-01-06 09:51:03 +0100137
NeilBrown5bb23a62007-09-27 12:46:13 +0200138 if (&q->bar_rq != rq) {
139 if (error)
140 clear_bit(BIO_UPTODATE, &bio->bi_flags);
141 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
142 error = -EIO;
Tejun Heo797e7db2006-01-06 09:51:03 +0100143
NeilBrown5bb23a62007-09-27 12:46:13 +0200144 if (unlikely(nbytes > bio->bi_size)) {
Jens Axboe6728cb02008-01-31 13:03:55 +0100145 printk(KERN_ERR "%s: want %u bytes done, %u left\n",
Harvey Harrison24c03d42008-05-01 04:35:17 -0700146 __func__, nbytes, bio->bi_size);
NeilBrown5bb23a62007-09-27 12:46:13 +0200147 nbytes = bio->bi_size;
148 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100149
Keith Mannthey08bafc02008-11-25 10:24:35 +0100150 if (unlikely(rq->cmd_flags & REQ_QUIET))
151 set_bit(BIO_QUIET, &bio->bi_flags);
152
NeilBrown5bb23a62007-09-27 12:46:13 +0200153 bio->bi_size -= nbytes;
154 bio->bi_sector += (nbytes >> 9);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200155
156 if (bio_integrity(bio))
157 bio_integrity_advance(bio, nbytes);
158
NeilBrown5bb23a62007-09-27 12:46:13 +0200159 if (bio->bi_size == 0)
NeilBrown6712ecf2007-09-27 12:47:43 +0200160 bio_endio(bio, error);
NeilBrown5bb23a62007-09-27 12:46:13 +0200161 } else {
162
163 /*
164 * Okay, this is the barrier request in progress, just
165 * record the error;
166 */
167 if (error && !q->orderr)
168 q->orderr = error;
169 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100170}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172void blk_dump_rq_flags(struct request *rq, char *msg)
173{
174 int bit;
175
Jens Axboe6728cb02008-01-31 13:03:55 +0100176 printk(KERN_INFO "%s: dev %s: type=%x, flags=%x\n", msg,
Jens Axboe4aff5e22006-08-10 08:44:47 +0200177 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
178 rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Tejun Heo83096eb2009-05-07 22:24:39 +0900180 printk(KERN_INFO " sector %llu, nr/cnr %u/%u\n",
181 (unsigned long long)blk_rq_pos(rq),
182 blk_rq_sectors(rq), blk_rq_cur_sectors(rq));
Tejun Heo731ec492009-04-23 11:05:20 +0900183 printk(KERN_INFO " bio %p, biotail %p, buffer %p, len %u\n",
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900184 rq->bio, rq->biotail, rq->buffer, blk_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Jens Axboe4aff5e22006-08-10 08:44:47 +0200186 if (blk_pc_request(rq)) {
Jens Axboe6728cb02008-01-31 13:03:55 +0100187 printk(KERN_INFO " cdb: ");
FUJITA Tomonorid34c87e2008-04-29 14:37:52 +0200188 for (bit = 0; bit < BLK_MAX_CDB; bit++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 printk("%02x ", rq->cmd[bit]);
190 printk("\n");
191 }
192}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193EXPORT_SYMBOL(blk_dump_rq_flags);
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195/*
196 * "plug" the device if there are no outstanding requests: this will
197 * force the transfer to start only after we have put all the requests
198 * on the list.
199 *
200 * This is called with interrupts off and no requests on the queue and
201 * with the queue lock held.
202 */
Jens Axboe165125e2007-07-24 09:28:11 +0200203void blk_plug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 WARN_ON(!irqs_disabled());
206
207 /*
208 * don't plug a stopped queue, it must be paired with blk_start_queue()
209 * which will restart the queueing
210 */
Coywolf Qi Hunt7daac492006-04-19 10:14:49 +0200211 if (blk_queue_stopped(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return;
213
Jens Axboee48ec692008-07-03 13:18:54 +0200214 if (!queue_flag_test_and_set(QUEUE_FLAG_PLUGGED, q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100216 trace_block_plug(q);
Jens Axboe2056a782006-03-23 20:00:26 +0100217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219EXPORT_SYMBOL(blk_plug_device);
220
Jens Axboe6c5e0c42008-08-01 20:31:32 +0200221/**
222 * blk_plug_device_unlocked - plug a device without queue lock held
223 * @q: The &struct request_queue to plug
224 *
225 * Description:
226 * Like @blk_plug_device(), but grabs the queue lock and disables
227 * interrupts.
228 **/
229void blk_plug_device_unlocked(struct request_queue *q)
230{
231 unsigned long flags;
232
233 spin_lock_irqsave(q->queue_lock, flags);
234 blk_plug_device(q);
235 spin_unlock_irqrestore(q->queue_lock, flags);
236}
237EXPORT_SYMBOL(blk_plug_device_unlocked);
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239/*
240 * remove the queue from the plugged list, if present. called with
241 * queue lock held and interrupts disabled.
242 */
Jens Axboe165125e2007-07-24 09:28:11 +0200243int blk_remove_plug(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 WARN_ON(!irqs_disabled());
246
Jens Axboee48ec692008-07-03 13:18:54 +0200247 if (!queue_flag_test_and_clear(QUEUE_FLAG_PLUGGED, q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return 0;
249
250 del_timer(&q->unplug_timer);
251 return 1;
252}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253EXPORT_SYMBOL(blk_remove_plug);
254
255/*
256 * remove the plug and let it rip..
257 */
Jens Axboe165125e2007-07-24 09:28:11 +0200258void __generic_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Coywolf Qi Hunt7daac492006-04-19 10:14:49 +0200260 if (unlikely(blk_queue_stopped(q)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return;
Jens Axboea31a9732008-10-17 13:58:29 +0200262 if (!blk_remove_plug(q) && !blk_queue_nonrot(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return;
264
Jens Axboe22e2c502005-06-27 10:55:12 +0200265 q->request_fn(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268/**
269 * generic_unplug_device - fire a request queue
Jens Axboe165125e2007-07-24 09:28:11 +0200270 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 *
272 * Description:
273 * Linux uses plugging to build bigger requests queues before letting
274 * the device have at them. If a queue is plugged, the I/O scheduler
275 * is still adding and merging requests on the queue. Once the queue
276 * gets unplugged, the request_fn defined for the queue is invoked and
277 * transfers started.
278 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200279void generic_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Jens Axboedbaf2c02008-05-07 09:48:17 +0200281 if (blk_queue_plugged(q)) {
282 spin_lock_irq(q->queue_lock);
283 __generic_unplug_device(q);
284 spin_unlock_irq(q->queue_lock);
285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287EXPORT_SYMBOL(generic_unplug_device);
288
289static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
290 struct page *page)
291{
Jens Axboe165125e2007-07-24 09:28:11 +0200292 struct request_queue *q = bdi->unplug_io_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500294 blk_unplug(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
Jens Axboe86db1e22008-01-29 14:53:40 +0100297void blk_unplug_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Jens Axboe165125e2007-07-24 09:28:11 +0200299 struct request_queue *q =
300 container_of(work, struct request_queue, unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100302 trace_block_unplug_io(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 q->unplug_fn(q);
304}
305
Jens Axboe86db1e22008-01-29 14:53:40 +0100306void blk_unplug_timeout(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Jens Axboe165125e2007-07-24 09:28:11 +0200308 struct request_queue *q = (struct request_queue *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100310 trace_block_unplug_timer(q);
Jens Axboe18887ad2008-07-28 13:08:45 +0200311 kblockd_schedule_work(q, &q->unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500314void blk_unplug(struct request_queue *q)
315{
316 /*
317 * devices don't necessarily have an ->unplug_fn defined
318 */
319 if (q->unplug_fn) {
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100320 trace_block_unplug_io(q);
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500321 q->unplug_fn(q);
322 }
323}
324EXPORT_SYMBOL(blk_unplug);
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326/**
327 * blk_start_queue - restart a previously stopped queue
Jens Axboe165125e2007-07-24 09:28:11 +0200328 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 *
330 * Description:
331 * blk_start_queue() will clear the stop flag on the queue, and call
332 * the request_fn for the queue if it was in a stopped state when
333 * entered. Also see blk_stop_queue(). Queue lock must be held.
334 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200335void blk_start_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200337 WARN_ON(!irqs_disabled());
338
Nick Piggin75ad23b2008-04-29 14:48:33 +0200339 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
Tejun Heoa538cd02009-04-23 11:05:17 +0900340 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342EXPORT_SYMBOL(blk_start_queue);
343
344/**
345 * blk_stop_queue - stop a queue
Jens Axboe165125e2007-07-24 09:28:11 +0200346 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 *
348 * Description:
349 * The Linux block layer assumes that a block driver will consume all
350 * entries on the request queue when the request_fn strategy is called.
351 * Often this will not happen, because of hardware limitations (queue
352 * depth settings). If a device driver gets a 'queue full' response,
353 * or if it simply chooses not to queue more I/O at one point, it can
354 * call this function to prevent the request_fn from being called until
355 * the driver has signalled it's ready to go again. This happens by calling
356 * blk_start_queue() to restart queue operations. Queue lock must be held.
357 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200358void blk_stop_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 blk_remove_plug(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200361 queue_flag_set(QUEUE_FLAG_STOPPED, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363EXPORT_SYMBOL(blk_stop_queue);
364
365/**
366 * blk_sync_queue - cancel any pending callbacks on a queue
367 * @q: the queue
368 *
369 * Description:
370 * The block layer may perform asynchronous callback activity
371 * on a queue, such as calling the unplug function after a timeout.
372 * A block device may call blk_sync_queue to ensure that any
373 * such activity is cancelled, thus allowing it to release resources
Michael Opdenacker59c51592007-05-09 08:57:56 +0200374 * that the callbacks might use. The caller must already have made sure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 * that its ->make_request_fn will not re-add plugging prior to calling
376 * this function.
377 *
378 */
379void blk_sync_queue(struct request_queue *q)
380{
381 del_timer_sync(&q->unplug_timer);
Jens Axboe70ed28b2008-11-19 14:38:39 +0100382 del_timer_sync(&q->timeout);
Cheng Renquan64d01dc2008-12-03 12:41:39 +0100383 cancel_work_sync(&q->unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385EXPORT_SYMBOL(blk_sync_queue);
386
387/**
Jens Axboe80a4b582008-10-14 09:51:06 +0200388 * __blk_run_queue - run a single device queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200390 *
391 * Description:
392 * See @blk_run_queue. This variant must be called with the queue lock
393 * held and interrupts disabled.
394 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 */
Nick Piggin75ad23b2008-04-29 14:48:33 +0200396void __blk_run_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 blk_remove_plug(q);
Jens Axboedac07ec2006-05-11 08:20:16 +0200399
Tejun Heoa538cd02009-04-23 11:05:17 +0900400 if (unlikely(blk_queue_stopped(q)))
401 return;
402
403 if (elv_queue_empty(q))
404 return;
405
Jens Axboedac07ec2006-05-11 08:20:16 +0200406 /*
407 * Only recurse once to avoid overrunning the stack, let the unplug
408 * handling reinvoke the handler shortly if we already got there.
409 */
Tejun Heoa538cd02009-04-23 11:05:17 +0900410 if (!queue_flag_test_and_set(QUEUE_FLAG_REENTER, q)) {
411 q->request_fn(q);
412 queue_flag_clear(QUEUE_FLAG_REENTER, q);
413 } else {
414 queue_flag_set(QUEUE_FLAG_PLUGGED, q);
415 kblockd_schedule_work(q, &q->unplug_work);
416 }
Nick Piggin75ad23b2008-04-29 14:48:33 +0200417}
418EXPORT_SYMBOL(__blk_run_queue);
Jens Axboedac07ec2006-05-11 08:20:16 +0200419
Nick Piggin75ad23b2008-04-29 14:48:33 +0200420/**
421 * blk_run_queue - run a single device queue
422 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200423 *
424 * Description:
425 * Invoke request handling on this queue, if it has pending work to do.
Tejun Heoa7f55792009-04-23 11:05:17 +0900426 * May be used to restart queueing when a request has completed.
Nick Piggin75ad23b2008-04-29 14:48:33 +0200427 */
428void blk_run_queue(struct request_queue *q)
429{
430 unsigned long flags;
431
432 spin_lock_irqsave(q->queue_lock, flags);
433 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 spin_unlock_irqrestore(q->queue_lock, flags);
435}
436EXPORT_SYMBOL(blk_run_queue);
437
Jens Axboe165125e2007-07-24 09:28:11 +0200438void blk_put_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500439{
440 kobject_put(&q->kobj);
441}
Al Viro483f4af2006-03-18 18:34:37 -0500442
Jens Axboe6728cb02008-01-31 13:03:55 +0100443void blk_cleanup_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500444{
Jens Axboee3335de2008-09-18 09:22:54 -0700445 /*
446 * We know we have process context here, so we can be a little
447 * cautious and ensure that pending block actions on this device
448 * are done before moving on. Going into this function, we should
449 * not have processes doing IO to this device.
450 */
451 blk_sync_queue(q);
452
Al Viro483f4af2006-03-18 18:34:37 -0500453 mutex_lock(&q->sysfs_lock);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200454 queue_flag_set_unlocked(QUEUE_FLAG_DEAD, q);
Al Viro483f4af2006-03-18 18:34:37 -0500455 mutex_unlock(&q->sysfs_lock);
456
457 if (q->elevator)
458 elevator_exit(q->elevator);
459
460 blk_put_queue(q);
461}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462EXPORT_SYMBOL(blk_cleanup_queue);
463
Jens Axboe165125e2007-07-24 09:28:11 +0200464static int blk_init_free_list(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 struct request_list *rl = &q->rq;
467
Jens Axboe1faa16d2009-04-06 14:48:01 +0200468 rl->count[BLK_RW_SYNC] = rl->count[BLK_RW_ASYNC] = 0;
469 rl->starved[BLK_RW_SYNC] = rl->starved[BLK_RW_ASYNC] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200470 rl->elvpriv = 0;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200471 init_waitqueue_head(&rl->wait[BLK_RW_SYNC]);
472 init_waitqueue_head(&rl->wait[BLK_RW_ASYNC]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Christoph Lameter19460892005-06-23 00:08:19 -0700474 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
475 mempool_free_slab, request_cachep, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 if (!rl->rq_pool)
478 return -ENOMEM;
479
480 return 0;
481}
482
Jens Axboe165125e2007-07-24 09:28:11 +0200483struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Christoph Lameter19460892005-06-23 00:08:19 -0700485 return blk_alloc_queue_node(gfp_mask, -1);
486}
487EXPORT_SYMBOL(blk_alloc_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Jens Axboe165125e2007-07-24 09:28:11 +0200489struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -0700490{
Jens Axboe165125e2007-07-24 09:28:11 +0200491 struct request_queue *q;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700492 int err;
Christoph Lameter19460892005-06-23 00:08:19 -0700493
Jens Axboe8324aa92008-01-29 14:51:59 +0100494 q = kmem_cache_alloc_node(blk_requestq_cachep,
Christoph Lameter94f60302007-07-17 04:03:29 -0700495 gfp_mask | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (!q)
497 return NULL;
498
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700499 q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
500 q->backing_dev_info.unplug_io_data = q;
Jens Axboe0989a022009-06-12 14:42:56 +0200501 q->backing_dev_info.ra_pages =
502 (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
503 q->backing_dev_info.state = 0;
504 q->backing_dev_info.capabilities = BDI_CAP_MAP_COPY;
Jens Axboed9938312009-06-12 14:45:52 +0200505 q->backing_dev_info.name = "block";
Jens Axboe0989a022009-06-12 14:42:56 +0200506
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700507 err = bdi_init(&q->backing_dev_info);
508 if (err) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100509 kmem_cache_free(blk_requestq_cachep, q);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700510 return NULL;
511 }
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 init_timer(&q->unplug_timer);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700514 setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
515 INIT_LIST_HEAD(&q->timeout_list);
Peter Zijlstra713ada92008-10-16 13:44:57 +0200516 INIT_WORK(&q->unplug_work, blk_unplug_work);
Al Viro483f4af2006-03-18 18:34:37 -0500517
Jens Axboe8324aa92008-01-29 14:51:59 +0100518 kobject_init(&q->kobj, &blk_queue_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Al Viro483f4af2006-03-18 18:34:37 -0500520 mutex_init(&q->sysfs_lock);
Neil Browne7e72bf2008-05-14 16:05:54 -0700521 spin_lock_init(&q->__queue_lock);
Al Viro483f4af2006-03-18 18:34:37 -0500522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return q;
524}
Christoph Lameter19460892005-06-23 00:08:19 -0700525EXPORT_SYMBOL(blk_alloc_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527/**
528 * blk_init_queue - prepare a request queue for use with a block device
529 * @rfn: The function to be called to process requests that have been
530 * placed on the queue.
531 * @lock: Request queue spin lock
532 *
533 * Description:
534 * If a block device wishes to use the standard request handling procedures,
535 * which sorts requests and coalesces adjacent requests, then it must
536 * call blk_init_queue(). The function @rfn will be called when there
537 * are requests on the queue that need to be processed. If the device
538 * supports plugging, then @rfn may not be called immediately when requests
539 * are available on the queue, but may be called at some time later instead.
540 * Plugged queues are generally unplugged when a buffer belonging to one
541 * of the requests on the queue is needed, or due to memory pressure.
542 *
543 * @rfn is not required, or even expected, to remove all requests off the
544 * queue, but only as many as it can handle at a time. If it does leave
545 * requests on the queue, it is responsible for arranging that the requests
546 * get dealt with eventually.
547 *
548 * The queue spin lock must be held while manipulating the requests on the
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200549 * request queue; this lock will be taken also from interrupt context, so irq
550 * disabling is needed for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 *
Randy Dunlap710027a2008-08-19 20:13:11 +0200552 * Function returns a pointer to the initialized request queue, or %NULL if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 * it didn't succeed.
554 *
555 * Note:
556 * blk_init_queue() must be paired with a blk_cleanup_queue() call
557 * when the block device is deactivated (such as at module unload).
558 **/
Christoph Lameter19460892005-06-23 00:08:19 -0700559
Jens Axboe165125e2007-07-24 09:28:11 +0200560struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Christoph Lameter19460892005-06-23 00:08:19 -0700562 return blk_init_queue_node(rfn, lock, -1);
563}
564EXPORT_SYMBOL(blk_init_queue);
565
Jens Axboe165125e2007-07-24 09:28:11 +0200566struct request_queue *
Christoph Lameter19460892005-06-23 00:08:19 -0700567blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
568{
Jens Axboe165125e2007-07-24 09:28:11 +0200569 struct request_queue *q = blk_alloc_queue_node(GFP_KERNEL, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 if (!q)
572 return NULL;
573
Christoph Lameter19460892005-06-23 00:08:19 -0700574 q->node = node_id;
Al Viro8669aaf2006-03-18 13:50:00 -0500575 if (blk_init_free_list(q)) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100576 kmem_cache_free(blk_requestq_cachep, q);
Al Viro8669aaf2006-03-18 13:50:00 -0500577 return NULL;
578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 q->request_fn = rfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 q->prep_rq_fn = NULL;
582 q->unplug_fn = generic_unplug_device;
Jens Axboebc58ba92009-01-23 10:54:44 +0100583 q->queue_flags = QUEUE_FLAG_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 q->queue_lock = lock;
585
Jens Axboef3b144a2009-03-06 08:48:33 +0100586 /*
587 * This also sets hw/phys segments, boundary and size
588 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 blk_queue_make_request(q, __make_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Alan Stern44ec9542007-02-20 11:01:57 -0500591 q->sg_reserved_size = INT_MAX;
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 /*
594 * all done
595 */
596 if (!elevator_init(q, NULL)) {
597 blk_queue_congestion_threshold(q);
598 return q;
599 }
600
Al Viro8669aaf2006-03-18 13:50:00 -0500601 blk_put_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return NULL;
603}
Christoph Lameter19460892005-06-23 00:08:19 -0700604EXPORT_SYMBOL(blk_init_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Jens Axboe165125e2007-07-24 09:28:11 +0200606int blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Nick Pigginfde6ad22005-06-23 00:08:53 -0700608 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
Al Viro483f4af2006-03-18 18:34:37 -0500609 kobject_get(&q->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return 0;
611 }
612
613 return 1;
614}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Jens Axboe165125e2007-07-24 09:28:11 +0200616static inline void blk_free_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Jens Axboe4aff5e22006-08-10 08:44:47 +0200618 if (rq->cmd_flags & REQ_ELVPRIV)
Tejun Heocb98fc82005-10-28 08:29:39 +0200619 elv_put_request(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 mempool_free(rq, q->rq.rq_pool);
621}
622
Jens Axboe1ea25ec2006-07-18 22:24:11 +0200623static struct request *
Jerome Marchand42dad762009-04-22 14:01:49 +0200624blk_alloc_request(struct request_queue *q, int flags, int priv, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
626 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
627
628 if (!rq)
629 return NULL;
630
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200631 blk_rq_init(q, rq);
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200632
Jerome Marchand42dad762009-04-22 14:01:49 +0200633 rq->cmd_flags = flags | REQ_ALLOCED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Tejun Heocb98fc82005-10-28 08:29:39 +0200635 if (priv) {
Jens Axboecb78b282006-07-28 09:32:57 +0200636 if (unlikely(elv_set_request(q, rq, gfp_mask))) {
Tejun Heocb98fc82005-10-28 08:29:39 +0200637 mempool_free(rq, q->rq.rq_pool);
638 return NULL;
639 }
Jens Axboe4aff5e22006-08-10 08:44:47 +0200640 rq->cmd_flags |= REQ_ELVPRIV;
Tejun Heocb98fc82005-10-28 08:29:39 +0200641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Tejun Heocb98fc82005-10-28 08:29:39 +0200643 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
646/*
647 * ioc_batching returns true if the ioc is a valid batching request and
648 * should be given priority access to a request.
649 */
Jens Axboe165125e2007-07-24 09:28:11 +0200650static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
652 if (!ioc)
653 return 0;
654
655 /*
656 * Make sure the process is able to allocate at least 1 request
657 * even if the batch times out, otherwise we could theoretically
658 * lose wakeups.
659 */
660 return ioc->nr_batch_requests == q->nr_batching ||
661 (ioc->nr_batch_requests > 0
662 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
663}
664
665/*
666 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
667 * will cause the process to be a "batcher" on all queues in the system. This
668 * is the behaviour we want though - once it gets a wakeup it should be given
669 * a nice run.
670 */
Jens Axboe165125e2007-07-24 09:28:11 +0200671static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673 if (!ioc || ioc_batching(q, ioc))
674 return;
675
676 ioc->nr_batch_requests = q->nr_batching;
677 ioc->last_waited = jiffies;
678}
679
Jens Axboe1faa16d2009-04-06 14:48:01 +0200680static void __freed_request(struct request_queue *q, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
682 struct request_list *rl = &q->rq;
683
Jens Axboe1faa16d2009-04-06 14:48:01 +0200684 if (rl->count[sync] < queue_congestion_off_threshold(q))
685 blk_clear_queue_congested(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Jens Axboe1faa16d2009-04-06 14:48:01 +0200687 if (rl->count[sync] + 1 <= q->nr_requests) {
688 if (waitqueue_active(&rl->wait[sync]))
689 wake_up(&rl->wait[sync]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Jens Axboe1faa16d2009-04-06 14:48:01 +0200691 blk_clear_queue_full(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
693}
694
695/*
696 * A request has just been released. Account for it, update the full and
697 * congestion status, wake up any waiters. Called under q->queue_lock.
698 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200699static void freed_request(struct request_queue *q, int sync, int priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
701 struct request_list *rl = &q->rq;
702
Jens Axboe1faa16d2009-04-06 14:48:01 +0200703 rl->count[sync]--;
Tejun Heocb98fc82005-10-28 08:29:39 +0200704 if (priv)
705 rl->elvpriv--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Jens Axboe1faa16d2009-04-06 14:48:01 +0200707 __freed_request(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Jens Axboe1faa16d2009-04-06 14:48:01 +0200709 if (unlikely(rl->starved[sync ^ 1]))
710 __freed_request(q, sync ^ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713/*
Nick Piggind6344532005-06-28 20:45:14 -0700714 * Get a free request, queue_lock must be held.
715 * Returns NULL on failure, with queue_lock held.
716 * Returns !NULL on success, with queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 */
Jens Axboe165125e2007-07-24 09:28:11 +0200718static struct request *get_request(struct request_queue *q, int rw_flags,
Jens Axboe7749a8d2006-12-13 13:02:26 +0100719 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
721 struct request *rq = NULL;
722 struct request_list *rl = &q->rq;
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100723 struct io_context *ioc = NULL;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200724 const bool is_sync = rw_is_sync(rw_flags) != 0;
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100725 int may_queue, priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Jens Axboe7749a8d2006-12-13 13:02:26 +0100727 may_queue = elv_may_queue(q, rw_flags);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100728 if (may_queue == ELV_MQUEUE_NO)
729 goto rq_starved;
730
Jens Axboe1faa16d2009-04-06 14:48:01 +0200731 if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) {
732 if (rl->count[is_sync]+1 >= q->nr_requests) {
Jens Axboeb5deef92006-07-19 23:39:40 +0200733 ioc = current_io_context(GFP_ATOMIC, q->node);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100734 /*
735 * The queue will fill after this allocation, so set
736 * it as full, and mark this process as "batching".
737 * This process will be allowed to complete a batch of
738 * requests, others will be blocked.
739 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200740 if (!blk_queue_full(q, is_sync)) {
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100741 ioc_set_batching(q, ioc);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200742 blk_set_queue_full(q, is_sync);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100743 } else {
744 if (may_queue != ELV_MQUEUE_MUST
745 && !ioc_batching(q, ioc)) {
746 /*
747 * The queue is full and the allocating
748 * process is not a "batcher", and not
749 * exempted by the IO scheduler
750 */
751 goto out;
752 }
753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
Jens Axboe1faa16d2009-04-06 14:48:01 +0200755 blk_set_queue_congested(q, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
757
Jens Axboe082cf692005-06-28 16:35:11 +0200758 /*
759 * Only allow batching queuers to allocate up to 50% over the defined
760 * limit of requests, otherwise we could have thousands of requests
761 * allocated with any setting of ->nr_requests
762 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200763 if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
Jens Axboe082cf692005-06-28 16:35:11 +0200764 goto out;
Hugh Dickinsfd782a42005-06-29 15:15:40 +0100765
Jens Axboe1faa16d2009-04-06 14:48:01 +0200766 rl->count[is_sync]++;
767 rl->starved[is_sync] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200768
Jens Axboe64521d12005-10-28 08:30:39 +0200769 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Tejun Heocb98fc82005-10-28 08:29:39 +0200770 if (priv)
771 rl->elvpriv++;
772
Jerome Marchand42dad762009-04-22 14:01:49 +0200773 if (blk_queue_io_stat(q))
774 rw_flags |= REQ_IO_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 spin_unlock_irq(q->queue_lock);
776
Jens Axboe7749a8d2006-12-13 13:02:26 +0100777 rq = blk_alloc_request(q, rw_flags, priv, gfp_mask);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100778 if (unlikely(!rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 /*
780 * Allocation failed presumably due to memory. Undo anything
781 * we might have messed up.
782 *
783 * Allocating task should really be put onto the front of the
784 * wait queue, but this is pretty rare.
785 */
786 spin_lock_irq(q->queue_lock);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200787 freed_request(q, is_sync, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 /*
790 * in the very unlikely event that allocation failed and no
791 * requests for this direction was pending, mark us starved
792 * so that freeing of a request in the other direction will
793 * notice us. another possible fix would be to split the
794 * rq mempool into READ and WRITE
795 */
796rq_starved:
Jens Axboe1faa16d2009-04-06 14:48:01 +0200797 if (unlikely(rl->count[is_sync] == 0))
798 rl->starved[is_sync] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 goto out;
801 }
802
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100803 /*
804 * ioc may be NULL here, and ioc_batching will be false. That's
805 * OK, if the queue is under the request limit then requests need
806 * not count toward the nr_batch_requests limit. There will always
807 * be some limit enforced by BLK_BATCH_TIME.
808 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (ioc_batching(q, ioc))
810 ioc->nr_batch_requests--;
Jens Axboe6728cb02008-01-31 13:03:55 +0100811
Jens Axboe1faa16d2009-04-06 14:48:01 +0200812 trace_block_getrq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return rq;
815}
816
817/*
818 * No available requests for this queue, unplug the device and wait for some
819 * requests to become available.
Nick Piggind6344532005-06-28 20:45:14 -0700820 *
821 * Called with q->queue_lock held, and returns with it unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 */
Jens Axboe165125e2007-07-24 09:28:11 +0200823static struct request *get_request_wait(struct request_queue *q, int rw_flags,
Jens Axboe22e2c502005-06-27 10:55:12 +0200824 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Jens Axboe1faa16d2009-04-06 14:48:01 +0200826 const bool is_sync = rw_is_sync(rw_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 struct request *rq;
828
Jens Axboe7749a8d2006-12-13 13:02:26 +0100829 rq = get_request(q, rw_flags, bio, GFP_NOIO);
Nick Piggin450991b2005-06-28 20:45:13 -0700830 while (!rq) {
831 DEFINE_WAIT(wait);
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200832 struct io_context *ioc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 struct request_list *rl = &q->rq;
834
Jens Axboe1faa16d2009-04-06 14:48:01 +0200835 prepare_to_wait_exclusive(&rl->wait[is_sync], &wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 TASK_UNINTERRUPTIBLE);
837
Jens Axboe1faa16d2009-04-06 14:48:01 +0200838 trace_block_sleeprq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200840 __generic_unplug_device(q);
841 spin_unlock_irq(q->queue_lock);
842 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200844 /*
845 * After sleeping, we become a "batching" process and
846 * will be able to allocate at least one request, and
847 * up to a big batch of them for a small period time.
848 * See ioc_batching, ioc_set_batching
849 */
850 ioc = current_io_context(GFP_NOIO, q->node);
851 ioc_set_batching(q, ioc);
Jens Axboe2056a782006-03-23 20:00:26 +0100852
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200853 spin_lock_irq(q->queue_lock);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200854 finish_wait(&rl->wait[is_sync], &wait);
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200855
856 rq = get_request(q, rw_flags, bio, GFP_NOIO);
857 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 return rq;
860}
861
Jens Axboe165125e2007-07-24 09:28:11 +0200862struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
864 struct request *rq;
865
866 BUG_ON(rw != READ && rw != WRITE);
867
Nick Piggind6344532005-06-28 20:45:14 -0700868 spin_lock_irq(q->queue_lock);
869 if (gfp_mask & __GFP_WAIT) {
Jens Axboe22e2c502005-06-27 10:55:12 +0200870 rq = get_request_wait(q, rw, NULL);
Nick Piggind6344532005-06-28 20:45:14 -0700871 } else {
Jens Axboe22e2c502005-06-27 10:55:12 +0200872 rq = get_request(q, rw, NULL, gfp_mask);
Nick Piggind6344532005-06-28 20:45:14 -0700873 if (!rq)
874 spin_unlock_irq(q->queue_lock);
875 }
876 /* q->queue_lock is unlocked at this point */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 return rq;
879}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880EXPORT_SYMBOL(blk_get_request);
881
882/**
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300883 * blk_make_request - given a bio, allocate a corresponding struct request.
Randy Dunlap8ebf9752009-06-11 20:00:41 -0700884 * @q: target request queue
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300885 * @bio: The bio describing the memory mappings that will be submitted for IO.
886 * It may be a chained-bio properly constructed by block/bio layer.
Randy Dunlap8ebf9752009-06-11 20:00:41 -0700887 * @gfp_mask: gfp flags to be used for memory allocation
Jens Axboedc72ef42006-07-20 14:54:05 +0200888 *
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300889 * blk_make_request is the parallel of generic_make_request for BLOCK_PC
890 * type commands. Where the struct request needs to be farther initialized by
891 * the caller. It is passed a &struct bio, which describes the memory info of
892 * the I/O transfer.
893 *
894 * The caller of blk_make_request must make sure that bi_io_vec
895 * are set to describe the memory buffers. That bio_data_dir() will return
896 * the needed direction of the request. (And all bio's in the passed bio-chain
897 * are properly set accordingly)
898 *
899 * If called under none-sleepable conditions, mapped bio buffers must not
900 * need bouncing, by calling the appropriate masked or flagged allocator,
901 * suitable for the target device. Otherwise the call to blk_queue_bounce will
902 * BUG.
Jens Axboe53674ac2009-05-19 19:52:35 +0200903 *
904 * WARNING: When allocating/cloning a bio-chain, careful consideration should be
905 * given to how you allocate bios. In particular, you cannot use __GFP_WAIT for
906 * anything but the first bio in the chain. Otherwise you risk waiting for IO
907 * completion of a bio that hasn't been submitted yet, thus resulting in a
908 * deadlock. Alternatively bios should be allocated using bio_kmalloc() instead
909 * of bio_alloc(), as that avoids the mempool deadlock.
910 * If possible a big IO should be split into smaller parts when allocation
911 * fails. Partial allocation should not be an error, or you risk a live-lock.
Jens Axboedc72ef42006-07-20 14:54:05 +0200912 */
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300913struct request *blk_make_request(struct request_queue *q, struct bio *bio,
914 gfp_t gfp_mask)
Jens Axboedc72ef42006-07-20 14:54:05 +0200915{
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300916 struct request *rq = blk_get_request(q, bio_data_dir(bio), gfp_mask);
917
918 if (unlikely(!rq))
919 return ERR_PTR(-ENOMEM);
920
921 for_each_bio(bio) {
922 struct bio *bounce_bio = bio;
923 int ret;
924
925 blk_queue_bounce(q, &bounce_bio);
926 ret = blk_rq_append_bio(q, rq, bounce_bio);
927 if (unlikely(ret)) {
928 blk_put_request(rq);
929 return ERR_PTR(ret);
930 }
931 }
932
933 return rq;
Jens Axboedc72ef42006-07-20 14:54:05 +0200934}
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300935EXPORT_SYMBOL(blk_make_request);
Jens Axboedc72ef42006-07-20 14:54:05 +0200936
937/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 * blk_requeue_request - put a request back on queue
939 * @q: request queue where request should be inserted
940 * @rq: request to be inserted
941 *
942 * Description:
943 * Drivers often keep queueing requests until the hardware cannot accept
944 * more, when that condition happens we need to put the request back
945 * on the queue. Must be called with queue lock held.
946 */
Jens Axboe165125e2007-07-24 09:28:11 +0200947void blk_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Jens Axboe242f9dc2008-09-14 05:55:09 -0700949 blk_delete_timer(rq);
950 blk_clear_rq_complete(rq);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100951 trace_block_rq_requeue(q, rq);
Jens Axboe2056a782006-03-23 20:00:26 +0100952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 if (blk_rq_tagged(rq))
954 blk_queue_end_tag(q, rq);
955
James Bottomleyba396a62009-05-27 14:17:08 +0200956 BUG_ON(blk_queued_rq(rq));
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 elv_requeue_request(q, rq);
959}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960EXPORT_SYMBOL(blk_requeue_request);
961
962/**
Randy Dunlap710027a2008-08-19 20:13:11 +0200963 * blk_insert_request - insert a special request into a request queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 * @q: request queue where request should be inserted
965 * @rq: request to be inserted
966 * @at_head: insert request at head or tail of queue
967 * @data: private data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 *
969 * Description:
970 * Many block devices need to execute commands asynchronously, so they don't
971 * block the whole kernel from preemption during request execution. This is
972 * accomplished normally by inserting aritficial requests tagged as
Randy Dunlap710027a2008-08-19 20:13:11 +0200973 * REQ_TYPE_SPECIAL in to the corresponding request queue, and letting them
974 * be scheduled for actual execution by the request queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 *
976 * We have the option of inserting the head or the tail of the queue.
977 * Typically we use the tail for new ioctls and so forth. We use the head
978 * of the queue for things like a QUEUE_FULL message from a device, or a
979 * host that is unable to accept a particular command.
980 */
Jens Axboe165125e2007-07-24 09:28:11 +0200981void blk_insert_request(struct request_queue *q, struct request *rq,
Tejun Heo 867d1192005-04-24 02:06:05 -0500982 int at_head, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Tejun Heo 867d1192005-04-24 02:06:05 -0500984 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 unsigned long flags;
986
987 /*
988 * tell I/O scheduler that this isn't a regular read/write (ie it
989 * must not attempt merges on this) and that it acts as a soft
990 * barrier
991 */
Jens Axboe4aff5e22006-08-10 08:44:47 +0200992 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 rq->special = data;
995
996 spin_lock_irqsave(q->queue_lock, flags);
997
998 /*
999 * If command is tagged, release the tag
1000 */
Tejun Heo 867d1192005-04-24 02:06:05 -05001001 if (blk_rq_tagged(rq))
1002 blk_queue_end_tag(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Jerome Marchandb238b3d2007-10-23 15:05:46 +02001004 drive_stat_acct(rq, 1);
Tejun Heo 867d1192005-04-24 02:06:05 -05001005 __elv_add_request(q, rq, where, 0);
Tejun Heoa7f55792009-04-23 11:05:17 +09001006 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 spin_unlock_irqrestore(q->queue_lock, flags);
1008}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009EXPORT_SYMBOL(blk_insert_request);
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011/*
1012 * add-request adds a request to the linked list.
1013 * queue lock is held and interrupts disabled, as we muck with the
1014 * request queue list.
1015 */
Jens Axboe6728cb02008-01-31 13:03:55 +01001016static inline void add_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
Jerome Marchandb238b3d2007-10-23 15:05:46 +02001018 drive_stat_acct(req, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 /*
1021 * elevator indicated where it wants this request to be
1022 * inserted at elevator_merge time
1023 */
1024 __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
1025}
Jens Axboe6728cb02008-01-31 13:03:55 +01001026
Tejun Heo074a7ac2008-08-25 19:56:14 +09001027static void part_round_stats_single(int cpu, struct hd_struct *part,
1028 unsigned long now)
1029{
1030 if (now == part->stamp)
1031 return;
1032
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001033 if (part_in_flight(part)) {
Tejun Heo074a7ac2008-08-25 19:56:14 +09001034 __part_stat_add(cpu, part, time_in_queue,
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001035 part_in_flight(part) * (now - part->stamp));
Tejun Heo074a7ac2008-08-25 19:56:14 +09001036 __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
1037 }
1038 part->stamp = now;
1039}
1040
1041/**
Randy Dunlap496aa8a2008-10-16 07:46:23 +02001042 * part_round_stats() - Round off the performance stats on a struct disk_stats.
1043 * @cpu: cpu number for stats access
1044 * @part: target partition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 *
1046 * The average IO queue length and utilisation statistics are maintained
1047 * by observing the current state of the queue length and the amount of
1048 * time it has been in this state for.
1049 *
1050 * Normally, that accounting is done on IO completion, but that can result
1051 * in more than a second's worth of IO being accounted for within any one
1052 * second, leading to >100% utilisation. To deal with that, we call this
1053 * function to do a round-off before returning the results when reading
1054 * /proc/diskstats. This accounts immediately for all queue usage up to
1055 * the current jiffies and restarts the counters again.
1056 */
Tejun Heoc9959052008-08-25 19:47:21 +09001057void part_round_stats(int cpu, struct hd_struct *part)
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001058{
1059 unsigned long now = jiffies;
1060
Tejun Heo074a7ac2008-08-25 19:56:14 +09001061 if (part->partno)
1062 part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
1063 part_round_stats_single(cpu, part, now);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001064}
Tejun Heo074a7ac2008-08-25 19:56:14 +09001065EXPORT_SYMBOL_GPL(part_round_stats);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067/*
1068 * queue lock must be held
1069 */
Jens Axboe165125e2007-07-24 09:28:11 +02001070void __blk_put_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (unlikely(!q))
1073 return;
1074 if (unlikely(--req->ref_count))
1075 return;
1076
Tejun Heo8922e162005-10-20 16:23:44 +02001077 elv_completed_request(q, req);
1078
Boaz Harrosh1cd96c22009-03-24 12:35:07 +01001079 /* this is a bio leak */
1080 WARN_ON(req->bio != NULL);
1081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 /*
1083 * Request may not have originated from ll_rw_blk. if not,
1084 * it didn't come out of our reserved rq pools
1085 */
Jens Axboe49171e52006-08-10 08:59:11 +02001086 if (req->cmd_flags & REQ_ALLOCED) {
Jens Axboe1faa16d2009-04-06 14:48:01 +02001087 int is_sync = rq_is_sync(req) != 0;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001088 int priv = req->cmd_flags & REQ_ELVPRIV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 BUG_ON(!list_empty(&req->queuelist));
Jens Axboe98170642006-07-28 09:23:08 +02001091 BUG_ON(!hlist_unhashed(&req->hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 blk_free_request(q, req);
Jens Axboe1faa16d2009-04-06 14:48:01 +02001094 freed_request(q, is_sync, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 }
1096}
Mike Christie6e39b692005-11-11 05:30:24 -06001097EXPORT_SYMBOL_GPL(__blk_put_request);
1098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099void blk_put_request(struct request *req)
1100{
Tejun Heo8922e162005-10-20 16:23:44 +02001101 unsigned long flags;
Jens Axboe165125e2007-07-24 09:28:11 +02001102 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
FUJITA Tomonori52a93ba2008-07-15 21:21:45 +02001104 spin_lock_irqsave(q->queue_lock, flags);
1105 __blk_put_request(q, req);
1106 spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108EXPORT_SYMBOL(blk_put_request);
1109
Jens Axboe86db1e22008-01-29 14:53:40 +01001110void init_request_from_bio(struct request *req, struct bio *bio)
Tejun Heo52d9e672006-01-06 09:49:58 +01001111{
Jens Axboec7c22e42008-09-13 20:26:01 +02001112 req->cpu = bio->bi_comp_cpu;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001113 req->cmd_type = REQ_TYPE_FS;
Tejun Heo52d9e672006-01-06 09:49:58 +01001114
1115 /*
Tejun Heoa82afdf2009-07-03 17:48:16 +09001116 * Inherit FAILFAST from bio (for read-ahead, and explicit
1117 * FAILFAST). FAILFAST flags are identical for req and bio.
Tejun Heo52d9e672006-01-06 09:49:58 +01001118 */
Jens Axboe1f98a132009-09-11 14:32:04 +02001119 if (bio_rw_flagged(bio, BIO_RW_AHEAD))
Tejun Heoa82afdf2009-07-03 17:48:16 +09001120 req->cmd_flags |= REQ_FAILFAST_MASK;
1121 else
1122 req->cmd_flags |= bio->bi_rw & REQ_FAILFAST_MASK;
Tejun Heo52d9e672006-01-06 09:49:58 +01001123
Jens Axboe1f98a132009-09-11 14:32:04 +02001124 if (unlikely(bio_rw_flagged(bio, BIO_RW_DISCARD))) {
David Woodhousee17fc0a2008-08-09 16:42:20 +01001125 req->cmd_flags |= REQ_DISCARD;
Jens Axboe1f98a132009-09-11 14:32:04 +02001126 if (bio_rw_flagged(bio, BIO_RW_BARRIER))
David Woodhousee17fc0a2008-08-09 16:42:20 +01001127 req->cmd_flags |= REQ_SOFTBARRIER;
Jens Axboe1f98a132009-09-11 14:32:04 +02001128 } else if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER)))
Tejun Heoe4025f62009-04-23 11:05:17 +09001129 req->cmd_flags |= REQ_HARDBARRIER;
Tejun Heo52d9e672006-01-06 09:49:58 +01001130
Jens Axboe1f98a132009-09-11 14:32:04 +02001131 if (bio_rw_flagged(bio, BIO_RW_SYNCIO))
Jens Axboe4aff5e22006-08-10 08:44:47 +02001132 req->cmd_flags |= REQ_RW_SYNC;
Jens Axboe1f98a132009-09-11 14:32:04 +02001133 if (bio_rw_flagged(bio, BIO_RW_META))
Jens Axboe5404bc72006-08-10 09:01:02 +02001134 req->cmd_flags |= REQ_RW_META;
Jens Axboe1f98a132009-09-11 14:32:04 +02001135 if (bio_rw_flagged(bio, BIO_RW_NOIDLE))
Jens Axboeaeb6faf2009-04-06 14:48:07 +02001136 req->cmd_flags |= REQ_NOIDLE;
Jens Axboeb31dc662006-06-13 08:26:10 +02001137
Tejun Heo52d9e672006-01-06 09:49:58 +01001138 req->errors = 0;
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001139 req->__sector = bio->bi_sector;
Tejun Heo52d9e672006-01-06 09:49:58 +01001140 req->ioprio = bio_prio(bio);
NeilBrownbc1c56f2007-08-16 13:31:30 +02001141 blk_rq_bio_prep(req->q, req, bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001142}
1143
Jens Axboe644b2d92009-04-06 14:48:06 +02001144/*
1145 * Only disabling plugging for non-rotational devices if it does tagging
1146 * as well, otherwise we do need the proper merging
1147 */
1148static inline bool queue_should_plug(struct request_queue *q)
1149{
Jens Axboefb1e7532009-07-30 08:18:24 +02001150 return !(blk_queue_nonrot(q) && blk_queue_queuing(q));
Jens Axboe644b2d92009-04-06 14:48:06 +02001151}
1152
Jens Axboe165125e2007-07-24 09:28:11 +02001153static int __make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
Nick Piggin450991b2005-06-28 20:45:13 -07001155 struct request *req;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001156 int el_ret;
1157 unsigned int bytes = bio->bi_size;
Jens Axboe51da90f2006-07-18 04:14:45 +02001158 const unsigned short prio = bio_prio(bio);
Jens Axboe1f98a132009-09-11 14:32:04 +02001159 const bool sync = bio_rw_flagged(bio, BIO_RW_SYNCIO);
1160 const bool unplug = bio_rw_flagged(bio, BIO_RW_UNPLUG);
Tejun Heo80a761f2009-07-03 17:48:17 +09001161 const unsigned int ff = bio->bi_rw & REQ_FAILFAST_MASK;
Jens Axboe7749a8d2006-12-13 13:02:26 +01001162 int rw_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Mark McLoughlin6cafb122009-10-24 14:14:31 +02001164 if (bio_rw_flagged(bio, BIO_RW_BARRIER) &&
NeilBrowndb64f682009-06-30 09:35:44 +02001165 (q->next_ordered == QUEUE_ORDERED_NONE)) {
1166 bio_endio(bio, -EOPNOTSUPP);
1167 return 0;
1168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 /*
1170 * low level driver can indicate that it wants pages above a
1171 * certain limit bounced to low memory (ie for highmem, or even
1172 * ISA dma in theory)
1173 */
1174 blk_queue_bounce(q, &bio);
1175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 spin_lock_irq(q->queue_lock);
1177
Jens Axboe1f98a132009-09-11 14:32:04 +02001178 if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER)) || elv_queue_empty(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 goto get_rq;
1180
1181 el_ret = elv_merge(q, &req, bio);
1182 switch (el_ret) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001183 case ELEVATOR_BACK_MERGE:
1184 BUG_ON(!rq_mergeable(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Jens Axboe6728cb02008-01-31 13:03:55 +01001186 if (!ll_back_merge_fn(q, req, bio))
1187 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001189 trace_block_bio_backmerge(q, bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001190
Tejun Heo80a761f2009-07-03 17:48:17 +09001191 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1192 blk_rq_set_mixed_merge(req);
1193
Jens Axboe6728cb02008-01-31 13:03:55 +01001194 req->biotail->bi_next = bio;
1195 req->biotail = bio;
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001196 req->__data_len += bytes;
Jens Axboe6728cb02008-01-31 13:03:55 +01001197 req->ioprio = ioprio_best(req->ioprio, prio);
Jens Axboeab780f12008-08-26 10:25:02 +02001198 if (!blk_rq_cpu_valid(req))
1199 req->cpu = bio->bi_comp_cpu;
Jens Axboe6728cb02008-01-31 13:03:55 +01001200 drive_stat_acct(req, 0);
1201 if (!attempt_back_merge(q, req))
1202 elv_merged_request(q, req, el_ret);
1203 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Jens Axboe6728cb02008-01-31 13:03:55 +01001205 case ELEVATOR_FRONT_MERGE:
1206 BUG_ON(!rq_mergeable(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Jens Axboe6728cb02008-01-31 13:03:55 +01001208 if (!ll_front_merge_fn(q, req, bio))
1209 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001211 trace_block_bio_frontmerge(q, bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001212
Tejun Heo80a761f2009-07-03 17:48:17 +09001213 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff) {
1214 blk_rq_set_mixed_merge(req);
1215 req->cmd_flags &= ~REQ_FAILFAST_MASK;
1216 req->cmd_flags |= ff;
1217 }
1218
Jens Axboe6728cb02008-01-31 13:03:55 +01001219 bio->bi_next = req->bio;
1220 req->bio = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Jens Axboe6728cb02008-01-31 13:03:55 +01001222 /*
1223 * may not be valid. if the low level driver said
1224 * it didn't need a bounce buffer then it better
1225 * not touch req->buffer either...
1226 */
1227 req->buffer = bio_data(bio);
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001228 req->__sector = bio->bi_sector;
1229 req->__data_len += bytes;
Jens Axboe6728cb02008-01-31 13:03:55 +01001230 req->ioprio = ioprio_best(req->ioprio, prio);
Jens Axboeab780f12008-08-26 10:25:02 +02001231 if (!blk_rq_cpu_valid(req))
1232 req->cpu = bio->bi_comp_cpu;
Jens Axboe6728cb02008-01-31 13:03:55 +01001233 drive_stat_acct(req, 0);
1234 if (!attempt_front_merge(q, req))
1235 elv_merged_request(q, req, el_ret);
1236 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Jens Axboe6728cb02008-01-31 13:03:55 +01001238 /* ELV_NO_MERGE: elevator says don't/can't merge. */
1239 default:
1240 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 }
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243get_rq:
Nick Piggin450991b2005-06-28 20:45:13 -07001244 /*
Jens Axboe7749a8d2006-12-13 13:02:26 +01001245 * This sync check and mask will be re-done in init_request_from_bio(),
1246 * but we need to set it earlier to expose the sync flag to the
1247 * rq allocator and io schedulers.
1248 */
1249 rw_flags = bio_data_dir(bio);
1250 if (sync)
1251 rw_flags |= REQ_RW_SYNC;
1252
1253 /*
Nick Piggin450991b2005-06-28 20:45:13 -07001254 * Grab a free request. This is might sleep but can not fail.
Nick Piggind6344532005-06-28 20:45:14 -07001255 * Returns with the queue unlocked.
Nick Piggin450991b2005-06-28 20:45:13 -07001256 */
Jens Axboe7749a8d2006-12-13 13:02:26 +01001257 req = get_request_wait(q, rw_flags, bio);
Nick Piggind6344532005-06-28 20:45:14 -07001258
Nick Piggin450991b2005-06-28 20:45:13 -07001259 /*
1260 * After dropping the lock and possibly sleeping here, our request
1261 * may now be mergeable after it had proven unmergeable (above).
1262 * We don't worry about that case for efficiency. It won't happen
1263 * often, and the elevators are able to handle it.
1264 */
Tejun Heo52d9e672006-01-06 09:49:58 +01001265 init_request_from_bio(req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Nick Piggin450991b2005-06-28 20:45:13 -07001267 spin_lock_irq(q->queue_lock);
Jens Axboec7c22e42008-09-13 20:26:01 +02001268 if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags) ||
1269 bio_flagged(bio, BIO_CPU_AFFINE))
1270 req->cpu = blk_cpu_to_group(smp_processor_id());
Jens Axboe644b2d92009-04-06 14:48:06 +02001271 if (queue_should_plug(q) && elv_queue_empty(q))
Nick Piggin450991b2005-06-28 20:45:13 -07001272 blk_plug_device(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 add_request(q, req);
1274out:
Jens Axboe644b2d92009-04-06 14:48:06 +02001275 if (unplug || !queue_should_plug(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 __generic_unplug_device(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 spin_unlock_irq(q->queue_lock);
1278 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279}
1280
1281/*
1282 * If bio->bi_dev is a partition, remap the location
1283 */
1284static inline void blk_partition_remap(struct bio *bio)
1285{
1286 struct block_device *bdev = bio->bi_bdev;
1287
Jens Axboebf2de6f2007-09-27 13:01:25 +02001288 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 struct hd_struct *p = bdev->bd_part;
Jens Axboea3623572005-11-01 09:26:16 +01001290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 bio->bi_sector += p->start_sect;
1292 bio->bi_bdev = bdev->bd_contains;
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001293
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001294 trace_block_remap(bdev_get_queue(bio->bi_bdev), bio,
Alan D. Brunelle22a7c312009-05-04 16:35:08 -04001295 bdev->bd_dev,
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001296 bio->bi_sector - p->start_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
1298}
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300static void handle_bad_sector(struct bio *bio)
1301{
1302 char b[BDEVNAME_SIZE];
1303
1304 printk(KERN_INFO "attempt to access beyond end of device\n");
1305 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1306 bdevname(bio->bi_bdev, b),
1307 bio->bi_rw,
1308 (unsigned long long)bio->bi_sector + bio_sectors(bio),
1309 (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
1310
1311 set_bit(BIO_EOF, &bio->bi_flags);
1312}
1313
Akinobu Mitac17bb492006-12-08 02:39:46 -08001314#ifdef CONFIG_FAIL_MAKE_REQUEST
1315
1316static DECLARE_FAULT_ATTR(fail_make_request);
1317
1318static int __init setup_fail_make_request(char *str)
1319{
1320 return setup_fault_attr(&fail_make_request, str);
1321}
1322__setup("fail_make_request=", setup_fail_make_request);
1323
1324static int should_fail_request(struct bio *bio)
1325{
Tejun Heoeddb2e22008-08-25 19:56:13 +09001326 struct hd_struct *part = bio->bi_bdev->bd_part;
1327
1328 if (part_to_disk(part)->part0.make_it_fail || part->make_it_fail)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001329 return should_fail(&fail_make_request, bio->bi_size);
1330
1331 return 0;
1332}
1333
1334static int __init fail_make_request_debugfs(void)
1335{
1336 return init_fault_attr_dentries(&fail_make_request,
1337 "fail_make_request");
1338}
1339
1340late_initcall(fail_make_request_debugfs);
1341
1342#else /* CONFIG_FAIL_MAKE_REQUEST */
1343
1344static inline int should_fail_request(struct bio *bio)
1345{
1346 return 0;
1347}
1348
1349#endif /* CONFIG_FAIL_MAKE_REQUEST */
1350
Jens Axboec07e2b42007-07-18 13:27:58 +02001351/*
1352 * Check whether this bio extends beyond the end of the device.
1353 */
1354static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1355{
1356 sector_t maxsector;
1357
1358 if (!nr_sectors)
1359 return 0;
1360
1361 /* Test device or partition size, when known. */
1362 maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
1363 if (maxsector) {
1364 sector_t sector = bio->bi_sector;
1365
1366 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1367 /*
1368 * This may well happen - the kernel calls bread()
1369 * without checking the size of the device, e.g., when
1370 * mounting a device.
1371 */
1372 handle_bad_sector(bio);
1373 return 1;
1374 }
1375 }
1376
1377 return 0;
1378}
1379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001381 * generic_make_request - hand a buffer to its device driver for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 * @bio: The bio describing the location in memory and on the device.
1383 *
1384 * generic_make_request() is used to make I/O requests of block
1385 * devices. It is passed a &struct bio, which describes the I/O that needs
1386 * to be done.
1387 *
1388 * generic_make_request() does not return any status. The
1389 * success/failure status of the request, along with notification of
1390 * completion, is delivered asynchronously through the bio->bi_end_io
1391 * function described (one day) else where.
1392 *
1393 * The caller of generic_make_request must make sure that bi_io_vec
1394 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1395 * set to describe the device address, and the
1396 * bi_end_io and optionally bi_private are set to describe how
1397 * completion notification should be signaled.
1398 *
1399 * generic_make_request and the drivers it calls may use bi_next if this
1400 * bio happens to be merged with someone else, and may change bi_dev and
1401 * bi_sector for remaps as it sees fit. So the values of these fields
1402 * should NOT be depended on after the call to generic_make_request.
1403 */
Neil Brownd89d8792007-05-01 09:53:42 +02001404static inline void __generic_make_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
Jens Axboe165125e2007-07-24 09:28:11 +02001406 struct request_queue *q;
NeilBrown5ddfe962006-10-30 22:07:21 -08001407 sector_t old_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 int ret, nr_sectors = bio_sectors(bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001409 dev_t old_dev;
Jens Axboe51fd77b2007-11-02 08:49:08 +01001410 int err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Jens Axboec07e2b42007-07-18 13:27:58 +02001414 if (bio_check_eod(bio, nr_sectors))
1415 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 /*
1418 * Resolve the mapping until finished. (drivers are
1419 * still free to implement/resolve their own stacking
1420 * by explicitly returning 0)
1421 *
1422 * NOTE: we don't repeat the blk_size check for each new device.
1423 * Stacking drivers are expected to know what they are doing.
1424 */
NeilBrown5ddfe962006-10-30 22:07:21 -08001425 old_sector = -1;
Jens Axboe2056a782006-03-23 20:00:26 +01001426 old_dev = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 do {
1428 char b[BDEVNAME_SIZE];
1429
1430 q = bdev_get_queue(bio->bi_bdev);
Tejun Heoa7384672008-11-28 13:32:03 +09001431 if (unlikely(!q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 printk(KERN_ERR
1433 "generic_make_request: Trying to access "
1434 "nonexistent block-device %s (%Lu)\n",
1435 bdevname(bio->bi_bdev, b),
1436 (long long) bio->bi_sector);
Tejun Heoa7384672008-11-28 13:32:03 +09001437 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 }
1439
Christoph Hellwig67efc922009-09-30 13:54:20 +02001440 if (unlikely(!bio_rw_flagged(bio, BIO_RW_DISCARD) &&
1441 nr_sectors > queue_max_hw_sectors(q))) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001442 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
Martin K. Petersenae03bf62009-05-22 17:17:50 -04001443 bdevname(bio->bi_bdev, b),
1444 bio_sectors(bio),
1445 queue_max_hw_sectors(q));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 goto end_io;
1447 }
1448
Nick Pigginfde6ad22005-06-23 00:08:53 -07001449 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 goto end_io;
1451
Akinobu Mitac17bb492006-12-08 02:39:46 -08001452 if (should_fail_request(bio))
1453 goto end_io;
1454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 /*
1456 * If this device has partitions, remap block n
1457 * of partition p to block n+start(p) of the disk.
1458 */
1459 blk_partition_remap(bio);
1460
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +02001461 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio))
1462 goto end_io;
1463
NeilBrown5ddfe962006-10-30 22:07:21 -08001464 if (old_sector != -1)
Alan D. Brunelle22a7c312009-05-04 16:35:08 -04001465 trace_block_remap(q, bio, old_dev, old_sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001466
NeilBrown5ddfe962006-10-30 22:07:21 -08001467 old_sector = bio->bi_sector;
Jens Axboe2056a782006-03-23 20:00:26 +01001468 old_dev = bio->bi_bdev->bd_dev;
1469
Jens Axboec07e2b42007-07-18 13:27:58 +02001470 if (bio_check_eod(bio, nr_sectors))
1471 goto end_io;
Tejun Heoa7384672008-11-28 13:32:03 +09001472
Jens Axboe1f98a132009-09-11 14:32:04 +02001473 if (bio_rw_flagged(bio, BIO_RW_DISCARD) &&
Christoph Hellwigc15227d2009-09-30 13:52:12 +02001474 !blk_queue_discard(q)) {
Jens Axboe51fd77b2007-11-02 08:49:08 +01001475 err = -EOPNOTSUPP;
1476 goto end_io;
1477 }
NeilBrown5ddfe962006-10-30 22:07:21 -08001478
Minchan Kim01edede2009-09-08 21:56:38 +02001479 trace_block_bio_queue(q, bio);
1480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 ret = q->make_request_fn(q, bio);
1482 } while (ret);
Tejun Heoa7384672008-11-28 13:32:03 +09001483
1484 return;
1485
1486end_io:
1487 bio_endio(bio, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488}
1489
Neil Brownd89d8792007-05-01 09:53:42 +02001490/*
1491 * We only want one ->make_request_fn to be active at a time,
1492 * else stack usage with stacked devices could be a problem.
1493 * So use current->bio_{list,tail} to keep a list of requests
1494 * submited by a make_request_fn function.
1495 * current->bio_tail is also used as a flag to say if
1496 * generic_make_request is currently active in this task or not.
1497 * If it is NULL, then no make_request is active. If it is non-NULL,
1498 * then a make_request is active, and new requests should be added
1499 * at the tail
1500 */
1501void generic_make_request(struct bio *bio)
1502{
1503 if (current->bio_tail) {
1504 /* make_request is active */
1505 *(current->bio_tail) = bio;
1506 bio->bi_next = NULL;
1507 current->bio_tail = &bio->bi_next;
1508 return;
1509 }
1510 /* following loop may be a bit non-obvious, and so deserves some
1511 * explanation.
1512 * Before entering the loop, bio->bi_next is NULL (as all callers
1513 * ensure that) so we have a list with a single bio.
1514 * We pretend that we have just taken it off a longer list, so
1515 * we assign bio_list to the next (which is NULL) and bio_tail
1516 * to &bio_list, thus initialising the bio_list of new bios to be
1517 * added. __generic_make_request may indeed add some more bios
1518 * through a recursive call to generic_make_request. If it
1519 * did, we find a non-NULL value in bio_list and re-enter the loop
1520 * from the top. In this case we really did just take the bio
1521 * of the top of the list (no pretending) and so fixup bio_list and
1522 * bio_tail or bi_next, and call into __generic_make_request again.
1523 *
1524 * The loop was structured like this to make only one call to
1525 * __generic_make_request (which is important as it is large and
1526 * inlined) and to keep the structure simple.
1527 */
1528 BUG_ON(bio->bi_next);
1529 do {
1530 current->bio_list = bio->bi_next;
1531 if (bio->bi_next == NULL)
1532 current->bio_tail = &current->bio_list;
1533 else
1534 bio->bi_next = NULL;
1535 __generic_make_request(bio);
1536 bio = current->bio_list;
1537 } while (bio);
1538 current->bio_tail = NULL; /* deactivate */
1539}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540EXPORT_SYMBOL(generic_make_request);
1541
1542/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001543 * submit_bio - submit a bio to the block device layer for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1545 * @bio: The &struct bio which describes the I/O
1546 *
1547 * submit_bio() is very similar in purpose to generic_make_request(), and
1548 * uses that function to do most of the work. Both are fairly rough
Randy Dunlap710027a2008-08-19 20:13:11 +02001549 * interfaces; @bio must be presetup and ready for I/O.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 *
1551 */
1552void submit_bio(int rw, struct bio *bio)
1553{
1554 int count = bio_sectors(bio);
1555
Jens Axboe22e2c502005-06-27 10:55:12 +02001556 bio->bi_rw |= rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Jens Axboebf2de6f2007-09-27 13:01:25 +02001558 /*
1559 * If it's a regular read/write or a barrier with data attached,
1560 * go through the normal accounting stuff before submission.
1561 */
Jens Axboea9c701e2008-08-08 11:04:44 +02001562 if (bio_has_data(bio)) {
Jens Axboebf2de6f2007-09-27 13:01:25 +02001563 if (rw & WRITE) {
1564 count_vm_events(PGPGOUT, count);
1565 } else {
1566 task_io_account_read(bio->bi_size);
1567 count_vm_events(PGPGIN, count);
1568 }
1569
1570 if (unlikely(block_dump)) {
1571 char b[BDEVNAME_SIZE];
1572 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001573 current->comm, task_pid_nr(current),
Jens Axboebf2de6f2007-09-27 13:01:25 +02001574 (rw & WRITE) ? "WRITE" : "READ",
1575 (unsigned long long)bio->bi_sector,
Jens Axboe6728cb02008-01-31 13:03:55 +01001576 bdevname(bio->bi_bdev, b));
Jens Axboebf2de6f2007-09-27 13:01:25 +02001577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 }
1579
1580 generic_make_request(bio);
1581}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582EXPORT_SYMBOL(submit_bio);
1583
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001584/**
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001585 * blk_rq_check_limits - Helper function to check a request for the queue limit
1586 * @q: the queue
1587 * @rq: the request being checked
1588 *
1589 * Description:
1590 * @rq may have been made based on weaker limitations of upper-level queues
1591 * in request stacking drivers, and it may violate the limitation of @q.
1592 * Since the block layer and the underlying device driver trust @rq
1593 * after it is inserted to @q, it should be checked against @q before
1594 * the insertion using this generic function.
1595 *
1596 * This function should also be useful for request stacking drivers
1597 * in some cases below, so export this fuction.
1598 * Request stacking drivers like request-based dm may change the queue
1599 * limits while requests are in the queue (e.g. dm's table swapping).
1600 * Such request stacking drivers should check those requests agaist
1601 * the new queue limits again when they dispatch those requests,
1602 * although such checkings are also done against the old queue limits
1603 * when submitting requests.
1604 */
1605int blk_rq_check_limits(struct request_queue *q, struct request *rq)
1606{
Martin K. Petersenae03bf62009-05-22 17:17:50 -04001607 if (blk_rq_sectors(rq) > queue_max_sectors(q) ||
1608 blk_rq_bytes(rq) > queue_max_hw_sectors(q) << 9) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001609 printk(KERN_ERR "%s: over max size limit.\n", __func__);
1610 return -EIO;
1611 }
1612
1613 /*
1614 * queue's settings related to segment counting like q->bounce_pfn
1615 * may differ from that of other stacking queues.
1616 * Recalculate it to check the request correctly on this queue's
1617 * limitation.
1618 */
1619 blk_recalc_rq_segments(rq);
Martin K. Petersenae03bf62009-05-22 17:17:50 -04001620 if (rq->nr_phys_segments > queue_max_phys_segments(q) ||
1621 rq->nr_phys_segments > queue_max_hw_segments(q)) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001622 printk(KERN_ERR "%s: over max segments limit.\n", __func__);
1623 return -EIO;
1624 }
1625
1626 return 0;
1627}
1628EXPORT_SYMBOL_GPL(blk_rq_check_limits);
1629
1630/**
1631 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
1632 * @q: the queue to submit the request
1633 * @rq: the request being queued
1634 */
1635int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
1636{
1637 unsigned long flags;
1638
1639 if (blk_rq_check_limits(q, rq))
1640 return -EIO;
1641
1642#ifdef CONFIG_FAIL_MAKE_REQUEST
1643 if (rq->rq_disk && rq->rq_disk->part0.make_it_fail &&
1644 should_fail(&fail_make_request, blk_rq_bytes(rq)))
1645 return -EIO;
1646#endif
1647
1648 spin_lock_irqsave(q->queue_lock, flags);
1649
1650 /*
1651 * Submitting request must be dequeued before calling this function
1652 * because it will be linked to another request_queue
1653 */
1654 BUG_ON(blk_queued_rq(rq));
1655
1656 drive_stat_acct(rq, 1);
1657 __elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 0);
1658
1659 spin_unlock_irqrestore(q->queue_lock, flags);
1660
1661 return 0;
1662}
1663EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
1664
Tejun Heo80a761f2009-07-03 17:48:17 +09001665/**
1666 * blk_rq_err_bytes - determine number of bytes till the next failure boundary
1667 * @rq: request to examine
1668 *
1669 * Description:
1670 * A request could be merge of IOs which require different failure
1671 * handling. This function determines the number of bytes which
1672 * can be failed from the beginning of the request without
1673 * crossing into area which need to be retried further.
1674 *
1675 * Return:
1676 * The number of bytes to fail.
1677 *
1678 * Context:
1679 * queue_lock must be held.
1680 */
1681unsigned int blk_rq_err_bytes(const struct request *rq)
1682{
1683 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
1684 unsigned int bytes = 0;
1685 struct bio *bio;
1686
1687 if (!(rq->cmd_flags & REQ_MIXED_MERGE))
1688 return blk_rq_bytes(rq);
1689
1690 /*
1691 * Currently the only 'mixing' which can happen is between
1692 * different fastfail types. We can safely fail portions
1693 * which have all the failfast bits that the first one has -
1694 * the ones which are at least as eager to fail as the first
1695 * one.
1696 */
1697 for (bio = rq->bio; bio; bio = bio->bi_next) {
1698 if ((bio->bi_rw & ff) != ff)
1699 break;
1700 bytes += bio->bi_size;
1701 }
1702
1703 /* this could lead to infinite loop */
1704 BUG_ON(blk_rq_bytes(rq) && !bytes);
1705 return bytes;
1706}
1707EXPORT_SYMBOL_GPL(blk_rq_err_bytes);
1708
Jens Axboebc58ba92009-01-23 10:54:44 +01001709static void blk_account_io_completion(struct request *req, unsigned int bytes)
1710{
Jens Axboec2553b52009-04-24 08:10:11 +02001711 if (blk_do_io_stat(req)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01001712 const int rw = rq_data_dir(req);
1713 struct hd_struct *part;
1714 int cpu;
1715
1716 cpu = part_stat_lock();
Tejun Heo83096eb2009-05-07 22:24:39 +09001717 part = disk_map_sector_rcu(req->rq_disk, blk_rq_pos(req));
Jens Axboebc58ba92009-01-23 10:54:44 +01001718 part_stat_add(cpu, part, sectors[rw], bytes >> 9);
1719 part_stat_unlock();
1720 }
1721}
1722
1723static void blk_account_io_done(struct request *req)
1724{
Jens Axboebc58ba92009-01-23 10:54:44 +01001725 /*
1726 * Account IO completion. bar_rq isn't accounted as a normal
1727 * IO on queueing nor completion. Accounting the containing
1728 * request is enough.
1729 */
Jens Axboec2553b52009-04-24 08:10:11 +02001730 if (blk_do_io_stat(req) && req != &req->q->bar_rq) {
Jens Axboebc58ba92009-01-23 10:54:44 +01001731 unsigned long duration = jiffies - req->start_time;
1732 const int rw = rq_data_dir(req);
1733 struct hd_struct *part;
1734 int cpu;
1735
1736 cpu = part_stat_lock();
Tejun Heo83096eb2009-05-07 22:24:39 +09001737 part = disk_map_sector_rcu(req->rq_disk, blk_rq_pos(req));
Jens Axboebc58ba92009-01-23 10:54:44 +01001738
1739 part_stat_inc(cpu, part, ios[rw]);
1740 part_stat_add(cpu, part, ticks[rw], duration);
1741 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001742 part_dec_in_flight(part, rw);
Jens Axboebc58ba92009-01-23 10:54:44 +01001743
1744 part_stat_unlock();
1745 }
1746}
1747
Tejun Heo53a08802008-12-03 12:41:26 +01001748/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09001749 * blk_peek_request - peek at the top of a request queue
1750 * @q: request queue to peek at
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001751 *
1752 * Description:
Tejun Heo9934c8c2009-05-08 11:54:16 +09001753 * Return the request at the top of @q. The returned request
1754 * should be started using blk_start_request() before LLD starts
1755 * processing it.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001756 *
1757 * Return:
Tejun Heo9934c8c2009-05-08 11:54:16 +09001758 * Pointer to the request at the top of @q if available. Null
1759 * otherwise.
1760 *
1761 * Context:
1762 * queue_lock must be held.
1763 */
1764struct request *blk_peek_request(struct request_queue *q)
Tejun Heo158dbda2009-04-23 11:05:18 +09001765{
1766 struct request *rq;
1767 int ret;
1768
1769 while ((rq = __elv_next_request(q)) != NULL) {
1770 if (!(rq->cmd_flags & REQ_STARTED)) {
1771 /*
1772 * This is the first time the device driver
1773 * sees this request (possibly after
1774 * requeueing). Notify IO scheduler.
1775 */
1776 if (blk_sorted_rq(rq))
1777 elv_activate_rq(q, rq);
1778
1779 /*
1780 * just mark as started even if we don't start
1781 * it, a request that has been delayed should
1782 * not be passed by new incoming requests
1783 */
1784 rq->cmd_flags |= REQ_STARTED;
1785 trace_block_rq_issue(q, rq);
1786 }
1787
1788 if (!q->boundary_rq || q->boundary_rq == rq) {
1789 q->end_sector = rq_end_sector(rq);
1790 q->boundary_rq = NULL;
1791 }
1792
1793 if (rq->cmd_flags & REQ_DONTPREP)
1794 break;
1795
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001796 if (q->dma_drain_size && blk_rq_bytes(rq)) {
Tejun Heo158dbda2009-04-23 11:05:18 +09001797 /*
1798 * make sure space for the drain appears we
1799 * know we can do this because max_hw_segments
1800 * has been adjusted to be one fewer than the
1801 * device can handle
1802 */
1803 rq->nr_phys_segments++;
1804 }
1805
1806 if (!q->prep_rq_fn)
1807 break;
1808
1809 ret = q->prep_rq_fn(q, rq);
1810 if (ret == BLKPREP_OK) {
1811 break;
1812 } else if (ret == BLKPREP_DEFER) {
1813 /*
1814 * the request may have been (partially) prepped.
1815 * we need to keep this request in the front to
1816 * avoid resource deadlock. REQ_STARTED will
1817 * prevent other fs requests from passing this one.
1818 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001819 if (q->dma_drain_size && blk_rq_bytes(rq) &&
Tejun Heo158dbda2009-04-23 11:05:18 +09001820 !(rq->cmd_flags & REQ_DONTPREP)) {
1821 /*
1822 * remove the space for the drain we added
1823 * so that we don't add it again
1824 */
1825 --rq->nr_phys_segments;
1826 }
1827
1828 rq = NULL;
1829 break;
1830 } else if (ret == BLKPREP_KILL) {
1831 rq->cmd_flags |= REQ_QUIET;
James Bottomleyc143dc92009-05-30 06:43:49 +02001832 /*
1833 * Mark this request as started so we don't trigger
1834 * any debug logic in the end I/O path.
1835 */
1836 blk_start_request(rq);
Tejun Heo40cbbb72009-04-23 11:05:19 +09001837 __blk_end_request_all(rq, -EIO);
Tejun Heo158dbda2009-04-23 11:05:18 +09001838 } else {
1839 printk(KERN_ERR "%s: bad return=%d\n", __func__, ret);
1840 break;
1841 }
1842 }
1843
1844 return rq;
1845}
Tejun Heo9934c8c2009-05-08 11:54:16 +09001846EXPORT_SYMBOL(blk_peek_request);
Tejun Heo158dbda2009-04-23 11:05:18 +09001847
Tejun Heo9934c8c2009-05-08 11:54:16 +09001848void blk_dequeue_request(struct request *rq)
Tejun Heo158dbda2009-04-23 11:05:18 +09001849{
Tejun Heo9934c8c2009-05-08 11:54:16 +09001850 struct request_queue *q = rq->q;
1851
Tejun Heo158dbda2009-04-23 11:05:18 +09001852 BUG_ON(list_empty(&rq->queuelist));
1853 BUG_ON(ELV_ON_HASH(rq));
1854
1855 list_del_init(&rq->queuelist);
1856
1857 /*
1858 * the time frame between a request being removed from the lists
1859 * and to it is freed is accounted as io that is in progress at
1860 * the driver side.
1861 */
Jens Axboefb1e7532009-07-30 08:18:24 +02001862 if (blk_account_rq(rq)) {
Jens Axboe0a7ae2f2009-05-20 08:54:31 +02001863 q->in_flight[rq_is_sync(rq)]++;
Jens Axboefb1e7532009-07-30 08:18:24 +02001864 /*
1865 * Mark this device as supporting hardware queuing, if
1866 * we have more IOs in flight than 4.
1867 */
1868 if (!blk_queue_queuing(q) && queue_in_flight(q) > 4)
1869 set_bit(QUEUE_FLAG_CQ, &q->queue_flags);
1870 }
Tejun Heo158dbda2009-04-23 11:05:18 +09001871}
1872
Tejun Heo5efccd12009-04-23 11:05:18 +09001873/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09001874 * blk_start_request - start request processing on the driver
1875 * @req: request to dequeue
1876 *
1877 * Description:
1878 * Dequeue @req and start timeout timer on it. This hands off the
1879 * request to the driver.
1880 *
1881 * Block internal functions which don't want to start timer should
1882 * call blk_dequeue_request().
1883 *
1884 * Context:
1885 * queue_lock must be held.
1886 */
1887void blk_start_request(struct request *req)
1888{
1889 blk_dequeue_request(req);
1890
1891 /*
Tejun Heo5f49f632009-05-19 18:33:05 +09001892 * We are now handing the request to the hardware, initialize
1893 * resid_len to full count and add the timeout handler.
Tejun Heo9934c8c2009-05-08 11:54:16 +09001894 */
Tejun Heo5f49f632009-05-19 18:33:05 +09001895 req->resid_len = blk_rq_bytes(req);
FUJITA Tomonoridbb66c42009-06-09 05:47:10 +02001896 if (unlikely(blk_bidi_rq(req)))
1897 req->next_rq->resid_len = blk_rq_bytes(req->next_rq);
1898
Tejun Heo9934c8c2009-05-08 11:54:16 +09001899 blk_add_timer(req);
1900}
1901EXPORT_SYMBOL(blk_start_request);
1902
1903/**
1904 * blk_fetch_request - fetch a request from a request queue
1905 * @q: request queue to fetch a request from
1906 *
1907 * Description:
1908 * Return the request at the top of @q. The request is started on
1909 * return and LLD can start processing it immediately.
1910 *
1911 * Return:
1912 * Pointer to the request at the top of @q if available. Null
1913 * otherwise.
1914 *
1915 * Context:
1916 * queue_lock must be held.
1917 */
1918struct request *blk_fetch_request(struct request_queue *q)
1919{
1920 struct request *rq;
1921
1922 rq = blk_peek_request(q);
1923 if (rq)
1924 blk_start_request(rq);
1925 return rq;
1926}
1927EXPORT_SYMBOL(blk_fetch_request);
1928
1929/**
Tejun Heo2e60e022009-04-23 11:05:18 +09001930 * blk_update_request - Special helper function for request stacking drivers
Randy Dunlap8ebf9752009-06-11 20:00:41 -07001931 * @req: the request being processed
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001932 * @error: %0 for success, < %0 for error
Randy Dunlap8ebf9752009-06-11 20:00:41 -07001933 * @nr_bytes: number of bytes to complete @req
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001934 *
1935 * Description:
Randy Dunlap8ebf9752009-06-11 20:00:41 -07001936 * Ends I/O on a number of bytes attached to @req, but doesn't complete
1937 * the request structure even if @req doesn't have leftover.
1938 * If @req has leftover, sets it up for the next range of segments.
Tejun Heo2e60e022009-04-23 11:05:18 +09001939 *
1940 * This special helper function is only for request stacking drivers
1941 * (e.g. request-based dm) so that they can handle partial completion.
1942 * Actual device drivers should use blk_end_request instead.
1943 *
1944 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
1945 * %false return from this function.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001946 *
1947 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09001948 * %false - this request doesn't have any more data
1949 * %true - this request has more data
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001950 **/
Tejun Heo2e60e022009-04-23 11:05:18 +09001951bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952{
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001953 int total_bytes, bio_nbytes, next_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 struct bio *bio;
1955
Tejun Heo2e60e022009-04-23 11:05:18 +09001956 if (!req->bio)
1957 return false;
1958
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001959 trace_block_rq_complete(req->q, req);
Jens Axboe2056a782006-03-23 20:00:26 +01001960
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 /*
Tejun Heo6f414692009-04-19 07:00:41 +09001962 * For fs requests, rq is just carrier of independent bio's
1963 * and each partial completion should be handled separately.
1964 * Reset per-request error on each partial completion.
1965 *
1966 * TODO: tj: This is too subtle. It would be better to let
1967 * low level drivers do what they see fit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 */
Tejun Heo6f414692009-04-19 07:00:41 +09001969 if (blk_fs_request(req))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 req->errors = 0;
1971
Jens Axboe6728cb02008-01-31 13:03:55 +01001972 if (error && (blk_fs_request(req) && !(req->cmd_flags & REQ_QUIET))) {
1973 printk(KERN_ERR "end_request: I/O error, dev %s, sector %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 req->rq_disk ? req->rq_disk->disk_name : "?",
Tejun Heo83096eb2009-05-07 22:24:39 +09001975 (unsigned long long)blk_rq_pos(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 }
1977
Jens Axboebc58ba92009-01-23 10:54:44 +01001978 blk_account_io_completion(req, nr_bytes);
Jens Axboed72d9042005-11-01 08:35:42 +01001979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 total_bytes = bio_nbytes = 0;
1981 while ((bio = req->bio) != NULL) {
1982 int nbytes;
1983
1984 if (nr_bytes >= bio->bi_size) {
1985 req->bio = bio->bi_next;
1986 nbytes = bio->bi_size;
NeilBrown5bb23a62007-09-27 12:46:13 +02001987 req_bio_endio(req, bio, nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 next_idx = 0;
1989 bio_nbytes = 0;
1990 } else {
1991 int idx = bio->bi_idx + next_idx;
1992
Kazuhisa Ichikawaaf498d72009-05-12 13:27:45 +02001993 if (unlikely(idx >= bio->bi_vcnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 blk_dump_rq_flags(req, "__end_that");
Jens Axboe6728cb02008-01-31 13:03:55 +01001995 printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
Kazuhisa Ichikawaaf498d72009-05-12 13:27:45 +02001996 __func__, idx, bio->bi_vcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 break;
1998 }
1999
2000 nbytes = bio_iovec_idx(bio, idx)->bv_len;
2001 BIO_BUG_ON(nbytes > bio->bi_size);
2002
2003 /*
2004 * not a complete bvec done
2005 */
2006 if (unlikely(nbytes > nr_bytes)) {
2007 bio_nbytes += nr_bytes;
2008 total_bytes += nr_bytes;
2009 break;
2010 }
2011
2012 /*
2013 * advance to the next vector
2014 */
2015 next_idx++;
2016 bio_nbytes += nbytes;
2017 }
2018
2019 total_bytes += nbytes;
2020 nr_bytes -= nbytes;
2021
Jens Axboe6728cb02008-01-31 13:03:55 +01002022 bio = req->bio;
2023 if (bio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 /*
2025 * end more in this run, or just return 'not-done'
2026 */
2027 if (unlikely(nr_bytes <= 0))
2028 break;
2029 }
2030 }
2031
2032 /*
2033 * completely done
2034 */
Tejun Heo2e60e022009-04-23 11:05:18 +09002035 if (!req->bio) {
2036 /*
2037 * Reset counters so that the request stacking driver
2038 * can find how many bytes remain in the request
2039 * later.
2040 */
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002041 req->__data_len = 0;
Tejun Heo2e60e022009-04-23 11:05:18 +09002042 return false;
2043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
2045 /*
2046 * if the request wasn't completed, update state
2047 */
2048 if (bio_nbytes) {
NeilBrown5bb23a62007-09-27 12:46:13 +02002049 req_bio_endio(req, bio, bio_nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 bio->bi_idx += next_idx;
2051 bio_iovec(bio)->bv_offset += nr_bytes;
2052 bio_iovec(bio)->bv_len -= nr_bytes;
2053 }
2054
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002055 req->__data_len -= total_bytes;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002056 req->buffer = bio_data(req->bio);
2057
2058 /* update sector only for requests with clear definition of sector */
2059 if (blk_fs_request(req) || blk_discard_rq(req))
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002060 req->__sector += total_bytes >> 9;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002061
Tejun Heo80a761f2009-07-03 17:48:17 +09002062 /* mixed attributes always follow the first bio */
2063 if (req->cmd_flags & REQ_MIXED_MERGE) {
2064 req->cmd_flags &= ~REQ_FAILFAST_MASK;
2065 req->cmd_flags |= req->bio->bi_rw & REQ_FAILFAST_MASK;
2066 }
2067
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002068 /*
2069 * If total number of sectors is less than the first segment
2070 * size, something has gone terribly wrong.
2071 */
2072 if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
2073 printk(KERN_ERR "blk: request botched\n");
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002074 req->__data_len = blk_rq_cur_bytes(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002075 }
2076
2077 /* recalculate the number of segments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 blk_recalc_rq_segments(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002079
Tejun Heo2e60e022009-04-23 11:05:18 +09002080 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081}
Tejun Heo2e60e022009-04-23 11:05:18 +09002082EXPORT_SYMBOL_GPL(blk_update_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Tejun Heo2e60e022009-04-23 11:05:18 +09002084static bool blk_update_bidi_request(struct request *rq, int error,
2085 unsigned int nr_bytes,
2086 unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002087{
Tejun Heo2e60e022009-04-23 11:05:18 +09002088 if (blk_update_request(rq, error, nr_bytes))
2089 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002090
Tejun Heo2e60e022009-04-23 11:05:18 +09002091 /* Bidi request must be completed as a whole */
2092 if (unlikely(blk_bidi_rq(rq)) &&
2093 blk_update_request(rq->next_rq, error, bidi_bytes))
2094 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002095
Tejun Heo2e60e022009-04-23 11:05:18 +09002096 add_disk_randomness(rq->rq_disk);
2097
2098 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099}
2100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101/*
2102 * queue lock must be held
2103 */
Tejun Heo2e60e022009-04-23 11:05:18 +09002104static void blk_finish_request(struct request *req, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105{
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002106 if (blk_rq_tagged(req))
2107 blk_queue_end_tag(req->q, req);
2108
James Bottomleyba396a62009-05-27 14:17:08 +02002109 BUG_ON(blk_queued_rq(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
2111 if (unlikely(laptop_mode) && blk_fs_request(req))
2112 laptop_io_completion();
2113
Mike Andersone78042e2008-10-30 02:16:20 -07002114 blk_delete_timer(req);
2115
Jens Axboebc58ba92009-01-23 10:54:44 +01002116 blk_account_io_done(req);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002117
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 if (req->end_io)
Tejun Heo8ffdc652006-01-06 09:49:03 +01002119 req->end_io(req, error);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002120 else {
2121 if (blk_bidi_rq(req))
2122 __blk_put_request(req->next_rq->q, req->next_rq);
2123
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 __blk_put_request(req->q, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 }
2126}
2127
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05002128/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002129 * blk_end_bidi_request - Complete a bidi request
2130 * @rq: the request to complete
Randy Dunlap710027a2008-08-19 20:13:11 +02002131 * @error: %0 for success, < %0 for error
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002132 * @nr_bytes: number of bytes to complete @rq
2133 * @bidi_bytes: number of bytes to complete @rq->next_rq
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002134 *
2135 * Description:
2136 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
Tejun Heo2e60e022009-04-23 11:05:18 +09002137 * Drivers that supports bidi can safely call this member for any
2138 * type of request, bidi or uni. In the later case @bidi_bytes is
2139 * just ignored.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002140 *
2141 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002142 * %false - we are done with this request
2143 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002144 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002145static bool blk_end_bidi_request(struct request *rq, int error,
2146 unsigned int nr_bytes, unsigned int bidi_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002147{
2148 struct request_queue *q = rq->q;
Tejun Heo2e60e022009-04-23 11:05:18 +09002149 unsigned long flags;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002150
Tejun Heo2e60e022009-04-23 11:05:18 +09002151 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2152 return true;
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002153
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002154 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo2e60e022009-04-23 11:05:18 +09002155 blk_finish_request(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002156 spin_unlock_irqrestore(q->queue_lock, flags);
2157
Tejun Heo2e60e022009-04-23 11:05:18 +09002158 return false;
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002159}
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002160
2161/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002162 * __blk_end_bidi_request - Complete a bidi request with queue lock held
2163 * @rq: the request to complete
2164 * @error: %0 for success, < %0 for error
2165 * @nr_bytes: number of bytes to complete @rq
2166 * @bidi_bytes: number of bytes to complete @rq->next_rq
Tejun Heo5efccd12009-04-23 11:05:18 +09002167 *
2168 * Description:
Tejun Heo2e60e022009-04-23 11:05:18 +09002169 * Identical to blk_end_bidi_request() except that queue lock is
2170 * assumed to be locked on entry and remains so on return.
Tejun Heo5efccd12009-04-23 11:05:18 +09002171 *
Tejun Heo2e60e022009-04-23 11:05:18 +09002172 * Return:
2173 * %false - we are done with this request
2174 * %true - still buffers pending for this request
Tejun Heo5efccd12009-04-23 11:05:18 +09002175 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002176static bool __blk_end_bidi_request(struct request *rq, int error,
2177 unsigned int nr_bytes, unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002178{
Tejun Heo2e60e022009-04-23 11:05:18 +09002179 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2180 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002181
Tejun Heo2e60e022009-04-23 11:05:18 +09002182 blk_finish_request(rq, error);
Tejun Heo5efccd12009-04-23 11:05:18 +09002183
Tejun Heo2e60e022009-04-23 11:05:18 +09002184 return false;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002185}
2186
2187/**
2188 * blk_end_request - Helper function for drivers to complete the request.
2189 * @rq: the request being processed
2190 * @error: %0 for success, < %0 for error
2191 * @nr_bytes: number of bytes to complete
2192 *
2193 * Description:
2194 * Ends I/O on a number of bytes attached to @rq.
2195 * If @rq has leftover, sets it up for the next range of segments.
2196 *
2197 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002198 * %false - we are done with this request
2199 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002200 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002201bool blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002202{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002203 return blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002204}
Jens Axboe56ad1742009-07-28 22:11:24 +02002205EXPORT_SYMBOL(blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002206
2207/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002208 * blk_end_request_all - Helper function for drives to finish the request.
2209 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002210 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002211 *
2212 * Description:
2213 * Completely finish @rq.
2214 */
2215void blk_end_request_all(struct request *rq, int error)
2216{
2217 bool pending;
2218 unsigned int bidi_bytes = 0;
2219
2220 if (unlikely(blk_bidi_rq(rq)))
2221 bidi_bytes = blk_rq_bytes(rq->next_rq);
2222
2223 pending = blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2224 BUG_ON(pending);
2225}
Jens Axboe56ad1742009-07-28 22:11:24 +02002226EXPORT_SYMBOL(blk_end_request_all);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002227
2228/**
2229 * blk_end_request_cur - Helper function to finish the current request chunk.
2230 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002231 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002232 *
2233 * Description:
2234 * Complete the current consecutively mapped chunk from @rq.
2235 *
2236 * Return:
2237 * %false - we are done with this request
2238 * %true - still buffers pending for this request
2239 */
2240bool blk_end_request_cur(struct request *rq, int error)
2241{
2242 return blk_end_request(rq, error, blk_rq_cur_bytes(rq));
2243}
Jens Axboe56ad1742009-07-28 22:11:24 +02002244EXPORT_SYMBOL(blk_end_request_cur);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002245
2246/**
Tejun Heo80a761f2009-07-03 17:48:17 +09002247 * blk_end_request_err - Finish a request till the next failure boundary.
2248 * @rq: the request to finish till the next failure boundary for
2249 * @error: must be negative errno
2250 *
2251 * Description:
2252 * Complete @rq till the next failure boundary.
2253 *
2254 * Return:
2255 * %false - we are done with this request
2256 * %true - still buffers pending for this request
2257 */
2258bool blk_end_request_err(struct request *rq, int error)
2259{
2260 WARN_ON(error >= 0);
2261 return blk_end_request(rq, error, blk_rq_err_bytes(rq));
2262}
2263EXPORT_SYMBOL_GPL(blk_end_request_err);
2264
2265/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002266 * __blk_end_request - Helper function for drivers to complete the request.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002267 * @rq: the request being processed
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002268 * @error: %0 for success, < %0 for error
2269 * @nr_bytes: number of bytes to complete
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002270 *
2271 * Description:
2272 * Must be called with queue lock held unlike blk_end_request().
2273 *
2274 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002275 * %false - we are done with this request
2276 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002277 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002278bool __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002279{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002280 return __blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002281}
Jens Axboe56ad1742009-07-28 22:11:24 +02002282EXPORT_SYMBOL(__blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002283
2284/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002285 * __blk_end_request_all - Helper function for drives to finish the request.
2286 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002287 * @error: %0 for success, < %0 for error
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002288 *
2289 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002290 * Completely finish @rq. Must be called with queue lock held.
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002291 */
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002292void __blk_end_request_all(struct request *rq, int error)
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002293{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002294 bool pending;
2295 unsigned int bidi_bytes = 0;
2296
2297 if (unlikely(blk_bidi_rq(rq)))
2298 bidi_bytes = blk_rq_bytes(rq->next_rq);
2299
2300 pending = __blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2301 BUG_ON(pending);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002302}
Jens Axboe56ad1742009-07-28 22:11:24 +02002303EXPORT_SYMBOL(__blk_end_request_all);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002304
2305/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002306 * __blk_end_request_cur - Helper function to finish the current request chunk.
2307 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002308 * @error: %0 for success, < %0 for error
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002309 *
2310 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002311 * Complete the current consecutively mapped chunk from @rq. Must
2312 * be called with queue lock held.
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002313 *
2314 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002315 * %false - we are done with this request
2316 * %true - still buffers pending for this request
2317 */
2318bool __blk_end_request_cur(struct request *rq, int error)
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002319{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002320 return __blk_end_request(rq, error, blk_rq_cur_bytes(rq));
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002321}
Jens Axboe56ad1742009-07-28 22:11:24 +02002322EXPORT_SYMBOL(__blk_end_request_cur);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002323
Tejun Heo80a761f2009-07-03 17:48:17 +09002324/**
2325 * __blk_end_request_err - Finish a request till the next failure boundary.
2326 * @rq: the request to finish till the next failure boundary for
2327 * @error: must be negative errno
2328 *
2329 * Description:
2330 * Complete @rq till the next failure boundary. Must be called
2331 * with queue lock held.
2332 *
2333 * Return:
2334 * %false - we are done with this request
2335 * %true - still buffers pending for this request
2336 */
2337bool __blk_end_request_err(struct request *rq, int error)
2338{
2339 WARN_ON(error >= 0);
2340 return __blk_end_request(rq, error, blk_rq_err_bytes(rq));
2341}
2342EXPORT_SYMBOL_GPL(__blk_end_request_err);
2343
Jens Axboe86db1e22008-01-29 14:53:40 +01002344void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2345 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346{
Tejun Heoa82afdf2009-07-03 17:48:16 +09002347 /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw */
2348 rq->cmd_flags |= bio->bi_rw & REQ_RW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
David Woodhousefb2dce82008-08-05 18:01:53 +01002350 if (bio_has_data(bio)) {
2351 rq->nr_phys_segments = bio_phys_segments(q, bio);
David Woodhousefb2dce82008-08-05 18:01:53 +01002352 rq->buffer = bio_data(bio);
2353 }
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002354 rq->__data_len = bio->bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 rq->bio = rq->biotail = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
NeilBrown66846572007-08-16 13:31:28 +02002357 if (bio->bi_bdev)
2358 rq->rq_disk = bio->bi_bdev->bd_disk;
2359}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Ilya Loginov2d4dc892009-11-26 09:16:19 +01002361#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
2362/**
2363 * rq_flush_dcache_pages - Helper function to flush all pages in a request
2364 * @rq: the request to be flushed
2365 *
2366 * Description:
2367 * Flush all pages in @rq.
2368 */
2369void rq_flush_dcache_pages(struct request *rq)
2370{
2371 struct req_iterator iter;
2372 struct bio_vec *bvec;
2373
2374 rq_for_each_segment(bvec, rq, iter)
2375 flush_dcache_page(bvec->bv_page);
2376}
2377EXPORT_SYMBOL_GPL(rq_flush_dcache_pages);
2378#endif
2379
Kiyoshi Uedaef9e3fa2008-10-01 16:12:15 +02002380/**
2381 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
2382 * @q : the queue of the device being checked
2383 *
2384 * Description:
2385 * Check if underlying low-level drivers of a device are busy.
2386 * If the drivers want to export their busy state, they must set own
2387 * exporting function using blk_queue_lld_busy() first.
2388 *
2389 * Basically, this function is used only by request stacking drivers
2390 * to stop dispatching requests to underlying devices when underlying
2391 * devices are busy. This behavior helps more I/O merging on the queue
2392 * of the request stacking driver and prevents I/O throughput regression
2393 * on burst I/O load.
2394 *
2395 * Return:
2396 * 0 - Not busy (The request stacking driver should dispatch request)
2397 * 1 - Busy (The request stacking driver should stop dispatching request)
2398 */
2399int blk_lld_busy(struct request_queue *q)
2400{
2401 if (q->lld_busy_fn)
2402 return q->lld_busy_fn(q);
2403
2404 return 0;
2405}
2406EXPORT_SYMBOL_GPL(blk_lld_busy);
2407
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002408/**
2409 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
2410 * @rq: the clone request to be cleaned up
2411 *
2412 * Description:
2413 * Free all bios in @rq for a cloned request.
2414 */
2415void blk_rq_unprep_clone(struct request *rq)
2416{
2417 struct bio *bio;
2418
2419 while ((bio = rq->bio) != NULL) {
2420 rq->bio = bio->bi_next;
2421
2422 bio_put(bio);
2423 }
2424}
2425EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
2426
2427/*
2428 * Copy attributes of the original request to the clone request.
2429 * The actual data parts (e.g. ->cmd, ->buffer, ->sense) are not copied.
2430 */
2431static void __blk_rq_prep_clone(struct request *dst, struct request *src)
2432{
2433 dst->cpu = src->cpu;
2434 dst->cmd_flags = (rq_data_dir(src) | REQ_NOMERGE);
2435 dst->cmd_type = src->cmd_type;
2436 dst->__sector = blk_rq_pos(src);
2437 dst->__data_len = blk_rq_bytes(src);
2438 dst->nr_phys_segments = src->nr_phys_segments;
2439 dst->ioprio = src->ioprio;
2440 dst->extra_len = src->extra_len;
2441}
2442
2443/**
2444 * blk_rq_prep_clone - Helper function to setup clone request
2445 * @rq: the request to be setup
2446 * @rq_src: original request to be cloned
2447 * @bs: bio_set that bios for clone are allocated from
2448 * @gfp_mask: memory allocation mask for bio
2449 * @bio_ctr: setup function to be called for each clone bio.
2450 * Returns %0 for success, non %0 for failure.
2451 * @data: private data to be passed to @bio_ctr
2452 *
2453 * Description:
2454 * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
2455 * The actual data parts of @rq_src (e.g. ->cmd, ->buffer, ->sense)
2456 * are not copied, and copying such parts is the caller's responsibility.
2457 * Also, pages which the original bios are pointing to are not copied
2458 * and the cloned bios just point same pages.
2459 * So cloned bios must be completed before original bios, which means
2460 * the caller must complete @rq before @rq_src.
2461 */
2462int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
2463 struct bio_set *bs, gfp_t gfp_mask,
2464 int (*bio_ctr)(struct bio *, struct bio *, void *),
2465 void *data)
2466{
2467 struct bio *bio, *bio_src;
2468
2469 if (!bs)
2470 bs = fs_bio_set;
2471
2472 blk_rq_init(NULL, rq);
2473
2474 __rq_for_each_bio(bio_src, rq_src) {
2475 bio = bio_alloc_bioset(gfp_mask, bio_src->bi_max_vecs, bs);
2476 if (!bio)
2477 goto free_and_out;
2478
2479 __bio_clone(bio, bio_src);
2480
2481 if (bio_integrity(bio_src) &&
Martin K. Petersen7878cba2009-06-26 15:37:49 +02002482 bio_integrity_clone(bio, bio_src, gfp_mask, bs))
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002483 goto free_and_out;
2484
2485 if (bio_ctr && bio_ctr(bio, bio_src, data))
2486 goto free_and_out;
2487
2488 if (rq->bio) {
2489 rq->biotail->bi_next = bio;
2490 rq->biotail = bio;
2491 } else
2492 rq->bio = rq->biotail = bio;
2493 }
2494
2495 __blk_rq_prep_clone(rq, rq_src);
2496
2497 return 0;
2498
2499free_and_out:
2500 if (bio)
2501 bio_free(bio, bs);
2502 blk_rq_unprep_clone(rq);
2503
2504 return -ENOMEM;
2505}
2506EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
2507
Jens Axboe18887ad2008-07-28 13:08:45 +02002508int kblockd_schedule_work(struct request_queue *q, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509{
2510 return queue_work(kblockd_workqueue, work);
2511}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512EXPORT_SYMBOL(kblockd_schedule_work);
2513
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514int __init blk_dev_init(void)
2515{
Nikanth Karthikesan9eb55b02009-04-27 14:53:54 +02002516 BUILD_BUG_ON(__REQ_NR_BITS > 8 *
2517 sizeof(((struct request *)0)->cmd_flags));
2518
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 kblockd_workqueue = create_workqueue("kblockd");
2520 if (!kblockd_workqueue)
2521 panic("Failed to create kblockd\n");
2522
2523 request_cachep = kmem_cache_create("blkdev_requests",
Paul Mundt20c2df82007-07-20 10:11:58 +09002524 sizeof(struct request), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
Jens Axboe8324aa92008-01-29 14:51:59 +01002526 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02002527 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 return 0;
2530}
2531