blob: 3b02ba351f8cef5b93f12a06edcd174595e648a2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 1994, Karl Keyte: Added support for disk statistics
4 * Elevator latency, (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
5 * Queue request tables / lock, selectable elevator, Jens Axboe <axboe@suse.de>
Jens Axboe6728cb02008-01-31 13:03:55 +01006 * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au>
7 * - July2000
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
9 */
10
11/*
12 * This handles all read/write requests to block devices
13 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/backing-dev.h>
17#include <linux/bio.h>
18#include <linux/blkdev.h>
19#include <linux/highmem.h>
20#include <linux/mm.h>
21#include <linux/kernel_stat.h>
22#include <linux/string.h>
23#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/completion.h>
25#include <linux/slab.h>
26#include <linux/swap.h>
27#include <linux/writeback.h>
Andrew Mortonfaccbd4b2006-12-10 02:19:35 -080028#include <linux/task_io_accounting_ops.h>
Akinobu Mitac17bb492006-12-08 02:39:46 -080029#include <linux/fault-inject.h>
Jens Axboe73c10102011-03-08 13:19:51 +010030#include <linux/list_sort.h>
Tejun Heoe3c78ca2011-10-19 14:32:38 +020031#include <linux/delay.h>
Li Zefan55782132009-06-09 13:43:05 +080032
33#define CREATE_TRACE_POINTS
34#include <trace/events/block.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Jens Axboe8324aa92008-01-29 14:51:59 +010036#include "blk.h"
Tejun Heo5efd6112012-03-05 13:15:12 -080037#include "blk-cgroup.h"
Jens Axboe8324aa92008-01-29 14:51:59 +010038
Mike Snitzerd07335e2010-11-16 12:52:38 +010039EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_remap);
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +020040EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_remap);
Li Zefan55782132009-06-09 13:43:05 +080041EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_complete);
Ingo Molnar0bfc2452008-11-26 11:59:56 +010042
Tejun Heoa73f7302011-12-14 00:33:37 +010043DEFINE_IDA(blk_queue_ida);
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/*
46 * For the allocated request tables
47 */
Adrian Bunk5ece6c52008-02-18 13:45:51 +010048static struct kmem_cache *request_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/*
51 * For queue allocation
52 */
Jens Axboe6728cb02008-01-31 13:03:55 +010053struct kmem_cache *blk_requestq_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 * Controlling structure to kblockd
57 */
Jens Axboeff856ba2006-01-09 16:02:34 +010058static struct workqueue_struct *kblockd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Jens Axboe26b82562008-01-29 13:54:41 +010060static void drive_stat_acct(struct request *rq, int new_io)
61{
Jens Axboe28f13702008-05-07 10:15:46 +020062 struct hd_struct *part;
Jens Axboe26b82562008-01-29 13:54:41 +010063 int rw = rq_data_dir(rq);
Tejun Heoc9959052008-08-25 19:47:21 +090064 int cpu;
Jens Axboe26b82562008-01-29 13:54:41 +010065
Jens Axboec2553b52009-04-24 08:10:11 +020066 if (!blk_do_io_stat(rq))
Jens Axboe26b82562008-01-29 13:54:41 +010067 return;
68
Tejun Heo074a7ac2008-08-25 19:56:14 +090069 cpu = part_stat_lock();
Tejun Heoc9959052008-08-25 19:47:21 +090070
Jerome Marchand09e099d2011-01-05 16:57:38 +010071 if (!new_io) {
72 part = rq->part;
Tejun Heo074a7ac2008-08-25 19:56:14 +090073 part_stat_inc(cpu, part, merges[rw]);
Jerome Marchand09e099d2011-01-05 16:57:38 +010074 } else {
75 part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq));
Jens Axboe6c23a962011-01-07 08:43:37 +010076 if (!hd_struct_try_get(part)) {
Jerome Marchand09e099d2011-01-05 16:57:38 +010077 /*
78 * The partition is already being removed,
79 * the request will be accounted on the disk only
80 *
81 * We take a reference on disk->part0 although that
82 * partition will never be deleted, so we can treat
83 * it as any other partition.
84 */
85 part = &rq->rq_disk->part0;
Jens Axboe6c23a962011-01-07 08:43:37 +010086 hd_struct_get(part);
Jerome Marchand09e099d2011-01-05 16:57:38 +010087 }
Tejun Heo074a7ac2008-08-25 19:56:14 +090088 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +020089 part_inc_in_flight(part, rw);
Jerome Marchand09e099d2011-01-05 16:57:38 +010090 rq->part = part;
Jens Axboe26b82562008-01-29 13:54:41 +010091 }
Tejun Heoe71bf0d2008-09-03 09:03:02 +020092
Tejun Heo074a7ac2008-08-25 19:56:14 +090093 part_stat_unlock();
Jens Axboe26b82562008-01-29 13:54:41 +010094}
95
Jens Axboe8324aa92008-01-29 14:51:59 +010096void blk_queue_congestion_threshold(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
98 int nr;
99
100 nr = q->nr_requests - (q->nr_requests / 8) + 1;
101 if (nr > q->nr_requests)
102 nr = q->nr_requests;
103 q->nr_congestion_on = nr;
104
105 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
106 if (nr < 1)
107 nr = 1;
108 q->nr_congestion_off = nr;
109}
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111/**
112 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
113 * @bdev: device
114 *
115 * Locates the passed device's request queue and returns the address of its
116 * backing_dev_info
117 *
118 * Will return NULL if the request queue cannot be located.
119 */
120struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
121{
122 struct backing_dev_info *ret = NULL;
Jens Axboe165125e2007-07-24 09:28:11 +0200123 struct request_queue *q = bdev_get_queue(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 if (q)
126 ret = &q->backing_dev_info;
127 return ret;
128}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129EXPORT_SYMBOL(blk_get_backing_dev_info);
130
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200131void blk_rq_init(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200133 memset(rq, 0, sizeof(*rq));
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 INIT_LIST_HEAD(&rq->queuelist);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700136 INIT_LIST_HEAD(&rq->timeout_list);
Jens Axboec7c22e42008-09-13 20:26:01 +0200137 rq->cpu = -1;
Jens Axboe63a71382008-02-08 12:41:03 +0100138 rq->q = q;
Tejun Heoa2dec7b2009-05-07 22:24:44 +0900139 rq->__sector = (sector_t) -1;
Jens Axboe2e662b62006-07-13 11:55:04 +0200140 INIT_HLIST_NODE(&rq->hash);
141 RB_CLEAR_NODE(&rq->rb_node);
FUJITA Tomonorid7e3c322008-04-29 09:54:39 +0200142 rq->cmd = rq->__cmd;
Li Zefane2494e12009-04-02 13:43:26 +0800143 rq->cmd_len = BLK_MAX_CDB;
Jens Axboe63a71382008-02-08 12:41:03 +0100144 rq->tag = -1;
Jens Axboe63a71382008-02-08 12:41:03 +0100145 rq->ref_count = 1;
Tejun Heob243ddc2009-04-23 11:05:18 +0900146 rq->start_time = jiffies;
Divyesh Shah91952912010-04-01 15:01:41 -0700147 set_start_time_ns(rq);
Jerome Marchand09e099d2011-01-05 16:57:38 +0100148 rq->part = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200150EXPORT_SYMBOL(blk_rq_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
NeilBrown5bb23a62007-09-27 12:46:13 +0200152static void req_bio_endio(struct request *rq, struct bio *bio,
153 unsigned int nbytes, int error)
Tejun Heo797e7db2006-01-06 09:51:03 +0100154{
Tejun Heo143a87f2011-01-25 12:43:52 +0100155 if (error)
156 clear_bit(BIO_UPTODATE, &bio->bi_flags);
157 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
158 error = -EIO;
Tejun Heo797e7db2006-01-06 09:51:03 +0100159
Tejun Heo143a87f2011-01-25 12:43:52 +0100160 if (unlikely(nbytes > bio->bi_size)) {
161 printk(KERN_ERR "%s: want %u bytes done, %u left\n",
162 __func__, nbytes, bio->bi_size);
163 nbytes = bio->bi_size;
NeilBrown5bb23a62007-09-27 12:46:13 +0200164 }
Tejun Heo143a87f2011-01-25 12:43:52 +0100165
166 if (unlikely(rq->cmd_flags & REQ_QUIET))
167 set_bit(BIO_QUIET, &bio->bi_flags);
168
169 bio->bi_size -= nbytes;
170 bio->bi_sector += (nbytes >> 9);
171
172 if (bio_integrity(bio))
173 bio_integrity_advance(bio, nbytes);
174
175 /* don't actually finish bio if it's part of flush sequence */
176 if (bio->bi_size == 0 && !(rq->cmd_flags & REQ_FLUSH_SEQ))
177 bio_endio(bio, error);
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
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200194 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
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
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500203static void blk_delay_work(struct work_struct *work)
Jens Axboe6c5e0c42008-08-01 20:31:32 +0200204{
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500205 struct request_queue *q;
Jens Axboe6c5e0c42008-08-01 20:31:32 +0200206
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500207 q = container_of(work, struct request_queue, delay_work.work);
208 spin_lock_irq(q->queue_lock);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200209 __blk_run_queue(q);
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500210 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213/**
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500214 * blk_delay_queue - restart queueing after defined interval
215 * @q: The &struct request_queue in question
216 * @msecs: Delay in msecs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 *
218 * Description:
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500219 * Sometimes queueing needs to be postponed for a little while, to allow
220 * resources to come back. This function will make sure that queueing is
221 * restarted around the specified time.
222 */
223void blk_delay_queue(struct request_queue *q, unsigned long msecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Jens Axboe4521cc42011-04-18 11:36:39 +0200225 queue_delayed_work(kblockd_workqueue, &q->delay_work,
226 msecs_to_jiffies(msecs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500228EXPORT_SYMBOL(blk_delay_queue);
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230/**
231 * blk_start_queue - restart a previously stopped queue
Jens Axboe165125e2007-07-24 09:28:11 +0200232 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 *
234 * Description:
235 * blk_start_queue() will clear the stop flag on the queue, and call
236 * the request_fn for the queue if it was in a stopped state when
237 * entered. Also see blk_stop_queue(). Queue lock must be held.
238 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200239void blk_start_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200241 WARN_ON(!irqs_disabled());
242
Nick Piggin75ad23b2008-04-29 14:48:33 +0200243 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200244 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246EXPORT_SYMBOL(blk_start_queue);
247
248/**
249 * blk_stop_queue - stop a queue
Jens Axboe165125e2007-07-24 09:28:11 +0200250 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 *
252 * Description:
253 * The Linux block layer assumes that a block driver will consume all
254 * entries on the request queue when the request_fn strategy is called.
255 * Often this will not happen, because of hardware limitations (queue
256 * depth settings). If a device driver gets a 'queue full' response,
257 * or if it simply chooses not to queue more I/O at one point, it can
258 * call this function to prevent the request_fn from being called until
259 * the driver has signalled it's ready to go again. This happens by calling
260 * blk_start_queue() to restart queue operations. Queue lock must be held.
261 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200262void blk_stop_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Jens Axboead3d9d72011-03-25 16:58:59 +0100264 __cancel_delayed_work(&q->delay_work);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200265 queue_flag_set(QUEUE_FLAG_STOPPED, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267EXPORT_SYMBOL(blk_stop_queue);
268
269/**
270 * blk_sync_queue - cancel any pending callbacks on a queue
271 * @q: the queue
272 *
273 * Description:
274 * The block layer may perform asynchronous callback activity
275 * on a queue, such as calling the unplug function after a timeout.
276 * A block device may call blk_sync_queue to ensure that any
277 * such activity is cancelled, thus allowing it to release resources
Michael Opdenacker59c51592007-05-09 08:57:56 +0200278 * that the callbacks might use. The caller must already have made sure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 * that its ->make_request_fn will not re-add plugging prior to calling
280 * this function.
281 *
Vivek Goyalda527772011-03-02 19:05:33 -0500282 * This function does not cancel any asynchronous activity arising
283 * out of elevator or throttling code. That would require elevaotor_exit()
Tejun Heo5efd6112012-03-05 13:15:12 -0800284 * and blkcg_exit_queue() to be called with queue lock initialized.
Vivek Goyalda527772011-03-02 19:05:33 -0500285 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 */
287void blk_sync_queue(struct request_queue *q)
288{
Jens Axboe70ed28b2008-11-19 14:38:39 +0100289 del_timer_sync(&q->timeout);
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500290 cancel_delayed_work_sync(&q->delay_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292EXPORT_SYMBOL(blk_sync_queue);
293
294/**
Jens Axboe80a4b582008-10-14 09:51:06 +0200295 * __blk_run_queue - run a single device queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200297 *
298 * Description:
299 * See @blk_run_queue. This variant must be called with the queue lock
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200300 * held and interrupts disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 */
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200302void __blk_run_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Tejun Heoa538cd02009-04-23 11:05:17 +0900304 if (unlikely(blk_queue_stopped(q)))
305 return;
306
Jens Axboec21e6be2011-04-19 13:32:46 +0200307 q->request_fn(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200308}
309EXPORT_SYMBOL(__blk_run_queue);
Jens Axboedac07ec2006-05-11 08:20:16 +0200310
Nick Piggin75ad23b2008-04-29 14:48:33 +0200311/**
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200312 * blk_run_queue_async - run a single device queue in workqueue context
313 * @q: The queue to run
314 *
315 * Description:
316 * Tells kblockd to perform the equivalent of @blk_run_queue on behalf
317 * of us.
318 */
319void blk_run_queue_async(struct request_queue *q)
320{
Shaohua Li3ec717b2011-05-18 11:22:43 +0200321 if (likely(!blk_queue_stopped(q))) {
322 __cancel_delayed_work(&q->delay_work);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200323 queue_delayed_work(kblockd_workqueue, &q->delay_work, 0);
Shaohua Li3ec717b2011-05-18 11:22:43 +0200324 }
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200325}
Jens Axboec21e6be2011-04-19 13:32:46 +0200326EXPORT_SYMBOL(blk_run_queue_async);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200327
328/**
Nick Piggin75ad23b2008-04-29 14:48:33 +0200329 * blk_run_queue - run a single device queue
330 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200331 *
332 * Description:
333 * Invoke request handling on this queue, if it has pending work to do.
Tejun Heoa7f55792009-04-23 11:05:17 +0900334 * May be used to restart queueing when a request has completed.
Nick Piggin75ad23b2008-04-29 14:48:33 +0200335 */
336void blk_run_queue(struct request_queue *q)
337{
338 unsigned long flags;
339
340 spin_lock_irqsave(q->queue_lock, flags);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200341 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 spin_unlock_irqrestore(q->queue_lock, flags);
343}
344EXPORT_SYMBOL(blk_run_queue);
345
Jens Axboe165125e2007-07-24 09:28:11 +0200346void blk_put_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500347{
348 kobject_put(&q->kobj);
349}
Jens Axboed86e0e82011-05-27 07:44:43 +0200350EXPORT_SYMBOL(blk_put_queue);
Al Viro483f4af2006-03-18 18:34:37 -0500351
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200352/**
353 * blk_drain_queue - drain requests from request_queue
354 * @q: queue to drain
Tejun Heoc9a929d2011-10-19 14:42:16 +0200355 * @drain_all: whether to drain all requests or only the ones w/ ELVPRIV
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200356 *
Tejun Heoc9a929d2011-10-19 14:42:16 +0200357 * Drain requests from @q. If @drain_all is set, all requests are drained.
358 * If not, only ELVPRIV requests are drained. The caller is responsible
359 * for ensuring that no new requests which need to be drained are queued.
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200360 */
Tejun Heoc9a929d2011-10-19 14:42:16 +0200361void blk_drain_queue(struct request_queue *q, bool drain_all)
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200362{
363 while (true) {
Tejun Heo481a7d62011-12-14 00:33:37 +0100364 bool drain = false;
365 int i;
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200366
367 spin_lock_irq(q->queue_lock);
368
Tejun Heob855b042012-03-06 21:24:55 +0100369 /*
370 * The caller might be trying to drain @q before its
371 * elevator is initialized.
372 */
373 if (q->elevator)
374 elv_drain_elevator(q);
375
Tejun Heo5efd6112012-03-05 13:15:12 -0800376 blkcg_drain_queue(q);
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200377
Tejun Heo4eabc942011-12-15 20:03:04 +0100378 /*
379 * This function might be called on a queue which failed
Tejun Heob855b042012-03-06 21:24:55 +0100380 * driver init after queue creation or is not yet fully
381 * active yet. Some drivers (e.g. fd and loop) get unhappy
382 * in such cases. Kick queue iff dispatch queue has
383 * something on it and @q has request_fn set.
Tejun Heo4eabc942011-12-15 20:03:04 +0100384 */
Tejun Heob855b042012-03-06 21:24:55 +0100385 if (!list_empty(&q->queue_head) && q->request_fn)
Tejun Heo4eabc942011-12-15 20:03:04 +0100386 __blk_run_queue(q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200387
Tejun Heo481a7d62011-12-14 00:33:37 +0100388 drain |= q->rq.elvpriv;
389
390 /*
391 * Unfortunately, requests are queued at and tracked from
392 * multiple places and there's no single counter which can
393 * be drained. Check all the queues and counters.
394 */
395 if (drain_all) {
396 drain |= !list_empty(&q->queue_head);
397 for (i = 0; i < 2; i++) {
398 drain |= q->rq.count[i];
399 drain |= q->in_flight[i];
400 drain |= !list_empty(&q->flush_queue[i]);
401 }
402 }
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200403
404 spin_unlock_irq(q->queue_lock);
405
Tejun Heo481a7d62011-12-14 00:33:37 +0100406 if (!drain)
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200407 break;
408 msleep(10);
409 }
410}
411
Tejun Heoc9a929d2011-10-19 14:42:16 +0200412/**
Tejun Heod7325802012-03-05 13:14:58 -0800413 * blk_queue_bypass_start - enter queue bypass mode
414 * @q: queue of interest
415 *
416 * In bypass mode, only the dispatch FIFO queue of @q is used. This
417 * function makes @q enter bypass mode and drains all requests which were
Tejun Heo6ecf23a2012-03-05 13:14:59 -0800418 * throttled or issued before. On return, it's guaranteed that no request
Tejun Heo80fd9972012-04-13 14:50:53 -0700419 * is being throttled or has ELVPRIV set and blk_queue_bypass() %true
420 * inside queue or RCU read lock.
Tejun Heod7325802012-03-05 13:14:58 -0800421 */
422void blk_queue_bypass_start(struct request_queue *q)
423{
Tejun Heob82d4b12012-04-13 13:11:31 -0700424 bool drain;
425
Tejun Heod7325802012-03-05 13:14:58 -0800426 spin_lock_irq(q->queue_lock);
Tejun Heob82d4b12012-04-13 13:11:31 -0700427 drain = !q->bypass_depth++;
Tejun Heod7325802012-03-05 13:14:58 -0800428 queue_flag_set(QUEUE_FLAG_BYPASS, q);
429 spin_unlock_irq(q->queue_lock);
430
Tejun Heob82d4b12012-04-13 13:11:31 -0700431 if (drain) {
432 blk_drain_queue(q, false);
433 /* ensure blk_queue_bypass() is %true inside RCU read lock */
434 synchronize_rcu();
435 }
Tejun Heod7325802012-03-05 13:14:58 -0800436}
437EXPORT_SYMBOL_GPL(blk_queue_bypass_start);
438
439/**
440 * blk_queue_bypass_end - leave queue bypass mode
441 * @q: queue of interest
442 *
443 * Leave bypass mode and restore the normal queueing behavior.
444 */
445void blk_queue_bypass_end(struct request_queue *q)
446{
447 spin_lock_irq(q->queue_lock);
448 if (!--q->bypass_depth)
449 queue_flag_clear(QUEUE_FLAG_BYPASS, q);
450 WARN_ON_ONCE(q->bypass_depth < 0);
451 spin_unlock_irq(q->queue_lock);
452}
453EXPORT_SYMBOL_GPL(blk_queue_bypass_end);
454
455/**
Tejun Heoc9a929d2011-10-19 14:42:16 +0200456 * blk_cleanup_queue - shutdown a request queue
457 * @q: request queue to shutdown
458 *
459 * Mark @q DEAD, drain all pending requests, destroy and put it. All
460 * future requests will be failed immediately with -ENODEV.
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500461 */
Jens Axboe6728cb02008-01-31 13:03:55 +0100462void blk_cleanup_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500463{
Tejun Heoc9a929d2011-10-19 14:42:16 +0200464 spinlock_t *lock = q->queue_lock;
Jens Axboee3335de92008-09-18 09:22:54 -0700465
Tejun Heoc9a929d2011-10-19 14:42:16 +0200466 /* mark @q DEAD, no new request or merges will be allowed afterwards */
Al Viro483f4af2006-03-18 18:34:37 -0500467 mutex_lock(&q->sysfs_lock);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200468 queue_flag_set_unlocked(QUEUE_FLAG_DEAD, q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200469
470 spin_lock_irq(lock);
Tejun Heo6ecf23a2012-03-05 13:14:59 -0800471
Tejun Heo80fd9972012-04-13 14:50:53 -0700472 /*
473 * Dead queue is permanently in bypass mode till released. Note
474 * that, unlike blk_queue_bypass_start(), we aren't performing
475 * synchronize_rcu() after entering bypass mode to avoid the delay
476 * as some drivers create and destroy a lot of queues while
477 * probing. This is still safe because blk_release_queue() will be
478 * called only after the queue refcnt drops to zero and nothing,
479 * RCU or not, would be traversing the queue by then.
480 */
Tejun Heo6ecf23a2012-03-05 13:14:59 -0800481 q->bypass_depth++;
482 queue_flag_set(QUEUE_FLAG_BYPASS, q);
483
Tejun Heoc9a929d2011-10-19 14:42:16 +0200484 queue_flag_set(QUEUE_FLAG_NOMERGES, q);
485 queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
486 queue_flag_set(QUEUE_FLAG_DEAD, q);
Al Viro483f4af2006-03-18 18:34:37 -0500487
Hannes Reinecke777eb1b2011-09-28 08:07:01 -0600488 if (q->queue_lock != &q->__queue_lock)
489 q->queue_lock = &q->__queue_lock;
Vivek Goyalda527772011-03-02 19:05:33 -0500490
Tejun Heoc9a929d2011-10-19 14:42:16 +0200491 spin_unlock_irq(lock);
492 mutex_unlock(&q->sysfs_lock);
493
Tejun Heob855b042012-03-06 21:24:55 +0100494 /* drain all requests queued before DEAD marking */
495 blk_drain_queue(q, true);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200496
497 /* @q won't process any more request, flush async actions */
498 del_timer_sync(&q->backing_dev_info.laptop_mode_wb_timer);
499 blk_sync_queue(q);
500
501 /* @q is and will stay empty, shutdown and put */
Al Viro483f4af2006-03-18 18:34:37 -0500502 blk_put_queue(q);
503}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504EXPORT_SYMBOL(blk_cleanup_queue);
505
Jens Axboe165125e2007-07-24 09:28:11 +0200506static int blk_init_free_list(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
508 struct request_list *rl = &q->rq;
509
Mike Snitzer1abec4f2010-05-25 13:15:15 -0400510 if (unlikely(rl->rq_pool))
511 return 0;
512
Jens Axboe1faa16d2009-04-06 14:48:01 +0200513 rl->count[BLK_RW_SYNC] = rl->count[BLK_RW_ASYNC] = 0;
514 rl->starved[BLK_RW_SYNC] = rl->starved[BLK_RW_ASYNC] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200515 rl->elvpriv = 0;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200516 init_waitqueue_head(&rl->wait[BLK_RW_SYNC]);
517 init_waitqueue_head(&rl->wait[BLK_RW_ASYNC]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Christoph Lameter19460892005-06-23 00:08:19 -0700519 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
520 mempool_free_slab, request_cachep, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 if (!rl->rq_pool)
523 return -ENOMEM;
524
525 return 0;
526}
527
Jens Axboe165125e2007-07-24 09:28:11 +0200528struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Christoph Lameter19460892005-06-23 00:08:19 -0700530 return blk_alloc_queue_node(gfp_mask, -1);
531}
532EXPORT_SYMBOL(blk_alloc_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Jens Axboe165125e2007-07-24 09:28:11 +0200534struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -0700535{
Jens Axboe165125e2007-07-24 09:28:11 +0200536 struct request_queue *q;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700537 int err;
Christoph Lameter19460892005-06-23 00:08:19 -0700538
Jens Axboe8324aa92008-01-29 14:51:59 +0100539 q = kmem_cache_alloc_node(blk_requestq_cachep,
Christoph Lameter94f60302007-07-17 04:03:29 -0700540 gfp_mask | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (!q)
542 return NULL;
543
Tejun Heoa73f7302011-12-14 00:33:37 +0100544 q->id = ida_simple_get(&blk_queue_ida, 0, 0, GFP_KERNEL);
545 if (q->id < 0)
546 goto fail_q;
547
Jens Axboe0989a022009-06-12 14:42:56 +0200548 q->backing_dev_info.ra_pages =
549 (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
550 q->backing_dev_info.state = 0;
551 q->backing_dev_info.capabilities = BDI_CAP_MAP_COPY;
Jens Axboed9938312009-06-12 14:45:52 +0200552 q->backing_dev_info.name = "block";
Mike Snitzer51514122011-11-23 10:59:13 +0100553 q->node = node_id;
Jens Axboe0989a022009-06-12 14:42:56 +0200554
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700555 err = bdi_init(&q->backing_dev_info);
Tejun Heoa73f7302011-12-14 00:33:37 +0100556 if (err)
557 goto fail_id;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700558
Matthew Garrett31373d02010-04-06 14:25:14 +0200559 setup_timer(&q->backing_dev_info.laptop_mode_wb_timer,
560 laptop_mode_timer_fn, (unsigned long) q);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700561 setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
Tejun Heob855b042012-03-06 21:24:55 +0100562 INIT_LIST_HEAD(&q->queue_head);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700563 INIT_LIST_HEAD(&q->timeout_list);
Tejun Heoa612fdd2011-12-14 00:33:41 +0100564 INIT_LIST_HEAD(&q->icq_list);
Tejun Heo4eef3042012-03-05 13:15:18 -0800565#ifdef CONFIG_BLK_CGROUP
Tejun Heoe8989fa2012-03-05 13:15:20 -0800566 INIT_LIST_HEAD(&q->blkg_list);
Tejun Heo4eef3042012-03-05 13:15:18 -0800567#endif
Tejun Heoae1b1532011-01-25 12:43:54 +0100568 INIT_LIST_HEAD(&q->flush_queue[0]);
569 INIT_LIST_HEAD(&q->flush_queue[1]);
570 INIT_LIST_HEAD(&q->flush_data_in_flight);
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500571 INIT_DELAYED_WORK(&q->delay_work, blk_delay_work);
Al Viro483f4af2006-03-18 18:34:37 -0500572
Jens Axboe8324aa92008-01-29 14:51:59 +0100573 kobject_init(&q->kobj, &blk_queue_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Al Viro483f4af2006-03-18 18:34:37 -0500575 mutex_init(&q->sysfs_lock);
Neil Browne7e72bf2008-05-14 16:05:54 -0700576 spin_lock_init(&q->__queue_lock);
Al Viro483f4af2006-03-18 18:34:37 -0500577
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500578 /*
579 * By default initialize queue_lock to internal lock and driver can
580 * override it later if need be.
581 */
582 q->queue_lock = &q->__queue_lock;
583
Tejun Heob82d4b12012-04-13 13:11:31 -0700584 /*
585 * A queue starts its life with bypass turned on to avoid
586 * unnecessary bypass on/off overhead and nasty surprises during
587 * init. The initial bypass will be finished at the end of
588 * blk_init_allocated_queue().
589 */
590 q->bypass_depth = 1;
591 __set_bit(QUEUE_FLAG_BYPASS, &q->queue_flags);
592
Tejun Heo5efd6112012-03-05 13:15:12 -0800593 if (blkcg_init_queue(q))
Tejun Heof51b8022012-03-05 13:15:05 -0800594 goto fail_id;
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 return q;
Tejun Heoa73f7302011-12-14 00:33:37 +0100597
598fail_id:
599 ida_simple_remove(&blk_queue_ida, q->id);
600fail_q:
601 kmem_cache_free(blk_requestq_cachep, q);
602 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
Christoph Lameter19460892005-06-23 00:08:19 -0700604EXPORT_SYMBOL(blk_alloc_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606/**
607 * blk_init_queue - prepare a request queue for use with a block device
608 * @rfn: The function to be called to process requests that have been
609 * placed on the queue.
610 * @lock: Request queue spin lock
611 *
612 * Description:
613 * If a block device wishes to use the standard request handling procedures,
614 * which sorts requests and coalesces adjacent requests, then it must
615 * call blk_init_queue(). The function @rfn will be called when there
616 * are requests on the queue that need to be processed. If the device
617 * supports plugging, then @rfn may not be called immediately when requests
618 * are available on the queue, but may be called at some time later instead.
619 * Plugged queues are generally unplugged when a buffer belonging to one
620 * of the requests on the queue is needed, or due to memory pressure.
621 *
622 * @rfn is not required, or even expected, to remove all requests off the
623 * queue, but only as many as it can handle at a time. If it does leave
624 * requests on the queue, it is responsible for arranging that the requests
625 * get dealt with eventually.
626 *
627 * The queue spin lock must be held while manipulating the requests on the
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200628 * request queue; this lock will be taken also from interrupt context, so irq
629 * disabling is needed for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 *
Randy Dunlap710027a2008-08-19 20:13:11 +0200631 * Function returns a pointer to the initialized request queue, or %NULL if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 * it didn't succeed.
633 *
634 * Note:
635 * blk_init_queue() must be paired with a blk_cleanup_queue() call
636 * when the block device is deactivated (such as at module unload).
637 **/
Christoph Lameter19460892005-06-23 00:08:19 -0700638
Jens Axboe165125e2007-07-24 09:28:11 +0200639struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Christoph Lameter19460892005-06-23 00:08:19 -0700641 return blk_init_queue_node(rfn, lock, -1);
642}
643EXPORT_SYMBOL(blk_init_queue);
644
Jens Axboe165125e2007-07-24 09:28:11 +0200645struct request_queue *
Christoph Lameter19460892005-06-23 00:08:19 -0700646blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
647{
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600648 struct request_queue *uninit_q, *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600650 uninit_q = blk_alloc_queue_node(GFP_KERNEL, node_id);
651 if (!uninit_q)
652 return NULL;
653
Mike Snitzer51514122011-11-23 10:59:13 +0100654 q = blk_init_allocated_queue(uninit_q, rfn, lock);
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600655 if (!q)
656 blk_cleanup_queue(uninit_q);
657
658 return q;
Mike Snitzer01effb02010-05-11 08:57:42 +0200659}
660EXPORT_SYMBOL(blk_init_queue_node);
661
662struct request_queue *
663blk_init_allocated_queue(struct request_queue *q, request_fn_proc *rfn,
664 spinlock_t *lock)
665{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (!q)
667 return NULL;
668
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600669 if (blk_init_free_list(q))
Al Viro8669aaf2006-03-18 13:50:00 -0500670 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 q->request_fn = rfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 q->prep_rq_fn = NULL;
James Bottomley28018c22010-07-01 19:49:17 +0900674 q->unprep_rq_fn = NULL;
Jens Axboebc58ba92009-01-23 10:54:44 +0100675 q->queue_flags = QUEUE_FLAG_DEFAULT;
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500676
677 /* Override internal queue lock with supplied lock pointer */
678 if (lock)
679 q->queue_lock = lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Jens Axboef3b144a2009-03-06 08:48:33 +0100681 /*
682 * This also sets hw/phys segments, boundary and size
683 */
Jens Axboec20e8de2011-09-12 12:03:37 +0200684 blk_queue_make_request(q, blk_queue_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Alan Stern44ec9542007-02-20 11:01:57 -0500686 q->sg_reserved_size = INT_MAX;
687
Tejun Heob82d4b12012-04-13 13:11:31 -0700688 /* init elevator */
689 if (elevator_init(q, NULL))
690 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Tejun Heob82d4b12012-04-13 13:11:31 -0700692 blk_queue_congestion_threshold(q);
693
694 /* all done, end the initial bypass */
695 blk_queue_bypass_end(q);
696 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
Mike Snitzer51514122011-11-23 10:59:13 +0100698EXPORT_SYMBOL(blk_init_allocated_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Tejun Heo09ac46c2011-12-14 00:33:38 +0100700bool blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Tejun Heo34f60552011-12-14 00:33:37 +0100702 if (likely(!blk_queue_dead(q))) {
Tejun Heo09ac46c2011-12-14 00:33:38 +0100703 __blk_get_queue(q);
704 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706
Tejun Heo09ac46c2011-12-14 00:33:38 +0100707 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
Jens Axboed86e0e82011-05-27 07:44:43 +0200709EXPORT_SYMBOL(blk_get_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Jens Axboe165125e2007-07-24 09:28:11 +0200711static inline void blk_free_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Tejun Heof1f8cc92011-12-14 00:33:42 +0100713 if (rq->cmd_flags & REQ_ELVPRIV) {
Tejun Heocb98fc82005-10-28 08:29:39 +0200714 elv_put_request(q, rq);
Tejun Heof1f8cc92011-12-14 00:33:42 +0100715 if (rq->elv.icq)
Tejun Heo11a31222012-02-07 07:51:30 +0100716 put_io_context(rq->elv.icq->ioc);
Tejun Heof1f8cc92011-12-14 00:33:42 +0100717 }
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 mempool_free(rq, q->rq.rq_pool);
720}
721
Jens Axboe1ea25ecb2006-07-18 22:24:11 +0200722static struct request *
Tejun Heo852c7882012-03-05 13:15:27 -0800723blk_alloc_request(struct request_queue *q, struct bio *bio, struct io_cq *icq,
Tejun Heof1f8cc92011-12-14 00:33:42 +0100724 unsigned int flags, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
726 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
727
728 if (!rq)
729 return NULL;
730
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200731 blk_rq_init(q, rq);
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200732
Jerome Marchand42dad762009-04-22 14:01:49 +0200733 rq->cmd_flags = flags | REQ_ALLOCED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Tejun Heof1f8cc92011-12-14 00:33:42 +0100735 if (flags & REQ_ELVPRIV) {
736 rq->elv.icq = icq;
Tejun Heo852c7882012-03-05 13:15:27 -0800737 if (unlikely(elv_set_request(q, rq, bio, gfp_mask))) {
Tejun Heof1f8cc92011-12-14 00:33:42 +0100738 mempool_free(rq, q->rq.rq_pool);
739 return NULL;
740 }
741 /* @rq->elv.icq holds on to io_context until @rq is freed */
742 if (icq)
743 get_io_context(icq->ioc);
Tejun Heocb98fc82005-10-28 08:29:39 +0200744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Tejun Heocb98fc82005-10-28 08:29:39 +0200746 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
749/*
750 * ioc_batching returns true if the ioc is a valid batching request and
751 * should be given priority access to a request.
752 */
Jens Axboe165125e2007-07-24 09:28:11 +0200753static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
755 if (!ioc)
756 return 0;
757
758 /*
759 * Make sure the process is able to allocate at least 1 request
760 * even if the batch times out, otherwise we could theoretically
761 * lose wakeups.
762 */
763 return ioc->nr_batch_requests == q->nr_batching ||
764 (ioc->nr_batch_requests > 0
765 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
766}
767
768/*
769 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
770 * will cause the process to be a "batcher" on all queues in the system. This
771 * is the behaviour we want though - once it gets a wakeup it should be given
772 * a nice run.
773 */
Jens Axboe165125e2007-07-24 09:28:11 +0200774static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
776 if (!ioc || ioc_batching(q, ioc))
777 return;
778
779 ioc->nr_batch_requests = q->nr_batching;
780 ioc->last_waited = jiffies;
781}
782
Jens Axboe1faa16d2009-04-06 14:48:01 +0200783static void __freed_request(struct request_queue *q, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
785 struct request_list *rl = &q->rq;
786
Jens Axboe1faa16d2009-04-06 14:48:01 +0200787 if (rl->count[sync] < queue_congestion_off_threshold(q))
788 blk_clear_queue_congested(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Jens Axboe1faa16d2009-04-06 14:48:01 +0200790 if (rl->count[sync] + 1 <= q->nr_requests) {
791 if (waitqueue_active(&rl->wait[sync]))
792 wake_up(&rl->wait[sync]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Jens Axboe1faa16d2009-04-06 14:48:01 +0200794 blk_clear_queue_full(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
796}
797
798/*
799 * A request has just been released. Account for it, update the full and
800 * congestion status, wake up any waiters. Called under q->queue_lock.
801 */
Tejun Heo75eb6c32011-10-19 14:31:22 +0200802static void freed_request(struct request_queue *q, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
804 struct request_list *rl = &q->rq;
Tejun Heo75eb6c32011-10-19 14:31:22 +0200805 int sync = rw_is_sync(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Jens Axboe1faa16d2009-04-06 14:48:01 +0200807 rl->count[sync]--;
Tejun Heo75eb6c32011-10-19 14:31:22 +0200808 if (flags & REQ_ELVPRIV)
Tejun Heocb98fc82005-10-28 08:29:39 +0200809 rl->elvpriv--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Jens Axboe1faa16d2009-04-06 14:48:01 +0200811 __freed_request(q, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Jens Axboe1faa16d2009-04-06 14:48:01 +0200813 if (unlikely(rl->starved[sync ^ 1]))
814 __freed_request(q, sync ^ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815}
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817/*
Mike Snitzer9d5a4e92011-02-11 11:05:46 +0100818 * Determine if elevator data should be initialized when allocating the
819 * request associated with @bio.
820 */
821static bool blk_rq_should_init_elevator(struct bio *bio)
822{
823 if (!bio)
824 return true;
825
826 /*
827 * Flush requests do not use the elevator so skip initialization.
828 * This allows a request to share the flush and elevator data.
829 */
830 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA))
831 return false;
832
833 return true;
834}
835
Tejun Heoda8303c2011-10-19 14:33:05 +0200836/**
Tejun Heo852c7882012-03-05 13:15:27 -0800837 * rq_ioc - determine io_context for request allocation
838 * @bio: request being allocated is for this bio (can be %NULL)
839 *
840 * Determine io_context to use for request allocation for @bio. May return
841 * %NULL if %current->io_context doesn't exist.
842 */
843static struct io_context *rq_ioc(struct bio *bio)
844{
845#ifdef CONFIG_BLK_CGROUP
846 if (bio && bio->bi_ioc)
847 return bio->bi_ioc;
848#endif
849 return current->io_context;
850}
851
852/**
Tejun Heoda8303c2011-10-19 14:33:05 +0200853 * get_request - get a free request
854 * @q: request_queue to allocate request from
855 * @rw_flags: RW and SYNC flags
856 * @bio: bio to allocate request for (can be %NULL)
857 * @gfp_mask: allocation mask
858 *
859 * Get a free request from @q. This function may fail under memory
860 * pressure or if @q is dead.
861 *
862 * Must be callled with @q->queue_lock held and,
863 * Returns %NULL on failure, with @q->queue_lock held.
864 * Returns !%NULL on success, with @q->queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 */
Jens Axboe165125e2007-07-24 09:28:11 +0200866static struct request *get_request(struct request_queue *q, int rw_flags,
Jens Axboe7749a8d2006-12-13 13:02:26 +0100867 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Tejun Heob6792812012-03-05 13:15:23 -0800869 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 struct request_list *rl = &q->rq;
Tejun Heof1f8cc92011-12-14 00:33:42 +0100871 struct elevator_type *et;
Tejun Heof2dbd762011-12-14 00:33:40 +0100872 struct io_context *ioc;
Tejun Heof1f8cc92011-12-14 00:33:42 +0100873 struct io_cq *icq = NULL;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200874 const bool is_sync = rw_is_sync(rw_flags) != 0;
Tejun Heof2dbd762011-12-14 00:33:40 +0100875 bool retried = false;
Tejun Heo75eb6c32011-10-19 14:31:22 +0200876 int may_queue;
Tejun Heof2dbd762011-12-14 00:33:40 +0100877retry:
Tejun Heof1f8cc92011-12-14 00:33:42 +0100878 et = q->elevator->type;
Tejun Heo852c7882012-03-05 13:15:27 -0800879 ioc = rq_ioc(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Tejun Heo34f60552011-12-14 00:33:37 +0100881 if (unlikely(blk_queue_dead(q)))
Tejun Heoda8303c2011-10-19 14:33:05 +0200882 return NULL;
883
Jens Axboe7749a8d2006-12-13 13:02:26 +0100884 may_queue = elv_may_queue(q, rw_flags);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100885 if (may_queue == ELV_MQUEUE_NO)
886 goto rq_starved;
887
Jens Axboe1faa16d2009-04-06 14:48:01 +0200888 if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) {
889 if (rl->count[is_sync]+1 >= q->nr_requests) {
Tejun Heof2dbd762011-12-14 00:33:40 +0100890 /*
891 * We want ioc to record batching state. If it's
892 * not already there, creating a new one requires
893 * dropping queue_lock, which in turn requires
894 * retesting conditions to avoid queue hang.
895 */
896 if (!ioc && !retried) {
897 spin_unlock_irq(q->queue_lock);
Tejun Heo24acfc32012-03-05 13:15:24 -0800898 create_io_context(gfp_mask, q->node);
Tejun Heof2dbd762011-12-14 00:33:40 +0100899 spin_lock_irq(q->queue_lock);
900 retried = true;
901 goto retry;
902 }
903
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100904 /*
905 * The queue will fill after this allocation, so set
906 * it as full, and mark this process as "batching".
907 * This process will be allowed to complete a batch of
908 * requests, others will be blocked.
909 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200910 if (!blk_queue_full(q, is_sync)) {
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100911 ioc_set_batching(q, ioc);
Jens Axboe1faa16d2009-04-06 14:48:01 +0200912 blk_set_queue_full(q, is_sync);
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100913 } else {
914 if (may_queue != ELV_MQUEUE_MUST
915 && !ioc_batching(q, ioc)) {
916 /*
917 * The queue is full and the allocating
918 * process is not a "batcher", and not
919 * exempted by the IO scheduler
920 */
Tejun Heob6792812012-03-05 13:15:23 -0800921 return NULL;
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100922 }
923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 }
Jens Axboe1faa16d2009-04-06 14:48:01 +0200925 blk_set_queue_congested(q, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 }
927
Jens Axboe082cf692005-06-28 16:35:11 +0200928 /*
929 * Only allow batching queuers to allocate up to 50% over the defined
930 * limit of requests, otherwise we could have thousands of requests
931 * allocated with any setting of ->nr_requests
932 */
Jens Axboe1faa16d2009-04-06 14:48:01 +0200933 if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
Tejun Heob6792812012-03-05 13:15:23 -0800934 return NULL;
Hugh Dickinsfd782a42005-06-29 15:15:40 +0100935
Jens Axboe1faa16d2009-04-06 14:48:01 +0200936 rl->count[is_sync]++;
937 rl->starved[is_sync] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200938
Tejun Heof1f8cc92011-12-14 00:33:42 +0100939 /*
940 * Decide whether the new request will be managed by elevator. If
941 * so, mark @rw_flags and increment elvpriv. Non-zero elvpriv will
942 * prevent the current elevator from being destroyed until the new
943 * request is freed. This guarantees icq's won't be destroyed and
944 * makes creating new ones safe.
945 *
946 * Also, lookup icq while holding queue_lock. If it doesn't exist,
947 * it will be created after releasing queue_lock.
948 */
Tejun Heod7325802012-03-05 13:14:58 -0800949 if (blk_rq_should_init_elevator(bio) && !blk_queue_bypass(q)) {
Tejun Heo75eb6c32011-10-19 14:31:22 +0200950 rw_flags |= REQ_ELVPRIV;
951 rl->elvpriv++;
Tejun Heof1f8cc92011-12-14 00:33:42 +0100952 if (et->icq_cache && ioc)
953 icq = ioc_lookup_icq(ioc, q);
Mike Snitzer9d5a4e92011-02-11 11:05:46 +0100954 }
Tejun Heocb98fc82005-10-28 08:29:39 +0200955
Jens Axboef253b862010-10-24 22:06:02 +0200956 if (blk_queue_io_stat(q))
957 rw_flags |= REQ_IO_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 spin_unlock_irq(q->queue_lock);
959
Tejun Heof1f8cc92011-12-14 00:33:42 +0100960 /* create icq if missing */
Shaohua Li05c30b92012-01-19 09:20:10 +0100961 if ((rw_flags & REQ_ELVPRIV) && unlikely(et->icq_cache && !icq)) {
Tejun Heo852c7882012-03-05 13:15:27 -0800962 create_io_context(gfp_mask, q->node);
963 ioc = rq_ioc(bio);
964 if (!ioc)
965 goto fail_alloc;
966 icq = ioc_create_icq(ioc, q, gfp_mask);
Shaohua Li05c30b92012-01-19 09:20:10 +0100967 if (!icq)
Tejun Heob6792812012-03-05 13:15:23 -0800968 goto fail_alloc;
Shaohua Li05c30b92012-01-19 09:20:10 +0100969 }
Tejun Heof1f8cc92011-12-14 00:33:42 +0100970
Tejun Heo852c7882012-03-05 13:15:27 -0800971 rq = blk_alloc_request(q, bio, icq, rw_flags, gfp_mask);
Tejun Heob6792812012-03-05 13:15:23 -0800972 if (unlikely(!rq))
973 goto fail_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Jens Axboe88ee5ef2005-11-12 11:09:12 +0100975 /*
976 * ioc may be NULL here, and ioc_batching will be false. That's
977 * OK, if the queue is under the request limit then requests need
978 * not count toward the nr_batch_requests limit. There will always
979 * be some limit enforced by BLK_BATCH_TIME.
980 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (ioc_batching(q, ioc))
982 ioc->nr_batch_requests--;
Jens Axboe6728cb02008-01-31 13:03:55 +0100983
Jens Axboe1faa16d2009-04-06 14:48:01 +0200984 trace_block_getrq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return rq;
Tejun Heob6792812012-03-05 13:15:23 -0800986
987fail_alloc:
988 /*
989 * Allocation failed presumably due to memory. Undo anything we
990 * might have messed up.
991 *
992 * Allocating task should really be put onto the front of the wait
993 * queue, but this is pretty rare.
994 */
995 spin_lock_irq(q->queue_lock);
996 freed_request(q, rw_flags);
997
998 /*
999 * in the very unlikely event that allocation failed and no
1000 * requests for this direction was pending, mark us starved so that
1001 * freeing of a request in the other direction will notice
1002 * us. another possible fix would be to split the rq mempool into
1003 * READ and WRITE
1004 */
1005rq_starved:
1006 if (unlikely(rl->count[is_sync] == 0))
1007 rl->starved[is_sync] = 1;
1008 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010
Tejun Heoda8303c2011-10-19 14:33:05 +02001011/**
1012 * get_request_wait - get a free request with retry
1013 * @q: request_queue to allocate request from
1014 * @rw_flags: RW and SYNC flags
1015 * @bio: bio to allocate request for (can be %NULL)
Nick Piggind6344532005-06-28 20:45:14 -07001016 *
Tejun Heoda8303c2011-10-19 14:33:05 +02001017 * Get a free request from @q. This function keeps retrying under memory
1018 * pressure and fails iff @q is dead.
1019 *
1020 * Must be callled with @q->queue_lock held and,
1021 * Returns %NULL on failure, with @q->queue_lock held.
1022 * Returns !%NULL on success, with @q->queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 */
Jens Axboe165125e2007-07-24 09:28:11 +02001024static struct request *get_request_wait(struct request_queue *q, int rw_flags,
Jens Axboe22e2c502005-06-27 10:55:12 +02001025 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
Jens Axboe1faa16d2009-04-06 14:48:01 +02001027 const bool is_sync = rw_is_sync(rw_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 struct request *rq;
1029
Jens Axboe7749a8d2006-12-13 13:02:26 +01001030 rq = get_request(q, rw_flags, bio, GFP_NOIO);
Nick Piggin450991b2005-06-28 20:45:13 -07001031 while (!rq) {
1032 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 struct request_list *rl = &q->rq;
1034
Tejun Heo34f60552011-12-14 00:33:37 +01001035 if (unlikely(blk_queue_dead(q)))
Tejun Heoda8303c2011-10-19 14:33:05 +02001036 return NULL;
1037
Jens Axboe1faa16d2009-04-06 14:48:01 +02001038 prepare_to_wait_exclusive(&rl->wait[is_sync], &wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 TASK_UNINTERRUPTIBLE);
1040
Jens Axboe1faa16d2009-04-06 14:48:01 +02001041 trace_block_sleeprq(q, bio, rw_flags & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +02001043 spin_unlock_irq(q->queue_lock);
1044 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +02001046 /*
1047 * After sleeping, we become a "batching" process and
1048 * will be able to allocate at least one request, and
1049 * up to a big batch of them for a small period time.
1050 * See ioc_batching, ioc_set_batching
1051 */
Tejun Heo24acfc32012-03-05 13:15:24 -08001052 create_io_context(GFP_NOIO, q->node);
Tejun Heof2dbd762011-12-14 00:33:40 +01001053 ioc_set_batching(q, current->io_context);
Jens Axboe2056a782006-03-23 20:00:26 +01001054
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +02001055 spin_lock_irq(q->queue_lock);
Jens Axboe1faa16d2009-04-06 14:48:01 +02001056 finish_wait(&rl->wait[is_sync], &wait);
Zhang, Yanmin05caf8d2008-05-22 15:13:29 +02001057
1058 rq = get_request(q, rw_flags, bio, GFP_NOIO);
1059 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 return rq;
1062}
1063
Jens Axboe165125e2007-07-24 09:28:11 +02001064struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
1066 struct request *rq;
1067
1068 BUG_ON(rw != READ && rw != WRITE);
1069
Nick Piggind6344532005-06-28 20:45:14 -07001070 spin_lock_irq(q->queue_lock);
Tejun Heoda8303c2011-10-19 14:33:05 +02001071 if (gfp_mask & __GFP_WAIT)
Jens Axboe22e2c502005-06-27 10:55:12 +02001072 rq = get_request_wait(q, rw, NULL);
Tejun Heoda8303c2011-10-19 14:33:05 +02001073 else
Jens Axboe22e2c502005-06-27 10:55:12 +02001074 rq = get_request(q, rw, NULL, gfp_mask);
Tejun Heoda8303c2011-10-19 14:33:05 +02001075 if (!rq)
1076 spin_unlock_irq(q->queue_lock);
Nick Piggind6344532005-06-28 20:45:14 -07001077 /* q->queue_lock is unlocked at this point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 return rq;
1080}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081EXPORT_SYMBOL(blk_get_request);
1082
1083/**
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001084 * blk_make_request - given a bio, allocate a corresponding struct request.
Randy Dunlap8ebf9752009-06-11 20:00:41 -07001085 * @q: target request queue
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001086 * @bio: The bio describing the memory mappings that will be submitted for IO.
1087 * It may be a chained-bio properly constructed by block/bio layer.
Randy Dunlap8ebf9752009-06-11 20:00:41 -07001088 * @gfp_mask: gfp flags to be used for memory allocation
Jens Axboedc72ef42006-07-20 14:54:05 +02001089 *
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001090 * blk_make_request is the parallel of generic_make_request for BLOCK_PC
1091 * type commands. Where the struct request needs to be farther initialized by
1092 * the caller. It is passed a &struct bio, which describes the memory info of
1093 * the I/O transfer.
1094 *
1095 * The caller of blk_make_request must make sure that bi_io_vec
1096 * are set to describe the memory buffers. That bio_data_dir() will return
1097 * the needed direction of the request. (And all bio's in the passed bio-chain
1098 * are properly set accordingly)
1099 *
1100 * If called under none-sleepable conditions, mapped bio buffers must not
1101 * need bouncing, by calling the appropriate masked or flagged allocator,
1102 * suitable for the target device. Otherwise the call to blk_queue_bounce will
1103 * BUG.
Jens Axboe53674ac2009-05-19 19:52:35 +02001104 *
1105 * WARNING: When allocating/cloning a bio-chain, careful consideration should be
1106 * given to how you allocate bios. In particular, you cannot use __GFP_WAIT for
1107 * anything but the first bio in the chain. Otherwise you risk waiting for IO
1108 * completion of a bio that hasn't been submitted yet, thus resulting in a
1109 * deadlock. Alternatively bios should be allocated using bio_kmalloc() instead
1110 * of bio_alloc(), as that avoids the mempool deadlock.
1111 * If possible a big IO should be split into smaller parts when allocation
1112 * fails. Partial allocation should not be an error, or you risk a live-lock.
Jens Axboedc72ef42006-07-20 14:54:05 +02001113 */
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001114struct request *blk_make_request(struct request_queue *q, struct bio *bio,
1115 gfp_t gfp_mask)
Jens Axboedc72ef42006-07-20 14:54:05 +02001116{
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001117 struct request *rq = blk_get_request(q, bio_data_dir(bio), gfp_mask);
1118
1119 if (unlikely(!rq))
1120 return ERR_PTR(-ENOMEM);
1121
1122 for_each_bio(bio) {
1123 struct bio *bounce_bio = bio;
1124 int ret;
1125
1126 blk_queue_bounce(q, &bounce_bio);
1127 ret = blk_rq_append_bio(q, rq, bounce_bio);
1128 if (unlikely(ret)) {
1129 blk_put_request(rq);
1130 return ERR_PTR(ret);
1131 }
1132 }
1133
1134 return rq;
Jens Axboedc72ef42006-07-20 14:54:05 +02001135}
Boaz Harrosh79eb63e2009-05-17 18:57:15 +03001136EXPORT_SYMBOL(blk_make_request);
Jens Axboedc72ef42006-07-20 14:54:05 +02001137
1138/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 * blk_requeue_request - put a request back on queue
1140 * @q: request queue where request should be inserted
1141 * @rq: request to be inserted
1142 *
1143 * Description:
1144 * Drivers often keep queueing requests until the hardware cannot accept
1145 * more, when that condition happens we need to put the request back
1146 * on the queue. Must be called with queue lock held.
1147 */
Jens Axboe165125e2007-07-24 09:28:11 +02001148void blk_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
Jens Axboe242f9dc2008-09-14 05:55:09 -07001150 blk_delete_timer(rq);
1151 blk_clear_rq_complete(rq);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001152 trace_block_rq_requeue(q, rq);
Jens Axboe2056a782006-03-23 20:00:26 +01001153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 if (blk_rq_tagged(rq))
1155 blk_queue_end_tag(q, rq);
1156
James Bottomleyba396a62009-05-27 14:17:08 +02001157 BUG_ON(blk_queued_rq(rq));
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 elv_requeue_request(q, rq);
1160}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161EXPORT_SYMBOL(blk_requeue_request);
1162
Jens Axboe73c10102011-03-08 13:19:51 +01001163static void add_acct_request(struct request_queue *q, struct request *rq,
1164 int where)
1165{
1166 drive_stat_acct(rq, 1);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001167 __elv_add_request(q, rq, where);
Jens Axboe73c10102011-03-08 13:19:51 +01001168}
1169
Tejun Heo074a7ac2008-08-25 19:56:14 +09001170static void part_round_stats_single(int cpu, struct hd_struct *part,
1171 unsigned long now)
1172{
1173 if (now == part->stamp)
1174 return;
1175
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001176 if (part_in_flight(part)) {
Tejun Heo074a7ac2008-08-25 19:56:14 +09001177 __part_stat_add(cpu, part, time_in_queue,
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001178 part_in_flight(part) * (now - part->stamp));
Tejun Heo074a7ac2008-08-25 19:56:14 +09001179 __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
1180 }
1181 part->stamp = now;
1182}
1183
1184/**
Randy Dunlap496aa8a2008-10-16 07:46:23 +02001185 * part_round_stats() - Round off the performance stats on a struct disk_stats.
1186 * @cpu: cpu number for stats access
1187 * @part: target partition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 *
1189 * The average IO queue length and utilisation statistics are maintained
1190 * by observing the current state of the queue length and the amount of
1191 * time it has been in this state for.
1192 *
1193 * Normally, that accounting is done on IO completion, but that can result
1194 * in more than a second's worth of IO being accounted for within any one
1195 * second, leading to >100% utilisation. To deal with that, we call this
1196 * function to do a round-off before returning the results when reading
1197 * /proc/diskstats. This accounts immediately for all queue usage up to
1198 * the current jiffies and restarts the counters again.
1199 */
Tejun Heoc9959052008-08-25 19:47:21 +09001200void part_round_stats(int cpu, struct hd_struct *part)
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001201{
1202 unsigned long now = jiffies;
1203
Tejun Heo074a7ac2008-08-25 19:56:14 +09001204 if (part->partno)
1205 part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
1206 part_round_stats_single(cpu, part, now);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001207}
Tejun Heo074a7ac2008-08-25 19:56:14 +09001208EXPORT_SYMBOL_GPL(part_round_stats);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210/*
1211 * queue lock must be held
1212 */
Jens Axboe165125e2007-07-24 09:28:11 +02001213void __blk_put_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (unlikely(!q))
1216 return;
1217 if (unlikely(--req->ref_count))
1218 return;
1219
Tejun Heo8922e162005-10-20 16:23:44 +02001220 elv_completed_request(q, req);
1221
Boaz Harrosh1cd96c22009-03-24 12:35:07 +01001222 /* this is a bio leak */
1223 WARN_ON(req->bio != NULL);
1224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 /*
1226 * Request may not have originated from ll_rw_blk. if not,
1227 * it didn't come out of our reserved rq pools
1228 */
Jens Axboe49171e52006-08-10 08:59:11 +02001229 if (req->cmd_flags & REQ_ALLOCED) {
Tejun Heo75eb6c32011-10-19 14:31:22 +02001230 unsigned int flags = req->cmd_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 BUG_ON(!list_empty(&req->queuelist));
Jens Axboe98170642006-07-28 09:23:08 +02001233 BUG_ON(!hlist_unhashed(&req->hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 blk_free_request(q, req);
Tejun Heo75eb6c32011-10-19 14:31:22 +02001236 freed_request(q, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 }
1238}
Mike Christie6e39b69e2005-11-11 05:30:24 -06001239EXPORT_SYMBOL_GPL(__blk_put_request);
1240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241void blk_put_request(struct request *req)
1242{
Tejun Heo8922e162005-10-20 16:23:44 +02001243 unsigned long flags;
Jens Axboe165125e2007-07-24 09:28:11 +02001244 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
FUJITA Tomonori52a93ba2008-07-15 21:21:45 +02001246 spin_lock_irqsave(q->queue_lock, flags);
1247 __blk_put_request(q, req);
1248 spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250EXPORT_SYMBOL(blk_put_request);
1251
Christoph Hellwig66ac0282010-06-18 16:59:42 +02001252/**
1253 * blk_add_request_payload - add a payload to a request
1254 * @rq: request to update
1255 * @page: page backing the payload
1256 * @len: length of the payload.
1257 *
1258 * This allows to later add a payload to an already submitted request by
1259 * a block driver. The driver needs to take care of freeing the payload
1260 * itself.
1261 *
1262 * Note that this is a quite horrible hack and nothing but handling of
1263 * discard requests should ever use it.
1264 */
1265void blk_add_request_payload(struct request *rq, struct page *page,
1266 unsigned int len)
1267{
1268 struct bio *bio = rq->bio;
1269
1270 bio->bi_io_vec->bv_page = page;
1271 bio->bi_io_vec->bv_offset = 0;
1272 bio->bi_io_vec->bv_len = len;
1273
1274 bio->bi_size = len;
1275 bio->bi_vcnt = 1;
1276 bio->bi_phys_segments = 1;
1277
1278 rq->__data_len = rq->resid_len = len;
1279 rq->nr_phys_segments = 1;
1280 rq->buffer = bio_data(bio);
1281}
1282EXPORT_SYMBOL_GPL(blk_add_request_payload);
1283
Jens Axboe73c10102011-03-08 13:19:51 +01001284static bool bio_attempt_back_merge(struct request_queue *q, struct request *req,
1285 struct bio *bio)
1286{
1287 const int ff = bio->bi_rw & REQ_FAILFAST_MASK;
1288
Jens Axboe73c10102011-03-08 13:19:51 +01001289 if (!ll_back_merge_fn(q, req, bio))
1290 return false;
1291
1292 trace_block_bio_backmerge(q, bio);
1293
1294 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1295 blk_rq_set_mixed_merge(req);
1296
1297 req->biotail->bi_next = bio;
1298 req->biotail = bio;
1299 req->__data_len += bio->bi_size;
1300 req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
1301
1302 drive_stat_acct(req, 0);
1303 return true;
1304}
1305
1306static bool bio_attempt_front_merge(struct request_queue *q,
1307 struct request *req, struct bio *bio)
1308{
1309 const int ff = bio->bi_rw & REQ_FAILFAST_MASK;
Jens Axboe73c10102011-03-08 13:19:51 +01001310
Jens Axboe73c10102011-03-08 13:19:51 +01001311 if (!ll_front_merge_fn(q, req, bio))
1312 return false;
1313
1314 trace_block_bio_frontmerge(q, bio);
1315
1316 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1317 blk_rq_set_mixed_merge(req);
1318
Jens Axboe73c10102011-03-08 13:19:51 +01001319 bio->bi_next = req->bio;
1320 req->bio = bio;
1321
1322 /*
1323 * may not be valid. if the low level driver said
1324 * it didn't need a bounce buffer then it better
1325 * not touch req->buffer either...
1326 */
1327 req->buffer = bio_data(bio);
1328 req->__sector = bio->bi_sector;
1329 req->__data_len += bio->bi_size;
1330 req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
1331
1332 drive_stat_acct(req, 0);
1333 return true;
1334}
1335
Tejun Heobd87b582011-10-19 14:33:08 +02001336/**
1337 * attempt_plug_merge - try to merge with %current's plugged list
1338 * @q: request_queue new bio is being queued at
1339 * @bio: new bio being queued
1340 * @request_count: out parameter for number of traversed plugged requests
1341 *
1342 * Determine whether @bio being queued on @q can be merged with a request
1343 * on %current's plugged list. Returns %true if merge was successful,
1344 * otherwise %false.
1345 *
Tejun Heo07c2bd32012-02-08 09:19:42 +01001346 * Plugging coalesces IOs from the same issuer for the same purpose without
1347 * going through @q->queue_lock. As such it's more of an issuing mechanism
1348 * than scheduling, and the request, while may have elvpriv data, is not
1349 * added on the elevator at this point. In addition, we don't have
1350 * reliable access to the elevator outside queue lock. Only check basic
1351 * merging parameters without querying the elevator.
Jens Axboe73c10102011-03-08 13:19:51 +01001352 */
Tejun Heobd87b582011-10-19 14:33:08 +02001353static bool attempt_plug_merge(struct request_queue *q, struct bio *bio,
1354 unsigned int *request_count)
Jens Axboe73c10102011-03-08 13:19:51 +01001355{
1356 struct blk_plug *plug;
1357 struct request *rq;
1358 bool ret = false;
1359
Tejun Heobd87b582011-10-19 14:33:08 +02001360 plug = current->plug;
Jens Axboe73c10102011-03-08 13:19:51 +01001361 if (!plug)
1362 goto out;
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001363 *request_count = 0;
Jens Axboe73c10102011-03-08 13:19:51 +01001364
1365 list_for_each_entry_reverse(rq, &plug->list, queuelist) {
1366 int el_ret;
1367
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001368 (*request_count)++;
1369
Tejun Heo07c2bd32012-02-08 09:19:42 +01001370 if (rq->q != q || !blk_rq_merge_ok(rq, bio))
Jens Axboe73c10102011-03-08 13:19:51 +01001371 continue;
1372
Tejun Heo050c8ea2012-02-08 09:19:38 +01001373 el_ret = blk_try_merge(rq, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001374 if (el_ret == ELEVATOR_BACK_MERGE) {
1375 ret = bio_attempt_back_merge(q, rq, bio);
1376 if (ret)
1377 break;
1378 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
1379 ret = bio_attempt_front_merge(q, rq, bio);
1380 if (ret)
1381 break;
1382 }
1383 }
1384out:
1385 return ret;
1386}
1387
Jens Axboe86db1e22008-01-29 14:53:40 +01001388void init_request_from_bio(struct request *req, struct bio *bio)
Tejun Heo52d9e672006-01-06 09:49:58 +01001389{
Jens Axboe4aff5e22006-08-10 08:44:47 +02001390 req->cmd_type = REQ_TYPE_FS;
Tejun Heo52d9e672006-01-06 09:49:58 +01001391
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001392 req->cmd_flags |= bio->bi_rw & REQ_COMMON_MASK;
1393 if (bio->bi_rw & REQ_RAHEAD)
Tejun Heoa82afdf2009-07-03 17:48:16 +09001394 req->cmd_flags |= REQ_FAILFAST_MASK;
Jens Axboeb31dc662006-06-13 08:26:10 +02001395
Tejun Heo52d9e672006-01-06 09:49:58 +01001396 req->errors = 0;
Tejun Heoa2dec7b2009-05-07 22:24:44 +09001397 req->__sector = bio->bi_sector;
Tejun Heo52d9e672006-01-06 09:49:58 +01001398 req->ioprio = bio_prio(bio);
NeilBrownbc1c56f2007-08-16 13:31:30 +02001399 blk_rq_bio_prep(req->q, req, bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001400}
1401
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001402void blk_queue_bio(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
Jiri Slaby5e00d1b2010-08-12 14:31:06 +02001404 const bool sync = !!(bio->bi_rw & REQ_SYNC);
Jens Axboe73c10102011-03-08 13:19:51 +01001405 struct blk_plug *plug;
1406 int el_ret, rw_flags, where = ELEVATOR_INSERT_SORT;
1407 struct request *req;
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001408 unsigned int request_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 /*
1411 * low level driver can indicate that it wants pages above a
1412 * certain limit bounced to low memory (ie for highmem, or even
1413 * ISA dma in theory)
1414 */
1415 blk_queue_bounce(q, &bio);
1416
Tejun Heo4fed9472010-09-03 11:56:17 +02001417 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) {
Jens Axboe73c10102011-03-08 13:19:51 +01001418 spin_lock_irq(q->queue_lock);
Tejun Heoae1b1532011-01-25 12:43:54 +01001419 where = ELEVATOR_INSERT_FLUSH;
Tejun Heo28e7d182010-09-03 11:56:16 +02001420 goto get_rq;
1421 }
1422
Jens Axboe73c10102011-03-08 13:19:51 +01001423 /*
1424 * Check if we can merge with the plugged list before grabbing
1425 * any locks.
1426 */
Tejun Heobd87b582011-10-19 14:33:08 +02001427 if (attempt_plug_merge(q, bio, &request_count))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001428 return;
Jens Axboe73c10102011-03-08 13:19:51 +01001429
1430 spin_lock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432 el_ret = elv_merge(q, &req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001433 if (el_ret == ELEVATOR_BACK_MERGE) {
Jens Axboe73c10102011-03-08 13:19:51 +01001434 if (bio_attempt_back_merge(q, req, bio)) {
Tejun Heo07c2bd32012-02-08 09:19:42 +01001435 elv_bio_merged(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001436 if (!attempt_back_merge(q, req))
1437 elv_merged_request(q, req, el_ret);
1438 goto out_unlock;
Tejun Heo80a761f2009-07-03 17:48:17 +09001439 }
Jens Axboe73c10102011-03-08 13:19:51 +01001440 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
Jens Axboe73c10102011-03-08 13:19:51 +01001441 if (bio_attempt_front_merge(q, req, bio)) {
Tejun Heo07c2bd32012-02-08 09:19:42 +01001442 elv_bio_merged(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001443 if (!attempt_front_merge(q, req))
1444 elv_merged_request(q, req, el_ret);
1445 goto out_unlock;
1446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 }
1448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449get_rq:
Nick Piggin450991b2005-06-28 20:45:13 -07001450 /*
Jens Axboe7749a8d2006-12-13 13:02:26 +01001451 * This sync check and mask will be re-done in init_request_from_bio(),
1452 * but we need to set it earlier to expose the sync flag to the
1453 * rq allocator and io schedulers.
1454 */
1455 rw_flags = bio_data_dir(bio);
1456 if (sync)
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001457 rw_flags |= REQ_SYNC;
Jens Axboe7749a8d2006-12-13 13:02:26 +01001458
1459 /*
Nick Piggin450991b2005-06-28 20:45:13 -07001460 * Grab a free request. This is might sleep but can not fail.
Nick Piggind6344532005-06-28 20:45:14 -07001461 * Returns with the queue unlocked.
Nick Piggin450991b2005-06-28 20:45:13 -07001462 */
Jens Axboe7749a8d2006-12-13 13:02:26 +01001463 req = get_request_wait(q, rw_flags, bio);
Tejun Heoda8303c2011-10-19 14:33:05 +02001464 if (unlikely(!req)) {
1465 bio_endio(bio, -ENODEV); /* @q is dead */
1466 goto out_unlock;
1467 }
Nick Piggind6344532005-06-28 20:45:14 -07001468
Nick Piggin450991b2005-06-28 20:45:13 -07001469 /*
1470 * After dropping the lock and possibly sleeping here, our request
1471 * may now be mergeable after it had proven unmergeable (above).
1472 * We don't worry about that case for efficiency. It won't happen
1473 * often, and the elevators are able to handle it.
1474 */
Tejun Heo52d9e672006-01-06 09:49:58 +01001475 init_request_from_bio(req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Tao Ma9562ad92011-10-24 16:11:30 +02001477 if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags))
Jens Axboe11ccf112011-07-26 15:01:15 +02001478 req->cpu = raw_smp_processor_id();
Tejun Heodd831002010-09-03 11:56:16 +02001479
Jens Axboe73c10102011-03-08 13:19:51 +01001480 plug = current->plug;
Jens Axboe721a9602011-03-09 11:56:30 +01001481 if (plug) {
Jens Axboedc6d36c2011-04-12 10:28:28 +02001482 /*
1483 * If this is the first request added after a plug, fire
1484 * of a plug trace. If others have been added before, check
1485 * if we have multiple devices in this plug. If so, make a
1486 * note to sort the list before dispatch.
1487 */
1488 if (list_empty(&plug->list))
1489 trace_block_plug(q);
Shaohua Li3540d5e2011-11-16 09:21:50 +01001490 else {
1491 if (!plug->should_sort) {
1492 struct request *__rq;
Jens Axboe73c10102011-03-08 13:19:51 +01001493
Shaohua Li3540d5e2011-11-16 09:21:50 +01001494 __rq = list_entry_rq(plug->list.prev);
1495 if (__rq->q != q)
1496 plug->should_sort = 1;
1497 }
Shaohua Li019ceb72011-11-16 09:21:50 +01001498 if (request_count >= BLK_MAX_REQUEST_COUNT) {
Shaohua Li3540d5e2011-11-16 09:21:50 +01001499 blk_flush_plug_list(plug, false);
Shaohua Li019ceb72011-11-16 09:21:50 +01001500 trace_block_plug(q);
1501 }
Jens Axboe73c10102011-03-08 13:19:51 +01001502 }
Shaohua Lia6327162011-08-24 16:04:32 +02001503 list_add_tail(&req->queuelist, &plug->list);
1504 drive_stat_acct(req, 1);
Jens Axboe73c10102011-03-08 13:19:51 +01001505 } else {
1506 spin_lock_irq(q->queue_lock);
1507 add_acct_request(q, req, where);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02001508 __blk_run_queue(q);
Jens Axboe73c10102011-03-08 13:19:51 +01001509out_unlock:
1510 spin_unlock_irq(q->queue_lock);
1511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512}
Jens Axboec20e8de2011-09-12 12:03:37 +02001513EXPORT_SYMBOL_GPL(blk_queue_bio); /* for device mapper only */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
1515/*
1516 * If bio->bi_dev is a partition, remap the location
1517 */
1518static inline void blk_partition_remap(struct bio *bio)
1519{
1520 struct block_device *bdev = bio->bi_bdev;
1521
Jens Axboebf2de6f2007-09-27 13:01:25 +02001522 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 struct hd_struct *p = bdev->bd_part;
Jens Axboea3623572005-11-01 09:26:16 +01001524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 bio->bi_sector += p->start_sect;
1526 bio->bi_bdev = bdev->bd_contains;
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001527
Mike Snitzerd07335e2010-11-16 12:52:38 +01001528 trace_block_bio_remap(bdev_get_queue(bio->bi_bdev), bio,
1529 bdev->bd_dev,
1530 bio->bi_sector - p->start_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 }
1532}
1533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534static void handle_bad_sector(struct bio *bio)
1535{
1536 char b[BDEVNAME_SIZE];
1537
1538 printk(KERN_INFO "attempt to access beyond end of device\n");
1539 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1540 bdevname(bio->bi_bdev, b),
1541 bio->bi_rw,
1542 (unsigned long long)bio->bi_sector + bio_sectors(bio),
Mike Snitzer77304d22010-11-08 14:39:12 +01001543 (long long)(i_size_read(bio->bi_bdev->bd_inode) >> 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
1545 set_bit(BIO_EOF, &bio->bi_flags);
1546}
1547
Akinobu Mitac17bb492006-12-08 02:39:46 -08001548#ifdef CONFIG_FAIL_MAKE_REQUEST
1549
1550static DECLARE_FAULT_ATTR(fail_make_request);
1551
1552static int __init setup_fail_make_request(char *str)
1553{
1554 return setup_fault_attr(&fail_make_request, str);
1555}
1556__setup("fail_make_request=", setup_fail_make_request);
1557
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001558static bool should_fail_request(struct hd_struct *part, unsigned int bytes)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001559{
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001560 return part->make_it_fail && should_fail(&fail_make_request, bytes);
Akinobu Mitac17bb492006-12-08 02:39:46 -08001561}
1562
1563static int __init fail_make_request_debugfs(void)
1564{
Akinobu Mitadd48c082011-08-03 16:21:01 -07001565 struct dentry *dir = fault_create_debugfs_attr("fail_make_request",
1566 NULL, &fail_make_request);
1567
1568 return IS_ERR(dir) ? PTR_ERR(dir) : 0;
Akinobu Mitac17bb492006-12-08 02:39:46 -08001569}
1570
1571late_initcall(fail_make_request_debugfs);
1572
1573#else /* CONFIG_FAIL_MAKE_REQUEST */
1574
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001575static inline bool should_fail_request(struct hd_struct *part,
1576 unsigned int bytes)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001577{
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001578 return false;
Akinobu Mitac17bb492006-12-08 02:39:46 -08001579}
1580
1581#endif /* CONFIG_FAIL_MAKE_REQUEST */
1582
Jens Axboec07e2b42007-07-18 13:27:58 +02001583/*
1584 * Check whether this bio extends beyond the end of the device.
1585 */
1586static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1587{
1588 sector_t maxsector;
1589
1590 if (!nr_sectors)
1591 return 0;
1592
1593 /* Test device or partition size, when known. */
Mike Snitzer77304d22010-11-08 14:39:12 +01001594 maxsector = i_size_read(bio->bi_bdev->bd_inode) >> 9;
Jens Axboec07e2b42007-07-18 13:27:58 +02001595 if (maxsector) {
1596 sector_t sector = bio->bi_sector;
1597
1598 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1599 /*
1600 * This may well happen - the kernel calls bread()
1601 * without checking the size of the device, e.g., when
1602 * mounting a device.
1603 */
1604 handle_bad_sector(bio);
1605 return 1;
1606 }
1607 }
1608
1609 return 0;
1610}
1611
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001612static noinline_for_stack bool
1613generic_make_request_checks(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
Jens Axboe165125e2007-07-24 09:28:11 +02001615 struct request_queue *q;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001616 int nr_sectors = bio_sectors(bio);
Jens Axboe51fd77b2007-11-02 08:49:08 +01001617 int err = -EIO;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001618 char b[BDEVNAME_SIZE];
1619 struct hd_struct *part;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
1621 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Jens Axboec07e2b42007-07-18 13:27:58 +02001623 if (bio_check_eod(bio, nr_sectors))
1624 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001626 q = bdev_get_queue(bio->bi_bdev);
1627 if (unlikely(!q)) {
1628 printk(KERN_ERR
1629 "generic_make_request: Trying to access "
1630 "nonexistent block-device %s (%Lu)\n",
1631 bdevname(bio->bi_bdev, b),
1632 (long long) bio->bi_sector);
1633 goto end_io;
1634 }
1635
1636 if (unlikely(!(bio->bi_rw & REQ_DISCARD) &&
1637 nr_sectors > queue_max_hw_sectors(q))) {
1638 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
1639 bdevname(bio->bi_bdev, b),
1640 bio_sectors(bio),
1641 queue_max_hw_sectors(q));
1642 goto end_io;
1643 }
1644
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001645 part = bio->bi_bdev->bd_part;
1646 if (should_fail_request(part, bio->bi_size) ||
1647 should_fail_request(&part_to_disk(part)->part0,
1648 bio->bi_size))
1649 goto end_io;
1650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 /*
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001652 * If this device has partitions, remap block n
1653 * of partition p to block n+start(p) of the disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001655 blk_partition_remap(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001657 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio))
1658 goto end_io;
1659
1660 if (bio_check_eod(bio, nr_sectors))
1661 goto end_io;
1662
1663 /*
1664 * Filter flush bio's early so that make_request based
1665 * drivers without flush support don't have to worry
1666 * about them.
1667 */
1668 if ((bio->bi_rw & (REQ_FLUSH | REQ_FUA)) && !q->flush_flags) {
1669 bio->bi_rw &= ~(REQ_FLUSH | REQ_FUA);
1670 if (!nr_sectors) {
1671 err = 0;
Tejun Heoa7384672008-11-28 13:32:03 +09001672 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 }
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001676 if ((bio->bi_rw & REQ_DISCARD) &&
1677 (!blk_queue_discard(q) ||
1678 ((bio->bi_rw & REQ_SECURE) &&
1679 !blk_queue_secdiscard(q)))) {
1680 err = -EOPNOTSUPP;
1681 goto end_io;
1682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Tejun Heobc16a4f2011-10-19 14:33:01 +02001684 if (blk_throtl_bio(q, bio))
1685 return false; /* throttled, will be resubmitted later */
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001686
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001687 trace_block_bio_queue(q, bio);
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001688 return true;
Tejun Heoa7384672008-11-28 13:32:03 +09001689
1690end_io:
1691 bio_endio(bio, err);
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001692 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693}
1694
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001695/**
1696 * generic_make_request - hand a buffer to its device driver for I/O
1697 * @bio: The bio describing the location in memory and on the device.
1698 *
1699 * generic_make_request() is used to make I/O requests of block
1700 * devices. It is passed a &struct bio, which describes the I/O that needs
1701 * to be done.
1702 *
1703 * generic_make_request() does not return any status. The
1704 * success/failure status of the request, along with notification of
1705 * completion, is delivered asynchronously through the bio->bi_end_io
1706 * function described (one day) else where.
1707 *
1708 * The caller of generic_make_request must make sure that bi_io_vec
1709 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1710 * set to describe the device address, and the
1711 * bi_end_io and optionally bi_private are set to describe how
1712 * completion notification should be signaled.
1713 *
1714 * generic_make_request and the drivers it calls may use bi_next if this
1715 * bio happens to be merged with someone else, and may resubmit the bio to
1716 * a lower device by calling into generic_make_request recursively, which
1717 * means the bio should NOT be touched after the call to ->make_request_fn.
Neil Brownd89d8792007-05-01 09:53:42 +02001718 */
1719void generic_make_request(struct bio *bio)
1720{
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001721 struct bio_list bio_list_on_stack;
1722
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001723 if (!generic_make_request_checks(bio))
1724 return;
1725
1726 /*
1727 * We only want one ->make_request_fn to be active at a time, else
1728 * stack usage with stacked devices could be a problem. So use
1729 * current->bio_list to keep a list of requests submited by a
1730 * make_request_fn function. current->bio_list is also used as a
1731 * flag to say if generic_make_request is currently active in this
1732 * task or not. If it is NULL, then no make_request is active. If
1733 * it is non-NULL, then a make_request is active, and new requests
1734 * should be added at the tail
1735 */
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001736 if (current->bio_list) {
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001737 bio_list_add(current->bio_list, bio);
Neil Brownd89d8792007-05-01 09:53:42 +02001738 return;
1739 }
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001740
Neil Brownd89d8792007-05-01 09:53:42 +02001741 /* following loop may be a bit non-obvious, and so deserves some
1742 * explanation.
1743 * Before entering the loop, bio->bi_next is NULL (as all callers
1744 * ensure that) so we have a list with a single bio.
1745 * We pretend that we have just taken it off a longer list, so
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001746 * we assign bio_list to a pointer to the bio_list_on_stack,
1747 * thus initialising the bio_list of new bios to be
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001748 * added. ->make_request() may indeed add some more bios
Neil Brownd89d8792007-05-01 09:53:42 +02001749 * through a recursive call to generic_make_request. If it
1750 * did, we find a non-NULL value in bio_list and re-enter the loop
1751 * from the top. In this case we really did just take the bio
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001752 * of the top of the list (no pretending) and so remove it from
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001753 * bio_list, and call into ->make_request() again.
Neil Brownd89d8792007-05-01 09:53:42 +02001754 */
1755 BUG_ON(bio->bi_next);
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001756 bio_list_init(&bio_list_on_stack);
1757 current->bio_list = &bio_list_on_stack;
Neil Brownd89d8792007-05-01 09:53:42 +02001758 do {
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001759 struct request_queue *q = bdev_get_queue(bio->bi_bdev);
1760
1761 q->make_request_fn(q, bio);
1762
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001763 bio = bio_list_pop(current->bio_list);
Neil Brownd89d8792007-05-01 09:53:42 +02001764 } while (bio);
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001765 current->bio_list = NULL; /* deactivate */
Neil Brownd89d8792007-05-01 09:53:42 +02001766}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767EXPORT_SYMBOL(generic_make_request);
1768
1769/**
Randy Dunlap710027a2008-08-19 20:13:11 +02001770 * submit_bio - submit a bio to the block device layer for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1772 * @bio: The &struct bio which describes the I/O
1773 *
1774 * submit_bio() is very similar in purpose to generic_make_request(), and
1775 * uses that function to do most of the work. Both are fairly rough
Randy Dunlap710027a2008-08-19 20:13:11 +02001776 * interfaces; @bio must be presetup and ready for I/O.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 *
1778 */
1779void submit_bio(int rw, struct bio *bio)
1780{
1781 int count = bio_sectors(bio);
1782
Jens Axboe22e2c502005-06-27 10:55:12 +02001783 bio->bi_rw |= rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Jens Axboebf2de6f2007-09-27 13:01:25 +02001785 /*
1786 * If it's a regular read/write or a barrier with data attached,
1787 * go through the normal accounting stuff before submission.
1788 */
Jens Axboe3ffb52e2010-06-29 13:33:38 +02001789 if (bio_has_data(bio) && !(rw & REQ_DISCARD)) {
Jens Axboebf2de6f2007-09-27 13:01:25 +02001790 if (rw & WRITE) {
1791 count_vm_events(PGPGOUT, count);
1792 } else {
1793 task_io_account_read(bio->bi_size);
1794 count_vm_events(PGPGIN, count);
1795 }
1796
1797 if (unlikely(block_dump)) {
1798 char b[BDEVNAME_SIZE];
San Mehat8dcbdc72010-09-14 08:48:01 +02001799 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s (%u sectors)\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001800 current->comm, task_pid_nr(current),
Jens Axboebf2de6f2007-09-27 13:01:25 +02001801 (rw & WRITE) ? "WRITE" : "READ",
1802 (unsigned long long)bio->bi_sector,
San Mehat8dcbdc72010-09-14 08:48:01 +02001803 bdevname(bio->bi_bdev, b),
1804 count);
Jens Axboebf2de6f2007-09-27 13:01:25 +02001805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 }
1807
1808 generic_make_request(bio);
1809}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810EXPORT_SYMBOL(submit_bio);
1811
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001812/**
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001813 * blk_rq_check_limits - Helper function to check a request for the queue limit
1814 * @q: the queue
1815 * @rq: the request being checked
1816 *
1817 * Description:
1818 * @rq may have been made based on weaker limitations of upper-level queues
1819 * in request stacking drivers, and it may violate the limitation of @q.
1820 * Since the block layer and the underlying device driver trust @rq
1821 * after it is inserted to @q, it should be checked against @q before
1822 * the insertion using this generic function.
1823 *
1824 * This function should also be useful for request stacking drivers
Stefan Weileef35c22010-08-06 21:11:15 +02001825 * in some cases below, so export this function.
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001826 * Request stacking drivers like request-based dm may change the queue
1827 * limits while requests are in the queue (e.g. dm's table swapping).
1828 * Such request stacking drivers should check those requests agaist
1829 * the new queue limits again when they dispatch those requests,
1830 * although such checkings are also done against the old queue limits
1831 * when submitting requests.
1832 */
1833int blk_rq_check_limits(struct request_queue *q, struct request *rq)
1834{
ike Snitzer33839772010-08-08 12:11:33 -04001835 if (rq->cmd_flags & REQ_DISCARD)
1836 return 0;
1837
Martin K. Petersenae03bf62009-05-22 17:17:50 -04001838 if (blk_rq_sectors(rq) > queue_max_sectors(q) ||
1839 blk_rq_bytes(rq) > queue_max_hw_sectors(q) << 9) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001840 printk(KERN_ERR "%s: over max size limit.\n", __func__);
1841 return -EIO;
1842 }
1843
1844 /*
1845 * queue's settings related to segment counting like q->bounce_pfn
1846 * may differ from that of other stacking queues.
1847 * Recalculate it to check the request correctly on this queue's
1848 * limitation.
1849 */
1850 blk_recalc_rq_segments(rq);
Martin K. Petersen8a783622010-02-26 00:20:39 -05001851 if (rq->nr_phys_segments > queue_max_segments(q)) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001852 printk(KERN_ERR "%s: over max segments limit.\n", __func__);
1853 return -EIO;
1854 }
1855
1856 return 0;
1857}
1858EXPORT_SYMBOL_GPL(blk_rq_check_limits);
1859
1860/**
1861 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
1862 * @q: the queue to submit the request
1863 * @rq: the request being queued
1864 */
1865int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
1866{
1867 unsigned long flags;
Jeff Moyer4853aba2011-08-15 21:37:25 +02001868 int where = ELEVATOR_INSERT_BACK;
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001869
1870 if (blk_rq_check_limits(q, rq))
1871 return -EIO;
1872
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001873 if (rq->rq_disk &&
1874 should_fail_request(&rq->rq_disk->part0, blk_rq_bytes(rq)))
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001875 return -EIO;
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001876
1877 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo8ba61432011-12-14 00:33:37 +01001878 if (unlikely(blk_queue_dead(q))) {
1879 spin_unlock_irqrestore(q->queue_lock, flags);
1880 return -ENODEV;
1881 }
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001882
1883 /*
1884 * Submitting request must be dequeued before calling this function
1885 * because it will be linked to another request_queue
1886 */
1887 BUG_ON(blk_queued_rq(rq));
1888
Jeff Moyer4853aba2011-08-15 21:37:25 +02001889 if (rq->cmd_flags & (REQ_FLUSH|REQ_FUA))
1890 where = ELEVATOR_INSERT_FLUSH;
1891
1892 add_acct_request(q, rq, where);
Jeff Moyere67b77c2011-10-17 12:57:23 +02001893 if (where == ELEVATOR_INSERT_FLUSH)
1894 __blk_run_queue(q);
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04001895 spin_unlock_irqrestore(q->queue_lock, flags);
1896
1897 return 0;
1898}
1899EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
1900
Tejun Heo80a761f2009-07-03 17:48:17 +09001901/**
1902 * blk_rq_err_bytes - determine number of bytes till the next failure boundary
1903 * @rq: request to examine
1904 *
1905 * Description:
1906 * A request could be merge of IOs which require different failure
1907 * handling. This function determines the number of bytes which
1908 * can be failed from the beginning of the request without
1909 * crossing into area which need to be retried further.
1910 *
1911 * Return:
1912 * The number of bytes to fail.
1913 *
1914 * Context:
1915 * queue_lock must be held.
1916 */
1917unsigned int blk_rq_err_bytes(const struct request *rq)
1918{
1919 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
1920 unsigned int bytes = 0;
1921 struct bio *bio;
1922
1923 if (!(rq->cmd_flags & REQ_MIXED_MERGE))
1924 return blk_rq_bytes(rq);
1925
1926 /*
1927 * Currently the only 'mixing' which can happen is between
1928 * different fastfail types. We can safely fail portions
1929 * which have all the failfast bits that the first one has -
1930 * the ones which are at least as eager to fail as the first
1931 * one.
1932 */
1933 for (bio = rq->bio; bio; bio = bio->bi_next) {
1934 if ((bio->bi_rw & ff) != ff)
1935 break;
1936 bytes += bio->bi_size;
1937 }
1938
1939 /* this could lead to infinite loop */
1940 BUG_ON(blk_rq_bytes(rq) && !bytes);
1941 return bytes;
1942}
1943EXPORT_SYMBOL_GPL(blk_rq_err_bytes);
1944
Jens Axboebc58ba92009-01-23 10:54:44 +01001945static void blk_account_io_completion(struct request *req, unsigned int bytes)
1946{
Jens Axboec2553b52009-04-24 08:10:11 +02001947 if (blk_do_io_stat(req)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01001948 const int rw = rq_data_dir(req);
1949 struct hd_struct *part;
1950 int cpu;
1951
1952 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +01001953 part = req->part;
Jens Axboebc58ba92009-01-23 10:54:44 +01001954 part_stat_add(cpu, part, sectors[rw], bytes >> 9);
1955 part_stat_unlock();
1956 }
1957}
1958
1959static void blk_account_io_done(struct request *req)
1960{
Jens Axboebc58ba92009-01-23 10:54:44 +01001961 /*
Tejun Heodd4c1332010-09-03 11:56:16 +02001962 * Account IO completion. flush_rq isn't accounted as a
1963 * normal IO on queueing nor completion. Accounting the
1964 * containing request is enough.
Jens Axboebc58ba92009-01-23 10:54:44 +01001965 */
Tejun Heo414b4ff2011-01-25 12:43:49 +01001966 if (blk_do_io_stat(req) && !(req->cmd_flags & REQ_FLUSH_SEQ)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01001967 unsigned long duration = jiffies - req->start_time;
1968 const int rw = rq_data_dir(req);
1969 struct hd_struct *part;
1970 int cpu;
1971
1972 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +01001973 part = req->part;
Jens Axboebc58ba92009-01-23 10:54:44 +01001974
1975 part_stat_inc(cpu, part, ios[rw]);
1976 part_stat_add(cpu, part, ticks[rw], duration);
1977 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02001978 part_dec_in_flight(part, rw);
Jens Axboebc58ba92009-01-23 10:54:44 +01001979
Jens Axboe6c23a962011-01-07 08:43:37 +01001980 hd_struct_put(part);
Jens Axboebc58ba92009-01-23 10:54:44 +01001981 part_stat_unlock();
1982 }
1983}
1984
Tejun Heo53a08802008-12-03 12:41:26 +01001985/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09001986 * blk_peek_request - peek at the top of a request queue
1987 * @q: request queue to peek at
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001988 *
1989 * Description:
Tejun Heo9934c8c2009-05-08 11:54:16 +09001990 * Return the request at the top of @q. The returned request
1991 * should be started using blk_start_request() before LLD starts
1992 * processing it.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001993 *
1994 * Return:
Tejun Heo9934c8c2009-05-08 11:54:16 +09001995 * Pointer to the request at the top of @q if available. Null
1996 * otherwise.
1997 *
1998 * Context:
1999 * queue_lock must be held.
2000 */
2001struct request *blk_peek_request(struct request_queue *q)
Tejun Heo158dbda2009-04-23 11:05:18 +09002002{
2003 struct request *rq;
2004 int ret;
2005
2006 while ((rq = __elv_next_request(q)) != NULL) {
2007 if (!(rq->cmd_flags & REQ_STARTED)) {
2008 /*
2009 * This is the first time the device driver
2010 * sees this request (possibly after
2011 * requeueing). Notify IO scheduler.
2012 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002013 if (rq->cmd_flags & REQ_SORTED)
Tejun Heo158dbda2009-04-23 11:05:18 +09002014 elv_activate_rq(q, rq);
2015
2016 /*
2017 * just mark as started even if we don't start
2018 * it, a request that has been delayed should
2019 * not be passed by new incoming requests
2020 */
2021 rq->cmd_flags |= REQ_STARTED;
2022 trace_block_rq_issue(q, rq);
2023 }
2024
2025 if (!q->boundary_rq || q->boundary_rq == rq) {
2026 q->end_sector = rq_end_sector(rq);
2027 q->boundary_rq = NULL;
2028 }
2029
2030 if (rq->cmd_flags & REQ_DONTPREP)
2031 break;
2032
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002033 if (q->dma_drain_size && blk_rq_bytes(rq)) {
Tejun Heo158dbda2009-04-23 11:05:18 +09002034 /*
2035 * make sure space for the drain appears we
2036 * know we can do this because max_hw_segments
2037 * has been adjusted to be one fewer than the
2038 * device can handle
2039 */
2040 rq->nr_phys_segments++;
2041 }
2042
2043 if (!q->prep_rq_fn)
2044 break;
2045
2046 ret = q->prep_rq_fn(q, rq);
2047 if (ret == BLKPREP_OK) {
2048 break;
2049 } else if (ret == BLKPREP_DEFER) {
2050 /*
2051 * the request may have been (partially) prepped.
2052 * we need to keep this request in the front to
2053 * avoid resource deadlock. REQ_STARTED will
2054 * prevent other fs requests from passing this one.
2055 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002056 if (q->dma_drain_size && blk_rq_bytes(rq) &&
Tejun Heo158dbda2009-04-23 11:05:18 +09002057 !(rq->cmd_flags & REQ_DONTPREP)) {
2058 /*
2059 * remove the space for the drain we added
2060 * so that we don't add it again
2061 */
2062 --rq->nr_phys_segments;
2063 }
2064
2065 rq = NULL;
2066 break;
2067 } else if (ret == BLKPREP_KILL) {
2068 rq->cmd_flags |= REQ_QUIET;
James Bottomleyc143dc92009-05-30 06:43:49 +02002069 /*
2070 * Mark this request as started so we don't trigger
2071 * any debug logic in the end I/O path.
2072 */
2073 blk_start_request(rq);
Tejun Heo40cbbb72009-04-23 11:05:19 +09002074 __blk_end_request_all(rq, -EIO);
Tejun Heo158dbda2009-04-23 11:05:18 +09002075 } else {
2076 printk(KERN_ERR "%s: bad return=%d\n", __func__, ret);
2077 break;
2078 }
2079 }
2080
2081 return rq;
2082}
Tejun Heo9934c8c2009-05-08 11:54:16 +09002083EXPORT_SYMBOL(blk_peek_request);
Tejun Heo158dbda2009-04-23 11:05:18 +09002084
Tejun Heo9934c8c2009-05-08 11:54:16 +09002085void blk_dequeue_request(struct request *rq)
Tejun Heo158dbda2009-04-23 11:05:18 +09002086{
Tejun Heo9934c8c2009-05-08 11:54:16 +09002087 struct request_queue *q = rq->q;
2088
Tejun Heo158dbda2009-04-23 11:05:18 +09002089 BUG_ON(list_empty(&rq->queuelist));
2090 BUG_ON(ELV_ON_HASH(rq));
2091
2092 list_del_init(&rq->queuelist);
2093
2094 /*
2095 * the time frame between a request being removed from the lists
2096 * and to it is freed is accounted as io that is in progress at
2097 * the driver side.
2098 */
Divyesh Shah91952912010-04-01 15:01:41 -07002099 if (blk_account_rq(rq)) {
Jens Axboe0a7ae2f2009-05-20 08:54:31 +02002100 q->in_flight[rq_is_sync(rq)]++;
Divyesh Shah91952912010-04-01 15:01:41 -07002101 set_io_start_time_ns(rq);
2102 }
Tejun Heo158dbda2009-04-23 11:05:18 +09002103}
2104
Tejun Heo5efccd12009-04-23 11:05:18 +09002105/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09002106 * blk_start_request - start request processing on the driver
2107 * @req: request to dequeue
2108 *
2109 * Description:
2110 * Dequeue @req and start timeout timer on it. This hands off the
2111 * request to the driver.
2112 *
2113 * Block internal functions which don't want to start timer should
2114 * call blk_dequeue_request().
2115 *
2116 * Context:
2117 * queue_lock must be held.
2118 */
2119void blk_start_request(struct request *req)
2120{
2121 blk_dequeue_request(req);
2122
2123 /*
Tejun Heo5f49f632009-05-19 18:33:05 +09002124 * We are now handing the request to the hardware, initialize
2125 * resid_len to full count and add the timeout handler.
Tejun Heo9934c8c2009-05-08 11:54:16 +09002126 */
Tejun Heo5f49f632009-05-19 18:33:05 +09002127 req->resid_len = blk_rq_bytes(req);
FUJITA Tomonoridbb66c42009-06-09 05:47:10 +02002128 if (unlikely(blk_bidi_rq(req)))
2129 req->next_rq->resid_len = blk_rq_bytes(req->next_rq);
2130
Tejun Heo9934c8c2009-05-08 11:54:16 +09002131 blk_add_timer(req);
2132}
2133EXPORT_SYMBOL(blk_start_request);
2134
2135/**
2136 * blk_fetch_request - fetch a request from a request queue
2137 * @q: request queue to fetch a request from
2138 *
2139 * Description:
2140 * Return the request at the top of @q. The request is started on
2141 * return and LLD can start processing it immediately.
2142 *
2143 * Return:
2144 * Pointer to the request at the top of @q if available. Null
2145 * otherwise.
2146 *
2147 * Context:
2148 * queue_lock must be held.
2149 */
2150struct request *blk_fetch_request(struct request_queue *q)
2151{
2152 struct request *rq;
2153
2154 rq = blk_peek_request(q);
2155 if (rq)
2156 blk_start_request(rq);
2157 return rq;
2158}
2159EXPORT_SYMBOL(blk_fetch_request);
2160
2161/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002162 * blk_update_request - Special helper function for request stacking drivers
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002163 * @req: the request being processed
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002164 * @error: %0 for success, < %0 for error
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002165 * @nr_bytes: number of bytes to complete @req
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002166 *
2167 * Description:
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002168 * Ends I/O on a number of bytes attached to @req, but doesn't complete
2169 * the request structure even if @req doesn't have leftover.
2170 * If @req has leftover, sets it up for the next range of segments.
Tejun Heo2e60e022009-04-23 11:05:18 +09002171 *
2172 * This special helper function is only for request stacking drivers
2173 * (e.g. request-based dm) so that they can handle partial completion.
2174 * Actual device drivers should use blk_end_request instead.
2175 *
2176 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
2177 * %false return from this function.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002178 *
2179 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002180 * %false - this request doesn't have any more data
2181 * %true - this request has more data
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002182 **/
Tejun Heo2e60e022009-04-23 11:05:18 +09002183bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184{
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05002185 int total_bytes, bio_nbytes, next_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 struct bio *bio;
2187
Tejun Heo2e60e022009-04-23 11:05:18 +09002188 if (!req->bio)
2189 return false;
2190
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01002191 trace_block_rq_complete(req->q, req);
Jens Axboe2056a782006-03-23 20:00:26 +01002192
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 /*
Tejun Heo6f414692009-04-19 07:00:41 +09002194 * For fs requests, rq is just carrier of independent bio's
2195 * and each partial completion should be handled separately.
2196 * Reset per-request error on each partial completion.
2197 *
2198 * TODO: tj: This is too subtle. It would be better to let
2199 * low level drivers do what they see fit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002201 if (req->cmd_type == REQ_TYPE_FS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 req->errors = 0;
2203
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002204 if (error && req->cmd_type == REQ_TYPE_FS &&
2205 !(req->cmd_flags & REQ_QUIET)) {
Hannes Reinecke79775562011-01-18 10:13:13 +01002206 char *error_type;
2207
2208 switch (error) {
2209 case -ENOLINK:
2210 error_type = "recoverable transport";
2211 break;
2212 case -EREMOTEIO:
2213 error_type = "critical target";
2214 break;
2215 case -EBADE:
2216 error_type = "critical nexus";
2217 break;
2218 case -EIO:
2219 default:
2220 error_type = "I/O";
2221 break;
2222 }
2223 printk(KERN_ERR "end_request: %s error, dev %s, sector %llu\n",
2224 error_type, req->rq_disk ? req->rq_disk->disk_name : "?",
2225 (unsigned long long)blk_rq_pos(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 }
2227
Jens Axboebc58ba92009-01-23 10:54:44 +01002228 blk_account_io_completion(req, nr_bytes);
Jens Axboed72d9042005-11-01 08:35:42 +01002229
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 total_bytes = bio_nbytes = 0;
2231 while ((bio = req->bio) != NULL) {
2232 int nbytes;
2233
2234 if (nr_bytes >= bio->bi_size) {
2235 req->bio = bio->bi_next;
2236 nbytes = bio->bi_size;
NeilBrown5bb23a62007-09-27 12:46:13 +02002237 req_bio_endio(req, bio, nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 next_idx = 0;
2239 bio_nbytes = 0;
2240 } else {
2241 int idx = bio->bi_idx + next_idx;
2242
Kazuhisa Ichikawaaf498d72009-05-12 13:27:45 +02002243 if (unlikely(idx >= bio->bi_vcnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 blk_dump_rq_flags(req, "__end_that");
Jens Axboe6728cb02008-01-31 13:03:55 +01002245 printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
Kazuhisa Ichikawaaf498d72009-05-12 13:27:45 +02002246 __func__, idx, bio->bi_vcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 break;
2248 }
2249
2250 nbytes = bio_iovec_idx(bio, idx)->bv_len;
2251 BIO_BUG_ON(nbytes > bio->bi_size);
2252
2253 /*
2254 * not a complete bvec done
2255 */
2256 if (unlikely(nbytes > nr_bytes)) {
2257 bio_nbytes += nr_bytes;
2258 total_bytes += nr_bytes;
2259 break;
2260 }
2261
2262 /*
2263 * advance to the next vector
2264 */
2265 next_idx++;
2266 bio_nbytes += nbytes;
2267 }
2268
2269 total_bytes += nbytes;
2270 nr_bytes -= nbytes;
2271
Jens Axboe6728cb02008-01-31 13:03:55 +01002272 bio = req->bio;
2273 if (bio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 /*
2275 * end more in this run, or just return 'not-done'
2276 */
2277 if (unlikely(nr_bytes <= 0))
2278 break;
2279 }
2280 }
2281
2282 /*
2283 * completely done
2284 */
Tejun Heo2e60e022009-04-23 11:05:18 +09002285 if (!req->bio) {
2286 /*
2287 * Reset counters so that the request stacking driver
2288 * can find how many bytes remain in the request
2289 * later.
2290 */
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002291 req->__data_len = 0;
Tejun Heo2e60e022009-04-23 11:05:18 +09002292 return false;
2293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
2295 /*
2296 * if the request wasn't completed, update state
2297 */
2298 if (bio_nbytes) {
NeilBrown5bb23a62007-09-27 12:46:13 +02002299 req_bio_endio(req, bio, bio_nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 bio->bi_idx += next_idx;
2301 bio_iovec(bio)->bv_offset += nr_bytes;
2302 bio_iovec(bio)->bv_len -= nr_bytes;
2303 }
2304
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002305 req->__data_len -= total_bytes;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002306 req->buffer = bio_data(req->bio);
2307
2308 /* update sector only for requests with clear definition of sector */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002309 if (req->cmd_type == REQ_TYPE_FS || (req->cmd_flags & REQ_DISCARD))
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002310 req->__sector += total_bytes >> 9;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002311
Tejun Heo80a761f2009-07-03 17:48:17 +09002312 /* mixed attributes always follow the first bio */
2313 if (req->cmd_flags & REQ_MIXED_MERGE) {
2314 req->cmd_flags &= ~REQ_FAILFAST_MASK;
2315 req->cmd_flags |= req->bio->bi_rw & REQ_FAILFAST_MASK;
2316 }
2317
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002318 /*
2319 * If total number of sectors is less than the first segment
2320 * size, something has gone terribly wrong.
2321 */
2322 if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
Jens Axboe81829242011-03-30 09:51:33 +02002323 blk_dump_rq_flags(req, "request botched");
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002324 req->__data_len = blk_rq_cur_bytes(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002325 }
2326
2327 /* recalculate the number of segments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 blk_recalc_rq_segments(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002329
Tejun Heo2e60e022009-04-23 11:05:18 +09002330 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331}
Tejun Heo2e60e022009-04-23 11:05:18 +09002332EXPORT_SYMBOL_GPL(blk_update_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
Tejun Heo2e60e022009-04-23 11:05:18 +09002334static bool blk_update_bidi_request(struct request *rq, int error,
2335 unsigned int nr_bytes,
2336 unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002337{
Tejun Heo2e60e022009-04-23 11:05:18 +09002338 if (blk_update_request(rq, error, nr_bytes))
2339 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002340
Tejun Heo2e60e022009-04-23 11:05:18 +09002341 /* Bidi request must be completed as a whole */
2342 if (unlikely(blk_bidi_rq(rq)) &&
2343 blk_update_request(rq->next_rq, error, bidi_bytes))
2344 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002345
Jens Axboee2e1a142010-06-09 10:42:09 +02002346 if (blk_queue_add_random(rq->q))
2347 add_disk_randomness(rq->rq_disk);
Tejun Heo2e60e022009-04-23 11:05:18 +09002348
2349 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350}
2351
James Bottomley28018c22010-07-01 19:49:17 +09002352/**
2353 * blk_unprep_request - unprepare a request
2354 * @req: the request
2355 *
2356 * This function makes a request ready for complete resubmission (or
2357 * completion). It happens only after all error handling is complete,
2358 * so represents the appropriate moment to deallocate any resources
2359 * that were allocated to the request in the prep_rq_fn. The queue
2360 * lock is held when calling this.
2361 */
2362void blk_unprep_request(struct request *req)
2363{
2364 struct request_queue *q = req->q;
2365
2366 req->cmd_flags &= ~REQ_DONTPREP;
2367 if (q->unprep_rq_fn)
2368 q->unprep_rq_fn(q, req);
2369}
2370EXPORT_SYMBOL_GPL(blk_unprep_request);
2371
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372/*
2373 * queue lock must be held
2374 */
Tejun Heo2e60e022009-04-23 11:05:18 +09002375static void blk_finish_request(struct request *req, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376{
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002377 if (blk_rq_tagged(req))
2378 blk_queue_end_tag(req->q, req);
2379
James Bottomleyba396a62009-05-27 14:17:08 +02002380 BUG_ON(blk_queued_rq(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002382 if (unlikely(laptop_mode) && req->cmd_type == REQ_TYPE_FS)
Matthew Garrett31373d02010-04-06 14:25:14 +02002383 laptop_io_completion(&req->q->backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
Mike Andersone78042e2008-10-30 02:16:20 -07002385 blk_delete_timer(req);
2386
James Bottomley28018c22010-07-01 19:49:17 +09002387 if (req->cmd_flags & REQ_DONTPREP)
2388 blk_unprep_request(req);
2389
2390
Jens Axboebc58ba92009-01-23 10:54:44 +01002391 blk_account_io_done(req);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002392
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 if (req->end_io)
Tejun Heo8ffdc652006-01-06 09:49:03 +01002394 req->end_io(req, error);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002395 else {
2396 if (blk_bidi_rq(req))
2397 __blk_put_request(req->next_rq->q, req->next_rq);
2398
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 __blk_put_request(req->q, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 }
2401}
2402
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05002403/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002404 * blk_end_bidi_request - Complete a bidi request
2405 * @rq: the request to complete
Randy Dunlap710027a2008-08-19 20:13:11 +02002406 * @error: %0 for success, < %0 for error
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002407 * @nr_bytes: number of bytes to complete @rq
2408 * @bidi_bytes: number of bytes to complete @rq->next_rq
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002409 *
2410 * Description:
2411 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
Tejun Heo2e60e022009-04-23 11:05:18 +09002412 * Drivers that supports bidi can safely call this member for any
2413 * type of request, bidi or uni. In the later case @bidi_bytes is
2414 * just ignored.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002415 *
2416 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002417 * %false - we are done with this request
2418 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002419 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002420static bool blk_end_bidi_request(struct request *rq, int error,
2421 unsigned int nr_bytes, unsigned int bidi_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002422{
2423 struct request_queue *q = rq->q;
Tejun Heo2e60e022009-04-23 11:05:18 +09002424 unsigned long flags;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002425
Tejun Heo2e60e022009-04-23 11:05:18 +09002426 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2427 return true;
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002428
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002429 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo2e60e022009-04-23 11:05:18 +09002430 blk_finish_request(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002431 spin_unlock_irqrestore(q->queue_lock, flags);
2432
Tejun Heo2e60e022009-04-23 11:05:18 +09002433 return false;
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002434}
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002435
2436/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002437 * __blk_end_bidi_request - Complete a bidi request with queue lock held
2438 * @rq: the request to complete
2439 * @error: %0 for success, < %0 for error
2440 * @nr_bytes: number of bytes to complete @rq
2441 * @bidi_bytes: number of bytes to complete @rq->next_rq
Tejun Heo5efccd12009-04-23 11:05:18 +09002442 *
2443 * Description:
Tejun Heo2e60e022009-04-23 11:05:18 +09002444 * Identical to blk_end_bidi_request() except that queue lock is
2445 * assumed to be locked on entry and remains so on return.
Tejun Heo5efccd12009-04-23 11:05:18 +09002446 *
Tejun Heo2e60e022009-04-23 11:05:18 +09002447 * Return:
2448 * %false - we are done with this request
2449 * %true - still buffers pending for this request
Tejun Heo5efccd12009-04-23 11:05:18 +09002450 **/
Jeff Moyer4853aba2011-08-15 21:37:25 +02002451bool __blk_end_bidi_request(struct request *rq, int error,
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002452 unsigned int nr_bytes, unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002453{
Tejun Heo2e60e022009-04-23 11:05:18 +09002454 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2455 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002456
Tejun Heo2e60e022009-04-23 11:05:18 +09002457 blk_finish_request(rq, error);
Tejun Heo5efccd12009-04-23 11:05:18 +09002458
Tejun Heo2e60e022009-04-23 11:05:18 +09002459 return false;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002460}
2461
2462/**
2463 * blk_end_request - Helper function for drivers to complete the request.
2464 * @rq: the request being processed
2465 * @error: %0 for success, < %0 for error
2466 * @nr_bytes: number of bytes to complete
2467 *
2468 * Description:
2469 * Ends I/O on a number of bytes attached to @rq.
2470 * If @rq has leftover, sets it up for the next range of segments.
2471 *
2472 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002473 * %false - we are done with this request
2474 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002475 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002476bool blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002477{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002478 return blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002479}
Jens Axboe56ad1742009-07-28 22:11:24 +02002480EXPORT_SYMBOL(blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002481
2482/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002483 * blk_end_request_all - Helper function for drives to finish the request.
2484 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002485 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002486 *
2487 * Description:
2488 * Completely finish @rq.
2489 */
2490void blk_end_request_all(struct request *rq, int error)
2491{
2492 bool pending;
2493 unsigned int bidi_bytes = 0;
2494
2495 if (unlikely(blk_bidi_rq(rq)))
2496 bidi_bytes = blk_rq_bytes(rq->next_rq);
2497
2498 pending = blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2499 BUG_ON(pending);
2500}
Jens Axboe56ad1742009-07-28 22:11:24 +02002501EXPORT_SYMBOL(blk_end_request_all);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002502
2503/**
2504 * blk_end_request_cur - Helper function to finish the current request chunk.
2505 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002506 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002507 *
2508 * Description:
2509 * Complete the current consecutively mapped chunk from @rq.
2510 *
2511 * Return:
2512 * %false - we are done with this request
2513 * %true - still buffers pending for this request
2514 */
2515bool blk_end_request_cur(struct request *rq, int error)
2516{
2517 return blk_end_request(rq, error, blk_rq_cur_bytes(rq));
2518}
Jens Axboe56ad1742009-07-28 22:11:24 +02002519EXPORT_SYMBOL(blk_end_request_cur);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002520
2521/**
Tejun Heo80a761f2009-07-03 17:48:17 +09002522 * blk_end_request_err - Finish a request till the next failure boundary.
2523 * @rq: the request to finish till the next failure boundary for
2524 * @error: must be negative errno
2525 *
2526 * Description:
2527 * Complete @rq till the next failure boundary.
2528 *
2529 * Return:
2530 * %false - we are done with this request
2531 * %true - still buffers pending for this request
2532 */
2533bool blk_end_request_err(struct request *rq, int error)
2534{
2535 WARN_ON(error >= 0);
2536 return blk_end_request(rq, error, blk_rq_err_bytes(rq));
2537}
2538EXPORT_SYMBOL_GPL(blk_end_request_err);
2539
2540/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002541 * __blk_end_request - Helper function for drivers to complete the request.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002542 * @rq: the request being processed
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002543 * @error: %0 for success, < %0 for error
2544 * @nr_bytes: number of bytes to complete
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002545 *
2546 * Description:
2547 * Must be called with queue lock held unlike blk_end_request().
2548 *
2549 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002550 * %false - we are done with this request
2551 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002552 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002553bool __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002554{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002555 return __blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002556}
Jens Axboe56ad1742009-07-28 22:11:24 +02002557EXPORT_SYMBOL(__blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002558
2559/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002560 * __blk_end_request_all - Helper function for drives to finish the request.
2561 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002562 * @error: %0 for success, < %0 for error
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002563 *
2564 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002565 * Completely finish @rq. Must be called with queue lock held.
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002566 */
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002567void __blk_end_request_all(struct request *rq, int error)
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002568{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002569 bool pending;
2570 unsigned int bidi_bytes = 0;
2571
2572 if (unlikely(blk_bidi_rq(rq)))
2573 bidi_bytes = blk_rq_bytes(rq->next_rq);
2574
2575 pending = __blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2576 BUG_ON(pending);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002577}
Jens Axboe56ad1742009-07-28 22:11:24 +02002578EXPORT_SYMBOL(__blk_end_request_all);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002579
2580/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002581 * __blk_end_request_cur - Helper function to finish the current request chunk.
2582 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002583 * @error: %0 for success, < %0 for error
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002584 *
2585 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002586 * Complete the current consecutively mapped chunk from @rq. Must
2587 * be called with queue lock held.
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002588 *
2589 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002590 * %false - we are done with this request
2591 * %true - still buffers pending for this request
2592 */
2593bool __blk_end_request_cur(struct request *rq, int error)
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002594{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002595 return __blk_end_request(rq, error, blk_rq_cur_bytes(rq));
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002596}
Jens Axboe56ad1742009-07-28 22:11:24 +02002597EXPORT_SYMBOL(__blk_end_request_cur);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002598
Tejun Heo80a761f2009-07-03 17:48:17 +09002599/**
2600 * __blk_end_request_err - Finish a request till the next failure boundary.
2601 * @rq: the request to finish till the next failure boundary for
2602 * @error: must be negative errno
2603 *
2604 * Description:
2605 * Complete @rq till the next failure boundary. Must be called
2606 * with queue lock held.
2607 *
2608 * Return:
2609 * %false - we are done with this request
2610 * %true - still buffers pending for this request
2611 */
2612bool __blk_end_request_err(struct request *rq, int error)
2613{
2614 WARN_ON(error >= 0);
2615 return __blk_end_request(rq, error, blk_rq_err_bytes(rq));
2616}
2617EXPORT_SYMBOL_GPL(__blk_end_request_err);
2618
Jens Axboe86db1e22008-01-29 14:53:40 +01002619void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2620 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621{
Tejun Heoa82afdf2009-07-03 17:48:16 +09002622 /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002623 rq->cmd_flags |= bio->bi_rw & REQ_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
David Woodhousefb2dce82008-08-05 18:01:53 +01002625 if (bio_has_data(bio)) {
2626 rq->nr_phys_segments = bio_phys_segments(q, bio);
David Woodhousefb2dce82008-08-05 18:01:53 +01002627 rq->buffer = bio_data(bio);
2628 }
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002629 rq->__data_len = bio->bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 rq->bio = rq->biotail = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
NeilBrown66846572007-08-16 13:31:28 +02002632 if (bio->bi_bdev)
2633 rq->rq_disk = bio->bi_bdev->bd_disk;
2634}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
Ilya Loginov2d4dc892009-11-26 09:16:19 +01002636#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
2637/**
2638 * rq_flush_dcache_pages - Helper function to flush all pages in a request
2639 * @rq: the request to be flushed
2640 *
2641 * Description:
2642 * Flush all pages in @rq.
2643 */
2644void rq_flush_dcache_pages(struct request *rq)
2645{
2646 struct req_iterator iter;
2647 struct bio_vec *bvec;
2648
2649 rq_for_each_segment(bvec, rq, iter)
2650 flush_dcache_page(bvec->bv_page);
2651}
2652EXPORT_SYMBOL_GPL(rq_flush_dcache_pages);
2653#endif
2654
Kiyoshi Uedaef9e3fa2008-10-01 16:12:15 +02002655/**
2656 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
2657 * @q : the queue of the device being checked
2658 *
2659 * Description:
2660 * Check if underlying low-level drivers of a device are busy.
2661 * If the drivers want to export their busy state, they must set own
2662 * exporting function using blk_queue_lld_busy() first.
2663 *
2664 * Basically, this function is used only by request stacking drivers
2665 * to stop dispatching requests to underlying devices when underlying
2666 * devices are busy. This behavior helps more I/O merging on the queue
2667 * of the request stacking driver and prevents I/O throughput regression
2668 * on burst I/O load.
2669 *
2670 * Return:
2671 * 0 - Not busy (The request stacking driver should dispatch request)
2672 * 1 - Busy (The request stacking driver should stop dispatching request)
2673 */
2674int blk_lld_busy(struct request_queue *q)
2675{
2676 if (q->lld_busy_fn)
2677 return q->lld_busy_fn(q);
2678
2679 return 0;
2680}
2681EXPORT_SYMBOL_GPL(blk_lld_busy);
2682
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002683/**
2684 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
2685 * @rq: the clone request to be cleaned up
2686 *
2687 * Description:
2688 * Free all bios in @rq for a cloned request.
2689 */
2690void blk_rq_unprep_clone(struct request *rq)
2691{
2692 struct bio *bio;
2693
2694 while ((bio = rq->bio) != NULL) {
2695 rq->bio = bio->bi_next;
2696
2697 bio_put(bio);
2698 }
2699}
2700EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
2701
2702/*
2703 * Copy attributes of the original request to the clone request.
2704 * The actual data parts (e.g. ->cmd, ->buffer, ->sense) are not copied.
2705 */
2706static void __blk_rq_prep_clone(struct request *dst, struct request *src)
2707{
2708 dst->cpu = src->cpu;
Tejun Heo3a2edd02010-09-03 11:56:18 +02002709 dst->cmd_flags = (src->cmd_flags & REQ_CLONE_MASK) | REQ_NOMERGE;
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002710 dst->cmd_type = src->cmd_type;
2711 dst->__sector = blk_rq_pos(src);
2712 dst->__data_len = blk_rq_bytes(src);
2713 dst->nr_phys_segments = src->nr_phys_segments;
2714 dst->ioprio = src->ioprio;
2715 dst->extra_len = src->extra_len;
2716}
2717
2718/**
2719 * blk_rq_prep_clone - Helper function to setup clone request
2720 * @rq: the request to be setup
2721 * @rq_src: original request to be cloned
2722 * @bs: bio_set that bios for clone are allocated from
2723 * @gfp_mask: memory allocation mask for bio
2724 * @bio_ctr: setup function to be called for each clone bio.
2725 * Returns %0 for success, non %0 for failure.
2726 * @data: private data to be passed to @bio_ctr
2727 *
2728 * Description:
2729 * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
2730 * The actual data parts of @rq_src (e.g. ->cmd, ->buffer, ->sense)
2731 * are not copied, and copying such parts is the caller's responsibility.
2732 * Also, pages which the original bios are pointing to are not copied
2733 * and the cloned bios just point same pages.
2734 * So cloned bios must be completed before original bios, which means
2735 * the caller must complete @rq before @rq_src.
2736 */
2737int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
2738 struct bio_set *bs, gfp_t gfp_mask,
2739 int (*bio_ctr)(struct bio *, struct bio *, void *),
2740 void *data)
2741{
2742 struct bio *bio, *bio_src;
2743
2744 if (!bs)
2745 bs = fs_bio_set;
2746
2747 blk_rq_init(NULL, rq);
2748
2749 __rq_for_each_bio(bio_src, rq_src) {
2750 bio = bio_alloc_bioset(gfp_mask, bio_src->bi_max_vecs, bs);
2751 if (!bio)
2752 goto free_and_out;
2753
2754 __bio_clone(bio, bio_src);
2755
2756 if (bio_integrity(bio_src) &&
Martin K. Petersen7878cba2009-06-26 15:37:49 +02002757 bio_integrity_clone(bio, bio_src, gfp_mask, bs))
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002758 goto free_and_out;
2759
2760 if (bio_ctr && bio_ctr(bio, bio_src, data))
2761 goto free_and_out;
2762
2763 if (rq->bio) {
2764 rq->biotail->bi_next = bio;
2765 rq->biotail = bio;
2766 } else
2767 rq->bio = rq->biotail = bio;
2768 }
2769
2770 __blk_rq_prep_clone(rq, rq_src);
2771
2772 return 0;
2773
2774free_and_out:
2775 if (bio)
2776 bio_free(bio, bs);
2777 blk_rq_unprep_clone(rq);
2778
2779 return -ENOMEM;
2780}
2781EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
2782
Jens Axboe18887ad2008-07-28 13:08:45 +02002783int kblockd_schedule_work(struct request_queue *q, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784{
2785 return queue_work(kblockd_workqueue, work);
2786}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787EXPORT_SYMBOL(kblockd_schedule_work);
2788
Vivek Goyale43473b2010-09-15 17:06:35 -04002789int kblockd_schedule_delayed_work(struct request_queue *q,
2790 struct delayed_work *dwork, unsigned long delay)
2791{
2792 return queue_delayed_work(kblockd_workqueue, dwork, delay);
2793}
2794EXPORT_SYMBOL(kblockd_schedule_delayed_work);
2795
Jens Axboe73c10102011-03-08 13:19:51 +01002796#define PLUG_MAGIC 0x91827364
2797
Suresh Jayaraman75df7132011-09-21 10:00:16 +02002798/**
2799 * blk_start_plug - initialize blk_plug and track it inside the task_struct
2800 * @plug: The &struct blk_plug that needs to be initialized
2801 *
2802 * Description:
2803 * Tracking blk_plug inside the task_struct will help with auto-flushing the
2804 * pending I/O should the task end up blocking between blk_start_plug() and
2805 * blk_finish_plug(). This is important from a performance perspective, but
2806 * also ensures that we don't deadlock. For instance, if the task is blocking
2807 * for a memory allocation, memory reclaim could end up wanting to free a
2808 * page belonging to that request that is currently residing in our private
2809 * plug. By flushing the pending I/O when the process goes to sleep, we avoid
2810 * this kind of deadlock.
2811 */
Jens Axboe73c10102011-03-08 13:19:51 +01002812void blk_start_plug(struct blk_plug *plug)
2813{
2814 struct task_struct *tsk = current;
2815
2816 plug->magic = PLUG_MAGIC;
2817 INIT_LIST_HEAD(&plug->list);
NeilBrown048c9372011-04-18 09:52:22 +02002818 INIT_LIST_HEAD(&plug->cb_list);
Jens Axboe73c10102011-03-08 13:19:51 +01002819 plug->should_sort = 0;
2820
2821 /*
2822 * If this is a nested plug, don't actually assign it. It will be
2823 * flushed on its own.
2824 */
2825 if (!tsk->plug) {
2826 /*
2827 * Store ordering should not be needed here, since a potential
2828 * preempt will imply a full memory barrier
2829 */
2830 tsk->plug = plug;
2831 }
2832}
2833EXPORT_SYMBOL(blk_start_plug);
2834
2835static int plug_rq_cmp(void *priv, struct list_head *a, struct list_head *b)
2836{
2837 struct request *rqa = container_of(a, struct request, queuelist);
2838 struct request *rqb = container_of(b, struct request, queuelist);
2839
Konstantin Khlebnikovf83e8262011-04-04 00:15:02 +02002840 return !(rqa->q <= rqb->q);
Jens Axboe73c10102011-03-08 13:19:51 +01002841}
2842
Jens Axboe49cac012011-04-16 13:51:05 +02002843/*
2844 * If 'from_schedule' is true, then postpone the dispatch of requests
2845 * until a safe kblockd context. We due this to avoid accidental big
2846 * additional stack usage in driver dispatch, in places where the originally
2847 * plugger did not intend it.
2848 */
Jens Axboef6603782011-04-15 15:49:07 +02002849static void queue_unplugged(struct request_queue *q, unsigned int depth,
Jens Axboe49cac012011-04-16 13:51:05 +02002850 bool from_schedule)
Jens Axboe99e22592011-04-18 09:59:55 +02002851 __releases(q->queue_lock)
Jens Axboe94b5eb22011-04-12 10:12:19 +02002852{
Jens Axboe49cac012011-04-16 13:51:05 +02002853 trace_block_unplug(q, depth, !from_schedule);
Jens Axboe99e22592011-04-18 09:59:55 +02002854
2855 /*
Tejun Heo8ba61432011-12-14 00:33:37 +01002856 * Don't mess with dead queue.
2857 */
2858 if (unlikely(blk_queue_dead(q))) {
2859 spin_unlock(q->queue_lock);
2860 return;
2861 }
2862
2863 /*
Jens Axboe99e22592011-04-18 09:59:55 +02002864 * If we are punting this to kblockd, then we can safely drop
2865 * the queue_lock before waking kblockd (which needs to take
2866 * this lock).
2867 */
2868 if (from_schedule) {
2869 spin_unlock(q->queue_lock);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02002870 blk_run_queue_async(q);
Jens Axboe99e22592011-04-18 09:59:55 +02002871 } else {
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02002872 __blk_run_queue(q);
Jens Axboe99e22592011-04-18 09:59:55 +02002873 spin_unlock(q->queue_lock);
2874 }
2875
Jens Axboe94b5eb22011-04-12 10:12:19 +02002876}
2877
NeilBrown048c9372011-04-18 09:52:22 +02002878static void flush_plug_callbacks(struct blk_plug *plug)
2879{
2880 LIST_HEAD(callbacks);
2881
2882 if (list_empty(&plug->cb_list))
2883 return;
2884
2885 list_splice_init(&plug->cb_list, &callbacks);
2886
2887 while (!list_empty(&callbacks)) {
2888 struct blk_plug_cb *cb = list_first_entry(&callbacks,
2889 struct blk_plug_cb,
2890 list);
2891 list_del(&cb->list);
2892 cb->callback(cb);
2893 }
2894}
2895
Jens Axboe49cac012011-04-16 13:51:05 +02002896void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
Jens Axboe73c10102011-03-08 13:19:51 +01002897{
2898 struct request_queue *q;
2899 unsigned long flags;
2900 struct request *rq;
NeilBrown109b8122011-04-11 14:13:10 +02002901 LIST_HEAD(list);
Jens Axboe94b5eb22011-04-12 10:12:19 +02002902 unsigned int depth;
Jens Axboe73c10102011-03-08 13:19:51 +01002903
2904 BUG_ON(plug->magic != PLUG_MAGIC);
2905
NeilBrown048c9372011-04-18 09:52:22 +02002906 flush_plug_callbacks(plug);
Jens Axboe73c10102011-03-08 13:19:51 +01002907 if (list_empty(&plug->list))
2908 return;
2909
NeilBrown109b8122011-04-11 14:13:10 +02002910 list_splice_init(&plug->list, &list);
2911
2912 if (plug->should_sort) {
2913 list_sort(NULL, &list, plug_rq_cmp);
2914 plug->should_sort = 0;
2915 }
Jens Axboe73c10102011-03-08 13:19:51 +01002916
2917 q = NULL;
Jens Axboe94b5eb22011-04-12 10:12:19 +02002918 depth = 0;
Jens Axboe18811272011-04-12 10:11:24 +02002919
2920 /*
2921 * Save and disable interrupts here, to avoid doing it for every
2922 * queue lock we have to take.
2923 */
Jens Axboe73c10102011-03-08 13:19:51 +01002924 local_irq_save(flags);
NeilBrown109b8122011-04-11 14:13:10 +02002925 while (!list_empty(&list)) {
2926 rq = list_entry_rq(list.next);
Jens Axboe73c10102011-03-08 13:19:51 +01002927 list_del_init(&rq->queuelist);
Jens Axboe73c10102011-03-08 13:19:51 +01002928 BUG_ON(!rq->q);
2929 if (rq->q != q) {
Jens Axboe99e22592011-04-18 09:59:55 +02002930 /*
2931 * This drops the queue lock
2932 */
2933 if (q)
Jens Axboe49cac012011-04-16 13:51:05 +02002934 queue_unplugged(q, depth, from_schedule);
Jens Axboe73c10102011-03-08 13:19:51 +01002935 q = rq->q;
Jens Axboe94b5eb22011-04-12 10:12:19 +02002936 depth = 0;
Jens Axboe73c10102011-03-08 13:19:51 +01002937 spin_lock(q->queue_lock);
2938 }
Tejun Heo8ba61432011-12-14 00:33:37 +01002939
2940 /*
2941 * Short-circuit if @q is dead
2942 */
2943 if (unlikely(blk_queue_dead(q))) {
2944 __blk_end_request_all(rq, -ENODEV);
2945 continue;
2946 }
2947
Jens Axboe73c10102011-03-08 13:19:51 +01002948 /*
2949 * rq is already accounted, so use raw insert
2950 */
Jens Axboe401a18e2011-03-25 16:57:52 +01002951 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA))
2952 __elv_add_request(q, rq, ELEVATOR_INSERT_FLUSH);
2953 else
2954 __elv_add_request(q, rq, ELEVATOR_INSERT_SORT_MERGE);
Jens Axboe94b5eb22011-04-12 10:12:19 +02002955
2956 depth++;
Jens Axboe73c10102011-03-08 13:19:51 +01002957 }
2958
Jens Axboe99e22592011-04-18 09:59:55 +02002959 /*
2960 * This drops the queue lock
2961 */
2962 if (q)
Jens Axboe49cac012011-04-16 13:51:05 +02002963 queue_unplugged(q, depth, from_schedule);
Jens Axboe73c10102011-03-08 13:19:51 +01002964
Jens Axboe73c10102011-03-08 13:19:51 +01002965 local_irq_restore(flags);
2966}
Jens Axboe73c10102011-03-08 13:19:51 +01002967
2968void blk_finish_plug(struct blk_plug *plug)
2969{
Jens Axboef6603782011-04-15 15:49:07 +02002970 blk_flush_plug_list(plug, false);
Christoph Hellwig88b996c2011-04-15 15:20:10 +02002971
2972 if (plug == current->plug)
2973 current->plug = NULL;
Jens Axboe73c10102011-03-08 13:19:51 +01002974}
2975EXPORT_SYMBOL(blk_finish_plug);
2976
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977int __init blk_dev_init(void)
2978{
Nikanth Karthikesan9eb55b02009-04-27 14:53:54 +02002979 BUILD_BUG_ON(__REQ_NR_BITS > 8 *
2980 sizeof(((struct request *)0)->cmd_flags));
2981
Tejun Heo89b90be2011-01-03 15:01:47 +01002982 /* used for unplugging and affects IO latency/throughput - HIGHPRI */
2983 kblockd_workqueue = alloc_workqueue("kblockd",
2984 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 if (!kblockd_workqueue)
2986 panic("Failed to create kblockd\n");
2987
2988 request_cachep = kmem_cache_create("blkdev_requests",
Paul Mundt20c2df82007-07-20 10:11:58 +09002989 sizeof(struct request), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
Jens Axboe8324aa92008-01-29 14:51:59 +01002991 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02002992 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 return 0;
2995}