blob: 82dc20621c068b27d5af8e97e43427625f128f66 [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 Axboe2056a782006-03-23 20:00:26 +010029#include <linux/blktrace_api.h>
Akinobu Mitac17bb492006-12-08 02:39:46 -080030#include <linux/fault-inject.h>
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +010031#include <trace/block.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Jens Axboe8324aa92008-01-29 14:51:59 +010033#include "blk.h"
34
Ingo Molnar0bfc2452008-11-26 11:59:56 +010035DEFINE_TRACE(block_plug);
36DEFINE_TRACE(block_unplug_io);
37DEFINE_TRACE(block_unplug_timer);
38DEFINE_TRACE(block_getrq);
39DEFINE_TRACE(block_sleeprq);
40DEFINE_TRACE(block_rq_requeue);
41DEFINE_TRACE(block_bio_backmerge);
42DEFINE_TRACE(block_bio_frontmerge);
43DEFINE_TRACE(block_bio_queue);
44DEFINE_TRACE(block_rq_complete);
45DEFINE_TRACE(block_remap); /* Also used in drivers/md/dm.c */
46EXPORT_TRACEPOINT_SYMBOL_GPL(block_remap);
47
Jens Axboe165125e2007-07-24 09:28:11 +020048static int __make_request(struct request_queue *q, struct bio *bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/*
51 * For the allocated request tables
52 */
Adrian Bunk5ece6c52008-02-18 13:45:51 +010053static struct kmem_cache *request_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/*
56 * For queue allocation
57 */
Jens Axboe6728cb02008-01-31 13:03:55 +010058struct kmem_cache *blk_requestq_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 * Controlling structure to kblockd
62 */
Jens Axboeff856ba2006-01-09 16:02:34 +010063static struct workqueue_struct *kblockd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Jens Axboe26b82562008-01-29 13:54:41 +010065static void drive_stat_acct(struct request *rq, int new_io)
66{
Jens Axboe28f13702008-05-07 10:15:46 +020067 struct hd_struct *part;
Jens Axboe26b82562008-01-29 13:54:41 +010068 int rw = rq_data_dir(rq);
Tejun Heoc9959052008-08-25 19:47:21 +090069 int cpu;
Jens Axboe26b82562008-01-29 13:54:41 +010070
Jens Axboec2553b52009-04-24 08:10:11 +020071 if (!blk_do_io_stat(rq))
Jens Axboe26b82562008-01-29 13:54:41 +010072 return;
73
Tejun Heo074a7ac2008-08-25 19:56:14 +090074 cpu = part_stat_lock();
Tejun Heo83096eb2009-05-07 22:24:39 +090075 part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq));
Tejun Heoc9959052008-08-25 19:47:21 +090076
Jens Axboe28f13702008-05-07 10:15:46 +020077 if (!new_io)
Tejun Heo074a7ac2008-08-25 19:56:14 +090078 part_stat_inc(cpu, part, merges[rw]);
Jens Axboe28f13702008-05-07 10:15:46 +020079 else {
Tejun Heo074a7ac2008-08-25 19:56:14 +090080 part_round_stats(cpu, part);
81 part_inc_in_flight(part);
Jens Axboe26b82562008-01-29 13:54:41 +010082 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +020083
Tejun Heo074a7ac2008-08-25 19:56:14 +090084 part_stat_unlock();
Jens Axboe26b82562008-01-29 13:54:41 +010085}
86
Jens Axboe8324aa92008-01-29 14:51:59 +010087void blk_queue_congestion_threshold(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 int nr;
90
91 nr = q->nr_requests - (q->nr_requests / 8) + 1;
92 if (nr > q->nr_requests)
93 nr = q->nr_requests;
94 q->nr_congestion_on = nr;
95
96 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
97 if (nr < 1)
98 nr = 1;
99 q->nr_congestion_off = nr;
100}
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102/**
103 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
104 * @bdev: device
105 *
106 * Locates the passed device's request queue and returns the address of its
107 * backing_dev_info
108 *
109 * Will return NULL if the request queue cannot be located.
110 */
111struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
112{
113 struct backing_dev_info *ret = NULL;
Jens Axboe165125e2007-07-24 09:28:11 +0200114 struct request_queue *q = bdev_get_queue(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 if (q)
117 ret = &q->backing_dev_info;
118 return ret;
119}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120EXPORT_SYMBOL(blk_get_backing_dev_info);
121
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200122void blk_rq_init(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200124 memset(rq, 0, sizeof(*rq));
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 INIT_LIST_HEAD(&rq->queuelist);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700127 INIT_LIST_HEAD(&rq->timeout_list);
Jens Axboec7c22e42008-09-13 20:26:01 +0200128 rq->cpu = -1;
Jens Axboe63a71382008-02-08 12:41:03 +0100129 rq->q = q;
130 rq->sector = rq->hard_sector = (sector_t) -1;
Jens Axboe2e662b62006-07-13 11:55:04 +0200131 INIT_HLIST_NODE(&rq->hash);
132 RB_CLEAR_NODE(&rq->rb_node);
FUJITA Tomonorid7e3c322008-04-29 09:54:39 +0200133 rq->cmd = rq->__cmd;
Li Zefane2494e12009-04-02 13:43:26 +0800134 rq->cmd_len = BLK_MAX_CDB;
Jens Axboe63a71382008-02-08 12:41:03 +0100135 rq->tag = -1;
Jens Axboe63a71382008-02-08 12:41:03 +0100136 rq->ref_count = 1;
Tejun Heob243ddc2009-04-23 11:05:18 +0900137 rq->start_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200139EXPORT_SYMBOL(blk_rq_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
NeilBrown5bb23a62007-09-27 12:46:13 +0200141static void req_bio_endio(struct request *rq, struct bio *bio,
142 unsigned int nbytes, int error)
Tejun Heo797e7db2006-01-06 09:51:03 +0100143{
Jens Axboe165125e2007-07-24 09:28:11 +0200144 struct request_queue *q = rq->q;
Tejun Heo797e7db2006-01-06 09:51:03 +0100145
NeilBrown5bb23a62007-09-27 12:46:13 +0200146 if (&q->bar_rq != rq) {
147 if (error)
148 clear_bit(BIO_UPTODATE, &bio->bi_flags);
149 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
150 error = -EIO;
Tejun Heo797e7db2006-01-06 09:51:03 +0100151
NeilBrown5bb23a62007-09-27 12:46:13 +0200152 if (unlikely(nbytes > bio->bi_size)) {
Jens Axboe6728cb02008-01-31 13:03:55 +0100153 printk(KERN_ERR "%s: want %u bytes done, %u left\n",
Harvey Harrison24c03d42008-05-01 04:35:17 -0700154 __func__, nbytes, bio->bi_size);
NeilBrown5bb23a62007-09-27 12:46:13 +0200155 nbytes = bio->bi_size;
156 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100157
Keith Mannthey08bafc02008-11-25 10:24:35 +0100158 if (unlikely(rq->cmd_flags & REQ_QUIET))
159 set_bit(BIO_QUIET, &bio->bi_flags);
160
NeilBrown5bb23a62007-09-27 12:46:13 +0200161 bio->bi_size -= nbytes;
162 bio->bi_sector += (nbytes >> 9);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200163
164 if (bio_integrity(bio))
165 bio_integrity_advance(bio, nbytes);
166
NeilBrown5bb23a62007-09-27 12:46:13 +0200167 if (bio->bi_size == 0)
NeilBrown6712ecf2007-09-27 12:47:43 +0200168 bio_endio(bio, error);
NeilBrown5bb23a62007-09-27 12:46:13 +0200169 } else {
170
171 /*
172 * Okay, this is the barrier request in progress, just
173 * record the error;
174 */
175 if (error && !q->orderr)
176 q->orderr = error;
177 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100178}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180void blk_dump_rq_flags(struct request *rq, char *msg)
181{
182 int bit;
183
Jens Axboe6728cb02008-01-31 13:03:55 +0100184 printk(KERN_INFO "%s: dev %s: type=%x, flags=%x\n", msg,
Jens Axboe4aff5e22006-08-10 08:44:47 +0200185 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
186 rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Tejun Heo83096eb2009-05-07 22:24:39 +0900188 printk(KERN_INFO " sector %llu, nr/cnr %u/%u\n",
189 (unsigned long long)blk_rq_pos(rq),
190 blk_rq_sectors(rq), blk_rq_cur_sectors(rq));
Tejun Heo731ec492009-04-23 11:05:20 +0900191 printk(KERN_INFO " bio %p, biotail %p, buffer %p, len %u\n",
Jens Axboe6728cb02008-01-31 13:03:55 +0100192 rq->bio, rq->biotail,
Tejun Heo731ec492009-04-23 11:05:20 +0900193 rq->buffer, rq->data_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Jens Axboe4aff5e22006-08-10 08:44:47 +0200195 if (blk_pc_request(rq)) {
Jens Axboe6728cb02008-01-31 13:03:55 +0100196 printk(KERN_INFO " cdb: ");
FUJITA Tomonorid34c87e2008-04-29 14:37:52 +0200197 for (bit = 0; bit < BLK_MAX_CDB; bit++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 printk("%02x ", rq->cmd[bit]);
199 printk("\n");
200 }
201}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202EXPORT_SYMBOL(blk_dump_rq_flags);
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204/*
205 * "plug" the device if there are no outstanding requests: this will
206 * force the transfer to start only after we have put all the requests
207 * on the list.
208 *
209 * This is called with interrupts off and no requests on the queue and
210 * with the queue lock held.
211 */
Jens Axboe165125e2007-07-24 09:28:11 +0200212void blk_plug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 WARN_ON(!irqs_disabled());
215
216 /*
217 * don't plug a stopped queue, it must be paired with blk_start_queue()
218 * which will restart the queueing
219 */
Coywolf Qi Hunt7daac492006-04-19 10:14:49 +0200220 if (blk_queue_stopped(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return;
222
Jens Axboee48ec692008-07-03 13:18:54 +0200223 if (!queue_flag_test_and_set(QUEUE_FLAG_PLUGGED, q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100225 trace_block_plug(q);
Jens Axboe2056a782006-03-23 20:00:26 +0100226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228EXPORT_SYMBOL(blk_plug_device);
229
Jens Axboe6c5e0c42008-08-01 20:31:32 +0200230/**
231 * blk_plug_device_unlocked - plug a device without queue lock held
232 * @q: The &struct request_queue to plug
233 *
234 * Description:
235 * Like @blk_plug_device(), but grabs the queue lock and disables
236 * interrupts.
237 **/
238void blk_plug_device_unlocked(struct request_queue *q)
239{
240 unsigned long flags;
241
242 spin_lock_irqsave(q->queue_lock, flags);
243 blk_plug_device(q);
244 spin_unlock_irqrestore(q->queue_lock, flags);
245}
246EXPORT_SYMBOL(blk_plug_device_unlocked);
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248/*
249 * remove the queue from the plugged list, if present. called with
250 * queue lock held and interrupts disabled.
251 */
Jens Axboe165125e2007-07-24 09:28:11 +0200252int blk_remove_plug(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 WARN_ON(!irqs_disabled());
255
Jens Axboee48ec692008-07-03 13:18:54 +0200256 if (!queue_flag_test_and_clear(QUEUE_FLAG_PLUGGED, q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return 0;
258
259 del_timer(&q->unplug_timer);
260 return 1;
261}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262EXPORT_SYMBOL(blk_remove_plug);
263
264/*
265 * remove the plug and let it rip..
266 */
Jens Axboe165125e2007-07-24 09:28:11 +0200267void __generic_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Coywolf Qi Hunt7daac492006-04-19 10:14:49 +0200269 if (unlikely(blk_queue_stopped(q)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return;
Jens Axboea31a9732008-10-17 13:58:29 +0200271 if (!blk_remove_plug(q) && !blk_queue_nonrot(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return;
273
Jens Axboe22e2c502005-06-27 10:55:12 +0200274 q->request_fn(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277/**
278 * generic_unplug_device - fire a request queue
Jens Axboe165125e2007-07-24 09:28:11 +0200279 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 *
281 * Description:
282 * Linux uses plugging to build bigger requests queues before letting
283 * the device have at them. If a queue is plugged, the I/O scheduler
284 * is still adding and merging requests on the queue. Once the queue
285 * gets unplugged, the request_fn defined for the queue is invoked and
286 * transfers started.
287 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200288void generic_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Jens Axboedbaf2c02008-05-07 09:48:17 +0200290 if (blk_queue_plugged(q)) {
291 spin_lock_irq(q->queue_lock);
292 __generic_unplug_device(q);
293 spin_unlock_irq(q->queue_lock);
294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296EXPORT_SYMBOL(generic_unplug_device);
297
298static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
299 struct page *page)
300{
Jens Axboe165125e2007-07-24 09:28:11 +0200301 struct request_queue *q = bdi->unplug_io_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500303 blk_unplug(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
Jens Axboe86db1e22008-01-29 14:53:40 +0100306void blk_unplug_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Jens Axboe165125e2007-07-24 09:28:11 +0200308 struct request_queue *q =
309 container_of(work, struct request_queue, unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100311 trace_block_unplug_io(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 q->unplug_fn(q);
313}
314
Jens Axboe86db1e22008-01-29 14:53:40 +0100315void blk_unplug_timeout(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Jens Axboe165125e2007-07-24 09:28:11 +0200317 struct request_queue *q = (struct request_queue *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100319 trace_block_unplug_timer(q);
Jens Axboe18887ad2008-07-28 13:08:45 +0200320 kblockd_schedule_work(q, &q->unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500323void blk_unplug(struct request_queue *q)
324{
325 /*
326 * devices don't necessarily have an ->unplug_fn defined
327 */
328 if (q->unplug_fn) {
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100329 trace_block_unplug_io(q);
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500330 q->unplug_fn(q);
331 }
332}
333EXPORT_SYMBOL(blk_unplug);
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335/**
336 * blk_start_queue - restart a previously stopped queue
Jens Axboe165125e2007-07-24 09:28:11 +0200337 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 *
339 * Description:
340 * blk_start_queue() will clear the stop flag on the queue, and call
341 * the request_fn for the queue if it was in a stopped state when
342 * entered. Also see blk_stop_queue(). Queue lock must be held.
343 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200344void blk_start_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200346 WARN_ON(!irqs_disabled());
347
Nick Piggin75ad23b2008-04-29 14:48:33 +0200348 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
Tejun Heoa538cd02009-04-23 11:05:17 +0900349 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351EXPORT_SYMBOL(blk_start_queue);
352
353/**
354 * blk_stop_queue - stop a queue
Jens Axboe165125e2007-07-24 09:28:11 +0200355 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 *
357 * Description:
358 * The Linux block layer assumes that a block driver will consume all
359 * entries on the request queue when the request_fn strategy is called.
360 * Often this will not happen, because of hardware limitations (queue
361 * depth settings). If a device driver gets a 'queue full' response,
362 * or if it simply chooses not to queue more I/O at one point, it can
363 * call this function to prevent the request_fn from being called until
364 * the driver has signalled it's ready to go again. This happens by calling
365 * blk_start_queue() to restart queue operations. Queue lock must be held.
366 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200367void blk_stop_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 blk_remove_plug(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200370 queue_flag_set(QUEUE_FLAG_STOPPED, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372EXPORT_SYMBOL(blk_stop_queue);
373
374/**
375 * blk_sync_queue - cancel any pending callbacks on a queue
376 * @q: the queue
377 *
378 * Description:
379 * The block layer may perform asynchronous callback activity
380 * on a queue, such as calling the unplug function after a timeout.
381 * A block device may call blk_sync_queue to ensure that any
382 * such activity is cancelled, thus allowing it to release resources
Michael Opdenacker59c51592007-05-09 08:57:56 +0200383 * that the callbacks might use. The caller must already have made sure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 * that its ->make_request_fn will not re-add plugging prior to calling
385 * this function.
386 *
387 */
388void blk_sync_queue(struct request_queue *q)
389{
390 del_timer_sync(&q->unplug_timer);
Jens Axboe70ed28b2008-11-19 14:38:39 +0100391 del_timer_sync(&q->timeout);
Cheng Renquan64d01dc2008-12-03 12:41:39 +0100392 cancel_work_sync(&q->unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394EXPORT_SYMBOL(blk_sync_queue);
395
396/**
Jens Axboe80a4b582008-10-14 09:51:06 +0200397 * __blk_run_queue - run a single device queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200399 *
400 * Description:
401 * See @blk_run_queue. This variant must be called with the queue lock
402 * held and interrupts disabled.
403 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 */
Nick Piggin75ad23b2008-04-29 14:48:33 +0200405void __blk_run_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 blk_remove_plug(q);
Jens Axboedac07ec2006-05-11 08:20:16 +0200408
Tejun Heoa538cd02009-04-23 11:05:17 +0900409 if (unlikely(blk_queue_stopped(q)))
410 return;
411
412 if (elv_queue_empty(q))
413 return;
414
Jens Axboedac07ec2006-05-11 08:20:16 +0200415 /*
416 * Only recurse once to avoid overrunning the stack, let the unplug
417 * handling reinvoke the handler shortly if we already got there.
418 */
Tejun Heoa538cd02009-04-23 11:05:17 +0900419 if (!queue_flag_test_and_set(QUEUE_FLAG_REENTER, q)) {
420 q->request_fn(q);
421 queue_flag_clear(QUEUE_FLAG_REENTER, q);
422 } else {
423 queue_flag_set(QUEUE_FLAG_PLUGGED, q);
424 kblockd_schedule_work(q, &q->unplug_work);
425 }
Nick Piggin75ad23b2008-04-29 14:48:33 +0200426}
427EXPORT_SYMBOL(__blk_run_queue);
Jens Axboedac07ec2006-05-11 08:20:16 +0200428
Nick Piggin75ad23b2008-04-29 14:48:33 +0200429/**
430 * blk_run_queue - run a single device queue
431 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200432 *
433 * Description:
434 * Invoke request handling on this queue, if it has pending work to do.
Tejun Heoa7f55792009-04-23 11:05:17 +0900435 * May be used to restart queueing when a request has completed.
Nick Piggin75ad23b2008-04-29 14:48:33 +0200436 */
437void blk_run_queue(struct request_queue *q)
438{
439 unsigned long flags;
440
441 spin_lock_irqsave(q->queue_lock, flags);
442 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 spin_unlock_irqrestore(q->queue_lock, flags);
444}
445EXPORT_SYMBOL(blk_run_queue);
446
Jens Axboe165125e2007-07-24 09:28:11 +0200447void blk_put_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500448{
449 kobject_put(&q->kobj);
450}
Al Viro483f4af2006-03-18 18:34:37 -0500451
Jens Axboe6728cb02008-01-31 13:03:55 +0100452void blk_cleanup_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500453{
Jens Axboee3335de92008-09-18 09:22:54 -0700454 /*
455 * We know we have process context here, so we can be a little
456 * cautious and ensure that pending block actions on this device
457 * are done before moving on. Going into this function, we should
458 * not have processes doing IO to this device.
459 */
460 blk_sync_queue(q);
461
Al Viro483f4af2006-03-18 18:34:37 -0500462 mutex_lock(&q->sysfs_lock);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200463 queue_flag_set_unlocked(QUEUE_FLAG_DEAD, q);
Al Viro483f4af2006-03-18 18:34:37 -0500464 mutex_unlock(&q->sysfs_lock);
465
466 if (q->elevator)
467 elevator_exit(q->elevator);
468
469 blk_put_queue(q);
470}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471EXPORT_SYMBOL(blk_cleanup_queue);
472
Jens Axboe165125e2007-07-24 09:28:11 +0200473static int blk_init_free_list(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
475 struct request_list *rl = &q->rq;
476
Jens Axboe1faa16d2009-04-06 14:48:01 +0200477 rl->count[BLK_RW_SYNC] = rl->count[BLK_RW_ASYNC] = 0;
478 rl->starved[BLK_RW_SYNC] = rl->starved[BLK_RW_ASYNC] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200479 rl->elvpriv = 0;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200480 init_waitqueue_head(&rl->wait[BLK_RW_SYNC]);
481 init_waitqueue_head(&rl->wait[BLK_RW_ASYNC]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Christoph Lameter19460892005-06-23 00:08:19 -0700483 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
484 mempool_free_slab, request_cachep, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 if (!rl->rq_pool)
487 return -ENOMEM;
488
489 return 0;
490}
491
Jens Axboe165125e2007-07-24 09:28:11 +0200492struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Christoph Lameter19460892005-06-23 00:08:19 -0700494 return blk_alloc_queue_node(gfp_mask, -1);
495}
496EXPORT_SYMBOL(blk_alloc_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Jens Axboe165125e2007-07-24 09:28:11 +0200498struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -0700499{
Jens Axboe165125e2007-07-24 09:28:11 +0200500 struct request_queue *q;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700501 int err;
Christoph Lameter19460892005-06-23 00:08:19 -0700502
Jens Axboe8324aa92008-01-29 14:51:59 +0100503 q = kmem_cache_alloc_node(blk_requestq_cachep,
Christoph Lameter94f60302007-07-17 04:03:29 -0700504 gfp_mask | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (!q)
506 return NULL;
507
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700508 q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
509 q->backing_dev_info.unplug_io_data = q;
510 err = bdi_init(&q->backing_dev_info);
511 if (err) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100512 kmem_cache_free(blk_requestq_cachep, q);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700513 return NULL;
514 }
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 init_timer(&q->unplug_timer);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700517 setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
518 INIT_LIST_HEAD(&q->timeout_list);
Peter Zijlstra713ada92008-10-16 13:44:57 +0200519 INIT_WORK(&q->unplug_work, blk_unplug_work);
Al Viro483f4af2006-03-18 18:34:37 -0500520
Jens Axboe8324aa92008-01-29 14:51:59 +0100521 kobject_init(&q->kobj, &blk_queue_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Al Viro483f4af2006-03-18 18:34:37 -0500523 mutex_init(&q->sysfs_lock);
Neil Browne7e72bf2008-05-14 16:05:54 -0700524 spin_lock_init(&q->__queue_lock);
Al Viro483f4af2006-03-18 18:34:37 -0500525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return q;
527}
Christoph Lameter19460892005-06-23 00:08:19 -0700528EXPORT_SYMBOL(blk_alloc_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530/**
531 * blk_init_queue - prepare a request queue for use with a block device
532 * @rfn: The function to be called to process requests that have been
533 * placed on the queue.
534 * @lock: Request queue spin lock
535 *
536 * Description:
537 * If a block device wishes to use the standard request handling procedures,
538 * which sorts requests and coalesces adjacent requests, then it must
539 * call blk_init_queue(). The function @rfn will be called when there
540 * are requests on the queue that need to be processed. If the device
541 * supports plugging, then @rfn may not be called immediately when requests
542 * are available on the queue, but may be called at some time later instead.
543 * Plugged queues are generally unplugged when a buffer belonging to one
544 * of the requests on the queue is needed, or due to memory pressure.
545 *
546 * @rfn is not required, or even expected, to remove all requests off the
547 * queue, but only as many as it can handle at a time. If it does leave
548 * requests on the queue, it is responsible for arranging that the requests
549 * get dealt with eventually.
550 *
551 * The queue spin lock must be held while manipulating the requests on the
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200552 * request queue; this lock will be taken also from interrupt context, so irq
553 * disabling is needed for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 *
Randy Dunlap710027a2008-08-19 20:13:11 +0200555 * Function returns a pointer to the initialized request queue, or %NULL if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 * it didn't succeed.
557 *
558 * Note:
559 * blk_init_queue() must be paired with a blk_cleanup_queue() call
560 * when the block device is deactivated (such as at module unload).
561 **/
Christoph Lameter19460892005-06-23 00:08:19 -0700562
Jens Axboe165125e2007-07-24 09:28:11 +0200563struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Christoph Lameter19460892005-06-23 00:08:19 -0700565 return blk_init_queue_node(rfn, lock, -1);
566}
567EXPORT_SYMBOL(blk_init_queue);
568
Jens Axboe165125e2007-07-24 09:28:11 +0200569struct request_queue *
Christoph Lameter19460892005-06-23 00:08:19 -0700570blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
571{
Jens Axboe165125e2007-07-24 09:28:11 +0200572 struct request_queue *q = blk_alloc_queue_node(GFP_KERNEL, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 if (!q)
575 return NULL;
576
Christoph Lameter19460892005-06-23 00:08:19 -0700577 q->node = node_id;
Al Viro8669aaf2006-03-18 13:50:00 -0500578 if (blk_init_free_list(q)) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100579 kmem_cache_free(blk_requestq_cachep, q);
Al Viro8669aaf2006-03-18 13:50:00 -0500580 return NULL;
581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
152587d2005-04-12 16:22:06 -0500583 /*
584 * if caller didn't supply a lock, they get per-queue locking with
585 * our embedded lock
586 */
Neil Browne7e72bf2008-05-14 16:05:54 -0700587 if (!lock)
152587d2005-04-12 16:22:06 -0500588 lock = &q->__queue_lock;
152587d2005-04-12 16:22:06 -0500589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 q->request_fn = rfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 q->prep_rq_fn = NULL;
592 q->unplug_fn = generic_unplug_device;
Jens Axboebc58ba92009-01-23 10:54:44 +0100593 q->queue_flags = QUEUE_FLAG_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 q->queue_lock = lock;
595
Jens Axboef3b144a2009-03-06 08:48:33 +0100596 /*
597 * This also sets hw/phys segments, boundary and size
598 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 blk_queue_make_request(q, __make_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Alan Stern44ec9542007-02-20 11:01:57 -0500601 q->sg_reserved_size = INT_MAX;
602
FUJITA Tomonoriabf54392008-08-16 14:10:05 +0900603 blk_set_cmd_filter_defaults(&q->cmd_filter);
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 /*
606 * all done
607 */
608 if (!elevator_init(q, NULL)) {
609 blk_queue_congestion_threshold(q);
610 return q;
611 }
612
Al Viro8669aaf2006-03-18 13:50:00 -0500613 blk_put_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 return NULL;
615}
Christoph Lameter19460892005-06-23 00:08:19 -0700616EXPORT_SYMBOL(blk_init_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Jens Axboe165125e2007-07-24 09:28:11 +0200618int blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Nick Pigginfde6ad22005-06-23 00:08:53 -0700620 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
Al Viro483f4af2006-03-18 18:34:37 -0500621 kobject_get(&q->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return 0;
623 }
624
625 return 1;
626}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Jens Axboe165125e2007-07-24 09:28:11 +0200628static inline void blk_free_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Jens Axboe4aff5e22006-08-10 08:44:47 +0200630 if (rq->cmd_flags & REQ_ELVPRIV)
Tejun Heocb98fc82005-10-28 08:29:39 +0200631 elv_put_request(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 mempool_free(rq, q->rq.rq_pool);
633}
634
Jens Axboe1ea25ecb2006-07-18 22:24:11 +0200635static struct request *
Jerome Marchand42dad762009-04-22 14:01:49 +0200636blk_alloc_request(struct request_queue *q, int flags, int priv, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
638 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
639
640 if (!rq)
641 return NULL;
642
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200643 blk_rq_init(q, rq);
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200644
Jerome Marchand42dad762009-04-22 14:01:49 +0200645 rq->cmd_flags = flags | REQ_ALLOCED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Tejun Heocb98fc82005-10-28 08:29:39 +0200647 if (priv) {
Jens Axboecb78b282006-07-28 09:32:57 +0200648 if (unlikely(elv_set_request(q, rq, gfp_mask))) {
Tejun Heocb98fc82005-10-28 08:29:39 +0200649 mempool_free(rq, q->rq.rq_pool);
650 return NULL;
651 }
Jens Axboe4aff5e22006-08-10 08:44:47 +0200652 rq->cmd_flags |= REQ_ELVPRIV;
Tejun Heocb98fc82005-10-28 08:29:39 +0200653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Tejun Heocb98fc82005-10-28 08:29:39 +0200655 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
658/*
659 * ioc_batching returns true if the ioc is a valid batching request and
660 * should be given priority access to a request.
661 */
Jens Axboe165125e2007-07-24 09:28:11 +0200662static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
664 if (!ioc)
665 return 0;
666
667 /*
668 * Make sure the process is able to allocate at least 1 request
669 * even if the batch times out, otherwise we could theoretically
670 * lose wakeups.
671 */
672 return ioc->nr_batch_requests == q->nr_batching ||
673 (ioc->nr_batch_requests > 0
674 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
675}
676
677/*
678 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
679 * will cause the process to be a "batcher" on all queues in the system. This
680 * is the behaviour we want though - once it gets a wakeup it should be given
681 * a nice run.
682 */
Jens Axboe165125e2007-07-24 09:28:11 +0200683static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
685 if (!ioc || ioc_batching(q, ioc))
686 return;
687
688 ioc->nr_batch_requests = q->nr_batching;
689 ioc->last_waited = jiffies;
690}
691
Jens Axboe1faa16d2009-04-06 14:48:01 +0200692static void __freed_request(struct request_queue *q, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
694 struct request_list *rl = &q->rq;
695
Jens Axboe1faa16d2009-04-06 14:48:01 +0200696 if (rl->count[sync] < queue_congestion_off_threshold(q))
697 blk_clear_queue_congested(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Jens Axboe1faa16d2009-04-06 14:48:01 +0200699 if (rl->count[sync] + 1 <= q->nr_requests) {
700 if (waitqueue_active(&rl->wait[sync]))
701 wake_up(&rl->wait[sync]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Jens Axboe1faa16d2009-04-06 14:48:01 +0200703 blk_clear_queue_full(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
705}
706
707/*
708 * A request has just been released. Account for it, update the full and
709 * congestion status, wake up any waiters. Called under q->queue_lock.
710 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200711static void freed_request(struct request_queue *q, int sync, int priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
713 struct request_list *rl = &q->rq;
714
Jens Axboe1faa16d2009-04-06 14:48:01 +0200715 rl->count[sync]--;
Tejun Heocb98fc82005-10-28 08:29:39 +0200716 if (priv)
717 rl->elvpriv--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Jens Axboe1faa16d2009-04-06 14:48:01 +0200719 __freed_request(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Jens Axboe1faa16d2009-04-06 14:48:01 +0200721 if (unlikely(rl->starved[sync ^ 1]))
722 __freed_request(q, sync ^ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725/*
Nick Piggind6344532005-06-28 20:45:14 -0700726 * Get a free request, queue_lock must be held.
727 * Returns NULL on failure, with queue_lock held.
728 * Returns !NULL on success, with queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 */
Jens Axboe165125e2007-07-24 09:28:11 +0200730static struct request *get_request(struct request_queue *q, int rw_flags,
Jens Axboe7749a8d2006-12-13 13:02:26 +0100731 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
733 struct request *rq = NULL;
734 struct request_list *rl = &q->rq;
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100735 struct io_context *ioc = NULL;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200736 const bool is_sync = rw_is_sync(rw_flags) != 0;
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100737 int may_queue, priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Jens Axboe7749a8d2006-12-13 13:02:26 +0100739 may_queue = elv_may_queue(q, rw_flags);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100740 if (may_queue == ELV_MQUEUE_NO)
741 goto rq_starved;
742
Jens Axboe1faa16d2009-04-06 14:48:01 +0200743 if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) {
744 if (rl->count[is_sync]+1 >= q->nr_requests) {
Jens Axboeb5deef92006-07-19 23:39:40 +0200745 ioc = current_io_context(GFP_ATOMIC, q->node);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100746 /*
747 * The queue will fill after this allocation, so set
748 * it as full, and mark this process as "batching".
749 * This process will be allowed to complete a batch of
750 * requests, others will be blocked.
751 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200752 if (!blk_queue_full(q, is_sync)) {
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100753 ioc_set_batching(q, ioc);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200754 blk_set_queue_full(q, is_sync);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100755 } else {
756 if (may_queue != ELV_MQUEUE_MUST
757 && !ioc_batching(q, ioc)) {
758 /*
759 * The queue is full and the allocating
760 * process is not a "batcher", and not
761 * exempted by the IO scheduler
762 */
763 goto out;
764 }
765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
Jens Axboe1faa16d2009-04-06 14:48:01 +0200767 blk_set_queue_congested(q, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769
Jens Axboe082cf692005-06-28 16:35:11 +0200770 /*
771 * Only allow batching queuers to allocate up to 50% over the defined
772 * limit of requests, otherwise we could have thousands of requests
773 * allocated with any setting of ->nr_requests
774 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200775 if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
Jens Axboe082cf692005-06-28 16:35:11 +0200776 goto out;
Hugh Dickinsfd782a42005-06-29 15:15:40 +0100777
Jens Axboe1faa16d2009-04-06 14:48:01 +0200778 rl->count[is_sync]++;
779 rl->starved[is_sync] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200780
Jens Axboe64521d12005-10-28 08:30:39 +0200781 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Tejun Heocb98fc82005-10-28 08:29:39 +0200782 if (priv)
783 rl->elvpriv++;
784
Jerome Marchand42dad762009-04-22 14:01:49 +0200785 if (blk_queue_io_stat(q))
786 rw_flags |= REQ_IO_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 spin_unlock_irq(q->queue_lock);
788
Jens Axboe7749a8d2006-12-13 13:02:26 +0100789 rq = blk_alloc_request(q, rw_flags, priv, gfp_mask);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100790 if (unlikely(!rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 /*
792 * Allocation failed presumably due to memory. Undo anything
793 * we might have messed up.
794 *
795 * Allocating task should really be put onto the front of the
796 * wait queue, but this is pretty rare.
797 */
798 spin_lock_irq(q->queue_lock);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200799 freed_request(q, is_sync, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 /*
802 * in the very unlikely event that allocation failed and no
803 * requests for this direction was pending, mark us starved
804 * so that freeing of a request in the other direction will
805 * notice us. another possible fix would be to split the
806 * rq mempool into READ and WRITE
807 */
808rq_starved:
Jens Axboe1faa16d2009-04-06 14:48:01 +0200809 if (unlikely(rl->count[is_sync] == 0))
810 rl->starved[is_sync] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 goto out;
813 }
814
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100815 /*
816 * ioc may be NULL here, and ioc_batching will be false. That's
817 * OK, if the queue is under the request limit then requests need
818 * not count toward the nr_batch_requests limit. There will always
819 * be some limit enforced by BLK_BATCH_TIME.
820 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (ioc_batching(q, ioc))
822 ioc->nr_batch_requests--;
Jens Axboe6728cb02008-01-31 13:03:55 +0100823
Jens Axboe1faa16d2009-04-06 14:48:01 +0200824 trace_block_getrq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return rq;
827}
828
829/*
830 * No available requests for this queue, unplug the device and wait for some
831 * requests to become available.
Nick Piggind6344532005-06-28 20:45:14 -0700832 *
833 * Called with q->queue_lock held, and returns with it unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 */
Jens Axboe165125e2007-07-24 09:28:11 +0200835static struct request *get_request_wait(struct request_queue *q, int rw_flags,
Jens Axboe22e2c502005-06-27 10:55:12 +0200836 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
Jens Axboe1faa16d2009-04-06 14:48:01 +0200838 const bool is_sync = rw_is_sync(rw_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 struct request *rq;
840
Jens Axboe7749a8d2006-12-13 13:02:26 +0100841 rq = get_request(q, rw_flags, bio, GFP_NOIO);
Nick Piggin450991b2005-06-28 20:45:13 -0700842 while (!rq) {
843 DEFINE_WAIT(wait);
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200844 struct io_context *ioc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 struct request_list *rl = &q->rq;
846
Jens Axboe1faa16d2009-04-06 14:48:01 +0200847 prepare_to_wait_exclusive(&rl->wait[is_sync], &wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 TASK_UNINTERRUPTIBLE);
849
Jens Axboe1faa16d2009-04-06 14:48:01 +0200850 trace_block_sleeprq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200852 __generic_unplug_device(q);
853 spin_unlock_irq(q->queue_lock);
854 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200856 /*
857 * After sleeping, we become a "batching" process and
858 * will be able to allocate at least one request, and
859 * up to a big batch of them for a small period time.
860 * See ioc_batching, ioc_set_batching
861 */
862 ioc = current_io_context(GFP_NOIO, q->node);
863 ioc_set_batching(q, ioc);
Jens Axboe2056a782006-03-23 20:00:26 +0100864
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200865 spin_lock_irq(q->queue_lock);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200866 finish_wait(&rl->wait[is_sync], &wait);
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200867
868 rq = get_request(q, rw_flags, bio, GFP_NOIO);
869 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 return rq;
872}
873
Jens Axboe165125e2007-07-24 09:28:11 +0200874struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
876 struct request *rq;
877
878 BUG_ON(rw != READ && rw != WRITE);
879
Nick Piggind6344532005-06-28 20:45:14 -0700880 spin_lock_irq(q->queue_lock);
881 if (gfp_mask & __GFP_WAIT) {
Jens Axboe22e2c502005-06-27 10:55:12 +0200882 rq = get_request_wait(q, rw, NULL);
Nick Piggind6344532005-06-28 20:45:14 -0700883 } else {
Jens Axboe22e2c502005-06-27 10:55:12 +0200884 rq = get_request(q, rw, NULL, gfp_mask);
Nick Piggind6344532005-06-28 20:45:14 -0700885 if (!rq)
886 spin_unlock_irq(q->queue_lock);
887 }
888 /* q->queue_lock is unlocked at this point */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890 return rq;
891}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892EXPORT_SYMBOL(blk_get_request);
893
894/**
895 * blk_requeue_request - put a request back on queue
896 * @q: request queue where request should be inserted
897 * @rq: request to be inserted
898 *
899 * Description:
900 * Drivers often keep queueing requests until the hardware cannot accept
901 * more, when that condition happens we need to put the request back
902 * on the queue. Must be called with queue lock held.
903 */
Jens Axboe165125e2007-07-24 09:28:11 +0200904void blk_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Jens Axboe242f9dc2008-09-14 05:55:09 -0700906 blk_delete_timer(rq);
907 blk_clear_rq_complete(rq);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100908 trace_block_rq_requeue(q, rq);
Jens Axboe2056a782006-03-23 20:00:26 +0100909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 if (blk_rq_tagged(rq))
911 blk_queue_end_tag(q, rq);
912
913 elv_requeue_request(q, rq);
914}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915EXPORT_SYMBOL(blk_requeue_request);
916
917/**
Randy Dunlap710027a2008-08-19 20:13:11 +0200918 * blk_insert_request - insert a special request into a request queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 * @q: request queue where request should be inserted
920 * @rq: request to be inserted
921 * @at_head: insert request at head or tail of queue
922 * @data: private data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 *
924 * Description:
925 * Many block devices need to execute commands asynchronously, so they don't
926 * block the whole kernel from preemption during request execution. This is
927 * accomplished normally by inserting aritficial requests tagged as
Randy Dunlap710027a2008-08-19 20:13:11 +0200928 * REQ_TYPE_SPECIAL in to the corresponding request queue, and letting them
929 * be scheduled for actual execution by the request queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 *
931 * We have the option of inserting the head or the tail of the queue.
932 * Typically we use the tail for new ioctls and so forth. We use the head
933 * of the queue for things like a QUEUE_FULL message from a device, or a
934 * host that is unable to accept a particular command.
935 */
Jens Axboe165125e2007-07-24 09:28:11 +0200936void blk_insert_request(struct request_queue *q, struct request *rq,
Tejun Heo 867d1192005-04-24 02:06:05 -0500937 int at_head, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
Tejun Heo 867d1192005-04-24 02:06:05 -0500939 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 unsigned long flags;
941
942 /*
943 * tell I/O scheduler that this isn't a regular read/write (ie it
944 * must not attempt merges on this) and that it acts as a soft
945 * barrier
946 */
Jens Axboe4aff5e22006-08-10 08:44:47 +0200947 rq->cmd_type = REQ_TYPE_SPECIAL;
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);
Tejun Heoa7f55792009-04-23 11:05:17 +0900961 __blk_run_queue(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/**
Randy Dunlap496aa8a2008-10-16 07:46:23 +0200997 * part_round_stats() - Round off the performance stats on a struct disk_stats.
998 * @cpu: cpu number for stats access
999 * @part: target partition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 *
1001 * The average IO queue length and utilisation statistics are maintained
1002 * by observing the current state of the queue length and the amount of
1003 * time it has been in this state for.
1004 *
1005 * Normally, that accounting is done on IO completion, but that can result
1006 * in more than a second's worth of IO being accounted for within any one
1007 * second, leading to >100% utilisation. To deal with that, we call this
1008 * function to do a round-off before returning the results when reading
1009 * /proc/diskstats. This accounts immediately for all queue usage up to
1010 * the current jiffies and restarts the counters again.
1011 */
Tejun Heoc9959052008-08-25 19:47:21 +09001012void part_round_stats(int cpu, struct hd_struct *part)
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001013{
1014 unsigned long now = jiffies;
1015
Tejun Heo074a7ac2008-08-25 19:56:14 +09001016 if (part->partno)
1017 part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
1018 part_round_stats_single(cpu, part, now);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001019}
Tejun Heo074a7ac2008-08-25 19:56:14 +09001020EXPORT_SYMBOL_GPL(part_round_stats);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022/*
1023 * queue lock must be held
1024 */
Jens Axboe165125e2007-07-24 09:28:11 +02001025void __blk_put_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 if (unlikely(!q))
1028 return;
1029 if (unlikely(--req->ref_count))
1030 return;
1031
Tejun Heo8922e162005-10-20 16:23:44 +02001032 elv_completed_request(q, req);
1033
Boaz Harrosh1cd96c22009-03-24 12:35:07 +01001034 /* this is a bio leak */
1035 WARN_ON(req->bio != NULL);
1036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 /*
1038 * Request may not have originated from ll_rw_blk. if not,
1039 * it didn't come out of our reserved rq pools
1040 */
Jens Axboe49171e52006-08-10 08:59:11 +02001041 if (req->cmd_flags & REQ_ALLOCED) {
Jens Axboe1faa16d2009-04-06 14:48:01 +02001042 int is_sync = rq_is_sync(req) != 0;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001043 int priv = req->cmd_flags & REQ_ELVPRIV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 BUG_ON(!list_empty(&req->queuelist));
Jens Axboe98170642006-07-28 09:23:08 +02001046 BUG_ON(!hlist_unhashed(&req->hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 blk_free_request(q, req);
Jens Axboe1faa16d2009-04-06 14:48:01 +02001049 freed_request(q, is_sync, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 }
1051}
Mike Christie6e39b69e2005-11-11 05:30:24 -06001052EXPORT_SYMBOL_GPL(__blk_put_request);
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054void blk_put_request(struct request *req)
1055{
Tejun Heo8922e162005-10-20 16:23:44 +02001056 unsigned long flags;
Jens Axboe165125e2007-07-24 09:28:11 +02001057 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
FUJITA Tomonori52a93ba2008-07-15 21:21:45 +02001059 spin_lock_irqsave(q->queue_lock, flags);
1060 __blk_put_request(q, req);
1061 spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063EXPORT_SYMBOL(blk_put_request);
1064
Jens Axboe86db1e22008-01-29 14:53:40 +01001065void init_request_from_bio(struct request *req, struct bio *bio)
Tejun Heo52d9e672006-01-06 09:49:58 +01001066{
Jens Axboec7c22e42008-09-13 20:26:01 +02001067 req->cpu = bio->bi_comp_cpu;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001068 req->cmd_type = REQ_TYPE_FS;
Tejun Heo52d9e672006-01-06 09:49:58 +01001069
1070 /*
1071 * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
1072 */
Mike Christie6000a362008-08-19 18:45:30 -05001073 if (bio_rw_ahead(bio))
1074 req->cmd_flags |= (REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
1075 REQ_FAILFAST_DRIVER);
1076 if (bio_failfast_dev(bio))
1077 req->cmd_flags |= REQ_FAILFAST_DEV;
1078 if (bio_failfast_transport(bio))
1079 req->cmd_flags |= REQ_FAILFAST_TRANSPORT;
1080 if (bio_failfast_driver(bio))
1081 req->cmd_flags |= REQ_FAILFAST_DRIVER;
Tejun Heo52d9e672006-01-06 09:49:58 +01001082
David Woodhousefb2dce82008-08-05 18:01:53 +01001083 if (unlikely(bio_discard(bio))) {
David Woodhousee17fc0a2008-08-09 16:42:20 +01001084 req->cmd_flags |= REQ_DISCARD;
1085 if (bio_barrier(bio))
1086 req->cmd_flags |= REQ_SOFTBARRIER;
David Woodhousefb2dce82008-08-05 18:01:53 +01001087 req->q->prepare_discard_fn(req->q, req);
David Woodhousee17fc0a2008-08-09 16:42:20 +01001088 } else if (unlikely(bio_barrier(bio)))
Tejun Heoe4025f62009-04-23 11:05:17 +09001089 req->cmd_flags |= REQ_HARDBARRIER;
Tejun Heo52d9e672006-01-06 09:49:58 +01001090
Jens Axboeb31dc662006-06-13 08:26:10 +02001091 if (bio_sync(bio))
Jens Axboe4aff5e22006-08-10 08:44:47 +02001092 req->cmd_flags |= REQ_RW_SYNC;
Jens Axboe5404bc72006-08-10 09:01:02 +02001093 if (bio_rw_meta(bio))
1094 req->cmd_flags |= REQ_RW_META;
Jens Axboeaeb6faf2009-04-06 14:48:07 +02001095 if (bio_noidle(bio))
1096 req->cmd_flags |= REQ_NOIDLE;
Jens Axboeb31dc662006-06-13 08:26:10 +02001097
Tejun Heo52d9e672006-01-06 09:49:58 +01001098 req->errors = 0;
1099 req->hard_sector = req->sector = bio->bi_sector;
Tejun Heo52d9e672006-01-06 09:49:58 +01001100 req->ioprio = bio_prio(bio);
NeilBrownbc1c56f2007-08-16 13:31:30 +02001101 blk_rq_bio_prep(req->q, req, bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001102}
1103
Jens Axboe644b2d92009-04-06 14:48:06 +02001104/*
1105 * Only disabling plugging for non-rotational devices if it does tagging
1106 * as well, otherwise we do need the proper merging
1107 */
1108static inline bool queue_should_plug(struct request_queue *q)
1109{
1110 return !(blk_queue_nonrot(q) && blk_queue_tagged(q));
1111}
1112
Jens Axboe165125e2007-07-24 09:28:11 +02001113static int __make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
Nick Piggin450991b2005-06-28 20:45:13 -07001115 struct request *req;
Tejun Heoa7384672008-11-28 13:32:03 +09001116 int el_ret, nr_sectors;
Jens Axboe51da90f2006-07-18 04:14:45 +02001117 const unsigned short prio = bio_prio(bio);
1118 const int sync = bio_sync(bio);
Jens Axboe213d9412009-01-06 09:16:05 +01001119 const int unplug = bio_unplug(bio);
Jens Axboe7749a8d2006-12-13 13:02:26 +01001120 int rw_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 nr_sectors = bio_sectors(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 /*
1125 * low level driver can indicate that it wants pages above a
1126 * certain limit bounced to low memory (ie for highmem, or even
1127 * ISA dma in theory)
1128 */
1129 blk_queue_bounce(q, &bio);
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 spin_lock_irq(q->queue_lock);
1132
Tejun Heoa7384672008-11-28 13:32:03 +09001133 if (unlikely(bio_barrier(bio)) || elv_queue_empty(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 goto get_rq;
1135
1136 el_ret = elv_merge(q, &req, bio);
1137 switch (el_ret) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001138 case ELEVATOR_BACK_MERGE:
1139 BUG_ON(!rq_mergeable(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Jens Axboe6728cb02008-01-31 13:03:55 +01001141 if (!ll_back_merge_fn(q, req, bio))
1142 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001144 trace_block_bio_backmerge(q, bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001145
Jens Axboe6728cb02008-01-31 13:03:55 +01001146 req->biotail->bi_next = bio;
1147 req->biotail = bio;
1148 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
1149 req->ioprio = ioprio_best(req->ioprio, prio);
Jens Axboeab780f12008-08-26 10:25:02 +02001150 if (!blk_rq_cpu_valid(req))
1151 req->cpu = bio->bi_comp_cpu;
Jens Axboe6728cb02008-01-31 13:03:55 +01001152 drive_stat_acct(req, 0);
1153 if (!attempt_back_merge(q, req))
1154 elv_merged_request(q, req, el_ret);
1155 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Jens Axboe6728cb02008-01-31 13:03:55 +01001157 case ELEVATOR_FRONT_MERGE:
1158 BUG_ON(!rq_mergeable(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Jens Axboe6728cb02008-01-31 13:03:55 +01001160 if (!ll_front_merge_fn(q, req, bio))
1161 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001163 trace_block_bio_frontmerge(q, bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001164
Jens Axboe6728cb02008-01-31 13:03:55 +01001165 bio->bi_next = req->bio;
1166 req->bio = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Jens Axboe6728cb02008-01-31 13:03:55 +01001168 /*
1169 * may not be valid. if the low level driver said
1170 * it didn't need a bounce buffer then it better
1171 * not touch req->buffer either...
1172 */
1173 req->buffer = bio_data(bio);
1174 req->current_nr_sectors = bio_cur_sectors(bio);
1175 req->hard_cur_sectors = req->current_nr_sectors;
1176 req->sector = req->hard_sector = bio->bi_sector;
1177 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
1178 req->ioprio = ioprio_best(req->ioprio, prio);
Jens Axboeab780f12008-08-26 10:25:02 +02001179 if (!blk_rq_cpu_valid(req))
1180 req->cpu = bio->bi_comp_cpu;
Jens Axboe6728cb02008-01-31 13:03:55 +01001181 drive_stat_acct(req, 0);
1182 if (!attempt_front_merge(q, req))
1183 elv_merged_request(q, req, el_ret);
1184 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Jens Axboe6728cb02008-01-31 13:03:55 +01001186 /* ELV_NO_MERGE: elevator says don't/can't merge. */
1187 default:
1188 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 }
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191get_rq:
Nick Piggin450991b2005-06-28 20:45:13 -07001192 /*
Jens Axboe7749a8d2006-12-13 13:02:26 +01001193 * This sync check and mask will be re-done in init_request_from_bio(),
1194 * but we need to set it earlier to expose the sync flag to the
1195 * rq allocator and io schedulers.
1196 */
1197 rw_flags = bio_data_dir(bio);
1198 if (sync)
1199 rw_flags |= REQ_RW_SYNC;
1200
1201 /*
Nick Piggin450991b2005-06-28 20:45:13 -07001202 * Grab a free request. This is might sleep but can not fail.
Nick Piggind6344532005-06-28 20:45:14 -07001203 * Returns with the queue unlocked.
Nick Piggin450991b2005-06-28 20:45:13 -07001204 */
Jens Axboe7749a8d2006-12-13 13:02:26 +01001205 req = get_request_wait(q, rw_flags, bio);
Nick Piggind6344532005-06-28 20:45:14 -07001206
Nick Piggin450991b2005-06-28 20:45:13 -07001207 /*
1208 * After dropping the lock and possibly sleeping here, our request
1209 * may now be mergeable after it had proven unmergeable (above).
1210 * We don't worry about that case for efficiency. It won't happen
1211 * often, and the elevators are able to handle it.
1212 */
Tejun Heo52d9e672006-01-06 09:49:58 +01001213 init_request_from_bio(req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Nick Piggin450991b2005-06-28 20:45:13 -07001215 spin_lock_irq(q->queue_lock);
Jens Axboec7c22e42008-09-13 20:26:01 +02001216 if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags) ||
1217 bio_flagged(bio, BIO_CPU_AFFINE))
1218 req->cpu = blk_cpu_to_group(smp_processor_id());
Jens Axboe644b2d92009-04-06 14:48:06 +02001219 if (queue_should_plug(q) && elv_queue_empty(q))
Nick Piggin450991b2005-06-28 20:45:13 -07001220 blk_plug_device(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 add_request(q, req);
1222out:
Jens Axboe644b2d92009-04-06 14:48:06 +02001223 if (unplug || !queue_should_plug(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 __generic_unplug_device(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 spin_unlock_irq(q->queue_lock);
1226 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227}
1228
1229/*
1230 * If bio->bi_dev is a partition, remap the location
1231 */
1232static inline void blk_partition_remap(struct bio *bio)
1233{
1234 struct block_device *bdev = bio->bi_bdev;
1235
Jens Axboebf2de6f2007-09-27 13:01:25 +02001236 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 struct hd_struct *p = bdev->bd_part;
Jens Axboea3623572005-11-01 09:26:16 +01001238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 bio->bi_sector += p->start_sect;
1240 bio->bi_bdev = bdev->bd_contains;
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001241
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001242 trace_block_remap(bdev_get_queue(bio->bi_bdev), bio,
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001243 bdev->bd_dev, bio->bi_sector,
1244 bio->bi_sector - p->start_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
1246}
1247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248static void handle_bad_sector(struct bio *bio)
1249{
1250 char b[BDEVNAME_SIZE];
1251
1252 printk(KERN_INFO "attempt to access beyond end of device\n");
1253 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1254 bdevname(bio->bi_bdev, b),
1255 bio->bi_rw,
1256 (unsigned long long)bio->bi_sector + bio_sectors(bio),
1257 (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
1258
1259 set_bit(BIO_EOF, &bio->bi_flags);
1260}
1261
Akinobu Mitac17bb492006-12-08 02:39:46 -08001262#ifdef CONFIG_FAIL_MAKE_REQUEST
1263
1264static DECLARE_FAULT_ATTR(fail_make_request);
1265
1266static int __init setup_fail_make_request(char *str)
1267{
1268 return setup_fault_attr(&fail_make_request, str);
1269}
1270__setup("fail_make_request=", setup_fail_make_request);
1271
1272static int should_fail_request(struct bio *bio)
1273{
Tejun Heoeddb2e22008-08-25 19:56:13 +09001274 struct hd_struct *part = bio->bi_bdev->bd_part;
1275
1276 if (part_to_disk(part)->part0.make_it_fail || part->make_it_fail)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001277 return should_fail(&fail_make_request, bio->bi_size);
1278
1279 return 0;
1280}
1281
1282static int __init fail_make_request_debugfs(void)
1283{
1284 return init_fault_attr_dentries(&fail_make_request,
1285 "fail_make_request");
1286}
1287
1288late_initcall(fail_make_request_debugfs);
1289
1290#else /* CONFIG_FAIL_MAKE_REQUEST */
1291
1292static inline int should_fail_request(struct bio *bio)
1293{
1294 return 0;
1295}
1296
1297#endif /* CONFIG_FAIL_MAKE_REQUEST */
1298
Jens Axboec07e2b42007-07-18 13:27:58 +02001299/*
1300 * Check whether this bio extends beyond the end of the device.
1301 */
1302static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1303{
1304 sector_t maxsector;
1305
1306 if (!nr_sectors)
1307 return 0;
1308
1309 /* Test device or partition size, when known. */
1310 maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
1311 if (maxsector) {
1312 sector_t sector = bio->bi_sector;
1313
1314 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1315 /*
1316 * This may well happen - the kernel calls bread()
1317 * without checking the size of the device, e.g., when
1318 * mounting a device.
1319 */
1320 handle_bad_sector(bio);
1321 return 1;
1322 }
1323 }
1324
1325 return 0;
1326}
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001329 * generic_make_request - hand a buffer to its device driver for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 * @bio: The bio describing the location in memory and on the device.
1331 *
1332 * generic_make_request() is used to make I/O requests of block
1333 * devices. It is passed a &struct bio, which describes the I/O that needs
1334 * to be done.
1335 *
1336 * generic_make_request() does not return any status. The
1337 * success/failure status of the request, along with notification of
1338 * completion, is delivered asynchronously through the bio->bi_end_io
1339 * function described (one day) else where.
1340 *
1341 * The caller of generic_make_request must make sure that bi_io_vec
1342 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1343 * set to describe the device address, and the
1344 * bi_end_io and optionally bi_private are set to describe how
1345 * completion notification should be signaled.
1346 *
1347 * generic_make_request and the drivers it calls may use bi_next if this
1348 * bio happens to be merged with someone else, and may change bi_dev and
1349 * bi_sector for remaps as it sees fit. So the values of these fields
1350 * should NOT be depended on after the call to generic_make_request.
1351 */
Neil Brownd89d8792007-05-01 09:53:42 +02001352static inline void __generic_make_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
Jens Axboe165125e2007-07-24 09:28:11 +02001354 struct request_queue *q;
NeilBrown5ddfe962006-10-30 22:07:21 -08001355 sector_t old_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 int ret, nr_sectors = bio_sectors(bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001357 dev_t old_dev;
Jens Axboe51fd77b2007-11-02 08:49:08 +01001358 int err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Jens Axboec07e2b42007-07-18 13:27:58 +02001362 if (bio_check_eod(bio, nr_sectors))
1363 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 /*
1366 * Resolve the mapping until finished. (drivers are
1367 * still free to implement/resolve their own stacking
1368 * by explicitly returning 0)
1369 *
1370 * NOTE: we don't repeat the blk_size check for each new device.
1371 * Stacking drivers are expected to know what they are doing.
1372 */
NeilBrown5ddfe962006-10-30 22:07:21 -08001373 old_sector = -1;
Jens Axboe2056a782006-03-23 20:00:26 +01001374 old_dev = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 do {
1376 char b[BDEVNAME_SIZE];
1377
1378 q = bdev_get_queue(bio->bi_bdev);
Tejun Heoa7384672008-11-28 13:32:03 +09001379 if (unlikely(!q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 printk(KERN_ERR
1381 "generic_make_request: Trying to access "
1382 "nonexistent block-device %s (%Lu)\n",
1383 bdevname(bio->bi_bdev, b),
1384 (long long) bio->bi_sector);
Tejun Heoa7384672008-11-28 13:32:03 +09001385 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
1387
Jens Axboe4fa253f2007-07-18 13:13:10 +02001388 if (unlikely(nr_sectors > q->max_hw_sectors)) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001389 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 bdevname(bio->bi_bdev, b),
1391 bio_sectors(bio),
1392 q->max_hw_sectors);
1393 goto end_io;
1394 }
1395
Nick Pigginfde6ad22005-06-23 00:08:53 -07001396 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 goto end_io;
1398
Akinobu Mitac17bb492006-12-08 02:39:46 -08001399 if (should_fail_request(bio))
1400 goto end_io;
1401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 /*
1403 * If this device has partitions, remap block n
1404 * of partition p to block n+start(p) of the disk.
1405 */
1406 blk_partition_remap(bio);
1407
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +02001408 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio))
1409 goto end_io;
1410
NeilBrown5ddfe962006-10-30 22:07:21 -08001411 if (old_sector != -1)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001412 trace_block_remap(q, bio, old_dev, bio->bi_sector,
NeilBrown5ddfe962006-10-30 22:07:21 -08001413 old_sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001414
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001415 trace_block_bio_queue(q, bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001416
NeilBrown5ddfe962006-10-30 22:07:21 -08001417 old_sector = bio->bi_sector;
Jens Axboe2056a782006-03-23 20:00:26 +01001418 old_dev = bio->bi_bdev->bd_dev;
1419
Jens Axboec07e2b42007-07-18 13:27:58 +02001420 if (bio_check_eod(bio, nr_sectors))
1421 goto end_io;
Tejun Heoa7384672008-11-28 13:32:03 +09001422
1423 if (bio_discard(bio) && !q->prepare_discard_fn) {
Jens Axboe51fd77b2007-11-02 08:49:08 +01001424 err = -EOPNOTSUPP;
1425 goto end_io;
1426 }
Jens Axboecec07072009-01-13 15:28:32 +01001427 if (bio_barrier(bio) && bio_has_data(bio) &&
1428 (q->next_ordered == QUEUE_ORDERED_NONE)) {
1429 err = -EOPNOTSUPP;
1430 goto end_io;
1431 }
NeilBrown5ddfe962006-10-30 22:07:21 -08001432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 ret = q->make_request_fn(q, bio);
1434 } while (ret);
Tejun Heoa7384672008-11-28 13:32:03 +09001435
1436 return;
1437
1438end_io:
1439 bio_endio(bio, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440}
1441
Neil Brownd89d8792007-05-01 09:53:42 +02001442/*
1443 * We only want one ->make_request_fn to be active at a time,
1444 * else stack usage with stacked devices could be a problem.
1445 * So use current->bio_{list,tail} to keep a list of requests
1446 * submited by a make_request_fn function.
1447 * current->bio_tail is also used as a flag to say if
1448 * generic_make_request is currently active in this task or not.
1449 * If it is NULL, then no make_request is active. If it is non-NULL,
1450 * then a make_request is active, and new requests should be added
1451 * at the tail
1452 */
1453void generic_make_request(struct bio *bio)
1454{
1455 if (current->bio_tail) {
1456 /* make_request is active */
1457 *(current->bio_tail) = bio;
1458 bio->bi_next = NULL;
1459 current->bio_tail = &bio->bi_next;
1460 return;
1461 }
1462 /* following loop may be a bit non-obvious, and so deserves some
1463 * explanation.
1464 * Before entering the loop, bio->bi_next is NULL (as all callers
1465 * ensure that) so we have a list with a single bio.
1466 * We pretend that we have just taken it off a longer list, so
1467 * we assign bio_list to the next (which is NULL) and bio_tail
1468 * to &bio_list, thus initialising the bio_list of new bios to be
1469 * added. __generic_make_request may indeed add some more bios
1470 * through a recursive call to generic_make_request. If it
1471 * did, we find a non-NULL value in bio_list and re-enter the loop
1472 * from the top. In this case we really did just take the bio
1473 * of the top of the list (no pretending) and so fixup bio_list and
1474 * bio_tail or bi_next, and call into __generic_make_request again.
1475 *
1476 * The loop was structured like this to make only one call to
1477 * __generic_make_request (which is important as it is large and
1478 * inlined) and to keep the structure simple.
1479 */
1480 BUG_ON(bio->bi_next);
1481 do {
1482 current->bio_list = bio->bi_next;
1483 if (bio->bi_next == NULL)
1484 current->bio_tail = &current->bio_list;
1485 else
1486 bio->bi_next = NULL;
1487 __generic_make_request(bio);
1488 bio = current->bio_list;
1489 } while (bio);
1490 current->bio_tail = NULL; /* deactivate */
1491}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492EXPORT_SYMBOL(generic_make_request);
1493
1494/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001495 * submit_bio - submit a bio to the block device layer for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1497 * @bio: The &struct bio which describes the I/O
1498 *
1499 * submit_bio() is very similar in purpose to generic_make_request(), and
1500 * uses that function to do most of the work. Both are fairly rough
Randy Dunlap710027a2008-08-19 20:13:11 +02001501 * interfaces; @bio must be presetup and ready for I/O.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 *
1503 */
1504void submit_bio(int rw, struct bio *bio)
1505{
1506 int count = bio_sectors(bio);
1507
Jens Axboe22e2c502005-06-27 10:55:12 +02001508 bio->bi_rw |= rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Jens Axboebf2de6f2007-09-27 13:01:25 +02001510 /*
1511 * If it's a regular read/write or a barrier with data attached,
1512 * go through the normal accounting stuff before submission.
1513 */
Jens Axboea9c701e2008-08-08 11:04:44 +02001514 if (bio_has_data(bio)) {
Jens Axboebf2de6f2007-09-27 13:01:25 +02001515 if (rw & WRITE) {
1516 count_vm_events(PGPGOUT, count);
1517 } else {
1518 task_io_account_read(bio->bi_size);
1519 count_vm_events(PGPGIN, count);
1520 }
1521
1522 if (unlikely(block_dump)) {
1523 char b[BDEVNAME_SIZE];
1524 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001525 current->comm, task_pid_nr(current),
Jens Axboebf2de6f2007-09-27 13:01:25 +02001526 (rw & WRITE) ? "WRITE" : "READ",
1527 (unsigned long long)bio->bi_sector,
Jens Axboe6728cb02008-01-31 13:03:55 +01001528 bdevname(bio->bi_bdev, b));
Jens Axboebf2de6f2007-09-27 13:01:25 +02001529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 }
1531
1532 generic_make_request(bio);
1533}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534EXPORT_SYMBOL(submit_bio);
1535
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001536/**
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001537 * blk_rq_check_limits - Helper function to check a request for the queue limit
1538 * @q: the queue
1539 * @rq: the request being checked
1540 *
1541 * Description:
1542 * @rq may have been made based on weaker limitations of upper-level queues
1543 * in request stacking drivers, and it may violate the limitation of @q.
1544 * Since the block layer and the underlying device driver trust @rq
1545 * after it is inserted to @q, it should be checked against @q before
1546 * the insertion using this generic function.
1547 *
1548 * This function should also be useful for request stacking drivers
1549 * in some cases below, so export this fuction.
1550 * Request stacking drivers like request-based dm may change the queue
1551 * limits while requests are in the queue (e.g. dm's table swapping).
1552 * Such request stacking drivers should check those requests agaist
1553 * the new queue limits again when they dispatch those requests,
1554 * although such checkings are also done against the old queue limits
1555 * when submitting requests.
1556 */
1557int blk_rq_check_limits(struct request_queue *q, struct request *rq)
1558{
Tejun Heo83096eb2009-05-07 22:24:39 +09001559 if (blk_rq_sectors(rq) > q->max_sectors ||
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001560 rq->data_len > q->max_hw_sectors << 9) {
1561 printk(KERN_ERR "%s: over max size limit.\n", __func__);
1562 return -EIO;
1563 }
1564
1565 /*
1566 * queue's settings related to segment counting like q->bounce_pfn
1567 * may differ from that of other stacking queues.
1568 * Recalculate it to check the request correctly on this queue's
1569 * limitation.
1570 */
1571 blk_recalc_rq_segments(rq);
1572 if (rq->nr_phys_segments > q->max_phys_segments ||
1573 rq->nr_phys_segments > q->max_hw_segments) {
1574 printk(KERN_ERR "%s: over max segments limit.\n", __func__);
1575 return -EIO;
1576 }
1577
1578 return 0;
1579}
1580EXPORT_SYMBOL_GPL(blk_rq_check_limits);
1581
1582/**
1583 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
1584 * @q: the queue to submit the request
1585 * @rq: the request being queued
1586 */
1587int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
1588{
1589 unsigned long flags;
1590
1591 if (blk_rq_check_limits(q, rq))
1592 return -EIO;
1593
1594#ifdef CONFIG_FAIL_MAKE_REQUEST
1595 if (rq->rq_disk && rq->rq_disk->part0.make_it_fail &&
1596 should_fail(&fail_make_request, blk_rq_bytes(rq)))
1597 return -EIO;
1598#endif
1599
1600 spin_lock_irqsave(q->queue_lock, flags);
1601
1602 /*
1603 * Submitting request must be dequeued before calling this function
1604 * because it will be linked to another request_queue
1605 */
1606 BUG_ON(blk_queued_rq(rq));
1607
1608 drive_stat_acct(rq, 1);
1609 __elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 0);
1610
1611 spin_unlock_irqrestore(q->queue_lock, flags);
1612
1613 return 0;
1614}
1615EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
1616
1617/**
Tejun Heo53a08802008-12-03 12:41:26 +01001618 * blkdev_dequeue_request - dequeue request and start timeout timer
1619 * @req: request to dequeue
1620 *
1621 * Dequeue @req and start timeout timer on it. This hands off the
1622 * request to the driver.
1623 *
1624 * Block internal functions which don't want to start timer should
1625 * call elv_dequeue_request().
1626 */
1627void blkdev_dequeue_request(struct request *req)
1628{
1629 elv_dequeue_request(req->q, req);
1630
1631 /*
1632 * We are now handing the request to the hardware, add the
1633 * timeout handler.
1634 */
1635 blk_add_timer(req);
1636}
1637EXPORT_SYMBOL(blkdev_dequeue_request);
1638
Jens Axboebc58ba92009-01-23 10:54:44 +01001639static void blk_account_io_completion(struct request *req, unsigned int bytes)
1640{
Jens Axboec2553b52009-04-24 08:10:11 +02001641 if (blk_do_io_stat(req)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01001642 const int rw = rq_data_dir(req);
1643 struct hd_struct *part;
1644 int cpu;
1645
1646 cpu = part_stat_lock();
Tejun Heo83096eb2009-05-07 22:24:39 +09001647 part = disk_map_sector_rcu(req->rq_disk, blk_rq_pos(req));
Jens Axboebc58ba92009-01-23 10:54:44 +01001648 part_stat_add(cpu, part, sectors[rw], bytes >> 9);
1649 part_stat_unlock();
1650 }
1651}
1652
1653static void blk_account_io_done(struct request *req)
1654{
Jens Axboebc58ba92009-01-23 10:54:44 +01001655 /*
1656 * Account IO completion. bar_rq isn't accounted as a normal
1657 * IO on queueing nor completion. Accounting the containing
1658 * request is enough.
1659 */
Jens Axboec2553b52009-04-24 08:10:11 +02001660 if (blk_do_io_stat(req) && req != &req->q->bar_rq) {
Jens Axboebc58ba92009-01-23 10:54:44 +01001661 unsigned long duration = jiffies - req->start_time;
1662 const int rw = rq_data_dir(req);
1663 struct hd_struct *part;
1664 int cpu;
1665
1666 cpu = part_stat_lock();
Tejun Heo83096eb2009-05-07 22:24:39 +09001667 part = disk_map_sector_rcu(req->rq_disk, blk_rq_pos(req));
Jens Axboebc58ba92009-01-23 10:54:44 +01001668
1669 part_stat_inc(cpu, part, ios[rw]);
1670 part_stat_add(cpu, part, ticks[rw], duration);
1671 part_round_stats(cpu, part);
1672 part_dec_in_flight(part);
1673
1674 part_stat_unlock();
1675 }
1676}
1677
Tejun Heo53a08802008-12-03 12:41:26 +01001678/**
Tejun Heo5efccd12009-04-23 11:05:18 +09001679 * blk_rq_bytes - Returns bytes left to complete in the entire request
1680 * @rq: the request being processed
1681 **/
1682unsigned int blk_rq_bytes(struct request *rq)
1683{
1684 if (blk_fs_request(rq))
Tejun Heo5b936292009-05-07 22:24:38 +09001685 return blk_rq_sectors(rq) << 9;
Tejun Heo5efccd12009-04-23 11:05:18 +09001686
1687 return rq->data_len;
1688}
1689EXPORT_SYMBOL_GPL(blk_rq_bytes);
1690
1691/**
1692 * blk_rq_cur_bytes - Returns bytes left to complete in the current segment
1693 * @rq: the request being processed
1694 **/
1695unsigned int blk_rq_cur_bytes(struct request *rq)
1696{
1697 if (blk_fs_request(rq))
1698 return rq->current_nr_sectors << 9;
1699
1700 if (rq->bio)
1701 return rq->bio->bi_size;
1702
1703 return rq->data_len;
1704}
1705EXPORT_SYMBOL_GPL(blk_rq_cur_bytes);
1706
Tejun Heo158dbda2009-04-23 11:05:18 +09001707struct request *elv_next_request(struct request_queue *q)
1708{
1709 struct request *rq;
1710 int ret;
1711
1712 while ((rq = __elv_next_request(q)) != NULL) {
1713 if (!(rq->cmd_flags & REQ_STARTED)) {
1714 /*
1715 * This is the first time the device driver
1716 * sees this request (possibly after
1717 * requeueing). Notify IO scheduler.
1718 */
1719 if (blk_sorted_rq(rq))
1720 elv_activate_rq(q, rq);
1721
1722 /*
1723 * just mark as started even if we don't start
1724 * it, a request that has been delayed should
1725 * not be passed by new incoming requests
1726 */
1727 rq->cmd_flags |= REQ_STARTED;
1728 trace_block_rq_issue(q, rq);
1729 }
1730
1731 if (!q->boundary_rq || q->boundary_rq == rq) {
1732 q->end_sector = rq_end_sector(rq);
1733 q->boundary_rq = NULL;
1734 }
1735
1736 if (rq->cmd_flags & REQ_DONTPREP)
1737 break;
1738
1739 if (q->dma_drain_size && rq->data_len) {
1740 /*
1741 * make sure space for the drain appears we
1742 * know we can do this because max_hw_segments
1743 * has been adjusted to be one fewer than the
1744 * device can handle
1745 */
1746 rq->nr_phys_segments++;
1747 }
1748
1749 if (!q->prep_rq_fn)
1750 break;
1751
1752 ret = q->prep_rq_fn(q, rq);
1753 if (ret == BLKPREP_OK) {
1754 break;
1755 } else if (ret == BLKPREP_DEFER) {
1756 /*
1757 * the request may have been (partially) prepped.
1758 * we need to keep this request in the front to
1759 * avoid resource deadlock. REQ_STARTED will
1760 * prevent other fs requests from passing this one.
1761 */
1762 if (q->dma_drain_size && rq->data_len &&
1763 !(rq->cmd_flags & REQ_DONTPREP)) {
1764 /*
1765 * remove the space for the drain we added
1766 * so that we don't add it again
1767 */
1768 --rq->nr_phys_segments;
1769 }
1770
1771 rq = NULL;
1772 break;
1773 } else if (ret == BLKPREP_KILL) {
1774 rq->cmd_flags |= REQ_QUIET;
Tejun Heo40cbbb72009-04-23 11:05:19 +09001775 __blk_end_request_all(rq, -EIO);
Tejun Heo158dbda2009-04-23 11:05:18 +09001776 } else {
1777 printk(KERN_ERR "%s: bad return=%d\n", __func__, ret);
1778 break;
1779 }
1780 }
1781
1782 return rq;
1783}
1784EXPORT_SYMBOL(elv_next_request);
1785
1786void elv_dequeue_request(struct request_queue *q, struct request *rq)
1787{
1788 BUG_ON(list_empty(&rq->queuelist));
1789 BUG_ON(ELV_ON_HASH(rq));
1790
1791 list_del_init(&rq->queuelist);
1792
1793 /*
1794 * the time frame between a request being removed from the lists
1795 * and to it is freed is accounted as io that is in progress at
1796 * the driver side.
1797 */
1798 if (blk_account_rq(rq))
1799 q->in_flight++;
1800}
1801
Tejun Heo5efccd12009-04-23 11:05:18 +09001802/**
Tejun Heo2e60e022009-04-23 11:05:18 +09001803 * blk_update_request - Special helper function for request stacking drivers
1804 * @rq: the request being processed
Randy Dunlap710027a2008-08-19 20:13:11 +02001805 * @error: %0 for success, < %0 for error
Tejun Heo2e60e022009-04-23 11:05:18 +09001806 * @nr_bytes: number of bytes to complete @rq
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001807 *
1808 * Description:
Tejun Heo2e60e022009-04-23 11:05:18 +09001809 * Ends I/O on a number of bytes attached to @rq, but doesn't complete
1810 * the request structure even if @rq doesn't have leftover.
1811 * If @rq has leftover, sets it up for the next range of segments.
1812 *
1813 * This special helper function is only for request stacking drivers
1814 * (e.g. request-based dm) so that they can handle partial completion.
1815 * Actual device drivers should use blk_end_request instead.
1816 *
1817 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
1818 * %false return from this function.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001819 *
1820 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09001821 * %false - this request doesn't have any more data
1822 * %true - this request has more data
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001823 **/
Tejun Heo2e60e022009-04-23 11:05:18 +09001824bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001826 int total_bytes, bio_nbytes, next_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 struct bio *bio;
1828
Tejun Heo2e60e022009-04-23 11:05:18 +09001829 if (!req->bio)
1830 return false;
1831
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001832 trace_block_rq_complete(req->q, req);
Jens Axboe2056a782006-03-23 20:00:26 +01001833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 /*
Tejun Heo924cec72009-04-19 07:00:41 +09001835 * For fs requests, rq is just carrier of independent bio's
1836 * and each partial completion should be handled separately.
1837 * Reset per-request error on each partial completion.
1838 *
1839 * TODO: tj: This is too subtle. It would be better to let
1840 * low level drivers do what they see fit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 */
Tejun Heo924cec72009-04-19 07:00:41 +09001842 if (blk_fs_request(req))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 req->errors = 0;
1844
Jens Axboe6728cb02008-01-31 13:03:55 +01001845 if (error && (blk_fs_request(req) && !(req->cmd_flags & REQ_QUIET))) {
1846 printk(KERN_ERR "end_request: I/O error, dev %s, sector %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 req->rq_disk ? req->rq_disk->disk_name : "?",
Tejun Heo83096eb2009-05-07 22:24:39 +09001848 (unsigned long long)blk_rq_pos(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 }
1850
Jens Axboebc58ba92009-01-23 10:54:44 +01001851 blk_account_io_completion(req, nr_bytes);
Jens Axboed72d9042005-11-01 08:35:42 +01001852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 total_bytes = bio_nbytes = 0;
1854 while ((bio = req->bio) != NULL) {
1855 int nbytes;
1856
1857 if (nr_bytes >= bio->bi_size) {
1858 req->bio = bio->bi_next;
1859 nbytes = bio->bi_size;
NeilBrown5bb23a62007-09-27 12:46:13 +02001860 req_bio_endio(req, bio, nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 next_idx = 0;
1862 bio_nbytes = 0;
1863 } else {
1864 int idx = bio->bi_idx + next_idx;
1865
1866 if (unlikely(bio->bi_idx >= bio->bi_vcnt)) {
1867 blk_dump_rq_flags(req, "__end_that");
Jens Axboe6728cb02008-01-31 13:03:55 +01001868 printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
Harvey Harrison24c03d42008-05-01 04:35:17 -07001869 __func__, bio->bi_idx, bio->bi_vcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 break;
1871 }
1872
1873 nbytes = bio_iovec_idx(bio, idx)->bv_len;
1874 BIO_BUG_ON(nbytes > bio->bi_size);
1875
1876 /*
1877 * not a complete bvec done
1878 */
1879 if (unlikely(nbytes > nr_bytes)) {
1880 bio_nbytes += nr_bytes;
1881 total_bytes += nr_bytes;
1882 break;
1883 }
1884
1885 /*
1886 * advance to the next vector
1887 */
1888 next_idx++;
1889 bio_nbytes += nbytes;
1890 }
1891
1892 total_bytes += nbytes;
1893 nr_bytes -= nbytes;
1894
Jens Axboe6728cb02008-01-31 13:03:55 +01001895 bio = req->bio;
1896 if (bio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 /*
1898 * end more in this run, or just return 'not-done'
1899 */
1900 if (unlikely(nr_bytes <= 0))
1901 break;
1902 }
1903 }
1904
1905 /*
1906 * completely done
1907 */
Tejun Heo2e60e022009-04-23 11:05:18 +09001908 if (!req->bio) {
1909 /*
1910 * Reset counters so that the request stacking driver
1911 * can find how many bytes remain in the request
1912 * later.
1913 */
1914 req->nr_sectors = req->hard_nr_sectors = 0;
1915 req->current_nr_sectors = req->hard_cur_sectors = 0;
1916 return false;
1917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
1919 /*
1920 * if the request wasn't completed, update state
1921 */
1922 if (bio_nbytes) {
NeilBrown5bb23a62007-09-27 12:46:13 +02001923 req_bio_endio(req, bio, bio_nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 bio->bi_idx += next_idx;
1925 bio_iovec(bio)->bv_offset += nr_bytes;
1926 bio_iovec(bio)->bv_len -= nr_bytes;
1927 }
1928
1929 blk_recalc_rq_sectors(req, total_bytes >> 9);
1930 blk_recalc_rq_segments(req);
Tejun Heo2e60e022009-04-23 11:05:18 +09001931 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932}
Tejun Heo2e60e022009-04-23 11:05:18 +09001933EXPORT_SYMBOL_GPL(blk_update_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Tejun Heo2e60e022009-04-23 11:05:18 +09001935static bool blk_update_bidi_request(struct request *rq, int error,
1936 unsigned int nr_bytes,
1937 unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09001938{
Tejun Heo2e60e022009-04-23 11:05:18 +09001939 if (blk_update_request(rq, error, nr_bytes))
1940 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09001941
Tejun Heo2e60e022009-04-23 11:05:18 +09001942 /* Bidi request must be completed as a whole */
1943 if (unlikely(blk_bidi_rq(rq)) &&
1944 blk_update_request(rq->next_rq, error, bidi_bytes))
1945 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09001946
Tejun Heo2e60e022009-04-23 11:05:18 +09001947 add_disk_randomness(rq->rq_disk);
1948
1949 return false;
Tejun Heo5efccd12009-04-23 11:05:18 +09001950}
1951
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952/*
1953 * queue lock must be held
1954 */
Tejun Heo2e60e022009-04-23 11:05:18 +09001955static void blk_finish_request(struct request *req, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956{
Kiyoshi Uedab8286232007-12-11 17:53:24 -05001957 if (blk_rq_tagged(req))
1958 blk_queue_end_tag(req->q, req);
1959
1960 if (blk_queued_rq(req))
Tejun Heo53a08802008-12-03 12:41:26 +01001961 elv_dequeue_request(req->q, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
1963 if (unlikely(laptop_mode) && blk_fs_request(req))
1964 laptop_io_completion();
1965
Mike Andersone78042e2008-10-30 02:16:20 -07001966 blk_delete_timer(req);
1967
Jens Axboebc58ba92009-01-23 10:54:44 +01001968 blk_account_io_done(req);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05001969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 if (req->end_io)
Tejun Heo8ffdc652006-01-06 09:49:03 +01001971 req->end_io(req, error);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05001972 else {
1973 if (blk_bidi_rq(req))
1974 __blk_put_request(req->next_rq->q, req->next_rq);
1975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 __blk_put_request(req->q, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 }
1978}
1979
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05001980/**
Tejun Heo2e60e022009-04-23 11:05:18 +09001981 * blk_end_bidi_request - Complete a bidi request
1982 * @rq: the request to complete
Randy Dunlap710027a2008-08-19 20:13:11 +02001983 * @error: %0 for success, < %0 for error
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001984 * @nr_bytes: number of bytes to complete @rq
1985 * @bidi_bytes: number of bytes to complete @rq->next_rq
1986 *
1987 * Description:
1988 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
Tejun Heo2e60e022009-04-23 11:05:18 +09001989 * Drivers that supports bidi can safely call this member for any
1990 * type of request, bidi or uni. In the later case @bidi_bytes is
1991 * just ignored.
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001992 *
1993 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09001994 * %false - we are done with this request
1995 * %true - still buffers pending for this request
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001996 **/
Tejun Heo2e60e022009-04-23 11:05:18 +09001997bool blk_end_bidi_request(struct request *rq, int error,
1998 unsigned int nr_bytes, unsigned int bidi_bytes)
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001999{
Tejun Heo2e60e022009-04-23 11:05:18 +09002000 struct request_queue *q = rq->q;
2001 unsigned long flags;
2002
2003 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2004 return true;
2005
2006 spin_lock_irqsave(q->queue_lock, flags);
2007 blk_finish_request(rq, error);
2008 spin_unlock_irqrestore(q->queue_lock, flags);
2009
2010 return false;
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002011}
2012EXPORT_SYMBOL_GPL(blk_end_bidi_request);
2013
2014/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002015 * __blk_end_bidi_request - Complete a bidi request with queue lock held
2016 * @rq: the request to complete
2017 * @error: %0 for success, < %0 for error
2018 * @nr_bytes: number of bytes to complete @rq
2019 * @bidi_bytes: number of bytes to complete @rq->next_rq
Tejun Heo5efccd12009-04-23 11:05:18 +09002020 *
2021 * Description:
Tejun Heo2e60e022009-04-23 11:05:18 +09002022 * Identical to blk_end_bidi_request() except that queue lock is
2023 * assumed to be locked on entry and remains so on return.
Tejun Heo5efccd12009-04-23 11:05:18 +09002024 *
Tejun Heo2e60e022009-04-23 11:05:18 +09002025 * Return:
2026 * %false - we are done with this request
2027 * %true - still buffers pending for this request
Tejun Heo5efccd12009-04-23 11:05:18 +09002028 **/
Tejun Heo2e60e022009-04-23 11:05:18 +09002029bool __blk_end_bidi_request(struct request *rq, int error,
2030 unsigned int nr_bytes, unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002031{
Tejun Heo2e60e022009-04-23 11:05:18 +09002032 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2033 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002034
Tejun Heo2e60e022009-04-23 11:05:18 +09002035 blk_finish_request(rq, error);
Tejun Heo5efccd12009-04-23 11:05:18 +09002036
Tejun Heo2e60e022009-04-23 11:05:18 +09002037 return false;
Tejun Heo5efccd12009-04-23 11:05:18 +09002038}
Tejun Heo2e60e022009-04-23 11:05:18 +09002039EXPORT_SYMBOL_GPL(__blk_end_bidi_request);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002040
Jens Axboe86db1e22008-01-29 14:53:40 +01002041void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2042 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
David Woodhoused628eae2008-08-09 16:22:17 +01002044 /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw, and
2045 we want BIO_RW_AHEAD (bit 1) to imply REQ_FAILFAST (bit 1). */
Jens Axboe4aff5e22006-08-10 08:44:47 +02002046 rq->cmd_flags |= (bio->bi_rw & 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
David Woodhousefb2dce82008-08-05 18:01:53 +01002048 if (bio_has_data(bio)) {
2049 rq->nr_phys_segments = bio_phys_segments(q, bio);
David Woodhousefb2dce82008-08-05 18:01:53 +01002050 rq->buffer = bio_data(bio);
2051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 rq->current_nr_sectors = bio_cur_sectors(bio);
2053 rq->hard_cur_sectors = rq->current_nr_sectors;
2054 rq->hard_nr_sectors = rq->nr_sectors = bio_sectors(bio);
Mike Christie0e75f902006-12-01 10:40:55 +01002055 rq->data_len = bio->bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
2057 rq->bio = rq->biotail = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
NeilBrown66846572007-08-16 13:31:28 +02002059 if (bio->bi_bdev)
2060 rq->rq_disk = bio->bi_bdev->bd_disk;
2061}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Kiyoshi Uedaef9e3fa2008-10-01 16:12:15 +02002063/**
2064 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
2065 * @q : the queue of the device being checked
2066 *
2067 * Description:
2068 * Check if underlying low-level drivers of a device are busy.
2069 * If the drivers want to export their busy state, they must set own
2070 * exporting function using blk_queue_lld_busy() first.
2071 *
2072 * Basically, this function is used only by request stacking drivers
2073 * to stop dispatching requests to underlying devices when underlying
2074 * devices are busy. This behavior helps more I/O merging on the queue
2075 * of the request stacking driver and prevents I/O throughput regression
2076 * on burst I/O load.
2077 *
2078 * Return:
2079 * 0 - Not busy (The request stacking driver should dispatch request)
2080 * 1 - Busy (The request stacking driver should stop dispatching request)
2081 */
2082int blk_lld_busy(struct request_queue *q)
2083{
2084 if (q->lld_busy_fn)
2085 return q->lld_busy_fn(q);
2086
2087 return 0;
2088}
2089EXPORT_SYMBOL_GPL(blk_lld_busy);
2090
Jens Axboe18887ad2008-07-28 13:08:45 +02002091int kblockd_schedule_work(struct request_queue *q, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
2093 return queue_work(kblockd_workqueue, work);
2094}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095EXPORT_SYMBOL(kblockd_schedule_work);
2096
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097int __init blk_dev_init(void)
2098{
Nikanth Karthikesan9eb55b02009-04-27 14:53:54 +02002099 BUILD_BUG_ON(__REQ_NR_BITS > 8 *
2100 sizeof(((struct request *)0)->cmd_flags));
2101
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 kblockd_workqueue = create_workqueue("kblockd");
2103 if (!kblockd_workqueue)
2104 panic("Failed to create kblockd\n");
2105
2106 request_cachep = kmem_cache_create("blkdev_requests",
Paul Mundt20c2df82007-07-20 10:11:58 +09002107 sizeof(struct request), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Jens Axboe8324aa92008-01-29 14:51:59 +01002109 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02002110 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 return 0;
2113}
2114