blob: d2f23ec5ebfa5e49e39e1613d6526ef2665df844 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 1994, Karl Keyte: Added support for disk statistics
4 * Elevator latency, (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
5 * Queue request tables / lock, selectable elevator, Jens Axboe <axboe@suse.de>
Jens Axboe6728cb02008-01-31 13:03:55 +01006 * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au>
7 * - July2000
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
9 */
10
11/*
12 * This handles all read/write requests to block devices
13 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/backing-dev.h>
17#include <linux/bio.h>
18#include <linux/blkdev.h>
19#include <linux/highmem.h>
20#include <linux/mm.h>
21#include <linux/kernel_stat.h>
22#include <linux/string.h>
23#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/completion.h>
25#include <linux/slab.h>
26#include <linux/swap.h>
27#include <linux/writeback.h>
Andrew Mortonfaccbd42006-12-10 02:19:35 -080028#include <linux/task_io_accounting_ops.h>
Jens Axboeff856ba2006-01-09 16:02:34 +010029#include <linux/interrupt.h>
30#include <linux/cpu.h>
Jens Axboe2056a782006-03-23 20:00:26 +010031#include <linux/blktrace_api.h>
Akinobu Mitac17bb492006-12-08 02:39:46 -080032#include <linux/fault-inject.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Jens Axboe8324aa92008-01-29 14:51:59 +010034#include "blk.h"
35
Jens Axboe165125e2007-07-24 09:28:11 +020036static int __make_request(struct request_queue *q, struct bio *bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/*
39 * For the allocated request tables
40 */
Adrian Bunk5ece6c52008-02-18 13:45:51 +010041static struct kmem_cache *request_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/*
44 * For queue allocation
45 */
Jens Axboe6728cb02008-01-31 13:03:55 +010046struct kmem_cache *blk_requestq_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * Controlling structure to kblockd
50 */
Jens Axboeff856ba2006-01-09 16:02:34 +010051static struct workqueue_struct *kblockd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Jens Axboeff856ba2006-01-09 16:02:34 +010053static DEFINE_PER_CPU(struct list_head, blk_cpu_done);
54
Jens Axboe26b82562008-01-29 13:54:41 +010055static void drive_stat_acct(struct request *rq, int new_io)
56{
57 int rw = rq_data_dir(rq);
58
59 if (!blk_fs_request(rq) || !rq->rq_disk)
60 return;
61
62 if (!new_io) {
Jerome Marchand6f2576a2008-02-08 11:04:35 +010063 __all_stat_inc(rq->rq_disk, merges[rw], rq->sector);
Jens Axboe26b82562008-01-29 13:54:41 +010064 } else {
Jerome Marchand6f2576a2008-02-08 11:04:35 +010065 struct hd_struct *part = get_part(rq->rq_disk, rq->sector);
Jens Axboe26b82562008-01-29 13:54:41 +010066 disk_round_stats(rq->rq_disk);
67 rq->rq_disk->in_flight++;
Jerome Marchand6f2576a2008-02-08 11:04:35 +010068 if (part) {
69 part_round_stats(part);
70 part->in_flight++;
71 }
Jens Axboe26b82562008-01-29 13:54:41 +010072 }
73}
74
Jens Axboe8324aa92008-01-29 14:51:59 +010075void blk_queue_congestion_threshold(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
77 int nr;
78
79 nr = q->nr_requests - (q->nr_requests / 8) + 1;
80 if (nr > q->nr_requests)
81 nr = q->nr_requests;
82 q->nr_congestion_on = nr;
83
84 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
85 if (nr < 1)
86 nr = 1;
87 q->nr_congestion_off = nr;
88}
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/**
91 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
92 * @bdev: device
93 *
94 * Locates the passed device's request queue and returns the address of its
95 * backing_dev_info
96 *
97 * Will return NULL if the request queue cannot be located.
98 */
99struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
100{
101 struct backing_dev_info *ret = NULL;
Jens Axboe165125e2007-07-24 09:28:11 +0200102 struct request_queue *q = bdev_get_queue(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 if (q)
105 ret = &q->backing_dev_info;
106 return ret;
107}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108EXPORT_SYMBOL(blk_get_backing_dev_info);
109
Jens Axboe86db1e22008-01-29 14:53:40 +0100110void rq_init(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200112 memset(rq, 0, sizeof(*rq));
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 INIT_LIST_HEAD(&rq->queuelist);
Jens Axboeff856ba2006-01-09 16:02:34 +0100115 INIT_LIST_HEAD(&rq->donelist);
Jens Axboe63a71382008-02-08 12:41:03 +0100116 rq->q = q;
117 rq->sector = rq->hard_sector = (sector_t) -1;
Jens Axboe2e662b62006-07-13 11:55:04 +0200118 INIT_HLIST_NODE(&rq->hash);
119 RB_CLEAR_NODE(&rq->rb_node);
Jens Axboe63a71382008-02-08 12:41:03 +0100120 rq->tag = -1;
Jens Axboe63a71382008-02-08 12:41:03 +0100121 rq->ref_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
NeilBrown5bb23a62007-09-27 12:46:13 +0200124static void req_bio_endio(struct request *rq, struct bio *bio,
125 unsigned int nbytes, int error)
Tejun Heo797e7db2006-01-06 09:51:03 +0100126{
Jens Axboe165125e2007-07-24 09:28:11 +0200127 struct request_queue *q = rq->q;
Tejun Heo797e7db2006-01-06 09:51:03 +0100128
NeilBrown5bb23a62007-09-27 12:46:13 +0200129 if (&q->bar_rq != rq) {
130 if (error)
131 clear_bit(BIO_UPTODATE, &bio->bi_flags);
132 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
133 error = -EIO;
Tejun Heo797e7db2006-01-06 09:51:03 +0100134
NeilBrown5bb23a62007-09-27 12:46:13 +0200135 if (unlikely(nbytes > bio->bi_size)) {
Jens Axboe6728cb02008-01-31 13:03:55 +0100136 printk(KERN_ERR "%s: want %u bytes done, %u left\n",
NeilBrown5bb23a62007-09-27 12:46:13 +0200137 __FUNCTION__, nbytes, bio->bi_size);
138 nbytes = bio->bi_size;
139 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100140
NeilBrown5bb23a62007-09-27 12:46:13 +0200141 bio->bi_size -= nbytes;
142 bio->bi_sector += (nbytes >> 9);
143 if (bio->bi_size == 0)
NeilBrown6712ecf2007-09-27 12:47:43 +0200144 bio_endio(bio, error);
NeilBrown5bb23a62007-09-27 12:46:13 +0200145 } else {
146
147 /*
148 * Okay, this is the barrier request in progress, just
149 * record the error;
150 */
151 if (error && !q->orderr)
152 q->orderr = error;
153 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100154}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156void blk_dump_rq_flags(struct request *rq, char *msg)
157{
158 int bit;
159
Jens Axboe6728cb02008-01-31 13:03:55 +0100160 printk(KERN_INFO "%s: dev %s: type=%x, flags=%x\n", msg,
Jens Axboe4aff5e22006-08-10 08:44:47 +0200161 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
162 rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Jens Axboe6728cb02008-01-31 13:03:55 +0100164 printk(KERN_INFO " sector %llu, nr/cnr %lu/%u\n",
165 (unsigned long long)rq->sector,
166 rq->nr_sectors,
167 rq->current_nr_sectors);
168 printk(KERN_INFO " bio %p, biotail %p, buffer %p, data %p, len %u\n",
169 rq->bio, rq->biotail,
170 rq->buffer, rq->data,
171 rq->data_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Jens Axboe4aff5e22006-08-10 08:44:47 +0200173 if (blk_pc_request(rq)) {
Jens Axboe6728cb02008-01-31 13:03:55 +0100174 printk(KERN_INFO " cdb: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 for (bit = 0; bit < sizeof(rq->cmd); bit++)
176 printk("%02x ", rq->cmd[bit]);
177 printk("\n");
178 }
179}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180EXPORT_SYMBOL(blk_dump_rq_flags);
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/*
183 * "plug" the device if there are no outstanding requests: this will
184 * force the transfer to start only after we have put all the requests
185 * on the list.
186 *
187 * This is called with interrupts off and no requests on the queue and
188 * with the queue lock held.
189 */
Jens Axboe165125e2007-07-24 09:28:11 +0200190void blk_plug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 WARN_ON(!irqs_disabled());
193
194 /*
195 * don't plug a stopped queue, it must be paired with blk_start_queue()
196 * which will restart the queueing
197 */
Coywolf Qi Hunt7daac492006-04-19 10:14:49 +0200198 if (blk_queue_stopped(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return;
200
Nick Piggin75ad23b2008-04-29 14:48:33 +0200201 if (!test_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags)) {
202 __set_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
Jens Axboe2056a782006-03-23 20:00:26 +0100204 blk_add_trace_generic(q, NULL, 0, BLK_TA_PLUG);
205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207EXPORT_SYMBOL(blk_plug_device);
208
209/*
210 * remove the queue from the plugged list, if present. called with
211 * queue lock held and interrupts disabled.
212 */
Jens Axboe165125e2007-07-24 09:28:11 +0200213int blk_remove_plug(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 WARN_ON(!irqs_disabled());
216
Nick Piggin75ad23b2008-04-29 14:48:33 +0200217 if (!test_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return 0;
219
Nick Piggin75ad23b2008-04-29 14:48:33 +0200220 queue_flag_clear(QUEUE_FLAG_PLUGGED, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 del_timer(&q->unplug_timer);
222 return 1;
223}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224EXPORT_SYMBOL(blk_remove_plug);
225
226/*
227 * remove the plug and let it rip..
228 */
Jens Axboe165125e2007-07-24 09:28:11 +0200229void __generic_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Coywolf Qi Hunt7daac492006-04-19 10:14:49 +0200231 if (unlikely(blk_queue_stopped(q)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return;
233
234 if (!blk_remove_plug(q))
235 return;
236
Jens Axboe22e2c502005-06-27 10:55:12 +0200237 q->request_fn(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239EXPORT_SYMBOL(__generic_unplug_device);
240
241/**
242 * generic_unplug_device - fire a request queue
Jens Axboe165125e2007-07-24 09:28:11 +0200243 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 *
245 * Description:
246 * Linux uses plugging to build bigger requests queues before letting
247 * the device have at them. If a queue is plugged, the I/O scheduler
248 * is still adding and merging requests on the queue. Once the queue
249 * gets unplugged, the request_fn defined for the queue is invoked and
250 * transfers started.
251 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200252void generic_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 spin_lock_irq(q->queue_lock);
255 __generic_unplug_device(q);
256 spin_unlock_irq(q->queue_lock);
257}
258EXPORT_SYMBOL(generic_unplug_device);
259
260static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
261 struct page *page)
262{
Jens Axboe165125e2007-07-24 09:28:11 +0200263 struct request_queue *q = bdi->unplug_io_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500265 blk_unplug(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
Jens Axboe86db1e22008-01-29 14:53:40 +0100268void blk_unplug_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Jens Axboe165125e2007-07-24 09:28:11 +0200270 struct request_queue *q =
271 container_of(work, struct request_queue, unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Jens Axboe2056a782006-03-23 20:00:26 +0100273 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
274 q->rq.count[READ] + q->rq.count[WRITE]);
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 q->unplug_fn(q);
277}
278
Jens Axboe86db1e22008-01-29 14:53:40 +0100279void blk_unplug_timeout(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Jens Axboe165125e2007-07-24 09:28:11 +0200281 struct request_queue *q = (struct request_queue *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Jens Axboe2056a782006-03-23 20:00:26 +0100283 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_TIMER, NULL,
284 q->rq.count[READ] + q->rq.count[WRITE]);
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 kblockd_schedule_work(&q->unplug_work);
287}
288
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500289void blk_unplug(struct request_queue *q)
290{
291 /*
292 * devices don't necessarily have an ->unplug_fn defined
293 */
294 if (q->unplug_fn) {
295 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
296 q->rq.count[READ] + q->rq.count[WRITE]);
297
298 q->unplug_fn(q);
299 }
300}
301EXPORT_SYMBOL(blk_unplug);
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303/**
304 * blk_start_queue - restart a previously stopped queue
Jens Axboe165125e2007-07-24 09:28:11 +0200305 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 *
307 * Description:
308 * blk_start_queue() will clear the stop flag on the queue, and call
309 * the request_fn for the queue if it was in a stopped state when
310 * entered. Also see blk_stop_queue(). Queue lock must be held.
311 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200312void blk_start_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200314 WARN_ON(!irqs_disabled());
315
Nick Piggin75ad23b2008-04-29 14:48:33 +0200316 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 /*
319 * one level of recursion is ok and is much faster than kicking
320 * the unplug handling
321 */
Nick Piggin75ad23b2008-04-29 14:48:33 +0200322 if (!test_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) {
323 queue_flag_set(QUEUE_FLAG_REENTER, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 q->request_fn(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200325 queue_flag_clear(QUEUE_FLAG_REENTER, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 } else {
327 blk_plug_device(q);
328 kblockd_schedule_work(&q->unplug_work);
329 }
330}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331EXPORT_SYMBOL(blk_start_queue);
332
333/**
334 * blk_stop_queue - stop a queue
Jens Axboe165125e2007-07-24 09:28:11 +0200335 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 *
337 * Description:
338 * The Linux block layer assumes that a block driver will consume all
339 * entries on the request queue when the request_fn strategy is called.
340 * Often this will not happen, because of hardware limitations (queue
341 * depth settings). If a device driver gets a 'queue full' response,
342 * or if it simply chooses not to queue more I/O at one point, it can
343 * call this function to prevent the request_fn from being called until
344 * the driver has signalled it's ready to go again. This happens by calling
345 * blk_start_queue() to restart queue operations. Queue lock must be held.
346 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200347void blk_stop_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 blk_remove_plug(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200350 queue_flag_set(QUEUE_FLAG_STOPPED, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352EXPORT_SYMBOL(blk_stop_queue);
353
354/**
355 * blk_sync_queue - cancel any pending callbacks on a queue
356 * @q: the queue
357 *
358 * Description:
359 * The block layer may perform asynchronous callback activity
360 * on a queue, such as calling the unplug function after a timeout.
361 * A block device may call blk_sync_queue to ensure that any
362 * such activity is cancelled, thus allowing it to release resources
Michael Opdenacker59c51592007-05-09 08:57:56 +0200363 * that the callbacks might use. The caller must already have made sure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 * that its ->make_request_fn will not re-add plugging prior to calling
365 * this function.
366 *
367 */
368void blk_sync_queue(struct request_queue *q)
369{
370 del_timer_sync(&q->unplug_timer);
Oleg Nesterovabbeb882007-10-23 15:08:19 +0200371 kblockd_flush_work(&q->unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373EXPORT_SYMBOL(blk_sync_queue);
374
375/**
376 * blk_run_queue - run a single device queue
377 * @q: The queue to run
378 */
Nick Piggin75ad23b2008-04-29 14:48:33 +0200379void __blk_run_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 blk_remove_plug(q);
Jens Axboedac07ec2006-05-11 08:20:16 +0200382
383 /*
384 * Only recurse once to avoid overrunning the stack, let the unplug
385 * handling reinvoke the handler shortly if we already got there.
386 */
387 if (!elv_queue_empty(q)) {
Nick Piggin75ad23b2008-04-29 14:48:33 +0200388 if (!test_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) {
389 queue_flag_set(QUEUE_FLAG_REENTER, q);
Jens Axboedac07ec2006-05-11 08:20:16 +0200390 q->request_fn(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200391 queue_flag_clear(QUEUE_FLAG_REENTER, q);
Jens Axboedac07ec2006-05-11 08:20:16 +0200392 } else {
393 blk_plug_device(q);
394 kblockd_schedule_work(&q->unplug_work);
395 }
396 }
Nick Piggin75ad23b2008-04-29 14:48:33 +0200397}
398EXPORT_SYMBOL(__blk_run_queue);
Jens Axboedac07ec2006-05-11 08:20:16 +0200399
Nick Piggin75ad23b2008-04-29 14:48:33 +0200400/**
401 * blk_run_queue - run a single device queue
402 * @q: The queue to run
403 */
404void blk_run_queue(struct request_queue *q)
405{
406 unsigned long flags;
407
408 spin_lock_irqsave(q->queue_lock, flags);
409 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 spin_unlock_irqrestore(q->queue_lock, flags);
411}
412EXPORT_SYMBOL(blk_run_queue);
413
Jens Axboe165125e2007-07-24 09:28:11 +0200414void blk_put_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500415{
416 kobject_put(&q->kobj);
417}
Al Viro483f4af2006-03-18 18:34:37 -0500418
Jens Axboe6728cb02008-01-31 13:03:55 +0100419void blk_cleanup_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500420{
421 mutex_lock(&q->sysfs_lock);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200422 queue_flag_set_unlocked(QUEUE_FLAG_DEAD, q);
Al Viro483f4af2006-03-18 18:34:37 -0500423 mutex_unlock(&q->sysfs_lock);
424
425 if (q->elevator)
426 elevator_exit(q->elevator);
427
428 blk_put_queue(q);
429}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430EXPORT_SYMBOL(blk_cleanup_queue);
431
Jens Axboe165125e2007-07-24 09:28:11 +0200432static int blk_init_free_list(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
434 struct request_list *rl = &q->rq;
435
436 rl->count[READ] = rl->count[WRITE] = 0;
437 rl->starved[READ] = rl->starved[WRITE] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200438 rl->elvpriv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 init_waitqueue_head(&rl->wait[READ]);
440 init_waitqueue_head(&rl->wait[WRITE]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Christoph Lameter19460892005-06-23 00:08:19 -0700442 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
443 mempool_free_slab, request_cachep, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 if (!rl->rq_pool)
446 return -ENOMEM;
447
448 return 0;
449}
450
Jens Axboe165125e2007-07-24 09:28:11 +0200451struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Christoph Lameter19460892005-06-23 00:08:19 -0700453 return blk_alloc_queue_node(gfp_mask, -1);
454}
455EXPORT_SYMBOL(blk_alloc_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Jens Axboe165125e2007-07-24 09:28:11 +0200457struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -0700458{
Jens Axboe165125e2007-07-24 09:28:11 +0200459 struct request_queue *q;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700460 int err;
Christoph Lameter19460892005-06-23 00:08:19 -0700461
Jens Axboe8324aa92008-01-29 14:51:59 +0100462 q = kmem_cache_alloc_node(blk_requestq_cachep,
Christoph Lameter94f60302007-07-17 04:03:29 -0700463 gfp_mask | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (!q)
465 return NULL;
466
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700467 q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
468 q->backing_dev_info.unplug_io_data = q;
469 err = bdi_init(&q->backing_dev_info);
470 if (err) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100471 kmem_cache_free(blk_requestq_cachep, q);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700472 return NULL;
473 }
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 init_timer(&q->unplug_timer);
Al Viro483f4af2006-03-18 18:34:37 -0500476
Jens Axboe8324aa92008-01-29 14:51:59 +0100477 kobject_init(&q->kobj, &blk_queue_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Al Viro483f4af2006-03-18 18:34:37 -0500479 mutex_init(&q->sysfs_lock);
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 return q;
482}
Christoph Lameter19460892005-06-23 00:08:19 -0700483EXPORT_SYMBOL(blk_alloc_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485/**
486 * blk_init_queue - prepare a request queue for use with a block device
487 * @rfn: The function to be called to process requests that have been
488 * placed on the queue.
489 * @lock: Request queue spin lock
490 *
491 * Description:
492 * If a block device wishes to use the standard request handling procedures,
493 * which sorts requests and coalesces adjacent requests, then it must
494 * call blk_init_queue(). The function @rfn will be called when there
495 * are requests on the queue that need to be processed. If the device
496 * supports plugging, then @rfn may not be called immediately when requests
497 * are available on the queue, but may be called at some time later instead.
498 * Plugged queues are generally unplugged when a buffer belonging to one
499 * of the requests on the queue is needed, or due to memory pressure.
500 *
501 * @rfn is not required, or even expected, to remove all requests off the
502 * queue, but only as many as it can handle at a time. If it does leave
503 * requests on the queue, it is responsible for arranging that the requests
504 * get dealt with eventually.
505 *
506 * The queue spin lock must be held while manipulating the requests on the
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200507 * request queue; this lock will be taken also from interrupt context, so irq
508 * disabling is needed for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 *
510 * Function returns a pointer to the initialized request queue, or NULL if
511 * it didn't succeed.
512 *
513 * Note:
514 * blk_init_queue() must be paired with a blk_cleanup_queue() call
515 * when the block device is deactivated (such as at module unload).
516 **/
Christoph Lameter19460892005-06-23 00:08:19 -0700517
Jens Axboe165125e2007-07-24 09:28:11 +0200518struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Christoph Lameter19460892005-06-23 00:08:19 -0700520 return blk_init_queue_node(rfn, lock, -1);
521}
522EXPORT_SYMBOL(blk_init_queue);
523
Jens Axboe165125e2007-07-24 09:28:11 +0200524struct request_queue *
Christoph Lameter19460892005-06-23 00:08:19 -0700525blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
526{
Jens Axboe165125e2007-07-24 09:28:11 +0200527 struct request_queue *q = blk_alloc_queue_node(GFP_KERNEL, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 if (!q)
530 return NULL;
531
Christoph Lameter19460892005-06-23 00:08:19 -0700532 q->node = node_id;
Al Viro8669aaf2006-03-18 13:50:00 -0500533 if (blk_init_free_list(q)) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100534 kmem_cache_free(blk_requestq_cachep, q);
Al Viro8669aaf2006-03-18 13:50:00 -0500535 return NULL;
536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
152587d2005-04-12 16:22:06 -0500538 /*
539 * if caller didn't supply a lock, they get per-queue locking with
540 * our embedded lock
541 */
542 if (!lock) {
543 spin_lock_init(&q->__queue_lock);
544 lock = &q->__queue_lock;
545 }
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 q->request_fn = rfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 q->prep_rq_fn = NULL;
549 q->unplug_fn = generic_unplug_device;
550 q->queue_flags = (1 << QUEUE_FLAG_CLUSTER);
551 q->queue_lock = lock;
552
553 blk_queue_segment_boundary(q, 0xffffffff);
554
555 blk_queue_make_request(q, __make_request);
556 blk_queue_max_segment_size(q, MAX_SEGMENT_SIZE);
557
558 blk_queue_max_hw_segments(q, MAX_HW_SEGMENTS);
559 blk_queue_max_phys_segments(q, MAX_PHYS_SEGMENTS);
560
Alan Stern44ec9542007-02-20 11:01:57 -0500561 q->sg_reserved_size = INT_MAX;
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 /*
564 * all done
565 */
566 if (!elevator_init(q, NULL)) {
567 blk_queue_congestion_threshold(q);
568 return q;
569 }
570
Al Viro8669aaf2006-03-18 13:50:00 -0500571 blk_put_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 return NULL;
573}
Christoph Lameter19460892005-06-23 00:08:19 -0700574EXPORT_SYMBOL(blk_init_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Jens Axboe165125e2007-07-24 09:28:11 +0200576int blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Nick Pigginfde6ad22005-06-23 00:08:53 -0700578 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
Al Viro483f4af2006-03-18 18:34:37 -0500579 kobject_get(&q->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return 0;
581 }
582
583 return 1;
584}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Jens Axboe165125e2007-07-24 09:28:11 +0200586static inline void blk_free_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Jens Axboe4aff5e22006-08-10 08:44:47 +0200588 if (rq->cmd_flags & REQ_ELVPRIV)
Tejun Heocb98fc82005-10-28 08:29:39 +0200589 elv_put_request(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 mempool_free(rq, q->rq.rq_pool);
591}
592
Jens Axboe1ea25ec2006-07-18 22:24:11 +0200593static struct request *
Jens Axboe165125e2007-07-24 09:28:11 +0200594blk_alloc_request(struct request_queue *q, int rw, int priv, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
597
598 if (!rq)
599 return NULL;
600
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200601 rq_init(q, rq);
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 /*
Jens Axboe4aff5e22006-08-10 08:44:47 +0200604 * first three bits are identical in rq->cmd_flags and bio->bi_rw,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 * see bio.h and blkdev.h
606 */
Jens Axboe49171e52006-08-10 08:59:11 +0200607 rq->cmd_flags = rw | REQ_ALLOCED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Tejun Heocb98fc82005-10-28 08:29:39 +0200609 if (priv) {
Jens Axboecb78b282006-07-28 09:32:57 +0200610 if (unlikely(elv_set_request(q, rq, gfp_mask))) {
Tejun Heocb98fc82005-10-28 08:29:39 +0200611 mempool_free(rq, q->rq.rq_pool);
612 return NULL;
613 }
Jens Axboe4aff5e22006-08-10 08:44:47 +0200614 rq->cmd_flags |= REQ_ELVPRIV;
Tejun Heocb98fc82005-10-28 08:29:39 +0200615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Tejun Heocb98fc82005-10-28 08:29:39 +0200617 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
620/*
621 * ioc_batching returns true if the ioc is a valid batching request and
622 * should be given priority access to a request.
623 */
Jens Axboe165125e2007-07-24 09:28:11 +0200624static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
626 if (!ioc)
627 return 0;
628
629 /*
630 * Make sure the process is able to allocate at least 1 request
631 * even if the batch times out, otherwise we could theoretically
632 * lose wakeups.
633 */
634 return ioc->nr_batch_requests == q->nr_batching ||
635 (ioc->nr_batch_requests > 0
636 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
637}
638
639/*
640 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
641 * will cause the process to be a "batcher" on all queues in the system. This
642 * is the behaviour we want though - once it gets a wakeup it should be given
643 * a nice run.
644 */
Jens Axboe165125e2007-07-24 09:28:11 +0200645static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
647 if (!ioc || ioc_batching(q, ioc))
648 return;
649
650 ioc->nr_batch_requests = q->nr_batching;
651 ioc->last_waited = jiffies;
652}
653
Jens Axboe165125e2007-07-24 09:28:11 +0200654static void __freed_request(struct request_queue *q, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
656 struct request_list *rl = &q->rq;
657
658 if (rl->count[rw] < queue_congestion_off_threshold(q))
Thomas Maier79e2de42006-10-19 23:28:15 -0700659 blk_clear_queue_congested(q, rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 if (rl->count[rw] + 1 <= q->nr_requests) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (waitqueue_active(&rl->wait[rw]))
663 wake_up(&rl->wait[rw]);
664
665 blk_clear_queue_full(q, rw);
666 }
667}
668
669/*
670 * A request has just been released. Account for it, update the full and
671 * congestion status, wake up any waiters. Called under q->queue_lock.
672 */
Jens Axboe165125e2007-07-24 09:28:11 +0200673static void freed_request(struct request_queue *q, int rw, int priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
675 struct request_list *rl = &q->rq;
676
677 rl->count[rw]--;
Tejun Heocb98fc82005-10-28 08:29:39 +0200678 if (priv)
679 rl->elvpriv--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 __freed_request(q, rw);
682
683 if (unlikely(rl->starved[rw ^ 1]))
684 __freed_request(q, rw ^ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
687#define blkdev_free_rq(list) list_entry((list)->next, struct request, queuelist)
688/*
Nick Piggind6344532005-06-28 20:45:14 -0700689 * Get a free request, queue_lock must be held.
690 * Returns NULL on failure, with queue_lock held.
691 * Returns !NULL on success, with queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 */
Jens Axboe165125e2007-07-24 09:28:11 +0200693static struct request *get_request(struct request_queue *q, int rw_flags,
Jens Axboe7749a8d2006-12-13 13:02:26 +0100694 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
696 struct request *rq = NULL;
697 struct request_list *rl = &q->rq;
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100698 struct io_context *ioc = NULL;
Jens Axboe7749a8d2006-12-13 13:02:26 +0100699 const int rw = rw_flags & 0x01;
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100700 int may_queue, priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Jens Axboe7749a8d2006-12-13 13:02:26 +0100702 may_queue = elv_may_queue(q, rw_flags);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100703 if (may_queue == ELV_MQUEUE_NO)
704 goto rq_starved;
705
706 if (rl->count[rw]+1 >= queue_congestion_on_threshold(q)) {
707 if (rl->count[rw]+1 >= q->nr_requests) {
Jens Axboeb5deef92006-07-19 23:39:40 +0200708 ioc = current_io_context(GFP_ATOMIC, q->node);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100709 /*
710 * The queue will fill after this allocation, so set
711 * it as full, and mark this process as "batching".
712 * This process will be allowed to complete a batch of
713 * requests, others will be blocked.
714 */
715 if (!blk_queue_full(q, rw)) {
716 ioc_set_batching(q, ioc);
717 blk_set_queue_full(q, rw);
718 } else {
719 if (may_queue != ELV_MQUEUE_MUST
720 && !ioc_batching(q, ioc)) {
721 /*
722 * The queue is full and the allocating
723 * process is not a "batcher", and not
724 * exempted by the IO scheduler
725 */
726 goto out;
727 }
728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
Thomas Maier79e2de42006-10-19 23:28:15 -0700730 blk_set_queue_congested(q, rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
732
Jens Axboe082cf692005-06-28 16:35:11 +0200733 /*
734 * Only allow batching queuers to allocate up to 50% over the defined
735 * limit of requests, otherwise we could have thousands of requests
736 * allocated with any setting of ->nr_requests
737 */
Hugh Dickinsfd782a42005-06-29 15:15:40 +0100738 if (rl->count[rw] >= (3 * q->nr_requests / 2))
Jens Axboe082cf692005-06-28 16:35:11 +0200739 goto out;
Hugh Dickinsfd782a42005-06-29 15:15:40 +0100740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 rl->count[rw]++;
742 rl->starved[rw] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200743
Jens Axboe64521d12005-10-28 08:30:39 +0200744 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Tejun Heocb98fc82005-10-28 08:29:39 +0200745 if (priv)
746 rl->elvpriv++;
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 spin_unlock_irq(q->queue_lock);
749
Jens Axboe7749a8d2006-12-13 13:02:26 +0100750 rq = blk_alloc_request(q, rw_flags, priv, gfp_mask);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100751 if (unlikely(!rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 /*
753 * Allocation failed presumably due to memory. Undo anything
754 * we might have messed up.
755 *
756 * Allocating task should really be put onto the front of the
757 * wait queue, but this is pretty rare.
758 */
759 spin_lock_irq(q->queue_lock);
Tejun Heocb98fc82005-10-28 08:29:39 +0200760 freed_request(q, rw, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 /*
763 * in the very unlikely event that allocation failed and no
764 * requests for this direction was pending, mark us starved
765 * so that freeing of a request in the other direction will
766 * notice us. another possible fix would be to split the
767 * rq mempool into READ and WRITE
768 */
769rq_starved:
770 if (unlikely(rl->count[rw] == 0))
771 rl->starved[rw] = 1;
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 goto out;
774 }
775
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100776 /*
777 * ioc may be NULL here, and ioc_batching will be false. That's
778 * OK, if the queue is under the request limit then requests need
779 * not count toward the nr_batch_requests limit. There will always
780 * be some limit enforced by BLK_BATCH_TIME.
781 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (ioc_batching(q, ioc))
783 ioc->nr_batch_requests--;
Jens Axboe6728cb02008-01-31 13:03:55 +0100784
Jens Axboe2056a782006-03-23 20:00:26 +0100785 blk_add_trace_generic(q, bio, rw, BLK_TA_GETRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return rq;
788}
789
790/*
791 * No available requests for this queue, unplug the device and wait for some
792 * requests to become available.
Nick Piggind6344532005-06-28 20:45:14 -0700793 *
794 * Called with q->queue_lock held, and returns with it unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 */
Jens Axboe165125e2007-07-24 09:28:11 +0200796static struct request *get_request_wait(struct request_queue *q, int rw_flags,
Jens Axboe22e2c502005-06-27 10:55:12 +0200797 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Jens Axboe7749a8d2006-12-13 13:02:26 +0100799 const int rw = rw_flags & 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 struct request *rq;
801
Jens Axboe7749a8d2006-12-13 13:02:26 +0100802 rq = get_request(q, rw_flags, bio, GFP_NOIO);
Nick Piggin450991b2005-06-28 20:45:13 -0700803 while (!rq) {
804 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 struct request_list *rl = &q->rq;
806
807 prepare_to_wait_exclusive(&rl->wait[rw], &wait,
808 TASK_UNINTERRUPTIBLE);
809
Jens Axboe7749a8d2006-12-13 13:02:26 +0100810 rq = get_request(q, rw_flags, bio, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 if (!rq) {
813 struct io_context *ioc;
814
Jens Axboe2056a782006-03-23 20:00:26 +0100815 blk_add_trace_generic(q, bio, rw, BLK_TA_SLEEPRQ);
816
Nick Piggind6344532005-06-28 20:45:14 -0700817 __generic_unplug_device(q);
818 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 io_schedule();
820
821 /*
822 * After sleeping, we become a "batching" process and
823 * will be able to allocate at least one request, and
824 * up to a big batch of them for a small period time.
825 * See ioc_batching, ioc_set_batching
826 */
Jens Axboeb5deef92006-07-19 23:39:40 +0200827 ioc = current_io_context(GFP_NOIO, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 ioc_set_batching(q, ioc);
Nick Piggind6344532005-06-28 20:45:14 -0700829
830 spin_lock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832 finish_wait(&rl->wait[rw], &wait);
Nick Piggin450991b2005-06-28 20:45:13 -0700833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 return rq;
836}
837
Jens Axboe165125e2007-07-24 09:28:11 +0200838struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
840 struct request *rq;
841
842 BUG_ON(rw != READ && rw != WRITE);
843
Nick Piggind6344532005-06-28 20:45:14 -0700844 spin_lock_irq(q->queue_lock);
845 if (gfp_mask & __GFP_WAIT) {
Jens Axboe22e2c502005-06-27 10:55:12 +0200846 rq = get_request_wait(q, rw, NULL);
Nick Piggind6344532005-06-28 20:45:14 -0700847 } else {
Jens Axboe22e2c502005-06-27 10:55:12 +0200848 rq = get_request(q, rw, NULL, gfp_mask);
Nick Piggind6344532005-06-28 20:45:14 -0700849 if (!rq)
850 spin_unlock_irq(q->queue_lock);
851 }
852 /* q->queue_lock is unlocked at this point */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 return rq;
855}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856EXPORT_SYMBOL(blk_get_request);
857
858/**
Jens Axboedc72ef42006-07-20 14:54:05 +0200859 * blk_start_queueing - initiate dispatch of requests to device
860 * @q: request queue to kick into gear
861 *
862 * This is basically a helper to remove the need to know whether a queue
863 * is plugged or not if someone just wants to initiate dispatch of requests
864 * for this queue.
865 *
866 * The queue lock must be held with interrupts disabled.
867 */
Jens Axboe165125e2007-07-24 09:28:11 +0200868void blk_start_queueing(struct request_queue *q)
Jens Axboedc72ef42006-07-20 14:54:05 +0200869{
870 if (!blk_queue_plugged(q))
871 q->request_fn(q);
872 else
873 __generic_unplug_device(q);
874}
875EXPORT_SYMBOL(blk_start_queueing);
876
877/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 * blk_requeue_request - put a request back on queue
879 * @q: request queue where request should be inserted
880 * @rq: request to be inserted
881 *
882 * Description:
883 * Drivers often keep queueing requests until the hardware cannot accept
884 * more, when that condition happens we need to put the request back
885 * on the queue. Must be called with queue lock held.
886 */
Jens Axboe165125e2007-07-24 09:28:11 +0200887void blk_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Jens Axboe2056a782006-03-23 20:00:26 +0100889 blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 if (blk_rq_tagged(rq))
892 blk_queue_end_tag(q, rq);
893
894 elv_requeue_request(q, rq);
895}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896EXPORT_SYMBOL(blk_requeue_request);
897
898/**
899 * blk_insert_request - insert a special request in to a request queue
900 * @q: request queue where request should be inserted
901 * @rq: request to be inserted
902 * @at_head: insert request at head or tail of queue
903 * @data: private data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 *
905 * Description:
906 * Many block devices need to execute commands asynchronously, so they don't
907 * block the whole kernel from preemption during request execution. This is
908 * accomplished normally by inserting aritficial requests tagged as
909 * REQ_SPECIAL in to the corresponding request queue, and letting them be
910 * scheduled for actual execution by the request queue.
911 *
912 * We have the option of inserting the head or the tail of the queue.
913 * Typically we use the tail for new ioctls and so forth. We use the head
914 * of the queue for things like a QUEUE_FULL message from a device, or a
915 * host that is unable to accept a particular command.
916 */
Jens Axboe165125e2007-07-24 09:28:11 +0200917void blk_insert_request(struct request_queue *q, struct request *rq,
Tejun Heo 867d1192005-04-24 02:06:05 -0500918 int at_head, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Tejun Heo 867d1192005-04-24 02:06:05 -0500920 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 unsigned long flags;
922
923 /*
924 * tell I/O scheduler that this isn't a regular read/write (ie it
925 * must not attempt merges on this) and that it acts as a soft
926 * barrier
927 */
Jens Axboe4aff5e22006-08-10 08:44:47 +0200928 rq->cmd_type = REQ_TYPE_SPECIAL;
929 rq->cmd_flags |= REQ_SOFTBARRIER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931 rq->special = data;
932
933 spin_lock_irqsave(q->queue_lock, flags);
934
935 /*
936 * If command is tagged, release the tag
937 */
Tejun Heo 867d1192005-04-24 02:06:05 -0500938 if (blk_rq_tagged(rq))
939 blk_queue_end_tag(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Jerome Marchandb238b3d2007-10-23 15:05:46 +0200941 drive_stat_acct(rq, 1);
Tejun Heo 867d1192005-04-24 02:06:05 -0500942 __elv_add_request(q, rq, where, 0);
Jens Axboedc72ef42006-07-20 14:54:05 +0200943 blk_start_queueing(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 spin_unlock_irqrestore(q->queue_lock, flags);
945}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946EXPORT_SYMBOL(blk_insert_request);
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948/*
949 * add-request adds a request to the linked list.
950 * queue lock is held and interrupts disabled, as we muck with the
951 * request queue list.
952 */
Jens Axboe6728cb02008-01-31 13:03:55 +0100953static inline void add_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
Jerome Marchandb238b3d2007-10-23 15:05:46 +0200955 drive_stat_acct(req, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 /*
958 * elevator indicated where it wants this request to be
959 * inserted at elevator_merge time
960 */
961 __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
962}
Jens Axboe6728cb02008-01-31 13:03:55 +0100963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964/*
965 * disk_round_stats() - Round off the performance stats on a struct
966 * disk_stats.
967 *
968 * The average IO queue length and utilisation statistics are maintained
969 * by observing the current state of the queue length and the amount of
970 * time it has been in this state for.
971 *
972 * Normally, that accounting is done on IO completion, but that can result
973 * in more than a second's worth of IO being accounted for within any one
974 * second, leading to >100% utilisation. To deal with that, we call this
975 * function to do a round-off before returning the results when reading
976 * /proc/diskstats. This accounts immediately for all queue usage up to
977 * the current jiffies and restarts the counters again.
978 */
979void disk_round_stats(struct gendisk *disk)
980{
981 unsigned long now = jiffies;
982
Chen, Kenneth Wb2982642005-10-13 21:49:29 +0200983 if (now == disk->stamp)
984 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Chen, Kenneth W20e5c812005-10-13 21:48:42 +0200986 if (disk->in_flight) {
987 __disk_stat_add(disk, time_in_queue,
988 disk->in_flight * (now - disk->stamp));
989 __disk_stat_add(disk, io_ticks, (now - disk->stamp));
990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 disk->stamp = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800993EXPORT_SYMBOL_GPL(disk_round_stats);
994
Jerome Marchand6f2576a2008-02-08 11:04:35 +0100995void part_round_stats(struct hd_struct *part)
996{
997 unsigned long now = jiffies;
998
999 if (now == part->stamp)
1000 return;
1001
1002 if (part->in_flight) {
1003 __part_stat_add(part, time_in_queue,
1004 part->in_flight * (now - part->stamp));
1005 __part_stat_add(part, io_ticks, (now - part->stamp));
1006 }
1007 part->stamp = now;
1008}
1009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010/*
1011 * queue lock must be held
1012 */
Jens Axboe165125e2007-07-24 09:28:11 +02001013void __blk_put_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (unlikely(!q))
1016 return;
1017 if (unlikely(--req->ref_count))
1018 return;
1019
Tejun Heo8922e162005-10-20 16:23:44 +02001020 elv_completed_request(q, req);
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 /*
1023 * Request may not have originated from ll_rw_blk. if not,
1024 * it didn't come out of our reserved rq pools
1025 */
Jens Axboe49171e52006-08-10 08:59:11 +02001026 if (req->cmd_flags & REQ_ALLOCED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 int rw = rq_data_dir(req);
Jens Axboe4aff5e22006-08-10 08:44:47 +02001028 int priv = req->cmd_flags & REQ_ELVPRIV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 BUG_ON(!list_empty(&req->queuelist));
Jens Axboe98170642006-07-28 09:23:08 +02001031 BUG_ON(!hlist_unhashed(&req->hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 blk_free_request(q, req);
Tejun Heocb98fc82005-10-28 08:29:39 +02001034 freed_request(q, rw, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
1036}
Mike Christie6e39b692005-11-11 05:30:24 -06001037EXPORT_SYMBOL_GPL(__blk_put_request);
1038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039void blk_put_request(struct request *req)
1040{
Tejun Heo8922e162005-10-20 16:23:44 +02001041 unsigned long flags;
Jens Axboe165125e2007-07-24 09:28:11 +02001042 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Tejun Heo8922e162005-10-20 16:23:44 +02001044 /*
1045 * Gee, IDE calls in w/ NULL q. Fix IDE and remove the
1046 * following if (q) test.
1047 */
1048 if (q) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 spin_lock_irqsave(q->queue_lock, flags);
1050 __blk_put_request(q, req);
1051 spin_unlock_irqrestore(q->queue_lock, flags);
1052 }
1053}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054EXPORT_SYMBOL(blk_put_request);
1055
Jens Axboe86db1e22008-01-29 14:53:40 +01001056void init_request_from_bio(struct request *req, struct bio *bio)
Tejun Heo52d9e672006-01-06 09:49:58 +01001057{
Jens Axboe4aff5e22006-08-10 08:44:47 +02001058 req->cmd_type = REQ_TYPE_FS;
Tejun Heo52d9e672006-01-06 09:49:58 +01001059
1060 /*
1061 * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
1062 */
1063 if (bio_rw_ahead(bio) || bio_failfast(bio))
Jens Axboe4aff5e22006-08-10 08:44:47 +02001064 req->cmd_flags |= REQ_FAILFAST;
Tejun Heo52d9e672006-01-06 09:49:58 +01001065
1066 /*
1067 * REQ_BARRIER implies no merging, but lets make it explicit
1068 */
1069 if (unlikely(bio_barrier(bio)))
Jens Axboe4aff5e22006-08-10 08:44:47 +02001070 req->cmd_flags |= (REQ_HARDBARRIER | REQ_NOMERGE);
Tejun Heo52d9e672006-01-06 09:49:58 +01001071
Jens Axboeb31dc662006-06-13 08:26:10 +02001072 if (bio_sync(bio))
Jens Axboe4aff5e22006-08-10 08:44:47 +02001073 req->cmd_flags |= REQ_RW_SYNC;
Jens Axboe5404bc72006-08-10 09:01:02 +02001074 if (bio_rw_meta(bio))
1075 req->cmd_flags |= REQ_RW_META;
Jens Axboeb31dc662006-06-13 08:26:10 +02001076
Tejun Heo52d9e672006-01-06 09:49:58 +01001077 req->errors = 0;
1078 req->hard_sector = req->sector = bio->bi_sector;
Tejun Heo52d9e672006-01-06 09:49:58 +01001079 req->ioprio = bio_prio(bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001080 req->start_time = jiffies;
NeilBrownbc1c56f2007-08-16 13:31:30 +02001081 blk_rq_bio_prep(req->q, req, bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001082}
1083
Jens Axboe165125e2007-07-24 09:28:11 +02001084static int __make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
Nick Piggin450991b2005-06-28 20:45:13 -07001086 struct request *req;
Jens Axboe51da90f2006-07-18 04:14:45 +02001087 int el_ret, nr_sectors, barrier, err;
1088 const unsigned short prio = bio_prio(bio);
1089 const int sync = bio_sync(bio);
Jens Axboe7749a8d2006-12-13 13:02:26 +01001090 int rw_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 nr_sectors = bio_sectors(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 /*
1095 * low level driver can indicate that it wants pages above a
1096 * certain limit bounced to low memory (ie for highmem, or even
1097 * ISA dma in theory)
1098 */
1099 blk_queue_bounce(q, &bio);
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 barrier = bio_barrier(bio);
Tejun Heo797e7db2006-01-06 09:51:03 +01001102 if (unlikely(barrier) && (q->next_ordered == QUEUE_ORDERED_NONE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 err = -EOPNOTSUPP;
1104 goto end_io;
1105 }
1106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 spin_lock_irq(q->queue_lock);
1108
Nick Piggin450991b2005-06-28 20:45:13 -07001109 if (unlikely(barrier) || elv_queue_empty(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 goto get_rq;
1111
1112 el_ret = elv_merge(q, &req, bio);
1113 switch (el_ret) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001114 case ELEVATOR_BACK_MERGE:
1115 BUG_ON(!rq_mergeable(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Jens Axboe6728cb02008-01-31 13:03:55 +01001117 if (!ll_back_merge_fn(q, req, bio))
1118 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Jens Axboe6728cb02008-01-31 13:03:55 +01001120 blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
Jens Axboe2056a782006-03-23 20:00:26 +01001121
Jens Axboe6728cb02008-01-31 13:03:55 +01001122 req->biotail->bi_next = bio;
1123 req->biotail = bio;
1124 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
1125 req->ioprio = ioprio_best(req->ioprio, prio);
1126 drive_stat_acct(req, 0);
1127 if (!attempt_back_merge(q, req))
1128 elv_merged_request(q, req, el_ret);
1129 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Jens Axboe6728cb02008-01-31 13:03:55 +01001131 case ELEVATOR_FRONT_MERGE:
1132 BUG_ON(!rq_mergeable(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Jens Axboe6728cb02008-01-31 13:03:55 +01001134 if (!ll_front_merge_fn(q, req, bio))
1135 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Jens Axboe6728cb02008-01-31 13:03:55 +01001137 blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
Jens Axboe2056a782006-03-23 20:00:26 +01001138
Jens Axboe6728cb02008-01-31 13:03:55 +01001139 bio->bi_next = req->bio;
1140 req->bio = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Jens Axboe6728cb02008-01-31 13:03:55 +01001142 /*
1143 * may not be valid. if the low level driver said
1144 * it didn't need a bounce buffer then it better
1145 * not touch req->buffer either...
1146 */
1147 req->buffer = bio_data(bio);
1148 req->current_nr_sectors = bio_cur_sectors(bio);
1149 req->hard_cur_sectors = req->current_nr_sectors;
1150 req->sector = req->hard_sector = bio->bi_sector;
1151 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
1152 req->ioprio = ioprio_best(req->ioprio, prio);
1153 drive_stat_acct(req, 0);
1154 if (!attempt_front_merge(q, req))
1155 elv_merged_request(q, req, el_ret);
1156 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Jens Axboe6728cb02008-01-31 13:03:55 +01001158 /* ELV_NO_MERGE: elevator says don't/can't merge. */
1159 default:
1160 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163get_rq:
Nick Piggin450991b2005-06-28 20:45:13 -07001164 /*
Jens Axboe7749a8d2006-12-13 13:02:26 +01001165 * This sync check and mask will be re-done in init_request_from_bio(),
1166 * but we need to set it earlier to expose the sync flag to the
1167 * rq allocator and io schedulers.
1168 */
1169 rw_flags = bio_data_dir(bio);
1170 if (sync)
1171 rw_flags |= REQ_RW_SYNC;
1172
1173 /*
Nick Piggin450991b2005-06-28 20:45:13 -07001174 * Grab a free request. This is might sleep but can not fail.
Nick Piggind6344532005-06-28 20:45:14 -07001175 * Returns with the queue unlocked.
Nick Piggin450991b2005-06-28 20:45:13 -07001176 */
Jens Axboe7749a8d2006-12-13 13:02:26 +01001177 req = get_request_wait(q, rw_flags, bio);
Nick Piggind6344532005-06-28 20:45:14 -07001178
Nick Piggin450991b2005-06-28 20:45:13 -07001179 /*
1180 * After dropping the lock and possibly sleeping here, our request
1181 * may now be mergeable after it had proven unmergeable (above).
1182 * We don't worry about that case for efficiency. It won't happen
1183 * often, and the elevators are able to handle it.
1184 */
Tejun Heo52d9e672006-01-06 09:49:58 +01001185 init_request_from_bio(req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Nick Piggin450991b2005-06-28 20:45:13 -07001187 spin_lock_irq(q->queue_lock);
1188 if (elv_queue_empty(q))
1189 blk_plug_device(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 add_request(q, req);
1191out:
Jens Axboe4a534f92005-04-16 15:25:40 -07001192 if (sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 __generic_unplug_device(q);
1194
1195 spin_unlock_irq(q->queue_lock);
1196 return 0;
1197
1198end_io:
NeilBrown6712ecf2007-09-27 12:47:43 +02001199 bio_endio(bio, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 return 0;
1201}
1202
1203/*
1204 * If bio->bi_dev is a partition, remap the location
1205 */
1206static inline void blk_partition_remap(struct bio *bio)
1207{
1208 struct block_device *bdev = bio->bi_bdev;
1209
Jens Axboebf2de6f2007-09-27 13:01:25 +02001210 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 struct hd_struct *p = bdev->bd_part;
Jens Axboea3623572005-11-01 09:26:16 +01001212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 bio->bi_sector += p->start_sect;
1214 bio->bi_bdev = bdev->bd_contains;
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001215
1216 blk_add_trace_remap(bdev_get_queue(bio->bi_bdev), bio,
1217 bdev->bd_dev, bio->bi_sector,
1218 bio->bi_sector - p->start_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 }
1220}
1221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222static void handle_bad_sector(struct bio *bio)
1223{
1224 char b[BDEVNAME_SIZE];
1225
1226 printk(KERN_INFO "attempt to access beyond end of device\n");
1227 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1228 bdevname(bio->bi_bdev, b),
1229 bio->bi_rw,
1230 (unsigned long long)bio->bi_sector + bio_sectors(bio),
1231 (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
1232
1233 set_bit(BIO_EOF, &bio->bi_flags);
1234}
1235
Akinobu Mitac17bb492006-12-08 02:39:46 -08001236#ifdef CONFIG_FAIL_MAKE_REQUEST
1237
1238static DECLARE_FAULT_ATTR(fail_make_request);
1239
1240static int __init setup_fail_make_request(char *str)
1241{
1242 return setup_fault_attr(&fail_make_request, str);
1243}
1244__setup("fail_make_request=", setup_fail_make_request);
1245
1246static int should_fail_request(struct bio *bio)
1247{
1248 if ((bio->bi_bdev->bd_disk->flags & GENHD_FL_FAIL) ||
1249 (bio->bi_bdev->bd_part && bio->bi_bdev->bd_part->make_it_fail))
1250 return should_fail(&fail_make_request, bio->bi_size);
1251
1252 return 0;
1253}
1254
1255static int __init fail_make_request_debugfs(void)
1256{
1257 return init_fault_attr_dentries(&fail_make_request,
1258 "fail_make_request");
1259}
1260
1261late_initcall(fail_make_request_debugfs);
1262
1263#else /* CONFIG_FAIL_MAKE_REQUEST */
1264
1265static inline int should_fail_request(struct bio *bio)
1266{
1267 return 0;
1268}
1269
1270#endif /* CONFIG_FAIL_MAKE_REQUEST */
1271
Jens Axboec07e2b42007-07-18 13:27:58 +02001272/*
1273 * Check whether this bio extends beyond the end of the device.
1274 */
1275static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1276{
1277 sector_t maxsector;
1278
1279 if (!nr_sectors)
1280 return 0;
1281
1282 /* Test device or partition size, when known. */
1283 maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
1284 if (maxsector) {
1285 sector_t sector = bio->bi_sector;
1286
1287 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1288 /*
1289 * This may well happen - the kernel calls bread()
1290 * without checking the size of the device, e.g., when
1291 * mounting a device.
1292 */
1293 handle_bad_sector(bio);
1294 return 1;
1295 }
1296 }
1297
1298 return 0;
1299}
1300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301/**
1302 * generic_make_request: hand a buffer to its device driver for I/O
1303 * @bio: The bio describing the location in memory and on the device.
1304 *
1305 * generic_make_request() is used to make I/O requests of block
1306 * devices. It is passed a &struct bio, which describes the I/O that needs
1307 * to be done.
1308 *
1309 * generic_make_request() does not return any status. The
1310 * success/failure status of the request, along with notification of
1311 * completion, is delivered asynchronously through the bio->bi_end_io
1312 * function described (one day) else where.
1313 *
1314 * The caller of generic_make_request must make sure that bi_io_vec
1315 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1316 * set to describe the device address, and the
1317 * bi_end_io and optionally bi_private are set to describe how
1318 * completion notification should be signaled.
1319 *
1320 * generic_make_request and the drivers it calls may use bi_next if this
1321 * bio happens to be merged with someone else, and may change bi_dev and
1322 * bi_sector for remaps as it sees fit. So the values of these fields
1323 * should NOT be depended on after the call to generic_make_request.
1324 */
Neil Brownd89d8792007-05-01 09:53:42 +02001325static inline void __generic_make_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
Jens Axboe165125e2007-07-24 09:28:11 +02001327 struct request_queue *q;
NeilBrown5ddfe962006-10-30 22:07:21 -08001328 sector_t old_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 int ret, nr_sectors = bio_sectors(bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001330 dev_t old_dev;
Jens Axboe51fd77b2007-11-02 08:49:08 +01001331 int err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Jens Axboec07e2b42007-07-18 13:27:58 +02001335 if (bio_check_eod(bio, nr_sectors))
1336 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 /*
1339 * Resolve the mapping until finished. (drivers are
1340 * still free to implement/resolve their own stacking
1341 * by explicitly returning 0)
1342 *
1343 * NOTE: we don't repeat the blk_size check for each new device.
1344 * Stacking drivers are expected to know what they are doing.
1345 */
NeilBrown5ddfe962006-10-30 22:07:21 -08001346 old_sector = -1;
Jens Axboe2056a782006-03-23 20:00:26 +01001347 old_dev = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 do {
1349 char b[BDEVNAME_SIZE];
1350
1351 q = bdev_get_queue(bio->bi_bdev);
1352 if (!q) {
1353 printk(KERN_ERR
1354 "generic_make_request: Trying to access "
1355 "nonexistent block-device %s (%Lu)\n",
1356 bdevname(bio->bi_bdev, b),
1357 (long long) bio->bi_sector);
1358end_io:
Jens Axboe51fd77b2007-11-02 08:49:08 +01001359 bio_endio(bio, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 break;
1361 }
1362
Jens Axboe4fa253f2007-07-18 13:13:10 +02001363 if (unlikely(nr_sectors > q->max_hw_sectors)) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001364 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 bdevname(bio->bi_bdev, b),
1366 bio_sectors(bio),
1367 q->max_hw_sectors);
1368 goto end_io;
1369 }
1370
Nick Pigginfde6ad22005-06-23 00:08:53 -07001371 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 goto end_io;
1373
Akinobu Mitac17bb492006-12-08 02:39:46 -08001374 if (should_fail_request(bio))
1375 goto end_io;
1376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 /*
1378 * If this device has partitions, remap block n
1379 * of partition p to block n+start(p) of the disk.
1380 */
1381 blk_partition_remap(bio);
1382
NeilBrown5ddfe962006-10-30 22:07:21 -08001383 if (old_sector != -1)
Jens Axboe4fa253f2007-07-18 13:13:10 +02001384 blk_add_trace_remap(q, bio, old_dev, bio->bi_sector,
NeilBrown5ddfe962006-10-30 22:07:21 -08001385 old_sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001386
1387 blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
1388
NeilBrown5ddfe962006-10-30 22:07:21 -08001389 old_sector = bio->bi_sector;
Jens Axboe2056a782006-03-23 20:00:26 +01001390 old_dev = bio->bi_bdev->bd_dev;
1391
Jens Axboec07e2b42007-07-18 13:27:58 +02001392 if (bio_check_eod(bio, nr_sectors))
1393 goto end_io;
Jens Axboe51fd77b2007-11-02 08:49:08 +01001394 if (bio_empty_barrier(bio) && !q->prepare_flush_fn) {
1395 err = -EOPNOTSUPP;
1396 goto end_io;
1397 }
NeilBrown5ddfe962006-10-30 22:07:21 -08001398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 ret = q->make_request_fn(q, bio);
1400 } while (ret);
1401}
1402
Neil Brownd89d8792007-05-01 09:53:42 +02001403/*
1404 * We only want one ->make_request_fn to be active at a time,
1405 * else stack usage with stacked devices could be a problem.
1406 * So use current->bio_{list,tail} to keep a list of requests
1407 * submited by a make_request_fn function.
1408 * current->bio_tail is also used as a flag to say if
1409 * generic_make_request is currently active in this task or not.
1410 * If it is NULL, then no make_request is active. If it is non-NULL,
1411 * then a make_request is active, and new requests should be added
1412 * at the tail
1413 */
1414void generic_make_request(struct bio *bio)
1415{
1416 if (current->bio_tail) {
1417 /* make_request is active */
1418 *(current->bio_tail) = bio;
1419 bio->bi_next = NULL;
1420 current->bio_tail = &bio->bi_next;
1421 return;
1422 }
1423 /* following loop may be a bit non-obvious, and so deserves some
1424 * explanation.
1425 * Before entering the loop, bio->bi_next is NULL (as all callers
1426 * ensure that) so we have a list with a single bio.
1427 * We pretend that we have just taken it off a longer list, so
1428 * we assign bio_list to the next (which is NULL) and bio_tail
1429 * to &bio_list, thus initialising the bio_list of new bios to be
1430 * added. __generic_make_request may indeed add some more bios
1431 * through a recursive call to generic_make_request. If it
1432 * did, we find a non-NULL value in bio_list and re-enter the loop
1433 * from the top. In this case we really did just take the bio
1434 * of the top of the list (no pretending) and so fixup bio_list and
1435 * bio_tail or bi_next, and call into __generic_make_request again.
1436 *
1437 * The loop was structured like this to make only one call to
1438 * __generic_make_request (which is important as it is large and
1439 * inlined) and to keep the structure simple.
1440 */
1441 BUG_ON(bio->bi_next);
1442 do {
1443 current->bio_list = bio->bi_next;
1444 if (bio->bi_next == NULL)
1445 current->bio_tail = &current->bio_list;
1446 else
1447 bio->bi_next = NULL;
1448 __generic_make_request(bio);
1449 bio = current->bio_list;
1450 } while (bio);
1451 current->bio_tail = NULL; /* deactivate */
1452}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453EXPORT_SYMBOL(generic_make_request);
1454
1455/**
1456 * submit_bio: submit a bio to the block device layer for I/O
1457 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1458 * @bio: The &struct bio which describes the I/O
1459 *
1460 * submit_bio() is very similar in purpose to generic_make_request(), and
1461 * uses that function to do most of the work. Both are fairly rough
1462 * interfaces, @bio must be presetup and ready for I/O.
1463 *
1464 */
1465void submit_bio(int rw, struct bio *bio)
1466{
1467 int count = bio_sectors(bio);
1468
Jens Axboe22e2c502005-06-27 10:55:12 +02001469 bio->bi_rw |= rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Jens Axboebf2de6f2007-09-27 13:01:25 +02001471 /*
1472 * If it's a regular read/write or a barrier with data attached,
1473 * go through the normal accounting stuff before submission.
1474 */
1475 if (!bio_empty_barrier(bio)) {
1476
1477 BIO_BUG_ON(!bio->bi_size);
1478 BIO_BUG_ON(!bio->bi_io_vec);
1479
1480 if (rw & WRITE) {
1481 count_vm_events(PGPGOUT, count);
1482 } else {
1483 task_io_account_read(bio->bi_size);
1484 count_vm_events(PGPGIN, count);
1485 }
1486
1487 if (unlikely(block_dump)) {
1488 char b[BDEVNAME_SIZE];
1489 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001490 current->comm, task_pid_nr(current),
Jens Axboebf2de6f2007-09-27 13:01:25 +02001491 (rw & WRITE) ? "WRITE" : "READ",
1492 (unsigned long long)bio->bi_sector,
Jens Axboe6728cb02008-01-31 13:03:55 +01001493 bdevname(bio->bi_bdev, b));
Jens Axboebf2de6f2007-09-27 13:01:25 +02001494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 }
1496
1497 generic_make_request(bio);
1498}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499EXPORT_SYMBOL(submit_bio);
1500
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001501/**
1502 * __end_that_request_first - end I/O on a request
1503 * @req: the request being processed
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001504 * @error: 0 for success, < 0 for error
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001505 * @nr_bytes: number of bytes to complete
1506 *
1507 * Description:
1508 * Ends I/O on a number of bytes attached to @req, and sets it up
1509 * for the next range of segments (if any) in the cluster.
1510 *
1511 * Return:
1512 * 0 - we are done with this request, call end_that_request_last()
1513 * 1 - still buffers pending for this request
1514 **/
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001515static int __end_that_request_first(struct request *req, int error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 int nr_bytes)
1517{
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001518 int total_bytes, bio_nbytes, next_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 struct bio *bio;
1520
Jens Axboe2056a782006-03-23 20:00:26 +01001521 blk_add_trace_rq(req->q, req, BLK_TA_COMPLETE);
1522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 * for a REQ_BLOCK_PC request, we want to carry any eventual
1525 * sense key with us all the way through
1526 */
1527 if (!blk_pc_request(req))
1528 req->errors = 0;
1529
Jens Axboe6728cb02008-01-31 13:03:55 +01001530 if (error && (blk_fs_request(req) && !(req->cmd_flags & REQ_QUIET))) {
1531 printk(KERN_ERR "end_request: I/O error, dev %s, sector %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 req->rq_disk ? req->rq_disk->disk_name : "?",
1533 (unsigned long long)req->sector);
1534 }
1535
Jens Axboed72d9042005-11-01 08:35:42 +01001536 if (blk_fs_request(req) && req->rq_disk) {
Jens Axboea3623572005-11-01 09:26:16 +01001537 const int rw = rq_data_dir(req);
1538
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001539 all_stat_add(req->rq_disk, sectors[rw],
1540 nr_bytes >> 9, req->sector);
Jens Axboed72d9042005-11-01 08:35:42 +01001541 }
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 total_bytes = bio_nbytes = 0;
1544 while ((bio = req->bio) != NULL) {
1545 int nbytes;
1546
Jens Axboebf2de6f2007-09-27 13:01:25 +02001547 /*
1548 * For an empty barrier request, the low level driver must
1549 * store a potential error location in ->sector. We pass
1550 * that back up in ->bi_sector.
1551 */
1552 if (blk_empty_barrier(req))
1553 bio->bi_sector = req->sector;
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 if (nr_bytes >= bio->bi_size) {
1556 req->bio = bio->bi_next;
1557 nbytes = bio->bi_size;
NeilBrown5bb23a62007-09-27 12:46:13 +02001558 req_bio_endio(req, bio, nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 next_idx = 0;
1560 bio_nbytes = 0;
1561 } else {
1562 int idx = bio->bi_idx + next_idx;
1563
1564 if (unlikely(bio->bi_idx >= bio->bi_vcnt)) {
1565 blk_dump_rq_flags(req, "__end_that");
Jens Axboe6728cb02008-01-31 13:03:55 +01001566 printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
1567 __FUNCTION__, bio->bi_idx,
1568 bio->bi_vcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 break;
1570 }
1571
1572 nbytes = bio_iovec_idx(bio, idx)->bv_len;
1573 BIO_BUG_ON(nbytes > bio->bi_size);
1574
1575 /*
1576 * not a complete bvec done
1577 */
1578 if (unlikely(nbytes > nr_bytes)) {
1579 bio_nbytes += nr_bytes;
1580 total_bytes += nr_bytes;
1581 break;
1582 }
1583
1584 /*
1585 * advance to the next vector
1586 */
1587 next_idx++;
1588 bio_nbytes += nbytes;
1589 }
1590
1591 total_bytes += nbytes;
1592 nr_bytes -= nbytes;
1593
Jens Axboe6728cb02008-01-31 13:03:55 +01001594 bio = req->bio;
1595 if (bio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 /*
1597 * end more in this run, or just return 'not-done'
1598 */
1599 if (unlikely(nr_bytes <= 0))
1600 break;
1601 }
1602 }
1603
1604 /*
1605 * completely done
1606 */
1607 if (!req->bio)
1608 return 0;
1609
1610 /*
1611 * if the request wasn't completed, update state
1612 */
1613 if (bio_nbytes) {
NeilBrown5bb23a62007-09-27 12:46:13 +02001614 req_bio_endio(req, bio, bio_nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 bio->bi_idx += next_idx;
1616 bio_iovec(bio)->bv_offset += nr_bytes;
1617 bio_iovec(bio)->bv_len -= nr_bytes;
1618 }
1619
1620 blk_recalc_rq_sectors(req, total_bytes >> 9);
1621 blk_recalc_rq_segments(req);
1622 return 1;
1623}
1624
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625/*
Jens Axboeff856ba2006-01-09 16:02:34 +01001626 * splice the completion data to a local structure and hand off to
1627 * process_completion_queue() to complete the requests
1628 */
1629static void blk_done_softirq(struct softirq_action *h)
1630{
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07001631 struct list_head *cpu_list, local_list;
Jens Axboeff856ba2006-01-09 16:02:34 +01001632
1633 local_irq_disable();
1634 cpu_list = &__get_cpu_var(blk_cpu_done);
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07001635 list_replace_init(cpu_list, &local_list);
Jens Axboeff856ba2006-01-09 16:02:34 +01001636 local_irq_enable();
1637
1638 while (!list_empty(&local_list)) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001639 struct request *rq;
Jens Axboeff856ba2006-01-09 16:02:34 +01001640
Jens Axboe6728cb02008-01-31 13:03:55 +01001641 rq = list_entry(local_list.next, struct request, donelist);
Jens Axboeff856ba2006-01-09 16:02:34 +01001642 list_del_init(&rq->donelist);
1643 rq->q->softirq_done_fn(rq);
1644 }
1645}
1646
Jens Axboe6728cb02008-01-31 13:03:55 +01001647static int __cpuinit blk_cpu_notify(struct notifier_block *self,
1648 unsigned long action, void *hcpu)
Jens Axboeff856ba2006-01-09 16:02:34 +01001649{
1650 /*
1651 * If a CPU goes away, splice its entries to the current CPU
1652 * and trigger a run of the softirq
1653 */
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001654 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
Jens Axboeff856ba2006-01-09 16:02:34 +01001655 int cpu = (unsigned long) hcpu;
1656
1657 local_irq_disable();
1658 list_splice_init(&per_cpu(blk_cpu_done, cpu),
1659 &__get_cpu_var(blk_cpu_done));
1660 raise_softirq_irqoff(BLOCK_SOFTIRQ);
1661 local_irq_enable();
1662 }
1663
1664 return NOTIFY_OK;
1665}
1666
1667
Satyam Sharmadb47d472007-08-23 09:29:40 +02001668static struct notifier_block blk_cpu_notifier __cpuinitdata = {
Jens Axboeff856ba2006-01-09 16:02:34 +01001669 .notifier_call = blk_cpu_notify,
1670};
1671
Jens Axboeff856ba2006-01-09 16:02:34 +01001672/**
1673 * blk_complete_request - end I/O on a request
1674 * @req: the request being processed
1675 *
1676 * Description:
1677 * Ends all I/O on a request. It does not handle partial completions,
Andreas Mohrd6e05ed2006-06-26 18:35:02 +02001678 * unless the driver actually implements this in its completion callback
Jens Axboe4fa253f2007-07-18 13:13:10 +02001679 * through requeueing. The actual completion happens out-of-order,
Jens Axboeff856ba2006-01-09 16:02:34 +01001680 * through a softirq handler. The user must have registered a completion
1681 * callback through blk_queue_softirq_done().
1682 **/
1683
1684void blk_complete_request(struct request *req)
1685{
1686 struct list_head *cpu_list;
1687 unsigned long flags;
1688
1689 BUG_ON(!req->q->softirq_done_fn);
Jens Axboe6728cb02008-01-31 13:03:55 +01001690
Jens Axboeff856ba2006-01-09 16:02:34 +01001691 local_irq_save(flags);
1692
1693 cpu_list = &__get_cpu_var(blk_cpu_done);
1694 list_add_tail(&req->donelist, cpu_list);
1695 raise_softirq_irqoff(BLOCK_SOFTIRQ);
1696
1697 local_irq_restore(flags);
1698}
Jens Axboeff856ba2006-01-09 16:02:34 +01001699EXPORT_SYMBOL(blk_complete_request);
Jens Axboe6728cb02008-01-31 13:03:55 +01001700
Jens Axboeff856ba2006-01-09 16:02:34 +01001701/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 * queue lock must be held
1703 */
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001704static void end_that_request_last(struct request *req, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705{
1706 struct gendisk *disk = req->rq_disk;
Tejun Heo8ffdc652006-01-06 09:49:03 +01001707
Kiyoshi Uedab8286232007-12-11 17:53:24 -05001708 if (blk_rq_tagged(req))
1709 blk_queue_end_tag(req->q, req);
1710
1711 if (blk_queued_rq(req))
1712 blkdev_dequeue_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
1714 if (unlikely(laptop_mode) && blk_fs_request(req))
1715 laptop_io_completion();
1716
Jens Axboefd0ff8a2006-05-23 11:23:49 +02001717 /*
1718 * Account IO completion. bar_rq isn't accounted as a normal
1719 * IO on queueing nor completion. Accounting the containing
1720 * request is enough.
1721 */
1722 if (disk && blk_fs_request(req) && req != &req->q->bar_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 unsigned long duration = jiffies - req->start_time;
Jens Axboea3623572005-11-01 09:26:16 +01001724 const int rw = rq_data_dir(req);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001725 struct hd_struct *part = get_part(disk, req->sector);
Jens Axboea3623572005-11-01 09:26:16 +01001726
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001727 __all_stat_inc(disk, ios[rw], req->sector);
1728 __all_stat_add(disk, ticks[rw], duration, req->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 disk_round_stats(disk);
1730 disk->in_flight--;
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001731 if (part) {
1732 part_round_stats(part);
1733 part->in_flight--;
1734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 }
Kiyoshi Uedab8286232007-12-11 17:53:24 -05001736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 if (req->end_io)
Tejun Heo8ffdc652006-01-06 09:49:03 +01001738 req->end_io(req, error);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05001739 else {
1740 if (blk_bidi_rq(req))
1741 __blk_put_request(req->next_rq->q, req->next_rq);
1742
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 __blk_put_request(req->q, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 }
1745}
1746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747static inline void __end_request(struct request *rq, int uptodate,
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05001748 unsigned int nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05001750 int error = 0;
1751
1752 if (uptodate <= 0)
1753 error = uptodate ? uptodate : -EIO;
1754
1755 __blk_end_request(rq, error, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756}
1757
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05001758/**
1759 * blk_rq_bytes - Returns bytes left to complete in the entire request
Randy Dunlap5d87a052008-02-20 09:01:22 +01001760 * @rq: the request being processed
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05001761 **/
1762unsigned int blk_rq_bytes(struct request *rq)
Jens Axboea0cd1282007-09-21 10:41:07 +02001763{
1764 if (blk_fs_request(rq))
1765 return rq->hard_nr_sectors << 9;
1766
1767 return rq->data_len;
1768}
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05001769EXPORT_SYMBOL_GPL(blk_rq_bytes);
1770
1771/**
1772 * blk_rq_cur_bytes - Returns bytes left to complete in the current segment
Randy Dunlap5d87a052008-02-20 09:01:22 +01001773 * @rq: the request being processed
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05001774 **/
1775unsigned int blk_rq_cur_bytes(struct request *rq)
1776{
1777 if (blk_fs_request(rq))
1778 return rq->current_nr_sectors << 9;
1779
1780 if (rq->bio)
1781 return rq->bio->bi_size;
1782
1783 return rq->data_len;
1784}
1785EXPORT_SYMBOL_GPL(blk_rq_cur_bytes);
Jens Axboea0cd1282007-09-21 10:41:07 +02001786
1787/**
1788 * end_queued_request - end all I/O on a queued request
1789 * @rq: the request being processed
1790 * @uptodate: error value or 0/1 uptodate flag
1791 *
1792 * Description:
1793 * Ends all I/O on a request, and removes it from the block layer queues.
1794 * Not suitable for normal IO completion, unless the driver still has
1795 * the request attached to the block layer.
1796 *
1797 **/
1798void end_queued_request(struct request *rq, int uptodate)
1799{
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05001800 __end_request(rq, uptodate, blk_rq_bytes(rq));
Jens Axboea0cd1282007-09-21 10:41:07 +02001801}
1802EXPORT_SYMBOL(end_queued_request);
1803
1804/**
1805 * end_dequeued_request - end all I/O on a dequeued request
1806 * @rq: the request being processed
1807 * @uptodate: error value or 0/1 uptodate flag
1808 *
1809 * Description:
1810 * Ends all I/O on a request. The request must already have been
1811 * dequeued using blkdev_dequeue_request(), as is normally the case
1812 * for most drivers.
1813 *
1814 **/
1815void end_dequeued_request(struct request *rq, int uptodate)
1816{
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05001817 __end_request(rq, uptodate, blk_rq_bytes(rq));
Jens Axboea0cd1282007-09-21 10:41:07 +02001818}
1819EXPORT_SYMBOL(end_dequeued_request);
1820
1821
1822/**
1823 * end_request - end I/O on the current segment of the request
Randy Dunlap8f731f72007-10-18 23:39:28 -07001824 * @req: the request being processed
Jens Axboea0cd1282007-09-21 10:41:07 +02001825 * @uptodate: error value or 0/1 uptodate flag
1826 *
1827 * Description:
1828 * Ends I/O on the current segment of a request. If that is the only
1829 * remaining segment, the request is also completed and freed.
1830 *
1831 * This is a remnant of how older block drivers handled IO completions.
1832 * Modern drivers typically end IO on the full request in one go, unless
1833 * they have a residual value to account for. For that case this function
1834 * isn't really useful, unless the residual just happens to be the
1835 * full current segment. In other words, don't use this function in new
1836 * code. Either use end_request_completely(), or the
1837 * end_that_request_chunk() (along with end_that_request_last()) for
1838 * partial completions.
1839 *
1840 **/
1841void end_request(struct request *req, int uptodate)
1842{
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05001843 __end_request(req, uptodate, req->hard_cur_sectors << 9);
Jens Axboea0cd1282007-09-21 10:41:07 +02001844}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845EXPORT_SYMBOL(end_request);
1846
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001847/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001848 * blk_end_io - Generic end_io function to complete a request.
1849 * @rq: the request being processed
1850 * @error: 0 for success, < 0 for error
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001851 * @nr_bytes: number of bytes to complete @rq
1852 * @bidi_bytes: number of bytes to complete @rq->next_rq
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001853 * @drv_callback: function called between completion of bios in the request
1854 * and completion of the request.
1855 * If the callback returns non 0, this helper returns without
1856 * completion of the request.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001857 *
1858 * Description:
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001859 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001860 * If @rq has leftover, sets it up for the next range of segments.
1861 *
1862 * Return:
1863 * 0 - we are done with this request
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001864 * 1 - this request is not freed yet, it still has pending buffers.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001865 **/
Jens Axboe22b13212008-01-31 12:36:19 +01001866static int blk_end_io(struct request *rq, int error, unsigned int nr_bytes,
1867 unsigned int bidi_bytes,
1868 int (drv_callback)(struct request *))
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001869{
1870 struct request_queue *q = rq->q;
1871 unsigned long flags = 0UL;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001872
1873 if (blk_fs_request(rq) || blk_pc_request(rq)) {
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001874 if (__end_that_request_first(rq, error, nr_bytes))
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001875 return 1;
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001876
1877 /* Bidi request must be completed as a whole */
1878 if (blk_bidi_rq(rq) &&
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001879 __end_that_request_first(rq->next_rq, error, bidi_bytes))
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001880 return 1;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001881 }
1882
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001883 /* Special feature for tricky drivers */
1884 if (drv_callback && drv_callback(rq))
1885 return 1;
1886
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001887 add_disk_randomness(rq->rq_disk);
1888
1889 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05001890 end_that_request_last(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001891 spin_unlock_irqrestore(q->queue_lock, flags);
1892
1893 return 0;
1894}
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001895
1896/**
1897 * blk_end_request - Helper function for drivers to complete the request.
1898 * @rq: the request being processed
1899 * @error: 0 for success, < 0 for error
1900 * @nr_bytes: number of bytes to complete
1901 *
1902 * Description:
1903 * Ends I/O on a number of bytes attached to @rq.
1904 * If @rq has leftover, sets it up for the next range of segments.
1905 *
1906 * Return:
1907 * 0 - we are done with this request
1908 * 1 - still buffers pending for this request
1909 **/
Jens Axboe22b13212008-01-31 12:36:19 +01001910int blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001911{
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001912 return blk_end_io(rq, error, nr_bytes, 0, NULL);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001913}
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001914EXPORT_SYMBOL_GPL(blk_end_request);
1915
1916/**
1917 * __blk_end_request - Helper function for drivers to complete the request.
1918 * @rq: the request being processed
1919 * @error: 0 for success, < 0 for error
1920 * @nr_bytes: number of bytes to complete
1921 *
1922 * Description:
1923 * Must be called with queue lock held unlike blk_end_request().
1924 *
1925 * Return:
1926 * 0 - we are done with this request
1927 * 1 - still buffers pending for this request
1928 **/
Jens Axboe22b13212008-01-31 12:36:19 +01001929int __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001930{
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001931 if (blk_fs_request(rq) || blk_pc_request(rq)) {
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001932 if (__end_that_request_first(rq, error, nr_bytes))
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001933 return 1;
1934 }
1935
1936 add_disk_randomness(rq->rq_disk);
1937
Kiyoshi Uedab8286232007-12-11 17:53:24 -05001938 end_that_request_last(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001939
1940 return 0;
1941}
1942EXPORT_SYMBOL_GPL(__blk_end_request);
1943
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001944/**
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001945 * blk_end_bidi_request - Helper function for drivers to complete bidi request.
1946 * @rq: the bidi request being processed
1947 * @error: 0 for success, < 0 for error
1948 * @nr_bytes: number of bytes to complete @rq
1949 * @bidi_bytes: number of bytes to complete @rq->next_rq
1950 *
1951 * Description:
1952 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
1953 *
1954 * Return:
1955 * 0 - we are done with this request
1956 * 1 - still buffers pending for this request
1957 **/
Jens Axboe22b13212008-01-31 12:36:19 +01001958int blk_end_bidi_request(struct request *rq, int error, unsigned int nr_bytes,
1959 unsigned int bidi_bytes)
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001960{
1961 return blk_end_io(rq, error, nr_bytes, bidi_bytes, NULL);
1962}
1963EXPORT_SYMBOL_GPL(blk_end_bidi_request);
1964
1965/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001966 * blk_end_request_callback - Special helper function for tricky drivers
1967 * @rq: the request being processed
1968 * @error: 0 for success, < 0 for error
1969 * @nr_bytes: number of bytes to complete
1970 * @drv_callback: function called between completion of bios in the request
1971 * and completion of the request.
1972 * If the callback returns non 0, this helper returns without
1973 * completion of the request.
1974 *
1975 * Description:
1976 * Ends I/O on a number of bytes attached to @rq.
1977 * If @rq has leftover, sets it up for the next range of segments.
1978 *
1979 * This special helper function is used only for existing tricky drivers.
1980 * (e.g. cdrom_newpc_intr() of ide-cd)
1981 * This interface will be removed when such drivers are rewritten.
1982 * Don't use this interface in other places anymore.
1983 *
1984 * Return:
1985 * 0 - we are done with this request
1986 * 1 - this request is not freed yet.
1987 * this request still has pending buffers or
1988 * the driver doesn't want to finish this request yet.
1989 **/
Jens Axboe22b13212008-01-31 12:36:19 +01001990int blk_end_request_callback(struct request *rq, int error,
1991 unsigned int nr_bytes,
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001992 int (drv_callback)(struct request *))
1993{
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001994 return blk_end_io(rq, error, nr_bytes, 0, drv_callback);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001995}
1996EXPORT_SYMBOL_GPL(blk_end_request_callback);
1997
Jens Axboe86db1e22008-01-29 14:53:40 +01001998void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
1999 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
Jens Axboe4aff5e22006-08-10 08:44:47 +02002001 /* first two bits are identical in rq->cmd_flags and bio->bi_rw */
2002 rq->cmd_flags |= (bio->bi_rw & 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
2004 rq->nr_phys_segments = bio_phys_segments(q, bio);
2005 rq->nr_hw_segments = bio_hw_segments(q, bio);
2006 rq->current_nr_sectors = bio_cur_sectors(bio);
2007 rq->hard_cur_sectors = rq->current_nr_sectors;
2008 rq->hard_nr_sectors = rq->nr_sectors = bio_sectors(bio);
2009 rq->buffer = bio_data(bio);
Mike Christie0e75f902006-12-01 10:40:55 +01002010 rq->data_len = bio->bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
2012 rq->bio = rq->biotail = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
NeilBrown66846572007-08-16 13:31:28 +02002014 if (bio->bi_bdev)
2015 rq->rq_disk = bio->bi_bdev->bd_disk;
2016}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
2018int kblockd_schedule_work(struct work_struct *work)
2019{
2020 return queue_work(kblockd_workqueue, work);
2021}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022EXPORT_SYMBOL(kblockd_schedule_work);
2023
Andrew Morton19a75d82007-05-09 02:33:56 -07002024void kblockd_flush_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025{
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002026 cancel_work_sync(work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027}
Andrew Morton19a75d82007-05-09 02:33:56 -07002028EXPORT_SYMBOL(kblockd_flush_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
2030int __init blk_dev_init(void)
2031{
Jens Axboeff856ba2006-01-09 16:02:34 +01002032 int i;
2033
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 kblockd_workqueue = create_workqueue("kblockd");
2035 if (!kblockd_workqueue)
2036 panic("Failed to create kblockd\n");
2037
2038 request_cachep = kmem_cache_create("blkdev_requests",
Paul Mundt20c2df82007-07-20 10:11:58 +09002039 sizeof(struct request), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Jens Axboe8324aa92008-01-29 14:51:59 +01002041 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02002042 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002044 for_each_possible_cpu(i)
Jens Axboeff856ba2006-01-09 16:02:34 +01002045 INIT_LIST_HEAD(&per_cpu(blk_cpu_done, i));
2046
2047 open_softirq(BLOCK_SOFTIRQ, blk_done_softirq, NULL);
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07002048 register_hotcpu_notifier(&blk_cpu_notifier);
Jens Axboeff856ba2006-01-09 16:02:34 +01002049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 return 0;
2051}
2052