Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / block / blk-core.c
blob792709f33f2080e44e97653498c68a7829ed4899
1 /*
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>
6 * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au>
7 * - July2000
8 * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
9 */
12 * This handles all read/write requests to block devices
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>
24 #include <linux/completion.h>
25 #include <linux/slab.h>
26 #include <linux/swap.h>
27 #include <linux/writeback.h>
28 #include <linux/task_io_accounting_ops.h>
29 #include <linux/interrupt.h>
30 #include <linux/cpu.h>
31 #include <linux/blktrace_api.h>
32 #include <linux/fault-inject.h>
34 #include "blk.h"
36 static int __make_request(struct request_queue *q, struct bio *bio);
39 * For the allocated request tables
41 <<<<<<< HEAD:block/blk-core.c
42 struct kmem_cache *request_cachep;
43 =======
44 static struct kmem_cache *request_cachep;
45 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:block/blk-core.c
48 * For queue allocation
50 struct kmem_cache *blk_requestq_cachep;
53 * Controlling structure to kblockd
55 static struct workqueue_struct *kblockd_workqueue;
57 static DEFINE_PER_CPU(struct list_head, blk_cpu_done);
59 static void drive_stat_acct(struct request *rq, int new_io)
61 int rw = rq_data_dir(rq);
63 if (!blk_fs_request(rq) || !rq->rq_disk)
64 return;
66 if (!new_io) {
67 __all_stat_inc(rq->rq_disk, merges[rw], rq->sector);
68 } else {
69 struct hd_struct *part = get_part(rq->rq_disk, rq->sector);
70 disk_round_stats(rq->rq_disk);
71 rq->rq_disk->in_flight++;
72 if (part) {
73 part_round_stats(part);
74 part->in_flight++;
79 void blk_queue_congestion_threshold(struct request_queue *q)
81 int nr;
83 nr = q->nr_requests - (q->nr_requests / 8) + 1;
84 if (nr > q->nr_requests)
85 nr = q->nr_requests;
86 q->nr_congestion_on = nr;
88 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
89 if (nr < 1)
90 nr = 1;
91 q->nr_congestion_off = nr;
94 /**
95 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
96 * @bdev: device
98 * Locates the passed device's request queue and returns the address of its
99 * backing_dev_info
101 * Will return NULL if the request queue cannot be located.
103 struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
105 struct backing_dev_info *ret = NULL;
106 struct request_queue *q = bdev_get_queue(bdev);
108 if (q)
109 ret = &q->backing_dev_info;
110 return ret;
112 EXPORT_SYMBOL(blk_get_backing_dev_info);
115 * We can't just memset() the structure, since the allocation path
116 * already stored some information in the request.
118 void rq_init(struct request_queue *q, struct request *rq)
120 INIT_LIST_HEAD(&rq->queuelist);
121 INIT_LIST_HEAD(&rq->donelist);
122 rq->q = q;
123 rq->sector = rq->hard_sector = (sector_t) -1;
124 rq->nr_sectors = rq->hard_nr_sectors = 0;
125 rq->current_nr_sectors = rq->hard_cur_sectors = 0;
126 rq->bio = rq->biotail = NULL;
127 INIT_HLIST_NODE(&rq->hash);
128 RB_CLEAR_NODE(&rq->rb_node);
129 rq->rq_disk = NULL;
130 rq->nr_phys_segments = 0;
131 rq->nr_hw_segments = 0;
132 rq->ioprio = 0;
133 rq->special = NULL;
134 rq->buffer = NULL;
135 rq->tag = -1;
136 rq->errors = 0;
137 rq->ref_count = 1;
138 rq->cmd_len = 0;
139 memset(rq->cmd, 0, sizeof(rq->cmd));
140 rq->data_len = 0;
141 <<<<<<< HEAD:block/blk-core.c
142 =======
143 rq->extra_len = 0;
144 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:block/blk-core.c
145 rq->sense_len = 0;
146 rq->data = NULL;
147 rq->sense = NULL;
148 rq->end_io = NULL;
149 rq->end_io_data = NULL;
150 rq->next_rq = NULL;
153 static void req_bio_endio(struct request *rq, struct bio *bio,
154 unsigned int nbytes, int error)
156 struct request_queue *q = rq->q;
158 if (&q->bar_rq != rq) {
159 if (error)
160 clear_bit(BIO_UPTODATE, &bio->bi_flags);
161 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
162 error = -EIO;
164 if (unlikely(nbytes > bio->bi_size)) {
165 printk(KERN_ERR "%s: want %u bytes done, %u left\n",
166 __FUNCTION__, nbytes, bio->bi_size);
167 nbytes = bio->bi_size;
170 bio->bi_size -= nbytes;
171 bio->bi_sector += (nbytes >> 9);
172 if (bio->bi_size == 0)
173 bio_endio(bio, error);
174 } else {
177 * Okay, this is the barrier request in progress, just
178 * record the error;
180 if (error && !q->orderr)
181 q->orderr = error;
185 void blk_dump_rq_flags(struct request *rq, char *msg)
187 int bit;
189 printk(KERN_INFO "%s: dev %s: type=%x, flags=%x\n", msg,
190 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
191 rq->cmd_flags);
193 printk(KERN_INFO " sector %llu, nr/cnr %lu/%u\n",
194 (unsigned long long)rq->sector,
195 rq->nr_sectors,
196 rq->current_nr_sectors);
197 printk(KERN_INFO " bio %p, biotail %p, buffer %p, data %p, len %u\n",
198 rq->bio, rq->biotail,
199 rq->buffer, rq->data,
200 rq->data_len);
202 if (blk_pc_request(rq)) {
203 printk(KERN_INFO " cdb: ");
204 for (bit = 0; bit < sizeof(rq->cmd); bit++)
205 printk("%02x ", rq->cmd[bit]);
206 printk("\n");
209 EXPORT_SYMBOL(blk_dump_rq_flags);
212 * "plug" the device if there are no outstanding requests: this will
213 * force the transfer to start only after we have put all the requests
214 * on the list.
216 * This is called with interrupts off and no requests on the queue and
217 * with the queue lock held.
219 void blk_plug_device(struct request_queue *q)
221 WARN_ON(!irqs_disabled());
224 * don't plug a stopped queue, it must be paired with blk_start_queue()
225 * which will restart the queueing
227 if (blk_queue_stopped(q))
228 return;
230 if (!test_and_set_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags)) {
231 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
232 blk_add_trace_generic(q, NULL, 0, BLK_TA_PLUG);
235 EXPORT_SYMBOL(blk_plug_device);
238 * remove the queue from the plugged list, if present. called with
239 * queue lock held and interrupts disabled.
241 int blk_remove_plug(struct request_queue *q)
243 WARN_ON(!irqs_disabled());
245 if (!test_and_clear_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags))
246 return 0;
248 del_timer(&q->unplug_timer);
249 return 1;
251 EXPORT_SYMBOL(blk_remove_plug);
254 * remove the plug and let it rip..
256 void __generic_unplug_device(struct request_queue *q)
258 if (unlikely(blk_queue_stopped(q)))
259 return;
261 if (!blk_remove_plug(q))
262 return;
264 q->request_fn(q);
266 EXPORT_SYMBOL(__generic_unplug_device);
269 * generic_unplug_device - fire a request queue
270 * @q: The &struct request_queue in question
272 * Description:
273 * Linux uses plugging to build bigger requests queues before letting
274 * the device have at them. If a queue is plugged, the I/O scheduler
275 * is still adding and merging requests on the queue. Once the queue
276 * gets unplugged, the request_fn defined for the queue is invoked and
277 * transfers started.
279 void generic_unplug_device(struct request_queue *q)
281 spin_lock_irq(q->queue_lock);
282 __generic_unplug_device(q);
283 spin_unlock_irq(q->queue_lock);
285 EXPORT_SYMBOL(generic_unplug_device);
287 static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
288 struct page *page)
290 struct request_queue *q = bdi->unplug_io_data;
292 blk_unplug(q);
295 void blk_unplug_work(struct work_struct *work)
297 struct request_queue *q =
298 container_of(work, struct request_queue, unplug_work);
300 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
301 q->rq.count[READ] + q->rq.count[WRITE]);
303 q->unplug_fn(q);
306 void blk_unplug_timeout(unsigned long data)
308 struct request_queue *q = (struct request_queue *)data;
310 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_TIMER, NULL,
311 q->rq.count[READ] + q->rq.count[WRITE]);
313 kblockd_schedule_work(&q->unplug_work);
316 void blk_unplug(struct request_queue *q)
319 * devices don't necessarily have an ->unplug_fn defined
321 if (q->unplug_fn) {
322 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
323 q->rq.count[READ] + q->rq.count[WRITE]);
325 q->unplug_fn(q);
328 EXPORT_SYMBOL(blk_unplug);
331 * blk_start_queue - restart a previously stopped queue
332 * @q: The &struct request_queue in question
334 * Description:
335 * blk_start_queue() will clear the stop flag on the queue, and call
336 * the request_fn for the queue if it was in a stopped state when
337 * entered. Also see blk_stop_queue(). Queue lock must be held.
339 void blk_start_queue(struct request_queue *q)
341 WARN_ON(!irqs_disabled());
343 clear_bit(QUEUE_FLAG_STOPPED, &q->queue_flags);
346 * one level of recursion is ok and is much faster than kicking
347 * the unplug handling
349 if (!test_and_set_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) {
350 q->request_fn(q);
351 clear_bit(QUEUE_FLAG_REENTER, &q->queue_flags);
352 } else {
353 blk_plug_device(q);
354 kblockd_schedule_work(&q->unplug_work);
357 EXPORT_SYMBOL(blk_start_queue);
360 * blk_stop_queue - stop a queue
361 * @q: The &struct request_queue in question
363 * Description:
364 * The Linux block layer assumes that a block driver will consume all
365 * entries on the request queue when the request_fn strategy is called.
366 * Often this will not happen, because of hardware limitations (queue
367 * depth settings). If a device driver gets a 'queue full' response,
368 * or if it simply chooses not to queue more I/O at one point, it can
369 * call this function to prevent the request_fn from being called until
370 * the driver has signalled it's ready to go again. This happens by calling
371 * blk_start_queue() to restart queue operations. Queue lock must be held.
373 void blk_stop_queue(struct request_queue *q)
375 blk_remove_plug(q);
376 set_bit(QUEUE_FLAG_STOPPED, &q->queue_flags);
378 EXPORT_SYMBOL(blk_stop_queue);
381 * blk_sync_queue - cancel any pending callbacks on a queue
382 * @q: the queue
384 * Description:
385 * The block layer may perform asynchronous callback activity
386 * on a queue, such as calling the unplug function after a timeout.
387 * A block device may call blk_sync_queue to ensure that any
388 * such activity is cancelled, thus allowing it to release resources
389 * that the callbacks might use. The caller must already have made sure
390 * that its ->make_request_fn will not re-add plugging prior to calling
391 * this function.
394 void blk_sync_queue(struct request_queue *q)
396 del_timer_sync(&q->unplug_timer);
397 kblockd_flush_work(&q->unplug_work);
399 EXPORT_SYMBOL(blk_sync_queue);
402 * blk_run_queue - run a single device queue
403 * @q: The queue to run
405 void blk_run_queue(struct request_queue *q)
407 unsigned long flags;
409 spin_lock_irqsave(q->queue_lock, flags);
410 blk_remove_plug(q);
413 * Only recurse once to avoid overrunning the stack, let the unplug
414 * handling reinvoke the handler shortly if we already got there.
416 if (!elv_queue_empty(q)) {
417 if (!test_and_set_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) {
418 q->request_fn(q);
419 clear_bit(QUEUE_FLAG_REENTER, &q->queue_flags);
420 } else {
421 blk_plug_device(q);
422 kblockd_schedule_work(&q->unplug_work);
426 spin_unlock_irqrestore(q->queue_lock, flags);
428 EXPORT_SYMBOL(blk_run_queue);
430 void blk_put_queue(struct request_queue *q)
432 kobject_put(&q->kobj);
434 <<<<<<< HEAD:block/blk-core.c
435 EXPORT_SYMBOL(blk_put_queue);
436 =======
437 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:block/blk-core.c
439 void blk_cleanup_queue(struct request_queue *q)
441 mutex_lock(&q->sysfs_lock);
442 set_bit(QUEUE_FLAG_DEAD, &q->queue_flags);
443 mutex_unlock(&q->sysfs_lock);
445 if (q->elevator)
446 elevator_exit(q->elevator);
448 blk_put_queue(q);
450 EXPORT_SYMBOL(blk_cleanup_queue);
452 static int blk_init_free_list(struct request_queue *q)
454 struct request_list *rl = &q->rq;
456 rl->count[READ] = rl->count[WRITE] = 0;
457 rl->starved[READ] = rl->starved[WRITE] = 0;
458 rl->elvpriv = 0;
459 init_waitqueue_head(&rl->wait[READ]);
460 init_waitqueue_head(&rl->wait[WRITE]);
462 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
463 mempool_free_slab, request_cachep, q->node);
465 if (!rl->rq_pool)
466 return -ENOMEM;
468 return 0;
471 struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
473 return blk_alloc_queue_node(gfp_mask, -1);
475 EXPORT_SYMBOL(blk_alloc_queue);
477 struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
479 struct request_queue *q;
480 int err;
482 q = kmem_cache_alloc_node(blk_requestq_cachep,
483 gfp_mask | __GFP_ZERO, node_id);
484 if (!q)
485 return NULL;
487 q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
488 q->backing_dev_info.unplug_io_data = q;
489 err = bdi_init(&q->backing_dev_info);
490 if (err) {
491 kmem_cache_free(blk_requestq_cachep, q);
492 return NULL;
495 init_timer(&q->unplug_timer);
497 kobject_init(&q->kobj, &blk_queue_ktype);
499 mutex_init(&q->sysfs_lock);
501 return q;
503 EXPORT_SYMBOL(blk_alloc_queue_node);
506 * blk_init_queue - prepare a request queue for use with a block device
507 * @rfn: The function to be called to process requests that have been
508 * placed on the queue.
509 * @lock: Request queue spin lock
511 * Description:
512 * If a block device wishes to use the standard request handling procedures,
513 * which sorts requests and coalesces adjacent requests, then it must
514 * call blk_init_queue(). The function @rfn will be called when there
515 * are requests on the queue that need to be processed. If the device
516 * supports plugging, then @rfn may not be called immediately when requests
517 * are available on the queue, but may be called at some time later instead.
518 * Plugged queues are generally unplugged when a buffer belonging to one
519 * of the requests on the queue is needed, or due to memory pressure.
521 * @rfn is not required, or even expected, to remove all requests off the
522 * queue, but only as many as it can handle at a time. If it does leave
523 * requests on the queue, it is responsible for arranging that the requests
524 * get dealt with eventually.
526 * The queue spin lock must be held while manipulating the requests on the
527 * request queue; this lock will be taken also from interrupt context, so irq
528 * disabling is needed for it.
530 * Function returns a pointer to the initialized request queue, or NULL if
531 * it didn't succeed.
533 * Note:
534 * blk_init_queue() must be paired with a blk_cleanup_queue() call
535 * when the block device is deactivated (such as at module unload).
538 struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
540 return blk_init_queue_node(rfn, lock, -1);
542 EXPORT_SYMBOL(blk_init_queue);
544 struct request_queue *
545 blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
547 struct request_queue *q = blk_alloc_queue_node(GFP_KERNEL, node_id);
549 if (!q)
550 return NULL;
552 q->node = node_id;
553 if (blk_init_free_list(q)) {
554 kmem_cache_free(blk_requestq_cachep, q);
555 return NULL;
559 * if caller didn't supply a lock, they get per-queue locking with
560 * our embedded lock
562 if (!lock) {
563 spin_lock_init(&q->__queue_lock);
564 lock = &q->__queue_lock;
567 q->request_fn = rfn;
568 q->prep_rq_fn = NULL;
569 q->unplug_fn = generic_unplug_device;
570 q->queue_flags = (1 << QUEUE_FLAG_CLUSTER);
571 q->queue_lock = lock;
573 blk_queue_segment_boundary(q, 0xffffffff);
575 blk_queue_make_request(q, __make_request);
576 blk_queue_max_segment_size(q, MAX_SEGMENT_SIZE);
578 blk_queue_max_hw_segments(q, MAX_HW_SEGMENTS);
579 blk_queue_max_phys_segments(q, MAX_PHYS_SEGMENTS);
581 q->sg_reserved_size = INT_MAX;
584 * all done
586 if (!elevator_init(q, NULL)) {
587 blk_queue_congestion_threshold(q);
588 return q;
591 blk_put_queue(q);
592 return NULL;
594 EXPORT_SYMBOL(blk_init_queue_node);
596 int blk_get_queue(struct request_queue *q)
598 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
599 kobject_get(&q->kobj);
600 return 0;
603 return 1;
605 <<<<<<< HEAD:block/blk-core.c
606 EXPORT_SYMBOL(blk_get_queue);
607 =======
608 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:block/blk-core.c
610 static inline void blk_free_request(struct request_queue *q, struct request *rq)
612 if (rq->cmd_flags & REQ_ELVPRIV)
613 elv_put_request(q, rq);
614 mempool_free(rq, q->rq.rq_pool);
617 static struct request *
618 blk_alloc_request(struct request_queue *q, int rw, int priv, gfp_t gfp_mask)
620 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
622 if (!rq)
623 return NULL;
626 * first three bits are identical in rq->cmd_flags and bio->bi_rw,
627 * see bio.h and blkdev.h
629 rq->cmd_flags = rw | REQ_ALLOCED;
631 if (priv) {
632 if (unlikely(elv_set_request(q, rq, gfp_mask))) {
633 mempool_free(rq, q->rq.rq_pool);
634 return NULL;
636 rq->cmd_flags |= REQ_ELVPRIV;
639 return rq;
643 * ioc_batching returns true if the ioc is a valid batching request and
644 * should be given priority access to a request.
646 static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
648 if (!ioc)
649 return 0;
652 * Make sure the process is able to allocate at least 1 request
653 * even if the batch times out, otherwise we could theoretically
654 * lose wakeups.
656 return ioc->nr_batch_requests == q->nr_batching ||
657 (ioc->nr_batch_requests > 0
658 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
662 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
663 * will cause the process to be a "batcher" on all queues in the system. This
664 * is the behaviour we want though - once it gets a wakeup it should be given
665 * a nice run.
667 static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
669 if (!ioc || ioc_batching(q, ioc))
670 return;
672 ioc->nr_batch_requests = q->nr_batching;
673 ioc->last_waited = jiffies;
676 static void __freed_request(struct request_queue *q, int rw)
678 struct request_list *rl = &q->rq;
680 if (rl->count[rw] < queue_congestion_off_threshold(q))
681 blk_clear_queue_congested(q, rw);
683 if (rl->count[rw] + 1 <= q->nr_requests) {
684 if (waitqueue_active(&rl->wait[rw]))
685 wake_up(&rl->wait[rw]);
687 blk_clear_queue_full(q, rw);
692 * A request has just been released. Account for it, update the full and
693 * congestion status, wake up any waiters. Called under q->queue_lock.
695 static void freed_request(struct request_queue *q, int rw, int priv)
697 struct request_list *rl = &q->rq;
699 rl->count[rw]--;
700 if (priv)
701 rl->elvpriv--;
703 __freed_request(q, rw);
705 if (unlikely(rl->starved[rw ^ 1]))
706 __freed_request(q, rw ^ 1);
709 #define blkdev_free_rq(list) list_entry((list)->next, struct request, queuelist)
711 * Get a free request, queue_lock must be held.
712 * Returns NULL on failure, with queue_lock held.
713 * Returns !NULL on success, with queue_lock *not held*.
715 static struct request *get_request(struct request_queue *q, int rw_flags,
716 struct bio *bio, gfp_t gfp_mask)
718 struct request *rq = NULL;
719 struct request_list *rl = &q->rq;
720 struct io_context *ioc = NULL;
721 const int rw = rw_flags & 0x01;
722 int may_queue, priv;
724 may_queue = elv_may_queue(q, rw_flags);
725 if (may_queue == ELV_MQUEUE_NO)
726 goto rq_starved;
728 if (rl->count[rw]+1 >= queue_congestion_on_threshold(q)) {
729 if (rl->count[rw]+1 >= q->nr_requests) {
730 ioc = current_io_context(GFP_ATOMIC, q->node);
732 * The queue will fill after this allocation, so set
733 * it as full, and mark this process as "batching".
734 * This process will be allowed to complete a batch of
735 * requests, others will be blocked.
737 if (!blk_queue_full(q, rw)) {
738 ioc_set_batching(q, ioc);
739 blk_set_queue_full(q, rw);
740 } else {
741 if (may_queue != ELV_MQUEUE_MUST
742 && !ioc_batching(q, ioc)) {
744 * The queue is full and the allocating
745 * process is not a "batcher", and not
746 * exempted by the IO scheduler
748 goto out;
752 blk_set_queue_congested(q, rw);
756 * Only allow batching queuers to allocate up to 50% over the defined
757 * limit of requests, otherwise we could have thousands of requests
758 * allocated with any setting of ->nr_requests
760 if (rl->count[rw] >= (3 * q->nr_requests / 2))
761 goto out;
763 rl->count[rw]++;
764 rl->starved[rw] = 0;
766 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
767 if (priv)
768 rl->elvpriv++;
770 spin_unlock_irq(q->queue_lock);
772 rq = blk_alloc_request(q, rw_flags, priv, gfp_mask);
773 if (unlikely(!rq)) {
775 * Allocation failed presumably due to memory. Undo anything
776 * we might have messed up.
778 * Allocating task should really be put onto the front of the
779 * wait queue, but this is pretty rare.
781 spin_lock_irq(q->queue_lock);
782 freed_request(q, rw, priv);
785 * in the very unlikely event that allocation failed and no
786 * requests for this direction was pending, mark us starved
787 * so that freeing of a request in the other direction will
788 * notice us. another possible fix would be to split the
789 * rq mempool into READ and WRITE
791 rq_starved:
792 if (unlikely(rl->count[rw] == 0))
793 rl->starved[rw] = 1;
795 goto out;
799 * ioc may be NULL here, and ioc_batching will be false. That's
800 * OK, if the queue is under the request limit then requests need
801 * not count toward the nr_batch_requests limit. There will always
802 * be some limit enforced by BLK_BATCH_TIME.
804 if (ioc_batching(q, ioc))
805 ioc->nr_batch_requests--;
807 rq_init(q, rq);
809 blk_add_trace_generic(q, bio, rw, BLK_TA_GETRQ);
810 out:
811 return rq;
815 * No available requests for this queue, unplug the device and wait for some
816 * requests to become available.
818 * Called with q->queue_lock held, and returns with it unlocked.
820 static struct request *get_request_wait(struct request_queue *q, int rw_flags,
821 struct bio *bio)
823 const int rw = rw_flags & 0x01;
824 struct request *rq;
826 rq = get_request(q, rw_flags, bio, GFP_NOIO);
827 while (!rq) {
828 DEFINE_WAIT(wait);
829 struct request_list *rl = &q->rq;
831 prepare_to_wait_exclusive(&rl->wait[rw], &wait,
832 TASK_UNINTERRUPTIBLE);
834 rq = get_request(q, rw_flags, bio, GFP_NOIO);
836 if (!rq) {
837 struct io_context *ioc;
839 blk_add_trace_generic(q, bio, rw, BLK_TA_SLEEPRQ);
841 __generic_unplug_device(q);
842 spin_unlock_irq(q->queue_lock);
843 io_schedule();
846 * After sleeping, we become a "batching" process and
847 * will be able to allocate at least one request, and
848 * up to a big batch of them for a small period time.
849 * See ioc_batching, ioc_set_batching
851 ioc = current_io_context(GFP_NOIO, q->node);
852 ioc_set_batching(q, ioc);
854 spin_lock_irq(q->queue_lock);
856 finish_wait(&rl->wait[rw], &wait);
859 return rq;
862 struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
864 struct request *rq;
866 BUG_ON(rw != READ && rw != WRITE);
868 spin_lock_irq(q->queue_lock);
869 if (gfp_mask & __GFP_WAIT) {
870 rq = get_request_wait(q, rw, NULL);
871 } else {
872 rq = get_request(q, rw, NULL, gfp_mask);
873 if (!rq)
874 spin_unlock_irq(q->queue_lock);
876 /* q->queue_lock is unlocked at this point */
878 return rq;
880 EXPORT_SYMBOL(blk_get_request);
883 * blk_start_queueing - initiate dispatch of requests to device
884 * @q: request queue to kick into gear
886 * This is basically a helper to remove the need to know whether a queue
887 * is plugged or not if someone just wants to initiate dispatch of requests
888 * for this queue.
890 * The queue lock must be held with interrupts disabled.
892 void blk_start_queueing(struct request_queue *q)
894 if (!blk_queue_plugged(q))
895 q->request_fn(q);
896 else
897 __generic_unplug_device(q);
899 EXPORT_SYMBOL(blk_start_queueing);
902 * blk_requeue_request - put a request back on queue
903 * @q: request queue where request should be inserted
904 * @rq: request to be inserted
906 * Description:
907 * Drivers often keep queueing requests until the hardware cannot accept
908 * more, when that condition happens we need to put the request back
909 * on the queue. Must be called with queue lock held.
911 void blk_requeue_request(struct request_queue *q, struct request *rq)
913 blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
915 if (blk_rq_tagged(rq))
916 blk_queue_end_tag(q, rq);
918 elv_requeue_request(q, rq);
920 EXPORT_SYMBOL(blk_requeue_request);
923 * blk_insert_request - insert a special request in to a request queue
924 * @q: request queue where request should be inserted
925 * @rq: request to be inserted
926 * @at_head: insert request at head or tail of queue
927 * @data: private data
929 * Description:
930 * Many block devices need to execute commands asynchronously, so they don't
931 * block the whole kernel from preemption during request execution. This is
932 * accomplished normally by inserting aritficial requests tagged as
933 * REQ_SPECIAL in to the corresponding request queue, and letting them be
934 * scheduled for actual execution by the request queue.
936 * We have the option of inserting the head or the tail of the queue.
937 * Typically we use the tail for new ioctls and so forth. We use the head
938 * of the queue for things like a QUEUE_FULL message from a device, or a
939 * host that is unable to accept a particular command.
941 void blk_insert_request(struct request_queue *q, struct request *rq,
942 int at_head, void *data)
944 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
945 unsigned long flags;
948 * tell I/O scheduler that this isn't a regular read/write (ie it
949 * must not attempt merges on this) and that it acts as a soft
950 * barrier
952 rq->cmd_type = REQ_TYPE_SPECIAL;
953 rq->cmd_flags |= REQ_SOFTBARRIER;
955 rq->special = data;
957 spin_lock_irqsave(q->queue_lock, flags);
960 * If command is tagged, release the tag
962 if (blk_rq_tagged(rq))
963 blk_queue_end_tag(q, rq);
965 drive_stat_acct(rq, 1);
966 __elv_add_request(q, rq, where, 0);
967 blk_start_queueing(q);
968 spin_unlock_irqrestore(q->queue_lock, flags);
970 EXPORT_SYMBOL(blk_insert_request);
973 * add-request adds a request to the linked list.
974 * queue lock is held and interrupts disabled, as we muck with the
975 * request queue list.
977 static inline void add_request(struct request_queue *q, struct request *req)
979 drive_stat_acct(req, 1);
982 * elevator indicated where it wants this request to be
983 * inserted at elevator_merge time
985 __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
989 * disk_round_stats() - Round off the performance stats on a struct
990 * disk_stats.
992 * The average IO queue length and utilisation statistics are maintained
993 * by observing the current state of the queue length and the amount of
994 * time it has been in this state for.
996 * Normally, that accounting is done on IO completion, but that can result
997 * in more than a second's worth of IO being accounted for within any one
998 * second, leading to >100% utilisation. To deal with that, we call this
999 * function to do a round-off before returning the results when reading
1000 * /proc/diskstats. This accounts immediately for all queue usage up to
1001 * the current jiffies and restarts the counters again.
1003 void disk_round_stats(struct gendisk *disk)
1005 unsigned long now = jiffies;
1007 if (now == disk->stamp)
1008 return;
1010 if (disk->in_flight) {
1011 __disk_stat_add(disk, time_in_queue,
1012 disk->in_flight * (now - disk->stamp));
1013 __disk_stat_add(disk, io_ticks, (now - disk->stamp));
1015 disk->stamp = now;
1017 EXPORT_SYMBOL_GPL(disk_round_stats);
1019 void part_round_stats(struct hd_struct *part)
1021 unsigned long now = jiffies;
1023 if (now == part->stamp)
1024 return;
1026 if (part->in_flight) {
1027 __part_stat_add(part, time_in_queue,
1028 part->in_flight * (now - part->stamp));
1029 __part_stat_add(part, io_ticks, (now - part->stamp));
1031 part->stamp = now;
1035 * queue lock must be held
1037 void __blk_put_request(struct request_queue *q, struct request *req)
1039 if (unlikely(!q))
1040 return;
1041 if (unlikely(--req->ref_count))
1042 return;
1044 elv_completed_request(q, req);
1047 * Request may not have originated from ll_rw_blk. if not,
1048 * it didn't come out of our reserved rq pools
1050 if (req->cmd_flags & REQ_ALLOCED) {
1051 int rw = rq_data_dir(req);
1052 int priv = req->cmd_flags & REQ_ELVPRIV;
1054 BUG_ON(!list_empty(&req->queuelist));
1055 BUG_ON(!hlist_unhashed(&req->hash));
1057 blk_free_request(q, req);
1058 freed_request(q, rw, priv);
1061 EXPORT_SYMBOL_GPL(__blk_put_request);
1063 void blk_put_request(struct request *req)
1065 unsigned long flags;
1066 struct request_queue *q = req->q;
1069 * Gee, IDE calls in w/ NULL q. Fix IDE and remove the
1070 * following if (q) test.
1072 if (q) {
1073 spin_lock_irqsave(q->queue_lock, flags);
1074 __blk_put_request(q, req);
1075 spin_unlock_irqrestore(q->queue_lock, flags);
1078 EXPORT_SYMBOL(blk_put_request);
1080 void init_request_from_bio(struct request *req, struct bio *bio)
1082 req->cmd_type = REQ_TYPE_FS;
1085 * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
1087 if (bio_rw_ahead(bio) || bio_failfast(bio))
1088 req->cmd_flags |= REQ_FAILFAST;
1091 * REQ_BARRIER implies no merging, but lets make it explicit
1093 if (unlikely(bio_barrier(bio)))
1094 req->cmd_flags |= (REQ_HARDBARRIER | REQ_NOMERGE);
1096 if (bio_sync(bio))
1097 req->cmd_flags |= REQ_RW_SYNC;
1098 if (bio_rw_meta(bio))
1099 req->cmd_flags |= REQ_RW_META;
1101 req->errors = 0;
1102 req->hard_sector = req->sector = bio->bi_sector;
1103 req->ioprio = bio_prio(bio);
1104 req->start_time = jiffies;
1105 blk_rq_bio_prep(req->q, req, bio);
1108 static int __make_request(struct request_queue *q, struct bio *bio)
1110 struct request *req;
1111 int el_ret, nr_sectors, barrier, err;
1112 const unsigned short prio = bio_prio(bio);
1113 const int sync = bio_sync(bio);
1114 int rw_flags;
1116 nr_sectors = bio_sectors(bio);
1119 * low level driver can indicate that it wants pages above a
1120 * certain limit bounced to low memory (ie for highmem, or even
1121 * ISA dma in theory)
1123 blk_queue_bounce(q, &bio);
1125 barrier = bio_barrier(bio);
1126 if (unlikely(barrier) && (q->next_ordered == QUEUE_ORDERED_NONE)) {
1127 err = -EOPNOTSUPP;
1128 goto end_io;
1131 spin_lock_irq(q->queue_lock);
1133 if (unlikely(barrier) || elv_queue_empty(q))
1134 goto get_rq;
1136 el_ret = elv_merge(q, &req, bio);
1137 switch (el_ret) {
1138 case ELEVATOR_BACK_MERGE:
1139 BUG_ON(!rq_mergeable(req));
1141 if (!ll_back_merge_fn(q, req, bio))
1142 break;
1144 blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
1146 req->biotail->bi_next = bio;
1147 req->biotail = bio;
1148 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
1149 req->ioprio = ioprio_best(req->ioprio, prio);
1150 drive_stat_acct(req, 0);
1151 if (!attempt_back_merge(q, req))
1152 elv_merged_request(q, req, el_ret);
1153 goto out;
1155 case ELEVATOR_FRONT_MERGE:
1156 BUG_ON(!rq_mergeable(req));
1158 if (!ll_front_merge_fn(q, req, bio))
1159 break;
1161 blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
1163 bio->bi_next = req->bio;
1164 req->bio = bio;
1167 * may not be valid. if the low level driver said
1168 * it didn't need a bounce buffer then it better
1169 * not touch req->buffer either...
1171 req->buffer = bio_data(bio);
1172 req->current_nr_sectors = bio_cur_sectors(bio);
1173 req->hard_cur_sectors = req->current_nr_sectors;
1174 req->sector = req->hard_sector = bio->bi_sector;
1175 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
1176 req->ioprio = ioprio_best(req->ioprio, prio);
1177 drive_stat_acct(req, 0);
1178 if (!attempt_front_merge(q, req))
1179 elv_merged_request(q, req, el_ret);
1180 goto out;
1182 /* ELV_NO_MERGE: elevator says don't/can't merge. */
1183 default:
1187 get_rq:
1189 * This sync check and mask will be re-done in init_request_from_bio(),
1190 * but we need to set it earlier to expose the sync flag to the
1191 * rq allocator and io schedulers.
1193 rw_flags = bio_data_dir(bio);
1194 if (sync)
1195 rw_flags |= REQ_RW_SYNC;
1198 * Grab a free request. This is might sleep but can not fail.
1199 * Returns with the queue unlocked.
1201 req = get_request_wait(q, rw_flags, bio);
1204 * After dropping the lock and possibly sleeping here, our request
1205 * may now be mergeable after it had proven unmergeable (above).
1206 * We don't worry about that case for efficiency. It won't happen
1207 * often, and the elevators are able to handle it.
1209 init_request_from_bio(req, bio);
1211 spin_lock_irq(q->queue_lock);
1212 if (elv_queue_empty(q))
1213 blk_plug_device(q);
1214 add_request(q, req);
1215 out:
1216 if (sync)
1217 __generic_unplug_device(q);
1219 spin_unlock_irq(q->queue_lock);
1220 return 0;
1222 end_io:
1223 bio_endio(bio, err);
1224 return 0;
1228 * If bio->bi_dev is a partition, remap the location
1230 static inline void blk_partition_remap(struct bio *bio)
1232 struct block_device *bdev = bio->bi_bdev;
1234 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
1235 struct hd_struct *p = bdev->bd_part;
1237 bio->bi_sector += p->start_sect;
1238 bio->bi_bdev = bdev->bd_contains;
1240 blk_add_trace_remap(bdev_get_queue(bio->bi_bdev), bio,
1241 bdev->bd_dev, bio->bi_sector,
1242 bio->bi_sector - p->start_sect);
1246 static void handle_bad_sector(struct bio *bio)
1248 char b[BDEVNAME_SIZE];
1250 printk(KERN_INFO "attempt to access beyond end of device\n");
1251 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1252 bdevname(bio->bi_bdev, b),
1253 bio->bi_rw,
1254 (unsigned long long)bio->bi_sector + bio_sectors(bio),
1255 (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
1257 set_bit(BIO_EOF, &bio->bi_flags);
1260 #ifdef CONFIG_FAIL_MAKE_REQUEST
1262 static DECLARE_FAULT_ATTR(fail_make_request);
1264 static int __init setup_fail_make_request(char *str)
1266 return setup_fault_attr(&fail_make_request, str);
1268 __setup("fail_make_request=", setup_fail_make_request);
1270 static int should_fail_request(struct bio *bio)
1272 if ((bio->bi_bdev->bd_disk->flags & GENHD_FL_FAIL) ||
1273 (bio->bi_bdev->bd_part && bio->bi_bdev->bd_part->make_it_fail))
1274 return should_fail(&fail_make_request, bio->bi_size);
1276 return 0;
1279 static int __init fail_make_request_debugfs(void)
1281 return init_fault_attr_dentries(&fail_make_request,
1282 "fail_make_request");
1285 late_initcall(fail_make_request_debugfs);
1287 #else /* CONFIG_FAIL_MAKE_REQUEST */
1289 static inline int should_fail_request(struct bio *bio)
1291 return 0;
1294 #endif /* CONFIG_FAIL_MAKE_REQUEST */
1297 * Check whether this bio extends beyond the end of the device.
1299 static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1301 sector_t maxsector;
1303 if (!nr_sectors)
1304 return 0;
1306 /* Test device or partition size, when known. */
1307 maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
1308 if (maxsector) {
1309 sector_t sector = bio->bi_sector;
1311 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1313 * This may well happen - the kernel calls bread()
1314 * without checking the size of the device, e.g., when
1315 * mounting a device.
1317 handle_bad_sector(bio);
1318 return 1;
1322 return 0;
1326 * generic_make_request: hand a buffer to its device driver for I/O
1327 * @bio: The bio describing the location in memory and on the device.
1329 * generic_make_request() is used to make I/O requests of block
1330 * devices. It is passed a &struct bio, which describes the I/O that needs
1331 * to be done.
1333 * generic_make_request() does not return any status. The
1334 * success/failure status of the request, along with notification of
1335 * completion, is delivered asynchronously through the bio->bi_end_io
1336 * function described (one day) else where.
1338 * The caller of generic_make_request must make sure that bi_io_vec
1339 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1340 * set to describe the device address, and the
1341 * bi_end_io and optionally bi_private are set to describe how
1342 * completion notification should be signaled.
1344 * generic_make_request and the drivers it calls may use bi_next if this
1345 * bio happens to be merged with someone else, and may change bi_dev and
1346 * bi_sector for remaps as it sees fit. So the values of these fields
1347 * should NOT be depended on after the call to generic_make_request.
1349 static inline void __generic_make_request(struct bio *bio)
1351 struct request_queue *q;
1352 sector_t old_sector;
1353 int ret, nr_sectors = bio_sectors(bio);
1354 dev_t old_dev;
1355 int err = -EIO;
1357 might_sleep();
1359 if (bio_check_eod(bio, nr_sectors))
1360 goto end_io;
1363 * Resolve the mapping until finished. (drivers are
1364 * still free to implement/resolve their own stacking
1365 * by explicitly returning 0)
1367 * NOTE: we don't repeat the blk_size check for each new device.
1368 * Stacking drivers are expected to know what they are doing.
1370 old_sector = -1;
1371 old_dev = 0;
1372 do {
1373 char b[BDEVNAME_SIZE];
1375 q = bdev_get_queue(bio->bi_bdev);
1376 if (!q) {
1377 printk(KERN_ERR
1378 "generic_make_request: Trying to access "
1379 "nonexistent block-device %s (%Lu)\n",
1380 bdevname(bio->bi_bdev, b),
1381 (long long) bio->bi_sector);
1382 end_io:
1383 bio_endio(bio, err);
1384 break;
1387 if (unlikely(nr_sectors > q->max_hw_sectors)) {
1388 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
1389 bdevname(bio->bi_bdev, b),
1390 bio_sectors(bio),
1391 q->max_hw_sectors);
1392 goto end_io;
1395 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
1396 goto end_io;
1398 if (should_fail_request(bio))
1399 goto end_io;
1402 * If this device has partitions, remap block n
1403 * of partition p to block n+start(p) of the disk.
1405 blk_partition_remap(bio);
1407 if (old_sector != -1)
1408 blk_add_trace_remap(q, bio, old_dev, bio->bi_sector,
1409 old_sector);
1411 blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
1413 old_sector = bio->bi_sector;
1414 old_dev = bio->bi_bdev->bd_dev;
1416 if (bio_check_eod(bio, nr_sectors))
1417 goto end_io;
1418 if (bio_empty_barrier(bio) && !q->prepare_flush_fn) {
1419 err = -EOPNOTSUPP;
1420 goto end_io;
1423 ret = q->make_request_fn(q, bio);
1424 } while (ret);
1428 * We only want one ->make_request_fn to be active at a time,
1429 * else stack usage with stacked devices could be a problem.
1430 * So use current->bio_{list,tail} to keep a list of requests
1431 * submited by a make_request_fn function.
1432 * current->bio_tail is also used as a flag to say if
1433 * generic_make_request is currently active in this task or not.
1434 * If it is NULL, then no make_request is active. If it is non-NULL,
1435 * then a make_request is active, and new requests should be added
1436 * at the tail
1438 void generic_make_request(struct bio *bio)
1440 if (current->bio_tail) {
1441 /* make_request is active */
1442 *(current->bio_tail) = bio;
1443 bio->bi_next = NULL;
1444 current->bio_tail = &bio->bi_next;
1445 return;
1447 /* following loop may be a bit non-obvious, and so deserves some
1448 * explanation.
1449 * Before entering the loop, bio->bi_next is NULL (as all callers
1450 * ensure that) so we have a list with a single bio.
1451 * We pretend that we have just taken it off a longer list, so
1452 * we assign bio_list to the next (which is NULL) and bio_tail
1453 * to &bio_list, thus initialising the bio_list of new bios to be
1454 * added. __generic_make_request may indeed add some more bios
1455 * through a recursive call to generic_make_request. If it
1456 * did, we find a non-NULL value in bio_list and re-enter the loop
1457 * from the top. In this case we really did just take the bio
1458 * of the top of the list (no pretending) and so fixup bio_list and
1459 * bio_tail or bi_next, and call into __generic_make_request again.
1461 * The loop was structured like this to make only one call to
1462 * __generic_make_request (which is important as it is large and
1463 * inlined) and to keep the structure simple.
1465 BUG_ON(bio->bi_next);
1466 do {
1467 current->bio_list = bio->bi_next;
1468 if (bio->bi_next == NULL)
1469 current->bio_tail = &current->bio_list;
1470 else
1471 bio->bi_next = NULL;
1472 __generic_make_request(bio);
1473 bio = current->bio_list;
1474 } while (bio);
1475 current->bio_tail = NULL; /* deactivate */
1477 EXPORT_SYMBOL(generic_make_request);
1480 * submit_bio: submit a bio to the block device layer for I/O
1481 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1482 * @bio: The &struct bio which describes the I/O
1484 * submit_bio() is very similar in purpose to generic_make_request(), and
1485 * uses that function to do most of the work. Both are fairly rough
1486 * interfaces, @bio must be presetup and ready for I/O.
1489 void submit_bio(int rw, struct bio *bio)
1491 int count = bio_sectors(bio);
1493 bio->bi_rw |= rw;
1496 * If it's a regular read/write or a barrier with data attached,
1497 * go through the normal accounting stuff before submission.
1499 if (!bio_empty_barrier(bio)) {
1501 BIO_BUG_ON(!bio->bi_size);
1502 BIO_BUG_ON(!bio->bi_io_vec);
1504 if (rw & WRITE) {
1505 count_vm_events(PGPGOUT, count);
1506 } else {
1507 task_io_account_read(bio->bi_size);
1508 count_vm_events(PGPGIN, count);
1511 if (unlikely(block_dump)) {
1512 char b[BDEVNAME_SIZE];
1513 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n",
1514 current->comm, task_pid_nr(current),
1515 (rw & WRITE) ? "WRITE" : "READ",
1516 (unsigned long long)bio->bi_sector,
1517 bdevname(bio->bi_bdev, b));
1521 generic_make_request(bio);
1523 EXPORT_SYMBOL(submit_bio);
1526 * __end_that_request_first - end I/O on a request
1527 * @req: the request being processed
1528 * @error: 0 for success, < 0 for error
1529 * @nr_bytes: number of bytes to complete
1531 * Description:
1532 * Ends I/O on a number of bytes attached to @req, and sets it up
1533 * for the next range of segments (if any) in the cluster.
1535 * Return:
1536 * 0 - we are done with this request, call end_that_request_last()
1537 * 1 - still buffers pending for this request
1539 static int __end_that_request_first(struct request *req, int error,
1540 int nr_bytes)
1542 int total_bytes, bio_nbytes, next_idx = 0;
1543 struct bio *bio;
1545 blk_add_trace_rq(req->q, req, BLK_TA_COMPLETE);
1548 * for a REQ_BLOCK_PC request, we want to carry any eventual
1549 * sense key with us all the way through
1551 if (!blk_pc_request(req))
1552 req->errors = 0;
1554 if (error && (blk_fs_request(req) && !(req->cmd_flags & REQ_QUIET))) {
1555 printk(KERN_ERR "end_request: I/O error, dev %s, sector %llu\n",
1556 req->rq_disk ? req->rq_disk->disk_name : "?",
1557 (unsigned long long)req->sector);
1560 if (blk_fs_request(req) && req->rq_disk) {
1561 const int rw = rq_data_dir(req);
1563 all_stat_add(req->rq_disk, sectors[rw],
1564 nr_bytes >> 9, req->sector);
1567 total_bytes = bio_nbytes = 0;
1568 while ((bio = req->bio) != NULL) {
1569 int nbytes;
1572 * For an empty barrier request, the low level driver must
1573 * store a potential error location in ->sector. We pass
1574 * that back up in ->bi_sector.
1576 if (blk_empty_barrier(req))
1577 bio->bi_sector = req->sector;
1579 if (nr_bytes >= bio->bi_size) {
1580 req->bio = bio->bi_next;
1581 nbytes = bio->bi_size;
1582 req_bio_endio(req, bio, nbytes, error);
1583 next_idx = 0;
1584 bio_nbytes = 0;
1585 } else {
1586 int idx = bio->bi_idx + next_idx;
1588 if (unlikely(bio->bi_idx >= bio->bi_vcnt)) {
1589 blk_dump_rq_flags(req, "__end_that");
1590 printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
1591 __FUNCTION__, bio->bi_idx,
1592 bio->bi_vcnt);
1593 break;
1596 nbytes = bio_iovec_idx(bio, idx)->bv_len;
1597 BIO_BUG_ON(nbytes > bio->bi_size);
1600 * not a complete bvec done
1602 if (unlikely(nbytes > nr_bytes)) {
1603 bio_nbytes += nr_bytes;
1604 total_bytes += nr_bytes;
1605 break;
1609 * advance to the next vector
1611 next_idx++;
1612 bio_nbytes += nbytes;
1615 total_bytes += nbytes;
1616 nr_bytes -= nbytes;
1618 bio = req->bio;
1619 if (bio) {
1621 * end more in this run, or just return 'not-done'
1623 if (unlikely(nr_bytes <= 0))
1624 break;
1629 * completely done
1631 if (!req->bio)
1632 return 0;
1635 * if the request wasn't completed, update state
1637 if (bio_nbytes) {
1638 req_bio_endio(req, bio, bio_nbytes, error);
1639 bio->bi_idx += next_idx;
1640 bio_iovec(bio)->bv_offset += nr_bytes;
1641 bio_iovec(bio)->bv_len -= nr_bytes;
1644 blk_recalc_rq_sectors(req, total_bytes >> 9);
1645 blk_recalc_rq_segments(req);
1646 return 1;
1650 * splice the completion data to a local structure and hand off to
1651 * process_completion_queue() to complete the requests
1653 static void blk_done_softirq(struct softirq_action *h)
1655 struct list_head *cpu_list, local_list;
1657 local_irq_disable();
1658 cpu_list = &__get_cpu_var(blk_cpu_done);
1659 list_replace_init(cpu_list, &local_list);
1660 local_irq_enable();
1662 while (!list_empty(&local_list)) {
1663 struct request *rq;
1665 rq = list_entry(local_list.next, struct request, donelist);
1666 list_del_init(&rq->donelist);
1667 rq->q->softirq_done_fn(rq);
1671 static int __cpuinit blk_cpu_notify(struct notifier_block *self,
1672 unsigned long action, void *hcpu)
1675 * If a CPU goes away, splice its entries to the current CPU
1676 * and trigger a run of the softirq
1678 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
1679 int cpu = (unsigned long) hcpu;
1681 local_irq_disable();
1682 list_splice_init(&per_cpu(blk_cpu_done, cpu),
1683 &__get_cpu_var(blk_cpu_done));
1684 raise_softirq_irqoff(BLOCK_SOFTIRQ);
1685 local_irq_enable();
1688 return NOTIFY_OK;
1692 static struct notifier_block blk_cpu_notifier __cpuinitdata = {
1693 .notifier_call = blk_cpu_notify,
1697 * blk_complete_request - end I/O on a request
1698 * @req: the request being processed
1700 * Description:
1701 * Ends all I/O on a request. It does not handle partial completions,
1702 * unless the driver actually implements this in its completion callback
1703 * through requeueing. The actual completion happens out-of-order,
1704 * through a softirq handler. The user must have registered a completion
1705 * callback through blk_queue_softirq_done().
1708 void blk_complete_request(struct request *req)
1710 struct list_head *cpu_list;
1711 unsigned long flags;
1713 BUG_ON(!req->q->softirq_done_fn);
1715 local_irq_save(flags);
1717 cpu_list = &__get_cpu_var(blk_cpu_done);
1718 list_add_tail(&req->donelist, cpu_list);
1719 raise_softirq_irqoff(BLOCK_SOFTIRQ);
1721 local_irq_restore(flags);
1723 EXPORT_SYMBOL(blk_complete_request);
1726 * queue lock must be held
1728 static void end_that_request_last(struct request *req, int error)
1730 struct gendisk *disk = req->rq_disk;
1732 if (blk_rq_tagged(req))
1733 blk_queue_end_tag(req->q, req);
1735 if (blk_queued_rq(req))
1736 blkdev_dequeue_request(req);
1738 if (unlikely(laptop_mode) && blk_fs_request(req))
1739 laptop_io_completion();
1742 * Account IO completion. bar_rq isn't accounted as a normal
1743 * IO on queueing nor completion. Accounting the containing
1744 * request is enough.
1746 if (disk && blk_fs_request(req) && req != &req->q->bar_rq) {
1747 unsigned long duration = jiffies - req->start_time;
1748 const int rw = rq_data_dir(req);
1749 struct hd_struct *part = get_part(disk, req->sector);
1751 __all_stat_inc(disk, ios[rw], req->sector);
1752 __all_stat_add(disk, ticks[rw], duration, req->sector);
1753 disk_round_stats(disk);
1754 disk->in_flight--;
1755 if (part) {
1756 part_round_stats(part);
1757 part->in_flight--;
1761 if (req->end_io)
1762 req->end_io(req, error);
1763 else {
1764 if (blk_bidi_rq(req))
1765 __blk_put_request(req->next_rq->q, req->next_rq);
1767 __blk_put_request(req->q, req);
1771 static inline void __end_request(struct request *rq, int uptodate,
1772 unsigned int nr_bytes)
1774 int error = 0;
1776 if (uptodate <= 0)
1777 error = uptodate ? uptodate : -EIO;
1779 __blk_end_request(rq, error, nr_bytes);
1783 * blk_rq_bytes - Returns bytes left to complete in the entire request
1784 <<<<<<< HEAD:block/blk-core.c
1785 =======
1786 * @rq: the request being processed
1787 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:block/blk-core.c
1789 unsigned int blk_rq_bytes(struct request *rq)
1791 if (blk_fs_request(rq))
1792 return rq->hard_nr_sectors << 9;
1794 return rq->data_len;
1796 EXPORT_SYMBOL_GPL(blk_rq_bytes);
1799 * blk_rq_cur_bytes - Returns bytes left to complete in the current segment
1800 <<<<<<< HEAD:block/blk-core.c
1801 =======
1802 * @rq: the request being processed
1803 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:block/blk-core.c
1805 unsigned int blk_rq_cur_bytes(struct request *rq)
1807 if (blk_fs_request(rq))
1808 return rq->current_nr_sectors << 9;
1810 if (rq->bio)
1811 return rq->bio->bi_size;
1813 return rq->data_len;
1815 EXPORT_SYMBOL_GPL(blk_rq_cur_bytes);
1818 * end_queued_request - end all I/O on a queued request
1819 * @rq: the request being processed
1820 * @uptodate: error value or 0/1 uptodate flag
1822 * Description:
1823 * Ends all I/O on a request, and removes it from the block layer queues.
1824 * Not suitable for normal IO completion, unless the driver still has
1825 * the request attached to the block layer.
1828 void end_queued_request(struct request *rq, int uptodate)
1830 __end_request(rq, uptodate, blk_rq_bytes(rq));
1832 EXPORT_SYMBOL(end_queued_request);
1835 * end_dequeued_request - end all I/O on a dequeued request
1836 * @rq: the request being processed
1837 * @uptodate: error value or 0/1 uptodate flag
1839 * Description:
1840 * Ends all I/O on a request. The request must already have been
1841 * dequeued using blkdev_dequeue_request(), as is normally the case
1842 * for most drivers.
1845 void end_dequeued_request(struct request *rq, int uptodate)
1847 __end_request(rq, uptodate, blk_rq_bytes(rq));
1849 EXPORT_SYMBOL(end_dequeued_request);
1853 * end_request - end I/O on the current segment of the request
1854 * @req: the request being processed
1855 * @uptodate: error value or 0/1 uptodate flag
1857 * Description:
1858 * Ends I/O on the current segment of a request. If that is the only
1859 * remaining segment, the request is also completed and freed.
1861 * This is a remnant of how older block drivers handled IO completions.
1862 * Modern drivers typically end IO on the full request in one go, unless
1863 * they have a residual value to account for. For that case this function
1864 * isn't really useful, unless the residual just happens to be the
1865 * full current segment. In other words, don't use this function in new
1866 * code. Either use end_request_completely(), or the
1867 * end_that_request_chunk() (along with end_that_request_last()) for
1868 * partial completions.
1871 void end_request(struct request *req, int uptodate)
1873 __end_request(req, uptodate, req->hard_cur_sectors << 9);
1875 EXPORT_SYMBOL(end_request);
1878 * blk_end_io - Generic end_io function to complete a request.
1879 * @rq: the request being processed
1880 * @error: 0 for success, < 0 for error
1881 * @nr_bytes: number of bytes to complete @rq
1882 * @bidi_bytes: number of bytes to complete @rq->next_rq
1883 * @drv_callback: function called between completion of bios in the request
1884 * and completion of the request.
1885 * If the callback returns non 0, this helper returns without
1886 * completion of the request.
1888 * Description:
1889 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
1890 * If @rq has leftover, sets it up for the next range of segments.
1892 * Return:
1893 * 0 - we are done with this request
1894 * 1 - this request is not freed yet, it still has pending buffers.
1896 static int blk_end_io(struct request *rq, int error, unsigned int nr_bytes,
1897 unsigned int bidi_bytes,
1898 int (drv_callback)(struct request *))
1900 struct request_queue *q = rq->q;
1901 unsigned long flags = 0UL;
1903 if (blk_fs_request(rq) || blk_pc_request(rq)) {
1904 if (__end_that_request_first(rq, error, nr_bytes))
1905 return 1;
1907 /* Bidi request must be completed as a whole */
1908 if (blk_bidi_rq(rq) &&
1909 __end_that_request_first(rq->next_rq, error, bidi_bytes))
1910 return 1;
1913 /* Special feature for tricky drivers */
1914 if (drv_callback && drv_callback(rq))
1915 return 1;
1917 add_disk_randomness(rq->rq_disk);
1919 spin_lock_irqsave(q->queue_lock, flags);
1920 end_that_request_last(rq, error);
1921 spin_unlock_irqrestore(q->queue_lock, flags);
1923 return 0;
1927 * blk_end_request - Helper function for drivers to complete the request.
1928 * @rq: the request being processed
1929 * @error: 0 for success, < 0 for error
1930 * @nr_bytes: number of bytes to complete
1932 * Description:
1933 * Ends I/O on a number of bytes attached to @rq.
1934 * If @rq has leftover, sets it up for the next range of segments.
1936 * Return:
1937 * 0 - we are done with this request
1938 * 1 - still buffers pending for this request
1940 int blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
1942 return blk_end_io(rq, error, nr_bytes, 0, NULL);
1944 EXPORT_SYMBOL_GPL(blk_end_request);
1947 * __blk_end_request - Helper function for drivers to complete the request.
1948 * @rq: the request being processed
1949 * @error: 0 for success, < 0 for error
1950 * @nr_bytes: number of bytes to complete
1952 * Description:
1953 * Must be called with queue lock held unlike blk_end_request().
1955 * Return:
1956 * 0 - we are done with this request
1957 * 1 - still buffers pending for this request
1959 int __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
1961 if (blk_fs_request(rq) || blk_pc_request(rq)) {
1962 if (__end_that_request_first(rq, error, nr_bytes))
1963 return 1;
1966 add_disk_randomness(rq->rq_disk);
1968 end_that_request_last(rq, error);
1970 return 0;
1972 EXPORT_SYMBOL_GPL(__blk_end_request);
1975 * blk_end_bidi_request - Helper function for drivers to complete bidi request.
1976 * @rq: the bidi request being processed
1977 * @error: 0 for success, < 0 for error
1978 * @nr_bytes: number of bytes to complete @rq
1979 * @bidi_bytes: number of bytes to complete @rq->next_rq
1981 * Description:
1982 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
1984 * Return:
1985 * 0 - we are done with this request
1986 * 1 - still buffers pending for this request
1988 int blk_end_bidi_request(struct request *rq, int error, unsigned int nr_bytes,
1989 unsigned int bidi_bytes)
1991 return blk_end_io(rq, error, nr_bytes, bidi_bytes, NULL);
1993 EXPORT_SYMBOL_GPL(blk_end_bidi_request);
1996 * blk_end_request_callback - Special helper function for tricky drivers
1997 * @rq: the request being processed
1998 * @error: 0 for success, < 0 for error
1999 * @nr_bytes: number of bytes to complete
2000 * @drv_callback: function called between completion of bios in the request
2001 * and completion of the request.
2002 * If the callback returns non 0, this helper returns without
2003 * completion of the request.
2005 * Description:
2006 * Ends I/O on a number of bytes attached to @rq.
2007 * If @rq has leftover, sets it up for the next range of segments.
2009 * This special helper function is used only for existing tricky drivers.
2010 * (e.g. cdrom_newpc_intr() of ide-cd)
2011 * This interface will be removed when such drivers are rewritten.
2012 * Don't use this interface in other places anymore.
2014 * Return:
2015 * 0 - we are done with this request
2016 * 1 - this request is not freed yet.
2017 * this request still has pending buffers or
2018 * the driver doesn't want to finish this request yet.
2020 int blk_end_request_callback(struct request *rq, int error,
2021 unsigned int nr_bytes,
2022 int (drv_callback)(struct request *))
2024 return blk_end_io(rq, error, nr_bytes, 0, drv_callback);
2026 EXPORT_SYMBOL_GPL(blk_end_request_callback);
2028 void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2029 struct bio *bio)
2031 /* first two bits are identical in rq->cmd_flags and bio->bi_rw */
2032 rq->cmd_flags |= (bio->bi_rw & 3);
2034 rq->nr_phys_segments = bio_phys_segments(q, bio);
2035 rq->nr_hw_segments = bio_hw_segments(q, bio);
2036 rq->current_nr_sectors = bio_cur_sectors(bio);
2037 rq->hard_cur_sectors = rq->current_nr_sectors;
2038 rq->hard_nr_sectors = rq->nr_sectors = bio_sectors(bio);
2039 rq->buffer = bio_data(bio);
2040 rq->data_len = bio->bi_size;
2042 rq->bio = rq->biotail = bio;
2044 if (bio->bi_bdev)
2045 rq->rq_disk = bio->bi_bdev->bd_disk;
2048 int kblockd_schedule_work(struct work_struct *work)
2050 return queue_work(kblockd_workqueue, work);
2052 EXPORT_SYMBOL(kblockd_schedule_work);
2054 void kblockd_flush_work(struct work_struct *work)
2056 cancel_work_sync(work);
2058 EXPORT_SYMBOL(kblockd_flush_work);
2060 int __init blk_dev_init(void)
2062 int i;
2064 kblockd_workqueue = create_workqueue("kblockd");
2065 if (!kblockd_workqueue)
2066 panic("Failed to create kblockd\n");
2068 request_cachep = kmem_cache_create("blkdev_requests",
2069 sizeof(struct request), 0, SLAB_PANIC, NULL);
2071 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
2072 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
2074 for_each_possible_cpu(i)
2075 INIT_LIST_HEAD(&per_cpu(blk_cpu_done, i));
2077 open_softirq(BLOCK_SOFTIRQ, blk_done_softirq, NULL);
2078 register_hotcpu_notifier(&blk_cpu_notifier);
2080 return 0;