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>
8 * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
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>
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/blktrace_api.h>
30 #include <linux/fault-inject.h>
31 #include <trace/block.h>
35 DEFINE_TRACE(block_plug
);
36 DEFINE_TRACE(block_unplug_io
);
37 DEFINE_TRACE(block_unplug_timer
);
38 DEFINE_TRACE(block_getrq
);
39 DEFINE_TRACE(block_sleeprq
);
40 DEFINE_TRACE(block_rq_requeue
);
41 DEFINE_TRACE(block_bio_backmerge
);
42 DEFINE_TRACE(block_bio_frontmerge
);
43 DEFINE_TRACE(block_bio_queue
);
44 DEFINE_TRACE(block_rq_complete
);
45 DEFINE_TRACE(block_remap
); /* Also used in drivers/md/dm.c */
46 EXPORT_TRACEPOINT_SYMBOL_GPL(block_remap
);
48 static int __make_request(struct request_queue
*q
, struct bio
*bio
);
51 * For the allocated request tables
53 static struct kmem_cache
*request_cachep
;
56 * For queue allocation
58 struct kmem_cache
*blk_requestq_cachep
;
61 * Controlling structure to kblockd
63 static struct workqueue_struct
*kblockd_workqueue
;
65 static void drive_stat_acct(struct request
*rq
, int new_io
)
67 struct hd_struct
*part
;
68 int rw
= rq_data_dir(rq
);
71 if (!blk_fs_request(rq
) || !rq
->rq_disk
)
74 cpu
= part_stat_lock();
75 part
= disk_map_sector_rcu(rq
->rq_disk
, rq
->sector
);
78 part_stat_inc(cpu
, part
, merges
[rw
]);
80 part_round_stats(cpu
, part
);
81 part_inc_in_flight(part
);
87 void blk_queue_congestion_threshold(struct request_queue
*q
)
91 nr
= q
->nr_requests
- (q
->nr_requests
/ 8) + 1;
92 if (nr
> q
->nr_requests
)
94 q
->nr_congestion_on
= nr
;
96 nr
= q
->nr_requests
- (q
->nr_requests
/ 8) - (q
->nr_requests
/ 16) - 1;
99 q
->nr_congestion_off
= nr
;
103 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
106 * Locates the passed device's request queue and returns the address of its
109 * Will return NULL if the request queue cannot be located.
111 struct backing_dev_info
*blk_get_backing_dev_info(struct block_device
*bdev
)
113 struct backing_dev_info
*ret
= NULL
;
114 struct request_queue
*q
= bdev_get_queue(bdev
);
117 ret
= &q
->backing_dev_info
;
120 EXPORT_SYMBOL(blk_get_backing_dev_info
);
122 void blk_rq_init(struct request_queue
*q
, struct request
*rq
)
124 memset(rq
, 0, sizeof(*rq
));
126 INIT_LIST_HEAD(&rq
->queuelist
);
127 INIT_LIST_HEAD(&rq
->timeout_list
);
130 rq
->sector
= rq
->hard_sector
= (sector_t
) -1;
131 INIT_HLIST_NODE(&rq
->hash
);
132 RB_CLEAR_NODE(&rq
->rb_node
);
137 EXPORT_SYMBOL(blk_rq_init
);
139 static void req_bio_endio(struct request
*rq
, struct bio
*bio
,
140 unsigned int nbytes
, int error
)
142 struct request_queue
*q
= rq
->q
;
144 if (&q
->bar_rq
!= rq
) {
146 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
147 else if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
150 if (unlikely(nbytes
> bio
->bi_size
)) {
151 printk(KERN_ERR
"%s: want %u bytes done, %u left\n",
152 __func__
, nbytes
, bio
->bi_size
);
153 nbytes
= bio
->bi_size
;
156 bio
->bi_size
-= nbytes
;
157 bio
->bi_sector
+= (nbytes
>> 9);
159 if (bio_integrity(bio
))
160 bio_integrity_advance(bio
, nbytes
);
162 if (bio
->bi_size
== 0)
163 bio_endio(bio
, error
);
167 * Okay, this is the barrier request in progress, just
170 if (error
&& !q
->orderr
)
175 void blk_dump_rq_flags(struct request
*rq
, char *msg
)
179 printk(KERN_INFO
"%s: dev %s: type=%x, flags=%x\n", msg
,
180 rq
->rq_disk
? rq
->rq_disk
->disk_name
: "?", rq
->cmd_type
,
183 printk(KERN_INFO
" sector %llu, nr/cnr %lu/%u\n",
184 (unsigned long long)rq
->sector
,
186 rq
->current_nr_sectors
);
187 printk(KERN_INFO
" bio %p, biotail %p, buffer %p, data %p, len %u\n",
188 rq
->bio
, rq
->biotail
,
189 rq
->buffer
, rq
->data
,
192 if (blk_pc_request(rq
)) {
193 printk(KERN_INFO
" cdb: ");
194 for (bit
= 0; bit
< BLK_MAX_CDB
; bit
++)
195 printk("%02x ", rq
->cmd
[bit
]);
199 EXPORT_SYMBOL(blk_dump_rq_flags
);
202 * "plug" the device if there are no outstanding requests: this will
203 * force the transfer to start only after we have put all the requests
206 * This is called with interrupts off and no requests on the queue and
207 * with the queue lock held.
209 void blk_plug_device(struct request_queue
*q
)
211 WARN_ON(!irqs_disabled());
214 * don't plug a stopped queue, it must be paired with blk_start_queue()
215 * which will restart the queueing
217 if (blk_queue_stopped(q
))
220 if (!queue_flag_test_and_set(QUEUE_FLAG_PLUGGED
, q
)) {
221 mod_timer(&q
->unplug_timer
, jiffies
+ q
->unplug_delay
);
225 EXPORT_SYMBOL(blk_plug_device
);
228 * blk_plug_device_unlocked - plug a device without queue lock held
229 * @q: The &struct request_queue to plug
232 * Like @blk_plug_device(), but grabs the queue lock and disables
235 void blk_plug_device_unlocked(struct request_queue
*q
)
239 spin_lock_irqsave(q
->queue_lock
, flags
);
241 spin_unlock_irqrestore(q
->queue_lock
, flags
);
243 EXPORT_SYMBOL(blk_plug_device_unlocked
);
246 * remove the queue from the plugged list, if present. called with
247 * queue lock held and interrupts disabled.
249 int blk_remove_plug(struct request_queue
*q
)
251 WARN_ON(!irqs_disabled());
253 if (!queue_flag_test_and_clear(QUEUE_FLAG_PLUGGED
, q
))
256 del_timer(&q
->unplug_timer
);
259 EXPORT_SYMBOL(blk_remove_plug
);
262 * remove the plug and let it rip..
264 void __generic_unplug_device(struct request_queue
*q
)
266 if (unlikely(blk_queue_stopped(q
)))
269 if (!blk_remove_plug(q
))
276 * generic_unplug_device - fire a request queue
277 * @q: The &struct request_queue in question
280 * Linux uses plugging to build bigger requests queues before letting
281 * the device have at them. If a queue is plugged, the I/O scheduler
282 * is still adding and merging requests on the queue. Once the queue
283 * gets unplugged, the request_fn defined for the queue is invoked and
286 void generic_unplug_device(struct request_queue
*q
)
288 if (blk_queue_plugged(q
)) {
289 spin_lock_irq(q
->queue_lock
);
290 __generic_unplug_device(q
);
291 spin_unlock_irq(q
->queue_lock
);
294 EXPORT_SYMBOL(generic_unplug_device
);
296 static void blk_backing_dev_unplug(struct backing_dev_info
*bdi
,
299 struct request_queue
*q
= bdi
->unplug_io_data
;
304 void blk_unplug_work(struct work_struct
*work
)
306 struct request_queue
*q
=
307 container_of(work
, struct request_queue
, unplug_work
);
309 trace_block_unplug_io(q
);
313 void blk_unplug_timeout(unsigned long data
)
315 struct request_queue
*q
= (struct request_queue
*)data
;
317 trace_block_unplug_timer(q
);
318 kblockd_schedule_work(q
, &q
->unplug_work
);
321 void blk_unplug(struct request_queue
*q
)
324 * devices don't necessarily have an ->unplug_fn defined
327 trace_block_unplug_io(q
);
331 EXPORT_SYMBOL(blk_unplug
);
333 static void blk_invoke_request_fn(struct request_queue
*q
)
335 if (unlikely(blk_queue_stopped(q
)))
339 * one level of recursion is ok and is much faster than kicking
340 * the unplug handling
342 if (!queue_flag_test_and_set(QUEUE_FLAG_REENTER
, q
)) {
344 queue_flag_clear(QUEUE_FLAG_REENTER
, q
);
346 queue_flag_set(QUEUE_FLAG_PLUGGED
, q
);
347 kblockd_schedule_work(q
, &q
->unplug_work
);
352 * blk_start_queue - restart a previously stopped queue
353 * @q: The &struct request_queue in question
356 * blk_start_queue() will clear the stop flag on the queue, and call
357 * the request_fn for the queue if it was in a stopped state when
358 * entered. Also see blk_stop_queue(). Queue lock must be held.
360 void blk_start_queue(struct request_queue
*q
)
362 WARN_ON(!irqs_disabled());
364 queue_flag_clear(QUEUE_FLAG_STOPPED
, q
);
365 blk_invoke_request_fn(q
);
367 EXPORT_SYMBOL(blk_start_queue
);
370 * blk_stop_queue - stop a queue
371 * @q: The &struct request_queue in question
374 * The Linux block layer assumes that a block driver will consume all
375 * entries on the request queue when the request_fn strategy is called.
376 * Often this will not happen, because of hardware limitations (queue
377 * depth settings). If a device driver gets a 'queue full' response,
378 * or if it simply chooses not to queue more I/O at one point, it can
379 * call this function to prevent the request_fn from being called until
380 * the driver has signalled it's ready to go again. This happens by calling
381 * blk_start_queue() to restart queue operations. Queue lock must be held.
383 void blk_stop_queue(struct request_queue
*q
)
386 queue_flag_set(QUEUE_FLAG_STOPPED
, q
);
388 EXPORT_SYMBOL(blk_stop_queue
);
391 * blk_sync_queue - cancel any pending callbacks on a queue
395 * The block layer may perform asynchronous callback activity
396 * on a queue, such as calling the unplug function after a timeout.
397 * A block device may call blk_sync_queue to ensure that any
398 * such activity is cancelled, thus allowing it to release resources
399 * that the callbacks might use. The caller must already have made sure
400 * that its ->make_request_fn will not re-add plugging prior to calling
404 void blk_sync_queue(struct request_queue
*q
)
406 del_timer_sync(&q
->unplug_timer
);
407 del_timer_sync(&q
->timeout
);
408 kblockd_flush_work(&q
->unplug_work
);
410 EXPORT_SYMBOL(blk_sync_queue
);
413 * __blk_run_queue - run a single device queue
414 * @q: The queue to run
417 * See @blk_run_queue. This variant must be called with the queue lock
418 * held and interrupts disabled.
421 void __blk_run_queue(struct request_queue
*q
)
426 * Only recurse once to avoid overrunning the stack, let the unplug
427 * handling reinvoke the handler shortly if we already got there.
429 if (!elv_queue_empty(q
))
430 blk_invoke_request_fn(q
);
432 EXPORT_SYMBOL(__blk_run_queue
);
435 * blk_run_queue - run a single device queue
436 * @q: The queue to run
439 * Invoke request handling on this queue, if it has pending work to do.
440 * May be used to restart queueing when a request has completed. Also
441 * See @blk_start_queueing.
444 void blk_run_queue(struct request_queue
*q
)
448 spin_lock_irqsave(q
->queue_lock
, flags
);
450 spin_unlock_irqrestore(q
->queue_lock
, flags
);
452 EXPORT_SYMBOL(blk_run_queue
);
454 void blk_put_queue(struct request_queue
*q
)
456 kobject_put(&q
->kobj
);
459 void blk_cleanup_queue(struct request_queue
*q
)
462 * We know we have process context here, so we can be a little
463 * cautious and ensure that pending block actions on this device
464 * are done before moving on. Going into this function, we should
465 * not have processes doing IO to this device.
469 mutex_lock(&q
->sysfs_lock
);
470 queue_flag_set_unlocked(QUEUE_FLAG_DEAD
, q
);
471 mutex_unlock(&q
->sysfs_lock
);
474 elevator_exit(q
->elevator
);
478 EXPORT_SYMBOL(blk_cleanup_queue
);
480 static int blk_init_free_list(struct request_queue
*q
)
482 struct request_list
*rl
= &q
->rq
;
484 rl
->count
[READ
] = rl
->count
[WRITE
] = 0;
485 rl
->starved
[READ
] = rl
->starved
[WRITE
] = 0;
487 init_waitqueue_head(&rl
->wait
[READ
]);
488 init_waitqueue_head(&rl
->wait
[WRITE
]);
490 rl
->rq_pool
= mempool_create_node(BLKDEV_MIN_RQ
, mempool_alloc_slab
,
491 mempool_free_slab
, request_cachep
, q
->node
);
499 struct request_queue
*blk_alloc_queue(gfp_t gfp_mask
)
501 return blk_alloc_queue_node(gfp_mask
, -1);
503 EXPORT_SYMBOL(blk_alloc_queue
);
505 struct request_queue
*blk_alloc_queue_node(gfp_t gfp_mask
, int node_id
)
507 struct request_queue
*q
;
510 q
= kmem_cache_alloc_node(blk_requestq_cachep
,
511 gfp_mask
| __GFP_ZERO
, node_id
);
515 q
->backing_dev_info
.unplug_io_fn
= blk_backing_dev_unplug
;
516 q
->backing_dev_info
.unplug_io_data
= q
;
517 err
= bdi_init(&q
->backing_dev_info
);
519 kmem_cache_free(blk_requestq_cachep
, q
);
523 init_timer(&q
->unplug_timer
);
524 setup_timer(&q
->timeout
, blk_rq_timed_out_timer
, (unsigned long) q
);
525 INIT_LIST_HEAD(&q
->timeout_list
);
526 INIT_WORK(&q
->unplug_work
, blk_unplug_work
);
528 kobject_init(&q
->kobj
, &blk_queue_ktype
);
530 mutex_init(&q
->sysfs_lock
);
531 spin_lock_init(&q
->__queue_lock
);
535 EXPORT_SYMBOL(blk_alloc_queue_node
);
538 * blk_init_queue - prepare a request queue for use with a block device
539 * @rfn: The function to be called to process requests that have been
540 * placed on the queue.
541 * @lock: Request queue spin lock
544 * If a block device wishes to use the standard request handling procedures,
545 * which sorts requests and coalesces adjacent requests, then it must
546 * call blk_init_queue(). The function @rfn will be called when there
547 * are requests on the queue that need to be processed. If the device
548 * supports plugging, then @rfn may not be called immediately when requests
549 * are available on the queue, but may be called at some time later instead.
550 * Plugged queues are generally unplugged when a buffer belonging to one
551 * of the requests on the queue is needed, or due to memory pressure.
553 * @rfn is not required, or even expected, to remove all requests off the
554 * queue, but only as many as it can handle at a time. If it does leave
555 * requests on the queue, it is responsible for arranging that the requests
556 * get dealt with eventually.
558 * The queue spin lock must be held while manipulating the requests on the
559 * request queue; this lock will be taken also from interrupt context, so irq
560 * disabling is needed for it.
562 * Function returns a pointer to the initialized request queue, or %NULL if
566 * blk_init_queue() must be paired with a blk_cleanup_queue() call
567 * when the block device is deactivated (such as at module unload).
570 struct request_queue
*blk_init_queue(request_fn_proc
*rfn
, spinlock_t
*lock
)
572 return blk_init_queue_node(rfn
, lock
, -1);
574 EXPORT_SYMBOL(blk_init_queue
);
576 struct request_queue
*
577 blk_init_queue_node(request_fn_proc
*rfn
, spinlock_t
*lock
, int node_id
)
579 struct request_queue
*q
= blk_alloc_queue_node(GFP_KERNEL
, node_id
);
585 if (blk_init_free_list(q
)) {
586 kmem_cache_free(blk_requestq_cachep
, q
);
591 * if caller didn't supply a lock, they get per-queue locking with
595 lock
= &q
->__queue_lock
;
598 q
->prep_rq_fn
= NULL
;
599 q
->unplug_fn
= generic_unplug_device
;
600 q
->queue_flags
= (1 << QUEUE_FLAG_CLUSTER
|
601 1 << QUEUE_FLAG_STACKABLE
);
602 q
->queue_lock
= lock
;
604 blk_queue_segment_boundary(q
, BLK_SEG_BOUNDARY_MASK
);
606 blk_queue_make_request(q
, __make_request
);
607 blk_queue_max_segment_size(q
, MAX_SEGMENT_SIZE
);
609 blk_queue_max_hw_segments(q
, MAX_HW_SEGMENTS
);
610 blk_queue_max_phys_segments(q
, MAX_PHYS_SEGMENTS
);
612 q
->sg_reserved_size
= INT_MAX
;
614 blk_set_cmd_filter_defaults(&q
->cmd_filter
);
619 if (!elevator_init(q
, NULL
)) {
620 blk_queue_congestion_threshold(q
);
627 EXPORT_SYMBOL(blk_init_queue_node
);
629 int blk_get_queue(struct request_queue
*q
)
631 if (likely(!test_bit(QUEUE_FLAG_DEAD
, &q
->queue_flags
))) {
632 kobject_get(&q
->kobj
);
639 static inline void blk_free_request(struct request_queue
*q
, struct request
*rq
)
641 if (rq
->cmd_flags
& REQ_ELVPRIV
)
642 elv_put_request(q
, rq
);
643 mempool_free(rq
, q
->rq
.rq_pool
);
646 static struct request
*
647 blk_alloc_request(struct request_queue
*q
, int rw
, int priv
, gfp_t gfp_mask
)
649 struct request
*rq
= mempool_alloc(q
->rq
.rq_pool
, gfp_mask
);
656 rq
->cmd_flags
= rw
| REQ_ALLOCED
;
659 if (unlikely(elv_set_request(q
, rq
, gfp_mask
))) {
660 mempool_free(rq
, q
->rq
.rq_pool
);
663 rq
->cmd_flags
|= REQ_ELVPRIV
;
670 * ioc_batching returns true if the ioc is a valid batching request and
671 * should be given priority access to a request.
673 static inline int ioc_batching(struct request_queue
*q
, struct io_context
*ioc
)
679 * Make sure the process is able to allocate at least 1 request
680 * even if the batch times out, otherwise we could theoretically
683 return ioc
->nr_batch_requests
== q
->nr_batching
||
684 (ioc
->nr_batch_requests
> 0
685 && time_before(jiffies
, ioc
->last_waited
+ BLK_BATCH_TIME
));
689 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
690 * will cause the process to be a "batcher" on all queues in the system. This
691 * is the behaviour we want though - once it gets a wakeup it should be given
694 static void ioc_set_batching(struct request_queue
*q
, struct io_context
*ioc
)
696 if (!ioc
|| ioc_batching(q
, ioc
))
699 ioc
->nr_batch_requests
= q
->nr_batching
;
700 ioc
->last_waited
= jiffies
;
703 static void __freed_request(struct request_queue
*q
, int rw
)
705 struct request_list
*rl
= &q
->rq
;
707 if (rl
->count
[rw
] < queue_congestion_off_threshold(q
))
708 blk_clear_queue_congested(q
, rw
);
710 if (rl
->count
[rw
] + 1 <= q
->nr_requests
) {
711 if (waitqueue_active(&rl
->wait
[rw
]))
712 wake_up(&rl
->wait
[rw
]);
714 blk_clear_queue_full(q
, rw
);
719 * A request has just been released. Account for it, update the full and
720 * congestion status, wake up any waiters. Called under q->queue_lock.
722 static void freed_request(struct request_queue
*q
, int rw
, int priv
)
724 struct request_list
*rl
= &q
->rq
;
730 __freed_request(q
, rw
);
732 if (unlikely(rl
->starved
[rw
^ 1]))
733 __freed_request(q
, rw
^ 1);
736 #define blkdev_free_rq(list) list_entry((list)->next, struct request, queuelist)
738 * Get a free request, queue_lock must be held.
739 * Returns NULL on failure, with queue_lock held.
740 * Returns !NULL on success, with queue_lock *not held*.
742 static struct request
*get_request(struct request_queue
*q
, int rw_flags
,
743 struct bio
*bio
, gfp_t gfp_mask
)
745 struct request
*rq
= NULL
;
746 struct request_list
*rl
= &q
->rq
;
747 struct io_context
*ioc
= NULL
;
748 const int rw
= rw_flags
& 0x01;
751 may_queue
= elv_may_queue(q
, rw_flags
);
752 if (may_queue
== ELV_MQUEUE_NO
)
755 if (rl
->count
[rw
]+1 >= queue_congestion_on_threshold(q
)) {
756 if (rl
->count
[rw
]+1 >= q
->nr_requests
) {
757 ioc
= current_io_context(GFP_ATOMIC
, q
->node
);
759 * The queue will fill after this allocation, so set
760 * it as full, and mark this process as "batching".
761 * This process will be allowed to complete a batch of
762 * requests, others will be blocked.
764 if (!blk_queue_full(q
, rw
)) {
765 ioc_set_batching(q
, ioc
);
766 blk_set_queue_full(q
, rw
);
768 if (may_queue
!= ELV_MQUEUE_MUST
769 && !ioc_batching(q
, ioc
)) {
771 * The queue is full and the allocating
772 * process is not a "batcher", and not
773 * exempted by the IO scheduler
779 blk_set_queue_congested(q
, rw
);
783 * Only allow batching queuers to allocate up to 50% over the defined
784 * limit of requests, otherwise we could have thousands of requests
785 * allocated with any setting of ->nr_requests
787 if (rl
->count
[rw
] >= (3 * q
->nr_requests
/ 2))
793 priv
= !test_bit(QUEUE_FLAG_ELVSWITCH
, &q
->queue_flags
);
797 spin_unlock_irq(q
->queue_lock
);
799 rq
= blk_alloc_request(q
, rw_flags
, priv
, gfp_mask
);
802 * Allocation failed presumably due to memory. Undo anything
803 * we might have messed up.
805 * Allocating task should really be put onto the front of the
806 * wait queue, but this is pretty rare.
808 spin_lock_irq(q
->queue_lock
);
809 freed_request(q
, rw
, priv
);
812 * in the very unlikely event that allocation failed and no
813 * requests for this direction was pending, mark us starved
814 * so that freeing of a request in the other direction will
815 * notice us. another possible fix would be to split the
816 * rq mempool into READ and WRITE
819 if (unlikely(rl
->count
[rw
] == 0))
826 * ioc may be NULL here, and ioc_batching will be false. That's
827 * OK, if the queue is under the request limit then requests need
828 * not count toward the nr_batch_requests limit. There will always
829 * be some limit enforced by BLK_BATCH_TIME.
831 if (ioc_batching(q
, ioc
))
832 ioc
->nr_batch_requests
--;
834 trace_block_getrq(q
, bio
, rw
);
840 * No available requests for this queue, unplug the device and wait for some
841 * requests to become available.
843 * Called with q->queue_lock held, and returns with it unlocked.
845 static struct request
*get_request_wait(struct request_queue
*q
, int rw_flags
,
848 const int rw
= rw_flags
& 0x01;
851 rq
= get_request(q
, rw_flags
, bio
, GFP_NOIO
);
854 struct io_context
*ioc
;
855 struct request_list
*rl
= &q
->rq
;
857 prepare_to_wait_exclusive(&rl
->wait
[rw
], &wait
,
858 TASK_UNINTERRUPTIBLE
);
860 trace_block_sleeprq(q
, bio
, rw
);
862 __generic_unplug_device(q
);
863 spin_unlock_irq(q
->queue_lock
);
867 * After sleeping, we become a "batching" process and
868 * will be able to allocate at least one request, and
869 * up to a big batch of them for a small period time.
870 * See ioc_batching, ioc_set_batching
872 ioc
= current_io_context(GFP_NOIO
, q
->node
);
873 ioc_set_batching(q
, ioc
);
875 spin_lock_irq(q
->queue_lock
);
876 finish_wait(&rl
->wait
[rw
], &wait
);
878 rq
= get_request(q
, rw_flags
, bio
, GFP_NOIO
);
884 struct request
*blk_get_request(struct request_queue
*q
, int rw
, gfp_t gfp_mask
)
888 BUG_ON(rw
!= READ
&& rw
!= WRITE
);
890 spin_lock_irq(q
->queue_lock
);
891 if (gfp_mask
& __GFP_WAIT
) {
892 rq
= get_request_wait(q
, rw
, NULL
);
894 rq
= get_request(q
, rw
, NULL
, gfp_mask
);
896 spin_unlock_irq(q
->queue_lock
);
898 /* q->queue_lock is unlocked at this point */
902 EXPORT_SYMBOL(blk_get_request
);
905 * blk_start_queueing - initiate dispatch of requests to device
906 * @q: request queue to kick into gear
908 * This is basically a helper to remove the need to know whether a queue
909 * is plugged or not if someone just wants to initiate dispatch of requests
910 * for this queue. Should be used to start queueing on a device outside
911 * of ->request_fn() context. Also see @blk_run_queue.
913 * The queue lock must be held with interrupts disabled.
915 void blk_start_queueing(struct request_queue
*q
)
917 if (!blk_queue_plugged(q
)) {
918 if (unlikely(blk_queue_stopped(q
)))
922 __generic_unplug_device(q
);
924 EXPORT_SYMBOL(blk_start_queueing
);
927 * blk_requeue_request - put a request back on queue
928 * @q: request queue where request should be inserted
929 * @rq: request to be inserted
932 * Drivers often keep queueing requests until the hardware cannot accept
933 * more, when that condition happens we need to put the request back
934 * on the queue. Must be called with queue lock held.
936 void blk_requeue_request(struct request_queue
*q
, struct request
*rq
)
938 blk_delete_timer(rq
);
939 blk_clear_rq_complete(rq
);
940 trace_block_rq_requeue(q
, rq
);
942 if (blk_rq_tagged(rq
))
943 blk_queue_end_tag(q
, rq
);
945 elv_requeue_request(q
, rq
);
947 EXPORT_SYMBOL(blk_requeue_request
);
950 * blk_insert_request - insert a special request into a request queue
951 * @q: request queue where request should be inserted
952 * @rq: request to be inserted
953 * @at_head: insert request at head or tail of queue
954 * @data: private data
957 * Many block devices need to execute commands asynchronously, so they don't
958 * block the whole kernel from preemption during request execution. This is
959 * accomplished normally by inserting aritficial requests tagged as
960 * REQ_TYPE_SPECIAL in to the corresponding request queue, and letting them
961 * be scheduled for actual execution by the request queue.
963 * We have the option of inserting the head or the tail of the queue.
964 * Typically we use the tail for new ioctls and so forth. We use the head
965 * of the queue for things like a QUEUE_FULL message from a device, or a
966 * host that is unable to accept a particular command.
968 void blk_insert_request(struct request_queue
*q
, struct request
*rq
,
969 int at_head
, void *data
)
971 int where
= at_head
? ELEVATOR_INSERT_FRONT
: ELEVATOR_INSERT_BACK
;
975 * tell I/O scheduler that this isn't a regular read/write (ie it
976 * must not attempt merges on this) and that it acts as a soft
979 rq
->cmd_type
= REQ_TYPE_SPECIAL
;
980 rq
->cmd_flags
|= REQ_SOFTBARRIER
;
984 spin_lock_irqsave(q
->queue_lock
, flags
);
987 * If command is tagged, release the tag
989 if (blk_rq_tagged(rq
))
990 blk_queue_end_tag(q
, rq
);
992 drive_stat_acct(rq
, 1);
993 __elv_add_request(q
, rq
, where
, 0);
994 blk_start_queueing(q
);
995 spin_unlock_irqrestore(q
->queue_lock
, flags
);
997 EXPORT_SYMBOL(blk_insert_request
);
1000 * add-request adds a request to the linked list.
1001 * queue lock is held and interrupts disabled, as we muck with the
1002 * request queue list.
1004 static inline void add_request(struct request_queue
*q
, struct request
*req
)
1006 drive_stat_acct(req
, 1);
1009 * elevator indicated where it wants this request to be
1010 * inserted at elevator_merge time
1012 __elv_add_request(q
, req
, ELEVATOR_INSERT_SORT
, 0);
1015 static void part_round_stats_single(int cpu
, struct hd_struct
*part
,
1018 if (now
== part
->stamp
)
1021 if (part
->in_flight
) {
1022 __part_stat_add(cpu
, part
, time_in_queue
,
1023 part
->in_flight
* (now
- part
->stamp
));
1024 __part_stat_add(cpu
, part
, io_ticks
, (now
- part
->stamp
));
1030 * part_round_stats() - Round off the performance stats on a struct disk_stats.
1031 * @cpu: cpu number for stats access
1032 * @part: target partition
1034 * The average IO queue length and utilisation statistics are maintained
1035 * by observing the current state of the queue length and the amount of
1036 * time it has been in this state for.
1038 * Normally, that accounting is done on IO completion, but that can result
1039 * in more than a second's worth of IO being accounted for within any one
1040 * second, leading to >100% utilisation. To deal with that, we call this
1041 * function to do a round-off before returning the results when reading
1042 * /proc/diskstats. This accounts immediately for all queue usage up to
1043 * the current jiffies and restarts the counters again.
1045 void part_round_stats(int cpu
, struct hd_struct
*part
)
1047 unsigned long now
= jiffies
;
1050 part_round_stats_single(cpu
, &part_to_disk(part
)->part0
, now
);
1051 part_round_stats_single(cpu
, part
, now
);
1053 EXPORT_SYMBOL_GPL(part_round_stats
);
1056 * queue lock must be held
1058 void __blk_put_request(struct request_queue
*q
, struct request
*req
)
1062 if (unlikely(--req
->ref_count
))
1065 elv_completed_request(q
, req
);
1068 * Request may not have originated from ll_rw_blk. if not,
1069 * it didn't come out of our reserved rq pools
1071 if (req
->cmd_flags
& REQ_ALLOCED
) {
1072 int rw
= rq_data_dir(req
);
1073 int priv
= req
->cmd_flags
& REQ_ELVPRIV
;
1075 BUG_ON(!list_empty(&req
->queuelist
));
1076 BUG_ON(!hlist_unhashed(&req
->hash
));
1078 blk_free_request(q
, req
);
1079 freed_request(q
, rw
, priv
);
1082 EXPORT_SYMBOL_GPL(__blk_put_request
);
1084 void blk_put_request(struct request
*req
)
1086 unsigned long flags
;
1087 struct request_queue
*q
= req
->q
;
1089 spin_lock_irqsave(q
->queue_lock
, flags
);
1090 __blk_put_request(q
, req
);
1091 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1093 EXPORT_SYMBOL(blk_put_request
);
1095 void init_request_from_bio(struct request
*req
, struct bio
*bio
)
1097 req
->cpu
= bio
->bi_comp_cpu
;
1098 req
->cmd_type
= REQ_TYPE_FS
;
1101 * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
1103 if (bio_rw_ahead(bio
))
1104 req
->cmd_flags
|= (REQ_FAILFAST_DEV
| REQ_FAILFAST_TRANSPORT
|
1105 REQ_FAILFAST_DRIVER
);
1106 if (bio_failfast_dev(bio
))
1107 req
->cmd_flags
|= REQ_FAILFAST_DEV
;
1108 if (bio_failfast_transport(bio
))
1109 req
->cmd_flags
|= REQ_FAILFAST_TRANSPORT
;
1110 if (bio_failfast_driver(bio
))
1111 req
->cmd_flags
|= REQ_FAILFAST_DRIVER
;
1114 * REQ_BARRIER implies no merging, but lets make it explicit
1116 if (unlikely(bio_discard(bio
))) {
1117 req
->cmd_flags
|= REQ_DISCARD
;
1118 if (bio_barrier(bio
))
1119 req
->cmd_flags
|= REQ_SOFTBARRIER
;
1120 req
->q
->prepare_discard_fn(req
->q
, req
);
1121 } else if (unlikely(bio_barrier(bio
)))
1122 req
->cmd_flags
|= (REQ_HARDBARRIER
| REQ_NOMERGE
);
1125 req
->cmd_flags
|= REQ_RW_SYNC
;
1126 if (bio_rw_meta(bio
))
1127 req
->cmd_flags
|= REQ_RW_META
;
1130 req
->hard_sector
= req
->sector
= bio
->bi_sector
;
1131 req
->ioprio
= bio_prio(bio
);
1132 req
->start_time
= jiffies
;
1133 blk_rq_bio_prep(req
->q
, req
, bio
);
1136 static int __make_request(struct request_queue
*q
, struct bio
*bio
)
1138 struct request
*req
;
1139 int el_ret
, nr_sectors
, barrier
, discard
, err
;
1140 const unsigned short prio
= bio_prio(bio
);
1141 const int sync
= bio_sync(bio
);
1144 nr_sectors
= bio_sectors(bio
);
1147 * low level driver can indicate that it wants pages above a
1148 * certain limit bounced to low memory (ie for highmem, or even
1149 * ISA dma in theory)
1151 blk_queue_bounce(q
, &bio
);
1153 barrier
= bio_barrier(bio
);
1154 if (unlikely(barrier
) && bio_has_data(bio
) &&
1155 (q
->next_ordered
== QUEUE_ORDERED_NONE
)) {
1160 discard
= bio_discard(bio
);
1161 if (unlikely(discard
) && !q
->prepare_discard_fn
) {
1166 spin_lock_irq(q
->queue_lock
);
1168 if (unlikely(barrier
) || elv_queue_empty(q
))
1171 el_ret
= elv_merge(q
, &req
, bio
);
1173 case ELEVATOR_BACK_MERGE
:
1174 BUG_ON(!rq_mergeable(req
));
1176 if (!ll_back_merge_fn(q
, req
, bio
))
1179 trace_block_bio_backmerge(q
, bio
);
1181 req
->biotail
->bi_next
= bio
;
1183 req
->nr_sectors
= req
->hard_nr_sectors
+= nr_sectors
;
1184 req
->ioprio
= ioprio_best(req
->ioprio
, prio
);
1185 if (!blk_rq_cpu_valid(req
))
1186 req
->cpu
= bio
->bi_comp_cpu
;
1187 drive_stat_acct(req
, 0);
1188 if (!attempt_back_merge(q
, req
))
1189 elv_merged_request(q
, req
, el_ret
);
1192 case ELEVATOR_FRONT_MERGE
:
1193 BUG_ON(!rq_mergeable(req
));
1195 if (!ll_front_merge_fn(q
, req
, bio
))
1198 trace_block_bio_frontmerge(q
, bio
);
1200 bio
->bi_next
= req
->bio
;
1204 * may not be valid. if the low level driver said
1205 * it didn't need a bounce buffer then it better
1206 * not touch req->buffer either...
1208 req
->buffer
= bio_data(bio
);
1209 req
->current_nr_sectors
= bio_cur_sectors(bio
);
1210 req
->hard_cur_sectors
= req
->current_nr_sectors
;
1211 req
->sector
= req
->hard_sector
= bio
->bi_sector
;
1212 req
->nr_sectors
= req
->hard_nr_sectors
+= nr_sectors
;
1213 req
->ioprio
= ioprio_best(req
->ioprio
, prio
);
1214 if (!blk_rq_cpu_valid(req
))
1215 req
->cpu
= bio
->bi_comp_cpu
;
1216 drive_stat_acct(req
, 0);
1217 if (!attempt_front_merge(q
, req
))
1218 elv_merged_request(q
, req
, el_ret
);
1221 /* ELV_NO_MERGE: elevator says don't/can't merge. */
1228 * This sync check and mask will be re-done in init_request_from_bio(),
1229 * but we need to set it earlier to expose the sync flag to the
1230 * rq allocator and io schedulers.
1232 rw_flags
= bio_data_dir(bio
);
1234 rw_flags
|= REQ_RW_SYNC
;
1237 * Grab a free request. This is might sleep but can not fail.
1238 * Returns with the queue unlocked.
1240 req
= get_request_wait(q
, rw_flags
, bio
);
1243 * After dropping the lock and possibly sleeping here, our request
1244 * may now be mergeable after it had proven unmergeable (above).
1245 * We don't worry about that case for efficiency. It won't happen
1246 * often, and the elevators are able to handle it.
1248 init_request_from_bio(req
, bio
);
1250 spin_lock_irq(q
->queue_lock
);
1251 if (test_bit(QUEUE_FLAG_SAME_COMP
, &q
->queue_flags
) ||
1252 bio_flagged(bio
, BIO_CPU_AFFINE
))
1253 req
->cpu
= blk_cpu_to_group(smp_processor_id());
1254 if (elv_queue_empty(q
))
1256 add_request(q
, req
);
1259 __generic_unplug_device(q
);
1260 spin_unlock_irq(q
->queue_lock
);
1264 bio_endio(bio
, err
);
1269 * If bio->bi_dev is a partition, remap the location
1271 static inline void blk_partition_remap(struct bio
*bio
)
1273 struct block_device
*bdev
= bio
->bi_bdev
;
1275 if (bio_sectors(bio
) && bdev
!= bdev
->bd_contains
) {
1276 struct hd_struct
*p
= bdev
->bd_part
;
1278 bio
->bi_sector
+= p
->start_sect
;
1279 bio
->bi_bdev
= bdev
->bd_contains
;
1281 trace_block_remap(bdev_get_queue(bio
->bi_bdev
), bio
,
1282 bdev
->bd_dev
, bio
->bi_sector
,
1283 bio
->bi_sector
- p
->start_sect
);
1287 static void handle_bad_sector(struct bio
*bio
)
1289 char b
[BDEVNAME_SIZE
];
1291 printk(KERN_INFO
"attempt to access beyond end of device\n");
1292 printk(KERN_INFO
"%s: rw=%ld, want=%Lu, limit=%Lu\n",
1293 bdevname(bio
->bi_bdev
, b
),
1295 (unsigned long long)bio
->bi_sector
+ bio_sectors(bio
),
1296 (long long)(bio
->bi_bdev
->bd_inode
->i_size
>> 9));
1298 set_bit(BIO_EOF
, &bio
->bi_flags
);
1301 #ifdef CONFIG_FAIL_MAKE_REQUEST
1303 static DECLARE_FAULT_ATTR(fail_make_request
);
1305 static int __init
setup_fail_make_request(char *str
)
1307 return setup_fault_attr(&fail_make_request
, str
);
1309 __setup("fail_make_request=", setup_fail_make_request
);
1311 static int should_fail_request(struct bio
*bio
)
1313 struct hd_struct
*part
= bio
->bi_bdev
->bd_part
;
1315 if (part_to_disk(part
)->part0
.make_it_fail
|| part
->make_it_fail
)
1316 return should_fail(&fail_make_request
, bio
->bi_size
);
1321 static int __init
fail_make_request_debugfs(void)
1323 return init_fault_attr_dentries(&fail_make_request
,
1324 "fail_make_request");
1327 late_initcall(fail_make_request_debugfs
);
1329 #else /* CONFIG_FAIL_MAKE_REQUEST */
1331 static inline int should_fail_request(struct bio
*bio
)
1336 #endif /* CONFIG_FAIL_MAKE_REQUEST */
1339 * Check whether this bio extends beyond the end of the device.
1341 static inline int bio_check_eod(struct bio
*bio
, unsigned int nr_sectors
)
1348 /* Test device or partition size, when known. */
1349 maxsector
= bio
->bi_bdev
->bd_inode
->i_size
>> 9;
1351 sector_t sector
= bio
->bi_sector
;
1353 if (maxsector
< nr_sectors
|| maxsector
- nr_sectors
< sector
) {
1355 * This may well happen - the kernel calls bread()
1356 * without checking the size of the device, e.g., when
1357 * mounting a device.
1359 handle_bad_sector(bio
);
1368 * generic_make_request - hand a buffer to its device driver for I/O
1369 * @bio: The bio describing the location in memory and on the device.
1371 * generic_make_request() is used to make I/O requests of block
1372 * devices. It is passed a &struct bio, which describes the I/O that needs
1375 * generic_make_request() does not return any status. The
1376 * success/failure status of the request, along with notification of
1377 * completion, is delivered asynchronously through the bio->bi_end_io
1378 * function described (one day) else where.
1380 * The caller of generic_make_request must make sure that bi_io_vec
1381 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1382 * set to describe the device address, and the
1383 * bi_end_io and optionally bi_private are set to describe how
1384 * completion notification should be signaled.
1386 * generic_make_request and the drivers it calls may use bi_next if this
1387 * bio happens to be merged with someone else, and may change bi_dev and
1388 * bi_sector for remaps as it sees fit. So the values of these fields
1389 * should NOT be depended on after the call to generic_make_request.
1391 static inline void __generic_make_request(struct bio
*bio
)
1393 struct request_queue
*q
;
1394 sector_t old_sector
;
1395 int ret
, nr_sectors
= bio_sectors(bio
);
1401 if (bio_check_eod(bio
, nr_sectors
))
1405 * Resolve the mapping until finished. (drivers are
1406 * still free to implement/resolve their own stacking
1407 * by explicitly returning 0)
1409 * NOTE: we don't repeat the blk_size check for each new device.
1410 * Stacking drivers are expected to know what they are doing.
1415 char b
[BDEVNAME_SIZE
];
1417 q
= bdev_get_queue(bio
->bi_bdev
);
1420 "generic_make_request: Trying to access "
1421 "nonexistent block-device %s (%Lu)\n",
1422 bdevname(bio
->bi_bdev
, b
),
1423 (long long) bio
->bi_sector
);
1425 bio_endio(bio
, err
);
1429 if (unlikely(nr_sectors
> q
->max_hw_sectors
)) {
1430 printk(KERN_ERR
"bio too big device %s (%u > %u)\n",
1431 bdevname(bio
->bi_bdev
, b
),
1437 if (unlikely(test_bit(QUEUE_FLAG_DEAD
, &q
->queue_flags
)))
1440 if (should_fail_request(bio
))
1444 * If this device has partitions, remap block n
1445 * of partition p to block n+start(p) of the disk.
1447 blk_partition_remap(bio
);
1449 if (bio_integrity_enabled(bio
) && bio_integrity_prep(bio
))
1452 if (old_sector
!= -1)
1453 trace_block_remap(q
, bio
, old_dev
, bio
->bi_sector
,
1456 trace_block_bio_queue(q
, bio
);
1458 old_sector
= bio
->bi_sector
;
1459 old_dev
= bio
->bi_bdev
->bd_dev
;
1461 if (bio_check_eod(bio
, nr_sectors
))
1463 if ((bio_empty_barrier(bio
) && !q
->prepare_flush_fn
) ||
1464 (bio_discard(bio
) && !q
->prepare_discard_fn
)) {
1469 ret
= q
->make_request_fn(q
, bio
);
1474 * We only want one ->make_request_fn to be active at a time,
1475 * else stack usage with stacked devices could be a problem.
1476 * So use current->bio_{list,tail} to keep a list of requests
1477 * submited by a make_request_fn function.
1478 * current->bio_tail is also used as a flag to say if
1479 * generic_make_request is currently active in this task or not.
1480 * If it is NULL, then no make_request is active. If it is non-NULL,
1481 * then a make_request is active, and new requests should be added
1484 void generic_make_request(struct bio
*bio
)
1486 if (current
->bio_tail
) {
1487 /* make_request is active */
1488 *(current
->bio_tail
) = bio
;
1489 bio
->bi_next
= NULL
;
1490 current
->bio_tail
= &bio
->bi_next
;
1493 /* following loop may be a bit non-obvious, and so deserves some
1495 * Before entering the loop, bio->bi_next is NULL (as all callers
1496 * ensure that) so we have a list with a single bio.
1497 * We pretend that we have just taken it off a longer list, so
1498 * we assign bio_list to the next (which is NULL) and bio_tail
1499 * to &bio_list, thus initialising the bio_list of new bios to be
1500 * added. __generic_make_request may indeed add some more bios
1501 * through a recursive call to generic_make_request. If it
1502 * did, we find a non-NULL value in bio_list and re-enter the loop
1503 * from the top. In this case we really did just take the bio
1504 * of the top of the list (no pretending) and so fixup bio_list and
1505 * bio_tail or bi_next, and call into __generic_make_request again.
1507 * The loop was structured like this to make only one call to
1508 * __generic_make_request (which is important as it is large and
1509 * inlined) and to keep the structure simple.
1511 BUG_ON(bio
->bi_next
);
1513 current
->bio_list
= bio
->bi_next
;
1514 if (bio
->bi_next
== NULL
)
1515 current
->bio_tail
= ¤t
->bio_list
;
1517 bio
->bi_next
= NULL
;
1518 __generic_make_request(bio
);
1519 bio
= current
->bio_list
;
1521 current
->bio_tail
= NULL
; /* deactivate */
1523 EXPORT_SYMBOL(generic_make_request
);
1526 * submit_bio - submit a bio to the block device layer for I/O
1527 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1528 * @bio: The &struct bio which describes the I/O
1530 * submit_bio() is very similar in purpose to generic_make_request(), and
1531 * uses that function to do most of the work. Both are fairly rough
1532 * interfaces; @bio must be presetup and ready for I/O.
1535 void submit_bio(int rw
, struct bio
*bio
)
1537 int count
= bio_sectors(bio
);
1542 * If it's a regular read/write or a barrier with data attached,
1543 * go through the normal accounting stuff before submission.
1545 if (bio_has_data(bio
)) {
1547 count_vm_events(PGPGOUT
, count
);
1549 task_io_account_read(bio
->bi_size
);
1550 count_vm_events(PGPGIN
, count
);
1553 if (unlikely(block_dump
)) {
1554 char b
[BDEVNAME_SIZE
];
1555 printk(KERN_DEBUG
"%s(%d): %s block %Lu on %s\n",
1556 current
->comm
, task_pid_nr(current
),
1557 (rw
& WRITE
) ? "WRITE" : "READ",
1558 (unsigned long long)bio
->bi_sector
,
1559 bdevname(bio
->bi_bdev
, b
));
1563 generic_make_request(bio
);
1565 EXPORT_SYMBOL(submit_bio
);
1568 * blk_rq_check_limits - Helper function to check a request for the queue limit
1570 * @rq: the request being checked
1573 * @rq may have been made based on weaker limitations of upper-level queues
1574 * in request stacking drivers, and it may violate the limitation of @q.
1575 * Since the block layer and the underlying device driver trust @rq
1576 * after it is inserted to @q, it should be checked against @q before
1577 * the insertion using this generic function.
1579 * This function should also be useful for request stacking drivers
1580 * in some cases below, so export this fuction.
1581 * Request stacking drivers like request-based dm may change the queue
1582 * limits while requests are in the queue (e.g. dm's table swapping).
1583 * Such request stacking drivers should check those requests agaist
1584 * the new queue limits again when they dispatch those requests,
1585 * although such checkings are also done against the old queue limits
1586 * when submitting requests.
1588 int blk_rq_check_limits(struct request_queue
*q
, struct request
*rq
)
1590 if (rq
->nr_sectors
> q
->max_sectors
||
1591 rq
->data_len
> q
->max_hw_sectors
<< 9) {
1592 printk(KERN_ERR
"%s: over max size limit.\n", __func__
);
1597 * queue's settings related to segment counting like q->bounce_pfn
1598 * may differ from that of other stacking queues.
1599 * Recalculate it to check the request correctly on this queue's
1602 blk_recalc_rq_segments(rq
);
1603 if (rq
->nr_phys_segments
> q
->max_phys_segments
||
1604 rq
->nr_phys_segments
> q
->max_hw_segments
) {
1605 printk(KERN_ERR
"%s: over max segments limit.\n", __func__
);
1611 EXPORT_SYMBOL_GPL(blk_rq_check_limits
);
1614 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
1615 * @q: the queue to submit the request
1616 * @rq: the request being queued
1618 int blk_insert_cloned_request(struct request_queue
*q
, struct request
*rq
)
1620 unsigned long flags
;
1622 if (blk_rq_check_limits(q
, rq
))
1625 #ifdef CONFIG_FAIL_MAKE_REQUEST
1626 if (rq
->rq_disk
&& rq
->rq_disk
->part0
.make_it_fail
&&
1627 should_fail(&fail_make_request
, blk_rq_bytes(rq
)))
1631 spin_lock_irqsave(q
->queue_lock
, flags
);
1634 * Submitting request must be dequeued before calling this function
1635 * because it will be linked to another request_queue
1637 BUG_ON(blk_queued_rq(rq
));
1639 drive_stat_acct(rq
, 1);
1640 __elv_add_request(q
, rq
, ELEVATOR_INSERT_BACK
, 0);
1642 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1646 EXPORT_SYMBOL_GPL(blk_insert_cloned_request
);
1649 * blkdev_dequeue_request - dequeue request and start timeout timer
1650 * @req: request to dequeue
1652 * Dequeue @req and start timeout timer on it. This hands off the
1653 * request to the driver.
1655 * Block internal functions which don't want to start timer should
1656 * call elv_dequeue_request().
1658 void blkdev_dequeue_request(struct request
*req
)
1660 elv_dequeue_request(req
->q
, req
);
1663 * We are now handing the request to the hardware, add the
1668 EXPORT_SYMBOL(blkdev_dequeue_request
);
1671 * __end_that_request_first - end I/O on a request
1672 * @req: the request being processed
1673 * @error: %0 for success, < %0 for error
1674 * @nr_bytes: number of bytes to complete
1677 * Ends I/O on a number of bytes attached to @req, and sets it up
1678 * for the next range of segments (if any) in the cluster.
1681 * %0 - we are done with this request, call end_that_request_last()
1682 * %1 - still buffers pending for this request
1684 static int __end_that_request_first(struct request
*req
, int error
,
1687 int total_bytes
, bio_nbytes
, next_idx
= 0;
1690 trace_block_rq_complete(req
->q
, req
);
1693 * for a REQ_TYPE_BLOCK_PC request, we want to carry any eventual
1694 * sense key with us all the way through
1696 if (!blk_pc_request(req
))
1699 if (error
&& (blk_fs_request(req
) && !(req
->cmd_flags
& REQ_QUIET
))) {
1700 printk(KERN_ERR
"end_request: I/O error, dev %s, sector %llu\n",
1701 req
->rq_disk
? req
->rq_disk
->disk_name
: "?",
1702 (unsigned long long)req
->sector
);
1705 if (blk_fs_request(req
) && req
->rq_disk
) {
1706 const int rw
= rq_data_dir(req
);
1707 struct hd_struct
*part
;
1710 cpu
= part_stat_lock();
1711 part
= disk_map_sector_rcu(req
->rq_disk
, req
->sector
);
1712 part_stat_add(cpu
, part
, sectors
[rw
], nr_bytes
>> 9);
1716 total_bytes
= bio_nbytes
= 0;
1717 while ((bio
= req
->bio
) != NULL
) {
1721 * For an empty barrier request, the low level driver must
1722 * store a potential error location in ->sector. We pass
1723 * that back up in ->bi_sector.
1725 if (blk_empty_barrier(req
))
1726 bio
->bi_sector
= req
->sector
;
1728 if (nr_bytes
>= bio
->bi_size
) {
1729 req
->bio
= bio
->bi_next
;
1730 nbytes
= bio
->bi_size
;
1731 req_bio_endio(req
, bio
, nbytes
, error
);
1735 int idx
= bio
->bi_idx
+ next_idx
;
1737 if (unlikely(bio
->bi_idx
>= bio
->bi_vcnt
)) {
1738 blk_dump_rq_flags(req
, "__end_that");
1739 printk(KERN_ERR
"%s: bio idx %d >= vcnt %d\n",
1740 __func__
, bio
->bi_idx
, bio
->bi_vcnt
);
1744 nbytes
= bio_iovec_idx(bio
, idx
)->bv_len
;
1745 BIO_BUG_ON(nbytes
> bio
->bi_size
);
1748 * not a complete bvec done
1750 if (unlikely(nbytes
> nr_bytes
)) {
1751 bio_nbytes
+= nr_bytes
;
1752 total_bytes
+= nr_bytes
;
1757 * advance to the next vector
1760 bio_nbytes
+= nbytes
;
1763 total_bytes
+= nbytes
;
1769 * end more in this run, or just return 'not-done'
1771 if (unlikely(nr_bytes
<= 0))
1783 * if the request wasn't completed, update state
1786 req_bio_endio(req
, bio
, bio_nbytes
, error
);
1787 bio
->bi_idx
+= next_idx
;
1788 bio_iovec(bio
)->bv_offset
+= nr_bytes
;
1789 bio_iovec(bio
)->bv_len
-= nr_bytes
;
1792 blk_recalc_rq_sectors(req
, total_bytes
>> 9);
1793 blk_recalc_rq_segments(req
);
1798 * queue lock must be held
1800 static void end_that_request_last(struct request
*req
, int error
)
1802 struct gendisk
*disk
= req
->rq_disk
;
1804 if (blk_rq_tagged(req
))
1805 blk_queue_end_tag(req
->q
, req
);
1807 if (blk_queued_rq(req
))
1808 elv_dequeue_request(req
->q
, req
);
1810 if (unlikely(laptop_mode
) && blk_fs_request(req
))
1811 laptop_io_completion();
1813 blk_delete_timer(req
);
1816 * Account IO completion. bar_rq isn't accounted as a normal
1817 * IO on queueing nor completion. Accounting the containing
1818 * request is enough.
1820 if (disk
&& blk_fs_request(req
) && req
!= &req
->q
->bar_rq
) {
1821 unsigned long duration
= jiffies
- req
->start_time
;
1822 const int rw
= rq_data_dir(req
);
1823 struct hd_struct
*part
;
1826 cpu
= part_stat_lock();
1827 part
= disk_map_sector_rcu(disk
, req
->sector
);
1829 part_stat_inc(cpu
, part
, ios
[rw
]);
1830 part_stat_add(cpu
, part
, ticks
[rw
], duration
);
1831 part_round_stats(cpu
, part
);
1832 part_dec_in_flight(part
);
1838 req
->end_io(req
, error
);
1840 if (blk_bidi_rq(req
))
1841 __blk_put_request(req
->next_rq
->q
, req
->next_rq
);
1843 __blk_put_request(req
->q
, req
);
1848 * blk_rq_bytes - Returns bytes left to complete in the entire request
1849 * @rq: the request being processed
1851 unsigned int blk_rq_bytes(struct request
*rq
)
1853 if (blk_fs_request(rq
))
1854 return rq
->hard_nr_sectors
<< 9;
1856 return rq
->data_len
;
1858 EXPORT_SYMBOL_GPL(blk_rq_bytes
);
1861 * blk_rq_cur_bytes - Returns bytes left to complete in the current segment
1862 * @rq: the request being processed
1864 unsigned int blk_rq_cur_bytes(struct request
*rq
)
1866 if (blk_fs_request(rq
))
1867 return rq
->current_nr_sectors
<< 9;
1870 return rq
->bio
->bi_size
;
1872 return rq
->data_len
;
1874 EXPORT_SYMBOL_GPL(blk_rq_cur_bytes
);
1877 * end_request - end I/O on the current segment of the request
1878 * @req: the request being processed
1879 * @uptodate: error value or %0/%1 uptodate flag
1882 * Ends I/O on the current segment of a request. If that is the only
1883 * remaining segment, the request is also completed and freed.
1885 * This is a remnant of how older block drivers handled I/O completions.
1886 * Modern drivers typically end I/O on the full request in one go, unless
1887 * they have a residual value to account for. For that case this function
1888 * isn't really useful, unless the residual just happens to be the
1889 * full current segment. In other words, don't use this function in new
1890 * code. Use blk_end_request() or __blk_end_request() to end a request.
1892 void end_request(struct request
*req
, int uptodate
)
1897 error
= uptodate
? uptodate
: -EIO
;
1899 __blk_end_request(req
, error
, req
->hard_cur_sectors
<< 9);
1901 EXPORT_SYMBOL(end_request
);
1903 static int end_that_request_data(struct request
*rq
, int error
,
1904 unsigned int nr_bytes
, unsigned int bidi_bytes
)
1907 if (__end_that_request_first(rq
, error
, nr_bytes
))
1910 /* Bidi request must be completed as a whole */
1911 if (blk_bidi_rq(rq
) &&
1912 __end_that_request_first(rq
->next_rq
, error
, bidi_bytes
))
1920 * blk_end_io - Generic end_io function to complete a request.
1921 * @rq: the request being processed
1922 * @error: %0 for success, < %0 for error
1923 * @nr_bytes: number of bytes to complete @rq
1924 * @bidi_bytes: number of bytes to complete @rq->next_rq
1925 * @drv_callback: function called between completion of bios in the request
1926 * and completion of the request.
1927 * If the callback returns non %0, this helper returns without
1928 * completion of the request.
1931 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
1932 * If @rq has leftover, sets it up for the next range of segments.
1935 * %0 - we are done with this request
1936 * %1 - this request is not freed yet, it still has pending buffers.
1938 static int blk_end_io(struct request
*rq
, int error
, unsigned int nr_bytes
,
1939 unsigned int bidi_bytes
,
1940 int (drv_callback
)(struct request
*))
1942 struct request_queue
*q
= rq
->q
;
1943 unsigned long flags
= 0UL;
1945 if (end_that_request_data(rq
, error
, nr_bytes
, bidi_bytes
))
1948 /* Special feature for tricky drivers */
1949 if (drv_callback
&& drv_callback(rq
))
1952 add_disk_randomness(rq
->rq_disk
);
1954 spin_lock_irqsave(q
->queue_lock
, flags
);
1955 end_that_request_last(rq
, error
);
1956 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1962 * blk_end_request - Helper function for drivers to complete the request.
1963 * @rq: the request being processed
1964 * @error: %0 for success, < %0 for error
1965 * @nr_bytes: number of bytes to complete
1968 * Ends I/O on a number of bytes attached to @rq.
1969 * If @rq has leftover, sets it up for the next range of segments.
1972 * %0 - we are done with this request
1973 * %1 - still buffers pending for this request
1975 int blk_end_request(struct request
*rq
, int error
, unsigned int nr_bytes
)
1977 return blk_end_io(rq
, error
, nr_bytes
, 0, NULL
);
1979 EXPORT_SYMBOL_GPL(blk_end_request
);
1982 * __blk_end_request - Helper function for drivers to complete the request.
1983 * @rq: the request being processed
1984 * @error: %0 for success, < %0 for error
1985 * @nr_bytes: number of bytes to complete
1988 * Must be called with queue lock held unlike blk_end_request().
1991 * %0 - we are done with this request
1992 * %1 - still buffers pending for this request
1994 int __blk_end_request(struct request
*rq
, int error
, unsigned int nr_bytes
)
1996 if (rq
->bio
&& __end_that_request_first(rq
, error
, nr_bytes
))
1999 add_disk_randomness(rq
->rq_disk
);
2001 end_that_request_last(rq
, error
);
2005 EXPORT_SYMBOL_GPL(__blk_end_request
);
2008 * blk_end_bidi_request - Helper function for drivers to complete bidi request.
2009 * @rq: the bidi request being processed
2010 * @error: %0 for success, < %0 for error
2011 * @nr_bytes: number of bytes to complete @rq
2012 * @bidi_bytes: number of bytes to complete @rq->next_rq
2015 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
2018 * %0 - we are done with this request
2019 * %1 - still buffers pending for this request
2021 int blk_end_bidi_request(struct request
*rq
, int error
, unsigned int nr_bytes
,
2022 unsigned int bidi_bytes
)
2024 return blk_end_io(rq
, error
, nr_bytes
, bidi_bytes
, NULL
);
2026 EXPORT_SYMBOL_GPL(blk_end_bidi_request
);
2029 * blk_update_request - Special helper function for request stacking drivers
2030 * @rq: the request being processed
2031 * @error: %0 for success, < %0 for error
2032 * @nr_bytes: number of bytes to complete @rq
2035 * Ends I/O on a number of bytes attached to @rq, but doesn't complete
2036 * the request structure even if @rq doesn't have leftover.
2037 * If @rq has leftover, sets it up for the next range of segments.
2039 * This special helper function is only for request stacking drivers
2040 * (e.g. request-based dm) so that they can handle partial completion.
2041 * Actual device drivers should use blk_end_request instead.
2043 void blk_update_request(struct request
*rq
, int error
, unsigned int nr_bytes
)
2045 if (!end_that_request_data(rq
, error
, nr_bytes
, 0)) {
2047 * These members are not updated in end_that_request_data()
2048 * when all bios are completed.
2049 * Update them so that the request stacking driver can find
2050 * how many bytes remain in the request later.
2052 rq
->nr_sectors
= rq
->hard_nr_sectors
= 0;
2053 rq
->current_nr_sectors
= rq
->hard_cur_sectors
= 0;
2056 EXPORT_SYMBOL_GPL(blk_update_request
);
2059 * blk_end_request_callback - Special helper function for tricky drivers
2060 * @rq: the request being processed
2061 * @error: %0 for success, < %0 for error
2062 * @nr_bytes: number of bytes to complete
2063 * @drv_callback: function called between completion of bios in the request
2064 * and completion of the request.
2065 * If the callback returns non %0, this helper returns without
2066 * completion of the request.
2069 * Ends I/O on a number of bytes attached to @rq.
2070 * If @rq has leftover, sets it up for the next range of segments.
2072 * This special helper function is used only for existing tricky drivers.
2073 * (e.g. cdrom_newpc_intr() of ide-cd)
2074 * This interface will be removed when such drivers are rewritten.
2075 * Don't use this interface in other places anymore.
2078 * %0 - we are done with this request
2079 * %1 - this request is not freed yet.
2080 * this request still has pending buffers or
2081 * the driver doesn't want to finish this request yet.
2083 int blk_end_request_callback(struct request
*rq
, int error
,
2084 unsigned int nr_bytes
,
2085 int (drv_callback
)(struct request
*))
2087 return blk_end_io(rq
, error
, nr_bytes
, 0, drv_callback
);
2089 EXPORT_SYMBOL_GPL(blk_end_request_callback
);
2091 void blk_rq_bio_prep(struct request_queue
*q
, struct request
*rq
,
2094 /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw, and
2095 we want BIO_RW_AHEAD (bit 1) to imply REQ_FAILFAST (bit 1). */
2096 rq
->cmd_flags
|= (bio
->bi_rw
& 3);
2098 if (bio_has_data(bio
)) {
2099 rq
->nr_phys_segments
= bio_phys_segments(q
, bio
);
2100 rq
->buffer
= bio_data(bio
);
2102 rq
->current_nr_sectors
= bio_cur_sectors(bio
);
2103 rq
->hard_cur_sectors
= rq
->current_nr_sectors
;
2104 rq
->hard_nr_sectors
= rq
->nr_sectors
= bio_sectors(bio
);
2105 rq
->data_len
= bio
->bi_size
;
2107 rq
->bio
= rq
->biotail
= bio
;
2110 rq
->rq_disk
= bio
->bi_bdev
->bd_disk
;
2114 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
2115 * @q : the queue of the device being checked
2118 * Check if underlying low-level drivers of a device are busy.
2119 * If the drivers want to export their busy state, they must set own
2120 * exporting function using blk_queue_lld_busy() first.
2122 * Basically, this function is used only by request stacking drivers
2123 * to stop dispatching requests to underlying devices when underlying
2124 * devices are busy. This behavior helps more I/O merging on the queue
2125 * of the request stacking driver and prevents I/O throughput regression
2126 * on burst I/O load.
2129 * 0 - Not busy (The request stacking driver should dispatch request)
2130 * 1 - Busy (The request stacking driver should stop dispatching request)
2132 int blk_lld_busy(struct request_queue
*q
)
2135 return q
->lld_busy_fn(q
);
2139 EXPORT_SYMBOL_GPL(blk_lld_busy
);
2141 int kblockd_schedule_work(struct request_queue
*q
, struct work_struct
*work
)
2143 return queue_work(kblockd_workqueue
, work
);
2145 EXPORT_SYMBOL(kblockd_schedule_work
);
2147 void kblockd_flush_work(struct work_struct
*work
)
2149 cancel_work_sync(work
);
2151 EXPORT_SYMBOL(kblockd_flush_work
);
2153 int __init
blk_dev_init(void)
2155 kblockd_workqueue
= create_workqueue("kblockd");
2156 if (!kblockd_workqueue
)
2157 panic("Failed to create kblockd\n");
2159 request_cachep
= kmem_cache_create("blkdev_requests",
2160 sizeof(struct request
), 0, SLAB_PANIC
, NULL
);
2162 blk_requestq_cachep
= kmem_cache_create("blkdev_queue",
2163 sizeof(struct request_queue
), 0, SLAB_PANIC
, NULL
);