]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - block/blk-core.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs-2.6
[mirror_ubuntu-bionic-kernel.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>
2056a782 29#include <linux/blktrace_api.h>
c17bb495 30#include <linux/fault-inject.h>
55782138
LZ
31
32#define CREATE_TRACE_POINTS
33#include <trace/events/block.h>
1da177e4 34
8324aa91
JA
35#include "blk.h"
36
0bfc2455 37EXPORT_TRACEPOINT_SYMBOL_GPL(block_remap);
55782138 38EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_complete);
0bfc2455 39
165125e1 40static int __make_request(struct request_queue *q, struct bio *bio);
1da177e4
LT
41
42/*
43 * For the allocated request tables
44 */
5ece6c52 45static struct kmem_cache *request_cachep;
1da177e4
LT
46
47/*
48 * For queue allocation
49 */
6728cb0e 50struct kmem_cache *blk_requestq_cachep;
1da177e4 51
1da177e4
LT
52/*
53 * Controlling structure to kblockd
54 */
ff856bad 55static struct workqueue_struct *kblockd_workqueue;
1da177e4 56
26b8256e
JA
57static void drive_stat_acct(struct request *rq, int new_io)
58{
28f13702 59 struct hd_struct *part;
26b8256e 60 int rw = rq_data_dir(rq);
c9959059 61 int cpu;
26b8256e 62
c2553b58 63 if (!blk_do_io_stat(rq))
26b8256e
JA
64 return;
65
074a7aca 66 cpu = part_stat_lock();
83096ebf 67 part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq));
c9959059 68
28f13702 69 if (!new_io)
074a7aca 70 part_stat_inc(cpu, part, merges[rw]);
28f13702 71 else {
074a7aca
TH
72 part_round_stats(cpu, part);
73 part_inc_in_flight(part);
26b8256e 74 }
e71bf0d0 75
074a7aca 76 part_stat_unlock();
26b8256e
JA
77}
78
8324aa91 79void blk_queue_congestion_threshold(struct request_queue *q)
1da177e4
LT
80{
81 int nr;
82
83 nr = q->nr_requests - (q->nr_requests / 8) + 1;
84 if (nr > q->nr_requests)
85 nr = q->nr_requests;
86 q->nr_congestion_on = nr;
87
88 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
89 if (nr < 1)
90 nr = 1;
91 q->nr_congestion_off = nr;
92}
93
1da177e4
LT
94/**
95 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
96 * @bdev: device
97 *
98 * Locates the passed device's request queue and returns the address of its
99 * backing_dev_info
100 *
101 * Will return NULL if the request queue cannot be located.
102 */
103struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
104{
105 struct backing_dev_info *ret = NULL;
165125e1 106 struct request_queue *q = bdev_get_queue(bdev);
1da177e4
LT
107
108 if (q)
109 ret = &q->backing_dev_info;
110 return ret;
111}
1da177e4
LT
112EXPORT_SYMBOL(blk_get_backing_dev_info);
113
2a4aa30c 114void blk_rq_init(struct request_queue *q, struct request *rq)
1da177e4 115{
1afb20f3
FT
116 memset(rq, 0, sizeof(*rq));
117
1da177e4 118 INIT_LIST_HEAD(&rq->queuelist);
242f9dcb 119 INIT_LIST_HEAD(&rq->timeout_list);
c7c22e4d 120 rq->cpu = -1;
63a71386 121 rq->q = q;
a2dec7b3 122 rq->__sector = (sector_t) -1;
2e662b65
JA
123 INIT_HLIST_NODE(&rq->hash);
124 RB_CLEAR_NODE(&rq->rb_node);
d7e3c324 125 rq->cmd = rq->__cmd;
e2494e1b 126 rq->cmd_len = BLK_MAX_CDB;
63a71386 127 rq->tag = -1;
1da177e4 128 rq->ref_count = 1;
b243ddcb 129 rq->start_time = jiffies;
1da177e4 130}
2a4aa30c 131EXPORT_SYMBOL(blk_rq_init);
1da177e4 132
5bb23a68
N
133static void req_bio_endio(struct request *rq, struct bio *bio,
134 unsigned int nbytes, int error)
1da177e4 135{
165125e1 136 struct request_queue *q = rq->q;
797e7dbb 137
5bb23a68
N
138 if (&q->bar_rq != rq) {
139 if (error)
140 clear_bit(BIO_UPTODATE, &bio->bi_flags);
141 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
142 error = -EIO;
797e7dbb 143
5bb23a68 144 if (unlikely(nbytes > bio->bi_size)) {
6728cb0e 145 printk(KERN_ERR "%s: want %u bytes done, %u left\n",
24c03d47 146 __func__, nbytes, bio->bi_size);
5bb23a68
N
147 nbytes = bio->bi_size;
148 }
797e7dbb 149
08bafc03
KM
150 if (unlikely(rq->cmd_flags & REQ_QUIET))
151 set_bit(BIO_QUIET, &bio->bi_flags);
152
5bb23a68
N
153 bio->bi_size -= nbytes;
154 bio->bi_sector += (nbytes >> 9);
7ba1ba12
MP
155
156 if (bio_integrity(bio))
157 bio_integrity_advance(bio, nbytes);
158
5bb23a68 159 if (bio->bi_size == 0)
6712ecf8 160 bio_endio(bio, error);
5bb23a68
N
161 } else {
162
163 /*
164 * Okay, this is the barrier request in progress, just
165 * record the error;
166 */
167 if (error && !q->orderr)
168 q->orderr = error;
169 }
1da177e4 170}
1da177e4 171
1da177e4
LT
172void blk_dump_rq_flags(struct request *rq, char *msg)
173{
174 int bit;
175
6728cb0e 176 printk(KERN_INFO "%s: dev %s: type=%x, flags=%x\n", msg,
4aff5e23
JA
177 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
178 rq->cmd_flags);
1da177e4 179
83096ebf
TH
180 printk(KERN_INFO " sector %llu, nr/cnr %u/%u\n",
181 (unsigned long long)blk_rq_pos(rq),
182 blk_rq_sectors(rq), blk_rq_cur_sectors(rq));
731ec497 183 printk(KERN_INFO " bio %p, biotail %p, buffer %p, len %u\n",
2e46e8b2 184 rq->bio, rq->biotail, rq->buffer, blk_rq_bytes(rq));
1da177e4 185
4aff5e23 186 if (blk_pc_request(rq)) {
6728cb0e 187 printk(KERN_INFO " cdb: ");
d34c87e4 188 for (bit = 0; bit < BLK_MAX_CDB; bit++)
1da177e4
LT
189 printk("%02x ", rq->cmd[bit]);
190 printk("\n");
191 }
192}
1da177e4
LT
193EXPORT_SYMBOL(blk_dump_rq_flags);
194
1da177e4
LT
195/*
196 * "plug" the device if there are no outstanding requests: this will
197 * force the transfer to start only after we have put all the requests
198 * on the list.
199 *
200 * This is called with interrupts off and no requests on the queue and
201 * with the queue lock held.
202 */
165125e1 203void blk_plug_device(struct request_queue *q)
1da177e4
LT
204{
205 WARN_ON(!irqs_disabled());
206
207 /*
208 * don't plug a stopped queue, it must be paired with blk_start_queue()
209 * which will restart the queueing
210 */
7daac490 211 if (blk_queue_stopped(q))
1da177e4
LT
212 return;
213
e48ec690 214 if (!queue_flag_test_and_set(QUEUE_FLAG_PLUGGED, q)) {
1da177e4 215 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
5f3ea37c 216 trace_block_plug(q);
2056a782 217 }
1da177e4 218}
1da177e4
LT
219EXPORT_SYMBOL(blk_plug_device);
220
6c5e0c4d
JA
221/**
222 * blk_plug_device_unlocked - plug a device without queue lock held
223 * @q: The &struct request_queue to plug
224 *
225 * Description:
226 * Like @blk_plug_device(), but grabs the queue lock and disables
227 * interrupts.
228 **/
229void blk_plug_device_unlocked(struct request_queue *q)
230{
231 unsigned long flags;
232
233 spin_lock_irqsave(q->queue_lock, flags);
234 blk_plug_device(q);
235 spin_unlock_irqrestore(q->queue_lock, flags);
236}
237EXPORT_SYMBOL(blk_plug_device_unlocked);
238
1da177e4
LT
239/*
240 * remove the queue from the plugged list, if present. called with
241 * queue lock held and interrupts disabled.
242 */
165125e1 243int blk_remove_plug(struct request_queue *q)
1da177e4
LT
244{
245 WARN_ON(!irqs_disabled());
246
e48ec690 247 if (!queue_flag_test_and_clear(QUEUE_FLAG_PLUGGED, q))
1da177e4
LT
248 return 0;
249
250 del_timer(&q->unplug_timer);
251 return 1;
252}
1da177e4
LT
253EXPORT_SYMBOL(blk_remove_plug);
254
255/*
256 * remove the plug and let it rip..
257 */
165125e1 258void __generic_unplug_device(struct request_queue *q)
1da177e4 259{
7daac490 260 if (unlikely(blk_queue_stopped(q)))
1da177e4 261 return;
a31a9738 262 if (!blk_remove_plug(q) && !blk_queue_nonrot(q))
1da177e4
LT
263 return;
264
22e2c507 265 q->request_fn(q);
1da177e4 266}
1da177e4
LT
267
268/**
269 * generic_unplug_device - fire a request queue
165125e1 270 * @q: The &struct request_queue in question
1da177e4
LT
271 *
272 * Description:
273 * Linux uses plugging to build bigger requests queues before letting
274 * the device have at them. If a queue is plugged, the I/O scheduler
275 * is still adding and merging requests on the queue. Once the queue
276 * gets unplugged, the request_fn defined for the queue is invoked and
277 * transfers started.
278 **/
165125e1 279void generic_unplug_device(struct request_queue *q)
1da177e4 280{
dbaf2c00
JA
281 if (blk_queue_plugged(q)) {
282 spin_lock_irq(q->queue_lock);
283 __generic_unplug_device(q);
284 spin_unlock_irq(q->queue_lock);
285 }
1da177e4
LT
286}
287EXPORT_SYMBOL(generic_unplug_device);
288
289static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
290 struct page *page)
291{
165125e1 292 struct request_queue *q = bdi->unplug_io_data;
1da177e4 293
2ad8b1ef 294 blk_unplug(q);
1da177e4
LT
295}
296
86db1e29 297void blk_unplug_work(struct work_struct *work)
1da177e4 298{
165125e1
JA
299 struct request_queue *q =
300 container_of(work, struct request_queue, unplug_work);
1da177e4 301
5f3ea37c 302 trace_block_unplug_io(q);
1da177e4
LT
303 q->unplug_fn(q);
304}
305
86db1e29 306void blk_unplug_timeout(unsigned long data)
1da177e4 307{
165125e1 308 struct request_queue *q = (struct request_queue *)data;
1da177e4 309
5f3ea37c 310 trace_block_unplug_timer(q);
18887ad9 311 kblockd_schedule_work(q, &q->unplug_work);
1da177e4
LT
312}
313
2ad8b1ef
AB
314void blk_unplug(struct request_queue *q)
315{
316 /*
317 * devices don't necessarily have an ->unplug_fn defined
318 */
319 if (q->unplug_fn) {
5f3ea37c 320 trace_block_unplug_io(q);
2ad8b1ef
AB
321 q->unplug_fn(q);
322 }
323}
324EXPORT_SYMBOL(blk_unplug);
325
1da177e4
LT
326/**
327 * blk_start_queue - restart a previously stopped queue
165125e1 328 * @q: The &struct request_queue in question
1da177e4
LT
329 *
330 * Description:
331 * blk_start_queue() will clear the stop flag on the queue, and call
332 * the request_fn for the queue if it was in a stopped state when
333 * entered. Also see blk_stop_queue(). Queue lock must be held.
334 **/
165125e1 335void blk_start_queue(struct request_queue *q)
1da177e4 336{
a038e253
PBG
337 WARN_ON(!irqs_disabled());
338
75ad23bc 339 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
a538cd03 340 __blk_run_queue(q);
1da177e4 341}
1da177e4
LT
342EXPORT_SYMBOL(blk_start_queue);
343
344/**
345 * blk_stop_queue - stop a queue
165125e1 346 * @q: The &struct request_queue in question
1da177e4
LT
347 *
348 * Description:
349 * The Linux block layer assumes that a block driver will consume all
350 * entries on the request queue when the request_fn strategy is called.
351 * Often this will not happen, because of hardware limitations (queue
352 * depth settings). If a device driver gets a 'queue full' response,
353 * or if it simply chooses not to queue more I/O at one point, it can
354 * call this function to prevent the request_fn from being called until
355 * the driver has signalled it's ready to go again. This happens by calling
356 * blk_start_queue() to restart queue operations. Queue lock must be held.
357 **/
165125e1 358void blk_stop_queue(struct request_queue *q)
1da177e4
LT
359{
360 blk_remove_plug(q);
75ad23bc 361 queue_flag_set(QUEUE_FLAG_STOPPED, q);
1da177e4
LT
362}
363EXPORT_SYMBOL(blk_stop_queue);
364
365/**
366 * blk_sync_queue - cancel any pending callbacks on a queue
367 * @q: the queue
368 *
369 * Description:
370 * The block layer may perform asynchronous callback activity
371 * on a queue, such as calling the unplug function after a timeout.
372 * A block device may call blk_sync_queue to ensure that any
373 * such activity is cancelled, thus allowing it to release resources
59c51591 374 * that the callbacks might use. The caller must already have made sure
1da177e4
LT
375 * that its ->make_request_fn will not re-add plugging prior to calling
376 * this function.
377 *
378 */
379void blk_sync_queue(struct request_queue *q)
380{
381 del_timer_sync(&q->unplug_timer);
70ed28b9 382 del_timer_sync(&q->timeout);
64d01dc9 383 cancel_work_sync(&q->unplug_work);
1da177e4
LT
384}
385EXPORT_SYMBOL(blk_sync_queue);
386
387/**
80a4b58e 388 * __blk_run_queue - run a single device queue
1da177e4 389 * @q: The queue to run
80a4b58e
JA
390 *
391 * Description:
392 * See @blk_run_queue. This variant must be called with the queue lock
393 * held and interrupts disabled.
394 *
1da177e4 395 */
75ad23bc 396void __blk_run_queue(struct request_queue *q)
1da177e4 397{
1da177e4 398 blk_remove_plug(q);
dac07ec1 399
a538cd03
TH
400 if (unlikely(blk_queue_stopped(q)))
401 return;
402
403 if (elv_queue_empty(q))
404 return;
405
dac07ec1
JA
406 /*
407 * Only recurse once to avoid overrunning the stack, let the unplug
408 * handling reinvoke the handler shortly if we already got there.
409 */
a538cd03
TH
410 if (!queue_flag_test_and_set(QUEUE_FLAG_REENTER, q)) {
411 q->request_fn(q);
412 queue_flag_clear(QUEUE_FLAG_REENTER, q);
413 } else {
414 queue_flag_set(QUEUE_FLAG_PLUGGED, q);
415 kblockd_schedule_work(q, &q->unplug_work);
416 }
75ad23bc
NP
417}
418EXPORT_SYMBOL(__blk_run_queue);
dac07ec1 419
75ad23bc
NP
420/**
421 * blk_run_queue - run a single device queue
422 * @q: The queue to run
80a4b58e
JA
423 *
424 * Description:
425 * Invoke request handling on this queue, if it has pending work to do.
a7f55792 426 * May be used to restart queueing when a request has completed.
75ad23bc
NP
427 */
428void blk_run_queue(struct request_queue *q)
429{
430 unsigned long flags;
431
432 spin_lock_irqsave(q->queue_lock, flags);
433 __blk_run_queue(q);
1da177e4
LT
434 spin_unlock_irqrestore(q->queue_lock, flags);
435}
436EXPORT_SYMBOL(blk_run_queue);
437
165125e1 438void blk_put_queue(struct request_queue *q)
483f4afc
AV
439{
440 kobject_put(&q->kobj);
441}
483f4afc 442
6728cb0e 443void blk_cleanup_queue(struct request_queue *q)
483f4afc 444{
e3335de9
JA
445 /*
446 * We know we have process context here, so we can be a little
447 * cautious and ensure that pending block actions on this device
448 * are done before moving on. Going into this function, we should
449 * not have processes doing IO to this device.
450 */
451 blk_sync_queue(q);
452
483f4afc 453 mutex_lock(&q->sysfs_lock);
75ad23bc 454 queue_flag_set_unlocked(QUEUE_FLAG_DEAD, q);
483f4afc
AV
455 mutex_unlock(&q->sysfs_lock);
456
457 if (q->elevator)
458 elevator_exit(q->elevator);
459
460 blk_put_queue(q);
461}
1da177e4
LT
462EXPORT_SYMBOL(blk_cleanup_queue);
463
165125e1 464static int blk_init_free_list(struct request_queue *q)
1da177e4
LT
465{
466 struct request_list *rl = &q->rq;
467
1faa16d2
JA
468 rl->count[BLK_RW_SYNC] = rl->count[BLK_RW_ASYNC] = 0;
469 rl->starved[BLK_RW_SYNC] = rl->starved[BLK_RW_ASYNC] = 0;
cb98fc8b 470 rl->elvpriv = 0;
1faa16d2
JA
471 init_waitqueue_head(&rl->wait[BLK_RW_SYNC]);
472 init_waitqueue_head(&rl->wait[BLK_RW_ASYNC]);
1da177e4 473
1946089a
CL
474 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
475 mempool_free_slab, request_cachep, q->node);
1da177e4
LT
476
477 if (!rl->rq_pool)
478 return -ENOMEM;
479
480 return 0;
481}
482
165125e1 483struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
1da177e4 484{
1946089a
CL
485 return blk_alloc_queue_node(gfp_mask, -1);
486}
487EXPORT_SYMBOL(blk_alloc_queue);
1da177e4 488
165125e1 489struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
1946089a 490{
165125e1 491 struct request_queue *q;
e0bf68dd 492 int err;
1946089a 493
8324aa91 494 q = kmem_cache_alloc_node(blk_requestq_cachep,
94f6030c 495 gfp_mask | __GFP_ZERO, node_id);
1da177e4
LT
496 if (!q)
497 return NULL;
498
e0bf68dd
PZ
499 q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
500 q->backing_dev_info.unplug_io_data = q;
501 err = bdi_init(&q->backing_dev_info);
502 if (err) {
8324aa91 503 kmem_cache_free(blk_requestq_cachep, q);
e0bf68dd
PZ
504 return NULL;
505 }
506
1da177e4 507 init_timer(&q->unplug_timer);
242f9dcb
JA
508 setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
509 INIT_LIST_HEAD(&q->timeout_list);
713ada9b 510 INIT_WORK(&q->unplug_work, blk_unplug_work);
483f4afc 511
8324aa91 512 kobject_init(&q->kobj, &blk_queue_ktype);
1da177e4 513
483f4afc 514 mutex_init(&q->sysfs_lock);
e7e72bf6 515 spin_lock_init(&q->__queue_lock);
483f4afc 516
1da177e4
LT
517 return q;
518}
1946089a 519EXPORT_SYMBOL(blk_alloc_queue_node);
1da177e4
LT
520
521/**
522 * blk_init_queue - prepare a request queue for use with a block device
523 * @rfn: The function to be called to process requests that have been
524 * placed on the queue.
525 * @lock: Request queue spin lock
526 *
527 * Description:
528 * If a block device wishes to use the standard request handling procedures,
529 * which sorts requests and coalesces adjacent requests, then it must
530 * call blk_init_queue(). The function @rfn will be called when there
531 * are requests on the queue that need to be processed. If the device
532 * supports plugging, then @rfn may not be called immediately when requests
533 * are available on the queue, but may be called at some time later instead.
534 * Plugged queues are generally unplugged when a buffer belonging to one
535 * of the requests on the queue is needed, or due to memory pressure.
536 *
537 * @rfn is not required, or even expected, to remove all requests off the
538 * queue, but only as many as it can handle at a time. If it does leave
539 * requests on the queue, it is responsible for arranging that the requests
540 * get dealt with eventually.
541 *
542 * The queue spin lock must be held while manipulating the requests on the
a038e253
PBG
543 * request queue; this lock will be taken also from interrupt context, so irq
544 * disabling is needed for it.
1da177e4 545 *
710027a4 546 * Function returns a pointer to the initialized request queue, or %NULL if
1da177e4
LT
547 * it didn't succeed.
548 *
549 * Note:
550 * blk_init_queue() must be paired with a blk_cleanup_queue() call
551 * when the block device is deactivated (such as at module unload).
552 **/
1946089a 553
165125e1 554struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
1da177e4 555{
1946089a
CL
556 return blk_init_queue_node(rfn, lock, -1);
557}
558EXPORT_SYMBOL(blk_init_queue);
559
165125e1 560struct request_queue *
1946089a
CL
561blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
562{
165125e1 563 struct request_queue *q = blk_alloc_queue_node(GFP_KERNEL, node_id);
1da177e4
LT
564
565 if (!q)
566 return NULL;
567
1946089a 568 q->node = node_id;
8669aafd 569 if (blk_init_free_list(q)) {
8324aa91 570 kmem_cache_free(blk_requestq_cachep, q);
8669aafd
AV
571 return NULL;
572 }
1da177e4 573
152587de
JA
574 /*
575 * if caller didn't supply a lock, they get per-queue locking with
576 * our embedded lock
577 */
e7e72bf6 578 if (!lock)
152587de 579 lock = &q->__queue_lock;
152587de 580
1da177e4 581 q->request_fn = rfn;
1da177e4
LT
582 q->prep_rq_fn = NULL;
583 q->unplug_fn = generic_unplug_device;
bc58ba94 584 q->queue_flags = QUEUE_FLAG_DEFAULT;
1da177e4
LT
585 q->queue_lock = lock;
586
f3b144aa
JA
587 /*
588 * This also sets hw/phys segments, boundary and size
589 */
1da177e4 590 blk_queue_make_request(q, __make_request);
1da177e4 591
44ec9542
AS
592 q->sg_reserved_size = INT_MAX;
593
abf54393
FT
594 blk_set_cmd_filter_defaults(&q->cmd_filter);
595
1da177e4
LT
596 /*
597 * all done
598 */
599 if (!elevator_init(q, NULL)) {
600 blk_queue_congestion_threshold(q);
601 return q;
602 }
603
8669aafd 604 blk_put_queue(q);
1da177e4
LT
605 return NULL;
606}
1946089a 607EXPORT_SYMBOL(blk_init_queue_node);
1da177e4 608
165125e1 609int blk_get_queue(struct request_queue *q)
1da177e4 610{
fde6ad22 611 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
483f4afc 612 kobject_get(&q->kobj);
1da177e4
LT
613 return 0;
614 }
615
616 return 1;
617}
1da177e4 618
165125e1 619static inline void blk_free_request(struct request_queue *q, struct request *rq)
1da177e4 620{
4aff5e23 621 if (rq->cmd_flags & REQ_ELVPRIV)
cb98fc8b 622 elv_put_request(q, rq);
1da177e4
LT
623 mempool_free(rq, q->rq.rq_pool);
624}
625
1ea25ecb 626static struct request *
42dad764 627blk_alloc_request(struct request_queue *q, int flags, int priv, gfp_t gfp_mask)
1da177e4
LT
628{
629 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
630
631 if (!rq)
632 return NULL;
633
2a4aa30c 634 blk_rq_init(q, rq);
1afb20f3 635
42dad764 636 rq->cmd_flags = flags | REQ_ALLOCED;
1da177e4 637
cb98fc8b 638 if (priv) {
cb78b285 639 if (unlikely(elv_set_request(q, rq, gfp_mask))) {
cb98fc8b
TH
640 mempool_free(rq, q->rq.rq_pool);
641 return NULL;
642 }
4aff5e23 643 rq->cmd_flags |= REQ_ELVPRIV;
cb98fc8b 644 }
1da177e4 645
cb98fc8b 646 return rq;
1da177e4
LT
647}
648
649/*
650 * ioc_batching returns true if the ioc is a valid batching request and
651 * should be given priority access to a request.
652 */
165125e1 653static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
1da177e4
LT
654{
655 if (!ioc)
656 return 0;
657
658 /*
659 * Make sure the process is able to allocate at least 1 request
660 * even if the batch times out, otherwise we could theoretically
661 * lose wakeups.
662 */
663 return ioc->nr_batch_requests == q->nr_batching ||
664 (ioc->nr_batch_requests > 0
665 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
666}
667
668/*
669 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
670 * will cause the process to be a "batcher" on all queues in the system. This
671 * is the behaviour we want though - once it gets a wakeup it should be given
672 * a nice run.
673 */
165125e1 674static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
1da177e4
LT
675{
676 if (!ioc || ioc_batching(q, ioc))
677 return;
678
679 ioc->nr_batch_requests = q->nr_batching;
680 ioc->last_waited = jiffies;
681}
682
1faa16d2 683static void __freed_request(struct request_queue *q, int sync)
1da177e4
LT
684{
685 struct request_list *rl = &q->rq;
686
1faa16d2
JA
687 if (rl->count[sync] < queue_congestion_off_threshold(q))
688 blk_clear_queue_congested(q, sync);
1da177e4 689
1faa16d2
JA
690 if (rl->count[sync] + 1 <= q->nr_requests) {
691 if (waitqueue_active(&rl->wait[sync]))
692 wake_up(&rl->wait[sync]);
1da177e4 693
1faa16d2 694 blk_clear_queue_full(q, sync);
1da177e4
LT
695 }
696}
697
698/*
699 * A request has just been released. Account for it, update the full and
700 * congestion status, wake up any waiters. Called under q->queue_lock.
701 */
1faa16d2 702static void freed_request(struct request_queue *q, int sync, int priv)
1da177e4
LT
703{
704 struct request_list *rl = &q->rq;
705
1faa16d2 706 rl->count[sync]--;
cb98fc8b
TH
707 if (priv)
708 rl->elvpriv--;
1da177e4 709
1faa16d2 710 __freed_request(q, sync);
1da177e4 711
1faa16d2
JA
712 if (unlikely(rl->starved[sync ^ 1]))
713 __freed_request(q, sync ^ 1);
1da177e4
LT
714}
715
1da177e4 716/*
d6344532
NP
717 * Get a free request, queue_lock must be held.
718 * Returns NULL on failure, with queue_lock held.
719 * Returns !NULL on success, with queue_lock *not held*.
1da177e4 720 */
165125e1 721static struct request *get_request(struct request_queue *q, int rw_flags,
7749a8d4 722 struct bio *bio, gfp_t gfp_mask)
1da177e4
LT
723{
724 struct request *rq = NULL;
725 struct request_list *rl = &q->rq;
88ee5ef1 726 struct io_context *ioc = NULL;
1faa16d2 727 const bool is_sync = rw_is_sync(rw_flags) != 0;
88ee5ef1
JA
728 int may_queue, priv;
729
7749a8d4 730 may_queue = elv_may_queue(q, rw_flags);
88ee5ef1
JA
731 if (may_queue == ELV_MQUEUE_NO)
732 goto rq_starved;
733
1faa16d2
JA
734 if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) {
735 if (rl->count[is_sync]+1 >= q->nr_requests) {
b5deef90 736 ioc = current_io_context(GFP_ATOMIC, q->node);
88ee5ef1
JA
737 /*
738 * The queue will fill after this allocation, so set
739 * it as full, and mark this process as "batching".
740 * This process will be allowed to complete a batch of
741 * requests, others will be blocked.
742 */
1faa16d2 743 if (!blk_queue_full(q, is_sync)) {
88ee5ef1 744 ioc_set_batching(q, ioc);
1faa16d2 745 blk_set_queue_full(q, is_sync);
88ee5ef1
JA
746 } else {
747 if (may_queue != ELV_MQUEUE_MUST
748 && !ioc_batching(q, ioc)) {
749 /*
750 * The queue is full and the allocating
751 * process is not a "batcher", and not
752 * exempted by the IO scheduler
753 */
754 goto out;
755 }
756 }
1da177e4 757 }
1faa16d2 758 blk_set_queue_congested(q, is_sync);
1da177e4
LT
759 }
760
082cf69e
JA
761 /*
762 * Only allow batching queuers to allocate up to 50% over the defined
763 * limit of requests, otherwise we could have thousands of requests
764 * allocated with any setting of ->nr_requests
765 */
1faa16d2 766 if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
082cf69e 767 goto out;
fd782a4a 768
1faa16d2
JA
769 rl->count[is_sync]++;
770 rl->starved[is_sync] = 0;
cb98fc8b 771
64521d1a 772 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
cb98fc8b
TH
773 if (priv)
774 rl->elvpriv++;
775
42dad764
JM
776 if (blk_queue_io_stat(q))
777 rw_flags |= REQ_IO_STAT;
1da177e4
LT
778 spin_unlock_irq(q->queue_lock);
779
7749a8d4 780 rq = blk_alloc_request(q, rw_flags, priv, gfp_mask);
88ee5ef1 781 if (unlikely(!rq)) {
1da177e4
LT
782 /*
783 * Allocation failed presumably due to memory. Undo anything
784 * we might have messed up.
785 *
786 * Allocating task should really be put onto the front of the
787 * wait queue, but this is pretty rare.
788 */
789 spin_lock_irq(q->queue_lock);
1faa16d2 790 freed_request(q, is_sync, priv);
1da177e4
LT
791
792 /*
793 * in the very unlikely event that allocation failed and no
794 * requests for this direction was pending, mark us starved
795 * so that freeing of a request in the other direction will
796 * notice us. another possible fix would be to split the
797 * rq mempool into READ and WRITE
798 */
799rq_starved:
1faa16d2
JA
800 if (unlikely(rl->count[is_sync] == 0))
801 rl->starved[is_sync] = 1;
1da177e4 802
1da177e4
LT
803 goto out;
804 }
805
88ee5ef1
JA
806 /*
807 * ioc may be NULL here, and ioc_batching will be false. That's
808 * OK, if the queue is under the request limit then requests need
809 * not count toward the nr_batch_requests limit. There will always
810 * be some limit enforced by BLK_BATCH_TIME.
811 */
1da177e4
LT
812 if (ioc_batching(q, ioc))
813 ioc->nr_batch_requests--;
6728cb0e 814
1faa16d2 815 trace_block_getrq(q, bio, rw_flags & 1);
1da177e4 816out:
1da177e4
LT
817 return rq;
818}
819
820/*
821 * No available requests for this queue, unplug the device and wait for some
822 * requests to become available.
d6344532
NP
823 *
824 * Called with q->queue_lock held, and returns with it unlocked.
1da177e4 825 */
165125e1 826static struct request *get_request_wait(struct request_queue *q, int rw_flags,
22e2c507 827 struct bio *bio)
1da177e4 828{
1faa16d2 829 const bool is_sync = rw_is_sync(rw_flags) != 0;
1da177e4
LT
830 struct request *rq;
831
7749a8d4 832 rq = get_request(q, rw_flags, bio, GFP_NOIO);
450991bc
NP
833 while (!rq) {
834 DEFINE_WAIT(wait);
05caf8db 835 struct io_context *ioc;
1da177e4
LT
836 struct request_list *rl = &q->rq;
837
1faa16d2 838 prepare_to_wait_exclusive(&rl->wait[is_sync], &wait,
1da177e4
LT
839 TASK_UNINTERRUPTIBLE);
840
1faa16d2 841 trace_block_sleeprq(q, bio, rw_flags & 1);
1da177e4 842
05caf8db
ZY
843 __generic_unplug_device(q);
844 spin_unlock_irq(q->queue_lock);
845 io_schedule();
1da177e4 846
05caf8db
ZY
847 /*
848 * After sleeping, we become a "batching" process and
849 * will be able to allocate at least one request, and
850 * up to a big batch of them for a small period time.
851 * See ioc_batching, ioc_set_batching
852 */
853 ioc = current_io_context(GFP_NOIO, q->node);
854 ioc_set_batching(q, ioc);
d6344532 855
05caf8db 856 spin_lock_irq(q->queue_lock);
1faa16d2 857 finish_wait(&rl->wait[is_sync], &wait);
05caf8db
ZY
858
859 rq = get_request(q, rw_flags, bio, GFP_NOIO);
860 };
1da177e4
LT
861
862 return rq;
863}
864
165125e1 865struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
1da177e4
LT
866{
867 struct request *rq;
868
869 BUG_ON(rw != READ && rw != WRITE);
870
d6344532
NP
871 spin_lock_irq(q->queue_lock);
872 if (gfp_mask & __GFP_WAIT) {
22e2c507 873 rq = get_request_wait(q, rw, NULL);
d6344532 874 } else {
22e2c507 875 rq = get_request(q, rw, NULL, gfp_mask);
d6344532
NP
876 if (!rq)
877 spin_unlock_irq(q->queue_lock);
878 }
879 /* q->queue_lock is unlocked at this point */
1da177e4
LT
880
881 return rq;
882}
1da177e4
LT
883EXPORT_SYMBOL(blk_get_request);
884
dc72ef4a 885/**
79eb63e9
BH
886 * blk_make_request - given a bio, allocate a corresponding struct request.
887 *
888 * @bio: The bio describing the memory mappings that will be submitted for IO.
889 * It may be a chained-bio properly constructed by block/bio layer.
dc72ef4a 890 *
79eb63e9
BH
891 * blk_make_request is the parallel of generic_make_request for BLOCK_PC
892 * type commands. Where the struct request needs to be farther initialized by
893 * the caller. It is passed a &struct bio, which describes the memory info of
894 * the I/O transfer.
dc72ef4a 895 *
79eb63e9
BH
896 * The caller of blk_make_request must make sure that bi_io_vec
897 * are set to describe the memory buffers. That bio_data_dir() will return
898 * the needed direction of the request. (And all bio's in the passed bio-chain
899 * are properly set accordingly)
900 *
901 * If called under none-sleepable conditions, mapped bio buffers must not
902 * need bouncing, by calling the appropriate masked or flagged allocator,
903 * suitable for the target device. Otherwise the call to blk_queue_bounce will
904 * BUG.
53674ac5
JA
905 *
906 * WARNING: When allocating/cloning a bio-chain, careful consideration should be
907 * given to how you allocate bios. In particular, you cannot use __GFP_WAIT for
908 * anything but the first bio in the chain. Otherwise you risk waiting for IO
909 * completion of a bio that hasn't been submitted yet, thus resulting in a
910 * deadlock. Alternatively bios should be allocated using bio_kmalloc() instead
911 * of bio_alloc(), as that avoids the mempool deadlock.
912 * If possible a big IO should be split into smaller parts when allocation
913 * fails. Partial allocation should not be an error, or you risk a live-lock.
dc72ef4a 914 */
79eb63e9
BH
915struct request *blk_make_request(struct request_queue *q, struct bio *bio,
916 gfp_t gfp_mask)
dc72ef4a 917{
79eb63e9
BH
918 struct request *rq = blk_get_request(q, bio_data_dir(bio), gfp_mask);
919
920 if (unlikely(!rq))
921 return ERR_PTR(-ENOMEM);
922
923 for_each_bio(bio) {
924 struct bio *bounce_bio = bio;
925 int ret;
926
927 blk_queue_bounce(q, &bounce_bio);
928 ret = blk_rq_append_bio(q, rq, bounce_bio);
929 if (unlikely(ret)) {
930 blk_put_request(rq);
931 return ERR_PTR(ret);
932 }
933 }
934
935 return rq;
dc72ef4a 936}
79eb63e9 937EXPORT_SYMBOL(blk_make_request);
dc72ef4a 938
1da177e4
LT
939/**
940 * blk_requeue_request - put a request back on queue
941 * @q: request queue where request should be inserted
942 * @rq: request to be inserted
943 *
944 * Description:
945 * Drivers often keep queueing requests until the hardware cannot accept
946 * more, when that condition happens we need to put the request back
947 * on the queue. Must be called with queue lock held.
948 */
165125e1 949void blk_requeue_request(struct request_queue *q, struct request *rq)
1da177e4 950{
242f9dcb
JA
951 blk_delete_timer(rq);
952 blk_clear_rq_complete(rq);
5f3ea37c 953 trace_block_rq_requeue(q, rq);
2056a782 954
1da177e4
LT
955 if (blk_rq_tagged(rq))
956 blk_queue_end_tag(q, rq);
957
ba396a6c
JB
958 BUG_ON(blk_queued_rq(rq));
959
1da177e4
LT
960 elv_requeue_request(q, rq);
961}
1da177e4
LT
962EXPORT_SYMBOL(blk_requeue_request);
963
964/**
710027a4 965 * blk_insert_request - insert a special request into a request queue
1da177e4
LT
966 * @q: request queue where request should be inserted
967 * @rq: request to be inserted
968 * @at_head: insert request at head or tail of queue
969 * @data: private data
1da177e4
LT
970 *
971 * Description:
972 * Many block devices need to execute commands asynchronously, so they don't
973 * block the whole kernel from preemption during request execution. This is
974 * accomplished normally by inserting aritficial requests tagged as
710027a4
RD
975 * REQ_TYPE_SPECIAL in to the corresponding request queue, and letting them
976 * be scheduled for actual execution by the request queue.
1da177e4
LT
977 *
978 * We have the option of inserting the head or the tail of the queue.
979 * Typically we use the tail for new ioctls and so forth. We use the head
980 * of the queue for things like a QUEUE_FULL message from a device, or a
981 * host that is unable to accept a particular command.
982 */
165125e1 983void blk_insert_request(struct request_queue *q, struct request *rq,
867d1191 984 int at_head, void *data)
1da177e4 985{
867d1191 986 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
1da177e4
LT
987 unsigned long flags;
988
989 /*
990 * tell I/O scheduler that this isn't a regular read/write (ie it
991 * must not attempt merges on this) and that it acts as a soft
992 * barrier
993 */
4aff5e23 994 rq->cmd_type = REQ_TYPE_SPECIAL;
1da177e4
LT
995
996 rq->special = data;
997
998 spin_lock_irqsave(q->queue_lock, flags);
999
1000 /*
1001 * If command is tagged, release the tag
1002 */
867d1191
TH
1003 if (blk_rq_tagged(rq))
1004 blk_queue_end_tag(q, rq);
1da177e4 1005
b238b3d4 1006 drive_stat_acct(rq, 1);
867d1191 1007 __elv_add_request(q, rq, where, 0);
a7f55792 1008 __blk_run_queue(q);
1da177e4
LT
1009 spin_unlock_irqrestore(q->queue_lock, flags);
1010}
1da177e4
LT
1011EXPORT_SYMBOL(blk_insert_request);
1012
1da177e4
LT
1013/*
1014 * add-request adds a request to the linked list.
1015 * queue lock is held and interrupts disabled, as we muck with the
1016 * request queue list.
1017 */
6728cb0e 1018static inline void add_request(struct request_queue *q, struct request *req)
1da177e4 1019{
b238b3d4 1020 drive_stat_acct(req, 1);
1da177e4 1021
1da177e4
LT
1022 /*
1023 * elevator indicated where it wants this request to be
1024 * inserted at elevator_merge time
1025 */
1026 __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
1027}
6728cb0e 1028
074a7aca
TH
1029static void part_round_stats_single(int cpu, struct hd_struct *part,
1030 unsigned long now)
1031{
1032 if (now == part->stamp)
1033 return;
1034
1035 if (part->in_flight) {
1036 __part_stat_add(cpu, part, time_in_queue,
1037 part->in_flight * (now - part->stamp));
1038 __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
1039 }
1040 part->stamp = now;
1041}
1042
1043/**
496aa8a9
RD
1044 * part_round_stats() - Round off the performance stats on a struct disk_stats.
1045 * @cpu: cpu number for stats access
1046 * @part: target partition
1da177e4
LT
1047 *
1048 * The average IO queue length and utilisation statistics are maintained
1049 * by observing the current state of the queue length and the amount of
1050 * time it has been in this state for.
1051 *
1052 * Normally, that accounting is done on IO completion, but that can result
1053 * in more than a second's worth of IO being accounted for within any one
1054 * second, leading to >100% utilisation. To deal with that, we call this
1055 * function to do a round-off before returning the results when reading
1056 * /proc/diskstats. This accounts immediately for all queue usage up to
1057 * the current jiffies and restarts the counters again.
1058 */
c9959059 1059void part_round_stats(int cpu, struct hd_struct *part)
6f2576af
JM
1060{
1061 unsigned long now = jiffies;
1062
074a7aca
TH
1063 if (part->partno)
1064 part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
1065 part_round_stats_single(cpu, part, now);
6f2576af 1066}
074a7aca 1067EXPORT_SYMBOL_GPL(part_round_stats);
6f2576af 1068
1da177e4
LT
1069/*
1070 * queue lock must be held
1071 */
165125e1 1072void __blk_put_request(struct request_queue *q, struct request *req)
1da177e4 1073{
1da177e4
LT
1074 if (unlikely(!q))
1075 return;
1076 if (unlikely(--req->ref_count))
1077 return;
1078
8922e16c
TH
1079 elv_completed_request(q, req);
1080
1cd96c24
BH
1081 /* this is a bio leak */
1082 WARN_ON(req->bio != NULL);
1083
1da177e4
LT
1084 /*
1085 * Request may not have originated from ll_rw_blk. if not,
1086 * it didn't come out of our reserved rq pools
1087 */
49171e5c 1088 if (req->cmd_flags & REQ_ALLOCED) {
1faa16d2 1089 int is_sync = rq_is_sync(req) != 0;
4aff5e23 1090 int priv = req->cmd_flags & REQ_ELVPRIV;
1da177e4 1091
1da177e4 1092 BUG_ON(!list_empty(&req->queuelist));
9817064b 1093 BUG_ON(!hlist_unhashed(&req->hash));
1da177e4
LT
1094
1095 blk_free_request(q, req);
1faa16d2 1096 freed_request(q, is_sync, priv);
1da177e4
LT
1097 }
1098}
6e39b69e
MC
1099EXPORT_SYMBOL_GPL(__blk_put_request);
1100
1da177e4
LT
1101void blk_put_request(struct request *req)
1102{
8922e16c 1103 unsigned long flags;
165125e1 1104 struct request_queue *q = req->q;
8922e16c 1105
52a93ba8
FT
1106 spin_lock_irqsave(q->queue_lock, flags);
1107 __blk_put_request(q, req);
1108 spin_unlock_irqrestore(q->queue_lock, flags);
1da177e4 1109}
1da177e4
LT
1110EXPORT_SYMBOL(blk_put_request);
1111
86db1e29 1112void init_request_from_bio(struct request *req, struct bio *bio)
52d9e675 1113{
c7c22e4d 1114 req->cpu = bio->bi_comp_cpu;
4aff5e23 1115 req->cmd_type = REQ_TYPE_FS;
52d9e675
TH
1116
1117 /*
1118 * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
1119 */
6000a368
MC
1120 if (bio_rw_ahead(bio))
1121 req->cmd_flags |= (REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
1122 REQ_FAILFAST_DRIVER);
1123 if (bio_failfast_dev(bio))
1124 req->cmd_flags |= REQ_FAILFAST_DEV;
1125 if (bio_failfast_transport(bio))
1126 req->cmd_flags |= REQ_FAILFAST_TRANSPORT;
1127 if (bio_failfast_driver(bio))
1128 req->cmd_flags |= REQ_FAILFAST_DRIVER;
52d9e675 1129
fb2dce86 1130 if (unlikely(bio_discard(bio))) {
e17fc0a1
DW
1131 req->cmd_flags |= REQ_DISCARD;
1132 if (bio_barrier(bio))
1133 req->cmd_flags |= REQ_SOFTBARRIER;
fb2dce86 1134 req->q->prepare_discard_fn(req->q, req);
e17fc0a1 1135 } else if (unlikely(bio_barrier(bio)))
e4025f6c 1136 req->cmd_flags |= REQ_HARDBARRIER;
52d9e675 1137
b31dc66a 1138 if (bio_sync(bio))
4aff5e23 1139 req->cmd_flags |= REQ_RW_SYNC;
5404bc7a
JA
1140 if (bio_rw_meta(bio))
1141 req->cmd_flags |= REQ_RW_META;
aeb6fafb
JA
1142 if (bio_noidle(bio))
1143 req->cmd_flags |= REQ_NOIDLE;
b31dc66a 1144
52d9e675 1145 req->errors = 0;
a2dec7b3 1146 req->__sector = bio->bi_sector;
52d9e675 1147 req->ioprio = bio_prio(bio);
bc1c56fd 1148 blk_rq_bio_prep(req->q, req, bio);
52d9e675
TH
1149}
1150
644b2d99
JA
1151/*
1152 * Only disabling plugging for non-rotational devices if it does tagging
1153 * as well, otherwise we do need the proper merging
1154 */
1155static inline bool queue_should_plug(struct request_queue *q)
1156{
1157 return !(blk_queue_nonrot(q) && blk_queue_tagged(q));
1158}
1159
165125e1 1160static int __make_request(struct request_queue *q, struct bio *bio)
1da177e4 1161{
450991bc 1162 struct request *req;
2e46e8b2
TH
1163 int el_ret;
1164 unsigned int bytes = bio->bi_size;
51da90fc
JA
1165 const unsigned short prio = bio_prio(bio);
1166 const int sync = bio_sync(bio);
213d9417 1167 const int unplug = bio_unplug(bio);
7749a8d4 1168 int rw_flags;
1da177e4 1169
1da177e4
LT
1170 /*
1171 * low level driver can indicate that it wants pages above a
1172 * certain limit bounced to low memory (ie for highmem, or even
1173 * ISA dma in theory)
1174 */
1175 blk_queue_bounce(q, &bio);
1176
1da177e4
LT
1177 spin_lock_irq(q->queue_lock);
1178
a7384677 1179 if (unlikely(bio_barrier(bio)) || elv_queue_empty(q))
1da177e4
LT
1180 goto get_rq;
1181
1182 el_ret = elv_merge(q, &req, bio);
1183 switch (el_ret) {
6728cb0e
JA
1184 case ELEVATOR_BACK_MERGE:
1185 BUG_ON(!rq_mergeable(req));
1da177e4 1186
6728cb0e
JA
1187 if (!ll_back_merge_fn(q, req, bio))
1188 break;
1da177e4 1189
5f3ea37c 1190 trace_block_bio_backmerge(q, bio);
2056a782 1191
6728cb0e
JA
1192 req->biotail->bi_next = bio;
1193 req->biotail = bio;
a2dec7b3 1194 req->__data_len += bytes;
6728cb0e 1195 req->ioprio = ioprio_best(req->ioprio, prio);
ab780f1e
JA
1196 if (!blk_rq_cpu_valid(req))
1197 req->cpu = bio->bi_comp_cpu;
6728cb0e
JA
1198 drive_stat_acct(req, 0);
1199 if (!attempt_back_merge(q, req))
1200 elv_merged_request(q, req, el_ret);
1201 goto out;
1da177e4 1202
6728cb0e
JA
1203 case ELEVATOR_FRONT_MERGE:
1204 BUG_ON(!rq_mergeable(req));
1da177e4 1205
6728cb0e
JA
1206 if (!ll_front_merge_fn(q, req, bio))
1207 break;
1da177e4 1208
5f3ea37c 1209 trace_block_bio_frontmerge(q, bio);
2056a782 1210
6728cb0e
JA
1211 bio->bi_next = req->bio;
1212 req->bio = bio;
1da177e4 1213
6728cb0e
JA
1214 /*
1215 * may not be valid. if the low level driver said
1216 * it didn't need a bounce buffer then it better
1217 * not touch req->buffer either...
1218 */
1219 req->buffer = bio_data(bio);
a2dec7b3
TH
1220 req->__sector = bio->bi_sector;
1221 req->__data_len += bytes;
6728cb0e 1222 req->ioprio = ioprio_best(req->ioprio, prio);
ab780f1e
JA
1223 if (!blk_rq_cpu_valid(req))
1224 req->cpu = bio->bi_comp_cpu;
6728cb0e
JA
1225 drive_stat_acct(req, 0);
1226 if (!attempt_front_merge(q, req))
1227 elv_merged_request(q, req, el_ret);
1228 goto out;
1229
1230 /* ELV_NO_MERGE: elevator says don't/can't merge. */
1231 default:
1232 ;
1da177e4
LT
1233 }
1234
450991bc 1235get_rq:
7749a8d4
JA
1236 /*
1237 * This sync check and mask will be re-done in init_request_from_bio(),
1238 * but we need to set it earlier to expose the sync flag to the
1239 * rq allocator and io schedulers.
1240 */
1241 rw_flags = bio_data_dir(bio);
1242 if (sync)
1243 rw_flags |= REQ_RW_SYNC;
1244
1da177e4 1245 /*
450991bc 1246 * Grab a free request. This is might sleep but can not fail.
d6344532 1247 * Returns with the queue unlocked.
450991bc 1248 */
7749a8d4 1249 req = get_request_wait(q, rw_flags, bio);
d6344532 1250
450991bc
NP
1251 /*
1252 * After dropping the lock and possibly sleeping here, our request
1253 * may now be mergeable after it had proven unmergeable (above).
1254 * We don't worry about that case for efficiency. It won't happen
1255 * often, and the elevators are able to handle it.
1da177e4 1256 */
52d9e675 1257 init_request_from_bio(req, bio);
1da177e4 1258
450991bc 1259 spin_lock_irq(q->queue_lock);
c7c22e4d
JA
1260 if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags) ||
1261 bio_flagged(bio, BIO_CPU_AFFINE))
1262 req->cpu = blk_cpu_to_group(smp_processor_id());
644b2d99 1263 if (queue_should_plug(q) && elv_queue_empty(q))
450991bc 1264 blk_plug_device(q);
1da177e4
LT
1265 add_request(q, req);
1266out:
644b2d99 1267 if (unplug || !queue_should_plug(q))
1da177e4 1268 __generic_unplug_device(q);
1da177e4
LT
1269 spin_unlock_irq(q->queue_lock);
1270 return 0;
1da177e4
LT
1271}
1272
1273/*
1274 * If bio->bi_dev is a partition, remap the location
1275 */
1276static inline void blk_partition_remap(struct bio *bio)
1277{
1278 struct block_device *bdev = bio->bi_bdev;
1279
bf2de6f5 1280 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
1da177e4
LT
1281 struct hd_struct *p = bdev->bd_part;
1282
1da177e4
LT
1283 bio->bi_sector += p->start_sect;
1284 bio->bi_bdev = bdev->bd_contains;
c7149d6b 1285
5f3ea37c 1286 trace_block_remap(bdev_get_queue(bio->bi_bdev), bio,
22a7c31a 1287 bdev->bd_dev,
c7149d6b 1288 bio->bi_sector - p->start_sect);
1da177e4
LT
1289 }
1290}
1291
1da177e4
LT
1292static void handle_bad_sector(struct bio *bio)
1293{
1294 char b[BDEVNAME_SIZE];
1295
1296 printk(KERN_INFO "attempt to access beyond end of device\n");
1297 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1298 bdevname(bio->bi_bdev, b),
1299 bio->bi_rw,
1300 (unsigned long long)bio->bi_sector + bio_sectors(bio),
1301 (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
1302
1303 set_bit(BIO_EOF, &bio->bi_flags);
1304}
1305
c17bb495
AM
1306#ifdef CONFIG_FAIL_MAKE_REQUEST
1307
1308static DECLARE_FAULT_ATTR(fail_make_request);
1309
1310static int __init setup_fail_make_request(char *str)
1311{
1312 return setup_fault_attr(&fail_make_request, str);
1313}
1314__setup("fail_make_request=", setup_fail_make_request);
1315
1316static int should_fail_request(struct bio *bio)
1317{
eddb2e26
TH
1318 struct hd_struct *part = bio->bi_bdev->bd_part;
1319
1320 if (part_to_disk(part)->part0.make_it_fail || part->make_it_fail)
c17bb495
AM
1321 return should_fail(&fail_make_request, bio->bi_size);
1322
1323 return 0;
1324}
1325
1326static int __init fail_make_request_debugfs(void)
1327{
1328 return init_fault_attr_dentries(&fail_make_request,
1329 "fail_make_request");
1330}
1331
1332late_initcall(fail_make_request_debugfs);
1333
1334#else /* CONFIG_FAIL_MAKE_REQUEST */
1335
1336static inline int should_fail_request(struct bio *bio)
1337{
1338 return 0;
1339}
1340
1341#endif /* CONFIG_FAIL_MAKE_REQUEST */
1342
c07e2b41
JA
1343/*
1344 * Check whether this bio extends beyond the end of the device.
1345 */
1346static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1347{
1348 sector_t maxsector;
1349
1350 if (!nr_sectors)
1351 return 0;
1352
1353 /* Test device or partition size, when known. */
1354 maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
1355 if (maxsector) {
1356 sector_t sector = bio->bi_sector;
1357
1358 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1359 /*
1360 * This may well happen - the kernel calls bread()
1361 * without checking the size of the device, e.g., when
1362 * mounting a device.
1363 */
1364 handle_bad_sector(bio);
1365 return 1;
1366 }
1367 }
1368
1369 return 0;
1370}
1371
1da177e4 1372/**
710027a4 1373 * generic_make_request - hand a buffer to its device driver for I/O
1da177e4
LT
1374 * @bio: The bio describing the location in memory and on the device.
1375 *
1376 * generic_make_request() is used to make I/O requests of block
1377 * devices. It is passed a &struct bio, which describes the I/O that needs
1378 * to be done.
1379 *
1380 * generic_make_request() does not return any status. The
1381 * success/failure status of the request, along with notification of
1382 * completion, is delivered asynchronously through the bio->bi_end_io
1383 * function described (one day) else where.
1384 *
1385 * The caller of generic_make_request must make sure that bi_io_vec
1386 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1387 * set to describe the device address, and the
1388 * bi_end_io and optionally bi_private are set to describe how
1389 * completion notification should be signaled.
1390 *
1391 * generic_make_request and the drivers it calls may use bi_next if this
1392 * bio happens to be merged with someone else, and may change bi_dev and
1393 * bi_sector for remaps as it sees fit. So the values of these fields
1394 * should NOT be depended on after the call to generic_make_request.
1395 */
d89d8796 1396static inline void __generic_make_request(struct bio *bio)
1da177e4 1397{
165125e1 1398 struct request_queue *q;
5ddfe969 1399 sector_t old_sector;
1da177e4 1400 int ret, nr_sectors = bio_sectors(bio);
2056a782 1401 dev_t old_dev;
51fd77bd 1402 int err = -EIO;
1da177e4
LT
1403
1404 might_sleep();
1da177e4 1405
c07e2b41
JA
1406 if (bio_check_eod(bio, nr_sectors))
1407 goto end_io;
1da177e4
LT
1408
1409 /*
1410 * Resolve the mapping until finished. (drivers are
1411 * still free to implement/resolve their own stacking
1412 * by explicitly returning 0)
1413 *
1414 * NOTE: we don't repeat the blk_size check for each new device.
1415 * Stacking drivers are expected to know what they are doing.
1416 */
5ddfe969 1417 old_sector = -1;
2056a782 1418 old_dev = 0;
1da177e4
LT
1419 do {
1420 char b[BDEVNAME_SIZE];
1421
1422 q = bdev_get_queue(bio->bi_bdev);
a7384677 1423 if (unlikely(!q)) {
1da177e4
LT
1424 printk(KERN_ERR
1425 "generic_make_request: Trying to access "
1426 "nonexistent block-device %s (%Lu)\n",
1427 bdevname(bio->bi_bdev, b),
1428 (long long) bio->bi_sector);
a7384677 1429 goto end_io;
1da177e4
LT
1430 }
1431
ae03bf63 1432 if (unlikely(nr_sectors > queue_max_hw_sectors(q))) {
6728cb0e 1433 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
ae03bf63
MP
1434 bdevname(bio->bi_bdev, b),
1435 bio_sectors(bio),
1436 queue_max_hw_sectors(q));
1da177e4
LT
1437 goto end_io;
1438 }
1439
fde6ad22 1440 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
1da177e4
LT
1441 goto end_io;
1442
c17bb495
AM
1443 if (should_fail_request(bio))
1444 goto end_io;
1445
1da177e4
LT
1446 /*
1447 * If this device has partitions, remap block n
1448 * of partition p to block n+start(p) of the disk.
1449 */
1450 blk_partition_remap(bio);
1451
7ba1ba12
MP
1452 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio))
1453 goto end_io;
1454
5ddfe969 1455 if (old_sector != -1)
22a7c31a 1456 trace_block_remap(q, bio, old_dev, old_sector);
2056a782 1457
5f3ea37c 1458 trace_block_bio_queue(q, bio);
2056a782 1459
5ddfe969 1460 old_sector = bio->bi_sector;
2056a782
JA
1461 old_dev = bio->bi_bdev->bd_dev;
1462
c07e2b41
JA
1463 if (bio_check_eod(bio, nr_sectors))
1464 goto end_io;
a7384677
TH
1465
1466 if (bio_discard(bio) && !q->prepare_discard_fn) {
51fd77bd
JA
1467 err = -EOPNOTSUPP;
1468 goto end_io;
1469 }
cec0707e
JA
1470 if (bio_barrier(bio) && bio_has_data(bio) &&
1471 (q->next_ordered == QUEUE_ORDERED_NONE)) {
1472 err = -EOPNOTSUPP;
1473 goto end_io;
1474 }
5ddfe969 1475
1da177e4
LT
1476 ret = q->make_request_fn(q, bio);
1477 } while (ret);
a7384677
TH
1478
1479 return;
1480
1481end_io:
1482 bio_endio(bio, err);
1da177e4
LT
1483}
1484
d89d8796
NB
1485/*
1486 * We only want one ->make_request_fn to be active at a time,
1487 * else stack usage with stacked devices could be a problem.
1488 * So use current->bio_{list,tail} to keep a list of requests
1489 * submited by a make_request_fn function.
1490 * current->bio_tail is also used as a flag to say if
1491 * generic_make_request is currently active in this task or not.
1492 * If it is NULL, then no make_request is active. If it is non-NULL,
1493 * then a make_request is active, and new requests should be added
1494 * at the tail
1495 */
1496void generic_make_request(struct bio *bio)
1497{
1498 if (current->bio_tail) {
1499 /* make_request is active */
1500 *(current->bio_tail) = bio;
1501 bio->bi_next = NULL;
1502 current->bio_tail = &bio->bi_next;
1503 return;
1504 }
1505 /* following loop may be a bit non-obvious, and so deserves some
1506 * explanation.
1507 * Before entering the loop, bio->bi_next is NULL (as all callers
1508 * ensure that) so we have a list with a single bio.
1509 * We pretend that we have just taken it off a longer list, so
1510 * we assign bio_list to the next (which is NULL) and bio_tail
1511 * to &bio_list, thus initialising the bio_list of new bios to be
1512 * added. __generic_make_request may indeed add some more bios
1513 * through a recursive call to generic_make_request. If it
1514 * did, we find a non-NULL value in bio_list and re-enter the loop
1515 * from the top. In this case we really did just take the bio
1516 * of the top of the list (no pretending) and so fixup bio_list and
1517 * bio_tail or bi_next, and call into __generic_make_request again.
1518 *
1519 * The loop was structured like this to make only one call to
1520 * __generic_make_request (which is important as it is large and
1521 * inlined) and to keep the structure simple.
1522 */
1523 BUG_ON(bio->bi_next);
1524 do {
1525 current->bio_list = bio->bi_next;
1526 if (bio->bi_next == NULL)
1527 current->bio_tail = &current->bio_list;
1528 else
1529 bio->bi_next = NULL;
1530 __generic_make_request(bio);
1531 bio = current->bio_list;
1532 } while (bio);
1533 current->bio_tail = NULL; /* deactivate */
1534}
1da177e4
LT
1535EXPORT_SYMBOL(generic_make_request);
1536
1537/**
710027a4 1538 * submit_bio - submit a bio to the block device layer for I/O
1da177e4
LT
1539 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1540 * @bio: The &struct bio which describes the I/O
1541 *
1542 * submit_bio() is very similar in purpose to generic_make_request(), and
1543 * uses that function to do most of the work. Both are fairly rough
710027a4 1544 * interfaces; @bio must be presetup and ready for I/O.
1da177e4
LT
1545 *
1546 */
1547void submit_bio(int rw, struct bio *bio)
1548{
1549 int count = bio_sectors(bio);
1550
22e2c507 1551 bio->bi_rw |= rw;
1da177e4 1552
bf2de6f5
JA
1553 /*
1554 * If it's a regular read/write or a barrier with data attached,
1555 * go through the normal accounting stuff before submission.
1556 */
a9c701e5 1557 if (bio_has_data(bio)) {
bf2de6f5
JA
1558 if (rw & WRITE) {
1559 count_vm_events(PGPGOUT, count);
1560 } else {
1561 task_io_account_read(bio->bi_size);
1562 count_vm_events(PGPGIN, count);
1563 }
1564
1565 if (unlikely(block_dump)) {
1566 char b[BDEVNAME_SIZE];
1567 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n",
ba25f9dc 1568 current->comm, task_pid_nr(current),
bf2de6f5
JA
1569 (rw & WRITE) ? "WRITE" : "READ",
1570 (unsigned long long)bio->bi_sector,
6728cb0e 1571 bdevname(bio->bi_bdev, b));
bf2de6f5 1572 }
1da177e4
LT
1573 }
1574
1575 generic_make_request(bio);
1576}
1da177e4
LT
1577EXPORT_SYMBOL(submit_bio);
1578
82124d60
KU
1579/**
1580 * blk_rq_check_limits - Helper function to check a request for the queue limit
1581 * @q: the queue
1582 * @rq: the request being checked
1583 *
1584 * Description:
1585 * @rq may have been made based on weaker limitations of upper-level queues
1586 * in request stacking drivers, and it may violate the limitation of @q.
1587 * Since the block layer and the underlying device driver trust @rq
1588 * after it is inserted to @q, it should be checked against @q before
1589 * the insertion using this generic function.
1590 *
1591 * This function should also be useful for request stacking drivers
1592 * in some cases below, so export this fuction.
1593 * Request stacking drivers like request-based dm may change the queue
1594 * limits while requests are in the queue (e.g. dm's table swapping).
1595 * Such request stacking drivers should check those requests agaist
1596 * the new queue limits again when they dispatch those requests,
1597 * although such checkings are also done against the old queue limits
1598 * when submitting requests.
1599 */
1600int blk_rq_check_limits(struct request_queue *q, struct request *rq)
1601{
ae03bf63
MP
1602 if (blk_rq_sectors(rq) > queue_max_sectors(q) ||
1603 blk_rq_bytes(rq) > queue_max_hw_sectors(q) << 9) {
82124d60
KU
1604 printk(KERN_ERR "%s: over max size limit.\n", __func__);
1605 return -EIO;
1606 }
1607
1608 /*
1609 * queue's settings related to segment counting like q->bounce_pfn
1610 * may differ from that of other stacking queues.
1611 * Recalculate it to check the request correctly on this queue's
1612 * limitation.
1613 */
1614 blk_recalc_rq_segments(rq);
ae03bf63
MP
1615 if (rq->nr_phys_segments > queue_max_phys_segments(q) ||
1616 rq->nr_phys_segments > queue_max_hw_segments(q)) {
82124d60
KU
1617 printk(KERN_ERR "%s: over max segments limit.\n", __func__);
1618 return -EIO;
1619 }
1620
1621 return 0;
1622}
1623EXPORT_SYMBOL_GPL(blk_rq_check_limits);
1624
1625/**
1626 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
1627 * @q: the queue to submit the request
1628 * @rq: the request being queued
1629 */
1630int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
1631{
1632 unsigned long flags;
1633
1634 if (blk_rq_check_limits(q, rq))
1635 return -EIO;
1636
1637#ifdef CONFIG_FAIL_MAKE_REQUEST
1638 if (rq->rq_disk && rq->rq_disk->part0.make_it_fail &&
1639 should_fail(&fail_make_request, blk_rq_bytes(rq)))
1640 return -EIO;
1641#endif
1642
1643 spin_lock_irqsave(q->queue_lock, flags);
1644
1645 /*
1646 * Submitting request must be dequeued before calling this function
1647 * because it will be linked to another request_queue
1648 */
1649 BUG_ON(blk_queued_rq(rq));
1650
1651 drive_stat_acct(rq, 1);
1652 __elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 0);
1653
1654 spin_unlock_irqrestore(q->queue_lock, flags);
1655
1656 return 0;
1657}
1658EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
1659
bc58ba94
JA
1660static void blk_account_io_completion(struct request *req, unsigned int bytes)
1661{
c2553b58 1662 if (blk_do_io_stat(req)) {
bc58ba94
JA
1663 const int rw = rq_data_dir(req);
1664 struct hd_struct *part;
1665 int cpu;
1666
1667 cpu = part_stat_lock();
83096ebf 1668 part = disk_map_sector_rcu(req->rq_disk, blk_rq_pos(req));
bc58ba94
JA
1669 part_stat_add(cpu, part, sectors[rw], bytes >> 9);
1670 part_stat_unlock();
1671 }
1672}
1673
1674static void blk_account_io_done(struct request *req)
1675{
bc58ba94
JA
1676 /*
1677 * Account IO completion. bar_rq isn't accounted as a normal
1678 * IO on queueing nor completion. Accounting the containing
1679 * request is enough.
1680 */
c2553b58 1681 if (blk_do_io_stat(req) && req != &req->q->bar_rq) {
bc58ba94
JA
1682 unsigned long duration = jiffies - req->start_time;
1683 const int rw = rq_data_dir(req);
1684 struct hd_struct *part;
1685 int cpu;
1686
1687 cpu = part_stat_lock();
83096ebf 1688 part = disk_map_sector_rcu(req->rq_disk, blk_rq_pos(req));
bc58ba94
JA
1689
1690 part_stat_inc(cpu, part, ios[rw]);
1691 part_stat_add(cpu, part, ticks[rw], duration);
1692 part_round_stats(cpu, part);
1693 part_dec_in_flight(part);
1694
1695 part_stat_unlock();
1696 }
1697}
1698
3bcddeac 1699/**
9934c8c0
TH
1700 * blk_peek_request - peek at the top of a request queue
1701 * @q: request queue to peek at
1702 *
1703 * Description:
1704 * Return the request at the top of @q. The returned request
1705 * should be started using blk_start_request() before LLD starts
1706 * processing it.
1707 *
1708 * Return:
1709 * Pointer to the request at the top of @q if available. Null
1710 * otherwise.
1711 *
1712 * Context:
1713 * queue_lock must be held.
1714 */
1715struct request *blk_peek_request(struct request_queue *q)
158dbda0
TH
1716{
1717 struct request *rq;
1718 int ret;
1719
1720 while ((rq = __elv_next_request(q)) != NULL) {
1721 if (!(rq->cmd_flags & REQ_STARTED)) {
1722 /*
1723 * This is the first time the device driver
1724 * sees this request (possibly after
1725 * requeueing). Notify IO scheduler.
1726 */
1727 if (blk_sorted_rq(rq))
1728 elv_activate_rq(q, rq);
1729
1730 /*
1731 * just mark as started even if we don't start
1732 * it, a request that has been delayed should
1733 * not be passed by new incoming requests
1734 */
1735 rq->cmd_flags |= REQ_STARTED;
1736 trace_block_rq_issue(q, rq);
1737 }
1738
1739 if (!q->boundary_rq || q->boundary_rq == rq) {
1740 q->end_sector = rq_end_sector(rq);
1741 q->boundary_rq = NULL;
1742 }
1743
1744 if (rq->cmd_flags & REQ_DONTPREP)
1745 break;
1746
2e46e8b2 1747 if (q->dma_drain_size && blk_rq_bytes(rq)) {
158dbda0
TH
1748 /*
1749 * make sure space for the drain appears we
1750 * know we can do this because max_hw_segments
1751 * has been adjusted to be one fewer than the
1752 * device can handle
1753 */
1754 rq->nr_phys_segments++;
1755 }
1756
1757 if (!q->prep_rq_fn)
1758 break;
1759
1760 ret = q->prep_rq_fn(q, rq);
1761 if (ret == BLKPREP_OK) {
1762 break;
1763 } else if (ret == BLKPREP_DEFER) {
1764 /*
1765 * the request may have been (partially) prepped.
1766 * we need to keep this request in the front to
1767 * avoid resource deadlock. REQ_STARTED will
1768 * prevent other fs requests from passing this one.
1769 */
2e46e8b2 1770 if (q->dma_drain_size && blk_rq_bytes(rq) &&
158dbda0
TH
1771 !(rq->cmd_flags & REQ_DONTPREP)) {
1772 /*
1773 * remove the space for the drain we added
1774 * so that we don't add it again
1775 */
1776 --rq->nr_phys_segments;
1777 }
1778
1779 rq = NULL;
1780 break;
1781 } else if (ret == BLKPREP_KILL) {
1782 rq->cmd_flags |= REQ_QUIET;
c143dc90
JB
1783 /*
1784 * Mark this request as started so we don't trigger
1785 * any debug logic in the end I/O path.
1786 */
1787 blk_start_request(rq);
40cbbb78 1788 __blk_end_request_all(rq, -EIO);
158dbda0
TH
1789 } else {
1790 printk(KERN_ERR "%s: bad return=%d\n", __func__, ret);
1791 break;
1792 }
1793 }
1794
1795 return rq;
1796}
9934c8c0 1797EXPORT_SYMBOL(blk_peek_request);
158dbda0 1798
9934c8c0 1799void blk_dequeue_request(struct request *rq)
158dbda0 1800{
9934c8c0
TH
1801 struct request_queue *q = rq->q;
1802
158dbda0
TH
1803 BUG_ON(list_empty(&rq->queuelist));
1804 BUG_ON(ELV_ON_HASH(rq));
1805
1806 list_del_init(&rq->queuelist);
1807
1808 /*
1809 * the time frame between a request being removed from the lists
1810 * and to it is freed is accounted as io that is in progress at
1811 * the driver side.
1812 */
1813 if (blk_account_rq(rq))
0a7ae2ff 1814 q->in_flight[rq_is_sync(rq)]++;
158dbda0
TH
1815}
1816
9934c8c0
TH
1817/**
1818 * blk_start_request - start request processing on the driver
1819 * @req: request to dequeue
1820 *
1821 * Description:
1822 * Dequeue @req and start timeout timer on it. This hands off the
1823 * request to the driver.
1824 *
1825 * Block internal functions which don't want to start timer should
1826 * call blk_dequeue_request().
1827 *
1828 * Context:
1829 * queue_lock must be held.
1830 */
1831void blk_start_request(struct request *req)
1832{
1833 blk_dequeue_request(req);
1834
1835 /*
5f49f631
TH
1836 * We are now handing the request to the hardware, initialize
1837 * resid_len to full count and add the timeout handler.
9934c8c0 1838 */
5f49f631 1839 req->resid_len = blk_rq_bytes(req);
dbb66c4b
FT
1840 if (unlikely(blk_bidi_rq(req)))
1841 req->next_rq->resid_len = blk_rq_bytes(req->next_rq);
1842
9934c8c0
TH
1843 blk_add_timer(req);
1844}
1845EXPORT_SYMBOL(blk_start_request);
1846
1847/**
1848 * blk_fetch_request - fetch a request from a request queue
1849 * @q: request queue to fetch a request from
1850 *
1851 * Description:
1852 * Return the request at the top of @q. The request is started on
1853 * return and LLD can start processing it immediately.
1854 *
1855 * Return:
1856 * Pointer to the request at the top of @q if available. Null
1857 * otherwise.
1858 *
1859 * Context:
1860 * queue_lock must be held.
1861 */
1862struct request *blk_fetch_request(struct request_queue *q)
1863{
1864 struct request *rq;
1865
1866 rq = blk_peek_request(q);
1867 if (rq)
1868 blk_start_request(rq);
1869 return rq;
1870}
1871EXPORT_SYMBOL(blk_fetch_request);
1872
3bcddeac 1873/**
2e60e022
TH
1874 * blk_update_request - Special helper function for request stacking drivers
1875 * @rq: the request being processed
710027a4 1876 * @error: %0 for success, < %0 for error
2e60e022 1877 * @nr_bytes: number of bytes to complete @rq
3bcddeac
KU
1878 *
1879 * Description:
2e60e022
TH
1880 * Ends I/O on a number of bytes attached to @rq, but doesn't complete
1881 * the request structure even if @rq doesn't have leftover.
1882 * If @rq has leftover, sets it up for the next range of segments.
1883 *
1884 * This special helper function is only for request stacking drivers
1885 * (e.g. request-based dm) so that they can handle partial completion.
1886 * Actual device drivers should use blk_end_request instead.
1887 *
1888 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
1889 * %false return from this function.
3bcddeac
KU
1890 *
1891 * Return:
2e60e022
TH
1892 * %false - this request doesn't have any more data
1893 * %true - this request has more data
3bcddeac 1894 **/
2e60e022 1895bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
1da177e4 1896{
5450d3e1 1897 int total_bytes, bio_nbytes, next_idx = 0;
1da177e4
LT
1898 struct bio *bio;
1899
2e60e022
TH
1900 if (!req->bio)
1901 return false;
1902
5f3ea37c 1903 trace_block_rq_complete(req->q, req);
2056a782 1904
1da177e4 1905 /*
6f41469c
TH
1906 * For fs requests, rq is just carrier of independent bio's
1907 * and each partial completion should be handled separately.
1908 * Reset per-request error on each partial completion.
1909 *
1910 * TODO: tj: This is too subtle. It would be better to let
1911 * low level drivers do what they see fit.
1da177e4 1912 */
6f41469c 1913 if (blk_fs_request(req))
1da177e4
LT
1914 req->errors = 0;
1915
6728cb0e
JA
1916 if (error && (blk_fs_request(req) && !(req->cmd_flags & REQ_QUIET))) {
1917 printk(KERN_ERR "end_request: I/O error, dev %s, sector %llu\n",
1da177e4 1918 req->rq_disk ? req->rq_disk->disk_name : "?",
83096ebf 1919 (unsigned long long)blk_rq_pos(req));
1da177e4
LT
1920 }
1921
bc58ba94 1922 blk_account_io_completion(req, nr_bytes);
d72d904a 1923
1da177e4
LT
1924 total_bytes = bio_nbytes = 0;
1925 while ((bio = req->bio) != NULL) {
1926 int nbytes;
1927
1928 if (nr_bytes >= bio->bi_size) {
1929 req->bio = bio->bi_next;
1930 nbytes = bio->bi_size;
5bb23a68 1931 req_bio_endio(req, bio, nbytes, error);
1da177e4
LT
1932 next_idx = 0;
1933 bio_nbytes = 0;
1934 } else {
1935 int idx = bio->bi_idx + next_idx;
1936
af498d7f 1937 if (unlikely(idx >= bio->bi_vcnt)) {
1da177e4 1938 blk_dump_rq_flags(req, "__end_that");
6728cb0e 1939 printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
af498d7f 1940 __func__, idx, bio->bi_vcnt);
1da177e4
LT
1941 break;
1942 }
1943
1944 nbytes = bio_iovec_idx(bio, idx)->bv_len;
1945 BIO_BUG_ON(nbytes > bio->bi_size);
1946
1947 /*
1948 * not a complete bvec done
1949 */
1950 if (unlikely(nbytes > nr_bytes)) {
1951 bio_nbytes += nr_bytes;
1952 total_bytes += nr_bytes;
1953 break;
1954 }
1955
1956 /*
1957 * advance to the next vector
1958 */
1959 next_idx++;
1960 bio_nbytes += nbytes;
1961 }
1962
1963 total_bytes += nbytes;
1964 nr_bytes -= nbytes;
1965
6728cb0e
JA
1966 bio = req->bio;
1967 if (bio) {
1da177e4
LT
1968 /*
1969 * end more in this run, or just return 'not-done'
1970 */
1971 if (unlikely(nr_bytes <= 0))
1972 break;
1973 }
1974 }
1975
1976 /*
1977 * completely done
1978 */
2e60e022
TH
1979 if (!req->bio) {
1980 /*
1981 * Reset counters so that the request stacking driver
1982 * can find how many bytes remain in the request
1983 * later.
1984 */
a2dec7b3 1985 req->__data_len = 0;
2e60e022
TH
1986 return false;
1987 }
1da177e4
LT
1988
1989 /*
1990 * if the request wasn't completed, update state
1991 */
1992 if (bio_nbytes) {
5bb23a68 1993 req_bio_endio(req, bio, bio_nbytes, error);
1da177e4
LT
1994 bio->bi_idx += next_idx;
1995 bio_iovec(bio)->bv_offset += nr_bytes;
1996 bio_iovec(bio)->bv_len -= nr_bytes;
1997 }
1998
a2dec7b3 1999 req->__data_len -= total_bytes;
2e46e8b2
TH
2000 req->buffer = bio_data(req->bio);
2001
2002 /* update sector only for requests with clear definition of sector */
2003 if (blk_fs_request(req) || blk_discard_rq(req))
a2dec7b3 2004 req->__sector += total_bytes >> 9;
2e46e8b2
TH
2005
2006 /*
2007 * If total number of sectors is less than the first segment
2008 * size, something has gone terribly wrong.
2009 */
2010 if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
2011 printk(KERN_ERR "blk: request botched\n");
a2dec7b3 2012 req->__data_len = blk_rq_cur_bytes(req);
2e46e8b2
TH
2013 }
2014
2015 /* recalculate the number of segments */
1da177e4 2016 blk_recalc_rq_segments(req);
2e46e8b2 2017
2e60e022 2018 return true;
1da177e4 2019}
2e60e022 2020EXPORT_SYMBOL_GPL(blk_update_request);
1da177e4 2021
2e60e022
TH
2022static bool blk_update_bidi_request(struct request *rq, int error,
2023 unsigned int nr_bytes,
2024 unsigned int bidi_bytes)
5efccd17 2025{
2e60e022
TH
2026 if (blk_update_request(rq, error, nr_bytes))
2027 return true;
5efccd17 2028
2e60e022
TH
2029 /* Bidi request must be completed as a whole */
2030 if (unlikely(blk_bidi_rq(rq)) &&
2031 blk_update_request(rq->next_rq, error, bidi_bytes))
2032 return true;
5efccd17 2033
2e60e022
TH
2034 add_disk_randomness(rq->rq_disk);
2035
2036 return false;
1da177e4
LT
2037}
2038
1da177e4
LT
2039/*
2040 * queue lock must be held
2041 */
2e60e022 2042static void blk_finish_request(struct request *req, int error)
1da177e4 2043{
b8286239
KU
2044 if (blk_rq_tagged(req))
2045 blk_queue_end_tag(req->q, req);
2046
ba396a6c 2047 BUG_ON(blk_queued_rq(req));
1da177e4
LT
2048
2049 if (unlikely(laptop_mode) && blk_fs_request(req))
2050 laptop_io_completion();
2051
e78042e5
MA
2052 blk_delete_timer(req);
2053
bc58ba94 2054 blk_account_io_done(req);
b8286239 2055
1da177e4 2056 if (req->end_io)
8ffdc655 2057 req->end_io(req, error);
b8286239
KU
2058 else {
2059 if (blk_bidi_rq(req))
2060 __blk_put_request(req->next_rq->q, req->next_rq);
2061
1da177e4 2062 __blk_put_request(req->q, req);
b8286239 2063 }
1da177e4
LT
2064}
2065
3b11313a 2066/**
2e60e022
TH
2067 * blk_end_bidi_request - Complete a bidi request
2068 * @rq: the request to complete
2069 * @error: %0 for success, < %0 for error
2070 * @nr_bytes: number of bytes to complete @rq
2071 * @bidi_bytes: number of bytes to complete @rq->next_rq
a0cd1285
JA
2072 *
2073 * Description:
e3a04fe3 2074 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
2e60e022
TH
2075 * Drivers that supports bidi can safely call this member for any
2076 * type of request, bidi or uni. In the later case @bidi_bytes is
2077 * just ignored.
336cdb40
KU
2078 *
2079 * Return:
2e60e022
TH
2080 * %false - we are done with this request
2081 * %true - still buffers pending for this request
a0cd1285 2082 **/
b1f74493 2083static bool blk_end_bidi_request(struct request *rq, int error,
32fab448
KU
2084 unsigned int nr_bytes, unsigned int bidi_bytes)
2085{
336cdb40 2086 struct request_queue *q = rq->q;
2e60e022 2087 unsigned long flags;
32fab448 2088
2e60e022
TH
2089 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2090 return true;
32fab448 2091
336cdb40 2092 spin_lock_irqsave(q->queue_lock, flags);
2e60e022 2093 blk_finish_request(rq, error);
336cdb40
KU
2094 spin_unlock_irqrestore(q->queue_lock, flags);
2095
2e60e022 2096 return false;
32fab448
KU
2097}
2098
336cdb40 2099/**
2e60e022
TH
2100 * __blk_end_bidi_request - Complete a bidi request with queue lock held
2101 * @rq: the request to complete
710027a4 2102 * @error: %0 for success, < %0 for error
e3a04fe3
KU
2103 * @nr_bytes: number of bytes to complete @rq
2104 * @bidi_bytes: number of bytes to complete @rq->next_rq
336cdb40
KU
2105 *
2106 * Description:
2e60e022
TH
2107 * Identical to blk_end_bidi_request() except that queue lock is
2108 * assumed to be locked on entry and remains so on return.
336cdb40
KU
2109 *
2110 * Return:
2e60e022
TH
2111 * %false - we are done with this request
2112 * %true - still buffers pending for this request
336cdb40 2113 **/
b1f74493
FT
2114static bool __blk_end_bidi_request(struct request *rq, int error,
2115 unsigned int nr_bytes, unsigned int bidi_bytes)
336cdb40 2116{
2e60e022
TH
2117 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2118 return true;
336cdb40 2119
2e60e022 2120 blk_finish_request(rq, error);
336cdb40 2121
2e60e022 2122 return false;
336cdb40 2123}
e19a3ab0
KU
2124
2125/**
2126 * blk_end_request - Helper function for drivers to complete the request.
2127 * @rq: the request being processed
710027a4 2128 * @error: %0 for success, < %0 for error
e19a3ab0
KU
2129 * @nr_bytes: number of bytes to complete
2130 *
2131 * Description:
2132 * Ends I/O on a number of bytes attached to @rq.
2133 * If @rq has leftover, sets it up for the next range of segments.
2134 *
2135 * Return:
b1f74493
FT
2136 * %false - we are done with this request
2137 * %true - still buffers pending for this request
e19a3ab0 2138 **/
b1f74493 2139bool blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
e19a3ab0 2140{
b1f74493 2141 return blk_end_bidi_request(rq, error, nr_bytes, 0);
e19a3ab0 2142}
336cdb40
KU
2143EXPORT_SYMBOL_GPL(blk_end_request);
2144
2145/**
b1f74493
FT
2146 * blk_end_request_all - Helper function for drives to finish the request.
2147 * @rq: the request to finish
2148 * @err: %0 for success, < %0 for error
336cdb40
KU
2149 *
2150 * Description:
b1f74493
FT
2151 * Completely finish @rq.
2152 */
2153void blk_end_request_all(struct request *rq, int error)
336cdb40 2154{
b1f74493
FT
2155 bool pending;
2156 unsigned int bidi_bytes = 0;
336cdb40 2157
b1f74493
FT
2158 if (unlikely(blk_bidi_rq(rq)))
2159 bidi_bytes = blk_rq_bytes(rq->next_rq);
336cdb40 2160
b1f74493
FT
2161 pending = blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2162 BUG_ON(pending);
2163}
2164EXPORT_SYMBOL_GPL(blk_end_request_all);
336cdb40 2165
b1f74493
FT
2166/**
2167 * blk_end_request_cur - Helper function to finish the current request chunk.
2168 * @rq: the request to finish the current chunk for
2169 * @err: %0 for success, < %0 for error
2170 *
2171 * Description:
2172 * Complete the current consecutively mapped chunk from @rq.
2173 *
2174 * Return:
2175 * %false - we are done with this request
2176 * %true - still buffers pending for this request
2177 */
2178bool blk_end_request_cur(struct request *rq, int error)
2179{
2180 return blk_end_request(rq, error, blk_rq_cur_bytes(rq));
336cdb40 2181}
b1f74493 2182EXPORT_SYMBOL_GPL(blk_end_request_cur);
336cdb40 2183
e3a04fe3 2184/**
b1f74493
FT
2185 * __blk_end_request - Helper function for drivers to complete the request.
2186 * @rq: the request being processed
2187 * @error: %0 for success, < %0 for error
2188 * @nr_bytes: number of bytes to complete
e3a04fe3
KU
2189 *
2190 * Description:
b1f74493 2191 * Must be called with queue lock held unlike blk_end_request().
e3a04fe3
KU
2192 *
2193 * Return:
b1f74493
FT
2194 * %false - we are done with this request
2195 * %true - still buffers pending for this request
e3a04fe3 2196 **/
b1f74493 2197bool __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
e3a04fe3 2198{
b1f74493 2199 return __blk_end_bidi_request(rq, error, nr_bytes, 0);
e3a04fe3 2200}
b1f74493 2201EXPORT_SYMBOL_GPL(__blk_end_request);
e3a04fe3 2202
32fab448 2203/**
b1f74493
FT
2204 * __blk_end_request_all - Helper function for drives to finish the request.
2205 * @rq: the request to finish
2206 * @err: %0 for success, < %0 for error
32fab448
KU
2207 *
2208 * Description:
b1f74493 2209 * Completely finish @rq. Must be called with queue lock held.
32fab448 2210 */
b1f74493 2211void __blk_end_request_all(struct request *rq, int error)
32fab448 2212{
b1f74493
FT
2213 bool pending;
2214 unsigned int bidi_bytes = 0;
2215
2216 if (unlikely(blk_bidi_rq(rq)))
2217 bidi_bytes = blk_rq_bytes(rq->next_rq);
2218
2219 pending = __blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2220 BUG_ON(pending);
32fab448 2221}
b1f74493 2222EXPORT_SYMBOL_GPL(__blk_end_request_all);
32fab448 2223
e19a3ab0 2224/**
b1f74493
FT
2225 * __blk_end_request_cur - Helper function to finish the current request chunk.
2226 * @rq: the request to finish the current chunk for
2227 * @err: %0 for success, < %0 for error
e19a3ab0
KU
2228 *
2229 * Description:
b1f74493
FT
2230 * Complete the current consecutively mapped chunk from @rq. Must
2231 * be called with queue lock held.
e19a3ab0
KU
2232 *
2233 * Return:
b1f74493
FT
2234 * %false - we are done with this request
2235 * %true - still buffers pending for this request
2236 */
2237bool __blk_end_request_cur(struct request *rq, int error)
e19a3ab0 2238{
b1f74493 2239 return __blk_end_request(rq, error, blk_rq_cur_bytes(rq));
e19a3ab0 2240}
b1f74493 2241EXPORT_SYMBOL_GPL(__blk_end_request_cur);
e19a3ab0 2242
86db1e29
JA
2243void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2244 struct bio *bio)
1da177e4 2245{
d628eaef
DW
2246 /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw, and
2247 we want BIO_RW_AHEAD (bit 1) to imply REQ_FAILFAST (bit 1). */
4aff5e23 2248 rq->cmd_flags |= (bio->bi_rw & 3);
1da177e4 2249
fb2dce86
DW
2250 if (bio_has_data(bio)) {
2251 rq->nr_phys_segments = bio_phys_segments(q, bio);
fb2dce86
DW
2252 rq->buffer = bio_data(bio);
2253 }
a2dec7b3 2254 rq->__data_len = bio->bi_size;
1da177e4 2255 rq->bio = rq->biotail = bio;
1da177e4 2256
66846572
N
2257 if (bio->bi_bdev)
2258 rq->rq_disk = bio->bi_bdev->bd_disk;
2259}
1da177e4 2260
ef9e3fac
KU
2261/**
2262 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
2263 * @q : the queue of the device being checked
2264 *
2265 * Description:
2266 * Check if underlying low-level drivers of a device are busy.
2267 * If the drivers want to export their busy state, they must set own
2268 * exporting function using blk_queue_lld_busy() first.
2269 *
2270 * Basically, this function is used only by request stacking drivers
2271 * to stop dispatching requests to underlying devices when underlying
2272 * devices are busy. This behavior helps more I/O merging on the queue
2273 * of the request stacking driver and prevents I/O throughput regression
2274 * on burst I/O load.
2275 *
2276 * Return:
2277 * 0 - Not busy (The request stacking driver should dispatch request)
2278 * 1 - Busy (The request stacking driver should stop dispatching request)
2279 */
2280int blk_lld_busy(struct request_queue *q)
2281{
2282 if (q->lld_busy_fn)
2283 return q->lld_busy_fn(q);
2284
2285 return 0;
2286}
2287EXPORT_SYMBOL_GPL(blk_lld_busy);
2288
b0fd271d
KU
2289/**
2290 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
2291 * @rq: the clone request to be cleaned up
2292 *
2293 * Description:
2294 * Free all bios in @rq for a cloned request.
2295 */
2296void blk_rq_unprep_clone(struct request *rq)
2297{
2298 struct bio *bio;
2299
2300 while ((bio = rq->bio) != NULL) {
2301 rq->bio = bio->bi_next;
2302
2303 bio_put(bio);
2304 }
2305}
2306EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
2307
2308/*
2309 * Copy attributes of the original request to the clone request.
2310 * The actual data parts (e.g. ->cmd, ->buffer, ->sense) are not copied.
2311 */
2312static void __blk_rq_prep_clone(struct request *dst, struct request *src)
2313{
2314 dst->cpu = src->cpu;
2315 dst->cmd_flags = (rq_data_dir(src) | REQ_NOMERGE);
2316 dst->cmd_type = src->cmd_type;
2317 dst->__sector = blk_rq_pos(src);
2318 dst->__data_len = blk_rq_bytes(src);
2319 dst->nr_phys_segments = src->nr_phys_segments;
2320 dst->ioprio = src->ioprio;
2321 dst->extra_len = src->extra_len;
2322}
2323
2324/**
2325 * blk_rq_prep_clone - Helper function to setup clone request
2326 * @rq: the request to be setup
2327 * @rq_src: original request to be cloned
2328 * @bs: bio_set that bios for clone are allocated from
2329 * @gfp_mask: memory allocation mask for bio
2330 * @bio_ctr: setup function to be called for each clone bio.
2331 * Returns %0 for success, non %0 for failure.
2332 * @data: private data to be passed to @bio_ctr
2333 *
2334 * Description:
2335 * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
2336 * The actual data parts of @rq_src (e.g. ->cmd, ->buffer, ->sense)
2337 * are not copied, and copying such parts is the caller's responsibility.
2338 * Also, pages which the original bios are pointing to are not copied
2339 * and the cloned bios just point same pages.
2340 * So cloned bios must be completed before original bios, which means
2341 * the caller must complete @rq before @rq_src.
2342 */
2343int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
2344 struct bio_set *bs, gfp_t gfp_mask,
2345 int (*bio_ctr)(struct bio *, struct bio *, void *),
2346 void *data)
2347{
2348 struct bio *bio, *bio_src;
2349
2350 if (!bs)
2351 bs = fs_bio_set;
2352
2353 blk_rq_init(NULL, rq);
2354
2355 __rq_for_each_bio(bio_src, rq_src) {
2356 bio = bio_alloc_bioset(gfp_mask, bio_src->bi_max_vecs, bs);
2357 if (!bio)
2358 goto free_and_out;
2359
2360 __bio_clone(bio, bio_src);
2361
2362 if (bio_integrity(bio_src) &&
2363 bio_integrity_clone(bio, bio_src, gfp_mask))
2364 goto free_and_out;
2365
2366 if (bio_ctr && bio_ctr(bio, bio_src, data))
2367 goto free_and_out;
2368
2369 if (rq->bio) {
2370 rq->biotail->bi_next = bio;
2371 rq->biotail = bio;
2372 } else
2373 rq->bio = rq->biotail = bio;
2374 }
2375
2376 __blk_rq_prep_clone(rq, rq_src);
2377
2378 return 0;
2379
2380free_and_out:
2381 if (bio)
2382 bio_free(bio, bs);
2383 blk_rq_unprep_clone(rq);
2384
2385 return -ENOMEM;
2386}
2387EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
2388
18887ad9 2389int kblockd_schedule_work(struct request_queue *q, struct work_struct *work)
1da177e4
LT
2390{
2391 return queue_work(kblockd_workqueue, work);
2392}
1da177e4
LT
2393EXPORT_SYMBOL(kblockd_schedule_work);
2394
1da177e4
LT
2395int __init blk_dev_init(void)
2396{
9eb55b03
NK
2397 BUILD_BUG_ON(__REQ_NR_BITS > 8 *
2398 sizeof(((struct request *)0)->cmd_flags));
2399
1da177e4
LT
2400 kblockd_workqueue = create_workqueue("kblockd");
2401 if (!kblockd_workqueue)
2402 panic("Failed to create kblockd\n");
2403
2404 request_cachep = kmem_cache_create("blkdev_requests",
20c2df83 2405 sizeof(struct request), 0, SLAB_PANIC, NULL);
1da177e4 2406
8324aa91 2407 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
165125e1 2408 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
1da177e4 2409
d38ecf93 2410 return 0;
1da177e4 2411}
1da177e4 2412