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> - July2000
7 * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
11 * This handles all read/write requests to block devices
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/backing-dev.h>
16 #include <linux/bio.h>
17 #include <linux/blkdev.h>
18 #include <linux/highmem.h>
20 #include <linux/kernel_stat.h>
21 #include <linux/string.h>
22 #include <linux/init.h>
23 #include <linux/completion.h>
24 #include <linux/slab.h>
25 #include <linux/swap.h>
26 #include <linux/writeback.h>
27 #include <linux/task_io_accounting_ops.h>
28 #include <linux/interrupt.h>
29 #include <linux/cpu.h>
30 #include <linux/blktrace_api.h>
31 #include <linux/fault-inject.h>
35 static int __make_request(struct request_queue
*q
, struct bio
*bio
);
38 * For the allocated request tables
40 struct kmem_cache
*request_cachep
;
43 * For queue allocation
45 struct kmem_cache
*blk_requestq_cachep
= NULL
;
48 * Controlling structure to kblockd
50 static struct workqueue_struct
*kblockd_workqueue
;
52 static DEFINE_PER_CPU(struct list_head
, blk_cpu_done
);
54 static void drive_stat_acct(struct request
*rq
, int new_io
)
56 int rw
= rq_data_dir(rq
);
58 if (!blk_fs_request(rq
) || !rq
->rq_disk
)
62 __disk_stat_inc(rq
->rq_disk
, merges
[rw
]);
64 disk_round_stats(rq
->rq_disk
);
65 rq
->rq_disk
->in_flight
++;
69 void blk_queue_congestion_threshold(struct request_queue
*q
)
73 nr
= q
->nr_requests
- (q
->nr_requests
/ 8) + 1;
74 if (nr
> q
->nr_requests
)
76 q
->nr_congestion_on
= nr
;
78 nr
= q
->nr_requests
- (q
->nr_requests
/ 8) - (q
->nr_requests
/ 16) - 1;
81 q
->nr_congestion_off
= nr
;
85 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
88 * Locates the passed device's request queue and returns the address of its
91 * Will return NULL if the request queue cannot be located.
93 struct backing_dev_info
*blk_get_backing_dev_info(struct block_device
*bdev
)
95 struct backing_dev_info
*ret
= NULL
;
96 struct request_queue
*q
= bdev_get_queue(bdev
);
99 ret
= &q
->backing_dev_info
;
102 EXPORT_SYMBOL(blk_get_backing_dev_info
);
104 void rq_init(struct request_queue
*q
, struct request
*rq
)
106 INIT_LIST_HEAD(&rq
->queuelist
);
107 INIT_LIST_HEAD(&rq
->donelist
);
110 rq
->bio
= rq
->biotail
= NULL
;
111 INIT_HLIST_NODE(&rq
->hash
);
112 RB_CLEAR_NODE(&rq
->rb_node
);
120 rq
->nr_phys_segments
= 0;
123 rq
->end_io_data
= NULL
;
124 rq
->completion_data
= NULL
;
128 static void req_bio_endio(struct request
*rq
, struct bio
*bio
,
129 unsigned int nbytes
, int error
)
131 struct request_queue
*q
= rq
->q
;
133 if (&q
->bar_rq
!= rq
) {
135 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
136 else if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
139 if (unlikely(nbytes
> bio
->bi_size
)) {
140 printk("%s: want %u bytes done, only %u left\n",
141 __FUNCTION__
, nbytes
, bio
->bi_size
);
142 nbytes
= bio
->bi_size
;
145 bio
->bi_size
-= nbytes
;
146 bio
->bi_sector
+= (nbytes
>> 9);
147 if (bio
->bi_size
== 0)
148 bio_endio(bio
, error
);
152 * Okay, this is the barrier request in progress, just
155 if (error
&& !q
->orderr
)
160 void blk_dump_rq_flags(struct request
*rq
, char *msg
)
164 printk("%s: dev %s: type=%x, flags=%x\n", msg
,
165 rq
->rq_disk
? rq
->rq_disk
->disk_name
: "?", rq
->cmd_type
,
168 printk("\nsector %llu, nr/cnr %lu/%u\n", (unsigned long long)rq
->sector
,
170 rq
->current_nr_sectors
);
171 printk("bio %p, biotail %p, buffer %p, data %p, len %u\n", rq
->bio
, rq
->biotail
, rq
->buffer
, rq
->data
, rq
->data_len
);
173 if (blk_pc_request(rq
)) {
175 for (bit
= 0; bit
< sizeof(rq
->cmd
); bit
++)
176 printk("%02x ", rq
->cmd
[bit
]);
181 EXPORT_SYMBOL(blk_dump_rq_flags
);
184 * "plug" the device if there are no outstanding requests: this will
185 * force the transfer to start only after we have put all the requests
188 * This is called with interrupts off and no requests on the queue and
189 * with the queue lock held.
191 void blk_plug_device(struct request_queue
*q
)
193 WARN_ON(!irqs_disabled());
196 * don't plug a stopped queue, it must be paired with blk_start_queue()
197 * which will restart the queueing
199 if (blk_queue_stopped(q
))
202 if (!test_and_set_bit(QUEUE_FLAG_PLUGGED
, &q
->queue_flags
)) {
203 mod_timer(&q
->unplug_timer
, jiffies
+ q
->unplug_delay
);
204 blk_add_trace_generic(q
, NULL
, 0, BLK_TA_PLUG
);
208 EXPORT_SYMBOL(blk_plug_device
);
211 * remove the queue from the plugged list, if present. called with
212 * queue lock held and interrupts disabled.
214 int blk_remove_plug(struct request_queue
*q
)
216 WARN_ON(!irqs_disabled());
218 if (!test_and_clear_bit(QUEUE_FLAG_PLUGGED
, &q
->queue_flags
))
221 del_timer(&q
->unplug_timer
);
225 EXPORT_SYMBOL(blk_remove_plug
);
228 * remove the plug and let it rip..
230 void __generic_unplug_device(struct request_queue
*q
)
232 if (unlikely(blk_queue_stopped(q
)))
235 if (!blk_remove_plug(q
))
240 EXPORT_SYMBOL(__generic_unplug_device
);
243 * generic_unplug_device - fire a request queue
244 * @q: The &struct request_queue in question
247 * Linux uses plugging to build bigger requests queues before letting
248 * the device have at them. If a queue is plugged, the I/O scheduler
249 * is still adding and merging requests on the queue. Once the queue
250 * gets unplugged, the request_fn defined for the queue is invoked and
253 void generic_unplug_device(struct request_queue
*q
)
255 spin_lock_irq(q
->queue_lock
);
256 __generic_unplug_device(q
);
257 spin_unlock_irq(q
->queue_lock
);
259 EXPORT_SYMBOL(generic_unplug_device
);
261 static void blk_backing_dev_unplug(struct backing_dev_info
*bdi
,
264 struct request_queue
*q
= bdi
->unplug_io_data
;
269 void blk_unplug_work(struct work_struct
*work
)
271 struct request_queue
*q
=
272 container_of(work
, struct request_queue
, unplug_work
);
274 blk_add_trace_pdu_int(q
, BLK_TA_UNPLUG_IO
, NULL
,
275 q
->rq
.count
[READ
] + q
->rq
.count
[WRITE
]);
280 void blk_unplug_timeout(unsigned long data
)
282 struct request_queue
*q
= (struct request_queue
*)data
;
284 blk_add_trace_pdu_int(q
, BLK_TA_UNPLUG_TIMER
, NULL
,
285 q
->rq
.count
[READ
] + q
->rq
.count
[WRITE
]);
287 kblockd_schedule_work(&q
->unplug_work
);
290 void blk_unplug(struct request_queue
*q
)
293 * devices don't necessarily have an ->unplug_fn defined
296 blk_add_trace_pdu_int(q
, BLK_TA_UNPLUG_IO
, NULL
,
297 q
->rq
.count
[READ
] + q
->rq
.count
[WRITE
]);
302 EXPORT_SYMBOL(blk_unplug
);
305 * blk_start_queue - restart a previously stopped queue
306 * @q: The &struct request_queue in question
309 * blk_start_queue() will clear the stop flag on the queue, and call
310 * the request_fn for the queue if it was in a stopped state when
311 * entered. Also see blk_stop_queue(). Queue lock must be held.
313 void blk_start_queue(struct request_queue
*q
)
315 WARN_ON(!irqs_disabled());
317 clear_bit(QUEUE_FLAG_STOPPED
, &q
->queue_flags
);
320 * one level of recursion is ok and is much faster than kicking
321 * the unplug handling
323 if (!test_and_set_bit(QUEUE_FLAG_REENTER
, &q
->queue_flags
)) {
325 clear_bit(QUEUE_FLAG_REENTER
, &q
->queue_flags
);
328 kblockd_schedule_work(&q
->unplug_work
);
332 EXPORT_SYMBOL(blk_start_queue
);
335 * blk_stop_queue - stop a queue
336 * @q: The &struct request_queue in question
339 * The Linux block layer assumes that a block driver will consume all
340 * entries on the request queue when the request_fn strategy is called.
341 * Often this will not happen, because of hardware limitations (queue
342 * depth settings). If a device driver gets a 'queue full' response,
343 * or if it simply chooses not to queue more I/O at one point, it can
344 * call this function to prevent the request_fn from being called until
345 * the driver has signalled it's ready to go again. This happens by calling
346 * blk_start_queue() to restart queue operations. Queue lock must be held.
348 void blk_stop_queue(struct request_queue
*q
)
351 set_bit(QUEUE_FLAG_STOPPED
, &q
->queue_flags
);
353 EXPORT_SYMBOL(blk_stop_queue
);
356 * blk_sync_queue - cancel any pending callbacks on a queue
360 * The block layer may perform asynchronous callback activity
361 * on a queue, such as calling the unplug function after a timeout.
362 * A block device may call blk_sync_queue to ensure that any
363 * such activity is cancelled, thus allowing it to release resources
364 * that the callbacks might use. The caller must already have made sure
365 * that its ->make_request_fn will not re-add plugging prior to calling
369 void blk_sync_queue(struct request_queue
*q
)
371 del_timer_sync(&q
->unplug_timer
);
372 kblockd_flush_work(&q
->unplug_work
);
374 EXPORT_SYMBOL(blk_sync_queue
);
377 * blk_run_queue - run a single device queue
378 * @q: The queue to run
380 void blk_run_queue(struct request_queue
*q
)
384 spin_lock_irqsave(q
->queue_lock
, flags
);
388 * Only recurse once to avoid overrunning the stack, let the unplug
389 * handling reinvoke the handler shortly if we already got there.
391 if (!elv_queue_empty(q
)) {
392 if (!test_and_set_bit(QUEUE_FLAG_REENTER
, &q
->queue_flags
)) {
394 clear_bit(QUEUE_FLAG_REENTER
, &q
->queue_flags
);
397 kblockd_schedule_work(&q
->unplug_work
);
401 spin_unlock_irqrestore(q
->queue_lock
, flags
);
403 EXPORT_SYMBOL(blk_run_queue
);
405 void blk_put_queue(struct request_queue
*q
)
407 kobject_put(&q
->kobj
);
409 EXPORT_SYMBOL(blk_put_queue
);
411 void blk_cleanup_queue(struct request_queue
* q
)
413 mutex_lock(&q
->sysfs_lock
);
414 set_bit(QUEUE_FLAG_DEAD
, &q
->queue_flags
);
415 mutex_unlock(&q
->sysfs_lock
);
418 elevator_exit(q
->elevator
);
423 EXPORT_SYMBOL(blk_cleanup_queue
);
425 static int blk_init_free_list(struct request_queue
*q
)
427 struct request_list
*rl
= &q
->rq
;
429 rl
->count
[READ
] = rl
->count
[WRITE
] = 0;
430 rl
->starved
[READ
] = rl
->starved
[WRITE
] = 0;
432 init_waitqueue_head(&rl
->wait
[READ
]);
433 init_waitqueue_head(&rl
->wait
[WRITE
]);
435 rl
->rq_pool
= mempool_create_node(BLKDEV_MIN_RQ
, mempool_alloc_slab
,
436 mempool_free_slab
, request_cachep
, q
->node
);
444 struct request_queue
*blk_alloc_queue(gfp_t gfp_mask
)
446 return blk_alloc_queue_node(gfp_mask
, -1);
448 EXPORT_SYMBOL(blk_alloc_queue
);
450 struct request_queue
*blk_alloc_queue_node(gfp_t gfp_mask
, int node_id
)
452 struct request_queue
*q
;
455 q
= kmem_cache_alloc_node(blk_requestq_cachep
,
456 gfp_mask
| __GFP_ZERO
, node_id
);
460 q
->backing_dev_info
.unplug_io_fn
= blk_backing_dev_unplug
;
461 q
->backing_dev_info
.unplug_io_data
= q
;
462 err
= bdi_init(&q
->backing_dev_info
);
464 kmem_cache_free(blk_requestq_cachep
, q
);
468 init_timer(&q
->unplug_timer
);
470 kobject_init(&q
->kobj
, &blk_queue_ktype
);
472 mutex_init(&q
->sysfs_lock
);
476 EXPORT_SYMBOL(blk_alloc_queue_node
);
479 * blk_init_queue - prepare a request queue for use with a block device
480 * @rfn: The function to be called to process requests that have been
481 * placed on the queue.
482 * @lock: Request queue spin lock
485 * If a block device wishes to use the standard request handling procedures,
486 * which sorts requests and coalesces adjacent requests, then it must
487 * call blk_init_queue(). The function @rfn will be called when there
488 * are requests on the queue that need to be processed. If the device
489 * supports plugging, then @rfn may not be called immediately when requests
490 * are available on the queue, but may be called at some time later instead.
491 * Plugged queues are generally unplugged when a buffer belonging to one
492 * of the requests on the queue is needed, or due to memory pressure.
494 * @rfn is not required, or even expected, to remove all requests off the
495 * queue, but only as many as it can handle at a time. If it does leave
496 * requests on the queue, it is responsible for arranging that the requests
497 * get dealt with eventually.
499 * The queue spin lock must be held while manipulating the requests on the
500 * request queue; this lock will be taken also from interrupt context, so irq
501 * disabling is needed for it.
503 * Function returns a pointer to the initialized request queue, or NULL if
507 * blk_init_queue() must be paired with a blk_cleanup_queue() call
508 * when the block device is deactivated (such as at module unload).
511 struct request_queue
*blk_init_queue(request_fn_proc
*rfn
, spinlock_t
*lock
)
513 return blk_init_queue_node(rfn
, lock
, -1);
515 EXPORT_SYMBOL(blk_init_queue
);
517 struct request_queue
*
518 blk_init_queue_node(request_fn_proc
*rfn
, spinlock_t
*lock
, int node_id
)
520 struct request_queue
*q
= blk_alloc_queue_node(GFP_KERNEL
, node_id
);
526 if (blk_init_free_list(q
)) {
527 kmem_cache_free(blk_requestq_cachep
, q
);
532 * if caller didn't supply a lock, they get per-queue locking with
536 spin_lock_init(&q
->__queue_lock
);
537 lock
= &q
->__queue_lock
;
541 q
->prep_rq_fn
= NULL
;
542 q
->unplug_fn
= generic_unplug_device
;
543 q
->queue_flags
= (1 << QUEUE_FLAG_CLUSTER
);
544 q
->queue_lock
= lock
;
546 blk_queue_segment_boundary(q
, 0xffffffff);
548 blk_queue_make_request(q
, __make_request
);
549 blk_queue_max_segment_size(q
, MAX_SEGMENT_SIZE
);
551 blk_queue_max_hw_segments(q
, MAX_HW_SEGMENTS
);
552 blk_queue_max_phys_segments(q
, MAX_PHYS_SEGMENTS
);
554 q
->sg_reserved_size
= INT_MAX
;
559 if (!elevator_init(q
, NULL
)) {
560 blk_queue_congestion_threshold(q
);
567 EXPORT_SYMBOL(blk_init_queue_node
);
569 int blk_get_queue(struct request_queue
*q
)
571 if (likely(!test_bit(QUEUE_FLAG_DEAD
, &q
->queue_flags
))) {
572 kobject_get(&q
->kobj
);
579 EXPORT_SYMBOL(blk_get_queue
);
581 static inline void blk_free_request(struct request_queue
*q
, struct request
*rq
)
583 if (rq
->cmd_flags
& REQ_ELVPRIV
)
584 elv_put_request(q
, rq
);
585 mempool_free(rq
, q
->rq
.rq_pool
);
588 static struct request
*
589 blk_alloc_request(struct request_queue
*q
, int rw
, int priv
, gfp_t gfp_mask
)
591 struct request
*rq
= mempool_alloc(q
->rq
.rq_pool
, gfp_mask
);
597 * first three bits are identical in rq->cmd_flags and bio->bi_rw,
598 * see bio.h and blkdev.h
600 rq
->cmd_flags
= rw
| REQ_ALLOCED
;
603 if (unlikely(elv_set_request(q
, rq
, gfp_mask
))) {
604 mempool_free(rq
, q
->rq
.rq_pool
);
607 rq
->cmd_flags
|= REQ_ELVPRIV
;
614 * ioc_batching returns true if the ioc is a valid batching request and
615 * should be given priority access to a request.
617 static inline int ioc_batching(struct request_queue
*q
, struct io_context
*ioc
)
623 * Make sure the process is able to allocate at least 1 request
624 * even if the batch times out, otherwise we could theoretically
627 return ioc
->nr_batch_requests
== q
->nr_batching
||
628 (ioc
->nr_batch_requests
> 0
629 && time_before(jiffies
, ioc
->last_waited
+ BLK_BATCH_TIME
));
633 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
634 * will cause the process to be a "batcher" on all queues in the system. This
635 * is the behaviour we want though - once it gets a wakeup it should be given
638 static void ioc_set_batching(struct request_queue
*q
, struct io_context
*ioc
)
640 if (!ioc
|| ioc_batching(q
, ioc
))
643 ioc
->nr_batch_requests
= q
->nr_batching
;
644 ioc
->last_waited
= jiffies
;
647 static void __freed_request(struct request_queue
*q
, int rw
)
649 struct request_list
*rl
= &q
->rq
;
651 if (rl
->count
[rw
] < queue_congestion_off_threshold(q
))
652 blk_clear_queue_congested(q
, rw
);
654 if (rl
->count
[rw
] + 1 <= q
->nr_requests
) {
655 if (waitqueue_active(&rl
->wait
[rw
]))
656 wake_up(&rl
->wait
[rw
]);
658 blk_clear_queue_full(q
, rw
);
663 * A request has just been released. Account for it, update the full and
664 * congestion status, wake up any waiters. Called under q->queue_lock.
666 static void freed_request(struct request_queue
*q
, int rw
, int priv
)
668 struct request_list
*rl
= &q
->rq
;
674 __freed_request(q
, rw
);
676 if (unlikely(rl
->starved
[rw
^ 1]))
677 __freed_request(q
, rw
^ 1);
680 #define blkdev_free_rq(list) list_entry((list)->next, struct request, queuelist)
682 * Get a free request, queue_lock must be held.
683 * Returns NULL on failure, with queue_lock held.
684 * Returns !NULL on success, with queue_lock *not held*.
686 static struct request
*get_request(struct request_queue
*q
, int rw_flags
,
687 struct bio
*bio
, gfp_t gfp_mask
)
689 struct request
*rq
= NULL
;
690 struct request_list
*rl
= &q
->rq
;
691 struct io_context
*ioc
= NULL
;
692 const int rw
= rw_flags
& 0x01;
695 may_queue
= elv_may_queue(q
, rw_flags
);
696 if (may_queue
== ELV_MQUEUE_NO
)
699 if (rl
->count
[rw
]+1 >= queue_congestion_on_threshold(q
)) {
700 if (rl
->count
[rw
]+1 >= q
->nr_requests
) {
701 ioc
= current_io_context(GFP_ATOMIC
, q
->node
);
703 * The queue will fill after this allocation, so set
704 * it as full, and mark this process as "batching".
705 * This process will be allowed to complete a batch of
706 * requests, others will be blocked.
708 if (!blk_queue_full(q
, rw
)) {
709 ioc_set_batching(q
, ioc
);
710 blk_set_queue_full(q
, rw
);
712 if (may_queue
!= ELV_MQUEUE_MUST
713 && !ioc_batching(q
, ioc
)) {
715 * The queue is full and the allocating
716 * process is not a "batcher", and not
717 * exempted by the IO scheduler
723 blk_set_queue_congested(q
, rw
);
727 * Only allow batching queuers to allocate up to 50% over the defined
728 * limit of requests, otherwise we could have thousands of requests
729 * allocated with any setting of ->nr_requests
731 if (rl
->count
[rw
] >= (3 * q
->nr_requests
/ 2))
737 priv
= !test_bit(QUEUE_FLAG_ELVSWITCH
, &q
->queue_flags
);
741 spin_unlock_irq(q
->queue_lock
);
743 rq
= blk_alloc_request(q
, rw_flags
, priv
, gfp_mask
);
746 * Allocation failed presumably due to memory. Undo anything
747 * we might have messed up.
749 * Allocating task should really be put onto the front of the
750 * wait queue, but this is pretty rare.
752 spin_lock_irq(q
->queue_lock
);
753 freed_request(q
, rw
, priv
);
756 * in the very unlikely event that allocation failed and no
757 * requests for this direction was pending, mark us starved
758 * so that freeing of a request in the other direction will
759 * notice us. another possible fix would be to split the
760 * rq mempool into READ and WRITE
763 if (unlikely(rl
->count
[rw
] == 0))
770 * ioc may be NULL here, and ioc_batching will be false. That's
771 * OK, if the queue is under the request limit then requests need
772 * not count toward the nr_batch_requests limit. There will always
773 * be some limit enforced by BLK_BATCH_TIME.
775 if (ioc_batching(q
, ioc
))
776 ioc
->nr_batch_requests
--;
780 blk_add_trace_generic(q
, bio
, rw
, BLK_TA_GETRQ
);
786 * No available requests for this queue, unplug the device and wait for some
787 * requests to become available.
789 * Called with q->queue_lock held, and returns with it unlocked.
791 static struct request
*get_request_wait(struct request_queue
*q
, int rw_flags
,
794 const int rw
= rw_flags
& 0x01;
797 rq
= get_request(q
, rw_flags
, bio
, GFP_NOIO
);
800 struct request_list
*rl
= &q
->rq
;
802 prepare_to_wait_exclusive(&rl
->wait
[rw
], &wait
,
803 TASK_UNINTERRUPTIBLE
);
805 rq
= get_request(q
, rw_flags
, bio
, GFP_NOIO
);
808 struct io_context
*ioc
;
810 blk_add_trace_generic(q
, bio
, rw
, BLK_TA_SLEEPRQ
);
812 __generic_unplug_device(q
);
813 spin_unlock_irq(q
->queue_lock
);
817 * After sleeping, we become a "batching" process and
818 * will be able to allocate at least one request, and
819 * up to a big batch of them for a small period time.
820 * See ioc_batching, ioc_set_batching
822 ioc
= current_io_context(GFP_NOIO
, q
->node
);
823 ioc_set_batching(q
, ioc
);
825 spin_lock_irq(q
->queue_lock
);
827 finish_wait(&rl
->wait
[rw
], &wait
);
833 struct request
*blk_get_request(struct request_queue
*q
, int rw
, gfp_t gfp_mask
)
837 BUG_ON(rw
!= READ
&& rw
!= WRITE
);
839 spin_lock_irq(q
->queue_lock
);
840 if (gfp_mask
& __GFP_WAIT
) {
841 rq
= get_request_wait(q
, rw
, NULL
);
843 rq
= get_request(q
, rw
, NULL
, gfp_mask
);
845 spin_unlock_irq(q
->queue_lock
);
847 /* q->queue_lock is unlocked at this point */
851 EXPORT_SYMBOL(blk_get_request
);
854 * blk_start_queueing - initiate dispatch of requests to device
855 * @q: request queue to kick into gear
857 * This is basically a helper to remove the need to know whether a queue
858 * is plugged or not if someone just wants to initiate dispatch of requests
861 * The queue lock must be held with interrupts disabled.
863 void blk_start_queueing(struct request_queue
*q
)
865 if (!blk_queue_plugged(q
))
868 __generic_unplug_device(q
);
870 EXPORT_SYMBOL(blk_start_queueing
);
873 * blk_requeue_request - put a request back on queue
874 * @q: request queue where request should be inserted
875 * @rq: request to be inserted
878 * Drivers often keep queueing requests until the hardware cannot accept
879 * more, when that condition happens we need to put the request back
880 * on the queue. Must be called with queue lock held.
882 void blk_requeue_request(struct request_queue
*q
, struct request
*rq
)
884 blk_add_trace_rq(q
, rq
, BLK_TA_REQUEUE
);
886 if (blk_rq_tagged(rq
))
887 blk_queue_end_tag(q
, rq
);
889 elv_requeue_request(q
, rq
);
892 EXPORT_SYMBOL(blk_requeue_request
);
895 * blk_insert_request - insert a special request in to a request queue
896 * @q: request queue where request should be inserted
897 * @rq: request to be inserted
898 * @at_head: insert request at head or tail of queue
899 * @data: private data
902 * Many block devices need to execute commands asynchronously, so they don't
903 * block the whole kernel from preemption during request execution. This is
904 * accomplished normally by inserting aritficial requests tagged as
905 * REQ_SPECIAL in to the corresponding request queue, and letting them be
906 * scheduled for actual execution by the request queue.
908 * We have the option of inserting the head or the tail of the queue.
909 * Typically we use the tail for new ioctls and so forth. We use the head
910 * of the queue for things like a QUEUE_FULL message from a device, or a
911 * host that is unable to accept a particular command.
913 void blk_insert_request(struct request_queue
*q
, struct request
*rq
,
914 int at_head
, void *data
)
916 int where
= at_head
? ELEVATOR_INSERT_FRONT
: ELEVATOR_INSERT_BACK
;
920 * tell I/O scheduler that this isn't a regular read/write (ie it
921 * must not attempt merges on this) and that it acts as a soft
924 rq
->cmd_type
= REQ_TYPE_SPECIAL
;
925 rq
->cmd_flags
|= REQ_SOFTBARRIER
;
929 spin_lock_irqsave(q
->queue_lock
, flags
);
932 * If command is tagged, release the tag
934 if (blk_rq_tagged(rq
))
935 blk_queue_end_tag(q
, rq
);
937 drive_stat_acct(rq
, 1);
938 __elv_add_request(q
, rq
, where
, 0);
939 blk_start_queueing(q
);
940 spin_unlock_irqrestore(q
->queue_lock
, flags
);
943 EXPORT_SYMBOL(blk_insert_request
);
946 * add-request adds a request to the linked list.
947 * queue lock is held and interrupts disabled, as we muck with the
948 * request queue list.
950 static inline void add_request(struct request_queue
* q
, struct request
* req
)
952 drive_stat_acct(req
, 1);
955 * elevator indicated where it wants this request to be
956 * inserted at elevator_merge time
958 __elv_add_request(q
, req
, ELEVATOR_INSERT_SORT
, 0);
962 * disk_round_stats() - Round off the performance stats on a struct
965 * The average IO queue length and utilisation statistics are maintained
966 * by observing the current state of the queue length and the amount of
967 * time it has been in this state for.
969 * Normally, that accounting is done on IO completion, but that can result
970 * in more than a second's worth of IO being accounted for within any one
971 * second, leading to >100% utilisation. To deal with that, we call this
972 * function to do a round-off before returning the results when reading
973 * /proc/diskstats. This accounts immediately for all queue usage up to
974 * the current jiffies and restarts the counters again.
976 void disk_round_stats(struct gendisk
*disk
)
978 unsigned long now
= jiffies
;
980 if (now
== disk
->stamp
)
983 if (disk
->in_flight
) {
984 __disk_stat_add(disk
, time_in_queue
,
985 disk
->in_flight
* (now
- disk
->stamp
));
986 __disk_stat_add(disk
, io_ticks
, (now
- disk
->stamp
));
991 EXPORT_SYMBOL_GPL(disk_round_stats
);
994 * queue lock must be held
996 void __blk_put_request(struct request_queue
*q
, struct request
*req
)
1000 if (unlikely(--req
->ref_count
))
1003 elv_completed_request(q
, req
);
1006 * Request may not have originated from ll_rw_blk. if not,
1007 * it didn't come out of our reserved rq pools
1009 if (req
->cmd_flags
& REQ_ALLOCED
) {
1010 int rw
= rq_data_dir(req
);
1011 int priv
= req
->cmd_flags
& REQ_ELVPRIV
;
1013 BUG_ON(!list_empty(&req
->queuelist
));
1014 BUG_ON(!hlist_unhashed(&req
->hash
));
1016 blk_free_request(q
, req
);
1017 freed_request(q
, rw
, priv
);
1021 EXPORT_SYMBOL_GPL(__blk_put_request
);
1023 void blk_put_request(struct request
*req
)
1025 unsigned long flags
;
1026 struct request_queue
*q
= req
->q
;
1029 * Gee, IDE calls in w/ NULL q. Fix IDE and remove the
1030 * following if (q) test.
1033 spin_lock_irqsave(q
->queue_lock
, flags
);
1034 __blk_put_request(q
, req
);
1035 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1039 EXPORT_SYMBOL(blk_put_request
);
1041 void init_request_from_bio(struct request
*req
, struct bio
*bio
)
1043 req
->cmd_type
= REQ_TYPE_FS
;
1046 * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
1048 if (bio_rw_ahead(bio
) || bio_failfast(bio
))
1049 req
->cmd_flags
|= REQ_FAILFAST
;
1052 * REQ_BARRIER implies no merging, but lets make it explicit
1054 if (unlikely(bio_barrier(bio
)))
1055 req
->cmd_flags
|= (REQ_HARDBARRIER
| REQ_NOMERGE
);
1058 req
->cmd_flags
|= REQ_RW_SYNC
;
1059 if (bio_rw_meta(bio
))
1060 req
->cmd_flags
|= REQ_RW_META
;
1063 req
->hard_sector
= req
->sector
= bio
->bi_sector
;
1064 req
->ioprio
= bio_prio(bio
);
1065 req
->start_time
= jiffies
;
1066 blk_rq_bio_prep(req
->q
, req
, bio
);
1069 static int __make_request(struct request_queue
*q
, struct bio
*bio
)
1071 struct request
*req
;
1072 int el_ret
, nr_sectors
, barrier
, err
;
1073 const unsigned short prio
= bio_prio(bio
);
1074 const int sync
= bio_sync(bio
);
1077 nr_sectors
= bio_sectors(bio
);
1080 * low level driver can indicate that it wants pages above a
1081 * certain limit bounced to low memory (ie for highmem, or even
1082 * ISA dma in theory)
1084 blk_queue_bounce(q
, &bio
);
1086 barrier
= bio_barrier(bio
);
1087 if (unlikely(barrier
) && (q
->next_ordered
== QUEUE_ORDERED_NONE
)) {
1092 spin_lock_irq(q
->queue_lock
);
1094 if (unlikely(barrier
) || elv_queue_empty(q
))
1097 el_ret
= elv_merge(q
, &req
, bio
);
1099 case ELEVATOR_BACK_MERGE
:
1100 BUG_ON(!rq_mergeable(req
));
1102 if (!ll_back_merge_fn(q
, req
, bio
))
1105 blk_add_trace_bio(q
, bio
, BLK_TA_BACKMERGE
);
1107 req
->biotail
->bi_next
= bio
;
1109 req
->nr_sectors
= req
->hard_nr_sectors
+= nr_sectors
;
1110 req
->ioprio
= ioprio_best(req
->ioprio
, prio
);
1111 drive_stat_acct(req
, 0);
1112 if (!attempt_back_merge(q
, req
))
1113 elv_merged_request(q
, req
, el_ret
);
1116 case ELEVATOR_FRONT_MERGE
:
1117 BUG_ON(!rq_mergeable(req
));
1119 if (!ll_front_merge_fn(q
, req
, bio
))
1122 blk_add_trace_bio(q
, bio
, BLK_TA_FRONTMERGE
);
1124 bio
->bi_next
= req
->bio
;
1128 * may not be valid. if the low level driver said
1129 * it didn't need a bounce buffer then it better
1130 * not touch req->buffer either...
1132 req
->buffer
= bio_data(bio
);
1133 req
->current_nr_sectors
= bio_cur_sectors(bio
);
1134 req
->hard_cur_sectors
= req
->current_nr_sectors
;
1135 req
->sector
= req
->hard_sector
= bio
->bi_sector
;
1136 req
->nr_sectors
= req
->hard_nr_sectors
+= nr_sectors
;
1137 req
->ioprio
= ioprio_best(req
->ioprio
, prio
);
1138 drive_stat_acct(req
, 0);
1139 if (!attempt_front_merge(q
, req
))
1140 elv_merged_request(q
, req
, el_ret
);
1143 /* ELV_NO_MERGE: elevator says don't/can't merge. */
1150 * This sync check and mask will be re-done in init_request_from_bio(),
1151 * but we need to set it earlier to expose the sync flag to the
1152 * rq allocator and io schedulers.
1154 rw_flags
= bio_data_dir(bio
);
1156 rw_flags
|= REQ_RW_SYNC
;
1159 * Grab a free request. This is might sleep but can not fail.
1160 * Returns with the queue unlocked.
1162 req
= get_request_wait(q
, rw_flags
, bio
);
1165 * After dropping the lock and possibly sleeping here, our request
1166 * may now be mergeable after it had proven unmergeable (above).
1167 * We don't worry about that case for efficiency. It won't happen
1168 * often, and the elevators are able to handle it.
1170 init_request_from_bio(req
, bio
);
1172 spin_lock_irq(q
->queue_lock
);
1173 if (elv_queue_empty(q
))
1175 add_request(q
, req
);
1178 __generic_unplug_device(q
);
1180 spin_unlock_irq(q
->queue_lock
);
1184 bio_endio(bio
, err
);
1189 * If bio->bi_dev is a partition, remap the location
1191 static inline void blk_partition_remap(struct bio
*bio
)
1193 struct block_device
*bdev
= bio
->bi_bdev
;
1195 if (bio_sectors(bio
) && bdev
!= bdev
->bd_contains
) {
1196 struct hd_struct
*p
= bdev
->bd_part
;
1197 const int rw
= bio_data_dir(bio
);
1199 p
->sectors
[rw
] += bio_sectors(bio
);
1202 bio
->bi_sector
+= p
->start_sect
;
1203 bio
->bi_bdev
= bdev
->bd_contains
;
1205 blk_add_trace_remap(bdev_get_queue(bio
->bi_bdev
), bio
,
1206 bdev
->bd_dev
, bio
->bi_sector
,
1207 bio
->bi_sector
- p
->start_sect
);
1211 static void handle_bad_sector(struct bio
*bio
)
1213 char b
[BDEVNAME_SIZE
];
1215 printk(KERN_INFO
"attempt to access beyond end of device\n");
1216 printk(KERN_INFO
"%s: rw=%ld, want=%Lu, limit=%Lu\n",
1217 bdevname(bio
->bi_bdev
, b
),
1219 (unsigned long long)bio
->bi_sector
+ bio_sectors(bio
),
1220 (long long)(bio
->bi_bdev
->bd_inode
->i_size
>> 9));
1222 set_bit(BIO_EOF
, &bio
->bi_flags
);
1225 #ifdef CONFIG_FAIL_MAKE_REQUEST
1227 static DECLARE_FAULT_ATTR(fail_make_request
);
1229 static int __init
setup_fail_make_request(char *str
)
1231 return setup_fault_attr(&fail_make_request
, str
);
1233 __setup("fail_make_request=", setup_fail_make_request
);
1235 static int should_fail_request(struct bio
*bio
)
1237 if ((bio
->bi_bdev
->bd_disk
->flags
& GENHD_FL_FAIL
) ||
1238 (bio
->bi_bdev
->bd_part
&& bio
->bi_bdev
->bd_part
->make_it_fail
))
1239 return should_fail(&fail_make_request
, bio
->bi_size
);
1244 static int __init
fail_make_request_debugfs(void)
1246 return init_fault_attr_dentries(&fail_make_request
,
1247 "fail_make_request");
1250 late_initcall(fail_make_request_debugfs
);
1252 #else /* CONFIG_FAIL_MAKE_REQUEST */
1254 static inline int should_fail_request(struct bio
*bio
)
1259 #endif /* CONFIG_FAIL_MAKE_REQUEST */
1262 * Check whether this bio extends beyond the end of the device.
1264 static inline int bio_check_eod(struct bio
*bio
, unsigned int nr_sectors
)
1271 /* Test device or partition size, when known. */
1272 maxsector
= bio
->bi_bdev
->bd_inode
->i_size
>> 9;
1274 sector_t sector
= bio
->bi_sector
;
1276 if (maxsector
< nr_sectors
|| maxsector
- nr_sectors
< sector
) {
1278 * This may well happen - the kernel calls bread()
1279 * without checking the size of the device, e.g., when
1280 * mounting a device.
1282 handle_bad_sector(bio
);
1291 * generic_make_request: hand a buffer to its device driver for I/O
1292 * @bio: The bio describing the location in memory and on the device.
1294 * generic_make_request() is used to make I/O requests of block
1295 * devices. It is passed a &struct bio, which describes the I/O that needs
1298 * generic_make_request() does not return any status. The
1299 * success/failure status of the request, along with notification of
1300 * completion, is delivered asynchronously through the bio->bi_end_io
1301 * function described (one day) else where.
1303 * The caller of generic_make_request must make sure that bi_io_vec
1304 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1305 * set to describe the device address, and the
1306 * bi_end_io and optionally bi_private are set to describe how
1307 * completion notification should be signaled.
1309 * generic_make_request and the drivers it calls may use bi_next if this
1310 * bio happens to be merged with someone else, and may change bi_dev and
1311 * bi_sector for remaps as it sees fit. So the values of these fields
1312 * should NOT be depended on after the call to generic_make_request.
1314 static inline void __generic_make_request(struct bio
*bio
)
1316 struct request_queue
*q
;
1317 sector_t old_sector
;
1318 int ret
, nr_sectors
= bio_sectors(bio
);
1324 if (bio_check_eod(bio
, nr_sectors
))
1328 * Resolve the mapping until finished. (drivers are
1329 * still free to implement/resolve their own stacking
1330 * by explicitly returning 0)
1332 * NOTE: we don't repeat the blk_size check for each new device.
1333 * Stacking drivers are expected to know what they are doing.
1338 char b
[BDEVNAME_SIZE
];
1340 q
= bdev_get_queue(bio
->bi_bdev
);
1343 "generic_make_request: Trying to access "
1344 "nonexistent block-device %s (%Lu)\n",
1345 bdevname(bio
->bi_bdev
, b
),
1346 (long long) bio
->bi_sector
);
1348 bio_endio(bio
, err
);
1352 if (unlikely(nr_sectors
> q
->max_hw_sectors
)) {
1353 printk("bio too big device %s (%u > %u)\n",
1354 bdevname(bio
->bi_bdev
, b
),
1360 if (unlikely(test_bit(QUEUE_FLAG_DEAD
, &q
->queue_flags
)))
1363 if (should_fail_request(bio
))
1367 * If this device has partitions, remap block n
1368 * of partition p to block n+start(p) of the disk.
1370 blk_partition_remap(bio
);
1372 if (old_sector
!= -1)
1373 blk_add_trace_remap(q
, bio
, old_dev
, bio
->bi_sector
,
1376 blk_add_trace_bio(q
, bio
, BLK_TA_QUEUE
);
1378 old_sector
= bio
->bi_sector
;
1379 old_dev
= bio
->bi_bdev
->bd_dev
;
1381 if (bio_check_eod(bio
, nr_sectors
))
1383 if (bio_empty_barrier(bio
) && !q
->prepare_flush_fn
) {
1388 ret
= q
->make_request_fn(q
, bio
);
1393 * We only want one ->make_request_fn to be active at a time,
1394 * else stack usage with stacked devices could be a problem.
1395 * So use current->bio_{list,tail} to keep a list of requests
1396 * submited by a make_request_fn function.
1397 * current->bio_tail is also used as a flag to say if
1398 * generic_make_request is currently active in this task or not.
1399 * If it is NULL, then no make_request is active. If it is non-NULL,
1400 * then a make_request is active, and new requests should be added
1403 void generic_make_request(struct bio
*bio
)
1405 if (current
->bio_tail
) {
1406 /* make_request is active */
1407 *(current
->bio_tail
) = bio
;
1408 bio
->bi_next
= NULL
;
1409 current
->bio_tail
= &bio
->bi_next
;
1412 /* following loop may be a bit non-obvious, and so deserves some
1414 * Before entering the loop, bio->bi_next is NULL (as all callers
1415 * ensure that) so we have a list with a single bio.
1416 * We pretend that we have just taken it off a longer list, so
1417 * we assign bio_list to the next (which is NULL) and bio_tail
1418 * to &bio_list, thus initialising the bio_list of new bios to be
1419 * added. __generic_make_request may indeed add some more bios
1420 * through a recursive call to generic_make_request. If it
1421 * did, we find a non-NULL value in bio_list and re-enter the loop
1422 * from the top. In this case we really did just take the bio
1423 * of the top of the list (no pretending) and so fixup bio_list and
1424 * bio_tail or bi_next, and call into __generic_make_request again.
1426 * The loop was structured like this to make only one call to
1427 * __generic_make_request (which is important as it is large and
1428 * inlined) and to keep the structure simple.
1430 BUG_ON(bio
->bi_next
);
1432 current
->bio_list
= bio
->bi_next
;
1433 if (bio
->bi_next
== NULL
)
1434 current
->bio_tail
= ¤t
->bio_list
;
1436 bio
->bi_next
= NULL
;
1437 __generic_make_request(bio
);
1438 bio
= current
->bio_list
;
1440 current
->bio_tail
= NULL
; /* deactivate */
1443 EXPORT_SYMBOL(generic_make_request
);
1446 * submit_bio: submit a bio to the block device layer for I/O
1447 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1448 * @bio: The &struct bio which describes the I/O
1450 * submit_bio() is very similar in purpose to generic_make_request(), and
1451 * uses that function to do most of the work. Both are fairly rough
1452 * interfaces, @bio must be presetup and ready for I/O.
1455 void submit_bio(int rw
, struct bio
*bio
)
1457 int count
= bio_sectors(bio
);
1462 * If it's a regular read/write or a barrier with data attached,
1463 * go through the normal accounting stuff before submission.
1465 if (!bio_empty_barrier(bio
)) {
1467 BIO_BUG_ON(!bio
->bi_size
);
1468 BIO_BUG_ON(!bio
->bi_io_vec
);
1471 count_vm_events(PGPGOUT
, count
);
1473 task_io_account_read(bio
->bi_size
);
1474 count_vm_events(PGPGIN
, count
);
1477 if (unlikely(block_dump
)) {
1478 char b
[BDEVNAME_SIZE
];
1479 printk(KERN_DEBUG
"%s(%d): %s block %Lu on %s\n",
1480 current
->comm
, task_pid_nr(current
),
1481 (rw
& WRITE
) ? "WRITE" : "READ",
1482 (unsigned long long)bio
->bi_sector
,
1483 bdevname(bio
->bi_bdev
,b
));
1487 generic_make_request(bio
);
1490 EXPORT_SYMBOL(submit_bio
);
1493 * __end_that_request_first - end I/O on a request
1494 * @req: the request being processed
1495 * @error: 0 for success, < 0 for error
1496 * @nr_bytes: number of bytes to complete
1499 * Ends I/O on a number of bytes attached to @req, and sets it up
1500 * for the next range of segments (if any) in the cluster.
1503 * 0 - we are done with this request, call end_that_request_last()
1504 * 1 - still buffers pending for this request
1506 static int __end_that_request_first(struct request
*req
, int error
,
1509 int total_bytes
, bio_nbytes
, next_idx
= 0;
1512 blk_add_trace_rq(req
->q
, req
, BLK_TA_COMPLETE
);
1515 * for a REQ_BLOCK_PC request, we want to carry any eventual
1516 * sense key with us all the way through
1518 if (!blk_pc_request(req
))
1522 if (blk_fs_request(req
) && !(req
->cmd_flags
& REQ_QUIET
))
1523 printk("end_request: I/O error, dev %s, sector %llu\n",
1524 req
->rq_disk
? req
->rq_disk
->disk_name
: "?",
1525 (unsigned long long)req
->sector
);
1528 if (blk_fs_request(req
) && req
->rq_disk
) {
1529 const int rw
= rq_data_dir(req
);
1531 disk_stat_add(req
->rq_disk
, sectors
[rw
], nr_bytes
>> 9);
1534 total_bytes
= bio_nbytes
= 0;
1535 while ((bio
= req
->bio
) != NULL
) {
1539 * For an empty barrier request, the low level driver must
1540 * store a potential error location in ->sector. We pass
1541 * that back up in ->bi_sector.
1543 if (blk_empty_barrier(req
))
1544 bio
->bi_sector
= req
->sector
;
1546 if (nr_bytes
>= bio
->bi_size
) {
1547 req
->bio
= bio
->bi_next
;
1548 nbytes
= bio
->bi_size
;
1549 req_bio_endio(req
, bio
, nbytes
, error
);
1553 int idx
= bio
->bi_idx
+ next_idx
;
1555 if (unlikely(bio
->bi_idx
>= bio
->bi_vcnt
)) {
1556 blk_dump_rq_flags(req
, "__end_that");
1557 printk("%s: bio idx %d >= vcnt %d\n",
1559 bio
->bi_idx
, bio
->bi_vcnt
);
1563 nbytes
= bio_iovec_idx(bio
, idx
)->bv_len
;
1564 BIO_BUG_ON(nbytes
> bio
->bi_size
);
1567 * not a complete bvec done
1569 if (unlikely(nbytes
> nr_bytes
)) {
1570 bio_nbytes
+= nr_bytes
;
1571 total_bytes
+= nr_bytes
;
1576 * advance to the next vector
1579 bio_nbytes
+= nbytes
;
1582 total_bytes
+= nbytes
;
1585 if ((bio
= req
->bio
)) {
1587 * end more in this run, or just return 'not-done'
1589 if (unlikely(nr_bytes
<= 0))
1601 * if the request wasn't completed, update state
1604 req_bio_endio(req
, bio
, bio_nbytes
, error
);
1605 bio
->bi_idx
+= next_idx
;
1606 bio_iovec(bio
)->bv_offset
+= nr_bytes
;
1607 bio_iovec(bio
)->bv_len
-= nr_bytes
;
1610 blk_recalc_rq_sectors(req
, total_bytes
>> 9);
1611 blk_recalc_rq_segments(req
);
1616 * splice the completion data to a local structure and hand off to
1617 * process_completion_queue() to complete the requests
1619 static void blk_done_softirq(struct softirq_action
*h
)
1621 struct list_head
*cpu_list
, local_list
;
1623 local_irq_disable();
1624 cpu_list
= &__get_cpu_var(blk_cpu_done
);
1625 list_replace_init(cpu_list
, &local_list
);
1628 while (!list_empty(&local_list
)) {
1629 struct request
*rq
= list_entry(local_list
.next
, struct request
, donelist
);
1631 list_del_init(&rq
->donelist
);
1632 rq
->q
->softirq_done_fn(rq
);
1636 static int __cpuinit
blk_cpu_notify(struct notifier_block
*self
, unsigned long action
,
1640 * If a CPU goes away, splice its entries to the current CPU
1641 * and trigger a run of the softirq
1643 if (action
== CPU_DEAD
|| action
== CPU_DEAD_FROZEN
) {
1644 int cpu
= (unsigned long) hcpu
;
1646 local_irq_disable();
1647 list_splice_init(&per_cpu(blk_cpu_done
, cpu
),
1648 &__get_cpu_var(blk_cpu_done
));
1649 raise_softirq_irqoff(BLOCK_SOFTIRQ
);
1657 static struct notifier_block blk_cpu_notifier __cpuinitdata
= {
1658 .notifier_call
= blk_cpu_notify
,
1662 * blk_complete_request - end I/O on a request
1663 * @req: the request being processed
1666 * Ends all I/O on a request. It does not handle partial completions,
1667 * unless the driver actually implements this in its completion callback
1668 * through requeueing. The actual completion happens out-of-order,
1669 * through a softirq handler. The user must have registered a completion
1670 * callback through blk_queue_softirq_done().
1673 void blk_complete_request(struct request
*req
)
1675 struct list_head
*cpu_list
;
1676 unsigned long flags
;
1678 BUG_ON(!req
->q
->softirq_done_fn
);
1680 local_irq_save(flags
);
1682 cpu_list
= &__get_cpu_var(blk_cpu_done
);
1683 list_add_tail(&req
->donelist
, cpu_list
);
1684 raise_softirq_irqoff(BLOCK_SOFTIRQ
);
1686 local_irq_restore(flags
);
1689 EXPORT_SYMBOL(blk_complete_request
);
1692 * queue lock must be held
1694 static void end_that_request_last(struct request
*req
, int error
)
1696 struct gendisk
*disk
= req
->rq_disk
;
1698 if (blk_rq_tagged(req
))
1699 blk_queue_end_tag(req
->q
, req
);
1701 if (blk_queued_rq(req
))
1702 blkdev_dequeue_request(req
);
1704 if (unlikely(laptop_mode
) && blk_fs_request(req
))
1705 laptop_io_completion();
1708 * Account IO completion. bar_rq isn't accounted as a normal
1709 * IO on queueing nor completion. Accounting the containing
1710 * request is enough.
1712 if (disk
&& blk_fs_request(req
) && req
!= &req
->q
->bar_rq
) {
1713 unsigned long duration
= jiffies
- req
->start_time
;
1714 const int rw
= rq_data_dir(req
);
1716 __disk_stat_inc(disk
, ios
[rw
]);
1717 __disk_stat_add(disk
, ticks
[rw
], duration
);
1718 disk_round_stats(disk
);
1723 req
->end_io(req
, error
);
1725 if (blk_bidi_rq(req
))
1726 __blk_put_request(req
->next_rq
->q
, req
->next_rq
);
1728 __blk_put_request(req
->q
, req
);
1732 static inline void __end_request(struct request
*rq
, int uptodate
,
1733 unsigned int nr_bytes
)
1738 error
= uptodate
? uptodate
: -EIO
;
1740 __blk_end_request(rq
, error
, nr_bytes
);
1744 * blk_rq_bytes - Returns bytes left to complete in the entire request
1746 unsigned int blk_rq_bytes(struct request
*rq
)
1748 if (blk_fs_request(rq
))
1749 return rq
->hard_nr_sectors
<< 9;
1751 return rq
->data_len
;
1753 EXPORT_SYMBOL_GPL(blk_rq_bytes
);
1756 * blk_rq_cur_bytes - Returns bytes left to complete in the current segment
1758 unsigned int blk_rq_cur_bytes(struct request
*rq
)
1760 if (blk_fs_request(rq
))
1761 return rq
->current_nr_sectors
<< 9;
1764 return rq
->bio
->bi_size
;
1766 return rq
->data_len
;
1768 EXPORT_SYMBOL_GPL(blk_rq_cur_bytes
);
1771 * end_queued_request - end all I/O on a queued request
1772 * @rq: the request being processed
1773 * @uptodate: error value or 0/1 uptodate flag
1776 * Ends all I/O on a request, and removes it from the block layer queues.
1777 * Not suitable for normal IO completion, unless the driver still has
1778 * the request attached to the block layer.
1781 void end_queued_request(struct request
*rq
, int uptodate
)
1783 __end_request(rq
, uptodate
, blk_rq_bytes(rq
));
1785 EXPORT_SYMBOL(end_queued_request
);
1788 * end_dequeued_request - end all I/O on a dequeued request
1789 * @rq: the request being processed
1790 * @uptodate: error value or 0/1 uptodate flag
1793 * Ends all I/O on a request. The request must already have been
1794 * dequeued using blkdev_dequeue_request(), as is normally the case
1798 void end_dequeued_request(struct request
*rq
, int uptodate
)
1800 __end_request(rq
, uptodate
, blk_rq_bytes(rq
));
1802 EXPORT_SYMBOL(end_dequeued_request
);
1806 * end_request - end I/O on the current segment of the request
1807 * @req: the request being processed
1808 * @uptodate: error value or 0/1 uptodate flag
1811 * Ends I/O on the current segment of a request. If that is the only
1812 * remaining segment, the request is also completed and freed.
1814 * This is a remnant of how older block drivers handled IO completions.
1815 * Modern drivers typically end IO on the full request in one go, unless
1816 * they have a residual value to account for. For that case this function
1817 * isn't really useful, unless the residual just happens to be the
1818 * full current segment. In other words, don't use this function in new
1819 * code. Either use end_request_completely(), or the
1820 * end_that_request_chunk() (along with end_that_request_last()) for
1821 * partial completions.
1824 void end_request(struct request
*req
, int uptodate
)
1826 __end_request(req
, uptodate
, req
->hard_cur_sectors
<< 9);
1828 EXPORT_SYMBOL(end_request
);
1831 * blk_end_io - Generic end_io function to complete a request.
1832 * @rq: the request being processed
1833 * @error: 0 for success, < 0 for error
1834 * @nr_bytes: number of bytes to complete @rq
1835 * @bidi_bytes: number of bytes to complete @rq->next_rq
1836 * @drv_callback: function called between completion of bios in the request
1837 * and completion of the request.
1838 * If the callback returns non 0, this helper returns without
1839 * completion of the request.
1842 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
1843 * If @rq has leftover, sets it up for the next range of segments.
1846 * 0 - we are done with this request
1847 * 1 - this request is not freed yet, it still has pending buffers.
1849 static int blk_end_io(struct request
*rq
, int error
, int nr_bytes
,
1850 int bidi_bytes
, int (drv_callback
)(struct request
*))
1852 struct request_queue
*q
= rq
->q
;
1853 unsigned long flags
= 0UL;
1855 if (blk_fs_request(rq
) || blk_pc_request(rq
)) {
1856 if (__end_that_request_first(rq
, error
, nr_bytes
))
1859 /* Bidi request must be completed as a whole */
1860 if (blk_bidi_rq(rq
) &&
1861 __end_that_request_first(rq
->next_rq
, error
, bidi_bytes
))
1865 /* Special feature for tricky drivers */
1866 if (drv_callback
&& drv_callback(rq
))
1869 add_disk_randomness(rq
->rq_disk
);
1871 spin_lock_irqsave(q
->queue_lock
, flags
);
1872 end_that_request_last(rq
, error
);
1873 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1879 * blk_end_request - Helper function for drivers to complete the request.
1880 * @rq: the request being processed
1881 * @error: 0 for success, < 0 for error
1882 * @nr_bytes: number of bytes to complete
1885 * Ends I/O on a number of bytes attached to @rq.
1886 * If @rq has leftover, sets it up for the next range of segments.
1889 * 0 - we are done with this request
1890 * 1 - still buffers pending for this request
1892 int blk_end_request(struct request
*rq
, int error
, int nr_bytes
)
1894 return blk_end_io(rq
, error
, nr_bytes
, 0, NULL
);
1896 EXPORT_SYMBOL_GPL(blk_end_request
);
1899 * __blk_end_request - Helper function for drivers to complete the request.
1900 * @rq: the request being processed
1901 * @error: 0 for success, < 0 for error
1902 * @nr_bytes: number of bytes to complete
1905 * Must be called with queue lock held unlike blk_end_request().
1908 * 0 - we are done with this request
1909 * 1 - still buffers pending for this request
1911 int __blk_end_request(struct request
*rq
, int error
, int nr_bytes
)
1913 if (blk_fs_request(rq
) || blk_pc_request(rq
)) {
1914 if (__end_that_request_first(rq
, error
, nr_bytes
))
1918 add_disk_randomness(rq
->rq_disk
);
1920 end_that_request_last(rq
, error
);
1924 EXPORT_SYMBOL_GPL(__blk_end_request
);
1927 * blk_end_bidi_request - Helper function for drivers to complete bidi request.
1928 * @rq: the bidi request being processed
1929 * @error: 0 for success, < 0 for error
1930 * @nr_bytes: number of bytes to complete @rq
1931 * @bidi_bytes: number of bytes to complete @rq->next_rq
1934 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
1937 * 0 - we are done with this request
1938 * 1 - still buffers pending for this request
1940 int blk_end_bidi_request(struct request
*rq
, int error
, int nr_bytes
,
1943 return blk_end_io(rq
, error
, nr_bytes
, bidi_bytes
, NULL
);
1945 EXPORT_SYMBOL_GPL(blk_end_bidi_request
);
1948 * blk_end_request_callback - Special helper function for tricky drivers
1949 * @rq: the request being processed
1950 * @error: 0 for success, < 0 for error
1951 * @nr_bytes: number of bytes to complete
1952 * @drv_callback: function called between completion of bios in the request
1953 * and completion of the request.
1954 * If the callback returns non 0, this helper returns without
1955 * completion of the request.
1958 * Ends I/O on a number of bytes attached to @rq.
1959 * If @rq has leftover, sets it up for the next range of segments.
1961 * This special helper function is used only for existing tricky drivers.
1962 * (e.g. cdrom_newpc_intr() of ide-cd)
1963 * This interface will be removed when such drivers are rewritten.
1964 * Don't use this interface in other places anymore.
1967 * 0 - we are done with this request
1968 * 1 - this request is not freed yet.
1969 * this request still has pending buffers or
1970 * the driver doesn't want to finish this request yet.
1972 int blk_end_request_callback(struct request
*rq
, int error
, int nr_bytes
,
1973 int (drv_callback
)(struct request
*))
1975 return blk_end_io(rq
, error
, nr_bytes
, 0, drv_callback
);
1977 EXPORT_SYMBOL_GPL(blk_end_request_callback
);
1979 void blk_rq_bio_prep(struct request_queue
*q
, struct request
*rq
,
1982 /* first two bits are identical in rq->cmd_flags and bio->bi_rw */
1983 rq
->cmd_flags
|= (bio
->bi_rw
& 3);
1985 rq
->nr_phys_segments
= bio_phys_segments(q
, bio
);
1986 rq
->nr_hw_segments
= bio_hw_segments(q
, bio
);
1987 rq
->current_nr_sectors
= bio_cur_sectors(bio
);
1988 rq
->hard_cur_sectors
= rq
->current_nr_sectors
;
1989 rq
->hard_nr_sectors
= rq
->nr_sectors
= bio_sectors(bio
);
1990 rq
->buffer
= bio_data(bio
);
1991 rq
->data_len
= bio
->bi_size
;
1993 rq
->bio
= rq
->biotail
= bio
;
1996 rq
->rq_disk
= bio
->bi_bdev
->bd_disk
;
1999 int kblockd_schedule_work(struct work_struct
*work
)
2001 return queue_work(kblockd_workqueue
, work
);
2004 EXPORT_SYMBOL(kblockd_schedule_work
);
2006 void kblockd_flush_work(struct work_struct
*work
)
2008 cancel_work_sync(work
);
2010 EXPORT_SYMBOL(kblockd_flush_work
);
2012 int __init
blk_dev_init(void)
2016 kblockd_workqueue
= create_workqueue("kblockd");
2017 if (!kblockd_workqueue
)
2018 panic("Failed to create kblockd\n");
2020 request_cachep
= kmem_cache_create("blkdev_requests",
2021 sizeof(struct request
), 0, SLAB_PANIC
, NULL
);
2023 blk_requestq_cachep
= kmem_cache_create("blkdev_queue",
2024 sizeof(struct request_queue
), 0, SLAB_PANIC
, NULL
);
2026 for_each_possible_cpu(i
)
2027 INIT_LIST_HEAD(&per_cpu(blk_cpu_done
, i
));
2029 open_softirq(BLOCK_SOFTIRQ
, blk_done_softirq
, NULL
);
2030 register_hotcpu_notifier(&blk_cpu_notifier
);