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/config.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/backing-dev.h>
17 #include <linux/bio.h>
18 #include <linux/blkdev.h>
19 #include <linux/highmem.h>
21 #include <linux/kernel_stat.h>
22 #include <linux/string.h>
23 #include <linux/init.h>
24 #include <linux/bootmem.h> /* for max_pfn/max_low_pfn */
25 #include <linux/completion.h>
26 #include <linux/slab.h>
27 #include <linux/swap.h>
28 #include <linux/writeback.h>
29 #include <linux/blkdev.h>
34 #include <scsi/scsi_cmnd.h>
36 static void blk_unplug_work(void *data
);
37 static void blk_unplug_timeout(unsigned long data
);
38 static void drive_stat_acct(struct request
*rq
, int nr_sectors
, int new_io
);
39 static void init_request_from_bio(struct request
*req
, struct bio
*bio
);
40 static int __make_request(request_queue_t
*q
, struct bio
*bio
);
43 * For the allocated request tables
45 static kmem_cache_t
*request_cachep
;
48 * For queue allocation
50 static kmem_cache_t
*requestq_cachep
;
53 * For io context allocations
55 static kmem_cache_t
*iocontext_cachep
;
57 static wait_queue_head_t congestion_wqh
[2] = {
58 __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh
[0]),
59 __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh
[1])
63 * Controlling structure to kblockd
65 static struct workqueue_struct
*kblockd_workqueue
;
67 unsigned long blk_max_low_pfn
, blk_max_pfn
;
69 EXPORT_SYMBOL(blk_max_low_pfn
);
70 EXPORT_SYMBOL(blk_max_pfn
);
72 /* Amount of time in which a process may batch requests */
73 #define BLK_BATCH_TIME (HZ/50UL)
75 /* Number of requests a "batching" process may submit */
76 #define BLK_BATCH_REQ 32
79 * Return the threshold (number of used requests) at which the queue is
80 * considered to be congested. It include a little hysteresis to keep the
81 * context switch rate down.
83 static inline int queue_congestion_on_threshold(struct request_queue
*q
)
85 return q
->nr_congestion_on
;
89 * The threshold at which a queue is considered to be uncongested
91 static inline int queue_congestion_off_threshold(struct request_queue
*q
)
93 return q
->nr_congestion_off
;
96 static void blk_queue_congestion_threshold(struct request_queue
*q
)
100 nr
= q
->nr_requests
- (q
->nr_requests
/ 8) + 1;
101 if (nr
> q
->nr_requests
)
103 q
->nr_congestion_on
= nr
;
105 nr
= q
->nr_requests
- (q
->nr_requests
/ 8) - (q
->nr_requests
/ 16) - 1;
108 q
->nr_congestion_off
= nr
;
112 * A queue has just exitted congestion. Note this in the global counter of
113 * congested queues, and wake up anyone who was waiting for requests to be
116 static void clear_queue_congested(request_queue_t
*q
, int rw
)
119 wait_queue_head_t
*wqh
= &congestion_wqh
[rw
];
121 bit
= (rw
== WRITE
) ? BDI_write_congested
: BDI_read_congested
;
122 clear_bit(bit
, &q
->backing_dev_info
.state
);
123 smp_mb__after_clear_bit();
124 if (waitqueue_active(wqh
))
129 * A queue has just entered congestion. Flag that in the queue's VM-visible
130 * state flags and increment the global gounter of congested queues.
132 static void set_queue_congested(request_queue_t
*q
, int rw
)
136 bit
= (rw
== WRITE
) ? BDI_write_congested
: BDI_read_congested
;
137 set_bit(bit
, &q
->backing_dev_info
.state
);
141 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
144 * Locates the passed device's request queue and returns the address of its
147 * Will return NULL if the request queue cannot be located.
149 struct backing_dev_info
*blk_get_backing_dev_info(struct block_device
*bdev
)
151 struct backing_dev_info
*ret
= NULL
;
152 request_queue_t
*q
= bdev_get_queue(bdev
);
155 ret
= &q
->backing_dev_info
;
159 EXPORT_SYMBOL(blk_get_backing_dev_info
);
161 void blk_queue_activity_fn(request_queue_t
*q
, activity_fn
*fn
, void *data
)
164 q
->activity_data
= data
;
167 EXPORT_SYMBOL(blk_queue_activity_fn
);
170 * blk_queue_prep_rq - set a prepare_request function for queue
172 * @pfn: prepare_request function
174 * It's possible for a queue to register a prepare_request callback which
175 * is invoked before the request is handed to the request_fn. The goal of
176 * the function is to prepare a request for I/O, it can be used to build a
177 * cdb from the request data for instance.
180 void blk_queue_prep_rq(request_queue_t
*q
, prep_rq_fn
*pfn
)
185 EXPORT_SYMBOL(blk_queue_prep_rq
);
188 * blk_queue_merge_bvec - set a merge_bvec function for queue
190 * @mbfn: merge_bvec_fn
192 * Usually queues have static limitations on the max sectors or segments that
193 * we can put in a request. Stacking drivers may have some settings that
194 * are dynamic, and thus we have to query the queue whether it is ok to
195 * add a new bio_vec to a bio at a given offset or not. If the block device
196 * has such limitations, it needs to register a merge_bvec_fn to control
197 * the size of bio's sent to it. Note that a block device *must* allow a
198 * single page to be added to an empty bio. The block device driver may want
199 * to use the bio_split() function to deal with these bio's. By default
200 * no merge_bvec_fn is defined for a queue, and only the fixed limits are
203 void blk_queue_merge_bvec(request_queue_t
*q
, merge_bvec_fn
*mbfn
)
205 q
->merge_bvec_fn
= mbfn
;
208 EXPORT_SYMBOL(blk_queue_merge_bvec
);
211 * blk_queue_make_request - define an alternate make_request function for a device
212 * @q: the request queue for the device to be affected
213 * @mfn: the alternate make_request function
216 * The normal way for &struct bios to be passed to a device
217 * driver is for them to be collected into requests on a request
218 * queue, and then to allow the device driver to select requests
219 * off that queue when it is ready. This works well for many block
220 * devices. However some block devices (typically virtual devices
221 * such as md or lvm) do not benefit from the processing on the
222 * request queue, and are served best by having the requests passed
223 * directly to them. This can be achieved by providing a function
224 * to blk_queue_make_request().
227 * The driver that does this *must* be able to deal appropriately
228 * with buffers in "highmemory". This can be accomplished by either calling
229 * __bio_kmap_atomic() to get a temporary kernel mapping, or by calling
230 * blk_queue_bounce() to create a buffer in normal memory.
232 void blk_queue_make_request(request_queue_t
* q
, make_request_fn
* mfn
)
237 q
->nr_requests
= BLKDEV_MAX_RQ
;
238 blk_queue_max_phys_segments(q
, MAX_PHYS_SEGMENTS
);
239 blk_queue_max_hw_segments(q
, MAX_HW_SEGMENTS
);
240 q
->make_request_fn
= mfn
;
241 q
->backing_dev_info
.ra_pages
= (VM_MAX_READAHEAD
* 1024) / PAGE_CACHE_SIZE
;
242 q
->backing_dev_info
.state
= 0;
243 q
->backing_dev_info
.capabilities
= BDI_CAP_MAP_COPY
;
244 blk_queue_max_sectors(q
, SAFE_MAX_SECTORS
);
245 blk_queue_hardsect_size(q
, 512);
246 blk_queue_dma_alignment(q
, 511);
247 blk_queue_congestion_threshold(q
);
248 q
->nr_batching
= BLK_BATCH_REQ
;
250 q
->unplug_thresh
= 4; /* hmm */
251 q
->unplug_delay
= (3 * HZ
) / 1000; /* 3 milliseconds */
252 if (q
->unplug_delay
== 0)
255 INIT_WORK(&q
->unplug_work
, blk_unplug_work
, q
);
257 q
->unplug_timer
.function
= blk_unplug_timeout
;
258 q
->unplug_timer
.data
= (unsigned long)q
;
261 * by default assume old behaviour and bounce for any highmem page
263 blk_queue_bounce_limit(q
, BLK_BOUNCE_HIGH
);
265 blk_queue_activity_fn(q
, NULL
, NULL
);
268 EXPORT_SYMBOL(blk_queue_make_request
);
270 static inline void rq_init(request_queue_t
*q
, struct request
*rq
)
272 INIT_LIST_HEAD(&rq
->queuelist
);
275 rq
->rq_status
= RQ_ACTIVE
;
276 rq
->bio
= rq
->biotail
= NULL
;
285 rq
->nr_phys_segments
= 0;
288 rq
->end_io_data
= NULL
;
292 * blk_queue_ordered - does this queue support ordered writes
293 * @q: the request queue
294 * @ordered: one of QUEUE_ORDERED_*
297 * For journalled file systems, doing ordered writes on a commit
298 * block instead of explicitly doing wait_on_buffer (which is bad
299 * for performance) can be a big win. Block drivers supporting this
300 * feature should call this function and indicate so.
303 int blk_queue_ordered(request_queue_t
*q
, unsigned ordered
,
304 prepare_flush_fn
*prepare_flush_fn
)
306 if (ordered
& (QUEUE_ORDERED_PREFLUSH
| QUEUE_ORDERED_POSTFLUSH
) &&
307 prepare_flush_fn
== NULL
) {
308 printk(KERN_ERR
"blk_queue_ordered: prepare_flush_fn required\n");
312 if (ordered
!= QUEUE_ORDERED_NONE
&&
313 ordered
!= QUEUE_ORDERED_DRAIN
&&
314 ordered
!= QUEUE_ORDERED_DRAIN_FLUSH
&&
315 ordered
!= QUEUE_ORDERED_DRAIN_FUA
&&
316 ordered
!= QUEUE_ORDERED_TAG
&&
317 ordered
!= QUEUE_ORDERED_TAG_FLUSH
&&
318 ordered
!= QUEUE_ORDERED_TAG_FUA
) {
319 printk(KERN_ERR
"blk_queue_ordered: bad value %d\n", ordered
);
323 q
->next_ordered
= ordered
;
324 q
->prepare_flush_fn
= prepare_flush_fn
;
329 EXPORT_SYMBOL(blk_queue_ordered
);
332 * blk_queue_issue_flush_fn - set function for issuing a flush
333 * @q: the request queue
334 * @iff: the function to be called issuing the flush
337 * If a driver supports issuing a flush command, the support is notified
338 * to the block layer by defining it through this call.
341 void blk_queue_issue_flush_fn(request_queue_t
*q
, issue_flush_fn
*iff
)
343 q
->issue_flush_fn
= iff
;
346 EXPORT_SYMBOL(blk_queue_issue_flush_fn
);
349 * Cache flushing for ordered writes handling
351 inline unsigned blk_ordered_cur_seq(request_queue_t
*q
)
355 return 1 << ffz(q
->ordseq
);
358 unsigned blk_ordered_req_seq(struct request
*rq
)
360 request_queue_t
*q
= rq
->q
;
362 BUG_ON(q
->ordseq
== 0);
364 if (rq
== &q
->pre_flush_rq
)
365 return QUEUE_ORDSEQ_PREFLUSH
;
366 if (rq
== &q
->bar_rq
)
367 return QUEUE_ORDSEQ_BAR
;
368 if (rq
== &q
->post_flush_rq
)
369 return QUEUE_ORDSEQ_POSTFLUSH
;
371 if ((rq
->flags
& REQ_ORDERED_COLOR
) ==
372 (q
->orig_bar_rq
->flags
& REQ_ORDERED_COLOR
))
373 return QUEUE_ORDSEQ_DRAIN
;
375 return QUEUE_ORDSEQ_DONE
;
378 void blk_ordered_complete_seq(request_queue_t
*q
, unsigned seq
, int error
)
383 if (error
&& !q
->orderr
)
386 BUG_ON(q
->ordseq
& seq
);
389 if (blk_ordered_cur_seq(q
) != QUEUE_ORDSEQ_DONE
)
393 * Okay, sequence complete.
396 uptodate
= q
->orderr
? q
->orderr
: 1;
400 end_that_request_first(rq
, uptodate
, rq
->hard_nr_sectors
);
401 end_that_request_last(rq
, uptodate
);
404 static void pre_flush_end_io(struct request
*rq
, int error
)
406 elv_completed_request(rq
->q
, rq
);
407 blk_ordered_complete_seq(rq
->q
, QUEUE_ORDSEQ_PREFLUSH
, error
);
410 static void bar_end_io(struct request
*rq
, int error
)
412 elv_completed_request(rq
->q
, rq
);
413 blk_ordered_complete_seq(rq
->q
, QUEUE_ORDSEQ_BAR
, error
);
416 static void post_flush_end_io(struct request
*rq
, int error
)
418 elv_completed_request(rq
->q
, rq
);
419 blk_ordered_complete_seq(rq
->q
, QUEUE_ORDSEQ_POSTFLUSH
, error
);
422 static void queue_flush(request_queue_t
*q
, unsigned which
)
425 rq_end_io_fn
*end_io
;
427 if (which
== QUEUE_ORDERED_PREFLUSH
) {
428 rq
= &q
->pre_flush_rq
;
429 end_io
= pre_flush_end_io
;
431 rq
= &q
->post_flush_rq
;
432 end_io
= post_flush_end_io
;
436 rq
->flags
= REQ_HARDBARRIER
;
437 rq
->elevator_private
= NULL
;
438 rq
->rq_disk
= q
->bar_rq
.rq_disk
;
441 q
->prepare_flush_fn(q
, rq
);
443 __elv_add_request(q
, rq
, ELEVATOR_INSERT_FRONT
, 0);
446 static inline struct request
*start_ordered(request_queue_t
*q
,
451 q
->ordered
= q
->next_ordered
;
452 q
->ordseq
|= QUEUE_ORDSEQ_STARTED
;
455 * Prep proxy barrier request.
457 blkdev_dequeue_request(rq
);
461 rq
->flags
= bio_data_dir(q
->orig_bar_rq
->bio
);
462 rq
->flags
|= q
->ordered
& QUEUE_ORDERED_FUA
? REQ_FUA
: 0;
463 rq
->elevator_private
= NULL
;
465 init_request_from_bio(rq
, q
->orig_bar_rq
->bio
);
466 rq
->end_io
= bar_end_io
;
469 * Queue ordered sequence. As we stack them at the head, we
470 * need to queue in reverse order. Note that we rely on that
471 * no fs request uses ELEVATOR_INSERT_FRONT and thus no fs
472 * request gets inbetween ordered sequence.
474 if (q
->ordered
& QUEUE_ORDERED_POSTFLUSH
)
475 queue_flush(q
, QUEUE_ORDERED_POSTFLUSH
);
477 q
->ordseq
|= QUEUE_ORDSEQ_POSTFLUSH
;
479 __elv_add_request(q
, rq
, ELEVATOR_INSERT_FRONT
, 0);
481 if (q
->ordered
& QUEUE_ORDERED_PREFLUSH
) {
482 queue_flush(q
, QUEUE_ORDERED_PREFLUSH
);
483 rq
= &q
->pre_flush_rq
;
485 q
->ordseq
|= QUEUE_ORDSEQ_PREFLUSH
;
487 if ((q
->ordered
& QUEUE_ORDERED_TAG
) || q
->in_flight
== 0)
488 q
->ordseq
|= QUEUE_ORDSEQ_DRAIN
;
495 int blk_do_ordered(request_queue_t
*q
, struct request
**rqp
)
497 struct request
*rq
= *rqp
, *allowed_rq
;
498 int is_barrier
= blk_fs_request(rq
) && blk_barrier_rq(rq
);
504 if (q
->next_ordered
!= QUEUE_ORDERED_NONE
) {
505 *rqp
= start_ordered(q
, rq
);
509 * This can happen when the queue switches to
510 * ORDERED_NONE while this request is on it.
512 blkdev_dequeue_request(rq
);
513 end_that_request_first(rq
, -EOPNOTSUPP
,
514 rq
->hard_nr_sectors
);
515 end_that_request_last(rq
, -EOPNOTSUPP
);
521 if (q
->ordered
& QUEUE_ORDERED_TAG
) {
522 if (is_barrier
&& rq
!= &q
->bar_rq
)
527 switch (blk_ordered_cur_seq(q
)) {
528 case QUEUE_ORDSEQ_PREFLUSH
:
529 allowed_rq
= &q
->pre_flush_rq
;
531 case QUEUE_ORDSEQ_BAR
:
532 allowed_rq
= &q
->bar_rq
;
534 case QUEUE_ORDSEQ_POSTFLUSH
:
535 allowed_rq
= &q
->post_flush_rq
;
542 if (rq
!= allowed_rq
&&
543 (blk_fs_request(rq
) || rq
== &q
->pre_flush_rq
||
544 rq
== &q
->post_flush_rq
))
550 static int flush_dry_bio_endio(struct bio
*bio
, unsigned int bytes
, int error
)
552 request_queue_t
*q
= bio
->bi_private
;
553 struct bio_vec
*bvec
;
557 * This is dry run, restore bio_sector and size. We'll finish
558 * this request again with the original bi_end_io after an
559 * error occurs or post flush is complete.
568 bio_for_each_segment(bvec
, bio
, i
) {
569 bvec
->bv_len
+= bvec
->bv_offset
;
574 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
575 bio
->bi_size
= q
->bi_size
;
576 bio
->bi_sector
-= (q
->bi_size
>> 9);
582 static inline int ordered_bio_endio(struct request
*rq
, struct bio
*bio
,
583 unsigned int nbytes
, int error
)
585 request_queue_t
*q
= rq
->q
;
589 if (&q
->bar_rq
!= rq
)
593 * Okay, this is the barrier request in progress, dry finish it.
595 if (error
&& !q
->orderr
)
598 endio
= bio
->bi_end_io
;
599 private = bio
->bi_private
;
600 bio
->bi_end_io
= flush_dry_bio_endio
;
603 bio_endio(bio
, nbytes
, error
);
605 bio
->bi_end_io
= endio
;
606 bio
->bi_private
= private;
612 * blk_queue_bounce_limit - set bounce buffer limit for queue
613 * @q: the request queue for the device
614 * @dma_addr: bus address limit
617 * Different hardware can have different requirements as to what pages
618 * it can do I/O directly to. A low level driver can call
619 * blk_queue_bounce_limit to have lower memory pages allocated as bounce
620 * buffers for doing I/O to pages residing above @page. By default
621 * the block layer sets this to the highest numbered "low" memory page.
623 void blk_queue_bounce_limit(request_queue_t
*q
, u64 dma_addr
)
625 unsigned long bounce_pfn
= dma_addr
>> PAGE_SHIFT
;
628 * set appropriate bounce gfp mask -- unfortunately we don't have a
629 * full 4GB zone, so we have to resort to low memory for any bounces.
630 * ISA has its own < 16MB zone.
632 if (bounce_pfn
< blk_max_low_pfn
) {
633 BUG_ON(dma_addr
< BLK_BOUNCE_ISA
);
634 init_emergency_isa_pool();
635 q
->bounce_gfp
= GFP_NOIO
| GFP_DMA
;
637 q
->bounce_gfp
= GFP_NOIO
;
639 q
->bounce_pfn
= bounce_pfn
;
642 EXPORT_SYMBOL(blk_queue_bounce_limit
);
645 * blk_queue_max_sectors - set max sectors for a request for this queue
646 * @q: the request queue for the device
647 * @max_sectors: max sectors in the usual 512b unit
650 * Enables a low level driver to set an upper limit on the size of
653 void blk_queue_max_sectors(request_queue_t
*q
, unsigned short max_sectors
)
655 if ((max_sectors
<< 9) < PAGE_CACHE_SIZE
) {
656 max_sectors
= 1 << (PAGE_CACHE_SHIFT
- 9);
657 printk("%s: set to minimum %d\n", __FUNCTION__
, max_sectors
);
660 if (BLK_DEF_MAX_SECTORS
> max_sectors
)
661 q
->max_hw_sectors
= q
->max_sectors
= max_sectors
;
663 q
->max_sectors
= BLK_DEF_MAX_SECTORS
;
664 q
->max_hw_sectors
= max_sectors
;
668 EXPORT_SYMBOL(blk_queue_max_sectors
);
671 * blk_queue_max_phys_segments - set max phys segments for a request for this queue
672 * @q: the request queue for the device
673 * @max_segments: max number of segments
676 * Enables a low level driver to set an upper limit on the number of
677 * physical data segments in a request. This would be the largest sized
678 * scatter list the driver could handle.
680 void blk_queue_max_phys_segments(request_queue_t
*q
, unsigned short max_segments
)
684 printk("%s: set to minimum %d\n", __FUNCTION__
, max_segments
);
687 q
->max_phys_segments
= max_segments
;
690 EXPORT_SYMBOL(blk_queue_max_phys_segments
);
693 * blk_queue_max_hw_segments - set max hw segments for a request for this queue
694 * @q: the request queue for the device
695 * @max_segments: max number of segments
698 * Enables a low level driver to set an upper limit on the number of
699 * hw data segments in a request. This would be the largest number of
700 * address/length pairs the host adapter can actually give as once
703 void blk_queue_max_hw_segments(request_queue_t
*q
, unsigned short max_segments
)
707 printk("%s: set to minimum %d\n", __FUNCTION__
, max_segments
);
710 q
->max_hw_segments
= max_segments
;
713 EXPORT_SYMBOL(blk_queue_max_hw_segments
);
716 * blk_queue_max_segment_size - set max segment size for blk_rq_map_sg
717 * @q: the request queue for the device
718 * @max_size: max size of segment in bytes
721 * Enables a low level driver to set an upper limit on the size of a
724 void blk_queue_max_segment_size(request_queue_t
*q
, unsigned int max_size
)
726 if (max_size
< PAGE_CACHE_SIZE
) {
727 max_size
= PAGE_CACHE_SIZE
;
728 printk("%s: set to minimum %d\n", __FUNCTION__
, max_size
);
731 q
->max_segment_size
= max_size
;
734 EXPORT_SYMBOL(blk_queue_max_segment_size
);
737 * blk_queue_hardsect_size - set hardware sector size for the queue
738 * @q: the request queue for the device
739 * @size: the hardware sector size, in bytes
742 * This should typically be set to the lowest possible sector size
743 * that the hardware can operate on (possible without reverting to
744 * even internal read-modify-write operations). Usually the default
745 * of 512 covers most hardware.
747 void blk_queue_hardsect_size(request_queue_t
*q
, unsigned short size
)
749 q
->hardsect_size
= size
;
752 EXPORT_SYMBOL(blk_queue_hardsect_size
);
755 * Returns the minimum that is _not_ zero, unless both are zero.
757 #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
760 * blk_queue_stack_limits - inherit underlying queue limits for stacked drivers
761 * @t: the stacking driver (top)
762 * @b: the underlying device (bottom)
764 void blk_queue_stack_limits(request_queue_t
*t
, request_queue_t
*b
)
766 /* zero is "infinity" */
767 t
->max_sectors
= min_not_zero(t
->max_sectors
,b
->max_sectors
);
768 t
->max_hw_sectors
= min_not_zero(t
->max_hw_sectors
,b
->max_hw_sectors
);
770 t
->max_phys_segments
= min(t
->max_phys_segments
,b
->max_phys_segments
);
771 t
->max_hw_segments
= min(t
->max_hw_segments
,b
->max_hw_segments
);
772 t
->max_segment_size
= min(t
->max_segment_size
,b
->max_segment_size
);
773 t
->hardsect_size
= max(t
->hardsect_size
,b
->hardsect_size
);
776 EXPORT_SYMBOL(blk_queue_stack_limits
);
779 * blk_queue_segment_boundary - set boundary rules for segment merging
780 * @q: the request queue for the device
781 * @mask: the memory boundary mask
783 void blk_queue_segment_boundary(request_queue_t
*q
, unsigned long mask
)
785 if (mask
< PAGE_CACHE_SIZE
- 1) {
786 mask
= PAGE_CACHE_SIZE
- 1;
787 printk("%s: set to minimum %lx\n", __FUNCTION__
, mask
);
790 q
->seg_boundary_mask
= mask
;
793 EXPORT_SYMBOL(blk_queue_segment_boundary
);
796 * blk_queue_dma_alignment - set dma length and memory alignment
797 * @q: the request queue for the device
798 * @mask: alignment mask
801 * set required memory and length aligment for direct dma transactions.
802 * this is used when buiding direct io requests for the queue.
805 void blk_queue_dma_alignment(request_queue_t
*q
, int mask
)
807 q
->dma_alignment
= mask
;
810 EXPORT_SYMBOL(blk_queue_dma_alignment
);
813 * blk_queue_find_tag - find a request by its tag and queue
814 * @q: The request queue for the device
815 * @tag: The tag of the request
818 * Should be used when a device returns a tag and you want to match
821 * no locks need be held.
823 struct request
*blk_queue_find_tag(request_queue_t
*q
, int tag
)
825 struct blk_queue_tag
*bqt
= q
->queue_tags
;
827 if (unlikely(bqt
== NULL
|| tag
>= bqt
->real_max_depth
))
830 return bqt
->tag_index
[tag
];
833 EXPORT_SYMBOL(blk_queue_find_tag
);
836 * __blk_queue_free_tags - release tag maintenance info
837 * @q: the request queue for the device
840 * blk_cleanup_queue() will take care of calling this function, if tagging
841 * has been used. So there's no need to call this directly.
843 static void __blk_queue_free_tags(request_queue_t
*q
)
845 struct blk_queue_tag
*bqt
= q
->queue_tags
;
850 if (atomic_dec_and_test(&bqt
->refcnt
)) {
852 BUG_ON(!list_empty(&bqt
->busy_list
));
854 kfree(bqt
->tag_index
);
855 bqt
->tag_index
= NULL
;
863 q
->queue_tags
= NULL
;
864 q
->queue_flags
&= ~(1 << QUEUE_FLAG_QUEUED
);
868 * blk_queue_free_tags - release tag maintenance info
869 * @q: the request queue for the device
872 * This is used to disabled tagged queuing to a device, yet leave
875 void blk_queue_free_tags(request_queue_t
*q
)
877 clear_bit(QUEUE_FLAG_QUEUED
, &q
->queue_flags
);
880 EXPORT_SYMBOL(blk_queue_free_tags
);
883 init_tag_map(request_queue_t
*q
, struct blk_queue_tag
*tags
, int depth
)
885 struct request
**tag_index
;
886 unsigned long *tag_map
;
889 if (depth
> q
->nr_requests
* 2) {
890 depth
= q
->nr_requests
* 2;
891 printk(KERN_ERR
"%s: adjusted depth to %d\n",
892 __FUNCTION__
, depth
);
895 tag_index
= kmalloc(depth
* sizeof(struct request
*), GFP_ATOMIC
);
899 nr_ulongs
= ALIGN(depth
, BITS_PER_LONG
) / BITS_PER_LONG
;
900 tag_map
= kmalloc(nr_ulongs
* sizeof(unsigned long), GFP_ATOMIC
);
904 memset(tag_index
, 0, depth
* sizeof(struct request
*));
905 memset(tag_map
, 0, nr_ulongs
* sizeof(unsigned long));
906 tags
->real_max_depth
= depth
;
907 tags
->max_depth
= depth
;
908 tags
->tag_index
= tag_index
;
909 tags
->tag_map
= tag_map
;
918 * blk_queue_init_tags - initialize the queue tag info
919 * @q: the request queue for the device
920 * @depth: the maximum queue depth supported
921 * @tags: the tag to use
923 int blk_queue_init_tags(request_queue_t
*q
, int depth
,
924 struct blk_queue_tag
*tags
)
928 BUG_ON(tags
&& q
->queue_tags
&& tags
!= q
->queue_tags
);
930 if (!tags
&& !q
->queue_tags
) {
931 tags
= kmalloc(sizeof(struct blk_queue_tag
), GFP_ATOMIC
);
935 if (init_tag_map(q
, tags
, depth
))
938 INIT_LIST_HEAD(&tags
->busy_list
);
940 atomic_set(&tags
->refcnt
, 1);
941 } else if (q
->queue_tags
) {
942 if ((rc
= blk_queue_resize_tags(q
, depth
)))
944 set_bit(QUEUE_FLAG_QUEUED
, &q
->queue_flags
);
947 atomic_inc(&tags
->refcnt
);
950 * assign it, all done
952 q
->queue_tags
= tags
;
953 q
->queue_flags
|= (1 << QUEUE_FLAG_QUEUED
);
960 EXPORT_SYMBOL(blk_queue_init_tags
);
963 * blk_queue_resize_tags - change the queueing depth
964 * @q: the request queue for the device
965 * @new_depth: the new max command queueing depth
968 * Must be called with the queue lock held.
970 int blk_queue_resize_tags(request_queue_t
*q
, int new_depth
)
972 struct blk_queue_tag
*bqt
= q
->queue_tags
;
973 struct request
**tag_index
;
974 unsigned long *tag_map
;
975 int max_depth
, nr_ulongs
;
981 * if we already have large enough real_max_depth. just
982 * adjust max_depth. *NOTE* as requests with tag value
983 * between new_depth and real_max_depth can be in-flight, tag
984 * map can not be shrunk blindly here.
986 if (new_depth
<= bqt
->real_max_depth
) {
987 bqt
->max_depth
= new_depth
;
992 * save the old state info, so we can copy it back
994 tag_index
= bqt
->tag_index
;
995 tag_map
= bqt
->tag_map
;
996 max_depth
= bqt
->real_max_depth
;
998 if (init_tag_map(q
, bqt
, new_depth
))
1001 memcpy(bqt
->tag_index
, tag_index
, max_depth
* sizeof(struct request
*));
1002 nr_ulongs
= ALIGN(max_depth
, BITS_PER_LONG
) / BITS_PER_LONG
;
1003 memcpy(bqt
->tag_map
, tag_map
, nr_ulongs
* sizeof(unsigned long));
1010 EXPORT_SYMBOL(blk_queue_resize_tags
);
1013 * blk_queue_end_tag - end tag operations for a request
1014 * @q: the request queue for the device
1015 * @rq: the request that has completed
1018 * Typically called when end_that_request_first() returns 0, meaning
1019 * all transfers have been done for a request. It's important to call
1020 * this function before end_that_request_last(), as that will put the
1021 * request back on the free list thus corrupting the internal tag list.
1024 * queue lock must be held.
1026 void blk_queue_end_tag(request_queue_t
*q
, struct request
*rq
)
1028 struct blk_queue_tag
*bqt
= q
->queue_tags
;
1033 if (unlikely(tag
>= bqt
->real_max_depth
))
1035 * This can happen after tag depth has been reduced.
1036 * FIXME: how about a warning or info message here?
1040 if (unlikely(!__test_and_clear_bit(tag
, bqt
->tag_map
))) {
1041 printk(KERN_ERR
"%s: attempt to clear non-busy tag (%d)\n",
1046 list_del_init(&rq
->queuelist
);
1047 rq
->flags
&= ~REQ_QUEUED
;
1050 if (unlikely(bqt
->tag_index
[tag
] == NULL
))
1051 printk(KERN_ERR
"%s: tag %d is missing\n",
1054 bqt
->tag_index
[tag
] = NULL
;
1058 EXPORT_SYMBOL(blk_queue_end_tag
);
1061 * blk_queue_start_tag - find a free tag and assign it
1062 * @q: the request queue for the device
1063 * @rq: the block request that needs tagging
1066 * This can either be used as a stand-alone helper, or possibly be
1067 * assigned as the queue &prep_rq_fn (in which case &struct request
1068 * automagically gets a tag assigned). Note that this function
1069 * assumes that any type of request can be queued! if this is not
1070 * true for your device, you must check the request type before
1071 * calling this function. The request will also be removed from
1072 * the request queue, so it's the drivers responsibility to readd
1073 * it if it should need to be restarted for some reason.
1076 * queue lock must be held.
1078 int blk_queue_start_tag(request_queue_t
*q
, struct request
*rq
)
1080 struct blk_queue_tag
*bqt
= q
->queue_tags
;
1083 if (unlikely((rq
->flags
& REQ_QUEUED
))) {
1085 "%s: request %p for device [%s] already tagged %d",
1087 rq
->rq_disk
? rq
->rq_disk
->disk_name
: "?", rq
->tag
);
1091 tag
= find_first_zero_bit(bqt
->tag_map
, bqt
->max_depth
);
1092 if (tag
>= bqt
->max_depth
)
1095 __set_bit(tag
, bqt
->tag_map
);
1097 rq
->flags
|= REQ_QUEUED
;
1099 bqt
->tag_index
[tag
] = rq
;
1100 blkdev_dequeue_request(rq
);
1101 list_add(&rq
->queuelist
, &bqt
->busy_list
);
1106 EXPORT_SYMBOL(blk_queue_start_tag
);
1109 * blk_queue_invalidate_tags - invalidate all pending tags
1110 * @q: the request queue for the device
1113 * Hardware conditions may dictate a need to stop all pending requests.
1114 * In this case, we will safely clear the block side of the tag queue and
1115 * readd all requests to the request queue in the right order.
1118 * queue lock must be held.
1120 void blk_queue_invalidate_tags(request_queue_t
*q
)
1122 struct blk_queue_tag
*bqt
= q
->queue_tags
;
1123 struct list_head
*tmp
, *n
;
1126 list_for_each_safe(tmp
, n
, &bqt
->busy_list
) {
1127 rq
= list_entry_rq(tmp
);
1129 if (rq
->tag
== -1) {
1131 "%s: bad tag found on list\n", __FUNCTION__
);
1132 list_del_init(&rq
->queuelist
);
1133 rq
->flags
&= ~REQ_QUEUED
;
1135 blk_queue_end_tag(q
, rq
);
1137 rq
->flags
&= ~REQ_STARTED
;
1138 __elv_add_request(q
, rq
, ELEVATOR_INSERT_BACK
, 0);
1142 EXPORT_SYMBOL(blk_queue_invalidate_tags
);
1144 static const char * const rq_flags
[] = {
1165 "REQ_DRIVE_TASKFILE",
1170 "REQ_ORDERED_COLOR",
1173 void blk_dump_rq_flags(struct request
*rq
, char *msg
)
1177 printk("%s: dev %s: flags = ", msg
,
1178 rq
->rq_disk
? rq
->rq_disk
->disk_name
: "?");
1181 if (rq
->flags
& (1 << bit
))
1182 printk("%s ", rq_flags
[bit
]);
1184 } while (bit
< __REQ_NR_BITS
);
1186 printk("\nsector %llu, nr/cnr %lu/%u\n", (unsigned long long)rq
->sector
,
1188 rq
->current_nr_sectors
);
1189 printk("bio %p, biotail %p, buffer %p, data %p, len %u\n", rq
->bio
, rq
->biotail
, rq
->buffer
, rq
->data
, rq
->data_len
);
1191 if (rq
->flags
& (REQ_BLOCK_PC
| REQ_PC
)) {
1193 for (bit
= 0; bit
< sizeof(rq
->cmd
); bit
++)
1194 printk("%02x ", rq
->cmd
[bit
]);
1199 EXPORT_SYMBOL(blk_dump_rq_flags
);
1201 void blk_recount_segments(request_queue_t
*q
, struct bio
*bio
)
1203 struct bio_vec
*bv
, *bvprv
= NULL
;
1204 int i
, nr_phys_segs
, nr_hw_segs
, seg_size
, hw_seg_size
, cluster
;
1205 int high
, highprv
= 1;
1207 if (unlikely(!bio
->bi_io_vec
))
1210 cluster
= q
->queue_flags
& (1 << QUEUE_FLAG_CLUSTER
);
1211 hw_seg_size
= seg_size
= nr_phys_segs
= nr_hw_segs
= 0;
1212 bio_for_each_segment(bv
, bio
, i
) {
1214 * the trick here is making sure that a high page is never
1215 * considered part of another segment, since that might
1216 * change with the bounce page.
1218 high
= page_to_pfn(bv
->bv_page
) >= q
->bounce_pfn
;
1219 if (high
|| highprv
)
1220 goto new_hw_segment
;
1222 if (seg_size
+ bv
->bv_len
> q
->max_segment_size
)
1224 if (!BIOVEC_PHYS_MERGEABLE(bvprv
, bv
))
1226 if (!BIOVEC_SEG_BOUNDARY(q
, bvprv
, bv
))
1228 if (BIOVEC_VIRT_OVERSIZE(hw_seg_size
+ bv
->bv_len
))
1229 goto new_hw_segment
;
1231 seg_size
+= bv
->bv_len
;
1232 hw_seg_size
+= bv
->bv_len
;
1237 if (BIOVEC_VIRT_MERGEABLE(bvprv
, bv
) &&
1238 !BIOVEC_VIRT_OVERSIZE(hw_seg_size
+ bv
->bv_len
)) {
1239 hw_seg_size
+= bv
->bv_len
;
1242 if (hw_seg_size
> bio
->bi_hw_front_size
)
1243 bio
->bi_hw_front_size
= hw_seg_size
;
1244 hw_seg_size
= BIOVEC_VIRT_START_SIZE(bv
) + bv
->bv_len
;
1250 seg_size
= bv
->bv_len
;
1253 if (hw_seg_size
> bio
->bi_hw_back_size
)
1254 bio
->bi_hw_back_size
= hw_seg_size
;
1255 if (nr_hw_segs
== 1 && hw_seg_size
> bio
->bi_hw_front_size
)
1256 bio
->bi_hw_front_size
= hw_seg_size
;
1257 bio
->bi_phys_segments
= nr_phys_segs
;
1258 bio
->bi_hw_segments
= nr_hw_segs
;
1259 bio
->bi_flags
|= (1 << BIO_SEG_VALID
);
1263 static int blk_phys_contig_segment(request_queue_t
*q
, struct bio
*bio
,
1266 if (!(q
->queue_flags
& (1 << QUEUE_FLAG_CLUSTER
)))
1269 if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio
), __BVEC_START(nxt
)))
1271 if (bio
->bi_size
+ nxt
->bi_size
> q
->max_segment_size
)
1275 * bio and nxt are contigous in memory, check if the queue allows
1276 * these two to be merged into one
1278 if (BIO_SEG_BOUNDARY(q
, bio
, nxt
))
1284 static int blk_hw_contig_segment(request_queue_t
*q
, struct bio
*bio
,
1287 if (unlikely(!bio_flagged(bio
, BIO_SEG_VALID
)))
1288 blk_recount_segments(q
, bio
);
1289 if (unlikely(!bio_flagged(nxt
, BIO_SEG_VALID
)))
1290 blk_recount_segments(q
, nxt
);
1291 if (!BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio
), __BVEC_START(nxt
)) ||
1292 BIOVEC_VIRT_OVERSIZE(bio
->bi_hw_front_size
+ bio
->bi_hw_back_size
))
1294 if (bio
->bi_size
+ nxt
->bi_size
> q
->max_segment_size
)
1301 * map a request to scatterlist, return number of sg entries setup. Caller
1302 * must make sure sg can hold rq->nr_phys_segments entries
1304 int blk_rq_map_sg(request_queue_t
*q
, struct request
*rq
, struct scatterlist
*sg
)
1306 struct bio_vec
*bvec
, *bvprv
;
1308 int nsegs
, i
, cluster
;
1311 cluster
= q
->queue_flags
& (1 << QUEUE_FLAG_CLUSTER
);
1314 * for each bio in rq
1317 rq_for_each_bio(bio
, rq
) {
1319 * for each segment in bio
1321 bio_for_each_segment(bvec
, bio
, i
) {
1322 int nbytes
= bvec
->bv_len
;
1324 if (bvprv
&& cluster
) {
1325 if (sg
[nsegs
- 1].length
+ nbytes
> q
->max_segment_size
)
1328 if (!BIOVEC_PHYS_MERGEABLE(bvprv
, bvec
))
1330 if (!BIOVEC_SEG_BOUNDARY(q
, bvprv
, bvec
))
1333 sg
[nsegs
- 1].length
+= nbytes
;
1336 memset(&sg
[nsegs
],0,sizeof(struct scatterlist
));
1337 sg
[nsegs
].page
= bvec
->bv_page
;
1338 sg
[nsegs
].length
= nbytes
;
1339 sg
[nsegs
].offset
= bvec
->bv_offset
;
1344 } /* segments in bio */
1350 EXPORT_SYMBOL(blk_rq_map_sg
);
1353 * the standard queue merge functions, can be overridden with device
1354 * specific ones if so desired
1357 static inline int ll_new_mergeable(request_queue_t
*q
,
1358 struct request
*req
,
1361 int nr_phys_segs
= bio_phys_segments(q
, bio
);
1363 if (req
->nr_phys_segments
+ nr_phys_segs
> q
->max_phys_segments
) {
1364 req
->flags
|= REQ_NOMERGE
;
1365 if (req
== q
->last_merge
)
1366 q
->last_merge
= NULL
;
1371 * A hw segment is just getting larger, bump just the phys
1374 req
->nr_phys_segments
+= nr_phys_segs
;
1378 static inline int ll_new_hw_segment(request_queue_t
*q
,
1379 struct request
*req
,
1382 int nr_hw_segs
= bio_hw_segments(q
, bio
);
1383 int nr_phys_segs
= bio_phys_segments(q
, bio
);
1385 if (req
->nr_hw_segments
+ nr_hw_segs
> q
->max_hw_segments
1386 || req
->nr_phys_segments
+ nr_phys_segs
> q
->max_phys_segments
) {
1387 req
->flags
|= REQ_NOMERGE
;
1388 if (req
== q
->last_merge
)
1389 q
->last_merge
= NULL
;
1394 * This will form the start of a new hw segment. Bump both
1397 req
->nr_hw_segments
+= nr_hw_segs
;
1398 req
->nr_phys_segments
+= nr_phys_segs
;
1402 static int ll_back_merge_fn(request_queue_t
*q
, struct request
*req
,
1405 unsigned short max_sectors
;
1408 if (unlikely(blk_pc_request(req
)))
1409 max_sectors
= q
->max_hw_sectors
;
1411 max_sectors
= q
->max_sectors
;
1413 if (req
->nr_sectors
+ bio_sectors(bio
) > max_sectors
) {
1414 req
->flags
|= REQ_NOMERGE
;
1415 if (req
== q
->last_merge
)
1416 q
->last_merge
= NULL
;
1419 if (unlikely(!bio_flagged(req
->biotail
, BIO_SEG_VALID
)))
1420 blk_recount_segments(q
, req
->biotail
);
1421 if (unlikely(!bio_flagged(bio
, BIO_SEG_VALID
)))
1422 blk_recount_segments(q
, bio
);
1423 len
= req
->biotail
->bi_hw_back_size
+ bio
->bi_hw_front_size
;
1424 if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(req
->biotail
), __BVEC_START(bio
)) &&
1425 !BIOVEC_VIRT_OVERSIZE(len
)) {
1426 int mergeable
= ll_new_mergeable(q
, req
, bio
);
1429 if (req
->nr_hw_segments
== 1)
1430 req
->bio
->bi_hw_front_size
= len
;
1431 if (bio
->bi_hw_segments
== 1)
1432 bio
->bi_hw_back_size
= len
;
1437 return ll_new_hw_segment(q
, req
, bio
);
1440 static int ll_front_merge_fn(request_queue_t
*q
, struct request
*req
,
1443 unsigned short max_sectors
;
1446 if (unlikely(blk_pc_request(req
)))
1447 max_sectors
= q
->max_hw_sectors
;
1449 max_sectors
= q
->max_sectors
;
1452 if (req
->nr_sectors
+ bio_sectors(bio
) > max_sectors
) {
1453 req
->flags
|= REQ_NOMERGE
;
1454 if (req
== q
->last_merge
)
1455 q
->last_merge
= NULL
;
1458 len
= bio
->bi_hw_back_size
+ req
->bio
->bi_hw_front_size
;
1459 if (unlikely(!bio_flagged(bio
, BIO_SEG_VALID
)))
1460 blk_recount_segments(q
, bio
);
1461 if (unlikely(!bio_flagged(req
->bio
, BIO_SEG_VALID
)))
1462 blk_recount_segments(q
, req
->bio
);
1463 if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio
), __BVEC_START(req
->bio
)) &&
1464 !BIOVEC_VIRT_OVERSIZE(len
)) {
1465 int mergeable
= ll_new_mergeable(q
, req
, bio
);
1468 if (bio
->bi_hw_segments
== 1)
1469 bio
->bi_hw_front_size
= len
;
1470 if (req
->nr_hw_segments
== 1)
1471 req
->biotail
->bi_hw_back_size
= len
;
1476 return ll_new_hw_segment(q
, req
, bio
);
1479 static int ll_merge_requests_fn(request_queue_t
*q
, struct request
*req
,
1480 struct request
*next
)
1482 int total_phys_segments
;
1483 int total_hw_segments
;
1486 * First check if the either of the requests are re-queued
1487 * requests. Can't merge them if they are.
1489 if (req
->special
|| next
->special
)
1493 * Will it become too large?
1495 if ((req
->nr_sectors
+ next
->nr_sectors
) > q
->max_sectors
)
1498 total_phys_segments
= req
->nr_phys_segments
+ next
->nr_phys_segments
;
1499 if (blk_phys_contig_segment(q
, req
->biotail
, next
->bio
))
1500 total_phys_segments
--;
1502 if (total_phys_segments
> q
->max_phys_segments
)
1505 total_hw_segments
= req
->nr_hw_segments
+ next
->nr_hw_segments
;
1506 if (blk_hw_contig_segment(q
, req
->biotail
, next
->bio
)) {
1507 int len
= req
->biotail
->bi_hw_back_size
+ next
->bio
->bi_hw_front_size
;
1509 * propagate the combined length to the end of the requests
1511 if (req
->nr_hw_segments
== 1)
1512 req
->bio
->bi_hw_front_size
= len
;
1513 if (next
->nr_hw_segments
== 1)
1514 next
->biotail
->bi_hw_back_size
= len
;
1515 total_hw_segments
--;
1518 if (total_hw_segments
> q
->max_hw_segments
)
1521 /* Merge is OK... */
1522 req
->nr_phys_segments
= total_phys_segments
;
1523 req
->nr_hw_segments
= total_hw_segments
;
1528 * "plug" the device if there are no outstanding requests: this will
1529 * force the transfer to start only after we have put all the requests
1532 * This is called with interrupts off and no requests on the queue and
1533 * with the queue lock held.
1535 void blk_plug_device(request_queue_t
*q
)
1537 WARN_ON(!irqs_disabled());
1540 * don't plug a stopped queue, it must be paired with blk_start_queue()
1541 * which will restart the queueing
1543 if (test_bit(QUEUE_FLAG_STOPPED
, &q
->queue_flags
))
1546 if (!test_and_set_bit(QUEUE_FLAG_PLUGGED
, &q
->queue_flags
))
1547 mod_timer(&q
->unplug_timer
, jiffies
+ q
->unplug_delay
);
1550 EXPORT_SYMBOL(blk_plug_device
);
1553 * remove the queue from the plugged list, if present. called with
1554 * queue lock held and interrupts disabled.
1556 int blk_remove_plug(request_queue_t
*q
)
1558 WARN_ON(!irqs_disabled());
1560 if (!test_and_clear_bit(QUEUE_FLAG_PLUGGED
, &q
->queue_flags
))
1563 del_timer(&q
->unplug_timer
);
1567 EXPORT_SYMBOL(blk_remove_plug
);
1570 * remove the plug and let it rip..
1572 void __generic_unplug_device(request_queue_t
*q
)
1574 if (unlikely(test_bit(QUEUE_FLAG_STOPPED
, &q
->queue_flags
)))
1577 if (!blk_remove_plug(q
))
1582 EXPORT_SYMBOL(__generic_unplug_device
);
1585 * generic_unplug_device - fire a request queue
1586 * @q: The &request_queue_t in question
1589 * Linux uses plugging to build bigger requests queues before letting
1590 * the device have at them. If a queue is plugged, the I/O scheduler
1591 * is still adding and merging requests on the queue. Once the queue
1592 * gets unplugged, the request_fn defined for the queue is invoked and
1593 * transfers started.
1595 void generic_unplug_device(request_queue_t
*q
)
1597 spin_lock_irq(q
->queue_lock
);
1598 __generic_unplug_device(q
);
1599 spin_unlock_irq(q
->queue_lock
);
1601 EXPORT_SYMBOL(generic_unplug_device
);
1603 static void blk_backing_dev_unplug(struct backing_dev_info
*bdi
,
1606 request_queue_t
*q
= bdi
->unplug_io_data
;
1609 * devices don't necessarily have an ->unplug_fn defined
1615 static void blk_unplug_work(void *data
)
1617 request_queue_t
*q
= data
;
1622 static void blk_unplug_timeout(unsigned long data
)
1624 request_queue_t
*q
= (request_queue_t
*)data
;
1626 kblockd_schedule_work(&q
->unplug_work
);
1630 * blk_start_queue - restart a previously stopped queue
1631 * @q: The &request_queue_t in question
1634 * blk_start_queue() will clear the stop flag on the queue, and call
1635 * the request_fn for the queue if it was in a stopped state when
1636 * entered. Also see blk_stop_queue(). Queue lock must be held.
1638 void blk_start_queue(request_queue_t
*q
)
1640 clear_bit(QUEUE_FLAG_STOPPED
, &q
->queue_flags
);
1643 * one level of recursion is ok and is much faster than kicking
1644 * the unplug handling
1646 if (!test_and_set_bit(QUEUE_FLAG_REENTER
, &q
->queue_flags
)) {
1648 clear_bit(QUEUE_FLAG_REENTER
, &q
->queue_flags
);
1651 kblockd_schedule_work(&q
->unplug_work
);
1655 EXPORT_SYMBOL(blk_start_queue
);
1658 * blk_stop_queue - stop a queue
1659 * @q: The &request_queue_t in question
1662 * The Linux block layer assumes that a block driver will consume all
1663 * entries on the request queue when the request_fn strategy is called.
1664 * Often this will not happen, because of hardware limitations (queue
1665 * depth settings). If a device driver gets a 'queue full' response,
1666 * or if it simply chooses not to queue more I/O at one point, it can
1667 * call this function to prevent the request_fn from being called until
1668 * the driver has signalled it's ready to go again. This happens by calling
1669 * blk_start_queue() to restart queue operations. Queue lock must be held.
1671 void blk_stop_queue(request_queue_t
*q
)
1674 set_bit(QUEUE_FLAG_STOPPED
, &q
->queue_flags
);
1676 EXPORT_SYMBOL(blk_stop_queue
);
1679 * blk_sync_queue - cancel any pending callbacks on a queue
1683 * The block layer may perform asynchronous callback activity
1684 * on a queue, such as calling the unplug function after a timeout.
1685 * A block device may call blk_sync_queue to ensure that any
1686 * such activity is cancelled, thus allowing it to release resources
1687 * the the callbacks might use. The caller must already have made sure
1688 * that its ->make_request_fn will not re-add plugging prior to calling
1692 void blk_sync_queue(struct request_queue
*q
)
1694 del_timer_sync(&q
->unplug_timer
);
1697 EXPORT_SYMBOL(blk_sync_queue
);
1700 * blk_run_queue - run a single device queue
1701 * @q: The queue to run
1703 void blk_run_queue(struct request_queue
*q
)
1705 unsigned long flags
;
1707 spin_lock_irqsave(q
->queue_lock
, flags
);
1709 if (!elv_queue_empty(q
))
1711 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1713 EXPORT_SYMBOL(blk_run_queue
);
1716 * blk_cleanup_queue: - release a &request_queue_t when it is no longer needed
1717 * @q: the request queue to be released
1720 * blk_cleanup_queue is the pair to blk_init_queue() or
1721 * blk_queue_make_request(). It should be called when a request queue is
1722 * being released; typically when a block device is being de-registered.
1723 * Currently, its primary task it to free all the &struct request
1724 * structures that were allocated to the queue and the queue itself.
1727 * Hopefully the low level driver will have finished any
1728 * outstanding requests first...
1730 void blk_cleanup_queue(request_queue_t
* q
)
1732 struct request_list
*rl
= &q
->rq
;
1734 if (!atomic_dec_and_test(&q
->refcnt
))
1738 elevator_exit(q
->elevator
);
1743 mempool_destroy(rl
->rq_pool
);
1746 __blk_queue_free_tags(q
);
1748 kmem_cache_free(requestq_cachep
, q
);
1751 EXPORT_SYMBOL(blk_cleanup_queue
);
1753 static int blk_init_free_list(request_queue_t
*q
)
1755 struct request_list
*rl
= &q
->rq
;
1757 rl
->count
[READ
] = rl
->count
[WRITE
] = 0;
1758 rl
->starved
[READ
] = rl
->starved
[WRITE
] = 0;
1760 init_waitqueue_head(&rl
->wait
[READ
]);
1761 init_waitqueue_head(&rl
->wait
[WRITE
]);
1763 rl
->rq_pool
= mempool_create_node(BLKDEV_MIN_RQ
, mempool_alloc_slab
,
1764 mempool_free_slab
, request_cachep
, q
->node
);
1772 request_queue_t
*blk_alloc_queue(gfp_t gfp_mask
)
1774 return blk_alloc_queue_node(gfp_mask
, -1);
1776 EXPORT_SYMBOL(blk_alloc_queue
);
1778 request_queue_t
*blk_alloc_queue_node(gfp_t gfp_mask
, int node_id
)
1782 q
= kmem_cache_alloc_node(requestq_cachep
, gfp_mask
, node_id
);
1786 memset(q
, 0, sizeof(*q
));
1787 init_timer(&q
->unplug_timer
);
1788 atomic_set(&q
->refcnt
, 1);
1790 q
->backing_dev_info
.unplug_io_fn
= blk_backing_dev_unplug
;
1791 q
->backing_dev_info
.unplug_io_data
= q
;
1795 EXPORT_SYMBOL(blk_alloc_queue_node
);
1798 * blk_init_queue - prepare a request queue for use with a block device
1799 * @rfn: The function to be called to process requests that have been
1800 * placed on the queue.
1801 * @lock: Request queue spin lock
1804 * If a block device wishes to use the standard request handling procedures,
1805 * which sorts requests and coalesces adjacent requests, then it must
1806 * call blk_init_queue(). The function @rfn will be called when there
1807 * are requests on the queue that need to be processed. If the device
1808 * supports plugging, then @rfn may not be called immediately when requests
1809 * are available on the queue, but may be called at some time later instead.
1810 * Plugged queues are generally unplugged when a buffer belonging to one
1811 * of the requests on the queue is needed, or due to memory pressure.
1813 * @rfn is not required, or even expected, to remove all requests off the
1814 * queue, but only as many as it can handle at a time. If it does leave
1815 * requests on the queue, it is responsible for arranging that the requests
1816 * get dealt with eventually.
1818 * The queue spin lock must be held while manipulating the requests on the
1821 * Function returns a pointer to the initialized request queue, or NULL if
1822 * it didn't succeed.
1825 * blk_init_queue() must be paired with a blk_cleanup_queue() call
1826 * when the block device is deactivated (such as at module unload).
1829 request_queue_t
*blk_init_queue(request_fn_proc
*rfn
, spinlock_t
*lock
)
1831 return blk_init_queue_node(rfn
, lock
, -1);
1833 EXPORT_SYMBOL(blk_init_queue
);
1836 blk_init_queue_node(request_fn_proc
*rfn
, spinlock_t
*lock
, int node_id
)
1838 request_queue_t
*q
= blk_alloc_queue_node(GFP_KERNEL
, node_id
);
1844 if (blk_init_free_list(q
))
1848 * if caller didn't supply a lock, they get per-queue locking with
1852 spin_lock_init(&q
->__queue_lock
);
1853 lock
= &q
->__queue_lock
;
1856 q
->request_fn
= rfn
;
1857 q
->back_merge_fn
= ll_back_merge_fn
;
1858 q
->front_merge_fn
= ll_front_merge_fn
;
1859 q
->merge_requests_fn
= ll_merge_requests_fn
;
1860 q
->prep_rq_fn
= NULL
;
1861 q
->unplug_fn
= generic_unplug_device
;
1862 q
->queue_flags
= (1 << QUEUE_FLAG_CLUSTER
);
1863 q
->queue_lock
= lock
;
1865 blk_queue_segment_boundary(q
, 0xffffffff);
1867 blk_queue_make_request(q
, __make_request
);
1868 blk_queue_max_segment_size(q
, MAX_SEGMENT_SIZE
);
1870 blk_queue_max_hw_segments(q
, MAX_HW_SEGMENTS
);
1871 blk_queue_max_phys_segments(q
, MAX_PHYS_SEGMENTS
);
1876 if (!elevator_init(q
, NULL
)) {
1877 blk_queue_congestion_threshold(q
);
1881 blk_cleanup_queue(q
);
1883 kmem_cache_free(requestq_cachep
, q
);
1886 EXPORT_SYMBOL(blk_init_queue_node
);
1888 int blk_get_queue(request_queue_t
*q
)
1890 if (likely(!test_bit(QUEUE_FLAG_DEAD
, &q
->queue_flags
))) {
1891 atomic_inc(&q
->refcnt
);
1898 EXPORT_SYMBOL(blk_get_queue
);
1900 static inline void blk_free_request(request_queue_t
*q
, struct request
*rq
)
1902 if (rq
->flags
& REQ_ELVPRIV
)
1903 elv_put_request(q
, rq
);
1904 mempool_free(rq
, q
->rq
.rq_pool
);
1907 static inline struct request
*
1908 blk_alloc_request(request_queue_t
*q
, int rw
, struct bio
*bio
,
1909 int priv
, gfp_t gfp_mask
)
1911 struct request
*rq
= mempool_alloc(q
->rq
.rq_pool
, gfp_mask
);
1917 * first three bits are identical in rq->flags and bio->bi_rw,
1918 * see bio.h and blkdev.h
1923 if (unlikely(elv_set_request(q
, rq
, bio
, gfp_mask
))) {
1924 mempool_free(rq
, q
->rq
.rq_pool
);
1927 rq
->flags
|= REQ_ELVPRIV
;
1934 * ioc_batching returns true if the ioc is a valid batching request and
1935 * should be given priority access to a request.
1937 static inline int ioc_batching(request_queue_t
*q
, struct io_context
*ioc
)
1943 * Make sure the process is able to allocate at least 1 request
1944 * even if the batch times out, otherwise we could theoretically
1947 return ioc
->nr_batch_requests
== q
->nr_batching
||
1948 (ioc
->nr_batch_requests
> 0
1949 && time_before(jiffies
, ioc
->last_waited
+ BLK_BATCH_TIME
));
1953 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
1954 * will cause the process to be a "batcher" on all queues in the system. This
1955 * is the behaviour we want though - once it gets a wakeup it should be given
1958 static void ioc_set_batching(request_queue_t
*q
, struct io_context
*ioc
)
1960 if (!ioc
|| ioc_batching(q
, ioc
))
1963 ioc
->nr_batch_requests
= q
->nr_batching
;
1964 ioc
->last_waited
= jiffies
;
1967 static void __freed_request(request_queue_t
*q
, int rw
)
1969 struct request_list
*rl
= &q
->rq
;
1971 if (rl
->count
[rw
] < queue_congestion_off_threshold(q
))
1972 clear_queue_congested(q
, rw
);
1974 if (rl
->count
[rw
] + 1 <= q
->nr_requests
) {
1975 if (waitqueue_active(&rl
->wait
[rw
]))
1976 wake_up(&rl
->wait
[rw
]);
1978 blk_clear_queue_full(q
, rw
);
1983 * A request has just been released. Account for it, update the full and
1984 * congestion status, wake up any waiters. Called under q->queue_lock.
1986 static void freed_request(request_queue_t
*q
, int rw
, int priv
)
1988 struct request_list
*rl
= &q
->rq
;
1994 __freed_request(q
, rw
);
1996 if (unlikely(rl
->starved
[rw
^ 1]))
1997 __freed_request(q
, rw
^ 1);
2000 #define blkdev_free_rq(list) list_entry((list)->next, struct request, queuelist)
2002 * Get a free request, queue_lock must be held.
2003 * Returns NULL on failure, with queue_lock held.
2004 * Returns !NULL on success, with queue_lock *not held*.
2006 static struct request
*get_request(request_queue_t
*q
, int rw
, struct bio
*bio
,
2009 struct request
*rq
= NULL
;
2010 struct request_list
*rl
= &q
->rq
;
2011 struct io_context
*ioc
= NULL
;
2012 int may_queue
, priv
;
2014 may_queue
= elv_may_queue(q
, rw
, bio
);
2015 if (may_queue
== ELV_MQUEUE_NO
)
2018 if (rl
->count
[rw
]+1 >= queue_congestion_on_threshold(q
)) {
2019 if (rl
->count
[rw
]+1 >= q
->nr_requests
) {
2020 ioc
= current_io_context(GFP_ATOMIC
);
2022 * The queue will fill after this allocation, so set
2023 * it as full, and mark this process as "batching".
2024 * This process will be allowed to complete a batch of
2025 * requests, others will be blocked.
2027 if (!blk_queue_full(q
, rw
)) {
2028 ioc_set_batching(q
, ioc
);
2029 blk_set_queue_full(q
, rw
);
2031 if (may_queue
!= ELV_MQUEUE_MUST
2032 && !ioc_batching(q
, ioc
)) {
2034 * The queue is full and the allocating
2035 * process is not a "batcher", and not
2036 * exempted by the IO scheduler
2042 set_queue_congested(q
, rw
);
2046 * Only allow batching queuers to allocate up to 50% over the defined
2047 * limit of requests, otherwise we could have thousands of requests
2048 * allocated with any setting of ->nr_requests
2050 if (rl
->count
[rw
] >= (3 * q
->nr_requests
/ 2))
2054 rl
->starved
[rw
] = 0;
2056 priv
= !test_bit(QUEUE_FLAG_ELVSWITCH
, &q
->queue_flags
);
2060 spin_unlock_irq(q
->queue_lock
);
2062 rq
= blk_alloc_request(q
, rw
, bio
, priv
, gfp_mask
);
2063 if (unlikely(!rq
)) {
2065 * Allocation failed presumably due to memory. Undo anything
2066 * we might have messed up.
2068 * Allocating task should really be put onto the front of the
2069 * wait queue, but this is pretty rare.
2071 spin_lock_irq(q
->queue_lock
);
2072 freed_request(q
, rw
, priv
);
2075 * in the very unlikely event that allocation failed and no
2076 * requests for this direction was pending, mark us starved
2077 * so that freeing of a request in the other direction will
2078 * notice us. another possible fix would be to split the
2079 * rq mempool into READ and WRITE
2082 if (unlikely(rl
->count
[rw
] == 0))
2083 rl
->starved
[rw
] = 1;
2089 * ioc may be NULL here, and ioc_batching will be false. That's
2090 * OK, if the queue is under the request limit then requests need
2091 * not count toward the nr_batch_requests limit. There will always
2092 * be some limit enforced by BLK_BATCH_TIME.
2094 if (ioc_batching(q
, ioc
))
2095 ioc
->nr_batch_requests
--;
2104 * No available requests for this queue, unplug the device and wait for some
2105 * requests to become available.
2107 * Called with q->queue_lock held, and returns with it unlocked.
2109 static struct request
*get_request_wait(request_queue_t
*q
, int rw
,
2114 rq
= get_request(q
, rw
, bio
, GFP_NOIO
);
2117 struct request_list
*rl
= &q
->rq
;
2119 prepare_to_wait_exclusive(&rl
->wait
[rw
], &wait
,
2120 TASK_UNINTERRUPTIBLE
);
2122 rq
= get_request(q
, rw
, bio
, GFP_NOIO
);
2125 struct io_context
*ioc
;
2127 __generic_unplug_device(q
);
2128 spin_unlock_irq(q
->queue_lock
);
2132 * After sleeping, we become a "batching" process and
2133 * will be able to allocate at least one request, and
2134 * up to a big batch of them for a small period time.
2135 * See ioc_batching, ioc_set_batching
2137 ioc
= current_io_context(GFP_NOIO
);
2138 ioc_set_batching(q
, ioc
);
2140 spin_lock_irq(q
->queue_lock
);
2142 finish_wait(&rl
->wait
[rw
], &wait
);
2148 struct request
*blk_get_request(request_queue_t
*q
, int rw
, gfp_t gfp_mask
)
2152 BUG_ON(rw
!= READ
&& rw
!= WRITE
);
2154 spin_lock_irq(q
->queue_lock
);
2155 if (gfp_mask
& __GFP_WAIT
) {
2156 rq
= get_request_wait(q
, rw
, NULL
);
2158 rq
= get_request(q
, rw
, NULL
, gfp_mask
);
2160 spin_unlock_irq(q
->queue_lock
);
2162 /* q->queue_lock is unlocked at this point */
2166 EXPORT_SYMBOL(blk_get_request
);
2169 * blk_requeue_request - put a request back on queue
2170 * @q: request queue where request should be inserted
2171 * @rq: request to be inserted
2174 * Drivers often keep queueing requests until the hardware cannot accept
2175 * more, when that condition happens we need to put the request back
2176 * on the queue. Must be called with queue lock held.
2178 void blk_requeue_request(request_queue_t
*q
, struct request
*rq
)
2180 if (blk_rq_tagged(rq
))
2181 blk_queue_end_tag(q
, rq
);
2183 elv_requeue_request(q
, rq
);
2186 EXPORT_SYMBOL(blk_requeue_request
);
2189 * blk_insert_request - insert a special request in to a request queue
2190 * @q: request queue where request should be inserted
2191 * @rq: request to be inserted
2192 * @at_head: insert request at head or tail of queue
2193 * @data: private data
2196 * Many block devices need to execute commands asynchronously, so they don't
2197 * block the whole kernel from preemption during request execution. This is
2198 * accomplished normally by inserting aritficial requests tagged as
2199 * REQ_SPECIAL in to the corresponding request queue, and letting them be
2200 * scheduled for actual execution by the request queue.
2202 * We have the option of inserting the head or the tail of the queue.
2203 * Typically we use the tail for new ioctls and so forth. We use the head
2204 * of the queue for things like a QUEUE_FULL message from a device, or a
2205 * host that is unable to accept a particular command.
2207 void blk_insert_request(request_queue_t
*q
, struct request
*rq
,
2208 int at_head
, void *data
)
2210 int where
= at_head
? ELEVATOR_INSERT_FRONT
: ELEVATOR_INSERT_BACK
;
2211 unsigned long flags
;
2214 * tell I/O scheduler that this isn't a regular read/write (ie it
2215 * must not attempt merges on this) and that it acts as a soft
2218 rq
->flags
|= REQ_SPECIAL
| REQ_SOFTBARRIER
;
2222 spin_lock_irqsave(q
->queue_lock
, flags
);
2225 * If command is tagged, release the tag
2227 if (blk_rq_tagged(rq
))
2228 blk_queue_end_tag(q
, rq
);
2230 drive_stat_acct(rq
, rq
->nr_sectors
, 1);
2231 __elv_add_request(q
, rq
, where
, 0);
2233 if (blk_queue_plugged(q
))
2234 __generic_unplug_device(q
);
2237 spin_unlock_irqrestore(q
->queue_lock
, flags
);
2240 EXPORT_SYMBOL(blk_insert_request
);
2243 * blk_rq_map_user - map user data to a request, for REQ_BLOCK_PC usage
2244 * @q: request queue where request should be inserted
2245 * @rq: request structure to fill
2246 * @ubuf: the user buffer
2247 * @len: length of user data
2250 * Data will be mapped directly for zero copy io, if possible. Otherwise
2251 * a kernel bounce buffer is used.
2253 * A matching blk_rq_unmap_user() must be issued at the end of io, while
2254 * still in process context.
2256 * Note: The mapped bio may need to be bounced through blk_queue_bounce()
2257 * before being submitted to the device, as pages mapped may be out of
2258 * reach. It's the callers responsibility to make sure this happens. The
2259 * original bio must be passed back in to blk_rq_unmap_user() for proper
2262 int blk_rq_map_user(request_queue_t
*q
, struct request
*rq
, void __user
*ubuf
,
2265 unsigned long uaddr
;
2269 if (len
> (q
->max_hw_sectors
<< 9))
2274 reading
= rq_data_dir(rq
) == READ
;
2277 * if alignment requirement is satisfied, map in user pages for
2278 * direct dma. else, set up kernel bounce buffers
2280 uaddr
= (unsigned long) ubuf
;
2281 if (!(uaddr
& queue_dma_alignment(q
)) && !(len
& queue_dma_alignment(q
)))
2282 bio
= bio_map_user(q
, NULL
, uaddr
, len
, reading
);
2284 bio
= bio_copy_user(q
, uaddr
, len
, reading
);
2287 rq
->bio
= rq
->biotail
= bio
;
2288 blk_rq_bio_prep(q
, rq
, bio
);
2290 rq
->buffer
= rq
->data
= NULL
;
2296 * bio is the err-ptr
2298 return PTR_ERR(bio
);
2301 EXPORT_SYMBOL(blk_rq_map_user
);
2304 * blk_rq_map_user_iov - map user data to a request, for REQ_BLOCK_PC usage
2305 * @q: request queue where request should be inserted
2306 * @rq: request to map data to
2307 * @iov: pointer to the iovec
2308 * @iov_count: number of elements in the iovec
2311 * Data will be mapped directly for zero copy io, if possible. Otherwise
2312 * a kernel bounce buffer is used.
2314 * A matching blk_rq_unmap_user() must be issued at the end of io, while
2315 * still in process context.
2317 * Note: The mapped bio may need to be bounced through blk_queue_bounce()
2318 * before being submitted to the device, as pages mapped may be out of
2319 * reach. It's the callers responsibility to make sure this happens. The
2320 * original bio must be passed back in to blk_rq_unmap_user() for proper
2323 int blk_rq_map_user_iov(request_queue_t
*q
, struct request
*rq
,
2324 struct sg_iovec
*iov
, int iov_count
)
2328 if (!iov
|| iov_count
<= 0)
2331 /* we don't allow misaligned data like bio_map_user() does. If the
2332 * user is using sg, they're expected to know the alignment constraints
2333 * and respect them accordingly */
2334 bio
= bio_map_user_iov(q
, NULL
, iov
, iov_count
, rq_data_dir(rq
)== READ
);
2336 return PTR_ERR(bio
);
2338 rq
->bio
= rq
->biotail
= bio
;
2339 blk_rq_bio_prep(q
, rq
, bio
);
2340 rq
->buffer
= rq
->data
= NULL
;
2341 rq
->data_len
= bio
->bi_size
;
2345 EXPORT_SYMBOL(blk_rq_map_user_iov
);
2348 * blk_rq_unmap_user - unmap a request with user data
2349 * @bio: bio to be unmapped
2350 * @ulen: length of user buffer
2353 * Unmap a bio previously mapped by blk_rq_map_user().
2355 int blk_rq_unmap_user(struct bio
*bio
, unsigned int ulen
)
2360 if (bio_flagged(bio
, BIO_USER_MAPPED
))
2361 bio_unmap_user(bio
);
2363 ret
= bio_uncopy_user(bio
);
2369 EXPORT_SYMBOL(blk_rq_unmap_user
);
2372 * blk_rq_map_kern - map kernel data to a request, for REQ_BLOCK_PC usage
2373 * @q: request queue where request should be inserted
2374 * @rq: request to fill
2375 * @kbuf: the kernel buffer
2376 * @len: length of user data
2377 * @gfp_mask: memory allocation flags
2379 int blk_rq_map_kern(request_queue_t
*q
, struct request
*rq
, void *kbuf
,
2380 unsigned int len
, gfp_t gfp_mask
)
2384 if (len
> (q
->max_hw_sectors
<< 9))
2389 bio
= bio_map_kern(q
, kbuf
, len
, gfp_mask
);
2391 return PTR_ERR(bio
);
2393 if (rq_data_dir(rq
) == WRITE
)
2394 bio
->bi_rw
|= (1 << BIO_RW
);
2396 rq
->bio
= rq
->biotail
= bio
;
2397 blk_rq_bio_prep(q
, rq
, bio
);
2399 rq
->buffer
= rq
->data
= NULL
;
2404 EXPORT_SYMBOL(blk_rq_map_kern
);
2407 * blk_execute_rq_nowait - insert a request into queue for execution
2408 * @q: queue to insert the request in
2409 * @bd_disk: matching gendisk
2410 * @rq: request to insert
2411 * @at_head: insert request at head or tail of queue
2412 * @done: I/O completion handler
2415 * Insert a fully prepared request at the back of the io scheduler queue
2416 * for execution. Don't wait for completion.
2418 void blk_execute_rq_nowait(request_queue_t
*q
, struct gendisk
*bd_disk
,
2419 struct request
*rq
, int at_head
,
2422 int where
= at_head
? ELEVATOR_INSERT_FRONT
: ELEVATOR_INSERT_BACK
;
2424 rq
->rq_disk
= bd_disk
;
2425 rq
->flags
|= REQ_NOMERGE
;
2427 elv_add_request(q
, rq
, where
, 1);
2428 generic_unplug_device(q
);
2431 EXPORT_SYMBOL_GPL(blk_execute_rq_nowait
);
2434 * blk_execute_rq - insert a request into queue for execution
2435 * @q: queue to insert the request in
2436 * @bd_disk: matching gendisk
2437 * @rq: request to insert
2438 * @at_head: insert request at head or tail of queue
2441 * Insert a fully prepared request at the back of the io scheduler queue
2442 * for execution and wait for completion.
2444 int blk_execute_rq(request_queue_t
*q
, struct gendisk
*bd_disk
,
2445 struct request
*rq
, int at_head
)
2447 DECLARE_COMPLETION(wait
);
2448 char sense
[SCSI_SENSE_BUFFERSIZE
];
2452 * we need an extra reference to the request, so we can look at
2453 * it after io completion
2458 memset(sense
, 0, sizeof(sense
));
2463 rq
->waiting
= &wait
;
2464 blk_execute_rq_nowait(q
, bd_disk
, rq
, at_head
, blk_end_sync_rq
);
2465 wait_for_completion(&wait
);
2474 EXPORT_SYMBOL(blk_execute_rq
);
2477 * blkdev_issue_flush - queue a flush
2478 * @bdev: blockdev to issue flush for
2479 * @error_sector: error sector
2482 * Issue a flush for the block device in question. Caller can supply
2483 * room for storing the error offset in case of a flush error, if they
2484 * wish to. Caller must run wait_for_completion() on its own.
2486 int blkdev_issue_flush(struct block_device
*bdev
, sector_t
*error_sector
)
2490 if (bdev
->bd_disk
== NULL
)
2493 q
= bdev_get_queue(bdev
);
2496 if (!q
->issue_flush_fn
)
2499 return q
->issue_flush_fn(q
, bdev
->bd_disk
, error_sector
);
2502 EXPORT_SYMBOL(blkdev_issue_flush
);
2504 static void drive_stat_acct(struct request
*rq
, int nr_sectors
, int new_io
)
2506 int rw
= rq_data_dir(rq
);
2508 if (!blk_fs_request(rq
) || !rq
->rq_disk
)
2512 __disk_stat_inc(rq
->rq_disk
, merges
[rw
]);
2514 disk_round_stats(rq
->rq_disk
);
2515 rq
->rq_disk
->in_flight
++;
2520 * add-request adds a request to the linked list.
2521 * queue lock is held and interrupts disabled, as we muck with the
2522 * request queue list.
2524 static inline void add_request(request_queue_t
* q
, struct request
* req
)
2526 drive_stat_acct(req
, req
->nr_sectors
, 1);
2529 q
->activity_fn(q
->activity_data
, rq_data_dir(req
));
2532 * elevator indicated where it wants this request to be
2533 * inserted at elevator_merge time
2535 __elv_add_request(q
, req
, ELEVATOR_INSERT_SORT
, 0);
2539 * disk_round_stats() - Round off the performance stats on a struct
2542 * The average IO queue length and utilisation statistics are maintained
2543 * by observing the current state of the queue length and the amount of
2544 * time it has been in this state for.
2546 * Normally, that accounting is done on IO completion, but that can result
2547 * in more than a second's worth of IO being accounted for within any one
2548 * second, leading to >100% utilisation. To deal with that, we call this
2549 * function to do a round-off before returning the results when reading
2550 * /proc/diskstats. This accounts immediately for all queue usage up to
2551 * the current jiffies and restarts the counters again.
2553 void disk_round_stats(struct gendisk
*disk
)
2555 unsigned long now
= jiffies
;
2557 if (now
== disk
->stamp
)
2560 if (disk
->in_flight
) {
2561 __disk_stat_add(disk
, time_in_queue
,
2562 disk
->in_flight
* (now
- disk
->stamp
));
2563 __disk_stat_add(disk
, io_ticks
, (now
- disk
->stamp
));
2569 * queue lock must be held
2571 void __blk_put_request(request_queue_t
*q
, struct request
*req
)
2573 struct request_list
*rl
= req
->rl
;
2577 if (unlikely(--req
->ref_count
))
2580 elv_completed_request(q
, req
);
2582 req
->rq_status
= RQ_INACTIVE
;
2586 * Request may not have originated from ll_rw_blk. if not,
2587 * it didn't come out of our reserved rq pools
2590 int rw
= rq_data_dir(req
);
2591 int priv
= req
->flags
& REQ_ELVPRIV
;
2593 BUG_ON(!list_empty(&req
->queuelist
));
2595 blk_free_request(q
, req
);
2596 freed_request(q
, rw
, priv
);
2600 EXPORT_SYMBOL_GPL(__blk_put_request
);
2602 void blk_put_request(struct request
*req
)
2604 unsigned long flags
;
2605 request_queue_t
*q
= req
->q
;
2608 * Gee, IDE calls in w/ NULL q. Fix IDE and remove the
2609 * following if (q) test.
2612 spin_lock_irqsave(q
->queue_lock
, flags
);
2613 __blk_put_request(q
, req
);
2614 spin_unlock_irqrestore(q
->queue_lock
, flags
);
2618 EXPORT_SYMBOL(blk_put_request
);
2621 * blk_end_sync_rq - executes a completion event on a request
2622 * @rq: request to complete
2624 void blk_end_sync_rq(struct request
*rq
, int error
)
2626 struct completion
*waiting
= rq
->waiting
;
2629 __blk_put_request(rq
->q
, rq
);
2632 * complete last, if this is a stack request the process (and thus
2633 * the rq pointer) could be invalid right after this complete()
2637 EXPORT_SYMBOL(blk_end_sync_rq
);
2640 * blk_congestion_wait - wait for a queue to become uncongested
2641 * @rw: READ or WRITE
2642 * @timeout: timeout in jiffies
2644 * Waits for up to @timeout jiffies for a queue (any queue) to exit congestion.
2645 * If no queues are congested then just wait for the next request to be
2648 long blk_congestion_wait(int rw
, long timeout
)
2652 wait_queue_head_t
*wqh
= &congestion_wqh
[rw
];
2654 prepare_to_wait(wqh
, &wait
, TASK_UNINTERRUPTIBLE
);
2655 ret
= io_schedule_timeout(timeout
);
2656 finish_wait(wqh
, &wait
);
2660 EXPORT_SYMBOL(blk_congestion_wait
);
2663 * Has to be called with the request spinlock acquired
2665 static int attempt_merge(request_queue_t
*q
, struct request
*req
,
2666 struct request
*next
)
2668 if (!rq_mergeable(req
) || !rq_mergeable(next
))
2674 if (req
->sector
+ req
->nr_sectors
!= next
->sector
)
2677 if (rq_data_dir(req
) != rq_data_dir(next
)
2678 || req
->rq_disk
!= next
->rq_disk
2679 || next
->waiting
|| next
->special
)
2683 * If we are allowed to merge, then append bio list
2684 * from next to rq and release next. merge_requests_fn
2685 * will have updated segment counts, update sector
2688 if (!q
->merge_requests_fn(q
, req
, next
))
2692 * At this point we have either done a back merge
2693 * or front merge. We need the smaller start_time of
2694 * the merged requests to be the current request
2695 * for accounting purposes.
2697 if (time_after(req
->start_time
, next
->start_time
))
2698 req
->start_time
= next
->start_time
;
2700 req
->biotail
->bi_next
= next
->bio
;
2701 req
->biotail
= next
->biotail
;
2703 req
->nr_sectors
= req
->hard_nr_sectors
+= next
->hard_nr_sectors
;
2705 elv_merge_requests(q
, req
, next
);
2708 disk_round_stats(req
->rq_disk
);
2709 req
->rq_disk
->in_flight
--;
2712 req
->ioprio
= ioprio_best(req
->ioprio
, next
->ioprio
);
2714 __blk_put_request(q
, next
);
2718 static inline int attempt_back_merge(request_queue_t
*q
, struct request
*rq
)
2720 struct request
*next
= elv_latter_request(q
, rq
);
2723 return attempt_merge(q
, rq
, next
);
2728 static inline int attempt_front_merge(request_queue_t
*q
, struct request
*rq
)
2730 struct request
*prev
= elv_former_request(q
, rq
);
2733 return attempt_merge(q
, prev
, rq
);
2739 * blk_attempt_remerge - attempt to remerge active head with next request
2740 * @q: The &request_queue_t belonging to the device
2741 * @rq: The head request (usually)
2744 * For head-active devices, the queue can easily be unplugged so quickly
2745 * that proper merging is not done on the front request. This may hurt
2746 * performance greatly for some devices. The block layer cannot safely
2747 * do merging on that first request for these queues, but the driver can
2748 * call this function and make it happen any way. Only the driver knows
2749 * when it is safe to do so.
2751 void blk_attempt_remerge(request_queue_t
*q
, struct request
*rq
)
2753 unsigned long flags
;
2755 spin_lock_irqsave(q
->queue_lock
, flags
);
2756 attempt_back_merge(q
, rq
);
2757 spin_unlock_irqrestore(q
->queue_lock
, flags
);
2760 EXPORT_SYMBOL(blk_attempt_remerge
);
2762 static void init_request_from_bio(struct request
*req
, struct bio
*bio
)
2764 req
->flags
|= REQ_CMD
;
2767 * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
2769 if (bio_rw_ahead(bio
) || bio_failfast(bio
))
2770 req
->flags
|= REQ_FAILFAST
;
2773 * REQ_BARRIER implies no merging, but lets make it explicit
2775 if (unlikely(bio_barrier(bio
)))
2776 req
->flags
|= (REQ_HARDBARRIER
| REQ_NOMERGE
);
2779 req
->hard_sector
= req
->sector
= bio
->bi_sector
;
2780 req
->hard_nr_sectors
= req
->nr_sectors
= bio_sectors(bio
);
2781 req
->current_nr_sectors
= req
->hard_cur_sectors
= bio_cur_sectors(bio
);
2782 req
->nr_phys_segments
= bio_phys_segments(req
->q
, bio
);
2783 req
->nr_hw_segments
= bio_hw_segments(req
->q
, bio
);
2784 req
->buffer
= bio_data(bio
); /* see ->buffer comment above */
2785 req
->waiting
= NULL
;
2786 req
->bio
= req
->biotail
= bio
;
2787 req
->ioprio
= bio_prio(bio
);
2788 req
->rq_disk
= bio
->bi_bdev
->bd_disk
;
2789 req
->start_time
= jiffies
;
2792 static int __make_request(request_queue_t
*q
, struct bio
*bio
)
2794 struct request
*req
;
2795 int el_ret
, rw
, nr_sectors
, cur_nr_sectors
, barrier
, err
, sync
;
2796 unsigned short prio
;
2799 sector
= bio
->bi_sector
;
2800 nr_sectors
= bio_sectors(bio
);
2801 cur_nr_sectors
= bio_cur_sectors(bio
);
2802 prio
= bio_prio(bio
);
2804 rw
= bio_data_dir(bio
);
2805 sync
= bio_sync(bio
);
2808 * low level driver can indicate that it wants pages above a
2809 * certain limit bounced to low memory (ie for highmem, or even
2810 * ISA dma in theory)
2812 blk_queue_bounce(q
, &bio
);
2814 spin_lock_prefetch(q
->queue_lock
);
2816 barrier
= bio_barrier(bio
);
2817 if (unlikely(barrier
) && (q
->next_ordered
== QUEUE_ORDERED_NONE
)) {
2822 spin_lock_irq(q
->queue_lock
);
2824 if (unlikely(barrier
) || elv_queue_empty(q
))
2827 el_ret
= elv_merge(q
, &req
, bio
);
2829 case ELEVATOR_BACK_MERGE
:
2830 BUG_ON(!rq_mergeable(req
));
2832 if (!q
->back_merge_fn(q
, req
, bio
))
2835 req
->biotail
->bi_next
= bio
;
2837 req
->nr_sectors
= req
->hard_nr_sectors
+= nr_sectors
;
2838 req
->ioprio
= ioprio_best(req
->ioprio
, prio
);
2839 drive_stat_acct(req
, nr_sectors
, 0);
2840 if (!attempt_back_merge(q
, req
))
2841 elv_merged_request(q
, req
);
2844 case ELEVATOR_FRONT_MERGE
:
2845 BUG_ON(!rq_mergeable(req
));
2847 if (!q
->front_merge_fn(q
, req
, bio
))
2850 bio
->bi_next
= req
->bio
;
2854 * may not be valid. if the low level driver said
2855 * it didn't need a bounce buffer then it better
2856 * not touch req->buffer either...
2858 req
->buffer
= bio_data(bio
);
2859 req
->current_nr_sectors
= cur_nr_sectors
;
2860 req
->hard_cur_sectors
= cur_nr_sectors
;
2861 req
->sector
= req
->hard_sector
= sector
;
2862 req
->nr_sectors
= req
->hard_nr_sectors
+= nr_sectors
;
2863 req
->ioprio
= ioprio_best(req
->ioprio
, prio
);
2864 drive_stat_acct(req
, nr_sectors
, 0);
2865 if (!attempt_front_merge(q
, req
))
2866 elv_merged_request(q
, req
);
2869 /* ELV_NO_MERGE: elevator says don't/can't merge. */
2876 * Grab a free request. This is might sleep but can not fail.
2877 * Returns with the queue unlocked.
2879 req
= get_request_wait(q
, rw
, bio
);
2882 * After dropping the lock and possibly sleeping here, our request
2883 * may now be mergeable after it had proven unmergeable (above).
2884 * We don't worry about that case for efficiency. It won't happen
2885 * often, and the elevators are able to handle it.
2887 init_request_from_bio(req
, bio
);
2889 spin_lock_irq(q
->queue_lock
);
2890 if (elv_queue_empty(q
))
2892 add_request(q
, req
);
2895 __generic_unplug_device(q
);
2897 spin_unlock_irq(q
->queue_lock
);
2901 bio_endio(bio
, nr_sectors
<< 9, err
);
2906 * If bio->bi_dev is a partition, remap the location
2908 static inline void blk_partition_remap(struct bio
*bio
)
2910 struct block_device
*bdev
= bio
->bi_bdev
;
2912 if (bdev
!= bdev
->bd_contains
) {
2913 struct hd_struct
*p
= bdev
->bd_part
;
2914 const int rw
= bio_data_dir(bio
);
2916 p
->sectors
[rw
] += bio_sectors(bio
);
2919 bio
->bi_sector
+= p
->start_sect
;
2920 bio
->bi_bdev
= bdev
->bd_contains
;
2924 static void handle_bad_sector(struct bio
*bio
)
2926 char b
[BDEVNAME_SIZE
];
2928 printk(KERN_INFO
"attempt to access beyond end of device\n");
2929 printk(KERN_INFO
"%s: rw=%ld, want=%Lu, limit=%Lu\n",
2930 bdevname(bio
->bi_bdev
, b
),
2932 (unsigned long long)bio
->bi_sector
+ bio_sectors(bio
),
2933 (long long)(bio
->bi_bdev
->bd_inode
->i_size
>> 9));
2935 set_bit(BIO_EOF
, &bio
->bi_flags
);
2939 * generic_make_request: hand a buffer to its device driver for I/O
2940 * @bio: The bio describing the location in memory and on the device.
2942 * generic_make_request() is used to make I/O requests of block
2943 * devices. It is passed a &struct bio, which describes the I/O that needs
2946 * generic_make_request() does not return any status. The
2947 * success/failure status of the request, along with notification of
2948 * completion, is delivered asynchronously through the bio->bi_end_io
2949 * function described (one day) else where.
2951 * The caller of generic_make_request must make sure that bi_io_vec
2952 * are set to describe the memory buffer, and that bi_dev and bi_sector are
2953 * set to describe the device address, and the
2954 * bi_end_io and optionally bi_private are set to describe how
2955 * completion notification should be signaled.
2957 * generic_make_request and the drivers it calls may use bi_next if this
2958 * bio happens to be merged with someone else, and may change bi_dev and
2959 * bi_sector for remaps as it sees fit. So the values of these fields
2960 * should NOT be depended on after the call to generic_make_request.
2962 void generic_make_request(struct bio
*bio
)
2966 int ret
, nr_sectors
= bio_sectors(bio
);
2969 /* Test device or partition size, when known. */
2970 maxsector
= bio
->bi_bdev
->bd_inode
->i_size
>> 9;
2972 sector_t sector
= bio
->bi_sector
;
2974 if (maxsector
< nr_sectors
|| maxsector
- nr_sectors
< sector
) {
2976 * This may well happen - the kernel calls bread()
2977 * without checking the size of the device, e.g., when
2978 * mounting a device.
2980 handle_bad_sector(bio
);
2986 * Resolve the mapping until finished. (drivers are
2987 * still free to implement/resolve their own stacking
2988 * by explicitly returning 0)
2990 * NOTE: we don't repeat the blk_size check for each new device.
2991 * Stacking drivers are expected to know what they are doing.
2994 char b
[BDEVNAME_SIZE
];
2996 q
= bdev_get_queue(bio
->bi_bdev
);
2999 "generic_make_request: Trying to access "
3000 "nonexistent block-device %s (%Lu)\n",
3001 bdevname(bio
->bi_bdev
, b
),
3002 (long long) bio
->bi_sector
);
3004 bio_endio(bio
, bio
->bi_size
, -EIO
);
3008 if (unlikely(bio_sectors(bio
) > q
->max_hw_sectors
)) {
3009 printk("bio too big device %s (%u > %u)\n",
3010 bdevname(bio
->bi_bdev
, b
),
3016 if (unlikely(test_bit(QUEUE_FLAG_DEAD
, &q
->queue_flags
)))
3020 * If this device has partitions, remap block n
3021 * of partition p to block n+start(p) of the disk.
3023 blk_partition_remap(bio
);
3025 ret
= q
->make_request_fn(q
, bio
);
3029 EXPORT_SYMBOL(generic_make_request
);
3032 * submit_bio: submit a bio to the block device layer for I/O
3033 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
3034 * @bio: The &struct bio which describes the I/O
3036 * submit_bio() is very similar in purpose to generic_make_request(), and
3037 * uses that function to do most of the work. Both are fairly rough
3038 * interfaces, @bio must be presetup and ready for I/O.
3041 void submit_bio(int rw
, struct bio
*bio
)
3043 int count
= bio_sectors(bio
);
3045 BIO_BUG_ON(!bio
->bi_size
);
3046 BIO_BUG_ON(!bio
->bi_io_vec
);
3049 mod_page_state(pgpgout
, count
);
3051 mod_page_state(pgpgin
, count
);
3053 if (unlikely(block_dump
)) {
3054 char b
[BDEVNAME_SIZE
];
3055 printk(KERN_DEBUG
"%s(%d): %s block %Lu on %s\n",
3056 current
->comm
, current
->pid
,
3057 (rw
& WRITE
) ? "WRITE" : "READ",
3058 (unsigned long long)bio
->bi_sector
,
3059 bdevname(bio
->bi_bdev
,b
));
3062 generic_make_request(bio
);
3065 EXPORT_SYMBOL(submit_bio
);
3067 static void blk_recalc_rq_segments(struct request
*rq
)
3069 struct bio
*bio
, *prevbio
= NULL
;
3070 int nr_phys_segs
, nr_hw_segs
;
3071 unsigned int phys_size
, hw_size
;
3072 request_queue_t
*q
= rq
->q
;
3077 phys_size
= hw_size
= nr_phys_segs
= nr_hw_segs
= 0;
3078 rq_for_each_bio(bio
, rq
) {
3079 /* Force bio hw/phys segs to be recalculated. */
3080 bio
->bi_flags
&= ~(1 << BIO_SEG_VALID
);
3082 nr_phys_segs
+= bio_phys_segments(q
, bio
);
3083 nr_hw_segs
+= bio_hw_segments(q
, bio
);
3085 int pseg
= phys_size
+ prevbio
->bi_size
+ bio
->bi_size
;
3086 int hseg
= hw_size
+ prevbio
->bi_size
+ bio
->bi_size
;
3088 if (blk_phys_contig_segment(q
, prevbio
, bio
) &&
3089 pseg
<= q
->max_segment_size
) {
3091 phys_size
+= prevbio
->bi_size
+ bio
->bi_size
;
3095 if (blk_hw_contig_segment(q
, prevbio
, bio
) &&
3096 hseg
<= q
->max_segment_size
) {
3098 hw_size
+= prevbio
->bi_size
+ bio
->bi_size
;
3105 rq
->nr_phys_segments
= nr_phys_segs
;
3106 rq
->nr_hw_segments
= nr_hw_segs
;
3109 static void blk_recalc_rq_sectors(struct request
*rq
, int nsect
)
3111 if (blk_fs_request(rq
)) {
3112 rq
->hard_sector
+= nsect
;
3113 rq
->hard_nr_sectors
-= nsect
;
3116 * Move the I/O submission pointers ahead if required.
3118 if ((rq
->nr_sectors
>= rq
->hard_nr_sectors
) &&
3119 (rq
->sector
<= rq
->hard_sector
)) {
3120 rq
->sector
= rq
->hard_sector
;
3121 rq
->nr_sectors
= rq
->hard_nr_sectors
;
3122 rq
->hard_cur_sectors
= bio_cur_sectors(rq
->bio
);
3123 rq
->current_nr_sectors
= rq
->hard_cur_sectors
;
3124 rq
->buffer
= bio_data(rq
->bio
);
3128 * if total number of sectors is less than the first segment
3129 * size, something has gone terribly wrong
3131 if (rq
->nr_sectors
< rq
->current_nr_sectors
) {
3132 printk("blk: request botched\n");
3133 rq
->nr_sectors
= rq
->current_nr_sectors
;
3138 static int __end_that_request_first(struct request
*req
, int uptodate
,
3141 int total_bytes
, bio_nbytes
, error
, next_idx
= 0;
3145 * extend uptodate bool to allow < 0 value to be direct io error
3148 if (end_io_error(uptodate
))
3149 error
= !uptodate
? -EIO
: uptodate
;
3152 * for a REQ_BLOCK_PC request, we want to carry any eventual
3153 * sense key with us all the way through
3155 if (!blk_pc_request(req
))
3159 if (blk_fs_request(req
) && !(req
->flags
& REQ_QUIET
))
3160 printk("end_request: I/O error, dev %s, sector %llu\n",
3161 req
->rq_disk
? req
->rq_disk
->disk_name
: "?",
3162 (unsigned long long)req
->sector
);
3165 if (blk_fs_request(req
) && req
->rq_disk
) {
3166 const int rw
= rq_data_dir(req
);
3168 __disk_stat_add(req
->rq_disk
, sectors
[rw
], nr_bytes
>> 9);
3171 total_bytes
= bio_nbytes
= 0;
3172 while ((bio
= req
->bio
) != NULL
) {
3175 if (nr_bytes
>= bio
->bi_size
) {
3176 req
->bio
= bio
->bi_next
;
3177 nbytes
= bio
->bi_size
;
3178 if (!ordered_bio_endio(req
, bio
, nbytes
, error
))
3179 bio_endio(bio
, nbytes
, error
);
3183 int idx
= bio
->bi_idx
+ next_idx
;
3185 if (unlikely(bio
->bi_idx
>= bio
->bi_vcnt
)) {
3186 blk_dump_rq_flags(req
, "__end_that");
3187 printk("%s: bio idx %d >= vcnt %d\n",
3189 bio
->bi_idx
, bio
->bi_vcnt
);
3193 nbytes
= bio_iovec_idx(bio
, idx
)->bv_len
;
3194 BIO_BUG_ON(nbytes
> bio
->bi_size
);
3197 * not a complete bvec done
3199 if (unlikely(nbytes
> nr_bytes
)) {
3200 bio_nbytes
+= nr_bytes
;
3201 total_bytes
+= nr_bytes
;
3206 * advance to the next vector
3209 bio_nbytes
+= nbytes
;
3212 total_bytes
+= nbytes
;
3215 if ((bio
= req
->bio
)) {
3217 * end more in this run, or just return 'not-done'
3219 if (unlikely(nr_bytes
<= 0))
3231 * if the request wasn't completed, update state
3234 if (!ordered_bio_endio(req
, bio
, bio_nbytes
, error
))
3235 bio_endio(bio
, bio_nbytes
, error
);
3236 bio
->bi_idx
+= next_idx
;
3237 bio_iovec(bio
)->bv_offset
+= nr_bytes
;
3238 bio_iovec(bio
)->bv_len
-= nr_bytes
;
3241 blk_recalc_rq_sectors(req
, total_bytes
>> 9);
3242 blk_recalc_rq_segments(req
);
3247 * end_that_request_first - end I/O on a request
3248 * @req: the request being processed
3249 * @uptodate: 1 for success, 0 for I/O error, < 0 for specific error
3250 * @nr_sectors: number of sectors to end I/O on
3253 * Ends I/O on a number of sectors attached to @req, and sets it up
3254 * for the next range of segments (if any) in the cluster.
3257 * 0 - we are done with this request, call end_that_request_last()
3258 * 1 - still buffers pending for this request
3260 int end_that_request_first(struct request
*req
, int uptodate
, int nr_sectors
)
3262 return __end_that_request_first(req
, uptodate
, nr_sectors
<< 9);
3265 EXPORT_SYMBOL(end_that_request_first
);
3268 * end_that_request_chunk - end I/O on a request
3269 * @req: the request being processed
3270 * @uptodate: 1 for success, 0 for I/O error, < 0 for specific error
3271 * @nr_bytes: number of bytes to complete
3274 * Ends I/O on a number of bytes attached to @req, and sets it up
3275 * for the next range of segments (if any). Like end_that_request_first(),
3276 * but deals with bytes instead of sectors.
3279 * 0 - we are done with this request, call end_that_request_last()
3280 * 1 - still buffers pending for this request
3282 int end_that_request_chunk(struct request
*req
, int uptodate
, int nr_bytes
)
3284 return __end_that_request_first(req
, uptodate
, nr_bytes
);
3287 EXPORT_SYMBOL(end_that_request_chunk
);
3290 * queue lock must be held
3292 void end_that_request_last(struct request
*req
, int uptodate
)
3294 struct gendisk
*disk
= req
->rq_disk
;
3298 * extend uptodate bool to allow < 0 value to be direct io error
3301 if (end_io_error(uptodate
))
3302 error
= !uptodate
? -EIO
: uptodate
;
3304 if (unlikely(laptop_mode
) && blk_fs_request(req
))
3305 laptop_io_completion();
3307 if (disk
&& blk_fs_request(req
)) {
3308 unsigned long duration
= jiffies
- req
->start_time
;
3309 const int rw
= rq_data_dir(req
);
3311 __disk_stat_inc(disk
, ios
[rw
]);
3312 __disk_stat_add(disk
, ticks
[rw
], duration
);
3313 disk_round_stats(disk
);
3317 req
->end_io(req
, error
);
3319 __blk_put_request(req
->q
, req
);
3322 EXPORT_SYMBOL(end_that_request_last
);
3324 void end_request(struct request
*req
, int uptodate
)
3326 if (!end_that_request_first(req
, uptodate
, req
->hard_cur_sectors
)) {
3327 add_disk_randomness(req
->rq_disk
);
3328 blkdev_dequeue_request(req
);
3329 end_that_request_last(req
, uptodate
);
3333 EXPORT_SYMBOL(end_request
);
3335 void blk_rq_bio_prep(request_queue_t
*q
, struct request
*rq
, struct bio
*bio
)
3337 /* first three bits are identical in rq->flags and bio->bi_rw */
3338 rq
->flags
|= (bio
->bi_rw
& 7);
3340 rq
->nr_phys_segments
= bio_phys_segments(q
, bio
);
3341 rq
->nr_hw_segments
= bio_hw_segments(q
, bio
);
3342 rq
->current_nr_sectors
= bio_cur_sectors(bio
);
3343 rq
->hard_cur_sectors
= rq
->current_nr_sectors
;
3344 rq
->hard_nr_sectors
= rq
->nr_sectors
= bio_sectors(bio
);
3345 rq
->buffer
= bio_data(bio
);
3347 rq
->bio
= rq
->biotail
= bio
;
3350 EXPORT_SYMBOL(blk_rq_bio_prep
);
3352 int kblockd_schedule_work(struct work_struct
*work
)
3354 return queue_work(kblockd_workqueue
, work
);
3357 EXPORT_SYMBOL(kblockd_schedule_work
);
3359 void kblockd_flush(void)
3361 flush_workqueue(kblockd_workqueue
);
3363 EXPORT_SYMBOL(kblockd_flush
);
3365 int __init
blk_dev_init(void)
3367 kblockd_workqueue
= create_workqueue("kblockd");
3368 if (!kblockd_workqueue
)
3369 panic("Failed to create kblockd\n");
3371 request_cachep
= kmem_cache_create("blkdev_requests",
3372 sizeof(struct request
), 0, SLAB_PANIC
, NULL
, NULL
);
3374 requestq_cachep
= kmem_cache_create("blkdev_queue",
3375 sizeof(request_queue_t
), 0, SLAB_PANIC
, NULL
, NULL
);
3377 iocontext_cachep
= kmem_cache_create("blkdev_ioc",
3378 sizeof(struct io_context
), 0, SLAB_PANIC
, NULL
, NULL
);
3380 blk_max_low_pfn
= max_low_pfn
;
3381 blk_max_pfn
= max_pfn
;
3387 * IO Context helper functions
3389 void put_io_context(struct io_context
*ioc
)
3394 BUG_ON(atomic_read(&ioc
->refcount
) == 0);
3396 if (atomic_dec_and_test(&ioc
->refcount
)) {
3397 if (ioc
->aic
&& ioc
->aic
->dtor
)
3398 ioc
->aic
->dtor(ioc
->aic
);
3399 if (ioc
->cic
&& ioc
->cic
->dtor
)
3400 ioc
->cic
->dtor(ioc
->cic
);
3402 kmem_cache_free(iocontext_cachep
, ioc
);
3405 EXPORT_SYMBOL(put_io_context
);
3407 /* Called by the exitting task */
3408 void exit_io_context(void)
3410 unsigned long flags
;
3411 struct io_context
*ioc
;
3413 local_irq_save(flags
);
3415 ioc
= current
->io_context
;
3416 current
->io_context
= NULL
;
3418 task_unlock(current
);
3419 local_irq_restore(flags
);
3421 if (ioc
->aic
&& ioc
->aic
->exit
)
3422 ioc
->aic
->exit(ioc
->aic
);
3423 if (ioc
->cic
&& ioc
->cic
->exit
)
3424 ioc
->cic
->exit(ioc
->cic
);
3426 put_io_context(ioc
);
3430 * If the current task has no IO context then create one and initialise it.
3431 * Otherwise, return its existing IO context.
3433 * This returned IO context doesn't have a specifically elevated refcount,
3434 * but since the current task itself holds a reference, the context can be
3435 * used in general code, so long as it stays within `current` context.
3437 struct io_context
*current_io_context(gfp_t gfp_flags
)
3439 struct task_struct
*tsk
= current
;
3440 struct io_context
*ret
;
3442 ret
= tsk
->io_context
;
3446 ret
= kmem_cache_alloc(iocontext_cachep
, gfp_flags
);
3448 atomic_set(&ret
->refcount
, 1);
3449 ret
->task
= current
;
3450 ret
->set_ioprio
= NULL
;
3451 ret
->last_waited
= jiffies
; /* doesn't matter... */
3452 ret
->nr_batch_requests
= 0; /* because this is 0 */
3455 tsk
->io_context
= ret
;
3460 EXPORT_SYMBOL(current_io_context
);
3463 * If the current task has no IO context then create one and initialise it.
3464 * If it does have a context, take a ref on it.
3466 * This is always called in the context of the task which submitted the I/O.
3468 struct io_context
*get_io_context(gfp_t gfp_flags
)
3470 struct io_context
*ret
;
3471 ret
= current_io_context(gfp_flags
);
3473 atomic_inc(&ret
->refcount
);
3476 EXPORT_SYMBOL(get_io_context
);
3478 void copy_io_context(struct io_context
**pdst
, struct io_context
**psrc
)
3480 struct io_context
*src
= *psrc
;
3481 struct io_context
*dst
= *pdst
;
3484 BUG_ON(atomic_read(&src
->refcount
) == 0);
3485 atomic_inc(&src
->refcount
);
3486 put_io_context(dst
);
3490 EXPORT_SYMBOL(copy_io_context
);
3492 void swap_io_context(struct io_context
**ioc1
, struct io_context
**ioc2
)
3494 struct io_context
*temp
;
3499 EXPORT_SYMBOL(swap_io_context
);
3504 struct queue_sysfs_entry
{
3505 struct attribute attr
;
3506 ssize_t (*show
)(struct request_queue
*, char *);
3507 ssize_t (*store
)(struct request_queue
*, const char *, size_t);
3511 queue_var_show(unsigned int var
, char *page
)
3513 return sprintf(page
, "%d\n", var
);
3517 queue_var_store(unsigned long *var
, const char *page
, size_t count
)
3519 char *p
= (char *) page
;
3521 *var
= simple_strtoul(p
, &p
, 10);
3525 static ssize_t
queue_requests_show(struct request_queue
*q
, char *page
)
3527 return queue_var_show(q
->nr_requests
, (page
));
3531 queue_requests_store(struct request_queue
*q
, const char *page
, size_t count
)
3533 struct request_list
*rl
= &q
->rq
;
3535 int ret
= queue_var_store(&q
->nr_requests
, page
, count
);
3536 if (q
->nr_requests
< BLKDEV_MIN_RQ
)
3537 q
->nr_requests
= BLKDEV_MIN_RQ
;
3538 blk_queue_congestion_threshold(q
);
3540 if (rl
->count
[READ
] >= queue_congestion_on_threshold(q
))
3541 set_queue_congested(q
, READ
);
3542 else if (rl
->count
[READ
] < queue_congestion_off_threshold(q
))
3543 clear_queue_congested(q
, READ
);
3545 if (rl
->count
[WRITE
] >= queue_congestion_on_threshold(q
))
3546 set_queue_congested(q
, WRITE
);
3547 else if (rl
->count
[WRITE
] < queue_congestion_off_threshold(q
))
3548 clear_queue_congested(q
, WRITE
);
3550 if (rl
->count
[READ
] >= q
->nr_requests
) {
3551 blk_set_queue_full(q
, READ
);
3552 } else if (rl
->count
[READ
]+1 <= q
->nr_requests
) {
3553 blk_clear_queue_full(q
, READ
);
3554 wake_up(&rl
->wait
[READ
]);
3557 if (rl
->count
[WRITE
] >= q
->nr_requests
) {
3558 blk_set_queue_full(q
, WRITE
);
3559 } else if (rl
->count
[WRITE
]+1 <= q
->nr_requests
) {
3560 blk_clear_queue_full(q
, WRITE
);
3561 wake_up(&rl
->wait
[WRITE
]);
3566 static ssize_t
queue_ra_show(struct request_queue
*q
, char *page
)
3568 int ra_kb
= q
->backing_dev_info
.ra_pages
<< (PAGE_CACHE_SHIFT
- 10);
3570 return queue_var_show(ra_kb
, (page
));
3574 queue_ra_store(struct request_queue
*q
, const char *page
, size_t count
)
3576 unsigned long ra_kb
;
3577 ssize_t ret
= queue_var_store(&ra_kb
, page
, count
);
3579 spin_lock_irq(q
->queue_lock
);
3580 if (ra_kb
> (q
->max_sectors
>> 1))
3581 ra_kb
= (q
->max_sectors
>> 1);
3583 q
->backing_dev_info
.ra_pages
= ra_kb
>> (PAGE_CACHE_SHIFT
- 10);
3584 spin_unlock_irq(q
->queue_lock
);
3589 static ssize_t
queue_max_sectors_show(struct request_queue
*q
, char *page
)
3591 int max_sectors_kb
= q
->max_sectors
>> 1;
3593 return queue_var_show(max_sectors_kb
, (page
));
3597 queue_max_sectors_store(struct request_queue
*q
, const char *page
, size_t count
)
3599 unsigned long max_sectors_kb
,
3600 max_hw_sectors_kb
= q
->max_hw_sectors
>> 1,
3601 page_kb
= 1 << (PAGE_CACHE_SHIFT
- 10);
3602 ssize_t ret
= queue_var_store(&max_sectors_kb
, page
, count
);
3605 if (max_sectors_kb
> max_hw_sectors_kb
|| max_sectors_kb
< page_kb
)
3608 * Take the queue lock to update the readahead and max_sectors
3609 * values synchronously:
3611 spin_lock_irq(q
->queue_lock
);
3613 * Trim readahead window as well, if necessary:
3615 ra_kb
= q
->backing_dev_info
.ra_pages
<< (PAGE_CACHE_SHIFT
- 10);
3616 if (ra_kb
> max_sectors_kb
)
3617 q
->backing_dev_info
.ra_pages
=
3618 max_sectors_kb
>> (PAGE_CACHE_SHIFT
- 10);
3620 q
->max_sectors
= max_sectors_kb
<< 1;
3621 spin_unlock_irq(q
->queue_lock
);
3626 static ssize_t
queue_max_hw_sectors_show(struct request_queue
*q
, char *page
)
3628 int max_hw_sectors_kb
= q
->max_hw_sectors
>> 1;
3630 return queue_var_show(max_hw_sectors_kb
, (page
));
3634 static struct queue_sysfs_entry queue_requests_entry
= {
3635 .attr
= {.name
= "nr_requests", .mode
= S_IRUGO
| S_IWUSR
},
3636 .show
= queue_requests_show
,
3637 .store
= queue_requests_store
,
3640 static struct queue_sysfs_entry queue_ra_entry
= {
3641 .attr
= {.name
= "read_ahead_kb", .mode
= S_IRUGO
| S_IWUSR
},
3642 .show
= queue_ra_show
,
3643 .store
= queue_ra_store
,
3646 static struct queue_sysfs_entry queue_max_sectors_entry
= {
3647 .attr
= {.name
= "max_sectors_kb", .mode
= S_IRUGO
| S_IWUSR
},
3648 .show
= queue_max_sectors_show
,
3649 .store
= queue_max_sectors_store
,
3652 static struct queue_sysfs_entry queue_max_hw_sectors_entry
= {
3653 .attr
= {.name
= "max_hw_sectors_kb", .mode
= S_IRUGO
},
3654 .show
= queue_max_hw_sectors_show
,
3657 static struct queue_sysfs_entry queue_iosched_entry
= {
3658 .attr
= {.name
= "scheduler", .mode
= S_IRUGO
| S_IWUSR
},
3659 .show
= elv_iosched_show
,
3660 .store
= elv_iosched_store
,
3663 static struct attribute
*default_attrs
[] = {
3664 &queue_requests_entry
.attr
,
3665 &queue_ra_entry
.attr
,
3666 &queue_max_hw_sectors_entry
.attr
,
3667 &queue_max_sectors_entry
.attr
,
3668 &queue_iosched_entry
.attr
,
3672 #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
3675 queue_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
3677 struct queue_sysfs_entry
*entry
= to_queue(attr
);
3678 struct request_queue
*q
;
3680 q
= container_of(kobj
, struct request_queue
, kobj
);
3684 return entry
->show(q
, page
);
3688 queue_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
3689 const char *page
, size_t length
)
3691 struct queue_sysfs_entry
*entry
= to_queue(attr
);
3692 struct request_queue
*q
;
3694 q
= container_of(kobj
, struct request_queue
, kobj
);
3698 return entry
->store(q
, page
, length
);
3701 static struct sysfs_ops queue_sysfs_ops
= {
3702 .show
= queue_attr_show
,
3703 .store
= queue_attr_store
,
3706 static struct kobj_type queue_ktype
= {
3707 .sysfs_ops
= &queue_sysfs_ops
,
3708 .default_attrs
= default_attrs
,
3711 int blk_register_queue(struct gendisk
*disk
)
3715 request_queue_t
*q
= disk
->queue
;
3717 if (!q
|| !q
->request_fn
)
3720 q
->kobj
.parent
= kobject_get(&disk
->kobj
);
3721 if (!q
->kobj
.parent
)
3724 snprintf(q
->kobj
.name
, KOBJ_NAME_LEN
, "%s", "queue");
3725 q
->kobj
.ktype
= &queue_ktype
;
3727 ret
= kobject_register(&q
->kobj
);
3731 ret
= elv_register_queue(q
);
3733 kobject_unregister(&q
->kobj
);
3740 void blk_unregister_queue(struct gendisk
*disk
)
3742 request_queue_t
*q
= disk
->queue
;
3744 if (q
&& q
->request_fn
) {
3745 elv_unregister_queue(q
);
3747 kobject_unregister(&q
->kobj
);
3748 kobject_put(&disk
->kobj
);