blob: 4afb39c823396ccec50c3ded2673d22ed0b07cf3 [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 Axboe86db1e22008-01-29 14:53:40 +0100105void rq_init(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
107 INIT_LIST_HEAD(&rq->queuelist);
Jens Axboeff856ba2006-01-09 16:02:34 +0100108 INIT_LIST_HEAD(&rq->donelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 rq->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 rq->bio = rq->biotail = NULL;
Jens Axboe2e662b62006-07-13 11:55:04 +0200112 INIT_HLIST_NODE(&rq->hash);
113 RB_CLEAR_NODE(&rq->rb_node);
Jens Axboe22e2c502005-06-27 10:55:12 +0200114 rq->ioprio = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 rq->buffer = NULL;
116 rq->ref_count = 1;
117 rq->q = q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 rq->special = NULL;
119 rq->data_len = 0;
120 rq->data = NULL;
Mike Christie df46b9a2005-06-20 14:04:44 +0200121 rq->nr_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 rq->sense = NULL;
123 rq->end_io = NULL;
124 rq->end_io_data = NULL;
Jens Axboeff856ba2006-01-09 16:02:34 +0100125 rq->completion_data = NULL;
FUJITA Tomonoriabae1fd2007-07-16 08:52:14 +0200126 rq->next_rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
NeilBrown5bb23a62007-09-27 12:46:13 +0200129static void req_bio_endio(struct request *rq, struct bio *bio,
130 unsigned int nbytes, int error)
Tejun Heo797e7db2006-01-06 09:51:03 +0100131{
Jens Axboe165125e2007-07-24 09:28:11 +0200132 struct request_queue *q = rq->q;
Tejun Heo797e7db2006-01-06 09:51:03 +0100133
NeilBrown5bb23a62007-09-27 12:46:13 +0200134 if (&q->bar_rq != rq) {
135 if (error)
136 clear_bit(BIO_UPTODATE, &bio->bi_flags);
137 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
138 error = -EIO;
Tejun Heo797e7db2006-01-06 09:51:03 +0100139
NeilBrown5bb23a62007-09-27 12:46:13 +0200140 if (unlikely(nbytes > bio->bi_size)) {
Jens Axboe6728cb02008-01-31 13:03:55 +0100141 printk(KERN_ERR "%s: want %u bytes done, %u left\n",
NeilBrown5bb23a62007-09-27 12:46:13 +0200142 __FUNCTION__, nbytes, bio->bi_size);
143 nbytes = bio->bi_size;
144 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100145
NeilBrown5bb23a62007-09-27 12:46:13 +0200146 bio->bi_size -= nbytes;
147 bio->bi_sector += (nbytes >> 9);
148 if (bio->bi_size == 0)
NeilBrown6712ecf2007-09-27 12:47:43 +0200149 bio_endio(bio, error);
NeilBrown5bb23a62007-09-27 12:46:13 +0200150 } else {
151
152 /*
153 * Okay, this is the barrier request in progress, just
154 * record the error;
155 */
156 if (error && !q->orderr)
157 q->orderr = error;
158 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100159}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161void blk_dump_rq_flags(struct request *rq, char *msg)
162{
163 int bit;
164
Jens Axboe6728cb02008-01-31 13:03:55 +0100165 printk(KERN_INFO "%s: dev %s: type=%x, flags=%x\n", msg,
Jens Axboe4aff5e22006-08-10 08:44:47 +0200166 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
167 rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Jens Axboe6728cb02008-01-31 13:03:55 +0100169 printk(KERN_INFO " sector %llu, nr/cnr %lu/%u\n",
170 (unsigned long long)rq->sector,
171 rq->nr_sectors,
172 rq->current_nr_sectors);
173 printk(KERN_INFO " bio %p, biotail %p, buffer %p, data %p, len %u\n",
174 rq->bio, rq->biotail,
175 rq->buffer, rq->data,
176 rq->data_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Jens Axboe4aff5e22006-08-10 08:44:47 +0200178 if (blk_pc_request(rq)) {
Jens Axboe6728cb02008-01-31 13:03:55 +0100179 printk(KERN_INFO " cdb: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 for (bit = 0; bit < sizeof(rq->cmd); bit++)
181 printk("%02x ", rq->cmd[bit]);
182 printk("\n");
183 }
184}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185EXPORT_SYMBOL(blk_dump_rq_flags);
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187/*
188 * "plug" the device if there are no outstanding requests: this will
189 * force the transfer to start only after we have put all the requests
190 * on the list.
191 *
192 * This is called with interrupts off and no requests on the queue and
193 * with the queue lock held.
194 */
Jens Axboe165125e2007-07-24 09:28:11 +0200195void blk_plug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 WARN_ON(!irqs_disabled());
198
199 /*
200 * don't plug a stopped queue, it must be paired with blk_start_queue()
201 * which will restart the queueing
202 */
Coywolf Qi Hunt7daac492006-04-19 10:14:49 +0200203 if (blk_queue_stopped(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return;
205
Jens Axboe2056a782006-03-23 20:00:26 +0100206 if (!test_and_set_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
Jens Axboe2056a782006-03-23 20:00:26 +0100208 blk_add_trace_generic(q, NULL, 0, BLK_TA_PLUG);
209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211EXPORT_SYMBOL(blk_plug_device);
212
213/*
214 * remove the queue from the plugged list, if present. called with
215 * queue lock held and interrupts disabled.
216 */
Jens Axboe165125e2007-07-24 09:28:11 +0200217int blk_remove_plug(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 WARN_ON(!irqs_disabled());
220
221 if (!test_and_clear_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags))
222 return 0;
223
224 del_timer(&q->unplug_timer);
225 return 1;
226}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227EXPORT_SYMBOL(blk_remove_plug);
228
229/*
230 * remove the plug and let it rip..
231 */
Jens Axboe165125e2007-07-24 09:28:11 +0200232void __generic_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Coywolf Qi Hunt7daac492006-04-19 10:14:49 +0200234 if (unlikely(blk_queue_stopped(q)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return;
236
237 if (!blk_remove_plug(q))
238 return;
239
Jens Axboe22e2c502005-06-27 10:55:12 +0200240 q->request_fn(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242EXPORT_SYMBOL(__generic_unplug_device);
243
244/**
245 * generic_unplug_device - fire a request queue
Jens Axboe165125e2007-07-24 09:28:11 +0200246 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 *
248 * Description:
249 * Linux uses plugging to build bigger requests queues before letting
250 * the device have at them. If a queue is plugged, the I/O scheduler
251 * is still adding and merging requests on the queue. Once the queue
252 * gets unplugged, the request_fn defined for the queue is invoked and
253 * transfers started.
254 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200255void generic_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
257 spin_lock_irq(q->queue_lock);
258 __generic_unplug_device(q);
259 spin_unlock_irq(q->queue_lock);
260}
261EXPORT_SYMBOL(generic_unplug_device);
262
263static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
264 struct page *page)
265{
Jens Axboe165125e2007-07-24 09:28:11 +0200266 struct request_queue *q = bdi->unplug_io_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500268 blk_unplug(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
Jens Axboe86db1e22008-01-29 14:53:40 +0100271void blk_unplug_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Jens Axboe165125e2007-07-24 09:28:11 +0200273 struct request_queue *q =
274 container_of(work, struct request_queue, unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Jens Axboe2056a782006-03-23 20:00:26 +0100276 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
277 q->rq.count[READ] + q->rq.count[WRITE]);
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 q->unplug_fn(q);
280}
281
Jens Axboe86db1e22008-01-29 14:53:40 +0100282void blk_unplug_timeout(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Jens Axboe165125e2007-07-24 09:28:11 +0200284 struct request_queue *q = (struct request_queue *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Jens Axboe2056a782006-03-23 20:00:26 +0100286 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_TIMER, NULL,
287 q->rq.count[READ] + q->rq.count[WRITE]);
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 kblockd_schedule_work(&q->unplug_work);
290}
291
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500292void blk_unplug(struct request_queue *q)
293{
294 /*
295 * devices don't necessarily have an ->unplug_fn defined
296 */
297 if (q->unplug_fn) {
298 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
299 q->rq.count[READ] + q->rq.count[WRITE]);
300
301 q->unplug_fn(q);
302 }
303}
304EXPORT_SYMBOL(blk_unplug);
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306/**
307 * blk_start_queue - restart a previously stopped queue
Jens Axboe165125e2007-07-24 09:28:11 +0200308 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 *
310 * Description:
311 * blk_start_queue() will clear the stop flag on the queue, and call
312 * the request_fn for the queue if it was in a stopped state when
313 * entered. Also see blk_stop_queue(). Queue lock must be held.
314 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200315void blk_start_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200317 WARN_ON(!irqs_disabled());
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 clear_bit(QUEUE_FLAG_STOPPED, &q->queue_flags);
320
321 /*
322 * one level of recursion is ok and is much faster than kicking
323 * the unplug handling
324 */
325 if (!test_and_set_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) {
326 q->request_fn(q);
327 clear_bit(QUEUE_FLAG_REENTER, &q->queue_flags);
328 } else {
329 blk_plug_device(q);
330 kblockd_schedule_work(&q->unplug_work);
331 }
332}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333EXPORT_SYMBOL(blk_start_queue);
334
335/**
336 * blk_stop_queue - stop a queue
Jens Axboe165125e2007-07-24 09:28:11 +0200337 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 *
339 * Description:
340 * The Linux block layer assumes that a block driver will consume all
341 * entries on the request queue when the request_fn strategy is called.
342 * Often this will not happen, because of hardware limitations (queue
343 * depth settings). If a device driver gets a 'queue full' response,
344 * or if it simply chooses not to queue more I/O at one point, it can
345 * call this function to prevent the request_fn from being called until
346 * the driver has signalled it's ready to go again. This happens by calling
347 * blk_start_queue() to restart queue operations. Queue lock must be held.
348 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200349void blk_stop_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
351 blk_remove_plug(q);
352 set_bit(QUEUE_FLAG_STOPPED, &q->queue_flags);
353}
354EXPORT_SYMBOL(blk_stop_queue);
355
356/**
357 * blk_sync_queue - cancel any pending callbacks on a queue
358 * @q: the queue
359 *
360 * Description:
361 * The block layer may perform asynchronous callback activity
362 * on a queue, such as calling the unplug function after a timeout.
363 * A block device may call blk_sync_queue to ensure that any
364 * such activity is cancelled, thus allowing it to release resources
Michael Opdenacker59c51592007-05-09 08:57:56 +0200365 * that the callbacks might use. The caller must already have made sure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 * that its ->make_request_fn will not re-add plugging prior to calling
367 * this function.
368 *
369 */
370void blk_sync_queue(struct request_queue *q)
371{
372 del_timer_sync(&q->unplug_timer);
Oleg Nesterovabbeb882007-10-23 15:08:19 +0200373 kblockd_flush_work(&q->unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375EXPORT_SYMBOL(blk_sync_queue);
376
377/**
378 * blk_run_queue - run a single device queue
379 * @q: The queue to run
380 */
381void blk_run_queue(struct request_queue *q)
382{
383 unsigned long flags;
384
385 spin_lock_irqsave(q->queue_lock, flags);
386 blk_remove_plug(q);
Jens Axboedac07ec2006-05-11 08:20:16 +0200387
388 /*
389 * Only recurse once to avoid overrunning the stack, let the unplug
390 * handling reinvoke the handler shortly if we already got there.
391 */
392 if (!elv_queue_empty(q)) {
393 if (!test_and_set_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) {
394 q->request_fn(q);
395 clear_bit(QUEUE_FLAG_REENTER, &q->queue_flags);
396 } else {
397 blk_plug_device(q);
398 kblockd_schedule_work(&q->unplug_work);
399 }
400 }
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 spin_unlock_irqrestore(q->queue_lock, flags);
403}
404EXPORT_SYMBOL(blk_run_queue);
405
Jens Axboe165125e2007-07-24 09:28:11 +0200406void blk_put_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500407{
408 kobject_put(&q->kobj);
409}
410EXPORT_SYMBOL(blk_put_queue);
411
Jens Axboe6728cb02008-01-31 13:03:55 +0100412void blk_cleanup_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500413{
414 mutex_lock(&q->sysfs_lock);
415 set_bit(QUEUE_FLAG_DEAD, &q->queue_flags);
416 mutex_unlock(&q->sysfs_lock);
417
418 if (q->elevator)
419 elevator_exit(q->elevator);
420
421 blk_put_queue(q);
422}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423EXPORT_SYMBOL(blk_cleanup_queue);
424
Jens Axboe165125e2007-07-24 09:28:11 +0200425static int blk_init_free_list(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 struct request_list *rl = &q->rq;
428
429 rl->count[READ] = rl->count[WRITE] = 0;
430 rl->starved[READ] = rl->starved[WRITE] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200431 rl->elvpriv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 init_waitqueue_head(&rl->wait[READ]);
433 init_waitqueue_head(&rl->wait[WRITE]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Christoph Lameter19460892005-06-23 00:08:19 -0700435 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
436 mempool_free_slab, request_cachep, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 if (!rl->rq_pool)
439 return -ENOMEM;
440
441 return 0;
442}
443
Jens Axboe165125e2007-07-24 09:28:11 +0200444struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Christoph Lameter19460892005-06-23 00:08:19 -0700446 return blk_alloc_queue_node(gfp_mask, -1);
447}
448EXPORT_SYMBOL(blk_alloc_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Jens Axboe165125e2007-07-24 09:28:11 +0200450struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -0700451{
Jens Axboe165125e2007-07-24 09:28:11 +0200452 struct request_queue *q;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700453 int err;
Christoph Lameter19460892005-06-23 00:08:19 -0700454
Jens Axboe8324aa92008-01-29 14:51:59 +0100455 q = kmem_cache_alloc_node(blk_requestq_cachep,
Christoph Lameter94f60302007-07-17 04:03:29 -0700456 gfp_mask | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (!q)
458 return NULL;
459
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700460 q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
461 q->backing_dev_info.unplug_io_data = q;
462 err = bdi_init(&q->backing_dev_info);
463 if (err) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100464 kmem_cache_free(blk_requestq_cachep, q);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700465 return NULL;
466 }
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 init_timer(&q->unplug_timer);
Al Viro483f4af2006-03-18 18:34:37 -0500469
Jens Axboe8324aa92008-01-29 14:51:59 +0100470 kobject_init(&q->kobj, &blk_queue_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Al Viro483f4af2006-03-18 18:34:37 -0500472 mutex_init(&q->sysfs_lock);
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return q;
475}
Christoph Lameter19460892005-06-23 00:08:19 -0700476EXPORT_SYMBOL(blk_alloc_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478/**
479 * blk_init_queue - prepare a request queue for use with a block device
480 * @rfn: The function to be called to process requests that have been
481 * placed on the queue.
482 * @lock: Request queue spin lock
483 *
484 * Description:
485 * If a block device wishes to use the standard request handling procedures,
486 * which sorts requests and coalesces adjacent requests, then it must
487 * call blk_init_queue(). The function @rfn will be called when there
488 * are requests on the queue that need to be processed. If the device
489 * supports plugging, then @rfn may not be called immediately when requests
490 * are available on the queue, but may be called at some time later instead.
491 * Plugged queues are generally unplugged when a buffer belonging to one
492 * of the requests on the queue is needed, or due to memory pressure.
493 *
494 * @rfn is not required, or even expected, to remove all requests off the
495 * queue, but only as many as it can handle at a time. If it does leave
496 * requests on the queue, it is responsible for arranging that the requests
497 * get dealt with eventually.
498 *
499 * The queue spin lock must be held while manipulating the requests on the
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200500 * request queue; this lock will be taken also from interrupt context, so irq
501 * disabling is needed for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 *
503 * Function returns a pointer to the initialized request queue, or NULL if
504 * it didn't succeed.
505 *
506 * Note:
507 * blk_init_queue() must be paired with a blk_cleanup_queue() call
508 * when the block device is deactivated (such as at module unload).
509 **/
Christoph Lameter19460892005-06-23 00:08:19 -0700510
Jens Axboe165125e2007-07-24 09:28:11 +0200511struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Christoph Lameter19460892005-06-23 00:08:19 -0700513 return blk_init_queue_node(rfn, lock, -1);
514}
515EXPORT_SYMBOL(blk_init_queue);
516
Jens Axboe165125e2007-07-24 09:28:11 +0200517struct request_queue *
Christoph Lameter19460892005-06-23 00:08:19 -0700518blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
519{
Jens Axboe165125e2007-07-24 09:28:11 +0200520 struct request_queue *q = blk_alloc_queue_node(GFP_KERNEL, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 if (!q)
523 return NULL;
524
Christoph Lameter19460892005-06-23 00:08:19 -0700525 q->node = node_id;
Al Viro8669aaf2006-03-18 13:50:00 -0500526 if (blk_init_free_list(q)) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100527 kmem_cache_free(blk_requestq_cachep, q);
Al Viro8669aaf2006-03-18 13:50:00 -0500528 return NULL;
529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
152587d2005-04-12 16:22:06 -0500531 /*
532 * if caller didn't supply a lock, they get per-queue locking with
533 * our embedded lock
534 */
535 if (!lock) {
536 spin_lock_init(&q->__queue_lock);
537 lock = &q->__queue_lock;
538 }
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 q->request_fn = rfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 q->prep_rq_fn = NULL;
542 q->unplug_fn = generic_unplug_device;
543 q->queue_flags = (1 << QUEUE_FLAG_CLUSTER);
544 q->queue_lock = lock;
545
546 blk_queue_segment_boundary(q, 0xffffffff);
547
548 blk_queue_make_request(q, __make_request);
549 blk_queue_max_segment_size(q, MAX_SEGMENT_SIZE);
550
551 blk_queue_max_hw_segments(q, MAX_HW_SEGMENTS);
552 blk_queue_max_phys_segments(q, MAX_PHYS_SEGMENTS);
553
Alan Stern44ec9542007-02-20 11:01:57 -0500554 q->sg_reserved_size = INT_MAX;
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 /*
557 * all done
558 */
559 if (!elevator_init(q, NULL)) {
560 blk_queue_congestion_threshold(q);
561 return q;
562 }
563
Al Viro8669aaf2006-03-18 13:50:00 -0500564 blk_put_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return NULL;
566}
Christoph Lameter19460892005-06-23 00:08:19 -0700567EXPORT_SYMBOL(blk_init_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Jens Axboe165125e2007-07-24 09:28:11 +0200569int blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Nick Pigginfde6ad22005-06-23 00:08:53 -0700571 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
Al Viro483f4af2006-03-18 18:34:37 -0500572 kobject_get(&q->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return 0;
574 }
575
576 return 1;
577}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578EXPORT_SYMBOL(blk_get_queue);
579
Jens Axboe165125e2007-07-24 09:28:11 +0200580static inline void blk_free_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Jens Axboe4aff5e22006-08-10 08:44:47 +0200582 if (rq->cmd_flags & REQ_ELVPRIV)
Tejun Heocb98fc82005-10-28 08:29:39 +0200583 elv_put_request(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 mempool_free(rq, q->rq.rq_pool);
585}
586
Jens Axboe1ea25ecb2006-07-18 22:24:11 +0200587static struct request *
Jens Axboe165125e2007-07-24 09:28:11 +0200588blk_alloc_request(struct request_queue *q, int rw, int priv, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
591
592 if (!rq)
593 return NULL;
594
595 /*
Jens Axboe4aff5e22006-08-10 08:44:47 +0200596 * first three bits are identical in rq->cmd_flags and bio->bi_rw,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 * see bio.h and blkdev.h
598 */
Jens Axboe49171e52006-08-10 08:59:11 +0200599 rq->cmd_flags = rw | REQ_ALLOCED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Tejun Heocb98fc82005-10-28 08:29:39 +0200601 if (priv) {
Jens Axboecb78b282006-07-28 09:32:57 +0200602 if (unlikely(elv_set_request(q, rq, gfp_mask))) {
Tejun Heocb98fc82005-10-28 08:29:39 +0200603 mempool_free(rq, q->rq.rq_pool);
604 return NULL;
605 }
Jens Axboe4aff5e22006-08-10 08:44:47 +0200606 rq->cmd_flags |= REQ_ELVPRIV;
Tejun Heocb98fc82005-10-28 08:29:39 +0200607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Tejun Heocb98fc82005-10-28 08:29:39 +0200609 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
612/*
613 * ioc_batching returns true if the ioc is a valid batching request and
614 * should be given priority access to a request.
615 */
Jens Axboe165125e2007-07-24 09:28:11 +0200616static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
618 if (!ioc)
619 return 0;
620
621 /*
622 * Make sure the process is able to allocate at least 1 request
623 * even if the batch times out, otherwise we could theoretically
624 * lose wakeups.
625 */
626 return ioc->nr_batch_requests == q->nr_batching ||
627 (ioc->nr_batch_requests > 0
628 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
629}
630
631/*
632 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
633 * will cause the process to be a "batcher" on all queues in the system. This
634 * is the behaviour we want though - once it gets a wakeup it should be given
635 * a nice run.
636 */
Jens Axboe165125e2007-07-24 09:28:11 +0200637static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 if (!ioc || ioc_batching(q, ioc))
640 return;
641
642 ioc->nr_batch_requests = q->nr_batching;
643 ioc->last_waited = jiffies;
644}
645
Jens Axboe165125e2007-07-24 09:28:11 +0200646static void __freed_request(struct request_queue *q, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
648 struct request_list *rl = &q->rq;
649
650 if (rl->count[rw] < queue_congestion_off_threshold(q))
Thomas Maier79e2de42006-10-19 23:28:15 -0700651 blk_clear_queue_congested(q, rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 if (rl->count[rw] + 1 <= q->nr_requests) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (waitqueue_active(&rl->wait[rw]))
655 wake_up(&rl->wait[rw]);
656
657 blk_clear_queue_full(q, rw);
658 }
659}
660
661/*
662 * A request has just been released. Account for it, update the full and
663 * congestion status, wake up any waiters. Called under q->queue_lock.
664 */
Jens Axboe165125e2007-07-24 09:28:11 +0200665static void freed_request(struct request_queue *q, int rw, int priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
667 struct request_list *rl = &q->rq;
668
669 rl->count[rw]--;
Tejun Heocb98fc82005-10-28 08:29:39 +0200670 if (priv)
671 rl->elvpriv--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 __freed_request(q, rw);
674
675 if (unlikely(rl->starved[rw ^ 1]))
676 __freed_request(q, rw ^ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
679#define blkdev_free_rq(list) list_entry((list)->next, struct request, queuelist)
680/*
Nick Piggind6344532005-06-28 20:45:14 -0700681 * Get a free request, queue_lock must be held.
682 * Returns NULL on failure, with queue_lock held.
683 * Returns !NULL on success, with queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 */
Jens Axboe165125e2007-07-24 09:28:11 +0200685static struct request *get_request(struct request_queue *q, int rw_flags,
Jens Axboe7749a8d2006-12-13 13:02:26 +0100686 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688 struct request *rq = NULL;
689 struct request_list *rl = &q->rq;
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100690 struct io_context *ioc = NULL;
Jens Axboe7749a8d2006-12-13 13:02:26 +0100691 const int rw = rw_flags & 0x01;
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100692 int may_queue, priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Jens Axboe7749a8d2006-12-13 13:02:26 +0100694 may_queue = elv_may_queue(q, rw_flags);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100695 if (may_queue == ELV_MQUEUE_NO)
696 goto rq_starved;
697
698 if (rl->count[rw]+1 >= queue_congestion_on_threshold(q)) {
699 if (rl->count[rw]+1 >= q->nr_requests) {
Jens Axboeb5deef92006-07-19 23:39:40 +0200700 ioc = current_io_context(GFP_ATOMIC, q->node);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100701 /*
702 * The queue will fill after this allocation, so set
703 * it as full, and mark this process as "batching".
704 * This process will be allowed to complete a batch of
705 * requests, others will be blocked.
706 */
707 if (!blk_queue_full(q, rw)) {
708 ioc_set_batching(q, ioc);
709 blk_set_queue_full(q, rw);
710 } else {
711 if (may_queue != ELV_MQUEUE_MUST
712 && !ioc_batching(q, ioc)) {
713 /*
714 * The queue is full and the allocating
715 * process is not a "batcher", and not
716 * exempted by the IO scheduler
717 */
718 goto out;
719 }
720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 }
Thomas Maier79e2de42006-10-19 23:28:15 -0700722 blk_set_queue_congested(q, rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
724
Jens Axboe082cf692005-06-28 16:35:11 +0200725 /*
726 * Only allow batching queuers to allocate up to 50% over the defined
727 * limit of requests, otherwise we could have thousands of requests
728 * allocated with any setting of ->nr_requests
729 */
Hugh Dickinsfd782a42005-06-29 15:15:40 +0100730 if (rl->count[rw] >= (3 * q->nr_requests / 2))
Jens Axboe082cf692005-06-28 16:35:11 +0200731 goto out;
Hugh Dickinsfd782a42005-06-29 15:15:40 +0100732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 rl->count[rw]++;
734 rl->starved[rw] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200735
Jens Axboe64521d12005-10-28 08:30:39 +0200736 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Tejun Heocb98fc82005-10-28 08:29:39 +0200737 if (priv)
738 rl->elvpriv++;
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 spin_unlock_irq(q->queue_lock);
741
Jens Axboe7749a8d2006-12-13 13:02:26 +0100742 rq = blk_alloc_request(q, rw_flags, priv, gfp_mask);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100743 if (unlikely(!rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 /*
745 * Allocation failed presumably due to memory. Undo anything
746 * we might have messed up.
747 *
748 * Allocating task should really be put onto the front of the
749 * wait queue, but this is pretty rare.
750 */
751 spin_lock_irq(q->queue_lock);
Tejun Heocb98fc82005-10-28 08:29:39 +0200752 freed_request(q, rw, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 /*
755 * in the very unlikely event that allocation failed and no
756 * requests for this direction was pending, mark us starved
757 * so that freeing of a request in the other direction will
758 * notice us. another possible fix would be to split the
759 * rq mempool into READ and WRITE
760 */
761rq_starved:
762 if (unlikely(rl->count[rw] == 0))
763 rl->starved[rw] = 1;
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 goto out;
766 }
767
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100768 /*
769 * ioc may be NULL here, and ioc_batching will be false. That's
770 * OK, if the queue is under the request limit then requests need
771 * not count toward the nr_batch_requests limit. There will always
772 * be some limit enforced by BLK_BATCH_TIME.
773 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (ioc_batching(q, ioc))
775 ioc->nr_batch_requests--;
Jens Axboe6728cb02008-01-31 13:03:55 +0100776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 rq_init(q, rq);
Jens Axboe2056a782006-03-23 20:00:26 +0100778
779 blk_add_trace_generic(q, bio, rw, BLK_TA_GETRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return rq;
782}
783
784/*
785 * No available requests for this queue, unplug the device and wait for some
786 * requests to become available.
Nick Piggind6344532005-06-28 20:45:14 -0700787 *
788 * Called with q->queue_lock held, and returns with it unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 */
Jens Axboe165125e2007-07-24 09:28:11 +0200790static struct request *get_request_wait(struct request_queue *q, int rw_flags,
Jens Axboe22e2c502005-06-27 10:55:12 +0200791 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
Jens Axboe7749a8d2006-12-13 13:02:26 +0100793 const int rw = rw_flags & 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 struct request *rq;
795
Jens Axboe7749a8d2006-12-13 13:02:26 +0100796 rq = get_request(q, rw_flags, bio, GFP_NOIO);
Nick Piggin450991b2005-06-28 20:45:13 -0700797 while (!rq) {
798 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 struct request_list *rl = &q->rq;
800
801 prepare_to_wait_exclusive(&rl->wait[rw], &wait,
802 TASK_UNINTERRUPTIBLE);
803
Jens Axboe7749a8d2006-12-13 13:02:26 +0100804 rq = get_request(q, rw_flags, bio, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 if (!rq) {
807 struct io_context *ioc;
808
Jens Axboe2056a782006-03-23 20:00:26 +0100809 blk_add_trace_generic(q, bio, rw, BLK_TA_SLEEPRQ);
810
Nick Piggind6344532005-06-28 20:45:14 -0700811 __generic_unplug_device(q);
812 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 io_schedule();
814
815 /*
816 * After sleeping, we become a "batching" process and
817 * will be able to allocate at least one request, and
818 * up to a big batch of them for a small period time.
819 * See ioc_batching, ioc_set_batching
820 */
Jens Axboeb5deef92006-07-19 23:39:40 +0200821 ioc = current_io_context(GFP_NOIO, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 ioc_set_batching(q, ioc);
Nick Piggind6344532005-06-28 20:45:14 -0700823
824 spin_lock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
826 finish_wait(&rl->wait[rw], &wait);
Nick Piggin450991b2005-06-28 20:45:13 -0700827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 return rq;
830}
831
Jens Axboe165125e2007-07-24 09:28:11 +0200832struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
834 struct request *rq;
835
836 BUG_ON(rw != READ && rw != WRITE);
837
Nick Piggind6344532005-06-28 20:45:14 -0700838 spin_lock_irq(q->queue_lock);
839 if (gfp_mask & __GFP_WAIT) {
Jens Axboe22e2c502005-06-27 10:55:12 +0200840 rq = get_request_wait(q, rw, NULL);
Nick Piggind6344532005-06-28 20:45:14 -0700841 } else {
Jens Axboe22e2c502005-06-27 10:55:12 +0200842 rq = get_request(q, rw, NULL, gfp_mask);
Nick Piggind6344532005-06-28 20:45:14 -0700843 if (!rq)
844 spin_unlock_irq(q->queue_lock);
845 }
846 /* q->queue_lock is unlocked at this point */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 return rq;
849}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850EXPORT_SYMBOL(blk_get_request);
851
852/**
Jens Axboedc72ef42006-07-20 14:54:05 +0200853 * blk_start_queueing - initiate dispatch of requests to device
854 * @q: request queue to kick into gear
855 *
856 * This is basically a helper to remove the need to know whether a queue
857 * is plugged or not if someone just wants to initiate dispatch of requests
858 * for this queue.
859 *
860 * The queue lock must be held with interrupts disabled.
861 */
Jens Axboe165125e2007-07-24 09:28:11 +0200862void blk_start_queueing(struct request_queue *q)
Jens Axboedc72ef42006-07-20 14:54:05 +0200863{
864 if (!blk_queue_plugged(q))
865 q->request_fn(q);
866 else
867 __generic_unplug_device(q);
868}
869EXPORT_SYMBOL(blk_start_queueing);
870
871/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 * blk_requeue_request - put a request back on queue
873 * @q: request queue where request should be inserted
874 * @rq: request to be inserted
875 *
876 * Description:
877 * Drivers often keep queueing requests until the hardware cannot accept
878 * more, when that condition happens we need to put the request back
879 * on the queue. Must be called with queue lock held.
880 */
Jens Axboe165125e2007-07-24 09:28:11 +0200881void blk_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Jens Axboe2056a782006-03-23 20:00:26 +0100883 blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 if (blk_rq_tagged(rq))
886 blk_queue_end_tag(q, rq);
887
888 elv_requeue_request(q, rq);
889}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890EXPORT_SYMBOL(blk_requeue_request);
891
892/**
893 * blk_insert_request - insert a special request in to a request queue
894 * @q: request queue where request should be inserted
895 * @rq: request to be inserted
896 * @at_head: insert request at head or tail of queue
897 * @data: private data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 *
899 * Description:
900 * Many block devices need to execute commands asynchronously, so they don't
901 * block the whole kernel from preemption during request execution. This is
902 * accomplished normally by inserting aritficial requests tagged as
903 * REQ_SPECIAL in to the corresponding request queue, and letting them be
904 * scheduled for actual execution by the request queue.
905 *
906 * We have the option of inserting the head or the tail of the queue.
907 * Typically we use the tail for new ioctls and so forth. We use the head
908 * of the queue for things like a QUEUE_FULL message from a device, or a
909 * host that is unable to accept a particular command.
910 */
Jens Axboe165125e2007-07-24 09:28:11 +0200911void blk_insert_request(struct request_queue *q, struct request *rq,
Tejun Heo 867d1192005-04-24 02:06:05 -0500912 int at_head, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Tejun Heo 867d1192005-04-24 02:06:05 -0500914 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 unsigned long flags;
916
917 /*
918 * tell I/O scheduler that this isn't a regular read/write (ie it
919 * must not attempt merges on this) and that it acts as a soft
920 * barrier
921 */
Jens Axboe4aff5e22006-08-10 08:44:47 +0200922 rq->cmd_type = REQ_TYPE_SPECIAL;
923 rq->cmd_flags |= REQ_SOFTBARRIER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 rq->special = data;
926
927 spin_lock_irqsave(q->queue_lock, flags);
928
929 /*
930 * If command is tagged, release the tag
931 */
Tejun Heo 867d1192005-04-24 02:06:05 -0500932 if (blk_rq_tagged(rq))
933 blk_queue_end_tag(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Jerome Marchandb238b3d2007-10-23 15:05:46 +0200935 drive_stat_acct(rq, 1);
Tejun Heo 867d1192005-04-24 02:06:05 -0500936 __elv_add_request(q, rq, where, 0);
Jens Axboedc72ef42006-07-20 14:54:05 +0200937 blk_start_queueing(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 spin_unlock_irqrestore(q->queue_lock, flags);
939}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940EXPORT_SYMBOL(blk_insert_request);
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942/*
943 * add-request adds a request to the linked list.
944 * queue lock is held and interrupts disabled, as we muck with the
945 * request queue list.
946 */
Jens Axboe6728cb02008-01-31 13:03:55 +0100947static inline void add_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Jerome Marchandb238b3d2007-10-23 15:05:46 +0200949 drive_stat_acct(req, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 /*
952 * elevator indicated where it wants this request to be
953 * inserted at elevator_merge time
954 */
955 __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
956}
Jens Axboe6728cb02008-01-31 13:03:55 +0100957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958/*
959 * disk_round_stats() - Round off the performance stats on a struct
960 * disk_stats.
961 *
962 * The average IO queue length and utilisation statistics are maintained
963 * by observing the current state of the queue length and the amount of
964 * time it has been in this state for.
965 *
966 * Normally, that accounting is done on IO completion, but that can result
967 * in more than a second's worth of IO being accounted for within any one
968 * second, leading to >100% utilisation. To deal with that, we call this
969 * function to do a round-off before returning the results when reading
970 * /proc/diskstats. This accounts immediately for all queue usage up to
971 * the current jiffies and restarts the counters again.
972 */
973void disk_round_stats(struct gendisk *disk)
974{
975 unsigned long now = jiffies;
976
Chen, Kenneth Wb2982642005-10-13 21:49:29 +0200977 if (now == disk->stamp)
978 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Chen, Kenneth W20e5c812005-10-13 21:48:42 +0200980 if (disk->in_flight) {
981 __disk_stat_add(disk, time_in_queue,
982 disk->in_flight * (now - disk->stamp));
983 __disk_stat_add(disk, io_ticks, (now - disk->stamp));
984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 disk->stamp = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986}
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800987EXPORT_SYMBOL_GPL(disk_round_stats);
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989/*
990 * queue lock must be held
991 */
Jens Axboe165125e2007-07-24 09:28:11 +0200992void __blk_put_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (unlikely(!q))
995 return;
996 if (unlikely(--req->ref_count))
997 return;
998
Tejun Heo8922e162005-10-20 16:23:44 +0200999 elv_completed_request(q, req);
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 /*
1002 * Request may not have originated from ll_rw_blk. if not,
1003 * it didn't come out of our reserved rq pools
1004 */
Jens Axboe49171e52006-08-10 08:59:11 +02001005 if (req->cmd_flags & REQ_ALLOCED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 int rw = rq_data_dir(req);
Jens Axboe4aff5e22006-08-10 08:44:47 +02001007 int priv = req->cmd_flags & REQ_ELVPRIV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 BUG_ON(!list_empty(&req->queuelist));
Jens Axboe98170642006-07-28 09:23:08 +02001010 BUG_ON(!hlist_unhashed(&req->hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012 blk_free_request(q, req);
Tejun Heocb98fc82005-10-28 08:29:39 +02001013 freed_request(q, rw, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 }
1015}
Mike Christie6e39b69e2005-11-11 05:30:24 -06001016EXPORT_SYMBOL_GPL(__blk_put_request);
1017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018void blk_put_request(struct request *req)
1019{
Tejun Heo8922e162005-10-20 16:23:44 +02001020 unsigned long flags;
Jens Axboe165125e2007-07-24 09:28:11 +02001021 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Tejun Heo8922e162005-10-20 16:23:44 +02001023 /*
1024 * Gee, IDE calls in w/ NULL q. Fix IDE and remove the
1025 * following if (q) test.
1026 */
1027 if (q) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 spin_lock_irqsave(q->queue_lock, flags);
1029 __blk_put_request(q, req);
1030 spin_unlock_irqrestore(q->queue_lock, flags);
1031 }
1032}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033EXPORT_SYMBOL(blk_put_request);
1034
Jens Axboe86db1e22008-01-29 14:53:40 +01001035void init_request_from_bio(struct request *req, struct bio *bio)
Tejun Heo52d9e672006-01-06 09:49:58 +01001036{
Jens Axboe4aff5e22006-08-10 08:44:47 +02001037 req->cmd_type = REQ_TYPE_FS;
Tejun Heo52d9e672006-01-06 09:49:58 +01001038
1039 /*
1040 * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
1041 */
1042 if (bio_rw_ahead(bio) || bio_failfast(bio))
Jens Axboe4aff5e22006-08-10 08:44:47 +02001043 req->cmd_flags |= REQ_FAILFAST;
Tejun Heo52d9e672006-01-06 09:49:58 +01001044
1045 /*
1046 * REQ_BARRIER implies no merging, but lets make it explicit
1047 */
1048 if (unlikely(bio_barrier(bio)))
Jens Axboe4aff5e22006-08-10 08:44:47 +02001049 req->cmd_flags |= (REQ_HARDBARRIER | REQ_NOMERGE);
Tejun Heo52d9e672006-01-06 09:49:58 +01001050
Jens Axboeb31dc662006-06-13 08:26:10 +02001051 if (bio_sync(bio))
Jens Axboe4aff5e22006-08-10 08:44:47 +02001052 req->cmd_flags |= REQ_RW_SYNC;
Jens Axboe5404bc72006-08-10 09:01:02 +02001053 if (bio_rw_meta(bio))
1054 req->cmd_flags |= REQ_RW_META;
Jens Axboeb31dc662006-06-13 08:26:10 +02001055
Tejun Heo52d9e672006-01-06 09:49:58 +01001056 req->errors = 0;
1057 req->hard_sector = req->sector = bio->bi_sector;
Tejun Heo52d9e672006-01-06 09:49:58 +01001058 req->ioprio = bio_prio(bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001059 req->start_time = jiffies;
NeilBrownbc1c56f2007-08-16 13:31:30 +02001060 blk_rq_bio_prep(req->q, req, bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001061}
1062
Jens Axboe165125e2007-07-24 09:28:11 +02001063static int __make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
Nick Piggin450991b2005-06-28 20:45:13 -07001065 struct request *req;
Jens Axboe51da90f2006-07-18 04:14:45 +02001066 int el_ret, nr_sectors, barrier, err;
1067 const unsigned short prio = bio_prio(bio);
1068 const int sync = bio_sync(bio);
Jens Axboe7749a8d2006-12-13 13:02:26 +01001069 int rw_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 nr_sectors = bio_sectors(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 /*
1074 * low level driver can indicate that it wants pages above a
1075 * certain limit bounced to low memory (ie for highmem, or even
1076 * ISA dma in theory)
1077 */
1078 blk_queue_bounce(q, &bio);
1079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 barrier = bio_barrier(bio);
Tejun Heo797e7db2006-01-06 09:51:03 +01001081 if (unlikely(barrier) && (q->next_ordered == QUEUE_ORDERED_NONE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 err = -EOPNOTSUPP;
1083 goto end_io;
1084 }
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 spin_lock_irq(q->queue_lock);
1087
Nick Piggin450991b2005-06-28 20:45:13 -07001088 if (unlikely(barrier) || elv_queue_empty(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 goto get_rq;
1090
1091 el_ret = elv_merge(q, &req, bio);
1092 switch (el_ret) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001093 case ELEVATOR_BACK_MERGE:
1094 BUG_ON(!rq_mergeable(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Jens Axboe6728cb02008-01-31 13:03:55 +01001096 if (!ll_back_merge_fn(q, req, bio))
1097 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Jens Axboe6728cb02008-01-31 13:03:55 +01001099 blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
Jens Axboe2056a782006-03-23 20:00:26 +01001100
Jens Axboe6728cb02008-01-31 13:03:55 +01001101 req->biotail->bi_next = bio;
1102 req->biotail = bio;
1103 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
1104 req->ioprio = ioprio_best(req->ioprio, prio);
1105 drive_stat_acct(req, 0);
1106 if (!attempt_back_merge(q, req))
1107 elv_merged_request(q, req, el_ret);
1108 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Jens Axboe6728cb02008-01-31 13:03:55 +01001110 case ELEVATOR_FRONT_MERGE:
1111 BUG_ON(!rq_mergeable(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Jens Axboe6728cb02008-01-31 13:03:55 +01001113 if (!ll_front_merge_fn(q, req, bio))
1114 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Jens Axboe6728cb02008-01-31 13:03:55 +01001116 blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
Jens Axboe2056a782006-03-23 20:00:26 +01001117
Jens Axboe6728cb02008-01-31 13:03:55 +01001118 bio->bi_next = req->bio;
1119 req->bio = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Jens Axboe6728cb02008-01-31 13:03:55 +01001121 /*
1122 * may not be valid. if the low level driver said
1123 * it didn't need a bounce buffer then it better
1124 * not touch req->buffer either...
1125 */
1126 req->buffer = bio_data(bio);
1127 req->current_nr_sectors = bio_cur_sectors(bio);
1128 req->hard_cur_sectors = req->current_nr_sectors;
1129 req->sector = req->hard_sector = bio->bi_sector;
1130 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
1131 req->ioprio = ioprio_best(req->ioprio, prio);
1132 drive_stat_acct(req, 0);
1133 if (!attempt_front_merge(q, req))
1134 elv_merged_request(q, req, el_ret);
1135 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Jens Axboe6728cb02008-01-31 13:03:55 +01001137 /* ELV_NO_MERGE: elevator says don't/can't merge. */
1138 default:
1139 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
1141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142get_rq:
Nick Piggin450991b2005-06-28 20:45:13 -07001143 /*
Jens Axboe7749a8d2006-12-13 13:02:26 +01001144 * This sync check and mask will be re-done in init_request_from_bio(),
1145 * but we need to set it earlier to expose the sync flag to the
1146 * rq allocator and io schedulers.
1147 */
1148 rw_flags = bio_data_dir(bio);
1149 if (sync)
1150 rw_flags |= REQ_RW_SYNC;
1151
1152 /*
Nick Piggin450991b2005-06-28 20:45:13 -07001153 * Grab a free request. This is might sleep but can not fail.
Nick Piggind6344532005-06-28 20:45:14 -07001154 * Returns with the queue unlocked.
Nick Piggin450991b2005-06-28 20:45:13 -07001155 */
Jens Axboe7749a8d2006-12-13 13:02:26 +01001156 req = get_request_wait(q, rw_flags, bio);
Nick Piggind6344532005-06-28 20:45:14 -07001157
Nick Piggin450991b2005-06-28 20:45:13 -07001158 /*
1159 * After dropping the lock and possibly sleeping here, our request
1160 * may now be mergeable after it had proven unmergeable (above).
1161 * We don't worry about that case for efficiency. It won't happen
1162 * often, and the elevators are able to handle it.
1163 */
Tejun Heo52d9e672006-01-06 09:49:58 +01001164 init_request_from_bio(req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Nick Piggin450991b2005-06-28 20:45:13 -07001166 spin_lock_irq(q->queue_lock);
1167 if (elv_queue_empty(q))
1168 blk_plug_device(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 add_request(q, req);
1170out:
Jens Axboe4a534f92005-04-16 15:25:40 -07001171 if (sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 __generic_unplug_device(q);
1173
1174 spin_unlock_irq(q->queue_lock);
1175 return 0;
1176
1177end_io:
NeilBrown6712ecf2007-09-27 12:47:43 +02001178 bio_endio(bio, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 return 0;
1180}
1181
1182/*
1183 * If bio->bi_dev is a partition, remap the location
1184 */
1185static inline void blk_partition_remap(struct bio *bio)
1186{
1187 struct block_device *bdev = bio->bi_bdev;
1188
Jens Axboebf2de6f2007-09-27 13:01:25 +02001189 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 struct hd_struct *p = bdev->bd_part;
Jens Axboea3623572005-11-01 09:26:16 +01001191 const int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Jens Axboea3623572005-11-01 09:26:16 +01001193 p->sectors[rw] += bio_sectors(bio);
1194 p->ios[rw]++;
1195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 bio->bi_sector += p->start_sect;
1197 bio->bi_bdev = bdev->bd_contains;
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001198
1199 blk_add_trace_remap(bdev_get_queue(bio->bi_bdev), bio,
1200 bdev->bd_dev, bio->bi_sector,
1201 bio->bi_sector - p->start_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 }
1203}
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205static void handle_bad_sector(struct bio *bio)
1206{
1207 char b[BDEVNAME_SIZE];
1208
1209 printk(KERN_INFO "attempt to access beyond end of device\n");
1210 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1211 bdevname(bio->bi_bdev, b),
1212 bio->bi_rw,
1213 (unsigned long long)bio->bi_sector + bio_sectors(bio),
1214 (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
1215
1216 set_bit(BIO_EOF, &bio->bi_flags);
1217}
1218
Akinobu Mitac17bb492006-12-08 02:39:46 -08001219#ifdef CONFIG_FAIL_MAKE_REQUEST
1220
1221static DECLARE_FAULT_ATTR(fail_make_request);
1222
1223static int __init setup_fail_make_request(char *str)
1224{
1225 return setup_fault_attr(&fail_make_request, str);
1226}
1227__setup("fail_make_request=", setup_fail_make_request);
1228
1229static int should_fail_request(struct bio *bio)
1230{
1231 if ((bio->bi_bdev->bd_disk->flags & GENHD_FL_FAIL) ||
1232 (bio->bi_bdev->bd_part && bio->bi_bdev->bd_part->make_it_fail))
1233 return should_fail(&fail_make_request, bio->bi_size);
1234
1235 return 0;
1236}
1237
1238static int __init fail_make_request_debugfs(void)
1239{
1240 return init_fault_attr_dentries(&fail_make_request,
1241 "fail_make_request");
1242}
1243
1244late_initcall(fail_make_request_debugfs);
1245
1246#else /* CONFIG_FAIL_MAKE_REQUEST */
1247
1248static inline int should_fail_request(struct bio *bio)
1249{
1250 return 0;
1251}
1252
1253#endif /* CONFIG_FAIL_MAKE_REQUEST */
1254
Jens Axboec07e2b42007-07-18 13:27:58 +02001255/*
1256 * Check whether this bio extends beyond the end of the device.
1257 */
1258static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1259{
1260 sector_t maxsector;
1261
1262 if (!nr_sectors)
1263 return 0;
1264
1265 /* Test device or partition size, when known. */
1266 maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
1267 if (maxsector) {
1268 sector_t sector = bio->bi_sector;
1269
1270 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1271 /*
1272 * This may well happen - the kernel calls bread()
1273 * without checking the size of the device, e.g., when
1274 * mounting a device.
1275 */
1276 handle_bad_sector(bio);
1277 return 1;
1278 }
1279 }
1280
1281 return 0;
1282}
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284/**
1285 * generic_make_request: hand a buffer to its device driver for I/O
1286 * @bio: The bio describing the location in memory and on the device.
1287 *
1288 * generic_make_request() is used to make I/O requests of block
1289 * devices. It is passed a &struct bio, which describes the I/O that needs
1290 * to be done.
1291 *
1292 * generic_make_request() does not return any status. The
1293 * success/failure status of the request, along with notification of
1294 * completion, is delivered asynchronously through the bio->bi_end_io
1295 * function described (one day) else where.
1296 *
1297 * The caller of generic_make_request must make sure that bi_io_vec
1298 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1299 * set to describe the device address, and the
1300 * bi_end_io and optionally bi_private are set to describe how
1301 * completion notification should be signaled.
1302 *
1303 * generic_make_request and the drivers it calls may use bi_next if this
1304 * bio happens to be merged with someone else, and may change bi_dev and
1305 * bi_sector for remaps as it sees fit. So the values of these fields
1306 * should NOT be depended on after the call to generic_make_request.
1307 */
Neil Brownd89d8792007-05-01 09:53:42 +02001308static inline void __generic_make_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
Jens Axboe165125e2007-07-24 09:28:11 +02001310 struct request_queue *q;
NeilBrown5ddfe962006-10-30 22:07:21 -08001311 sector_t old_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 int ret, nr_sectors = bio_sectors(bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001313 dev_t old_dev;
Jens Axboe51fd77b2007-11-02 08:49:08 +01001314 int err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Jens Axboec07e2b42007-07-18 13:27:58 +02001318 if (bio_check_eod(bio, nr_sectors))
1319 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 /*
1322 * Resolve the mapping until finished. (drivers are
1323 * still free to implement/resolve their own stacking
1324 * by explicitly returning 0)
1325 *
1326 * NOTE: we don't repeat the blk_size check for each new device.
1327 * Stacking drivers are expected to know what they are doing.
1328 */
NeilBrown5ddfe962006-10-30 22:07:21 -08001329 old_sector = -1;
Jens Axboe2056a782006-03-23 20:00:26 +01001330 old_dev = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 do {
1332 char b[BDEVNAME_SIZE];
1333
1334 q = bdev_get_queue(bio->bi_bdev);
1335 if (!q) {
1336 printk(KERN_ERR
1337 "generic_make_request: Trying to access "
1338 "nonexistent block-device %s (%Lu)\n",
1339 bdevname(bio->bi_bdev, b),
1340 (long long) bio->bi_sector);
1341end_io:
Jens Axboe51fd77b2007-11-02 08:49:08 +01001342 bio_endio(bio, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 break;
1344 }
1345
Jens Axboe4fa253f2007-07-18 13:13:10 +02001346 if (unlikely(nr_sectors > q->max_hw_sectors)) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001347 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 bdevname(bio->bi_bdev, b),
1349 bio_sectors(bio),
1350 q->max_hw_sectors);
1351 goto end_io;
1352 }
1353
Nick Pigginfde6ad22005-06-23 00:08:53 -07001354 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 goto end_io;
1356
Akinobu Mitac17bb492006-12-08 02:39:46 -08001357 if (should_fail_request(bio))
1358 goto end_io;
1359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 /*
1361 * If this device has partitions, remap block n
1362 * of partition p to block n+start(p) of the disk.
1363 */
1364 blk_partition_remap(bio);
1365
NeilBrown5ddfe962006-10-30 22:07:21 -08001366 if (old_sector != -1)
Jens Axboe4fa253f2007-07-18 13:13:10 +02001367 blk_add_trace_remap(q, bio, old_dev, bio->bi_sector,
NeilBrown5ddfe962006-10-30 22:07:21 -08001368 old_sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001369
1370 blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
1371
NeilBrown5ddfe962006-10-30 22:07:21 -08001372 old_sector = bio->bi_sector;
Jens Axboe2056a782006-03-23 20:00:26 +01001373 old_dev = bio->bi_bdev->bd_dev;
1374
Jens Axboec07e2b42007-07-18 13:27:58 +02001375 if (bio_check_eod(bio, nr_sectors))
1376 goto end_io;
Jens Axboe51fd77b2007-11-02 08:49:08 +01001377 if (bio_empty_barrier(bio) && !q->prepare_flush_fn) {
1378 err = -EOPNOTSUPP;
1379 goto end_io;
1380 }
NeilBrown5ddfe962006-10-30 22:07:21 -08001381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 ret = q->make_request_fn(q, bio);
1383 } while (ret);
1384}
1385
Neil Brownd89d8792007-05-01 09:53:42 +02001386/*
1387 * We only want one ->make_request_fn to be active at a time,
1388 * else stack usage with stacked devices could be a problem.
1389 * So use current->bio_{list,tail} to keep a list of requests
1390 * submited by a make_request_fn function.
1391 * current->bio_tail is also used as a flag to say if
1392 * generic_make_request is currently active in this task or not.
1393 * If it is NULL, then no make_request is active. If it is non-NULL,
1394 * then a make_request is active, and new requests should be added
1395 * at the tail
1396 */
1397void generic_make_request(struct bio *bio)
1398{
1399 if (current->bio_tail) {
1400 /* make_request is active */
1401 *(current->bio_tail) = bio;
1402 bio->bi_next = NULL;
1403 current->bio_tail = &bio->bi_next;
1404 return;
1405 }
1406 /* following loop may be a bit non-obvious, and so deserves some
1407 * explanation.
1408 * Before entering the loop, bio->bi_next is NULL (as all callers
1409 * ensure that) so we have a list with a single bio.
1410 * We pretend that we have just taken it off a longer list, so
1411 * we assign bio_list to the next (which is NULL) and bio_tail
1412 * to &bio_list, thus initialising the bio_list of new bios to be
1413 * added. __generic_make_request may indeed add some more bios
1414 * through a recursive call to generic_make_request. If it
1415 * did, we find a non-NULL value in bio_list and re-enter the loop
1416 * from the top. In this case we really did just take the bio
1417 * of the top of the list (no pretending) and so fixup bio_list and
1418 * bio_tail or bi_next, and call into __generic_make_request again.
1419 *
1420 * The loop was structured like this to make only one call to
1421 * __generic_make_request (which is important as it is large and
1422 * inlined) and to keep the structure simple.
1423 */
1424 BUG_ON(bio->bi_next);
1425 do {
1426 current->bio_list = bio->bi_next;
1427 if (bio->bi_next == NULL)
1428 current->bio_tail = &current->bio_list;
1429 else
1430 bio->bi_next = NULL;
1431 __generic_make_request(bio);
1432 bio = current->bio_list;
1433 } while (bio);
1434 current->bio_tail = NULL; /* deactivate */
1435}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436EXPORT_SYMBOL(generic_make_request);
1437
1438/**
1439 * submit_bio: submit a bio to the block device layer for I/O
1440 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1441 * @bio: The &struct bio which describes the I/O
1442 *
1443 * submit_bio() is very similar in purpose to generic_make_request(), and
1444 * uses that function to do most of the work. Both are fairly rough
1445 * interfaces, @bio must be presetup and ready for I/O.
1446 *
1447 */
1448void submit_bio(int rw, struct bio *bio)
1449{
1450 int count = bio_sectors(bio);
1451
Jens Axboe22e2c502005-06-27 10:55:12 +02001452 bio->bi_rw |= rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Jens Axboebf2de6f2007-09-27 13:01:25 +02001454 /*
1455 * If it's a regular read/write or a barrier with data attached,
1456 * go through the normal accounting stuff before submission.
1457 */
1458 if (!bio_empty_barrier(bio)) {
1459
1460 BIO_BUG_ON(!bio->bi_size);
1461 BIO_BUG_ON(!bio->bi_io_vec);
1462
1463 if (rw & WRITE) {
1464 count_vm_events(PGPGOUT, count);
1465 } else {
1466 task_io_account_read(bio->bi_size);
1467 count_vm_events(PGPGIN, count);
1468 }
1469
1470 if (unlikely(block_dump)) {
1471 char b[BDEVNAME_SIZE];
1472 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001473 current->comm, task_pid_nr(current),
Jens Axboebf2de6f2007-09-27 13:01:25 +02001474 (rw & WRITE) ? "WRITE" : "READ",
1475 (unsigned long long)bio->bi_sector,
Jens Axboe6728cb02008-01-31 13:03:55 +01001476 bdevname(bio->bi_bdev, b));
Jens Axboebf2de6f2007-09-27 13:01:25 +02001477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 }
1479
1480 generic_make_request(bio);
1481}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482EXPORT_SYMBOL(submit_bio);
1483
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001484/**
1485 * __end_that_request_first - end I/O on a request
1486 * @req: the request being processed
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001487 * @error: 0 for success, < 0 for error
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001488 * @nr_bytes: number of bytes to complete
1489 *
1490 * Description:
1491 * Ends I/O on a number of bytes attached to @req, and sets it up
1492 * for the next range of segments (if any) in the cluster.
1493 *
1494 * Return:
1495 * 0 - we are done with this request, call end_that_request_last()
1496 * 1 - still buffers pending for this request
1497 **/
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001498static int __end_that_request_first(struct request *req, int error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 int nr_bytes)
1500{
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001501 int total_bytes, bio_nbytes, next_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 struct bio *bio;
1503
Jens Axboe2056a782006-03-23 20:00:26 +01001504 blk_add_trace_rq(req->q, req, BLK_TA_COMPLETE);
1505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 * for a REQ_BLOCK_PC request, we want to carry any eventual
1508 * sense key with us all the way through
1509 */
1510 if (!blk_pc_request(req))
1511 req->errors = 0;
1512
Jens Axboe6728cb02008-01-31 13:03:55 +01001513 if (error && (blk_fs_request(req) && !(req->cmd_flags & REQ_QUIET))) {
1514 printk(KERN_ERR "end_request: I/O error, dev %s, sector %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 req->rq_disk ? req->rq_disk->disk_name : "?",
1516 (unsigned long long)req->sector);
1517 }
1518
Jens Axboed72d9042005-11-01 08:35:42 +01001519 if (blk_fs_request(req) && req->rq_disk) {
Jens Axboea3623572005-11-01 09:26:16 +01001520 const int rw = rq_data_dir(req);
1521
Jens Axboe53e86062006-01-17 11:09:27 +01001522 disk_stat_add(req->rq_disk, sectors[rw], nr_bytes >> 9);
Jens Axboed72d9042005-11-01 08:35:42 +01001523 }
1524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 total_bytes = bio_nbytes = 0;
1526 while ((bio = req->bio) != NULL) {
1527 int nbytes;
1528
Jens Axboebf2de6f2007-09-27 13:01:25 +02001529 /*
1530 * For an empty barrier request, the low level driver must
1531 * store a potential error location in ->sector. We pass
1532 * that back up in ->bi_sector.
1533 */
1534 if (blk_empty_barrier(req))
1535 bio->bi_sector = req->sector;
1536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 if (nr_bytes >= bio->bi_size) {
1538 req->bio = bio->bi_next;
1539 nbytes = bio->bi_size;
NeilBrown5bb23a62007-09-27 12:46:13 +02001540 req_bio_endio(req, bio, nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 next_idx = 0;
1542 bio_nbytes = 0;
1543 } else {
1544 int idx = bio->bi_idx + next_idx;
1545
1546 if (unlikely(bio->bi_idx >= bio->bi_vcnt)) {
1547 blk_dump_rq_flags(req, "__end_that");
Jens Axboe6728cb02008-01-31 13:03:55 +01001548 printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
1549 __FUNCTION__, bio->bi_idx,
1550 bio->bi_vcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 break;
1552 }
1553
1554 nbytes = bio_iovec_idx(bio, idx)->bv_len;
1555 BIO_BUG_ON(nbytes > bio->bi_size);
1556
1557 /*
1558 * not a complete bvec done
1559 */
1560 if (unlikely(nbytes > nr_bytes)) {
1561 bio_nbytes += nr_bytes;
1562 total_bytes += nr_bytes;
1563 break;
1564 }
1565
1566 /*
1567 * advance to the next vector
1568 */
1569 next_idx++;
1570 bio_nbytes += nbytes;
1571 }
1572
1573 total_bytes += nbytes;
1574 nr_bytes -= nbytes;
1575
Jens Axboe6728cb02008-01-31 13:03:55 +01001576 bio = req->bio;
1577 if (bio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 /*
1579 * end more in this run, or just return 'not-done'
1580 */
1581 if (unlikely(nr_bytes <= 0))
1582 break;
1583 }
1584 }
1585
1586 /*
1587 * completely done
1588 */
1589 if (!req->bio)
1590 return 0;
1591
1592 /*
1593 * if the request wasn't completed, update state
1594 */
1595 if (bio_nbytes) {
NeilBrown5bb23a62007-09-27 12:46:13 +02001596 req_bio_endio(req, bio, bio_nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 bio->bi_idx += next_idx;
1598 bio_iovec(bio)->bv_offset += nr_bytes;
1599 bio_iovec(bio)->bv_len -= nr_bytes;
1600 }
1601
1602 blk_recalc_rq_sectors(req, total_bytes >> 9);
1603 blk_recalc_rq_segments(req);
1604 return 1;
1605}
1606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607/*
Jens Axboeff856ba2006-01-09 16:02:34 +01001608 * splice the completion data to a local structure and hand off to
1609 * process_completion_queue() to complete the requests
1610 */
1611static void blk_done_softirq(struct softirq_action *h)
1612{
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07001613 struct list_head *cpu_list, local_list;
Jens Axboeff856ba2006-01-09 16:02:34 +01001614
1615 local_irq_disable();
1616 cpu_list = &__get_cpu_var(blk_cpu_done);
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07001617 list_replace_init(cpu_list, &local_list);
Jens Axboeff856ba2006-01-09 16:02:34 +01001618 local_irq_enable();
1619
1620 while (!list_empty(&local_list)) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001621 struct request *rq;
Jens Axboeff856ba2006-01-09 16:02:34 +01001622
Jens Axboe6728cb02008-01-31 13:03:55 +01001623 rq = list_entry(local_list.next, struct request, donelist);
Jens Axboeff856ba2006-01-09 16:02:34 +01001624 list_del_init(&rq->donelist);
1625 rq->q->softirq_done_fn(rq);
1626 }
1627}
1628
Jens Axboe6728cb02008-01-31 13:03:55 +01001629static int __cpuinit blk_cpu_notify(struct notifier_block *self,
1630 unsigned long action, void *hcpu)
Jens Axboeff856ba2006-01-09 16:02:34 +01001631{
1632 /*
1633 * If a CPU goes away, splice its entries to the current CPU
1634 * and trigger a run of the softirq
1635 */
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001636 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
Jens Axboeff856ba2006-01-09 16:02:34 +01001637 int cpu = (unsigned long) hcpu;
1638
1639 local_irq_disable();
1640 list_splice_init(&per_cpu(blk_cpu_done, cpu),
1641 &__get_cpu_var(blk_cpu_done));
1642 raise_softirq_irqoff(BLOCK_SOFTIRQ);
1643 local_irq_enable();
1644 }
1645
1646 return NOTIFY_OK;
1647}
1648
1649
Satyam Sharmadb47d472007-08-23 09:29:40 +02001650static struct notifier_block blk_cpu_notifier __cpuinitdata = {
Jens Axboeff856ba2006-01-09 16:02:34 +01001651 .notifier_call = blk_cpu_notify,
1652};
1653
Jens Axboeff856ba2006-01-09 16:02:34 +01001654/**
1655 * blk_complete_request - end I/O on a request
1656 * @req: the request being processed
1657 *
1658 * Description:
1659 * Ends all I/O on a request. It does not handle partial completions,
Andreas Mohrd6e05ed2006-06-26 18:35:02 +02001660 * unless the driver actually implements this in its completion callback
Jens Axboe4fa253f2007-07-18 13:13:10 +02001661 * through requeueing. The actual completion happens out-of-order,
Jens Axboeff856ba2006-01-09 16:02:34 +01001662 * through a softirq handler. The user must have registered a completion
1663 * callback through blk_queue_softirq_done().
1664 **/
1665
1666void blk_complete_request(struct request *req)
1667{
1668 struct list_head *cpu_list;
1669 unsigned long flags;
1670
1671 BUG_ON(!req->q->softirq_done_fn);
Jens Axboe6728cb02008-01-31 13:03:55 +01001672
Jens Axboeff856ba2006-01-09 16:02:34 +01001673 local_irq_save(flags);
1674
1675 cpu_list = &__get_cpu_var(blk_cpu_done);
1676 list_add_tail(&req->donelist, cpu_list);
1677 raise_softirq_irqoff(BLOCK_SOFTIRQ);
1678
1679 local_irq_restore(flags);
1680}
Jens Axboeff856ba2006-01-09 16:02:34 +01001681EXPORT_SYMBOL(blk_complete_request);
Jens Axboe6728cb02008-01-31 13:03:55 +01001682
Jens Axboeff856ba2006-01-09 16:02:34 +01001683/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 * queue lock must be held
1685 */
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001686static void end_that_request_last(struct request *req, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
1688 struct gendisk *disk = req->rq_disk;
Tejun Heo8ffdc652006-01-06 09:49:03 +01001689
Kiyoshi Uedab8286232007-12-11 17:53:24 -05001690 if (blk_rq_tagged(req))
1691 blk_queue_end_tag(req->q, req);
1692
1693 if (blk_queued_rq(req))
1694 blkdev_dequeue_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
1696 if (unlikely(laptop_mode) && blk_fs_request(req))
1697 laptop_io_completion();
1698
Jens Axboefd0ff8a2006-05-23 11:23:49 +02001699 /*
1700 * Account IO completion. bar_rq isn't accounted as a normal
1701 * IO on queueing nor completion. Accounting the containing
1702 * request is enough.
1703 */
1704 if (disk && blk_fs_request(req) && req != &req->q->bar_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 unsigned long duration = jiffies - req->start_time;
Jens Axboea3623572005-11-01 09:26:16 +01001706 const int rw = rq_data_dir(req);
1707
1708 __disk_stat_inc(disk, ios[rw]);
1709 __disk_stat_add(disk, ticks[rw], duration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 disk_round_stats(disk);
1711 disk->in_flight--;
1712 }
Kiyoshi Uedab8286232007-12-11 17:53:24 -05001713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 if (req->end_io)
Tejun Heo8ffdc652006-01-06 09:49:03 +01001715 req->end_io(req, error);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05001716 else {
1717 if (blk_bidi_rq(req))
1718 __blk_put_request(req->next_rq->q, req->next_rq);
1719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 __blk_put_request(req->q, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 }
1722}
1723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724static inline void __end_request(struct request *rq, int uptodate,
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05001725 unsigned int nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05001727 int error = 0;
1728
1729 if (uptodate <= 0)
1730 error = uptodate ? uptodate : -EIO;
1731
1732 __blk_end_request(rq, error, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733}
1734
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05001735/**
1736 * blk_rq_bytes - Returns bytes left to complete in the entire request
1737 **/
1738unsigned int blk_rq_bytes(struct request *rq)
Jens Axboea0cd1282007-09-21 10:41:07 +02001739{
1740 if (blk_fs_request(rq))
1741 return rq->hard_nr_sectors << 9;
1742
1743 return rq->data_len;
1744}
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05001745EXPORT_SYMBOL_GPL(blk_rq_bytes);
1746
1747/**
1748 * blk_rq_cur_bytes - Returns bytes left to complete in the current segment
1749 **/
1750unsigned int blk_rq_cur_bytes(struct request *rq)
1751{
1752 if (blk_fs_request(rq))
1753 return rq->current_nr_sectors << 9;
1754
1755 if (rq->bio)
1756 return rq->bio->bi_size;
1757
1758 return rq->data_len;
1759}
1760EXPORT_SYMBOL_GPL(blk_rq_cur_bytes);
Jens Axboea0cd1282007-09-21 10:41:07 +02001761
1762/**
1763 * end_queued_request - end all I/O on a queued request
1764 * @rq: the request being processed
1765 * @uptodate: error value or 0/1 uptodate flag
1766 *
1767 * Description:
1768 * Ends all I/O on a request, and removes it from the block layer queues.
1769 * Not suitable for normal IO completion, unless the driver still has
1770 * the request attached to the block layer.
1771 *
1772 **/
1773void end_queued_request(struct request *rq, int uptodate)
1774{
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05001775 __end_request(rq, uptodate, blk_rq_bytes(rq));
Jens Axboea0cd1282007-09-21 10:41:07 +02001776}
1777EXPORT_SYMBOL(end_queued_request);
1778
1779/**
1780 * end_dequeued_request - end all I/O on a dequeued request
1781 * @rq: the request being processed
1782 * @uptodate: error value or 0/1 uptodate flag
1783 *
1784 * Description:
1785 * Ends all I/O on a request. The request must already have been
1786 * dequeued using blkdev_dequeue_request(), as is normally the case
1787 * for most drivers.
1788 *
1789 **/
1790void end_dequeued_request(struct request *rq, int uptodate)
1791{
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05001792 __end_request(rq, uptodate, blk_rq_bytes(rq));
Jens Axboea0cd1282007-09-21 10:41:07 +02001793}
1794EXPORT_SYMBOL(end_dequeued_request);
1795
1796
1797/**
1798 * end_request - end I/O on the current segment of the request
Randy Dunlap8f731f72007-10-18 23:39:28 -07001799 * @req: the request being processed
Jens Axboea0cd1282007-09-21 10:41:07 +02001800 * @uptodate: error value or 0/1 uptodate flag
1801 *
1802 * Description:
1803 * Ends I/O on the current segment of a request. If that is the only
1804 * remaining segment, the request is also completed and freed.
1805 *
1806 * This is a remnant of how older block drivers handled IO completions.
1807 * Modern drivers typically end IO on the full request in one go, unless
1808 * they have a residual value to account for. For that case this function
1809 * isn't really useful, unless the residual just happens to be the
1810 * full current segment. In other words, don't use this function in new
1811 * code. Either use end_request_completely(), or the
1812 * end_that_request_chunk() (along with end_that_request_last()) for
1813 * partial completions.
1814 *
1815 **/
1816void end_request(struct request *req, int uptodate)
1817{
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05001818 __end_request(req, uptodate, req->hard_cur_sectors << 9);
Jens Axboea0cd1282007-09-21 10:41:07 +02001819}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820EXPORT_SYMBOL(end_request);
1821
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001822/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001823 * blk_end_io - Generic end_io function to complete a request.
1824 * @rq: the request being processed
1825 * @error: 0 for success, < 0 for error
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001826 * @nr_bytes: number of bytes to complete @rq
1827 * @bidi_bytes: number of bytes to complete @rq->next_rq
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001828 * @drv_callback: function called between completion of bios in the request
1829 * and completion of the request.
1830 * If the callback returns non 0, this helper returns without
1831 * completion of the request.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001832 *
1833 * Description:
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001834 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001835 * If @rq has leftover, sets it up for the next range of segments.
1836 *
1837 * Return:
1838 * 0 - we are done with this request
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001839 * 1 - this request is not freed yet, it still has pending buffers.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001840 **/
Jens Axboe22b13212008-01-31 12:36:19 +01001841static int blk_end_io(struct request *rq, int error, unsigned int nr_bytes,
1842 unsigned int bidi_bytes,
1843 int (drv_callback)(struct request *))
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001844{
1845 struct request_queue *q = rq->q;
1846 unsigned long flags = 0UL;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001847
1848 if (blk_fs_request(rq) || blk_pc_request(rq)) {
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001849 if (__end_that_request_first(rq, error, nr_bytes))
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001850 return 1;
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001851
1852 /* Bidi request must be completed as a whole */
1853 if (blk_bidi_rq(rq) &&
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001854 __end_that_request_first(rq->next_rq, error, bidi_bytes))
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001855 return 1;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001856 }
1857
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001858 /* Special feature for tricky drivers */
1859 if (drv_callback && drv_callback(rq))
1860 return 1;
1861
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001862 add_disk_randomness(rq->rq_disk);
1863
1864 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05001865 end_that_request_last(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001866 spin_unlock_irqrestore(q->queue_lock, flags);
1867
1868 return 0;
1869}
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001870
1871/**
1872 * blk_end_request - Helper function for drivers to complete the request.
1873 * @rq: the request being processed
1874 * @error: 0 for success, < 0 for error
1875 * @nr_bytes: number of bytes to complete
1876 *
1877 * Description:
1878 * Ends I/O on a number of bytes attached to @rq.
1879 * If @rq has leftover, sets it up for the next range of segments.
1880 *
1881 * Return:
1882 * 0 - we are done with this request
1883 * 1 - still buffers pending for this request
1884 **/
Jens Axboe22b13212008-01-31 12:36:19 +01001885int blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001886{
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001887 return blk_end_io(rq, error, nr_bytes, 0, NULL);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001888}
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001889EXPORT_SYMBOL_GPL(blk_end_request);
1890
1891/**
1892 * __blk_end_request - Helper function for drivers to complete the request.
1893 * @rq: the request being processed
1894 * @error: 0 for success, < 0 for error
1895 * @nr_bytes: number of bytes to complete
1896 *
1897 * Description:
1898 * Must be called with queue lock held unlike blk_end_request().
1899 *
1900 * Return:
1901 * 0 - we are done with this request
1902 * 1 - still buffers pending for this request
1903 **/
Jens Axboe22b13212008-01-31 12:36:19 +01001904int __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001905{
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001906 if (blk_fs_request(rq) || blk_pc_request(rq)) {
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001907 if (__end_that_request_first(rq, error, nr_bytes))
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001908 return 1;
1909 }
1910
1911 add_disk_randomness(rq->rq_disk);
1912
Kiyoshi Uedab8286232007-12-11 17:53:24 -05001913 end_that_request_last(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001914
1915 return 0;
1916}
1917EXPORT_SYMBOL_GPL(__blk_end_request);
1918
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001919/**
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001920 * blk_end_bidi_request - Helper function for drivers to complete bidi request.
1921 * @rq: the bidi request being processed
1922 * @error: 0 for success, < 0 for error
1923 * @nr_bytes: number of bytes to complete @rq
1924 * @bidi_bytes: number of bytes to complete @rq->next_rq
1925 *
1926 * Description:
1927 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
1928 *
1929 * Return:
1930 * 0 - we are done with this request
1931 * 1 - still buffers pending for this request
1932 **/
Jens Axboe22b13212008-01-31 12:36:19 +01001933int blk_end_bidi_request(struct request *rq, int error, unsigned int nr_bytes,
1934 unsigned int bidi_bytes)
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001935{
1936 return blk_end_io(rq, error, nr_bytes, bidi_bytes, NULL);
1937}
1938EXPORT_SYMBOL_GPL(blk_end_bidi_request);
1939
1940/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001941 * blk_end_request_callback - Special helper function for tricky drivers
1942 * @rq: the request being processed
1943 * @error: 0 for success, < 0 for error
1944 * @nr_bytes: number of bytes to complete
1945 * @drv_callback: function called between completion of bios in the request
1946 * and completion of the request.
1947 * If the callback returns non 0, this helper returns without
1948 * completion of the request.
1949 *
1950 * Description:
1951 * Ends I/O on a number of bytes attached to @rq.
1952 * If @rq has leftover, sets it up for the next range of segments.
1953 *
1954 * This special helper function is used only for existing tricky drivers.
1955 * (e.g. cdrom_newpc_intr() of ide-cd)
1956 * This interface will be removed when such drivers are rewritten.
1957 * Don't use this interface in other places anymore.
1958 *
1959 * Return:
1960 * 0 - we are done with this request
1961 * 1 - this request is not freed yet.
1962 * this request still has pending buffers or
1963 * the driver doesn't want to finish this request yet.
1964 **/
Jens Axboe22b13212008-01-31 12:36:19 +01001965int blk_end_request_callback(struct request *rq, int error,
1966 unsigned int nr_bytes,
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001967 int (drv_callback)(struct request *))
1968{
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001969 return blk_end_io(rq, error, nr_bytes, 0, drv_callback);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001970}
1971EXPORT_SYMBOL_GPL(blk_end_request_callback);
1972
Jens Axboe86db1e22008-01-29 14:53:40 +01001973void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
1974 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975{
Jens Axboe4aff5e22006-08-10 08:44:47 +02001976 /* first two bits are identical in rq->cmd_flags and bio->bi_rw */
1977 rq->cmd_flags |= (bio->bi_rw & 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979 rq->nr_phys_segments = bio_phys_segments(q, bio);
1980 rq->nr_hw_segments = bio_hw_segments(q, bio);
1981 rq->current_nr_sectors = bio_cur_sectors(bio);
1982 rq->hard_cur_sectors = rq->current_nr_sectors;
1983 rq->hard_nr_sectors = rq->nr_sectors = bio_sectors(bio);
1984 rq->buffer = bio_data(bio);
Mike Christie0e75f902006-12-01 10:40:55 +01001985 rq->data_len = bio->bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
1987 rq->bio = rq->biotail = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
NeilBrown66846572007-08-16 13:31:28 +02001989 if (bio->bi_bdev)
1990 rq->rq_disk = bio->bi_bdev->bd_disk;
1991}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
1993int kblockd_schedule_work(struct work_struct *work)
1994{
1995 return queue_work(kblockd_workqueue, work);
1996}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997EXPORT_SYMBOL(kblockd_schedule_work);
1998
Andrew Morton19a75d82007-05-09 02:33:56 -07001999void kblockd_flush_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002001 cancel_work_sync(work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002}
Andrew Morton19a75d82007-05-09 02:33:56 -07002003EXPORT_SYMBOL(kblockd_flush_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
2005int __init blk_dev_init(void)
2006{
Jens Axboeff856ba2006-01-09 16:02:34 +01002007 int i;
2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 kblockd_workqueue = create_workqueue("kblockd");
2010 if (!kblockd_workqueue)
2011 panic("Failed to create kblockd\n");
2012
2013 request_cachep = kmem_cache_create("blkdev_requests",
Paul Mundt20c2df82007-07-20 10:11:58 +09002014 sizeof(struct request), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Jens Axboe8324aa92008-01-29 14:51:59 +01002016 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02002017 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002019 for_each_possible_cpu(i)
Jens Axboeff856ba2006-01-09 16:02:34 +01002020 INIT_LIST_HEAD(&per_cpu(blk_cpu_done, i));
2021
2022 open_softirq(BLOCK_SOFTIRQ, blk_done_softirq, NULL);
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07002023 register_hotcpu_notifier(&blk_cpu_notifier);
Jens Axboeff856ba2006-01-09 16:02:34 +01002024
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 return 0;
2026}
2027