blob: bc2b7c5004e179cc31b88f2b480521a9c4a16fbe [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200/*
201 * "plug" the device if there are no outstanding requests: this will
202 * force the transfer to start only after we have put all the requests
203 * on the list.
204 *
205 * This is called with interrupts off and no requests on the queue and
206 * with the queue lock held.
207 */
Jens Axboe165125e2007-07-24 09:28:11 +0200208void blk_plug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210 WARN_ON(!irqs_disabled());
211
212 /*
213 * don't plug a stopped queue, it must be paired with blk_start_queue()
214 * which will restart the queueing
215 */
Coywolf Qi Hunt7daac492006-04-19 10:14:49 +0200216 if (blk_queue_stopped(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return;
218
Jens Axboee48ec692008-07-03 13:18:54 +0200219 if (!queue_flag_test_and_set(QUEUE_FLAG_PLUGGED, q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100221 trace_block_plug(q);
Jens Axboe2056a782006-03-23 20:00:26 +0100222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224EXPORT_SYMBOL(blk_plug_device);
225
Jens Axboe6c5e0c42008-08-01 20:31:32 +0200226/**
227 * blk_plug_device_unlocked - plug a device without queue lock held
228 * @q: The &struct request_queue to plug
229 *
230 * Description:
231 * Like @blk_plug_device(), but grabs the queue lock and disables
232 * interrupts.
233 **/
234void blk_plug_device_unlocked(struct request_queue *q)
235{
236 unsigned long flags;
237
238 spin_lock_irqsave(q->queue_lock, flags);
239 blk_plug_device(q);
240 spin_unlock_irqrestore(q->queue_lock, flags);
241}
242EXPORT_SYMBOL(blk_plug_device_unlocked);
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244/*
245 * remove the queue from the plugged list, if present. called with
246 * queue lock held and interrupts disabled.
247 */
Jens Axboe165125e2007-07-24 09:28:11 +0200248int blk_remove_plug(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 WARN_ON(!irqs_disabled());
251
Jens Axboee48ec692008-07-03 13:18:54 +0200252 if (!queue_flag_test_and_clear(QUEUE_FLAG_PLUGGED, q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return 0;
254
255 del_timer(&q->unplug_timer);
256 return 1;
257}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258EXPORT_SYMBOL(blk_remove_plug);
259
260/*
261 * remove the plug and let it rip..
262 */
Jens Axboe165125e2007-07-24 09:28:11 +0200263void __generic_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Coywolf Qi Hunt7daac492006-04-19 10:14:49 +0200265 if (unlikely(blk_queue_stopped(q)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return;
Jens Axboea31a9732008-10-17 13:58:29 +0200267 if (!blk_remove_plug(q) && !blk_queue_nonrot(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return;
269
Jens Axboe22e2c502005-06-27 10:55:12 +0200270 q->request_fn(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273/**
274 * generic_unplug_device - fire a request queue
Jens Axboe165125e2007-07-24 09:28:11 +0200275 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 *
277 * Description:
278 * Linux uses plugging to build bigger requests queues before letting
279 * the device have at them. If a queue is plugged, the I/O scheduler
280 * is still adding and merging requests on the queue. Once the queue
281 * gets unplugged, the request_fn defined for the queue is invoked and
282 * transfers started.
283 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200284void generic_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Jens Axboedbaf2c02008-05-07 09:48:17 +0200286 if (blk_queue_plugged(q)) {
287 spin_lock_irq(q->queue_lock);
288 __generic_unplug_device(q);
289 spin_unlock_irq(q->queue_lock);
290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292EXPORT_SYMBOL(generic_unplug_device);
293
294static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
295 struct page *page)
296{
Jens Axboe165125e2007-07-24 09:28:11 +0200297 struct request_queue *q = bdi->unplug_io_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500299 blk_unplug(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
Jens Axboe86db1e22008-01-29 14:53:40 +0100302void blk_unplug_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Jens Axboe165125e2007-07-24 09:28:11 +0200304 struct request_queue *q =
305 container_of(work, struct request_queue, unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100307 trace_block_unplug_io(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 q->unplug_fn(q);
309}
310
Jens Axboe86db1e22008-01-29 14:53:40 +0100311void blk_unplug_timeout(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
Jens Axboe165125e2007-07-24 09:28:11 +0200313 struct request_queue *q = (struct request_queue *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100315 trace_block_unplug_timer(q);
Jens Axboe18887ad2008-07-28 13:08:45 +0200316 kblockd_schedule_work(q, &q->unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500319void blk_unplug(struct request_queue *q)
320{
321 /*
322 * devices don't necessarily have an ->unplug_fn defined
323 */
324 if (q->unplug_fn) {
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100325 trace_block_unplug_io(q);
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500326 q->unplug_fn(q);
327 }
328}
329EXPORT_SYMBOL(blk_unplug);
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331/**
332 * blk_start_queue - restart a previously stopped queue
Jens Axboe165125e2007-07-24 09:28:11 +0200333 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 *
335 * Description:
336 * blk_start_queue() will clear the stop flag on the queue, and call
337 * the request_fn for the queue if it was in a stopped state when
338 * entered. Also see blk_stop_queue(). Queue lock must be held.
339 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200340void blk_start_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200342 WARN_ON(!irqs_disabled());
343
Nick Piggin75ad23b2008-04-29 14:48:33 +0200344 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
Tejun Heoa538cd02009-04-23 11:05:17 +0900345 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347EXPORT_SYMBOL(blk_start_queue);
348
349/**
350 * blk_stop_queue - stop a queue
Jens Axboe165125e2007-07-24 09:28:11 +0200351 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 *
353 * Description:
354 * The Linux block layer assumes that a block driver will consume all
355 * entries on the request queue when the request_fn strategy is called.
356 * Often this will not happen, because of hardware limitations (queue
357 * depth settings). If a device driver gets a 'queue full' response,
358 * or if it simply chooses not to queue more I/O at one point, it can
359 * call this function to prevent the request_fn from being called until
360 * the driver has signalled it's ready to go again. This happens by calling
361 * blk_start_queue() to restart queue operations. Queue lock must be held.
362 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200363void blk_stop_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
365 blk_remove_plug(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200366 queue_flag_set(QUEUE_FLAG_STOPPED, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368EXPORT_SYMBOL(blk_stop_queue);
369
370/**
371 * blk_sync_queue - cancel any pending callbacks on a queue
372 * @q: the queue
373 *
374 * Description:
375 * The block layer may perform asynchronous callback activity
376 * on a queue, such as calling the unplug function after a timeout.
377 * A block device may call blk_sync_queue to ensure that any
378 * such activity is cancelled, thus allowing it to release resources
Michael Opdenacker59c51592007-05-09 08:57:56 +0200379 * that the callbacks might use. The caller must already have made sure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 * that its ->make_request_fn will not re-add plugging prior to calling
381 * this function.
382 *
383 */
384void blk_sync_queue(struct request_queue *q)
385{
386 del_timer_sync(&q->unplug_timer);
Jens Axboe70ed28b2008-11-19 14:38:39 +0100387 del_timer_sync(&q->timeout);
Cheng Renquan64d01dc2008-12-03 12:41:39 +0100388 cancel_work_sync(&q->unplug_work);
Vivek Goyale43473b2010-09-15 17:06:35 -0400389 throtl_shutdown_timer_wq(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391EXPORT_SYMBOL(blk_sync_queue);
392
393/**
Jens Axboe80a4b582008-10-14 09:51:06 +0200394 * __blk_run_queue - run a single device queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200396 *
397 * Description:
398 * See @blk_run_queue. This variant must be called with the queue lock
399 * held and interrupts disabled.
400 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 */
Nick Piggin75ad23b2008-04-29 14:48:33 +0200402void __blk_run_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 blk_remove_plug(q);
Jens Axboedac07ec2006-05-11 08:20:16 +0200405
Tejun Heoa538cd02009-04-23 11:05:17 +0900406 if (unlikely(blk_queue_stopped(q)))
407 return;
408
409 if (elv_queue_empty(q))
410 return;
411
Jens Axboedac07ec2006-05-11 08:20:16 +0200412 /*
413 * Only recurse once to avoid overrunning the stack, let the unplug
414 * handling reinvoke the handler shortly if we already got there.
415 */
Tejun Heoa538cd02009-04-23 11:05:17 +0900416 if (!queue_flag_test_and_set(QUEUE_FLAG_REENTER, q)) {
417 q->request_fn(q);
418 queue_flag_clear(QUEUE_FLAG_REENTER, q);
419 } else {
420 queue_flag_set(QUEUE_FLAG_PLUGGED, q);
421 kblockd_schedule_work(q, &q->unplug_work);
422 }
Nick Piggin75ad23b2008-04-29 14:48:33 +0200423}
424EXPORT_SYMBOL(__blk_run_queue);
Jens Axboedac07ec2006-05-11 08:20:16 +0200425
Nick Piggin75ad23b2008-04-29 14:48:33 +0200426/**
427 * blk_run_queue - run a single device queue
428 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200429 *
430 * Description:
431 * Invoke request handling on this queue, if it has pending work to do.
Tejun Heoa7f55792009-04-23 11:05:17 +0900432 * May be used to restart queueing when a request has completed.
Nick Piggin75ad23b2008-04-29 14:48:33 +0200433 */
434void blk_run_queue(struct request_queue *q)
435{
436 unsigned long flags;
437
438 spin_lock_irqsave(q->queue_lock, flags);
439 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 spin_unlock_irqrestore(q->queue_lock, flags);
441}
442EXPORT_SYMBOL(blk_run_queue);
443
Jens Axboe165125e2007-07-24 09:28:11 +0200444void blk_put_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500445{
446 kobject_put(&q->kobj);
447}
Al Viro483f4af2006-03-18 18:34:37 -0500448
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500449/*
450 * Note: If a driver supplied the queue lock, it should not zap that lock
451 * unexpectedly as some queue cleanup components like elevator_exit() and
452 * blk_throtl_exit() need queue lock.
453 */
Jens Axboe6728cb02008-01-31 13:03:55 +0100454void blk_cleanup_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500455{
Jens Axboee3335de2008-09-18 09:22:54 -0700456 /*
457 * We know we have process context here, so we can be a little
458 * cautious and ensure that pending block actions on this device
459 * are done before moving on. Going into this function, we should
460 * not have processes doing IO to this device.
461 */
462 blk_sync_queue(q);
463
Matthew Garrett31373d02010-04-06 14:25:14 +0200464 del_timer_sync(&q->backing_dev_info.laptop_mode_wb_timer);
Al Viro483f4af2006-03-18 18:34:37 -0500465 mutex_lock(&q->sysfs_lock);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200466 queue_flag_set_unlocked(QUEUE_FLAG_DEAD, q);
Al Viro483f4af2006-03-18 18:34:37 -0500467 mutex_unlock(&q->sysfs_lock);
468
469 if (q->elevator)
470 elevator_exit(q->elevator);
471
472 blk_put_queue(q);
473}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474EXPORT_SYMBOL(blk_cleanup_queue);
475
Jens Axboe165125e2007-07-24 09:28:11 +0200476static int blk_init_free_list(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
478 struct request_list *rl = &q->rq;
479
Mike Snitzer1abec4f2010-05-25 13:15:15 -0400480 if (unlikely(rl->rq_pool))
481 return 0;
482
Jens Axboe1faa16d2009-04-06 14:48:01 +0200483 rl->count[BLK_RW_SYNC] = rl->count[BLK_RW_ASYNC] = 0;
484 rl->starved[BLK_RW_SYNC] = rl->starved[BLK_RW_ASYNC] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200485 rl->elvpriv = 0;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200486 init_waitqueue_head(&rl->wait[BLK_RW_SYNC]);
487 init_waitqueue_head(&rl->wait[BLK_RW_ASYNC]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Christoph Lameter19460892005-06-23 00:08:19 -0700489 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
490 mempool_free_slab, request_cachep, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 if (!rl->rq_pool)
493 return -ENOMEM;
494
495 return 0;
496}
497
Jens Axboe165125e2007-07-24 09:28:11 +0200498struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Christoph Lameter19460892005-06-23 00:08:19 -0700500 return blk_alloc_queue_node(gfp_mask, -1);
501}
502EXPORT_SYMBOL(blk_alloc_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Jens Axboe165125e2007-07-24 09:28:11 +0200504struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -0700505{
Jens Axboe165125e2007-07-24 09:28:11 +0200506 struct request_queue *q;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700507 int err;
Christoph Lameter19460892005-06-23 00:08:19 -0700508
Jens Axboe8324aa92008-01-29 14:51:59 +0100509 q = kmem_cache_alloc_node(blk_requestq_cachep,
Christoph Lameter94f60302007-07-17 04:03:29 -0700510 gfp_mask | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (!q)
512 return NULL;
513
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700514 q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
515 q->backing_dev_info.unplug_io_data = q;
Jens Axboe0989a022009-06-12 14:42:56 +0200516 q->backing_dev_info.ra_pages =
517 (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
518 q->backing_dev_info.state = 0;
519 q->backing_dev_info.capabilities = BDI_CAP_MAP_COPY;
Jens Axboed9938312009-06-12 14:45:52 +0200520 q->backing_dev_info.name = "block";
Jens Axboe0989a022009-06-12 14:42:56 +0200521
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700522 err = bdi_init(&q->backing_dev_info);
523 if (err) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100524 kmem_cache_free(blk_requestq_cachep, q);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700525 return NULL;
526 }
527
Vivek Goyale43473b2010-09-15 17:06:35 -0400528 if (blk_throtl_init(q)) {
529 kmem_cache_free(blk_requestq_cachep, q);
530 return NULL;
531 }
532
Matthew Garrett31373d02010-04-06 14:25:14 +0200533 setup_timer(&q->backing_dev_info.laptop_mode_wb_timer,
534 laptop_mode_timer_fn, (unsigned long) q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 init_timer(&q->unplug_timer);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700536 setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
537 INIT_LIST_HEAD(&q->timeout_list);
Tejun Heoae1b1532011-01-25 12:43:54 +0100538 INIT_LIST_HEAD(&q->flush_queue[0]);
539 INIT_LIST_HEAD(&q->flush_queue[1]);
540 INIT_LIST_HEAD(&q->flush_data_in_flight);
Peter Zijlstra713ada92008-10-16 13:44:57 +0200541 INIT_WORK(&q->unplug_work, blk_unplug_work);
Al Viro483f4af2006-03-18 18:34:37 -0500542
Jens Axboe8324aa92008-01-29 14:51:59 +0100543 kobject_init(&q->kobj, &blk_queue_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Al Viro483f4af2006-03-18 18:34:37 -0500545 mutex_init(&q->sysfs_lock);
Neil Browne7e72bf2008-05-14 16:05:54 -0700546 spin_lock_init(&q->__queue_lock);
Al Viro483f4af2006-03-18 18:34:37 -0500547
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500548 /*
549 * By default initialize queue_lock to internal lock and driver can
550 * override it later if need be.
551 */
552 q->queue_lock = &q->__queue_lock;
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return q;
555}
Christoph Lameter19460892005-06-23 00:08:19 -0700556EXPORT_SYMBOL(blk_alloc_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558/**
559 * blk_init_queue - prepare a request queue for use with a block device
560 * @rfn: The function to be called to process requests that have been
561 * placed on the queue.
562 * @lock: Request queue spin lock
563 *
564 * Description:
565 * If a block device wishes to use the standard request handling procedures,
566 * which sorts requests and coalesces adjacent requests, then it must
567 * call blk_init_queue(). The function @rfn will be called when there
568 * are requests on the queue that need to be processed. If the device
569 * supports plugging, then @rfn may not be called immediately when requests
570 * are available on the queue, but may be called at some time later instead.
571 * Plugged queues are generally unplugged when a buffer belonging to one
572 * of the requests on the queue is needed, or due to memory pressure.
573 *
574 * @rfn is not required, or even expected, to remove all requests off the
575 * queue, but only as many as it can handle at a time. If it does leave
576 * requests on the queue, it is responsible for arranging that the requests
577 * get dealt with eventually.
578 *
579 * The queue spin lock must be held while manipulating the requests on the
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200580 * request queue; this lock will be taken also from interrupt context, so irq
581 * disabling is needed for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 *
Randy Dunlap710027a2008-08-19 20:13:11 +0200583 * Function returns a pointer to the initialized request queue, or %NULL if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 * it didn't succeed.
585 *
586 * Note:
587 * blk_init_queue() must be paired with a blk_cleanup_queue() call
588 * when the block device is deactivated (such as at module unload).
589 **/
Christoph Lameter19460892005-06-23 00:08:19 -0700590
Jens Axboe165125e2007-07-24 09:28:11 +0200591struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Christoph Lameter19460892005-06-23 00:08:19 -0700593 return blk_init_queue_node(rfn, lock, -1);
594}
595EXPORT_SYMBOL(blk_init_queue);
596
Jens Axboe165125e2007-07-24 09:28:11 +0200597struct request_queue *
Christoph Lameter19460892005-06-23 00:08:19 -0700598blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
599{
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600600 struct request_queue *uninit_q, *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600602 uninit_q = blk_alloc_queue_node(GFP_KERNEL, node_id);
603 if (!uninit_q)
604 return NULL;
605
606 q = blk_init_allocated_queue_node(uninit_q, rfn, lock, node_id);
607 if (!q)
608 blk_cleanup_queue(uninit_q);
609
610 return q;
Mike Snitzer01effb02010-05-11 08:57:42 +0200611}
612EXPORT_SYMBOL(blk_init_queue_node);
613
614struct request_queue *
615blk_init_allocated_queue(struct request_queue *q, request_fn_proc *rfn,
616 spinlock_t *lock)
617{
618 return blk_init_allocated_queue_node(q, rfn, lock, -1);
619}
620EXPORT_SYMBOL(blk_init_allocated_queue);
621
622struct request_queue *
623blk_init_allocated_queue_node(struct request_queue *q, request_fn_proc *rfn,
624 spinlock_t *lock, int node_id)
625{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (!q)
627 return NULL;
628
Christoph Lameter19460892005-06-23 00:08:19 -0700629 q->node = node_id;
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600630 if (blk_init_free_list(q))
Al Viro8669aaf2006-03-18 13:50:00 -0500631 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 q->request_fn = rfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 q->prep_rq_fn = NULL;
James Bottomley28018c22010-07-01 19:49:17 +0900635 q->unprep_rq_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 q->unplug_fn = generic_unplug_device;
Jens Axboebc58ba92009-01-23 10:54:44 +0100637 q->queue_flags = QUEUE_FLAG_DEFAULT;
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500638
639 /* Override internal queue lock with supplied lock pointer */
640 if (lock)
641 q->queue_lock = lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Jens Axboef3b144a2009-03-06 08:48:33 +0100643 /*
644 * This also sets hw/phys segments, boundary and size
645 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 blk_queue_make_request(q, __make_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Alan Stern44ec9542007-02-20 11:01:57 -0500648 q->sg_reserved_size = INT_MAX;
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 /*
651 * all done
652 */
653 if (!elevator_init(q, NULL)) {
654 blk_queue_congestion_threshold(q);
655 return q;
656 }
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return NULL;
659}
Mike Snitzer01effb02010-05-11 08:57:42 +0200660EXPORT_SYMBOL(blk_init_allocated_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Jens Axboe165125e2007-07-24 09:28:11 +0200662int blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Nick Pigginfde6ad22005-06-23 00:08:53 -0700664 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
Al Viro483f4af2006-03-18 18:34:37 -0500665 kobject_get(&q->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return 0;
667 }
668
669 return 1;
670}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Jens Axboe165125e2007-07-24 09:28:11 +0200672static inline void blk_free_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Jens Axboe4aff5e22006-08-10 08:44:47 +0200674 if (rq->cmd_flags & REQ_ELVPRIV)
Tejun Heocb98fc82005-10-28 08:29:39 +0200675 elv_put_request(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 mempool_free(rq, q->rq.rq_pool);
677}
678
Jens Axboe1ea25ecb2006-07-18 22:24:11 +0200679static struct request *
Jerome Marchand42dad762009-04-22 14:01:49 +0200680blk_alloc_request(struct request_queue *q, int flags, int priv, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
682 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
683
684 if (!rq)
685 return NULL;
686
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200687 blk_rq_init(q, rq);
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200688
Jerome Marchand42dad762009-04-22 14:01:49 +0200689 rq->cmd_flags = flags | REQ_ALLOCED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Tejun Heocb98fc82005-10-28 08:29:39 +0200691 if (priv) {
Jens Axboecb78b282006-07-28 09:32:57 +0200692 if (unlikely(elv_set_request(q, rq, gfp_mask))) {
Tejun Heocb98fc82005-10-28 08:29:39 +0200693 mempool_free(rq, q->rq.rq_pool);
694 return NULL;
695 }
Jens Axboe4aff5e22006-08-10 08:44:47 +0200696 rq->cmd_flags |= REQ_ELVPRIV;
Tejun Heocb98fc82005-10-28 08:29:39 +0200697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Tejun Heocb98fc82005-10-28 08:29:39 +0200699 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
702/*
703 * ioc_batching returns true if the ioc is a valid batching request and
704 * should be given priority access to a request.
705 */
Jens Axboe165125e2007-07-24 09:28:11 +0200706static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
708 if (!ioc)
709 return 0;
710
711 /*
712 * Make sure the process is able to allocate at least 1 request
713 * even if the batch times out, otherwise we could theoretically
714 * lose wakeups.
715 */
716 return ioc->nr_batch_requests == q->nr_batching ||
717 (ioc->nr_batch_requests > 0
718 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
719}
720
721/*
722 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
723 * will cause the process to be a "batcher" on all queues in the system. This
724 * is the behaviour we want though - once it gets a wakeup it should be given
725 * a nice run.
726 */
Jens Axboe165125e2007-07-24 09:28:11 +0200727static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
729 if (!ioc || ioc_batching(q, ioc))
730 return;
731
732 ioc->nr_batch_requests = q->nr_batching;
733 ioc->last_waited = jiffies;
734}
735
Jens Axboe1faa16d2009-04-06 14:48:01 +0200736static void __freed_request(struct request_queue *q, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
738 struct request_list *rl = &q->rq;
739
Jens Axboe1faa16d2009-04-06 14:48:01 +0200740 if (rl->count[sync] < queue_congestion_off_threshold(q))
741 blk_clear_queue_congested(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Jens Axboe1faa16d2009-04-06 14:48:01 +0200743 if (rl->count[sync] + 1 <= q->nr_requests) {
744 if (waitqueue_active(&rl->wait[sync]))
745 wake_up(&rl->wait[sync]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Jens Axboe1faa16d2009-04-06 14:48:01 +0200747 blk_clear_queue_full(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
749}
750
751/*
752 * A request has just been released. Account for it, update the full and
753 * congestion status, wake up any waiters. Called under q->queue_lock.
754 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200755static void freed_request(struct request_queue *q, int sync, int priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
757 struct request_list *rl = &q->rq;
758
Jens Axboe1faa16d2009-04-06 14:48:01 +0200759 rl->count[sync]--;
Tejun Heocb98fc82005-10-28 08:29:39 +0200760 if (priv)
761 rl->elvpriv--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Jens Axboe1faa16d2009-04-06 14:48:01 +0200763 __freed_request(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Jens Axboe1faa16d2009-04-06 14:48:01 +0200765 if (unlikely(rl->starved[sync ^ 1]))
766 __freed_request(q, sync ^ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769/*
Mike Snitzer9d5a4e92011-02-11 11:05:46 +0100770 * Determine if elevator data should be initialized when allocating the
771 * request associated with @bio.
772 */
773static bool blk_rq_should_init_elevator(struct bio *bio)
774{
775 if (!bio)
776 return true;
777
778 /*
779 * Flush requests do not use the elevator so skip initialization.
780 * This allows a request to share the flush and elevator data.
781 */
782 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA))
783 return false;
784
785 return true;
786}
787
788/*
Nick Piggind6344532005-06-28 20:45:14 -0700789 * Get a free request, queue_lock must be held.
790 * Returns NULL on failure, with queue_lock held.
791 * Returns !NULL on success, with queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 */
Jens Axboe165125e2007-07-24 09:28:11 +0200793static struct request *get_request(struct request_queue *q, int rw_flags,
Jens Axboe7749a8d2006-12-13 13:02:26 +0100794 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
796 struct request *rq = NULL;
797 struct request_list *rl = &q->rq;
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100798 struct io_context *ioc = NULL;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200799 const bool is_sync = rw_is_sync(rw_flags) != 0;
Mike Snitzer9d5a4e92011-02-11 11:05:46 +0100800 int may_queue, priv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Jens Axboe7749a8d2006-12-13 13:02:26 +0100802 may_queue = elv_may_queue(q, rw_flags);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100803 if (may_queue == ELV_MQUEUE_NO)
804 goto rq_starved;
805
Jens Axboe1faa16d2009-04-06 14:48:01 +0200806 if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) {
807 if (rl->count[is_sync]+1 >= q->nr_requests) {
Jens Axboeb5deef92006-07-19 23:39:40 +0200808 ioc = current_io_context(GFP_ATOMIC, q->node);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100809 /*
810 * The queue will fill after this allocation, so set
811 * it as full, and mark this process as "batching".
812 * This process will be allowed to complete a batch of
813 * requests, others will be blocked.
814 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200815 if (!blk_queue_full(q, is_sync)) {
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100816 ioc_set_batching(q, ioc);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200817 blk_set_queue_full(q, is_sync);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100818 } else {
819 if (may_queue != ELV_MQUEUE_MUST
820 && !ioc_batching(q, ioc)) {
821 /*
822 * The queue is full and the allocating
823 * process is not a "batcher", and not
824 * exempted by the IO scheduler
825 */
826 goto out;
827 }
828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
Jens Axboe1faa16d2009-04-06 14:48:01 +0200830 blk_set_queue_congested(q, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832
Jens Axboe082cf692005-06-28 16:35:11 +0200833 /*
834 * Only allow batching queuers to allocate up to 50% over the defined
835 * limit of requests, otherwise we could have thousands of requests
836 * allocated with any setting of ->nr_requests
837 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200838 if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
Jens Axboe082cf692005-06-28 16:35:11 +0200839 goto out;
Hugh Dickinsfd782a42005-06-29 15:15:40 +0100840
Jens Axboe1faa16d2009-04-06 14:48:01 +0200841 rl->count[is_sync]++;
842 rl->starved[is_sync] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200843
Mike Snitzer9d5a4e92011-02-11 11:05:46 +0100844 if (blk_rq_should_init_elevator(bio)) {
845 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
846 if (priv)
847 rl->elvpriv++;
848 }
Tejun Heocb98fc82005-10-28 08:29:39 +0200849
Jens Axboef253b862010-10-24 22:06:02 +0200850 if (blk_queue_io_stat(q))
851 rw_flags |= REQ_IO_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 spin_unlock_irq(q->queue_lock);
853
Jens Axboe7749a8d2006-12-13 13:02:26 +0100854 rq = blk_alloc_request(q, rw_flags, priv, gfp_mask);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100855 if (unlikely(!rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 /*
857 * Allocation failed presumably due to memory. Undo anything
858 * we might have messed up.
859 *
860 * Allocating task should really be put onto the front of the
861 * wait queue, but this is pretty rare.
862 */
863 spin_lock_irq(q->queue_lock);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200864 freed_request(q, is_sync, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866 /*
867 * in the very unlikely event that allocation failed and no
868 * requests for this direction was pending, mark us starved
869 * so that freeing of a request in the other direction will
870 * notice us. another possible fix would be to split the
871 * rq mempool into READ and WRITE
872 */
873rq_starved:
Jens Axboe1faa16d2009-04-06 14:48:01 +0200874 if (unlikely(rl->count[is_sync] == 0))
875 rl->starved[is_sync] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 goto out;
878 }
879
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100880 /*
881 * ioc may be NULL here, and ioc_batching will be false. That's
882 * OK, if the queue is under the request limit then requests need
883 * not count toward the nr_batch_requests limit. There will always
884 * be some limit enforced by BLK_BATCH_TIME.
885 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (ioc_batching(q, ioc))
887 ioc->nr_batch_requests--;
Jens Axboe6728cb02008-01-31 13:03:55 +0100888
Jens Axboe1faa16d2009-04-06 14:48:01 +0200889 trace_block_getrq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return rq;
892}
893
894/*
895 * No available requests for this queue, unplug the device and wait for some
896 * requests to become available.
Nick Piggind6344532005-06-28 20:45:14 -0700897 *
898 * Called with q->queue_lock held, and returns with it unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 */
Jens Axboe165125e2007-07-24 09:28:11 +0200900static struct request *get_request_wait(struct request_queue *q, int rw_flags,
Jens Axboe22e2c502005-06-27 10:55:12 +0200901 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Jens Axboe1faa16d2009-04-06 14:48:01 +0200903 const bool is_sync = rw_is_sync(rw_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 struct request *rq;
905
Jens Axboe7749a8d2006-12-13 13:02:26 +0100906 rq = get_request(q, rw_flags, bio, GFP_NOIO);
Nick Piggin450991b2005-06-28 20:45:13 -0700907 while (!rq) {
908 DEFINE_WAIT(wait);
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200909 struct io_context *ioc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 struct request_list *rl = &q->rq;
911
Jens Axboe1faa16d2009-04-06 14:48:01 +0200912 prepare_to_wait_exclusive(&rl->wait[is_sync], &wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 TASK_UNINTERRUPTIBLE);
914
Jens Axboe1faa16d2009-04-06 14:48:01 +0200915 trace_block_sleeprq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200917 __generic_unplug_device(q);
918 spin_unlock_irq(q->queue_lock);
919 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200921 /*
922 * After sleeping, we become a "batching" process and
923 * will be able to allocate at least one request, and
924 * up to a big batch of them for a small period time.
925 * See ioc_batching, ioc_set_batching
926 */
927 ioc = current_io_context(GFP_NOIO, q->node);
928 ioc_set_batching(q, ioc);
Jens Axboe2056a782006-03-23 20:00:26 +0100929
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200930 spin_lock_irq(q->queue_lock);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200931 finish_wait(&rl->wait[is_sync], &wait);
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200932
933 rq = get_request(q, rw_flags, bio, GFP_NOIO);
934 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 return rq;
937}
938
Jens Axboe165125e2007-07-24 09:28:11 +0200939struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
941 struct request *rq;
942
943 BUG_ON(rw != READ && rw != WRITE);
944
Nick Piggind6344532005-06-28 20:45:14 -0700945 spin_lock_irq(q->queue_lock);
946 if (gfp_mask & __GFP_WAIT) {
Jens Axboe22e2c502005-06-27 10:55:12 +0200947 rq = get_request_wait(q, rw, NULL);
Nick Piggind6344532005-06-28 20:45:14 -0700948 } else {
Jens Axboe22e2c502005-06-27 10:55:12 +0200949 rq = get_request(q, rw, NULL, gfp_mask);
Nick Piggind6344532005-06-28 20:45:14 -0700950 if (!rq)
951 spin_unlock_irq(q->queue_lock);
952 }
953 /* q->queue_lock is unlocked at this point */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 return rq;
956}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957EXPORT_SYMBOL(blk_get_request);
958
959/**
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300960 * blk_make_request - given a bio, allocate a corresponding struct request.
Randy Dunlap8ebf9752009-06-11 20:00:41 -0700961 * @q: target request queue
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300962 * @bio: The bio describing the memory mappings that will be submitted for IO.
963 * It may be a chained-bio properly constructed by block/bio layer.
Randy Dunlap8ebf9752009-06-11 20:00:41 -0700964 * @gfp_mask: gfp flags to be used for memory allocation
Jens Axboedc72ef42006-07-20 14:54:05 +0200965 *
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300966 * blk_make_request is the parallel of generic_make_request for BLOCK_PC
967 * type commands. Where the struct request needs to be farther initialized by
968 * the caller. It is passed a &struct bio, which describes the memory info of
969 * the I/O transfer.
970 *
971 * The caller of blk_make_request must make sure that bi_io_vec
972 * are set to describe the memory buffers. That bio_data_dir() will return
973 * the needed direction of the request. (And all bio's in the passed bio-chain
974 * are properly set accordingly)
975 *
976 * If called under none-sleepable conditions, mapped bio buffers must not
977 * need bouncing, by calling the appropriate masked or flagged allocator,
978 * suitable for the target device. Otherwise the call to blk_queue_bounce will
979 * BUG.
Jens Axboe53674ac2009-05-19 19:52:35 +0200980 *
981 * WARNING: When allocating/cloning a bio-chain, careful consideration should be
982 * given to how you allocate bios. In particular, you cannot use __GFP_WAIT for
983 * anything but the first bio in the chain. Otherwise you risk waiting for IO
984 * completion of a bio that hasn't been submitted yet, thus resulting in a
985 * deadlock. Alternatively bios should be allocated using bio_kmalloc() instead
986 * of bio_alloc(), as that avoids the mempool deadlock.
987 * If possible a big IO should be split into smaller parts when allocation
988 * fails. Partial allocation should not be an error, or you risk a live-lock.
Jens Axboedc72ef42006-07-20 14:54:05 +0200989 */
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300990struct request *blk_make_request(struct request_queue *q, struct bio *bio,
991 gfp_t gfp_mask)
Jens Axboedc72ef42006-07-20 14:54:05 +0200992{
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300993 struct request *rq = blk_get_request(q, bio_data_dir(bio), gfp_mask);
994
995 if (unlikely(!rq))
996 return ERR_PTR(-ENOMEM);
997
998 for_each_bio(bio) {
999 struct bio *bounce_bio = bio;
1000 int ret;
1001
1002 blk_queue_bounce(q, &bounce_bio);
1003 ret = blk_rq_append_bio(q, rq, bounce_bio);
1004 if (unlikely(ret)) {
1005 blk_put_request(rq);
1006 return ERR_PTR(ret);
1007 }
1008 }
1009
1010 return rq;
Jens Axboedc72ef42006-07-20 14:54:05 +02001011}
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001012EXPORT_SYMBOL(blk_make_request);
Jens Axboedc72ef42006-07-20 14:54:05 +02001013
1014/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 * blk_requeue_request - put a request back on queue
1016 * @q: request queue where request should be inserted
1017 * @rq: request to be inserted
1018 *
1019 * Description:
1020 * Drivers often keep queueing requests until the hardware cannot accept
1021 * more, when that condition happens we need to put the request back
1022 * on the queue. Must be called with queue lock held.
1023 */
Jens Axboe165125e2007-07-24 09:28:11 +02001024void blk_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Jens Axboe242f9dc2008-09-14 05:55:09 -07001026 blk_delete_timer(rq);
1027 blk_clear_rq_complete(rq);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001028 trace_block_rq_requeue(q, rq);
Jens Axboe2056a782006-03-23 20:00:26 +01001029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 if (blk_rq_tagged(rq))
1031 blk_queue_end_tag(q, rq);
1032
James Bottomleyba396a62009-05-27 14:17:08 +02001033 BUG_ON(blk_queued_rq(rq));
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 elv_requeue_request(q, rq);
1036}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037EXPORT_SYMBOL(blk_requeue_request);
1038
1039/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001040 * blk_insert_request - insert a special request into a request queue
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 * @q: request queue where request should be inserted
1042 * @rq: request to be inserted
1043 * @at_head: insert request at head or tail of queue
1044 * @data: private data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 *
1046 * Description:
1047 * Many block devices need to execute commands asynchronously, so they don't
1048 * block the whole kernel from preemption during request execution. This is
1049 * accomplished normally by inserting aritficial requests tagged as
Randy Dunlap710027a2008-08-19 20:13:11 +02001050 * REQ_TYPE_SPECIAL in to the corresponding request queue, and letting them
1051 * be scheduled for actual execution by the request queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 *
1053 * We have the option of inserting the head or the tail of the queue.
1054 * Typically we use the tail for new ioctls and so forth. We use the head
1055 * of the queue for things like a QUEUE_FULL message from a device, or a
1056 * host that is unable to accept a particular command.
1057 */
Jens Axboe165125e2007-07-24 09:28:11 +02001058void blk_insert_request(struct request_queue *q, struct request *rq,
Tejun Heo 867d1192005-04-24 02:06:05 -05001059 int at_head, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
Tejun Heo 867d1192005-04-24 02:06:05 -05001061 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 unsigned long flags;
1063
1064 /*
1065 * tell I/O scheduler that this isn't a regular read/write (ie it
1066 * must not attempt merges on this) and that it acts as a soft
1067 * barrier
1068 */
Jens Axboe4aff5e22006-08-10 08:44:47 +02001069 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 rq->special = data;
1072
1073 spin_lock_irqsave(q->queue_lock, flags);
1074
1075 /*
1076 * If command is tagged, release the tag
1077 */
Tejun Heo 867d1192005-04-24 02:06:05 -05001078 if (blk_rq_tagged(rq))
1079 blk_queue_end_tag(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Jerome Marchandb238b3d2007-10-23 15:05:46 +02001081 drive_stat_acct(rq, 1);
Tejun Heo 867d1192005-04-24 02:06:05 -05001082 __elv_add_request(q, rq, where, 0);
Tejun Heoa7f55792009-04-23 11:05:17 +09001083 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 spin_unlock_irqrestore(q->queue_lock, flags);
1085}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086EXPORT_SYMBOL(blk_insert_request);
1087
Tejun Heo074a7ac2008-08-25 19:56:14 +09001088static void part_round_stats_single(int cpu, struct hd_struct *part,
1089 unsigned long now)
1090{
1091 if (now == part->stamp)
1092 return;
1093
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001094 if (part_in_flight(part)) {
Tejun Heo074a7ac2008-08-25 19:56:14 +09001095 __part_stat_add(cpu, part, time_in_queue,
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001096 part_in_flight(part) * (now - part->stamp));
Tejun Heo074a7ac2008-08-25 19:56:14 +09001097 __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
1098 }
1099 part->stamp = now;
1100}
1101
1102/**
Randy Dunlap496aa8a2008-10-16 07:46:23 +02001103 * part_round_stats() - Round off the performance stats on a struct disk_stats.
1104 * @cpu: cpu number for stats access
1105 * @part: target partition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 *
1107 * The average IO queue length and utilisation statistics are maintained
1108 * by observing the current state of the queue length and the amount of
1109 * time it has been in this state for.
1110 *
1111 * Normally, that accounting is done on IO completion, but that can result
1112 * in more than a second's worth of IO being accounted for within any one
1113 * second, leading to >100% utilisation. To deal with that, we call this
1114 * function to do a round-off before returning the results when reading
1115 * /proc/diskstats. This accounts immediately for all queue usage up to
1116 * the current jiffies and restarts the counters again.
1117 */
Tejun Heoc9959052008-08-25 19:47:21 +09001118void part_round_stats(int cpu, struct hd_struct *part)
Jerome Marchand6f2576af2008-02-08 11:04:35 +01001119{
1120 unsigned long now = jiffies;
1121
Tejun Heo074a7ac2008-08-25 19:56:14 +09001122 if (part->partno)
1123 part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
1124 part_round_stats_single(cpu, part, now);
Jerome Marchand6f2576af2008-02-08 11:04:35 +01001125}
Tejun Heo074a7ac2008-08-25 19:56:14 +09001126EXPORT_SYMBOL_GPL(part_round_stats);
Jerome Marchand6f2576af2008-02-08 11:04:35 +01001127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128/*
1129 * queue lock must be held
1130 */
Jens Axboe165125e2007-07-24 09:28:11 +02001131void __blk_put_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 if (unlikely(!q))
1134 return;
1135 if (unlikely(--req->ref_count))
1136 return;
1137
Tejun Heo8922e162005-10-20 16:23:44 +02001138 elv_completed_request(q, req);
1139
Boaz Harrosh1cd96c22009-03-24 12:35:07 +01001140 /* this is a bio leak */
1141 WARN_ON(req->bio != NULL);
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 /*
1144 * Request may not have originated from ll_rw_blk. if not,
1145 * it didn't come out of our reserved rq pools
1146 */
Jens Axboe49171e52006-08-10 08:59:11 +02001147 if (req->cmd_flags & REQ_ALLOCED) {
Jens Axboe1faa16d2009-04-06 14:48:01 +02001148 int is_sync = rq_is_sync(req) != 0;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001149 int priv = req->cmd_flags & REQ_ELVPRIV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 BUG_ON(!list_empty(&req->queuelist));
Jens Axboe98170642006-07-28 09:23:08 +02001152 BUG_ON(!hlist_unhashed(&req->hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154 blk_free_request(q, req);
Jens Axboe1faa16d2009-04-06 14:48:01 +02001155 freed_request(q, is_sync, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
1157}
Mike Christie6e39b692005-11-11 05:30:24 -06001158EXPORT_SYMBOL_GPL(__blk_put_request);
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160void blk_put_request(struct request *req)
1161{
Tejun Heo8922e162005-10-20 16:23:44 +02001162 unsigned long flags;
Jens Axboe165125e2007-07-24 09:28:11 +02001163 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
FUJITA Tomonori52a93ba2008-07-15 21:21:45 +02001165 spin_lock_irqsave(q->queue_lock, flags);
1166 __blk_put_request(q, req);
1167 spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169EXPORT_SYMBOL(blk_put_request);
1170
Christoph Hellwig66ac0282010-06-18 16:59:42 +02001171/**
1172 * blk_add_request_payload - add a payload to a request
1173 * @rq: request to update
1174 * @page: page backing the payload
1175 * @len: length of the payload.
1176 *
1177 * This allows to later add a payload to an already submitted request by
1178 * a block driver. The driver needs to take care of freeing the payload
1179 * itself.
1180 *
1181 * Note that this is a quite horrible hack and nothing but handling of
1182 * discard requests should ever use it.
1183 */
1184void blk_add_request_payload(struct request *rq, struct page *page,
1185 unsigned int len)
1186{
1187 struct bio *bio = rq->bio;
1188
1189 bio->bi_io_vec->bv_page = page;
1190 bio->bi_io_vec->bv_offset = 0;
1191 bio->bi_io_vec->bv_len = len;
1192
1193 bio->bi_size = len;
1194 bio->bi_vcnt = 1;
1195 bio->bi_phys_segments = 1;
1196
1197 rq->__data_len = rq->resid_len = len;
1198 rq->nr_phys_segments = 1;
1199 rq->buffer = bio_data(bio);
1200}
1201EXPORT_SYMBOL_GPL(blk_add_request_payload);
1202
Jens Axboe86db1e22008-01-29 14:53:40 +01001203void init_request_from_bio(struct request *req, struct bio *bio)
Tejun Heo52d9e672006-01-06 09:49:58 +01001204{
Jens Axboec7c22e42008-09-13 20:26:01 +02001205 req->cpu = bio->bi_comp_cpu;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001206 req->cmd_type = REQ_TYPE_FS;
Tejun Heo52d9e672006-01-06 09:49:58 +01001207
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001208 req->cmd_flags |= bio->bi_rw & REQ_COMMON_MASK;
1209 if (bio->bi_rw & REQ_RAHEAD)
Tejun Heoa82afdf2009-07-03 17:48:16 +09001210 req->cmd_flags |= REQ_FAILFAST_MASK;
Jens Axboeb31dc662006-06-13 08:26:10 +02001211
Tejun Heo52d9e672006-01-06 09:49:58 +01001212 req->errors = 0;
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001213 req->__sector = bio->bi_sector;
Tejun Heo52d9e672006-01-06 09:49:58 +01001214 req->ioprio = bio_prio(bio);
NeilBrownbc1c56f2007-08-16 13:31:30 +02001215 blk_rq_bio_prep(req->q, req, bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001216}
1217
Jens Axboe644b2d92009-04-06 14:48:06 +02001218/*
1219 * Only disabling plugging for non-rotational devices if it does tagging
1220 * as well, otherwise we do need the proper merging
1221 */
1222static inline bool queue_should_plug(struct request_queue *q)
1223{
Jens Axboe79da06442010-02-23 08:40:43 +01001224 return !(blk_queue_nonrot(q) && blk_queue_tagged(q));
Jens Axboe644b2d92009-04-06 14:48:06 +02001225}
1226
Jens Axboe165125e2007-07-24 09:28:11 +02001227static int __make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
Nick Piggin450991b2005-06-28 20:45:13 -07001229 struct request *req;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001230 int el_ret;
1231 unsigned int bytes = bio->bi_size;
Jens Axboe51da90f2006-07-18 04:14:45 +02001232 const unsigned short prio = bio_prio(bio);
Jiri Slaby5e00d1b2010-08-12 14:31:06 +02001233 const bool sync = !!(bio->bi_rw & REQ_SYNC);
1234 const bool unplug = !!(bio->bi_rw & REQ_UNPLUG);
1235 const unsigned long ff = bio->bi_rw & REQ_FAILFAST_MASK;
Tejun Heo28e7d182010-09-03 11:56:16 +02001236 int where = ELEVATOR_INSERT_SORT;
Jens Axboe7749a8d2006-12-13 13:02:26 +01001237 int rw_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 /*
1240 * low level driver can indicate that it wants pages above a
1241 * certain limit bounced to low memory (ie for highmem, or even
1242 * ISA dma in theory)
1243 */
1244 blk_queue_bounce(q, &bio);
1245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 spin_lock_irq(q->queue_lock);
1247
Tejun Heo4fed9472010-09-03 11:56:17 +02001248 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) {
Tejun Heoae1b1532011-01-25 12:43:54 +01001249 where = ELEVATOR_INSERT_FLUSH;
Tejun Heo28e7d182010-09-03 11:56:16 +02001250 goto get_rq;
1251 }
1252
1253 if (elv_queue_empty(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 goto get_rq;
1255
1256 el_ret = elv_merge(q, &req, bio);
1257 switch (el_ret) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001258 case ELEVATOR_BACK_MERGE:
1259 BUG_ON(!rq_mergeable(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Jens Axboe6728cb02008-01-31 13:03:55 +01001261 if (!ll_back_merge_fn(q, req, bio))
1262 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001264 trace_block_bio_backmerge(q, bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001265
Tejun Heo80a761f2009-07-03 17:48:17 +09001266 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1267 blk_rq_set_mixed_merge(req);
1268
Jens Axboe6728cb02008-01-31 13:03:55 +01001269 req->biotail->bi_next = bio;
1270 req->biotail = bio;
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001271 req->__data_len += bytes;
Jens Axboe6728cb02008-01-31 13:03:55 +01001272 req->ioprio = ioprio_best(req->ioprio, prio);
Jens Axboeab780f12008-08-26 10:25:02 +02001273 if (!blk_rq_cpu_valid(req))
1274 req->cpu = bio->bi_comp_cpu;
Jens Axboe6728cb02008-01-31 13:03:55 +01001275 drive_stat_acct(req, 0);
Divyesh Shah812d4022010-04-08 21:14:23 -07001276 elv_bio_merged(q, req, bio);
Jens Axboe6728cb02008-01-31 13:03:55 +01001277 if (!attempt_back_merge(q, req))
1278 elv_merged_request(q, req, el_ret);
1279 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Jens Axboe6728cb02008-01-31 13:03:55 +01001281 case ELEVATOR_FRONT_MERGE:
1282 BUG_ON(!rq_mergeable(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Jens Axboe6728cb02008-01-31 13:03:55 +01001284 if (!ll_front_merge_fn(q, req, bio))
1285 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001287 trace_block_bio_frontmerge(q, bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001288
Tejun Heo80a761f2009-07-03 17:48:17 +09001289 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff) {
1290 blk_rq_set_mixed_merge(req);
1291 req->cmd_flags &= ~REQ_FAILFAST_MASK;
1292 req->cmd_flags |= ff;
1293 }
1294
Jens Axboe6728cb02008-01-31 13:03:55 +01001295 bio->bi_next = req->bio;
1296 req->bio = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Jens Axboe6728cb02008-01-31 13:03:55 +01001298 /*
1299 * may not be valid. if the low level driver said
1300 * it didn't need a bounce buffer then it better
1301 * not touch req->buffer either...
1302 */
1303 req->buffer = bio_data(bio);
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001304 req->__sector = bio->bi_sector;
1305 req->__data_len += bytes;
Jens Axboe6728cb02008-01-31 13:03:55 +01001306 req->ioprio = ioprio_best(req->ioprio, prio);
Jens Axboeab780f12008-08-26 10:25:02 +02001307 if (!blk_rq_cpu_valid(req))
1308 req->cpu = bio->bi_comp_cpu;
Jens Axboe6728cb02008-01-31 13:03:55 +01001309 drive_stat_acct(req, 0);
Divyesh Shah812d4022010-04-08 21:14:23 -07001310 elv_bio_merged(q, req, bio);
Jens Axboe6728cb02008-01-31 13:03:55 +01001311 if (!attempt_front_merge(q, req))
1312 elv_merged_request(q, req, el_ret);
1313 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Jens Axboe6728cb02008-01-31 13:03:55 +01001315 /* ELV_NO_MERGE: elevator says don't/can't merge. */
1316 default:
1317 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
1319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320get_rq:
Nick Piggin450991b2005-06-28 20:45:13 -07001321 /*
Jens Axboe7749a8d2006-12-13 13:02:26 +01001322 * This sync check and mask will be re-done in init_request_from_bio(),
1323 * but we need to set it earlier to expose the sync flag to the
1324 * rq allocator and io schedulers.
1325 */
1326 rw_flags = bio_data_dir(bio);
1327 if (sync)
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001328 rw_flags |= REQ_SYNC;
Jens Axboe7749a8d2006-12-13 13:02:26 +01001329
1330 /*
Nick Piggin450991b2005-06-28 20:45:13 -07001331 * Grab a free request. This is might sleep but can not fail.
Nick Piggind6344532005-06-28 20:45:14 -07001332 * Returns with the queue unlocked.
Nick Piggin450991b2005-06-28 20:45:13 -07001333 */
Jens Axboe7749a8d2006-12-13 13:02:26 +01001334 req = get_request_wait(q, rw_flags, bio);
Nick Piggind6344532005-06-28 20:45:14 -07001335
Nick Piggin450991b2005-06-28 20:45:13 -07001336 /*
1337 * After dropping the lock and possibly sleeping here, our request
1338 * may now be mergeable after it had proven unmergeable (above).
1339 * We don't worry about that case for efficiency. It won't happen
1340 * often, and the elevators are able to handle it.
1341 */
Tejun Heo52d9e672006-01-06 09:49:58 +01001342 init_request_from_bio(req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Nick Piggin450991b2005-06-28 20:45:13 -07001344 spin_lock_irq(q->queue_lock);
Jens Axboec7c22e42008-09-13 20:26:01 +02001345 if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags) ||
1346 bio_flagged(bio, BIO_CPU_AFFINE))
1347 req->cpu = blk_cpu_to_group(smp_processor_id());
Jens Axboe644b2d92009-04-06 14:48:06 +02001348 if (queue_should_plug(q) && elv_queue_empty(q))
Nick Piggin450991b2005-06-28 20:45:13 -07001349 blk_plug_device(q);
Tejun Heodd831002010-09-03 11:56:16 +02001350
1351 /* insert the request into the elevator */
1352 drive_stat_acct(req, 1);
Tejun Heo28e7d182010-09-03 11:56:16 +02001353 __elv_add_request(q, req, where, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354out:
Jens Axboe644b2d92009-04-06 14:48:06 +02001355 if (unplug || !queue_should_plug(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 __generic_unplug_device(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 spin_unlock_irq(q->queue_lock);
1358 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359}
1360
1361/*
1362 * If bio->bi_dev is a partition, remap the location
1363 */
1364static inline void blk_partition_remap(struct bio *bio)
1365{
1366 struct block_device *bdev = bio->bi_bdev;
1367
Jens Axboebf2de6f2007-09-27 13:01:25 +02001368 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 struct hd_struct *p = bdev->bd_part;
Jens Axboea3623572005-11-01 09:26:16 +01001370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 bio->bi_sector += p->start_sect;
1372 bio->bi_bdev = bdev->bd_contains;
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001373
Mike Snitzerd07335e2010-11-16 12:52:38 +01001374 trace_block_bio_remap(bdev_get_queue(bio->bi_bdev), bio,
1375 bdev->bd_dev,
1376 bio->bi_sector - p->start_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
1378}
1379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380static void handle_bad_sector(struct bio *bio)
1381{
1382 char b[BDEVNAME_SIZE];
1383
1384 printk(KERN_INFO "attempt to access beyond end of device\n");
1385 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1386 bdevname(bio->bi_bdev, b),
1387 bio->bi_rw,
1388 (unsigned long long)bio->bi_sector + bio_sectors(bio),
Mike Snitzer77304d22010-11-08 14:39:12 +01001389 (long long)(i_size_read(bio->bi_bdev->bd_inode) >> 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 set_bit(BIO_EOF, &bio->bi_flags);
1392}
1393
Akinobu Mitac17bb492006-12-08 02:39:46 -08001394#ifdef CONFIG_FAIL_MAKE_REQUEST
1395
1396static DECLARE_FAULT_ATTR(fail_make_request);
1397
1398static int __init setup_fail_make_request(char *str)
1399{
1400 return setup_fault_attr(&fail_make_request, str);
1401}
1402__setup("fail_make_request=", setup_fail_make_request);
1403
1404static int should_fail_request(struct bio *bio)
1405{
Tejun Heoeddb2e22008-08-25 19:56:13 +09001406 struct hd_struct *part = bio->bi_bdev->bd_part;
1407
1408 if (part_to_disk(part)->part0.make_it_fail || part->make_it_fail)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001409 return should_fail(&fail_make_request, bio->bi_size);
1410
1411 return 0;
1412}
1413
1414static int __init fail_make_request_debugfs(void)
1415{
1416 return init_fault_attr_dentries(&fail_make_request,
1417 "fail_make_request");
1418}
1419
1420late_initcall(fail_make_request_debugfs);
1421
1422#else /* CONFIG_FAIL_MAKE_REQUEST */
1423
1424static inline int should_fail_request(struct bio *bio)
1425{
1426 return 0;
1427}
1428
1429#endif /* CONFIG_FAIL_MAKE_REQUEST */
1430
Jens Axboec07e2b42007-07-18 13:27:58 +02001431/*
1432 * Check whether this bio extends beyond the end of the device.
1433 */
1434static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1435{
1436 sector_t maxsector;
1437
1438 if (!nr_sectors)
1439 return 0;
1440
1441 /* Test device or partition size, when known. */
Mike Snitzer77304d22010-11-08 14:39:12 +01001442 maxsector = i_size_read(bio->bi_bdev->bd_inode) >> 9;
Jens Axboec07e2b42007-07-18 13:27:58 +02001443 if (maxsector) {
1444 sector_t sector = bio->bi_sector;
1445
1446 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1447 /*
1448 * This may well happen - the kernel calls bread()
1449 * without checking the size of the device, e.g., when
1450 * mounting a device.
1451 */
1452 handle_bad_sector(bio);
1453 return 1;
1454 }
1455 }
1456
1457 return 0;
1458}
1459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001461 * generic_make_request - hand a buffer to its device driver for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 * @bio: The bio describing the location in memory and on the device.
1463 *
1464 * generic_make_request() is used to make I/O requests of block
1465 * devices. It is passed a &struct bio, which describes the I/O that needs
1466 * to be done.
1467 *
1468 * generic_make_request() does not return any status. The
1469 * success/failure status of the request, along with notification of
1470 * completion, is delivered asynchronously through the bio->bi_end_io
1471 * function described (one day) else where.
1472 *
1473 * The caller of generic_make_request must make sure that bi_io_vec
1474 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1475 * set to describe the device address, and the
1476 * bi_end_io and optionally bi_private are set to describe how
1477 * completion notification should be signaled.
1478 *
1479 * generic_make_request and the drivers it calls may use bi_next if this
1480 * bio happens to be merged with someone else, and may change bi_dev and
1481 * bi_sector for remaps as it sees fit. So the values of these fields
1482 * should NOT be depended on after the call to generic_make_request.
1483 */
Neil Brownd89d8792007-05-01 09:53:42 +02001484static inline void __generic_make_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
Jens Axboe165125e2007-07-24 09:28:11 +02001486 struct request_queue *q;
NeilBrown5ddfe962006-10-30 22:07:21 -08001487 sector_t old_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 int ret, nr_sectors = bio_sectors(bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001489 dev_t old_dev;
Jens Axboe51fd77b2007-11-02 08:49:08 +01001490 int err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Jens Axboec07e2b42007-07-18 13:27:58 +02001494 if (bio_check_eod(bio, nr_sectors))
1495 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497 /*
1498 * Resolve the mapping until finished. (drivers are
1499 * still free to implement/resolve their own stacking
1500 * by explicitly returning 0)
1501 *
1502 * NOTE: we don't repeat the blk_size check for each new device.
1503 * Stacking drivers are expected to know what they are doing.
1504 */
NeilBrown5ddfe962006-10-30 22:07:21 -08001505 old_sector = -1;
Jens Axboe2056a782006-03-23 20:00:26 +01001506 old_dev = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 do {
1508 char b[BDEVNAME_SIZE];
1509
1510 q = bdev_get_queue(bio->bi_bdev);
Tejun Heoa7384672008-11-28 13:32:03 +09001511 if (unlikely(!q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 printk(KERN_ERR
1513 "generic_make_request: Trying to access "
1514 "nonexistent block-device %s (%Lu)\n",
1515 bdevname(bio->bi_bdev, b),
1516 (long long) bio->bi_sector);
Tejun Heoa7384672008-11-28 13:32:03 +09001517 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 }
1519
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001520 if (unlikely(!(bio->bi_rw & REQ_DISCARD) &&
Christoph Hellwig67efc922009-09-30 13:54:20 +02001521 nr_sectors > queue_max_hw_sectors(q))) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001522 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
Martin K. Petersenae03bf62009-05-22 17:17:50 -04001523 bdevname(bio->bi_bdev, b),
1524 bio_sectors(bio),
1525 queue_max_hw_sectors(q));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 goto end_io;
1527 }
1528
Nick Pigginfde6ad22005-06-23 00:08:53 -07001529 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 goto end_io;
1531
Akinobu Mitac17bb492006-12-08 02:39:46 -08001532 if (should_fail_request(bio))
1533 goto end_io;
1534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 /*
1536 * If this device has partitions, remap block n
1537 * of partition p to block n+start(p) of the disk.
1538 */
1539 blk_partition_remap(bio);
1540
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +02001541 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio))
1542 goto end_io;
1543
NeilBrown5ddfe962006-10-30 22:07:21 -08001544 if (old_sector != -1)
Mike Snitzerd07335e2010-11-16 12:52:38 +01001545 trace_block_bio_remap(q, bio, old_dev, old_sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001546
NeilBrown5ddfe962006-10-30 22:07:21 -08001547 old_sector = bio->bi_sector;
Jens Axboe2056a782006-03-23 20:00:26 +01001548 old_dev = bio->bi_bdev->bd_dev;
1549
Jens Axboec07e2b42007-07-18 13:27:58 +02001550 if (bio_check_eod(bio, nr_sectors))
1551 goto end_io;
Tejun Heoa7384672008-11-28 13:32:03 +09001552
Tejun Heo1e879012010-09-03 11:56:17 +02001553 /*
1554 * Filter flush bio's early so that make_request based
1555 * drivers without flush support don't have to worry
1556 * about them.
1557 */
1558 if ((bio->bi_rw & (REQ_FLUSH | REQ_FUA)) && !q->flush_flags) {
1559 bio->bi_rw &= ~(REQ_FLUSH | REQ_FUA);
1560 if (!nr_sectors) {
1561 err = 0;
1562 goto end_io;
1563 }
1564 }
1565
Adrian Hunter8d57a982010-08-11 14:17:49 -07001566 if ((bio->bi_rw & REQ_DISCARD) &&
1567 (!blk_queue_discard(q) ||
1568 ((bio->bi_rw & REQ_SECURE) &&
1569 !blk_queue_secdiscard(q)))) {
Jens Axboe51fd77b2007-11-02 08:49:08 +01001570 err = -EOPNOTSUPP;
1571 goto end_io;
1572 }
NeilBrown5ddfe962006-10-30 22:07:21 -08001573
Vivek Goyale43473b2010-09-15 17:06:35 -04001574 blk_throtl_bio(q, &bio);
1575
1576 /*
1577 * If bio = NULL, bio has been throttled and will be submitted
1578 * later.
1579 */
1580 if (!bio)
1581 break;
1582
Minchan Kim01edede2009-09-08 21:56:38 +02001583 trace_block_bio_queue(q, bio);
1584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 ret = q->make_request_fn(q, bio);
1586 } while (ret);
Tejun Heoa7384672008-11-28 13:32:03 +09001587
1588 return;
1589
1590end_io:
1591 bio_endio(bio, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592}
1593
Neil Brownd89d8792007-05-01 09:53:42 +02001594/*
1595 * We only want one ->make_request_fn to be active at a time,
1596 * else stack usage with stacked devices could be a problem.
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001597 * So use current->bio_list to keep a list of requests
Neil Brownd89d8792007-05-01 09:53:42 +02001598 * submited by a make_request_fn function.
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001599 * current->bio_list is also used as a flag to say if
Neil Brownd89d8792007-05-01 09:53:42 +02001600 * generic_make_request is currently active in this task or not.
1601 * If it is NULL, then no make_request is active. If it is non-NULL,
1602 * then a make_request is active, and new requests should be added
1603 * at the tail
1604 */
1605void generic_make_request(struct bio *bio)
1606{
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001607 struct bio_list bio_list_on_stack;
1608
1609 if (current->bio_list) {
Neil Brownd89d8792007-05-01 09:53:42 +02001610 /* make_request is active */
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001611 bio_list_add(current->bio_list, bio);
Neil Brownd89d8792007-05-01 09:53:42 +02001612 return;
1613 }
1614 /* following loop may be a bit non-obvious, and so deserves some
1615 * explanation.
1616 * Before entering the loop, bio->bi_next is NULL (as all callers
1617 * ensure that) so we have a list with a single bio.
1618 * We pretend that we have just taken it off a longer list, so
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001619 * we assign bio_list to a pointer to the bio_list_on_stack,
1620 * thus initialising the bio_list of new bios to be
Neil Brownd89d8792007-05-01 09:53:42 +02001621 * added. __generic_make_request may indeed add some more bios
1622 * through a recursive call to generic_make_request. If it
1623 * did, we find a non-NULL value in bio_list and re-enter the loop
1624 * from the top. In this case we really did just take the bio
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001625 * of the top of the list (no pretending) and so remove it from
1626 * bio_list, and call into __generic_make_request again.
Neil Brownd89d8792007-05-01 09:53:42 +02001627 *
1628 * The loop was structured like this to make only one call to
1629 * __generic_make_request (which is important as it is large and
1630 * inlined) and to keep the structure simple.
1631 */
1632 BUG_ON(bio->bi_next);
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001633 bio_list_init(&bio_list_on_stack);
1634 current->bio_list = &bio_list_on_stack;
Neil Brownd89d8792007-05-01 09:53:42 +02001635 do {
Neil Brownd89d8792007-05-01 09:53:42 +02001636 __generic_make_request(bio);
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001637 bio = bio_list_pop(current->bio_list);
Neil Brownd89d8792007-05-01 09:53:42 +02001638 } while (bio);
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001639 current->bio_list = NULL; /* deactivate */
Neil Brownd89d8792007-05-01 09:53:42 +02001640}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641EXPORT_SYMBOL(generic_make_request);
1642
1643/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001644 * submit_bio - submit a bio to the block device layer for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1646 * @bio: The &struct bio which describes the I/O
1647 *
1648 * submit_bio() is very similar in purpose to generic_make_request(), and
1649 * uses that function to do most of the work. Both are fairly rough
Randy Dunlap710027a2008-08-19 20:13:11 +02001650 * interfaces; @bio must be presetup and ready for I/O.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 *
1652 */
1653void submit_bio(int rw, struct bio *bio)
1654{
1655 int count = bio_sectors(bio);
1656
Jens Axboe22e2c502005-06-27 10:55:12 +02001657 bio->bi_rw |= rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Jens Axboebf2de6f2007-09-27 13:01:25 +02001659 /*
1660 * If it's a regular read/write or a barrier with data attached,
1661 * go through the normal accounting stuff before submission.
1662 */
Jens Axboe3ffb52e2010-06-29 13:33:38 +02001663 if (bio_has_data(bio) && !(rw & REQ_DISCARD)) {
Jens Axboebf2de6f2007-09-27 13:01:25 +02001664 if (rw & WRITE) {
1665 count_vm_events(PGPGOUT, count);
1666 } else {
1667 task_io_account_read(bio->bi_size);
1668 count_vm_events(PGPGIN, count);
1669 }
1670
1671 if (unlikely(block_dump)) {
1672 char b[BDEVNAME_SIZE];
San Mehat8dcbdc72010-09-14 08:48:01 +02001673 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s (%u sectors)\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001674 current->comm, task_pid_nr(current),
Jens Axboebf2de6f2007-09-27 13:01:25 +02001675 (rw & WRITE) ? "WRITE" : "READ",
1676 (unsigned long long)bio->bi_sector,
San Mehat8dcbdc72010-09-14 08:48:01 +02001677 bdevname(bio->bi_bdev, b),
1678 count);
Jens Axboebf2de6f2007-09-27 13:01:25 +02001679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 }
1681
1682 generic_make_request(bio);
1683}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684EXPORT_SYMBOL(submit_bio);
1685
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001686/**
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001687 * blk_rq_check_limits - Helper function to check a request for the queue limit
1688 * @q: the queue
1689 * @rq: the request being checked
1690 *
1691 * Description:
1692 * @rq may have been made based on weaker limitations of upper-level queues
1693 * in request stacking drivers, and it may violate the limitation of @q.
1694 * Since the block layer and the underlying device driver trust @rq
1695 * after it is inserted to @q, it should be checked against @q before
1696 * the insertion using this generic function.
1697 *
1698 * This function should also be useful for request stacking drivers
Stefan Weileef35c22010-08-06 21:11:15 +02001699 * in some cases below, so export this function.
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001700 * Request stacking drivers like request-based dm may change the queue
1701 * limits while requests are in the queue (e.g. dm's table swapping).
1702 * Such request stacking drivers should check those requests agaist
1703 * the new queue limits again when they dispatch those requests,
1704 * although such checkings are also done against the old queue limits
1705 * when submitting requests.
1706 */
1707int blk_rq_check_limits(struct request_queue *q, struct request *rq)
1708{
ike Snitzer33839772010-08-08 12:11:33 -04001709 if (rq->cmd_flags & REQ_DISCARD)
1710 return 0;
1711
Martin K. Petersenae03bf62009-05-22 17:17:50 -04001712 if (blk_rq_sectors(rq) > queue_max_sectors(q) ||
1713 blk_rq_bytes(rq) > queue_max_hw_sectors(q) << 9) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001714 printk(KERN_ERR "%s: over max size limit.\n", __func__);
1715 return -EIO;
1716 }
1717
1718 /*
1719 * queue's settings related to segment counting like q->bounce_pfn
1720 * may differ from that of other stacking queues.
1721 * Recalculate it to check the request correctly on this queue's
1722 * limitation.
1723 */
1724 blk_recalc_rq_segments(rq);
Martin K. Petersen8a783622010-02-26 00:20:39 -05001725 if (rq->nr_phys_segments > queue_max_segments(q)) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001726 printk(KERN_ERR "%s: over max segments limit.\n", __func__);
1727 return -EIO;
1728 }
1729
1730 return 0;
1731}
1732EXPORT_SYMBOL_GPL(blk_rq_check_limits);
1733
1734/**
1735 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
1736 * @q: the queue to submit the request
1737 * @rq: the request being queued
1738 */
1739int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
1740{
1741 unsigned long flags;
1742
1743 if (blk_rq_check_limits(q, rq))
1744 return -EIO;
1745
1746#ifdef CONFIG_FAIL_MAKE_REQUEST
1747 if (rq->rq_disk && rq->rq_disk->part0.make_it_fail &&
1748 should_fail(&fail_make_request, blk_rq_bytes(rq)))
1749 return -EIO;
1750#endif
1751
1752 spin_lock_irqsave(q->queue_lock, flags);
1753
1754 /*
1755 * Submitting request must be dequeued before calling this function
1756 * because it will be linked to another request_queue
1757 */
1758 BUG_ON(blk_queued_rq(rq));
1759
1760 drive_stat_acct(rq, 1);
1761 __elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 0);
1762
1763 spin_unlock_irqrestore(q->queue_lock, flags);
1764
1765 return 0;
1766}
1767EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
1768
Tejun Heo80a761f2009-07-03 17:48:17 +09001769/**
1770 * blk_rq_err_bytes - determine number of bytes till the next failure boundary
1771 * @rq: request to examine
1772 *
1773 * Description:
1774 * A request could be merge of IOs which require different failure
1775 * handling. This function determines the number of bytes which
1776 * can be failed from the beginning of the request without
1777 * crossing into area which need to be retried further.
1778 *
1779 * Return:
1780 * The number of bytes to fail.
1781 *
1782 * Context:
1783 * queue_lock must be held.
1784 */
1785unsigned int blk_rq_err_bytes(const struct request *rq)
1786{
1787 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
1788 unsigned int bytes = 0;
1789 struct bio *bio;
1790
1791 if (!(rq->cmd_flags & REQ_MIXED_MERGE))
1792 return blk_rq_bytes(rq);
1793
1794 /*
1795 * Currently the only 'mixing' which can happen is between
1796 * different fastfail types. We can safely fail portions
1797 * which have all the failfast bits that the first one has -
1798 * the ones which are at least as eager to fail as the first
1799 * one.
1800 */
1801 for (bio = rq->bio; bio; bio = bio->bi_next) {
1802 if ((bio->bi_rw & ff) != ff)
1803 break;
1804 bytes += bio->bi_size;
1805 }
1806
1807 /* this could lead to infinite loop */
1808 BUG_ON(blk_rq_bytes(rq) && !bytes);
1809 return bytes;
1810}
1811EXPORT_SYMBOL_GPL(blk_rq_err_bytes);
1812
Jens Axboebc58ba92009-01-23 10:54:44 +01001813static void blk_account_io_completion(struct request *req, unsigned int bytes)
1814{
Jens Axboec2553b52009-04-24 08:10:11 +02001815 if (blk_do_io_stat(req)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01001816 const int rw = rq_data_dir(req);
1817 struct hd_struct *part;
1818 int cpu;
1819
1820 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +01001821 part = req->part;
Jens Axboebc58ba92009-01-23 10:54:44 +01001822 part_stat_add(cpu, part, sectors[rw], bytes >> 9);
1823 part_stat_unlock();
1824 }
1825}
1826
1827static void blk_account_io_done(struct request *req)
1828{
Jens Axboebc58ba92009-01-23 10:54:44 +01001829 /*
Tejun Heodd4c1332010-09-03 11:56:16 +02001830 * Account IO completion. flush_rq isn't accounted as a
1831 * normal IO on queueing nor completion. Accounting the
1832 * containing request is enough.
Jens Axboebc58ba92009-01-23 10:54:44 +01001833 */
Tejun Heo414b4ff2011-01-25 12:43:49 +01001834 if (blk_do_io_stat(req) && !(req->cmd_flags & REQ_FLUSH_SEQ)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01001835 unsigned long duration = jiffies - req->start_time;
1836 const int rw = rq_data_dir(req);
1837 struct hd_struct *part;
1838 int cpu;
1839
1840 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +01001841 part = req->part;
Jens Axboebc58ba92009-01-23 10:54:44 +01001842
1843 part_stat_inc(cpu, part, ios[rw]);
1844 part_stat_add(cpu, part, ticks[rw], duration);
1845 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001846 part_dec_in_flight(part, rw);
Jens Axboebc58ba92009-01-23 10:54:44 +01001847
Jens Axboe6c23a962011-01-07 08:43:37 +01001848 hd_struct_put(part);
Jens Axboebc58ba92009-01-23 10:54:44 +01001849 part_stat_unlock();
1850 }
1851}
1852
Tejun Heo53a08802008-12-03 12:41:26 +01001853/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09001854 * blk_peek_request - peek at the top of a request queue
1855 * @q: request queue to peek at
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001856 *
1857 * Description:
Tejun Heo9934c8c2009-05-08 11:54:16 +09001858 * Return the request at the top of @q. The returned request
1859 * should be started using blk_start_request() before LLD starts
1860 * processing it.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001861 *
1862 * Return:
Tejun Heo9934c8c2009-05-08 11:54:16 +09001863 * Pointer to the request at the top of @q if available. Null
1864 * otherwise.
1865 *
1866 * Context:
1867 * queue_lock must be held.
1868 */
1869struct request *blk_peek_request(struct request_queue *q)
Tejun Heo158dbda2009-04-23 11:05:18 +09001870{
1871 struct request *rq;
1872 int ret;
1873
1874 while ((rq = __elv_next_request(q)) != NULL) {
1875 if (!(rq->cmd_flags & REQ_STARTED)) {
1876 /*
1877 * This is the first time the device driver
1878 * sees this request (possibly after
1879 * requeueing). Notify IO scheduler.
1880 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001881 if (rq->cmd_flags & REQ_SORTED)
Tejun Heo158dbda2009-04-23 11:05:18 +09001882 elv_activate_rq(q, rq);
1883
1884 /*
1885 * just mark as started even if we don't start
1886 * it, a request that has been delayed should
1887 * not be passed by new incoming requests
1888 */
1889 rq->cmd_flags |= REQ_STARTED;
1890 trace_block_rq_issue(q, rq);
1891 }
1892
1893 if (!q->boundary_rq || q->boundary_rq == rq) {
1894 q->end_sector = rq_end_sector(rq);
1895 q->boundary_rq = NULL;
1896 }
1897
1898 if (rq->cmd_flags & REQ_DONTPREP)
1899 break;
1900
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001901 if (q->dma_drain_size && blk_rq_bytes(rq)) {
Tejun Heo158dbda2009-04-23 11:05:18 +09001902 /*
1903 * make sure space for the drain appears we
1904 * know we can do this because max_hw_segments
1905 * has been adjusted to be one fewer than the
1906 * device can handle
1907 */
1908 rq->nr_phys_segments++;
1909 }
1910
1911 if (!q->prep_rq_fn)
1912 break;
1913
1914 ret = q->prep_rq_fn(q, rq);
1915 if (ret == BLKPREP_OK) {
1916 break;
1917 } else if (ret == BLKPREP_DEFER) {
1918 /*
1919 * the request may have been (partially) prepped.
1920 * we need to keep this request in the front to
1921 * avoid resource deadlock. REQ_STARTED will
1922 * prevent other fs requests from passing this one.
1923 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001924 if (q->dma_drain_size && blk_rq_bytes(rq) &&
Tejun Heo158dbda2009-04-23 11:05:18 +09001925 !(rq->cmd_flags & REQ_DONTPREP)) {
1926 /*
1927 * remove the space for the drain we added
1928 * so that we don't add it again
1929 */
1930 --rq->nr_phys_segments;
1931 }
1932
1933 rq = NULL;
1934 break;
1935 } else if (ret == BLKPREP_KILL) {
1936 rq->cmd_flags |= REQ_QUIET;
James Bottomleyc143dc92009-05-30 06:43:49 +02001937 /*
1938 * Mark this request as started so we don't trigger
1939 * any debug logic in the end I/O path.
1940 */
1941 blk_start_request(rq);
Tejun Heo40cbbb72009-04-23 11:05:19 +09001942 __blk_end_request_all(rq, -EIO);
Tejun Heo158dbda2009-04-23 11:05:18 +09001943 } else {
1944 printk(KERN_ERR "%s: bad return=%d\n", __func__, ret);
1945 break;
1946 }
1947 }
1948
1949 return rq;
1950}
Tejun Heo9934c8c2009-05-08 11:54:16 +09001951EXPORT_SYMBOL(blk_peek_request);
Tejun Heo158dbda2009-04-23 11:05:18 +09001952
Tejun Heo9934c8c2009-05-08 11:54:16 +09001953void blk_dequeue_request(struct request *rq)
Tejun Heo158dbda2009-04-23 11:05:18 +09001954{
Tejun Heo9934c8c2009-05-08 11:54:16 +09001955 struct request_queue *q = rq->q;
1956
Tejun Heo158dbda2009-04-23 11:05:18 +09001957 BUG_ON(list_empty(&rq->queuelist));
1958 BUG_ON(ELV_ON_HASH(rq));
1959
1960 list_del_init(&rq->queuelist);
1961
1962 /*
1963 * the time frame between a request being removed from the lists
1964 * and to it is freed is accounted as io that is in progress at
1965 * the driver side.
1966 */
Divyesh Shah91952912010-04-01 15:01:41 -07001967 if (blk_account_rq(rq)) {
Jens Axboe0a7ae2f2009-05-20 08:54:31 +02001968 q->in_flight[rq_is_sync(rq)]++;
Divyesh Shah91952912010-04-01 15:01:41 -07001969 set_io_start_time_ns(rq);
1970 }
Tejun Heo158dbda2009-04-23 11:05:18 +09001971}
1972
Tejun Heo5efccd12009-04-23 11:05:18 +09001973/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09001974 * blk_start_request - start request processing on the driver
1975 * @req: request to dequeue
1976 *
1977 * Description:
1978 * Dequeue @req and start timeout timer on it. This hands off the
1979 * request to the driver.
1980 *
1981 * Block internal functions which don't want to start timer should
1982 * call blk_dequeue_request().
1983 *
1984 * Context:
1985 * queue_lock must be held.
1986 */
1987void blk_start_request(struct request *req)
1988{
1989 blk_dequeue_request(req);
1990
1991 /*
Tejun Heo5f49f632009-05-19 18:33:05 +09001992 * We are now handing the request to the hardware, initialize
1993 * resid_len to full count and add the timeout handler.
Tejun Heo9934c8c2009-05-08 11:54:16 +09001994 */
Tejun Heo5f49f632009-05-19 18:33:05 +09001995 req->resid_len = blk_rq_bytes(req);
FUJITA Tomonoridbb66c42009-06-09 05:47:10 +02001996 if (unlikely(blk_bidi_rq(req)))
1997 req->next_rq->resid_len = blk_rq_bytes(req->next_rq);
1998
Tejun Heo9934c8c2009-05-08 11:54:16 +09001999 blk_add_timer(req);
2000}
2001EXPORT_SYMBOL(blk_start_request);
2002
2003/**
2004 * blk_fetch_request - fetch a request from a request queue
2005 * @q: request queue to fetch a request from
2006 *
2007 * Description:
2008 * Return the request at the top of @q. The request is started on
2009 * return and LLD can start processing it immediately.
2010 *
2011 * Return:
2012 * Pointer to the request at the top of @q if available. Null
2013 * otherwise.
2014 *
2015 * Context:
2016 * queue_lock must be held.
2017 */
2018struct request *blk_fetch_request(struct request_queue *q)
2019{
2020 struct request *rq;
2021
2022 rq = blk_peek_request(q);
2023 if (rq)
2024 blk_start_request(rq);
2025 return rq;
2026}
2027EXPORT_SYMBOL(blk_fetch_request);
2028
2029/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002030 * blk_update_request - Special helper function for request stacking drivers
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002031 * @req: the request being processed
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002032 * @error: %0 for success, < %0 for error
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002033 * @nr_bytes: number of bytes to complete @req
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002034 *
2035 * Description:
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002036 * Ends I/O on a number of bytes attached to @req, but doesn't complete
2037 * the request structure even if @req doesn't have leftover.
2038 * If @req has leftover, sets it up for the next range of segments.
Tejun Heo2e60e022009-04-23 11:05:18 +09002039 *
2040 * This special helper function is only for request stacking drivers
2041 * (e.g. request-based dm) so that they can handle partial completion.
2042 * Actual device drivers should use blk_end_request instead.
2043 *
2044 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
2045 * %false return from this function.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002046 *
2047 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002048 * %false - this request doesn't have any more data
2049 * %true - this request has more data
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002050 **/
Tejun Heo2e60e022009-04-23 11:05:18 +09002051bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052{
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05002053 int total_bytes, bio_nbytes, next_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 struct bio *bio;
2055
Tejun Heo2e60e022009-04-23 11:05:18 +09002056 if (!req->bio)
2057 return false;
2058
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01002059 trace_block_rq_complete(req->q, req);
Jens Axboe2056a782006-03-23 20:00:26 +01002060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 /*
Tejun Heo6f414692009-04-19 07:00:41 +09002062 * For fs requests, rq is just carrier of independent bio's
2063 * and each partial completion should be handled separately.
2064 * Reset per-request error on each partial completion.
2065 *
2066 * TODO: tj: This is too subtle. It would be better to let
2067 * low level drivers do what they see fit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002069 if (req->cmd_type == REQ_TYPE_FS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 req->errors = 0;
2071
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002072 if (error && req->cmd_type == REQ_TYPE_FS &&
2073 !(req->cmd_flags & REQ_QUIET)) {
Jens Axboe6728cb02008-01-31 13:03:55 +01002074 printk(KERN_ERR "end_request: I/O error, dev %s, sector %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 req->rq_disk ? req->rq_disk->disk_name : "?",
Tejun Heo83096eb2009-05-07 22:24:39 +09002076 (unsigned long long)blk_rq_pos(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 }
2078
Jens Axboebc58ba92009-01-23 10:54:44 +01002079 blk_account_io_completion(req, nr_bytes);
Jens Axboed72d9042005-11-01 08:35:42 +01002080
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 total_bytes = bio_nbytes = 0;
2082 while ((bio = req->bio) != NULL) {
2083 int nbytes;
2084
2085 if (nr_bytes >= bio->bi_size) {
2086 req->bio = bio->bi_next;
2087 nbytes = bio->bi_size;
NeilBrown5bb23a62007-09-27 12:46:13 +02002088 req_bio_endio(req, bio, nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 next_idx = 0;
2090 bio_nbytes = 0;
2091 } else {
2092 int idx = bio->bi_idx + next_idx;
2093
Kazuhisa Ichikawaaf498d72009-05-12 13:27:45 +02002094 if (unlikely(idx >= bio->bi_vcnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 blk_dump_rq_flags(req, "__end_that");
Jens Axboe6728cb02008-01-31 13:03:55 +01002096 printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
Kazuhisa Ichikawaaf498d72009-05-12 13:27:45 +02002097 __func__, idx, bio->bi_vcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 break;
2099 }
2100
2101 nbytes = bio_iovec_idx(bio, idx)->bv_len;
2102 BIO_BUG_ON(nbytes > bio->bi_size);
2103
2104 /*
2105 * not a complete bvec done
2106 */
2107 if (unlikely(nbytes > nr_bytes)) {
2108 bio_nbytes += nr_bytes;
2109 total_bytes += nr_bytes;
2110 break;
2111 }
2112
2113 /*
2114 * advance to the next vector
2115 */
2116 next_idx++;
2117 bio_nbytes += nbytes;
2118 }
2119
2120 total_bytes += nbytes;
2121 nr_bytes -= nbytes;
2122
Jens Axboe6728cb02008-01-31 13:03:55 +01002123 bio = req->bio;
2124 if (bio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 /*
2126 * end more in this run, or just return 'not-done'
2127 */
2128 if (unlikely(nr_bytes <= 0))
2129 break;
2130 }
2131 }
2132
2133 /*
2134 * completely done
2135 */
Tejun Heo2e60e022009-04-23 11:05:18 +09002136 if (!req->bio) {
2137 /*
2138 * Reset counters so that the request stacking driver
2139 * can find how many bytes remain in the request
2140 * later.
2141 */
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002142 req->__data_len = 0;
Tejun Heo2e60e022009-04-23 11:05:18 +09002143 return false;
2144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
2146 /*
2147 * if the request wasn't completed, update state
2148 */
2149 if (bio_nbytes) {
NeilBrown5bb23a62007-09-27 12:46:13 +02002150 req_bio_endio(req, bio, bio_nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 bio->bi_idx += next_idx;
2152 bio_iovec(bio)->bv_offset += nr_bytes;
2153 bio_iovec(bio)->bv_len -= nr_bytes;
2154 }
2155
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002156 req->__data_len -= total_bytes;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002157 req->buffer = bio_data(req->bio);
2158
2159 /* update sector only for requests with clear definition of sector */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002160 if (req->cmd_type == REQ_TYPE_FS || (req->cmd_flags & REQ_DISCARD))
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002161 req->__sector += total_bytes >> 9;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002162
Tejun Heo80a761f2009-07-03 17:48:17 +09002163 /* mixed attributes always follow the first bio */
2164 if (req->cmd_flags & REQ_MIXED_MERGE) {
2165 req->cmd_flags &= ~REQ_FAILFAST_MASK;
2166 req->cmd_flags |= req->bio->bi_rw & REQ_FAILFAST_MASK;
2167 }
2168
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002169 /*
2170 * If total number of sectors is less than the first segment
2171 * size, something has gone terribly wrong.
2172 */
2173 if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
2174 printk(KERN_ERR "blk: request botched\n");
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002175 req->__data_len = blk_rq_cur_bytes(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002176 }
2177
2178 /* recalculate the number of segments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 blk_recalc_rq_segments(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002180
Tejun Heo2e60e022009-04-23 11:05:18 +09002181 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182}
Tejun Heo2e60e022009-04-23 11:05:18 +09002183EXPORT_SYMBOL_GPL(blk_update_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Tejun Heo2e60e022009-04-23 11:05:18 +09002185static bool blk_update_bidi_request(struct request *rq, int error,
2186 unsigned int nr_bytes,
2187 unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002188{
Tejun Heo2e60e022009-04-23 11:05:18 +09002189 if (blk_update_request(rq, error, nr_bytes))
2190 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002191
Tejun Heo2e60e022009-04-23 11:05:18 +09002192 /* Bidi request must be completed as a whole */
2193 if (unlikely(blk_bidi_rq(rq)) &&
2194 blk_update_request(rq->next_rq, error, bidi_bytes))
2195 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002196
Jens Axboee2e1a142010-06-09 10:42:09 +02002197 if (blk_queue_add_random(rq->q))
2198 add_disk_randomness(rq->rq_disk);
Tejun Heo2e60e022009-04-23 11:05:18 +09002199
2200 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201}
2202
James Bottomley28018c22010-07-01 19:49:17 +09002203/**
2204 * blk_unprep_request - unprepare a request
2205 * @req: the request
2206 *
2207 * This function makes a request ready for complete resubmission (or
2208 * completion). It happens only after all error handling is complete,
2209 * so represents the appropriate moment to deallocate any resources
2210 * that were allocated to the request in the prep_rq_fn. The queue
2211 * lock is held when calling this.
2212 */
2213void blk_unprep_request(struct request *req)
2214{
2215 struct request_queue *q = req->q;
2216
2217 req->cmd_flags &= ~REQ_DONTPREP;
2218 if (q->unprep_rq_fn)
2219 q->unprep_rq_fn(q, req);
2220}
2221EXPORT_SYMBOL_GPL(blk_unprep_request);
2222
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223/*
2224 * queue lock must be held
2225 */
Tejun Heo2e60e022009-04-23 11:05:18 +09002226static void blk_finish_request(struct request *req, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227{
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002228 if (blk_rq_tagged(req))
2229 blk_queue_end_tag(req->q, req);
2230
James Bottomleyba396a62009-05-27 14:17:08 +02002231 BUG_ON(blk_queued_rq(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002233 if (unlikely(laptop_mode) && req->cmd_type == REQ_TYPE_FS)
Matthew Garrett31373d02010-04-06 14:25:14 +02002234 laptop_io_completion(&req->q->backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
Mike Andersone78042e52008-10-30 02:16:20 -07002236 blk_delete_timer(req);
2237
James Bottomley28018c22010-07-01 19:49:17 +09002238 if (req->cmd_flags & REQ_DONTPREP)
2239 blk_unprep_request(req);
2240
2241
Jens Axboebc58ba92009-01-23 10:54:44 +01002242 blk_account_io_done(req);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002243
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 if (req->end_io)
Tejun Heo8ffdc652006-01-06 09:49:03 +01002245 req->end_io(req, error);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002246 else {
2247 if (blk_bidi_rq(req))
2248 __blk_put_request(req->next_rq->q, req->next_rq);
2249
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 __blk_put_request(req->q, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 }
2252}
2253
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05002254/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002255 * blk_end_bidi_request - Complete a bidi request
2256 * @rq: the request to complete
Randy Dunlap710027a2008-08-19 20:13:11 +02002257 * @error: %0 for success, < %0 for error
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002258 * @nr_bytes: number of bytes to complete @rq
2259 * @bidi_bytes: number of bytes to complete @rq->next_rq
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002260 *
2261 * Description:
2262 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
Tejun Heo2e60e022009-04-23 11:05:18 +09002263 * Drivers that supports bidi can safely call this member for any
2264 * type of request, bidi or uni. In the later case @bidi_bytes is
2265 * just ignored.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002266 *
2267 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002268 * %false - we are done with this request
2269 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002270 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002271static bool blk_end_bidi_request(struct request *rq, int error,
2272 unsigned int nr_bytes, unsigned int bidi_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002273{
2274 struct request_queue *q = rq->q;
Tejun Heo2e60e022009-04-23 11:05:18 +09002275 unsigned long flags;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002276
Tejun Heo2e60e022009-04-23 11:05:18 +09002277 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2278 return true;
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002279
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002280 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo2e60e022009-04-23 11:05:18 +09002281 blk_finish_request(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002282 spin_unlock_irqrestore(q->queue_lock, flags);
2283
Tejun Heo2e60e022009-04-23 11:05:18 +09002284 return false;
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002285}
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002286
2287/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002288 * __blk_end_bidi_request - Complete a bidi request with queue lock held
2289 * @rq: the request to complete
2290 * @error: %0 for success, < %0 for error
2291 * @nr_bytes: number of bytes to complete @rq
2292 * @bidi_bytes: number of bytes to complete @rq->next_rq
Tejun Heo5efccd12009-04-23 11:05:18 +09002293 *
2294 * Description:
Tejun Heo2e60e022009-04-23 11:05:18 +09002295 * Identical to blk_end_bidi_request() except that queue lock is
2296 * assumed to be locked on entry and remains so on return.
Tejun Heo5efccd12009-04-23 11:05:18 +09002297 *
Tejun Heo2e60e022009-04-23 11:05:18 +09002298 * Return:
2299 * %false - we are done with this request
2300 * %true - still buffers pending for this request
Tejun Heo5efccd12009-04-23 11:05:18 +09002301 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002302static bool __blk_end_bidi_request(struct request *rq, int error,
2303 unsigned int nr_bytes, unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002304{
Tejun Heo2e60e022009-04-23 11:05:18 +09002305 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2306 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002307
Tejun Heo2e60e022009-04-23 11:05:18 +09002308 blk_finish_request(rq, error);
Tejun Heo5efccd12009-04-23 11:05:18 +09002309
Tejun Heo2e60e022009-04-23 11:05:18 +09002310 return false;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002311}
2312
2313/**
2314 * blk_end_request - Helper function for drivers to complete the request.
2315 * @rq: the request being processed
2316 * @error: %0 for success, < %0 for error
2317 * @nr_bytes: number of bytes to complete
2318 *
2319 * Description:
2320 * Ends I/O on a number of bytes attached to @rq.
2321 * If @rq has leftover, sets it up for the next range of segments.
2322 *
2323 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002324 * %false - we are done with this request
2325 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002326 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002327bool blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002328{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002329 return blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002330}
Jens Axboe56ad1742009-07-28 22:11:24 +02002331EXPORT_SYMBOL(blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002332
2333/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002334 * blk_end_request_all - Helper function for drives to finish the request.
2335 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002336 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002337 *
2338 * Description:
2339 * Completely finish @rq.
2340 */
2341void blk_end_request_all(struct request *rq, int error)
2342{
2343 bool pending;
2344 unsigned int bidi_bytes = 0;
2345
2346 if (unlikely(blk_bidi_rq(rq)))
2347 bidi_bytes = blk_rq_bytes(rq->next_rq);
2348
2349 pending = blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2350 BUG_ON(pending);
2351}
Jens Axboe56ad1742009-07-28 22:11:24 +02002352EXPORT_SYMBOL(blk_end_request_all);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002353
2354/**
2355 * blk_end_request_cur - Helper function to finish the current request chunk.
2356 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002357 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002358 *
2359 * Description:
2360 * Complete the current consecutively mapped chunk from @rq.
2361 *
2362 * Return:
2363 * %false - we are done with this request
2364 * %true - still buffers pending for this request
2365 */
2366bool blk_end_request_cur(struct request *rq, int error)
2367{
2368 return blk_end_request(rq, error, blk_rq_cur_bytes(rq));
2369}
Jens Axboe56ad1742009-07-28 22:11:24 +02002370EXPORT_SYMBOL(blk_end_request_cur);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002371
2372/**
Tejun Heo80a761f2009-07-03 17:48:17 +09002373 * blk_end_request_err - Finish a request till the next failure boundary.
2374 * @rq: the request to finish till the next failure boundary for
2375 * @error: must be negative errno
2376 *
2377 * Description:
2378 * Complete @rq till the next failure boundary.
2379 *
2380 * Return:
2381 * %false - we are done with this request
2382 * %true - still buffers pending for this request
2383 */
2384bool blk_end_request_err(struct request *rq, int error)
2385{
2386 WARN_ON(error >= 0);
2387 return blk_end_request(rq, error, blk_rq_err_bytes(rq));
2388}
2389EXPORT_SYMBOL_GPL(blk_end_request_err);
2390
2391/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002392 * __blk_end_request - Helper function for drivers to complete the request.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002393 * @rq: the request being processed
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002394 * @error: %0 for success, < %0 for error
2395 * @nr_bytes: number of bytes to complete
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002396 *
2397 * Description:
2398 * Must be called with queue lock held unlike blk_end_request().
2399 *
2400 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002401 * %false - we are done with this request
2402 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002403 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002404bool __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002405{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002406 return __blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002407}
Jens Axboe56ad1742009-07-28 22:11:24 +02002408EXPORT_SYMBOL(__blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002409
2410/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002411 * __blk_end_request_all - Helper function for drives to finish the request.
2412 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002413 * @error: %0 for success, < %0 for error
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002414 *
2415 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002416 * Completely finish @rq. Must be called with queue lock held.
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002417 */
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002418void __blk_end_request_all(struct request *rq, int error)
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002419{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002420 bool pending;
2421 unsigned int bidi_bytes = 0;
2422
2423 if (unlikely(blk_bidi_rq(rq)))
2424 bidi_bytes = blk_rq_bytes(rq->next_rq);
2425
2426 pending = __blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2427 BUG_ON(pending);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002428}
Jens Axboe56ad1742009-07-28 22:11:24 +02002429EXPORT_SYMBOL(__blk_end_request_all);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002430
2431/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002432 * __blk_end_request_cur - Helper function to finish the current request chunk.
2433 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002434 * @error: %0 for success, < %0 for error
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002435 *
2436 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002437 * Complete the current consecutively mapped chunk from @rq. Must
2438 * be called with queue lock held.
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002439 *
2440 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002441 * %false - we are done with this request
2442 * %true - still buffers pending for this request
2443 */
2444bool __blk_end_request_cur(struct request *rq, int error)
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002445{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002446 return __blk_end_request(rq, error, blk_rq_cur_bytes(rq));
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002447}
Jens Axboe56ad1742009-07-28 22:11:24 +02002448EXPORT_SYMBOL(__blk_end_request_cur);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002449
Tejun Heo80a761f2009-07-03 17:48:17 +09002450/**
2451 * __blk_end_request_err - Finish a request till the next failure boundary.
2452 * @rq: the request to finish till the next failure boundary for
2453 * @error: must be negative errno
2454 *
2455 * Description:
2456 * Complete @rq till the next failure boundary. Must be called
2457 * with queue lock held.
2458 *
2459 * Return:
2460 * %false - we are done with this request
2461 * %true - still buffers pending for this request
2462 */
2463bool __blk_end_request_err(struct request *rq, int error)
2464{
2465 WARN_ON(error >= 0);
2466 return __blk_end_request(rq, error, blk_rq_err_bytes(rq));
2467}
2468EXPORT_SYMBOL_GPL(__blk_end_request_err);
2469
Jens Axboe86db1e22008-01-29 14:53:40 +01002470void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2471 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472{
Tejun Heoa82afdf2009-07-03 17:48:16 +09002473 /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002474 rq->cmd_flags |= bio->bi_rw & REQ_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
David Woodhousefb2dce82008-08-05 18:01:53 +01002476 if (bio_has_data(bio)) {
2477 rq->nr_phys_segments = bio_phys_segments(q, bio);
David Woodhousefb2dce82008-08-05 18:01:53 +01002478 rq->buffer = bio_data(bio);
2479 }
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002480 rq->__data_len = bio->bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 rq->bio = rq->biotail = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
NeilBrown66846572007-08-16 13:31:28 +02002483 if (bio->bi_bdev)
2484 rq->rq_disk = bio->bi_bdev->bd_disk;
2485}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
Ilya Loginov2d4dc892009-11-26 09:16:19 +01002487#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
2488/**
2489 * rq_flush_dcache_pages - Helper function to flush all pages in a request
2490 * @rq: the request to be flushed
2491 *
2492 * Description:
2493 * Flush all pages in @rq.
2494 */
2495void rq_flush_dcache_pages(struct request *rq)
2496{
2497 struct req_iterator iter;
2498 struct bio_vec *bvec;
2499
2500 rq_for_each_segment(bvec, rq, iter)
2501 flush_dcache_page(bvec->bv_page);
2502}
2503EXPORT_SYMBOL_GPL(rq_flush_dcache_pages);
2504#endif
2505
Kiyoshi Uedaef9e3fa2008-10-01 16:12:15 +02002506/**
2507 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
2508 * @q : the queue of the device being checked
2509 *
2510 * Description:
2511 * Check if underlying low-level drivers of a device are busy.
2512 * If the drivers want to export their busy state, they must set own
2513 * exporting function using blk_queue_lld_busy() first.
2514 *
2515 * Basically, this function is used only by request stacking drivers
2516 * to stop dispatching requests to underlying devices when underlying
2517 * devices are busy. This behavior helps more I/O merging on the queue
2518 * of the request stacking driver and prevents I/O throughput regression
2519 * on burst I/O load.
2520 *
2521 * Return:
2522 * 0 - Not busy (The request stacking driver should dispatch request)
2523 * 1 - Busy (The request stacking driver should stop dispatching request)
2524 */
2525int blk_lld_busy(struct request_queue *q)
2526{
2527 if (q->lld_busy_fn)
2528 return q->lld_busy_fn(q);
2529
2530 return 0;
2531}
2532EXPORT_SYMBOL_GPL(blk_lld_busy);
2533
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002534/**
2535 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
2536 * @rq: the clone request to be cleaned up
2537 *
2538 * Description:
2539 * Free all bios in @rq for a cloned request.
2540 */
2541void blk_rq_unprep_clone(struct request *rq)
2542{
2543 struct bio *bio;
2544
2545 while ((bio = rq->bio) != NULL) {
2546 rq->bio = bio->bi_next;
2547
2548 bio_put(bio);
2549 }
2550}
2551EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
2552
2553/*
2554 * Copy attributes of the original request to the clone request.
2555 * The actual data parts (e.g. ->cmd, ->buffer, ->sense) are not copied.
2556 */
2557static void __blk_rq_prep_clone(struct request *dst, struct request *src)
2558{
2559 dst->cpu = src->cpu;
Tejun Heo3a2edd02010-09-03 11:56:18 +02002560 dst->cmd_flags = (src->cmd_flags & REQ_CLONE_MASK) | REQ_NOMERGE;
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002561 dst->cmd_type = src->cmd_type;
2562 dst->__sector = blk_rq_pos(src);
2563 dst->__data_len = blk_rq_bytes(src);
2564 dst->nr_phys_segments = src->nr_phys_segments;
2565 dst->ioprio = src->ioprio;
2566 dst->extra_len = src->extra_len;
2567}
2568
2569/**
2570 * blk_rq_prep_clone - Helper function to setup clone request
2571 * @rq: the request to be setup
2572 * @rq_src: original request to be cloned
2573 * @bs: bio_set that bios for clone are allocated from
2574 * @gfp_mask: memory allocation mask for bio
2575 * @bio_ctr: setup function to be called for each clone bio.
2576 * Returns %0 for success, non %0 for failure.
2577 * @data: private data to be passed to @bio_ctr
2578 *
2579 * Description:
2580 * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
2581 * The actual data parts of @rq_src (e.g. ->cmd, ->buffer, ->sense)
2582 * are not copied, and copying such parts is the caller's responsibility.
2583 * Also, pages which the original bios are pointing to are not copied
2584 * and the cloned bios just point same pages.
2585 * So cloned bios must be completed before original bios, which means
2586 * the caller must complete @rq before @rq_src.
2587 */
2588int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
2589 struct bio_set *bs, gfp_t gfp_mask,
2590 int (*bio_ctr)(struct bio *, struct bio *, void *),
2591 void *data)
2592{
2593 struct bio *bio, *bio_src;
2594
2595 if (!bs)
2596 bs = fs_bio_set;
2597
2598 blk_rq_init(NULL, rq);
2599
2600 __rq_for_each_bio(bio_src, rq_src) {
2601 bio = bio_alloc_bioset(gfp_mask, bio_src->bi_max_vecs, bs);
2602 if (!bio)
2603 goto free_and_out;
2604
2605 __bio_clone(bio, bio_src);
2606
2607 if (bio_integrity(bio_src) &&
Martin K. Petersen7878cba2009-06-26 15:37:49 +02002608 bio_integrity_clone(bio, bio_src, gfp_mask, bs))
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002609 goto free_and_out;
2610
2611 if (bio_ctr && bio_ctr(bio, bio_src, data))
2612 goto free_and_out;
2613
2614 if (rq->bio) {
2615 rq->biotail->bi_next = bio;
2616 rq->biotail = bio;
2617 } else
2618 rq->bio = rq->biotail = bio;
2619 }
2620
2621 __blk_rq_prep_clone(rq, rq_src);
2622
2623 return 0;
2624
2625free_and_out:
2626 if (bio)
2627 bio_free(bio, bs);
2628 blk_rq_unprep_clone(rq);
2629
2630 return -ENOMEM;
2631}
2632EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
2633
Jens Axboe18887ad2008-07-28 13:08:45 +02002634int kblockd_schedule_work(struct request_queue *q, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635{
2636 return queue_work(kblockd_workqueue, work);
2637}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638EXPORT_SYMBOL(kblockd_schedule_work);
2639
Vivek Goyale43473b2010-09-15 17:06:35 -04002640int kblockd_schedule_delayed_work(struct request_queue *q,
2641 struct delayed_work *dwork, unsigned long delay)
2642{
2643 return queue_delayed_work(kblockd_workqueue, dwork, delay);
2644}
2645EXPORT_SYMBOL(kblockd_schedule_delayed_work);
2646
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647int __init blk_dev_init(void)
2648{
Nikanth Karthikesan9eb55b02009-04-27 14:53:54 +02002649 BUILD_BUG_ON(__REQ_NR_BITS > 8 *
2650 sizeof(((struct request *)0)->cmd_flags));
2651
Tejun Heo89b90be2011-01-03 15:01:47 +01002652 /* used for unplugging and affects IO latency/throughput - HIGHPRI */
2653 kblockd_workqueue = alloc_workqueue("kblockd",
2654 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 if (!kblockd_workqueue)
2656 panic("Failed to create kblockd\n");
2657
2658 request_cachep = kmem_cache_create("blkdev_requests",
Paul Mundt20c2df82007-07-20 10:11:58 +09002659 sizeof(struct request), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
Jens Axboe8324aa92008-01-29 14:51:59 +01002661 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02002662 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 return 0;
2665}