blob: ab4a7696956d9c2ae4599e9cd4261caf958f18a0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 1994, Karl Keyte: Added support for disk statistics
4 * Elevator latency, (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
5 * Queue request tables / lock, selectable elevator, Jens Axboe <axboe@suse.de>
Jens Axboe6728cb02008-01-31 13:03:55 +01006 * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au>
7 * - July2000
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
9 */
10
11/*
12 * This handles all read/write requests to block devices
13 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/backing-dev.h>
17#include <linux/bio.h>
18#include <linux/blkdev.h>
19#include <linux/highmem.h>
20#include <linux/mm.h>
21#include <linux/kernel_stat.h>
22#include <linux/string.h>
23#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/completion.h>
25#include <linux/slab.h>
26#include <linux/swap.h>
27#include <linux/writeback.h>
Andrew Mortonfaccbd4b2006-12-10 02:19:35 -080028#include <linux/task_io_accounting_ops.h>
Akinobu Mitac17bb492006-12-08 02:39:46 -080029#include <linux/fault-inject.h>
Li Zefan55782132009-06-09 13:43:05 +080030
31#define CREATE_TRACE_POINTS
32#include <trace/events/block.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Jens Axboe8324aa92008-01-29 14:51:59 +010034#include "blk.h"
35
Ingo Molnar0bfc2452008-11-26 11:59:56 +010036EXPORT_TRACEPOINT_SYMBOL_GPL(block_remap);
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();
Jens Axboef253b862010-10-24 22:06:02 +020067 part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq));
Tejun Heoc9959052008-08-25 19:47:21 +090068
Jens Axboef253b862010-10-24 22:06:02 +020069 if (!new_io)
Tejun Heo074a7ac2008-08-25 19:56:14 +090070 part_stat_inc(cpu, part, merges[rw]);
Jens Axboef253b862010-10-24 22:06:02 +020071 else {
Tejun Heo074a7ac2008-08-25 19:56:14 +090072 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +020073 part_inc_in_flight(part, rw);
Jens Axboe26b82562008-01-29 13:54:41 +010074 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +020075
Tejun Heo074a7ac2008-08-25 19:56:14 +090076 part_stat_unlock();
Jens Axboe26b82562008-01-29 13:54:41 +010077}
78
Jens Axboe8324aa92008-01-29 14:51:59 +010079void blk_queue_congestion_threshold(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
81 int nr;
82
83 nr = q->nr_requests - (q->nr_requests / 8) + 1;
84 if (nr > q->nr_requests)
85 nr = q->nr_requests;
86 q->nr_congestion_on = nr;
87
88 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
89 if (nr < 1)
90 nr = 1;
91 q->nr_congestion_off = nr;
92}
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/**
95 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
96 * @bdev: device
97 *
98 * Locates the passed device's request queue and returns the address of its
99 * backing_dev_info
100 *
101 * Will return NULL if the request queue cannot be located.
102 */
103struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
104{
105 struct backing_dev_info *ret = NULL;
Jens Axboe165125e2007-07-24 09:28:11 +0200106 struct request_queue *q = bdev_get_queue(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 if (q)
109 ret = &q->backing_dev_info;
110 return ret;
111}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112EXPORT_SYMBOL(blk_get_backing_dev_info);
113
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200114void blk_rq_init(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200116 memset(rq, 0, sizeof(*rq));
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 INIT_LIST_HEAD(&rq->queuelist);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700119 INIT_LIST_HEAD(&rq->timeout_list);
Jens Axboec7c22e42008-09-13 20:26:01 +0200120 rq->cpu = -1;
Jens Axboe63a71382008-02-08 12:41:03 +0100121 rq->q = q;
Tejun Heoa2dec7b2009-05-07 22:24:44 +0900122 rq->__sector = (sector_t) -1;
Jens Axboe2e662b62006-07-13 11:55:04 +0200123 INIT_HLIST_NODE(&rq->hash);
124 RB_CLEAR_NODE(&rq->rb_node);
FUJITA Tomonorid7e3c322008-04-29 09:54:39 +0200125 rq->cmd = rq->__cmd;
Li Zefane2494e12009-04-02 13:43:26 +0800126 rq->cmd_len = BLK_MAX_CDB;
Jens Axboe63a71382008-02-08 12:41:03 +0100127 rq->tag = -1;
Jens Axboe63a71382008-02-08 12:41:03 +0100128 rq->ref_count = 1;
Tejun Heob243ddc2009-04-23 11:05:18 +0900129 rq->start_time = jiffies;
Divyesh Shah91952912010-04-01 15:01:41 -0700130 set_start_time_ns(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200132EXPORT_SYMBOL(blk_rq_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
NeilBrown5bb23a62007-09-27 12:46:13 +0200134static void req_bio_endio(struct request *rq, struct bio *bio,
135 unsigned int nbytes, int error)
Tejun Heo797e7db2006-01-06 09:51:03 +0100136{
Tejun Heo143a87f2011-01-25 12:43:52 +0100137 if (error)
138 clear_bit(BIO_UPTODATE, &bio->bi_flags);
139 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
140 error = -EIO;
Tejun Heo797e7db2006-01-06 09:51:03 +0100141
Tejun Heo143a87f2011-01-25 12:43:52 +0100142 if (unlikely(nbytes > bio->bi_size)) {
143 printk(KERN_ERR "%s: want %u bytes done, %u left\n",
144 __func__, nbytes, bio->bi_size);
145 nbytes = bio->bi_size;
NeilBrown5bb23a62007-09-27 12:46:13 +0200146 }
Tejun Heo143a87f2011-01-25 12:43:52 +0100147
148 if (unlikely(rq->cmd_flags & REQ_QUIET))
149 set_bit(BIO_QUIET, &bio->bi_flags);
150
151 bio->bi_size -= nbytes;
152 bio->bi_sector += (nbytes >> 9);
153
154 if (bio_integrity(bio))
155 bio_integrity_advance(bio, nbytes);
156
157 /* don't actually finish bio if it's part of flush sequence */
158 if (bio->bi_size == 0 && !(rq->cmd_flags & REQ_FLUSH_SEQ))
159 bio_endio(bio, error);
Tejun Heo797e7db2006-01-06 09:51:03 +0100160}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162void blk_dump_rq_flags(struct request *rq, char *msg)
163{
164 int bit;
165
Jens Axboe6728cb02008-01-31 13:03:55 +0100166 printk(KERN_INFO "%s: dev %s: type=%x, flags=%x\n", msg,
Jens Axboe4aff5e22006-08-10 08:44:47 +0200167 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
168 rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Tejun Heo83096eb2009-05-07 22:24:39 +0900170 printk(KERN_INFO " sector %llu, nr/cnr %u/%u\n",
171 (unsigned long long)blk_rq_pos(rq),
172 blk_rq_sectors(rq), blk_rq_cur_sectors(rq));
Tejun Heo731ec492009-04-23 11:05:20 +0900173 printk(KERN_INFO " bio %p, biotail %p, buffer %p, len %u\n",
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900174 rq->bio, rq->biotail, rq->buffer, blk_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200176 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
Jens Axboe6728cb02008-01-31 13:03:55 +0100177 printk(KERN_INFO " cdb: ");
FUJITA Tomonorid34c87e2008-04-29 14:37:52 +0200178 for (bit = 0; bit < BLK_MAX_CDB; bit++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 printk("%02x ", rq->cmd[bit]);
180 printk("\n");
181 }
182}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183EXPORT_SYMBOL(blk_dump_rq_flags);
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185/*
186 * "plug" the device if there are no outstanding requests: this will
187 * force the transfer to start only after we have put all the requests
188 * on the list.
189 *
190 * This is called with interrupts off and no requests on the queue and
191 * with the queue lock held.
192 */
Jens Axboe165125e2007-07-24 09:28:11 +0200193void blk_plug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 WARN_ON(!irqs_disabled());
196
197 /*
198 * don't plug a stopped queue, it must be paired with blk_start_queue()
199 * which will restart the queueing
200 */
Coywolf Qi Hunt7daac492006-04-19 10:14:49 +0200201 if (blk_queue_stopped(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return;
203
Jens Axboee48ec692008-07-03 13:18:54 +0200204 if (!queue_flag_test_and_set(QUEUE_FLAG_PLUGGED, q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100206 trace_block_plug(q);
Jens Axboe2056a782006-03-23 20:00:26 +0100207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209EXPORT_SYMBOL(blk_plug_device);
210
Jens Axboe6c5e0c42008-08-01 20:31:32 +0200211/**
212 * blk_plug_device_unlocked - plug a device without queue lock held
213 * @q: The &struct request_queue to plug
214 *
215 * Description:
216 * Like @blk_plug_device(), but grabs the queue lock and disables
217 * interrupts.
218 **/
219void blk_plug_device_unlocked(struct request_queue *q)
220{
221 unsigned long flags;
222
223 spin_lock_irqsave(q->queue_lock, flags);
224 blk_plug_device(q);
225 spin_unlock_irqrestore(q->queue_lock, flags);
226}
227EXPORT_SYMBOL(blk_plug_device_unlocked);
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229/*
230 * remove the queue from the plugged list, if present. called with
231 * queue lock held and interrupts disabled.
232 */
Jens Axboe165125e2007-07-24 09:28:11 +0200233int blk_remove_plug(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
235 WARN_ON(!irqs_disabled());
236
Jens Axboee48ec692008-07-03 13:18:54 +0200237 if (!queue_flag_test_and_clear(QUEUE_FLAG_PLUGGED, q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return 0;
239
240 del_timer(&q->unplug_timer);
241 return 1;
242}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243EXPORT_SYMBOL(blk_remove_plug);
244
245/*
246 * remove the plug and let it rip..
247 */
Jens Axboe165125e2007-07-24 09:28:11 +0200248void __generic_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Coywolf Qi Hunt7daac492006-04-19 10:14:49 +0200250 if (unlikely(blk_queue_stopped(q)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return;
Jens Axboea31a9732008-10-17 13:58:29 +0200252 if (!blk_remove_plug(q) && !blk_queue_nonrot(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return;
254
Jens Axboe22e2c502005-06-27 10:55:12 +0200255 q->request_fn(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258/**
259 * generic_unplug_device - fire a request queue
Jens Axboe165125e2007-07-24 09:28:11 +0200260 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 *
262 * Description:
263 * Linux uses plugging to build bigger requests queues before letting
264 * the device have at them. If a queue is plugged, the I/O scheduler
265 * is still adding and merging requests on the queue. Once the queue
266 * gets unplugged, the request_fn defined for the queue is invoked and
267 * transfers started.
268 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200269void generic_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Jens Axboedbaf2c02008-05-07 09:48:17 +0200271 if (blk_queue_plugged(q)) {
272 spin_lock_irq(q->queue_lock);
273 __generic_unplug_device(q);
274 spin_unlock_irq(q->queue_lock);
275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277EXPORT_SYMBOL(generic_unplug_device);
278
279static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
280 struct page *page)
281{
Jens Axboe165125e2007-07-24 09:28:11 +0200282 struct request_queue *q = bdi->unplug_io_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500284 blk_unplug(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
Jens Axboe86db1e22008-01-29 14:53:40 +0100287void blk_unplug_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Jens Axboe165125e2007-07-24 09:28:11 +0200289 struct request_queue *q =
290 container_of(work, struct request_queue, unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100292 trace_block_unplug_io(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 q->unplug_fn(q);
294}
295
Jens Axboe86db1e22008-01-29 14:53:40 +0100296void blk_unplug_timeout(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Jens Axboe165125e2007-07-24 09:28:11 +0200298 struct request_queue *q = (struct request_queue *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100300 trace_block_unplug_timer(q);
Jens Axboe18887ad2008-07-28 13:08:45 +0200301 kblockd_schedule_work(q, &q->unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500304void blk_unplug(struct request_queue *q)
305{
306 /*
307 * devices don't necessarily have an ->unplug_fn defined
308 */
309 if (q->unplug_fn) {
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100310 trace_block_unplug_io(q);
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500311 q->unplug_fn(q);
312 }
313}
314EXPORT_SYMBOL(blk_unplug);
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316/**
317 * blk_start_queue - restart a previously stopped queue
Jens Axboe165125e2007-07-24 09:28:11 +0200318 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 *
320 * Description:
321 * blk_start_queue() will clear the stop flag on the queue, and call
322 * the request_fn for the queue if it was in a stopped state when
323 * entered. Also see blk_stop_queue(). Queue lock must be held.
324 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200325void blk_start_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200327 WARN_ON(!irqs_disabled());
328
Nick Piggin75ad23b2008-04-29 14:48:33 +0200329 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
Tejun Heoa538cd02009-04-23 11:05:17 +0900330 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332EXPORT_SYMBOL(blk_start_queue);
333
334/**
335 * blk_stop_queue - stop a queue
Jens Axboe165125e2007-07-24 09:28:11 +0200336 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 *
338 * Description:
339 * The Linux block layer assumes that a block driver will consume all
340 * entries on the request queue when the request_fn strategy is called.
341 * Often this will not happen, because of hardware limitations (queue
342 * depth settings). If a device driver gets a 'queue full' response,
343 * or if it simply chooses not to queue more I/O at one point, it can
344 * call this function to prevent the request_fn from being called until
345 * the driver has signalled it's ready to go again. This happens by calling
346 * blk_start_queue() to restart queue operations. Queue lock must be held.
347 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200348void blk_stop_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 blk_remove_plug(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200351 queue_flag_set(QUEUE_FLAG_STOPPED, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353EXPORT_SYMBOL(blk_stop_queue);
354
355/**
356 * blk_sync_queue - cancel any pending callbacks on a queue
357 * @q: the queue
358 *
359 * Description:
360 * The block layer may perform asynchronous callback activity
361 * on a queue, such as calling the unplug function after a timeout.
362 * A block device may call blk_sync_queue to ensure that any
363 * such activity is cancelled, thus allowing it to release resources
Michael Opdenacker59c51592007-05-09 08:57:56 +0200364 * that the callbacks might use. The caller must already have made sure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 * that its ->make_request_fn will not re-add plugging prior to calling
366 * this function.
367 *
368 */
369void blk_sync_queue(struct request_queue *q)
370{
371 del_timer_sync(&q->unplug_timer);
Jens Axboe70ed28b2008-11-19 14:38:39 +0100372 del_timer_sync(&q->timeout);
Cheng Renquan64d01dc2008-12-03 12:41:39 +0100373 cancel_work_sync(&q->unplug_work);
Vivek Goyale43473b2010-09-15 17:06:35 -0400374 throtl_shutdown_timer_wq(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376EXPORT_SYMBOL(blk_sync_queue);
377
378/**
Jens Axboe80a4b582008-10-14 09:51:06 +0200379 * __blk_run_queue - run a single device queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200381 *
382 * Description:
383 * See @blk_run_queue. This variant must be called with the queue lock
384 * held and interrupts disabled.
385 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 */
Nick Piggin75ad23b2008-04-29 14:48:33 +0200387void __blk_run_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 blk_remove_plug(q);
Jens Axboedac07ec2006-05-11 08:20:16 +0200390
Tejun Heoa538cd02009-04-23 11:05:17 +0900391 if (unlikely(blk_queue_stopped(q)))
392 return;
393
394 if (elv_queue_empty(q))
395 return;
396
Jens Axboedac07ec2006-05-11 08:20:16 +0200397 /*
398 * Only recurse once to avoid overrunning the stack, let the unplug
399 * handling reinvoke the handler shortly if we already got there.
400 */
Tejun Heoa538cd02009-04-23 11:05:17 +0900401 if (!queue_flag_test_and_set(QUEUE_FLAG_REENTER, q)) {
402 q->request_fn(q);
403 queue_flag_clear(QUEUE_FLAG_REENTER, q);
404 } else {
405 queue_flag_set(QUEUE_FLAG_PLUGGED, q);
406 kblockd_schedule_work(q, &q->unplug_work);
407 }
Nick Piggin75ad23b2008-04-29 14:48:33 +0200408}
409EXPORT_SYMBOL(__blk_run_queue);
Jens Axboedac07ec2006-05-11 08:20:16 +0200410
Nick Piggin75ad23b2008-04-29 14:48:33 +0200411/**
412 * blk_run_queue - run a single device queue
413 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200414 *
415 * Description:
416 * Invoke request handling on this queue, if it has pending work to do.
Tejun Heoa7f55792009-04-23 11:05:17 +0900417 * May be used to restart queueing when a request has completed.
Nick Piggin75ad23b2008-04-29 14:48:33 +0200418 */
419void blk_run_queue(struct request_queue *q)
420{
421 unsigned long flags;
422
423 spin_lock_irqsave(q->queue_lock, flags);
424 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 spin_unlock_irqrestore(q->queue_lock, flags);
426}
427EXPORT_SYMBOL(blk_run_queue);
428
Jens Axboe165125e2007-07-24 09:28:11 +0200429void blk_put_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500430{
431 kobject_put(&q->kobj);
432}
Al Viro483f4af2006-03-18 18:34:37 -0500433
Jens Axboe6728cb02008-01-31 13:03:55 +0100434void blk_cleanup_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500435{
Jens Axboee3335de92008-09-18 09:22:54 -0700436 /*
437 * We know we have process context here, so we can be a little
438 * cautious and ensure that pending block actions on this device
439 * are done before moving on. Going into this function, we should
440 * not have processes doing IO to this device.
441 */
442 blk_sync_queue(q);
443
Matthew Garrett31373d02010-04-06 14:25:14 +0200444 del_timer_sync(&q->backing_dev_info.laptop_mode_wb_timer);
Al Viro483f4af2006-03-18 18:34:37 -0500445 mutex_lock(&q->sysfs_lock);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200446 queue_flag_set_unlocked(QUEUE_FLAG_DEAD, q);
Al Viro483f4af2006-03-18 18:34:37 -0500447 mutex_unlock(&q->sysfs_lock);
448
449 if (q->elevator)
450 elevator_exit(q->elevator);
451
452 blk_put_queue(q);
453}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454EXPORT_SYMBOL(blk_cleanup_queue);
455
Jens Axboe165125e2007-07-24 09:28:11 +0200456static int blk_init_free_list(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
458 struct request_list *rl = &q->rq;
459
Mike Snitzer1abec4f2010-05-25 13:15:15 -0400460 if (unlikely(rl->rq_pool))
461 return 0;
462
Jens Axboe1faa16d2009-04-06 14:48:01 +0200463 rl->count[BLK_RW_SYNC] = rl->count[BLK_RW_ASYNC] = 0;
464 rl->starved[BLK_RW_SYNC] = rl->starved[BLK_RW_ASYNC] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200465 rl->elvpriv = 0;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200466 init_waitqueue_head(&rl->wait[BLK_RW_SYNC]);
467 init_waitqueue_head(&rl->wait[BLK_RW_ASYNC]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Christoph Lameter19460892005-06-23 00:08:19 -0700469 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
470 mempool_free_slab, request_cachep, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 if (!rl->rq_pool)
473 return -ENOMEM;
474
475 return 0;
476}
477
Jens Axboe165125e2007-07-24 09:28:11 +0200478struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Christoph Lameter19460892005-06-23 00:08:19 -0700480 return blk_alloc_queue_node(gfp_mask, -1);
481}
482EXPORT_SYMBOL(blk_alloc_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Jens Axboe165125e2007-07-24 09:28:11 +0200484struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -0700485{
Jens Axboe165125e2007-07-24 09:28:11 +0200486 struct request_queue *q;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700487 int err;
Christoph Lameter19460892005-06-23 00:08:19 -0700488
Jens Axboe8324aa92008-01-29 14:51:59 +0100489 q = kmem_cache_alloc_node(blk_requestq_cachep,
Christoph Lameter94f60302007-07-17 04:03:29 -0700490 gfp_mask | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (!q)
492 return NULL;
493
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700494 q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
495 q->backing_dev_info.unplug_io_data = q;
Jens Axboe0989a022009-06-12 14:42:56 +0200496 q->backing_dev_info.ra_pages =
497 (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
498 q->backing_dev_info.state = 0;
499 q->backing_dev_info.capabilities = BDI_CAP_MAP_COPY;
Jens Axboed9938312009-06-12 14:45:52 +0200500 q->backing_dev_info.name = "block";
Jens Axboe0989a022009-06-12 14:42:56 +0200501
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700502 err = bdi_init(&q->backing_dev_info);
503 if (err) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100504 kmem_cache_free(blk_requestq_cachep, q);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700505 return NULL;
506 }
507
Vivek Goyale43473b2010-09-15 17:06:35 -0400508 if (blk_throtl_init(q)) {
509 kmem_cache_free(blk_requestq_cachep, q);
510 return NULL;
511 }
512
Matthew Garrett31373d02010-04-06 14:25:14 +0200513 setup_timer(&q->backing_dev_info.laptop_mode_wb_timer,
514 laptop_mode_timer_fn, (unsigned long) q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 init_timer(&q->unplug_timer);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700516 setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
517 INIT_LIST_HEAD(&q->timeout_list);
Tejun Heoae1b1532011-01-25 12:43:54 +0100518 INIT_LIST_HEAD(&q->flush_queue[0]);
519 INIT_LIST_HEAD(&q->flush_queue[1]);
520 INIT_LIST_HEAD(&q->flush_data_in_flight);
Peter Zijlstra713ada92008-10-16 13:44:57 +0200521 INIT_WORK(&q->unplug_work, blk_unplug_work);
Al Viro483f4af2006-03-18 18:34:37 -0500522
Jens Axboe8324aa92008-01-29 14:51:59 +0100523 kobject_init(&q->kobj, &blk_queue_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Al Viro483f4af2006-03-18 18:34:37 -0500525 mutex_init(&q->sysfs_lock);
Neil Browne7e72bf2008-05-14 16:05:54 -0700526 spin_lock_init(&q->__queue_lock);
Al Viro483f4af2006-03-18 18:34:37 -0500527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return q;
529}
Christoph Lameter19460892005-06-23 00:08:19 -0700530EXPORT_SYMBOL(blk_alloc_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532/**
533 * blk_init_queue - prepare a request queue for use with a block device
534 * @rfn: The function to be called to process requests that have been
535 * placed on the queue.
536 * @lock: Request queue spin lock
537 *
538 * Description:
539 * If a block device wishes to use the standard request handling procedures,
540 * which sorts requests and coalesces adjacent requests, then it must
541 * call blk_init_queue(). The function @rfn will be called when there
542 * are requests on the queue that need to be processed. If the device
543 * supports plugging, then @rfn may not be called immediately when requests
544 * are available on the queue, but may be called at some time later instead.
545 * Plugged queues are generally unplugged when a buffer belonging to one
546 * of the requests on the queue is needed, or due to memory pressure.
547 *
548 * @rfn is not required, or even expected, to remove all requests off the
549 * queue, but only as many as it can handle at a time. If it does leave
550 * requests on the queue, it is responsible for arranging that the requests
551 * get dealt with eventually.
552 *
553 * The queue spin lock must be held while manipulating the requests on the
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200554 * request queue; this lock will be taken also from interrupt context, so irq
555 * disabling is needed for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 *
Randy Dunlap710027a2008-08-19 20:13:11 +0200557 * Function returns a pointer to the initialized request queue, or %NULL if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 * it didn't succeed.
559 *
560 * Note:
561 * blk_init_queue() must be paired with a blk_cleanup_queue() call
562 * when the block device is deactivated (such as at module unload).
563 **/
Christoph Lameter19460892005-06-23 00:08:19 -0700564
Jens Axboe165125e2007-07-24 09:28:11 +0200565struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Christoph Lameter19460892005-06-23 00:08:19 -0700567 return blk_init_queue_node(rfn, lock, -1);
568}
569EXPORT_SYMBOL(blk_init_queue);
570
Jens Axboe165125e2007-07-24 09:28:11 +0200571struct request_queue *
Christoph Lameter19460892005-06-23 00:08:19 -0700572blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
573{
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600574 struct request_queue *uninit_q, *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600576 uninit_q = blk_alloc_queue_node(GFP_KERNEL, node_id);
577 if (!uninit_q)
578 return NULL;
579
580 q = blk_init_allocated_queue_node(uninit_q, rfn, lock, node_id);
581 if (!q)
582 blk_cleanup_queue(uninit_q);
583
584 return q;
Mike Snitzer01effb02010-05-11 08:57:42 +0200585}
586EXPORT_SYMBOL(blk_init_queue_node);
587
588struct request_queue *
589blk_init_allocated_queue(struct request_queue *q, request_fn_proc *rfn,
590 spinlock_t *lock)
591{
592 return blk_init_allocated_queue_node(q, rfn, lock, -1);
593}
594EXPORT_SYMBOL(blk_init_allocated_queue);
595
596struct request_queue *
597blk_init_allocated_queue_node(struct request_queue *q, request_fn_proc *rfn,
598 spinlock_t *lock, int node_id)
599{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (!q)
601 return NULL;
602
Christoph Lameter19460892005-06-23 00:08:19 -0700603 q->node = node_id;
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600604 if (blk_init_free_list(q))
Al Viro8669aaf2006-03-18 13:50:00 -0500605 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 q->request_fn = rfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 q->prep_rq_fn = NULL;
James Bottomley28018c22010-07-01 19:49:17 +0900609 q->unprep_rq_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 q->unplug_fn = generic_unplug_device;
Jens Axboebc58ba92009-01-23 10:54:44 +0100611 q->queue_flags = QUEUE_FLAG_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 q->queue_lock = lock;
613
Jens Axboef3b144a2009-03-06 08:48:33 +0100614 /*
615 * This also sets hw/phys segments, boundary and size
616 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 blk_queue_make_request(q, __make_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Alan Stern44ec9542007-02-20 11:01:57 -0500619 q->sg_reserved_size = INT_MAX;
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 /*
622 * all done
623 */
624 if (!elevator_init(q, NULL)) {
625 blk_queue_congestion_threshold(q);
626 return q;
627 }
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return NULL;
630}
Mike Snitzer01effb02010-05-11 08:57:42 +0200631EXPORT_SYMBOL(blk_init_allocated_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Jens Axboe165125e2007-07-24 09:28:11 +0200633int blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Nick Pigginfde6ad22005-06-23 00:08:53 -0700635 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
Al Viro483f4af2006-03-18 18:34:37 -0500636 kobject_get(&q->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return 0;
638 }
639
640 return 1;
641}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Jens Axboe165125e2007-07-24 09:28:11 +0200643static inline void blk_free_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Jens Axboe4aff5e22006-08-10 08:44:47 +0200645 if (rq->cmd_flags & REQ_ELVPRIV)
Tejun Heocb98fc82005-10-28 08:29:39 +0200646 elv_put_request(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 mempool_free(rq, q->rq.rq_pool);
648}
649
Jens Axboe1ea25ecb2006-07-18 22:24:11 +0200650static struct request *
Jerome Marchand42dad762009-04-22 14:01:49 +0200651blk_alloc_request(struct request_queue *q, int flags, int priv, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
653 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
654
655 if (!rq)
656 return NULL;
657
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200658 blk_rq_init(q, rq);
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200659
Jerome Marchand42dad762009-04-22 14:01:49 +0200660 rq->cmd_flags = flags | REQ_ALLOCED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Tejun Heocb98fc82005-10-28 08:29:39 +0200662 if (priv) {
Jens Axboecb78b282006-07-28 09:32:57 +0200663 if (unlikely(elv_set_request(q, rq, gfp_mask))) {
Tejun Heocb98fc82005-10-28 08:29:39 +0200664 mempool_free(rq, q->rq.rq_pool);
665 return NULL;
666 }
Jens Axboe4aff5e22006-08-10 08:44:47 +0200667 rq->cmd_flags |= REQ_ELVPRIV;
Tejun Heocb98fc82005-10-28 08:29:39 +0200668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Tejun Heocb98fc82005-10-28 08:29:39 +0200670 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
673/*
674 * ioc_batching returns true if the ioc is a valid batching request and
675 * should be given priority access to a request.
676 */
Jens Axboe165125e2007-07-24 09:28:11 +0200677static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
679 if (!ioc)
680 return 0;
681
682 /*
683 * Make sure the process is able to allocate at least 1 request
684 * even if the batch times out, otherwise we could theoretically
685 * lose wakeups.
686 */
687 return ioc->nr_batch_requests == q->nr_batching ||
688 (ioc->nr_batch_requests > 0
689 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
690}
691
692/*
693 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
694 * will cause the process to be a "batcher" on all queues in the system. This
695 * is the behaviour we want though - once it gets a wakeup it should be given
696 * a nice run.
697 */
Jens Axboe165125e2007-07-24 09:28:11 +0200698static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
700 if (!ioc || ioc_batching(q, ioc))
701 return;
702
703 ioc->nr_batch_requests = q->nr_batching;
704 ioc->last_waited = jiffies;
705}
706
Jens Axboe1faa16d2009-04-06 14:48:01 +0200707static void __freed_request(struct request_queue *q, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
709 struct request_list *rl = &q->rq;
710
Jens Axboe1faa16d2009-04-06 14:48:01 +0200711 if (rl->count[sync] < queue_congestion_off_threshold(q))
712 blk_clear_queue_congested(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Jens Axboe1faa16d2009-04-06 14:48:01 +0200714 if (rl->count[sync] + 1 <= q->nr_requests) {
715 if (waitqueue_active(&rl->wait[sync]))
716 wake_up(&rl->wait[sync]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Jens Axboe1faa16d2009-04-06 14:48:01 +0200718 blk_clear_queue_full(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
720}
721
722/*
723 * A request has just been released. Account for it, update the full and
724 * congestion status, wake up any waiters. Called under q->queue_lock.
725 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200726static void freed_request(struct request_queue *q, int sync, int priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
728 struct request_list *rl = &q->rq;
729
Jens Axboe1faa16d2009-04-06 14:48:01 +0200730 rl->count[sync]--;
Tejun Heocb98fc82005-10-28 08:29:39 +0200731 if (priv)
732 rl->elvpriv--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Jens Axboe1faa16d2009-04-06 14:48:01 +0200734 __freed_request(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Jens Axboe1faa16d2009-04-06 14:48:01 +0200736 if (unlikely(rl->starved[sync ^ 1]))
737 __freed_request(q, sync ^ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740/*
Mike Snitzer9d5a4e92011-02-11 11:05:46 +0100741 * Determine if elevator data should be initialized when allocating the
742 * request associated with @bio.
743 */
744static bool blk_rq_should_init_elevator(struct bio *bio)
745{
746 if (!bio)
747 return true;
748
749 /*
750 * Flush requests do not use the elevator so skip initialization.
751 * This allows a request to share the flush and elevator data.
752 */
753 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA))
754 return false;
755
756 return true;
757}
758
759/*
Nick Piggind6344532005-06-28 20:45:14 -0700760 * Get a free request, queue_lock must be held.
761 * Returns NULL on failure, with queue_lock held.
762 * Returns !NULL on success, with queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 */
Jens Axboe165125e2007-07-24 09:28:11 +0200764static struct request *get_request(struct request_queue *q, int rw_flags,
Jens Axboe7749a8d2006-12-13 13:02:26 +0100765 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
767 struct request *rq = NULL;
768 struct request_list *rl = &q->rq;
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100769 struct io_context *ioc = NULL;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200770 const bool is_sync = rw_is_sync(rw_flags) != 0;
Mike Snitzer9d5a4e92011-02-11 11:05:46 +0100771 int may_queue, priv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Jens Axboe7749a8d2006-12-13 13:02:26 +0100773 may_queue = elv_may_queue(q, rw_flags);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100774 if (may_queue == ELV_MQUEUE_NO)
775 goto rq_starved;
776
Jens Axboe1faa16d2009-04-06 14:48:01 +0200777 if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) {
778 if (rl->count[is_sync]+1 >= q->nr_requests) {
Jens Axboeb5deef92006-07-19 23:39:40 +0200779 ioc = current_io_context(GFP_ATOMIC, q->node);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100780 /*
781 * The queue will fill after this allocation, so set
782 * it as full, and mark this process as "batching".
783 * This process will be allowed to complete a batch of
784 * requests, others will be blocked.
785 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200786 if (!blk_queue_full(q, is_sync)) {
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100787 ioc_set_batching(q, ioc);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200788 blk_set_queue_full(q, is_sync);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100789 } else {
790 if (may_queue != ELV_MQUEUE_MUST
791 && !ioc_batching(q, ioc)) {
792 /*
793 * The queue is full and the allocating
794 * process is not a "batcher", and not
795 * exempted by the IO scheduler
796 */
797 goto out;
798 }
799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
Jens Axboe1faa16d2009-04-06 14:48:01 +0200801 blk_set_queue_congested(q, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
803
Jens Axboe082cf692005-06-28 16:35:11 +0200804 /*
805 * Only allow batching queuers to allocate up to 50% over the defined
806 * limit of requests, otherwise we could have thousands of requests
807 * allocated with any setting of ->nr_requests
808 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200809 if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
Jens Axboe082cf692005-06-28 16:35:11 +0200810 goto out;
Hugh Dickinsfd782a42005-06-29 15:15:40 +0100811
Jens Axboe1faa16d2009-04-06 14:48:01 +0200812 rl->count[is_sync]++;
813 rl->starved[is_sync] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200814
Mike Snitzer9d5a4e92011-02-11 11:05:46 +0100815 if (blk_rq_should_init_elevator(bio)) {
816 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
817 if (priv)
818 rl->elvpriv++;
819 }
Tejun Heocb98fc82005-10-28 08:29:39 +0200820
Jens Axboef253b862010-10-24 22:06:02 +0200821 if (blk_queue_io_stat(q))
822 rw_flags |= REQ_IO_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 spin_unlock_irq(q->queue_lock);
824
Jens Axboe7749a8d2006-12-13 13:02:26 +0100825 rq = blk_alloc_request(q, rw_flags, priv, gfp_mask);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100826 if (unlikely(!rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 /*
828 * Allocation failed presumably due to memory. Undo anything
829 * we might have messed up.
830 *
831 * Allocating task should really be put onto the front of the
832 * wait queue, but this is pretty rare.
833 */
834 spin_lock_irq(q->queue_lock);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200835 freed_request(q, is_sync, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 /*
838 * in the very unlikely event that allocation failed and no
839 * requests for this direction was pending, mark us starved
840 * so that freeing of a request in the other direction will
841 * notice us. another possible fix would be to split the
842 * rq mempool into READ and WRITE
843 */
844rq_starved:
Jens Axboe1faa16d2009-04-06 14:48:01 +0200845 if (unlikely(rl->count[is_sync] == 0))
846 rl->starved[is_sync] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 goto out;
849 }
850
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100851 /*
852 * ioc may be NULL here, and ioc_batching will be false. That's
853 * OK, if the queue is under the request limit then requests need
854 * not count toward the nr_batch_requests limit. There will always
855 * be some limit enforced by BLK_BATCH_TIME.
856 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (ioc_batching(q, ioc))
858 ioc->nr_batch_requests--;
Jens Axboe6728cb02008-01-31 13:03:55 +0100859
Jens Axboe1faa16d2009-04-06 14:48:01 +0200860 trace_block_getrq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return rq;
863}
864
865/*
866 * No available requests for this queue, unplug the device and wait for some
867 * requests to become available.
Nick Piggind6344532005-06-28 20:45:14 -0700868 *
869 * Called with q->queue_lock held, and returns with it unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 */
Jens Axboe165125e2007-07-24 09:28:11 +0200871static struct request *get_request_wait(struct request_queue *q, int rw_flags,
Jens Axboe22e2c502005-06-27 10:55:12 +0200872 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Jens Axboe1faa16d2009-04-06 14:48:01 +0200874 const bool is_sync = rw_is_sync(rw_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 struct request *rq;
876
Jens Axboe7749a8d2006-12-13 13:02:26 +0100877 rq = get_request(q, rw_flags, bio, GFP_NOIO);
Nick Piggin450991b2005-06-28 20:45:13 -0700878 while (!rq) {
879 DEFINE_WAIT(wait);
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200880 struct io_context *ioc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 struct request_list *rl = &q->rq;
882
Jens Axboe1faa16d2009-04-06 14:48:01 +0200883 prepare_to_wait_exclusive(&rl->wait[is_sync], &wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 TASK_UNINTERRUPTIBLE);
885
Jens Axboe1faa16d2009-04-06 14:48:01 +0200886 trace_block_sleeprq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200888 __generic_unplug_device(q);
889 spin_unlock_irq(q->queue_lock);
890 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200892 /*
893 * After sleeping, we become a "batching" process and
894 * will be able to allocate at least one request, and
895 * up to a big batch of them for a small period time.
896 * See ioc_batching, ioc_set_batching
897 */
898 ioc = current_io_context(GFP_NOIO, q->node);
899 ioc_set_batching(q, ioc);
Jens Axboe2056a782006-03-23 20:00:26 +0100900
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200901 spin_lock_irq(q->queue_lock);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200902 finish_wait(&rl->wait[is_sync], &wait);
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200903
904 rq = get_request(q, rw_flags, bio, GFP_NOIO);
905 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 return rq;
908}
909
Jens Axboe165125e2007-07-24 09:28:11 +0200910struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
912 struct request *rq;
913
914 BUG_ON(rw != READ && rw != WRITE);
915
Nick Piggind6344532005-06-28 20:45:14 -0700916 spin_lock_irq(q->queue_lock);
917 if (gfp_mask & __GFP_WAIT) {
Jens Axboe22e2c502005-06-27 10:55:12 +0200918 rq = get_request_wait(q, rw, NULL);
Nick Piggind6344532005-06-28 20:45:14 -0700919 } else {
Jens Axboe22e2c502005-06-27 10:55:12 +0200920 rq = get_request(q, rw, NULL, gfp_mask);
Nick Piggind6344532005-06-28 20:45:14 -0700921 if (!rq)
922 spin_unlock_irq(q->queue_lock);
923 }
924 /* q->queue_lock is unlocked at this point */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 return rq;
927}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928EXPORT_SYMBOL(blk_get_request);
929
930/**
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300931 * blk_make_request - given a bio, allocate a corresponding struct request.
Randy Dunlap8ebf9752009-06-11 20:00:41 -0700932 * @q: target request queue
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300933 * @bio: The bio describing the memory mappings that will be submitted for IO.
934 * It may be a chained-bio properly constructed by block/bio layer.
Randy Dunlap8ebf9752009-06-11 20:00:41 -0700935 * @gfp_mask: gfp flags to be used for memory allocation
Jens Axboedc72ef42006-07-20 14:54:05 +0200936 *
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300937 * blk_make_request is the parallel of generic_make_request for BLOCK_PC
938 * type commands. Where the struct request needs to be farther initialized by
939 * the caller. It is passed a &struct bio, which describes the memory info of
940 * the I/O transfer.
941 *
942 * The caller of blk_make_request must make sure that bi_io_vec
943 * are set to describe the memory buffers. That bio_data_dir() will return
944 * the needed direction of the request. (And all bio's in the passed bio-chain
945 * are properly set accordingly)
946 *
947 * If called under none-sleepable conditions, mapped bio buffers must not
948 * need bouncing, by calling the appropriate masked or flagged allocator,
949 * suitable for the target device. Otherwise the call to blk_queue_bounce will
950 * BUG.
Jens Axboe53674ac2009-05-19 19:52:35 +0200951 *
952 * WARNING: When allocating/cloning a bio-chain, careful consideration should be
953 * given to how you allocate bios. In particular, you cannot use __GFP_WAIT for
954 * anything but the first bio in the chain. Otherwise you risk waiting for IO
955 * completion of a bio that hasn't been submitted yet, thus resulting in a
956 * deadlock. Alternatively bios should be allocated using bio_kmalloc() instead
957 * of bio_alloc(), as that avoids the mempool deadlock.
958 * If possible a big IO should be split into smaller parts when allocation
959 * fails. Partial allocation should not be an error, or you risk a live-lock.
Jens Axboedc72ef42006-07-20 14:54:05 +0200960 */
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300961struct request *blk_make_request(struct request_queue *q, struct bio *bio,
962 gfp_t gfp_mask)
Jens Axboedc72ef42006-07-20 14:54:05 +0200963{
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300964 struct request *rq = blk_get_request(q, bio_data_dir(bio), gfp_mask);
965
966 if (unlikely(!rq))
967 return ERR_PTR(-ENOMEM);
968
969 for_each_bio(bio) {
970 struct bio *bounce_bio = bio;
971 int ret;
972
973 blk_queue_bounce(q, &bounce_bio);
974 ret = blk_rq_append_bio(q, rq, bounce_bio);
975 if (unlikely(ret)) {
976 blk_put_request(rq);
977 return ERR_PTR(ret);
978 }
979 }
980
981 return rq;
Jens Axboedc72ef42006-07-20 14:54:05 +0200982}
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300983EXPORT_SYMBOL(blk_make_request);
Jens Axboedc72ef42006-07-20 14:54:05 +0200984
985/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 * blk_requeue_request - put a request back on queue
987 * @q: request queue where request should be inserted
988 * @rq: request to be inserted
989 *
990 * Description:
991 * Drivers often keep queueing requests until the hardware cannot accept
992 * more, when that condition happens we need to put the request back
993 * on the queue. Must be called with queue lock held.
994 */
Jens Axboe165125e2007-07-24 09:28:11 +0200995void blk_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
Jens Axboe242f9dc2008-09-14 05:55:09 -0700997 blk_delete_timer(rq);
998 blk_clear_rq_complete(rq);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100999 trace_block_rq_requeue(q, rq);
Jens Axboe2056a782006-03-23 20:00:26 +01001000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 if (blk_rq_tagged(rq))
1002 blk_queue_end_tag(q, rq);
1003
James Bottomleyba396a62009-05-27 14:17:08 +02001004 BUG_ON(blk_queued_rq(rq));
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 elv_requeue_request(q, rq);
1007}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008EXPORT_SYMBOL(blk_requeue_request);
1009
1010/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001011 * blk_insert_request - insert a special request into a request queue
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 * @q: request queue where request should be inserted
1013 * @rq: request to be inserted
1014 * @at_head: insert request at head or tail of queue
1015 * @data: private data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 *
1017 * Description:
1018 * Many block devices need to execute commands asynchronously, so they don't
1019 * block the whole kernel from preemption during request execution. This is
1020 * accomplished normally by inserting aritficial requests tagged as
Randy Dunlap710027a2008-08-19 20:13:11 +02001021 * REQ_TYPE_SPECIAL in to the corresponding request queue, and letting them
1022 * be scheduled for actual execution by the request queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 *
1024 * We have the option of inserting the head or the tail of the queue.
1025 * Typically we use the tail for new ioctls and so forth. We use the head
1026 * of the queue for things like a QUEUE_FULL message from a device, or a
1027 * host that is unable to accept a particular command.
1028 */
Jens Axboe165125e2007-07-24 09:28:11 +02001029void blk_insert_request(struct request_queue *q, struct request *rq,
Tejun Heo 867d1192005-04-24 02:06:05 -05001030 int at_head, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
Tejun Heo 867d1192005-04-24 02:06:05 -05001032 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 unsigned long flags;
1034
1035 /*
1036 * tell I/O scheduler that this isn't a regular read/write (ie it
1037 * must not attempt merges on this) and that it acts as a soft
1038 * barrier
1039 */
Jens Axboe4aff5e22006-08-10 08:44:47 +02001040 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 rq->special = data;
1043
1044 spin_lock_irqsave(q->queue_lock, flags);
1045
1046 /*
1047 * If command is tagged, release the tag
1048 */
Tejun Heo 867d1192005-04-24 02:06:05 -05001049 if (blk_rq_tagged(rq))
1050 blk_queue_end_tag(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Jerome Marchandb238b3d2007-10-23 15:05:46 +02001052 drive_stat_acct(rq, 1);
Tejun Heo 867d1192005-04-24 02:06:05 -05001053 __elv_add_request(q, rq, where, 0);
Tejun Heoa7f55792009-04-23 11:05:17 +09001054 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 spin_unlock_irqrestore(q->queue_lock, flags);
1056}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057EXPORT_SYMBOL(blk_insert_request);
1058
Tejun Heo074a7ac2008-08-25 19:56:14 +09001059static void part_round_stats_single(int cpu, struct hd_struct *part,
1060 unsigned long now)
1061{
1062 if (now == part->stamp)
1063 return;
1064
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001065 if (part_in_flight(part)) {
Tejun Heo074a7ac2008-08-25 19:56:14 +09001066 __part_stat_add(cpu, part, time_in_queue,
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001067 part_in_flight(part) * (now - part->stamp));
Tejun Heo074a7ac2008-08-25 19:56:14 +09001068 __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
1069 }
1070 part->stamp = now;
1071}
1072
1073/**
Randy Dunlap496aa8a2008-10-16 07:46:23 +02001074 * part_round_stats() - Round off the performance stats on a struct disk_stats.
1075 * @cpu: cpu number for stats access
1076 * @part: target partition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 *
1078 * The average IO queue length and utilisation statistics are maintained
1079 * by observing the current state of the queue length and the amount of
1080 * time it has been in this state for.
1081 *
1082 * Normally, that accounting is done on IO completion, but that can result
1083 * in more than a second's worth of IO being accounted for within any one
1084 * second, leading to >100% utilisation. To deal with that, we call this
1085 * function to do a round-off before returning the results when reading
1086 * /proc/diskstats. This accounts immediately for all queue usage up to
1087 * the current jiffies and restarts the counters again.
1088 */
Tejun Heoc9959052008-08-25 19:47:21 +09001089void part_round_stats(int cpu, struct hd_struct *part)
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001090{
1091 unsigned long now = jiffies;
1092
Tejun Heo074a7ac2008-08-25 19:56:14 +09001093 if (part->partno)
1094 part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
1095 part_round_stats_single(cpu, part, now);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001096}
Tejun Heo074a7ac2008-08-25 19:56:14 +09001097EXPORT_SYMBOL_GPL(part_round_stats);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099/*
1100 * queue lock must be held
1101 */
Jens Axboe165125e2007-07-24 09:28:11 +02001102void __blk_put_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (unlikely(!q))
1105 return;
1106 if (unlikely(--req->ref_count))
1107 return;
1108
Tejun Heo8922e162005-10-20 16:23:44 +02001109 elv_completed_request(q, req);
1110
Boaz Harrosh1cd96c22009-03-24 12:35:07 +01001111 /* this is a bio leak */
1112 WARN_ON(req->bio != NULL);
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 /*
1115 * Request may not have originated from ll_rw_blk. if not,
1116 * it didn't come out of our reserved rq pools
1117 */
Jens Axboe49171e52006-08-10 08:59:11 +02001118 if (req->cmd_flags & REQ_ALLOCED) {
Jens Axboe1faa16d2009-04-06 14:48:01 +02001119 int is_sync = rq_is_sync(req) != 0;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001120 int priv = req->cmd_flags & REQ_ELVPRIV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 BUG_ON(!list_empty(&req->queuelist));
Jens Axboe98170642006-07-28 09:23:08 +02001123 BUG_ON(!hlist_unhashed(&req->hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 blk_free_request(q, req);
Jens Axboe1faa16d2009-04-06 14:48:01 +02001126 freed_request(q, is_sync, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
1128}
Mike Christie6e39b69e2005-11-11 05:30:24 -06001129EXPORT_SYMBOL_GPL(__blk_put_request);
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131void blk_put_request(struct request *req)
1132{
Tejun Heo8922e162005-10-20 16:23:44 +02001133 unsigned long flags;
Jens Axboe165125e2007-07-24 09:28:11 +02001134 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
FUJITA Tomonori52a93ba2008-07-15 21:21:45 +02001136 spin_lock_irqsave(q->queue_lock, flags);
1137 __blk_put_request(q, req);
1138 spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140EXPORT_SYMBOL(blk_put_request);
1141
Christoph Hellwig66ac0282010-06-18 16:59:42 +02001142/**
1143 * blk_add_request_payload - add a payload to a request
1144 * @rq: request to update
1145 * @page: page backing the payload
1146 * @len: length of the payload.
1147 *
1148 * This allows to later add a payload to an already submitted request by
1149 * a block driver. The driver needs to take care of freeing the payload
1150 * itself.
1151 *
1152 * Note that this is a quite horrible hack and nothing but handling of
1153 * discard requests should ever use it.
1154 */
1155void blk_add_request_payload(struct request *rq, struct page *page,
1156 unsigned int len)
1157{
1158 struct bio *bio = rq->bio;
1159
1160 bio->bi_io_vec->bv_page = page;
1161 bio->bi_io_vec->bv_offset = 0;
1162 bio->bi_io_vec->bv_len = len;
1163
1164 bio->bi_size = len;
1165 bio->bi_vcnt = 1;
1166 bio->bi_phys_segments = 1;
1167
1168 rq->__data_len = rq->resid_len = len;
1169 rq->nr_phys_segments = 1;
1170 rq->buffer = bio_data(bio);
1171}
1172EXPORT_SYMBOL_GPL(blk_add_request_payload);
1173
Jens Axboe86db1e22008-01-29 14:53:40 +01001174void init_request_from_bio(struct request *req, struct bio *bio)
Tejun Heo52d9e672006-01-06 09:49:58 +01001175{
Jens Axboec7c22e42008-09-13 20:26:01 +02001176 req->cpu = bio->bi_comp_cpu;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001177 req->cmd_type = REQ_TYPE_FS;
Tejun Heo52d9e672006-01-06 09:49:58 +01001178
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001179 req->cmd_flags |= bio->bi_rw & REQ_COMMON_MASK;
1180 if (bio->bi_rw & REQ_RAHEAD)
Tejun Heoa82afdf2009-07-03 17:48:16 +09001181 req->cmd_flags |= REQ_FAILFAST_MASK;
Jens Axboeb31dc662006-06-13 08:26:10 +02001182
Tejun Heo52d9e672006-01-06 09:49:58 +01001183 req->errors = 0;
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001184 req->__sector = bio->bi_sector;
Tejun Heo52d9e672006-01-06 09:49:58 +01001185 req->ioprio = bio_prio(bio);
NeilBrownbc1c56f2007-08-16 13:31:30 +02001186 blk_rq_bio_prep(req->q, req, bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001187}
1188
Jens Axboe644b2d92009-04-06 14:48:06 +02001189/*
1190 * Only disabling plugging for non-rotational devices if it does tagging
1191 * as well, otherwise we do need the proper merging
1192 */
1193static inline bool queue_should_plug(struct request_queue *q)
1194{
Jens Axboe79da06442010-02-23 08:40:43 +01001195 return !(blk_queue_nonrot(q) && blk_queue_tagged(q));
Jens Axboe644b2d92009-04-06 14:48:06 +02001196}
1197
Jens Axboe165125e2007-07-24 09:28:11 +02001198static int __make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
Nick Piggin450991b2005-06-28 20:45:13 -07001200 struct request *req;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001201 int el_ret;
1202 unsigned int bytes = bio->bi_size;
Jens Axboe51da90f2006-07-18 04:14:45 +02001203 const unsigned short prio = bio_prio(bio);
Jiri Slaby5e00d1b2010-08-12 14:31:06 +02001204 const bool sync = !!(bio->bi_rw & REQ_SYNC);
1205 const bool unplug = !!(bio->bi_rw & REQ_UNPLUG);
1206 const unsigned long ff = bio->bi_rw & REQ_FAILFAST_MASK;
Tejun Heo28e7d182010-09-03 11:56:16 +02001207 int where = ELEVATOR_INSERT_SORT;
Jens Axboe7749a8d2006-12-13 13:02:26 +01001208 int rw_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 /*
1211 * low level driver can indicate that it wants pages above a
1212 * certain limit bounced to low memory (ie for highmem, or even
1213 * ISA dma in theory)
1214 */
1215 blk_queue_bounce(q, &bio);
1216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 spin_lock_irq(q->queue_lock);
1218
Tejun Heo4fed9472010-09-03 11:56:17 +02001219 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) {
Tejun Heoae1b1532011-01-25 12:43:54 +01001220 where = ELEVATOR_INSERT_FLUSH;
Tejun Heo28e7d182010-09-03 11:56:16 +02001221 goto get_rq;
1222 }
1223
1224 if (elv_queue_empty(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 goto get_rq;
1226
1227 el_ret = elv_merge(q, &req, bio);
1228 switch (el_ret) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001229 case ELEVATOR_BACK_MERGE:
1230 BUG_ON(!rq_mergeable(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Jens Axboe6728cb02008-01-31 13:03:55 +01001232 if (!ll_back_merge_fn(q, req, bio))
1233 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001235 trace_block_bio_backmerge(q, bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001236
Tejun Heo80a761f2009-07-03 17:48:17 +09001237 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1238 blk_rq_set_mixed_merge(req);
1239
Jens Axboe6728cb02008-01-31 13:03:55 +01001240 req->biotail->bi_next = bio;
1241 req->biotail = bio;
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001242 req->__data_len += bytes;
Jens Axboe6728cb02008-01-31 13:03:55 +01001243 req->ioprio = ioprio_best(req->ioprio, prio);
Jens Axboeab780f12008-08-26 10:25:02 +02001244 if (!blk_rq_cpu_valid(req))
1245 req->cpu = bio->bi_comp_cpu;
Jens Axboe6728cb02008-01-31 13:03:55 +01001246 drive_stat_acct(req, 0);
Divyesh Shah812d4022010-04-08 21:14:23 -07001247 elv_bio_merged(q, req, bio);
Jens Axboe6728cb02008-01-31 13:03:55 +01001248 if (!attempt_back_merge(q, req))
1249 elv_merged_request(q, req, el_ret);
1250 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Jens Axboe6728cb02008-01-31 13:03:55 +01001252 case ELEVATOR_FRONT_MERGE:
1253 BUG_ON(!rq_mergeable(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Jens Axboe6728cb02008-01-31 13:03:55 +01001255 if (!ll_front_merge_fn(q, req, bio))
1256 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001258 trace_block_bio_frontmerge(q, bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001259
Tejun Heo80a761f2009-07-03 17:48:17 +09001260 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff) {
1261 blk_rq_set_mixed_merge(req);
1262 req->cmd_flags &= ~REQ_FAILFAST_MASK;
1263 req->cmd_flags |= ff;
1264 }
1265
Jens Axboe6728cb02008-01-31 13:03:55 +01001266 bio->bi_next = req->bio;
1267 req->bio = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Jens Axboe6728cb02008-01-31 13:03:55 +01001269 /*
1270 * may not be valid. if the low level driver said
1271 * it didn't need a bounce buffer then it better
1272 * not touch req->buffer either...
1273 */
1274 req->buffer = bio_data(bio);
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001275 req->__sector = bio->bi_sector;
1276 req->__data_len += bytes;
Jens Axboe6728cb02008-01-31 13:03:55 +01001277 req->ioprio = ioprio_best(req->ioprio, prio);
Jens Axboeab780f12008-08-26 10:25:02 +02001278 if (!blk_rq_cpu_valid(req))
1279 req->cpu = bio->bi_comp_cpu;
Jens Axboe6728cb02008-01-31 13:03:55 +01001280 drive_stat_acct(req, 0);
Divyesh Shah812d4022010-04-08 21:14:23 -07001281 elv_bio_merged(q, req, bio);
Jens Axboe6728cb02008-01-31 13:03:55 +01001282 if (!attempt_front_merge(q, req))
1283 elv_merged_request(q, req, el_ret);
1284 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Jens Axboe6728cb02008-01-31 13:03:55 +01001286 /* ELV_NO_MERGE: elevator says don't/can't merge. */
1287 default:
1288 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
1290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291get_rq:
Nick Piggin450991b2005-06-28 20:45:13 -07001292 /*
Jens Axboe7749a8d2006-12-13 13:02:26 +01001293 * This sync check and mask will be re-done in init_request_from_bio(),
1294 * but we need to set it earlier to expose the sync flag to the
1295 * rq allocator and io schedulers.
1296 */
1297 rw_flags = bio_data_dir(bio);
1298 if (sync)
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001299 rw_flags |= REQ_SYNC;
Jens Axboe7749a8d2006-12-13 13:02:26 +01001300
1301 /*
Nick Piggin450991b2005-06-28 20:45:13 -07001302 * Grab a free request. This is might sleep but can not fail.
Nick Piggind6344532005-06-28 20:45:14 -07001303 * Returns with the queue unlocked.
Nick Piggin450991b2005-06-28 20:45:13 -07001304 */
Jens Axboe7749a8d2006-12-13 13:02:26 +01001305 req = get_request_wait(q, rw_flags, bio);
Nick Piggind6344532005-06-28 20:45:14 -07001306
Nick Piggin450991b2005-06-28 20:45:13 -07001307 /*
1308 * After dropping the lock and possibly sleeping here, our request
1309 * may now be mergeable after it had proven unmergeable (above).
1310 * We don't worry about that case for efficiency. It won't happen
1311 * often, and the elevators are able to handle it.
1312 */
Tejun Heo52d9e672006-01-06 09:49:58 +01001313 init_request_from_bio(req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Nick Piggin450991b2005-06-28 20:45:13 -07001315 spin_lock_irq(q->queue_lock);
Jens Axboec7c22e42008-09-13 20:26:01 +02001316 if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags) ||
1317 bio_flagged(bio, BIO_CPU_AFFINE))
1318 req->cpu = blk_cpu_to_group(smp_processor_id());
Jens Axboe644b2d92009-04-06 14:48:06 +02001319 if (queue_should_plug(q) && elv_queue_empty(q))
Nick Piggin450991b2005-06-28 20:45:13 -07001320 blk_plug_device(q);
Tejun Heodd831002010-09-03 11:56:16 +02001321
1322 /* insert the request into the elevator */
1323 drive_stat_acct(req, 1);
Tejun Heo28e7d182010-09-03 11:56:16 +02001324 __elv_add_request(q, req, where, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325out:
Jens Axboe644b2d92009-04-06 14:48:06 +02001326 if (unplug || !queue_should_plug(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 __generic_unplug_device(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 spin_unlock_irq(q->queue_lock);
1329 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330}
1331
1332/*
1333 * If bio->bi_dev is a partition, remap the location
1334 */
1335static inline void blk_partition_remap(struct bio *bio)
1336{
1337 struct block_device *bdev = bio->bi_bdev;
1338
Jens Axboebf2de6f2007-09-27 13:01:25 +02001339 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 struct hd_struct *p = bdev->bd_part;
Jens Axboea3623572005-11-01 09:26:16 +01001341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 bio->bi_sector += p->start_sect;
1343 bio->bi_bdev = bdev->bd_contains;
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001344
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001345 trace_block_remap(bdev_get_queue(bio->bi_bdev), bio,
Alan D. Brunelle22a7c312009-05-04 16:35:08 -04001346 bdev->bd_dev,
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001347 bio->bi_sector - p->start_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
1349}
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351static void handle_bad_sector(struct bio *bio)
1352{
1353 char b[BDEVNAME_SIZE];
1354
1355 printk(KERN_INFO "attempt to access beyond end of device\n");
1356 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1357 bdevname(bio->bi_bdev, b),
1358 bio->bi_rw,
1359 (unsigned long long)bio->bi_sector + bio_sectors(bio),
Mike Snitzer77304d22010-11-08 14:39:12 +01001360 (long long)(i_size_read(bio->bi_bdev->bd_inode) >> 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 set_bit(BIO_EOF, &bio->bi_flags);
1363}
1364
Akinobu Mitac17bb492006-12-08 02:39:46 -08001365#ifdef CONFIG_FAIL_MAKE_REQUEST
1366
1367static DECLARE_FAULT_ATTR(fail_make_request);
1368
1369static int __init setup_fail_make_request(char *str)
1370{
1371 return setup_fault_attr(&fail_make_request, str);
1372}
1373__setup("fail_make_request=", setup_fail_make_request);
1374
1375static int should_fail_request(struct bio *bio)
1376{
Tejun Heoeddb2e22008-08-25 19:56:13 +09001377 struct hd_struct *part = bio->bi_bdev->bd_part;
1378
1379 if (part_to_disk(part)->part0.make_it_fail || part->make_it_fail)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001380 return should_fail(&fail_make_request, bio->bi_size);
1381
1382 return 0;
1383}
1384
1385static int __init fail_make_request_debugfs(void)
1386{
1387 return init_fault_attr_dentries(&fail_make_request,
1388 "fail_make_request");
1389}
1390
1391late_initcall(fail_make_request_debugfs);
1392
1393#else /* CONFIG_FAIL_MAKE_REQUEST */
1394
1395static inline int should_fail_request(struct bio *bio)
1396{
1397 return 0;
1398}
1399
1400#endif /* CONFIG_FAIL_MAKE_REQUEST */
1401
Jens Axboec07e2b42007-07-18 13:27:58 +02001402/*
1403 * Check whether this bio extends beyond the end of the device.
1404 */
1405static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1406{
1407 sector_t maxsector;
1408
1409 if (!nr_sectors)
1410 return 0;
1411
1412 /* Test device or partition size, when known. */
Mike Snitzer77304d22010-11-08 14:39:12 +01001413 maxsector = i_size_read(bio->bi_bdev->bd_inode) >> 9;
Jens Axboec07e2b42007-07-18 13:27:58 +02001414 if (maxsector) {
1415 sector_t sector = bio->bi_sector;
1416
1417 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1418 /*
1419 * This may well happen - the kernel calls bread()
1420 * without checking the size of the device, e.g., when
1421 * mounting a device.
1422 */
1423 handle_bad_sector(bio);
1424 return 1;
1425 }
1426 }
1427
1428 return 0;
1429}
1430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001432 * generic_make_request - hand a buffer to its device driver for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 * @bio: The bio describing the location in memory and on the device.
1434 *
1435 * generic_make_request() is used to make I/O requests of block
1436 * devices. It is passed a &struct bio, which describes the I/O that needs
1437 * to be done.
1438 *
1439 * generic_make_request() does not return any status. The
1440 * success/failure status of the request, along with notification of
1441 * completion, is delivered asynchronously through the bio->bi_end_io
1442 * function described (one day) else where.
1443 *
1444 * The caller of generic_make_request must make sure that bi_io_vec
1445 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1446 * set to describe the device address, and the
1447 * bi_end_io and optionally bi_private are set to describe how
1448 * completion notification should be signaled.
1449 *
1450 * generic_make_request and the drivers it calls may use bi_next if this
1451 * bio happens to be merged with someone else, and may change bi_dev and
1452 * bi_sector for remaps as it sees fit. So the values of these fields
1453 * should NOT be depended on after the call to generic_make_request.
1454 */
Neil Brownd89d8792007-05-01 09:53:42 +02001455static inline void __generic_make_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
Jens Axboe165125e2007-07-24 09:28:11 +02001457 struct request_queue *q;
NeilBrown5ddfe962006-10-30 22:07:21 -08001458 sector_t old_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 int ret, nr_sectors = bio_sectors(bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001460 dev_t old_dev;
Jens Axboe51fd77b2007-11-02 08:49:08 +01001461 int err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Jens Axboec07e2b42007-07-18 13:27:58 +02001465 if (bio_check_eod(bio, nr_sectors))
1466 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 /*
1469 * Resolve the mapping until finished. (drivers are
1470 * still free to implement/resolve their own stacking
1471 * by explicitly returning 0)
1472 *
1473 * NOTE: we don't repeat the blk_size check for each new device.
1474 * Stacking drivers are expected to know what they are doing.
1475 */
NeilBrown5ddfe962006-10-30 22:07:21 -08001476 old_sector = -1;
Jens Axboe2056a782006-03-23 20:00:26 +01001477 old_dev = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 do {
1479 char b[BDEVNAME_SIZE];
1480
1481 q = bdev_get_queue(bio->bi_bdev);
Tejun Heoa7384672008-11-28 13:32:03 +09001482 if (unlikely(!q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 printk(KERN_ERR
1484 "generic_make_request: Trying to access "
1485 "nonexistent block-device %s (%Lu)\n",
1486 bdevname(bio->bi_bdev, b),
1487 (long long) bio->bi_sector);
Tejun Heoa7384672008-11-28 13:32:03 +09001488 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 }
1490
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001491 if (unlikely(!(bio->bi_rw & REQ_DISCARD) &&
Christoph Hellwig67efc922009-09-30 13:54:20 +02001492 nr_sectors > queue_max_hw_sectors(q))) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001493 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
Martin K. Petersenae03bf62009-05-22 17:17:50 -04001494 bdevname(bio->bi_bdev, b),
1495 bio_sectors(bio),
1496 queue_max_hw_sectors(q));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 goto end_io;
1498 }
1499
Nick Pigginfde6ad22005-06-23 00:08:53 -07001500 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 goto end_io;
1502
Akinobu Mitac17bb492006-12-08 02:39:46 -08001503 if (should_fail_request(bio))
1504 goto end_io;
1505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 /*
1507 * If this device has partitions, remap block n
1508 * of partition p to block n+start(p) of the disk.
1509 */
1510 blk_partition_remap(bio);
1511
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +02001512 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio))
1513 goto end_io;
1514
NeilBrown5ddfe962006-10-30 22:07:21 -08001515 if (old_sector != -1)
Alan D. Brunelle22a7c312009-05-04 16:35:08 -04001516 trace_block_remap(q, bio, old_dev, old_sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001517
NeilBrown5ddfe962006-10-30 22:07:21 -08001518 old_sector = bio->bi_sector;
Jens Axboe2056a782006-03-23 20:00:26 +01001519 old_dev = bio->bi_bdev->bd_dev;
1520
Jens Axboec07e2b42007-07-18 13:27:58 +02001521 if (bio_check_eod(bio, nr_sectors))
1522 goto end_io;
Tejun Heoa7384672008-11-28 13:32:03 +09001523
Tejun Heo1e879012010-09-03 11:56:17 +02001524 /*
1525 * Filter flush bio's early so that make_request based
1526 * drivers without flush support don't have to worry
1527 * about them.
1528 */
1529 if ((bio->bi_rw & (REQ_FLUSH | REQ_FUA)) && !q->flush_flags) {
1530 bio->bi_rw &= ~(REQ_FLUSH | REQ_FUA);
1531 if (!nr_sectors) {
1532 err = 0;
1533 goto end_io;
1534 }
1535 }
1536
Adrian Hunter8d57a982010-08-11 14:17:49 -07001537 if ((bio->bi_rw & REQ_DISCARD) &&
1538 (!blk_queue_discard(q) ||
1539 ((bio->bi_rw & REQ_SECURE) &&
1540 !blk_queue_secdiscard(q)))) {
Jens Axboe51fd77b2007-11-02 08:49:08 +01001541 err = -EOPNOTSUPP;
1542 goto end_io;
1543 }
NeilBrown5ddfe962006-10-30 22:07:21 -08001544
Vivek Goyale43473b2010-09-15 17:06:35 -04001545 blk_throtl_bio(q, &bio);
1546
1547 /*
1548 * If bio = NULL, bio has been throttled and will be submitted
1549 * later.
1550 */
1551 if (!bio)
1552 break;
1553
Minchan Kim01edede2009-09-08 21:56:38 +02001554 trace_block_bio_queue(q, bio);
1555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 ret = q->make_request_fn(q, bio);
1557 } while (ret);
Tejun Heoa7384672008-11-28 13:32:03 +09001558
1559 return;
1560
1561end_io:
1562 bio_endio(bio, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563}
1564
Neil Brownd89d8792007-05-01 09:53:42 +02001565/*
1566 * We only want one ->make_request_fn to be active at a time,
1567 * else stack usage with stacked devices could be a problem.
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001568 * So use current->bio_list to keep a list of requests
Neil Brownd89d8792007-05-01 09:53:42 +02001569 * submited by a make_request_fn function.
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001570 * current->bio_list is also used as a flag to say if
Neil Brownd89d8792007-05-01 09:53:42 +02001571 * generic_make_request is currently active in this task or not.
1572 * If it is NULL, then no make_request is active. If it is non-NULL,
1573 * then a make_request is active, and new requests should be added
1574 * at the tail
1575 */
1576void generic_make_request(struct bio *bio)
1577{
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001578 struct bio_list bio_list_on_stack;
1579
1580 if (current->bio_list) {
Neil Brownd89d8792007-05-01 09:53:42 +02001581 /* make_request is active */
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001582 bio_list_add(current->bio_list, bio);
Neil Brownd89d8792007-05-01 09:53:42 +02001583 return;
1584 }
1585 /* following loop may be a bit non-obvious, and so deserves some
1586 * explanation.
1587 * Before entering the loop, bio->bi_next is NULL (as all callers
1588 * ensure that) so we have a list with a single bio.
1589 * We pretend that we have just taken it off a longer list, so
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001590 * we assign bio_list to a pointer to the bio_list_on_stack,
1591 * thus initialising the bio_list of new bios to be
Neil Brownd89d8792007-05-01 09:53:42 +02001592 * added. __generic_make_request may indeed add some more bios
1593 * through a recursive call to generic_make_request. If it
1594 * did, we find a non-NULL value in bio_list and re-enter the loop
1595 * from the top. In this case we really did just take the bio
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001596 * of the top of the list (no pretending) and so remove it from
1597 * bio_list, and call into __generic_make_request again.
Neil Brownd89d8792007-05-01 09:53:42 +02001598 *
1599 * The loop was structured like this to make only one call to
1600 * __generic_make_request (which is important as it is large and
1601 * inlined) and to keep the structure simple.
1602 */
1603 BUG_ON(bio->bi_next);
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001604 bio_list_init(&bio_list_on_stack);
1605 current->bio_list = &bio_list_on_stack;
Neil Brownd89d8792007-05-01 09:53:42 +02001606 do {
Neil Brownd89d8792007-05-01 09:53:42 +02001607 __generic_make_request(bio);
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001608 bio = bio_list_pop(current->bio_list);
Neil Brownd89d8792007-05-01 09:53:42 +02001609 } while (bio);
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001610 current->bio_list = NULL; /* deactivate */
Neil Brownd89d8792007-05-01 09:53:42 +02001611}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612EXPORT_SYMBOL(generic_make_request);
1613
1614/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001615 * submit_bio - submit a bio to the block device layer for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1617 * @bio: The &struct bio which describes the I/O
1618 *
1619 * submit_bio() is very similar in purpose to generic_make_request(), and
1620 * uses that function to do most of the work. Both are fairly rough
Randy Dunlap710027a2008-08-19 20:13:11 +02001621 * interfaces; @bio must be presetup and ready for I/O.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 *
1623 */
1624void submit_bio(int rw, struct bio *bio)
1625{
1626 int count = bio_sectors(bio);
1627
Jens Axboe22e2c502005-06-27 10:55:12 +02001628 bio->bi_rw |= rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Jens Axboebf2de6f2007-09-27 13:01:25 +02001630 /*
1631 * If it's a regular read/write or a barrier with data attached,
1632 * go through the normal accounting stuff before submission.
1633 */
Jens Axboe3ffb52e2010-06-29 13:33:38 +02001634 if (bio_has_data(bio) && !(rw & REQ_DISCARD)) {
Jens Axboebf2de6f2007-09-27 13:01:25 +02001635 if (rw & WRITE) {
1636 count_vm_events(PGPGOUT, count);
1637 } else {
1638 task_io_account_read(bio->bi_size);
1639 count_vm_events(PGPGIN, count);
1640 }
1641
1642 if (unlikely(block_dump)) {
1643 char b[BDEVNAME_SIZE];
San Mehat8dcbdc72010-09-14 08:48:01 +02001644 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s (%u sectors)\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001645 current->comm, task_pid_nr(current),
Jens Axboebf2de6f2007-09-27 13:01:25 +02001646 (rw & WRITE) ? "WRITE" : "READ",
1647 (unsigned long long)bio->bi_sector,
San Mehat8dcbdc72010-09-14 08:48:01 +02001648 bdevname(bio->bi_bdev, b),
1649 count);
Jens Axboebf2de6f2007-09-27 13:01:25 +02001650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 }
1652
1653 generic_make_request(bio);
1654}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655EXPORT_SYMBOL(submit_bio);
1656
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001657/**
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001658 * blk_rq_check_limits - Helper function to check a request for the queue limit
1659 * @q: the queue
1660 * @rq: the request being checked
1661 *
1662 * Description:
1663 * @rq may have been made based on weaker limitations of upper-level queues
1664 * in request stacking drivers, and it may violate the limitation of @q.
1665 * Since the block layer and the underlying device driver trust @rq
1666 * after it is inserted to @q, it should be checked against @q before
1667 * the insertion using this generic function.
1668 *
1669 * This function should also be useful for request stacking drivers
Stefan Weileef35c22010-08-06 21:11:15 +02001670 * in some cases below, so export this function.
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001671 * Request stacking drivers like request-based dm may change the queue
1672 * limits while requests are in the queue (e.g. dm's table swapping).
1673 * Such request stacking drivers should check those requests agaist
1674 * the new queue limits again when they dispatch those requests,
1675 * although such checkings are also done against the old queue limits
1676 * when submitting requests.
1677 */
1678int blk_rq_check_limits(struct request_queue *q, struct request *rq)
1679{
ike Snitzer33839772010-08-08 12:11:33 -04001680 if (rq->cmd_flags & REQ_DISCARD)
1681 return 0;
1682
Martin K. Petersenae03bf62009-05-22 17:17:50 -04001683 if (blk_rq_sectors(rq) > queue_max_sectors(q) ||
1684 blk_rq_bytes(rq) > queue_max_hw_sectors(q) << 9) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001685 printk(KERN_ERR "%s: over max size limit.\n", __func__);
1686 return -EIO;
1687 }
1688
1689 /*
1690 * queue's settings related to segment counting like q->bounce_pfn
1691 * may differ from that of other stacking queues.
1692 * Recalculate it to check the request correctly on this queue's
1693 * limitation.
1694 */
1695 blk_recalc_rq_segments(rq);
Martin K. Petersen8a783622010-02-26 00:20:39 -05001696 if (rq->nr_phys_segments > queue_max_segments(q)) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001697 printk(KERN_ERR "%s: over max segments limit.\n", __func__);
1698 return -EIO;
1699 }
1700
1701 return 0;
1702}
1703EXPORT_SYMBOL_GPL(blk_rq_check_limits);
1704
1705/**
1706 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
1707 * @q: the queue to submit the request
1708 * @rq: the request being queued
1709 */
1710int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
1711{
1712 unsigned long flags;
1713
1714 if (blk_rq_check_limits(q, rq))
1715 return -EIO;
1716
1717#ifdef CONFIG_FAIL_MAKE_REQUEST
1718 if (rq->rq_disk && rq->rq_disk->part0.make_it_fail &&
1719 should_fail(&fail_make_request, blk_rq_bytes(rq)))
1720 return -EIO;
1721#endif
1722
1723 spin_lock_irqsave(q->queue_lock, flags);
1724
1725 /*
1726 * Submitting request must be dequeued before calling this function
1727 * because it will be linked to another request_queue
1728 */
1729 BUG_ON(blk_queued_rq(rq));
1730
1731 drive_stat_acct(rq, 1);
1732 __elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 0);
1733
1734 spin_unlock_irqrestore(q->queue_lock, flags);
1735
1736 return 0;
1737}
1738EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
1739
Tejun Heo80a761f2009-07-03 17:48:17 +09001740/**
1741 * blk_rq_err_bytes - determine number of bytes till the next failure boundary
1742 * @rq: request to examine
1743 *
1744 * Description:
1745 * A request could be merge of IOs which require different failure
1746 * handling. This function determines the number of bytes which
1747 * can be failed from the beginning of the request without
1748 * crossing into area which need to be retried further.
1749 *
1750 * Return:
1751 * The number of bytes to fail.
1752 *
1753 * Context:
1754 * queue_lock must be held.
1755 */
1756unsigned int blk_rq_err_bytes(const struct request *rq)
1757{
1758 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
1759 unsigned int bytes = 0;
1760 struct bio *bio;
1761
1762 if (!(rq->cmd_flags & REQ_MIXED_MERGE))
1763 return blk_rq_bytes(rq);
1764
1765 /*
1766 * Currently the only 'mixing' which can happen is between
1767 * different fastfail types. We can safely fail portions
1768 * which have all the failfast bits that the first one has -
1769 * the ones which are at least as eager to fail as the first
1770 * one.
1771 */
1772 for (bio = rq->bio; bio; bio = bio->bi_next) {
1773 if ((bio->bi_rw & ff) != ff)
1774 break;
1775 bytes += bio->bi_size;
1776 }
1777
1778 /* this could lead to infinite loop */
1779 BUG_ON(blk_rq_bytes(rq) && !bytes);
1780 return bytes;
1781}
1782EXPORT_SYMBOL_GPL(blk_rq_err_bytes);
1783
Jens Axboebc58ba92009-01-23 10:54:44 +01001784static void blk_account_io_completion(struct request *req, unsigned int bytes)
1785{
Jens Axboec2553b52009-04-24 08:10:11 +02001786 if (blk_do_io_stat(req)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01001787 const int rw = rq_data_dir(req);
1788 struct hd_struct *part;
1789 int cpu;
1790
1791 cpu = part_stat_lock();
Jens Axboef253b862010-10-24 22:06:02 +02001792 part = disk_map_sector_rcu(req->rq_disk, blk_rq_pos(req));
Jens Axboebc58ba92009-01-23 10:54:44 +01001793 part_stat_add(cpu, part, sectors[rw], bytes >> 9);
1794 part_stat_unlock();
1795 }
1796}
1797
1798static void blk_account_io_done(struct request *req)
1799{
Jens Axboebc58ba92009-01-23 10:54:44 +01001800 /*
Tejun Heodd4c1332010-09-03 11:56:16 +02001801 * Account IO completion. flush_rq isn't accounted as a
1802 * normal IO on queueing nor completion. Accounting the
1803 * containing request is enough.
Jens Axboebc58ba92009-01-23 10:54:44 +01001804 */
Tejun Heo414b4ff2011-01-25 12:43:49 +01001805 if (blk_do_io_stat(req) && !(req->cmd_flags & REQ_FLUSH_SEQ)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01001806 unsigned long duration = jiffies - req->start_time;
1807 const int rw = rq_data_dir(req);
1808 struct hd_struct *part;
1809 int cpu;
1810
1811 cpu = part_stat_lock();
Jens Axboef253b862010-10-24 22:06:02 +02001812 part = disk_map_sector_rcu(req->rq_disk, blk_rq_pos(req));
Jens Axboebc58ba92009-01-23 10:54:44 +01001813
1814 part_stat_inc(cpu, part, ios[rw]);
1815 part_stat_add(cpu, part, ticks[rw], duration);
1816 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001817 part_dec_in_flight(part, rw);
Jens Axboebc58ba92009-01-23 10:54:44 +01001818
1819 part_stat_unlock();
1820 }
1821}
1822
Tejun Heo53a08802008-12-03 12:41:26 +01001823/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09001824 * blk_peek_request - peek at the top of a request queue
1825 * @q: request queue to peek at
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001826 *
1827 * Description:
Tejun Heo9934c8c2009-05-08 11:54:16 +09001828 * Return the request at the top of @q. The returned request
1829 * should be started using blk_start_request() before LLD starts
1830 * processing it.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001831 *
1832 * Return:
Tejun Heo9934c8c2009-05-08 11:54:16 +09001833 * Pointer to the request at the top of @q if available. Null
1834 * otherwise.
1835 *
1836 * Context:
1837 * queue_lock must be held.
1838 */
1839struct request *blk_peek_request(struct request_queue *q)
Tejun Heo158dbda2009-04-23 11:05:18 +09001840{
1841 struct request *rq;
1842 int ret;
1843
1844 while ((rq = __elv_next_request(q)) != NULL) {
1845 if (!(rq->cmd_flags & REQ_STARTED)) {
1846 /*
1847 * This is the first time the device driver
1848 * sees this request (possibly after
1849 * requeueing). Notify IO scheduler.
1850 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001851 if (rq->cmd_flags & REQ_SORTED)
Tejun Heo158dbda2009-04-23 11:05:18 +09001852 elv_activate_rq(q, rq);
1853
1854 /*
1855 * just mark as started even if we don't start
1856 * it, a request that has been delayed should
1857 * not be passed by new incoming requests
1858 */
1859 rq->cmd_flags |= REQ_STARTED;
1860 trace_block_rq_issue(q, rq);
1861 }
1862
1863 if (!q->boundary_rq || q->boundary_rq == rq) {
1864 q->end_sector = rq_end_sector(rq);
1865 q->boundary_rq = NULL;
1866 }
1867
1868 if (rq->cmd_flags & REQ_DONTPREP)
1869 break;
1870
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001871 if (q->dma_drain_size && blk_rq_bytes(rq)) {
Tejun Heo158dbda2009-04-23 11:05:18 +09001872 /*
1873 * make sure space for the drain appears we
1874 * know we can do this because max_hw_segments
1875 * has been adjusted to be one fewer than the
1876 * device can handle
1877 */
1878 rq->nr_phys_segments++;
1879 }
1880
1881 if (!q->prep_rq_fn)
1882 break;
1883
1884 ret = q->prep_rq_fn(q, rq);
1885 if (ret == BLKPREP_OK) {
1886 break;
1887 } else if (ret == BLKPREP_DEFER) {
1888 /*
1889 * the request may have been (partially) prepped.
1890 * we need to keep this request in the front to
1891 * avoid resource deadlock. REQ_STARTED will
1892 * prevent other fs requests from passing this one.
1893 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001894 if (q->dma_drain_size && blk_rq_bytes(rq) &&
Tejun Heo158dbda2009-04-23 11:05:18 +09001895 !(rq->cmd_flags & REQ_DONTPREP)) {
1896 /*
1897 * remove the space for the drain we added
1898 * so that we don't add it again
1899 */
1900 --rq->nr_phys_segments;
1901 }
1902
1903 rq = NULL;
1904 break;
1905 } else if (ret == BLKPREP_KILL) {
1906 rq->cmd_flags |= REQ_QUIET;
James Bottomleyc143dc92009-05-30 06:43:49 +02001907 /*
1908 * Mark this request as started so we don't trigger
1909 * any debug logic in the end I/O path.
1910 */
1911 blk_start_request(rq);
Tejun Heo40cbbb72009-04-23 11:05:19 +09001912 __blk_end_request_all(rq, -EIO);
Tejun Heo158dbda2009-04-23 11:05:18 +09001913 } else {
1914 printk(KERN_ERR "%s: bad return=%d\n", __func__, ret);
1915 break;
1916 }
1917 }
1918
1919 return rq;
1920}
Tejun Heo9934c8c2009-05-08 11:54:16 +09001921EXPORT_SYMBOL(blk_peek_request);
Tejun Heo158dbda2009-04-23 11:05:18 +09001922
Tejun Heo9934c8c2009-05-08 11:54:16 +09001923void blk_dequeue_request(struct request *rq)
Tejun Heo158dbda2009-04-23 11:05:18 +09001924{
Tejun Heo9934c8c2009-05-08 11:54:16 +09001925 struct request_queue *q = rq->q;
1926
Tejun Heo158dbda2009-04-23 11:05:18 +09001927 BUG_ON(list_empty(&rq->queuelist));
1928 BUG_ON(ELV_ON_HASH(rq));
1929
1930 list_del_init(&rq->queuelist);
1931
1932 /*
1933 * the time frame between a request being removed from the lists
1934 * and to it is freed is accounted as io that is in progress at
1935 * the driver side.
1936 */
Divyesh Shah91952912010-04-01 15:01:41 -07001937 if (blk_account_rq(rq)) {
Jens Axboe0a7ae2f2009-05-20 08:54:31 +02001938 q->in_flight[rq_is_sync(rq)]++;
Divyesh Shah91952912010-04-01 15:01:41 -07001939 set_io_start_time_ns(rq);
1940 }
Tejun Heo158dbda2009-04-23 11:05:18 +09001941}
1942
Tejun Heo5efccd12009-04-23 11:05:18 +09001943/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09001944 * blk_start_request - start request processing on the driver
1945 * @req: request to dequeue
1946 *
1947 * Description:
1948 * Dequeue @req and start timeout timer on it. This hands off the
1949 * request to the driver.
1950 *
1951 * Block internal functions which don't want to start timer should
1952 * call blk_dequeue_request().
1953 *
1954 * Context:
1955 * queue_lock must be held.
1956 */
1957void blk_start_request(struct request *req)
1958{
1959 blk_dequeue_request(req);
1960
1961 /*
Tejun Heo5f49f632009-05-19 18:33:05 +09001962 * We are now handing the request to the hardware, initialize
1963 * resid_len to full count and add the timeout handler.
Tejun Heo9934c8c2009-05-08 11:54:16 +09001964 */
Tejun Heo5f49f632009-05-19 18:33:05 +09001965 req->resid_len = blk_rq_bytes(req);
FUJITA Tomonoridbb66c42009-06-09 05:47:10 +02001966 if (unlikely(blk_bidi_rq(req)))
1967 req->next_rq->resid_len = blk_rq_bytes(req->next_rq);
1968
Tejun Heo9934c8c2009-05-08 11:54:16 +09001969 blk_add_timer(req);
1970}
1971EXPORT_SYMBOL(blk_start_request);
1972
1973/**
1974 * blk_fetch_request - fetch a request from a request queue
1975 * @q: request queue to fetch a request from
1976 *
1977 * Description:
1978 * Return the request at the top of @q. The request is started on
1979 * return and LLD can start processing it immediately.
1980 *
1981 * Return:
1982 * Pointer to the request at the top of @q if available. Null
1983 * otherwise.
1984 *
1985 * Context:
1986 * queue_lock must be held.
1987 */
1988struct request *blk_fetch_request(struct request_queue *q)
1989{
1990 struct request *rq;
1991
1992 rq = blk_peek_request(q);
1993 if (rq)
1994 blk_start_request(rq);
1995 return rq;
1996}
1997EXPORT_SYMBOL(blk_fetch_request);
1998
1999/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002000 * blk_update_request - Special helper function for request stacking drivers
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002001 * @req: the request being processed
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002002 * @error: %0 for success, < %0 for error
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002003 * @nr_bytes: number of bytes to complete @req
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002004 *
2005 * Description:
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002006 * Ends I/O on a number of bytes attached to @req, but doesn't complete
2007 * the request structure even if @req doesn't have leftover.
2008 * If @req has leftover, sets it up for the next range of segments.
Tejun Heo2e60e022009-04-23 11:05:18 +09002009 *
2010 * This special helper function is only for request stacking drivers
2011 * (e.g. request-based dm) so that they can handle partial completion.
2012 * Actual device drivers should use blk_end_request instead.
2013 *
2014 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
2015 * %false return from this function.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002016 *
2017 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002018 * %false - this request doesn't have any more data
2019 * %true - this request has more data
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002020 **/
Tejun Heo2e60e022009-04-23 11:05:18 +09002021bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05002023 int total_bytes, bio_nbytes, next_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 struct bio *bio;
2025
Tejun Heo2e60e022009-04-23 11:05:18 +09002026 if (!req->bio)
2027 return false;
2028
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01002029 trace_block_rq_complete(req->q, req);
Jens Axboe2056a782006-03-23 20:00:26 +01002030
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 /*
Tejun Heo6f414692009-04-19 07:00:41 +09002032 * For fs requests, rq is just carrier of independent bio's
2033 * and each partial completion should be handled separately.
2034 * Reset per-request error on each partial completion.
2035 *
2036 * TODO: tj: This is too subtle. It would be better to let
2037 * low level drivers do what they see fit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002039 if (req->cmd_type == REQ_TYPE_FS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 req->errors = 0;
2041
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002042 if (error && req->cmd_type == REQ_TYPE_FS &&
2043 !(req->cmd_flags & REQ_QUIET)) {
Jens Axboe6728cb02008-01-31 13:03:55 +01002044 printk(KERN_ERR "end_request: I/O error, dev %s, sector %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 req->rq_disk ? req->rq_disk->disk_name : "?",
Tejun Heo83096eb2009-05-07 22:24:39 +09002046 (unsigned long long)blk_rq_pos(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 }
2048
Jens Axboebc58ba92009-01-23 10:54:44 +01002049 blk_account_io_completion(req, nr_bytes);
Jens Axboed72d9042005-11-01 08:35:42 +01002050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 total_bytes = bio_nbytes = 0;
2052 while ((bio = req->bio) != NULL) {
2053 int nbytes;
2054
2055 if (nr_bytes >= bio->bi_size) {
2056 req->bio = bio->bi_next;
2057 nbytes = bio->bi_size;
NeilBrown5bb23a62007-09-27 12:46:13 +02002058 req_bio_endio(req, bio, nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 next_idx = 0;
2060 bio_nbytes = 0;
2061 } else {
2062 int idx = bio->bi_idx + next_idx;
2063
Kazuhisa Ichikawaaf498d72009-05-12 13:27:45 +02002064 if (unlikely(idx >= bio->bi_vcnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 blk_dump_rq_flags(req, "__end_that");
Jens Axboe6728cb02008-01-31 13:03:55 +01002066 printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
Kazuhisa Ichikawaaf498d72009-05-12 13:27:45 +02002067 __func__, idx, bio->bi_vcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 break;
2069 }
2070
2071 nbytes = bio_iovec_idx(bio, idx)->bv_len;
2072 BIO_BUG_ON(nbytes > bio->bi_size);
2073
2074 /*
2075 * not a complete bvec done
2076 */
2077 if (unlikely(nbytes > nr_bytes)) {
2078 bio_nbytes += nr_bytes;
2079 total_bytes += nr_bytes;
2080 break;
2081 }
2082
2083 /*
2084 * advance to the next vector
2085 */
2086 next_idx++;
2087 bio_nbytes += nbytes;
2088 }
2089
2090 total_bytes += nbytes;
2091 nr_bytes -= nbytes;
2092
Jens Axboe6728cb02008-01-31 13:03:55 +01002093 bio = req->bio;
2094 if (bio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 /*
2096 * end more in this run, or just return 'not-done'
2097 */
2098 if (unlikely(nr_bytes <= 0))
2099 break;
2100 }
2101 }
2102
2103 /*
2104 * completely done
2105 */
Tejun Heo2e60e022009-04-23 11:05:18 +09002106 if (!req->bio) {
2107 /*
2108 * Reset counters so that the request stacking driver
2109 * can find how many bytes remain in the request
2110 * later.
2111 */
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002112 req->__data_len = 0;
Tejun Heo2e60e022009-04-23 11:05:18 +09002113 return false;
2114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
2116 /*
2117 * if the request wasn't completed, update state
2118 */
2119 if (bio_nbytes) {
NeilBrown5bb23a62007-09-27 12:46:13 +02002120 req_bio_endio(req, bio, bio_nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 bio->bi_idx += next_idx;
2122 bio_iovec(bio)->bv_offset += nr_bytes;
2123 bio_iovec(bio)->bv_len -= nr_bytes;
2124 }
2125
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002126 req->__data_len -= total_bytes;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002127 req->buffer = bio_data(req->bio);
2128
2129 /* update sector only for requests with clear definition of sector */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002130 if (req->cmd_type == REQ_TYPE_FS || (req->cmd_flags & REQ_DISCARD))
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002131 req->__sector += total_bytes >> 9;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002132
Tejun Heo80a761f2009-07-03 17:48:17 +09002133 /* mixed attributes always follow the first bio */
2134 if (req->cmd_flags & REQ_MIXED_MERGE) {
2135 req->cmd_flags &= ~REQ_FAILFAST_MASK;
2136 req->cmd_flags |= req->bio->bi_rw & REQ_FAILFAST_MASK;
2137 }
2138
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002139 /*
2140 * If total number of sectors is less than the first segment
2141 * size, something has gone terribly wrong.
2142 */
2143 if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
2144 printk(KERN_ERR "blk: request botched\n");
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002145 req->__data_len = blk_rq_cur_bytes(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002146 }
2147
2148 /* recalculate the number of segments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 blk_recalc_rq_segments(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002150
Tejun Heo2e60e022009-04-23 11:05:18 +09002151 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152}
Tejun Heo2e60e022009-04-23 11:05:18 +09002153EXPORT_SYMBOL_GPL(blk_update_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Tejun Heo2e60e022009-04-23 11:05:18 +09002155static bool blk_update_bidi_request(struct request *rq, int error,
2156 unsigned int nr_bytes,
2157 unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002158{
Tejun Heo2e60e022009-04-23 11:05:18 +09002159 if (blk_update_request(rq, error, nr_bytes))
2160 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002161
Tejun Heo2e60e022009-04-23 11:05:18 +09002162 /* Bidi request must be completed as a whole */
2163 if (unlikely(blk_bidi_rq(rq)) &&
2164 blk_update_request(rq->next_rq, error, bidi_bytes))
2165 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002166
Jens Axboee2e1a142010-06-09 10:42:09 +02002167 if (blk_queue_add_random(rq->q))
2168 add_disk_randomness(rq->rq_disk);
Tejun Heo2e60e022009-04-23 11:05:18 +09002169
2170 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171}
2172
James Bottomley28018c22010-07-01 19:49:17 +09002173/**
2174 * blk_unprep_request - unprepare a request
2175 * @req: the request
2176 *
2177 * This function makes a request ready for complete resubmission (or
2178 * completion). It happens only after all error handling is complete,
2179 * so represents the appropriate moment to deallocate any resources
2180 * that were allocated to the request in the prep_rq_fn. The queue
2181 * lock is held when calling this.
2182 */
2183void blk_unprep_request(struct request *req)
2184{
2185 struct request_queue *q = req->q;
2186
2187 req->cmd_flags &= ~REQ_DONTPREP;
2188 if (q->unprep_rq_fn)
2189 q->unprep_rq_fn(q, req);
2190}
2191EXPORT_SYMBOL_GPL(blk_unprep_request);
2192
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193/*
2194 * queue lock must be held
2195 */
Tejun Heo2e60e022009-04-23 11:05:18 +09002196static void blk_finish_request(struct request *req, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002198 if (blk_rq_tagged(req))
2199 blk_queue_end_tag(req->q, req);
2200
James Bottomleyba396a62009-05-27 14:17:08 +02002201 BUG_ON(blk_queued_rq(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002203 if (unlikely(laptop_mode) && req->cmd_type == REQ_TYPE_FS)
Matthew Garrett31373d02010-04-06 14:25:14 +02002204 laptop_io_completion(&req->q->backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Mike Andersone78042e2008-10-30 02:16:20 -07002206 blk_delete_timer(req);
2207
James Bottomley28018c22010-07-01 19:49:17 +09002208 if (req->cmd_flags & REQ_DONTPREP)
2209 blk_unprep_request(req);
2210
2211
Jens Axboebc58ba92009-01-23 10:54:44 +01002212 blk_account_io_done(req);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002213
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 if (req->end_io)
Tejun Heo8ffdc652006-01-06 09:49:03 +01002215 req->end_io(req, error);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002216 else {
2217 if (blk_bidi_rq(req))
2218 __blk_put_request(req->next_rq->q, req->next_rq);
2219
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 __blk_put_request(req->q, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 }
2222}
2223
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05002224/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002225 * blk_end_bidi_request - Complete a bidi request
2226 * @rq: the request to complete
Randy Dunlap710027a2008-08-19 20:13:11 +02002227 * @error: %0 for success, < %0 for error
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002228 * @nr_bytes: number of bytes to complete @rq
2229 * @bidi_bytes: number of bytes to complete @rq->next_rq
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002230 *
2231 * Description:
2232 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
Tejun Heo2e60e022009-04-23 11:05:18 +09002233 * Drivers that supports bidi can safely call this member for any
2234 * type of request, bidi or uni. In the later case @bidi_bytes is
2235 * just ignored.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002236 *
2237 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002238 * %false - we are done with this request
2239 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002240 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002241static bool blk_end_bidi_request(struct request *rq, int error,
2242 unsigned int nr_bytes, unsigned int bidi_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002243{
2244 struct request_queue *q = rq->q;
Tejun Heo2e60e022009-04-23 11:05:18 +09002245 unsigned long flags;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002246
Tejun Heo2e60e022009-04-23 11:05:18 +09002247 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2248 return true;
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002249
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002250 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo2e60e022009-04-23 11:05:18 +09002251 blk_finish_request(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002252 spin_unlock_irqrestore(q->queue_lock, flags);
2253
Tejun Heo2e60e022009-04-23 11:05:18 +09002254 return false;
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002255}
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002256
2257/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002258 * __blk_end_bidi_request - Complete a bidi request with queue lock held
2259 * @rq: the request to complete
2260 * @error: %0 for success, < %0 for error
2261 * @nr_bytes: number of bytes to complete @rq
2262 * @bidi_bytes: number of bytes to complete @rq->next_rq
Tejun Heo5efccd12009-04-23 11:05:18 +09002263 *
2264 * Description:
Tejun Heo2e60e022009-04-23 11:05:18 +09002265 * Identical to blk_end_bidi_request() except that queue lock is
2266 * assumed to be locked on entry and remains so on return.
Tejun Heo5efccd12009-04-23 11:05:18 +09002267 *
Tejun Heo2e60e022009-04-23 11:05:18 +09002268 * Return:
2269 * %false - we are done with this request
2270 * %true - still buffers pending for this request
Tejun Heo5efccd12009-04-23 11:05:18 +09002271 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002272static bool __blk_end_bidi_request(struct request *rq, int error,
2273 unsigned int nr_bytes, unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002274{
Tejun Heo2e60e022009-04-23 11:05:18 +09002275 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2276 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002277
Tejun Heo2e60e022009-04-23 11:05:18 +09002278 blk_finish_request(rq, error);
Tejun Heo5efccd12009-04-23 11:05:18 +09002279
Tejun Heo2e60e022009-04-23 11:05:18 +09002280 return false;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002281}
2282
2283/**
2284 * blk_end_request - Helper function for drivers to complete the request.
2285 * @rq: the request being processed
2286 * @error: %0 for success, < %0 for error
2287 * @nr_bytes: number of bytes to complete
2288 *
2289 * Description:
2290 * Ends I/O on a number of bytes attached to @rq.
2291 * If @rq has leftover, sets it up for the next range of segments.
2292 *
2293 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002294 * %false - we are done with this request
2295 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002296 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002297bool blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002298{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002299 return blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002300}
Jens Axboe56ad1742009-07-28 22:11:24 +02002301EXPORT_SYMBOL(blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002302
2303/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002304 * blk_end_request_all - Helper function for drives to finish the request.
2305 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002306 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002307 *
2308 * Description:
2309 * Completely finish @rq.
2310 */
2311void blk_end_request_all(struct request *rq, int error)
2312{
2313 bool pending;
2314 unsigned int bidi_bytes = 0;
2315
2316 if (unlikely(blk_bidi_rq(rq)))
2317 bidi_bytes = blk_rq_bytes(rq->next_rq);
2318
2319 pending = blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2320 BUG_ON(pending);
2321}
Jens Axboe56ad1742009-07-28 22:11:24 +02002322EXPORT_SYMBOL(blk_end_request_all);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002323
2324/**
2325 * blk_end_request_cur - Helper function to finish the current request chunk.
2326 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002327 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002328 *
2329 * Description:
2330 * Complete the current consecutively mapped chunk from @rq.
2331 *
2332 * Return:
2333 * %false - we are done with this request
2334 * %true - still buffers pending for this request
2335 */
2336bool blk_end_request_cur(struct request *rq, int error)
2337{
2338 return blk_end_request(rq, error, blk_rq_cur_bytes(rq));
2339}
Jens Axboe56ad1742009-07-28 22:11:24 +02002340EXPORT_SYMBOL(blk_end_request_cur);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002341
2342/**
Tejun Heo80a761f2009-07-03 17:48:17 +09002343 * blk_end_request_err - Finish a request till the next failure boundary.
2344 * @rq: the request to finish till the next failure boundary for
2345 * @error: must be negative errno
2346 *
2347 * Description:
2348 * Complete @rq till the next failure boundary.
2349 *
2350 * Return:
2351 * %false - we are done with this request
2352 * %true - still buffers pending for this request
2353 */
2354bool blk_end_request_err(struct request *rq, int error)
2355{
2356 WARN_ON(error >= 0);
2357 return blk_end_request(rq, error, blk_rq_err_bytes(rq));
2358}
2359EXPORT_SYMBOL_GPL(blk_end_request_err);
2360
2361/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002362 * __blk_end_request - Helper function for drivers to complete the request.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002363 * @rq: the request being processed
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002364 * @error: %0 for success, < %0 for error
2365 * @nr_bytes: number of bytes to complete
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002366 *
2367 * Description:
2368 * Must be called with queue lock held unlike blk_end_request().
2369 *
2370 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002371 * %false - we are done with this request
2372 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002373 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002374bool __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002375{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002376 return __blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002377}
Jens Axboe56ad1742009-07-28 22:11:24 +02002378EXPORT_SYMBOL(__blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002379
2380/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002381 * __blk_end_request_all - Helper function for drives to finish the request.
2382 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002383 * @error: %0 for success, < %0 for error
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002384 *
2385 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002386 * Completely finish @rq. Must be called with queue lock held.
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002387 */
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002388void __blk_end_request_all(struct request *rq, int error)
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002389{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002390 bool pending;
2391 unsigned int bidi_bytes = 0;
2392
2393 if (unlikely(blk_bidi_rq(rq)))
2394 bidi_bytes = blk_rq_bytes(rq->next_rq);
2395
2396 pending = __blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2397 BUG_ON(pending);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002398}
Jens Axboe56ad1742009-07-28 22:11:24 +02002399EXPORT_SYMBOL(__blk_end_request_all);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002400
2401/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002402 * __blk_end_request_cur - Helper function to finish the current request chunk.
2403 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002404 * @error: %0 for success, < %0 for error
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002405 *
2406 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002407 * Complete the current consecutively mapped chunk from @rq. Must
2408 * be called with queue lock held.
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002409 *
2410 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002411 * %false - we are done with this request
2412 * %true - still buffers pending for this request
2413 */
2414bool __blk_end_request_cur(struct request *rq, int error)
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002415{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002416 return __blk_end_request(rq, error, blk_rq_cur_bytes(rq));
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002417}
Jens Axboe56ad1742009-07-28 22:11:24 +02002418EXPORT_SYMBOL(__blk_end_request_cur);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002419
Tejun Heo80a761f2009-07-03 17:48:17 +09002420/**
2421 * __blk_end_request_err - Finish a request till the next failure boundary.
2422 * @rq: the request to finish till the next failure boundary for
2423 * @error: must be negative errno
2424 *
2425 * Description:
2426 * Complete @rq till the next failure boundary. Must be called
2427 * with queue lock held.
2428 *
2429 * Return:
2430 * %false - we are done with this request
2431 * %true - still buffers pending for this request
2432 */
2433bool __blk_end_request_err(struct request *rq, int error)
2434{
2435 WARN_ON(error >= 0);
2436 return __blk_end_request(rq, error, blk_rq_err_bytes(rq));
2437}
2438EXPORT_SYMBOL_GPL(__blk_end_request_err);
2439
Jens Axboe86db1e22008-01-29 14:53:40 +01002440void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2441 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442{
Tejun Heoa82afdf2009-07-03 17:48:16 +09002443 /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002444 rq->cmd_flags |= bio->bi_rw & REQ_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
David Woodhousefb2dce82008-08-05 18:01:53 +01002446 if (bio_has_data(bio)) {
2447 rq->nr_phys_segments = bio_phys_segments(q, bio);
David Woodhousefb2dce82008-08-05 18:01:53 +01002448 rq->buffer = bio_data(bio);
2449 }
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002450 rq->__data_len = bio->bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 rq->bio = rq->biotail = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
NeilBrown66846572007-08-16 13:31:28 +02002453 if (bio->bi_bdev)
2454 rq->rq_disk = bio->bi_bdev->bd_disk;
2455}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Ilya Loginov2d4dc892009-11-26 09:16:19 +01002457#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
2458/**
2459 * rq_flush_dcache_pages - Helper function to flush all pages in a request
2460 * @rq: the request to be flushed
2461 *
2462 * Description:
2463 * Flush all pages in @rq.
2464 */
2465void rq_flush_dcache_pages(struct request *rq)
2466{
2467 struct req_iterator iter;
2468 struct bio_vec *bvec;
2469
2470 rq_for_each_segment(bvec, rq, iter)
2471 flush_dcache_page(bvec->bv_page);
2472}
2473EXPORT_SYMBOL_GPL(rq_flush_dcache_pages);
2474#endif
2475
Kiyoshi Uedaef9e3fa2008-10-01 16:12:15 +02002476/**
2477 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
2478 * @q : the queue of the device being checked
2479 *
2480 * Description:
2481 * Check if underlying low-level drivers of a device are busy.
2482 * If the drivers want to export their busy state, they must set own
2483 * exporting function using blk_queue_lld_busy() first.
2484 *
2485 * Basically, this function is used only by request stacking drivers
2486 * to stop dispatching requests to underlying devices when underlying
2487 * devices are busy. This behavior helps more I/O merging on the queue
2488 * of the request stacking driver and prevents I/O throughput regression
2489 * on burst I/O load.
2490 *
2491 * Return:
2492 * 0 - Not busy (The request stacking driver should dispatch request)
2493 * 1 - Busy (The request stacking driver should stop dispatching request)
2494 */
2495int blk_lld_busy(struct request_queue *q)
2496{
2497 if (q->lld_busy_fn)
2498 return q->lld_busy_fn(q);
2499
2500 return 0;
2501}
2502EXPORT_SYMBOL_GPL(blk_lld_busy);
2503
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002504/**
2505 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
2506 * @rq: the clone request to be cleaned up
2507 *
2508 * Description:
2509 * Free all bios in @rq for a cloned request.
2510 */
2511void blk_rq_unprep_clone(struct request *rq)
2512{
2513 struct bio *bio;
2514
2515 while ((bio = rq->bio) != NULL) {
2516 rq->bio = bio->bi_next;
2517
2518 bio_put(bio);
2519 }
2520}
2521EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
2522
2523/*
2524 * Copy attributes of the original request to the clone request.
2525 * The actual data parts (e.g. ->cmd, ->buffer, ->sense) are not copied.
2526 */
2527static void __blk_rq_prep_clone(struct request *dst, struct request *src)
2528{
2529 dst->cpu = src->cpu;
Tejun Heo3a2edd02010-09-03 11:56:18 +02002530 dst->cmd_flags = (src->cmd_flags & REQ_CLONE_MASK) | REQ_NOMERGE;
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002531 dst->cmd_type = src->cmd_type;
2532 dst->__sector = blk_rq_pos(src);
2533 dst->__data_len = blk_rq_bytes(src);
2534 dst->nr_phys_segments = src->nr_phys_segments;
2535 dst->ioprio = src->ioprio;
2536 dst->extra_len = src->extra_len;
2537}
2538
2539/**
2540 * blk_rq_prep_clone - Helper function to setup clone request
2541 * @rq: the request to be setup
2542 * @rq_src: original request to be cloned
2543 * @bs: bio_set that bios for clone are allocated from
2544 * @gfp_mask: memory allocation mask for bio
2545 * @bio_ctr: setup function to be called for each clone bio.
2546 * Returns %0 for success, non %0 for failure.
2547 * @data: private data to be passed to @bio_ctr
2548 *
2549 * Description:
2550 * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
2551 * The actual data parts of @rq_src (e.g. ->cmd, ->buffer, ->sense)
2552 * are not copied, and copying such parts is the caller's responsibility.
2553 * Also, pages which the original bios are pointing to are not copied
2554 * and the cloned bios just point same pages.
2555 * So cloned bios must be completed before original bios, which means
2556 * the caller must complete @rq before @rq_src.
2557 */
2558int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
2559 struct bio_set *bs, gfp_t gfp_mask,
2560 int (*bio_ctr)(struct bio *, struct bio *, void *),
2561 void *data)
2562{
2563 struct bio *bio, *bio_src;
2564
2565 if (!bs)
2566 bs = fs_bio_set;
2567
2568 blk_rq_init(NULL, rq);
2569
2570 __rq_for_each_bio(bio_src, rq_src) {
2571 bio = bio_alloc_bioset(gfp_mask, bio_src->bi_max_vecs, bs);
2572 if (!bio)
2573 goto free_and_out;
2574
2575 __bio_clone(bio, bio_src);
2576
2577 if (bio_integrity(bio_src) &&
Martin K. Petersen7878cba2009-06-26 15:37:49 +02002578 bio_integrity_clone(bio, bio_src, gfp_mask, bs))
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002579 goto free_and_out;
2580
2581 if (bio_ctr && bio_ctr(bio, bio_src, data))
2582 goto free_and_out;
2583
2584 if (rq->bio) {
2585 rq->biotail->bi_next = bio;
2586 rq->biotail = bio;
2587 } else
2588 rq->bio = rq->biotail = bio;
2589 }
2590
2591 __blk_rq_prep_clone(rq, rq_src);
2592
2593 return 0;
2594
2595free_and_out:
2596 if (bio)
2597 bio_free(bio, bs);
2598 blk_rq_unprep_clone(rq);
2599
2600 return -ENOMEM;
2601}
2602EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
2603
Jens Axboe18887ad2008-07-28 13:08:45 +02002604int kblockd_schedule_work(struct request_queue *q, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605{
2606 return queue_work(kblockd_workqueue, work);
2607}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608EXPORT_SYMBOL(kblockd_schedule_work);
2609
Vivek Goyale43473b2010-09-15 17:06:35 -04002610int kblockd_schedule_delayed_work(struct request_queue *q,
2611 struct delayed_work *dwork, unsigned long delay)
2612{
2613 return queue_delayed_work(kblockd_workqueue, dwork, delay);
2614}
2615EXPORT_SYMBOL(kblockd_schedule_delayed_work);
2616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617int __init blk_dev_init(void)
2618{
Nikanth Karthikesan9eb55b02009-04-27 14:53:54 +02002619 BUILD_BUG_ON(__REQ_NR_BITS > 8 *
2620 sizeof(((struct request *)0)->cmd_flags));
2621
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 kblockd_workqueue = create_workqueue("kblockd");
2623 if (!kblockd_workqueue)
2624 panic("Failed to create kblockd\n");
2625
2626 request_cachep = kmem_cache_create("blkdev_requests",
Paul Mundt20c2df82007-07-20 10:11:58 +09002627 sizeof(struct request), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
Jens Axboe8324aa92008-01-29 14:51:59 +01002629 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02002630 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 return 0;
2633}