blob: e0a5ee36849c725e2a3bf6d5276fba040f358cee [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 1994, Karl Keyte: Added support for disk statistics
4 * Elevator latency, (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
5 * Queue request tables / lock, selectable elevator, Jens Axboe <axboe@suse.de>
Jens Axboe6728cb02008-01-31 13:03:55 +01006 * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au>
7 * - July2000
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
9 */
10
11/*
12 * This handles all read/write requests to block devices
13 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/backing-dev.h>
17#include <linux/bio.h>
18#include <linux/blkdev.h>
19#include <linux/highmem.h>
20#include <linux/mm.h>
21#include <linux/kernel_stat.h>
22#include <linux/string.h>
23#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/completion.h>
25#include <linux/slab.h>
26#include <linux/swap.h>
27#include <linux/writeback.h>
Andrew Mortonfaccbd42006-12-10 02:19:35 -080028#include <linux/task_io_accounting_ops.h>
Jens Axboeff856ba2006-01-09 16:02:34 +010029#include <linux/interrupt.h>
30#include <linux/cpu.h>
Jens Axboe2056a782006-03-23 20:00:26 +010031#include <linux/blktrace_api.h>
Akinobu Mitac17bb492006-12-08 02:39:46 -080032#include <linux/fault-inject.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Jens Axboe8324aa92008-01-29 14:51:59 +010034#include "blk.h"
35
Jens Axboe165125e2007-07-24 09:28:11 +020036static int __make_request(struct request_queue *q, struct bio *bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/*
39 * For the allocated request tables
40 */
Adrian Bunk5ece6c52008-02-18 13:45:51 +010041static struct kmem_cache *request_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/*
44 * For queue allocation
45 */
Jens Axboe6728cb02008-01-31 13:03:55 +010046struct kmem_cache *blk_requestq_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * Controlling structure to kblockd
50 */
Jens Axboeff856ba2006-01-09 16:02:34 +010051static struct workqueue_struct *kblockd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Jens Axboeff856ba2006-01-09 16:02:34 +010053static DEFINE_PER_CPU(struct list_head, blk_cpu_done);
54
Jens Axboe26b82562008-01-29 13:54:41 +010055static void drive_stat_acct(struct request *rq, int new_io)
56{
Jens Axboe28f13702008-05-07 10:15:46 +020057 struct hd_struct *part;
Jens Axboe26b82562008-01-29 13:54:41 +010058 int rw = rq_data_dir(rq);
Tejun Heoc9959052008-08-25 19:47:21 +090059 int cpu;
Jens Axboe26b82562008-01-29 13:54:41 +010060
61 if (!blk_fs_request(rq) || !rq->rq_disk)
62 return;
63
Tejun Heoc9959052008-08-25 19:47:21 +090064 cpu = disk_stat_lock();
Tejun Heoe71bf0d2008-09-03 09:03:02 +020065 part = disk_map_sector_rcu(rq->rq_disk, rq->sector);
Tejun Heoc9959052008-08-25 19:47:21 +090066
Jens Axboe28f13702008-05-07 10:15:46 +020067 if (!new_io)
Tejun Heoc9959052008-08-25 19:47:21 +090068 all_stat_inc(cpu, rq->rq_disk, part, merges[rw], rq->sector);
Jens Axboe28f13702008-05-07 10:15:46 +020069 else {
Tejun Heoc9959052008-08-25 19:47:21 +090070 disk_round_stats(cpu, rq->rq_disk);
Jens Axboe26b82562008-01-29 13:54:41 +010071 rq->rq_disk->in_flight++;
Jerome Marchand6f2576a2008-02-08 11:04:35 +010072 if (part) {
Tejun Heoc9959052008-08-25 19:47:21 +090073 part_round_stats(cpu, part);
Jerome Marchand6f2576a2008-02-08 11:04:35 +010074 part->in_flight++;
75 }
Jens Axboe26b82562008-01-29 13:54:41 +010076 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +020077
Tejun Heoc9959052008-08-25 19:47:21 +090078 disk_stat_unlock();
Jens Axboe26b82562008-01-29 13:54:41 +010079}
80
Jens Axboe8324aa92008-01-29 14:51:59 +010081void blk_queue_congestion_threshold(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
83 int nr;
84
85 nr = q->nr_requests - (q->nr_requests / 8) + 1;
86 if (nr > q->nr_requests)
87 nr = q->nr_requests;
88 q->nr_congestion_on = nr;
89
90 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
91 if (nr < 1)
92 nr = 1;
93 q->nr_congestion_off = nr;
94}
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096/**
97 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
98 * @bdev: device
99 *
100 * Locates the passed device's request queue and returns the address of its
101 * backing_dev_info
102 *
103 * Will return NULL if the request queue cannot be located.
104 */
105struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
106{
107 struct backing_dev_info *ret = NULL;
Jens Axboe165125e2007-07-24 09:28:11 +0200108 struct request_queue *q = bdev_get_queue(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 if (q)
111 ret = &q->backing_dev_info;
112 return ret;
113}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114EXPORT_SYMBOL(blk_get_backing_dev_info);
115
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200116void blk_rq_init(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200118 memset(rq, 0, sizeof(*rq));
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 INIT_LIST_HEAD(&rq->queuelist);
Jens Axboeff856ba2006-01-09 16:02:34 +0100121 INIT_LIST_HEAD(&rq->donelist);
Jens Axboe63a71382008-02-08 12:41:03 +0100122 rq->q = q;
123 rq->sector = rq->hard_sector = (sector_t) -1;
Jens Axboe2e662b62006-07-13 11:55:04 +0200124 INIT_HLIST_NODE(&rq->hash);
125 RB_CLEAR_NODE(&rq->rb_node);
FUJITA Tomonorid7e3c322008-04-29 09:54:39 +0200126 rq->cmd = rq->__cmd;
Jens Axboe63a71382008-02-08 12:41:03 +0100127 rq->tag = -1;
Jens Axboe63a71382008-02-08 12:41:03 +0100128 rq->ref_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200130EXPORT_SYMBOL(blk_rq_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
NeilBrown5bb23a62007-09-27 12:46:13 +0200132static void req_bio_endio(struct request *rq, struct bio *bio,
133 unsigned int nbytes, int error)
Tejun Heo797e7db2006-01-06 09:51:03 +0100134{
Jens Axboe165125e2007-07-24 09:28:11 +0200135 struct request_queue *q = rq->q;
Tejun Heo797e7db2006-01-06 09:51:03 +0100136
NeilBrown5bb23a62007-09-27 12:46:13 +0200137 if (&q->bar_rq != rq) {
138 if (error)
139 clear_bit(BIO_UPTODATE, &bio->bi_flags);
140 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
141 error = -EIO;
Tejun Heo797e7db2006-01-06 09:51:03 +0100142
NeilBrown5bb23a62007-09-27 12:46:13 +0200143 if (unlikely(nbytes > bio->bi_size)) {
Jens Axboe6728cb02008-01-31 13:03:55 +0100144 printk(KERN_ERR "%s: want %u bytes done, %u left\n",
Harvey Harrison24c03d42008-05-01 04:35:17 -0700145 __func__, nbytes, bio->bi_size);
NeilBrown5bb23a62007-09-27 12:46:13 +0200146 nbytes = bio->bi_size;
147 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100148
NeilBrown5bb23a62007-09-27 12:46:13 +0200149 bio->bi_size -= nbytes;
150 bio->bi_sector += (nbytes >> 9);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200151
152 if (bio_integrity(bio))
153 bio_integrity_advance(bio, nbytes);
154
NeilBrown5bb23a62007-09-27 12:46:13 +0200155 if (bio->bi_size == 0)
NeilBrown6712ecf2007-09-27 12:47:43 +0200156 bio_endio(bio, error);
NeilBrown5bb23a62007-09-27 12:46:13 +0200157 } else {
158
159 /*
160 * Okay, this is the barrier request in progress, just
161 * record the error;
162 */
163 if (error && !q->orderr)
164 q->orderr = error;
165 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100166}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168void blk_dump_rq_flags(struct request *rq, char *msg)
169{
170 int bit;
171
Jens Axboe6728cb02008-01-31 13:03:55 +0100172 printk(KERN_INFO "%s: dev %s: type=%x, flags=%x\n", msg,
Jens Axboe4aff5e22006-08-10 08:44:47 +0200173 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
174 rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Jens Axboe6728cb02008-01-31 13:03:55 +0100176 printk(KERN_INFO " sector %llu, nr/cnr %lu/%u\n",
177 (unsigned long long)rq->sector,
178 rq->nr_sectors,
179 rq->current_nr_sectors);
180 printk(KERN_INFO " bio %p, biotail %p, buffer %p, data %p, len %u\n",
181 rq->bio, rq->biotail,
182 rq->buffer, rq->data,
183 rq->data_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Jens Axboe4aff5e22006-08-10 08:44:47 +0200185 if (blk_pc_request(rq)) {
Jens Axboe6728cb02008-01-31 13:03:55 +0100186 printk(KERN_INFO " cdb: ");
FUJITA Tomonorid34c87e2008-04-29 14:37:52 +0200187 for (bit = 0; bit < BLK_MAX_CDB; bit++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 printk("%02x ", rq->cmd[bit]);
189 printk("\n");
190 }
191}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192EXPORT_SYMBOL(blk_dump_rq_flags);
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194/*
195 * "plug" the device if there are no outstanding requests: this will
196 * force the transfer to start only after we have put all the requests
197 * on the list.
198 *
199 * This is called with interrupts off and no requests on the queue and
200 * with the queue lock held.
201 */
Jens Axboe165125e2007-07-24 09:28:11 +0200202void blk_plug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 WARN_ON(!irqs_disabled());
205
206 /*
207 * don't plug a stopped queue, it must be paired with blk_start_queue()
208 * which will restart the queueing
209 */
Coywolf Qi Hunt7daac492006-04-19 10:14:49 +0200210 if (blk_queue_stopped(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return;
212
Jens Axboee48ec692008-07-03 13:18:54 +0200213 if (!queue_flag_test_and_set(QUEUE_FLAG_PLUGGED, q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
Jens Axboe2056a782006-03-23 20:00:26 +0100215 blk_add_trace_generic(q, NULL, 0, BLK_TA_PLUG);
216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218EXPORT_SYMBOL(blk_plug_device);
219
Jens Axboe6c5e0c42008-08-01 20:31:32 +0200220/**
221 * blk_plug_device_unlocked - plug a device without queue lock held
222 * @q: The &struct request_queue to plug
223 *
224 * Description:
225 * Like @blk_plug_device(), but grabs the queue lock and disables
226 * interrupts.
227 **/
228void blk_plug_device_unlocked(struct request_queue *q)
229{
230 unsigned long flags;
231
232 spin_lock_irqsave(q->queue_lock, flags);
233 blk_plug_device(q);
234 spin_unlock_irqrestore(q->queue_lock, flags);
235}
236EXPORT_SYMBOL(blk_plug_device_unlocked);
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238/*
239 * remove the queue from the plugged list, if present. called with
240 * queue lock held and interrupts disabled.
241 */
Jens Axboe165125e2007-07-24 09:28:11 +0200242int blk_remove_plug(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 WARN_ON(!irqs_disabled());
245
Jens Axboee48ec692008-07-03 13:18:54 +0200246 if (!queue_flag_test_and_clear(QUEUE_FLAG_PLUGGED, q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return 0;
248
249 del_timer(&q->unplug_timer);
250 return 1;
251}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252EXPORT_SYMBOL(blk_remove_plug);
253
254/*
255 * remove the plug and let it rip..
256 */
Jens Axboe165125e2007-07-24 09:28:11 +0200257void __generic_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Coywolf Qi Hunt7daac492006-04-19 10:14:49 +0200259 if (unlikely(blk_queue_stopped(q)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return;
261
262 if (!blk_remove_plug(q))
263 return;
264
Jens Axboe22e2c502005-06-27 10:55:12 +0200265 q->request_fn(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267EXPORT_SYMBOL(__generic_unplug_device);
268
269/**
270 * generic_unplug_device - fire a request queue
Jens Axboe165125e2007-07-24 09:28:11 +0200271 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 *
273 * Description:
274 * Linux uses plugging to build bigger requests queues before letting
275 * the device have at them. If a queue is plugged, the I/O scheduler
276 * is still adding and merging requests on the queue. Once the queue
277 * gets unplugged, the request_fn defined for the queue is invoked and
278 * transfers started.
279 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200280void generic_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Jens Axboedbaf2c02008-05-07 09:48:17 +0200282 if (blk_queue_plugged(q)) {
283 spin_lock_irq(q->queue_lock);
284 __generic_unplug_device(q);
285 spin_unlock_irq(q->queue_lock);
286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288EXPORT_SYMBOL(generic_unplug_device);
289
290static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
291 struct page *page)
292{
Jens Axboe165125e2007-07-24 09:28:11 +0200293 struct request_queue *q = bdi->unplug_io_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500295 blk_unplug(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
Jens Axboe86db1e22008-01-29 14:53:40 +0100298void blk_unplug_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Jens Axboe165125e2007-07-24 09:28:11 +0200300 struct request_queue *q =
301 container_of(work, struct request_queue, unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Jens Axboe2056a782006-03-23 20:00:26 +0100303 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
304 q->rq.count[READ] + q->rq.count[WRITE]);
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 q->unplug_fn(q);
307}
308
Jens Axboe86db1e22008-01-29 14:53:40 +0100309void blk_unplug_timeout(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Jens Axboe165125e2007-07-24 09:28:11 +0200311 struct request_queue *q = (struct request_queue *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Jens Axboe2056a782006-03-23 20:00:26 +0100313 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_TIMER, NULL,
314 q->rq.count[READ] + q->rq.count[WRITE]);
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 kblockd_schedule_work(&q->unplug_work);
317}
318
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500319void blk_unplug(struct request_queue *q)
320{
321 /*
322 * devices don't necessarily have an ->unplug_fn defined
323 */
324 if (q->unplug_fn) {
325 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
326 q->rq.count[READ] + q->rq.count[WRITE]);
327
328 q->unplug_fn(q);
329 }
330}
331EXPORT_SYMBOL(blk_unplug);
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333/**
334 * blk_start_queue - restart a previously stopped queue
Jens Axboe165125e2007-07-24 09:28:11 +0200335 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 *
337 * Description:
338 * blk_start_queue() will clear the stop flag on the queue, and call
339 * the request_fn for the queue if it was in a stopped state when
340 * entered. Also see blk_stop_queue(). Queue lock must be held.
341 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200342void blk_start_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200344 WARN_ON(!irqs_disabled());
345
Nick Piggin75ad23b2008-04-29 14:48:33 +0200346 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 /*
349 * one level of recursion is ok and is much faster than kicking
350 * the unplug handling
351 */
Jens Axboee48ec692008-07-03 13:18:54 +0200352 if (!queue_flag_test_and_set(QUEUE_FLAG_REENTER, q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 q->request_fn(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200354 queue_flag_clear(QUEUE_FLAG_REENTER, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 } else {
356 blk_plug_device(q);
357 kblockd_schedule_work(&q->unplug_work);
358 }
359}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360EXPORT_SYMBOL(blk_start_queue);
361
362/**
363 * blk_stop_queue - stop a queue
Jens Axboe165125e2007-07-24 09:28:11 +0200364 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 *
366 * Description:
367 * The Linux block layer assumes that a block driver will consume all
368 * entries on the request queue when the request_fn strategy is called.
369 * Often this will not happen, because of hardware limitations (queue
370 * depth settings). If a device driver gets a 'queue full' response,
371 * or if it simply chooses not to queue more I/O at one point, it can
372 * call this function to prevent the request_fn from being called until
373 * the driver has signalled it's ready to go again. This happens by calling
374 * blk_start_queue() to restart queue operations. Queue lock must be held.
375 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200376void blk_stop_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
378 blk_remove_plug(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200379 queue_flag_set(QUEUE_FLAG_STOPPED, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381EXPORT_SYMBOL(blk_stop_queue);
382
383/**
384 * blk_sync_queue - cancel any pending callbacks on a queue
385 * @q: the queue
386 *
387 * Description:
388 * The block layer may perform asynchronous callback activity
389 * on a queue, such as calling the unplug function after a timeout.
390 * A block device may call blk_sync_queue to ensure that any
391 * such activity is cancelled, thus allowing it to release resources
Michael Opdenacker59c51592007-05-09 08:57:56 +0200392 * that the callbacks might use. The caller must already have made sure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 * that its ->make_request_fn will not re-add plugging prior to calling
394 * this function.
395 *
396 */
397void blk_sync_queue(struct request_queue *q)
398{
399 del_timer_sync(&q->unplug_timer);
Oleg Nesterovabbeb882007-10-23 15:08:19 +0200400 kblockd_flush_work(&q->unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402EXPORT_SYMBOL(blk_sync_queue);
403
404/**
405 * blk_run_queue - run a single device queue
406 * @q: The queue to run
407 */
Nick Piggin75ad23b2008-04-29 14:48:33 +0200408void __blk_run_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 blk_remove_plug(q);
Jens Axboedac07ec2006-05-11 08:20:16 +0200411
412 /*
413 * Only recurse once to avoid overrunning the stack, let the unplug
414 * handling reinvoke the handler shortly if we already got there.
415 */
416 if (!elv_queue_empty(q)) {
Jens Axboee48ec692008-07-03 13:18:54 +0200417 if (!queue_flag_test_and_set(QUEUE_FLAG_REENTER, q)) {
Jens Axboedac07ec2006-05-11 08:20:16 +0200418 q->request_fn(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200419 queue_flag_clear(QUEUE_FLAG_REENTER, q);
Jens Axboedac07ec2006-05-11 08:20:16 +0200420 } else {
421 blk_plug_device(q);
422 kblockd_schedule_work(&q->unplug_work);
423 }
424 }
Nick Piggin75ad23b2008-04-29 14:48:33 +0200425}
426EXPORT_SYMBOL(__blk_run_queue);
Jens Axboedac07ec2006-05-11 08:20:16 +0200427
Nick Piggin75ad23b2008-04-29 14:48:33 +0200428/**
429 * blk_run_queue - run a single device queue
430 * @q: The queue to run
431 */
432void blk_run_queue(struct request_queue *q)
433{
434 unsigned long flags;
435
436 spin_lock_irqsave(q->queue_lock, flags);
437 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 spin_unlock_irqrestore(q->queue_lock, flags);
439}
440EXPORT_SYMBOL(blk_run_queue);
441
Jens Axboe165125e2007-07-24 09:28:11 +0200442void blk_put_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500443{
444 kobject_put(&q->kobj);
445}
Al Viro483f4af2006-03-18 18:34:37 -0500446
Jens Axboe6728cb02008-01-31 13:03:55 +0100447void blk_cleanup_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500448{
449 mutex_lock(&q->sysfs_lock);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200450 queue_flag_set_unlocked(QUEUE_FLAG_DEAD, q);
Al Viro483f4af2006-03-18 18:34:37 -0500451 mutex_unlock(&q->sysfs_lock);
452
453 if (q->elevator)
454 elevator_exit(q->elevator);
455
456 blk_put_queue(q);
457}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458EXPORT_SYMBOL(blk_cleanup_queue);
459
Jens Axboe165125e2007-07-24 09:28:11 +0200460static int blk_init_free_list(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
462 struct request_list *rl = &q->rq;
463
464 rl->count[READ] = rl->count[WRITE] = 0;
465 rl->starved[READ] = rl->starved[WRITE] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200466 rl->elvpriv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 init_waitqueue_head(&rl->wait[READ]);
468 init_waitqueue_head(&rl->wait[WRITE]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Christoph Lameter19460892005-06-23 00:08:19 -0700470 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
471 mempool_free_slab, request_cachep, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 if (!rl->rq_pool)
474 return -ENOMEM;
475
476 return 0;
477}
478
Jens Axboe165125e2007-07-24 09:28:11 +0200479struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Christoph Lameter19460892005-06-23 00:08:19 -0700481 return blk_alloc_queue_node(gfp_mask, -1);
482}
483EXPORT_SYMBOL(blk_alloc_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Jens Axboe165125e2007-07-24 09:28:11 +0200485struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -0700486{
Jens Axboe165125e2007-07-24 09:28:11 +0200487 struct request_queue *q;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700488 int err;
Christoph Lameter19460892005-06-23 00:08:19 -0700489
Jens Axboe8324aa92008-01-29 14:51:59 +0100490 q = kmem_cache_alloc_node(blk_requestq_cachep,
Christoph Lameter94f60302007-07-17 04:03:29 -0700491 gfp_mask | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (!q)
493 return NULL;
494
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700495 q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
496 q->backing_dev_info.unplug_io_data = q;
497 err = bdi_init(&q->backing_dev_info);
498 if (err) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100499 kmem_cache_free(blk_requestq_cachep, q);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700500 return NULL;
501 }
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 init_timer(&q->unplug_timer);
Al Viro483f4af2006-03-18 18:34:37 -0500504
Jens Axboe8324aa92008-01-29 14:51:59 +0100505 kobject_init(&q->kobj, &blk_queue_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Al Viro483f4af2006-03-18 18:34:37 -0500507 mutex_init(&q->sysfs_lock);
Neil Browne7e72bf2008-05-14 16:05:54 -0700508 spin_lock_init(&q->__queue_lock);
Al Viro483f4af2006-03-18 18:34:37 -0500509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return q;
511}
Christoph Lameter19460892005-06-23 00:08:19 -0700512EXPORT_SYMBOL(blk_alloc_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514/**
515 * blk_init_queue - prepare a request queue for use with a block device
516 * @rfn: The function to be called to process requests that have been
517 * placed on the queue.
518 * @lock: Request queue spin lock
519 *
520 * Description:
521 * If a block device wishes to use the standard request handling procedures,
522 * which sorts requests and coalesces adjacent requests, then it must
523 * call blk_init_queue(). The function @rfn will be called when there
524 * are requests on the queue that need to be processed. If the device
525 * supports plugging, then @rfn may not be called immediately when requests
526 * are available on the queue, but may be called at some time later instead.
527 * Plugged queues are generally unplugged when a buffer belonging to one
528 * of the requests on the queue is needed, or due to memory pressure.
529 *
530 * @rfn is not required, or even expected, to remove all requests off the
531 * queue, but only as many as it can handle at a time. If it does leave
532 * requests on the queue, it is responsible for arranging that the requests
533 * get dealt with eventually.
534 *
535 * The queue spin lock must be held while manipulating the requests on the
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200536 * request queue; this lock will be taken also from interrupt context, so irq
537 * disabling is needed for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 *
Randy Dunlap710027a2008-08-19 20:13:11 +0200539 * Function returns a pointer to the initialized request queue, or %NULL if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 * it didn't succeed.
541 *
542 * Note:
543 * blk_init_queue() must be paired with a blk_cleanup_queue() call
544 * when the block device is deactivated (such as at module unload).
545 **/
Christoph Lameter19460892005-06-23 00:08:19 -0700546
Jens Axboe165125e2007-07-24 09:28:11 +0200547struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Christoph Lameter19460892005-06-23 00:08:19 -0700549 return blk_init_queue_node(rfn, lock, -1);
550}
551EXPORT_SYMBOL(blk_init_queue);
552
Jens Axboe165125e2007-07-24 09:28:11 +0200553struct request_queue *
Christoph Lameter19460892005-06-23 00:08:19 -0700554blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
555{
Jens Axboe165125e2007-07-24 09:28:11 +0200556 struct request_queue *q = blk_alloc_queue_node(GFP_KERNEL, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 if (!q)
559 return NULL;
560
Christoph Lameter19460892005-06-23 00:08:19 -0700561 q->node = node_id;
Al Viro8669aaf2006-03-18 13:50:00 -0500562 if (blk_init_free_list(q)) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100563 kmem_cache_free(blk_requestq_cachep, q);
Al Viro8669aaf2006-03-18 13:50:00 -0500564 return NULL;
565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
152587d2005-04-12 16:22:06 -0500567 /*
568 * if caller didn't supply a lock, they get per-queue locking with
569 * our embedded lock
570 */
Neil Browne7e72bf2008-05-14 16:05:54 -0700571 if (!lock)
152587d2005-04-12 16:22:06 -0500572 lock = &q->__queue_lock;
152587d2005-04-12 16:22:06 -0500573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 q->request_fn = rfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 q->prep_rq_fn = NULL;
576 q->unplug_fn = generic_unplug_device;
577 q->queue_flags = (1 << QUEUE_FLAG_CLUSTER);
578 q->queue_lock = lock;
579
580 blk_queue_segment_boundary(q, 0xffffffff);
581
582 blk_queue_make_request(q, __make_request);
583 blk_queue_max_segment_size(q, MAX_SEGMENT_SIZE);
584
585 blk_queue_max_hw_segments(q, MAX_HW_SEGMENTS);
586 blk_queue_max_phys_segments(q, MAX_PHYS_SEGMENTS);
587
Alan Stern44ec9542007-02-20 11:01:57 -0500588 q->sg_reserved_size = INT_MAX;
589
FUJITA Tomonoriabf54392008-08-16 14:10:05 +0900590 blk_set_cmd_filter_defaults(&q->cmd_filter);
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 /*
593 * all done
594 */
595 if (!elevator_init(q, NULL)) {
596 blk_queue_congestion_threshold(q);
597 return q;
598 }
599
Al Viro8669aaf2006-03-18 13:50:00 -0500600 blk_put_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return NULL;
602}
Christoph Lameter19460892005-06-23 00:08:19 -0700603EXPORT_SYMBOL(blk_init_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Jens Axboe165125e2007-07-24 09:28:11 +0200605int blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Nick Pigginfde6ad22005-06-23 00:08:53 -0700607 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
Al Viro483f4af2006-03-18 18:34:37 -0500608 kobject_get(&q->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return 0;
610 }
611
612 return 1;
613}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Jens Axboe165125e2007-07-24 09:28:11 +0200615static inline void blk_free_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Jens Axboe4aff5e22006-08-10 08:44:47 +0200617 if (rq->cmd_flags & REQ_ELVPRIV)
Tejun Heocb98fc82005-10-28 08:29:39 +0200618 elv_put_request(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 mempool_free(rq, q->rq.rq_pool);
620}
621
Jens Axboe1ea25ec2006-07-18 22:24:11 +0200622static struct request *
Jens Axboe165125e2007-07-24 09:28:11 +0200623blk_alloc_request(struct request_queue *q, int rw, int priv, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
625 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
626
627 if (!rq)
628 return NULL;
629
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200630 blk_rq_init(q, rq);
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200631
Jens Axboe49171e52006-08-10 08:59:11 +0200632 rq->cmd_flags = rw | REQ_ALLOCED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Tejun Heocb98fc82005-10-28 08:29:39 +0200634 if (priv) {
Jens Axboecb78b282006-07-28 09:32:57 +0200635 if (unlikely(elv_set_request(q, rq, gfp_mask))) {
Tejun Heocb98fc82005-10-28 08:29:39 +0200636 mempool_free(rq, q->rq.rq_pool);
637 return NULL;
638 }
Jens Axboe4aff5e22006-08-10 08:44:47 +0200639 rq->cmd_flags |= REQ_ELVPRIV;
Tejun Heocb98fc82005-10-28 08:29:39 +0200640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Tejun Heocb98fc82005-10-28 08:29:39 +0200642 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
645/*
646 * ioc_batching returns true if the ioc is a valid batching request and
647 * should be given priority access to a request.
648 */
Jens Axboe165125e2007-07-24 09:28:11 +0200649static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
651 if (!ioc)
652 return 0;
653
654 /*
655 * Make sure the process is able to allocate at least 1 request
656 * even if the batch times out, otherwise we could theoretically
657 * lose wakeups.
658 */
659 return ioc->nr_batch_requests == q->nr_batching ||
660 (ioc->nr_batch_requests > 0
661 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
662}
663
664/*
665 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
666 * will cause the process to be a "batcher" on all queues in the system. This
667 * is the behaviour we want though - once it gets a wakeup it should be given
668 * a nice run.
669 */
Jens Axboe165125e2007-07-24 09:28:11 +0200670static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 if (!ioc || ioc_batching(q, ioc))
673 return;
674
675 ioc->nr_batch_requests = q->nr_batching;
676 ioc->last_waited = jiffies;
677}
678
Jens Axboe165125e2007-07-24 09:28:11 +0200679static void __freed_request(struct request_queue *q, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
681 struct request_list *rl = &q->rq;
682
683 if (rl->count[rw] < queue_congestion_off_threshold(q))
Thomas Maier79e2de42006-10-19 23:28:15 -0700684 blk_clear_queue_congested(q, rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 if (rl->count[rw] + 1 <= q->nr_requests) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (waitqueue_active(&rl->wait[rw]))
688 wake_up(&rl->wait[rw]);
689
690 blk_clear_queue_full(q, rw);
691 }
692}
693
694/*
695 * A request has just been released. Account for it, update the full and
696 * congestion status, wake up any waiters. Called under q->queue_lock.
697 */
Jens Axboe165125e2007-07-24 09:28:11 +0200698static void freed_request(struct request_queue *q, int rw, int priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
700 struct request_list *rl = &q->rq;
701
702 rl->count[rw]--;
Tejun Heocb98fc82005-10-28 08:29:39 +0200703 if (priv)
704 rl->elvpriv--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 __freed_request(q, rw);
707
708 if (unlikely(rl->starved[rw ^ 1]))
709 __freed_request(q, rw ^ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
712#define blkdev_free_rq(list) list_entry((list)->next, struct request, queuelist)
713/*
Nick Piggind6344532005-06-28 20:45:14 -0700714 * Get a free request, queue_lock must be held.
715 * Returns NULL on failure, with queue_lock held.
716 * Returns !NULL on success, with queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 */
Jens Axboe165125e2007-07-24 09:28:11 +0200718static struct request *get_request(struct request_queue *q, int rw_flags,
Jens Axboe7749a8d2006-12-13 13:02:26 +0100719 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
721 struct request *rq = NULL;
722 struct request_list *rl = &q->rq;
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100723 struct io_context *ioc = NULL;
Jens Axboe7749a8d2006-12-13 13:02:26 +0100724 const int rw = rw_flags & 0x01;
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100725 int may_queue, priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Jens Axboe7749a8d2006-12-13 13:02:26 +0100727 may_queue = elv_may_queue(q, rw_flags);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100728 if (may_queue == ELV_MQUEUE_NO)
729 goto rq_starved;
730
731 if (rl->count[rw]+1 >= queue_congestion_on_threshold(q)) {
732 if (rl->count[rw]+1 >= q->nr_requests) {
Jens Axboeb5deef92006-07-19 23:39:40 +0200733 ioc = current_io_context(GFP_ATOMIC, q->node);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100734 /*
735 * The queue will fill after this allocation, so set
736 * it as full, and mark this process as "batching".
737 * This process will be allowed to complete a batch of
738 * requests, others will be blocked.
739 */
740 if (!blk_queue_full(q, rw)) {
741 ioc_set_batching(q, ioc);
742 blk_set_queue_full(q, rw);
743 } else {
744 if (may_queue != ELV_MQUEUE_MUST
745 && !ioc_batching(q, ioc)) {
746 /*
747 * The queue is full and the allocating
748 * process is not a "batcher", and not
749 * exempted by the IO scheduler
750 */
751 goto out;
752 }
753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
Thomas Maier79e2de42006-10-19 23:28:15 -0700755 blk_set_queue_congested(q, rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
757
Jens Axboe082cf692005-06-28 16:35:11 +0200758 /*
759 * Only allow batching queuers to allocate up to 50% over the defined
760 * limit of requests, otherwise we could have thousands of requests
761 * allocated with any setting of ->nr_requests
762 */
Hugh Dickinsfd782a42005-06-29 15:15:40 +0100763 if (rl->count[rw] >= (3 * q->nr_requests / 2))
Jens Axboe082cf692005-06-28 16:35:11 +0200764 goto out;
Hugh Dickinsfd782a42005-06-29 15:15:40 +0100765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 rl->count[rw]++;
767 rl->starved[rw] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200768
Jens Axboe64521d12005-10-28 08:30:39 +0200769 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Tejun Heocb98fc82005-10-28 08:29:39 +0200770 if (priv)
771 rl->elvpriv++;
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 spin_unlock_irq(q->queue_lock);
774
Jens Axboe7749a8d2006-12-13 13:02:26 +0100775 rq = blk_alloc_request(q, rw_flags, priv, gfp_mask);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100776 if (unlikely(!rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 /*
778 * Allocation failed presumably due to memory. Undo anything
779 * we might have messed up.
780 *
781 * Allocating task should really be put onto the front of the
782 * wait queue, but this is pretty rare.
783 */
784 spin_lock_irq(q->queue_lock);
Tejun Heocb98fc82005-10-28 08:29:39 +0200785 freed_request(q, rw, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 /*
788 * in the very unlikely event that allocation failed and no
789 * requests for this direction was pending, mark us starved
790 * so that freeing of a request in the other direction will
791 * notice us. another possible fix would be to split the
792 * rq mempool into READ and WRITE
793 */
794rq_starved:
795 if (unlikely(rl->count[rw] == 0))
796 rl->starved[rw] = 1;
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 goto out;
799 }
800
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100801 /*
802 * ioc may be NULL here, and ioc_batching will be false. That's
803 * OK, if the queue is under the request limit then requests need
804 * not count toward the nr_batch_requests limit. There will always
805 * be some limit enforced by BLK_BATCH_TIME.
806 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (ioc_batching(q, ioc))
808 ioc->nr_batch_requests--;
Jens Axboe6728cb02008-01-31 13:03:55 +0100809
Jens Axboe2056a782006-03-23 20:00:26 +0100810 blk_add_trace_generic(q, bio, rw, BLK_TA_GETRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return rq;
813}
814
815/*
816 * No available requests for this queue, unplug the device and wait for some
817 * requests to become available.
Nick Piggind6344532005-06-28 20:45:14 -0700818 *
819 * Called with q->queue_lock held, and returns with it unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 */
Jens Axboe165125e2007-07-24 09:28:11 +0200821static struct request *get_request_wait(struct request_queue *q, int rw_flags,
Jens Axboe22e2c502005-06-27 10:55:12 +0200822 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Jens Axboe7749a8d2006-12-13 13:02:26 +0100824 const int rw = rw_flags & 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 struct request *rq;
826
Jens Axboe7749a8d2006-12-13 13:02:26 +0100827 rq = get_request(q, rw_flags, bio, GFP_NOIO);
Nick Piggin450991b2005-06-28 20:45:13 -0700828 while (!rq) {
829 DEFINE_WAIT(wait);
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200830 struct io_context *ioc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 struct request_list *rl = &q->rq;
832
833 prepare_to_wait_exclusive(&rl->wait[rw], &wait,
834 TASK_UNINTERRUPTIBLE);
835
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200836 blk_add_trace_generic(q, bio, rw, BLK_TA_SLEEPRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200838 __generic_unplug_device(q);
839 spin_unlock_irq(q->queue_lock);
840 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200842 /*
843 * After sleeping, we become a "batching" process and
844 * will be able to allocate at least one request, and
845 * up to a big batch of them for a small period time.
846 * See ioc_batching, ioc_set_batching
847 */
848 ioc = current_io_context(GFP_NOIO, q->node);
849 ioc_set_batching(q, ioc);
Jens Axboe2056a782006-03-23 20:00:26 +0100850
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200851 spin_lock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 finish_wait(&rl->wait[rw], &wait);
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200853
854 rq = get_request(q, rw_flags, bio, GFP_NOIO);
855 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 return rq;
858}
859
Jens Axboe165125e2007-07-24 09:28:11 +0200860struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
862 struct request *rq;
863
864 BUG_ON(rw != READ && rw != WRITE);
865
Nick Piggind6344532005-06-28 20:45:14 -0700866 spin_lock_irq(q->queue_lock);
867 if (gfp_mask & __GFP_WAIT) {
Jens Axboe22e2c502005-06-27 10:55:12 +0200868 rq = get_request_wait(q, rw, NULL);
Nick Piggind6344532005-06-28 20:45:14 -0700869 } else {
Jens Axboe22e2c502005-06-27 10:55:12 +0200870 rq = get_request(q, rw, NULL, gfp_mask);
Nick Piggind6344532005-06-28 20:45:14 -0700871 if (!rq)
872 spin_unlock_irq(q->queue_lock);
873 }
874 /* q->queue_lock is unlocked at this point */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 return rq;
877}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878EXPORT_SYMBOL(blk_get_request);
879
880/**
Jens Axboedc72ef42006-07-20 14:54:05 +0200881 * blk_start_queueing - initiate dispatch of requests to device
882 * @q: request queue to kick into gear
883 *
884 * This is basically a helper to remove the need to know whether a queue
885 * is plugged or not if someone just wants to initiate dispatch of requests
886 * for this queue.
887 *
888 * The queue lock must be held with interrupts disabled.
889 */
Jens Axboe165125e2007-07-24 09:28:11 +0200890void blk_start_queueing(struct request_queue *q)
Jens Axboedc72ef42006-07-20 14:54:05 +0200891{
892 if (!blk_queue_plugged(q))
893 q->request_fn(q);
894 else
895 __generic_unplug_device(q);
896}
897EXPORT_SYMBOL(blk_start_queueing);
898
899/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 * blk_requeue_request - put a request back on queue
901 * @q: request queue where request should be inserted
902 * @rq: request to be inserted
903 *
904 * Description:
905 * Drivers often keep queueing requests until the hardware cannot accept
906 * more, when that condition happens we need to put the request back
907 * on the queue. Must be called with queue lock held.
908 */
Jens Axboe165125e2007-07-24 09:28:11 +0200909void blk_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Jens Axboe2056a782006-03-23 20:00:26 +0100911 blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (blk_rq_tagged(rq))
914 blk_queue_end_tag(q, rq);
915
916 elv_requeue_request(q, rq);
917}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918EXPORT_SYMBOL(blk_requeue_request);
919
920/**
Randy Dunlap710027a2008-08-19 20:13:11 +0200921 * blk_insert_request - insert a special request into a request queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 * @q: request queue where request should be inserted
923 * @rq: request to be inserted
924 * @at_head: insert request at head or tail of queue
925 * @data: private data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 *
927 * Description:
928 * Many block devices need to execute commands asynchronously, so they don't
929 * block the whole kernel from preemption during request execution. This is
930 * accomplished normally by inserting aritficial requests tagged as
Randy Dunlap710027a2008-08-19 20:13:11 +0200931 * REQ_TYPE_SPECIAL in to the corresponding request queue, and letting them
932 * be scheduled for actual execution by the request queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 *
934 * We have the option of inserting the head or the tail of the queue.
935 * Typically we use the tail for new ioctls and so forth. We use the head
936 * of the queue for things like a QUEUE_FULL message from a device, or a
937 * host that is unable to accept a particular command.
938 */
Jens Axboe165125e2007-07-24 09:28:11 +0200939void blk_insert_request(struct request_queue *q, struct request *rq,
Tejun Heo 867d1192005-04-24 02:06:05 -0500940 int at_head, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Tejun Heo 867d1192005-04-24 02:06:05 -0500942 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 unsigned long flags;
944
945 /*
946 * tell I/O scheduler that this isn't a regular read/write (ie it
947 * must not attempt merges on this) and that it acts as a soft
948 * barrier
949 */
Jens Axboe4aff5e22006-08-10 08:44:47 +0200950 rq->cmd_type = REQ_TYPE_SPECIAL;
951 rq->cmd_flags |= REQ_SOFTBARRIER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 rq->special = data;
954
955 spin_lock_irqsave(q->queue_lock, flags);
956
957 /*
958 * If command is tagged, release the tag
959 */
Tejun Heo 867d1192005-04-24 02:06:05 -0500960 if (blk_rq_tagged(rq))
961 blk_queue_end_tag(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Jerome Marchandb238b3d2007-10-23 15:05:46 +0200963 drive_stat_acct(rq, 1);
Tejun Heo 867d1192005-04-24 02:06:05 -0500964 __elv_add_request(q, rq, where, 0);
Jens Axboedc72ef42006-07-20 14:54:05 +0200965 blk_start_queueing(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 spin_unlock_irqrestore(q->queue_lock, flags);
967}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968EXPORT_SYMBOL(blk_insert_request);
969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970/*
971 * add-request adds a request to the linked list.
972 * queue lock is held and interrupts disabled, as we muck with the
973 * request queue list.
974 */
Jens Axboe6728cb02008-01-31 13:03:55 +0100975static inline void add_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
Jerome Marchandb238b3d2007-10-23 15:05:46 +0200977 drive_stat_acct(req, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 /*
980 * elevator indicated where it wants this request to be
981 * inserted at elevator_merge time
982 */
983 __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
984}
Jens Axboe6728cb02008-01-31 13:03:55 +0100985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986/*
987 * disk_round_stats() - Round off the performance stats on a struct
988 * disk_stats.
989 *
990 * The average IO queue length and utilisation statistics are maintained
991 * by observing the current state of the queue length and the amount of
992 * time it has been in this state for.
993 *
994 * Normally, that accounting is done on IO completion, but that can result
995 * in more than a second's worth of IO being accounted for within any one
996 * second, leading to >100% utilisation. To deal with that, we call this
997 * function to do a round-off before returning the results when reading
998 * /proc/diskstats. This accounts immediately for all queue usage up to
999 * the current jiffies and restarts the counters again.
1000 */
Tejun Heoc9959052008-08-25 19:47:21 +09001001void disk_round_stats(int cpu, struct gendisk *disk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
1003 unsigned long now = jiffies;
1004
Chen, Kenneth Wb2982642005-10-13 21:49:29 +02001005 if (now == disk->stamp)
1006 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Chen, Kenneth W20e5c812005-10-13 21:48:42 +02001008 if (disk->in_flight) {
Tejun Heoc9959052008-08-25 19:47:21 +09001009 disk_stat_add(cpu, disk, time_in_queue,
1010 disk->in_flight * (now - disk->stamp));
1011 disk_stat_add(cpu, disk, io_ticks, (now - disk->stamp));
Chen, Kenneth W20e5c812005-10-13 21:48:42 +02001012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 disk->stamp = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -08001015EXPORT_SYMBOL_GPL(disk_round_stats);
1016
Tejun Heoc9959052008-08-25 19:47:21 +09001017void part_round_stats(int cpu, struct hd_struct *part)
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001018{
1019 unsigned long now = jiffies;
1020
1021 if (now == part->stamp)
1022 return;
1023
1024 if (part->in_flight) {
Tejun Heoc9959052008-08-25 19:47:21 +09001025 part_stat_add(cpu, part, time_in_queue,
1026 part->in_flight * (now - part->stamp));
1027 part_stat_add(cpu, part, io_ticks, (now - part->stamp));
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001028 }
1029 part->stamp = now;
1030}
1031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032/*
1033 * queue lock must be held
1034 */
Jens Axboe165125e2007-07-24 09:28:11 +02001035void __blk_put_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (unlikely(!q))
1038 return;
1039 if (unlikely(--req->ref_count))
1040 return;
1041
Tejun Heo8922e162005-10-20 16:23:44 +02001042 elv_completed_request(q, req);
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 /*
1045 * Request may not have originated from ll_rw_blk. if not,
1046 * it didn't come out of our reserved rq pools
1047 */
Jens Axboe49171e52006-08-10 08:59:11 +02001048 if (req->cmd_flags & REQ_ALLOCED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 int rw = rq_data_dir(req);
Jens Axboe4aff5e22006-08-10 08:44:47 +02001050 int priv = req->cmd_flags & REQ_ELVPRIV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 BUG_ON(!list_empty(&req->queuelist));
Jens Axboe98170642006-07-28 09:23:08 +02001053 BUG_ON(!hlist_unhashed(&req->hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 blk_free_request(q, req);
Tejun Heocb98fc82005-10-28 08:29:39 +02001056 freed_request(q, rw, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
1058}
Mike Christie6e39b692005-11-11 05:30:24 -06001059EXPORT_SYMBOL_GPL(__blk_put_request);
1060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061void blk_put_request(struct request *req)
1062{
Tejun Heo8922e162005-10-20 16:23:44 +02001063 unsigned long flags;
Jens Axboe165125e2007-07-24 09:28:11 +02001064 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
FUJITA Tomonori52a93ba2008-07-15 21:21:45 +02001066 spin_lock_irqsave(q->queue_lock, flags);
1067 __blk_put_request(q, req);
1068 spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070EXPORT_SYMBOL(blk_put_request);
1071
Jens Axboe86db1e22008-01-29 14:53:40 +01001072void init_request_from_bio(struct request *req, struct bio *bio)
Tejun Heo52d9e672006-01-06 09:49:58 +01001073{
Jens Axboe4aff5e22006-08-10 08:44:47 +02001074 req->cmd_type = REQ_TYPE_FS;
Tejun Heo52d9e672006-01-06 09:49:58 +01001075
1076 /*
1077 * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
1078 */
1079 if (bio_rw_ahead(bio) || bio_failfast(bio))
Jens Axboe4aff5e22006-08-10 08:44:47 +02001080 req->cmd_flags |= REQ_FAILFAST;
Tejun Heo52d9e672006-01-06 09:49:58 +01001081
1082 /*
1083 * REQ_BARRIER implies no merging, but lets make it explicit
1084 */
David Woodhousefb2dce82008-08-05 18:01:53 +01001085 if (unlikely(bio_discard(bio))) {
David Woodhousee17fc0a2008-08-09 16:42:20 +01001086 req->cmd_flags |= REQ_DISCARD;
1087 if (bio_barrier(bio))
1088 req->cmd_flags |= REQ_SOFTBARRIER;
David Woodhousefb2dce82008-08-05 18:01:53 +01001089 req->q->prepare_discard_fn(req->q, req);
David Woodhousee17fc0a2008-08-09 16:42:20 +01001090 } else if (unlikely(bio_barrier(bio)))
1091 req->cmd_flags |= (REQ_HARDBARRIER | REQ_NOMERGE);
Tejun Heo52d9e672006-01-06 09:49:58 +01001092
Jens Axboeb31dc662006-06-13 08:26:10 +02001093 if (bio_sync(bio))
Jens Axboe4aff5e22006-08-10 08:44:47 +02001094 req->cmd_flags |= REQ_RW_SYNC;
Jens Axboe5404bc72006-08-10 09:01:02 +02001095 if (bio_rw_meta(bio))
1096 req->cmd_flags |= REQ_RW_META;
Jens Axboeb31dc662006-06-13 08:26:10 +02001097
Tejun Heo52d9e672006-01-06 09:49:58 +01001098 req->errors = 0;
1099 req->hard_sector = req->sector = bio->bi_sector;
Tejun Heo52d9e672006-01-06 09:49:58 +01001100 req->ioprio = bio_prio(bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001101 req->start_time = jiffies;
NeilBrownbc1c56f2007-08-16 13:31:30 +02001102 blk_rq_bio_prep(req->q, req, bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001103}
1104
Jens Axboe165125e2007-07-24 09:28:11 +02001105static int __make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
Nick Piggin450991b2005-06-28 20:45:13 -07001107 struct request *req;
David Woodhousefb2dce82008-08-05 18:01:53 +01001108 int el_ret, nr_sectors, barrier, discard, err;
Jens Axboe51da90f2006-07-18 04:14:45 +02001109 const unsigned short prio = bio_prio(bio);
1110 const int sync = bio_sync(bio);
Jens Axboe7749a8d2006-12-13 13:02:26 +01001111 int rw_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 nr_sectors = bio_sectors(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 /*
1116 * low level driver can indicate that it wants pages above a
1117 * certain limit bounced to low memory (ie for highmem, or even
1118 * ISA dma in theory)
1119 */
1120 blk_queue_bounce(q, &bio);
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 barrier = bio_barrier(bio);
David Woodhousee17fc0a2008-08-09 16:42:20 +01001123 if (unlikely(barrier) && bio_has_data(bio) &&
1124 (q->next_ordered == QUEUE_ORDERED_NONE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 err = -EOPNOTSUPP;
1126 goto end_io;
1127 }
1128
David Woodhousefb2dce82008-08-05 18:01:53 +01001129 discard = bio_discard(bio);
1130 if (unlikely(discard) && !q->prepare_discard_fn) {
1131 err = -EOPNOTSUPP;
1132 goto end_io;
1133 }
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 spin_lock_irq(q->queue_lock);
1136
Nick Piggin450991b2005-06-28 20:45:13 -07001137 if (unlikely(barrier) || elv_queue_empty(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 goto get_rq;
1139
1140 el_ret = elv_merge(q, &req, bio);
1141 switch (el_ret) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001142 case ELEVATOR_BACK_MERGE:
1143 BUG_ON(!rq_mergeable(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Jens Axboe6728cb02008-01-31 13:03:55 +01001145 if (!ll_back_merge_fn(q, req, bio))
1146 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Jens Axboe6728cb02008-01-31 13:03:55 +01001148 blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
Jens Axboe2056a782006-03-23 20:00:26 +01001149
Jens Axboe6728cb02008-01-31 13:03:55 +01001150 req->biotail->bi_next = bio;
1151 req->biotail = bio;
1152 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
1153 req->ioprio = ioprio_best(req->ioprio, prio);
1154 drive_stat_acct(req, 0);
1155 if (!attempt_back_merge(q, req))
1156 elv_merged_request(q, req, el_ret);
1157 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Jens Axboe6728cb02008-01-31 13:03:55 +01001159 case ELEVATOR_FRONT_MERGE:
1160 BUG_ON(!rq_mergeable(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Jens Axboe6728cb02008-01-31 13:03:55 +01001162 if (!ll_front_merge_fn(q, req, bio))
1163 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Jens Axboe6728cb02008-01-31 13:03:55 +01001165 blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
Jens Axboe2056a782006-03-23 20:00:26 +01001166
Jens Axboe6728cb02008-01-31 13:03:55 +01001167 bio->bi_next = req->bio;
1168 req->bio = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Jens Axboe6728cb02008-01-31 13:03:55 +01001170 /*
1171 * may not be valid. if the low level driver said
1172 * it didn't need a bounce buffer then it better
1173 * not touch req->buffer either...
1174 */
1175 req->buffer = bio_data(bio);
1176 req->current_nr_sectors = bio_cur_sectors(bio);
1177 req->hard_cur_sectors = req->current_nr_sectors;
1178 req->sector = req->hard_sector = bio->bi_sector;
1179 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
1180 req->ioprio = ioprio_best(req->ioprio, prio);
1181 drive_stat_acct(req, 0);
1182 if (!attempt_front_merge(q, req))
1183 elv_merged_request(q, req, el_ret);
1184 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Jens Axboe6728cb02008-01-31 13:03:55 +01001186 /* ELV_NO_MERGE: elevator says don't/can't merge. */
1187 default:
1188 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 }
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191get_rq:
Nick Piggin450991b2005-06-28 20:45:13 -07001192 /*
Jens Axboe7749a8d2006-12-13 13:02:26 +01001193 * This sync check and mask will be re-done in init_request_from_bio(),
1194 * but we need to set it earlier to expose the sync flag to the
1195 * rq allocator and io schedulers.
1196 */
1197 rw_flags = bio_data_dir(bio);
1198 if (sync)
1199 rw_flags |= REQ_RW_SYNC;
1200
1201 /*
Nick Piggin450991b2005-06-28 20:45:13 -07001202 * Grab a free request. This is might sleep but can not fail.
Nick Piggind6344532005-06-28 20:45:14 -07001203 * Returns with the queue unlocked.
Nick Piggin450991b2005-06-28 20:45:13 -07001204 */
Jens Axboe7749a8d2006-12-13 13:02:26 +01001205 req = get_request_wait(q, rw_flags, bio);
Nick Piggind6344532005-06-28 20:45:14 -07001206
Nick Piggin450991b2005-06-28 20:45:13 -07001207 /*
1208 * After dropping the lock and possibly sleeping here, our request
1209 * may now be mergeable after it had proven unmergeable (above).
1210 * We don't worry about that case for efficiency. It won't happen
1211 * often, and the elevators are able to handle it.
1212 */
Tejun Heo52d9e672006-01-06 09:49:58 +01001213 init_request_from_bio(req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Nick Piggin450991b2005-06-28 20:45:13 -07001215 spin_lock_irq(q->queue_lock);
1216 if (elv_queue_empty(q))
1217 blk_plug_device(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 add_request(q, req);
1219out:
Jens Axboe4a534f92005-04-16 15:25:40 -07001220 if (sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 __generic_unplug_device(q);
1222
1223 spin_unlock_irq(q->queue_lock);
1224 return 0;
1225
1226end_io:
NeilBrown6712ecf2007-09-27 12:47:43 +02001227 bio_endio(bio, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 return 0;
1229}
1230
1231/*
1232 * If bio->bi_dev is a partition, remap the location
1233 */
1234static inline void blk_partition_remap(struct bio *bio)
1235{
1236 struct block_device *bdev = bio->bi_bdev;
1237
Jens Axboebf2de6f2007-09-27 13:01:25 +02001238 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 struct hd_struct *p = bdev->bd_part;
Jens Axboea3623572005-11-01 09:26:16 +01001240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 bio->bi_sector += p->start_sect;
1242 bio->bi_bdev = bdev->bd_contains;
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001243
1244 blk_add_trace_remap(bdev_get_queue(bio->bi_bdev), bio,
1245 bdev->bd_dev, bio->bi_sector,
1246 bio->bi_sector - p->start_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 }
1248}
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250static void handle_bad_sector(struct bio *bio)
1251{
1252 char b[BDEVNAME_SIZE];
1253
1254 printk(KERN_INFO "attempt to access beyond end of device\n");
1255 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1256 bdevname(bio->bi_bdev, b),
1257 bio->bi_rw,
1258 (unsigned long long)bio->bi_sector + bio_sectors(bio),
1259 (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
1260
1261 set_bit(BIO_EOF, &bio->bi_flags);
1262}
1263
Akinobu Mitac17bb492006-12-08 02:39:46 -08001264#ifdef CONFIG_FAIL_MAKE_REQUEST
1265
1266static DECLARE_FAULT_ATTR(fail_make_request);
1267
1268static int __init setup_fail_make_request(char *str)
1269{
1270 return setup_fault_attr(&fail_make_request, str);
1271}
1272__setup("fail_make_request=", setup_fail_make_request);
1273
1274static int should_fail_request(struct bio *bio)
1275{
1276 if ((bio->bi_bdev->bd_disk->flags & GENHD_FL_FAIL) ||
1277 (bio->bi_bdev->bd_part && bio->bi_bdev->bd_part->make_it_fail))
1278 return should_fail(&fail_make_request, bio->bi_size);
1279
1280 return 0;
1281}
1282
1283static int __init fail_make_request_debugfs(void)
1284{
1285 return init_fault_attr_dentries(&fail_make_request,
1286 "fail_make_request");
1287}
1288
1289late_initcall(fail_make_request_debugfs);
1290
1291#else /* CONFIG_FAIL_MAKE_REQUEST */
1292
1293static inline int should_fail_request(struct bio *bio)
1294{
1295 return 0;
1296}
1297
1298#endif /* CONFIG_FAIL_MAKE_REQUEST */
1299
Jens Axboec07e2b42007-07-18 13:27:58 +02001300/*
1301 * Check whether this bio extends beyond the end of the device.
1302 */
1303static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1304{
1305 sector_t maxsector;
1306
1307 if (!nr_sectors)
1308 return 0;
1309
1310 /* Test device or partition size, when known. */
1311 maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
1312 if (maxsector) {
1313 sector_t sector = bio->bi_sector;
1314
1315 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1316 /*
1317 * This may well happen - the kernel calls bread()
1318 * without checking the size of the device, e.g., when
1319 * mounting a device.
1320 */
1321 handle_bad_sector(bio);
1322 return 1;
1323 }
1324 }
1325
1326 return 0;
1327}
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001330 * generic_make_request - hand a buffer to its device driver for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 * @bio: The bio describing the location in memory and on the device.
1332 *
1333 * generic_make_request() is used to make I/O requests of block
1334 * devices. It is passed a &struct bio, which describes the I/O that needs
1335 * to be done.
1336 *
1337 * generic_make_request() does not return any status. The
1338 * success/failure status of the request, along with notification of
1339 * completion, is delivered asynchronously through the bio->bi_end_io
1340 * function described (one day) else where.
1341 *
1342 * The caller of generic_make_request must make sure that bi_io_vec
1343 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1344 * set to describe the device address, and the
1345 * bi_end_io and optionally bi_private are set to describe how
1346 * completion notification should be signaled.
1347 *
1348 * generic_make_request and the drivers it calls may use bi_next if this
1349 * bio happens to be merged with someone else, and may change bi_dev and
1350 * bi_sector for remaps as it sees fit. So the values of these fields
1351 * should NOT be depended on after the call to generic_make_request.
1352 */
Neil Brownd89d8792007-05-01 09:53:42 +02001353static inline void __generic_make_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
Jens Axboe165125e2007-07-24 09:28:11 +02001355 struct request_queue *q;
NeilBrown5ddfe962006-10-30 22:07:21 -08001356 sector_t old_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 int ret, nr_sectors = bio_sectors(bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001358 dev_t old_dev;
Jens Axboe51fd77b2007-11-02 08:49:08 +01001359 int err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Jens Axboec07e2b42007-07-18 13:27:58 +02001363 if (bio_check_eod(bio, nr_sectors))
1364 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 /*
1367 * Resolve the mapping until finished. (drivers are
1368 * still free to implement/resolve their own stacking
1369 * by explicitly returning 0)
1370 *
1371 * NOTE: we don't repeat the blk_size check for each new device.
1372 * Stacking drivers are expected to know what they are doing.
1373 */
NeilBrown5ddfe962006-10-30 22:07:21 -08001374 old_sector = -1;
Jens Axboe2056a782006-03-23 20:00:26 +01001375 old_dev = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 do {
1377 char b[BDEVNAME_SIZE];
1378
1379 q = bdev_get_queue(bio->bi_bdev);
1380 if (!q) {
1381 printk(KERN_ERR
1382 "generic_make_request: Trying to access "
1383 "nonexistent block-device %s (%Lu)\n",
1384 bdevname(bio->bi_bdev, b),
1385 (long long) bio->bi_sector);
1386end_io:
Jens Axboe51fd77b2007-11-02 08:49:08 +01001387 bio_endio(bio, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 break;
1389 }
1390
Jens Axboe4fa253f2007-07-18 13:13:10 +02001391 if (unlikely(nr_sectors > q->max_hw_sectors)) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001392 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 bdevname(bio->bi_bdev, b),
1394 bio_sectors(bio),
1395 q->max_hw_sectors);
1396 goto end_io;
1397 }
1398
Nick Pigginfde6ad22005-06-23 00:08:53 -07001399 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 goto end_io;
1401
Akinobu Mitac17bb492006-12-08 02:39:46 -08001402 if (should_fail_request(bio))
1403 goto end_io;
1404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 /*
1406 * If this device has partitions, remap block n
1407 * of partition p to block n+start(p) of the disk.
1408 */
1409 blk_partition_remap(bio);
1410
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +02001411 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio))
1412 goto end_io;
1413
NeilBrown5ddfe962006-10-30 22:07:21 -08001414 if (old_sector != -1)
Jens Axboe4fa253f2007-07-18 13:13:10 +02001415 blk_add_trace_remap(q, bio, old_dev, bio->bi_sector,
NeilBrown5ddfe962006-10-30 22:07:21 -08001416 old_sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001417
1418 blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
1419
NeilBrown5ddfe962006-10-30 22:07:21 -08001420 old_sector = bio->bi_sector;
Jens Axboe2056a782006-03-23 20:00:26 +01001421 old_dev = bio->bi_bdev->bd_dev;
1422
Jens Axboec07e2b42007-07-18 13:27:58 +02001423 if (bio_check_eod(bio, nr_sectors))
1424 goto end_io;
David Woodhousefb2dce82008-08-05 18:01:53 +01001425 if ((bio_empty_barrier(bio) && !q->prepare_flush_fn) ||
1426 (bio_discard(bio) && !q->prepare_discard_fn)) {
Jens Axboe51fd77b2007-11-02 08:49:08 +01001427 err = -EOPNOTSUPP;
1428 goto end_io;
1429 }
NeilBrown5ddfe962006-10-30 22:07:21 -08001430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 ret = q->make_request_fn(q, bio);
1432 } while (ret);
1433}
1434
Neil Brownd89d8792007-05-01 09:53:42 +02001435/*
1436 * We only want one ->make_request_fn to be active at a time,
1437 * else stack usage with stacked devices could be a problem.
1438 * So use current->bio_{list,tail} to keep a list of requests
1439 * submited by a make_request_fn function.
1440 * current->bio_tail is also used as a flag to say if
1441 * generic_make_request is currently active in this task or not.
1442 * If it is NULL, then no make_request is active. If it is non-NULL,
1443 * then a make_request is active, and new requests should be added
1444 * at the tail
1445 */
1446void generic_make_request(struct bio *bio)
1447{
1448 if (current->bio_tail) {
1449 /* make_request is active */
1450 *(current->bio_tail) = bio;
1451 bio->bi_next = NULL;
1452 current->bio_tail = &bio->bi_next;
1453 return;
1454 }
1455 /* following loop may be a bit non-obvious, and so deserves some
1456 * explanation.
1457 * Before entering the loop, bio->bi_next is NULL (as all callers
1458 * ensure that) so we have a list with a single bio.
1459 * We pretend that we have just taken it off a longer list, so
1460 * we assign bio_list to the next (which is NULL) and bio_tail
1461 * to &bio_list, thus initialising the bio_list of new bios to be
1462 * added. __generic_make_request may indeed add some more bios
1463 * through a recursive call to generic_make_request. If it
1464 * did, we find a non-NULL value in bio_list and re-enter the loop
1465 * from the top. In this case we really did just take the bio
1466 * of the top of the list (no pretending) and so fixup bio_list and
1467 * bio_tail or bi_next, and call into __generic_make_request again.
1468 *
1469 * The loop was structured like this to make only one call to
1470 * __generic_make_request (which is important as it is large and
1471 * inlined) and to keep the structure simple.
1472 */
1473 BUG_ON(bio->bi_next);
1474 do {
1475 current->bio_list = bio->bi_next;
1476 if (bio->bi_next == NULL)
1477 current->bio_tail = &current->bio_list;
1478 else
1479 bio->bi_next = NULL;
1480 __generic_make_request(bio);
1481 bio = current->bio_list;
1482 } while (bio);
1483 current->bio_tail = NULL; /* deactivate */
1484}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485EXPORT_SYMBOL(generic_make_request);
1486
1487/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001488 * submit_bio - submit a bio to the block device layer for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1490 * @bio: The &struct bio which describes the I/O
1491 *
1492 * submit_bio() is very similar in purpose to generic_make_request(), and
1493 * uses that function to do most of the work. Both are fairly rough
Randy Dunlap710027a2008-08-19 20:13:11 +02001494 * interfaces; @bio must be presetup and ready for I/O.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 *
1496 */
1497void submit_bio(int rw, struct bio *bio)
1498{
1499 int count = bio_sectors(bio);
1500
Jens Axboe22e2c502005-06-27 10:55:12 +02001501 bio->bi_rw |= rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Jens Axboebf2de6f2007-09-27 13:01:25 +02001503 /*
1504 * If it's a regular read/write or a barrier with data attached,
1505 * go through the normal accounting stuff before submission.
1506 */
Jens Axboea9c701e2008-08-08 11:04:44 +02001507 if (bio_has_data(bio)) {
Jens Axboebf2de6f2007-09-27 13:01:25 +02001508 if (rw & WRITE) {
1509 count_vm_events(PGPGOUT, count);
1510 } else {
1511 task_io_account_read(bio->bi_size);
1512 count_vm_events(PGPGIN, count);
1513 }
1514
1515 if (unlikely(block_dump)) {
1516 char b[BDEVNAME_SIZE];
1517 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001518 current->comm, task_pid_nr(current),
Jens Axboebf2de6f2007-09-27 13:01:25 +02001519 (rw & WRITE) ? "WRITE" : "READ",
1520 (unsigned long long)bio->bi_sector,
Jens Axboe6728cb02008-01-31 13:03:55 +01001521 bdevname(bio->bi_bdev, b));
Jens Axboebf2de6f2007-09-27 13:01:25 +02001522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 }
1524
1525 generic_make_request(bio);
1526}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527EXPORT_SYMBOL(submit_bio);
1528
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001529/**
1530 * __end_that_request_first - end I/O on a request
1531 * @req: the request being processed
Randy Dunlap710027a2008-08-19 20:13:11 +02001532 * @error: %0 for success, < %0 for error
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001533 * @nr_bytes: number of bytes to complete
1534 *
1535 * Description:
1536 * Ends I/O on a number of bytes attached to @req, and sets it up
1537 * for the next range of segments (if any) in the cluster.
1538 *
1539 * Return:
Randy Dunlap710027a2008-08-19 20:13:11 +02001540 * %0 - we are done with this request, call end_that_request_last()
1541 * %1 - still buffers pending for this request
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001542 **/
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001543static int __end_that_request_first(struct request *req, int error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 int nr_bytes)
1545{
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001546 int total_bytes, bio_nbytes, next_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 struct bio *bio;
1548
Jens Axboe2056a782006-03-23 20:00:26 +01001549 blk_add_trace_rq(req->q, req, BLK_TA_COMPLETE);
1550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 /*
Randy Dunlap710027a2008-08-19 20:13:11 +02001552 * for a REQ_TYPE_BLOCK_PC request, we want to carry any eventual
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 * sense key with us all the way through
1554 */
1555 if (!blk_pc_request(req))
1556 req->errors = 0;
1557
Jens Axboe6728cb02008-01-31 13:03:55 +01001558 if (error && (blk_fs_request(req) && !(req->cmd_flags & REQ_QUIET))) {
1559 printk(KERN_ERR "end_request: I/O error, dev %s, sector %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 req->rq_disk ? req->rq_disk->disk_name : "?",
1561 (unsigned long long)req->sector);
1562 }
1563
Jens Axboed72d9042005-11-01 08:35:42 +01001564 if (blk_fs_request(req) && req->rq_disk) {
Jens Axboea3623572005-11-01 09:26:16 +01001565 const int rw = rq_data_dir(req);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001566 struct hd_struct *part;
Tejun Heoc9959052008-08-25 19:47:21 +09001567 int cpu;
Jens Axboea3623572005-11-01 09:26:16 +01001568
Tejun Heoc9959052008-08-25 19:47:21 +09001569 cpu = disk_stat_lock();
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001570 part = disk_map_sector_rcu(req->rq_disk, req->sector);
Tejun Heoc9959052008-08-25 19:47:21 +09001571 all_stat_add(cpu, req->rq_disk, part, sectors[rw],
1572 nr_bytes >> 9, req->sector);
1573 disk_stat_unlock();
Jens Axboed72d9042005-11-01 08:35:42 +01001574 }
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 total_bytes = bio_nbytes = 0;
1577 while ((bio = req->bio) != NULL) {
1578 int nbytes;
1579
Jens Axboebf2de6f2007-09-27 13:01:25 +02001580 /*
1581 * For an empty barrier request, the low level driver must
1582 * store a potential error location in ->sector. We pass
1583 * that back up in ->bi_sector.
1584 */
1585 if (blk_empty_barrier(req))
1586 bio->bi_sector = req->sector;
1587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 if (nr_bytes >= bio->bi_size) {
1589 req->bio = bio->bi_next;
1590 nbytes = bio->bi_size;
NeilBrown5bb23a62007-09-27 12:46:13 +02001591 req_bio_endio(req, bio, nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 next_idx = 0;
1593 bio_nbytes = 0;
1594 } else {
1595 int idx = bio->bi_idx + next_idx;
1596
1597 if (unlikely(bio->bi_idx >= bio->bi_vcnt)) {
1598 blk_dump_rq_flags(req, "__end_that");
Jens Axboe6728cb02008-01-31 13:03:55 +01001599 printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
Harvey Harrison24c03d42008-05-01 04:35:17 -07001600 __func__, bio->bi_idx, bio->bi_vcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 break;
1602 }
1603
1604 nbytes = bio_iovec_idx(bio, idx)->bv_len;
1605 BIO_BUG_ON(nbytes > bio->bi_size);
1606
1607 /*
1608 * not a complete bvec done
1609 */
1610 if (unlikely(nbytes > nr_bytes)) {
1611 bio_nbytes += nr_bytes;
1612 total_bytes += nr_bytes;
1613 break;
1614 }
1615
1616 /*
1617 * advance to the next vector
1618 */
1619 next_idx++;
1620 bio_nbytes += nbytes;
1621 }
1622
1623 total_bytes += nbytes;
1624 nr_bytes -= nbytes;
1625
Jens Axboe6728cb02008-01-31 13:03:55 +01001626 bio = req->bio;
1627 if (bio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 /*
1629 * end more in this run, or just return 'not-done'
1630 */
1631 if (unlikely(nr_bytes <= 0))
1632 break;
1633 }
1634 }
1635
1636 /*
1637 * completely done
1638 */
1639 if (!req->bio)
1640 return 0;
1641
1642 /*
1643 * if the request wasn't completed, update state
1644 */
1645 if (bio_nbytes) {
NeilBrown5bb23a62007-09-27 12:46:13 +02001646 req_bio_endio(req, bio, bio_nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 bio->bi_idx += next_idx;
1648 bio_iovec(bio)->bv_offset += nr_bytes;
1649 bio_iovec(bio)->bv_len -= nr_bytes;
1650 }
1651
1652 blk_recalc_rq_sectors(req, total_bytes >> 9);
1653 blk_recalc_rq_segments(req);
1654 return 1;
1655}
1656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657/*
Jens Axboeff856ba2006-01-09 16:02:34 +01001658 * splice the completion data to a local structure and hand off to
1659 * process_completion_queue() to complete the requests
1660 */
1661static void blk_done_softirq(struct softirq_action *h)
1662{
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07001663 struct list_head *cpu_list, local_list;
Jens Axboeff856ba2006-01-09 16:02:34 +01001664
1665 local_irq_disable();
1666 cpu_list = &__get_cpu_var(blk_cpu_done);
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07001667 list_replace_init(cpu_list, &local_list);
Jens Axboeff856ba2006-01-09 16:02:34 +01001668 local_irq_enable();
1669
1670 while (!list_empty(&local_list)) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001671 struct request *rq;
Jens Axboeff856ba2006-01-09 16:02:34 +01001672
Jens Axboe6728cb02008-01-31 13:03:55 +01001673 rq = list_entry(local_list.next, struct request, donelist);
Jens Axboeff856ba2006-01-09 16:02:34 +01001674 list_del_init(&rq->donelist);
1675 rq->q->softirq_done_fn(rq);
1676 }
1677}
1678
Jens Axboe6728cb02008-01-31 13:03:55 +01001679static int __cpuinit blk_cpu_notify(struct notifier_block *self,
1680 unsigned long action, void *hcpu)
Jens Axboeff856ba2006-01-09 16:02:34 +01001681{
1682 /*
1683 * If a CPU goes away, splice its entries to the current CPU
1684 * and trigger a run of the softirq
1685 */
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001686 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
Jens Axboeff856ba2006-01-09 16:02:34 +01001687 int cpu = (unsigned long) hcpu;
1688
1689 local_irq_disable();
1690 list_splice_init(&per_cpu(blk_cpu_done, cpu),
1691 &__get_cpu_var(blk_cpu_done));
1692 raise_softirq_irqoff(BLOCK_SOFTIRQ);
1693 local_irq_enable();
1694 }
1695
1696 return NOTIFY_OK;
1697}
1698
1699
Satyam Sharmadb47d472007-08-23 09:29:40 +02001700static struct notifier_block blk_cpu_notifier __cpuinitdata = {
Jens Axboeff856ba2006-01-09 16:02:34 +01001701 .notifier_call = blk_cpu_notify,
1702};
1703
Jens Axboeff856ba2006-01-09 16:02:34 +01001704/**
1705 * blk_complete_request - end I/O on a request
1706 * @req: the request being processed
1707 *
1708 * Description:
1709 * Ends all I/O on a request. It does not handle partial completions,
Andreas Mohrd6e05ed2006-06-26 18:35:02 +02001710 * unless the driver actually implements this in its completion callback
Jens Axboe4fa253f2007-07-18 13:13:10 +02001711 * through requeueing. The actual completion happens out-of-order,
Jens Axboeff856ba2006-01-09 16:02:34 +01001712 * through a softirq handler. The user must have registered a completion
1713 * callback through blk_queue_softirq_done().
1714 **/
1715
1716void blk_complete_request(struct request *req)
1717{
1718 struct list_head *cpu_list;
1719 unsigned long flags;
1720
1721 BUG_ON(!req->q->softirq_done_fn);
Jens Axboe6728cb02008-01-31 13:03:55 +01001722
Jens Axboeff856ba2006-01-09 16:02:34 +01001723 local_irq_save(flags);
1724
1725 cpu_list = &__get_cpu_var(blk_cpu_done);
1726 list_add_tail(&req->donelist, cpu_list);
1727 raise_softirq_irqoff(BLOCK_SOFTIRQ);
1728
1729 local_irq_restore(flags);
1730}
Jens Axboeff856ba2006-01-09 16:02:34 +01001731EXPORT_SYMBOL(blk_complete_request);
Jens Axboe6728cb02008-01-31 13:03:55 +01001732
Jens Axboeff856ba2006-01-09 16:02:34 +01001733/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 * queue lock must be held
1735 */
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001736static void end_that_request_last(struct request *req, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
1738 struct gendisk *disk = req->rq_disk;
Tejun Heo8ffdc652006-01-06 09:49:03 +01001739
Kiyoshi Uedab8286232007-12-11 17:53:24 -05001740 if (blk_rq_tagged(req))
1741 blk_queue_end_tag(req->q, req);
1742
1743 if (blk_queued_rq(req))
1744 blkdev_dequeue_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
1746 if (unlikely(laptop_mode) && blk_fs_request(req))
1747 laptop_io_completion();
1748
Jens Axboefd0ff8a2006-05-23 11:23:49 +02001749 /*
1750 * Account IO completion. bar_rq isn't accounted as a normal
1751 * IO on queueing nor completion. Accounting the containing
1752 * request is enough.
1753 */
1754 if (disk && blk_fs_request(req) && req != &req->q->bar_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 unsigned long duration = jiffies - req->start_time;
Jens Axboea3623572005-11-01 09:26:16 +01001756 const int rw = rq_data_dir(req);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001757 struct hd_struct *part;
Tejun Heoc9959052008-08-25 19:47:21 +09001758 int cpu;
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001759
Tejun Heoc9959052008-08-25 19:47:21 +09001760 cpu = disk_stat_lock();
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001761 part = disk_map_sector_rcu(disk, req->sector);
Jens Axboea3623572005-11-01 09:26:16 +01001762
Tejun Heoc9959052008-08-25 19:47:21 +09001763 all_stat_inc(cpu, disk, part, ios[rw], req->sector);
1764 all_stat_add(cpu, disk, part, ticks[rw], duration, req->sector);
1765 disk_round_stats(cpu, disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 disk->in_flight--;
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001767 if (part) {
Tejun Heoc9959052008-08-25 19:47:21 +09001768 part_round_stats(cpu, part);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001769 part->in_flight--;
1770 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001771
Tejun Heoc9959052008-08-25 19:47:21 +09001772 disk_stat_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 }
Kiyoshi Uedab8286232007-12-11 17:53:24 -05001774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 if (req->end_io)
Tejun Heo8ffdc652006-01-06 09:49:03 +01001776 req->end_io(req, error);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05001777 else {
1778 if (blk_bidi_rq(req))
1779 __blk_put_request(req->next_rq->q, req->next_rq);
1780
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 __blk_put_request(req->q, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 }
1783}
1784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785static inline void __end_request(struct request *rq, int uptodate,
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05001786 unsigned int nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05001788 int error = 0;
1789
1790 if (uptodate <= 0)
1791 error = uptodate ? uptodate : -EIO;
1792
1793 __blk_end_request(rq, error, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794}
1795
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05001796/**
1797 * blk_rq_bytes - Returns bytes left to complete in the entire request
Randy Dunlap5d87a052008-02-20 09:01:22 +01001798 * @rq: the request being processed
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05001799 **/
1800unsigned int blk_rq_bytes(struct request *rq)
Jens Axboea0cd1282007-09-21 10:41:07 +02001801{
1802 if (blk_fs_request(rq))
1803 return rq->hard_nr_sectors << 9;
1804
1805 return rq->data_len;
1806}
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05001807EXPORT_SYMBOL_GPL(blk_rq_bytes);
1808
1809/**
1810 * blk_rq_cur_bytes - Returns bytes left to complete in the current segment
Randy Dunlap5d87a052008-02-20 09:01:22 +01001811 * @rq: the request being processed
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05001812 **/
1813unsigned int blk_rq_cur_bytes(struct request *rq)
1814{
1815 if (blk_fs_request(rq))
1816 return rq->current_nr_sectors << 9;
1817
1818 if (rq->bio)
1819 return rq->bio->bi_size;
1820
1821 return rq->data_len;
1822}
1823EXPORT_SYMBOL_GPL(blk_rq_cur_bytes);
Jens Axboea0cd1282007-09-21 10:41:07 +02001824
1825/**
1826 * end_queued_request - end all I/O on a queued request
1827 * @rq: the request being processed
Randy Dunlap710027a2008-08-19 20:13:11 +02001828 * @uptodate: error value or %0/%1 uptodate flag
Jens Axboea0cd1282007-09-21 10:41:07 +02001829 *
1830 * Description:
1831 * Ends all I/O on a request, and removes it from the block layer queues.
Randy Dunlap710027a2008-08-19 20:13:11 +02001832 * Not suitable for normal I/O completion, unless the driver still has
Jens Axboea0cd1282007-09-21 10:41:07 +02001833 * the request attached to the block layer.
1834 *
1835 **/
1836void end_queued_request(struct request *rq, int uptodate)
1837{
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05001838 __end_request(rq, uptodate, blk_rq_bytes(rq));
Jens Axboea0cd1282007-09-21 10:41:07 +02001839}
1840EXPORT_SYMBOL(end_queued_request);
1841
1842/**
1843 * end_dequeued_request - end all I/O on a dequeued request
1844 * @rq: the request being processed
Randy Dunlap710027a2008-08-19 20:13:11 +02001845 * @uptodate: error value or %0/%1 uptodate flag
Jens Axboea0cd1282007-09-21 10:41:07 +02001846 *
1847 * Description:
1848 * Ends all I/O on a request. The request must already have been
1849 * dequeued using blkdev_dequeue_request(), as is normally the case
1850 * for most drivers.
1851 *
1852 **/
1853void end_dequeued_request(struct request *rq, int uptodate)
1854{
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05001855 __end_request(rq, uptodate, blk_rq_bytes(rq));
Jens Axboea0cd1282007-09-21 10:41:07 +02001856}
1857EXPORT_SYMBOL(end_dequeued_request);
1858
1859
1860/**
1861 * end_request - end I/O on the current segment of the request
Randy Dunlap8f731f72007-10-18 23:39:28 -07001862 * @req: the request being processed
Randy Dunlap710027a2008-08-19 20:13:11 +02001863 * @uptodate: error value or %0/%1 uptodate flag
Jens Axboea0cd1282007-09-21 10:41:07 +02001864 *
1865 * Description:
1866 * Ends I/O on the current segment of a request. If that is the only
1867 * remaining segment, the request is also completed and freed.
1868 *
Randy Dunlap710027a2008-08-19 20:13:11 +02001869 * This is a remnant of how older block drivers handled I/O completions.
1870 * Modern drivers typically end I/O on the full request in one go, unless
Jens Axboea0cd1282007-09-21 10:41:07 +02001871 * they have a residual value to account for. For that case this function
1872 * isn't really useful, unless the residual just happens to be the
1873 * full current segment. In other words, don't use this function in new
1874 * code. Either use end_request_completely(), or the
1875 * end_that_request_chunk() (along with end_that_request_last()) for
1876 * partial completions.
1877 *
1878 **/
1879void end_request(struct request *req, int uptodate)
1880{
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05001881 __end_request(req, uptodate, req->hard_cur_sectors << 9);
Jens Axboea0cd1282007-09-21 10:41:07 +02001882}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883EXPORT_SYMBOL(end_request);
1884
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001885/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001886 * blk_end_io - Generic end_io function to complete a request.
1887 * @rq: the request being processed
Randy Dunlap710027a2008-08-19 20:13:11 +02001888 * @error: %0 for success, < %0 for error
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001889 * @nr_bytes: number of bytes to complete @rq
1890 * @bidi_bytes: number of bytes to complete @rq->next_rq
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001891 * @drv_callback: function called between completion of bios in the request
1892 * and completion of the request.
Randy Dunlap710027a2008-08-19 20:13:11 +02001893 * If the callback returns non %0, this helper returns without
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001894 * completion of the request.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001895 *
1896 * Description:
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001897 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001898 * If @rq has leftover, sets it up for the next range of segments.
1899 *
1900 * Return:
Randy Dunlap710027a2008-08-19 20:13:11 +02001901 * %0 - we are done with this request
1902 * %1 - this request is not freed yet, it still has pending buffers.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001903 **/
Jens Axboe22b13212008-01-31 12:36:19 +01001904static int blk_end_io(struct request *rq, int error, unsigned int nr_bytes,
1905 unsigned int bidi_bytes,
1906 int (drv_callback)(struct request *))
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001907{
1908 struct request_queue *q = rq->q;
1909 unsigned long flags = 0UL;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001910
David Woodhousefb2dce82008-08-05 18:01:53 +01001911 if (bio_has_data(rq->bio) || blk_discard_rq(rq)) {
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001912 if (__end_that_request_first(rq, error, nr_bytes))
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001913 return 1;
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001914
1915 /* Bidi request must be completed as a whole */
1916 if (blk_bidi_rq(rq) &&
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001917 __end_that_request_first(rq->next_rq, error, bidi_bytes))
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001918 return 1;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001919 }
1920
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001921 /* Special feature for tricky drivers */
1922 if (drv_callback && drv_callback(rq))
1923 return 1;
1924
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001925 add_disk_randomness(rq->rq_disk);
1926
1927 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05001928 end_that_request_last(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001929 spin_unlock_irqrestore(q->queue_lock, flags);
1930
1931 return 0;
1932}
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001933
1934/**
1935 * blk_end_request - Helper function for drivers to complete the request.
1936 * @rq: the request being processed
Randy Dunlap710027a2008-08-19 20:13:11 +02001937 * @error: %0 for success, < %0 for error
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001938 * @nr_bytes: number of bytes to complete
1939 *
1940 * Description:
1941 * Ends I/O on a number of bytes attached to @rq.
1942 * If @rq has leftover, sets it up for the next range of segments.
1943 *
1944 * Return:
Randy Dunlap710027a2008-08-19 20:13:11 +02001945 * %0 - we are done with this request
1946 * %1 - still buffers pending for this request
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001947 **/
Jens Axboe22b13212008-01-31 12:36:19 +01001948int blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001949{
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001950 return blk_end_io(rq, error, nr_bytes, 0, NULL);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001951}
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001952EXPORT_SYMBOL_GPL(blk_end_request);
1953
1954/**
1955 * __blk_end_request - Helper function for drivers to complete the request.
1956 * @rq: the request being processed
Randy Dunlap710027a2008-08-19 20:13:11 +02001957 * @error: %0 for success, < %0 for error
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001958 * @nr_bytes: number of bytes to complete
1959 *
1960 * Description:
1961 * Must be called with queue lock held unlike blk_end_request().
1962 *
1963 * Return:
Randy Dunlap710027a2008-08-19 20:13:11 +02001964 * %0 - we are done with this request
1965 * %1 - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001966 **/
Jens Axboe22b13212008-01-31 12:36:19 +01001967int __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001968{
David Woodhousefb2dce82008-08-05 18:01:53 +01001969 if ((bio_has_data(rq->bio) || blk_discard_rq(rq)) &&
Jens Axboe051cc392008-08-08 11:06:45 +02001970 __end_that_request_first(rq, error, nr_bytes))
1971 return 1;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001972
1973 add_disk_randomness(rq->rq_disk);
1974
Kiyoshi Uedab8286232007-12-11 17:53:24 -05001975 end_that_request_last(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001976
1977 return 0;
1978}
1979EXPORT_SYMBOL_GPL(__blk_end_request);
1980
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001981/**
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001982 * blk_end_bidi_request - Helper function for drivers to complete bidi request.
1983 * @rq: the bidi request being processed
Randy Dunlap710027a2008-08-19 20:13:11 +02001984 * @error: %0 for success, < %0 for error
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001985 * @nr_bytes: number of bytes to complete @rq
1986 * @bidi_bytes: number of bytes to complete @rq->next_rq
1987 *
1988 * Description:
1989 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
1990 *
1991 * Return:
Randy Dunlap710027a2008-08-19 20:13:11 +02001992 * %0 - we are done with this request
1993 * %1 - still buffers pending for this request
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001994 **/
Jens Axboe22b13212008-01-31 12:36:19 +01001995int blk_end_bidi_request(struct request *rq, int error, unsigned int nr_bytes,
1996 unsigned int bidi_bytes)
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001997{
1998 return blk_end_io(rq, error, nr_bytes, bidi_bytes, NULL);
1999}
2000EXPORT_SYMBOL_GPL(blk_end_bidi_request);
2001
2002/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002003 * blk_end_request_callback - Special helper function for tricky drivers
2004 * @rq: the request being processed
Randy Dunlap710027a2008-08-19 20:13:11 +02002005 * @error: %0 for success, < %0 for error
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002006 * @nr_bytes: number of bytes to complete
2007 * @drv_callback: function called between completion of bios in the request
2008 * and completion of the request.
Randy Dunlap710027a2008-08-19 20:13:11 +02002009 * If the callback returns non %0, this helper returns without
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002010 * completion of the request.
2011 *
2012 * Description:
2013 * Ends I/O on a number of bytes attached to @rq.
2014 * If @rq has leftover, sets it up for the next range of segments.
2015 *
2016 * This special helper function is used only for existing tricky drivers.
2017 * (e.g. cdrom_newpc_intr() of ide-cd)
2018 * This interface will be removed when such drivers are rewritten.
2019 * Don't use this interface in other places anymore.
2020 *
2021 * Return:
Randy Dunlap710027a2008-08-19 20:13:11 +02002022 * %0 - we are done with this request
2023 * %1 - this request is not freed yet.
2024 * this request still has pending buffers or
2025 * the driver doesn't want to finish this request yet.
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002026 **/
Jens Axboe22b13212008-01-31 12:36:19 +01002027int blk_end_request_callback(struct request *rq, int error,
2028 unsigned int nr_bytes,
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002029 int (drv_callback)(struct request *))
2030{
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002031 return blk_end_io(rq, error, nr_bytes, 0, drv_callback);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002032}
2033EXPORT_SYMBOL_GPL(blk_end_request_callback);
2034
Jens Axboe86db1e22008-01-29 14:53:40 +01002035void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2036 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037{
David Woodhoused628eae2008-08-09 16:22:17 +01002038 /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw, and
2039 we want BIO_RW_AHEAD (bit 1) to imply REQ_FAILFAST (bit 1). */
Jens Axboe4aff5e22006-08-10 08:44:47 +02002040 rq->cmd_flags |= (bio->bi_rw & 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
David Woodhousefb2dce82008-08-05 18:01:53 +01002042 if (bio_has_data(bio)) {
2043 rq->nr_phys_segments = bio_phys_segments(q, bio);
David Woodhousefb2dce82008-08-05 18:01:53 +01002044 rq->buffer = bio_data(bio);
2045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 rq->current_nr_sectors = bio_cur_sectors(bio);
2047 rq->hard_cur_sectors = rq->current_nr_sectors;
2048 rq->hard_nr_sectors = rq->nr_sectors = bio_sectors(bio);
Mike Christie0e75f902006-12-01 10:40:55 +01002049 rq->data_len = bio->bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
2051 rq->bio = rq->biotail = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
NeilBrown66846572007-08-16 13:31:28 +02002053 if (bio->bi_bdev)
2054 rq->rq_disk = bio->bi_bdev->bd_disk;
2055}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
2057int kblockd_schedule_work(struct work_struct *work)
2058{
2059 return queue_work(kblockd_workqueue, work);
2060}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061EXPORT_SYMBOL(kblockd_schedule_work);
2062
Andrew Morton19a75d82007-05-09 02:33:56 -07002063void kblockd_flush_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064{
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002065 cancel_work_sync(work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066}
Andrew Morton19a75d82007-05-09 02:33:56 -07002067EXPORT_SYMBOL(kblockd_flush_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
2069int __init blk_dev_init(void)
2070{
Jens Axboeff856ba2006-01-09 16:02:34 +01002071 int i;
2072
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 kblockd_workqueue = create_workqueue("kblockd");
2074 if (!kblockd_workqueue)
2075 panic("Failed to create kblockd\n");
2076
2077 request_cachep = kmem_cache_create("blkdev_requests",
Paul Mundt20c2df82007-07-20 10:11:58 +09002078 sizeof(struct request), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Jens Axboe8324aa92008-01-29 14:51:59 +01002080 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02002081 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002083 for_each_possible_cpu(i)
Jens Axboeff856ba2006-01-09 16:02:34 +01002084 INIT_LIST_HEAD(&per_cpu(blk_cpu_done, i));
2085
Carlos R. Mafra962cf362008-05-15 11:15:37 -03002086 open_softirq(BLOCK_SOFTIRQ, blk_done_softirq);
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07002087 register_hotcpu_notifier(&blk_cpu_notifier);
Jens Axboeff856ba2006-01-09 16:02:34 +01002088
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 return 0;
2090}
2091