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_do_io_stat(rq
))
74 cpu
= part_stat_lock();
75 part
= disk_map_sector_rcu(rq
->rq_disk
, blk_rq_pos(rq
));
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
= (sector_t
) -1;
131 INIT_HLIST_NODE(&rq
->hash
);
132 RB_CLEAR_NODE(&rq
->rb_node
);
134 rq
->cmd_len
= BLK_MAX_CDB
;
137 rq
->start_time
= jiffies
;
139 EXPORT_SYMBOL(blk_rq_init
);
141 static void req_bio_endio(struct request
*rq
, struct bio
*bio
,
142 unsigned int nbytes
, int error
)
144 struct request_queue
*q
= rq
->q
;
146 if (&q
->bar_rq
!= rq
) {
148 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
149 else if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
152 if (unlikely(nbytes
> bio
->bi_size
)) {
153 printk(KERN_ERR
"%s: want %u bytes done, %u left\n",
154 __func__
, nbytes
, bio
->bi_size
);
155 nbytes
= bio
->bi_size
;
158 if (unlikely(rq
->cmd_flags
& REQ_QUIET
))
159 set_bit(BIO_QUIET
, &bio
->bi_flags
);
161 bio
->bi_size
-= nbytes
;
162 bio
->bi_sector
+= (nbytes
>> 9);
164 if (bio_integrity(bio
))
165 bio_integrity_advance(bio
, nbytes
);
167 if (bio
->bi_size
== 0)
168 bio_endio(bio
, error
);
172 * Okay, this is the barrier request in progress, just
175 if (error
&& !q
->orderr
)
180 void blk_dump_rq_flags(struct request
*rq
, char *msg
)
184 printk(KERN_INFO
"%s: dev %s: type=%x, flags=%x\n", msg
,
185 rq
->rq_disk
? rq
->rq_disk
->disk_name
: "?", rq
->cmd_type
,
188 printk(KERN_INFO
" sector %llu, nr/cnr %u/%u\n",
189 (unsigned long long)blk_rq_pos(rq
),
190 blk_rq_sectors(rq
), blk_rq_cur_sectors(rq
));
191 printk(KERN_INFO
" bio %p, biotail %p, buffer %p, len %u\n",
192 rq
->bio
, rq
->biotail
, rq
->buffer
, blk_rq_bytes(rq
));
194 if (blk_pc_request(rq
)) {
195 printk(KERN_INFO
" cdb: ");
196 for (bit
= 0; bit
< BLK_MAX_CDB
; bit
++)
197 printk("%02x ", rq
->cmd
[bit
]);
201 EXPORT_SYMBOL(blk_dump_rq_flags
);
204 * "plug" the device if there are no outstanding requests: this will
205 * force the transfer to start only after we have put all the requests
208 * This is called with interrupts off and no requests on the queue and
209 * with the queue lock held.
211 void blk_plug_device(struct request_queue
*q
)
213 WARN_ON(!irqs_disabled());
216 * don't plug a stopped queue, it must be paired with blk_start_queue()
217 * which will restart the queueing
219 if (blk_queue_stopped(q
))
222 if (!queue_flag_test_and_set(QUEUE_FLAG_PLUGGED
, q
)) {
223 mod_timer(&q
->unplug_timer
, jiffies
+ q
->unplug_delay
);
227 EXPORT_SYMBOL(blk_plug_device
);
230 * blk_plug_device_unlocked - plug a device without queue lock held
231 * @q: The &struct request_queue to plug
234 * Like @blk_plug_device(), but grabs the queue lock and disables
237 void blk_plug_device_unlocked(struct request_queue
*q
)
241 spin_lock_irqsave(q
->queue_lock
, flags
);
243 spin_unlock_irqrestore(q
->queue_lock
, flags
);
245 EXPORT_SYMBOL(blk_plug_device_unlocked
);
248 * remove the queue from the plugged list, if present. called with
249 * queue lock held and interrupts disabled.
251 int blk_remove_plug(struct request_queue
*q
)
253 WARN_ON(!irqs_disabled());
255 if (!queue_flag_test_and_clear(QUEUE_FLAG_PLUGGED
, q
))
258 del_timer(&q
->unplug_timer
);
261 EXPORT_SYMBOL(blk_remove_plug
);
264 * remove the plug and let it rip..
266 void __generic_unplug_device(struct request_queue
*q
)
268 if (unlikely(blk_queue_stopped(q
)))
270 if (!blk_remove_plug(q
) && !blk_queue_nonrot(q
))
277 * generic_unplug_device - fire a request queue
278 * @q: The &struct request_queue in question
281 * Linux uses plugging to build bigger requests queues before letting
282 * the device have at them. If a queue is plugged, the I/O scheduler
283 * is still adding and merging requests on the queue. Once the queue
284 * gets unplugged, the request_fn defined for the queue is invoked and
287 void generic_unplug_device(struct request_queue
*q
)
289 if (blk_queue_plugged(q
)) {
290 spin_lock_irq(q
->queue_lock
);
291 __generic_unplug_device(q
);
292 spin_unlock_irq(q
->queue_lock
);
295 EXPORT_SYMBOL(generic_unplug_device
);
297 static void blk_backing_dev_unplug(struct backing_dev_info
*bdi
,
300 struct request_queue
*q
= bdi
->unplug_io_data
;
305 void blk_unplug_work(struct work_struct
*work
)
307 struct request_queue
*q
=
308 container_of(work
, struct request_queue
, unplug_work
);
310 trace_block_unplug_io(q
);
314 void blk_unplug_timeout(unsigned long data
)
316 struct request_queue
*q
= (struct request_queue
*)data
;
318 trace_block_unplug_timer(q
);
319 kblockd_schedule_work(q
, &q
->unplug_work
);
322 void blk_unplug(struct request_queue
*q
)
325 * devices don't necessarily have an ->unplug_fn defined
328 trace_block_unplug_io(q
);
332 EXPORT_SYMBOL(blk_unplug
);
335 * blk_start_queue - restart a previously stopped queue
336 * @q: The &struct request_queue in question
339 * blk_start_queue() will clear the stop flag on the queue, and call
340 * the request_fn for the queue if it was in a stopped state when
341 * entered. Also see blk_stop_queue(). Queue lock must be held.
343 void blk_start_queue(struct request_queue
*q
)
345 WARN_ON(!irqs_disabled());
347 queue_flag_clear(QUEUE_FLAG_STOPPED
, q
);
350 EXPORT_SYMBOL(blk_start_queue
);
353 * blk_stop_queue - stop a queue
354 * @q: The &struct request_queue in question
357 * The Linux block layer assumes that a block driver will consume all
358 * entries on the request queue when the request_fn strategy is called.
359 * Often this will not happen, because of hardware limitations (queue
360 * depth settings). If a device driver gets a 'queue full' response,
361 * or if it simply chooses not to queue more I/O at one point, it can
362 * call this function to prevent the request_fn from being called until
363 * the driver has signalled it's ready to go again. This happens by calling
364 * blk_start_queue() to restart queue operations. Queue lock must be held.
366 void blk_stop_queue(struct request_queue
*q
)
369 queue_flag_set(QUEUE_FLAG_STOPPED
, q
);
371 EXPORT_SYMBOL(blk_stop_queue
);
374 * blk_sync_queue - cancel any pending callbacks on a queue
378 * The block layer may perform asynchronous callback activity
379 * on a queue, such as calling the unplug function after a timeout.
380 * A block device may call blk_sync_queue to ensure that any
381 * such activity is cancelled, thus allowing it to release resources
382 * that the callbacks might use. The caller must already have made sure
383 * that its ->make_request_fn will not re-add plugging prior to calling
387 void blk_sync_queue(struct request_queue
*q
)
389 del_timer_sync(&q
->unplug_timer
);
390 del_timer_sync(&q
->timeout
);
391 cancel_work_sync(&q
->unplug_work
);
393 EXPORT_SYMBOL(blk_sync_queue
);
396 * __blk_run_queue - run a single device queue
397 * @q: The queue to run
400 * See @blk_run_queue. This variant must be called with the queue lock
401 * held and interrupts disabled.
404 void __blk_run_queue(struct request_queue
*q
)
408 if (unlikely(blk_queue_stopped(q
)))
411 if (elv_queue_empty(q
))
415 * Only recurse once to avoid overrunning the stack, let the unplug
416 * handling reinvoke the handler shortly if we already got there.
418 if (!queue_flag_test_and_set(QUEUE_FLAG_REENTER
, q
)) {
420 queue_flag_clear(QUEUE_FLAG_REENTER
, q
);
422 queue_flag_set(QUEUE_FLAG_PLUGGED
, q
);
423 kblockd_schedule_work(q
, &q
->unplug_work
);
426 EXPORT_SYMBOL(__blk_run_queue
);
429 * blk_run_queue - run a single device queue
430 * @q: The queue to run
433 * Invoke request handling on this queue, if it has pending work to do.
434 * May be used to restart queueing when a request has completed.
436 void blk_run_queue(struct request_queue
*q
)
440 spin_lock_irqsave(q
->queue_lock
, flags
);
442 spin_unlock_irqrestore(q
->queue_lock
, flags
);
444 EXPORT_SYMBOL(blk_run_queue
);
446 void blk_put_queue(struct request_queue
*q
)
448 kobject_put(&q
->kobj
);
451 void blk_cleanup_queue(struct request_queue
*q
)
454 * We know we have process context here, so we can be a little
455 * cautious and ensure that pending block actions on this device
456 * are done before moving on. Going into this function, we should
457 * not have processes doing IO to this device.
461 mutex_lock(&q
->sysfs_lock
);
462 queue_flag_set_unlocked(QUEUE_FLAG_DEAD
, q
);
463 mutex_unlock(&q
->sysfs_lock
);
466 elevator_exit(q
->elevator
);
470 EXPORT_SYMBOL(blk_cleanup_queue
);
472 static int blk_init_free_list(struct request_queue
*q
)
474 struct request_list
*rl
= &q
->rq
;
476 rl
->count
[BLK_RW_SYNC
] = rl
->count
[BLK_RW_ASYNC
] = 0;
477 rl
->starved
[BLK_RW_SYNC
] = rl
->starved
[BLK_RW_ASYNC
] = 0;
479 init_waitqueue_head(&rl
->wait
[BLK_RW_SYNC
]);
480 init_waitqueue_head(&rl
->wait
[BLK_RW_ASYNC
]);
482 rl
->rq_pool
= mempool_create_node(BLKDEV_MIN_RQ
, mempool_alloc_slab
,
483 mempool_free_slab
, request_cachep
, q
->node
);
491 struct request_queue
*blk_alloc_queue(gfp_t gfp_mask
)
493 return blk_alloc_queue_node(gfp_mask
, -1);
495 EXPORT_SYMBOL(blk_alloc_queue
);
497 struct request_queue
*blk_alloc_queue_node(gfp_t gfp_mask
, int node_id
)
499 struct request_queue
*q
;
502 q
= kmem_cache_alloc_node(blk_requestq_cachep
,
503 gfp_mask
| __GFP_ZERO
, node_id
);
507 q
->backing_dev_info
.unplug_io_fn
= blk_backing_dev_unplug
;
508 q
->backing_dev_info
.unplug_io_data
= q
;
509 err
= bdi_init(&q
->backing_dev_info
);
511 kmem_cache_free(blk_requestq_cachep
, q
);
515 init_timer(&q
->unplug_timer
);
516 setup_timer(&q
->timeout
, blk_rq_timed_out_timer
, (unsigned long) q
);
517 INIT_LIST_HEAD(&q
->timeout_list
);
518 INIT_WORK(&q
->unplug_work
, blk_unplug_work
);
520 kobject_init(&q
->kobj
, &blk_queue_ktype
);
522 mutex_init(&q
->sysfs_lock
);
523 spin_lock_init(&q
->__queue_lock
);
527 EXPORT_SYMBOL(blk_alloc_queue_node
);
530 * blk_init_queue - prepare a request queue for use with a block device
531 * @rfn: The function to be called to process requests that have been
532 * placed on the queue.
533 * @lock: Request queue spin lock
536 * If a block device wishes to use the standard request handling procedures,
537 * which sorts requests and coalesces adjacent requests, then it must
538 * call blk_init_queue(). The function @rfn will be called when there
539 * are requests on the queue that need to be processed. If the device
540 * supports plugging, then @rfn may not be called immediately when requests
541 * are available on the queue, but may be called at some time later instead.
542 * Plugged queues are generally unplugged when a buffer belonging to one
543 * of the requests on the queue is needed, or due to memory pressure.
545 * @rfn is not required, or even expected, to remove all requests off the
546 * queue, but only as many as it can handle at a time. If it does leave
547 * requests on the queue, it is responsible for arranging that the requests
548 * get dealt with eventually.
550 * The queue spin lock must be held while manipulating the requests on the
551 * request queue; this lock will be taken also from interrupt context, so irq
552 * disabling is needed for it.
554 * Function returns a pointer to the initialized request queue, or %NULL if
558 * blk_init_queue() must be paired with a blk_cleanup_queue() call
559 * when the block device is deactivated (such as at module unload).
562 struct request_queue
*blk_init_queue(request_fn_proc
*rfn
, spinlock_t
*lock
)
564 return blk_init_queue_node(rfn
, lock
, -1);
566 EXPORT_SYMBOL(blk_init_queue
);
568 struct request_queue
*
569 blk_init_queue_node(request_fn_proc
*rfn
, spinlock_t
*lock
, int node_id
)
571 struct request_queue
*q
= blk_alloc_queue_node(GFP_KERNEL
, node_id
);
577 if (blk_init_free_list(q
)) {
578 kmem_cache_free(blk_requestq_cachep
, q
);
583 * if caller didn't supply a lock, they get per-queue locking with
587 lock
= &q
->__queue_lock
;
590 q
->prep_rq_fn
= NULL
;
591 q
->unplug_fn
= generic_unplug_device
;
592 q
->queue_flags
= QUEUE_FLAG_DEFAULT
;
593 q
->queue_lock
= lock
;
596 * This also sets hw/phys segments, boundary and size
598 blk_queue_make_request(q
, __make_request
);
600 q
->sg_reserved_size
= INT_MAX
;
602 blk_set_cmd_filter_defaults(&q
->cmd_filter
);
607 if (!elevator_init(q
, NULL
)) {
608 blk_queue_congestion_threshold(q
);
615 EXPORT_SYMBOL(blk_init_queue_node
);
617 int blk_get_queue(struct request_queue
*q
)
619 if (likely(!test_bit(QUEUE_FLAG_DEAD
, &q
->queue_flags
))) {
620 kobject_get(&q
->kobj
);
627 static inline void blk_free_request(struct request_queue
*q
, struct request
*rq
)
629 if (rq
->cmd_flags
& REQ_ELVPRIV
)
630 elv_put_request(q
, rq
);
631 mempool_free(rq
, q
->rq
.rq_pool
);
634 static struct request
*
635 blk_alloc_request(struct request_queue
*q
, int flags
, int priv
, gfp_t gfp_mask
)
637 struct request
*rq
= mempool_alloc(q
->rq
.rq_pool
, gfp_mask
);
644 rq
->cmd_flags
= flags
| REQ_ALLOCED
;
647 if (unlikely(elv_set_request(q
, rq
, gfp_mask
))) {
648 mempool_free(rq
, q
->rq
.rq_pool
);
651 rq
->cmd_flags
|= REQ_ELVPRIV
;
658 * ioc_batching returns true if the ioc is a valid batching request and
659 * should be given priority access to a request.
661 static inline int ioc_batching(struct request_queue
*q
, struct io_context
*ioc
)
667 * Make sure the process is able to allocate at least 1 request
668 * even if the batch times out, otherwise we could theoretically
671 return ioc
->nr_batch_requests
== q
->nr_batching
||
672 (ioc
->nr_batch_requests
> 0
673 && time_before(jiffies
, ioc
->last_waited
+ BLK_BATCH_TIME
));
677 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
678 * will cause the process to be a "batcher" on all queues in the system. This
679 * is the behaviour we want though - once it gets a wakeup it should be given
682 static void ioc_set_batching(struct request_queue
*q
, struct io_context
*ioc
)
684 if (!ioc
|| ioc_batching(q
, ioc
))
687 ioc
->nr_batch_requests
= q
->nr_batching
;
688 ioc
->last_waited
= jiffies
;
691 static void __freed_request(struct request_queue
*q
, int sync
)
693 struct request_list
*rl
= &q
->rq
;
695 if (rl
->count
[sync
] < queue_congestion_off_threshold(q
))
696 blk_clear_queue_congested(q
, sync
);
698 if (rl
->count
[sync
] + 1 <= q
->nr_requests
) {
699 if (waitqueue_active(&rl
->wait
[sync
]))
700 wake_up(&rl
->wait
[sync
]);
702 blk_clear_queue_full(q
, sync
);
707 * A request has just been released. Account for it, update the full and
708 * congestion status, wake up any waiters. Called under q->queue_lock.
710 static void freed_request(struct request_queue
*q
, int sync
, int priv
)
712 struct request_list
*rl
= &q
->rq
;
718 __freed_request(q
, sync
);
720 if (unlikely(rl
->starved
[sync
^ 1]))
721 __freed_request(q
, sync
^ 1);
725 * Get a free request, queue_lock must be held.
726 * Returns NULL on failure, with queue_lock held.
727 * Returns !NULL on success, with queue_lock *not held*.
729 static struct request
*get_request(struct request_queue
*q
, int rw_flags
,
730 struct bio
*bio
, gfp_t gfp_mask
)
732 struct request
*rq
= NULL
;
733 struct request_list
*rl
= &q
->rq
;
734 struct io_context
*ioc
= NULL
;
735 const bool is_sync
= rw_is_sync(rw_flags
) != 0;
738 may_queue
= elv_may_queue(q
, rw_flags
);
739 if (may_queue
== ELV_MQUEUE_NO
)
742 if (rl
->count
[is_sync
]+1 >= queue_congestion_on_threshold(q
)) {
743 if (rl
->count
[is_sync
]+1 >= q
->nr_requests
) {
744 ioc
= current_io_context(GFP_ATOMIC
, q
->node
);
746 * The queue will fill after this allocation, so set
747 * it as full, and mark this process as "batching".
748 * This process will be allowed to complete a batch of
749 * requests, others will be blocked.
751 if (!blk_queue_full(q
, is_sync
)) {
752 ioc_set_batching(q
, ioc
);
753 blk_set_queue_full(q
, is_sync
);
755 if (may_queue
!= ELV_MQUEUE_MUST
756 && !ioc_batching(q
, ioc
)) {
758 * The queue is full and the allocating
759 * process is not a "batcher", and not
760 * exempted by the IO scheduler
766 blk_set_queue_congested(q
, is_sync
);
770 * Only allow batching queuers to allocate up to 50% over the defined
771 * limit of requests, otherwise we could have thousands of requests
772 * allocated with any setting of ->nr_requests
774 if (rl
->count
[is_sync
] >= (3 * q
->nr_requests
/ 2))
777 rl
->count
[is_sync
]++;
778 rl
->starved
[is_sync
] = 0;
780 priv
= !test_bit(QUEUE_FLAG_ELVSWITCH
, &q
->queue_flags
);
784 if (blk_queue_io_stat(q
))
785 rw_flags
|= REQ_IO_STAT
;
786 spin_unlock_irq(q
->queue_lock
);
788 rq
= blk_alloc_request(q
, rw_flags
, priv
, gfp_mask
);
791 * Allocation failed presumably due to memory. Undo anything
792 * we might have messed up.
794 * Allocating task should really be put onto the front of the
795 * wait queue, but this is pretty rare.
797 spin_lock_irq(q
->queue_lock
);
798 freed_request(q
, is_sync
, priv
);
801 * in the very unlikely event that allocation failed and no
802 * requests for this direction was pending, mark us starved
803 * so that freeing of a request in the other direction will
804 * notice us. another possible fix would be to split the
805 * rq mempool into READ and WRITE
808 if (unlikely(rl
->count
[is_sync
] == 0))
809 rl
->starved
[is_sync
] = 1;
815 * ioc may be NULL here, and ioc_batching will be false. That's
816 * OK, if the queue is under the request limit then requests need
817 * not count toward the nr_batch_requests limit. There will always
818 * be some limit enforced by BLK_BATCH_TIME.
820 if (ioc_batching(q
, ioc
))
821 ioc
->nr_batch_requests
--;
823 trace_block_getrq(q
, bio
, rw_flags
& 1);
829 * No available requests for this queue, unplug the device and wait for some
830 * requests to become available.
832 * Called with q->queue_lock held, and returns with it unlocked.
834 static struct request
*get_request_wait(struct request_queue
*q
, int rw_flags
,
837 const bool is_sync
= rw_is_sync(rw_flags
) != 0;
840 rq
= get_request(q
, rw_flags
, bio
, GFP_NOIO
);
843 struct io_context
*ioc
;
844 struct request_list
*rl
= &q
->rq
;
846 prepare_to_wait_exclusive(&rl
->wait
[is_sync
], &wait
,
847 TASK_UNINTERRUPTIBLE
);
849 trace_block_sleeprq(q
, bio
, rw_flags
& 1);
851 __generic_unplug_device(q
);
852 spin_unlock_irq(q
->queue_lock
);
856 * After sleeping, we become a "batching" process and
857 * will be able to allocate at least one request, and
858 * up to a big batch of them for a small period time.
859 * See ioc_batching, ioc_set_batching
861 ioc
= current_io_context(GFP_NOIO
, q
->node
);
862 ioc_set_batching(q
, ioc
);
864 spin_lock_irq(q
->queue_lock
);
865 finish_wait(&rl
->wait
[is_sync
], &wait
);
867 rq
= get_request(q
, rw_flags
, bio
, GFP_NOIO
);
873 struct request
*blk_get_request(struct request_queue
*q
, int rw
, gfp_t gfp_mask
)
877 BUG_ON(rw
!= READ
&& rw
!= WRITE
);
879 spin_lock_irq(q
->queue_lock
);
880 if (gfp_mask
& __GFP_WAIT
) {
881 rq
= get_request_wait(q
, rw
, NULL
);
883 rq
= get_request(q
, rw
, NULL
, gfp_mask
);
885 spin_unlock_irq(q
->queue_lock
);
887 /* q->queue_lock is unlocked at this point */
891 EXPORT_SYMBOL(blk_get_request
);
894 * blk_requeue_request - put a request back on queue
895 * @q: request queue where request should be inserted
896 * @rq: request to be inserted
899 * Drivers often keep queueing requests until the hardware cannot accept
900 * more, when that condition happens we need to put the request back
901 * on the queue. Must be called with queue lock held.
903 void blk_requeue_request(struct request_queue
*q
, struct request
*rq
)
905 BUG_ON(blk_queued_rq(rq
));
907 blk_delete_timer(rq
);
908 blk_clear_rq_complete(rq
);
909 trace_block_rq_requeue(q
, rq
);
911 if (blk_rq_tagged(rq
))
912 blk_queue_end_tag(q
, rq
);
914 elv_requeue_request(q
, rq
);
916 EXPORT_SYMBOL(blk_requeue_request
);
919 * blk_insert_request - insert a special request into a request queue
920 * @q: request queue where request should be inserted
921 * @rq: request to be inserted
922 * @at_head: insert request at head or tail of queue
923 * @data: private data
926 * Many block devices need to execute commands asynchronously, so they don't
927 * block the whole kernel from preemption during request execution. This is
928 * accomplished normally by inserting aritficial requests tagged as
929 * REQ_TYPE_SPECIAL in to the corresponding request queue, and letting them
930 * be scheduled for actual execution by the request queue.
932 * We have the option of inserting the head or the tail of the queue.
933 * Typically we use the tail for new ioctls and so forth. We use the head
934 * of the queue for things like a QUEUE_FULL message from a device, or a
935 * host that is unable to accept a particular command.
937 void blk_insert_request(struct request_queue
*q
, struct request
*rq
,
938 int at_head
, void *data
)
940 int where
= at_head
? ELEVATOR_INSERT_FRONT
: ELEVATOR_INSERT_BACK
;
944 * tell I/O scheduler that this isn't a regular read/write (ie it
945 * must not attempt merges on this) and that it acts as a soft
948 rq
->cmd_type
= REQ_TYPE_SPECIAL
;
952 spin_lock_irqsave(q
->queue_lock
, flags
);
955 * If command is tagged, release the tag
957 if (blk_rq_tagged(rq
))
958 blk_queue_end_tag(q
, rq
);
960 drive_stat_acct(rq
, 1);
961 __elv_add_request(q
, rq
, where
, 0);
963 spin_unlock_irqrestore(q
->queue_lock
, flags
);
965 EXPORT_SYMBOL(blk_insert_request
);
968 * add-request adds a request to the linked list.
969 * queue lock is held and interrupts disabled, as we muck with the
970 * request queue list.
972 static inline void add_request(struct request_queue
*q
, struct request
*req
)
974 drive_stat_acct(req
, 1);
977 * elevator indicated where it wants this request to be
978 * inserted at elevator_merge time
980 __elv_add_request(q
, req
, ELEVATOR_INSERT_SORT
, 0);
983 static void part_round_stats_single(int cpu
, struct hd_struct
*part
,
986 if (now
== part
->stamp
)
989 if (part
->in_flight
) {
990 __part_stat_add(cpu
, part
, time_in_queue
,
991 part
->in_flight
* (now
- part
->stamp
));
992 __part_stat_add(cpu
, part
, io_ticks
, (now
- part
->stamp
));
998 * part_round_stats() - Round off the performance stats on a struct disk_stats.
999 * @cpu: cpu number for stats access
1000 * @part: target partition
1002 * The average IO queue length and utilisation statistics are maintained
1003 * by observing the current state of the queue length and the amount of
1004 * time it has been in this state for.
1006 * Normally, that accounting is done on IO completion, but that can result
1007 * in more than a second's worth of IO being accounted for within any one
1008 * second, leading to >100% utilisation. To deal with that, we call this
1009 * function to do a round-off before returning the results when reading
1010 * /proc/diskstats. This accounts immediately for all queue usage up to
1011 * the current jiffies and restarts the counters again.
1013 void part_round_stats(int cpu
, struct hd_struct
*part
)
1015 unsigned long now
= jiffies
;
1018 part_round_stats_single(cpu
, &part_to_disk(part
)->part0
, now
);
1019 part_round_stats_single(cpu
, part
, now
);
1021 EXPORT_SYMBOL_GPL(part_round_stats
);
1024 * queue lock must be held
1026 void __blk_put_request(struct request_queue
*q
, struct request
*req
)
1030 if (unlikely(--req
->ref_count
))
1033 elv_completed_request(q
, req
);
1035 /* this is a bio leak */
1036 WARN_ON(req
->bio
!= NULL
);
1039 * Request may not have originated from ll_rw_blk. if not,
1040 * it didn't come out of our reserved rq pools
1042 if (req
->cmd_flags
& REQ_ALLOCED
) {
1043 int is_sync
= rq_is_sync(req
) != 0;
1044 int priv
= req
->cmd_flags
& REQ_ELVPRIV
;
1046 BUG_ON(!list_empty(&req
->queuelist
));
1047 BUG_ON(!hlist_unhashed(&req
->hash
));
1049 blk_free_request(q
, req
);
1050 freed_request(q
, is_sync
, priv
);
1053 EXPORT_SYMBOL_GPL(__blk_put_request
);
1055 void blk_put_request(struct request
*req
)
1057 unsigned long flags
;
1058 struct request_queue
*q
= req
->q
;
1060 spin_lock_irqsave(q
->queue_lock
, flags
);
1061 __blk_put_request(q
, req
);
1062 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1064 EXPORT_SYMBOL(blk_put_request
);
1066 void init_request_from_bio(struct request
*req
, struct bio
*bio
)
1068 req
->cpu
= bio
->bi_comp_cpu
;
1069 req
->cmd_type
= REQ_TYPE_FS
;
1072 * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
1074 if (bio_rw_ahead(bio
))
1075 req
->cmd_flags
|= (REQ_FAILFAST_DEV
| REQ_FAILFAST_TRANSPORT
|
1076 REQ_FAILFAST_DRIVER
);
1077 if (bio_failfast_dev(bio
))
1078 req
->cmd_flags
|= REQ_FAILFAST_DEV
;
1079 if (bio_failfast_transport(bio
))
1080 req
->cmd_flags
|= REQ_FAILFAST_TRANSPORT
;
1081 if (bio_failfast_driver(bio
))
1082 req
->cmd_flags
|= REQ_FAILFAST_DRIVER
;
1084 if (unlikely(bio_discard(bio
))) {
1085 req
->cmd_flags
|= REQ_DISCARD
;
1086 if (bio_barrier(bio
))
1087 req
->cmd_flags
|= REQ_SOFTBARRIER
;
1088 req
->q
->prepare_discard_fn(req
->q
, req
);
1089 } else if (unlikely(bio_barrier(bio
)))
1090 req
->cmd_flags
|= REQ_HARDBARRIER
;
1093 req
->cmd_flags
|= REQ_RW_SYNC
;
1094 if (bio_rw_meta(bio
))
1095 req
->cmd_flags
|= REQ_RW_META
;
1096 if (bio_noidle(bio
))
1097 req
->cmd_flags
|= REQ_NOIDLE
;
1100 req
->__sector
= bio
->bi_sector
;
1101 req
->ioprio
= bio_prio(bio
);
1102 blk_rq_bio_prep(req
->q
, req
, bio
);
1106 * Only disabling plugging for non-rotational devices if it does tagging
1107 * as well, otherwise we do need the proper merging
1109 static inline bool queue_should_plug(struct request_queue
*q
)
1111 return !(blk_queue_nonrot(q
) && blk_queue_tagged(q
));
1114 static int __make_request(struct request_queue
*q
, struct bio
*bio
)
1116 struct request
*req
;
1118 unsigned int bytes
= bio
->bi_size
;
1119 const unsigned short prio
= bio_prio(bio
);
1120 const int sync
= bio_sync(bio
);
1121 const int unplug
= bio_unplug(bio
);
1125 * low level driver can indicate that it wants pages above a
1126 * certain limit bounced to low memory (ie for highmem, or even
1127 * ISA dma in theory)
1129 blk_queue_bounce(q
, &bio
);
1131 spin_lock_irq(q
->queue_lock
);
1133 if (unlikely(bio_barrier(bio
)) || elv_queue_empty(q
))
1136 el_ret
= elv_merge(q
, &req
, bio
);
1138 case ELEVATOR_BACK_MERGE
:
1139 BUG_ON(!rq_mergeable(req
));
1141 if (!ll_back_merge_fn(q
, req
, bio
))
1144 trace_block_bio_backmerge(q
, bio
);
1146 req
->biotail
->bi_next
= bio
;
1148 req
->__data_len
+= bytes
;
1149 req
->ioprio
= ioprio_best(req
->ioprio
, prio
);
1150 if (!blk_rq_cpu_valid(req
))
1151 req
->cpu
= bio
->bi_comp_cpu
;
1152 drive_stat_acct(req
, 0);
1153 if (!attempt_back_merge(q
, req
))
1154 elv_merged_request(q
, req
, el_ret
);
1157 case ELEVATOR_FRONT_MERGE
:
1158 BUG_ON(!rq_mergeable(req
));
1160 if (!ll_front_merge_fn(q
, req
, bio
))
1163 trace_block_bio_frontmerge(q
, bio
);
1165 bio
->bi_next
= req
->bio
;
1169 * may not be valid. if the low level driver said
1170 * it didn't need a bounce buffer then it better
1171 * not touch req->buffer either...
1173 req
->buffer
= bio_data(bio
);
1174 req
->__sector
= bio
->bi_sector
;
1175 req
->__data_len
+= bytes
;
1176 req
->ioprio
= ioprio_best(req
->ioprio
, prio
);
1177 if (!blk_rq_cpu_valid(req
))
1178 req
->cpu
= bio
->bi_comp_cpu
;
1179 drive_stat_acct(req
, 0);
1180 if (!attempt_front_merge(q
, req
))
1181 elv_merged_request(q
, req
, el_ret
);
1184 /* ELV_NO_MERGE: elevator says don't/can't merge. */
1191 * This sync check and mask will be re-done in init_request_from_bio(),
1192 * but we need to set it earlier to expose the sync flag to the
1193 * rq allocator and io schedulers.
1195 rw_flags
= bio_data_dir(bio
);
1197 rw_flags
|= REQ_RW_SYNC
;
1200 * Grab a free request. This is might sleep but can not fail.
1201 * Returns with the queue unlocked.
1203 req
= get_request_wait(q
, rw_flags
, bio
);
1206 * After dropping the lock and possibly sleeping here, our request
1207 * may now be mergeable after it had proven unmergeable (above).
1208 * We don't worry about that case for efficiency. It won't happen
1209 * often, and the elevators are able to handle it.
1211 init_request_from_bio(req
, bio
);
1213 spin_lock_irq(q
->queue_lock
);
1214 if (test_bit(QUEUE_FLAG_SAME_COMP
, &q
->queue_flags
) ||
1215 bio_flagged(bio
, BIO_CPU_AFFINE
))
1216 req
->cpu
= blk_cpu_to_group(smp_processor_id());
1217 if (queue_should_plug(q
) && elv_queue_empty(q
))
1219 add_request(q
, req
);
1221 if (unplug
|| !queue_should_plug(q
))
1222 __generic_unplug_device(q
);
1223 spin_unlock_irq(q
->queue_lock
);
1228 * If bio->bi_dev is a partition, remap the location
1230 static inline void blk_partition_remap(struct bio
*bio
)
1232 struct block_device
*bdev
= bio
->bi_bdev
;
1234 if (bio_sectors(bio
) && bdev
!= bdev
->bd_contains
) {
1235 struct hd_struct
*p
= bdev
->bd_part
;
1237 bio
->bi_sector
+= p
->start_sect
;
1238 bio
->bi_bdev
= bdev
->bd_contains
;
1240 trace_block_remap(bdev_get_queue(bio
->bi_bdev
), bio
,
1241 bdev
->bd_dev
, bio
->bi_sector
,
1242 bio
->bi_sector
- p
->start_sect
);
1246 static void handle_bad_sector(struct bio
*bio
)
1248 char b
[BDEVNAME_SIZE
];
1250 printk(KERN_INFO
"attempt to access beyond end of device\n");
1251 printk(KERN_INFO
"%s: rw=%ld, want=%Lu, limit=%Lu\n",
1252 bdevname(bio
->bi_bdev
, b
),
1254 (unsigned long long)bio
->bi_sector
+ bio_sectors(bio
),
1255 (long long)(bio
->bi_bdev
->bd_inode
->i_size
>> 9));
1257 set_bit(BIO_EOF
, &bio
->bi_flags
);
1260 #ifdef CONFIG_FAIL_MAKE_REQUEST
1262 static DECLARE_FAULT_ATTR(fail_make_request
);
1264 static int __init
setup_fail_make_request(char *str
)
1266 return setup_fault_attr(&fail_make_request
, str
);
1268 __setup("fail_make_request=", setup_fail_make_request
);
1270 static int should_fail_request(struct bio
*bio
)
1272 struct hd_struct
*part
= bio
->bi_bdev
->bd_part
;
1274 if (part_to_disk(part
)->part0
.make_it_fail
|| part
->make_it_fail
)
1275 return should_fail(&fail_make_request
, bio
->bi_size
);
1280 static int __init
fail_make_request_debugfs(void)
1282 return init_fault_attr_dentries(&fail_make_request
,
1283 "fail_make_request");
1286 late_initcall(fail_make_request_debugfs
);
1288 #else /* CONFIG_FAIL_MAKE_REQUEST */
1290 static inline int should_fail_request(struct bio
*bio
)
1295 #endif /* CONFIG_FAIL_MAKE_REQUEST */
1298 * Check whether this bio extends beyond the end of the device.
1300 static inline int bio_check_eod(struct bio
*bio
, unsigned int nr_sectors
)
1307 /* Test device or partition size, when known. */
1308 maxsector
= bio
->bi_bdev
->bd_inode
->i_size
>> 9;
1310 sector_t sector
= bio
->bi_sector
;
1312 if (maxsector
< nr_sectors
|| maxsector
- nr_sectors
< sector
) {
1314 * This may well happen - the kernel calls bread()
1315 * without checking the size of the device, e.g., when
1316 * mounting a device.
1318 handle_bad_sector(bio
);
1327 * generic_make_request - hand a buffer to its device driver for I/O
1328 * @bio: The bio describing the location in memory and on the device.
1330 * generic_make_request() is used to make I/O requests of block
1331 * devices. It is passed a &struct bio, which describes the I/O that needs
1334 * generic_make_request() does not return any status. The
1335 * success/failure status of the request, along with notification of
1336 * completion, is delivered asynchronously through the bio->bi_end_io
1337 * function described (one day) else where.
1339 * The caller of generic_make_request must make sure that bi_io_vec
1340 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1341 * set to describe the device address, and the
1342 * bi_end_io and optionally bi_private are set to describe how
1343 * completion notification should be signaled.
1345 * generic_make_request and the drivers it calls may use bi_next if this
1346 * bio happens to be merged with someone else, and may change bi_dev and
1347 * bi_sector for remaps as it sees fit. So the values of these fields
1348 * should NOT be depended on after the call to generic_make_request.
1350 static inline void __generic_make_request(struct bio
*bio
)
1352 struct request_queue
*q
;
1353 sector_t old_sector
;
1354 int ret
, nr_sectors
= bio_sectors(bio
);
1360 if (bio_check_eod(bio
, nr_sectors
))
1364 * Resolve the mapping until finished. (drivers are
1365 * still free to implement/resolve their own stacking
1366 * by explicitly returning 0)
1368 * NOTE: we don't repeat the blk_size check for each new device.
1369 * Stacking drivers are expected to know what they are doing.
1374 char b
[BDEVNAME_SIZE
];
1376 q
= bdev_get_queue(bio
->bi_bdev
);
1379 "generic_make_request: Trying to access "
1380 "nonexistent block-device %s (%Lu)\n",
1381 bdevname(bio
->bi_bdev
, b
),
1382 (long long) bio
->bi_sector
);
1386 if (unlikely(nr_sectors
> q
->max_hw_sectors
)) {
1387 printk(KERN_ERR
"bio too big device %s (%u > %u)\n",
1388 bdevname(bio
->bi_bdev
, b
),
1394 if (unlikely(test_bit(QUEUE_FLAG_DEAD
, &q
->queue_flags
)))
1397 if (should_fail_request(bio
))
1401 * If this device has partitions, remap block n
1402 * of partition p to block n+start(p) of the disk.
1404 blk_partition_remap(bio
);
1406 if (bio_integrity_enabled(bio
) && bio_integrity_prep(bio
))
1409 if (old_sector
!= -1)
1410 trace_block_remap(q
, bio
, old_dev
, bio
->bi_sector
,
1413 trace_block_bio_queue(q
, bio
);
1415 old_sector
= bio
->bi_sector
;
1416 old_dev
= bio
->bi_bdev
->bd_dev
;
1418 if (bio_check_eod(bio
, nr_sectors
))
1421 if (bio_discard(bio
) && !q
->prepare_discard_fn
) {
1425 if (bio_barrier(bio
) && bio_has_data(bio
) &&
1426 (q
->next_ordered
== QUEUE_ORDERED_NONE
)) {
1431 ret
= q
->make_request_fn(q
, bio
);
1437 bio_endio(bio
, err
);
1441 * We only want one ->make_request_fn to be active at a time,
1442 * else stack usage with stacked devices could be a problem.
1443 * So use current->bio_{list,tail} to keep a list of requests
1444 * submited by a make_request_fn function.
1445 * current->bio_tail is also used as a flag to say if
1446 * generic_make_request is currently active in this task or not.
1447 * If it is NULL, then no make_request is active. If it is non-NULL,
1448 * then a make_request is active, and new requests should be added
1451 void generic_make_request(struct bio
*bio
)
1453 if (current
->bio_tail
) {
1454 /* make_request is active */
1455 *(current
->bio_tail
) = bio
;
1456 bio
->bi_next
= NULL
;
1457 current
->bio_tail
= &bio
->bi_next
;
1460 /* following loop may be a bit non-obvious, and so deserves some
1462 * Before entering the loop, bio->bi_next is NULL (as all callers
1463 * ensure that) so we have a list with a single bio.
1464 * We pretend that we have just taken it off a longer list, so
1465 * we assign bio_list to the next (which is NULL) and bio_tail
1466 * to &bio_list, thus initialising the bio_list of new bios to be
1467 * added. __generic_make_request may indeed add some more bios
1468 * through a recursive call to generic_make_request. If it
1469 * did, we find a non-NULL value in bio_list and re-enter the loop
1470 * from the top. In this case we really did just take the bio
1471 * of the top of the list (no pretending) and so fixup bio_list and
1472 * bio_tail or bi_next, and call into __generic_make_request again.
1474 * The loop was structured like this to make only one call to
1475 * __generic_make_request (which is important as it is large and
1476 * inlined) and to keep the structure simple.
1478 BUG_ON(bio
->bi_next
);
1480 current
->bio_list
= bio
->bi_next
;
1481 if (bio
->bi_next
== NULL
)
1482 current
->bio_tail
= ¤t
->bio_list
;
1484 bio
->bi_next
= NULL
;
1485 __generic_make_request(bio
);
1486 bio
= current
->bio_list
;
1488 current
->bio_tail
= NULL
; /* deactivate */
1490 EXPORT_SYMBOL(generic_make_request
);
1493 * submit_bio - submit a bio to the block device layer for I/O
1494 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1495 * @bio: The &struct bio which describes the I/O
1497 * submit_bio() is very similar in purpose to generic_make_request(), and
1498 * uses that function to do most of the work. Both are fairly rough
1499 * interfaces; @bio must be presetup and ready for I/O.
1502 void submit_bio(int rw
, struct bio
*bio
)
1504 int count
= bio_sectors(bio
);
1509 * If it's a regular read/write or a barrier with data attached,
1510 * go through the normal accounting stuff before submission.
1512 if (bio_has_data(bio
)) {
1514 count_vm_events(PGPGOUT
, count
);
1516 task_io_account_read(bio
->bi_size
);
1517 count_vm_events(PGPGIN
, count
);
1520 if (unlikely(block_dump
)) {
1521 char b
[BDEVNAME_SIZE
];
1522 printk(KERN_DEBUG
"%s(%d): %s block %Lu on %s\n",
1523 current
->comm
, task_pid_nr(current
),
1524 (rw
& WRITE
) ? "WRITE" : "READ",
1525 (unsigned long long)bio
->bi_sector
,
1526 bdevname(bio
->bi_bdev
, b
));
1530 generic_make_request(bio
);
1532 EXPORT_SYMBOL(submit_bio
);
1535 * blk_rq_check_limits - Helper function to check a request for the queue limit
1537 * @rq: the request being checked
1540 * @rq may have been made based on weaker limitations of upper-level queues
1541 * in request stacking drivers, and it may violate the limitation of @q.
1542 * Since the block layer and the underlying device driver trust @rq
1543 * after it is inserted to @q, it should be checked against @q before
1544 * the insertion using this generic function.
1546 * This function should also be useful for request stacking drivers
1547 * in some cases below, so export this fuction.
1548 * Request stacking drivers like request-based dm may change the queue
1549 * limits while requests are in the queue (e.g. dm's table swapping).
1550 * Such request stacking drivers should check those requests agaist
1551 * the new queue limits again when they dispatch those requests,
1552 * although such checkings are also done against the old queue limits
1553 * when submitting requests.
1555 int blk_rq_check_limits(struct request_queue
*q
, struct request
*rq
)
1557 if (blk_rq_sectors(rq
) > q
->max_sectors
||
1558 blk_rq_bytes(rq
) > q
->max_hw_sectors
<< 9) {
1559 printk(KERN_ERR
"%s: over max size limit.\n", __func__
);
1564 * queue's settings related to segment counting like q->bounce_pfn
1565 * may differ from that of other stacking queues.
1566 * Recalculate it to check the request correctly on this queue's
1569 blk_recalc_rq_segments(rq
);
1570 if (rq
->nr_phys_segments
> q
->max_phys_segments
||
1571 rq
->nr_phys_segments
> q
->max_hw_segments
) {
1572 printk(KERN_ERR
"%s: over max segments limit.\n", __func__
);
1578 EXPORT_SYMBOL_GPL(blk_rq_check_limits
);
1581 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
1582 * @q: the queue to submit the request
1583 * @rq: the request being queued
1585 int blk_insert_cloned_request(struct request_queue
*q
, struct request
*rq
)
1587 unsigned long flags
;
1589 if (blk_rq_check_limits(q
, rq
))
1592 #ifdef CONFIG_FAIL_MAKE_REQUEST
1593 if (rq
->rq_disk
&& rq
->rq_disk
->part0
.make_it_fail
&&
1594 should_fail(&fail_make_request
, blk_rq_bytes(rq
)))
1598 spin_lock_irqsave(q
->queue_lock
, flags
);
1601 * Submitting request must be dequeued before calling this function
1602 * because it will be linked to another request_queue
1604 BUG_ON(blk_queued_rq(rq
));
1606 drive_stat_acct(rq
, 1);
1607 __elv_add_request(q
, rq
, ELEVATOR_INSERT_BACK
, 0);
1609 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1613 EXPORT_SYMBOL_GPL(blk_insert_cloned_request
);
1615 static void blk_account_io_completion(struct request
*req
, unsigned int bytes
)
1617 if (blk_do_io_stat(req
)) {
1618 const int rw
= rq_data_dir(req
);
1619 struct hd_struct
*part
;
1622 cpu
= part_stat_lock();
1623 part
= disk_map_sector_rcu(req
->rq_disk
, blk_rq_pos(req
));
1624 part_stat_add(cpu
, part
, sectors
[rw
], bytes
>> 9);
1629 static void blk_account_io_done(struct request
*req
)
1632 * Account IO completion. bar_rq isn't accounted as a normal
1633 * IO on queueing nor completion. Accounting the containing
1634 * request is enough.
1636 if (blk_do_io_stat(req
) && req
!= &req
->q
->bar_rq
) {
1637 unsigned long duration
= jiffies
- req
->start_time
;
1638 const int rw
= rq_data_dir(req
);
1639 struct hd_struct
*part
;
1642 cpu
= part_stat_lock();
1643 part
= disk_map_sector_rcu(req
->rq_disk
, blk_rq_pos(req
));
1645 part_stat_inc(cpu
, part
, ios
[rw
]);
1646 part_stat_add(cpu
, part
, ticks
[rw
], duration
);
1647 part_round_stats(cpu
, part
);
1648 part_dec_in_flight(part
);
1655 * blk_peek_request - peek at the top of a request queue
1656 * @q: request queue to peek at
1659 * Return the request at the top of @q. The returned request
1660 * should be started using blk_start_request() before LLD starts
1664 * Pointer to the request at the top of @q if available. Null
1668 * queue_lock must be held.
1670 struct request
*blk_peek_request(struct request_queue
*q
)
1675 while ((rq
= __elv_next_request(q
)) != NULL
) {
1676 if (!(rq
->cmd_flags
& REQ_STARTED
)) {
1678 * This is the first time the device driver
1679 * sees this request (possibly after
1680 * requeueing). Notify IO scheduler.
1682 if (blk_sorted_rq(rq
))
1683 elv_activate_rq(q
, rq
);
1686 * just mark as started even if we don't start
1687 * it, a request that has been delayed should
1688 * not be passed by new incoming requests
1690 rq
->cmd_flags
|= REQ_STARTED
;
1691 trace_block_rq_issue(q
, rq
);
1694 if (!q
->boundary_rq
|| q
->boundary_rq
== rq
) {
1695 q
->end_sector
= rq_end_sector(rq
);
1696 q
->boundary_rq
= NULL
;
1699 if (rq
->cmd_flags
& REQ_DONTPREP
)
1702 if (q
->dma_drain_size
&& blk_rq_bytes(rq
)) {
1704 * make sure space for the drain appears we
1705 * know we can do this because max_hw_segments
1706 * has been adjusted to be one fewer than the
1709 rq
->nr_phys_segments
++;
1715 ret
= q
->prep_rq_fn(q
, rq
);
1716 if (ret
== BLKPREP_OK
) {
1718 } else if (ret
== BLKPREP_DEFER
) {
1720 * the request may have been (partially) prepped.
1721 * we need to keep this request in the front to
1722 * avoid resource deadlock. REQ_STARTED will
1723 * prevent other fs requests from passing this one.
1725 if (q
->dma_drain_size
&& blk_rq_bytes(rq
) &&
1726 !(rq
->cmd_flags
& REQ_DONTPREP
)) {
1728 * remove the space for the drain we added
1729 * so that we don't add it again
1731 --rq
->nr_phys_segments
;
1736 } else if (ret
== BLKPREP_KILL
) {
1737 rq
->cmd_flags
|= REQ_QUIET
;
1738 __blk_end_request_all(rq
, -EIO
);
1740 printk(KERN_ERR
"%s: bad return=%d\n", __func__
, ret
);
1747 EXPORT_SYMBOL(blk_peek_request
);
1749 void blk_dequeue_request(struct request
*rq
)
1751 struct request_queue
*q
= rq
->q
;
1753 BUG_ON(list_empty(&rq
->queuelist
));
1754 BUG_ON(ELV_ON_HASH(rq
));
1756 list_del_init(&rq
->queuelist
);
1759 * the time frame between a request being removed from the lists
1760 * and to it is freed is accounted as io that is in progress at
1763 if (blk_account_rq(rq
))
1768 * blk_start_request - start request processing on the driver
1769 * @req: request to dequeue
1772 * Dequeue @req and start timeout timer on it. This hands off the
1773 * request to the driver.
1775 * Block internal functions which don't want to start timer should
1776 * call blk_dequeue_request().
1779 * queue_lock must be held.
1781 void blk_start_request(struct request
*req
)
1783 blk_dequeue_request(req
);
1786 * We are now handing the request to the hardware, initialize
1787 * resid_len to full count and add the timeout handler.
1789 req
->resid_len
= blk_rq_bytes(req
);
1792 EXPORT_SYMBOL(blk_start_request
);
1795 * blk_fetch_request - fetch a request from a request queue
1796 * @q: request queue to fetch a request from
1799 * Return the request at the top of @q. The request is started on
1800 * return and LLD can start processing it immediately.
1803 * Pointer to the request at the top of @q if available. Null
1807 * queue_lock must be held.
1809 struct request
*blk_fetch_request(struct request_queue
*q
)
1813 rq
= blk_peek_request(q
);
1815 blk_start_request(rq
);
1818 EXPORT_SYMBOL(blk_fetch_request
);
1821 * blk_update_request - Special helper function for request stacking drivers
1822 * @rq: the request being processed
1823 * @error: %0 for success, < %0 for error
1824 * @nr_bytes: number of bytes to complete @rq
1827 * Ends I/O on a number of bytes attached to @rq, but doesn't complete
1828 * the request structure even if @rq doesn't have leftover.
1829 * If @rq has leftover, sets it up for the next range of segments.
1831 * This special helper function is only for request stacking drivers
1832 * (e.g. request-based dm) so that they can handle partial completion.
1833 * Actual device drivers should use blk_end_request instead.
1835 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
1836 * %false return from this function.
1839 * %false - this request doesn't have any more data
1840 * %true - this request has more data
1842 bool blk_update_request(struct request
*req
, int error
, unsigned int nr_bytes
)
1844 int total_bytes
, bio_nbytes
, next_idx
= 0;
1850 trace_block_rq_complete(req
->q
, req
);
1853 * For fs requests, rq is just carrier of independent bio's
1854 * and each partial completion should be handled separately.
1855 * Reset per-request error on each partial completion.
1857 * TODO: tj: This is too subtle. It would be better to let
1858 * low level drivers do what they see fit.
1860 if (blk_fs_request(req
))
1863 if (error
&& (blk_fs_request(req
) && !(req
->cmd_flags
& REQ_QUIET
))) {
1864 printk(KERN_ERR
"end_request: I/O error, dev %s, sector %llu\n",
1865 req
->rq_disk
? req
->rq_disk
->disk_name
: "?",
1866 (unsigned long long)blk_rq_pos(req
));
1869 blk_account_io_completion(req
, nr_bytes
);
1871 total_bytes
= bio_nbytes
= 0;
1872 while ((bio
= req
->bio
) != NULL
) {
1875 if (nr_bytes
>= bio
->bi_size
) {
1876 req
->bio
= bio
->bi_next
;
1877 nbytes
= bio
->bi_size
;
1878 req_bio_endio(req
, bio
, nbytes
, error
);
1882 int idx
= bio
->bi_idx
+ next_idx
;
1884 if (unlikely(bio
->bi_idx
>= bio
->bi_vcnt
)) {
1885 blk_dump_rq_flags(req
, "__end_that");
1886 printk(KERN_ERR
"%s: bio idx %d >= vcnt %d\n",
1887 __func__
, bio
->bi_idx
, bio
->bi_vcnt
);
1891 nbytes
= bio_iovec_idx(bio
, idx
)->bv_len
;
1892 BIO_BUG_ON(nbytes
> bio
->bi_size
);
1895 * not a complete bvec done
1897 if (unlikely(nbytes
> nr_bytes
)) {
1898 bio_nbytes
+= nr_bytes
;
1899 total_bytes
+= nr_bytes
;
1904 * advance to the next vector
1907 bio_nbytes
+= nbytes
;
1910 total_bytes
+= nbytes
;
1916 * end more in this run, or just return 'not-done'
1918 if (unlikely(nr_bytes
<= 0))
1928 * Reset counters so that the request stacking driver
1929 * can find how many bytes remain in the request
1932 req
->__data_len
= 0;
1937 * if the request wasn't completed, update state
1940 req_bio_endio(req
, bio
, bio_nbytes
, error
);
1941 bio
->bi_idx
+= next_idx
;
1942 bio_iovec(bio
)->bv_offset
+= nr_bytes
;
1943 bio_iovec(bio
)->bv_len
-= nr_bytes
;
1946 req
->__data_len
-= total_bytes
;
1947 req
->buffer
= bio_data(req
->bio
);
1949 /* update sector only for requests with clear definition of sector */
1950 if (blk_fs_request(req
) || blk_discard_rq(req
))
1951 req
->__sector
+= total_bytes
>> 9;
1954 * If total number of sectors is less than the first segment
1955 * size, something has gone terribly wrong.
1957 if (blk_rq_bytes(req
) < blk_rq_cur_bytes(req
)) {
1958 printk(KERN_ERR
"blk: request botched\n");
1959 req
->__data_len
= blk_rq_cur_bytes(req
);
1962 /* recalculate the number of segments */
1963 blk_recalc_rq_segments(req
);
1967 EXPORT_SYMBOL_GPL(blk_update_request
);
1969 static bool blk_update_bidi_request(struct request
*rq
, int error
,
1970 unsigned int nr_bytes
,
1971 unsigned int bidi_bytes
)
1973 if (blk_update_request(rq
, error
, nr_bytes
))
1976 /* Bidi request must be completed as a whole */
1977 if (unlikely(blk_bidi_rq(rq
)) &&
1978 blk_update_request(rq
->next_rq
, error
, bidi_bytes
))
1981 add_disk_randomness(rq
->rq_disk
);
1987 * queue lock must be held
1989 static void blk_finish_request(struct request
*req
, int error
)
1991 BUG_ON(blk_queued_rq(req
));
1993 if (blk_rq_tagged(req
))
1994 blk_queue_end_tag(req
->q
, req
);
1996 if (unlikely(laptop_mode
) && blk_fs_request(req
))
1997 laptop_io_completion();
1999 blk_delete_timer(req
);
2001 blk_account_io_done(req
);
2004 req
->end_io(req
, error
);
2006 if (blk_bidi_rq(req
))
2007 __blk_put_request(req
->next_rq
->q
, req
->next_rq
);
2009 __blk_put_request(req
->q
, req
);
2014 * blk_end_bidi_request - Complete a bidi request
2015 * @rq: the request to complete
2016 * @error: %0 for success, < %0 for error
2017 * @nr_bytes: number of bytes to complete @rq
2018 * @bidi_bytes: number of bytes to complete @rq->next_rq
2021 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
2022 * Drivers that supports bidi can safely call this member for any
2023 * type of request, bidi or uni. In the later case @bidi_bytes is
2027 * %false - we are done with this request
2028 * %true - still buffers pending for this request
2030 static bool blk_end_bidi_request(struct request
*rq
, int error
,
2031 unsigned int nr_bytes
, unsigned int bidi_bytes
)
2033 struct request_queue
*q
= rq
->q
;
2034 unsigned long flags
;
2036 if (blk_update_bidi_request(rq
, error
, nr_bytes
, bidi_bytes
))
2039 spin_lock_irqsave(q
->queue_lock
, flags
);
2040 blk_finish_request(rq
, error
);
2041 spin_unlock_irqrestore(q
->queue_lock
, flags
);
2047 * __blk_end_bidi_request - Complete a bidi request with queue lock held
2048 * @rq: the request to complete
2049 * @error: %0 for success, < %0 for error
2050 * @nr_bytes: number of bytes to complete @rq
2051 * @bidi_bytes: number of bytes to complete @rq->next_rq
2054 * Identical to blk_end_bidi_request() except that queue lock is
2055 * assumed to be locked on entry and remains so on return.
2058 * %false - we are done with this request
2059 * %true - still buffers pending for this request
2061 static bool __blk_end_bidi_request(struct request
*rq
, int error
,
2062 unsigned int nr_bytes
, unsigned int bidi_bytes
)
2064 if (blk_update_bidi_request(rq
, error
, nr_bytes
, bidi_bytes
))
2067 blk_finish_request(rq
, error
);
2073 * blk_end_request - Helper function for drivers to complete the request.
2074 * @rq: the request being processed
2075 * @error: %0 for success, < %0 for error
2076 * @nr_bytes: number of bytes to complete
2079 * Ends I/O on a number of bytes attached to @rq.
2080 * If @rq has leftover, sets it up for the next range of segments.
2083 * %false - we are done with this request
2084 * %true - still buffers pending for this request
2086 bool blk_end_request(struct request
*rq
, int error
, unsigned int nr_bytes
)
2088 return blk_end_bidi_request(rq
, error
, nr_bytes
, 0);
2090 EXPORT_SYMBOL_GPL(blk_end_request
);
2093 * blk_end_request_all - Helper function for drives to finish the request.
2094 * @rq: the request to finish
2095 * @err: %0 for success, < %0 for error
2098 * Completely finish @rq.
2100 void blk_end_request_all(struct request
*rq
, int error
)
2103 unsigned int bidi_bytes
= 0;
2105 if (unlikely(blk_bidi_rq(rq
)))
2106 bidi_bytes
= blk_rq_bytes(rq
->next_rq
);
2108 pending
= blk_end_bidi_request(rq
, error
, blk_rq_bytes(rq
), bidi_bytes
);
2111 EXPORT_SYMBOL_GPL(blk_end_request_all
);
2114 * blk_end_request_cur - Helper function to finish the current request chunk.
2115 * @rq: the request to finish the current chunk for
2116 * @err: %0 for success, < %0 for error
2119 * Complete the current consecutively mapped chunk from @rq.
2122 * %false - we are done with this request
2123 * %true - still buffers pending for this request
2125 bool blk_end_request_cur(struct request
*rq
, int error
)
2127 return blk_end_request(rq
, error
, blk_rq_cur_bytes(rq
));
2129 EXPORT_SYMBOL_GPL(blk_end_request_cur
);
2132 * __blk_end_request - Helper function for drivers to complete the request.
2133 * @rq: the request being processed
2134 * @error: %0 for success, < %0 for error
2135 * @nr_bytes: number of bytes to complete
2138 * Must be called with queue lock held unlike blk_end_request().
2141 * %false - we are done with this request
2142 * %true - still buffers pending for this request
2144 bool __blk_end_request(struct request
*rq
, int error
, unsigned int nr_bytes
)
2146 return __blk_end_bidi_request(rq
, error
, nr_bytes
, 0);
2148 EXPORT_SYMBOL_GPL(__blk_end_request
);
2151 * __blk_end_request_all - Helper function for drives to finish the request.
2152 * @rq: the request to finish
2153 * @err: %0 for success, < %0 for error
2156 * Completely finish @rq. Must be called with queue lock held.
2158 void __blk_end_request_all(struct request
*rq
, int error
)
2161 unsigned int bidi_bytes
= 0;
2163 if (unlikely(blk_bidi_rq(rq
)))
2164 bidi_bytes
= blk_rq_bytes(rq
->next_rq
);
2166 pending
= __blk_end_bidi_request(rq
, error
, blk_rq_bytes(rq
), bidi_bytes
);
2169 EXPORT_SYMBOL_GPL(__blk_end_request_all
);
2172 * __blk_end_request_cur - Helper function to finish the current request chunk.
2173 * @rq: the request to finish the current chunk for
2174 * @err: %0 for success, < %0 for error
2177 * Complete the current consecutively mapped chunk from @rq. Must
2178 * be called with queue lock held.
2181 * %false - we are done with this request
2182 * %true - still buffers pending for this request
2184 bool __blk_end_request_cur(struct request
*rq
, int error
)
2186 return __blk_end_request(rq
, error
, blk_rq_cur_bytes(rq
));
2188 EXPORT_SYMBOL_GPL(__blk_end_request_cur
);
2190 void blk_rq_bio_prep(struct request_queue
*q
, struct request
*rq
,
2193 /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw, and
2194 we want BIO_RW_AHEAD (bit 1) to imply REQ_FAILFAST (bit 1). */
2195 rq
->cmd_flags
|= (bio
->bi_rw
& 3);
2197 if (bio_has_data(bio
)) {
2198 rq
->nr_phys_segments
= bio_phys_segments(q
, bio
);
2199 rq
->buffer
= bio_data(bio
);
2201 rq
->__data_len
= bio
->bi_size
;
2202 rq
->bio
= rq
->biotail
= bio
;
2205 rq
->rq_disk
= bio
->bi_bdev
->bd_disk
;
2209 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
2210 * @q : the queue of the device being checked
2213 * Check if underlying low-level drivers of a device are busy.
2214 * If the drivers want to export their busy state, they must set own
2215 * exporting function using blk_queue_lld_busy() first.
2217 * Basically, this function is used only by request stacking drivers
2218 * to stop dispatching requests to underlying devices when underlying
2219 * devices are busy. This behavior helps more I/O merging on the queue
2220 * of the request stacking driver and prevents I/O throughput regression
2221 * on burst I/O load.
2224 * 0 - Not busy (The request stacking driver should dispatch request)
2225 * 1 - Busy (The request stacking driver should stop dispatching request)
2227 int blk_lld_busy(struct request_queue
*q
)
2230 return q
->lld_busy_fn(q
);
2234 EXPORT_SYMBOL_GPL(blk_lld_busy
);
2236 int kblockd_schedule_work(struct request_queue
*q
, struct work_struct
*work
)
2238 return queue_work(kblockd_workqueue
, work
);
2240 EXPORT_SYMBOL(kblockd_schedule_work
);
2242 int __init
blk_dev_init(void)
2244 BUILD_BUG_ON(__REQ_NR_BITS
> 8 *
2245 sizeof(((struct request
*)0)->cmd_flags
));
2247 kblockd_workqueue
= create_workqueue("kblockd");
2248 if (!kblockd_workqueue
)
2249 panic("Failed to create kblockd\n");
2251 request_cachep
= kmem_cache_create("blkdev_requests",
2252 sizeof(struct request
), 0, SLAB_PANIC
, NULL
);
2254 blk_requestq_cachep
= kmem_cache_create("blkdev_queue",
2255 sizeof(struct request_queue
), 0, SLAB_PANIC
, NULL
);