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