2 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 1994, Karl Keyte: Added support for disk statistics
4 * Elevator latency, (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
5 * Queue request tables / lock, selectable elevator, Jens Axboe <axboe@suse.de>
6 * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au> - July2000
7 * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
11 * This handles all read/write requests to block devices
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/backing-dev.h>
16 #include <linux/bio.h>
17 #include <linux/blkdev.h>
18 #include <linux/highmem.h>
20 #include <linux/kernel_stat.h>
21 #include <linux/string.h>
22 #include <linux/init.h>
23 #include <linux/bootmem.h> /* for max_pfn/max_low_pfn */
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>
33 #include <linux/scatterlist.h>
40 #include <scsi/scsi_cmnd.h>
42 static void blk_unplug_work(struct work_struct
*work
);
43 static void blk_unplug_timeout(unsigned long data
);
44 static void drive_stat_acct(struct request
*rq
, int new_io
);
45 static void init_request_from_bio(struct request
*req
, struct bio
*bio
);
46 static int __make_request(struct request_queue
*q
, struct bio
*bio
);
47 static struct io_context
*current_io_context(gfp_t gfp_flags
, int node
);
48 static void blk_recalc_rq_segments(struct request
*rq
);
49 static void blk_rq_bio_prep(struct request_queue
*q
, struct request
*rq
,
53 * For the allocated request tables
55 struct kmem_cache
*request_cachep
;
58 * For queue allocation
60 struct kmem_cache
*blk_requestq_cachep
= NULL
;
63 * For io context allocations
65 static struct kmem_cache
*iocontext_cachep
;
68 * Controlling structure to kblockd
70 static struct workqueue_struct
*kblockd_workqueue
;
72 unsigned long blk_max_low_pfn
, blk_max_pfn
;
74 EXPORT_SYMBOL(blk_max_low_pfn
);
75 EXPORT_SYMBOL(blk_max_pfn
);
77 static DEFINE_PER_CPU(struct list_head
, blk_cpu_done
);
79 /* Amount of time in which a process may batch requests */
80 #define BLK_BATCH_TIME (HZ/50UL)
82 /* Number of requests a "batching" process may submit */
83 #define BLK_BATCH_REQ 32
85 void blk_queue_congestion_threshold(struct request_queue
*q
)
89 nr
= q
->nr_requests
- (q
->nr_requests
/ 8) + 1;
90 if (nr
> q
->nr_requests
)
92 q
->nr_congestion_on
= nr
;
94 nr
= q
->nr_requests
- (q
->nr_requests
/ 8) - (q
->nr_requests
/ 16) - 1;
97 q
->nr_congestion_off
= nr
;
101 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
104 * Locates the passed device's request queue and returns the address of its
107 * Will return NULL if the request queue cannot be located.
109 struct backing_dev_info
*blk_get_backing_dev_info(struct block_device
*bdev
)
111 struct backing_dev_info
*ret
= NULL
;
112 struct request_queue
*q
= bdev_get_queue(bdev
);
115 ret
= &q
->backing_dev_info
;
118 EXPORT_SYMBOL(blk_get_backing_dev_info
);
121 * blk_queue_prep_rq - set a prepare_request function for queue
123 * @pfn: prepare_request function
125 * It's possible for a queue to register a prepare_request callback which
126 * is invoked before the request is handed to the request_fn. The goal of
127 * the function is to prepare a request for I/O, it can be used to build a
128 * cdb from the request data for instance.
131 void blk_queue_prep_rq(struct request_queue
*q
, prep_rq_fn
*pfn
)
136 EXPORT_SYMBOL(blk_queue_prep_rq
);
139 * blk_queue_merge_bvec - set a merge_bvec function for queue
141 * @mbfn: merge_bvec_fn
143 * Usually queues have static limitations on the max sectors or segments that
144 * we can put in a request. Stacking drivers may have some settings that
145 * are dynamic, and thus we have to query the queue whether it is ok to
146 * add a new bio_vec to a bio at a given offset or not. If the block device
147 * has such limitations, it needs to register a merge_bvec_fn to control
148 * the size of bio's sent to it. Note that a block device *must* allow a
149 * single page to be added to an empty bio. The block device driver may want
150 * to use the bio_split() function to deal with these bio's. By default
151 * no merge_bvec_fn is defined for a queue, and only the fixed limits are
154 void blk_queue_merge_bvec(struct request_queue
*q
, merge_bvec_fn
*mbfn
)
156 q
->merge_bvec_fn
= mbfn
;
159 EXPORT_SYMBOL(blk_queue_merge_bvec
);
161 void blk_queue_softirq_done(struct request_queue
*q
, softirq_done_fn
*fn
)
163 q
->softirq_done_fn
= fn
;
166 EXPORT_SYMBOL(blk_queue_softirq_done
);
169 * blk_queue_make_request - define an alternate make_request function for a device
170 * @q: the request queue for the device to be affected
171 * @mfn: the alternate make_request function
174 * The normal way for &struct bios to be passed to a device
175 * driver is for them to be collected into requests on a request
176 * queue, and then to allow the device driver to select requests
177 * off that queue when it is ready. This works well for many block
178 * devices. However some block devices (typically virtual devices
179 * such as md or lvm) do not benefit from the processing on the
180 * request queue, and are served best by having the requests passed
181 * directly to them. This can be achieved by providing a function
182 * to blk_queue_make_request().
185 * The driver that does this *must* be able to deal appropriately
186 * with buffers in "highmemory". This can be accomplished by either calling
187 * __bio_kmap_atomic() to get a temporary kernel mapping, or by calling
188 * blk_queue_bounce() to create a buffer in normal memory.
190 void blk_queue_make_request(struct request_queue
* q
, make_request_fn
* mfn
)
195 q
->nr_requests
= BLKDEV_MAX_RQ
;
196 blk_queue_max_phys_segments(q
, MAX_PHYS_SEGMENTS
);
197 blk_queue_max_hw_segments(q
, MAX_HW_SEGMENTS
);
198 q
->make_request_fn
= mfn
;
199 q
->backing_dev_info
.ra_pages
= (VM_MAX_READAHEAD
* 1024) / PAGE_CACHE_SIZE
;
200 q
->backing_dev_info
.state
= 0;
201 q
->backing_dev_info
.capabilities
= BDI_CAP_MAP_COPY
;
202 blk_queue_max_sectors(q
, SAFE_MAX_SECTORS
);
203 blk_queue_hardsect_size(q
, 512);
204 blk_queue_dma_alignment(q
, 511);
205 blk_queue_congestion_threshold(q
);
206 q
->nr_batching
= BLK_BATCH_REQ
;
208 q
->unplug_thresh
= 4; /* hmm */
209 q
->unplug_delay
= (3 * HZ
) / 1000; /* 3 milliseconds */
210 if (q
->unplug_delay
== 0)
213 INIT_WORK(&q
->unplug_work
, blk_unplug_work
);
215 q
->unplug_timer
.function
= blk_unplug_timeout
;
216 q
->unplug_timer
.data
= (unsigned long)q
;
219 * by default assume old behaviour and bounce for any highmem page
221 blk_queue_bounce_limit(q
, BLK_BOUNCE_HIGH
);
224 EXPORT_SYMBOL(blk_queue_make_request
);
226 static void rq_init(struct request_queue
*q
, struct request
*rq
)
228 INIT_LIST_HEAD(&rq
->queuelist
);
229 INIT_LIST_HEAD(&rq
->donelist
);
232 rq
->bio
= rq
->biotail
= NULL
;
233 INIT_HLIST_NODE(&rq
->hash
);
234 RB_CLEAR_NODE(&rq
->rb_node
);
242 rq
->nr_phys_segments
= 0;
245 rq
->end_io_data
= NULL
;
246 rq
->completion_data
= NULL
;
251 * blk_queue_ordered - does this queue support ordered writes
252 * @q: the request queue
253 * @ordered: one of QUEUE_ORDERED_*
254 * @prepare_flush_fn: rq setup helper for cache flush ordered writes
257 * For journalled file systems, doing ordered writes on a commit
258 * block instead of explicitly doing wait_on_buffer (which is bad
259 * for performance) can be a big win. Block drivers supporting this
260 * feature should call this function and indicate so.
263 int blk_queue_ordered(struct request_queue
*q
, unsigned ordered
,
264 prepare_flush_fn
*prepare_flush_fn
)
266 if (ordered
& (QUEUE_ORDERED_PREFLUSH
| QUEUE_ORDERED_POSTFLUSH
) &&
267 prepare_flush_fn
== NULL
) {
268 printk(KERN_ERR
"blk_queue_ordered: prepare_flush_fn required\n");
272 if (ordered
!= QUEUE_ORDERED_NONE
&&
273 ordered
!= QUEUE_ORDERED_DRAIN
&&
274 ordered
!= QUEUE_ORDERED_DRAIN_FLUSH
&&
275 ordered
!= QUEUE_ORDERED_DRAIN_FUA
&&
276 ordered
!= QUEUE_ORDERED_TAG
&&
277 ordered
!= QUEUE_ORDERED_TAG_FLUSH
&&
278 ordered
!= QUEUE_ORDERED_TAG_FUA
) {
279 printk(KERN_ERR
"blk_queue_ordered: bad value %d\n", ordered
);
283 q
->ordered
= ordered
;
284 q
->next_ordered
= ordered
;
285 q
->prepare_flush_fn
= prepare_flush_fn
;
290 EXPORT_SYMBOL(blk_queue_ordered
);
293 * Cache flushing for ordered writes handling
295 inline unsigned blk_ordered_cur_seq(struct request_queue
*q
)
299 return 1 << ffz(q
->ordseq
);
302 unsigned blk_ordered_req_seq(struct request
*rq
)
304 struct request_queue
*q
= rq
->q
;
306 BUG_ON(q
->ordseq
== 0);
308 if (rq
== &q
->pre_flush_rq
)
309 return QUEUE_ORDSEQ_PREFLUSH
;
310 if (rq
== &q
->bar_rq
)
311 return QUEUE_ORDSEQ_BAR
;
312 if (rq
== &q
->post_flush_rq
)
313 return QUEUE_ORDSEQ_POSTFLUSH
;
316 * !fs requests don't need to follow barrier ordering. Always
317 * put them at the front. This fixes the following deadlock.
319 * http://thread.gmane.org/gmane.linux.kernel/537473
321 if (!blk_fs_request(rq
))
322 return QUEUE_ORDSEQ_DRAIN
;
324 if ((rq
->cmd_flags
& REQ_ORDERED_COLOR
) ==
325 (q
->orig_bar_rq
->cmd_flags
& REQ_ORDERED_COLOR
))
326 return QUEUE_ORDSEQ_DRAIN
;
328 return QUEUE_ORDSEQ_DONE
;
331 void blk_ordered_complete_seq(struct request_queue
*q
, unsigned seq
, int error
)
335 if (error
&& !q
->orderr
)
338 BUG_ON(q
->ordseq
& seq
);
341 if (blk_ordered_cur_seq(q
) != QUEUE_ORDSEQ_DONE
)
345 * Okay, sequence complete.
350 if (__blk_end_request(rq
, q
->orderr
, blk_rq_bytes(rq
)))
354 static void pre_flush_end_io(struct request
*rq
, int error
)
356 elv_completed_request(rq
->q
, rq
);
357 blk_ordered_complete_seq(rq
->q
, QUEUE_ORDSEQ_PREFLUSH
, error
);
360 static void bar_end_io(struct request
*rq
, int error
)
362 elv_completed_request(rq
->q
, rq
);
363 blk_ordered_complete_seq(rq
->q
, QUEUE_ORDSEQ_BAR
, error
);
366 static void post_flush_end_io(struct request
*rq
, int error
)
368 elv_completed_request(rq
->q
, rq
);
369 blk_ordered_complete_seq(rq
->q
, QUEUE_ORDSEQ_POSTFLUSH
, error
);
372 static void queue_flush(struct request_queue
*q
, unsigned which
)
375 rq_end_io_fn
*end_io
;
377 if (which
== QUEUE_ORDERED_PREFLUSH
) {
378 rq
= &q
->pre_flush_rq
;
379 end_io
= pre_flush_end_io
;
381 rq
= &q
->post_flush_rq
;
382 end_io
= post_flush_end_io
;
385 rq
->cmd_flags
= REQ_HARDBARRIER
;
387 rq
->elevator_private
= NULL
;
388 rq
->elevator_private2
= NULL
;
389 rq
->rq_disk
= q
->bar_rq
.rq_disk
;
391 q
->prepare_flush_fn(q
, rq
);
393 elv_insert(q
, rq
, ELEVATOR_INSERT_FRONT
);
396 static inline struct request
*start_ordered(struct request_queue
*q
,
400 q
->ordered
= q
->next_ordered
;
401 q
->ordseq
|= QUEUE_ORDSEQ_STARTED
;
404 * Prep proxy barrier request.
406 blkdev_dequeue_request(rq
);
411 if (bio_data_dir(q
->orig_bar_rq
->bio
) == WRITE
)
412 rq
->cmd_flags
|= REQ_RW
;
413 if (q
->ordered
& QUEUE_ORDERED_FUA
)
414 rq
->cmd_flags
|= REQ_FUA
;
415 rq
->elevator_private
= NULL
;
416 rq
->elevator_private2
= NULL
;
417 init_request_from_bio(rq
, q
->orig_bar_rq
->bio
);
418 rq
->end_io
= bar_end_io
;
421 * Queue ordered sequence. As we stack them at the head, we
422 * need to queue in reverse order. Note that we rely on that
423 * no fs request uses ELEVATOR_INSERT_FRONT and thus no fs
424 * request gets inbetween ordered sequence. If this request is
425 * an empty barrier, we don't need to do a postflush ever since
426 * there will be no data written between the pre and post flush.
427 * Hence a single flush will suffice.
429 if ((q
->ordered
& QUEUE_ORDERED_POSTFLUSH
) && !blk_empty_barrier(rq
))
430 queue_flush(q
, QUEUE_ORDERED_POSTFLUSH
);
432 q
->ordseq
|= QUEUE_ORDSEQ_POSTFLUSH
;
434 elv_insert(q
, rq
, ELEVATOR_INSERT_FRONT
);
436 if (q
->ordered
& QUEUE_ORDERED_PREFLUSH
) {
437 queue_flush(q
, QUEUE_ORDERED_PREFLUSH
);
438 rq
= &q
->pre_flush_rq
;
440 q
->ordseq
|= QUEUE_ORDSEQ_PREFLUSH
;
442 if ((q
->ordered
& QUEUE_ORDERED_TAG
) || q
->in_flight
== 0)
443 q
->ordseq
|= QUEUE_ORDSEQ_DRAIN
;
450 int blk_do_ordered(struct request_queue
*q
, struct request
**rqp
)
452 struct request
*rq
= *rqp
;
453 const int is_barrier
= blk_fs_request(rq
) && blk_barrier_rq(rq
);
459 if (q
->next_ordered
!= QUEUE_ORDERED_NONE
) {
460 *rqp
= start_ordered(q
, rq
);
464 * This can happen when the queue switches to
465 * ORDERED_NONE while this request is on it.
467 blkdev_dequeue_request(rq
);
468 if (__blk_end_request(rq
, -EOPNOTSUPP
,
477 * Ordered sequence in progress
480 /* Special requests are not subject to ordering rules. */
481 if (!blk_fs_request(rq
) &&
482 rq
!= &q
->pre_flush_rq
&& rq
!= &q
->post_flush_rq
)
485 if (q
->ordered
& QUEUE_ORDERED_TAG
) {
486 /* Ordered by tag. Blocking the next barrier is enough. */
487 if (is_barrier
&& rq
!= &q
->bar_rq
)
490 /* Ordered by draining. Wait for turn. */
491 WARN_ON(blk_ordered_req_seq(rq
) < blk_ordered_cur_seq(q
));
492 if (blk_ordered_req_seq(rq
) > blk_ordered_cur_seq(q
))
499 static void req_bio_endio(struct request
*rq
, struct bio
*bio
,
500 unsigned int nbytes
, int error
)
502 struct request_queue
*q
= rq
->q
;
504 if (&q
->bar_rq
!= rq
) {
506 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
507 else if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
510 if (unlikely(nbytes
> bio
->bi_size
)) {
511 printk("%s: want %u bytes done, only %u left\n",
512 __FUNCTION__
, nbytes
, bio
->bi_size
);
513 nbytes
= bio
->bi_size
;
516 bio
->bi_size
-= nbytes
;
517 bio
->bi_sector
+= (nbytes
>> 9);
518 if (bio
->bi_size
== 0)
519 bio_endio(bio
, error
);
523 * Okay, this is the barrier request in progress, just
526 if (error
&& !q
->orderr
)
532 * blk_queue_bounce_limit - set bounce buffer limit for queue
533 * @q: the request queue for the device
534 * @dma_addr: bus address limit
537 * Different hardware can have different requirements as to what pages
538 * it can do I/O directly to. A low level driver can call
539 * blk_queue_bounce_limit to have lower memory pages allocated as bounce
540 * buffers for doing I/O to pages residing above @page.
542 void blk_queue_bounce_limit(struct request_queue
*q
, u64 dma_addr
)
544 unsigned long bounce_pfn
= dma_addr
>> PAGE_SHIFT
;
547 q
->bounce_gfp
= GFP_NOIO
;
548 #if BITS_PER_LONG == 64
549 /* Assume anything <= 4GB can be handled by IOMMU.
550 Actually some IOMMUs can handle everything, but I don't
551 know of a way to test this here. */
552 if (bounce_pfn
< (min_t(u64
,0xffffffff,BLK_BOUNCE_HIGH
) >> PAGE_SHIFT
))
554 q
->bounce_pfn
= max_low_pfn
;
556 if (bounce_pfn
< blk_max_low_pfn
)
558 q
->bounce_pfn
= bounce_pfn
;
561 init_emergency_isa_pool();
562 q
->bounce_gfp
= GFP_NOIO
| GFP_DMA
;
563 q
->bounce_pfn
= bounce_pfn
;
567 EXPORT_SYMBOL(blk_queue_bounce_limit
);
570 * blk_queue_max_sectors - set max sectors for a request for this queue
571 * @q: the request queue for the device
572 * @max_sectors: max sectors in the usual 512b unit
575 * Enables a low level driver to set an upper limit on the size of
578 void blk_queue_max_sectors(struct request_queue
*q
, unsigned int max_sectors
)
580 if ((max_sectors
<< 9) < PAGE_CACHE_SIZE
) {
581 max_sectors
= 1 << (PAGE_CACHE_SHIFT
- 9);
582 printk("%s: set to minimum %d\n", __FUNCTION__
, max_sectors
);
585 if (BLK_DEF_MAX_SECTORS
> max_sectors
)
586 q
->max_hw_sectors
= q
->max_sectors
= max_sectors
;
588 q
->max_sectors
= BLK_DEF_MAX_SECTORS
;
589 q
->max_hw_sectors
= max_sectors
;
593 EXPORT_SYMBOL(blk_queue_max_sectors
);
596 * blk_queue_max_phys_segments - set max phys segments for a request for this queue
597 * @q: the request queue for the device
598 * @max_segments: max number of segments
601 * Enables a low level driver to set an upper limit on the number of
602 * physical data segments in a request. This would be the largest sized
603 * scatter list the driver could handle.
605 void blk_queue_max_phys_segments(struct request_queue
*q
,
606 unsigned short max_segments
)
610 printk("%s: set to minimum %d\n", __FUNCTION__
, max_segments
);
613 q
->max_phys_segments
= max_segments
;
616 EXPORT_SYMBOL(blk_queue_max_phys_segments
);
619 * blk_queue_max_hw_segments - set max hw segments for a request for this queue
620 * @q: the request queue for the device
621 * @max_segments: max number of segments
624 * Enables a low level driver to set an upper limit on the number of
625 * hw data segments in a request. This would be the largest number of
626 * address/length pairs the host adapter can actually give as once
629 void blk_queue_max_hw_segments(struct request_queue
*q
,
630 unsigned short max_segments
)
634 printk("%s: set to minimum %d\n", __FUNCTION__
, max_segments
);
637 q
->max_hw_segments
= max_segments
;
640 EXPORT_SYMBOL(blk_queue_max_hw_segments
);
643 * blk_queue_max_segment_size - set max segment size for blk_rq_map_sg
644 * @q: the request queue for the device
645 * @max_size: max size of segment in bytes
648 * Enables a low level driver to set an upper limit on the size of a
651 void blk_queue_max_segment_size(struct request_queue
*q
, unsigned int max_size
)
653 if (max_size
< PAGE_CACHE_SIZE
) {
654 max_size
= PAGE_CACHE_SIZE
;
655 printk("%s: set to minimum %d\n", __FUNCTION__
, max_size
);
658 q
->max_segment_size
= max_size
;
661 EXPORT_SYMBOL(blk_queue_max_segment_size
);
664 * blk_queue_hardsect_size - set hardware sector size for the queue
665 * @q: the request queue for the device
666 * @size: the hardware sector size, in bytes
669 * This should typically be set to the lowest possible sector size
670 * that the hardware can operate on (possible without reverting to
671 * even internal read-modify-write operations). Usually the default
672 * of 512 covers most hardware.
674 void blk_queue_hardsect_size(struct request_queue
*q
, unsigned short size
)
676 q
->hardsect_size
= size
;
679 EXPORT_SYMBOL(blk_queue_hardsect_size
);
682 * Returns the minimum that is _not_ zero, unless both are zero.
684 #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
687 * blk_queue_stack_limits - inherit underlying queue limits for stacked drivers
688 * @t: the stacking driver (top)
689 * @b: the underlying device (bottom)
691 void blk_queue_stack_limits(struct request_queue
*t
, struct request_queue
*b
)
693 /* zero is "infinity" */
694 t
->max_sectors
= min_not_zero(t
->max_sectors
,b
->max_sectors
);
695 t
->max_hw_sectors
= min_not_zero(t
->max_hw_sectors
,b
->max_hw_sectors
);
697 t
->max_phys_segments
= min(t
->max_phys_segments
,b
->max_phys_segments
);
698 t
->max_hw_segments
= min(t
->max_hw_segments
,b
->max_hw_segments
);
699 t
->max_segment_size
= min(t
->max_segment_size
,b
->max_segment_size
);
700 t
->hardsect_size
= max(t
->hardsect_size
,b
->hardsect_size
);
701 if (!test_bit(QUEUE_FLAG_CLUSTER
, &b
->queue_flags
))
702 clear_bit(QUEUE_FLAG_CLUSTER
, &t
->queue_flags
);
705 EXPORT_SYMBOL(blk_queue_stack_limits
);
708 * blk_queue_dma_drain - Set up a drain buffer for excess dma.
710 * @q: the request queue for the device
711 * @buf: physically contiguous buffer
712 * @size: size of the buffer in bytes
714 * Some devices have excess DMA problems and can't simply discard (or
715 * zero fill) the unwanted piece of the transfer. They have to have a
716 * real area of memory to transfer it into. The use case for this is
717 * ATAPI devices in DMA mode. If the packet command causes a transfer
718 * bigger than the transfer size some HBAs will lock up if there
719 * aren't DMA elements to contain the excess transfer. What this API
720 * does is adjust the queue so that the buf is always appended
721 * silently to the scatterlist.
723 * Note: This routine adjusts max_hw_segments to make room for
724 * appending the drain buffer. If you call
725 * blk_queue_max_hw_segments() or blk_queue_max_phys_segments() after
726 * calling this routine, you must set the limit to one fewer than your
727 * device can support otherwise there won't be room for the drain
730 int blk_queue_dma_drain(struct request_queue
*q
, void *buf
,
733 if (q
->max_hw_segments
< 2 || q
->max_phys_segments
< 2)
735 /* make room for appending the drain */
736 --q
->max_hw_segments
;
737 --q
->max_phys_segments
;
738 q
->dma_drain_buffer
= buf
;
739 q
->dma_drain_size
= size
;
744 EXPORT_SYMBOL_GPL(blk_queue_dma_drain
);
747 * blk_queue_segment_boundary - set boundary rules for segment merging
748 * @q: the request queue for the device
749 * @mask: the memory boundary mask
751 void blk_queue_segment_boundary(struct request_queue
*q
, unsigned long mask
)
753 if (mask
< PAGE_CACHE_SIZE
- 1) {
754 mask
= PAGE_CACHE_SIZE
- 1;
755 printk("%s: set to minimum %lx\n", __FUNCTION__
, mask
);
758 q
->seg_boundary_mask
= mask
;
761 EXPORT_SYMBOL(blk_queue_segment_boundary
);
764 * blk_queue_dma_alignment - set dma length and memory alignment
765 * @q: the request queue for the device
766 * @mask: alignment mask
769 * set required memory and length aligment for direct dma transactions.
770 * this is used when buiding direct io requests for the queue.
773 void blk_queue_dma_alignment(struct request_queue
*q
, int mask
)
775 q
->dma_alignment
= mask
;
778 EXPORT_SYMBOL(blk_queue_dma_alignment
);
781 * blk_queue_update_dma_alignment - update dma length and memory alignment
782 * @q: the request queue for the device
783 * @mask: alignment mask
786 * update required memory and length aligment for direct dma transactions.
787 * If the requested alignment is larger than the current alignment, then
788 * the current queue alignment is updated to the new value, otherwise it
789 * is left alone. The design of this is to allow multiple objects
790 * (driver, device, transport etc) to set their respective
791 * alignments without having them interfere.
794 void blk_queue_update_dma_alignment(struct request_queue
*q
, int mask
)
796 BUG_ON(mask
> PAGE_SIZE
);
798 if (mask
> q
->dma_alignment
)
799 q
->dma_alignment
= mask
;
802 EXPORT_SYMBOL(blk_queue_update_dma_alignment
);
804 void blk_dump_rq_flags(struct request
*rq
, char *msg
)
808 printk("%s: dev %s: type=%x, flags=%x\n", msg
,
809 rq
->rq_disk
? rq
->rq_disk
->disk_name
: "?", rq
->cmd_type
,
812 printk("\nsector %llu, nr/cnr %lu/%u\n", (unsigned long long)rq
->sector
,
814 rq
->current_nr_sectors
);
815 printk("bio %p, biotail %p, buffer %p, data %p, len %u\n", rq
->bio
, rq
->biotail
, rq
->buffer
, rq
->data
, rq
->data_len
);
817 if (blk_pc_request(rq
)) {
819 for (bit
= 0; bit
< sizeof(rq
->cmd
); bit
++)
820 printk("%02x ", rq
->cmd
[bit
]);
825 EXPORT_SYMBOL(blk_dump_rq_flags
);
827 void blk_recount_segments(struct request_queue
*q
, struct bio
*bio
)
830 struct bio
*nxt
= bio
->bi_next
;
832 rq
.bio
= rq
.biotail
= bio
;
834 blk_recalc_rq_segments(&rq
);
836 bio
->bi_phys_segments
= rq
.nr_phys_segments
;
837 bio
->bi_hw_segments
= rq
.nr_hw_segments
;
838 bio
->bi_flags
|= (1 << BIO_SEG_VALID
);
840 EXPORT_SYMBOL(blk_recount_segments
);
842 static void blk_recalc_rq_segments(struct request
*rq
)
846 unsigned int phys_size
;
847 unsigned int hw_size
;
848 struct bio_vec
*bv
, *bvprv
= NULL
;
852 struct req_iterator iter
;
853 int high
, highprv
= 1;
854 struct request_queue
*q
= rq
->q
;
859 cluster
= q
->queue_flags
& (1 << QUEUE_FLAG_CLUSTER
);
860 hw_seg_size
= seg_size
= 0;
861 phys_size
= hw_size
= nr_phys_segs
= nr_hw_segs
= 0;
862 rq_for_each_segment(bv
, rq
, iter
) {
864 * the trick here is making sure that a high page is never
865 * considered part of another segment, since that might
866 * change with the bounce page.
868 high
= page_to_pfn(bv
->bv_page
) > q
->bounce_pfn
;
872 if (seg_size
+ bv
->bv_len
> q
->max_segment_size
)
874 if (!BIOVEC_PHYS_MERGEABLE(bvprv
, bv
))
876 if (!BIOVEC_SEG_BOUNDARY(q
, bvprv
, bv
))
878 if (BIOVEC_VIRT_OVERSIZE(hw_seg_size
+ bv
->bv_len
))
881 seg_size
+= bv
->bv_len
;
882 hw_seg_size
+= bv
->bv_len
;
887 if (BIOVEC_VIRT_MERGEABLE(bvprv
, bv
) &&
888 !BIOVEC_VIRT_OVERSIZE(hw_seg_size
+ bv
->bv_len
))
889 hw_seg_size
+= bv
->bv_len
;
892 if (nr_hw_segs
== 1 &&
893 hw_seg_size
> rq
->bio
->bi_hw_front_size
)
894 rq
->bio
->bi_hw_front_size
= hw_seg_size
;
895 hw_seg_size
= BIOVEC_VIRT_START_SIZE(bv
) + bv
->bv_len
;
901 seg_size
= bv
->bv_len
;
905 if (nr_hw_segs
== 1 &&
906 hw_seg_size
> rq
->bio
->bi_hw_front_size
)
907 rq
->bio
->bi_hw_front_size
= hw_seg_size
;
908 if (hw_seg_size
> rq
->biotail
->bi_hw_back_size
)
909 rq
->biotail
->bi_hw_back_size
= hw_seg_size
;
910 rq
->nr_phys_segments
= nr_phys_segs
;
911 rq
->nr_hw_segments
= nr_hw_segs
;
914 static int blk_phys_contig_segment(struct request_queue
*q
, struct bio
*bio
,
917 if (!(q
->queue_flags
& (1 << QUEUE_FLAG_CLUSTER
)))
920 if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio
), __BVEC_START(nxt
)))
922 if (bio
->bi_size
+ nxt
->bi_size
> q
->max_segment_size
)
926 * bio and nxt are contigous in memory, check if the queue allows
927 * these two to be merged into one
929 if (BIO_SEG_BOUNDARY(q
, bio
, nxt
))
935 static int blk_hw_contig_segment(struct request_queue
*q
, struct bio
*bio
,
938 if (unlikely(!bio_flagged(bio
, BIO_SEG_VALID
)))
939 blk_recount_segments(q
, bio
);
940 if (unlikely(!bio_flagged(nxt
, BIO_SEG_VALID
)))
941 blk_recount_segments(q
, nxt
);
942 if (!BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio
), __BVEC_START(nxt
)) ||
943 BIOVEC_VIRT_OVERSIZE(bio
->bi_hw_back_size
+ nxt
->bi_hw_front_size
))
945 if (bio
->bi_hw_back_size
+ nxt
->bi_hw_front_size
> q
->max_segment_size
)
952 * map a request to scatterlist, return number of sg entries setup. Caller
953 * must make sure sg can hold rq->nr_phys_segments entries
955 int blk_rq_map_sg(struct request_queue
*q
, struct request
*rq
,
956 struct scatterlist
*sglist
)
958 struct bio_vec
*bvec
, *bvprv
;
959 struct req_iterator iter
;
960 struct scatterlist
*sg
;
964 cluster
= q
->queue_flags
& (1 << QUEUE_FLAG_CLUSTER
);
971 rq_for_each_segment(bvec
, rq
, iter
) {
972 int nbytes
= bvec
->bv_len
;
974 if (bvprv
&& cluster
) {
975 if (sg
->length
+ nbytes
> q
->max_segment_size
)
978 if (!BIOVEC_PHYS_MERGEABLE(bvprv
, bvec
))
980 if (!BIOVEC_SEG_BOUNDARY(q
, bvprv
, bvec
))
983 sg
->length
+= nbytes
;
990 * If the driver previously mapped a shorter
991 * list, we could see a termination bit
992 * prematurely unless it fully inits the sg
993 * table on each mapping. We KNOW that there
994 * must be more entries here or the driver
995 * would be buggy, so force clear the
996 * termination bit to avoid doing a full
997 * sg_init_table() in drivers for each command.
999 sg
->page_link
&= ~0x02;
1003 sg_set_page(sg
, bvec
->bv_page
, nbytes
, bvec
->bv_offset
);
1007 } /* segments in rq */
1009 if (q
->dma_drain_size
) {
1010 sg
->page_link
&= ~0x02;
1012 sg_set_page(sg
, virt_to_page(q
->dma_drain_buffer
),
1014 ((unsigned long)q
->dma_drain_buffer
) &
1025 EXPORT_SYMBOL(blk_rq_map_sg
);
1028 * the standard queue merge functions, can be overridden with device
1029 * specific ones if so desired
1032 static inline int ll_new_mergeable(struct request_queue
*q
,
1033 struct request
*req
,
1036 int nr_phys_segs
= bio_phys_segments(q
, bio
);
1038 if (req
->nr_phys_segments
+ nr_phys_segs
> q
->max_phys_segments
) {
1039 req
->cmd_flags
|= REQ_NOMERGE
;
1040 if (req
== q
->last_merge
)
1041 q
->last_merge
= NULL
;
1046 * A hw segment is just getting larger, bump just the phys
1049 req
->nr_phys_segments
+= nr_phys_segs
;
1053 static inline int ll_new_hw_segment(struct request_queue
*q
,
1054 struct request
*req
,
1057 int nr_hw_segs
= bio_hw_segments(q
, bio
);
1058 int nr_phys_segs
= bio_phys_segments(q
, bio
);
1060 if (req
->nr_hw_segments
+ nr_hw_segs
> q
->max_hw_segments
1061 || req
->nr_phys_segments
+ nr_phys_segs
> q
->max_phys_segments
) {
1062 req
->cmd_flags
|= REQ_NOMERGE
;
1063 if (req
== q
->last_merge
)
1064 q
->last_merge
= NULL
;
1069 * This will form the start of a new hw segment. Bump both
1072 req
->nr_hw_segments
+= nr_hw_segs
;
1073 req
->nr_phys_segments
+= nr_phys_segs
;
1077 static int ll_back_merge_fn(struct request_queue
*q
, struct request
*req
,
1080 unsigned short max_sectors
;
1083 if (unlikely(blk_pc_request(req
)))
1084 max_sectors
= q
->max_hw_sectors
;
1086 max_sectors
= q
->max_sectors
;
1088 if (req
->nr_sectors
+ bio_sectors(bio
) > max_sectors
) {
1089 req
->cmd_flags
|= REQ_NOMERGE
;
1090 if (req
== q
->last_merge
)
1091 q
->last_merge
= NULL
;
1094 if (unlikely(!bio_flagged(req
->biotail
, BIO_SEG_VALID
)))
1095 blk_recount_segments(q
, req
->biotail
);
1096 if (unlikely(!bio_flagged(bio
, BIO_SEG_VALID
)))
1097 blk_recount_segments(q
, bio
);
1098 len
= req
->biotail
->bi_hw_back_size
+ bio
->bi_hw_front_size
;
1099 if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(req
->biotail
), __BVEC_START(bio
)) &&
1100 !BIOVEC_VIRT_OVERSIZE(len
)) {
1101 int mergeable
= ll_new_mergeable(q
, req
, bio
);
1104 if (req
->nr_hw_segments
== 1)
1105 req
->bio
->bi_hw_front_size
= len
;
1106 if (bio
->bi_hw_segments
== 1)
1107 bio
->bi_hw_back_size
= len
;
1112 return ll_new_hw_segment(q
, req
, bio
);
1115 static int ll_front_merge_fn(struct request_queue
*q
, struct request
*req
,
1118 unsigned short max_sectors
;
1121 if (unlikely(blk_pc_request(req
)))
1122 max_sectors
= q
->max_hw_sectors
;
1124 max_sectors
= q
->max_sectors
;
1127 if (req
->nr_sectors
+ bio_sectors(bio
) > max_sectors
) {
1128 req
->cmd_flags
|= REQ_NOMERGE
;
1129 if (req
== q
->last_merge
)
1130 q
->last_merge
= NULL
;
1133 len
= bio
->bi_hw_back_size
+ req
->bio
->bi_hw_front_size
;
1134 if (unlikely(!bio_flagged(bio
, BIO_SEG_VALID
)))
1135 blk_recount_segments(q
, bio
);
1136 if (unlikely(!bio_flagged(req
->bio
, BIO_SEG_VALID
)))
1137 blk_recount_segments(q
, req
->bio
);
1138 if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio
), __BVEC_START(req
->bio
)) &&
1139 !BIOVEC_VIRT_OVERSIZE(len
)) {
1140 int mergeable
= ll_new_mergeable(q
, req
, bio
);
1143 if (bio
->bi_hw_segments
== 1)
1144 bio
->bi_hw_front_size
= len
;
1145 if (req
->nr_hw_segments
== 1)
1146 req
->biotail
->bi_hw_back_size
= len
;
1151 return ll_new_hw_segment(q
, req
, bio
);
1154 static int ll_merge_requests_fn(struct request_queue
*q
, struct request
*req
,
1155 struct request
*next
)
1157 int total_phys_segments
;
1158 int total_hw_segments
;
1161 * First check if the either of the requests are re-queued
1162 * requests. Can't merge them if they are.
1164 if (req
->special
|| next
->special
)
1168 * Will it become too large?
1170 if ((req
->nr_sectors
+ next
->nr_sectors
) > q
->max_sectors
)
1173 total_phys_segments
= req
->nr_phys_segments
+ next
->nr_phys_segments
;
1174 if (blk_phys_contig_segment(q
, req
->biotail
, next
->bio
))
1175 total_phys_segments
--;
1177 if (total_phys_segments
> q
->max_phys_segments
)
1180 total_hw_segments
= req
->nr_hw_segments
+ next
->nr_hw_segments
;
1181 if (blk_hw_contig_segment(q
, req
->biotail
, next
->bio
)) {
1182 int len
= req
->biotail
->bi_hw_back_size
+ next
->bio
->bi_hw_front_size
;
1184 * propagate the combined length to the end of the requests
1186 if (req
->nr_hw_segments
== 1)
1187 req
->bio
->bi_hw_front_size
= len
;
1188 if (next
->nr_hw_segments
== 1)
1189 next
->biotail
->bi_hw_back_size
= len
;
1190 total_hw_segments
--;
1193 if (total_hw_segments
> q
->max_hw_segments
)
1196 /* Merge is OK... */
1197 req
->nr_phys_segments
= total_phys_segments
;
1198 req
->nr_hw_segments
= total_hw_segments
;
1203 * "plug" the device if there are no outstanding requests: this will
1204 * force the transfer to start only after we have put all the requests
1207 * This is called with interrupts off and no requests on the queue and
1208 * with the queue lock held.
1210 void blk_plug_device(struct request_queue
*q
)
1212 WARN_ON(!irqs_disabled());
1215 * don't plug a stopped queue, it must be paired with blk_start_queue()
1216 * which will restart the queueing
1218 if (blk_queue_stopped(q
))
1221 if (!test_and_set_bit(QUEUE_FLAG_PLUGGED
, &q
->queue_flags
)) {
1222 mod_timer(&q
->unplug_timer
, jiffies
+ q
->unplug_delay
);
1223 blk_add_trace_generic(q
, NULL
, 0, BLK_TA_PLUG
);
1227 EXPORT_SYMBOL(blk_plug_device
);
1230 * remove the queue from the plugged list, if present. called with
1231 * queue lock held and interrupts disabled.
1233 int blk_remove_plug(struct request_queue
*q
)
1235 WARN_ON(!irqs_disabled());
1237 if (!test_and_clear_bit(QUEUE_FLAG_PLUGGED
, &q
->queue_flags
))
1240 del_timer(&q
->unplug_timer
);
1244 EXPORT_SYMBOL(blk_remove_plug
);
1247 * remove the plug and let it rip..
1249 void __generic_unplug_device(struct request_queue
*q
)
1251 if (unlikely(blk_queue_stopped(q
)))
1254 if (!blk_remove_plug(q
))
1259 EXPORT_SYMBOL(__generic_unplug_device
);
1262 * generic_unplug_device - fire a request queue
1263 * @q: The &struct request_queue in question
1266 * Linux uses plugging to build bigger requests queues before letting
1267 * the device have at them. If a queue is plugged, the I/O scheduler
1268 * is still adding and merging requests on the queue. Once the queue
1269 * gets unplugged, the request_fn defined for the queue is invoked and
1270 * transfers started.
1272 void generic_unplug_device(struct request_queue
*q
)
1274 spin_lock_irq(q
->queue_lock
);
1275 __generic_unplug_device(q
);
1276 spin_unlock_irq(q
->queue_lock
);
1278 EXPORT_SYMBOL(generic_unplug_device
);
1280 static void blk_backing_dev_unplug(struct backing_dev_info
*bdi
,
1283 struct request_queue
*q
= bdi
->unplug_io_data
;
1288 static void blk_unplug_work(struct work_struct
*work
)
1290 struct request_queue
*q
=
1291 container_of(work
, struct request_queue
, unplug_work
);
1293 blk_add_trace_pdu_int(q
, BLK_TA_UNPLUG_IO
, NULL
,
1294 q
->rq
.count
[READ
] + q
->rq
.count
[WRITE
]);
1299 static void blk_unplug_timeout(unsigned long data
)
1301 struct request_queue
*q
= (struct request_queue
*)data
;
1303 blk_add_trace_pdu_int(q
, BLK_TA_UNPLUG_TIMER
, NULL
,
1304 q
->rq
.count
[READ
] + q
->rq
.count
[WRITE
]);
1306 kblockd_schedule_work(&q
->unplug_work
);
1309 void blk_unplug(struct request_queue
*q
)
1312 * devices don't necessarily have an ->unplug_fn defined
1315 blk_add_trace_pdu_int(q
, BLK_TA_UNPLUG_IO
, NULL
,
1316 q
->rq
.count
[READ
] + q
->rq
.count
[WRITE
]);
1321 EXPORT_SYMBOL(blk_unplug
);
1324 * blk_start_queue - restart a previously stopped queue
1325 * @q: The &struct request_queue in question
1328 * blk_start_queue() will clear the stop flag on the queue, and call
1329 * the request_fn for the queue if it was in a stopped state when
1330 * entered. Also see blk_stop_queue(). Queue lock must be held.
1332 void blk_start_queue(struct request_queue
*q
)
1334 WARN_ON(!irqs_disabled());
1336 clear_bit(QUEUE_FLAG_STOPPED
, &q
->queue_flags
);
1339 * one level of recursion is ok and is much faster than kicking
1340 * the unplug handling
1342 if (!test_and_set_bit(QUEUE_FLAG_REENTER
, &q
->queue_flags
)) {
1344 clear_bit(QUEUE_FLAG_REENTER
, &q
->queue_flags
);
1347 kblockd_schedule_work(&q
->unplug_work
);
1351 EXPORT_SYMBOL(blk_start_queue
);
1354 * blk_stop_queue - stop a queue
1355 * @q: The &struct request_queue in question
1358 * The Linux block layer assumes that a block driver will consume all
1359 * entries on the request queue when the request_fn strategy is called.
1360 * Often this will not happen, because of hardware limitations (queue
1361 * depth settings). If a device driver gets a 'queue full' response,
1362 * or if it simply chooses not to queue more I/O at one point, it can
1363 * call this function to prevent the request_fn from being called until
1364 * the driver has signalled it's ready to go again. This happens by calling
1365 * blk_start_queue() to restart queue operations. Queue lock must be held.
1367 void blk_stop_queue(struct request_queue
*q
)
1370 set_bit(QUEUE_FLAG_STOPPED
, &q
->queue_flags
);
1372 EXPORT_SYMBOL(blk_stop_queue
);
1375 * blk_sync_queue - cancel any pending callbacks on a queue
1379 * The block layer may perform asynchronous callback activity
1380 * on a queue, such as calling the unplug function after a timeout.
1381 * A block device may call blk_sync_queue to ensure that any
1382 * such activity is cancelled, thus allowing it to release resources
1383 * that the callbacks might use. The caller must already have made sure
1384 * that its ->make_request_fn will not re-add plugging prior to calling
1388 void blk_sync_queue(struct request_queue
*q
)
1390 del_timer_sync(&q
->unplug_timer
);
1391 kblockd_flush_work(&q
->unplug_work
);
1393 EXPORT_SYMBOL(blk_sync_queue
);
1396 * blk_run_queue - run a single device queue
1397 * @q: The queue to run
1399 void blk_run_queue(struct request_queue
*q
)
1401 unsigned long flags
;
1403 spin_lock_irqsave(q
->queue_lock
, flags
);
1407 * Only recurse once to avoid overrunning the stack, let the unplug
1408 * handling reinvoke the handler shortly if we already got there.
1410 if (!elv_queue_empty(q
)) {
1411 if (!test_and_set_bit(QUEUE_FLAG_REENTER
, &q
->queue_flags
)) {
1413 clear_bit(QUEUE_FLAG_REENTER
, &q
->queue_flags
);
1416 kblockd_schedule_work(&q
->unplug_work
);
1420 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1422 EXPORT_SYMBOL(blk_run_queue
);
1424 void blk_put_queue(struct request_queue
*q
)
1426 kobject_put(&q
->kobj
);
1428 EXPORT_SYMBOL(blk_put_queue
);
1430 void blk_cleanup_queue(struct request_queue
* q
)
1432 mutex_lock(&q
->sysfs_lock
);
1433 set_bit(QUEUE_FLAG_DEAD
, &q
->queue_flags
);
1434 mutex_unlock(&q
->sysfs_lock
);
1437 elevator_exit(q
->elevator
);
1442 EXPORT_SYMBOL(blk_cleanup_queue
);
1444 static int blk_init_free_list(struct request_queue
*q
)
1446 struct request_list
*rl
= &q
->rq
;
1448 rl
->count
[READ
] = rl
->count
[WRITE
] = 0;
1449 rl
->starved
[READ
] = rl
->starved
[WRITE
] = 0;
1451 init_waitqueue_head(&rl
->wait
[READ
]);
1452 init_waitqueue_head(&rl
->wait
[WRITE
]);
1454 rl
->rq_pool
= mempool_create_node(BLKDEV_MIN_RQ
, mempool_alloc_slab
,
1455 mempool_free_slab
, request_cachep
, q
->node
);
1463 struct request_queue
*blk_alloc_queue(gfp_t gfp_mask
)
1465 return blk_alloc_queue_node(gfp_mask
, -1);
1467 EXPORT_SYMBOL(blk_alloc_queue
);
1469 struct request_queue
*blk_alloc_queue_node(gfp_t gfp_mask
, int node_id
)
1471 struct request_queue
*q
;
1474 q
= kmem_cache_alloc_node(blk_requestq_cachep
,
1475 gfp_mask
| __GFP_ZERO
, node_id
);
1479 q
->backing_dev_info
.unplug_io_fn
= blk_backing_dev_unplug
;
1480 q
->backing_dev_info
.unplug_io_data
= q
;
1481 err
= bdi_init(&q
->backing_dev_info
);
1483 kmem_cache_free(blk_requestq_cachep
, q
);
1487 init_timer(&q
->unplug_timer
);
1489 kobject_init(&q
->kobj
, &blk_queue_ktype
);
1491 mutex_init(&q
->sysfs_lock
);
1495 EXPORT_SYMBOL(blk_alloc_queue_node
);
1498 * blk_init_queue - prepare a request queue for use with a block device
1499 * @rfn: The function to be called to process requests that have been
1500 * placed on the queue.
1501 * @lock: Request queue spin lock
1504 * If a block device wishes to use the standard request handling procedures,
1505 * which sorts requests and coalesces adjacent requests, then it must
1506 * call blk_init_queue(). The function @rfn will be called when there
1507 * are requests on the queue that need to be processed. If the device
1508 * supports plugging, then @rfn may not be called immediately when requests
1509 * are available on the queue, but may be called at some time later instead.
1510 * Plugged queues are generally unplugged when a buffer belonging to one
1511 * of the requests on the queue is needed, or due to memory pressure.
1513 * @rfn is not required, or even expected, to remove all requests off the
1514 * queue, but only as many as it can handle at a time. If it does leave
1515 * requests on the queue, it is responsible for arranging that the requests
1516 * get dealt with eventually.
1518 * The queue spin lock must be held while manipulating the requests on the
1519 * request queue; this lock will be taken also from interrupt context, so irq
1520 * disabling is needed for it.
1522 * Function returns a pointer to the initialized request queue, or NULL if
1523 * it didn't succeed.
1526 * blk_init_queue() must be paired with a blk_cleanup_queue() call
1527 * when the block device is deactivated (such as at module unload).
1530 struct request_queue
*blk_init_queue(request_fn_proc
*rfn
, spinlock_t
*lock
)
1532 return blk_init_queue_node(rfn
, lock
, -1);
1534 EXPORT_SYMBOL(blk_init_queue
);
1536 struct request_queue
*
1537 blk_init_queue_node(request_fn_proc
*rfn
, spinlock_t
*lock
, int node_id
)
1539 struct request_queue
*q
= blk_alloc_queue_node(GFP_KERNEL
, node_id
);
1545 if (blk_init_free_list(q
)) {
1546 kmem_cache_free(blk_requestq_cachep
, q
);
1551 * if caller didn't supply a lock, they get per-queue locking with
1555 spin_lock_init(&q
->__queue_lock
);
1556 lock
= &q
->__queue_lock
;
1559 q
->request_fn
= rfn
;
1560 q
->prep_rq_fn
= NULL
;
1561 q
->unplug_fn
= generic_unplug_device
;
1562 q
->queue_flags
= (1 << QUEUE_FLAG_CLUSTER
);
1563 q
->queue_lock
= lock
;
1565 blk_queue_segment_boundary(q
, 0xffffffff);
1567 blk_queue_make_request(q
, __make_request
);
1568 blk_queue_max_segment_size(q
, MAX_SEGMENT_SIZE
);
1570 blk_queue_max_hw_segments(q
, MAX_HW_SEGMENTS
);
1571 blk_queue_max_phys_segments(q
, MAX_PHYS_SEGMENTS
);
1573 q
->sg_reserved_size
= INT_MAX
;
1578 if (!elevator_init(q
, NULL
)) {
1579 blk_queue_congestion_threshold(q
);
1586 EXPORT_SYMBOL(blk_init_queue_node
);
1588 int blk_get_queue(struct request_queue
*q
)
1590 if (likely(!test_bit(QUEUE_FLAG_DEAD
, &q
->queue_flags
))) {
1591 kobject_get(&q
->kobj
);
1598 EXPORT_SYMBOL(blk_get_queue
);
1600 static inline void blk_free_request(struct request_queue
*q
, struct request
*rq
)
1602 if (rq
->cmd_flags
& REQ_ELVPRIV
)
1603 elv_put_request(q
, rq
);
1604 mempool_free(rq
, q
->rq
.rq_pool
);
1607 static struct request
*
1608 blk_alloc_request(struct request_queue
*q
, int rw
, int priv
, gfp_t gfp_mask
)
1610 struct request
*rq
= mempool_alloc(q
->rq
.rq_pool
, gfp_mask
);
1616 * first three bits are identical in rq->cmd_flags and bio->bi_rw,
1617 * see bio.h and blkdev.h
1619 rq
->cmd_flags
= rw
| REQ_ALLOCED
;
1622 if (unlikely(elv_set_request(q
, rq
, gfp_mask
))) {
1623 mempool_free(rq
, q
->rq
.rq_pool
);
1626 rq
->cmd_flags
|= REQ_ELVPRIV
;
1633 * ioc_batching returns true if the ioc is a valid batching request and
1634 * should be given priority access to a request.
1636 static inline int ioc_batching(struct request_queue
*q
, struct io_context
*ioc
)
1642 * Make sure the process is able to allocate at least 1 request
1643 * even if the batch times out, otherwise we could theoretically
1646 return ioc
->nr_batch_requests
== q
->nr_batching
||
1647 (ioc
->nr_batch_requests
> 0
1648 && time_before(jiffies
, ioc
->last_waited
+ BLK_BATCH_TIME
));
1652 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
1653 * will cause the process to be a "batcher" on all queues in the system. This
1654 * is the behaviour we want though - once it gets a wakeup it should be given
1657 static void ioc_set_batching(struct request_queue
*q
, struct io_context
*ioc
)
1659 if (!ioc
|| ioc_batching(q
, ioc
))
1662 ioc
->nr_batch_requests
= q
->nr_batching
;
1663 ioc
->last_waited
= jiffies
;
1666 static void __freed_request(struct request_queue
*q
, int rw
)
1668 struct request_list
*rl
= &q
->rq
;
1670 if (rl
->count
[rw
] < queue_congestion_off_threshold(q
))
1671 blk_clear_queue_congested(q
, rw
);
1673 if (rl
->count
[rw
] + 1 <= q
->nr_requests
) {
1674 if (waitqueue_active(&rl
->wait
[rw
]))
1675 wake_up(&rl
->wait
[rw
]);
1677 blk_clear_queue_full(q
, rw
);
1682 * A request has just been released. Account for it, update the full and
1683 * congestion status, wake up any waiters. Called under q->queue_lock.
1685 static void freed_request(struct request_queue
*q
, int rw
, int priv
)
1687 struct request_list
*rl
= &q
->rq
;
1693 __freed_request(q
, rw
);
1695 if (unlikely(rl
->starved
[rw
^ 1]))
1696 __freed_request(q
, rw
^ 1);
1699 #define blkdev_free_rq(list) list_entry((list)->next, struct request, queuelist)
1701 * Get a free request, queue_lock must be held.
1702 * Returns NULL on failure, with queue_lock held.
1703 * Returns !NULL on success, with queue_lock *not held*.
1705 static struct request
*get_request(struct request_queue
*q
, int rw_flags
,
1706 struct bio
*bio
, gfp_t gfp_mask
)
1708 struct request
*rq
= NULL
;
1709 struct request_list
*rl
= &q
->rq
;
1710 struct io_context
*ioc
= NULL
;
1711 const int rw
= rw_flags
& 0x01;
1712 int may_queue
, priv
;
1714 may_queue
= elv_may_queue(q
, rw_flags
);
1715 if (may_queue
== ELV_MQUEUE_NO
)
1718 if (rl
->count
[rw
]+1 >= queue_congestion_on_threshold(q
)) {
1719 if (rl
->count
[rw
]+1 >= q
->nr_requests
) {
1720 ioc
= current_io_context(GFP_ATOMIC
, q
->node
);
1722 * The queue will fill after this allocation, so set
1723 * it as full, and mark this process as "batching".
1724 * This process will be allowed to complete a batch of
1725 * requests, others will be blocked.
1727 if (!blk_queue_full(q
, rw
)) {
1728 ioc_set_batching(q
, ioc
);
1729 blk_set_queue_full(q
, rw
);
1731 if (may_queue
!= ELV_MQUEUE_MUST
1732 && !ioc_batching(q
, ioc
)) {
1734 * The queue is full and the allocating
1735 * process is not a "batcher", and not
1736 * exempted by the IO scheduler
1742 blk_set_queue_congested(q
, rw
);
1746 * Only allow batching queuers to allocate up to 50% over the defined
1747 * limit of requests, otherwise we could have thousands of requests
1748 * allocated with any setting of ->nr_requests
1750 if (rl
->count
[rw
] >= (3 * q
->nr_requests
/ 2))
1754 rl
->starved
[rw
] = 0;
1756 priv
= !test_bit(QUEUE_FLAG_ELVSWITCH
, &q
->queue_flags
);
1760 spin_unlock_irq(q
->queue_lock
);
1762 rq
= blk_alloc_request(q
, rw_flags
, priv
, gfp_mask
);
1763 if (unlikely(!rq
)) {
1765 * Allocation failed presumably due to memory. Undo anything
1766 * we might have messed up.
1768 * Allocating task should really be put onto the front of the
1769 * wait queue, but this is pretty rare.
1771 spin_lock_irq(q
->queue_lock
);
1772 freed_request(q
, rw
, priv
);
1775 * in the very unlikely event that allocation failed and no
1776 * requests for this direction was pending, mark us starved
1777 * so that freeing of a request in the other direction will
1778 * notice us. another possible fix would be to split the
1779 * rq mempool into READ and WRITE
1782 if (unlikely(rl
->count
[rw
] == 0))
1783 rl
->starved
[rw
] = 1;
1789 * ioc may be NULL here, and ioc_batching will be false. That's
1790 * OK, if the queue is under the request limit then requests need
1791 * not count toward the nr_batch_requests limit. There will always
1792 * be some limit enforced by BLK_BATCH_TIME.
1794 if (ioc_batching(q
, ioc
))
1795 ioc
->nr_batch_requests
--;
1799 blk_add_trace_generic(q
, bio
, rw
, BLK_TA_GETRQ
);
1805 * No available requests for this queue, unplug the device and wait for some
1806 * requests to become available.
1808 * Called with q->queue_lock held, and returns with it unlocked.
1810 static struct request
*get_request_wait(struct request_queue
*q
, int rw_flags
,
1813 const int rw
= rw_flags
& 0x01;
1816 rq
= get_request(q
, rw_flags
, bio
, GFP_NOIO
);
1819 struct request_list
*rl
= &q
->rq
;
1821 prepare_to_wait_exclusive(&rl
->wait
[rw
], &wait
,
1822 TASK_UNINTERRUPTIBLE
);
1824 rq
= get_request(q
, rw_flags
, bio
, GFP_NOIO
);
1827 struct io_context
*ioc
;
1829 blk_add_trace_generic(q
, bio
, rw
, BLK_TA_SLEEPRQ
);
1831 __generic_unplug_device(q
);
1832 spin_unlock_irq(q
->queue_lock
);
1836 * After sleeping, we become a "batching" process and
1837 * will be able to allocate at least one request, and
1838 * up to a big batch of them for a small period time.
1839 * See ioc_batching, ioc_set_batching
1841 ioc
= current_io_context(GFP_NOIO
, q
->node
);
1842 ioc_set_batching(q
, ioc
);
1844 spin_lock_irq(q
->queue_lock
);
1846 finish_wait(&rl
->wait
[rw
], &wait
);
1852 struct request
*blk_get_request(struct request_queue
*q
, int rw
, gfp_t gfp_mask
)
1856 BUG_ON(rw
!= READ
&& rw
!= WRITE
);
1858 spin_lock_irq(q
->queue_lock
);
1859 if (gfp_mask
& __GFP_WAIT
) {
1860 rq
= get_request_wait(q
, rw
, NULL
);
1862 rq
= get_request(q
, rw
, NULL
, gfp_mask
);
1864 spin_unlock_irq(q
->queue_lock
);
1866 /* q->queue_lock is unlocked at this point */
1870 EXPORT_SYMBOL(blk_get_request
);
1873 * blk_start_queueing - initiate dispatch of requests to device
1874 * @q: request queue to kick into gear
1876 * This is basically a helper to remove the need to know whether a queue
1877 * is plugged or not if someone just wants to initiate dispatch of requests
1880 * The queue lock must be held with interrupts disabled.
1882 void blk_start_queueing(struct request_queue
*q
)
1884 if (!blk_queue_plugged(q
))
1887 __generic_unplug_device(q
);
1889 EXPORT_SYMBOL(blk_start_queueing
);
1892 * blk_requeue_request - put a request back on queue
1893 * @q: request queue where request should be inserted
1894 * @rq: request to be inserted
1897 * Drivers often keep queueing requests until the hardware cannot accept
1898 * more, when that condition happens we need to put the request back
1899 * on the queue. Must be called with queue lock held.
1901 void blk_requeue_request(struct request_queue
*q
, struct request
*rq
)
1903 blk_add_trace_rq(q
, rq
, BLK_TA_REQUEUE
);
1905 if (blk_rq_tagged(rq
))
1906 blk_queue_end_tag(q
, rq
);
1908 elv_requeue_request(q
, rq
);
1911 EXPORT_SYMBOL(blk_requeue_request
);
1914 * blk_insert_request - insert a special request in to a request queue
1915 * @q: request queue where request should be inserted
1916 * @rq: request to be inserted
1917 * @at_head: insert request at head or tail of queue
1918 * @data: private data
1921 * Many block devices need to execute commands asynchronously, so they don't
1922 * block the whole kernel from preemption during request execution. This is
1923 * accomplished normally by inserting aritficial requests tagged as
1924 * REQ_SPECIAL in to the corresponding request queue, and letting them be
1925 * scheduled for actual execution by the request queue.
1927 * We have the option of inserting the head or the tail of the queue.
1928 * Typically we use the tail for new ioctls and so forth. We use the head
1929 * of the queue for things like a QUEUE_FULL message from a device, or a
1930 * host that is unable to accept a particular command.
1932 void blk_insert_request(struct request_queue
*q
, struct request
*rq
,
1933 int at_head
, void *data
)
1935 int where
= at_head
? ELEVATOR_INSERT_FRONT
: ELEVATOR_INSERT_BACK
;
1936 unsigned long flags
;
1939 * tell I/O scheduler that this isn't a regular read/write (ie it
1940 * must not attempt merges on this) and that it acts as a soft
1943 rq
->cmd_type
= REQ_TYPE_SPECIAL
;
1944 rq
->cmd_flags
|= REQ_SOFTBARRIER
;
1948 spin_lock_irqsave(q
->queue_lock
, flags
);
1951 * If command is tagged, release the tag
1953 if (blk_rq_tagged(rq
))
1954 blk_queue_end_tag(q
, rq
);
1956 drive_stat_acct(rq
, 1);
1957 __elv_add_request(q
, rq
, where
, 0);
1958 blk_start_queueing(q
);
1959 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1962 EXPORT_SYMBOL(blk_insert_request
);
1964 static int __blk_rq_unmap_user(struct bio
*bio
)
1969 if (bio_flagged(bio
, BIO_USER_MAPPED
))
1970 bio_unmap_user(bio
);
1972 ret
= bio_uncopy_user(bio
);
1978 int blk_rq_append_bio(struct request_queue
*q
, struct request
*rq
,
1982 blk_rq_bio_prep(q
, rq
, bio
);
1983 else if (!ll_back_merge_fn(q
, rq
, bio
))
1986 rq
->biotail
->bi_next
= bio
;
1989 rq
->data_len
+= bio
->bi_size
;
1993 EXPORT_SYMBOL(blk_rq_append_bio
);
1995 static int __blk_rq_map_user(struct request_queue
*q
, struct request
*rq
,
1996 void __user
*ubuf
, unsigned int len
)
1998 unsigned long uaddr
;
1999 struct bio
*bio
, *orig_bio
;
2002 reading
= rq_data_dir(rq
) == READ
;
2005 * if alignment requirement is satisfied, map in user pages for
2006 * direct dma. else, set up kernel bounce buffers
2008 uaddr
= (unsigned long) ubuf
;
2009 if (!(uaddr
& queue_dma_alignment(q
)) && !(len
& queue_dma_alignment(q
)))
2010 bio
= bio_map_user(q
, NULL
, uaddr
, len
, reading
);
2012 bio
= bio_copy_user(q
, uaddr
, len
, reading
);
2015 return PTR_ERR(bio
);
2018 blk_queue_bounce(q
, &bio
);
2021 * We link the bounce buffer in and could have to traverse it
2022 * later so we have to get a ref to prevent it from being freed
2026 ret
= blk_rq_append_bio(q
, rq
, bio
);
2028 return bio
->bi_size
;
2030 /* if it was boucned we must call the end io function */
2032 __blk_rq_unmap_user(orig_bio
);
2038 * blk_rq_map_user - map user data to a request, for REQ_BLOCK_PC usage
2039 * @q: request queue where request should be inserted
2040 * @rq: request structure to fill
2041 * @ubuf: the user buffer
2042 * @len: length of user data
2045 * Data will be mapped directly for zero copy io, if possible. Otherwise
2046 * a kernel bounce buffer is used.
2048 * A matching blk_rq_unmap_user() must be issued at the end of io, while
2049 * still in process context.
2051 * Note: The mapped bio may need to be bounced through blk_queue_bounce()
2052 * before being submitted to the device, as pages mapped may be out of
2053 * reach. It's the callers responsibility to make sure this happens. The
2054 * original bio must be passed back in to blk_rq_unmap_user() for proper
2057 int blk_rq_map_user(struct request_queue
*q
, struct request
*rq
,
2058 void __user
*ubuf
, unsigned long len
)
2060 unsigned long bytes_read
= 0;
2061 struct bio
*bio
= NULL
;
2064 if (len
> (q
->max_hw_sectors
<< 9))
2069 while (bytes_read
!= len
) {
2070 unsigned long map_len
, end
, start
;
2072 map_len
= min_t(unsigned long, len
- bytes_read
, BIO_MAX_SIZE
);
2073 end
= ((unsigned long)ubuf
+ map_len
+ PAGE_SIZE
- 1)
2075 start
= (unsigned long)ubuf
>> PAGE_SHIFT
;
2078 * A bad offset could cause us to require BIO_MAX_PAGES + 1
2079 * pages. If this happens we just lower the requested
2080 * mapping len by a page so that we can fit
2082 if (end
- start
> BIO_MAX_PAGES
)
2083 map_len
-= PAGE_SIZE
;
2085 ret
= __blk_rq_map_user(q
, rq
, ubuf
, map_len
);
2094 rq
->buffer
= rq
->data
= NULL
;
2097 blk_rq_unmap_user(bio
);
2101 EXPORT_SYMBOL(blk_rq_map_user
);
2104 * blk_rq_map_user_iov - map user data to a request, for REQ_BLOCK_PC usage
2105 * @q: request queue where request should be inserted
2106 * @rq: request to map data to
2107 * @iov: pointer to the iovec
2108 * @iov_count: number of elements in the iovec
2109 * @len: I/O byte count
2112 * Data will be mapped directly for zero copy io, if possible. Otherwise
2113 * a kernel bounce buffer is used.
2115 * A matching blk_rq_unmap_user() must be issued at the end of io, while
2116 * still in process context.
2118 * Note: The mapped bio may need to be bounced through blk_queue_bounce()
2119 * before being submitted to the device, as pages mapped may be out of
2120 * reach. It's the callers responsibility to make sure this happens. The
2121 * original bio must be passed back in to blk_rq_unmap_user() for proper
2124 int blk_rq_map_user_iov(struct request_queue
*q
, struct request
*rq
,
2125 struct sg_iovec
*iov
, int iov_count
, unsigned int len
)
2129 if (!iov
|| iov_count
<= 0)
2132 /* we don't allow misaligned data like bio_map_user() does. If the
2133 * user is using sg, they're expected to know the alignment constraints
2134 * and respect them accordingly */
2135 bio
= bio_map_user_iov(q
, NULL
, iov
, iov_count
, rq_data_dir(rq
)== READ
);
2137 return PTR_ERR(bio
);
2139 if (bio
->bi_size
!= len
) {
2141 bio_unmap_user(bio
);
2146 blk_rq_bio_prep(q
, rq
, bio
);
2147 rq
->buffer
= rq
->data
= NULL
;
2151 EXPORT_SYMBOL(blk_rq_map_user_iov
);
2154 * blk_rq_unmap_user - unmap a request with user data
2155 * @bio: start of bio list
2158 * Unmap a rq previously mapped by blk_rq_map_user(). The caller must
2159 * supply the original rq->bio from the blk_rq_map_user() return, since
2160 * the io completion may have changed rq->bio.
2162 int blk_rq_unmap_user(struct bio
*bio
)
2164 struct bio
*mapped_bio
;
2169 if (unlikely(bio_flagged(bio
, BIO_BOUNCED
)))
2170 mapped_bio
= bio
->bi_private
;
2172 ret2
= __blk_rq_unmap_user(mapped_bio
);
2178 bio_put(mapped_bio
);
2184 EXPORT_SYMBOL(blk_rq_unmap_user
);
2187 * blk_rq_map_kern - map kernel data to a request, for REQ_BLOCK_PC usage
2188 * @q: request queue where request should be inserted
2189 * @rq: request to fill
2190 * @kbuf: the kernel buffer
2191 * @len: length of user data
2192 * @gfp_mask: memory allocation flags
2194 int blk_rq_map_kern(struct request_queue
*q
, struct request
*rq
, void *kbuf
,
2195 unsigned int len
, gfp_t gfp_mask
)
2199 if (len
> (q
->max_hw_sectors
<< 9))
2204 bio
= bio_map_kern(q
, kbuf
, len
, gfp_mask
);
2206 return PTR_ERR(bio
);
2208 if (rq_data_dir(rq
) == WRITE
)
2209 bio
->bi_rw
|= (1 << BIO_RW
);
2211 blk_rq_bio_prep(q
, rq
, bio
);
2212 blk_queue_bounce(q
, &rq
->bio
);
2213 rq
->buffer
= rq
->data
= NULL
;
2217 EXPORT_SYMBOL(blk_rq_map_kern
);
2220 * blk_execute_rq_nowait - insert a request into queue for execution
2221 * @q: queue to insert the request in
2222 * @bd_disk: matching gendisk
2223 * @rq: request to insert
2224 * @at_head: insert request at head or tail of queue
2225 * @done: I/O completion handler
2228 * Insert a fully prepared request at the back of the io scheduler queue
2229 * for execution. Don't wait for completion.
2231 void blk_execute_rq_nowait(struct request_queue
*q
, struct gendisk
*bd_disk
,
2232 struct request
*rq
, int at_head
,
2235 int where
= at_head
? ELEVATOR_INSERT_FRONT
: ELEVATOR_INSERT_BACK
;
2237 rq
->rq_disk
= bd_disk
;
2238 rq
->cmd_flags
|= REQ_NOMERGE
;
2240 WARN_ON(irqs_disabled());
2241 spin_lock_irq(q
->queue_lock
);
2242 __elv_add_request(q
, rq
, where
, 1);
2243 __generic_unplug_device(q
);
2244 spin_unlock_irq(q
->queue_lock
);
2246 EXPORT_SYMBOL_GPL(blk_execute_rq_nowait
);
2249 * blk_execute_rq - insert a request into queue for execution
2250 * @q: queue to insert the request in
2251 * @bd_disk: matching gendisk
2252 * @rq: request to insert
2253 * @at_head: insert request at head or tail of queue
2256 * Insert a fully prepared request at the back of the io scheduler queue
2257 * for execution and wait for completion.
2259 int blk_execute_rq(struct request_queue
*q
, struct gendisk
*bd_disk
,
2260 struct request
*rq
, int at_head
)
2262 DECLARE_COMPLETION_ONSTACK(wait
);
2263 char sense
[SCSI_SENSE_BUFFERSIZE
];
2267 * we need an extra reference to the request, so we can look at
2268 * it after io completion
2273 memset(sense
, 0, sizeof(sense
));
2278 rq
->end_io_data
= &wait
;
2279 blk_execute_rq_nowait(q
, bd_disk
, rq
, at_head
, blk_end_sync_rq
);
2280 wait_for_completion(&wait
);
2288 EXPORT_SYMBOL(blk_execute_rq
);
2290 static void bio_end_empty_barrier(struct bio
*bio
, int err
)
2293 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
2295 complete(bio
->bi_private
);
2299 * blkdev_issue_flush - queue a flush
2300 * @bdev: blockdev to issue flush for
2301 * @error_sector: error sector
2304 * Issue a flush for the block device in question. Caller can supply
2305 * room for storing the error offset in case of a flush error, if they
2306 * wish to. Caller must run wait_for_completion() on its own.
2308 int blkdev_issue_flush(struct block_device
*bdev
, sector_t
*error_sector
)
2310 DECLARE_COMPLETION_ONSTACK(wait
);
2311 struct request_queue
*q
;
2315 if (bdev
->bd_disk
== NULL
)
2318 q
= bdev_get_queue(bdev
);
2322 bio
= bio_alloc(GFP_KERNEL
, 0);
2326 bio
->bi_end_io
= bio_end_empty_barrier
;
2327 bio
->bi_private
= &wait
;
2328 bio
->bi_bdev
= bdev
;
2329 submit_bio(1 << BIO_RW_BARRIER
, bio
);
2331 wait_for_completion(&wait
);
2334 * The driver must store the error location in ->bi_sector, if
2335 * it supports it. For non-stacked drivers, this should be copied
2339 *error_sector
= bio
->bi_sector
;
2342 if (!bio_flagged(bio
, BIO_UPTODATE
))
2349 EXPORT_SYMBOL(blkdev_issue_flush
);
2351 static void drive_stat_acct(struct request
*rq
, int new_io
)
2353 int rw
= rq_data_dir(rq
);
2355 if (!blk_fs_request(rq
) || !rq
->rq_disk
)
2359 __disk_stat_inc(rq
->rq_disk
, merges
[rw
]);
2361 disk_round_stats(rq
->rq_disk
);
2362 rq
->rq_disk
->in_flight
++;
2367 * add-request adds a request to the linked list.
2368 * queue lock is held and interrupts disabled, as we muck with the
2369 * request queue list.
2371 static inline void add_request(struct request_queue
* q
, struct request
* req
)
2373 drive_stat_acct(req
, 1);
2376 * elevator indicated where it wants this request to be
2377 * inserted at elevator_merge time
2379 __elv_add_request(q
, req
, ELEVATOR_INSERT_SORT
, 0);
2383 * disk_round_stats() - Round off the performance stats on a struct
2386 * The average IO queue length and utilisation statistics are maintained
2387 * by observing the current state of the queue length and the amount of
2388 * time it has been in this state for.
2390 * Normally, that accounting is done on IO completion, but that can result
2391 * in more than a second's worth of IO being accounted for within any one
2392 * second, leading to >100% utilisation. To deal with that, we call this
2393 * function to do a round-off before returning the results when reading
2394 * /proc/diskstats. This accounts immediately for all queue usage up to
2395 * the current jiffies and restarts the counters again.
2397 void disk_round_stats(struct gendisk
*disk
)
2399 unsigned long now
= jiffies
;
2401 if (now
== disk
->stamp
)
2404 if (disk
->in_flight
) {
2405 __disk_stat_add(disk
, time_in_queue
,
2406 disk
->in_flight
* (now
- disk
->stamp
));
2407 __disk_stat_add(disk
, io_ticks
, (now
- disk
->stamp
));
2412 EXPORT_SYMBOL_GPL(disk_round_stats
);
2415 * queue lock must be held
2417 void __blk_put_request(struct request_queue
*q
, struct request
*req
)
2421 if (unlikely(--req
->ref_count
))
2424 elv_completed_request(q
, req
);
2427 * Request may not have originated from ll_rw_blk. if not,
2428 * it didn't come out of our reserved rq pools
2430 if (req
->cmd_flags
& REQ_ALLOCED
) {
2431 int rw
= rq_data_dir(req
);
2432 int priv
= req
->cmd_flags
& REQ_ELVPRIV
;
2434 BUG_ON(!list_empty(&req
->queuelist
));
2435 BUG_ON(!hlist_unhashed(&req
->hash
));
2437 blk_free_request(q
, req
);
2438 freed_request(q
, rw
, priv
);
2442 EXPORT_SYMBOL_GPL(__blk_put_request
);
2444 void blk_put_request(struct request
*req
)
2446 unsigned long flags
;
2447 struct request_queue
*q
= req
->q
;
2450 * Gee, IDE calls in w/ NULL q. Fix IDE and remove the
2451 * following if (q) test.
2454 spin_lock_irqsave(q
->queue_lock
, flags
);
2455 __blk_put_request(q
, req
);
2456 spin_unlock_irqrestore(q
->queue_lock
, flags
);
2460 EXPORT_SYMBOL(blk_put_request
);
2463 * blk_end_sync_rq - executes a completion event on a request
2464 * @rq: request to complete
2465 * @error: end io status of the request
2467 void blk_end_sync_rq(struct request
*rq
, int error
)
2469 struct completion
*waiting
= rq
->end_io_data
;
2471 rq
->end_io_data
= NULL
;
2472 __blk_put_request(rq
->q
, rq
);
2475 * complete last, if this is a stack request the process (and thus
2476 * the rq pointer) could be invalid right after this complete()
2480 EXPORT_SYMBOL(blk_end_sync_rq
);
2483 * Has to be called with the request spinlock acquired
2485 static int attempt_merge(struct request_queue
*q
, struct request
*req
,
2486 struct request
*next
)
2488 if (!rq_mergeable(req
) || !rq_mergeable(next
))
2494 if (req
->sector
+ req
->nr_sectors
!= next
->sector
)
2497 if (rq_data_dir(req
) != rq_data_dir(next
)
2498 || req
->rq_disk
!= next
->rq_disk
2503 * If we are allowed to merge, then append bio list
2504 * from next to rq and release next. merge_requests_fn
2505 * will have updated segment counts, update sector
2508 if (!ll_merge_requests_fn(q
, req
, next
))
2512 * At this point we have either done a back merge
2513 * or front merge. We need the smaller start_time of
2514 * the merged requests to be the current request
2515 * for accounting purposes.
2517 if (time_after(req
->start_time
, next
->start_time
))
2518 req
->start_time
= next
->start_time
;
2520 req
->biotail
->bi_next
= next
->bio
;
2521 req
->biotail
= next
->biotail
;
2523 req
->nr_sectors
= req
->hard_nr_sectors
+= next
->hard_nr_sectors
;
2525 elv_merge_requests(q
, req
, next
);
2528 disk_round_stats(req
->rq_disk
);
2529 req
->rq_disk
->in_flight
--;
2532 req
->ioprio
= ioprio_best(req
->ioprio
, next
->ioprio
);
2534 __blk_put_request(q
, next
);
2538 static inline int attempt_back_merge(struct request_queue
*q
,
2541 struct request
*next
= elv_latter_request(q
, rq
);
2544 return attempt_merge(q
, rq
, next
);
2549 static inline int attempt_front_merge(struct request_queue
*q
,
2552 struct request
*prev
= elv_former_request(q
, rq
);
2555 return attempt_merge(q
, prev
, rq
);
2560 static void init_request_from_bio(struct request
*req
, struct bio
*bio
)
2562 req
->cmd_type
= REQ_TYPE_FS
;
2565 * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
2567 if (bio_rw_ahead(bio
) || bio_failfast(bio
))
2568 req
->cmd_flags
|= REQ_FAILFAST
;
2571 * REQ_BARRIER implies no merging, but lets make it explicit
2573 if (unlikely(bio_barrier(bio
)))
2574 req
->cmd_flags
|= (REQ_HARDBARRIER
| REQ_NOMERGE
);
2577 req
->cmd_flags
|= REQ_RW_SYNC
;
2578 if (bio_rw_meta(bio
))
2579 req
->cmd_flags
|= REQ_RW_META
;
2582 req
->hard_sector
= req
->sector
= bio
->bi_sector
;
2583 req
->ioprio
= bio_prio(bio
);
2584 req
->start_time
= jiffies
;
2585 blk_rq_bio_prep(req
->q
, req
, bio
);
2588 static int __make_request(struct request_queue
*q
, struct bio
*bio
)
2590 struct request
*req
;
2591 int el_ret
, nr_sectors
, barrier
, err
;
2592 const unsigned short prio
= bio_prio(bio
);
2593 const int sync
= bio_sync(bio
);
2596 nr_sectors
= bio_sectors(bio
);
2599 * low level driver can indicate that it wants pages above a
2600 * certain limit bounced to low memory (ie for highmem, or even
2601 * ISA dma in theory)
2603 blk_queue_bounce(q
, &bio
);
2605 barrier
= bio_barrier(bio
);
2606 if (unlikely(barrier
) && (q
->next_ordered
== QUEUE_ORDERED_NONE
)) {
2611 spin_lock_irq(q
->queue_lock
);
2613 if (unlikely(barrier
) || elv_queue_empty(q
))
2616 el_ret
= elv_merge(q
, &req
, bio
);
2618 case ELEVATOR_BACK_MERGE
:
2619 BUG_ON(!rq_mergeable(req
));
2621 if (!ll_back_merge_fn(q
, req
, bio
))
2624 blk_add_trace_bio(q
, bio
, BLK_TA_BACKMERGE
);
2626 req
->biotail
->bi_next
= bio
;
2628 req
->nr_sectors
= req
->hard_nr_sectors
+= nr_sectors
;
2629 req
->ioprio
= ioprio_best(req
->ioprio
, prio
);
2630 drive_stat_acct(req
, 0);
2631 if (!attempt_back_merge(q
, req
))
2632 elv_merged_request(q
, req
, el_ret
);
2635 case ELEVATOR_FRONT_MERGE
:
2636 BUG_ON(!rq_mergeable(req
));
2638 if (!ll_front_merge_fn(q
, req
, bio
))
2641 blk_add_trace_bio(q
, bio
, BLK_TA_FRONTMERGE
);
2643 bio
->bi_next
= req
->bio
;
2647 * may not be valid. if the low level driver said
2648 * it didn't need a bounce buffer then it better
2649 * not touch req->buffer either...
2651 req
->buffer
= bio_data(bio
);
2652 req
->current_nr_sectors
= bio_cur_sectors(bio
);
2653 req
->hard_cur_sectors
= req
->current_nr_sectors
;
2654 req
->sector
= req
->hard_sector
= bio
->bi_sector
;
2655 req
->nr_sectors
= req
->hard_nr_sectors
+= nr_sectors
;
2656 req
->ioprio
= ioprio_best(req
->ioprio
, prio
);
2657 drive_stat_acct(req
, 0);
2658 if (!attempt_front_merge(q
, req
))
2659 elv_merged_request(q
, req
, el_ret
);
2662 /* ELV_NO_MERGE: elevator says don't/can't merge. */
2669 * This sync check and mask will be re-done in init_request_from_bio(),
2670 * but we need to set it earlier to expose the sync flag to the
2671 * rq allocator and io schedulers.
2673 rw_flags
= bio_data_dir(bio
);
2675 rw_flags
|= REQ_RW_SYNC
;
2678 * Grab a free request. This is might sleep but can not fail.
2679 * Returns with the queue unlocked.
2681 req
= get_request_wait(q
, rw_flags
, bio
);
2684 * After dropping the lock and possibly sleeping here, our request
2685 * may now be mergeable after it had proven unmergeable (above).
2686 * We don't worry about that case for efficiency. It won't happen
2687 * often, and the elevators are able to handle it.
2689 init_request_from_bio(req
, bio
);
2691 spin_lock_irq(q
->queue_lock
);
2692 if (elv_queue_empty(q
))
2694 add_request(q
, req
);
2697 __generic_unplug_device(q
);
2699 spin_unlock_irq(q
->queue_lock
);
2703 bio_endio(bio
, err
);
2708 * If bio->bi_dev is a partition, remap the location
2710 static inline void blk_partition_remap(struct bio
*bio
)
2712 struct block_device
*bdev
= bio
->bi_bdev
;
2714 if (bio_sectors(bio
) && bdev
!= bdev
->bd_contains
) {
2715 struct hd_struct
*p
= bdev
->bd_part
;
2716 const int rw
= bio_data_dir(bio
);
2718 p
->sectors
[rw
] += bio_sectors(bio
);
2721 bio
->bi_sector
+= p
->start_sect
;
2722 bio
->bi_bdev
= bdev
->bd_contains
;
2724 blk_add_trace_remap(bdev_get_queue(bio
->bi_bdev
), bio
,
2725 bdev
->bd_dev
, bio
->bi_sector
,
2726 bio
->bi_sector
- p
->start_sect
);
2730 static void handle_bad_sector(struct bio
*bio
)
2732 char b
[BDEVNAME_SIZE
];
2734 printk(KERN_INFO
"attempt to access beyond end of device\n");
2735 printk(KERN_INFO
"%s: rw=%ld, want=%Lu, limit=%Lu\n",
2736 bdevname(bio
->bi_bdev
, b
),
2738 (unsigned long long)bio
->bi_sector
+ bio_sectors(bio
),
2739 (long long)(bio
->bi_bdev
->bd_inode
->i_size
>> 9));
2741 set_bit(BIO_EOF
, &bio
->bi_flags
);
2744 #ifdef CONFIG_FAIL_MAKE_REQUEST
2746 static DECLARE_FAULT_ATTR(fail_make_request
);
2748 static int __init
setup_fail_make_request(char *str
)
2750 return setup_fault_attr(&fail_make_request
, str
);
2752 __setup("fail_make_request=", setup_fail_make_request
);
2754 static int should_fail_request(struct bio
*bio
)
2756 if ((bio
->bi_bdev
->bd_disk
->flags
& GENHD_FL_FAIL
) ||
2757 (bio
->bi_bdev
->bd_part
&& bio
->bi_bdev
->bd_part
->make_it_fail
))
2758 return should_fail(&fail_make_request
, bio
->bi_size
);
2763 static int __init
fail_make_request_debugfs(void)
2765 return init_fault_attr_dentries(&fail_make_request
,
2766 "fail_make_request");
2769 late_initcall(fail_make_request_debugfs
);
2771 #else /* CONFIG_FAIL_MAKE_REQUEST */
2773 static inline int should_fail_request(struct bio
*bio
)
2778 #endif /* CONFIG_FAIL_MAKE_REQUEST */
2781 * Check whether this bio extends beyond the end of the device.
2783 static inline int bio_check_eod(struct bio
*bio
, unsigned int nr_sectors
)
2790 /* Test device or partition size, when known. */
2791 maxsector
= bio
->bi_bdev
->bd_inode
->i_size
>> 9;
2793 sector_t sector
= bio
->bi_sector
;
2795 if (maxsector
< nr_sectors
|| maxsector
- nr_sectors
< sector
) {
2797 * This may well happen - the kernel calls bread()
2798 * without checking the size of the device, e.g., when
2799 * mounting a device.
2801 handle_bad_sector(bio
);
2810 * generic_make_request: hand a buffer to its device driver for I/O
2811 * @bio: The bio describing the location in memory and on the device.
2813 * generic_make_request() is used to make I/O requests of block
2814 * devices. It is passed a &struct bio, which describes the I/O that needs
2817 * generic_make_request() does not return any status. The
2818 * success/failure status of the request, along with notification of
2819 * completion, is delivered asynchronously through the bio->bi_end_io
2820 * function described (one day) else where.
2822 * The caller of generic_make_request must make sure that bi_io_vec
2823 * are set to describe the memory buffer, and that bi_dev and bi_sector are
2824 * set to describe the device address, and the
2825 * bi_end_io and optionally bi_private are set to describe how
2826 * completion notification should be signaled.
2828 * generic_make_request and the drivers it calls may use bi_next if this
2829 * bio happens to be merged with someone else, and may change bi_dev and
2830 * bi_sector for remaps as it sees fit. So the values of these fields
2831 * should NOT be depended on after the call to generic_make_request.
2833 static inline void __generic_make_request(struct bio
*bio
)
2835 struct request_queue
*q
;
2836 sector_t old_sector
;
2837 int ret
, nr_sectors
= bio_sectors(bio
);
2843 if (bio_check_eod(bio
, nr_sectors
))
2847 * Resolve the mapping until finished. (drivers are
2848 * still free to implement/resolve their own stacking
2849 * by explicitly returning 0)
2851 * NOTE: we don't repeat the blk_size check for each new device.
2852 * Stacking drivers are expected to know what they are doing.
2857 char b
[BDEVNAME_SIZE
];
2859 q
= bdev_get_queue(bio
->bi_bdev
);
2862 "generic_make_request: Trying to access "
2863 "nonexistent block-device %s (%Lu)\n",
2864 bdevname(bio
->bi_bdev
, b
),
2865 (long long) bio
->bi_sector
);
2867 bio_endio(bio
, err
);
2871 if (unlikely(nr_sectors
> q
->max_hw_sectors
)) {
2872 printk("bio too big device %s (%u > %u)\n",
2873 bdevname(bio
->bi_bdev
, b
),
2879 if (unlikely(test_bit(QUEUE_FLAG_DEAD
, &q
->queue_flags
)))
2882 if (should_fail_request(bio
))
2886 * If this device has partitions, remap block n
2887 * of partition p to block n+start(p) of the disk.
2889 blk_partition_remap(bio
);
2891 if (old_sector
!= -1)
2892 blk_add_trace_remap(q
, bio
, old_dev
, bio
->bi_sector
,
2895 blk_add_trace_bio(q
, bio
, BLK_TA_QUEUE
);
2897 old_sector
= bio
->bi_sector
;
2898 old_dev
= bio
->bi_bdev
->bd_dev
;
2900 if (bio_check_eod(bio
, nr_sectors
))
2902 if (bio_empty_barrier(bio
) && !q
->prepare_flush_fn
) {
2907 ret
= q
->make_request_fn(q
, bio
);
2912 * We only want one ->make_request_fn to be active at a time,
2913 * else stack usage with stacked devices could be a problem.
2914 * So use current->bio_{list,tail} to keep a list of requests
2915 * submited by a make_request_fn function.
2916 * current->bio_tail is also used as a flag to say if
2917 * generic_make_request is currently active in this task or not.
2918 * If it is NULL, then no make_request is active. If it is non-NULL,
2919 * then a make_request is active, and new requests should be added
2922 void generic_make_request(struct bio
*bio
)
2924 if (current
->bio_tail
) {
2925 /* make_request is active */
2926 *(current
->bio_tail
) = bio
;
2927 bio
->bi_next
= NULL
;
2928 current
->bio_tail
= &bio
->bi_next
;
2931 /* following loop may be a bit non-obvious, and so deserves some
2933 * Before entering the loop, bio->bi_next is NULL (as all callers
2934 * ensure that) so we have a list with a single bio.
2935 * We pretend that we have just taken it off a longer list, so
2936 * we assign bio_list to the next (which is NULL) and bio_tail
2937 * to &bio_list, thus initialising the bio_list of new bios to be
2938 * added. __generic_make_request may indeed add some more bios
2939 * through a recursive call to generic_make_request. If it
2940 * did, we find a non-NULL value in bio_list and re-enter the loop
2941 * from the top. In this case we really did just take the bio
2942 * of the top of the list (no pretending) and so fixup bio_list and
2943 * bio_tail or bi_next, and call into __generic_make_request again.
2945 * The loop was structured like this to make only one call to
2946 * __generic_make_request (which is important as it is large and
2947 * inlined) and to keep the structure simple.
2949 BUG_ON(bio
->bi_next
);
2951 current
->bio_list
= bio
->bi_next
;
2952 if (bio
->bi_next
== NULL
)
2953 current
->bio_tail
= ¤t
->bio_list
;
2955 bio
->bi_next
= NULL
;
2956 __generic_make_request(bio
);
2957 bio
= current
->bio_list
;
2959 current
->bio_tail
= NULL
; /* deactivate */
2962 EXPORT_SYMBOL(generic_make_request
);
2965 * submit_bio: submit a bio to the block device layer for I/O
2966 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
2967 * @bio: The &struct bio which describes the I/O
2969 * submit_bio() is very similar in purpose to generic_make_request(), and
2970 * uses that function to do most of the work. Both are fairly rough
2971 * interfaces, @bio must be presetup and ready for I/O.
2974 void submit_bio(int rw
, struct bio
*bio
)
2976 int count
= bio_sectors(bio
);
2981 * If it's a regular read/write or a barrier with data attached,
2982 * go through the normal accounting stuff before submission.
2984 if (!bio_empty_barrier(bio
)) {
2986 BIO_BUG_ON(!bio
->bi_size
);
2987 BIO_BUG_ON(!bio
->bi_io_vec
);
2990 count_vm_events(PGPGOUT
, count
);
2992 task_io_account_read(bio
->bi_size
);
2993 count_vm_events(PGPGIN
, count
);
2996 if (unlikely(block_dump
)) {
2997 char b
[BDEVNAME_SIZE
];
2998 printk(KERN_DEBUG
"%s(%d): %s block %Lu on %s\n",
2999 current
->comm
, task_pid_nr(current
),
3000 (rw
& WRITE
) ? "WRITE" : "READ",
3001 (unsigned long long)bio
->bi_sector
,
3002 bdevname(bio
->bi_bdev
,b
));
3006 generic_make_request(bio
);
3009 EXPORT_SYMBOL(submit_bio
);
3011 static void blk_recalc_rq_sectors(struct request
*rq
, int nsect
)
3013 if (blk_fs_request(rq
)) {
3014 rq
->hard_sector
+= nsect
;
3015 rq
->hard_nr_sectors
-= nsect
;
3018 * Move the I/O submission pointers ahead if required.
3020 if ((rq
->nr_sectors
>= rq
->hard_nr_sectors
) &&
3021 (rq
->sector
<= rq
->hard_sector
)) {
3022 rq
->sector
= rq
->hard_sector
;
3023 rq
->nr_sectors
= rq
->hard_nr_sectors
;
3024 rq
->hard_cur_sectors
= bio_cur_sectors(rq
->bio
);
3025 rq
->current_nr_sectors
= rq
->hard_cur_sectors
;
3026 rq
->buffer
= bio_data(rq
->bio
);
3030 * if total number of sectors is less than the first segment
3031 * size, something has gone terribly wrong
3033 if (rq
->nr_sectors
< rq
->current_nr_sectors
) {
3034 printk("blk: request botched\n");
3035 rq
->nr_sectors
= rq
->current_nr_sectors
;
3041 * __end_that_request_first - end I/O on a request
3042 * @req: the request being processed
3043 * @error: 0 for success, < 0 for error
3044 * @nr_bytes: number of bytes to complete
3047 * Ends I/O on a number of bytes attached to @req, and sets it up
3048 * for the next range of segments (if any) in the cluster.
3051 * 0 - we are done with this request, call end_that_request_last()
3052 * 1 - still buffers pending for this request
3054 static int __end_that_request_first(struct request
*req
, int error
,
3057 int total_bytes
, bio_nbytes
, next_idx
= 0;
3060 blk_add_trace_rq(req
->q
, req
, BLK_TA_COMPLETE
);
3063 * for a REQ_BLOCK_PC request, we want to carry any eventual
3064 * sense key with us all the way through
3066 if (!blk_pc_request(req
))
3070 if (blk_fs_request(req
) && !(req
->cmd_flags
& REQ_QUIET
))
3071 printk("end_request: I/O error, dev %s, sector %llu\n",
3072 req
->rq_disk
? req
->rq_disk
->disk_name
: "?",
3073 (unsigned long long)req
->sector
);
3076 if (blk_fs_request(req
) && req
->rq_disk
) {
3077 const int rw
= rq_data_dir(req
);
3079 disk_stat_add(req
->rq_disk
, sectors
[rw
], nr_bytes
>> 9);
3082 total_bytes
= bio_nbytes
= 0;
3083 while ((bio
= req
->bio
) != NULL
) {
3087 * For an empty barrier request, the low level driver must
3088 * store a potential error location in ->sector. We pass
3089 * that back up in ->bi_sector.
3091 if (blk_empty_barrier(req
))
3092 bio
->bi_sector
= req
->sector
;
3094 if (nr_bytes
>= bio
->bi_size
) {
3095 req
->bio
= bio
->bi_next
;
3096 nbytes
= bio
->bi_size
;
3097 req_bio_endio(req
, bio
, nbytes
, error
);
3101 int idx
= bio
->bi_idx
+ next_idx
;
3103 if (unlikely(bio
->bi_idx
>= bio
->bi_vcnt
)) {
3104 blk_dump_rq_flags(req
, "__end_that");
3105 printk("%s: bio idx %d >= vcnt %d\n",
3107 bio
->bi_idx
, bio
->bi_vcnt
);
3111 nbytes
= bio_iovec_idx(bio
, idx
)->bv_len
;
3112 BIO_BUG_ON(nbytes
> bio
->bi_size
);
3115 * not a complete bvec done
3117 if (unlikely(nbytes
> nr_bytes
)) {
3118 bio_nbytes
+= nr_bytes
;
3119 total_bytes
+= nr_bytes
;
3124 * advance to the next vector
3127 bio_nbytes
+= nbytes
;
3130 total_bytes
+= nbytes
;
3133 if ((bio
= req
->bio
)) {
3135 * end more in this run, or just return 'not-done'
3137 if (unlikely(nr_bytes
<= 0))
3149 * if the request wasn't completed, update state
3152 req_bio_endio(req
, bio
, bio_nbytes
, error
);
3153 bio
->bi_idx
+= next_idx
;
3154 bio_iovec(bio
)->bv_offset
+= nr_bytes
;
3155 bio_iovec(bio
)->bv_len
-= nr_bytes
;
3158 blk_recalc_rq_sectors(req
, total_bytes
>> 9);
3159 blk_recalc_rq_segments(req
);
3164 * splice the completion data to a local structure and hand off to
3165 * process_completion_queue() to complete the requests
3167 static void blk_done_softirq(struct softirq_action
*h
)
3169 struct list_head
*cpu_list
, local_list
;
3171 local_irq_disable();
3172 cpu_list
= &__get_cpu_var(blk_cpu_done
);
3173 list_replace_init(cpu_list
, &local_list
);
3176 while (!list_empty(&local_list
)) {
3177 struct request
*rq
= list_entry(local_list
.next
, struct request
, donelist
);
3179 list_del_init(&rq
->donelist
);
3180 rq
->q
->softirq_done_fn(rq
);
3184 static int __cpuinit
blk_cpu_notify(struct notifier_block
*self
, unsigned long action
,
3188 * If a CPU goes away, splice its entries to the current CPU
3189 * and trigger a run of the softirq
3191 if (action
== CPU_DEAD
|| action
== CPU_DEAD_FROZEN
) {
3192 int cpu
= (unsigned long) hcpu
;
3194 local_irq_disable();
3195 list_splice_init(&per_cpu(blk_cpu_done
, cpu
),
3196 &__get_cpu_var(blk_cpu_done
));
3197 raise_softirq_irqoff(BLOCK_SOFTIRQ
);
3205 static struct notifier_block blk_cpu_notifier __cpuinitdata
= {
3206 .notifier_call
= blk_cpu_notify
,
3210 * blk_complete_request - end I/O on a request
3211 * @req: the request being processed
3214 * Ends all I/O on a request. It does not handle partial completions,
3215 * unless the driver actually implements this in its completion callback
3216 * through requeueing. The actual completion happens out-of-order,
3217 * through a softirq handler. The user must have registered a completion
3218 * callback through blk_queue_softirq_done().
3221 void blk_complete_request(struct request
*req
)
3223 struct list_head
*cpu_list
;
3224 unsigned long flags
;
3226 BUG_ON(!req
->q
->softirq_done_fn
);
3228 local_irq_save(flags
);
3230 cpu_list
= &__get_cpu_var(blk_cpu_done
);
3231 list_add_tail(&req
->donelist
, cpu_list
);
3232 raise_softirq_irqoff(BLOCK_SOFTIRQ
);
3234 local_irq_restore(flags
);
3237 EXPORT_SYMBOL(blk_complete_request
);
3240 * queue lock must be held
3242 static void end_that_request_last(struct request
*req
, int error
)
3244 struct gendisk
*disk
= req
->rq_disk
;
3246 if (blk_rq_tagged(req
))
3247 blk_queue_end_tag(req
->q
, req
);
3249 if (blk_queued_rq(req
))
3250 blkdev_dequeue_request(req
);
3252 if (unlikely(laptop_mode
) && blk_fs_request(req
))
3253 laptop_io_completion();
3256 * Account IO completion. bar_rq isn't accounted as a normal
3257 * IO on queueing nor completion. Accounting the containing
3258 * request is enough.
3260 if (disk
&& blk_fs_request(req
) && req
!= &req
->q
->bar_rq
) {
3261 unsigned long duration
= jiffies
- req
->start_time
;
3262 const int rw
= rq_data_dir(req
);
3264 __disk_stat_inc(disk
, ios
[rw
]);
3265 __disk_stat_add(disk
, ticks
[rw
], duration
);
3266 disk_round_stats(disk
);
3271 req
->end_io(req
, error
);
3273 if (blk_bidi_rq(req
))
3274 __blk_put_request(req
->next_rq
->q
, req
->next_rq
);
3276 __blk_put_request(req
->q
, req
);
3280 static inline void __end_request(struct request
*rq
, int uptodate
,
3281 unsigned int nr_bytes
)
3286 error
= uptodate
? uptodate
: -EIO
;
3288 __blk_end_request(rq
, error
, nr_bytes
);
3292 * blk_rq_bytes - Returns bytes left to complete in the entire request
3294 unsigned int blk_rq_bytes(struct request
*rq
)
3296 if (blk_fs_request(rq
))
3297 return rq
->hard_nr_sectors
<< 9;
3299 return rq
->data_len
;
3301 EXPORT_SYMBOL_GPL(blk_rq_bytes
);
3304 * blk_rq_cur_bytes - Returns bytes left to complete in the current segment
3306 unsigned int blk_rq_cur_bytes(struct request
*rq
)
3308 if (blk_fs_request(rq
))
3309 return rq
->current_nr_sectors
<< 9;
3312 return rq
->bio
->bi_size
;
3314 return rq
->data_len
;
3316 EXPORT_SYMBOL_GPL(blk_rq_cur_bytes
);
3319 * end_queued_request - end all I/O on a queued request
3320 * @rq: the request being processed
3321 * @uptodate: error value or 0/1 uptodate flag
3324 * Ends all I/O on a request, and removes it from the block layer queues.
3325 * Not suitable for normal IO completion, unless the driver still has
3326 * the request attached to the block layer.
3329 void end_queued_request(struct request
*rq
, int uptodate
)
3331 __end_request(rq
, uptodate
, blk_rq_bytes(rq
));
3333 EXPORT_SYMBOL(end_queued_request
);
3336 * end_dequeued_request - end all I/O on a dequeued request
3337 * @rq: the request being processed
3338 * @uptodate: error value or 0/1 uptodate flag
3341 * Ends all I/O on a request. The request must already have been
3342 * dequeued using blkdev_dequeue_request(), as is normally the case
3346 void end_dequeued_request(struct request
*rq
, int uptodate
)
3348 __end_request(rq
, uptodate
, blk_rq_bytes(rq
));
3350 EXPORT_SYMBOL(end_dequeued_request
);
3354 * end_request - end I/O on the current segment of the request
3355 * @req: the request being processed
3356 * @uptodate: error value or 0/1 uptodate flag
3359 * Ends I/O on the current segment of a request. If that is the only
3360 * remaining segment, the request is also completed and freed.
3362 * This is a remnant of how older block drivers handled IO completions.
3363 * Modern drivers typically end IO on the full request in one go, unless
3364 * they have a residual value to account for. For that case this function
3365 * isn't really useful, unless the residual just happens to be the
3366 * full current segment. In other words, don't use this function in new
3367 * code. Either use end_request_completely(), or the
3368 * end_that_request_chunk() (along with end_that_request_last()) for
3369 * partial completions.
3372 void end_request(struct request
*req
, int uptodate
)
3374 __end_request(req
, uptodate
, req
->hard_cur_sectors
<< 9);
3376 EXPORT_SYMBOL(end_request
);
3379 * blk_end_io - Generic end_io function to complete a request.
3380 * @rq: the request being processed
3381 * @error: 0 for success, < 0 for error
3382 * @nr_bytes: number of bytes to complete @rq
3383 * @bidi_bytes: number of bytes to complete @rq->next_rq
3384 * @drv_callback: function called between completion of bios in the request
3385 * and completion of the request.
3386 * If the callback returns non 0, this helper returns without
3387 * completion of the request.
3390 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
3391 * If @rq has leftover, sets it up for the next range of segments.
3394 * 0 - we are done with this request
3395 * 1 - this request is not freed yet, it still has pending buffers.
3397 static int blk_end_io(struct request
*rq
, int error
, int nr_bytes
,
3398 int bidi_bytes
, int (drv_callback
)(struct request
*))
3400 struct request_queue
*q
= rq
->q
;
3401 unsigned long flags
= 0UL;
3403 if (blk_fs_request(rq
) || blk_pc_request(rq
)) {
3404 if (__end_that_request_first(rq
, error
, nr_bytes
))
3407 /* Bidi request must be completed as a whole */
3408 if (blk_bidi_rq(rq
) &&
3409 __end_that_request_first(rq
->next_rq
, error
, bidi_bytes
))
3413 /* Special feature for tricky drivers */
3414 if (drv_callback
&& drv_callback(rq
))
3417 add_disk_randomness(rq
->rq_disk
);
3419 spin_lock_irqsave(q
->queue_lock
, flags
);
3420 end_that_request_last(rq
, error
);
3421 spin_unlock_irqrestore(q
->queue_lock
, flags
);
3427 * blk_end_request - Helper function for drivers to complete the request.
3428 * @rq: the request being processed
3429 * @error: 0 for success, < 0 for error
3430 * @nr_bytes: number of bytes to complete
3433 * Ends I/O on a number of bytes attached to @rq.
3434 * If @rq has leftover, sets it up for the next range of segments.
3437 * 0 - we are done with this request
3438 * 1 - still buffers pending for this request
3440 int blk_end_request(struct request
*rq
, int error
, int nr_bytes
)
3442 return blk_end_io(rq
, error
, nr_bytes
, 0, NULL
);
3444 EXPORT_SYMBOL_GPL(blk_end_request
);
3447 * __blk_end_request - Helper function for drivers to complete the request.
3448 * @rq: the request being processed
3449 * @error: 0 for success, < 0 for error
3450 * @nr_bytes: number of bytes to complete
3453 * Must be called with queue lock held unlike blk_end_request().
3456 * 0 - we are done with this request
3457 * 1 - still buffers pending for this request
3459 int __blk_end_request(struct request
*rq
, int error
, int nr_bytes
)
3461 if (blk_fs_request(rq
) || blk_pc_request(rq
)) {
3462 if (__end_that_request_first(rq
, error
, nr_bytes
))
3466 add_disk_randomness(rq
->rq_disk
);
3468 end_that_request_last(rq
, error
);
3472 EXPORT_SYMBOL_GPL(__blk_end_request
);
3475 * blk_end_bidi_request - Helper function for drivers to complete bidi request.
3476 * @rq: the bidi request being processed
3477 * @error: 0 for success, < 0 for error
3478 * @nr_bytes: number of bytes to complete @rq
3479 * @bidi_bytes: number of bytes to complete @rq->next_rq
3482 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
3485 * 0 - we are done with this request
3486 * 1 - still buffers pending for this request
3488 int blk_end_bidi_request(struct request
*rq
, int error
, int nr_bytes
,
3491 return blk_end_io(rq
, error
, nr_bytes
, bidi_bytes
, NULL
);
3493 EXPORT_SYMBOL_GPL(blk_end_bidi_request
);
3496 * blk_end_request_callback - Special helper function for tricky drivers
3497 * @rq: the request being processed
3498 * @error: 0 for success, < 0 for error
3499 * @nr_bytes: number of bytes to complete
3500 * @drv_callback: function called between completion of bios in the request
3501 * and completion of the request.
3502 * If the callback returns non 0, this helper returns without
3503 * completion of the request.
3506 * Ends I/O on a number of bytes attached to @rq.
3507 * If @rq has leftover, sets it up for the next range of segments.
3509 * This special helper function is used only for existing tricky drivers.
3510 * (e.g. cdrom_newpc_intr() of ide-cd)
3511 * This interface will be removed when such drivers are rewritten.
3512 * Don't use this interface in other places anymore.
3515 * 0 - we are done with this request
3516 * 1 - this request is not freed yet.
3517 * this request still has pending buffers or
3518 * the driver doesn't want to finish this request yet.
3520 int blk_end_request_callback(struct request
*rq
, int error
, int nr_bytes
,
3521 int (drv_callback
)(struct request
*))
3523 return blk_end_io(rq
, error
, nr_bytes
, 0, drv_callback
);
3525 EXPORT_SYMBOL_GPL(blk_end_request_callback
);
3527 static void blk_rq_bio_prep(struct request_queue
*q
, struct request
*rq
,
3530 /* first two bits are identical in rq->cmd_flags and bio->bi_rw */
3531 rq
->cmd_flags
|= (bio
->bi_rw
& 3);
3533 rq
->nr_phys_segments
= bio_phys_segments(q
, bio
);
3534 rq
->nr_hw_segments
= bio_hw_segments(q
, bio
);
3535 rq
->current_nr_sectors
= bio_cur_sectors(bio
);
3536 rq
->hard_cur_sectors
= rq
->current_nr_sectors
;
3537 rq
->hard_nr_sectors
= rq
->nr_sectors
= bio_sectors(bio
);
3538 rq
->buffer
= bio_data(bio
);
3539 rq
->data_len
= bio
->bi_size
;
3541 rq
->bio
= rq
->biotail
= bio
;
3544 rq
->rq_disk
= bio
->bi_bdev
->bd_disk
;
3547 int kblockd_schedule_work(struct work_struct
*work
)
3549 return queue_work(kblockd_workqueue
, work
);
3552 EXPORT_SYMBOL(kblockd_schedule_work
);
3554 void kblockd_flush_work(struct work_struct
*work
)
3556 cancel_work_sync(work
);
3558 EXPORT_SYMBOL(kblockd_flush_work
);
3560 int __init
blk_dev_init(void)
3564 kblockd_workqueue
= create_workqueue("kblockd");
3565 if (!kblockd_workqueue
)
3566 panic("Failed to create kblockd\n");
3568 request_cachep
= kmem_cache_create("blkdev_requests",
3569 sizeof(struct request
), 0, SLAB_PANIC
, NULL
);
3571 blk_requestq_cachep
= kmem_cache_create("blkdev_queue",
3572 sizeof(struct request_queue
), 0, SLAB_PANIC
, NULL
);
3574 iocontext_cachep
= kmem_cache_create("blkdev_ioc",
3575 sizeof(struct io_context
), 0, SLAB_PANIC
, NULL
);
3577 for_each_possible_cpu(i
)
3578 INIT_LIST_HEAD(&per_cpu(blk_cpu_done
, i
));
3580 open_softirq(BLOCK_SOFTIRQ
, blk_done_softirq
, NULL
);
3581 register_hotcpu_notifier(&blk_cpu_notifier
);
3583 blk_max_low_pfn
= max_low_pfn
- 1;
3584 blk_max_pfn
= max_pfn
- 1;
3589 static void cfq_dtor(struct io_context
*ioc
)
3591 struct cfq_io_context
*cic
[1];
3595 * We don't have a specific key to lookup with, so use the gang
3596 * lookup to just retrieve the first item stored. The cfq exit
3597 * function will iterate the full tree, so any member will do.
3599 r
= radix_tree_gang_lookup(&ioc
->radix_root
, (void **) cic
, 0, 1);
3605 * IO Context helper functions. put_io_context() returns 1 if there are no
3606 * more users of this io context, 0 otherwise.
3608 int put_io_context(struct io_context
*ioc
)
3613 BUG_ON(atomic_read(&ioc
->refcount
) == 0);
3615 if (atomic_dec_and_test(&ioc
->refcount
)) {
3617 if (ioc
->aic
&& ioc
->aic
->dtor
)
3618 ioc
->aic
->dtor(ioc
->aic
);
3622 kmem_cache_free(iocontext_cachep
, ioc
);
3627 EXPORT_SYMBOL(put_io_context
);
3629 static void cfq_exit(struct io_context
*ioc
)
3631 struct cfq_io_context
*cic
[1];
3636 * See comment for cfq_dtor()
3638 r
= radix_tree_gang_lookup(&ioc
->radix_root
, (void **) cic
, 0, 1);
3645 /* Called by the exitting task */
3646 void exit_io_context(void)
3648 struct io_context
*ioc
;
3651 ioc
= current
->io_context
;
3652 current
->io_context
= NULL
;
3653 task_unlock(current
);
3655 if (atomic_dec_and_test(&ioc
->nr_tasks
)) {
3656 if (ioc
->aic
&& ioc
->aic
->exit
)
3657 ioc
->aic
->exit(ioc
->aic
);
3660 put_io_context(ioc
);
3664 struct io_context
*alloc_io_context(gfp_t gfp_flags
, int node
)
3666 struct io_context
*ret
;
3668 ret
= kmem_cache_alloc_node(iocontext_cachep
, gfp_flags
, node
);
3670 atomic_set(&ret
->refcount
, 1);
3671 atomic_set(&ret
->nr_tasks
, 1);
3672 spin_lock_init(&ret
->lock
);
3673 ret
->ioprio_changed
= 0;
3675 ret
->last_waited
= jiffies
; /* doesn't matter... */
3676 ret
->nr_batch_requests
= 0; /* because this is 0 */
3678 INIT_RADIX_TREE(&ret
->radix_root
, GFP_ATOMIC
| __GFP_HIGH
);
3679 ret
->ioc_data
= NULL
;
3686 * If the current task has no IO context then create one and initialise it.
3687 * Otherwise, return its existing IO context.
3689 * This returned IO context doesn't have a specifically elevated refcount,
3690 * but since the current task itself holds a reference, the context can be
3691 * used in general code, so long as it stays within `current` context.
3693 static struct io_context
*current_io_context(gfp_t gfp_flags
, int node
)
3695 struct task_struct
*tsk
= current
;
3696 struct io_context
*ret
;
3698 ret
= tsk
->io_context
;
3702 ret
= alloc_io_context(gfp_flags
, node
);
3704 /* make sure set_task_ioprio() sees the settings above */
3706 tsk
->io_context
= ret
;
3713 * If the current task has no IO context then create one and initialise it.
3714 * If it does have a context, take a ref on it.
3716 * This is always called in the context of the task which submitted the I/O.
3718 struct io_context
*get_io_context(gfp_t gfp_flags
, int node
)
3720 struct io_context
*ret
= NULL
;
3723 * Check for unlikely race with exiting task. ioc ref count is
3724 * zero when ioc is being detached.
3727 ret
= current_io_context(gfp_flags
, node
);
3730 } while (!atomic_inc_not_zero(&ret
->refcount
));
3734 EXPORT_SYMBOL(get_io_context
);
3736 void copy_io_context(struct io_context
**pdst
, struct io_context
**psrc
)
3738 struct io_context
*src
= *psrc
;
3739 struct io_context
*dst
= *pdst
;
3742 BUG_ON(atomic_read(&src
->refcount
) == 0);
3743 atomic_inc(&src
->refcount
);
3744 put_io_context(dst
);
3748 EXPORT_SYMBOL(copy_io_context
);
3750 void swap_io_context(struct io_context
**ioc1
, struct io_context
**ioc2
)
3752 struct io_context
*temp
;
3757 EXPORT_SYMBOL(swap_io_context
);