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/interrupt.h>
30 #include <linux/cpu.h>
31 #include <linux/blktrace_api.h>
32 #include <linux/fault-inject.h>
36 static int __make_request(struct request_queue
*q
, struct bio
*bio
);
39 * For the allocated request tables
41 static struct kmem_cache
*request_cachep
;
44 * For queue allocation
46 struct kmem_cache
*blk_requestq_cachep
;
49 * Controlling structure to kblockd
51 static struct workqueue_struct
*kblockd_workqueue
;
53 static DEFINE_PER_CPU(struct list_head
, blk_cpu_done
);
55 static void drive_stat_acct(struct request
*rq
, int new_io
)
57 struct hd_struct
*part
;
58 int rw
= rq_data_dir(rq
);
61 if (!blk_fs_request(rq
) || !rq
->rq_disk
)
64 cpu
= disk_stat_lock();
65 part
= disk_map_sector_rcu(rq
->rq_disk
, rq
->sector
);
68 all_stat_inc(cpu
, rq
->rq_disk
, part
, merges
[rw
], rq
->sector
);
70 disk_round_stats(cpu
, rq
->rq_disk
);
71 rq
->rq_disk
->in_flight
++;
73 part_round_stats(cpu
, part
);
81 void blk_queue_congestion_threshold(struct request_queue
*q
)
85 nr
= q
->nr_requests
- (q
->nr_requests
/ 8) + 1;
86 if (nr
> q
->nr_requests
)
88 q
->nr_congestion_on
= nr
;
90 nr
= q
->nr_requests
- (q
->nr_requests
/ 8) - (q
->nr_requests
/ 16) - 1;
93 q
->nr_congestion_off
= nr
;
97 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
100 * Locates the passed device's request queue and returns the address of its
103 * Will return NULL if the request queue cannot be located.
105 struct backing_dev_info
*blk_get_backing_dev_info(struct block_device
*bdev
)
107 struct backing_dev_info
*ret
= NULL
;
108 struct request_queue
*q
= bdev_get_queue(bdev
);
111 ret
= &q
->backing_dev_info
;
114 EXPORT_SYMBOL(blk_get_backing_dev_info
);
116 void blk_rq_init(struct request_queue
*q
, struct request
*rq
)
118 memset(rq
, 0, sizeof(*rq
));
120 INIT_LIST_HEAD(&rq
->queuelist
);
121 INIT_LIST_HEAD(&rq
->donelist
);
123 rq
->sector
= rq
->hard_sector
= (sector_t
) -1;
124 INIT_HLIST_NODE(&rq
->hash
);
125 RB_CLEAR_NODE(&rq
->rb_node
);
130 EXPORT_SYMBOL(blk_rq_init
);
132 static void req_bio_endio(struct request
*rq
, struct bio
*bio
,
133 unsigned int nbytes
, int error
)
135 struct request_queue
*q
= rq
->q
;
137 if (&q
->bar_rq
!= rq
) {
139 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
140 else if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
143 if (unlikely(nbytes
> bio
->bi_size
)) {
144 printk(KERN_ERR
"%s: want %u bytes done, %u left\n",
145 __func__
, nbytes
, bio
->bi_size
);
146 nbytes
= bio
->bi_size
;
149 bio
->bi_size
-= nbytes
;
150 bio
->bi_sector
+= (nbytes
>> 9);
152 if (bio_integrity(bio
))
153 bio_integrity_advance(bio
, nbytes
);
155 if (bio
->bi_size
== 0)
156 bio_endio(bio
, error
);
160 * Okay, this is the barrier request in progress, just
163 if (error
&& !q
->orderr
)
168 void blk_dump_rq_flags(struct request
*rq
, char *msg
)
172 printk(KERN_INFO
"%s: dev %s: type=%x, flags=%x\n", msg
,
173 rq
->rq_disk
? rq
->rq_disk
->disk_name
: "?", rq
->cmd_type
,
176 printk(KERN_INFO
" sector %llu, nr/cnr %lu/%u\n",
177 (unsigned long long)rq
->sector
,
179 rq
->current_nr_sectors
);
180 printk(KERN_INFO
" bio %p, biotail %p, buffer %p, data %p, len %u\n",
181 rq
->bio
, rq
->biotail
,
182 rq
->buffer
, rq
->data
,
185 if (blk_pc_request(rq
)) {
186 printk(KERN_INFO
" cdb: ");
187 for (bit
= 0; bit
< BLK_MAX_CDB
; bit
++)
188 printk("%02x ", rq
->cmd
[bit
]);
192 EXPORT_SYMBOL(blk_dump_rq_flags
);
195 * "plug" the device if there are no outstanding requests: this will
196 * force the transfer to start only after we have put all the requests
199 * This is called with interrupts off and no requests on the queue and
200 * with the queue lock held.
202 void blk_plug_device(struct request_queue
*q
)
204 WARN_ON(!irqs_disabled());
207 * don't plug a stopped queue, it must be paired with blk_start_queue()
208 * which will restart the queueing
210 if (blk_queue_stopped(q
))
213 if (!queue_flag_test_and_set(QUEUE_FLAG_PLUGGED
, q
)) {
214 mod_timer(&q
->unplug_timer
, jiffies
+ q
->unplug_delay
);
215 blk_add_trace_generic(q
, NULL
, 0, BLK_TA_PLUG
);
218 EXPORT_SYMBOL(blk_plug_device
);
221 * blk_plug_device_unlocked - plug a device without queue lock held
222 * @q: The &struct request_queue to plug
225 * Like @blk_plug_device(), but grabs the queue lock and disables
228 void blk_plug_device_unlocked(struct request_queue
*q
)
232 spin_lock_irqsave(q
->queue_lock
, flags
);
234 spin_unlock_irqrestore(q
->queue_lock
, flags
);
236 EXPORT_SYMBOL(blk_plug_device_unlocked
);
239 * remove the queue from the plugged list, if present. called with
240 * queue lock held and interrupts disabled.
242 int blk_remove_plug(struct request_queue
*q
)
244 WARN_ON(!irqs_disabled());
246 if (!queue_flag_test_and_clear(QUEUE_FLAG_PLUGGED
, q
))
249 del_timer(&q
->unplug_timer
);
252 EXPORT_SYMBOL(blk_remove_plug
);
255 * remove the plug and let it rip..
257 void __generic_unplug_device(struct request_queue
*q
)
259 if (unlikely(blk_queue_stopped(q
)))
262 if (!blk_remove_plug(q
))
267 EXPORT_SYMBOL(__generic_unplug_device
);
270 * generic_unplug_device - fire a request queue
271 * @q: The &struct request_queue in question
274 * Linux uses plugging to build bigger requests queues before letting
275 * the device have at them. If a queue is plugged, the I/O scheduler
276 * is still adding and merging requests on the queue. Once the queue
277 * gets unplugged, the request_fn defined for the queue is invoked and
280 void generic_unplug_device(struct request_queue
*q
)
282 if (blk_queue_plugged(q
)) {
283 spin_lock_irq(q
->queue_lock
);
284 __generic_unplug_device(q
);
285 spin_unlock_irq(q
->queue_lock
);
288 EXPORT_SYMBOL(generic_unplug_device
);
290 static void blk_backing_dev_unplug(struct backing_dev_info
*bdi
,
293 struct request_queue
*q
= bdi
->unplug_io_data
;
298 void blk_unplug_work(struct work_struct
*work
)
300 struct request_queue
*q
=
301 container_of(work
, struct request_queue
, unplug_work
);
303 blk_add_trace_pdu_int(q
, BLK_TA_UNPLUG_IO
, NULL
,
304 q
->rq
.count
[READ
] + q
->rq
.count
[WRITE
]);
309 void blk_unplug_timeout(unsigned long data
)
311 struct request_queue
*q
= (struct request_queue
*)data
;
313 blk_add_trace_pdu_int(q
, BLK_TA_UNPLUG_TIMER
, NULL
,
314 q
->rq
.count
[READ
] + q
->rq
.count
[WRITE
]);
316 kblockd_schedule_work(&q
->unplug_work
);
319 void blk_unplug(struct request_queue
*q
)
322 * devices don't necessarily have an ->unplug_fn defined
325 blk_add_trace_pdu_int(q
, BLK_TA_UNPLUG_IO
, NULL
,
326 q
->rq
.count
[READ
] + q
->rq
.count
[WRITE
]);
331 EXPORT_SYMBOL(blk_unplug
);
334 * blk_start_queue - restart a previously stopped queue
335 * @q: The &struct request_queue in question
338 * blk_start_queue() will clear the stop flag on the queue, and call
339 * the request_fn for the queue if it was in a stopped state when
340 * entered. Also see blk_stop_queue(). Queue lock must be held.
342 void blk_start_queue(struct request_queue
*q
)
344 WARN_ON(!irqs_disabled());
346 queue_flag_clear(QUEUE_FLAG_STOPPED
, q
);
349 * one level of recursion is ok and is much faster than kicking
350 * the unplug handling
352 if (!queue_flag_test_and_set(QUEUE_FLAG_REENTER
, q
)) {
354 queue_flag_clear(QUEUE_FLAG_REENTER
, q
);
357 kblockd_schedule_work(&q
->unplug_work
);
360 EXPORT_SYMBOL(blk_start_queue
);
363 * blk_stop_queue - stop a queue
364 * @q: The &struct request_queue in question
367 * The Linux block layer assumes that a block driver will consume all
368 * entries on the request queue when the request_fn strategy is called.
369 * Often this will not happen, because of hardware limitations (queue
370 * depth settings). If a device driver gets a 'queue full' response,
371 * or if it simply chooses not to queue more I/O at one point, it can
372 * call this function to prevent the request_fn from being called until
373 * the driver has signalled it's ready to go again. This happens by calling
374 * blk_start_queue() to restart queue operations. Queue lock must be held.
376 void blk_stop_queue(struct request_queue
*q
)
379 queue_flag_set(QUEUE_FLAG_STOPPED
, q
);
381 EXPORT_SYMBOL(blk_stop_queue
);
384 * blk_sync_queue - cancel any pending callbacks on a queue
388 * The block layer may perform asynchronous callback activity
389 * on a queue, such as calling the unplug function after a timeout.
390 * A block device may call blk_sync_queue to ensure that any
391 * such activity is cancelled, thus allowing it to release resources
392 * that the callbacks might use. The caller must already have made sure
393 * that its ->make_request_fn will not re-add plugging prior to calling
397 void blk_sync_queue(struct request_queue
*q
)
399 del_timer_sync(&q
->unplug_timer
);
400 kblockd_flush_work(&q
->unplug_work
);
402 EXPORT_SYMBOL(blk_sync_queue
);
405 * blk_run_queue - run a single device queue
406 * @q: The queue to run
408 void __blk_run_queue(struct request_queue
*q
)
413 * Only recurse once to avoid overrunning the stack, let the unplug
414 * handling reinvoke the handler shortly if we already got there.
416 if (!elv_queue_empty(q
)) {
417 if (!queue_flag_test_and_set(QUEUE_FLAG_REENTER
, q
)) {
419 queue_flag_clear(QUEUE_FLAG_REENTER
, q
);
422 kblockd_schedule_work(&q
->unplug_work
);
426 EXPORT_SYMBOL(__blk_run_queue
);
429 * blk_run_queue - run a single device queue
430 * @q: The queue to run
432 void blk_run_queue(struct request_queue
*q
)
436 spin_lock_irqsave(q
->queue_lock
, flags
);
438 spin_unlock_irqrestore(q
->queue_lock
, flags
);
440 EXPORT_SYMBOL(blk_run_queue
);
442 void blk_put_queue(struct request_queue
*q
)
444 kobject_put(&q
->kobj
);
447 void blk_cleanup_queue(struct request_queue
*q
)
449 mutex_lock(&q
->sysfs_lock
);
450 queue_flag_set_unlocked(QUEUE_FLAG_DEAD
, q
);
451 mutex_unlock(&q
->sysfs_lock
);
454 elevator_exit(q
->elevator
);
458 EXPORT_SYMBOL(blk_cleanup_queue
);
460 static int blk_init_free_list(struct request_queue
*q
)
462 struct request_list
*rl
= &q
->rq
;
464 rl
->count
[READ
] = rl
->count
[WRITE
] = 0;
465 rl
->starved
[READ
] = rl
->starved
[WRITE
] = 0;
467 init_waitqueue_head(&rl
->wait
[READ
]);
468 init_waitqueue_head(&rl
->wait
[WRITE
]);
470 rl
->rq_pool
= mempool_create_node(BLKDEV_MIN_RQ
, mempool_alloc_slab
,
471 mempool_free_slab
, request_cachep
, q
->node
);
479 struct request_queue
*blk_alloc_queue(gfp_t gfp_mask
)
481 return blk_alloc_queue_node(gfp_mask
, -1);
483 EXPORT_SYMBOL(blk_alloc_queue
);
485 struct request_queue
*blk_alloc_queue_node(gfp_t gfp_mask
, int node_id
)
487 struct request_queue
*q
;
490 q
= kmem_cache_alloc_node(blk_requestq_cachep
,
491 gfp_mask
| __GFP_ZERO
, node_id
);
495 q
->backing_dev_info
.unplug_io_fn
= blk_backing_dev_unplug
;
496 q
->backing_dev_info
.unplug_io_data
= q
;
497 err
= bdi_init(&q
->backing_dev_info
);
499 kmem_cache_free(blk_requestq_cachep
, q
);
503 init_timer(&q
->unplug_timer
);
505 kobject_init(&q
->kobj
, &blk_queue_ktype
);
507 mutex_init(&q
->sysfs_lock
);
508 spin_lock_init(&q
->__queue_lock
);
512 EXPORT_SYMBOL(blk_alloc_queue_node
);
515 * blk_init_queue - prepare a request queue for use with a block device
516 * @rfn: The function to be called to process requests that have been
517 * placed on the queue.
518 * @lock: Request queue spin lock
521 * If a block device wishes to use the standard request handling procedures,
522 * which sorts requests and coalesces adjacent requests, then it must
523 * call blk_init_queue(). The function @rfn will be called when there
524 * are requests on the queue that need to be processed. If the device
525 * supports plugging, then @rfn may not be called immediately when requests
526 * are available on the queue, but may be called at some time later instead.
527 * Plugged queues are generally unplugged when a buffer belonging to one
528 * of the requests on the queue is needed, or due to memory pressure.
530 * @rfn is not required, or even expected, to remove all requests off the
531 * queue, but only as many as it can handle at a time. If it does leave
532 * requests on the queue, it is responsible for arranging that the requests
533 * get dealt with eventually.
535 * The queue spin lock must be held while manipulating the requests on the
536 * request queue; this lock will be taken also from interrupt context, so irq
537 * disabling is needed for it.
539 * Function returns a pointer to the initialized request queue, or %NULL if
543 * blk_init_queue() must be paired with a blk_cleanup_queue() call
544 * when the block device is deactivated (such as at module unload).
547 struct request_queue
*blk_init_queue(request_fn_proc
*rfn
, spinlock_t
*lock
)
549 return blk_init_queue_node(rfn
, lock
, -1);
551 EXPORT_SYMBOL(blk_init_queue
);
553 struct request_queue
*
554 blk_init_queue_node(request_fn_proc
*rfn
, spinlock_t
*lock
, int node_id
)
556 struct request_queue
*q
= blk_alloc_queue_node(GFP_KERNEL
, node_id
);
562 if (blk_init_free_list(q
)) {
563 kmem_cache_free(blk_requestq_cachep
, q
);
568 * if caller didn't supply a lock, they get per-queue locking with
572 lock
= &q
->__queue_lock
;
575 q
->prep_rq_fn
= NULL
;
576 q
->unplug_fn
= generic_unplug_device
;
577 q
->queue_flags
= (1 << QUEUE_FLAG_CLUSTER
);
578 q
->queue_lock
= lock
;
580 blk_queue_segment_boundary(q
, 0xffffffff);
582 blk_queue_make_request(q
, __make_request
);
583 blk_queue_max_segment_size(q
, MAX_SEGMENT_SIZE
);
585 blk_queue_max_hw_segments(q
, MAX_HW_SEGMENTS
);
586 blk_queue_max_phys_segments(q
, MAX_PHYS_SEGMENTS
);
588 q
->sg_reserved_size
= INT_MAX
;
590 blk_set_cmd_filter_defaults(&q
->cmd_filter
);
595 if (!elevator_init(q
, NULL
)) {
596 blk_queue_congestion_threshold(q
);
603 EXPORT_SYMBOL(blk_init_queue_node
);
605 int blk_get_queue(struct request_queue
*q
)
607 if (likely(!test_bit(QUEUE_FLAG_DEAD
, &q
->queue_flags
))) {
608 kobject_get(&q
->kobj
);
615 static inline void blk_free_request(struct request_queue
*q
, struct request
*rq
)
617 if (rq
->cmd_flags
& REQ_ELVPRIV
)
618 elv_put_request(q
, rq
);
619 mempool_free(rq
, q
->rq
.rq_pool
);
622 static struct request
*
623 blk_alloc_request(struct request_queue
*q
, int rw
, int priv
, gfp_t gfp_mask
)
625 struct request
*rq
= mempool_alloc(q
->rq
.rq_pool
, gfp_mask
);
632 rq
->cmd_flags
= rw
| REQ_ALLOCED
;
635 if (unlikely(elv_set_request(q
, rq
, gfp_mask
))) {
636 mempool_free(rq
, q
->rq
.rq_pool
);
639 rq
->cmd_flags
|= REQ_ELVPRIV
;
646 * ioc_batching returns true if the ioc is a valid batching request and
647 * should be given priority access to a request.
649 static inline int ioc_batching(struct request_queue
*q
, struct io_context
*ioc
)
655 * Make sure the process is able to allocate at least 1 request
656 * even if the batch times out, otherwise we could theoretically
659 return ioc
->nr_batch_requests
== q
->nr_batching
||
660 (ioc
->nr_batch_requests
> 0
661 && time_before(jiffies
, ioc
->last_waited
+ BLK_BATCH_TIME
));
665 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
666 * will cause the process to be a "batcher" on all queues in the system. This
667 * is the behaviour we want though - once it gets a wakeup it should be given
670 static void ioc_set_batching(struct request_queue
*q
, struct io_context
*ioc
)
672 if (!ioc
|| ioc_batching(q
, ioc
))
675 ioc
->nr_batch_requests
= q
->nr_batching
;
676 ioc
->last_waited
= jiffies
;
679 static void __freed_request(struct request_queue
*q
, int rw
)
681 struct request_list
*rl
= &q
->rq
;
683 if (rl
->count
[rw
] < queue_congestion_off_threshold(q
))
684 blk_clear_queue_congested(q
, rw
);
686 if (rl
->count
[rw
] + 1 <= q
->nr_requests
) {
687 if (waitqueue_active(&rl
->wait
[rw
]))
688 wake_up(&rl
->wait
[rw
]);
690 blk_clear_queue_full(q
, rw
);
695 * A request has just been released. Account for it, update the full and
696 * congestion status, wake up any waiters. Called under q->queue_lock.
698 static void freed_request(struct request_queue
*q
, int rw
, int priv
)
700 struct request_list
*rl
= &q
->rq
;
706 __freed_request(q
, rw
);
708 if (unlikely(rl
->starved
[rw
^ 1]))
709 __freed_request(q
, rw
^ 1);
712 #define blkdev_free_rq(list) list_entry((list)->next, struct request, queuelist)
714 * Get a free request, queue_lock must be held.
715 * Returns NULL on failure, with queue_lock held.
716 * Returns !NULL on success, with queue_lock *not held*.
718 static struct request
*get_request(struct request_queue
*q
, int rw_flags
,
719 struct bio
*bio
, gfp_t gfp_mask
)
721 struct request
*rq
= NULL
;
722 struct request_list
*rl
= &q
->rq
;
723 struct io_context
*ioc
= NULL
;
724 const int rw
= rw_flags
& 0x01;
727 may_queue
= elv_may_queue(q
, rw_flags
);
728 if (may_queue
== ELV_MQUEUE_NO
)
731 if (rl
->count
[rw
]+1 >= queue_congestion_on_threshold(q
)) {
732 if (rl
->count
[rw
]+1 >= q
->nr_requests
) {
733 ioc
= current_io_context(GFP_ATOMIC
, q
->node
);
735 * The queue will fill after this allocation, so set
736 * it as full, and mark this process as "batching".
737 * This process will be allowed to complete a batch of
738 * requests, others will be blocked.
740 if (!blk_queue_full(q
, rw
)) {
741 ioc_set_batching(q
, ioc
);
742 blk_set_queue_full(q
, rw
);
744 if (may_queue
!= ELV_MQUEUE_MUST
745 && !ioc_batching(q
, ioc
)) {
747 * The queue is full and the allocating
748 * process is not a "batcher", and not
749 * exempted by the IO scheduler
755 blk_set_queue_congested(q
, rw
);
759 * Only allow batching queuers to allocate up to 50% over the defined
760 * limit of requests, otherwise we could have thousands of requests
761 * allocated with any setting of ->nr_requests
763 if (rl
->count
[rw
] >= (3 * q
->nr_requests
/ 2))
769 priv
= !test_bit(QUEUE_FLAG_ELVSWITCH
, &q
->queue_flags
);
773 spin_unlock_irq(q
->queue_lock
);
775 rq
= blk_alloc_request(q
, rw_flags
, priv
, gfp_mask
);
778 * Allocation failed presumably due to memory. Undo anything
779 * we might have messed up.
781 * Allocating task should really be put onto the front of the
782 * wait queue, but this is pretty rare.
784 spin_lock_irq(q
->queue_lock
);
785 freed_request(q
, rw
, priv
);
788 * in the very unlikely event that allocation failed and no
789 * requests for this direction was pending, mark us starved
790 * so that freeing of a request in the other direction will
791 * notice us. another possible fix would be to split the
792 * rq mempool into READ and WRITE
795 if (unlikely(rl
->count
[rw
] == 0))
802 * ioc may be NULL here, and ioc_batching will be false. That's
803 * OK, if the queue is under the request limit then requests need
804 * not count toward the nr_batch_requests limit. There will always
805 * be some limit enforced by BLK_BATCH_TIME.
807 if (ioc_batching(q
, ioc
))
808 ioc
->nr_batch_requests
--;
810 blk_add_trace_generic(q
, bio
, rw
, BLK_TA_GETRQ
);
816 * No available requests for this queue, unplug the device and wait for some
817 * requests to become available.
819 * Called with q->queue_lock held, and returns with it unlocked.
821 static struct request
*get_request_wait(struct request_queue
*q
, int rw_flags
,
824 const int rw
= rw_flags
& 0x01;
827 rq
= get_request(q
, rw_flags
, bio
, GFP_NOIO
);
830 struct io_context
*ioc
;
831 struct request_list
*rl
= &q
->rq
;
833 prepare_to_wait_exclusive(&rl
->wait
[rw
], &wait
,
834 TASK_UNINTERRUPTIBLE
);
836 blk_add_trace_generic(q
, bio
, rw
, BLK_TA_SLEEPRQ
);
838 __generic_unplug_device(q
);
839 spin_unlock_irq(q
->queue_lock
);
843 * After sleeping, we become a "batching" process and
844 * will be able to allocate at least one request, and
845 * up to a big batch of them for a small period time.
846 * See ioc_batching, ioc_set_batching
848 ioc
= current_io_context(GFP_NOIO
, q
->node
);
849 ioc_set_batching(q
, ioc
);
851 spin_lock_irq(q
->queue_lock
);
852 finish_wait(&rl
->wait
[rw
], &wait
);
854 rq
= get_request(q
, rw_flags
, bio
, GFP_NOIO
);
860 struct request
*blk_get_request(struct request_queue
*q
, int rw
, gfp_t gfp_mask
)
864 BUG_ON(rw
!= READ
&& rw
!= WRITE
);
866 spin_lock_irq(q
->queue_lock
);
867 if (gfp_mask
& __GFP_WAIT
) {
868 rq
= get_request_wait(q
, rw
, NULL
);
870 rq
= get_request(q
, rw
, NULL
, gfp_mask
);
872 spin_unlock_irq(q
->queue_lock
);
874 /* q->queue_lock is unlocked at this point */
878 EXPORT_SYMBOL(blk_get_request
);
881 * blk_start_queueing - initiate dispatch of requests to device
882 * @q: request queue to kick into gear
884 * This is basically a helper to remove the need to know whether a queue
885 * is plugged or not if someone just wants to initiate dispatch of requests
888 * The queue lock must be held with interrupts disabled.
890 void blk_start_queueing(struct request_queue
*q
)
892 if (!blk_queue_plugged(q
))
895 __generic_unplug_device(q
);
897 EXPORT_SYMBOL(blk_start_queueing
);
900 * blk_requeue_request - put a request back on queue
901 * @q: request queue where request should be inserted
902 * @rq: request to be inserted
905 * Drivers often keep queueing requests until the hardware cannot accept
906 * more, when that condition happens we need to put the request back
907 * on the queue. Must be called with queue lock held.
909 void blk_requeue_request(struct request_queue
*q
, struct request
*rq
)
911 blk_add_trace_rq(q
, rq
, BLK_TA_REQUEUE
);
913 if (blk_rq_tagged(rq
))
914 blk_queue_end_tag(q
, rq
);
916 elv_requeue_request(q
, rq
);
918 EXPORT_SYMBOL(blk_requeue_request
);
921 * blk_insert_request - insert a special request into a request queue
922 * @q: request queue where request should be inserted
923 * @rq: request to be inserted
924 * @at_head: insert request at head or tail of queue
925 * @data: private data
928 * Many block devices need to execute commands asynchronously, so they don't
929 * block the whole kernel from preemption during request execution. This is
930 * accomplished normally by inserting aritficial requests tagged as
931 * REQ_TYPE_SPECIAL in to the corresponding request queue, and letting them
932 * be scheduled for actual execution by the request queue.
934 * We have the option of inserting the head or the tail of the queue.
935 * Typically we use the tail for new ioctls and so forth. We use the head
936 * of the queue for things like a QUEUE_FULL message from a device, or a
937 * host that is unable to accept a particular command.
939 void blk_insert_request(struct request_queue
*q
, struct request
*rq
,
940 int at_head
, void *data
)
942 int where
= at_head
? ELEVATOR_INSERT_FRONT
: ELEVATOR_INSERT_BACK
;
946 * tell I/O scheduler that this isn't a regular read/write (ie it
947 * must not attempt merges on this) and that it acts as a soft
950 rq
->cmd_type
= REQ_TYPE_SPECIAL
;
951 rq
->cmd_flags
|= REQ_SOFTBARRIER
;
955 spin_lock_irqsave(q
->queue_lock
, flags
);
958 * If command is tagged, release the tag
960 if (blk_rq_tagged(rq
))
961 blk_queue_end_tag(q
, rq
);
963 drive_stat_acct(rq
, 1);
964 __elv_add_request(q
, rq
, where
, 0);
965 blk_start_queueing(q
);
966 spin_unlock_irqrestore(q
->queue_lock
, flags
);
968 EXPORT_SYMBOL(blk_insert_request
);
971 * add-request adds a request to the linked list.
972 * queue lock is held and interrupts disabled, as we muck with the
973 * request queue list.
975 static inline void add_request(struct request_queue
*q
, struct request
*req
)
977 drive_stat_acct(req
, 1);
980 * elevator indicated where it wants this request to be
981 * inserted at elevator_merge time
983 __elv_add_request(q
, req
, ELEVATOR_INSERT_SORT
, 0);
987 * disk_round_stats() - Round off the performance stats on a struct
990 * The average IO queue length and utilisation statistics are maintained
991 * by observing the current state of the queue length and the amount of
992 * time it has been in this state for.
994 * Normally, that accounting is done on IO completion, but that can result
995 * in more than a second's worth of IO being accounted for within any one
996 * second, leading to >100% utilisation. To deal with that, we call this
997 * function to do a round-off before returning the results when reading
998 * /proc/diskstats. This accounts immediately for all queue usage up to
999 * the current jiffies and restarts the counters again.
1001 void disk_round_stats(int cpu
, struct gendisk
*disk
)
1003 unsigned long now
= jiffies
;
1005 if (now
== disk
->stamp
)
1008 if (disk
->in_flight
) {
1009 disk_stat_add(cpu
, disk
, time_in_queue
,
1010 disk
->in_flight
* (now
- disk
->stamp
));
1011 disk_stat_add(cpu
, disk
, io_ticks
, (now
- disk
->stamp
));
1015 EXPORT_SYMBOL_GPL(disk_round_stats
);
1017 void part_round_stats(int cpu
, struct hd_struct
*part
)
1019 unsigned long now
= jiffies
;
1021 if (now
== part
->stamp
)
1024 if (part
->in_flight
) {
1025 part_stat_add(cpu
, part
, time_in_queue
,
1026 part
->in_flight
* (now
- part
->stamp
));
1027 part_stat_add(cpu
, part
, io_ticks
, (now
- part
->stamp
));
1033 * queue lock must be held
1035 void __blk_put_request(struct request_queue
*q
, struct request
*req
)
1039 if (unlikely(--req
->ref_count
))
1042 elv_completed_request(q
, req
);
1045 * Request may not have originated from ll_rw_blk. if not,
1046 * it didn't come out of our reserved rq pools
1048 if (req
->cmd_flags
& REQ_ALLOCED
) {
1049 int rw
= rq_data_dir(req
);
1050 int priv
= req
->cmd_flags
& REQ_ELVPRIV
;
1052 BUG_ON(!list_empty(&req
->queuelist
));
1053 BUG_ON(!hlist_unhashed(&req
->hash
));
1055 blk_free_request(q
, req
);
1056 freed_request(q
, rw
, priv
);
1059 EXPORT_SYMBOL_GPL(__blk_put_request
);
1061 void blk_put_request(struct request
*req
)
1063 unsigned long flags
;
1064 struct request_queue
*q
= req
->q
;
1066 spin_lock_irqsave(q
->queue_lock
, flags
);
1067 __blk_put_request(q
, req
);
1068 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1070 EXPORT_SYMBOL(blk_put_request
);
1072 void init_request_from_bio(struct request
*req
, struct bio
*bio
)
1074 req
->cmd_type
= REQ_TYPE_FS
;
1077 * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
1079 if (bio_rw_ahead(bio
) || bio_failfast(bio
))
1080 req
->cmd_flags
|= REQ_FAILFAST
;
1083 * REQ_BARRIER implies no merging, but lets make it explicit
1085 if (unlikely(bio_discard(bio
))) {
1086 req
->cmd_flags
|= REQ_DISCARD
;
1087 if (bio_barrier(bio
))
1088 req
->cmd_flags
|= REQ_SOFTBARRIER
;
1089 req
->q
->prepare_discard_fn(req
->q
, req
);
1090 } else if (unlikely(bio_barrier(bio
)))
1091 req
->cmd_flags
|= (REQ_HARDBARRIER
| REQ_NOMERGE
);
1094 req
->cmd_flags
|= REQ_RW_SYNC
;
1095 if (bio_rw_meta(bio
))
1096 req
->cmd_flags
|= REQ_RW_META
;
1099 req
->hard_sector
= req
->sector
= bio
->bi_sector
;
1100 req
->ioprio
= bio_prio(bio
);
1101 req
->start_time
= jiffies
;
1102 blk_rq_bio_prep(req
->q
, req
, bio
);
1105 static int __make_request(struct request_queue
*q
, struct bio
*bio
)
1107 struct request
*req
;
1108 int el_ret
, nr_sectors
, barrier
, discard
, err
;
1109 const unsigned short prio
= bio_prio(bio
);
1110 const int sync
= bio_sync(bio
);
1113 nr_sectors
= bio_sectors(bio
);
1116 * low level driver can indicate that it wants pages above a
1117 * certain limit bounced to low memory (ie for highmem, or even
1118 * ISA dma in theory)
1120 blk_queue_bounce(q
, &bio
);
1122 barrier
= bio_barrier(bio
);
1123 if (unlikely(barrier
) && bio_has_data(bio
) &&
1124 (q
->next_ordered
== QUEUE_ORDERED_NONE
)) {
1129 discard
= bio_discard(bio
);
1130 if (unlikely(discard
) && !q
->prepare_discard_fn
) {
1135 spin_lock_irq(q
->queue_lock
);
1137 if (unlikely(barrier
) || elv_queue_empty(q
))
1140 el_ret
= elv_merge(q
, &req
, bio
);
1142 case ELEVATOR_BACK_MERGE
:
1143 BUG_ON(!rq_mergeable(req
));
1145 if (!ll_back_merge_fn(q
, req
, bio
))
1148 blk_add_trace_bio(q
, bio
, BLK_TA_BACKMERGE
);
1150 req
->biotail
->bi_next
= bio
;
1152 req
->nr_sectors
= req
->hard_nr_sectors
+= nr_sectors
;
1153 req
->ioprio
= ioprio_best(req
->ioprio
, prio
);
1154 drive_stat_acct(req
, 0);
1155 if (!attempt_back_merge(q
, req
))
1156 elv_merged_request(q
, req
, el_ret
);
1159 case ELEVATOR_FRONT_MERGE
:
1160 BUG_ON(!rq_mergeable(req
));
1162 if (!ll_front_merge_fn(q
, req
, bio
))
1165 blk_add_trace_bio(q
, bio
, BLK_TA_FRONTMERGE
);
1167 bio
->bi_next
= req
->bio
;
1171 * may not be valid. if the low level driver said
1172 * it didn't need a bounce buffer then it better
1173 * not touch req->buffer either...
1175 req
->buffer
= bio_data(bio
);
1176 req
->current_nr_sectors
= bio_cur_sectors(bio
);
1177 req
->hard_cur_sectors
= req
->current_nr_sectors
;
1178 req
->sector
= req
->hard_sector
= bio
->bi_sector
;
1179 req
->nr_sectors
= req
->hard_nr_sectors
+= nr_sectors
;
1180 req
->ioprio
= ioprio_best(req
->ioprio
, prio
);
1181 drive_stat_acct(req
, 0);
1182 if (!attempt_front_merge(q
, req
))
1183 elv_merged_request(q
, req
, el_ret
);
1186 /* ELV_NO_MERGE: elevator says don't/can't merge. */
1193 * This sync check and mask will be re-done in init_request_from_bio(),
1194 * but we need to set it earlier to expose the sync flag to the
1195 * rq allocator and io schedulers.
1197 rw_flags
= bio_data_dir(bio
);
1199 rw_flags
|= REQ_RW_SYNC
;
1202 * Grab a free request. This is might sleep but can not fail.
1203 * Returns with the queue unlocked.
1205 req
= get_request_wait(q
, rw_flags
, bio
);
1208 * After dropping the lock and possibly sleeping here, our request
1209 * may now be mergeable after it had proven unmergeable (above).
1210 * We don't worry about that case for efficiency. It won't happen
1211 * often, and the elevators are able to handle it.
1213 init_request_from_bio(req
, bio
);
1215 spin_lock_irq(q
->queue_lock
);
1216 if (elv_queue_empty(q
))
1218 add_request(q
, req
);
1221 __generic_unplug_device(q
);
1223 spin_unlock_irq(q
->queue_lock
);
1227 bio_endio(bio
, err
);
1232 * If bio->bi_dev is a partition, remap the location
1234 static inline void blk_partition_remap(struct bio
*bio
)
1236 struct block_device
*bdev
= bio
->bi_bdev
;
1238 if (bio_sectors(bio
) && bdev
!= bdev
->bd_contains
) {
1239 struct hd_struct
*p
= bdev
->bd_part
;
1241 bio
->bi_sector
+= p
->start_sect
;
1242 bio
->bi_bdev
= bdev
->bd_contains
;
1244 blk_add_trace_remap(bdev_get_queue(bio
->bi_bdev
), bio
,
1245 bdev
->bd_dev
, bio
->bi_sector
,
1246 bio
->bi_sector
- p
->start_sect
);
1250 static void handle_bad_sector(struct bio
*bio
)
1252 char b
[BDEVNAME_SIZE
];
1254 printk(KERN_INFO
"attempt to access beyond end of device\n");
1255 printk(KERN_INFO
"%s: rw=%ld, want=%Lu, limit=%Lu\n",
1256 bdevname(bio
->bi_bdev
, b
),
1258 (unsigned long long)bio
->bi_sector
+ bio_sectors(bio
),
1259 (long long)(bio
->bi_bdev
->bd_inode
->i_size
>> 9));
1261 set_bit(BIO_EOF
, &bio
->bi_flags
);
1264 #ifdef CONFIG_FAIL_MAKE_REQUEST
1266 static DECLARE_FAULT_ATTR(fail_make_request
);
1268 static int __init
setup_fail_make_request(char *str
)
1270 return setup_fault_attr(&fail_make_request
, str
);
1272 __setup("fail_make_request=", setup_fail_make_request
);
1274 static int should_fail_request(struct bio
*bio
)
1276 if ((bio
->bi_bdev
->bd_disk
->flags
& GENHD_FL_FAIL
) ||
1277 (bio
->bi_bdev
->bd_part
&& bio
->bi_bdev
->bd_part
->make_it_fail
))
1278 return should_fail(&fail_make_request
, bio
->bi_size
);
1283 static int __init
fail_make_request_debugfs(void)
1285 return init_fault_attr_dentries(&fail_make_request
,
1286 "fail_make_request");
1289 late_initcall(fail_make_request_debugfs
);
1291 #else /* CONFIG_FAIL_MAKE_REQUEST */
1293 static inline int should_fail_request(struct bio
*bio
)
1298 #endif /* CONFIG_FAIL_MAKE_REQUEST */
1301 * Check whether this bio extends beyond the end of the device.
1303 static inline int bio_check_eod(struct bio
*bio
, unsigned int nr_sectors
)
1310 /* Test device or partition size, when known. */
1311 maxsector
= bio
->bi_bdev
->bd_inode
->i_size
>> 9;
1313 sector_t sector
= bio
->bi_sector
;
1315 if (maxsector
< nr_sectors
|| maxsector
- nr_sectors
< sector
) {
1317 * This may well happen - the kernel calls bread()
1318 * without checking the size of the device, e.g., when
1319 * mounting a device.
1321 handle_bad_sector(bio
);
1330 * generic_make_request - hand a buffer to its device driver for I/O
1331 * @bio: The bio describing the location in memory and on the device.
1333 * generic_make_request() is used to make I/O requests of block
1334 * devices. It is passed a &struct bio, which describes the I/O that needs
1337 * generic_make_request() does not return any status. The
1338 * success/failure status of the request, along with notification of
1339 * completion, is delivered asynchronously through the bio->bi_end_io
1340 * function described (one day) else where.
1342 * The caller of generic_make_request must make sure that bi_io_vec
1343 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1344 * set to describe the device address, and the
1345 * bi_end_io and optionally bi_private are set to describe how
1346 * completion notification should be signaled.
1348 * generic_make_request and the drivers it calls may use bi_next if this
1349 * bio happens to be merged with someone else, and may change bi_dev and
1350 * bi_sector for remaps as it sees fit. So the values of these fields
1351 * should NOT be depended on after the call to generic_make_request.
1353 static inline void __generic_make_request(struct bio
*bio
)
1355 struct request_queue
*q
;
1356 sector_t old_sector
;
1357 int ret
, nr_sectors
= bio_sectors(bio
);
1363 if (bio_check_eod(bio
, nr_sectors
))
1367 * Resolve the mapping until finished. (drivers are
1368 * still free to implement/resolve their own stacking
1369 * by explicitly returning 0)
1371 * NOTE: we don't repeat the blk_size check for each new device.
1372 * Stacking drivers are expected to know what they are doing.
1377 char b
[BDEVNAME_SIZE
];
1379 q
= bdev_get_queue(bio
->bi_bdev
);
1382 "generic_make_request: Trying to access "
1383 "nonexistent block-device %s (%Lu)\n",
1384 bdevname(bio
->bi_bdev
, b
),
1385 (long long) bio
->bi_sector
);
1387 bio_endio(bio
, err
);
1391 if (unlikely(nr_sectors
> q
->max_hw_sectors
)) {
1392 printk(KERN_ERR
"bio too big device %s (%u > %u)\n",
1393 bdevname(bio
->bi_bdev
, b
),
1399 if (unlikely(test_bit(QUEUE_FLAG_DEAD
, &q
->queue_flags
)))
1402 if (should_fail_request(bio
))
1406 * If this device has partitions, remap block n
1407 * of partition p to block n+start(p) of the disk.
1409 blk_partition_remap(bio
);
1411 if (bio_integrity_enabled(bio
) && bio_integrity_prep(bio
))
1414 if (old_sector
!= -1)
1415 blk_add_trace_remap(q
, bio
, old_dev
, bio
->bi_sector
,
1418 blk_add_trace_bio(q
, bio
, BLK_TA_QUEUE
);
1420 old_sector
= bio
->bi_sector
;
1421 old_dev
= bio
->bi_bdev
->bd_dev
;
1423 if (bio_check_eod(bio
, nr_sectors
))
1425 if ((bio_empty_barrier(bio
) && !q
->prepare_flush_fn
) ||
1426 (bio_discard(bio
) && !q
->prepare_discard_fn
)) {
1431 ret
= q
->make_request_fn(q
, bio
);
1436 * We only want one ->make_request_fn to be active at a time,
1437 * else stack usage with stacked devices could be a problem.
1438 * So use current->bio_{list,tail} to keep a list of requests
1439 * submited by a make_request_fn function.
1440 * current->bio_tail is also used as a flag to say if
1441 * generic_make_request is currently active in this task or not.
1442 * If it is NULL, then no make_request is active. If it is non-NULL,
1443 * then a make_request is active, and new requests should be added
1446 void generic_make_request(struct bio
*bio
)
1448 if (current
->bio_tail
) {
1449 /* make_request is active */
1450 *(current
->bio_tail
) = bio
;
1451 bio
->bi_next
= NULL
;
1452 current
->bio_tail
= &bio
->bi_next
;
1455 /* following loop may be a bit non-obvious, and so deserves some
1457 * Before entering the loop, bio->bi_next is NULL (as all callers
1458 * ensure that) so we have a list with a single bio.
1459 * We pretend that we have just taken it off a longer list, so
1460 * we assign bio_list to the next (which is NULL) and bio_tail
1461 * to &bio_list, thus initialising the bio_list of new bios to be
1462 * added. __generic_make_request may indeed add some more bios
1463 * through a recursive call to generic_make_request. If it
1464 * did, we find a non-NULL value in bio_list and re-enter the loop
1465 * from the top. In this case we really did just take the bio
1466 * of the top of the list (no pretending) and so fixup bio_list and
1467 * bio_tail or bi_next, and call into __generic_make_request again.
1469 * The loop was structured like this to make only one call to
1470 * __generic_make_request (which is important as it is large and
1471 * inlined) and to keep the structure simple.
1473 BUG_ON(bio
->bi_next
);
1475 current
->bio_list
= bio
->bi_next
;
1476 if (bio
->bi_next
== NULL
)
1477 current
->bio_tail
= ¤t
->bio_list
;
1479 bio
->bi_next
= NULL
;
1480 __generic_make_request(bio
);
1481 bio
= current
->bio_list
;
1483 current
->bio_tail
= NULL
; /* deactivate */
1485 EXPORT_SYMBOL(generic_make_request
);
1488 * submit_bio - submit a bio to the block device layer for I/O
1489 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1490 * @bio: The &struct bio which describes the I/O
1492 * submit_bio() is very similar in purpose to generic_make_request(), and
1493 * uses that function to do most of the work. Both are fairly rough
1494 * interfaces; @bio must be presetup and ready for I/O.
1497 void submit_bio(int rw
, struct bio
*bio
)
1499 int count
= bio_sectors(bio
);
1504 * If it's a regular read/write or a barrier with data attached,
1505 * go through the normal accounting stuff before submission.
1507 if (bio_has_data(bio
)) {
1509 count_vm_events(PGPGOUT
, count
);
1511 task_io_account_read(bio
->bi_size
);
1512 count_vm_events(PGPGIN
, count
);
1515 if (unlikely(block_dump
)) {
1516 char b
[BDEVNAME_SIZE
];
1517 printk(KERN_DEBUG
"%s(%d): %s block %Lu on %s\n",
1518 current
->comm
, task_pid_nr(current
),
1519 (rw
& WRITE
) ? "WRITE" : "READ",
1520 (unsigned long long)bio
->bi_sector
,
1521 bdevname(bio
->bi_bdev
, b
));
1525 generic_make_request(bio
);
1527 EXPORT_SYMBOL(submit_bio
);
1530 * __end_that_request_first - end I/O on a request
1531 * @req: the request being processed
1532 * @error: %0 for success, < %0 for error
1533 * @nr_bytes: number of bytes to complete
1536 * Ends I/O on a number of bytes attached to @req, and sets it up
1537 * for the next range of segments (if any) in the cluster.
1540 * %0 - we are done with this request, call end_that_request_last()
1541 * %1 - still buffers pending for this request
1543 static int __end_that_request_first(struct request
*req
, int error
,
1546 int total_bytes
, bio_nbytes
, next_idx
= 0;
1549 blk_add_trace_rq(req
->q
, req
, BLK_TA_COMPLETE
);
1552 * for a REQ_TYPE_BLOCK_PC request, we want to carry any eventual
1553 * sense key with us all the way through
1555 if (!blk_pc_request(req
))
1558 if (error
&& (blk_fs_request(req
) && !(req
->cmd_flags
& REQ_QUIET
))) {
1559 printk(KERN_ERR
"end_request: I/O error, dev %s, sector %llu\n",
1560 req
->rq_disk
? req
->rq_disk
->disk_name
: "?",
1561 (unsigned long long)req
->sector
);
1564 if (blk_fs_request(req
) && req
->rq_disk
) {
1565 const int rw
= rq_data_dir(req
);
1566 struct hd_struct
*part
;
1569 cpu
= disk_stat_lock();
1570 part
= disk_map_sector_rcu(req
->rq_disk
, req
->sector
);
1571 all_stat_add(cpu
, req
->rq_disk
, part
, sectors
[rw
],
1572 nr_bytes
>> 9, req
->sector
);
1576 total_bytes
= bio_nbytes
= 0;
1577 while ((bio
= req
->bio
) != NULL
) {
1581 * For an empty barrier request, the low level driver must
1582 * store a potential error location in ->sector. We pass
1583 * that back up in ->bi_sector.
1585 if (blk_empty_barrier(req
))
1586 bio
->bi_sector
= req
->sector
;
1588 if (nr_bytes
>= bio
->bi_size
) {
1589 req
->bio
= bio
->bi_next
;
1590 nbytes
= bio
->bi_size
;
1591 req_bio_endio(req
, bio
, nbytes
, error
);
1595 int idx
= bio
->bi_idx
+ next_idx
;
1597 if (unlikely(bio
->bi_idx
>= bio
->bi_vcnt
)) {
1598 blk_dump_rq_flags(req
, "__end_that");
1599 printk(KERN_ERR
"%s: bio idx %d >= vcnt %d\n",
1600 __func__
, bio
->bi_idx
, bio
->bi_vcnt
);
1604 nbytes
= bio_iovec_idx(bio
, idx
)->bv_len
;
1605 BIO_BUG_ON(nbytes
> bio
->bi_size
);
1608 * not a complete bvec done
1610 if (unlikely(nbytes
> nr_bytes
)) {
1611 bio_nbytes
+= nr_bytes
;
1612 total_bytes
+= nr_bytes
;
1617 * advance to the next vector
1620 bio_nbytes
+= nbytes
;
1623 total_bytes
+= nbytes
;
1629 * end more in this run, or just return 'not-done'
1631 if (unlikely(nr_bytes
<= 0))
1643 * if the request wasn't completed, update state
1646 req_bio_endio(req
, bio
, bio_nbytes
, error
);
1647 bio
->bi_idx
+= next_idx
;
1648 bio_iovec(bio
)->bv_offset
+= nr_bytes
;
1649 bio_iovec(bio
)->bv_len
-= nr_bytes
;
1652 blk_recalc_rq_sectors(req
, total_bytes
>> 9);
1653 blk_recalc_rq_segments(req
);
1658 * splice the completion data to a local structure and hand off to
1659 * process_completion_queue() to complete the requests
1661 static void blk_done_softirq(struct softirq_action
*h
)
1663 struct list_head
*cpu_list
, local_list
;
1665 local_irq_disable();
1666 cpu_list
= &__get_cpu_var(blk_cpu_done
);
1667 list_replace_init(cpu_list
, &local_list
);
1670 while (!list_empty(&local_list
)) {
1673 rq
= list_entry(local_list
.next
, struct request
, donelist
);
1674 list_del_init(&rq
->donelist
);
1675 rq
->q
->softirq_done_fn(rq
);
1679 static int __cpuinit
blk_cpu_notify(struct notifier_block
*self
,
1680 unsigned long action
, void *hcpu
)
1683 * If a CPU goes away, splice its entries to the current CPU
1684 * and trigger a run of the softirq
1686 if (action
== CPU_DEAD
|| action
== CPU_DEAD_FROZEN
) {
1687 int cpu
= (unsigned long) hcpu
;
1689 local_irq_disable();
1690 list_splice_init(&per_cpu(blk_cpu_done
, cpu
),
1691 &__get_cpu_var(blk_cpu_done
));
1692 raise_softirq_irqoff(BLOCK_SOFTIRQ
);
1700 static struct notifier_block blk_cpu_notifier __cpuinitdata
= {
1701 .notifier_call
= blk_cpu_notify
,
1705 * blk_complete_request - end I/O on a request
1706 * @req: the request being processed
1709 * Ends all I/O on a request. It does not handle partial completions,
1710 * unless the driver actually implements this in its completion callback
1711 * through requeueing. The actual completion happens out-of-order,
1712 * through a softirq handler. The user must have registered a completion
1713 * callback through blk_queue_softirq_done().
1716 void blk_complete_request(struct request
*req
)
1718 struct list_head
*cpu_list
;
1719 unsigned long flags
;
1721 BUG_ON(!req
->q
->softirq_done_fn
);
1723 local_irq_save(flags
);
1725 cpu_list
= &__get_cpu_var(blk_cpu_done
);
1726 list_add_tail(&req
->donelist
, cpu_list
);
1727 raise_softirq_irqoff(BLOCK_SOFTIRQ
);
1729 local_irq_restore(flags
);
1731 EXPORT_SYMBOL(blk_complete_request
);
1734 * queue lock must be held
1736 static void end_that_request_last(struct request
*req
, int error
)
1738 struct gendisk
*disk
= req
->rq_disk
;
1740 if (blk_rq_tagged(req
))
1741 blk_queue_end_tag(req
->q
, req
);
1743 if (blk_queued_rq(req
))
1744 blkdev_dequeue_request(req
);
1746 if (unlikely(laptop_mode
) && blk_fs_request(req
))
1747 laptop_io_completion();
1750 * Account IO completion. bar_rq isn't accounted as a normal
1751 * IO on queueing nor completion. Accounting the containing
1752 * request is enough.
1754 if (disk
&& blk_fs_request(req
) && req
!= &req
->q
->bar_rq
) {
1755 unsigned long duration
= jiffies
- req
->start_time
;
1756 const int rw
= rq_data_dir(req
);
1757 struct hd_struct
*part
;
1760 cpu
= disk_stat_lock();
1761 part
= disk_map_sector_rcu(disk
, req
->sector
);
1763 all_stat_inc(cpu
, disk
, part
, ios
[rw
], req
->sector
);
1764 all_stat_add(cpu
, disk
, part
, ticks
[rw
], duration
, req
->sector
);
1765 disk_round_stats(cpu
, disk
);
1768 part_round_stats(cpu
, part
);
1776 req
->end_io(req
, error
);
1778 if (blk_bidi_rq(req
))
1779 __blk_put_request(req
->next_rq
->q
, req
->next_rq
);
1781 __blk_put_request(req
->q
, req
);
1785 static inline void __end_request(struct request
*rq
, int uptodate
,
1786 unsigned int nr_bytes
)
1791 error
= uptodate
? uptodate
: -EIO
;
1793 __blk_end_request(rq
, error
, nr_bytes
);
1797 * blk_rq_bytes - Returns bytes left to complete in the entire request
1798 * @rq: the request being processed
1800 unsigned int blk_rq_bytes(struct request
*rq
)
1802 if (blk_fs_request(rq
))
1803 return rq
->hard_nr_sectors
<< 9;
1805 return rq
->data_len
;
1807 EXPORT_SYMBOL_GPL(blk_rq_bytes
);
1810 * blk_rq_cur_bytes - Returns bytes left to complete in the current segment
1811 * @rq: the request being processed
1813 unsigned int blk_rq_cur_bytes(struct request
*rq
)
1815 if (blk_fs_request(rq
))
1816 return rq
->current_nr_sectors
<< 9;
1819 return rq
->bio
->bi_size
;
1821 return rq
->data_len
;
1823 EXPORT_SYMBOL_GPL(blk_rq_cur_bytes
);
1826 * end_queued_request - end all I/O on a queued request
1827 * @rq: the request being processed
1828 * @uptodate: error value or %0/%1 uptodate flag
1831 * Ends all I/O on a request, and removes it from the block layer queues.
1832 * Not suitable for normal I/O completion, unless the driver still has
1833 * the request attached to the block layer.
1836 void end_queued_request(struct request
*rq
, int uptodate
)
1838 __end_request(rq
, uptodate
, blk_rq_bytes(rq
));
1840 EXPORT_SYMBOL(end_queued_request
);
1843 * end_dequeued_request - end all I/O on a dequeued request
1844 * @rq: the request being processed
1845 * @uptodate: error value or %0/%1 uptodate flag
1848 * Ends all I/O on a request. The request must already have been
1849 * dequeued using blkdev_dequeue_request(), as is normally the case
1853 void end_dequeued_request(struct request
*rq
, int uptodate
)
1855 __end_request(rq
, uptodate
, blk_rq_bytes(rq
));
1857 EXPORT_SYMBOL(end_dequeued_request
);
1861 * end_request - end I/O on the current segment of the request
1862 * @req: the request being processed
1863 * @uptodate: error value or %0/%1 uptodate flag
1866 * Ends I/O on the current segment of a request. If that is the only
1867 * remaining segment, the request is also completed and freed.
1869 * This is a remnant of how older block drivers handled I/O completions.
1870 * Modern drivers typically end I/O on the full request in one go, unless
1871 * they have a residual value to account for. For that case this function
1872 * isn't really useful, unless the residual just happens to be the
1873 * full current segment. In other words, don't use this function in new
1874 * code. Either use end_request_completely(), or the
1875 * end_that_request_chunk() (along with end_that_request_last()) for
1876 * partial completions.
1879 void end_request(struct request
*req
, int uptodate
)
1881 __end_request(req
, uptodate
, req
->hard_cur_sectors
<< 9);
1883 EXPORT_SYMBOL(end_request
);
1886 * blk_end_io - Generic end_io function to complete a request.
1887 * @rq: the request being processed
1888 * @error: %0 for success, < %0 for error
1889 * @nr_bytes: number of bytes to complete @rq
1890 * @bidi_bytes: number of bytes to complete @rq->next_rq
1891 * @drv_callback: function called between completion of bios in the request
1892 * and completion of the request.
1893 * If the callback returns non %0, this helper returns without
1894 * completion of the request.
1897 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
1898 * If @rq has leftover, sets it up for the next range of segments.
1901 * %0 - we are done with this request
1902 * %1 - this request is not freed yet, it still has pending buffers.
1904 static int blk_end_io(struct request
*rq
, int error
, unsigned int nr_bytes
,
1905 unsigned int bidi_bytes
,
1906 int (drv_callback
)(struct request
*))
1908 struct request_queue
*q
= rq
->q
;
1909 unsigned long flags
= 0UL;
1911 if (bio_has_data(rq
->bio
) || blk_discard_rq(rq
)) {
1912 if (__end_that_request_first(rq
, error
, nr_bytes
))
1915 /* Bidi request must be completed as a whole */
1916 if (blk_bidi_rq(rq
) &&
1917 __end_that_request_first(rq
->next_rq
, error
, bidi_bytes
))
1921 /* Special feature for tricky drivers */
1922 if (drv_callback
&& drv_callback(rq
))
1925 add_disk_randomness(rq
->rq_disk
);
1927 spin_lock_irqsave(q
->queue_lock
, flags
);
1928 end_that_request_last(rq
, error
);
1929 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1935 * blk_end_request - Helper function for drivers to complete the request.
1936 * @rq: the request being processed
1937 * @error: %0 for success, < %0 for error
1938 * @nr_bytes: number of bytes to complete
1941 * Ends I/O on a number of bytes attached to @rq.
1942 * If @rq has leftover, sets it up for the next range of segments.
1945 * %0 - we are done with this request
1946 * %1 - still buffers pending for this request
1948 int blk_end_request(struct request
*rq
, int error
, unsigned int nr_bytes
)
1950 return blk_end_io(rq
, error
, nr_bytes
, 0, NULL
);
1952 EXPORT_SYMBOL_GPL(blk_end_request
);
1955 * __blk_end_request - Helper function for drivers to complete the request.
1956 * @rq: the request being processed
1957 * @error: %0 for success, < %0 for error
1958 * @nr_bytes: number of bytes to complete
1961 * Must be called with queue lock held unlike blk_end_request().
1964 * %0 - we are done with this request
1965 * %1 - still buffers pending for this request
1967 int __blk_end_request(struct request
*rq
, int error
, unsigned int nr_bytes
)
1969 if ((bio_has_data(rq
->bio
) || blk_discard_rq(rq
)) &&
1970 __end_that_request_first(rq
, error
, nr_bytes
))
1973 add_disk_randomness(rq
->rq_disk
);
1975 end_that_request_last(rq
, error
);
1979 EXPORT_SYMBOL_GPL(__blk_end_request
);
1982 * blk_end_bidi_request - Helper function for drivers to complete bidi request.
1983 * @rq: the bidi request being processed
1984 * @error: %0 for success, < %0 for error
1985 * @nr_bytes: number of bytes to complete @rq
1986 * @bidi_bytes: number of bytes to complete @rq->next_rq
1989 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
1992 * %0 - we are done with this request
1993 * %1 - still buffers pending for this request
1995 int blk_end_bidi_request(struct request
*rq
, int error
, unsigned int nr_bytes
,
1996 unsigned int bidi_bytes
)
1998 return blk_end_io(rq
, error
, nr_bytes
, bidi_bytes
, NULL
);
2000 EXPORT_SYMBOL_GPL(blk_end_bidi_request
);
2003 * blk_end_request_callback - Special helper function for tricky drivers
2004 * @rq: the request being processed
2005 * @error: %0 for success, < %0 for error
2006 * @nr_bytes: number of bytes to complete
2007 * @drv_callback: function called between completion of bios in the request
2008 * and completion of the request.
2009 * If the callback returns non %0, this helper returns without
2010 * completion of the request.
2013 * Ends I/O on a number of bytes attached to @rq.
2014 * If @rq has leftover, sets it up for the next range of segments.
2016 * This special helper function is used only for existing tricky drivers.
2017 * (e.g. cdrom_newpc_intr() of ide-cd)
2018 * This interface will be removed when such drivers are rewritten.
2019 * Don't use this interface in other places anymore.
2022 * %0 - we are done with this request
2023 * %1 - this request is not freed yet.
2024 * this request still has pending buffers or
2025 * the driver doesn't want to finish this request yet.
2027 int blk_end_request_callback(struct request
*rq
, int error
,
2028 unsigned int nr_bytes
,
2029 int (drv_callback
)(struct request
*))
2031 return blk_end_io(rq
, error
, nr_bytes
, 0, drv_callback
);
2033 EXPORT_SYMBOL_GPL(blk_end_request_callback
);
2035 void blk_rq_bio_prep(struct request_queue
*q
, struct request
*rq
,
2038 /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw, and
2039 we want BIO_RW_AHEAD (bit 1) to imply REQ_FAILFAST (bit 1). */
2040 rq
->cmd_flags
|= (bio
->bi_rw
& 3);
2042 if (bio_has_data(bio
)) {
2043 rq
->nr_phys_segments
= bio_phys_segments(q
, bio
);
2044 rq
->buffer
= bio_data(bio
);
2046 rq
->current_nr_sectors
= bio_cur_sectors(bio
);
2047 rq
->hard_cur_sectors
= rq
->current_nr_sectors
;
2048 rq
->hard_nr_sectors
= rq
->nr_sectors
= bio_sectors(bio
);
2049 rq
->data_len
= bio
->bi_size
;
2051 rq
->bio
= rq
->biotail
= bio
;
2054 rq
->rq_disk
= bio
->bi_bdev
->bd_disk
;
2057 int kblockd_schedule_work(struct work_struct
*work
)
2059 return queue_work(kblockd_workqueue
, work
);
2061 EXPORT_SYMBOL(kblockd_schedule_work
);
2063 void kblockd_flush_work(struct work_struct
*work
)
2065 cancel_work_sync(work
);
2067 EXPORT_SYMBOL(kblockd_flush_work
);
2069 int __init
blk_dev_init(void)
2073 kblockd_workqueue
= create_workqueue("kblockd");
2074 if (!kblockd_workqueue
)
2075 panic("Failed to create kblockd\n");
2077 request_cachep
= kmem_cache_create("blkdev_requests",
2078 sizeof(struct request
), 0, SLAB_PANIC
, NULL
);
2080 blk_requestq_cachep
= kmem_cache_create("blkdev_queue",
2081 sizeof(struct request_queue
), 0, SLAB_PANIC
, NULL
);
2083 for_each_possible_cpu(i
)
2084 INIT_LIST_HEAD(&per_cpu(blk_cpu_done
, i
));
2086 open_softirq(BLOCK_SOFTIRQ
, blk_done_softirq
);
2087 register_hotcpu_notifier(&blk_cpu_notifier
);