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