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
= part_stat_lock();
65 part
= disk_map_sector_rcu(rq
->rq_disk
, rq
->sector
);
68 part_stat_inc(cpu
, part
, merges
[rw
]);
70 part_round_stats(cpu
, part
);
71 part_inc_in_flight(part
);
77 void blk_queue_congestion_threshold(struct request_queue
*q
)
81 nr
= q
->nr_requests
- (q
->nr_requests
/ 8) + 1;
82 if (nr
> q
->nr_requests
)
84 q
->nr_congestion_on
= nr
;
86 nr
= q
->nr_requests
- (q
->nr_requests
/ 8) - (q
->nr_requests
/ 16) - 1;
89 q
->nr_congestion_off
= nr
;
93 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
96 * Locates the passed device's request queue and returns the address of its
99 * Will return NULL if the request queue cannot be located.
101 struct backing_dev_info
*blk_get_backing_dev_info(struct block_device
*bdev
)
103 struct backing_dev_info
*ret
= NULL
;
104 struct request_queue
*q
= bdev_get_queue(bdev
);
107 ret
= &q
->backing_dev_info
;
110 EXPORT_SYMBOL(blk_get_backing_dev_info
);
112 void blk_rq_init(struct request_queue
*q
, struct request
*rq
)
114 memset(rq
, 0, sizeof(*rq
));
116 INIT_LIST_HEAD(&rq
->queuelist
);
117 INIT_LIST_HEAD(&rq
->donelist
);
119 rq
->sector
= rq
->hard_sector
= (sector_t
) -1;
120 INIT_HLIST_NODE(&rq
->hash
);
121 RB_CLEAR_NODE(&rq
->rb_node
);
126 EXPORT_SYMBOL(blk_rq_init
);
128 static void req_bio_endio(struct request
*rq
, struct bio
*bio
,
129 unsigned int nbytes
, int error
)
131 struct request_queue
*q
= rq
->q
;
133 if (&q
->bar_rq
!= rq
) {
135 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
136 else if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
139 if (unlikely(nbytes
> bio
->bi_size
)) {
140 printk(KERN_ERR
"%s: want %u bytes done, %u left\n",
141 __func__
, nbytes
, bio
->bi_size
);
142 nbytes
= bio
->bi_size
;
145 bio
->bi_size
-= nbytes
;
146 bio
->bi_sector
+= (nbytes
>> 9);
148 if (bio_integrity(bio
))
149 bio_integrity_advance(bio
, nbytes
);
151 if (bio
->bi_size
== 0)
152 bio_endio(bio
, error
);
156 * Okay, this is the barrier request in progress, just
159 if (error
&& !q
->orderr
)
164 void blk_dump_rq_flags(struct request
*rq
, char *msg
)
168 printk(KERN_INFO
"%s: dev %s: type=%x, flags=%x\n", msg
,
169 rq
->rq_disk
? rq
->rq_disk
->disk_name
: "?", rq
->cmd_type
,
172 printk(KERN_INFO
" sector %llu, nr/cnr %lu/%u\n",
173 (unsigned long long)rq
->sector
,
175 rq
->current_nr_sectors
);
176 printk(KERN_INFO
" bio %p, biotail %p, buffer %p, data %p, len %u\n",
177 rq
->bio
, rq
->biotail
,
178 rq
->buffer
, rq
->data
,
181 if (blk_pc_request(rq
)) {
182 printk(KERN_INFO
" cdb: ");
183 for (bit
= 0; bit
< BLK_MAX_CDB
; bit
++)
184 printk("%02x ", rq
->cmd
[bit
]);
188 EXPORT_SYMBOL(blk_dump_rq_flags
);
191 * "plug" the device if there are no outstanding requests: this will
192 * force the transfer to start only after we have put all the requests
195 * This is called with interrupts off and no requests on the queue and
196 * with the queue lock held.
198 void blk_plug_device(struct request_queue
*q
)
200 WARN_ON(!irqs_disabled());
203 * don't plug a stopped queue, it must be paired with blk_start_queue()
204 * which will restart the queueing
206 if (blk_queue_stopped(q
))
209 if (!queue_flag_test_and_set(QUEUE_FLAG_PLUGGED
, q
)) {
210 mod_timer(&q
->unplug_timer
, jiffies
+ q
->unplug_delay
);
211 blk_add_trace_generic(q
, NULL
, 0, BLK_TA_PLUG
);
214 EXPORT_SYMBOL(blk_plug_device
);
217 * blk_plug_device_unlocked - plug a device without queue lock held
218 * @q: The &struct request_queue to plug
221 * Like @blk_plug_device(), but grabs the queue lock and disables
224 void blk_plug_device_unlocked(struct request_queue
*q
)
228 spin_lock_irqsave(q
->queue_lock
, flags
);
230 spin_unlock_irqrestore(q
->queue_lock
, flags
);
232 EXPORT_SYMBOL(blk_plug_device_unlocked
);
235 * remove the queue from the plugged list, if present. called with
236 * queue lock held and interrupts disabled.
238 int blk_remove_plug(struct request_queue
*q
)
240 WARN_ON(!irqs_disabled());
242 if (!queue_flag_test_and_clear(QUEUE_FLAG_PLUGGED
, q
))
245 del_timer(&q
->unplug_timer
);
248 EXPORT_SYMBOL(blk_remove_plug
);
251 * remove the plug and let it rip..
253 void __generic_unplug_device(struct request_queue
*q
)
255 if (unlikely(blk_queue_stopped(q
)))
258 if (!blk_remove_plug(q
))
263 EXPORT_SYMBOL(__generic_unplug_device
);
266 * generic_unplug_device - fire a request queue
267 * @q: The &struct request_queue in question
270 * Linux uses plugging to build bigger requests queues before letting
271 * the device have at them. If a queue is plugged, the I/O scheduler
272 * is still adding and merging requests on the queue. Once the queue
273 * gets unplugged, the request_fn defined for the queue is invoked and
276 void generic_unplug_device(struct request_queue
*q
)
278 if (blk_queue_plugged(q
)) {
279 spin_lock_irq(q
->queue_lock
);
280 __generic_unplug_device(q
);
281 spin_unlock_irq(q
->queue_lock
);
284 EXPORT_SYMBOL(generic_unplug_device
);
286 static void blk_backing_dev_unplug(struct backing_dev_info
*bdi
,
289 struct request_queue
*q
= bdi
->unplug_io_data
;
294 void blk_unplug_work(struct work_struct
*work
)
296 struct request_queue
*q
=
297 container_of(work
, struct request_queue
, unplug_work
);
299 blk_add_trace_pdu_int(q
, BLK_TA_UNPLUG_IO
, NULL
,
300 q
->rq
.count
[READ
] + q
->rq
.count
[WRITE
]);
305 void blk_unplug_timeout(unsigned long data
)
307 struct request_queue
*q
= (struct request_queue
*)data
;
309 blk_add_trace_pdu_int(q
, BLK_TA_UNPLUG_TIMER
, NULL
,
310 q
->rq
.count
[READ
] + q
->rq
.count
[WRITE
]);
312 kblockd_schedule_work(&q
->unplug_work
);
315 void blk_unplug(struct request_queue
*q
)
318 * devices don't necessarily have an ->unplug_fn defined
321 blk_add_trace_pdu_int(q
, BLK_TA_UNPLUG_IO
, NULL
,
322 q
->rq
.count
[READ
] + q
->rq
.count
[WRITE
]);
327 EXPORT_SYMBOL(blk_unplug
);
330 * blk_start_queue - restart a previously stopped queue
331 * @q: The &struct request_queue in question
334 * blk_start_queue() will clear the stop flag on the queue, and call
335 * the request_fn for the queue if it was in a stopped state when
336 * entered. Also see blk_stop_queue(). Queue lock must be held.
338 void blk_start_queue(struct request_queue
*q
)
340 WARN_ON(!irqs_disabled());
342 queue_flag_clear(QUEUE_FLAG_STOPPED
, q
);
345 * one level of recursion is ok and is much faster than kicking
346 * the unplug handling
348 if (!queue_flag_test_and_set(QUEUE_FLAG_REENTER
, q
)) {
350 queue_flag_clear(QUEUE_FLAG_REENTER
, q
);
353 kblockd_schedule_work(&q
->unplug_work
);
356 EXPORT_SYMBOL(blk_start_queue
);
359 * blk_stop_queue - stop a queue
360 * @q: The &struct request_queue in question
363 * The Linux block layer assumes that a block driver will consume all
364 * entries on the request queue when the request_fn strategy is called.
365 * Often this will not happen, because of hardware limitations (queue
366 * depth settings). If a device driver gets a 'queue full' response,
367 * or if it simply chooses not to queue more I/O at one point, it can
368 * call this function to prevent the request_fn from being called until
369 * the driver has signalled it's ready to go again. This happens by calling
370 * blk_start_queue() to restart queue operations. Queue lock must be held.
372 void blk_stop_queue(struct request_queue
*q
)
375 queue_flag_set(QUEUE_FLAG_STOPPED
, q
);
377 EXPORT_SYMBOL(blk_stop_queue
);
380 * blk_sync_queue - cancel any pending callbacks on a queue
384 * The block layer may perform asynchronous callback activity
385 * on a queue, such as calling the unplug function after a timeout.
386 * A block device may call blk_sync_queue to ensure that any
387 * such activity is cancelled, thus allowing it to release resources
388 * that the callbacks might use. The caller must already have made sure
389 * that its ->make_request_fn will not re-add plugging prior to calling
393 void blk_sync_queue(struct request_queue
*q
)
395 del_timer_sync(&q
->unplug_timer
);
396 kblockd_flush_work(&q
->unplug_work
);
398 EXPORT_SYMBOL(blk_sync_queue
);
401 * blk_run_queue - run a single device queue
402 * @q: The queue to run
404 void __blk_run_queue(struct request_queue
*q
)
409 * Only recurse once to avoid overrunning the stack, let the unplug
410 * handling reinvoke the handler shortly if we already got there.
412 if (!elv_queue_empty(q
)) {
413 if (!queue_flag_test_and_set(QUEUE_FLAG_REENTER
, q
)) {
415 queue_flag_clear(QUEUE_FLAG_REENTER
, q
);
418 kblockd_schedule_work(&q
->unplug_work
);
422 EXPORT_SYMBOL(__blk_run_queue
);
425 * blk_run_queue - run a single device queue
426 * @q: The queue to run
428 void blk_run_queue(struct request_queue
*q
)
432 spin_lock_irqsave(q
->queue_lock
, flags
);
434 spin_unlock_irqrestore(q
->queue_lock
, flags
);
436 EXPORT_SYMBOL(blk_run_queue
);
438 void blk_put_queue(struct request_queue
*q
)
440 kobject_put(&q
->kobj
);
443 void blk_cleanup_queue(struct request_queue
*q
)
445 mutex_lock(&q
->sysfs_lock
);
446 queue_flag_set_unlocked(QUEUE_FLAG_DEAD
, q
);
447 mutex_unlock(&q
->sysfs_lock
);
450 elevator_exit(q
->elevator
);
454 EXPORT_SYMBOL(blk_cleanup_queue
);
456 static int blk_init_free_list(struct request_queue
*q
)
458 struct request_list
*rl
= &q
->rq
;
460 rl
->count
[READ
] = rl
->count
[WRITE
] = 0;
461 rl
->starved
[READ
] = rl
->starved
[WRITE
] = 0;
463 init_waitqueue_head(&rl
->wait
[READ
]);
464 init_waitqueue_head(&rl
->wait
[WRITE
]);
466 rl
->rq_pool
= mempool_create_node(BLKDEV_MIN_RQ
, mempool_alloc_slab
,
467 mempool_free_slab
, request_cachep
, q
->node
);
475 struct request_queue
*blk_alloc_queue(gfp_t gfp_mask
)
477 return blk_alloc_queue_node(gfp_mask
, -1);
479 EXPORT_SYMBOL(blk_alloc_queue
);
481 struct request_queue
*blk_alloc_queue_node(gfp_t gfp_mask
, int node_id
)
483 struct request_queue
*q
;
486 q
= kmem_cache_alloc_node(blk_requestq_cachep
,
487 gfp_mask
| __GFP_ZERO
, node_id
);
491 q
->backing_dev_info
.unplug_io_fn
= blk_backing_dev_unplug
;
492 q
->backing_dev_info
.unplug_io_data
= q
;
493 err
= bdi_init(&q
->backing_dev_info
);
495 kmem_cache_free(blk_requestq_cachep
, q
);
499 init_timer(&q
->unplug_timer
);
501 kobject_init(&q
->kobj
, &blk_queue_ktype
);
503 mutex_init(&q
->sysfs_lock
);
504 spin_lock_init(&q
->__queue_lock
);
508 EXPORT_SYMBOL(blk_alloc_queue_node
);
511 * blk_init_queue - prepare a request queue for use with a block device
512 * @rfn: The function to be called to process requests that have been
513 * placed on the queue.
514 * @lock: Request queue spin lock
517 * If a block device wishes to use the standard request handling procedures,
518 * which sorts requests and coalesces adjacent requests, then it must
519 * call blk_init_queue(). The function @rfn will be called when there
520 * are requests on the queue that need to be processed. If the device
521 * supports plugging, then @rfn may not be called immediately when requests
522 * are available on the queue, but may be called at some time later instead.
523 * Plugged queues are generally unplugged when a buffer belonging to one
524 * of the requests on the queue is needed, or due to memory pressure.
526 * @rfn is not required, or even expected, to remove all requests off the
527 * queue, but only as many as it can handle at a time. If it does leave
528 * requests on the queue, it is responsible for arranging that the requests
529 * get dealt with eventually.
531 * The queue spin lock must be held while manipulating the requests on the
532 * request queue; this lock will be taken also from interrupt context, so irq
533 * disabling is needed for it.
535 * Function returns a pointer to the initialized request queue, or %NULL if
539 * blk_init_queue() must be paired with a blk_cleanup_queue() call
540 * when the block device is deactivated (such as at module unload).
543 struct request_queue
*blk_init_queue(request_fn_proc
*rfn
, spinlock_t
*lock
)
545 return blk_init_queue_node(rfn
, lock
, -1);
547 EXPORT_SYMBOL(blk_init_queue
);
549 struct request_queue
*
550 blk_init_queue_node(request_fn_proc
*rfn
, spinlock_t
*lock
, int node_id
)
552 struct request_queue
*q
= blk_alloc_queue_node(GFP_KERNEL
, node_id
);
558 if (blk_init_free_list(q
)) {
559 kmem_cache_free(blk_requestq_cachep
, q
);
564 * if caller didn't supply a lock, they get per-queue locking with
568 lock
= &q
->__queue_lock
;
571 q
->prep_rq_fn
= NULL
;
572 q
->unplug_fn
= generic_unplug_device
;
573 q
->queue_flags
= (1 << QUEUE_FLAG_CLUSTER
);
574 q
->queue_lock
= lock
;
576 blk_queue_segment_boundary(q
, 0xffffffff);
578 blk_queue_make_request(q
, __make_request
);
579 blk_queue_max_segment_size(q
, MAX_SEGMENT_SIZE
);
581 blk_queue_max_hw_segments(q
, MAX_HW_SEGMENTS
);
582 blk_queue_max_phys_segments(q
, MAX_PHYS_SEGMENTS
);
584 q
->sg_reserved_size
= INT_MAX
;
586 blk_set_cmd_filter_defaults(&q
->cmd_filter
);
591 if (!elevator_init(q
, NULL
)) {
592 blk_queue_congestion_threshold(q
);
599 EXPORT_SYMBOL(blk_init_queue_node
);
601 int blk_get_queue(struct request_queue
*q
)
603 if (likely(!test_bit(QUEUE_FLAG_DEAD
, &q
->queue_flags
))) {
604 kobject_get(&q
->kobj
);
611 static inline void blk_free_request(struct request_queue
*q
, struct request
*rq
)
613 if (rq
->cmd_flags
& REQ_ELVPRIV
)
614 elv_put_request(q
, rq
);
615 mempool_free(rq
, q
->rq
.rq_pool
);
618 static struct request
*
619 blk_alloc_request(struct request_queue
*q
, int rw
, int priv
, gfp_t gfp_mask
)
621 struct request
*rq
= mempool_alloc(q
->rq
.rq_pool
, gfp_mask
);
628 rq
->cmd_flags
= rw
| REQ_ALLOCED
;
631 if (unlikely(elv_set_request(q
, rq
, gfp_mask
))) {
632 mempool_free(rq
, q
->rq
.rq_pool
);
635 rq
->cmd_flags
|= REQ_ELVPRIV
;
642 * ioc_batching returns true if the ioc is a valid batching request and
643 * should be given priority access to a request.
645 static inline int ioc_batching(struct request_queue
*q
, struct io_context
*ioc
)
651 * Make sure the process is able to allocate at least 1 request
652 * even if the batch times out, otherwise we could theoretically
655 return ioc
->nr_batch_requests
== q
->nr_batching
||
656 (ioc
->nr_batch_requests
> 0
657 && time_before(jiffies
, ioc
->last_waited
+ BLK_BATCH_TIME
));
661 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
662 * will cause the process to be a "batcher" on all queues in the system. This
663 * is the behaviour we want though - once it gets a wakeup it should be given
666 static void ioc_set_batching(struct request_queue
*q
, struct io_context
*ioc
)
668 if (!ioc
|| ioc_batching(q
, ioc
))
671 ioc
->nr_batch_requests
= q
->nr_batching
;
672 ioc
->last_waited
= jiffies
;
675 static void __freed_request(struct request_queue
*q
, int rw
)
677 struct request_list
*rl
= &q
->rq
;
679 if (rl
->count
[rw
] < queue_congestion_off_threshold(q
))
680 blk_clear_queue_congested(q
, rw
);
682 if (rl
->count
[rw
] + 1 <= q
->nr_requests
) {
683 if (waitqueue_active(&rl
->wait
[rw
]))
684 wake_up(&rl
->wait
[rw
]);
686 blk_clear_queue_full(q
, rw
);
691 * A request has just been released. Account for it, update the full and
692 * congestion status, wake up any waiters. Called under q->queue_lock.
694 static void freed_request(struct request_queue
*q
, int rw
, int priv
)
696 struct request_list
*rl
= &q
->rq
;
702 __freed_request(q
, rw
);
704 if (unlikely(rl
->starved
[rw
^ 1]))
705 __freed_request(q
, rw
^ 1);
708 #define blkdev_free_rq(list) list_entry((list)->next, struct request, queuelist)
710 * Get a free request, queue_lock must be held.
711 * Returns NULL on failure, with queue_lock held.
712 * Returns !NULL on success, with queue_lock *not held*.
714 static struct request
*get_request(struct request_queue
*q
, int rw_flags
,
715 struct bio
*bio
, gfp_t gfp_mask
)
717 struct request
*rq
= NULL
;
718 struct request_list
*rl
= &q
->rq
;
719 struct io_context
*ioc
= NULL
;
720 const int rw
= rw_flags
& 0x01;
723 may_queue
= elv_may_queue(q
, rw_flags
);
724 if (may_queue
== ELV_MQUEUE_NO
)
727 if (rl
->count
[rw
]+1 >= queue_congestion_on_threshold(q
)) {
728 if (rl
->count
[rw
]+1 >= q
->nr_requests
) {
729 ioc
= current_io_context(GFP_ATOMIC
, q
->node
);
731 * The queue will fill after this allocation, so set
732 * it as full, and mark this process as "batching".
733 * This process will be allowed to complete a batch of
734 * requests, others will be blocked.
736 if (!blk_queue_full(q
, rw
)) {
737 ioc_set_batching(q
, ioc
);
738 blk_set_queue_full(q
, rw
);
740 if (may_queue
!= ELV_MQUEUE_MUST
741 && !ioc_batching(q
, ioc
)) {
743 * The queue is full and the allocating
744 * process is not a "batcher", and not
745 * exempted by the IO scheduler
751 blk_set_queue_congested(q
, rw
);
755 * Only allow batching queuers to allocate up to 50% over the defined
756 * limit of requests, otherwise we could have thousands of requests
757 * allocated with any setting of ->nr_requests
759 if (rl
->count
[rw
] >= (3 * q
->nr_requests
/ 2))
765 priv
= !test_bit(QUEUE_FLAG_ELVSWITCH
, &q
->queue_flags
);
769 spin_unlock_irq(q
->queue_lock
);
771 rq
= blk_alloc_request(q
, rw_flags
, priv
, gfp_mask
);
774 * Allocation failed presumably due to memory. Undo anything
775 * we might have messed up.
777 * Allocating task should really be put onto the front of the
778 * wait queue, but this is pretty rare.
780 spin_lock_irq(q
->queue_lock
);
781 freed_request(q
, rw
, priv
);
784 * in the very unlikely event that allocation failed and no
785 * requests for this direction was pending, mark us starved
786 * so that freeing of a request in the other direction will
787 * notice us. another possible fix would be to split the
788 * rq mempool into READ and WRITE
791 if (unlikely(rl
->count
[rw
] == 0))
798 * ioc may be NULL here, and ioc_batching will be false. That's
799 * OK, if the queue is under the request limit then requests need
800 * not count toward the nr_batch_requests limit. There will always
801 * be some limit enforced by BLK_BATCH_TIME.
803 if (ioc_batching(q
, ioc
))
804 ioc
->nr_batch_requests
--;
806 blk_add_trace_generic(q
, bio
, rw
, BLK_TA_GETRQ
);
812 * No available requests for this queue, unplug the device and wait for some
813 * requests to become available.
815 * Called with q->queue_lock held, and returns with it unlocked.
817 static struct request
*get_request_wait(struct request_queue
*q
, int rw_flags
,
820 const int rw
= rw_flags
& 0x01;
823 rq
= get_request(q
, rw_flags
, bio
, GFP_NOIO
);
826 struct io_context
*ioc
;
827 struct request_list
*rl
= &q
->rq
;
829 prepare_to_wait_exclusive(&rl
->wait
[rw
], &wait
,
830 TASK_UNINTERRUPTIBLE
);
832 blk_add_trace_generic(q
, bio
, rw
, BLK_TA_SLEEPRQ
);
834 __generic_unplug_device(q
);
835 spin_unlock_irq(q
->queue_lock
);
839 * After sleeping, we become a "batching" process and
840 * will be able to allocate at least one request, and
841 * up to a big batch of them for a small period time.
842 * See ioc_batching, ioc_set_batching
844 ioc
= current_io_context(GFP_NOIO
, q
->node
);
845 ioc_set_batching(q
, ioc
);
847 spin_lock_irq(q
->queue_lock
);
848 finish_wait(&rl
->wait
[rw
], &wait
);
850 rq
= get_request(q
, rw_flags
, bio
, GFP_NOIO
);
856 struct request
*blk_get_request(struct request_queue
*q
, int rw
, gfp_t gfp_mask
)
860 BUG_ON(rw
!= READ
&& rw
!= WRITE
);
862 spin_lock_irq(q
->queue_lock
);
863 if (gfp_mask
& __GFP_WAIT
) {
864 rq
= get_request_wait(q
, rw
, NULL
);
866 rq
= get_request(q
, rw
, NULL
, gfp_mask
);
868 spin_unlock_irq(q
->queue_lock
);
870 /* q->queue_lock is unlocked at this point */
874 EXPORT_SYMBOL(blk_get_request
);
877 * blk_start_queueing - initiate dispatch of requests to device
878 * @q: request queue to kick into gear
880 * This is basically a helper to remove the need to know whether a queue
881 * is plugged or not if someone just wants to initiate dispatch of requests
884 * The queue lock must be held with interrupts disabled.
886 void blk_start_queueing(struct request_queue
*q
)
888 if (!blk_queue_plugged(q
))
891 __generic_unplug_device(q
);
893 EXPORT_SYMBOL(blk_start_queueing
);
896 * blk_requeue_request - put a request back on queue
897 * @q: request queue where request should be inserted
898 * @rq: request to be inserted
901 * Drivers often keep queueing requests until the hardware cannot accept
902 * more, when that condition happens we need to put the request back
903 * on the queue. Must be called with queue lock held.
905 void blk_requeue_request(struct request_queue
*q
, struct request
*rq
)
907 blk_add_trace_rq(q
, rq
, BLK_TA_REQUEUE
);
909 if (blk_rq_tagged(rq
))
910 blk_queue_end_tag(q
, rq
);
912 elv_requeue_request(q
, rq
);
914 EXPORT_SYMBOL(blk_requeue_request
);
917 * blk_insert_request - insert a special request into a request queue
918 * @q: request queue where request should be inserted
919 * @rq: request to be inserted
920 * @at_head: insert request at head or tail of queue
921 * @data: private data
924 * Many block devices need to execute commands asynchronously, so they don't
925 * block the whole kernel from preemption during request execution. This is
926 * accomplished normally by inserting aritficial requests tagged as
927 * REQ_TYPE_SPECIAL in to the corresponding request queue, and letting them
928 * be scheduled for actual execution by the request queue.
930 * We have the option of inserting the head or the tail of the queue.
931 * Typically we use the tail for new ioctls and so forth. We use the head
932 * of the queue for things like a QUEUE_FULL message from a device, or a
933 * host that is unable to accept a particular command.
935 void blk_insert_request(struct request_queue
*q
, struct request
*rq
,
936 int at_head
, void *data
)
938 int where
= at_head
? ELEVATOR_INSERT_FRONT
: ELEVATOR_INSERT_BACK
;
942 * tell I/O scheduler that this isn't a regular read/write (ie it
943 * must not attempt merges on this) and that it acts as a soft
946 rq
->cmd_type
= REQ_TYPE_SPECIAL
;
947 rq
->cmd_flags
|= REQ_SOFTBARRIER
;
951 spin_lock_irqsave(q
->queue_lock
, flags
);
954 * If command is tagged, release the tag
956 if (blk_rq_tagged(rq
))
957 blk_queue_end_tag(q
, rq
);
959 drive_stat_acct(rq
, 1);
960 __elv_add_request(q
, rq
, where
, 0);
961 blk_start_queueing(q
);
962 spin_unlock_irqrestore(q
->queue_lock
, flags
);
964 EXPORT_SYMBOL(blk_insert_request
);
967 * add-request adds a request to the linked list.
968 * queue lock is held and interrupts disabled, as we muck with the
969 * request queue list.
971 static inline void add_request(struct request_queue
*q
, struct request
*req
)
973 drive_stat_acct(req
, 1);
976 * elevator indicated where it wants this request to be
977 * inserted at elevator_merge time
979 __elv_add_request(q
, req
, ELEVATOR_INSERT_SORT
, 0);
982 static void part_round_stats_single(int cpu
, struct hd_struct
*part
,
985 if (now
== part
->stamp
)
988 if (part
->in_flight
) {
989 __part_stat_add(cpu
, part
, time_in_queue
,
990 part
->in_flight
* (now
- part
->stamp
));
991 __part_stat_add(cpu
, part
, io_ticks
, (now
- part
->stamp
));
997 * part_round_stats() - Round off the performance stats on a struct
1000 * The average IO queue length and utilisation statistics are maintained
1001 * by observing the current state of the queue length and the amount of
1002 * time it has been in this state for.
1004 * Normally, that accounting is done on IO completion, but that can result
1005 * in more than a second's worth of IO being accounted for within any one
1006 * second, leading to >100% utilisation. To deal with that, we call this
1007 * function to do a round-off before returning the results when reading
1008 * /proc/diskstats. This accounts immediately for all queue usage up to
1009 * the current jiffies and restarts the counters again.
1011 void part_round_stats(int cpu
, struct hd_struct
*part
)
1013 unsigned long now
= jiffies
;
1016 part_round_stats_single(cpu
, &part_to_disk(part
)->part0
, now
);
1017 part_round_stats_single(cpu
, part
, now
);
1019 EXPORT_SYMBOL_GPL(part_round_stats
);
1022 * queue lock must be held
1024 void __blk_put_request(struct request_queue
*q
, struct request
*req
)
1028 if (unlikely(--req
->ref_count
))
1031 elv_completed_request(q
, req
);
1034 * Request may not have originated from ll_rw_blk. if not,
1035 * it didn't come out of our reserved rq pools
1037 if (req
->cmd_flags
& REQ_ALLOCED
) {
1038 int rw
= rq_data_dir(req
);
1039 int priv
= req
->cmd_flags
& REQ_ELVPRIV
;
1041 BUG_ON(!list_empty(&req
->queuelist
));
1042 BUG_ON(!hlist_unhashed(&req
->hash
));
1044 blk_free_request(q
, req
);
1045 freed_request(q
, rw
, priv
);
1048 EXPORT_SYMBOL_GPL(__blk_put_request
);
1050 void blk_put_request(struct request
*req
)
1052 unsigned long flags
;
1053 struct request_queue
*q
= req
->q
;
1055 spin_lock_irqsave(q
->queue_lock
, flags
);
1056 __blk_put_request(q
, req
);
1057 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1059 EXPORT_SYMBOL(blk_put_request
);
1061 void init_request_from_bio(struct request
*req
, struct bio
*bio
)
1063 req
->cmd_type
= REQ_TYPE_FS
;
1066 * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
1068 if (bio_rw_ahead(bio
) || bio_failfast(bio
))
1069 req
->cmd_flags
|= REQ_FAILFAST
;
1072 * REQ_BARRIER implies no merging, but lets make it explicit
1074 if (unlikely(bio_discard(bio
))) {
1075 req
->cmd_flags
|= REQ_DISCARD
;
1076 if (bio_barrier(bio
))
1077 req
->cmd_flags
|= REQ_SOFTBARRIER
;
1078 req
->q
->prepare_discard_fn(req
->q
, req
);
1079 } else if (unlikely(bio_barrier(bio
)))
1080 req
->cmd_flags
|= (REQ_HARDBARRIER
| REQ_NOMERGE
);
1083 req
->cmd_flags
|= REQ_RW_SYNC
;
1084 if (bio_rw_meta(bio
))
1085 req
->cmd_flags
|= REQ_RW_META
;
1088 req
->hard_sector
= req
->sector
= bio
->bi_sector
;
1089 req
->ioprio
= bio_prio(bio
);
1090 req
->start_time
= jiffies
;
1091 blk_rq_bio_prep(req
->q
, req
, bio
);
1094 static int __make_request(struct request_queue
*q
, struct bio
*bio
)
1096 struct request
*req
;
1097 int el_ret
, nr_sectors
, barrier
, discard
, err
;
1098 const unsigned short prio
= bio_prio(bio
);
1099 const int sync
= bio_sync(bio
);
1102 nr_sectors
= bio_sectors(bio
);
1105 * low level driver can indicate that it wants pages above a
1106 * certain limit bounced to low memory (ie for highmem, or even
1107 * ISA dma in theory)
1109 blk_queue_bounce(q
, &bio
);
1111 barrier
= bio_barrier(bio
);
1112 if (unlikely(barrier
) && bio_has_data(bio
) &&
1113 (q
->next_ordered
== QUEUE_ORDERED_NONE
)) {
1118 discard
= bio_discard(bio
);
1119 if (unlikely(discard
) && !q
->prepare_discard_fn
) {
1124 spin_lock_irq(q
->queue_lock
);
1126 if (unlikely(barrier
) || elv_queue_empty(q
))
1129 el_ret
= elv_merge(q
, &req
, bio
);
1131 case ELEVATOR_BACK_MERGE
:
1132 BUG_ON(!rq_mergeable(req
));
1134 if (!ll_back_merge_fn(q
, req
, bio
))
1137 blk_add_trace_bio(q
, bio
, BLK_TA_BACKMERGE
);
1139 req
->biotail
->bi_next
= bio
;
1141 req
->nr_sectors
= req
->hard_nr_sectors
+= nr_sectors
;
1142 req
->ioprio
= ioprio_best(req
->ioprio
, prio
);
1143 drive_stat_acct(req
, 0);
1144 if (!attempt_back_merge(q
, req
))
1145 elv_merged_request(q
, req
, el_ret
);
1148 case ELEVATOR_FRONT_MERGE
:
1149 BUG_ON(!rq_mergeable(req
));
1151 if (!ll_front_merge_fn(q
, req
, bio
))
1154 blk_add_trace_bio(q
, bio
, BLK_TA_FRONTMERGE
);
1156 bio
->bi_next
= req
->bio
;
1160 * may not be valid. if the low level driver said
1161 * it didn't need a bounce buffer then it better
1162 * not touch req->buffer either...
1164 req
->buffer
= bio_data(bio
);
1165 req
->current_nr_sectors
= bio_cur_sectors(bio
);
1166 req
->hard_cur_sectors
= req
->current_nr_sectors
;
1167 req
->sector
= req
->hard_sector
= bio
->bi_sector
;
1168 req
->nr_sectors
= req
->hard_nr_sectors
+= nr_sectors
;
1169 req
->ioprio
= ioprio_best(req
->ioprio
, prio
);
1170 drive_stat_acct(req
, 0);
1171 if (!attempt_front_merge(q
, req
))
1172 elv_merged_request(q
, req
, el_ret
);
1175 /* ELV_NO_MERGE: elevator says don't/can't merge. */
1182 * This sync check and mask will be re-done in init_request_from_bio(),
1183 * but we need to set it earlier to expose the sync flag to the
1184 * rq allocator and io schedulers.
1186 rw_flags
= bio_data_dir(bio
);
1188 rw_flags
|= REQ_RW_SYNC
;
1191 * Grab a free request. This is might sleep but can not fail.
1192 * Returns with the queue unlocked.
1194 req
= get_request_wait(q
, rw_flags
, bio
);
1197 * After dropping the lock and possibly sleeping here, our request
1198 * may now be mergeable after it had proven unmergeable (above).
1199 * We don't worry about that case for efficiency. It won't happen
1200 * often, and the elevators are able to handle it.
1202 init_request_from_bio(req
, bio
);
1204 spin_lock_irq(q
->queue_lock
);
1205 if (elv_queue_empty(q
))
1207 add_request(q
, req
);
1210 __generic_unplug_device(q
);
1212 spin_unlock_irq(q
->queue_lock
);
1216 bio_endio(bio
, err
);
1221 * If bio->bi_dev is a partition, remap the location
1223 static inline void blk_partition_remap(struct bio
*bio
)
1225 struct block_device
*bdev
= bio
->bi_bdev
;
1227 if (bio_sectors(bio
) && bdev
!= bdev
->bd_contains
) {
1228 struct hd_struct
*p
= bdev
->bd_part
;
1230 bio
->bi_sector
+= p
->start_sect
;
1231 bio
->bi_bdev
= bdev
->bd_contains
;
1233 blk_add_trace_remap(bdev_get_queue(bio
->bi_bdev
), bio
,
1234 bdev
->bd_dev
, bio
->bi_sector
,
1235 bio
->bi_sector
- p
->start_sect
);
1239 static void handle_bad_sector(struct bio
*bio
)
1241 char b
[BDEVNAME_SIZE
];
1243 printk(KERN_INFO
"attempt to access beyond end of device\n");
1244 printk(KERN_INFO
"%s: rw=%ld, want=%Lu, limit=%Lu\n",
1245 bdevname(bio
->bi_bdev
, b
),
1247 (unsigned long long)bio
->bi_sector
+ bio_sectors(bio
),
1248 (long long)(bio
->bi_bdev
->bd_inode
->i_size
>> 9));
1250 set_bit(BIO_EOF
, &bio
->bi_flags
);
1253 #ifdef CONFIG_FAIL_MAKE_REQUEST
1255 static DECLARE_FAULT_ATTR(fail_make_request
);
1257 static int __init
setup_fail_make_request(char *str
)
1259 return setup_fault_attr(&fail_make_request
, str
);
1261 __setup("fail_make_request=", setup_fail_make_request
);
1263 static int should_fail_request(struct bio
*bio
)
1265 struct hd_struct
*part
= bio
->bi_bdev
->bd_part
;
1267 if (part_to_disk(part
)->part0
.make_it_fail
|| part
->make_it_fail
)
1268 return should_fail(&fail_make_request
, bio
->bi_size
);
1273 static int __init
fail_make_request_debugfs(void)
1275 return init_fault_attr_dentries(&fail_make_request
,
1276 "fail_make_request");
1279 late_initcall(fail_make_request_debugfs
);
1281 #else /* CONFIG_FAIL_MAKE_REQUEST */
1283 static inline int should_fail_request(struct bio
*bio
)
1288 #endif /* CONFIG_FAIL_MAKE_REQUEST */
1291 * Check whether this bio extends beyond the end of the device.
1293 static inline int bio_check_eod(struct bio
*bio
, unsigned int nr_sectors
)
1300 /* Test device or partition size, when known. */
1301 maxsector
= bio
->bi_bdev
->bd_inode
->i_size
>> 9;
1303 sector_t sector
= bio
->bi_sector
;
1305 if (maxsector
< nr_sectors
|| maxsector
- nr_sectors
< sector
) {
1307 * This may well happen - the kernel calls bread()
1308 * without checking the size of the device, e.g., when
1309 * mounting a device.
1311 handle_bad_sector(bio
);
1320 * generic_make_request - hand a buffer to its device driver for I/O
1321 * @bio: The bio describing the location in memory and on the device.
1323 * generic_make_request() is used to make I/O requests of block
1324 * devices. It is passed a &struct bio, which describes the I/O that needs
1327 * generic_make_request() does not return any status. The
1328 * success/failure status of the request, along with notification of
1329 * completion, is delivered asynchronously through the bio->bi_end_io
1330 * function described (one day) else where.
1332 * The caller of generic_make_request must make sure that bi_io_vec
1333 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1334 * set to describe the device address, and the
1335 * bi_end_io and optionally bi_private are set to describe how
1336 * completion notification should be signaled.
1338 * generic_make_request and the drivers it calls may use bi_next if this
1339 * bio happens to be merged with someone else, and may change bi_dev and
1340 * bi_sector for remaps as it sees fit. So the values of these fields
1341 * should NOT be depended on after the call to generic_make_request.
1343 static inline void __generic_make_request(struct bio
*bio
)
1345 struct request_queue
*q
;
1346 sector_t old_sector
;
1347 int ret
, nr_sectors
= bio_sectors(bio
);
1353 if (bio_check_eod(bio
, nr_sectors
))
1357 * Resolve the mapping until finished. (drivers are
1358 * still free to implement/resolve their own stacking
1359 * by explicitly returning 0)
1361 * NOTE: we don't repeat the blk_size check for each new device.
1362 * Stacking drivers are expected to know what they are doing.
1367 char b
[BDEVNAME_SIZE
];
1369 q
= bdev_get_queue(bio
->bi_bdev
);
1372 "generic_make_request: Trying to access "
1373 "nonexistent block-device %s (%Lu)\n",
1374 bdevname(bio
->bi_bdev
, b
),
1375 (long long) bio
->bi_sector
);
1377 bio_endio(bio
, err
);
1381 if (unlikely(nr_sectors
> q
->max_hw_sectors
)) {
1382 printk(KERN_ERR
"bio too big device %s (%u > %u)\n",
1383 bdevname(bio
->bi_bdev
, b
),
1389 if (unlikely(test_bit(QUEUE_FLAG_DEAD
, &q
->queue_flags
)))
1392 if (should_fail_request(bio
))
1396 * If this device has partitions, remap block n
1397 * of partition p to block n+start(p) of the disk.
1399 blk_partition_remap(bio
);
1401 if (bio_integrity_enabled(bio
) && bio_integrity_prep(bio
))
1404 if (old_sector
!= -1)
1405 blk_add_trace_remap(q
, bio
, old_dev
, bio
->bi_sector
,
1408 blk_add_trace_bio(q
, bio
, BLK_TA_QUEUE
);
1410 old_sector
= bio
->bi_sector
;
1411 old_dev
= bio
->bi_bdev
->bd_dev
;
1413 if (bio_check_eod(bio
, nr_sectors
))
1415 if ((bio_empty_barrier(bio
) && !q
->prepare_flush_fn
) ||
1416 (bio_discard(bio
) && !q
->prepare_discard_fn
)) {
1421 ret
= q
->make_request_fn(q
, bio
);
1426 * We only want one ->make_request_fn to be active at a time,
1427 * else stack usage with stacked devices could be a problem.
1428 * So use current->bio_{list,tail} to keep a list of requests
1429 * submited by a make_request_fn function.
1430 * current->bio_tail is also used as a flag to say if
1431 * generic_make_request is currently active in this task or not.
1432 * If it is NULL, then no make_request is active. If it is non-NULL,
1433 * then a make_request is active, and new requests should be added
1436 void generic_make_request(struct bio
*bio
)
1438 if (current
->bio_tail
) {
1439 /* make_request is active */
1440 *(current
->bio_tail
) = bio
;
1441 bio
->bi_next
= NULL
;
1442 current
->bio_tail
= &bio
->bi_next
;
1445 /* following loop may be a bit non-obvious, and so deserves some
1447 * Before entering the loop, bio->bi_next is NULL (as all callers
1448 * ensure that) so we have a list with a single bio.
1449 * We pretend that we have just taken it off a longer list, so
1450 * we assign bio_list to the next (which is NULL) and bio_tail
1451 * to &bio_list, thus initialising the bio_list of new bios to be
1452 * added. __generic_make_request may indeed add some more bios
1453 * through a recursive call to generic_make_request. If it
1454 * did, we find a non-NULL value in bio_list and re-enter the loop
1455 * from the top. In this case we really did just take the bio
1456 * of the top of the list (no pretending) and so fixup bio_list and
1457 * bio_tail or bi_next, and call into __generic_make_request again.
1459 * The loop was structured like this to make only one call to
1460 * __generic_make_request (which is important as it is large and
1461 * inlined) and to keep the structure simple.
1463 BUG_ON(bio
->bi_next
);
1465 current
->bio_list
= bio
->bi_next
;
1466 if (bio
->bi_next
== NULL
)
1467 current
->bio_tail
= ¤t
->bio_list
;
1469 bio
->bi_next
= NULL
;
1470 __generic_make_request(bio
);
1471 bio
= current
->bio_list
;
1473 current
->bio_tail
= NULL
; /* deactivate */
1475 EXPORT_SYMBOL(generic_make_request
);
1478 * submit_bio - submit a bio to the block device layer for I/O
1479 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1480 * @bio: The &struct bio which describes the I/O
1482 * submit_bio() is very similar in purpose to generic_make_request(), and
1483 * uses that function to do most of the work. Both are fairly rough
1484 * interfaces; @bio must be presetup and ready for I/O.
1487 void submit_bio(int rw
, struct bio
*bio
)
1489 int count
= bio_sectors(bio
);
1494 * If it's a regular read/write or a barrier with data attached,
1495 * go through the normal accounting stuff before submission.
1497 if (bio_has_data(bio
)) {
1499 count_vm_events(PGPGOUT
, count
);
1501 task_io_account_read(bio
->bi_size
);
1502 count_vm_events(PGPGIN
, count
);
1505 if (unlikely(block_dump
)) {
1506 char b
[BDEVNAME_SIZE
];
1507 printk(KERN_DEBUG
"%s(%d): %s block %Lu on %s\n",
1508 current
->comm
, task_pid_nr(current
),
1509 (rw
& WRITE
) ? "WRITE" : "READ",
1510 (unsigned long long)bio
->bi_sector
,
1511 bdevname(bio
->bi_bdev
, b
));
1515 generic_make_request(bio
);
1517 EXPORT_SYMBOL(submit_bio
);
1520 * __end_that_request_first - end I/O on a request
1521 * @req: the request being processed
1522 * @error: %0 for success, < %0 for error
1523 * @nr_bytes: number of bytes to complete
1526 * Ends I/O on a number of bytes attached to @req, and sets it up
1527 * for the next range of segments (if any) in the cluster.
1530 * %0 - we are done with this request, call end_that_request_last()
1531 * %1 - still buffers pending for this request
1533 static int __end_that_request_first(struct request
*req
, int error
,
1536 int total_bytes
, bio_nbytes
, next_idx
= 0;
1539 blk_add_trace_rq(req
->q
, req
, BLK_TA_COMPLETE
);
1542 * for a REQ_TYPE_BLOCK_PC request, we want to carry any eventual
1543 * sense key with us all the way through
1545 if (!blk_pc_request(req
))
1548 if (error
&& (blk_fs_request(req
) && !(req
->cmd_flags
& REQ_QUIET
))) {
1549 printk(KERN_ERR
"end_request: I/O error, dev %s, sector %llu\n",
1550 req
->rq_disk
? req
->rq_disk
->disk_name
: "?",
1551 (unsigned long long)req
->sector
);
1554 if (blk_fs_request(req
) && req
->rq_disk
) {
1555 const int rw
= rq_data_dir(req
);
1556 struct hd_struct
*part
;
1559 cpu
= part_stat_lock();
1560 part
= disk_map_sector_rcu(req
->rq_disk
, req
->sector
);
1561 part_stat_add(cpu
, part
, sectors
[rw
], nr_bytes
>> 9);
1565 total_bytes
= bio_nbytes
= 0;
1566 while ((bio
= req
->bio
) != NULL
) {
1570 * For an empty barrier request, the low level driver must
1571 * store a potential error location in ->sector. We pass
1572 * that back up in ->bi_sector.
1574 if (blk_empty_barrier(req
))
1575 bio
->bi_sector
= req
->sector
;
1577 if (nr_bytes
>= bio
->bi_size
) {
1578 req
->bio
= bio
->bi_next
;
1579 nbytes
= bio
->bi_size
;
1580 req_bio_endio(req
, bio
, nbytes
, error
);
1584 int idx
= bio
->bi_idx
+ next_idx
;
1586 if (unlikely(bio
->bi_idx
>= bio
->bi_vcnt
)) {
1587 blk_dump_rq_flags(req
, "__end_that");
1588 printk(KERN_ERR
"%s: bio idx %d >= vcnt %d\n",
1589 __func__
, bio
->bi_idx
, bio
->bi_vcnt
);
1593 nbytes
= bio_iovec_idx(bio
, idx
)->bv_len
;
1594 BIO_BUG_ON(nbytes
> bio
->bi_size
);
1597 * not a complete bvec done
1599 if (unlikely(nbytes
> nr_bytes
)) {
1600 bio_nbytes
+= nr_bytes
;
1601 total_bytes
+= nr_bytes
;
1606 * advance to the next vector
1609 bio_nbytes
+= nbytes
;
1612 total_bytes
+= nbytes
;
1618 * end more in this run, or just return 'not-done'
1620 if (unlikely(nr_bytes
<= 0))
1632 * if the request wasn't completed, update state
1635 req_bio_endio(req
, bio
, bio_nbytes
, error
);
1636 bio
->bi_idx
+= next_idx
;
1637 bio_iovec(bio
)->bv_offset
+= nr_bytes
;
1638 bio_iovec(bio
)->bv_len
-= nr_bytes
;
1641 blk_recalc_rq_sectors(req
, total_bytes
>> 9);
1642 blk_recalc_rq_segments(req
);
1647 * splice the completion data to a local structure and hand off to
1648 * process_completion_queue() to complete the requests
1650 static void blk_done_softirq(struct softirq_action
*h
)
1652 struct list_head
*cpu_list
, local_list
;
1654 local_irq_disable();
1655 cpu_list
= &__get_cpu_var(blk_cpu_done
);
1656 list_replace_init(cpu_list
, &local_list
);
1659 while (!list_empty(&local_list
)) {
1662 rq
= list_entry(local_list
.next
, struct request
, donelist
);
1663 list_del_init(&rq
->donelist
);
1664 rq
->q
->softirq_done_fn(rq
);
1668 static int __cpuinit
blk_cpu_notify(struct notifier_block
*self
,
1669 unsigned long action
, void *hcpu
)
1672 * If a CPU goes away, splice its entries to the current CPU
1673 * and trigger a run of the softirq
1675 if (action
== CPU_DEAD
|| action
== CPU_DEAD_FROZEN
) {
1676 int cpu
= (unsigned long) hcpu
;
1678 local_irq_disable();
1679 list_splice_init(&per_cpu(blk_cpu_done
, cpu
),
1680 &__get_cpu_var(blk_cpu_done
));
1681 raise_softirq_irqoff(BLOCK_SOFTIRQ
);
1689 static struct notifier_block blk_cpu_notifier __cpuinitdata
= {
1690 .notifier_call
= blk_cpu_notify
,
1694 * blk_complete_request - end I/O on a request
1695 * @req: the request being processed
1698 * Ends all I/O on a request. It does not handle partial completions,
1699 * unless the driver actually implements this in its completion callback
1700 * through requeueing. The actual completion happens out-of-order,
1701 * through a softirq handler. The user must have registered a completion
1702 * callback through blk_queue_softirq_done().
1705 void blk_complete_request(struct request
*req
)
1707 struct list_head
*cpu_list
;
1708 unsigned long flags
;
1710 BUG_ON(!req
->q
->softirq_done_fn
);
1712 local_irq_save(flags
);
1714 cpu_list
= &__get_cpu_var(blk_cpu_done
);
1715 list_add_tail(&req
->donelist
, cpu_list
);
1716 raise_softirq_irqoff(BLOCK_SOFTIRQ
);
1718 local_irq_restore(flags
);
1720 EXPORT_SYMBOL(blk_complete_request
);
1723 * queue lock must be held
1725 static void end_that_request_last(struct request
*req
, int error
)
1727 struct gendisk
*disk
= req
->rq_disk
;
1729 if (blk_rq_tagged(req
))
1730 blk_queue_end_tag(req
->q
, req
);
1732 if (blk_queued_rq(req
))
1733 blkdev_dequeue_request(req
);
1735 if (unlikely(laptop_mode
) && blk_fs_request(req
))
1736 laptop_io_completion();
1739 * Account IO completion. bar_rq isn't accounted as a normal
1740 * IO on queueing nor completion. Accounting the containing
1741 * request is enough.
1743 if (disk
&& blk_fs_request(req
) && req
!= &req
->q
->bar_rq
) {
1744 unsigned long duration
= jiffies
- req
->start_time
;
1745 const int rw
= rq_data_dir(req
);
1746 struct hd_struct
*part
;
1749 cpu
= part_stat_lock();
1750 part
= disk_map_sector_rcu(disk
, req
->sector
);
1752 part_stat_inc(cpu
, part
, ios
[rw
]);
1753 part_stat_add(cpu
, part
, ticks
[rw
], duration
);
1754 part_round_stats(cpu
, part
);
1755 part_dec_in_flight(part
);
1761 req
->end_io(req
, error
);
1763 if (blk_bidi_rq(req
))
1764 __blk_put_request(req
->next_rq
->q
, req
->next_rq
);
1766 __blk_put_request(req
->q
, req
);
1770 static inline void __end_request(struct request
*rq
, int uptodate
,
1771 unsigned int nr_bytes
)
1776 error
= uptodate
? uptodate
: -EIO
;
1778 __blk_end_request(rq
, error
, nr_bytes
);
1782 * blk_rq_bytes - Returns bytes left to complete in the entire request
1783 * @rq: the request being processed
1785 unsigned int blk_rq_bytes(struct request
*rq
)
1787 if (blk_fs_request(rq
))
1788 return rq
->hard_nr_sectors
<< 9;
1790 return rq
->data_len
;
1792 EXPORT_SYMBOL_GPL(blk_rq_bytes
);
1795 * blk_rq_cur_bytes - Returns bytes left to complete in the current segment
1796 * @rq: the request being processed
1798 unsigned int blk_rq_cur_bytes(struct request
*rq
)
1800 if (blk_fs_request(rq
))
1801 return rq
->current_nr_sectors
<< 9;
1804 return rq
->bio
->bi_size
;
1806 return rq
->data_len
;
1808 EXPORT_SYMBOL_GPL(blk_rq_cur_bytes
);
1811 * end_queued_request - end all I/O on a queued request
1812 * @rq: the request being processed
1813 * @uptodate: error value or %0/%1 uptodate flag
1816 * Ends all I/O on a request, and removes it from the block layer queues.
1817 * Not suitable for normal I/O completion, unless the driver still has
1818 * the request attached to the block layer.
1821 void end_queued_request(struct request
*rq
, int uptodate
)
1823 __end_request(rq
, uptodate
, blk_rq_bytes(rq
));
1825 EXPORT_SYMBOL(end_queued_request
);
1828 * end_dequeued_request - end all I/O on a dequeued request
1829 * @rq: the request being processed
1830 * @uptodate: error value or %0/%1 uptodate flag
1833 * Ends all I/O on a request. The request must already have been
1834 * dequeued using blkdev_dequeue_request(), as is normally the case
1838 void end_dequeued_request(struct request
*rq
, int uptodate
)
1840 __end_request(rq
, uptodate
, blk_rq_bytes(rq
));
1842 EXPORT_SYMBOL(end_dequeued_request
);
1846 * end_request - end I/O on the current segment of the request
1847 * @req: the request being processed
1848 * @uptodate: error value or %0/%1 uptodate flag
1851 * Ends I/O on the current segment of a request. If that is the only
1852 * remaining segment, the request is also completed and freed.
1854 * This is a remnant of how older block drivers handled I/O completions.
1855 * Modern drivers typically end I/O on the full request in one go, unless
1856 * they have a residual value to account for. For that case this function
1857 * isn't really useful, unless the residual just happens to be the
1858 * full current segment. In other words, don't use this function in new
1859 * code. Either use end_request_completely(), or the
1860 * end_that_request_chunk() (along with end_that_request_last()) for
1861 * partial completions.
1864 void end_request(struct request
*req
, int uptodate
)
1866 __end_request(req
, uptodate
, req
->hard_cur_sectors
<< 9);
1868 EXPORT_SYMBOL(end_request
);
1871 * blk_end_io - Generic end_io function to complete a request.
1872 * @rq: the request being processed
1873 * @error: %0 for success, < %0 for error
1874 * @nr_bytes: number of bytes to complete @rq
1875 * @bidi_bytes: number of bytes to complete @rq->next_rq
1876 * @drv_callback: function called between completion of bios in the request
1877 * and completion of the request.
1878 * If the callback returns non %0, this helper returns without
1879 * completion of the request.
1882 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
1883 * If @rq has leftover, sets it up for the next range of segments.
1886 * %0 - we are done with this request
1887 * %1 - this request is not freed yet, it still has pending buffers.
1889 static int blk_end_io(struct request
*rq
, int error
, unsigned int nr_bytes
,
1890 unsigned int bidi_bytes
,
1891 int (drv_callback
)(struct request
*))
1893 struct request_queue
*q
= rq
->q
;
1894 unsigned long flags
= 0UL;
1896 if (bio_has_data(rq
->bio
) || blk_discard_rq(rq
)) {
1897 if (__end_that_request_first(rq
, error
, nr_bytes
))
1900 /* Bidi request must be completed as a whole */
1901 if (blk_bidi_rq(rq
) &&
1902 __end_that_request_first(rq
->next_rq
, error
, bidi_bytes
))
1906 /* Special feature for tricky drivers */
1907 if (drv_callback
&& drv_callback(rq
))
1910 add_disk_randomness(rq
->rq_disk
);
1912 spin_lock_irqsave(q
->queue_lock
, flags
);
1913 end_that_request_last(rq
, error
);
1914 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1920 * blk_end_request - Helper function for drivers to complete the request.
1921 * @rq: the request being processed
1922 * @error: %0 for success, < %0 for error
1923 * @nr_bytes: number of bytes to complete
1926 * Ends I/O on a number of bytes attached to @rq.
1927 * If @rq has leftover, sets it up for the next range of segments.
1930 * %0 - we are done with this request
1931 * %1 - still buffers pending for this request
1933 int blk_end_request(struct request
*rq
, int error
, unsigned int nr_bytes
)
1935 return blk_end_io(rq
, error
, nr_bytes
, 0, NULL
);
1937 EXPORT_SYMBOL_GPL(blk_end_request
);
1940 * __blk_end_request - Helper function for drivers to complete the request.
1941 * @rq: the request being processed
1942 * @error: %0 for success, < %0 for error
1943 * @nr_bytes: number of bytes to complete
1946 * Must be called with queue lock held unlike blk_end_request().
1949 * %0 - we are done with this request
1950 * %1 - still buffers pending for this request
1952 int __blk_end_request(struct request
*rq
, int error
, unsigned int nr_bytes
)
1954 if ((bio_has_data(rq
->bio
) || blk_discard_rq(rq
)) &&
1955 __end_that_request_first(rq
, error
, nr_bytes
))
1958 add_disk_randomness(rq
->rq_disk
);
1960 end_that_request_last(rq
, error
);
1964 EXPORT_SYMBOL_GPL(__blk_end_request
);
1967 * blk_end_bidi_request - Helper function for drivers to complete bidi request.
1968 * @rq: the bidi request being processed
1969 * @error: %0 for success, < %0 for error
1970 * @nr_bytes: number of bytes to complete @rq
1971 * @bidi_bytes: number of bytes to complete @rq->next_rq
1974 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
1977 * %0 - we are done with this request
1978 * %1 - still buffers pending for this request
1980 int blk_end_bidi_request(struct request
*rq
, int error
, unsigned int nr_bytes
,
1981 unsigned int bidi_bytes
)
1983 return blk_end_io(rq
, error
, nr_bytes
, bidi_bytes
, NULL
);
1985 EXPORT_SYMBOL_GPL(blk_end_bidi_request
);
1988 * blk_end_request_callback - Special helper function for tricky drivers
1989 * @rq: the request being processed
1990 * @error: %0 for success, < %0 for error
1991 * @nr_bytes: number of bytes to complete
1992 * @drv_callback: function called between completion of bios in the request
1993 * and completion of the request.
1994 * If the callback returns non %0, this helper returns without
1995 * completion of the request.
1998 * Ends I/O on a number of bytes attached to @rq.
1999 * If @rq has leftover, sets it up for the next range of segments.
2001 * This special helper function is used only for existing tricky drivers.
2002 * (e.g. cdrom_newpc_intr() of ide-cd)
2003 * This interface will be removed when such drivers are rewritten.
2004 * Don't use this interface in other places anymore.
2007 * %0 - we are done with this request
2008 * %1 - this request is not freed yet.
2009 * this request still has pending buffers or
2010 * the driver doesn't want to finish this request yet.
2012 int blk_end_request_callback(struct request
*rq
, int error
,
2013 unsigned int nr_bytes
,
2014 int (drv_callback
)(struct request
*))
2016 return blk_end_io(rq
, error
, nr_bytes
, 0, drv_callback
);
2018 EXPORT_SYMBOL_GPL(blk_end_request_callback
);
2020 void blk_rq_bio_prep(struct request_queue
*q
, struct request
*rq
,
2023 /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw, and
2024 we want BIO_RW_AHEAD (bit 1) to imply REQ_FAILFAST (bit 1). */
2025 rq
->cmd_flags
|= (bio
->bi_rw
& 3);
2027 if (bio_has_data(bio
)) {
2028 rq
->nr_phys_segments
= bio_phys_segments(q
, bio
);
2029 rq
->buffer
= bio_data(bio
);
2031 rq
->current_nr_sectors
= bio_cur_sectors(bio
);
2032 rq
->hard_cur_sectors
= rq
->current_nr_sectors
;
2033 rq
->hard_nr_sectors
= rq
->nr_sectors
= bio_sectors(bio
);
2034 rq
->data_len
= bio
->bi_size
;
2036 rq
->bio
= rq
->biotail
= bio
;
2039 rq
->rq_disk
= bio
->bi_bdev
->bd_disk
;
2042 int kblockd_schedule_work(struct work_struct
*work
)
2044 return queue_work(kblockd_workqueue
, work
);
2046 EXPORT_SYMBOL(kblockd_schedule_work
);
2048 void kblockd_flush_work(struct work_struct
*work
)
2050 cancel_work_sync(work
);
2052 EXPORT_SYMBOL(kblockd_flush_work
);
2054 int __init
blk_dev_init(void)
2058 kblockd_workqueue
= create_workqueue("kblockd");
2059 if (!kblockd_workqueue
)
2060 panic("Failed to create kblockd\n");
2062 request_cachep
= kmem_cache_create("blkdev_requests",
2063 sizeof(struct request
), 0, SLAB_PANIC
, NULL
);
2065 blk_requestq_cachep
= kmem_cache_create("blkdev_queue",
2066 sizeof(struct request_queue
), 0, SLAB_PANIC
, NULL
);
2068 for_each_possible_cpu(i
)
2069 INIT_LIST_HEAD(&per_cpu(blk_cpu_done
, i
));
2071 open_softirq(BLOCK_SOFTIRQ
, blk_done_softirq
);
2072 register_hotcpu_notifier(&blk_cpu_notifier
);