blob: e447799256d6c0abeb460d61f10e15b6e80030e4 [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 */
Adrian Bunk5ece6c52008-02-18 13:45:51 +010041static struct kmem_cache *request_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/*
44 * For queue allocation
45 */
Jens Axboe6728cb02008-01-31 13:03:55 +010046struct kmem_cache *blk_requestq_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * Controlling structure to kblockd
50 */
Jens Axboeff856ba2006-01-09 16:02:34 +010051static struct workqueue_struct *kblockd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Jens Axboeff856ba2006-01-09 16:02:34 +010053static DEFINE_PER_CPU(struct list_head, blk_cpu_done);
54
Jens Axboe26b82562008-01-29 13:54:41 +010055static void drive_stat_acct(struct request *rq, int new_io)
56{
57 int rw = rq_data_dir(rq);
58
59 if (!blk_fs_request(rq) || !rq->rq_disk)
60 return;
61
62 if (!new_io) {
Jerome Marchand6f2576af2008-02-08 11:04:35 +010063 __all_stat_inc(rq->rq_disk, merges[rw], rq->sector);
Jens Axboe26b82562008-01-29 13:54:41 +010064 } else {
Jerome Marchand6f2576af2008-02-08 11:04:35 +010065 struct hd_struct *part = get_part(rq->rq_disk, rq->sector);
Jens Axboe26b82562008-01-29 13:54:41 +010066 disk_round_stats(rq->rq_disk);
67 rq->rq_disk->in_flight++;
Jerome Marchand6f2576af2008-02-08 11:04:35 +010068 if (part) {
69 part_round_stats(part);
70 part->in_flight++;
71 }
Jens Axboe26b82562008-01-29 13:54:41 +010072 }
73}
74
Jens Axboe8324aa92008-01-29 14:51:59 +010075void blk_queue_congestion_threshold(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
77 int nr;
78
79 nr = q->nr_requests - (q->nr_requests / 8) + 1;
80 if (nr > q->nr_requests)
81 nr = q->nr_requests;
82 q->nr_congestion_on = nr;
83
84 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
85 if (nr < 1)
86 nr = 1;
87 q->nr_congestion_off = nr;
88}
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/**
91 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
92 * @bdev: device
93 *
94 * Locates the passed device's request queue and returns the address of its
95 * backing_dev_info
96 *
97 * Will return NULL if the request queue cannot be located.
98 */
99struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
100{
101 struct backing_dev_info *ret = NULL;
Jens Axboe165125e2007-07-24 09:28:11 +0200102 struct request_queue *q = bdev_get_queue(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 if (q)
105 ret = &q->backing_dev_info;
106 return ret;
107}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108EXPORT_SYMBOL(blk_get_backing_dev_info);
109
Jens Axboe86db1e22008-01-29 14:53:40 +0100110void rq_init(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200112 memset(rq, 0, sizeof(*rq));
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 INIT_LIST_HEAD(&rq->queuelist);
Jens Axboeff856ba2006-01-09 16:02:34 +0100115 INIT_LIST_HEAD(&rq->donelist);
Jens Axboe63a71382008-02-08 12:41:03 +0100116 rq->q = q;
117 rq->sector = rq->hard_sector = (sector_t) -1;
Jens Axboe2e662b62006-07-13 11:55:04 +0200118 INIT_HLIST_NODE(&rq->hash);
119 RB_CLEAR_NODE(&rq->rb_node);
Jens Axboe63a71382008-02-08 12:41:03 +0100120 rq->tag = -1;
Jens Axboe63a71382008-02-08 12:41:03 +0100121 rq->ref_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
NeilBrown5bb23a62007-09-27 12:46:13 +0200124static void req_bio_endio(struct request *rq, struct bio *bio,
125 unsigned int nbytes, int error)
Tejun Heo797e7db2006-01-06 09:51:03 +0100126{
Jens Axboe165125e2007-07-24 09:28:11 +0200127 struct request_queue *q = rq->q;
Tejun Heo797e7db2006-01-06 09:51:03 +0100128
NeilBrown5bb23a62007-09-27 12:46:13 +0200129 if (&q->bar_rq != rq) {
130 if (error)
131 clear_bit(BIO_UPTODATE, &bio->bi_flags);
132 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
133 error = -EIO;
Tejun Heo797e7db2006-01-06 09:51:03 +0100134
NeilBrown5bb23a62007-09-27 12:46:13 +0200135 if (unlikely(nbytes > bio->bi_size)) {
Jens Axboe6728cb02008-01-31 13:03:55 +0100136 printk(KERN_ERR "%s: want %u bytes done, %u left\n",
NeilBrown5bb23a62007-09-27 12:46:13 +0200137 __FUNCTION__, nbytes, bio->bi_size);
138 nbytes = bio->bi_size;
139 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100140
NeilBrown5bb23a62007-09-27 12:46:13 +0200141 bio->bi_size -= nbytes;
142 bio->bi_sector += (nbytes >> 9);
143 if (bio->bi_size == 0)
NeilBrown6712ecf2007-09-27 12:47:43 +0200144 bio_endio(bio, error);
NeilBrown5bb23a62007-09-27 12:46:13 +0200145 } else {
146
147 /*
148 * Okay, this is the barrier request in progress, just
149 * record the error;
150 */
151 if (error && !q->orderr)
152 q->orderr = error;
153 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100154}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156void blk_dump_rq_flags(struct request *rq, char *msg)
157{
158 int bit;
159
Jens Axboe6728cb02008-01-31 13:03:55 +0100160 printk(KERN_INFO "%s: dev %s: type=%x, flags=%x\n", msg,
Jens Axboe4aff5e22006-08-10 08:44:47 +0200161 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
162 rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Jens Axboe6728cb02008-01-31 13:03:55 +0100164 printk(KERN_INFO " sector %llu, nr/cnr %lu/%u\n",
165 (unsigned long long)rq->sector,
166 rq->nr_sectors,
167 rq->current_nr_sectors);
168 printk(KERN_INFO " bio %p, biotail %p, buffer %p, data %p, len %u\n",
169 rq->bio, rq->biotail,
170 rq->buffer, rq->data,
171 rq->data_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Jens Axboe4aff5e22006-08-10 08:44:47 +0200173 if (blk_pc_request(rq)) {
Jens Axboe6728cb02008-01-31 13:03:55 +0100174 printk(KERN_INFO " cdb: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 for (bit = 0; bit < sizeof(rq->cmd); bit++)
176 printk("%02x ", rq->cmd[bit]);
177 printk("\n");
178 }
179}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180EXPORT_SYMBOL(blk_dump_rq_flags);
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/*
183 * "plug" the device if there are no outstanding requests: this will
184 * force the transfer to start only after we have put all the requests
185 * on the list.
186 *
187 * This is called with interrupts off and no requests on the queue and
188 * with the queue lock held.
189 */
Jens Axboe165125e2007-07-24 09:28:11 +0200190void blk_plug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 WARN_ON(!irqs_disabled());
193
194 /*
195 * don't plug a stopped queue, it must be paired with blk_start_queue()
196 * which will restart the queueing
197 */
Coywolf Qi Hunt7daac492006-04-19 10:14:49 +0200198 if (blk_queue_stopped(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return;
200
Jens Axboe2056a782006-03-23 20:00:26 +0100201 if (!test_and_set_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
Jens Axboe2056a782006-03-23 20:00:26 +0100203 blk_add_trace_generic(q, NULL, 0, BLK_TA_PLUG);
204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206EXPORT_SYMBOL(blk_plug_device);
207
208/*
209 * remove the queue from the plugged list, if present. called with
210 * queue lock held and interrupts disabled.
211 */
Jens Axboe165125e2007-07-24 09:28:11 +0200212int blk_remove_plug(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 WARN_ON(!irqs_disabled());
215
216 if (!test_and_clear_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags))
217 return 0;
218
219 del_timer(&q->unplug_timer);
220 return 1;
221}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222EXPORT_SYMBOL(blk_remove_plug);
223
224/*
225 * remove the plug and let it rip..
226 */
Jens Axboe165125e2007-07-24 09:28:11 +0200227void __generic_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Coywolf Qi Hunt7daac492006-04-19 10:14:49 +0200229 if (unlikely(blk_queue_stopped(q)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return;
231
232 if (!blk_remove_plug(q))
233 return;
234
Jens Axboe22e2c502005-06-27 10:55:12 +0200235 q->request_fn(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237EXPORT_SYMBOL(__generic_unplug_device);
238
239/**
240 * generic_unplug_device - fire a request queue
Jens Axboe165125e2007-07-24 09:28:11 +0200241 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 *
243 * Description:
244 * Linux uses plugging to build bigger requests queues before letting
245 * the device have at them. If a queue is plugged, the I/O scheduler
246 * is still adding and merging requests on the queue. Once the queue
247 * gets unplugged, the request_fn defined for the queue is invoked and
248 * transfers started.
249 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200250void generic_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
252 spin_lock_irq(q->queue_lock);
253 __generic_unplug_device(q);
254 spin_unlock_irq(q->queue_lock);
255}
256EXPORT_SYMBOL(generic_unplug_device);
257
258static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
259 struct page *page)
260{
Jens Axboe165125e2007-07-24 09:28:11 +0200261 struct request_queue *q = bdi->unplug_io_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500263 blk_unplug(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
Jens Axboe86db1e22008-01-29 14:53:40 +0100266void blk_unplug_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Jens Axboe165125e2007-07-24 09:28:11 +0200268 struct request_queue *q =
269 container_of(work, struct request_queue, unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Jens Axboe2056a782006-03-23 20:00:26 +0100271 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
272 q->rq.count[READ] + q->rq.count[WRITE]);
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 q->unplug_fn(q);
275}
276
Jens Axboe86db1e22008-01-29 14:53:40 +0100277void blk_unplug_timeout(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Jens Axboe165125e2007-07-24 09:28:11 +0200279 struct request_queue *q = (struct request_queue *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Jens Axboe2056a782006-03-23 20:00:26 +0100281 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_TIMER, NULL,
282 q->rq.count[READ] + q->rq.count[WRITE]);
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 kblockd_schedule_work(&q->unplug_work);
285}
286
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500287void blk_unplug(struct request_queue *q)
288{
289 /*
290 * devices don't necessarily have an ->unplug_fn defined
291 */
292 if (q->unplug_fn) {
293 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
294 q->rq.count[READ] + q->rq.count[WRITE]);
295
296 q->unplug_fn(q);
297 }
298}
299EXPORT_SYMBOL(blk_unplug);
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301/**
302 * blk_start_queue - restart a previously stopped queue
Jens Axboe165125e2007-07-24 09:28:11 +0200303 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 *
305 * Description:
306 * blk_start_queue() will clear the stop flag on the queue, and call
307 * the request_fn for the queue if it was in a stopped state when
308 * entered. Also see blk_stop_queue(). Queue lock must be held.
309 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200310void blk_start_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200312 WARN_ON(!irqs_disabled());
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 clear_bit(QUEUE_FLAG_STOPPED, &q->queue_flags);
315
316 /*
317 * one level of recursion is ok and is much faster than kicking
318 * the unplug handling
319 */
320 if (!test_and_set_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) {
321 q->request_fn(q);
322 clear_bit(QUEUE_FLAG_REENTER, &q->queue_flags);
323 } else {
324 blk_plug_device(q);
325 kblockd_schedule_work(&q->unplug_work);
326 }
327}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328EXPORT_SYMBOL(blk_start_queue);
329
330/**
331 * blk_stop_queue - stop a queue
Jens Axboe165125e2007-07-24 09:28:11 +0200332 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 *
334 * Description:
335 * The Linux block layer assumes that a block driver will consume all
336 * entries on the request queue when the request_fn strategy is called.
337 * Often this will not happen, because of hardware limitations (queue
338 * depth settings). If a device driver gets a 'queue full' response,
339 * or if it simply chooses not to queue more I/O at one point, it can
340 * call this function to prevent the request_fn from being called until
341 * the driver has signalled it's ready to go again. This happens by calling
342 * blk_start_queue() to restart queue operations. Queue lock must be held.
343 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200344void blk_stop_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 blk_remove_plug(q);
347 set_bit(QUEUE_FLAG_STOPPED, &q->queue_flags);
348}
349EXPORT_SYMBOL(blk_stop_queue);
350
351/**
352 * blk_sync_queue - cancel any pending callbacks on a queue
353 * @q: the queue
354 *
355 * Description:
356 * The block layer may perform asynchronous callback activity
357 * on a queue, such as calling the unplug function after a timeout.
358 * A block device may call blk_sync_queue to ensure that any
359 * such activity is cancelled, thus allowing it to release resources
Michael Opdenacker59c51592007-05-09 08:57:56 +0200360 * that the callbacks might use. The caller must already have made sure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 * that its ->make_request_fn will not re-add plugging prior to calling
362 * this function.
363 *
364 */
365void blk_sync_queue(struct request_queue *q)
366{
367 del_timer_sync(&q->unplug_timer);
Oleg Nesterovabbeb882007-10-23 15:08:19 +0200368 kblockd_flush_work(&q->unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370EXPORT_SYMBOL(blk_sync_queue);
371
372/**
373 * blk_run_queue - run a single device queue
374 * @q: The queue to run
375 */
376void blk_run_queue(struct request_queue *q)
377{
378 unsigned long flags;
379
380 spin_lock_irqsave(q->queue_lock, flags);
381 blk_remove_plug(q);
Jens Axboedac07ec2006-05-11 08:20:16 +0200382
383 /*
384 * Only recurse once to avoid overrunning the stack, let the unplug
385 * handling reinvoke the handler shortly if we already got there.
386 */
387 if (!elv_queue_empty(q)) {
388 if (!test_and_set_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) {
389 q->request_fn(q);
390 clear_bit(QUEUE_FLAG_REENTER, &q->queue_flags);
391 } else {
392 blk_plug_device(q);
393 kblockd_schedule_work(&q->unplug_work);
394 }
395 }
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 spin_unlock_irqrestore(q->queue_lock, flags);
398}
399EXPORT_SYMBOL(blk_run_queue);
400
Jens Axboe165125e2007-07-24 09:28:11 +0200401void blk_put_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500402{
403 kobject_put(&q->kobj);
404}
Al Viro483f4af2006-03-18 18:34:37 -0500405
Jens Axboe6728cb02008-01-31 13:03:55 +0100406void blk_cleanup_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500407{
408 mutex_lock(&q->sysfs_lock);
409 set_bit(QUEUE_FLAG_DEAD, &q->queue_flags);
410 mutex_unlock(&q->sysfs_lock);
411
412 if (q->elevator)
413 elevator_exit(q->elevator);
414
415 blk_put_queue(q);
416}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417EXPORT_SYMBOL(blk_cleanup_queue);
418
Jens Axboe165125e2007-07-24 09:28:11 +0200419static int blk_init_free_list(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 struct request_list *rl = &q->rq;
422
423 rl->count[READ] = rl->count[WRITE] = 0;
424 rl->starved[READ] = rl->starved[WRITE] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200425 rl->elvpriv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 init_waitqueue_head(&rl->wait[READ]);
427 init_waitqueue_head(&rl->wait[WRITE]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Christoph Lameter19460892005-06-23 00:08:19 -0700429 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
430 mempool_free_slab, request_cachep, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 if (!rl->rq_pool)
433 return -ENOMEM;
434
435 return 0;
436}
437
Jens Axboe165125e2007-07-24 09:28:11 +0200438struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Christoph Lameter19460892005-06-23 00:08:19 -0700440 return blk_alloc_queue_node(gfp_mask, -1);
441}
442EXPORT_SYMBOL(blk_alloc_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Jens Axboe165125e2007-07-24 09:28:11 +0200444struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -0700445{
Jens Axboe165125e2007-07-24 09:28:11 +0200446 struct request_queue *q;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700447 int err;
Christoph Lameter19460892005-06-23 00:08:19 -0700448
Jens Axboe8324aa92008-01-29 14:51:59 +0100449 q = kmem_cache_alloc_node(blk_requestq_cachep,
Christoph Lameter94f60302007-07-17 04:03:29 -0700450 gfp_mask | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (!q)
452 return NULL;
453
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700454 q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
455 q->backing_dev_info.unplug_io_data = q;
456 err = bdi_init(&q->backing_dev_info);
457 if (err) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100458 kmem_cache_free(blk_requestq_cachep, q);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700459 return NULL;
460 }
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 init_timer(&q->unplug_timer);
Al Viro483f4af2006-03-18 18:34:37 -0500463
Jens Axboe8324aa92008-01-29 14:51:59 +0100464 kobject_init(&q->kobj, &blk_queue_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Al Viro483f4af2006-03-18 18:34:37 -0500466 mutex_init(&q->sysfs_lock);
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return q;
469}
Christoph Lameter19460892005-06-23 00:08:19 -0700470EXPORT_SYMBOL(blk_alloc_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472/**
473 * blk_init_queue - prepare a request queue for use with a block device
474 * @rfn: The function to be called to process requests that have been
475 * placed on the queue.
476 * @lock: Request queue spin lock
477 *
478 * Description:
479 * If a block device wishes to use the standard request handling procedures,
480 * which sorts requests and coalesces adjacent requests, then it must
481 * call blk_init_queue(). The function @rfn will be called when there
482 * are requests on the queue that need to be processed. If the device
483 * supports plugging, then @rfn may not be called immediately when requests
484 * are available on the queue, but may be called at some time later instead.
485 * Plugged queues are generally unplugged when a buffer belonging to one
486 * of the requests on the queue is needed, or due to memory pressure.
487 *
488 * @rfn is not required, or even expected, to remove all requests off the
489 * queue, but only as many as it can handle at a time. If it does leave
490 * requests on the queue, it is responsible for arranging that the requests
491 * get dealt with eventually.
492 *
493 * The queue spin lock must be held while manipulating the requests on the
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200494 * request queue; this lock will be taken also from interrupt context, so irq
495 * disabling is needed for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 *
497 * Function returns a pointer to the initialized request queue, or NULL if
498 * it didn't succeed.
499 *
500 * Note:
501 * blk_init_queue() must be paired with a blk_cleanup_queue() call
502 * when the block device is deactivated (such as at module unload).
503 **/
Christoph Lameter19460892005-06-23 00:08:19 -0700504
Jens Axboe165125e2007-07-24 09:28:11 +0200505struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Christoph Lameter19460892005-06-23 00:08:19 -0700507 return blk_init_queue_node(rfn, lock, -1);
508}
509EXPORT_SYMBOL(blk_init_queue);
510
Jens Axboe165125e2007-07-24 09:28:11 +0200511struct request_queue *
Christoph Lameter19460892005-06-23 00:08:19 -0700512blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
513{
Jens Axboe165125e2007-07-24 09:28:11 +0200514 struct request_queue *q = blk_alloc_queue_node(GFP_KERNEL, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 if (!q)
517 return NULL;
518
Christoph Lameter19460892005-06-23 00:08:19 -0700519 q->node = node_id;
Al Viro8669aaf2006-03-18 13:50:00 -0500520 if (blk_init_free_list(q)) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100521 kmem_cache_free(blk_requestq_cachep, q);
Al Viro8669aaf2006-03-18 13:50:00 -0500522 return NULL;
523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
152587d2005-04-12 16:22:06 -0500525 /*
526 * if caller didn't supply a lock, they get per-queue locking with
527 * our embedded lock
528 */
529 if (!lock) {
530 spin_lock_init(&q->__queue_lock);
531 lock = &q->__queue_lock;
532 }
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 q->request_fn = rfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 q->prep_rq_fn = NULL;
536 q->unplug_fn = generic_unplug_device;
537 q->queue_flags = (1 << QUEUE_FLAG_CLUSTER);
538 q->queue_lock = lock;
539
540 blk_queue_segment_boundary(q, 0xffffffff);
541
542 blk_queue_make_request(q, __make_request);
543 blk_queue_max_segment_size(q, MAX_SEGMENT_SIZE);
544
545 blk_queue_max_hw_segments(q, MAX_HW_SEGMENTS);
546 blk_queue_max_phys_segments(q, MAX_PHYS_SEGMENTS);
547
Alan Stern44ec9542007-02-20 11:01:57 -0500548 q->sg_reserved_size = INT_MAX;
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 /*
551 * all done
552 */
553 if (!elevator_init(q, NULL)) {
554 blk_queue_congestion_threshold(q);
555 return q;
556 }
557
Al Viro8669aaf2006-03-18 13:50:00 -0500558 blk_put_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return NULL;
560}
Christoph Lameter19460892005-06-23 00:08:19 -0700561EXPORT_SYMBOL(blk_init_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Jens Axboe165125e2007-07-24 09:28:11 +0200563int blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Nick Pigginfde6ad22005-06-23 00:08:53 -0700565 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
Al Viro483f4af2006-03-18 18:34:37 -0500566 kobject_get(&q->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return 0;
568 }
569
570 return 1;
571}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Jens Axboe165125e2007-07-24 09:28:11 +0200573static inline void blk_free_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Jens Axboe4aff5e22006-08-10 08:44:47 +0200575 if (rq->cmd_flags & REQ_ELVPRIV)
Tejun Heocb98fc82005-10-28 08:29:39 +0200576 elv_put_request(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 mempool_free(rq, q->rq.rq_pool);
578}
579
Jens Axboe1ea25ecb2006-07-18 22:24:11 +0200580static struct request *
Jens Axboe165125e2007-07-24 09:28:11 +0200581blk_alloc_request(struct request_queue *q, int rw, int priv, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
584
585 if (!rq)
586 return NULL;
587
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200588 rq_init(q, rq);
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 /*
Jens Axboe4aff5e22006-08-10 08:44:47 +0200591 * first three bits are identical in rq->cmd_flags and bio->bi_rw,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 * see bio.h and blkdev.h
593 */
Jens Axboe49171e52006-08-10 08:59:11 +0200594 rq->cmd_flags = rw | REQ_ALLOCED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Tejun Heocb98fc82005-10-28 08:29:39 +0200596 if (priv) {
Jens Axboecb78b282006-07-28 09:32:57 +0200597 if (unlikely(elv_set_request(q, rq, gfp_mask))) {
Tejun Heocb98fc82005-10-28 08:29:39 +0200598 mempool_free(rq, q->rq.rq_pool);
599 return NULL;
600 }
Jens Axboe4aff5e22006-08-10 08:44:47 +0200601 rq->cmd_flags |= REQ_ELVPRIV;
Tejun Heocb98fc82005-10-28 08:29:39 +0200602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Tejun Heocb98fc82005-10-28 08:29:39 +0200604 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
607/*
608 * ioc_batching returns true if the ioc is a valid batching request and
609 * should be given priority access to a request.
610 */
Jens Axboe165125e2007-07-24 09:28:11 +0200611static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
613 if (!ioc)
614 return 0;
615
616 /*
617 * Make sure the process is able to allocate at least 1 request
618 * even if the batch times out, otherwise we could theoretically
619 * lose wakeups.
620 */
621 return ioc->nr_batch_requests == q->nr_batching ||
622 (ioc->nr_batch_requests > 0
623 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
624}
625
626/*
627 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
628 * will cause the process to be a "batcher" on all queues in the system. This
629 * is the behaviour we want though - once it gets a wakeup it should be given
630 * a nice run.
631 */
Jens Axboe165125e2007-07-24 09:28:11 +0200632static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
634 if (!ioc || ioc_batching(q, ioc))
635 return;
636
637 ioc->nr_batch_requests = q->nr_batching;
638 ioc->last_waited = jiffies;
639}
640
Jens Axboe165125e2007-07-24 09:28:11 +0200641static void __freed_request(struct request_queue *q, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
643 struct request_list *rl = &q->rq;
644
645 if (rl->count[rw] < queue_congestion_off_threshold(q))
Thomas Maier79e2de42006-10-19 23:28:15 -0700646 blk_clear_queue_congested(q, rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 if (rl->count[rw] + 1 <= q->nr_requests) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (waitqueue_active(&rl->wait[rw]))
650 wake_up(&rl->wait[rw]);
651
652 blk_clear_queue_full(q, rw);
653 }
654}
655
656/*
657 * A request has just been released. Account for it, update the full and
658 * congestion status, wake up any waiters. Called under q->queue_lock.
659 */
Jens Axboe165125e2007-07-24 09:28:11 +0200660static void freed_request(struct request_queue *q, int rw, int priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
662 struct request_list *rl = &q->rq;
663
664 rl->count[rw]--;
Tejun Heocb98fc82005-10-28 08:29:39 +0200665 if (priv)
666 rl->elvpriv--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 __freed_request(q, rw);
669
670 if (unlikely(rl->starved[rw ^ 1]))
671 __freed_request(q, rw ^ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
674#define blkdev_free_rq(list) list_entry((list)->next, struct request, queuelist)
675/*
Nick Piggind6344532005-06-28 20:45:14 -0700676 * Get a free request, queue_lock must be held.
677 * Returns NULL on failure, with queue_lock held.
678 * Returns !NULL on success, with queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 */
Jens Axboe165125e2007-07-24 09:28:11 +0200680static struct request *get_request(struct request_queue *q, int rw_flags,
Jens Axboe7749a8d2006-12-13 13:02:26 +0100681 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
683 struct request *rq = NULL;
684 struct request_list *rl = &q->rq;
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100685 struct io_context *ioc = NULL;
Jens Axboe7749a8d2006-12-13 13:02:26 +0100686 const int rw = rw_flags & 0x01;
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100687 int may_queue, priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Jens Axboe7749a8d2006-12-13 13:02:26 +0100689 may_queue = elv_may_queue(q, rw_flags);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100690 if (may_queue == ELV_MQUEUE_NO)
691 goto rq_starved;
692
693 if (rl->count[rw]+1 >= queue_congestion_on_threshold(q)) {
694 if (rl->count[rw]+1 >= q->nr_requests) {
Jens Axboeb5deef92006-07-19 23:39:40 +0200695 ioc = current_io_context(GFP_ATOMIC, q->node);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100696 /*
697 * The queue will fill after this allocation, so set
698 * it as full, and mark this process as "batching".
699 * This process will be allowed to complete a batch of
700 * requests, others will be blocked.
701 */
702 if (!blk_queue_full(q, rw)) {
703 ioc_set_batching(q, ioc);
704 blk_set_queue_full(q, rw);
705 } else {
706 if (may_queue != ELV_MQUEUE_MUST
707 && !ioc_batching(q, ioc)) {
708 /*
709 * The queue is full and the allocating
710 * process is not a "batcher", and not
711 * exempted by the IO scheduler
712 */
713 goto out;
714 }
715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
Thomas Maier79e2de42006-10-19 23:28:15 -0700717 blk_set_queue_congested(q, rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
719
Jens Axboe082cf692005-06-28 16:35:11 +0200720 /*
721 * Only allow batching queuers to allocate up to 50% over the defined
722 * limit of requests, otherwise we could have thousands of requests
723 * allocated with any setting of ->nr_requests
724 */
Hugh Dickinsfd782a42005-06-29 15:15:40 +0100725 if (rl->count[rw] >= (3 * q->nr_requests / 2))
Jens Axboe082cf692005-06-28 16:35:11 +0200726 goto out;
Hugh Dickinsfd782a42005-06-29 15:15:40 +0100727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 rl->count[rw]++;
729 rl->starved[rw] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200730
Jens Axboe64521d12005-10-28 08:30:39 +0200731 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Tejun Heocb98fc82005-10-28 08:29:39 +0200732 if (priv)
733 rl->elvpriv++;
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 spin_unlock_irq(q->queue_lock);
736
Jens Axboe7749a8d2006-12-13 13:02:26 +0100737 rq = blk_alloc_request(q, rw_flags, priv, gfp_mask);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100738 if (unlikely(!rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 /*
740 * Allocation failed presumably due to memory. Undo anything
741 * we might have messed up.
742 *
743 * Allocating task should really be put onto the front of the
744 * wait queue, but this is pretty rare.
745 */
746 spin_lock_irq(q->queue_lock);
Tejun Heocb98fc82005-10-28 08:29:39 +0200747 freed_request(q, rw, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 /*
750 * in the very unlikely event that allocation failed and no
751 * requests for this direction was pending, mark us starved
752 * so that freeing of a request in the other direction will
753 * notice us. another possible fix would be to split the
754 * rq mempool into READ and WRITE
755 */
756rq_starved:
757 if (unlikely(rl->count[rw] == 0))
758 rl->starved[rw] = 1;
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 goto out;
761 }
762
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100763 /*
764 * ioc may be NULL here, and ioc_batching will be false. That's
765 * OK, if the queue is under the request limit then requests need
766 * not count toward the nr_batch_requests limit. There will always
767 * be some limit enforced by BLK_BATCH_TIME.
768 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 if (ioc_batching(q, ioc))
770 ioc->nr_batch_requests--;
Jens Axboe6728cb02008-01-31 13:03:55 +0100771
Jens Axboe2056a782006-03-23 20:00:26 +0100772 blk_add_trace_generic(q, bio, rw, BLK_TA_GETRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return rq;
775}
776
777/*
778 * No available requests for this queue, unplug the device and wait for some
779 * requests to become available.
Nick Piggind6344532005-06-28 20:45:14 -0700780 *
781 * Called with q->queue_lock held, and returns with it unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 */
Jens Axboe165125e2007-07-24 09:28:11 +0200783static struct request *get_request_wait(struct request_queue *q, int rw_flags,
Jens Axboe22e2c502005-06-27 10:55:12 +0200784 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
Jens Axboe7749a8d2006-12-13 13:02:26 +0100786 const int rw = rw_flags & 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 struct request *rq;
788
Jens Axboe7749a8d2006-12-13 13:02:26 +0100789 rq = get_request(q, rw_flags, bio, GFP_NOIO);
Nick Piggin450991b2005-06-28 20:45:13 -0700790 while (!rq) {
791 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 struct request_list *rl = &q->rq;
793
794 prepare_to_wait_exclusive(&rl->wait[rw], &wait,
795 TASK_UNINTERRUPTIBLE);
796
Jens Axboe7749a8d2006-12-13 13:02:26 +0100797 rq = get_request(q, rw_flags, bio, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 if (!rq) {
800 struct io_context *ioc;
801
Jens Axboe2056a782006-03-23 20:00:26 +0100802 blk_add_trace_generic(q, bio, rw, BLK_TA_SLEEPRQ);
803
Nick Piggind6344532005-06-28 20:45:14 -0700804 __generic_unplug_device(q);
805 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 io_schedule();
807
808 /*
809 * After sleeping, we become a "batching" process and
810 * will be able to allocate at least one request, and
811 * up to a big batch of them for a small period time.
812 * See ioc_batching, ioc_set_batching
813 */
Jens Axboeb5deef92006-07-19 23:39:40 +0200814 ioc = current_io_context(GFP_NOIO, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 ioc_set_batching(q, ioc);
Nick Piggind6344532005-06-28 20:45:14 -0700816
817 spin_lock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
819 finish_wait(&rl->wait[rw], &wait);
Nick Piggin450991b2005-06-28 20:45:13 -0700820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 return rq;
823}
824
Jens Axboe165125e2007-07-24 09:28:11 +0200825struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
827 struct request *rq;
828
829 BUG_ON(rw != READ && rw != WRITE);
830
Nick Piggind6344532005-06-28 20:45:14 -0700831 spin_lock_irq(q->queue_lock);
832 if (gfp_mask & __GFP_WAIT) {
Jens Axboe22e2c502005-06-27 10:55:12 +0200833 rq = get_request_wait(q, rw, NULL);
Nick Piggind6344532005-06-28 20:45:14 -0700834 } else {
Jens Axboe22e2c502005-06-27 10:55:12 +0200835 rq = get_request(q, rw, NULL, gfp_mask);
Nick Piggind6344532005-06-28 20:45:14 -0700836 if (!rq)
837 spin_unlock_irq(q->queue_lock);
838 }
839 /* q->queue_lock is unlocked at this point */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 return rq;
842}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843EXPORT_SYMBOL(blk_get_request);
844
845/**
Jens Axboedc72ef42006-07-20 14:54:05 +0200846 * blk_start_queueing - initiate dispatch of requests to device
847 * @q: request queue to kick into gear
848 *
849 * This is basically a helper to remove the need to know whether a queue
850 * is plugged or not if someone just wants to initiate dispatch of requests
851 * for this queue.
852 *
853 * The queue lock must be held with interrupts disabled.
854 */
Jens Axboe165125e2007-07-24 09:28:11 +0200855void blk_start_queueing(struct request_queue *q)
Jens Axboedc72ef42006-07-20 14:54:05 +0200856{
857 if (!blk_queue_plugged(q))
858 q->request_fn(q);
859 else
860 __generic_unplug_device(q);
861}
862EXPORT_SYMBOL(blk_start_queueing);
863
864/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 * blk_requeue_request - put a request back on queue
866 * @q: request queue where request should be inserted
867 * @rq: request to be inserted
868 *
869 * Description:
870 * Drivers often keep queueing requests until the hardware cannot accept
871 * more, when that condition happens we need to put the request back
872 * on the queue. Must be called with queue lock held.
873 */
Jens Axboe165125e2007-07-24 09:28:11 +0200874void blk_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
Jens Axboe2056a782006-03-23 20:00:26 +0100876 blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (blk_rq_tagged(rq))
879 blk_queue_end_tag(q, rq);
880
881 elv_requeue_request(q, rq);
882}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883EXPORT_SYMBOL(blk_requeue_request);
884
885/**
886 * blk_insert_request - insert a special request in to a request queue
887 * @q: request queue where request should be inserted
888 * @rq: request to be inserted
889 * @at_head: insert request at head or tail of queue
890 * @data: private data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 *
892 * Description:
893 * Many block devices need to execute commands asynchronously, so they don't
894 * block the whole kernel from preemption during request execution. This is
895 * accomplished normally by inserting aritficial requests tagged as
896 * REQ_SPECIAL in to the corresponding request queue, and letting them be
897 * scheduled for actual execution by the request queue.
898 *
899 * We have the option of inserting the head or the tail of the queue.
900 * Typically we use the tail for new ioctls and so forth. We use the head
901 * of the queue for things like a QUEUE_FULL message from a device, or a
902 * host that is unable to accept a particular command.
903 */
Jens Axboe165125e2007-07-24 09:28:11 +0200904void blk_insert_request(struct request_queue *q, struct request *rq,
Tejun Heo 867d1192005-04-24 02:06:05 -0500905 int at_head, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Tejun Heo 867d1192005-04-24 02:06:05 -0500907 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 unsigned long flags;
909
910 /*
911 * tell I/O scheduler that this isn't a regular read/write (ie it
912 * must not attempt merges on this) and that it acts as a soft
913 * barrier
914 */
Jens Axboe4aff5e22006-08-10 08:44:47 +0200915 rq->cmd_type = REQ_TYPE_SPECIAL;
916 rq->cmd_flags |= REQ_SOFTBARRIER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 rq->special = data;
919
920 spin_lock_irqsave(q->queue_lock, flags);
921
922 /*
923 * If command is tagged, release the tag
924 */
Tejun Heo 867d1192005-04-24 02:06:05 -0500925 if (blk_rq_tagged(rq))
926 blk_queue_end_tag(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Jerome Marchandb238b3d2007-10-23 15:05:46 +0200928 drive_stat_acct(rq, 1);
Tejun Heo 867d1192005-04-24 02:06:05 -0500929 __elv_add_request(q, rq, where, 0);
Jens Axboedc72ef42006-07-20 14:54:05 +0200930 blk_start_queueing(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 spin_unlock_irqrestore(q->queue_lock, flags);
932}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933EXPORT_SYMBOL(blk_insert_request);
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935/*
936 * add-request adds a request to the linked list.
937 * queue lock is held and interrupts disabled, as we muck with the
938 * request queue list.
939 */
Jens Axboe6728cb02008-01-31 13:03:55 +0100940static inline void add_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Jerome Marchandb238b3d2007-10-23 15:05:46 +0200942 drive_stat_acct(req, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 /*
945 * elevator indicated where it wants this request to be
946 * inserted at elevator_merge time
947 */
948 __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
949}
Jens Axboe6728cb02008-01-31 13:03:55 +0100950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951/*
952 * disk_round_stats() - Round off the performance stats on a struct
953 * disk_stats.
954 *
955 * The average IO queue length and utilisation statistics are maintained
956 * by observing the current state of the queue length and the amount of
957 * time it has been in this state for.
958 *
959 * Normally, that accounting is done on IO completion, but that can result
960 * in more than a second's worth of IO being accounted for within any one
961 * second, leading to >100% utilisation. To deal with that, we call this
962 * function to do a round-off before returning the results when reading
963 * /proc/diskstats. This accounts immediately for all queue usage up to
964 * the current jiffies and restarts the counters again.
965 */
966void disk_round_stats(struct gendisk *disk)
967{
968 unsigned long now = jiffies;
969
Chen, Kenneth Wb2982642005-10-13 21:49:29 +0200970 if (now == disk->stamp)
971 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Chen, Kenneth W20e5c812005-10-13 21:48:42 +0200973 if (disk->in_flight) {
974 __disk_stat_add(disk, time_in_queue,
975 disk->in_flight * (now - disk->stamp));
976 __disk_stat_add(disk, io_ticks, (now - disk->stamp));
977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 disk->stamp = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -0800980EXPORT_SYMBOL_GPL(disk_round_stats);
981
Jerome Marchand6f2576af2008-02-08 11:04:35 +0100982void part_round_stats(struct hd_struct *part)
983{
984 unsigned long now = jiffies;
985
986 if (now == part->stamp)
987 return;
988
989 if (part->in_flight) {
990 __part_stat_add(part, time_in_queue,
991 part->in_flight * (now - part->stamp));
992 __part_stat_add(part, io_ticks, (now - part->stamp));
993 }
994 part->stamp = now;
995}
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997/*
998 * queue lock must be held
999 */
Jens Axboe165125e2007-07-24 09:28:11 +02001000void __blk_put_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 if (unlikely(!q))
1003 return;
1004 if (unlikely(--req->ref_count))
1005 return;
1006
Tejun Heo8922e162005-10-20 16:23:44 +02001007 elv_completed_request(q, req);
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 /*
1010 * Request may not have originated from ll_rw_blk. if not,
1011 * it didn't come out of our reserved rq pools
1012 */
Jens Axboe49171e52006-08-10 08:59:11 +02001013 if (req->cmd_flags & REQ_ALLOCED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 int rw = rq_data_dir(req);
Jens Axboe4aff5e22006-08-10 08:44:47 +02001015 int priv = req->cmd_flags & REQ_ELVPRIV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 BUG_ON(!list_empty(&req->queuelist));
Jens Axboe98170642006-07-28 09:23:08 +02001018 BUG_ON(!hlist_unhashed(&req->hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 blk_free_request(q, req);
Tejun Heocb98fc82005-10-28 08:29:39 +02001021 freed_request(q, rw, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 }
1023}
Mike Christie6e39b692005-11-11 05:30:24 -06001024EXPORT_SYMBOL_GPL(__blk_put_request);
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026void blk_put_request(struct request *req)
1027{
Tejun Heo8922e162005-10-20 16:23:44 +02001028 unsigned long flags;
Jens Axboe165125e2007-07-24 09:28:11 +02001029 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Tejun Heo8922e162005-10-20 16:23:44 +02001031 /*
1032 * Gee, IDE calls in w/ NULL q. Fix IDE and remove the
1033 * following if (q) test.
1034 */
1035 if (q) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 spin_lock_irqsave(q->queue_lock, flags);
1037 __blk_put_request(q, req);
1038 spin_unlock_irqrestore(q->queue_lock, flags);
1039 }
1040}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041EXPORT_SYMBOL(blk_put_request);
1042
Jens Axboe86db1e22008-01-29 14:53:40 +01001043void init_request_from_bio(struct request *req, struct bio *bio)
Tejun Heo52d9e672006-01-06 09:49:58 +01001044{
Jens Axboe4aff5e22006-08-10 08:44:47 +02001045 req->cmd_type = REQ_TYPE_FS;
Tejun Heo52d9e672006-01-06 09:49:58 +01001046
1047 /*
1048 * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
1049 */
1050 if (bio_rw_ahead(bio) || bio_failfast(bio))
Jens Axboe4aff5e22006-08-10 08:44:47 +02001051 req->cmd_flags |= REQ_FAILFAST;
Tejun Heo52d9e672006-01-06 09:49:58 +01001052
1053 /*
1054 * REQ_BARRIER implies no merging, but lets make it explicit
1055 */
1056 if (unlikely(bio_barrier(bio)))
Jens Axboe4aff5e22006-08-10 08:44:47 +02001057 req->cmd_flags |= (REQ_HARDBARRIER | REQ_NOMERGE);
Tejun Heo52d9e672006-01-06 09:49:58 +01001058
Jens Axboeb31dc662006-06-13 08:26:10 +02001059 if (bio_sync(bio))
Jens Axboe4aff5e22006-08-10 08:44:47 +02001060 req->cmd_flags |= REQ_RW_SYNC;
Jens Axboe5404bc72006-08-10 09:01:02 +02001061 if (bio_rw_meta(bio))
1062 req->cmd_flags |= REQ_RW_META;
Jens Axboeb31dc662006-06-13 08:26:10 +02001063
Tejun Heo52d9e672006-01-06 09:49:58 +01001064 req->errors = 0;
1065 req->hard_sector = req->sector = bio->bi_sector;
Tejun Heo52d9e672006-01-06 09:49:58 +01001066 req->ioprio = bio_prio(bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001067 req->start_time = jiffies;
NeilBrownbc1c56f2007-08-16 13:31:30 +02001068 blk_rq_bio_prep(req->q, req, bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001069}
1070
Jens Axboe165125e2007-07-24 09:28:11 +02001071static int __make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
Nick Piggin450991b2005-06-28 20:45:13 -07001073 struct request *req;
Jens Axboe51da90f2006-07-18 04:14:45 +02001074 int el_ret, nr_sectors, barrier, err;
1075 const unsigned short prio = bio_prio(bio);
1076 const int sync = bio_sync(bio);
Jens Axboe7749a8d2006-12-13 13:02:26 +01001077 int rw_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 nr_sectors = bio_sectors(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 /*
1082 * low level driver can indicate that it wants pages above a
1083 * certain limit bounced to low memory (ie for highmem, or even
1084 * ISA dma in theory)
1085 */
1086 blk_queue_bounce(q, &bio);
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 barrier = bio_barrier(bio);
Tejun Heo797e7db2006-01-06 09:51:03 +01001089 if (unlikely(barrier) && (q->next_ordered == QUEUE_ORDERED_NONE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 err = -EOPNOTSUPP;
1091 goto end_io;
1092 }
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 spin_lock_irq(q->queue_lock);
1095
Nick Piggin450991b2005-06-28 20:45:13 -07001096 if (unlikely(barrier) || elv_queue_empty(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 goto get_rq;
1098
1099 el_ret = elv_merge(q, &req, bio);
1100 switch (el_ret) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001101 case ELEVATOR_BACK_MERGE:
1102 BUG_ON(!rq_mergeable(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Jens Axboe6728cb02008-01-31 13:03:55 +01001104 if (!ll_back_merge_fn(q, req, bio))
1105 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Jens Axboe6728cb02008-01-31 13:03:55 +01001107 blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
Jens Axboe2056a782006-03-23 20:00:26 +01001108
Jens Axboe6728cb02008-01-31 13:03:55 +01001109 req->biotail->bi_next = bio;
1110 req->biotail = bio;
1111 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
1112 req->ioprio = ioprio_best(req->ioprio, prio);
1113 drive_stat_acct(req, 0);
1114 if (!attempt_back_merge(q, req))
1115 elv_merged_request(q, req, el_ret);
1116 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Jens Axboe6728cb02008-01-31 13:03:55 +01001118 case ELEVATOR_FRONT_MERGE:
1119 BUG_ON(!rq_mergeable(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Jens Axboe6728cb02008-01-31 13:03:55 +01001121 if (!ll_front_merge_fn(q, req, bio))
1122 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Jens Axboe6728cb02008-01-31 13:03:55 +01001124 blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
Jens Axboe2056a782006-03-23 20:00:26 +01001125
Jens Axboe6728cb02008-01-31 13:03:55 +01001126 bio->bi_next = req->bio;
1127 req->bio = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Jens Axboe6728cb02008-01-31 13:03:55 +01001129 /*
1130 * may not be valid. if the low level driver said
1131 * it didn't need a bounce buffer then it better
1132 * not touch req->buffer either...
1133 */
1134 req->buffer = bio_data(bio);
1135 req->current_nr_sectors = bio_cur_sectors(bio);
1136 req->hard_cur_sectors = req->current_nr_sectors;
1137 req->sector = req->hard_sector = bio->bi_sector;
1138 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
1139 req->ioprio = ioprio_best(req->ioprio, prio);
1140 drive_stat_acct(req, 0);
1141 if (!attempt_front_merge(q, req))
1142 elv_merged_request(q, req, el_ret);
1143 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Jens Axboe6728cb02008-01-31 13:03:55 +01001145 /* ELV_NO_MERGE: elevator says don't/can't merge. */
1146 default:
1147 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
1149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150get_rq:
Nick Piggin450991b2005-06-28 20:45:13 -07001151 /*
Jens Axboe7749a8d2006-12-13 13:02:26 +01001152 * This sync check and mask will be re-done in init_request_from_bio(),
1153 * but we need to set it earlier to expose the sync flag to the
1154 * rq allocator and io schedulers.
1155 */
1156 rw_flags = bio_data_dir(bio);
1157 if (sync)
1158 rw_flags |= REQ_RW_SYNC;
1159
1160 /*
Nick Piggin450991b2005-06-28 20:45:13 -07001161 * Grab a free request. This is might sleep but can not fail.
Nick Piggind6344532005-06-28 20:45:14 -07001162 * Returns with the queue unlocked.
Nick Piggin450991b2005-06-28 20:45:13 -07001163 */
Jens Axboe7749a8d2006-12-13 13:02:26 +01001164 req = get_request_wait(q, rw_flags, bio);
Nick Piggind6344532005-06-28 20:45:14 -07001165
Nick Piggin450991b2005-06-28 20:45:13 -07001166 /*
1167 * After dropping the lock and possibly sleeping here, our request
1168 * may now be mergeable after it had proven unmergeable (above).
1169 * We don't worry about that case for efficiency. It won't happen
1170 * often, and the elevators are able to handle it.
1171 */
Tejun Heo52d9e672006-01-06 09:49:58 +01001172 init_request_from_bio(req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Nick Piggin450991b2005-06-28 20:45:13 -07001174 spin_lock_irq(q->queue_lock);
1175 if (elv_queue_empty(q))
1176 blk_plug_device(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 add_request(q, req);
1178out:
Jens Axboe4a534f92005-04-16 15:25:40 -07001179 if (sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 __generic_unplug_device(q);
1181
1182 spin_unlock_irq(q->queue_lock);
1183 return 0;
1184
1185end_io:
NeilBrown6712ecf2007-09-27 12:47:43 +02001186 bio_endio(bio, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return 0;
1188}
1189
1190/*
1191 * If bio->bi_dev is a partition, remap the location
1192 */
1193static inline void blk_partition_remap(struct bio *bio)
1194{
1195 struct block_device *bdev = bio->bi_bdev;
1196
Jens Axboebf2de6f2007-09-27 13:01:25 +02001197 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 struct hd_struct *p = bdev->bd_part;
Jens Axboea3623572005-11-01 09:26:16 +01001199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 bio->bi_sector += p->start_sect;
1201 bio->bi_bdev = bdev->bd_contains;
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001202
1203 blk_add_trace_remap(bdev_get_queue(bio->bi_bdev), bio,
1204 bdev->bd_dev, bio->bi_sector,
1205 bio->bi_sector - p->start_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 }
1207}
1208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209static void handle_bad_sector(struct bio *bio)
1210{
1211 char b[BDEVNAME_SIZE];
1212
1213 printk(KERN_INFO "attempt to access beyond end of device\n");
1214 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1215 bdevname(bio->bi_bdev, b),
1216 bio->bi_rw,
1217 (unsigned long long)bio->bi_sector + bio_sectors(bio),
1218 (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
1219
1220 set_bit(BIO_EOF, &bio->bi_flags);
1221}
1222
Akinobu Mitac17bb492006-12-08 02:39:46 -08001223#ifdef CONFIG_FAIL_MAKE_REQUEST
1224
1225static DECLARE_FAULT_ATTR(fail_make_request);
1226
1227static int __init setup_fail_make_request(char *str)
1228{
1229 return setup_fault_attr(&fail_make_request, str);
1230}
1231__setup("fail_make_request=", setup_fail_make_request);
1232
1233static int should_fail_request(struct bio *bio)
1234{
1235 if ((bio->bi_bdev->bd_disk->flags & GENHD_FL_FAIL) ||
1236 (bio->bi_bdev->bd_part && bio->bi_bdev->bd_part->make_it_fail))
1237 return should_fail(&fail_make_request, bio->bi_size);
1238
1239 return 0;
1240}
1241
1242static int __init fail_make_request_debugfs(void)
1243{
1244 return init_fault_attr_dentries(&fail_make_request,
1245 "fail_make_request");
1246}
1247
1248late_initcall(fail_make_request_debugfs);
1249
1250#else /* CONFIG_FAIL_MAKE_REQUEST */
1251
1252static inline int should_fail_request(struct bio *bio)
1253{
1254 return 0;
1255}
1256
1257#endif /* CONFIG_FAIL_MAKE_REQUEST */
1258
Jens Axboec07e2b42007-07-18 13:27:58 +02001259/*
1260 * Check whether this bio extends beyond the end of the device.
1261 */
1262static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1263{
1264 sector_t maxsector;
1265
1266 if (!nr_sectors)
1267 return 0;
1268
1269 /* Test device or partition size, when known. */
1270 maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
1271 if (maxsector) {
1272 sector_t sector = bio->bi_sector;
1273
1274 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1275 /*
1276 * This may well happen - the kernel calls bread()
1277 * without checking the size of the device, e.g., when
1278 * mounting a device.
1279 */
1280 handle_bad_sector(bio);
1281 return 1;
1282 }
1283 }
1284
1285 return 0;
1286}
1287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288/**
1289 * generic_make_request: hand a buffer to its device driver for I/O
1290 * @bio: The bio describing the location in memory and on the device.
1291 *
1292 * generic_make_request() is used to make I/O requests of block
1293 * devices. It is passed a &struct bio, which describes the I/O that needs
1294 * to be done.
1295 *
1296 * generic_make_request() does not return any status. The
1297 * success/failure status of the request, along with notification of
1298 * completion, is delivered asynchronously through the bio->bi_end_io
1299 * function described (one day) else where.
1300 *
1301 * The caller of generic_make_request must make sure that bi_io_vec
1302 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1303 * set to describe the device address, and the
1304 * bi_end_io and optionally bi_private are set to describe how
1305 * completion notification should be signaled.
1306 *
1307 * generic_make_request and the drivers it calls may use bi_next if this
1308 * bio happens to be merged with someone else, and may change bi_dev and
1309 * bi_sector for remaps as it sees fit. So the values of these fields
1310 * should NOT be depended on after the call to generic_make_request.
1311 */
Neil Brownd89d8792007-05-01 09:53:42 +02001312static inline void __generic_make_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
Jens Axboe165125e2007-07-24 09:28:11 +02001314 struct request_queue *q;
NeilBrown5ddfe962006-10-30 22:07:21 -08001315 sector_t old_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 int ret, nr_sectors = bio_sectors(bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001317 dev_t old_dev;
Jens Axboe51fd77b2007-11-02 08:49:08 +01001318 int err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Jens Axboec07e2b42007-07-18 13:27:58 +02001322 if (bio_check_eod(bio, nr_sectors))
1323 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 /*
1326 * Resolve the mapping until finished. (drivers are
1327 * still free to implement/resolve their own stacking
1328 * by explicitly returning 0)
1329 *
1330 * NOTE: we don't repeat the blk_size check for each new device.
1331 * Stacking drivers are expected to know what they are doing.
1332 */
NeilBrown5ddfe962006-10-30 22:07:21 -08001333 old_sector = -1;
Jens Axboe2056a782006-03-23 20:00:26 +01001334 old_dev = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 do {
1336 char b[BDEVNAME_SIZE];
1337
1338 q = bdev_get_queue(bio->bi_bdev);
1339 if (!q) {
1340 printk(KERN_ERR
1341 "generic_make_request: Trying to access "
1342 "nonexistent block-device %s (%Lu)\n",
1343 bdevname(bio->bi_bdev, b),
1344 (long long) bio->bi_sector);
1345end_io:
Jens Axboe51fd77b2007-11-02 08:49:08 +01001346 bio_endio(bio, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 break;
1348 }
1349
Jens Axboe4fa253f2007-07-18 13:13:10 +02001350 if (unlikely(nr_sectors > q->max_hw_sectors)) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001351 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 bdevname(bio->bi_bdev, b),
1353 bio_sectors(bio),
1354 q->max_hw_sectors);
1355 goto end_io;
1356 }
1357
Nick Pigginfde6ad22005-06-23 00:08:53 -07001358 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 goto end_io;
1360
Akinobu Mitac17bb492006-12-08 02:39:46 -08001361 if (should_fail_request(bio))
1362 goto end_io;
1363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 /*
1365 * If this device has partitions, remap block n
1366 * of partition p to block n+start(p) of the disk.
1367 */
1368 blk_partition_remap(bio);
1369
NeilBrown5ddfe962006-10-30 22:07:21 -08001370 if (old_sector != -1)
Jens Axboe4fa253f2007-07-18 13:13:10 +02001371 blk_add_trace_remap(q, bio, old_dev, bio->bi_sector,
NeilBrown5ddfe962006-10-30 22:07:21 -08001372 old_sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001373
1374 blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
1375
NeilBrown5ddfe962006-10-30 22:07:21 -08001376 old_sector = bio->bi_sector;
Jens Axboe2056a782006-03-23 20:00:26 +01001377 old_dev = bio->bi_bdev->bd_dev;
1378
Jens Axboec07e2b42007-07-18 13:27:58 +02001379 if (bio_check_eod(bio, nr_sectors))
1380 goto end_io;
Jens Axboe51fd77b2007-11-02 08:49:08 +01001381 if (bio_empty_barrier(bio) && !q->prepare_flush_fn) {
1382 err = -EOPNOTSUPP;
1383 goto end_io;
1384 }
NeilBrown5ddfe962006-10-30 22:07:21 -08001385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 ret = q->make_request_fn(q, bio);
1387 } while (ret);
1388}
1389
Neil Brownd89d8792007-05-01 09:53:42 +02001390/*
1391 * We only want one ->make_request_fn to be active at a time,
1392 * else stack usage with stacked devices could be a problem.
1393 * So use current->bio_{list,tail} to keep a list of requests
1394 * submited by a make_request_fn function.
1395 * current->bio_tail is also used as a flag to say if
1396 * generic_make_request is currently active in this task or not.
1397 * If it is NULL, then no make_request is active. If it is non-NULL,
1398 * then a make_request is active, and new requests should be added
1399 * at the tail
1400 */
1401void generic_make_request(struct bio *bio)
1402{
1403 if (current->bio_tail) {
1404 /* make_request is active */
1405 *(current->bio_tail) = bio;
1406 bio->bi_next = NULL;
1407 current->bio_tail = &bio->bi_next;
1408 return;
1409 }
1410 /* following loop may be a bit non-obvious, and so deserves some
1411 * explanation.
1412 * Before entering the loop, bio->bi_next is NULL (as all callers
1413 * ensure that) so we have a list with a single bio.
1414 * We pretend that we have just taken it off a longer list, so
1415 * we assign bio_list to the next (which is NULL) and bio_tail
1416 * to &bio_list, thus initialising the bio_list of new bios to be
1417 * added. __generic_make_request may indeed add some more bios
1418 * through a recursive call to generic_make_request. If it
1419 * did, we find a non-NULL value in bio_list and re-enter the loop
1420 * from the top. In this case we really did just take the bio
1421 * of the top of the list (no pretending) and so fixup bio_list and
1422 * bio_tail or bi_next, and call into __generic_make_request again.
1423 *
1424 * The loop was structured like this to make only one call to
1425 * __generic_make_request (which is important as it is large and
1426 * inlined) and to keep the structure simple.
1427 */
1428 BUG_ON(bio->bi_next);
1429 do {
1430 current->bio_list = bio->bi_next;
1431 if (bio->bi_next == NULL)
1432 current->bio_tail = &current->bio_list;
1433 else
1434 bio->bi_next = NULL;
1435 __generic_make_request(bio);
1436 bio = current->bio_list;
1437 } while (bio);
1438 current->bio_tail = NULL; /* deactivate */
1439}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440EXPORT_SYMBOL(generic_make_request);
1441
1442/**
1443 * submit_bio: submit a bio to the block device layer for I/O
1444 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1445 * @bio: The &struct bio which describes the I/O
1446 *
1447 * submit_bio() is very similar in purpose to generic_make_request(), and
1448 * uses that function to do most of the work. Both are fairly rough
1449 * interfaces, @bio must be presetup and ready for I/O.
1450 *
1451 */
1452void submit_bio(int rw, struct bio *bio)
1453{
1454 int count = bio_sectors(bio);
1455
Jens Axboe22e2c502005-06-27 10:55:12 +02001456 bio->bi_rw |= rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Jens Axboebf2de6f2007-09-27 13:01:25 +02001458 /*
1459 * If it's a regular read/write or a barrier with data attached,
1460 * go through the normal accounting stuff before submission.
1461 */
1462 if (!bio_empty_barrier(bio)) {
1463
1464 BIO_BUG_ON(!bio->bi_size);
1465 BIO_BUG_ON(!bio->bi_io_vec);
1466
1467 if (rw & WRITE) {
1468 count_vm_events(PGPGOUT, count);
1469 } else {
1470 task_io_account_read(bio->bi_size);
1471 count_vm_events(PGPGIN, count);
1472 }
1473
1474 if (unlikely(block_dump)) {
1475 char b[BDEVNAME_SIZE];
1476 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001477 current->comm, task_pid_nr(current),
Jens Axboebf2de6f2007-09-27 13:01:25 +02001478 (rw & WRITE) ? "WRITE" : "READ",
1479 (unsigned long long)bio->bi_sector,
Jens Axboe6728cb02008-01-31 13:03:55 +01001480 bdevname(bio->bi_bdev, b));
Jens Axboebf2de6f2007-09-27 13:01:25 +02001481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 }
1483
1484 generic_make_request(bio);
1485}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486EXPORT_SYMBOL(submit_bio);
1487
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001488/**
1489 * __end_that_request_first - end I/O on a request
1490 * @req: the request being processed
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001491 * @error: 0 for success, < 0 for error
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001492 * @nr_bytes: number of bytes to complete
1493 *
1494 * Description:
1495 * Ends I/O on a number of bytes attached to @req, and sets it up
1496 * for the next range of segments (if any) in the cluster.
1497 *
1498 * Return:
1499 * 0 - we are done with this request, call end_that_request_last()
1500 * 1 - still buffers pending for this request
1501 **/
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001502static int __end_that_request_first(struct request *req, int error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 int nr_bytes)
1504{
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001505 int total_bytes, bio_nbytes, next_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 struct bio *bio;
1507
Jens Axboe2056a782006-03-23 20:00:26 +01001508 blk_add_trace_rq(req->q, req, BLK_TA_COMPLETE);
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 * for a REQ_BLOCK_PC request, we want to carry any eventual
1512 * sense key with us all the way through
1513 */
1514 if (!blk_pc_request(req))
1515 req->errors = 0;
1516
Jens Axboe6728cb02008-01-31 13:03:55 +01001517 if (error && (blk_fs_request(req) && !(req->cmd_flags & REQ_QUIET))) {
1518 printk(KERN_ERR "end_request: I/O error, dev %s, sector %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 req->rq_disk ? req->rq_disk->disk_name : "?",
1520 (unsigned long long)req->sector);
1521 }
1522
Jens Axboed72d9042005-11-01 08:35:42 +01001523 if (blk_fs_request(req) && req->rq_disk) {
Jens Axboea3623572005-11-01 09:26:16 +01001524 const int rw = rq_data_dir(req);
1525
Jerome Marchand6f2576af2008-02-08 11:04:35 +01001526 all_stat_add(req->rq_disk, sectors[rw],
1527 nr_bytes >> 9, req->sector);
Jens Axboed72d9042005-11-01 08:35:42 +01001528 }
1529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 total_bytes = bio_nbytes = 0;
1531 while ((bio = req->bio) != NULL) {
1532 int nbytes;
1533
Jens Axboebf2de6f2007-09-27 13:01:25 +02001534 /*
1535 * For an empty barrier request, the low level driver must
1536 * store a potential error location in ->sector. We pass
1537 * that back up in ->bi_sector.
1538 */
1539 if (blk_empty_barrier(req))
1540 bio->bi_sector = req->sector;
1541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 if (nr_bytes >= bio->bi_size) {
1543 req->bio = bio->bi_next;
1544 nbytes = bio->bi_size;
NeilBrown5bb23a62007-09-27 12:46:13 +02001545 req_bio_endio(req, bio, nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 next_idx = 0;
1547 bio_nbytes = 0;
1548 } else {
1549 int idx = bio->bi_idx + next_idx;
1550
1551 if (unlikely(bio->bi_idx >= bio->bi_vcnt)) {
1552 blk_dump_rq_flags(req, "__end_that");
Jens Axboe6728cb02008-01-31 13:03:55 +01001553 printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
1554 __FUNCTION__, bio->bi_idx,
1555 bio->bi_vcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 break;
1557 }
1558
1559 nbytes = bio_iovec_idx(bio, idx)->bv_len;
1560 BIO_BUG_ON(nbytes > bio->bi_size);
1561
1562 /*
1563 * not a complete bvec done
1564 */
1565 if (unlikely(nbytes > nr_bytes)) {
1566 bio_nbytes += nr_bytes;
1567 total_bytes += nr_bytes;
1568 break;
1569 }
1570
1571 /*
1572 * advance to the next vector
1573 */
1574 next_idx++;
1575 bio_nbytes += nbytes;
1576 }
1577
1578 total_bytes += nbytes;
1579 nr_bytes -= nbytes;
1580
Jens Axboe6728cb02008-01-31 13:03:55 +01001581 bio = req->bio;
1582 if (bio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 /*
1584 * end more in this run, or just return 'not-done'
1585 */
1586 if (unlikely(nr_bytes <= 0))
1587 break;
1588 }
1589 }
1590
1591 /*
1592 * completely done
1593 */
1594 if (!req->bio)
1595 return 0;
1596
1597 /*
1598 * if the request wasn't completed, update state
1599 */
1600 if (bio_nbytes) {
NeilBrown5bb23a62007-09-27 12:46:13 +02001601 req_bio_endio(req, bio, bio_nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 bio->bi_idx += next_idx;
1603 bio_iovec(bio)->bv_offset += nr_bytes;
1604 bio_iovec(bio)->bv_len -= nr_bytes;
1605 }
1606
1607 blk_recalc_rq_sectors(req, total_bytes >> 9);
1608 blk_recalc_rq_segments(req);
1609 return 1;
1610}
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612/*
Jens Axboeff856ba2006-01-09 16:02:34 +01001613 * splice the completion data to a local structure and hand off to
1614 * process_completion_queue() to complete the requests
1615 */
1616static void blk_done_softirq(struct softirq_action *h)
1617{
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07001618 struct list_head *cpu_list, local_list;
Jens Axboeff856ba2006-01-09 16:02:34 +01001619
1620 local_irq_disable();
1621 cpu_list = &__get_cpu_var(blk_cpu_done);
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07001622 list_replace_init(cpu_list, &local_list);
Jens Axboeff856ba2006-01-09 16:02:34 +01001623 local_irq_enable();
1624
1625 while (!list_empty(&local_list)) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001626 struct request *rq;
Jens Axboeff856ba2006-01-09 16:02:34 +01001627
Jens Axboe6728cb02008-01-31 13:03:55 +01001628 rq = list_entry(local_list.next, struct request, donelist);
Jens Axboeff856ba2006-01-09 16:02:34 +01001629 list_del_init(&rq->donelist);
1630 rq->q->softirq_done_fn(rq);
1631 }
1632}
1633
Jens Axboe6728cb02008-01-31 13:03:55 +01001634static int __cpuinit blk_cpu_notify(struct notifier_block *self,
1635 unsigned long action, void *hcpu)
Jens Axboeff856ba2006-01-09 16:02:34 +01001636{
1637 /*
1638 * If a CPU goes away, splice its entries to the current CPU
1639 * and trigger a run of the softirq
1640 */
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001641 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
Jens Axboeff856ba2006-01-09 16:02:34 +01001642 int cpu = (unsigned long) hcpu;
1643
1644 local_irq_disable();
1645 list_splice_init(&per_cpu(blk_cpu_done, cpu),
1646 &__get_cpu_var(blk_cpu_done));
1647 raise_softirq_irqoff(BLOCK_SOFTIRQ);
1648 local_irq_enable();
1649 }
1650
1651 return NOTIFY_OK;
1652}
1653
1654
Satyam Sharmadb47d472007-08-23 09:29:40 +02001655static struct notifier_block blk_cpu_notifier __cpuinitdata = {
Jens Axboeff856ba2006-01-09 16:02:34 +01001656 .notifier_call = blk_cpu_notify,
1657};
1658
Jens Axboeff856ba2006-01-09 16:02:34 +01001659/**
1660 * blk_complete_request - end I/O on a request
1661 * @req: the request being processed
1662 *
1663 * Description:
1664 * Ends all I/O on a request. It does not handle partial completions,
Andreas Mohrd6e05ed2006-06-26 18:35:02 +02001665 * unless the driver actually implements this in its completion callback
Jens Axboe4fa253f2007-07-18 13:13:10 +02001666 * through requeueing. The actual completion happens out-of-order,
Jens Axboeff856ba2006-01-09 16:02:34 +01001667 * through a softirq handler. The user must have registered a completion
1668 * callback through blk_queue_softirq_done().
1669 **/
1670
1671void blk_complete_request(struct request *req)
1672{
1673 struct list_head *cpu_list;
1674 unsigned long flags;
1675
1676 BUG_ON(!req->q->softirq_done_fn);
Jens Axboe6728cb02008-01-31 13:03:55 +01001677
Jens Axboeff856ba2006-01-09 16:02:34 +01001678 local_irq_save(flags);
1679
1680 cpu_list = &__get_cpu_var(blk_cpu_done);
1681 list_add_tail(&req->donelist, cpu_list);
1682 raise_softirq_irqoff(BLOCK_SOFTIRQ);
1683
1684 local_irq_restore(flags);
1685}
Jens Axboeff856ba2006-01-09 16:02:34 +01001686EXPORT_SYMBOL(blk_complete_request);
Jens Axboe6728cb02008-01-31 13:03:55 +01001687
Jens Axboeff856ba2006-01-09 16:02:34 +01001688/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 * queue lock must be held
1690 */
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001691static void end_that_request_last(struct request *req, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
1693 struct gendisk *disk = req->rq_disk;
Tejun Heo8ffdc652006-01-06 09:49:03 +01001694
Kiyoshi Uedab8286232007-12-11 17:53:24 -05001695 if (blk_rq_tagged(req))
1696 blk_queue_end_tag(req->q, req);
1697
1698 if (blk_queued_rq(req))
1699 blkdev_dequeue_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
1701 if (unlikely(laptop_mode) && blk_fs_request(req))
1702 laptop_io_completion();
1703
Jens Axboefd0ff8a2006-05-23 11:23:49 +02001704 /*
1705 * Account IO completion. bar_rq isn't accounted as a normal
1706 * IO on queueing nor completion. Accounting the containing
1707 * request is enough.
1708 */
1709 if (disk && blk_fs_request(req) && req != &req->q->bar_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 unsigned long duration = jiffies - req->start_time;
Jens Axboea3623572005-11-01 09:26:16 +01001711 const int rw = rq_data_dir(req);
Jerome Marchand6f2576af2008-02-08 11:04:35 +01001712 struct hd_struct *part = get_part(disk, req->sector);
Jens Axboea3623572005-11-01 09:26:16 +01001713
Jerome Marchand6f2576af2008-02-08 11:04:35 +01001714 __all_stat_inc(disk, ios[rw], req->sector);
1715 __all_stat_add(disk, ticks[rw], duration, req->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 disk_round_stats(disk);
1717 disk->in_flight--;
Jerome Marchand6f2576af2008-02-08 11:04:35 +01001718 if (part) {
1719 part_round_stats(part);
1720 part->in_flight--;
1721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 }
Kiyoshi Uedab8286232007-12-11 17:53:24 -05001723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 if (req->end_io)
Tejun Heo8ffdc652006-01-06 09:49:03 +01001725 req->end_io(req, error);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05001726 else {
1727 if (blk_bidi_rq(req))
1728 __blk_put_request(req->next_rq->q, req->next_rq);
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 __blk_put_request(req->q, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 }
1732}
1733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734static inline void __end_request(struct request *rq, int uptodate,
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05001735 unsigned int nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736{
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05001737 int error = 0;
1738
1739 if (uptodate <= 0)
1740 error = uptodate ? uptodate : -EIO;
1741
1742 __blk_end_request(rq, error, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743}
1744
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05001745/**
1746 * blk_rq_bytes - Returns bytes left to complete in the entire request
Randy Dunlap5d87a052008-02-20 09:01:22 +01001747 * @rq: the request being processed
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05001748 **/
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
Randy Dunlap5d87a052008-02-20 09:01:22 +01001760 * @rq: the request being processed
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05001761 **/
1762unsigned int blk_rq_cur_bytes(struct request *rq)
1763{
1764 if (blk_fs_request(rq))
1765 return rq->current_nr_sectors << 9;
1766
1767 if (rq->bio)
1768 return rq->bio->bi_size;
1769
1770 return rq->data_len;
1771}
1772EXPORT_SYMBOL_GPL(blk_rq_cur_bytes);
Jens Axboea0cd1282007-09-21 10:41:07 +02001773
1774/**
1775 * end_queued_request - end all I/O on a queued request
1776 * @rq: the request being processed
1777 * @uptodate: error value or 0/1 uptodate flag
1778 *
1779 * Description:
1780 * Ends all I/O on a request, and removes it from the block layer queues.
1781 * Not suitable for normal IO completion, unless the driver still has
1782 * the request attached to the block layer.
1783 *
1784 **/
1785void end_queued_request(struct request *rq, int uptodate)
1786{
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05001787 __end_request(rq, uptodate, blk_rq_bytes(rq));
Jens Axboea0cd1282007-09-21 10:41:07 +02001788}
1789EXPORT_SYMBOL(end_queued_request);
1790
1791/**
1792 * end_dequeued_request - end all I/O on a dequeued request
1793 * @rq: the request being processed
1794 * @uptodate: error value or 0/1 uptodate flag
1795 *
1796 * Description:
1797 * Ends all I/O on a request. The request must already have been
1798 * dequeued using blkdev_dequeue_request(), as is normally the case
1799 * for most drivers.
1800 *
1801 **/
1802void end_dequeued_request(struct request *rq, int uptodate)
1803{
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05001804 __end_request(rq, uptodate, blk_rq_bytes(rq));
Jens Axboea0cd1282007-09-21 10:41:07 +02001805}
1806EXPORT_SYMBOL(end_dequeued_request);
1807
1808
1809/**
1810 * end_request - end I/O on the current segment of the request
Randy Dunlap8f731f72007-10-18 23:39:28 -07001811 * @req: the request being processed
Jens Axboea0cd1282007-09-21 10:41:07 +02001812 * @uptodate: error value or 0/1 uptodate flag
1813 *
1814 * Description:
1815 * Ends I/O on the current segment of a request. If that is the only
1816 * remaining segment, the request is also completed and freed.
1817 *
1818 * This is a remnant of how older block drivers handled IO completions.
1819 * Modern drivers typically end IO on the full request in one go, unless
1820 * they have a residual value to account for. For that case this function
1821 * isn't really useful, unless the residual just happens to be the
1822 * full current segment. In other words, don't use this function in new
1823 * code. Either use end_request_completely(), or the
1824 * end_that_request_chunk() (along with end_that_request_last()) for
1825 * partial completions.
1826 *
1827 **/
1828void end_request(struct request *req, int uptodate)
1829{
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05001830 __end_request(req, uptodate, req->hard_cur_sectors << 9);
Jens Axboea0cd1282007-09-21 10:41:07 +02001831}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832EXPORT_SYMBOL(end_request);
1833
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001834/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001835 * blk_end_io - Generic end_io function to complete a request.
1836 * @rq: the request being processed
1837 * @error: 0 for success, < 0 for error
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001838 * @nr_bytes: number of bytes to complete @rq
1839 * @bidi_bytes: number of bytes to complete @rq->next_rq
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001840 * @drv_callback: function called between completion of bios in the request
1841 * and completion of the request.
1842 * If the callback returns non 0, this helper returns without
1843 * completion of the request.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001844 *
1845 * Description:
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001846 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001847 * If @rq has leftover, sets it up for the next range of segments.
1848 *
1849 * Return:
1850 * 0 - we are done with this request
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001851 * 1 - this request is not freed yet, it still has pending buffers.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001852 **/
Jens Axboe22b13212008-01-31 12:36:19 +01001853static int blk_end_io(struct request *rq, int error, unsigned int nr_bytes,
1854 unsigned int bidi_bytes,
1855 int (drv_callback)(struct request *))
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001856{
1857 struct request_queue *q = rq->q;
1858 unsigned long flags = 0UL;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001859
1860 if (blk_fs_request(rq) || blk_pc_request(rq)) {
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001861 if (__end_that_request_first(rq, error, nr_bytes))
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001862 return 1;
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001863
1864 /* Bidi request must be completed as a whole */
1865 if (blk_bidi_rq(rq) &&
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001866 __end_that_request_first(rq->next_rq, error, bidi_bytes))
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001867 return 1;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001868 }
1869
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001870 /* Special feature for tricky drivers */
1871 if (drv_callback && drv_callback(rq))
1872 return 1;
1873
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001874 add_disk_randomness(rq->rq_disk);
1875
1876 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05001877 end_that_request_last(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001878 spin_unlock_irqrestore(q->queue_lock, flags);
1879
1880 return 0;
1881}
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001882
1883/**
1884 * blk_end_request - Helper function for drivers to complete the request.
1885 * @rq: the request being processed
1886 * @error: 0 for success, < 0 for error
1887 * @nr_bytes: number of bytes to complete
1888 *
1889 * Description:
1890 * Ends I/O on a number of bytes attached to @rq.
1891 * If @rq has leftover, sets it up for the next range of segments.
1892 *
1893 * Return:
1894 * 0 - we are done with this request
1895 * 1 - still buffers pending for this request
1896 **/
Jens Axboe22b13212008-01-31 12:36:19 +01001897int blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001898{
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001899 return blk_end_io(rq, error, nr_bytes, 0, NULL);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001900}
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001901EXPORT_SYMBOL_GPL(blk_end_request);
1902
1903/**
1904 * __blk_end_request - Helper function for drivers to complete the request.
1905 * @rq: the request being processed
1906 * @error: 0 for success, < 0 for error
1907 * @nr_bytes: number of bytes to complete
1908 *
1909 * Description:
1910 * Must be called with queue lock held unlike blk_end_request().
1911 *
1912 * Return:
1913 * 0 - we are done with this request
1914 * 1 - still buffers pending for this request
1915 **/
Jens Axboe22b13212008-01-31 12:36:19 +01001916int __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001917{
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001918 if (blk_fs_request(rq) || blk_pc_request(rq)) {
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001919 if (__end_that_request_first(rq, error, nr_bytes))
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001920 return 1;
1921 }
1922
1923 add_disk_randomness(rq->rq_disk);
1924
Kiyoshi Uedab8286232007-12-11 17:53:24 -05001925 end_that_request_last(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001926
1927 return 0;
1928}
1929EXPORT_SYMBOL_GPL(__blk_end_request);
1930
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001931/**
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001932 * blk_end_bidi_request - Helper function for drivers to complete bidi request.
1933 * @rq: the bidi request being processed
1934 * @error: 0 for success, < 0 for error
1935 * @nr_bytes: number of bytes to complete @rq
1936 * @bidi_bytes: number of bytes to complete @rq->next_rq
1937 *
1938 * Description:
1939 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
1940 *
1941 * Return:
1942 * 0 - we are done with this request
1943 * 1 - still buffers pending for this request
1944 **/
Jens Axboe22b13212008-01-31 12:36:19 +01001945int blk_end_bidi_request(struct request *rq, int error, unsigned int nr_bytes,
1946 unsigned int bidi_bytes)
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001947{
1948 return blk_end_io(rq, error, nr_bytes, bidi_bytes, NULL);
1949}
1950EXPORT_SYMBOL_GPL(blk_end_bidi_request);
1951
1952/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001953 * blk_end_request_callback - Special helper function for tricky drivers
1954 * @rq: the request being processed
1955 * @error: 0 for success, < 0 for error
1956 * @nr_bytes: number of bytes to complete
1957 * @drv_callback: function called between completion of bios in the request
1958 * and completion of the request.
1959 * If the callback returns non 0, this helper returns without
1960 * completion of the request.
1961 *
1962 * Description:
1963 * Ends I/O on a number of bytes attached to @rq.
1964 * If @rq has leftover, sets it up for the next range of segments.
1965 *
1966 * This special helper function is used only for existing tricky drivers.
1967 * (e.g. cdrom_newpc_intr() of ide-cd)
1968 * This interface will be removed when such drivers are rewritten.
1969 * Don't use this interface in other places anymore.
1970 *
1971 * Return:
1972 * 0 - we are done with this request
1973 * 1 - this request is not freed yet.
1974 * this request still has pending buffers or
1975 * the driver doesn't want to finish this request yet.
1976 **/
Jens Axboe22b13212008-01-31 12:36:19 +01001977int blk_end_request_callback(struct request *rq, int error,
1978 unsigned int nr_bytes,
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001979 int (drv_callback)(struct request *))
1980{
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001981 return blk_end_io(rq, error, nr_bytes, 0, drv_callback);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001982}
1983EXPORT_SYMBOL_GPL(blk_end_request_callback);
1984
Jens Axboe86db1e22008-01-29 14:53:40 +01001985void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
1986 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987{
Jens Axboe4aff5e22006-08-10 08:44:47 +02001988 /* first two bits are identical in rq->cmd_flags and bio->bi_rw */
1989 rq->cmd_flags |= (bio->bi_rw & 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
1991 rq->nr_phys_segments = bio_phys_segments(q, bio);
1992 rq->nr_hw_segments = bio_hw_segments(q, bio);
1993 rq->current_nr_sectors = bio_cur_sectors(bio);
1994 rq->hard_cur_sectors = rq->current_nr_sectors;
1995 rq->hard_nr_sectors = rq->nr_sectors = bio_sectors(bio);
1996 rq->buffer = bio_data(bio);
Mike Christie0e75f902006-12-01 10:40:55 +01001997 rq->data_len = bio->bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
1999 rq->bio = rq->biotail = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
NeilBrown66846572007-08-16 13:31:28 +02002001 if (bio->bi_bdev)
2002 rq->rq_disk = bio->bi_bdev->bd_disk;
2003}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
2005int kblockd_schedule_work(struct work_struct *work)
2006{
2007 return queue_work(kblockd_workqueue, work);
2008}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009EXPORT_SYMBOL(kblockd_schedule_work);
2010
Andrew Morton19a75d82007-05-09 02:33:56 -07002011void kblockd_flush_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012{
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002013 cancel_work_sync(work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014}
Andrew Morton19a75d82007-05-09 02:33:56 -07002015EXPORT_SYMBOL(kblockd_flush_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
2017int __init blk_dev_init(void)
2018{
Jens Axboeff856ba2006-01-09 16:02:34 +01002019 int i;
2020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 kblockd_workqueue = create_workqueue("kblockd");
2022 if (!kblockd_workqueue)
2023 panic("Failed to create kblockd\n");
2024
2025 request_cachep = kmem_cache_create("blkdev_requests",
Paul Mundt20c2df82007-07-20 10:11:58 +09002026 sizeof(struct request), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Jens Axboe8324aa92008-01-29 14:51:59 +01002028 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02002029 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002031 for_each_possible_cpu(i)
Jens Axboeff856ba2006-01-09 16:02:34 +01002032 INIT_LIST_HEAD(&per_cpu(blk_cpu_done, i));
2033
2034 open_softirq(BLOCK_SOFTIRQ, blk_done_softirq, NULL);
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07002035 register_hotcpu_notifier(&blk_cpu_notifier);
Jens Axboeff856ba2006-01-09 16:02:34 +01002036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 return 0;
2038}
2039