blob: e695634882a61200c50a3ee9dc5be650abfbc632 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 1994, Karl Keyte: Added support for disk statistics
4 * Elevator latency, (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
5 * Queue request tables / lock, selectable elevator, Jens Axboe <axboe@suse.de>
Jens Axboe6728cb02008-01-31 13:03:55 +01006 * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au>
7 * - July2000
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
9 */
10
11/*
12 * This handles all read/write requests to block devices
13 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/backing-dev.h>
17#include <linux/bio.h>
18#include <linux/blkdev.h>
19#include <linux/highmem.h>
20#include <linux/mm.h>
21#include <linux/kernel_stat.h>
22#include <linux/string.h>
23#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/completion.h>
25#include <linux/slab.h>
26#include <linux/swap.h>
27#include <linux/writeback.h>
Andrew Mortonfaccbd42006-12-10 02:19:35 -080028#include <linux/task_io_accounting_ops.h>
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;
Jens Axboed9938312009-06-12 14:45:52 +0200504 q->backing_dev_info.name = "block";
Jens Axboe0989a022009-06-12 14:42:56 +0200505
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700506 err = bdi_init(&q->backing_dev_info);
507 if (err) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100508 kmem_cache_free(blk_requestq_cachep, q);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700509 return NULL;
510 }
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 init_timer(&q->unplug_timer);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700513 setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
514 INIT_LIST_HEAD(&q->timeout_list);
Peter Zijlstra713ada92008-10-16 13:44:57 +0200515 INIT_WORK(&q->unplug_work, blk_unplug_work);
Al Viro483f4af2006-03-18 18:34:37 -0500516
Jens Axboe8324aa92008-01-29 14:51:59 +0100517 kobject_init(&q->kobj, &blk_queue_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Al Viro483f4af2006-03-18 18:34:37 -0500519 mutex_init(&q->sysfs_lock);
Neil Browne7e72bf2008-05-14 16:05:54 -0700520 spin_lock_init(&q->__queue_lock);
Al Viro483f4af2006-03-18 18:34:37 -0500521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return q;
523}
Christoph Lameter19460892005-06-23 00:08:19 -0700524EXPORT_SYMBOL(blk_alloc_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526/**
527 * blk_init_queue - prepare a request queue for use with a block device
528 * @rfn: The function to be called to process requests that have been
529 * placed on the queue.
530 * @lock: Request queue spin lock
531 *
532 * Description:
533 * If a block device wishes to use the standard request handling procedures,
534 * which sorts requests and coalesces adjacent requests, then it must
535 * call blk_init_queue(). The function @rfn will be called when there
536 * are requests on the queue that need to be processed. If the device
537 * supports plugging, then @rfn may not be called immediately when requests
538 * are available on the queue, but may be called at some time later instead.
539 * Plugged queues are generally unplugged when a buffer belonging to one
540 * of the requests on the queue is needed, or due to memory pressure.
541 *
542 * @rfn is not required, or even expected, to remove all requests off the
543 * queue, but only as many as it can handle at a time. If it does leave
544 * requests on the queue, it is responsible for arranging that the requests
545 * get dealt with eventually.
546 *
547 * The queue spin lock must be held while manipulating the requests on the
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200548 * request queue; this lock will be taken also from interrupt context, so irq
549 * disabling is needed for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 *
Randy Dunlap710027a2008-08-19 20:13:11 +0200551 * Function returns a pointer to the initialized request queue, or %NULL if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 * it didn't succeed.
553 *
554 * Note:
555 * blk_init_queue() must be paired with a blk_cleanup_queue() call
556 * when the block device is deactivated (such as at module unload).
557 **/
Christoph Lameter19460892005-06-23 00:08:19 -0700558
Jens Axboe165125e2007-07-24 09:28:11 +0200559struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Christoph Lameter19460892005-06-23 00:08:19 -0700561 return blk_init_queue_node(rfn, lock, -1);
562}
563EXPORT_SYMBOL(blk_init_queue);
564
Jens Axboe165125e2007-07-24 09:28:11 +0200565struct request_queue *
Christoph Lameter19460892005-06-23 00:08:19 -0700566blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
567{
Jens Axboe165125e2007-07-24 09:28:11 +0200568 struct request_queue *q = blk_alloc_queue_node(GFP_KERNEL, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 if (!q)
571 return NULL;
572
Christoph Lameter19460892005-06-23 00:08:19 -0700573 q->node = node_id;
Al Viro8669aaf2006-03-18 13:50:00 -0500574 if (blk_init_free_list(q)) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100575 kmem_cache_free(blk_requestq_cachep, q);
Al Viro8669aaf2006-03-18 13:50:00 -0500576 return NULL;
577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 q->request_fn = rfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 q->prep_rq_fn = NULL;
581 q->unplug_fn = generic_unplug_device;
Jens Axboebc58ba92009-01-23 10:54:44 +0100582 q->queue_flags = QUEUE_FLAG_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 q->queue_lock = lock;
584
Jens Axboef3b144a2009-03-06 08:48:33 +0100585 /*
586 * This also sets hw/phys segments, boundary and size
587 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 blk_queue_make_request(q, __make_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Alan Stern44ec9542007-02-20 11:01:57 -0500590 q->sg_reserved_size = INT_MAX;
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 /*
593 * all done
594 */
595 if (!elevator_init(q, NULL)) {
596 blk_queue_congestion_threshold(q);
597 return q;
598 }
599
Al Viro8669aaf2006-03-18 13:50:00 -0500600 blk_put_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return NULL;
602}
Christoph Lameter19460892005-06-23 00:08:19 -0700603EXPORT_SYMBOL(blk_init_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Jens Axboe165125e2007-07-24 09:28:11 +0200605int blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Nick Pigginfde6ad22005-06-23 00:08:53 -0700607 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
Al Viro483f4af2006-03-18 18:34:37 -0500608 kobject_get(&q->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return 0;
610 }
611
612 return 1;
613}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Jens Axboe165125e2007-07-24 09:28:11 +0200615static inline void blk_free_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Jens Axboe4aff5e22006-08-10 08:44:47 +0200617 if (rq->cmd_flags & REQ_ELVPRIV)
Tejun Heocb98fc82005-10-28 08:29:39 +0200618 elv_put_request(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 mempool_free(rq, q->rq.rq_pool);
620}
621
Jens Axboe1ea25ec2006-07-18 22:24:11 +0200622static struct request *
Jerome Marchand42dad762009-04-22 14:01:49 +0200623blk_alloc_request(struct request_queue *q, int flags, int priv, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
625 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
626
627 if (!rq)
628 return NULL;
629
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200630 blk_rq_init(q, rq);
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200631
Jerome Marchand42dad762009-04-22 14:01:49 +0200632 rq->cmd_flags = flags | REQ_ALLOCED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Tejun Heocb98fc82005-10-28 08:29:39 +0200634 if (priv) {
Jens Axboecb78b282006-07-28 09:32:57 +0200635 if (unlikely(elv_set_request(q, rq, gfp_mask))) {
Tejun Heocb98fc82005-10-28 08:29:39 +0200636 mempool_free(rq, q->rq.rq_pool);
637 return NULL;
638 }
Jens Axboe4aff5e22006-08-10 08:44:47 +0200639 rq->cmd_flags |= REQ_ELVPRIV;
Tejun Heocb98fc82005-10-28 08:29:39 +0200640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Tejun Heocb98fc82005-10-28 08:29:39 +0200642 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
645/*
646 * ioc_batching returns true if the ioc is a valid batching request and
647 * should be given priority access to a request.
648 */
Jens Axboe165125e2007-07-24 09:28:11 +0200649static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
651 if (!ioc)
652 return 0;
653
654 /*
655 * Make sure the process is able to allocate at least 1 request
656 * even if the batch times out, otherwise we could theoretically
657 * lose wakeups.
658 */
659 return ioc->nr_batch_requests == q->nr_batching ||
660 (ioc->nr_batch_requests > 0
661 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
662}
663
664/*
665 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
666 * will cause the process to be a "batcher" on all queues in the system. This
667 * is the behaviour we want though - once it gets a wakeup it should be given
668 * a nice run.
669 */
Jens Axboe165125e2007-07-24 09:28:11 +0200670static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 if (!ioc || ioc_batching(q, ioc))
673 return;
674
675 ioc->nr_batch_requests = q->nr_batching;
676 ioc->last_waited = jiffies;
677}
678
Jens Axboe1faa16d2009-04-06 14:48:01 +0200679static void __freed_request(struct request_queue *q, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
681 struct request_list *rl = &q->rq;
682
Jens Axboe1faa16d2009-04-06 14:48:01 +0200683 if (rl->count[sync] < queue_congestion_off_threshold(q))
684 blk_clear_queue_congested(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Jens Axboe1faa16d2009-04-06 14:48:01 +0200686 if (rl->count[sync] + 1 <= q->nr_requests) {
687 if (waitqueue_active(&rl->wait[sync]))
688 wake_up(&rl->wait[sync]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Jens Axboe1faa16d2009-04-06 14:48:01 +0200690 blk_clear_queue_full(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
692}
693
694/*
695 * A request has just been released. Account for it, update the full and
696 * congestion status, wake up any waiters. Called under q->queue_lock.
697 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200698static void freed_request(struct request_queue *q, int sync, int priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
700 struct request_list *rl = &q->rq;
701
Jens Axboe1faa16d2009-04-06 14:48:01 +0200702 rl->count[sync]--;
Tejun Heocb98fc82005-10-28 08:29:39 +0200703 if (priv)
704 rl->elvpriv--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Jens Axboe1faa16d2009-04-06 14:48:01 +0200706 __freed_request(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Jens Axboe1faa16d2009-04-06 14:48:01 +0200708 if (unlikely(rl->starved[sync ^ 1]))
709 __freed_request(q, sync ^ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712/*
Nick Piggind6344532005-06-28 20:45:14 -0700713 * Get a free request, queue_lock must be held.
714 * Returns NULL on failure, with queue_lock held.
715 * Returns !NULL on success, with queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 */
Jens Axboe165125e2007-07-24 09:28:11 +0200717static struct request *get_request(struct request_queue *q, int rw_flags,
Jens Axboe7749a8d2006-12-13 13:02:26 +0100718 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
720 struct request *rq = NULL;
721 struct request_list *rl = &q->rq;
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100722 struct io_context *ioc = NULL;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200723 const bool is_sync = rw_is_sync(rw_flags) != 0;
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100724 int may_queue, priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Jens Axboe7749a8d2006-12-13 13:02:26 +0100726 may_queue = elv_may_queue(q, rw_flags);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100727 if (may_queue == ELV_MQUEUE_NO)
728 goto rq_starved;
729
Jens Axboe1faa16d2009-04-06 14:48:01 +0200730 if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) {
731 if (rl->count[is_sync]+1 >= q->nr_requests) {
Jens Axboeb5deef92006-07-19 23:39:40 +0200732 ioc = current_io_context(GFP_ATOMIC, q->node);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100733 /*
734 * The queue will fill after this allocation, so set
735 * it as full, and mark this process as "batching".
736 * This process will be allowed to complete a batch of
737 * requests, others will be blocked.
738 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200739 if (!blk_queue_full(q, is_sync)) {
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100740 ioc_set_batching(q, ioc);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200741 blk_set_queue_full(q, is_sync);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100742 } else {
743 if (may_queue != ELV_MQUEUE_MUST
744 && !ioc_batching(q, ioc)) {
745 /*
746 * The queue is full and the allocating
747 * process is not a "batcher", and not
748 * exempted by the IO scheduler
749 */
750 goto out;
751 }
752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
Jens Axboe1faa16d2009-04-06 14:48:01 +0200754 blk_set_queue_congested(q, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
756
Jens Axboe082cf692005-06-28 16:35:11 +0200757 /*
758 * Only allow batching queuers to allocate up to 50% over the defined
759 * limit of requests, otherwise we could have thousands of requests
760 * allocated with any setting of ->nr_requests
761 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200762 if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
Jens Axboe082cf692005-06-28 16:35:11 +0200763 goto out;
Hugh Dickinsfd782a42005-06-29 15:15:40 +0100764
Jens Axboe1faa16d2009-04-06 14:48:01 +0200765 rl->count[is_sync]++;
766 rl->starved[is_sync] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200767
Jens Axboe64521d12005-10-28 08:30:39 +0200768 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Tejun Heocb98fc82005-10-28 08:29:39 +0200769 if (priv)
770 rl->elvpriv++;
771
Jerome Marchand42dad762009-04-22 14:01:49 +0200772 if (blk_queue_io_stat(q))
773 rw_flags |= REQ_IO_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 spin_unlock_irq(q->queue_lock);
775
Jens Axboe7749a8d2006-12-13 13:02:26 +0100776 rq = blk_alloc_request(q, rw_flags, priv, gfp_mask);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100777 if (unlikely(!rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 /*
779 * Allocation failed presumably due to memory. Undo anything
780 * we might have messed up.
781 *
782 * Allocating task should really be put onto the front of the
783 * wait queue, but this is pretty rare.
784 */
785 spin_lock_irq(q->queue_lock);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200786 freed_request(q, is_sync, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 /*
789 * in the very unlikely event that allocation failed and no
790 * requests for this direction was pending, mark us starved
791 * so that freeing of a request in the other direction will
792 * notice us. another possible fix would be to split the
793 * rq mempool into READ and WRITE
794 */
795rq_starved:
Jens Axboe1faa16d2009-04-06 14:48:01 +0200796 if (unlikely(rl->count[is_sync] == 0))
797 rl->starved[is_sync] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 goto out;
800 }
801
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100802 /*
803 * ioc may be NULL here, and ioc_batching will be false. That's
804 * OK, if the queue is under the request limit then requests need
805 * not count toward the nr_batch_requests limit. There will always
806 * be some limit enforced by BLK_BATCH_TIME.
807 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (ioc_batching(q, ioc))
809 ioc->nr_batch_requests--;
Jens Axboe6728cb02008-01-31 13:03:55 +0100810
Jens Axboe1faa16d2009-04-06 14:48:01 +0200811 trace_block_getrq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return rq;
814}
815
816/*
817 * No available requests for this queue, unplug the device and wait for some
818 * requests to become available.
Nick Piggind6344532005-06-28 20:45:14 -0700819 *
820 * Called with q->queue_lock held, and returns with it unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 */
Jens Axboe165125e2007-07-24 09:28:11 +0200822static struct request *get_request_wait(struct request_queue *q, int rw_flags,
Jens Axboe22e2c502005-06-27 10:55:12 +0200823 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Jens Axboe1faa16d2009-04-06 14:48:01 +0200825 const bool is_sync = rw_is_sync(rw_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 struct request *rq;
827
Jens Axboe7749a8d2006-12-13 13:02:26 +0100828 rq = get_request(q, rw_flags, bio, GFP_NOIO);
Nick Piggin450991b2005-06-28 20:45:13 -0700829 while (!rq) {
830 DEFINE_WAIT(wait);
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200831 struct io_context *ioc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 struct request_list *rl = &q->rq;
833
Jens Axboe1faa16d2009-04-06 14:48:01 +0200834 prepare_to_wait_exclusive(&rl->wait[is_sync], &wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 TASK_UNINTERRUPTIBLE);
836
Jens Axboe1faa16d2009-04-06 14:48:01 +0200837 trace_block_sleeprq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200839 __generic_unplug_device(q);
840 spin_unlock_irq(q->queue_lock);
841 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200843 /*
844 * After sleeping, we become a "batching" process and
845 * will be able to allocate at least one request, and
846 * up to a big batch of them for a small period time.
847 * See ioc_batching, ioc_set_batching
848 */
849 ioc = current_io_context(GFP_NOIO, q->node);
850 ioc_set_batching(q, ioc);
Jens Axboe2056a782006-03-23 20:00:26 +0100851
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200852 spin_lock_irq(q->queue_lock);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200853 finish_wait(&rl->wait[is_sync], &wait);
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200854
855 rq = get_request(q, rw_flags, bio, GFP_NOIO);
856 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 return rq;
859}
860
Jens Axboe165125e2007-07-24 09:28:11 +0200861struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
863 struct request *rq;
864
865 BUG_ON(rw != READ && rw != WRITE);
866
Nick Piggind6344532005-06-28 20:45:14 -0700867 spin_lock_irq(q->queue_lock);
868 if (gfp_mask & __GFP_WAIT) {
Jens Axboe22e2c502005-06-27 10:55:12 +0200869 rq = get_request_wait(q, rw, NULL);
Nick Piggind6344532005-06-28 20:45:14 -0700870 } else {
Jens Axboe22e2c502005-06-27 10:55:12 +0200871 rq = get_request(q, rw, NULL, gfp_mask);
Nick Piggind6344532005-06-28 20:45:14 -0700872 if (!rq)
873 spin_unlock_irq(q->queue_lock);
874 }
875 /* q->queue_lock is unlocked at this point */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 return rq;
878}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879EXPORT_SYMBOL(blk_get_request);
880
881/**
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300882 * blk_make_request - given a bio, allocate a corresponding struct request.
Randy Dunlap8ebf9752009-06-11 20:00:41 -0700883 * @q: target request queue
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300884 * @bio: The bio describing the memory mappings that will be submitted for IO.
885 * It may be a chained-bio properly constructed by block/bio layer.
Randy Dunlap8ebf9752009-06-11 20:00:41 -0700886 * @gfp_mask: gfp flags to be used for memory allocation
Jens Axboedc72ef42006-07-20 14:54:05 +0200887 *
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300888 * blk_make_request is the parallel of generic_make_request for BLOCK_PC
889 * type commands. Where the struct request needs to be farther initialized by
890 * the caller. It is passed a &struct bio, which describes the memory info of
891 * the I/O transfer.
892 *
893 * The caller of blk_make_request must make sure that bi_io_vec
894 * are set to describe the memory buffers. That bio_data_dir() will return
895 * the needed direction of the request. (And all bio's in the passed bio-chain
896 * are properly set accordingly)
897 *
898 * If called under none-sleepable conditions, mapped bio buffers must not
899 * need bouncing, by calling the appropriate masked or flagged allocator,
900 * suitable for the target device. Otherwise the call to blk_queue_bounce will
901 * BUG.
Jens Axboe53674ac2009-05-19 19:52:35 +0200902 *
903 * WARNING: When allocating/cloning a bio-chain, careful consideration should be
904 * given to how you allocate bios. In particular, you cannot use __GFP_WAIT for
905 * anything but the first bio in the chain. Otherwise you risk waiting for IO
906 * completion of a bio that hasn't been submitted yet, thus resulting in a
907 * deadlock. Alternatively bios should be allocated using bio_kmalloc() instead
908 * of bio_alloc(), as that avoids the mempool deadlock.
909 * If possible a big IO should be split into smaller parts when allocation
910 * fails. Partial allocation should not be an error, or you risk a live-lock.
Jens Axboedc72ef42006-07-20 14:54:05 +0200911 */
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300912struct request *blk_make_request(struct request_queue *q, struct bio *bio,
913 gfp_t gfp_mask)
Jens Axboedc72ef42006-07-20 14:54:05 +0200914{
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300915 struct request *rq = blk_get_request(q, bio_data_dir(bio), gfp_mask);
916
917 if (unlikely(!rq))
918 return ERR_PTR(-ENOMEM);
919
920 for_each_bio(bio) {
921 struct bio *bounce_bio = bio;
922 int ret;
923
924 blk_queue_bounce(q, &bounce_bio);
925 ret = blk_rq_append_bio(q, rq, bounce_bio);
926 if (unlikely(ret)) {
927 blk_put_request(rq);
928 return ERR_PTR(ret);
929 }
930 }
931
932 return rq;
Jens Axboedc72ef42006-07-20 14:54:05 +0200933}
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300934EXPORT_SYMBOL(blk_make_request);
Jens Axboedc72ef42006-07-20 14:54:05 +0200935
936/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 * blk_requeue_request - put a request back on queue
938 * @q: request queue where request should be inserted
939 * @rq: request to be inserted
940 *
941 * Description:
942 * Drivers often keep queueing requests until the hardware cannot accept
943 * more, when that condition happens we need to put the request back
944 * on the queue. Must be called with queue lock held.
945 */
Jens Axboe165125e2007-07-24 09:28:11 +0200946void blk_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
Jens Axboe242f9dc2008-09-14 05:55:09 -0700948 blk_delete_timer(rq);
949 blk_clear_rq_complete(rq);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100950 trace_block_rq_requeue(q, rq);
Jens Axboe2056a782006-03-23 20:00:26 +0100951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (blk_rq_tagged(rq))
953 blk_queue_end_tag(q, rq);
954
James Bottomleyba396a62009-05-27 14:17:08 +0200955 BUG_ON(blk_queued_rq(rq));
956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 elv_requeue_request(q, rq);
958}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959EXPORT_SYMBOL(blk_requeue_request);
960
961/**
Randy Dunlap710027a2008-08-19 20:13:11 +0200962 * blk_insert_request - insert a special request into a request queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 * @q: request queue where request should be inserted
964 * @rq: request to be inserted
965 * @at_head: insert request at head or tail of queue
966 * @data: private data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 *
968 * Description:
969 * Many block devices need to execute commands asynchronously, so they don't
970 * block the whole kernel from preemption during request execution. This is
971 * accomplished normally by inserting aritficial requests tagged as
Randy Dunlap710027a2008-08-19 20:13:11 +0200972 * REQ_TYPE_SPECIAL in to the corresponding request queue, and letting them
973 * be scheduled for actual execution by the request queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 *
975 * We have the option of inserting the head or the tail of the queue.
976 * Typically we use the tail for new ioctls and so forth. We use the head
977 * of the queue for things like a QUEUE_FULL message from a device, or a
978 * host that is unable to accept a particular command.
979 */
Jens Axboe165125e2007-07-24 09:28:11 +0200980void blk_insert_request(struct request_queue *q, struct request *rq,
Tejun Heo 867d1192005-04-24 02:06:05 -0500981 int at_head, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
Tejun Heo 867d1192005-04-24 02:06:05 -0500983 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 unsigned long flags;
985
986 /*
987 * tell I/O scheduler that this isn't a regular read/write (ie it
988 * must not attempt merges on this) and that it acts as a soft
989 * barrier
990 */
Jens Axboe4aff5e22006-08-10 08:44:47 +0200991 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 rq->special = data;
994
995 spin_lock_irqsave(q->queue_lock, flags);
996
997 /*
998 * If command is tagged, release the tag
999 */
Tejun Heo 867d1192005-04-24 02:06:05 -05001000 if (blk_rq_tagged(rq))
1001 blk_queue_end_tag(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Jerome Marchandb238b3d2007-10-23 15:05:46 +02001003 drive_stat_acct(rq, 1);
Tejun Heo 867d1192005-04-24 02:06:05 -05001004 __elv_add_request(q, rq, where, 0);
Tejun Heoa7f55792009-04-23 11:05:17 +09001005 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 spin_unlock_irqrestore(q->queue_lock, flags);
1007}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008EXPORT_SYMBOL(blk_insert_request);
1009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010/*
1011 * add-request adds a request to the linked list.
1012 * queue lock is held and interrupts disabled, as we muck with the
1013 * request queue list.
1014 */
Jens Axboe6728cb02008-01-31 13:03:55 +01001015static inline void add_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
Jerome Marchandb238b3d2007-10-23 15:05:46 +02001017 drive_stat_acct(req, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 /*
1020 * elevator indicated where it wants this request to be
1021 * inserted at elevator_merge time
1022 */
1023 __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
1024}
Jens Axboe6728cb02008-01-31 13:03:55 +01001025
Tejun Heo074a7ac2008-08-25 19:56:14 +09001026static void part_round_stats_single(int cpu, struct hd_struct *part,
1027 unsigned long now)
1028{
1029 if (now == part->stamp)
1030 return;
1031
1032 if (part->in_flight) {
1033 __part_stat_add(cpu, part, time_in_queue,
1034 part->in_flight * (now - part->stamp));
1035 __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
1036 }
1037 part->stamp = now;
1038}
1039
1040/**
Randy Dunlap496aa8a2008-10-16 07:46:23 +02001041 * part_round_stats() - Round off the performance stats on a struct disk_stats.
1042 * @cpu: cpu number for stats access
1043 * @part: target partition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 *
1045 * The average IO queue length and utilisation statistics are maintained
1046 * by observing the current state of the queue length and the amount of
1047 * time it has been in this state for.
1048 *
1049 * Normally, that accounting is done on IO completion, but that can result
1050 * in more than a second's worth of IO being accounted for within any one
1051 * second, leading to >100% utilisation. To deal with that, we call this
1052 * function to do a round-off before returning the results when reading
1053 * /proc/diskstats. This accounts immediately for all queue usage up to
1054 * the current jiffies and restarts the counters again.
1055 */
Tejun Heoc9959052008-08-25 19:47:21 +09001056void part_round_stats(int cpu, struct hd_struct *part)
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001057{
1058 unsigned long now = jiffies;
1059
Tejun Heo074a7ac2008-08-25 19:56:14 +09001060 if (part->partno)
1061 part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
1062 part_round_stats_single(cpu, part, now);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001063}
Tejun Heo074a7ac2008-08-25 19:56:14 +09001064EXPORT_SYMBOL_GPL(part_round_stats);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066/*
1067 * queue lock must be held
1068 */
Jens Axboe165125e2007-07-24 09:28:11 +02001069void __blk_put_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 if (unlikely(!q))
1072 return;
1073 if (unlikely(--req->ref_count))
1074 return;
1075
Tejun Heo8922e162005-10-20 16:23:44 +02001076 elv_completed_request(q, req);
1077
Boaz Harrosh1cd96c22009-03-24 12:35:07 +01001078 /* this is a bio leak */
1079 WARN_ON(req->bio != NULL);
1080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 /*
1082 * Request may not have originated from ll_rw_blk. if not,
1083 * it didn't come out of our reserved rq pools
1084 */
Jens Axboe49171e52006-08-10 08:59:11 +02001085 if (req->cmd_flags & REQ_ALLOCED) {
Jens Axboe1faa16d2009-04-06 14:48:01 +02001086 int is_sync = rq_is_sync(req) != 0;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001087 int priv = req->cmd_flags & REQ_ELVPRIV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 BUG_ON(!list_empty(&req->queuelist));
Jens Axboe98170642006-07-28 09:23:08 +02001090 BUG_ON(!hlist_unhashed(&req->hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 blk_free_request(q, req);
Jens Axboe1faa16d2009-04-06 14:48:01 +02001093 freed_request(q, is_sync, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
1095}
Mike Christie6e39b692005-11-11 05:30:24 -06001096EXPORT_SYMBOL_GPL(__blk_put_request);
1097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098void blk_put_request(struct request *req)
1099{
Tejun Heo8922e162005-10-20 16:23:44 +02001100 unsigned long flags;
Jens Axboe165125e2007-07-24 09:28:11 +02001101 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
FUJITA Tomonori52a93ba2008-07-15 21:21:45 +02001103 spin_lock_irqsave(q->queue_lock, flags);
1104 __blk_put_request(q, req);
1105 spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107EXPORT_SYMBOL(blk_put_request);
1108
Jens Axboe86db1e22008-01-29 14:53:40 +01001109void init_request_from_bio(struct request *req, struct bio *bio)
Tejun Heo52d9e672006-01-06 09:49:58 +01001110{
Jens Axboec7c22e42008-09-13 20:26:01 +02001111 req->cpu = bio->bi_comp_cpu;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001112 req->cmd_type = REQ_TYPE_FS;
Tejun Heo52d9e672006-01-06 09:49:58 +01001113
1114 /*
1115 * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
1116 */
Mike Christie6000a362008-08-19 18:45:30 -05001117 if (bio_rw_ahead(bio))
1118 req->cmd_flags |= (REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
1119 REQ_FAILFAST_DRIVER);
1120 if (bio_failfast_dev(bio))
1121 req->cmd_flags |= REQ_FAILFAST_DEV;
1122 if (bio_failfast_transport(bio))
1123 req->cmd_flags |= REQ_FAILFAST_TRANSPORT;
1124 if (bio_failfast_driver(bio))
1125 req->cmd_flags |= REQ_FAILFAST_DRIVER;
Tejun Heo52d9e672006-01-06 09:49:58 +01001126
David Woodhousefb2dce82008-08-05 18:01:53 +01001127 if (unlikely(bio_discard(bio))) {
David Woodhousee17fc0a2008-08-09 16:42:20 +01001128 req->cmd_flags |= REQ_DISCARD;
1129 if (bio_barrier(bio))
1130 req->cmd_flags |= REQ_SOFTBARRIER;
David Woodhousefb2dce82008-08-05 18:01:53 +01001131 req->q->prepare_discard_fn(req->q, req);
David Woodhousee17fc0a2008-08-09 16:42:20 +01001132 } else if (unlikely(bio_barrier(bio)))
Tejun Heoe4025f62009-04-23 11:05:17 +09001133 req->cmd_flags |= REQ_HARDBARRIER;
Tejun Heo52d9e672006-01-06 09:49:58 +01001134
Jens Axboeb31dc662006-06-13 08:26:10 +02001135 if (bio_sync(bio))
Jens Axboe4aff5e22006-08-10 08:44:47 +02001136 req->cmd_flags |= REQ_RW_SYNC;
Jens Axboe5404bc72006-08-10 09:01:02 +02001137 if (bio_rw_meta(bio))
1138 req->cmd_flags |= REQ_RW_META;
Jens Axboeaeb6faf2009-04-06 14:48:07 +02001139 if (bio_noidle(bio))
1140 req->cmd_flags |= REQ_NOIDLE;
Jens Axboeb31dc662006-06-13 08:26:10 +02001141
Tejun Heo52d9e672006-01-06 09:49:58 +01001142 req->errors = 0;
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001143 req->__sector = bio->bi_sector;
Tejun Heo52d9e672006-01-06 09:49:58 +01001144 req->ioprio = bio_prio(bio);
NeilBrownbc1c56f2007-08-16 13:31:30 +02001145 blk_rq_bio_prep(req->q, req, bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001146}
1147
Jens Axboe644b2d92009-04-06 14:48:06 +02001148/*
1149 * Only disabling plugging for non-rotational devices if it does tagging
1150 * as well, otherwise we do need the proper merging
1151 */
1152static inline bool queue_should_plug(struct request_queue *q)
1153{
1154 return !(blk_queue_nonrot(q) && blk_queue_tagged(q));
1155}
1156
Jens Axboe165125e2007-07-24 09:28:11 +02001157static int __make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
Nick Piggin450991b2005-06-28 20:45:13 -07001159 struct request *req;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001160 int el_ret;
1161 unsigned int bytes = bio->bi_size;
Jens Axboe51da90f2006-07-18 04:14:45 +02001162 const unsigned short prio = bio_prio(bio);
1163 const int sync = bio_sync(bio);
Jens Axboe213d9412009-01-06 09:16:05 +01001164 const int unplug = bio_unplug(bio);
Jens Axboe7749a8d2006-12-13 13:02:26 +01001165 int rw_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
NeilBrowndb64f682009-06-30 09:35:44 +02001167 if (bio_barrier(bio) && bio_has_data(bio) &&
1168 (q->next_ordered == QUEUE_ORDERED_NONE)) {
1169 bio_endio(bio, -EOPNOTSUPP);
1170 return 0;
1171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 /*
1173 * low level driver can indicate that it wants pages above a
1174 * certain limit bounced to low memory (ie for highmem, or even
1175 * ISA dma in theory)
1176 */
1177 blk_queue_bounce(q, &bio);
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 spin_lock_irq(q->queue_lock);
1180
Tejun Heoa7384672008-11-28 13:32:03 +09001181 if (unlikely(bio_barrier(bio)) || elv_queue_empty(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 goto get_rq;
1183
1184 el_ret = elv_merge(q, &req, bio);
1185 switch (el_ret) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001186 case ELEVATOR_BACK_MERGE:
1187 BUG_ON(!rq_mergeable(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Jens Axboe6728cb02008-01-31 13:03:55 +01001189 if (!ll_back_merge_fn(q, req, bio))
1190 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001192 trace_block_bio_backmerge(q, bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001193
Jens Axboe6728cb02008-01-31 13:03:55 +01001194 req->biotail->bi_next = bio;
1195 req->biotail = bio;
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001196 req->__data_len += bytes;
Jens Axboe6728cb02008-01-31 13:03:55 +01001197 req->ioprio = ioprio_best(req->ioprio, prio);
Jens Axboeab780f12008-08-26 10:25:02 +02001198 if (!blk_rq_cpu_valid(req))
1199 req->cpu = bio->bi_comp_cpu;
Jens Axboe6728cb02008-01-31 13:03:55 +01001200 drive_stat_acct(req, 0);
1201 if (!attempt_back_merge(q, req))
1202 elv_merged_request(q, req, el_ret);
1203 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Jens Axboe6728cb02008-01-31 13:03:55 +01001205 case ELEVATOR_FRONT_MERGE:
1206 BUG_ON(!rq_mergeable(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Jens Axboe6728cb02008-01-31 13:03:55 +01001208 if (!ll_front_merge_fn(q, req, bio))
1209 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001211 trace_block_bio_frontmerge(q, bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001212
Jens Axboe6728cb02008-01-31 13:03:55 +01001213 bio->bi_next = req->bio;
1214 req->bio = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Jens Axboe6728cb02008-01-31 13:03:55 +01001216 /*
1217 * may not be valid. if the low level driver said
1218 * it didn't need a bounce buffer then it better
1219 * not touch req->buffer either...
1220 */
1221 req->buffer = bio_data(bio);
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001222 req->__sector = bio->bi_sector;
1223 req->__data_len += bytes;
Jens Axboe6728cb02008-01-31 13:03:55 +01001224 req->ioprio = ioprio_best(req->ioprio, prio);
Jens Axboeab780f12008-08-26 10:25:02 +02001225 if (!blk_rq_cpu_valid(req))
1226 req->cpu = bio->bi_comp_cpu;
Jens Axboe6728cb02008-01-31 13:03:55 +01001227 drive_stat_acct(req, 0);
1228 if (!attempt_front_merge(q, req))
1229 elv_merged_request(q, req, el_ret);
1230 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Jens Axboe6728cb02008-01-31 13:03:55 +01001232 /* ELV_NO_MERGE: elevator says don't/can't merge. */
1233 default:
1234 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 }
1236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237get_rq:
Nick Piggin450991b2005-06-28 20:45:13 -07001238 /*
Jens Axboe7749a8d2006-12-13 13:02:26 +01001239 * This sync check and mask will be re-done in init_request_from_bio(),
1240 * but we need to set it earlier to expose the sync flag to the
1241 * rq allocator and io schedulers.
1242 */
1243 rw_flags = bio_data_dir(bio);
1244 if (sync)
1245 rw_flags |= REQ_RW_SYNC;
1246
1247 /*
Nick Piggin450991b2005-06-28 20:45:13 -07001248 * Grab a free request. This is might sleep but can not fail.
Nick Piggind6344532005-06-28 20:45:14 -07001249 * Returns with the queue unlocked.
Nick Piggin450991b2005-06-28 20:45:13 -07001250 */
Jens Axboe7749a8d2006-12-13 13:02:26 +01001251 req = get_request_wait(q, rw_flags, bio);
Nick Piggind6344532005-06-28 20:45:14 -07001252
Nick Piggin450991b2005-06-28 20:45:13 -07001253 /*
1254 * After dropping the lock and possibly sleeping here, our request
1255 * may now be mergeable after it had proven unmergeable (above).
1256 * We don't worry about that case for efficiency. It won't happen
1257 * often, and the elevators are able to handle it.
1258 */
Tejun Heo52d9e672006-01-06 09:49:58 +01001259 init_request_from_bio(req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Nick Piggin450991b2005-06-28 20:45:13 -07001261 spin_lock_irq(q->queue_lock);
Jens Axboec7c22e42008-09-13 20:26:01 +02001262 if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags) ||
1263 bio_flagged(bio, BIO_CPU_AFFINE))
1264 req->cpu = blk_cpu_to_group(smp_processor_id());
Jens Axboe644b2d92009-04-06 14:48:06 +02001265 if (queue_should_plug(q) && elv_queue_empty(q))
Nick Piggin450991b2005-06-28 20:45:13 -07001266 blk_plug_device(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 add_request(q, req);
1268out:
Jens Axboe644b2d92009-04-06 14:48:06 +02001269 if (unplug || !queue_should_plug(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 __generic_unplug_device(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 spin_unlock_irq(q->queue_lock);
1272 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273}
1274
1275/*
1276 * If bio->bi_dev is a partition, remap the location
1277 */
1278static inline void blk_partition_remap(struct bio *bio)
1279{
1280 struct block_device *bdev = bio->bi_bdev;
1281
Jens Axboebf2de6f2007-09-27 13:01:25 +02001282 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 struct hd_struct *p = bdev->bd_part;
Jens Axboea3623572005-11-01 09:26:16 +01001284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 bio->bi_sector += p->start_sect;
1286 bio->bi_bdev = bdev->bd_contains;
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001287
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001288 trace_block_remap(bdev_get_queue(bio->bi_bdev), bio,
Alan D. Brunelle22a7c312009-05-04 16:35:08 -04001289 bdev->bd_dev,
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001290 bio->bi_sector - p->start_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 }
1292}
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294static void handle_bad_sector(struct bio *bio)
1295{
1296 char b[BDEVNAME_SIZE];
1297
1298 printk(KERN_INFO "attempt to access beyond end of device\n");
1299 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1300 bdevname(bio->bi_bdev, b),
1301 bio->bi_rw,
1302 (unsigned long long)bio->bi_sector + bio_sectors(bio),
1303 (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
1304
1305 set_bit(BIO_EOF, &bio->bi_flags);
1306}
1307
Akinobu Mitac17bb492006-12-08 02:39:46 -08001308#ifdef CONFIG_FAIL_MAKE_REQUEST
1309
1310static DECLARE_FAULT_ATTR(fail_make_request);
1311
1312static int __init setup_fail_make_request(char *str)
1313{
1314 return setup_fault_attr(&fail_make_request, str);
1315}
1316__setup("fail_make_request=", setup_fail_make_request);
1317
1318static int should_fail_request(struct bio *bio)
1319{
Tejun Heoeddb2e22008-08-25 19:56:13 +09001320 struct hd_struct *part = bio->bi_bdev->bd_part;
1321
1322 if (part_to_disk(part)->part0.make_it_fail || part->make_it_fail)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001323 return should_fail(&fail_make_request, bio->bi_size);
1324
1325 return 0;
1326}
1327
1328static int __init fail_make_request_debugfs(void)
1329{
1330 return init_fault_attr_dentries(&fail_make_request,
1331 "fail_make_request");
1332}
1333
1334late_initcall(fail_make_request_debugfs);
1335
1336#else /* CONFIG_FAIL_MAKE_REQUEST */
1337
1338static inline int should_fail_request(struct bio *bio)
1339{
1340 return 0;
1341}
1342
1343#endif /* CONFIG_FAIL_MAKE_REQUEST */
1344
Jens Axboec07e2b42007-07-18 13:27:58 +02001345/*
1346 * Check whether this bio extends beyond the end of the device.
1347 */
1348static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1349{
1350 sector_t maxsector;
1351
1352 if (!nr_sectors)
1353 return 0;
1354
1355 /* Test device or partition size, when known. */
1356 maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
1357 if (maxsector) {
1358 sector_t sector = bio->bi_sector;
1359
1360 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1361 /*
1362 * This may well happen - the kernel calls bread()
1363 * without checking the size of the device, e.g., when
1364 * mounting a device.
1365 */
1366 handle_bad_sector(bio);
1367 return 1;
1368 }
1369 }
1370
1371 return 0;
1372}
1373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001375 * generic_make_request - hand a buffer to its device driver for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 * @bio: The bio describing the location in memory and on the device.
1377 *
1378 * generic_make_request() is used to make I/O requests of block
1379 * devices. It is passed a &struct bio, which describes the I/O that needs
1380 * to be done.
1381 *
1382 * generic_make_request() does not return any status. The
1383 * success/failure status of the request, along with notification of
1384 * completion, is delivered asynchronously through the bio->bi_end_io
1385 * function described (one day) else where.
1386 *
1387 * The caller of generic_make_request must make sure that bi_io_vec
1388 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1389 * set to describe the device address, and the
1390 * bi_end_io and optionally bi_private are set to describe how
1391 * completion notification should be signaled.
1392 *
1393 * generic_make_request and the drivers it calls may use bi_next if this
1394 * bio happens to be merged with someone else, and may change bi_dev and
1395 * bi_sector for remaps as it sees fit. So the values of these fields
1396 * should NOT be depended on after the call to generic_make_request.
1397 */
Neil Brownd89d8792007-05-01 09:53:42 +02001398static inline void __generic_make_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
Jens Axboe165125e2007-07-24 09:28:11 +02001400 struct request_queue *q;
NeilBrown5ddfe962006-10-30 22:07:21 -08001401 sector_t old_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 int ret, nr_sectors = bio_sectors(bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001403 dev_t old_dev;
Jens Axboe51fd77b2007-11-02 08:49:08 +01001404 int err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Jens Axboec07e2b42007-07-18 13:27:58 +02001408 if (bio_check_eod(bio, nr_sectors))
1409 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 /*
1412 * Resolve the mapping until finished. (drivers are
1413 * still free to implement/resolve their own stacking
1414 * by explicitly returning 0)
1415 *
1416 * NOTE: we don't repeat the blk_size check for each new device.
1417 * Stacking drivers are expected to know what they are doing.
1418 */
NeilBrown5ddfe962006-10-30 22:07:21 -08001419 old_sector = -1;
Jens Axboe2056a782006-03-23 20:00:26 +01001420 old_dev = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 do {
1422 char b[BDEVNAME_SIZE];
1423
1424 q = bdev_get_queue(bio->bi_bdev);
Tejun Heoa7384672008-11-28 13:32:03 +09001425 if (unlikely(!q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 printk(KERN_ERR
1427 "generic_make_request: Trying to access "
1428 "nonexistent block-device %s (%Lu)\n",
1429 bdevname(bio->bi_bdev, b),
1430 (long long) bio->bi_sector);
Tejun Heoa7384672008-11-28 13:32:03 +09001431 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 }
1433
Martin K. Petersenae03bf62009-05-22 17:17:50 -04001434 if (unlikely(nr_sectors > queue_max_hw_sectors(q))) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001435 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
Martin K. Petersenae03bf62009-05-22 17:17:50 -04001436 bdevname(bio->bi_bdev, b),
1437 bio_sectors(bio),
1438 queue_max_hw_sectors(q));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 goto end_io;
1440 }
1441
Nick Pigginfde6ad22005-06-23 00:08:53 -07001442 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 goto end_io;
1444
Akinobu Mitac17bb492006-12-08 02:39:46 -08001445 if (should_fail_request(bio))
1446 goto end_io;
1447
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 /*
1449 * If this device has partitions, remap block n
1450 * of partition p to block n+start(p) of the disk.
1451 */
1452 blk_partition_remap(bio);
1453
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +02001454 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio))
1455 goto end_io;
1456
NeilBrown5ddfe962006-10-30 22:07:21 -08001457 if (old_sector != -1)
Alan D. Brunelle22a7c312009-05-04 16:35:08 -04001458 trace_block_remap(q, bio, old_dev, old_sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001459
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001460 trace_block_bio_queue(q, bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001461
NeilBrown5ddfe962006-10-30 22:07:21 -08001462 old_sector = bio->bi_sector;
Jens Axboe2056a782006-03-23 20:00:26 +01001463 old_dev = bio->bi_bdev->bd_dev;
1464
Jens Axboec07e2b42007-07-18 13:27:58 +02001465 if (bio_check_eod(bio, nr_sectors))
1466 goto end_io;
Tejun Heoa7384672008-11-28 13:32:03 +09001467
1468 if (bio_discard(bio) && !q->prepare_discard_fn) {
Jens Axboe51fd77b2007-11-02 08:49:08 +01001469 err = -EOPNOTSUPP;
1470 goto end_io;
1471 }
NeilBrown5ddfe962006-10-30 22:07:21 -08001472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 ret = q->make_request_fn(q, bio);
1474 } while (ret);
Tejun Heoa7384672008-11-28 13:32:03 +09001475
1476 return;
1477
1478end_io:
1479 bio_endio(bio, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480}
1481
Neil Brownd89d8792007-05-01 09:53:42 +02001482/*
1483 * We only want one ->make_request_fn to be active at a time,
1484 * else stack usage with stacked devices could be a problem.
1485 * So use current->bio_{list,tail} to keep a list of requests
1486 * submited by a make_request_fn function.
1487 * current->bio_tail is also used as a flag to say if
1488 * generic_make_request is currently active in this task or not.
1489 * If it is NULL, then no make_request is active. If it is non-NULL,
1490 * then a make_request is active, and new requests should be added
1491 * at the tail
1492 */
1493void generic_make_request(struct bio *bio)
1494{
1495 if (current->bio_tail) {
1496 /* make_request is active */
1497 *(current->bio_tail) = bio;
1498 bio->bi_next = NULL;
1499 current->bio_tail = &bio->bi_next;
1500 return;
1501 }
1502 /* following loop may be a bit non-obvious, and so deserves some
1503 * explanation.
1504 * Before entering the loop, bio->bi_next is NULL (as all callers
1505 * ensure that) so we have a list with a single bio.
1506 * We pretend that we have just taken it off a longer list, so
1507 * we assign bio_list to the next (which is NULL) and bio_tail
1508 * to &bio_list, thus initialising the bio_list of new bios to be
1509 * added. __generic_make_request may indeed add some more bios
1510 * through a recursive call to generic_make_request. If it
1511 * did, we find a non-NULL value in bio_list and re-enter the loop
1512 * from the top. In this case we really did just take the bio
1513 * of the top of the list (no pretending) and so fixup bio_list and
1514 * bio_tail or bi_next, and call into __generic_make_request again.
1515 *
1516 * The loop was structured like this to make only one call to
1517 * __generic_make_request (which is important as it is large and
1518 * inlined) and to keep the structure simple.
1519 */
1520 BUG_ON(bio->bi_next);
1521 do {
1522 current->bio_list = bio->bi_next;
1523 if (bio->bi_next == NULL)
1524 current->bio_tail = &current->bio_list;
1525 else
1526 bio->bi_next = NULL;
1527 __generic_make_request(bio);
1528 bio = current->bio_list;
1529 } while (bio);
1530 current->bio_tail = NULL; /* deactivate */
1531}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532EXPORT_SYMBOL(generic_make_request);
1533
1534/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001535 * submit_bio - submit a bio to the block device layer for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1537 * @bio: The &struct bio which describes the I/O
1538 *
1539 * submit_bio() is very similar in purpose to generic_make_request(), and
1540 * uses that function to do most of the work. Both are fairly rough
Randy Dunlap710027a2008-08-19 20:13:11 +02001541 * interfaces; @bio must be presetup and ready for I/O.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 *
1543 */
1544void submit_bio(int rw, struct bio *bio)
1545{
1546 int count = bio_sectors(bio);
1547
Jens Axboe22e2c502005-06-27 10:55:12 +02001548 bio->bi_rw |= rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Jens Axboebf2de6f2007-09-27 13:01:25 +02001550 /*
1551 * If it's a regular read/write or a barrier with data attached,
1552 * go through the normal accounting stuff before submission.
1553 */
Jens Axboea9c701e2008-08-08 11:04:44 +02001554 if (bio_has_data(bio)) {
Jens Axboebf2de6f2007-09-27 13:01:25 +02001555 if (rw & WRITE) {
1556 count_vm_events(PGPGOUT, count);
1557 } else {
1558 task_io_account_read(bio->bi_size);
1559 count_vm_events(PGPGIN, count);
1560 }
1561
1562 if (unlikely(block_dump)) {
1563 char b[BDEVNAME_SIZE];
1564 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001565 current->comm, task_pid_nr(current),
Jens Axboebf2de6f2007-09-27 13:01:25 +02001566 (rw & WRITE) ? "WRITE" : "READ",
1567 (unsigned long long)bio->bi_sector,
Jens Axboe6728cb02008-01-31 13:03:55 +01001568 bdevname(bio->bi_bdev, b));
Jens Axboebf2de6f2007-09-27 13:01:25 +02001569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 }
1571
1572 generic_make_request(bio);
1573}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574EXPORT_SYMBOL(submit_bio);
1575
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001576/**
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001577 * blk_rq_check_limits - Helper function to check a request for the queue limit
1578 * @q: the queue
1579 * @rq: the request being checked
1580 *
1581 * Description:
1582 * @rq may have been made based on weaker limitations of upper-level queues
1583 * in request stacking drivers, and it may violate the limitation of @q.
1584 * Since the block layer and the underlying device driver trust @rq
1585 * after it is inserted to @q, it should be checked against @q before
1586 * the insertion using this generic function.
1587 *
1588 * This function should also be useful for request stacking drivers
1589 * in some cases below, so export this fuction.
1590 * Request stacking drivers like request-based dm may change the queue
1591 * limits while requests are in the queue (e.g. dm's table swapping).
1592 * Such request stacking drivers should check those requests agaist
1593 * the new queue limits again when they dispatch those requests,
1594 * although such checkings are also done against the old queue limits
1595 * when submitting requests.
1596 */
1597int blk_rq_check_limits(struct request_queue *q, struct request *rq)
1598{
Martin K. Petersenae03bf62009-05-22 17:17:50 -04001599 if (blk_rq_sectors(rq) > queue_max_sectors(q) ||
1600 blk_rq_bytes(rq) > queue_max_hw_sectors(q) << 9) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001601 printk(KERN_ERR "%s: over max size limit.\n", __func__);
1602 return -EIO;
1603 }
1604
1605 /*
1606 * queue's settings related to segment counting like q->bounce_pfn
1607 * may differ from that of other stacking queues.
1608 * Recalculate it to check the request correctly on this queue's
1609 * limitation.
1610 */
1611 blk_recalc_rq_segments(rq);
Martin K. Petersenae03bf62009-05-22 17:17:50 -04001612 if (rq->nr_phys_segments > queue_max_phys_segments(q) ||
1613 rq->nr_phys_segments > queue_max_hw_segments(q)) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001614 printk(KERN_ERR "%s: over max segments limit.\n", __func__);
1615 return -EIO;
1616 }
1617
1618 return 0;
1619}
1620EXPORT_SYMBOL_GPL(blk_rq_check_limits);
1621
1622/**
1623 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
1624 * @q: the queue to submit the request
1625 * @rq: the request being queued
1626 */
1627int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
1628{
1629 unsigned long flags;
1630
1631 if (blk_rq_check_limits(q, rq))
1632 return -EIO;
1633
1634#ifdef CONFIG_FAIL_MAKE_REQUEST
1635 if (rq->rq_disk && rq->rq_disk->part0.make_it_fail &&
1636 should_fail(&fail_make_request, blk_rq_bytes(rq)))
1637 return -EIO;
1638#endif
1639
1640 spin_lock_irqsave(q->queue_lock, flags);
1641
1642 /*
1643 * Submitting request must be dequeued before calling this function
1644 * because it will be linked to another request_queue
1645 */
1646 BUG_ON(blk_queued_rq(rq));
1647
1648 drive_stat_acct(rq, 1);
1649 __elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 0);
1650
1651 spin_unlock_irqrestore(q->queue_lock, flags);
1652
1653 return 0;
1654}
1655EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
1656
Jens Axboebc58ba92009-01-23 10:54:44 +01001657static void blk_account_io_completion(struct request *req, unsigned int bytes)
1658{
Jens Axboec2553b52009-04-24 08:10:11 +02001659 if (blk_do_io_stat(req)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01001660 const int rw = rq_data_dir(req);
1661 struct hd_struct *part;
1662 int cpu;
1663
1664 cpu = part_stat_lock();
Tejun Heo83096eb2009-05-07 22:24:39 +09001665 part = disk_map_sector_rcu(req->rq_disk, blk_rq_pos(req));
Jens Axboebc58ba92009-01-23 10:54:44 +01001666 part_stat_add(cpu, part, sectors[rw], bytes >> 9);
1667 part_stat_unlock();
1668 }
1669}
1670
1671static void blk_account_io_done(struct request *req)
1672{
Jens Axboebc58ba92009-01-23 10:54:44 +01001673 /*
1674 * Account IO completion. bar_rq isn't accounted as a normal
1675 * IO on queueing nor completion. Accounting the containing
1676 * request is enough.
1677 */
Jens Axboec2553b52009-04-24 08:10:11 +02001678 if (blk_do_io_stat(req) && req != &req->q->bar_rq) {
Jens Axboebc58ba92009-01-23 10:54:44 +01001679 unsigned long duration = jiffies - req->start_time;
1680 const int rw = rq_data_dir(req);
1681 struct hd_struct *part;
1682 int cpu;
1683
1684 cpu = part_stat_lock();
Tejun Heo83096eb2009-05-07 22:24:39 +09001685 part = disk_map_sector_rcu(req->rq_disk, blk_rq_pos(req));
Jens Axboebc58ba92009-01-23 10:54:44 +01001686
1687 part_stat_inc(cpu, part, ios[rw]);
1688 part_stat_add(cpu, part, ticks[rw], duration);
1689 part_round_stats(cpu, part);
1690 part_dec_in_flight(part);
1691
1692 part_stat_unlock();
1693 }
1694}
1695
Tejun Heo53a08802008-12-03 12:41:26 +01001696/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09001697 * blk_peek_request - peek at the top of a request queue
1698 * @q: request queue to peek at
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001699 *
1700 * Description:
Tejun Heo9934c8c2009-05-08 11:54:16 +09001701 * Return the request at the top of @q. The returned request
1702 * should be started using blk_start_request() before LLD starts
1703 * processing it.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001704 *
1705 * Return:
Tejun Heo9934c8c2009-05-08 11:54:16 +09001706 * Pointer to the request at the top of @q if available. Null
1707 * otherwise.
1708 *
1709 * Context:
1710 * queue_lock must be held.
1711 */
1712struct request *blk_peek_request(struct request_queue *q)
Tejun Heo158dbda2009-04-23 11:05:18 +09001713{
1714 struct request *rq;
1715 int ret;
1716
1717 while ((rq = __elv_next_request(q)) != NULL) {
1718 if (!(rq->cmd_flags & REQ_STARTED)) {
1719 /*
1720 * This is the first time the device driver
1721 * sees this request (possibly after
1722 * requeueing). Notify IO scheduler.
1723 */
1724 if (blk_sorted_rq(rq))
1725 elv_activate_rq(q, rq);
1726
1727 /*
1728 * just mark as started even if we don't start
1729 * it, a request that has been delayed should
1730 * not be passed by new incoming requests
1731 */
1732 rq->cmd_flags |= REQ_STARTED;
1733 trace_block_rq_issue(q, rq);
1734 }
1735
1736 if (!q->boundary_rq || q->boundary_rq == rq) {
1737 q->end_sector = rq_end_sector(rq);
1738 q->boundary_rq = NULL;
1739 }
1740
1741 if (rq->cmd_flags & REQ_DONTPREP)
1742 break;
1743
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001744 if (q->dma_drain_size && blk_rq_bytes(rq)) {
Tejun Heo158dbda2009-04-23 11:05:18 +09001745 /*
1746 * make sure space for the drain appears we
1747 * know we can do this because max_hw_segments
1748 * has been adjusted to be one fewer than the
1749 * device can handle
1750 */
1751 rq->nr_phys_segments++;
1752 }
1753
1754 if (!q->prep_rq_fn)
1755 break;
1756
1757 ret = q->prep_rq_fn(q, rq);
1758 if (ret == BLKPREP_OK) {
1759 break;
1760 } else if (ret == BLKPREP_DEFER) {
1761 /*
1762 * the request may have been (partially) prepped.
1763 * we need to keep this request in the front to
1764 * avoid resource deadlock. REQ_STARTED will
1765 * prevent other fs requests from passing this one.
1766 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001767 if (q->dma_drain_size && blk_rq_bytes(rq) &&
Tejun Heo158dbda2009-04-23 11:05:18 +09001768 !(rq->cmd_flags & REQ_DONTPREP)) {
1769 /*
1770 * remove the space for the drain we added
1771 * so that we don't add it again
1772 */
1773 --rq->nr_phys_segments;
1774 }
1775
1776 rq = NULL;
1777 break;
1778 } else if (ret == BLKPREP_KILL) {
1779 rq->cmd_flags |= REQ_QUIET;
James Bottomleyc143dc92009-05-30 06:43:49 +02001780 /*
1781 * Mark this request as started so we don't trigger
1782 * any debug logic in the end I/O path.
1783 */
1784 blk_start_request(rq);
Tejun Heo40cbbb72009-04-23 11:05:19 +09001785 __blk_end_request_all(rq, -EIO);
Tejun Heo158dbda2009-04-23 11:05:18 +09001786 } else {
1787 printk(KERN_ERR "%s: bad return=%d\n", __func__, ret);
1788 break;
1789 }
1790 }
1791
1792 return rq;
1793}
Tejun Heo9934c8c2009-05-08 11:54:16 +09001794EXPORT_SYMBOL(blk_peek_request);
Tejun Heo158dbda2009-04-23 11:05:18 +09001795
Tejun Heo9934c8c2009-05-08 11:54:16 +09001796void blk_dequeue_request(struct request *rq)
Tejun Heo158dbda2009-04-23 11:05:18 +09001797{
Tejun Heo9934c8c2009-05-08 11:54:16 +09001798 struct request_queue *q = rq->q;
1799
Tejun Heo158dbda2009-04-23 11:05:18 +09001800 BUG_ON(list_empty(&rq->queuelist));
1801 BUG_ON(ELV_ON_HASH(rq));
1802
1803 list_del_init(&rq->queuelist);
1804
1805 /*
1806 * the time frame between a request being removed from the lists
1807 * and to it is freed is accounted as io that is in progress at
1808 * the driver side.
1809 */
1810 if (blk_account_rq(rq))
Jens Axboe0a7ae2f2009-05-20 08:54:31 +02001811 q->in_flight[rq_is_sync(rq)]++;
Tejun Heo158dbda2009-04-23 11:05:18 +09001812}
1813
Tejun Heo5efccd12009-04-23 11:05:18 +09001814/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09001815 * blk_start_request - start request processing on the driver
1816 * @req: request to dequeue
1817 *
1818 * Description:
1819 * Dequeue @req and start timeout timer on it. This hands off the
1820 * request to the driver.
1821 *
1822 * Block internal functions which don't want to start timer should
1823 * call blk_dequeue_request().
1824 *
1825 * Context:
1826 * queue_lock must be held.
1827 */
1828void blk_start_request(struct request *req)
1829{
1830 blk_dequeue_request(req);
1831
1832 /*
Tejun Heo5f49f632009-05-19 18:33:05 +09001833 * We are now handing the request to the hardware, initialize
1834 * resid_len to full count and add the timeout handler.
Tejun Heo9934c8c2009-05-08 11:54:16 +09001835 */
Tejun Heo5f49f632009-05-19 18:33:05 +09001836 req->resid_len = blk_rq_bytes(req);
FUJITA Tomonoridbb66c42009-06-09 05:47:10 +02001837 if (unlikely(blk_bidi_rq(req)))
1838 req->next_rq->resid_len = blk_rq_bytes(req->next_rq);
1839
Tejun Heo9934c8c2009-05-08 11:54:16 +09001840 blk_add_timer(req);
1841}
1842EXPORT_SYMBOL(blk_start_request);
1843
1844/**
1845 * blk_fetch_request - fetch a request from a request queue
1846 * @q: request queue to fetch a request from
1847 *
1848 * Description:
1849 * Return the request at the top of @q. The request is started on
1850 * return and LLD can start processing it immediately.
1851 *
1852 * Return:
1853 * Pointer to the request at the top of @q if available. Null
1854 * otherwise.
1855 *
1856 * Context:
1857 * queue_lock must be held.
1858 */
1859struct request *blk_fetch_request(struct request_queue *q)
1860{
1861 struct request *rq;
1862
1863 rq = blk_peek_request(q);
1864 if (rq)
1865 blk_start_request(rq);
1866 return rq;
1867}
1868EXPORT_SYMBOL(blk_fetch_request);
1869
1870/**
Tejun Heo2e60e022009-04-23 11:05:18 +09001871 * blk_update_request - Special helper function for request stacking drivers
Randy Dunlap8ebf9752009-06-11 20:00:41 -07001872 * @req: the request being processed
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001873 * @error: %0 for success, < %0 for error
Randy Dunlap8ebf9752009-06-11 20:00:41 -07001874 * @nr_bytes: number of bytes to complete @req
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001875 *
1876 * Description:
Randy Dunlap8ebf9752009-06-11 20:00:41 -07001877 * Ends I/O on a number of bytes attached to @req, but doesn't complete
1878 * the request structure even if @req doesn't have leftover.
1879 * If @req has leftover, sets it up for the next range of segments.
Tejun Heo2e60e022009-04-23 11:05:18 +09001880 *
1881 * This special helper function is only for request stacking drivers
1882 * (e.g. request-based dm) so that they can handle partial completion.
1883 * Actual device drivers should use blk_end_request instead.
1884 *
1885 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
1886 * %false return from this function.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001887 *
1888 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09001889 * %false - this request doesn't have any more data
1890 * %true - this request has more data
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001891 **/
Tejun Heo2e60e022009-04-23 11:05:18 +09001892bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893{
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001894 int total_bytes, bio_nbytes, next_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 struct bio *bio;
1896
Tejun Heo2e60e022009-04-23 11:05:18 +09001897 if (!req->bio)
1898 return false;
1899
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001900 trace_block_rq_complete(req->q, req);
Jens Axboe2056a782006-03-23 20:00:26 +01001901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 /*
Tejun Heo6f414692009-04-19 07:00:41 +09001903 * For fs requests, rq is just carrier of independent bio's
1904 * and each partial completion should be handled separately.
1905 * Reset per-request error on each partial completion.
1906 *
1907 * TODO: tj: This is too subtle. It would be better to let
1908 * low level drivers do what they see fit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 */
Tejun Heo6f414692009-04-19 07:00:41 +09001910 if (blk_fs_request(req))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 req->errors = 0;
1912
Jens Axboe6728cb02008-01-31 13:03:55 +01001913 if (error && (blk_fs_request(req) && !(req->cmd_flags & REQ_QUIET))) {
1914 printk(KERN_ERR "end_request: I/O error, dev %s, sector %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 req->rq_disk ? req->rq_disk->disk_name : "?",
Tejun Heo83096eb2009-05-07 22:24:39 +09001916 (unsigned long long)blk_rq_pos(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 }
1918
Jens Axboebc58ba92009-01-23 10:54:44 +01001919 blk_account_io_completion(req, nr_bytes);
Jens Axboed72d9042005-11-01 08:35:42 +01001920
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 total_bytes = bio_nbytes = 0;
1922 while ((bio = req->bio) != NULL) {
1923 int nbytes;
1924
1925 if (nr_bytes >= bio->bi_size) {
1926 req->bio = bio->bi_next;
1927 nbytes = bio->bi_size;
NeilBrown5bb23a62007-09-27 12:46:13 +02001928 req_bio_endio(req, bio, nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 next_idx = 0;
1930 bio_nbytes = 0;
1931 } else {
1932 int idx = bio->bi_idx + next_idx;
1933
Kazuhisa Ichikawaaf498d72009-05-12 13:27:45 +02001934 if (unlikely(idx >= bio->bi_vcnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 blk_dump_rq_flags(req, "__end_that");
Jens Axboe6728cb02008-01-31 13:03:55 +01001936 printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
Kazuhisa Ichikawaaf498d72009-05-12 13:27:45 +02001937 __func__, idx, bio->bi_vcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 break;
1939 }
1940
1941 nbytes = bio_iovec_idx(bio, idx)->bv_len;
1942 BIO_BUG_ON(nbytes > bio->bi_size);
1943
1944 /*
1945 * not a complete bvec done
1946 */
1947 if (unlikely(nbytes > nr_bytes)) {
1948 bio_nbytes += nr_bytes;
1949 total_bytes += nr_bytes;
1950 break;
1951 }
1952
1953 /*
1954 * advance to the next vector
1955 */
1956 next_idx++;
1957 bio_nbytes += nbytes;
1958 }
1959
1960 total_bytes += nbytes;
1961 nr_bytes -= nbytes;
1962
Jens Axboe6728cb02008-01-31 13:03:55 +01001963 bio = req->bio;
1964 if (bio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 /*
1966 * end more in this run, or just return 'not-done'
1967 */
1968 if (unlikely(nr_bytes <= 0))
1969 break;
1970 }
1971 }
1972
1973 /*
1974 * completely done
1975 */
Tejun Heo2e60e022009-04-23 11:05:18 +09001976 if (!req->bio) {
1977 /*
1978 * Reset counters so that the request stacking driver
1979 * can find how many bytes remain in the request
1980 * later.
1981 */
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001982 req->__data_len = 0;
Tejun Heo2e60e022009-04-23 11:05:18 +09001983 return false;
1984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
1986 /*
1987 * if the request wasn't completed, update state
1988 */
1989 if (bio_nbytes) {
NeilBrown5bb23a62007-09-27 12:46:13 +02001990 req_bio_endio(req, bio, bio_nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 bio->bi_idx += next_idx;
1992 bio_iovec(bio)->bv_offset += nr_bytes;
1993 bio_iovec(bio)->bv_len -= nr_bytes;
1994 }
1995
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001996 req->__data_len -= total_bytes;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001997 req->buffer = bio_data(req->bio);
1998
1999 /* update sector only for requests with clear definition of sector */
2000 if (blk_fs_request(req) || blk_discard_rq(req))
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002001 req->__sector += total_bytes >> 9;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002002
2003 /*
2004 * If total number of sectors is less than the first segment
2005 * size, something has gone terribly wrong.
2006 */
2007 if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
2008 printk(KERN_ERR "blk: request botched\n");
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002009 req->__data_len = blk_rq_cur_bytes(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002010 }
2011
2012 /* recalculate the number of segments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 blk_recalc_rq_segments(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002014
Tejun Heo2e60e022009-04-23 11:05:18 +09002015 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016}
Tejun Heo2e60e022009-04-23 11:05:18 +09002017EXPORT_SYMBOL_GPL(blk_update_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Tejun Heo2e60e022009-04-23 11:05:18 +09002019static bool blk_update_bidi_request(struct request *rq, int error,
2020 unsigned int nr_bytes,
2021 unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002022{
Tejun Heo2e60e022009-04-23 11:05:18 +09002023 if (blk_update_request(rq, error, nr_bytes))
2024 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002025
Tejun Heo2e60e022009-04-23 11:05:18 +09002026 /* Bidi request must be completed as a whole */
2027 if (unlikely(blk_bidi_rq(rq)) &&
2028 blk_update_request(rq->next_rq, error, bidi_bytes))
2029 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002030
Tejun Heo2e60e022009-04-23 11:05:18 +09002031 add_disk_randomness(rq->rq_disk);
2032
2033 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034}
2035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036/*
2037 * queue lock must be held
2038 */
Tejun Heo2e60e022009-04-23 11:05:18 +09002039static void blk_finish_request(struct request *req, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002041 if (blk_rq_tagged(req))
2042 blk_queue_end_tag(req->q, req);
2043
James Bottomleyba396a62009-05-27 14:17:08 +02002044 BUG_ON(blk_queued_rq(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
2046 if (unlikely(laptop_mode) && blk_fs_request(req))
2047 laptop_io_completion();
2048
Mike Andersone78042e2008-10-30 02:16:20 -07002049 blk_delete_timer(req);
2050
Jens Axboebc58ba92009-01-23 10:54:44 +01002051 blk_account_io_done(req);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002052
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 if (req->end_io)
Tejun Heo8ffdc652006-01-06 09:49:03 +01002054 req->end_io(req, error);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002055 else {
2056 if (blk_bidi_rq(req))
2057 __blk_put_request(req->next_rq->q, req->next_rq);
2058
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 __blk_put_request(req->q, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 }
2061}
2062
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05002063/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002064 * blk_end_bidi_request - Complete a bidi request
2065 * @rq: the request to complete
Randy Dunlap710027a2008-08-19 20:13:11 +02002066 * @error: %0 for success, < %0 for error
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002067 * @nr_bytes: number of bytes to complete @rq
2068 * @bidi_bytes: number of bytes to complete @rq->next_rq
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002069 *
2070 * Description:
2071 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
Tejun Heo2e60e022009-04-23 11:05:18 +09002072 * Drivers that supports bidi can safely call this member for any
2073 * type of request, bidi or uni. In the later case @bidi_bytes is
2074 * just ignored.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002075 *
2076 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002077 * %false - we are done with this request
2078 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002079 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002080static bool blk_end_bidi_request(struct request *rq, int error,
2081 unsigned int nr_bytes, unsigned int bidi_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002082{
2083 struct request_queue *q = rq->q;
Tejun Heo2e60e022009-04-23 11:05:18 +09002084 unsigned long flags;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002085
Tejun Heo2e60e022009-04-23 11:05:18 +09002086 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2087 return true;
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002088
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002089 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo2e60e022009-04-23 11:05:18 +09002090 blk_finish_request(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002091 spin_unlock_irqrestore(q->queue_lock, flags);
2092
Tejun Heo2e60e022009-04-23 11:05:18 +09002093 return false;
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002094}
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002095
2096/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002097 * __blk_end_bidi_request - Complete a bidi request with queue lock held
2098 * @rq: the request to complete
2099 * @error: %0 for success, < %0 for error
2100 * @nr_bytes: number of bytes to complete @rq
2101 * @bidi_bytes: number of bytes to complete @rq->next_rq
Tejun Heo5efccd12009-04-23 11:05:18 +09002102 *
2103 * Description:
Tejun Heo2e60e022009-04-23 11:05:18 +09002104 * Identical to blk_end_bidi_request() except that queue lock is
2105 * assumed to be locked on entry and remains so on return.
Tejun Heo5efccd12009-04-23 11:05:18 +09002106 *
Tejun Heo2e60e022009-04-23 11:05:18 +09002107 * Return:
2108 * %false - we are done with this request
2109 * %true - still buffers pending for this request
Tejun Heo5efccd12009-04-23 11:05:18 +09002110 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002111static bool __blk_end_bidi_request(struct request *rq, int error,
2112 unsigned int nr_bytes, unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002113{
Tejun Heo2e60e022009-04-23 11:05:18 +09002114 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2115 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002116
Tejun Heo2e60e022009-04-23 11:05:18 +09002117 blk_finish_request(rq, error);
Tejun Heo5efccd12009-04-23 11:05:18 +09002118
Tejun Heo2e60e022009-04-23 11:05:18 +09002119 return false;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002120}
2121
2122/**
2123 * blk_end_request - Helper function for drivers to complete the request.
2124 * @rq: the request being processed
2125 * @error: %0 for success, < %0 for error
2126 * @nr_bytes: number of bytes to complete
2127 *
2128 * Description:
2129 * Ends I/O on a number of bytes attached to @rq.
2130 * If @rq has leftover, sets it up for the next range of segments.
2131 *
2132 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002133 * %false - we are done with this request
2134 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002135 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002136bool blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002137{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002138 return blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002139}
Jens Axboe56ad1742009-07-28 22:11:24 +02002140EXPORT_SYMBOL(blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002141
2142/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002143 * blk_end_request_all - Helper function for drives to finish the request.
2144 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002145 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002146 *
2147 * Description:
2148 * Completely finish @rq.
2149 */
2150void blk_end_request_all(struct request *rq, int error)
2151{
2152 bool pending;
2153 unsigned int bidi_bytes = 0;
2154
2155 if (unlikely(blk_bidi_rq(rq)))
2156 bidi_bytes = blk_rq_bytes(rq->next_rq);
2157
2158 pending = blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2159 BUG_ON(pending);
2160}
Jens Axboe56ad1742009-07-28 22:11:24 +02002161EXPORT_SYMBOL(blk_end_request_all);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002162
2163/**
2164 * blk_end_request_cur - Helper function to finish the current request chunk.
2165 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002166 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002167 *
2168 * Description:
2169 * Complete the current consecutively mapped chunk from @rq.
2170 *
2171 * Return:
2172 * %false - we are done with this request
2173 * %true - still buffers pending for this request
2174 */
2175bool blk_end_request_cur(struct request *rq, int error)
2176{
2177 return blk_end_request(rq, error, blk_rq_cur_bytes(rq));
2178}
Jens Axboe56ad1742009-07-28 22:11:24 +02002179EXPORT_SYMBOL(blk_end_request_cur);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002180
2181/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002182 * __blk_end_request - Helper function for drivers to complete the request.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002183 * @rq: the request being processed
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002184 * @error: %0 for success, < %0 for error
2185 * @nr_bytes: number of bytes to complete
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002186 *
2187 * Description:
2188 * Must be called with queue lock held unlike blk_end_request().
2189 *
2190 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002191 * %false - we are done with this request
2192 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002193 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002194bool __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002195{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002196 return __blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002197}
Jens Axboe56ad1742009-07-28 22:11:24 +02002198EXPORT_SYMBOL(__blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002199
2200/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002201 * __blk_end_request_all - Helper function for drives to finish the request.
2202 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002203 * @error: %0 for success, < %0 for error
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002204 *
2205 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002206 * Completely finish @rq. Must be called with queue lock held.
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002207 */
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002208void __blk_end_request_all(struct request *rq, int error)
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002209{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002210 bool pending;
2211 unsigned int bidi_bytes = 0;
2212
2213 if (unlikely(blk_bidi_rq(rq)))
2214 bidi_bytes = blk_rq_bytes(rq->next_rq);
2215
2216 pending = __blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2217 BUG_ON(pending);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002218}
Jens Axboe56ad1742009-07-28 22:11:24 +02002219EXPORT_SYMBOL(__blk_end_request_all);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002220
2221/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002222 * __blk_end_request_cur - Helper function to finish the current request chunk.
2223 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002224 * @error: %0 for success, < %0 for error
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002225 *
2226 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002227 * Complete the current consecutively mapped chunk from @rq. Must
2228 * be called with queue lock held.
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002229 *
2230 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002231 * %false - we are done with this request
2232 * %true - still buffers pending for this request
2233 */
2234bool __blk_end_request_cur(struct request *rq, int error)
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002235{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002236 return __blk_end_request(rq, error, blk_rq_cur_bytes(rq));
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002237}
Jens Axboe56ad1742009-07-28 22:11:24 +02002238EXPORT_SYMBOL(__blk_end_request_cur);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002239
Jens Axboe86db1e22008-01-29 14:53:40 +01002240void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2241 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242{
David Woodhoused628eae2008-08-09 16:22:17 +01002243 /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw, and
2244 we want BIO_RW_AHEAD (bit 1) to imply REQ_FAILFAST (bit 1). */
Jens Axboe4aff5e22006-08-10 08:44:47 +02002245 rq->cmd_flags |= (bio->bi_rw & 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
David Woodhousefb2dce82008-08-05 18:01:53 +01002247 if (bio_has_data(bio)) {
2248 rq->nr_phys_segments = bio_phys_segments(q, bio);
David Woodhousefb2dce82008-08-05 18:01:53 +01002249 rq->buffer = bio_data(bio);
2250 }
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002251 rq->__data_len = bio->bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 rq->bio = rq->biotail = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
NeilBrown66846572007-08-16 13:31:28 +02002254 if (bio->bi_bdev)
2255 rq->rq_disk = bio->bi_bdev->bd_disk;
2256}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
Kiyoshi Uedaef9e3fa2008-10-01 16:12:15 +02002258/**
2259 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
2260 * @q : the queue of the device being checked
2261 *
2262 * Description:
2263 * Check if underlying low-level drivers of a device are busy.
2264 * If the drivers want to export their busy state, they must set own
2265 * exporting function using blk_queue_lld_busy() first.
2266 *
2267 * Basically, this function is used only by request stacking drivers
2268 * to stop dispatching requests to underlying devices when underlying
2269 * devices are busy. This behavior helps more I/O merging on the queue
2270 * of the request stacking driver and prevents I/O throughput regression
2271 * on burst I/O load.
2272 *
2273 * Return:
2274 * 0 - Not busy (The request stacking driver should dispatch request)
2275 * 1 - Busy (The request stacking driver should stop dispatching request)
2276 */
2277int blk_lld_busy(struct request_queue *q)
2278{
2279 if (q->lld_busy_fn)
2280 return q->lld_busy_fn(q);
2281
2282 return 0;
2283}
2284EXPORT_SYMBOL_GPL(blk_lld_busy);
2285
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002286/**
2287 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
2288 * @rq: the clone request to be cleaned up
2289 *
2290 * Description:
2291 * Free all bios in @rq for a cloned request.
2292 */
2293void blk_rq_unprep_clone(struct request *rq)
2294{
2295 struct bio *bio;
2296
2297 while ((bio = rq->bio) != NULL) {
2298 rq->bio = bio->bi_next;
2299
2300 bio_put(bio);
2301 }
2302}
2303EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
2304
2305/*
2306 * Copy attributes of the original request to the clone request.
2307 * The actual data parts (e.g. ->cmd, ->buffer, ->sense) are not copied.
2308 */
2309static void __blk_rq_prep_clone(struct request *dst, struct request *src)
2310{
2311 dst->cpu = src->cpu;
2312 dst->cmd_flags = (rq_data_dir(src) | REQ_NOMERGE);
2313 dst->cmd_type = src->cmd_type;
2314 dst->__sector = blk_rq_pos(src);
2315 dst->__data_len = blk_rq_bytes(src);
2316 dst->nr_phys_segments = src->nr_phys_segments;
2317 dst->ioprio = src->ioprio;
2318 dst->extra_len = src->extra_len;
2319}
2320
2321/**
2322 * blk_rq_prep_clone - Helper function to setup clone request
2323 * @rq: the request to be setup
2324 * @rq_src: original request to be cloned
2325 * @bs: bio_set that bios for clone are allocated from
2326 * @gfp_mask: memory allocation mask for bio
2327 * @bio_ctr: setup function to be called for each clone bio.
2328 * Returns %0 for success, non %0 for failure.
2329 * @data: private data to be passed to @bio_ctr
2330 *
2331 * Description:
2332 * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
2333 * The actual data parts of @rq_src (e.g. ->cmd, ->buffer, ->sense)
2334 * are not copied, and copying such parts is the caller's responsibility.
2335 * Also, pages which the original bios are pointing to are not copied
2336 * and the cloned bios just point same pages.
2337 * So cloned bios must be completed before original bios, which means
2338 * the caller must complete @rq before @rq_src.
2339 */
2340int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
2341 struct bio_set *bs, gfp_t gfp_mask,
2342 int (*bio_ctr)(struct bio *, struct bio *, void *),
2343 void *data)
2344{
2345 struct bio *bio, *bio_src;
2346
2347 if (!bs)
2348 bs = fs_bio_set;
2349
2350 blk_rq_init(NULL, rq);
2351
2352 __rq_for_each_bio(bio_src, rq_src) {
2353 bio = bio_alloc_bioset(gfp_mask, bio_src->bi_max_vecs, bs);
2354 if (!bio)
2355 goto free_and_out;
2356
2357 __bio_clone(bio, bio_src);
2358
2359 if (bio_integrity(bio_src) &&
Martin K. Petersen7878cba2009-06-26 15:37:49 +02002360 bio_integrity_clone(bio, bio_src, gfp_mask, bs))
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002361 goto free_and_out;
2362
2363 if (bio_ctr && bio_ctr(bio, bio_src, data))
2364 goto free_and_out;
2365
2366 if (rq->bio) {
2367 rq->biotail->bi_next = bio;
2368 rq->biotail = bio;
2369 } else
2370 rq->bio = rq->biotail = bio;
2371 }
2372
2373 __blk_rq_prep_clone(rq, rq_src);
2374
2375 return 0;
2376
2377free_and_out:
2378 if (bio)
2379 bio_free(bio, bs);
2380 blk_rq_unprep_clone(rq);
2381
2382 return -ENOMEM;
2383}
2384EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
2385
Jens Axboe18887ad2008-07-28 13:08:45 +02002386int kblockd_schedule_work(struct request_queue *q, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387{
2388 return queue_work(kblockd_workqueue, work);
2389}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390EXPORT_SYMBOL(kblockd_schedule_work);
2391
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392int __init blk_dev_init(void)
2393{
Nikanth Karthikesan9eb55b02009-04-27 14:53:54 +02002394 BUILD_BUG_ON(__REQ_NR_BITS > 8 *
2395 sizeof(((struct request *)0)->cmd_flags));
2396
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 kblockd_workqueue = create_workqueue("kblockd");
2398 if (!kblockd_workqueue)
2399 panic("Failed to create kblockd\n");
2400
2401 request_cachep = kmem_cache_create("blkdev_requests",
Paul Mundt20c2df82007-07-20 10:11:58 +09002402 sizeof(struct request), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
Jens Axboe8324aa92008-01-29 14:51:59 +01002404 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02002405 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 return 0;
2408}
2409