blob: 797d5095eb83fa5cada4be1712858fe6dc391145 [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();
Tejun Heo83096eb2009-05-07 22:24:39 +090067 part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq));
Tejun Heoc9959052008-08-25 19:47:21 +090068
Jens Axboe28f13702008-05-07 10:15:46 +020069 if (!new_io)
Tejun Heo074a7ac2008-08-25 19:56:14 +090070 part_stat_inc(cpu, part, merges[rw]);
Jens Axboe28f13702008-05-07 10:15:46 +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{
Jens Axboe165125e2007-07-24 09:28:11 +0200137 struct request_queue *q = rq->q;
Tejun Heo797e7db2006-01-06 09:51:03 +0100138
NeilBrown5bb23a62007-09-27 12:46:13 +0200139 if (&q->bar_rq != rq) {
140 if (error)
141 clear_bit(BIO_UPTODATE, &bio->bi_flags);
142 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
143 error = -EIO;
Tejun Heo797e7db2006-01-06 09:51:03 +0100144
NeilBrown5bb23a62007-09-27 12:46:13 +0200145 if (unlikely(nbytes > bio->bi_size)) {
Jens Axboe6728cb02008-01-31 13:03:55 +0100146 printk(KERN_ERR "%s: want %u bytes done, %u left\n",
Harvey Harrison24c03d42008-05-01 04:35:17 -0700147 __func__, nbytes, bio->bi_size);
NeilBrown5bb23a62007-09-27 12:46:13 +0200148 nbytes = bio->bi_size;
149 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100150
Keith Mannthey08bafc02008-11-25 10:24:35 +0100151 if (unlikely(rq->cmd_flags & REQ_QUIET))
152 set_bit(BIO_QUIET, &bio->bi_flags);
153
NeilBrown5bb23a62007-09-27 12:46:13 +0200154 bio->bi_size -= nbytes;
155 bio->bi_sector += (nbytes >> 9);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200156
157 if (bio_integrity(bio))
158 bio_integrity_advance(bio, nbytes);
159
NeilBrown5bb23a62007-09-27 12:46:13 +0200160 if (bio->bi_size == 0)
NeilBrown6712ecf2007-09-27 12:47:43 +0200161 bio_endio(bio, error);
NeilBrown5bb23a62007-09-27 12:46:13 +0200162 } else {
163
164 /*
165 * Okay, this is the barrier request in progress, just
166 * record the error;
167 */
168 if (error && !q->orderr)
169 q->orderr = error;
170 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100171}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173void blk_dump_rq_flags(struct request *rq, char *msg)
174{
175 int bit;
176
Jens Axboe6728cb02008-01-31 13:03:55 +0100177 printk(KERN_INFO "%s: dev %s: type=%x, flags=%x\n", msg,
Jens Axboe4aff5e22006-08-10 08:44:47 +0200178 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
179 rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Tejun Heo83096eb2009-05-07 22:24:39 +0900181 printk(KERN_INFO " sector %llu, nr/cnr %u/%u\n",
182 (unsigned long long)blk_rq_pos(rq),
183 blk_rq_sectors(rq), blk_rq_cur_sectors(rq));
Tejun Heo731ec492009-04-23 11:05:20 +0900184 printk(KERN_INFO " bio %p, biotail %p, buffer %p, len %u\n",
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900185 rq->bio, rq->biotail, rq->buffer, blk_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200187 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
Jens Axboe6728cb02008-01-31 13:03:55 +0100188 printk(KERN_INFO " cdb: ");
FUJITA Tomonorid34c87e2008-04-29 14:37:52 +0200189 for (bit = 0; bit < BLK_MAX_CDB; bit++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 printk("%02x ", rq->cmd[bit]);
191 printk("\n");
192 }
193}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194EXPORT_SYMBOL(blk_dump_rq_flags);
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196/*
197 * "plug" the device if there are no outstanding requests: this will
198 * force the transfer to start only after we have put all the requests
199 * on the list.
200 *
201 * This is called with interrupts off and no requests on the queue and
202 * with the queue lock held.
203 */
Jens Axboe165125e2007-07-24 09:28:11 +0200204void blk_plug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 WARN_ON(!irqs_disabled());
207
208 /*
209 * don't plug a stopped queue, it must be paired with blk_start_queue()
210 * which will restart the queueing
211 */
Coywolf Qi Hunt7daac492006-04-19 10:14:49 +0200212 if (blk_queue_stopped(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return;
214
Jens Axboee48ec692008-07-03 13:18:54 +0200215 if (!queue_flag_test_and_set(QUEUE_FLAG_PLUGGED, q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100217 trace_block_plug(q);
Jens Axboe2056a782006-03-23 20:00:26 +0100218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220EXPORT_SYMBOL(blk_plug_device);
221
Jens Axboe6c5e0c42008-08-01 20:31:32 +0200222/**
223 * blk_plug_device_unlocked - plug a device without queue lock held
224 * @q: The &struct request_queue to plug
225 *
226 * Description:
227 * Like @blk_plug_device(), but grabs the queue lock and disables
228 * interrupts.
229 **/
230void blk_plug_device_unlocked(struct request_queue *q)
231{
232 unsigned long flags;
233
234 spin_lock_irqsave(q->queue_lock, flags);
235 blk_plug_device(q);
236 spin_unlock_irqrestore(q->queue_lock, flags);
237}
238EXPORT_SYMBOL(blk_plug_device_unlocked);
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/*
241 * remove the queue from the plugged list, if present. called with
242 * queue lock held and interrupts disabled.
243 */
Jens Axboe165125e2007-07-24 09:28:11 +0200244int blk_remove_plug(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 WARN_ON(!irqs_disabled());
247
Jens Axboee48ec692008-07-03 13:18:54 +0200248 if (!queue_flag_test_and_clear(QUEUE_FLAG_PLUGGED, q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return 0;
250
251 del_timer(&q->unplug_timer);
252 return 1;
253}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254EXPORT_SYMBOL(blk_remove_plug);
255
256/*
257 * remove the plug and let it rip..
258 */
Jens Axboe165125e2007-07-24 09:28:11 +0200259void __generic_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Coywolf Qi Hunt7daac492006-04-19 10:14:49 +0200261 if (unlikely(blk_queue_stopped(q)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return;
Jens Axboea31a9732008-10-17 13:58:29 +0200263 if (!blk_remove_plug(q) && !blk_queue_nonrot(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return;
265
Jens Axboe22e2c502005-06-27 10:55:12 +0200266 q->request_fn(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269/**
270 * generic_unplug_device - fire a request queue
Jens Axboe165125e2007-07-24 09:28:11 +0200271 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 *
273 * Description:
274 * Linux uses plugging to build bigger requests queues before letting
275 * the device have at them. If a queue is plugged, the I/O scheduler
276 * is still adding and merging requests on the queue. Once the queue
277 * gets unplugged, the request_fn defined for the queue is invoked and
278 * transfers started.
279 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200280void generic_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Jens Axboedbaf2c02008-05-07 09:48:17 +0200282 if (blk_queue_plugged(q)) {
283 spin_lock_irq(q->queue_lock);
284 __generic_unplug_device(q);
285 spin_unlock_irq(q->queue_lock);
286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288EXPORT_SYMBOL(generic_unplug_device);
289
290static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
291 struct page *page)
292{
Jens Axboe165125e2007-07-24 09:28:11 +0200293 struct request_queue *q = bdi->unplug_io_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500295 blk_unplug(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
Jens Axboe86db1e22008-01-29 14:53:40 +0100298void blk_unplug_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Jens Axboe165125e2007-07-24 09:28:11 +0200300 struct request_queue *q =
301 container_of(work, struct request_queue, unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100303 trace_block_unplug_io(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 q->unplug_fn(q);
305}
306
Jens Axboe86db1e22008-01-29 14:53:40 +0100307void blk_unplug_timeout(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Jens Axboe165125e2007-07-24 09:28:11 +0200309 struct request_queue *q = (struct request_queue *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100311 trace_block_unplug_timer(q);
Jens Axboe18887ad2008-07-28 13:08:45 +0200312 kblockd_schedule_work(q, &q->unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500315void blk_unplug(struct request_queue *q)
316{
317 /*
318 * devices don't necessarily have an ->unplug_fn defined
319 */
320 if (q->unplug_fn) {
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100321 trace_block_unplug_io(q);
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500322 q->unplug_fn(q);
323 }
324}
325EXPORT_SYMBOL(blk_unplug);
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327/**
328 * blk_start_queue - restart a previously stopped queue
Jens Axboe165125e2007-07-24 09:28:11 +0200329 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 *
331 * Description:
332 * blk_start_queue() will clear the stop flag on the queue, and call
333 * the request_fn for the queue if it was in a stopped state when
334 * entered. Also see blk_stop_queue(). Queue lock must be held.
335 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200336void blk_start_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200338 WARN_ON(!irqs_disabled());
339
Nick Piggin75ad23b2008-04-29 14:48:33 +0200340 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
Tejun Heoa538cd02009-04-23 11:05:17 +0900341 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343EXPORT_SYMBOL(blk_start_queue);
344
345/**
346 * blk_stop_queue - stop a queue
Jens Axboe165125e2007-07-24 09:28:11 +0200347 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 *
349 * Description:
350 * The Linux block layer assumes that a block driver will consume all
351 * entries on the request queue when the request_fn strategy is called.
352 * Often this will not happen, because of hardware limitations (queue
353 * depth settings). If a device driver gets a 'queue full' response,
354 * or if it simply chooses not to queue more I/O at one point, it can
355 * call this function to prevent the request_fn from being called until
356 * the driver has signalled it's ready to go again. This happens by calling
357 * blk_start_queue() to restart queue operations. Queue lock must be held.
358 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200359void blk_stop_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
361 blk_remove_plug(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200362 queue_flag_set(QUEUE_FLAG_STOPPED, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364EXPORT_SYMBOL(blk_stop_queue);
365
366/**
367 * blk_sync_queue - cancel any pending callbacks on a queue
368 * @q: the queue
369 *
370 * Description:
371 * The block layer may perform asynchronous callback activity
372 * on a queue, such as calling the unplug function after a timeout.
373 * A block device may call blk_sync_queue to ensure that any
374 * such activity is cancelled, thus allowing it to release resources
Michael Opdenacker59c51592007-05-09 08:57:56 +0200375 * that the callbacks might use. The caller must already have made sure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 * that its ->make_request_fn will not re-add plugging prior to calling
377 * this function.
378 *
379 */
380void blk_sync_queue(struct request_queue *q)
381{
382 del_timer_sync(&q->unplug_timer);
Jens Axboe70ed28b2008-11-19 14:38:39 +0100383 del_timer_sync(&q->timeout);
Cheng Renquan64d01dc2008-12-03 12:41:39 +0100384 cancel_work_sync(&q->unplug_work);
Vivek Goyale43473b2010-09-15 17:06:35 -0400385 throtl_shutdown_timer_wq(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387EXPORT_SYMBOL(blk_sync_queue);
388
389/**
Jens Axboe80a4b582008-10-14 09:51:06 +0200390 * __blk_run_queue - run a single device queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200392 *
393 * Description:
394 * See @blk_run_queue. This variant must be called with the queue lock
395 * held and interrupts disabled.
396 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 */
Nick Piggin75ad23b2008-04-29 14:48:33 +0200398void __blk_run_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 blk_remove_plug(q);
Jens Axboedac07ec2006-05-11 08:20:16 +0200401
Tejun Heoa538cd02009-04-23 11:05:17 +0900402 if (unlikely(blk_queue_stopped(q)))
403 return;
404
405 if (elv_queue_empty(q))
406 return;
407
Jens Axboedac07ec2006-05-11 08:20:16 +0200408 /*
409 * Only recurse once to avoid overrunning the stack, let the unplug
410 * handling reinvoke the handler shortly if we already got there.
411 */
Tejun Heoa538cd02009-04-23 11:05:17 +0900412 if (!queue_flag_test_and_set(QUEUE_FLAG_REENTER, q)) {
413 q->request_fn(q);
414 queue_flag_clear(QUEUE_FLAG_REENTER, q);
415 } else {
416 queue_flag_set(QUEUE_FLAG_PLUGGED, q);
417 kblockd_schedule_work(q, &q->unplug_work);
418 }
Nick Piggin75ad23b2008-04-29 14:48:33 +0200419}
420EXPORT_SYMBOL(__blk_run_queue);
Jens Axboedac07ec2006-05-11 08:20:16 +0200421
Nick Piggin75ad23b2008-04-29 14:48:33 +0200422/**
423 * blk_run_queue - run a single device queue
424 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200425 *
426 * Description:
427 * Invoke request handling on this queue, if it has pending work to do.
Tejun Heoa7f55792009-04-23 11:05:17 +0900428 * May be used to restart queueing when a request has completed.
Nick Piggin75ad23b2008-04-29 14:48:33 +0200429 */
430void blk_run_queue(struct request_queue *q)
431{
432 unsigned long flags;
433
434 spin_lock_irqsave(q->queue_lock, flags);
435 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 spin_unlock_irqrestore(q->queue_lock, flags);
437}
438EXPORT_SYMBOL(blk_run_queue);
439
Jens Axboe165125e2007-07-24 09:28:11 +0200440void blk_put_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500441{
442 kobject_put(&q->kobj);
443}
Al Viro483f4af2006-03-18 18:34:37 -0500444
Jens Axboe6728cb02008-01-31 13:03:55 +0100445void blk_cleanup_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500446{
Jens Axboee3335de92008-09-18 09:22:54 -0700447 /*
448 * We know we have process context here, so we can be a little
449 * cautious and ensure that pending block actions on this device
450 * are done before moving on. Going into this function, we should
451 * not have processes doing IO to this device.
452 */
453 blk_sync_queue(q);
454
Matthew Garrett31373d02010-04-06 14:25:14 +0200455 del_timer_sync(&q->backing_dev_info.laptop_mode_wb_timer);
Al Viro483f4af2006-03-18 18:34:37 -0500456 mutex_lock(&q->sysfs_lock);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200457 queue_flag_set_unlocked(QUEUE_FLAG_DEAD, q);
Al Viro483f4af2006-03-18 18:34:37 -0500458 mutex_unlock(&q->sysfs_lock);
459
460 if (q->elevator)
461 elevator_exit(q->elevator);
462
Vivek Goyale43473b2010-09-15 17:06:35 -0400463 blk_throtl_exit(q);
464
Al Viro483f4af2006-03-18 18:34:37 -0500465 blk_put_queue(q);
466}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467EXPORT_SYMBOL(blk_cleanup_queue);
468
Jens Axboe165125e2007-07-24 09:28:11 +0200469static int blk_init_free_list(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
471 struct request_list *rl = &q->rq;
472
Mike Snitzer1abec4f2010-05-25 13:15:15 -0400473 if (unlikely(rl->rq_pool))
474 return 0;
475
Jens Axboe1faa16d2009-04-06 14:48:01 +0200476 rl->count[BLK_RW_SYNC] = rl->count[BLK_RW_ASYNC] = 0;
477 rl->starved[BLK_RW_SYNC] = rl->starved[BLK_RW_ASYNC] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200478 rl->elvpriv = 0;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200479 init_waitqueue_head(&rl->wait[BLK_RW_SYNC]);
480 init_waitqueue_head(&rl->wait[BLK_RW_ASYNC]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Christoph Lameter19460892005-06-23 00:08:19 -0700482 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
483 mempool_free_slab, request_cachep, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 if (!rl->rq_pool)
486 return -ENOMEM;
487
488 return 0;
489}
490
Jens Axboe165125e2007-07-24 09:28:11 +0200491struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Christoph Lameter19460892005-06-23 00:08:19 -0700493 return blk_alloc_queue_node(gfp_mask, -1);
494}
495EXPORT_SYMBOL(blk_alloc_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Jens Axboe165125e2007-07-24 09:28:11 +0200497struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -0700498{
Jens Axboe165125e2007-07-24 09:28:11 +0200499 struct request_queue *q;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700500 int err;
Christoph Lameter19460892005-06-23 00:08:19 -0700501
Jens Axboe8324aa92008-01-29 14:51:59 +0100502 q = kmem_cache_alloc_node(blk_requestq_cachep,
Christoph Lameter94f60302007-07-17 04:03:29 -0700503 gfp_mask | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (!q)
505 return NULL;
506
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700507 q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
508 q->backing_dev_info.unplug_io_data = q;
Jens Axboe0989a022009-06-12 14:42:56 +0200509 q->backing_dev_info.ra_pages =
510 (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
511 q->backing_dev_info.state = 0;
512 q->backing_dev_info.capabilities = BDI_CAP_MAP_COPY;
Jens Axboed9938312009-06-12 14:45:52 +0200513 q->backing_dev_info.name = "block";
Jens Axboe0989a022009-06-12 14:42:56 +0200514
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700515 err = bdi_init(&q->backing_dev_info);
516 if (err) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100517 kmem_cache_free(blk_requestq_cachep, q);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700518 return NULL;
519 }
520
Vivek Goyale43473b2010-09-15 17:06:35 -0400521 if (blk_throtl_init(q)) {
522 kmem_cache_free(blk_requestq_cachep, q);
523 return NULL;
524 }
525
Matthew Garrett31373d02010-04-06 14:25:14 +0200526 setup_timer(&q->backing_dev_info.laptop_mode_wb_timer,
527 laptop_mode_timer_fn, (unsigned long) q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 init_timer(&q->unplug_timer);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700529 setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
530 INIT_LIST_HEAD(&q->timeout_list);
Peter Zijlstra713ada92008-10-16 13:44:57 +0200531 INIT_WORK(&q->unplug_work, blk_unplug_work);
Al Viro483f4af2006-03-18 18:34:37 -0500532
Jens Axboe8324aa92008-01-29 14:51:59 +0100533 kobject_init(&q->kobj, &blk_queue_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Al Viro483f4af2006-03-18 18:34:37 -0500535 mutex_init(&q->sysfs_lock);
Neil Browne7e72bf2008-05-14 16:05:54 -0700536 spin_lock_init(&q->__queue_lock);
Al Viro483f4af2006-03-18 18:34:37 -0500537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return q;
539}
Christoph Lameter19460892005-06-23 00:08:19 -0700540EXPORT_SYMBOL(blk_alloc_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542/**
543 * blk_init_queue - prepare a request queue for use with a block device
544 * @rfn: The function to be called to process requests that have been
545 * placed on the queue.
546 * @lock: Request queue spin lock
547 *
548 * Description:
549 * If a block device wishes to use the standard request handling procedures,
550 * which sorts requests and coalesces adjacent requests, then it must
551 * call blk_init_queue(). The function @rfn will be called when there
552 * are requests on the queue that need to be processed. If the device
553 * supports plugging, then @rfn may not be called immediately when requests
554 * are available on the queue, but may be called at some time later instead.
555 * Plugged queues are generally unplugged when a buffer belonging to one
556 * of the requests on the queue is needed, or due to memory pressure.
557 *
558 * @rfn is not required, or even expected, to remove all requests off the
559 * queue, but only as many as it can handle at a time. If it does leave
560 * requests on the queue, it is responsible for arranging that the requests
561 * get dealt with eventually.
562 *
563 * The queue spin lock must be held while manipulating the requests on the
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200564 * request queue; this lock will be taken also from interrupt context, so irq
565 * disabling is needed for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 *
Randy Dunlap710027a2008-08-19 20:13:11 +0200567 * Function returns a pointer to the initialized request queue, or %NULL if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 * it didn't succeed.
569 *
570 * Note:
571 * blk_init_queue() must be paired with a blk_cleanup_queue() call
572 * when the block device is deactivated (such as at module unload).
573 **/
Christoph Lameter19460892005-06-23 00:08:19 -0700574
Jens Axboe165125e2007-07-24 09:28:11 +0200575struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
Christoph Lameter19460892005-06-23 00:08:19 -0700577 return blk_init_queue_node(rfn, lock, -1);
578}
579EXPORT_SYMBOL(blk_init_queue);
580
Jens Axboe165125e2007-07-24 09:28:11 +0200581struct request_queue *
Christoph Lameter19460892005-06-23 00:08:19 -0700582blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
583{
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600584 struct request_queue *uninit_q, *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600586 uninit_q = blk_alloc_queue_node(GFP_KERNEL, node_id);
587 if (!uninit_q)
588 return NULL;
589
590 q = blk_init_allocated_queue_node(uninit_q, rfn, lock, node_id);
591 if (!q)
592 blk_cleanup_queue(uninit_q);
593
594 return q;
Mike Snitzer01effb02010-05-11 08:57:42 +0200595}
596EXPORT_SYMBOL(blk_init_queue_node);
597
598struct request_queue *
599blk_init_allocated_queue(struct request_queue *q, request_fn_proc *rfn,
600 spinlock_t *lock)
601{
602 return blk_init_allocated_queue_node(q, rfn, lock, -1);
603}
604EXPORT_SYMBOL(blk_init_allocated_queue);
605
606struct request_queue *
607blk_init_allocated_queue_node(struct request_queue *q, request_fn_proc *rfn,
608 spinlock_t *lock, int node_id)
609{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (!q)
611 return NULL;
612
Christoph Lameter19460892005-06-23 00:08:19 -0700613 q->node = node_id;
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600614 if (blk_init_free_list(q))
Al Viro8669aaf2006-03-18 13:50:00 -0500615 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 q->request_fn = rfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 q->prep_rq_fn = NULL;
James Bottomley28018c22010-07-01 19:49:17 +0900619 q->unprep_rq_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 q->unplug_fn = generic_unplug_device;
Jens Axboebc58ba92009-01-23 10:54:44 +0100621 q->queue_flags = QUEUE_FLAG_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 q->queue_lock = lock;
623
Jens Axboef3b144a2009-03-06 08:48:33 +0100624 /*
625 * This also sets hw/phys segments, boundary and size
626 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 blk_queue_make_request(q, __make_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Alan Stern44ec9542007-02-20 11:01:57 -0500629 q->sg_reserved_size = INT_MAX;
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 /*
632 * all done
633 */
634 if (!elevator_init(q, NULL)) {
635 blk_queue_congestion_threshold(q);
636 return q;
637 }
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return NULL;
640}
Mike Snitzer01effb02010-05-11 08:57:42 +0200641EXPORT_SYMBOL(blk_init_allocated_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Jens Axboe165125e2007-07-24 09:28:11 +0200643int blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Nick Pigginfde6ad22005-06-23 00:08:53 -0700645 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
Al Viro483f4af2006-03-18 18:34:37 -0500646 kobject_get(&q->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return 0;
648 }
649
650 return 1;
651}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Jens Axboe165125e2007-07-24 09:28:11 +0200653static inline void blk_free_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
Jens Axboe4aff5e22006-08-10 08:44:47 +0200655 if (rq->cmd_flags & REQ_ELVPRIV)
Tejun Heocb98fc82005-10-28 08:29:39 +0200656 elv_put_request(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 mempool_free(rq, q->rq.rq_pool);
658}
659
Jens Axboe1ea25ecb2006-07-18 22:24:11 +0200660static struct request *
Jerome Marchand42dad762009-04-22 14:01:49 +0200661blk_alloc_request(struct request_queue *q, int flags, int priv, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
663 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
664
665 if (!rq)
666 return NULL;
667
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200668 blk_rq_init(q, rq);
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200669
Jerome Marchand42dad762009-04-22 14:01:49 +0200670 rq->cmd_flags = flags | REQ_ALLOCED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Tejun Heocb98fc82005-10-28 08:29:39 +0200672 if (priv) {
Jens Axboecb78b282006-07-28 09:32:57 +0200673 if (unlikely(elv_set_request(q, rq, gfp_mask))) {
Tejun Heocb98fc82005-10-28 08:29:39 +0200674 mempool_free(rq, q->rq.rq_pool);
675 return NULL;
676 }
Jens Axboe4aff5e22006-08-10 08:44:47 +0200677 rq->cmd_flags |= REQ_ELVPRIV;
Tejun Heocb98fc82005-10-28 08:29:39 +0200678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Tejun Heocb98fc82005-10-28 08:29:39 +0200680 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
683/*
684 * ioc_batching returns true if the ioc is a valid batching request and
685 * should be given priority access to a request.
686 */
Jens Axboe165125e2007-07-24 09:28:11 +0200687static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
689 if (!ioc)
690 return 0;
691
692 /*
693 * Make sure the process is able to allocate at least 1 request
694 * even if the batch times out, otherwise we could theoretically
695 * lose wakeups.
696 */
697 return ioc->nr_batch_requests == q->nr_batching ||
698 (ioc->nr_batch_requests > 0
699 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
700}
701
702/*
703 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
704 * will cause the process to be a "batcher" on all queues in the system. This
705 * is the behaviour we want though - once it gets a wakeup it should be given
706 * a nice run.
707 */
Jens Axboe165125e2007-07-24 09:28:11 +0200708static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
710 if (!ioc || ioc_batching(q, ioc))
711 return;
712
713 ioc->nr_batch_requests = q->nr_batching;
714 ioc->last_waited = jiffies;
715}
716
Jens Axboe1faa16d2009-04-06 14:48:01 +0200717static void __freed_request(struct request_queue *q, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
719 struct request_list *rl = &q->rq;
720
Jens Axboe1faa16d2009-04-06 14:48:01 +0200721 if (rl->count[sync] < queue_congestion_off_threshold(q))
722 blk_clear_queue_congested(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Jens Axboe1faa16d2009-04-06 14:48:01 +0200724 if (rl->count[sync] + 1 <= q->nr_requests) {
725 if (waitqueue_active(&rl->wait[sync]))
726 wake_up(&rl->wait[sync]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Jens Axboe1faa16d2009-04-06 14:48:01 +0200728 blk_clear_queue_full(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
730}
731
732/*
733 * A request has just been released. Account for it, update the full and
734 * congestion status, wake up any waiters. Called under q->queue_lock.
735 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200736static void freed_request(struct request_queue *q, int sync, int priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
738 struct request_list *rl = &q->rq;
739
Jens Axboe1faa16d2009-04-06 14:48:01 +0200740 rl->count[sync]--;
Tejun Heocb98fc82005-10-28 08:29:39 +0200741 if (priv)
742 rl->elvpriv--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Jens Axboe1faa16d2009-04-06 14:48:01 +0200744 __freed_request(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Jens Axboe1faa16d2009-04-06 14:48:01 +0200746 if (unlikely(rl->starved[sync ^ 1]))
747 __freed_request(q, sync ^ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750/*
Nick Piggind6344532005-06-28 20:45:14 -0700751 * Get a free request, queue_lock must be held.
752 * Returns NULL on failure, with queue_lock held.
753 * Returns !NULL on success, with queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 */
Jens Axboe165125e2007-07-24 09:28:11 +0200755static struct request *get_request(struct request_queue *q, int rw_flags,
Jens Axboe7749a8d2006-12-13 13:02:26 +0100756 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
758 struct request *rq = NULL;
759 struct request_list *rl = &q->rq;
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100760 struct io_context *ioc = NULL;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200761 const bool is_sync = rw_is_sync(rw_flags) != 0;
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100762 int may_queue, priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Jens Axboe7749a8d2006-12-13 13:02:26 +0100764 may_queue = elv_may_queue(q, rw_flags);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100765 if (may_queue == ELV_MQUEUE_NO)
766 goto rq_starved;
767
Jens Axboe1faa16d2009-04-06 14:48:01 +0200768 if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) {
769 if (rl->count[is_sync]+1 >= q->nr_requests) {
Jens Axboeb5deef92006-07-19 23:39:40 +0200770 ioc = current_io_context(GFP_ATOMIC, q->node);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100771 /*
772 * The queue will fill after this allocation, so set
773 * it as full, and mark this process as "batching".
774 * This process will be allowed to complete a batch of
775 * requests, others will be blocked.
776 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200777 if (!blk_queue_full(q, is_sync)) {
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100778 ioc_set_batching(q, ioc);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200779 blk_set_queue_full(q, is_sync);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100780 } else {
781 if (may_queue != ELV_MQUEUE_MUST
782 && !ioc_batching(q, ioc)) {
783 /*
784 * The queue is full and the allocating
785 * process is not a "batcher", and not
786 * exempted by the IO scheduler
787 */
788 goto out;
789 }
790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
Jens Axboe1faa16d2009-04-06 14:48:01 +0200792 blk_set_queue_congested(q, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 }
794
Jens Axboe082cf692005-06-28 16:35:11 +0200795 /*
796 * Only allow batching queuers to allocate up to 50% over the defined
797 * limit of requests, otherwise we could have thousands of requests
798 * allocated with any setting of ->nr_requests
799 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200800 if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
Jens Axboe082cf692005-06-28 16:35:11 +0200801 goto out;
Hugh Dickinsfd782a42005-06-29 15:15:40 +0100802
Jens Axboe1faa16d2009-04-06 14:48:01 +0200803 rl->count[is_sync]++;
804 rl->starved[is_sync] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200805
Jens Axboe64521d12005-10-28 08:30:39 +0200806 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Tejun Heocb98fc82005-10-28 08:29:39 +0200807 if (priv)
808 rl->elvpriv++;
809
Jerome Marchand42dad762009-04-22 14:01:49 +0200810 if (blk_queue_io_stat(q))
811 rw_flags |= REQ_IO_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 spin_unlock_irq(q->queue_lock);
813
Jens Axboe7749a8d2006-12-13 13:02:26 +0100814 rq = blk_alloc_request(q, rw_flags, priv, gfp_mask);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100815 if (unlikely(!rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 /*
817 * Allocation failed presumably due to memory. Undo anything
818 * we might have messed up.
819 *
820 * Allocating task should really be put onto the front of the
821 * wait queue, but this is pretty rare.
822 */
823 spin_lock_irq(q->queue_lock);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200824 freed_request(q, is_sync, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 /*
827 * in the very unlikely event that allocation failed and no
828 * requests for this direction was pending, mark us starved
829 * so that freeing of a request in the other direction will
830 * notice us. another possible fix would be to split the
831 * rq mempool into READ and WRITE
832 */
833rq_starved:
Jens Axboe1faa16d2009-04-06 14:48:01 +0200834 if (unlikely(rl->count[is_sync] == 0))
835 rl->starved[is_sync] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 goto out;
838 }
839
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100840 /*
841 * ioc may be NULL here, and ioc_batching will be false. That's
842 * OK, if the queue is under the request limit then requests need
843 * not count toward the nr_batch_requests limit. There will always
844 * be some limit enforced by BLK_BATCH_TIME.
845 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (ioc_batching(q, ioc))
847 ioc->nr_batch_requests--;
Jens Axboe6728cb02008-01-31 13:03:55 +0100848
Jens Axboe1faa16d2009-04-06 14:48:01 +0200849 trace_block_getrq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return rq;
852}
853
854/*
855 * No available requests for this queue, unplug the device and wait for some
856 * requests to become available.
Nick Piggind6344532005-06-28 20:45:14 -0700857 *
858 * Called with q->queue_lock held, and returns with it unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 */
Jens Axboe165125e2007-07-24 09:28:11 +0200860static struct request *get_request_wait(struct request_queue *q, int rw_flags,
Jens Axboe22e2c502005-06-27 10:55:12 +0200861 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
Jens Axboe1faa16d2009-04-06 14:48:01 +0200863 const bool is_sync = rw_is_sync(rw_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 struct request *rq;
865
Jens Axboe7749a8d2006-12-13 13:02:26 +0100866 rq = get_request(q, rw_flags, bio, GFP_NOIO);
Nick Piggin450991b2005-06-28 20:45:13 -0700867 while (!rq) {
868 DEFINE_WAIT(wait);
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200869 struct io_context *ioc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 struct request_list *rl = &q->rq;
871
Jens Axboe1faa16d2009-04-06 14:48:01 +0200872 prepare_to_wait_exclusive(&rl->wait[is_sync], &wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 TASK_UNINTERRUPTIBLE);
874
Jens Axboe1faa16d2009-04-06 14:48:01 +0200875 trace_block_sleeprq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200877 __generic_unplug_device(q);
878 spin_unlock_irq(q->queue_lock);
879 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200881 /*
882 * After sleeping, we become a "batching" process and
883 * will be able to allocate at least one request, and
884 * up to a big batch of them for a small period time.
885 * See ioc_batching, ioc_set_batching
886 */
887 ioc = current_io_context(GFP_NOIO, q->node);
888 ioc_set_batching(q, ioc);
Jens Axboe2056a782006-03-23 20:00:26 +0100889
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200890 spin_lock_irq(q->queue_lock);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200891 finish_wait(&rl->wait[is_sync], &wait);
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200892
893 rq = get_request(q, rw_flags, bio, GFP_NOIO);
894 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 return rq;
897}
898
Jens Axboe165125e2007-07-24 09:28:11 +0200899struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
901 struct request *rq;
902
903 BUG_ON(rw != READ && rw != WRITE);
904
Nick Piggind6344532005-06-28 20:45:14 -0700905 spin_lock_irq(q->queue_lock);
906 if (gfp_mask & __GFP_WAIT) {
Jens Axboe22e2c502005-06-27 10:55:12 +0200907 rq = get_request_wait(q, rw, NULL);
Nick Piggind6344532005-06-28 20:45:14 -0700908 } else {
Jens Axboe22e2c502005-06-27 10:55:12 +0200909 rq = get_request(q, rw, NULL, gfp_mask);
Nick Piggind6344532005-06-28 20:45:14 -0700910 if (!rq)
911 spin_unlock_irq(q->queue_lock);
912 }
913 /* q->queue_lock is unlocked at this point */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 return rq;
916}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917EXPORT_SYMBOL(blk_get_request);
918
919/**
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300920 * blk_make_request - given a bio, allocate a corresponding struct request.
Randy Dunlap8ebf9752009-06-11 20:00:41 -0700921 * @q: target request queue
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300922 * @bio: The bio describing the memory mappings that will be submitted for IO.
923 * It may be a chained-bio properly constructed by block/bio layer.
Randy Dunlap8ebf9752009-06-11 20:00:41 -0700924 * @gfp_mask: gfp flags to be used for memory allocation
Jens Axboedc72ef42006-07-20 14:54:05 +0200925 *
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300926 * blk_make_request is the parallel of generic_make_request for BLOCK_PC
927 * type commands. Where the struct request needs to be farther initialized by
928 * the caller. It is passed a &struct bio, which describes the memory info of
929 * the I/O transfer.
930 *
931 * The caller of blk_make_request must make sure that bi_io_vec
932 * are set to describe the memory buffers. That bio_data_dir() will return
933 * the needed direction of the request. (And all bio's in the passed bio-chain
934 * are properly set accordingly)
935 *
936 * If called under none-sleepable conditions, mapped bio buffers must not
937 * need bouncing, by calling the appropriate masked or flagged allocator,
938 * suitable for the target device. Otherwise the call to blk_queue_bounce will
939 * BUG.
Jens Axboe53674ac2009-05-19 19:52:35 +0200940 *
941 * WARNING: When allocating/cloning a bio-chain, careful consideration should be
942 * given to how you allocate bios. In particular, you cannot use __GFP_WAIT for
943 * anything but the first bio in the chain. Otherwise you risk waiting for IO
944 * completion of a bio that hasn't been submitted yet, thus resulting in a
945 * deadlock. Alternatively bios should be allocated using bio_kmalloc() instead
946 * of bio_alloc(), as that avoids the mempool deadlock.
947 * If possible a big IO should be split into smaller parts when allocation
948 * fails. Partial allocation should not be an error, or you risk a live-lock.
Jens Axboedc72ef42006-07-20 14:54:05 +0200949 */
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300950struct request *blk_make_request(struct request_queue *q, struct bio *bio,
951 gfp_t gfp_mask)
Jens Axboedc72ef42006-07-20 14:54:05 +0200952{
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300953 struct request *rq = blk_get_request(q, bio_data_dir(bio), gfp_mask);
954
955 if (unlikely(!rq))
956 return ERR_PTR(-ENOMEM);
957
958 for_each_bio(bio) {
959 struct bio *bounce_bio = bio;
960 int ret;
961
962 blk_queue_bounce(q, &bounce_bio);
963 ret = blk_rq_append_bio(q, rq, bounce_bio);
964 if (unlikely(ret)) {
965 blk_put_request(rq);
966 return ERR_PTR(ret);
967 }
968 }
969
970 return rq;
Jens Axboedc72ef42006-07-20 14:54:05 +0200971}
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300972EXPORT_SYMBOL(blk_make_request);
Jens Axboedc72ef42006-07-20 14:54:05 +0200973
974/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 * blk_requeue_request - put a request back on queue
976 * @q: request queue where request should be inserted
977 * @rq: request to be inserted
978 *
979 * Description:
980 * Drivers often keep queueing requests until the hardware cannot accept
981 * more, when that condition happens we need to put the request back
982 * on the queue. Must be called with queue lock held.
983 */
Jens Axboe165125e2007-07-24 09:28:11 +0200984void blk_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Jens Axboe242f9dc2008-09-14 05:55:09 -0700986 blk_delete_timer(rq);
987 blk_clear_rq_complete(rq);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100988 trace_block_rq_requeue(q, rq);
Jens Axboe2056a782006-03-23 20:00:26 +0100989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 if (blk_rq_tagged(rq))
991 blk_queue_end_tag(q, rq);
992
James Bottomleyba396a62009-05-27 14:17:08 +0200993 BUG_ON(blk_queued_rq(rq));
994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 elv_requeue_request(q, rq);
996}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997EXPORT_SYMBOL(blk_requeue_request);
998
999/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001000 * blk_insert_request - insert a special request into a request queue
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 * @q: request queue where request should be inserted
1002 * @rq: request to be inserted
1003 * @at_head: insert request at head or tail of queue
1004 * @data: private data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 *
1006 * Description:
1007 * Many block devices need to execute commands asynchronously, so they don't
1008 * block the whole kernel from preemption during request execution. This is
1009 * accomplished normally by inserting aritficial requests tagged as
Randy Dunlap710027a2008-08-19 20:13:11 +02001010 * REQ_TYPE_SPECIAL in to the corresponding request queue, and letting them
1011 * be scheduled for actual execution by the request queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 *
1013 * We have the option of inserting the head or the tail of the queue.
1014 * Typically we use the tail for new ioctls and so forth. We use the head
1015 * of the queue for things like a QUEUE_FULL message from a device, or a
1016 * host that is unable to accept a particular command.
1017 */
Jens Axboe165125e2007-07-24 09:28:11 +02001018void blk_insert_request(struct request_queue *q, struct request *rq,
Tejun Heo 867d1192005-04-24 02:06:05 -05001019 int at_head, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
Tejun Heo 867d1192005-04-24 02:06:05 -05001021 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 unsigned long flags;
1023
1024 /*
1025 * tell I/O scheduler that this isn't a regular read/write (ie it
1026 * must not attempt merges on this) and that it acts as a soft
1027 * barrier
1028 */
Jens Axboe4aff5e22006-08-10 08:44:47 +02001029 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 rq->special = data;
1032
1033 spin_lock_irqsave(q->queue_lock, flags);
1034
1035 /*
1036 * If command is tagged, release the tag
1037 */
Tejun Heo 867d1192005-04-24 02:06:05 -05001038 if (blk_rq_tagged(rq))
1039 blk_queue_end_tag(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Jerome Marchandb238b3d2007-10-23 15:05:46 +02001041 drive_stat_acct(rq, 1);
Tejun Heo 867d1192005-04-24 02:06:05 -05001042 __elv_add_request(q, rq, where, 0);
Tejun Heoa7f55792009-04-23 11:05:17 +09001043 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 spin_unlock_irqrestore(q->queue_lock, flags);
1045}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046EXPORT_SYMBOL(blk_insert_request);
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048/*
1049 * add-request adds a request to the linked list.
1050 * queue lock is held and interrupts disabled, as we muck with the
1051 * request queue list.
1052 */
Jens Axboe6728cb02008-01-31 13:03:55 +01001053static inline void add_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
Jerome Marchandb238b3d2007-10-23 15:05:46 +02001055 drive_stat_acct(req, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 /*
1058 * elevator indicated where it wants this request to be
1059 * inserted at elevator_merge time
1060 */
1061 __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
1062}
Jens Axboe6728cb02008-01-31 13:03:55 +01001063
Tejun Heo074a7ac2008-08-25 19:56:14 +09001064static void part_round_stats_single(int cpu, struct hd_struct *part,
1065 unsigned long now)
1066{
1067 if (now == part->stamp)
1068 return;
1069
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001070 if (part_in_flight(part)) {
Tejun Heo074a7ac2008-08-25 19:56:14 +09001071 __part_stat_add(cpu, part, time_in_queue,
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001072 part_in_flight(part) * (now - part->stamp));
Tejun Heo074a7ac2008-08-25 19:56:14 +09001073 __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
1074 }
1075 part->stamp = now;
1076}
1077
1078/**
Randy Dunlap496aa8a2008-10-16 07:46:23 +02001079 * part_round_stats() - Round off the performance stats on a struct disk_stats.
1080 * @cpu: cpu number for stats access
1081 * @part: target partition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 *
1083 * The average IO queue length and utilisation statistics are maintained
1084 * by observing the current state of the queue length and the amount of
1085 * time it has been in this state for.
1086 *
1087 * Normally, that accounting is done on IO completion, but that can result
1088 * in more than a second's worth of IO being accounted for within any one
1089 * second, leading to >100% utilisation. To deal with that, we call this
1090 * function to do a round-off before returning the results when reading
1091 * /proc/diskstats. This accounts immediately for all queue usage up to
1092 * the current jiffies and restarts the counters again.
1093 */
Tejun Heoc9959052008-08-25 19:47:21 +09001094void part_round_stats(int cpu, struct hd_struct *part)
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001095{
1096 unsigned long now = jiffies;
1097
Tejun Heo074a7ac2008-08-25 19:56:14 +09001098 if (part->partno)
1099 part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
1100 part_round_stats_single(cpu, part, now);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001101}
Tejun Heo074a7ac2008-08-25 19:56:14 +09001102EXPORT_SYMBOL_GPL(part_round_stats);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104/*
1105 * queue lock must be held
1106 */
Jens Axboe165125e2007-07-24 09:28:11 +02001107void __blk_put_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 if (unlikely(!q))
1110 return;
1111 if (unlikely(--req->ref_count))
1112 return;
1113
Tejun Heo8922e162005-10-20 16:23:44 +02001114 elv_completed_request(q, req);
1115
Boaz Harrosh1cd96c22009-03-24 12:35:07 +01001116 /* this is a bio leak */
1117 WARN_ON(req->bio != NULL);
1118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 /*
1120 * Request may not have originated from ll_rw_blk. if not,
1121 * it didn't come out of our reserved rq pools
1122 */
Jens Axboe49171e52006-08-10 08:59:11 +02001123 if (req->cmd_flags & REQ_ALLOCED) {
Jens Axboe1faa16d2009-04-06 14:48:01 +02001124 int is_sync = rq_is_sync(req) != 0;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001125 int priv = req->cmd_flags & REQ_ELVPRIV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 BUG_ON(!list_empty(&req->queuelist));
Jens Axboe98170642006-07-28 09:23:08 +02001128 BUG_ON(!hlist_unhashed(&req->hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 blk_free_request(q, req);
Jens Axboe1faa16d2009-04-06 14:48:01 +02001131 freed_request(q, is_sync, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 }
1133}
Mike Christie6e39b69e2005-11-11 05:30:24 -06001134EXPORT_SYMBOL_GPL(__blk_put_request);
1135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136void blk_put_request(struct request *req)
1137{
Tejun Heo8922e162005-10-20 16:23:44 +02001138 unsigned long flags;
Jens Axboe165125e2007-07-24 09:28:11 +02001139 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
FUJITA Tomonori52a93ba2008-07-15 21:21:45 +02001141 spin_lock_irqsave(q->queue_lock, flags);
1142 __blk_put_request(q, req);
1143 spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145EXPORT_SYMBOL(blk_put_request);
1146
Christoph Hellwig66ac0282010-06-18 16:59:42 +02001147/**
1148 * blk_add_request_payload - add a payload to a request
1149 * @rq: request to update
1150 * @page: page backing the payload
1151 * @len: length of the payload.
1152 *
1153 * This allows to later add a payload to an already submitted request by
1154 * a block driver. The driver needs to take care of freeing the payload
1155 * itself.
1156 *
1157 * Note that this is a quite horrible hack and nothing but handling of
1158 * discard requests should ever use it.
1159 */
1160void blk_add_request_payload(struct request *rq, struct page *page,
1161 unsigned int len)
1162{
1163 struct bio *bio = rq->bio;
1164
1165 bio->bi_io_vec->bv_page = page;
1166 bio->bi_io_vec->bv_offset = 0;
1167 bio->bi_io_vec->bv_len = len;
1168
1169 bio->bi_size = len;
1170 bio->bi_vcnt = 1;
1171 bio->bi_phys_segments = 1;
1172
1173 rq->__data_len = rq->resid_len = len;
1174 rq->nr_phys_segments = 1;
1175 rq->buffer = bio_data(bio);
1176}
1177EXPORT_SYMBOL_GPL(blk_add_request_payload);
1178
Jens Axboe86db1e22008-01-29 14:53:40 +01001179void init_request_from_bio(struct request *req, struct bio *bio)
Tejun Heo52d9e672006-01-06 09:49:58 +01001180{
Jens Axboec7c22e42008-09-13 20:26:01 +02001181 req->cpu = bio->bi_comp_cpu;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001182 req->cmd_type = REQ_TYPE_FS;
Tejun Heo52d9e672006-01-06 09:49:58 +01001183
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001184 req->cmd_flags |= bio->bi_rw & REQ_COMMON_MASK;
1185 if (bio->bi_rw & REQ_RAHEAD)
Tejun Heoa82afdf2009-07-03 17:48:16 +09001186 req->cmd_flags |= REQ_FAILFAST_MASK;
Jens Axboeb31dc662006-06-13 08:26:10 +02001187
Tejun Heo52d9e672006-01-06 09:49:58 +01001188 req->errors = 0;
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001189 req->__sector = bio->bi_sector;
Tejun Heo52d9e672006-01-06 09:49:58 +01001190 req->ioprio = bio_prio(bio);
NeilBrownbc1c56f2007-08-16 13:31:30 +02001191 blk_rq_bio_prep(req->q, req, bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001192}
1193
Jens Axboe644b2d92009-04-06 14:48:06 +02001194/*
1195 * Only disabling plugging for non-rotational devices if it does tagging
1196 * as well, otherwise we do need the proper merging
1197 */
1198static inline bool queue_should_plug(struct request_queue *q)
1199{
Jens Axboe79da06442010-02-23 08:40:43 +01001200 return !(blk_queue_nonrot(q) && blk_queue_tagged(q));
Jens Axboe644b2d92009-04-06 14:48:06 +02001201}
1202
Jens Axboe165125e2007-07-24 09:28:11 +02001203static int __make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
Nick Piggin450991b2005-06-28 20:45:13 -07001205 struct request *req;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001206 int el_ret;
1207 unsigned int bytes = bio->bi_size;
Jens Axboe51da90f2006-07-18 04:14:45 +02001208 const unsigned short prio = bio_prio(bio);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001209 const bool sync = (bio->bi_rw & REQ_SYNC);
1210 const bool unplug = (bio->bi_rw & REQ_UNPLUG);
Tejun Heo80a761f2009-07-03 17:48:17 +09001211 const unsigned int ff = bio->bi_rw & REQ_FAILFAST_MASK;
Jens Axboe7749a8d2006-12-13 13:02:26 +01001212 int rw_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001214 if ((bio->bi_rw & REQ_HARDBARRIER) &&
NeilBrowndb64f682009-06-30 09:35:44 +02001215 (q->next_ordered == QUEUE_ORDERED_NONE)) {
1216 bio_endio(bio, -EOPNOTSUPP);
1217 return 0;
1218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 /*
1220 * low level driver can indicate that it wants pages above a
1221 * certain limit bounced to low memory (ie for highmem, or even
1222 * ISA dma in theory)
1223 */
1224 blk_queue_bounce(q, &bio);
1225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 spin_lock_irq(q->queue_lock);
1227
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001228 if (unlikely((bio->bi_rw & REQ_HARDBARRIER)) || elv_queue_empty(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 goto get_rq;
1230
1231 el_ret = elv_merge(q, &req, bio);
1232 switch (el_ret) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001233 case ELEVATOR_BACK_MERGE:
1234 BUG_ON(!rq_mergeable(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Jens Axboe6728cb02008-01-31 13:03:55 +01001236 if (!ll_back_merge_fn(q, req, bio))
1237 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001239 trace_block_bio_backmerge(q, bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001240
Tejun Heo80a761f2009-07-03 17:48:17 +09001241 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1242 blk_rq_set_mixed_merge(req);
1243
Jens Axboe6728cb02008-01-31 13:03:55 +01001244 req->biotail->bi_next = bio;
1245 req->biotail = bio;
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001246 req->__data_len += bytes;
Jens Axboe6728cb02008-01-31 13:03:55 +01001247 req->ioprio = ioprio_best(req->ioprio, prio);
Jens Axboeab780f12008-08-26 10:25:02 +02001248 if (!blk_rq_cpu_valid(req))
1249 req->cpu = bio->bi_comp_cpu;
Jens Axboe6728cb02008-01-31 13:03:55 +01001250 drive_stat_acct(req, 0);
Divyesh Shah812d4022010-04-08 21:14:23 -07001251 elv_bio_merged(q, req, bio);
Jens Axboe6728cb02008-01-31 13:03:55 +01001252 if (!attempt_back_merge(q, req))
1253 elv_merged_request(q, req, el_ret);
1254 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Jens Axboe6728cb02008-01-31 13:03:55 +01001256 case ELEVATOR_FRONT_MERGE:
1257 BUG_ON(!rq_mergeable(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Jens Axboe6728cb02008-01-31 13:03:55 +01001259 if (!ll_front_merge_fn(q, req, bio))
1260 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001262 trace_block_bio_frontmerge(q, bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001263
Tejun Heo80a761f2009-07-03 17:48:17 +09001264 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff) {
1265 blk_rq_set_mixed_merge(req);
1266 req->cmd_flags &= ~REQ_FAILFAST_MASK;
1267 req->cmd_flags |= ff;
1268 }
1269
Jens Axboe6728cb02008-01-31 13:03:55 +01001270 bio->bi_next = req->bio;
1271 req->bio = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Jens Axboe6728cb02008-01-31 13:03:55 +01001273 /*
1274 * may not be valid. if the low level driver said
1275 * it didn't need a bounce buffer then it better
1276 * not touch req->buffer either...
1277 */
1278 req->buffer = bio_data(bio);
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001279 req->__sector = bio->bi_sector;
1280 req->__data_len += bytes;
Jens Axboe6728cb02008-01-31 13:03:55 +01001281 req->ioprio = ioprio_best(req->ioprio, prio);
Jens Axboeab780f12008-08-26 10:25:02 +02001282 if (!blk_rq_cpu_valid(req))
1283 req->cpu = bio->bi_comp_cpu;
Jens Axboe6728cb02008-01-31 13:03:55 +01001284 drive_stat_acct(req, 0);
Divyesh Shah812d4022010-04-08 21:14:23 -07001285 elv_bio_merged(q, req, bio);
Jens Axboe6728cb02008-01-31 13:03:55 +01001286 if (!attempt_front_merge(q, req))
1287 elv_merged_request(q, req, el_ret);
1288 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Jens Axboe6728cb02008-01-31 13:03:55 +01001290 /* ELV_NO_MERGE: elevator says don't/can't merge. */
1291 default:
1292 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
1294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295get_rq:
Nick Piggin450991b2005-06-28 20:45:13 -07001296 /*
Jens Axboe7749a8d2006-12-13 13:02:26 +01001297 * This sync check and mask will be re-done in init_request_from_bio(),
1298 * but we need to set it earlier to expose the sync flag to the
1299 * rq allocator and io schedulers.
1300 */
1301 rw_flags = bio_data_dir(bio);
1302 if (sync)
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001303 rw_flags |= REQ_SYNC;
Jens Axboe7749a8d2006-12-13 13:02:26 +01001304
1305 /*
Nick Piggin450991b2005-06-28 20:45:13 -07001306 * Grab a free request. This is might sleep but can not fail.
Nick Piggind6344532005-06-28 20:45:14 -07001307 * Returns with the queue unlocked.
Nick Piggin450991b2005-06-28 20:45:13 -07001308 */
Jens Axboe7749a8d2006-12-13 13:02:26 +01001309 req = get_request_wait(q, rw_flags, bio);
Nick Piggind6344532005-06-28 20:45:14 -07001310
Nick Piggin450991b2005-06-28 20:45:13 -07001311 /*
1312 * After dropping the lock and possibly sleeping here, our request
1313 * may now be mergeable after it had proven unmergeable (above).
1314 * We don't worry about that case for efficiency. It won't happen
1315 * often, and the elevators are able to handle it.
1316 */
Tejun Heo52d9e672006-01-06 09:49:58 +01001317 init_request_from_bio(req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Nick Piggin450991b2005-06-28 20:45:13 -07001319 spin_lock_irq(q->queue_lock);
Jens Axboec7c22e42008-09-13 20:26:01 +02001320 if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags) ||
1321 bio_flagged(bio, BIO_CPU_AFFINE))
1322 req->cpu = blk_cpu_to_group(smp_processor_id());
Jens Axboe644b2d92009-04-06 14:48:06 +02001323 if (queue_should_plug(q) && elv_queue_empty(q))
Nick Piggin450991b2005-06-28 20:45:13 -07001324 blk_plug_device(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 add_request(q, req);
1326out:
Jens Axboe644b2d92009-04-06 14:48:06 +02001327 if (unplug || !queue_should_plug(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 __generic_unplug_device(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 spin_unlock_irq(q->queue_lock);
1330 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331}
1332
1333/*
1334 * If bio->bi_dev is a partition, remap the location
1335 */
1336static inline void blk_partition_remap(struct bio *bio)
1337{
1338 struct block_device *bdev = bio->bi_bdev;
1339
Jens Axboebf2de6f2007-09-27 13:01:25 +02001340 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 struct hd_struct *p = bdev->bd_part;
Jens Axboea3623572005-11-01 09:26:16 +01001342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 bio->bi_sector += p->start_sect;
1344 bio->bi_bdev = bdev->bd_contains;
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001345
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001346 trace_block_remap(bdev_get_queue(bio->bi_bdev), bio,
Alan D. Brunelle22a7c312009-05-04 16:35:08 -04001347 bdev->bd_dev,
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001348 bio->bi_sector - p->start_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
1350}
1351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352static void handle_bad_sector(struct bio *bio)
1353{
1354 char b[BDEVNAME_SIZE];
1355
1356 printk(KERN_INFO "attempt to access beyond end of device\n");
1357 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1358 bdevname(bio->bi_bdev, b),
1359 bio->bi_rw,
1360 (unsigned long long)bio->bi_sector + bio_sectors(bio),
1361 (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
1362
1363 set_bit(BIO_EOF, &bio->bi_flags);
1364}
1365
Akinobu Mitac17bb492006-12-08 02:39:46 -08001366#ifdef CONFIG_FAIL_MAKE_REQUEST
1367
1368static DECLARE_FAULT_ATTR(fail_make_request);
1369
1370static int __init setup_fail_make_request(char *str)
1371{
1372 return setup_fault_attr(&fail_make_request, str);
1373}
1374__setup("fail_make_request=", setup_fail_make_request);
1375
1376static int should_fail_request(struct bio *bio)
1377{
Tejun Heoeddb2e22008-08-25 19:56:13 +09001378 struct hd_struct *part = bio->bi_bdev->bd_part;
1379
1380 if (part_to_disk(part)->part0.make_it_fail || part->make_it_fail)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001381 return should_fail(&fail_make_request, bio->bi_size);
1382
1383 return 0;
1384}
1385
1386static int __init fail_make_request_debugfs(void)
1387{
1388 return init_fault_attr_dentries(&fail_make_request,
1389 "fail_make_request");
1390}
1391
1392late_initcall(fail_make_request_debugfs);
1393
1394#else /* CONFIG_FAIL_MAKE_REQUEST */
1395
1396static inline int should_fail_request(struct bio *bio)
1397{
1398 return 0;
1399}
1400
1401#endif /* CONFIG_FAIL_MAKE_REQUEST */
1402
Jens Axboec07e2b42007-07-18 13:27:58 +02001403/*
1404 * Check whether this bio extends beyond the end of the device.
1405 */
1406static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1407{
1408 sector_t maxsector;
1409
1410 if (!nr_sectors)
1411 return 0;
1412
1413 /* Test device or partition size, when known. */
1414 maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
1415 if (maxsector) {
1416 sector_t sector = bio->bi_sector;
1417
1418 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1419 /*
1420 * This may well happen - the kernel calls bread()
1421 * without checking the size of the device, e.g., when
1422 * mounting a device.
1423 */
1424 handle_bad_sector(bio);
1425 return 1;
1426 }
1427 }
1428
1429 return 0;
1430}
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001433 * generic_make_request - hand a buffer to its device driver for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 * @bio: The bio describing the location in memory and on the device.
1435 *
1436 * generic_make_request() is used to make I/O requests of block
1437 * devices. It is passed a &struct bio, which describes the I/O that needs
1438 * to be done.
1439 *
1440 * generic_make_request() does not return any status. The
1441 * success/failure status of the request, along with notification of
1442 * completion, is delivered asynchronously through the bio->bi_end_io
1443 * function described (one day) else where.
1444 *
1445 * The caller of generic_make_request must make sure that bi_io_vec
1446 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1447 * set to describe the device address, and the
1448 * bi_end_io and optionally bi_private are set to describe how
1449 * completion notification should be signaled.
1450 *
1451 * generic_make_request and the drivers it calls may use bi_next if this
1452 * bio happens to be merged with someone else, and may change bi_dev and
1453 * bi_sector for remaps as it sees fit. So the values of these fields
1454 * should NOT be depended on after the call to generic_make_request.
1455 */
Neil Brownd89d8792007-05-01 09:53:42 +02001456static inline void __generic_make_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
Jens Axboe165125e2007-07-24 09:28:11 +02001458 struct request_queue *q;
NeilBrown5ddfe962006-10-30 22:07:21 -08001459 sector_t old_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 int ret, nr_sectors = bio_sectors(bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001461 dev_t old_dev;
Jens Axboe51fd77b2007-11-02 08:49:08 +01001462 int err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
1464 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Jens Axboec07e2b42007-07-18 13:27:58 +02001466 if (bio_check_eod(bio, nr_sectors))
1467 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
1469 /*
1470 * Resolve the mapping until finished. (drivers are
1471 * still free to implement/resolve their own stacking
1472 * by explicitly returning 0)
1473 *
1474 * NOTE: we don't repeat the blk_size check for each new device.
1475 * Stacking drivers are expected to know what they are doing.
1476 */
NeilBrown5ddfe962006-10-30 22:07:21 -08001477 old_sector = -1;
Jens Axboe2056a782006-03-23 20:00:26 +01001478 old_dev = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 do {
1480 char b[BDEVNAME_SIZE];
1481
1482 q = bdev_get_queue(bio->bi_bdev);
Tejun Heoa7384672008-11-28 13:32:03 +09001483 if (unlikely(!q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 printk(KERN_ERR
1485 "generic_make_request: Trying to access "
1486 "nonexistent block-device %s (%Lu)\n",
1487 bdevname(bio->bi_bdev, b),
1488 (long long) bio->bi_sector);
Tejun Heoa7384672008-11-28 13:32:03 +09001489 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 }
1491
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001492 if (unlikely(!(bio->bi_rw & REQ_DISCARD) &&
Christoph Hellwig67efc922009-09-30 13:54:20 +02001493 nr_sectors > queue_max_hw_sectors(q))) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001494 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
Martin K. Petersenae03bf62009-05-22 17:17:50 -04001495 bdevname(bio->bi_bdev, b),
1496 bio_sectors(bio),
1497 queue_max_hw_sectors(q));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 goto end_io;
1499 }
1500
Nick Pigginfde6ad22005-06-23 00:08:53 -07001501 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 goto end_io;
1503
Akinobu Mitac17bb492006-12-08 02:39:46 -08001504 if (should_fail_request(bio))
1505 goto end_io;
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 /*
1508 * If this device has partitions, remap block n
1509 * of partition p to block n+start(p) of the disk.
1510 */
1511 blk_partition_remap(bio);
1512
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +02001513 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio))
1514 goto end_io;
1515
NeilBrown5ddfe962006-10-30 22:07:21 -08001516 if (old_sector != -1)
Alan D. Brunelle22a7c312009-05-04 16:35:08 -04001517 trace_block_remap(q, bio, old_dev, old_sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001518
NeilBrown5ddfe962006-10-30 22:07:21 -08001519 old_sector = bio->bi_sector;
Jens Axboe2056a782006-03-23 20:00:26 +01001520 old_dev = bio->bi_bdev->bd_dev;
1521
Jens Axboec07e2b42007-07-18 13:27:58 +02001522 if (bio_check_eod(bio, nr_sectors))
1523 goto end_io;
Tejun Heoa7384672008-11-28 13:32:03 +09001524
Adrian Hunter8d57a982010-08-11 14:17:49 -07001525 if ((bio->bi_rw & REQ_DISCARD) &&
1526 (!blk_queue_discard(q) ||
1527 ((bio->bi_rw & REQ_SECURE) &&
1528 !blk_queue_secdiscard(q)))) {
Jens Axboe51fd77b2007-11-02 08:49:08 +01001529 err = -EOPNOTSUPP;
1530 goto end_io;
1531 }
NeilBrown5ddfe962006-10-30 22:07:21 -08001532
Vivek Goyale43473b2010-09-15 17:06:35 -04001533 blk_throtl_bio(q, &bio);
1534
1535 /*
1536 * If bio = NULL, bio has been throttled and will be submitted
1537 * later.
1538 */
1539 if (!bio)
1540 break;
1541
Minchan Kim01edede2009-09-08 21:56:38 +02001542 trace_block_bio_queue(q, bio);
1543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 ret = q->make_request_fn(q, bio);
1545 } while (ret);
Tejun Heoa7384672008-11-28 13:32:03 +09001546
1547 return;
1548
1549end_io:
1550 bio_endio(bio, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551}
1552
Neil Brownd89d8792007-05-01 09:53:42 +02001553/*
1554 * We only want one ->make_request_fn to be active at a time,
1555 * else stack usage with stacked devices could be a problem.
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001556 * So use current->bio_list to keep a list of requests
Neil Brownd89d8792007-05-01 09:53:42 +02001557 * submited by a make_request_fn function.
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001558 * current->bio_list is also used as a flag to say if
Neil Brownd89d8792007-05-01 09:53:42 +02001559 * generic_make_request is currently active in this task or not.
1560 * If it is NULL, then no make_request is active. If it is non-NULL,
1561 * then a make_request is active, and new requests should be added
1562 * at the tail
1563 */
1564void generic_make_request(struct bio *bio)
1565{
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001566 struct bio_list bio_list_on_stack;
1567
1568 if (current->bio_list) {
Neil Brownd89d8792007-05-01 09:53:42 +02001569 /* make_request is active */
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001570 bio_list_add(current->bio_list, bio);
Neil Brownd89d8792007-05-01 09:53:42 +02001571 return;
1572 }
1573 /* following loop may be a bit non-obvious, and so deserves some
1574 * explanation.
1575 * Before entering the loop, bio->bi_next is NULL (as all callers
1576 * ensure that) so we have a list with a single bio.
1577 * We pretend that we have just taken it off a longer list, so
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001578 * we assign bio_list to a pointer to the bio_list_on_stack,
1579 * thus initialising the bio_list of new bios to be
Neil Brownd89d8792007-05-01 09:53:42 +02001580 * added. __generic_make_request may indeed add some more bios
1581 * through a recursive call to generic_make_request. If it
1582 * did, we find a non-NULL value in bio_list and re-enter the loop
1583 * from the top. In this case we really did just take the bio
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001584 * of the top of the list (no pretending) and so remove it from
1585 * bio_list, and call into __generic_make_request again.
Neil Brownd89d8792007-05-01 09:53:42 +02001586 *
1587 * The loop was structured like this to make only one call to
1588 * __generic_make_request (which is important as it is large and
1589 * inlined) and to keep the structure simple.
1590 */
1591 BUG_ON(bio->bi_next);
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001592 bio_list_init(&bio_list_on_stack);
1593 current->bio_list = &bio_list_on_stack;
Neil Brownd89d8792007-05-01 09:53:42 +02001594 do {
Neil Brownd89d8792007-05-01 09:53:42 +02001595 __generic_make_request(bio);
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001596 bio = bio_list_pop(current->bio_list);
Neil Brownd89d8792007-05-01 09:53:42 +02001597 } while (bio);
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001598 current->bio_list = NULL; /* deactivate */
Neil Brownd89d8792007-05-01 09:53:42 +02001599}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600EXPORT_SYMBOL(generic_make_request);
1601
1602/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001603 * submit_bio - submit a bio to the block device layer for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1605 * @bio: The &struct bio which describes the I/O
1606 *
1607 * submit_bio() is very similar in purpose to generic_make_request(), and
1608 * uses that function to do most of the work. Both are fairly rough
Randy Dunlap710027a2008-08-19 20:13:11 +02001609 * interfaces; @bio must be presetup and ready for I/O.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 *
1611 */
1612void submit_bio(int rw, struct bio *bio)
1613{
1614 int count = bio_sectors(bio);
1615
Jens Axboe22e2c502005-06-27 10:55:12 +02001616 bio->bi_rw |= rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Jens Axboebf2de6f2007-09-27 13:01:25 +02001618 /*
1619 * If it's a regular read/write or a barrier with data attached,
1620 * go through the normal accounting stuff before submission.
1621 */
Jens Axboe3ffb52e2010-06-29 13:33:38 +02001622 if (bio_has_data(bio) && !(rw & REQ_DISCARD)) {
Jens Axboebf2de6f2007-09-27 13:01:25 +02001623 if (rw & WRITE) {
1624 count_vm_events(PGPGOUT, count);
1625 } else {
1626 task_io_account_read(bio->bi_size);
1627 count_vm_events(PGPGIN, count);
1628 }
1629
1630 if (unlikely(block_dump)) {
1631 char b[BDEVNAME_SIZE];
San Mehat8dcbdc72010-09-14 08:48:01 +02001632 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s (%u sectors)\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001633 current->comm, task_pid_nr(current),
Jens Axboebf2de6f2007-09-27 13:01:25 +02001634 (rw & WRITE) ? "WRITE" : "READ",
1635 (unsigned long long)bio->bi_sector,
San Mehat8dcbdc72010-09-14 08:48:01 +02001636 bdevname(bio->bi_bdev, b),
1637 count);
Jens Axboebf2de6f2007-09-27 13:01:25 +02001638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 }
1640
1641 generic_make_request(bio);
1642}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643EXPORT_SYMBOL(submit_bio);
1644
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001645/**
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001646 * blk_rq_check_limits - Helper function to check a request for the queue limit
1647 * @q: the queue
1648 * @rq: the request being checked
1649 *
1650 * Description:
1651 * @rq may have been made based on weaker limitations of upper-level queues
1652 * in request stacking drivers, and it may violate the limitation of @q.
1653 * Since the block layer and the underlying device driver trust @rq
1654 * after it is inserted to @q, it should be checked against @q before
1655 * the insertion using this generic function.
1656 *
1657 * This function should also be useful for request stacking drivers
1658 * in some cases below, so export this fuction.
1659 * Request stacking drivers like request-based dm may change the queue
1660 * limits while requests are in the queue (e.g. dm's table swapping).
1661 * Such request stacking drivers should check those requests agaist
1662 * the new queue limits again when they dispatch those requests,
1663 * although such checkings are also done against the old queue limits
1664 * when submitting requests.
1665 */
1666int blk_rq_check_limits(struct request_queue *q, struct request *rq)
1667{
ike Snitzer33839772010-08-08 12:11:33 -04001668 if (rq->cmd_flags & REQ_DISCARD)
1669 return 0;
1670
Martin K. Petersenae03bf62009-05-22 17:17:50 -04001671 if (blk_rq_sectors(rq) > queue_max_sectors(q) ||
1672 blk_rq_bytes(rq) > queue_max_hw_sectors(q) << 9) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001673 printk(KERN_ERR "%s: over max size limit.\n", __func__);
1674 return -EIO;
1675 }
1676
1677 /*
1678 * queue's settings related to segment counting like q->bounce_pfn
1679 * may differ from that of other stacking queues.
1680 * Recalculate it to check the request correctly on this queue's
1681 * limitation.
1682 */
1683 blk_recalc_rq_segments(rq);
Martin K. Petersen8a783622010-02-26 00:20:39 -05001684 if (rq->nr_phys_segments > queue_max_segments(q)) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001685 printk(KERN_ERR "%s: over max segments limit.\n", __func__);
1686 return -EIO;
1687 }
1688
1689 return 0;
1690}
1691EXPORT_SYMBOL_GPL(blk_rq_check_limits);
1692
1693/**
1694 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
1695 * @q: the queue to submit the request
1696 * @rq: the request being queued
1697 */
1698int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
1699{
1700 unsigned long flags;
1701
1702 if (blk_rq_check_limits(q, rq))
1703 return -EIO;
1704
1705#ifdef CONFIG_FAIL_MAKE_REQUEST
1706 if (rq->rq_disk && rq->rq_disk->part0.make_it_fail &&
1707 should_fail(&fail_make_request, blk_rq_bytes(rq)))
1708 return -EIO;
1709#endif
1710
1711 spin_lock_irqsave(q->queue_lock, flags);
1712
1713 /*
1714 * Submitting request must be dequeued before calling this function
1715 * because it will be linked to another request_queue
1716 */
1717 BUG_ON(blk_queued_rq(rq));
1718
1719 drive_stat_acct(rq, 1);
1720 __elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 0);
1721
1722 spin_unlock_irqrestore(q->queue_lock, flags);
1723
1724 return 0;
1725}
1726EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
1727
Tejun Heo80a761f2009-07-03 17:48:17 +09001728/**
1729 * blk_rq_err_bytes - determine number of bytes till the next failure boundary
1730 * @rq: request to examine
1731 *
1732 * Description:
1733 * A request could be merge of IOs which require different failure
1734 * handling. This function determines the number of bytes which
1735 * can be failed from the beginning of the request without
1736 * crossing into area which need to be retried further.
1737 *
1738 * Return:
1739 * The number of bytes to fail.
1740 *
1741 * Context:
1742 * queue_lock must be held.
1743 */
1744unsigned int blk_rq_err_bytes(const struct request *rq)
1745{
1746 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
1747 unsigned int bytes = 0;
1748 struct bio *bio;
1749
1750 if (!(rq->cmd_flags & REQ_MIXED_MERGE))
1751 return blk_rq_bytes(rq);
1752
1753 /*
1754 * Currently the only 'mixing' which can happen is between
1755 * different fastfail types. We can safely fail portions
1756 * which have all the failfast bits that the first one has -
1757 * the ones which are at least as eager to fail as the first
1758 * one.
1759 */
1760 for (bio = rq->bio; bio; bio = bio->bi_next) {
1761 if ((bio->bi_rw & ff) != ff)
1762 break;
1763 bytes += bio->bi_size;
1764 }
1765
1766 /* this could lead to infinite loop */
1767 BUG_ON(blk_rq_bytes(rq) && !bytes);
1768 return bytes;
1769}
1770EXPORT_SYMBOL_GPL(blk_rq_err_bytes);
1771
Jens Axboebc58ba92009-01-23 10:54:44 +01001772static void blk_account_io_completion(struct request *req, unsigned int bytes)
1773{
Jens Axboec2553b52009-04-24 08:10:11 +02001774 if (blk_do_io_stat(req)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01001775 const int rw = rq_data_dir(req);
1776 struct hd_struct *part;
1777 int cpu;
1778
1779 cpu = part_stat_lock();
Tejun Heo83096eb2009-05-07 22:24:39 +09001780 part = disk_map_sector_rcu(req->rq_disk, blk_rq_pos(req));
Jens Axboebc58ba92009-01-23 10:54:44 +01001781 part_stat_add(cpu, part, sectors[rw], bytes >> 9);
1782 part_stat_unlock();
1783 }
1784}
1785
1786static void blk_account_io_done(struct request *req)
1787{
Jens Axboebc58ba92009-01-23 10:54:44 +01001788 /*
1789 * Account IO completion. bar_rq isn't accounted as a normal
1790 * IO on queueing nor completion. Accounting the containing
1791 * request is enough.
1792 */
Jens Axboec2553b52009-04-24 08:10:11 +02001793 if (blk_do_io_stat(req) && req != &req->q->bar_rq) {
Jens Axboebc58ba92009-01-23 10:54:44 +01001794 unsigned long duration = jiffies - req->start_time;
1795 const int rw = rq_data_dir(req);
1796 struct hd_struct *part;
1797 int cpu;
1798
1799 cpu = part_stat_lock();
Tejun Heo83096eb2009-05-07 22:24:39 +09001800 part = disk_map_sector_rcu(req->rq_disk, blk_rq_pos(req));
Jens Axboebc58ba92009-01-23 10:54:44 +01001801
1802 part_stat_inc(cpu, part, ios[rw]);
1803 part_stat_add(cpu, part, ticks[rw], duration);
1804 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001805 part_dec_in_flight(part, rw);
Jens Axboebc58ba92009-01-23 10:54:44 +01001806
1807 part_stat_unlock();
1808 }
1809}
1810
Tejun Heo53a08802008-12-03 12:41:26 +01001811/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09001812 * blk_peek_request - peek at the top of a request queue
1813 * @q: request queue to peek at
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001814 *
1815 * Description:
Tejun Heo9934c8c2009-05-08 11:54:16 +09001816 * Return the request at the top of @q. The returned request
1817 * should be started using blk_start_request() before LLD starts
1818 * processing it.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001819 *
1820 * Return:
Tejun Heo9934c8c2009-05-08 11:54:16 +09001821 * Pointer to the request at the top of @q if available. Null
1822 * otherwise.
1823 *
1824 * Context:
1825 * queue_lock must be held.
1826 */
1827struct request *blk_peek_request(struct request_queue *q)
Tejun Heo158dbda2009-04-23 11:05:18 +09001828{
1829 struct request *rq;
1830 int ret;
1831
1832 while ((rq = __elv_next_request(q)) != NULL) {
1833 if (!(rq->cmd_flags & REQ_STARTED)) {
1834 /*
1835 * This is the first time the device driver
1836 * sees this request (possibly after
1837 * requeueing). Notify IO scheduler.
1838 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001839 if (rq->cmd_flags & REQ_SORTED)
Tejun Heo158dbda2009-04-23 11:05:18 +09001840 elv_activate_rq(q, rq);
1841
1842 /*
1843 * just mark as started even if we don't start
1844 * it, a request that has been delayed should
1845 * not be passed by new incoming requests
1846 */
1847 rq->cmd_flags |= REQ_STARTED;
1848 trace_block_rq_issue(q, rq);
1849 }
1850
1851 if (!q->boundary_rq || q->boundary_rq == rq) {
1852 q->end_sector = rq_end_sector(rq);
1853 q->boundary_rq = NULL;
1854 }
1855
1856 if (rq->cmd_flags & REQ_DONTPREP)
1857 break;
1858
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001859 if (q->dma_drain_size && blk_rq_bytes(rq)) {
Tejun Heo158dbda2009-04-23 11:05:18 +09001860 /*
1861 * make sure space for the drain appears we
1862 * know we can do this because max_hw_segments
1863 * has been adjusted to be one fewer than the
1864 * device can handle
1865 */
1866 rq->nr_phys_segments++;
1867 }
1868
1869 if (!q->prep_rq_fn)
1870 break;
1871
1872 ret = q->prep_rq_fn(q, rq);
1873 if (ret == BLKPREP_OK) {
1874 break;
1875 } else if (ret == BLKPREP_DEFER) {
1876 /*
1877 * the request may have been (partially) prepped.
1878 * we need to keep this request in the front to
1879 * avoid resource deadlock. REQ_STARTED will
1880 * prevent other fs requests from passing this one.
1881 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001882 if (q->dma_drain_size && blk_rq_bytes(rq) &&
Tejun Heo158dbda2009-04-23 11:05:18 +09001883 !(rq->cmd_flags & REQ_DONTPREP)) {
1884 /*
1885 * remove the space for the drain we added
1886 * so that we don't add it again
1887 */
1888 --rq->nr_phys_segments;
1889 }
1890
1891 rq = NULL;
1892 break;
1893 } else if (ret == BLKPREP_KILL) {
1894 rq->cmd_flags |= REQ_QUIET;
James Bottomleyc143dc92009-05-30 06:43:49 +02001895 /*
1896 * Mark this request as started so we don't trigger
1897 * any debug logic in the end I/O path.
1898 */
1899 blk_start_request(rq);
Tejun Heo40cbbb72009-04-23 11:05:19 +09001900 __blk_end_request_all(rq, -EIO);
Tejun Heo158dbda2009-04-23 11:05:18 +09001901 } else {
1902 printk(KERN_ERR "%s: bad return=%d\n", __func__, ret);
1903 break;
1904 }
1905 }
1906
1907 return rq;
1908}
Tejun Heo9934c8c2009-05-08 11:54:16 +09001909EXPORT_SYMBOL(blk_peek_request);
Tejun Heo158dbda2009-04-23 11:05:18 +09001910
Tejun Heo9934c8c2009-05-08 11:54:16 +09001911void blk_dequeue_request(struct request *rq)
Tejun Heo158dbda2009-04-23 11:05:18 +09001912{
Tejun Heo9934c8c2009-05-08 11:54:16 +09001913 struct request_queue *q = rq->q;
1914
Tejun Heo158dbda2009-04-23 11:05:18 +09001915 BUG_ON(list_empty(&rq->queuelist));
1916 BUG_ON(ELV_ON_HASH(rq));
1917
1918 list_del_init(&rq->queuelist);
1919
1920 /*
1921 * the time frame between a request being removed from the lists
1922 * and to it is freed is accounted as io that is in progress at
1923 * the driver side.
1924 */
Divyesh Shah91952912010-04-01 15:01:41 -07001925 if (blk_account_rq(rq)) {
Jens Axboe0a7ae2f2009-05-20 08:54:31 +02001926 q->in_flight[rq_is_sync(rq)]++;
Divyesh Shah91952912010-04-01 15:01:41 -07001927 set_io_start_time_ns(rq);
1928 }
Tejun Heo158dbda2009-04-23 11:05:18 +09001929}
1930
Tejun Heo5efccd12009-04-23 11:05:18 +09001931/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09001932 * blk_start_request - start request processing on the driver
1933 * @req: request to dequeue
1934 *
1935 * Description:
1936 * Dequeue @req and start timeout timer on it. This hands off the
1937 * request to the driver.
1938 *
1939 * Block internal functions which don't want to start timer should
1940 * call blk_dequeue_request().
1941 *
1942 * Context:
1943 * queue_lock must be held.
1944 */
1945void blk_start_request(struct request *req)
1946{
1947 blk_dequeue_request(req);
1948
1949 /*
Tejun Heo5f49f632009-05-19 18:33:05 +09001950 * We are now handing the request to the hardware, initialize
1951 * resid_len to full count and add the timeout handler.
Tejun Heo9934c8c2009-05-08 11:54:16 +09001952 */
Tejun Heo5f49f632009-05-19 18:33:05 +09001953 req->resid_len = blk_rq_bytes(req);
FUJITA Tomonoridbb66c42009-06-09 05:47:10 +02001954 if (unlikely(blk_bidi_rq(req)))
1955 req->next_rq->resid_len = blk_rq_bytes(req->next_rq);
1956
Tejun Heo9934c8c2009-05-08 11:54:16 +09001957 blk_add_timer(req);
1958}
1959EXPORT_SYMBOL(blk_start_request);
1960
1961/**
1962 * blk_fetch_request - fetch a request from a request queue
1963 * @q: request queue to fetch a request from
1964 *
1965 * Description:
1966 * Return the request at the top of @q. The request is started on
1967 * return and LLD can start processing it immediately.
1968 *
1969 * Return:
1970 * Pointer to the request at the top of @q if available. Null
1971 * otherwise.
1972 *
1973 * Context:
1974 * queue_lock must be held.
1975 */
1976struct request *blk_fetch_request(struct request_queue *q)
1977{
1978 struct request *rq;
1979
1980 rq = blk_peek_request(q);
1981 if (rq)
1982 blk_start_request(rq);
1983 return rq;
1984}
1985EXPORT_SYMBOL(blk_fetch_request);
1986
1987/**
Tejun Heo2e60e022009-04-23 11:05:18 +09001988 * blk_update_request - Special helper function for request stacking drivers
Randy Dunlap8ebf9752009-06-11 20:00:41 -07001989 * @req: the request being processed
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001990 * @error: %0 for success, < %0 for error
Randy Dunlap8ebf9752009-06-11 20:00:41 -07001991 * @nr_bytes: number of bytes to complete @req
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001992 *
1993 * Description:
Randy Dunlap8ebf9752009-06-11 20:00:41 -07001994 * Ends I/O on a number of bytes attached to @req, but doesn't complete
1995 * the request structure even if @req doesn't have leftover.
1996 * If @req has leftover, sets it up for the next range of segments.
Tejun Heo2e60e022009-04-23 11:05:18 +09001997 *
1998 * This special helper function is only for request stacking drivers
1999 * (e.g. request-based dm) so that they can handle partial completion.
2000 * Actual device drivers should use blk_end_request instead.
2001 *
2002 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
2003 * %false return from this function.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002004 *
2005 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002006 * %false - this request doesn't have any more data
2007 * %true - this request has more data
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002008 **/
Tejun Heo2e60e022009-04-23 11:05:18 +09002009bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010{
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05002011 int total_bytes, bio_nbytes, next_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 struct bio *bio;
2013
Tejun Heo2e60e022009-04-23 11:05:18 +09002014 if (!req->bio)
2015 return false;
2016
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01002017 trace_block_rq_complete(req->q, req);
Jens Axboe2056a782006-03-23 20:00:26 +01002018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 /*
Tejun Heo6f414692009-04-19 07:00:41 +09002020 * For fs requests, rq is just carrier of independent bio's
2021 * and each partial completion should be handled separately.
2022 * Reset per-request error on each partial completion.
2023 *
2024 * TODO: tj: This is too subtle. It would be better to let
2025 * low level drivers do what they see fit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002027 if (req->cmd_type == REQ_TYPE_FS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 req->errors = 0;
2029
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002030 if (error && req->cmd_type == REQ_TYPE_FS &&
2031 !(req->cmd_flags & REQ_QUIET)) {
Jens Axboe6728cb02008-01-31 13:03:55 +01002032 printk(KERN_ERR "end_request: I/O error, dev %s, sector %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 req->rq_disk ? req->rq_disk->disk_name : "?",
Tejun Heo83096eb2009-05-07 22:24:39 +09002034 (unsigned long long)blk_rq_pos(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 }
2036
Jens Axboebc58ba92009-01-23 10:54:44 +01002037 blk_account_io_completion(req, nr_bytes);
Jens Axboed72d9042005-11-01 08:35:42 +01002038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 total_bytes = bio_nbytes = 0;
2040 while ((bio = req->bio) != NULL) {
2041 int nbytes;
2042
2043 if (nr_bytes >= bio->bi_size) {
2044 req->bio = bio->bi_next;
2045 nbytes = bio->bi_size;
NeilBrown5bb23a62007-09-27 12:46:13 +02002046 req_bio_endio(req, bio, nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 next_idx = 0;
2048 bio_nbytes = 0;
2049 } else {
2050 int idx = bio->bi_idx + next_idx;
2051
Kazuhisa Ichikawaaf498d72009-05-12 13:27:45 +02002052 if (unlikely(idx >= bio->bi_vcnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 blk_dump_rq_flags(req, "__end_that");
Jens Axboe6728cb02008-01-31 13:03:55 +01002054 printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
Kazuhisa Ichikawaaf498d72009-05-12 13:27:45 +02002055 __func__, idx, bio->bi_vcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 break;
2057 }
2058
2059 nbytes = bio_iovec_idx(bio, idx)->bv_len;
2060 BIO_BUG_ON(nbytes > bio->bi_size);
2061
2062 /*
2063 * not a complete bvec done
2064 */
2065 if (unlikely(nbytes > nr_bytes)) {
2066 bio_nbytes += nr_bytes;
2067 total_bytes += nr_bytes;
2068 break;
2069 }
2070
2071 /*
2072 * advance to the next vector
2073 */
2074 next_idx++;
2075 bio_nbytes += nbytes;
2076 }
2077
2078 total_bytes += nbytes;
2079 nr_bytes -= nbytes;
2080
Jens Axboe6728cb02008-01-31 13:03:55 +01002081 bio = req->bio;
2082 if (bio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 /*
2084 * end more in this run, or just return 'not-done'
2085 */
2086 if (unlikely(nr_bytes <= 0))
2087 break;
2088 }
2089 }
2090
2091 /*
2092 * completely done
2093 */
Tejun Heo2e60e022009-04-23 11:05:18 +09002094 if (!req->bio) {
2095 /*
2096 * Reset counters so that the request stacking driver
2097 * can find how many bytes remain in the request
2098 * later.
2099 */
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002100 req->__data_len = 0;
Tejun Heo2e60e022009-04-23 11:05:18 +09002101 return false;
2102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
2104 /*
2105 * if the request wasn't completed, update state
2106 */
2107 if (bio_nbytes) {
NeilBrown5bb23a62007-09-27 12:46:13 +02002108 req_bio_endio(req, bio, bio_nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 bio->bi_idx += next_idx;
2110 bio_iovec(bio)->bv_offset += nr_bytes;
2111 bio_iovec(bio)->bv_len -= nr_bytes;
2112 }
2113
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002114 req->__data_len -= total_bytes;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002115 req->buffer = bio_data(req->bio);
2116
2117 /* update sector only for requests with clear definition of sector */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002118 if (req->cmd_type == REQ_TYPE_FS || (req->cmd_flags & REQ_DISCARD))
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002119 req->__sector += total_bytes >> 9;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002120
Tejun Heo80a761f2009-07-03 17:48:17 +09002121 /* mixed attributes always follow the first bio */
2122 if (req->cmd_flags & REQ_MIXED_MERGE) {
2123 req->cmd_flags &= ~REQ_FAILFAST_MASK;
2124 req->cmd_flags |= req->bio->bi_rw & REQ_FAILFAST_MASK;
2125 }
2126
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002127 /*
2128 * If total number of sectors is less than the first segment
2129 * size, something has gone terribly wrong.
2130 */
2131 if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
2132 printk(KERN_ERR "blk: request botched\n");
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002133 req->__data_len = blk_rq_cur_bytes(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002134 }
2135
2136 /* recalculate the number of segments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 blk_recalc_rq_segments(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002138
Tejun Heo2e60e022009-04-23 11:05:18 +09002139 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140}
Tejun Heo2e60e022009-04-23 11:05:18 +09002141EXPORT_SYMBOL_GPL(blk_update_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
Tejun Heo2e60e022009-04-23 11:05:18 +09002143static bool blk_update_bidi_request(struct request *rq, int error,
2144 unsigned int nr_bytes,
2145 unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002146{
Tejun Heo2e60e022009-04-23 11:05:18 +09002147 if (blk_update_request(rq, error, nr_bytes))
2148 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002149
Tejun Heo2e60e022009-04-23 11:05:18 +09002150 /* Bidi request must be completed as a whole */
2151 if (unlikely(blk_bidi_rq(rq)) &&
2152 blk_update_request(rq->next_rq, error, bidi_bytes))
2153 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002154
Jens Axboee2e1a142010-06-09 10:42:09 +02002155 if (blk_queue_add_random(rq->q))
2156 add_disk_randomness(rq->rq_disk);
Tejun Heo2e60e022009-04-23 11:05:18 +09002157
2158 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159}
2160
James Bottomley28018c22010-07-01 19:49:17 +09002161/**
2162 * blk_unprep_request - unprepare a request
2163 * @req: the request
2164 *
2165 * This function makes a request ready for complete resubmission (or
2166 * completion). It happens only after all error handling is complete,
2167 * so represents the appropriate moment to deallocate any resources
2168 * that were allocated to the request in the prep_rq_fn. The queue
2169 * lock is held when calling this.
2170 */
2171void blk_unprep_request(struct request *req)
2172{
2173 struct request_queue *q = req->q;
2174
2175 req->cmd_flags &= ~REQ_DONTPREP;
2176 if (q->unprep_rq_fn)
2177 q->unprep_rq_fn(q, req);
2178}
2179EXPORT_SYMBOL_GPL(blk_unprep_request);
2180
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181/*
2182 * queue lock must be held
2183 */
Tejun Heo2e60e022009-04-23 11:05:18 +09002184static void blk_finish_request(struct request *req, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002186 if (blk_rq_tagged(req))
2187 blk_queue_end_tag(req->q, req);
2188
James Bottomleyba396a62009-05-27 14:17:08 +02002189 BUG_ON(blk_queued_rq(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002191 if (unlikely(laptop_mode) && req->cmd_type == REQ_TYPE_FS)
Matthew Garrett31373d02010-04-06 14:25:14 +02002192 laptop_io_completion(&req->q->backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
Mike Andersone78042e2008-10-30 02:16:20 -07002194 blk_delete_timer(req);
2195
James Bottomley28018c22010-07-01 19:49:17 +09002196 if (req->cmd_flags & REQ_DONTPREP)
2197 blk_unprep_request(req);
2198
2199
Jens Axboebc58ba92009-01-23 10:54:44 +01002200 blk_account_io_done(req);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002201
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 if (req->end_io)
Tejun Heo8ffdc652006-01-06 09:49:03 +01002203 req->end_io(req, error);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002204 else {
2205 if (blk_bidi_rq(req))
2206 __blk_put_request(req->next_rq->q, req->next_rq);
2207
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 __blk_put_request(req->q, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 }
2210}
2211
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05002212/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002213 * blk_end_bidi_request - Complete a bidi request
2214 * @rq: the request to complete
Randy Dunlap710027a2008-08-19 20:13:11 +02002215 * @error: %0 for success, < %0 for error
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002216 * @nr_bytes: number of bytes to complete @rq
2217 * @bidi_bytes: number of bytes to complete @rq->next_rq
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002218 *
2219 * Description:
2220 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
Tejun Heo2e60e022009-04-23 11:05:18 +09002221 * Drivers that supports bidi can safely call this member for any
2222 * type of request, bidi or uni. In the later case @bidi_bytes is
2223 * just ignored.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002224 *
2225 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002226 * %false - we are done with this request
2227 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002228 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002229static bool blk_end_bidi_request(struct request *rq, int error,
2230 unsigned int nr_bytes, unsigned int bidi_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002231{
2232 struct request_queue *q = rq->q;
Tejun Heo2e60e022009-04-23 11:05:18 +09002233 unsigned long flags;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002234
Tejun Heo2e60e022009-04-23 11:05:18 +09002235 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2236 return true;
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002237
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002238 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo2e60e022009-04-23 11:05:18 +09002239 blk_finish_request(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002240 spin_unlock_irqrestore(q->queue_lock, flags);
2241
Tejun Heo2e60e022009-04-23 11:05:18 +09002242 return false;
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002243}
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002244
2245/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002246 * __blk_end_bidi_request - Complete a bidi request with queue lock held
2247 * @rq: the request to complete
2248 * @error: %0 for success, < %0 for error
2249 * @nr_bytes: number of bytes to complete @rq
2250 * @bidi_bytes: number of bytes to complete @rq->next_rq
Tejun Heo5efccd12009-04-23 11:05:18 +09002251 *
2252 * Description:
Tejun Heo2e60e022009-04-23 11:05:18 +09002253 * Identical to blk_end_bidi_request() except that queue lock is
2254 * assumed to be locked on entry and remains so on return.
Tejun Heo5efccd12009-04-23 11:05:18 +09002255 *
Tejun Heo2e60e022009-04-23 11:05:18 +09002256 * Return:
2257 * %false - we are done with this request
2258 * %true - still buffers pending for this request
Tejun Heo5efccd12009-04-23 11:05:18 +09002259 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002260static bool __blk_end_bidi_request(struct request *rq, int error,
2261 unsigned int nr_bytes, unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002262{
Tejun Heo2e60e022009-04-23 11:05:18 +09002263 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2264 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002265
Tejun Heo2e60e022009-04-23 11:05:18 +09002266 blk_finish_request(rq, error);
Tejun Heo5efccd12009-04-23 11:05:18 +09002267
Tejun Heo2e60e022009-04-23 11:05:18 +09002268 return false;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002269}
2270
2271/**
2272 * blk_end_request - Helper function for drivers to complete the request.
2273 * @rq: the request being processed
2274 * @error: %0 for success, < %0 for error
2275 * @nr_bytes: number of bytes to complete
2276 *
2277 * Description:
2278 * Ends I/O on a number of bytes attached to @rq.
2279 * If @rq has leftover, sets it up for the next range of segments.
2280 *
2281 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002282 * %false - we are done with this request
2283 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002284 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002285bool blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002286{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002287 return blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002288}
Jens Axboe56ad1742009-07-28 22:11:24 +02002289EXPORT_SYMBOL(blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002290
2291/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002292 * blk_end_request_all - Helper function for drives to finish the request.
2293 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002294 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002295 *
2296 * Description:
2297 * Completely finish @rq.
2298 */
2299void blk_end_request_all(struct request *rq, int error)
2300{
2301 bool pending;
2302 unsigned int bidi_bytes = 0;
2303
2304 if (unlikely(blk_bidi_rq(rq)))
2305 bidi_bytes = blk_rq_bytes(rq->next_rq);
2306
2307 pending = blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2308 BUG_ON(pending);
2309}
Jens Axboe56ad1742009-07-28 22:11:24 +02002310EXPORT_SYMBOL(blk_end_request_all);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002311
2312/**
2313 * blk_end_request_cur - Helper function to finish the current request chunk.
2314 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002315 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002316 *
2317 * Description:
2318 * Complete the current consecutively mapped chunk from @rq.
2319 *
2320 * Return:
2321 * %false - we are done with this request
2322 * %true - still buffers pending for this request
2323 */
2324bool blk_end_request_cur(struct request *rq, int error)
2325{
2326 return blk_end_request(rq, error, blk_rq_cur_bytes(rq));
2327}
Jens Axboe56ad1742009-07-28 22:11:24 +02002328EXPORT_SYMBOL(blk_end_request_cur);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002329
2330/**
Tejun Heo80a761f2009-07-03 17:48:17 +09002331 * blk_end_request_err - Finish a request till the next failure boundary.
2332 * @rq: the request to finish till the next failure boundary for
2333 * @error: must be negative errno
2334 *
2335 * Description:
2336 * Complete @rq till the next failure boundary.
2337 *
2338 * Return:
2339 * %false - we are done with this request
2340 * %true - still buffers pending for this request
2341 */
2342bool blk_end_request_err(struct request *rq, int error)
2343{
2344 WARN_ON(error >= 0);
2345 return blk_end_request(rq, error, blk_rq_err_bytes(rq));
2346}
2347EXPORT_SYMBOL_GPL(blk_end_request_err);
2348
2349/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002350 * __blk_end_request - Helper function for drivers to complete the request.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002351 * @rq: the request being processed
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002352 * @error: %0 for success, < %0 for error
2353 * @nr_bytes: number of bytes to complete
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002354 *
2355 * Description:
2356 * Must be called with queue lock held unlike blk_end_request().
2357 *
2358 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002359 * %false - we are done with this request
2360 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002361 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002362bool __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002363{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002364 return __blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002365}
Jens Axboe56ad1742009-07-28 22:11:24 +02002366EXPORT_SYMBOL(__blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002367
2368/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002369 * __blk_end_request_all - Helper function for drives to finish the request.
2370 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002371 * @error: %0 for success, < %0 for error
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002372 *
2373 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002374 * Completely finish @rq. Must be called with queue lock held.
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002375 */
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002376void __blk_end_request_all(struct request *rq, int error)
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002377{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002378 bool pending;
2379 unsigned int bidi_bytes = 0;
2380
2381 if (unlikely(blk_bidi_rq(rq)))
2382 bidi_bytes = blk_rq_bytes(rq->next_rq);
2383
2384 pending = __blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2385 BUG_ON(pending);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002386}
Jens Axboe56ad1742009-07-28 22:11:24 +02002387EXPORT_SYMBOL(__blk_end_request_all);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002388
2389/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002390 * __blk_end_request_cur - Helper function to finish the current request chunk.
2391 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002392 * @error: %0 for success, < %0 for error
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002393 *
2394 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002395 * Complete the current consecutively mapped chunk from @rq. Must
2396 * be called with queue lock held.
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002397 *
2398 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002399 * %false - we are done with this request
2400 * %true - still buffers pending for this request
2401 */
2402bool __blk_end_request_cur(struct request *rq, int error)
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002403{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002404 return __blk_end_request(rq, error, blk_rq_cur_bytes(rq));
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002405}
Jens Axboe56ad1742009-07-28 22:11:24 +02002406EXPORT_SYMBOL(__blk_end_request_cur);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002407
Tejun Heo80a761f2009-07-03 17:48:17 +09002408/**
2409 * __blk_end_request_err - Finish a request till the next failure boundary.
2410 * @rq: the request to finish till the next failure boundary for
2411 * @error: must be negative errno
2412 *
2413 * Description:
2414 * Complete @rq till the next failure boundary. Must be called
2415 * with queue lock held.
2416 *
2417 * Return:
2418 * %false - we are done with this request
2419 * %true - still buffers pending for this request
2420 */
2421bool __blk_end_request_err(struct request *rq, int error)
2422{
2423 WARN_ON(error >= 0);
2424 return __blk_end_request(rq, error, blk_rq_err_bytes(rq));
2425}
2426EXPORT_SYMBOL_GPL(__blk_end_request_err);
2427
Jens Axboe86db1e22008-01-29 14:53:40 +01002428void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2429 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430{
Tejun Heoa82afdf2009-07-03 17:48:16 +09002431 /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002432 rq->cmd_flags |= bio->bi_rw & REQ_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
David Woodhousefb2dce82008-08-05 18:01:53 +01002434 if (bio_has_data(bio)) {
2435 rq->nr_phys_segments = bio_phys_segments(q, bio);
David Woodhousefb2dce82008-08-05 18:01:53 +01002436 rq->buffer = bio_data(bio);
2437 }
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002438 rq->__data_len = bio->bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 rq->bio = rq->biotail = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
NeilBrown66846572007-08-16 13:31:28 +02002441 if (bio->bi_bdev)
2442 rq->rq_disk = bio->bi_bdev->bd_disk;
2443}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
Ilya Loginov2d4dc892009-11-26 09:16:19 +01002445#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
2446/**
2447 * rq_flush_dcache_pages - Helper function to flush all pages in a request
2448 * @rq: the request to be flushed
2449 *
2450 * Description:
2451 * Flush all pages in @rq.
2452 */
2453void rq_flush_dcache_pages(struct request *rq)
2454{
2455 struct req_iterator iter;
2456 struct bio_vec *bvec;
2457
2458 rq_for_each_segment(bvec, rq, iter)
2459 flush_dcache_page(bvec->bv_page);
2460}
2461EXPORT_SYMBOL_GPL(rq_flush_dcache_pages);
2462#endif
2463
Kiyoshi Uedaef9e3fa2008-10-01 16:12:15 +02002464/**
2465 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
2466 * @q : the queue of the device being checked
2467 *
2468 * Description:
2469 * Check if underlying low-level drivers of a device are busy.
2470 * If the drivers want to export their busy state, they must set own
2471 * exporting function using blk_queue_lld_busy() first.
2472 *
2473 * Basically, this function is used only by request stacking drivers
2474 * to stop dispatching requests to underlying devices when underlying
2475 * devices are busy. This behavior helps more I/O merging on the queue
2476 * of the request stacking driver and prevents I/O throughput regression
2477 * on burst I/O load.
2478 *
2479 * Return:
2480 * 0 - Not busy (The request stacking driver should dispatch request)
2481 * 1 - Busy (The request stacking driver should stop dispatching request)
2482 */
2483int blk_lld_busy(struct request_queue *q)
2484{
2485 if (q->lld_busy_fn)
2486 return q->lld_busy_fn(q);
2487
2488 return 0;
2489}
2490EXPORT_SYMBOL_GPL(blk_lld_busy);
2491
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002492/**
2493 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
2494 * @rq: the clone request to be cleaned up
2495 *
2496 * Description:
2497 * Free all bios in @rq for a cloned request.
2498 */
2499void blk_rq_unprep_clone(struct request *rq)
2500{
2501 struct bio *bio;
2502
2503 while ((bio = rq->bio) != NULL) {
2504 rq->bio = bio->bi_next;
2505
2506 bio_put(bio);
2507 }
2508}
2509EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
2510
2511/*
2512 * Copy attributes of the original request to the clone request.
2513 * The actual data parts (e.g. ->cmd, ->buffer, ->sense) are not copied.
2514 */
2515static void __blk_rq_prep_clone(struct request *dst, struct request *src)
2516{
2517 dst->cpu = src->cpu;
2518 dst->cmd_flags = (rq_data_dir(src) | REQ_NOMERGE);
ike Snitzer33839772010-08-08 12:11:33 -04002519 if (src->cmd_flags & REQ_DISCARD)
2520 dst->cmd_flags |= REQ_DISCARD;
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002521 dst->cmd_type = src->cmd_type;
2522 dst->__sector = blk_rq_pos(src);
2523 dst->__data_len = blk_rq_bytes(src);
2524 dst->nr_phys_segments = src->nr_phys_segments;
2525 dst->ioprio = src->ioprio;
2526 dst->extra_len = src->extra_len;
2527}
2528
2529/**
2530 * blk_rq_prep_clone - Helper function to setup clone request
2531 * @rq: the request to be setup
2532 * @rq_src: original request to be cloned
2533 * @bs: bio_set that bios for clone are allocated from
2534 * @gfp_mask: memory allocation mask for bio
2535 * @bio_ctr: setup function to be called for each clone bio.
2536 * Returns %0 for success, non %0 for failure.
2537 * @data: private data to be passed to @bio_ctr
2538 *
2539 * Description:
2540 * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
2541 * The actual data parts of @rq_src (e.g. ->cmd, ->buffer, ->sense)
2542 * are not copied, and copying such parts is the caller's responsibility.
2543 * Also, pages which the original bios are pointing to are not copied
2544 * and the cloned bios just point same pages.
2545 * So cloned bios must be completed before original bios, which means
2546 * the caller must complete @rq before @rq_src.
2547 */
2548int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
2549 struct bio_set *bs, gfp_t gfp_mask,
2550 int (*bio_ctr)(struct bio *, struct bio *, void *),
2551 void *data)
2552{
2553 struct bio *bio, *bio_src;
2554
2555 if (!bs)
2556 bs = fs_bio_set;
2557
2558 blk_rq_init(NULL, rq);
2559
2560 __rq_for_each_bio(bio_src, rq_src) {
2561 bio = bio_alloc_bioset(gfp_mask, bio_src->bi_max_vecs, bs);
2562 if (!bio)
2563 goto free_and_out;
2564
2565 __bio_clone(bio, bio_src);
2566
2567 if (bio_integrity(bio_src) &&
Martin K. Petersen7878cba2009-06-26 15:37:49 +02002568 bio_integrity_clone(bio, bio_src, gfp_mask, bs))
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002569 goto free_and_out;
2570
2571 if (bio_ctr && bio_ctr(bio, bio_src, data))
2572 goto free_and_out;
2573
2574 if (rq->bio) {
2575 rq->biotail->bi_next = bio;
2576 rq->biotail = bio;
2577 } else
2578 rq->bio = rq->biotail = bio;
2579 }
2580
2581 __blk_rq_prep_clone(rq, rq_src);
2582
2583 return 0;
2584
2585free_and_out:
2586 if (bio)
2587 bio_free(bio, bs);
2588 blk_rq_unprep_clone(rq);
2589
2590 return -ENOMEM;
2591}
2592EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
2593
Jens Axboe18887ad2008-07-28 13:08:45 +02002594int kblockd_schedule_work(struct request_queue *q, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595{
2596 return queue_work(kblockd_workqueue, work);
2597}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598EXPORT_SYMBOL(kblockd_schedule_work);
2599
Vivek Goyale43473b2010-09-15 17:06:35 -04002600int kblockd_schedule_delayed_work(struct request_queue *q,
2601 struct delayed_work *dwork, unsigned long delay)
2602{
2603 return queue_delayed_work(kblockd_workqueue, dwork, delay);
2604}
2605EXPORT_SYMBOL(kblockd_schedule_delayed_work);
2606
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607int __init blk_dev_init(void)
2608{
Nikanth Karthikesan9eb55b02009-04-27 14:53:54 +02002609 BUILD_BUG_ON(__REQ_NR_BITS > 8 *
2610 sizeof(((struct request *)0)->cmd_flags));
2611
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 kblockd_workqueue = create_workqueue("kblockd");
2613 if (!kblockd_workqueue)
2614 panic("Failed to create kblockd\n");
2615
2616 request_cachep = kmem_cache_create("blkdev_requests",
Paul Mundt20c2df82007-07-20 10:11:58 +09002617 sizeof(struct request), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
Jens Axboe8324aa92008-01-29 14:51:59 +01002619 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02002620 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 return 0;
2623}