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/interrupt.h>
30 #include <linux/cpu.h>
35 #include <scsi/scsi_cmnd.h>
37 static void blk_unplug_work(void *data);
38 static void blk_unplug_timeout(unsigned long data);
39 static void drive_stat_acct(struct request *rq, int nr_sectors, int new_io);
40 static void init_request_from_bio(struct request *req, struct bio *bio);
41 static int __make_request(request_queue_t *q, struct bio *bio);
44 * For the allocated request tables
46 static kmem_cache_t *request_cachep;
49 * For queue allocation
51 static kmem_cache_t *requestq_cachep;
54 * For io context allocations
56 static kmem_cache_t *iocontext_cachep;
58 static wait_queue_head_t congestion_wqh[2] = {
59 __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[0]),
60 __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[1])
64 * Controlling structure to kblockd
66 static struct workqueue_struct *kblockd_workqueue;
68 unsigned long blk_max_low_pfn, blk_max_pfn;
70 EXPORT_SYMBOL(blk_max_low_pfn);
71 EXPORT_SYMBOL(blk_max_pfn);
73 static DEFINE_PER_CPU(struct list_head, blk_cpu_done);
75 /* Amount of time in which a process may batch requests */
76 #define BLK_BATCH_TIME (HZ/50UL)
78 /* Number of requests a "batching" process may submit */
79 #define BLK_BATCH_REQ 32
82 * Return the threshold (number of used requests) at which the queue is
83 * considered to be congested. It include a little hysteresis to keep the
84 * context switch rate down.
86 static inline int queue_congestion_on_threshold(struct request_queue *q)
88 return q->nr_congestion_on;
92 * The threshold at which a queue is considered to be uncongested
94 static inline int queue_congestion_off_threshold(struct request_queue *q)
96 return q->nr_congestion_off;
99 static void blk_queue_congestion_threshold(struct request_queue *q)
103 nr = q->nr_requests - (q->nr_requests / 8) + 1;
104 if (nr > q->nr_requests)
106 q->nr_congestion_on = nr;
108 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
111 q->nr_congestion_off = nr;
115 * A queue has just exitted congestion. Note this in the global counter of
116 * congested queues, and wake up anyone who was waiting for requests to be
119 static void clear_queue_congested(request_queue_t *q, int rw)
122 wait_queue_head_t *wqh = &congestion_wqh[rw];
124 bit = (rw == WRITE) ? BDI_write_congested : BDI_read_congested;
125 clear_bit(bit, &q->backing_dev_info.state);
126 smp_mb__after_clear_bit();
127 if (waitqueue_active(wqh))
132 * A queue has just entered congestion. Flag that in the queue's VM-visible
133 * state flags and increment the global gounter of congested queues.
135 static void set_queue_congested(request_queue_t *q, int rw)
139 bit = (rw == WRITE) ? BDI_write_congested : BDI_read_congested;
140 set_bit(bit, &q->backing_dev_info.state);
144 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
147 * Locates the passed device's request queue and returns the address of its
150 * Will return NULL if the request queue cannot be located.
152 struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
154 struct backing_dev_info *ret = NULL;
155 request_queue_t *q = bdev_get_queue(bdev);
158 ret = &q->backing_dev_info;
162 EXPORT_SYMBOL(blk_get_backing_dev_info);
164 void blk_queue_activity_fn(request_queue_t *q, activity_fn *fn, void *data)
167 q->activity_data = data;
170 EXPORT_SYMBOL(blk_queue_activity_fn);
173 * blk_queue_prep_rq - set a prepare_request function for queue
175 * @pfn: prepare_request function
177 * It's possible for a queue to register a prepare_request callback which
178 * is invoked before the request is handed to the request_fn. The goal of
179 * the function is to prepare a request for I/O, it can be used to build a
180 * cdb from the request data for instance.
183 void blk_queue_prep_rq(request_queue_t *q, prep_rq_fn *pfn)
188 EXPORT_SYMBOL(blk_queue_prep_rq);
191 * blk_queue_merge_bvec - set a merge_bvec function for queue
193 * @mbfn: merge_bvec_fn
195 * Usually queues have static limitations on the max sectors or segments that
196 * we can put in a request. Stacking drivers may have some settings that
197 * are dynamic, and thus we have to query the queue whether it is ok to
198 * add a new bio_vec to a bio at a given offset or not. If the block device
199 * has such limitations, it needs to register a merge_bvec_fn to control
200 * the size of bio's sent to it. Note that a block device *must* allow a
201 * single page to be added to an empty bio. The block device driver may want
202 * to use the bio_split() function to deal with these bio's. By default
203 * no merge_bvec_fn is defined for a queue, and only the fixed limits are
206 void blk_queue_merge_bvec(request_queue_t *q, merge_bvec_fn *mbfn)
208 q->merge_bvec_fn = mbfn;
211 EXPORT_SYMBOL(blk_queue_merge_bvec);
213 void blk_queue_softirq_done(request_queue_t *q, softirq_done_fn *fn)
215 q->softirq_done_fn = fn;
218 EXPORT_SYMBOL(blk_queue_softirq_done);
221 * blk_queue_make_request - define an alternate make_request function for a device
222 * @q: the request queue for the device to be affected
223 * @mfn: the alternate make_request function
226 * The normal way for &struct bios to be passed to a device
227 * driver is for them to be collected into requests on a request
228 * queue, and then to allow the device driver to select requests
229 * off that queue when it is ready. This works well for many block
230 * devices. However some block devices (typically virtual devices
231 * such as md or lvm) do not benefit from the processing on the
232 * request queue, and are served best by having the requests passed
233 * directly to them. This can be achieved by providing a function
234 * to blk_queue_make_request().
237 * The driver that does this *must* be able to deal appropriately
238 * with buffers in "highmemory". This can be accomplished by either calling
239 * __bio_kmap_atomic() to get a temporary kernel mapping, or by calling
240 * blk_queue_bounce() to create a buffer in normal memory.
242 void blk_queue_make_request(request_queue_t * q, make_request_fn * mfn)
247 q->nr_requests = BLKDEV_MAX_RQ;
248 blk_queue_max_phys_segments(q, MAX_PHYS_SEGMENTS);
249 blk_queue_max_hw_segments(q, MAX_HW_SEGMENTS);
250 q->make_request_fn = mfn;
251 q->backing_dev_info.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
252 q->backing_dev_info.state = 0;
253 q->backing_dev_info.capabilities = BDI_CAP_MAP_COPY;
254 blk_queue_max_sectors(q, SAFE_MAX_SECTORS);
255 blk_queue_hardsect_size(q, 512);
256 blk_queue_dma_alignment(q, 511);
257 blk_queue_congestion_threshold(q);
258 q->nr_batching = BLK_BATCH_REQ;
260 q->unplug_thresh = 4; /* hmm */
261 q->unplug_delay = (3 * HZ) / 1000; /* 3 milliseconds */
262 if (q->unplug_delay == 0)
265 INIT_WORK(&q->unplug_work, blk_unplug_work, q);
267 q->unplug_timer.function = blk_unplug_timeout;
268 q->unplug_timer.data = (unsigned long)q;
271 * by default assume old behaviour and bounce for any highmem page
273 blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
275 blk_queue_activity_fn(q, NULL, NULL);
278 EXPORT_SYMBOL(blk_queue_make_request);
280 static inline void rq_init(request_queue_t *q, struct request *rq)
282 INIT_LIST_HEAD(&rq->queuelist);
283 INIT_LIST_HEAD(&rq->donelist);
286 rq->rq_status = RQ_ACTIVE;
287 rq->bio = rq->biotail = NULL;
296 rq->nr_phys_segments = 0;
299 rq->end_io_data = NULL;
300 rq->completion_data = NULL;
304 * blk_queue_ordered - does this queue support ordered writes
305 * @q: the request queue
306 * @ordered: one of QUEUE_ORDERED_*
309 * For journalled file systems, doing ordered writes on a commit
310 * block instead of explicitly doing wait_on_buffer (which is bad
311 * for performance) can be a big win. Block drivers supporting this
312 * feature should call this function and indicate so.
315 int blk_queue_ordered(request_queue_t *q, unsigned ordered,
316 prepare_flush_fn *prepare_flush_fn)
318 if (ordered & (QUEUE_ORDERED_PREFLUSH | QUEUE_ORDERED_POSTFLUSH) &&
319 prepare_flush_fn == NULL) {
320 printk(KERN_ERR "blk_queue_ordered: prepare_flush_fn required\n");
324 if (ordered != QUEUE_ORDERED_NONE &&
325 ordered != QUEUE_ORDERED_DRAIN &&
326 ordered != QUEUE_ORDERED_DRAIN_FLUSH &&
327 ordered != QUEUE_ORDERED_DRAIN_FUA &&
328 ordered != QUEUE_ORDERED_TAG &&
329 ordered != QUEUE_ORDERED_TAG_FLUSH &&
330 ordered != QUEUE_ORDERED_TAG_FUA) {
331 printk(KERN_ERR "blk_queue_ordered: bad value %d\n", ordered);
335 q->next_ordered = ordered;
336 q->prepare_flush_fn = prepare_flush_fn;
341 EXPORT_SYMBOL(blk_queue_ordered);
344 * blk_queue_issue_flush_fn - set function for issuing a flush
345 * @q: the request queue
346 * @iff: the function to be called issuing the flush
349 * If a driver supports issuing a flush command, the support is notified
350 * to the block layer by defining it through this call.
353 void blk_queue_issue_flush_fn(request_queue_t *q, issue_flush_fn *iff)
355 q->issue_flush_fn = iff;
358 EXPORT_SYMBOL(blk_queue_issue_flush_fn);
361 * Cache flushing for ordered writes handling
363 inline unsigned blk_ordered_cur_seq(request_queue_t *q)
367 return 1 << ffz(q->ordseq);
370 unsigned blk_ordered_req_seq(struct request *rq)
372 request_queue_t *q = rq->q;
374 BUG_ON(q->ordseq == 0);
376 if (rq == &q->pre_flush_rq)
377 return QUEUE_ORDSEQ_PREFLUSH;
378 if (rq == &q->bar_rq)
379 return QUEUE_ORDSEQ_BAR;
380 if (rq == &q->post_flush_rq)
381 return QUEUE_ORDSEQ_POSTFLUSH;
383 if ((rq->flags & REQ_ORDERED_COLOR) ==
384 (q->orig_bar_rq->flags & REQ_ORDERED_COLOR))
385 return QUEUE_ORDSEQ_DRAIN;
387 return QUEUE_ORDSEQ_DONE;
390 void blk_ordered_complete_seq(request_queue_t *q, unsigned seq, int error)
395 if (error && !q->orderr)
398 BUG_ON(q->ordseq & seq);
401 if (blk_ordered_cur_seq(q) != QUEUE_ORDSEQ_DONE)
405 * Okay, sequence complete.
408 uptodate = q->orderr ? q->orderr : 1;
412 end_that_request_first(rq, uptodate, rq->hard_nr_sectors);
413 end_that_request_last(rq, uptodate);
416 static void pre_flush_end_io(struct request *rq, int error)
418 elv_completed_request(rq->q, rq);
419 blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_PREFLUSH, error);
422 static void bar_end_io(struct request *rq, int error)
424 elv_completed_request(rq->q, rq);
425 blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_BAR, error);
428 static void post_flush_end_io(struct request *rq, int error)
430 elv_completed_request(rq->q, rq);
431 blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_POSTFLUSH, error);
434 static void queue_flush(request_queue_t *q, unsigned which)
437 rq_end_io_fn *end_io;
439 if (which == QUEUE_ORDERED_PREFLUSH) {
440 rq = &q->pre_flush_rq;
441 end_io = pre_flush_end_io;
443 rq = &q->post_flush_rq;
444 end_io = post_flush_end_io;
448 rq->flags = REQ_HARDBARRIER;
449 rq->elevator_private = NULL;
450 rq->rq_disk = q->bar_rq.rq_disk;
453 q->prepare_flush_fn(q, rq);
455 __elv_add_request(q, rq, ELEVATOR_INSERT_FRONT, 0);
458 static inline struct request *start_ordered(request_queue_t *q,
463 q->ordered = q->next_ordered;
464 q->ordseq |= QUEUE_ORDSEQ_STARTED;
467 * Prep proxy barrier request.
469 blkdev_dequeue_request(rq);
473 rq->flags = bio_data_dir(q->orig_bar_rq->bio);
474 rq->flags |= q->ordered & QUEUE_ORDERED_FUA ? REQ_FUA : 0;
475 rq->elevator_private = NULL;
477 init_request_from_bio(rq, q->orig_bar_rq->bio);
478 rq->end_io = bar_end_io;
481 * Queue ordered sequence. As we stack them at the head, we
482 * need to queue in reverse order. Note that we rely on that
483 * no fs request uses ELEVATOR_INSERT_FRONT and thus no fs
484 * request gets inbetween ordered sequence.
486 if (q->ordered & QUEUE_ORDERED_POSTFLUSH)
487 queue_flush(q, QUEUE_ORDERED_POSTFLUSH);
489 q->ordseq |= QUEUE_ORDSEQ_POSTFLUSH;
491 __elv_add_request(q, rq, ELEVATOR_INSERT_FRONT, 0);
493 if (q->ordered & QUEUE_ORDERED_PREFLUSH) {
494 queue_flush(q, QUEUE_ORDERED_PREFLUSH);
495 rq = &q->pre_flush_rq;
497 q->ordseq |= QUEUE_ORDSEQ_PREFLUSH;
499 if ((q->ordered & QUEUE_ORDERED_TAG) || q->in_flight == 0)
500 q->ordseq |= QUEUE_ORDSEQ_DRAIN;
507 int blk_do_ordered(request_queue_t *q, struct request **rqp)
509 struct request *rq = *rqp, *allowed_rq;
510 int is_barrier = blk_fs_request(rq) && blk_barrier_rq(rq);
516 if (q->next_ordered != QUEUE_ORDERED_NONE) {
517 *rqp = start_ordered(q, rq);
521 * This can happen when the queue switches to
522 * ORDERED_NONE while this request is on it.
524 blkdev_dequeue_request(rq);
525 end_that_request_first(rq, -EOPNOTSUPP,
526 rq->hard_nr_sectors);
527 end_that_request_last(rq, -EOPNOTSUPP);
533 if (q->ordered & QUEUE_ORDERED_TAG) {
534 if (is_barrier && rq != &q->bar_rq)
539 switch (blk_ordered_cur_seq(q)) {
540 case QUEUE_ORDSEQ_PREFLUSH:
541 allowed_rq = &q->pre_flush_rq;
543 case QUEUE_ORDSEQ_BAR:
544 allowed_rq = &q->bar_rq;
546 case QUEUE_ORDSEQ_POSTFLUSH:
547 allowed_rq = &q->post_flush_rq;
554 if (rq != allowed_rq &&
555 (blk_fs_request(rq) || rq == &q->pre_flush_rq ||
556 rq == &q->post_flush_rq))
562 static int flush_dry_bio_endio(struct bio *bio, unsigned int bytes, int error)
564 request_queue_t *q = bio->bi_private;
565 struct bio_vec *bvec;
569 * This is dry run, restore bio_sector and size. We'll finish
570 * this request again with the original bi_end_io after an
571 * error occurs or post flush is complete.
580 bio_for_each_segment(bvec, bio, i) {
581 bvec->bv_len += bvec->bv_offset;
586 set_bit(BIO_UPTODATE, &bio->bi_flags);
587 bio->bi_size = q->bi_size;
588 bio->bi_sector -= (q->bi_size >> 9);
594 static inline int ordered_bio_endio(struct request *rq, struct bio *bio,
595 unsigned int nbytes, int error)
597 request_queue_t *q = rq->q;
601 if (&q->bar_rq != rq)
605 * Okay, this is the barrier request in progress, dry finish it.
607 if (error && !q->orderr)
610 endio = bio->bi_end_io;
611 private = bio->bi_private;
612 bio->bi_end_io = flush_dry_bio_endio;
615 bio_endio(bio, nbytes, error);
617 bio->bi_end_io = endio;
618 bio->bi_private = private;
624 * blk_queue_bounce_limit - set bounce buffer limit for queue
625 * @q: the request queue for the device
626 * @dma_addr: bus address limit
629 * Different hardware can have different requirements as to what pages
630 * it can do I/O directly to. A low level driver can call
631 * blk_queue_bounce_limit to have lower memory pages allocated as bounce
632 * buffers for doing I/O to pages residing above @page. By default
633 * the block layer sets this to the highest numbered "low" memory page.
635 void blk_queue_bounce_limit(request_queue_t *q, u64 dma_addr)
637 unsigned long bounce_pfn = dma_addr >> PAGE_SHIFT;
640 * set appropriate bounce gfp mask -- unfortunately we don't have a
641 * full 4GB zone, so we have to resort to low memory for any bounces.
642 * ISA has its own < 16MB zone.
644 if (bounce_pfn < blk_max_low_pfn) {
645 BUG_ON(dma_addr < BLK_BOUNCE_ISA);
646 init_emergency_isa_pool();
647 q->bounce_gfp = GFP_NOIO | GFP_DMA;
649 q->bounce_gfp = GFP_NOIO;
651 q->bounce_pfn = bounce_pfn;
654 EXPORT_SYMBOL(blk_queue_bounce_limit);
657 * blk_queue_max_sectors - set max sectors for a request for this queue
658 * @q: the request queue for the device
659 * @max_sectors: max sectors in the usual 512b unit
662 * Enables a low level driver to set an upper limit on the size of
665 void blk_queue_max_sectors(request_queue_t *q, unsigned short max_sectors)
667 if ((max_sectors << 9) < PAGE_CACHE_SIZE) {
668 max_sectors = 1 << (PAGE_CACHE_SHIFT - 9);
669 printk("%s: set to minimum %d\n", __FUNCTION__, max_sectors);
672 if (BLK_DEF_MAX_SECTORS > max_sectors)
673 q->max_hw_sectors = q->max_sectors = max_sectors;
675 q->max_sectors = BLK_DEF_MAX_SECTORS;
676 q->max_hw_sectors = max_sectors;
680 EXPORT_SYMBOL(blk_queue_max_sectors);
683 * blk_queue_max_phys_segments - set max phys segments for a request for this queue
684 * @q: the request queue for the device
685 * @max_segments: max number of segments
688 * Enables a low level driver to set an upper limit on the number of
689 * physical data segments in a request. This would be the largest sized
690 * scatter list the driver could handle.
692 void blk_queue_max_phys_segments(request_queue_t *q, unsigned short max_segments)
696 printk("%s: set to minimum %d\n", __FUNCTION__, max_segments);
699 q->max_phys_segments = max_segments;
702 EXPORT_SYMBOL(blk_queue_max_phys_segments);
705 * blk_queue_max_hw_segments - set max hw segments for a request for this queue
706 * @q: the request queue for the device
707 * @max_segments: max number of segments
710 * Enables a low level driver to set an upper limit on the number of
711 * hw data segments in a request. This would be the largest number of
712 * address/length pairs the host adapter can actually give as once
715 void blk_queue_max_hw_segments(request_queue_t *q, unsigned short max_segments)
719 printk("%s: set to minimum %d\n", __FUNCTION__, max_segments);
722 q->max_hw_segments = max_segments;
725 EXPORT_SYMBOL(blk_queue_max_hw_segments);
728 * blk_queue_max_segment_size - set max segment size for blk_rq_map_sg
729 * @q: the request queue for the device
730 * @max_size: max size of segment in bytes
733 * Enables a low level driver to set an upper limit on the size of a
736 void blk_queue_max_segment_size(request_queue_t *q, unsigned int max_size)
738 if (max_size < PAGE_CACHE_SIZE) {
739 max_size = PAGE_CACHE_SIZE;
740 printk("%s: set to minimum %d\n", __FUNCTION__, max_size);
743 q->max_segment_size = max_size;
746 EXPORT_SYMBOL(blk_queue_max_segment_size);
749 * blk_queue_hardsect_size - set hardware sector size for the queue
750 * @q: the request queue for the device
751 * @size: the hardware sector size, in bytes
754 * This should typically be set to the lowest possible sector size
755 * that the hardware can operate on (possible without reverting to
756 * even internal read-modify-write operations). Usually the default
757 * of 512 covers most hardware.
759 void blk_queue_hardsect_size(request_queue_t *q, unsigned short size)
761 q->hardsect_size = size;
764 EXPORT_SYMBOL(blk_queue_hardsect_size);
767 * Returns the minimum that is _not_ zero, unless both are zero.
769 #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
772 * blk_queue_stack_limits - inherit underlying queue limits for stacked drivers
773 * @t: the stacking driver (top)
774 * @b: the underlying device (bottom)
776 void blk_queue_stack_limits(request_queue_t *t, request_queue_t *b)
778 /* zero is "infinity" */
779 t->max_sectors = min_not_zero(t->max_sectors,b->max_sectors);
780 t->max_hw_sectors = min_not_zero(t->max_hw_sectors,b->max_hw_sectors);
782 t->max_phys_segments = min(t->max_phys_segments,b->max_phys_segments);
783 t->max_hw_segments = min(t->max_hw_segments,b->max_hw_segments);
784 t->max_segment_size = min(t->max_segment_size,b->max_segment_size);
785 t->hardsect_size = max(t->hardsect_size,b->hardsect_size);
788 EXPORT_SYMBOL(blk_queue_stack_limits);
791 * blk_queue_segment_boundary - set boundary rules for segment merging
792 * @q: the request queue for the device
793 * @mask: the memory boundary mask
795 void blk_queue_segment_boundary(request_queue_t *q, unsigned long mask)
797 if (mask < PAGE_CACHE_SIZE - 1) {
798 mask = PAGE_CACHE_SIZE - 1;
799 printk("%s: set to minimum %lx\n", __FUNCTION__, mask);
802 q->seg_boundary_mask = mask;
805 EXPORT_SYMBOL(blk_queue_segment_boundary);
808 * blk_queue_dma_alignment - set dma length and memory alignment
809 * @q: the request queue for the device
810 * @mask: alignment mask
813 * set required memory and length aligment for direct dma transactions.
814 * this is used when buiding direct io requests for the queue.
817 void blk_queue_dma_alignment(request_queue_t *q, int mask)
819 q->dma_alignment = mask;
822 EXPORT_SYMBOL(blk_queue_dma_alignment);
825 * blk_queue_find_tag - find a request by its tag and queue
826 * @q: The request queue for the device
827 * @tag: The tag of the request
830 * Should be used when a device returns a tag and you want to match
833 * no locks need be held.
835 struct request *blk_queue_find_tag(request_queue_t *q, int tag)
837 struct blk_queue_tag *bqt = q->queue_tags;
839 if (unlikely(bqt == NULL || tag >= bqt->real_max_depth))
842 return bqt->tag_index[tag];
845 EXPORT_SYMBOL(blk_queue_find_tag);
848 * __blk_queue_free_tags - release tag maintenance info
849 * @q: the request queue for the device
852 * blk_cleanup_queue() will take care of calling this function, if tagging
853 * has been used. So there's no need to call this directly.
855 static void __blk_queue_free_tags(request_queue_t *q)
857 struct blk_queue_tag *bqt = q->queue_tags;
862 if (atomic_dec_and_test(&bqt->refcnt)) {
864 BUG_ON(!list_empty(&bqt->busy_list));
866 kfree(bqt->tag_index);
867 bqt->tag_index = NULL;
875 q->queue_tags = NULL;
876 q->queue_flags &= ~(1 << QUEUE_FLAG_QUEUED);
880 * blk_queue_free_tags - release tag maintenance info
881 * @q: the request queue for the device
884 * This is used to disabled tagged queuing to a device, yet leave
887 void blk_queue_free_tags(request_queue_t *q)
889 clear_bit(QUEUE_FLAG_QUEUED, &q->queue_flags);
892 EXPORT_SYMBOL(blk_queue_free_tags);
895 init_tag_map(request_queue_t *q, struct blk_queue_tag *tags, int depth)
897 struct request **tag_index;
898 unsigned long *tag_map;
901 if (depth > q->nr_requests * 2) {
902 depth = q->nr_requests * 2;
903 printk(KERN_ERR "%s: adjusted depth to %d\n",
904 __FUNCTION__, depth);
907 tag_index = kmalloc(depth * sizeof(struct request *), GFP_ATOMIC);
911 nr_ulongs = ALIGN(depth, BITS_PER_LONG) / BITS_PER_LONG;
912 tag_map = kmalloc(nr_ulongs * sizeof(unsigned long), GFP_ATOMIC);
916 memset(tag_index, 0, depth * sizeof(struct request *));
917 memset(tag_map, 0, nr_ulongs * sizeof(unsigned long));
918 tags->real_max_depth = depth;
919 tags->max_depth = depth;
920 tags->tag_index = tag_index;
921 tags->tag_map = tag_map;
930 * blk_queue_init_tags - initialize the queue tag info
931 * @q: the request queue for the device
932 * @depth: the maximum queue depth supported
933 * @tags: the tag to use
935 int blk_queue_init_tags(request_queue_t *q, int depth,
936 struct blk_queue_tag *tags)
940 BUG_ON(tags && q->queue_tags && tags != q->queue_tags);
942 if (!tags && !q->queue_tags) {
943 tags = kmalloc(sizeof(struct blk_queue_tag), GFP_ATOMIC);
947 if (init_tag_map(q, tags, depth))
950 INIT_LIST_HEAD(&tags->busy_list);
952 atomic_set(&tags->refcnt, 1);
953 } else if (q->queue_tags) {
954 if ((rc = blk_queue_resize_tags(q, depth)))
956 set_bit(QUEUE_FLAG_QUEUED, &q->queue_flags);
959 atomic_inc(&tags->refcnt);
962 * assign it, all done
964 q->queue_tags = tags;
965 q->queue_flags |= (1 << QUEUE_FLAG_QUEUED);
972 EXPORT_SYMBOL(blk_queue_init_tags);
975 * blk_queue_resize_tags - change the queueing depth
976 * @q: the request queue for the device
977 * @new_depth: the new max command queueing depth
980 * Must be called with the queue lock held.
982 int blk_queue_resize_tags(request_queue_t *q, int new_depth)
984 struct blk_queue_tag *bqt = q->queue_tags;
985 struct request **tag_index;
986 unsigned long *tag_map;
987 int max_depth, nr_ulongs;
993 * if we already have large enough real_max_depth. just
994 * adjust max_depth. *NOTE* as requests with tag value
995 * between new_depth and real_max_depth can be in-flight, tag
996 * map can not be shrunk blindly here.
998 if (new_depth <= bqt->real_max_depth) {
999 bqt->max_depth = new_depth;
1004 * save the old state info, so we can copy it back
1006 tag_index = bqt->tag_index;
1007 tag_map = bqt->tag_map;
1008 max_depth = bqt->real_max_depth;
1010 if (init_tag_map(q, bqt, new_depth))
1013 memcpy(bqt->tag_index, tag_index, max_depth * sizeof(struct request *));
1014 nr_ulongs = ALIGN(max_depth, BITS_PER_LONG) / BITS_PER_LONG;
1015 memcpy(bqt->tag_map, tag_map, nr_ulongs * sizeof(unsigned long));
1022 EXPORT_SYMBOL(blk_queue_resize_tags);
1025 * blk_queue_end_tag - end tag operations for a request
1026 * @q: the request queue for the device
1027 * @rq: the request that has completed
1030 * Typically called when end_that_request_first() returns 0, meaning
1031 * all transfers have been done for a request. It's important to call
1032 * this function before end_that_request_last(), as that will put the
1033 * request back on the free list thus corrupting the internal tag list.
1036 * queue lock must be held.
1038 void blk_queue_end_tag(request_queue_t *q, struct request *rq)
1040 struct blk_queue_tag *bqt = q->queue_tags;
1045 if (unlikely(tag >= bqt->real_max_depth))
1047 * This can happen after tag depth has been reduced.
1048 * FIXME: how about a warning or info message here?
1052 if (unlikely(!__test_and_clear_bit(tag, bqt->tag_map))) {
1053 printk(KERN_ERR "%s: attempt to clear non-busy tag (%d)\n",
1058 list_del_init(&rq->queuelist);
1059 rq->flags &= ~REQ_QUEUED;
1062 if (unlikely(bqt->tag_index[tag] == NULL))
1063 printk(KERN_ERR "%s: tag %d is missing\n",
1066 bqt->tag_index[tag] = NULL;
1070 EXPORT_SYMBOL(blk_queue_end_tag);
1073 * blk_queue_start_tag - find a free tag and assign it
1074 * @q: the request queue for the device
1075 * @rq: the block request that needs tagging
1078 * This can either be used as a stand-alone helper, or possibly be
1079 * assigned as the queue &prep_rq_fn (in which case &struct request
1080 * automagically gets a tag assigned). Note that this function
1081 * assumes that any type of request can be queued! if this is not
1082 * true for your device, you must check the request type before
1083 * calling this function. The request will also be removed from
1084 * the request queue, so it's the drivers responsibility to readd
1085 * it if it should need to be restarted for some reason.
1088 * queue lock must be held.
1090 int blk_queue_start_tag(request_queue_t *q, struct request *rq)
1092 struct blk_queue_tag *bqt = q->queue_tags;
1095 if (unlikely((rq->flags & REQ_QUEUED))) {
1097 "%s: request %p for device [%s] already tagged %d",
1099 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->tag);
1103 tag = find_first_zero_bit(bqt->tag_map, bqt->max_depth);
1104 if (tag >= bqt->max_depth)
1107 __set_bit(tag, bqt->tag_map);
1109 rq->flags |= REQ_QUEUED;
1111 bqt->tag_index[tag] = rq;
1112 blkdev_dequeue_request(rq);
1113 list_add(&rq->queuelist, &bqt->busy_list);
1118 EXPORT_SYMBOL(blk_queue_start_tag);
1121 * blk_queue_invalidate_tags - invalidate all pending tags
1122 * @q: the request queue for the device
1125 * Hardware conditions may dictate a need to stop all pending requests.
1126 * In this case, we will safely clear the block side of the tag queue and
1127 * readd all requests to the request queue in the right order.
1130 * queue lock must be held.
1132 void blk_queue_invalidate_tags(request_queue_t *q)
1134 struct blk_queue_tag *bqt = q->queue_tags;
1135 struct list_head *tmp, *n;
1138 list_for_each_safe(tmp, n, &bqt->busy_list) {
1139 rq = list_entry_rq(tmp);
1141 if (rq->tag == -1) {
1143 "%s: bad tag found on list\n", __FUNCTION__);
1144 list_del_init(&rq->queuelist);
1145 rq->flags &= ~REQ_QUEUED;
1147 blk_queue_end_tag(q, rq);
1149 rq->flags &= ~REQ_STARTED;
1150 __elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 0);
1154 EXPORT_SYMBOL(blk_queue_invalidate_tags);
1156 static const char * const rq_flags[] = {
1177 "REQ_DRIVE_TASKFILE",
1182 "REQ_ORDERED_COLOR",
1185 void blk_dump_rq_flags(struct request *rq, char *msg)
1189 printk("%s: dev %s: flags = ", msg,
1190 rq->rq_disk ? rq->rq_disk->disk_name : "?");
1193 if (rq->flags & (1 << bit))
1194 printk("%s ", rq_flags[bit]);
1196 } while (bit < __REQ_NR_BITS);
1198 printk("\nsector %llu, nr/cnr %lu/%u\n", (unsigned long long)rq->sector,
1200 rq->current_nr_sectors);
1201 printk("bio %p, biotail %p, buffer %p, data %p, len %u\n", rq->bio, rq->biotail, rq->buffer, rq->data, rq->data_len);
1203 if (rq->flags & (REQ_BLOCK_PC | REQ_PC)) {
1205 for (bit = 0; bit < sizeof(rq->cmd); bit++)
1206 printk("%02x ", rq->cmd[bit]);
1211 EXPORT_SYMBOL(blk_dump_rq_flags);
1213 void blk_recount_segments(request_queue_t *q, struct bio *bio)
1215 struct bio_vec *bv, *bvprv = NULL;
1216 int i, nr_phys_segs, nr_hw_segs, seg_size, hw_seg_size, cluster;
1217 int high, highprv = 1;
1219 if (unlikely(!bio->bi_io_vec))
1222 cluster = q->queue_flags & (1 << QUEUE_FLAG_CLUSTER);
1223 hw_seg_size = seg_size = nr_phys_segs = nr_hw_segs = 0;
1224 bio_for_each_segment(bv, bio, i) {
1226 * the trick here is making sure that a high page is never
1227 * considered part of another segment, since that might
1228 * change with the bounce page.
1230 high = page_to_pfn(bv->bv_page) >= q->bounce_pfn;
1231 if (high || highprv)
1232 goto new_hw_segment;
1234 if (seg_size + bv->bv_len > q->max_segment_size)
1236 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bv))
1238 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bv))
1240 if (BIOVEC_VIRT_OVERSIZE(hw_seg_size + bv->bv_len))
1241 goto new_hw_segment;
1243 seg_size += bv->bv_len;
1244 hw_seg_size += bv->bv_len;
1249 if (BIOVEC_VIRT_MERGEABLE(bvprv, bv) &&
1250 !BIOVEC_VIRT_OVERSIZE(hw_seg_size + bv->bv_len)) {
1251 hw_seg_size += bv->bv_len;
1254 if (hw_seg_size > bio->bi_hw_front_size)
1255 bio->bi_hw_front_size = hw_seg_size;
1256 hw_seg_size = BIOVEC_VIRT_START_SIZE(bv) + bv->bv_len;
1262 seg_size = bv->bv_len;
1265 if (hw_seg_size > bio->bi_hw_back_size)
1266 bio->bi_hw_back_size = hw_seg_size;
1267 if (nr_hw_segs == 1 && hw_seg_size > bio->bi_hw_front_size)
1268 bio->bi_hw_front_size = hw_seg_size;
1269 bio->bi_phys_segments = nr_phys_segs;
1270 bio->bi_hw_segments = nr_hw_segs;
1271 bio->bi_flags |= (1 << BIO_SEG_VALID);
1275 static int blk_phys_contig_segment(request_queue_t *q, struct bio *bio,
1278 if (!(q->queue_flags & (1 << QUEUE_FLAG_CLUSTER)))
1281 if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)))
1283 if (bio->bi_size + nxt->bi_size > q->max_segment_size)
1287 * bio and nxt are contigous in memory, check if the queue allows
1288 * these two to be merged into one
1290 if (BIO_SEG_BOUNDARY(q, bio, nxt))
1296 static int blk_hw_contig_segment(request_queue_t *q, struct bio *bio,
1299 if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
1300 blk_recount_segments(q, bio);
1301 if (unlikely(!bio_flagged(nxt, BIO_SEG_VALID)))
1302 blk_recount_segments(q, nxt);
1303 if (!BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)) ||
1304 BIOVEC_VIRT_OVERSIZE(bio->bi_hw_front_size + bio->bi_hw_back_size))
1306 if (bio->bi_size + nxt->bi_size > q->max_segment_size)
1313 * map a request to scatterlist, return number of sg entries setup. Caller
1314 * must make sure sg can hold rq->nr_phys_segments entries
1316 int blk_rq_map_sg(request_queue_t *q, struct request *rq, struct scatterlist *sg)
1318 struct bio_vec *bvec, *bvprv;
1320 int nsegs, i, cluster;
1323 cluster = q->queue_flags & (1 << QUEUE_FLAG_CLUSTER);
1326 * for each bio in rq
1329 rq_for_each_bio(bio, rq) {
1331 * for each segment in bio
1333 bio_for_each_segment(bvec, bio, i) {
1334 int nbytes = bvec->bv_len;
1336 if (bvprv && cluster) {
1337 if (sg[nsegs - 1].length + nbytes > q->max_segment_size)
1340 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
1342 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
1345 sg[nsegs - 1].length += nbytes;
1348 memset(&sg[nsegs],0,sizeof(struct scatterlist));
1349 sg[nsegs].page = bvec->bv_page;
1350 sg[nsegs].length = nbytes;
1351 sg[nsegs].offset = bvec->bv_offset;
1356 } /* segments in bio */
1362 EXPORT_SYMBOL(blk_rq_map_sg);
1365 * the standard queue merge functions, can be overridden with device
1366 * specific ones if so desired
1369 static inline int ll_new_mergeable(request_queue_t *q,
1370 struct request *req,
1373 int nr_phys_segs = bio_phys_segments(q, bio);
1375 if (req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
1376 req->flags |= REQ_NOMERGE;
1377 if (req == q->last_merge)
1378 q->last_merge = NULL;
1383 * A hw segment is just getting larger, bump just the phys
1386 req->nr_phys_segments += nr_phys_segs;
1390 static inline int ll_new_hw_segment(request_queue_t *q,
1391 struct request *req,
1394 int nr_hw_segs = bio_hw_segments(q, bio);
1395 int nr_phys_segs = bio_phys_segments(q, bio);
1397 if (req->nr_hw_segments + nr_hw_segs > q->max_hw_segments
1398 || req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
1399 req->flags |= REQ_NOMERGE;
1400 if (req == q->last_merge)
1401 q->last_merge = NULL;
1406 * This will form the start of a new hw segment. Bump both
1409 req->nr_hw_segments += nr_hw_segs;
1410 req->nr_phys_segments += nr_phys_segs;
1414 static int ll_back_merge_fn(request_queue_t *q, struct request *req,
1417 unsigned short max_sectors;
1420 if (unlikely(blk_pc_request(req)))
1421 max_sectors = q->max_hw_sectors;
1423 max_sectors = q->max_sectors;
1425 if (req->nr_sectors + bio_sectors(bio) > max_sectors) {
1426 req->flags |= REQ_NOMERGE;
1427 if (req == q->last_merge)
1428 q->last_merge = NULL;
1431 if (unlikely(!bio_flagged(req->biotail, BIO_SEG_VALID)))
1432 blk_recount_segments(q, req->biotail);
1433 if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
1434 blk_recount_segments(q, bio);
1435 len = req->biotail->bi_hw_back_size + bio->bi_hw_front_size;
1436 if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(req->biotail), __BVEC_START(bio)) &&
1437 !BIOVEC_VIRT_OVERSIZE(len)) {
1438 int mergeable = ll_new_mergeable(q, req, bio);
1441 if (req->nr_hw_segments == 1)
1442 req->bio->bi_hw_front_size = len;
1443 if (bio->bi_hw_segments == 1)
1444 bio->bi_hw_back_size = len;
1449 return ll_new_hw_segment(q, req, bio);
1452 static int ll_front_merge_fn(request_queue_t *q, struct request *req,
1455 unsigned short max_sectors;
1458 if (unlikely(blk_pc_request(req)))
1459 max_sectors = q->max_hw_sectors;
1461 max_sectors = q->max_sectors;
1464 if (req->nr_sectors + bio_sectors(bio) > max_sectors) {
1465 req->flags |= REQ_NOMERGE;
1466 if (req == q->last_merge)
1467 q->last_merge = NULL;
1470 len = bio->bi_hw_back_size + req->bio->bi_hw_front_size;
1471 if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
1472 blk_recount_segments(q, bio);
1473 if (unlikely(!bio_flagged(req->bio, BIO_SEG_VALID)))
1474 blk_recount_segments(q, req->bio);
1475 if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio), __BVEC_START(req->bio)) &&
1476 !BIOVEC_VIRT_OVERSIZE(len)) {
1477 int mergeable = ll_new_mergeable(q, req, bio);
1480 if (bio->bi_hw_segments == 1)
1481 bio->bi_hw_front_size = len;
1482 if (req->nr_hw_segments == 1)
1483 req->biotail->bi_hw_back_size = len;
1488 return ll_new_hw_segment(q, req, bio);
1491 static int ll_merge_requests_fn(request_queue_t *q, struct request *req,
1492 struct request *next)
1494 int total_phys_segments;
1495 int total_hw_segments;
1498 * First check if the either of the requests are re-queued
1499 * requests. Can't merge them if they are.
1501 if (req->special || next->special)
1505 * Will it become too large?
1507 if ((req->nr_sectors + next->nr_sectors) > q->max_sectors)
1510 total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
1511 if (blk_phys_contig_segment(q, req->biotail, next->bio))
1512 total_phys_segments--;
1514 if (total_phys_segments > q->max_phys_segments)
1517 total_hw_segments = req->nr_hw_segments + next->nr_hw_segments;
1518 if (blk_hw_contig_segment(q, req->biotail, next->bio)) {
1519 int len = req->biotail->bi_hw_back_size + next->bio->bi_hw_front_size;
1521 * propagate the combined length to the end of the requests
1523 if (req->nr_hw_segments == 1)
1524 req->bio->bi_hw_front_size = len;
1525 if (next->nr_hw_segments == 1)
1526 next->biotail->bi_hw_back_size = len;
1527 total_hw_segments--;
1530 if (total_hw_segments > q->max_hw_segments)
1533 /* Merge is OK... */
1534 req->nr_phys_segments = total_phys_segments;
1535 req->nr_hw_segments = total_hw_segments;
1540 * "plug" the device if there are no outstanding requests: this will
1541 * force the transfer to start only after we have put all the requests
1544 * This is called with interrupts off and no requests on the queue and
1545 * with the queue lock held.
1547 void blk_plug_device(request_queue_t *q)
1549 WARN_ON(!irqs_disabled());
1552 * don't plug a stopped queue, it must be paired with blk_start_queue()
1553 * which will restart the queueing
1555 if (test_bit(QUEUE_FLAG_STOPPED, &q->queue_flags))
1558 if (!test_and_set_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags))
1559 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
1562 EXPORT_SYMBOL(blk_plug_device);
1565 * remove the queue from the plugged list, if present. called with
1566 * queue lock held and interrupts disabled.
1568 int blk_remove_plug(request_queue_t *q)
1570 WARN_ON(!irqs_disabled());
1572 if (!test_and_clear_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags))
1575 del_timer(&q->unplug_timer);
1579 EXPORT_SYMBOL(blk_remove_plug);
1582 * remove the plug and let it rip..
1584 void __generic_unplug_device(request_queue_t *q)
1586 if (unlikely(test_bit(QUEUE_FLAG_STOPPED, &q->queue_flags)))
1589 if (!blk_remove_plug(q))
1594 EXPORT_SYMBOL(__generic_unplug_device);
1597 * generic_unplug_device - fire a request queue
1598 * @q: The &request_queue_t in question
1601 * Linux uses plugging to build bigger requests queues before letting
1602 * the device have at them. If a queue is plugged, the I/O scheduler
1603 * is still adding and merging requests on the queue. Once the queue
1604 * gets unplugged, the request_fn defined for the queue is invoked and
1605 * transfers started.
1607 void generic_unplug_device(request_queue_t *q)
1609 spin_lock_irq(q->queue_lock);
1610 __generic_unplug_device(q);
1611 spin_unlock_irq(q->queue_lock);
1613 EXPORT_SYMBOL(generic_unplug_device);
1615 static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
1618 request_queue_t *q = bdi->unplug_io_data;
1621 * devices don't necessarily have an ->unplug_fn defined
1627 static void blk_unplug_work(void *data)
1629 request_queue_t *q = data;
1634 static void blk_unplug_timeout(unsigned long data)
1636 request_queue_t *q = (request_queue_t *)data;
1638 kblockd_schedule_work(&q->unplug_work);
1642 * blk_start_queue - restart a previously stopped queue
1643 * @q: The &request_queue_t in question
1646 * blk_start_queue() will clear the stop flag on the queue, and call
1647 * the request_fn for the queue if it was in a stopped state when
1648 * entered. Also see blk_stop_queue(). Queue lock must be held.
1650 void blk_start_queue(request_queue_t *q)
1652 clear_bit(QUEUE_FLAG_STOPPED, &q->queue_flags);
1655 * one level of recursion is ok and is much faster than kicking
1656 * the unplug handling
1658 if (!test_and_set_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) {
1660 clear_bit(QUEUE_FLAG_REENTER, &q->queue_flags);
1663 kblockd_schedule_work(&q->unplug_work);
1667 EXPORT_SYMBOL(blk_start_queue);
1670 * blk_stop_queue - stop a queue
1671 * @q: The &request_queue_t in question
1674 * The Linux block layer assumes that a block driver will consume all
1675 * entries on the request queue when the request_fn strategy is called.
1676 * Often this will not happen, because of hardware limitations (queue
1677 * depth settings). If a device driver gets a 'queue full' response,
1678 * or if it simply chooses not to queue more I/O at one point, it can
1679 * call this function to prevent the request_fn from being called until
1680 * the driver has signalled it's ready to go again. This happens by calling
1681 * blk_start_queue() to restart queue operations. Queue lock must be held.
1683 void blk_stop_queue(request_queue_t *q)
1686 set_bit(QUEUE_FLAG_STOPPED, &q->queue_flags);
1688 EXPORT_SYMBOL(blk_stop_queue);
1691 * blk_sync_queue - cancel any pending callbacks on a queue
1695 * The block layer may perform asynchronous callback activity
1696 * on a queue, such as calling the unplug function after a timeout.
1697 * A block device may call blk_sync_queue to ensure that any
1698 * such activity is cancelled, thus allowing it to release resources
1699 * the the callbacks might use. The caller must already have made sure
1700 * that its ->make_request_fn will not re-add plugging prior to calling
1704 void blk_sync_queue(struct request_queue *q)
1706 del_timer_sync(&q->unplug_timer);
1709 EXPORT_SYMBOL(blk_sync_queue);
1712 * blk_run_queue - run a single device queue
1713 * @q: The queue to run
1715 void blk_run_queue(struct request_queue *q)
1717 unsigned long flags;
1719 spin_lock_irqsave(q->queue_lock, flags);
1721 if (!elv_queue_empty(q))
1723 spin_unlock_irqrestore(q->queue_lock, flags);
1725 EXPORT_SYMBOL(blk_run_queue);
1728 * blk_cleanup_queue: - release a &request_queue_t when it is no longer needed
1729 * @q: the request queue to be released
1732 * blk_cleanup_queue is the pair to blk_init_queue() or
1733 * blk_queue_make_request(). It should be called when a request queue is
1734 * being released; typically when a block device is being de-registered.
1735 * Currently, its primary task it to free all the &struct request
1736 * structures that were allocated to the queue and the queue itself.
1739 * Hopefully the low level driver will have finished any
1740 * outstanding requests first...
1742 void blk_cleanup_queue(request_queue_t * q)
1744 struct request_list *rl = &q->rq;
1746 if (!atomic_dec_and_test(&q->refcnt))
1750 elevator_exit(q->elevator);
1755 mempool_destroy(rl->rq_pool);
1758 __blk_queue_free_tags(q);
1760 kmem_cache_free(requestq_cachep, q);
1763 EXPORT_SYMBOL(blk_cleanup_queue);
1765 static int blk_init_free_list(request_queue_t *q)
1767 struct request_list *rl = &q->rq;
1769 rl->count[READ] = rl->count[WRITE] = 0;
1770 rl->starved[READ] = rl->starved[WRITE] = 0;
1772 init_waitqueue_head(&rl->wait[READ]);
1773 init_waitqueue_head(&rl->wait[WRITE]);
1775 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
1776 mempool_free_slab, request_cachep, q->node);
1784 request_queue_t *blk_alloc_queue(gfp_t gfp_mask)
1786 return blk_alloc_queue_node(gfp_mask, -1);
1788 EXPORT_SYMBOL(blk_alloc_queue);
1790 request_queue_t *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
1794 q = kmem_cache_alloc_node(requestq_cachep, gfp_mask, node_id);
1798 memset(q, 0, sizeof(*q));
1799 init_timer(&q->unplug_timer);
1800 atomic_set(&q->refcnt, 1);
1802 q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
1803 q->backing_dev_info.unplug_io_data = q;
1807 EXPORT_SYMBOL(blk_alloc_queue_node);
1810 * blk_init_queue - prepare a request queue for use with a block device
1811 * @rfn: The function to be called to process requests that have been
1812 * placed on the queue.
1813 * @lock: Request queue spin lock
1816 * If a block device wishes to use the standard request handling procedures,
1817 * which sorts requests and coalesces adjacent requests, then it must
1818 * call blk_init_queue(). The function @rfn will be called when there
1819 * are requests on the queue that need to be processed. If the device
1820 * supports plugging, then @rfn may not be called immediately when requests
1821 * are available on the queue, but may be called at some time later instead.
1822 * Plugged queues are generally unplugged when a buffer belonging to one
1823 * of the requests on the queue is needed, or due to memory pressure.
1825 * @rfn is not required, or even expected, to remove all requests off the
1826 * queue, but only as many as it can handle at a time. If it does leave
1827 * requests on the queue, it is responsible for arranging that the requests
1828 * get dealt with eventually.
1830 * The queue spin lock must be held while manipulating the requests on the
1833 * Function returns a pointer to the initialized request queue, or NULL if
1834 * it didn't succeed.
1837 * blk_init_queue() must be paired with a blk_cleanup_queue() call
1838 * when the block device is deactivated (such as at module unload).
1841 request_queue_t *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
1843 return blk_init_queue_node(rfn, lock, -1);
1845 EXPORT_SYMBOL(blk_init_queue);
1848 blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
1850 request_queue_t *q = blk_alloc_queue_node(GFP_KERNEL, node_id);
1856 if (blk_init_free_list(q))
1860 * if caller didn't supply a lock, they get per-queue locking with
1864 spin_lock_init(&q->__queue_lock);
1865 lock = &q->__queue_lock;
1868 q->request_fn = rfn;
1869 q->back_merge_fn = ll_back_merge_fn;
1870 q->front_merge_fn = ll_front_merge_fn;
1871 q->merge_requests_fn = ll_merge_requests_fn;
1872 q->prep_rq_fn = NULL;
1873 q->unplug_fn = generic_unplug_device;
1874 q->queue_flags = (1 << QUEUE_FLAG_CLUSTER);
1875 q->queue_lock = lock;
1877 blk_queue_segment_boundary(q, 0xffffffff);
1879 blk_queue_make_request(q, __make_request);
1880 blk_queue_max_segment_size(q, MAX_SEGMENT_SIZE);
1882 blk_queue_max_hw_segments(q, MAX_HW_SEGMENTS);
1883 blk_queue_max_phys_segments(q, MAX_PHYS_SEGMENTS);
1888 if (!elevator_init(q, NULL)) {
1889 blk_queue_congestion_threshold(q);
1893 blk_cleanup_queue(q);
1895 kmem_cache_free(requestq_cachep, q);
1898 EXPORT_SYMBOL(blk_init_queue_node);
1900 int blk_get_queue(request_queue_t *q)
1902 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
1903 atomic_inc(&q->refcnt);
1910 EXPORT_SYMBOL(blk_get_queue);
1912 static inline void blk_free_request(request_queue_t *q, struct request *rq)
1914 if (rq->flags & REQ_ELVPRIV)
1915 elv_put_request(q, rq);
1916 mempool_free(rq, q->rq.rq_pool);
1919 static inline struct request *
1920 blk_alloc_request(request_queue_t *q, int rw, struct bio *bio,
1921 int priv, gfp_t gfp_mask)
1923 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
1929 * first three bits are identical in rq->flags and bio->bi_rw,
1930 * see bio.h and blkdev.h
1935 if (unlikely(elv_set_request(q, rq, bio, gfp_mask))) {
1936 mempool_free(rq, q->rq.rq_pool);
1939 rq->flags |= REQ_ELVPRIV;
1946 * ioc_batching returns true if the ioc is a valid batching request and
1947 * should be given priority access to a request.
1949 static inline int ioc_batching(request_queue_t *q, struct io_context *ioc)
1955 * Make sure the process is able to allocate at least 1 request
1956 * even if the batch times out, otherwise we could theoretically
1959 return ioc->nr_batch_requests == q->nr_batching ||
1960 (ioc->nr_batch_requests > 0
1961 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
1965 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
1966 * will cause the process to be a "batcher" on all queues in the system. This
1967 * is the behaviour we want though - once it gets a wakeup it should be given
1970 static void ioc_set_batching(request_queue_t *q, struct io_context *ioc)
1972 if (!ioc || ioc_batching(q, ioc))
1975 ioc->nr_batch_requests = q->nr_batching;
1976 ioc->last_waited = jiffies;
1979 static void __freed_request(request_queue_t *q, int rw)
1981 struct request_list *rl = &q->rq;
1983 if (rl->count[rw] < queue_congestion_off_threshold(q))
1984 clear_queue_congested(q, rw);
1986 if (rl->count[rw] + 1 <= q->nr_requests) {
1987 if (waitqueue_active(&rl->wait[rw]))
1988 wake_up(&rl->wait[rw]);
1990 blk_clear_queue_full(q, rw);
1995 * A request has just been released. Account for it, update the full and
1996 * congestion status, wake up any waiters. Called under q->queue_lock.
1998 static void freed_request(request_queue_t *q, int rw, int priv)
2000 struct request_list *rl = &q->rq;
2006 __freed_request(q, rw);
2008 if (unlikely(rl->starved[rw ^ 1]))
2009 __freed_request(q, rw ^ 1);
2012 #define blkdev_free_rq(list) list_entry((list)->next, struct request, queuelist)
2014 * Get a free request, queue_lock must be held.
2015 * Returns NULL on failure, with queue_lock held.
2016 * Returns !NULL on success, with queue_lock *not held*.
2018 static struct request *get_request(request_queue_t *q, int rw, struct bio *bio,
2021 struct request *rq = NULL;
2022 struct request_list *rl = &q->rq;
2023 struct io_context *ioc = NULL;
2024 int may_queue, priv;
2026 may_queue = elv_may_queue(q, rw, bio);
2027 if (may_queue == ELV_MQUEUE_NO)
2030 if (rl->count[rw]+1 >= queue_congestion_on_threshold(q)) {
2031 if (rl->count[rw]+1 >= q->nr_requests) {
2032 ioc = current_io_context(GFP_ATOMIC);
2034 * The queue will fill after this allocation, so set
2035 * it as full, and mark this process as "batching".
2036 * This process will be allowed to complete a batch of
2037 * requests, others will be blocked.
2039 if (!blk_queue_full(q, rw)) {
2040 ioc_set_batching(q, ioc);
2041 blk_set_queue_full(q, rw);
2043 if (may_queue != ELV_MQUEUE_MUST
2044 && !ioc_batching(q, ioc)) {
2046 * The queue is full and the allocating
2047 * process is not a "batcher", and not
2048 * exempted by the IO scheduler
2054 set_queue_congested(q, rw);
2058 * Only allow batching queuers to allocate up to 50% over the defined
2059 * limit of requests, otherwise we could have thousands of requests
2060 * allocated with any setting of ->nr_requests
2062 if (rl->count[rw] >= (3 * q->nr_requests / 2))
2066 rl->starved[rw] = 0;
2068 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
2072 spin_unlock_irq(q->queue_lock);
2074 rq = blk_alloc_request(q, rw, bio, priv, gfp_mask);
2075 if (unlikely(!rq)) {
2077 * Allocation failed presumably due to memory. Undo anything
2078 * we might have messed up.
2080 * Allocating task should really be put onto the front of the
2081 * wait queue, but this is pretty rare.
2083 spin_lock_irq(q->queue_lock);
2084 freed_request(q, rw, priv);
2087 * in the very unlikely event that allocation failed and no
2088 * requests for this direction was pending, mark us starved
2089 * so that freeing of a request in the other direction will
2090 * notice us. another possible fix would be to split the
2091 * rq mempool into READ and WRITE
2094 if (unlikely(rl->count[rw] == 0))
2095 rl->starved[rw] = 1;
2101 * ioc may be NULL here, and ioc_batching will be false. That's
2102 * OK, if the queue is under the request limit then requests need
2103 * not count toward the nr_batch_requests limit. There will always
2104 * be some limit enforced by BLK_BATCH_TIME.
2106 if (ioc_batching(q, ioc))
2107 ioc->nr_batch_requests--;
2116 * No available requests for this queue, unplug the device and wait for some
2117 * requests to become available.
2119 * Called with q->queue_lock held, and returns with it unlocked.
2121 static struct request *get_request_wait(request_queue_t *q, int rw,
2126 rq = get_request(q, rw, bio, GFP_NOIO);
2129 struct request_list *rl = &q->rq;
2131 prepare_to_wait_exclusive(&rl->wait[rw], &wait,
2132 TASK_UNINTERRUPTIBLE);
2134 rq = get_request(q, rw, bio, GFP_NOIO);
2137 struct io_context *ioc;
2139 __generic_unplug_device(q);
2140 spin_unlock_irq(q->queue_lock);
2144 * After sleeping, we become a "batching" process and
2145 * will be able to allocate at least one request, and
2146 * up to a big batch of them for a small period time.
2147 * See ioc_batching, ioc_set_batching
2149 ioc = current_io_context(GFP_NOIO);
2150 ioc_set_batching(q, ioc);
2152 spin_lock_irq(q->queue_lock);
2154 finish_wait(&rl->wait[rw], &wait);
2160 struct request *blk_get_request(request_queue_t *q, int rw, gfp_t gfp_mask)
2164 BUG_ON(rw != READ && rw != WRITE);
2166 spin_lock_irq(q->queue_lock);
2167 if (gfp_mask & __GFP_WAIT) {
2168 rq = get_request_wait(q, rw, NULL);
2170 rq = get_request(q, rw, NULL, gfp_mask);
2172 spin_unlock_irq(q->queue_lock);
2174 /* q->queue_lock is unlocked at this point */
2178 EXPORT_SYMBOL(blk_get_request);
2181 * blk_requeue_request - put a request back on queue
2182 * @q: request queue where request should be inserted
2183 * @rq: request to be inserted
2186 * Drivers often keep queueing requests until the hardware cannot accept
2187 * more, when that condition happens we need to put the request back
2188 * on the queue. Must be called with queue lock held.
2190 void blk_requeue_request(request_queue_t *q, struct request *rq)
2192 if (blk_rq_tagged(rq))
2193 blk_queue_end_tag(q, rq);
2195 elv_requeue_request(q, rq);
2198 EXPORT_SYMBOL(blk_requeue_request);
2201 * blk_insert_request - insert a special request in to a request queue
2202 * @q: request queue where request should be inserted
2203 * @rq: request to be inserted
2204 * @at_head: insert request at head or tail of queue
2205 * @data: private data
2208 * Many block devices need to execute commands asynchronously, so they don't
2209 * block the whole kernel from preemption during request execution. This is
2210 * accomplished normally by inserting aritficial requests tagged as
2211 * REQ_SPECIAL in to the corresponding request queue, and letting them be
2212 * scheduled for actual execution by the request queue.
2214 * We have the option of inserting the head or the tail of the queue.
2215 * Typically we use the tail for new ioctls and so forth. We use the head
2216 * of the queue for things like a QUEUE_FULL message from a device, or a
2217 * host that is unable to accept a particular command.
2219 void blk_insert_request(request_queue_t *q, struct request *rq,
2220 int at_head, void *data)
2222 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
2223 unsigned long flags;
2226 * tell I/O scheduler that this isn't a regular read/write (ie it
2227 * must not attempt merges on this) and that it acts as a soft
2230 rq->flags |= REQ_SPECIAL | REQ_SOFTBARRIER;
2234 spin_lock_irqsave(q->queue_lock, flags);
2237 * If command is tagged, release the tag
2239 if (blk_rq_tagged(rq))
2240 blk_queue_end_tag(q, rq);
2242 drive_stat_acct(rq, rq->nr_sectors, 1);
2243 __elv_add_request(q, rq, where, 0);
2245 if (blk_queue_plugged(q))
2246 __generic_unplug_device(q);
2249 spin_unlock_irqrestore(q->queue_lock, flags);
2252 EXPORT_SYMBOL(blk_insert_request);
2255 * blk_rq_map_user - map user data to a request, for REQ_BLOCK_PC usage
2256 * @q: request queue where request should be inserted
2257 * @rq: request structure to fill
2258 * @ubuf: the user buffer
2259 * @len: length of user data
2262 * Data will be mapped directly for zero copy io, if possible. Otherwise
2263 * a kernel bounce buffer is used.
2265 * A matching blk_rq_unmap_user() must be issued at the end of io, while
2266 * still in process context.
2268 * Note: The mapped bio may need to be bounced through blk_queue_bounce()
2269 * before being submitted to the device, as pages mapped may be out of
2270 * reach. It's the callers responsibility to make sure this happens. The
2271 * original bio must be passed back in to blk_rq_unmap_user() for proper
2274 int blk_rq_map_user(request_queue_t *q, struct request *rq, void __user *ubuf,
2277 unsigned long uaddr;
2281 if (len > (q->max_hw_sectors << 9))
2286 reading = rq_data_dir(rq) == READ;
2289 * if alignment requirement is satisfied, map in user pages for
2290 * direct dma. else, set up kernel bounce buffers
2292 uaddr = (unsigned long) ubuf;
2293 if (!(uaddr & queue_dma_alignment(q)) && !(len & queue_dma_alignment(q)))
2294 bio = bio_map_user(q, NULL, uaddr, len, reading);
2296 bio = bio_copy_user(q, uaddr, len, reading);
2299 rq->bio = rq->biotail = bio;
2300 blk_rq_bio_prep(q, rq, bio);
2302 rq->buffer = rq->data = NULL;
2308 * bio is the err-ptr
2310 return PTR_ERR(bio);
2313 EXPORT_SYMBOL(blk_rq_map_user);
2316 * blk_rq_map_user_iov - map user data to a request, for REQ_BLOCK_PC usage
2317 * @q: request queue where request should be inserted
2318 * @rq: request to map data to
2319 * @iov: pointer to the iovec
2320 * @iov_count: number of elements in the iovec
2323 * Data will be mapped directly for zero copy io, if possible. Otherwise
2324 * a kernel bounce buffer is used.
2326 * A matching blk_rq_unmap_user() must be issued at the end of io, while
2327 * still in process context.
2329 * Note: The mapped bio may need to be bounced through blk_queue_bounce()
2330 * before being submitted to the device, as pages mapped may be out of
2331 * reach. It's the callers responsibility to make sure this happens. The
2332 * original bio must be passed back in to blk_rq_unmap_user() for proper
2335 int blk_rq_map_user_iov(request_queue_t *q, struct request *rq,
2336 struct sg_iovec *iov, int iov_count)
2340 if (!iov || iov_count <= 0)
2343 /* we don't allow misaligned data like bio_map_user() does. If the
2344 * user is using sg, they're expected to know the alignment constraints
2345 * and respect them accordingly */
2346 bio = bio_map_user_iov(q, NULL, iov, iov_count, rq_data_dir(rq)== READ);
2348 return PTR_ERR(bio);
2350 rq->bio = rq->biotail = bio;
2351 blk_rq_bio_prep(q, rq, bio);
2352 rq->buffer = rq->data = NULL;
2353 rq->data_len = bio->bi_size;
2357 EXPORT_SYMBOL(blk_rq_map_user_iov);
2360 * blk_rq_unmap_user - unmap a request with user data
2361 * @bio: bio to be unmapped
2362 * @ulen: length of user buffer
2365 * Unmap a bio previously mapped by blk_rq_map_user().
2367 int blk_rq_unmap_user(struct bio *bio, unsigned int ulen)
2372 if (bio_flagged(bio, BIO_USER_MAPPED))
2373 bio_unmap_user(bio);
2375 ret = bio_uncopy_user(bio);
2381 EXPORT_SYMBOL(blk_rq_unmap_user);
2384 * blk_rq_map_kern - map kernel data to a request, for REQ_BLOCK_PC usage
2385 * @q: request queue where request should be inserted
2386 * @rq: request to fill
2387 * @kbuf: the kernel buffer
2388 * @len: length of user data
2389 * @gfp_mask: memory allocation flags
2391 int blk_rq_map_kern(request_queue_t *q, struct request *rq, void *kbuf,
2392 unsigned int len, gfp_t gfp_mask)
2396 if (len > (q->max_hw_sectors << 9))
2401 bio = bio_map_kern(q, kbuf, len, gfp_mask);
2403 return PTR_ERR(bio);
2405 if (rq_data_dir(rq) == WRITE)
2406 bio->bi_rw |= (1 << BIO_RW);
2408 rq->bio = rq->biotail = bio;
2409 blk_rq_bio_prep(q, rq, bio);
2411 rq->buffer = rq->data = NULL;
2416 EXPORT_SYMBOL(blk_rq_map_kern);
2419 * blk_execute_rq_nowait - insert a request into queue for execution
2420 * @q: queue to insert the request in
2421 * @bd_disk: matching gendisk
2422 * @rq: request to insert
2423 * @at_head: insert request at head or tail of queue
2424 * @done: I/O completion handler
2427 * Insert a fully prepared request at the back of the io scheduler queue
2428 * for execution. Don't wait for completion.
2430 void blk_execute_rq_nowait(request_queue_t *q, struct gendisk *bd_disk,
2431 struct request *rq, int at_head,
2434 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
2436 rq->rq_disk = bd_disk;
2437 rq->flags |= REQ_NOMERGE;
2439 elv_add_request(q, rq, where, 1);
2440 generic_unplug_device(q);
2443 EXPORT_SYMBOL_GPL(blk_execute_rq_nowait);
2446 * blk_execute_rq - insert a request into queue for execution
2447 * @q: queue to insert the request in
2448 * @bd_disk: matching gendisk
2449 * @rq: request to insert
2450 * @at_head: insert request at head or tail of queue
2453 * Insert a fully prepared request at the back of the io scheduler queue
2454 * for execution and wait for completion.
2456 int blk_execute_rq(request_queue_t *q, struct gendisk *bd_disk,
2457 struct request *rq, int at_head)
2459 DECLARE_COMPLETION(wait);
2460 char sense[SCSI_SENSE_BUFFERSIZE];
2464 * we need an extra reference to the request, so we can look at
2465 * it after io completion
2470 memset(sense, 0, sizeof(sense));
2475 rq->waiting = &wait;
2476 blk_execute_rq_nowait(q, bd_disk, rq, at_head, blk_end_sync_rq);
2477 wait_for_completion(&wait);
2486 EXPORT_SYMBOL(blk_execute_rq);
2489 * blkdev_issue_flush - queue a flush
2490 * @bdev: blockdev to issue flush for
2491 * @error_sector: error sector
2494 * Issue a flush for the block device in question. Caller can supply
2495 * room for storing the error offset in case of a flush error, if they
2496 * wish to. Caller must run wait_for_completion() on its own.
2498 int blkdev_issue_flush(struct block_device *bdev, sector_t *error_sector)
2502 if (bdev->bd_disk == NULL)
2505 q = bdev_get_queue(bdev);
2508 if (!q->issue_flush_fn)
2511 return q->issue_flush_fn(q, bdev->bd_disk, error_sector);
2514 EXPORT_SYMBOL(blkdev_issue_flush);
2516 static void drive_stat_acct(struct request *rq, int nr_sectors, int new_io)
2518 int rw = rq_data_dir(rq);
2520 if (!blk_fs_request(rq) || !rq->rq_disk)
2524 __disk_stat_inc(rq->rq_disk, merges[rw]);
2526 disk_round_stats(rq->rq_disk);
2527 rq->rq_disk->in_flight++;
2532 * add-request adds a request to the linked list.
2533 * queue lock is held and interrupts disabled, as we muck with the
2534 * request queue list.
2536 static inline void add_request(request_queue_t * q, struct request * req)
2538 drive_stat_acct(req, req->nr_sectors, 1);
2541 q->activity_fn(q->activity_data, rq_data_dir(req));
2544 * elevator indicated where it wants this request to be
2545 * inserted at elevator_merge time
2547 __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
2551 * disk_round_stats() - Round off the performance stats on a struct
2554 * The average IO queue length and utilisation statistics are maintained
2555 * by observing the current state of the queue length and the amount of
2556 * time it has been in this state for.
2558 * Normally, that accounting is done on IO completion, but that can result
2559 * in more than a second's worth of IO being accounted for within any one
2560 * second, leading to >100% utilisation. To deal with that, we call this
2561 * function to do a round-off before returning the results when reading
2562 * /proc/diskstats. This accounts immediately for all queue usage up to
2563 * the current jiffies and restarts the counters again.
2565 void disk_round_stats(struct gendisk *disk)
2567 unsigned long now = jiffies;
2569 if (now == disk->stamp)
2572 if (disk->in_flight) {
2573 __disk_stat_add(disk, time_in_queue,
2574 disk->in_flight * (now - disk->stamp));
2575 __disk_stat_add(disk, io_ticks, (now - disk->stamp));
2581 * queue lock must be held
2583 void __blk_put_request(request_queue_t *q, struct request *req)
2585 struct request_list *rl = req->rl;
2589 if (unlikely(--req->ref_count))
2592 elv_completed_request(q, req);
2594 req->rq_status = RQ_INACTIVE;
2598 * Request may not have originated from ll_rw_blk. if not,
2599 * it didn't come out of our reserved rq pools
2602 int rw = rq_data_dir(req);
2603 int priv = req->flags & REQ_ELVPRIV;
2605 BUG_ON(!list_empty(&req->queuelist));
2607 blk_free_request(q, req);
2608 freed_request(q, rw, priv);
2612 EXPORT_SYMBOL_GPL(__blk_put_request);
2614 void blk_put_request(struct request *req)
2616 unsigned long flags;
2617 request_queue_t *q = req->q;
2620 * Gee, IDE calls in w/ NULL q. Fix IDE and remove the
2621 * following if (q) test.
2624 spin_lock_irqsave(q->queue_lock, flags);
2625 __blk_put_request(q, req);
2626 spin_unlock_irqrestore(q->queue_lock, flags);
2630 EXPORT_SYMBOL(blk_put_request);
2633 * blk_end_sync_rq - executes a completion event on a request
2634 * @rq: request to complete
2636 void blk_end_sync_rq(struct request *rq, int error)
2638 struct completion *waiting = rq->waiting;
2641 __blk_put_request(rq->q, rq);
2644 * complete last, if this is a stack request the process (and thus
2645 * the rq pointer) could be invalid right after this complete()
2649 EXPORT_SYMBOL(blk_end_sync_rq);
2652 * blk_congestion_wait - wait for a queue to become uncongested
2653 * @rw: READ or WRITE
2654 * @timeout: timeout in jiffies
2656 * Waits for up to @timeout jiffies for a queue (any queue) to exit congestion.
2657 * If no queues are congested then just wait for the next request to be
2660 long blk_congestion_wait(int rw, long timeout)
2664 wait_queue_head_t *wqh = &congestion_wqh[rw];
2666 prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
2667 ret = io_schedule_timeout(timeout);
2668 finish_wait(wqh, &wait);
2672 EXPORT_SYMBOL(blk_congestion_wait);
2675 * Has to be called with the request spinlock acquired
2677 static int attempt_merge(request_queue_t *q, struct request *req,
2678 struct request *next)
2680 if (!rq_mergeable(req) || !rq_mergeable(next))
2686 if (req->sector + req->nr_sectors != next->sector)
2689 if (rq_data_dir(req) != rq_data_dir(next)
2690 || req->rq_disk != next->rq_disk
2691 || next->waiting || next->special)
2695 * If we are allowed to merge, then append bio list
2696 * from next to rq and release next. merge_requests_fn
2697 * will have updated segment counts, update sector
2700 if (!q->merge_requests_fn(q, req, next))
2704 * At this point we have either done a back merge
2705 * or front merge. We need the smaller start_time of
2706 * the merged requests to be the current request
2707 * for accounting purposes.
2709 if (time_after(req->start_time, next->start_time))
2710 req->start_time = next->start_time;
2712 req->biotail->bi_next = next->bio;
2713 req->biotail = next->biotail;
2715 req->nr_sectors = req->hard_nr_sectors += next->hard_nr_sectors;
2717 elv_merge_requests(q, req, next);
2720 disk_round_stats(req->rq_disk);
2721 req->rq_disk->in_flight--;
2724 req->ioprio = ioprio_best(req->ioprio, next->ioprio);
2726 __blk_put_request(q, next);
2730 static inline int attempt_back_merge(request_queue_t *q, struct request *rq)
2732 struct request *next = elv_latter_request(q, rq);
2735 return attempt_merge(q, rq, next);
2740 static inline int attempt_front_merge(request_queue_t *q, struct request *rq)
2742 struct request *prev = elv_former_request(q, rq);
2745 return attempt_merge(q, prev, rq);
2750 static void init_request_from_bio(struct request *req, struct bio *bio)
2752 req->flags |= REQ_CMD;
2755 * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
2757 if (bio_rw_ahead(bio) || bio_failfast(bio))
2758 req->flags |= REQ_FAILFAST;
2761 * REQ_BARRIER implies no merging, but lets make it explicit
2763 if (unlikely(bio_barrier(bio)))
2764 req->flags |= (REQ_HARDBARRIER | REQ_NOMERGE);
2767 req->hard_sector = req->sector = bio->bi_sector;
2768 req->hard_nr_sectors = req->nr_sectors = bio_sectors(bio);
2769 req->current_nr_sectors = req->hard_cur_sectors = bio_cur_sectors(bio);
2770 req->nr_phys_segments = bio_phys_segments(req->q, bio);
2771 req->nr_hw_segments = bio_hw_segments(req->q, bio);
2772 req->buffer = bio_data(bio); /* see ->buffer comment above */
2773 req->waiting = NULL;
2774 req->bio = req->biotail = bio;
2775 req->ioprio = bio_prio(bio);
2776 req->rq_disk = bio->bi_bdev->bd_disk;
2777 req->start_time = jiffies;
2780 static int __make_request(request_queue_t *q, struct bio *bio)
2782 struct request *req;
2783 int el_ret, rw, nr_sectors, cur_nr_sectors, barrier, err, sync;
2784 unsigned short prio;
2787 sector = bio->bi_sector;
2788 nr_sectors = bio_sectors(bio);
2789 cur_nr_sectors = bio_cur_sectors(bio);
2790 prio = bio_prio(bio);
2792 rw = bio_data_dir(bio);
2793 sync = bio_sync(bio);
2796 * low level driver can indicate that it wants pages above a
2797 * certain limit bounced to low memory (ie for highmem, or even
2798 * ISA dma in theory)
2800 blk_queue_bounce(q, &bio);
2802 spin_lock_prefetch(q->queue_lock);
2804 barrier = bio_barrier(bio);
2805 if (unlikely(barrier) && (q->next_ordered == QUEUE_ORDERED_NONE)) {
2810 spin_lock_irq(q->queue_lock);
2812 if (unlikely(barrier) || elv_queue_empty(q))
2815 el_ret = elv_merge(q, &req, bio);
2817 case ELEVATOR_BACK_MERGE:
2818 BUG_ON(!rq_mergeable(req));
2820 if (!q->back_merge_fn(q, req, bio))
2823 req->biotail->bi_next = bio;
2825 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
2826 req->ioprio = ioprio_best(req->ioprio, prio);
2827 drive_stat_acct(req, nr_sectors, 0);
2828 if (!attempt_back_merge(q, req))
2829 elv_merged_request(q, req);
2832 case ELEVATOR_FRONT_MERGE:
2833 BUG_ON(!rq_mergeable(req));
2835 if (!q->front_merge_fn(q, req, bio))
2838 bio->bi_next = req->bio;
2842 * may not be valid. if the low level driver said
2843 * it didn't need a bounce buffer then it better
2844 * not touch req->buffer either...
2846 req->buffer = bio_data(bio);
2847 req->current_nr_sectors = cur_nr_sectors;
2848 req->hard_cur_sectors = cur_nr_sectors;
2849 req->sector = req->hard_sector = sector;
2850 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
2851 req->ioprio = ioprio_best(req->ioprio, prio);
2852 drive_stat_acct(req, nr_sectors, 0);
2853 if (!attempt_front_merge(q, req))
2854 elv_merged_request(q, req);
2857 /* ELV_NO_MERGE: elevator says don't/can't merge. */
2864 * Grab a free request. This is might sleep but can not fail.
2865 * Returns with the queue unlocked.
2867 req = get_request_wait(q, rw, bio);
2870 * After dropping the lock and possibly sleeping here, our request
2871 * may now be mergeable after it had proven unmergeable (above).
2872 * We don't worry about that case for efficiency. It won't happen
2873 * often, and the elevators are able to handle it.
2875 init_request_from_bio(req, bio);
2877 spin_lock_irq(q->queue_lock);
2878 if (elv_queue_empty(q))
2880 add_request(q, req);
2883 __generic_unplug_device(q);
2885 spin_unlock_irq(q->queue_lock);
2889 bio_endio(bio, nr_sectors << 9, err);
2894 * If bio->bi_dev is a partition, remap the location
2896 static inline void blk_partition_remap(struct bio *bio)
2898 struct block_device *bdev = bio->bi_bdev;
2900 if (bdev != bdev->bd_contains) {
2901 struct hd_struct *p = bdev->bd_part;
2902 const int rw = bio_data_dir(bio);
2904 p->sectors[rw] += bio_sectors(bio);
2907 bio->bi_sector += p->start_sect;
2908 bio->bi_bdev = bdev->bd_contains;
2912 static void handle_bad_sector(struct bio *bio)
2914 char b[BDEVNAME_SIZE];
2916 printk(KERN_INFO "attempt to access beyond end of device\n");
2917 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
2918 bdevname(bio->bi_bdev, b),
2920 (unsigned long long)bio->bi_sector + bio_sectors(bio),
2921 (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
2923 set_bit(BIO_EOF, &bio->bi_flags);
2927 * generic_make_request: hand a buffer to its device driver for I/O
2928 * @bio: The bio describing the location in memory and on the device.
2930 * generic_make_request() is used to make I/O requests of block
2931 * devices. It is passed a &struct bio, which describes the I/O that needs
2934 * generic_make_request() does not return any status. The
2935 * success/failure status of the request, along with notification of
2936 * completion, is delivered asynchronously through the bio->bi_end_io
2937 * function described (one day) else where.
2939 * The caller of generic_make_request must make sure that bi_io_vec
2940 * are set to describe the memory buffer, and that bi_dev and bi_sector are
2941 * set to describe the device address, and the
2942 * bi_end_io and optionally bi_private are set to describe how
2943 * completion notification should be signaled.
2945 * generic_make_request and the drivers it calls may use bi_next if this
2946 * bio happens to be merged with someone else, and may change bi_dev and
2947 * bi_sector for remaps as it sees fit. So the values of these fields
2948 * should NOT be depended on after the call to generic_make_request.
2950 void generic_make_request(struct bio *bio)
2954 int ret, nr_sectors = bio_sectors(bio);
2957 /* Test device or partition size, when known. */
2958 maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
2960 sector_t sector = bio->bi_sector;
2962 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
2964 * This may well happen - the kernel calls bread()
2965 * without checking the size of the device, e.g., when
2966 * mounting a device.
2968 handle_bad_sector(bio);
2974 * Resolve the mapping until finished. (drivers are
2975 * still free to implement/resolve their own stacking
2976 * by explicitly returning 0)
2978 * NOTE: we don't repeat the blk_size check for each new device.
2979 * Stacking drivers are expected to know what they are doing.
2982 char b[BDEVNAME_SIZE];
2984 q = bdev_get_queue(bio->bi_bdev);
2987 "generic_make_request: Trying to access "
2988 "nonexistent block-device %s (%Lu)\n",
2989 bdevname(bio->bi_bdev, b),
2990 (long long) bio->bi_sector);
2992 bio_endio(bio, bio->bi_size, -EIO);
2996 if (unlikely(bio_sectors(bio) > q->max_hw_sectors)) {
2997 printk("bio too big device %s (%u > %u)\n",
2998 bdevname(bio->bi_bdev, b),
3004 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
3008 * If this device has partitions, remap block n
3009 * of partition p to block n+start(p) of the disk.
3011 blk_partition_remap(bio);
3013 ret = q->make_request_fn(q, bio);
3017 EXPORT_SYMBOL(generic_make_request);
3020 * submit_bio: submit a bio to the block device layer for I/O
3021 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
3022 * @bio: The &struct bio which describes the I/O
3024 * submit_bio() is very similar in purpose to generic_make_request(), and
3025 * uses that function to do most of the work. Both are fairly rough
3026 * interfaces, @bio must be presetup and ready for I/O.
3029 void submit_bio(int rw, struct bio *bio)
3031 int count = bio_sectors(bio);
3033 BIO_BUG_ON(!bio->bi_size);
3034 BIO_BUG_ON(!bio->bi_io_vec);
3037 mod_page_state(pgpgout, count);
3039 mod_page_state(pgpgin, count);
3041 if (unlikely(block_dump)) {
3042 char b[BDEVNAME_SIZE];
3043 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n",
3044 current->comm, current->pid,
3045 (rw & WRITE) ? "WRITE" : "READ",
3046 (unsigned long long)bio->bi_sector,
3047 bdevname(bio->bi_bdev,b));
3050 generic_make_request(bio);
3053 EXPORT_SYMBOL(submit_bio);
3055 static void blk_recalc_rq_segments(struct request *rq)
3057 struct bio *bio, *prevbio = NULL;
3058 int nr_phys_segs, nr_hw_segs;
3059 unsigned int phys_size, hw_size;
3060 request_queue_t *q = rq->q;
3065 phys_size = hw_size = nr_phys_segs = nr_hw_segs = 0;
3066 rq_for_each_bio(bio, rq) {
3067 /* Force bio hw/phys segs to be recalculated. */
3068 bio->bi_flags &= ~(1 << BIO_SEG_VALID);
3070 nr_phys_segs += bio_phys_segments(q, bio);
3071 nr_hw_segs += bio_hw_segments(q, bio);
3073 int pseg = phys_size + prevbio->bi_size + bio->bi_size;
3074 int hseg = hw_size + prevbio->bi_size + bio->bi_size;
3076 if (blk_phys_contig_segment(q, prevbio, bio) &&
3077 pseg <= q->max_segment_size) {
3079 phys_size += prevbio->bi_size + bio->bi_size;
3083 if (blk_hw_contig_segment(q, prevbio, bio) &&
3084 hseg <= q->max_segment_size) {
3086 hw_size += prevbio->bi_size + bio->bi_size;
3093 rq->nr_phys_segments = nr_phys_segs;
3094 rq->nr_hw_segments = nr_hw_segs;
3097 static void blk_recalc_rq_sectors(struct request *rq, int nsect)
3099 if (blk_fs_request(rq)) {
3100 rq->hard_sector += nsect;
3101 rq->hard_nr_sectors -= nsect;
3104 * Move the I/O submission pointers ahead if required.
3106 if ((rq->nr_sectors >= rq->hard_nr_sectors) &&
3107 (rq->sector <= rq->hard_sector)) {
3108 rq->sector = rq->hard_sector;
3109 rq->nr_sectors = rq->hard_nr_sectors;
3110 rq->hard_cur_sectors = bio_cur_sectors(rq->bio);
3111 rq->current_nr_sectors = rq->hard_cur_sectors;
3112 rq->buffer = bio_data(rq->bio);
3116 * if total number of sectors is less than the first segment
3117 * size, something has gone terribly wrong
3119 if (rq->nr_sectors < rq->current_nr_sectors) {
3120 printk("blk: request botched\n");
3121 rq->nr_sectors = rq->current_nr_sectors;
3126 static int __end_that_request_first(struct request *req, int uptodate,
3129 int total_bytes, bio_nbytes, error, next_idx = 0;
3133 * extend uptodate bool to allow < 0 value to be direct io error
3136 if (end_io_error(uptodate))
3137 error = !uptodate ? -EIO : uptodate;
3140 * for a REQ_BLOCK_PC request, we want to carry any eventual
3141 * sense key with us all the way through
3143 if (!blk_pc_request(req))
3147 if (blk_fs_request(req) && !(req->flags & REQ_QUIET))
3148 printk("end_request: I/O error, dev %s, sector %llu\n",
3149 req->rq_disk ? req->rq_disk->disk_name : "?",
3150 (unsigned long long)req->sector);
3153 if (blk_fs_request(req) && req->rq_disk) {
3154 const int rw = rq_data_dir(req);
3156 __disk_stat_add(req->rq_disk, sectors[rw], nr_bytes >> 9);
3159 total_bytes = bio_nbytes = 0;
3160 while ((bio = req->bio) != NULL) {
3163 if (nr_bytes >= bio->bi_size) {
3164 req->bio = bio->bi_next;
3165 nbytes = bio->bi_size;
3166 if (!ordered_bio_endio(req, bio, nbytes, error))
3167 bio_endio(bio, nbytes, error);
3171 int idx = bio->bi_idx + next_idx;
3173 if (unlikely(bio->bi_idx >= bio->bi_vcnt)) {
3174 blk_dump_rq_flags(req, "__end_that");
3175 printk("%s: bio idx %d >= vcnt %d\n",
3177 bio->bi_idx, bio->bi_vcnt);
3181 nbytes = bio_iovec_idx(bio, idx)->bv_len;
3182 BIO_BUG_ON(nbytes > bio->bi_size);
3185 * not a complete bvec done
3187 if (unlikely(nbytes > nr_bytes)) {
3188 bio_nbytes += nr_bytes;
3189 total_bytes += nr_bytes;
3194 * advance to the next vector
3197 bio_nbytes += nbytes;
3200 total_bytes += nbytes;
3203 if ((bio = req->bio)) {
3205 * end more in this run, or just return 'not-done'
3207 if (unlikely(nr_bytes <= 0))
3219 * if the request wasn't completed, update state
3222 if (!ordered_bio_endio(req, bio, bio_nbytes, error))
3223 bio_endio(bio, bio_nbytes, error);
3224 bio->bi_idx += next_idx;
3225 bio_iovec(bio)->bv_offset += nr_bytes;
3226 bio_iovec(bio)->bv_len -= nr_bytes;
3229 blk_recalc_rq_sectors(req, total_bytes >> 9);
3230 blk_recalc_rq_segments(req);
3235 * end_that_request_first - end I/O on a request
3236 * @req: the request being processed
3237 * @uptodate: 1 for success, 0 for I/O error, < 0 for specific error
3238 * @nr_sectors: number of sectors to end I/O on
3241 * Ends I/O on a number of sectors attached to @req, and sets it up
3242 * for the next range of segments (if any) in the cluster.
3245 * 0 - we are done with this request, call end_that_request_last()
3246 * 1 - still buffers pending for this request
3248 int end_that_request_first(struct request *req, int uptodate, int nr_sectors)
3250 return __end_that_request_first(req, uptodate, nr_sectors << 9);
3253 EXPORT_SYMBOL(end_that_request_first);
3256 * end_that_request_chunk - end I/O on a request
3257 * @req: the request being processed
3258 * @uptodate: 1 for success, 0 for I/O error, < 0 for specific error
3259 * @nr_bytes: number of bytes to complete
3262 * Ends I/O on a number of bytes attached to @req, and sets it up
3263 * for the next range of segments (if any). Like end_that_request_first(),
3264 * but deals with bytes instead of sectors.
3267 * 0 - we are done with this request, call end_that_request_last()
3268 * 1 - still buffers pending for this request
3270 int end_that_request_chunk(struct request *req, int uptodate, int nr_bytes)
3272 return __end_that_request_first(req, uptodate, nr_bytes);
3275 EXPORT_SYMBOL(end_that_request_chunk);
3278 * splice the completion data to a local structure and hand off to
3279 * process_completion_queue() to complete the requests
3281 static void blk_done_softirq(struct softirq_action *h)
3283 struct list_head *cpu_list;
3284 LIST_HEAD(local_list);
3286 local_irq_disable();
3287 cpu_list = &__get_cpu_var(blk_cpu_done);
3288 list_splice_init(cpu_list, &local_list);
3291 while (!list_empty(&local_list)) {
3292 struct request *rq = list_entry(local_list.next, struct request, donelist);
3294 list_del_init(&rq->donelist);
3295 rq->q->softirq_done_fn(rq);
3299 #ifdef CONFIG_HOTPLUG_CPU
3301 static int blk_cpu_notify(struct notifier_block *self, unsigned long action,
3305 * If a CPU goes away, splice its entries to the current CPU
3306 * and trigger a run of the softirq
3308 if (action == CPU_DEAD) {
3309 int cpu = (unsigned long) hcpu;
3311 local_irq_disable();
3312 list_splice_init(&per_cpu(blk_cpu_done, cpu),
3313 &__get_cpu_var(blk_cpu_done));
3314 raise_softirq_irqoff(BLOCK_SOFTIRQ);
3322 static struct notifier_block __devinitdata blk_cpu_notifier = {
3323 .notifier_call = blk_cpu_notify,
3326 #endif /* CONFIG_HOTPLUG_CPU */
3329 * blk_complete_request - end I/O on a request
3330 * @req: the request being processed
3333 * Ends all I/O on a request. It does not handle partial completions,
3334 * unless the driver actually implements this in its completionc callback
3335 * through requeueing. Theh actual completion happens out-of-order,
3336 * through a softirq handler. The user must have registered a completion
3337 * callback through blk_queue_softirq_done().
3340 void blk_complete_request(struct request *req)
3342 struct list_head *cpu_list;
3343 unsigned long flags;
3345 BUG_ON(!req->q->softirq_done_fn);
3347 local_irq_save(flags);
3349 cpu_list = &__get_cpu_var(blk_cpu_done);
3350 list_add_tail(&req->donelist, cpu_list);
3351 raise_softirq_irqoff(BLOCK_SOFTIRQ);
3353 local_irq_restore(flags);
3356 EXPORT_SYMBOL(blk_complete_request);
3359 * queue lock must be held
3361 void end_that_request_last(struct request *req, int uptodate)
3363 struct gendisk *disk = req->rq_disk;
3367 * extend uptodate bool to allow < 0 value to be direct io error
3370 if (end_io_error(uptodate))
3371 error = !uptodate ? -EIO : uptodate;
3373 if (unlikely(laptop_mode) && blk_fs_request(req))
3374 laptop_io_completion();
3376 if (disk && blk_fs_request(req)) {
3377 unsigned long duration = jiffies - req->start_time;
3378 const int rw = rq_data_dir(req);
3380 __disk_stat_inc(disk, ios[rw]);
3381 __disk_stat_add(disk, ticks[rw], duration);
3382 disk_round_stats(disk);
3386 req->end_io(req, error);
3388 __blk_put_request(req->q, req);
3391 EXPORT_SYMBOL(end_that_request_last);
3393 void end_request(struct request *req, int uptodate)
3395 if (!end_that_request_first(req, uptodate, req->hard_cur_sectors)) {
3396 add_disk_randomness(req->rq_disk);
3397 blkdev_dequeue_request(req);
3398 end_that_request_last(req, uptodate);
3402 EXPORT_SYMBOL(end_request);
3404 void blk_rq_bio_prep(request_queue_t *q, struct request *rq, struct bio *bio)
3406 /* first three bits are identical in rq->flags and bio->bi_rw */
3407 rq->flags |= (bio->bi_rw & 7);
3409 rq->nr_phys_segments = bio_phys_segments(q, bio);
3410 rq->nr_hw_segments = bio_hw_segments(q, bio);
3411 rq->current_nr_sectors = bio_cur_sectors(bio);
3412 rq->hard_cur_sectors = rq->current_nr_sectors;
3413 rq->hard_nr_sectors = rq->nr_sectors = bio_sectors(bio);
3414 rq->buffer = bio_data(bio);
3416 rq->bio = rq->biotail = bio;
3419 EXPORT_SYMBOL(blk_rq_bio_prep);
3421 int kblockd_schedule_work(struct work_struct *work)
3423 return queue_work(kblockd_workqueue, work);
3426 EXPORT_SYMBOL(kblockd_schedule_work);
3428 void kblockd_flush(void)
3430 flush_workqueue(kblockd_workqueue);
3432 EXPORT_SYMBOL(kblockd_flush);
3434 int __init blk_dev_init(void)
3438 kblockd_workqueue = create_workqueue("kblockd");
3439 if (!kblockd_workqueue)
3440 panic("Failed to create kblockd\n");
3442 request_cachep = kmem_cache_create("blkdev_requests",
3443 sizeof(struct request), 0, SLAB_PANIC, NULL, NULL);
3445 requestq_cachep = kmem_cache_create("blkdev_queue",
3446 sizeof(request_queue_t), 0, SLAB_PANIC, NULL, NULL);
3448 iocontext_cachep = kmem_cache_create("blkdev_ioc",
3449 sizeof(struct io_context), 0, SLAB_PANIC, NULL, NULL);
3451 for (i = 0; i < NR_CPUS; i++)
3452 INIT_LIST_HEAD(&per_cpu(blk_cpu_done, i));
3454 open_softirq(BLOCK_SOFTIRQ, blk_done_softirq, NULL);
3455 #ifdef CONFIG_HOTPLUG_CPU
3456 register_cpu_notifier(&blk_cpu_notifier);
3459 blk_max_low_pfn = max_low_pfn;
3460 blk_max_pfn = max_pfn;
3466 * IO Context helper functions
3468 void put_io_context(struct io_context *ioc)
3473 BUG_ON(atomic_read(&ioc->refcount) == 0);
3475 if (atomic_dec_and_test(&ioc->refcount)) {
3476 if (ioc->aic && ioc->aic->dtor)
3477 ioc->aic->dtor(ioc->aic);
3478 if (ioc->cic && ioc->cic->dtor)
3479 ioc->cic->dtor(ioc->cic);
3481 kmem_cache_free(iocontext_cachep, ioc);
3484 EXPORT_SYMBOL(put_io_context);
3486 /* Called by the exitting task */
3487 void exit_io_context(void)
3489 unsigned long flags;
3490 struct io_context *ioc;
3492 local_irq_save(flags);
3494 ioc = current->io_context;
3495 current->io_context = NULL;
3497 task_unlock(current);
3498 local_irq_restore(flags);
3500 if (ioc->aic && ioc->aic->exit)
3501 ioc->aic->exit(ioc->aic);
3502 if (ioc->cic && ioc->cic->exit)
3503 ioc->cic->exit(ioc->cic);
3505 put_io_context(ioc);
3509 * If the current task has no IO context then create one and initialise it.
3510 * Otherwise, return its existing IO context.
3512 * This returned IO context doesn't have a specifically elevated refcount,
3513 * but since the current task itself holds a reference, the context can be
3514 * used in general code, so long as it stays within `current` context.
3516 struct io_context *current_io_context(gfp_t gfp_flags)
3518 struct task_struct *tsk = current;
3519 struct io_context *ret;
3521 ret = tsk->io_context;
3525 ret = kmem_cache_alloc(iocontext_cachep, gfp_flags);
3527 atomic_set(&ret->refcount, 1);
3528 ret->task = current;
3529 ret->set_ioprio = NULL;
3530 ret->last_waited = jiffies; /* doesn't matter... */
3531 ret->nr_batch_requests = 0; /* because this is 0 */
3534 tsk->io_context = ret;
3539 EXPORT_SYMBOL(current_io_context);
3542 * If the current task has no IO context then create one and initialise it.
3543 * If it does have a context, take a ref on it.
3545 * This is always called in the context of the task which submitted the I/O.
3547 struct io_context *get_io_context(gfp_t gfp_flags)
3549 struct io_context *ret;
3550 ret = current_io_context(gfp_flags);
3552 atomic_inc(&ret->refcount);
3555 EXPORT_SYMBOL(get_io_context);
3557 void copy_io_context(struct io_context **pdst, struct io_context **psrc)
3559 struct io_context *src = *psrc;
3560 struct io_context *dst = *pdst;
3563 BUG_ON(atomic_read(&src->refcount) == 0);
3564 atomic_inc(&src->refcount);
3565 put_io_context(dst);
3569 EXPORT_SYMBOL(copy_io_context);
3571 void swap_io_context(struct io_context **ioc1, struct io_context **ioc2)
3573 struct io_context *temp;
3578 EXPORT_SYMBOL(swap_io_context);
3583 struct queue_sysfs_entry {
3584 struct attribute attr;
3585 ssize_t (*show)(struct request_queue *, char *);
3586 ssize_t (*store)(struct request_queue *, const char *, size_t);
3590 queue_var_show(unsigned int var, char *page)
3592 return sprintf(page, "%d\n", var);
3596 queue_var_store(unsigned long *var, const char *page, size_t count)
3598 char *p = (char *) page;
3600 *var = simple_strtoul(p, &p, 10);
3604 static ssize_t queue_requests_show(struct request_queue *q, char *page)
3606 return queue_var_show(q->nr_requests, (page));
3610 queue_requests_store(struct request_queue *q, const char *page, size_t count)
3612 struct request_list *rl = &q->rq;
3614 int ret = queue_var_store(&q->nr_requests, page, count);
3615 if (q->nr_requests < BLKDEV_MIN_RQ)
3616 q->nr_requests = BLKDEV_MIN_RQ;
3617 blk_queue_congestion_threshold(q);
3619 if (rl->count[READ] >= queue_congestion_on_threshold(q))
3620 set_queue_congested(q, READ);
3621 else if (rl->count[READ] < queue_congestion_off_threshold(q))
3622 clear_queue_congested(q, READ);
3624 if (rl->count[WRITE] >= queue_congestion_on_threshold(q))
3625 set_queue_congested(q, WRITE);
3626 else if (rl->count[WRITE] < queue_congestion_off_threshold(q))
3627 clear_queue_congested(q, WRITE);
3629 if (rl->count[READ] >= q->nr_requests) {
3630 blk_set_queue_full(q, READ);
3631 } else if (rl->count[READ]+1 <= q->nr_requests) {
3632 blk_clear_queue_full(q, READ);
3633 wake_up(&rl->wait[READ]);
3636 if (rl->count[WRITE] >= q->nr_requests) {
3637 blk_set_queue_full(q, WRITE);
3638 } else if (rl->count[WRITE]+1 <= q->nr_requests) {
3639 blk_clear_queue_full(q, WRITE);
3640 wake_up(&rl->wait[WRITE]);
3645 static ssize_t queue_ra_show(struct request_queue *q, char *page)
3647 int ra_kb = q->backing_dev_info.ra_pages << (PAGE_CACHE_SHIFT - 10);
3649 return queue_var_show(ra_kb, (page));
3653 queue_ra_store(struct request_queue *q, const char *page, size_t count)
3655 unsigned long ra_kb;
3656 ssize_t ret = queue_var_store(&ra_kb, page, count);
3658 spin_lock_irq(q->queue_lock);
3659 if (ra_kb > (q->max_sectors >> 1))
3660 ra_kb = (q->max_sectors >> 1);
3662 q->backing_dev_info.ra_pages = ra_kb >> (PAGE_CACHE_SHIFT - 10);
3663 spin_unlock_irq(q->queue_lock);
3668 static ssize_t queue_max_sectors_show(struct request_queue *q, char *page)
3670 int max_sectors_kb = q->max_sectors >> 1;
3672 return queue_var_show(max_sectors_kb, (page));
3676 queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
3678 unsigned long max_sectors_kb,
3679 max_hw_sectors_kb = q->max_hw_sectors >> 1,
3680 page_kb = 1 << (PAGE_CACHE_SHIFT - 10);
3681 ssize_t ret = queue_var_store(&max_sectors_kb, page, count);
3684 if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
3687 * Take the queue lock to update the readahead and max_sectors
3688 * values synchronously:
3690 spin_lock_irq(q->queue_lock);
3692 * Trim readahead window as well, if necessary:
3694 ra_kb = q->backing_dev_info.ra_pages << (PAGE_CACHE_SHIFT - 10);
3695 if (ra_kb > max_sectors_kb)
3696 q->backing_dev_info.ra_pages =
3697 max_sectors_kb >> (PAGE_CACHE_SHIFT - 10);
3699 q->max_sectors = max_sectors_kb << 1;
3700 spin_unlock_irq(q->queue_lock);
3705 static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
3707 int max_hw_sectors_kb = q->max_hw_sectors >> 1;
3709 return queue_var_show(max_hw_sectors_kb, (page));
3713 static struct queue_sysfs_entry queue_requests_entry = {
3714 .attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR },
3715 .show = queue_requests_show,
3716 .store = queue_requests_store,
3719 static struct queue_sysfs_entry queue_ra_entry = {
3720 .attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR },
3721 .show = queue_ra_show,
3722 .store = queue_ra_store,
3725 static struct queue_sysfs_entry queue_max_sectors_entry = {
3726 .attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR },
3727 .show = queue_max_sectors_show,
3728 .store = queue_max_sectors_store,
3731 static struct queue_sysfs_entry queue_max_hw_sectors_entry = {
3732 .attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO },
3733 .show = queue_max_hw_sectors_show,
3736 static struct queue_sysfs_entry queue_iosched_entry = {
3737 .attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR },
3738 .show = elv_iosched_show,
3739 .store = elv_iosched_store,
3742 static struct attribute *default_attrs[] = {
3743 &queue_requests_entry.attr,
3744 &queue_ra_entry.attr,
3745 &queue_max_hw_sectors_entry.attr,
3746 &queue_max_sectors_entry.attr,
3747 &queue_iosched_entry.attr,
3751 #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
3754 queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3756 struct queue_sysfs_entry *entry = to_queue(attr);
3757 struct request_queue *q;
3759 q = container_of(kobj, struct request_queue, kobj);
3763 return entry->show(q, page);
3767 queue_attr_store(struct kobject *kobj, struct attribute *attr,
3768 const char *page, size_t length)
3770 struct queue_sysfs_entry *entry = to_queue(attr);
3771 struct request_queue *q;
3773 q = container_of(kobj, struct request_queue, kobj);
3777 return entry->store(q, page, length);
3780 static struct sysfs_ops queue_sysfs_ops = {
3781 .show = queue_attr_show,
3782 .store = queue_attr_store,
3785 static struct kobj_type queue_ktype = {
3786 .sysfs_ops = &queue_sysfs_ops,
3787 .default_attrs = default_attrs,
3790 int blk_register_queue(struct gendisk *disk)
3794 request_queue_t *q = disk->queue;
3796 if (!q || !q->request_fn)
3799 q->kobj.parent = kobject_get(&disk->kobj);
3800 if (!q->kobj.parent)
3803 snprintf(q->kobj.name, KOBJ_NAME_LEN, "%s", "queue");
3804 q->kobj.ktype = &queue_ktype;
3806 ret = kobject_register(&q->kobj);
3810 ret = elv_register_queue(q);
3812 kobject_unregister(&q->kobj);
3819 void blk_unregister_queue(struct gendisk *disk)
3821 request_queue_t *q = disk->queue;
3823 if (q && q->request_fn) {
3824 elv_unregister_queue(q);
3826 kobject_unregister(&q->kobj);
3827 kobject_put(&disk->kobj);