blob: d17d71c71d4f16e1c878f97ee76218688149b1ef [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 1994, Karl Keyte: Added support for disk statistics
4 * Elevator latency, (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
5 * Queue request tables / lock, selectable elevator, Jens Axboe <axboe@suse.de>
Jens Axboe6728cb02008-01-31 13:03:55 +01006 * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au>
7 * - July2000
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
9 */
10
11/*
12 * This handles all read/write requests to block devices
13 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/backing-dev.h>
17#include <linux/bio.h>
18#include <linux/blkdev.h>
19#include <linux/highmem.h>
20#include <linux/mm.h>
21#include <linux/kernel_stat.h>
22#include <linux/string.h>
23#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/completion.h>
25#include <linux/slab.h>
26#include <linux/swap.h>
27#include <linux/writeback.h>
Andrew Mortonfaccbd4b2006-12-10 02:19:35 -080028#include <linux/task_io_accounting_ops.h>
Jens Axboe2056a782006-03-23 20:00:26 +010029#include <linux/blktrace_api.h>
Akinobu Mitac17bb492006-12-08 02:39:46 -080030#include <linux/fault-inject.h>
Li Zefan55782132009-06-09 13:43:05 +080031
32#define CREATE_TRACE_POINTS
33#include <trace/events/block.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Jens Axboe8324aa92008-01-29 14:51:59 +010035#include "blk.h"
36
Ingo Molnar0bfc2452008-11-26 11:59:56 +010037EXPORT_TRACEPOINT_SYMBOL_GPL(block_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);
73 part_inc_in_flight(part);
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 Axboee3335de92008-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;
501 err = bdi_init(&q->backing_dev_info);
502 if (err) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100503 kmem_cache_free(blk_requestq_cachep, q);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700504 return NULL;
505 }
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 init_timer(&q->unplug_timer);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700508 setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
509 INIT_LIST_HEAD(&q->timeout_list);
Peter Zijlstra713ada92008-10-16 13:44:57 +0200510 INIT_WORK(&q->unplug_work, blk_unplug_work);
Al Viro483f4af2006-03-18 18:34:37 -0500511
Jens Axboe8324aa92008-01-29 14:51:59 +0100512 kobject_init(&q->kobj, &blk_queue_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Al Viro483f4af2006-03-18 18:34:37 -0500514 mutex_init(&q->sysfs_lock);
Neil Browne7e72bf2008-05-14 16:05:54 -0700515 spin_lock_init(&q->__queue_lock);
Al Viro483f4af2006-03-18 18:34:37 -0500516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return q;
518}
Christoph Lameter19460892005-06-23 00:08:19 -0700519EXPORT_SYMBOL(blk_alloc_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521/**
522 * blk_init_queue - prepare a request queue for use with a block device
523 * @rfn: The function to be called to process requests that have been
524 * placed on the queue.
525 * @lock: Request queue spin lock
526 *
527 * Description:
528 * If a block device wishes to use the standard request handling procedures,
529 * which sorts requests and coalesces adjacent requests, then it must
530 * call blk_init_queue(). The function @rfn will be called when there
531 * are requests on the queue that need to be processed. If the device
532 * supports plugging, then @rfn may not be called immediately when requests
533 * are available on the queue, but may be called at some time later instead.
534 * Plugged queues are generally unplugged when a buffer belonging to one
535 * of the requests on the queue is needed, or due to memory pressure.
536 *
537 * @rfn is not required, or even expected, to remove all requests off the
538 * queue, but only as many as it can handle at a time. If it does leave
539 * requests on the queue, it is responsible for arranging that the requests
540 * get dealt with eventually.
541 *
542 * The queue spin lock must be held while manipulating the requests on the
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200543 * request queue; this lock will be taken also from interrupt context, so irq
544 * disabling is needed for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 *
Randy Dunlap710027a2008-08-19 20:13:11 +0200546 * Function returns a pointer to the initialized request queue, or %NULL if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 * it didn't succeed.
548 *
549 * Note:
550 * blk_init_queue() must be paired with a blk_cleanup_queue() call
551 * when the block device is deactivated (such as at module unload).
552 **/
Christoph Lameter19460892005-06-23 00:08:19 -0700553
Jens Axboe165125e2007-07-24 09:28:11 +0200554struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Christoph Lameter19460892005-06-23 00:08:19 -0700556 return blk_init_queue_node(rfn, lock, -1);
557}
558EXPORT_SYMBOL(blk_init_queue);
559
Jens Axboe165125e2007-07-24 09:28:11 +0200560struct request_queue *
Christoph Lameter19460892005-06-23 00:08:19 -0700561blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
562{
Jens Axboe165125e2007-07-24 09:28:11 +0200563 struct request_queue *q = blk_alloc_queue_node(GFP_KERNEL, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 if (!q)
566 return NULL;
567
Christoph Lameter19460892005-06-23 00:08:19 -0700568 q->node = node_id;
Al Viro8669aaf2006-03-18 13:50:00 -0500569 if (blk_init_free_list(q)) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100570 kmem_cache_free(blk_requestq_cachep, q);
Al Viro8669aaf2006-03-18 13:50:00 -0500571 return NULL;
572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
152587d2005-04-12 16:22:06 -0500574 /*
575 * if caller didn't supply a lock, they get per-queue locking with
576 * our embedded lock
577 */
Neil Browne7e72bf2008-05-14 16:05:54 -0700578 if (!lock)
152587d2005-04-12 16:22:06 -0500579 lock = &q->__queue_lock;
152587d2005-04-12 16:22:06 -0500580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 q->request_fn = rfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 q->prep_rq_fn = NULL;
583 q->unplug_fn = generic_unplug_device;
Jens Axboebc58ba92009-01-23 10:54:44 +0100584 q->queue_flags = QUEUE_FLAG_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 q->queue_lock = lock;
586
Jens Axboef3b144a2009-03-06 08:48:33 +0100587 /*
588 * This also sets hw/phys segments, boundary and size
589 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 blk_queue_make_request(q, __make_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Alan Stern44ec9542007-02-20 11:01:57 -0500592 q->sg_reserved_size = INT_MAX;
593
FUJITA Tomonoriabf54392008-08-16 14:10:05 +0900594 blk_set_cmd_filter_defaults(&q->cmd_filter);
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 /*
597 * all done
598 */
599 if (!elevator_init(q, NULL)) {
600 blk_queue_congestion_threshold(q);
601 return q;
602 }
603
Al Viro8669aaf2006-03-18 13:50:00 -0500604 blk_put_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return NULL;
606}
Christoph Lameter19460892005-06-23 00:08:19 -0700607EXPORT_SYMBOL(blk_init_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Jens Axboe165125e2007-07-24 09:28:11 +0200609int blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Nick Pigginfde6ad22005-06-23 00:08:53 -0700611 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
Al Viro483f4af2006-03-18 18:34:37 -0500612 kobject_get(&q->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return 0;
614 }
615
616 return 1;
617}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Jens Axboe165125e2007-07-24 09:28:11 +0200619static inline void blk_free_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Jens Axboe4aff5e22006-08-10 08:44:47 +0200621 if (rq->cmd_flags & REQ_ELVPRIV)
Tejun Heocb98fc82005-10-28 08:29:39 +0200622 elv_put_request(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 mempool_free(rq, q->rq.rq_pool);
624}
625
Jens Axboe1ea25ecb2006-07-18 22:24:11 +0200626static struct request *
Jerome Marchand42dad762009-04-22 14:01:49 +0200627blk_alloc_request(struct request_queue *q, int flags, int priv, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
629 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
630
631 if (!rq)
632 return NULL;
633
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200634 blk_rq_init(q, rq);
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200635
Jerome Marchand42dad762009-04-22 14:01:49 +0200636 rq->cmd_flags = flags | REQ_ALLOCED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Tejun Heocb98fc82005-10-28 08:29:39 +0200638 if (priv) {
Jens Axboecb78b282006-07-28 09:32:57 +0200639 if (unlikely(elv_set_request(q, rq, gfp_mask))) {
Tejun Heocb98fc82005-10-28 08:29:39 +0200640 mempool_free(rq, q->rq.rq_pool);
641 return NULL;
642 }
Jens Axboe4aff5e22006-08-10 08:44:47 +0200643 rq->cmd_flags |= REQ_ELVPRIV;
Tejun Heocb98fc82005-10-28 08:29:39 +0200644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Tejun Heocb98fc82005-10-28 08:29:39 +0200646 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
649/*
650 * ioc_batching returns true if the ioc is a valid batching request and
651 * should be given priority access to a request.
652 */
Jens Axboe165125e2007-07-24 09:28:11 +0200653static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
655 if (!ioc)
656 return 0;
657
658 /*
659 * Make sure the process is able to allocate at least 1 request
660 * even if the batch times out, otherwise we could theoretically
661 * lose wakeups.
662 */
663 return ioc->nr_batch_requests == q->nr_batching ||
664 (ioc->nr_batch_requests > 0
665 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
666}
667
668/*
669 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
670 * will cause the process to be a "batcher" on all queues in the system. This
671 * is the behaviour we want though - once it gets a wakeup it should be given
672 * a nice run.
673 */
Jens Axboe165125e2007-07-24 09:28:11 +0200674static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
676 if (!ioc || ioc_batching(q, ioc))
677 return;
678
679 ioc->nr_batch_requests = q->nr_batching;
680 ioc->last_waited = jiffies;
681}
682
Jens Axboe1faa16d2009-04-06 14:48:01 +0200683static void __freed_request(struct request_queue *q, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
685 struct request_list *rl = &q->rq;
686
Jens Axboe1faa16d2009-04-06 14:48:01 +0200687 if (rl->count[sync] < queue_congestion_off_threshold(q))
688 blk_clear_queue_congested(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Jens Axboe1faa16d2009-04-06 14:48:01 +0200690 if (rl->count[sync] + 1 <= q->nr_requests) {
691 if (waitqueue_active(&rl->wait[sync]))
692 wake_up(&rl->wait[sync]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Jens Axboe1faa16d2009-04-06 14:48:01 +0200694 blk_clear_queue_full(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
696}
697
698/*
699 * A request has just been released. Account for it, update the full and
700 * congestion status, wake up any waiters. Called under q->queue_lock.
701 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200702static void freed_request(struct request_queue *q, int sync, int priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
704 struct request_list *rl = &q->rq;
705
Jens Axboe1faa16d2009-04-06 14:48:01 +0200706 rl->count[sync]--;
Tejun Heocb98fc82005-10-28 08:29:39 +0200707 if (priv)
708 rl->elvpriv--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Jens Axboe1faa16d2009-04-06 14:48:01 +0200710 __freed_request(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Jens Axboe1faa16d2009-04-06 14:48:01 +0200712 if (unlikely(rl->starved[sync ^ 1]))
713 __freed_request(q, sync ^ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716/*
Nick Piggind6344532005-06-28 20:45:14 -0700717 * Get a free request, queue_lock must be held.
718 * Returns NULL on failure, with queue_lock held.
719 * Returns !NULL on success, with queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 */
Jens Axboe165125e2007-07-24 09:28:11 +0200721static struct request *get_request(struct request_queue *q, int rw_flags,
Jens Axboe7749a8d2006-12-13 13:02:26 +0100722 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
724 struct request *rq = NULL;
725 struct request_list *rl = &q->rq;
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100726 struct io_context *ioc = NULL;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200727 const bool is_sync = rw_is_sync(rw_flags) != 0;
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100728 int may_queue, priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Jens Axboe7749a8d2006-12-13 13:02:26 +0100730 may_queue = elv_may_queue(q, rw_flags);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100731 if (may_queue == ELV_MQUEUE_NO)
732 goto rq_starved;
733
Jens Axboe1faa16d2009-04-06 14:48:01 +0200734 if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) {
735 if (rl->count[is_sync]+1 >= q->nr_requests) {
Jens Axboeb5deef92006-07-19 23:39:40 +0200736 ioc = current_io_context(GFP_ATOMIC, q->node);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100737 /*
738 * The queue will fill after this allocation, so set
739 * it as full, and mark this process as "batching".
740 * This process will be allowed to complete a batch of
741 * requests, others will be blocked.
742 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200743 if (!blk_queue_full(q, is_sync)) {
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100744 ioc_set_batching(q, ioc);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200745 blk_set_queue_full(q, is_sync);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100746 } else {
747 if (may_queue != ELV_MQUEUE_MUST
748 && !ioc_batching(q, ioc)) {
749 /*
750 * The queue is full and the allocating
751 * process is not a "batcher", and not
752 * exempted by the IO scheduler
753 */
754 goto out;
755 }
756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
Jens Axboe1faa16d2009-04-06 14:48:01 +0200758 blk_set_queue_congested(q, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760
Jens Axboe082cf692005-06-28 16:35:11 +0200761 /*
762 * Only allow batching queuers to allocate up to 50% over the defined
763 * limit of requests, otherwise we could have thousands of requests
764 * allocated with any setting of ->nr_requests
765 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200766 if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
Jens Axboe082cf692005-06-28 16:35:11 +0200767 goto out;
Hugh Dickinsfd782a42005-06-29 15:15:40 +0100768
Jens Axboe1faa16d2009-04-06 14:48:01 +0200769 rl->count[is_sync]++;
770 rl->starved[is_sync] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200771
Jens Axboe64521d12005-10-28 08:30:39 +0200772 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Tejun Heocb98fc82005-10-28 08:29:39 +0200773 if (priv)
774 rl->elvpriv++;
775
Jerome Marchand42dad762009-04-22 14:01:49 +0200776 if (blk_queue_io_stat(q))
777 rw_flags |= REQ_IO_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 spin_unlock_irq(q->queue_lock);
779
Jens Axboe7749a8d2006-12-13 13:02:26 +0100780 rq = blk_alloc_request(q, rw_flags, priv, gfp_mask);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100781 if (unlikely(!rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 /*
783 * Allocation failed presumably due to memory. Undo anything
784 * we might have messed up.
785 *
786 * Allocating task should really be put onto the front of the
787 * wait queue, but this is pretty rare.
788 */
789 spin_lock_irq(q->queue_lock);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200790 freed_request(q, is_sync, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 /*
793 * in the very unlikely event that allocation failed and no
794 * requests for this direction was pending, mark us starved
795 * so that freeing of a request in the other direction will
796 * notice us. another possible fix would be to split the
797 * rq mempool into READ and WRITE
798 */
799rq_starved:
Jens Axboe1faa16d2009-04-06 14:48:01 +0200800 if (unlikely(rl->count[is_sync] == 0))
801 rl->starved[is_sync] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 goto out;
804 }
805
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100806 /*
807 * ioc may be NULL here, and ioc_batching will be false. That's
808 * OK, if the queue is under the request limit then requests need
809 * not count toward the nr_batch_requests limit. There will always
810 * be some limit enforced by BLK_BATCH_TIME.
811 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if (ioc_batching(q, ioc))
813 ioc->nr_batch_requests--;
Jens Axboe6728cb02008-01-31 13:03:55 +0100814
Jens Axboe1faa16d2009-04-06 14:48:01 +0200815 trace_block_getrq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return rq;
818}
819
820/*
821 * No available requests for this queue, unplug the device and wait for some
822 * requests to become available.
Nick Piggind6344532005-06-28 20:45:14 -0700823 *
824 * Called with q->queue_lock held, and returns with it unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 */
Jens Axboe165125e2007-07-24 09:28:11 +0200826static struct request *get_request_wait(struct request_queue *q, int rw_flags,
Jens Axboe22e2c502005-06-27 10:55:12 +0200827 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
Jens Axboe1faa16d2009-04-06 14:48:01 +0200829 const bool is_sync = rw_is_sync(rw_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 struct request *rq;
831
Jens Axboe7749a8d2006-12-13 13:02:26 +0100832 rq = get_request(q, rw_flags, bio, GFP_NOIO);
Nick Piggin450991b2005-06-28 20:45:13 -0700833 while (!rq) {
834 DEFINE_WAIT(wait);
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200835 struct io_context *ioc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 struct request_list *rl = &q->rq;
837
Jens Axboe1faa16d2009-04-06 14:48:01 +0200838 prepare_to_wait_exclusive(&rl->wait[is_sync], &wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 TASK_UNINTERRUPTIBLE);
840
Jens Axboe1faa16d2009-04-06 14:48:01 +0200841 trace_block_sleeprq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200843 __generic_unplug_device(q);
844 spin_unlock_irq(q->queue_lock);
845 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200847 /*
848 * After sleeping, we become a "batching" process and
849 * will be able to allocate at least one request, and
850 * up to a big batch of them for a small period time.
851 * See ioc_batching, ioc_set_batching
852 */
853 ioc = current_io_context(GFP_NOIO, q->node);
854 ioc_set_batching(q, ioc);
Jens Axboe2056a782006-03-23 20:00:26 +0100855
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200856 spin_lock_irq(q->queue_lock);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200857 finish_wait(&rl->wait[is_sync], &wait);
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200858
859 rq = get_request(q, rw_flags, bio, GFP_NOIO);
860 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 return rq;
863}
864
Jens Axboe165125e2007-07-24 09:28:11 +0200865struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
867 struct request *rq;
868
869 BUG_ON(rw != READ && rw != WRITE);
870
Nick Piggind6344532005-06-28 20:45:14 -0700871 spin_lock_irq(q->queue_lock);
872 if (gfp_mask & __GFP_WAIT) {
Jens Axboe22e2c502005-06-27 10:55:12 +0200873 rq = get_request_wait(q, rw, NULL);
Nick Piggind6344532005-06-28 20:45:14 -0700874 } else {
Jens Axboe22e2c502005-06-27 10:55:12 +0200875 rq = get_request(q, rw, NULL, gfp_mask);
Nick Piggind6344532005-06-28 20:45:14 -0700876 if (!rq)
877 spin_unlock_irq(q->queue_lock);
878 }
879 /* q->queue_lock is unlocked at this point */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 return rq;
882}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883EXPORT_SYMBOL(blk_get_request);
884
885/**
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300886 * blk_make_request - given a bio, allocate a corresponding struct request.
Jens Axboedc72ef42006-07-20 14:54:05 +0200887 *
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300888 * @bio: The bio describing the memory mappings that will be submitted for IO.
889 * It may be a chained-bio properly constructed by block/bio layer.
Jens Axboedc72ef42006-07-20 14:54:05 +0200890 *
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300891 * blk_make_request is the parallel of generic_make_request for BLOCK_PC
892 * type commands. Where the struct request needs to be farther initialized by
893 * the caller. It is passed a &struct bio, which describes the memory info of
894 * the I/O transfer.
895 *
896 * The caller of blk_make_request must make sure that bi_io_vec
897 * are set to describe the memory buffers. That bio_data_dir() will return
898 * the needed direction of the request. (And all bio's in the passed bio-chain
899 * are properly set accordingly)
900 *
901 * If called under none-sleepable conditions, mapped bio buffers must not
902 * need bouncing, by calling the appropriate masked or flagged allocator,
903 * suitable for the target device. Otherwise the call to blk_queue_bounce will
904 * BUG.
Jens Axboe53674ac2009-05-19 19:52:35 +0200905 *
906 * WARNING: When allocating/cloning a bio-chain, careful consideration should be
907 * given to how you allocate bios. In particular, you cannot use __GFP_WAIT for
908 * anything but the first bio in the chain. Otherwise you risk waiting for IO
909 * completion of a bio that hasn't been submitted yet, thus resulting in a
910 * deadlock. Alternatively bios should be allocated using bio_kmalloc() instead
911 * of bio_alloc(), as that avoids the mempool deadlock.
912 * If possible a big IO should be split into smaller parts when allocation
913 * fails. Partial allocation should not be an error, or you risk a live-lock.
Jens Axboedc72ef42006-07-20 14:54:05 +0200914 */
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300915struct request *blk_make_request(struct request_queue *q, struct bio *bio,
916 gfp_t gfp_mask)
Jens Axboedc72ef42006-07-20 14:54:05 +0200917{
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300918 struct request *rq = blk_get_request(q, bio_data_dir(bio), gfp_mask);
919
920 if (unlikely(!rq))
921 return ERR_PTR(-ENOMEM);
922
923 for_each_bio(bio) {
924 struct bio *bounce_bio = bio;
925 int ret;
926
927 blk_queue_bounce(q, &bounce_bio);
928 ret = blk_rq_append_bio(q, rq, bounce_bio);
929 if (unlikely(ret)) {
930 blk_put_request(rq);
931 return ERR_PTR(ret);
932 }
933 }
934
935 return rq;
Jens Axboedc72ef42006-07-20 14:54:05 +0200936}
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300937EXPORT_SYMBOL(blk_make_request);
Jens Axboedc72ef42006-07-20 14:54:05 +0200938
939/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 * blk_requeue_request - put a request back on queue
941 * @q: request queue where request should be inserted
942 * @rq: request to be inserted
943 *
944 * Description:
945 * Drivers often keep queueing requests until the hardware cannot accept
946 * more, when that condition happens we need to put the request back
947 * on the queue. Must be called with queue lock held.
948 */
Jens Axboe165125e2007-07-24 09:28:11 +0200949void blk_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Jens Axboe242f9dc2008-09-14 05:55:09 -0700951 blk_delete_timer(rq);
952 blk_clear_rq_complete(rq);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100953 trace_block_rq_requeue(q, rq);
Jens Axboe2056a782006-03-23 20:00:26 +0100954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (blk_rq_tagged(rq))
956 blk_queue_end_tag(q, rq);
957
James Bottomleyba396a62009-05-27 14:17:08 +0200958 BUG_ON(blk_queued_rq(rq));
959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 elv_requeue_request(q, rq);
961}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962EXPORT_SYMBOL(blk_requeue_request);
963
964/**
Randy Dunlap710027a2008-08-19 20:13:11 +0200965 * blk_insert_request - insert a special request into a request queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 * @q: request queue where request should be inserted
967 * @rq: request to be inserted
968 * @at_head: insert request at head or tail of queue
969 * @data: private data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 *
971 * Description:
972 * Many block devices need to execute commands asynchronously, so they don't
973 * block the whole kernel from preemption during request execution. This is
974 * accomplished normally by inserting aritficial requests tagged as
Randy Dunlap710027a2008-08-19 20:13:11 +0200975 * REQ_TYPE_SPECIAL in to the corresponding request queue, and letting them
976 * be scheduled for actual execution by the request queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 *
978 * We have the option of inserting the head or the tail of the queue.
979 * Typically we use the tail for new ioctls and so forth. We use the head
980 * of the queue for things like a QUEUE_FULL message from a device, or a
981 * host that is unable to accept a particular command.
982 */
Jens Axboe165125e2007-07-24 09:28:11 +0200983void blk_insert_request(struct request_queue *q, struct request *rq,
Tejun Heo 867d1192005-04-24 02:06:05 -0500984 int at_head, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Tejun Heo 867d1192005-04-24 02:06:05 -0500986 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 unsigned long flags;
988
989 /*
990 * tell I/O scheduler that this isn't a regular read/write (ie it
991 * must not attempt merges on this) and that it acts as a soft
992 * barrier
993 */
Jens Axboe4aff5e22006-08-10 08:44:47 +0200994 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 rq->special = data;
997
998 spin_lock_irqsave(q->queue_lock, flags);
999
1000 /*
1001 * If command is tagged, release the tag
1002 */
Tejun Heo 867d1192005-04-24 02:06:05 -05001003 if (blk_rq_tagged(rq))
1004 blk_queue_end_tag(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Jerome Marchandb238b3d2007-10-23 15:05:46 +02001006 drive_stat_acct(rq, 1);
Tejun Heo 867d1192005-04-24 02:06:05 -05001007 __elv_add_request(q, rq, where, 0);
Tejun Heoa7f55792009-04-23 11:05:17 +09001008 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 spin_unlock_irqrestore(q->queue_lock, flags);
1010}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011EXPORT_SYMBOL(blk_insert_request);
1012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013/*
1014 * add-request adds a request to the linked list.
1015 * queue lock is held and interrupts disabled, as we muck with the
1016 * request queue list.
1017 */
Jens Axboe6728cb02008-01-31 13:03:55 +01001018static inline void add_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
Jerome Marchandb238b3d2007-10-23 15:05:46 +02001020 drive_stat_acct(req, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 /*
1023 * elevator indicated where it wants this request to be
1024 * inserted at elevator_merge time
1025 */
1026 __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
1027}
Jens Axboe6728cb02008-01-31 13:03:55 +01001028
Tejun Heo074a7ac2008-08-25 19:56:14 +09001029static void part_round_stats_single(int cpu, struct hd_struct *part,
1030 unsigned long now)
1031{
1032 if (now == part->stamp)
1033 return;
1034
1035 if (part->in_flight) {
1036 __part_stat_add(cpu, part, time_in_queue,
1037 part->in_flight * (now - part->stamp));
1038 __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
1039 }
1040 part->stamp = now;
1041}
1042
1043/**
Randy Dunlap496aa8a2008-10-16 07:46:23 +02001044 * part_round_stats() - Round off the performance stats on a struct disk_stats.
1045 * @cpu: cpu number for stats access
1046 * @part: target partition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 *
1048 * The average IO queue length and utilisation statistics are maintained
1049 * by observing the current state of the queue length and the amount of
1050 * time it has been in this state for.
1051 *
1052 * Normally, that accounting is done on IO completion, but that can result
1053 * in more than a second's worth of IO being accounted for within any one
1054 * second, leading to >100% utilisation. To deal with that, we call this
1055 * function to do a round-off before returning the results when reading
1056 * /proc/diskstats. This accounts immediately for all queue usage up to
1057 * the current jiffies and restarts the counters again.
1058 */
Tejun Heoc9959052008-08-25 19:47:21 +09001059void part_round_stats(int cpu, struct hd_struct *part)
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001060{
1061 unsigned long now = jiffies;
1062
Tejun Heo074a7ac2008-08-25 19:56:14 +09001063 if (part->partno)
1064 part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
1065 part_round_stats_single(cpu, part, now);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001066}
Tejun Heo074a7ac2008-08-25 19:56:14 +09001067EXPORT_SYMBOL_GPL(part_round_stats);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069/*
1070 * queue lock must be held
1071 */
Jens Axboe165125e2007-07-24 09:28:11 +02001072void __blk_put_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 if (unlikely(!q))
1075 return;
1076 if (unlikely(--req->ref_count))
1077 return;
1078
Tejun Heo8922e162005-10-20 16:23:44 +02001079 elv_completed_request(q, req);
1080
Boaz Harrosh1cd96c22009-03-24 12:35:07 +01001081 /* this is a bio leak */
1082 WARN_ON(req->bio != NULL);
1083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 /*
1085 * Request may not have originated from ll_rw_blk. if not,
1086 * it didn't come out of our reserved rq pools
1087 */
Jens Axboe49171e52006-08-10 08:59:11 +02001088 if (req->cmd_flags & REQ_ALLOCED) {
Jens Axboe1faa16d2009-04-06 14:48:01 +02001089 int is_sync = rq_is_sync(req) != 0;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001090 int priv = req->cmd_flags & REQ_ELVPRIV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 BUG_ON(!list_empty(&req->queuelist));
Jens Axboe98170642006-07-28 09:23:08 +02001093 BUG_ON(!hlist_unhashed(&req->hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095 blk_free_request(q, req);
Jens Axboe1faa16d2009-04-06 14:48:01 +02001096 freed_request(q, is_sync, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 }
1098}
Mike Christie6e39b69e2005-11-11 05:30:24 -06001099EXPORT_SYMBOL_GPL(__blk_put_request);
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101void blk_put_request(struct request *req)
1102{
Tejun Heo8922e162005-10-20 16:23:44 +02001103 unsigned long flags;
Jens Axboe165125e2007-07-24 09:28:11 +02001104 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
FUJITA Tomonori52a93ba2008-07-15 21:21:45 +02001106 spin_lock_irqsave(q->queue_lock, flags);
1107 __blk_put_request(q, req);
1108 spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110EXPORT_SYMBOL(blk_put_request);
1111
Jens Axboe86db1e22008-01-29 14:53:40 +01001112void init_request_from_bio(struct request *req, struct bio *bio)
Tejun Heo52d9e672006-01-06 09:49:58 +01001113{
Jens Axboec7c22e42008-09-13 20:26:01 +02001114 req->cpu = bio->bi_comp_cpu;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001115 req->cmd_type = REQ_TYPE_FS;
Tejun Heo52d9e672006-01-06 09:49:58 +01001116
1117 /*
1118 * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
1119 */
Mike Christie6000a362008-08-19 18:45:30 -05001120 if (bio_rw_ahead(bio))
1121 req->cmd_flags |= (REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
1122 REQ_FAILFAST_DRIVER);
1123 if (bio_failfast_dev(bio))
1124 req->cmd_flags |= REQ_FAILFAST_DEV;
1125 if (bio_failfast_transport(bio))
1126 req->cmd_flags |= REQ_FAILFAST_TRANSPORT;
1127 if (bio_failfast_driver(bio))
1128 req->cmd_flags |= REQ_FAILFAST_DRIVER;
Tejun Heo52d9e672006-01-06 09:49:58 +01001129
David Woodhousefb2dce82008-08-05 18:01:53 +01001130 if (unlikely(bio_discard(bio))) {
David Woodhousee17fc0a2008-08-09 16:42:20 +01001131 req->cmd_flags |= REQ_DISCARD;
1132 if (bio_barrier(bio))
1133 req->cmd_flags |= REQ_SOFTBARRIER;
David Woodhousefb2dce82008-08-05 18:01:53 +01001134 req->q->prepare_discard_fn(req->q, req);
David Woodhousee17fc0a2008-08-09 16:42:20 +01001135 } else if (unlikely(bio_barrier(bio)))
Tejun Heoe4025f62009-04-23 11:05:17 +09001136 req->cmd_flags |= REQ_HARDBARRIER;
Tejun Heo52d9e672006-01-06 09:49:58 +01001137
Jens Axboeb31dc662006-06-13 08:26:10 +02001138 if (bio_sync(bio))
Jens Axboe4aff5e22006-08-10 08:44:47 +02001139 req->cmd_flags |= REQ_RW_SYNC;
Jens Axboe5404bc72006-08-10 09:01:02 +02001140 if (bio_rw_meta(bio))
1141 req->cmd_flags |= REQ_RW_META;
Jens Axboeaeb6faf2009-04-06 14:48:07 +02001142 if (bio_noidle(bio))
1143 req->cmd_flags |= REQ_NOIDLE;
Jens Axboeb31dc662006-06-13 08:26:10 +02001144
Tejun Heo52d9e672006-01-06 09:49:58 +01001145 req->errors = 0;
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001146 req->__sector = bio->bi_sector;
Tejun Heo52d9e672006-01-06 09:49:58 +01001147 req->ioprio = bio_prio(bio);
NeilBrownbc1c56f2007-08-16 13:31:30 +02001148 blk_rq_bio_prep(req->q, req, bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001149}
1150
Jens Axboe644b2d92009-04-06 14:48:06 +02001151/*
1152 * Only disabling plugging for non-rotational devices if it does tagging
1153 * as well, otherwise we do need the proper merging
1154 */
1155static inline bool queue_should_plug(struct request_queue *q)
1156{
1157 return !(blk_queue_nonrot(q) && blk_queue_tagged(q));
1158}
1159
Jens Axboe165125e2007-07-24 09:28:11 +02001160static int __make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
Nick Piggin450991b2005-06-28 20:45:13 -07001162 struct request *req;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001163 int el_ret;
1164 unsigned int bytes = bio->bi_size;
Jens Axboe51da90f2006-07-18 04:14:45 +02001165 const unsigned short prio = bio_prio(bio);
1166 const int sync = bio_sync(bio);
Jens Axboe213d9412009-01-06 09:16:05 +01001167 const int unplug = bio_unplug(bio);
Jens Axboe7749a8d2006-12-13 13:02:26 +01001168 int rw_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 /*
1171 * low level driver can indicate that it wants pages above a
1172 * certain limit bounced to low memory (ie for highmem, or even
1173 * ISA dma in theory)
1174 */
1175 blk_queue_bounce(q, &bio);
1176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 spin_lock_irq(q->queue_lock);
1178
Tejun Heoa7384672008-11-28 13:32:03 +09001179 if (unlikely(bio_barrier(bio)) || elv_queue_empty(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 goto get_rq;
1181
1182 el_ret = elv_merge(q, &req, bio);
1183 switch (el_ret) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001184 case ELEVATOR_BACK_MERGE:
1185 BUG_ON(!rq_mergeable(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Jens Axboe6728cb02008-01-31 13:03:55 +01001187 if (!ll_back_merge_fn(q, req, bio))
1188 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001190 trace_block_bio_backmerge(q, bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001191
Jens Axboe6728cb02008-01-31 13:03:55 +01001192 req->biotail->bi_next = bio;
1193 req->biotail = bio;
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001194 req->__data_len += bytes;
Jens Axboe6728cb02008-01-31 13:03:55 +01001195 req->ioprio = ioprio_best(req->ioprio, prio);
Jens Axboeab780f12008-08-26 10:25:02 +02001196 if (!blk_rq_cpu_valid(req))
1197 req->cpu = bio->bi_comp_cpu;
Jens Axboe6728cb02008-01-31 13:03:55 +01001198 drive_stat_acct(req, 0);
1199 if (!attempt_back_merge(q, req))
1200 elv_merged_request(q, req, el_ret);
1201 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Jens Axboe6728cb02008-01-31 13:03:55 +01001203 case ELEVATOR_FRONT_MERGE:
1204 BUG_ON(!rq_mergeable(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Jens Axboe6728cb02008-01-31 13:03:55 +01001206 if (!ll_front_merge_fn(q, req, bio))
1207 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001209 trace_block_bio_frontmerge(q, bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001210
Jens Axboe6728cb02008-01-31 13:03:55 +01001211 bio->bi_next = req->bio;
1212 req->bio = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Jens Axboe6728cb02008-01-31 13:03:55 +01001214 /*
1215 * may not be valid. if the low level driver said
1216 * it didn't need a bounce buffer then it better
1217 * not touch req->buffer either...
1218 */
1219 req->buffer = bio_data(bio);
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001220 req->__sector = bio->bi_sector;
1221 req->__data_len += bytes;
Jens Axboe6728cb02008-01-31 13:03:55 +01001222 req->ioprio = ioprio_best(req->ioprio, prio);
Jens Axboeab780f12008-08-26 10:25:02 +02001223 if (!blk_rq_cpu_valid(req))
1224 req->cpu = bio->bi_comp_cpu;
Jens Axboe6728cb02008-01-31 13:03:55 +01001225 drive_stat_acct(req, 0);
1226 if (!attempt_front_merge(q, req))
1227 elv_merged_request(q, req, el_ret);
1228 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Jens Axboe6728cb02008-01-31 13:03:55 +01001230 /* ELV_NO_MERGE: elevator says don't/can't merge. */
1231 default:
1232 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
1234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235get_rq:
Nick Piggin450991b2005-06-28 20:45:13 -07001236 /*
Jens Axboe7749a8d2006-12-13 13:02:26 +01001237 * This sync check and mask will be re-done in init_request_from_bio(),
1238 * but we need to set it earlier to expose the sync flag to the
1239 * rq allocator and io schedulers.
1240 */
1241 rw_flags = bio_data_dir(bio);
1242 if (sync)
1243 rw_flags |= REQ_RW_SYNC;
1244
1245 /*
Nick Piggin450991b2005-06-28 20:45:13 -07001246 * Grab a free request. This is might sleep but can not fail.
Nick Piggind6344532005-06-28 20:45:14 -07001247 * Returns with the queue unlocked.
Nick Piggin450991b2005-06-28 20:45:13 -07001248 */
Jens Axboe7749a8d2006-12-13 13:02:26 +01001249 req = get_request_wait(q, rw_flags, bio);
Nick Piggind6344532005-06-28 20:45:14 -07001250
Nick Piggin450991b2005-06-28 20:45:13 -07001251 /*
1252 * After dropping the lock and possibly sleeping here, our request
1253 * may now be mergeable after it had proven unmergeable (above).
1254 * We don't worry about that case for efficiency. It won't happen
1255 * often, and the elevators are able to handle it.
1256 */
Tejun Heo52d9e672006-01-06 09:49:58 +01001257 init_request_from_bio(req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Nick Piggin450991b2005-06-28 20:45:13 -07001259 spin_lock_irq(q->queue_lock);
Jens Axboec7c22e42008-09-13 20:26:01 +02001260 if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags) ||
1261 bio_flagged(bio, BIO_CPU_AFFINE))
1262 req->cpu = blk_cpu_to_group(smp_processor_id());
Jens Axboe644b2d92009-04-06 14:48:06 +02001263 if (queue_should_plug(q) && elv_queue_empty(q))
Nick Piggin450991b2005-06-28 20:45:13 -07001264 blk_plug_device(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 add_request(q, req);
1266out:
Jens Axboe644b2d92009-04-06 14:48:06 +02001267 if (unplug || !queue_should_plug(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 __generic_unplug_device(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 spin_unlock_irq(q->queue_lock);
1270 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
1273/*
1274 * If bio->bi_dev is a partition, remap the location
1275 */
1276static inline void blk_partition_remap(struct bio *bio)
1277{
1278 struct block_device *bdev = bio->bi_bdev;
1279
Jens Axboebf2de6f2007-09-27 13:01:25 +02001280 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 struct hd_struct *p = bdev->bd_part;
Jens Axboea3623572005-11-01 09:26:16 +01001282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 bio->bi_sector += p->start_sect;
1284 bio->bi_bdev = bdev->bd_contains;
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001285
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001286 trace_block_remap(bdev_get_queue(bio->bi_bdev), bio,
Alan D. Brunelle22a7c312009-05-04 16:35:08 -04001287 bdev->bd_dev,
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001288 bio->bi_sector - p->start_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
1290}
1291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292static void handle_bad_sector(struct bio *bio)
1293{
1294 char b[BDEVNAME_SIZE];
1295
1296 printk(KERN_INFO "attempt to access beyond end of device\n");
1297 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1298 bdevname(bio->bi_bdev, b),
1299 bio->bi_rw,
1300 (unsigned long long)bio->bi_sector + bio_sectors(bio),
1301 (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
1302
1303 set_bit(BIO_EOF, &bio->bi_flags);
1304}
1305
Akinobu Mitac17bb492006-12-08 02:39:46 -08001306#ifdef CONFIG_FAIL_MAKE_REQUEST
1307
1308static DECLARE_FAULT_ATTR(fail_make_request);
1309
1310static int __init setup_fail_make_request(char *str)
1311{
1312 return setup_fault_attr(&fail_make_request, str);
1313}
1314__setup("fail_make_request=", setup_fail_make_request);
1315
1316static int should_fail_request(struct bio *bio)
1317{
Tejun Heoeddb2e22008-08-25 19:56:13 +09001318 struct hd_struct *part = bio->bi_bdev->bd_part;
1319
1320 if (part_to_disk(part)->part0.make_it_fail || part->make_it_fail)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001321 return should_fail(&fail_make_request, bio->bi_size);
1322
1323 return 0;
1324}
1325
1326static int __init fail_make_request_debugfs(void)
1327{
1328 return init_fault_attr_dentries(&fail_make_request,
1329 "fail_make_request");
1330}
1331
1332late_initcall(fail_make_request_debugfs);
1333
1334#else /* CONFIG_FAIL_MAKE_REQUEST */
1335
1336static inline int should_fail_request(struct bio *bio)
1337{
1338 return 0;
1339}
1340
1341#endif /* CONFIG_FAIL_MAKE_REQUEST */
1342
Jens Axboec07e2b42007-07-18 13:27:58 +02001343/*
1344 * Check whether this bio extends beyond the end of the device.
1345 */
1346static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1347{
1348 sector_t maxsector;
1349
1350 if (!nr_sectors)
1351 return 0;
1352
1353 /* Test device or partition size, when known. */
1354 maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
1355 if (maxsector) {
1356 sector_t sector = bio->bi_sector;
1357
1358 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1359 /*
1360 * This may well happen - the kernel calls bread()
1361 * without checking the size of the device, e.g., when
1362 * mounting a device.
1363 */
1364 handle_bad_sector(bio);
1365 return 1;
1366 }
1367 }
1368
1369 return 0;
1370}
1371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001373 * generic_make_request - hand a buffer to its device driver for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 * @bio: The bio describing the location in memory and on the device.
1375 *
1376 * generic_make_request() is used to make I/O requests of block
1377 * devices. It is passed a &struct bio, which describes the I/O that needs
1378 * to be done.
1379 *
1380 * generic_make_request() does not return any status. The
1381 * success/failure status of the request, along with notification of
1382 * completion, is delivered asynchronously through the bio->bi_end_io
1383 * function described (one day) else where.
1384 *
1385 * The caller of generic_make_request must make sure that bi_io_vec
1386 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1387 * set to describe the device address, and the
1388 * bi_end_io and optionally bi_private are set to describe how
1389 * completion notification should be signaled.
1390 *
1391 * generic_make_request and the drivers it calls may use bi_next if this
1392 * bio happens to be merged with someone else, and may change bi_dev and
1393 * bi_sector for remaps as it sees fit. So the values of these fields
1394 * should NOT be depended on after the call to generic_make_request.
1395 */
Neil Brownd89d8792007-05-01 09:53:42 +02001396static inline void __generic_make_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
Jens Axboe165125e2007-07-24 09:28:11 +02001398 struct request_queue *q;
NeilBrown5ddfe962006-10-30 22:07:21 -08001399 sector_t old_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 int ret, nr_sectors = bio_sectors(bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001401 dev_t old_dev;
Jens Axboe51fd77b2007-11-02 08:49:08 +01001402 int err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Jens Axboec07e2b42007-07-18 13:27:58 +02001406 if (bio_check_eod(bio, nr_sectors))
1407 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 /*
1410 * Resolve the mapping until finished. (drivers are
1411 * still free to implement/resolve their own stacking
1412 * by explicitly returning 0)
1413 *
1414 * NOTE: we don't repeat the blk_size check for each new device.
1415 * Stacking drivers are expected to know what they are doing.
1416 */
NeilBrown5ddfe962006-10-30 22:07:21 -08001417 old_sector = -1;
Jens Axboe2056a782006-03-23 20:00:26 +01001418 old_dev = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 do {
1420 char b[BDEVNAME_SIZE];
1421
1422 q = bdev_get_queue(bio->bi_bdev);
Tejun Heoa7384672008-11-28 13:32:03 +09001423 if (unlikely(!q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 printk(KERN_ERR
1425 "generic_make_request: Trying to access "
1426 "nonexistent block-device %s (%Lu)\n",
1427 bdevname(bio->bi_bdev, b),
1428 (long long) bio->bi_sector);
Tejun Heoa7384672008-11-28 13:32:03 +09001429 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 }
1431
Martin K. Petersenae03bf62009-05-22 17:17:50 -04001432 if (unlikely(nr_sectors > queue_max_hw_sectors(q))) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001433 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
Martin K. Petersenae03bf62009-05-22 17:17:50 -04001434 bdevname(bio->bi_bdev, b),
1435 bio_sectors(bio),
1436 queue_max_hw_sectors(q));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 goto end_io;
1438 }
1439
Nick Pigginfde6ad22005-06-23 00:08:53 -07001440 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 goto end_io;
1442
Akinobu Mitac17bb492006-12-08 02:39:46 -08001443 if (should_fail_request(bio))
1444 goto end_io;
1445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 /*
1447 * If this device has partitions, remap block n
1448 * of partition p to block n+start(p) of the disk.
1449 */
1450 blk_partition_remap(bio);
1451
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +02001452 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio))
1453 goto end_io;
1454
NeilBrown5ddfe962006-10-30 22:07:21 -08001455 if (old_sector != -1)
Alan D. Brunelle22a7c312009-05-04 16:35:08 -04001456 trace_block_remap(q, bio, old_dev, old_sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001457
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001458 trace_block_bio_queue(q, bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001459
NeilBrown5ddfe962006-10-30 22:07:21 -08001460 old_sector = bio->bi_sector;
Jens Axboe2056a782006-03-23 20:00:26 +01001461 old_dev = bio->bi_bdev->bd_dev;
1462
Jens Axboec07e2b42007-07-18 13:27:58 +02001463 if (bio_check_eod(bio, nr_sectors))
1464 goto end_io;
Tejun Heoa7384672008-11-28 13:32:03 +09001465
1466 if (bio_discard(bio) && !q->prepare_discard_fn) {
Jens Axboe51fd77b2007-11-02 08:49:08 +01001467 err = -EOPNOTSUPP;
1468 goto end_io;
1469 }
Jens Axboecec07072009-01-13 15:28:32 +01001470 if (bio_barrier(bio) && bio_has_data(bio) &&
1471 (q->next_ordered == QUEUE_ORDERED_NONE)) {
1472 err = -EOPNOTSUPP;
1473 goto end_io;
1474 }
NeilBrown5ddfe962006-10-30 22:07:21 -08001475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 ret = q->make_request_fn(q, bio);
1477 } while (ret);
Tejun Heoa7384672008-11-28 13:32:03 +09001478
1479 return;
1480
1481end_io:
1482 bio_endio(bio, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483}
1484
Neil Brownd89d8792007-05-01 09:53:42 +02001485/*
1486 * We only want one ->make_request_fn to be active at a time,
1487 * else stack usage with stacked devices could be a problem.
1488 * So use current->bio_{list,tail} to keep a list of requests
1489 * submited by a make_request_fn function.
1490 * current->bio_tail is also used as a flag to say if
1491 * generic_make_request is currently active in this task or not.
1492 * If it is NULL, then no make_request is active. If it is non-NULL,
1493 * then a make_request is active, and new requests should be added
1494 * at the tail
1495 */
1496void generic_make_request(struct bio *bio)
1497{
1498 if (current->bio_tail) {
1499 /* make_request is active */
1500 *(current->bio_tail) = bio;
1501 bio->bi_next = NULL;
1502 current->bio_tail = &bio->bi_next;
1503 return;
1504 }
1505 /* following loop may be a bit non-obvious, and so deserves some
1506 * explanation.
1507 * Before entering the loop, bio->bi_next is NULL (as all callers
1508 * ensure that) so we have a list with a single bio.
1509 * We pretend that we have just taken it off a longer list, so
1510 * we assign bio_list to the next (which is NULL) and bio_tail
1511 * to &bio_list, thus initialising the bio_list of new bios to be
1512 * added. __generic_make_request may indeed add some more bios
1513 * through a recursive call to generic_make_request. If it
1514 * did, we find a non-NULL value in bio_list and re-enter the loop
1515 * from the top. In this case we really did just take the bio
1516 * of the top of the list (no pretending) and so fixup bio_list and
1517 * bio_tail or bi_next, and call into __generic_make_request again.
1518 *
1519 * The loop was structured like this to make only one call to
1520 * __generic_make_request (which is important as it is large and
1521 * inlined) and to keep the structure simple.
1522 */
1523 BUG_ON(bio->bi_next);
1524 do {
1525 current->bio_list = bio->bi_next;
1526 if (bio->bi_next == NULL)
1527 current->bio_tail = &current->bio_list;
1528 else
1529 bio->bi_next = NULL;
1530 __generic_make_request(bio);
1531 bio = current->bio_list;
1532 } while (bio);
1533 current->bio_tail = NULL; /* deactivate */
1534}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535EXPORT_SYMBOL(generic_make_request);
1536
1537/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001538 * submit_bio - submit a bio to the block device layer for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1540 * @bio: The &struct bio which describes the I/O
1541 *
1542 * submit_bio() is very similar in purpose to generic_make_request(), and
1543 * uses that function to do most of the work. Both are fairly rough
Randy Dunlap710027a2008-08-19 20:13:11 +02001544 * interfaces; @bio must be presetup and ready for I/O.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 *
1546 */
1547void submit_bio(int rw, struct bio *bio)
1548{
1549 int count = bio_sectors(bio);
1550
Jens Axboe22e2c502005-06-27 10:55:12 +02001551 bio->bi_rw |= rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Jens Axboebf2de6f2007-09-27 13:01:25 +02001553 /*
1554 * If it's a regular read/write or a barrier with data attached,
1555 * go through the normal accounting stuff before submission.
1556 */
Jens Axboea9c701e2008-08-08 11:04:44 +02001557 if (bio_has_data(bio)) {
Jens Axboebf2de6f2007-09-27 13:01:25 +02001558 if (rw & WRITE) {
1559 count_vm_events(PGPGOUT, count);
1560 } else {
1561 task_io_account_read(bio->bi_size);
1562 count_vm_events(PGPGIN, count);
1563 }
1564
1565 if (unlikely(block_dump)) {
1566 char b[BDEVNAME_SIZE];
1567 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001568 current->comm, task_pid_nr(current),
Jens Axboebf2de6f2007-09-27 13:01:25 +02001569 (rw & WRITE) ? "WRITE" : "READ",
1570 (unsigned long long)bio->bi_sector,
Jens Axboe6728cb02008-01-31 13:03:55 +01001571 bdevname(bio->bi_bdev, b));
Jens Axboebf2de6f2007-09-27 13:01:25 +02001572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
1574
1575 generic_make_request(bio);
1576}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577EXPORT_SYMBOL(submit_bio);
1578
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001579/**
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001580 * blk_rq_check_limits - Helper function to check a request for the queue limit
1581 * @q: the queue
1582 * @rq: the request being checked
1583 *
1584 * Description:
1585 * @rq may have been made based on weaker limitations of upper-level queues
1586 * in request stacking drivers, and it may violate the limitation of @q.
1587 * Since the block layer and the underlying device driver trust @rq
1588 * after it is inserted to @q, it should be checked against @q before
1589 * the insertion using this generic function.
1590 *
1591 * This function should also be useful for request stacking drivers
1592 * in some cases below, so export this fuction.
1593 * Request stacking drivers like request-based dm may change the queue
1594 * limits while requests are in the queue (e.g. dm's table swapping).
1595 * Such request stacking drivers should check those requests agaist
1596 * the new queue limits again when they dispatch those requests,
1597 * although such checkings are also done against the old queue limits
1598 * when submitting requests.
1599 */
1600int blk_rq_check_limits(struct request_queue *q, struct request *rq)
1601{
Martin K. Petersenae03bf62009-05-22 17:17:50 -04001602 if (blk_rq_sectors(rq) > queue_max_sectors(q) ||
1603 blk_rq_bytes(rq) > queue_max_hw_sectors(q) << 9) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001604 printk(KERN_ERR "%s: over max size limit.\n", __func__);
1605 return -EIO;
1606 }
1607
1608 /*
1609 * queue's settings related to segment counting like q->bounce_pfn
1610 * may differ from that of other stacking queues.
1611 * Recalculate it to check the request correctly on this queue's
1612 * limitation.
1613 */
1614 blk_recalc_rq_segments(rq);
Martin K. Petersenae03bf62009-05-22 17:17:50 -04001615 if (rq->nr_phys_segments > queue_max_phys_segments(q) ||
1616 rq->nr_phys_segments > queue_max_hw_segments(q)) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001617 printk(KERN_ERR "%s: over max segments limit.\n", __func__);
1618 return -EIO;
1619 }
1620
1621 return 0;
1622}
1623EXPORT_SYMBOL_GPL(blk_rq_check_limits);
1624
1625/**
1626 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
1627 * @q: the queue to submit the request
1628 * @rq: the request being queued
1629 */
1630int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
1631{
1632 unsigned long flags;
1633
1634 if (blk_rq_check_limits(q, rq))
1635 return -EIO;
1636
1637#ifdef CONFIG_FAIL_MAKE_REQUEST
1638 if (rq->rq_disk && rq->rq_disk->part0.make_it_fail &&
1639 should_fail(&fail_make_request, blk_rq_bytes(rq)))
1640 return -EIO;
1641#endif
1642
1643 spin_lock_irqsave(q->queue_lock, flags);
1644
1645 /*
1646 * Submitting request must be dequeued before calling this function
1647 * because it will be linked to another request_queue
1648 */
1649 BUG_ON(blk_queued_rq(rq));
1650
1651 drive_stat_acct(rq, 1);
1652 __elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 0);
1653
1654 spin_unlock_irqrestore(q->queue_lock, flags);
1655
1656 return 0;
1657}
1658EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
1659
Jens Axboebc58ba92009-01-23 10:54:44 +01001660static void blk_account_io_completion(struct request *req, unsigned int bytes)
1661{
Jens Axboec2553b52009-04-24 08:10:11 +02001662 if (blk_do_io_stat(req)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01001663 const int rw = rq_data_dir(req);
1664 struct hd_struct *part;
1665 int cpu;
1666
1667 cpu = part_stat_lock();
Tejun Heo83096eb2009-05-07 22:24:39 +09001668 part = disk_map_sector_rcu(req->rq_disk, blk_rq_pos(req));
Jens Axboebc58ba92009-01-23 10:54:44 +01001669 part_stat_add(cpu, part, sectors[rw], bytes >> 9);
1670 part_stat_unlock();
1671 }
1672}
1673
1674static void blk_account_io_done(struct request *req)
1675{
Jens Axboebc58ba92009-01-23 10:54:44 +01001676 /*
1677 * Account IO completion. bar_rq isn't accounted as a normal
1678 * IO on queueing nor completion. Accounting the containing
1679 * request is enough.
1680 */
Jens Axboec2553b52009-04-24 08:10:11 +02001681 if (blk_do_io_stat(req) && req != &req->q->bar_rq) {
Jens Axboebc58ba92009-01-23 10:54:44 +01001682 unsigned long duration = jiffies - req->start_time;
1683 const int rw = rq_data_dir(req);
1684 struct hd_struct *part;
1685 int cpu;
1686
1687 cpu = part_stat_lock();
Tejun Heo83096eb2009-05-07 22:24:39 +09001688 part = disk_map_sector_rcu(req->rq_disk, blk_rq_pos(req));
Jens Axboebc58ba92009-01-23 10:54:44 +01001689
1690 part_stat_inc(cpu, part, ios[rw]);
1691 part_stat_add(cpu, part, ticks[rw], duration);
1692 part_round_stats(cpu, part);
1693 part_dec_in_flight(part);
1694
1695 part_stat_unlock();
1696 }
1697}
1698
Tejun Heo53a08802008-12-03 12:41:26 +01001699/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09001700 * blk_peek_request - peek at the top of a request queue
1701 * @q: request queue to peek at
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001702 *
1703 * Description:
Tejun Heo9934c8c2009-05-08 11:54:16 +09001704 * Return the request at the top of @q. The returned request
1705 * should be started using blk_start_request() before LLD starts
1706 * processing it.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001707 *
1708 * Return:
Tejun Heo9934c8c2009-05-08 11:54:16 +09001709 * Pointer to the request at the top of @q if available. Null
1710 * otherwise.
1711 *
1712 * Context:
1713 * queue_lock must be held.
1714 */
1715struct request *blk_peek_request(struct request_queue *q)
Tejun Heo158dbda2009-04-23 11:05:18 +09001716{
1717 struct request *rq;
1718 int ret;
1719
1720 while ((rq = __elv_next_request(q)) != NULL) {
1721 if (!(rq->cmd_flags & REQ_STARTED)) {
1722 /*
1723 * This is the first time the device driver
1724 * sees this request (possibly after
1725 * requeueing). Notify IO scheduler.
1726 */
1727 if (blk_sorted_rq(rq))
1728 elv_activate_rq(q, rq);
1729
1730 /*
1731 * just mark as started even if we don't start
1732 * it, a request that has been delayed should
1733 * not be passed by new incoming requests
1734 */
1735 rq->cmd_flags |= REQ_STARTED;
1736 trace_block_rq_issue(q, rq);
1737 }
1738
1739 if (!q->boundary_rq || q->boundary_rq == rq) {
1740 q->end_sector = rq_end_sector(rq);
1741 q->boundary_rq = NULL;
1742 }
1743
1744 if (rq->cmd_flags & REQ_DONTPREP)
1745 break;
1746
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001747 if (q->dma_drain_size && blk_rq_bytes(rq)) {
Tejun Heo158dbda2009-04-23 11:05:18 +09001748 /*
1749 * make sure space for the drain appears we
1750 * know we can do this because max_hw_segments
1751 * has been adjusted to be one fewer than the
1752 * device can handle
1753 */
1754 rq->nr_phys_segments++;
1755 }
1756
1757 if (!q->prep_rq_fn)
1758 break;
1759
1760 ret = q->prep_rq_fn(q, rq);
1761 if (ret == BLKPREP_OK) {
1762 break;
1763 } else if (ret == BLKPREP_DEFER) {
1764 /*
1765 * the request may have been (partially) prepped.
1766 * we need to keep this request in the front to
1767 * avoid resource deadlock. REQ_STARTED will
1768 * prevent other fs requests from passing this one.
1769 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001770 if (q->dma_drain_size && blk_rq_bytes(rq) &&
Tejun Heo158dbda2009-04-23 11:05:18 +09001771 !(rq->cmd_flags & REQ_DONTPREP)) {
1772 /*
1773 * remove the space for the drain we added
1774 * so that we don't add it again
1775 */
1776 --rq->nr_phys_segments;
1777 }
1778
1779 rq = NULL;
1780 break;
1781 } else if (ret == BLKPREP_KILL) {
1782 rq->cmd_flags |= REQ_QUIET;
James Bottomleyc143dc92009-05-30 06:43:49 +02001783 /*
1784 * Mark this request as started so we don't trigger
1785 * any debug logic in the end I/O path.
1786 */
1787 blk_start_request(rq);
Tejun Heo40cbbb72009-04-23 11:05:19 +09001788 __blk_end_request_all(rq, -EIO);
Tejun Heo158dbda2009-04-23 11:05:18 +09001789 } else {
1790 printk(KERN_ERR "%s: bad return=%d\n", __func__, ret);
1791 break;
1792 }
1793 }
1794
1795 return rq;
1796}
Tejun Heo9934c8c2009-05-08 11:54:16 +09001797EXPORT_SYMBOL(blk_peek_request);
Tejun Heo158dbda2009-04-23 11:05:18 +09001798
Tejun Heo9934c8c2009-05-08 11:54:16 +09001799void blk_dequeue_request(struct request *rq)
Tejun Heo158dbda2009-04-23 11:05:18 +09001800{
Tejun Heo9934c8c2009-05-08 11:54:16 +09001801 struct request_queue *q = rq->q;
1802
Tejun Heo158dbda2009-04-23 11:05:18 +09001803 BUG_ON(list_empty(&rq->queuelist));
1804 BUG_ON(ELV_ON_HASH(rq));
1805
1806 list_del_init(&rq->queuelist);
1807
1808 /*
1809 * the time frame between a request being removed from the lists
1810 * and to it is freed is accounted as io that is in progress at
1811 * the driver side.
1812 */
1813 if (blk_account_rq(rq))
Jens Axboe0a7ae2f2009-05-20 08:54:31 +02001814 q->in_flight[rq_is_sync(rq)]++;
Tejun Heo158dbda2009-04-23 11:05:18 +09001815}
1816
Tejun Heo5efccd12009-04-23 11:05:18 +09001817/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09001818 * blk_start_request - start request processing on the driver
1819 * @req: request to dequeue
1820 *
1821 * Description:
1822 * Dequeue @req and start timeout timer on it. This hands off the
1823 * request to the driver.
1824 *
1825 * Block internal functions which don't want to start timer should
1826 * call blk_dequeue_request().
1827 *
1828 * Context:
1829 * queue_lock must be held.
1830 */
1831void blk_start_request(struct request *req)
1832{
1833 blk_dequeue_request(req);
1834
1835 /*
Tejun Heo5f49f632009-05-19 18:33:05 +09001836 * We are now handing the request to the hardware, initialize
1837 * resid_len to full count and add the timeout handler.
Tejun Heo9934c8c2009-05-08 11:54:16 +09001838 */
Tejun Heo5f49f632009-05-19 18:33:05 +09001839 req->resid_len = blk_rq_bytes(req);
FUJITA Tomonoridbb66c42009-06-09 05:47:10 +02001840 if (unlikely(blk_bidi_rq(req)))
1841 req->next_rq->resid_len = blk_rq_bytes(req->next_rq);
1842
Tejun Heo9934c8c2009-05-08 11:54:16 +09001843 blk_add_timer(req);
1844}
1845EXPORT_SYMBOL(blk_start_request);
1846
1847/**
1848 * blk_fetch_request - fetch a request from a request queue
1849 * @q: request queue to fetch a request from
1850 *
1851 * Description:
1852 * Return the request at the top of @q. The request is started on
1853 * return and LLD can start processing it immediately.
1854 *
1855 * Return:
1856 * Pointer to the request at the top of @q if available. Null
1857 * otherwise.
1858 *
1859 * Context:
1860 * queue_lock must be held.
1861 */
1862struct request *blk_fetch_request(struct request_queue *q)
1863{
1864 struct request *rq;
1865
1866 rq = blk_peek_request(q);
1867 if (rq)
1868 blk_start_request(rq);
1869 return rq;
1870}
1871EXPORT_SYMBOL(blk_fetch_request);
1872
1873/**
Tejun Heo2e60e022009-04-23 11:05:18 +09001874 * blk_update_request - Special helper function for request stacking drivers
1875 * @rq: the request being processed
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001876 * @error: %0 for success, < %0 for error
Tejun Heo2e60e022009-04-23 11:05:18 +09001877 * @nr_bytes: number of bytes to complete @rq
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001878 *
1879 * Description:
Tejun Heo2e60e022009-04-23 11:05:18 +09001880 * Ends I/O on a number of bytes attached to @rq, but doesn't complete
1881 * the request structure even if @rq doesn't have leftover.
1882 * If @rq has leftover, sets it up for the next range of segments.
1883 *
1884 * This special helper function is only for request stacking drivers
1885 * (e.g. request-based dm) so that they can handle partial completion.
1886 * Actual device drivers should use blk_end_request instead.
1887 *
1888 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
1889 * %false return from this function.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001890 *
1891 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09001892 * %false - this request doesn't have any more data
1893 * %true - this request has more data
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001894 **/
Tejun Heo2e60e022009-04-23 11:05:18 +09001895bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896{
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001897 int total_bytes, bio_nbytes, next_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 struct bio *bio;
1899
Tejun Heo2e60e022009-04-23 11:05:18 +09001900 if (!req->bio)
1901 return false;
1902
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001903 trace_block_rq_complete(req->q, req);
Jens Axboe2056a782006-03-23 20:00:26 +01001904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 /*
Tejun Heo6f414692009-04-19 07:00:41 +09001906 * For fs requests, rq is just carrier of independent bio's
1907 * and each partial completion should be handled separately.
1908 * Reset per-request error on each partial completion.
1909 *
1910 * TODO: tj: This is too subtle. It would be better to let
1911 * low level drivers do what they see fit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 */
Tejun Heo6f414692009-04-19 07:00:41 +09001913 if (blk_fs_request(req))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 req->errors = 0;
1915
Jens Axboe6728cb02008-01-31 13:03:55 +01001916 if (error && (blk_fs_request(req) && !(req->cmd_flags & REQ_QUIET))) {
1917 printk(KERN_ERR "end_request: I/O error, dev %s, sector %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 req->rq_disk ? req->rq_disk->disk_name : "?",
Tejun Heo83096eb2009-05-07 22:24:39 +09001919 (unsigned long long)blk_rq_pos(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 }
1921
Jens Axboebc58ba92009-01-23 10:54:44 +01001922 blk_account_io_completion(req, nr_bytes);
Jens Axboed72d9042005-11-01 08:35:42 +01001923
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 total_bytes = bio_nbytes = 0;
1925 while ((bio = req->bio) != NULL) {
1926 int nbytes;
1927
1928 if (nr_bytes >= bio->bi_size) {
1929 req->bio = bio->bi_next;
1930 nbytes = bio->bi_size;
NeilBrown5bb23a62007-09-27 12:46:13 +02001931 req_bio_endio(req, bio, nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 next_idx = 0;
1933 bio_nbytes = 0;
1934 } else {
1935 int idx = bio->bi_idx + next_idx;
1936
Kazuhisa Ichikawaaf498d72009-05-12 13:27:45 +02001937 if (unlikely(idx >= bio->bi_vcnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 blk_dump_rq_flags(req, "__end_that");
Jens Axboe6728cb02008-01-31 13:03:55 +01001939 printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
Kazuhisa Ichikawaaf498d72009-05-12 13:27:45 +02001940 __func__, idx, bio->bi_vcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 break;
1942 }
1943
1944 nbytes = bio_iovec_idx(bio, idx)->bv_len;
1945 BIO_BUG_ON(nbytes > bio->bi_size);
1946
1947 /*
1948 * not a complete bvec done
1949 */
1950 if (unlikely(nbytes > nr_bytes)) {
1951 bio_nbytes += nr_bytes;
1952 total_bytes += nr_bytes;
1953 break;
1954 }
1955
1956 /*
1957 * advance to the next vector
1958 */
1959 next_idx++;
1960 bio_nbytes += nbytes;
1961 }
1962
1963 total_bytes += nbytes;
1964 nr_bytes -= nbytes;
1965
Jens Axboe6728cb02008-01-31 13:03:55 +01001966 bio = req->bio;
1967 if (bio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 /*
1969 * end more in this run, or just return 'not-done'
1970 */
1971 if (unlikely(nr_bytes <= 0))
1972 break;
1973 }
1974 }
1975
1976 /*
1977 * completely done
1978 */
Tejun Heo2e60e022009-04-23 11:05:18 +09001979 if (!req->bio) {
1980 /*
1981 * Reset counters so that the request stacking driver
1982 * can find how many bytes remain in the request
1983 * later.
1984 */
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001985 req->__data_len = 0;
Tejun Heo2e60e022009-04-23 11:05:18 +09001986 return false;
1987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
1989 /*
1990 * if the request wasn't completed, update state
1991 */
1992 if (bio_nbytes) {
NeilBrown5bb23a62007-09-27 12:46:13 +02001993 req_bio_endio(req, bio, bio_nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 bio->bi_idx += next_idx;
1995 bio_iovec(bio)->bv_offset += nr_bytes;
1996 bio_iovec(bio)->bv_len -= nr_bytes;
1997 }
1998
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001999 req->__data_len -= total_bytes;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002000 req->buffer = bio_data(req->bio);
2001
2002 /* update sector only for requests with clear definition of sector */
2003 if (blk_fs_request(req) || blk_discard_rq(req))
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002004 req->__sector += total_bytes >> 9;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002005
2006 /*
2007 * If total number of sectors is less than the first segment
2008 * size, something has gone terribly wrong.
2009 */
2010 if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
2011 printk(KERN_ERR "blk: request botched\n");
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002012 req->__data_len = blk_rq_cur_bytes(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002013 }
2014
2015 /* recalculate the number of segments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 blk_recalc_rq_segments(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002017
Tejun Heo2e60e022009-04-23 11:05:18 +09002018 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019}
Tejun Heo2e60e022009-04-23 11:05:18 +09002020EXPORT_SYMBOL_GPL(blk_update_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
Tejun Heo2e60e022009-04-23 11:05:18 +09002022static bool blk_update_bidi_request(struct request *rq, int error,
2023 unsigned int nr_bytes,
2024 unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002025{
Tejun Heo2e60e022009-04-23 11:05:18 +09002026 if (blk_update_request(rq, error, nr_bytes))
2027 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002028
Tejun Heo2e60e022009-04-23 11:05:18 +09002029 /* Bidi request must be completed as a whole */
2030 if (unlikely(blk_bidi_rq(rq)) &&
2031 blk_update_request(rq->next_rq, error, bidi_bytes))
2032 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002033
Tejun Heo2e60e022009-04-23 11:05:18 +09002034 add_disk_randomness(rq->rq_disk);
2035
2036 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037}
2038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039/*
2040 * queue lock must be held
2041 */
Tejun Heo2e60e022009-04-23 11:05:18 +09002042static void blk_finish_request(struct request *req, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002044 if (blk_rq_tagged(req))
2045 blk_queue_end_tag(req->q, req);
2046
James Bottomleyba396a62009-05-27 14:17:08 +02002047 BUG_ON(blk_queued_rq(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
2049 if (unlikely(laptop_mode) && blk_fs_request(req))
2050 laptop_io_completion();
2051
Mike Andersone78042e2008-10-30 02:16:20 -07002052 blk_delete_timer(req);
2053
Jens Axboebc58ba92009-01-23 10:54:44 +01002054 blk_account_io_done(req);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 if (req->end_io)
Tejun Heo8ffdc652006-01-06 09:49:03 +01002057 req->end_io(req, error);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002058 else {
2059 if (blk_bidi_rq(req))
2060 __blk_put_request(req->next_rq->q, req->next_rq);
2061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 __blk_put_request(req->q, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 }
2064}
2065
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05002066/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002067 * blk_end_bidi_request - Complete a bidi request
2068 * @rq: the request to complete
Randy Dunlap710027a2008-08-19 20:13:11 +02002069 * @error: %0 for success, < %0 for error
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002070 * @nr_bytes: number of bytes to complete @rq
2071 * @bidi_bytes: number of bytes to complete @rq->next_rq
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002072 *
2073 * Description:
2074 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
Tejun Heo2e60e022009-04-23 11:05:18 +09002075 * Drivers that supports bidi can safely call this member for any
2076 * type of request, bidi or uni. In the later case @bidi_bytes is
2077 * just ignored.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002078 *
2079 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002080 * %false - we are done with this request
2081 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002082 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002083static bool blk_end_bidi_request(struct request *rq, int error,
2084 unsigned int nr_bytes, unsigned int bidi_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002085{
2086 struct request_queue *q = rq->q;
Tejun Heo2e60e022009-04-23 11:05:18 +09002087 unsigned long flags;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002088
Tejun Heo2e60e022009-04-23 11:05:18 +09002089 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2090 return true;
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002091
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002092 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo2e60e022009-04-23 11:05:18 +09002093 blk_finish_request(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002094 spin_unlock_irqrestore(q->queue_lock, flags);
2095
Tejun Heo2e60e022009-04-23 11:05:18 +09002096 return false;
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002097}
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002098
2099/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002100 * __blk_end_bidi_request - Complete a bidi request with queue lock held
2101 * @rq: the request to complete
2102 * @error: %0 for success, < %0 for error
2103 * @nr_bytes: number of bytes to complete @rq
2104 * @bidi_bytes: number of bytes to complete @rq->next_rq
Tejun Heo5efccd12009-04-23 11:05:18 +09002105 *
2106 * Description:
Tejun Heo2e60e022009-04-23 11:05:18 +09002107 * Identical to blk_end_bidi_request() except that queue lock is
2108 * assumed to be locked on entry and remains so on return.
Tejun Heo5efccd12009-04-23 11:05:18 +09002109 *
Tejun Heo2e60e022009-04-23 11:05:18 +09002110 * Return:
2111 * %false - we are done with this request
2112 * %true - still buffers pending for this request
Tejun Heo5efccd12009-04-23 11:05:18 +09002113 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002114static bool __blk_end_bidi_request(struct request *rq, int error,
2115 unsigned int nr_bytes, unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002116{
Tejun Heo2e60e022009-04-23 11:05:18 +09002117 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2118 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002119
Tejun Heo2e60e022009-04-23 11:05:18 +09002120 blk_finish_request(rq, error);
Tejun Heo5efccd12009-04-23 11:05:18 +09002121
Tejun Heo2e60e022009-04-23 11:05:18 +09002122 return false;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002123}
2124
2125/**
2126 * blk_end_request - Helper function for drivers to complete the request.
2127 * @rq: the request being processed
2128 * @error: %0 for success, < %0 for error
2129 * @nr_bytes: number of bytes to complete
2130 *
2131 * Description:
2132 * Ends I/O on a number of bytes attached to @rq.
2133 * If @rq has leftover, sets it up for the next range of segments.
2134 *
2135 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002136 * %false - we are done with this request
2137 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002138 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002139bool blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002140{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002141 return blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002142}
2143EXPORT_SYMBOL_GPL(blk_end_request);
2144
2145/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002146 * blk_end_request_all - Helper function for drives to finish the request.
2147 * @rq: the request to finish
2148 * @err: %0 for success, < %0 for error
2149 *
2150 * Description:
2151 * Completely finish @rq.
2152 */
2153void blk_end_request_all(struct request *rq, int error)
2154{
2155 bool pending;
2156 unsigned int bidi_bytes = 0;
2157
2158 if (unlikely(blk_bidi_rq(rq)))
2159 bidi_bytes = blk_rq_bytes(rq->next_rq);
2160
2161 pending = blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2162 BUG_ON(pending);
2163}
2164EXPORT_SYMBOL_GPL(blk_end_request_all);
2165
2166/**
2167 * blk_end_request_cur - Helper function to finish the current request chunk.
2168 * @rq: the request to finish the current chunk for
2169 * @err: %0 for success, < %0 for error
2170 *
2171 * Description:
2172 * Complete the current consecutively mapped chunk from @rq.
2173 *
2174 * Return:
2175 * %false - we are done with this request
2176 * %true - still buffers pending for this request
2177 */
2178bool blk_end_request_cur(struct request *rq, int error)
2179{
2180 return blk_end_request(rq, error, blk_rq_cur_bytes(rq));
2181}
2182EXPORT_SYMBOL_GPL(blk_end_request_cur);
2183
2184/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002185 * __blk_end_request - Helper function for drivers to complete the request.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002186 * @rq: the request being processed
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002187 * @error: %0 for success, < %0 for error
2188 * @nr_bytes: number of bytes to complete
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002189 *
2190 * Description:
2191 * Must be called with queue lock held unlike blk_end_request().
2192 *
2193 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002194 * %false - we are done with this request
2195 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002196 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002197bool __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002198{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002199 return __blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002200}
2201EXPORT_SYMBOL_GPL(__blk_end_request);
2202
2203/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002204 * __blk_end_request_all - Helper function for drives to finish the request.
2205 * @rq: the request to finish
2206 * @err: %0 for success, < %0 for error
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002207 *
2208 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002209 * Completely finish @rq. Must be called with queue lock held.
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002210 */
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002211void __blk_end_request_all(struct request *rq, int error)
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002212{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002213 bool pending;
2214 unsigned int bidi_bytes = 0;
2215
2216 if (unlikely(blk_bidi_rq(rq)))
2217 bidi_bytes = blk_rq_bytes(rq->next_rq);
2218
2219 pending = __blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2220 BUG_ON(pending);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002221}
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002222EXPORT_SYMBOL_GPL(__blk_end_request_all);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002223
2224/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002225 * __blk_end_request_cur - Helper function to finish the current request chunk.
2226 * @rq: the request to finish the current chunk for
2227 * @err: %0 for success, < %0 for error
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002228 *
2229 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002230 * Complete the current consecutively mapped chunk from @rq. Must
2231 * be called with queue lock held.
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002232 *
2233 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002234 * %false - we are done with this request
2235 * %true - still buffers pending for this request
2236 */
2237bool __blk_end_request_cur(struct request *rq, int error)
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002238{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002239 return __blk_end_request(rq, error, blk_rq_cur_bytes(rq));
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002240}
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002241EXPORT_SYMBOL_GPL(__blk_end_request_cur);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002242
Jens Axboe86db1e22008-01-29 14:53:40 +01002243void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2244 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
David Woodhoused628eae2008-08-09 16:22:17 +01002246 /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw, and
2247 we want BIO_RW_AHEAD (bit 1) to imply REQ_FAILFAST (bit 1). */
Jens Axboe4aff5e22006-08-10 08:44:47 +02002248 rq->cmd_flags |= (bio->bi_rw & 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
David Woodhousefb2dce82008-08-05 18:01:53 +01002250 if (bio_has_data(bio)) {
2251 rq->nr_phys_segments = bio_phys_segments(q, bio);
David Woodhousefb2dce82008-08-05 18:01:53 +01002252 rq->buffer = bio_data(bio);
2253 }
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002254 rq->__data_len = bio->bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 rq->bio = rq->biotail = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
NeilBrown66846572007-08-16 13:31:28 +02002257 if (bio->bi_bdev)
2258 rq->rq_disk = bio->bi_bdev->bd_disk;
2259}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
Kiyoshi Uedaef9e3fa2008-10-01 16:12:15 +02002261/**
2262 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
2263 * @q : the queue of the device being checked
2264 *
2265 * Description:
2266 * Check if underlying low-level drivers of a device are busy.
2267 * If the drivers want to export their busy state, they must set own
2268 * exporting function using blk_queue_lld_busy() first.
2269 *
2270 * Basically, this function is used only by request stacking drivers
2271 * to stop dispatching requests to underlying devices when underlying
2272 * devices are busy. This behavior helps more I/O merging on the queue
2273 * of the request stacking driver and prevents I/O throughput regression
2274 * on burst I/O load.
2275 *
2276 * Return:
2277 * 0 - Not busy (The request stacking driver should dispatch request)
2278 * 1 - Busy (The request stacking driver should stop dispatching request)
2279 */
2280int blk_lld_busy(struct request_queue *q)
2281{
2282 if (q->lld_busy_fn)
2283 return q->lld_busy_fn(q);
2284
2285 return 0;
2286}
2287EXPORT_SYMBOL_GPL(blk_lld_busy);
2288
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002289/**
2290 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
2291 * @rq: the clone request to be cleaned up
2292 *
2293 * Description:
2294 * Free all bios in @rq for a cloned request.
2295 */
2296void blk_rq_unprep_clone(struct request *rq)
2297{
2298 struct bio *bio;
2299
2300 while ((bio = rq->bio) != NULL) {
2301 rq->bio = bio->bi_next;
2302
2303 bio_put(bio);
2304 }
2305}
2306EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
2307
2308/*
2309 * Copy attributes of the original request to the clone request.
2310 * The actual data parts (e.g. ->cmd, ->buffer, ->sense) are not copied.
2311 */
2312static void __blk_rq_prep_clone(struct request *dst, struct request *src)
2313{
2314 dst->cpu = src->cpu;
2315 dst->cmd_flags = (rq_data_dir(src) | REQ_NOMERGE);
2316 dst->cmd_type = src->cmd_type;
2317 dst->__sector = blk_rq_pos(src);
2318 dst->__data_len = blk_rq_bytes(src);
2319 dst->nr_phys_segments = src->nr_phys_segments;
2320 dst->ioprio = src->ioprio;
2321 dst->extra_len = src->extra_len;
2322}
2323
2324/**
2325 * blk_rq_prep_clone - Helper function to setup clone request
2326 * @rq: the request to be setup
2327 * @rq_src: original request to be cloned
2328 * @bs: bio_set that bios for clone are allocated from
2329 * @gfp_mask: memory allocation mask for bio
2330 * @bio_ctr: setup function to be called for each clone bio.
2331 * Returns %0 for success, non %0 for failure.
2332 * @data: private data to be passed to @bio_ctr
2333 *
2334 * Description:
2335 * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
2336 * The actual data parts of @rq_src (e.g. ->cmd, ->buffer, ->sense)
2337 * are not copied, and copying such parts is the caller's responsibility.
2338 * Also, pages which the original bios are pointing to are not copied
2339 * and the cloned bios just point same pages.
2340 * So cloned bios must be completed before original bios, which means
2341 * the caller must complete @rq before @rq_src.
2342 */
2343int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
2344 struct bio_set *bs, gfp_t gfp_mask,
2345 int (*bio_ctr)(struct bio *, struct bio *, void *),
2346 void *data)
2347{
2348 struct bio *bio, *bio_src;
2349
2350 if (!bs)
2351 bs = fs_bio_set;
2352
2353 blk_rq_init(NULL, rq);
2354
2355 __rq_for_each_bio(bio_src, rq_src) {
2356 bio = bio_alloc_bioset(gfp_mask, bio_src->bi_max_vecs, bs);
2357 if (!bio)
2358 goto free_and_out;
2359
2360 __bio_clone(bio, bio_src);
2361
2362 if (bio_integrity(bio_src) &&
2363 bio_integrity_clone(bio, bio_src, gfp_mask))
2364 goto free_and_out;
2365
2366 if (bio_ctr && bio_ctr(bio, bio_src, data))
2367 goto free_and_out;
2368
2369 if (rq->bio) {
2370 rq->biotail->bi_next = bio;
2371 rq->biotail = bio;
2372 } else
2373 rq->bio = rq->biotail = bio;
2374 }
2375
2376 __blk_rq_prep_clone(rq, rq_src);
2377
2378 return 0;
2379
2380free_and_out:
2381 if (bio)
2382 bio_free(bio, bs);
2383 blk_rq_unprep_clone(rq);
2384
2385 return -ENOMEM;
2386}
2387EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
2388
Jens Axboe18887ad2008-07-28 13:08:45 +02002389int kblockd_schedule_work(struct request_queue *q, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390{
2391 return queue_work(kblockd_workqueue, work);
2392}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393EXPORT_SYMBOL(kblockd_schedule_work);
2394
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395int __init blk_dev_init(void)
2396{
Nikanth Karthikesan9eb55b02009-04-27 14:53:54 +02002397 BUILD_BUG_ON(__REQ_NR_BITS > 8 *
2398 sizeof(((struct request *)0)->cmd_flags));
2399
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 kblockd_workqueue = create_workqueue("kblockd");
2401 if (!kblockd_workqueue)
2402 panic("Failed to create kblockd\n");
2403
2404 request_cachep = kmem_cache_create("blkdev_requests",
Paul Mundt20c2df82007-07-20 10:11:58 +09002405 sizeof(struct request), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
Jens Axboe8324aa92008-01-29 14:51:59 +01002407 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02002408 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 return 0;
2411}
2412