blob: 1c748403882915470df51ba0d639f881e8933b44 [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>
Jens Axboe2056a782006-03-23 20:00:26 +010029#include <linux/blktrace_api.h>
Akinobu Mitac17bb492006-12-08 02:39:46 -080030#include <linux/fault-inject.h>
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +010031#include <trace/block.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Jens Axboe8324aa92008-01-29 14:51:59 +010033#include "blk.h"
34
Ingo Molnar0bfc2452008-11-26 11:59:56 +010035DEFINE_TRACE(block_plug);
36DEFINE_TRACE(block_unplug_io);
37DEFINE_TRACE(block_unplug_timer);
38DEFINE_TRACE(block_getrq);
39DEFINE_TRACE(block_sleeprq);
40DEFINE_TRACE(block_rq_requeue);
41DEFINE_TRACE(block_bio_backmerge);
42DEFINE_TRACE(block_bio_frontmerge);
43DEFINE_TRACE(block_bio_queue);
44DEFINE_TRACE(block_rq_complete);
45DEFINE_TRACE(block_remap); /* Also used in drivers/md/dm.c */
46EXPORT_TRACEPOINT_SYMBOL_GPL(block_remap);
47
Jens Axboe165125e2007-07-24 09:28:11 +020048static int __make_request(struct request_queue *q, struct bio *bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/*
51 * For the allocated request tables
52 */
Adrian Bunk5ece6c52008-02-18 13:45:51 +010053static struct kmem_cache *request_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/*
56 * For queue allocation
57 */
Jens Axboe6728cb02008-01-31 13:03:55 +010058struct kmem_cache *blk_requestq_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 * Controlling structure to kblockd
62 */
Jens Axboeff856ba2006-01-09 16:02:34 +010063static struct workqueue_struct *kblockd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Jens Axboe26b82562008-01-29 13:54:41 +010065static void drive_stat_acct(struct request *rq, int new_io)
66{
Jens Axboe28f13702008-05-07 10:15:46 +020067 struct hd_struct *part;
Jens Axboe26b82562008-01-29 13:54:41 +010068 int rw = rq_data_dir(rq);
Tejun Heoc9959052008-08-25 19:47:21 +090069 int cpu;
Jens Axboe26b82562008-01-29 13:54:41 +010070
Jens Axboec2553b52009-04-24 08:10:11 +020071 if (!blk_do_io_stat(rq))
Jens Axboe26b82562008-01-29 13:54:41 +010072 return;
73
Tejun Heo074a7ac2008-08-25 19:56:14 +090074 cpu = part_stat_lock();
Tejun Heo83096eb2009-05-07 22:24:39 +090075 part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq));
Tejun Heoc9959052008-08-25 19:47:21 +090076
Jens Axboe28f13702008-05-07 10:15:46 +020077 if (!new_io)
Tejun Heo074a7ac2008-08-25 19:56:14 +090078 part_stat_inc(cpu, part, merges[rw]);
Jens Axboe28f13702008-05-07 10:15:46 +020079 else {
Tejun Heo074a7ac2008-08-25 19:56:14 +090080 part_round_stats(cpu, part);
81 part_inc_in_flight(part);
Jens Axboe26b82562008-01-29 13:54:41 +010082 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +020083
Tejun Heo074a7ac2008-08-25 19:56:14 +090084 part_stat_unlock();
Jens Axboe26b82562008-01-29 13:54:41 +010085}
86
Jens Axboe8324aa92008-01-29 14:51:59 +010087void blk_queue_congestion_threshold(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 int nr;
90
91 nr = q->nr_requests - (q->nr_requests / 8) + 1;
92 if (nr > q->nr_requests)
93 nr = q->nr_requests;
94 q->nr_congestion_on = nr;
95
96 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
97 if (nr < 1)
98 nr = 1;
99 q->nr_congestion_off = nr;
100}
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102/**
103 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
104 * @bdev: device
105 *
106 * Locates the passed device's request queue and returns the address of its
107 * backing_dev_info
108 *
109 * Will return NULL if the request queue cannot be located.
110 */
111struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
112{
113 struct backing_dev_info *ret = NULL;
Jens Axboe165125e2007-07-24 09:28:11 +0200114 struct request_queue *q = bdev_get_queue(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 if (q)
117 ret = &q->backing_dev_info;
118 return ret;
119}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120EXPORT_SYMBOL(blk_get_backing_dev_info);
121
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200122void blk_rq_init(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200124 memset(rq, 0, sizeof(*rq));
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 INIT_LIST_HEAD(&rq->queuelist);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700127 INIT_LIST_HEAD(&rq->timeout_list);
Jens Axboec7c22e42008-09-13 20:26:01 +0200128 rq->cpu = -1;
Jens Axboe63a71382008-02-08 12:41:03 +0100129 rq->q = q;
Tejun Heoa2dec7b2009-05-07 22:24:44 +0900130 rq->__sector = (sector_t) -1;
Jens Axboe2e662b62006-07-13 11:55:04 +0200131 INIT_HLIST_NODE(&rq->hash);
132 RB_CLEAR_NODE(&rq->rb_node);
FUJITA Tomonorid7e3c322008-04-29 09:54:39 +0200133 rq->cmd = rq->__cmd;
Li Zefane2494e12009-04-02 13:43:26 +0800134 rq->cmd_len = BLK_MAX_CDB;
Jens Axboe63a71382008-02-08 12:41:03 +0100135 rq->tag = -1;
Jens Axboe63a71382008-02-08 12:41:03 +0100136 rq->ref_count = 1;
Tejun Heob243ddc2009-04-23 11:05:18 +0900137 rq->start_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200139EXPORT_SYMBOL(blk_rq_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
NeilBrown5bb23a62007-09-27 12:46:13 +0200141static void req_bio_endio(struct request *rq, struct bio *bio,
142 unsigned int nbytes, int error)
Tejun Heo797e7db2006-01-06 09:51:03 +0100143{
Jens Axboe165125e2007-07-24 09:28:11 +0200144 struct request_queue *q = rq->q;
Tejun Heo797e7db2006-01-06 09:51:03 +0100145
NeilBrown5bb23a62007-09-27 12:46:13 +0200146 if (&q->bar_rq != rq) {
147 if (error)
148 clear_bit(BIO_UPTODATE, &bio->bi_flags);
149 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
150 error = -EIO;
Tejun Heo797e7db2006-01-06 09:51:03 +0100151
NeilBrown5bb23a62007-09-27 12:46:13 +0200152 if (unlikely(nbytes > bio->bi_size)) {
Jens Axboe6728cb02008-01-31 13:03:55 +0100153 printk(KERN_ERR "%s: want %u bytes done, %u left\n",
Harvey Harrison24c03d42008-05-01 04:35:17 -0700154 __func__, nbytes, bio->bi_size);
NeilBrown5bb23a62007-09-27 12:46:13 +0200155 nbytes = bio->bi_size;
156 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100157
Keith Mannthey08bafc02008-11-25 10:24:35 +0100158 if (unlikely(rq->cmd_flags & REQ_QUIET))
159 set_bit(BIO_QUIET, &bio->bi_flags);
160
NeilBrown5bb23a62007-09-27 12:46:13 +0200161 bio->bi_size -= nbytes;
162 bio->bi_sector += (nbytes >> 9);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200163
164 if (bio_integrity(bio))
165 bio_integrity_advance(bio, nbytes);
166
NeilBrown5bb23a62007-09-27 12:46:13 +0200167 if (bio->bi_size == 0)
NeilBrown6712ecf2007-09-27 12:47:43 +0200168 bio_endio(bio, error);
NeilBrown5bb23a62007-09-27 12:46:13 +0200169 } else {
170
171 /*
172 * Okay, this is the barrier request in progress, just
173 * record the error;
174 */
175 if (error && !q->orderr)
176 q->orderr = error;
177 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100178}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180void blk_dump_rq_flags(struct request *rq, char *msg)
181{
182 int bit;
183
Jens Axboe6728cb02008-01-31 13:03:55 +0100184 printk(KERN_INFO "%s: dev %s: type=%x, flags=%x\n", msg,
Jens Axboe4aff5e22006-08-10 08:44:47 +0200185 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
186 rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Tejun Heo83096eb2009-05-07 22:24:39 +0900188 printk(KERN_INFO " sector %llu, nr/cnr %u/%u\n",
189 (unsigned long long)blk_rq_pos(rq),
190 blk_rq_sectors(rq), blk_rq_cur_sectors(rq));
Tejun Heo731ec492009-04-23 11:05:20 +0900191 printk(KERN_INFO " bio %p, biotail %p, buffer %p, len %u\n",
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900192 rq->bio, rq->biotail, rq->buffer, blk_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Jens Axboe4aff5e22006-08-10 08:44:47 +0200194 if (blk_pc_request(rq)) {
Jens Axboe6728cb02008-01-31 13:03:55 +0100195 printk(KERN_INFO " cdb: ");
FUJITA Tomonorid34c87e2008-04-29 14:37:52 +0200196 for (bit = 0; bit < BLK_MAX_CDB; bit++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 printk("%02x ", rq->cmd[bit]);
198 printk("\n");
199 }
200}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201EXPORT_SYMBOL(blk_dump_rq_flags);
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203/*
204 * "plug" the device if there are no outstanding requests: this will
205 * force the transfer to start only after we have put all the requests
206 * on the list.
207 *
208 * This is called with interrupts off and no requests on the queue and
209 * with the queue lock held.
210 */
Jens Axboe165125e2007-07-24 09:28:11 +0200211void blk_plug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 WARN_ON(!irqs_disabled());
214
215 /*
216 * don't plug a stopped queue, it must be paired with blk_start_queue()
217 * which will restart the queueing
218 */
Coywolf Qi Hunt7daac492006-04-19 10:14:49 +0200219 if (blk_queue_stopped(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return;
221
Jens Axboee48ec692008-07-03 13:18:54 +0200222 if (!queue_flag_test_and_set(QUEUE_FLAG_PLUGGED, q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100224 trace_block_plug(q);
Jens Axboe2056a782006-03-23 20:00:26 +0100225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227EXPORT_SYMBOL(blk_plug_device);
228
Jens Axboe6c5e0c42008-08-01 20:31:32 +0200229/**
230 * blk_plug_device_unlocked - plug a device without queue lock held
231 * @q: The &struct request_queue to plug
232 *
233 * Description:
234 * Like @blk_plug_device(), but grabs the queue lock and disables
235 * interrupts.
236 **/
237void blk_plug_device_unlocked(struct request_queue *q)
238{
239 unsigned long flags;
240
241 spin_lock_irqsave(q->queue_lock, flags);
242 blk_plug_device(q);
243 spin_unlock_irqrestore(q->queue_lock, flags);
244}
245EXPORT_SYMBOL(blk_plug_device_unlocked);
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247/*
248 * remove the queue from the plugged list, if present. called with
249 * queue lock held and interrupts disabled.
250 */
Jens Axboe165125e2007-07-24 09:28:11 +0200251int blk_remove_plug(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 WARN_ON(!irqs_disabled());
254
Jens Axboee48ec692008-07-03 13:18:54 +0200255 if (!queue_flag_test_and_clear(QUEUE_FLAG_PLUGGED, q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return 0;
257
258 del_timer(&q->unplug_timer);
259 return 1;
260}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261EXPORT_SYMBOL(blk_remove_plug);
262
263/*
264 * remove the plug and let it rip..
265 */
Jens Axboe165125e2007-07-24 09:28:11 +0200266void __generic_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Coywolf Qi Hunt7daac492006-04-19 10:14:49 +0200268 if (unlikely(blk_queue_stopped(q)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return;
Jens Axboea31a9732008-10-17 13:58:29 +0200270 if (!blk_remove_plug(q) && !blk_queue_nonrot(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return;
272
Jens Axboe22e2c502005-06-27 10:55:12 +0200273 q->request_fn(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276/**
277 * generic_unplug_device - fire a request queue
Jens Axboe165125e2007-07-24 09:28:11 +0200278 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 *
280 * Description:
281 * Linux uses plugging to build bigger requests queues before letting
282 * the device have at them. If a queue is plugged, the I/O scheduler
283 * is still adding and merging requests on the queue. Once the queue
284 * gets unplugged, the request_fn defined for the queue is invoked and
285 * transfers started.
286 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200287void generic_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Jens Axboedbaf2c02008-05-07 09:48:17 +0200289 if (blk_queue_plugged(q)) {
290 spin_lock_irq(q->queue_lock);
291 __generic_unplug_device(q);
292 spin_unlock_irq(q->queue_lock);
293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295EXPORT_SYMBOL(generic_unplug_device);
296
297static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
298 struct page *page)
299{
Jens Axboe165125e2007-07-24 09:28:11 +0200300 struct request_queue *q = bdi->unplug_io_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500302 blk_unplug(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
Jens Axboe86db1e22008-01-29 14:53:40 +0100305void blk_unplug_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Jens Axboe165125e2007-07-24 09:28:11 +0200307 struct request_queue *q =
308 container_of(work, struct request_queue, unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100310 trace_block_unplug_io(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 q->unplug_fn(q);
312}
313
Jens Axboe86db1e22008-01-29 14:53:40 +0100314void blk_unplug_timeout(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Jens Axboe165125e2007-07-24 09:28:11 +0200316 struct request_queue *q = (struct request_queue *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100318 trace_block_unplug_timer(q);
Jens Axboe18887ad2008-07-28 13:08:45 +0200319 kblockd_schedule_work(q, &q->unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500322void blk_unplug(struct request_queue *q)
323{
324 /*
325 * devices don't necessarily have an ->unplug_fn defined
326 */
327 if (q->unplug_fn) {
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100328 trace_block_unplug_io(q);
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500329 q->unplug_fn(q);
330 }
331}
332EXPORT_SYMBOL(blk_unplug);
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334/**
335 * blk_start_queue - restart a previously stopped queue
Jens Axboe165125e2007-07-24 09:28:11 +0200336 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 *
338 * Description:
339 * blk_start_queue() will clear the stop flag on the queue, and call
340 * the request_fn for the queue if it was in a stopped state when
341 * entered. Also see blk_stop_queue(). Queue lock must be held.
342 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200343void blk_start_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200345 WARN_ON(!irqs_disabled());
346
Nick Piggin75ad23b2008-04-29 14:48:33 +0200347 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
Tejun Heoa538cd02009-04-23 11:05:17 +0900348 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350EXPORT_SYMBOL(blk_start_queue);
351
352/**
353 * blk_stop_queue - stop a queue
Jens Axboe165125e2007-07-24 09:28:11 +0200354 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 *
356 * Description:
357 * The Linux block layer assumes that a block driver will consume all
358 * entries on the request queue when the request_fn strategy is called.
359 * Often this will not happen, because of hardware limitations (queue
360 * depth settings). If a device driver gets a 'queue full' response,
361 * or if it simply chooses not to queue more I/O at one point, it can
362 * call this function to prevent the request_fn from being called until
363 * the driver has signalled it's ready to go again. This happens by calling
364 * blk_start_queue() to restart queue operations. Queue lock must be held.
365 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200366void blk_stop_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
368 blk_remove_plug(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200369 queue_flag_set(QUEUE_FLAG_STOPPED, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371EXPORT_SYMBOL(blk_stop_queue);
372
373/**
374 * blk_sync_queue - cancel any pending callbacks on a queue
375 * @q: the queue
376 *
377 * Description:
378 * The block layer may perform asynchronous callback activity
379 * on a queue, such as calling the unplug function after a timeout.
380 * A block device may call blk_sync_queue to ensure that any
381 * such activity is cancelled, thus allowing it to release resources
Michael Opdenacker59c51592007-05-09 08:57:56 +0200382 * that the callbacks might use. The caller must already have made sure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 * that its ->make_request_fn will not re-add plugging prior to calling
384 * this function.
385 *
386 */
387void blk_sync_queue(struct request_queue *q)
388{
389 del_timer_sync(&q->unplug_timer);
Jens Axboe70ed28b2008-11-19 14:38:39 +0100390 del_timer_sync(&q->timeout);
Cheng Renquan64d01dc2008-12-03 12:41:39 +0100391 cancel_work_sync(&q->unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393EXPORT_SYMBOL(blk_sync_queue);
394
395/**
Jens Axboe80a4b582008-10-14 09:51:06 +0200396 * __blk_run_queue - run a single device queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200398 *
399 * Description:
400 * See @blk_run_queue. This variant must be called with the queue lock
401 * held and interrupts disabled.
402 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 */
Nick Piggin75ad23b2008-04-29 14:48:33 +0200404void __blk_run_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 blk_remove_plug(q);
Jens Axboedac07ec2006-05-11 08:20:16 +0200407
Tejun Heoa538cd02009-04-23 11:05:17 +0900408 if (unlikely(blk_queue_stopped(q)))
409 return;
410
411 if (elv_queue_empty(q))
412 return;
413
Jens Axboedac07ec2006-05-11 08:20:16 +0200414 /*
415 * Only recurse once to avoid overrunning the stack, let the unplug
416 * handling reinvoke the handler shortly if we already got there.
417 */
Tejun Heoa538cd02009-04-23 11:05:17 +0900418 if (!queue_flag_test_and_set(QUEUE_FLAG_REENTER, q)) {
419 q->request_fn(q);
420 queue_flag_clear(QUEUE_FLAG_REENTER, q);
421 } else {
422 queue_flag_set(QUEUE_FLAG_PLUGGED, q);
423 kblockd_schedule_work(q, &q->unplug_work);
424 }
Nick Piggin75ad23b2008-04-29 14:48:33 +0200425}
426EXPORT_SYMBOL(__blk_run_queue);
Jens Axboedac07ec2006-05-11 08:20:16 +0200427
Nick Piggin75ad23b2008-04-29 14:48:33 +0200428/**
429 * blk_run_queue - run a single device queue
430 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200431 *
432 * Description:
433 * Invoke request handling on this queue, if it has pending work to do.
Tejun Heoa7f55792009-04-23 11:05:17 +0900434 * May be used to restart queueing when a request has completed.
Nick Piggin75ad23b2008-04-29 14:48:33 +0200435 */
436void blk_run_queue(struct request_queue *q)
437{
438 unsigned long flags;
439
440 spin_lock_irqsave(q->queue_lock, flags);
441 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 spin_unlock_irqrestore(q->queue_lock, flags);
443}
444EXPORT_SYMBOL(blk_run_queue);
445
Jens Axboe165125e2007-07-24 09:28:11 +0200446void blk_put_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500447{
448 kobject_put(&q->kobj);
449}
Al Viro483f4af2006-03-18 18:34:37 -0500450
Jens Axboe6728cb02008-01-31 13:03:55 +0100451void blk_cleanup_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500452{
Jens Axboee3335de92008-09-18 09:22:54 -0700453 /*
454 * We know we have process context here, so we can be a little
455 * cautious and ensure that pending block actions on this device
456 * are done before moving on. Going into this function, we should
457 * not have processes doing IO to this device.
458 */
459 blk_sync_queue(q);
460
Al Viro483f4af2006-03-18 18:34:37 -0500461 mutex_lock(&q->sysfs_lock);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200462 queue_flag_set_unlocked(QUEUE_FLAG_DEAD, q);
Al Viro483f4af2006-03-18 18:34:37 -0500463 mutex_unlock(&q->sysfs_lock);
464
465 if (q->elevator)
466 elevator_exit(q->elevator);
467
468 blk_put_queue(q);
469}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470EXPORT_SYMBOL(blk_cleanup_queue);
471
Jens Axboe165125e2007-07-24 09:28:11 +0200472static int blk_init_free_list(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 struct request_list *rl = &q->rq;
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;
509 err = bdi_init(&q->backing_dev_info);
510 if (err) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100511 kmem_cache_free(blk_requestq_cachep, q);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700512 return NULL;
513 }
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 init_timer(&q->unplug_timer);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700516 setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
517 INIT_LIST_HEAD(&q->timeout_list);
Peter Zijlstra713ada92008-10-16 13:44:57 +0200518 INIT_WORK(&q->unplug_work, blk_unplug_work);
Al Viro483f4af2006-03-18 18:34:37 -0500519
Jens Axboe8324aa92008-01-29 14:51:59 +0100520 kobject_init(&q->kobj, &blk_queue_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Al Viro483f4af2006-03-18 18:34:37 -0500522 mutex_init(&q->sysfs_lock);
Neil Browne7e72bf2008-05-14 16:05:54 -0700523 spin_lock_init(&q->__queue_lock);
Al Viro483f4af2006-03-18 18:34:37 -0500524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return q;
526}
Christoph Lameter19460892005-06-23 00:08:19 -0700527EXPORT_SYMBOL(blk_alloc_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529/**
530 * blk_init_queue - prepare a request queue for use with a block device
531 * @rfn: The function to be called to process requests that have been
532 * placed on the queue.
533 * @lock: Request queue spin lock
534 *
535 * Description:
536 * If a block device wishes to use the standard request handling procedures,
537 * which sorts requests and coalesces adjacent requests, then it must
538 * call blk_init_queue(). The function @rfn will be called when there
539 * are requests on the queue that need to be processed. If the device
540 * supports plugging, then @rfn may not be called immediately when requests
541 * are available on the queue, but may be called at some time later instead.
542 * Plugged queues are generally unplugged when a buffer belonging to one
543 * of the requests on the queue is needed, or due to memory pressure.
544 *
545 * @rfn is not required, or even expected, to remove all requests off the
546 * queue, but only as many as it can handle at a time. If it does leave
547 * requests on the queue, it is responsible for arranging that the requests
548 * get dealt with eventually.
549 *
550 * The queue spin lock must be held while manipulating the requests on the
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200551 * request queue; this lock will be taken also from interrupt context, so irq
552 * disabling is needed for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 *
Randy Dunlap710027a2008-08-19 20:13:11 +0200554 * Function returns a pointer to the initialized request queue, or %NULL if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 * it didn't succeed.
556 *
557 * Note:
558 * blk_init_queue() must be paired with a blk_cleanup_queue() call
559 * when the block device is deactivated (such as at module unload).
560 **/
Christoph Lameter19460892005-06-23 00:08:19 -0700561
Jens Axboe165125e2007-07-24 09:28:11 +0200562struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Christoph Lameter19460892005-06-23 00:08:19 -0700564 return blk_init_queue_node(rfn, lock, -1);
565}
566EXPORT_SYMBOL(blk_init_queue);
567
Jens Axboe165125e2007-07-24 09:28:11 +0200568struct request_queue *
Christoph Lameter19460892005-06-23 00:08:19 -0700569blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
570{
Jens Axboe165125e2007-07-24 09:28:11 +0200571 struct request_queue *q = blk_alloc_queue_node(GFP_KERNEL, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 if (!q)
574 return NULL;
575
Christoph Lameter19460892005-06-23 00:08:19 -0700576 q->node = node_id;
Al Viro8669aaf2006-03-18 13:50:00 -0500577 if (blk_init_free_list(q)) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100578 kmem_cache_free(blk_requestq_cachep, q);
Al Viro8669aaf2006-03-18 13:50:00 -0500579 return NULL;
580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
152587d2005-04-12 16:22:06 -0500582 /*
583 * if caller didn't supply a lock, they get per-queue locking with
584 * our embedded lock
585 */
Neil Browne7e72bf2008-05-14 16:05:54 -0700586 if (!lock)
152587d2005-04-12 16:22:06 -0500587 lock = &q->__queue_lock;
152587d2005-04-12 16:22:06 -0500588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 q->request_fn = rfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 q->prep_rq_fn = NULL;
591 q->unplug_fn = generic_unplug_device;
Jens Axboebc58ba92009-01-23 10:54:44 +0100592 q->queue_flags = QUEUE_FLAG_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 q->queue_lock = lock;
594
Jens Axboef3b144a2009-03-06 08:48:33 +0100595 /*
596 * This also sets hw/phys segments, boundary and size
597 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 blk_queue_make_request(q, __make_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Alan Stern44ec9542007-02-20 11:01:57 -0500600 q->sg_reserved_size = INT_MAX;
601
FUJITA Tomonoriabf54392008-08-16 14:10:05 +0900602 blk_set_cmd_filter_defaults(&q->cmd_filter);
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 /*
605 * all done
606 */
607 if (!elevator_init(q, NULL)) {
608 blk_queue_congestion_threshold(q);
609 return q;
610 }
611
Al Viro8669aaf2006-03-18 13:50:00 -0500612 blk_put_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return NULL;
614}
Christoph Lameter19460892005-06-23 00:08:19 -0700615EXPORT_SYMBOL(blk_init_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Jens Axboe165125e2007-07-24 09:28:11 +0200617int blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Nick Pigginfde6ad22005-06-23 00:08:53 -0700619 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
Al Viro483f4af2006-03-18 18:34:37 -0500620 kobject_get(&q->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return 0;
622 }
623
624 return 1;
625}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Jens Axboe165125e2007-07-24 09:28:11 +0200627static inline void blk_free_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Jens Axboe4aff5e22006-08-10 08:44:47 +0200629 if (rq->cmd_flags & REQ_ELVPRIV)
Tejun Heocb98fc82005-10-28 08:29:39 +0200630 elv_put_request(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 mempool_free(rq, q->rq.rq_pool);
632}
633
Jens Axboe1ea25ecb2006-07-18 22:24:11 +0200634static struct request *
Jerome Marchand42dad762009-04-22 14:01:49 +0200635blk_alloc_request(struct request_queue *q, int flags, int priv, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
637 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
638
639 if (!rq)
640 return NULL;
641
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200642 blk_rq_init(q, rq);
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200643
Jerome Marchand42dad762009-04-22 14:01:49 +0200644 rq->cmd_flags = flags | REQ_ALLOCED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Tejun Heocb98fc82005-10-28 08:29:39 +0200646 if (priv) {
Jens Axboecb78b282006-07-28 09:32:57 +0200647 if (unlikely(elv_set_request(q, rq, gfp_mask))) {
Tejun Heocb98fc82005-10-28 08:29:39 +0200648 mempool_free(rq, q->rq.rq_pool);
649 return NULL;
650 }
Jens Axboe4aff5e22006-08-10 08:44:47 +0200651 rq->cmd_flags |= REQ_ELVPRIV;
Tejun Heocb98fc82005-10-28 08:29:39 +0200652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Tejun Heocb98fc82005-10-28 08:29:39 +0200654 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
657/*
658 * ioc_batching returns true if the ioc is a valid batching request and
659 * should be given priority access to a request.
660 */
Jens Axboe165125e2007-07-24 09:28:11 +0200661static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
663 if (!ioc)
664 return 0;
665
666 /*
667 * Make sure the process is able to allocate at least 1 request
668 * even if the batch times out, otherwise we could theoretically
669 * lose wakeups.
670 */
671 return ioc->nr_batch_requests == q->nr_batching ||
672 (ioc->nr_batch_requests > 0
673 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
674}
675
676/*
677 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
678 * will cause the process to be a "batcher" on all queues in the system. This
679 * is the behaviour we want though - once it gets a wakeup it should be given
680 * a nice run.
681 */
Jens Axboe165125e2007-07-24 09:28:11 +0200682static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
684 if (!ioc || ioc_batching(q, ioc))
685 return;
686
687 ioc->nr_batch_requests = q->nr_batching;
688 ioc->last_waited = jiffies;
689}
690
Jens Axboe1faa16d2009-04-06 14:48:01 +0200691static void __freed_request(struct request_queue *q, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
693 struct request_list *rl = &q->rq;
694
Jens Axboe1faa16d2009-04-06 14:48:01 +0200695 if (rl->count[sync] < queue_congestion_off_threshold(q))
696 blk_clear_queue_congested(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Jens Axboe1faa16d2009-04-06 14:48:01 +0200698 if (rl->count[sync] + 1 <= q->nr_requests) {
699 if (waitqueue_active(&rl->wait[sync]))
700 wake_up(&rl->wait[sync]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Jens Axboe1faa16d2009-04-06 14:48:01 +0200702 blk_clear_queue_full(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
704}
705
706/*
707 * A request has just been released. Account for it, update the full and
708 * congestion status, wake up any waiters. Called under q->queue_lock.
709 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200710static void freed_request(struct request_queue *q, int sync, int priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
712 struct request_list *rl = &q->rq;
713
Jens Axboe1faa16d2009-04-06 14:48:01 +0200714 rl->count[sync]--;
Tejun Heocb98fc82005-10-28 08:29:39 +0200715 if (priv)
716 rl->elvpriv--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Jens Axboe1faa16d2009-04-06 14:48:01 +0200718 __freed_request(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Jens Axboe1faa16d2009-04-06 14:48:01 +0200720 if (unlikely(rl->starved[sync ^ 1]))
721 __freed_request(q, sync ^ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724/*
Nick Piggind6344532005-06-28 20:45:14 -0700725 * Get a free request, queue_lock must be held.
726 * Returns NULL on failure, with queue_lock held.
727 * Returns !NULL on success, with queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 */
Jens Axboe165125e2007-07-24 09:28:11 +0200729static struct request *get_request(struct request_queue *q, int rw_flags,
Jens Axboe7749a8d2006-12-13 13:02:26 +0100730 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
732 struct request *rq = NULL;
733 struct request_list *rl = &q->rq;
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100734 struct io_context *ioc = NULL;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200735 const bool is_sync = rw_is_sync(rw_flags) != 0;
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100736 int may_queue, priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Jens Axboe7749a8d2006-12-13 13:02:26 +0100738 may_queue = elv_may_queue(q, rw_flags);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100739 if (may_queue == ELV_MQUEUE_NO)
740 goto rq_starved;
741
Jens Axboe1faa16d2009-04-06 14:48:01 +0200742 if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) {
743 if (rl->count[is_sync]+1 >= q->nr_requests) {
Jens Axboeb5deef92006-07-19 23:39:40 +0200744 ioc = current_io_context(GFP_ATOMIC, q->node);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100745 /*
746 * The queue will fill after this allocation, so set
747 * it as full, and mark this process as "batching".
748 * This process will be allowed to complete a batch of
749 * requests, others will be blocked.
750 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200751 if (!blk_queue_full(q, is_sync)) {
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100752 ioc_set_batching(q, ioc);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200753 blk_set_queue_full(q, is_sync);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100754 } else {
755 if (may_queue != ELV_MQUEUE_MUST
756 && !ioc_batching(q, ioc)) {
757 /*
758 * The queue is full and the allocating
759 * process is not a "batcher", and not
760 * exempted by the IO scheduler
761 */
762 goto out;
763 }
764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
Jens Axboe1faa16d2009-04-06 14:48:01 +0200766 blk_set_queue_congested(q, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
768
Jens Axboe082cf692005-06-28 16:35:11 +0200769 /*
770 * Only allow batching queuers to allocate up to 50% over the defined
771 * limit of requests, otherwise we could have thousands of requests
772 * allocated with any setting of ->nr_requests
773 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200774 if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
Jens Axboe082cf692005-06-28 16:35:11 +0200775 goto out;
Hugh Dickinsfd782a42005-06-29 15:15:40 +0100776
Jens Axboe1faa16d2009-04-06 14:48:01 +0200777 rl->count[is_sync]++;
778 rl->starved[is_sync] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200779
Jens Axboe64521d12005-10-28 08:30:39 +0200780 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Tejun Heocb98fc82005-10-28 08:29:39 +0200781 if (priv)
782 rl->elvpriv++;
783
Jerome Marchand42dad762009-04-22 14:01:49 +0200784 if (blk_queue_io_stat(q))
785 rw_flags |= REQ_IO_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 spin_unlock_irq(q->queue_lock);
787
Jens Axboe7749a8d2006-12-13 13:02:26 +0100788 rq = blk_alloc_request(q, rw_flags, priv, gfp_mask);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100789 if (unlikely(!rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 /*
791 * Allocation failed presumably due to memory. Undo anything
792 * we might have messed up.
793 *
794 * Allocating task should really be put onto the front of the
795 * wait queue, but this is pretty rare.
796 */
797 spin_lock_irq(q->queue_lock);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200798 freed_request(q, is_sync, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 /*
801 * in the very unlikely event that allocation failed and no
802 * requests for this direction was pending, mark us starved
803 * so that freeing of a request in the other direction will
804 * notice us. another possible fix would be to split the
805 * rq mempool into READ and WRITE
806 */
807rq_starved:
Jens Axboe1faa16d2009-04-06 14:48:01 +0200808 if (unlikely(rl->count[is_sync] == 0))
809 rl->starved[is_sync] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 goto out;
812 }
813
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100814 /*
815 * ioc may be NULL here, and ioc_batching will be false. That's
816 * OK, if the queue is under the request limit then requests need
817 * not count toward the nr_batch_requests limit. There will always
818 * be some limit enforced by BLK_BATCH_TIME.
819 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (ioc_batching(q, ioc))
821 ioc->nr_batch_requests--;
Jens Axboe6728cb02008-01-31 13:03:55 +0100822
Jens Axboe1faa16d2009-04-06 14:48:01 +0200823 trace_block_getrq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return rq;
826}
827
828/*
829 * No available requests for this queue, unplug the device and wait for some
830 * requests to become available.
Nick Piggind6344532005-06-28 20:45:14 -0700831 *
832 * Called with q->queue_lock held, and returns with it unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 */
Jens Axboe165125e2007-07-24 09:28:11 +0200834static struct request *get_request_wait(struct request_queue *q, int rw_flags,
Jens Axboe22e2c502005-06-27 10:55:12 +0200835 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Jens Axboe1faa16d2009-04-06 14:48:01 +0200837 const bool is_sync = rw_is_sync(rw_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 struct request *rq;
839
Jens Axboe7749a8d2006-12-13 13:02:26 +0100840 rq = get_request(q, rw_flags, bio, GFP_NOIO);
Nick Piggin450991b2005-06-28 20:45:13 -0700841 while (!rq) {
842 DEFINE_WAIT(wait);
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200843 struct io_context *ioc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 struct request_list *rl = &q->rq;
845
Jens Axboe1faa16d2009-04-06 14:48:01 +0200846 prepare_to_wait_exclusive(&rl->wait[is_sync], &wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 TASK_UNINTERRUPTIBLE);
848
Jens Axboe1faa16d2009-04-06 14:48:01 +0200849 trace_block_sleeprq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200851 __generic_unplug_device(q);
852 spin_unlock_irq(q->queue_lock);
853 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200855 /*
856 * After sleeping, we become a "batching" process and
857 * will be able to allocate at least one request, and
858 * up to a big batch of them for a small period time.
859 * See ioc_batching, ioc_set_batching
860 */
861 ioc = current_io_context(GFP_NOIO, q->node);
862 ioc_set_batching(q, ioc);
Jens Axboe2056a782006-03-23 20:00:26 +0100863
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200864 spin_lock_irq(q->queue_lock);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200865 finish_wait(&rl->wait[is_sync], &wait);
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +0200866
867 rq = get_request(q, rw_flags, bio, GFP_NOIO);
868 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 return rq;
871}
872
Jens Axboe165125e2007-07-24 09:28:11 +0200873struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
875 struct request *rq;
876
877 BUG_ON(rw != READ && rw != WRITE);
878
Nick Piggind6344532005-06-28 20:45:14 -0700879 spin_lock_irq(q->queue_lock);
880 if (gfp_mask & __GFP_WAIT) {
Jens Axboe22e2c502005-06-27 10:55:12 +0200881 rq = get_request_wait(q, rw, NULL);
Nick Piggind6344532005-06-28 20:45:14 -0700882 } else {
Jens Axboe22e2c502005-06-27 10:55:12 +0200883 rq = get_request(q, rw, NULL, gfp_mask);
Nick Piggind6344532005-06-28 20:45:14 -0700884 if (!rq)
885 spin_unlock_irq(q->queue_lock);
886 }
887 /* q->queue_lock is unlocked at this point */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 return rq;
890}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891EXPORT_SYMBOL(blk_get_request);
892
893/**
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300894 * blk_make_request - given a bio, allocate a corresponding struct request.
895 *
896 * @bio: The bio describing the memory mappings that will be submitted for IO.
897 * It may be a chained-bio properly constructed by block/bio layer.
898 *
899 * blk_make_request is the parallel of generic_make_request for BLOCK_PC
900 * type commands. Where the struct request needs to be farther initialized by
901 * the caller. It is passed a &struct bio, which describes the memory info of
902 * the I/O transfer.
903 *
904 * The caller of blk_make_request must make sure that bi_io_vec
905 * are set to describe the memory buffers. That bio_data_dir() will return
906 * the needed direction of the request. (And all bio's in the passed bio-chain
907 * are properly set accordingly)
908 *
909 * If called under none-sleepable conditions, mapped bio buffers must not
910 * need bouncing, by calling the appropriate masked or flagged allocator,
911 * suitable for the target device. Otherwise the call to blk_queue_bounce will
912 * BUG.
Jens Axboe53674ac2009-05-19 19:52:35 +0200913 *
914 * WARNING: When allocating/cloning a bio-chain, careful consideration should be
915 * given to how you allocate bios. In particular, you cannot use __GFP_WAIT for
916 * anything but the first bio in the chain. Otherwise you risk waiting for IO
917 * completion of a bio that hasn't been submitted yet, thus resulting in a
918 * deadlock. Alternatively bios should be allocated using bio_kmalloc() instead
919 * of bio_alloc(), as that avoids the mempool deadlock.
920 * If possible a big IO should be split into smaller parts when allocation
921 * fails. Partial allocation should not be an error, or you risk a live-lock.
Boaz Harrosh79eb63e2009-05-17 18:57:15 +0300922 */
923struct request *blk_make_request(struct request_queue *q, struct bio *bio,
924 gfp_t gfp_mask)
925{
926 struct request *rq = blk_get_request(q, bio_data_dir(bio), gfp_mask);
927
928 if (unlikely(!rq))
929 return ERR_PTR(-ENOMEM);
930
931 for_each_bio(bio) {
932 struct bio *bounce_bio = bio;
933 int ret;
934
935 blk_queue_bounce(q, &bounce_bio);
936 ret = blk_rq_append_bio(q, rq, bounce_bio);
937 if (unlikely(ret)) {
938 blk_put_request(rq);
939 return ERR_PTR(ret);
940 }
941 }
942
943 return rq;
944}
945EXPORT_SYMBOL(blk_make_request);
946
947/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 * blk_requeue_request - put a request back on queue
949 * @q: request queue where request should be inserted
950 * @rq: request to be inserted
951 *
952 * Description:
953 * Drivers often keep queueing requests until the hardware cannot accept
954 * more, when that condition happens we need to put the request back
955 * on the queue. Must be called with queue lock held.
956 */
Jens Axboe165125e2007-07-24 09:28:11 +0200957void blk_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
Tejun Heo9934c8c2009-05-08 11:54:16 +0900959 BUG_ON(blk_queued_rq(rq));
960
Jens Axboe242f9dc2008-09-14 05:55:09 -0700961 blk_delete_timer(rq);
962 blk_clear_rq_complete(rq);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100963 trace_block_rq_requeue(q, rq);
Jens Axboe2056a782006-03-23 20:00:26 +0100964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 if (blk_rq_tagged(rq))
966 blk_queue_end_tag(q, rq);
967
968 elv_requeue_request(q, rq);
969}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970EXPORT_SYMBOL(blk_requeue_request);
971
972/**
Randy Dunlap710027a2008-08-19 20:13:11 +0200973 * blk_insert_request - insert a special request into a request queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 * @q: request queue where request should be inserted
975 * @rq: request to be inserted
976 * @at_head: insert request at head or tail of queue
977 * @data: private data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 *
979 * Description:
980 * Many block devices need to execute commands asynchronously, so they don't
981 * block the whole kernel from preemption during request execution. This is
982 * accomplished normally by inserting aritficial requests tagged as
Randy Dunlap710027a2008-08-19 20:13:11 +0200983 * REQ_TYPE_SPECIAL in to the corresponding request queue, and letting them
984 * be scheduled for actual execution by the request queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 *
986 * We have the option of inserting the head or the tail of the queue.
987 * Typically we use the tail for new ioctls and so forth. We use the head
988 * of the queue for things like a QUEUE_FULL message from a device, or a
989 * host that is unable to accept a particular command.
990 */
Jens Axboe165125e2007-07-24 09:28:11 +0200991void blk_insert_request(struct request_queue *q, struct request *rq,
Tejun Heo 867d1192005-04-24 02:06:05 -0500992 int at_head, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
Tejun Heo 867d1192005-04-24 02:06:05 -0500994 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 unsigned long flags;
996
997 /*
998 * tell I/O scheduler that this isn't a regular read/write (ie it
999 * must not attempt merges on this) and that it acts as a soft
1000 * barrier
1001 */
Jens Axboe4aff5e22006-08-10 08:44:47 +02001002 rq->cmd_type = REQ_TYPE_SPECIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 rq->special = data;
1005
1006 spin_lock_irqsave(q->queue_lock, flags);
1007
1008 /*
1009 * If command is tagged, release the tag
1010 */
Tejun Heo 867d1192005-04-24 02:06:05 -05001011 if (blk_rq_tagged(rq))
1012 blk_queue_end_tag(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Jerome Marchandb238b3d2007-10-23 15:05:46 +02001014 drive_stat_acct(rq, 1);
Tejun Heo 867d1192005-04-24 02:06:05 -05001015 __elv_add_request(q, rq, where, 0);
Tejun Heoa7f55792009-04-23 11:05:17 +09001016 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 spin_unlock_irqrestore(q->queue_lock, flags);
1018}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019EXPORT_SYMBOL(blk_insert_request);
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021/*
1022 * add-request adds a request to the linked list.
1023 * queue lock is held and interrupts disabled, as we muck with the
1024 * request queue list.
1025 */
Jens Axboe6728cb02008-01-31 13:03:55 +01001026static inline void add_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
Jerome Marchandb238b3d2007-10-23 15:05:46 +02001028 drive_stat_acct(req, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 /*
1031 * elevator indicated where it wants this request to be
1032 * inserted at elevator_merge time
1033 */
1034 __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
1035}
Jens Axboe6728cb02008-01-31 13:03:55 +01001036
Tejun Heo074a7ac2008-08-25 19:56:14 +09001037static void part_round_stats_single(int cpu, struct hd_struct *part,
1038 unsigned long now)
1039{
1040 if (now == part->stamp)
1041 return;
1042
1043 if (part->in_flight) {
1044 __part_stat_add(cpu, part, time_in_queue,
1045 part->in_flight * (now - part->stamp));
1046 __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
1047 }
1048 part->stamp = now;
1049}
1050
1051/**
Randy Dunlap496aa8a2008-10-16 07:46:23 +02001052 * part_round_stats() - Round off the performance stats on a struct disk_stats.
1053 * @cpu: cpu number for stats access
1054 * @part: target partition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 *
1056 * The average IO queue length and utilisation statistics are maintained
1057 * by observing the current state of the queue length and the amount of
1058 * time it has been in this state for.
1059 *
1060 * Normally, that accounting is done on IO completion, but that can result
1061 * in more than a second's worth of IO being accounted for within any one
1062 * second, leading to >100% utilisation. To deal with that, we call this
1063 * function to do a round-off before returning the results when reading
1064 * /proc/diskstats. This accounts immediately for all queue usage up to
1065 * the current jiffies and restarts the counters again.
1066 */
Tejun Heoc9959052008-08-25 19:47:21 +09001067void part_round_stats(int cpu, struct hd_struct *part)
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001068{
1069 unsigned long now = jiffies;
1070
Tejun Heo074a7ac2008-08-25 19:56:14 +09001071 if (part->partno)
1072 part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
1073 part_round_stats_single(cpu, part, now);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001074}
Tejun Heo074a7ac2008-08-25 19:56:14 +09001075EXPORT_SYMBOL_GPL(part_round_stats);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077/*
1078 * queue lock must be held
1079 */
Jens Axboe165125e2007-07-24 09:28:11 +02001080void __blk_put_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (unlikely(!q))
1083 return;
1084 if (unlikely(--req->ref_count))
1085 return;
1086
Tejun Heo8922e162005-10-20 16:23:44 +02001087 elv_completed_request(q, req);
1088
Boaz Harrosh1cd96c22009-03-24 12:35:07 +01001089 /* this is a bio leak */
1090 WARN_ON(req->bio != NULL);
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 /*
1093 * Request may not have originated from ll_rw_blk. if not,
1094 * it didn't come out of our reserved rq pools
1095 */
Jens Axboe49171e52006-08-10 08:59:11 +02001096 if (req->cmd_flags & REQ_ALLOCED) {
Jens Axboe1faa16d2009-04-06 14:48:01 +02001097 int is_sync = rq_is_sync(req) != 0;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001098 int priv = req->cmd_flags & REQ_ELVPRIV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 BUG_ON(!list_empty(&req->queuelist));
Jens Axboe98170642006-07-28 09:23:08 +02001101 BUG_ON(!hlist_unhashed(&req->hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 blk_free_request(q, req);
Jens Axboe1faa16d2009-04-06 14:48:01 +02001104 freed_request(q, is_sync, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 }
1106}
Mike Christie6e39b69e2005-11-11 05:30:24 -06001107EXPORT_SYMBOL_GPL(__blk_put_request);
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109void blk_put_request(struct request *req)
1110{
Tejun Heo8922e162005-10-20 16:23:44 +02001111 unsigned long flags;
Jens Axboe165125e2007-07-24 09:28:11 +02001112 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
FUJITA Tomonori52a93ba2008-07-15 21:21:45 +02001114 spin_lock_irqsave(q->queue_lock, flags);
1115 __blk_put_request(q, req);
1116 spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118EXPORT_SYMBOL(blk_put_request);
1119
Jens Axboe86db1e22008-01-29 14:53:40 +01001120void init_request_from_bio(struct request *req, struct bio *bio)
Tejun Heo52d9e672006-01-06 09:49:58 +01001121{
Jens Axboec7c22e42008-09-13 20:26:01 +02001122 req->cpu = bio->bi_comp_cpu;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001123 req->cmd_type = REQ_TYPE_FS;
Tejun Heo52d9e672006-01-06 09:49:58 +01001124
1125 /*
1126 * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
1127 */
Mike Christie6000a362008-08-19 18:45:30 -05001128 if (bio_rw_ahead(bio))
1129 req->cmd_flags |= (REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
1130 REQ_FAILFAST_DRIVER);
1131 if (bio_failfast_dev(bio))
1132 req->cmd_flags |= REQ_FAILFAST_DEV;
1133 if (bio_failfast_transport(bio))
1134 req->cmd_flags |= REQ_FAILFAST_TRANSPORT;
1135 if (bio_failfast_driver(bio))
1136 req->cmd_flags |= REQ_FAILFAST_DRIVER;
Tejun Heo52d9e672006-01-06 09:49:58 +01001137
David Woodhousefb2dce82008-08-05 18:01:53 +01001138 if (unlikely(bio_discard(bio))) {
David Woodhousee17fc0a2008-08-09 16:42:20 +01001139 req->cmd_flags |= REQ_DISCARD;
1140 if (bio_barrier(bio))
1141 req->cmd_flags |= REQ_SOFTBARRIER;
David Woodhousefb2dce82008-08-05 18:01:53 +01001142 req->q->prepare_discard_fn(req->q, req);
David Woodhousee17fc0a2008-08-09 16:42:20 +01001143 } else if (unlikely(bio_barrier(bio)))
Tejun Heoe4025f62009-04-23 11:05:17 +09001144 req->cmd_flags |= REQ_HARDBARRIER;
Tejun Heo52d9e672006-01-06 09:49:58 +01001145
Jens Axboeb31dc662006-06-13 08:26:10 +02001146 if (bio_sync(bio))
Jens Axboe4aff5e22006-08-10 08:44:47 +02001147 req->cmd_flags |= REQ_RW_SYNC;
Jens Axboe5404bc72006-08-10 09:01:02 +02001148 if (bio_rw_meta(bio))
1149 req->cmd_flags |= REQ_RW_META;
Jens Axboeaeb6faf2009-04-06 14:48:07 +02001150 if (bio_noidle(bio))
1151 req->cmd_flags |= REQ_NOIDLE;
Jens Axboeb31dc662006-06-13 08:26:10 +02001152
Tejun Heo52d9e672006-01-06 09:49:58 +01001153 req->errors = 0;
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001154 req->__sector = bio->bi_sector;
Tejun Heo52d9e672006-01-06 09:49:58 +01001155 req->ioprio = bio_prio(bio);
NeilBrownbc1c56f2007-08-16 13:31:30 +02001156 blk_rq_bio_prep(req->q, req, bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001157}
1158
Jens Axboe644b2d92009-04-06 14:48:06 +02001159/*
1160 * Only disabling plugging for non-rotational devices if it does tagging
1161 * as well, otherwise we do need the proper merging
1162 */
1163static inline bool queue_should_plug(struct request_queue *q)
1164{
1165 return !(blk_queue_nonrot(q) && blk_queue_tagged(q));
1166}
1167
Jens Axboe165125e2007-07-24 09:28:11 +02001168static int __make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
Nick Piggin450991b2005-06-28 20:45:13 -07001170 struct request *req;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001171 int el_ret;
1172 unsigned int bytes = bio->bi_size;
Jens Axboe51da90f2006-07-18 04:14:45 +02001173 const unsigned short prio = bio_prio(bio);
1174 const int sync = bio_sync(bio);
Jens Axboe213d9412009-01-06 09:16:05 +01001175 const int unplug = bio_unplug(bio);
Jens Axboe7749a8d2006-12-13 13:02:26 +01001176 int rw_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 /*
1179 * low level driver can indicate that it wants pages above a
1180 * certain limit bounced to low memory (ie for highmem, or even
1181 * ISA dma in theory)
1182 */
1183 blk_queue_bounce(q, &bio);
1184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 spin_lock_irq(q->queue_lock);
1186
Tejun Heoa7384672008-11-28 13:32:03 +09001187 if (unlikely(bio_barrier(bio)) || elv_queue_empty(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 goto get_rq;
1189
1190 el_ret = elv_merge(q, &req, bio);
1191 switch (el_ret) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001192 case ELEVATOR_BACK_MERGE:
1193 BUG_ON(!rq_mergeable(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Jens Axboe6728cb02008-01-31 13:03:55 +01001195 if (!ll_back_merge_fn(q, req, bio))
1196 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001198 trace_block_bio_backmerge(q, bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001199
Jens Axboe6728cb02008-01-31 13:03:55 +01001200 req->biotail->bi_next = bio;
1201 req->biotail = bio;
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001202 req->__data_len += bytes;
Jens Axboe6728cb02008-01-31 13:03:55 +01001203 req->ioprio = ioprio_best(req->ioprio, prio);
Jens Axboeab780f12008-08-26 10:25:02 +02001204 if (!blk_rq_cpu_valid(req))
1205 req->cpu = bio->bi_comp_cpu;
Jens Axboe6728cb02008-01-31 13:03:55 +01001206 drive_stat_acct(req, 0);
1207 if (!attempt_back_merge(q, req))
1208 elv_merged_request(q, req, el_ret);
1209 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Jens Axboe6728cb02008-01-31 13:03:55 +01001211 case ELEVATOR_FRONT_MERGE:
1212 BUG_ON(!rq_mergeable(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Jens Axboe6728cb02008-01-31 13:03:55 +01001214 if (!ll_front_merge_fn(q, req, bio))
1215 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001217 trace_block_bio_frontmerge(q, bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001218
Jens Axboe6728cb02008-01-31 13:03:55 +01001219 bio->bi_next = req->bio;
1220 req->bio = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Jens Axboe6728cb02008-01-31 13:03:55 +01001222 /*
1223 * may not be valid. if the low level driver said
1224 * it didn't need a bounce buffer then it better
1225 * not touch req->buffer either...
1226 */
1227 req->buffer = bio_data(bio);
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001228 req->__sector = bio->bi_sector;
1229 req->__data_len += bytes;
Jens Axboe6728cb02008-01-31 13:03:55 +01001230 req->ioprio = ioprio_best(req->ioprio, prio);
Jens Axboeab780f12008-08-26 10:25:02 +02001231 if (!blk_rq_cpu_valid(req))
1232 req->cpu = bio->bi_comp_cpu;
Jens Axboe6728cb02008-01-31 13:03:55 +01001233 drive_stat_acct(req, 0);
1234 if (!attempt_front_merge(q, req))
1235 elv_merged_request(q, req, el_ret);
1236 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Jens Axboe6728cb02008-01-31 13:03:55 +01001238 /* ELV_NO_MERGE: elevator says don't/can't merge. */
1239 default:
1240 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 }
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243get_rq:
Nick Piggin450991b2005-06-28 20:45:13 -07001244 /*
Jens Axboe7749a8d2006-12-13 13:02:26 +01001245 * This sync check and mask will be re-done in init_request_from_bio(),
1246 * but we need to set it earlier to expose the sync flag to the
1247 * rq allocator and io schedulers.
1248 */
1249 rw_flags = bio_data_dir(bio);
1250 if (sync)
1251 rw_flags |= REQ_RW_SYNC;
1252
1253 /*
Nick Piggin450991b2005-06-28 20:45:13 -07001254 * Grab a free request. This is might sleep but can not fail.
Nick Piggind6344532005-06-28 20:45:14 -07001255 * Returns with the queue unlocked.
Nick Piggin450991b2005-06-28 20:45:13 -07001256 */
Jens Axboe7749a8d2006-12-13 13:02:26 +01001257 req = get_request_wait(q, rw_flags, bio);
Nick Piggind6344532005-06-28 20:45:14 -07001258
Nick Piggin450991b2005-06-28 20:45:13 -07001259 /*
1260 * After dropping the lock and possibly sleeping here, our request
1261 * may now be mergeable after it had proven unmergeable (above).
1262 * We don't worry about that case for efficiency. It won't happen
1263 * often, and the elevators are able to handle it.
1264 */
Tejun Heo52d9e672006-01-06 09:49:58 +01001265 init_request_from_bio(req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Nick Piggin450991b2005-06-28 20:45:13 -07001267 spin_lock_irq(q->queue_lock);
Jens Axboec7c22e42008-09-13 20:26:01 +02001268 if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags) ||
1269 bio_flagged(bio, BIO_CPU_AFFINE))
1270 req->cpu = blk_cpu_to_group(smp_processor_id());
Jens Axboe644b2d92009-04-06 14:48:06 +02001271 if (queue_should_plug(q) && elv_queue_empty(q))
Nick Piggin450991b2005-06-28 20:45:13 -07001272 blk_plug_device(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 add_request(q, req);
1274out:
Jens Axboe644b2d92009-04-06 14:48:06 +02001275 if (unplug || !queue_should_plug(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 __generic_unplug_device(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 spin_unlock_irq(q->queue_lock);
1278 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279}
1280
1281/*
1282 * If bio->bi_dev is a partition, remap the location
1283 */
1284static inline void blk_partition_remap(struct bio *bio)
1285{
1286 struct block_device *bdev = bio->bi_bdev;
1287
Jens Axboebf2de6f2007-09-27 13:01:25 +02001288 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 struct hd_struct *p = bdev->bd_part;
Jens Axboea3623572005-11-01 09:26:16 +01001290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 bio->bi_sector += p->start_sect;
1292 bio->bi_bdev = bdev->bd_contains;
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001293
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001294 trace_block_remap(bdev_get_queue(bio->bi_bdev), bio,
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001295 bdev->bd_dev, bio->bi_sector,
1296 bio->bi_sector - p->start_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
1298}
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300static void handle_bad_sector(struct bio *bio)
1301{
1302 char b[BDEVNAME_SIZE];
1303
1304 printk(KERN_INFO "attempt to access beyond end of device\n");
1305 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1306 bdevname(bio->bi_bdev, b),
1307 bio->bi_rw,
1308 (unsigned long long)bio->bi_sector + bio_sectors(bio),
1309 (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
1310
1311 set_bit(BIO_EOF, &bio->bi_flags);
1312}
1313
Akinobu Mitac17bb492006-12-08 02:39:46 -08001314#ifdef CONFIG_FAIL_MAKE_REQUEST
1315
1316static DECLARE_FAULT_ATTR(fail_make_request);
1317
1318static int __init setup_fail_make_request(char *str)
1319{
1320 return setup_fault_attr(&fail_make_request, str);
1321}
1322__setup("fail_make_request=", setup_fail_make_request);
1323
1324static int should_fail_request(struct bio *bio)
1325{
Tejun Heoeddb2e22008-08-25 19:56:13 +09001326 struct hd_struct *part = bio->bi_bdev->bd_part;
1327
1328 if (part_to_disk(part)->part0.make_it_fail || part->make_it_fail)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001329 return should_fail(&fail_make_request, bio->bi_size);
1330
1331 return 0;
1332}
1333
1334static int __init fail_make_request_debugfs(void)
1335{
1336 return init_fault_attr_dentries(&fail_make_request,
1337 "fail_make_request");
1338}
1339
1340late_initcall(fail_make_request_debugfs);
1341
1342#else /* CONFIG_FAIL_MAKE_REQUEST */
1343
1344static inline int should_fail_request(struct bio *bio)
1345{
1346 return 0;
1347}
1348
1349#endif /* CONFIG_FAIL_MAKE_REQUEST */
1350
Jens Axboec07e2b42007-07-18 13:27:58 +02001351/*
1352 * Check whether this bio extends beyond the end of the device.
1353 */
1354static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1355{
1356 sector_t maxsector;
1357
1358 if (!nr_sectors)
1359 return 0;
1360
1361 /* Test device or partition size, when known. */
1362 maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
1363 if (maxsector) {
1364 sector_t sector = bio->bi_sector;
1365
1366 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1367 /*
1368 * This may well happen - the kernel calls bread()
1369 * without checking the size of the device, e.g., when
1370 * mounting a device.
1371 */
1372 handle_bad_sector(bio);
1373 return 1;
1374 }
1375 }
1376
1377 return 0;
1378}
1379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001381 * generic_make_request - hand a buffer to its device driver for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 * @bio: The bio describing the location in memory and on the device.
1383 *
1384 * generic_make_request() is used to make I/O requests of block
1385 * devices. It is passed a &struct bio, which describes the I/O that needs
1386 * to be done.
1387 *
1388 * generic_make_request() does not return any status. The
1389 * success/failure status of the request, along with notification of
1390 * completion, is delivered asynchronously through the bio->bi_end_io
1391 * function described (one day) else where.
1392 *
1393 * The caller of generic_make_request must make sure that bi_io_vec
1394 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1395 * set to describe the device address, and the
1396 * bi_end_io and optionally bi_private are set to describe how
1397 * completion notification should be signaled.
1398 *
1399 * generic_make_request and the drivers it calls may use bi_next if this
1400 * bio happens to be merged with someone else, and may change bi_dev and
1401 * bi_sector for remaps as it sees fit. So the values of these fields
1402 * should NOT be depended on after the call to generic_make_request.
1403 */
Neil Brownd89d8792007-05-01 09:53:42 +02001404static inline void __generic_make_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
Jens Axboe165125e2007-07-24 09:28:11 +02001406 struct request_queue *q;
NeilBrown5ddfe962006-10-30 22:07:21 -08001407 sector_t old_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 int ret, nr_sectors = bio_sectors(bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001409 dev_t old_dev;
Jens Axboe51fd77b2007-11-02 08:49:08 +01001410 int err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Jens Axboec07e2b42007-07-18 13:27:58 +02001414 if (bio_check_eod(bio, nr_sectors))
1415 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 /*
1418 * Resolve the mapping until finished. (drivers are
1419 * still free to implement/resolve their own stacking
1420 * by explicitly returning 0)
1421 *
1422 * NOTE: we don't repeat the blk_size check for each new device.
1423 * Stacking drivers are expected to know what they are doing.
1424 */
NeilBrown5ddfe962006-10-30 22:07:21 -08001425 old_sector = -1;
Jens Axboe2056a782006-03-23 20:00:26 +01001426 old_dev = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 do {
1428 char b[BDEVNAME_SIZE];
1429
1430 q = bdev_get_queue(bio->bi_bdev);
Tejun Heoa7384672008-11-28 13:32:03 +09001431 if (unlikely(!q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 printk(KERN_ERR
1433 "generic_make_request: Trying to access "
1434 "nonexistent block-device %s (%Lu)\n",
1435 bdevname(bio->bi_bdev, b),
1436 (long long) bio->bi_sector);
Tejun Heoa7384672008-11-28 13:32:03 +09001437 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 }
1439
Jens Axboe4fa253f2007-07-18 13:13:10 +02001440 if (unlikely(nr_sectors > q->max_hw_sectors)) {
Jens Axboe6728cb02008-01-31 13:03:55 +01001441 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 bdevname(bio->bi_bdev, b),
1443 bio_sectors(bio),
1444 q->max_hw_sectors);
1445 goto end_io;
1446 }
1447
Nick Pigginfde6ad22005-06-23 00:08:53 -07001448 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 goto end_io;
1450
Akinobu Mitac17bb492006-12-08 02:39:46 -08001451 if (should_fail_request(bio))
1452 goto end_io;
1453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 /*
1455 * If this device has partitions, remap block n
1456 * of partition p to block n+start(p) of the disk.
1457 */
1458 blk_partition_remap(bio);
1459
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +02001460 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio))
1461 goto end_io;
1462
NeilBrown5ddfe962006-10-30 22:07:21 -08001463 if (old_sector != -1)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001464 trace_block_remap(q, bio, old_dev, bio->bi_sector,
NeilBrown5ddfe962006-10-30 22:07:21 -08001465 old_sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001466
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001467 trace_block_bio_queue(q, bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001468
NeilBrown5ddfe962006-10-30 22:07:21 -08001469 old_sector = bio->bi_sector;
Jens Axboe2056a782006-03-23 20:00:26 +01001470 old_dev = bio->bi_bdev->bd_dev;
1471
Jens Axboec07e2b42007-07-18 13:27:58 +02001472 if (bio_check_eod(bio, nr_sectors))
1473 goto end_io;
Tejun Heoa7384672008-11-28 13:32:03 +09001474
1475 if (bio_discard(bio) && !q->prepare_discard_fn) {
Jens Axboe51fd77b2007-11-02 08:49:08 +01001476 err = -EOPNOTSUPP;
1477 goto end_io;
1478 }
Jens Axboecec07072009-01-13 15:28:32 +01001479 if (bio_barrier(bio) && bio_has_data(bio) &&
1480 (q->next_ordered == QUEUE_ORDERED_NONE)) {
1481 err = -EOPNOTSUPP;
1482 goto end_io;
1483 }
NeilBrown5ddfe962006-10-30 22:07:21 -08001484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 ret = q->make_request_fn(q, bio);
1486 } while (ret);
Tejun Heoa7384672008-11-28 13:32:03 +09001487
1488 return;
1489
1490end_io:
1491 bio_endio(bio, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492}
1493
Neil Brownd89d8792007-05-01 09:53:42 +02001494/*
1495 * We only want one ->make_request_fn to be active at a time,
1496 * else stack usage with stacked devices could be a problem.
1497 * So use current->bio_{list,tail} to keep a list of requests
1498 * submited by a make_request_fn function.
1499 * current->bio_tail is also used as a flag to say if
1500 * generic_make_request is currently active in this task or not.
1501 * If it is NULL, then no make_request is active. If it is non-NULL,
1502 * then a make_request is active, and new requests should be added
1503 * at the tail
1504 */
1505void generic_make_request(struct bio *bio)
1506{
1507 if (current->bio_tail) {
1508 /* make_request is active */
1509 *(current->bio_tail) = bio;
1510 bio->bi_next = NULL;
1511 current->bio_tail = &bio->bi_next;
1512 return;
1513 }
1514 /* following loop may be a bit non-obvious, and so deserves some
1515 * explanation.
1516 * Before entering the loop, bio->bi_next is NULL (as all callers
1517 * ensure that) so we have a list with a single bio.
1518 * We pretend that we have just taken it off a longer list, so
1519 * we assign bio_list to the next (which is NULL) and bio_tail
1520 * to &bio_list, thus initialising the bio_list of new bios to be
1521 * added. __generic_make_request may indeed add some more bios
1522 * through a recursive call to generic_make_request. If it
1523 * did, we find a non-NULL value in bio_list and re-enter the loop
1524 * from the top. In this case we really did just take the bio
1525 * of the top of the list (no pretending) and so fixup bio_list and
1526 * bio_tail or bi_next, and call into __generic_make_request again.
1527 *
1528 * The loop was structured like this to make only one call to
1529 * __generic_make_request (which is important as it is large and
1530 * inlined) and to keep the structure simple.
1531 */
1532 BUG_ON(bio->bi_next);
1533 do {
1534 current->bio_list = bio->bi_next;
1535 if (bio->bi_next == NULL)
1536 current->bio_tail = &current->bio_list;
1537 else
1538 bio->bi_next = NULL;
1539 __generic_make_request(bio);
1540 bio = current->bio_list;
1541 } while (bio);
1542 current->bio_tail = NULL; /* deactivate */
1543}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544EXPORT_SYMBOL(generic_make_request);
1545
1546/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001547 * submit_bio - submit a bio to the block device layer for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1549 * @bio: The &struct bio which describes the I/O
1550 *
1551 * submit_bio() is very similar in purpose to generic_make_request(), and
1552 * uses that function to do most of the work. Both are fairly rough
Randy Dunlap710027a2008-08-19 20:13:11 +02001553 * interfaces; @bio must be presetup and ready for I/O.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 *
1555 */
1556void submit_bio(int rw, struct bio *bio)
1557{
1558 int count = bio_sectors(bio);
1559
Jens Axboe22e2c502005-06-27 10:55:12 +02001560 bio->bi_rw |= rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Jens Axboebf2de6f2007-09-27 13:01:25 +02001562 /*
1563 * If it's a regular read/write or a barrier with data attached,
1564 * go through the normal accounting stuff before submission.
1565 */
Jens Axboea9c701e2008-08-08 11:04:44 +02001566 if (bio_has_data(bio)) {
Jens Axboebf2de6f2007-09-27 13:01:25 +02001567 if (rw & WRITE) {
1568 count_vm_events(PGPGOUT, count);
1569 } else {
1570 task_io_account_read(bio->bi_size);
1571 count_vm_events(PGPGIN, count);
1572 }
1573
1574 if (unlikely(block_dump)) {
1575 char b[BDEVNAME_SIZE];
1576 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001577 current->comm, task_pid_nr(current),
Jens Axboebf2de6f2007-09-27 13:01:25 +02001578 (rw & WRITE) ? "WRITE" : "READ",
1579 (unsigned long long)bio->bi_sector,
Jens Axboe6728cb02008-01-31 13:03:55 +01001580 bdevname(bio->bi_bdev, b));
Jens Axboebf2de6f2007-09-27 13:01:25 +02001581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 }
1583
1584 generic_make_request(bio);
1585}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586EXPORT_SYMBOL(submit_bio);
1587
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001588/**
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001589 * blk_rq_check_limits - Helper function to check a request for the queue limit
1590 * @q: the queue
1591 * @rq: the request being checked
1592 *
1593 * Description:
1594 * @rq may have been made based on weaker limitations of upper-level queues
1595 * in request stacking drivers, and it may violate the limitation of @q.
1596 * Since the block layer and the underlying device driver trust @rq
1597 * after it is inserted to @q, it should be checked against @q before
1598 * the insertion using this generic function.
1599 *
1600 * This function should also be useful for request stacking drivers
1601 * in some cases below, so export this fuction.
1602 * Request stacking drivers like request-based dm may change the queue
1603 * limits while requests are in the queue (e.g. dm's table swapping).
1604 * Such request stacking drivers should check those requests agaist
1605 * the new queue limits again when they dispatch those requests,
1606 * although such checkings are also done against the old queue limits
1607 * when submitting requests.
1608 */
1609int blk_rq_check_limits(struct request_queue *q, struct request *rq)
1610{
Tejun Heo83096eb2009-05-07 22:24:39 +09001611 if (blk_rq_sectors(rq) > q->max_sectors ||
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001612 blk_rq_bytes(rq) > q->max_hw_sectors << 9) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001613 printk(KERN_ERR "%s: over max size limit.\n", __func__);
1614 return -EIO;
1615 }
1616
1617 /*
1618 * queue's settings related to segment counting like q->bounce_pfn
1619 * may differ from that of other stacking queues.
1620 * Recalculate it to check the request correctly on this queue's
1621 * limitation.
1622 */
1623 blk_recalc_rq_segments(rq);
1624 if (rq->nr_phys_segments > q->max_phys_segments ||
1625 rq->nr_phys_segments > q->max_hw_segments) {
1626 printk(KERN_ERR "%s: over max segments limit.\n", __func__);
1627 return -EIO;
1628 }
1629
1630 return 0;
1631}
1632EXPORT_SYMBOL_GPL(blk_rq_check_limits);
1633
1634/**
1635 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
1636 * @q: the queue to submit the request
1637 * @rq: the request being queued
1638 */
1639int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
1640{
1641 unsigned long flags;
1642
1643 if (blk_rq_check_limits(q, rq))
1644 return -EIO;
1645
1646#ifdef CONFIG_FAIL_MAKE_REQUEST
1647 if (rq->rq_disk && rq->rq_disk->part0.make_it_fail &&
1648 should_fail(&fail_make_request, blk_rq_bytes(rq)))
1649 return -EIO;
1650#endif
1651
1652 spin_lock_irqsave(q->queue_lock, flags);
1653
1654 /*
1655 * Submitting request must be dequeued before calling this function
1656 * because it will be linked to another request_queue
1657 */
1658 BUG_ON(blk_queued_rq(rq));
1659
1660 drive_stat_acct(rq, 1);
1661 __elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 0);
1662
1663 spin_unlock_irqrestore(q->queue_lock, flags);
1664
1665 return 0;
1666}
1667EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
1668
Jens Axboebc58ba92009-01-23 10:54:44 +01001669static void blk_account_io_completion(struct request *req, unsigned int bytes)
1670{
Jens Axboec2553b52009-04-24 08:10:11 +02001671 if (blk_do_io_stat(req)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01001672 const int rw = rq_data_dir(req);
1673 struct hd_struct *part;
1674 int cpu;
1675
1676 cpu = part_stat_lock();
Tejun Heo83096eb2009-05-07 22:24:39 +09001677 part = disk_map_sector_rcu(req->rq_disk, blk_rq_pos(req));
Jens Axboebc58ba92009-01-23 10:54:44 +01001678 part_stat_add(cpu, part, sectors[rw], bytes >> 9);
1679 part_stat_unlock();
1680 }
1681}
1682
1683static void blk_account_io_done(struct request *req)
1684{
Jens Axboebc58ba92009-01-23 10:54:44 +01001685 /*
1686 * Account IO completion. bar_rq isn't accounted as a normal
1687 * IO on queueing nor completion. Accounting the containing
1688 * request is enough.
1689 */
Jens Axboec2553b52009-04-24 08:10:11 +02001690 if (blk_do_io_stat(req) && req != &req->q->bar_rq) {
Jens Axboebc58ba92009-01-23 10:54:44 +01001691 unsigned long duration = jiffies - req->start_time;
1692 const int rw = rq_data_dir(req);
1693 struct hd_struct *part;
1694 int cpu;
1695
1696 cpu = part_stat_lock();
Tejun Heo83096eb2009-05-07 22:24:39 +09001697 part = disk_map_sector_rcu(req->rq_disk, blk_rq_pos(req));
Jens Axboebc58ba92009-01-23 10:54:44 +01001698
1699 part_stat_inc(cpu, part, ios[rw]);
1700 part_stat_add(cpu, part, ticks[rw], duration);
1701 part_round_stats(cpu, part);
1702 part_dec_in_flight(part);
1703
1704 part_stat_unlock();
1705 }
1706}
1707
Tejun Heo9934c8c2009-05-08 11:54:16 +09001708/**
1709 * blk_peek_request - peek at the top of a request queue
1710 * @q: request queue to peek at
1711 *
1712 * Description:
1713 * Return the request at the top of @q. The returned request
1714 * should be started using blk_start_request() before LLD starts
1715 * processing it.
1716 *
1717 * Return:
1718 * Pointer to the request at the top of @q if available. Null
1719 * otherwise.
1720 *
1721 * Context:
1722 * queue_lock must be held.
1723 */
1724struct request *blk_peek_request(struct request_queue *q)
Tejun Heo158dbda2009-04-23 11:05:18 +09001725{
1726 struct request *rq;
1727 int ret;
1728
1729 while ((rq = __elv_next_request(q)) != NULL) {
1730 if (!(rq->cmd_flags & REQ_STARTED)) {
1731 /*
1732 * This is the first time the device driver
1733 * sees this request (possibly after
1734 * requeueing). Notify IO scheduler.
1735 */
1736 if (blk_sorted_rq(rq))
1737 elv_activate_rq(q, rq);
1738
1739 /*
1740 * just mark as started even if we don't start
1741 * it, a request that has been delayed should
1742 * not be passed by new incoming requests
1743 */
1744 rq->cmd_flags |= REQ_STARTED;
1745 trace_block_rq_issue(q, rq);
1746 }
1747
1748 if (!q->boundary_rq || q->boundary_rq == rq) {
1749 q->end_sector = rq_end_sector(rq);
1750 q->boundary_rq = NULL;
1751 }
1752
1753 if (rq->cmd_flags & REQ_DONTPREP)
1754 break;
1755
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001756 if (q->dma_drain_size && blk_rq_bytes(rq)) {
Tejun Heo158dbda2009-04-23 11:05:18 +09001757 /*
1758 * make sure space for the drain appears we
1759 * know we can do this because max_hw_segments
1760 * has been adjusted to be one fewer than the
1761 * device can handle
1762 */
1763 rq->nr_phys_segments++;
1764 }
1765
1766 if (!q->prep_rq_fn)
1767 break;
1768
1769 ret = q->prep_rq_fn(q, rq);
1770 if (ret == BLKPREP_OK) {
1771 break;
1772 } else if (ret == BLKPREP_DEFER) {
1773 /*
1774 * the request may have been (partially) prepped.
1775 * we need to keep this request in the front to
1776 * avoid resource deadlock. REQ_STARTED will
1777 * prevent other fs requests from passing this one.
1778 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001779 if (q->dma_drain_size && blk_rq_bytes(rq) &&
Tejun Heo158dbda2009-04-23 11:05:18 +09001780 !(rq->cmd_flags & REQ_DONTPREP)) {
1781 /*
1782 * remove the space for the drain we added
1783 * so that we don't add it again
1784 */
1785 --rq->nr_phys_segments;
1786 }
1787
1788 rq = NULL;
1789 break;
1790 } else if (ret == BLKPREP_KILL) {
1791 rq->cmd_flags |= REQ_QUIET;
Tejun Heo40cbbb72009-04-23 11:05:19 +09001792 __blk_end_request_all(rq, -EIO);
Tejun Heo158dbda2009-04-23 11:05:18 +09001793 } else {
1794 printk(KERN_ERR "%s: bad return=%d\n", __func__, ret);
1795 break;
1796 }
1797 }
1798
1799 return rq;
1800}
Tejun Heo9934c8c2009-05-08 11:54:16 +09001801EXPORT_SYMBOL(blk_peek_request);
Tejun Heo158dbda2009-04-23 11:05:18 +09001802
Tejun Heo9934c8c2009-05-08 11:54:16 +09001803void blk_dequeue_request(struct request *rq)
Tejun Heo158dbda2009-04-23 11:05:18 +09001804{
Tejun Heo9934c8c2009-05-08 11:54:16 +09001805 struct request_queue *q = rq->q;
1806
Tejun Heo158dbda2009-04-23 11:05:18 +09001807 BUG_ON(list_empty(&rq->queuelist));
1808 BUG_ON(ELV_ON_HASH(rq));
1809
1810 list_del_init(&rq->queuelist);
1811
1812 /*
1813 * the time frame between a request being removed from the lists
1814 * and to it is freed is accounted as io that is in progress at
1815 * the driver side.
1816 */
1817 if (blk_account_rq(rq))
Jens Axboe0a7ae2f2009-05-20 08:54:31 +02001818 q->in_flight[rq_is_sync(rq)]++;
Tejun Heo158dbda2009-04-23 11:05:18 +09001819}
1820
Tejun Heo5efccd12009-04-23 11:05:18 +09001821/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09001822 * blk_start_request - start request processing on the driver
1823 * @req: request to dequeue
1824 *
1825 * Description:
1826 * Dequeue @req and start timeout timer on it. This hands off the
1827 * request to the driver.
1828 *
1829 * Block internal functions which don't want to start timer should
1830 * call blk_dequeue_request().
1831 *
1832 * Context:
1833 * queue_lock must be held.
1834 */
1835void blk_start_request(struct request *req)
1836{
1837 blk_dequeue_request(req);
1838
1839 /*
Tejun Heo5f49f632009-05-19 18:33:05 +09001840 * We are now handing the request to the hardware, initialize
1841 * resid_len to full count and add the timeout handler.
Tejun Heo9934c8c2009-05-08 11:54:16 +09001842 */
Tejun Heo5f49f632009-05-19 18:33:05 +09001843 req->resid_len = blk_rq_bytes(req);
Tejun Heo9934c8c2009-05-08 11:54:16 +09001844 blk_add_timer(req);
1845}
1846EXPORT_SYMBOL(blk_start_request);
1847
1848/**
1849 * blk_fetch_request - fetch a request from a request queue
1850 * @q: request queue to fetch a request from
1851 *
1852 * Description:
1853 * Return the request at the top of @q. The request is started on
1854 * return and LLD can start processing it immediately.
1855 *
1856 * Return:
1857 * Pointer to the request at the top of @q if available. Null
1858 * otherwise.
1859 *
1860 * Context:
1861 * queue_lock must be held.
1862 */
1863struct request *blk_fetch_request(struct request_queue *q)
1864{
1865 struct request *rq;
1866
1867 rq = blk_peek_request(q);
1868 if (rq)
1869 blk_start_request(rq);
1870 return rq;
1871}
1872EXPORT_SYMBOL(blk_fetch_request);
1873
1874/**
Tejun Heo2e60e022009-04-23 11:05:18 +09001875 * blk_update_request - Special helper function for request stacking drivers
1876 * @rq: the request being processed
Randy Dunlap710027a2008-08-19 20:13:11 +02001877 * @error: %0 for success, < %0 for error
Tejun Heo2e60e022009-04-23 11:05:18 +09001878 * @nr_bytes: number of bytes to complete @rq
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001879 *
1880 * Description:
Tejun Heo2e60e022009-04-23 11:05:18 +09001881 * Ends I/O on a number of bytes attached to @rq, but doesn't complete
1882 * the request structure even if @rq doesn't have leftover.
1883 * If @rq has leftover, sets it up for the next range of segments.
1884 *
1885 * This special helper function is only for request stacking drivers
1886 * (e.g. request-based dm) so that they can handle partial completion.
1887 * Actual device drivers should use blk_end_request instead.
1888 *
1889 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
1890 * %false return from this function.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001891 *
1892 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09001893 * %false - this request doesn't have any more data
1894 * %true - this request has more data
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001895 **/
Tejun Heo2e60e022009-04-23 11:05:18 +09001896bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001898 int total_bytes, bio_nbytes, next_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 struct bio *bio;
1900
Tejun Heo2e60e022009-04-23 11:05:18 +09001901 if (!req->bio)
1902 return false;
1903
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001904 trace_block_rq_complete(req->q, req);
Jens Axboe2056a782006-03-23 20:00:26 +01001905
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 /*
Tejun Heo924cec72009-04-19 07:00:41 +09001907 * For fs requests, rq is just carrier of independent bio's
1908 * and each partial completion should be handled separately.
1909 * Reset per-request error on each partial completion.
1910 *
1911 * TODO: tj: This is too subtle. It would be better to let
1912 * low level drivers do what they see fit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 */
Tejun Heo924cec72009-04-19 07:00:41 +09001914 if (blk_fs_request(req))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 req->errors = 0;
1916
Jens Axboe6728cb02008-01-31 13:03:55 +01001917 if (error && (blk_fs_request(req) && !(req->cmd_flags & REQ_QUIET))) {
1918 printk(KERN_ERR "end_request: I/O error, dev %s, sector %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 req->rq_disk ? req->rq_disk->disk_name : "?",
Tejun Heo83096eb2009-05-07 22:24:39 +09001920 (unsigned long long)blk_rq_pos(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 }
1922
Jens Axboebc58ba92009-01-23 10:54:44 +01001923 blk_account_io_completion(req, nr_bytes);
Jens Axboed72d9042005-11-01 08:35:42 +01001924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 total_bytes = bio_nbytes = 0;
1926 while ((bio = req->bio) != NULL) {
1927 int nbytes;
1928
1929 if (nr_bytes >= bio->bi_size) {
1930 req->bio = bio->bi_next;
1931 nbytes = bio->bi_size;
NeilBrown5bb23a62007-09-27 12:46:13 +02001932 req_bio_endio(req, bio, nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 next_idx = 0;
1934 bio_nbytes = 0;
1935 } else {
1936 int idx = bio->bi_idx + next_idx;
1937
1938 if (unlikely(bio->bi_idx >= bio->bi_vcnt)) {
1939 blk_dump_rq_flags(req, "__end_that");
Jens Axboe6728cb02008-01-31 13:03:55 +01001940 printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
Harvey Harrison24c03d42008-05-01 04:35:17 -07001941 __func__, bio->bi_idx, bio->bi_vcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 break;
1943 }
1944
1945 nbytes = bio_iovec_idx(bio, idx)->bv_len;
1946 BIO_BUG_ON(nbytes > bio->bi_size);
1947
1948 /*
1949 * not a complete bvec done
1950 */
1951 if (unlikely(nbytes > nr_bytes)) {
1952 bio_nbytes += nr_bytes;
1953 total_bytes += nr_bytes;
1954 break;
1955 }
1956
1957 /*
1958 * advance to the next vector
1959 */
1960 next_idx++;
1961 bio_nbytes += nbytes;
1962 }
1963
1964 total_bytes += nbytes;
1965 nr_bytes -= nbytes;
1966
Jens Axboe6728cb02008-01-31 13:03:55 +01001967 bio = req->bio;
1968 if (bio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 /*
1970 * end more in this run, or just return 'not-done'
1971 */
1972 if (unlikely(nr_bytes <= 0))
1973 break;
1974 }
1975 }
1976
1977 /*
1978 * completely done
1979 */
Tejun Heo2e60e022009-04-23 11:05:18 +09001980 if (!req->bio) {
1981 /*
1982 * Reset counters so that the request stacking driver
1983 * can find how many bytes remain in the request
1984 * later.
1985 */
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001986 req->__data_len = 0;
Tejun Heo2e60e022009-04-23 11:05:18 +09001987 return false;
1988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
1990 /*
1991 * if the request wasn't completed, update state
1992 */
1993 if (bio_nbytes) {
NeilBrown5bb23a62007-09-27 12:46:13 +02001994 req_bio_endio(req, bio, bio_nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 bio->bi_idx += next_idx;
1996 bio_iovec(bio)->bv_offset += nr_bytes;
1997 bio_iovec(bio)->bv_len -= nr_bytes;
1998 }
1999
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002000 req->__data_len -= total_bytes;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002001 req->buffer = bio_data(req->bio);
2002
2003 /* update sector only for requests with clear definition of sector */
2004 if (blk_fs_request(req) || blk_discard_rq(req))
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002005 req->__sector += total_bytes >> 9;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002006
2007 /*
2008 * If total number of sectors is less than the first segment
2009 * size, something has gone terribly wrong.
2010 */
2011 if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
2012 printk(KERN_ERR "blk: request botched\n");
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002013 req->__data_len = blk_rq_cur_bytes(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002014 }
2015
2016 /* recalculate the number of segments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 blk_recalc_rq_segments(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002018
Tejun Heo2e60e022009-04-23 11:05:18 +09002019 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020}
Tejun Heo2e60e022009-04-23 11:05:18 +09002021EXPORT_SYMBOL_GPL(blk_update_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Tejun Heo2e60e022009-04-23 11:05:18 +09002023static bool blk_update_bidi_request(struct request *rq, int error,
2024 unsigned int nr_bytes,
2025 unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002026{
Tejun Heo2e60e022009-04-23 11:05:18 +09002027 if (blk_update_request(rq, error, nr_bytes))
2028 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002029
Tejun Heo2e60e022009-04-23 11:05:18 +09002030 /* Bidi request must be completed as a whole */
2031 if (unlikely(blk_bidi_rq(rq)) &&
2032 blk_update_request(rq->next_rq, error, bidi_bytes))
2033 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002034
Tejun Heo2e60e022009-04-23 11:05:18 +09002035 add_disk_randomness(rq->rq_disk);
2036
2037 return false;
Tejun Heo5efccd12009-04-23 11:05:18 +09002038}
2039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040/*
2041 * queue lock must be held
2042 */
Tejun Heo2e60e022009-04-23 11:05:18 +09002043static void blk_finish_request(struct request *req, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
Tejun Heo9934c8c2009-05-08 11:54:16 +09002045 BUG_ON(blk_queued_rq(req));
2046
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002047 if (blk_rq_tagged(req))
2048 blk_queue_end_tag(req->q, req);
2049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 if (unlikely(laptop_mode) && blk_fs_request(req))
2051 laptop_io_completion();
2052
Mike Andersone78042e2008-10-30 02:16:20 -07002053 blk_delete_timer(req);
2054
Jens Axboebc58ba92009-01-23 10:54:44 +01002055 blk_account_io_done(req);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002056
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 if (req->end_io)
Tejun Heo8ffdc652006-01-06 09:49:03 +01002058 req->end_io(req, error);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002059 else {
2060 if (blk_bidi_rq(req))
2061 __blk_put_request(req->next_rq->q, req->next_rq);
2062
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 __blk_put_request(req->q, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 }
2065}
2066
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05002067/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002068 * blk_end_bidi_request - Complete a bidi request
2069 * @rq: the request to complete
Randy Dunlap710027a2008-08-19 20:13:11 +02002070 * @error: %0 for success, < %0 for error
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002071 * @nr_bytes: number of bytes to complete @rq
2072 * @bidi_bytes: number of bytes to complete @rq->next_rq
2073 *
2074 * Description:
2075 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
Tejun Heo2e60e022009-04-23 11:05:18 +09002076 * Drivers that supports bidi can safely call this member for any
2077 * type of request, bidi or uni. In the later case @bidi_bytes is
2078 * just ignored.
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002079 *
2080 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002081 * %false - we are done with this request
2082 * %true - still buffers pending for this request
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002083 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002084static bool blk_end_bidi_request(struct request *rq, int error,
2085 unsigned int nr_bytes, unsigned int bidi_bytes)
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002086{
Tejun Heo2e60e022009-04-23 11:05:18 +09002087 struct request_queue *q = rq->q;
2088 unsigned long flags;
2089
2090 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2091 return true;
2092
2093 spin_lock_irqsave(q->queue_lock, flags);
2094 blk_finish_request(rq, error);
2095 spin_unlock_irqrestore(q->queue_lock, flags);
2096
2097 return false;
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002098}
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002099
2100/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002101 * __blk_end_bidi_request - Complete a bidi request with queue lock held
2102 * @rq: the request to complete
2103 * @error: %0 for success, < %0 for error
2104 * @nr_bytes: number of bytes to complete @rq
2105 * @bidi_bytes: number of bytes to complete @rq->next_rq
Tejun Heo5efccd12009-04-23 11:05:18 +09002106 *
2107 * Description:
Tejun Heo2e60e022009-04-23 11:05:18 +09002108 * Identical to blk_end_bidi_request() except that queue lock is
2109 * assumed to be locked on entry and remains so on return.
Tejun Heo5efccd12009-04-23 11:05:18 +09002110 *
Tejun Heo2e60e022009-04-23 11:05:18 +09002111 * Return:
2112 * %false - we are done with this request
2113 * %true - still buffers pending for this request
Tejun Heo5efccd12009-04-23 11:05:18 +09002114 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002115static bool __blk_end_bidi_request(struct request *rq, int error,
2116 unsigned int nr_bytes, unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002117{
Tejun Heo2e60e022009-04-23 11:05:18 +09002118 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2119 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002120
Tejun Heo2e60e022009-04-23 11:05:18 +09002121 blk_finish_request(rq, error);
Tejun Heo5efccd12009-04-23 11:05:18 +09002122
Tejun Heo2e60e022009-04-23 11:05:18 +09002123 return false;
Tejun Heo5efccd12009-04-23 11:05:18 +09002124}
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002125
2126/**
2127 * blk_end_request - Helper function for drivers to complete the request.
2128 * @rq: the request being processed
2129 * @error: %0 for success, < %0 for error
2130 * @nr_bytes: number of bytes to complete
2131 *
2132 * Description:
2133 * Ends I/O on a number of bytes attached to @rq.
2134 * If @rq has leftover, sets it up for the next range of segments.
2135 *
2136 * Return:
2137 * %false - we are done with this request
2138 * %true - still buffers pending for this request
2139 **/
2140bool blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
2141{
2142 return blk_end_bidi_request(rq, error, nr_bytes, 0);
2143}
2144EXPORT_SYMBOL_GPL(blk_end_request);
2145
2146/**
2147 * blk_end_request_all - Helper function for drives to finish the request.
2148 * @rq: the request to finish
2149 * @err: %0 for success, < %0 for error
2150 *
2151 * Description:
2152 * Completely finish @rq.
2153 */
2154void blk_end_request_all(struct request *rq, int error)
2155{
2156 bool pending;
2157 unsigned int bidi_bytes = 0;
2158
2159 if (unlikely(blk_bidi_rq(rq)))
2160 bidi_bytes = blk_rq_bytes(rq->next_rq);
2161
2162 pending = blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2163 BUG_ON(pending);
2164}
2165EXPORT_SYMBOL_GPL(blk_end_request_all);
2166
2167/**
2168 * blk_end_request_cur - Helper function to finish the current request chunk.
2169 * @rq: the request to finish the current chunk for
2170 * @err: %0 for success, < %0 for error
2171 *
2172 * Description:
2173 * Complete the current consecutively mapped chunk from @rq.
2174 *
2175 * Return:
2176 * %false - we are done with this request
2177 * %true - still buffers pending for this request
2178 */
2179bool blk_end_request_cur(struct request *rq, int error)
2180{
2181 return blk_end_request(rq, error, blk_rq_cur_bytes(rq));
2182}
2183EXPORT_SYMBOL_GPL(blk_end_request_cur);
2184
2185/**
2186 * __blk_end_request - Helper function for drivers to complete the request.
2187 * @rq: the request being processed
2188 * @error: %0 for success, < %0 for error
2189 * @nr_bytes: number of bytes to complete
2190 *
2191 * Description:
2192 * Must be called with queue lock held unlike blk_end_request().
2193 *
2194 * Return:
2195 * %false - we are done with this request
2196 * %true - still buffers pending for this request
2197 **/
2198bool __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
2199{
2200 return __blk_end_bidi_request(rq, error, nr_bytes, 0);
2201}
2202EXPORT_SYMBOL_GPL(__blk_end_request);
2203
2204/**
2205 * __blk_end_request_all - Helper function for drives to finish the request.
2206 * @rq: the request to finish
2207 * @err: %0 for success, < %0 for error
2208 *
2209 * Description:
2210 * Completely finish @rq. Must be called with queue lock held.
2211 */
2212void __blk_end_request_all(struct request *rq, int error)
2213{
2214 bool pending;
2215 unsigned int bidi_bytes = 0;
2216
2217 if (unlikely(blk_bidi_rq(rq)))
2218 bidi_bytes = blk_rq_bytes(rq->next_rq);
2219
2220 pending = __blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2221 BUG_ON(pending);
2222}
2223EXPORT_SYMBOL_GPL(__blk_end_request_all);
2224
2225/**
2226 * __blk_end_request_cur - Helper function to finish the current request chunk.
2227 * @rq: the request to finish the current chunk for
2228 * @err: %0 for success, < %0 for error
2229 *
2230 * Description:
2231 * Complete the current consecutively mapped chunk from @rq. Must
2232 * be called with queue lock held.
2233 *
2234 * Return:
2235 * %false - we are done with this request
2236 * %true - still buffers pending for this request
2237 */
2238bool __blk_end_request_cur(struct request *rq, int error)
2239{
2240 return __blk_end_request(rq, error, blk_rq_cur_bytes(rq));
2241}
2242EXPORT_SYMBOL_GPL(__blk_end_request_cur);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002243
Jens Axboe86db1e22008-01-29 14:53:40 +01002244void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2245 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246{
David Woodhoused628eae2008-08-09 16:22:17 +01002247 /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw, and
2248 we want BIO_RW_AHEAD (bit 1) to imply REQ_FAILFAST (bit 1). */
Jens Axboe4aff5e22006-08-10 08:44:47 +02002249 rq->cmd_flags |= (bio->bi_rw & 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
David Woodhousefb2dce82008-08-05 18:01:53 +01002251 if (bio_has_data(bio)) {
2252 rq->nr_phys_segments = bio_phys_segments(q, bio);
David Woodhousefb2dce82008-08-05 18:01:53 +01002253 rq->buffer = bio_data(bio);
2254 }
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002255 rq->__data_len = bio->bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 rq->bio = rq->biotail = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
NeilBrown66846572007-08-16 13:31:28 +02002258 if (bio->bi_bdev)
2259 rq->rq_disk = bio->bi_bdev->bd_disk;
2260}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
Kiyoshi Uedaef9e3fa2008-10-01 16:12:15 +02002262/**
2263 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
2264 * @q : the queue of the device being checked
2265 *
2266 * Description:
2267 * Check if underlying low-level drivers of a device are busy.
2268 * If the drivers want to export their busy state, they must set own
2269 * exporting function using blk_queue_lld_busy() first.
2270 *
2271 * Basically, this function is used only by request stacking drivers
2272 * to stop dispatching requests to underlying devices when underlying
2273 * devices are busy. This behavior helps more I/O merging on the queue
2274 * of the request stacking driver and prevents I/O throughput regression
2275 * on burst I/O load.
2276 *
2277 * Return:
2278 * 0 - Not busy (The request stacking driver should dispatch request)
2279 * 1 - Busy (The request stacking driver should stop dispatching request)
2280 */
2281int blk_lld_busy(struct request_queue *q)
2282{
2283 if (q->lld_busy_fn)
2284 return q->lld_busy_fn(q);
2285
2286 return 0;
2287}
2288EXPORT_SYMBOL_GPL(blk_lld_busy);
2289
Jens Axboe18887ad2008-07-28 13:08:45 +02002290int kblockd_schedule_work(struct request_queue *q, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291{
2292 return queue_work(kblockd_workqueue, work);
2293}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294EXPORT_SYMBOL(kblockd_schedule_work);
2295
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296int __init blk_dev_init(void)
2297{
Nikanth Karthikesan9eb55b02009-04-27 14:53:54 +02002298 BUILD_BUG_ON(__REQ_NR_BITS > 8 *
2299 sizeof(((struct request *)0)->cmd_flags));
2300
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 kblockd_workqueue = create_workqueue("kblockd");
2302 if (!kblockd_workqueue)
2303 panic("Failed to create kblockd\n");
2304
2305 request_cachep = kmem_cache_create("blkdev_requests",
Paul Mundt20c2df82007-07-20 10:11:58 +09002306 sizeof(struct request), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
Jens Axboe8324aa92008-01-29 14:51:59 +01002308 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02002309 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 return 0;
2312}
2313