blkcg: make sure blkg_lookup() returns %NULL if @q is bypassing
[linux-2.6-block.git] / block / blk-core.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 * 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>
6728cb0e
JA
6 * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au>
7 * - July2000
1da177e4
LT
8 * 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 */
1da177e4
LT
14#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>
1da177e4
LT
24#include <linux/completion.h>
25#include <linux/slab.h>
26#include <linux/swap.h>
27#include <linux/writeback.h>
faccbd4b 28#include <linux/task_io_accounting_ops.h>
c17bb495 29#include <linux/fault-inject.h>
73c10101 30#include <linux/list_sort.h>
e3c78ca5 31#include <linux/delay.h>
55782138
LZ
32
33#define CREATE_TRACE_POINTS
34#include <trace/events/block.h>
1da177e4 35
8324aa91 36#include "blk.h"
5efd6113 37#include "blk-cgroup.h"
8324aa91 38
d07335e5 39EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_remap);
b0da3f0d 40EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_remap);
55782138 41EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_complete);
0bfc2455 42
a73f730d
TH
43DEFINE_IDA(blk_queue_ida);
44
1da177e4
LT
45/*
46 * For the allocated request tables
47 */
5ece6c52 48static struct kmem_cache *request_cachep;
1da177e4
LT
49
50/*
51 * For queue allocation
52 */
6728cb0e 53struct kmem_cache *blk_requestq_cachep;
1da177e4 54
1da177e4
LT
55/*
56 * Controlling structure to kblockd
57 */
ff856bad 58static struct workqueue_struct *kblockd_workqueue;
1da177e4 59
26b8256e
JA
60static void drive_stat_acct(struct request *rq, int new_io)
61{
28f13702 62 struct hd_struct *part;
26b8256e 63 int rw = rq_data_dir(rq);
c9959059 64 int cpu;
26b8256e 65
c2553b58 66 if (!blk_do_io_stat(rq))
26b8256e
JA
67 return;
68
074a7aca 69 cpu = part_stat_lock();
c9959059 70
09e099d4
JM
71 if (!new_io) {
72 part = rq->part;
074a7aca 73 part_stat_inc(cpu, part, merges[rw]);
09e099d4
JM
74 } else {
75 part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq));
6c23a968 76 if (!hd_struct_try_get(part)) {
09e099d4
JM
77 /*
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;
6c23a968 86 hd_struct_get(part);
09e099d4 87 }
074a7aca 88 part_round_stats(cpu, part);
316d315b 89 part_inc_in_flight(part, rw);
09e099d4 90 rq->part = part;
26b8256e 91 }
e71bf0d0 92
074a7aca 93 part_stat_unlock();
26b8256e
JA
94}
95
8324aa91 96void blk_queue_congestion_threshold(struct request_queue *q)
1da177e4
LT
97{
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
1da177e4
LT
111/**
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;
165125e1 123 struct request_queue *q = bdev_get_queue(bdev);
1da177e4
LT
124
125 if (q)
126 ret = &q->backing_dev_info;
127 return ret;
128}
1da177e4
LT
129EXPORT_SYMBOL(blk_get_backing_dev_info);
130
2a4aa30c 131void blk_rq_init(struct request_queue *q, struct request *rq)
1da177e4 132{
1afb20f3
FT
133 memset(rq, 0, sizeof(*rq));
134
1da177e4 135 INIT_LIST_HEAD(&rq->queuelist);
242f9dcb 136 INIT_LIST_HEAD(&rq->timeout_list);
c7c22e4d 137 rq->cpu = -1;
63a71386 138 rq->q = q;
a2dec7b3 139 rq->__sector = (sector_t) -1;
2e662b65
JA
140 INIT_HLIST_NODE(&rq->hash);
141 RB_CLEAR_NODE(&rq->rb_node);
d7e3c324 142 rq->cmd = rq->__cmd;
e2494e1b 143 rq->cmd_len = BLK_MAX_CDB;
63a71386 144 rq->tag = -1;
1da177e4 145 rq->ref_count = 1;
b243ddcb 146 rq->start_time = jiffies;
9195291e 147 set_start_time_ns(rq);
09e099d4 148 rq->part = NULL;
1da177e4 149}
2a4aa30c 150EXPORT_SYMBOL(blk_rq_init);
1da177e4 151
5bb23a68
N
152static void req_bio_endio(struct request *rq, struct bio *bio,
153 unsigned int nbytes, int error)
1da177e4 154{
143a87f4
TH
155 if (error)
156 clear_bit(BIO_UPTODATE, &bio->bi_flags);
157 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
158 error = -EIO;
797e7dbb 159
143a87f4
TH
160 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;
5bb23a68 164 }
797e7dbb 165
143a87f4
TH
166 if (unlikely(rq->cmd_flags & REQ_QUIET))
167 set_bit(BIO_QUIET, &bio->bi_flags);
08bafc03 168
143a87f4
TH
169 bio->bi_size -= nbytes;
170 bio->bi_sector += (nbytes >> 9);
7ba1ba12 171
143a87f4
TH
172 if (bio_integrity(bio))
173 bio_integrity_advance(bio, nbytes);
7ba1ba12 174
143a87f4
TH
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);
1da177e4 178}
1da177e4 179
1da177e4
LT
180void blk_dump_rq_flags(struct request *rq, char *msg)
181{
182 int bit;
183
6728cb0e 184 printk(KERN_INFO "%s: dev %s: type=%x, flags=%x\n", msg,
4aff5e23
JA
185 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
186 rq->cmd_flags);
1da177e4 187
83096ebf
TH
188 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));
731ec497 191 printk(KERN_INFO " bio %p, biotail %p, buffer %p, len %u\n",
2e46e8b2 192 rq->bio, rq->biotail, rq->buffer, blk_rq_bytes(rq));
1da177e4 193
33659ebb 194 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
6728cb0e 195 printk(KERN_INFO " cdb: ");
d34c87e4 196 for (bit = 0; bit < BLK_MAX_CDB; bit++)
1da177e4
LT
197 printk("%02x ", rq->cmd[bit]);
198 printk("\n");
199 }
200}
1da177e4
LT
201EXPORT_SYMBOL(blk_dump_rq_flags);
202
3cca6dc1 203static void blk_delay_work(struct work_struct *work)
1da177e4 204{
3cca6dc1 205 struct request_queue *q;
1da177e4 206
3cca6dc1
JA
207 q = container_of(work, struct request_queue, delay_work.work);
208 spin_lock_irq(q->queue_lock);
24ecfbe2 209 __blk_run_queue(q);
3cca6dc1 210 spin_unlock_irq(q->queue_lock);
1da177e4 211}
1da177e4
LT
212
213/**
3cca6dc1
JA
214 * blk_delay_queue - restart queueing after defined interval
215 * @q: The &struct request_queue in question
216 * @msecs: Delay in msecs
1da177e4
LT
217 *
218 * Description:
3cca6dc1
JA
219 * 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)
2ad8b1ef 224{
4521cc4e
JA
225 queue_delayed_work(kblockd_workqueue, &q->delay_work,
226 msecs_to_jiffies(msecs));
2ad8b1ef 227}
3cca6dc1 228EXPORT_SYMBOL(blk_delay_queue);
2ad8b1ef 229
1da177e4
LT
230/**
231 * blk_start_queue - restart a previously stopped queue
165125e1 232 * @q: The &struct request_queue in question
1da177e4
LT
233 *
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 **/
165125e1 239void blk_start_queue(struct request_queue *q)
1da177e4 240{
a038e253
PBG
241 WARN_ON(!irqs_disabled());
242
75ad23bc 243 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
24ecfbe2 244 __blk_run_queue(q);
1da177e4 245}
1da177e4
LT
246EXPORT_SYMBOL(blk_start_queue);
247
248/**
249 * blk_stop_queue - stop a queue
165125e1 250 * @q: The &struct request_queue in question
1da177e4
LT
251 *
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 **/
165125e1 262void blk_stop_queue(struct request_queue *q)
1da177e4 263{
ad3d9d7e 264 __cancel_delayed_work(&q->delay_work);
75ad23bc 265 queue_flag_set(QUEUE_FLAG_STOPPED, q);
1da177e4
LT
266}
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
59c51591 278 * that the callbacks might use. The caller must already have made sure
1da177e4
LT
279 * that its ->make_request_fn will not re-add plugging prior to calling
280 * this function.
281 *
da527770
VG
282 * This function does not cancel any asynchronous activity arising
283 * out of elevator or throttling code. That would require elevaotor_exit()
5efd6113 284 * and blkcg_exit_queue() to be called with queue lock initialized.
da527770 285 *
1da177e4
LT
286 */
287void blk_sync_queue(struct request_queue *q)
288{
70ed28b9 289 del_timer_sync(&q->timeout);
3cca6dc1 290 cancel_delayed_work_sync(&q->delay_work);
1da177e4
LT
291}
292EXPORT_SYMBOL(blk_sync_queue);
293
294/**
80a4b58e 295 * __blk_run_queue - run a single device queue
1da177e4 296 * @q: The queue to run
80a4b58e
JA
297 *
298 * Description:
299 * See @blk_run_queue. This variant must be called with the queue lock
24ecfbe2 300 * held and interrupts disabled.
1da177e4 301 */
24ecfbe2 302void __blk_run_queue(struct request_queue *q)
1da177e4 303{
a538cd03
TH
304 if (unlikely(blk_queue_stopped(q)))
305 return;
306
c21e6beb 307 q->request_fn(q);
75ad23bc
NP
308}
309EXPORT_SYMBOL(__blk_run_queue);
dac07ec1 310
24ecfbe2
CH
311/**
312 * 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{
3ec717b7
SL
321 if (likely(!blk_queue_stopped(q))) {
322 __cancel_delayed_work(&q->delay_work);
24ecfbe2 323 queue_delayed_work(kblockd_workqueue, &q->delay_work, 0);
3ec717b7 324 }
24ecfbe2 325}
c21e6beb 326EXPORT_SYMBOL(blk_run_queue_async);
24ecfbe2 327
75ad23bc
NP
328/**
329 * blk_run_queue - run a single device queue
330 * @q: The queue to run
80a4b58e
JA
331 *
332 * Description:
333 * Invoke request handling on this queue, if it has pending work to do.
a7f55792 334 * May be used to restart queueing when a request has completed.
75ad23bc
NP
335 */
336void blk_run_queue(struct request_queue *q)
337{
338 unsigned long flags;
339
340 spin_lock_irqsave(q->queue_lock, flags);
24ecfbe2 341 __blk_run_queue(q);
1da177e4
LT
342 spin_unlock_irqrestore(q->queue_lock, flags);
343}
344EXPORT_SYMBOL(blk_run_queue);
345
165125e1 346void blk_put_queue(struct request_queue *q)
483f4afc
AV
347{
348 kobject_put(&q->kobj);
349}
d86e0e83 350EXPORT_SYMBOL(blk_put_queue);
483f4afc 351
e3c78ca5
TH
352/**
353 * blk_drain_queue - drain requests from request_queue
354 * @q: queue to drain
c9a929dd 355 * @drain_all: whether to drain all requests or only the ones w/ ELVPRIV
e3c78ca5 356 *
c9a929dd
TH
357 * 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.
e3c78ca5 360 */
c9a929dd 361void blk_drain_queue(struct request_queue *q, bool drain_all)
e3c78ca5
TH
362{
363 while (true) {
481a7d64
TH
364 bool drain = false;
365 int i;
e3c78ca5
TH
366
367 spin_lock_irq(q->queue_lock);
368
b855b04a
TH
369 /*
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
5efd6113 376 blkcg_drain_queue(q);
e3c78ca5 377
4eabc941
TH
378 /*
379 * This function might be called on a queue which failed
b855b04a
TH
380 * 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.
4eabc941 384 */
b855b04a 385 if (!list_empty(&q->queue_head) && q->request_fn)
4eabc941 386 __blk_run_queue(q);
c9a929dd 387
481a7d64
TH
388 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 }
e3c78ca5
TH
403
404 spin_unlock_irq(q->queue_lock);
405
481a7d64 406 if (!drain)
e3c78ca5
TH
407 break;
408 msleep(10);
409 }
410}
411
d732580b
TH
412/**
413 * 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
6ecf23af 418 * throttled or issued before. On return, it's guaranteed that no request
80fd9979
TH
419 * is being throttled or has ELVPRIV set and blk_queue_bypass() %true
420 * inside queue or RCU read lock.
d732580b
TH
421 */
422void blk_queue_bypass_start(struct request_queue *q)
423{
424 spin_lock_irq(q->queue_lock);
425 q->bypass_depth++;
426 queue_flag_set(QUEUE_FLAG_BYPASS, q);
427 spin_unlock_irq(q->queue_lock);
428
429 blk_drain_queue(q, false);
80fd9979
TH
430 /* ensure blk_queue_bypass() is %true inside RCU read lock */
431 synchronize_rcu();
d732580b
TH
432}
433EXPORT_SYMBOL_GPL(blk_queue_bypass_start);
434
435/**
436 * blk_queue_bypass_end - leave queue bypass mode
437 * @q: queue of interest
438 *
439 * Leave bypass mode and restore the normal queueing behavior.
440 */
441void blk_queue_bypass_end(struct request_queue *q)
442{
443 spin_lock_irq(q->queue_lock);
444 if (!--q->bypass_depth)
445 queue_flag_clear(QUEUE_FLAG_BYPASS, q);
446 WARN_ON_ONCE(q->bypass_depth < 0);
447 spin_unlock_irq(q->queue_lock);
448}
449EXPORT_SYMBOL_GPL(blk_queue_bypass_end);
450
c9a929dd
TH
451/**
452 * blk_cleanup_queue - shutdown a request queue
453 * @q: request queue to shutdown
454 *
455 * Mark @q DEAD, drain all pending requests, destroy and put it. All
456 * future requests will be failed immediately with -ENODEV.
c94a96ac 457 */
6728cb0e 458void blk_cleanup_queue(struct request_queue *q)
483f4afc 459{
c9a929dd 460 spinlock_t *lock = q->queue_lock;
e3335de9 461
c9a929dd 462 /* mark @q DEAD, no new request or merges will be allowed afterwards */
483f4afc 463 mutex_lock(&q->sysfs_lock);
75ad23bc 464 queue_flag_set_unlocked(QUEUE_FLAG_DEAD, q);
c9a929dd
TH
465
466 spin_lock_irq(lock);
6ecf23af 467
80fd9979
TH
468 /*
469 * Dead queue is permanently in bypass mode till released. Note
470 * that, unlike blk_queue_bypass_start(), we aren't performing
471 * synchronize_rcu() after entering bypass mode to avoid the delay
472 * as some drivers create and destroy a lot of queues while
473 * probing. This is still safe because blk_release_queue() will be
474 * called only after the queue refcnt drops to zero and nothing,
475 * RCU or not, would be traversing the queue by then.
476 */
6ecf23af
TH
477 q->bypass_depth++;
478 queue_flag_set(QUEUE_FLAG_BYPASS, q);
479
c9a929dd
TH
480 queue_flag_set(QUEUE_FLAG_NOMERGES, q);
481 queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
482 queue_flag_set(QUEUE_FLAG_DEAD, q);
483f4afc 483
777eb1bf
HR
484 if (q->queue_lock != &q->__queue_lock)
485 q->queue_lock = &q->__queue_lock;
da527770 486
c9a929dd
TH
487 spin_unlock_irq(lock);
488 mutex_unlock(&q->sysfs_lock);
489
b855b04a
TH
490 /* drain all requests queued before DEAD marking */
491 blk_drain_queue(q, true);
c9a929dd
TH
492
493 /* @q won't process any more request, flush async actions */
494 del_timer_sync(&q->backing_dev_info.laptop_mode_wb_timer);
495 blk_sync_queue(q);
496
497 /* @q is and will stay empty, shutdown and put */
483f4afc
AV
498 blk_put_queue(q);
499}
1da177e4
LT
500EXPORT_SYMBOL(blk_cleanup_queue);
501
165125e1 502static int blk_init_free_list(struct request_queue *q)
1da177e4
LT
503{
504 struct request_list *rl = &q->rq;
505
1abec4fd
MS
506 if (unlikely(rl->rq_pool))
507 return 0;
508
1faa16d2
JA
509 rl->count[BLK_RW_SYNC] = rl->count[BLK_RW_ASYNC] = 0;
510 rl->starved[BLK_RW_SYNC] = rl->starved[BLK_RW_ASYNC] = 0;
cb98fc8b 511 rl->elvpriv = 0;
1faa16d2
JA
512 init_waitqueue_head(&rl->wait[BLK_RW_SYNC]);
513 init_waitqueue_head(&rl->wait[BLK_RW_ASYNC]);
1da177e4 514
1946089a
CL
515 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
516 mempool_free_slab, request_cachep, q->node);
1da177e4
LT
517
518 if (!rl->rq_pool)
519 return -ENOMEM;
520
521 return 0;
522}
523
165125e1 524struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
1da177e4 525{
1946089a
CL
526 return blk_alloc_queue_node(gfp_mask, -1);
527}
528EXPORT_SYMBOL(blk_alloc_queue);
1da177e4 529
165125e1 530struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
1946089a 531{
165125e1 532 struct request_queue *q;
e0bf68dd 533 int err;
1946089a 534
8324aa91 535 q = kmem_cache_alloc_node(blk_requestq_cachep,
94f6030c 536 gfp_mask | __GFP_ZERO, node_id);
1da177e4
LT
537 if (!q)
538 return NULL;
539
a73f730d
TH
540 q->id = ida_simple_get(&blk_queue_ida, 0, 0, GFP_KERNEL);
541 if (q->id < 0)
542 goto fail_q;
543
0989a025
JA
544 q->backing_dev_info.ra_pages =
545 (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
546 q->backing_dev_info.state = 0;
547 q->backing_dev_info.capabilities = BDI_CAP_MAP_COPY;
d993831f 548 q->backing_dev_info.name = "block";
5151412d 549 q->node = node_id;
0989a025 550
e0bf68dd 551 err = bdi_init(&q->backing_dev_info);
a73f730d
TH
552 if (err)
553 goto fail_id;
e0bf68dd 554
31373d09
MG
555 setup_timer(&q->backing_dev_info.laptop_mode_wb_timer,
556 laptop_mode_timer_fn, (unsigned long) q);
242f9dcb 557 setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
b855b04a 558 INIT_LIST_HEAD(&q->queue_head);
242f9dcb 559 INIT_LIST_HEAD(&q->timeout_list);
a612fddf 560 INIT_LIST_HEAD(&q->icq_list);
4eef3049 561#ifdef CONFIG_BLK_CGROUP
e8989fae 562 INIT_LIST_HEAD(&q->blkg_list);
4eef3049 563#endif
ae1b1539
TH
564 INIT_LIST_HEAD(&q->flush_queue[0]);
565 INIT_LIST_HEAD(&q->flush_queue[1]);
566 INIT_LIST_HEAD(&q->flush_data_in_flight);
3cca6dc1 567 INIT_DELAYED_WORK(&q->delay_work, blk_delay_work);
483f4afc 568
8324aa91 569 kobject_init(&q->kobj, &blk_queue_ktype);
1da177e4 570
483f4afc 571 mutex_init(&q->sysfs_lock);
e7e72bf6 572 spin_lock_init(&q->__queue_lock);
483f4afc 573
c94a96ac
VG
574 /*
575 * By default initialize queue_lock to internal lock and driver can
576 * override it later if need be.
577 */
578 q->queue_lock = &q->__queue_lock;
579
5efd6113 580 if (blkcg_init_queue(q))
f51b802c
TH
581 goto fail_id;
582
1da177e4 583 return q;
a73f730d
TH
584
585fail_id:
586 ida_simple_remove(&blk_queue_ida, q->id);
587fail_q:
588 kmem_cache_free(blk_requestq_cachep, q);
589 return NULL;
1da177e4 590}
1946089a 591EXPORT_SYMBOL(blk_alloc_queue_node);
1da177e4
LT
592
593/**
594 * blk_init_queue - prepare a request queue for use with a block device
595 * @rfn: The function to be called to process requests that have been
596 * placed on the queue.
597 * @lock: Request queue spin lock
598 *
599 * Description:
600 * If a block device wishes to use the standard request handling procedures,
601 * which sorts requests and coalesces adjacent requests, then it must
602 * call blk_init_queue(). The function @rfn will be called when there
603 * are requests on the queue that need to be processed. If the device
604 * supports plugging, then @rfn may not be called immediately when requests
605 * are available on the queue, but may be called at some time later instead.
606 * Plugged queues are generally unplugged when a buffer belonging to one
607 * of the requests on the queue is needed, or due to memory pressure.
608 *
609 * @rfn is not required, or even expected, to remove all requests off the
610 * queue, but only as many as it can handle at a time. If it does leave
611 * requests on the queue, it is responsible for arranging that the requests
612 * get dealt with eventually.
613 *
614 * The queue spin lock must be held while manipulating the requests on the
a038e253
PBG
615 * request queue; this lock will be taken also from interrupt context, so irq
616 * disabling is needed for it.
1da177e4 617 *
710027a4 618 * Function returns a pointer to the initialized request queue, or %NULL if
1da177e4
LT
619 * it didn't succeed.
620 *
621 * Note:
622 * blk_init_queue() must be paired with a blk_cleanup_queue() call
623 * when the block device is deactivated (such as at module unload).
624 **/
1946089a 625
165125e1 626struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
1da177e4 627{
1946089a
CL
628 return blk_init_queue_node(rfn, lock, -1);
629}
630EXPORT_SYMBOL(blk_init_queue);
631
165125e1 632struct request_queue *
1946089a
CL
633blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
634{
c86d1b8a 635 struct request_queue *uninit_q, *q;
1da177e4 636
c86d1b8a
MS
637 uninit_q = blk_alloc_queue_node(GFP_KERNEL, node_id);
638 if (!uninit_q)
639 return NULL;
640
5151412d 641 q = blk_init_allocated_queue(uninit_q, rfn, lock);
c86d1b8a
MS
642 if (!q)
643 blk_cleanup_queue(uninit_q);
644
645 return q;
01effb0d
MS
646}
647EXPORT_SYMBOL(blk_init_queue_node);
648
649struct request_queue *
650blk_init_allocated_queue(struct request_queue *q, request_fn_proc *rfn,
651 spinlock_t *lock)
01effb0d 652{
1da177e4
LT
653 if (!q)
654 return NULL;
655
c86d1b8a 656 if (blk_init_free_list(q))
8669aafd 657 return NULL;
1da177e4
LT
658
659 q->request_fn = rfn;
1da177e4 660 q->prep_rq_fn = NULL;
28018c24 661 q->unprep_rq_fn = NULL;
bc58ba94 662 q->queue_flags = QUEUE_FLAG_DEFAULT;
c94a96ac
VG
663
664 /* Override internal queue lock with supplied lock pointer */
665 if (lock)
666 q->queue_lock = lock;
1da177e4 667
f3b144aa
JA
668 /*
669 * This also sets hw/phys segments, boundary and size
670 */
c20e8de2 671 blk_queue_make_request(q, blk_queue_bio);
1da177e4 672
44ec9542
AS
673 q->sg_reserved_size = INT_MAX;
674
1da177e4
LT
675 /*
676 * all done
677 */
678 if (!elevator_init(q, NULL)) {
679 blk_queue_congestion_threshold(q);
680 return q;
681 }
682
1da177e4
LT
683 return NULL;
684}
5151412d 685EXPORT_SYMBOL(blk_init_allocated_queue);
1da177e4 686
09ac46c4 687bool blk_get_queue(struct request_queue *q)
1da177e4 688{
34f6055c 689 if (likely(!blk_queue_dead(q))) {
09ac46c4
TH
690 __blk_get_queue(q);
691 return true;
1da177e4
LT
692 }
693
09ac46c4 694 return false;
1da177e4 695}
d86e0e83 696EXPORT_SYMBOL(blk_get_queue);
1da177e4 697
165125e1 698static inline void blk_free_request(struct request_queue *q, struct request *rq)
1da177e4 699{
f1f8cc94 700 if (rq->cmd_flags & REQ_ELVPRIV) {
cb98fc8b 701 elv_put_request(q, rq);
f1f8cc94 702 if (rq->elv.icq)
11a3122f 703 put_io_context(rq->elv.icq->ioc);
f1f8cc94
TH
704 }
705
1da177e4
LT
706 mempool_free(rq, q->rq.rq_pool);
707}
708
1ea25ecb 709static struct request *
852c788f 710blk_alloc_request(struct request_queue *q, struct bio *bio, struct io_cq *icq,
f1f8cc94 711 unsigned int flags, gfp_t gfp_mask)
1da177e4
LT
712{
713 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
714
715 if (!rq)
716 return NULL;
717
2a4aa30c 718 blk_rq_init(q, rq);
1afb20f3 719
42dad764 720 rq->cmd_flags = flags | REQ_ALLOCED;
1da177e4 721
f1f8cc94
TH
722 if (flags & REQ_ELVPRIV) {
723 rq->elv.icq = icq;
852c788f 724 if (unlikely(elv_set_request(q, rq, bio, gfp_mask))) {
f1f8cc94
TH
725 mempool_free(rq, q->rq.rq_pool);
726 return NULL;
727 }
728 /* @rq->elv.icq holds on to io_context until @rq is freed */
729 if (icq)
730 get_io_context(icq->ioc);
cb98fc8b 731 }
1da177e4 732
cb98fc8b 733 return rq;
1da177e4
LT
734}
735
736/*
737 * ioc_batching returns true if the ioc is a valid batching request and
738 * should be given priority access to a request.
739 */
165125e1 740static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
1da177e4
LT
741{
742 if (!ioc)
743 return 0;
744
745 /*
746 * Make sure the process is able to allocate at least 1 request
747 * even if the batch times out, otherwise we could theoretically
748 * lose wakeups.
749 */
750 return ioc->nr_batch_requests == q->nr_batching ||
751 (ioc->nr_batch_requests > 0
752 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
753}
754
755/*
756 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
757 * will cause the process to be a "batcher" on all queues in the system. This
758 * is the behaviour we want though - once it gets a wakeup it should be given
759 * a nice run.
760 */
165125e1 761static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
1da177e4
LT
762{
763 if (!ioc || ioc_batching(q, ioc))
764 return;
765
766 ioc->nr_batch_requests = q->nr_batching;
767 ioc->last_waited = jiffies;
768}
769
1faa16d2 770static void __freed_request(struct request_queue *q, int sync)
1da177e4
LT
771{
772 struct request_list *rl = &q->rq;
773
1faa16d2
JA
774 if (rl->count[sync] < queue_congestion_off_threshold(q))
775 blk_clear_queue_congested(q, sync);
1da177e4 776
1faa16d2
JA
777 if (rl->count[sync] + 1 <= q->nr_requests) {
778 if (waitqueue_active(&rl->wait[sync]))
779 wake_up(&rl->wait[sync]);
1da177e4 780
1faa16d2 781 blk_clear_queue_full(q, sync);
1da177e4
LT
782 }
783}
784
785/*
786 * A request has just been released. Account for it, update the full and
787 * congestion status, wake up any waiters. Called under q->queue_lock.
788 */
75eb6c37 789static void freed_request(struct request_queue *q, unsigned int flags)
1da177e4
LT
790{
791 struct request_list *rl = &q->rq;
75eb6c37 792 int sync = rw_is_sync(flags);
1da177e4 793
1faa16d2 794 rl->count[sync]--;
75eb6c37 795 if (flags & REQ_ELVPRIV)
cb98fc8b 796 rl->elvpriv--;
1da177e4 797
1faa16d2 798 __freed_request(q, sync);
1da177e4 799
1faa16d2
JA
800 if (unlikely(rl->starved[sync ^ 1]))
801 __freed_request(q, sync ^ 1);
1da177e4
LT
802}
803
9d5a4e94
MS
804/*
805 * Determine if elevator data should be initialized when allocating the
806 * request associated with @bio.
807 */
808static bool blk_rq_should_init_elevator(struct bio *bio)
809{
810 if (!bio)
811 return true;
812
813 /*
814 * Flush requests do not use the elevator so skip initialization.
815 * This allows a request to share the flush and elevator data.
816 */
817 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA))
818 return false;
819
820 return true;
821}
822
852c788f
TH
823/**
824 * rq_ioc - determine io_context for request allocation
825 * @bio: request being allocated is for this bio (can be %NULL)
826 *
827 * Determine io_context to use for request allocation for @bio. May return
828 * %NULL if %current->io_context doesn't exist.
829 */
830static struct io_context *rq_ioc(struct bio *bio)
831{
832#ifdef CONFIG_BLK_CGROUP
833 if (bio && bio->bi_ioc)
834 return bio->bi_ioc;
835#endif
836 return current->io_context;
837}
838
da8303c6
TH
839/**
840 * get_request - get a free request
841 * @q: request_queue to allocate request from
842 * @rw_flags: RW and SYNC flags
843 * @bio: bio to allocate request for (can be %NULL)
844 * @gfp_mask: allocation mask
845 *
846 * Get a free request from @q. This function may fail under memory
847 * pressure or if @q is dead.
848 *
849 * Must be callled with @q->queue_lock held and,
850 * Returns %NULL on failure, with @q->queue_lock held.
851 * Returns !%NULL on success, with @q->queue_lock *not held*.
1da177e4 852 */
165125e1 853static struct request *get_request(struct request_queue *q, int rw_flags,
7749a8d4 854 struct bio *bio, gfp_t gfp_mask)
1da177e4 855{
b679281a 856 struct request *rq;
1da177e4 857 struct request_list *rl = &q->rq;
f1f8cc94 858 struct elevator_type *et;
f2dbd76a 859 struct io_context *ioc;
f1f8cc94 860 struct io_cq *icq = NULL;
1faa16d2 861 const bool is_sync = rw_is_sync(rw_flags) != 0;
f2dbd76a 862 bool retried = false;
75eb6c37 863 int may_queue;
f2dbd76a 864retry:
f1f8cc94 865 et = q->elevator->type;
852c788f 866 ioc = rq_ioc(bio);
88ee5ef1 867
34f6055c 868 if (unlikely(blk_queue_dead(q)))
da8303c6
TH
869 return NULL;
870
7749a8d4 871 may_queue = elv_may_queue(q, rw_flags);
88ee5ef1
JA
872 if (may_queue == ELV_MQUEUE_NO)
873 goto rq_starved;
874
1faa16d2
JA
875 if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) {
876 if (rl->count[is_sync]+1 >= q->nr_requests) {
f2dbd76a
TH
877 /*
878 * We want ioc to record batching state. If it's
879 * not already there, creating a new one requires
880 * dropping queue_lock, which in turn requires
881 * retesting conditions to avoid queue hang.
882 */
883 if (!ioc && !retried) {
884 spin_unlock_irq(q->queue_lock);
24acfc34 885 create_io_context(gfp_mask, q->node);
f2dbd76a
TH
886 spin_lock_irq(q->queue_lock);
887 retried = true;
888 goto retry;
889 }
890
88ee5ef1
JA
891 /*
892 * The queue will fill after this allocation, so set
893 * it as full, and mark this process as "batching".
894 * This process will be allowed to complete a batch of
895 * requests, others will be blocked.
896 */
1faa16d2 897 if (!blk_queue_full(q, is_sync)) {
88ee5ef1 898 ioc_set_batching(q, ioc);
1faa16d2 899 blk_set_queue_full(q, is_sync);
88ee5ef1
JA
900 } else {
901 if (may_queue != ELV_MQUEUE_MUST
902 && !ioc_batching(q, ioc)) {
903 /*
904 * The queue is full and the allocating
905 * process is not a "batcher", and not
906 * exempted by the IO scheduler
907 */
b679281a 908 return NULL;
88ee5ef1
JA
909 }
910 }
1da177e4 911 }
1faa16d2 912 blk_set_queue_congested(q, is_sync);
1da177e4
LT
913 }
914
082cf69e
JA
915 /*
916 * Only allow batching queuers to allocate up to 50% over the defined
917 * limit of requests, otherwise we could have thousands of requests
918 * allocated with any setting of ->nr_requests
919 */
1faa16d2 920 if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
b679281a 921 return NULL;
fd782a4a 922
1faa16d2
JA
923 rl->count[is_sync]++;
924 rl->starved[is_sync] = 0;
cb98fc8b 925
f1f8cc94
TH
926 /*
927 * Decide whether the new request will be managed by elevator. If
928 * so, mark @rw_flags and increment elvpriv. Non-zero elvpriv will
929 * prevent the current elevator from being destroyed until the new
930 * request is freed. This guarantees icq's won't be destroyed and
931 * makes creating new ones safe.
932 *
933 * Also, lookup icq while holding queue_lock. If it doesn't exist,
934 * it will be created after releasing queue_lock.
935 */
d732580b 936 if (blk_rq_should_init_elevator(bio) && !blk_queue_bypass(q)) {
75eb6c37
TH
937 rw_flags |= REQ_ELVPRIV;
938 rl->elvpriv++;
f1f8cc94
TH
939 if (et->icq_cache && ioc)
940 icq = ioc_lookup_icq(ioc, q);
9d5a4e94 941 }
cb98fc8b 942
f253b86b
JA
943 if (blk_queue_io_stat(q))
944 rw_flags |= REQ_IO_STAT;
1da177e4
LT
945 spin_unlock_irq(q->queue_lock);
946
f1f8cc94 947 /* create icq if missing */
05c30b95 948 if ((rw_flags & REQ_ELVPRIV) && unlikely(et->icq_cache && !icq)) {
852c788f
TH
949 create_io_context(gfp_mask, q->node);
950 ioc = rq_ioc(bio);
951 if (!ioc)
952 goto fail_alloc;
953 icq = ioc_create_icq(ioc, q, gfp_mask);
05c30b95 954 if (!icq)
b679281a 955 goto fail_alloc;
05c30b95 956 }
f1f8cc94 957
852c788f 958 rq = blk_alloc_request(q, bio, icq, rw_flags, gfp_mask);
b679281a
TH
959 if (unlikely(!rq))
960 goto fail_alloc;
1da177e4 961
88ee5ef1
JA
962 /*
963 * ioc may be NULL here, and ioc_batching will be false. That's
964 * OK, if the queue is under the request limit then requests need
965 * not count toward the nr_batch_requests limit. There will always
966 * be some limit enforced by BLK_BATCH_TIME.
967 */
1da177e4
LT
968 if (ioc_batching(q, ioc))
969 ioc->nr_batch_requests--;
6728cb0e 970
1faa16d2 971 trace_block_getrq(q, bio, rw_flags & 1);
1da177e4 972 return rq;
b679281a
TH
973
974fail_alloc:
975 /*
976 * Allocation failed presumably due to memory. Undo anything we
977 * might have messed up.
978 *
979 * Allocating task should really be put onto the front of the wait
980 * queue, but this is pretty rare.
981 */
982 spin_lock_irq(q->queue_lock);
983 freed_request(q, rw_flags);
984
985 /*
986 * in the very unlikely event that allocation failed and no
987 * requests for this direction was pending, mark us starved so that
988 * freeing of a request in the other direction will notice
989 * us. another possible fix would be to split the rq mempool into
990 * READ and WRITE
991 */
992rq_starved:
993 if (unlikely(rl->count[is_sync] == 0))
994 rl->starved[is_sync] = 1;
995 return NULL;
1da177e4
LT
996}
997
da8303c6
TH
998/**
999 * get_request_wait - get a free request with retry
1000 * @q: request_queue to allocate request from
1001 * @rw_flags: RW and SYNC flags
1002 * @bio: bio to allocate request for (can be %NULL)
1003 *
1004 * Get a free request from @q. This function keeps retrying under memory
1005 * pressure and fails iff @q is dead.
d6344532 1006 *
da8303c6
TH
1007 * Must be callled with @q->queue_lock held and,
1008 * Returns %NULL on failure, with @q->queue_lock held.
1009 * Returns !%NULL on success, with @q->queue_lock *not held*.
1da177e4 1010 */
165125e1 1011static struct request *get_request_wait(struct request_queue *q, int rw_flags,
22e2c507 1012 struct bio *bio)
1da177e4 1013{
1faa16d2 1014 const bool is_sync = rw_is_sync(rw_flags) != 0;
1da177e4
LT
1015 struct request *rq;
1016
7749a8d4 1017 rq = get_request(q, rw_flags, bio, GFP_NOIO);
450991bc
NP
1018 while (!rq) {
1019 DEFINE_WAIT(wait);
1da177e4
LT
1020 struct request_list *rl = &q->rq;
1021
34f6055c 1022 if (unlikely(blk_queue_dead(q)))
da8303c6
TH
1023 return NULL;
1024
1faa16d2 1025 prepare_to_wait_exclusive(&rl->wait[is_sync], &wait,
1da177e4
LT
1026 TASK_UNINTERRUPTIBLE);
1027
1faa16d2 1028 trace_block_sleeprq(q, bio, rw_flags & 1);
1da177e4 1029
05caf8db
ZY
1030 spin_unlock_irq(q->queue_lock);
1031 io_schedule();
1da177e4 1032
05caf8db
ZY
1033 /*
1034 * After sleeping, we become a "batching" process and
1035 * will be able to allocate at least one request, and
1036 * up to a big batch of them for a small period time.
1037 * See ioc_batching, ioc_set_batching
1038 */
24acfc34 1039 create_io_context(GFP_NOIO, q->node);
f2dbd76a 1040 ioc_set_batching(q, current->io_context);
d6344532 1041
05caf8db 1042 spin_lock_irq(q->queue_lock);
1faa16d2 1043 finish_wait(&rl->wait[is_sync], &wait);
05caf8db
ZY
1044
1045 rq = get_request(q, rw_flags, bio, GFP_NOIO);
1046 };
1da177e4
LT
1047
1048 return rq;
1049}
1050
165125e1 1051struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
1da177e4
LT
1052{
1053 struct request *rq;
1054
1055 BUG_ON(rw != READ && rw != WRITE);
1056
d6344532 1057 spin_lock_irq(q->queue_lock);
da8303c6 1058 if (gfp_mask & __GFP_WAIT)
22e2c507 1059 rq = get_request_wait(q, rw, NULL);
da8303c6 1060 else
22e2c507 1061 rq = get_request(q, rw, NULL, gfp_mask);
da8303c6
TH
1062 if (!rq)
1063 spin_unlock_irq(q->queue_lock);
d6344532 1064 /* q->queue_lock is unlocked at this point */
1da177e4
LT
1065
1066 return rq;
1067}
1da177e4
LT
1068EXPORT_SYMBOL(blk_get_request);
1069
dc72ef4a 1070/**
79eb63e9 1071 * blk_make_request - given a bio, allocate a corresponding struct request.
8ebf9756 1072 * @q: target request queue
79eb63e9
BH
1073 * @bio: The bio describing the memory mappings that will be submitted for IO.
1074 * It may be a chained-bio properly constructed by block/bio layer.
8ebf9756 1075 * @gfp_mask: gfp flags to be used for memory allocation
dc72ef4a 1076 *
79eb63e9
BH
1077 * blk_make_request is the parallel of generic_make_request for BLOCK_PC
1078 * type commands. Where the struct request needs to be farther initialized by
1079 * the caller. It is passed a &struct bio, which describes the memory info of
1080 * the I/O transfer.
dc72ef4a 1081 *
79eb63e9
BH
1082 * The caller of blk_make_request must make sure that bi_io_vec
1083 * are set to describe the memory buffers. That bio_data_dir() will return
1084 * the needed direction of the request. (And all bio's in the passed bio-chain
1085 * are properly set accordingly)
1086 *
1087 * If called under none-sleepable conditions, mapped bio buffers must not
1088 * need bouncing, by calling the appropriate masked or flagged allocator,
1089 * suitable for the target device. Otherwise the call to blk_queue_bounce will
1090 * BUG.
53674ac5
JA
1091 *
1092 * WARNING: When allocating/cloning a bio-chain, careful consideration should be
1093 * given to how you allocate bios. In particular, you cannot use __GFP_WAIT for
1094 * anything but the first bio in the chain. Otherwise you risk waiting for IO
1095 * completion of a bio that hasn't been submitted yet, thus resulting in a
1096 * deadlock. Alternatively bios should be allocated using bio_kmalloc() instead
1097 * of bio_alloc(), as that avoids the mempool deadlock.
1098 * If possible a big IO should be split into smaller parts when allocation
1099 * fails. Partial allocation should not be an error, or you risk a live-lock.
dc72ef4a 1100 */
79eb63e9
BH
1101struct request *blk_make_request(struct request_queue *q, struct bio *bio,
1102 gfp_t gfp_mask)
dc72ef4a 1103{
79eb63e9
BH
1104 struct request *rq = blk_get_request(q, bio_data_dir(bio), gfp_mask);
1105
1106 if (unlikely(!rq))
1107 return ERR_PTR(-ENOMEM);
1108
1109 for_each_bio(bio) {
1110 struct bio *bounce_bio = bio;
1111 int ret;
1112
1113 blk_queue_bounce(q, &bounce_bio);
1114 ret = blk_rq_append_bio(q, rq, bounce_bio);
1115 if (unlikely(ret)) {
1116 blk_put_request(rq);
1117 return ERR_PTR(ret);
1118 }
1119 }
1120
1121 return rq;
dc72ef4a 1122}
79eb63e9 1123EXPORT_SYMBOL(blk_make_request);
dc72ef4a 1124
1da177e4
LT
1125/**
1126 * blk_requeue_request - put a request back on queue
1127 * @q: request queue where request should be inserted
1128 * @rq: request to be inserted
1129 *
1130 * Description:
1131 * Drivers often keep queueing requests until the hardware cannot accept
1132 * more, when that condition happens we need to put the request back
1133 * on the queue. Must be called with queue lock held.
1134 */
165125e1 1135void blk_requeue_request(struct request_queue *q, struct request *rq)
1da177e4 1136{
242f9dcb
JA
1137 blk_delete_timer(rq);
1138 blk_clear_rq_complete(rq);
5f3ea37c 1139 trace_block_rq_requeue(q, rq);
2056a782 1140
1da177e4
LT
1141 if (blk_rq_tagged(rq))
1142 blk_queue_end_tag(q, rq);
1143
ba396a6c
JB
1144 BUG_ON(blk_queued_rq(rq));
1145
1da177e4
LT
1146 elv_requeue_request(q, rq);
1147}
1da177e4
LT
1148EXPORT_SYMBOL(blk_requeue_request);
1149
73c10101
JA
1150static void add_acct_request(struct request_queue *q, struct request *rq,
1151 int where)
1152{
1153 drive_stat_acct(rq, 1);
7eaceacc 1154 __elv_add_request(q, rq, where);
73c10101
JA
1155}
1156
074a7aca
TH
1157static void part_round_stats_single(int cpu, struct hd_struct *part,
1158 unsigned long now)
1159{
1160 if (now == part->stamp)
1161 return;
1162
316d315b 1163 if (part_in_flight(part)) {
074a7aca 1164 __part_stat_add(cpu, part, time_in_queue,
316d315b 1165 part_in_flight(part) * (now - part->stamp));
074a7aca
TH
1166 __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
1167 }
1168 part->stamp = now;
1169}
1170
1171/**
496aa8a9
RD
1172 * part_round_stats() - Round off the performance stats on a struct disk_stats.
1173 * @cpu: cpu number for stats access
1174 * @part: target partition
1da177e4
LT
1175 *
1176 * The average IO queue length and utilisation statistics are maintained
1177 * by observing the current state of the queue length and the amount of
1178 * time it has been in this state for.
1179 *
1180 * Normally, that accounting is done on IO completion, but that can result
1181 * in more than a second's worth of IO being accounted for within any one
1182 * second, leading to >100% utilisation. To deal with that, we call this
1183 * function to do a round-off before returning the results when reading
1184 * /proc/diskstats. This accounts immediately for all queue usage up to
1185 * the current jiffies and restarts the counters again.
1186 */
c9959059 1187void part_round_stats(int cpu, struct hd_struct *part)
6f2576af
JM
1188{
1189 unsigned long now = jiffies;
1190
074a7aca
TH
1191 if (part->partno)
1192 part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
1193 part_round_stats_single(cpu, part, now);
6f2576af 1194}
074a7aca 1195EXPORT_SYMBOL_GPL(part_round_stats);
6f2576af 1196
1da177e4
LT
1197/*
1198 * queue lock must be held
1199 */
165125e1 1200void __blk_put_request(struct request_queue *q, struct request *req)
1da177e4 1201{
1da177e4
LT
1202 if (unlikely(!q))
1203 return;
1204 if (unlikely(--req->ref_count))
1205 return;
1206
8922e16c
TH
1207 elv_completed_request(q, req);
1208
1cd96c24
BH
1209 /* this is a bio leak */
1210 WARN_ON(req->bio != NULL);
1211
1da177e4
LT
1212 /*
1213 * Request may not have originated from ll_rw_blk. if not,
1214 * it didn't come out of our reserved rq pools
1215 */
49171e5c 1216 if (req->cmd_flags & REQ_ALLOCED) {
75eb6c37 1217 unsigned int flags = req->cmd_flags;
1da177e4 1218
1da177e4 1219 BUG_ON(!list_empty(&req->queuelist));
9817064b 1220 BUG_ON(!hlist_unhashed(&req->hash));
1da177e4
LT
1221
1222 blk_free_request(q, req);
75eb6c37 1223 freed_request(q, flags);
1da177e4
LT
1224 }
1225}
6e39b69e
MC
1226EXPORT_SYMBOL_GPL(__blk_put_request);
1227
1da177e4
LT
1228void blk_put_request(struct request *req)
1229{
8922e16c 1230 unsigned long flags;
165125e1 1231 struct request_queue *q = req->q;
8922e16c 1232
52a93ba8
FT
1233 spin_lock_irqsave(q->queue_lock, flags);
1234 __blk_put_request(q, req);
1235 spin_unlock_irqrestore(q->queue_lock, flags);
1da177e4 1236}
1da177e4
LT
1237EXPORT_SYMBOL(blk_put_request);
1238
66ac0280
CH
1239/**
1240 * blk_add_request_payload - add a payload to a request
1241 * @rq: request to update
1242 * @page: page backing the payload
1243 * @len: length of the payload.
1244 *
1245 * This allows to later add a payload to an already submitted request by
1246 * a block driver. The driver needs to take care of freeing the payload
1247 * itself.
1248 *
1249 * Note that this is a quite horrible hack and nothing but handling of
1250 * discard requests should ever use it.
1251 */
1252void blk_add_request_payload(struct request *rq, struct page *page,
1253 unsigned int len)
1254{
1255 struct bio *bio = rq->bio;
1256
1257 bio->bi_io_vec->bv_page = page;
1258 bio->bi_io_vec->bv_offset = 0;
1259 bio->bi_io_vec->bv_len = len;
1260
1261 bio->bi_size = len;
1262 bio->bi_vcnt = 1;
1263 bio->bi_phys_segments = 1;
1264
1265 rq->__data_len = rq->resid_len = len;
1266 rq->nr_phys_segments = 1;
1267 rq->buffer = bio_data(bio);
1268}
1269EXPORT_SYMBOL_GPL(blk_add_request_payload);
1270
73c10101
JA
1271static bool bio_attempt_back_merge(struct request_queue *q, struct request *req,
1272 struct bio *bio)
1273{
1274 const int ff = bio->bi_rw & REQ_FAILFAST_MASK;
1275
73c10101
JA
1276 if (!ll_back_merge_fn(q, req, bio))
1277 return false;
1278
1279 trace_block_bio_backmerge(q, bio);
1280
1281 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1282 blk_rq_set_mixed_merge(req);
1283
1284 req->biotail->bi_next = bio;
1285 req->biotail = bio;
1286 req->__data_len += bio->bi_size;
1287 req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
1288
1289 drive_stat_acct(req, 0);
1290 return true;
1291}
1292
1293static bool bio_attempt_front_merge(struct request_queue *q,
1294 struct request *req, struct bio *bio)
1295{
1296 const int ff = bio->bi_rw & REQ_FAILFAST_MASK;
73c10101 1297
73c10101
JA
1298 if (!ll_front_merge_fn(q, req, bio))
1299 return false;
1300
1301 trace_block_bio_frontmerge(q, bio);
1302
1303 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1304 blk_rq_set_mixed_merge(req);
1305
73c10101
JA
1306 bio->bi_next = req->bio;
1307 req->bio = bio;
1308
1309 /*
1310 * may not be valid. if the low level driver said
1311 * it didn't need a bounce buffer then it better
1312 * not touch req->buffer either...
1313 */
1314 req->buffer = bio_data(bio);
1315 req->__sector = bio->bi_sector;
1316 req->__data_len += bio->bi_size;
1317 req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
1318
1319 drive_stat_acct(req, 0);
1320 return true;
1321}
1322
bd87b589
TH
1323/**
1324 * attempt_plug_merge - try to merge with %current's plugged list
1325 * @q: request_queue new bio is being queued at
1326 * @bio: new bio being queued
1327 * @request_count: out parameter for number of traversed plugged requests
1328 *
1329 * Determine whether @bio being queued on @q can be merged with a request
1330 * on %current's plugged list. Returns %true if merge was successful,
1331 * otherwise %false.
1332 *
07c2bd37
TH
1333 * Plugging coalesces IOs from the same issuer for the same purpose without
1334 * going through @q->queue_lock. As such it's more of an issuing mechanism
1335 * than scheduling, and the request, while may have elvpriv data, is not
1336 * added on the elevator at this point. In addition, we don't have
1337 * reliable access to the elevator outside queue lock. Only check basic
1338 * merging parameters without querying the elevator.
73c10101 1339 */
bd87b589
TH
1340static bool attempt_plug_merge(struct request_queue *q, struct bio *bio,
1341 unsigned int *request_count)
73c10101
JA
1342{
1343 struct blk_plug *plug;
1344 struct request *rq;
1345 bool ret = false;
1346
bd87b589 1347 plug = current->plug;
73c10101
JA
1348 if (!plug)
1349 goto out;
56ebdaf2 1350 *request_count = 0;
73c10101
JA
1351
1352 list_for_each_entry_reverse(rq, &plug->list, queuelist) {
1353 int el_ret;
1354
56ebdaf2
SL
1355 (*request_count)++;
1356
07c2bd37 1357 if (rq->q != q || !blk_rq_merge_ok(rq, bio))
73c10101
JA
1358 continue;
1359
050c8ea8 1360 el_ret = blk_try_merge(rq, bio);
73c10101
JA
1361 if (el_ret == ELEVATOR_BACK_MERGE) {
1362 ret = bio_attempt_back_merge(q, rq, bio);
1363 if (ret)
1364 break;
1365 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
1366 ret = bio_attempt_front_merge(q, rq, bio);
1367 if (ret)
1368 break;
1369 }
1370 }
1371out:
1372 return ret;
1373}
1374
86db1e29 1375void init_request_from_bio(struct request *req, struct bio *bio)
52d9e675 1376{
4aff5e23 1377 req->cmd_type = REQ_TYPE_FS;
52d9e675 1378
7b6d91da
CH
1379 req->cmd_flags |= bio->bi_rw & REQ_COMMON_MASK;
1380 if (bio->bi_rw & REQ_RAHEAD)
a82afdfc 1381 req->cmd_flags |= REQ_FAILFAST_MASK;
b31dc66a 1382
52d9e675 1383 req->errors = 0;
a2dec7b3 1384 req->__sector = bio->bi_sector;
52d9e675 1385 req->ioprio = bio_prio(bio);
bc1c56fd 1386 blk_rq_bio_prep(req->q, req, bio);
52d9e675
TH
1387}
1388
5a7bbad2 1389void blk_queue_bio(struct request_queue *q, struct bio *bio)
1da177e4 1390{
5e00d1b5 1391 const bool sync = !!(bio->bi_rw & REQ_SYNC);
73c10101
JA
1392 struct blk_plug *plug;
1393 int el_ret, rw_flags, where = ELEVATOR_INSERT_SORT;
1394 struct request *req;
56ebdaf2 1395 unsigned int request_count = 0;
1da177e4 1396
1da177e4
LT
1397 /*
1398 * low level driver can indicate that it wants pages above a
1399 * certain limit bounced to low memory (ie for highmem, or even
1400 * ISA dma in theory)
1401 */
1402 blk_queue_bounce(q, &bio);
1403
4fed947c 1404 if (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) {
73c10101 1405 spin_lock_irq(q->queue_lock);
ae1b1539 1406 where = ELEVATOR_INSERT_FLUSH;
28e7d184
TH
1407 goto get_rq;
1408 }
1409
73c10101
JA
1410 /*
1411 * Check if we can merge with the plugged list before grabbing
1412 * any locks.
1413 */
bd87b589 1414 if (attempt_plug_merge(q, bio, &request_count))
5a7bbad2 1415 return;
1da177e4 1416
73c10101 1417 spin_lock_irq(q->queue_lock);
2056a782 1418
73c10101
JA
1419 el_ret = elv_merge(q, &req, bio);
1420 if (el_ret == ELEVATOR_BACK_MERGE) {
73c10101 1421 if (bio_attempt_back_merge(q, req, bio)) {
07c2bd37 1422 elv_bio_merged(q, req, bio);
73c10101
JA
1423 if (!attempt_back_merge(q, req))
1424 elv_merged_request(q, req, el_ret);
1425 goto out_unlock;
1426 }
1427 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
73c10101 1428 if (bio_attempt_front_merge(q, req, bio)) {
07c2bd37 1429 elv_bio_merged(q, req, bio);
73c10101
JA
1430 if (!attempt_front_merge(q, req))
1431 elv_merged_request(q, req, el_ret);
1432 goto out_unlock;
80a761fd 1433 }
1da177e4
LT
1434 }
1435
450991bc 1436get_rq:
7749a8d4
JA
1437 /*
1438 * This sync check and mask will be re-done in init_request_from_bio(),
1439 * but we need to set it earlier to expose the sync flag to the
1440 * rq allocator and io schedulers.
1441 */
1442 rw_flags = bio_data_dir(bio);
1443 if (sync)
7b6d91da 1444 rw_flags |= REQ_SYNC;
7749a8d4 1445
1da177e4 1446 /*
450991bc 1447 * Grab a free request. This is might sleep but can not fail.
d6344532 1448 * Returns with the queue unlocked.
450991bc 1449 */
7749a8d4 1450 req = get_request_wait(q, rw_flags, bio);
da8303c6
TH
1451 if (unlikely(!req)) {
1452 bio_endio(bio, -ENODEV); /* @q is dead */
1453 goto out_unlock;
1454 }
d6344532 1455
450991bc
NP
1456 /*
1457 * After dropping the lock and possibly sleeping here, our request
1458 * may now be mergeable after it had proven unmergeable (above).
1459 * We don't worry about that case for efficiency. It won't happen
1460 * often, and the elevators are able to handle it.
1da177e4 1461 */
52d9e675 1462 init_request_from_bio(req, bio);
1da177e4 1463
9562ad9a 1464 if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags))
11ccf116 1465 req->cpu = raw_smp_processor_id();
73c10101
JA
1466
1467 plug = current->plug;
721a9602 1468 if (plug) {
dc6d36c9
JA
1469 /*
1470 * If this is the first request added after a plug, fire
1471 * of a plug trace. If others have been added before, check
1472 * if we have multiple devices in this plug. If so, make a
1473 * note to sort the list before dispatch.
1474 */
1475 if (list_empty(&plug->list))
1476 trace_block_plug(q);
3540d5e8
SL
1477 else {
1478 if (!plug->should_sort) {
1479 struct request *__rq;
73c10101 1480
3540d5e8
SL
1481 __rq = list_entry_rq(plug->list.prev);
1482 if (__rq->q != q)
1483 plug->should_sort = 1;
1484 }
019ceb7d 1485 if (request_count >= BLK_MAX_REQUEST_COUNT) {
3540d5e8 1486 blk_flush_plug_list(plug, false);
019ceb7d
SL
1487 trace_block_plug(q);
1488 }
73c10101 1489 }
73c10101
JA
1490 list_add_tail(&req->queuelist, &plug->list);
1491 drive_stat_acct(req, 1);
1492 } else {
1493 spin_lock_irq(q->queue_lock);
1494 add_acct_request(q, req, where);
24ecfbe2 1495 __blk_run_queue(q);
73c10101
JA
1496out_unlock:
1497 spin_unlock_irq(q->queue_lock);
1498 }
1da177e4 1499}
c20e8de2 1500EXPORT_SYMBOL_GPL(blk_queue_bio); /* for device mapper only */
1da177e4
LT
1501
1502/*
1503 * If bio->bi_dev is a partition, remap the location
1504 */
1505static inline void blk_partition_remap(struct bio *bio)
1506{
1507 struct block_device *bdev = bio->bi_bdev;
1508
bf2de6f5 1509 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
1da177e4
LT
1510 struct hd_struct *p = bdev->bd_part;
1511
1da177e4
LT
1512 bio->bi_sector += p->start_sect;
1513 bio->bi_bdev = bdev->bd_contains;
c7149d6b 1514
d07335e5
MS
1515 trace_block_bio_remap(bdev_get_queue(bio->bi_bdev), bio,
1516 bdev->bd_dev,
1517 bio->bi_sector - p->start_sect);
1da177e4
LT
1518 }
1519}
1520
1da177e4
LT
1521static void handle_bad_sector(struct bio *bio)
1522{
1523 char b[BDEVNAME_SIZE];
1524
1525 printk(KERN_INFO "attempt to access beyond end of device\n");
1526 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1527 bdevname(bio->bi_bdev, b),
1528 bio->bi_rw,
1529 (unsigned long long)bio->bi_sector + bio_sectors(bio),
77304d2a 1530 (long long)(i_size_read(bio->bi_bdev->bd_inode) >> 9));
1da177e4
LT
1531
1532 set_bit(BIO_EOF, &bio->bi_flags);
1533}
1534
c17bb495
AM
1535#ifdef CONFIG_FAIL_MAKE_REQUEST
1536
1537static DECLARE_FAULT_ATTR(fail_make_request);
1538
1539static int __init setup_fail_make_request(char *str)
1540{
1541 return setup_fault_attr(&fail_make_request, str);
1542}
1543__setup("fail_make_request=", setup_fail_make_request);
1544
b2c9cd37 1545static bool should_fail_request(struct hd_struct *part, unsigned int bytes)
c17bb495 1546{
b2c9cd37 1547 return part->make_it_fail && should_fail(&fail_make_request, bytes);
c17bb495
AM
1548}
1549
1550static int __init fail_make_request_debugfs(void)
1551{
dd48c085
AM
1552 struct dentry *dir = fault_create_debugfs_attr("fail_make_request",
1553 NULL, &fail_make_request);
1554
1555 return IS_ERR(dir) ? PTR_ERR(dir) : 0;
c17bb495
AM
1556}
1557
1558late_initcall(fail_make_request_debugfs);
1559
1560#else /* CONFIG_FAIL_MAKE_REQUEST */
1561
b2c9cd37
AM
1562static inline bool should_fail_request(struct hd_struct *part,
1563 unsigned int bytes)
c17bb495 1564{
b2c9cd37 1565 return false;
c17bb495
AM
1566}
1567
1568#endif /* CONFIG_FAIL_MAKE_REQUEST */
1569
c07e2b41
JA
1570/*
1571 * Check whether this bio extends beyond the end of the device.
1572 */
1573static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1574{
1575 sector_t maxsector;
1576
1577 if (!nr_sectors)
1578 return 0;
1579
1580 /* Test device or partition size, when known. */
77304d2a 1581 maxsector = i_size_read(bio->bi_bdev->bd_inode) >> 9;
c07e2b41
JA
1582 if (maxsector) {
1583 sector_t sector = bio->bi_sector;
1584
1585 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1586 /*
1587 * This may well happen - the kernel calls bread()
1588 * without checking the size of the device, e.g., when
1589 * mounting a device.
1590 */
1591 handle_bad_sector(bio);
1592 return 1;
1593 }
1594 }
1595
1596 return 0;
1597}
1598
27a84d54
CH
1599static noinline_for_stack bool
1600generic_make_request_checks(struct bio *bio)
1da177e4 1601{
165125e1 1602 struct request_queue *q;
5a7bbad2 1603 int nr_sectors = bio_sectors(bio);
51fd77bd 1604 int err = -EIO;
5a7bbad2
CH
1605 char b[BDEVNAME_SIZE];
1606 struct hd_struct *part;
1da177e4
LT
1607
1608 might_sleep();
1da177e4 1609
c07e2b41
JA
1610 if (bio_check_eod(bio, nr_sectors))
1611 goto end_io;
1da177e4 1612
5a7bbad2
CH
1613 q = bdev_get_queue(bio->bi_bdev);
1614 if (unlikely(!q)) {
1615 printk(KERN_ERR
1616 "generic_make_request: Trying to access "
1617 "nonexistent block-device %s (%Lu)\n",
1618 bdevname(bio->bi_bdev, b),
1619 (long long) bio->bi_sector);
1620 goto end_io;
1621 }
c17bb495 1622
5a7bbad2
CH
1623 if (unlikely(!(bio->bi_rw & REQ_DISCARD) &&
1624 nr_sectors > queue_max_hw_sectors(q))) {
1625 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
1626 bdevname(bio->bi_bdev, b),
1627 bio_sectors(bio),
1628 queue_max_hw_sectors(q));
1629 goto end_io;
1630 }
1da177e4 1631
5a7bbad2
CH
1632 part = bio->bi_bdev->bd_part;
1633 if (should_fail_request(part, bio->bi_size) ||
1634 should_fail_request(&part_to_disk(part)->part0,
1635 bio->bi_size))
1636 goto end_io;
2056a782 1637
5a7bbad2
CH
1638 /*
1639 * If this device has partitions, remap block n
1640 * of partition p to block n+start(p) of the disk.
1641 */
1642 blk_partition_remap(bio);
2056a782 1643
5a7bbad2
CH
1644 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio))
1645 goto end_io;
a7384677 1646
5a7bbad2
CH
1647 if (bio_check_eod(bio, nr_sectors))
1648 goto end_io;
1e87901e 1649
5a7bbad2
CH
1650 /*
1651 * Filter flush bio's early so that make_request based
1652 * drivers without flush support don't have to worry
1653 * about them.
1654 */
1655 if ((bio->bi_rw & (REQ_FLUSH | REQ_FUA)) && !q->flush_flags) {
1656 bio->bi_rw &= ~(REQ_FLUSH | REQ_FUA);
1657 if (!nr_sectors) {
1658 err = 0;
51fd77bd
JA
1659 goto end_io;
1660 }
5a7bbad2 1661 }
5ddfe969 1662
5a7bbad2
CH
1663 if ((bio->bi_rw & REQ_DISCARD) &&
1664 (!blk_queue_discard(q) ||
1665 ((bio->bi_rw & REQ_SECURE) &&
1666 !blk_queue_secdiscard(q)))) {
1667 err = -EOPNOTSUPP;
1668 goto end_io;
1669 }
01edede4 1670
bc16a4f9
TH
1671 if (blk_throtl_bio(q, bio))
1672 return false; /* throttled, will be resubmitted later */
27a84d54 1673
5a7bbad2 1674 trace_block_bio_queue(q, bio);
27a84d54 1675 return true;
a7384677
TH
1676
1677end_io:
1678 bio_endio(bio, err);
27a84d54 1679 return false;
1da177e4
LT
1680}
1681
27a84d54
CH
1682/**
1683 * generic_make_request - hand a buffer to its device driver for I/O
1684 * @bio: The bio describing the location in memory and on the device.
1685 *
1686 * generic_make_request() is used to make I/O requests of block
1687 * devices. It is passed a &struct bio, which describes the I/O that needs
1688 * to be done.
1689 *
1690 * generic_make_request() does not return any status. The
1691 * success/failure status of the request, along with notification of
1692 * completion, is delivered asynchronously through the bio->bi_end_io
1693 * function described (one day) else where.
1694 *
1695 * The caller of generic_make_request must make sure that bi_io_vec
1696 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1697 * set to describe the device address, and the
1698 * bi_end_io and optionally bi_private are set to describe how
1699 * completion notification should be signaled.
1700 *
1701 * generic_make_request and the drivers it calls may use bi_next if this
1702 * bio happens to be merged with someone else, and may resubmit the bio to
1703 * a lower device by calling into generic_make_request recursively, which
1704 * means the bio should NOT be touched after the call to ->make_request_fn.
d89d8796
NB
1705 */
1706void generic_make_request(struct bio *bio)
1707{
bddd87c7
AM
1708 struct bio_list bio_list_on_stack;
1709
27a84d54
CH
1710 if (!generic_make_request_checks(bio))
1711 return;
1712
1713 /*
1714 * We only want one ->make_request_fn to be active at a time, else
1715 * stack usage with stacked devices could be a problem. So use
1716 * current->bio_list to keep a list of requests submited by a
1717 * make_request_fn function. current->bio_list is also used as a
1718 * flag to say if generic_make_request is currently active in this
1719 * task or not. If it is NULL, then no make_request is active. If
1720 * it is non-NULL, then a make_request is active, and new requests
1721 * should be added at the tail
1722 */
bddd87c7 1723 if (current->bio_list) {
bddd87c7 1724 bio_list_add(current->bio_list, bio);
d89d8796
NB
1725 return;
1726 }
27a84d54 1727
d89d8796
NB
1728 /* following loop may be a bit non-obvious, and so deserves some
1729 * explanation.
1730 * Before entering the loop, bio->bi_next is NULL (as all callers
1731 * ensure that) so we have a list with a single bio.
1732 * We pretend that we have just taken it off a longer list, so
bddd87c7
AM
1733 * we assign bio_list to a pointer to the bio_list_on_stack,
1734 * thus initialising the bio_list of new bios to be
27a84d54 1735 * added. ->make_request() may indeed add some more bios
d89d8796
NB
1736 * through a recursive call to generic_make_request. If it
1737 * did, we find a non-NULL value in bio_list and re-enter the loop
1738 * from the top. In this case we really did just take the bio
bddd87c7 1739 * of the top of the list (no pretending) and so remove it from
27a84d54 1740 * bio_list, and call into ->make_request() again.
d89d8796
NB
1741 */
1742 BUG_ON(bio->bi_next);
bddd87c7
AM
1743 bio_list_init(&bio_list_on_stack);
1744 current->bio_list = &bio_list_on_stack;
d89d8796 1745 do {
27a84d54
CH
1746 struct request_queue *q = bdev_get_queue(bio->bi_bdev);
1747
1748 q->make_request_fn(q, bio);
1749
bddd87c7 1750 bio = bio_list_pop(current->bio_list);
d89d8796 1751 } while (bio);
bddd87c7 1752 current->bio_list = NULL; /* deactivate */
d89d8796 1753}
1da177e4
LT
1754EXPORT_SYMBOL(generic_make_request);
1755
1756/**
710027a4 1757 * submit_bio - submit a bio to the block device layer for I/O
1da177e4
LT
1758 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1759 * @bio: The &struct bio which describes the I/O
1760 *
1761 * submit_bio() is very similar in purpose to generic_make_request(), and
1762 * uses that function to do most of the work. Both are fairly rough
710027a4 1763 * interfaces; @bio must be presetup and ready for I/O.
1da177e4
LT
1764 *
1765 */
1766void submit_bio(int rw, struct bio *bio)
1767{
1768 int count = bio_sectors(bio);
1769
22e2c507 1770 bio->bi_rw |= rw;
1da177e4 1771
bf2de6f5
JA
1772 /*
1773 * If it's a regular read/write or a barrier with data attached,
1774 * go through the normal accounting stuff before submission.
1775 */
3ffb52e7 1776 if (bio_has_data(bio) && !(rw & REQ_DISCARD)) {
bf2de6f5
JA
1777 if (rw & WRITE) {
1778 count_vm_events(PGPGOUT, count);
1779 } else {
1780 task_io_account_read(bio->bi_size);
1781 count_vm_events(PGPGIN, count);
1782 }
1783
1784 if (unlikely(block_dump)) {
1785 char b[BDEVNAME_SIZE];
8dcbdc74 1786 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s (%u sectors)\n",
ba25f9dc 1787 current->comm, task_pid_nr(current),
bf2de6f5
JA
1788 (rw & WRITE) ? "WRITE" : "READ",
1789 (unsigned long long)bio->bi_sector,
8dcbdc74
SM
1790 bdevname(bio->bi_bdev, b),
1791 count);
bf2de6f5 1792 }
1da177e4
LT
1793 }
1794
1795 generic_make_request(bio);
1796}
1da177e4
LT
1797EXPORT_SYMBOL(submit_bio);
1798
82124d60
KU
1799/**
1800 * blk_rq_check_limits - Helper function to check a request for the queue limit
1801 * @q: the queue
1802 * @rq: the request being checked
1803 *
1804 * Description:
1805 * @rq may have been made based on weaker limitations of upper-level queues
1806 * in request stacking drivers, and it may violate the limitation of @q.
1807 * Since the block layer and the underlying device driver trust @rq
1808 * after it is inserted to @q, it should be checked against @q before
1809 * the insertion using this generic function.
1810 *
1811 * This function should also be useful for request stacking drivers
eef35c2d 1812 * in some cases below, so export this function.
82124d60
KU
1813 * Request stacking drivers like request-based dm may change the queue
1814 * limits while requests are in the queue (e.g. dm's table swapping).
1815 * Such request stacking drivers should check those requests agaist
1816 * the new queue limits again when they dispatch those requests,
1817 * although such checkings are also done against the old queue limits
1818 * when submitting requests.
1819 */
1820int blk_rq_check_limits(struct request_queue *q, struct request *rq)
1821{
3383977f
S
1822 if (rq->cmd_flags & REQ_DISCARD)
1823 return 0;
1824
ae03bf63
MP
1825 if (blk_rq_sectors(rq) > queue_max_sectors(q) ||
1826 blk_rq_bytes(rq) > queue_max_hw_sectors(q) << 9) {
82124d60
KU
1827 printk(KERN_ERR "%s: over max size limit.\n", __func__);
1828 return -EIO;
1829 }
1830
1831 /*
1832 * queue's settings related to segment counting like q->bounce_pfn
1833 * may differ from that of other stacking queues.
1834 * Recalculate it to check the request correctly on this queue's
1835 * limitation.
1836 */
1837 blk_recalc_rq_segments(rq);
8a78362c 1838 if (rq->nr_phys_segments > queue_max_segments(q)) {
82124d60
KU
1839 printk(KERN_ERR "%s: over max segments limit.\n", __func__);
1840 return -EIO;
1841 }
1842
1843 return 0;
1844}
1845EXPORT_SYMBOL_GPL(blk_rq_check_limits);
1846
1847/**
1848 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
1849 * @q: the queue to submit the request
1850 * @rq: the request being queued
1851 */
1852int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
1853{
1854 unsigned long flags;
4853abaa 1855 int where = ELEVATOR_INSERT_BACK;
82124d60
KU
1856
1857 if (blk_rq_check_limits(q, rq))
1858 return -EIO;
1859
b2c9cd37
AM
1860 if (rq->rq_disk &&
1861 should_fail_request(&rq->rq_disk->part0, blk_rq_bytes(rq)))
82124d60 1862 return -EIO;
82124d60
KU
1863
1864 spin_lock_irqsave(q->queue_lock, flags);
8ba61435
TH
1865 if (unlikely(blk_queue_dead(q))) {
1866 spin_unlock_irqrestore(q->queue_lock, flags);
1867 return -ENODEV;
1868 }
82124d60
KU
1869
1870 /*
1871 * Submitting request must be dequeued before calling this function
1872 * because it will be linked to another request_queue
1873 */
1874 BUG_ON(blk_queued_rq(rq));
1875
4853abaa
JM
1876 if (rq->cmd_flags & (REQ_FLUSH|REQ_FUA))
1877 where = ELEVATOR_INSERT_FLUSH;
1878
1879 add_acct_request(q, rq, where);
e67b77c7
JM
1880 if (where == ELEVATOR_INSERT_FLUSH)
1881 __blk_run_queue(q);
82124d60
KU
1882 spin_unlock_irqrestore(q->queue_lock, flags);
1883
1884 return 0;
1885}
1886EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
1887
80a761fd
TH
1888/**
1889 * blk_rq_err_bytes - determine number of bytes till the next failure boundary
1890 * @rq: request to examine
1891 *
1892 * Description:
1893 * A request could be merge of IOs which require different failure
1894 * handling. This function determines the number of bytes which
1895 * can be failed from the beginning of the request without
1896 * crossing into area which need to be retried further.
1897 *
1898 * Return:
1899 * The number of bytes to fail.
1900 *
1901 * Context:
1902 * queue_lock must be held.
1903 */
1904unsigned int blk_rq_err_bytes(const struct request *rq)
1905{
1906 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
1907 unsigned int bytes = 0;
1908 struct bio *bio;
1909
1910 if (!(rq->cmd_flags & REQ_MIXED_MERGE))
1911 return blk_rq_bytes(rq);
1912
1913 /*
1914 * Currently the only 'mixing' which can happen is between
1915 * different fastfail types. We can safely fail portions
1916 * which have all the failfast bits that the first one has -
1917 * the ones which are at least as eager to fail as the first
1918 * one.
1919 */
1920 for (bio = rq->bio; bio; bio = bio->bi_next) {
1921 if ((bio->bi_rw & ff) != ff)
1922 break;
1923 bytes += bio->bi_size;
1924 }
1925
1926 /* this could lead to infinite loop */
1927 BUG_ON(blk_rq_bytes(rq) && !bytes);
1928 return bytes;
1929}
1930EXPORT_SYMBOL_GPL(blk_rq_err_bytes);
1931
bc58ba94
JA
1932static void blk_account_io_completion(struct request *req, unsigned int bytes)
1933{
c2553b58 1934 if (blk_do_io_stat(req)) {
bc58ba94
JA
1935 const int rw = rq_data_dir(req);
1936 struct hd_struct *part;
1937 int cpu;
1938
1939 cpu = part_stat_lock();
09e099d4 1940 part = req->part;
bc58ba94
JA
1941 part_stat_add(cpu, part, sectors[rw], bytes >> 9);
1942 part_stat_unlock();
1943 }
1944}
1945
1946static void blk_account_io_done(struct request *req)
1947{
bc58ba94 1948 /*
dd4c133f
TH
1949 * Account IO completion. flush_rq isn't accounted as a
1950 * normal IO on queueing nor completion. Accounting the
1951 * containing request is enough.
bc58ba94 1952 */
414b4ff5 1953 if (blk_do_io_stat(req) && !(req->cmd_flags & REQ_FLUSH_SEQ)) {
bc58ba94
JA
1954 unsigned long duration = jiffies - req->start_time;
1955 const int rw = rq_data_dir(req);
1956 struct hd_struct *part;
1957 int cpu;
1958
1959 cpu = part_stat_lock();
09e099d4 1960 part = req->part;
bc58ba94
JA
1961
1962 part_stat_inc(cpu, part, ios[rw]);
1963 part_stat_add(cpu, part, ticks[rw], duration);
1964 part_round_stats(cpu, part);
316d315b 1965 part_dec_in_flight(part, rw);
bc58ba94 1966
6c23a968 1967 hd_struct_put(part);
bc58ba94
JA
1968 part_stat_unlock();
1969 }
1970}
1971
3bcddeac 1972/**
9934c8c0
TH
1973 * blk_peek_request - peek at the top of a request queue
1974 * @q: request queue to peek at
1975 *
1976 * Description:
1977 * Return the request at the top of @q. The returned request
1978 * should be started using blk_start_request() before LLD starts
1979 * processing it.
1980 *
1981 * Return:
1982 * Pointer to the request at the top of @q if available. Null
1983 * otherwise.
1984 *
1985 * Context:
1986 * queue_lock must be held.
1987 */
1988struct request *blk_peek_request(struct request_queue *q)
158dbda0
TH
1989{
1990 struct request *rq;
1991 int ret;
1992
1993 while ((rq = __elv_next_request(q)) != NULL) {
1994 if (!(rq->cmd_flags & REQ_STARTED)) {
1995 /*
1996 * This is the first time the device driver
1997 * sees this request (possibly after
1998 * requeueing). Notify IO scheduler.
1999 */
33659ebb 2000 if (rq->cmd_flags & REQ_SORTED)
158dbda0
TH
2001 elv_activate_rq(q, rq);
2002
2003 /*
2004 * just mark as started even if we don't start
2005 * it, a request that has been delayed should
2006 * not be passed by new incoming requests
2007 */
2008 rq->cmd_flags |= REQ_STARTED;
2009 trace_block_rq_issue(q, rq);
2010 }
2011
2012 if (!q->boundary_rq || q->boundary_rq == rq) {
2013 q->end_sector = rq_end_sector(rq);
2014 q->boundary_rq = NULL;
2015 }
2016
2017 if (rq->cmd_flags & REQ_DONTPREP)
2018 break;
2019
2e46e8b2 2020 if (q->dma_drain_size && blk_rq_bytes(rq)) {
158dbda0
TH
2021 /*
2022 * make sure space for the drain appears we
2023 * know we can do this because max_hw_segments
2024 * has been adjusted to be one fewer than the
2025 * device can handle
2026 */
2027 rq->nr_phys_segments++;
2028 }
2029
2030 if (!q->prep_rq_fn)
2031 break;
2032
2033 ret = q->prep_rq_fn(q, rq);
2034 if (ret == BLKPREP_OK) {
2035 break;
2036 } else if (ret == BLKPREP_DEFER) {
2037 /*
2038 * the request may have been (partially) prepped.
2039 * we need to keep this request in the front to
2040 * avoid resource deadlock. REQ_STARTED will
2041 * prevent other fs requests from passing this one.
2042 */
2e46e8b2 2043 if (q->dma_drain_size && blk_rq_bytes(rq) &&
158dbda0
TH
2044 !(rq->cmd_flags & REQ_DONTPREP)) {
2045 /*
2046 * remove the space for the drain we added
2047 * so that we don't add it again
2048 */
2049 --rq->nr_phys_segments;
2050 }
2051
2052 rq = NULL;
2053 break;
2054 } else if (ret == BLKPREP_KILL) {
2055 rq->cmd_flags |= REQ_QUIET;
c143dc90
JB
2056 /*
2057 * Mark this request as started so we don't trigger
2058 * any debug logic in the end I/O path.
2059 */
2060 blk_start_request(rq);
40cbbb78 2061 __blk_end_request_all(rq, -EIO);
158dbda0
TH
2062 } else {
2063 printk(KERN_ERR "%s: bad return=%d\n", __func__, ret);
2064 break;
2065 }
2066 }
2067
2068 return rq;
2069}
9934c8c0 2070EXPORT_SYMBOL(blk_peek_request);
158dbda0 2071
9934c8c0 2072void blk_dequeue_request(struct request *rq)
158dbda0 2073{
9934c8c0
TH
2074 struct request_queue *q = rq->q;
2075
158dbda0
TH
2076 BUG_ON(list_empty(&rq->queuelist));
2077 BUG_ON(ELV_ON_HASH(rq));
2078
2079 list_del_init(&rq->queuelist);
2080
2081 /*
2082 * the time frame between a request being removed from the lists
2083 * and to it is freed is accounted as io that is in progress at
2084 * the driver side.
2085 */
9195291e 2086 if (blk_account_rq(rq)) {
0a7ae2ff 2087 q->in_flight[rq_is_sync(rq)]++;
9195291e
DS
2088 set_io_start_time_ns(rq);
2089 }
158dbda0
TH
2090}
2091
9934c8c0
TH
2092/**
2093 * blk_start_request - start request processing on the driver
2094 * @req: request to dequeue
2095 *
2096 * Description:
2097 * Dequeue @req and start timeout timer on it. This hands off the
2098 * request to the driver.
2099 *
2100 * Block internal functions which don't want to start timer should
2101 * call blk_dequeue_request().
2102 *
2103 * Context:
2104 * queue_lock must be held.
2105 */
2106void blk_start_request(struct request *req)
2107{
2108 blk_dequeue_request(req);
2109
2110 /*
5f49f631
TH
2111 * We are now handing the request to the hardware, initialize
2112 * resid_len to full count and add the timeout handler.
9934c8c0 2113 */
5f49f631 2114 req->resid_len = blk_rq_bytes(req);
dbb66c4b
FT
2115 if (unlikely(blk_bidi_rq(req)))
2116 req->next_rq->resid_len = blk_rq_bytes(req->next_rq);
2117
9934c8c0
TH
2118 blk_add_timer(req);
2119}
2120EXPORT_SYMBOL(blk_start_request);
2121
2122/**
2123 * blk_fetch_request - fetch a request from a request queue
2124 * @q: request queue to fetch a request from
2125 *
2126 * Description:
2127 * Return the request at the top of @q. The request is started on
2128 * return and LLD can start processing it immediately.
2129 *
2130 * Return:
2131 * Pointer to the request at the top of @q if available. Null
2132 * otherwise.
2133 *
2134 * Context:
2135 * queue_lock must be held.
2136 */
2137struct request *blk_fetch_request(struct request_queue *q)
2138{
2139 struct request *rq;
2140
2141 rq = blk_peek_request(q);
2142 if (rq)
2143 blk_start_request(rq);
2144 return rq;
2145}
2146EXPORT_SYMBOL(blk_fetch_request);
2147
3bcddeac 2148/**
2e60e022 2149 * blk_update_request - Special helper function for request stacking drivers
8ebf9756 2150 * @req: the request being processed
710027a4 2151 * @error: %0 for success, < %0 for error
8ebf9756 2152 * @nr_bytes: number of bytes to complete @req
3bcddeac
KU
2153 *
2154 * Description:
8ebf9756
RD
2155 * Ends I/O on a number of bytes attached to @req, but doesn't complete
2156 * the request structure even if @req doesn't have leftover.
2157 * If @req has leftover, sets it up for the next range of segments.
2e60e022
TH
2158 *
2159 * This special helper function is only for request stacking drivers
2160 * (e.g. request-based dm) so that they can handle partial completion.
2161 * Actual device drivers should use blk_end_request instead.
2162 *
2163 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
2164 * %false return from this function.
3bcddeac
KU
2165 *
2166 * Return:
2e60e022
TH
2167 * %false - this request doesn't have any more data
2168 * %true - this request has more data
3bcddeac 2169 **/
2e60e022 2170bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
1da177e4 2171{
5450d3e1 2172 int total_bytes, bio_nbytes, next_idx = 0;
1da177e4
LT
2173 struct bio *bio;
2174
2e60e022
TH
2175 if (!req->bio)
2176 return false;
2177
5f3ea37c 2178 trace_block_rq_complete(req->q, req);
2056a782 2179
1da177e4 2180 /*
6f41469c
TH
2181 * For fs requests, rq is just carrier of independent bio's
2182 * and each partial completion should be handled separately.
2183 * Reset per-request error on each partial completion.
2184 *
2185 * TODO: tj: This is too subtle. It would be better to let
2186 * low level drivers do what they see fit.
1da177e4 2187 */
33659ebb 2188 if (req->cmd_type == REQ_TYPE_FS)
1da177e4
LT
2189 req->errors = 0;
2190
33659ebb
CH
2191 if (error && req->cmd_type == REQ_TYPE_FS &&
2192 !(req->cmd_flags & REQ_QUIET)) {
79775567
HR
2193 char *error_type;
2194
2195 switch (error) {
2196 case -ENOLINK:
2197 error_type = "recoverable transport";
2198 break;
2199 case -EREMOTEIO:
2200 error_type = "critical target";
2201 break;
2202 case -EBADE:
2203 error_type = "critical nexus";
2204 break;
2205 case -EIO:
2206 default:
2207 error_type = "I/O";
2208 break;
2209 }
2210 printk(KERN_ERR "end_request: %s error, dev %s, sector %llu\n",
2211 error_type, req->rq_disk ? req->rq_disk->disk_name : "?",
2212 (unsigned long long)blk_rq_pos(req));
1da177e4
LT
2213 }
2214
bc58ba94 2215 blk_account_io_completion(req, nr_bytes);
d72d904a 2216
1da177e4
LT
2217 total_bytes = bio_nbytes = 0;
2218 while ((bio = req->bio) != NULL) {
2219 int nbytes;
2220
2221 if (nr_bytes >= bio->bi_size) {
2222 req->bio = bio->bi_next;
2223 nbytes = bio->bi_size;
5bb23a68 2224 req_bio_endio(req, bio, nbytes, error);
1da177e4
LT
2225 next_idx = 0;
2226 bio_nbytes = 0;
2227 } else {
2228 int idx = bio->bi_idx + next_idx;
2229
af498d7f 2230 if (unlikely(idx >= bio->bi_vcnt)) {
1da177e4 2231 blk_dump_rq_flags(req, "__end_that");
6728cb0e 2232 printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
af498d7f 2233 __func__, idx, bio->bi_vcnt);
1da177e4
LT
2234 break;
2235 }
2236
2237 nbytes = bio_iovec_idx(bio, idx)->bv_len;
2238 BIO_BUG_ON(nbytes > bio->bi_size);
2239
2240 /*
2241 * not a complete bvec done
2242 */
2243 if (unlikely(nbytes > nr_bytes)) {
2244 bio_nbytes += nr_bytes;
2245 total_bytes += nr_bytes;
2246 break;
2247 }
2248
2249 /*
2250 * advance to the next vector
2251 */
2252 next_idx++;
2253 bio_nbytes += nbytes;
2254 }
2255
2256 total_bytes += nbytes;
2257 nr_bytes -= nbytes;
2258
6728cb0e
JA
2259 bio = req->bio;
2260 if (bio) {
1da177e4
LT
2261 /*
2262 * end more in this run, or just return 'not-done'
2263 */
2264 if (unlikely(nr_bytes <= 0))
2265 break;
2266 }
2267 }
2268
2269 /*
2270 * completely done
2271 */
2e60e022
TH
2272 if (!req->bio) {
2273 /*
2274 * Reset counters so that the request stacking driver
2275 * can find how many bytes remain in the request
2276 * later.
2277 */
a2dec7b3 2278 req->__data_len = 0;
2e60e022
TH
2279 return false;
2280 }
1da177e4
LT
2281
2282 /*
2283 * if the request wasn't completed, update state
2284 */
2285 if (bio_nbytes) {
5bb23a68 2286 req_bio_endio(req, bio, bio_nbytes, error);
1da177e4
LT
2287 bio->bi_idx += next_idx;
2288 bio_iovec(bio)->bv_offset += nr_bytes;
2289 bio_iovec(bio)->bv_len -= nr_bytes;
2290 }
2291
a2dec7b3 2292 req->__data_len -= total_bytes;
2e46e8b2
TH
2293 req->buffer = bio_data(req->bio);
2294
2295 /* update sector only for requests with clear definition of sector */
33659ebb 2296 if (req->cmd_type == REQ_TYPE_FS || (req->cmd_flags & REQ_DISCARD))
a2dec7b3 2297 req->__sector += total_bytes >> 9;
2e46e8b2 2298
80a761fd
TH
2299 /* mixed attributes always follow the first bio */
2300 if (req->cmd_flags & REQ_MIXED_MERGE) {
2301 req->cmd_flags &= ~REQ_FAILFAST_MASK;
2302 req->cmd_flags |= req->bio->bi_rw & REQ_FAILFAST_MASK;
2303 }
2304
2e46e8b2
TH
2305 /*
2306 * If total number of sectors is less than the first segment
2307 * size, something has gone terribly wrong.
2308 */
2309 if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
8182924b 2310 blk_dump_rq_flags(req, "request botched");
a2dec7b3 2311 req->__data_len = blk_rq_cur_bytes(req);
2e46e8b2
TH
2312 }
2313
2314 /* recalculate the number of segments */
1da177e4 2315 blk_recalc_rq_segments(req);
2e46e8b2 2316
2e60e022 2317 return true;
1da177e4 2318}
2e60e022 2319EXPORT_SYMBOL_GPL(blk_update_request);
1da177e4 2320
2e60e022
TH
2321static bool blk_update_bidi_request(struct request *rq, int error,
2322 unsigned int nr_bytes,
2323 unsigned int bidi_bytes)
5efccd17 2324{
2e60e022
TH
2325 if (blk_update_request(rq, error, nr_bytes))
2326 return true;
5efccd17 2327
2e60e022
TH
2328 /* Bidi request must be completed as a whole */
2329 if (unlikely(blk_bidi_rq(rq)) &&
2330 blk_update_request(rq->next_rq, error, bidi_bytes))
2331 return true;
5efccd17 2332
e2e1a148
JA
2333 if (blk_queue_add_random(rq->q))
2334 add_disk_randomness(rq->rq_disk);
2e60e022
TH
2335
2336 return false;
1da177e4
LT
2337}
2338
28018c24
JB
2339/**
2340 * blk_unprep_request - unprepare a request
2341 * @req: the request
2342 *
2343 * This function makes a request ready for complete resubmission (or
2344 * completion). It happens only after all error handling is complete,
2345 * so represents the appropriate moment to deallocate any resources
2346 * that were allocated to the request in the prep_rq_fn. The queue
2347 * lock is held when calling this.
2348 */
2349void blk_unprep_request(struct request *req)
2350{
2351 struct request_queue *q = req->q;
2352
2353 req->cmd_flags &= ~REQ_DONTPREP;
2354 if (q->unprep_rq_fn)
2355 q->unprep_rq_fn(q, req);
2356}
2357EXPORT_SYMBOL_GPL(blk_unprep_request);
2358
1da177e4
LT
2359/*
2360 * queue lock must be held
2361 */
2e60e022 2362static void blk_finish_request(struct request *req, int error)
1da177e4 2363{
b8286239
KU
2364 if (blk_rq_tagged(req))
2365 blk_queue_end_tag(req->q, req);
2366
ba396a6c 2367 BUG_ON(blk_queued_rq(req));
1da177e4 2368
33659ebb 2369 if (unlikely(laptop_mode) && req->cmd_type == REQ_TYPE_FS)
31373d09 2370 laptop_io_completion(&req->q->backing_dev_info);
1da177e4 2371
e78042e5
MA
2372 blk_delete_timer(req);
2373
28018c24
JB
2374 if (req->cmd_flags & REQ_DONTPREP)
2375 blk_unprep_request(req);
2376
2377
bc58ba94 2378 blk_account_io_done(req);
b8286239 2379
1da177e4 2380 if (req->end_io)
8ffdc655 2381 req->end_io(req, error);
b8286239
KU
2382 else {
2383 if (blk_bidi_rq(req))
2384 __blk_put_request(req->next_rq->q, req->next_rq);
2385
1da177e4 2386 __blk_put_request(req->q, req);
b8286239 2387 }
1da177e4
LT
2388}
2389
3b11313a 2390/**
2e60e022
TH
2391 * blk_end_bidi_request - Complete a bidi request
2392 * @rq: the request to complete
2393 * @error: %0 for success, < %0 for error
2394 * @nr_bytes: number of bytes to complete @rq
2395 * @bidi_bytes: number of bytes to complete @rq->next_rq
a0cd1285
JA
2396 *
2397 * Description:
e3a04fe3 2398 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
2e60e022
TH
2399 * Drivers that supports bidi can safely call this member for any
2400 * type of request, bidi or uni. In the later case @bidi_bytes is
2401 * just ignored.
336cdb40
KU
2402 *
2403 * Return:
2e60e022
TH
2404 * %false - we are done with this request
2405 * %true - still buffers pending for this request
a0cd1285 2406 **/
b1f74493 2407static bool blk_end_bidi_request(struct request *rq, int error,
32fab448
KU
2408 unsigned int nr_bytes, unsigned int bidi_bytes)
2409{
336cdb40 2410 struct request_queue *q = rq->q;
2e60e022 2411 unsigned long flags;
32fab448 2412
2e60e022
TH
2413 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2414 return true;
32fab448 2415
336cdb40 2416 spin_lock_irqsave(q->queue_lock, flags);
2e60e022 2417 blk_finish_request(rq, error);
336cdb40
KU
2418 spin_unlock_irqrestore(q->queue_lock, flags);
2419
2e60e022 2420 return false;
32fab448
KU
2421}
2422
336cdb40 2423/**
2e60e022
TH
2424 * __blk_end_bidi_request - Complete a bidi request with queue lock held
2425 * @rq: the request to complete
710027a4 2426 * @error: %0 for success, < %0 for error
e3a04fe3
KU
2427 * @nr_bytes: number of bytes to complete @rq
2428 * @bidi_bytes: number of bytes to complete @rq->next_rq
336cdb40
KU
2429 *
2430 * Description:
2e60e022
TH
2431 * Identical to blk_end_bidi_request() except that queue lock is
2432 * assumed to be locked on entry and remains so on return.
336cdb40
KU
2433 *
2434 * Return:
2e60e022
TH
2435 * %false - we are done with this request
2436 * %true - still buffers pending for this request
336cdb40 2437 **/
4853abaa 2438bool __blk_end_bidi_request(struct request *rq, int error,
b1f74493 2439 unsigned int nr_bytes, unsigned int bidi_bytes)
336cdb40 2440{
2e60e022
TH
2441 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2442 return true;
336cdb40 2443
2e60e022 2444 blk_finish_request(rq, error);
336cdb40 2445
2e60e022 2446 return false;
336cdb40 2447}
e19a3ab0
KU
2448
2449/**
2450 * blk_end_request - Helper function for drivers to complete the request.
2451 * @rq: the request being processed
710027a4 2452 * @error: %0 for success, < %0 for error
e19a3ab0
KU
2453 * @nr_bytes: number of bytes to complete
2454 *
2455 * Description:
2456 * Ends I/O on a number of bytes attached to @rq.
2457 * If @rq has leftover, sets it up for the next range of segments.
2458 *
2459 * Return:
b1f74493
FT
2460 * %false - we are done with this request
2461 * %true - still buffers pending for this request
e19a3ab0 2462 **/
b1f74493 2463bool blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
e19a3ab0 2464{
b1f74493 2465 return blk_end_bidi_request(rq, error, nr_bytes, 0);
e19a3ab0 2466}
56ad1740 2467EXPORT_SYMBOL(blk_end_request);
336cdb40
KU
2468
2469/**
b1f74493
FT
2470 * blk_end_request_all - Helper function for drives to finish the request.
2471 * @rq: the request to finish
8ebf9756 2472 * @error: %0 for success, < %0 for error
336cdb40
KU
2473 *
2474 * Description:
b1f74493
FT
2475 * Completely finish @rq.
2476 */
2477void blk_end_request_all(struct request *rq, int error)
336cdb40 2478{
b1f74493
FT
2479 bool pending;
2480 unsigned int bidi_bytes = 0;
336cdb40 2481
b1f74493
FT
2482 if (unlikely(blk_bidi_rq(rq)))
2483 bidi_bytes = blk_rq_bytes(rq->next_rq);
336cdb40 2484
b1f74493
FT
2485 pending = blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2486 BUG_ON(pending);
2487}
56ad1740 2488EXPORT_SYMBOL(blk_end_request_all);
336cdb40 2489
b1f74493
FT
2490/**
2491 * blk_end_request_cur - Helper function to finish the current request chunk.
2492 * @rq: the request to finish the current chunk for
8ebf9756 2493 * @error: %0 for success, < %0 for error
b1f74493
FT
2494 *
2495 * Description:
2496 * Complete the current consecutively mapped chunk from @rq.
2497 *
2498 * Return:
2499 * %false - we are done with this request
2500 * %true - still buffers pending for this request
2501 */
2502bool blk_end_request_cur(struct request *rq, int error)
2503{
2504 return blk_end_request(rq, error, blk_rq_cur_bytes(rq));
336cdb40 2505}
56ad1740 2506EXPORT_SYMBOL(blk_end_request_cur);
336cdb40 2507
80a761fd
TH
2508/**
2509 * blk_end_request_err - Finish a request till the next failure boundary.
2510 * @rq: the request to finish till the next failure boundary for
2511 * @error: must be negative errno
2512 *
2513 * Description:
2514 * Complete @rq till the next failure boundary.
2515 *
2516 * Return:
2517 * %false - we are done with this request
2518 * %true - still buffers pending for this request
2519 */
2520bool blk_end_request_err(struct request *rq, int error)
2521{
2522 WARN_ON(error >= 0);
2523 return blk_end_request(rq, error, blk_rq_err_bytes(rq));
2524}
2525EXPORT_SYMBOL_GPL(blk_end_request_err);
2526
e3a04fe3 2527/**
b1f74493
FT
2528 * __blk_end_request - Helper function for drivers to complete the request.
2529 * @rq: the request being processed
2530 * @error: %0 for success, < %0 for error
2531 * @nr_bytes: number of bytes to complete
e3a04fe3
KU
2532 *
2533 * Description:
b1f74493 2534 * Must be called with queue lock held unlike blk_end_request().
e3a04fe3
KU
2535 *
2536 * Return:
b1f74493
FT
2537 * %false - we are done with this request
2538 * %true - still buffers pending for this request
e3a04fe3 2539 **/
b1f74493 2540bool __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
e3a04fe3 2541{
b1f74493 2542 return __blk_end_bidi_request(rq, error, nr_bytes, 0);
e3a04fe3 2543}
56ad1740 2544EXPORT_SYMBOL(__blk_end_request);
e3a04fe3 2545
32fab448 2546/**
b1f74493
FT
2547 * __blk_end_request_all - Helper function for drives to finish the request.
2548 * @rq: the request to finish
8ebf9756 2549 * @error: %0 for success, < %0 for error
32fab448
KU
2550 *
2551 * Description:
b1f74493 2552 * Completely finish @rq. Must be called with queue lock held.
32fab448 2553 */
b1f74493 2554void __blk_end_request_all(struct request *rq, int error)
32fab448 2555{
b1f74493
FT
2556 bool pending;
2557 unsigned int bidi_bytes = 0;
2558
2559 if (unlikely(blk_bidi_rq(rq)))
2560 bidi_bytes = blk_rq_bytes(rq->next_rq);
2561
2562 pending = __blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2563 BUG_ON(pending);
32fab448 2564}
56ad1740 2565EXPORT_SYMBOL(__blk_end_request_all);
32fab448 2566
e19a3ab0 2567/**
b1f74493
FT
2568 * __blk_end_request_cur - Helper function to finish the current request chunk.
2569 * @rq: the request to finish the current chunk for
8ebf9756 2570 * @error: %0 for success, < %0 for error
e19a3ab0
KU
2571 *
2572 * Description:
b1f74493
FT
2573 * Complete the current consecutively mapped chunk from @rq. Must
2574 * be called with queue lock held.
e19a3ab0
KU
2575 *
2576 * Return:
b1f74493
FT
2577 * %false - we are done with this request
2578 * %true - still buffers pending for this request
2579 */
2580bool __blk_end_request_cur(struct request *rq, int error)
e19a3ab0 2581{
b1f74493 2582 return __blk_end_request(rq, error, blk_rq_cur_bytes(rq));
e19a3ab0 2583}
56ad1740 2584EXPORT_SYMBOL(__blk_end_request_cur);
e19a3ab0 2585
80a761fd
TH
2586/**
2587 * __blk_end_request_err - Finish a request till the next failure boundary.
2588 * @rq: the request to finish till the next failure boundary for
2589 * @error: must be negative errno
2590 *
2591 * Description:
2592 * Complete @rq till the next failure boundary. Must be called
2593 * with queue lock held.
2594 *
2595 * Return:
2596 * %false - we are done with this request
2597 * %true - still buffers pending for this request
2598 */
2599bool __blk_end_request_err(struct request *rq, int error)
2600{
2601 WARN_ON(error >= 0);
2602 return __blk_end_request(rq, error, blk_rq_err_bytes(rq));
2603}
2604EXPORT_SYMBOL_GPL(__blk_end_request_err);
2605
86db1e29
JA
2606void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2607 struct bio *bio)
1da177e4 2608{
a82afdfc 2609 /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw */
7b6d91da 2610 rq->cmd_flags |= bio->bi_rw & REQ_WRITE;
1da177e4 2611
fb2dce86
DW
2612 if (bio_has_data(bio)) {
2613 rq->nr_phys_segments = bio_phys_segments(q, bio);
fb2dce86
DW
2614 rq->buffer = bio_data(bio);
2615 }
a2dec7b3 2616 rq->__data_len = bio->bi_size;
1da177e4 2617 rq->bio = rq->biotail = bio;
1da177e4 2618
66846572
N
2619 if (bio->bi_bdev)
2620 rq->rq_disk = bio->bi_bdev->bd_disk;
2621}
1da177e4 2622
2d4dc890
IL
2623#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
2624/**
2625 * rq_flush_dcache_pages - Helper function to flush all pages in a request
2626 * @rq: the request to be flushed
2627 *
2628 * Description:
2629 * Flush all pages in @rq.
2630 */
2631void rq_flush_dcache_pages(struct request *rq)
2632{
2633 struct req_iterator iter;
2634 struct bio_vec *bvec;
2635
2636 rq_for_each_segment(bvec, rq, iter)
2637 flush_dcache_page(bvec->bv_page);
2638}
2639EXPORT_SYMBOL_GPL(rq_flush_dcache_pages);
2640#endif
2641
ef9e3fac
KU
2642/**
2643 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
2644 * @q : the queue of the device being checked
2645 *
2646 * Description:
2647 * Check if underlying low-level drivers of a device are busy.
2648 * If the drivers want to export their busy state, they must set own
2649 * exporting function using blk_queue_lld_busy() first.
2650 *
2651 * Basically, this function is used only by request stacking drivers
2652 * to stop dispatching requests to underlying devices when underlying
2653 * devices are busy. This behavior helps more I/O merging on the queue
2654 * of the request stacking driver and prevents I/O throughput regression
2655 * on burst I/O load.
2656 *
2657 * Return:
2658 * 0 - Not busy (The request stacking driver should dispatch request)
2659 * 1 - Busy (The request stacking driver should stop dispatching request)
2660 */
2661int blk_lld_busy(struct request_queue *q)
2662{
2663 if (q->lld_busy_fn)
2664 return q->lld_busy_fn(q);
2665
2666 return 0;
2667}
2668EXPORT_SYMBOL_GPL(blk_lld_busy);
2669
b0fd271d
KU
2670/**
2671 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
2672 * @rq: the clone request to be cleaned up
2673 *
2674 * Description:
2675 * Free all bios in @rq for a cloned request.
2676 */
2677void blk_rq_unprep_clone(struct request *rq)
2678{
2679 struct bio *bio;
2680
2681 while ((bio = rq->bio) != NULL) {
2682 rq->bio = bio->bi_next;
2683
2684 bio_put(bio);
2685 }
2686}
2687EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
2688
2689/*
2690 * Copy attributes of the original request to the clone request.
2691 * The actual data parts (e.g. ->cmd, ->buffer, ->sense) are not copied.
2692 */
2693static void __blk_rq_prep_clone(struct request *dst, struct request *src)
2694{
2695 dst->cpu = src->cpu;
3a2edd0d 2696 dst->cmd_flags = (src->cmd_flags & REQ_CLONE_MASK) | REQ_NOMERGE;
b0fd271d
KU
2697 dst->cmd_type = src->cmd_type;
2698 dst->__sector = blk_rq_pos(src);
2699 dst->__data_len = blk_rq_bytes(src);
2700 dst->nr_phys_segments = src->nr_phys_segments;
2701 dst->ioprio = src->ioprio;
2702 dst->extra_len = src->extra_len;
2703}
2704
2705/**
2706 * blk_rq_prep_clone - Helper function to setup clone request
2707 * @rq: the request to be setup
2708 * @rq_src: original request to be cloned
2709 * @bs: bio_set that bios for clone are allocated from
2710 * @gfp_mask: memory allocation mask for bio
2711 * @bio_ctr: setup function to be called for each clone bio.
2712 * Returns %0 for success, non %0 for failure.
2713 * @data: private data to be passed to @bio_ctr
2714 *
2715 * Description:
2716 * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
2717 * The actual data parts of @rq_src (e.g. ->cmd, ->buffer, ->sense)
2718 * are not copied, and copying such parts is the caller's responsibility.
2719 * Also, pages which the original bios are pointing to are not copied
2720 * and the cloned bios just point same pages.
2721 * So cloned bios must be completed before original bios, which means
2722 * the caller must complete @rq before @rq_src.
2723 */
2724int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
2725 struct bio_set *bs, gfp_t gfp_mask,
2726 int (*bio_ctr)(struct bio *, struct bio *, void *),
2727 void *data)
2728{
2729 struct bio *bio, *bio_src;
2730
2731 if (!bs)
2732 bs = fs_bio_set;
2733
2734 blk_rq_init(NULL, rq);
2735
2736 __rq_for_each_bio(bio_src, rq_src) {
2737 bio = bio_alloc_bioset(gfp_mask, bio_src->bi_max_vecs, bs);
2738 if (!bio)
2739 goto free_and_out;
2740
2741 __bio_clone(bio, bio_src);
2742
2743 if (bio_integrity(bio_src) &&
7878cba9 2744 bio_integrity_clone(bio, bio_src, gfp_mask, bs))
b0fd271d
KU
2745 goto free_and_out;
2746
2747 if (bio_ctr && bio_ctr(bio, bio_src, data))
2748 goto free_and_out;
2749
2750 if (rq->bio) {
2751 rq->biotail->bi_next = bio;
2752 rq->biotail = bio;
2753 } else
2754 rq->bio = rq->biotail = bio;
2755 }
2756
2757 __blk_rq_prep_clone(rq, rq_src);
2758
2759 return 0;
2760
2761free_and_out:
2762 if (bio)
2763 bio_free(bio, bs);
2764 blk_rq_unprep_clone(rq);
2765
2766 return -ENOMEM;
2767}
2768EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
2769
18887ad9 2770int kblockd_schedule_work(struct request_queue *q, struct work_struct *work)
1da177e4
LT
2771{
2772 return queue_work(kblockd_workqueue, work);
2773}
1da177e4
LT
2774EXPORT_SYMBOL(kblockd_schedule_work);
2775
e43473b7
VG
2776int kblockd_schedule_delayed_work(struct request_queue *q,
2777 struct delayed_work *dwork, unsigned long delay)
2778{
2779 return queue_delayed_work(kblockd_workqueue, dwork, delay);
2780}
2781EXPORT_SYMBOL(kblockd_schedule_delayed_work);
2782
73c10101
JA
2783#define PLUG_MAGIC 0x91827364
2784
75df7136
SJ
2785/**
2786 * blk_start_plug - initialize blk_plug and track it inside the task_struct
2787 * @plug: The &struct blk_plug that needs to be initialized
2788 *
2789 * Description:
2790 * Tracking blk_plug inside the task_struct will help with auto-flushing the
2791 * pending I/O should the task end up blocking between blk_start_plug() and
2792 * blk_finish_plug(). This is important from a performance perspective, but
2793 * also ensures that we don't deadlock. For instance, if the task is blocking
2794 * for a memory allocation, memory reclaim could end up wanting to free a
2795 * page belonging to that request that is currently residing in our private
2796 * plug. By flushing the pending I/O when the process goes to sleep, we avoid
2797 * this kind of deadlock.
2798 */
73c10101
JA
2799void blk_start_plug(struct blk_plug *plug)
2800{
2801 struct task_struct *tsk = current;
2802
2803 plug->magic = PLUG_MAGIC;
2804 INIT_LIST_HEAD(&plug->list);
048c9374 2805 INIT_LIST_HEAD(&plug->cb_list);
73c10101
JA
2806 plug->should_sort = 0;
2807
2808 /*
2809 * If this is a nested plug, don't actually assign it. It will be
2810 * flushed on its own.
2811 */
2812 if (!tsk->plug) {
2813 /*
2814 * Store ordering should not be needed here, since a potential
2815 * preempt will imply a full memory barrier
2816 */
2817 tsk->plug = plug;
2818 }
2819}
2820EXPORT_SYMBOL(blk_start_plug);
2821
2822static int plug_rq_cmp(void *priv, struct list_head *a, struct list_head *b)
2823{
2824 struct request *rqa = container_of(a, struct request, queuelist);
2825 struct request *rqb = container_of(b, struct request, queuelist);
2826
f83e8261 2827 return !(rqa->q <= rqb->q);
73c10101
JA
2828}
2829
49cac01e
JA
2830/*
2831 * If 'from_schedule' is true, then postpone the dispatch of requests
2832 * until a safe kblockd context. We due this to avoid accidental big
2833 * additional stack usage in driver dispatch, in places where the originally
2834 * plugger did not intend it.
2835 */
f6603783 2836static void queue_unplugged(struct request_queue *q, unsigned int depth,
49cac01e 2837 bool from_schedule)
99e22598 2838 __releases(q->queue_lock)
94b5eb28 2839{
49cac01e 2840 trace_block_unplug(q, depth, !from_schedule);
99e22598 2841
8ba61435
TH
2842 /*
2843 * Don't mess with dead queue.
2844 */
2845 if (unlikely(blk_queue_dead(q))) {
2846 spin_unlock(q->queue_lock);
2847 return;
2848 }
2849
99e22598
JA
2850 /*
2851 * If we are punting this to kblockd, then we can safely drop
2852 * the queue_lock before waking kblockd (which needs to take
2853 * this lock).
2854 */
2855 if (from_schedule) {
2856 spin_unlock(q->queue_lock);
24ecfbe2 2857 blk_run_queue_async(q);
99e22598 2858 } else {
24ecfbe2 2859 __blk_run_queue(q);
99e22598
JA
2860 spin_unlock(q->queue_lock);
2861 }
2862
94b5eb28
JA
2863}
2864
048c9374
N
2865static void flush_plug_callbacks(struct blk_plug *plug)
2866{
2867 LIST_HEAD(callbacks);
2868
2869 if (list_empty(&plug->cb_list))
2870 return;
2871
2872 list_splice_init(&plug->cb_list, &callbacks);
2873
2874 while (!list_empty(&callbacks)) {
2875 struct blk_plug_cb *cb = list_first_entry(&callbacks,
2876 struct blk_plug_cb,
2877 list);
2878 list_del(&cb->list);
2879 cb->callback(cb);
2880 }
2881}
2882
49cac01e 2883void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
73c10101
JA
2884{
2885 struct request_queue *q;
2886 unsigned long flags;
2887 struct request *rq;
109b8129 2888 LIST_HEAD(list);
94b5eb28 2889 unsigned int depth;
73c10101
JA
2890
2891 BUG_ON(plug->magic != PLUG_MAGIC);
2892
048c9374 2893 flush_plug_callbacks(plug);
73c10101
JA
2894 if (list_empty(&plug->list))
2895 return;
2896
109b8129
N
2897 list_splice_init(&plug->list, &list);
2898
2899 if (plug->should_sort) {
2900 list_sort(NULL, &list, plug_rq_cmp);
2901 plug->should_sort = 0;
2902 }
73c10101
JA
2903
2904 q = NULL;
94b5eb28 2905 depth = 0;
18811272
JA
2906
2907 /*
2908 * Save and disable interrupts here, to avoid doing it for every
2909 * queue lock we have to take.
2910 */
73c10101 2911 local_irq_save(flags);
109b8129
N
2912 while (!list_empty(&list)) {
2913 rq = list_entry_rq(list.next);
73c10101 2914 list_del_init(&rq->queuelist);
73c10101
JA
2915 BUG_ON(!rq->q);
2916 if (rq->q != q) {
99e22598
JA
2917 /*
2918 * This drops the queue lock
2919 */
2920 if (q)
49cac01e 2921 queue_unplugged(q, depth, from_schedule);
73c10101 2922 q = rq->q;
94b5eb28 2923 depth = 0;
73c10101
JA
2924 spin_lock(q->queue_lock);
2925 }
8ba61435
TH
2926
2927 /*
2928 * Short-circuit if @q is dead
2929 */
2930 if (unlikely(blk_queue_dead(q))) {
2931 __blk_end_request_all(rq, -ENODEV);
2932 continue;
2933 }
2934
73c10101
JA
2935 /*
2936 * rq is already accounted, so use raw insert
2937 */
401a18e9
JA
2938 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA))
2939 __elv_add_request(q, rq, ELEVATOR_INSERT_FLUSH);
2940 else
2941 __elv_add_request(q, rq, ELEVATOR_INSERT_SORT_MERGE);
94b5eb28
JA
2942
2943 depth++;
73c10101
JA
2944 }
2945
99e22598
JA
2946 /*
2947 * This drops the queue lock
2948 */
2949 if (q)
49cac01e 2950 queue_unplugged(q, depth, from_schedule);
73c10101 2951
73c10101
JA
2952 local_irq_restore(flags);
2953}
73c10101
JA
2954
2955void blk_finish_plug(struct blk_plug *plug)
2956{
f6603783 2957 blk_flush_plug_list(plug, false);
73c10101 2958
88b996cd
CH
2959 if (plug == current->plug)
2960 current->plug = NULL;
73c10101 2961}
88b996cd 2962EXPORT_SYMBOL(blk_finish_plug);
73c10101 2963
1da177e4
LT
2964int __init blk_dev_init(void)
2965{
9eb55b03
NK
2966 BUILD_BUG_ON(__REQ_NR_BITS > 8 *
2967 sizeof(((struct request *)0)->cmd_flags));
2968
89b90be2
TH
2969 /* used for unplugging and affects IO latency/throughput - HIGHPRI */
2970 kblockd_workqueue = alloc_workqueue("kblockd",
2971 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
1da177e4
LT
2972 if (!kblockd_workqueue)
2973 panic("Failed to create kblockd\n");
2974
2975 request_cachep = kmem_cache_create("blkdev_requests",
20c2df83 2976 sizeof(struct request), 0, SLAB_PANIC, NULL);
1da177e4 2977
8324aa91 2978 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
165125e1 2979 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
1da177e4 2980
d38ecf93 2981 return 0;
1da177e4 2982}