block: no need to initialize rq->cmd with blk_get_request
[GitHub/mt8127/android_kernel_alcatel_ttab.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>
ff856bad
JA
29#include <linux/interrupt.h>
30#include <linux/cpu.h>
2056a782 31#include <linux/blktrace_api.h>
c17bb495 32#include <linux/fault-inject.h>
1da177e4 33
8324aa91
JA
34#include "blk.h"
35
165125e1 36static int __make_request(struct request_queue *q, struct bio *bio);
1da177e4
LT
37
38/*
39 * For the allocated request tables
40 */
5ece6c52 41static struct kmem_cache *request_cachep;
1da177e4
LT
42
43/*
44 * For queue allocation
45 */
6728cb0e 46struct kmem_cache *blk_requestq_cachep;
1da177e4 47
1da177e4
LT
48/*
49 * Controlling structure to kblockd
50 */
ff856bad 51static struct workqueue_struct *kblockd_workqueue;
1da177e4 52
ff856bad
JA
53static DEFINE_PER_CPU(struct list_head, blk_cpu_done);
54
26b8256e
JA
55static void drive_stat_acct(struct request *rq, int new_io)
56{
57 int rw = rq_data_dir(rq);
58
59 if (!blk_fs_request(rq) || !rq->rq_disk)
60 return;
61
62 if (!new_io) {
6f2576af 63 __all_stat_inc(rq->rq_disk, merges[rw], rq->sector);
26b8256e 64 } else {
6f2576af 65 struct hd_struct *part = get_part(rq->rq_disk, rq->sector);
26b8256e
JA
66 disk_round_stats(rq->rq_disk);
67 rq->rq_disk->in_flight++;
6f2576af
JM
68 if (part) {
69 part_round_stats(part);
70 part->in_flight++;
71 }
26b8256e
JA
72 }
73}
74
8324aa91 75void blk_queue_congestion_threshold(struct request_queue *q)
1da177e4
LT
76{
77 int nr;
78
79 nr = q->nr_requests - (q->nr_requests / 8) + 1;
80 if (nr > q->nr_requests)
81 nr = q->nr_requests;
82 q->nr_congestion_on = nr;
83
84 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
85 if (nr < 1)
86 nr = 1;
87 q->nr_congestion_off = nr;
88}
89
1da177e4
LT
90/**
91 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
92 * @bdev: device
93 *
94 * Locates the passed device's request queue and returns the address of its
95 * backing_dev_info
96 *
97 * Will return NULL if the request queue cannot be located.
98 */
99struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
100{
101 struct backing_dev_info *ret = NULL;
165125e1 102 struct request_queue *q = bdev_get_queue(bdev);
1da177e4
LT
103
104 if (q)
105 ret = &q->backing_dev_info;
106 return ret;
107}
1da177e4
LT
108EXPORT_SYMBOL(blk_get_backing_dev_info);
109
86db1e29 110void rq_init(struct request_queue *q, struct request *rq)
1da177e4 111{
1afb20f3
FT
112 memset(rq, 0, sizeof(*rq));
113
1da177e4 114 INIT_LIST_HEAD(&rq->queuelist);
ff856bad 115 INIT_LIST_HEAD(&rq->donelist);
63a71386
JA
116 rq->q = q;
117 rq->sector = rq->hard_sector = (sector_t) -1;
2e662b65
JA
118 INIT_HLIST_NODE(&rq->hash);
119 RB_CLEAR_NODE(&rq->rb_node);
63a71386 120 rq->tag = -1;
1da177e4 121 rq->ref_count = 1;
1da177e4
LT
122}
123
5bb23a68
N
124static void req_bio_endio(struct request *rq, struct bio *bio,
125 unsigned int nbytes, int error)
1da177e4 126{
165125e1 127 struct request_queue *q = rq->q;
797e7dbb 128
5bb23a68
N
129 if (&q->bar_rq != rq) {
130 if (error)
131 clear_bit(BIO_UPTODATE, &bio->bi_flags);
132 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
133 error = -EIO;
797e7dbb 134
5bb23a68 135 if (unlikely(nbytes > bio->bi_size)) {
6728cb0e 136 printk(KERN_ERR "%s: want %u bytes done, %u left\n",
5bb23a68
N
137 __FUNCTION__, nbytes, bio->bi_size);
138 nbytes = bio->bi_size;
139 }
797e7dbb 140
5bb23a68
N
141 bio->bi_size -= nbytes;
142 bio->bi_sector += (nbytes >> 9);
143 if (bio->bi_size == 0)
6712ecf8 144 bio_endio(bio, error);
5bb23a68
N
145 } else {
146
147 /*
148 * Okay, this is the barrier request in progress, just
149 * record the error;
150 */
151 if (error && !q->orderr)
152 q->orderr = error;
153 }
1da177e4 154}
1da177e4 155
1da177e4
LT
156void blk_dump_rq_flags(struct request *rq, char *msg)
157{
158 int bit;
159
6728cb0e 160 printk(KERN_INFO "%s: dev %s: type=%x, flags=%x\n", msg,
4aff5e23
JA
161 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
162 rq->cmd_flags);
1da177e4 163
6728cb0e
JA
164 printk(KERN_INFO " sector %llu, nr/cnr %lu/%u\n",
165 (unsigned long long)rq->sector,
166 rq->nr_sectors,
167 rq->current_nr_sectors);
168 printk(KERN_INFO " bio %p, biotail %p, buffer %p, data %p, len %u\n",
169 rq->bio, rq->biotail,
170 rq->buffer, rq->data,
171 rq->data_len);
1da177e4 172
4aff5e23 173 if (blk_pc_request(rq)) {
6728cb0e 174 printk(KERN_INFO " cdb: ");
1da177e4
LT
175 for (bit = 0; bit < sizeof(rq->cmd); bit++)
176 printk("%02x ", rq->cmd[bit]);
177 printk("\n");
178 }
179}
1da177e4
LT
180EXPORT_SYMBOL(blk_dump_rq_flags);
181
1da177e4
LT
182/*
183 * "plug" the device if there are no outstanding requests: this will
184 * force the transfer to start only after we have put all the requests
185 * on the list.
186 *
187 * This is called with interrupts off and no requests on the queue and
188 * with the queue lock held.
189 */
165125e1 190void blk_plug_device(struct request_queue *q)
1da177e4
LT
191{
192 WARN_ON(!irqs_disabled());
193
194 /*
195 * don't plug a stopped queue, it must be paired with blk_start_queue()
196 * which will restart the queueing
197 */
7daac490 198 if (blk_queue_stopped(q))
1da177e4
LT
199 return;
200
75ad23bc
NP
201 if (!test_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags)) {
202 __set_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags);
1da177e4 203 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
2056a782
JA
204 blk_add_trace_generic(q, NULL, 0, BLK_TA_PLUG);
205 }
1da177e4 206}
1da177e4
LT
207EXPORT_SYMBOL(blk_plug_device);
208
209/*
210 * remove the queue from the plugged list, if present. called with
211 * queue lock held and interrupts disabled.
212 */
165125e1 213int blk_remove_plug(struct request_queue *q)
1da177e4
LT
214{
215 WARN_ON(!irqs_disabled());
216
75ad23bc 217 if (!test_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags))
1da177e4
LT
218 return 0;
219
75ad23bc 220 queue_flag_clear(QUEUE_FLAG_PLUGGED, q);
1da177e4
LT
221 del_timer(&q->unplug_timer);
222 return 1;
223}
1da177e4
LT
224EXPORT_SYMBOL(blk_remove_plug);
225
226/*
227 * remove the plug and let it rip..
228 */
165125e1 229void __generic_unplug_device(struct request_queue *q)
1da177e4 230{
7daac490 231 if (unlikely(blk_queue_stopped(q)))
1da177e4
LT
232 return;
233
234 if (!blk_remove_plug(q))
235 return;
236
22e2c507 237 q->request_fn(q);
1da177e4
LT
238}
239EXPORT_SYMBOL(__generic_unplug_device);
240
241/**
242 * generic_unplug_device - fire a request queue
165125e1 243 * @q: The &struct request_queue in question
1da177e4
LT
244 *
245 * Description:
246 * Linux uses plugging to build bigger requests queues before letting
247 * the device have at them. If a queue is plugged, the I/O scheduler
248 * is still adding and merging requests on the queue. Once the queue
249 * gets unplugged, the request_fn defined for the queue is invoked and
250 * transfers started.
251 **/
165125e1 252void generic_unplug_device(struct request_queue *q)
1da177e4
LT
253{
254 spin_lock_irq(q->queue_lock);
255 __generic_unplug_device(q);
256 spin_unlock_irq(q->queue_lock);
257}
258EXPORT_SYMBOL(generic_unplug_device);
259
260static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
261 struct page *page)
262{
165125e1 263 struct request_queue *q = bdi->unplug_io_data;
1da177e4 264
2ad8b1ef 265 blk_unplug(q);
1da177e4
LT
266}
267
86db1e29 268void blk_unplug_work(struct work_struct *work)
1da177e4 269{
165125e1
JA
270 struct request_queue *q =
271 container_of(work, struct request_queue, unplug_work);
1da177e4 272
2056a782
JA
273 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
274 q->rq.count[READ] + q->rq.count[WRITE]);
275
1da177e4
LT
276 q->unplug_fn(q);
277}
278
86db1e29 279void blk_unplug_timeout(unsigned long data)
1da177e4 280{
165125e1 281 struct request_queue *q = (struct request_queue *)data;
1da177e4 282
2056a782
JA
283 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_TIMER, NULL,
284 q->rq.count[READ] + q->rq.count[WRITE]);
285
1da177e4
LT
286 kblockd_schedule_work(&q->unplug_work);
287}
288
2ad8b1ef
AB
289void blk_unplug(struct request_queue *q)
290{
291 /*
292 * devices don't necessarily have an ->unplug_fn defined
293 */
294 if (q->unplug_fn) {
295 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
296 q->rq.count[READ] + q->rq.count[WRITE]);
297
298 q->unplug_fn(q);
299 }
300}
301EXPORT_SYMBOL(blk_unplug);
302
1da177e4
LT
303/**
304 * blk_start_queue - restart a previously stopped queue
165125e1 305 * @q: The &struct request_queue in question
1da177e4
LT
306 *
307 * Description:
308 * blk_start_queue() will clear the stop flag on the queue, and call
309 * the request_fn for the queue if it was in a stopped state when
310 * entered. Also see blk_stop_queue(). Queue lock must be held.
311 **/
165125e1 312void blk_start_queue(struct request_queue *q)
1da177e4 313{
a038e253
PBG
314 WARN_ON(!irqs_disabled());
315
75ad23bc 316 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
1da177e4
LT
317
318 /*
319 * one level of recursion is ok and is much faster than kicking
320 * the unplug handling
321 */
75ad23bc
NP
322 if (!test_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) {
323 queue_flag_set(QUEUE_FLAG_REENTER, q);
1da177e4 324 q->request_fn(q);
75ad23bc 325 queue_flag_clear(QUEUE_FLAG_REENTER, q);
1da177e4
LT
326 } else {
327 blk_plug_device(q);
328 kblockd_schedule_work(&q->unplug_work);
329 }
330}
1da177e4
LT
331EXPORT_SYMBOL(blk_start_queue);
332
333/**
334 * blk_stop_queue - stop a queue
165125e1 335 * @q: The &struct request_queue in question
1da177e4
LT
336 *
337 * Description:
338 * The Linux block layer assumes that a block driver will consume all
339 * entries on the request queue when the request_fn strategy is called.
340 * Often this will not happen, because of hardware limitations (queue
341 * depth settings). If a device driver gets a 'queue full' response,
342 * or if it simply chooses not to queue more I/O at one point, it can
343 * call this function to prevent the request_fn from being called until
344 * the driver has signalled it's ready to go again. This happens by calling
345 * blk_start_queue() to restart queue operations. Queue lock must be held.
346 **/
165125e1 347void blk_stop_queue(struct request_queue *q)
1da177e4
LT
348{
349 blk_remove_plug(q);
75ad23bc 350 queue_flag_set(QUEUE_FLAG_STOPPED, q);
1da177e4
LT
351}
352EXPORT_SYMBOL(blk_stop_queue);
353
354/**
355 * blk_sync_queue - cancel any pending callbacks on a queue
356 * @q: the queue
357 *
358 * Description:
359 * The block layer may perform asynchronous callback activity
360 * on a queue, such as calling the unplug function after a timeout.
361 * A block device may call blk_sync_queue to ensure that any
362 * such activity is cancelled, thus allowing it to release resources
59c51591 363 * that the callbacks might use. The caller must already have made sure
1da177e4
LT
364 * that its ->make_request_fn will not re-add plugging prior to calling
365 * this function.
366 *
367 */
368void blk_sync_queue(struct request_queue *q)
369{
370 del_timer_sync(&q->unplug_timer);
abbeb88d 371 kblockd_flush_work(&q->unplug_work);
1da177e4
LT
372}
373EXPORT_SYMBOL(blk_sync_queue);
374
375/**
376 * blk_run_queue - run a single device queue
377 * @q: The queue to run
378 */
75ad23bc 379void __blk_run_queue(struct request_queue *q)
1da177e4 380{
1da177e4 381 blk_remove_plug(q);
dac07ec1
JA
382
383 /*
384 * Only recurse once to avoid overrunning the stack, let the unplug
385 * handling reinvoke the handler shortly if we already got there.
386 */
387 if (!elv_queue_empty(q)) {
75ad23bc
NP
388 if (!test_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) {
389 queue_flag_set(QUEUE_FLAG_REENTER, q);
dac07ec1 390 q->request_fn(q);
75ad23bc 391 queue_flag_clear(QUEUE_FLAG_REENTER, q);
dac07ec1
JA
392 } else {
393 blk_plug_device(q);
394 kblockd_schedule_work(&q->unplug_work);
395 }
396 }
75ad23bc
NP
397}
398EXPORT_SYMBOL(__blk_run_queue);
dac07ec1 399
75ad23bc
NP
400/**
401 * blk_run_queue - run a single device queue
402 * @q: The queue to run
403 */
404void blk_run_queue(struct request_queue *q)
405{
406 unsigned long flags;
407
408 spin_lock_irqsave(q->queue_lock, flags);
409 __blk_run_queue(q);
1da177e4
LT
410 spin_unlock_irqrestore(q->queue_lock, flags);
411}
412EXPORT_SYMBOL(blk_run_queue);
413
165125e1 414void blk_put_queue(struct request_queue *q)
483f4afc
AV
415{
416 kobject_put(&q->kobj);
417}
483f4afc 418
6728cb0e 419void blk_cleanup_queue(struct request_queue *q)
483f4afc
AV
420{
421 mutex_lock(&q->sysfs_lock);
75ad23bc 422 queue_flag_set_unlocked(QUEUE_FLAG_DEAD, q);
483f4afc
AV
423 mutex_unlock(&q->sysfs_lock);
424
425 if (q->elevator)
426 elevator_exit(q->elevator);
427
428 blk_put_queue(q);
429}
1da177e4
LT
430EXPORT_SYMBOL(blk_cleanup_queue);
431
165125e1 432static int blk_init_free_list(struct request_queue *q)
1da177e4
LT
433{
434 struct request_list *rl = &q->rq;
435
436 rl->count[READ] = rl->count[WRITE] = 0;
437 rl->starved[READ] = rl->starved[WRITE] = 0;
cb98fc8b 438 rl->elvpriv = 0;
1da177e4
LT
439 init_waitqueue_head(&rl->wait[READ]);
440 init_waitqueue_head(&rl->wait[WRITE]);
1da177e4 441
1946089a
CL
442 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
443 mempool_free_slab, request_cachep, q->node);
1da177e4
LT
444
445 if (!rl->rq_pool)
446 return -ENOMEM;
447
448 return 0;
449}
450
165125e1 451struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
1da177e4 452{
1946089a
CL
453 return blk_alloc_queue_node(gfp_mask, -1);
454}
455EXPORT_SYMBOL(blk_alloc_queue);
1da177e4 456
165125e1 457struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
1946089a 458{
165125e1 459 struct request_queue *q;
e0bf68dd 460 int err;
1946089a 461
8324aa91 462 q = kmem_cache_alloc_node(blk_requestq_cachep,
94f6030c 463 gfp_mask | __GFP_ZERO, node_id);
1da177e4
LT
464 if (!q)
465 return NULL;
466
e0bf68dd
PZ
467 q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
468 q->backing_dev_info.unplug_io_data = q;
469 err = bdi_init(&q->backing_dev_info);
470 if (err) {
8324aa91 471 kmem_cache_free(blk_requestq_cachep, q);
e0bf68dd
PZ
472 return NULL;
473 }
474
1da177e4 475 init_timer(&q->unplug_timer);
483f4afc 476
8324aa91 477 kobject_init(&q->kobj, &blk_queue_ktype);
1da177e4 478
483f4afc
AV
479 mutex_init(&q->sysfs_lock);
480
1da177e4
LT
481 return q;
482}
1946089a 483EXPORT_SYMBOL(blk_alloc_queue_node);
1da177e4
LT
484
485/**
486 * blk_init_queue - prepare a request queue for use with a block device
487 * @rfn: The function to be called to process requests that have been
488 * placed on the queue.
489 * @lock: Request queue spin lock
490 *
491 * Description:
492 * If a block device wishes to use the standard request handling procedures,
493 * which sorts requests and coalesces adjacent requests, then it must
494 * call blk_init_queue(). The function @rfn will be called when there
495 * are requests on the queue that need to be processed. If the device
496 * supports plugging, then @rfn may not be called immediately when requests
497 * are available on the queue, but may be called at some time later instead.
498 * Plugged queues are generally unplugged when a buffer belonging to one
499 * of the requests on the queue is needed, or due to memory pressure.
500 *
501 * @rfn is not required, or even expected, to remove all requests off the
502 * queue, but only as many as it can handle at a time. If it does leave
503 * requests on the queue, it is responsible for arranging that the requests
504 * get dealt with eventually.
505 *
506 * The queue spin lock must be held while manipulating the requests on the
a038e253
PBG
507 * request queue; this lock will be taken also from interrupt context, so irq
508 * disabling is needed for it.
1da177e4
LT
509 *
510 * Function returns a pointer to the initialized request queue, or NULL if
511 * it didn't succeed.
512 *
513 * Note:
514 * blk_init_queue() must be paired with a blk_cleanup_queue() call
515 * when the block device is deactivated (such as at module unload).
516 **/
1946089a 517
165125e1 518struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
1da177e4 519{
1946089a
CL
520 return blk_init_queue_node(rfn, lock, -1);
521}
522EXPORT_SYMBOL(blk_init_queue);
523
165125e1 524struct request_queue *
1946089a
CL
525blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
526{
165125e1 527 struct request_queue *q = blk_alloc_queue_node(GFP_KERNEL, node_id);
1da177e4
LT
528
529 if (!q)
530 return NULL;
531
1946089a 532 q->node = node_id;
8669aafd 533 if (blk_init_free_list(q)) {
8324aa91 534 kmem_cache_free(blk_requestq_cachep, q);
8669aafd
AV
535 return NULL;
536 }
1da177e4 537
152587de
JA
538 /*
539 * if caller didn't supply a lock, they get per-queue locking with
540 * our embedded lock
541 */
542 if (!lock) {
543 spin_lock_init(&q->__queue_lock);
544 lock = &q->__queue_lock;
545 }
546
1da177e4 547 q->request_fn = rfn;
1da177e4
LT
548 q->prep_rq_fn = NULL;
549 q->unplug_fn = generic_unplug_device;
550 q->queue_flags = (1 << QUEUE_FLAG_CLUSTER);
551 q->queue_lock = lock;
552
553 blk_queue_segment_boundary(q, 0xffffffff);
554
555 blk_queue_make_request(q, __make_request);
556 blk_queue_max_segment_size(q, MAX_SEGMENT_SIZE);
557
558 blk_queue_max_hw_segments(q, MAX_HW_SEGMENTS);
559 blk_queue_max_phys_segments(q, MAX_PHYS_SEGMENTS);
560
44ec9542
AS
561 q->sg_reserved_size = INT_MAX;
562
1da177e4
LT
563 /*
564 * all done
565 */
566 if (!elevator_init(q, NULL)) {
567 blk_queue_congestion_threshold(q);
568 return q;
569 }
570
8669aafd 571 blk_put_queue(q);
1da177e4
LT
572 return NULL;
573}
1946089a 574EXPORT_SYMBOL(blk_init_queue_node);
1da177e4 575
165125e1 576int blk_get_queue(struct request_queue *q)
1da177e4 577{
fde6ad22 578 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
483f4afc 579 kobject_get(&q->kobj);
1da177e4
LT
580 return 0;
581 }
582
583 return 1;
584}
1da177e4 585
165125e1 586static inline void blk_free_request(struct request_queue *q, struct request *rq)
1da177e4 587{
4aff5e23 588 if (rq->cmd_flags & REQ_ELVPRIV)
cb98fc8b 589 elv_put_request(q, rq);
1da177e4
LT
590 mempool_free(rq, q->rq.rq_pool);
591}
592
1ea25ecb 593static struct request *
165125e1 594blk_alloc_request(struct request_queue *q, int rw, int priv, gfp_t gfp_mask)
1da177e4
LT
595{
596 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
597
598 if (!rq)
599 return NULL;
600
1afb20f3
FT
601 rq_init(q, rq);
602
1da177e4 603 /*
4aff5e23 604 * first three bits are identical in rq->cmd_flags and bio->bi_rw,
1da177e4
LT
605 * see bio.h and blkdev.h
606 */
49171e5c 607 rq->cmd_flags = rw | REQ_ALLOCED;
1da177e4 608
cb98fc8b 609 if (priv) {
cb78b285 610 if (unlikely(elv_set_request(q, rq, gfp_mask))) {
cb98fc8b
TH
611 mempool_free(rq, q->rq.rq_pool);
612 return NULL;
613 }
4aff5e23 614 rq->cmd_flags |= REQ_ELVPRIV;
cb98fc8b 615 }
1da177e4 616
cb98fc8b 617 return rq;
1da177e4
LT
618}
619
620/*
621 * ioc_batching returns true if the ioc is a valid batching request and
622 * should be given priority access to a request.
623 */
165125e1 624static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
1da177e4
LT
625{
626 if (!ioc)
627 return 0;
628
629 /*
630 * Make sure the process is able to allocate at least 1 request
631 * even if the batch times out, otherwise we could theoretically
632 * lose wakeups.
633 */
634 return ioc->nr_batch_requests == q->nr_batching ||
635 (ioc->nr_batch_requests > 0
636 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
637}
638
639/*
640 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
641 * will cause the process to be a "batcher" on all queues in the system. This
642 * is the behaviour we want though - once it gets a wakeup it should be given
643 * a nice run.
644 */
165125e1 645static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
1da177e4
LT
646{
647 if (!ioc || ioc_batching(q, ioc))
648 return;
649
650 ioc->nr_batch_requests = q->nr_batching;
651 ioc->last_waited = jiffies;
652}
653
165125e1 654static void __freed_request(struct request_queue *q, int rw)
1da177e4
LT
655{
656 struct request_list *rl = &q->rq;
657
658 if (rl->count[rw] < queue_congestion_off_threshold(q))
79e2de4b 659 blk_clear_queue_congested(q, rw);
1da177e4
LT
660
661 if (rl->count[rw] + 1 <= q->nr_requests) {
1da177e4
LT
662 if (waitqueue_active(&rl->wait[rw]))
663 wake_up(&rl->wait[rw]);
664
665 blk_clear_queue_full(q, rw);
666 }
667}
668
669/*
670 * A request has just been released. Account for it, update the full and
671 * congestion status, wake up any waiters. Called under q->queue_lock.
672 */
165125e1 673static void freed_request(struct request_queue *q, int rw, int priv)
1da177e4
LT
674{
675 struct request_list *rl = &q->rq;
676
677 rl->count[rw]--;
cb98fc8b
TH
678 if (priv)
679 rl->elvpriv--;
1da177e4
LT
680
681 __freed_request(q, rw);
682
683 if (unlikely(rl->starved[rw ^ 1]))
684 __freed_request(q, rw ^ 1);
1da177e4
LT
685}
686
687#define blkdev_free_rq(list) list_entry((list)->next, struct request, queuelist)
688/*
d6344532
NP
689 * Get a free request, queue_lock must be held.
690 * Returns NULL on failure, with queue_lock held.
691 * Returns !NULL on success, with queue_lock *not held*.
1da177e4 692 */
165125e1 693static struct request *get_request(struct request_queue *q, int rw_flags,
7749a8d4 694 struct bio *bio, gfp_t gfp_mask)
1da177e4
LT
695{
696 struct request *rq = NULL;
697 struct request_list *rl = &q->rq;
88ee5ef1 698 struct io_context *ioc = NULL;
7749a8d4 699 const int rw = rw_flags & 0x01;
88ee5ef1
JA
700 int may_queue, priv;
701
7749a8d4 702 may_queue = elv_may_queue(q, rw_flags);
88ee5ef1
JA
703 if (may_queue == ELV_MQUEUE_NO)
704 goto rq_starved;
705
706 if (rl->count[rw]+1 >= queue_congestion_on_threshold(q)) {
707 if (rl->count[rw]+1 >= q->nr_requests) {
b5deef90 708 ioc = current_io_context(GFP_ATOMIC, q->node);
88ee5ef1
JA
709 /*
710 * The queue will fill after this allocation, so set
711 * it as full, and mark this process as "batching".
712 * This process will be allowed to complete a batch of
713 * requests, others will be blocked.
714 */
715 if (!blk_queue_full(q, rw)) {
716 ioc_set_batching(q, ioc);
717 blk_set_queue_full(q, rw);
718 } else {
719 if (may_queue != ELV_MQUEUE_MUST
720 && !ioc_batching(q, ioc)) {
721 /*
722 * The queue is full and the allocating
723 * process is not a "batcher", and not
724 * exempted by the IO scheduler
725 */
726 goto out;
727 }
728 }
1da177e4 729 }
79e2de4b 730 blk_set_queue_congested(q, rw);
1da177e4
LT
731 }
732
082cf69e
JA
733 /*
734 * Only allow batching queuers to allocate up to 50% over the defined
735 * limit of requests, otherwise we could have thousands of requests
736 * allocated with any setting of ->nr_requests
737 */
fd782a4a 738 if (rl->count[rw] >= (3 * q->nr_requests / 2))
082cf69e 739 goto out;
fd782a4a 740
1da177e4
LT
741 rl->count[rw]++;
742 rl->starved[rw] = 0;
cb98fc8b 743
64521d1a 744 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
cb98fc8b
TH
745 if (priv)
746 rl->elvpriv++;
747
1da177e4
LT
748 spin_unlock_irq(q->queue_lock);
749
7749a8d4 750 rq = blk_alloc_request(q, rw_flags, priv, gfp_mask);
88ee5ef1 751 if (unlikely(!rq)) {
1da177e4
LT
752 /*
753 * Allocation failed presumably due to memory. Undo anything
754 * we might have messed up.
755 *
756 * Allocating task should really be put onto the front of the
757 * wait queue, but this is pretty rare.
758 */
759 spin_lock_irq(q->queue_lock);
cb98fc8b 760 freed_request(q, rw, priv);
1da177e4
LT
761
762 /*
763 * in the very unlikely event that allocation failed and no
764 * requests for this direction was pending, mark us starved
765 * so that freeing of a request in the other direction will
766 * notice us. another possible fix would be to split the
767 * rq mempool into READ and WRITE
768 */
769rq_starved:
770 if (unlikely(rl->count[rw] == 0))
771 rl->starved[rw] = 1;
772
1da177e4
LT
773 goto out;
774 }
775
88ee5ef1
JA
776 /*
777 * ioc may be NULL here, and ioc_batching will be false. That's
778 * OK, if the queue is under the request limit then requests need
779 * not count toward the nr_batch_requests limit. There will always
780 * be some limit enforced by BLK_BATCH_TIME.
781 */
1da177e4
LT
782 if (ioc_batching(q, ioc))
783 ioc->nr_batch_requests--;
6728cb0e 784
2056a782 785 blk_add_trace_generic(q, bio, rw, BLK_TA_GETRQ);
1da177e4 786out:
1da177e4
LT
787 return rq;
788}
789
790/*
791 * No available requests for this queue, unplug the device and wait for some
792 * requests to become available.
d6344532
NP
793 *
794 * Called with q->queue_lock held, and returns with it unlocked.
1da177e4 795 */
165125e1 796static struct request *get_request_wait(struct request_queue *q, int rw_flags,
22e2c507 797 struct bio *bio)
1da177e4 798{
7749a8d4 799 const int rw = rw_flags & 0x01;
1da177e4
LT
800 struct request *rq;
801
7749a8d4 802 rq = get_request(q, rw_flags, bio, GFP_NOIO);
450991bc
NP
803 while (!rq) {
804 DEFINE_WAIT(wait);
1da177e4
LT
805 struct request_list *rl = &q->rq;
806
807 prepare_to_wait_exclusive(&rl->wait[rw], &wait,
808 TASK_UNINTERRUPTIBLE);
809
7749a8d4 810 rq = get_request(q, rw_flags, bio, GFP_NOIO);
1da177e4
LT
811
812 if (!rq) {
813 struct io_context *ioc;
814
2056a782
JA
815 blk_add_trace_generic(q, bio, rw, BLK_TA_SLEEPRQ);
816
d6344532
NP
817 __generic_unplug_device(q);
818 spin_unlock_irq(q->queue_lock);
1da177e4
LT
819 io_schedule();
820
821 /*
822 * After sleeping, we become a "batching" process and
823 * will be able to allocate at least one request, and
824 * up to a big batch of them for a small period time.
825 * See ioc_batching, ioc_set_batching
826 */
b5deef90 827 ioc = current_io_context(GFP_NOIO, q->node);
1da177e4 828 ioc_set_batching(q, ioc);
d6344532
NP
829
830 spin_lock_irq(q->queue_lock);
1da177e4
LT
831 }
832 finish_wait(&rl->wait[rw], &wait);
450991bc 833 }
1da177e4
LT
834
835 return rq;
836}
837
165125e1 838struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
1da177e4
LT
839{
840 struct request *rq;
841
842 BUG_ON(rw != READ && rw != WRITE);
843
d6344532
NP
844 spin_lock_irq(q->queue_lock);
845 if (gfp_mask & __GFP_WAIT) {
22e2c507 846 rq = get_request_wait(q, rw, NULL);
d6344532 847 } else {
22e2c507 848 rq = get_request(q, rw, NULL, gfp_mask);
d6344532
NP
849 if (!rq)
850 spin_unlock_irq(q->queue_lock);
851 }
852 /* q->queue_lock is unlocked at this point */
1da177e4
LT
853
854 return rq;
855}
1da177e4
LT
856EXPORT_SYMBOL(blk_get_request);
857
dc72ef4a
JA
858/**
859 * blk_start_queueing - initiate dispatch of requests to device
860 * @q: request queue to kick into gear
861 *
862 * This is basically a helper to remove the need to know whether a queue
863 * is plugged or not if someone just wants to initiate dispatch of requests
864 * for this queue.
865 *
866 * The queue lock must be held with interrupts disabled.
867 */
165125e1 868void blk_start_queueing(struct request_queue *q)
dc72ef4a
JA
869{
870 if (!blk_queue_plugged(q))
871 q->request_fn(q);
872 else
873 __generic_unplug_device(q);
874}
875EXPORT_SYMBOL(blk_start_queueing);
876
1da177e4
LT
877/**
878 * blk_requeue_request - put a request back on queue
879 * @q: request queue where request should be inserted
880 * @rq: request to be inserted
881 *
882 * Description:
883 * Drivers often keep queueing requests until the hardware cannot accept
884 * more, when that condition happens we need to put the request back
885 * on the queue. Must be called with queue lock held.
886 */
165125e1 887void blk_requeue_request(struct request_queue *q, struct request *rq)
1da177e4 888{
2056a782
JA
889 blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
890
1da177e4
LT
891 if (blk_rq_tagged(rq))
892 blk_queue_end_tag(q, rq);
893
894 elv_requeue_request(q, rq);
895}
1da177e4
LT
896EXPORT_SYMBOL(blk_requeue_request);
897
898/**
899 * blk_insert_request - insert a special request in to a request queue
900 * @q: request queue where request should be inserted
901 * @rq: request to be inserted
902 * @at_head: insert request at head or tail of queue
903 * @data: private data
1da177e4
LT
904 *
905 * Description:
906 * Many block devices need to execute commands asynchronously, so they don't
907 * block the whole kernel from preemption during request execution. This is
908 * accomplished normally by inserting aritficial requests tagged as
909 * REQ_SPECIAL in to the corresponding request queue, and letting them be
910 * scheduled for actual execution by the request queue.
911 *
912 * We have the option of inserting the head or the tail of the queue.
913 * Typically we use the tail for new ioctls and so forth. We use the head
914 * of the queue for things like a QUEUE_FULL message from a device, or a
915 * host that is unable to accept a particular command.
916 */
165125e1 917void blk_insert_request(struct request_queue *q, struct request *rq,
867d1191 918 int at_head, void *data)
1da177e4 919{
867d1191 920 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
1da177e4
LT
921 unsigned long flags;
922
923 /*
924 * tell I/O scheduler that this isn't a regular read/write (ie it
925 * must not attempt merges on this) and that it acts as a soft
926 * barrier
927 */
4aff5e23
JA
928 rq->cmd_type = REQ_TYPE_SPECIAL;
929 rq->cmd_flags |= REQ_SOFTBARRIER;
1da177e4
LT
930
931 rq->special = data;
932
933 spin_lock_irqsave(q->queue_lock, flags);
934
935 /*
936 * If command is tagged, release the tag
937 */
867d1191
TH
938 if (blk_rq_tagged(rq))
939 blk_queue_end_tag(q, rq);
1da177e4 940
b238b3d4 941 drive_stat_acct(rq, 1);
867d1191 942 __elv_add_request(q, rq, where, 0);
dc72ef4a 943 blk_start_queueing(q);
1da177e4
LT
944 spin_unlock_irqrestore(q->queue_lock, flags);
945}
1da177e4
LT
946EXPORT_SYMBOL(blk_insert_request);
947
1da177e4
LT
948/*
949 * add-request adds a request to the linked list.
950 * queue lock is held and interrupts disabled, as we muck with the
951 * request queue list.
952 */
6728cb0e 953static inline void add_request(struct request_queue *q, struct request *req)
1da177e4 954{
b238b3d4 955 drive_stat_acct(req, 1);
1da177e4 956
1da177e4
LT
957 /*
958 * elevator indicated where it wants this request to be
959 * inserted at elevator_merge time
960 */
961 __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
962}
6728cb0e 963
1da177e4
LT
964/*
965 * disk_round_stats() - Round off the performance stats on a struct
966 * disk_stats.
967 *
968 * The average IO queue length and utilisation statistics are maintained
969 * by observing the current state of the queue length and the amount of
970 * time it has been in this state for.
971 *
972 * Normally, that accounting is done on IO completion, but that can result
973 * in more than a second's worth of IO being accounted for within any one
974 * second, leading to >100% utilisation. To deal with that, we call this
975 * function to do a round-off before returning the results when reading
976 * /proc/diskstats. This accounts immediately for all queue usage up to
977 * the current jiffies and restarts the counters again.
978 */
979void disk_round_stats(struct gendisk *disk)
980{
981 unsigned long now = jiffies;
982
b2982649
KC
983 if (now == disk->stamp)
984 return;
1da177e4 985
20e5c81f
KC
986 if (disk->in_flight) {
987 __disk_stat_add(disk, time_in_queue,
988 disk->in_flight * (now - disk->stamp));
989 __disk_stat_add(disk, io_ticks, (now - disk->stamp));
990 }
1da177e4 991 disk->stamp = now;
1da177e4 992}
3eaf840e
JNN
993EXPORT_SYMBOL_GPL(disk_round_stats);
994
6f2576af
JM
995void part_round_stats(struct hd_struct *part)
996{
997 unsigned long now = jiffies;
998
999 if (now == part->stamp)
1000 return;
1001
1002 if (part->in_flight) {
1003 __part_stat_add(part, time_in_queue,
1004 part->in_flight * (now - part->stamp));
1005 __part_stat_add(part, io_ticks, (now - part->stamp));
1006 }
1007 part->stamp = now;
1008}
1009
1da177e4
LT
1010/*
1011 * queue lock must be held
1012 */
165125e1 1013void __blk_put_request(struct request_queue *q, struct request *req)
1da177e4 1014{
1da177e4
LT
1015 if (unlikely(!q))
1016 return;
1017 if (unlikely(--req->ref_count))
1018 return;
1019
8922e16c
TH
1020 elv_completed_request(q, req);
1021
1da177e4
LT
1022 /*
1023 * Request may not have originated from ll_rw_blk. if not,
1024 * it didn't come out of our reserved rq pools
1025 */
49171e5c 1026 if (req->cmd_flags & REQ_ALLOCED) {
1da177e4 1027 int rw = rq_data_dir(req);
4aff5e23 1028 int priv = req->cmd_flags & REQ_ELVPRIV;
1da177e4 1029
1da177e4 1030 BUG_ON(!list_empty(&req->queuelist));
9817064b 1031 BUG_ON(!hlist_unhashed(&req->hash));
1da177e4
LT
1032
1033 blk_free_request(q, req);
cb98fc8b 1034 freed_request(q, rw, priv);
1da177e4
LT
1035 }
1036}
6e39b69e
MC
1037EXPORT_SYMBOL_GPL(__blk_put_request);
1038
1da177e4
LT
1039void blk_put_request(struct request *req)
1040{
8922e16c 1041 unsigned long flags;
165125e1 1042 struct request_queue *q = req->q;
8922e16c 1043
1da177e4 1044 /*
8922e16c
TH
1045 * Gee, IDE calls in w/ NULL q. Fix IDE and remove the
1046 * following if (q) test.
1da177e4 1047 */
8922e16c 1048 if (q) {
1da177e4
LT
1049 spin_lock_irqsave(q->queue_lock, flags);
1050 __blk_put_request(q, req);
1051 spin_unlock_irqrestore(q->queue_lock, flags);
1052 }
1053}
1da177e4
LT
1054EXPORT_SYMBOL(blk_put_request);
1055
86db1e29 1056void init_request_from_bio(struct request *req, struct bio *bio)
52d9e675 1057{
4aff5e23 1058 req->cmd_type = REQ_TYPE_FS;
52d9e675
TH
1059
1060 /*
1061 * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
1062 */
1063 if (bio_rw_ahead(bio) || bio_failfast(bio))
4aff5e23 1064 req->cmd_flags |= REQ_FAILFAST;
52d9e675
TH
1065
1066 /*
1067 * REQ_BARRIER implies no merging, but lets make it explicit
1068 */
1069 if (unlikely(bio_barrier(bio)))
4aff5e23 1070 req->cmd_flags |= (REQ_HARDBARRIER | REQ_NOMERGE);
52d9e675 1071
b31dc66a 1072 if (bio_sync(bio))
4aff5e23 1073 req->cmd_flags |= REQ_RW_SYNC;
5404bc7a
JA
1074 if (bio_rw_meta(bio))
1075 req->cmd_flags |= REQ_RW_META;
b31dc66a 1076
52d9e675
TH
1077 req->errors = 0;
1078 req->hard_sector = req->sector = bio->bi_sector;
52d9e675 1079 req->ioprio = bio_prio(bio);
52d9e675 1080 req->start_time = jiffies;
bc1c56fd 1081 blk_rq_bio_prep(req->q, req, bio);
52d9e675
TH
1082}
1083
165125e1 1084static int __make_request(struct request_queue *q, struct bio *bio)
1da177e4 1085{
450991bc 1086 struct request *req;
51da90fc
JA
1087 int el_ret, nr_sectors, barrier, err;
1088 const unsigned short prio = bio_prio(bio);
1089 const int sync = bio_sync(bio);
7749a8d4 1090 int rw_flags;
1da177e4 1091
1da177e4 1092 nr_sectors = bio_sectors(bio);
1da177e4
LT
1093
1094 /*
1095 * low level driver can indicate that it wants pages above a
1096 * certain limit bounced to low memory (ie for highmem, or even
1097 * ISA dma in theory)
1098 */
1099 blk_queue_bounce(q, &bio);
1100
1da177e4 1101 barrier = bio_barrier(bio);
797e7dbb 1102 if (unlikely(barrier) && (q->next_ordered == QUEUE_ORDERED_NONE)) {
1da177e4
LT
1103 err = -EOPNOTSUPP;
1104 goto end_io;
1105 }
1106
1da177e4
LT
1107 spin_lock_irq(q->queue_lock);
1108
450991bc 1109 if (unlikely(barrier) || elv_queue_empty(q))
1da177e4
LT
1110 goto get_rq;
1111
1112 el_ret = elv_merge(q, &req, bio);
1113 switch (el_ret) {
6728cb0e
JA
1114 case ELEVATOR_BACK_MERGE:
1115 BUG_ON(!rq_mergeable(req));
1da177e4 1116
6728cb0e
JA
1117 if (!ll_back_merge_fn(q, req, bio))
1118 break;
1da177e4 1119
6728cb0e 1120 blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
2056a782 1121
6728cb0e
JA
1122 req->biotail->bi_next = bio;
1123 req->biotail = bio;
1124 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
1125 req->ioprio = ioprio_best(req->ioprio, prio);
1126 drive_stat_acct(req, 0);
1127 if (!attempt_back_merge(q, req))
1128 elv_merged_request(q, req, el_ret);
1129 goto out;
1da177e4 1130
6728cb0e
JA
1131 case ELEVATOR_FRONT_MERGE:
1132 BUG_ON(!rq_mergeable(req));
1da177e4 1133
6728cb0e
JA
1134 if (!ll_front_merge_fn(q, req, bio))
1135 break;
1da177e4 1136
6728cb0e 1137 blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
2056a782 1138
6728cb0e
JA
1139 bio->bi_next = req->bio;
1140 req->bio = bio;
1da177e4 1141
6728cb0e
JA
1142 /*
1143 * may not be valid. if the low level driver said
1144 * it didn't need a bounce buffer then it better
1145 * not touch req->buffer either...
1146 */
1147 req->buffer = bio_data(bio);
1148 req->current_nr_sectors = bio_cur_sectors(bio);
1149 req->hard_cur_sectors = req->current_nr_sectors;
1150 req->sector = req->hard_sector = bio->bi_sector;
1151 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
1152 req->ioprio = ioprio_best(req->ioprio, prio);
1153 drive_stat_acct(req, 0);
1154 if (!attempt_front_merge(q, req))
1155 elv_merged_request(q, req, el_ret);
1156 goto out;
1157
1158 /* ELV_NO_MERGE: elevator says don't/can't merge. */
1159 default:
1160 ;
1da177e4
LT
1161 }
1162
450991bc 1163get_rq:
7749a8d4
JA
1164 /*
1165 * This sync check and mask will be re-done in init_request_from_bio(),
1166 * but we need to set it earlier to expose the sync flag to the
1167 * rq allocator and io schedulers.
1168 */
1169 rw_flags = bio_data_dir(bio);
1170 if (sync)
1171 rw_flags |= REQ_RW_SYNC;
1172
1da177e4 1173 /*
450991bc 1174 * Grab a free request. This is might sleep but can not fail.
d6344532 1175 * Returns with the queue unlocked.
450991bc 1176 */
7749a8d4 1177 req = get_request_wait(q, rw_flags, bio);
d6344532 1178
450991bc
NP
1179 /*
1180 * After dropping the lock and possibly sleeping here, our request
1181 * may now be mergeable after it had proven unmergeable (above).
1182 * We don't worry about that case for efficiency. It won't happen
1183 * often, and the elevators are able to handle it.
1da177e4 1184 */
52d9e675 1185 init_request_from_bio(req, bio);
1da177e4 1186
450991bc
NP
1187 spin_lock_irq(q->queue_lock);
1188 if (elv_queue_empty(q))
1189 blk_plug_device(q);
1da177e4
LT
1190 add_request(q, req);
1191out:
4a534f93 1192 if (sync)
1da177e4
LT
1193 __generic_unplug_device(q);
1194
1195 spin_unlock_irq(q->queue_lock);
1196 return 0;
1197
1198end_io:
6712ecf8 1199 bio_endio(bio, err);
1da177e4
LT
1200 return 0;
1201}
1202
1203/*
1204 * If bio->bi_dev is a partition, remap the location
1205 */
1206static inline void blk_partition_remap(struct bio *bio)
1207{
1208 struct block_device *bdev = bio->bi_bdev;
1209
bf2de6f5 1210 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
1da177e4
LT
1211 struct hd_struct *p = bdev->bd_part;
1212
1da177e4
LT
1213 bio->bi_sector += p->start_sect;
1214 bio->bi_bdev = bdev->bd_contains;
c7149d6b
AB
1215
1216 blk_add_trace_remap(bdev_get_queue(bio->bi_bdev), bio,
1217 bdev->bd_dev, bio->bi_sector,
1218 bio->bi_sector - p->start_sect);
1da177e4
LT
1219 }
1220}
1221
1da177e4
LT
1222static void handle_bad_sector(struct bio *bio)
1223{
1224 char b[BDEVNAME_SIZE];
1225
1226 printk(KERN_INFO "attempt to access beyond end of device\n");
1227 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1228 bdevname(bio->bi_bdev, b),
1229 bio->bi_rw,
1230 (unsigned long long)bio->bi_sector + bio_sectors(bio),
1231 (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
1232
1233 set_bit(BIO_EOF, &bio->bi_flags);
1234}
1235
c17bb495
AM
1236#ifdef CONFIG_FAIL_MAKE_REQUEST
1237
1238static DECLARE_FAULT_ATTR(fail_make_request);
1239
1240static int __init setup_fail_make_request(char *str)
1241{
1242 return setup_fault_attr(&fail_make_request, str);
1243}
1244__setup("fail_make_request=", setup_fail_make_request);
1245
1246static int should_fail_request(struct bio *bio)
1247{
1248 if ((bio->bi_bdev->bd_disk->flags & GENHD_FL_FAIL) ||
1249 (bio->bi_bdev->bd_part && bio->bi_bdev->bd_part->make_it_fail))
1250 return should_fail(&fail_make_request, bio->bi_size);
1251
1252 return 0;
1253}
1254
1255static int __init fail_make_request_debugfs(void)
1256{
1257 return init_fault_attr_dentries(&fail_make_request,
1258 "fail_make_request");
1259}
1260
1261late_initcall(fail_make_request_debugfs);
1262
1263#else /* CONFIG_FAIL_MAKE_REQUEST */
1264
1265static inline int should_fail_request(struct bio *bio)
1266{
1267 return 0;
1268}
1269
1270#endif /* CONFIG_FAIL_MAKE_REQUEST */
1271
c07e2b41
JA
1272/*
1273 * Check whether this bio extends beyond the end of the device.
1274 */
1275static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1276{
1277 sector_t maxsector;
1278
1279 if (!nr_sectors)
1280 return 0;
1281
1282 /* Test device or partition size, when known. */
1283 maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
1284 if (maxsector) {
1285 sector_t sector = bio->bi_sector;
1286
1287 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1288 /*
1289 * This may well happen - the kernel calls bread()
1290 * without checking the size of the device, e.g., when
1291 * mounting a device.
1292 */
1293 handle_bad_sector(bio);
1294 return 1;
1295 }
1296 }
1297
1298 return 0;
1299}
1300
1da177e4
LT
1301/**
1302 * generic_make_request: hand a buffer to its device driver for I/O
1303 * @bio: The bio describing the location in memory and on the device.
1304 *
1305 * generic_make_request() is used to make I/O requests of block
1306 * devices. It is passed a &struct bio, which describes the I/O that needs
1307 * to be done.
1308 *
1309 * generic_make_request() does not return any status. The
1310 * success/failure status of the request, along with notification of
1311 * completion, is delivered asynchronously through the bio->bi_end_io
1312 * function described (one day) else where.
1313 *
1314 * The caller of generic_make_request must make sure that bi_io_vec
1315 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1316 * set to describe the device address, and the
1317 * bi_end_io and optionally bi_private are set to describe how
1318 * completion notification should be signaled.
1319 *
1320 * generic_make_request and the drivers it calls may use bi_next if this
1321 * bio happens to be merged with someone else, and may change bi_dev and
1322 * bi_sector for remaps as it sees fit. So the values of these fields
1323 * should NOT be depended on after the call to generic_make_request.
1324 */
d89d8796 1325static inline void __generic_make_request(struct bio *bio)
1da177e4 1326{
165125e1 1327 struct request_queue *q;
5ddfe969 1328 sector_t old_sector;
1da177e4 1329 int ret, nr_sectors = bio_sectors(bio);
2056a782 1330 dev_t old_dev;
51fd77bd 1331 int err = -EIO;
1da177e4
LT
1332
1333 might_sleep();
1da177e4 1334
c07e2b41
JA
1335 if (bio_check_eod(bio, nr_sectors))
1336 goto end_io;
1da177e4
LT
1337
1338 /*
1339 * Resolve the mapping until finished. (drivers are
1340 * still free to implement/resolve their own stacking
1341 * by explicitly returning 0)
1342 *
1343 * NOTE: we don't repeat the blk_size check for each new device.
1344 * Stacking drivers are expected to know what they are doing.
1345 */
5ddfe969 1346 old_sector = -1;
2056a782 1347 old_dev = 0;
1da177e4
LT
1348 do {
1349 char b[BDEVNAME_SIZE];
1350
1351 q = bdev_get_queue(bio->bi_bdev);
1352 if (!q) {
1353 printk(KERN_ERR
1354 "generic_make_request: Trying to access "
1355 "nonexistent block-device %s (%Lu)\n",
1356 bdevname(bio->bi_bdev, b),
1357 (long long) bio->bi_sector);
1358end_io:
51fd77bd 1359 bio_endio(bio, err);
1da177e4
LT
1360 break;
1361 }
1362
4fa253f3 1363 if (unlikely(nr_sectors > q->max_hw_sectors)) {
6728cb0e 1364 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
1da177e4
LT
1365 bdevname(bio->bi_bdev, b),
1366 bio_sectors(bio),
1367 q->max_hw_sectors);
1368 goto end_io;
1369 }
1370
fde6ad22 1371 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
1da177e4
LT
1372 goto end_io;
1373
c17bb495
AM
1374 if (should_fail_request(bio))
1375 goto end_io;
1376
1da177e4
LT
1377 /*
1378 * If this device has partitions, remap block n
1379 * of partition p to block n+start(p) of the disk.
1380 */
1381 blk_partition_remap(bio);
1382
5ddfe969 1383 if (old_sector != -1)
4fa253f3 1384 blk_add_trace_remap(q, bio, old_dev, bio->bi_sector,
5ddfe969 1385 old_sector);
2056a782
JA
1386
1387 blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
1388
5ddfe969 1389 old_sector = bio->bi_sector;
2056a782
JA
1390 old_dev = bio->bi_bdev->bd_dev;
1391
c07e2b41
JA
1392 if (bio_check_eod(bio, nr_sectors))
1393 goto end_io;
51fd77bd
JA
1394 if (bio_empty_barrier(bio) && !q->prepare_flush_fn) {
1395 err = -EOPNOTSUPP;
1396 goto end_io;
1397 }
5ddfe969 1398
1da177e4
LT
1399 ret = q->make_request_fn(q, bio);
1400 } while (ret);
1401}
1402
d89d8796
NB
1403/*
1404 * We only want one ->make_request_fn to be active at a time,
1405 * else stack usage with stacked devices could be a problem.
1406 * So use current->bio_{list,tail} to keep a list of requests
1407 * submited by a make_request_fn function.
1408 * current->bio_tail is also used as a flag to say if
1409 * generic_make_request is currently active in this task or not.
1410 * If it is NULL, then no make_request is active. If it is non-NULL,
1411 * then a make_request is active, and new requests should be added
1412 * at the tail
1413 */
1414void generic_make_request(struct bio *bio)
1415{
1416 if (current->bio_tail) {
1417 /* make_request is active */
1418 *(current->bio_tail) = bio;
1419 bio->bi_next = NULL;
1420 current->bio_tail = &bio->bi_next;
1421 return;
1422 }
1423 /* following loop may be a bit non-obvious, and so deserves some
1424 * explanation.
1425 * Before entering the loop, bio->bi_next is NULL (as all callers
1426 * ensure that) so we have a list with a single bio.
1427 * We pretend that we have just taken it off a longer list, so
1428 * we assign bio_list to the next (which is NULL) and bio_tail
1429 * to &bio_list, thus initialising the bio_list of new bios to be
1430 * added. __generic_make_request may indeed add some more bios
1431 * through a recursive call to generic_make_request. If it
1432 * did, we find a non-NULL value in bio_list and re-enter the loop
1433 * from the top. In this case we really did just take the bio
1434 * of the top of the list (no pretending) and so fixup bio_list and
1435 * bio_tail or bi_next, and call into __generic_make_request again.
1436 *
1437 * The loop was structured like this to make only one call to
1438 * __generic_make_request (which is important as it is large and
1439 * inlined) and to keep the structure simple.
1440 */
1441 BUG_ON(bio->bi_next);
1442 do {
1443 current->bio_list = bio->bi_next;
1444 if (bio->bi_next == NULL)
1445 current->bio_tail = &current->bio_list;
1446 else
1447 bio->bi_next = NULL;
1448 __generic_make_request(bio);
1449 bio = current->bio_list;
1450 } while (bio);
1451 current->bio_tail = NULL; /* deactivate */
1452}
1da177e4
LT
1453EXPORT_SYMBOL(generic_make_request);
1454
1455/**
1456 * submit_bio: submit a bio to the block device layer for I/O
1457 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1458 * @bio: The &struct bio which describes the I/O
1459 *
1460 * submit_bio() is very similar in purpose to generic_make_request(), and
1461 * uses that function to do most of the work. Both are fairly rough
1462 * interfaces, @bio must be presetup and ready for I/O.
1463 *
1464 */
1465void submit_bio(int rw, struct bio *bio)
1466{
1467 int count = bio_sectors(bio);
1468
22e2c507 1469 bio->bi_rw |= rw;
1da177e4 1470
bf2de6f5
JA
1471 /*
1472 * If it's a regular read/write or a barrier with data attached,
1473 * go through the normal accounting stuff before submission.
1474 */
1475 if (!bio_empty_barrier(bio)) {
1476
1477 BIO_BUG_ON(!bio->bi_size);
1478 BIO_BUG_ON(!bio->bi_io_vec);
1479
1480 if (rw & WRITE) {
1481 count_vm_events(PGPGOUT, count);
1482 } else {
1483 task_io_account_read(bio->bi_size);
1484 count_vm_events(PGPGIN, count);
1485 }
1486
1487 if (unlikely(block_dump)) {
1488 char b[BDEVNAME_SIZE];
1489 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n",
ba25f9dc 1490 current->comm, task_pid_nr(current),
bf2de6f5
JA
1491 (rw & WRITE) ? "WRITE" : "READ",
1492 (unsigned long long)bio->bi_sector,
6728cb0e 1493 bdevname(bio->bi_bdev, b));
bf2de6f5 1494 }
1da177e4
LT
1495 }
1496
1497 generic_make_request(bio);
1498}
1da177e4
LT
1499EXPORT_SYMBOL(submit_bio);
1500
3bcddeac
KU
1501/**
1502 * __end_that_request_first - end I/O on a request
1503 * @req: the request being processed
5450d3e1 1504 * @error: 0 for success, < 0 for error
3bcddeac
KU
1505 * @nr_bytes: number of bytes to complete
1506 *
1507 * Description:
1508 * Ends I/O on a number of bytes attached to @req, and sets it up
1509 * for the next range of segments (if any) in the cluster.
1510 *
1511 * Return:
1512 * 0 - we are done with this request, call end_that_request_last()
1513 * 1 - still buffers pending for this request
1514 **/
5450d3e1 1515static int __end_that_request_first(struct request *req, int error,
1da177e4
LT
1516 int nr_bytes)
1517{
5450d3e1 1518 int total_bytes, bio_nbytes, next_idx = 0;
1da177e4
LT
1519 struct bio *bio;
1520
2056a782
JA
1521 blk_add_trace_rq(req->q, req, BLK_TA_COMPLETE);
1522
1da177e4
LT
1523 /*
1524 * for a REQ_BLOCK_PC request, we want to carry any eventual
1525 * sense key with us all the way through
1526 */
1527 if (!blk_pc_request(req))
1528 req->errors = 0;
1529
6728cb0e
JA
1530 if (error && (blk_fs_request(req) && !(req->cmd_flags & REQ_QUIET))) {
1531 printk(KERN_ERR "end_request: I/O error, dev %s, sector %llu\n",
1da177e4
LT
1532 req->rq_disk ? req->rq_disk->disk_name : "?",
1533 (unsigned long long)req->sector);
1534 }
1535
d72d904a 1536 if (blk_fs_request(req) && req->rq_disk) {
a362357b
JA
1537 const int rw = rq_data_dir(req);
1538
6f2576af
JM
1539 all_stat_add(req->rq_disk, sectors[rw],
1540 nr_bytes >> 9, req->sector);
d72d904a
JA
1541 }
1542
1da177e4
LT
1543 total_bytes = bio_nbytes = 0;
1544 while ((bio = req->bio) != NULL) {
1545 int nbytes;
1546
bf2de6f5
JA
1547 /*
1548 * For an empty barrier request, the low level driver must
1549 * store a potential error location in ->sector. We pass
1550 * that back up in ->bi_sector.
1551 */
1552 if (blk_empty_barrier(req))
1553 bio->bi_sector = req->sector;
1554
1da177e4
LT
1555 if (nr_bytes >= bio->bi_size) {
1556 req->bio = bio->bi_next;
1557 nbytes = bio->bi_size;
5bb23a68 1558 req_bio_endio(req, bio, nbytes, error);
1da177e4
LT
1559 next_idx = 0;
1560 bio_nbytes = 0;
1561 } else {
1562 int idx = bio->bi_idx + next_idx;
1563
1564 if (unlikely(bio->bi_idx >= bio->bi_vcnt)) {
1565 blk_dump_rq_flags(req, "__end_that");
6728cb0e
JA
1566 printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
1567 __FUNCTION__, bio->bi_idx,
1568 bio->bi_vcnt);
1da177e4
LT
1569 break;
1570 }
1571
1572 nbytes = bio_iovec_idx(bio, idx)->bv_len;
1573 BIO_BUG_ON(nbytes > bio->bi_size);
1574
1575 /*
1576 * not a complete bvec done
1577 */
1578 if (unlikely(nbytes > nr_bytes)) {
1579 bio_nbytes += nr_bytes;
1580 total_bytes += nr_bytes;
1581 break;
1582 }
1583
1584 /*
1585 * advance to the next vector
1586 */
1587 next_idx++;
1588 bio_nbytes += nbytes;
1589 }
1590
1591 total_bytes += nbytes;
1592 nr_bytes -= nbytes;
1593
6728cb0e
JA
1594 bio = req->bio;
1595 if (bio) {
1da177e4
LT
1596 /*
1597 * end more in this run, or just return 'not-done'
1598 */
1599 if (unlikely(nr_bytes <= 0))
1600 break;
1601 }
1602 }
1603
1604 /*
1605 * completely done
1606 */
1607 if (!req->bio)
1608 return 0;
1609
1610 /*
1611 * if the request wasn't completed, update state
1612 */
1613 if (bio_nbytes) {
5bb23a68 1614 req_bio_endio(req, bio, bio_nbytes, error);
1da177e4
LT
1615 bio->bi_idx += next_idx;
1616 bio_iovec(bio)->bv_offset += nr_bytes;
1617 bio_iovec(bio)->bv_len -= nr_bytes;
1618 }
1619
1620 blk_recalc_rq_sectors(req, total_bytes >> 9);
1621 blk_recalc_rq_segments(req);
1622 return 1;
1623}
1624
ff856bad
JA
1625/*
1626 * splice the completion data to a local structure and hand off to
1627 * process_completion_queue() to complete the requests
1628 */
1629static void blk_done_softirq(struct softirq_action *h)
1630{
626ab0e6 1631 struct list_head *cpu_list, local_list;
ff856bad
JA
1632
1633 local_irq_disable();
1634 cpu_list = &__get_cpu_var(blk_cpu_done);
626ab0e6 1635 list_replace_init(cpu_list, &local_list);
ff856bad
JA
1636 local_irq_enable();
1637
1638 while (!list_empty(&local_list)) {
6728cb0e 1639 struct request *rq;
ff856bad 1640
6728cb0e 1641 rq = list_entry(local_list.next, struct request, donelist);
ff856bad
JA
1642 list_del_init(&rq->donelist);
1643 rq->q->softirq_done_fn(rq);
1644 }
1645}
1646
6728cb0e
JA
1647static int __cpuinit blk_cpu_notify(struct notifier_block *self,
1648 unsigned long action, void *hcpu)
ff856bad
JA
1649{
1650 /*
1651 * If a CPU goes away, splice its entries to the current CPU
1652 * and trigger a run of the softirq
1653 */
8bb78442 1654 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
ff856bad
JA
1655 int cpu = (unsigned long) hcpu;
1656
1657 local_irq_disable();
1658 list_splice_init(&per_cpu(blk_cpu_done, cpu),
1659 &__get_cpu_var(blk_cpu_done));
1660 raise_softirq_irqoff(BLOCK_SOFTIRQ);
1661 local_irq_enable();
1662 }
1663
1664 return NOTIFY_OK;
1665}
1666
1667
db47d475 1668static struct notifier_block blk_cpu_notifier __cpuinitdata = {
ff856bad
JA
1669 .notifier_call = blk_cpu_notify,
1670};
1671
ff856bad
JA
1672/**
1673 * blk_complete_request - end I/O on a request
1674 * @req: the request being processed
1675 *
1676 * Description:
1677 * Ends all I/O on a request. It does not handle partial completions,
d6e05edc 1678 * unless the driver actually implements this in its completion callback
4fa253f3 1679 * through requeueing. The actual completion happens out-of-order,
ff856bad
JA
1680 * through a softirq handler. The user must have registered a completion
1681 * callback through blk_queue_softirq_done().
1682 **/
1683
1684void blk_complete_request(struct request *req)
1685{
1686 struct list_head *cpu_list;
1687 unsigned long flags;
1688
1689 BUG_ON(!req->q->softirq_done_fn);
6728cb0e 1690
ff856bad
JA
1691 local_irq_save(flags);
1692
1693 cpu_list = &__get_cpu_var(blk_cpu_done);
1694 list_add_tail(&req->donelist, cpu_list);
1695 raise_softirq_irqoff(BLOCK_SOFTIRQ);
1696
1697 local_irq_restore(flags);
1698}
ff856bad 1699EXPORT_SYMBOL(blk_complete_request);
6728cb0e 1700
1da177e4
LT
1701/*
1702 * queue lock must be held
1703 */
5450d3e1 1704static void end_that_request_last(struct request *req, int error)
1da177e4
LT
1705{
1706 struct gendisk *disk = req->rq_disk;
8ffdc655 1707
b8286239
KU
1708 if (blk_rq_tagged(req))
1709 blk_queue_end_tag(req->q, req);
1710
1711 if (blk_queued_rq(req))
1712 blkdev_dequeue_request(req);
1da177e4
LT
1713
1714 if (unlikely(laptop_mode) && blk_fs_request(req))
1715 laptop_io_completion();
1716
fd0ff8aa
JA
1717 /*
1718 * Account IO completion. bar_rq isn't accounted as a normal
1719 * IO on queueing nor completion. Accounting the containing
1720 * request is enough.
1721 */
1722 if (disk && blk_fs_request(req) && req != &req->q->bar_rq) {
1da177e4 1723 unsigned long duration = jiffies - req->start_time;
a362357b 1724 const int rw = rq_data_dir(req);
6f2576af 1725 struct hd_struct *part = get_part(disk, req->sector);
a362357b 1726
6f2576af
JM
1727 __all_stat_inc(disk, ios[rw], req->sector);
1728 __all_stat_add(disk, ticks[rw], duration, req->sector);
1da177e4
LT
1729 disk_round_stats(disk);
1730 disk->in_flight--;
6f2576af
JM
1731 if (part) {
1732 part_round_stats(part);
1733 part->in_flight--;
1734 }
1da177e4 1735 }
b8286239 1736
1da177e4 1737 if (req->end_io)
8ffdc655 1738 req->end_io(req, error);
b8286239
KU
1739 else {
1740 if (blk_bidi_rq(req))
1741 __blk_put_request(req->next_rq->q, req->next_rq);
1742
1da177e4 1743 __blk_put_request(req->q, req);
b8286239 1744 }
1da177e4
LT
1745}
1746
a0cd1285 1747static inline void __end_request(struct request *rq, int uptodate,
9e6e39f2 1748 unsigned int nr_bytes)
1da177e4 1749{
9e6e39f2
KU
1750 int error = 0;
1751
1752 if (uptodate <= 0)
1753 error = uptodate ? uptodate : -EIO;
1754
1755 __blk_end_request(rq, error, nr_bytes);
1da177e4
LT
1756}
1757
3b11313a
KU
1758/**
1759 * blk_rq_bytes - Returns bytes left to complete in the entire request
5d87a052 1760 * @rq: the request being processed
3b11313a
KU
1761 **/
1762unsigned int blk_rq_bytes(struct request *rq)
a0cd1285
JA
1763{
1764 if (blk_fs_request(rq))
1765 return rq->hard_nr_sectors << 9;
1766
1767 return rq->data_len;
1768}
3b11313a
KU
1769EXPORT_SYMBOL_GPL(blk_rq_bytes);
1770
1771/**
1772 * blk_rq_cur_bytes - Returns bytes left to complete in the current segment
5d87a052 1773 * @rq: the request being processed
3b11313a
KU
1774 **/
1775unsigned int blk_rq_cur_bytes(struct request *rq)
1776{
1777 if (blk_fs_request(rq))
1778 return rq->current_nr_sectors << 9;
1779
1780 if (rq->bio)
1781 return rq->bio->bi_size;
1782
1783 return rq->data_len;
1784}
1785EXPORT_SYMBOL_GPL(blk_rq_cur_bytes);
a0cd1285
JA
1786
1787/**
1788 * end_queued_request - end all I/O on a queued request
1789 * @rq: the request being processed
1790 * @uptodate: error value or 0/1 uptodate flag
1791 *
1792 * Description:
1793 * Ends all I/O on a request, and removes it from the block layer queues.
1794 * Not suitable for normal IO completion, unless the driver still has
1795 * the request attached to the block layer.
1796 *
1797 **/
1798void end_queued_request(struct request *rq, int uptodate)
1799{
9e6e39f2 1800 __end_request(rq, uptodate, blk_rq_bytes(rq));
a0cd1285
JA
1801}
1802EXPORT_SYMBOL(end_queued_request);
1803
1804/**
1805 * end_dequeued_request - end all I/O on a dequeued request
1806 * @rq: the request being processed
1807 * @uptodate: error value or 0/1 uptodate flag
1808 *
1809 * Description:
1810 * Ends all I/O on a request. The request must already have been
1811 * dequeued using blkdev_dequeue_request(), as is normally the case
1812 * for most drivers.
1813 *
1814 **/
1815void end_dequeued_request(struct request *rq, int uptodate)
1816{
9e6e39f2 1817 __end_request(rq, uptodate, blk_rq_bytes(rq));
a0cd1285
JA
1818}
1819EXPORT_SYMBOL(end_dequeued_request);
1820
1821
1822/**
1823 * end_request - end I/O on the current segment of the request
8f731f7d 1824 * @req: the request being processed
a0cd1285
JA
1825 * @uptodate: error value or 0/1 uptodate flag
1826 *
1827 * Description:
1828 * Ends I/O on the current segment of a request. If that is the only
1829 * remaining segment, the request is also completed and freed.
1830 *
1831 * This is a remnant of how older block drivers handled IO completions.
1832 * Modern drivers typically end IO on the full request in one go, unless
1833 * they have a residual value to account for. For that case this function
1834 * isn't really useful, unless the residual just happens to be the
1835 * full current segment. In other words, don't use this function in new
1836 * code. Either use end_request_completely(), or the
1837 * end_that_request_chunk() (along with end_that_request_last()) for
1838 * partial completions.
1839 *
1840 **/
1841void end_request(struct request *req, int uptodate)
1842{
9e6e39f2 1843 __end_request(req, uptodate, req->hard_cur_sectors << 9);
a0cd1285 1844}
1da177e4
LT
1845EXPORT_SYMBOL(end_request);
1846
336cdb40 1847/**
e19a3ab0
KU
1848 * blk_end_io - Generic end_io function to complete a request.
1849 * @rq: the request being processed
1850 * @error: 0 for success, < 0 for error
e3a04fe3
KU
1851 * @nr_bytes: number of bytes to complete @rq
1852 * @bidi_bytes: number of bytes to complete @rq->next_rq
e19a3ab0
KU
1853 * @drv_callback: function called between completion of bios in the request
1854 * and completion of the request.
1855 * If the callback returns non 0, this helper returns without
1856 * completion of the request.
336cdb40
KU
1857 *
1858 * Description:
e3a04fe3 1859 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
336cdb40
KU
1860 * If @rq has leftover, sets it up for the next range of segments.
1861 *
1862 * Return:
1863 * 0 - we are done with this request
e19a3ab0 1864 * 1 - this request is not freed yet, it still has pending buffers.
336cdb40 1865 **/
22b13210
JA
1866static int blk_end_io(struct request *rq, int error, unsigned int nr_bytes,
1867 unsigned int bidi_bytes,
1868 int (drv_callback)(struct request *))
336cdb40
KU
1869{
1870 struct request_queue *q = rq->q;
1871 unsigned long flags = 0UL;
336cdb40
KU
1872
1873 if (blk_fs_request(rq) || blk_pc_request(rq)) {
5450d3e1 1874 if (__end_that_request_first(rq, error, nr_bytes))
336cdb40 1875 return 1;
e3a04fe3
KU
1876
1877 /* Bidi request must be completed as a whole */
1878 if (blk_bidi_rq(rq) &&
5450d3e1 1879 __end_that_request_first(rq->next_rq, error, bidi_bytes))
e3a04fe3 1880 return 1;
336cdb40
KU
1881 }
1882
e19a3ab0
KU
1883 /* Special feature for tricky drivers */
1884 if (drv_callback && drv_callback(rq))
1885 return 1;
1886
336cdb40
KU
1887 add_disk_randomness(rq->rq_disk);
1888
1889 spin_lock_irqsave(q->queue_lock, flags);
b8286239 1890 end_that_request_last(rq, error);
336cdb40
KU
1891 spin_unlock_irqrestore(q->queue_lock, flags);
1892
1893 return 0;
1894}
e19a3ab0
KU
1895
1896/**
1897 * blk_end_request - Helper function for drivers to complete the request.
1898 * @rq: the request being processed
1899 * @error: 0 for success, < 0 for error
1900 * @nr_bytes: number of bytes to complete
1901 *
1902 * Description:
1903 * Ends I/O on a number of bytes attached to @rq.
1904 * If @rq has leftover, sets it up for the next range of segments.
1905 *
1906 * Return:
1907 * 0 - we are done with this request
1908 * 1 - still buffers pending for this request
1909 **/
22b13210 1910int blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
e19a3ab0 1911{
e3a04fe3 1912 return blk_end_io(rq, error, nr_bytes, 0, NULL);
e19a3ab0 1913}
336cdb40
KU
1914EXPORT_SYMBOL_GPL(blk_end_request);
1915
1916/**
1917 * __blk_end_request - Helper function for drivers to complete the request.
1918 * @rq: the request being processed
1919 * @error: 0 for success, < 0 for error
1920 * @nr_bytes: number of bytes to complete
1921 *
1922 * Description:
1923 * Must be called with queue lock held unlike blk_end_request().
1924 *
1925 * Return:
1926 * 0 - we are done with this request
1927 * 1 - still buffers pending for this request
1928 **/
22b13210 1929int __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
336cdb40 1930{
336cdb40 1931 if (blk_fs_request(rq) || blk_pc_request(rq)) {
5450d3e1 1932 if (__end_that_request_first(rq, error, nr_bytes))
336cdb40
KU
1933 return 1;
1934 }
1935
1936 add_disk_randomness(rq->rq_disk);
1937
b8286239 1938 end_that_request_last(rq, error);
336cdb40
KU
1939
1940 return 0;
1941}
1942EXPORT_SYMBOL_GPL(__blk_end_request);
1943
e3a04fe3
KU
1944/**
1945 * blk_end_bidi_request - Helper function for drivers to complete bidi request.
1946 * @rq: the bidi request being processed
1947 * @error: 0 for success, < 0 for error
1948 * @nr_bytes: number of bytes to complete @rq
1949 * @bidi_bytes: number of bytes to complete @rq->next_rq
1950 *
1951 * Description:
1952 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
1953 *
1954 * Return:
1955 * 0 - we are done with this request
1956 * 1 - still buffers pending for this request
1957 **/
22b13210
JA
1958int blk_end_bidi_request(struct request *rq, int error, unsigned int nr_bytes,
1959 unsigned int bidi_bytes)
e3a04fe3
KU
1960{
1961 return blk_end_io(rq, error, nr_bytes, bidi_bytes, NULL);
1962}
1963EXPORT_SYMBOL_GPL(blk_end_bidi_request);
1964
e19a3ab0
KU
1965/**
1966 * blk_end_request_callback - Special helper function for tricky drivers
1967 * @rq: the request being processed
1968 * @error: 0 for success, < 0 for error
1969 * @nr_bytes: number of bytes to complete
1970 * @drv_callback: function called between completion of bios in the request
1971 * and completion of the request.
1972 * If the callback returns non 0, this helper returns without
1973 * completion of the request.
1974 *
1975 * Description:
1976 * Ends I/O on a number of bytes attached to @rq.
1977 * If @rq has leftover, sets it up for the next range of segments.
1978 *
1979 * This special helper function is used only for existing tricky drivers.
1980 * (e.g. cdrom_newpc_intr() of ide-cd)
1981 * This interface will be removed when such drivers are rewritten.
1982 * Don't use this interface in other places anymore.
1983 *
1984 * Return:
1985 * 0 - we are done with this request
1986 * 1 - this request is not freed yet.
1987 * this request still has pending buffers or
1988 * the driver doesn't want to finish this request yet.
1989 **/
22b13210
JA
1990int blk_end_request_callback(struct request *rq, int error,
1991 unsigned int nr_bytes,
e19a3ab0
KU
1992 int (drv_callback)(struct request *))
1993{
e3a04fe3 1994 return blk_end_io(rq, error, nr_bytes, 0, drv_callback);
e19a3ab0
KU
1995}
1996EXPORT_SYMBOL_GPL(blk_end_request_callback);
1997
86db1e29
JA
1998void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
1999 struct bio *bio)
1da177e4 2000{
4aff5e23
JA
2001 /* first two bits are identical in rq->cmd_flags and bio->bi_rw */
2002 rq->cmd_flags |= (bio->bi_rw & 3);
1da177e4
LT
2003
2004 rq->nr_phys_segments = bio_phys_segments(q, bio);
2005 rq->nr_hw_segments = bio_hw_segments(q, bio);
2006 rq->current_nr_sectors = bio_cur_sectors(bio);
2007 rq->hard_cur_sectors = rq->current_nr_sectors;
2008 rq->hard_nr_sectors = rq->nr_sectors = bio_sectors(bio);
2009 rq->buffer = bio_data(bio);
0e75f906 2010 rq->data_len = bio->bi_size;
1da177e4
LT
2011
2012 rq->bio = rq->biotail = bio;
1da177e4 2013
66846572
N
2014 if (bio->bi_bdev)
2015 rq->rq_disk = bio->bi_bdev->bd_disk;
2016}
1da177e4
LT
2017
2018int kblockd_schedule_work(struct work_struct *work)
2019{
2020 return queue_work(kblockd_workqueue, work);
2021}
1da177e4
LT
2022EXPORT_SYMBOL(kblockd_schedule_work);
2023
19a75d83 2024void kblockd_flush_work(struct work_struct *work)
1da177e4 2025{
28e53bdd 2026 cancel_work_sync(work);
1da177e4 2027}
19a75d83 2028EXPORT_SYMBOL(kblockd_flush_work);
1da177e4
LT
2029
2030int __init blk_dev_init(void)
2031{
ff856bad
JA
2032 int i;
2033
1da177e4
LT
2034 kblockd_workqueue = create_workqueue("kblockd");
2035 if (!kblockd_workqueue)
2036 panic("Failed to create kblockd\n");
2037
2038 request_cachep = kmem_cache_create("blkdev_requests",
20c2df83 2039 sizeof(struct request), 0, SLAB_PANIC, NULL);
1da177e4 2040
8324aa91 2041 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
165125e1 2042 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
1da177e4 2043
0a945022 2044 for_each_possible_cpu(i)
ff856bad
JA
2045 INIT_LIST_HEAD(&per_cpu(blk_cpu_done, i));
2046
2047 open_softirq(BLOCK_SOFTIRQ, blk_done_softirq, NULL);
5a67e4c5 2048 register_hotcpu_notifier(&blk_cpu_notifier);
ff856bad 2049
d38ecf93 2050 return 0;
1da177e4 2051}
1da177e4 2052