blob: ce0ea97f76d3a7dda8175118d3fd27a93e94f37c [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 Axboebc58ba92009-01-23 10:54:44 +010067 struct gendisk *disk = rq->rq_disk;
Jens Axboe28f13702008-05-07 10:15:46 +020068 struct hd_struct *part;
Jens Axboe26b82562008-01-29 13:54:41 +010069 int rw = rq_data_dir(rq);
Tejun Heoc9959052008-08-25 19:47:21 +090070 int cpu;
Jens Axboe26b82562008-01-29 13:54:41 +010071
Jens Axboefb8ec182009-02-02 08:42:32 +010072 if (!blk_fs_request(rq) || !disk || !blk_do_io_stat(disk->queue))
Jens Axboe26b82562008-01-29 13:54:41 +010073 return;
74
Tejun Heo074a7ac2008-08-25 19:56:14 +090075 cpu = part_stat_lock();
Tejun Heoe71bf0d2008-09-03 09:03:02 +020076 part = disk_map_sector_rcu(rq->rq_disk, rq->sector);
Tejun Heoc9959052008-08-25 19:47:21 +090077
Jens Axboe28f13702008-05-07 10:15:46 +020078 if (!new_io)
Tejun Heo074a7ac2008-08-25 19:56:14 +090079 part_stat_inc(cpu, part, merges[rw]);
Jens Axboe28f13702008-05-07 10:15:46 +020080 else {
Tejun Heo074a7ac2008-08-25 19:56:14 +090081 part_round_stats(cpu, part);
82 part_inc_in_flight(part);
Jens Axboe26b82562008-01-29 13:54:41 +010083 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +020084
Tejun Heo074a7ac2008-08-25 19:56:14 +090085 part_stat_unlock();
Jens Axboe26b82562008-01-29 13:54:41 +010086}
87
Jens Axboe8324aa92008-01-29 14:51:59 +010088void blk_queue_congestion_threshold(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
90 int nr;
91
92 nr = q->nr_requests - (q->nr_requests / 8) + 1;
93 if (nr > q->nr_requests)
94 nr = q->nr_requests;
95 q->nr_congestion_on = nr;
96
97 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
98 if (nr < 1)
99 nr = 1;
100 q->nr_congestion_off = nr;
101}
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103/**
104 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
105 * @bdev: device
106 *
107 * Locates the passed device's request queue and returns the address of its
108 * backing_dev_info
109 *
110 * Will return NULL if the request queue cannot be located.
111 */
112struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
113{
114 struct backing_dev_info *ret = NULL;
Jens Axboe165125e2007-07-24 09:28:11 +0200115 struct request_queue *q = bdev_get_queue(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 if (q)
118 ret = &q->backing_dev_info;
119 return ret;
120}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121EXPORT_SYMBOL(blk_get_backing_dev_info);
122
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200123void blk_rq_init(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200125 memset(rq, 0, sizeof(*rq));
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 INIT_LIST_HEAD(&rq->queuelist);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700128 INIT_LIST_HEAD(&rq->timeout_list);
Jens Axboec7c22e42008-09-13 20:26:01 +0200129 rq->cpu = -1;
Jens Axboe63a71382008-02-08 12:41:03 +0100130 rq->q = q;
131 rq->sector = rq->hard_sector = (sector_t) -1;
Jens Axboe2e662b62006-07-13 11:55:04 +0200132 INIT_HLIST_NODE(&rq->hash);
133 RB_CLEAR_NODE(&rq->rb_node);
FUJITA Tomonorid7e3c322008-04-29 09:54:39 +0200134 rq->cmd = rq->__cmd;
Li Zefane2494e12009-04-02 13:43:26 +0800135 rq->cmd_len = BLK_MAX_CDB;
Jens Axboe63a71382008-02-08 12:41:03 +0100136 rq->tag = -1;
Jens Axboe63a71382008-02-08 12:41:03 +0100137 rq->ref_count = 1;
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
Jens Axboe6728cb02008-01-31 13:03:55 +0100188 printk(KERN_INFO " sector %llu, nr/cnr %lu/%u\n",
189 (unsigned long long)rq->sector,
190 rq->nr_sectors,
191 rq->current_nr_sectors);
192 printk(KERN_INFO " bio %p, biotail %p, buffer %p, data %p, len %u\n",
193 rq->bio, rq->biotail,
194 rq->buffer, rq->data,
195 rq->data_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Jens Axboe4aff5e22006-08-10 08:44:47 +0200197 if (blk_pc_request(rq)) {
Jens Axboe6728cb02008-01-31 13:03:55 +0100198 printk(KERN_INFO " cdb: ");
FUJITA Tomonorid34c87e2008-04-29 14:37:52 +0200199 for (bit = 0; bit < BLK_MAX_CDB; bit++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 printk("%02x ", rq->cmd[bit]);
201 printk("\n");
202 }
203}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204EXPORT_SYMBOL(blk_dump_rq_flags);
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206/*
207 * "plug" the device if there are no outstanding requests: this will
208 * force the transfer to start only after we have put all the requests
209 * on the list.
210 *
211 * This is called with interrupts off and no requests on the queue and
212 * with the queue lock held.
213 */
Jens Axboe165125e2007-07-24 09:28:11 +0200214void blk_plug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 WARN_ON(!irqs_disabled());
217
218 /*
219 * don't plug a stopped queue, it must be paired with blk_start_queue()
220 * which will restart the queueing
221 */
Coywolf Qi Hunt7daac492006-04-19 10:14:49 +0200222 if (blk_queue_stopped(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return;
224
Jens Axboee48ec692008-07-03 13:18:54 +0200225 if (!queue_flag_test_and_set(QUEUE_FLAG_PLUGGED, q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100227 trace_block_plug(q);
Jens Axboe2056a782006-03-23 20:00:26 +0100228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230EXPORT_SYMBOL(blk_plug_device);
231
Jens Axboe6c5e0c42008-08-01 20:31:32 +0200232/**
233 * blk_plug_device_unlocked - plug a device without queue lock held
234 * @q: The &struct request_queue to plug
235 *
236 * Description:
237 * Like @blk_plug_device(), but grabs the queue lock and disables
238 * interrupts.
239 **/
240void blk_plug_device_unlocked(struct request_queue *q)
241{
242 unsigned long flags;
243
244 spin_lock_irqsave(q->queue_lock, flags);
245 blk_plug_device(q);
246 spin_unlock_irqrestore(q->queue_lock, flags);
247}
248EXPORT_SYMBOL(blk_plug_device_unlocked);
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250/*
251 * remove the queue from the plugged list, if present. called with
252 * queue lock held and interrupts disabled.
253 */
Jens Axboe165125e2007-07-24 09:28:11 +0200254int blk_remove_plug(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 WARN_ON(!irqs_disabled());
257
Jens Axboee48ec692008-07-03 13:18:54 +0200258 if (!queue_flag_test_and_clear(QUEUE_FLAG_PLUGGED, q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return 0;
260
261 del_timer(&q->unplug_timer);
262 return 1;
263}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264EXPORT_SYMBOL(blk_remove_plug);
265
266/*
267 * remove the plug and let it rip..
268 */
Jens Axboe165125e2007-07-24 09:28:11 +0200269void __generic_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Coywolf Qi Hunt7daac492006-04-19 10:14:49 +0200271 if (unlikely(blk_queue_stopped(q)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return;
Jens Axboea31a9732008-10-17 13:58:29 +0200273 if (!blk_remove_plug(q) && !blk_queue_nonrot(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return;
275
Jens Axboe22e2c502005-06-27 10:55:12 +0200276 q->request_fn(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279/**
280 * generic_unplug_device - fire a request queue
Jens Axboe165125e2007-07-24 09:28:11 +0200281 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 *
283 * Description:
284 * Linux uses plugging to build bigger requests queues before letting
285 * the device have at them. If a queue is plugged, the I/O scheduler
286 * is still adding and merging requests on the queue. Once the queue
287 * gets unplugged, the request_fn defined for the queue is invoked and
288 * transfers started.
289 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200290void generic_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Jens Axboedbaf2c02008-05-07 09:48:17 +0200292 if (blk_queue_plugged(q)) {
293 spin_lock_irq(q->queue_lock);
294 __generic_unplug_device(q);
295 spin_unlock_irq(q->queue_lock);
296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298EXPORT_SYMBOL(generic_unplug_device);
299
300static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
301 struct page *page)
302{
Jens Axboe165125e2007-07-24 09:28:11 +0200303 struct request_queue *q = bdi->unplug_io_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500305 blk_unplug(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
Jens Axboe86db1e22008-01-29 14:53:40 +0100308void blk_unplug_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Jens Axboe165125e2007-07-24 09:28:11 +0200310 struct request_queue *q =
311 container_of(work, struct request_queue, unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100313 trace_block_unplug_io(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 q->unplug_fn(q);
315}
316
Jens Axboe86db1e22008-01-29 14:53:40 +0100317void blk_unplug_timeout(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Jens Axboe165125e2007-07-24 09:28:11 +0200319 struct request_queue *q = (struct request_queue *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100321 trace_block_unplug_timer(q);
Jens Axboe18887ad2008-07-28 13:08:45 +0200322 kblockd_schedule_work(q, &q->unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500325void blk_unplug(struct request_queue *q)
326{
327 /*
328 * devices don't necessarily have an ->unplug_fn defined
329 */
330 if (q->unplug_fn) {
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100331 trace_block_unplug_io(q);
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500332 q->unplug_fn(q);
333 }
334}
335EXPORT_SYMBOL(blk_unplug);
336
Jens Axboec7c22e42008-09-13 20:26:01 +0200337static void blk_invoke_request_fn(struct request_queue *q)
338{
Jens Axboe80a4b582008-10-14 09:51:06 +0200339 if (unlikely(blk_queue_stopped(q)))
340 return;
341
Jens Axboec7c22e42008-09-13 20:26:01 +0200342 /*
343 * one level of recursion is ok and is much faster than kicking
344 * the unplug handling
345 */
346 if (!queue_flag_test_and_set(QUEUE_FLAG_REENTER, q)) {
347 q->request_fn(q);
348 queue_flag_clear(QUEUE_FLAG_REENTER, q);
349 } else {
350 queue_flag_set(QUEUE_FLAG_PLUGGED, q);
351 kblockd_schedule_work(q, &q->unplug_work);
352 }
353}
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355/**
356 * blk_start_queue - restart a previously stopped queue
Jens Axboe165125e2007-07-24 09:28:11 +0200357 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 *
359 * Description:
360 * blk_start_queue() will clear the stop flag on the queue, and call
361 * the request_fn for the queue if it was in a stopped state when
362 * entered. Also see blk_stop_queue(). Queue lock must be held.
363 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200364void blk_start_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200366 WARN_ON(!irqs_disabled());
367
Nick Piggin75ad23b2008-04-29 14:48:33 +0200368 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
Jens Axboec7c22e42008-09-13 20:26:01 +0200369 blk_invoke_request_fn(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371EXPORT_SYMBOL(blk_start_queue);
372
373/**
374 * blk_stop_queue - stop a queue
Jens Axboe165125e2007-07-24 09:28:11 +0200375 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 *
377 * Description:
378 * The Linux block layer assumes that a block driver will consume all
379 * entries on the request queue when the request_fn strategy is called.
380 * Often this will not happen, because of hardware limitations (queue
381 * depth settings). If a device driver gets a 'queue full' response,
382 * or if it simply chooses not to queue more I/O at one point, it can
383 * call this function to prevent the request_fn from being called until
384 * the driver has signalled it's ready to go again. This happens by calling
385 * blk_start_queue() to restart queue operations. Queue lock must be held.
386 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200387void blk_stop_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
389 blk_remove_plug(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200390 queue_flag_set(QUEUE_FLAG_STOPPED, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392EXPORT_SYMBOL(blk_stop_queue);
393
394/**
395 * blk_sync_queue - cancel any pending callbacks on a queue
396 * @q: the queue
397 *
398 * Description:
399 * The block layer may perform asynchronous callback activity
400 * on a queue, such as calling the unplug function after a timeout.
401 * A block device may call blk_sync_queue to ensure that any
402 * such activity is cancelled, thus allowing it to release resources
Michael Opdenacker59c51592007-05-09 08:57:56 +0200403 * that the callbacks might use. The caller must already have made sure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 * that its ->make_request_fn will not re-add plugging prior to calling
405 * this function.
406 *
407 */
408void blk_sync_queue(struct request_queue *q)
409{
410 del_timer_sync(&q->unplug_timer);
Jens Axboe70ed28b2008-11-19 14:38:39 +0100411 del_timer_sync(&q->timeout);
Cheng Renquan64d01dc2008-12-03 12:41:39 +0100412 cancel_work_sync(&q->unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414EXPORT_SYMBOL(blk_sync_queue);
415
416/**
Jens Axboe80a4b582008-10-14 09:51:06 +0200417 * __blk_run_queue - run a single device queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200419 *
420 * Description:
421 * See @blk_run_queue. This variant must be called with the queue lock
422 * held and interrupts disabled.
423 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 */
Nick Piggin75ad23b2008-04-29 14:48:33 +0200425void __blk_run_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 blk_remove_plug(q);
Jens Axboedac07ec2006-05-11 08:20:16 +0200428
429 /*
430 * Only recurse once to avoid overrunning the stack, let the unplug
431 * handling reinvoke the handler shortly if we already got there.
432 */
Jens Axboec7c22e42008-09-13 20:26:01 +0200433 if (!elv_queue_empty(q))
434 blk_invoke_request_fn(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200435}
436EXPORT_SYMBOL(__blk_run_queue);
Jens Axboedac07ec2006-05-11 08:20:16 +0200437
Nick Piggin75ad23b2008-04-29 14:48:33 +0200438/**
439 * blk_run_queue - run a single device queue
440 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200441 *
442 * Description:
443 * Invoke request handling on this queue, if it has pending work to do.
444 * May be used to restart queueing when a request has completed. Also
445 * See @blk_start_queueing.
446 *
Nick Piggin75ad23b2008-04-29 14:48:33 +0200447 */
448void blk_run_queue(struct request_queue *q)
449{
450 unsigned long flags;
451
452 spin_lock_irqsave(q->queue_lock, flags);
453 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 spin_unlock_irqrestore(q->queue_lock, flags);
455}
456EXPORT_SYMBOL(blk_run_queue);
457
Jens Axboe165125e2007-07-24 09:28:11 +0200458void blk_put_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500459{
460 kobject_put(&q->kobj);
461}
Al Viro483f4af2006-03-18 18:34:37 -0500462
Jens Axboe6728cb02008-01-31 13:03:55 +0100463void blk_cleanup_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500464{
Jens Axboee3335de92008-09-18 09:22:54 -0700465 /*
466 * We know we have process context here, so we can be a little
467 * cautious and ensure that pending block actions on this device
468 * are done before moving on. Going into this function, we should
469 * not have processes doing IO to this device.
470 */
471 blk_sync_queue(q);
472
Al Viro483f4af2006-03-18 18:34:37 -0500473 mutex_lock(&q->sysfs_lock);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200474 queue_flag_set_unlocked(QUEUE_FLAG_DEAD, q);
Al Viro483f4af2006-03-18 18:34:37 -0500475 mutex_unlock(&q->sysfs_lock);
476
477 if (q->elevator)
478 elevator_exit(q->elevator);
479
480 blk_put_queue(q);
481}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482EXPORT_SYMBOL(blk_cleanup_queue);
483
Jens Axboe165125e2007-07-24 09:28:11 +0200484static int blk_init_free_list(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 struct request_list *rl = &q->rq;
487
Jens Axboe1faa16d2009-04-06 14:48:01 +0200488 rl->count[BLK_RW_SYNC] = rl->count[BLK_RW_ASYNC] = 0;
489 rl->starved[BLK_RW_SYNC] = rl->starved[BLK_RW_ASYNC] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200490 rl->elvpriv = 0;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200491 init_waitqueue_head(&rl->wait[BLK_RW_SYNC]);
492 init_waitqueue_head(&rl->wait[BLK_RW_ASYNC]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Christoph Lameter19460892005-06-23 00:08:19 -0700494 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
495 mempool_free_slab, request_cachep, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 if (!rl->rq_pool)
498 return -ENOMEM;
499
500 return 0;
501}
502
Jens Axboe165125e2007-07-24 09:28:11 +0200503struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Christoph Lameter19460892005-06-23 00:08:19 -0700505 return blk_alloc_queue_node(gfp_mask, -1);
506}
507EXPORT_SYMBOL(blk_alloc_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Jens Axboe165125e2007-07-24 09:28:11 +0200509struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -0700510{
Jens Axboe165125e2007-07-24 09:28:11 +0200511 struct request_queue *q;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700512 int err;
Christoph Lameter19460892005-06-23 00:08:19 -0700513
Jens Axboe8324aa92008-01-29 14:51:59 +0100514 q = kmem_cache_alloc_node(blk_requestq_cachep,
Christoph Lameter94f60302007-07-17 04:03:29 -0700515 gfp_mask | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (!q)
517 return NULL;
518
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700519 q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
520 q->backing_dev_info.unplug_io_data = q;
521 err = bdi_init(&q->backing_dev_info);
522 if (err) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100523 kmem_cache_free(blk_requestq_cachep, q);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700524 return NULL;
525 }
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 init_timer(&q->unplug_timer);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700528 setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
529 INIT_LIST_HEAD(&q->timeout_list);
Peter Zijlstra713ada92008-10-16 13:44:57 +0200530 INIT_WORK(&q->unplug_work, blk_unplug_work);
Al Viro483f4af2006-03-18 18:34:37 -0500531
Jens Axboe8324aa92008-01-29 14:51:59 +0100532 kobject_init(&q->kobj, &blk_queue_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Al Viro483f4af2006-03-18 18:34:37 -0500534 mutex_init(&q->sysfs_lock);
Neil Browne7e72bf2008-05-14 16:05:54 -0700535 spin_lock_init(&q->__queue_lock);
Al Viro483f4af2006-03-18 18:34:37 -0500536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return q;
538}
Christoph Lameter19460892005-06-23 00:08:19 -0700539EXPORT_SYMBOL(blk_alloc_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541/**
542 * blk_init_queue - prepare a request queue for use with a block device
543 * @rfn: The function to be called to process requests that have been
544 * placed on the queue.
545 * @lock: Request queue spin lock
546 *
547 * Description:
548 * If a block device wishes to use the standard request handling procedures,
549 * which sorts requests and coalesces adjacent requests, then it must
550 * call blk_init_queue(). The function @rfn will be called when there
551 * are requests on the queue that need to be processed. If the device
552 * supports plugging, then @rfn may not be called immediately when requests
553 * are available on the queue, but may be called at some time later instead.
554 * Plugged queues are generally unplugged when a buffer belonging to one
555 * of the requests on the queue is needed, or due to memory pressure.
556 *
557 * @rfn is not required, or even expected, to remove all requests off the
558 * queue, but only as many as it can handle at a time. If it does leave
559 * requests on the queue, it is responsible for arranging that the requests
560 * get dealt with eventually.
561 *
562 * The queue spin lock must be held while manipulating the requests on the
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200563 * request queue; this lock will be taken also from interrupt context, so irq
564 * disabling is needed for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 *
Randy Dunlap710027a2008-08-19 20:13:11 +0200566 * Function returns a pointer to the initialized request queue, or %NULL if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 * it didn't succeed.
568 *
569 * Note:
570 * blk_init_queue() must be paired with a blk_cleanup_queue() call
571 * when the block device is deactivated (such as at module unload).
572 **/
Christoph Lameter19460892005-06-23 00:08:19 -0700573
Jens Axboe165125e2007-07-24 09:28:11 +0200574struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Christoph Lameter19460892005-06-23 00:08:19 -0700576 return blk_init_queue_node(rfn, lock, -1);
577}
578EXPORT_SYMBOL(blk_init_queue);
579
Jens Axboe165125e2007-07-24 09:28:11 +0200580struct request_queue *
Christoph Lameter19460892005-06-23 00:08:19 -0700581blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
582{
Jens Axboe165125e2007-07-24 09:28:11 +0200583 struct request_queue *q = blk_alloc_queue_node(GFP_KERNEL, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 if (!q)
586 return NULL;
587
Christoph Lameter19460892005-06-23 00:08:19 -0700588 q->node = node_id;
Al Viro8669aaf2006-03-18 13:50:00 -0500589 if (blk_init_free_list(q)) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100590 kmem_cache_free(blk_requestq_cachep, q);
Al Viro8669aaf2006-03-18 13:50:00 -0500591 return NULL;
592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
152587d2005-04-12 16:22:06 -0500594 /*
595 * if caller didn't supply a lock, they get per-queue locking with
596 * our embedded lock
597 */
Neil Browne7e72bf2008-05-14 16:05:54 -0700598 if (!lock)
152587d2005-04-12 16:22:06 -0500599 lock = &q->__queue_lock;
152587d2005-04-12 16:22:06 -0500600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 q->request_fn = rfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 q->prep_rq_fn = NULL;
603 q->unplug_fn = generic_unplug_device;
Jens Axboebc58ba92009-01-23 10:54:44 +0100604 q->queue_flags = QUEUE_FLAG_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 q->queue_lock = lock;
606
Jens Axboef3b144a2009-03-06 08:48:33 +0100607 /*
608 * This also sets hw/phys segments, boundary and size
609 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 blk_queue_make_request(q, __make_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Alan Stern44ec9542007-02-20 11:01:57 -0500612 q->sg_reserved_size = INT_MAX;
613
FUJITA Tomonoriabf54392008-08-16 14:10:05 +0900614 blk_set_cmd_filter_defaults(&q->cmd_filter);
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 /*
617 * all done
618 */
619 if (!elevator_init(q, NULL)) {
620 blk_queue_congestion_threshold(q);
621 return q;
622 }
623
Al Viro8669aaf2006-03-18 13:50:00 -0500624 blk_put_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return NULL;
626}
Christoph Lameter19460892005-06-23 00:08:19 -0700627EXPORT_SYMBOL(blk_init_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Jens Axboe165125e2007-07-24 09:28:11 +0200629int blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Nick Pigginfde6ad22005-06-23 00:08:53 -0700631 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
Al Viro483f4af2006-03-18 18:34:37 -0500632 kobject_get(&q->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 return 0;
634 }
635
636 return 1;
637}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Jens Axboe165125e2007-07-24 09:28:11 +0200639static inline void blk_free_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Jens Axboe4aff5e22006-08-10 08:44:47 +0200641 if (rq->cmd_flags & REQ_ELVPRIV)
Tejun Heocb98fc82005-10-28 08:29:39 +0200642 elv_put_request(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 mempool_free(rq, q->rq.rq_pool);
644}
645
Jens Axboe1ea25ecb2006-07-18 22:24:11 +0200646static struct request *
Jens Axboe165125e2007-07-24 09:28:11 +0200647blk_alloc_request(struct request_queue *q, int rw, int priv, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
650
651 if (!rq)
652 return NULL;
653
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200654 blk_rq_init(q, rq);
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200655
Jens Axboe49171e52006-08-10 08:59:11 +0200656 rq->cmd_flags = rw | REQ_ALLOCED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Tejun Heocb98fc82005-10-28 08:29:39 +0200658 if (priv) {
Jens Axboecb78b282006-07-28 09:32:57 +0200659 if (unlikely(elv_set_request(q, rq, gfp_mask))) {
Tejun Heocb98fc82005-10-28 08:29:39 +0200660 mempool_free(rq, q->rq.rq_pool);
661 return NULL;
662 }
Jens Axboe4aff5e22006-08-10 08:44:47 +0200663 rq->cmd_flags |= REQ_ELVPRIV;
Tejun Heocb98fc82005-10-28 08:29:39 +0200664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Tejun Heocb98fc82005-10-28 08:29:39 +0200666 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
669/*
670 * ioc_batching returns true if the ioc is a valid batching request and
671 * should be given priority access to a request.
672 */
Jens Axboe165125e2007-07-24 09:28:11 +0200673static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
675 if (!ioc)
676 return 0;
677
678 /*
679 * Make sure the process is able to allocate at least 1 request
680 * even if the batch times out, otherwise we could theoretically
681 * lose wakeups.
682 */
683 return ioc->nr_batch_requests == q->nr_batching ||
684 (ioc->nr_batch_requests > 0
685 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
686}
687
688/*
689 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
690 * will cause the process to be a "batcher" on all queues in the system. This
691 * is the behaviour we want though - once it gets a wakeup it should be given
692 * a nice run.
693 */
Jens Axboe165125e2007-07-24 09:28:11 +0200694static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
696 if (!ioc || ioc_batching(q, ioc))
697 return;
698
699 ioc->nr_batch_requests = q->nr_batching;
700 ioc->last_waited = jiffies;
701}
702
Jens Axboe1faa16d2009-04-06 14:48:01 +0200703static void __freed_request(struct request_queue *q, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 struct request_list *rl = &q->rq;
706
Jens Axboe1faa16d2009-04-06 14:48:01 +0200707 if (rl->count[sync] < queue_congestion_off_threshold(q))
708 blk_clear_queue_congested(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Jens Axboe1faa16d2009-04-06 14:48:01 +0200710 if (rl->count[sync] + 1 <= q->nr_requests) {
711 if (waitqueue_active(&rl->wait[sync]))
712 wake_up(&rl->wait[sync]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Jens Axboe1faa16d2009-04-06 14:48:01 +0200714 blk_clear_queue_full(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
716}
717
718/*
719 * A request has just been released. Account for it, update the full and
720 * congestion status, wake up any waiters. Called under q->queue_lock.
721 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200722static void freed_request(struct request_queue *q, int sync, int priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
724 struct request_list *rl = &q->rq;
725
Jens Axboe1faa16d2009-04-06 14:48:01 +0200726 rl->count[sync]--;
Tejun Heocb98fc82005-10-28 08:29:39 +0200727 if (priv)
728 rl->elvpriv--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Jens Axboe1faa16d2009-04-06 14:48:01 +0200730 __freed_request(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Jens Axboe1faa16d2009-04-06 14:48:01 +0200732 if (unlikely(rl->starved[sync ^ 1]))
733 __freed_request(q, sync ^ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736/*
Nick Piggind6344532005-06-28 20:45:14 -0700737 * Get a free request, queue_lock must be held.
738 * Returns NULL on failure, with queue_lock held.
739 * Returns !NULL on success, with queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 */
Jens Axboe165125e2007-07-24 09:28:11 +0200741static struct request *get_request(struct request_queue *q, int rw_flags,
Jens Axboe7749a8d2006-12-13 13:02:26 +0100742 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
744 struct request *rq = NULL;
745 struct request_list *rl = &q->rq;
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100746 struct io_context *ioc = NULL;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200747 const bool is_sync = rw_is_sync(rw_flags) != 0;
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100748 int may_queue, priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Jens Axboe7749a8d2006-12-13 13:02:26 +0100750 may_queue = elv_may_queue(q, rw_flags);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100751 if (may_queue == ELV_MQUEUE_NO)
752 goto rq_starved;
753
Jens Axboe1faa16d2009-04-06 14:48:01 +0200754 if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) {
755 if (rl->count[is_sync]+1 >= q->nr_requests) {
Jens Axboeb5deef92006-07-19 23:39:40 +0200756 ioc = current_io_context(GFP_ATOMIC, q->node);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100757 /*
758 * The queue will fill after this allocation, so set
759 * it as full, and mark this process as "batching".
760 * This process will be allowed to complete a batch of
761 * requests, others will be blocked.
762 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200763 if (!blk_queue_full(q, is_sync)) {
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100764 ioc_set_batching(q, ioc);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200765 blk_set_queue_full(q, is_sync);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100766 } else {
767 if (may_queue != ELV_MQUEUE_MUST
768 && !ioc_batching(q, ioc)) {
769 /*
770 * The queue is full and the allocating
771 * process is not a "batcher", and not
772 * exempted by the IO scheduler
773 */
774 goto out;
775 }
776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
Jens Axboe1faa16d2009-04-06 14:48:01 +0200778 blk_set_queue_congested(q, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
780
Jens Axboe082cf692005-06-28 16:35:11 +0200781 /*
782 * Only allow batching queuers to allocate up to 50% over the defined
783 * limit of requests, otherwise we could have thousands of requests
784 * allocated with any setting of ->nr_requests
785 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200786 if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
Jens Axboe082cf692005-06-28 16:35:11 +0200787 goto out;
Hugh Dickinsfd782a42005-06-29 15:15:40 +0100788
Jens Axboe1faa16d2009-04-06 14:48:01 +0200789 rl->count[is_sync]++;
790 rl->starved[is_sync] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200791
Jens Axboe64521d12005-10-28 08:30:39 +0200792 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Tejun Heocb98fc82005-10-28 08:29:39 +0200793 if (priv)
794 rl->elvpriv++;
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 spin_unlock_irq(q->queue_lock);
797
Jens Axboe7749a8d2006-12-13 13:02:26 +0100798 rq = blk_alloc_request(q, rw_flags, priv, gfp_mask);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100799 if (unlikely(!rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 /*
801 * Allocation failed presumably due to memory. Undo anything
802 * we might have messed up.
803 *
804 * Allocating task should really be put onto the front of the
805 * wait queue, but this is pretty rare.
806 */
807 spin_lock_irq(q->queue_lock);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200808 freed_request(q, is_sync, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 /*
811 * in the very unlikely event that allocation failed and no
812 * requests for this direction was pending, mark us starved
813 * so that freeing of a request in the other direction will
814 * notice us. another possible fix would be to split the
815 * rq mempool into READ and WRITE
816 */
817rq_starved:
Jens Axboe1faa16d2009-04-06 14:48:01 +0200818 if (unlikely(rl->count[is_sync] == 0))
819 rl->starved[is_sync] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 goto out;
822 }
823
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100824 /*
825 * ioc may be NULL here, and ioc_batching will be false. That's
826 * OK, if the queue is under the request limit then requests need
827 * not count toward the nr_batch_requests limit. There will always
828 * be some limit enforced by BLK_BATCH_TIME.
829 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (ioc_batching(q, ioc))
831 ioc->nr_batch_requests--;
Jens Axboe6728cb02008-01-31 13:03:55 +0100832
Jens Axboe1faa16d2009-04-06 14:48:01 +0200833 trace_block_getrq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return rq;
836}
837
838/*
839 * No available requests for this queue, unplug the device and wait for some
840 * requests to become available.
Nick Piggind6344532005-06-28 20:45:14 -0700841 *
842 * Called with q->queue_lock held, and returns with it unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 */
Jens Axboe165125e2007-07-24 09:28:11 +0200844static struct request *get_request_wait(struct request_queue *q, int rw_flags,
Jens Axboe22e2c502005-06-27 10:55:12 +0200845 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Jens Axboe1faa16d2009-04-06 14:48:01 +0200847 const bool is_sync = rw_is_sync(rw_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 struct request *rq;
849
Jens Axboe7749a8d2006-12-13 13:02:26 +0100850 rq = get_request(q, rw_flags, bio, GFP_NOIO);
Nick Piggin450991b2005-06-28 20:45:13 -0700851 while (!rq) {
852 DEFINE_WAIT(wait);
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200853 struct io_context *ioc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 struct request_list *rl = &q->rq;
855
Jens Axboe1faa16d2009-04-06 14:48:01 +0200856 prepare_to_wait_exclusive(&rl->wait[is_sync], &wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 TASK_UNINTERRUPTIBLE);
858
Jens Axboe1faa16d2009-04-06 14:48:01 +0200859 trace_block_sleeprq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200861 __generic_unplug_device(q);
862 spin_unlock_irq(q->queue_lock);
863 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200865 /*
866 * After sleeping, we become a "batching" process and
867 * will be able to allocate at least one request, and
868 * up to a big batch of them for a small period time.
869 * See ioc_batching, ioc_set_batching
870 */
871 ioc = current_io_context(GFP_NOIO, q->node);
872 ioc_set_batching(q, ioc);
Jens Axboe2056a782006-03-23 20:00:26 +0100873
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200874 spin_lock_irq(q->queue_lock);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200875 finish_wait(&rl->wait[is_sync], &wait);
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200876
877 rq = get_request(q, rw_flags, bio, GFP_NOIO);
878 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 return rq;
881}
882
Jens Axboe165125e2007-07-24 09:28:11 +0200883struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
885 struct request *rq;
886
887 BUG_ON(rw != READ && rw != WRITE);
888
Nick Piggind6344532005-06-28 20:45:14 -0700889 spin_lock_irq(q->queue_lock);
890 if (gfp_mask & __GFP_WAIT) {
Jens Axboe22e2c502005-06-27 10:55:12 +0200891 rq = get_request_wait(q, rw, NULL);
Nick Piggind6344532005-06-28 20:45:14 -0700892 } else {
Jens Axboe22e2c502005-06-27 10:55:12 +0200893 rq = get_request(q, rw, NULL, gfp_mask);
Nick Piggind6344532005-06-28 20:45:14 -0700894 if (!rq)
895 spin_unlock_irq(q->queue_lock);
896 }
897 /* q->queue_lock is unlocked at this point */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 return rq;
900}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901EXPORT_SYMBOL(blk_get_request);
902
903/**
Jens Axboedc72ef42006-07-20 14:54:05 +0200904 * blk_start_queueing - initiate dispatch of requests to device
905 * @q: request queue to kick into gear
906 *
907 * This is basically a helper to remove the need to know whether a queue
908 * is plugged or not if someone just wants to initiate dispatch of requests
Jens Axboe80a4b582008-10-14 09:51:06 +0200909 * for this queue. Should be used to start queueing on a device outside
910 * of ->request_fn() context. Also see @blk_run_queue.
Jens Axboedc72ef42006-07-20 14:54:05 +0200911 *
912 * The queue lock must be held with interrupts disabled.
913 */
Jens Axboe165125e2007-07-24 09:28:11 +0200914void blk_start_queueing(struct request_queue *q)
Jens Axboedc72ef42006-07-20 14:54:05 +0200915{
Elias Oltmanns336c3d82008-10-01 16:02:33 +0200916 if (!blk_queue_plugged(q)) {
917 if (unlikely(blk_queue_stopped(q)))
918 return;
Jens Axboedc72ef42006-07-20 14:54:05 +0200919 q->request_fn(q);
Elias Oltmanns336c3d82008-10-01 16:02:33 +0200920 } else
Jens Axboedc72ef42006-07-20 14:54:05 +0200921 __generic_unplug_device(q);
922}
923EXPORT_SYMBOL(blk_start_queueing);
924
925/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 * blk_requeue_request - put a request back on queue
927 * @q: request queue where request should be inserted
928 * @rq: request to be inserted
929 *
930 * Description:
931 * Drivers often keep queueing requests until the hardware cannot accept
932 * more, when that condition happens we need to put the request back
933 * on the queue. Must be called with queue lock held.
934 */
Jens Axboe165125e2007-07-24 09:28:11 +0200935void blk_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
Jens Axboe242f9dc2008-09-14 05:55:09 -0700937 blk_delete_timer(rq);
938 blk_clear_rq_complete(rq);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100939 trace_block_rq_requeue(q, rq);
Jens Axboe2056a782006-03-23 20:00:26 +0100940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (blk_rq_tagged(rq))
942 blk_queue_end_tag(q, rq);
943
944 elv_requeue_request(q, rq);
945}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946EXPORT_SYMBOL(blk_requeue_request);
947
948/**
Randy Dunlap710027a2008-08-19 20:13:11 +0200949 * blk_insert_request - insert a special request into a request queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 * @q: request queue where request should be inserted
951 * @rq: request to be inserted
952 * @at_head: insert request at head or tail of queue
953 * @data: private data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 *
955 * Description:
956 * Many block devices need to execute commands asynchronously, so they don't
957 * block the whole kernel from preemption during request execution. This is
958 * accomplished normally by inserting aritficial requests tagged as
Randy Dunlap710027a2008-08-19 20:13:11 +0200959 * REQ_TYPE_SPECIAL in to the corresponding request queue, and letting them
960 * be scheduled for actual execution by the request queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 *
962 * We have the option of inserting the head or the tail of the queue.
963 * Typically we use the tail for new ioctls and so forth. We use the head
964 * of the queue for things like a QUEUE_FULL message from a device, or a
965 * host that is unable to accept a particular command.
966 */
Jens Axboe165125e2007-07-24 09:28:11 +0200967void blk_insert_request(struct request_queue *q, struct request *rq,
Tejun Heo 867d1192005-04-24 02:06:05 -0500968 int at_head, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
Tejun Heo 867d1192005-04-24 02:06:05 -0500970 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 unsigned long flags;
972
973 /*
974 * tell I/O scheduler that this isn't a regular read/write (ie it
975 * must not attempt merges on this) and that it acts as a soft
976 * barrier
977 */
Jens Axboe4aff5e22006-08-10 08:44:47 +0200978 rq->cmd_type = REQ_TYPE_SPECIAL;
979 rq->cmd_flags |= REQ_SOFTBARRIER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 rq->special = data;
982
983 spin_lock_irqsave(q->queue_lock, flags);
984
985 /*
986 * If command is tagged, release the tag
987 */
Tejun Heo 867d1192005-04-24 02:06:05 -0500988 if (blk_rq_tagged(rq))
989 blk_queue_end_tag(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Jerome Marchandb238b3d2007-10-23 15:05:46 +0200991 drive_stat_acct(rq, 1);
Tejun Heo 867d1192005-04-24 02:06:05 -0500992 __elv_add_request(q, rq, where, 0);
Jens Axboedc72ef42006-07-20 14:54:05 +0200993 blk_start_queueing(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 spin_unlock_irqrestore(q->queue_lock, flags);
995}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996EXPORT_SYMBOL(blk_insert_request);
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998/*
999 * add-request adds a request to the linked list.
1000 * queue lock is held and interrupts disabled, as we muck with the
1001 * request queue list.
1002 */
Jens Axboe6728cb02008-01-31 13:03:55 +01001003static inline void add_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Jerome Marchandb238b3d2007-10-23 15:05:46 +02001005 drive_stat_acct(req, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 /*
1008 * elevator indicated where it wants this request to be
1009 * inserted at elevator_merge time
1010 */
1011 __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
1012}
Jens Axboe6728cb02008-01-31 13:03:55 +01001013
Tejun Heo074a7ac2008-08-25 19:56:14 +09001014static void part_round_stats_single(int cpu, struct hd_struct *part,
1015 unsigned long now)
1016{
1017 if (now == part->stamp)
1018 return;
1019
1020 if (part->in_flight) {
1021 __part_stat_add(cpu, part, time_in_queue,
1022 part->in_flight * (now - part->stamp));
1023 __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
1024 }
1025 part->stamp = now;
1026}
1027
1028/**
Randy Dunlap496aa8a2008-10-16 07:46:23 +02001029 * part_round_stats() - Round off the performance stats on a struct disk_stats.
1030 * @cpu: cpu number for stats access
1031 * @part: target partition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 *
1033 * The average IO queue length and utilisation statistics are maintained
1034 * by observing the current state of the queue length and the amount of
1035 * time it has been in this state for.
1036 *
1037 * Normally, that accounting is done on IO completion, but that can result
1038 * in more than a second's worth of IO being accounted for within any one
1039 * second, leading to >100% utilisation. To deal with that, we call this
1040 * function to do a round-off before returning the results when reading
1041 * /proc/diskstats. This accounts immediately for all queue usage up to
1042 * the current jiffies and restarts the counters again.
1043 */
Tejun Heoc9959052008-08-25 19:47:21 +09001044void part_round_stats(int cpu, struct hd_struct *part)
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001045{
1046 unsigned long now = jiffies;
1047
Tejun Heo074a7ac2008-08-25 19:56:14 +09001048 if (part->partno)
1049 part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
1050 part_round_stats_single(cpu, part, now);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001051}
Tejun Heo074a7ac2008-08-25 19:56:14 +09001052EXPORT_SYMBOL_GPL(part_round_stats);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054/*
1055 * queue lock must be held
1056 */
Jens Axboe165125e2007-07-24 09:28:11 +02001057void __blk_put_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 if (unlikely(!q))
1060 return;
1061 if (unlikely(--req->ref_count))
1062 return;
1063
Tejun Heo8922e162005-10-20 16:23:44 +02001064 elv_completed_request(q, req);
1065
Boaz Harrosh1cd96c22009-03-24 12:35:07 +01001066 /* this is a bio leak */
1067 WARN_ON(req->bio != NULL);
1068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 /*
1070 * Request may not have originated from ll_rw_blk. if not,
1071 * it didn't come out of our reserved rq pools
1072 */
Jens Axboe49171e52006-08-10 08:59:11 +02001073 if (req->cmd_flags & REQ_ALLOCED) {
Jens Axboe1faa16d2009-04-06 14:48:01 +02001074 int is_sync = rq_is_sync(req) != 0;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001075 int priv = req->cmd_flags & REQ_ELVPRIV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 BUG_ON(!list_empty(&req->queuelist));
Jens Axboe98170642006-07-28 09:23:08 +02001078 BUG_ON(!hlist_unhashed(&req->hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 blk_free_request(q, req);
Jens Axboe1faa16d2009-04-06 14:48:01 +02001081 freed_request(q, is_sync, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
1083}
Mike Christie6e39b69e2005-11-11 05:30:24 -06001084EXPORT_SYMBOL_GPL(__blk_put_request);
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086void blk_put_request(struct request *req)
1087{
Tejun Heo8922e162005-10-20 16:23:44 +02001088 unsigned long flags;
Jens Axboe165125e2007-07-24 09:28:11 +02001089 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
FUJITA Tomonori52a93ba2008-07-15 21:21:45 +02001091 spin_lock_irqsave(q->queue_lock, flags);
1092 __blk_put_request(q, req);
1093 spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095EXPORT_SYMBOL(blk_put_request);
1096
Jens Axboe86db1e22008-01-29 14:53:40 +01001097void init_request_from_bio(struct request *req, struct bio *bio)
Tejun Heo52d9e672006-01-06 09:49:58 +01001098{
Jens Axboec7c22e42008-09-13 20:26:01 +02001099 req->cpu = bio->bi_comp_cpu;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001100 req->cmd_type = REQ_TYPE_FS;
Tejun Heo52d9e672006-01-06 09:49:58 +01001101
1102 /*
1103 * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
1104 */
Mike Christie6000a362008-08-19 18:45:30 -05001105 if (bio_rw_ahead(bio))
1106 req->cmd_flags |= (REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
1107 REQ_FAILFAST_DRIVER);
1108 if (bio_failfast_dev(bio))
1109 req->cmd_flags |= REQ_FAILFAST_DEV;
1110 if (bio_failfast_transport(bio))
1111 req->cmd_flags |= REQ_FAILFAST_TRANSPORT;
1112 if (bio_failfast_driver(bio))
1113 req->cmd_flags |= REQ_FAILFAST_DRIVER;
Tejun Heo52d9e672006-01-06 09:49:58 +01001114
1115 /*
1116 * REQ_BARRIER implies no merging, but lets make it explicit
1117 */
David Woodhousefb2dce82008-08-05 18:01:53 +01001118 if (unlikely(bio_discard(bio))) {
David Woodhousee17fc0a2008-08-09 16:42:20 +01001119 req->cmd_flags |= REQ_DISCARD;
1120 if (bio_barrier(bio))
1121 req->cmd_flags |= REQ_SOFTBARRIER;
David Woodhousefb2dce82008-08-05 18:01:53 +01001122 req->q->prepare_discard_fn(req->q, req);
David Woodhousee17fc0a2008-08-09 16:42:20 +01001123 } else if (unlikely(bio_barrier(bio)))
1124 req->cmd_flags |= (REQ_HARDBARRIER | REQ_NOMERGE);
Tejun Heo52d9e672006-01-06 09:49:58 +01001125
Jens Axboeb31dc662006-06-13 08:26:10 +02001126 if (bio_sync(bio))
Jens Axboe4aff5e22006-08-10 08:44:47 +02001127 req->cmd_flags |= REQ_RW_SYNC;
Jens Axboe213d9412009-01-06 09:16:05 +01001128 if (bio_unplug(bio))
1129 req->cmd_flags |= REQ_UNPLUG;
Jens Axboe5404bc72006-08-10 09:01:02 +02001130 if (bio_rw_meta(bio))
1131 req->cmd_flags |= REQ_RW_META;
Jens Axboeaeb6faf2009-04-06 14:48:07 +02001132 if (bio_noidle(bio))
1133 req->cmd_flags |= REQ_NOIDLE;
Jens Axboeb31dc662006-06-13 08:26:10 +02001134
Tejun Heo52d9e672006-01-06 09:49:58 +01001135 req->errors = 0;
1136 req->hard_sector = req->sector = bio->bi_sector;
Tejun Heo52d9e672006-01-06 09:49:58 +01001137 req->ioprio = bio_prio(bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001138 req->start_time = jiffies;
NeilBrownbc1c56f2007-08-16 13:31:30 +02001139 blk_rq_bio_prep(req->q, req, bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001140}
1141
Jens Axboe644b2d92009-04-06 14:48:06 +02001142/*
1143 * Only disabling plugging for non-rotational devices if it does tagging
1144 * as well, otherwise we do need the proper merging
1145 */
1146static inline bool queue_should_plug(struct request_queue *q)
1147{
1148 return !(blk_queue_nonrot(q) && blk_queue_tagged(q));
1149}
1150
Jens Axboe165125e2007-07-24 09:28:11 +02001151static int __make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
Nick Piggin450991b2005-06-28 20:45:13 -07001153 struct request *req;
Tejun Heoa7384672008-11-28 13:32:03 +09001154 int el_ret, nr_sectors;
Jens Axboe51da90f2006-07-18 04:14:45 +02001155 const unsigned short prio = bio_prio(bio);
1156 const int sync = bio_sync(bio);
Jens Axboe213d9412009-01-06 09:16:05 +01001157 const int unplug = bio_unplug(bio);
Jens Axboe7749a8d2006-12-13 13:02:26 +01001158 int rw_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 nr_sectors = bio_sectors(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 /*
1163 * low level driver can indicate that it wants pages above a
1164 * certain limit bounced to low memory (ie for highmem, or even
1165 * ISA dma in theory)
1166 */
1167 blk_queue_bounce(q, &bio);
1168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 spin_lock_irq(q->queue_lock);
1170
Tejun Heoa7384672008-11-28 13:32:03 +09001171 if (unlikely(bio_barrier(bio)) || elv_queue_empty(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 goto get_rq;
1173
1174 el_ret = elv_merge(q, &req, bio);
1175 switch (el_ret) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001176 case ELEVATOR_BACK_MERGE:
1177 BUG_ON(!rq_mergeable(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Jens Axboe6728cb02008-01-31 13:03:55 +01001179 if (!ll_back_merge_fn(q, req, bio))
1180 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001182 trace_block_bio_backmerge(q, bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001183
Jens Axboe6728cb02008-01-31 13:03:55 +01001184 req->biotail->bi_next = bio;
1185 req->biotail = bio;
1186 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
1187 req->ioprio = ioprio_best(req->ioprio, prio);
Jens Axboeab780f12008-08-26 10:25:02 +02001188 if (!blk_rq_cpu_valid(req))
1189 req->cpu = bio->bi_comp_cpu;
Jens Axboe6728cb02008-01-31 13:03:55 +01001190 drive_stat_acct(req, 0);
1191 if (!attempt_back_merge(q, req))
1192 elv_merged_request(q, req, el_ret);
1193 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Jens Axboe6728cb02008-01-31 13:03:55 +01001195 case ELEVATOR_FRONT_MERGE:
1196 BUG_ON(!rq_mergeable(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Jens Axboe6728cb02008-01-31 13:03:55 +01001198 if (!ll_front_merge_fn(q, req, bio))
1199 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001201 trace_block_bio_frontmerge(q, bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001202
Jens Axboe6728cb02008-01-31 13:03:55 +01001203 bio->bi_next = req->bio;
1204 req->bio = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Jens Axboe6728cb02008-01-31 13:03:55 +01001206 /*
1207 * may not be valid. if the low level driver said
1208 * it didn't need a bounce buffer then it better
1209 * not touch req->buffer either...
1210 */
1211 req->buffer = bio_data(bio);
1212 req->current_nr_sectors = bio_cur_sectors(bio);
1213 req->hard_cur_sectors = req->current_nr_sectors;
1214 req->sector = req->hard_sector = bio->bi_sector;
1215 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
1216 req->ioprio = ioprio_best(req->ioprio, prio);
Jens Axboeab780f12008-08-26 10:25:02 +02001217 if (!blk_rq_cpu_valid(req))
1218 req->cpu = bio->bi_comp_cpu;
Jens Axboe6728cb02008-01-31 13:03:55 +01001219 drive_stat_acct(req, 0);
1220 if (!attempt_front_merge(q, req))
1221 elv_merged_request(q, req, el_ret);
1222 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Jens Axboe6728cb02008-01-31 13:03:55 +01001224 /* ELV_NO_MERGE: elevator says don't/can't merge. */
1225 default:
1226 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 }
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229get_rq:
Nick Piggin450991b2005-06-28 20:45:13 -07001230 /*
Jens Axboe7749a8d2006-12-13 13:02:26 +01001231 * This sync check and mask will be re-done in init_request_from_bio(),
1232 * but we need to set it earlier to expose the sync flag to the
1233 * rq allocator and io schedulers.
1234 */
1235 rw_flags = bio_data_dir(bio);
1236 if (sync)
1237 rw_flags |= REQ_RW_SYNC;
1238
1239 /*
Nick Piggin450991b2005-06-28 20:45:13 -07001240 * Grab a free request. This is might sleep but can not fail.
Nick Piggind6344532005-06-28 20:45:14 -07001241 * Returns with the queue unlocked.
Nick Piggin450991b2005-06-28 20:45:13 -07001242 */
Jens Axboe7749a8d2006-12-13 13:02:26 +01001243 req = get_request_wait(q, rw_flags, bio);
Nick Piggind6344532005-06-28 20:45:14 -07001244
Nick Piggin450991b2005-06-28 20:45:13 -07001245 /*
1246 * After dropping the lock and possibly sleeping here, our request
1247 * may now be mergeable after it had proven unmergeable (above).
1248 * We don't worry about that case for efficiency. It won't happen
1249 * often, and the elevators are able to handle it.
1250 */
Tejun Heo52d9e672006-01-06 09:49:58 +01001251 init_request_from_bio(req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Nick Piggin450991b2005-06-28 20:45:13 -07001253 spin_lock_irq(q->queue_lock);
Jens Axboec7c22e42008-09-13 20:26:01 +02001254 if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags) ||
1255 bio_flagged(bio, BIO_CPU_AFFINE))
1256 req->cpu = blk_cpu_to_group(smp_processor_id());
Jens Axboe644b2d92009-04-06 14:48:06 +02001257 if (queue_should_plug(q) && elv_queue_empty(q))
Nick Piggin450991b2005-06-28 20:45:13 -07001258 blk_plug_device(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 add_request(q, req);
1260out:
Jens Axboe644b2d92009-04-06 14:48:06 +02001261 if (unplug || !queue_should_plug(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 __generic_unplug_device(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 spin_unlock_irq(q->queue_lock);
1264 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265}
1266
1267/*
1268 * If bio->bi_dev is a partition, remap the location
1269 */
1270static inline void blk_partition_remap(struct bio *bio)
1271{
1272 struct block_device *bdev = bio->bi_bdev;
1273
Jens Axboebf2de6f2007-09-27 13:01:25 +02001274 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 struct hd_struct *p = bdev->bd_part;
Jens Axboea3623572005-11-01 09:26:16 +01001276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 bio->bi_sector += p->start_sect;
1278 bio->bi_bdev = bdev->bd_contains;
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001279
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001280 trace_block_remap(bdev_get_queue(bio->bi_bdev), bio,
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001281 bdev->bd_dev, bio->bi_sector,
1282 bio->bi_sector - p->start_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
1284}
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286static void handle_bad_sector(struct bio *bio)
1287{
1288 char b[BDEVNAME_SIZE];
1289
1290 printk(KERN_INFO "attempt to access beyond end of device\n");
1291 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1292 bdevname(bio->bi_bdev, b),
1293 bio->bi_rw,
1294 (unsigned long long)bio->bi_sector + bio_sectors(bio),
1295 (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
1296
1297 set_bit(BIO_EOF, &bio->bi_flags);
1298}
1299
Akinobu Mitac17bb492006-12-08 02:39:46 -08001300#ifdef CONFIG_FAIL_MAKE_REQUEST
1301
1302static DECLARE_FAULT_ATTR(fail_make_request);
1303
1304static int __init setup_fail_make_request(char *str)
1305{
1306 return setup_fault_attr(&fail_make_request, str);
1307}
1308__setup("fail_make_request=", setup_fail_make_request);
1309
1310static int should_fail_request(struct bio *bio)
1311{
Tejun Heoeddb2e22008-08-25 19:56:13 +09001312 struct hd_struct *part = bio->bi_bdev->bd_part;
1313
1314 if (part_to_disk(part)->part0.make_it_fail || part->make_it_fail)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001315 return should_fail(&fail_make_request, bio->bi_size);
1316
1317 return 0;
1318}
1319
1320static int __init fail_make_request_debugfs(void)
1321{
1322 return init_fault_attr_dentries(&fail_make_request,
1323 "fail_make_request");
1324}
1325
1326late_initcall(fail_make_request_debugfs);
1327
1328#else /* CONFIG_FAIL_MAKE_REQUEST */
1329
1330static inline int should_fail_request(struct bio *bio)
1331{
1332 return 0;
1333}
1334
1335#endif /* CONFIG_FAIL_MAKE_REQUEST */
1336
Jens Axboec07e2b42007-07-18 13:27:58 +02001337/*
1338 * Check whether this bio extends beyond the end of the device.
1339 */
1340static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1341{
1342 sector_t maxsector;
1343
1344 if (!nr_sectors)
1345 return 0;
1346
1347 /* Test device or partition size, when known. */
1348 maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
1349 if (maxsector) {
1350 sector_t sector = bio->bi_sector;
1351
1352 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1353 /*
1354 * This may well happen - the kernel calls bread()
1355 * without checking the size of the device, e.g., when
1356 * mounting a device.
1357 */
1358 handle_bad_sector(bio);
1359 return 1;
1360 }
1361 }
1362
1363 return 0;
1364}
1365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001367 * generic_make_request - hand a buffer to its device driver for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 * @bio: The bio describing the location in memory and on the device.
1369 *
1370 * generic_make_request() is used to make I/O requests of block
1371 * devices. It is passed a &struct bio, which describes the I/O that needs
1372 * to be done.
1373 *
1374 * generic_make_request() does not return any status. The
1375 * success/failure status of the request, along with notification of
1376 * completion, is delivered asynchronously through the bio->bi_end_io
1377 * function described (one day) else where.
1378 *
1379 * The caller of generic_make_request must make sure that bi_io_vec
1380 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1381 * set to describe the device address, and the
1382 * bi_end_io and optionally bi_private are set to describe how
1383 * completion notification should be signaled.
1384 *
1385 * generic_make_request and the drivers it calls may use bi_next if this
1386 * bio happens to be merged with someone else, and may change bi_dev and
1387 * bi_sector for remaps as it sees fit. So the values of these fields
1388 * should NOT be depended on after the call to generic_make_request.
1389 */
Neil Brownd89d8792007-05-01 09:53:42 +02001390static inline void __generic_make_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
Jens Axboe165125e2007-07-24 09:28:11 +02001392 struct request_queue *q;
NeilBrown5ddfe962006-10-30 22:07:21 -08001393 sector_t old_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 int ret, nr_sectors = bio_sectors(bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001395 dev_t old_dev;
Jens Axboe51fd77b2007-11-02 08:49:08 +01001396 int err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Jens Axboec07e2b42007-07-18 13:27:58 +02001400 if (bio_check_eod(bio, nr_sectors))
1401 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 /*
1404 * Resolve the mapping until finished. (drivers are
1405 * still free to implement/resolve their own stacking
1406 * by explicitly returning 0)
1407 *
1408 * NOTE: we don't repeat the blk_size check for each new device.
1409 * Stacking drivers are expected to know what they are doing.
1410 */
NeilBrown5ddfe962006-10-30 22:07:21 -08001411 old_sector = -1;
Jens Axboe2056a782006-03-23 20:00:26 +01001412 old_dev = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 do {
1414 char b[BDEVNAME_SIZE];
1415
1416 q = bdev_get_queue(bio->bi_bdev);
Tejun Heoa7384672008-11-28 13:32:03 +09001417 if (unlikely(!q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 printk(KERN_ERR
1419 "generic_make_request: Trying to access "
1420 "nonexistent block-device %s (%Lu)\n",
1421 bdevname(bio->bi_bdev, b),
1422 (long long) bio->bi_sector);
Tejun Heoa7384672008-11-28 13:32:03 +09001423 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 }
1425
Jens Axboe4fa253f2007-07-18 13:13:10 +02001426 if (unlikely(nr_sectors > q->max_hw_sectors)) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001427 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 bdevname(bio->bi_bdev, b),
1429 bio_sectors(bio),
1430 q->max_hw_sectors);
1431 goto end_io;
1432 }
1433
Nick Pigginfde6ad22005-06-23 00:08:53 -07001434 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 goto end_io;
1436
Akinobu Mitac17bb492006-12-08 02:39:46 -08001437 if (should_fail_request(bio))
1438 goto end_io;
1439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 /*
1441 * If this device has partitions, remap block n
1442 * of partition p to block n+start(p) of the disk.
1443 */
1444 blk_partition_remap(bio);
1445
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +02001446 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio))
1447 goto end_io;
1448
NeilBrown5ddfe962006-10-30 22:07:21 -08001449 if (old_sector != -1)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001450 trace_block_remap(q, bio, old_dev, bio->bi_sector,
NeilBrown5ddfe962006-10-30 22:07:21 -08001451 old_sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001452
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001453 trace_block_bio_queue(q, bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001454
NeilBrown5ddfe962006-10-30 22:07:21 -08001455 old_sector = bio->bi_sector;
Jens Axboe2056a782006-03-23 20:00:26 +01001456 old_dev = bio->bi_bdev->bd_dev;
1457
Jens Axboec07e2b42007-07-18 13:27:58 +02001458 if (bio_check_eod(bio, nr_sectors))
1459 goto end_io;
Tejun Heoa7384672008-11-28 13:32:03 +09001460
1461 if (bio_discard(bio) && !q->prepare_discard_fn) {
Jens Axboe51fd77b2007-11-02 08:49:08 +01001462 err = -EOPNOTSUPP;
1463 goto end_io;
1464 }
Jens Axboecec07072009-01-13 15:28:32 +01001465 if (bio_barrier(bio) && bio_has_data(bio) &&
1466 (q->next_ordered == QUEUE_ORDERED_NONE)) {
1467 err = -EOPNOTSUPP;
1468 goto end_io;
1469 }
NeilBrown5ddfe962006-10-30 22:07:21 -08001470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 ret = q->make_request_fn(q, bio);
1472 } while (ret);
Tejun Heoa7384672008-11-28 13:32:03 +09001473
1474 return;
1475
1476end_io:
1477 bio_endio(bio, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
1479
Neil Brownd89d8792007-05-01 09:53:42 +02001480/*
1481 * We only want one ->make_request_fn to be active at a time,
1482 * else stack usage with stacked devices could be a problem.
1483 * So use current->bio_{list,tail} to keep a list of requests
1484 * submited by a make_request_fn function.
1485 * current->bio_tail is also used as a flag to say if
1486 * generic_make_request is currently active in this task or not.
1487 * If it is NULL, then no make_request is active. If it is non-NULL,
1488 * then a make_request is active, and new requests should be added
1489 * at the tail
1490 */
1491void generic_make_request(struct bio *bio)
1492{
1493 if (current->bio_tail) {
1494 /* make_request is active */
1495 *(current->bio_tail) = bio;
1496 bio->bi_next = NULL;
1497 current->bio_tail = &bio->bi_next;
1498 return;
1499 }
1500 /* following loop may be a bit non-obvious, and so deserves some
1501 * explanation.
1502 * Before entering the loop, bio->bi_next is NULL (as all callers
1503 * ensure that) so we have a list with a single bio.
1504 * We pretend that we have just taken it off a longer list, so
1505 * we assign bio_list to the next (which is NULL) and bio_tail
1506 * to &bio_list, thus initialising the bio_list of new bios to be
1507 * added. __generic_make_request may indeed add some more bios
1508 * through a recursive call to generic_make_request. If it
1509 * did, we find a non-NULL value in bio_list and re-enter the loop
1510 * from the top. In this case we really did just take the bio
1511 * of the top of the list (no pretending) and so fixup bio_list and
1512 * bio_tail or bi_next, and call into __generic_make_request again.
1513 *
1514 * The loop was structured like this to make only one call to
1515 * __generic_make_request (which is important as it is large and
1516 * inlined) and to keep the structure simple.
1517 */
1518 BUG_ON(bio->bi_next);
1519 do {
1520 current->bio_list = bio->bi_next;
1521 if (bio->bi_next == NULL)
1522 current->bio_tail = &current->bio_list;
1523 else
1524 bio->bi_next = NULL;
1525 __generic_make_request(bio);
1526 bio = current->bio_list;
1527 } while (bio);
1528 current->bio_tail = NULL; /* deactivate */
1529}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530EXPORT_SYMBOL(generic_make_request);
1531
1532/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001533 * submit_bio - submit a bio to the block device layer for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1535 * @bio: The &struct bio which describes the I/O
1536 *
1537 * submit_bio() is very similar in purpose to generic_make_request(), and
1538 * uses that function to do most of the work. Both are fairly rough
Randy Dunlap710027a2008-08-19 20:13:11 +02001539 * interfaces; @bio must be presetup and ready for I/O.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 *
1541 */
1542void submit_bio(int rw, struct bio *bio)
1543{
1544 int count = bio_sectors(bio);
1545
Jens Axboe22e2c502005-06-27 10:55:12 +02001546 bio->bi_rw |= rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Jens Axboebf2de6f2007-09-27 13:01:25 +02001548 /*
1549 * If it's a regular read/write or a barrier with data attached,
1550 * go through the normal accounting stuff before submission.
1551 */
Jens Axboea9c701e2008-08-08 11:04:44 +02001552 if (bio_has_data(bio)) {
Jens Axboebf2de6f2007-09-27 13:01:25 +02001553 if (rw & WRITE) {
1554 count_vm_events(PGPGOUT, count);
1555 } else {
1556 task_io_account_read(bio->bi_size);
1557 count_vm_events(PGPGIN, count);
1558 }
1559
1560 if (unlikely(block_dump)) {
1561 char b[BDEVNAME_SIZE];
1562 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001563 current->comm, task_pid_nr(current),
Jens Axboebf2de6f2007-09-27 13:01:25 +02001564 (rw & WRITE) ? "WRITE" : "READ",
1565 (unsigned long long)bio->bi_sector,
Jens Axboe6728cb02008-01-31 13:03:55 +01001566 bdevname(bio->bi_bdev, b));
Jens Axboebf2de6f2007-09-27 13:01:25 +02001567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 }
1569
1570 generic_make_request(bio);
1571}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572EXPORT_SYMBOL(submit_bio);
1573
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001574/**
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001575 * blk_rq_check_limits - Helper function to check a request for the queue limit
1576 * @q: the queue
1577 * @rq: the request being checked
1578 *
1579 * Description:
1580 * @rq may have been made based on weaker limitations of upper-level queues
1581 * in request stacking drivers, and it may violate the limitation of @q.
1582 * Since the block layer and the underlying device driver trust @rq
1583 * after it is inserted to @q, it should be checked against @q before
1584 * the insertion using this generic function.
1585 *
1586 * This function should also be useful for request stacking drivers
1587 * in some cases below, so export this fuction.
1588 * Request stacking drivers like request-based dm may change the queue
1589 * limits while requests are in the queue (e.g. dm's table swapping).
1590 * Such request stacking drivers should check those requests agaist
1591 * the new queue limits again when they dispatch those requests,
1592 * although such checkings are also done against the old queue limits
1593 * when submitting requests.
1594 */
1595int blk_rq_check_limits(struct request_queue *q, struct request *rq)
1596{
1597 if (rq->nr_sectors > q->max_sectors ||
1598 rq->data_len > q->max_hw_sectors << 9) {
1599 printk(KERN_ERR "%s: over max size limit.\n", __func__);
1600 return -EIO;
1601 }
1602
1603 /*
1604 * queue's settings related to segment counting like q->bounce_pfn
1605 * may differ from that of other stacking queues.
1606 * Recalculate it to check the request correctly on this queue's
1607 * limitation.
1608 */
1609 blk_recalc_rq_segments(rq);
1610 if (rq->nr_phys_segments > q->max_phys_segments ||
1611 rq->nr_phys_segments > q->max_hw_segments) {
1612 printk(KERN_ERR "%s: over max segments limit.\n", __func__);
1613 return -EIO;
1614 }
1615
1616 return 0;
1617}
1618EXPORT_SYMBOL_GPL(blk_rq_check_limits);
1619
1620/**
1621 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
1622 * @q: the queue to submit the request
1623 * @rq: the request being queued
1624 */
1625int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
1626{
1627 unsigned long flags;
1628
1629 if (blk_rq_check_limits(q, rq))
1630 return -EIO;
1631
1632#ifdef CONFIG_FAIL_MAKE_REQUEST
1633 if (rq->rq_disk && rq->rq_disk->part0.make_it_fail &&
1634 should_fail(&fail_make_request, blk_rq_bytes(rq)))
1635 return -EIO;
1636#endif
1637
1638 spin_lock_irqsave(q->queue_lock, flags);
1639
1640 /*
1641 * Submitting request must be dequeued before calling this function
1642 * because it will be linked to another request_queue
1643 */
1644 BUG_ON(blk_queued_rq(rq));
1645
1646 drive_stat_acct(rq, 1);
1647 __elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 0);
1648
1649 spin_unlock_irqrestore(q->queue_lock, flags);
1650
1651 return 0;
1652}
1653EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
1654
1655/**
Tejun Heo53a08802008-12-03 12:41:26 +01001656 * blkdev_dequeue_request - dequeue request and start timeout timer
1657 * @req: request to dequeue
1658 *
1659 * Dequeue @req and start timeout timer on it. This hands off the
1660 * request to the driver.
1661 *
1662 * Block internal functions which don't want to start timer should
1663 * call elv_dequeue_request().
1664 */
1665void blkdev_dequeue_request(struct request *req)
1666{
1667 elv_dequeue_request(req->q, req);
1668
1669 /*
1670 * We are now handing the request to the hardware, add the
1671 * timeout handler.
1672 */
1673 blk_add_timer(req);
1674}
1675EXPORT_SYMBOL(blkdev_dequeue_request);
1676
Jens Axboebc58ba92009-01-23 10:54:44 +01001677static void blk_account_io_completion(struct request *req, unsigned int bytes)
1678{
1679 struct gendisk *disk = req->rq_disk;
1680
Jens Axboefb8ec182009-02-02 08:42:32 +01001681 if (!disk || !blk_do_io_stat(disk->queue))
Jens Axboebc58ba92009-01-23 10:54:44 +01001682 return;
1683
1684 if (blk_fs_request(req)) {
1685 const int rw = rq_data_dir(req);
1686 struct hd_struct *part;
1687 int cpu;
1688
1689 cpu = part_stat_lock();
1690 part = disk_map_sector_rcu(req->rq_disk, req->sector);
1691 part_stat_add(cpu, part, sectors[rw], bytes >> 9);
1692 part_stat_unlock();
1693 }
1694}
1695
1696static void blk_account_io_done(struct request *req)
1697{
1698 struct gendisk *disk = req->rq_disk;
1699
Jens Axboefb8ec182009-02-02 08:42:32 +01001700 if (!disk || !blk_do_io_stat(disk->queue))
Jens Axboebc58ba92009-01-23 10:54:44 +01001701 return;
1702
1703 /*
1704 * Account IO completion. bar_rq isn't accounted as a normal
1705 * IO on queueing nor completion. Accounting the containing
1706 * request is enough.
1707 */
1708 if (blk_fs_request(req) && req != &req->q->bar_rq) {
1709 unsigned long duration = jiffies - req->start_time;
1710 const int rw = rq_data_dir(req);
1711 struct hd_struct *part;
1712 int cpu;
1713
1714 cpu = part_stat_lock();
1715 part = disk_map_sector_rcu(disk, req->sector);
1716
1717 part_stat_inc(cpu, part, ios[rw]);
1718 part_stat_add(cpu, part, ticks[rw], duration);
1719 part_round_stats(cpu, part);
1720 part_dec_in_flight(part);
1721
1722 part_stat_unlock();
1723 }
1724}
1725
Tejun Heo53a08802008-12-03 12:41:26 +01001726/**
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001727 * __end_that_request_first - end I/O on a request
1728 * @req: the request being processed
Randy Dunlap710027a2008-08-19 20:13:11 +02001729 * @error: %0 for success, < %0 for error
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001730 * @nr_bytes: number of bytes to complete
1731 *
1732 * Description:
1733 * Ends I/O on a number of bytes attached to @req, and sets it up
1734 * for the next range of segments (if any) in the cluster.
1735 *
1736 * Return:
Randy Dunlap710027a2008-08-19 20:13:11 +02001737 * %0 - we are done with this request, call end_that_request_last()
1738 * %1 - still buffers pending for this request
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001739 **/
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001740static int __end_that_request_first(struct request *req, int error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 int nr_bytes)
1742{
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001743 int total_bytes, bio_nbytes, next_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 struct bio *bio;
1745
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001746 trace_block_rq_complete(req->q, req);
Jens Axboe2056a782006-03-23 20:00:26 +01001747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 /*
Randy Dunlap710027a2008-08-19 20:13:11 +02001749 * for a REQ_TYPE_BLOCK_PC request, we want to carry any eventual
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 * sense key with us all the way through
1751 */
1752 if (!blk_pc_request(req))
1753 req->errors = 0;
1754
Jens Axboe6728cb02008-01-31 13:03:55 +01001755 if (error && (blk_fs_request(req) && !(req->cmd_flags & REQ_QUIET))) {
1756 printk(KERN_ERR "end_request: I/O error, dev %s, sector %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 req->rq_disk ? req->rq_disk->disk_name : "?",
1758 (unsigned long long)req->sector);
1759 }
1760
Jens Axboebc58ba92009-01-23 10:54:44 +01001761 blk_account_io_completion(req, nr_bytes);
Jens Axboed72d9042005-11-01 08:35:42 +01001762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 total_bytes = bio_nbytes = 0;
1764 while ((bio = req->bio) != NULL) {
1765 int nbytes;
1766
1767 if (nr_bytes >= bio->bi_size) {
1768 req->bio = bio->bi_next;
1769 nbytes = bio->bi_size;
NeilBrown5bb23a62007-09-27 12:46:13 +02001770 req_bio_endio(req, bio, nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 next_idx = 0;
1772 bio_nbytes = 0;
1773 } else {
1774 int idx = bio->bi_idx + next_idx;
1775
1776 if (unlikely(bio->bi_idx >= bio->bi_vcnt)) {
1777 blk_dump_rq_flags(req, "__end_that");
Jens Axboe6728cb02008-01-31 13:03:55 +01001778 printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
Harvey Harrison24c03d42008-05-01 04:35:17 -07001779 __func__, bio->bi_idx, bio->bi_vcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 break;
1781 }
1782
1783 nbytes = bio_iovec_idx(bio, idx)->bv_len;
1784 BIO_BUG_ON(nbytes > bio->bi_size);
1785
1786 /*
1787 * not a complete bvec done
1788 */
1789 if (unlikely(nbytes > nr_bytes)) {
1790 bio_nbytes += nr_bytes;
1791 total_bytes += nr_bytes;
1792 break;
1793 }
1794
1795 /*
1796 * advance to the next vector
1797 */
1798 next_idx++;
1799 bio_nbytes += nbytes;
1800 }
1801
1802 total_bytes += nbytes;
1803 nr_bytes -= nbytes;
1804
Jens Axboe6728cb02008-01-31 13:03:55 +01001805 bio = req->bio;
1806 if (bio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 /*
1808 * end more in this run, or just return 'not-done'
1809 */
1810 if (unlikely(nr_bytes <= 0))
1811 break;
1812 }
1813 }
1814
1815 /*
1816 * completely done
1817 */
1818 if (!req->bio)
1819 return 0;
1820
1821 /*
1822 * if the request wasn't completed, update state
1823 */
1824 if (bio_nbytes) {
NeilBrown5bb23a62007-09-27 12:46:13 +02001825 req_bio_endio(req, bio, bio_nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 bio->bi_idx += next_idx;
1827 bio_iovec(bio)->bv_offset += nr_bytes;
1828 bio_iovec(bio)->bv_len -= nr_bytes;
1829 }
1830
1831 blk_recalc_rq_sectors(req, total_bytes >> 9);
1832 blk_recalc_rq_segments(req);
1833 return 1;
1834}
1835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836/*
1837 * queue lock must be held
1838 */
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001839static void end_that_request_last(struct request *req, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840{
Kiyoshi Uedab8286232007-12-11 17:53:24 -05001841 if (blk_rq_tagged(req))
1842 blk_queue_end_tag(req->q, req);
1843
1844 if (blk_queued_rq(req))
Tejun Heo53a08802008-12-03 12:41:26 +01001845 elv_dequeue_request(req->q, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
1847 if (unlikely(laptop_mode) && blk_fs_request(req))
1848 laptop_io_completion();
1849
Mike Andersone78042e2008-10-30 02:16:20 -07001850 blk_delete_timer(req);
1851
Jens Axboebc58ba92009-01-23 10:54:44 +01001852 blk_account_io_done(req);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05001853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 if (req->end_io)
Tejun Heo8ffdc652006-01-06 09:49:03 +01001855 req->end_io(req, error);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05001856 else {
1857 if (blk_bidi_rq(req))
1858 __blk_put_request(req->next_rq->q, req->next_rq);
1859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 __blk_put_request(req->q, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 }
1862}
1863
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05001864/**
1865 * blk_rq_bytes - Returns bytes left to complete in the entire request
Randy Dunlap5d87a052008-02-20 09:01:22 +01001866 * @rq: the request being processed
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05001867 **/
1868unsigned int blk_rq_bytes(struct request *rq)
Jens Axboea0cd1282007-09-21 10:41:07 +02001869{
1870 if (blk_fs_request(rq))
1871 return rq->hard_nr_sectors << 9;
1872
1873 return rq->data_len;
1874}
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05001875EXPORT_SYMBOL_GPL(blk_rq_bytes);
1876
1877/**
1878 * blk_rq_cur_bytes - Returns bytes left to complete in the current segment
Randy Dunlap5d87a052008-02-20 09:01:22 +01001879 * @rq: the request being processed
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05001880 **/
1881unsigned int blk_rq_cur_bytes(struct request *rq)
1882{
1883 if (blk_fs_request(rq))
1884 return rq->current_nr_sectors << 9;
1885
1886 if (rq->bio)
1887 return rq->bio->bi_size;
1888
1889 return rq->data_len;
1890}
1891EXPORT_SYMBOL_GPL(blk_rq_cur_bytes);
Jens Axboea0cd1282007-09-21 10:41:07 +02001892
1893/**
Jens Axboea0cd1282007-09-21 10:41:07 +02001894 * end_request - end I/O on the current segment of the request
Randy Dunlap8f731f72007-10-18 23:39:28 -07001895 * @req: the request being processed
Randy Dunlap710027a2008-08-19 20:13:11 +02001896 * @uptodate: error value or %0/%1 uptodate flag
Jens Axboea0cd1282007-09-21 10:41:07 +02001897 *
1898 * Description:
1899 * Ends I/O on the current segment of a request. If that is the only
1900 * remaining segment, the request is also completed and freed.
1901 *
Randy Dunlap710027a2008-08-19 20:13:11 +02001902 * This is a remnant of how older block drivers handled I/O completions.
1903 * Modern drivers typically end I/O on the full request in one go, unless
Jens Axboea0cd1282007-09-21 10:41:07 +02001904 * they have a residual value to account for. For that case this function
1905 * isn't really useful, unless the residual just happens to be the
1906 * full current segment. In other words, don't use this function in new
Kiyoshi Uedad00e29f2008-10-01 10:14:46 -04001907 * code. Use blk_end_request() or __blk_end_request() to end a request.
Jens Axboea0cd1282007-09-21 10:41:07 +02001908 **/
1909void end_request(struct request *req, int uptodate)
1910{
Kiyoshi Uedad00e29f2008-10-01 10:14:46 -04001911 int error = 0;
1912
1913 if (uptodate <= 0)
1914 error = uptodate ? uptodate : -EIO;
1915
1916 __blk_end_request(req, error, req->hard_cur_sectors << 9);
Jens Axboea0cd1282007-09-21 10:41:07 +02001917}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918EXPORT_SYMBOL(end_request);
1919
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04001920static int end_that_request_data(struct request *rq, int error,
1921 unsigned int nr_bytes, unsigned int bidi_bytes)
1922{
1923 if (rq->bio) {
1924 if (__end_that_request_first(rq, error, nr_bytes))
1925 return 1;
1926
1927 /* Bidi request must be completed as a whole */
1928 if (blk_bidi_rq(rq) &&
1929 __end_that_request_first(rq->next_rq, error, bidi_bytes))
1930 return 1;
1931 }
1932
1933 return 0;
1934}
1935
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001936/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001937 * blk_end_io - Generic end_io function to complete a request.
1938 * @rq: the request being processed
Randy Dunlap710027a2008-08-19 20:13:11 +02001939 * @error: %0 for success, < %0 for error
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001940 * @nr_bytes: number of bytes to complete @rq
1941 * @bidi_bytes: number of bytes to complete @rq->next_rq
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001942 * @drv_callback: function called between completion of bios in the request
1943 * and completion of the request.
Randy Dunlap710027a2008-08-19 20:13:11 +02001944 * If the callback returns non %0, this helper returns without
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001945 * completion of the request.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001946 *
1947 * Description:
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001948 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001949 * If @rq has leftover, sets it up for the next range of segments.
1950 *
1951 * Return:
Randy Dunlap710027a2008-08-19 20:13:11 +02001952 * %0 - we are done with this request
1953 * %1 - this request is not freed yet, it still has pending buffers.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001954 **/
Jens Axboe22b13212008-01-31 12:36:19 +01001955static int blk_end_io(struct request *rq, int error, unsigned int nr_bytes,
1956 unsigned int bidi_bytes,
1957 int (drv_callback)(struct request *))
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001958{
1959 struct request_queue *q = rq->q;
1960 unsigned long flags = 0UL;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001961
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04001962 if (end_that_request_data(rq, error, nr_bytes, bidi_bytes))
1963 return 1;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001964
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001965 /* Special feature for tricky drivers */
1966 if (drv_callback && drv_callback(rq))
1967 return 1;
1968
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001969 add_disk_randomness(rq->rq_disk);
1970
1971 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05001972 end_that_request_last(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001973 spin_unlock_irqrestore(q->queue_lock, flags);
1974
1975 return 0;
1976}
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001977
1978/**
1979 * blk_end_request - Helper function for drivers to complete the request.
1980 * @rq: the request being processed
Randy Dunlap710027a2008-08-19 20:13:11 +02001981 * @error: %0 for success, < %0 for error
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001982 * @nr_bytes: number of bytes to complete
1983 *
1984 * Description:
1985 * Ends I/O on a number of bytes attached to @rq.
1986 * If @rq has leftover, sets it up for the next range of segments.
1987 *
1988 * Return:
Randy Dunlap710027a2008-08-19 20:13:11 +02001989 * %0 - we are done with this request
1990 * %1 - still buffers pending for this request
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001991 **/
Jens Axboe22b13212008-01-31 12:36:19 +01001992int blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001993{
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05001994 return blk_end_io(rq, error, nr_bytes, 0, NULL);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05001995}
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05001996EXPORT_SYMBOL_GPL(blk_end_request);
1997
1998/**
1999 * __blk_end_request - Helper function for drivers to complete the request.
2000 * @rq: the request being processed
Randy Dunlap710027a2008-08-19 20:13:11 +02002001 * @error: %0 for success, < %0 for error
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002002 * @nr_bytes: number of bytes to complete
2003 *
2004 * Description:
2005 * Must be called with queue lock held unlike blk_end_request().
2006 *
2007 * Return:
Randy Dunlap710027a2008-08-19 20:13:11 +02002008 * %0 - we are done with this request
2009 * %1 - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002010 **/
Jens Axboe22b13212008-01-31 12:36:19 +01002011int __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002012{
Jens Axboe60540162008-08-26 13:34:34 +02002013 if (rq->bio && __end_that_request_first(rq, error, nr_bytes))
Jens Axboe051cc392008-08-08 11:06:45 +02002014 return 1;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002015
2016 add_disk_randomness(rq->rq_disk);
2017
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002018 end_that_request_last(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002019
2020 return 0;
2021}
2022EXPORT_SYMBOL_GPL(__blk_end_request);
2023
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002024/**
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002025 * blk_end_bidi_request - Helper function for drivers to complete bidi request.
2026 * @rq: the bidi request being processed
Randy Dunlap710027a2008-08-19 20:13:11 +02002027 * @error: %0 for success, < %0 for error
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002028 * @nr_bytes: number of bytes to complete @rq
2029 * @bidi_bytes: number of bytes to complete @rq->next_rq
2030 *
2031 * Description:
2032 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
2033 *
2034 * Return:
Randy Dunlap710027a2008-08-19 20:13:11 +02002035 * %0 - we are done with this request
2036 * %1 - still buffers pending for this request
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002037 **/
Jens Axboe22b13212008-01-31 12:36:19 +01002038int blk_end_bidi_request(struct request *rq, int error, unsigned int nr_bytes,
2039 unsigned int bidi_bytes)
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002040{
2041 return blk_end_io(rq, error, nr_bytes, bidi_bytes, NULL);
2042}
2043EXPORT_SYMBOL_GPL(blk_end_bidi_request);
2044
2045/**
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002046 * blk_update_request - Special helper function for request stacking drivers
2047 * @rq: the request being processed
2048 * @error: %0 for success, < %0 for error
2049 * @nr_bytes: number of bytes to complete @rq
2050 *
2051 * Description:
2052 * Ends I/O on a number of bytes attached to @rq, but doesn't complete
2053 * the request structure even if @rq doesn't have leftover.
2054 * If @rq has leftover, sets it up for the next range of segments.
2055 *
2056 * This special helper function is only for request stacking drivers
2057 * (e.g. request-based dm) so that they can handle partial completion.
2058 * Actual device drivers should use blk_end_request instead.
2059 */
2060void blk_update_request(struct request *rq, int error, unsigned int nr_bytes)
2061{
2062 if (!end_that_request_data(rq, error, nr_bytes, 0)) {
2063 /*
2064 * These members are not updated in end_that_request_data()
2065 * when all bios are completed.
2066 * Update them so that the request stacking driver can find
2067 * how many bytes remain in the request later.
2068 */
2069 rq->nr_sectors = rq->hard_nr_sectors = 0;
2070 rq->current_nr_sectors = rq->hard_cur_sectors = 0;
2071 }
2072}
2073EXPORT_SYMBOL_GPL(blk_update_request);
2074
2075/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002076 * blk_end_request_callback - Special helper function for tricky drivers
2077 * @rq: the request being processed
Randy Dunlap710027a2008-08-19 20:13:11 +02002078 * @error: %0 for success, < %0 for error
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002079 * @nr_bytes: number of bytes to complete
2080 * @drv_callback: function called between completion of bios in the request
2081 * and completion of the request.
Randy Dunlap710027a2008-08-19 20:13:11 +02002082 * If the callback returns non %0, this helper returns without
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002083 * completion of the request.
2084 *
2085 * Description:
2086 * Ends I/O on a number of bytes attached to @rq.
2087 * If @rq has leftover, sets it up for the next range of segments.
2088 *
2089 * This special helper function is used only for existing tricky drivers.
2090 * (e.g. cdrom_newpc_intr() of ide-cd)
2091 * This interface will be removed when such drivers are rewritten.
2092 * Don't use this interface in other places anymore.
2093 *
2094 * Return:
Randy Dunlap710027a2008-08-19 20:13:11 +02002095 * %0 - we are done with this request
2096 * %1 - this request is not freed yet.
2097 * this request still has pending buffers or
2098 * the driver doesn't want to finish this request yet.
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002099 **/
Jens Axboe22b13212008-01-31 12:36:19 +01002100int blk_end_request_callback(struct request *rq, int error,
2101 unsigned int nr_bytes,
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002102 int (drv_callback)(struct request *))
2103{
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002104 return blk_end_io(rq, error, nr_bytes, 0, drv_callback);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002105}
2106EXPORT_SYMBOL_GPL(blk_end_request_callback);
2107
Jens Axboe86db1e22008-01-29 14:53:40 +01002108void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2109 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110{
David Woodhoused628eae2008-08-09 16:22:17 +01002111 /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw, and
2112 we want BIO_RW_AHEAD (bit 1) to imply REQ_FAILFAST (bit 1). */
Jens Axboe4aff5e22006-08-10 08:44:47 +02002113 rq->cmd_flags |= (bio->bi_rw & 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
David Woodhousefb2dce82008-08-05 18:01:53 +01002115 if (bio_has_data(bio)) {
2116 rq->nr_phys_segments = bio_phys_segments(q, bio);
David Woodhousefb2dce82008-08-05 18:01:53 +01002117 rq->buffer = bio_data(bio);
2118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 rq->current_nr_sectors = bio_cur_sectors(bio);
2120 rq->hard_cur_sectors = rq->current_nr_sectors;
2121 rq->hard_nr_sectors = rq->nr_sectors = bio_sectors(bio);
Mike Christie0e75f902006-12-01 10:40:55 +01002122 rq->data_len = bio->bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
2124 rq->bio = rq->biotail = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
NeilBrown66846572007-08-16 13:31:28 +02002126 if (bio->bi_bdev)
2127 rq->rq_disk = bio->bi_bdev->bd_disk;
2128}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Kiyoshi Uedaef9e3fa2008-10-01 16:12:15 +02002130/**
2131 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
2132 * @q : the queue of the device being checked
2133 *
2134 * Description:
2135 * Check if underlying low-level drivers of a device are busy.
2136 * If the drivers want to export their busy state, they must set own
2137 * exporting function using blk_queue_lld_busy() first.
2138 *
2139 * Basically, this function is used only by request stacking drivers
2140 * to stop dispatching requests to underlying devices when underlying
2141 * devices are busy. This behavior helps more I/O merging on the queue
2142 * of the request stacking driver and prevents I/O throughput regression
2143 * on burst I/O load.
2144 *
2145 * Return:
2146 * 0 - Not busy (The request stacking driver should dispatch request)
2147 * 1 - Busy (The request stacking driver should stop dispatching request)
2148 */
2149int blk_lld_busy(struct request_queue *q)
2150{
2151 if (q->lld_busy_fn)
2152 return q->lld_busy_fn(q);
2153
2154 return 0;
2155}
2156EXPORT_SYMBOL_GPL(blk_lld_busy);
2157
Jens Axboe18887ad2008-07-28 13:08:45 +02002158int kblockd_schedule_work(struct request_queue *q, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159{
2160 return queue_work(kblockd_workqueue, work);
2161}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162EXPORT_SYMBOL(kblockd_schedule_work);
2163
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164int __init blk_dev_init(void)
2165{
2166 kblockd_workqueue = create_workqueue("kblockd");
2167 if (!kblockd_workqueue)
2168 panic("Failed to create kblockd\n");
2169
2170 request_cachep = kmem_cache_create("blkdev_requests",
Paul Mundt20c2df82007-07-20 10:11:58 +09002171 sizeof(struct request), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
Jens Axboe8324aa92008-01-29 14:51:59 +01002173 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02002174 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 return 0;
2177}
2178