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