blob: e3299a77a0d8b44d08944d9577d309dc4e40011a [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>
Akinobu Mitac17bb492006-12-08 02:39:46 -080029#include <linux/fault-inject.h>
Li Zefan55782132009-06-09 13:43:05 +080030
31#define CREATE_TRACE_POINTS
32#include <trace/events/block.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Jens Axboe8324aa92008-01-29 14:51:59 +010034#include "blk.h"
35
Ingo Molnar0bfc2452008-11-26 11:59:56 +010036EXPORT_TRACEPOINT_SYMBOL_GPL(block_remap);
Li Zefan55782132009-06-09 13:43:05 +080037EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_complete);
Ingo Molnar0bfc2452008-11-26 11:59:56 +010038
Jens Axboe165125e2007-07-24 09:28:11 +020039static int __make_request(struct request_queue *q, struct bio *bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/*
42 * For the allocated request tables
43 */
Adrian Bunk5ece6c52008-02-18 13:45:51 +010044static struct kmem_cache *request_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46/*
47 * For queue allocation
48 */
Jens Axboe6728cb02008-01-31 13:03:55 +010049struct kmem_cache *blk_requestq_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * Controlling structure to kblockd
53 */
Jens Axboeff856ba2006-01-09 16:02:34 +010054static struct workqueue_struct *kblockd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Jens Axboe26b82562008-01-29 13:54:41 +010056static void drive_stat_acct(struct request *rq, int new_io)
57{
Jens Axboe28f13702008-05-07 10:15:46 +020058 struct hd_struct *part;
Jens Axboe26b82562008-01-29 13:54:41 +010059 int rw = rq_data_dir(rq);
Tejun Heoc9959052008-08-25 19:47:21 +090060 int cpu;
Jens Axboe26b82562008-01-29 13:54:41 +010061
Jens Axboec2553b52009-04-24 08:10:11 +020062 if (!blk_do_io_stat(rq))
Jens Axboe26b82562008-01-29 13:54:41 +010063 return;
64
Tejun Heo074a7ac2008-08-25 19:56:14 +090065 cpu = part_stat_lock();
Tejun Heo83096eb2009-05-07 22:24:39 +090066 part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq));
Tejun Heoc9959052008-08-25 19:47:21 +090067
Jens Axboe28f13702008-05-07 10:15:46 +020068 if (!new_io)
Tejun Heo074a7ac2008-08-25 19:56:14 +090069 part_stat_inc(cpu, part, merges[rw]);
Jens Axboe28f13702008-05-07 10:15:46 +020070 else {
Tejun Heo074a7ac2008-08-25 19:56:14 +090071 part_round_stats(cpu, part);
72 part_inc_in_flight(part);
Jens Axboe26b82562008-01-29 13:54:41 +010073 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +020074
Tejun Heo074a7ac2008-08-25 19:56:14 +090075 part_stat_unlock();
Jens Axboe26b82562008-01-29 13:54:41 +010076}
77
Jens Axboe8324aa92008-01-29 14:51:59 +010078void blk_queue_congestion_threshold(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
80 int nr;
81
82 nr = q->nr_requests - (q->nr_requests / 8) + 1;
83 if (nr > q->nr_requests)
84 nr = q->nr_requests;
85 q->nr_congestion_on = nr;
86
87 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
88 if (nr < 1)
89 nr = 1;
90 q->nr_congestion_off = nr;
91}
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/**
94 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
95 * @bdev: device
96 *
97 * Locates the passed device's request queue and returns the address of its
98 * backing_dev_info
99 *
100 * Will return NULL if the request queue cannot be located.
101 */
102struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
103{
104 struct backing_dev_info *ret = NULL;
Jens Axboe165125e2007-07-24 09:28:11 +0200105 struct request_queue *q = bdev_get_queue(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 if (q)
108 ret = &q->backing_dev_info;
109 return ret;
110}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111EXPORT_SYMBOL(blk_get_backing_dev_info);
112
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200113void blk_rq_init(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200115 memset(rq, 0, sizeof(*rq));
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 INIT_LIST_HEAD(&rq->queuelist);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700118 INIT_LIST_HEAD(&rq->timeout_list);
Jens Axboec7c22e42008-09-13 20:26:01 +0200119 rq->cpu = -1;
Jens Axboe63a71382008-02-08 12:41:03 +0100120 rq->q = q;
Tejun Heoa2dec7b2009-05-07 22:24:44 +0900121 rq->__sector = (sector_t) -1;
Jens Axboe2e662b62006-07-13 11:55:04 +0200122 INIT_HLIST_NODE(&rq->hash);
123 RB_CLEAR_NODE(&rq->rb_node);
FUJITA Tomonorid7e3c322008-04-29 09:54:39 +0200124 rq->cmd = rq->__cmd;
Li Zefane2494e12009-04-02 13:43:26 +0800125 rq->cmd_len = BLK_MAX_CDB;
Jens Axboe63a71382008-02-08 12:41:03 +0100126 rq->tag = -1;
Jens Axboe63a71382008-02-08 12:41:03 +0100127 rq->ref_count = 1;
Tejun Heob243ddc2009-04-23 11:05:18 +0900128 rq->start_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200130EXPORT_SYMBOL(blk_rq_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
NeilBrown5bb23a62007-09-27 12:46:13 +0200132static void req_bio_endio(struct request *rq, struct bio *bio,
133 unsigned int nbytes, int error)
Tejun Heo797e7db2006-01-06 09:51:03 +0100134{
Jens Axboe165125e2007-07-24 09:28:11 +0200135 struct request_queue *q = rq->q;
Tejun Heo797e7db2006-01-06 09:51:03 +0100136
NeilBrown5bb23a62007-09-27 12:46:13 +0200137 if (&q->bar_rq != rq) {
138 if (error)
139 clear_bit(BIO_UPTODATE, &bio->bi_flags);
140 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
141 error = -EIO;
Tejun Heo797e7db2006-01-06 09:51:03 +0100142
NeilBrown5bb23a62007-09-27 12:46:13 +0200143 if (unlikely(nbytes > bio->bi_size)) {
Jens Axboe6728cb02008-01-31 13:03:55 +0100144 printk(KERN_ERR "%s: want %u bytes done, %u left\n",
Harvey Harrison24c03d42008-05-01 04:35:17 -0700145 __func__, nbytes, bio->bi_size);
NeilBrown5bb23a62007-09-27 12:46:13 +0200146 nbytes = bio->bi_size;
147 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100148
Keith Mannthey08bafc02008-11-25 10:24:35 +0100149 if (unlikely(rq->cmd_flags & REQ_QUIET))
150 set_bit(BIO_QUIET, &bio->bi_flags);
151
NeilBrown5bb23a62007-09-27 12:46:13 +0200152 bio->bi_size -= nbytes;
153 bio->bi_sector += (nbytes >> 9);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200154
155 if (bio_integrity(bio))
156 bio_integrity_advance(bio, nbytes);
157
NeilBrown5bb23a62007-09-27 12:46:13 +0200158 if (bio->bi_size == 0)
NeilBrown6712ecf2007-09-27 12:47:43 +0200159 bio_endio(bio, error);
NeilBrown5bb23a62007-09-27 12:46:13 +0200160 } else {
161
162 /*
163 * Okay, this is the barrier request in progress, just
164 * record the error;
165 */
166 if (error && !q->orderr)
167 q->orderr = error;
168 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100169}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171void blk_dump_rq_flags(struct request *rq, char *msg)
172{
173 int bit;
174
Jens Axboe6728cb02008-01-31 13:03:55 +0100175 printk(KERN_INFO "%s: dev %s: type=%x, flags=%x\n", msg,
Jens Axboe4aff5e22006-08-10 08:44:47 +0200176 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
177 rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Tejun Heo83096eb2009-05-07 22:24:39 +0900179 printk(KERN_INFO " sector %llu, nr/cnr %u/%u\n",
180 (unsigned long long)blk_rq_pos(rq),
181 blk_rq_sectors(rq), blk_rq_cur_sectors(rq));
Tejun Heo731ec492009-04-23 11:05:20 +0900182 printk(KERN_INFO " bio %p, biotail %p, buffer %p, len %u\n",
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900183 rq->bio, rq->biotail, rq->buffer, blk_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Jens Axboe4aff5e22006-08-10 08:44:47 +0200185 if (blk_pc_request(rq)) {
Jens Axboe6728cb02008-01-31 13:03:55 +0100186 printk(KERN_INFO " cdb: ");
FUJITA Tomonorid34c87e2008-04-29 14:37:52 +0200187 for (bit = 0; bit < BLK_MAX_CDB; bit++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 printk("%02x ", rq->cmd[bit]);
189 printk("\n");
190 }
191}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192EXPORT_SYMBOL(blk_dump_rq_flags);
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194/*
195 * "plug" the device if there are no outstanding requests: this will
196 * force the transfer to start only after we have put all the requests
197 * on the list.
198 *
199 * This is called with interrupts off and no requests on the queue and
200 * with the queue lock held.
201 */
Jens Axboe165125e2007-07-24 09:28:11 +0200202void blk_plug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 WARN_ON(!irqs_disabled());
205
206 /*
207 * don't plug a stopped queue, it must be paired with blk_start_queue()
208 * which will restart the queueing
209 */
Coywolf Qi Hunt7daac492006-04-19 10:14:49 +0200210 if (blk_queue_stopped(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return;
212
Jens Axboee48ec692008-07-03 13:18:54 +0200213 if (!queue_flag_test_and_set(QUEUE_FLAG_PLUGGED, q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100215 trace_block_plug(q);
Jens Axboe2056a782006-03-23 20:00:26 +0100216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218EXPORT_SYMBOL(blk_plug_device);
219
Jens Axboe6c5e0c42008-08-01 20:31:32 +0200220/**
221 * blk_plug_device_unlocked - plug a device without queue lock held
222 * @q: The &struct request_queue to plug
223 *
224 * Description:
225 * Like @blk_plug_device(), but grabs the queue lock and disables
226 * interrupts.
227 **/
228void blk_plug_device_unlocked(struct request_queue *q)
229{
230 unsigned long flags;
231
232 spin_lock_irqsave(q->queue_lock, flags);
233 blk_plug_device(q);
234 spin_unlock_irqrestore(q->queue_lock, flags);
235}
236EXPORT_SYMBOL(blk_plug_device_unlocked);
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238/*
239 * remove the queue from the plugged list, if present. called with
240 * queue lock held and interrupts disabled.
241 */
Jens Axboe165125e2007-07-24 09:28:11 +0200242int blk_remove_plug(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 WARN_ON(!irqs_disabled());
245
Jens Axboee48ec692008-07-03 13:18:54 +0200246 if (!queue_flag_test_and_clear(QUEUE_FLAG_PLUGGED, q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return 0;
248
249 del_timer(&q->unplug_timer);
250 return 1;
251}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252EXPORT_SYMBOL(blk_remove_plug);
253
254/*
255 * remove the plug and let it rip..
256 */
Jens Axboe165125e2007-07-24 09:28:11 +0200257void __generic_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Coywolf Qi Hunt7daac492006-04-19 10:14:49 +0200259 if (unlikely(blk_queue_stopped(q)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return;
Jens Axboea31a9732008-10-17 13:58:29 +0200261 if (!blk_remove_plug(q) && !blk_queue_nonrot(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return;
263
Jens Axboe22e2c502005-06-27 10:55:12 +0200264 q->request_fn(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267/**
268 * generic_unplug_device - fire a request queue
Jens Axboe165125e2007-07-24 09:28:11 +0200269 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 *
271 * Description:
272 * Linux uses plugging to build bigger requests queues before letting
273 * the device have at them. If a queue is plugged, the I/O scheduler
274 * is still adding and merging requests on the queue. Once the queue
275 * gets unplugged, the request_fn defined for the queue is invoked and
276 * transfers started.
277 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200278void generic_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Jens Axboedbaf2c02008-05-07 09:48:17 +0200280 if (blk_queue_plugged(q)) {
281 spin_lock_irq(q->queue_lock);
282 __generic_unplug_device(q);
283 spin_unlock_irq(q->queue_lock);
284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286EXPORT_SYMBOL(generic_unplug_device);
287
288static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
289 struct page *page)
290{
Jens Axboe165125e2007-07-24 09:28:11 +0200291 struct request_queue *q = bdi->unplug_io_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500293 blk_unplug(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
Jens Axboe86db1e22008-01-29 14:53:40 +0100296void blk_unplug_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Jens Axboe165125e2007-07-24 09:28:11 +0200298 struct request_queue *q =
299 container_of(work, struct request_queue, unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100301 trace_block_unplug_io(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 q->unplug_fn(q);
303}
304
Jens Axboe86db1e22008-01-29 14:53:40 +0100305void blk_unplug_timeout(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Jens Axboe165125e2007-07-24 09:28:11 +0200307 struct request_queue *q = (struct request_queue *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100309 trace_block_unplug_timer(q);
Jens Axboe18887ad2008-07-28 13:08:45 +0200310 kblockd_schedule_work(q, &q->unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500313void blk_unplug(struct request_queue *q)
314{
315 /*
316 * devices don't necessarily have an ->unplug_fn defined
317 */
318 if (q->unplug_fn) {
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100319 trace_block_unplug_io(q);
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500320 q->unplug_fn(q);
321 }
322}
323EXPORT_SYMBOL(blk_unplug);
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325/**
326 * blk_start_queue - restart a previously stopped queue
Jens Axboe165125e2007-07-24 09:28:11 +0200327 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 *
329 * Description:
330 * blk_start_queue() will clear the stop flag on the queue, and call
331 * the request_fn for the queue if it was in a stopped state when
332 * entered. Also see blk_stop_queue(). Queue lock must be held.
333 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200334void blk_start_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200336 WARN_ON(!irqs_disabled());
337
Nick Piggin75ad23b2008-04-29 14:48:33 +0200338 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
Tejun Heoa538cd02009-04-23 11:05:17 +0900339 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341EXPORT_SYMBOL(blk_start_queue);
342
343/**
344 * blk_stop_queue - stop a queue
Jens Axboe165125e2007-07-24 09:28:11 +0200345 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 *
347 * Description:
348 * The Linux block layer assumes that a block driver will consume all
349 * entries on the request queue when the request_fn strategy is called.
350 * Often this will not happen, because of hardware limitations (queue
351 * depth settings). If a device driver gets a 'queue full' response,
352 * or if it simply chooses not to queue more I/O at one point, it can
353 * call this function to prevent the request_fn from being called until
354 * the driver has signalled it's ready to go again. This happens by calling
355 * blk_start_queue() to restart queue operations. Queue lock must be held.
356 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200357void blk_stop_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 blk_remove_plug(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200360 queue_flag_set(QUEUE_FLAG_STOPPED, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362EXPORT_SYMBOL(blk_stop_queue);
363
364/**
365 * blk_sync_queue - cancel any pending callbacks on a queue
366 * @q: the queue
367 *
368 * Description:
369 * The block layer may perform asynchronous callback activity
370 * on a queue, such as calling the unplug function after a timeout.
371 * A block device may call blk_sync_queue to ensure that any
372 * such activity is cancelled, thus allowing it to release resources
Michael Opdenacker59c51592007-05-09 08:57:56 +0200373 * that the callbacks might use. The caller must already have made sure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 * that its ->make_request_fn will not re-add plugging prior to calling
375 * this function.
376 *
377 */
378void blk_sync_queue(struct request_queue *q)
379{
380 del_timer_sync(&q->unplug_timer);
Jens Axboe70ed28b2008-11-19 14:38:39 +0100381 del_timer_sync(&q->timeout);
Cheng Renquan64d01dc2008-12-03 12:41:39 +0100382 cancel_work_sync(&q->unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384EXPORT_SYMBOL(blk_sync_queue);
385
386/**
Jens Axboe80a4b582008-10-14 09:51:06 +0200387 * __blk_run_queue - run a single device queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200389 *
390 * Description:
391 * See @blk_run_queue. This variant must be called with the queue lock
392 * held and interrupts disabled.
393 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 */
Nick Piggin75ad23b2008-04-29 14:48:33 +0200395void __blk_run_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 blk_remove_plug(q);
Jens Axboedac07ec2006-05-11 08:20:16 +0200398
Tejun Heoa538cd02009-04-23 11:05:17 +0900399 if (unlikely(blk_queue_stopped(q)))
400 return;
401
402 if (elv_queue_empty(q))
403 return;
404
Jens Axboedac07ec2006-05-11 08:20:16 +0200405 /*
406 * Only recurse once to avoid overrunning the stack, let the unplug
407 * handling reinvoke the handler shortly if we already got there.
408 */
Tejun Heoa538cd02009-04-23 11:05:17 +0900409 if (!queue_flag_test_and_set(QUEUE_FLAG_REENTER, q)) {
410 q->request_fn(q);
411 queue_flag_clear(QUEUE_FLAG_REENTER, q);
412 } else {
413 queue_flag_set(QUEUE_FLAG_PLUGGED, q);
414 kblockd_schedule_work(q, &q->unplug_work);
415 }
Nick Piggin75ad23b2008-04-29 14:48:33 +0200416}
417EXPORT_SYMBOL(__blk_run_queue);
Jens Axboedac07ec2006-05-11 08:20:16 +0200418
Nick Piggin75ad23b2008-04-29 14:48:33 +0200419/**
420 * blk_run_queue - run a single device queue
421 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200422 *
423 * Description:
424 * Invoke request handling on this queue, if it has pending work to do.
Tejun Heoa7f55792009-04-23 11:05:17 +0900425 * May be used to restart queueing when a request has completed.
Nick Piggin75ad23b2008-04-29 14:48:33 +0200426 */
427void blk_run_queue(struct request_queue *q)
428{
429 unsigned long flags;
430
431 spin_lock_irqsave(q->queue_lock, flags);
432 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 spin_unlock_irqrestore(q->queue_lock, flags);
434}
435EXPORT_SYMBOL(blk_run_queue);
436
Jens Axboe165125e2007-07-24 09:28:11 +0200437void blk_put_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500438{
439 kobject_put(&q->kobj);
440}
Al Viro483f4af2006-03-18 18:34:37 -0500441
Jens Axboe6728cb02008-01-31 13:03:55 +0100442void blk_cleanup_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500443{
Jens Axboee3335de2008-09-18 09:22:54 -0700444 /*
445 * We know we have process context here, so we can be a little
446 * cautious and ensure that pending block actions on this device
447 * are done before moving on. Going into this function, we should
448 * not have processes doing IO to this device.
449 */
450 blk_sync_queue(q);
451
Al Viro483f4af2006-03-18 18:34:37 -0500452 mutex_lock(&q->sysfs_lock);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200453 queue_flag_set_unlocked(QUEUE_FLAG_DEAD, q);
Al Viro483f4af2006-03-18 18:34:37 -0500454 mutex_unlock(&q->sysfs_lock);
455
456 if (q->elevator)
457 elevator_exit(q->elevator);
458
459 blk_put_queue(q);
460}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461EXPORT_SYMBOL(blk_cleanup_queue);
462
Jens Axboe165125e2007-07-24 09:28:11 +0200463static int blk_init_free_list(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 struct request_list *rl = &q->rq;
466
Jens Axboe1faa16d2009-04-06 14:48:01 +0200467 rl->count[BLK_RW_SYNC] = rl->count[BLK_RW_ASYNC] = 0;
468 rl->starved[BLK_RW_SYNC] = rl->starved[BLK_RW_ASYNC] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200469 rl->elvpriv = 0;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200470 init_waitqueue_head(&rl->wait[BLK_RW_SYNC]);
471 init_waitqueue_head(&rl->wait[BLK_RW_ASYNC]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Christoph Lameter19460892005-06-23 00:08:19 -0700473 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
474 mempool_free_slab, request_cachep, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 if (!rl->rq_pool)
477 return -ENOMEM;
478
479 return 0;
480}
481
Jens Axboe165125e2007-07-24 09:28:11 +0200482struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
Christoph Lameter19460892005-06-23 00:08:19 -0700484 return blk_alloc_queue_node(gfp_mask, -1);
485}
486EXPORT_SYMBOL(blk_alloc_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Jens Axboe165125e2007-07-24 09:28:11 +0200488struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -0700489{
Jens Axboe165125e2007-07-24 09:28:11 +0200490 struct request_queue *q;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700491 int err;
Christoph Lameter19460892005-06-23 00:08:19 -0700492
Jens Axboe8324aa92008-01-29 14:51:59 +0100493 q = kmem_cache_alloc_node(blk_requestq_cachep,
Christoph Lameter94f60302007-07-17 04:03:29 -0700494 gfp_mask | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (!q)
496 return NULL;
497
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700498 q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
499 q->backing_dev_info.unplug_io_data = q;
Jens Axboe0989a022009-06-12 14:42:56 +0200500 q->backing_dev_info.ra_pages =
501 (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
502 q->backing_dev_info.state = 0;
503 q->backing_dev_info.capabilities = BDI_CAP_MAP_COPY;
504
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700505 err = bdi_init(&q->backing_dev_info);
506 if (err) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100507 kmem_cache_free(blk_requestq_cachep, q);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700508 return NULL;
509 }
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 init_timer(&q->unplug_timer);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700512 setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
513 INIT_LIST_HEAD(&q->timeout_list);
Peter Zijlstra713ada92008-10-16 13:44:57 +0200514 INIT_WORK(&q->unplug_work, blk_unplug_work);
Al Viro483f4af2006-03-18 18:34:37 -0500515
Jens Axboe8324aa92008-01-29 14:51:59 +0100516 kobject_init(&q->kobj, &blk_queue_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Al Viro483f4af2006-03-18 18:34:37 -0500518 mutex_init(&q->sysfs_lock);
Neil Browne7e72bf2008-05-14 16:05:54 -0700519 spin_lock_init(&q->__queue_lock);
Al Viro483f4af2006-03-18 18:34:37 -0500520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return q;
522}
Christoph Lameter19460892005-06-23 00:08:19 -0700523EXPORT_SYMBOL(blk_alloc_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525/**
526 * blk_init_queue - prepare a request queue for use with a block device
527 * @rfn: The function to be called to process requests that have been
528 * placed on the queue.
529 * @lock: Request queue spin lock
530 *
531 * Description:
532 * If a block device wishes to use the standard request handling procedures,
533 * which sorts requests and coalesces adjacent requests, then it must
534 * call blk_init_queue(). The function @rfn will be called when there
535 * are requests on the queue that need to be processed. If the device
536 * supports plugging, then @rfn may not be called immediately when requests
537 * are available on the queue, but may be called at some time later instead.
538 * Plugged queues are generally unplugged when a buffer belonging to one
539 * of the requests on the queue is needed, or due to memory pressure.
540 *
541 * @rfn is not required, or even expected, to remove all requests off the
542 * queue, but only as many as it can handle at a time. If it does leave
543 * requests on the queue, it is responsible for arranging that the requests
544 * get dealt with eventually.
545 *
546 * The queue spin lock must be held while manipulating the requests on the
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200547 * request queue; this lock will be taken also from interrupt context, so irq
548 * disabling is needed for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 *
Randy Dunlap710027a2008-08-19 20:13:11 +0200550 * Function returns a pointer to the initialized request queue, or %NULL if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 * it didn't succeed.
552 *
553 * Note:
554 * blk_init_queue() must be paired with a blk_cleanup_queue() call
555 * when the block device is deactivated (such as at module unload).
556 **/
Christoph Lameter19460892005-06-23 00:08:19 -0700557
Jens Axboe165125e2007-07-24 09:28:11 +0200558struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Christoph Lameter19460892005-06-23 00:08:19 -0700560 return blk_init_queue_node(rfn, lock, -1);
561}
562EXPORT_SYMBOL(blk_init_queue);
563
Jens Axboe165125e2007-07-24 09:28:11 +0200564struct request_queue *
Christoph Lameter19460892005-06-23 00:08:19 -0700565blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
566{
Jens Axboe165125e2007-07-24 09:28:11 +0200567 struct request_queue *q = blk_alloc_queue_node(GFP_KERNEL, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 if (!q)
570 return NULL;
571
Christoph Lameter19460892005-06-23 00:08:19 -0700572 q->node = node_id;
Al Viro8669aaf2006-03-18 13:50:00 -0500573 if (blk_init_free_list(q)) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100574 kmem_cache_free(blk_requestq_cachep, q);
Al Viro8669aaf2006-03-18 13:50:00 -0500575 return NULL;
576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 q->request_fn = rfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 q->prep_rq_fn = NULL;
580 q->unplug_fn = generic_unplug_device;
Jens Axboebc58ba92009-01-23 10:54:44 +0100581 q->queue_flags = QUEUE_FLAG_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 q->queue_lock = lock;
583
Jens Axboef3b144a2009-03-06 08:48:33 +0100584 /*
585 * This also sets hw/phys segments, boundary and size
586 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 blk_queue_make_request(q, __make_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Alan Stern44ec9542007-02-20 11:01:57 -0500589 q->sg_reserved_size = INT_MAX;
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 /*
592 * all done
593 */
594 if (!elevator_init(q, NULL)) {
595 blk_queue_congestion_threshold(q);
596 return q;
597 }
598
Al Viro8669aaf2006-03-18 13:50:00 -0500599 blk_put_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 return NULL;
601}
Christoph Lameter19460892005-06-23 00:08:19 -0700602EXPORT_SYMBOL(blk_init_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Jens Axboe165125e2007-07-24 09:28:11 +0200604int blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Nick Pigginfde6ad22005-06-23 00:08:53 -0700606 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
Al Viro483f4af2006-03-18 18:34:37 -0500607 kobject_get(&q->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return 0;
609 }
610
611 return 1;
612}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Jens Axboe165125e2007-07-24 09:28:11 +0200614static inline void blk_free_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Jens Axboe4aff5e22006-08-10 08:44:47 +0200616 if (rq->cmd_flags & REQ_ELVPRIV)
Tejun Heocb98fc82005-10-28 08:29:39 +0200617 elv_put_request(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 mempool_free(rq, q->rq.rq_pool);
619}
620
Jens Axboe1ea25ecb2006-07-18 22:24:11 +0200621static struct request *
Jerome Marchand42dad762009-04-22 14:01:49 +0200622blk_alloc_request(struct request_queue *q, int flags, int priv, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
624 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
625
626 if (!rq)
627 return NULL;
628
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200629 blk_rq_init(q, rq);
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200630
Jerome Marchand42dad762009-04-22 14:01:49 +0200631 rq->cmd_flags = flags | REQ_ALLOCED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Tejun Heocb98fc82005-10-28 08:29:39 +0200633 if (priv) {
Jens Axboecb78b282006-07-28 09:32:57 +0200634 if (unlikely(elv_set_request(q, rq, gfp_mask))) {
Tejun Heocb98fc82005-10-28 08:29:39 +0200635 mempool_free(rq, q->rq.rq_pool);
636 return NULL;
637 }
Jens Axboe4aff5e22006-08-10 08:44:47 +0200638 rq->cmd_flags |= REQ_ELVPRIV;
Tejun Heocb98fc82005-10-28 08:29:39 +0200639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Tejun Heocb98fc82005-10-28 08:29:39 +0200641 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
644/*
645 * ioc_batching returns true if the ioc is a valid batching request and
646 * should be given priority access to a request.
647 */
Jens Axboe165125e2007-07-24 09:28:11 +0200648static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
650 if (!ioc)
651 return 0;
652
653 /*
654 * Make sure the process is able to allocate at least 1 request
655 * even if the batch times out, otherwise we could theoretically
656 * lose wakeups.
657 */
658 return ioc->nr_batch_requests == q->nr_batching ||
659 (ioc->nr_batch_requests > 0
660 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
661}
662
663/*
664 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
665 * will cause the process to be a "batcher" on all queues in the system. This
666 * is the behaviour we want though - once it gets a wakeup it should be given
667 * a nice run.
668 */
Jens Axboe165125e2007-07-24 09:28:11 +0200669static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
671 if (!ioc || ioc_batching(q, ioc))
672 return;
673
674 ioc->nr_batch_requests = q->nr_batching;
675 ioc->last_waited = jiffies;
676}
677
Jens Axboe1faa16d2009-04-06 14:48:01 +0200678static void __freed_request(struct request_queue *q, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
680 struct request_list *rl = &q->rq;
681
Jens Axboe1faa16d2009-04-06 14:48:01 +0200682 if (rl->count[sync] < queue_congestion_off_threshold(q))
683 blk_clear_queue_congested(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Jens Axboe1faa16d2009-04-06 14:48:01 +0200685 if (rl->count[sync] + 1 <= q->nr_requests) {
686 if (waitqueue_active(&rl->wait[sync]))
687 wake_up(&rl->wait[sync]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Jens Axboe1faa16d2009-04-06 14:48:01 +0200689 blk_clear_queue_full(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
691}
692
693/*
694 * A request has just been released. Account for it, update the full and
695 * congestion status, wake up any waiters. Called under q->queue_lock.
696 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200697static void freed_request(struct request_queue *q, int sync, int priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
699 struct request_list *rl = &q->rq;
700
Jens Axboe1faa16d2009-04-06 14:48:01 +0200701 rl->count[sync]--;
Tejun Heocb98fc82005-10-28 08:29:39 +0200702 if (priv)
703 rl->elvpriv--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Jens Axboe1faa16d2009-04-06 14:48:01 +0200705 __freed_request(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Jens Axboe1faa16d2009-04-06 14:48:01 +0200707 if (unlikely(rl->starved[sync ^ 1]))
708 __freed_request(q, sync ^ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711/*
Nick Piggind6344532005-06-28 20:45:14 -0700712 * Get a free request, queue_lock must be held.
713 * Returns NULL on failure, with queue_lock held.
714 * Returns !NULL on success, with queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 */
Jens Axboe165125e2007-07-24 09:28:11 +0200716static struct request *get_request(struct request_queue *q, int rw_flags,
Jens Axboe7749a8d2006-12-13 13:02:26 +0100717 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
719 struct request *rq = NULL;
720 struct request_list *rl = &q->rq;
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100721 struct io_context *ioc = NULL;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200722 const bool is_sync = rw_is_sync(rw_flags) != 0;
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100723 int may_queue, priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Jens Axboe7749a8d2006-12-13 13:02:26 +0100725 may_queue = elv_may_queue(q, rw_flags);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100726 if (may_queue == ELV_MQUEUE_NO)
727 goto rq_starved;
728
Jens Axboe1faa16d2009-04-06 14:48:01 +0200729 if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) {
730 if (rl->count[is_sync]+1 >= q->nr_requests) {
Jens Axboeb5deef92006-07-19 23:39:40 +0200731 ioc = current_io_context(GFP_ATOMIC, q->node);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100732 /*
733 * The queue will fill after this allocation, so set
734 * it as full, and mark this process as "batching".
735 * This process will be allowed to complete a batch of
736 * requests, others will be blocked.
737 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200738 if (!blk_queue_full(q, is_sync)) {
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100739 ioc_set_batching(q, ioc);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200740 blk_set_queue_full(q, is_sync);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100741 } else {
742 if (may_queue != ELV_MQUEUE_MUST
743 && !ioc_batching(q, ioc)) {
744 /*
745 * The queue is full and the allocating
746 * process is not a "batcher", and not
747 * exempted by the IO scheduler
748 */
749 goto out;
750 }
751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
Jens Axboe1faa16d2009-04-06 14:48:01 +0200753 blk_set_queue_congested(q, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
755
Jens Axboe082cf692005-06-28 16:35:11 +0200756 /*
757 * Only allow batching queuers to allocate up to 50% over the defined
758 * limit of requests, otherwise we could have thousands of requests
759 * allocated with any setting of ->nr_requests
760 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200761 if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
Jens Axboe082cf692005-06-28 16:35:11 +0200762 goto out;
Hugh Dickinsfd782a42005-06-29 15:15:40 +0100763
Jens Axboe1faa16d2009-04-06 14:48:01 +0200764 rl->count[is_sync]++;
765 rl->starved[is_sync] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200766
Jens Axboe64521d12005-10-28 08:30:39 +0200767 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Tejun Heocb98fc82005-10-28 08:29:39 +0200768 if (priv)
769 rl->elvpriv++;
770
Jerome Marchand42dad762009-04-22 14:01:49 +0200771 if (blk_queue_io_stat(q))
772 rw_flags |= REQ_IO_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 spin_unlock_irq(q->queue_lock);
774
Jens Axboe7749a8d2006-12-13 13:02:26 +0100775 rq = blk_alloc_request(q, rw_flags, priv, gfp_mask);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100776 if (unlikely(!rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 /*
778 * Allocation failed presumably due to memory. Undo anything
779 * we might have messed up.
780 *
781 * Allocating task should really be put onto the front of the
782 * wait queue, but this is pretty rare.
783 */
784 spin_lock_irq(q->queue_lock);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200785 freed_request(q, is_sync, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 /*
788 * in the very unlikely event that allocation failed and no
789 * requests for this direction was pending, mark us starved
790 * so that freeing of a request in the other direction will
791 * notice us. another possible fix would be to split the
792 * rq mempool into READ and WRITE
793 */
794rq_starved:
Jens Axboe1faa16d2009-04-06 14:48:01 +0200795 if (unlikely(rl->count[is_sync] == 0))
796 rl->starved[is_sync] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 goto out;
799 }
800
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100801 /*
802 * ioc may be NULL here, and ioc_batching will be false. That's
803 * OK, if the queue is under the request limit then requests need
804 * not count toward the nr_batch_requests limit. There will always
805 * be some limit enforced by BLK_BATCH_TIME.
806 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (ioc_batching(q, ioc))
808 ioc->nr_batch_requests--;
Jens Axboe6728cb02008-01-31 13:03:55 +0100809
Jens Axboe1faa16d2009-04-06 14:48:01 +0200810 trace_block_getrq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return rq;
813}
814
815/*
816 * No available requests for this queue, unplug the device and wait for some
817 * requests to become available.
Nick Piggind6344532005-06-28 20:45:14 -0700818 *
819 * Called with q->queue_lock held, and returns with it unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 */
Jens Axboe165125e2007-07-24 09:28:11 +0200821static struct request *get_request_wait(struct request_queue *q, int rw_flags,
Jens Axboe22e2c502005-06-27 10:55:12 +0200822 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Jens Axboe1faa16d2009-04-06 14:48:01 +0200824 const bool is_sync = rw_is_sync(rw_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 struct request *rq;
826
Jens Axboe7749a8d2006-12-13 13:02:26 +0100827 rq = get_request(q, rw_flags, bio, GFP_NOIO);
Nick Piggin450991b2005-06-28 20:45:13 -0700828 while (!rq) {
829 DEFINE_WAIT(wait);
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200830 struct io_context *ioc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 struct request_list *rl = &q->rq;
832
Jens Axboe1faa16d2009-04-06 14:48:01 +0200833 prepare_to_wait_exclusive(&rl->wait[is_sync], &wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 TASK_UNINTERRUPTIBLE);
835
Jens Axboe1faa16d2009-04-06 14:48:01 +0200836 trace_block_sleeprq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200838 __generic_unplug_device(q);
839 spin_unlock_irq(q->queue_lock);
840 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200842 /*
843 * After sleeping, we become a "batching" process and
844 * will be able to allocate at least one request, and
845 * up to a big batch of them for a small period time.
846 * See ioc_batching, ioc_set_batching
847 */
848 ioc = current_io_context(GFP_NOIO, q->node);
849 ioc_set_batching(q, ioc);
Jens Axboe2056a782006-03-23 20:00:26 +0100850
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200851 spin_lock_irq(q->queue_lock);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200852 finish_wait(&rl->wait[is_sync], &wait);
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200853
854 rq = get_request(q, rw_flags, bio, GFP_NOIO);
855 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 return rq;
858}
859
Jens Axboe165125e2007-07-24 09:28:11 +0200860struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
862 struct request *rq;
863
864 BUG_ON(rw != READ && rw != WRITE);
865
Nick Piggind6344532005-06-28 20:45:14 -0700866 spin_lock_irq(q->queue_lock);
867 if (gfp_mask & __GFP_WAIT) {
Jens Axboe22e2c502005-06-27 10:55:12 +0200868 rq = get_request_wait(q, rw, NULL);
Nick Piggind6344532005-06-28 20:45:14 -0700869 } else {
Jens Axboe22e2c502005-06-27 10:55:12 +0200870 rq = get_request(q, rw, NULL, gfp_mask);
Nick Piggind6344532005-06-28 20:45:14 -0700871 if (!rq)
872 spin_unlock_irq(q->queue_lock);
873 }
874 /* q->queue_lock is unlocked at this point */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 return rq;
877}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878EXPORT_SYMBOL(blk_get_request);
879
880/**
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300881 * blk_make_request - given a bio, allocate a corresponding struct request.
Randy Dunlap8ebf9752009-06-11 20:00:41 -0700882 * @q: target request queue
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300883 * @bio: The bio describing the memory mappings that will be submitted for IO.
884 * It may be a chained-bio properly constructed by block/bio layer.
Randy Dunlap8ebf9752009-06-11 20:00:41 -0700885 * @gfp_mask: gfp flags to be used for memory allocation
Jens Axboedc72ef42006-07-20 14:54:05 +0200886 *
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300887 * blk_make_request is the parallel of generic_make_request for BLOCK_PC
888 * type commands. Where the struct request needs to be farther initialized by
889 * the caller. It is passed a &struct bio, which describes the memory info of
890 * the I/O transfer.
891 *
892 * The caller of blk_make_request must make sure that bi_io_vec
893 * are set to describe the memory buffers. That bio_data_dir() will return
894 * the needed direction of the request. (And all bio's in the passed bio-chain
895 * are properly set accordingly)
896 *
897 * If called under none-sleepable conditions, mapped bio buffers must not
898 * need bouncing, by calling the appropriate masked or flagged allocator,
899 * suitable for the target device. Otherwise the call to blk_queue_bounce will
900 * BUG.
Jens Axboe53674ac2009-05-19 19:52:35 +0200901 *
902 * WARNING: When allocating/cloning a bio-chain, careful consideration should be
903 * given to how you allocate bios. In particular, you cannot use __GFP_WAIT for
904 * anything but the first bio in the chain. Otherwise you risk waiting for IO
905 * completion of a bio that hasn't been submitted yet, thus resulting in a
906 * deadlock. Alternatively bios should be allocated using bio_kmalloc() instead
907 * of bio_alloc(), as that avoids the mempool deadlock.
908 * If possible a big IO should be split into smaller parts when allocation
909 * fails. Partial allocation should not be an error, or you risk a live-lock.
Jens Axboedc72ef42006-07-20 14:54:05 +0200910 */
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300911struct request *blk_make_request(struct request_queue *q, struct bio *bio,
912 gfp_t gfp_mask)
Jens Axboedc72ef42006-07-20 14:54:05 +0200913{
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300914 struct request *rq = blk_get_request(q, bio_data_dir(bio), gfp_mask);
915
916 if (unlikely(!rq))
917 return ERR_PTR(-ENOMEM);
918
919 for_each_bio(bio) {
920 struct bio *bounce_bio = bio;
921 int ret;
922
923 blk_queue_bounce(q, &bounce_bio);
924 ret = blk_rq_append_bio(q, rq, bounce_bio);
925 if (unlikely(ret)) {
926 blk_put_request(rq);
927 return ERR_PTR(ret);
928 }
929 }
930
931 return rq;
Jens Axboedc72ef42006-07-20 14:54:05 +0200932}
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300933EXPORT_SYMBOL(blk_make_request);
Jens Axboedc72ef42006-07-20 14:54:05 +0200934
935/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 * blk_requeue_request - put a request back on queue
937 * @q: request queue where request should be inserted
938 * @rq: request to be inserted
939 *
940 * Description:
941 * Drivers often keep queueing requests until the hardware cannot accept
942 * more, when that condition happens we need to put the request back
943 * on the queue. Must be called with queue lock held.
944 */
Jens Axboe165125e2007-07-24 09:28:11 +0200945void blk_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
Jens Axboe242f9dc2008-09-14 05:55:09 -0700947 blk_delete_timer(rq);
948 blk_clear_rq_complete(rq);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100949 trace_block_rq_requeue(q, rq);
Jens Axboe2056a782006-03-23 20:00:26 +0100950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (blk_rq_tagged(rq))
952 blk_queue_end_tag(q, rq);
953
James Bottomleyba396a62009-05-27 14:17:08 +0200954 BUG_ON(blk_queued_rq(rq));
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 elv_requeue_request(q, rq);
957}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958EXPORT_SYMBOL(blk_requeue_request);
959
960/**
Randy Dunlap710027a2008-08-19 20:13:11 +0200961 * blk_insert_request - insert a special request into a request queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 * @q: request queue where request should be inserted
963 * @rq: request to be inserted
964 * @at_head: insert request at head or tail of queue
965 * @data: private data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 *
967 * Description:
968 * Many block devices need to execute commands asynchronously, so they don't
969 * block the whole kernel from preemption during request execution. This is
970 * accomplished normally by inserting aritficial requests tagged as
Randy Dunlap710027a2008-08-19 20:13:11 +0200971 * REQ_TYPE_SPECIAL in to the corresponding request queue, and letting them
972 * be scheduled for actual execution by the request queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 *
974 * We have the option of inserting the head or the tail of the queue.
975 * Typically we use the tail for new ioctls and so forth. We use the head
976 * of the queue for things like a QUEUE_FULL message from a device, or a
977 * host that is unable to accept a particular command.
978 */
Jens Axboe165125e2007-07-24 09:28:11 +0200979void blk_insert_request(struct request_queue *q, struct request *rq,
Tejun Heo 867d1192005-04-24 02:06:05 -0500980 int at_head, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
Tejun Heo 867d1192005-04-24 02:06:05 -0500982 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 unsigned long flags;
984
985 /*
986 * tell I/O scheduler that this isn't a regular read/write (ie it
987 * must not attempt merges on this) and that it acts as a soft
988 * barrier
989 */
Jens Axboe4aff5e22006-08-10 08:44:47 +0200990 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 rq->special = data;
993
994 spin_lock_irqsave(q->queue_lock, flags);
995
996 /*
997 * If command is tagged, release the tag
998 */
Tejun Heo 867d1192005-04-24 02:06:05 -0500999 if (blk_rq_tagged(rq))
1000 blk_queue_end_tag(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Jerome Marchandb238b3d2007-10-23 15:05:46 +02001002 drive_stat_acct(rq, 1);
Tejun Heo 867d1192005-04-24 02:06:05 -05001003 __elv_add_request(q, rq, where, 0);
Tejun Heoa7f55792009-04-23 11:05:17 +09001004 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 spin_unlock_irqrestore(q->queue_lock, flags);
1006}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007EXPORT_SYMBOL(blk_insert_request);
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009/*
1010 * add-request adds a request to the linked list.
1011 * queue lock is held and interrupts disabled, as we muck with the
1012 * request queue list.
1013 */
Jens Axboe6728cb02008-01-31 13:03:55 +01001014static inline void add_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
Jerome Marchandb238b3d2007-10-23 15:05:46 +02001016 drive_stat_acct(req, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 /*
1019 * elevator indicated where it wants this request to be
1020 * inserted at elevator_merge time
1021 */
1022 __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
1023}
Jens Axboe6728cb02008-01-31 13:03:55 +01001024
Tejun Heo074a7ac2008-08-25 19:56:14 +09001025static void part_round_stats_single(int cpu, struct hd_struct *part,
1026 unsigned long now)
1027{
1028 if (now == part->stamp)
1029 return;
1030
1031 if (part->in_flight) {
1032 __part_stat_add(cpu, part, time_in_queue,
1033 part->in_flight * (now - part->stamp));
1034 __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
1035 }
1036 part->stamp = now;
1037}
1038
1039/**
Randy Dunlap496aa8a2008-10-16 07:46:23 +02001040 * part_round_stats() - Round off the performance stats on a struct disk_stats.
1041 * @cpu: cpu number for stats access
1042 * @part: target partition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 *
1044 * The average IO queue length and utilisation statistics are maintained
1045 * by observing the current state of the queue length and the amount of
1046 * time it has been in this state for.
1047 *
1048 * Normally, that accounting is done on IO completion, but that can result
1049 * in more than a second's worth of IO being accounted for within any one
1050 * second, leading to >100% utilisation. To deal with that, we call this
1051 * function to do a round-off before returning the results when reading
1052 * /proc/diskstats. This accounts immediately for all queue usage up to
1053 * the current jiffies and restarts the counters again.
1054 */
Tejun Heoc9959052008-08-25 19:47:21 +09001055void part_round_stats(int cpu, struct hd_struct *part)
Jerome Marchand6f2576af2008-02-08 11:04:35 +01001056{
1057 unsigned long now = jiffies;
1058
Tejun Heo074a7ac2008-08-25 19:56:14 +09001059 if (part->partno)
1060 part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
1061 part_round_stats_single(cpu, part, now);
Jerome Marchand6f2576af2008-02-08 11:04:35 +01001062}
Tejun Heo074a7ac2008-08-25 19:56:14 +09001063EXPORT_SYMBOL_GPL(part_round_stats);
Jerome Marchand6f2576af2008-02-08 11:04:35 +01001064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065/*
1066 * queue lock must be held
1067 */
Jens Axboe165125e2007-07-24 09:28:11 +02001068void __blk_put_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if (unlikely(!q))
1071 return;
1072 if (unlikely(--req->ref_count))
1073 return;
1074
Tejun Heo8922e162005-10-20 16:23:44 +02001075 elv_completed_request(q, req);
1076
Boaz Harrosh1cd96c22009-03-24 12:35:07 +01001077 /* this is a bio leak */
1078 WARN_ON(req->bio != NULL);
1079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 /*
1081 * Request may not have originated from ll_rw_blk. if not,
1082 * it didn't come out of our reserved rq pools
1083 */
Jens Axboe49171e52006-08-10 08:59:11 +02001084 if (req->cmd_flags & REQ_ALLOCED) {
Jens Axboe1faa16d2009-04-06 14:48:01 +02001085 int is_sync = rq_is_sync(req) != 0;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001086 int priv = req->cmd_flags & REQ_ELVPRIV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 BUG_ON(!list_empty(&req->queuelist));
Jens Axboe98170642006-07-28 09:23:08 +02001089 BUG_ON(!hlist_unhashed(&req->hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091 blk_free_request(q, req);
Jens Axboe1faa16d2009-04-06 14:48:01 +02001092 freed_request(q, is_sync, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 }
1094}
Mike Christie6e39b692005-11-11 05:30:24 -06001095EXPORT_SYMBOL_GPL(__blk_put_request);
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097void blk_put_request(struct request *req)
1098{
Tejun Heo8922e162005-10-20 16:23:44 +02001099 unsigned long flags;
Jens Axboe165125e2007-07-24 09:28:11 +02001100 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
FUJITA Tomonori52a93ba2008-07-15 21:21:45 +02001102 spin_lock_irqsave(q->queue_lock, flags);
1103 __blk_put_request(q, req);
1104 spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106EXPORT_SYMBOL(blk_put_request);
1107
Jens Axboe86db1e22008-01-29 14:53:40 +01001108void init_request_from_bio(struct request *req, struct bio *bio)
Tejun Heo52d9e672006-01-06 09:49:58 +01001109{
Jens Axboec7c22e42008-09-13 20:26:01 +02001110 req->cpu = bio->bi_comp_cpu;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001111 req->cmd_type = REQ_TYPE_FS;
Tejun Heo52d9e672006-01-06 09:49:58 +01001112
1113 /*
1114 * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
1115 */
Mike Christie6000a362008-08-19 18:45:30 -05001116 if (bio_rw_ahead(bio))
1117 req->cmd_flags |= (REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
1118 REQ_FAILFAST_DRIVER);
1119 if (bio_failfast_dev(bio))
1120 req->cmd_flags |= REQ_FAILFAST_DEV;
1121 if (bio_failfast_transport(bio))
1122 req->cmd_flags |= REQ_FAILFAST_TRANSPORT;
1123 if (bio_failfast_driver(bio))
1124 req->cmd_flags |= REQ_FAILFAST_DRIVER;
Tejun Heo52d9e672006-01-06 09:49:58 +01001125
David Woodhousefb2dce82008-08-05 18:01:53 +01001126 if (unlikely(bio_discard(bio))) {
David Woodhousee17fc0a2008-08-09 16:42:20 +01001127 req->cmd_flags |= REQ_DISCARD;
1128 if (bio_barrier(bio))
1129 req->cmd_flags |= REQ_SOFTBARRIER;
David Woodhousefb2dce82008-08-05 18:01:53 +01001130 req->q->prepare_discard_fn(req->q, req);
David Woodhousee17fc0a2008-08-09 16:42:20 +01001131 } else if (unlikely(bio_barrier(bio)))
Tejun Heoe4025f62009-04-23 11:05:17 +09001132 req->cmd_flags |= REQ_HARDBARRIER;
Tejun Heo52d9e672006-01-06 09:49:58 +01001133
Jens Axboeb31dc662006-06-13 08:26:10 +02001134 if (bio_sync(bio))
Jens Axboe4aff5e22006-08-10 08:44:47 +02001135 req->cmd_flags |= REQ_RW_SYNC;
Jens Axboe5404bc72006-08-10 09:01:02 +02001136 if (bio_rw_meta(bio))
1137 req->cmd_flags |= REQ_RW_META;
Jens Axboeaeb6faf2009-04-06 14:48:07 +02001138 if (bio_noidle(bio))
1139 req->cmd_flags |= REQ_NOIDLE;
Jens Axboeb31dc662006-06-13 08:26:10 +02001140
Tejun Heo52d9e672006-01-06 09:49:58 +01001141 req->errors = 0;
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001142 req->__sector = bio->bi_sector;
Tejun Heo52d9e672006-01-06 09:49:58 +01001143 req->ioprio = bio_prio(bio);
NeilBrownbc1c56f2007-08-16 13:31:30 +02001144 blk_rq_bio_prep(req->q, req, bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001145}
1146
Jens Axboe644b2d92009-04-06 14:48:06 +02001147/*
1148 * Only disabling plugging for non-rotational devices if it does tagging
1149 * as well, otherwise we do need the proper merging
1150 */
1151static inline bool queue_should_plug(struct request_queue *q)
1152{
1153 return !(blk_queue_nonrot(q) && blk_queue_tagged(q));
1154}
1155
Jens Axboe165125e2007-07-24 09:28:11 +02001156static int __make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
Nick Piggin450991b2005-06-28 20:45:13 -07001158 struct request *req;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001159 int el_ret;
1160 unsigned int bytes = bio->bi_size;
Jens Axboe51da90f2006-07-18 04:14:45 +02001161 const unsigned short prio = bio_prio(bio);
1162 const int sync = bio_sync(bio);
Jens Axboe213d9412009-01-06 09:16:05 +01001163 const int unplug = bio_unplug(bio);
Jens Axboe7749a8d2006-12-13 13:02:26 +01001164 int rw_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
NeilBrowndb64f682009-06-30 09:35:44 +02001166 if (bio_barrier(bio) && bio_has_data(bio) &&
1167 (q->next_ordered == QUEUE_ORDERED_NONE)) {
1168 bio_endio(bio, -EOPNOTSUPP);
1169 return 0;
1170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 /*
1172 * low level driver can indicate that it wants pages above a
1173 * certain limit bounced to low memory (ie for highmem, or even
1174 * ISA dma in theory)
1175 */
1176 blk_queue_bounce(q, &bio);
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 spin_lock_irq(q->queue_lock);
1179
Tejun Heoa7384672008-11-28 13:32:03 +09001180 if (unlikely(bio_barrier(bio)) || elv_queue_empty(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 goto get_rq;
1182
1183 el_ret = elv_merge(q, &req, bio);
1184 switch (el_ret) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001185 case ELEVATOR_BACK_MERGE:
1186 BUG_ON(!rq_mergeable(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Jens Axboe6728cb02008-01-31 13:03:55 +01001188 if (!ll_back_merge_fn(q, req, bio))
1189 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001191 trace_block_bio_backmerge(q, bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001192
Jens Axboe6728cb02008-01-31 13:03:55 +01001193 req->biotail->bi_next = bio;
1194 req->biotail = bio;
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001195 req->__data_len += bytes;
Jens Axboe6728cb02008-01-31 13:03:55 +01001196 req->ioprio = ioprio_best(req->ioprio, prio);
Jens Axboeab780f12008-08-26 10:25:02 +02001197 if (!blk_rq_cpu_valid(req))
1198 req->cpu = bio->bi_comp_cpu;
Jens Axboe6728cb02008-01-31 13:03:55 +01001199 drive_stat_acct(req, 0);
1200 if (!attempt_back_merge(q, req))
1201 elv_merged_request(q, req, el_ret);
1202 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Jens Axboe6728cb02008-01-31 13:03:55 +01001204 case ELEVATOR_FRONT_MERGE:
1205 BUG_ON(!rq_mergeable(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Jens Axboe6728cb02008-01-31 13:03:55 +01001207 if (!ll_front_merge_fn(q, req, bio))
1208 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001210 trace_block_bio_frontmerge(q, bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001211
Jens Axboe6728cb02008-01-31 13:03:55 +01001212 bio->bi_next = req->bio;
1213 req->bio = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Jens Axboe6728cb02008-01-31 13:03:55 +01001215 /*
1216 * may not be valid. if the low level driver said
1217 * it didn't need a bounce buffer then it better
1218 * not touch req->buffer either...
1219 */
1220 req->buffer = bio_data(bio);
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001221 req->__sector = bio->bi_sector;
1222 req->__data_len += bytes;
Jens Axboe6728cb02008-01-31 13:03:55 +01001223 req->ioprio = ioprio_best(req->ioprio, prio);
Jens Axboeab780f12008-08-26 10:25:02 +02001224 if (!blk_rq_cpu_valid(req))
1225 req->cpu = bio->bi_comp_cpu;
Jens Axboe6728cb02008-01-31 13:03:55 +01001226 drive_stat_acct(req, 0);
1227 if (!attempt_front_merge(q, req))
1228 elv_merged_request(q, req, el_ret);
1229 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Jens Axboe6728cb02008-01-31 13:03:55 +01001231 /* ELV_NO_MERGE: elevator says don't/can't merge. */
1232 default:
1233 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
1235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236get_rq:
Nick Piggin450991b2005-06-28 20:45:13 -07001237 /*
Jens Axboe7749a8d2006-12-13 13:02:26 +01001238 * This sync check and mask will be re-done in init_request_from_bio(),
1239 * but we need to set it earlier to expose the sync flag to the
1240 * rq allocator and io schedulers.
1241 */
1242 rw_flags = bio_data_dir(bio);
1243 if (sync)
1244 rw_flags |= REQ_RW_SYNC;
1245
1246 /*
Nick Piggin450991b2005-06-28 20:45:13 -07001247 * Grab a free request. This is might sleep but can not fail.
Nick Piggind6344532005-06-28 20:45:14 -07001248 * Returns with the queue unlocked.
Nick Piggin450991b2005-06-28 20:45:13 -07001249 */
Jens Axboe7749a8d2006-12-13 13:02:26 +01001250 req = get_request_wait(q, rw_flags, bio);
Nick Piggind6344532005-06-28 20:45:14 -07001251
Nick Piggin450991b2005-06-28 20:45:13 -07001252 /*
1253 * After dropping the lock and possibly sleeping here, our request
1254 * may now be mergeable after it had proven unmergeable (above).
1255 * We don't worry about that case for efficiency. It won't happen
1256 * often, and the elevators are able to handle it.
1257 */
Tejun Heo52d9e672006-01-06 09:49:58 +01001258 init_request_from_bio(req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Nick Piggin450991b2005-06-28 20:45:13 -07001260 spin_lock_irq(q->queue_lock);
Jens Axboec7c22e42008-09-13 20:26:01 +02001261 if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags) ||
1262 bio_flagged(bio, BIO_CPU_AFFINE))
1263 req->cpu = blk_cpu_to_group(smp_processor_id());
Jens Axboe644b2d92009-04-06 14:48:06 +02001264 if (queue_should_plug(q) && elv_queue_empty(q))
Nick Piggin450991b2005-06-28 20:45:13 -07001265 blk_plug_device(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 add_request(q, req);
1267out:
Jens Axboe644b2d92009-04-06 14:48:06 +02001268 if (unplug || !queue_should_plug(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 __generic_unplug_device(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 spin_unlock_irq(q->queue_lock);
1271 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
1273
1274/*
1275 * If bio->bi_dev is a partition, remap the location
1276 */
1277static inline void blk_partition_remap(struct bio *bio)
1278{
1279 struct block_device *bdev = bio->bi_bdev;
1280
Jens Axboebf2de6f2007-09-27 13:01:25 +02001281 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 struct hd_struct *p = bdev->bd_part;
Jens Axboea3623572005-11-01 09:26:16 +01001283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 bio->bi_sector += p->start_sect;
1285 bio->bi_bdev = bdev->bd_contains;
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001286
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001287 trace_block_remap(bdev_get_queue(bio->bi_bdev), bio,
Alan D. Brunelle22a7c312009-05-04 16:35:08 -04001288 bdev->bd_dev,
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001289 bio->bi_sector - p->start_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 }
1291}
1292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293static void handle_bad_sector(struct bio *bio)
1294{
1295 char b[BDEVNAME_SIZE];
1296
1297 printk(KERN_INFO "attempt to access beyond end of device\n");
1298 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1299 bdevname(bio->bi_bdev, b),
1300 bio->bi_rw,
1301 (unsigned long long)bio->bi_sector + bio_sectors(bio),
1302 (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
1303
1304 set_bit(BIO_EOF, &bio->bi_flags);
1305}
1306
Akinobu Mitac17bb492006-12-08 02:39:46 -08001307#ifdef CONFIG_FAIL_MAKE_REQUEST
1308
1309static DECLARE_FAULT_ATTR(fail_make_request);
1310
1311static int __init setup_fail_make_request(char *str)
1312{
1313 return setup_fault_attr(&fail_make_request, str);
1314}
1315__setup("fail_make_request=", setup_fail_make_request);
1316
1317static int should_fail_request(struct bio *bio)
1318{
Tejun Heoeddb2e22008-08-25 19:56:13 +09001319 struct hd_struct *part = bio->bi_bdev->bd_part;
1320
1321 if (part_to_disk(part)->part0.make_it_fail || part->make_it_fail)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001322 return should_fail(&fail_make_request, bio->bi_size);
1323
1324 return 0;
1325}
1326
1327static int __init fail_make_request_debugfs(void)
1328{
1329 return init_fault_attr_dentries(&fail_make_request,
1330 "fail_make_request");
1331}
1332
1333late_initcall(fail_make_request_debugfs);
1334
1335#else /* CONFIG_FAIL_MAKE_REQUEST */
1336
1337static inline int should_fail_request(struct bio *bio)
1338{
1339 return 0;
1340}
1341
1342#endif /* CONFIG_FAIL_MAKE_REQUEST */
1343
Jens Axboec07e2b42007-07-18 13:27:58 +02001344/*
1345 * Check whether this bio extends beyond the end of the device.
1346 */
1347static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1348{
1349 sector_t maxsector;
1350
1351 if (!nr_sectors)
1352 return 0;
1353
1354 /* Test device or partition size, when known. */
1355 maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
1356 if (maxsector) {
1357 sector_t sector = bio->bi_sector;
1358
1359 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1360 /*
1361 * This may well happen - the kernel calls bread()
1362 * without checking the size of the device, e.g., when
1363 * mounting a device.
1364 */
1365 handle_bad_sector(bio);
1366 return 1;
1367 }
1368 }
1369
1370 return 0;
1371}
1372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001374 * generic_make_request - hand a buffer to its device driver for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 * @bio: The bio describing the location in memory and on the device.
1376 *
1377 * generic_make_request() is used to make I/O requests of block
1378 * devices. It is passed a &struct bio, which describes the I/O that needs
1379 * to be done.
1380 *
1381 * generic_make_request() does not return any status. The
1382 * success/failure status of the request, along with notification of
1383 * completion, is delivered asynchronously through the bio->bi_end_io
1384 * function described (one day) else where.
1385 *
1386 * The caller of generic_make_request must make sure that bi_io_vec
1387 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1388 * set to describe the device address, and the
1389 * bi_end_io and optionally bi_private are set to describe how
1390 * completion notification should be signaled.
1391 *
1392 * generic_make_request and the drivers it calls may use bi_next if this
1393 * bio happens to be merged with someone else, and may change bi_dev and
1394 * bi_sector for remaps as it sees fit. So the values of these fields
1395 * should NOT be depended on after the call to generic_make_request.
1396 */
Neil Brownd89d8792007-05-01 09:53:42 +02001397static inline void __generic_make_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
Jens Axboe165125e2007-07-24 09:28:11 +02001399 struct request_queue *q;
NeilBrown5ddfe962006-10-30 22:07:21 -08001400 sector_t old_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 int ret, nr_sectors = bio_sectors(bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001402 dev_t old_dev;
Jens Axboe51fd77b2007-11-02 08:49:08 +01001403 int err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Jens Axboec07e2b42007-07-18 13:27:58 +02001407 if (bio_check_eod(bio, nr_sectors))
1408 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 /*
1411 * Resolve the mapping until finished. (drivers are
1412 * still free to implement/resolve their own stacking
1413 * by explicitly returning 0)
1414 *
1415 * NOTE: we don't repeat the blk_size check for each new device.
1416 * Stacking drivers are expected to know what they are doing.
1417 */
NeilBrown5ddfe962006-10-30 22:07:21 -08001418 old_sector = -1;
Jens Axboe2056a782006-03-23 20:00:26 +01001419 old_dev = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 do {
1421 char b[BDEVNAME_SIZE];
1422
1423 q = bdev_get_queue(bio->bi_bdev);
Tejun Heoa7384672008-11-28 13:32:03 +09001424 if (unlikely(!q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 printk(KERN_ERR
1426 "generic_make_request: Trying to access "
1427 "nonexistent block-device %s (%Lu)\n",
1428 bdevname(bio->bi_bdev, b),
1429 (long long) bio->bi_sector);
Tejun Heoa7384672008-11-28 13:32:03 +09001430 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 }
1432
Martin K. Petersenae03bf62009-05-22 17:17:50 -04001433 if (unlikely(nr_sectors > queue_max_hw_sectors(q))) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001434 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
Martin K. Petersenae03bf62009-05-22 17:17:50 -04001435 bdevname(bio->bi_bdev, b),
1436 bio_sectors(bio),
1437 queue_max_hw_sectors(q));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 goto end_io;
1439 }
1440
Nick Pigginfde6ad22005-06-23 00:08:53 -07001441 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 goto end_io;
1443
Akinobu Mitac17bb492006-12-08 02:39:46 -08001444 if (should_fail_request(bio))
1445 goto end_io;
1446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 /*
1448 * If this device has partitions, remap block n
1449 * of partition p to block n+start(p) of the disk.
1450 */
1451 blk_partition_remap(bio);
1452
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +02001453 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio))
1454 goto end_io;
1455
NeilBrown5ddfe962006-10-30 22:07:21 -08001456 if (old_sector != -1)
Alan D. Brunelle22a7c312009-05-04 16:35:08 -04001457 trace_block_remap(q, bio, old_dev, old_sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001458
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001459 trace_block_bio_queue(q, bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001460
NeilBrown5ddfe962006-10-30 22:07:21 -08001461 old_sector = bio->bi_sector;
Jens Axboe2056a782006-03-23 20:00:26 +01001462 old_dev = bio->bi_bdev->bd_dev;
1463
Jens Axboec07e2b42007-07-18 13:27:58 +02001464 if (bio_check_eod(bio, nr_sectors))
1465 goto end_io;
Tejun Heoa7384672008-11-28 13:32:03 +09001466
1467 if (bio_discard(bio) && !q->prepare_discard_fn) {
Jens Axboe51fd77b2007-11-02 08:49:08 +01001468 err = -EOPNOTSUPP;
1469 goto end_io;
1470 }
NeilBrown5ddfe962006-10-30 22:07:21 -08001471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 ret = q->make_request_fn(q, bio);
1473 } while (ret);
Tejun Heoa7384672008-11-28 13:32:03 +09001474
1475 return;
1476
1477end_io:
1478 bio_endio(bio, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479}
1480
Neil Brownd89d8792007-05-01 09:53:42 +02001481/*
1482 * We only want one ->make_request_fn to be active at a time,
1483 * else stack usage with stacked devices could be a problem.
1484 * So use current->bio_{list,tail} to keep a list of requests
1485 * submited by a make_request_fn function.
1486 * current->bio_tail is also used as a flag to say if
1487 * generic_make_request is currently active in this task or not.
1488 * If it is NULL, then no make_request is active. If it is non-NULL,
1489 * then a make_request is active, and new requests should be added
1490 * at the tail
1491 */
1492void generic_make_request(struct bio *bio)
1493{
1494 if (current->bio_tail) {
1495 /* make_request is active */
1496 *(current->bio_tail) = bio;
1497 bio->bi_next = NULL;
1498 current->bio_tail = &bio->bi_next;
1499 return;
1500 }
1501 /* following loop may be a bit non-obvious, and so deserves some
1502 * explanation.
1503 * Before entering the loop, bio->bi_next is NULL (as all callers
1504 * ensure that) so we have a list with a single bio.
1505 * We pretend that we have just taken it off a longer list, so
1506 * we assign bio_list to the next (which is NULL) and bio_tail
1507 * to &bio_list, thus initialising the bio_list of new bios to be
1508 * added. __generic_make_request may indeed add some more bios
1509 * through a recursive call to generic_make_request. If it
1510 * did, we find a non-NULL value in bio_list and re-enter the loop
1511 * from the top. In this case we really did just take the bio
1512 * of the top of the list (no pretending) and so fixup bio_list and
1513 * bio_tail or bi_next, and call into __generic_make_request again.
1514 *
1515 * The loop was structured like this to make only one call to
1516 * __generic_make_request (which is important as it is large and
1517 * inlined) and to keep the structure simple.
1518 */
1519 BUG_ON(bio->bi_next);
1520 do {
1521 current->bio_list = bio->bi_next;
1522 if (bio->bi_next == NULL)
1523 current->bio_tail = &current->bio_list;
1524 else
1525 bio->bi_next = NULL;
1526 __generic_make_request(bio);
1527 bio = current->bio_list;
1528 } while (bio);
1529 current->bio_tail = NULL; /* deactivate */
1530}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531EXPORT_SYMBOL(generic_make_request);
1532
1533/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001534 * submit_bio - submit a bio to the block device layer for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1536 * @bio: The &struct bio which describes the I/O
1537 *
1538 * submit_bio() is very similar in purpose to generic_make_request(), and
1539 * uses that function to do most of the work. Both are fairly rough
Randy Dunlap710027a2008-08-19 20:13:11 +02001540 * interfaces; @bio must be presetup and ready for I/O.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 *
1542 */
1543void submit_bio(int rw, struct bio *bio)
1544{
1545 int count = bio_sectors(bio);
1546
Jens Axboe22e2c502005-06-27 10:55:12 +02001547 bio->bi_rw |= rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Jens Axboebf2de6f2007-09-27 13:01:25 +02001549 /*
1550 * If it's a regular read/write or a barrier with data attached,
1551 * go through the normal accounting stuff before submission.
1552 */
Jens Axboea9c701e2008-08-08 11:04:44 +02001553 if (bio_has_data(bio)) {
Jens Axboebf2de6f2007-09-27 13:01:25 +02001554 if (rw & WRITE) {
1555 count_vm_events(PGPGOUT, count);
1556 } else {
1557 task_io_account_read(bio->bi_size);
1558 count_vm_events(PGPGIN, count);
1559 }
1560
1561 if (unlikely(block_dump)) {
1562 char b[BDEVNAME_SIZE];
1563 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001564 current->comm, task_pid_nr(current),
Jens Axboebf2de6f2007-09-27 13:01:25 +02001565 (rw & WRITE) ? "WRITE" : "READ",
1566 (unsigned long long)bio->bi_sector,
Jens Axboe6728cb02008-01-31 13:03:55 +01001567 bdevname(bio->bi_bdev, b));
Jens Axboebf2de6f2007-09-27 13:01:25 +02001568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 }
1570
1571 generic_make_request(bio);
1572}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573EXPORT_SYMBOL(submit_bio);
1574
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001575/**
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001576 * blk_rq_check_limits - Helper function to check a request for the queue limit
1577 * @q: the queue
1578 * @rq: the request being checked
1579 *
1580 * Description:
1581 * @rq may have been made based on weaker limitations of upper-level queues
1582 * in request stacking drivers, and it may violate the limitation of @q.
1583 * Since the block layer and the underlying device driver trust @rq
1584 * after it is inserted to @q, it should be checked against @q before
1585 * the insertion using this generic function.
1586 *
1587 * This function should also be useful for request stacking drivers
1588 * in some cases below, so export this fuction.
1589 * Request stacking drivers like request-based dm may change the queue
1590 * limits while requests are in the queue (e.g. dm's table swapping).
1591 * Such request stacking drivers should check those requests agaist
1592 * the new queue limits again when they dispatch those requests,
1593 * although such checkings are also done against the old queue limits
1594 * when submitting requests.
1595 */
1596int blk_rq_check_limits(struct request_queue *q, struct request *rq)
1597{
Martin K. Petersenae03bf62009-05-22 17:17:50 -04001598 if (blk_rq_sectors(rq) > queue_max_sectors(q) ||
1599 blk_rq_bytes(rq) > queue_max_hw_sectors(q) << 9) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001600 printk(KERN_ERR "%s: over max size limit.\n", __func__);
1601 return -EIO;
1602 }
1603
1604 /*
1605 * queue's settings related to segment counting like q->bounce_pfn
1606 * may differ from that of other stacking queues.
1607 * Recalculate it to check the request correctly on this queue's
1608 * limitation.
1609 */
1610 blk_recalc_rq_segments(rq);
Martin K. Petersenae03bf62009-05-22 17:17:50 -04001611 if (rq->nr_phys_segments > queue_max_phys_segments(q) ||
1612 rq->nr_phys_segments > queue_max_hw_segments(q)) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001613 printk(KERN_ERR "%s: over max segments limit.\n", __func__);
1614 return -EIO;
1615 }
1616
1617 return 0;
1618}
1619EXPORT_SYMBOL_GPL(blk_rq_check_limits);
1620
1621/**
1622 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
1623 * @q: the queue to submit the request
1624 * @rq: the request being queued
1625 */
1626int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
1627{
1628 unsigned long flags;
1629
1630 if (blk_rq_check_limits(q, rq))
1631 return -EIO;
1632
1633#ifdef CONFIG_FAIL_MAKE_REQUEST
1634 if (rq->rq_disk && rq->rq_disk->part0.make_it_fail &&
1635 should_fail(&fail_make_request, blk_rq_bytes(rq)))
1636 return -EIO;
1637#endif
1638
1639 spin_lock_irqsave(q->queue_lock, flags);
1640
1641 /*
1642 * Submitting request must be dequeued before calling this function
1643 * because it will be linked to another request_queue
1644 */
1645 BUG_ON(blk_queued_rq(rq));
1646
1647 drive_stat_acct(rq, 1);
1648 __elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 0);
1649
1650 spin_unlock_irqrestore(q->queue_lock, flags);
1651
1652 return 0;
1653}
1654EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
1655
Jens Axboebc58ba92009-01-23 10:54:44 +01001656static void blk_account_io_completion(struct request *req, unsigned int bytes)
1657{
Jens Axboec2553b52009-04-24 08:10:11 +02001658 if (blk_do_io_stat(req)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01001659 const int rw = rq_data_dir(req);
1660 struct hd_struct *part;
1661 int cpu;
1662
1663 cpu = part_stat_lock();
Tejun Heo83096eb2009-05-07 22:24:39 +09001664 part = disk_map_sector_rcu(req->rq_disk, blk_rq_pos(req));
Jens Axboebc58ba92009-01-23 10:54:44 +01001665 part_stat_add(cpu, part, sectors[rw], bytes >> 9);
1666 part_stat_unlock();
1667 }
1668}
1669
1670static void blk_account_io_done(struct request *req)
1671{
Jens Axboebc58ba92009-01-23 10:54:44 +01001672 /*
1673 * Account IO completion. bar_rq isn't accounted as a normal
1674 * IO on queueing nor completion. Accounting the containing
1675 * request is enough.
1676 */
Jens Axboec2553b52009-04-24 08:10:11 +02001677 if (blk_do_io_stat(req) && req != &req->q->bar_rq) {
Jens Axboebc58ba92009-01-23 10:54:44 +01001678 unsigned long duration = jiffies - req->start_time;
1679 const int rw = rq_data_dir(req);
1680 struct hd_struct *part;
1681 int cpu;
1682
1683 cpu = part_stat_lock();
Tejun Heo83096eb2009-05-07 22:24:39 +09001684 part = disk_map_sector_rcu(req->rq_disk, blk_rq_pos(req));
Jens Axboebc58ba92009-01-23 10:54:44 +01001685
1686 part_stat_inc(cpu, part, ios[rw]);
1687 part_stat_add(cpu, part, ticks[rw], duration);
1688 part_round_stats(cpu, part);
1689 part_dec_in_flight(part);
1690
1691 part_stat_unlock();
1692 }
1693}
1694
Tejun Heo53a08802008-12-03 12:41:26 +01001695/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09001696 * blk_peek_request - peek at the top of a request queue
1697 * @q: request queue to peek at
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001698 *
1699 * Description:
Tejun Heo9934c8c2009-05-08 11:54:16 +09001700 * Return the request at the top of @q. The returned request
1701 * should be started using blk_start_request() before LLD starts
1702 * processing it.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001703 *
1704 * Return:
Tejun Heo9934c8c2009-05-08 11:54:16 +09001705 * Pointer to the request at the top of @q if available. Null
1706 * otherwise.
1707 *
1708 * Context:
1709 * queue_lock must be held.
1710 */
1711struct request *blk_peek_request(struct request_queue *q)
Tejun Heo158dbda2009-04-23 11:05:18 +09001712{
1713 struct request *rq;
1714 int ret;
1715
1716 while ((rq = __elv_next_request(q)) != NULL) {
1717 if (!(rq->cmd_flags & REQ_STARTED)) {
1718 /*
1719 * This is the first time the device driver
1720 * sees this request (possibly after
1721 * requeueing). Notify IO scheduler.
1722 */
1723 if (blk_sorted_rq(rq))
1724 elv_activate_rq(q, rq);
1725
1726 /*
1727 * just mark as started even if we don't start
1728 * it, a request that has been delayed should
1729 * not be passed by new incoming requests
1730 */
1731 rq->cmd_flags |= REQ_STARTED;
1732 trace_block_rq_issue(q, rq);
1733 }
1734
1735 if (!q->boundary_rq || q->boundary_rq == rq) {
1736 q->end_sector = rq_end_sector(rq);
1737 q->boundary_rq = NULL;
1738 }
1739
1740 if (rq->cmd_flags & REQ_DONTPREP)
1741 break;
1742
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001743 if (q->dma_drain_size && blk_rq_bytes(rq)) {
Tejun Heo158dbda2009-04-23 11:05:18 +09001744 /*
1745 * make sure space for the drain appears we
1746 * know we can do this because max_hw_segments
1747 * has been adjusted to be one fewer than the
1748 * device can handle
1749 */
1750 rq->nr_phys_segments++;
1751 }
1752
1753 if (!q->prep_rq_fn)
1754 break;
1755
1756 ret = q->prep_rq_fn(q, rq);
1757 if (ret == BLKPREP_OK) {
1758 break;
1759 } else if (ret == BLKPREP_DEFER) {
1760 /*
1761 * the request may have been (partially) prepped.
1762 * we need to keep this request in the front to
1763 * avoid resource deadlock. REQ_STARTED will
1764 * prevent other fs requests from passing this one.
1765 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001766 if (q->dma_drain_size && blk_rq_bytes(rq) &&
Tejun Heo158dbda2009-04-23 11:05:18 +09001767 !(rq->cmd_flags & REQ_DONTPREP)) {
1768 /*
1769 * remove the space for the drain we added
1770 * so that we don't add it again
1771 */
1772 --rq->nr_phys_segments;
1773 }
1774
1775 rq = NULL;
1776 break;
1777 } else if (ret == BLKPREP_KILL) {
1778 rq->cmd_flags |= REQ_QUIET;
James Bottomleyc143dc92009-05-30 06:43:49 +02001779 /*
1780 * Mark this request as started so we don't trigger
1781 * any debug logic in the end I/O path.
1782 */
1783 blk_start_request(rq);
Tejun Heo40cbbb72009-04-23 11:05:19 +09001784 __blk_end_request_all(rq, -EIO);
Tejun Heo158dbda2009-04-23 11:05:18 +09001785 } else {
1786 printk(KERN_ERR "%s: bad return=%d\n", __func__, ret);
1787 break;
1788 }
1789 }
1790
1791 return rq;
1792}
Tejun Heo9934c8c2009-05-08 11:54:16 +09001793EXPORT_SYMBOL(blk_peek_request);
Tejun Heo158dbda2009-04-23 11:05:18 +09001794
Tejun Heo9934c8c2009-05-08 11:54:16 +09001795void blk_dequeue_request(struct request *rq)
Tejun Heo158dbda2009-04-23 11:05:18 +09001796{
Tejun Heo9934c8c2009-05-08 11:54:16 +09001797 struct request_queue *q = rq->q;
1798
Tejun Heo158dbda2009-04-23 11:05:18 +09001799 BUG_ON(list_empty(&rq->queuelist));
1800 BUG_ON(ELV_ON_HASH(rq));
1801
1802 list_del_init(&rq->queuelist);
1803
1804 /*
1805 * the time frame between a request being removed from the lists
1806 * and to it is freed is accounted as io that is in progress at
1807 * the driver side.
1808 */
1809 if (blk_account_rq(rq))
Jens Axboe0a7ae2f2009-05-20 08:54:31 +02001810 q->in_flight[rq_is_sync(rq)]++;
Tejun Heo158dbda2009-04-23 11:05:18 +09001811}
1812
Tejun Heo5efccd12009-04-23 11:05:18 +09001813/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09001814 * blk_start_request - start request processing on the driver
1815 * @req: request to dequeue
1816 *
1817 * Description:
1818 * Dequeue @req and start timeout timer on it. This hands off the
1819 * request to the driver.
1820 *
1821 * Block internal functions which don't want to start timer should
1822 * call blk_dequeue_request().
1823 *
1824 * Context:
1825 * queue_lock must be held.
1826 */
1827void blk_start_request(struct request *req)
1828{
1829 blk_dequeue_request(req);
1830
1831 /*
Tejun Heo5f49f632009-05-19 18:33:05 +09001832 * We are now handing the request to the hardware, initialize
1833 * resid_len to full count and add the timeout handler.
Tejun Heo9934c8c2009-05-08 11:54:16 +09001834 */
Tejun Heo5f49f632009-05-19 18:33:05 +09001835 req->resid_len = blk_rq_bytes(req);
FUJITA Tomonoridbb66c42009-06-09 05:47:10 +02001836 if (unlikely(blk_bidi_rq(req)))
1837 req->next_rq->resid_len = blk_rq_bytes(req->next_rq);
1838
Tejun Heo9934c8c2009-05-08 11:54:16 +09001839 blk_add_timer(req);
1840}
1841EXPORT_SYMBOL(blk_start_request);
1842
1843/**
1844 * blk_fetch_request - fetch a request from a request queue
1845 * @q: request queue to fetch a request from
1846 *
1847 * Description:
1848 * Return the request at the top of @q. The request is started on
1849 * return and LLD can start processing it immediately.
1850 *
1851 * Return:
1852 * Pointer to the request at the top of @q if available. Null
1853 * otherwise.
1854 *
1855 * Context:
1856 * queue_lock must be held.
1857 */
1858struct request *blk_fetch_request(struct request_queue *q)
1859{
1860 struct request *rq;
1861
1862 rq = blk_peek_request(q);
1863 if (rq)
1864 blk_start_request(rq);
1865 return rq;
1866}
1867EXPORT_SYMBOL(blk_fetch_request);
1868
1869/**
Tejun Heo2e60e022009-04-23 11:05:18 +09001870 * blk_update_request - Special helper function for request stacking drivers
Randy Dunlap8ebf9752009-06-11 20:00:41 -07001871 * @req: the request being processed
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001872 * @error: %0 for success, < %0 for error
Randy Dunlap8ebf9752009-06-11 20:00:41 -07001873 * @nr_bytes: number of bytes to complete @req
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001874 *
1875 * Description:
Randy Dunlap8ebf9752009-06-11 20:00:41 -07001876 * Ends I/O on a number of bytes attached to @req, but doesn't complete
1877 * the request structure even if @req doesn't have leftover.
1878 * If @req has leftover, sets it up for the next range of segments.
Tejun Heo2e60e022009-04-23 11:05:18 +09001879 *
1880 * This special helper function is only for request stacking drivers
1881 * (e.g. request-based dm) so that they can handle partial completion.
1882 * Actual device drivers should use blk_end_request instead.
1883 *
1884 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
1885 * %false return from this function.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001886 *
1887 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09001888 * %false - this request doesn't have any more data
1889 * %true - this request has more data
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001890 **/
Tejun Heo2e60e022009-04-23 11:05:18 +09001891bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001893 int total_bytes, bio_nbytes, next_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 struct bio *bio;
1895
Tejun Heo2e60e022009-04-23 11:05:18 +09001896 if (!req->bio)
1897 return false;
1898
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001899 trace_block_rq_complete(req->q, req);
Jens Axboe2056a782006-03-23 20:00:26 +01001900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 /*
Tejun Heo6f414692009-04-19 07:00:41 +09001902 * For fs requests, rq is just carrier of independent bio's
1903 * and each partial completion should be handled separately.
1904 * Reset per-request error on each partial completion.
1905 *
1906 * TODO: tj: This is too subtle. It would be better to let
1907 * low level drivers do what they see fit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 */
Tejun Heo6f414692009-04-19 07:00:41 +09001909 if (blk_fs_request(req))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 req->errors = 0;
1911
Jens Axboe6728cb02008-01-31 13:03:55 +01001912 if (error && (blk_fs_request(req) && !(req->cmd_flags & REQ_QUIET))) {
1913 printk(KERN_ERR "end_request: I/O error, dev %s, sector %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 req->rq_disk ? req->rq_disk->disk_name : "?",
Tejun Heo83096eb2009-05-07 22:24:39 +09001915 (unsigned long long)blk_rq_pos(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 }
1917
Jens Axboebc58ba92009-01-23 10:54:44 +01001918 blk_account_io_completion(req, nr_bytes);
Jens Axboed72d9042005-11-01 08:35:42 +01001919
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 total_bytes = bio_nbytes = 0;
1921 while ((bio = req->bio) != NULL) {
1922 int nbytes;
1923
1924 if (nr_bytes >= bio->bi_size) {
1925 req->bio = bio->bi_next;
1926 nbytes = bio->bi_size;
NeilBrown5bb23a62007-09-27 12:46:13 +02001927 req_bio_endio(req, bio, nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 next_idx = 0;
1929 bio_nbytes = 0;
1930 } else {
1931 int idx = bio->bi_idx + next_idx;
1932
Kazuhisa Ichikawaaf498d72009-05-12 13:27:45 +02001933 if (unlikely(idx >= bio->bi_vcnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 blk_dump_rq_flags(req, "__end_that");
Jens Axboe6728cb02008-01-31 13:03:55 +01001935 printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
Kazuhisa Ichikawaaf498d72009-05-12 13:27:45 +02001936 __func__, idx, bio->bi_vcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 break;
1938 }
1939
1940 nbytes = bio_iovec_idx(bio, idx)->bv_len;
1941 BIO_BUG_ON(nbytes > bio->bi_size);
1942
1943 /*
1944 * not a complete bvec done
1945 */
1946 if (unlikely(nbytes > nr_bytes)) {
1947 bio_nbytes += nr_bytes;
1948 total_bytes += nr_bytes;
1949 break;
1950 }
1951
1952 /*
1953 * advance to the next vector
1954 */
1955 next_idx++;
1956 bio_nbytes += nbytes;
1957 }
1958
1959 total_bytes += nbytes;
1960 nr_bytes -= nbytes;
1961
Jens Axboe6728cb02008-01-31 13:03:55 +01001962 bio = req->bio;
1963 if (bio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 /*
1965 * end more in this run, or just return 'not-done'
1966 */
1967 if (unlikely(nr_bytes <= 0))
1968 break;
1969 }
1970 }
1971
1972 /*
1973 * completely done
1974 */
Tejun Heo2e60e022009-04-23 11:05:18 +09001975 if (!req->bio) {
1976 /*
1977 * Reset counters so that the request stacking driver
1978 * can find how many bytes remain in the request
1979 * later.
1980 */
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001981 req->__data_len = 0;
Tejun Heo2e60e022009-04-23 11:05:18 +09001982 return false;
1983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
1985 /*
1986 * if the request wasn't completed, update state
1987 */
1988 if (bio_nbytes) {
NeilBrown5bb23a62007-09-27 12:46:13 +02001989 req_bio_endio(req, bio, bio_nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 bio->bi_idx += next_idx;
1991 bio_iovec(bio)->bv_offset += nr_bytes;
1992 bio_iovec(bio)->bv_len -= nr_bytes;
1993 }
1994
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001995 req->__data_len -= total_bytes;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001996 req->buffer = bio_data(req->bio);
1997
1998 /* update sector only for requests with clear definition of sector */
1999 if (blk_fs_request(req) || blk_discard_rq(req))
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002000 req->__sector += total_bytes >> 9;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002001
2002 /*
2003 * If total number of sectors is less than the first segment
2004 * size, something has gone terribly wrong.
2005 */
2006 if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
2007 printk(KERN_ERR "blk: request botched\n");
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002008 req->__data_len = blk_rq_cur_bytes(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002009 }
2010
2011 /* recalculate the number of segments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 blk_recalc_rq_segments(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002013
Tejun Heo2e60e022009-04-23 11:05:18 +09002014 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015}
Tejun Heo2e60e022009-04-23 11:05:18 +09002016EXPORT_SYMBOL_GPL(blk_update_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Tejun Heo2e60e022009-04-23 11:05:18 +09002018static bool blk_update_bidi_request(struct request *rq, int error,
2019 unsigned int nr_bytes,
2020 unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002021{
Tejun Heo2e60e022009-04-23 11:05:18 +09002022 if (blk_update_request(rq, error, nr_bytes))
2023 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002024
Tejun Heo2e60e022009-04-23 11:05:18 +09002025 /* Bidi request must be completed as a whole */
2026 if (unlikely(blk_bidi_rq(rq)) &&
2027 blk_update_request(rq->next_rq, error, bidi_bytes))
2028 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002029
Tejun Heo2e60e022009-04-23 11:05:18 +09002030 add_disk_randomness(rq->rq_disk);
2031
2032 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033}
2034
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035/*
2036 * queue lock must be held
2037 */
Tejun Heo2e60e022009-04-23 11:05:18 +09002038static void blk_finish_request(struct request *req, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002040 if (blk_rq_tagged(req))
2041 blk_queue_end_tag(req->q, req);
2042
James Bottomleyba396a62009-05-27 14:17:08 +02002043 BUG_ON(blk_queued_rq(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
2045 if (unlikely(laptop_mode) && blk_fs_request(req))
2046 laptop_io_completion();
2047
Mike Andersone78042e52008-10-30 02:16:20 -07002048 blk_delete_timer(req);
2049
Jens Axboebc58ba92009-01-23 10:54:44 +01002050 blk_account_io_done(req);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002051
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 if (req->end_io)
Tejun Heo8ffdc652006-01-06 09:49:03 +01002053 req->end_io(req, error);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002054 else {
2055 if (blk_bidi_rq(req))
2056 __blk_put_request(req->next_rq->q, req->next_rq);
2057
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 __blk_put_request(req->q, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 }
2060}
2061
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05002062/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002063 * blk_end_bidi_request - Complete a bidi request
2064 * @rq: the request to complete
Randy Dunlap710027a2008-08-19 20:13:11 +02002065 * @error: %0 for success, < %0 for error
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002066 * @nr_bytes: number of bytes to complete @rq
2067 * @bidi_bytes: number of bytes to complete @rq->next_rq
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002068 *
2069 * Description:
2070 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
Tejun Heo2e60e022009-04-23 11:05:18 +09002071 * Drivers that supports bidi can safely call this member for any
2072 * type of request, bidi or uni. In the later case @bidi_bytes is
2073 * just ignored.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002074 *
2075 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002076 * %false - we are done with this request
2077 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002078 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002079static bool blk_end_bidi_request(struct request *rq, int error,
2080 unsigned int nr_bytes, unsigned int bidi_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002081{
2082 struct request_queue *q = rq->q;
Tejun Heo2e60e022009-04-23 11:05:18 +09002083 unsigned long flags;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002084
Tejun Heo2e60e022009-04-23 11:05:18 +09002085 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2086 return true;
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002087
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002088 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo2e60e022009-04-23 11:05:18 +09002089 blk_finish_request(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002090 spin_unlock_irqrestore(q->queue_lock, flags);
2091
Tejun Heo2e60e022009-04-23 11:05:18 +09002092 return false;
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002093}
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002094
2095/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002096 * __blk_end_bidi_request - Complete a bidi request with queue lock held
2097 * @rq: the request to complete
2098 * @error: %0 for success, < %0 for error
2099 * @nr_bytes: number of bytes to complete @rq
2100 * @bidi_bytes: number of bytes to complete @rq->next_rq
Tejun Heo5efccd12009-04-23 11:05:18 +09002101 *
2102 * Description:
Tejun Heo2e60e022009-04-23 11:05:18 +09002103 * Identical to blk_end_bidi_request() except that queue lock is
2104 * assumed to be locked on entry and remains so on return.
Tejun Heo5efccd12009-04-23 11:05:18 +09002105 *
Tejun Heo2e60e022009-04-23 11:05:18 +09002106 * Return:
2107 * %false - we are done with this request
2108 * %true - still buffers pending for this request
Tejun Heo5efccd12009-04-23 11:05:18 +09002109 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002110static bool __blk_end_bidi_request(struct request *rq, int error,
2111 unsigned int nr_bytes, unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002112{
Tejun Heo2e60e022009-04-23 11:05:18 +09002113 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2114 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002115
Tejun Heo2e60e022009-04-23 11:05:18 +09002116 blk_finish_request(rq, error);
Tejun Heo5efccd12009-04-23 11:05:18 +09002117
Tejun Heo2e60e022009-04-23 11:05:18 +09002118 return false;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002119}
2120
2121/**
2122 * blk_end_request - Helper function for drivers to complete the request.
2123 * @rq: the request being processed
2124 * @error: %0 for success, < %0 for error
2125 * @nr_bytes: number of bytes to complete
2126 *
2127 * Description:
2128 * Ends I/O on a number of bytes attached to @rq.
2129 * If @rq has leftover, sets it up for the next range of segments.
2130 *
2131 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002132 * %false - we are done with this request
2133 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002134 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002135bool blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002136{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002137 return blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002138}
Jens Axboe56ad1742009-07-28 22:11:24 +02002139EXPORT_SYMBOL(blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002140
2141/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002142 * blk_end_request_all - Helper function for drives to finish the request.
2143 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002144 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002145 *
2146 * Description:
2147 * Completely finish @rq.
2148 */
2149void blk_end_request_all(struct request *rq, int error)
2150{
2151 bool pending;
2152 unsigned int bidi_bytes = 0;
2153
2154 if (unlikely(blk_bidi_rq(rq)))
2155 bidi_bytes = blk_rq_bytes(rq->next_rq);
2156
2157 pending = blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2158 BUG_ON(pending);
2159}
Jens Axboe56ad1742009-07-28 22:11:24 +02002160EXPORT_SYMBOL(blk_end_request_all);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002161
2162/**
2163 * blk_end_request_cur - Helper function to finish the current request chunk.
2164 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002165 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002166 *
2167 * Description:
2168 * Complete the current consecutively mapped chunk from @rq.
2169 *
2170 * Return:
2171 * %false - we are done with this request
2172 * %true - still buffers pending for this request
2173 */
2174bool blk_end_request_cur(struct request *rq, int error)
2175{
2176 return blk_end_request(rq, error, blk_rq_cur_bytes(rq));
2177}
Jens Axboe56ad1742009-07-28 22:11:24 +02002178EXPORT_SYMBOL(blk_end_request_cur);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002179
2180/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002181 * __blk_end_request - Helper function for drivers to complete the request.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002182 * @rq: the request being processed
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002183 * @error: %0 for success, < %0 for error
2184 * @nr_bytes: number of bytes to complete
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002185 *
2186 * Description:
2187 * Must be called with queue lock held unlike blk_end_request().
2188 *
2189 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002190 * %false - we are done with this request
2191 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002192 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002193bool __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002194{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002195 return __blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002196}
Jens Axboe56ad1742009-07-28 22:11:24 +02002197EXPORT_SYMBOL(__blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002198
2199/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002200 * __blk_end_request_all - Helper function for drives to finish the request.
2201 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002202 * @error: %0 for success, < %0 for error
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002203 *
2204 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002205 * Completely finish @rq. Must be called with queue lock held.
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002206 */
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002207void __blk_end_request_all(struct request *rq, int error)
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002208{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002209 bool pending;
2210 unsigned int bidi_bytes = 0;
2211
2212 if (unlikely(blk_bidi_rq(rq)))
2213 bidi_bytes = blk_rq_bytes(rq->next_rq);
2214
2215 pending = __blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2216 BUG_ON(pending);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002217}
Jens Axboe56ad1742009-07-28 22:11:24 +02002218EXPORT_SYMBOL(__blk_end_request_all);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002219
2220/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002221 * __blk_end_request_cur - Helper function to finish the current request chunk.
2222 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002223 * @error: %0 for success, < %0 for error
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002224 *
2225 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002226 * Complete the current consecutively mapped chunk from @rq. Must
2227 * be called with queue lock held.
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002228 *
2229 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002230 * %false - we are done with this request
2231 * %true - still buffers pending for this request
2232 */
2233bool __blk_end_request_cur(struct request *rq, int error)
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002234{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002235 return __blk_end_request(rq, error, blk_rq_cur_bytes(rq));
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002236}
Jens Axboe56ad1742009-07-28 22:11:24 +02002237EXPORT_SYMBOL(__blk_end_request_cur);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002238
Jens Axboe86db1e22008-01-29 14:53:40 +01002239void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2240 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241{
David Woodhoused628eae2008-08-09 16:22:17 +01002242 /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw, and
2243 we want BIO_RW_AHEAD (bit 1) to imply REQ_FAILFAST (bit 1). */
Jens Axboe4aff5e22006-08-10 08:44:47 +02002244 rq->cmd_flags |= (bio->bi_rw & 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
David Woodhousefb2dce82008-08-05 18:01:53 +01002246 if (bio_has_data(bio)) {
2247 rq->nr_phys_segments = bio_phys_segments(q, bio);
David Woodhousefb2dce82008-08-05 18:01:53 +01002248 rq->buffer = bio_data(bio);
2249 }
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002250 rq->__data_len = bio->bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 rq->bio = rq->biotail = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
NeilBrown66846572007-08-16 13:31:28 +02002253 if (bio->bi_bdev)
2254 rq->rq_disk = bio->bi_bdev->bd_disk;
2255}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Kiyoshi Uedaef9e3fa2008-10-01 16:12:15 +02002257/**
2258 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
2259 * @q : the queue of the device being checked
2260 *
2261 * Description:
2262 * Check if underlying low-level drivers of a device are busy.
2263 * If the drivers want to export their busy state, they must set own
2264 * exporting function using blk_queue_lld_busy() first.
2265 *
2266 * Basically, this function is used only by request stacking drivers
2267 * to stop dispatching requests to underlying devices when underlying
2268 * devices are busy. This behavior helps more I/O merging on the queue
2269 * of the request stacking driver and prevents I/O throughput regression
2270 * on burst I/O load.
2271 *
2272 * Return:
2273 * 0 - Not busy (The request stacking driver should dispatch request)
2274 * 1 - Busy (The request stacking driver should stop dispatching request)
2275 */
2276int blk_lld_busy(struct request_queue *q)
2277{
2278 if (q->lld_busy_fn)
2279 return q->lld_busy_fn(q);
2280
2281 return 0;
2282}
2283EXPORT_SYMBOL_GPL(blk_lld_busy);
2284
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002285/**
2286 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
2287 * @rq: the clone request to be cleaned up
2288 *
2289 * Description:
2290 * Free all bios in @rq for a cloned request.
2291 */
2292void blk_rq_unprep_clone(struct request *rq)
2293{
2294 struct bio *bio;
2295
2296 while ((bio = rq->bio) != NULL) {
2297 rq->bio = bio->bi_next;
2298
2299 bio_put(bio);
2300 }
2301}
2302EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
2303
2304/*
2305 * Copy attributes of the original request to the clone request.
2306 * The actual data parts (e.g. ->cmd, ->buffer, ->sense) are not copied.
2307 */
2308static void __blk_rq_prep_clone(struct request *dst, struct request *src)
2309{
2310 dst->cpu = src->cpu;
2311 dst->cmd_flags = (rq_data_dir(src) | REQ_NOMERGE);
2312 dst->cmd_type = src->cmd_type;
2313 dst->__sector = blk_rq_pos(src);
2314 dst->__data_len = blk_rq_bytes(src);
2315 dst->nr_phys_segments = src->nr_phys_segments;
2316 dst->ioprio = src->ioprio;
2317 dst->extra_len = src->extra_len;
2318}
2319
2320/**
2321 * blk_rq_prep_clone - Helper function to setup clone request
2322 * @rq: the request to be setup
2323 * @rq_src: original request to be cloned
2324 * @bs: bio_set that bios for clone are allocated from
2325 * @gfp_mask: memory allocation mask for bio
2326 * @bio_ctr: setup function to be called for each clone bio.
2327 * Returns %0 for success, non %0 for failure.
2328 * @data: private data to be passed to @bio_ctr
2329 *
2330 * Description:
2331 * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
2332 * The actual data parts of @rq_src (e.g. ->cmd, ->buffer, ->sense)
2333 * are not copied, and copying such parts is the caller's responsibility.
2334 * Also, pages which the original bios are pointing to are not copied
2335 * and the cloned bios just point same pages.
2336 * So cloned bios must be completed before original bios, which means
2337 * the caller must complete @rq before @rq_src.
2338 */
2339int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
2340 struct bio_set *bs, gfp_t gfp_mask,
2341 int (*bio_ctr)(struct bio *, struct bio *, void *),
2342 void *data)
2343{
2344 struct bio *bio, *bio_src;
2345
2346 if (!bs)
2347 bs = fs_bio_set;
2348
2349 blk_rq_init(NULL, rq);
2350
2351 __rq_for_each_bio(bio_src, rq_src) {
2352 bio = bio_alloc_bioset(gfp_mask, bio_src->bi_max_vecs, bs);
2353 if (!bio)
2354 goto free_and_out;
2355
2356 __bio_clone(bio, bio_src);
2357
2358 if (bio_integrity(bio_src) &&
Martin K. Petersen7878cba2009-06-26 15:37:49 +02002359 bio_integrity_clone(bio, bio_src, gfp_mask, bs))
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002360 goto free_and_out;
2361
2362 if (bio_ctr && bio_ctr(bio, bio_src, data))
2363 goto free_and_out;
2364
2365 if (rq->bio) {
2366 rq->biotail->bi_next = bio;
2367 rq->biotail = bio;
2368 } else
2369 rq->bio = rq->biotail = bio;
2370 }
2371
2372 __blk_rq_prep_clone(rq, rq_src);
2373
2374 return 0;
2375
2376free_and_out:
2377 if (bio)
2378 bio_free(bio, bs);
2379 blk_rq_unprep_clone(rq);
2380
2381 return -ENOMEM;
2382}
2383EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
2384
Jens Axboe18887ad2008-07-28 13:08:45 +02002385int kblockd_schedule_work(struct request_queue *q, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386{
2387 return queue_work(kblockd_workqueue, work);
2388}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389EXPORT_SYMBOL(kblockd_schedule_work);
2390
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391int __init blk_dev_init(void)
2392{
Nikanth Karthikesan9eb55b02009-04-27 14:53:54 +02002393 BUILD_BUG_ON(__REQ_NR_BITS > 8 *
2394 sizeof(((struct request *)0)->cmd_flags));
2395
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 kblockd_workqueue = create_workqueue("kblockd");
2397 if (!kblockd_workqueue)
2398 panic("Failed to create kblockd\n");
2399
2400 request_cachep = kmem_cache_create("blkdev_requests",
Paul Mundt20c2df82007-07-20 10:11:58 +09002401 sizeof(struct request), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
Jens Axboe8324aa92008-01-29 14:51:59 +01002403 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02002404 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 return 0;
2407}
2408