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