blob: 94d88fabc4bde21b2144952b24dd62888e8822fe [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;
Jens Axboe0989a022009-06-12 14:42:56 +0200501 q->backing_dev_info.ra_pages =
502 (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
503 q->backing_dev_info.state = 0;
504 q->backing_dev_info.capabilities = BDI_CAP_MAP_COPY;
505
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700506 err = bdi_init(&q->backing_dev_info);
507 if (err) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100508 kmem_cache_free(blk_requestq_cachep, q);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700509 return NULL;
510 }
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 init_timer(&q->unplug_timer);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700513 setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
514 INIT_LIST_HEAD(&q->timeout_list);
Peter Zijlstra713ada92008-10-16 13:44:57 +0200515 INIT_WORK(&q->unplug_work, blk_unplug_work);
Al Viro483f4af2006-03-18 18:34:37 -0500516
Jens Axboe8324aa92008-01-29 14:51:59 +0100517 kobject_init(&q->kobj, &blk_queue_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Al Viro483f4af2006-03-18 18:34:37 -0500519 mutex_init(&q->sysfs_lock);
Neil Browne7e72bf2008-05-14 16:05:54 -0700520 spin_lock_init(&q->__queue_lock);
Al Viro483f4af2006-03-18 18:34:37 -0500521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return q;
523}
Christoph Lameter19460892005-06-23 00:08:19 -0700524EXPORT_SYMBOL(blk_alloc_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526/**
527 * blk_init_queue - prepare a request queue for use with a block device
528 * @rfn: The function to be called to process requests that have been
529 * placed on the queue.
530 * @lock: Request queue spin lock
531 *
532 * Description:
533 * If a block device wishes to use the standard request handling procedures,
534 * which sorts requests and coalesces adjacent requests, then it must
535 * call blk_init_queue(). The function @rfn will be called when there
536 * are requests on the queue that need to be processed. If the device
537 * supports plugging, then @rfn may not be called immediately when requests
538 * are available on the queue, but may be called at some time later instead.
539 * Plugged queues are generally unplugged when a buffer belonging to one
540 * of the requests on the queue is needed, or due to memory pressure.
541 *
542 * @rfn is not required, or even expected, to remove all requests off the
543 * queue, but only as many as it can handle at a time. If it does leave
544 * requests on the queue, it is responsible for arranging that the requests
545 * get dealt with eventually.
546 *
547 * The queue spin lock must be held while manipulating the requests on the
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200548 * request queue; this lock will be taken also from interrupt context, so irq
549 * disabling is needed for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 *
Randy Dunlap710027a2008-08-19 20:13:11 +0200551 * Function returns a pointer to the initialized request queue, or %NULL if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 * it didn't succeed.
553 *
554 * Note:
555 * blk_init_queue() must be paired with a blk_cleanup_queue() call
556 * when the block device is deactivated (such as at module unload).
557 **/
Christoph Lameter19460892005-06-23 00:08:19 -0700558
Jens Axboe165125e2007-07-24 09:28:11 +0200559struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Christoph Lameter19460892005-06-23 00:08:19 -0700561 return blk_init_queue_node(rfn, lock, -1);
562}
563EXPORT_SYMBOL(blk_init_queue);
564
Jens Axboe165125e2007-07-24 09:28:11 +0200565struct request_queue *
Christoph Lameter19460892005-06-23 00:08:19 -0700566blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
567{
Jens Axboe165125e2007-07-24 09:28:11 +0200568 struct request_queue *q = blk_alloc_queue_node(GFP_KERNEL, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 if (!q)
571 return NULL;
572
Christoph Lameter19460892005-06-23 00:08:19 -0700573 q->node = node_id;
Al Viro8669aaf2006-03-18 13:50:00 -0500574 if (blk_init_free_list(q)) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100575 kmem_cache_free(blk_requestq_cachep, q);
Al Viro8669aaf2006-03-18 13:50:00 -0500576 return NULL;
577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
152587d2005-04-12 16:22:06 -0500579 /*
580 * if caller didn't supply a lock, they get per-queue locking with
581 * our embedded lock
582 */
Neil Browne7e72bf2008-05-14 16:05:54 -0700583 if (!lock)
152587d2005-04-12 16:22:06 -0500584 lock = &q->__queue_lock;
152587d2005-04-12 16:22:06 -0500585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 q->request_fn = rfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 q->prep_rq_fn = NULL;
588 q->unplug_fn = generic_unplug_device;
Jens Axboebc58ba92009-01-23 10:54:44 +0100589 q->queue_flags = QUEUE_FLAG_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 q->queue_lock = lock;
591
Jens Axboef3b144a2009-03-06 08:48:33 +0100592 /*
593 * This also sets hw/phys segments, boundary and size
594 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 blk_queue_make_request(q, __make_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Alan Stern44ec9542007-02-20 11:01:57 -0500597 q->sg_reserved_size = INT_MAX;
598
FUJITA Tomonoriabf54392008-08-16 14:10:05 +0900599 blk_set_cmd_filter_defaults(&q->cmd_filter);
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 /*
602 * all done
603 */
604 if (!elevator_init(q, NULL)) {
605 blk_queue_congestion_threshold(q);
606 return q;
607 }
608
Al Viro8669aaf2006-03-18 13:50:00 -0500609 blk_put_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return NULL;
611}
Christoph Lameter19460892005-06-23 00:08:19 -0700612EXPORT_SYMBOL(blk_init_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Jens Axboe165125e2007-07-24 09:28:11 +0200614int blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Nick Pigginfde6ad22005-06-23 00:08:53 -0700616 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
Al Viro483f4af2006-03-18 18:34:37 -0500617 kobject_get(&q->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return 0;
619 }
620
621 return 1;
622}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Jens Axboe165125e2007-07-24 09:28:11 +0200624static inline void blk_free_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Jens Axboe4aff5e22006-08-10 08:44:47 +0200626 if (rq->cmd_flags & REQ_ELVPRIV)
Tejun Heocb98fc82005-10-28 08:29:39 +0200627 elv_put_request(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 mempool_free(rq, q->rq.rq_pool);
629}
630
Jens Axboe1ea25ecb2006-07-18 22:24:11 +0200631static struct request *
Jerome Marchand42dad762009-04-22 14:01:49 +0200632blk_alloc_request(struct request_queue *q, int flags, int priv, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
634 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
635
636 if (!rq)
637 return NULL;
638
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200639 blk_rq_init(q, rq);
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200640
Jerome Marchand42dad762009-04-22 14:01:49 +0200641 rq->cmd_flags = flags | REQ_ALLOCED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Tejun Heocb98fc82005-10-28 08:29:39 +0200643 if (priv) {
Jens Axboecb78b282006-07-28 09:32:57 +0200644 if (unlikely(elv_set_request(q, rq, gfp_mask))) {
Tejun Heocb98fc82005-10-28 08:29:39 +0200645 mempool_free(rq, q->rq.rq_pool);
646 return NULL;
647 }
Jens Axboe4aff5e22006-08-10 08:44:47 +0200648 rq->cmd_flags |= REQ_ELVPRIV;
Tejun Heocb98fc82005-10-28 08:29:39 +0200649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Tejun Heocb98fc82005-10-28 08:29:39 +0200651 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
654/*
655 * ioc_batching returns true if the ioc is a valid batching request and
656 * should be given priority access to a request.
657 */
Jens Axboe165125e2007-07-24 09:28:11 +0200658static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
660 if (!ioc)
661 return 0;
662
663 /*
664 * Make sure the process is able to allocate at least 1 request
665 * even if the batch times out, otherwise we could theoretically
666 * lose wakeups.
667 */
668 return ioc->nr_batch_requests == q->nr_batching ||
669 (ioc->nr_batch_requests > 0
670 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
671}
672
673/*
674 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
675 * will cause the process to be a "batcher" on all queues in the system. This
676 * is the behaviour we want though - once it gets a wakeup it should be given
677 * a nice run.
678 */
Jens Axboe165125e2007-07-24 09:28:11 +0200679static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
681 if (!ioc || ioc_batching(q, ioc))
682 return;
683
684 ioc->nr_batch_requests = q->nr_batching;
685 ioc->last_waited = jiffies;
686}
687
Jens Axboe1faa16d2009-04-06 14:48:01 +0200688static void __freed_request(struct request_queue *q, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
690 struct request_list *rl = &q->rq;
691
Jens Axboe1faa16d2009-04-06 14:48:01 +0200692 if (rl->count[sync] < queue_congestion_off_threshold(q))
693 blk_clear_queue_congested(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Jens Axboe1faa16d2009-04-06 14:48:01 +0200695 if (rl->count[sync] + 1 <= q->nr_requests) {
696 if (waitqueue_active(&rl->wait[sync]))
697 wake_up(&rl->wait[sync]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Jens Axboe1faa16d2009-04-06 14:48:01 +0200699 blk_clear_queue_full(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
701}
702
703/*
704 * A request has just been released. Account for it, update the full and
705 * congestion status, wake up any waiters. Called under q->queue_lock.
706 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200707static void freed_request(struct request_queue *q, int sync, int priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
709 struct request_list *rl = &q->rq;
710
Jens Axboe1faa16d2009-04-06 14:48:01 +0200711 rl->count[sync]--;
Tejun Heocb98fc82005-10-28 08:29:39 +0200712 if (priv)
713 rl->elvpriv--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Jens Axboe1faa16d2009-04-06 14:48:01 +0200715 __freed_request(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Jens Axboe1faa16d2009-04-06 14:48:01 +0200717 if (unlikely(rl->starved[sync ^ 1]))
718 __freed_request(q, sync ^ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721/*
Nick Piggind6344532005-06-28 20:45:14 -0700722 * Get a free request, queue_lock must be held.
723 * Returns NULL on failure, with queue_lock held.
724 * Returns !NULL on success, with queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 */
Jens Axboe165125e2007-07-24 09:28:11 +0200726static struct request *get_request(struct request_queue *q, int rw_flags,
Jens Axboe7749a8d2006-12-13 13:02:26 +0100727 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
729 struct request *rq = NULL;
730 struct request_list *rl = &q->rq;
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100731 struct io_context *ioc = NULL;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200732 const bool is_sync = rw_is_sync(rw_flags) != 0;
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100733 int may_queue, priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Jens Axboe7749a8d2006-12-13 13:02:26 +0100735 may_queue = elv_may_queue(q, rw_flags);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100736 if (may_queue == ELV_MQUEUE_NO)
737 goto rq_starved;
738
Jens Axboe1faa16d2009-04-06 14:48:01 +0200739 if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) {
740 if (rl->count[is_sync]+1 >= q->nr_requests) {
Jens Axboeb5deef92006-07-19 23:39:40 +0200741 ioc = current_io_context(GFP_ATOMIC, q->node);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100742 /*
743 * The queue will fill after this allocation, so set
744 * it as full, and mark this process as "batching".
745 * This process will be allowed to complete a batch of
746 * requests, others will be blocked.
747 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200748 if (!blk_queue_full(q, is_sync)) {
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100749 ioc_set_batching(q, ioc);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200750 blk_set_queue_full(q, is_sync);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100751 } else {
752 if (may_queue != ELV_MQUEUE_MUST
753 && !ioc_batching(q, ioc)) {
754 /*
755 * The queue is full and the allocating
756 * process is not a "batcher", and not
757 * exempted by the IO scheduler
758 */
759 goto out;
760 }
761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
Jens Axboe1faa16d2009-04-06 14:48:01 +0200763 blk_set_queue_congested(q, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
765
Jens Axboe082cf692005-06-28 16:35:11 +0200766 /*
767 * Only allow batching queuers to allocate up to 50% over the defined
768 * limit of requests, otherwise we could have thousands of requests
769 * allocated with any setting of ->nr_requests
770 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200771 if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
Jens Axboe082cf692005-06-28 16:35:11 +0200772 goto out;
Hugh Dickinsfd782a42005-06-29 15:15:40 +0100773
Jens Axboe1faa16d2009-04-06 14:48:01 +0200774 rl->count[is_sync]++;
775 rl->starved[is_sync] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200776
Jens Axboe64521d12005-10-28 08:30:39 +0200777 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Tejun Heocb98fc82005-10-28 08:29:39 +0200778 if (priv)
779 rl->elvpriv++;
780
Jerome Marchand42dad762009-04-22 14:01:49 +0200781 if (blk_queue_io_stat(q))
782 rw_flags |= REQ_IO_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 spin_unlock_irq(q->queue_lock);
784
Jens Axboe7749a8d2006-12-13 13:02:26 +0100785 rq = blk_alloc_request(q, rw_flags, priv, gfp_mask);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100786 if (unlikely(!rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 /*
788 * Allocation failed presumably due to memory. Undo anything
789 * we might have messed up.
790 *
791 * Allocating task should really be put onto the front of the
792 * wait queue, but this is pretty rare.
793 */
794 spin_lock_irq(q->queue_lock);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200795 freed_request(q, is_sync, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 /*
798 * in the very unlikely event that allocation failed and no
799 * requests for this direction was pending, mark us starved
800 * so that freeing of a request in the other direction will
801 * notice us. another possible fix would be to split the
802 * rq mempool into READ and WRITE
803 */
804rq_starved:
Jens Axboe1faa16d2009-04-06 14:48:01 +0200805 if (unlikely(rl->count[is_sync] == 0))
806 rl->starved[is_sync] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 goto out;
809 }
810
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100811 /*
812 * ioc may be NULL here, and ioc_batching will be false. That's
813 * OK, if the queue is under the request limit then requests need
814 * not count toward the nr_batch_requests limit. There will always
815 * be some limit enforced by BLK_BATCH_TIME.
816 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (ioc_batching(q, ioc))
818 ioc->nr_batch_requests--;
Jens Axboe6728cb02008-01-31 13:03:55 +0100819
Jens Axboe1faa16d2009-04-06 14:48:01 +0200820 trace_block_getrq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return rq;
823}
824
825/*
826 * No available requests for this queue, unplug the device and wait for some
827 * requests to become available.
Nick Piggind6344532005-06-28 20:45:14 -0700828 *
829 * Called with q->queue_lock held, and returns with it unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 */
Jens Axboe165125e2007-07-24 09:28:11 +0200831static struct request *get_request_wait(struct request_queue *q, int rw_flags,
Jens Axboe22e2c502005-06-27 10:55:12 +0200832 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Jens Axboe1faa16d2009-04-06 14:48:01 +0200834 const bool is_sync = rw_is_sync(rw_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 struct request *rq;
836
Jens Axboe7749a8d2006-12-13 13:02:26 +0100837 rq = get_request(q, rw_flags, bio, GFP_NOIO);
Nick Piggin450991b2005-06-28 20:45:13 -0700838 while (!rq) {
839 DEFINE_WAIT(wait);
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200840 struct io_context *ioc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 struct request_list *rl = &q->rq;
842
Jens Axboe1faa16d2009-04-06 14:48:01 +0200843 prepare_to_wait_exclusive(&rl->wait[is_sync], &wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 TASK_UNINTERRUPTIBLE);
845
Jens Axboe1faa16d2009-04-06 14:48:01 +0200846 trace_block_sleeprq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200848 __generic_unplug_device(q);
849 spin_unlock_irq(q->queue_lock);
850 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200852 /*
853 * After sleeping, we become a "batching" process and
854 * will be able to allocate at least one request, and
855 * up to a big batch of them for a small period time.
856 * See ioc_batching, ioc_set_batching
857 */
858 ioc = current_io_context(GFP_NOIO, q->node);
859 ioc_set_batching(q, ioc);
Jens Axboe2056a782006-03-23 20:00:26 +0100860
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200861 spin_lock_irq(q->queue_lock);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200862 finish_wait(&rl->wait[is_sync], &wait);
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200863
864 rq = get_request(q, rw_flags, bio, GFP_NOIO);
865 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 return rq;
868}
869
Jens Axboe165125e2007-07-24 09:28:11 +0200870struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
872 struct request *rq;
873
874 BUG_ON(rw != READ && rw != WRITE);
875
Nick Piggind6344532005-06-28 20:45:14 -0700876 spin_lock_irq(q->queue_lock);
877 if (gfp_mask & __GFP_WAIT) {
Jens Axboe22e2c502005-06-27 10:55:12 +0200878 rq = get_request_wait(q, rw, NULL);
Nick Piggind6344532005-06-28 20:45:14 -0700879 } else {
Jens Axboe22e2c502005-06-27 10:55:12 +0200880 rq = get_request(q, rw, NULL, gfp_mask);
Nick Piggind6344532005-06-28 20:45:14 -0700881 if (!rq)
882 spin_unlock_irq(q->queue_lock);
883 }
884 /* q->queue_lock is unlocked at this point */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 return rq;
887}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888EXPORT_SYMBOL(blk_get_request);
889
890/**
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300891 * blk_make_request - given a bio, allocate a corresponding struct request.
Randy Dunlap8ebf9752009-06-11 20:00:41 -0700892 * @q: target request queue
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300893 * @bio: The bio describing the memory mappings that will be submitted for IO.
894 * It may be a chained-bio properly constructed by block/bio layer.
Randy Dunlap8ebf9752009-06-11 20:00:41 -0700895 * @gfp_mask: gfp flags to be used for memory allocation
Jens Axboedc72ef42006-07-20 14:54:05 +0200896 *
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300897 * blk_make_request is the parallel of generic_make_request for BLOCK_PC
898 * type commands. Where the struct request needs to be farther initialized by
899 * the caller. It is passed a &struct bio, which describes the memory info of
900 * the I/O transfer.
901 *
902 * The caller of blk_make_request must make sure that bi_io_vec
903 * are set to describe the memory buffers. That bio_data_dir() will return
904 * the needed direction of the request. (And all bio's in the passed bio-chain
905 * are properly set accordingly)
906 *
907 * If called under none-sleepable conditions, mapped bio buffers must not
908 * need bouncing, by calling the appropriate masked or flagged allocator,
909 * suitable for the target device. Otherwise the call to blk_queue_bounce will
910 * BUG.
Jens Axboe53674ac2009-05-19 19:52:35 +0200911 *
912 * WARNING: When allocating/cloning a bio-chain, careful consideration should be
913 * given to how you allocate bios. In particular, you cannot use __GFP_WAIT for
914 * anything but the first bio in the chain. Otherwise you risk waiting for IO
915 * completion of a bio that hasn't been submitted yet, thus resulting in a
916 * deadlock. Alternatively bios should be allocated using bio_kmalloc() instead
917 * of bio_alloc(), as that avoids the mempool deadlock.
918 * If possible a big IO should be split into smaller parts when allocation
919 * fails. Partial allocation should not be an error, or you risk a live-lock.
Jens Axboedc72ef42006-07-20 14:54:05 +0200920 */
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300921struct request *blk_make_request(struct request_queue *q, struct bio *bio,
922 gfp_t gfp_mask)
Jens Axboedc72ef42006-07-20 14:54:05 +0200923{
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300924 struct request *rq = blk_get_request(q, bio_data_dir(bio), gfp_mask);
925
926 if (unlikely(!rq))
927 return ERR_PTR(-ENOMEM);
928
929 for_each_bio(bio) {
930 struct bio *bounce_bio = bio;
931 int ret;
932
933 blk_queue_bounce(q, &bounce_bio);
934 ret = blk_rq_append_bio(q, rq, bounce_bio);
935 if (unlikely(ret)) {
936 blk_put_request(rq);
937 return ERR_PTR(ret);
938 }
939 }
940
941 return rq;
Jens Axboedc72ef42006-07-20 14:54:05 +0200942}
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300943EXPORT_SYMBOL(blk_make_request);
Jens Axboedc72ef42006-07-20 14:54:05 +0200944
945/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 * blk_requeue_request - put a request back on queue
947 * @q: request queue where request should be inserted
948 * @rq: request to be inserted
949 *
950 * Description:
951 * Drivers often keep queueing requests until the hardware cannot accept
952 * more, when that condition happens we need to put the request back
953 * on the queue. Must be called with queue lock held.
954 */
Jens Axboe165125e2007-07-24 09:28:11 +0200955void blk_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
Jens Axboe242f9dc2008-09-14 05:55:09 -0700957 blk_delete_timer(rq);
958 blk_clear_rq_complete(rq);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100959 trace_block_rq_requeue(q, rq);
Jens Axboe2056a782006-03-23 20:00:26 +0100960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 if (blk_rq_tagged(rq))
962 blk_queue_end_tag(q, rq);
963
James Bottomleyba396a62009-05-27 14:17:08 +0200964 BUG_ON(blk_queued_rq(rq));
965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 elv_requeue_request(q, rq);
967}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968EXPORT_SYMBOL(blk_requeue_request);
969
970/**
Randy Dunlap710027a2008-08-19 20:13:11 +0200971 * blk_insert_request - insert a special request into a request queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 * @q: request queue where request should be inserted
973 * @rq: request to be inserted
974 * @at_head: insert request at head or tail of queue
975 * @data: private data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 *
977 * Description:
978 * Many block devices need to execute commands asynchronously, so they don't
979 * block the whole kernel from preemption during request execution. This is
980 * accomplished normally by inserting aritficial requests tagged as
Randy Dunlap710027a2008-08-19 20:13:11 +0200981 * REQ_TYPE_SPECIAL in to the corresponding request queue, and letting them
982 * be scheduled for actual execution by the request queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 *
984 * We have the option of inserting the head or the tail of the queue.
985 * Typically we use the tail for new ioctls and so forth. We use the head
986 * of the queue for things like a QUEUE_FULL message from a device, or a
987 * host that is unable to accept a particular command.
988 */
Jens Axboe165125e2007-07-24 09:28:11 +0200989void blk_insert_request(struct request_queue *q, struct request *rq,
Tejun Heo 867d1192005-04-24 02:06:05 -0500990 int at_head, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Tejun Heo 867d1192005-04-24 02:06:05 -0500992 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 unsigned long flags;
994
995 /*
996 * tell I/O scheduler that this isn't a regular read/write (ie it
997 * must not attempt merges on this) and that it acts as a soft
998 * barrier
999 */
Jens Axboe4aff5e22006-08-10 08:44:47 +02001000 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 rq->special = data;
1003
1004 spin_lock_irqsave(q->queue_lock, flags);
1005
1006 /*
1007 * If command is tagged, release the tag
1008 */
Tejun Heo 867d1192005-04-24 02:06:05 -05001009 if (blk_rq_tagged(rq))
1010 blk_queue_end_tag(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Jerome Marchandb238b3d2007-10-23 15:05:46 +02001012 drive_stat_acct(rq, 1);
Tejun Heo 867d1192005-04-24 02:06:05 -05001013 __elv_add_request(q, rq, where, 0);
Tejun Heoa7f55792009-04-23 11:05:17 +09001014 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 spin_unlock_irqrestore(q->queue_lock, flags);
1016}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017EXPORT_SYMBOL(blk_insert_request);
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019/*
1020 * add-request adds a request to the linked list.
1021 * queue lock is held and interrupts disabled, as we muck with the
1022 * request queue list.
1023 */
Jens Axboe6728cb02008-01-31 13:03:55 +01001024static inline void add_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Jerome Marchandb238b3d2007-10-23 15:05:46 +02001026 drive_stat_acct(req, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 /*
1029 * elevator indicated where it wants this request to be
1030 * inserted at elevator_merge time
1031 */
1032 __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
1033}
Jens Axboe6728cb02008-01-31 13:03:55 +01001034
Tejun Heo074a7ac2008-08-25 19:56:14 +09001035static void part_round_stats_single(int cpu, struct hd_struct *part,
1036 unsigned long now)
1037{
1038 if (now == part->stamp)
1039 return;
1040
1041 if (part->in_flight) {
1042 __part_stat_add(cpu, part, time_in_queue,
1043 part->in_flight * (now - part->stamp));
1044 __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
1045 }
1046 part->stamp = now;
1047}
1048
1049/**
Randy Dunlap496aa8a2008-10-16 07:46:23 +02001050 * part_round_stats() - Round off the performance stats on a struct disk_stats.
1051 * @cpu: cpu number for stats access
1052 * @part: target partition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 *
1054 * The average IO queue length and utilisation statistics are maintained
1055 * by observing the current state of the queue length and the amount of
1056 * time it has been in this state for.
1057 *
1058 * Normally, that accounting is done on IO completion, but that can result
1059 * in more than a second's worth of IO being accounted for within any one
1060 * second, leading to >100% utilisation. To deal with that, we call this
1061 * function to do a round-off before returning the results when reading
1062 * /proc/diskstats. This accounts immediately for all queue usage up to
1063 * the current jiffies and restarts the counters again.
1064 */
Tejun Heoc9959052008-08-25 19:47:21 +09001065void part_round_stats(int cpu, struct hd_struct *part)
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001066{
1067 unsigned long now = jiffies;
1068
Tejun Heo074a7ac2008-08-25 19:56:14 +09001069 if (part->partno)
1070 part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
1071 part_round_stats_single(cpu, part, now);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001072}
Tejun Heo074a7ac2008-08-25 19:56:14 +09001073EXPORT_SYMBOL_GPL(part_round_stats);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075/*
1076 * queue lock must be held
1077 */
Jens Axboe165125e2007-07-24 09:28:11 +02001078void __blk_put_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (unlikely(!q))
1081 return;
1082 if (unlikely(--req->ref_count))
1083 return;
1084
Tejun Heo8922e162005-10-20 16:23:44 +02001085 elv_completed_request(q, req);
1086
Boaz Harrosh1cd96c22009-03-24 12:35:07 +01001087 /* this is a bio leak */
1088 WARN_ON(req->bio != NULL);
1089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 /*
1091 * Request may not have originated from ll_rw_blk. if not,
1092 * it didn't come out of our reserved rq pools
1093 */
Jens Axboe49171e52006-08-10 08:59:11 +02001094 if (req->cmd_flags & REQ_ALLOCED) {
Jens Axboe1faa16d2009-04-06 14:48:01 +02001095 int is_sync = rq_is_sync(req) != 0;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001096 int priv = req->cmd_flags & REQ_ELVPRIV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 BUG_ON(!list_empty(&req->queuelist));
Jens Axboe98170642006-07-28 09:23:08 +02001099 BUG_ON(!hlist_unhashed(&req->hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 blk_free_request(q, req);
Jens Axboe1faa16d2009-04-06 14:48:01 +02001102 freed_request(q, is_sync, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
1104}
Mike Christie6e39b69e2005-11-11 05:30:24 -06001105EXPORT_SYMBOL_GPL(__blk_put_request);
1106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107void blk_put_request(struct request *req)
1108{
Tejun Heo8922e162005-10-20 16:23:44 +02001109 unsigned long flags;
Jens Axboe165125e2007-07-24 09:28:11 +02001110 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
FUJITA Tomonori52a93ba2008-07-15 21:21:45 +02001112 spin_lock_irqsave(q->queue_lock, flags);
1113 __blk_put_request(q, req);
1114 spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116EXPORT_SYMBOL(blk_put_request);
1117
Jens Axboe86db1e22008-01-29 14:53:40 +01001118void init_request_from_bio(struct request *req, struct bio *bio)
Tejun Heo52d9e672006-01-06 09:49:58 +01001119{
Jens Axboec7c22e42008-09-13 20:26:01 +02001120 req->cpu = bio->bi_comp_cpu;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001121 req->cmd_type = REQ_TYPE_FS;
Tejun Heo52d9e672006-01-06 09:49:58 +01001122
1123 /*
1124 * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
1125 */
Mike Christie6000a362008-08-19 18:45:30 -05001126 if (bio_rw_ahead(bio))
1127 req->cmd_flags |= (REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
1128 REQ_FAILFAST_DRIVER);
1129 if (bio_failfast_dev(bio))
1130 req->cmd_flags |= REQ_FAILFAST_DEV;
1131 if (bio_failfast_transport(bio))
1132 req->cmd_flags |= REQ_FAILFAST_TRANSPORT;
1133 if (bio_failfast_driver(bio))
1134 req->cmd_flags |= REQ_FAILFAST_DRIVER;
Tejun Heo52d9e672006-01-06 09:49:58 +01001135
David Woodhousefb2dce82008-08-05 18:01:53 +01001136 if (unlikely(bio_discard(bio))) {
David Woodhousee17fc0a2008-08-09 16:42:20 +01001137 req->cmd_flags |= REQ_DISCARD;
1138 if (bio_barrier(bio))
1139 req->cmd_flags |= REQ_SOFTBARRIER;
David Woodhousefb2dce82008-08-05 18:01:53 +01001140 req->q->prepare_discard_fn(req->q, req);
David Woodhousee17fc0a2008-08-09 16:42:20 +01001141 } else if (unlikely(bio_barrier(bio)))
Tejun Heoe4025f62009-04-23 11:05:17 +09001142 req->cmd_flags |= REQ_HARDBARRIER;
Tejun Heo52d9e672006-01-06 09:49:58 +01001143
Jens Axboeb31dc662006-06-13 08:26:10 +02001144 if (bio_sync(bio))
Jens Axboe4aff5e22006-08-10 08:44:47 +02001145 req->cmd_flags |= REQ_RW_SYNC;
Jens Axboe5404bc72006-08-10 09:01:02 +02001146 if (bio_rw_meta(bio))
1147 req->cmd_flags |= REQ_RW_META;
Jens Axboeaeb6faf2009-04-06 14:48:07 +02001148 if (bio_noidle(bio))
1149 req->cmd_flags |= REQ_NOIDLE;
Jens Axboeb31dc662006-06-13 08:26:10 +02001150
Tejun Heo52d9e672006-01-06 09:49:58 +01001151 req->errors = 0;
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001152 req->__sector = bio->bi_sector;
Tejun Heo52d9e672006-01-06 09:49:58 +01001153 req->ioprio = bio_prio(bio);
NeilBrownbc1c56f2007-08-16 13:31:30 +02001154 blk_rq_bio_prep(req->q, req, bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001155}
1156
Jens Axboe644b2d92009-04-06 14:48:06 +02001157/*
1158 * Only disabling plugging for non-rotational devices if it does tagging
1159 * as well, otherwise we do need the proper merging
1160 */
1161static inline bool queue_should_plug(struct request_queue *q)
1162{
1163 return !(blk_queue_nonrot(q) && blk_queue_tagged(q));
1164}
1165
Jens Axboe165125e2007-07-24 09:28:11 +02001166static int __make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
Nick Piggin450991b2005-06-28 20:45:13 -07001168 struct request *req;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001169 int el_ret;
1170 unsigned int bytes = bio->bi_size;
Jens Axboe51da90f2006-07-18 04:14:45 +02001171 const unsigned short prio = bio_prio(bio);
1172 const int sync = bio_sync(bio);
Jens Axboe213d9412009-01-06 09:16:05 +01001173 const int unplug = bio_unplug(bio);
Jens Axboe7749a8d2006-12-13 13:02:26 +01001174 int rw_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 /*
1177 * low level driver can indicate that it wants pages above a
1178 * certain limit bounced to low memory (ie for highmem, or even
1179 * ISA dma in theory)
1180 */
1181 blk_queue_bounce(q, &bio);
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 spin_lock_irq(q->queue_lock);
1184
Tejun Heoa7384672008-11-28 13:32:03 +09001185 if (unlikely(bio_barrier(bio)) || elv_queue_empty(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 goto get_rq;
1187
1188 el_ret = elv_merge(q, &req, bio);
1189 switch (el_ret) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001190 case ELEVATOR_BACK_MERGE:
1191 BUG_ON(!rq_mergeable(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Jens Axboe6728cb02008-01-31 13:03:55 +01001193 if (!ll_back_merge_fn(q, req, bio))
1194 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001196 trace_block_bio_backmerge(q, bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001197
Jens Axboe6728cb02008-01-31 13:03:55 +01001198 req->biotail->bi_next = bio;
1199 req->biotail = bio;
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001200 req->__data_len += bytes;
Jens Axboe6728cb02008-01-31 13:03:55 +01001201 req->ioprio = ioprio_best(req->ioprio, prio);
Jens Axboeab780f12008-08-26 10:25:02 +02001202 if (!blk_rq_cpu_valid(req))
1203 req->cpu = bio->bi_comp_cpu;
Jens Axboe6728cb02008-01-31 13:03:55 +01001204 drive_stat_acct(req, 0);
1205 if (!attempt_back_merge(q, req))
1206 elv_merged_request(q, req, el_ret);
1207 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Jens Axboe6728cb02008-01-31 13:03:55 +01001209 case ELEVATOR_FRONT_MERGE:
1210 BUG_ON(!rq_mergeable(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Jens Axboe6728cb02008-01-31 13:03:55 +01001212 if (!ll_front_merge_fn(q, req, bio))
1213 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001215 trace_block_bio_frontmerge(q, bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001216
Jens Axboe6728cb02008-01-31 13:03:55 +01001217 bio->bi_next = req->bio;
1218 req->bio = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Jens Axboe6728cb02008-01-31 13:03:55 +01001220 /*
1221 * may not be valid. if the low level driver said
1222 * it didn't need a bounce buffer then it better
1223 * not touch req->buffer either...
1224 */
1225 req->buffer = bio_data(bio);
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001226 req->__sector = bio->bi_sector;
1227 req->__data_len += bytes;
Jens Axboe6728cb02008-01-31 13:03:55 +01001228 req->ioprio = ioprio_best(req->ioprio, prio);
Jens Axboeab780f12008-08-26 10:25:02 +02001229 if (!blk_rq_cpu_valid(req))
1230 req->cpu = bio->bi_comp_cpu;
Jens Axboe6728cb02008-01-31 13:03:55 +01001231 drive_stat_acct(req, 0);
1232 if (!attempt_front_merge(q, req))
1233 elv_merged_request(q, req, el_ret);
1234 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Jens Axboe6728cb02008-01-31 13:03:55 +01001236 /* ELV_NO_MERGE: elevator says don't/can't merge. */
1237 default:
1238 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
1240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241get_rq:
Nick Piggin450991b2005-06-28 20:45:13 -07001242 /*
Jens Axboe7749a8d2006-12-13 13:02:26 +01001243 * This sync check and mask will be re-done in init_request_from_bio(),
1244 * but we need to set it earlier to expose the sync flag to the
1245 * rq allocator and io schedulers.
1246 */
1247 rw_flags = bio_data_dir(bio);
1248 if (sync)
1249 rw_flags |= REQ_RW_SYNC;
1250
1251 /*
Nick Piggin450991b2005-06-28 20:45:13 -07001252 * Grab a free request. This is might sleep but can not fail.
Nick Piggind6344532005-06-28 20:45:14 -07001253 * Returns with the queue unlocked.
Nick Piggin450991b2005-06-28 20:45:13 -07001254 */
Jens Axboe7749a8d2006-12-13 13:02:26 +01001255 req = get_request_wait(q, rw_flags, bio);
Nick Piggind6344532005-06-28 20:45:14 -07001256
Nick Piggin450991b2005-06-28 20:45:13 -07001257 /*
1258 * After dropping the lock and possibly sleeping here, our request
1259 * may now be mergeable after it had proven unmergeable (above).
1260 * We don't worry about that case for efficiency. It won't happen
1261 * often, and the elevators are able to handle it.
1262 */
Tejun Heo52d9e672006-01-06 09:49:58 +01001263 init_request_from_bio(req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Nick Piggin450991b2005-06-28 20:45:13 -07001265 spin_lock_irq(q->queue_lock);
Jens Axboec7c22e42008-09-13 20:26:01 +02001266 if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags) ||
1267 bio_flagged(bio, BIO_CPU_AFFINE))
1268 req->cpu = blk_cpu_to_group(smp_processor_id());
Jens Axboe644b2d92009-04-06 14:48:06 +02001269 if (queue_should_plug(q) && elv_queue_empty(q))
Nick Piggin450991b2005-06-28 20:45:13 -07001270 blk_plug_device(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 add_request(q, req);
1272out:
Jens Axboe644b2d92009-04-06 14:48:06 +02001273 if (unplug || !queue_should_plug(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 __generic_unplug_device(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 spin_unlock_irq(q->queue_lock);
1276 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277}
1278
1279/*
1280 * If bio->bi_dev is a partition, remap the location
1281 */
1282static inline void blk_partition_remap(struct bio *bio)
1283{
1284 struct block_device *bdev = bio->bi_bdev;
1285
Jens Axboebf2de6f2007-09-27 13:01:25 +02001286 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 struct hd_struct *p = bdev->bd_part;
Jens Axboea3623572005-11-01 09:26:16 +01001288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 bio->bi_sector += p->start_sect;
1290 bio->bi_bdev = bdev->bd_contains;
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001291
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001292 trace_block_remap(bdev_get_queue(bio->bi_bdev), bio,
Alan D. Brunelle22a7c312009-05-04 16:35:08 -04001293 bdev->bd_dev,
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001294 bio->bi_sector - p->start_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
1296}
1297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298static void handle_bad_sector(struct bio *bio)
1299{
1300 char b[BDEVNAME_SIZE];
1301
1302 printk(KERN_INFO "attempt to access beyond end of device\n");
1303 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1304 bdevname(bio->bi_bdev, b),
1305 bio->bi_rw,
1306 (unsigned long long)bio->bi_sector + bio_sectors(bio),
1307 (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
1308
1309 set_bit(BIO_EOF, &bio->bi_flags);
1310}
1311
Akinobu Mitac17bb492006-12-08 02:39:46 -08001312#ifdef CONFIG_FAIL_MAKE_REQUEST
1313
1314static DECLARE_FAULT_ATTR(fail_make_request);
1315
1316static int __init setup_fail_make_request(char *str)
1317{
1318 return setup_fault_attr(&fail_make_request, str);
1319}
1320__setup("fail_make_request=", setup_fail_make_request);
1321
1322static int should_fail_request(struct bio *bio)
1323{
Tejun Heoeddb2e22008-08-25 19:56:13 +09001324 struct hd_struct *part = bio->bi_bdev->bd_part;
1325
1326 if (part_to_disk(part)->part0.make_it_fail || part->make_it_fail)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001327 return should_fail(&fail_make_request, bio->bi_size);
1328
1329 return 0;
1330}
1331
1332static int __init fail_make_request_debugfs(void)
1333{
1334 return init_fault_attr_dentries(&fail_make_request,
1335 "fail_make_request");
1336}
1337
1338late_initcall(fail_make_request_debugfs);
1339
1340#else /* CONFIG_FAIL_MAKE_REQUEST */
1341
1342static inline int should_fail_request(struct bio *bio)
1343{
1344 return 0;
1345}
1346
1347#endif /* CONFIG_FAIL_MAKE_REQUEST */
1348
Jens Axboec07e2b42007-07-18 13:27:58 +02001349/*
1350 * Check whether this bio extends beyond the end of the device.
1351 */
1352static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1353{
1354 sector_t maxsector;
1355
1356 if (!nr_sectors)
1357 return 0;
1358
1359 /* Test device or partition size, when known. */
1360 maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
1361 if (maxsector) {
1362 sector_t sector = bio->bi_sector;
1363
1364 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1365 /*
1366 * This may well happen - the kernel calls bread()
1367 * without checking the size of the device, e.g., when
1368 * mounting a device.
1369 */
1370 handle_bad_sector(bio);
1371 return 1;
1372 }
1373 }
1374
1375 return 0;
1376}
1377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001379 * generic_make_request - hand a buffer to its device driver for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 * @bio: The bio describing the location in memory and on the device.
1381 *
1382 * generic_make_request() is used to make I/O requests of block
1383 * devices. It is passed a &struct bio, which describes the I/O that needs
1384 * to be done.
1385 *
1386 * generic_make_request() does not return any status. The
1387 * success/failure status of the request, along with notification of
1388 * completion, is delivered asynchronously through the bio->bi_end_io
1389 * function described (one day) else where.
1390 *
1391 * The caller of generic_make_request must make sure that bi_io_vec
1392 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1393 * set to describe the device address, and the
1394 * bi_end_io and optionally bi_private are set to describe how
1395 * completion notification should be signaled.
1396 *
1397 * generic_make_request and the drivers it calls may use bi_next if this
1398 * bio happens to be merged with someone else, and may change bi_dev and
1399 * bi_sector for remaps as it sees fit. So the values of these fields
1400 * should NOT be depended on after the call to generic_make_request.
1401 */
Neil Brownd89d8792007-05-01 09:53:42 +02001402static inline void __generic_make_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
Jens Axboe165125e2007-07-24 09:28:11 +02001404 struct request_queue *q;
NeilBrown5ddfe962006-10-30 22:07:21 -08001405 sector_t old_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 int ret, nr_sectors = bio_sectors(bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001407 dev_t old_dev;
Jens Axboe51fd77b2007-11-02 08:49:08 +01001408 int err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Jens Axboec07e2b42007-07-18 13:27:58 +02001412 if (bio_check_eod(bio, nr_sectors))
1413 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415 /*
1416 * Resolve the mapping until finished. (drivers are
1417 * still free to implement/resolve their own stacking
1418 * by explicitly returning 0)
1419 *
1420 * NOTE: we don't repeat the blk_size check for each new device.
1421 * Stacking drivers are expected to know what they are doing.
1422 */
NeilBrown5ddfe962006-10-30 22:07:21 -08001423 old_sector = -1;
Jens Axboe2056a782006-03-23 20:00:26 +01001424 old_dev = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 do {
1426 char b[BDEVNAME_SIZE];
1427
1428 q = bdev_get_queue(bio->bi_bdev);
Tejun Heoa7384672008-11-28 13:32:03 +09001429 if (unlikely(!q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 printk(KERN_ERR
1431 "generic_make_request: Trying to access "
1432 "nonexistent block-device %s (%Lu)\n",
1433 bdevname(bio->bi_bdev, b),
1434 (long long) bio->bi_sector);
Tejun Heoa7384672008-11-28 13:32:03 +09001435 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 }
1437
Martin K. Petersenae03bf62009-05-22 17:17:50 -04001438 if (unlikely(nr_sectors > queue_max_hw_sectors(q))) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001439 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
Martin K. Petersenae03bf62009-05-22 17:17:50 -04001440 bdevname(bio->bi_bdev, b),
1441 bio_sectors(bio),
1442 queue_max_hw_sectors(q));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 goto end_io;
1444 }
1445
Nick Pigginfde6ad22005-06-23 00:08:53 -07001446 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 goto end_io;
1448
Akinobu Mitac17bb492006-12-08 02:39:46 -08001449 if (should_fail_request(bio))
1450 goto end_io;
1451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 /*
1453 * If this device has partitions, remap block n
1454 * of partition p to block n+start(p) of the disk.
1455 */
1456 blk_partition_remap(bio);
1457
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +02001458 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio))
1459 goto end_io;
1460
NeilBrown5ddfe962006-10-30 22:07:21 -08001461 if (old_sector != -1)
Alan D. Brunelle22a7c312009-05-04 16:35:08 -04001462 trace_block_remap(q, bio, old_dev, old_sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001463
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001464 trace_block_bio_queue(q, bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001465
NeilBrown5ddfe962006-10-30 22:07:21 -08001466 old_sector = bio->bi_sector;
Jens Axboe2056a782006-03-23 20:00:26 +01001467 old_dev = bio->bi_bdev->bd_dev;
1468
Jens Axboec07e2b42007-07-18 13:27:58 +02001469 if (bio_check_eod(bio, nr_sectors))
1470 goto end_io;
Tejun Heoa7384672008-11-28 13:32:03 +09001471
1472 if (bio_discard(bio) && !q->prepare_discard_fn) {
Jens Axboe51fd77b2007-11-02 08:49:08 +01001473 err = -EOPNOTSUPP;
1474 goto end_io;
1475 }
Jens Axboecec07072009-01-13 15:28:32 +01001476 if (bio_barrier(bio) && bio_has_data(bio) &&
1477 (q->next_ordered == QUEUE_ORDERED_NONE)) {
1478 err = -EOPNOTSUPP;
1479 goto end_io;
1480 }
NeilBrown5ddfe962006-10-30 22:07:21 -08001481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 ret = q->make_request_fn(q, bio);
1483 } while (ret);
Tejun Heoa7384672008-11-28 13:32:03 +09001484
1485 return;
1486
1487end_io:
1488 bio_endio(bio, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489}
1490
Neil Brownd89d8792007-05-01 09:53:42 +02001491/*
1492 * We only want one ->make_request_fn to be active at a time,
1493 * else stack usage with stacked devices could be a problem.
1494 * So use current->bio_{list,tail} to keep a list of requests
1495 * submited by a make_request_fn function.
1496 * current->bio_tail is also used as a flag to say if
1497 * generic_make_request is currently active in this task or not.
1498 * If it is NULL, then no make_request is active. If it is non-NULL,
1499 * then a make_request is active, and new requests should be added
1500 * at the tail
1501 */
1502void generic_make_request(struct bio *bio)
1503{
1504 if (current->bio_tail) {
1505 /* make_request is active */
1506 *(current->bio_tail) = bio;
1507 bio->bi_next = NULL;
1508 current->bio_tail = &bio->bi_next;
1509 return;
1510 }
1511 /* following loop may be a bit non-obvious, and so deserves some
1512 * explanation.
1513 * Before entering the loop, bio->bi_next is NULL (as all callers
1514 * ensure that) so we have a list with a single bio.
1515 * We pretend that we have just taken it off a longer list, so
1516 * we assign bio_list to the next (which is NULL) and bio_tail
1517 * to &bio_list, thus initialising the bio_list of new bios to be
1518 * added. __generic_make_request may indeed add some more bios
1519 * through a recursive call to generic_make_request. If it
1520 * did, we find a non-NULL value in bio_list and re-enter the loop
1521 * from the top. In this case we really did just take the bio
1522 * of the top of the list (no pretending) and so fixup bio_list and
1523 * bio_tail or bi_next, and call into __generic_make_request again.
1524 *
1525 * The loop was structured like this to make only one call to
1526 * __generic_make_request (which is important as it is large and
1527 * inlined) and to keep the structure simple.
1528 */
1529 BUG_ON(bio->bi_next);
1530 do {
1531 current->bio_list = bio->bi_next;
1532 if (bio->bi_next == NULL)
1533 current->bio_tail = &current->bio_list;
1534 else
1535 bio->bi_next = NULL;
1536 __generic_make_request(bio);
1537 bio = current->bio_list;
1538 } while (bio);
1539 current->bio_tail = NULL; /* deactivate */
1540}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541EXPORT_SYMBOL(generic_make_request);
1542
1543/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001544 * submit_bio - submit a bio to the block device layer for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1546 * @bio: The &struct bio which describes the I/O
1547 *
1548 * submit_bio() is very similar in purpose to generic_make_request(), and
1549 * uses that function to do most of the work. Both are fairly rough
Randy Dunlap710027a2008-08-19 20:13:11 +02001550 * interfaces; @bio must be presetup and ready for I/O.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 *
1552 */
1553void submit_bio(int rw, struct bio *bio)
1554{
1555 int count = bio_sectors(bio);
1556
Jens Axboe22e2c502005-06-27 10:55:12 +02001557 bio->bi_rw |= rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Jens Axboebf2de6f2007-09-27 13:01:25 +02001559 /*
1560 * If it's a regular read/write or a barrier with data attached,
1561 * go through the normal accounting stuff before submission.
1562 */
Jens Axboea9c701e2008-08-08 11:04:44 +02001563 if (bio_has_data(bio)) {
Jens Axboebf2de6f2007-09-27 13:01:25 +02001564 if (rw & WRITE) {
1565 count_vm_events(PGPGOUT, count);
1566 } else {
1567 task_io_account_read(bio->bi_size);
1568 count_vm_events(PGPGIN, count);
1569 }
1570
1571 if (unlikely(block_dump)) {
1572 char b[BDEVNAME_SIZE];
1573 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001574 current->comm, task_pid_nr(current),
Jens Axboebf2de6f2007-09-27 13:01:25 +02001575 (rw & WRITE) ? "WRITE" : "READ",
1576 (unsigned long long)bio->bi_sector,
Jens Axboe6728cb02008-01-31 13:03:55 +01001577 bdevname(bio->bi_bdev, b));
Jens Axboebf2de6f2007-09-27 13:01:25 +02001578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 }
1580
1581 generic_make_request(bio);
1582}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583EXPORT_SYMBOL(submit_bio);
1584
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001585/**
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001586 * blk_rq_check_limits - Helper function to check a request for the queue limit
1587 * @q: the queue
1588 * @rq: the request being checked
1589 *
1590 * Description:
1591 * @rq may have been made based on weaker limitations of upper-level queues
1592 * in request stacking drivers, and it may violate the limitation of @q.
1593 * Since the block layer and the underlying device driver trust @rq
1594 * after it is inserted to @q, it should be checked against @q before
1595 * the insertion using this generic function.
1596 *
1597 * This function should also be useful for request stacking drivers
1598 * in some cases below, so export this fuction.
1599 * Request stacking drivers like request-based dm may change the queue
1600 * limits while requests are in the queue (e.g. dm's table swapping).
1601 * Such request stacking drivers should check those requests agaist
1602 * the new queue limits again when they dispatch those requests,
1603 * although such checkings are also done against the old queue limits
1604 * when submitting requests.
1605 */
1606int blk_rq_check_limits(struct request_queue *q, struct request *rq)
1607{
Martin K. Petersenae03bf62009-05-22 17:17:50 -04001608 if (blk_rq_sectors(rq) > queue_max_sectors(q) ||
1609 blk_rq_bytes(rq) > queue_max_hw_sectors(q) << 9) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001610 printk(KERN_ERR "%s: over max size limit.\n", __func__);
1611 return -EIO;
1612 }
1613
1614 /*
1615 * queue's settings related to segment counting like q->bounce_pfn
1616 * may differ from that of other stacking queues.
1617 * Recalculate it to check the request correctly on this queue's
1618 * limitation.
1619 */
1620 blk_recalc_rq_segments(rq);
Martin K. Petersenae03bf62009-05-22 17:17:50 -04001621 if (rq->nr_phys_segments > queue_max_phys_segments(q) ||
1622 rq->nr_phys_segments > queue_max_hw_segments(q)) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001623 printk(KERN_ERR "%s: over max segments limit.\n", __func__);
1624 return -EIO;
1625 }
1626
1627 return 0;
1628}
1629EXPORT_SYMBOL_GPL(blk_rq_check_limits);
1630
1631/**
1632 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
1633 * @q: the queue to submit the request
1634 * @rq: the request being queued
1635 */
1636int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
1637{
1638 unsigned long flags;
1639
1640 if (blk_rq_check_limits(q, rq))
1641 return -EIO;
1642
1643#ifdef CONFIG_FAIL_MAKE_REQUEST
1644 if (rq->rq_disk && rq->rq_disk->part0.make_it_fail &&
1645 should_fail(&fail_make_request, blk_rq_bytes(rq)))
1646 return -EIO;
1647#endif
1648
1649 spin_lock_irqsave(q->queue_lock, flags);
1650
1651 /*
1652 * Submitting request must be dequeued before calling this function
1653 * because it will be linked to another request_queue
1654 */
1655 BUG_ON(blk_queued_rq(rq));
1656
1657 drive_stat_acct(rq, 1);
1658 __elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 0);
1659
1660 spin_unlock_irqrestore(q->queue_lock, flags);
1661
1662 return 0;
1663}
1664EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
1665
Jens Axboebc58ba92009-01-23 10:54:44 +01001666static void blk_account_io_completion(struct request *req, unsigned int bytes)
1667{
Jens Axboec2553b52009-04-24 08:10:11 +02001668 if (blk_do_io_stat(req)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01001669 const int rw = rq_data_dir(req);
1670 struct hd_struct *part;
1671 int cpu;
1672
1673 cpu = part_stat_lock();
Tejun Heo83096eb2009-05-07 22:24:39 +09001674 part = disk_map_sector_rcu(req->rq_disk, blk_rq_pos(req));
Jens Axboebc58ba92009-01-23 10:54:44 +01001675 part_stat_add(cpu, part, sectors[rw], bytes >> 9);
1676 part_stat_unlock();
1677 }
1678}
1679
1680static void blk_account_io_done(struct request *req)
1681{
Jens Axboebc58ba92009-01-23 10:54:44 +01001682 /*
1683 * Account IO completion. bar_rq isn't accounted as a normal
1684 * IO on queueing nor completion. Accounting the containing
1685 * request is enough.
1686 */
Jens Axboec2553b52009-04-24 08:10:11 +02001687 if (blk_do_io_stat(req) && req != &req->q->bar_rq) {
Jens Axboebc58ba92009-01-23 10:54:44 +01001688 unsigned long duration = jiffies - req->start_time;
1689 const int rw = rq_data_dir(req);
1690 struct hd_struct *part;
1691 int cpu;
1692
1693 cpu = part_stat_lock();
Tejun Heo83096eb2009-05-07 22:24:39 +09001694 part = disk_map_sector_rcu(req->rq_disk, blk_rq_pos(req));
Jens Axboebc58ba92009-01-23 10:54:44 +01001695
1696 part_stat_inc(cpu, part, ios[rw]);
1697 part_stat_add(cpu, part, ticks[rw], duration);
1698 part_round_stats(cpu, part);
1699 part_dec_in_flight(part);
1700
1701 part_stat_unlock();
1702 }
1703}
1704
Tejun Heo53a08802008-12-03 12:41:26 +01001705/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09001706 * blk_peek_request - peek at the top of a request queue
1707 * @q: request queue to peek at
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001708 *
1709 * Description:
Tejun Heo9934c8c2009-05-08 11:54:16 +09001710 * Return the request at the top of @q. The returned request
1711 * should be started using blk_start_request() before LLD starts
1712 * processing it.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001713 *
1714 * Return:
Tejun Heo9934c8c2009-05-08 11:54:16 +09001715 * Pointer to the request at the top of @q if available. Null
1716 * otherwise.
1717 *
1718 * Context:
1719 * queue_lock must be held.
1720 */
1721struct request *blk_peek_request(struct request_queue *q)
Tejun Heo158dbda2009-04-23 11:05:18 +09001722{
1723 struct request *rq;
1724 int ret;
1725
1726 while ((rq = __elv_next_request(q)) != NULL) {
1727 if (!(rq->cmd_flags & REQ_STARTED)) {
1728 /*
1729 * This is the first time the device driver
1730 * sees this request (possibly after
1731 * requeueing). Notify IO scheduler.
1732 */
1733 if (blk_sorted_rq(rq))
1734 elv_activate_rq(q, rq);
1735
1736 /*
1737 * just mark as started even if we don't start
1738 * it, a request that has been delayed should
1739 * not be passed by new incoming requests
1740 */
1741 rq->cmd_flags |= REQ_STARTED;
1742 trace_block_rq_issue(q, rq);
1743 }
1744
1745 if (!q->boundary_rq || q->boundary_rq == rq) {
1746 q->end_sector = rq_end_sector(rq);
1747 q->boundary_rq = NULL;
1748 }
1749
1750 if (rq->cmd_flags & REQ_DONTPREP)
1751 break;
1752
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001753 if (q->dma_drain_size && blk_rq_bytes(rq)) {
Tejun Heo158dbda2009-04-23 11:05:18 +09001754 /*
1755 * make sure space for the drain appears we
1756 * know we can do this because max_hw_segments
1757 * has been adjusted to be one fewer than the
1758 * device can handle
1759 */
1760 rq->nr_phys_segments++;
1761 }
1762
1763 if (!q->prep_rq_fn)
1764 break;
1765
1766 ret = q->prep_rq_fn(q, rq);
1767 if (ret == BLKPREP_OK) {
1768 break;
1769 } else if (ret == BLKPREP_DEFER) {
1770 /*
1771 * the request may have been (partially) prepped.
1772 * we need to keep this request in the front to
1773 * avoid resource deadlock. REQ_STARTED will
1774 * prevent other fs requests from passing this one.
1775 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001776 if (q->dma_drain_size && blk_rq_bytes(rq) &&
Tejun Heo158dbda2009-04-23 11:05:18 +09001777 !(rq->cmd_flags & REQ_DONTPREP)) {
1778 /*
1779 * remove the space for the drain we added
1780 * so that we don't add it again
1781 */
1782 --rq->nr_phys_segments;
1783 }
1784
1785 rq = NULL;
1786 break;
1787 } else if (ret == BLKPREP_KILL) {
1788 rq->cmd_flags |= REQ_QUIET;
James Bottomleyc143dc92009-05-30 06:43:49 +02001789 /*
1790 * Mark this request as started so we don't trigger
1791 * any debug logic in the end I/O path.
1792 */
1793 blk_start_request(rq);
Tejun Heo40cbbb72009-04-23 11:05:19 +09001794 __blk_end_request_all(rq, -EIO);
Tejun Heo158dbda2009-04-23 11:05:18 +09001795 } else {
1796 printk(KERN_ERR "%s: bad return=%d\n", __func__, ret);
1797 break;
1798 }
1799 }
1800
1801 return rq;
1802}
Tejun Heo9934c8c2009-05-08 11:54:16 +09001803EXPORT_SYMBOL(blk_peek_request);
Tejun Heo158dbda2009-04-23 11:05:18 +09001804
Tejun Heo9934c8c2009-05-08 11:54:16 +09001805void blk_dequeue_request(struct request *rq)
Tejun Heo158dbda2009-04-23 11:05:18 +09001806{
Tejun Heo9934c8c2009-05-08 11:54:16 +09001807 struct request_queue *q = rq->q;
1808
Tejun Heo158dbda2009-04-23 11:05:18 +09001809 BUG_ON(list_empty(&rq->queuelist));
1810 BUG_ON(ELV_ON_HASH(rq));
1811
1812 list_del_init(&rq->queuelist);
1813
1814 /*
1815 * the time frame between a request being removed from the lists
1816 * and to it is freed is accounted as io that is in progress at
1817 * the driver side.
1818 */
1819 if (blk_account_rq(rq))
Jens Axboe0a7ae2f2009-05-20 08:54:31 +02001820 q->in_flight[rq_is_sync(rq)]++;
Tejun Heo158dbda2009-04-23 11:05:18 +09001821}
1822
Tejun Heo5efccd12009-04-23 11:05:18 +09001823/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09001824 * blk_start_request - start request processing on the driver
1825 * @req: request to dequeue
1826 *
1827 * Description:
1828 * Dequeue @req and start timeout timer on it. This hands off the
1829 * request to the driver.
1830 *
1831 * Block internal functions which don't want to start timer should
1832 * call blk_dequeue_request().
1833 *
1834 * Context:
1835 * queue_lock must be held.
1836 */
1837void blk_start_request(struct request *req)
1838{
1839 blk_dequeue_request(req);
1840
1841 /*
Tejun Heo5f49f632009-05-19 18:33:05 +09001842 * We are now handing the request to the hardware, initialize
1843 * resid_len to full count and add the timeout handler.
Tejun Heo9934c8c2009-05-08 11:54:16 +09001844 */
Tejun Heo5f49f632009-05-19 18:33:05 +09001845 req->resid_len = blk_rq_bytes(req);
FUJITA Tomonoridbb66c42009-06-09 05:47:10 +02001846 if (unlikely(blk_bidi_rq(req)))
1847 req->next_rq->resid_len = blk_rq_bytes(req->next_rq);
1848
Tejun Heo9934c8c2009-05-08 11:54:16 +09001849 blk_add_timer(req);
1850}
1851EXPORT_SYMBOL(blk_start_request);
1852
1853/**
1854 * blk_fetch_request - fetch a request from a request queue
1855 * @q: request queue to fetch a request from
1856 *
1857 * Description:
1858 * Return the request at the top of @q. The request is started on
1859 * return and LLD can start processing it immediately.
1860 *
1861 * Return:
1862 * Pointer to the request at the top of @q if available. Null
1863 * otherwise.
1864 *
1865 * Context:
1866 * queue_lock must be held.
1867 */
1868struct request *blk_fetch_request(struct request_queue *q)
1869{
1870 struct request *rq;
1871
1872 rq = blk_peek_request(q);
1873 if (rq)
1874 blk_start_request(rq);
1875 return rq;
1876}
1877EXPORT_SYMBOL(blk_fetch_request);
1878
1879/**
Tejun Heo2e60e022009-04-23 11:05:18 +09001880 * blk_update_request - Special helper function for request stacking drivers
Randy Dunlap8ebf9752009-06-11 20:00:41 -07001881 * @req: the request being processed
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001882 * @error: %0 for success, < %0 for error
Randy Dunlap8ebf9752009-06-11 20:00:41 -07001883 * @nr_bytes: number of bytes to complete @req
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001884 *
1885 * Description:
Randy Dunlap8ebf9752009-06-11 20:00:41 -07001886 * Ends I/O on a number of bytes attached to @req, but doesn't complete
1887 * the request structure even if @req doesn't have leftover.
1888 * If @req has leftover, sets it up for the next range of segments.
Tejun Heo2e60e022009-04-23 11:05:18 +09001889 *
1890 * This special helper function is only for request stacking drivers
1891 * (e.g. request-based dm) so that they can handle partial completion.
1892 * Actual device drivers should use blk_end_request instead.
1893 *
1894 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
1895 * %false return from this function.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001896 *
1897 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09001898 * %false - this request doesn't have any more data
1899 * %true - this request has more data
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001900 **/
Tejun Heo2e60e022009-04-23 11:05:18 +09001901bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902{
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001903 int total_bytes, bio_nbytes, next_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 struct bio *bio;
1905
Tejun Heo2e60e022009-04-23 11:05:18 +09001906 if (!req->bio)
1907 return false;
1908
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001909 trace_block_rq_complete(req->q, req);
Jens Axboe2056a782006-03-23 20:00:26 +01001910
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 /*
Tejun Heo6f414692009-04-19 07:00:41 +09001912 * For fs requests, rq is just carrier of independent bio's
1913 * and each partial completion should be handled separately.
1914 * Reset per-request error on each partial completion.
1915 *
1916 * TODO: tj: This is too subtle. It would be better to let
1917 * low level drivers do what they see fit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 */
Tejun Heo6f414692009-04-19 07:00:41 +09001919 if (blk_fs_request(req))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 req->errors = 0;
1921
Jens Axboe6728cb02008-01-31 13:03:55 +01001922 if (error && (blk_fs_request(req) && !(req->cmd_flags & REQ_QUIET))) {
1923 printk(KERN_ERR "end_request: I/O error, dev %s, sector %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 req->rq_disk ? req->rq_disk->disk_name : "?",
Tejun Heo83096eb2009-05-07 22:24:39 +09001925 (unsigned long long)blk_rq_pos(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 }
1927
Jens Axboebc58ba92009-01-23 10:54:44 +01001928 blk_account_io_completion(req, nr_bytes);
Jens Axboed72d9042005-11-01 08:35:42 +01001929
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 total_bytes = bio_nbytes = 0;
1931 while ((bio = req->bio) != NULL) {
1932 int nbytes;
1933
1934 if (nr_bytes >= bio->bi_size) {
1935 req->bio = bio->bi_next;
1936 nbytes = bio->bi_size;
NeilBrown5bb23a62007-09-27 12:46:13 +02001937 req_bio_endio(req, bio, nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 next_idx = 0;
1939 bio_nbytes = 0;
1940 } else {
1941 int idx = bio->bi_idx + next_idx;
1942
Kazuhisa Ichikawaaf498d72009-05-12 13:27:45 +02001943 if (unlikely(idx >= bio->bi_vcnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 blk_dump_rq_flags(req, "__end_that");
Jens Axboe6728cb02008-01-31 13:03:55 +01001945 printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
Kazuhisa Ichikawaaf498d72009-05-12 13:27:45 +02001946 __func__, idx, bio->bi_vcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 break;
1948 }
1949
1950 nbytes = bio_iovec_idx(bio, idx)->bv_len;
1951 BIO_BUG_ON(nbytes > bio->bi_size);
1952
1953 /*
1954 * not a complete bvec done
1955 */
1956 if (unlikely(nbytes > nr_bytes)) {
1957 bio_nbytes += nr_bytes;
1958 total_bytes += nr_bytes;
1959 break;
1960 }
1961
1962 /*
1963 * advance to the next vector
1964 */
1965 next_idx++;
1966 bio_nbytes += nbytes;
1967 }
1968
1969 total_bytes += nbytes;
1970 nr_bytes -= nbytes;
1971
Jens Axboe6728cb02008-01-31 13:03:55 +01001972 bio = req->bio;
1973 if (bio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 /*
1975 * end more in this run, or just return 'not-done'
1976 */
1977 if (unlikely(nr_bytes <= 0))
1978 break;
1979 }
1980 }
1981
1982 /*
1983 * completely done
1984 */
Tejun Heo2e60e022009-04-23 11:05:18 +09001985 if (!req->bio) {
1986 /*
1987 * Reset counters so that the request stacking driver
1988 * can find how many bytes remain in the request
1989 * later.
1990 */
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001991 req->__data_len = 0;
Tejun Heo2e60e022009-04-23 11:05:18 +09001992 return false;
1993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
1995 /*
1996 * if the request wasn't completed, update state
1997 */
1998 if (bio_nbytes) {
NeilBrown5bb23a62007-09-27 12:46:13 +02001999 req_bio_endio(req, bio, bio_nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 bio->bi_idx += next_idx;
2001 bio_iovec(bio)->bv_offset += nr_bytes;
2002 bio_iovec(bio)->bv_len -= nr_bytes;
2003 }
2004
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002005 req->__data_len -= total_bytes;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002006 req->buffer = bio_data(req->bio);
2007
2008 /* update sector only for requests with clear definition of sector */
2009 if (blk_fs_request(req) || blk_discard_rq(req))
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002010 req->__sector += total_bytes >> 9;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002011
2012 /*
2013 * If total number of sectors is less than the first segment
2014 * size, something has gone terribly wrong.
2015 */
2016 if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
2017 printk(KERN_ERR "blk: request botched\n");
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002018 req->__data_len = blk_rq_cur_bytes(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002019 }
2020
2021 /* recalculate the number of segments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 blk_recalc_rq_segments(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002023
Tejun Heo2e60e022009-04-23 11:05:18 +09002024 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025}
Tejun Heo2e60e022009-04-23 11:05:18 +09002026EXPORT_SYMBOL_GPL(blk_update_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Tejun Heo2e60e022009-04-23 11:05:18 +09002028static bool blk_update_bidi_request(struct request *rq, int error,
2029 unsigned int nr_bytes,
2030 unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002031{
Tejun Heo2e60e022009-04-23 11:05:18 +09002032 if (blk_update_request(rq, error, nr_bytes))
2033 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002034
Tejun Heo2e60e022009-04-23 11:05:18 +09002035 /* Bidi request must be completed as a whole */
2036 if (unlikely(blk_bidi_rq(rq)) &&
2037 blk_update_request(rq->next_rq, error, bidi_bytes))
2038 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002039
Tejun Heo2e60e022009-04-23 11:05:18 +09002040 add_disk_randomness(rq->rq_disk);
2041
2042 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043}
2044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045/*
2046 * queue lock must be held
2047 */
Tejun Heo2e60e022009-04-23 11:05:18 +09002048static void blk_finish_request(struct request *req, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002050 if (blk_rq_tagged(req))
2051 blk_queue_end_tag(req->q, req);
2052
James Bottomleyba396a62009-05-27 14:17:08 +02002053 BUG_ON(blk_queued_rq(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
2055 if (unlikely(laptop_mode) && blk_fs_request(req))
2056 laptop_io_completion();
2057
Mike Andersone78042e2008-10-30 02:16:20 -07002058 blk_delete_timer(req);
2059
Jens Axboebc58ba92009-01-23 10:54:44 +01002060 blk_account_io_done(req);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 if (req->end_io)
Tejun Heo8ffdc652006-01-06 09:49:03 +01002063 req->end_io(req, error);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002064 else {
2065 if (blk_bidi_rq(req))
2066 __blk_put_request(req->next_rq->q, req->next_rq);
2067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 __blk_put_request(req->q, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 }
2070}
2071
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05002072/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002073 * blk_end_bidi_request - Complete a bidi request
2074 * @rq: the request to complete
Randy Dunlap710027a2008-08-19 20:13:11 +02002075 * @error: %0 for success, < %0 for error
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002076 * @nr_bytes: number of bytes to complete @rq
2077 * @bidi_bytes: number of bytes to complete @rq->next_rq
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002078 *
2079 * Description:
2080 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
Tejun Heo2e60e022009-04-23 11:05:18 +09002081 * Drivers that supports bidi can safely call this member for any
2082 * type of request, bidi or uni. In the later case @bidi_bytes is
2083 * just ignored.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002084 *
2085 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002086 * %false - we are done with this request
2087 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002088 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002089static bool blk_end_bidi_request(struct request *rq, int error,
2090 unsigned int nr_bytes, unsigned int bidi_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002091{
2092 struct request_queue *q = rq->q;
Tejun Heo2e60e022009-04-23 11:05:18 +09002093 unsigned long flags;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002094
Tejun Heo2e60e022009-04-23 11:05:18 +09002095 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2096 return true;
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002097
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002098 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo2e60e022009-04-23 11:05:18 +09002099 blk_finish_request(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002100 spin_unlock_irqrestore(q->queue_lock, flags);
2101
Tejun Heo2e60e022009-04-23 11:05:18 +09002102 return false;
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002103}
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002104
2105/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002106 * __blk_end_bidi_request - Complete a bidi request with queue lock held
2107 * @rq: the request to complete
2108 * @error: %0 for success, < %0 for error
2109 * @nr_bytes: number of bytes to complete @rq
2110 * @bidi_bytes: number of bytes to complete @rq->next_rq
Tejun Heo5efccd12009-04-23 11:05:18 +09002111 *
2112 * Description:
Tejun Heo2e60e022009-04-23 11:05:18 +09002113 * Identical to blk_end_bidi_request() except that queue lock is
2114 * assumed to be locked on entry and remains so on return.
Tejun Heo5efccd12009-04-23 11:05:18 +09002115 *
Tejun Heo2e60e022009-04-23 11:05:18 +09002116 * Return:
2117 * %false - we are done with this request
2118 * %true - still buffers pending for this request
Tejun Heo5efccd12009-04-23 11:05:18 +09002119 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002120static bool __blk_end_bidi_request(struct request *rq, int error,
2121 unsigned int nr_bytes, unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002122{
Tejun Heo2e60e022009-04-23 11:05:18 +09002123 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2124 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002125
Tejun Heo2e60e022009-04-23 11:05:18 +09002126 blk_finish_request(rq, error);
Tejun Heo5efccd12009-04-23 11:05:18 +09002127
Tejun Heo2e60e022009-04-23 11:05:18 +09002128 return false;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002129}
2130
2131/**
2132 * blk_end_request - Helper function for drivers to complete the request.
2133 * @rq: the request being processed
2134 * @error: %0 for success, < %0 for error
2135 * @nr_bytes: number of bytes to complete
2136 *
2137 * Description:
2138 * Ends I/O on a number of bytes attached to @rq.
2139 * If @rq has leftover, sets it up for the next range of segments.
2140 *
2141 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002142 * %false - we are done with this request
2143 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002144 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002145bool blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002146{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002147 return blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002148}
2149EXPORT_SYMBOL_GPL(blk_end_request);
2150
2151/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002152 * blk_end_request_all - Helper function for drives to finish the request.
2153 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002154 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002155 *
2156 * Description:
2157 * Completely finish @rq.
2158 */
2159void blk_end_request_all(struct request *rq, int error)
2160{
2161 bool pending;
2162 unsigned int bidi_bytes = 0;
2163
2164 if (unlikely(blk_bidi_rq(rq)))
2165 bidi_bytes = blk_rq_bytes(rq->next_rq);
2166
2167 pending = blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2168 BUG_ON(pending);
2169}
2170EXPORT_SYMBOL_GPL(blk_end_request_all);
2171
2172/**
2173 * blk_end_request_cur - Helper function to finish the current request chunk.
2174 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002175 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002176 *
2177 * Description:
2178 * Complete the current consecutively mapped chunk from @rq.
2179 *
2180 * Return:
2181 * %false - we are done with this request
2182 * %true - still buffers pending for this request
2183 */
2184bool blk_end_request_cur(struct request *rq, int error)
2185{
2186 return blk_end_request(rq, error, blk_rq_cur_bytes(rq));
2187}
2188EXPORT_SYMBOL_GPL(blk_end_request_cur);
2189
2190/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002191 * __blk_end_request - Helper function for drivers to complete the request.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002192 * @rq: the request being processed
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002193 * @error: %0 for success, < %0 for error
2194 * @nr_bytes: number of bytes to complete
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002195 *
2196 * Description:
2197 * Must be called with queue lock held unlike blk_end_request().
2198 *
2199 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002200 * %false - we are done with this request
2201 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002202 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002203bool __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002204{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002205 return __blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002206}
2207EXPORT_SYMBOL_GPL(__blk_end_request);
2208
2209/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002210 * __blk_end_request_all - Helper function for drives to finish the request.
2211 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002212 * @error: %0 for success, < %0 for error
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002213 *
2214 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002215 * Completely finish @rq. Must be called with queue lock held.
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002216 */
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002217void __blk_end_request_all(struct request *rq, int error)
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002218{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002219 bool pending;
2220 unsigned int bidi_bytes = 0;
2221
2222 if (unlikely(blk_bidi_rq(rq)))
2223 bidi_bytes = blk_rq_bytes(rq->next_rq);
2224
2225 pending = __blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2226 BUG_ON(pending);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002227}
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002228EXPORT_SYMBOL_GPL(__blk_end_request_all);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002229
2230/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002231 * __blk_end_request_cur - Helper function to finish the current request chunk.
2232 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002233 * @error: %0 for success, < %0 for error
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002234 *
2235 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002236 * Complete the current consecutively mapped chunk from @rq. Must
2237 * be called with queue lock held.
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002238 *
2239 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002240 * %false - we are done with this request
2241 * %true - still buffers pending for this request
2242 */
2243bool __blk_end_request_cur(struct request *rq, int error)
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002244{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002245 return __blk_end_request(rq, error, blk_rq_cur_bytes(rq));
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002246}
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002247EXPORT_SYMBOL_GPL(__blk_end_request_cur);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002248
Jens Axboe86db1e22008-01-29 14:53:40 +01002249void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2250 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251{
David Woodhoused628eae2008-08-09 16:22:17 +01002252 /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw, and
2253 we want BIO_RW_AHEAD (bit 1) to imply REQ_FAILFAST (bit 1). */
Jens Axboe4aff5e22006-08-10 08:44:47 +02002254 rq->cmd_flags |= (bio->bi_rw & 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
David Woodhousefb2dce82008-08-05 18:01:53 +01002256 if (bio_has_data(bio)) {
2257 rq->nr_phys_segments = bio_phys_segments(q, bio);
David Woodhousefb2dce82008-08-05 18:01:53 +01002258 rq->buffer = bio_data(bio);
2259 }
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002260 rq->__data_len = bio->bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 rq->bio = rq->biotail = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
NeilBrown66846572007-08-16 13:31:28 +02002263 if (bio->bi_bdev)
2264 rq->rq_disk = bio->bi_bdev->bd_disk;
2265}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Kiyoshi Uedaef9e3fa2008-10-01 16:12:15 +02002267/**
2268 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
2269 * @q : the queue of the device being checked
2270 *
2271 * Description:
2272 * Check if underlying low-level drivers of a device are busy.
2273 * If the drivers want to export their busy state, they must set own
2274 * exporting function using blk_queue_lld_busy() first.
2275 *
2276 * Basically, this function is used only by request stacking drivers
2277 * to stop dispatching requests to underlying devices when underlying
2278 * devices are busy. This behavior helps more I/O merging on the queue
2279 * of the request stacking driver and prevents I/O throughput regression
2280 * on burst I/O load.
2281 *
2282 * Return:
2283 * 0 - Not busy (The request stacking driver should dispatch request)
2284 * 1 - Busy (The request stacking driver should stop dispatching request)
2285 */
2286int blk_lld_busy(struct request_queue *q)
2287{
2288 if (q->lld_busy_fn)
2289 return q->lld_busy_fn(q);
2290
2291 return 0;
2292}
2293EXPORT_SYMBOL_GPL(blk_lld_busy);
2294
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002295/**
2296 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
2297 * @rq: the clone request to be cleaned up
2298 *
2299 * Description:
2300 * Free all bios in @rq for a cloned request.
2301 */
2302void blk_rq_unprep_clone(struct request *rq)
2303{
2304 struct bio *bio;
2305
2306 while ((bio = rq->bio) != NULL) {
2307 rq->bio = bio->bi_next;
2308
2309 bio_put(bio);
2310 }
2311}
2312EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
2313
2314/*
2315 * Copy attributes of the original request to the clone request.
2316 * The actual data parts (e.g. ->cmd, ->buffer, ->sense) are not copied.
2317 */
2318static void __blk_rq_prep_clone(struct request *dst, struct request *src)
2319{
2320 dst->cpu = src->cpu;
2321 dst->cmd_flags = (rq_data_dir(src) | REQ_NOMERGE);
2322 dst->cmd_type = src->cmd_type;
2323 dst->__sector = blk_rq_pos(src);
2324 dst->__data_len = blk_rq_bytes(src);
2325 dst->nr_phys_segments = src->nr_phys_segments;
2326 dst->ioprio = src->ioprio;
2327 dst->extra_len = src->extra_len;
2328}
2329
2330/**
2331 * blk_rq_prep_clone - Helper function to setup clone request
2332 * @rq: the request to be setup
2333 * @rq_src: original request to be cloned
2334 * @bs: bio_set that bios for clone are allocated from
2335 * @gfp_mask: memory allocation mask for bio
2336 * @bio_ctr: setup function to be called for each clone bio.
2337 * Returns %0 for success, non %0 for failure.
2338 * @data: private data to be passed to @bio_ctr
2339 *
2340 * Description:
2341 * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
2342 * The actual data parts of @rq_src (e.g. ->cmd, ->buffer, ->sense)
2343 * are not copied, and copying such parts is the caller's responsibility.
2344 * Also, pages which the original bios are pointing to are not copied
2345 * and the cloned bios just point same pages.
2346 * So cloned bios must be completed before original bios, which means
2347 * the caller must complete @rq before @rq_src.
2348 */
2349int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
2350 struct bio_set *bs, gfp_t gfp_mask,
2351 int (*bio_ctr)(struct bio *, struct bio *, void *),
2352 void *data)
2353{
2354 struct bio *bio, *bio_src;
2355
2356 if (!bs)
2357 bs = fs_bio_set;
2358
2359 blk_rq_init(NULL, rq);
2360
2361 __rq_for_each_bio(bio_src, rq_src) {
2362 bio = bio_alloc_bioset(gfp_mask, bio_src->bi_max_vecs, bs);
2363 if (!bio)
2364 goto free_and_out;
2365
2366 __bio_clone(bio, bio_src);
2367
2368 if (bio_integrity(bio_src) &&
2369 bio_integrity_clone(bio, bio_src, gfp_mask))
2370 goto free_and_out;
2371
2372 if (bio_ctr && bio_ctr(bio, bio_src, data))
2373 goto free_and_out;
2374
2375 if (rq->bio) {
2376 rq->biotail->bi_next = bio;
2377 rq->biotail = bio;
2378 } else
2379 rq->bio = rq->biotail = bio;
2380 }
2381
2382 __blk_rq_prep_clone(rq, rq_src);
2383
2384 return 0;
2385
2386free_and_out:
2387 if (bio)
2388 bio_free(bio, bs);
2389 blk_rq_unprep_clone(rq);
2390
2391 return -ENOMEM;
2392}
2393EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
2394
Jens Axboe18887ad2008-07-28 13:08:45 +02002395int kblockd_schedule_work(struct request_queue *q, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396{
2397 return queue_work(kblockd_workqueue, work);
2398}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399EXPORT_SYMBOL(kblockd_schedule_work);
2400
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401int __init blk_dev_init(void)
2402{
Nikanth Karthikesan9eb55b02009-04-27 14:53:54 +02002403 BUILD_BUG_ON(__REQ_NR_BITS > 8 *
2404 sizeof(((struct request *)0)->cmd_flags));
2405
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 kblockd_workqueue = create_workqueue("kblockd");
2407 if (!kblockd_workqueue)
2408 panic("Failed to create kblockd\n");
2409
2410 request_cachep = kmem_cache_create("blkdev_requests",
Paul Mundt20c2df82007-07-20 10:11:58 +09002411 sizeof(struct request), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
Jens Axboe8324aa92008-01-29 14:51:59 +01002413 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02002414 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 return 0;
2417}
2418