blob: fba4ca7c608648069403c7256a63c38352a25a40 [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 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 */
Jens Axboe8324aa92008-01-29 14:51:59 +010041struct 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) {
63 __disk_stat_inc(rq->rq_disk, merges[rw]);
64 } else {
65 disk_round_stats(rq->rq_disk);
66 rq->rq_disk->in_flight++;
67 }
68}
69
Jens Axboe8324aa92008-01-29 14:51:59 +010070void blk_queue_congestion_threshold(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 int nr;
73
74 nr = q->nr_requests - (q->nr_requests / 8) + 1;
75 if (nr > q->nr_requests)
76 nr = q->nr_requests;
77 q->nr_congestion_on = nr;
78
79 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
80 if (nr < 1)
81 nr = 1;
82 q->nr_congestion_off = nr;
83}
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/**
86 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
87 * @bdev: device
88 *
89 * Locates the passed device's request queue and returns the address of its
90 * backing_dev_info
91 *
92 * Will return NULL if the request queue cannot be located.
93 */
94struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
95{
96 struct backing_dev_info *ret = NULL;
Jens Axboe165125e2007-07-24 09:28:11 +020097 struct request_queue *q = bdev_get_queue(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 if (q)
100 ret = &q->backing_dev_info;
101 return ret;
102}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103EXPORT_SYMBOL(blk_get_backing_dev_info);
104
Jens Axboe63a71382008-02-08 12:41:03 +0100105/*
106 * We can't just memset() the structure, since the allocation path
107 * already stored some information in the request.
108 */
Jens Axboe86db1e22008-01-29 14:53:40 +0100109void rq_init(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 INIT_LIST_HEAD(&rq->queuelist);
Jens Axboeff856ba2006-01-09 16:02:34 +0100112 INIT_LIST_HEAD(&rq->donelist);
Jens Axboe63a71382008-02-08 12:41:03 +0100113 rq->q = q;
114 rq->sector = rq->hard_sector = (sector_t) -1;
115 rq->nr_sectors = rq->hard_nr_sectors = 0;
116 rq->current_nr_sectors = rq->hard_cur_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 rq->bio = rq->biotail = NULL;
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->rq_disk = NULL;
Mike Christie df46b9a2005-06-20 14:04:44 +0200121 rq->nr_phys_segments = 0;
Jens Axboe63a71382008-02-08 12:41:03 +0100122 rq->nr_hw_segments = 0;
123 rq->ioprio = 0;
124 rq->special = NULL;
125 rq->buffer = NULL;
126 rq->tag = -1;
127 rq->errors = 0;
128 rq->ref_count = 1;
129 rq->cmd_len = 0;
130 memset(rq->cmd, 0, sizeof(rq->cmd));
131 rq->data_len = 0;
132 rq->sense_len = 0;
133 rq->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 rq->sense = NULL;
135 rq->end_io = NULL;
136 rq->end_io_data = NULL;
FUJITA Tomonoriabae1fd2007-07-16 08:52:14 +0200137 rq->next_rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
NeilBrown5bb23a62007-09-27 12:46:13 +0200140static void req_bio_endio(struct request *rq, struct bio *bio,
141 unsigned int nbytes, int error)
Tejun Heo797e7db2006-01-06 09:51:03 +0100142{
Jens Axboe165125e2007-07-24 09:28:11 +0200143 struct request_queue *q = rq->q;
Tejun Heo797e7db2006-01-06 09:51:03 +0100144
NeilBrown5bb23a62007-09-27 12:46:13 +0200145 if (&q->bar_rq != rq) {
146 if (error)
147 clear_bit(BIO_UPTODATE, &bio->bi_flags);
148 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
149 error = -EIO;
Tejun Heo797e7db2006-01-06 09:51:03 +0100150
NeilBrown5bb23a62007-09-27 12:46:13 +0200151 if (unlikely(nbytes > bio->bi_size)) {
Jens Axboe6728cb02008-01-31 13:03:55 +0100152 printk(KERN_ERR "%s: want %u bytes done, %u left\n",
NeilBrown5bb23a62007-09-27 12:46:13 +0200153 __FUNCTION__, nbytes, bio->bi_size);
154 nbytes = bio->bi_size;
155 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100156
NeilBrown5bb23a62007-09-27 12:46:13 +0200157 bio->bi_size -= nbytes;
158 bio->bi_sector += (nbytes >> 9);
159 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
Jens Axboe6728cb02008-01-31 13:03:55 +0100180 printk(KERN_INFO " sector %llu, nr/cnr %lu/%u\n",
181 (unsigned long long)rq->sector,
182 rq->nr_sectors,
183 rq->current_nr_sectors);
184 printk(KERN_INFO " bio %p, biotail %p, buffer %p, data %p, len %u\n",
185 rq->bio, rq->biotail,
186 rq->buffer, rq->data,
187 rq->data_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Jens Axboe4aff5e22006-08-10 08:44:47 +0200189 if (blk_pc_request(rq)) {
Jens Axboe6728cb02008-01-31 13:03:55 +0100190 printk(KERN_INFO " cdb: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 for (bit = 0; bit < sizeof(rq->cmd); bit++)
192 printk("%02x ", rq->cmd[bit]);
193 printk("\n");
194 }
195}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196EXPORT_SYMBOL(blk_dump_rq_flags);
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198/*
199 * "plug" the device if there are no outstanding requests: this will
200 * force the transfer to start only after we have put all the requests
201 * on the list.
202 *
203 * This is called with interrupts off and no requests on the queue and
204 * with the queue lock held.
205 */
Jens Axboe165125e2007-07-24 09:28:11 +0200206void blk_plug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 WARN_ON(!irqs_disabled());
209
210 /*
211 * don't plug a stopped queue, it must be paired with blk_start_queue()
212 * which will restart the queueing
213 */
Coywolf Qi Hunt7daac492006-04-19 10:14:49 +0200214 if (blk_queue_stopped(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return;
216
Jens Axboe2056a782006-03-23 20:00:26 +0100217 if (!test_and_set_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
Jens Axboe2056a782006-03-23 20:00:26 +0100219 blk_add_trace_generic(q, NULL, 0, BLK_TA_PLUG);
220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222EXPORT_SYMBOL(blk_plug_device);
223
224/*
225 * remove the queue from the plugged list, if present. called with
226 * queue lock held and interrupts disabled.
227 */
Jens Axboe165125e2007-07-24 09:28:11 +0200228int blk_remove_plug(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 WARN_ON(!irqs_disabled());
231
232 if (!test_and_clear_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags))
233 return 0;
234
235 del_timer(&q->unplug_timer);
236 return 1;
237}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238EXPORT_SYMBOL(blk_remove_plug);
239
240/*
241 * remove the plug and let it rip..
242 */
Jens Axboe165125e2007-07-24 09:28:11 +0200243void __generic_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Coywolf Qi Hunt7daac492006-04-19 10:14:49 +0200245 if (unlikely(blk_queue_stopped(q)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return;
247
248 if (!blk_remove_plug(q))
249 return;
250
Jens Axboe22e2c502005-06-27 10:55:12 +0200251 q->request_fn(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253EXPORT_SYMBOL(__generic_unplug_device);
254
255/**
256 * generic_unplug_device - fire a request queue
Jens Axboe165125e2007-07-24 09:28:11 +0200257 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 *
259 * Description:
260 * Linux uses plugging to build bigger requests queues before letting
261 * the device have at them. If a queue is plugged, the I/O scheduler
262 * is still adding and merging requests on the queue. Once the queue
263 * gets unplugged, the request_fn defined for the queue is invoked and
264 * transfers started.
265 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200266void generic_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
268 spin_lock_irq(q->queue_lock);
269 __generic_unplug_device(q);
270 spin_unlock_irq(q->queue_lock);
271}
272EXPORT_SYMBOL(generic_unplug_device);
273
274static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
275 struct page *page)
276{
Jens Axboe165125e2007-07-24 09:28:11 +0200277 struct request_queue *q = bdi->unplug_io_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500279 blk_unplug(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
Jens Axboe86db1e22008-01-29 14:53:40 +0100282void blk_unplug_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Jens Axboe165125e2007-07-24 09:28:11 +0200284 struct request_queue *q =
285 container_of(work, struct request_queue, unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Jens Axboe2056a782006-03-23 20:00:26 +0100287 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
288 q->rq.count[READ] + q->rq.count[WRITE]);
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 q->unplug_fn(q);
291}
292
Jens Axboe86db1e22008-01-29 14:53:40 +0100293void blk_unplug_timeout(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Jens Axboe165125e2007-07-24 09:28:11 +0200295 struct request_queue *q = (struct request_queue *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Jens Axboe2056a782006-03-23 20:00:26 +0100297 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_TIMER, NULL,
298 q->rq.count[READ] + q->rq.count[WRITE]);
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 kblockd_schedule_work(&q->unplug_work);
301}
302
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500303void blk_unplug(struct request_queue *q)
304{
305 /*
306 * devices don't necessarily have an ->unplug_fn defined
307 */
308 if (q->unplug_fn) {
309 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
310 q->rq.count[READ] + q->rq.count[WRITE]);
311
312 q->unplug_fn(q);
313 }
314}
315EXPORT_SYMBOL(blk_unplug);
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317/**
318 * blk_start_queue - restart a previously stopped queue
Jens Axboe165125e2007-07-24 09:28:11 +0200319 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 *
321 * Description:
322 * blk_start_queue() will clear the stop flag on the queue, and call
323 * the request_fn for the queue if it was in a stopped state when
324 * entered. Also see blk_stop_queue(). Queue lock must be held.
325 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200326void blk_start_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200328 WARN_ON(!irqs_disabled());
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 clear_bit(QUEUE_FLAG_STOPPED, &q->queue_flags);
331
332 /*
333 * one level of recursion is ok and is much faster than kicking
334 * the unplug handling
335 */
336 if (!test_and_set_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) {
337 q->request_fn(q);
338 clear_bit(QUEUE_FLAG_REENTER, &q->queue_flags);
339 } else {
340 blk_plug_device(q);
341 kblockd_schedule_work(&q->unplug_work);
342 }
343}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344EXPORT_SYMBOL(blk_start_queue);
345
346/**
347 * blk_stop_queue - stop a queue
Jens Axboe165125e2007-07-24 09:28:11 +0200348 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 *
350 * Description:
351 * The Linux block layer assumes that a block driver will consume all
352 * entries on the request queue when the request_fn strategy is called.
353 * Often this will not happen, because of hardware limitations (queue
354 * depth settings). If a device driver gets a 'queue full' response,
355 * or if it simply chooses not to queue more I/O at one point, it can
356 * call this function to prevent the request_fn from being called until
357 * the driver has signalled it's ready to go again. This happens by calling
358 * blk_start_queue() to restart queue operations. Queue lock must be held.
359 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200360void blk_stop_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
362 blk_remove_plug(q);
363 set_bit(QUEUE_FLAG_STOPPED, &q->queue_flags);
364}
365EXPORT_SYMBOL(blk_stop_queue);
366
367/**
368 * blk_sync_queue - cancel any pending callbacks on a queue
369 * @q: the queue
370 *
371 * Description:
372 * The block layer may perform asynchronous callback activity
373 * on a queue, such as calling the unplug function after a timeout.
374 * A block device may call blk_sync_queue to ensure that any
375 * such activity is cancelled, thus allowing it to release resources
Michael Opdenacker59c51592007-05-09 08:57:56 +0200376 * that the callbacks might use. The caller must already have made sure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 * that its ->make_request_fn will not re-add plugging prior to calling
378 * this function.
379 *
380 */
381void blk_sync_queue(struct request_queue *q)
382{
383 del_timer_sync(&q->unplug_timer);
Oleg Nesterovabbeb882007-10-23 15:08:19 +0200384 kblockd_flush_work(&q->unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386EXPORT_SYMBOL(blk_sync_queue);
387
388/**
389 * blk_run_queue - run a single device queue
390 * @q: The queue to run
391 */
392void blk_run_queue(struct request_queue *q)
393{
394 unsigned long flags;
395
396 spin_lock_irqsave(q->queue_lock, flags);
397 blk_remove_plug(q);
Jens Axboedac07ec2006-05-11 08:20:16 +0200398
399 /*
400 * Only recurse once to avoid overrunning the stack, let the unplug
401 * handling reinvoke the handler shortly if we already got there.
402 */
403 if (!elv_queue_empty(q)) {
404 if (!test_and_set_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) {
405 q->request_fn(q);
406 clear_bit(QUEUE_FLAG_REENTER, &q->queue_flags);
407 } else {
408 blk_plug_device(q);
409 kblockd_schedule_work(&q->unplug_work);
410 }
411 }
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 spin_unlock_irqrestore(q->queue_lock, flags);
414}
415EXPORT_SYMBOL(blk_run_queue);
416
Jens Axboe165125e2007-07-24 09:28:11 +0200417void blk_put_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500418{
419 kobject_put(&q->kobj);
420}
421EXPORT_SYMBOL(blk_put_queue);
422
Jens Axboe6728cb02008-01-31 13:03:55 +0100423void blk_cleanup_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500424{
425 mutex_lock(&q->sysfs_lock);
426 set_bit(QUEUE_FLAG_DEAD, &q->queue_flags);
427 mutex_unlock(&q->sysfs_lock);
428
429 if (q->elevator)
430 elevator_exit(q->elevator);
431
432 blk_put_queue(q);
433}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434EXPORT_SYMBOL(blk_cleanup_queue);
435
Jens Axboe165125e2007-07-24 09:28:11 +0200436static int blk_init_free_list(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
438 struct request_list *rl = &q->rq;
439
440 rl->count[READ] = rl->count[WRITE] = 0;
441 rl->starved[READ] = rl->starved[WRITE] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200442 rl->elvpriv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 init_waitqueue_head(&rl->wait[READ]);
444 init_waitqueue_head(&rl->wait[WRITE]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Christoph Lameter19460892005-06-23 00:08:19 -0700446 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
447 mempool_free_slab, request_cachep, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 if (!rl->rq_pool)
450 return -ENOMEM;
451
452 return 0;
453}
454
Jens Axboe165125e2007-07-24 09:28:11 +0200455struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Christoph Lameter19460892005-06-23 00:08:19 -0700457 return blk_alloc_queue_node(gfp_mask, -1);
458}
459EXPORT_SYMBOL(blk_alloc_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Jens Axboe165125e2007-07-24 09:28:11 +0200461struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -0700462{
Jens Axboe165125e2007-07-24 09:28:11 +0200463 struct request_queue *q;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700464 int err;
Christoph Lameter19460892005-06-23 00:08:19 -0700465
Jens Axboe8324aa92008-01-29 14:51:59 +0100466 q = kmem_cache_alloc_node(blk_requestq_cachep,
Christoph Lameter94f60302007-07-17 04:03:29 -0700467 gfp_mask | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (!q)
469 return NULL;
470
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700471 q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
472 q->backing_dev_info.unplug_io_data = q;
473 err = bdi_init(&q->backing_dev_info);
474 if (err) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100475 kmem_cache_free(blk_requestq_cachep, q);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700476 return NULL;
477 }
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 init_timer(&q->unplug_timer);
Al Viro483f4af2006-03-18 18:34:37 -0500480
Jens Axboe8324aa92008-01-29 14:51:59 +0100481 kobject_init(&q->kobj, &blk_queue_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Al Viro483f4af2006-03-18 18:34:37 -0500483 mutex_init(&q->sysfs_lock);
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return q;
486}
Christoph Lameter19460892005-06-23 00:08:19 -0700487EXPORT_SYMBOL(blk_alloc_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489/**
490 * blk_init_queue - prepare a request queue for use with a block device
491 * @rfn: The function to be called to process requests that have been
492 * placed on the queue.
493 * @lock: Request queue spin lock
494 *
495 * Description:
496 * If a block device wishes to use the standard request handling procedures,
497 * which sorts requests and coalesces adjacent requests, then it must
498 * call blk_init_queue(). The function @rfn will be called when there
499 * are requests on the queue that need to be processed. If the device
500 * supports plugging, then @rfn may not be called immediately when requests
501 * are available on the queue, but may be called at some time later instead.
502 * Plugged queues are generally unplugged when a buffer belonging to one
503 * of the requests on the queue is needed, or due to memory pressure.
504 *
505 * @rfn is not required, or even expected, to remove all requests off the
506 * queue, but only as many as it can handle at a time. If it does leave
507 * requests on the queue, it is responsible for arranging that the requests
508 * get dealt with eventually.
509 *
510 * The queue spin lock must be held while manipulating the requests on the
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200511 * request queue; this lock will be taken also from interrupt context, so irq
512 * disabling is needed for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 *
514 * Function returns a pointer to the initialized request queue, or NULL if
515 * it didn't succeed.
516 *
517 * Note:
518 * blk_init_queue() must be paired with a blk_cleanup_queue() call
519 * when the block device is deactivated (such as at module unload).
520 **/
Christoph Lameter19460892005-06-23 00:08:19 -0700521
Jens Axboe165125e2007-07-24 09:28:11 +0200522struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Christoph Lameter19460892005-06-23 00:08:19 -0700524 return blk_init_queue_node(rfn, lock, -1);
525}
526EXPORT_SYMBOL(blk_init_queue);
527
Jens Axboe165125e2007-07-24 09:28:11 +0200528struct request_queue *
Christoph Lameter19460892005-06-23 00:08:19 -0700529blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
530{
Jens Axboe165125e2007-07-24 09:28:11 +0200531 struct request_queue *q = blk_alloc_queue_node(GFP_KERNEL, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 if (!q)
534 return NULL;
535
Christoph Lameter19460892005-06-23 00:08:19 -0700536 q->node = node_id;
Al Viro8669aaf2006-03-18 13:50:00 -0500537 if (blk_init_free_list(q)) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100538 kmem_cache_free(blk_requestq_cachep, q);
Al Viro8669aaf2006-03-18 13:50:00 -0500539 return NULL;
540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
152587d2005-04-12 16:22:06 -0500542 /*
543 * if caller didn't supply a lock, they get per-queue locking with
544 * our embedded lock
545 */
546 if (!lock) {
547 spin_lock_init(&q->__queue_lock);
548 lock = &q->__queue_lock;
549 }
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 q->request_fn = rfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 q->prep_rq_fn = NULL;
553 q->unplug_fn = generic_unplug_device;
554 q->queue_flags = (1 << QUEUE_FLAG_CLUSTER);
555 q->queue_lock = lock;
556
557 blk_queue_segment_boundary(q, 0xffffffff);
558
559 blk_queue_make_request(q, __make_request);
560 blk_queue_max_segment_size(q, MAX_SEGMENT_SIZE);
561
562 blk_queue_max_hw_segments(q, MAX_HW_SEGMENTS);
563 blk_queue_max_phys_segments(q, MAX_PHYS_SEGMENTS);
564
Alan Stern44ec9542007-02-20 11:01:57 -0500565 q->sg_reserved_size = INT_MAX;
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 /*
568 * all done
569 */
570 if (!elevator_init(q, NULL)) {
571 blk_queue_congestion_threshold(q);
572 return q;
573 }
574
Al Viro8669aaf2006-03-18 13:50:00 -0500575 blk_put_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return NULL;
577}
Christoph Lameter19460892005-06-23 00:08:19 -0700578EXPORT_SYMBOL(blk_init_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Jens Axboe165125e2007-07-24 09:28:11 +0200580int blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Nick Pigginfde6ad22005-06-23 00:08:53 -0700582 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
Al Viro483f4af2006-03-18 18:34:37 -0500583 kobject_get(&q->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return 0;
585 }
586
587 return 1;
588}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589EXPORT_SYMBOL(blk_get_queue);
590
Jens Axboe165125e2007-07-24 09:28:11 +0200591static inline void blk_free_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Jens Axboe4aff5e22006-08-10 08:44:47 +0200593 if (rq->cmd_flags & REQ_ELVPRIV)
Tejun Heocb98fc82005-10-28 08:29:39 +0200594 elv_put_request(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 mempool_free(rq, q->rq.rq_pool);
596}
597
Jens Axboe1ea25ecb2006-07-18 22:24:11 +0200598static struct request *
Jens Axboe165125e2007-07-24 09:28:11 +0200599blk_alloc_request(struct request_queue *q, int rw, int priv, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
601 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
602
603 if (!rq)
604 return NULL;
605
606 /*
Jens Axboe4aff5e22006-08-10 08:44:47 +0200607 * first three bits are identical in rq->cmd_flags and bio->bi_rw,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 * see bio.h and blkdev.h
609 */
Jens Axboe49171e52006-08-10 08:59:11 +0200610 rq->cmd_flags = rw | REQ_ALLOCED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Tejun Heocb98fc82005-10-28 08:29:39 +0200612 if (priv) {
Jens Axboecb78b282006-07-28 09:32:57 +0200613 if (unlikely(elv_set_request(q, rq, gfp_mask))) {
Tejun Heocb98fc82005-10-28 08:29:39 +0200614 mempool_free(rq, q->rq.rq_pool);
615 return NULL;
616 }
Jens Axboe4aff5e22006-08-10 08:44:47 +0200617 rq->cmd_flags |= REQ_ELVPRIV;
Tejun Heocb98fc82005-10-28 08:29:39 +0200618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Tejun Heocb98fc82005-10-28 08:29:39 +0200620 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
623/*
624 * ioc_batching returns true if the ioc is a valid batching request and
625 * should be given priority access to a request.
626 */
Jens Axboe165125e2007-07-24 09:28:11 +0200627static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
629 if (!ioc)
630 return 0;
631
632 /*
633 * Make sure the process is able to allocate at least 1 request
634 * even if the batch times out, otherwise we could theoretically
635 * lose wakeups.
636 */
637 return ioc->nr_batch_requests == q->nr_batching ||
638 (ioc->nr_batch_requests > 0
639 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
640}
641
642/*
643 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
644 * will cause the process to be a "batcher" on all queues in the system. This
645 * is the behaviour we want though - once it gets a wakeup it should be given
646 * a nice run.
647 */
Jens Axboe165125e2007-07-24 09:28:11 +0200648static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
650 if (!ioc || ioc_batching(q, ioc))
651 return;
652
653 ioc->nr_batch_requests = q->nr_batching;
654 ioc->last_waited = jiffies;
655}
656
Jens Axboe165125e2007-07-24 09:28:11 +0200657static void __freed_request(struct request_queue *q, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
659 struct request_list *rl = &q->rq;
660
661 if (rl->count[rw] < queue_congestion_off_threshold(q))
Thomas Maier79e2de42006-10-19 23:28:15 -0700662 blk_clear_queue_congested(q, rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 if (rl->count[rw] + 1 <= q->nr_requests) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if (waitqueue_active(&rl->wait[rw]))
666 wake_up(&rl->wait[rw]);
667
668 blk_clear_queue_full(q, rw);
669 }
670}
671
672/*
673 * A request has just been released. Account for it, update the full and
674 * congestion status, wake up any waiters. Called under q->queue_lock.
675 */
Jens Axboe165125e2007-07-24 09:28:11 +0200676static void freed_request(struct request_queue *q, int rw, int priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
678 struct request_list *rl = &q->rq;
679
680 rl->count[rw]--;
Tejun Heocb98fc82005-10-28 08:29:39 +0200681 if (priv)
682 rl->elvpriv--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 __freed_request(q, rw);
685
686 if (unlikely(rl->starved[rw ^ 1]))
687 __freed_request(q, rw ^ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
690#define blkdev_free_rq(list) list_entry((list)->next, struct request, queuelist)
691/*
Nick Piggind6344532005-06-28 20:45:14 -0700692 * Get a free request, queue_lock must be held.
693 * Returns NULL on failure, with queue_lock held.
694 * Returns !NULL on success, with queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 */
Jens Axboe165125e2007-07-24 09:28:11 +0200696static struct request *get_request(struct request_queue *q, int rw_flags,
Jens Axboe7749a8d2006-12-13 13:02:26 +0100697 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
699 struct request *rq = NULL;
700 struct request_list *rl = &q->rq;
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100701 struct io_context *ioc = NULL;
Jens Axboe7749a8d2006-12-13 13:02:26 +0100702 const int rw = rw_flags & 0x01;
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100703 int may_queue, priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Jens Axboe7749a8d2006-12-13 13:02:26 +0100705 may_queue = elv_may_queue(q, rw_flags);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100706 if (may_queue == ELV_MQUEUE_NO)
707 goto rq_starved;
708
709 if (rl->count[rw]+1 >= queue_congestion_on_threshold(q)) {
710 if (rl->count[rw]+1 >= q->nr_requests) {
Jens Axboeb5deef92006-07-19 23:39:40 +0200711 ioc = current_io_context(GFP_ATOMIC, q->node);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100712 /*
713 * The queue will fill after this allocation, so set
714 * it as full, and mark this process as "batching".
715 * This process will be allowed to complete a batch of
716 * requests, others will be blocked.
717 */
718 if (!blk_queue_full(q, rw)) {
719 ioc_set_batching(q, ioc);
720 blk_set_queue_full(q, rw);
721 } else {
722 if (may_queue != ELV_MQUEUE_MUST
723 && !ioc_batching(q, ioc)) {
724 /*
725 * The queue is full and the allocating
726 * process is not a "batcher", and not
727 * exempted by the IO scheduler
728 */
729 goto out;
730 }
731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 }
Thomas Maier79e2de42006-10-19 23:28:15 -0700733 blk_set_queue_congested(q, rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
735
Jens Axboe082cf692005-06-28 16:35:11 +0200736 /*
737 * Only allow batching queuers to allocate up to 50% over the defined
738 * limit of requests, otherwise we could have thousands of requests
739 * allocated with any setting of ->nr_requests
740 */
Hugh Dickinsfd782a42005-06-29 15:15:40 +0100741 if (rl->count[rw] >= (3 * q->nr_requests / 2))
Jens Axboe082cf692005-06-28 16:35:11 +0200742 goto out;
Hugh Dickinsfd782a42005-06-29 15:15:40 +0100743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 rl->count[rw]++;
745 rl->starved[rw] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200746
Jens Axboe64521d12005-10-28 08:30:39 +0200747 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Tejun Heocb98fc82005-10-28 08:29:39 +0200748 if (priv)
749 rl->elvpriv++;
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 spin_unlock_irq(q->queue_lock);
752
Jens Axboe7749a8d2006-12-13 13:02:26 +0100753 rq = blk_alloc_request(q, rw_flags, priv, gfp_mask);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100754 if (unlikely(!rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 /*
756 * Allocation failed presumably due to memory. Undo anything
757 * we might have messed up.
758 *
759 * Allocating task should really be put onto the front of the
760 * wait queue, but this is pretty rare.
761 */
762 spin_lock_irq(q->queue_lock);
Tejun Heocb98fc82005-10-28 08:29:39 +0200763 freed_request(q, rw, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 /*
766 * in the very unlikely event that allocation failed and no
767 * requests for this direction was pending, mark us starved
768 * so that freeing of a request in the other direction will
769 * notice us. another possible fix would be to split the
770 * rq mempool into READ and WRITE
771 */
772rq_starved:
773 if (unlikely(rl->count[rw] == 0))
774 rl->starved[rw] = 1;
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 goto out;
777 }
778
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100779 /*
780 * ioc may be NULL here, and ioc_batching will be false. That's
781 * OK, if the queue is under the request limit then requests need
782 * not count toward the nr_batch_requests limit. There will always
783 * be some limit enforced by BLK_BATCH_TIME.
784 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (ioc_batching(q, ioc))
786 ioc->nr_batch_requests--;
Jens Axboe6728cb02008-01-31 13:03:55 +0100787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 rq_init(q, rq);
Jens Axboe2056a782006-03-23 20:00:26 +0100789
790 blk_add_trace_generic(q, bio, rw, BLK_TA_GETRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return rq;
793}
794
795/*
796 * No available requests for this queue, unplug the device and wait for some
797 * requests to become available.
Nick Piggind6344532005-06-28 20:45:14 -0700798 *
799 * Called with q->queue_lock held, and returns with it unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 */
Jens Axboe165125e2007-07-24 09:28:11 +0200801static struct request *get_request_wait(struct request_queue *q, int rw_flags,
Jens Axboe22e2c502005-06-27 10:55:12 +0200802 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
Jens Axboe7749a8d2006-12-13 13:02:26 +0100804 const int rw = rw_flags & 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 struct request *rq;
806
Jens Axboe7749a8d2006-12-13 13:02:26 +0100807 rq = get_request(q, rw_flags, bio, GFP_NOIO);
Nick Piggin450991b2005-06-28 20:45:13 -0700808 while (!rq) {
809 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 struct request_list *rl = &q->rq;
811
812 prepare_to_wait_exclusive(&rl->wait[rw], &wait,
813 TASK_UNINTERRUPTIBLE);
814
Jens Axboe7749a8d2006-12-13 13:02:26 +0100815 rq = get_request(q, rw_flags, bio, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 if (!rq) {
818 struct io_context *ioc;
819
Jens Axboe2056a782006-03-23 20:00:26 +0100820 blk_add_trace_generic(q, bio, rw, BLK_TA_SLEEPRQ);
821
Nick Piggind6344532005-06-28 20:45:14 -0700822 __generic_unplug_device(q);
823 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 io_schedule();
825
826 /*
827 * After sleeping, we become a "batching" process and
828 * will be able to allocate at least one request, and
829 * up to a big batch of them for a small period time.
830 * See ioc_batching, ioc_set_batching
831 */
Jens Axboeb5deef92006-07-19 23:39:40 +0200832 ioc = current_io_context(GFP_NOIO, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 ioc_set_batching(q, ioc);
Nick Piggind6344532005-06-28 20:45:14 -0700834
835 spin_lock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
837 finish_wait(&rl->wait[rw], &wait);
Nick Piggin450991b2005-06-28 20:45:13 -0700838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 return rq;
841}
842
Jens Axboe165125e2007-07-24 09:28:11 +0200843struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
845 struct request *rq;
846
847 BUG_ON(rw != READ && rw != WRITE);
848
Nick Piggind6344532005-06-28 20:45:14 -0700849 spin_lock_irq(q->queue_lock);
850 if (gfp_mask & __GFP_WAIT) {
Jens Axboe22e2c502005-06-27 10:55:12 +0200851 rq = get_request_wait(q, rw, NULL);
Nick Piggind6344532005-06-28 20:45:14 -0700852 } else {
Jens Axboe22e2c502005-06-27 10:55:12 +0200853 rq = get_request(q, rw, NULL, gfp_mask);
Nick Piggind6344532005-06-28 20:45:14 -0700854 if (!rq)
855 spin_unlock_irq(q->queue_lock);
856 }
857 /* q->queue_lock is unlocked at this point */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 return rq;
860}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861EXPORT_SYMBOL(blk_get_request);
862
863/**
Jens Axboedc72ef42006-07-20 14:54:05 +0200864 * blk_start_queueing - initiate dispatch of requests to device
865 * @q: request queue to kick into gear
866 *
867 * This is basically a helper to remove the need to know whether a queue
868 * is plugged or not if someone just wants to initiate dispatch of requests
869 * for this queue.
870 *
871 * The queue lock must be held with interrupts disabled.
872 */
Jens Axboe165125e2007-07-24 09:28:11 +0200873void blk_start_queueing(struct request_queue *q)
Jens Axboedc72ef42006-07-20 14:54:05 +0200874{
875 if (!blk_queue_plugged(q))
876 q->request_fn(q);
877 else
878 __generic_unplug_device(q);
879}
880EXPORT_SYMBOL(blk_start_queueing);
881
882/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 * blk_requeue_request - put a request back on queue
884 * @q: request queue where request should be inserted
885 * @rq: request to be inserted
886 *
887 * Description:
888 * Drivers often keep queueing requests until the hardware cannot accept
889 * more, when that condition happens we need to put the request back
890 * on the queue. Must be called with queue lock held.
891 */
Jens Axboe165125e2007-07-24 09:28:11 +0200892void blk_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Jens Axboe2056a782006-03-23 20:00:26 +0100894 blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (blk_rq_tagged(rq))
897 blk_queue_end_tag(q, rq);
898
899 elv_requeue_request(q, rq);
900}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901EXPORT_SYMBOL(blk_requeue_request);
902
903/**
904 * blk_insert_request - insert a special request in to a request queue
905 * @q: request queue where request should be inserted
906 * @rq: request to be inserted
907 * @at_head: insert request at head or tail of queue
908 * @data: private data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 *
910 * Description:
911 * Many block devices need to execute commands asynchronously, so they don't
912 * block the whole kernel from preemption during request execution. This is
913 * accomplished normally by inserting aritficial requests tagged as
914 * REQ_SPECIAL in to the corresponding request queue, and letting them be
915 * scheduled for actual execution by the request queue.
916 *
917 * We have the option of inserting the head or the tail of the queue.
918 * Typically we use the tail for new ioctls and so forth. We use the head
919 * of the queue for things like a QUEUE_FULL message from a device, or a
920 * host that is unable to accept a particular command.
921 */
Jens Axboe165125e2007-07-24 09:28:11 +0200922void blk_insert_request(struct request_queue *q, struct request *rq,
Tejun Heo 867d1192005-04-24 02:06:05 -0500923 int at_head, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Tejun Heo 867d1192005-04-24 02:06:05 -0500925 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 unsigned long flags;
927
928 /*
929 * tell I/O scheduler that this isn't a regular read/write (ie it
930 * must not attempt merges on this) and that it acts as a soft
931 * barrier
932 */
Jens Axboe4aff5e22006-08-10 08:44:47 +0200933 rq->cmd_type = REQ_TYPE_SPECIAL;
934 rq->cmd_flags |= REQ_SOFTBARRIER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 rq->special = data;
937
938 spin_lock_irqsave(q->queue_lock, flags);
939
940 /*
941 * If command is tagged, release the tag
942 */
Tejun Heo 867d1192005-04-24 02:06:05 -0500943 if (blk_rq_tagged(rq))
944 blk_queue_end_tag(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Jerome Marchandb238b3d2007-10-23 15:05:46 +0200946 drive_stat_acct(rq, 1);
Tejun Heo 867d1192005-04-24 02:06:05 -0500947 __elv_add_request(q, rq, where, 0);
Jens Axboedc72ef42006-07-20 14:54:05 +0200948 blk_start_queueing(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 spin_unlock_irqrestore(q->queue_lock, flags);
950}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951EXPORT_SYMBOL(blk_insert_request);
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953/*
954 * add-request adds a request to the linked list.
955 * queue lock is held and interrupts disabled, as we muck with the
956 * request queue list.
957 */
Jens Axboe6728cb02008-01-31 13:03:55 +0100958static inline void add_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
Jerome Marchandb238b3d2007-10-23 15:05:46 +0200960 drive_stat_acct(req, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 /*
963 * elevator indicated where it wants this request to be
964 * inserted at elevator_merge time
965 */
966 __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
967}
Jens Axboe6728cb02008-01-31 13:03:55 +0100968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969/*
970 * disk_round_stats() - Round off the performance stats on a struct
971 * disk_stats.
972 *
973 * The average IO queue length and utilisation statistics are maintained
974 * by observing the current state of the queue length and the amount of
975 * time it has been in this state for.
976 *
977 * Normally, that accounting is done on IO completion, but that can result
978 * in more than a second's worth of IO being accounted for within any one
979 * second, leading to >100% utilisation. To deal with that, we call this
980 * function to do a round-off before returning the results when reading
981 * /proc/diskstats. This accounts immediately for all queue usage up to
982 * the current jiffies and restarts the counters again.
983 */
984void disk_round_stats(struct gendisk *disk)
985{
986 unsigned long now = jiffies;
987
Chen, Kenneth Wb2982642005-10-13 21:49:29 +0200988 if (now == disk->stamp)
989 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Chen, Kenneth W20e5c812005-10-13 21:48:42 +0200991 if (disk->in_flight) {
992 __disk_stat_add(disk, time_in_queue,
993 disk->in_flight * (now - disk->stamp));
994 __disk_stat_add(disk, io_ticks, (now - disk->stamp));
995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 disk->stamp = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800998EXPORT_SYMBOL_GPL(disk_round_stats);
999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000/*
1001 * queue lock must be held
1002 */
Jens Axboe165125e2007-07-24 09:28:11 +02001003void __blk_put_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (unlikely(!q))
1006 return;
1007 if (unlikely(--req->ref_count))
1008 return;
1009
Tejun Heo8922e162005-10-20 16:23:44 +02001010 elv_completed_request(q, req);
1011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 /*
1013 * Request may not have originated from ll_rw_blk. if not,
1014 * it didn't come out of our reserved rq pools
1015 */
Jens Axboe49171e52006-08-10 08:59:11 +02001016 if (req->cmd_flags & REQ_ALLOCED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 int rw = rq_data_dir(req);
Jens Axboe4aff5e22006-08-10 08:44:47 +02001018 int priv = req->cmd_flags & REQ_ELVPRIV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 BUG_ON(!list_empty(&req->queuelist));
Jens Axboe98170642006-07-28 09:23:08 +02001021 BUG_ON(!hlist_unhashed(&req->hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 blk_free_request(q, req);
Tejun Heocb98fc82005-10-28 08:29:39 +02001024 freed_request(q, rw, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
1026}
Mike Christie6e39b69e2005-11-11 05:30:24 -06001027EXPORT_SYMBOL_GPL(__blk_put_request);
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029void blk_put_request(struct request *req)
1030{
Tejun Heo8922e162005-10-20 16:23:44 +02001031 unsigned long flags;
Jens Axboe165125e2007-07-24 09:28:11 +02001032 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Tejun Heo8922e162005-10-20 16:23:44 +02001034 /*
1035 * Gee, IDE calls in w/ NULL q. Fix IDE and remove the
1036 * following if (q) test.
1037 */
1038 if (q) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 spin_lock_irqsave(q->queue_lock, flags);
1040 __blk_put_request(q, req);
1041 spin_unlock_irqrestore(q->queue_lock, flags);
1042 }
1043}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044EXPORT_SYMBOL(blk_put_request);
1045
Jens Axboe86db1e22008-01-29 14:53:40 +01001046void init_request_from_bio(struct request *req, struct bio *bio)
Tejun Heo52d9e672006-01-06 09:49:58 +01001047{
Jens Axboe4aff5e22006-08-10 08:44:47 +02001048 req->cmd_type = REQ_TYPE_FS;
Tejun Heo52d9e672006-01-06 09:49:58 +01001049
1050 /*
1051 * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
1052 */
1053 if (bio_rw_ahead(bio) || bio_failfast(bio))
Jens Axboe4aff5e22006-08-10 08:44:47 +02001054 req->cmd_flags |= REQ_FAILFAST;
Tejun Heo52d9e672006-01-06 09:49:58 +01001055
1056 /*
1057 * REQ_BARRIER implies no merging, but lets make it explicit
1058 */
1059 if (unlikely(bio_barrier(bio)))
Jens Axboe4aff5e22006-08-10 08:44:47 +02001060 req->cmd_flags |= (REQ_HARDBARRIER | REQ_NOMERGE);
Tejun Heo52d9e672006-01-06 09:49:58 +01001061
Jens Axboeb31dc662006-06-13 08:26:10 +02001062 if (bio_sync(bio))
Jens Axboe4aff5e22006-08-10 08:44:47 +02001063 req->cmd_flags |= REQ_RW_SYNC;
Jens Axboe5404bc72006-08-10 09:01:02 +02001064 if (bio_rw_meta(bio))
1065 req->cmd_flags |= REQ_RW_META;
Jens Axboeb31dc662006-06-13 08:26:10 +02001066
Tejun Heo52d9e672006-01-06 09:49:58 +01001067 req->errors = 0;
1068 req->hard_sector = req->sector = bio->bi_sector;
Tejun Heo52d9e672006-01-06 09:49:58 +01001069 req->ioprio = bio_prio(bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001070 req->start_time = jiffies;
NeilBrownbc1c56f2007-08-16 13:31:30 +02001071 blk_rq_bio_prep(req->q, req, bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001072}
1073
Jens Axboe165125e2007-07-24 09:28:11 +02001074static int __make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
Nick Piggin450991b2005-06-28 20:45:13 -07001076 struct request *req;
Jens Axboe51da90f2006-07-18 04:14:45 +02001077 int el_ret, nr_sectors, barrier, err;
1078 const unsigned short prio = bio_prio(bio);
1079 const int sync = bio_sync(bio);
Jens Axboe7749a8d2006-12-13 13:02:26 +01001080 int rw_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 nr_sectors = bio_sectors(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084 /*
1085 * low level driver can indicate that it wants pages above a
1086 * certain limit bounced to low memory (ie for highmem, or even
1087 * ISA dma in theory)
1088 */
1089 blk_queue_bounce(q, &bio);
1090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 barrier = bio_barrier(bio);
Tejun Heo797e7db2006-01-06 09:51:03 +01001092 if (unlikely(barrier) && (q->next_ordered == QUEUE_ORDERED_NONE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 err = -EOPNOTSUPP;
1094 goto end_io;
1095 }
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 spin_lock_irq(q->queue_lock);
1098
Nick Piggin450991b2005-06-28 20:45:13 -07001099 if (unlikely(barrier) || elv_queue_empty(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 goto get_rq;
1101
1102 el_ret = elv_merge(q, &req, bio);
1103 switch (el_ret) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001104 case ELEVATOR_BACK_MERGE:
1105 BUG_ON(!rq_mergeable(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Jens Axboe6728cb02008-01-31 13:03:55 +01001107 if (!ll_back_merge_fn(q, req, bio))
1108 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Jens Axboe6728cb02008-01-31 13:03:55 +01001110 blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
Jens Axboe2056a782006-03-23 20:00:26 +01001111
Jens Axboe6728cb02008-01-31 13:03:55 +01001112 req->biotail->bi_next = bio;
1113 req->biotail = bio;
1114 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
1115 req->ioprio = ioprio_best(req->ioprio, prio);
1116 drive_stat_acct(req, 0);
1117 if (!attempt_back_merge(q, req))
1118 elv_merged_request(q, req, el_ret);
1119 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Jens Axboe6728cb02008-01-31 13:03:55 +01001121 case ELEVATOR_FRONT_MERGE:
1122 BUG_ON(!rq_mergeable(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Jens Axboe6728cb02008-01-31 13:03:55 +01001124 if (!ll_front_merge_fn(q, req, bio))
1125 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Jens Axboe6728cb02008-01-31 13:03:55 +01001127 blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
Jens Axboe2056a782006-03-23 20:00:26 +01001128
Jens Axboe6728cb02008-01-31 13:03:55 +01001129 bio->bi_next = req->bio;
1130 req->bio = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Jens Axboe6728cb02008-01-31 13:03:55 +01001132 /*
1133 * may not be valid. if the low level driver said
1134 * it didn't need a bounce buffer then it better
1135 * not touch req->buffer either...
1136 */
1137 req->buffer = bio_data(bio);
1138 req->current_nr_sectors = bio_cur_sectors(bio);
1139 req->hard_cur_sectors = req->current_nr_sectors;
1140 req->sector = req->hard_sector = bio->bi_sector;
1141 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
1142 req->ioprio = ioprio_best(req->ioprio, prio);
1143 drive_stat_acct(req, 0);
1144 if (!attempt_front_merge(q, req))
1145 elv_merged_request(q, req, el_ret);
1146 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Jens Axboe6728cb02008-01-31 13:03:55 +01001148 /* ELV_NO_MERGE: elevator says don't/can't merge. */
1149 default:
1150 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153get_rq:
Nick Piggin450991b2005-06-28 20:45:13 -07001154 /*
Jens Axboe7749a8d2006-12-13 13:02:26 +01001155 * This sync check and mask will be re-done in init_request_from_bio(),
1156 * but we need to set it earlier to expose the sync flag to the
1157 * rq allocator and io schedulers.
1158 */
1159 rw_flags = bio_data_dir(bio);
1160 if (sync)
1161 rw_flags |= REQ_RW_SYNC;
1162
1163 /*
Nick Piggin450991b2005-06-28 20:45:13 -07001164 * Grab a free request. This is might sleep but can not fail.
Nick Piggind6344532005-06-28 20:45:14 -07001165 * Returns with the queue unlocked.
Nick Piggin450991b2005-06-28 20:45:13 -07001166 */
Jens Axboe7749a8d2006-12-13 13:02:26 +01001167 req = get_request_wait(q, rw_flags, bio);
Nick Piggind6344532005-06-28 20:45:14 -07001168
Nick Piggin450991b2005-06-28 20:45:13 -07001169 /*
1170 * After dropping the lock and possibly sleeping here, our request
1171 * may now be mergeable after it had proven unmergeable (above).
1172 * We don't worry about that case for efficiency. It won't happen
1173 * often, and the elevators are able to handle it.
1174 */
Tejun Heo52d9e672006-01-06 09:49:58 +01001175 init_request_from_bio(req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Nick Piggin450991b2005-06-28 20:45:13 -07001177 spin_lock_irq(q->queue_lock);
1178 if (elv_queue_empty(q))
1179 blk_plug_device(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 add_request(q, req);
1181out:
Jens Axboe4a534f92005-04-16 15:25:40 -07001182 if (sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 __generic_unplug_device(q);
1184
1185 spin_unlock_irq(q->queue_lock);
1186 return 0;
1187
1188end_io:
NeilBrown6712ecf2007-09-27 12:47:43 +02001189 bio_endio(bio, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 return 0;
1191}
1192
1193/*
1194 * If bio->bi_dev is a partition, remap the location
1195 */
1196static inline void blk_partition_remap(struct bio *bio)
1197{
1198 struct block_device *bdev = bio->bi_bdev;
1199
Jens Axboebf2de6f2007-09-27 13:01:25 +02001200 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 struct hd_struct *p = bdev->bd_part;
Jens Axboea3623572005-11-01 09:26:16 +01001202 const int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Jens Axboea3623572005-11-01 09:26:16 +01001204 p->sectors[rw] += bio_sectors(bio);
1205 p->ios[rw]++;
1206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 bio->bi_sector += p->start_sect;
1208 bio->bi_bdev = bdev->bd_contains;
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001209
1210 blk_add_trace_remap(bdev_get_queue(bio->bi_bdev), bio,
1211 bdev->bd_dev, bio->bi_sector,
1212 bio->bi_sector - p->start_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
1214}
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216static void handle_bad_sector(struct bio *bio)
1217{
1218 char b[BDEVNAME_SIZE];
1219
1220 printk(KERN_INFO "attempt to access beyond end of device\n");
1221 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1222 bdevname(bio->bi_bdev, b),
1223 bio->bi_rw,
1224 (unsigned long long)bio->bi_sector + bio_sectors(bio),
1225 (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
1226
1227 set_bit(BIO_EOF, &bio->bi_flags);
1228}
1229
Akinobu Mitac17bb492006-12-08 02:39:46 -08001230#ifdef CONFIG_FAIL_MAKE_REQUEST
1231
1232static DECLARE_FAULT_ATTR(fail_make_request);
1233
1234static int __init setup_fail_make_request(char *str)
1235{
1236 return setup_fault_attr(&fail_make_request, str);
1237}
1238__setup("fail_make_request=", setup_fail_make_request);
1239
1240static int should_fail_request(struct bio *bio)
1241{
1242 if ((bio->bi_bdev->bd_disk->flags & GENHD_FL_FAIL) ||
1243 (bio->bi_bdev->bd_part && bio->bi_bdev->bd_part->make_it_fail))
1244 return should_fail(&fail_make_request, bio->bi_size);
1245
1246 return 0;
1247}
1248
1249static int __init fail_make_request_debugfs(void)
1250{
1251 return init_fault_attr_dentries(&fail_make_request,
1252 "fail_make_request");
1253}
1254
1255late_initcall(fail_make_request_debugfs);
1256
1257#else /* CONFIG_FAIL_MAKE_REQUEST */
1258
1259static inline int should_fail_request(struct bio *bio)
1260{
1261 return 0;
1262}
1263
1264#endif /* CONFIG_FAIL_MAKE_REQUEST */
1265
Jens Axboec07e2b42007-07-18 13:27:58 +02001266/*
1267 * Check whether this bio extends beyond the end of the device.
1268 */
1269static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1270{
1271 sector_t maxsector;
1272
1273 if (!nr_sectors)
1274 return 0;
1275
1276 /* Test device or partition size, when known. */
1277 maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
1278 if (maxsector) {
1279 sector_t sector = bio->bi_sector;
1280
1281 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1282 /*
1283 * This may well happen - the kernel calls bread()
1284 * without checking the size of the device, e.g., when
1285 * mounting a device.
1286 */
1287 handle_bad_sector(bio);
1288 return 1;
1289 }
1290 }
1291
1292 return 0;
1293}
1294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295/**
1296 * generic_make_request: hand a buffer to its device driver for I/O
1297 * @bio: The bio describing the location in memory and on the device.
1298 *
1299 * generic_make_request() is used to make I/O requests of block
1300 * devices. It is passed a &struct bio, which describes the I/O that needs
1301 * to be done.
1302 *
1303 * generic_make_request() does not return any status. The
1304 * success/failure status of the request, along with notification of
1305 * completion, is delivered asynchronously through the bio->bi_end_io
1306 * function described (one day) else where.
1307 *
1308 * The caller of generic_make_request must make sure that bi_io_vec
1309 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1310 * set to describe the device address, and the
1311 * bi_end_io and optionally bi_private are set to describe how
1312 * completion notification should be signaled.
1313 *
1314 * generic_make_request and the drivers it calls may use bi_next if this
1315 * bio happens to be merged with someone else, and may change bi_dev and
1316 * bi_sector for remaps as it sees fit. So the values of these fields
1317 * should NOT be depended on after the call to generic_make_request.
1318 */
Neil Brownd89d8792007-05-01 09:53:42 +02001319static inline void __generic_make_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
Jens Axboe165125e2007-07-24 09:28:11 +02001321 struct request_queue *q;
NeilBrown5ddfe962006-10-30 22:07:21 -08001322 sector_t old_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 int ret, nr_sectors = bio_sectors(bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001324 dev_t old_dev;
Jens Axboe51fd77b2007-11-02 08:49:08 +01001325 int err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Jens Axboec07e2b42007-07-18 13:27:58 +02001329 if (bio_check_eod(bio, nr_sectors))
1330 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332 /*
1333 * Resolve the mapping until finished. (drivers are
1334 * still free to implement/resolve their own stacking
1335 * by explicitly returning 0)
1336 *
1337 * NOTE: we don't repeat the blk_size check for each new device.
1338 * Stacking drivers are expected to know what they are doing.
1339 */
NeilBrown5ddfe962006-10-30 22:07:21 -08001340 old_sector = -1;
Jens Axboe2056a782006-03-23 20:00:26 +01001341 old_dev = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 do {
1343 char b[BDEVNAME_SIZE];
1344
1345 q = bdev_get_queue(bio->bi_bdev);
1346 if (!q) {
1347 printk(KERN_ERR
1348 "generic_make_request: Trying to access "
1349 "nonexistent block-device %s (%Lu)\n",
1350 bdevname(bio->bi_bdev, b),
1351 (long long) bio->bi_sector);
1352end_io:
Jens Axboe51fd77b2007-11-02 08:49:08 +01001353 bio_endio(bio, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 break;
1355 }
1356
Jens Axboe4fa253f2007-07-18 13:13:10 +02001357 if (unlikely(nr_sectors > q->max_hw_sectors)) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001358 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 bdevname(bio->bi_bdev, b),
1360 bio_sectors(bio),
1361 q->max_hw_sectors);
1362 goto end_io;
1363 }
1364
Nick Pigginfde6ad22005-06-23 00:08:53 -07001365 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 goto end_io;
1367
Akinobu Mitac17bb492006-12-08 02:39:46 -08001368 if (should_fail_request(bio))
1369 goto end_io;
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 /*
1372 * If this device has partitions, remap block n
1373 * of partition p to block n+start(p) of the disk.
1374 */
1375 blk_partition_remap(bio);
1376
NeilBrown5ddfe962006-10-30 22:07:21 -08001377 if (old_sector != -1)
Jens Axboe4fa253f2007-07-18 13:13:10 +02001378 blk_add_trace_remap(q, bio, old_dev, bio->bi_sector,
NeilBrown5ddfe962006-10-30 22:07:21 -08001379 old_sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001380
1381 blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
1382
NeilBrown5ddfe962006-10-30 22:07:21 -08001383 old_sector = bio->bi_sector;
Jens Axboe2056a782006-03-23 20:00:26 +01001384 old_dev = bio->bi_bdev->bd_dev;
1385
Jens Axboec07e2b42007-07-18 13:27:58 +02001386 if (bio_check_eod(bio, nr_sectors))
1387 goto end_io;
Jens Axboe51fd77b2007-11-02 08:49:08 +01001388 if (bio_empty_barrier(bio) && !q->prepare_flush_fn) {
1389 err = -EOPNOTSUPP;
1390 goto end_io;
1391 }
NeilBrown5ddfe962006-10-30 22:07:21 -08001392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 ret = q->make_request_fn(q, bio);
1394 } while (ret);
1395}
1396
Neil Brownd89d8792007-05-01 09:53:42 +02001397/*
1398 * We only want one ->make_request_fn to be active at a time,
1399 * else stack usage with stacked devices could be a problem.
1400 * So use current->bio_{list,tail} to keep a list of requests
1401 * submited by a make_request_fn function.
1402 * current->bio_tail is also used as a flag to say if
1403 * generic_make_request is currently active in this task or not.
1404 * If it is NULL, then no make_request is active. If it is non-NULL,
1405 * then a make_request is active, and new requests should be added
1406 * at the tail
1407 */
1408void generic_make_request(struct bio *bio)
1409{
1410 if (current->bio_tail) {
1411 /* make_request is active */
1412 *(current->bio_tail) = bio;
1413 bio->bi_next = NULL;
1414 current->bio_tail = &bio->bi_next;
1415 return;
1416 }
1417 /* following loop may be a bit non-obvious, and so deserves some
1418 * explanation.
1419 * Before entering the loop, bio->bi_next is NULL (as all callers
1420 * ensure that) so we have a list with a single bio.
1421 * We pretend that we have just taken it off a longer list, so
1422 * we assign bio_list to the next (which is NULL) and bio_tail
1423 * to &bio_list, thus initialising the bio_list of new bios to be
1424 * added. __generic_make_request may indeed add some more bios
1425 * through a recursive call to generic_make_request. If it
1426 * did, we find a non-NULL value in bio_list and re-enter the loop
1427 * from the top. In this case we really did just take the bio
1428 * of the top of the list (no pretending) and so fixup bio_list and
1429 * bio_tail or bi_next, and call into __generic_make_request again.
1430 *
1431 * The loop was structured like this to make only one call to
1432 * __generic_make_request (which is important as it is large and
1433 * inlined) and to keep the structure simple.
1434 */
1435 BUG_ON(bio->bi_next);
1436 do {
1437 current->bio_list = bio->bi_next;
1438 if (bio->bi_next == NULL)
1439 current->bio_tail = &current->bio_list;
1440 else
1441 bio->bi_next = NULL;
1442 __generic_make_request(bio);
1443 bio = current->bio_list;
1444 } while (bio);
1445 current->bio_tail = NULL; /* deactivate */
1446}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447EXPORT_SYMBOL(generic_make_request);
1448
1449/**
1450 * submit_bio: submit a bio to the block device layer for I/O
1451 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1452 * @bio: The &struct bio which describes the I/O
1453 *
1454 * submit_bio() is very similar in purpose to generic_make_request(), and
1455 * uses that function to do most of the work. Both are fairly rough
1456 * interfaces, @bio must be presetup and ready for I/O.
1457 *
1458 */
1459void submit_bio(int rw, struct bio *bio)
1460{
1461 int count = bio_sectors(bio);
1462
Jens Axboe22e2c502005-06-27 10:55:12 +02001463 bio->bi_rw |= rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Jens Axboebf2de6f2007-09-27 13:01:25 +02001465 /*
1466 * If it's a regular read/write or a barrier with data attached,
1467 * go through the normal accounting stuff before submission.
1468 */
1469 if (!bio_empty_barrier(bio)) {
1470
1471 BIO_BUG_ON(!bio->bi_size);
1472 BIO_BUG_ON(!bio->bi_io_vec);
1473
1474 if (rw & WRITE) {
1475 count_vm_events(PGPGOUT, count);
1476 } else {
1477 task_io_account_read(bio->bi_size);
1478 count_vm_events(PGPGIN, count);
1479 }
1480
1481 if (unlikely(block_dump)) {
1482 char b[BDEVNAME_SIZE];
1483 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001484 current->comm, task_pid_nr(current),
Jens Axboebf2de6f2007-09-27 13:01:25 +02001485 (rw & WRITE) ? "WRITE" : "READ",
1486 (unsigned long long)bio->bi_sector,
Jens Axboe6728cb02008-01-31 13:03:55 +01001487 bdevname(bio->bi_bdev, b));
Jens Axboebf2de6f2007-09-27 13:01:25 +02001488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 }
1490
1491 generic_make_request(bio);
1492}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493EXPORT_SYMBOL(submit_bio);
1494
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001495/**
1496 * __end_that_request_first - end I/O on a request
1497 * @req: the request being processed
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001498 * @error: 0 for success, < 0 for error
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001499 * @nr_bytes: number of bytes to complete
1500 *
1501 * Description:
1502 * Ends I/O on a number of bytes attached to @req, and sets it up
1503 * for the next range of segments (if any) in the cluster.
1504 *
1505 * Return:
1506 * 0 - we are done with this request, call end_that_request_last()
1507 * 1 - still buffers pending for this request
1508 **/
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001509static int __end_that_request_first(struct request *req, int error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 int nr_bytes)
1511{
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001512 int total_bytes, bio_nbytes, next_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 struct bio *bio;
1514
Jens Axboe2056a782006-03-23 20:00:26 +01001515 blk_add_trace_rq(req->q, req, BLK_TA_COMPLETE);
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 * for a REQ_BLOCK_PC request, we want to carry any eventual
1519 * sense key with us all the way through
1520 */
1521 if (!blk_pc_request(req))
1522 req->errors = 0;
1523
Jens Axboe6728cb02008-01-31 13:03:55 +01001524 if (error && (blk_fs_request(req) && !(req->cmd_flags & REQ_QUIET))) {
1525 printk(KERN_ERR "end_request: I/O error, dev %s, sector %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 req->rq_disk ? req->rq_disk->disk_name : "?",
1527 (unsigned long long)req->sector);
1528 }
1529
Jens Axboed72d9042005-11-01 08:35:42 +01001530 if (blk_fs_request(req) && req->rq_disk) {
Jens Axboea3623572005-11-01 09:26:16 +01001531 const int rw = rq_data_dir(req);
1532
Jens Axboe53e86062006-01-17 11:09:27 +01001533 disk_stat_add(req->rq_disk, sectors[rw], nr_bytes >> 9);
Jens Axboed72d9042005-11-01 08:35:42 +01001534 }
1535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 total_bytes = bio_nbytes = 0;
1537 while ((bio = req->bio) != NULL) {
1538 int nbytes;
1539
Jens Axboebf2de6f2007-09-27 13:01:25 +02001540 /*
1541 * For an empty barrier request, the low level driver must
1542 * store a potential error location in ->sector. We pass
1543 * that back up in ->bi_sector.
1544 */
1545 if (blk_empty_barrier(req))
1546 bio->bi_sector = req->sector;
1547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 if (nr_bytes >= bio->bi_size) {
1549 req->bio = bio->bi_next;
1550 nbytes = bio->bi_size;
NeilBrown5bb23a62007-09-27 12:46:13 +02001551 req_bio_endio(req, bio, nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 next_idx = 0;
1553 bio_nbytes = 0;
1554 } else {
1555 int idx = bio->bi_idx + next_idx;
1556
1557 if (unlikely(bio->bi_idx >= bio->bi_vcnt)) {
1558 blk_dump_rq_flags(req, "__end_that");
Jens Axboe6728cb02008-01-31 13:03:55 +01001559 printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
1560 __FUNCTION__, bio->bi_idx,
1561 bio->bi_vcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 break;
1563 }
1564
1565 nbytes = bio_iovec_idx(bio, idx)->bv_len;
1566 BIO_BUG_ON(nbytes > bio->bi_size);
1567
1568 /*
1569 * not a complete bvec done
1570 */
1571 if (unlikely(nbytes > nr_bytes)) {
1572 bio_nbytes += nr_bytes;
1573 total_bytes += nr_bytes;
1574 break;
1575 }
1576
1577 /*
1578 * advance to the next vector
1579 */
1580 next_idx++;
1581 bio_nbytes += nbytes;
1582 }
1583
1584 total_bytes += nbytes;
1585 nr_bytes -= nbytes;
1586
Jens Axboe6728cb02008-01-31 13:03:55 +01001587 bio = req->bio;
1588 if (bio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 /*
1590 * end more in this run, or just return 'not-done'
1591 */
1592 if (unlikely(nr_bytes <= 0))
1593 break;
1594 }
1595 }
1596
1597 /*
1598 * completely done
1599 */
1600 if (!req->bio)
1601 return 0;
1602
1603 /*
1604 * if the request wasn't completed, update state
1605 */
1606 if (bio_nbytes) {
NeilBrown5bb23a62007-09-27 12:46:13 +02001607 req_bio_endio(req, bio, bio_nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 bio->bi_idx += next_idx;
1609 bio_iovec(bio)->bv_offset += nr_bytes;
1610 bio_iovec(bio)->bv_len -= nr_bytes;
1611 }
1612
1613 blk_recalc_rq_sectors(req, total_bytes >> 9);
1614 blk_recalc_rq_segments(req);
1615 return 1;
1616}
1617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618/*
Jens Axboeff856ba2006-01-09 16:02:34 +01001619 * splice the completion data to a local structure and hand off to
1620 * process_completion_queue() to complete the requests
1621 */
1622static void blk_done_softirq(struct softirq_action *h)
1623{
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07001624 struct list_head *cpu_list, local_list;
Jens Axboeff856ba2006-01-09 16:02:34 +01001625
1626 local_irq_disable();
1627 cpu_list = &__get_cpu_var(blk_cpu_done);
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07001628 list_replace_init(cpu_list, &local_list);
Jens Axboeff856ba2006-01-09 16:02:34 +01001629 local_irq_enable();
1630
1631 while (!list_empty(&local_list)) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001632 struct request *rq;
Jens Axboeff856ba2006-01-09 16:02:34 +01001633
Jens Axboe6728cb02008-01-31 13:03:55 +01001634 rq = list_entry(local_list.next, struct request, donelist);
Jens Axboeff856ba2006-01-09 16:02:34 +01001635 list_del_init(&rq->donelist);
1636 rq->q->softirq_done_fn(rq);
1637 }
1638}
1639
Jens Axboe6728cb02008-01-31 13:03:55 +01001640static int __cpuinit blk_cpu_notify(struct notifier_block *self,
1641 unsigned long action, void *hcpu)
Jens Axboeff856ba2006-01-09 16:02:34 +01001642{
1643 /*
1644 * If a CPU goes away, splice its entries to the current CPU
1645 * and trigger a run of the softirq
1646 */
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001647 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
Jens Axboeff856ba2006-01-09 16:02:34 +01001648 int cpu = (unsigned long) hcpu;
1649
1650 local_irq_disable();
1651 list_splice_init(&per_cpu(blk_cpu_done, cpu),
1652 &__get_cpu_var(blk_cpu_done));
1653 raise_softirq_irqoff(BLOCK_SOFTIRQ);
1654 local_irq_enable();
1655 }
1656
1657 return NOTIFY_OK;
1658}
1659
1660
Satyam Sharmadb47d472007-08-23 09:29:40 +02001661static struct notifier_block blk_cpu_notifier __cpuinitdata = {
Jens Axboeff856ba2006-01-09 16:02:34 +01001662 .notifier_call = blk_cpu_notify,
1663};
1664
Jens Axboeff856ba2006-01-09 16:02:34 +01001665/**
1666 * blk_complete_request - end I/O on a request
1667 * @req: the request being processed
1668 *
1669 * Description:
1670 * Ends all I/O on a request. It does not handle partial completions,
Andreas Mohrd6e05ed2006-06-26 18:35:02 +02001671 * unless the driver actually implements this in its completion callback
Jens Axboe4fa253f2007-07-18 13:13:10 +02001672 * through requeueing. The actual completion happens out-of-order,
Jens Axboeff856ba2006-01-09 16:02:34 +01001673 * through a softirq handler. The user must have registered a completion
1674 * callback through blk_queue_softirq_done().
1675 **/
1676
1677void blk_complete_request(struct request *req)
1678{
1679 struct list_head *cpu_list;
1680 unsigned long flags;
1681
1682 BUG_ON(!req->q->softirq_done_fn);
Jens Axboe6728cb02008-01-31 13:03:55 +01001683
Jens Axboeff856ba2006-01-09 16:02:34 +01001684 local_irq_save(flags);
1685
1686 cpu_list = &__get_cpu_var(blk_cpu_done);
1687 list_add_tail(&req->donelist, cpu_list);
1688 raise_softirq_irqoff(BLOCK_SOFTIRQ);
1689
1690 local_irq_restore(flags);
1691}
Jens Axboeff856ba2006-01-09 16:02:34 +01001692EXPORT_SYMBOL(blk_complete_request);
Jens Axboe6728cb02008-01-31 13:03:55 +01001693
Jens Axboeff856ba2006-01-09 16:02:34 +01001694/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 * queue lock must be held
1696 */
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001697static void end_that_request_last(struct request *req, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
1699 struct gendisk *disk = req->rq_disk;
Tejun Heo8ffdc652006-01-06 09:49:03 +01001700
Kiyoshi Uedab8286232007-12-11 17:53:24 -05001701 if (blk_rq_tagged(req))
1702 blk_queue_end_tag(req->q, req);
1703
1704 if (blk_queued_rq(req))
1705 blkdev_dequeue_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
1707 if (unlikely(laptop_mode) && blk_fs_request(req))
1708 laptop_io_completion();
1709
Jens Axboefd0ff8a2006-05-23 11:23:49 +02001710 /*
1711 * Account IO completion. bar_rq isn't accounted as a normal
1712 * IO on queueing nor completion. Accounting the containing
1713 * request is enough.
1714 */
1715 if (disk && blk_fs_request(req) && req != &req->q->bar_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 unsigned long duration = jiffies - req->start_time;
Jens Axboea3623572005-11-01 09:26:16 +01001717 const int rw = rq_data_dir(req);
1718
1719 __disk_stat_inc(disk, ios[rw]);
1720 __disk_stat_add(disk, ticks[rw], duration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 disk_round_stats(disk);
1722 disk->in_flight--;
1723 }
Kiyoshi Uedab8286232007-12-11 17:53:24 -05001724
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 if (req->end_io)
Tejun Heo8ffdc652006-01-06 09:49:03 +01001726 req->end_io(req, error);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05001727 else {
1728 if (blk_bidi_rq(req))
1729 __blk_put_request(req->next_rq->q, req->next_rq);
1730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 __blk_put_request(req->q, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 }
1733}
1734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735static inline void __end_request(struct request *rq, int uptodate,
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05001736 unsigned int nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05001738 int error = 0;
1739
1740 if (uptodate <= 0)
1741 error = uptodate ? uptodate : -EIO;
1742
1743 __blk_end_request(rq, error, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744}
1745
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05001746/**
1747 * blk_rq_bytes - Returns bytes left to complete in the entire request
1748 **/
1749unsigned int blk_rq_bytes(struct request *rq)
Jens Axboea0cd1282007-09-21 10:41:07 +02001750{
1751 if (blk_fs_request(rq))
1752 return rq->hard_nr_sectors << 9;
1753
1754 return rq->data_len;
1755}
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05001756EXPORT_SYMBOL_GPL(blk_rq_bytes);
1757
1758/**
1759 * blk_rq_cur_bytes - Returns bytes left to complete in the current segment
1760 **/
1761unsigned int blk_rq_cur_bytes(struct request *rq)
1762{
1763 if (blk_fs_request(rq))
1764 return rq->current_nr_sectors << 9;
1765
1766 if (rq->bio)
1767 return rq->bio->bi_size;
1768
1769 return rq->data_len;
1770}
1771EXPORT_SYMBOL_GPL(blk_rq_cur_bytes);
Jens Axboea0cd1282007-09-21 10:41:07 +02001772
1773/**
1774 * end_queued_request - end all I/O on a queued request
1775 * @rq: the request being processed
1776 * @uptodate: error value or 0/1 uptodate flag
1777 *
1778 * Description:
1779 * Ends all I/O on a request, and removes it from the block layer queues.
1780 * Not suitable for normal IO completion, unless the driver still has
1781 * the request attached to the block layer.
1782 *
1783 **/
1784void end_queued_request(struct request *rq, int uptodate)
1785{
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05001786 __end_request(rq, uptodate, blk_rq_bytes(rq));
Jens Axboea0cd1282007-09-21 10:41:07 +02001787}
1788EXPORT_SYMBOL(end_queued_request);
1789
1790/**
1791 * end_dequeued_request - end all I/O on a dequeued request
1792 * @rq: the request being processed
1793 * @uptodate: error value or 0/1 uptodate flag
1794 *
1795 * Description:
1796 * Ends all I/O on a request. The request must already have been
1797 * dequeued using blkdev_dequeue_request(), as is normally the case
1798 * for most drivers.
1799 *
1800 **/
1801void end_dequeued_request(struct request *rq, int uptodate)
1802{
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05001803 __end_request(rq, uptodate, blk_rq_bytes(rq));
Jens Axboea0cd1282007-09-21 10:41:07 +02001804}
1805EXPORT_SYMBOL(end_dequeued_request);
1806
1807
1808/**
1809 * end_request - end I/O on the current segment of the request
Randy Dunlap8f731f72007-10-18 23:39:28 -07001810 * @req: the request being processed
Jens Axboea0cd1282007-09-21 10:41:07 +02001811 * @uptodate: error value or 0/1 uptodate flag
1812 *
1813 * Description:
1814 * Ends I/O on the current segment of a request. If that is the only
1815 * remaining segment, the request is also completed and freed.
1816 *
1817 * This is a remnant of how older block drivers handled IO completions.
1818 * Modern drivers typically end IO on the full request in one go, unless
1819 * they have a residual value to account for. For that case this function
1820 * isn't really useful, unless the residual just happens to be the
1821 * full current segment. In other words, don't use this function in new
1822 * code. Either use end_request_completely(), or the
1823 * end_that_request_chunk() (along with end_that_request_last()) for
1824 * partial completions.
1825 *
1826 **/
1827void end_request(struct request *req, int uptodate)
1828{
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05001829 __end_request(req, uptodate, req->hard_cur_sectors << 9);
Jens Axboea0cd1282007-09-21 10:41:07 +02001830}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831EXPORT_SYMBOL(end_request);
1832
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001833/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001834 * blk_end_io - Generic end_io function to complete a request.
1835 * @rq: the request being processed
1836 * @error: 0 for success, < 0 for error
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001837 * @nr_bytes: number of bytes to complete @rq
1838 * @bidi_bytes: number of bytes to complete @rq->next_rq
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001839 * @drv_callback: function called between completion of bios in the request
1840 * and completion of the request.
1841 * If the callback returns non 0, this helper returns without
1842 * completion of the request.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001843 *
1844 * Description:
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001845 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001846 * If @rq has leftover, sets it up for the next range of segments.
1847 *
1848 * Return:
1849 * 0 - we are done with this request
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001850 * 1 - this request is not freed yet, it still has pending buffers.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001851 **/
Jens Axboe22b13212008-01-31 12:36:19 +01001852static int blk_end_io(struct request *rq, int error, unsigned int nr_bytes,
1853 unsigned int bidi_bytes,
1854 int (drv_callback)(struct request *))
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001855{
1856 struct request_queue *q = rq->q;
1857 unsigned long flags = 0UL;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001858
1859 if (blk_fs_request(rq) || blk_pc_request(rq)) {
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001860 if (__end_that_request_first(rq, error, nr_bytes))
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001861 return 1;
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001862
1863 /* Bidi request must be completed as a whole */
1864 if (blk_bidi_rq(rq) &&
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001865 __end_that_request_first(rq->next_rq, error, bidi_bytes))
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001866 return 1;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001867 }
1868
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001869 /* Special feature for tricky drivers */
1870 if (drv_callback && drv_callback(rq))
1871 return 1;
1872
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001873 add_disk_randomness(rq->rq_disk);
1874
1875 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05001876 end_that_request_last(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001877 spin_unlock_irqrestore(q->queue_lock, flags);
1878
1879 return 0;
1880}
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001881
1882/**
1883 * blk_end_request - Helper function for drivers to complete the request.
1884 * @rq: the request being processed
1885 * @error: 0 for success, < 0 for error
1886 * @nr_bytes: number of bytes to complete
1887 *
1888 * Description:
1889 * Ends I/O on a number of bytes attached to @rq.
1890 * If @rq has leftover, sets it up for the next range of segments.
1891 *
1892 * Return:
1893 * 0 - we are done with this request
1894 * 1 - still buffers pending for this request
1895 **/
Jens Axboe22b13212008-01-31 12:36:19 +01001896int blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001897{
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001898 return blk_end_io(rq, error, nr_bytes, 0, NULL);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001899}
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001900EXPORT_SYMBOL_GPL(blk_end_request);
1901
1902/**
1903 * __blk_end_request - Helper function for drivers to complete the request.
1904 * @rq: the request being processed
1905 * @error: 0 for success, < 0 for error
1906 * @nr_bytes: number of bytes to complete
1907 *
1908 * Description:
1909 * Must be called with queue lock held unlike blk_end_request().
1910 *
1911 * Return:
1912 * 0 - we are done with this request
1913 * 1 - still buffers pending for this request
1914 **/
Jens Axboe22b13212008-01-31 12:36:19 +01001915int __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001916{
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001917 if (blk_fs_request(rq) || blk_pc_request(rq)) {
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001918 if (__end_that_request_first(rq, error, nr_bytes))
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001919 return 1;
1920 }
1921
1922 add_disk_randomness(rq->rq_disk);
1923
Kiyoshi Uedab8286232007-12-11 17:53:24 -05001924 end_that_request_last(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001925
1926 return 0;
1927}
1928EXPORT_SYMBOL_GPL(__blk_end_request);
1929
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001930/**
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001931 * blk_end_bidi_request - Helper function for drivers to complete bidi request.
1932 * @rq: the bidi request being processed
1933 * @error: 0 for success, < 0 for error
1934 * @nr_bytes: number of bytes to complete @rq
1935 * @bidi_bytes: number of bytes to complete @rq->next_rq
1936 *
1937 * Description:
1938 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
1939 *
1940 * Return:
1941 * 0 - we are done with this request
1942 * 1 - still buffers pending for this request
1943 **/
Jens Axboe22b13212008-01-31 12:36:19 +01001944int blk_end_bidi_request(struct request *rq, int error, unsigned int nr_bytes,
1945 unsigned int bidi_bytes)
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001946{
1947 return blk_end_io(rq, error, nr_bytes, bidi_bytes, NULL);
1948}
1949EXPORT_SYMBOL_GPL(blk_end_bidi_request);
1950
1951/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001952 * blk_end_request_callback - Special helper function for tricky drivers
1953 * @rq: the request being processed
1954 * @error: 0 for success, < 0 for error
1955 * @nr_bytes: number of bytes to complete
1956 * @drv_callback: function called between completion of bios in the request
1957 * and completion of the request.
1958 * If the callback returns non 0, this helper returns without
1959 * completion of the request.
1960 *
1961 * Description:
1962 * Ends I/O on a number of bytes attached to @rq.
1963 * If @rq has leftover, sets it up for the next range of segments.
1964 *
1965 * This special helper function is used only for existing tricky drivers.
1966 * (e.g. cdrom_newpc_intr() of ide-cd)
1967 * This interface will be removed when such drivers are rewritten.
1968 * Don't use this interface in other places anymore.
1969 *
1970 * Return:
1971 * 0 - we are done with this request
1972 * 1 - this request is not freed yet.
1973 * this request still has pending buffers or
1974 * the driver doesn't want to finish this request yet.
1975 **/
Jens Axboe22b13212008-01-31 12:36:19 +01001976int blk_end_request_callback(struct request *rq, int error,
1977 unsigned int nr_bytes,
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001978 int (drv_callback)(struct request *))
1979{
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001980 return blk_end_io(rq, error, nr_bytes, 0, drv_callback);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001981}
1982EXPORT_SYMBOL_GPL(blk_end_request_callback);
1983
Jens Axboe86db1e22008-01-29 14:53:40 +01001984void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
1985 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986{
Jens Axboe4aff5e22006-08-10 08:44:47 +02001987 /* first two bits are identical in rq->cmd_flags and bio->bi_rw */
1988 rq->cmd_flags |= (bio->bi_rw & 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
1990 rq->nr_phys_segments = bio_phys_segments(q, bio);
1991 rq->nr_hw_segments = bio_hw_segments(q, bio);
1992 rq->current_nr_sectors = bio_cur_sectors(bio);
1993 rq->hard_cur_sectors = rq->current_nr_sectors;
1994 rq->hard_nr_sectors = rq->nr_sectors = bio_sectors(bio);
1995 rq->buffer = bio_data(bio);
Mike Christie0e75f902006-12-01 10:40:55 +01001996 rq->data_len = bio->bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
1998 rq->bio = rq->biotail = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
NeilBrown66846572007-08-16 13:31:28 +02002000 if (bio->bi_bdev)
2001 rq->rq_disk = bio->bi_bdev->bd_disk;
2002}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
2004int kblockd_schedule_work(struct work_struct *work)
2005{
2006 return queue_work(kblockd_workqueue, work);
2007}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008EXPORT_SYMBOL(kblockd_schedule_work);
2009
Andrew Morton19a75d82007-05-09 02:33:56 -07002010void kblockd_flush_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011{
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002012 cancel_work_sync(work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013}
Andrew Morton19a75d82007-05-09 02:33:56 -07002014EXPORT_SYMBOL(kblockd_flush_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016int __init blk_dev_init(void)
2017{
Jens Axboeff856ba2006-01-09 16:02:34 +01002018 int i;
2019
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 kblockd_workqueue = create_workqueue("kblockd");
2021 if (!kblockd_workqueue)
2022 panic("Failed to create kblockd\n");
2023
2024 request_cachep = kmem_cache_create("blkdev_requests",
Paul Mundt20c2df82007-07-20 10:11:58 +09002025 sizeof(struct request), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Jens Axboe8324aa92008-01-29 14:51:59 +01002027 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02002028 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002030 for_each_possible_cpu(i)
Jens Axboeff856ba2006-01-09 16:02:34 +01002031 INIT_LIST_HEAD(&per_cpu(blk_cpu_done, i));
2032
2033 open_softirq(BLOCK_SOFTIRQ, blk_done_softirq, NULL);
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07002034 register_hotcpu_notifier(&blk_cpu_notifier);
Jens Axboeff856ba2006-01-09 16:02:34 +01002035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 return 0;
2037}
2038