blob: 98138f0025240caa4b5aaeca28603a982124a9ad [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 1994, Karl Keyte: Added support for disk statistics
4 * Elevator latency, (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
5 * Queue request tables / lock, selectable elevator, Jens Axboe <axboe@suse.de>
Jens Axboe6728cb02008-01-31 13:03:55 +01006 * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au>
7 * - July2000
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
9 */
10
11/*
12 * This handles all read/write requests to block devices
13 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/backing-dev.h>
17#include <linux/bio.h>
18#include <linux/blkdev.h>
19#include <linux/highmem.h>
20#include <linux/mm.h>
21#include <linux/kernel_stat.h>
22#include <linux/string.h>
23#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/completion.h>
25#include <linux/slab.h>
26#include <linux/swap.h>
27#include <linux/writeback.h>
Andrew Mortonfaccbd4b2006-12-10 02:19:35 -080028#include <linux/task_io_accounting_ops.h>
Jens Axboeff856ba2006-01-09 16:02:34 +010029#include <linux/interrupt.h>
30#include <linux/cpu.h>
Jens Axboe2056a782006-03-23 20:00:26 +010031#include <linux/blktrace_api.h>
Akinobu Mitac17bb492006-12-08 02:39:46 -080032#include <linux/fault-inject.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Jens Axboe8324aa92008-01-29 14:51:59 +010034#include "blk.h"
35
Jens Axboe165125e2007-07-24 09:28:11 +020036static int __make_request(struct request_queue *q, struct bio *bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/*
39 * For the allocated request tables
40 */
Adrian Bunk5ece6c52008-02-18 13:45:51 +010041static struct kmem_cache *request_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/*
44 * For queue allocation
45 */
Jens Axboe6728cb02008-01-31 13:03:55 +010046struct kmem_cache *blk_requestq_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * Controlling structure to kblockd
50 */
Jens Axboeff856ba2006-01-09 16:02:34 +010051static struct workqueue_struct *kblockd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Jens Axboeff856ba2006-01-09 16:02:34 +010053static DEFINE_PER_CPU(struct list_head, blk_cpu_done);
54
Jens Axboe26b82562008-01-29 13:54:41 +010055static void drive_stat_acct(struct request *rq, int new_io)
56{
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 Heo074a7ac2008-08-25 19:56:14 +090064 cpu = part_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 Heo074a7ac2008-08-25 19:56:14 +090068 part_stat_inc(cpu, part, merges[rw]);
Jens Axboe28f13702008-05-07 10:15:46 +020069 else {
Tejun Heo074a7ac2008-08-25 19:56:14 +090070 part_round_stats(cpu, part);
71 part_inc_in_flight(part);
Jens Axboe26b82562008-01-29 13:54:41 +010072 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +020073
Tejun Heo074a7ac2008-08-25 19:56:14 +090074 part_stat_unlock();
Jens Axboe26b82562008-01-29 13:54:41 +010075}
76
Jens Axboe8324aa92008-01-29 14:51:59 +010077void blk_queue_congestion_threshold(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
79 int nr;
80
81 nr = q->nr_requests - (q->nr_requests / 8) + 1;
82 if (nr > q->nr_requests)
83 nr = q->nr_requests;
84 q->nr_congestion_on = nr;
85
86 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
87 if (nr < 1)
88 nr = 1;
89 q->nr_congestion_off = nr;
90}
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/**
93 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
94 * @bdev: device
95 *
96 * Locates the passed device's request queue and returns the address of its
97 * backing_dev_info
98 *
99 * Will return NULL if the request queue cannot be located.
100 */
101struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
102{
103 struct backing_dev_info *ret = NULL;
Jens Axboe165125e2007-07-24 09:28:11 +0200104 struct request_queue *q = bdev_get_queue(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 if (q)
107 ret = &q->backing_dev_info;
108 return ret;
109}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110EXPORT_SYMBOL(blk_get_backing_dev_info);
111
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200112void blk_rq_init(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200114 memset(rq, 0, sizeof(*rq));
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 INIT_LIST_HEAD(&rq->queuelist);
Jens Axboeff856ba2006-01-09 16:02:34 +0100117 INIT_LIST_HEAD(&rq->donelist);
Jens Axboe63a71382008-02-08 12:41:03 +0100118 rq->q = q;
119 rq->sector = rq->hard_sector = (sector_t) -1;
Jens Axboe2e662b62006-07-13 11:55:04 +0200120 INIT_HLIST_NODE(&rq->hash);
121 RB_CLEAR_NODE(&rq->rb_node);
FUJITA Tomonorid7e3c322008-04-29 09:54:39 +0200122 rq->cmd = rq->__cmd;
Jens Axboe63a71382008-02-08 12:41:03 +0100123 rq->tag = -1;
Jens Axboe63a71382008-02-08 12:41:03 +0100124 rq->ref_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200126EXPORT_SYMBOL(blk_rq_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
NeilBrown5bb23a62007-09-27 12:46:13 +0200128static void req_bio_endio(struct request *rq, struct bio *bio,
129 unsigned int nbytes, int error)
Tejun Heo797e7db2006-01-06 09:51:03 +0100130{
Jens Axboe165125e2007-07-24 09:28:11 +0200131 struct request_queue *q = rq->q;
Tejun Heo797e7db2006-01-06 09:51:03 +0100132
NeilBrown5bb23a62007-09-27 12:46:13 +0200133 if (&q->bar_rq != rq) {
134 if (error)
135 clear_bit(BIO_UPTODATE, &bio->bi_flags);
136 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
137 error = -EIO;
Tejun Heo797e7db2006-01-06 09:51:03 +0100138
NeilBrown5bb23a62007-09-27 12:46:13 +0200139 if (unlikely(nbytes > bio->bi_size)) {
Jens Axboe6728cb02008-01-31 13:03:55 +0100140 printk(KERN_ERR "%s: want %u bytes done, %u left\n",
Harvey Harrison24c03d42008-05-01 04:35:17 -0700141 __func__, nbytes, bio->bi_size);
NeilBrown5bb23a62007-09-27 12:46:13 +0200142 nbytes = bio->bi_size;
143 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100144
NeilBrown5bb23a62007-09-27 12:46:13 +0200145 bio->bi_size -= nbytes;
146 bio->bi_sector += (nbytes >> 9);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200147
148 if (bio_integrity(bio))
149 bio_integrity_advance(bio, nbytes);
150
NeilBrown5bb23a62007-09-27 12:46:13 +0200151 if (bio->bi_size == 0)
NeilBrown6712ecf2007-09-27 12:47:43 +0200152 bio_endio(bio, error);
NeilBrown5bb23a62007-09-27 12:46:13 +0200153 } else {
154
155 /*
156 * Okay, this is the barrier request in progress, just
157 * record the error;
158 */
159 if (error && !q->orderr)
160 q->orderr = error;
161 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100162}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164void blk_dump_rq_flags(struct request *rq, char *msg)
165{
166 int bit;
167
Jens Axboe6728cb02008-01-31 13:03:55 +0100168 printk(KERN_INFO "%s: dev %s: type=%x, flags=%x\n", msg,
Jens Axboe4aff5e22006-08-10 08:44:47 +0200169 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
170 rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Jens Axboe6728cb02008-01-31 13:03:55 +0100172 printk(KERN_INFO " sector %llu, nr/cnr %lu/%u\n",
173 (unsigned long long)rq->sector,
174 rq->nr_sectors,
175 rq->current_nr_sectors);
176 printk(KERN_INFO " bio %p, biotail %p, buffer %p, data %p, len %u\n",
177 rq->bio, rq->biotail,
178 rq->buffer, rq->data,
179 rq->data_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Jens Axboe4aff5e22006-08-10 08:44:47 +0200181 if (blk_pc_request(rq)) {
Jens Axboe6728cb02008-01-31 13:03:55 +0100182 printk(KERN_INFO " cdb: ");
FUJITA Tomonorid34c87e2008-04-29 14:37:52 +0200183 for (bit = 0; bit < BLK_MAX_CDB; bit++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 printk("%02x ", rq->cmd[bit]);
185 printk("\n");
186 }
187}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188EXPORT_SYMBOL(blk_dump_rq_flags);
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/*
191 * "plug" the device if there are no outstanding requests: this will
192 * force the transfer to start only after we have put all the requests
193 * on the list.
194 *
195 * This is called with interrupts off and no requests on the queue and
196 * with the queue lock held.
197 */
Jens Axboe165125e2007-07-24 09:28:11 +0200198void blk_plug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 WARN_ON(!irqs_disabled());
201
202 /*
203 * don't plug a stopped queue, it must be paired with blk_start_queue()
204 * which will restart the queueing
205 */
Coywolf Qi Hunt7daac492006-04-19 10:14:49 +0200206 if (blk_queue_stopped(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return;
208
Jens Axboee48ec692008-07-03 13:18:54 +0200209 if (!queue_flag_test_and_set(QUEUE_FLAG_PLUGGED, q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
Jens Axboe2056a782006-03-23 20:00:26 +0100211 blk_add_trace_generic(q, NULL, 0, BLK_TA_PLUG);
212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214EXPORT_SYMBOL(blk_plug_device);
215
Jens Axboe6c5e0c42008-08-01 20:31:32 +0200216/**
217 * blk_plug_device_unlocked - plug a device without queue lock held
218 * @q: The &struct request_queue to plug
219 *
220 * Description:
221 * Like @blk_plug_device(), but grabs the queue lock and disables
222 * interrupts.
223 **/
224void blk_plug_device_unlocked(struct request_queue *q)
225{
226 unsigned long flags;
227
228 spin_lock_irqsave(q->queue_lock, flags);
229 blk_plug_device(q);
230 spin_unlock_irqrestore(q->queue_lock, flags);
231}
232EXPORT_SYMBOL(blk_plug_device_unlocked);
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234/*
235 * remove the queue from the plugged list, if present. called with
236 * queue lock held and interrupts disabled.
237 */
Jens Axboe165125e2007-07-24 09:28:11 +0200238int blk_remove_plug(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 WARN_ON(!irqs_disabled());
241
Jens Axboee48ec692008-07-03 13:18:54 +0200242 if (!queue_flag_test_and_clear(QUEUE_FLAG_PLUGGED, q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return 0;
244
245 del_timer(&q->unplug_timer);
246 return 1;
247}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248EXPORT_SYMBOL(blk_remove_plug);
249
250/*
251 * remove the plug and let it rip..
252 */
Jens Axboe165125e2007-07-24 09:28:11 +0200253void __generic_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Coywolf Qi Hunt7daac492006-04-19 10:14:49 +0200255 if (unlikely(blk_queue_stopped(q)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return;
257
258 if (!blk_remove_plug(q))
259 return;
260
Jens Axboe22e2c502005-06-27 10:55:12 +0200261 q->request_fn(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263EXPORT_SYMBOL(__generic_unplug_device);
264
265/**
266 * generic_unplug_device - fire a request queue
Jens Axboe165125e2007-07-24 09:28:11 +0200267 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 *
269 * Description:
270 * Linux uses plugging to build bigger requests queues before letting
271 * the device have at them. If a queue is plugged, the I/O scheduler
272 * is still adding and merging requests on the queue. Once the queue
273 * gets unplugged, the request_fn defined for the queue is invoked and
274 * transfers started.
275 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200276void generic_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Jens Axboedbaf2c02008-05-07 09:48:17 +0200278 if (blk_queue_plugged(q)) {
279 spin_lock_irq(q->queue_lock);
280 __generic_unplug_device(q);
281 spin_unlock_irq(q->queue_lock);
282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284EXPORT_SYMBOL(generic_unplug_device);
285
286static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
287 struct page *page)
288{
Jens Axboe165125e2007-07-24 09:28:11 +0200289 struct request_queue *q = bdi->unplug_io_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500291 blk_unplug(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
Jens Axboe86db1e22008-01-29 14:53:40 +0100294void blk_unplug_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Jens Axboe165125e2007-07-24 09:28:11 +0200296 struct request_queue *q =
297 container_of(work, struct request_queue, unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Jens Axboe2056a782006-03-23 20:00:26 +0100299 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
300 q->rq.count[READ] + q->rq.count[WRITE]);
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 q->unplug_fn(q);
303}
304
Jens Axboe86db1e22008-01-29 14:53:40 +0100305void blk_unplug_timeout(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Jens Axboe165125e2007-07-24 09:28:11 +0200307 struct request_queue *q = (struct request_queue *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Jens Axboe2056a782006-03-23 20:00:26 +0100309 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_TIMER, NULL,
310 q->rq.count[READ] + q->rq.count[WRITE]);
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 kblockd_schedule_work(&q->unplug_work);
313}
314
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500315void blk_unplug(struct request_queue *q)
316{
317 /*
318 * devices don't necessarily have an ->unplug_fn defined
319 */
320 if (q->unplug_fn) {
321 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
322 q->rq.count[READ] + q->rq.count[WRITE]);
323
324 q->unplug_fn(q);
325 }
326}
327EXPORT_SYMBOL(blk_unplug);
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329/**
330 * blk_start_queue - restart a previously stopped queue
Jens Axboe165125e2007-07-24 09:28:11 +0200331 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 *
333 * Description:
334 * blk_start_queue() will clear the stop flag on the queue, and call
335 * the request_fn for the queue if it was in a stopped state when
336 * entered. Also see blk_stop_queue(). Queue lock must be held.
337 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200338void blk_start_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200340 WARN_ON(!irqs_disabled());
341
Nick Piggin75ad23b2008-04-29 14:48:33 +0200342 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 /*
345 * one level of recursion is ok and is much faster than kicking
346 * the unplug handling
347 */
Jens Axboee48ec692008-07-03 13:18:54 +0200348 if (!queue_flag_test_and_set(QUEUE_FLAG_REENTER, q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 q->request_fn(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200350 queue_flag_clear(QUEUE_FLAG_REENTER, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 } else {
352 blk_plug_device(q);
353 kblockd_schedule_work(&q->unplug_work);
354 }
355}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356EXPORT_SYMBOL(blk_start_queue);
357
358/**
359 * blk_stop_queue - stop a queue
Jens Axboe165125e2007-07-24 09:28:11 +0200360 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 *
362 * Description:
363 * The Linux block layer assumes that a block driver will consume all
364 * entries on the request queue when the request_fn strategy is called.
365 * Often this will not happen, because of hardware limitations (queue
366 * depth settings). If a device driver gets a 'queue full' response,
367 * or if it simply chooses not to queue more I/O at one point, it can
368 * call this function to prevent the request_fn from being called until
369 * the driver has signalled it's ready to go again. This happens by calling
370 * blk_start_queue() to restart queue operations. Queue lock must be held.
371 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200372void blk_stop_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 blk_remove_plug(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200375 queue_flag_set(QUEUE_FLAG_STOPPED, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377EXPORT_SYMBOL(blk_stop_queue);
378
379/**
380 * blk_sync_queue - cancel any pending callbacks on a queue
381 * @q: the queue
382 *
383 * Description:
384 * The block layer may perform asynchronous callback activity
385 * on a queue, such as calling the unplug function after a timeout.
386 * A block device may call blk_sync_queue to ensure that any
387 * such activity is cancelled, thus allowing it to release resources
Michael Opdenacker59c51592007-05-09 08:57:56 +0200388 * that the callbacks might use. The caller must already have made sure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 * that its ->make_request_fn will not re-add plugging prior to calling
390 * this function.
391 *
392 */
393void blk_sync_queue(struct request_queue *q)
394{
395 del_timer_sync(&q->unplug_timer);
Oleg Nesterovabbeb882007-10-23 15:08:19 +0200396 kblockd_flush_work(&q->unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398EXPORT_SYMBOL(blk_sync_queue);
399
400/**
401 * blk_run_queue - run a single device queue
402 * @q: The queue to run
403 */
Nick Piggin75ad23b2008-04-29 14:48:33 +0200404void __blk_run_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 blk_remove_plug(q);
Jens Axboedac07ec2006-05-11 08:20:16 +0200407
408 /*
409 * Only recurse once to avoid overrunning the stack, let the unplug
410 * handling reinvoke the handler shortly if we already got there.
411 */
412 if (!elv_queue_empty(q)) {
Jens Axboee48ec692008-07-03 13:18:54 +0200413 if (!queue_flag_test_and_set(QUEUE_FLAG_REENTER, q)) {
Jens Axboedac07ec2006-05-11 08:20:16 +0200414 q->request_fn(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200415 queue_flag_clear(QUEUE_FLAG_REENTER, q);
Jens Axboedac07ec2006-05-11 08:20:16 +0200416 } else {
417 blk_plug_device(q);
418 kblockd_schedule_work(&q->unplug_work);
419 }
420 }
Nick Piggin75ad23b2008-04-29 14:48:33 +0200421}
422EXPORT_SYMBOL(__blk_run_queue);
Jens Axboedac07ec2006-05-11 08:20:16 +0200423
Nick Piggin75ad23b2008-04-29 14:48:33 +0200424/**
425 * blk_run_queue - run a single device queue
426 * @q: The queue to run
427 */
428void blk_run_queue(struct request_queue *q)
429{
430 unsigned long flags;
431
432 spin_lock_irqsave(q->queue_lock, flags);
433 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 spin_unlock_irqrestore(q->queue_lock, flags);
435}
436EXPORT_SYMBOL(blk_run_queue);
437
Jens Axboe165125e2007-07-24 09:28:11 +0200438void blk_put_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500439{
440 kobject_put(&q->kobj);
441}
Al Viro483f4af2006-03-18 18:34:37 -0500442
Jens Axboe6728cb02008-01-31 13:03:55 +0100443void blk_cleanup_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500444{
445 mutex_lock(&q->sysfs_lock);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200446 queue_flag_set_unlocked(QUEUE_FLAG_DEAD, q);
Al Viro483f4af2006-03-18 18:34:37 -0500447 mutex_unlock(&q->sysfs_lock);
448
449 if (q->elevator)
450 elevator_exit(q->elevator);
451
452 blk_put_queue(q);
453}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454EXPORT_SYMBOL(blk_cleanup_queue);
455
Jens Axboe165125e2007-07-24 09:28:11 +0200456static int blk_init_free_list(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
458 struct request_list *rl = &q->rq;
459
460 rl->count[READ] = rl->count[WRITE] = 0;
461 rl->starved[READ] = rl->starved[WRITE] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200462 rl->elvpriv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 init_waitqueue_head(&rl->wait[READ]);
464 init_waitqueue_head(&rl->wait[WRITE]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Christoph Lameter19460892005-06-23 00:08:19 -0700466 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
467 mempool_free_slab, request_cachep, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 if (!rl->rq_pool)
470 return -ENOMEM;
471
472 return 0;
473}
474
Jens Axboe165125e2007-07-24 09:28:11 +0200475struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Christoph Lameter19460892005-06-23 00:08:19 -0700477 return blk_alloc_queue_node(gfp_mask, -1);
478}
479EXPORT_SYMBOL(blk_alloc_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Jens Axboe165125e2007-07-24 09:28:11 +0200481struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -0700482{
Jens Axboe165125e2007-07-24 09:28:11 +0200483 struct request_queue *q;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700484 int err;
Christoph Lameter19460892005-06-23 00:08:19 -0700485
Jens Axboe8324aa92008-01-29 14:51:59 +0100486 q = kmem_cache_alloc_node(blk_requestq_cachep,
Christoph Lameter94f60302007-07-17 04:03:29 -0700487 gfp_mask | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (!q)
489 return NULL;
490
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700491 q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
492 q->backing_dev_info.unplug_io_data = q;
493 err = bdi_init(&q->backing_dev_info);
494 if (err) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100495 kmem_cache_free(blk_requestq_cachep, q);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700496 return NULL;
497 }
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 init_timer(&q->unplug_timer);
Al Viro483f4af2006-03-18 18:34:37 -0500500
Jens Axboe8324aa92008-01-29 14:51:59 +0100501 kobject_init(&q->kobj, &blk_queue_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Al Viro483f4af2006-03-18 18:34:37 -0500503 mutex_init(&q->sysfs_lock);
Neil Browne7e72bf2008-05-14 16:05:54 -0700504 spin_lock_init(&q->__queue_lock);
Al Viro483f4af2006-03-18 18:34:37 -0500505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return q;
507}
Christoph Lameter19460892005-06-23 00:08:19 -0700508EXPORT_SYMBOL(blk_alloc_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510/**
511 * blk_init_queue - prepare a request queue for use with a block device
512 * @rfn: The function to be called to process requests that have been
513 * placed on the queue.
514 * @lock: Request queue spin lock
515 *
516 * Description:
517 * If a block device wishes to use the standard request handling procedures,
518 * which sorts requests and coalesces adjacent requests, then it must
519 * call blk_init_queue(). The function @rfn will be called when there
520 * are requests on the queue that need to be processed. If the device
521 * supports plugging, then @rfn may not be called immediately when requests
522 * are available on the queue, but may be called at some time later instead.
523 * Plugged queues are generally unplugged when a buffer belonging to one
524 * of the requests on the queue is needed, or due to memory pressure.
525 *
526 * @rfn is not required, or even expected, to remove all requests off the
527 * queue, but only as many as it can handle at a time. If it does leave
528 * requests on the queue, it is responsible for arranging that the requests
529 * get dealt with eventually.
530 *
531 * The queue spin lock must be held while manipulating the requests on the
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200532 * request queue; this lock will be taken also from interrupt context, so irq
533 * disabling is needed for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 *
Randy Dunlap710027a2008-08-19 20:13:11 +0200535 * Function returns a pointer to the initialized request queue, or %NULL if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 * it didn't succeed.
537 *
538 * Note:
539 * blk_init_queue() must be paired with a blk_cleanup_queue() call
540 * when the block device is deactivated (such as at module unload).
541 **/
Christoph Lameter19460892005-06-23 00:08:19 -0700542
Jens Axboe165125e2007-07-24 09:28:11 +0200543struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Christoph Lameter19460892005-06-23 00:08:19 -0700545 return blk_init_queue_node(rfn, lock, -1);
546}
547EXPORT_SYMBOL(blk_init_queue);
548
Jens Axboe165125e2007-07-24 09:28:11 +0200549struct request_queue *
Christoph Lameter19460892005-06-23 00:08:19 -0700550blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
551{
Jens Axboe165125e2007-07-24 09:28:11 +0200552 struct request_queue *q = blk_alloc_queue_node(GFP_KERNEL, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 if (!q)
555 return NULL;
556
Christoph Lameter19460892005-06-23 00:08:19 -0700557 q->node = node_id;
Al Viro8669aaf2006-03-18 13:50:00 -0500558 if (blk_init_free_list(q)) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100559 kmem_cache_free(blk_requestq_cachep, q);
Al Viro8669aaf2006-03-18 13:50:00 -0500560 return NULL;
561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
152587d2005-04-12 16:22:06 -0500563 /*
564 * if caller didn't supply a lock, they get per-queue locking with
565 * our embedded lock
566 */
Neil Browne7e72bf2008-05-14 16:05:54 -0700567 if (!lock)
152587d2005-04-12 16:22:06 -0500568 lock = &q->__queue_lock;
152587d2005-04-12 16:22:06 -0500569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 q->request_fn = rfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 q->prep_rq_fn = NULL;
572 q->unplug_fn = generic_unplug_device;
573 q->queue_flags = (1 << QUEUE_FLAG_CLUSTER);
574 q->queue_lock = lock;
575
576 blk_queue_segment_boundary(q, 0xffffffff);
577
578 blk_queue_make_request(q, __make_request);
579 blk_queue_max_segment_size(q, MAX_SEGMENT_SIZE);
580
581 blk_queue_max_hw_segments(q, MAX_HW_SEGMENTS);
582 blk_queue_max_phys_segments(q, MAX_PHYS_SEGMENTS);
583
Alan Stern44ec9542007-02-20 11:01:57 -0500584 q->sg_reserved_size = INT_MAX;
585
FUJITA Tomonoriabf54392008-08-16 14:10:05 +0900586 blk_set_cmd_filter_defaults(&q->cmd_filter);
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 /*
589 * all done
590 */
591 if (!elevator_init(q, NULL)) {
592 blk_queue_congestion_threshold(q);
593 return q;
594 }
595
Al Viro8669aaf2006-03-18 13:50:00 -0500596 blk_put_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return NULL;
598}
Christoph Lameter19460892005-06-23 00:08:19 -0700599EXPORT_SYMBOL(blk_init_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Jens Axboe165125e2007-07-24 09:28:11 +0200601int blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Nick Pigginfde6ad22005-06-23 00:08:53 -0700603 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
Al Viro483f4af2006-03-18 18:34:37 -0500604 kobject_get(&q->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return 0;
606 }
607
608 return 1;
609}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Jens Axboe165125e2007-07-24 09:28:11 +0200611static inline void blk_free_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Jens Axboe4aff5e22006-08-10 08:44:47 +0200613 if (rq->cmd_flags & REQ_ELVPRIV)
Tejun Heocb98fc82005-10-28 08:29:39 +0200614 elv_put_request(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 mempool_free(rq, q->rq.rq_pool);
616}
617
Jens Axboe1ea25ecb2006-07-18 22:24:11 +0200618static struct request *
Jens Axboe165125e2007-07-24 09:28:11 +0200619blk_alloc_request(struct request_queue *q, int rw, int priv, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
621 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
622
623 if (!rq)
624 return NULL;
625
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200626 blk_rq_init(q, rq);
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200627
Jens Axboe49171e52006-08-10 08:59:11 +0200628 rq->cmd_flags = rw | REQ_ALLOCED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Tejun Heocb98fc82005-10-28 08:29:39 +0200630 if (priv) {
Jens Axboecb78b282006-07-28 09:32:57 +0200631 if (unlikely(elv_set_request(q, rq, gfp_mask))) {
Tejun Heocb98fc82005-10-28 08:29:39 +0200632 mempool_free(rq, q->rq.rq_pool);
633 return NULL;
634 }
Jens Axboe4aff5e22006-08-10 08:44:47 +0200635 rq->cmd_flags |= REQ_ELVPRIV;
Tejun Heocb98fc82005-10-28 08:29:39 +0200636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Tejun Heocb98fc82005-10-28 08:29:39 +0200638 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
641/*
642 * ioc_batching returns true if the ioc is a valid batching request and
643 * should be given priority access to a request.
644 */
Jens Axboe165125e2007-07-24 09:28:11 +0200645static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
647 if (!ioc)
648 return 0;
649
650 /*
651 * Make sure the process is able to allocate at least 1 request
652 * even if the batch times out, otherwise we could theoretically
653 * lose wakeups.
654 */
655 return ioc->nr_batch_requests == q->nr_batching ||
656 (ioc->nr_batch_requests > 0
657 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
658}
659
660/*
661 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
662 * will cause the process to be a "batcher" on all queues in the system. This
663 * is the behaviour we want though - once it gets a wakeup it should be given
664 * a nice run.
665 */
Jens Axboe165125e2007-07-24 09:28:11 +0200666static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 if (!ioc || ioc_batching(q, ioc))
669 return;
670
671 ioc->nr_batch_requests = q->nr_batching;
672 ioc->last_waited = jiffies;
673}
674
Jens Axboe165125e2007-07-24 09:28:11 +0200675static void __freed_request(struct request_queue *q, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
677 struct request_list *rl = &q->rq;
678
679 if (rl->count[rw] < queue_congestion_off_threshold(q))
Thomas Maier79e2de42006-10-19 23:28:15 -0700680 blk_clear_queue_congested(q, rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 if (rl->count[rw] + 1 <= q->nr_requests) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (waitqueue_active(&rl->wait[rw]))
684 wake_up(&rl->wait[rw]);
685
686 blk_clear_queue_full(q, rw);
687 }
688}
689
690/*
691 * A request has just been released. Account for it, update the full and
692 * congestion status, wake up any waiters. Called under q->queue_lock.
693 */
Jens Axboe165125e2007-07-24 09:28:11 +0200694static void freed_request(struct request_queue *q, int rw, int priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
696 struct request_list *rl = &q->rq;
697
698 rl->count[rw]--;
Tejun Heocb98fc82005-10-28 08:29:39 +0200699 if (priv)
700 rl->elvpriv--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 __freed_request(q, rw);
703
704 if (unlikely(rl->starved[rw ^ 1]))
705 __freed_request(q, rw ^ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
708#define blkdev_free_rq(list) list_entry((list)->next, struct request, queuelist)
709/*
Nick Piggind6344532005-06-28 20:45:14 -0700710 * Get a free request, queue_lock must be held.
711 * Returns NULL on failure, with queue_lock held.
712 * Returns !NULL on success, with queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 */
Jens Axboe165125e2007-07-24 09:28:11 +0200714static struct request *get_request(struct request_queue *q, int rw_flags,
Jens Axboe7749a8d2006-12-13 13:02:26 +0100715 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
717 struct request *rq = NULL;
718 struct request_list *rl = &q->rq;
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100719 struct io_context *ioc = NULL;
Jens Axboe7749a8d2006-12-13 13:02:26 +0100720 const int rw = rw_flags & 0x01;
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100721 int may_queue, priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Jens Axboe7749a8d2006-12-13 13:02:26 +0100723 may_queue = elv_may_queue(q, rw_flags);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100724 if (may_queue == ELV_MQUEUE_NO)
725 goto rq_starved;
726
727 if (rl->count[rw]+1 >= queue_congestion_on_threshold(q)) {
728 if (rl->count[rw]+1 >= q->nr_requests) {
Jens Axboeb5deef92006-07-19 23:39:40 +0200729 ioc = current_io_context(GFP_ATOMIC, q->node);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100730 /*
731 * The queue will fill after this allocation, so set
732 * it as full, and mark this process as "batching".
733 * This process will be allowed to complete a batch of
734 * requests, others will be blocked.
735 */
736 if (!blk_queue_full(q, rw)) {
737 ioc_set_batching(q, ioc);
738 blk_set_queue_full(q, rw);
739 } else {
740 if (may_queue != ELV_MQUEUE_MUST
741 && !ioc_batching(q, ioc)) {
742 /*
743 * The queue is full and the allocating
744 * process is not a "batcher", and not
745 * exempted by the IO scheduler
746 */
747 goto out;
748 }
749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
Thomas Maier79e2de42006-10-19 23:28:15 -0700751 blk_set_queue_congested(q, rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
753
Jens Axboe082cf692005-06-28 16:35:11 +0200754 /*
755 * Only allow batching queuers to allocate up to 50% over the defined
756 * limit of requests, otherwise we could have thousands of requests
757 * allocated with any setting of ->nr_requests
758 */
Hugh Dickinsfd782a42005-06-29 15:15:40 +0100759 if (rl->count[rw] >= (3 * q->nr_requests / 2))
Jens Axboe082cf692005-06-28 16:35:11 +0200760 goto out;
Hugh Dickinsfd782a42005-06-29 15:15:40 +0100761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 rl->count[rw]++;
763 rl->starved[rw] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200764
Jens Axboe64521d12005-10-28 08:30:39 +0200765 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Tejun Heocb98fc82005-10-28 08:29:39 +0200766 if (priv)
767 rl->elvpriv++;
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 spin_unlock_irq(q->queue_lock);
770
Jens Axboe7749a8d2006-12-13 13:02:26 +0100771 rq = blk_alloc_request(q, rw_flags, priv, gfp_mask);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100772 if (unlikely(!rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 /*
774 * Allocation failed presumably due to memory. Undo anything
775 * we might have messed up.
776 *
777 * Allocating task should really be put onto the front of the
778 * wait queue, but this is pretty rare.
779 */
780 spin_lock_irq(q->queue_lock);
Tejun Heocb98fc82005-10-28 08:29:39 +0200781 freed_request(q, rw, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 /*
784 * in the very unlikely event that allocation failed and no
785 * requests for this direction was pending, mark us starved
786 * so that freeing of a request in the other direction will
787 * notice us. another possible fix would be to split the
788 * rq mempool into READ and WRITE
789 */
790rq_starved:
791 if (unlikely(rl->count[rw] == 0))
792 rl->starved[rw] = 1;
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 goto out;
795 }
796
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100797 /*
798 * ioc may be NULL here, and ioc_batching will be false. That's
799 * OK, if the queue is under the request limit then requests need
800 * not count toward the nr_batch_requests limit. There will always
801 * be some limit enforced by BLK_BATCH_TIME.
802 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (ioc_batching(q, ioc))
804 ioc->nr_batch_requests--;
Jens Axboe6728cb02008-01-31 13:03:55 +0100805
Jens Axboe2056a782006-03-23 20:00:26 +0100806 blk_add_trace_generic(q, bio, rw, BLK_TA_GETRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return rq;
809}
810
811/*
812 * No available requests for this queue, unplug the device and wait for some
813 * requests to become available.
Nick Piggind6344532005-06-28 20:45:14 -0700814 *
815 * Called with q->queue_lock held, and returns with it unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 */
Jens Axboe165125e2007-07-24 09:28:11 +0200817static struct request *get_request_wait(struct request_queue *q, int rw_flags,
Jens Axboe22e2c502005-06-27 10:55:12 +0200818 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
Jens Axboe7749a8d2006-12-13 13:02:26 +0100820 const int rw = rw_flags & 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 struct request *rq;
822
Jens Axboe7749a8d2006-12-13 13:02:26 +0100823 rq = get_request(q, rw_flags, bio, GFP_NOIO);
Nick Piggin450991b2005-06-28 20:45:13 -0700824 while (!rq) {
825 DEFINE_WAIT(wait);
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200826 struct io_context *ioc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 struct request_list *rl = &q->rq;
828
829 prepare_to_wait_exclusive(&rl->wait[rw], &wait,
830 TASK_UNINTERRUPTIBLE);
831
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200832 blk_add_trace_generic(q, bio, rw, BLK_TA_SLEEPRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200834 __generic_unplug_device(q);
835 spin_unlock_irq(q->queue_lock);
836 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200838 /*
839 * After sleeping, we become a "batching" process and
840 * will be able to allocate at least one request, and
841 * up to a big batch of them for a small period time.
842 * See ioc_batching, ioc_set_batching
843 */
844 ioc = current_io_context(GFP_NOIO, q->node);
845 ioc_set_batching(q, ioc);
Jens Axboe2056a782006-03-23 20:00:26 +0100846
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200847 spin_lock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 finish_wait(&rl->wait[rw], &wait);
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200849
850 rq = get_request(q, rw_flags, bio, GFP_NOIO);
851 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 return rq;
854}
855
Jens Axboe165125e2007-07-24 09:28:11 +0200856struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
858 struct request *rq;
859
860 BUG_ON(rw != READ && rw != WRITE);
861
Nick Piggind6344532005-06-28 20:45:14 -0700862 spin_lock_irq(q->queue_lock);
863 if (gfp_mask & __GFP_WAIT) {
Jens Axboe22e2c502005-06-27 10:55:12 +0200864 rq = get_request_wait(q, rw, NULL);
Nick Piggind6344532005-06-28 20:45:14 -0700865 } else {
Jens Axboe22e2c502005-06-27 10:55:12 +0200866 rq = get_request(q, rw, NULL, gfp_mask);
Nick Piggind6344532005-06-28 20:45:14 -0700867 if (!rq)
868 spin_unlock_irq(q->queue_lock);
869 }
870 /* q->queue_lock is unlocked at this point */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 return rq;
873}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874EXPORT_SYMBOL(blk_get_request);
875
876/**
Jens Axboedc72ef42006-07-20 14:54:05 +0200877 * blk_start_queueing - initiate dispatch of requests to device
878 * @q: request queue to kick into gear
879 *
880 * This is basically a helper to remove the need to know whether a queue
881 * is plugged or not if someone just wants to initiate dispatch of requests
882 * for this queue.
883 *
884 * The queue lock must be held with interrupts disabled.
885 */
Jens Axboe165125e2007-07-24 09:28:11 +0200886void blk_start_queueing(struct request_queue *q)
Jens Axboedc72ef42006-07-20 14:54:05 +0200887{
888 if (!blk_queue_plugged(q))
889 q->request_fn(q);
890 else
891 __generic_unplug_device(q);
892}
893EXPORT_SYMBOL(blk_start_queueing);
894
895/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 * blk_requeue_request - put a request back on queue
897 * @q: request queue where request should be inserted
898 * @rq: request to be inserted
899 *
900 * Description:
901 * Drivers often keep queueing requests until the hardware cannot accept
902 * more, when that condition happens we need to put the request back
903 * on the queue. Must be called with queue lock held.
904 */
Jens Axboe165125e2007-07-24 09:28:11 +0200905void blk_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Jens Axboe2056a782006-03-23 20:00:26 +0100907 blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (blk_rq_tagged(rq))
910 blk_queue_end_tag(q, rq);
911
912 elv_requeue_request(q, rq);
913}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914EXPORT_SYMBOL(blk_requeue_request);
915
916/**
Randy Dunlap710027a2008-08-19 20:13:11 +0200917 * blk_insert_request - insert a special request into a request queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 * @q: request queue where request should be inserted
919 * @rq: request to be inserted
920 * @at_head: insert request at head or tail of queue
921 * @data: private data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 *
923 * Description:
924 * Many block devices need to execute commands asynchronously, so they don't
925 * block the whole kernel from preemption during request execution. This is
926 * accomplished normally by inserting aritficial requests tagged as
Randy Dunlap710027a2008-08-19 20:13:11 +0200927 * REQ_TYPE_SPECIAL in to the corresponding request queue, and letting them
928 * be scheduled for actual execution by the request queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 *
930 * We have the option of inserting the head or the tail of the queue.
931 * Typically we use the tail for new ioctls and so forth. We use the head
932 * of the queue for things like a QUEUE_FULL message from a device, or a
933 * host that is unable to accept a particular command.
934 */
Jens Axboe165125e2007-07-24 09:28:11 +0200935void blk_insert_request(struct request_queue *q, struct request *rq,
Tejun Heo 867d1192005-04-24 02:06:05 -0500936 int at_head, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Tejun Heo 867d1192005-04-24 02:06:05 -0500938 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 unsigned long flags;
940
941 /*
942 * tell I/O scheduler that this isn't a regular read/write (ie it
943 * must not attempt merges on this) and that it acts as a soft
944 * barrier
945 */
Jens Axboe4aff5e22006-08-10 08:44:47 +0200946 rq->cmd_type = REQ_TYPE_SPECIAL;
947 rq->cmd_flags |= REQ_SOFTBARRIER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 rq->special = data;
950
951 spin_lock_irqsave(q->queue_lock, flags);
952
953 /*
954 * If command is tagged, release the tag
955 */
Tejun Heo 867d1192005-04-24 02:06:05 -0500956 if (blk_rq_tagged(rq))
957 blk_queue_end_tag(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Jerome Marchandb238b3d2007-10-23 15:05:46 +0200959 drive_stat_acct(rq, 1);
Tejun Heo 867d1192005-04-24 02:06:05 -0500960 __elv_add_request(q, rq, where, 0);
Jens Axboedc72ef42006-07-20 14:54:05 +0200961 blk_start_queueing(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 spin_unlock_irqrestore(q->queue_lock, flags);
963}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964EXPORT_SYMBOL(blk_insert_request);
965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966/*
967 * add-request adds a request to the linked list.
968 * queue lock is held and interrupts disabled, as we muck with the
969 * request queue list.
970 */
Jens Axboe6728cb02008-01-31 13:03:55 +0100971static inline void add_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Jerome Marchandb238b3d2007-10-23 15:05:46 +0200973 drive_stat_acct(req, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 /*
976 * elevator indicated where it wants this request to be
977 * inserted at elevator_merge time
978 */
979 __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
980}
Jens Axboe6728cb02008-01-31 13:03:55 +0100981
Tejun Heo074a7ac2008-08-25 19:56:14 +0900982static void part_round_stats_single(int cpu, struct hd_struct *part,
983 unsigned long now)
984{
985 if (now == part->stamp)
986 return;
987
988 if (part->in_flight) {
989 __part_stat_add(cpu, part, time_in_queue,
990 part->in_flight * (now - part->stamp));
991 __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
992 }
993 part->stamp = now;
994}
995
996/**
997 * part_round_stats() - Round off the performance stats on a struct
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 * disk_stats.
999 *
1000 * The average IO queue length and utilisation statistics are maintained
1001 * by observing the current state of the queue length and the amount of
1002 * time it has been in this state for.
1003 *
1004 * Normally, that accounting is done on IO completion, but that can result
1005 * in more than a second's worth of IO being accounted for within any one
1006 * second, leading to >100% utilisation. To deal with that, we call this
1007 * function to do a round-off before returning the results when reading
1008 * /proc/diskstats. This accounts immediately for all queue usage up to
1009 * the current jiffies and restarts the counters again.
1010 */
Tejun Heoc9959052008-08-25 19:47:21 +09001011void part_round_stats(int cpu, struct hd_struct *part)
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001012{
1013 unsigned long now = jiffies;
1014
Tejun Heo074a7ac2008-08-25 19:56:14 +09001015 if (part->partno)
1016 part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
1017 part_round_stats_single(cpu, part, now);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001018}
Tejun Heo074a7ac2008-08-25 19:56:14 +09001019EXPORT_SYMBOL_GPL(part_round_stats);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021/*
1022 * queue lock must be held
1023 */
Jens Axboe165125e2007-07-24 09:28:11 +02001024void __blk_put_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 if (unlikely(!q))
1027 return;
1028 if (unlikely(--req->ref_count))
1029 return;
1030
Tejun Heo8922e162005-10-20 16:23:44 +02001031 elv_completed_request(q, req);
1032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 /*
1034 * Request may not have originated from ll_rw_blk. if not,
1035 * it didn't come out of our reserved rq pools
1036 */
Jens Axboe49171e52006-08-10 08:59:11 +02001037 if (req->cmd_flags & REQ_ALLOCED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 int rw = rq_data_dir(req);
Jens Axboe4aff5e22006-08-10 08:44:47 +02001039 int priv = req->cmd_flags & REQ_ELVPRIV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 BUG_ON(!list_empty(&req->queuelist));
Jens Axboe98170642006-07-28 09:23:08 +02001042 BUG_ON(!hlist_unhashed(&req->hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 blk_free_request(q, req);
Tejun Heocb98fc82005-10-28 08:29:39 +02001045 freed_request(q, rw, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 }
1047}
Mike Christie6e39b69e2005-11-11 05:30:24 -06001048EXPORT_SYMBOL_GPL(__blk_put_request);
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050void blk_put_request(struct request *req)
1051{
Tejun Heo8922e162005-10-20 16:23:44 +02001052 unsigned long flags;
Jens Axboe165125e2007-07-24 09:28:11 +02001053 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
FUJITA Tomonori52a93ba2008-07-15 21:21:45 +02001055 spin_lock_irqsave(q->queue_lock, flags);
1056 __blk_put_request(q, req);
1057 spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059EXPORT_SYMBOL(blk_put_request);
1060
Jens Axboe86db1e22008-01-29 14:53:40 +01001061void init_request_from_bio(struct request *req, struct bio *bio)
Tejun Heo52d9e672006-01-06 09:49:58 +01001062{
Jens Axboe4aff5e22006-08-10 08:44:47 +02001063 req->cmd_type = REQ_TYPE_FS;
Tejun Heo52d9e672006-01-06 09:49:58 +01001064
1065 /*
1066 * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
1067 */
1068 if (bio_rw_ahead(bio) || bio_failfast(bio))
Jens Axboe4aff5e22006-08-10 08:44:47 +02001069 req->cmd_flags |= REQ_FAILFAST;
Tejun Heo52d9e672006-01-06 09:49:58 +01001070
1071 /*
1072 * REQ_BARRIER implies no merging, but lets make it explicit
1073 */
David Woodhousefb2dce82008-08-05 18:01:53 +01001074 if (unlikely(bio_discard(bio))) {
David Woodhousee17fc0a2008-08-09 16:42:20 +01001075 req->cmd_flags |= REQ_DISCARD;
1076 if (bio_barrier(bio))
1077 req->cmd_flags |= REQ_SOFTBARRIER;
David Woodhousefb2dce82008-08-05 18:01:53 +01001078 req->q->prepare_discard_fn(req->q, req);
David Woodhousee17fc0a2008-08-09 16:42:20 +01001079 } else if (unlikely(bio_barrier(bio)))
1080 req->cmd_flags |= (REQ_HARDBARRIER | REQ_NOMERGE);
Tejun Heo52d9e672006-01-06 09:49:58 +01001081
Jens Axboeb31dc662006-06-13 08:26:10 +02001082 if (bio_sync(bio))
Jens Axboe4aff5e22006-08-10 08:44:47 +02001083 req->cmd_flags |= REQ_RW_SYNC;
Jens Axboe5404bc72006-08-10 09:01:02 +02001084 if (bio_rw_meta(bio))
1085 req->cmd_flags |= REQ_RW_META;
Jens Axboeb31dc662006-06-13 08:26:10 +02001086
Tejun Heo52d9e672006-01-06 09:49:58 +01001087 req->errors = 0;
1088 req->hard_sector = req->sector = bio->bi_sector;
Tejun Heo52d9e672006-01-06 09:49:58 +01001089 req->ioprio = bio_prio(bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001090 req->start_time = jiffies;
NeilBrownbc1c56f2007-08-16 13:31:30 +02001091 blk_rq_bio_prep(req->q, req, bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001092}
1093
Jens Axboe165125e2007-07-24 09:28:11 +02001094static int __make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
Nick Piggin450991b2005-06-28 20:45:13 -07001096 struct request *req;
David Woodhousefb2dce82008-08-05 18:01:53 +01001097 int el_ret, nr_sectors, barrier, discard, err;
Jens Axboe51da90f2006-07-18 04:14:45 +02001098 const unsigned short prio = bio_prio(bio);
1099 const int sync = bio_sync(bio);
Jens Axboe7749a8d2006-12-13 13:02:26 +01001100 int rw_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 nr_sectors = bio_sectors(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 /*
1105 * low level driver can indicate that it wants pages above a
1106 * certain limit bounced to low memory (ie for highmem, or even
1107 * ISA dma in theory)
1108 */
1109 blk_queue_bounce(q, &bio);
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 barrier = bio_barrier(bio);
David Woodhousee17fc0a2008-08-09 16:42:20 +01001112 if (unlikely(barrier) && bio_has_data(bio) &&
1113 (q->next_ordered == QUEUE_ORDERED_NONE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 err = -EOPNOTSUPP;
1115 goto end_io;
1116 }
1117
David Woodhousefb2dce82008-08-05 18:01:53 +01001118 discard = bio_discard(bio);
1119 if (unlikely(discard) && !q->prepare_discard_fn) {
1120 err = -EOPNOTSUPP;
1121 goto end_io;
1122 }
1123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 spin_lock_irq(q->queue_lock);
1125
Nick Piggin450991b2005-06-28 20:45:13 -07001126 if (unlikely(barrier) || elv_queue_empty(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 goto get_rq;
1128
1129 el_ret = elv_merge(q, &req, bio);
1130 switch (el_ret) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001131 case ELEVATOR_BACK_MERGE:
1132 BUG_ON(!rq_mergeable(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Jens Axboe6728cb02008-01-31 13:03:55 +01001134 if (!ll_back_merge_fn(q, req, bio))
1135 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Jens Axboe6728cb02008-01-31 13:03:55 +01001137 blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
Jens Axboe2056a782006-03-23 20:00:26 +01001138
Jens Axboe6728cb02008-01-31 13:03:55 +01001139 req->biotail->bi_next = bio;
1140 req->biotail = bio;
1141 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
1142 req->ioprio = ioprio_best(req->ioprio, prio);
1143 drive_stat_acct(req, 0);
1144 if (!attempt_back_merge(q, req))
1145 elv_merged_request(q, req, el_ret);
1146 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Jens Axboe6728cb02008-01-31 13:03:55 +01001148 case ELEVATOR_FRONT_MERGE:
1149 BUG_ON(!rq_mergeable(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Jens Axboe6728cb02008-01-31 13:03:55 +01001151 if (!ll_front_merge_fn(q, req, bio))
1152 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Jens Axboe6728cb02008-01-31 13:03:55 +01001154 blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
Jens Axboe2056a782006-03-23 20:00:26 +01001155
Jens Axboe6728cb02008-01-31 13:03:55 +01001156 bio->bi_next = req->bio;
1157 req->bio = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Jens Axboe6728cb02008-01-31 13:03:55 +01001159 /*
1160 * may not be valid. if the low level driver said
1161 * it didn't need a bounce buffer then it better
1162 * not touch req->buffer either...
1163 */
1164 req->buffer = bio_data(bio);
1165 req->current_nr_sectors = bio_cur_sectors(bio);
1166 req->hard_cur_sectors = req->current_nr_sectors;
1167 req->sector = req->hard_sector = bio->bi_sector;
1168 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
1169 req->ioprio = ioprio_best(req->ioprio, prio);
1170 drive_stat_acct(req, 0);
1171 if (!attempt_front_merge(q, req))
1172 elv_merged_request(q, req, el_ret);
1173 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Jens Axboe6728cb02008-01-31 13:03:55 +01001175 /* ELV_NO_MERGE: elevator says don't/can't merge. */
1176 default:
1177 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 }
1179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180get_rq:
Nick Piggin450991b2005-06-28 20:45:13 -07001181 /*
Jens Axboe7749a8d2006-12-13 13:02:26 +01001182 * This sync check and mask will be re-done in init_request_from_bio(),
1183 * but we need to set it earlier to expose the sync flag to the
1184 * rq allocator and io schedulers.
1185 */
1186 rw_flags = bio_data_dir(bio);
1187 if (sync)
1188 rw_flags |= REQ_RW_SYNC;
1189
1190 /*
Nick Piggin450991b2005-06-28 20:45:13 -07001191 * Grab a free request. This is might sleep but can not fail.
Nick Piggind6344532005-06-28 20:45:14 -07001192 * Returns with the queue unlocked.
Nick Piggin450991b2005-06-28 20:45:13 -07001193 */
Jens Axboe7749a8d2006-12-13 13:02:26 +01001194 req = get_request_wait(q, rw_flags, bio);
Nick Piggind6344532005-06-28 20:45:14 -07001195
Nick Piggin450991b2005-06-28 20:45:13 -07001196 /*
1197 * After dropping the lock and possibly sleeping here, our request
1198 * may now be mergeable after it had proven unmergeable (above).
1199 * We don't worry about that case for efficiency. It won't happen
1200 * often, and the elevators are able to handle it.
1201 */
Tejun Heo52d9e672006-01-06 09:49:58 +01001202 init_request_from_bio(req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Nick Piggin450991b2005-06-28 20:45:13 -07001204 spin_lock_irq(q->queue_lock);
1205 if (elv_queue_empty(q))
1206 blk_plug_device(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 add_request(q, req);
1208out:
Jens Axboe4a534f92005-04-16 15:25:40 -07001209 if (sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 __generic_unplug_device(q);
1211
1212 spin_unlock_irq(q->queue_lock);
1213 return 0;
1214
1215end_io:
NeilBrown6712ecf2007-09-27 12:47:43 +02001216 bio_endio(bio, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 return 0;
1218}
1219
1220/*
1221 * If bio->bi_dev is a partition, remap the location
1222 */
1223static inline void blk_partition_remap(struct bio *bio)
1224{
1225 struct block_device *bdev = bio->bi_bdev;
1226
Jens Axboebf2de6f2007-09-27 13:01:25 +02001227 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 struct hd_struct *p = bdev->bd_part;
Jens Axboea3623572005-11-01 09:26:16 +01001229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 bio->bi_sector += p->start_sect;
1231 bio->bi_bdev = bdev->bd_contains;
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001232
1233 blk_add_trace_remap(bdev_get_queue(bio->bi_bdev), bio,
1234 bdev->bd_dev, bio->bi_sector,
1235 bio->bi_sector - p->start_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 }
1237}
1238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239static void handle_bad_sector(struct bio *bio)
1240{
1241 char b[BDEVNAME_SIZE];
1242
1243 printk(KERN_INFO "attempt to access beyond end of device\n");
1244 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1245 bdevname(bio->bi_bdev, b),
1246 bio->bi_rw,
1247 (unsigned long long)bio->bi_sector + bio_sectors(bio),
1248 (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
1249
1250 set_bit(BIO_EOF, &bio->bi_flags);
1251}
1252
Akinobu Mitac17bb492006-12-08 02:39:46 -08001253#ifdef CONFIG_FAIL_MAKE_REQUEST
1254
1255static DECLARE_FAULT_ATTR(fail_make_request);
1256
1257static int __init setup_fail_make_request(char *str)
1258{
1259 return setup_fault_attr(&fail_make_request, str);
1260}
1261__setup("fail_make_request=", setup_fail_make_request);
1262
1263static int should_fail_request(struct bio *bio)
1264{
Tejun Heoeddb2e22008-08-25 19:56:13 +09001265 struct hd_struct *part = bio->bi_bdev->bd_part;
1266
1267 if (part_to_disk(part)->part0.make_it_fail || part->make_it_fail)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001268 return should_fail(&fail_make_request, bio->bi_size);
1269
1270 return 0;
1271}
1272
1273static int __init fail_make_request_debugfs(void)
1274{
1275 return init_fault_attr_dentries(&fail_make_request,
1276 "fail_make_request");
1277}
1278
1279late_initcall(fail_make_request_debugfs);
1280
1281#else /* CONFIG_FAIL_MAKE_REQUEST */
1282
1283static inline int should_fail_request(struct bio *bio)
1284{
1285 return 0;
1286}
1287
1288#endif /* CONFIG_FAIL_MAKE_REQUEST */
1289
Jens Axboec07e2b42007-07-18 13:27:58 +02001290/*
1291 * Check whether this bio extends beyond the end of the device.
1292 */
1293static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1294{
1295 sector_t maxsector;
1296
1297 if (!nr_sectors)
1298 return 0;
1299
1300 /* Test device or partition size, when known. */
1301 maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
1302 if (maxsector) {
1303 sector_t sector = bio->bi_sector;
1304
1305 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1306 /*
1307 * This may well happen - the kernel calls bread()
1308 * without checking the size of the device, e.g., when
1309 * mounting a device.
1310 */
1311 handle_bad_sector(bio);
1312 return 1;
1313 }
1314 }
1315
1316 return 0;
1317}
1318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001320 * generic_make_request - hand a buffer to its device driver for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 * @bio: The bio describing the location in memory and on the device.
1322 *
1323 * generic_make_request() is used to make I/O requests of block
1324 * devices. It is passed a &struct bio, which describes the I/O that needs
1325 * to be done.
1326 *
1327 * generic_make_request() does not return any status. The
1328 * success/failure status of the request, along with notification of
1329 * completion, is delivered asynchronously through the bio->bi_end_io
1330 * function described (one day) else where.
1331 *
1332 * The caller of generic_make_request must make sure that bi_io_vec
1333 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1334 * set to describe the device address, and the
1335 * bi_end_io and optionally bi_private are set to describe how
1336 * completion notification should be signaled.
1337 *
1338 * generic_make_request and the drivers it calls may use bi_next if this
1339 * bio happens to be merged with someone else, and may change bi_dev and
1340 * bi_sector for remaps as it sees fit. So the values of these fields
1341 * should NOT be depended on after the call to generic_make_request.
1342 */
Neil Brownd89d8792007-05-01 09:53:42 +02001343static inline void __generic_make_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344{
Jens Axboe165125e2007-07-24 09:28:11 +02001345 struct request_queue *q;
NeilBrown5ddfe962006-10-30 22:07:21 -08001346 sector_t old_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 int ret, nr_sectors = bio_sectors(bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001348 dev_t old_dev;
Jens Axboe51fd77b2007-11-02 08:49:08 +01001349 int err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Jens Axboec07e2b42007-07-18 13:27:58 +02001353 if (bio_check_eod(bio, nr_sectors))
1354 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356 /*
1357 * Resolve the mapping until finished. (drivers are
1358 * still free to implement/resolve their own stacking
1359 * by explicitly returning 0)
1360 *
1361 * NOTE: we don't repeat the blk_size check for each new device.
1362 * Stacking drivers are expected to know what they are doing.
1363 */
NeilBrown5ddfe962006-10-30 22:07:21 -08001364 old_sector = -1;
Jens Axboe2056a782006-03-23 20:00:26 +01001365 old_dev = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 do {
1367 char b[BDEVNAME_SIZE];
1368
1369 q = bdev_get_queue(bio->bi_bdev);
1370 if (!q) {
1371 printk(KERN_ERR
1372 "generic_make_request: Trying to access "
1373 "nonexistent block-device %s (%Lu)\n",
1374 bdevname(bio->bi_bdev, b),
1375 (long long) bio->bi_sector);
1376end_io:
Jens Axboe51fd77b2007-11-02 08:49:08 +01001377 bio_endio(bio, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 break;
1379 }
1380
Jens Axboe4fa253f2007-07-18 13:13:10 +02001381 if (unlikely(nr_sectors > q->max_hw_sectors)) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001382 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 bdevname(bio->bi_bdev, b),
1384 bio_sectors(bio),
1385 q->max_hw_sectors);
1386 goto end_io;
1387 }
1388
Nick Pigginfde6ad22005-06-23 00:08:53 -07001389 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 goto end_io;
1391
Akinobu Mitac17bb492006-12-08 02:39:46 -08001392 if (should_fail_request(bio))
1393 goto end_io;
1394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 /*
1396 * If this device has partitions, remap block n
1397 * of partition p to block n+start(p) of the disk.
1398 */
1399 blk_partition_remap(bio);
1400
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +02001401 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio))
1402 goto end_io;
1403
NeilBrown5ddfe962006-10-30 22:07:21 -08001404 if (old_sector != -1)
Jens Axboe4fa253f2007-07-18 13:13:10 +02001405 blk_add_trace_remap(q, bio, old_dev, bio->bi_sector,
NeilBrown5ddfe962006-10-30 22:07:21 -08001406 old_sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001407
1408 blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
1409
NeilBrown5ddfe962006-10-30 22:07:21 -08001410 old_sector = bio->bi_sector;
Jens Axboe2056a782006-03-23 20:00:26 +01001411 old_dev = bio->bi_bdev->bd_dev;
1412
Jens Axboec07e2b42007-07-18 13:27:58 +02001413 if (bio_check_eod(bio, nr_sectors))
1414 goto end_io;
David Woodhousefb2dce82008-08-05 18:01:53 +01001415 if ((bio_empty_barrier(bio) && !q->prepare_flush_fn) ||
1416 (bio_discard(bio) && !q->prepare_discard_fn)) {
Jens Axboe51fd77b2007-11-02 08:49:08 +01001417 err = -EOPNOTSUPP;
1418 goto end_io;
1419 }
NeilBrown5ddfe962006-10-30 22:07:21 -08001420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 ret = q->make_request_fn(q, bio);
1422 } while (ret);
1423}
1424
Neil Brownd89d8792007-05-01 09:53:42 +02001425/*
1426 * We only want one ->make_request_fn to be active at a time,
1427 * else stack usage with stacked devices could be a problem.
1428 * So use current->bio_{list,tail} to keep a list of requests
1429 * submited by a make_request_fn function.
1430 * current->bio_tail is also used as a flag to say if
1431 * generic_make_request is currently active in this task or not.
1432 * If it is NULL, then no make_request is active. If it is non-NULL,
1433 * then a make_request is active, and new requests should be added
1434 * at the tail
1435 */
1436void generic_make_request(struct bio *bio)
1437{
1438 if (current->bio_tail) {
1439 /* make_request is active */
1440 *(current->bio_tail) = bio;
1441 bio->bi_next = NULL;
1442 current->bio_tail = &bio->bi_next;
1443 return;
1444 }
1445 /* following loop may be a bit non-obvious, and so deserves some
1446 * explanation.
1447 * Before entering the loop, bio->bi_next is NULL (as all callers
1448 * ensure that) so we have a list with a single bio.
1449 * We pretend that we have just taken it off a longer list, so
1450 * we assign bio_list to the next (which is NULL) and bio_tail
1451 * to &bio_list, thus initialising the bio_list of new bios to be
1452 * added. __generic_make_request may indeed add some more bios
1453 * through a recursive call to generic_make_request. If it
1454 * did, we find a non-NULL value in bio_list and re-enter the loop
1455 * from the top. In this case we really did just take the bio
1456 * of the top of the list (no pretending) and so fixup bio_list and
1457 * bio_tail or bi_next, and call into __generic_make_request again.
1458 *
1459 * The loop was structured like this to make only one call to
1460 * __generic_make_request (which is important as it is large and
1461 * inlined) and to keep the structure simple.
1462 */
1463 BUG_ON(bio->bi_next);
1464 do {
1465 current->bio_list = bio->bi_next;
1466 if (bio->bi_next == NULL)
1467 current->bio_tail = &current->bio_list;
1468 else
1469 bio->bi_next = NULL;
1470 __generic_make_request(bio);
1471 bio = current->bio_list;
1472 } while (bio);
1473 current->bio_tail = NULL; /* deactivate */
1474}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475EXPORT_SYMBOL(generic_make_request);
1476
1477/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001478 * submit_bio - submit a bio to the block device layer for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1480 * @bio: The &struct bio which describes the I/O
1481 *
1482 * submit_bio() is very similar in purpose to generic_make_request(), and
1483 * uses that function to do most of the work. Both are fairly rough
Randy Dunlap710027a2008-08-19 20:13:11 +02001484 * interfaces; @bio must be presetup and ready for I/O.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 *
1486 */
1487void submit_bio(int rw, struct bio *bio)
1488{
1489 int count = bio_sectors(bio);
1490
Jens Axboe22e2c502005-06-27 10:55:12 +02001491 bio->bi_rw |= rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Jens Axboebf2de6f2007-09-27 13:01:25 +02001493 /*
1494 * If it's a regular read/write or a barrier with data attached,
1495 * go through the normal accounting stuff before submission.
1496 */
Jens Axboea9c701e2008-08-08 11:04:44 +02001497 if (bio_has_data(bio)) {
Jens Axboebf2de6f2007-09-27 13:01:25 +02001498 if (rw & WRITE) {
1499 count_vm_events(PGPGOUT, count);
1500 } else {
1501 task_io_account_read(bio->bi_size);
1502 count_vm_events(PGPGIN, count);
1503 }
1504
1505 if (unlikely(block_dump)) {
1506 char b[BDEVNAME_SIZE];
1507 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001508 current->comm, task_pid_nr(current),
Jens Axboebf2de6f2007-09-27 13:01:25 +02001509 (rw & WRITE) ? "WRITE" : "READ",
1510 (unsigned long long)bio->bi_sector,
Jens Axboe6728cb02008-01-31 13:03:55 +01001511 bdevname(bio->bi_bdev, b));
Jens Axboebf2de6f2007-09-27 13:01:25 +02001512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 }
1514
1515 generic_make_request(bio);
1516}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517EXPORT_SYMBOL(submit_bio);
1518
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001519/**
1520 * __end_that_request_first - end I/O on a request
1521 * @req: the request being processed
Randy Dunlap710027a2008-08-19 20:13:11 +02001522 * @error: %0 for success, < %0 for error
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001523 * @nr_bytes: number of bytes to complete
1524 *
1525 * Description:
1526 * Ends I/O on a number of bytes attached to @req, and sets it up
1527 * for the next range of segments (if any) in the cluster.
1528 *
1529 * Return:
Randy Dunlap710027a2008-08-19 20:13:11 +02001530 * %0 - we are done with this request, call end_that_request_last()
1531 * %1 - still buffers pending for this request
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001532 **/
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001533static int __end_that_request_first(struct request *req, int error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 int nr_bytes)
1535{
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001536 int total_bytes, bio_nbytes, next_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 struct bio *bio;
1538
Jens Axboe2056a782006-03-23 20:00:26 +01001539 blk_add_trace_rq(req->q, req, BLK_TA_COMPLETE);
1540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 /*
Randy Dunlap710027a2008-08-19 20:13:11 +02001542 * for a REQ_TYPE_BLOCK_PC request, we want to carry any eventual
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 * sense key with us all the way through
1544 */
1545 if (!blk_pc_request(req))
1546 req->errors = 0;
1547
Jens Axboe6728cb02008-01-31 13:03:55 +01001548 if (error && (blk_fs_request(req) && !(req->cmd_flags & REQ_QUIET))) {
1549 printk(KERN_ERR "end_request: I/O error, dev %s, sector %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 req->rq_disk ? req->rq_disk->disk_name : "?",
1551 (unsigned long long)req->sector);
1552 }
1553
Jens Axboed72d9042005-11-01 08:35:42 +01001554 if (blk_fs_request(req) && req->rq_disk) {
Jens Axboea3623572005-11-01 09:26:16 +01001555 const int rw = rq_data_dir(req);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001556 struct hd_struct *part;
Tejun Heoc9959052008-08-25 19:47:21 +09001557 int cpu;
Jens Axboea3623572005-11-01 09:26:16 +01001558
Tejun Heo074a7ac2008-08-25 19:56:14 +09001559 cpu = part_stat_lock();
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001560 part = disk_map_sector_rcu(req->rq_disk, req->sector);
Tejun Heo074a7ac2008-08-25 19:56:14 +09001561 part_stat_add(cpu, part, sectors[rw], nr_bytes >> 9);
1562 part_stat_unlock();
Jens Axboed72d9042005-11-01 08:35:42 +01001563 }
1564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 total_bytes = bio_nbytes = 0;
1566 while ((bio = req->bio) != NULL) {
1567 int nbytes;
1568
Jens Axboebf2de6f2007-09-27 13:01:25 +02001569 /*
1570 * For an empty barrier request, the low level driver must
1571 * store a potential error location in ->sector. We pass
1572 * that back up in ->bi_sector.
1573 */
1574 if (blk_empty_barrier(req))
1575 bio->bi_sector = req->sector;
1576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 if (nr_bytes >= bio->bi_size) {
1578 req->bio = bio->bi_next;
1579 nbytes = bio->bi_size;
NeilBrown5bb23a62007-09-27 12:46:13 +02001580 req_bio_endio(req, bio, nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 next_idx = 0;
1582 bio_nbytes = 0;
1583 } else {
1584 int idx = bio->bi_idx + next_idx;
1585
1586 if (unlikely(bio->bi_idx >= bio->bi_vcnt)) {
1587 blk_dump_rq_flags(req, "__end_that");
Jens Axboe6728cb02008-01-31 13:03:55 +01001588 printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
Harvey Harrison24c03d42008-05-01 04:35:17 -07001589 __func__, bio->bi_idx, bio->bi_vcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 break;
1591 }
1592
1593 nbytes = bio_iovec_idx(bio, idx)->bv_len;
1594 BIO_BUG_ON(nbytes > bio->bi_size);
1595
1596 /*
1597 * not a complete bvec done
1598 */
1599 if (unlikely(nbytes > nr_bytes)) {
1600 bio_nbytes += nr_bytes;
1601 total_bytes += nr_bytes;
1602 break;
1603 }
1604
1605 /*
1606 * advance to the next vector
1607 */
1608 next_idx++;
1609 bio_nbytes += nbytes;
1610 }
1611
1612 total_bytes += nbytes;
1613 nr_bytes -= nbytes;
1614
Jens Axboe6728cb02008-01-31 13:03:55 +01001615 bio = req->bio;
1616 if (bio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 /*
1618 * end more in this run, or just return 'not-done'
1619 */
1620 if (unlikely(nr_bytes <= 0))
1621 break;
1622 }
1623 }
1624
1625 /*
1626 * completely done
1627 */
1628 if (!req->bio)
1629 return 0;
1630
1631 /*
1632 * if the request wasn't completed, update state
1633 */
1634 if (bio_nbytes) {
NeilBrown5bb23a62007-09-27 12:46:13 +02001635 req_bio_endio(req, bio, bio_nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 bio->bi_idx += next_idx;
1637 bio_iovec(bio)->bv_offset += nr_bytes;
1638 bio_iovec(bio)->bv_len -= nr_bytes;
1639 }
1640
1641 blk_recalc_rq_sectors(req, total_bytes >> 9);
1642 blk_recalc_rq_segments(req);
1643 return 1;
1644}
1645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646/*
Jens Axboeff856ba2006-01-09 16:02:34 +01001647 * splice the completion data to a local structure and hand off to
1648 * process_completion_queue() to complete the requests
1649 */
1650static void blk_done_softirq(struct softirq_action *h)
1651{
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07001652 struct list_head *cpu_list, local_list;
Jens Axboeff856ba2006-01-09 16:02:34 +01001653
1654 local_irq_disable();
1655 cpu_list = &__get_cpu_var(blk_cpu_done);
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07001656 list_replace_init(cpu_list, &local_list);
Jens Axboeff856ba2006-01-09 16:02:34 +01001657 local_irq_enable();
1658
1659 while (!list_empty(&local_list)) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001660 struct request *rq;
Jens Axboeff856ba2006-01-09 16:02:34 +01001661
Jens Axboe6728cb02008-01-31 13:03:55 +01001662 rq = list_entry(local_list.next, struct request, donelist);
Jens Axboeff856ba2006-01-09 16:02:34 +01001663 list_del_init(&rq->donelist);
1664 rq->q->softirq_done_fn(rq);
1665 }
1666}
1667
Jens Axboe6728cb02008-01-31 13:03:55 +01001668static int __cpuinit blk_cpu_notify(struct notifier_block *self,
1669 unsigned long action, void *hcpu)
Jens Axboeff856ba2006-01-09 16:02:34 +01001670{
1671 /*
1672 * If a CPU goes away, splice its entries to the current CPU
1673 * and trigger a run of the softirq
1674 */
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001675 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
Jens Axboeff856ba2006-01-09 16:02:34 +01001676 int cpu = (unsigned long) hcpu;
1677
1678 local_irq_disable();
1679 list_splice_init(&per_cpu(blk_cpu_done, cpu),
1680 &__get_cpu_var(blk_cpu_done));
1681 raise_softirq_irqoff(BLOCK_SOFTIRQ);
1682 local_irq_enable();
1683 }
1684
1685 return NOTIFY_OK;
1686}
1687
1688
Satyam Sharmadb47d472007-08-23 09:29:40 +02001689static struct notifier_block blk_cpu_notifier __cpuinitdata = {
Jens Axboeff856ba2006-01-09 16:02:34 +01001690 .notifier_call = blk_cpu_notify,
1691};
1692
Jens Axboeff856ba2006-01-09 16:02:34 +01001693/**
1694 * blk_complete_request - end I/O on a request
1695 * @req: the request being processed
1696 *
1697 * Description:
1698 * Ends all I/O on a request. It does not handle partial completions,
Andreas Mohrd6e05ed2006-06-26 18:35:02 +02001699 * unless the driver actually implements this in its completion callback
Jens Axboe4fa253f2007-07-18 13:13:10 +02001700 * through requeueing. The actual completion happens out-of-order,
Jens Axboeff856ba2006-01-09 16:02:34 +01001701 * through a softirq handler. The user must have registered a completion
1702 * callback through blk_queue_softirq_done().
1703 **/
1704
1705void blk_complete_request(struct request *req)
1706{
1707 struct list_head *cpu_list;
1708 unsigned long flags;
1709
1710 BUG_ON(!req->q->softirq_done_fn);
Jens Axboe6728cb02008-01-31 13:03:55 +01001711
Jens Axboeff856ba2006-01-09 16:02:34 +01001712 local_irq_save(flags);
1713
1714 cpu_list = &__get_cpu_var(blk_cpu_done);
1715 list_add_tail(&req->donelist, cpu_list);
1716 raise_softirq_irqoff(BLOCK_SOFTIRQ);
1717
1718 local_irq_restore(flags);
1719}
Jens Axboeff856ba2006-01-09 16:02:34 +01001720EXPORT_SYMBOL(blk_complete_request);
Jens Axboe6728cb02008-01-31 13:03:55 +01001721
Jens Axboeff856ba2006-01-09 16:02:34 +01001722/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 * queue lock must be held
1724 */
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001725static void end_that_request_last(struct request *req, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
1727 struct gendisk *disk = req->rq_disk;
Tejun Heo8ffdc652006-01-06 09:49:03 +01001728
Kiyoshi Uedab8286232007-12-11 17:53:24 -05001729 if (blk_rq_tagged(req))
1730 blk_queue_end_tag(req->q, req);
1731
1732 if (blk_queued_rq(req))
1733 blkdev_dequeue_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
1735 if (unlikely(laptop_mode) && blk_fs_request(req))
1736 laptop_io_completion();
1737
Jens Axboefd0ff8a2006-05-23 11:23:49 +02001738 /*
1739 * Account IO completion. bar_rq isn't accounted as a normal
1740 * IO on queueing nor completion. Accounting the containing
1741 * request is enough.
1742 */
1743 if (disk && blk_fs_request(req) && req != &req->q->bar_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 unsigned long duration = jiffies - req->start_time;
Jens Axboea3623572005-11-01 09:26:16 +01001745 const int rw = rq_data_dir(req);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001746 struct hd_struct *part;
Tejun Heoc9959052008-08-25 19:47:21 +09001747 int cpu;
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001748
Tejun Heo074a7ac2008-08-25 19:56:14 +09001749 cpu = part_stat_lock();
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001750 part = disk_map_sector_rcu(disk, req->sector);
Jens Axboea3623572005-11-01 09:26:16 +01001751
Tejun Heo074a7ac2008-08-25 19:56:14 +09001752 part_stat_inc(cpu, part, ios[rw]);
1753 part_stat_add(cpu, part, ticks[rw], duration);
1754 part_round_stats(cpu, part);
1755 part_dec_in_flight(part);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001756
Tejun Heo074a7ac2008-08-25 19:56:14 +09001757 part_stat_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 }
Kiyoshi Uedab8286232007-12-11 17:53:24 -05001759
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 if (req->end_io)
Tejun Heo8ffdc652006-01-06 09:49:03 +01001761 req->end_io(req, error);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05001762 else {
1763 if (blk_bidi_rq(req))
1764 __blk_put_request(req->next_rq->q, req->next_rq);
1765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 __blk_put_request(req->q, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 }
1768}
1769
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770static inline void __end_request(struct request *rq, int uptodate,
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05001771 unsigned int nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05001773 int error = 0;
1774
1775 if (uptodate <= 0)
1776 error = uptodate ? uptodate : -EIO;
1777
1778 __blk_end_request(rq, error, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779}
1780
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05001781/**
1782 * blk_rq_bytes - Returns bytes left to complete in the entire request
Randy Dunlap5d87a052008-02-20 09:01:22 +01001783 * @rq: the request being processed
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05001784 **/
1785unsigned int blk_rq_bytes(struct request *rq)
Jens Axboea0cd1282007-09-21 10:41:07 +02001786{
1787 if (blk_fs_request(rq))
1788 return rq->hard_nr_sectors << 9;
1789
1790 return rq->data_len;
1791}
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05001792EXPORT_SYMBOL_GPL(blk_rq_bytes);
1793
1794/**
1795 * blk_rq_cur_bytes - Returns bytes left to complete in the current segment
Randy Dunlap5d87a052008-02-20 09:01:22 +01001796 * @rq: the request being processed
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05001797 **/
1798unsigned int blk_rq_cur_bytes(struct request *rq)
1799{
1800 if (blk_fs_request(rq))
1801 return rq->current_nr_sectors << 9;
1802
1803 if (rq->bio)
1804 return rq->bio->bi_size;
1805
1806 return rq->data_len;
1807}
1808EXPORT_SYMBOL_GPL(blk_rq_cur_bytes);
Jens Axboea0cd1282007-09-21 10:41:07 +02001809
1810/**
1811 * end_queued_request - end all I/O on a queued request
1812 * @rq: the request being processed
Randy Dunlap710027a2008-08-19 20:13:11 +02001813 * @uptodate: error value or %0/%1 uptodate flag
Jens Axboea0cd1282007-09-21 10:41:07 +02001814 *
1815 * Description:
1816 * Ends all I/O on a request, and removes it from the block layer queues.
Randy Dunlap710027a2008-08-19 20:13:11 +02001817 * Not suitable for normal I/O completion, unless the driver still has
Jens Axboea0cd1282007-09-21 10:41:07 +02001818 * the request attached to the block layer.
1819 *
1820 **/
1821void end_queued_request(struct request *rq, int uptodate)
1822{
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05001823 __end_request(rq, uptodate, blk_rq_bytes(rq));
Jens Axboea0cd1282007-09-21 10:41:07 +02001824}
1825EXPORT_SYMBOL(end_queued_request);
1826
1827/**
1828 * end_dequeued_request - end all I/O on a dequeued request
1829 * @rq: the request being processed
Randy Dunlap710027a2008-08-19 20:13:11 +02001830 * @uptodate: error value or %0/%1 uptodate flag
Jens Axboea0cd1282007-09-21 10:41:07 +02001831 *
1832 * Description:
1833 * Ends all I/O on a request. The request must already have been
1834 * dequeued using blkdev_dequeue_request(), as is normally the case
1835 * for most drivers.
1836 *
1837 **/
1838void end_dequeued_request(struct request *rq, int uptodate)
1839{
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05001840 __end_request(rq, uptodate, blk_rq_bytes(rq));
Jens Axboea0cd1282007-09-21 10:41:07 +02001841}
1842EXPORT_SYMBOL(end_dequeued_request);
1843
1844
1845/**
1846 * end_request - end I/O on the current segment of the request
Randy Dunlap8f731f72007-10-18 23:39:28 -07001847 * @req: the request being processed
Randy Dunlap710027a2008-08-19 20:13:11 +02001848 * @uptodate: error value or %0/%1 uptodate flag
Jens Axboea0cd1282007-09-21 10:41:07 +02001849 *
1850 * Description:
1851 * Ends I/O on the current segment of a request. If that is the only
1852 * remaining segment, the request is also completed and freed.
1853 *
Randy Dunlap710027a2008-08-19 20:13:11 +02001854 * This is a remnant of how older block drivers handled I/O completions.
1855 * Modern drivers typically end I/O on the full request in one go, unless
Jens Axboea0cd1282007-09-21 10:41:07 +02001856 * they have a residual value to account for. For that case this function
1857 * isn't really useful, unless the residual just happens to be the
1858 * full current segment. In other words, don't use this function in new
1859 * code. Either use end_request_completely(), or the
1860 * end_that_request_chunk() (along with end_that_request_last()) for
1861 * partial completions.
1862 *
1863 **/
1864void end_request(struct request *req, int uptodate)
1865{
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05001866 __end_request(req, uptodate, req->hard_cur_sectors << 9);
Jens Axboea0cd1282007-09-21 10:41:07 +02001867}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868EXPORT_SYMBOL(end_request);
1869
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001870/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001871 * blk_end_io - Generic end_io function to complete a request.
1872 * @rq: the request being processed
Randy Dunlap710027a2008-08-19 20:13:11 +02001873 * @error: %0 for success, < %0 for error
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001874 * @nr_bytes: number of bytes to complete @rq
1875 * @bidi_bytes: number of bytes to complete @rq->next_rq
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001876 * @drv_callback: function called between completion of bios in the request
1877 * and completion of the request.
Randy Dunlap710027a2008-08-19 20:13:11 +02001878 * If the callback returns non %0, this helper returns without
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001879 * completion of the request.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001880 *
1881 * Description:
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001882 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001883 * If @rq has leftover, sets it up for the next range of segments.
1884 *
1885 * Return:
Randy Dunlap710027a2008-08-19 20:13:11 +02001886 * %0 - we are done with this request
1887 * %1 - this request is not freed yet, it still has pending buffers.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001888 **/
Jens Axboe22b13212008-01-31 12:36:19 +01001889static int blk_end_io(struct request *rq, int error, unsigned int nr_bytes,
1890 unsigned int bidi_bytes,
1891 int (drv_callback)(struct request *))
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001892{
1893 struct request_queue *q = rq->q;
1894 unsigned long flags = 0UL;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001895
David Woodhousefb2dce82008-08-05 18:01:53 +01001896 if (bio_has_data(rq->bio) || blk_discard_rq(rq)) {
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001897 if (__end_that_request_first(rq, error, nr_bytes))
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001898 return 1;
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001899
1900 /* Bidi request must be completed as a whole */
1901 if (blk_bidi_rq(rq) &&
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001902 __end_that_request_first(rq->next_rq, error, bidi_bytes))
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001903 return 1;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001904 }
1905
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001906 /* Special feature for tricky drivers */
1907 if (drv_callback && drv_callback(rq))
1908 return 1;
1909
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001910 add_disk_randomness(rq->rq_disk);
1911
1912 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05001913 end_that_request_last(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001914 spin_unlock_irqrestore(q->queue_lock, flags);
1915
1916 return 0;
1917}
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001918
1919/**
1920 * blk_end_request - Helper function for drivers to complete the request.
1921 * @rq: the request being processed
Randy Dunlap710027a2008-08-19 20:13:11 +02001922 * @error: %0 for success, < %0 for error
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001923 * @nr_bytes: number of bytes to complete
1924 *
1925 * Description:
1926 * Ends I/O on a number of bytes attached to @rq.
1927 * If @rq has leftover, sets it up for the next range of segments.
1928 *
1929 * Return:
Randy Dunlap710027a2008-08-19 20:13:11 +02001930 * %0 - we are done with this request
1931 * %1 - still buffers pending for this request
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001932 **/
Jens Axboe22b13212008-01-31 12:36:19 +01001933int blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001934{
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001935 return blk_end_io(rq, error, nr_bytes, 0, NULL);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001936}
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001937EXPORT_SYMBOL_GPL(blk_end_request);
1938
1939/**
1940 * __blk_end_request - Helper function for drivers to complete the request.
1941 * @rq: the request being processed
Randy Dunlap710027a2008-08-19 20:13:11 +02001942 * @error: %0 for success, < %0 for error
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001943 * @nr_bytes: number of bytes to complete
1944 *
1945 * Description:
1946 * Must be called with queue lock held unlike blk_end_request().
1947 *
1948 * Return:
Randy Dunlap710027a2008-08-19 20:13:11 +02001949 * %0 - we are done with this request
1950 * %1 - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001951 **/
Jens Axboe22b13212008-01-31 12:36:19 +01001952int __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001953{
David Woodhousefb2dce82008-08-05 18:01:53 +01001954 if ((bio_has_data(rq->bio) || blk_discard_rq(rq)) &&
Jens Axboe051cc392008-08-08 11:06:45 +02001955 __end_that_request_first(rq, error, nr_bytes))
1956 return 1;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001957
1958 add_disk_randomness(rq->rq_disk);
1959
Kiyoshi Uedab8286232007-12-11 17:53:24 -05001960 end_that_request_last(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001961
1962 return 0;
1963}
1964EXPORT_SYMBOL_GPL(__blk_end_request);
1965
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001966/**
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001967 * blk_end_bidi_request - Helper function for drivers to complete bidi request.
1968 * @rq: the bidi request being processed
Randy Dunlap710027a2008-08-19 20:13:11 +02001969 * @error: %0 for success, < %0 for error
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001970 * @nr_bytes: number of bytes to complete @rq
1971 * @bidi_bytes: number of bytes to complete @rq->next_rq
1972 *
1973 * Description:
1974 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
1975 *
1976 * Return:
Randy Dunlap710027a2008-08-19 20:13:11 +02001977 * %0 - we are done with this request
1978 * %1 - still buffers pending for this request
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001979 **/
Jens Axboe22b13212008-01-31 12:36:19 +01001980int blk_end_bidi_request(struct request *rq, int error, unsigned int nr_bytes,
1981 unsigned int bidi_bytes)
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001982{
1983 return blk_end_io(rq, error, nr_bytes, bidi_bytes, NULL);
1984}
1985EXPORT_SYMBOL_GPL(blk_end_bidi_request);
1986
1987/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001988 * blk_end_request_callback - Special helper function for tricky drivers
1989 * @rq: the request being processed
Randy Dunlap710027a2008-08-19 20:13:11 +02001990 * @error: %0 for success, < %0 for error
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001991 * @nr_bytes: number of bytes to complete
1992 * @drv_callback: function called between completion of bios in the request
1993 * and completion of the request.
Randy Dunlap710027a2008-08-19 20:13:11 +02001994 * If the callback returns non %0, this helper returns without
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001995 * completion of the request.
1996 *
1997 * Description:
1998 * Ends I/O on a number of bytes attached to @rq.
1999 * If @rq has leftover, sets it up for the next range of segments.
2000 *
2001 * This special helper function is used only for existing tricky drivers.
2002 * (e.g. cdrom_newpc_intr() of ide-cd)
2003 * This interface will be removed when such drivers are rewritten.
2004 * Don't use this interface in other places anymore.
2005 *
2006 * Return:
Randy Dunlap710027a2008-08-19 20:13:11 +02002007 * %0 - we are done with this request
2008 * %1 - this request is not freed yet.
2009 * this request still has pending buffers or
2010 * the driver doesn't want to finish this request yet.
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002011 **/
Jens Axboe22b13212008-01-31 12:36:19 +01002012int blk_end_request_callback(struct request *rq, int error,
2013 unsigned int nr_bytes,
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002014 int (drv_callback)(struct request *))
2015{
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002016 return blk_end_io(rq, error, nr_bytes, 0, drv_callback);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002017}
2018EXPORT_SYMBOL_GPL(blk_end_request_callback);
2019
Jens Axboe86db1e22008-01-29 14:53:40 +01002020void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2021 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
David Woodhoused628eae2008-08-09 16:22:17 +01002023 /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw, and
2024 we want BIO_RW_AHEAD (bit 1) to imply REQ_FAILFAST (bit 1). */
Jens Axboe4aff5e22006-08-10 08:44:47 +02002025 rq->cmd_flags |= (bio->bi_rw & 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
David Woodhousefb2dce82008-08-05 18:01:53 +01002027 if (bio_has_data(bio)) {
2028 rq->nr_phys_segments = bio_phys_segments(q, bio);
David Woodhousefb2dce82008-08-05 18:01:53 +01002029 rq->buffer = bio_data(bio);
2030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 rq->current_nr_sectors = bio_cur_sectors(bio);
2032 rq->hard_cur_sectors = rq->current_nr_sectors;
2033 rq->hard_nr_sectors = rq->nr_sectors = bio_sectors(bio);
Mike Christie0e75f902006-12-01 10:40:55 +01002034 rq->data_len = bio->bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
2036 rq->bio = rq->biotail = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
NeilBrown66846572007-08-16 13:31:28 +02002038 if (bio->bi_bdev)
2039 rq->rq_disk = bio->bi_bdev->bd_disk;
2040}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
2042int kblockd_schedule_work(struct work_struct *work)
2043{
2044 return queue_work(kblockd_workqueue, work);
2045}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046EXPORT_SYMBOL(kblockd_schedule_work);
2047
Andrew Morton19a75d82007-05-09 02:33:56 -07002048void kblockd_flush_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002050 cancel_work_sync(work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051}
Andrew Morton19a75d82007-05-09 02:33:56 -07002052EXPORT_SYMBOL(kblockd_flush_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
2054int __init blk_dev_init(void)
2055{
Jens Axboeff856ba2006-01-09 16:02:34 +01002056 int i;
2057
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 kblockd_workqueue = create_workqueue("kblockd");
2059 if (!kblockd_workqueue)
2060 panic("Failed to create kblockd\n");
2061
2062 request_cachep = kmem_cache_create("blkdev_requests",
Paul Mundt20c2df82007-07-20 10:11:58 +09002063 sizeof(struct request), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Jens Axboe8324aa92008-01-29 14:51:59 +01002065 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02002066 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002068 for_each_possible_cpu(i)
Jens Axboeff856ba2006-01-09 16:02:34 +01002069 INIT_LIST_HEAD(&per_cpu(blk_cpu_done, i));
2070
Carlos R. Mafra962cf362008-05-15 11:15:37 -03002071 open_softirq(BLOCK_SOFTIRQ, blk_done_softirq);
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07002072 register_hotcpu_notifier(&blk_cpu_notifier);
Jens Axboeff856ba2006-01-09 16:02:34 +01002073
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 return 0;
2075}
2076