blob: e958c7a1e462d3b296f7457410562bca50b3875a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 1994, Karl Keyte: Added support for disk statistics
4 * Elevator latency, (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
5 * Queue request tables / lock, selectable elevator, Jens Axboe <axboe@suse.de>
Jens Axboe6728cb02008-01-31 13:03:55 +01006 * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au>
7 * - July2000
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
9 */
10
11/*
12 * This handles all read/write requests to block devices
13 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/backing-dev.h>
17#include <linux/bio.h>
18#include <linux/blkdev.h>
19#include <linux/highmem.h>
20#include <linux/mm.h>
21#include <linux/kernel_stat.h>
22#include <linux/string.h>
23#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/completion.h>
25#include <linux/slab.h>
26#include <linux/swap.h>
27#include <linux/writeback.h>
Andrew Mortonfaccbd4b2006-12-10 02:19:35 -080028#include <linux/task_io_accounting_ops.h>
Akinobu Mitac17bb492006-12-08 02:39:46 -080029#include <linux/fault-inject.h>
Li Zefan55782132009-06-09 13:43:05 +080030
31#define CREATE_TRACE_POINTS
32#include <trace/events/block.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Jens Axboe8324aa92008-01-29 14:51:59 +010034#include "blk.h"
35
Mike Snitzerd07335e2010-11-16 12:52:38 +010036EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_remap);
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +020037EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_remap);
Li Zefan55782132009-06-09 13:43:05 +080038EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_complete);
Ingo Molnar0bfc2452008-11-26 11:59:56 +010039
Jens Axboe165125e2007-07-24 09:28:11 +020040static int __make_request(struct request_queue *q, struct bio *bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/*
43 * For the allocated request tables
44 */
Adrian Bunk5ece6c52008-02-18 13:45:51 +010045static struct kmem_cache *request_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47/*
48 * For queue allocation
49 */
Jens Axboe6728cb02008-01-31 13:03:55 +010050struct kmem_cache *blk_requestq_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 * Controlling structure to kblockd
54 */
Jens Axboeff856ba2006-01-09 16:02:34 +010055static struct workqueue_struct *kblockd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Jens Axboe26b82562008-01-29 13:54:41 +010057static void drive_stat_acct(struct request *rq, int new_io)
58{
Jens Axboe28f13702008-05-07 10:15:46 +020059 struct hd_struct *part;
Jens Axboe26b82562008-01-29 13:54:41 +010060 int rw = rq_data_dir(rq);
Tejun Heoc9959052008-08-25 19:47:21 +090061 int cpu;
Jens Axboe26b82562008-01-29 13:54:41 +010062
Jens Axboec2553b52009-04-24 08:10:11 +020063 if (!blk_do_io_stat(rq))
Jens Axboe26b82562008-01-29 13:54:41 +010064 return;
65
Tejun Heo074a7ac2008-08-25 19:56:14 +090066 cpu = part_stat_lock();
Tejun Heoc9959052008-08-25 19:47:21 +090067
Jerome Marchand09e099d2011-01-05 16:57:38 +010068 if (!new_io) {
69 part = rq->part;
Tejun Heo074a7ac2008-08-25 19:56:14 +090070 part_stat_inc(cpu, part, merges[rw]);
Jerome Marchand09e099d2011-01-05 16:57:38 +010071 } else {
72 part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq));
Jens Axboe6c23a962011-01-07 08:43:37 +010073 if (!hd_struct_try_get(part)) {
Jerome Marchand09e099d2011-01-05 16:57:38 +010074 /*
75 * The partition is already being removed,
76 * the request will be accounted on the disk only
77 *
78 * We take a reference on disk->part0 although that
79 * partition will never be deleted, so we can treat
80 * it as any other partition.
81 */
82 part = &rq->rq_disk->part0;
Jens Axboe6c23a962011-01-07 08:43:37 +010083 hd_struct_get(part);
Jerome Marchand09e099d2011-01-05 16:57:38 +010084 }
Tejun Heo074a7ac2008-08-25 19:56:14 +090085 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +020086 part_inc_in_flight(part, rw);
Jerome Marchand09e099d2011-01-05 16:57:38 +010087 rq->part = part;
Jens Axboe26b82562008-01-29 13:54:41 +010088 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +020089
Tejun Heo074a7ac2008-08-25 19:56:14 +090090 part_stat_unlock();
Jens Axboe26b82562008-01-29 13:54:41 +010091}
92
Jens Axboe8324aa92008-01-29 14:51:59 +010093void blk_queue_congestion_threshold(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
95 int nr;
96
97 nr = q->nr_requests - (q->nr_requests / 8) + 1;
98 if (nr > q->nr_requests)
99 nr = q->nr_requests;
100 q->nr_congestion_on = nr;
101
102 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
103 if (nr < 1)
104 nr = 1;
105 q->nr_congestion_off = nr;
106}
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108/**
109 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
110 * @bdev: device
111 *
112 * Locates the passed device's request queue and returns the address of its
113 * backing_dev_info
114 *
115 * Will return NULL if the request queue cannot be located.
116 */
117struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
118{
119 struct backing_dev_info *ret = NULL;
Jens Axboe165125e2007-07-24 09:28:11 +0200120 struct request_queue *q = bdev_get_queue(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 if (q)
123 ret = &q->backing_dev_info;
124 return ret;
125}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126EXPORT_SYMBOL(blk_get_backing_dev_info);
127
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200128void blk_rq_init(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200130 memset(rq, 0, sizeof(*rq));
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 INIT_LIST_HEAD(&rq->queuelist);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700133 INIT_LIST_HEAD(&rq->timeout_list);
Jens Axboec7c22e42008-09-13 20:26:01 +0200134 rq->cpu = -1;
Jens Axboe63a71382008-02-08 12:41:03 +0100135 rq->q = q;
Tejun Heoa2dec7b2009-05-07 22:24:44 +0900136 rq->__sector = (sector_t) -1;
Jens Axboe2e662b62006-07-13 11:55:04 +0200137 INIT_HLIST_NODE(&rq->hash);
138 RB_CLEAR_NODE(&rq->rb_node);
FUJITA Tomonorid7e3c322008-04-29 09:54:39 +0200139 rq->cmd = rq->__cmd;
Li Zefane2494e12009-04-02 13:43:26 +0800140 rq->cmd_len = BLK_MAX_CDB;
Jens Axboe63a71382008-02-08 12:41:03 +0100141 rq->tag = -1;
Jens Axboe63a71382008-02-08 12:41:03 +0100142 rq->ref_count = 1;
Tejun Heob243ddc2009-04-23 11:05:18 +0900143 rq->start_time = jiffies;
Divyesh Shah91952912010-04-01 15:01:41 -0700144 set_start_time_ns(rq);
Jerome Marchand09e099d2011-01-05 16:57:38 +0100145 rq->part = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200147EXPORT_SYMBOL(blk_rq_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
NeilBrown5bb23a62007-09-27 12:46:13 +0200149static void req_bio_endio(struct request *rq, struct bio *bio,
150 unsigned int nbytes, int error)
Tejun Heo797e7db2006-01-06 09:51:03 +0100151{
Tejun Heo143a87f2011-01-25 12:43:52 +0100152 if (error)
153 clear_bit(BIO_UPTODATE, &bio->bi_flags);
154 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
155 error = -EIO;
Tejun Heo797e7db2006-01-06 09:51:03 +0100156
Tejun Heo143a87f2011-01-25 12:43:52 +0100157 if (unlikely(nbytes > bio->bi_size)) {
158 printk(KERN_ERR "%s: want %u bytes done, %u left\n",
159 __func__, nbytes, bio->bi_size);
160 nbytes = bio->bi_size;
NeilBrown5bb23a62007-09-27 12:46:13 +0200161 }
Tejun Heo143a87f2011-01-25 12:43:52 +0100162
163 if (unlikely(rq->cmd_flags & REQ_QUIET))
164 set_bit(BIO_QUIET, &bio->bi_flags);
165
166 bio->bi_size -= nbytes;
167 bio->bi_sector += (nbytes >> 9);
168
169 if (bio_integrity(bio))
170 bio_integrity_advance(bio, nbytes);
171
172 /* don't actually finish bio if it's part of flush sequence */
173 if (bio->bi_size == 0 && !(rq->cmd_flags & REQ_FLUSH_SEQ))
174 bio_endio(bio, error);
Tejun Heo797e7db2006-01-06 09:51:03 +0100175}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177void blk_dump_rq_flags(struct request *rq, char *msg)
178{
179 int bit;
180
Jens Axboe6728cb02008-01-31 13:03:55 +0100181 printk(KERN_INFO "%s: dev %s: type=%x, flags=%x\n", msg,
Jens Axboe4aff5e22006-08-10 08:44:47 +0200182 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
183 rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Tejun Heo83096eb2009-05-07 22:24:39 +0900185 printk(KERN_INFO " sector %llu, nr/cnr %u/%u\n",
186 (unsigned long long)blk_rq_pos(rq),
187 blk_rq_sectors(rq), blk_rq_cur_sectors(rq));
Tejun Heo731ec492009-04-23 11:05:20 +0900188 printk(KERN_INFO " bio %p, biotail %p, buffer %p, len %u\n",
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900189 rq->bio, rq->biotail, rq->buffer, blk_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200191 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
Jens Axboe6728cb02008-01-31 13:03:55 +0100192 printk(KERN_INFO " cdb: ");
FUJITA Tomonorid34c87e2008-04-29 14:37:52 +0200193 for (bit = 0; bit < BLK_MAX_CDB; bit++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 printk("%02x ", rq->cmd[bit]);
195 printk("\n");
196 }
197}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198EXPORT_SYMBOL(blk_dump_rq_flags);
199
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500200static void blk_delay_work(struct work_struct *work)
201{
202 struct request_queue *q;
203
204 q = container_of(work, struct request_queue, delay_work.work);
205 spin_lock_irq(q->queue_lock);
206 q->request_fn(q);
207 spin_unlock_irq(q->queue_lock);
208}
209
210/**
211 * blk_delay_queue - restart queueing after defined interval
212 * @q: The &struct request_queue in question
213 * @msecs: Delay in msecs
214 *
215 * Description:
216 * Sometimes queueing needs to be postponed for a little while, to allow
217 * resources to come back. This function will make sure that queueing is
218 * restarted around the specified time.
219 */
220void blk_delay_queue(struct request_queue *q, unsigned long msecs)
221{
222 schedule_delayed_work(&q->delay_work, msecs_to_jiffies(msecs));
223}
224EXPORT_SYMBOL(blk_delay_queue);
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226/*
227 * "plug" the device if there are no outstanding requests: this will
228 * force the transfer to start only after we have put all the requests
229 * on the list.
230 *
231 * This is called with interrupts off and no requests on the queue and
232 * with the queue lock held.
233 */
Jens Axboe165125e2007-07-24 09:28:11 +0200234void blk_plug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
236 WARN_ON(!irqs_disabled());
237
238 /*
239 * don't plug a stopped queue, it must be paired with blk_start_queue()
240 * which will restart the queueing
241 */
Coywolf Qi Hunt7daac492006-04-19 10:14:49 +0200242 if (blk_queue_stopped(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return;
244
Jens Axboee48ec692008-07-03 13:18:54 +0200245 if (!queue_flag_test_and_set(QUEUE_FLAG_PLUGGED, q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100247 trace_block_plug(q);
Jens Axboe2056a782006-03-23 20:00:26 +0100248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250EXPORT_SYMBOL(blk_plug_device);
251
Jens Axboe6c5e0c42008-08-01 20:31:32 +0200252/**
253 * blk_plug_device_unlocked - plug a device without queue lock held
254 * @q: The &struct request_queue to plug
255 *
256 * Description:
257 * Like @blk_plug_device(), but grabs the queue lock and disables
258 * interrupts.
259 **/
260void blk_plug_device_unlocked(struct request_queue *q)
261{
262 unsigned long flags;
263
264 spin_lock_irqsave(q->queue_lock, flags);
265 blk_plug_device(q);
266 spin_unlock_irqrestore(q->queue_lock, flags);
267}
268EXPORT_SYMBOL(blk_plug_device_unlocked);
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270/*
271 * remove the queue from the plugged list, if present. called with
272 * queue lock held and interrupts disabled.
273 */
Jens Axboe165125e2007-07-24 09:28:11 +0200274int blk_remove_plug(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 WARN_ON(!irqs_disabled());
277
Jens Axboee48ec692008-07-03 13:18:54 +0200278 if (!queue_flag_test_and_clear(QUEUE_FLAG_PLUGGED, q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return 0;
280
281 del_timer(&q->unplug_timer);
282 return 1;
283}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284EXPORT_SYMBOL(blk_remove_plug);
285
286/*
287 * remove the plug and let it rip..
288 */
Jens Axboe165125e2007-07-24 09:28:11 +0200289void __generic_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Coywolf Qi Hunt7daac492006-04-19 10:14:49 +0200291 if (unlikely(blk_queue_stopped(q)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return;
Jens Axboea31a9732008-10-17 13:58:29 +0200293 if (!blk_remove_plug(q) && !blk_queue_nonrot(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return;
295
Jens Axboe22e2c502005-06-27 10:55:12 +0200296 q->request_fn(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299/**
300 * generic_unplug_device - fire a request queue
Jens Axboe165125e2007-07-24 09:28:11 +0200301 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 *
303 * Description:
304 * Linux uses plugging to build bigger requests queues before letting
305 * the device have at them. If a queue is plugged, the I/O scheduler
306 * is still adding and merging requests on the queue. Once the queue
307 * gets unplugged, the request_fn defined for the queue is invoked and
308 * transfers started.
309 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200310void generic_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Jens Axboedbaf2c02008-05-07 09:48:17 +0200312 if (blk_queue_plugged(q)) {
313 spin_lock_irq(q->queue_lock);
314 __generic_unplug_device(q);
315 spin_unlock_irq(q->queue_lock);
316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318EXPORT_SYMBOL(generic_unplug_device);
319
320static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
321 struct page *page)
322{
Jens Axboe165125e2007-07-24 09:28:11 +0200323 struct request_queue *q = bdi->unplug_io_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500325 blk_unplug(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
Jens Axboe86db1e22008-01-29 14:53:40 +0100328void blk_unplug_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Jens Axboe165125e2007-07-24 09:28:11 +0200330 struct request_queue *q =
331 container_of(work, struct request_queue, unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100333 trace_block_unplug_io(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 q->unplug_fn(q);
335}
336
Jens Axboe86db1e22008-01-29 14:53:40 +0100337void blk_unplug_timeout(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Jens Axboe165125e2007-07-24 09:28:11 +0200339 struct request_queue *q = (struct request_queue *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100341 trace_block_unplug_timer(q);
Jens Axboe18887ad2008-07-28 13:08:45 +0200342 kblockd_schedule_work(q, &q->unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500345void blk_unplug(struct request_queue *q)
346{
347 /*
348 * devices don't necessarily have an ->unplug_fn defined
349 */
350 if (q->unplug_fn) {
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100351 trace_block_unplug_io(q);
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500352 q->unplug_fn(q);
353 }
354}
355EXPORT_SYMBOL(blk_unplug);
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357/**
358 * blk_start_queue - restart a previously stopped queue
Jens Axboe165125e2007-07-24 09:28:11 +0200359 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 *
361 * Description:
362 * blk_start_queue() will clear the stop flag on the queue, and call
363 * the request_fn for the queue if it was in a stopped state when
364 * entered. Also see blk_stop_queue(). Queue lock must be held.
365 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200366void blk_start_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200368 WARN_ON(!irqs_disabled());
369
Nick Piggin75ad23b2008-04-29 14:48:33 +0200370 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
Tejun Heoa538cd02009-04-23 11:05:17 +0900371 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373EXPORT_SYMBOL(blk_start_queue);
374
375/**
376 * blk_stop_queue - stop a queue
Jens Axboe165125e2007-07-24 09:28:11 +0200377 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 *
379 * Description:
380 * The Linux block layer assumes that a block driver will consume all
381 * entries on the request queue when the request_fn strategy is called.
382 * Often this will not happen, because of hardware limitations (queue
383 * depth settings). If a device driver gets a 'queue full' response,
384 * or if it simply chooses not to queue more I/O at one point, it can
385 * call this function to prevent the request_fn from being called until
386 * the driver has signalled it's ready to go again. This happens by calling
387 * blk_start_queue() to restart queue operations. Queue lock must be held.
388 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200389void blk_stop_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 blk_remove_plug(q);
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500392 cancel_delayed_work(&q->delay_work);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200393 queue_flag_set(QUEUE_FLAG_STOPPED, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395EXPORT_SYMBOL(blk_stop_queue);
396
397/**
398 * blk_sync_queue - cancel any pending callbacks on a queue
399 * @q: the queue
400 *
401 * Description:
402 * The block layer may perform asynchronous callback activity
403 * on a queue, such as calling the unplug function after a timeout.
404 * A block device may call blk_sync_queue to ensure that any
405 * such activity is cancelled, thus allowing it to release resources
Michael Opdenacker59c51592007-05-09 08:57:56 +0200406 * that the callbacks might use. The caller must already have made sure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 * that its ->make_request_fn will not re-add plugging prior to calling
408 * this function.
409 *
410 */
411void blk_sync_queue(struct request_queue *q)
412{
413 del_timer_sync(&q->unplug_timer);
Jens Axboe70ed28b2008-11-19 14:38:39 +0100414 del_timer_sync(&q->timeout);
Cheng Renquan64d01dc2008-12-03 12:41:39 +0100415 cancel_work_sync(&q->unplug_work);
Vivek Goyale43473b2010-09-15 17:06:35 -0400416 throtl_shutdown_timer_wq(q);
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500417 cancel_delayed_work_sync(&q->delay_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419EXPORT_SYMBOL(blk_sync_queue);
420
421/**
Jens Axboe80a4b582008-10-14 09:51:06 +0200422 * __blk_run_queue - run a single device queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200424 *
425 * Description:
426 * See @blk_run_queue. This variant must be called with the queue lock
427 * held and interrupts disabled.
428 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 */
Nick Piggin75ad23b2008-04-29 14:48:33 +0200430void __blk_run_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 blk_remove_plug(q);
Jens Axboedac07ec2006-05-11 08:20:16 +0200433
Tejun Heoa538cd02009-04-23 11:05:17 +0900434 if (unlikely(blk_queue_stopped(q)))
435 return;
436
437 if (elv_queue_empty(q))
438 return;
439
Jens Axboedac07ec2006-05-11 08:20:16 +0200440 /*
441 * Only recurse once to avoid overrunning the stack, let the unplug
442 * handling reinvoke the handler shortly if we already got there.
443 */
Tejun Heoa538cd02009-04-23 11:05:17 +0900444 if (!queue_flag_test_and_set(QUEUE_FLAG_REENTER, q)) {
445 q->request_fn(q);
446 queue_flag_clear(QUEUE_FLAG_REENTER, q);
447 } else {
448 queue_flag_set(QUEUE_FLAG_PLUGGED, q);
449 kblockd_schedule_work(q, &q->unplug_work);
450 }
Nick Piggin75ad23b2008-04-29 14:48:33 +0200451}
452EXPORT_SYMBOL(__blk_run_queue);
Jens Axboedac07ec2006-05-11 08:20:16 +0200453
Nick Piggin75ad23b2008-04-29 14:48:33 +0200454/**
455 * blk_run_queue - run a single device queue
456 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200457 *
458 * Description:
459 * Invoke request handling on this queue, if it has pending work to do.
Tejun Heoa7f55792009-04-23 11:05:17 +0900460 * May be used to restart queueing when a request has completed.
Nick Piggin75ad23b2008-04-29 14:48:33 +0200461 */
462void blk_run_queue(struct request_queue *q)
463{
464 unsigned long flags;
465
466 spin_lock_irqsave(q->queue_lock, flags);
467 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 spin_unlock_irqrestore(q->queue_lock, flags);
469}
470EXPORT_SYMBOL(blk_run_queue);
471
Jens Axboe165125e2007-07-24 09:28:11 +0200472void blk_put_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500473{
474 kobject_put(&q->kobj);
475}
Al Viro483f4af2006-03-18 18:34:37 -0500476
Jens Axboe6728cb02008-01-31 13:03:55 +0100477void blk_cleanup_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500478{
Jens Axboee3335de92008-09-18 09:22:54 -0700479 /*
480 * We know we have process context here, so we can be a little
481 * cautious and ensure that pending block actions on this device
482 * are done before moving on. Going into this function, we should
483 * not have processes doing IO to this device.
484 */
485 blk_sync_queue(q);
486
Matthew Garrett31373d02010-04-06 14:25:14 +0200487 del_timer_sync(&q->backing_dev_info.laptop_mode_wb_timer);
Al Viro483f4af2006-03-18 18:34:37 -0500488 mutex_lock(&q->sysfs_lock);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200489 queue_flag_set_unlocked(QUEUE_FLAG_DEAD, q);
Al Viro483f4af2006-03-18 18:34:37 -0500490 mutex_unlock(&q->sysfs_lock);
491
492 if (q->elevator)
493 elevator_exit(q->elevator);
494
495 blk_put_queue(q);
496}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497EXPORT_SYMBOL(blk_cleanup_queue);
498
Jens Axboe165125e2007-07-24 09:28:11 +0200499static int blk_init_free_list(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
501 struct request_list *rl = &q->rq;
502
Mike Snitzer1abec4f2010-05-25 13:15:15 -0400503 if (unlikely(rl->rq_pool))
504 return 0;
505
Jens Axboe1faa16d2009-04-06 14:48:01 +0200506 rl->count[BLK_RW_SYNC] = rl->count[BLK_RW_ASYNC] = 0;
507 rl->starved[BLK_RW_SYNC] = rl->starved[BLK_RW_ASYNC] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200508 rl->elvpriv = 0;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200509 init_waitqueue_head(&rl->wait[BLK_RW_SYNC]);
510 init_waitqueue_head(&rl->wait[BLK_RW_ASYNC]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Christoph Lameter19460892005-06-23 00:08:19 -0700512 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
513 mempool_free_slab, request_cachep, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 if (!rl->rq_pool)
516 return -ENOMEM;
517
518 return 0;
519}
520
Jens Axboe165125e2007-07-24 09:28:11 +0200521struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Christoph Lameter19460892005-06-23 00:08:19 -0700523 return blk_alloc_queue_node(gfp_mask, -1);
524}
525EXPORT_SYMBOL(blk_alloc_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Jens Axboe165125e2007-07-24 09:28:11 +0200527struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -0700528{
Jens Axboe165125e2007-07-24 09:28:11 +0200529 struct request_queue *q;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700530 int err;
Christoph Lameter19460892005-06-23 00:08:19 -0700531
Jens Axboe8324aa92008-01-29 14:51:59 +0100532 q = kmem_cache_alloc_node(blk_requestq_cachep,
Christoph Lameter94f60302007-07-17 04:03:29 -0700533 gfp_mask | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (!q)
535 return NULL;
536
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700537 q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
538 q->backing_dev_info.unplug_io_data = q;
Jens Axboe0989a022009-06-12 14:42:56 +0200539 q->backing_dev_info.ra_pages =
540 (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
541 q->backing_dev_info.state = 0;
542 q->backing_dev_info.capabilities = BDI_CAP_MAP_COPY;
Jens Axboed9938312009-06-12 14:45:52 +0200543 q->backing_dev_info.name = "block";
Jens Axboe0989a022009-06-12 14:42:56 +0200544
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700545 err = bdi_init(&q->backing_dev_info);
546 if (err) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100547 kmem_cache_free(blk_requestq_cachep, q);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700548 return NULL;
549 }
550
Vivek Goyale43473b2010-09-15 17:06:35 -0400551 if (blk_throtl_init(q)) {
552 kmem_cache_free(blk_requestq_cachep, q);
553 return NULL;
554 }
555
Matthew Garrett31373d02010-04-06 14:25:14 +0200556 setup_timer(&q->backing_dev_info.laptop_mode_wb_timer,
557 laptop_mode_timer_fn, (unsigned long) q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 init_timer(&q->unplug_timer);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700559 setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
560 INIT_LIST_HEAD(&q->timeout_list);
Tejun Heoae1b1532011-01-25 12:43:54 +0100561 INIT_LIST_HEAD(&q->flush_queue[0]);
562 INIT_LIST_HEAD(&q->flush_queue[1]);
563 INIT_LIST_HEAD(&q->flush_data_in_flight);
Peter Zijlstra713ada92008-10-16 13:44:57 +0200564 INIT_WORK(&q->unplug_work, blk_unplug_work);
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500565 INIT_DELAYED_WORK(&q->delay_work, blk_delay_work);
Al Viro483f4af2006-03-18 18:34:37 -0500566
Jens Axboe8324aa92008-01-29 14:51:59 +0100567 kobject_init(&q->kobj, &blk_queue_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Al Viro483f4af2006-03-18 18:34:37 -0500569 mutex_init(&q->sysfs_lock);
Neil Browne7e72bf2008-05-14 16:05:54 -0700570 spin_lock_init(&q->__queue_lock);
Al Viro483f4af2006-03-18 18:34:37 -0500571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 return q;
573}
Christoph Lameter19460892005-06-23 00:08:19 -0700574EXPORT_SYMBOL(blk_alloc_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576/**
577 * blk_init_queue - prepare a request queue for use with a block device
578 * @rfn: The function to be called to process requests that have been
579 * placed on the queue.
580 * @lock: Request queue spin lock
581 *
582 * Description:
583 * If a block device wishes to use the standard request handling procedures,
584 * which sorts requests and coalesces adjacent requests, then it must
585 * call blk_init_queue(). The function @rfn will be called when there
586 * are requests on the queue that need to be processed. If the device
587 * supports plugging, then @rfn may not be called immediately when requests
588 * are available on the queue, but may be called at some time later instead.
589 * Plugged queues are generally unplugged when a buffer belonging to one
590 * of the requests on the queue is needed, or due to memory pressure.
591 *
592 * @rfn is not required, or even expected, to remove all requests off the
593 * queue, but only as many as it can handle at a time. If it does leave
594 * requests on the queue, it is responsible for arranging that the requests
595 * get dealt with eventually.
596 *
597 * The queue spin lock must be held while manipulating the requests on the
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200598 * request queue; this lock will be taken also from interrupt context, so irq
599 * disabling is needed for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 *
Randy Dunlap710027a2008-08-19 20:13:11 +0200601 * Function returns a pointer to the initialized request queue, or %NULL if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 * it didn't succeed.
603 *
604 * Note:
605 * blk_init_queue() must be paired with a blk_cleanup_queue() call
606 * when the block device is deactivated (such as at module unload).
607 **/
Christoph Lameter19460892005-06-23 00:08:19 -0700608
Jens Axboe165125e2007-07-24 09:28:11 +0200609struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Christoph Lameter19460892005-06-23 00:08:19 -0700611 return blk_init_queue_node(rfn, lock, -1);
612}
613EXPORT_SYMBOL(blk_init_queue);
614
Jens Axboe165125e2007-07-24 09:28:11 +0200615struct request_queue *
Christoph Lameter19460892005-06-23 00:08:19 -0700616blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
617{
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600618 struct request_queue *uninit_q, *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600620 uninit_q = blk_alloc_queue_node(GFP_KERNEL, node_id);
621 if (!uninit_q)
622 return NULL;
623
624 q = blk_init_allocated_queue_node(uninit_q, rfn, lock, node_id);
625 if (!q)
626 blk_cleanup_queue(uninit_q);
627
628 return q;
Mike Snitzer01effb02010-05-11 08:57:42 +0200629}
630EXPORT_SYMBOL(blk_init_queue_node);
631
632struct request_queue *
633blk_init_allocated_queue(struct request_queue *q, request_fn_proc *rfn,
634 spinlock_t *lock)
635{
636 return blk_init_allocated_queue_node(q, rfn, lock, -1);
637}
638EXPORT_SYMBOL(blk_init_allocated_queue);
639
640struct request_queue *
641blk_init_allocated_queue_node(struct request_queue *q, request_fn_proc *rfn,
642 spinlock_t *lock, int node_id)
643{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (!q)
645 return NULL;
646
Christoph Lameter19460892005-06-23 00:08:19 -0700647 q->node = node_id;
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600648 if (blk_init_free_list(q))
Al Viro8669aaf2006-03-18 13:50:00 -0500649 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 q->request_fn = rfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 q->prep_rq_fn = NULL;
James Bottomley28018c22010-07-01 19:49:17 +0900653 q->unprep_rq_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 q->unplug_fn = generic_unplug_device;
Jens Axboebc58ba92009-01-23 10:54:44 +0100655 q->queue_flags = QUEUE_FLAG_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 q->queue_lock = lock;
657
Jens Axboef3b144a2009-03-06 08:48:33 +0100658 /*
659 * This also sets hw/phys segments, boundary and size
660 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 blk_queue_make_request(q, __make_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Alan Stern44ec9542007-02-20 11:01:57 -0500663 q->sg_reserved_size = INT_MAX;
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 /*
666 * all done
667 */
668 if (!elevator_init(q, NULL)) {
669 blk_queue_congestion_threshold(q);
670 return q;
671 }
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return NULL;
674}
Mike Snitzer01effb02010-05-11 08:57:42 +0200675EXPORT_SYMBOL(blk_init_allocated_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Jens Axboe165125e2007-07-24 09:28:11 +0200677int blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Nick Pigginfde6ad22005-06-23 00:08:53 -0700679 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
Al Viro483f4af2006-03-18 18:34:37 -0500680 kobject_get(&q->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return 0;
682 }
683
684 return 1;
685}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Jens Axboe165125e2007-07-24 09:28:11 +0200687static inline void blk_free_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Jens Axboe4aff5e22006-08-10 08:44:47 +0200689 if (rq->cmd_flags & REQ_ELVPRIV)
Tejun Heocb98fc82005-10-28 08:29:39 +0200690 elv_put_request(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 mempool_free(rq, q->rq.rq_pool);
692}
693
Jens Axboe1ea25ecb2006-07-18 22:24:11 +0200694static struct request *
Jerome Marchand42dad762009-04-22 14:01:49 +0200695blk_alloc_request(struct request_queue *q, int flags, int priv, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
697 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
698
699 if (!rq)
700 return NULL;
701
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200702 blk_rq_init(q, rq);
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200703
Jerome Marchand42dad762009-04-22 14:01:49 +0200704 rq->cmd_flags = flags | REQ_ALLOCED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Tejun Heocb98fc82005-10-28 08:29:39 +0200706 if (priv) {
Jens Axboecb78b282006-07-28 09:32:57 +0200707 if (unlikely(elv_set_request(q, rq, gfp_mask))) {
Tejun Heocb98fc82005-10-28 08:29:39 +0200708 mempool_free(rq, q->rq.rq_pool);
709 return NULL;
710 }
Jens Axboe4aff5e22006-08-10 08:44:47 +0200711 rq->cmd_flags |= REQ_ELVPRIV;
Tejun Heocb98fc82005-10-28 08:29:39 +0200712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Tejun Heocb98fc82005-10-28 08:29:39 +0200714 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
717/*
718 * ioc_batching returns true if the ioc is a valid batching request and
719 * should be given priority access to a request.
720 */
Jens Axboe165125e2007-07-24 09:28:11 +0200721static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
723 if (!ioc)
724 return 0;
725
726 /*
727 * Make sure the process is able to allocate at least 1 request
728 * even if the batch times out, otherwise we could theoretically
729 * lose wakeups.
730 */
731 return ioc->nr_batch_requests == q->nr_batching ||
732 (ioc->nr_batch_requests > 0
733 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
734}
735
736/*
737 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
738 * will cause the process to be a "batcher" on all queues in the system. This
739 * is the behaviour we want though - once it gets a wakeup it should be given
740 * a nice run.
741 */
Jens Axboe165125e2007-07-24 09:28:11 +0200742static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
744 if (!ioc || ioc_batching(q, ioc))
745 return;
746
747 ioc->nr_batch_requests = q->nr_batching;
748 ioc->last_waited = jiffies;
749}
750
Jens Axboe1faa16d2009-04-06 14:48:01 +0200751static void __freed_request(struct request_queue *q, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
753 struct request_list *rl = &q->rq;
754
Jens Axboe1faa16d2009-04-06 14:48:01 +0200755 if (rl->count[sync] < queue_congestion_off_threshold(q))
756 blk_clear_queue_congested(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Jens Axboe1faa16d2009-04-06 14:48:01 +0200758 if (rl->count[sync] + 1 <= q->nr_requests) {
759 if (waitqueue_active(&rl->wait[sync]))
760 wake_up(&rl->wait[sync]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Jens Axboe1faa16d2009-04-06 14:48:01 +0200762 blk_clear_queue_full(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
764}
765
766/*
767 * A request has just been released. Account for it, update the full and
768 * congestion status, wake up any waiters. Called under q->queue_lock.
769 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200770static void freed_request(struct request_queue *q, int sync, int priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 struct request_list *rl = &q->rq;
773
Jens Axboe1faa16d2009-04-06 14:48:01 +0200774 rl->count[sync]--;
Tejun Heocb98fc82005-10-28 08:29:39 +0200775 if (priv)
776 rl->elvpriv--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Jens Axboe1faa16d2009-04-06 14:48:01 +0200778 __freed_request(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Jens Axboe1faa16d2009-04-06 14:48:01 +0200780 if (unlikely(rl->starved[sync ^ 1]))
781 __freed_request(q, sync ^ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784/*
Mike Snitzer9d5a4e92011-02-11 11:05:46 +0100785 * Determine if elevator data should be initialized when allocating the
786 * request associated with @bio.
787 */
788static bool blk_rq_should_init_elevator(struct bio *bio)
789{
790 if (!bio)
791 return true;
792
793 /*
794 * Flush requests do not use the elevator so skip initialization.
795 * This allows a request to share the flush and elevator data.
796 */
797 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA))
798 return false;
799
800 return true;
801}
802
803/*
Nick Piggind6344532005-06-28 20:45:14 -0700804 * Get a free request, queue_lock must be held.
805 * Returns NULL on failure, with queue_lock held.
806 * Returns !NULL on success, with queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 */
Jens Axboe165125e2007-07-24 09:28:11 +0200808static struct request *get_request(struct request_queue *q, int rw_flags,
Jens Axboe7749a8d2006-12-13 13:02:26 +0100809 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
811 struct request *rq = NULL;
812 struct request_list *rl = &q->rq;
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100813 struct io_context *ioc = NULL;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200814 const bool is_sync = rw_is_sync(rw_flags) != 0;
Mike Snitzer9d5a4e92011-02-11 11:05:46 +0100815 int may_queue, priv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Jens Axboe7749a8d2006-12-13 13:02:26 +0100817 may_queue = elv_may_queue(q, rw_flags);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100818 if (may_queue == ELV_MQUEUE_NO)
819 goto rq_starved;
820
Jens Axboe1faa16d2009-04-06 14:48:01 +0200821 if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) {
822 if (rl->count[is_sync]+1 >= q->nr_requests) {
Jens Axboeb5deef92006-07-19 23:39:40 +0200823 ioc = current_io_context(GFP_ATOMIC, q->node);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100824 /*
825 * The queue will fill after this allocation, so set
826 * it as full, and mark this process as "batching".
827 * This process will be allowed to complete a batch of
828 * requests, others will be blocked.
829 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200830 if (!blk_queue_full(q, is_sync)) {
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100831 ioc_set_batching(q, ioc);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200832 blk_set_queue_full(q, is_sync);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100833 } else {
834 if (may_queue != ELV_MQUEUE_MUST
835 && !ioc_batching(q, ioc)) {
836 /*
837 * The queue is full and the allocating
838 * process is not a "batcher", and not
839 * exempted by the IO scheduler
840 */
841 goto out;
842 }
843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
Jens Axboe1faa16d2009-04-06 14:48:01 +0200845 blk_set_queue_congested(q, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
847
Jens Axboe082cf692005-06-28 16:35:11 +0200848 /*
849 * Only allow batching queuers to allocate up to 50% over the defined
850 * limit of requests, otherwise we could have thousands of requests
851 * allocated with any setting of ->nr_requests
852 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200853 if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
Jens Axboe082cf692005-06-28 16:35:11 +0200854 goto out;
Hugh Dickinsfd782a42005-06-29 15:15:40 +0100855
Jens Axboe1faa16d2009-04-06 14:48:01 +0200856 rl->count[is_sync]++;
857 rl->starved[is_sync] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200858
Mike Snitzer9d5a4e92011-02-11 11:05:46 +0100859 if (blk_rq_should_init_elevator(bio)) {
860 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
861 if (priv)
862 rl->elvpriv++;
863 }
Tejun Heocb98fc82005-10-28 08:29:39 +0200864
Jens Axboef253b862010-10-24 22:06:02 +0200865 if (blk_queue_io_stat(q))
866 rw_flags |= REQ_IO_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 spin_unlock_irq(q->queue_lock);
868
Jens Axboe7749a8d2006-12-13 13:02:26 +0100869 rq = blk_alloc_request(q, rw_flags, priv, gfp_mask);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100870 if (unlikely(!rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 /*
872 * Allocation failed presumably due to memory. Undo anything
873 * we might have messed up.
874 *
875 * Allocating task should really be put onto the front of the
876 * wait queue, but this is pretty rare.
877 */
878 spin_lock_irq(q->queue_lock);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200879 freed_request(q, is_sync, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 /*
882 * in the very unlikely event that allocation failed and no
883 * requests for this direction was pending, mark us starved
884 * so that freeing of a request in the other direction will
885 * notice us. another possible fix would be to split the
886 * rq mempool into READ and WRITE
887 */
888rq_starved:
Jens Axboe1faa16d2009-04-06 14:48:01 +0200889 if (unlikely(rl->count[is_sync] == 0))
890 rl->starved[is_sync] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 goto out;
893 }
894
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100895 /*
896 * ioc may be NULL here, and ioc_batching will be false. That's
897 * OK, if the queue is under the request limit then requests need
898 * not count toward the nr_batch_requests limit. There will always
899 * be some limit enforced by BLK_BATCH_TIME.
900 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 if (ioc_batching(q, ioc))
902 ioc->nr_batch_requests--;
Jens Axboe6728cb02008-01-31 13:03:55 +0100903
Jens Axboe1faa16d2009-04-06 14:48:01 +0200904 trace_block_getrq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return rq;
907}
908
909/*
910 * No available requests for this queue, unplug the device and wait for some
911 * requests to become available.
Nick Piggind6344532005-06-28 20:45:14 -0700912 *
913 * Called with q->queue_lock held, and returns with it unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 */
Jens Axboe165125e2007-07-24 09:28:11 +0200915static struct request *get_request_wait(struct request_queue *q, int rw_flags,
Jens Axboe22e2c502005-06-27 10:55:12 +0200916 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Jens Axboe1faa16d2009-04-06 14:48:01 +0200918 const bool is_sync = rw_is_sync(rw_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 struct request *rq;
920
Jens Axboe7749a8d2006-12-13 13:02:26 +0100921 rq = get_request(q, rw_flags, bio, GFP_NOIO);
Nick Piggin450991b2005-06-28 20:45:13 -0700922 while (!rq) {
923 DEFINE_WAIT(wait);
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200924 struct io_context *ioc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 struct request_list *rl = &q->rq;
926
Jens Axboe1faa16d2009-04-06 14:48:01 +0200927 prepare_to_wait_exclusive(&rl->wait[is_sync], &wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 TASK_UNINTERRUPTIBLE);
929
Jens Axboe1faa16d2009-04-06 14:48:01 +0200930 trace_block_sleeprq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200932 __generic_unplug_device(q);
933 spin_unlock_irq(q->queue_lock);
934 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200936 /*
937 * After sleeping, we become a "batching" process and
938 * will be able to allocate at least one request, and
939 * up to a big batch of them for a small period time.
940 * See ioc_batching, ioc_set_batching
941 */
942 ioc = current_io_context(GFP_NOIO, q->node);
943 ioc_set_batching(q, ioc);
Jens Axboe2056a782006-03-23 20:00:26 +0100944
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200945 spin_lock_irq(q->queue_lock);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200946 finish_wait(&rl->wait[is_sync], &wait);
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200947
948 rq = get_request(q, rw_flags, bio, GFP_NOIO);
949 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 return rq;
952}
953
Jens Axboe165125e2007-07-24 09:28:11 +0200954struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
956 struct request *rq;
957
958 BUG_ON(rw != READ && rw != WRITE);
959
Nick Piggind6344532005-06-28 20:45:14 -0700960 spin_lock_irq(q->queue_lock);
961 if (gfp_mask & __GFP_WAIT) {
Jens Axboe22e2c502005-06-27 10:55:12 +0200962 rq = get_request_wait(q, rw, NULL);
Nick Piggind6344532005-06-28 20:45:14 -0700963 } else {
Jens Axboe22e2c502005-06-27 10:55:12 +0200964 rq = get_request(q, rw, NULL, gfp_mask);
Nick Piggind6344532005-06-28 20:45:14 -0700965 if (!rq)
966 spin_unlock_irq(q->queue_lock);
967 }
968 /* q->queue_lock is unlocked at this point */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 return rq;
971}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972EXPORT_SYMBOL(blk_get_request);
973
974/**
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300975 * blk_make_request - given a bio, allocate a corresponding struct request.
Randy Dunlap8ebf9752009-06-11 20:00:41 -0700976 * @q: target request queue
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300977 * @bio: The bio describing the memory mappings that will be submitted for IO.
978 * It may be a chained-bio properly constructed by block/bio layer.
Randy Dunlap8ebf9752009-06-11 20:00:41 -0700979 * @gfp_mask: gfp flags to be used for memory allocation
Jens Axboedc72ef42006-07-20 14:54:05 +0200980 *
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300981 * blk_make_request is the parallel of generic_make_request for BLOCK_PC
982 * type commands. Where the struct request needs to be farther initialized by
983 * the caller. It is passed a &struct bio, which describes the memory info of
984 * the I/O transfer.
985 *
986 * The caller of blk_make_request must make sure that bi_io_vec
987 * are set to describe the memory buffers. That bio_data_dir() will return
988 * the needed direction of the request. (And all bio's in the passed bio-chain
989 * are properly set accordingly)
990 *
991 * If called under none-sleepable conditions, mapped bio buffers must not
992 * need bouncing, by calling the appropriate masked or flagged allocator,
993 * suitable for the target device. Otherwise the call to blk_queue_bounce will
994 * BUG.
Jens Axboe53674ac2009-05-19 19:52:35 +0200995 *
996 * WARNING: When allocating/cloning a bio-chain, careful consideration should be
997 * given to how you allocate bios. In particular, you cannot use __GFP_WAIT for
998 * anything but the first bio in the chain. Otherwise you risk waiting for IO
999 * completion of a bio that hasn't been submitted yet, thus resulting in a
1000 * deadlock. Alternatively bios should be allocated using bio_kmalloc() instead
1001 * of bio_alloc(), as that avoids the mempool deadlock.
1002 * If possible a big IO should be split into smaller parts when allocation
1003 * fails. Partial allocation should not be an error, or you risk a live-lock.
Jens Axboedc72ef42006-07-20 14:54:05 +02001004 */
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001005struct request *blk_make_request(struct request_queue *q, struct bio *bio,
1006 gfp_t gfp_mask)
Jens Axboedc72ef42006-07-20 14:54:05 +02001007{
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001008 struct request *rq = blk_get_request(q, bio_data_dir(bio), gfp_mask);
1009
1010 if (unlikely(!rq))
1011 return ERR_PTR(-ENOMEM);
1012
1013 for_each_bio(bio) {
1014 struct bio *bounce_bio = bio;
1015 int ret;
1016
1017 blk_queue_bounce(q, &bounce_bio);
1018 ret = blk_rq_append_bio(q, rq, bounce_bio);
1019 if (unlikely(ret)) {
1020 blk_put_request(rq);
1021 return ERR_PTR(ret);
1022 }
1023 }
1024
1025 return rq;
Jens Axboedc72ef42006-07-20 14:54:05 +02001026}
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001027EXPORT_SYMBOL(blk_make_request);
Jens Axboedc72ef42006-07-20 14:54:05 +02001028
1029/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 * blk_requeue_request - put a request back on queue
1031 * @q: request queue where request should be inserted
1032 * @rq: request to be inserted
1033 *
1034 * Description:
1035 * Drivers often keep queueing requests until the hardware cannot accept
1036 * more, when that condition happens we need to put the request back
1037 * on the queue. Must be called with queue lock held.
1038 */
Jens Axboe165125e2007-07-24 09:28:11 +02001039void blk_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
Jens Axboe242f9dc2008-09-14 05:55:09 -07001041 blk_delete_timer(rq);
1042 blk_clear_rq_complete(rq);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001043 trace_block_rq_requeue(q, rq);
Jens Axboe2056a782006-03-23 20:00:26 +01001044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (blk_rq_tagged(rq))
1046 blk_queue_end_tag(q, rq);
1047
James Bottomleyba396a62009-05-27 14:17:08 +02001048 BUG_ON(blk_queued_rq(rq));
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 elv_requeue_request(q, rq);
1051}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052EXPORT_SYMBOL(blk_requeue_request);
1053
1054/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001055 * blk_insert_request - insert a special request into a request queue
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 * @q: request queue where request should be inserted
1057 * @rq: request to be inserted
1058 * @at_head: insert request at head or tail of queue
1059 * @data: private data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 *
1061 * Description:
1062 * Many block devices need to execute commands asynchronously, so they don't
1063 * block the whole kernel from preemption during request execution. This is
1064 * accomplished normally by inserting aritficial requests tagged as
Randy Dunlap710027a2008-08-19 20:13:11 +02001065 * REQ_TYPE_SPECIAL in to the corresponding request queue, and letting them
1066 * be scheduled for actual execution by the request queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 *
1068 * We have the option of inserting the head or the tail of the queue.
1069 * Typically we use the tail for new ioctls and so forth. We use the head
1070 * of the queue for things like a QUEUE_FULL message from a device, or a
1071 * host that is unable to accept a particular command.
1072 */
Jens Axboe165125e2007-07-24 09:28:11 +02001073void blk_insert_request(struct request_queue *q, struct request *rq,
Tejun Heo 867d1192005-04-24 02:06:05 -05001074 int at_head, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
Tejun Heo 867d1192005-04-24 02:06:05 -05001076 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 unsigned long flags;
1078
1079 /*
1080 * tell I/O scheduler that this isn't a regular read/write (ie it
1081 * must not attempt merges on this) and that it acts as a soft
1082 * barrier
1083 */
Jens Axboe4aff5e22006-08-10 08:44:47 +02001084 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086 rq->special = data;
1087
1088 spin_lock_irqsave(q->queue_lock, flags);
1089
1090 /*
1091 * If command is tagged, release the tag
1092 */
Tejun Heo 867d1192005-04-24 02:06:05 -05001093 if (blk_rq_tagged(rq))
1094 blk_queue_end_tag(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Jerome Marchandb238b3d2007-10-23 15:05:46 +02001096 drive_stat_acct(rq, 1);
Tejun Heo 867d1192005-04-24 02:06:05 -05001097 __elv_add_request(q, rq, where, 0);
Tejun Heoa7f55792009-04-23 11:05:17 +09001098 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 spin_unlock_irqrestore(q->queue_lock, flags);
1100}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101EXPORT_SYMBOL(blk_insert_request);
1102
Tejun Heo074a7ac2008-08-25 19:56:14 +09001103static void part_round_stats_single(int cpu, struct hd_struct *part,
1104 unsigned long now)
1105{
1106 if (now == part->stamp)
1107 return;
1108
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001109 if (part_in_flight(part)) {
Tejun Heo074a7ac2008-08-25 19:56:14 +09001110 __part_stat_add(cpu, part, time_in_queue,
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001111 part_in_flight(part) * (now - part->stamp));
Tejun Heo074a7ac2008-08-25 19:56:14 +09001112 __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
1113 }
1114 part->stamp = now;
1115}
1116
1117/**
Randy Dunlap496aa8a2008-10-16 07:46:23 +02001118 * part_round_stats() - Round off the performance stats on a struct disk_stats.
1119 * @cpu: cpu number for stats access
1120 * @part: target partition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 *
1122 * The average IO queue length and utilisation statistics are maintained
1123 * by observing the current state of the queue length and the amount of
1124 * time it has been in this state for.
1125 *
1126 * Normally, that accounting is done on IO completion, but that can result
1127 * in more than a second's worth of IO being accounted for within any one
1128 * second, leading to >100% utilisation. To deal with that, we call this
1129 * function to do a round-off before returning the results when reading
1130 * /proc/diskstats. This accounts immediately for all queue usage up to
1131 * the current jiffies and restarts the counters again.
1132 */
Tejun Heoc9959052008-08-25 19:47:21 +09001133void part_round_stats(int cpu, struct hd_struct *part)
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001134{
1135 unsigned long now = jiffies;
1136
Tejun Heo074a7ac2008-08-25 19:56:14 +09001137 if (part->partno)
1138 part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
1139 part_round_stats_single(cpu, part, now);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001140}
Tejun Heo074a7ac2008-08-25 19:56:14 +09001141EXPORT_SYMBOL_GPL(part_round_stats);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143/*
1144 * queue lock must be held
1145 */
Jens Axboe165125e2007-07-24 09:28:11 +02001146void __blk_put_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 if (unlikely(!q))
1149 return;
1150 if (unlikely(--req->ref_count))
1151 return;
1152
Tejun Heo8922e162005-10-20 16:23:44 +02001153 elv_completed_request(q, req);
1154
Boaz Harrosh1cd96c22009-03-24 12:35:07 +01001155 /* this is a bio leak */
1156 WARN_ON(req->bio != NULL);
1157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 /*
1159 * Request may not have originated from ll_rw_blk. if not,
1160 * it didn't come out of our reserved rq pools
1161 */
Jens Axboe49171e52006-08-10 08:59:11 +02001162 if (req->cmd_flags & REQ_ALLOCED) {
Jens Axboe1faa16d2009-04-06 14:48:01 +02001163 int is_sync = rq_is_sync(req) != 0;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001164 int priv = req->cmd_flags & REQ_ELVPRIV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 BUG_ON(!list_empty(&req->queuelist));
Jens Axboe98170642006-07-28 09:23:08 +02001167 BUG_ON(!hlist_unhashed(&req->hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169 blk_free_request(q, req);
Jens Axboe1faa16d2009-04-06 14:48:01 +02001170 freed_request(q, is_sync, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
1172}
Mike Christie6e39b69e2005-11-11 05:30:24 -06001173EXPORT_SYMBOL_GPL(__blk_put_request);
1174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175void blk_put_request(struct request *req)
1176{
Tejun Heo8922e162005-10-20 16:23:44 +02001177 unsigned long flags;
Jens Axboe165125e2007-07-24 09:28:11 +02001178 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
FUJITA Tomonori52a93ba2008-07-15 21:21:45 +02001180 spin_lock_irqsave(q->queue_lock, flags);
1181 __blk_put_request(q, req);
1182 spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184EXPORT_SYMBOL(blk_put_request);
1185
Christoph Hellwig66ac0282010-06-18 16:59:42 +02001186/**
1187 * blk_add_request_payload - add a payload to a request
1188 * @rq: request to update
1189 * @page: page backing the payload
1190 * @len: length of the payload.
1191 *
1192 * This allows to later add a payload to an already submitted request by
1193 * a block driver. The driver needs to take care of freeing the payload
1194 * itself.
1195 *
1196 * Note that this is a quite horrible hack and nothing but handling of
1197 * discard requests should ever use it.
1198 */
1199void blk_add_request_payload(struct request *rq, struct page *page,
1200 unsigned int len)
1201{
1202 struct bio *bio = rq->bio;
1203
1204 bio->bi_io_vec->bv_page = page;
1205 bio->bi_io_vec->bv_offset = 0;
1206 bio->bi_io_vec->bv_len = len;
1207
1208 bio->bi_size = len;
1209 bio->bi_vcnt = 1;
1210 bio->bi_phys_segments = 1;
1211
1212 rq->__data_len = rq->resid_len = len;
1213 rq->nr_phys_segments = 1;
1214 rq->buffer = bio_data(bio);
1215}
1216EXPORT_SYMBOL_GPL(blk_add_request_payload);
1217
Jens Axboe86db1e22008-01-29 14:53:40 +01001218void init_request_from_bio(struct request *req, struct bio *bio)
Tejun Heo52d9e672006-01-06 09:49:58 +01001219{
Jens Axboec7c22e42008-09-13 20:26:01 +02001220 req->cpu = bio->bi_comp_cpu;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001221 req->cmd_type = REQ_TYPE_FS;
Tejun Heo52d9e672006-01-06 09:49:58 +01001222
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001223 req->cmd_flags |= bio->bi_rw & REQ_COMMON_MASK;
1224 if (bio->bi_rw & REQ_RAHEAD)
Tejun Heoa82afdf2009-07-03 17:48:16 +09001225 req->cmd_flags |= REQ_FAILFAST_MASK;
Jens Axboeb31dc662006-06-13 08:26:10 +02001226
Tejun Heo52d9e672006-01-06 09:49:58 +01001227 req->errors = 0;
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001228 req->__sector = bio->bi_sector;
Tejun Heo52d9e672006-01-06 09:49:58 +01001229 req->ioprio = bio_prio(bio);
NeilBrownbc1c56f2007-08-16 13:31:30 +02001230 blk_rq_bio_prep(req->q, req, bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001231}
1232
Jens Axboe644b2d92009-04-06 14:48:06 +02001233/*
1234 * Only disabling plugging for non-rotational devices if it does tagging
1235 * as well, otherwise we do need the proper merging
1236 */
1237static inline bool queue_should_plug(struct request_queue *q)
1238{
Jens Axboe79da06442010-02-23 08:40:43 +01001239 return !(blk_queue_nonrot(q) && blk_queue_tagged(q));
Jens Axboe644b2d92009-04-06 14:48:06 +02001240}
1241
Jens Axboe165125e2007-07-24 09:28:11 +02001242static int __make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
Nick Piggin450991b2005-06-28 20:45:13 -07001244 struct request *req;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001245 int el_ret;
1246 unsigned int bytes = bio->bi_size;
Jens Axboe51da90f2006-07-18 04:14:45 +02001247 const unsigned short prio = bio_prio(bio);
Jiri Slaby5e00d1b2010-08-12 14:31:06 +02001248 const bool sync = !!(bio->bi_rw & REQ_SYNC);
1249 const bool unplug = !!(bio->bi_rw & REQ_UNPLUG);
1250 const unsigned long ff = bio->bi_rw & REQ_FAILFAST_MASK;
Tejun Heo28e7d182010-09-03 11:56:16 +02001251 int where = ELEVATOR_INSERT_SORT;
Jens Axboe7749a8d2006-12-13 13:02:26 +01001252 int rw_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 /*
1255 * low level driver can indicate that it wants pages above a
1256 * certain limit bounced to low memory (ie for highmem, or even
1257 * ISA dma in theory)
1258 */
1259 blk_queue_bounce(q, &bio);
1260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 spin_lock_irq(q->queue_lock);
1262
Tejun Heo4fed9472010-09-03 11:56:17 +02001263 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) {
Tejun Heoae1b1532011-01-25 12:43:54 +01001264 where = ELEVATOR_INSERT_FLUSH;
Tejun Heo28e7d182010-09-03 11:56:16 +02001265 goto get_rq;
1266 }
1267
1268 if (elv_queue_empty(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 goto get_rq;
1270
1271 el_ret = elv_merge(q, &req, bio);
1272 switch (el_ret) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001273 case ELEVATOR_BACK_MERGE:
1274 BUG_ON(!rq_mergeable(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Jens Axboe6728cb02008-01-31 13:03:55 +01001276 if (!ll_back_merge_fn(q, req, bio))
1277 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001279 trace_block_bio_backmerge(q, bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001280
Tejun Heo80a761f2009-07-03 17:48:17 +09001281 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1282 blk_rq_set_mixed_merge(req);
1283
Jens Axboe6728cb02008-01-31 13:03:55 +01001284 req->biotail->bi_next = bio;
1285 req->biotail = bio;
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001286 req->__data_len += bytes;
Jens Axboe6728cb02008-01-31 13:03:55 +01001287 req->ioprio = ioprio_best(req->ioprio, prio);
Jens Axboeab780f12008-08-26 10:25:02 +02001288 if (!blk_rq_cpu_valid(req))
1289 req->cpu = bio->bi_comp_cpu;
Jens Axboe6728cb02008-01-31 13:03:55 +01001290 drive_stat_acct(req, 0);
Divyesh Shah812d4022010-04-08 21:14:23 -07001291 elv_bio_merged(q, req, bio);
Jens Axboe6728cb02008-01-31 13:03:55 +01001292 if (!attempt_back_merge(q, req))
1293 elv_merged_request(q, req, el_ret);
1294 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Jens Axboe6728cb02008-01-31 13:03:55 +01001296 case ELEVATOR_FRONT_MERGE:
1297 BUG_ON(!rq_mergeable(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Jens Axboe6728cb02008-01-31 13:03:55 +01001299 if (!ll_front_merge_fn(q, req, bio))
1300 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001302 trace_block_bio_frontmerge(q, bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001303
Tejun Heo80a761f2009-07-03 17:48:17 +09001304 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff) {
1305 blk_rq_set_mixed_merge(req);
1306 req->cmd_flags &= ~REQ_FAILFAST_MASK;
1307 req->cmd_flags |= ff;
1308 }
1309
Jens Axboe6728cb02008-01-31 13:03:55 +01001310 bio->bi_next = req->bio;
1311 req->bio = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Jens Axboe6728cb02008-01-31 13:03:55 +01001313 /*
1314 * may not be valid. if the low level driver said
1315 * it didn't need a bounce buffer then it better
1316 * not touch req->buffer either...
1317 */
1318 req->buffer = bio_data(bio);
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001319 req->__sector = bio->bi_sector;
1320 req->__data_len += bytes;
Jens Axboe6728cb02008-01-31 13:03:55 +01001321 req->ioprio = ioprio_best(req->ioprio, prio);
Jens Axboeab780f12008-08-26 10:25:02 +02001322 if (!blk_rq_cpu_valid(req))
1323 req->cpu = bio->bi_comp_cpu;
Jens Axboe6728cb02008-01-31 13:03:55 +01001324 drive_stat_acct(req, 0);
Divyesh Shah812d4022010-04-08 21:14:23 -07001325 elv_bio_merged(q, req, bio);
Jens Axboe6728cb02008-01-31 13:03:55 +01001326 if (!attempt_front_merge(q, req))
1327 elv_merged_request(q, req, el_ret);
1328 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Jens Axboe6728cb02008-01-31 13:03:55 +01001330 /* ELV_NO_MERGE: elevator says don't/can't merge. */
1331 default:
1332 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 }
1334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335get_rq:
Nick Piggin450991b2005-06-28 20:45:13 -07001336 /*
Jens Axboe7749a8d2006-12-13 13:02:26 +01001337 * This sync check and mask will be re-done in init_request_from_bio(),
1338 * but we need to set it earlier to expose the sync flag to the
1339 * rq allocator and io schedulers.
1340 */
1341 rw_flags = bio_data_dir(bio);
1342 if (sync)
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001343 rw_flags |= REQ_SYNC;
Jens Axboe7749a8d2006-12-13 13:02:26 +01001344
1345 /*
Nick Piggin450991b2005-06-28 20:45:13 -07001346 * Grab a free request. This is might sleep but can not fail.
Nick Piggind6344532005-06-28 20:45:14 -07001347 * Returns with the queue unlocked.
Nick Piggin450991b2005-06-28 20:45:13 -07001348 */
Jens Axboe7749a8d2006-12-13 13:02:26 +01001349 req = get_request_wait(q, rw_flags, bio);
Nick Piggind6344532005-06-28 20:45:14 -07001350
Nick Piggin450991b2005-06-28 20:45:13 -07001351 /*
1352 * After dropping the lock and possibly sleeping here, our request
1353 * may now be mergeable after it had proven unmergeable (above).
1354 * We don't worry about that case for efficiency. It won't happen
1355 * often, and the elevators are able to handle it.
1356 */
Tejun Heo52d9e672006-01-06 09:49:58 +01001357 init_request_from_bio(req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Nick Piggin450991b2005-06-28 20:45:13 -07001359 spin_lock_irq(q->queue_lock);
Jens Axboec7c22e42008-09-13 20:26:01 +02001360 if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags) ||
1361 bio_flagged(bio, BIO_CPU_AFFINE))
1362 req->cpu = blk_cpu_to_group(smp_processor_id());
Jens Axboe644b2d92009-04-06 14:48:06 +02001363 if (queue_should_plug(q) && elv_queue_empty(q))
Nick Piggin450991b2005-06-28 20:45:13 -07001364 blk_plug_device(q);
Tejun Heodd831002010-09-03 11:56:16 +02001365
1366 /* insert the request into the elevator */
1367 drive_stat_acct(req, 1);
Tejun Heo28e7d182010-09-03 11:56:16 +02001368 __elv_add_request(q, req, where, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369out:
Jens Axboe644b2d92009-04-06 14:48:06 +02001370 if (unplug || !queue_should_plug(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 __generic_unplug_device(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 spin_unlock_irq(q->queue_lock);
1373 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374}
1375
1376/*
1377 * If bio->bi_dev is a partition, remap the location
1378 */
1379static inline void blk_partition_remap(struct bio *bio)
1380{
1381 struct block_device *bdev = bio->bi_bdev;
1382
Jens Axboebf2de6f2007-09-27 13:01:25 +02001383 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 struct hd_struct *p = bdev->bd_part;
Jens Axboea3623572005-11-01 09:26:16 +01001385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 bio->bi_sector += p->start_sect;
1387 bio->bi_bdev = bdev->bd_contains;
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001388
Mike Snitzerd07335e2010-11-16 12:52:38 +01001389 trace_block_bio_remap(bdev_get_queue(bio->bi_bdev), bio,
1390 bdev->bd_dev,
1391 bio->bi_sector - p->start_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 }
1393}
1394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395static void handle_bad_sector(struct bio *bio)
1396{
1397 char b[BDEVNAME_SIZE];
1398
1399 printk(KERN_INFO "attempt to access beyond end of device\n");
1400 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1401 bdevname(bio->bi_bdev, b),
1402 bio->bi_rw,
1403 (unsigned long long)bio->bi_sector + bio_sectors(bio),
Mike Snitzer77304d22010-11-08 14:39:12 +01001404 (long long)(i_size_read(bio->bi_bdev->bd_inode) >> 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 set_bit(BIO_EOF, &bio->bi_flags);
1407}
1408
Akinobu Mitac17bb492006-12-08 02:39:46 -08001409#ifdef CONFIG_FAIL_MAKE_REQUEST
1410
1411static DECLARE_FAULT_ATTR(fail_make_request);
1412
1413static int __init setup_fail_make_request(char *str)
1414{
1415 return setup_fault_attr(&fail_make_request, str);
1416}
1417__setup("fail_make_request=", setup_fail_make_request);
1418
1419static int should_fail_request(struct bio *bio)
1420{
Tejun Heoeddb2e22008-08-25 19:56:13 +09001421 struct hd_struct *part = bio->bi_bdev->bd_part;
1422
1423 if (part_to_disk(part)->part0.make_it_fail || part->make_it_fail)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001424 return should_fail(&fail_make_request, bio->bi_size);
1425
1426 return 0;
1427}
1428
1429static int __init fail_make_request_debugfs(void)
1430{
1431 return init_fault_attr_dentries(&fail_make_request,
1432 "fail_make_request");
1433}
1434
1435late_initcall(fail_make_request_debugfs);
1436
1437#else /* CONFIG_FAIL_MAKE_REQUEST */
1438
1439static inline int should_fail_request(struct bio *bio)
1440{
1441 return 0;
1442}
1443
1444#endif /* CONFIG_FAIL_MAKE_REQUEST */
1445
Jens Axboec07e2b42007-07-18 13:27:58 +02001446/*
1447 * Check whether this bio extends beyond the end of the device.
1448 */
1449static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1450{
1451 sector_t maxsector;
1452
1453 if (!nr_sectors)
1454 return 0;
1455
1456 /* Test device or partition size, when known. */
Mike Snitzer77304d22010-11-08 14:39:12 +01001457 maxsector = i_size_read(bio->bi_bdev->bd_inode) >> 9;
Jens Axboec07e2b42007-07-18 13:27:58 +02001458 if (maxsector) {
1459 sector_t sector = bio->bi_sector;
1460
1461 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1462 /*
1463 * This may well happen - the kernel calls bread()
1464 * without checking the size of the device, e.g., when
1465 * mounting a device.
1466 */
1467 handle_bad_sector(bio);
1468 return 1;
1469 }
1470 }
1471
1472 return 0;
1473}
1474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001476 * generic_make_request - hand a buffer to its device driver for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 * @bio: The bio describing the location in memory and on the device.
1478 *
1479 * generic_make_request() is used to make I/O requests of block
1480 * devices. It is passed a &struct bio, which describes the I/O that needs
1481 * to be done.
1482 *
1483 * generic_make_request() does not return any status. The
1484 * success/failure status of the request, along with notification of
1485 * completion, is delivered asynchronously through the bio->bi_end_io
1486 * function described (one day) else where.
1487 *
1488 * The caller of generic_make_request must make sure that bi_io_vec
1489 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1490 * set to describe the device address, and the
1491 * bi_end_io and optionally bi_private are set to describe how
1492 * completion notification should be signaled.
1493 *
1494 * generic_make_request and the drivers it calls may use bi_next if this
1495 * bio happens to be merged with someone else, and may change bi_dev and
1496 * bi_sector for remaps as it sees fit. So the values of these fields
1497 * should NOT be depended on after the call to generic_make_request.
1498 */
Neil Brownd89d8792007-05-01 09:53:42 +02001499static inline void __generic_make_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
Jens Axboe165125e2007-07-24 09:28:11 +02001501 struct request_queue *q;
NeilBrown5ddfe962006-10-30 22:07:21 -08001502 sector_t old_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 int ret, nr_sectors = bio_sectors(bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001504 dev_t old_dev;
Jens Axboe51fd77b2007-11-02 08:49:08 +01001505 int err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Jens Axboec07e2b42007-07-18 13:27:58 +02001509 if (bio_check_eod(bio, nr_sectors))
1510 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512 /*
1513 * Resolve the mapping until finished. (drivers are
1514 * still free to implement/resolve their own stacking
1515 * by explicitly returning 0)
1516 *
1517 * NOTE: we don't repeat the blk_size check for each new device.
1518 * Stacking drivers are expected to know what they are doing.
1519 */
NeilBrown5ddfe962006-10-30 22:07:21 -08001520 old_sector = -1;
Jens Axboe2056a782006-03-23 20:00:26 +01001521 old_dev = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 do {
1523 char b[BDEVNAME_SIZE];
1524
1525 q = bdev_get_queue(bio->bi_bdev);
Tejun Heoa7384672008-11-28 13:32:03 +09001526 if (unlikely(!q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 printk(KERN_ERR
1528 "generic_make_request: Trying to access "
1529 "nonexistent block-device %s (%Lu)\n",
1530 bdevname(bio->bi_bdev, b),
1531 (long long) bio->bi_sector);
Tejun Heoa7384672008-11-28 13:32:03 +09001532 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 }
1534
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001535 if (unlikely(!(bio->bi_rw & REQ_DISCARD) &&
Christoph Hellwig67efc922009-09-30 13:54:20 +02001536 nr_sectors > queue_max_hw_sectors(q))) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001537 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
Martin K. Petersenae03bf62009-05-22 17:17:50 -04001538 bdevname(bio->bi_bdev, b),
1539 bio_sectors(bio),
1540 queue_max_hw_sectors(q));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 goto end_io;
1542 }
1543
Nick Pigginfde6ad22005-06-23 00:08:53 -07001544 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 goto end_io;
1546
Akinobu Mitac17bb492006-12-08 02:39:46 -08001547 if (should_fail_request(bio))
1548 goto end_io;
1549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 /*
1551 * If this device has partitions, remap block n
1552 * of partition p to block n+start(p) of the disk.
1553 */
1554 blk_partition_remap(bio);
1555
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +02001556 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio))
1557 goto end_io;
1558
NeilBrown5ddfe962006-10-30 22:07:21 -08001559 if (old_sector != -1)
Mike Snitzerd07335e2010-11-16 12:52:38 +01001560 trace_block_bio_remap(q, bio, old_dev, old_sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001561
NeilBrown5ddfe962006-10-30 22:07:21 -08001562 old_sector = bio->bi_sector;
Jens Axboe2056a782006-03-23 20:00:26 +01001563 old_dev = bio->bi_bdev->bd_dev;
1564
Jens Axboec07e2b42007-07-18 13:27:58 +02001565 if (bio_check_eod(bio, nr_sectors))
1566 goto end_io;
Tejun Heoa7384672008-11-28 13:32:03 +09001567
Tejun Heo1e879012010-09-03 11:56:17 +02001568 /*
1569 * Filter flush bio's early so that make_request based
1570 * drivers without flush support don't have to worry
1571 * about them.
1572 */
1573 if ((bio->bi_rw & (REQ_FLUSH | REQ_FUA)) && !q->flush_flags) {
1574 bio->bi_rw &= ~(REQ_FLUSH | REQ_FUA);
1575 if (!nr_sectors) {
1576 err = 0;
1577 goto end_io;
1578 }
1579 }
1580
Adrian Hunter8d57a982010-08-11 14:17:49 -07001581 if ((bio->bi_rw & REQ_DISCARD) &&
1582 (!blk_queue_discard(q) ||
1583 ((bio->bi_rw & REQ_SECURE) &&
1584 !blk_queue_secdiscard(q)))) {
Jens Axboe51fd77b2007-11-02 08:49:08 +01001585 err = -EOPNOTSUPP;
1586 goto end_io;
1587 }
NeilBrown5ddfe962006-10-30 22:07:21 -08001588
Vivek Goyale43473b2010-09-15 17:06:35 -04001589 blk_throtl_bio(q, &bio);
1590
1591 /*
1592 * If bio = NULL, bio has been throttled and will be submitted
1593 * later.
1594 */
1595 if (!bio)
1596 break;
1597
Minchan Kim01edede2009-09-08 21:56:38 +02001598 trace_block_bio_queue(q, bio);
1599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 ret = q->make_request_fn(q, bio);
1601 } while (ret);
Tejun Heoa7384672008-11-28 13:32:03 +09001602
1603 return;
1604
1605end_io:
1606 bio_endio(bio, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607}
1608
Neil Brownd89d8792007-05-01 09:53:42 +02001609/*
1610 * We only want one ->make_request_fn to be active at a time,
1611 * else stack usage with stacked devices could be a problem.
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001612 * So use current->bio_list to keep a list of requests
Neil Brownd89d8792007-05-01 09:53:42 +02001613 * submited by a make_request_fn function.
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001614 * current->bio_list is also used as a flag to say if
Neil Brownd89d8792007-05-01 09:53:42 +02001615 * generic_make_request is currently active in this task or not.
1616 * If it is NULL, then no make_request is active. If it is non-NULL,
1617 * then a make_request is active, and new requests should be added
1618 * at the tail
1619 */
1620void generic_make_request(struct bio *bio)
1621{
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001622 struct bio_list bio_list_on_stack;
1623
1624 if (current->bio_list) {
Neil Brownd89d8792007-05-01 09:53:42 +02001625 /* make_request is active */
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001626 bio_list_add(current->bio_list, bio);
Neil Brownd89d8792007-05-01 09:53:42 +02001627 return;
1628 }
1629 /* following loop may be a bit non-obvious, and so deserves some
1630 * explanation.
1631 * Before entering the loop, bio->bi_next is NULL (as all callers
1632 * ensure that) so we have a list with a single bio.
1633 * We pretend that we have just taken it off a longer list, so
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001634 * we assign bio_list to a pointer to the bio_list_on_stack,
1635 * thus initialising the bio_list of new bios to be
Neil Brownd89d8792007-05-01 09:53:42 +02001636 * added. __generic_make_request may indeed add some more bios
1637 * through a recursive call to generic_make_request. If it
1638 * did, we find a non-NULL value in bio_list and re-enter the loop
1639 * from the top. In this case we really did just take the bio
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001640 * of the top of the list (no pretending) and so remove it from
1641 * bio_list, and call into __generic_make_request again.
Neil Brownd89d8792007-05-01 09:53:42 +02001642 *
1643 * The loop was structured like this to make only one call to
1644 * __generic_make_request (which is important as it is large and
1645 * inlined) and to keep the structure simple.
1646 */
1647 BUG_ON(bio->bi_next);
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001648 bio_list_init(&bio_list_on_stack);
1649 current->bio_list = &bio_list_on_stack;
Neil Brownd89d8792007-05-01 09:53:42 +02001650 do {
Neil Brownd89d8792007-05-01 09:53:42 +02001651 __generic_make_request(bio);
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001652 bio = bio_list_pop(current->bio_list);
Neil Brownd89d8792007-05-01 09:53:42 +02001653 } while (bio);
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001654 current->bio_list = NULL; /* deactivate */
Neil Brownd89d8792007-05-01 09:53:42 +02001655}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656EXPORT_SYMBOL(generic_make_request);
1657
1658/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001659 * submit_bio - submit a bio to the block device layer for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1661 * @bio: The &struct bio which describes the I/O
1662 *
1663 * submit_bio() is very similar in purpose to generic_make_request(), and
1664 * uses that function to do most of the work. Both are fairly rough
Randy Dunlap710027a2008-08-19 20:13:11 +02001665 * interfaces; @bio must be presetup and ready for I/O.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 *
1667 */
1668void submit_bio(int rw, struct bio *bio)
1669{
1670 int count = bio_sectors(bio);
1671
Jens Axboe22e2c502005-06-27 10:55:12 +02001672 bio->bi_rw |= rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Jens Axboebf2de6f2007-09-27 13:01:25 +02001674 /*
1675 * If it's a regular read/write or a barrier with data attached,
1676 * go through the normal accounting stuff before submission.
1677 */
Jens Axboe3ffb52e2010-06-29 13:33:38 +02001678 if (bio_has_data(bio) && !(rw & REQ_DISCARD)) {
Jens Axboebf2de6f2007-09-27 13:01:25 +02001679 if (rw & WRITE) {
1680 count_vm_events(PGPGOUT, count);
1681 } else {
1682 task_io_account_read(bio->bi_size);
1683 count_vm_events(PGPGIN, count);
1684 }
1685
1686 if (unlikely(block_dump)) {
1687 char b[BDEVNAME_SIZE];
San Mehat8dcbdc72010-09-14 08:48:01 +02001688 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s (%u sectors)\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001689 current->comm, task_pid_nr(current),
Jens Axboebf2de6f2007-09-27 13:01:25 +02001690 (rw & WRITE) ? "WRITE" : "READ",
1691 (unsigned long long)bio->bi_sector,
San Mehat8dcbdc72010-09-14 08:48:01 +02001692 bdevname(bio->bi_bdev, b),
1693 count);
Jens Axboebf2de6f2007-09-27 13:01:25 +02001694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 }
1696
1697 generic_make_request(bio);
1698}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699EXPORT_SYMBOL(submit_bio);
1700
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001701/**
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001702 * blk_rq_check_limits - Helper function to check a request for the queue limit
1703 * @q: the queue
1704 * @rq: the request being checked
1705 *
1706 * Description:
1707 * @rq may have been made based on weaker limitations of upper-level queues
1708 * in request stacking drivers, and it may violate the limitation of @q.
1709 * Since the block layer and the underlying device driver trust @rq
1710 * after it is inserted to @q, it should be checked against @q before
1711 * the insertion using this generic function.
1712 *
1713 * This function should also be useful for request stacking drivers
Stefan Weileef35c22010-08-06 21:11:15 +02001714 * in some cases below, so export this function.
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001715 * Request stacking drivers like request-based dm may change the queue
1716 * limits while requests are in the queue (e.g. dm's table swapping).
1717 * Such request stacking drivers should check those requests agaist
1718 * the new queue limits again when they dispatch those requests,
1719 * although such checkings are also done against the old queue limits
1720 * when submitting requests.
1721 */
1722int blk_rq_check_limits(struct request_queue *q, struct request *rq)
1723{
ike Snitzer33839772010-08-08 12:11:33 -04001724 if (rq->cmd_flags & REQ_DISCARD)
1725 return 0;
1726
Martin K. Petersenae03bf62009-05-22 17:17:50 -04001727 if (blk_rq_sectors(rq) > queue_max_sectors(q) ||
1728 blk_rq_bytes(rq) > queue_max_hw_sectors(q) << 9) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001729 printk(KERN_ERR "%s: over max size limit.\n", __func__);
1730 return -EIO;
1731 }
1732
1733 /*
1734 * queue's settings related to segment counting like q->bounce_pfn
1735 * may differ from that of other stacking queues.
1736 * Recalculate it to check the request correctly on this queue's
1737 * limitation.
1738 */
1739 blk_recalc_rq_segments(rq);
Martin K. Petersen8a783622010-02-26 00:20:39 -05001740 if (rq->nr_phys_segments > queue_max_segments(q)) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001741 printk(KERN_ERR "%s: over max segments limit.\n", __func__);
1742 return -EIO;
1743 }
1744
1745 return 0;
1746}
1747EXPORT_SYMBOL_GPL(blk_rq_check_limits);
1748
1749/**
1750 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
1751 * @q: the queue to submit the request
1752 * @rq: the request being queued
1753 */
1754int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
1755{
1756 unsigned long flags;
1757
1758 if (blk_rq_check_limits(q, rq))
1759 return -EIO;
1760
1761#ifdef CONFIG_FAIL_MAKE_REQUEST
1762 if (rq->rq_disk && rq->rq_disk->part0.make_it_fail &&
1763 should_fail(&fail_make_request, blk_rq_bytes(rq)))
1764 return -EIO;
1765#endif
1766
1767 spin_lock_irqsave(q->queue_lock, flags);
1768
1769 /*
1770 * Submitting request must be dequeued before calling this function
1771 * because it will be linked to another request_queue
1772 */
1773 BUG_ON(blk_queued_rq(rq));
1774
1775 drive_stat_acct(rq, 1);
1776 __elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 0);
1777
1778 spin_unlock_irqrestore(q->queue_lock, flags);
1779
1780 return 0;
1781}
1782EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
1783
Tejun Heo80a761f2009-07-03 17:48:17 +09001784/**
1785 * blk_rq_err_bytes - determine number of bytes till the next failure boundary
1786 * @rq: request to examine
1787 *
1788 * Description:
1789 * A request could be merge of IOs which require different failure
1790 * handling. This function determines the number of bytes which
1791 * can be failed from the beginning of the request without
1792 * crossing into area which need to be retried further.
1793 *
1794 * Return:
1795 * The number of bytes to fail.
1796 *
1797 * Context:
1798 * queue_lock must be held.
1799 */
1800unsigned int blk_rq_err_bytes(const struct request *rq)
1801{
1802 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
1803 unsigned int bytes = 0;
1804 struct bio *bio;
1805
1806 if (!(rq->cmd_flags & REQ_MIXED_MERGE))
1807 return blk_rq_bytes(rq);
1808
1809 /*
1810 * Currently the only 'mixing' which can happen is between
1811 * different fastfail types. We can safely fail portions
1812 * which have all the failfast bits that the first one has -
1813 * the ones which are at least as eager to fail as the first
1814 * one.
1815 */
1816 for (bio = rq->bio; bio; bio = bio->bi_next) {
1817 if ((bio->bi_rw & ff) != ff)
1818 break;
1819 bytes += bio->bi_size;
1820 }
1821
1822 /* this could lead to infinite loop */
1823 BUG_ON(blk_rq_bytes(rq) && !bytes);
1824 return bytes;
1825}
1826EXPORT_SYMBOL_GPL(blk_rq_err_bytes);
1827
Jens Axboebc58ba92009-01-23 10:54:44 +01001828static void blk_account_io_completion(struct request *req, unsigned int bytes)
1829{
Jens Axboec2553b52009-04-24 08:10:11 +02001830 if (blk_do_io_stat(req)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01001831 const int rw = rq_data_dir(req);
1832 struct hd_struct *part;
1833 int cpu;
1834
1835 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +01001836 part = req->part;
Jens Axboebc58ba92009-01-23 10:54:44 +01001837 part_stat_add(cpu, part, sectors[rw], bytes >> 9);
1838 part_stat_unlock();
1839 }
1840}
1841
1842static void blk_account_io_done(struct request *req)
1843{
Jens Axboebc58ba92009-01-23 10:54:44 +01001844 /*
Tejun Heodd4c1332010-09-03 11:56:16 +02001845 * Account IO completion. flush_rq isn't accounted as a
1846 * normal IO on queueing nor completion. Accounting the
1847 * containing request is enough.
Jens Axboebc58ba92009-01-23 10:54:44 +01001848 */
Tejun Heo414b4ff2011-01-25 12:43:49 +01001849 if (blk_do_io_stat(req) && !(req->cmd_flags & REQ_FLUSH_SEQ)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01001850 unsigned long duration = jiffies - req->start_time;
1851 const int rw = rq_data_dir(req);
1852 struct hd_struct *part;
1853 int cpu;
1854
1855 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +01001856 part = req->part;
Jens Axboebc58ba92009-01-23 10:54:44 +01001857
1858 part_stat_inc(cpu, part, ios[rw]);
1859 part_stat_add(cpu, part, ticks[rw], duration);
1860 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001861 part_dec_in_flight(part, rw);
Jens Axboebc58ba92009-01-23 10:54:44 +01001862
Jens Axboe6c23a962011-01-07 08:43:37 +01001863 hd_struct_put(part);
Jens Axboebc58ba92009-01-23 10:54:44 +01001864 part_stat_unlock();
1865 }
1866}
1867
Tejun Heo53a08802008-12-03 12:41:26 +01001868/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09001869 * blk_peek_request - peek at the top of a request queue
1870 * @q: request queue to peek at
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001871 *
1872 * Description:
Tejun Heo9934c8c2009-05-08 11:54:16 +09001873 * Return the request at the top of @q. The returned request
1874 * should be started using blk_start_request() before LLD starts
1875 * processing it.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001876 *
1877 * Return:
Tejun Heo9934c8c2009-05-08 11:54:16 +09001878 * Pointer to the request at the top of @q if available. Null
1879 * otherwise.
1880 *
1881 * Context:
1882 * queue_lock must be held.
1883 */
1884struct request *blk_peek_request(struct request_queue *q)
Tejun Heo158dbda2009-04-23 11:05:18 +09001885{
1886 struct request *rq;
1887 int ret;
1888
1889 while ((rq = __elv_next_request(q)) != NULL) {
1890 if (!(rq->cmd_flags & REQ_STARTED)) {
1891 /*
1892 * This is the first time the device driver
1893 * sees this request (possibly after
1894 * requeueing). Notify IO scheduler.
1895 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001896 if (rq->cmd_flags & REQ_SORTED)
Tejun Heo158dbda2009-04-23 11:05:18 +09001897 elv_activate_rq(q, rq);
1898
1899 /*
1900 * just mark as started even if we don't start
1901 * it, a request that has been delayed should
1902 * not be passed by new incoming requests
1903 */
1904 rq->cmd_flags |= REQ_STARTED;
1905 trace_block_rq_issue(q, rq);
1906 }
1907
1908 if (!q->boundary_rq || q->boundary_rq == rq) {
1909 q->end_sector = rq_end_sector(rq);
1910 q->boundary_rq = NULL;
1911 }
1912
1913 if (rq->cmd_flags & REQ_DONTPREP)
1914 break;
1915
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001916 if (q->dma_drain_size && blk_rq_bytes(rq)) {
Tejun Heo158dbda2009-04-23 11:05:18 +09001917 /*
1918 * make sure space for the drain appears we
1919 * know we can do this because max_hw_segments
1920 * has been adjusted to be one fewer than the
1921 * device can handle
1922 */
1923 rq->nr_phys_segments++;
1924 }
1925
1926 if (!q->prep_rq_fn)
1927 break;
1928
1929 ret = q->prep_rq_fn(q, rq);
1930 if (ret == BLKPREP_OK) {
1931 break;
1932 } else if (ret == BLKPREP_DEFER) {
1933 /*
1934 * the request may have been (partially) prepped.
1935 * we need to keep this request in the front to
1936 * avoid resource deadlock. REQ_STARTED will
1937 * prevent other fs requests from passing this one.
1938 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001939 if (q->dma_drain_size && blk_rq_bytes(rq) &&
Tejun Heo158dbda2009-04-23 11:05:18 +09001940 !(rq->cmd_flags & REQ_DONTPREP)) {
1941 /*
1942 * remove the space for the drain we added
1943 * so that we don't add it again
1944 */
1945 --rq->nr_phys_segments;
1946 }
1947
1948 rq = NULL;
1949 break;
1950 } else if (ret == BLKPREP_KILL) {
1951 rq->cmd_flags |= REQ_QUIET;
James Bottomleyc143dc92009-05-30 06:43:49 +02001952 /*
1953 * Mark this request as started so we don't trigger
1954 * any debug logic in the end I/O path.
1955 */
1956 blk_start_request(rq);
Tejun Heo40cbbb72009-04-23 11:05:19 +09001957 __blk_end_request_all(rq, -EIO);
Tejun Heo158dbda2009-04-23 11:05:18 +09001958 } else {
1959 printk(KERN_ERR "%s: bad return=%d\n", __func__, ret);
1960 break;
1961 }
1962 }
1963
1964 return rq;
1965}
Tejun Heo9934c8c2009-05-08 11:54:16 +09001966EXPORT_SYMBOL(blk_peek_request);
Tejun Heo158dbda2009-04-23 11:05:18 +09001967
Tejun Heo9934c8c2009-05-08 11:54:16 +09001968void blk_dequeue_request(struct request *rq)
Tejun Heo158dbda2009-04-23 11:05:18 +09001969{
Tejun Heo9934c8c2009-05-08 11:54:16 +09001970 struct request_queue *q = rq->q;
1971
Tejun Heo158dbda2009-04-23 11:05:18 +09001972 BUG_ON(list_empty(&rq->queuelist));
1973 BUG_ON(ELV_ON_HASH(rq));
1974
1975 list_del_init(&rq->queuelist);
1976
1977 /*
1978 * the time frame between a request being removed from the lists
1979 * and to it is freed is accounted as io that is in progress at
1980 * the driver side.
1981 */
Divyesh Shah91952912010-04-01 15:01:41 -07001982 if (blk_account_rq(rq)) {
Jens Axboe0a7ae2f2009-05-20 08:54:31 +02001983 q->in_flight[rq_is_sync(rq)]++;
Divyesh Shah91952912010-04-01 15:01:41 -07001984 set_io_start_time_ns(rq);
1985 }
Tejun Heo158dbda2009-04-23 11:05:18 +09001986}
1987
Tejun Heo5efccd12009-04-23 11:05:18 +09001988/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09001989 * blk_start_request - start request processing on the driver
1990 * @req: request to dequeue
1991 *
1992 * Description:
1993 * Dequeue @req and start timeout timer on it. This hands off the
1994 * request to the driver.
1995 *
1996 * Block internal functions which don't want to start timer should
1997 * call blk_dequeue_request().
1998 *
1999 * Context:
2000 * queue_lock must be held.
2001 */
2002void blk_start_request(struct request *req)
2003{
2004 blk_dequeue_request(req);
2005
2006 /*
Tejun Heo5f49f632009-05-19 18:33:05 +09002007 * We are now handing the request to the hardware, initialize
2008 * resid_len to full count and add the timeout handler.
Tejun Heo9934c8c2009-05-08 11:54:16 +09002009 */
Tejun Heo5f49f632009-05-19 18:33:05 +09002010 req->resid_len = blk_rq_bytes(req);
FUJITA Tomonoridbb66c42009-06-09 05:47:10 +02002011 if (unlikely(blk_bidi_rq(req)))
2012 req->next_rq->resid_len = blk_rq_bytes(req->next_rq);
2013
Tejun Heo9934c8c2009-05-08 11:54:16 +09002014 blk_add_timer(req);
2015}
2016EXPORT_SYMBOL(blk_start_request);
2017
2018/**
2019 * blk_fetch_request - fetch a request from a request queue
2020 * @q: request queue to fetch a request from
2021 *
2022 * Description:
2023 * Return the request at the top of @q. The request is started on
2024 * return and LLD can start processing it immediately.
2025 *
2026 * Return:
2027 * Pointer to the request at the top of @q if available. Null
2028 * otherwise.
2029 *
2030 * Context:
2031 * queue_lock must be held.
2032 */
2033struct request *blk_fetch_request(struct request_queue *q)
2034{
2035 struct request *rq;
2036
2037 rq = blk_peek_request(q);
2038 if (rq)
2039 blk_start_request(rq);
2040 return rq;
2041}
2042EXPORT_SYMBOL(blk_fetch_request);
2043
2044/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002045 * blk_update_request - Special helper function for request stacking drivers
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002046 * @req: the request being processed
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002047 * @error: %0 for success, < %0 for error
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002048 * @nr_bytes: number of bytes to complete @req
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002049 *
2050 * Description:
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002051 * Ends I/O on a number of bytes attached to @req, but doesn't complete
2052 * the request structure even if @req doesn't have leftover.
2053 * If @req has leftover, sets it up for the next range of segments.
Tejun Heo2e60e022009-04-23 11:05:18 +09002054 *
2055 * This special helper function is only for request stacking drivers
2056 * (e.g. request-based dm) so that they can handle partial completion.
2057 * Actual device drivers should use blk_end_request instead.
2058 *
2059 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
2060 * %false return from this function.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002061 *
2062 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002063 * %false - this request doesn't have any more data
2064 * %true - this request has more data
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002065 **/
Tejun Heo2e60e022009-04-23 11:05:18 +09002066bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05002068 int total_bytes, bio_nbytes, next_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 struct bio *bio;
2070
Tejun Heo2e60e022009-04-23 11:05:18 +09002071 if (!req->bio)
2072 return false;
2073
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01002074 trace_block_rq_complete(req->q, req);
Jens Axboe2056a782006-03-23 20:00:26 +01002075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 /*
Tejun Heo6f414692009-04-19 07:00:41 +09002077 * For fs requests, rq is just carrier of independent bio's
2078 * and each partial completion should be handled separately.
2079 * Reset per-request error on each partial completion.
2080 *
2081 * TODO: tj: This is too subtle. It would be better to let
2082 * low level drivers do what they see fit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002084 if (req->cmd_type == REQ_TYPE_FS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 req->errors = 0;
2086
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002087 if (error && req->cmd_type == REQ_TYPE_FS &&
2088 !(req->cmd_flags & REQ_QUIET)) {
Jens Axboe6728cb02008-01-31 13:03:55 +01002089 printk(KERN_ERR "end_request: I/O error, dev %s, sector %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 req->rq_disk ? req->rq_disk->disk_name : "?",
Tejun Heo83096eb2009-05-07 22:24:39 +09002091 (unsigned long long)blk_rq_pos(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 }
2093
Jens Axboebc58ba92009-01-23 10:54:44 +01002094 blk_account_io_completion(req, nr_bytes);
Jens Axboed72d9042005-11-01 08:35:42 +01002095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 total_bytes = bio_nbytes = 0;
2097 while ((bio = req->bio) != NULL) {
2098 int nbytes;
2099
2100 if (nr_bytes >= bio->bi_size) {
2101 req->bio = bio->bi_next;
2102 nbytes = bio->bi_size;
NeilBrown5bb23a62007-09-27 12:46:13 +02002103 req_bio_endio(req, bio, nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 next_idx = 0;
2105 bio_nbytes = 0;
2106 } else {
2107 int idx = bio->bi_idx + next_idx;
2108
Kazuhisa Ichikawaaf498d72009-05-12 13:27:45 +02002109 if (unlikely(idx >= bio->bi_vcnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 blk_dump_rq_flags(req, "__end_that");
Jens Axboe6728cb02008-01-31 13:03:55 +01002111 printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
Kazuhisa Ichikawaaf498d72009-05-12 13:27:45 +02002112 __func__, idx, bio->bi_vcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 break;
2114 }
2115
2116 nbytes = bio_iovec_idx(bio, idx)->bv_len;
2117 BIO_BUG_ON(nbytes > bio->bi_size);
2118
2119 /*
2120 * not a complete bvec done
2121 */
2122 if (unlikely(nbytes > nr_bytes)) {
2123 bio_nbytes += nr_bytes;
2124 total_bytes += nr_bytes;
2125 break;
2126 }
2127
2128 /*
2129 * advance to the next vector
2130 */
2131 next_idx++;
2132 bio_nbytes += nbytes;
2133 }
2134
2135 total_bytes += nbytes;
2136 nr_bytes -= nbytes;
2137
Jens Axboe6728cb02008-01-31 13:03:55 +01002138 bio = req->bio;
2139 if (bio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 /*
2141 * end more in this run, or just return 'not-done'
2142 */
2143 if (unlikely(nr_bytes <= 0))
2144 break;
2145 }
2146 }
2147
2148 /*
2149 * completely done
2150 */
Tejun Heo2e60e022009-04-23 11:05:18 +09002151 if (!req->bio) {
2152 /*
2153 * Reset counters so that the request stacking driver
2154 * can find how many bytes remain in the request
2155 * later.
2156 */
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002157 req->__data_len = 0;
Tejun Heo2e60e022009-04-23 11:05:18 +09002158 return false;
2159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
2161 /*
2162 * if the request wasn't completed, update state
2163 */
2164 if (bio_nbytes) {
NeilBrown5bb23a62007-09-27 12:46:13 +02002165 req_bio_endio(req, bio, bio_nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 bio->bi_idx += next_idx;
2167 bio_iovec(bio)->bv_offset += nr_bytes;
2168 bio_iovec(bio)->bv_len -= nr_bytes;
2169 }
2170
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002171 req->__data_len -= total_bytes;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002172 req->buffer = bio_data(req->bio);
2173
2174 /* update sector only for requests with clear definition of sector */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002175 if (req->cmd_type == REQ_TYPE_FS || (req->cmd_flags & REQ_DISCARD))
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002176 req->__sector += total_bytes >> 9;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002177
Tejun Heo80a761f2009-07-03 17:48:17 +09002178 /* mixed attributes always follow the first bio */
2179 if (req->cmd_flags & REQ_MIXED_MERGE) {
2180 req->cmd_flags &= ~REQ_FAILFAST_MASK;
2181 req->cmd_flags |= req->bio->bi_rw & REQ_FAILFAST_MASK;
2182 }
2183
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002184 /*
2185 * If total number of sectors is less than the first segment
2186 * size, something has gone terribly wrong.
2187 */
2188 if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
2189 printk(KERN_ERR "blk: request botched\n");
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002190 req->__data_len = blk_rq_cur_bytes(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002191 }
2192
2193 /* recalculate the number of segments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 blk_recalc_rq_segments(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002195
Tejun Heo2e60e022009-04-23 11:05:18 +09002196 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197}
Tejun Heo2e60e022009-04-23 11:05:18 +09002198EXPORT_SYMBOL_GPL(blk_update_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Tejun Heo2e60e022009-04-23 11:05:18 +09002200static bool blk_update_bidi_request(struct request *rq, int error,
2201 unsigned int nr_bytes,
2202 unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002203{
Tejun Heo2e60e022009-04-23 11:05:18 +09002204 if (blk_update_request(rq, error, nr_bytes))
2205 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002206
Tejun Heo2e60e022009-04-23 11:05:18 +09002207 /* Bidi request must be completed as a whole */
2208 if (unlikely(blk_bidi_rq(rq)) &&
2209 blk_update_request(rq->next_rq, error, bidi_bytes))
2210 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002211
Jens Axboee2e1a142010-06-09 10:42:09 +02002212 if (blk_queue_add_random(rq->q))
2213 add_disk_randomness(rq->rq_disk);
Tejun Heo2e60e022009-04-23 11:05:18 +09002214
2215 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216}
2217
James Bottomley28018c22010-07-01 19:49:17 +09002218/**
2219 * blk_unprep_request - unprepare a request
2220 * @req: the request
2221 *
2222 * This function makes a request ready for complete resubmission (or
2223 * completion). It happens only after all error handling is complete,
2224 * so represents the appropriate moment to deallocate any resources
2225 * that were allocated to the request in the prep_rq_fn. The queue
2226 * lock is held when calling this.
2227 */
2228void blk_unprep_request(struct request *req)
2229{
2230 struct request_queue *q = req->q;
2231
2232 req->cmd_flags &= ~REQ_DONTPREP;
2233 if (q->unprep_rq_fn)
2234 q->unprep_rq_fn(q, req);
2235}
2236EXPORT_SYMBOL_GPL(blk_unprep_request);
2237
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238/*
2239 * queue lock must be held
2240 */
Tejun Heo2e60e022009-04-23 11:05:18 +09002241static void blk_finish_request(struct request *req, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242{
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002243 if (blk_rq_tagged(req))
2244 blk_queue_end_tag(req->q, req);
2245
James Bottomleyba396a62009-05-27 14:17:08 +02002246 BUG_ON(blk_queued_rq(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002248 if (unlikely(laptop_mode) && req->cmd_type == REQ_TYPE_FS)
Matthew Garrett31373d02010-04-06 14:25:14 +02002249 laptop_io_completion(&req->q->backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
Mike Andersone78042e2008-10-30 02:16:20 -07002251 blk_delete_timer(req);
2252
James Bottomley28018c22010-07-01 19:49:17 +09002253 if (req->cmd_flags & REQ_DONTPREP)
2254 blk_unprep_request(req);
2255
2256
Jens Axboebc58ba92009-01-23 10:54:44 +01002257 blk_account_io_done(req);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002258
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 if (req->end_io)
Tejun Heo8ffdc652006-01-06 09:49:03 +01002260 req->end_io(req, error);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002261 else {
2262 if (blk_bidi_rq(req))
2263 __blk_put_request(req->next_rq->q, req->next_rq);
2264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 __blk_put_request(req->q, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 }
2267}
2268
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05002269/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002270 * blk_end_bidi_request - Complete a bidi request
2271 * @rq: the request to complete
Randy Dunlap710027a2008-08-19 20:13:11 +02002272 * @error: %0 for success, < %0 for error
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002273 * @nr_bytes: number of bytes to complete @rq
2274 * @bidi_bytes: number of bytes to complete @rq->next_rq
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002275 *
2276 * Description:
2277 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
Tejun Heo2e60e022009-04-23 11:05:18 +09002278 * Drivers that supports bidi can safely call this member for any
2279 * type of request, bidi or uni. In the later case @bidi_bytes is
2280 * just ignored.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002281 *
2282 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002283 * %false - we are done with this request
2284 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002285 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002286static bool blk_end_bidi_request(struct request *rq, int error,
2287 unsigned int nr_bytes, unsigned int bidi_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002288{
2289 struct request_queue *q = rq->q;
Tejun Heo2e60e022009-04-23 11:05:18 +09002290 unsigned long flags;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002291
Tejun Heo2e60e022009-04-23 11:05:18 +09002292 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2293 return true;
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002294
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002295 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo2e60e022009-04-23 11:05:18 +09002296 blk_finish_request(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002297 spin_unlock_irqrestore(q->queue_lock, flags);
2298
Tejun Heo2e60e022009-04-23 11:05:18 +09002299 return false;
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002300}
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002301
2302/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002303 * __blk_end_bidi_request - Complete a bidi request with queue lock held
2304 * @rq: the request to complete
2305 * @error: %0 for success, < %0 for error
2306 * @nr_bytes: number of bytes to complete @rq
2307 * @bidi_bytes: number of bytes to complete @rq->next_rq
Tejun Heo5efccd12009-04-23 11:05:18 +09002308 *
2309 * Description:
Tejun Heo2e60e022009-04-23 11:05:18 +09002310 * Identical to blk_end_bidi_request() except that queue lock is
2311 * assumed to be locked on entry and remains so on return.
Tejun Heo5efccd12009-04-23 11:05:18 +09002312 *
Tejun Heo2e60e022009-04-23 11:05:18 +09002313 * Return:
2314 * %false - we are done with this request
2315 * %true - still buffers pending for this request
Tejun Heo5efccd12009-04-23 11:05:18 +09002316 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002317static bool __blk_end_bidi_request(struct request *rq, int error,
2318 unsigned int nr_bytes, unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002319{
Tejun Heo2e60e022009-04-23 11:05:18 +09002320 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2321 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002322
Tejun Heo2e60e022009-04-23 11:05:18 +09002323 blk_finish_request(rq, error);
Tejun Heo5efccd12009-04-23 11:05:18 +09002324
Tejun Heo2e60e022009-04-23 11:05:18 +09002325 return false;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002326}
2327
2328/**
2329 * blk_end_request - Helper function for drivers to complete the request.
2330 * @rq: the request being processed
2331 * @error: %0 for success, < %0 for error
2332 * @nr_bytes: number of bytes to complete
2333 *
2334 * Description:
2335 * Ends I/O on a number of bytes attached to @rq.
2336 * If @rq has leftover, sets it up for the next range of segments.
2337 *
2338 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002339 * %false - we are done with this request
2340 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002341 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002342bool blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002343{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002344 return blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002345}
Jens Axboe56ad1742009-07-28 22:11:24 +02002346EXPORT_SYMBOL(blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002347
2348/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002349 * blk_end_request_all - Helper function for drives to finish the request.
2350 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002351 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002352 *
2353 * Description:
2354 * Completely finish @rq.
2355 */
2356void blk_end_request_all(struct request *rq, int error)
2357{
2358 bool pending;
2359 unsigned int bidi_bytes = 0;
2360
2361 if (unlikely(blk_bidi_rq(rq)))
2362 bidi_bytes = blk_rq_bytes(rq->next_rq);
2363
2364 pending = blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2365 BUG_ON(pending);
2366}
Jens Axboe56ad1742009-07-28 22:11:24 +02002367EXPORT_SYMBOL(blk_end_request_all);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002368
2369/**
2370 * blk_end_request_cur - Helper function to finish the current request chunk.
2371 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002372 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002373 *
2374 * Description:
2375 * Complete the current consecutively mapped chunk from @rq.
2376 *
2377 * Return:
2378 * %false - we are done with this request
2379 * %true - still buffers pending for this request
2380 */
2381bool blk_end_request_cur(struct request *rq, int error)
2382{
2383 return blk_end_request(rq, error, blk_rq_cur_bytes(rq));
2384}
Jens Axboe56ad1742009-07-28 22:11:24 +02002385EXPORT_SYMBOL(blk_end_request_cur);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002386
2387/**
Tejun Heo80a761f2009-07-03 17:48:17 +09002388 * blk_end_request_err - Finish a request till the next failure boundary.
2389 * @rq: the request to finish till the next failure boundary for
2390 * @error: must be negative errno
2391 *
2392 * Description:
2393 * Complete @rq till the next failure boundary.
2394 *
2395 * Return:
2396 * %false - we are done with this request
2397 * %true - still buffers pending for this request
2398 */
2399bool blk_end_request_err(struct request *rq, int error)
2400{
2401 WARN_ON(error >= 0);
2402 return blk_end_request(rq, error, blk_rq_err_bytes(rq));
2403}
2404EXPORT_SYMBOL_GPL(blk_end_request_err);
2405
2406/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002407 * __blk_end_request - Helper function for drivers to complete the request.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002408 * @rq: the request being processed
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002409 * @error: %0 for success, < %0 for error
2410 * @nr_bytes: number of bytes to complete
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002411 *
2412 * Description:
2413 * Must be called with queue lock held unlike blk_end_request().
2414 *
2415 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002416 * %false - we are done with this request
2417 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002418 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002419bool __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002420{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002421 return __blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002422}
Jens Axboe56ad1742009-07-28 22:11:24 +02002423EXPORT_SYMBOL(__blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002424
2425/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002426 * __blk_end_request_all - Helper function for drives to finish the request.
2427 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002428 * @error: %0 for success, < %0 for error
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002429 *
2430 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002431 * Completely finish @rq. Must be called with queue lock held.
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002432 */
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002433void __blk_end_request_all(struct request *rq, int error)
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002434{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002435 bool pending;
2436 unsigned int bidi_bytes = 0;
2437
2438 if (unlikely(blk_bidi_rq(rq)))
2439 bidi_bytes = blk_rq_bytes(rq->next_rq);
2440
2441 pending = __blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2442 BUG_ON(pending);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002443}
Jens Axboe56ad1742009-07-28 22:11:24 +02002444EXPORT_SYMBOL(__blk_end_request_all);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002445
2446/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002447 * __blk_end_request_cur - Helper function to finish the current request chunk.
2448 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002449 * @error: %0 for success, < %0 for error
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002450 *
2451 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002452 * Complete the current consecutively mapped chunk from @rq. Must
2453 * be called with queue lock held.
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002454 *
2455 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002456 * %false - we are done with this request
2457 * %true - still buffers pending for this request
2458 */
2459bool __blk_end_request_cur(struct request *rq, int error)
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002460{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002461 return __blk_end_request(rq, error, blk_rq_cur_bytes(rq));
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002462}
Jens Axboe56ad1742009-07-28 22:11:24 +02002463EXPORT_SYMBOL(__blk_end_request_cur);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002464
Tejun Heo80a761f2009-07-03 17:48:17 +09002465/**
2466 * __blk_end_request_err - Finish a request till the next failure boundary.
2467 * @rq: the request to finish till the next failure boundary for
2468 * @error: must be negative errno
2469 *
2470 * Description:
2471 * Complete @rq till the next failure boundary. Must be called
2472 * with queue lock held.
2473 *
2474 * Return:
2475 * %false - we are done with this request
2476 * %true - still buffers pending for this request
2477 */
2478bool __blk_end_request_err(struct request *rq, int error)
2479{
2480 WARN_ON(error >= 0);
2481 return __blk_end_request(rq, error, blk_rq_err_bytes(rq));
2482}
2483EXPORT_SYMBOL_GPL(__blk_end_request_err);
2484
Jens Axboe86db1e22008-01-29 14:53:40 +01002485void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2486 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487{
Tejun Heoa82afdf2009-07-03 17:48:16 +09002488 /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002489 rq->cmd_flags |= bio->bi_rw & REQ_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
David Woodhousefb2dce82008-08-05 18:01:53 +01002491 if (bio_has_data(bio)) {
2492 rq->nr_phys_segments = bio_phys_segments(q, bio);
David Woodhousefb2dce82008-08-05 18:01:53 +01002493 rq->buffer = bio_data(bio);
2494 }
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002495 rq->__data_len = bio->bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 rq->bio = rq->biotail = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
NeilBrown66846572007-08-16 13:31:28 +02002498 if (bio->bi_bdev)
2499 rq->rq_disk = bio->bi_bdev->bd_disk;
2500}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
Ilya Loginov2d4dc892009-11-26 09:16:19 +01002502#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
2503/**
2504 * rq_flush_dcache_pages - Helper function to flush all pages in a request
2505 * @rq: the request to be flushed
2506 *
2507 * Description:
2508 * Flush all pages in @rq.
2509 */
2510void rq_flush_dcache_pages(struct request *rq)
2511{
2512 struct req_iterator iter;
2513 struct bio_vec *bvec;
2514
2515 rq_for_each_segment(bvec, rq, iter)
2516 flush_dcache_page(bvec->bv_page);
2517}
2518EXPORT_SYMBOL_GPL(rq_flush_dcache_pages);
2519#endif
2520
Kiyoshi Uedaef9e3fa2008-10-01 16:12:15 +02002521/**
2522 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
2523 * @q : the queue of the device being checked
2524 *
2525 * Description:
2526 * Check if underlying low-level drivers of a device are busy.
2527 * If the drivers want to export their busy state, they must set own
2528 * exporting function using blk_queue_lld_busy() first.
2529 *
2530 * Basically, this function is used only by request stacking drivers
2531 * to stop dispatching requests to underlying devices when underlying
2532 * devices are busy. This behavior helps more I/O merging on the queue
2533 * of the request stacking driver and prevents I/O throughput regression
2534 * on burst I/O load.
2535 *
2536 * Return:
2537 * 0 - Not busy (The request stacking driver should dispatch request)
2538 * 1 - Busy (The request stacking driver should stop dispatching request)
2539 */
2540int blk_lld_busy(struct request_queue *q)
2541{
2542 if (q->lld_busy_fn)
2543 return q->lld_busy_fn(q);
2544
2545 return 0;
2546}
2547EXPORT_SYMBOL_GPL(blk_lld_busy);
2548
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002549/**
2550 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
2551 * @rq: the clone request to be cleaned up
2552 *
2553 * Description:
2554 * Free all bios in @rq for a cloned request.
2555 */
2556void blk_rq_unprep_clone(struct request *rq)
2557{
2558 struct bio *bio;
2559
2560 while ((bio = rq->bio) != NULL) {
2561 rq->bio = bio->bi_next;
2562
2563 bio_put(bio);
2564 }
2565}
2566EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
2567
2568/*
2569 * Copy attributes of the original request to the clone request.
2570 * The actual data parts (e.g. ->cmd, ->buffer, ->sense) are not copied.
2571 */
2572static void __blk_rq_prep_clone(struct request *dst, struct request *src)
2573{
2574 dst->cpu = src->cpu;
Tejun Heo3a2edd02010-09-03 11:56:18 +02002575 dst->cmd_flags = (src->cmd_flags & REQ_CLONE_MASK) | REQ_NOMERGE;
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002576 dst->cmd_type = src->cmd_type;
2577 dst->__sector = blk_rq_pos(src);
2578 dst->__data_len = blk_rq_bytes(src);
2579 dst->nr_phys_segments = src->nr_phys_segments;
2580 dst->ioprio = src->ioprio;
2581 dst->extra_len = src->extra_len;
2582}
2583
2584/**
2585 * blk_rq_prep_clone - Helper function to setup clone request
2586 * @rq: the request to be setup
2587 * @rq_src: original request to be cloned
2588 * @bs: bio_set that bios for clone are allocated from
2589 * @gfp_mask: memory allocation mask for bio
2590 * @bio_ctr: setup function to be called for each clone bio.
2591 * Returns %0 for success, non %0 for failure.
2592 * @data: private data to be passed to @bio_ctr
2593 *
2594 * Description:
2595 * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
2596 * The actual data parts of @rq_src (e.g. ->cmd, ->buffer, ->sense)
2597 * are not copied, and copying such parts is the caller's responsibility.
2598 * Also, pages which the original bios are pointing to are not copied
2599 * and the cloned bios just point same pages.
2600 * So cloned bios must be completed before original bios, which means
2601 * the caller must complete @rq before @rq_src.
2602 */
2603int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
2604 struct bio_set *bs, gfp_t gfp_mask,
2605 int (*bio_ctr)(struct bio *, struct bio *, void *),
2606 void *data)
2607{
2608 struct bio *bio, *bio_src;
2609
2610 if (!bs)
2611 bs = fs_bio_set;
2612
2613 blk_rq_init(NULL, rq);
2614
2615 __rq_for_each_bio(bio_src, rq_src) {
2616 bio = bio_alloc_bioset(gfp_mask, bio_src->bi_max_vecs, bs);
2617 if (!bio)
2618 goto free_and_out;
2619
2620 __bio_clone(bio, bio_src);
2621
2622 if (bio_integrity(bio_src) &&
Martin K. Petersen7878cba2009-06-26 15:37:49 +02002623 bio_integrity_clone(bio, bio_src, gfp_mask, bs))
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002624 goto free_and_out;
2625
2626 if (bio_ctr && bio_ctr(bio, bio_src, data))
2627 goto free_and_out;
2628
2629 if (rq->bio) {
2630 rq->biotail->bi_next = bio;
2631 rq->biotail = bio;
2632 } else
2633 rq->bio = rq->biotail = bio;
2634 }
2635
2636 __blk_rq_prep_clone(rq, rq_src);
2637
2638 return 0;
2639
2640free_and_out:
2641 if (bio)
2642 bio_free(bio, bs);
2643 blk_rq_unprep_clone(rq);
2644
2645 return -ENOMEM;
2646}
2647EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
2648
Jens Axboe18887ad2008-07-28 13:08:45 +02002649int kblockd_schedule_work(struct request_queue *q, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650{
2651 return queue_work(kblockd_workqueue, work);
2652}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653EXPORT_SYMBOL(kblockd_schedule_work);
2654
Vivek Goyale43473b2010-09-15 17:06:35 -04002655int kblockd_schedule_delayed_work(struct request_queue *q,
2656 struct delayed_work *dwork, unsigned long delay)
2657{
2658 return queue_delayed_work(kblockd_workqueue, dwork, delay);
2659}
2660EXPORT_SYMBOL(kblockd_schedule_delayed_work);
2661
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662int __init blk_dev_init(void)
2663{
Nikanth Karthikesan9eb55b02009-04-27 14:53:54 +02002664 BUILD_BUG_ON(__REQ_NR_BITS > 8 *
2665 sizeof(((struct request *)0)->cmd_flags));
2666
Tejun Heo89b90be2011-01-03 15:01:47 +01002667 /* used for unplugging and affects IO latency/throughput - HIGHPRI */
2668 kblockd_workqueue = alloc_workqueue("kblockd",
2669 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 if (!kblockd_workqueue)
2671 panic("Failed to create kblockd\n");
2672
2673 request_cachep = kmem_cache_create("blkdev_requests",
Paul Mundt20c2df82007-07-20 10:11:58 +09002674 sizeof(struct request), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
Jens Axboe8324aa92008-01-29 14:51:59 +01002676 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02002677 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 return 0;
2680}