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