2 * linux/drivers/block/ll_rw_blk.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 1994, Karl Keyte: Added support for disk statistics
6 * Elevator latency, (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
7 * Queue request tables / lock, selectable elevator, Jens Axboe <axboe@suse.de>
8 * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au> - July2000
9 * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
13 * This handles all read/write requests to block devices
15 #include <linux/config.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/backing-dev.h>
19 #include <linux/bio.h>
20 #include <linux/blkdev.h>
21 #include <linux/highmem.h>
23 #include <linux/kernel_stat.h>
24 #include <linux/string.h>
25 #include <linux/init.h>
26 #include <linux/bootmem.h> /* for max_pfn/max_low_pfn */
27 #include <linux/completion.h>
28 #include <linux/slab.h>
29 #include <linux/swap.h>
31 static void blk_unplug_work(void *data
);
32 static void blk_unplug_timeout(unsigned long data
);
35 * For the allocated request tables
37 static kmem_cache_t
*request_cachep
;
42 static LIST_HEAD(blk_plug_list
);
43 static spinlock_t blk_plug_lock __cacheline_aligned_in_smp
= SPIN_LOCK_UNLOCKED
;
45 static wait_queue_head_t congestion_wqh
[2];
48 * Controlling structure to kblockd
50 static struct workqueue_struct
*kblockd_workqueue
;
52 unsigned long blk_max_low_pfn
, blk_max_pfn
;
54 /* Amount of time in which a process may batch requests */
55 #define BLK_BATCH_TIME (HZ/50UL)
57 /* Number of requests a "batching" process may submit */
58 #define BLK_BATCH_REQ 32
61 * Return the threshold (number of used requests) at which the queue is
62 * considered to be congested. It include a little hysteresis to keep the
63 * context switch rate down.
65 static inline int queue_congestion_on_threshold(struct request_queue
*q
)
69 ret
= q
->nr_requests
- (q
->nr_requests
/ 8) + 1;
71 if (ret
> q
->nr_requests
)
78 * The threshold at which a queue is considered to be uncongested
80 static inline int queue_congestion_off_threshold(struct request_queue
*q
)
84 ret
= q
->nr_requests
- (q
->nr_requests
/ 8) - 1;
93 * A queue has just exitted congestion. Note this in the global counter of
94 * congested queues, and wake up anyone who was waiting for requests to be
97 static void clear_queue_congested(request_queue_t
*q
, int rw
)
100 wait_queue_head_t
*wqh
= &congestion_wqh
[rw
];
102 bit
= (rw
== WRITE
) ? BDI_write_congested
: BDI_read_congested
;
103 clear_bit(bit
, &q
->backing_dev_info
.state
);
104 if (waitqueue_active(wqh
))
109 * A queue has just entered congestion. Flag that in the queue's VM-visible
110 * state flags and increment the global gounter of congested queues.
112 static void set_queue_congested(request_queue_t
*q
, int rw
)
116 bit
= (rw
== WRITE
) ? BDI_write_congested
: BDI_read_congested
;
117 set_bit(bit
, &q
->backing_dev_info
.state
);
121 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
124 * Locates the passed device's request queue and returns the address of its
127 * Will return NULL if the request queue cannot be located.
129 struct backing_dev_info
*blk_get_backing_dev_info(struct block_device
*bdev
)
131 struct backing_dev_info
*ret
= NULL
;
132 request_queue_t
*q
= bdev_get_queue(bdev
);
135 ret
= &q
->backing_dev_info
;
140 * blk_queue_prep_rq - set a prepare_request function for queue
142 * @pfn: prepare_request function
144 * It's possible for a queue to register a prepare_request callback which
145 * is invoked before the request is handed to the request_fn. The goal of
146 * the function is to prepare a request for I/O, it can be used to build a
147 * cdb from the request data for instance.
150 void blk_queue_prep_rq(request_queue_t
*q
, prep_rq_fn
*pfn
)
156 * blk_queue_merge_bvec - set a merge_bvec function for queue
158 * @mbfn: merge_bvec_fn
160 * Usually queues have static limitations on the max sectors or segments that
161 * we can put in a request. Stacking drivers may have some settings that
162 * are dynamic, and thus we have to query the queue whether it is ok to
163 * add a new bio_vec to a bio at a given offset or not. If the block device
164 * has such limitations, it needs to register a merge_bvec_fn to control
165 * the size of bio's sent to it. Per default now merge_bvec_fn is defined for
166 * a queue, and only the fixed limits are honored.
169 void blk_queue_merge_bvec(request_queue_t
*q
, merge_bvec_fn
*mbfn
)
171 q
->merge_bvec_fn
= mbfn
;
175 * blk_queue_make_request - define an alternate make_request function for a device
176 * @q: the request queue for the device to be affected
177 * @mfn: the alternate make_request function
180 * The normal way for &struct bios to be passed to a device
181 * driver is for them to be collected into requests on a request
182 * queue, and then to allow the device driver to select requests
183 * off that queue when it is ready. This works well for many block
184 * devices. However some block devices (typically virtual devices
185 * such as md or lvm) do not benefit from the processing on the
186 * request queue, and are served best by having the requests passed
187 * directly to them. This can be achieved by providing a function
188 * to blk_queue_make_request().
191 * The driver that does this *must* be able to deal appropriately
192 * with buffers in "highmemory". This can be accomplished by either calling
193 * __bio_kmap_atomic() to get a temporary kernel mapping, or by calling
194 * blk_queue_bounce() to create a buffer in normal memory.
196 void blk_queue_make_request(request_queue_t
* q
, make_request_fn
* mfn
)
201 q
->nr_requests
= BLKDEV_MAX_RQ
;
202 q
->max_phys_segments
= MAX_PHYS_SEGMENTS
;
203 q
->max_hw_segments
= MAX_HW_SEGMENTS
;
204 q
->make_request_fn
= mfn
;
205 q
->backing_dev_info
.ra_pages
= (VM_MAX_READAHEAD
* 1024) / PAGE_CACHE_SIZE
;
206 q
->backing_dev_info
.state
= 0;
207 q
->backing_dev_info
.memory_backed
= 0;
208 blk_queue_max_sectors(q
, MAX_SECTORS
);
209 blk_queue_hardsect_size(q
, 512);
210 blk_queue_dma_alignment(q
, 511);
212 q
->unplug_thresh
= 4; /* hmm */
213 q
->unplug_delay
= (3 * HZ
) / 1000; /* 3 milliseconds */
214 if (q
->unplug_delay
== 0)
217 init_timer(&q
->unplug_timer
);
218 INIT_WORK(&q
->unplug_work
, blk_unplug_work
, q
);
220 q
->unplug_timer
.function
= blk_unplug_timeout
;
221 q
->unplug_timer
.data
= (unsigned long)q
;
224 * by default assume old behaviour and bounce for any highmem page
226 blk_queue_bounce_limit(q
, BLK_BOUNCE_HIGH
);
228 init_waitqueue_head(&q
->queue_wait
);
229 INIT_LIST_HEAD(&q
->plug_list
);
233 * blk_queue_bounce_limit - set bounce buffer limit for queue
234 * @q: the request queue for the device
235 * @dma_addr: bus address limit
238 * Different hardware can have different requirements as to what pages
239 * it can do I/O directly to. A low level driver can call
240 * blk_queue_bounce_limit to have lower memory pages allocated as bounce
241 * buffers for doing I/O to pages residing above @page. By default
242 * the block layer sets this to the highest numbered "low" memory page.
244 void blk_queue_bounce_limit(request_queue_t
*q
, u64 dma_addr
)
246 unsigned long bounce_pfn
= dma_addr
>> PAGE_SHIFT
;
247 unsigned long mb
= dma_addr
>> 20;
248 static request_queue_t
*last_q
;
251 * set appropriate bounce gfp mask -- unfortunately we don't have a
252 * full 4GB zone, so we have to resort to low memory for any bounces.
253 * ISA has its own < 16MB zone.
255 if (bounce_pfn
< blk_max_low_pfn
) {
256 BUG_ON(dma_addr
< BLK_BOUNCE_ISA
);
257 init_emergency_isa_pool();
258 q
->bounce_gfp
= GFP_NOIO
| GFP_DMA
;
260 q
->bounce_gfp
= GFP_NOIO
;
263 * keep this for debugging for now...
265 if (dma_addr
!= BLK_BOUNCE_HIGH
&& q
!= last_q
) {
266 printk("blk: queue %p, ", q
);
267 if (dma_addr
== BLK_BOUNCE_ANY
)
268 printk("no I/O memory limit\n");
270 printk("I/O limit %luMb (mask 0x%Lx)\n", mb
, (long long) dma_addr
);
273 q
->bounce_pfn
= bounce_pfn
;
279 * blk_queue_max_sectors - set max sectors for a request for this queue
280 * @q: the request queue for the device
281 * @max_sectors: max sectors in the usual 512b unit
284 * Enables a low level driver to set an upper limit on the size of
287 void blk_queue_max_sectors(request_queue_t
*q
, unsigned short max_sectors
)
289 if ((max_sectors
<< 9) < PAGE_CACHE_SIZE
) {
290 max_sectors
= 1 << (PAGE_CACHE_SHIFT
- 9);
291 printk("%s: set to minimum %d\n", __FUNCTION__
, max_sectors
);
294 q
->max_sectors
= max_sectors
;
298 * blk_queue_max_phys_segments - set max phys segments for a request for this queue
299 * @q: the request queue for the device
300 * @max_segments: max number of segments
303 * Enables a low level driver to set an upper limit on the number of
304 * physical data segments in a request. This would be the largest sized
305 * scatter list the driver could handle.
307 void blk_queue_max_phys_segments(request_queue_t
*q
, unsigned short max_segments
)
311 printk("%s: set to minimum %d\n", __FUNCTION__
, max_segments
);
314 q
->max_phys_segments
= max_segments
;
318 * blk_queue_max_hw_segments - set max hw segments for a request for this queue
319 * @q: the request queue for the device
320 * @max_segments: max number of segments
323 * Enables a low level driver to set an upper limit on the number of
324 * hw data segments in a request. This would be the largest number of
325 * address/length pairs the host adapter can actually give as once
328 void blk_queue_max_hw_segments(request_queue_t
*q
, unsigned short max_segments
)
332 printk("%s: set to minimum %d\n", __FUNCTION__
, max_segments
);
335 q
->max_hw_segments
= max_segments
;
339 * blk_queue_max_segment_size - set max segment size for blk_rq_map_sg
340 * @q: the request queue for the device
341 * @max_size: max size of segment in bytes
344 * Enables a low level driver to set an upper limit on the size of a
347 void blk_queue_max_segment_size(request_queue_t
*q
, unsigned int max_size
)
349 if (max_size
< PAGE_CACHE_SIZE
) {
350 max_size
= PAGE_CACHE_SIZE
;
351 printk("%s: set to minimum %d\n", __FUNCTION__
, max_size
);
354 q
->max_segment_size
= max_size
;
358 * blk_queue_hardsect_size - set hardware sector size for the queue
359 * @q: the request queue for the device
360 * @size: the hardware sector size, in bytes
363 * This should typically be set to the lowest possible sector size
364 * that the hardware can operate on (possible without reverting to
365 * even internal read-modify-write operations). Usually the default
366 * of 512 covers most hardware.
368 void blk_queue_hardsect_size(request_queue_t
*q
, unsigned short size
)
370 q
->hardsect_size
= size
;
374 * blk_queue_segment_boundary - set boundary rules for segment merging
375 * @q: the request queue for the device
376 * @mask: the memory boundary mask
378 void blk_queue_segment_boundary(request_queue_t
*q
, unsigned long mask
)
380 if (mask
< PAGE_CACHE_SIZE
- 1) {
381 mask
= PAGE_CACHE_SIZE
- 1;
382 printk("%s: set to minimum %lx\n", __FUNCTION__
, mask
);
385 q
->seg_boundary_mask
= mask
;
389 * blk_queue_dma_alignment - set dma length and memory alignment
390 * @q: the request queue for the device
391 * @dma_mask: alignment mask
394 * set required memory and length aligment for direct dma transactions.
395 * this is used when buiding direct io requests for the queue.
398 void blk_queue_dma_alignment(request_queue_t
*q
, int mask
)
400 q
->dma_alignment
= mask
;
404 * blk_queue_find_tag - find a request by its tag and queue
406 * @q: The request queue for the device
407 * @tag: The tag of the request
410 * Should be used when a device returns a tag and you want to match
413 * no locks need be held.
415 struct request
*blk_queue_find_tag(request_queue_t
*q
, int tag
)
417 struct blk_queue_tag
*bqt
= q
->queue_tags
;
419 if (unlikely(bqt
== NULL
|| tag
>= bqt
->real_max_depth
))
422 return bqt
->tag_index
[tag
];
426 * blk_queue_free_tags - release tag maintenance info
427 * @q: the request queue for the device
430 * blk_cleanup_queue() will take care of calling this function, if tagging
431 * has been used. So there's usually no need to call this directly, unless
432 * tagging is just being disabled but the queue remains in function.
434 void blk_queue_free_tags(request_queue_t
*q
)
436 struct blk_queue_tag
*bqt
= q
->queue_tags
;
442 BUG_ON(!list_empty(&bqt
->busy_list
));
444 kfree(bqt
->tag_index
);
445 bqt
->tag_index
= NULL
;
451 q
->queue_tags
= NULL
;
452 q
->queue_flags
&= ~(1 << QUEUE_FLAG_QUEUED
);
456 init_tag_map(request_queue_t
*q
, struct blk_queue_tag
*tags
, int depth
)
460 if (depth
> q
->nr_requests
* 2) {
461 depth
= q
->nr_requests
* 2;
462 printk(KERN_ERR
"%s: adjusted depth to %d\n",
463 __FUNCTION__
, depth
);
466 tags
->tag_index
= kmalloc(depth
* sizeof(struct request
*), GFP_ATOMIC
);
467 if (!tags
->tag_index
)
470 bits
= (depth
/ BLK_TAGS_PER_LONG
) + 1;
471 tags
->tag_map
= kmalloc(bits
* sizeof(unsigned long), GFP_ATOMIC
);
475 memset(tags
->tag_index
, 0, depth
* sizeof(struct request
*));
476 memset(tags
->tag_map
, 0, bits
* sizeof(unsigned long));
477 tags
->max_depth
= depth
;
478 tags
->real_max_depth
= bits
* BITS_PER_LONG
;
481 * set the upper bits if the depth isn't a multiple of the word size
483 for (i
= depth
; i
< bits
* BLK_TAGS_PER_LONG
; i
++)
484 __set_bit(i
, tags
->tag_map
);
488 kfree(tags
->tag_index
);
493 * blk_queue_init_tags - initialize the queue tag info
494 * @q: the request queue for the device
495 * @depth: the maximum queue depth supported
497 int blk_queue_init_tags(request_queue_t
*q
, int depth
)
499 struct blk_queue_tag
*tags
;
501 tags
= kmalloc(sizeof(struct blk_queue_tag
),GFP_ATOMIC
);
505 if (init_tag_map(q
, tags
, depth
))
508 INIT_LIST_HEAD(&tags
->busy_list
);
512 * assign it, all done
514 q
->queue_tags
= tags
;
515 q
->queue_flags
|= (1 << QUEUE_FLAG_QUEUED
);
523 * blk_queue_resize_tags - change the queueing depth
524 * @q: the request queue for the device
525 * @new_depth: the new max command queueing depth
528 * Must be called with the queue lock held.
530 int blk_queue_resize_tags(request_queue_t
*q
, int new_depth
)
532 struct blk_queue_tag
*bqt
= q
->queue_tags
;
533 struct request
**tag_index
;
534 unsigned long *tag_map
;
541 * don't bother sizing down
543 if (new_depth
<= bqt
->real_max_depth
) {
544 bqt
->max_depth
= new_depth
;
549 * save the old state info, so we can copy it back
551 tag_index
= bqt
->tag_index
;
552 tag_map
= bqt
->tag_map
;
553 max_depth
= bqt
->real_max_depth
;
555 if (init_tag_map(q
, bqt
, new_depth
))
558 memcpy(bqt
->tag_index
, tag_index
, max_depth
* sizeof(struct request
*));
559 bits
= max_depth
/ BLK_TAGS_PER_LONG
;
560 memcpy(bqt
->tag_map
, tag_map
, bits
* sizeof(unsigned long));
568 * blk_queue_end_tag - end tag operations for a request
569 * @q: the request queue for the device
570 * @tag: the tag that has completed
573 * Typically called when end_that_request_first() returns 0, meaning
574 * all transfers have been done for a request. It's important to call
575 * this function before end_that_request_last(), as that will put the
576 * request back on the free list thus corrupting the internal tag list.
579 * queue lock must be held.
581 void blk_queue_end_tag(request_queue_t
*q
, struct request
*rq
)
583 struct blk_queue_tag
*bqt
= q
->queue_tags
;
588 if (unlikely(tag
>= bqt
->real_max_depth
))
591 if (unlikely(!__test_and_clear_bit(tag
, bqt
->tag_map
))) {
592 printk("attempt to clear non-busy tag (%d)\n", tag
);
596 list_del_init(&rq
->queuelist
);
597 rq
->flags
&= ~REQ_QUEUED
;
600 if (unlikely(bqt
->tag_index
[tag
] == NULL
))
601 printk("tag %d is missing\n", tag
);
603 bqt
->tag_index
[tag
] = NULL
;
608 * blk_queue_start_tag - find a free tag and assign it
609 * @q: the request queue for the device
610 * @rq: the block request that needs tagging
613 * This can either be used as a stand-alone helper, or possibly be
614 * assigned as the queue &prep_rq_fn (in which case &struct request
615 * automagically gets a tag assigned). Note that this function
616 * assumes that any type of request can be queued! if this is not
617 * true for your device, you must check the request type before
618 * calling this function. The request will also be removed from
619 * the request queue, so it's the drivers responsibility to readd
620 * it if it should need to be restarted for some reason.
623 * queue lock must be held.
625 int blk_queue_start_tag(request_queue_t
*q
, struct request
*rq
)
627 struct blk_queue_tag
*bqt
= q
->queue_tags
;
628 unsigned long *map
= bqt
->tag_map
;
631 if (unlikely((rq
->flags
& REQ_QUEUED
))) {
633 "request %p for device [%s] already tagged %d",
634 rq
, rq
->rq_disk
? rq
->rq_disk
->disk_name
: "?", rq
->tag
);
638 for (map
= bqt
->tag_map
; *map
== -1UL; map
++) {
639 tag
+= BLK_TAGS_PER_LONG
;
641 if (tag
>= bqt
->max_depth
)
646 __set_bit(tag
, bqt
->tag_map
);
648 rq
->flags
|= REQ_QUEUED
;
650 bqt
->tag_index
[tag
] = rq
;
651 blkdev_dequeue_request(rq
);
652 list_add(&rq
->queuelist
, &bqt
->busy_list
);
658 * blk_queue_invalidate_tags - invalidate all pending tags
659 * @q: the request queue for the device
662 * Hardware conditions may dictate a need to stop all pending requests.
663 * In this case, we will safely clear the block side of the tag queue and
664 * readd all requests to the request queue in the right order.
667 * queue lock must be held.
669 void blk_queue_invalidate_tags(request_queue_t
*q
)
671 struct blk_queue_tag
*bqt
= q
->queue_tags
;
672 struct list_head
*tmp
, *n
;
675 list_for_each_safe(tmp
, n
, &bqt
->busy_list
) {
676 rq
= list_entry_rq(tmp
);
679 printk("bad tag found on list\n");
680 list_del_init(&rq
->queuelist
);
681 rq
->flags
&= ~REQ_QUEUED
;
683 blk_queue_end_tag(q
, rq
);
685 rq
->flags
&= ~REQ_STARTED
;
686 __elv_add_request(q
, rq
, 0, 0);
690 static char *rq_flags
[] = {
708 "REQ_DRIVE_TASKFILE",
715 void blk_dump_rq_flags(struct request
*rq
, char *msg
)
719 printk("%s: dev %s: flags = ", msg
,
720 rq
->rq_disk
? rq
->rq_disk
->disk_name
: "?");
723 if (rq
->flags
& (1 << bit
))
724 printk("%s ", rq_flags
[bit
]);
726 } while (bit
< __REQ_NR_BITS
);
728 printk("\nsector %llu, nr/cnr %lu/%u\n", (unsigned long long)rq
->sector
,
730 rq
->current_nr_sectors
);
731 printk("bio %p, biotail %p, buffer %p, data %p, len %u\n", rq
->bio
, rq
->biotail
, rq
->buffer
, rq
->data
, rq
->data_len
);
733 if (rq
->flags
& (REQ_BLOCK_PC
| REQ_PC
)) {
735 for (bit
= 0; bit
< sizeof(rq
->cmd
); bit
++)
736 printk("%02x ", rq
->cmd
[bit
]);
741 void blk_recount_segments(request_queue_t
*q
, struct bio
*bio
)
743 struct bio_vec
*bv
, *bvprv
= NULL
;
744 int i
, nr_phys_segs
, nr_hw_segs
, seg_size
, cluster
;
746 if (unlikely(!bio
->bi_io_vec
))
749 cluster
= q
->queue_flags
& (1 << QUEUE_FLAG_CLUSTER
);
750 seg_size
= nr_phys_segs
= nr_hw_segs
= 0;
751 bio_for_each_segment(bv
, bio
, i
) {
752 if (bvprv
&& cluster
) {
753 if (seg_size
+ bv
->bv_len
> q
->max_segment_size
)
755 if (!BIOVEC_PHYS_MERGEABLE(bvprv
, bv
))
757 if (!BIOVEC_SEG_BOUNDARY(q
, bvprv
, bv
))
760 seg_size
+= bv
->bv_len
;
765 if (!bvprv
|| !BIOVEC_VIRT_MERGEABLE(bvprv
, bv
))
770 seg_size
= bv
->bv_len
;
773 bio
->bi_phys_segments
= nr_phys_segs
;
774 bio
->bi_hw_segments
= nr_hw_segs
;
775 bio
->bi_flags
|= (1 << BIO_SEG_VALID
);
779 int blk_phys_contig_segment(request_queue_t
*q
, struct bio
*bio
,
782 if (!(q
->queue_flags
& (1 << QUEUE_FLAG_CLUSTER
)))
785 if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio
), __BVEC_START(nxt
)))
787 if (bio
->bi_size
+ nxt
->bi_size
> q
->max_segment_size
)
791 * bio and nxt are contigous in memory, check if the queue allows
792 * these two to be merged into one
794 if (BIO_SEG_BOUNDARY(q
, bio
, nxt
))
800 int blk_hw_contig_segment(request_queue_t
*q
, struct bio
*bio
,
803 if (!(q
->queue_flags
& (1 << QUEUE_FLAG_CLUSTER
)))
806 if (!BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio
), __BVEC_START(nxt
)))
808 if (bio
->bi_size
+ nxt
->bi_size
> q
->max_segment_size
)
812 * bio and nxt are contigous in memory, check if the queue allows
813 * these two to be merged into one
815 if (BIO_SEG_BOUNDARY(q
, bio
, nxt
))
822 * map a request to scatterlist, return number of sg entries setup. Caller
823 * must make sure sg can hold rq->nr_phys_segments entries
825 int blk_rq_map_sg(request_queue_t
*q
, struct request
*rq
, struct scatterlist
*sg
)
827 struct bio_vec
*bvec
, *bvprv
;
829 int nsegs
, i
, cluster
;
832 cluster
= q
->queue_flags
& (1 << QUEUE_FLAG_CLUSTER
);
838 rq_for_each_bio(bio
, rq
) {
840 * for each segment in bio
842 bio_for_each_segment(bvec
, bio
, i
) {
843 int nbytes
= bvec
->bv_len
;
845 if (bvprv
&& cluster
) {
846 if (sg
[nsegs
- 1].length
+ nbytes
> q
->max_segment_size
)
849 if (!BIOVEC_PHYS_MERGEABLE(bvprv
, bvec
))
851 if (!BIOVEC_SEG_BOUNDARY(q
, bvprv
, bvec
))
854 sg
[nsegs
- 1].length
+= nbytes
;
857 memset(&sg
[nsegs
],0,sizeof(struct scatterlist
));
858 sg
[nsegs
].page
= bvec
->bv_page
;
859 sg
[nsegs
].length
= nbytes
;
860 sg
[nsegs
].offset
= bvec
->bv_offset
;
865 } /* segments in bio */
872 * the standard queue merge functions, can be overridden with device
873 * specific ones if so desired
876 static inline int ll_new_mergeable(request_queue_t
*q
,
880 int nr_phys_segs
= bio_phys_segments(q
, bio
);
882 if (req
->nr_phys_segments
+ nr_phys_segs
> q
->max_phys_segments
) {
883 req
->flags
|= REQ_NOMERGE
;
884 q
->last_merge
= NULL
;
889 * A hw segment is just getting larger, bump just the phys
892 req
->nr_phys_segments
+= nr_phys_segs
;
896 static inline int ll_new_hw_segment(request_queue_t
*q
,
900 int nr_hw_segs
= bio_hw_segments(q
, bio
);
901 int nr_phys_segs
= bio_phys_segments(q
, bio
);
903 if (req
->nr_hw_segments
+ nr_hw_segs
> q
->max_hw_segments
904 || req
->nr_phys_segments
+ nr_phys_segs
> q
->max_phys_segments
) {
905 req
->flags
|= REQ_NOMERGE
;
906 q
->last_merge
= NULL
;
911 * This will form the start of a new hw segment. Bump both
914 req
->nr_hw_segments
+= nr_hw_segs
;
915 req
->nr_phys_segments
+= nr_phys_segs
;
919 static int ll_back_merge_fn(request_queue_t
*q
, struct request
*req
,
922 if (req
->nr_sectors
+ bio_sectors(bio
) > q
->max_sectors
) {
923 req
->flags
|= REQ_NOMERGE
;
924 q
->last_merge
= NULL
;
928 if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(req
->biotail
), __BVEC_START(bio
)))
929 return ll_new_mergeable(q
, req
, bio
);
931 return ll_new_hw_segment(q
, req
, bio
);
934 static int ll_front_merge_fn(request_queue_t
*q
, struct request
*req
,
937 if (req
->nr_sectors
+ bio_sectors(bio
) > q
->max_sectors
) {
938 req
->flags
|= REQ_NOMERGE
;
939 q
->last_merge
= NULL
;
943 if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio
), __BVEC_START(req
->bio
)))
944 return ll_new_mergeable(q
, req
, bio
);
946 return ll_new_hw_segment(q
, req
, bio
);
949 static int ll_merge_requests_fn(request_queue_t
*q
, struct request
*req
,
950 struct request
*next
)
952 int total_phys_segments
= req
->nr_phys_segments
+next
->nr_phys_segments
;
953 int total_hw_segments
= req
->nr_hw_segments
+ next
->nr_hw_segments
;
956 * First check if the either of the requests are re-queued
957 * requests. Can't merge them if they are.
959 if (req
->special
|| next
->special
)
963 * Will it become to large?
965 if ((req
->nr_sectors
+ next
->nr_sectors
) > q
->max_sectors
)
968 total_phys_segments
= req
->nr_phys_segments
+ next
->nr_phys_segments
;
969 if (blk_phys_contig_segment(q
, req
->biotail
, next
->bio
))
970 total_phys_segments
--;
972 if (total_phys_segments
> q
->max_phys_segments
)
975 total_hw_segments
= req
->nr_hw_segments
+ next
->nr_hw_segments
;
976 if (blk_hw_contig_segment(q
, req
->biotail
, next
->bio
))
979 if (total_hw_segments
> q
->max_hw_segments
)
983 req
->nr_phys_segments
= total_phys_segments
;
984 req
->nr_hw_segments
= total_hw_segments
;
989 * "plug" the device if there are no outstanding requests: this will
990 * force the transfer to start only after we have put all the requests
993 * This is called with interrupts off and no requests on the queue and
994 * with the queue lock held.
996 void blk_plug_device(request_queue_t
*q
)
998 WARN_ON(!irqs_disabled());
999 if (!blk_queue_plugged(q
)) {
1000 spin_lock(&blk_plug_lock
);
1001 list_add_tail(&q
->plug_list
, &blk_plug_list
);
1002 mod_timer(&q
->unplug_timer
, jiffies
+ q
->unplug_delay
);
1003 spin_unlock(&blk_plug_lock
);
1008 * remove the queue from the plugged list, if present. called with
1009 * queue lock held and interrupts disabled.
1011 int blk_remove_plug(request_queue_t
*q
)
1013 WARN_ON(!irqs_disabled());
1014 if (blk_queue_plugged(q
)) {
1015 spin_lock(&blk_plug_lock
);
1016 list_del_init(&q
->plug_list
);
1017 del_timer(&q
->unplug_timer
);
1018 spin_unlock(&blk_plug_lock
);
1026 * remove the plug and let it rip..
1028 static inline void __generic_unplug_device(request_queue_t
*q
)
1030 if (test_bit(QUEUE_FLAG_STOPPED
, &q
->queue_flags
))
1033 if (!blk_remove_plug(q
))
1036 del_timer(&q
->unplug_timer
);
1039 * was plugged, fire request_fn if queue has stuff to do
1041 if (elv_next_request(q
))
1046 * generic_unplug_device - fire a request queue
1047 * @data: The &request_queue_t in question
1050 * Linux uses plugging to build bigger requests queues before letting
1051 * the device have at them. If a queue is plugged, the I/O scheduler
1052 * is still adding and merging requests on the queue. Once the queue
1053 * gets unplugged (either by manually calling this function, or by
1054 * calling blk_run_queues()), the request_fn defined for the
1055 * queue is invoked and transfers started.
1057 void generic_unplug_device(void *data
)
1059 request_queue_t
*q
= data
;
1061 spin_lock_irq(q
->queue_lock
);
1062 __generic_unplug_device(q
);
1063 spin_unlock_irq(q
->queue_lock
);
1066 static void blk_unplug_work(void *data
)
1068 request_queue_t
*q
= data
;
1072 static void blk_unplug_timeout(unsigned long data
)
1074 request_queue_t
*q
= (request_queue_t
*)data
;
1076 kblockd_schedule_work(&q
->unplug_work
);
1080 * blk_start_queue - restart a previously stopped queue
1081 * @q: The &request_queue_t in question
1084 * blk_start_queue() will clear the stop flag on the queue, and call
1085 * the request_fn for the queue if it was in a stopped state when
1086 * entered. Also see blk_stop_queue(). Must not be called from driver
1087 * request function due to recursion issues. Queue lock must be held.
1089 void blk_start_queue(request_queue_t
*q
)
1091 clear_bit(QUEUE_FLAG_STOPPED
, &q
->queue_flags
);
1092 schedule_work(&q
->unplug_work
);
1096 * blk_stop_queue - stop a queue
1097 * @q: The &request_queue_t in question
1100 * The Linux block layer assumes that a block driver will consume all
1101 * entries on the request queue when the request_fn strategy is called.
1102 * Often this will not happen, because of hardware limitations (queue
1103 * depth settings). If a device driver gets a 'queue full' response,
1104 * or if it simply chooses not to queue more I/O at one point, it can
1105 * call this function to prevent the request_fn from being called until
1106 * the driver has signalled it's ready to go again. This happens by calling
1107 * blk_start_queue() to restart queue operations. Queue lock must be held.
1109 void blk_stop_queue(request_queue_t
*q
)
1112 set_bit(QUEUE_FLAG_STOPPED
, &q
->queue_flags
);
1116 * blk_run_queue - run a single device queue
1117 * @q The queue to run
1119 void blk_run_queue(struct request_queue
*q
)
1121 unsigned long flags
;
1123 spin_lock_irqsave(q
->queue_lock
, flags
);
1126 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1130 * blk_run_queues - fire all plugged queues
1133 * Start I/O on all plugged queues known to the block layer. Queues that
1134 * are currently stopped are ignored. This is equivalent to the older
1135 * tq_disk task queue run.
1137 #define blk_plug_entry(entry) list_entry((entry), request_queue_t, plug_list)
1138 void blk_run_queues(void)
1140 LIST_HEAD(local_plug_list
);
1142 spin_lock_irq(&blk_plug_lock
);
1145 * this will happen fairly often
1147 if (list_empty(&blk_plug_list
))
1150 list_splice_init(&blk_plug_list
, &local_plug_list
);
1152 while (!list_empty(&local_plug_list
)) {
1153 request_queue_t
*q
= blk_plug_entry(local_plug_list
.next
);
1155 spin_unlock_irq(&blk_plug_lock
);
1157 spin_lock_irq(&blk_plug_lock
);
1160 spin_unlock_irq(&blk_plug_lock
);
1164 * blk_cleanup_queue: - release a &request_queue_t when it is no longer needed
1165 * @q: the request queue to be released
1168 * blk_cleanup_queue is the pair to blk_init_queue(). It should
1169 * be called when a request queue is being released; typically
1170 * when a block device is being de-registered. Currently, its
1171 * primary task it to free all the &struct request structures that
1172 * were allocated to the queue.
1174 * Hopefully the low level driver will have finished any
1175 * outstanding requests first...
1177 void blk_cleanup_queue(request_queue_t
* q
)
1179 struct request_list
*rl
= &q
->rq
;
1183 del_timer_sync(&q
->unplug_timer
);
1186 mempool_destroy(rl
->rq_pool
);
1188 if (blk_queue_tagged(q
))
1189 blk_queue_free_tags(q
);
1191 memset(q
, 0, sizeof(*q
));
1194 static int blk_init_free_list(request_queue_t
*q
)
1196 struct request_list
*rl
= &q
->rq
;
1198 rl
->count
[READ
] = rl
->count
[WRITE
] = 0;
1199 init_waitqueue_head(&rl
->wait
[READ
]);
1200 init_waitqueue_head(&rl
->wait
[WRITE
]);
1202 rl
->rq_pool
= mempool_create(BLKDEV_MIN_RQ
, mempool_alloc_slab
, mempool_free_slab
, request_cachep
);
1210 static int __make_request(request_queue_t
*, struct bio
*);
1212 static elevator_t
*chosen_elevator
=
1213 #if defined(CONFIG_IOSCHED_AS)
1215 #elif defined(CONFIG_IOSCHED_DEADLINE)
1221 #if defined(CONFIG_IOSCHED_AS) || defined(CONFIG_IOSCHED_DEADLINE)
1222 static int __init
elevator_setup(char *str
)
1224 #ifdef CONFIG_IOSCHED_DEADLINE
1225 if (!strcmp(str
, "deadline"))
1226 chosen_elevator
= &iosched_deadline
;
1228 #ifdef CONFIG_IOSCHED_AS
1229 if (!strcmp(str
, "as"))
1230 chosen_elevator
= &iosched_as
;
1235 __setup("elevator=", elevator_setup
);
1236 #endif /* CONFIG_IOSCHED_AS || CONFIG_IOSCHED_DEADLINE */
1239 * blk_init_queue - prepare a request queue for use with a block device
1240 * @q: The &request_queue_t to be initialised
1241 * @rfn: The function to be called to process requests that have been
1242 * placed on the queue.
1245 * If a block device wishes to use the standard request handling procedures,
1246 * which sorts requests and coalesces adjacent requests, then it must
1247 * call blk_init_queue(). The function @rfn will be called when there
1248 * are requests on the queue that need to be processed. If the device
1249 * supports plugging, then @rfn may not be called immediately when requests
1250 * are available on the queue, but may be called at some time later instead.
1251 * Plugged queues are generally unplugged when a buffer belonging to one
1252 * of the requests on the queue is needed, or due to memory pressure.
1254 * @rfn is not required, or even expected, to remove all requests off the
1255 * queue, but only as many as it can handle at a time. If it does leave
1256 * requests on the queue, it is responsible for arranging that the requests
1257 * get dealt with eventually.
1259 * The queue spin lock must be held while manipulating the requests on the
1263 * blk_init_queue() must be paired with a blk_cleanup_queue() call
1264 * when the block device is deactivated (such as at module unload).
1266 int blk_init_queue(request_queue_t
*q
, request_fn_proc
*rfn
, spinlock_t
*lock
)
1271 if (blk_init_free_list(q
))
1276 printk("Using %s elevator\n", chosen_elevator
->elevator_name
);
1279 if ((ret
= elevator_init(q
, chosen_elevator
))) {
1280 blk_cleanup_queue(q
);
1284 q
->request_fn
= rfn
;
1285 q
->back_merge_fn
= ll_back_merge_fn
;
1286 q
->front_merge_fn
= ll_front_merge_fn
;
1287 q
->merge_requests_fn
= ll_merge_requests_fn
;
1288 q
->prep_rq_fn
= NULL
;
1289 q
->unplug_fn
= generic_unplug_device
;
1290 q
->queue_flags
= (1 << QUEUE_FLAG_CLUSTER
);
1291 q
->queue_lock
= lock
;
1293 blk_queue_segment_boundary(q
, 0xffffffff);
1295 blk_queue_make_request(q
, __make_request
);
1296 blk_queue_max_segment_size(q
, MAX_SEGMENT_SIZE
);
1298 blk_queue_max_hw_segments(q
, MAX_HW_SEGMENTS
);
1299 blk_queue_max_phys_segments(q
, MAX_PHYS_SEGMENTS
);
1304 static inline void blk_free_request(request_queue_t
*q
, struct request
*rq
)
1306 elv_put_request(q
, rq
);
1307 mempool_free(rq
, q
->rq
.rq_pool
);
1310 static inline struct request
*blk_alloc_request(request_queue_t
*q
,int gfp_mask
)
1312 struct request
*rq
= mempool_alloc(q
->rq
.rq_pool
, gfp_mask
);
1317 if (!elv_set_request(q
, rq
, gfp_mask
))
1320 mempool_free(rq
, q
->rq
.rq_pool
);
1325 * ioc_batching returns true if the ioc is a valid batching request and
1326 * should be given priority access to a request.
1328 static inline int ioc_batching(struct io_context
*ioc
)
1334 * Make sure the process is able to allocate at least 1 request
1335 * even if the batch times out, otherwise we could theoretically
1338 return ioc
->nr_batch_requests
== BLK_BATCH_REQ
||
1339 (ioc
->nr_batch_requests
> 0
1340 && time_before(jiffies
, ioc
->last_waited
+ BLK_BATCH_TIME
));
1344 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
1345 * will cause the process to be a "batcher" on all queues in the system. This
1346 * is the behaviour we want though - once it gets a wakeup it should be given
1349 void ioc_set_batching(struct io_context
*ioc
)
1351 if (!ioc
|| ioc_batching(ioc
))
1354 ioc
->nr_batch_requests
= BLK_BATCH_REQ
;
1355 ioc
->last_waited
= jiffies
;
1359 * A request has just been released. Account for it, update the full and
1360 * congestion status, wake up any waiters. Called under q->queue_lock.
1362 static void freed_request(request_queue_t
*q
, int rw
)
1364 struct request_list
*rl
= &q
->rq
;
1367 if (rl
->count
[rw
] < queue_congestion_off_threshold(q
))
1368 clear_queue_congested(q
, rw
);
1369 if (rl
->count
[rw
]+1 <= q
->nr_requests
) {
1371 if (waitqueue_active(&rl
->wait
[rw
]))
1372 wake_up(&rl
->wait
[rw
]);
1373 if (!waitqueue_active(&rl
->wait
[rw
]))
1374 blk_clear_queue_full(q
, rw
);
1378 #define blkdev_free_rq(list) list_entry((list)->next, struct request, queuelist)
1380 * Get a free request, queue_lock must not be held
1382 static struct request
*get_request(request_queue_t
*q
, int rw
, int gfp_mask
)
1384 struct request
*rq
= NULL
;
1385 struct request_list
*rl
= &q
->rq
;
1386 struct io_context
*ioc
= get_io_context(gfp_mask
);
1388 spin_lock_irq(q
->queue_lock
);
1389 if (rl
->count
[rw
]+1 >= q
->nr_requests
) {
1391 * The queue will fill after this allocation, so set it as
1392 * full, and mark this process as "batching". This process
1393 * will be allowed to complete a batch of requests, others
1396 if (!blk_queue_full(q
, rw
)) {
1397 ioc_set_batching(ioc
);
1398 blk_set_queue_full(q
, rw
);
1402 if (blk_queue_full(q
, rw
)
1403 && !ioc_batching(ioc
) && !elv_may_queue(q
, rw
)) {
1405 * The queue is full and the allocating process is not a
1406 * "batcher", and not exempted by the IO scheduler
1408 spin_unlock_irq(q
->queue_lock
);
1413 if (rl
->count
[rw
] >= queue_congestion_on_threshold(q
))
1414 set_queue_congested(q
, rw
);
1415 spin_unlock_irq(q
->queue_lock
);
1417 rq
= blk_alloc_request(q
, gfp_mask
);
1420 * Allocation failed presumably due to memory. Undo anything
1421 * we might have messed up.
1423 * Allocating task should really be put onto the front of the
1424 * wait queue, but this is pretty rare.
1426 spin_lock_irq(q
->queue_lock
);
1427 freed_request(q
, rw
);
1428 spin_unlock_irq(q
->queue_lock
);
1432 if (ioc_batching(ioc
))
1433 ioc
->nr_batch_requests
--;
1435 INIT_LIST_HEAD(&rq
->queuelist
);
1438 * first three bits are identical in rq->flags and bio->bi_rw,
1439 * see bio.h and blkdev.h
1444 rq
->rq_status
= RQ_ACTIVE
;
1445 rq
->bio
= rq
->biotail
= NULL
;
1456 put_io_context(ioc
);
1461 * No available requests for this queue, unplug the device and wait for some
1462 * requests to become available.
1464 static struct request
*get_request_wait(request_queue_t
*q
, int rw
)
1469 generic_unplug_device(q
);
1471 struct request_list
*rl
= &q
->rq
;
1473 prepare_to_wait_exclusive(&rl
->wait
[rw
], &wait
,
1474 TASK_UNINTERRUPTIBLE
);
1476 rq
= get_request(q
, rw
, GFP_NOIO
);
1479 struct io_context
*ioc
;
1484 * After sleeping, we become a "batching" process and
1485 * will be able to allocate at least one request, and
1486 * up to a big batch of them for a small period time.
1487 * See ioc_batching, ioc_set_batching
1489 ioc
= get_io_context(GFP_NOIO
);
1490 ioc_set_batching(ioc
);
1491 put_io_context(ioc
);
1493 finish_wait(&rl
->wait
[rw
], &wait
);
1499 struct request
*blk_get_request(request_queue_t
*q
, int rw
, int gfp_mask
)
1503 BUG_ON(rw
!= READ
&& rw
!= WRITE
);
1505 if (gfp_mask
& __GFP_WAIT
)
1506 rq
= get_request_wait(q
, rw
);
1508 rq
= get_request(q
, rw
, gfp_mask
);
1513 * blk_requeue_request - put a request back on queue
1514 * @q: request queue where request should be inserted
1515 * @rq: request to be inserted
1518 * Drivers often keep queueing requests until the hardware cannot accept
1519 * more, when that condition happens we need to put the request back
1520 * on the queue. Must be called with queue lock held.
1522 void blk_requeue_request(request_queue_t
*q
, struct request
*rq
)
1524 if (blk_rq_tagged(rq
))
1525 blk_queue_end_tag(q
, rq
);
1527 elv_requeue_request(q
, rq
);
1531 * blk_insert_request - insert a special request in to a request queue
1532 * @q: request queue where request should be inserted
1533 * @rq: request to be inserted
1534 * @at_head: insert request at head or tail of queue
1535 * @data: private data
1536 * @reinsert: true if request it a reinsertion of previously processed one
1539 * Many block devices need to execute commands asynchronously, so they don't
1540 * block the whole kernel from preemption during request execution. This is
1541 * accomplished normally by inserting aritficial requests tagged as
1542 * REQ_SPECIAL in to the corresponding request queue, and letting them be
1543 * scheduled for actual execution by the request queue.
1545 * We have the option of inserting the head or the tail of the queue.
1546 * Typically we use the tail for new ioctls and so forth. We use the head
1547 * of the queue for things like a QUEUE_FULL message from a device, or a
1548 * host that is unable to accept a particular command.
1550 void blk_insert_request(request_queue_t
*q
, struct request
*rq
,
1551 int at_head
, void *data
, int reinsert
)
1553 unsigned long flags
;
1556 * tell I/O scheduler that this isn't a regular read/write (ie it
1557 * must not attempt merges on this) and that it acts as a soft
1560 rq
->flags
|= REQ_SPECIAL
| REQ_SOFTBARRIER
;
1564 spin_lock_irqsave(q
->queue_lock
, flags
);
1567 * If command is tagged, release the tag
1570 blk_requeue_request(q
, rq
);
1572 if (blk_rq_tagged(rq
))
1573 blk_queue_end_tag(q
, rq
);
1575 drive_stat_acct(rq
, rq
->nr_sectors
, 1);
1576 __elv_add_request(q
, rq
, !at_head
, 0);
1579 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1582 void drive_stat_acct(struct request
*rq
, int nr_sectors
, int new_io
)
1584 int rw
= rq_data_dir(rq
);
1586 if (!blk_fs_request(rq
) || !rq
->rq_disk
)
1590 disk_stat_add(rq
->rq_disk
, read_sectors
, nr_sectors
);
1592 disk_stat_inc(rq
->rq_disk
, read_merges
);
1593 } else if (rw
== WRITE
) {
1594 disk_stat_add(rq
->rq_disk
, write_sectors
, nr_sectors
);
1596 disk_stat_inc(rq
->rq_disk
, write_merges
);
1599 disk_round_stats(rq
->rq_disk
);
1600 disk_stat_inc(rq
->rq_disk
, in_flight
);
1605 * add-request adds a request to the linked list.
1606 * queue lock is held and interrupts disabled, as we muck with the
1607 * request queue list.
1609 static inline void add_request(request_queue_t
* q
, struct request
* req
,
1610 struct list_head
*insert_here
)
1612 drive_stat_acct(req
, req
->nr_sectors
, 1);
1615 * elevator indicated where it wants this request to be
1616 * inserted at elevator_merge time
1618 __elv_add_request_pos(q
, req
, insert_here
);
1622 * disk_round_stats() - Round off the performance stats on a struct
1625 * The average IO queue length and utilisation statistics are maintained
1626 * by observing the current state of the queue length and the amount of
1627 * time it has been in this state for.
1629 * Normally, that accounting is done on IO completion, but that can result
1630 * in more than a second's worth of IO being accounted for within any one
1631 * second, leading to >100% utilisation. To deal with that, we call this
1632 * function to do a round-off before returning the results when reading
1633 * /proc/diskstats. This accounts immediately for all queue usage up to
1634 * the current jiffies and restarts the counters again.
1636 void disk_round_stats(struct gendisk
*disk
)
1638 unsigned long now
= jiffies
;
1640 disk_stat_add(disk
, time_in_queue
,
1641 disk_stat_read(disk
, in_flight
) * (now
- disk
->stamp
));
1644 if (disk_stat_read(disk
, in_flight
))
1645 disk_stat_add(disk
, io_ticks
, (now
- disk
->stamp_idle
));
1646 disk
->stamp_idle
= now
;
1650 * queue lock must be held
1652 void __blk_put_request(request_queue_t
*q
, struct request
*req
)
1654 struct request_list
*rl
= req
->rl
;
1658 if (unlikely(--req
->ref_count
))
1661 elv_completed_request(req
->q
, req
);
1663 req
->rq_status
= RQ_INACTIVE
;
1668 * Request may not have originated from ll_rw_blk. if not,
1669 * it didn't come out of our reserved rq pools
1672 int rw
= rq_data_dir(req
);
1674 BUG_ON(!list_empty(&req
->queuelist
));
1676 blk_free_request(q
, req
);
1677 freed_request(q
, rw
);
1681 void blk_put_request(struct request
*req
)
1683 request_queue_t
*q
= req
->q
;
1686 * if req->q isn't set, this request didnt originate from the
1687 * block layer, so it's safe to just disregard it
1690 unsigned long flags
;
1692 spin_lock_irqsave(q
->queue_lock
, flags
);
1693 __blk_put_request(q
, req
);
1694 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1699 * blk_congestion_wait - wait for a queue to become uncongested
1700 * @rw: READ or WRITE
1701 * @timeout: timeout in jiffies
1703 * Waits for up to @timeout jiffies for a queue (any queue) to exit congestion.
1704 * If no queues are congested then just wait for the next request to be
1707 void blk_congestion_wait(int rw
, long timeout
)
1710 wait_queue_head_t
*wqh
= &congestion_wqh
[rw
];
1713 prepare_to_wait(wqh
, &wait
, TASK_UNINTERRUPTIBLE
);
1714 io_schedule_timeout(timeout
);
1715 finish_wait(wqh
, &wait
);
1719 * Has to be called with the request spinlock acquired
1721 static int attempt_merge(request_queue_t
*q
, struct request
*req
,
1722 struct request
*next
)
1724 if (!rq_mergeable(req
) || !rq_mergeable(next
))
1730 if (req
->sector
+ req
->nr_sectors
!= next
->sector
)
1733 if (rq_data_dir(req
) != rq_data_dir(next
)
1734 || req
->rq_disk
!= next
->rq_disk
1735 || next
->waiting
|| next
->special
)
1739 * If we are allowed to merge, then append bio list
1740 * from next to rq and release next. merge_requests_fn
1741 * will have updated segment counts, update sector
1744 if (!q
->merge_requests_fn(q
, req
, next
))
1747 req
->biotail
->bi_next
= next
->bio
;
1748 req
->biotail
= next
->biotail
;
1750 req
->nr_sectors
= req
->hard_nr_sectors
+= next
->hard_nr_sectors
;
1752 elv_merge_requests(q
, req
, next
);
1755 disk_round_stats(req
->rq_disk
);
1756 disk_stat_dec(req
->rq_disk
, in_flight
);
1759 __blk_put_request(q
, next
);
1763 static inline int attempt_back_merge(request_queue_t
*q
, struct request
*rq
)
1765 struct request
*next
= elv_latter_request(q
, rq
);
1768 return attempt_merge(q
, rq
, next
);
1773 static inline int attempt_front_merge(request_queue_t
*q
, struct request
*rq
)
1775 struct request
*prev
= elv_former_request(q
, rq
);
1778 return attempt_merge(q
, prev
, rq
);
1784 * blk_attempt_remerge - attempt to remerge active head with next request
1785 * @q: The &request_queue_t belonging to the device
1786 * @rq: The head request (usually)
1789 * For head-active devices, the queue can easily be unplugged so quickly
1790 * that proper merging is not done on the front request. This may hurt
1791 * performance greatly for some devices. The block layer cannot safely
1792 * do merging on that first request for these queues, but the driver can
1793 * call this function and make it happen any way. Only the driver knows
1794 * when it is safe to do so.
1796 void blk_attempt_remerge(request_queue_t
*q
, struct request
*rq
)
1798 unsigned long flags
;
1800 spin_lock_irqsave(q
->queue_lock
, flags
);
1801 attempt_back_merge(q
, rq
);
1802 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1806 * Non-locking blk_attempt_remerge variant.
1808 void __blk_attempt_remerge(request_queue_t
*q
, struct request
*rq
)
1810 attempt_back_merge(q
, rq
);
1813 static int __make_request(request_queue_t
*q
, struct bio
*bio
)
1815 struct request
*req
, *freereq
= NULL
;
1816 int el_ret
, rw
, nr_sectors
, cur_nr_sectors
, barrier
, ra
;
1817 struct list_head
*insert_here
;
1820 sector
= bio
->bi_sector
;
1821 nr_sectors
= bio_sectors(bio
);
1822 cur_nr_sectors
= bio_cur_sectors(bio
);
1824 rw
= bio_data_dir(bio
);
1827 * low level driver can indicate that it wants pages above a
1828 * certain limit bounced to low memory (ie for highmem, or even
1829 * ISA dma in theory)
1831 blk_queue_bounce(q
, &bio
);
1833 spin_lock_prefetch(q
->queue_lock
);
1835 barrier
= test_bit(BIO_RW_BARRIER
, &bio
->bi_rw
);
1837 ra
= bio_flagged(bio
, BIO_RW_AHEAD
) || current
->flags
& PF_READAHEAD
;
1841 spin_lock_irq(q
->queue_lock
);
1843 if (elv_queue_empty(q
)) {
1850 el_ret
= elv_merge(q
, &insert_here
, bio
);
1852 case ELEVATOR_BACK_MERGE
:
1853 req
= list_entry_rq(insert_here
);
1855 BUG_ON(!rq_mergeable(req
));
1857 if (!q
->back_merge_fn(q
, req
, bio
)) {
1858 insert_here
= &req
->queuelist
;
1862 req
->biotail
->bi_next
= bio
;
1864 req
->nr_sectors
= req
->hard_nr_sectors
+= nr_sectors
;
1865 drive_stat_acct(req
, nr_sectors
, 0);
1866 if (!attempt_back_merge(q
, req
))
1867 elv_merged_request(q
, req
);
1870 case ELEVATOR_FRONT_MERGE
:
1871 req
= list_entry_rq(insert_here
);
1873 BUG_ON(!rq_mergeable(req
));
1875 if (!q
->front_merge_fn(q
, req
, bio
)) {
1876 insert_here
= req
->queuelist
.prev
;
1880 bio
->bi_next
= req
->bio
;
1881 req
->cbio
= req
->bio
= bio
;
1882 req
->nr_cbio_segments
= bio_segments(bio
);
1883 req
->nr_cbio_sectors
= bio_sectors(bio
);
1886 * may not be valid. if the low level driver said
1887 * it didn't need a bounce buffer then it better
1888 * not touch req->buffer either...
1890 req
->buffer
= bio_data(bio
);
1891 req
->current_nr_sectors
= cur_nr_sectors
;
1892 req
->hard_cur_sectors
= cur_nr_sectors
;
1893 req
->sector
= req
->hard_sector
= sector
;
1894 req
->nr_sectors
= req
->hard_nr_sectors
+= nr_sectors
;
1895 drive_stat_acct(req
, nr_sectors
, 0);
1896 if (!attempt_front_merge(q
, req
))
1897 elv_merged_request(q
, req
);
1901 * elevator says don't/can't merge. get new request
1903 case ELEVATOR_NO_MERGE
:
1907 printk("elevator returned crap (%d)\n", el_ret
);
1912 * Grab a free request from the freelist - if that is empty, check
1913 * if we are doing read ahead and abort instead of blocking for
1921 spin_unlock_irq(q
->queue_lock
);
1922 if ((freereq
= get_request(q
, rw
, GFP_ATOMIC
)) == NULL
) {
1929 freereq
= get_request_wait(q
, rw
);
1935 * first three bits are identical in rq->flags and bio->bi_rw,
1936 * see bio.h and blkdev.h
1938 req
->flags
= (bio
->bi_rw
& 7) | REQ_CMD
;
1941 * REQ_BARRIER implies no merging, but lets make it explicit
1944 req
->flags
|= (REQ_HARDBARRIER
| REQ_NOMERGE
);
1947 * don't stack up retries for read ahead
1950 req
->flags
|= REQ_FAILFAST
;
1953 req
->hard_sector
= req
->sector
= sector
;
1954 req
->hard_nr_sectors
= req
->nr_sectors
= nr_sectors
;
1955 req
->current_nr_sectors
= req
->hard_cur_sectors
= cur_nr_sectors
;
1956 req
->nr_phys_segments
= bio_phys_segments(q
, bio
);
1957 req
->nr_hw_segments
= bio_hw_segments(q
, bio
);
1958 req
->nr_cbio_segments
= bio_segments(bio
);
1959 req
->nr_cbio_sectors
= bio_sectors(bio
);
1960 req
->buffer
= bio_data(bio
); /* see ->buffer comment above */
1961 req
->waiting
= NULL
;
1962 req
->cbio
= req
->bio
= req
->biotail
= bio
;
1963 req
->rq_disk
= bio
->bi_bdev
->bd_disk
;
1964 req
->start_time
= jiffies
;
1966 add_request(q
, req
, insert_here
);
1969 __blk_put_request(q
, freereq
);
1971 if (blk_queue_plugged(q
)) {
1972 int nr_queued
= q
->rq
.count
[READ
] + q
->rq
.count
[WRITE
];
1974 if (nr_queued
== q
->unplug_thresh
)
1975 __generic_unplug_device(q
);
1977 spin_unlock_irq(q
->queue_lock
);
1981 bio_endio(bio
, nr_sectors
<< 9, -EWOULDBLOCK
);
1986 * If bio->bi_dev is a partition, remap the location
1988 static inline void blk_partition_remap(struct bio
*bio
)
1990 struct block_device
*bdev
= bio
->bi_bdev
;
1991 struct gendisk
*disk
= bdev
->bd_disk
;
1992 struct hd_struct
*p
;
1993 if (bdev
== bdev
->bd_contains
)
1996 p
= disk
->part
[bdev
->bd_dev
-MKDEV(disk
->major
,disk
->first_minor
)-1];
1997 switch (bio
->bi_rw
) {
1999 p
->read_sectors
+= bio_sectors(bio
);
2003 p
->write_sectors
+= bio_sectors(bio
);
2007 bio
->bi_sector
+= bdev
->bd_offset
;
2008 bio
->bi_bdev
= bdev
->bd_contains
;
2012 * generic_make_request: hand a buffer to its device driver for I/O
2013 * @bio: The bio describing the location in memory and on the device.
2015 * generic_make_request() is used to make I/O requests of block
2016 * devices. It is passed a &struct bio, which describes the I/O that needs
2019 * generic_make_request() does not return any status. The
2020 * success/failure status of the request, along with notification of
2021 * completion, is delivered asynchronously through the bio->bi_end_io
2022 * function described (one day) else where.
2024 * The caller of generic_make_request must make sure that bi_io_vec
2025 * are set to describe the memory buffer, and that bi_dev and bi_sector are
2026 * set to describe the device address, and the
2027 * bi_end_io and optionally bi_private are set to describe how
2028 * completion notification should be signaled.
2030 * generic_make_request and the drivers it calls may use bi_next if this
2031 * bio happens to be merged with someone else, and may change bi_dev and
2032 * bi_sector for remaps as it sees fit. So the values of these fields
2033 * should NOT be depended on after the call to generic_make_request.
2035 void generic_make_request(struct bio
*bio
)
2039 int ret
, nr_sectors
= bio_sectors(bio
);
2041 /* Test device or partition size, when known. */
2042 maxsector
= bio
->bi_bdev
->bd_inode
->i_size
>> 9;
2044 sector_t sector
= bio
->bi_sector
;
2046 if (maxsector
< nr_sectors
||
2047 maxsector
- nr_sectors
< sector
) {
2048 char b
[BDEVNAME_SIZE
];
2049 /* This may well happen - the kernel calls
2050 * bread() without checking the size of the
2051 * device, e.g., when mounting a device. */
2053 "attempt to access beyond end of device\n");
2054 printk(KERN_INFO
"%s: rw=%ld, want=%Lu, limit=%Lu\n",
2055 bdevname(bio
->bi_bdev
, b
),
2057 (unsigned long long) sector
+ nr_sectors
,
2058 (long long) maxsector
);
2060 set_bit(BIO_EOF
, &bio
->bi_flags
);
2066 * Resolve the mapping until finished. (drivers are
2067 * still free to implement/resolve their own stacking
2068 * by explicitly returning 0)
2070 * NOTE: we don't repeat the blk_size check for each new device.
2071 * Stacking drivers are expected to know what they are doing.
2074 char b
[BDEVNAME_SIZE
];
2076 q
= bdev_get_queue(bio
->bi_bdev
);
2079 "generic_make_request: Trying to access "
2080 "nonexistent block-device %s (%Lu)\n",
2081 bdevname(bio
->bi_bdev
, b
),
2082 (long long) bio
->bi_sector
);
2084 bio_endio(bio
, bio
->bi_size
, -EIO
);
2088 if (unlikely(bio_sectors(bio
) > q
->max_sectors
)) {
2089 printk("bio too big device %s (%u > %u)\n",
2090 bdevname(bio
->bi_bdev
, b
),
2097 * If this device has partitions, remap block n
2098 * of partition p to block n+start(p) of the disk.
2100 blk_partition_remap(bio
);
2102 ret
= q
->make_request_fn(q
, bio
);
2107 * submit_bio: submit a bio to the block device layer for I/O
2108 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
2109 * @bio: The &struct bio which describes the I/O
2111 * submit_bio() is very similar in purpose to generic_make_request(), and
2112 * uses that function to do most of the work. Both are fairly rough
2113 * interfaces, @bio must be presetup and ready for I/O.
2116 int submit_bio(int rw
, struct bio
*bio
)
2118 int count
= bio_sectors(bio
);
2120 BIO_BUG_ON(!bio
->bi_size
);
2121 BIO_BUG_ON(!bio
->bi_io_vec
);
2124 mod_page_state(pgpgout
, count
);
2126 mod_page_state(pgpgin
, count
);
2127 generic_make_request(bio
);
2132 * blk_rq_next_segment
2133 * @rq: the request being processed
2136 * Points to the next segment in the request if the current segment
2137 * is complete. Leaves things unchanged if this segment is not over
2138 * or if no more segments are left in this request.
2140 * Meant to be used for bio traversal during I/O submission
2141 * Does not affect any I/O completions or update completion state
2142 * in the request, and does not modify any bio fields.
2144 * Decrementing rq->nr_sectors, rq->current_nr_sectors and
2145 * rq->nr_cbio_sectors as data is transferred is the caller's
2146 * responsibility and should be done before calling this routine.
2148 void blk_rq_next_segment(struct request
*rq
)
2150 if (rq
->current_nr_sectors
> 0)
2153 if (rq
->nr_cbio_sectors
> 0) {
2154 --rq
->nr_cbio_segments
;
2155 rq
->current_nr_sectors
= blk_rq_vec(rq
)->bv_len
>> 9;
2157 if ((rq
->cbio
= rq
->cbio
->bi_next
)) {
2158 rq
->nr_cbio_segments
= bio_segments(rq
->cbio
);
2159 rq
->nr_cbio_sectors
= bio_sectors(rq
->cbio
);
2160 rq
->current_nr_sectors
= bio_cur_sectors(rq
->cbio
);
2164 /* remember the size of this segment before we start I/O */
2165 rq
->hard_cur_sectors
= rq
->current_nr_sectors
;
2169 * process_that_request_first - process partial request submission
2170 * @req: the request being processed
2171 * @nr_sectors: number of sectors I/O has been submitted on
2174 * May be used for processing bio's while submitting I/O without
2175 * signalling completion. Fails if more data is requested than is
2176 * available in the request in which case it doesn't advance any
2179 * Assumes a request is correctly set up. No sanity checks.
2182 * 0 - no more data left to submit (not processed)
2183 * 1 - data available to submit for this request (processed)
2185 int process_that_request_first(struct request
*req
, unsigned int nr_sectors
)
2189 if (req
->nr_sectors
< nr_sectors
)
2192 req
->nr_sectors
-= nr_sectors
;
2193 req
->sector
+= nr_sectors
;
2194 while (nr_sectors
) {
2195 nsect
= min_t(unsigned, req
->current_nr_sectors
, nr_sectors
);
2196 req
->current_nr_sectors
-= nsect
;
2197 nr_sectors
-= nsect
;
2199 req
->nr_cbio_sectors
-= nsect
;
2200 blk_rq_next_segment(req
);
2206 void blk_recalc_rq_segments(struct request
*rq
)
2209 int nr_phys_segs
, nr_hw_segs
;
2214 nr_phys_segs
= nr_hw_segs
= 0;
2215 rq_for_each_bio(bio
, rq
) {
2216 /* Force bio hw/phys segs to be recalculated. */
2217 bio
->bi_flags
&= ~(1 << BIO_SEG_VALID
);
2219 nr_phys_segs
+= bio_phys_segments(rq
->q
, bio
);
2220 nr_hw_segs
+= bio_hw_segments(rq
->q
, bio
);
2223 rq
->nr_phys_segments
= nr_phys_segs
;
2224 rq
->nr_hw_segments
= nr_hw_segs
;
2227 void blk_recalc_rq_sectors(struct request
*rq
, int nsect
)
2229 if (blk_fs_request(rq
)) {
2230 rq
->hard_sector
+= nsect
;
2231 rq
->hard_nr_sectors
-= nsect
;
2234 * Move the I/O submission pointers ahead if required,
2235 * i.e. for drivers not aware of rq->cbio.
2237 if ((rq
->nr_sectors
>= rq
->hard_nr_sectors
) &&
2238 (rq
->sector
<= rq
->hard_sector
)) {
2239 rq
->sector
= rq
->hard_sector
;
2240 rq
->nr_sectors
= rq
->hard_nr_sectors
;
2241 rq
->hard_cur_sectors
= bio_cur_sectors(rq
->bio
);
2242 rq
->current_nr_sectors
= rq
->hard_cur_sectors
;
2243 rq
->nr_cbio_segments
= bio_segments(rq
->bio
);
2244 rq
->nr_cbio_sectors
= bio_sectors(rq
->bio
);
2245 rq
->buffer
= bio_data(rq
->bio
);
2251 * if total number of sectors is less than the first segment
2252 * size, something has gone terribly wrong
2254 if (rq
->nr_sectors
< rq
->current_nr_sectors
) {
2255 printk("blk: request botched\n");
2256 rq
->nr_sectors
= rq
->current_nr_sectors
;
2261 static int __end_that_request_first(struct request
*req
, int uptodate
,
2264 int total_bytes
, bio_nbytes
, error
= 0, next_idx
= 0;
2268 * for a REQ_BLOCK_PC request, we want to carry any eventual
2269 * sense key with us all the way through
2271 if (!blk_pc_request(req
))
2276 if (!(req
->flags
& REQ_QUIET
))
2277 printk("end_request: I/O error, dev %s, sector %llu\n",
2278 req
->rq_disk
? req
->rq_disk
->disk_name
: "?",
2279 (unsigned long long)req
->sector
);
2282 total_bytes
= bio_nbytes
= 0;
2283 while ((bio
= req
->bio
)) {
2286 if (nr_bytes
>= bio
->bi_size
) {
2287 req
->bio
= bio
->bi_next
;
2288 nbytes
= bio
->bi_size
;
2289 bio_endio(bio
, nbytes
, error
);
2293 int idx
= bio
->bi_idx
+ next_idx
;
2295 if (unlikely(bio
->bi_idx
>= bio
->bi_vcnt
)) {
2296 blk_dump_rq_flags(req
, "__end_that");
2297 printk("%s: bio idx %d >= vcnt %d\n",
2299 bio
->bi_idx
, bio
->bi_vcnt
);
2303 nbytes
= bio_iovec_idx(bio
, idx
)->bv_len
;
2304 BIO_BUG_ON(nbytes
> bio
->bi_size
);
2307 * not a complete bvec done
2309 if (unlikely(nbytes
> nr_bytes
)) {
2310 bio_iovec_idx(bio
, idx
)->bv_offset
+= nr_bytes
;
2311 bio_iovec_idx(bio
, idx
)->bv_len
-= nr_bytes
;
2312 bio_nbytes
+= nr_bytes
;
2313 total_bytes
+= nr_bytes
;
2318 * advance to the next vector
2321 bio_nbytes
+= nbytes
;
2324 total_bytes
+= nbytes
;
2327 if ((bio
= req
->bio
)) {
2329 * end more in this run, or just return 'not-done'
2331 if (unlikely(nr_bytes
<= 0))
2343 * if the request wasn't completed, update state
2346 bio_endio(bio
, bio_nbytes
, error
);
2347 req
->bio
->bi_idx
+= next_idx
;
2350 blk_recalc_rq_sectors(req
, total_bytes
>> 9);
2351 blk_recalc_rq_segments(req
);
2356 * end_that_request_first - end I/O on a request
2357 * @req: the request being processed
2358 * @uptodate: 0 for I/O error
2359 * @nr_sectors: number of sectors to end I/O on
2362 * Ends I/O on a number of sectors attached to @req, and sets it up
2363 * for the next range of segments (if any) in the cluster.
2366 * 0 - we are done with this request, call end_that_request_last()
2367 * 1 - still buffers pending for this request
2369 int end_that_request_first(struct request
*req
, int uptodate
, int nr_sectors
)
2371 return __end_that_request_first(req
, uptodate
, nr_sectors
<< 9);
2375 * end_that_request_chunk - end I/O on a request
2376 * @req: the request being processed
2377 * @uptodate: 0 for I/O error
2378 * @nr_bytes: number of bytes to complete
2381 * Ends I/O on a number of bytes attached to @req, and sets it up
2382 * for the next range of segments (if any). Like end_that_request_first(),
2383 * but deals with bytes instead of sectors.
2386 * 0 - we are done with this request, call end_that_request_last()
2387 * 1 - still buffers pending for this request
2389 int end_that_request_chunk(struct request
*req
, int uptodate
, int nr_bytes
)
2391 return __end_that_request_first(req
, uptodate
, nr_bytes
);
2395 * queue lock must be held
2397 void end_that_request_last(struct request
*req
)
2399 struct gendisk
*disk
= req
->rq_disk
;
2400 struct completion
*waiting
= req
->waiting
;
2402 if (disk
&& blk_fs_request(req
)) {
2403 unsigned long duration
= jiffies
- req
->start_time
;
2404 switch (rq_data_dir(req
)) {
2406 disk_stat_inc(disk
, writes
);
2407 disk_stat_add(disk
, write_ticks
, duration
);
2410 disk_stat_inc(disk
, reads
);
2411 disk_stat_add(disk
, read_ticks
, duration
);
2414 disk_round_stats(disk
);
2415 disk_stat_dec(disk
, in_flight
);
2417 __blk_put_request(req
->q
, req
);
2418 /* Do this LAST! The structure may be freed immediately afterwards */
2423 void end_request(struct request
*req
, int uptodate
)
2425 if (!end_that_request_first(req
, uptodate
, req
->hard_cur_sectors
)) {
2426 add_disk_randomness(req
->rq_disk
);
2427 blkdev_dequeue_request(req
);
2428 end_that_request_last(req
);
2432 void blk_rq_bio_prep(request_queue_t
*q
, struct request
*rq
, struct bio
*bio
)
2434 /* first three bits are identical in rq->flags and bio->bi_rw */
2435 rq
->flags
|= (bio
->bi_rw
& 7);
2437 rq
->nr_phys_segments
= bio_phys_segments(q
, bio
);
2438 rq
->nr_hw_segments
= bio_hw_segments(q
, bio
);
2439 rq
->current_nr_sectors
= bio_cur_sectors(bio
);
2440 rq
->hard_cur_sectors
= rq
->current_nr_sectors
;
2441 rq
->hard_nr_sectors
= rq
->nr_sectors
= bio_sectors(bio
);
2442 rq
->nr_cbio_segments
= bio_segments(bio
);
2443 rq
->nr_cbio_sectors
= bio_sectors(bio
);
2444 rq
->buffer
= bio_data(bio
);
2446 rq
->cbio
= rq
->bio
= rq
->biotail
= bio
;
2449 void blk_rq_prep_restart(struct request
*rq
)
2453 bio
= rq
->cbio
= rq
->bio
;
2455 rq
->nr_cbio_segments
= bio_segments(bio
);
2456 rq
->nr_cbio_sectors
= bio_sectors(bio
);
2457 rq
->hard_cur_sectors
= bio_cur_sectors(bio
);
2458 rq
->buffer
= bio_data(bio
);
2460 rq
->sector
= rq
->hard_sector
;
2461 rq
->nr_sectors
= rq
->hard_nr_sectors
;
2462 rq
->current_nr_sectors
= rq
->hard_cur_sectors
;
2465 int kblockd_schedule_work(struct work_struct
*work
)
2467 return queue_work(kblockd_workqueue
, work
);
2470 void kblockd_flush(void)
2472 flush_workqueue(kblockd_workqueue
);
2475 int __init
blk_dev_init(void)
2479 kblockd_workqueue
= create_workqueue("kblockd");
2480 if (!kblockd_workqueue
)
2481 panic("Failed to create kblockd\n");
2483 request_cachep
= kmem_cache_create("blkdev_requests",
2484 sizeof(struct request
), 0, 0, NULL
, NULL
);
2485 if (!request_cachep
)
2486 panic("Can't create request pool slab cache\n");
2488 blk_max_low_pfn
= max_low_pfn
;
2489 blk_max_pfn
= max_pfn
;
2491 for (i
= 0; i
< ARRAY_SIZE(congestion_wqh
); i
++)
2492 init_waitqueue_head(&congestion_wqh
[i
]);
2496 static atomic_t nr_io_contexts
= ATOMIC_INIT(0);
2499 * IO Context helper functions
2501 void put_io_context(struct io_context
*ioc
)
2506 BUG_ON(atomic_read(&ioc
->refcount
) == 0);
2508 if (atomic_dec_and_test(&ioc
->refcount
)) {
2509 if (ioc
->aic
&& ioc
->aic
->dtor
)
2510 ioc
->aic
->dtor(ioc
->aic
);
2512 atomic_dec(&nr_io_contexts
);
2516 /* Called by the exitting task */
2517 void exit_io_context(void)
2519 unsigned long flags
;
2520 struct io_context
*ioc
;
2522 local_irq_save(flags
);
2523 ioc
= current
->io_context
;
2525 if (ioc
->aic
&& ioc
->aic
->exit
)
2526 ioc
->aic
->exit(ioc
->aic
);
2527 put_io_context(ioc
);
2528 current
->io_context
= NULL
;
2531 local_irq_restore(flags
);
2535 * If the current task has no IO context then create one and initialise it.
2536 * If it does have a context, take a ref on it.
2538 * This is always called in the context of the task which submitted the I/O.
2539 * But weird things happen, so we disable local interrupts to ensure exclusive
2540 * access to *current.
2542 struct io_context
*get_io_context(int gfp_flags
)
2544 struct task_struct
*tsk
= current
;
2545 unsigned long flags
;
2546 struct io_context
*ret
;
2548 local_irq_save(flags
);
2549 ret
= tsk
->io_context
;
2551 ret
= kmalloc(sizeof(*ret
), GFP_ATOMIC
);
2553 atomic_inc(&nr_io_contexts
);
2554 atomic_set(&ret
->refcount
, 1);
2555 ret
->pid
= tsk
->pid
;
2556 ret
->last_waited
= jiffies
; /* doesn't matter... */
2557 ret
->nr_batch_requests
= 0; /* because this is 0 */
2559 tsk
->io_context
= ret
;
2563 atomic_inc(&ret
->refcount
);
2564 local_irq_restore(flags
);
2568 void copy_io_context(struct io_context
**pdst
, struct io_context
**psrc
)
2570 struct io_context
*src
= *psrc
;
2571 struct io_context
*dst
= *pdst
;
2574 BUG_ON(atomic_read(&src
->refcount
) == 0);
2575 atomic_inc(&src
->refcount
);
2576 put_io_context(dst
);
2581 void swap_io_context(struct io_context
**ioc1
, struct io_context
**ioc2
)
2583 struct io_context
*temp
;
2593 struct queue_sysfs_entry
{
2594 struct attribute attr
;
2595 ssize_t (*show
)(struct request_queue
*, char *);
2596 ssize_t (*store
)(struct request_queue
*, const char *, size_t);
2600 queue_var_show(unsigned int var
, char *page
)
2602 return sprintf(page
, "%d\n", var
);
2606 queue_var_store(unsigned long *var
, const char *page
, size_t count
)
2608 char *p
= (char *) page
;
2610 *var
= simple_strtoul(p
, &p
, 10);
2614 static ssize_t
queue_requests_show(struct request_queue
*q
, char *page
)
2616 return queue_var_show(q
->nr_requests
, (page
));
2620 queue_requests_store(struct request_queue
*q
, const char *page
, size_t count
)
2622 struct request_list
*rl
= &q
->rq
;
2624 int ret
= queue_var_store(&q
->nr_requests
, page
, count
);
2625 if (q
->nr_requests
< BLKDEV_MIN_RQ
)
2626 q
->nr_requests
= BLKDEV_MIN_RQ
;
2628 if (rl
->count
[READ
] >= queue_congestion_on_threshold(q
))
2629 set_queue_congested(q
, READ
);
2630 else if (rl
->count
[READ
] < queue_congestion_off_threshold(q
))
2631 clear_queue_congested(q
, READ
);
2633 if (rl
->count
[WRITE
] >= queue_congestion_on_threshold(q
))
2634 set_queue_congested(q
, WRITE
);
2635 else if (rl
->count
[WRITE
] < queue_congestion_off_threshold(q
))
2636 clear_queue_congested(q
, WRITE
);
2638 if (rl
->count
[READ
] >= q
->nr_requests
) {
2639 blk_set_queue_full(q
, READ
);
2640 } else if (rl
->count
[READ
]+1 <= q
->nr_requests
) {
2641 blk_clear_queue_full(q
, READ
);
2642 wake_up(&rl
->wait
[READ
]);
2645 if (rl
->count
[WRITE
] >= q
->nr_requests
) {
2646 blk_set_queue_full(q
, WRITE
);
2647 } else if (rl
->count
[WRITE
]+1 <= q
->nr_requests
) {
2648 blk_clear_queue_full(q
, WRITE
);
2649 wake_up(&rl
->wait
[WRITE
]);
2654 static struct queue_sysfs_entry queue_requests_entry
= {
2655 .attr
= {.name
= "nr_requests", .mode
= S_IRUGO
| S_IWUSR
},
2656 .show
= queue_requests_show
,
2657 .store
= queue_requests_store
,
2660 static struct attribute
*default_attrs
[] = {
2661 &queue_requests_entry
.attr
,
2665 #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
2668 queue_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
2670 struct queue_sysfs_entry
*entry
= to_queue(attr
);
2671 struct request_queue
*q
;
2673 q
= container_of(kobj
, struct request_queue
, kobj
);
2677 return entry
->show(q
, page
);
2681 queue_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
2682 const char *page
, size_t length
)
2684 struct queue_sysfs_entry
*entry
= to_queue(attr
);
2685 struct request_queue
*q
;
2687 q
= container_of(kobj
, struct request_queue
, kobj
);
2691 return entry
->store(q
, page
, length
);
2694 static struct sysfs_ops queue_sysfs_ops
= {
2695 .show
= queue_attr_show
,
2696 .store
= queue_attr_store
,
2699 struct kobj_type queue_ktype
= {
2700 .sysfs_ops
= &queue_sysfs_ops
,
2701 .default_attrs
= default_attrs
,
2704 int blk_register_queue(struct gendisk
*disk
)
2708 request_queue_t
*q
= disk
->queue
;
2713 q
->kobj
.parent
= kobject_get(&disk
->kobj
);
2714 if (!q
->kobj
.parent
)
2717 snprintf(q
->kobj
.name
, KOBJ_NAME_LEN
, "%s", "queue");
2718 q
->kobj
.ktype
= &queue_ktype
;
2720 ret
= kobject_register(&q
->kobj
);
2724 ret
= elv_register_queue(q
);
2726 kobject_unregister(&q
->kobj
);
2733 void blk_unregister_queue(struct gendisk
*disk
)
2735 request_queue_t
*q
= disk
->queue
;
2738 elv_unregister_queue(q
);
2740 kobject_unregister(&q
->kobj
);
2741 kobject_put(&disk
->kobj
);
2746 EXPORT_SYMBOL(process_that_request_first
);
2747 EXPORT_SYMBOL(end_that_request_first
);
2748 EXPORT_SYMBOL(end_that_request_chunk
);
2749 EXPORT_SYMBOL(end_that_request_last
);
2750 EXPORT_SYMBOL(end_request
);
2751 EXPORT_SYMBOL(blk_init_queue
);
2752 EXPORT_SYMBOL(blk_cleanup_queue
);
2753 EXPORT_SYMBOL(blk_queue_make_request
);
2754 EXPORT_SYMBOL(blk_queue_bounce_limit
);
2755 EXPORT_SYMBOL(generic_make_request
);
2756 EXPORT_SYMBOL(generic_unplug_device
);
2757 EXPORT_SYMBOL(blk_plug_device
);
2758 EXPORT_SYMBOL(blk_remove_plug
);
2759 EXPORT_SYMBOL(blk_attempt_remerge
);
2760 EXPORT_SYMBOL(__blk_attempt_remerge
);
2761 EXPORT_SYMBOL(blk_max_low_pfn
);
2762 EXPORT_SYMBOL(blk_max_pfn
);
2763 EXPORT_SYMBOL(blk_queue_max_sectors
);
2764 EXPORT_SYMBOL(blk_queue_max_phys_segments
);
2765 EXPORT_SYMBOL(blk_queue_max_hw_segments
);
2766 EXPORT_SYMBOL(blk_queue_max_segment_size
);
2767 EXPORT_SYMBOL(blk_queue_hardsect_size
);
2768 EXPORT_SYMBOL(blk_queue_segment_boundary
);
2769 EXPORT_SYMBOL(blk_queue_dma_alignment
);
2770 EXPORT_SYMBOL(blk_rq_map_sg
);
2771 EXPORT_SYMBOL(blk_dump_rq_flags
);
2772 EXPORT_SYMBOL(submit_bio
);
2773 EXPORT_SYMBOL(blk_phys_contig_segment
);
2774 EXPORT_SYMBOL(blk_hw_contig_segment
);
2775 EXPORT_SYMBOL(blk_get_request
);
2776 EXPORT_SYMBOL(blk_put_request
);
2777 EXPORT_SYMBOL(blk_insert_request
);
2778 EXPORT_SYMBOL(blk_requeue_request
);
2780 EXPORT_SYMBOL(blk_queue_prep_rq
);
2781 EXPORT_SYMBOL(blk_queue_merge_bvec
);
2783 EXPORT_SYMBOL(blk_queue_find_tag
);
2784 EXPORT_SYMBOL(blk_queue_init_tags
);
2785 EXPORT_SYMBOL(blk_queue_free_tags
);
2786 EXPORT_SYMBOL(blk_queue_start_tag
);
2787 EXPORT_SYMBOL(blk_queue_end_tag
);
2788 EXPORT_SYMBOL(blk_queue_invalidate_tags
);
2790 EXPORT_SYMBOL(blk_start_queue
);
2791 EXPORT_SYMBOL(blk_stop_queue
);
2792 EXPORT_SYMBOL(blk_run_queue
);
2793 EXPORT_SYMBOL(blk_run_queues
);
2795 EXPORT_SYMBOL(blk_rq_bio_prep
);