MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / block / ll_rw_blk.c
blobae207fc12a4eb162aac1f9b73b8974bec985c7cc
1 /*
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
8 */
11 * This handles all read/write requests to block devices
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/backing-dev.h>
16 #include <linux/bio.h>
17 #include <linux/blkdev.h>
18 #include <linux/highmem.h>
19 #include <linux/mm.h>
20 #include <linux/kernel_stat.h>
21 #include <linux/string.h>
22 #include <linux/init.h>
23 #include <linux/bootmem.h> /* for max_pfn/max_low_pfn */
24 #include <linux/completion.h>
25 #include <linux/slab.h>
26 #include <linux/swap.h>
27 #include <linux/writeback.h>
28 #include <linux/interrupt.h>
29 #include <linux/cpu.h>
30 #include <linux/blktrace_api.h>
33 * for max sense size
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);
42 static struct io_context *current_io_context(gfp_t gfp_flags, int node);
45 * For the allocated request tables
47 static kmem_cache_t *request_cachep;
50 * For queue allocation
52 static kmem_cache_t *requestq_cachep;
55 * For io context allocations
57 static kmem_cache_t *iocontext_cachep;
60 * Controlling structure to kblockd
62 static struct workqueue_struct *kblockd_workqueue;
64 unsigned long blk_max_low_pfn, blk_max_pfn;
66 EXPORT_SYMBOL(blk_max_low_pfn);
67 EXPORT_SYMBOL(blk_max_pfn);
69 static DEFINE_PER_CPU(struct list_head, blk_cpu_done);
71 /* Amount of time in which a process may batch requests */
72 #define BLK_BATCH_TIME (HZ/50UL)
74 /* Number of requests a "batching" process may submit */
75 #define BLK_BATCH_REQ 32
78 * Return the threshold (number of used requests) at which the queue is
79 * considered to be congested. It include a little hysteresis to keep the
80 * context switch rate down.
82 static inline int queue_congestion_on_threshold(struct request_queue *q)
84 return q->nr_congestion_on;
88 * The threshold at which a queue is considered to be uncongested
90 static inline int queue_congestion_off_threshold(struct request_queue *q)
92 return q->nr_congestion_off;
95 static void blk_queue_congestion_threshold(struct request_queue *q)
97 int nr;
99 nr = q->nr_requests - (q->nr_requests / 8) + 1;
100 if (nr > q->nr_requests)
101 nr = q->nr_requests;
102 q->nr_congestion_on = nr;
104 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
105 if (nr < 1)
106 nr = 1;
107 q->nr_congestion_off = nr;
111 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
112 * @bdev: device
114 * Locates the passed device's request queue and returns the address of its
115 * backing_dev_info
117 * Will return NULL if the request queue cannot be located.
119 struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
121 struct backing_dev_info *ret = NULL;
122 request_queue_t *q = bdev_get_queue(bdev);
124 if (q)
125 ret = &q->backing_dev_info;
126 return ret;
128 EXPORT_SYMBOL(blk_get_backing_dev_info);
130 void blk_queue_activity_fn(request_queue_t *q, activity_fn *fn, void *data)
132 q->activity_fn = fn;
133 q->activity_data = data;
135 EXPORT_SYMBOL(blk_queue_activity_fn);
138 * blk_queue_prep_rq - set a prepare_request function for queue
139 * @q: queue
140 * @pfn: prepare_request function
142 * It's possible for a queue to register a prepare_request callback which
143 * is invoked before the request is handed to the request_fn. The goal of
144 * the function is to prepare a request for I/O, it can be used to build a
145 * cdb from the request data for instance.
148 void blk_queue_prep_rq(request_queue_t *q, prep_rq_fn *pfn)
150 q->prep_rq_fn = pfn;
153 EXPORT_SYMBOL(blk_queue_prep_rq);
156 * blk_queue_merge_bvec - set a merge_bvec function for queue
157 * @q: 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. Note that a block device *must* allow a
166 * single page to be added to an empty bio. The block device driver may want
167 * to use the bio_split() function to deal with these bio's. By default
168 * no merge_bvec_fn is defined for a queue, and only the fixed limits are
169 * honored.
171 void blk_queue_merge_bvec(request_queue_t *q, merge_bvec_fn *mbfn)
173 q->merge_bvec_fn = mbfn;
176 EXPORT_SYMBOL(blk_queue_merge_bvec);
178 void blk_queue_softirq_done(request_queue_t *q, softirq_done_fn *fn)
180 q->softirq_done_fn = fn;
183 EXPORT_SYMBOL(blk_queue_softirq_done);
186 * blk_queue_make_request - define an alternate make_request function for a device
187 * @q: the request queue for the device to be affected
188 * @mfn: the alternate make_request function
190 * Description:
191 * The normal way for &struct bios to be passed to a device
192 * driver is for them to be collected into requests on a request
193 * queue, and then to allow the device driver to select requests
194 * off that queue when it is ready. This works well for many block
195 * devices. However some block devices (typically virtual devices
196 * such as md or lvm) do not benefit from the processing on the
197 * request queue, and are served best by having the requests passed
198 * directly to them. This can be achieved by providing a function
199 * to blk_queue_make_request().
201 * Caveat:
202 * The driver that does this *must* be able to deal appropriately
203 * with buffers in "highmemory". This can be accomplished by either calling
204 * __bio_kmap_atomic() to get a temporary kernel mapping, or by calling
205 * blk_queue_bounce() to create a buffer in normal memory.
207 void blk_queue_make_request(request_queue_t * q, make_request_fn * mfn)
210 * set defaults
212 q->nr_requests = BLKDEV_MAX_RQ;
213 blk_queue_max_phys_segments(q, MAX_PHYS_SEGMENTS);
214 blk_queue_max_hw_segments(q, MAX_HW_SEGMENTS);
215 q->make_request_fn = mfn;
216 q->backing_dev_info.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
217 q->backing_dev_info.state = 0;
218 q->backing_dev_info.capabilities = BDI_CAP_MAP_COPY;
219 blk_queue_max_sectors(q, SAFE_MAX_SECTORS);
220 blk_queue_hardsect_size(q, 512);
221 blk_queue_dma_alignment(q, 511);
222 blk_queue_congestion_threshold(q);
223 q->nr_batching = BLK_BATCH_REQ;
225 q->unplug_thresh = 4; /* hmm */
226 q->unplug_delay = (3 * HZ) / 1000; /* 3 milliseconds */
227 if (q->unplug_delay == 0)
228 q->unplug_delay = 1;
230 INIT_WORK(&q->unplug_work, blk_unplug_work, q);
232 q->unplug_timer.function = blk_unplug_timeout;
233 q->unplug_timer.data = (unsigned long)q;
236 * by default assume old behaviour and bounce for any highmem page
238 blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
240 blk_queue_activity_fn(q, NULL, NULL);
243 EXPORT_SYMBOL(blk_queue_make_request);
245 static void rq_init(request_queue_t *q, struct request *rq)
247 INIT_LIST_HEAD(&rq->queuelist);
248 INIT_LIST_HEAD(&rq->donelist);
250 rq->errors = 0;
251 rq->bio = rq->biotail = NULL;
252 INIT_HLIST_NODE(&rq->hash);
253 #if 0 // mask by Victor Yu. 02-12-2007
254 RB_CLEAR_NODE(&rq->rb_node);
255 #else
256 RB_CLEAR_NODE(&rq->u.rb_node);
257 #endif
258 rq->ioprio = 0;
259 rq->buffer = NULL;
260 rq->ref_count = 1;
261 rq->q = q;
262 rq->special = NULL;
263 rq->data_len = 0;
264 rq->data = NULL;
265 rq->nr_phys_segments = 0;
266 rq->sense = NULL;
267 rq->end_io = NULL;
268 rq->end_io_data = NULL;
269 #if 0 // mask by Victor Yu. 02-12-2007
270 rq->completion_data = NULL;
271 #else
272 rq->u.completion_data = NULL;
273 #endif
277 * blk_queue_ordered - does this queue support ordered writes
278 * @q: the request queue
279 * @ordered: one of QUEUE_ORDERED_*
280 * @prepare_flush_fn: rq setup helper for cache flush ordered writes
282 * Description:
283 * For journalled file systems, doing ordered writes on a commit
284 * block instead of explicitly doing wait_on_buffer (which is bad
285 * for performance) can be a big win. Block drivers supporting this
286 * feature should call this function and indicate so.
289 int blk_queue_ordered(request_queue_t *q, unsigned ordered,
290 prepare_flush_fn *prepare_flush_fn)
292 if (ordered & (QUEUE_ORDERED_PREFLUSH | QUEUE_ORDERED_POSTFLUSH) &&
293 prepare_flush_fn == NULL) {
294 printk(KERN_ERR "blk_queue_ordered: prepare_flush_fn required\n");
295 return -EINVAL;
298 if (ordered != QUEUE_ORDERED_NONE &&
299 ordered != QUEUE_ORDERED_DRAIN &&
300 ordered != QUEUE_ORDERED_DRAIN_FLUSH &&
301 ordered != QUEUE_ORDERED_DRAIN_FUA &&
302 ordered != QUEUE_ORDERED_TAG &&
303 ordered != QUEUE_ORDERED_TAG_FLUSH &&
304 ordered != QUEUE_ORDERED_TAG_FUA) {
305 printk(KERN_ERR "blk_queue_ordered: bad value %d\n", ordered);
306 return -EINVAL;
309 q->ordered = ordered;
310 q->next_ordered = ordered;
311 q->prepare_flush_fn = prepare_flush_fn;
313 return 0;
316 EXPORT_SYMBOL(blk_queue_ordered);
319 * blk_queue_issue_flush_fn - set function for issuing a flush
320 * @q: the request queue
321 * @iff: the function to be called issuing the flush
323 * Description:
324 * If a driver supports issuing a flush command, the support is notified
325 * to the block layer by defining it through this call.
328 void blk_queue_issue_flush_fn(request_queue_t *q, issue_flush_fn *iff)
330 q->issue_flush_fn = iff;
333 EXPORT_SYMBOL(blk_queue_issue_flush_fn);
336 * Cache flushing for ordered writes handling
338 inline unsigned blk_ordered_cur_seq(request_queue_t *q)
340 if (!q->ordseq)
341 return 0;
342 return 1 << ffz(q->ordseq);
345 unsigned blk_ordered_req_seq(struct request *rq)
347 request_queue_t *q = rq->q;
349 BUG_ON(q->ordseq == 0);
351 if (rq == &q->pre_flush_rq)
352 return QUEUE_ORDSEQ_PREFLUSH;
353 if (rq == &q->bar_rq)
354 return QUEUE_ORDSEQ_BAR;
355 if (rq == &q->post_flush_rq)
356 return QUEUE_ORDSEQ_POSTFLUSH;
358 if ((rq->cmd_flags & REQ_ORDERED_COLOR) ==
359 (q->orig_bar_rq->cmd_flags & REQ_ORDERED_COLOR))
360 return QUEUE_ORDSEQ_DRAIN;
361 else
362 return QUEUE_ORDSEQ_DONE;
365 void blk_ordered_complete_seq(request_queue_t *q, unsigned seq, int error)
367 struct request *rq;
368 int uptodate;
370 if (error && !q->orderr)
371 q->orderr = error;
373 BUG_ON(q->ordseq & seq);
374 q->ordseq |= seq;
376 if (blk_ordered_cur_seq(q) != QUEUE_ORDSEQ_DONE)
377 return;
380 * Okay, sequence complete.
382 rq = q->orig_bar_rq;
383 uptodate = q->orderr ? q->orderr : 1;
385 q->ordseq = 0;
387 end_that_request_first(rq, uptodate, rq->hard_nr_sectors);
388 end_that_request_last(rq, uptodate);
391 static void pre_flush_end_io(struct request *rq, int error)
393 elv_completed_request(rq->q, rq);
394 blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_PREFLUSH, error);
397 static void bar_end_io(struct request *rq, int error)
399 elv_completed_request(rq->q, rq);
400 blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_BAR, error);
403 static void post_flush_end_io(struct request *rq, int error)
405 elv_completed_request(rq->q, rq);
406 blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_POSTFLUSH, error);
409 static void queue_flush(request_queue_t *q, unsigned which)
411 struct request *rq;
412 rq_end_io_fn *end_io;
414 if (which == QUEUE_ORDERED_PREFLUSH) {
415 rq = &q->pre_flush_rq;
416 end_io = pre_flush_end_io;
417 } else {
418 rq = &q->post_flush_rq;
419 end_io = post_flush_end_io;
422 rq->cmd_flags = REQ_HARDBARRIER;
423 rq_init(q, rq);
424 rq->elevator_private = NULL;
425 rq->elevator_private2 = NULL;
426 rq->rq_disk = q->bar_rq.rq_disk;
427 rq->end_io = end_io;
428 q->prepare_flush_fn(q, rq);
430 elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
433 static inline struct request *start_ordered(request_queue_t *q,
434 struct request *rq)
436 q->bi_size = 0;
437 q->orderr = 0;
438 q->ordered = q->next_ordered;
439 q->ordseq |= QUEUE_ORDSEQ_STARTED;
442 * Prep proxy barrier request.
444 blkdev_dequeue_request(rq);
445 q->orig_bar_rq = rq;
446 rq = &q->bar_rq;
447 rq->cmd_flags = 0;
448 rq_init(q, rq);
449 if (bio_data_dir(q->orig_bar_rq->bio) == WRITE)
450 rq->cmd_flags |= REQ_RW;
451 rq->cmd_flags |= q->ordered & QUEUE_ORDERED_FUA ? REQ_FUA : 0;
452 rq->elevator_private = NULL;
453 rq->elevator_private2 = NULL;
454 init_request_from_bio(rq, q->orig_bar_rq->bio);
455 rq->end_io = bar_end_io;
458 * Queue ordered sequence. As we stack them at the head, we
459 * need to queue in reverse order. Note that we rely on that
460 * no fs request uses ELEVATOR_INSERT_FRONT and thus no fs
461 * request gets inbetween ordered sequence.
463 if (q->ordered & QUEUE_ORDERED_POSTFLUSH)
464 queue_flush(q, QUEUE_ORDERED_POSTFLUSH);
465 else
466 q->ordseq |= QUEUE_ORDSEQ_POSTFLUSH;
468 elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
470 if (q->ordered & QUEUE_ORDERED_PREFLUSH) {
471 queue_flush(q, QUEUE_ORDERED_PREFLUSH);
472 rq = &q->pre_flush_rq;
473 } else
474 q->ordseq |= QUEUE_ORDSEQ_PREFLUSH;
476 if ((q->ordered & QUEUE_ORDERED_TAG) || q->in_flight == 0)
477 q->ordseq |= QUEUE_ORDSEQ_DRAIN;
478 else
479 rq = NULL;
481 return rq;
484 int blk_do_ordered(request_queue_t *q, struct request **rqp)
486 struct request *rq = *rqp;
487 int is_barrier = blk_fs_request(rq) && blk_barrier_rq(rq);
489 if (!q->ordseq) {
490 if (!is_barrier)
491 return 1;
493 if (q->next_ordered != QUEUE_ORDERED_NONE) {
494 *rqp = start_ordered(q, rq);
495 return 1;
496 } else {
498 * This can happen when the queue switches to
499 * ORDERED_NONE while this request is on it.
501 blkdev_dequeue_request(rq);
502 end_that_request_first(rq, -EOPNOTSUPP,
503 rq->hard_nr_sectors);
504 end_that_request_last(rq, -EOPNOTSUPP);
505 *rqp = NULL;
506 return 0;
511 * Ordered sequence in progress
514 /* Special requests are not subject to ordering rules. */
515 if (!blk_fs_request(rq) &&
516 rq != &q->pre_flush_rq && rq != &q->post_flush_rq)
517 return 1;
519 if (q->ordered & QUEUE_ORDERED_TAG) {
520 /* Ordered by tag. Blocking the next barrier is enough. */
521 if (is_barrier && rq != &q->bar_rq)
522 *rqp = NULL;
523 } else {
524 /* Ordered by draining. Wait for turn. */
525 WARN_ON(blk_ordered_req_seq(rq) < blk_ordered_cur_seq(q));
526 if (blk_ordered_req_seq(rq) > blk_ordered_cur_seq(q))
527 *rqp = NULL;
530 return 1;
533 static int flush_dry_bio_endio(struct bio *bio, unsigned int bytes, int error)
535 request_queue_t *q = bio->bi_private;
536 struct bio_vec *bvec;
537 int i;
540 * This is dry run, restore bio_sector and size. We'll finish
541 * this request again with the original bi_end_io after an
542 * error occurs or post flush is complete.
544 q->bi_size += bytes;
546 if (bio->bi_size)
547 return 1;
549 /* Rewind bvec's */
550 bio->bi_idx = 0;
551 bio_for_each_segment(bvec, bio, i) {
552 bvec->bv_len += bvec->bv_offset;
553 bvec->bv_offset = 0;
556 /* Reset bio */
557 set_bit(BIO_UPTODATE, &bio->bi_flags);
558 bio->bi_size = q->bi_size;
559 bio->bi_sector -= (q->bi_size >> 9);
560 q->bi_size = 0;
562 return 0;
565 static int ordered_bio_endio(struct request *rq, struct bio *bio,
566 unsigned int nbytes, int error)
568 request_queue_t *q = rq->q;
569 bio_end_io_t *endio;
570 void *private;
572 if (&q->bar_rq != rq)
573 return 0;
576 * Okay, this is the barrier request in progress, dry finish it.
578 if (error && !q->orderr)
579 q->orderr = error;
581 endio = bio->bi_end_io;
582 private = bio->bi_private;
583 bio->bi_end_io = flush_dry_bio_endio;
584 bio->bi_private = q;
586 bio_endio(bio, nbytes, error);
588 bio->bi_end_io = endio;
589 bio->bi_private = private;
591 return 1;
595 * blk_queue_bounce_limit - set bounce buffer limit for queue
596 * @q: the request queue for the device
597 * @dma_addr: bus address limit
599 * Description:
600 * Different hardware can have different requirements as to what pages
601 * it can do I/O directly to. A low level driver can call
602 * blk_queue_bounce_limit to have lower memory pages allocated as bounce
603 * buffers for doing I/O to pages residing above @page.
605 void blk_queue_bounce_limit(request_queue_t *q, u64 dma_addr)
607 unsigned long bounce_pfn = dma_addr >> PAGE_SHIFT;
608 int dma = 0;
610 q->bounce_gfp = GFP_NOIO;
611 #if BITS_PER_LONG == 64
612 /* Assume anything <= 4GB can be handled by IOMMU.
613 Actually some IOMMUs can handle everything, but I don't
614 know of a way to test this here. */
615 if (bounce_pfn < (min_t(u64,0xffffffff,BLK_BOUNCE_HIGH) >> PAGE_SHIFT))
616 dma = 1;
617 q->bounce_pfn = max_low_pfn;
618 #else
619 if (bounce_pfn < blk_max_low_pfn)
620 dma = 1;
621 q->bounce_pfn = bounce_pfn;
622 #endif
623 if (dma) {
624 init_emergency_isa_pool();
625 q->bounce_gfp = GFP_NOIO | GFP_DMA;
626 q->bounce_pfn = bounce_pfn;
630 EXPORT_SYMBOL(blk_queue_bounce_limit);
633 * blk_queue_max_sectors - set max sectors for a request for this queue
634 * @q: the request queue for the device
635 * @max_sectors: max sectors in the usual 512b unit
637 * Description:
638 * Enables a low level driver to set an upper limit on the size of
639 * received requests.
641 void blk_queue_max_sectors(request_queue_t *q, unsigned int max_sectors)
643 if ((max_sectors << 9) < PAGE_CACHE_SIZE) {
644 max_sectors = 1 << (PAGE_CACHE_SHIFT - 9);
645 printk("%s: set to minimum %d\n", __FUNCTION__, max_sectors);
648 if (BLK_DEF_MAX_SECTORS > max_sectors)
649 q->max_hw_sectors = q->max_sectors = max_sectors;
650 else {
651 q->max_sectors = BLK_DEF_MAX_SECTORS;
652 q->max_hw_sectors = max_sectors;
656 EXPORT_SYMBOL(blk_queue_max_sectors);
659 * blk_queue_max_phys_segments - set max phys segments for a request for this queue
660 * @q: the request queue for the device
661 * @max_segments: max number of segments
663 * Description:
664 * Enables a low level driver to set an upper limit on the number of
665 * physical data segments in a request. This would be the largest sized
666 * scatter list the driver could handle.
668 void blk_queue_max_phys_segments(request_queue_t *q, unsigned short max_segments)
670 if (!max_segments) {
671 max_segments = 1;
672 printk("%s: set to minimum %d\n", __FUNCTION__, max_segments);
675 q->max_phys_segments = max_segments;
678 EXPORT_SYMBOL(blk_queue_max_phys_segments);
681 * blk_queue_max_hw_segments - set max hw segments for a request for this queue
682 * @q: the request queue for the device
683 * @max_segments: max number of segments
685 * Description:
686 * Enables a low level driver to set an upper limit on the number of
687 * hw data segments in a request. This would be the largest number of
688 * address/length pairs the host adapter can actually give as once
689 * to the device.
691 void blk_queue_max_hw_segments(request_queue_t *q, unsigned short max_segments)
693 if (!max_segments) {
694 max_segments = 1;
695 printk("%s: set to minimum %d\n", __FUNCTION__, max_segments);
698 q->max_hw_segments = max_segments;
701 EXPORT_SYMBOL(blk_queue_max_hw_segments);
704 * blk_queue_max_segment_size - set max segment size for blk_rq_map_sg
705 * @q: the request queue for the device
706 * @max_size: max size of segment in bytes
708 * Description:
709 * Enables a low level driver to set an upper limit on the size of a
710 * coalesced segment
712 void blk_queue_max_segment_size(request_queue_t *q, unsigned int max_size)
714 if (max_size < PAGE_CACHE_SIZE) {
715 max_size = PAGE_CACHE_SIZE;
716 printk("%s: set to minimum %d\n", __FUNCTION__, max_size);
719 q->max_segment_size = max_size;
722 EXPORT_SYMBOL(blk_queue_max_segment_size);
725 * blk_queue_hardsect_size - set hardware sector size for the queue
726 * @q: the request queue for the device
727 * @size: the hardware sector size, in bytes
729 * Description:
730 * This should typically be set to the lowest possible sector size
731 * that the hardware can operate on (possible without reverting to
732 * even internal read-modify-write operations). Usually the default
733 * of 512 covers most hardware.
735 void blk_queue_hardsect_size(request_queue_t *q, unsigned short size)
737 q->hardsect_size = size;
740 EXPORT_SYMBOL(blk_queue_hardsect_size);
743 * Returns the minimum that is _not_ zero, unless both are zero.
745 #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
748 * blk_queue_stack_limits - inherit underlying queue limits for stacked drivers
749 * @t: the stacking driver (top)
750 * @b: the underlying device (bottom)
752 void blk_queue_stack_limits(request_queue_t *t, request_queue_t *b)
754 /* zero is "infinity" */
755 t->max_sectors = min_not_zero(t->max_sectors,b->max_sectors);
756 t->max_hw_sectors = min_not_zero(t->max_hw_sectors,b->max_hw_sectors);
758 t->max_phys_segments = min(t->max_phys_segments,b->max_phys_segments);
759 t->max_hw_segments = min(t->max_hw_segments,b->max_hw_segments);
760 t->max_segment_size = min(t->max_segment_size,b->max_segment_size);
761 t->hardsect_size = max(t->hardsect_size,b->hardsect_size);
762 if (!test_bit(QUEUE_FLAG_CLUSTER, &b->queue_flags))
763 clear_bit(QUEUE_FLAG_CLUSTER, &t->queue_flags);
766 EXPORT_SYMBOL(blk_queue_stack_limits);
769 * blk_queue_segment_boundary - set boundary rules for segment merging
770 * @q: the request queue for the device
771 * @mask: the memory boundary mask
773 void blk_queue_segment_boundary(request_queue_t *q, unsigned long mask)
775 if (mask < PAGE_CACHE_SIZE - 1) {
776 mask = PAGE_CACHE_SIZE - 1;
777 printk("%s: set to minimum %lx\n", __FUNCTION__, mask);
780 q->seg_boundary_mask = mask;
783 EXPORT_SYMBOL(blk_queue_segment_boundary);
786 * blk_queue_dma_alignment - set dma length and memory alignment
787 * @q: the request queue for the device
788 * @mask: alignment mask
790 * description:
791 * set required memory and length aligment for direct dma transactions.
792 * this is used when buiding direct io requests for the queue.
795 void blk_queue_dma_alignment(request_queue_t *q, int mask)
797 q->dma_alignment = mask;
800 EXPORT_SYMBOL(blk_queue_dma_alignment);
803 * blk_queue_find_tag - find a request by its tag and queue
804 * @q: The request queue for the device
805 * @tag: The tag of the request
807 * Notes:
808 * Should be used when a device returns a tag and you want to match
809 * it with a request.
811 * no locks need be held.
813 struct request *blk_queue_find_tag(request_queue_t *q, int tag)
815 return blk_map_queue_find_tag(q->queue_tags, tag);
818 EXPORT_SYMBOL(blk_queue_find_tag);
821 * __blk_free_tags - release a given set of tag maintenance info
822 * @bqt: the tag map to free
824 * Tries to free the specified @bqt@. Returns true if it was
825 * actually freed and false if there are still references using it
827 static int __blk_free_tags(struct blk_queue_tag *bqt)
829 int retval;
831 retval = atomic_dec_and_test(&bqt->refcnt);
832 if (retval) {
833 BUG_ON(bqt->busy);
834 BUG_ON(!list_empty(&bqt->busy_list));
836 kfree(bqt->tag_index);
837 bqt->tag_index = NULL;
839 kfree(bqt->tag_map);
840 bqt->tag_map = NULL;
842 kfree(bqt);
846 return retval;
850 * __blk_queue_free_tags - release tag maintenance info
851 * @q: the request queue for the device
853 * Notes:
854 * blk_cleanup_queue() will take care of calling this function, if tagging
855 * has been used. So there's no need to call this directly.
857 static void __blk_queue_free_tags(request_queue_t *q)
859 struct blk_queue_tag *bqt = q->queue_tags;
861 if (!bqt)
862 return;
864 __blk_free_tags(bqt);
866 q->queue_tags = NULL;
867 q->queue_flags &= ~(1 << QUEUE_FLAG_QUEUED);
872 * blk_free_tags - release a given set of tag maintenance info
873 * @bqt: the tag map to free
875 * For externally managed @bqt@ frees the map. Callers of this
876 * function must guarantee to have released all the queues that
877 * might have been using this tag map.
879 void blk_free_tags(struct blk_queue_tag *bqt)
881 if (unlikely(!__blk_free_tags(bqt)))
882 BUG();
884 EXPORT_SYMBOL(blk_free_tags);
887 * blk_queue_free_tags - release tag maintenance info
888 * @q: the request queue for the device
890 * Notes:
891 * This is used to disabled tagged queuing to a device, yet leave
892 * queue in function.
894 void blk_queue_free_tags(request_queue_t *q)
896 clear_bit(QUEUE_FLAG_QUEUED, &q->queue_flags);
899 EXPORT_SYMBOL(blk_queue_free_tags);
901 static int
902 init_tag_map(request_queue_t *q, struct blk_queue_tag *tags, int depth)
904 struct request **tag_index;
905 unsigned long *tag_map;
906 int nr_ulongs;
908 if (q && depth > q->nr_requests * 2) {
909 depth = q->nr_requests * 2;
910 printk(KERN_ERR "%s: adjusted depth to %d\n",
911 __FUNCTION__, depth);
914 tag_index = kzalloc(depth * sizeof(struct request *), GFP_ATOMIC);
915 if (!tag_index)
916 goto fail;
918 nr_ulongs = ALIGN(depth, BITS_PER_LONG) / BITS_PER_LONG;
919 tag_map = kzalloc(nr_ulongs * sizeof(unsigned long), GFP_ATOMIC);
920 if (!tag_map)
921 goto fail;
923 tags->real_max_depth = depth;
924 tags->max_depth = depth;
925 tags->tag_index = tag_index;
926 tags->tag_map = tag_map;
928 return 0;
929 fail:
930 kfree(tag_index);
931 return -ENOMEM;
934 static struct blk_queue_tag *__blk_queue_init_tags(struct request_queue *q,
935 int depth)
937 struct blk_queue_tag *tags;
939 tags = kmalloc(sizeof(struct blk_queue_tag), GFP_ATOMIC);
940 if (!tags)
941 goto fail;
943 if (init_tag_map(q, tags, depth))
944 goto fail;
946 INIT_LIST_HEAD(&tags->busy_list);
947 tags->busy = 0;
948 atomic_set(&tags->refcnt, 1);
949 return tags;
950 fail:
951 kfree(tags);
952 return NULL;
956 * blk_init_tags - initialize the tag info for an external tag map
957 * @depth: the maximum queue depth supported
958 * @tags: the tag to use
960 struct blk_queue_tag *blk_init_tags(int depth)
962 return __blk_queue_init_tags(NULL, depth);
964 EXPORT_SYMBOL(blk_init_tags);
967 * blk_queue_init_tags - initialize the queue tag info
968 * @q: the request queue for the device
969 * @depth: the maximum queue depth supported
970 * @tags: the tag to use
972 int blk_queue_init_tags(request_queue_t *q, int depth,
973 struct blk_queue_tag *tags)
975 int rc;
977 BUG_ON(tags && q->queue_tags && tags != q->queue_tags);
979 if (!tags && !q->queue_tags) {
980 tags = __blk_queue_init_tags(q, depth);
982 if (!tags)
983 goto fail;
984 } else if (q->queue_tags) {
985 if ((rc = blk_queue_resize_tags(q, depth)))
986 return rc;
987 set_bit(QUEUE_FLAG_QUEUED, &q->queue_flags);
988 return 0;
989 } else
990 atomic_inc(&tags->refcnt);
993 * assign it, all done
995 q->queue_tags = tags;
996 q->queue_flags |= (1 << QUEUE_FLAG_QUEUED);
997 return 0;
998 fail:
999 kfree(tags);
1000 return -ENOMEM;
1003 EXPORT_SYMBOL(blk_queue_init_tags);
1006 * blk_queue_resize_tags - change the queueing depth
1007 * @q: the request queue for the device
1008 * @new_depth: the new max command queueing depth
1010 * Notes:
1011 * Must be called with the queue lock held.
1013 int blk_queue_resize_tags(request_queue_t *q, int new_depth)
1015 struct blk_queue_tag *bqt = q->queue_tags;
1016 struct request **tag_index;
1017 unsigned long *tag_map;
1018 int max_depth, nr_ulongs;
1020 if (!bqt)
1021 return -ENXIO;
1024 * if we already have large enough real_max_depth. just
1025 * adjust max_depth. *NOTE* as requests with tag value
1026 * between new_depth and real_max_depth can be in-flight, tag
1027 * map can not be shrunk blindly here.
1029 if (new_depth <= bqt->real_max_depth) {
1030 bqt->max_depth = new_depth;
1031 return 0;
1035 * Currently cannot replace a shared tag map with a new
1036 * one, so error out if this is the case
1038 if (atomic_read(&bqt->refcnt) != 1)
1039 return -EBUSY;
1042 * save the old state info, so we can copy it back
1044 tag_index = bqt->tag_index;
1045 tag_map = bqt->tag_map;
1046 max_depth = bqt->real_max_depth;
1048 if (init_tag_map(q, bqt, new_depth))
1049 return -ENOMEM;
1051 memcpy(bqt->tag_index, tag_index, max_depth * sizeof(struct request *));
1052 nr_ulongs = ALIGN(max_depth, BITS_PER_LONG) / BITS_PER_LONG;
1053 memcpy(bqt->tag_map, tag_map, nr_ulongs * sizeof(unsigned long));
1055 kfree(tag_index);
1056 kfree(tag_map);
1057 return 0;
1060 EXPORT_SYMBOL(blk_queue_resize_tags);
1063 * blk_queue_end_tag - end tag operations for a request
1064 * @q: the request queue for the device
1065 * @rq: the request that has completed
1067 * Description:
1068 * Typically called when end_that_request_first() returns 0, meaning
1069 * all transfers have been done for a request. It's important to call
1070 * this function before end_that_request_last(), as that will put the
1071 * request back on the free list thus corrupting the internal tag list.
1073 * Notes:
1074 * queue lock must be held.
1076 void blk_queue_end_tag(request_queue_t *q, struct request *rq)
1078 struct blk_queue_tag *bqt = q->queue_tags;
1079 int tag = rq->tag;
1081 BUG_ON(tag == -1);
1083 if (unlikely(tag >= bqt->real_max_depth))
1085 * This can happen after tag depth has been reduced.
1086 * FIXME: how about a warning or info message here?
1088 return;
1090 if (unlikely(!__test_and_clear_bit(tag, bqt->tag_map))) {
1091 printk(KERN_ERR "%s: attempt to clear non-busy tag (%d)\n",
1092 __FUNCTION__, tag);
1093 return;
1096 list_del_init(&rq->queuelist);
1097 rq->cmd_flags &= ~REQ_QUEUED;
1098 rq->tag = -1;
1100 if (unlikely(bqt->tag_index[tag] == NULL))
1101 printk(KERN_ERR "%s: tag %d is missing\n",
1102 __FUNCTION__, tag);
1104 bqt->tag_index[tag] = NULL;
1105 bqt->busy--;
1108 EXPORT_SYMBOL(blk_queue_end_tag);
1111 * blk_queue_start_tag - find a free tag and assign it
1112 * @q: the request queue for the device
1113 * @rq: the block request that needs tagging
1115 * Description:
1116 * This can either be used as a stand-alone helper, or possibly be
1117 * assigned as the queue &prep_rq_fn (in which case &struct request
1118 * automagically gets a tag assigned). Note that this function
1119 * assumes that any type of request can be queued! if this is not
1120 * true for your device, you must check the request type before
1121 * calling this function. The request will also be removed from
1122 * the request queue, so it's the drivers responsibility to readd
1123 * it if it should need to be restarted for some reason.
1125 * Notes:
1126 * queue lock must be held.
1128 int blk_queue_start_tag(request_queue_t *q, struct request *rq)
1130 struct blk_queue_tag *bqt = q->queue_tags;
1131 int tag;
1133 if (unlikely((rq->cmd_flags & REQ_QUEUED))) {
1134 printk(KERN_ERR
1135 "%s: request %p for device [%s] already tagged %d",
1136 __FUNCTION__, rq,
1137 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->tag);
1138 BUG();
1142 * Protect against shared tag maps, as we may not have exclusive
1143 * access to the tag map.
1145 do {
1146 tag = find_first_zero_bit(bqt->tag_map, bqt->max_depth);
1147 if (tag >= bqt->max_depth)
1148 return 1;
1150 } while (test_and_set_bit(tag, bqt->tag_map));
1152 rq->cmd_flags |= REQ_QUEUED;
1153 rq->tag = tag;
1154 bqt->tag_index[tag] = rq;
1155 blkdev_dequeue_request(rq);
1156 list_add(&rq->queuelist, &bqt->busy_list);
1157 bqt->busy++;
1158 return 0;
1161 EXPORT_SYMBOL(blk_queue_start_tag);
1164 * blk_queue_invalidate_tags - invalidate all pending tags
1165 * @q: the request queue for the device
1167 * Description:
1168 * Hardware conditions may dictate a need to stop all pending requests.
1169 * In this case, we will safely clear the block side of the tag queue and
1170 * readd all requests to the request queue in the right order.
1172 * Notes:
1173 * queue lock must be held.
1175 void blk_queue_invalidate_tags(request_queue_t *q)
1177 struct blk_queue_tag *bqt = q->queue_tags;
1178 struct list_head *tmp, *n;
1179 struct request *rq;
1181 list_for_each_safe(tmp, n, &bqt->busy_list) {
1182 rq = list_entry_rq(tmp);
1184 if (rq->tag == -1) {
1185 printk(KERN_ERR
1186 "%s: bad tag found on list\n", __FUNCTION__);
1187 list_del_init(&rq->queuelist);
1188 rq->cmd_flags &= ~REQ_QUEUED;
1189 } else
1190 blk_queue_end_tag(q, rq);
1192 rq->cmd_flags &= ~REQ_STARTED;
1193 __elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 0);
1197 EXPORT_SYMBOL(blk_queue_invalidate_tags);
1199 void blk_dump_rq_flags(struct request *rq, char *msg)
1201 int bit;
1203 printk("%s: dev %s: type=%x, flags=%x\n", msg,
1204 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
1205 rq->cmd_flags);
1207 printk("\nsector %llu, nr/cnr %lu/%u\n", (unsigned long long)rq->sector,
1208 rq->nr_sectors,
1209 rq->current_nr_sectors);
1210 printk("bio %p, biotail %p, buffer %p, data %p, len %u\n", rq->bio, rq->biotail, rq->buffer, rq->data, rq->data_len);
1212 if (blk_pc_request(rq)) {
1213 printk("cdb: ");
1214 for (bit = 0; bit < sizeof(rq->cmd); bit++)
1215 printk("%02x ", rq->cmd[bit]);
1216 printk("\n");
1220 EXPORT_SYMBOL(blk_dump_rq_flags);
1222 void blk_recount_segments(request_queue_t *q, struct bio *bio)
1224 struct bio_vec *bv, *bvprv = NULL;
1225 int i, nr_phys_segs, nr_hw_segs, seg_size, hw_seg_size, cluster;
1226 int high, highprv = 1;
1228 if (unlikely(!bio->bi_io_vec))
1229 return;
1231 cluster = q->queue_flags & (1 << QUEUE_FLAG_CLUSTER);
1232 hw_seg_size = seg_size = nr_phys_segs = nr_hw_segs = 0;
1233 bio_for_each_segment(bv, bio, i) {
1235 * the trick here is making sure that a high page is never
1236 * considered part of another segment, since that might
1237 * change with the bounce page.
1239 high = page_to_pfn(bv->bv_page) >= q->bounce_pfn;
1240 if (high || highprv)
1241 goto new_hw_segment;
1242 if (cluster) {
1243 if (seg_size + bv->bv_len > q->max_segment_size)
1244 goto new_segment;
1245 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bv))
1246 goto new_segment;
1247 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bv))
1248 goto new_segment;
1249 if (BIOVEC_VIRT_OVERSIZE(hw_seg_size + bv->bv_len))
1250 goto new_hw_segment;
1252 seg_size += bv->bv_len;
1253 hw_seg_size += bv->bv_len;
1254 bvprv = bv;
1255 continue;
1257 new_segment:
1258 if (BIOVEC_VIRT_MERGEABLE(bvprv, bv) &&
1259 !BIOVEC_VIRT_OVERSIZE(hw_seg_size + bv->bv_len)) {
1260 hw_seg_size += bv->bv_len;
1261 } else {
1262 new_hw_segment:
1263 if (hw_seg_size > bio->bi_hw_front_size)
1264 bio->bi_hw_front_size = hw_seg_size;
1265 hw_seg_size = BIOVEC_VIRT_START_SIZE(bv) + bv->bv_len;
1266 nr_hw_segs++;
1269 nr_phys_segs++;
1270 bvprv = bv;
1271 seg_size = bv->bv_len;
1272 highprv = high;
1274 if (hw_seg_size > bio->bi_hw_back_size)
1275 bio->bi_hw_back_size = hw_seg_size;
1276 if (nr_hw_segs == 1 && hw_seg_size > bio->bi_hw_front_size)
1277 bio->bi_hw_front_size = hw_seg_size;
1278 bio->bi_phys_segments = nr_phys_segs;
1279 bio->bi_hw_segments = nr_hw_segs;
1280 bio->bi_flags |= (1 << BIO_SEG_VALID);
1284 static int blk_phys_contig_segment(request_queue_t *q, struct bio *bio,
1285 struct bio *nxt)
1287 if (!(q->queue_flags & (1 << QUEUE_FLAG_CLUSTER)))
1288 return 0;
1290 if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)))
1291 return 0;
1292 if (bio->bi_size + nxt->bi_size > q->max_segment_size)
1293 return 0;
1296 * bio and nxt are contigous in memory, check if the queue allows
1297 * these two to be merged into one
1299 if (BIO_SEG_BOUNDARY(q, bio, nxt))
1300 return 1;
1302 return 0;
1305 static int blk_hw_contig_segment(request_queue_t *q, struct bio *bio,
1306 struct bio *nxt)
1308 if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
1309 blk_recount_segments(q, bio);
1310 if (unlikely(!bio_flagged(nxt, BIO_SEG_VALID)))
1311 blk_recount_segments(q, nxt);
1312 if (!BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)) ||
1313 BIOVEC_VIRT_OVERSIZE(bio->bi_hw_front_size + bio->bi_hw_back_size))
1314 return 0;
1315 if (bio->bi_size + nxt->bi_size > q->max_segment_size)
1316 return 0;
1318 return 1;
1322 * map a request to scatterlist, return number of sg entries setup. Caller
1323 * must make sure sg can hold rq->nr_phys_segments entries
1325 int blk_rq_map_sg(request_queue_t *q, struct request *rq, struct scatterlist *sg)
1327 struct bio_vec *bvec, *bvprv;
1328 struct bio *bio;
1329 int nsegs, i, cluster;
1331 nsegs = 0;
1332 cluster = q->queue_flags & (1 << QUEUE_FLAG_CLUSTER);
1335 * for each bio in rq
1337 bvprv = NULL;
1338 rq_for_each_bio(bio, rq) {
1340 * for each segment in bio
1342 bio_for_each_segment(bvec, bio, i) {
1343 int nbytes = bvec->bv_len;
1345 if (bvprv && cluster) {
1346 if (sg[nsegs - 1].length + nbytes > q->max_segment_size)
1347 goto new_segment;
1349 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
1350 goto new_segment;
1351 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
1352 goto new_segment;
1354 sg[nsegs - 1].length += nbytes;
1355 } else {
1356 new_segment:
1357 memset(&sg[nsegs],0,sizeof(struct scatterlist));
1358 sg[nsegs].page = bvec->bv_page;
1359 sg[nsegs].length = nbytes;
1360 sg[nsegs].offset = bvec->bv_offset;
1362 nsegs++;
1364 bvprv = bvec;
1365 } /* segments in bio */
1366 } /* bios in rq */
1368 return nsegs;
1371 EXPORT_SYMBOL(blk_rq_map_sg);
1374 * the standard queue merge functions, can be overridden with device
1375 * specific ones if so desired
1378 static inline int ll_new_mergeable(request_queue_t *q,
1379 struct request *req,
1380 struct bio *bio)
1382 int nr_phys_segs = bio_phys_segments(q, bio);
1384 if (req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
1385 req->cmd_flags |= REQ_NOMERGE;
1386 if (req == q->last_merge)
1387 q->last_merge = NULL;
1388 return 0;
1392 * A hw segment is just getting larger, bump just the phys
1393 * counter.
1395 req->nr_phys_segments += nr_phys_segs;
1396 return 1;
1399 static inline int ll_new_hw_segment(request_queue_t *q,
1400 struct request *req,
1401 struct bio *bio)
1403 int nr_hw_segs = bio_hw_segments(q, bio);
1404 int nr_phys_segs = bio_phys_segments(q, bio);
1406 if (req->nr_hw_segments + nr_hw_segs > q->max_hw_segments
1407 || req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
1408 req->cmd_flags |= REQ_NOMERGE;
1409 if (req == q->last_merge)
1410 q->last_merge = NULL;
1411 return 0;
1415 * This will form the start of a new hw segment. Bump both
1416 * counters.
1418 req->nr_hw_segments += nr_hw_segs;
1419 req->nr_phys_segments += nr_phys_segs;
1420 return 1;
1423 static int ll_back_merge_fn(request_queue_t *q, struct request *req,
1424 struct bio *bio)
1426 unsigned short max_sectors;
1427 int len;
1429 if (unlikely(blk_pc_request(req)))
1430 max_sectors = q->max_hw_sectors;
1431 else
1432 max_sectors = q->max_sectors;
1434 if (req->nr_sectors + bio_sectors(bio) > max_sectors) {
1435 req->cmd_flags |= REQ_NOMERGE;
1436 if (req == q->last_merge)
1437 q->last_merge = NULL;
1438 return 0;
1440 if (unlikely(!bio_flagged(req->biotail, BIO_SEG_VALID)))
1441 blk_recount_segments(q, req->biotail);
1442 if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
1443 blk_recount_segments(q, bio);
1444 len = req->biotail->bi_hw_back_size + bio->bi_hw_front_size;
1445 if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(req->biotail), __BVEC_START(bio)) &&
1446 !BIOVEC_VIRT_OVERSIZE(len)) {
1447 int mergeable = ll_new_mergeable(q, req, bio);
1449 if (mergeable) {
1450 if (req->nr_hw_segments == 1)
1451 req->bio->bi_hw_front_size = len;
1452 if (bio->bi_hw_segments == 1)
1453 bio->bi_hw_back_size = len;
1455 return mergeable;
1458 return ll_new_hw_segment(q, req, bio);
1461 static int ll_front_merge_fn(request_queue_t *q, struct request *req,
1462 struct bio *bio)
1464 unsigned short max_sectors;
1465 int len;
1467 if (unlikely(blk_pc_request(req)))
1468 max_sectors = q->max_hw_sectors;
1469 else
1470 max_sectors = q->max_sectors;
1473 if (req->nr_sectors + bio_sectors(bio) > max_sectors) {
1474 req->cmd_flags |= REQ_NOMERGE;
1475 if (req == q->last_merge)
1476 q->last_merge = NULL;
1477 return 0;
1479 len = bio->bi_hw_back_size + req->bio->bi_hw_front_size;
1480 if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
1481 blk_recount_segments(q, bio);
1482 if (unlikely(!bio_flagged(req->bio, BIO_SEG_VALID)))
1483 blk_recount_segments(q, req->bio);
1484 if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio), __BVEC_START(req->bio)) &&
1485 !BIOVEC_VIRT_OVERSIZE(len)) {
1486 int mergeable = ll_new_mergeable(q, req, bio);
1488 if (mergeable) {
1489 if (bio->bi_hw_segments == 1)
1490 bio->bi_hw_front_size = len;
1491 if (req->nr_hw_segments == 1)
1492 req->biotail->bi_hw_back_size = len;
1494 return mergeable;
1497 return ll_new_hw_segment(q, req, bio);
1500 static int ll_merge_requests_fn(request_queue_t *q, struct request *req,
1501 struct request *next)
1503 int total_phys_segments;
1504 int total_hw_segments;
1507 * First check if the either of the requests are re-queued
1508 * requests. Can't merge them if they are.
1510 if (req->special || next->special)
1511 return 0;
1514 * Will it become too large?
1516 if ((req->nr_sectors + next->nr_sectors) > q->max_sectors)
1517 return 0;
1519 total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
1520 if (blk_phys_contig_segment(q, req->biotail, next->bio))
1521 total_phys_segments--;
1523 if (total_phys_segments > q->max_phys_segments)
1524 return 0;
1526 total_hw_segments = req->nr_hw_segments + next->nr_hw_segments;
1527 if (blk_hw_contig_segment(q, req->biotail, next->bio)) {
1528 int len = req->biotail->bi_hw_back_size + next->bio->bi_hw_front_size;
1530 * propagate the combined length to the end of the requests
1532 if (req->nr_hw_segments == 1)
1533 req->bio->bi_hw_front_size = len;
1534 if (next->nr_hw_segments == 1)
1535 next->biotail->bi_hw_back_size = len;
1536 total_hw_segments--;
1539 if (total_hw_segments > q->max_hw_segments)
1540 return 0;
1542 /* Merge is OK... */
1543 req->nr_phys_segments = total_phys_segments;
1544 req->nr_hw_segments = total_hw_segments;
1545 return 1;
1549 * "plug" the device if there are no outstanding requests: this will
1550 * force the transfer to start only after we have put all the requests
1551 * on the list.
1553 * This is called with interrupts off and no requests on the queue and
1554 * with the queue lock held.
1556 void blk_plug_device(request_queue_t *q)
1558 WARN_ON(!irqs_disabled());
1561 * don't plug a stopped queue, it must be paired with blk_start_queue()
1562 * which will restart the queueing
1564 if (blk_queue_stopped(q))
1565 return;
1567 if (!test_and_set_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags)) {
1568 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
1569 blk_add_trace_generic(q, NULL, 0, BLK_TA_PLUG);
1573 EXPORT_SYMBOL(blk_plug_device);
1576 * remove the queue from the plugged list, if present. called with
1577 * queue lock held and interrupts disabled.
1579 int blk_remove_plug(request_queue_t *q)
1581 WARN_ON(!irqs_disabled());
1583 if (!test_and_clear_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags))
1584 return 0;
1586 del_timer(&q->unplug_timer);
1587 return 1;
1590 EXPORT_SYMBOL(blk_remove_plug);
1593 * remove the plug and let it rip..
1595 void __generic_unplug_device(request_queue_t *q)
1597 if (unlikely(blk_queue_stopped(q)))
1598 return;
1600 if (!blk_remove_plug(q))
1601 return;
1603 q->request_fn(q);
1605 EXPORT_SYMBOL(__generic_unplug_device);
1608 * generic_unplug_device - fire a request queue
1609 * @q: The &request_queue_t in question
1611 * Description:
1612 * Linux uses plugging to build bigger requests queues before letting
1613 * the device have at them. If a queue is plugged, the I/O scheduler
1614 * is still adding and merging requests on the queue. Once the queue
1615 * gets unplugged, the request_fn defined for the queue is invoked and
1616 * transfers started.
1618 void generic_unplug_device(request_queue_t *q)
1620 spin_lock_irq(q->queue_lock);
1621 __generic_unplug_device(q);
1622 spin_unlock_irq(q->queue_lock);
1624 EXPORT_SYMBOL(generic_unplug_device);
1626 static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
1627 struct page *page)
1629 request_queue_t *q = bdi->unplug_io_data;
1632 * devices don't necessarily have an ->unplug_fn defined
1634 if (q->unplug_fn) {
1635 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
1636 q->rq.count[READ] + q->rq.count[WRITE]);
1638 q->unplug_fn(q);
1642 static void blk_unplug_work(void *data)
1644 request_queue_t *q = data;
1646 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
1647 q->rq.count[READ] + q->rq.count[WRITE]);
1649 q->unplug_fn(q);
1652 static void blk_unplug_timeout(unsigned long data)
1654 request_queue_t *q = (request_queue_t *)data;
1656 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_TIMER, NULL,
1657 q->rq.count[READ] + q->rq.count[WRITE]);
1659 kblockd_schedule_work(&q->unplug_work);
1663 * blk_start_queue - restart a previously stopped queue
1664 * @q: The &request_queue_t in question
1666 * Description:
1667 * blk_start_queue() will clear the stop flag on the queue, and call
1668 * the request_fn for the queue if it was in a stopped state when
1669 * entered. Also see blk_stop_queue(). Queue lock must be held.
1671 void blk_start_queue(request_queue_t *q)
1673 WARN_ON(!irqs_disabled());
1675 clear_bit(QUEUE_FLAG_STOPPED, &q->queue_flags);
1678 * one level of recursion is ok and is much faster than kicking
1679 * the unplug handling
1681 if (!test_and_set_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) {
1682 q->request_fn(q);
1683 clear_bit(QUEUE_FLAG_REENTER, &q->queue_flags);
1684 } else {
1685 blk_plug_device(q);
1686 kblockd_schedule_work(&q->unplug_work);
1690 EXPORT_SYMBOL(blk_start_queue);
1693 * blk_stop_queue - stop a queue
1694 * @q: The &request_queue_t in question
1696 * Description:
1697 * The Linux block layer assumes that a block driver will consume all
1698 * entries on the request queue when the request_fn strategy is called.
1699 * Often this will not happen, because of hardware limitations (queue
1700 * depth settings). If a device driver gets a 'queue full' response,
1701 * or if it simply chooses not to queue more I/O at one point, it can
1702 * call this function to prevent the request_fn from being called until
1703 * the driver has signalled it's ready to go again. This happens by calling
1704 * blk_start_queue() to restart queue operations. Queue lock must be held.
1706 void blk_stop_queue(request_queue_t *q)
1708 blk_remove_plug(q);
1709 set_bit(QUEUE_FLAG_STOPPED, &q->queue_flags);
1711 EXPORT_SYMBOL(blk_stop_queue);
1714 * blk_sync_queue - cancel any pending callbacks on a queue
1715 * @q: the queue
1717 * Description:
1718 * The block layer may perform asynchronous callback activity
1719 * on a queue, such as calling the unplug function after a timeout.
1720 * A block device may call blk_sync_queue to ensure that any
1721 * such activity is cancelled, thus allowing it to release resources
1722 * the the callbacks might use. The caller must already have made sure
1723 * that its ->make_request_fn will not re-add plugging prior to calling
1724 * this function.
1727 void blk_sync_queue(struct request_queue *q)
1729 del_timer_sync(&q->unplug_timer);
1730 kblockd_flush();
1732 EXPORT_SYMBOL(blk_sync_queue);
1735 * blk_run_queue - run a single device queue
1736 * @q: The queue to run
1738 void blk_run_queue(struct request_queue *q)
1740 unsigned long flags;
1742 spin_lock_irqsave(q->queue_lock, flags);
1743 blk_remove_plug(q);
1746 * Only recurse once to avoid overrunning the stack, let the unplug
1747 * handling reinvoke the handler shortly if we already got there.
1749 if (!elv_queue_empty(q)) {
1750 if (!test_and_set_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) {
1751 q->request_fn(q);
1752 clear_bit(QUEUE_FLAG_REENTER, &q->queue_flags);
1753 } else {
1754 blk_plug_device(q);
1755 kblockd_schedule_work(&q->unplug_work);
1759 spin_unlock_irqrestore(q->queue_lock, flags);
1761 EXPORT_SYMBOL(blk_run_queue);
1764 * blk_cleanup_queue: - release a &request_queue_t when it is no longer needed
1765 * @kobj: the kobj belonging of the request queue to be released
1767 * Description:
1768 * blk_cleanup_queue is the pair to blk_init_queue() or
1769 * blk_queue_make_request(). It should be called when a request queue is
1770 * being released; typically when a block device is being de-registered.
1771 * Currently, its primary task it to free all the &struct request
1772 * structures that were allocated to the queue and the queue itself.
1774 * Caveat:
1775 * Hopefully the low level driver will have finished any
1776 * outstanding requests first...
1778 static void blk_release_queue(struct kobject *kobj)
1780 request_queue_t *q = container_of(kobj, struct request_queue, kobj);
1781 struct request_list *rl = &q->rq;
1783 blk_sync_queue(q);
1785 if (rl->rq_pool)
1786 mempool_destroy(rl->rq_pool);
1788 if (q->queue_tags)
1789 __blk_queue_free_tags(q);
1791 blk_trace_shutdown(q);
1793 kmem_cache_free(requestq_cachep, q);
1796 void blk_put_queue(request_queue_t *q)
1798 kobject_put(&q->kobj);
1800 EXPORT_SYMBOL(blk_put_queue);
1802 void blk_cleanup_queue(request_queue_t * q)
1804 mutex_lock(&q->sysfs_lock);
1805 set_bit(QUEUE_FLAG_DEAD, &q->queue_flags);
1806 mutex_unlock(&q->sysfs_lock);
1808 if (q->elevator)
1809 elevator_exit(q->elevator);
1811 blk_put_queue(q);
1814 EXPORT_SYMBOL(blk_cleanup_queue);
1816 static int blk_init_free_list(request_queue_t *q)
1818 struct request_list *rl = &q->rq;
1820 rl->count[READ] = rl->count[WRITE] = 0;
1821 rl->starved[READ] = rl->starved[WRITE] = 0;
1822 rl->elvpriv = 0;
1823 init_waitqueue_head(&rl->wait[READ]);
1824 init_waitqueue_head(&rl->wait[WRITE]);
1826 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
1827 mempool_free_slab, request_cachep, q->node);
1829 if (!rl->rq_pool)
1830 return -ENOMEM;
1832 return 0;
1835 request_queue_t *blk_alloc_queue(gfp_t gfp_mask)
1837 return blk_alloc_queue_node(gfp_mask, -1);
1839 EXPORT_SYMBOL(blk_alloc_queue);
1841 static struct kobj_type queue_ktype;
1843 request_queue_t *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
1845 request_queue_t *q;
1847 q = kmem_cache_alloc_node(requestq_cachep, gfp_mask, node_id);
1848 if (!q)
1849 return NULL;
1851 memset(q, 0, sizeof(*q));
1852 init_timer(&q->unplug_timer);
1854 snprintf(q->kobj.name, KOBJ_NAME_LEN, "%s", "queue");
1855 q->kobj.ktype = &queue_ktype;
1856 kobject_init(&q->kobj);
1858 q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
1859 q->backing_dev_info.unplug_io_data = q;
1861 mutex_init(&q->sysfs_lock);
1863 return q;
1865 EXPORT_SYMBOL(blk_alloc_queue_node);
1868 * blk_init_queue - prepare a request queue for use with a block device
1869 * @rfn: The function to be called to process requests that have been
1870 * placed on the queue.
1871 * @lock: Request queue spin lock
1873 * Description:
1874 * If a block device wishes to use the standard request handling procedures,
1875 * which sorts requests and coalesces adjacent requests, then it must
1876 * call blk_init_queue(). The function @rfn will be called when there
1877 * are requests on the queue that need to be processed. If the device
1878 * supports plugging, then @rfn may not be called immediately when requests
1879 * are available on the queue, but may be called at some time later instead.
1880 * Plugged queues are generally unplugged when a buffer belonging to one
1881 * of the requests on the queue is needed, or due to memory pressure.
1883 * @rfn is not required, or even expected, to remove all requests off the
1884 * queue, but only as many as it can handle at a time. If it does leave
1885 * requests on the queue, it is responsible for arranging that the requests
1886 * get dealt with eventually.
1888 * The queue spin lock must be held while manipulating the requests on the
1889 * request queue; this lock will be taken also from interrupt context, so irq
1890 * disabling is needed for it.
1892 * Function returns a pointer to the initialized request queue, or NULL if
1893 * it didn't succeed.
1895 * Note:
1896 * blk_init_queue() must be paired with a blk_cleanup_queue() call
1897 * when the block device is deactivated (such as at module unload).
1900 request_queue_t *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
1902 return blk_init_queue_node(rfn, lock, -1);
1904 EXPORT_SYMBOL(blk_init_queue);
1906 request_queue_t *
1907 blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
1909 request_queue_t *q = blk_alloc_queue_node(GFP_KERNEL, node_id);
1911 if (!q)
1912 return NULL;
1914 q->node = node_id;
1915 if (blk_init_free_list(q)) {
1916 kmem_cache_free(requestq_cachep, q);
1917 return NULL;
1921 * if caller didn't supply a lock, they get per-queue locking with
1922 * our embedded lock
1924 if (!lock) {
1925 spin_lock_init(&q->__queue_lock);
1926 lock = &q->__queue_lock;
1929 q->request_fn = rfn;
1930 q->back_merge_fn = ll_back_merge_fn;
1931 q->front_merge_fn = ll_front_merge_fn;
1932 q->merge_requests_fn = ll_merge_requests_fn;
1933 q->prep_rq_fn = NULL;
1934 q->unplug_fn = generic_unplug_device;
1935 q->queue_flags = (1 << QUEUE_FLAG_CLUSTER);
1936 q->queue_lock = lock;
1938 blk_queue_segment_boundary(q, 0xffffffff);
1940 blk_queue_make_request(q, __make_request);
1941 blk_queue_max_segment_size(q, MAX_SEGMENT_SIZE);
1943 blk_queue_max_hw_segments(q, MAX_HW_SEGMENTS);
1944 blk_queue_max_phys_segments(q, MAX_PHYS_SEGMENTS);
1947 * all done
1949 if (!elevator_init(q, NULL)) {
1950 blk_queue_congestion_threshold(q);
1951 return q;
1954 blk_put_queue(q);
1955 return NULL;
1957 EXPORT_SYMBOL(blk_init_queue_node);
1959 int blk_get_queue(request_queue_t *q)
1961 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
1962 kobject_get(&q->kobj);
1963 return 0;
1966 return 1;
1969 EXPORT_SYMBOL(blk_get_queue);
1971 static inline void blk_free_request(request_queue_t *q, struct request *rq)
1973 if (rq->cmd_flags & REQ_ELVPRIV)
1974 elv_put_request(q, rq);
1975 mempool_free(rq, q->rq.rq_pool);
1978 static struct request *
1979 blk_alloc_request(request_queue_t *q, int rw, int priv, gfp_t gfp_mask)
1981 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
1983 if (!rq)
1984 return NULL;
1987 * first three bits are identical in rq->cmd_flags and bio->bi_rw,
1988 * see bio.h and blkdev.h
1990 rq->cmd_flags = rw | REQ_ALLOCED;
1992 if (priv) {
1993 if (unlikely(elv_set_request(q, rq, gfp_mask))) {
1994 mempool_free(rq, q->rq.rq_pool);
1995 return NULL;
1997 rq->cmd_flags |= REQ_ELVPRIV;
2000 return rq;
2004 * ioc_batching returns true if the ioc is a valid batching request and
2005 * should be given priority access to a request.
2007 static inline int ioc_batching(request_queue_t *q, struct io_context *ioc)
2009 if (!ioc)
2010 return 0;
2013 * Make sure the process is able to allocate at least 1 request
2014 * even if the batch times out, otherwise we could theoretically
2015 * lose wakeups.
2017 return ioc->nr_batch_requests == q->nr_batching ||
2018 (ioc->nr_batch_requests > 0
2019 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
2023 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
2024 * will cause the process to be a "batcher" on all queues in the system. This
2025 * is the behaviour we want though - once it gets a wakeup it should be given
2026 * a nice run.
2028 static void ioc_set_batching(request_queue_t *q, struct io_context *ioc)
2030 if (!ioc || ioc_batching(q, ioc))
2031 return;
2033 ioc->nr_batch_requests = q->nr_batching;
2034 ioc->last_waited = jiffies;
2037 static void __freed_request(request_queue_t *q, int rw)
2039 struct request_list *rl = &q->rq;
2041 if (rl->count[rw] < queue_congestion_off_threshold(q))
2042 blk_clear_queue_congested(q, rw);
2044 if (rl->count[rw] + 1 <= q->nr_requests) {
2045 if (waitqueue_active(&rl->wait[rw]))
2046 wake_up(&rl->wait[rw]);
2048 blk_clear_queue_full(q, rw);
2053 * A request has just been released. Account for it, update the full and
2054 * congestion status, wake up any waiters. Called under q->queue_lock.
2056 static void freed_request(request_queue_t *q, int rw, int priv)
2058 struct request_list *rl = &q->rq;
2060 rl->count[rw]--;
2061 if (priv)
2062 rl->elvpriv--;
2064 __freed_request(q, rw);
2066 if (unlikely(rl->starved[rw ^ 1]))
2067 __freed_request(q, rw ^ 1);
2070 #define blkdev_free_rq(list) list_entry((list)->next, struct request, queuelist)
2072 * Get a free request, queue_lock must be held.
2073 * Returns NULL on failure, with queue_lock held.
2074 * Returns !NULL on success, with queue_lock *not held*.
2076 static struct request *get_request(request_queue_t *q, int rw, struct bio *bio,
2077 gfp_t gfp_mask)
2079 struct request *rq = NULL;
2080 struct request_list *rl = &q->rq;
2081 struct io_context *ioc = NULL;
2082 int may_queue, priv;
2084 may_queue = elv_may_queue(q, rw);
2085 if (may_queue == ELV_MQUEUE_NO)
2086 goto rq_starved;
2088 if (rl->count[rw]+1 >= queue_congestion_on_threshold(q)) {
2089 if (rl->count[rw]+1 >= q->nr_requests) {
2090 ioc = current_io_context(GFP_ATOMIC, q->node);
2092 * The queue will fill after this allocation, so set
2093 * it as full, and mark this process as "batching".
2094 * This process will be allowed to complete a batch of
2095 * requests, others will be blocked.
2097 if (!blk_queue_full(q, rw)) {
2098 ioc_set_batching(q, ioc);
2099 blk_set_queue_full(q, rw);
2100 } else {
2101 if (may_queue != ELV_MQUEUE_MUST
2102 && !ioc_batching(q, ioc)) {
2104 * The queue is full and the allocating
2105 * process is not a "batcher", and not
2106 * exempted by the IO scheduler
2108 goto out;
2112 blk_set_queue_congested(q, rw);
2116 * Only allow batching queuers to allocate up to 50% over the defined
2117 * limit of requests, otherwise we could have thousands of requests
2118 * allocated with any setting of ->nr_requests
2120 if (rl->count[rw] >= (3 * q->nr_requests / 2))
2121 goto out;
2123 rl->count[rw]++;
2124 rl->starved[rw] = 0;
2126 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
2127 if (priv)
2128 rl->elvpriv++;
2130 spin_unlock_irq(q->queue_lock);
2132 rq = blk_alloc_request(q, rw, priv, gfp_mask);
2133 if (unlikely(!rq)) {
2135 * Allocation failed presumably due to memory. Undo anything
2136 * we might have messed up.
2138 * Allocating task should really be put onto the front of the
2139 * wait queue, but this is pretty rare.
2141 spin_lock_irq(q->queue_lock);
2142 freed_request(q, rw, priv);
2145 * in the very unlikely event that allocation failed and no
2146 * requests for this direction was pending, mark us starved
2147 * so that freeing of a request in the other direction will
2148 * notice us. another possible fix would be to split the
2149 * rq mempool into READ and WRITE
2151 rq_starved:
2152 if (unlikely(rl->count[rw] == 0))
2153 rl->starved[rw] = 1;
2155 goto out;
2159 * ioc may be NULL here, and ioc_batching will be false. That's
2160 * OK, if the queue is under the request limit then requests need
2161 * not count toward the nr_batch_requests limit. There will always
2162 * be some limit enforced by BLK_BATCH_TIME.
2164 if (ioc_batching(q, ioc))
2165 ioc->nr_batch_requests--;
2167 rq_init(q, rq);
2169 blk_add_trace_generic(q, bio, rw, BLK_TA_GETRQ);
2170 out:
2171 return rq;
2175 * No available requests for this queue, unplug the device and wait for some
2176 * requests to become available.
2178 * Called with q->queue_lock held, and returns with it unlocked.
2180 static struct request *get_request_wait(request_queue_t *q, int rw,
2181 struct bio *bio)
2183 struct request *rq;
2185 rq = get_request(q, rw, bio, GFP_NOIO);
2186 while (!rq) {
2187 DEFINE_WAIT(wait);
2188 struct request_list *rl = &q->rq;
2190 prepare_to_wait_exclusive(&rl->wait[rw], &wait,
2191 TASK_UNINTERRUPTIBLE);
2193 rq = get_request(q, rw, bio, GFP_NOIO);
2195 if (!rq) {
2196 struct io_context *ioc;
2198 blk_add_trace_generic(q, bio, rw, BLK_TA_SLEEPRQ);
2200 __generic_unplug_device(q);
2201 spin_unlock_irq(q->queue_lock);
2202 io_schedule();
2205 * After sleeping, we become a "batching" process and
2206 * will be able to allocate at least one request, and
2207 * up to a big batch of them for a small period time.
2208 * See ioc_batching, ioc_set_batching
2210 ioc = current_io_context(GFP_NOIO, q->node);
2211 ioc_set_batching(q, ioc);
2213 spin_lock_irq(q->queue_lock);
2215 finish_wait(&rl->wait[rw], &wait);
2218 return rq;
2221 struct request *blk_get_request(request_queue_t *q, int rw, gfp_t gfp_mask)
2223 struct request *rq;
2225 BUG_ON(rw != READ && rw != WRITE);
2227 spin_lock_irq(q->queue_lock);
2228 if (gfp_mask & __GFP_WAIT) {
2229 rq = get_request_wait(q, rw, NULL);
2230 } else {
2231 rq = get_request(q, rw, NULL, gfp_mask);
2232 if (!rq)
2233 spin_unlock_irq(q->queue_lock);
2235 /* q->queue_lock is unlocked at this point */
2237 return rq;
2239 EXPORT_SYMBOL(blk_get_request);
2242 * blk_start_queueing - initiate dispatch of requests to device
2243 * @q: request queue to kick into gear
2245 * This is basically a helper to remove the need to know whether a queue
2246 * is plugged or not if someone just wants to initiate dispatch of requests
2247 * for this queue.
2249 * The queue lock must be held with interrupts disabled.
2251 void blk_start_queueing(request_queue_t *q)
2253 if (!blk_queue_plugged(q))
2254 q->request_fn(q);
2255 else
2256 __generic_unplug_device(q);
2258 EXPORT_SYMBOL(blk_start_queueing);
2261 * blk_requeue_request - put a request back on queue
2262 * @q: request queue where request should be inserted
2263 * @rq: request to be inserted
2265 * Description:
2266 * Drivers often keep queueing requests until the hardware cannot accept
2267 * more, when that condition happens we need to put the request back
2268 * on the queue. Must be called with queue lock held.
2270 void blk_requeue_request(request_queue_t *q, struct request *rq)
2272 blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
2274 if (blk_rq_tagged(rq))
2275 blk_queue_end_tag(q, rq);
2277 elv_requeue_request(q, rq);
2280 EXPORT_SYMBOL(blk_requeue_request);
2283 * blk_insert_request - insert a special request in to a request queue
2284 * @q: request queue where request should be inserted
2285 * @rq: request to be inserted
2286 * @at_head: insert request at head or tail of queue
2287 * @data: private data
2289 * Description:
2290 * Many block devices need to execute commands asynchronously, so they don't
2291 * block the whole kernel from preemption during request execution. This is
2292 * accomplished normally by inserting aritficial requests tagged as
2293 * REQ_SPECIAL in to the corresponding request queue, and letting them be
2294 * scheduled for actual execution by the request queue.
2296 * We have the option of inserting the head or the tail of the queue.
2297 * Typically we use the tail for new ioctls and so forth. We use the head
2298 * of the queue for things like a QUEUE_FULL message from a device, or a
2299 * host that is unable to accept a particular command.
2301 void blk_insert_request(request_queue_t *q, struct request *rq,
2302 int at_head, void *data)
2304 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
2305 unsigned long flags;
2308 * tell I/O scheduler that this isn't a regular read/write (ie it
2309 * must not attempt merges on this) and that it acts as a soft
2310 * barrier
2312 rq->cmd_type = REQ_TYPE_SPECIAL;
2313 rq->cmd_flags |= REQ_SOFTBARRIER;
2315 rq->special = data;
2317 spin_lock_irqsave(q->queue_lock, flags);
2320 * If command is tagged, release the tag
2322 if (blk_rq_tagged(rq))
2323 blk_queue_end_tag(q, rq);
2325 drive_stat_acct(rq, rq->nr_sectors, 1);
2326 __elv_add_request(q, rq, where, 0);
2327 blk_start_queueing(q);
2328 spin_unlock_irqrestore(q->queue_lock, flags);
2331 EXPORT_SYMBOL(blk_insert_request);
2334 * blk_rq_map_user - map user data to a request, for REQ_BLOCK_PC usage
2335 * @q: request queue where request should be inserted
2336 * @rq: request structure to fill
2337 * @ubuf: the user buffer
2338 * @len: length of user data
2340 * Description:
2341 * Data will be mapped directly for zero copy io, if possible. Otherwise
2342 * a kernel bounce buffer is used.
2344 * A matching blk_rq_unmap_user() must be issued at the end of io, while
2345 * still in process context.
2347 * Note: The mapped bio may need to be bounced through blk_queue_bounce()
2348 * before being submitted to the device, as pages mapped may be out of
2349 * reach. It's the callers responsibility to make sure this happens. The
2350 * original bio must be passed back in to blk_rq_unmap_user() for proper
2351 * unmapping.
2353 int blk_rq_map_user(request_queue_t *q, struct request *rq, void __user *ubuf,
2354 unsigned int len)
2356 unsigned long uaddr;
2357 struct bio *bio;
2358 int reading;
2360 if (len > (q->max_hw_sectors << 9))
2361 return -EINVAL;
2362 if (!len || !ubuf)
2363 return -EINVAL;
2365 reading = rq_data_dir(rq) == READ;
2368 * if alignment requirement is satisfied, map in user pages for
2369 * direct dma. else, set up kernel bounce buffers
2371 uaddr = (unsigned long) ubuf;
2372 if (!(uaddr & queue_dma_alignment(q)) && !(len & queue_dma_alignment(q)))
2373 bio = bio_map_user(q, NULL, uaddr, len, reading);
2374 else
2375 bio = bio_copy_user(q, uaddr, len, reading);
2377 if (!IS_ERR(bio)) {
2378 rq->bio = rq->biotail = bio;
2379 blk_rq_bio_prep(q, rq, bio);
2381 rq->buffer = rq->data = NULL;
2382 rq->data_len = len;
2383 return 0;
2387 * bio is the err-ptr
2389 return PTR_ERR(bio);
2392 EXPORT_SYMBOL(blk_rq_map_user);
2395 * blk_rq_map_user_iov - map user data to a request, for REQ_BLOCK_PC usage
2396 * @q: request queue where request should be inserted
2397 * @rq: request to map data to
2398 * @iov: pointer to the iovec
2399 * @iov_count: number of elements in the iovec
2401 * Description:
2402 * Data will be mapped directly for zero copy io, if possible. Otherwise
2403 * a kernel bounce buffer is used.
2405 * A matching blk_rq_unmap_user() must be issued at the end of io, while
2406 * still in process context.
2408 * Note: The mapped bio may need to be bounced through blk_queue_bounce()
2409 * before being submitted to the device, as pages mapped may be out of
2410 * reach. It's the callers responsibility to make sure this happens. The
2411 * original bio must be passed back in to blk_rq_unmap_user() for proper
2412 * unmapping.
2414 int blk_rq_map_user_iov(request_queue_t *q, struct request *rq,
2415 struct sg_iovec *iov, int iov_count)
2417 struct bio *bio;
2419 if (!iov || iov_count <= 0)
2420 return -EINVAL;
2422 /* we don't allow misaligned data like bio_map_user() does. If the
2423 * user is using sg, they're expected to know the alignment constraints
2424 * and respect them accordingly */
2425 bio = bio_map_user_iov(q, NULL, iov, iov_count, rq_data_dir(rq)== READ);
2426 if (IS_ERR(bio))
2427 return PTR_ERR(bio);
2429 rq->bio = rq->biotail = bio;
2430 blk_rq_bio_prep(q, rq, bio);
2431 rq->buffer = rq->data = NULL;
2432 rq->data_len = bio->bi_size;
2433 return 0;
2436 EXPORT_SYMBOL(blk_rq_map_user_iov);
2439 * blk_rq_unmap_user - unmap a request with user data
2440 * @bio: bio to be unmapped
2441 * @ulen: length of user buffer
2443 * Description:
2444 * Unmap a bio previously mapped by blk_rq_map_user().
2446 int blk_rq_unmap_user(struct bio *bio, unsigned int ulen)
2448 int ret = 0;
2450 if (bio) {
2451 if (bio_flagged(bio, BIO_USER_MAPPED))
2452 bio_unmap_user(bio);
2453 else
2454 ret = bio_uncopy_user(bio);
2457 return 0;
2460 EXPORT_SYMBOL(blk_rq_unmap_user);
2463 * blk_rq_map_kern - map kernel data to a request, for REQ_BLOCK_PC usage
2464 * @q: request queue where request should be inserted
2465 * @rq: request to fill
2466 * @kbuf: the kernel buffer
2467 * @len: length of user data
2468 * @gfp_mask: memory allocation flags
2470 int blk_rq_map_kern(request_queue_t *q, struct request *rq, void *kbuf,
2471 unsigned int len, gfp_t gfp_mask)
2473 struct bio *bio;
2475 if (len > (q->max_hw_sectors << 9))
2476 return -EINVAL;
2477 if (!len || !kbuf)
2478 return -EINVAL;
2480 bio = bio_map_kern(q, kbuf, len, gfp_mask);
2481 if (IS_ERR(bio))
2482 return PTR_ERR(bio);
2484 if (rq_data_dir(rq) == WRITE)
2485 bio->bi_rw |= (1 << BIO_RW);
2487 rq->bio = rq->biotail = bio;
2488 blk_rq_bio_prep(q, rq, bio);
2490 rq->buffer = rq->data = NULL;
2491 rq->data_len = len;
2492 return 0;
2495 EXPORT_SYMBOL(blk_rq_map_kern);
2498 * blk_execute_rq_nowait - insert a request into queue for execution
2499 * @q: queue to insert the request in
2500 * @bd_disk: matching gendisk
2501 * @rq: request to insert
2502 * @at_head: insert request at head or tail of queue
2503 * @done: I/O completion handler
2505 * Description:
2506 * Insert a fully prepared request at the back of the io scheduler queue
2507 * for execution. Don't wait for completion.
2509 void blk_execute_rq_nowait(request_queue_t *q, struct gendisk *bd_disk,
2510 struct request *rq, int at_head,
2511 rq_end_io_fn *done)
2513 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
2515 rq->rq_disk = bd_disk;
2516 rq->cmd_flags |= REQ_NOMERGE;
2517 rq->end_io = done;
2518 WARN_ON(irqs_disabled());
2519 spin_lock_irq(q->queue_lock);
2520 __elv_add_request(q, rq, where, 1);
2521 __generic_unplug_device(q);
2522 spin_unlock_irq(q->queue_lock);
2524 EXPORT_SYMBOL_GPL(blk_execute_rq_nowait);
2527 * blk_execute_rq - insert a request into queue for execution
2528 * @q: queue to insert the request in
2529 * @bd_disk: matching gendisk
2530 * @rq: request to insert
2531 * @at_head: insert request at head or tail of queue
2533 * Description:
2534 * Insert a fully prepared request at the back of the io scheduler queue
2535 * for execution and wait for completion.
2537 int blk_execute_rq(request_queue_t *q, struct gendisk *bd_disk,
2538 struct request *rq, int at_head)
2540 DECLARE_COMPLETION_ONSTACK(wait);
2541 char sense[SCSI_SENSE_BUFFERSIZE];
2542 int err = 0;
2545 * we need an extra reference to the request, so we can look at
2546 * it after io completion
2548 rq->ref_count++;
2550 if (!rq->sense) {
2551 memset(sense, 0, sizeof(sense));
2552 rq->sense = sense;
2553 rq->sense_len = 0;
2556 rq->end_io_data = &wait;
2557 blk_execute_rq_nowait(q, bd_disk, rq, at_head, blk_end_sync_rq);
2558 wait_for_completion(&wait);
2560 if (rq->errors)
2561 err = -EIO;
2563 return err;
2566 EXPORT_SYMBOL(blk_execute_rq);
2569 * blkdev_issue_flush - queue a flush
2570 * @bdev: blockdev to issue flush for
2571 * @error_sector: error sector
2573 * Description:
2574 * Issue a flush for the block device in question. Caller can supply
2575 * room for storing the error offset in case of a flush error, if they
2576 * wish to. Caller must run wait_for_completion() on its own.
2578 int blkdev_issue_flush(struct block_device *bdev, sector_t *error_sector)
2580 request_queue_t *q;
2582 if (bdev->bd_disk == NULL)
2583 return -ENXIO;
2585 q = bdev_get_queue(bdev);
2586 if (!q)
2587 return -ENXIO;
2588 if (!q->issue_flush_fn)
2589 return -EOPNOTSUPP;
2591 return q->issue_flush_fn(q, bdev->bd_disk, error_sector);
2594 EXPORT_SYMBOL(blkdev_issue_flush);
2596 static void drive_stat_acct(struct request *rq, int nr_sectors, int new_io)
2598 int rw = rq_data_dir(rq);
2600 if (!blk_fs_request(rq) || !rq->rq_disk)
2601 return;
2603 if (!new_io) {
2604 __disk_stat_inc(rq->rq_disk, merges[rw]);
2605 } else {
2606 disk_round_stats(rq->rq_disk);
2607 rq->rq_disk->in_flight++;
2612 * add-request adds a request to the linked list.
2613 * queue lock is held and interrupts disabled, as we muck with the
2614 * request queue list.
2616 static inline void add_request(request_queue_t * q, struct request * req)
2618 drive_stat_acct(req, req->nr_sectors, 1);
2620 if (q->activity_fn)
2621 q->activity_fn(q->activity_data, rq_data_dir(req));
2624 * elevator indicated where it wants this request to be
2625 * inserted at elevator_merge time
2627 __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
2631 * disk_round_stats() - Round off the performance stats on a struct
2632 * disk_stats.
2634 * The average IO queue length and utilisation statistics are maintained
2635 * by observing the current state of the queue length and the amount of
2636 * time it has been in this state for.
2638 * Normally, that accounting is done on IO completion, but that can result
2639 * in more than a second's worth of IO being accounted for within any one
2640 * second, leading to >100% utilisation. To deal with that, we call this
2641 * function to do a round-off before returning the results when reading
2642 * /proc/diskstats. This accounts immediately for all queue usage up to
2643 * the current jiffies and restarts the counters again.
2645 void disk_round_stats(struct gendisk *disk)
2647 unsigned long now = jiffies;
2649 if (now == disk->stamp)
2650 return;
2652 if (disk->in_flight) {
2653 __disk_stat_add(disk, time_in_queue,
2654 disk->in_flight * (now - disk->stamp));
2655 __disk_stat_add(disk, io_ticks, (now - disk->stamp));
2657 disk->stamp = now;
2660 EXPORT_SYMBOL_GPL(disk_round_stats);
2663 * queue lock must be held
2665 void __blk_put_request(request_queue_t *q, struct request *req)
2667 if (unlikely(!q))
2668 return;
2669 if (unlikely(--req->ref_count))
2670 return;
2672 elv_completed_request(q, req);
2675 * Request may not have originated from ll_rw_blk. if not,
2676 * it didn't come out of our reserved rq pools
2678 if (req->cmd_flags & REQ_ALLOCED) {
2679 int rw = rq_data_dir(req);
2680 int priv = req->cmd_flags & REQ_ELVPRIV;
2682 BUG_ON(!list_empty(&req->queuelist));
2683 BUG_ON(!hlist_unhashed(&req->hash));
2685 blk_free_request(q, req);
2686 freed_request(q, rw, priv);
2690 EXPORT_SYMBOL_GPL(__blk_put_request);
2692 void blk_put_request(struct request *req)
2694 unsigned long flags;
2695 request_queue_t *q = req->q;
2698 * Gee, IDE calls in w/ NULL q. Fix IDE and remove the
2699 * following if (q) test.
2701 if (q) {
2702 spin_lock_irqsave(q->queue_lock, flags);
2703 __blk_put_request(q, req);
2704 spin_unlock_irqrestore(q->queue_lock, flags);
2708 EXPORT_SYMBOL(blk_put_request);
2711 * blk_end_sync_rq - executes a completion event on a request
2712 * @rq: request to complete
2713 * @error: end io status of the request
2715 void blk_end_sync_rq(struct request *rq, int error)
2717 struct completion *waiting = rq->end_io_data;
2719 rq->end_io_data = NULL;
2720 __blk_put_request(rq->q, rq);
2723 * complete last, if this is a stack request the process (and thus
2724 * the rq pointer) could be invalid right after this complete()
2726 complete(waiting);
2728 EXPORT_SYMBOL(blk_end_sync_rq);
2731 * Has to be called with the request spinlock acquired
2733 static int attempt_merge(request_queue_t *q, struct request *req,
2734 struct request *next)
2736 if (!rq_mergeable(req) || !rq_mergeable(next))
2737 return 0;
2740 * not contiguous
2742 if (req->sector + req->nr_sectors != next->sector)
2743 return 0;
2745 if (rq_data_dir(req) != rq_data_dir(next)
2746 || req->rq_disk != next->rq_disk
2747 || next->special)
2748 return 0;
2751 * If we are allowed to merge, then append bio list
2752 * from next to rq and release next. merge_requests_fn
2753 * will have updated segment counts, update sector
2754 * counts here.
2756 if (!q->merge_requests_fn(q, req, next))
2757 return 0;
2760 * At this point we have either done a back merge
2761 * or front merge. We need the smaller start_time of
2762 * the merged requests to be the current request
2763 * for accounting purposes.
2765 if (time_after(req->start_time, next->start_time))
2766 req->start_time = next->start_time;
2768 req->biotail->bi_next = next->bio;
2769 req->biotail = next->biotail;
2771 req->nr_sectors = req->hard_nr_sectors += next->hard_nr_sectors;
2773 elv_merge_requests(q, req, next);
2775 if (req->rq_disk) {
2776 disk_round_stats(req->rq_disk);
2777 req->rq_disk->in_flight--;
2780 req->ioprio = ioprio_best(req->ioprio, next->ioprio);
2782 __blk_put_request(q, next);
2783 return 1;
2786 static inline int attempt_back_merge(request_queue_t *q, struct request *rq)
2788 struct request *next = elv_latter_request(q, rq);
2790 if (next)
2791 return attempt_merge(q, rq, next);
2793 return 0;
2796 static inline int attempt_front_merge(request_queue_t *q, struct request *rq)
2798 struct request *prev = elv_former_request(q, rq);
2800 if (prev)
2801 return attempt_merge(q, prev, rq);
2803 return 0;
2806 static void init_request_from_bio(struct request *req, struct bio *bio)
2808 req->cmd_type = REQ_TYPE_FS;
2811 * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
2813 if (bio_rw_ahead(bio) || bio_failfast(bio))
2814 req->cmd_flags |= REQ_FAILFAST;
2817 * REQ_BARRIER implies no merging, but lets make it explicit
2819 if (unlikely(bio_barrier(bio)))
2820 req->cmd_flags |= (REQ_HARDBARRIER | REQ_NOMERGE);
2822 if (bio_sync(bio))
2823 req->cmd_flags |= REQ_RW_SYNC;
2824 if (bio_rw_meta(bio))
2825 req->cmd_flags |= REQ_RW_META;
2827 req->errors = 0;
2828 req->hard_sector = req->sector = bio->bi_sector;
2829 req->hard_nr_sectors = req->nr_sectors = bio_sectors(bio);
2830 req->current_nr_sectors = req->hard_cur_sectors = bio_cur_sectors(bio);
2831 req->nr_phys_segments = bio_phys_segments(req->q, bio);
2832 req->nr_hw_segments = bio_hw_segments(req->q, bio);
2833 req->buffer = bio_data(bio); /* see ->buffer comment above */
2834 req->bio = req->biotail = bio;
2835 req->ioprio = bio_prio(bio);
2836 req->rq_disk = bio->bi_bdev->bd_disk;
2837 req->start_time = jiffies;
2840 static int __make_request(request_queue_t *q, struct bio *bio)
2842 struct request *req;
2843 int el_ret, nr_sectors, barrier, err;
2844 const unsigned short prio = bio_prio(bio);
2845 const int sync = bio_sync(bio);
2847 nr_sectors = bio_sectors(bio);
2850 * low level driver can indicate that it wants pages above a
2851 * certain limit bounced to low memory (ie for highmem, or even
2852 * ISA dma in theory)
2854 blk_queue_bounce(q, &bio);
2856 barrier = bio_barrier(bio);
2857 if (unlikely(barrier) && (q->next_ordered == QUEUE_ORDERED_NONE)) {
2858 err = -EOPNOTSUPP;
2859 goto end_io;
2862 spin_lock_irq(q->queue_lock);
2864 if (unlikely(barrier) || elv_queue_empty(q))
2865 goto get_rq;
2867 el_ret = elv_merge(q, &req, bio);
2868 switch (el_ret) {
2869 case ELEVATOR_BACK_MERGE:
2870 BUG_ON(!rq_mergeable(req));
2872 if (!q->back_merge_fn(q, req, bio))
2873 break;
2875 blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
2877 req->biotail->bi_next = bio;
2878 req->biotail = bio;
2879 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
2880 req->ioprio = ioprio_best(req->ioprio, prio);
2881 drive_stat_acct(req, nr_sectors, 0);
2882 if (!attempt_back_merge(q, req))
2883 elv_merged_request(q, req, el_ret);
2884 goto out;
2886 case ELEVATOR_FRONT_MERGE:
2887 BUG_ON(!rq_mergeable(req));
2889 if (!q->front_merge_fn(q, req, bio))
2890 break;
2892 blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
2894 bio->bi_next = req->bio;
2895 req->bio = bio;
2898 * may not be valid. if the low level driver said
2899 * it didn't need a bounce buffer then it better
2900 * not touch req->buffer either...
2902 req->buffer = bio_data(bio);
2903 req->current_nr_sectors = bio_cur_sectors(bio);
2904 req->hard_cur_sectors = req->current_nr_sectors;
2905 req->sector = req->hard_sector = bio->bi_sector;
2906 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
2907 req->ioprio = ioprio_best(req->ioprio, prio);
2908 drive_stat_acct(req, nr_sectors, 0);
2909 if (!attempt_front_merge(q, req))
2910 elv_merged_request(q, req, el_ret);
2911 goto out;
2913 /* ELV_NO_MERGE: elevator says don't/can't merge. */
2914 default:
2918 get_rq:
2920 * Grab a free request. This is might sleep but can not fail.
2921 * Returns with the queue unlocked.
2923 req = get_request_wait(q, bio_data_dir(bio), bio);
2926 * After dropping the lock and possibly sleeping here, our request
2927 * may now be mergeable after it had proven unmergeable (above).
2928 * We don't worry about that case for efficiency. It won't happen
2929 * often, and the elevators are able to handle it.
2931 init_request_from_bio(req, bio);
2933 spin_lock_irq(q->queue_lock);
2934 if (elv_queue_empty(q))
2935 blk_plug_device(q);
2936 add_request(q, req);
2937 out:
2938 if (sync)
2939 __generic_unplug_device(q);
2941 spin_unlock_irq(q->queue_lock);
2942 return 0;
2944 end_io:
2945 bio_endio(bio, nr_sectors << 9, err);
2946 return 0;
2950 * If bio->bi_dev is a partition, remap the location
2952 static inline void blk_partition_remap(struct bio *bio)
2954 struct block_device *bdev = bio->bi_bdev;
2956 if (bdev != bdev->bd_contains) {
2957 struct hd_struct *p = bdev->bd_part;
2958 const int rw = bio_data_dir(bio);
2960 p->sectors[rw] += bio_sectors(bio);
2961 p->ios[rw]++;
2963 bio->bi_sector += p->start_sect;
2964 bio->bi_bdev = bdev->bd_contains;
2968 static void handle_bad_sector(struct bio *bio)
2970 char b[BDEVNAME_SIZE];
2972 printk(KERN_INFO "attempt to access beyond end of device\n");
2973 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
2974 bdevname(bio->bi_bdev, b),
2975 bio->bi_rw,
2976 (unsigned long long)bio->bi_sector + bio_sectors(bio),
2977 (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
2979 set_bit(BIO_EOF, &bio->bi_flags);
2983 * generic_make_request: hand a buffer to its device driver for I/O
2984 * @bio: The bio describing the location in memory and on the device.
2986 * generic_make_request() is used to make I/O requests of block
2987 * devices. It is passed a &struct bio, which describes the I/O that needs
2988 * to be done.
2990 * generic_make_request() does not return any status. The
2991 * success/failure status of the request, along with notification of
2992 * completion, is delivered asynchronously through the bio->bi_end_io
2993 * function described (one day) else where.
2995 * The caller of generic_make_request must make sure that bi_io_vec
2996 * are set to describe the memory buffer, and that bi_dev and bi_sector are
2997 * set to describe the device address, and the
2998 * bi_end_io and optionally bi_private are set to describe how
2999 * completion notification should be signaled.
3001 * generic_make_request and the drivers it calls may use bi_next if this
3002 * bio happens to be merged with someone else, and may change bi_dev and
3003 * bi_sector for remaps as it sees fit. So the values of these fields
3004 * should NOT be depended on after the call to generic_make_request.
3006 void generic_make_request(struct bio *bio)
3008 request_queue_t *q;
3009 sector_t maxsector;
3010 sector_t old_sector;
3011 int ret, nr_sectors = bio_sectors(bio);
3012 dev_t old_dev;
3014 might_sleep();
3015 /* Test device or partition size, when known. */
3016 maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
3017 if (maxsector) {
3018 sector_t sector = bio->bi_sector;
3020 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
3022 * This may well happen - the kernel calls bread()
3023 * without checking the size of the device, e.g., when
3024 * mounting a device.
3026 handle_bad_sector(bio);
3027 goto end_io;
3032 * Resolve the mapping until finished. (drivers are
3033 * still free to implement/resolve their own stacking
3034 * by explicitly returning 0)
3036 * NOTE: we don't repeat the blk_size check for each new device.
3037 * Stacking drivers are expected to know what they are doing.
3039 old_sector = -1;
3040 old_dev = 0;
3041 do {
3042 char b[BDEVNAME_SIZE];
3044 q = bdev_get_queue(bio->bi_bdev);
3045 if (!q) {
3046 printk(KERN_ERR
3047 "generic_make_request: Trying to access "
3048 "nonexistent block-device %s (%Lu)\n",
3049 bdevname(bio->bi_bdev, b),
3050 (long long) bio->bi_sector);
3051 end_io:
3052 bio_endio(bio, bio->bi_size, -EIO);
3053 break;
3056 if (unlikely(bio_sectors(bio) > q->max_hw_sectors)) {
3057 printk("bio too big device %s (%u > %u)\n",
3058 bdevname(bio->bi_bdev, b),
3059 bio_sectors(bio),
3060 q->max_hw_sectors);
3061 goto end_io;
3064 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
3065 goto end_io;
3068 * If this device has partitions, remap block n
3069 * of partition p to block n+start(p) of the disk.
3071 blk_partition_remap(bio);
3073 if (old_sector != -1)
3074 blk_add_trace_remap(q, bio, old_dev, bio->bi_sector,
3075 old_sector);
3077 blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
3079 old_sector = bio->bi_sector;
3080 old_dev = bio->bi_bdev->bd_dev;
3082 maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
3083 if (maxsector) {
3084 sector_t sector = bio->bi_sector;
3086 if (maxsector < nr_sectors ||
3087 maxsector - nr_sectors < sector) {
3089 * This may well happen - partitions are not
3090 * checked to make sure they are within the size
3091 * of the whole device.
3093 handle_bad_sector(bio);
3094 goto end_io;
3098 ret = q->make_request_fn(q, bio);
3099 } while (ret);
3102 EXPORT_SYMBOL(generic_make_request);
3105 * submit_bio: submit a bio to the block device layer for I/O
3106 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
3107 * @bio: The &struct bio which describes the I/O
3109 * submit_bio() is very similar in purpose to generic_make_request(), and
3110 * uses that function to do most of the work. Both are fairly rough
3111 * interfaces, @bio must be presetup and ready for I/O.
3114 void submit_bio(int rw, struct bio *bio)
3116 int count = bio_sectors(bio);
3118 BIO_BUG_ON(!bio->bi_size);
3119 BIO_BUG_ON(!bio->bi_io_vec);
3120 bio->bi_rw |= rw;
3121 if (rw & WRITE)
3122 count_vm_events(PGPGOUT, count);
3123 else
3124 count_vm_events(PGPGIN, count);
3126 if (unlikely(block_dump)) {
3127 char b[BDEVNAME_SIZE];
3128 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n",
3129 current->comm, current->pid,
3130 (rw & WRITE) ? "WRITE" : "READ",
3131 (unsigned long long)bio->bi_sector,
3132 bdevname(bio->bi_bdev,b));
3135 generic_make_request(bio);
3138 EXPORT_SYMBOL(submit_bio);
3140 static void blk_recalc_rq_segments(struct request *rq)
3142 struct bio *bio, *prevbio = NULL;
3143 int nr_phys_segs, nr_hw_segs;
3144 unsigned int phys_size, hw_size;
3145 request_queue_t *q = rq->q;
3147 if (!rq->bio)
3148 return;
3150 phys_size = hw_size = nr_phys_segs = nr_hw_segs = 0;
3151 rq_for_each_bio(bio, rq) {
3152 /* Force bio hw/phys segs to be recalculated. */
3153 bio->bi_flags &= ~(1 << BIO_SEG_VALID);
3155 nr_phys_segs += bio_phys_segments(q, bio);
3156 nr_hw_segs += bio_hw_segments(q, bio);
3157 if (prevbio) {
3158 int pseg = phys_size + prevbio->bi_size + bio->bi_size;
3159 int hseg = hw_size + prevbio->bi_size + bio->bi_size;
3161 if (blk_phys_contig_segment(q, prevbio, bio) &&
3162 pseg <= q->max_segment_size) {
3163 nr_phys_segs--;
3164 phys_size += prevbio->bi_size + bio->bi_size;
3165 } else
3166 phys_size = 0;
3168 if (blk_hw_contig_segment(q, prevbio, bio) &&
3169 hseg <= q->max_segment_size) {
3170 nr_hw_segs--;
3171 hw_size += prevbio->bi_size + bio->bi_size;
3172 } else
3173 hw_size = 0;
3175 prevbio = bio;
3178 rq->nr_phys_segments = nr_phys_segs;
3179 rq->nr_hw_segments = nr_hw_segs;
3182 static void blk_recalc_rq_sectors(struct request *rq, int nsect)
3184 if (blk_fs_request(rq)) {
3185 rq->hard_sector += nsect;
3186 rq->hard_nr_sectors -= nsect;
3189 * Move the I/O submission pointers ahead if required.
3191 if ((rq->nr_sectors >= rq->hard_nr_sectors) &&
3192 (rq->sector <= rq->hard_sector)) {
3193 rq->sector = rq->hard_sector;
3194 rq->nr_sectors = rq->hard_nr_sectors;
3195 rq->hard_cur_sectors = bio_cur_sectors(rq->bio);
3196 rq->current_nr_sectors = rq->hard_cur_sectors;
3197 rq->buffer = bio_data(rq->bio);
3201 * if total number of sectors is less than the first segment
3202 * size, something has gone terribly wrong
3204 if (rq->nr_sectors < rq->current_nr_sectors) {
3205 printk("blk: request botched\n");
3206 rq->nr_sectors = rq->current_nr_sectors;
3211 static int __end_that_request_first(struct request *req, int uptodate,
3212 int nr_bytes)
3214 int total_bytes, bio_nbytes, error, next_idx = 0;
3215 struct bio *bio;
3217 blk_add_trace_rq(req->q, req, BLK_TA_COMPLETE);
3220 * extend uptodate bool to allow < 0 value to be direct io error
3222 error = 0;
3223 if (end_io_error(uptodate))
3224 error = !uptodate ? -EIO : uptodate;
3227 * for a REQ_BLOCK_PC request, we want to carry any eventual
3228 * sense key with us all the way through
3230 if (!blk_pc_request(req))
3231 req->errors = 0;
3233 if (!uptodate) {
3234 if (blk_fs_request(req) && !(req->cmd_flags & REQ_QUIET))
3235 printk("end_request: I/O error, dev %s, sector %llu\n",
3236 req->rq_disk ? req->rq_disk->disk_name : "?",
3237 (unsigned long long)req->sector);
3240 if (blk_fs_request(req) && req->rq_disk) {
3241 const int rw = rq_data_dir(req);
3243 disk_stat_add(req->rq_disk, sectors[rw], nr_bytes >> 9);
3246 total_bytes = bio_nbytes = 0;
3247 while ((bio = req->bio) != NULL) {
3248 int nbytes;
3250 if (nr_bytes >= bio->bi_size) {
3251 req->bio = bio->bi_next;
3252 nbytes = bio->bi_size;
3253 if (!ordered_bio_endio(req, bio, nbytes, error))
3254 bio_endio(bio, nbytes, error);
3255 next_idx = 0;
3256 bio_nbytes = 0;
3257 } else {
3258 int idx = bio->bi_idx + next_idx;
3260 if (unlikely(bio->bi_idx >= bio->bi_vcnt)) {
3261 blk_dump_rq_flags(req, "__end_that");
3262 printk("%s: bio idx %d >= vcnt %d\n",
3263 __FUNCTION__,
3264 bio->bi_idx, bio->bi_vcnt);
3265 break;
3268 nbytes = bio_iovec_idx(bio, idx)->bv_len;
3269 BIO_BUG_ON(nbytes > bio->bi_size);
3272 * not a complete bvec done
3274 if (unlikely(nbytes > nr_bytes)) {
3275 bio_nbytes += nr_bytes;
3276 total_bytes += nr_bytes;
3277 break;
3281 * advance to the next vector
3283 next_idx++;
3284 bio_nbytes += nbytes;
3287 total_bytes += nbytes;
3288 nr_bytes -= nbytes;
3290 if ((bio = req->bio)) {
3292 * end more in this run, or just return 'not-done'
3294 if (unlikely(nr_bytes <= 0))
3295 break;
3300 * completely done
3302 if (!req->bio)
3303 return 0;
3306 * if the request wasn't completed, update state
3308 if (bio_nbytes) {
3309 if (!ordered_bio_endio(req, bio, bio_nbytes, error))
3310 bio_endio(bio, bio_nbytes, error);
3311 bio->bi_idx += next_idx;
3312 bio_iovec(bio)->bv_offset += nr_bytes;
3313 bio_iovec(bio)->bv_len -= nr_bytes;
3316 blk_recalc_rq_sectors(req, total_bytes >> 9);
3317 blk_recalc_rq_segments(req);
3318 return 1;
3322 * end_that_request_first - end I/O on a request
3323 * @req: the request being processed
3324 * @uptodate: 1 for success, 0 for I/O error, < 0 for specific error
3325 * @nr_sectors: number of sectors to end I/O on
3327 * Description:
3328 * Ends I/O on a number of sectors attached to @req, and sets it up
3329 * for the next range of segments (if any) in the cluster.
3331 * Return:
3332 * 0 - we are done with this request, call end_that_request_last()
3333 * 1 - still buffers pending for this request
3335 int end_that_request_first(struct request *req, int uptodate, int nr_sectors)
3337 return __end_that_request_first(req, uptodate, nr_sectors << 9);
3340 EXPORT_SYMBOL(end_that_request_first);
3343 * end_that_request_chunk - end I/O on a request
3344 * @req: the request being processed
3345 * @uptodate: 1 for success, 0 for I/O error, < 0 for specific error
3346 * @nr_bytes: number of bytes to complete
3348 * Description:
3349 * Ends I/O on a number of bytes attached to @req, and sets it up
3350 * for the next range of segments (if any). Like end_that_request_first(),
3351 * but deals with bytes instead of sectors.
3353 * Return:
3354 * 0 - we are done with this request, call end_that_request_last()
3355 * 1 - still buffers pending for this request
3357 int end_that_request_chunk(struct request *req, int uptodate, int nr_bytes)
3359 return __end_that_request_first(req, uptodate, nr_bytes);
3362 EXPORT_SYMBOL(end_that_request_chunk);
3365 * splice the completion data to a local structure and hand off to
3366 * process_completion_queue() to complete the requests
3368 static void blk_done_softirq(struct softirq_action *h)
3370 struct list_head *cpu_list, local_list;
3372 local_irq_disable();
3373 cpu_list = &__get_cpu_var(blk_cpu_done);
3374 list_replace_init(cpu_list, &local_list);
3375 local_irq_enable();
3377 while (!list_empty(&local_list)) {
3378 struct request *rq = list_entry(local_list.next, struct request, donelist);
3380 list_del_init(&rq->donelist);
3381 rq->q->softirq_done_fn(rq);
3385 #ifdef CONFIG_HOTPLUG_CPU
3387 static int blk_cpu_notify(struct notifier_block *self, unsigned long action,
3388 void *hcpu)
3391 * If a CPU goes away, splice its entries to the current CPU
3392 * and trigger a run of the softirq
3394 if (action == CPU_DEAD) {
3395 int cpu = (unsigned long) hcpu;
3397 local_irq_disable();
3398 list_splice_init(&per_cpu(blk_cpu_done, cpu),
3399 &__get_cpu_var(blk_cpu_done));
3400 raise_softirq_irqoff(BLOCK_SOFTIRQ);
3401 local_irq_enable();
3404 return NOTIFY_OK;
3408 static struct notifier_block __devinitdata blk_cpu_notifier = {
3409 .notifier_call = blk_cpu_notify,
3412 #endif /* CONFIG_HOTPLUG_CPU */
3415 * blk_complete_request - end I/O on a request
3416 * @req: the request being processed
3418 * Description:
3419 * Ends all I/O on a request. It does not handle partial completions,
3420 * unless the driver actually implements this in its completion callback
3421 * through requeueing. Theh actual completion happens out-of-order,
3422 * through a softirq handler. The user must have registered a completion
3423 * callback through blk_queue_softirq_done().
3426 void blk_complete_request(struct request *req)
3428 struct list_head *cpu_list;
3429 unsigned long flags;
3431 BUG_ON(!req->q->softirq_done_fn);
3433 local_irq_save(flags);
3435 cpu_list = &__get_cpu_var(blk_cpu_done);
3436 list_add_tail(&req->donelist, cpu_list);
3437 raise_softirq_irqoff(BLOCK_SOFTIRQ);
3439 local_irq_restore(flags);
3442 EXPORT_SYMBOL(blk_complete_request);
3445 * queue lock must be held
3447 void end_that_request_last(struct request *req, int uptodate)
3449 struct gendisk *disk = req->rq_disk;
3450 int error;
3453 * extend uptodate bool to allow < 0 value to be direct io error
3455 error = 0;
3456 if (end_io_error(uptodate))
3457 error = !uptodate ? -EIO : uptodate;
3459 if (unlikely(laptop_mode) && blk_fs_request(req))
3460 laptop_io_completion();
3463 * Account IO completion. bar_rq isn't accounted as a normal
3464 * IO on queueing nor completion. Accounting the containing
3465 * request is enough.
3467 if (disk && blk_fs_request(req) && req != &req->q->bar_rq) {
3468 unsigned long duration = jiffies - req->start_time;
3469 const int rw = rq_data_dir(req);
3471 __disk_stat_inc(disk, ios[rw]);
3472 __disk_stat_add(disk, ticks[rw], duration);
3473 disk_round_stats(disk);
3474 disk->in_flight--;
3476 if (req->end_io)
3477 req->end_io(req, error);
3478 else
3479 __blk_put_request(req->q, req);
3482 EXPORT_SYMBOL(end_that_request_last);
3484 void end_request(struct request *req, int uptodate)
3486 if (!end_that_request_first(req, uptodate, req->hard_cur_sectors)) {
3487 add_disk_randomness(req->rq_disk);
3488 blkdev_dequeue_request(req);
3489 end_that_request_last(req, uptodate);
3493 EXPORT_SYMBOL(end_request);
3495 void blk_rq_bio_prep(request_queue_t *q, struct request *rq, struct bio *bio)
3497 /* first two bits are identical in rq->cmd_flags and bio->bi_rw */
3498 rq->cmd_flags |= (bio->bi_rw & 3);
3500 rq->nr_phys_segments = bio_phys_segments(q, bio);
3501 rq->nr_hw_segments = bio_hw_segments(q, bio);
3502 rq->current_nr_sectors = bio_cur_sectors(bio);
3503 rq->hard_cur_sectors = rq->current_nr_sectors;
3504 rq->hard_nr_sectors = rq->nr_sectors = bio_sectors(bio);
3505 rq->buffer = bio_data(bio);
3507 rq->bio = rq->biotail = bio;
3510 EXPORT_SYMBOL(blk_rq_bio_prep);
3512 int kblockd_schedule_work(struct work_struct *work)
3514 return queue_work(kblockd_workqueue, work);
3517 EXPORT_SYMBOL(kblockd_schedule_work);
3519 void kblockd_flush(void)
3521 flush_workqueue(kblockd_workqueue);
3523 EXPORT_SYMBOL(kblockd_flush);
3525 int __init blk_dev_init(void)
3527 int i;
3529 kblockd_workqueue = create_workqueue("kblockd");
3530 if (!kblockd_workqueue)
3531 panic("Failed to create kblockd\n");
3533 request_cachep = kmem_cache_create("blkdev_requests",
3534 sizeof(struct request), 0, SLAB_PANIC, NULL, NULL);
3536 requestq_cachep = kmem_cache_create("blkdev_queue",
3537 sizeof(request_queue_t), 0, SLAB_PANIC, NULL, NULL);
3539 iocontext_cachep = kmem_cache_create("blkdev_ioc",
3540 sizeof(struct io_context), 0, SLAB_PANIC, NULL, NULL);
3542 for_each_possible_cpu(i)
3543 INIT_LIST_HEAD(&per_cpu(blk_cpu_done, i));
3545 open_softirq(BLOCK_SOFTIRQ, blk_done_softirq, NULL);
3546 register_hotcpu_notifier(&blk_cpu_notifier);
3548 blk_max_low_pfn = max_low_pfn;
3549 blk_max_pfn = max_pfn;
3551 return 0;
3555 * IO Context helper functions
3557 void put_io_context(struct io_context *ioc)
3559 if (ioc == NULL)
3560 return;
3562 BUG_ON(atomic_read(&ioc->refcount) == 0);
3564 if (atomic_dec_and_test(&ioc->refcount)) {
3565 struct cfq_io_context *cic;
3567 rcu_read_lock();
3568 if (ioc->aic && ioc->aic->dtor)
3569 ioc->aic->dtor(ioc->aic);
3570 if (ioc->cic_root.rb_node != NULL) {
3571 struct rb_node *n = rb_first(&ioc->cic_root);
3573 cic = rb_entry(n, struct cfq_io_context, rb_node);
3574 cic->dtor(ioc);
3576 rcu_read_unlock();
3578 kmem_cache_free(iocontext_cachep, ioc);
3581 EXPORT_SYMBOL(put_io_context);
3583 /* Called by the exitting task */
3584 void exit_io_context(void)
3586 struct io_context *ioc;
3587 struct cfq_io_context *cic;
3589 task_lock(current);
3590 ioc = current->io_context;
3591 current->io_context = NULL;
3592 task_unlock(current);
3594 ioc->task = NULL;
3595 if (ioc->aic && ioc->aic->exit)
3596 ioc->aic->exit(ioc->aic);
3597 if (ioc->cic_root.rb_node != NULL) {
3598 cic = rb_entry(rb_first(&ioc->cic_root), struct cfq_io_context, rb_node);
3599 cic->exit(ioc);
3602 put_io_context(ioc);
3606 * If the current task has no IO context then create one and initialise it.
3607 * Otherwise, return its existing IO context.
3609 * This returned IO context doesn't have a specifically elevated refcount,
3610 * but since the current task itself holds a reference, the context can be
3611 * used in general code, so long as it stays within `current` context.
3613 static struct io_context *current_io_context(gfp_t gfp_flags, int node)
3615 struct task_struct *tsk = current;
3616 struct io_context *ret;
3618 ret = tsk->io_context;
3619 if (likely(ret))
3620 return ret;
3622 ret = kmem_cache_alloc_node(iocontext_cachep, gfp_flags, node);
3623 if (ret) {
3624 atomic_set(&ret->refcount, 1);
3625 ret->task = current;
3626 ret->ioprio_changed = 0;
3627 ret->last_waited = jiffies; /* doesn't matter... */
3628 ret->nr_batch_requests = 0; /* because this is 0 */
3629 ret->aic = NULL;
3630 ret->cic_root.rb_node = NULL;
3631 /* make sure set_task_ioprio() sees the settings above */
3632 smp_wmb();
3633 tsk->io_context = ret;
3636 return ret;
3638 EXPORT_SYMBOL(current_io_context);
3641 * If the current task has no IO context then create one and initialise it.
3642 * If it does have a context, take a ref on it.
3644 * This is always called in the context of the task which submitted the I/O.
3646 struct io_context *get_io_context(gfp_t gfp_flags, int node)
3648 struct io_context *ret;
3649 ret = current_io_context(gfp_flags, node);
3650 if (likely(ret))
3651 atomic_inc(&ret->refcount);
3652 return ret;
3654 EXPORT_SYMBOL(get_io_context);
3656 void copy_io_context(struct io_context **pdst, struct io_context **psrc)
3658 struct io_context *src = *psrc;
3659 struct io_context *dst = *pdst;
3661 if (src) {
3662 BUG_ON(atomic_read(&src->refcount) == 0);
3663 atomic_inc(&src->refcount);
3664 put_io_context(dst);
3665 *pdst = src;
3668 EXPORT_SYMBOL(copy_io_context);
3670 void swap_io_context(struct io_context **ioc1, struct io_context **ioc2)
3672 struct io_context *temp;
3673 temp = *ioc1;
3674 *ioc1 = *ioc2;
3675 *ioc2 = temp;
3677 EXPORT_SYMBOL(swap_io_context);
3680 * sysfs parts below
3682 struct queue_sysfs_entry {
3683 struct attribute attr;
3684 ssize_t (*show)(struct request_queue *, char *);
3685 ssize_t (*store)(struct request_queue *, const char *, size_t);
3688 static ssize_t
3689 queue_var_show(unsigned int var, char *page)
3691 return sprintf(page, "%d\n", var);
3694 static ssize_t
3695 queue_var_store(unsigned long *var, const char *page, size_t count)
3697 char *p = (char *) page;
3699 *var = simple_strtoul(p, &p, 10);
3700 return count;
3703 static ssize_t queue_requests_show(struct request_queue *q, char *page)
3705 return queue_var_show(q->nr_requests, (page));
3708 static ssize_t
3709 queue_requests_store(struct request_queue *q, const char *page, size_t count)
3711 struct request_list *rl = &q->rq;
3712 unsigned long nr;
3713 int ret = queue_var_store(&nr, page, count);
3714 if (nr < BLKDEV_MIN_RQ)
3715 nr = BLKDEV_MIN_RQ;
3717 spin_lock_irq(q->queue_lock);
3718 q->nr_requests = nr;
3719 blk_queue_congestion_threshold(q);
3721 if (rl->count[READ] >= queue_congestion_on_threshold(q))
3722 blk_set_queue_congested(q, READ);
3723 else if (rl->count[READ] < queue_congestion_off_threshold(q))
3724 blk_clear_queue_congested(q, READ);
3726 if (rl->count[WRITE] >= queue_congestion_on_threshold(q))
3727 blk_set_queue_congested(q, WRITE);
3728 else if (rl->count[WRITE] < queue_congestion_off_threshold(q))
3729 blk_clear_queue_congested(q, WRITE);
3731 if (rl->count[READ] >= q->nr_requests) {
3732 blk_set_queue_full(q, READ);
3733 } else if (rl->count[READ]+1 <= q->nr_requests) {
3734 blk_clear_queue_full(q, READ);
3735 wake_up(&rl->wait[READ]);
3738 if (rl->count[WRITE] >= q->nr_requests) {
3739 blk_set_queue_full(q, WRITE);
3740 } else if (rl->count[WRITE]+1 <= q->nr_requests) {
3741 blk_clear_queue_full(q, WRITE);
3742 wake_up(&rl->wait[WRITE]);
3744 spin_unlock_irq(q->queue_lock);
3745 return ret;
3748 static ssize_t queue_ra_show(struct request_queue *q, char *page)
3750 int ra_kb = q->backing_dev_info.ra_pages << (PAGE_CACHE_SHIFT - 10);
3752 return queue_var_show(ra_kb, (page));
3755 static ssize_t
3756 queue_ra_store(struct request_queue *q, const char *page, size_t count)
3758 unsigned long ra_kb;
3759 ssize_t ret = queue_var_store(&ra_kb, page, count);
3761 spin_lock_irq(q->queue_lock);
3762 q->backing_dev_info.ra_pages = ra_kb >> (PAGE_CACHE_SHIFT - 10);
3763 spin_unlock_irq(q->queue_lock);
3765 return ret;
3768 static ssize_t queue_max_sectors_show(struct request_queue *q, char *page)
3770 int max_sectors_kb = q->max_sectors >> 1;
3772 return queue_var_show(max_sectors_kb, (page));
3775 static ssize_t
3776 queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
3778 unsigned long max_sectors_kb,
3779 max_hw_sectors_kb = q->max_hw_sectors >> 1,
3780 page_kb = 1 << (PAGE_CACHE_SHIFT - 10);
3781 ssize_t ret = queue_var_store(&max_sectors_kb, page, count);
3782 int ra_kb;
3784 if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
3785 return -EINVAL;
3787 * Take the queue lock to update the readahead and max_sectors
3788 * values synchronously:
3790 spin_lock_irq(q->queue_lock);
3792 * Trim readahead window as well, if necessary:
3794 ra_kb = q->backing_dev_info.ra_pages << (PAGE_CACHE_SHIFT - 10);
3795 if (ra_kb > max_sectors_kb)
3796 q->backing_dev_info.ra_pages =
3797 max_sectors_kb >> (PAGE_CACHE_SHIFT - 10);
3799 q->max_sectors = max_sectors_kb << 1;
3800 spin_unlock_irq(q->queue_lock);
3802 return ret;
3805 static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
3807 int max_hw_sectors_kb = q->max_hw_sectors >> 1;
3809 return queue_var_show(max_hw_sectors_kb, (page));
3813 static struct queue_sysfs_entry queue_requests_entry = {
3814 .attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR },
3815 .show = queue_requests_show,
3816 .store = queue_requests_store,
3819 static struct queue_sysfs_entry queue_ra_entry = {
3820 .attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR },
3821 .show = queue_ra_show,
3822 .store = queue_ra_store,
3825 static struct queue_sysfs_entry queue_max_sectors_entry = {
3826 .attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR },
3827 .show = queue_max_sectors_show,
3828 .store = queue_max_sectors_store,
3831 static struct queue_sysfs_entry queue_max_hw_sectors_entry = {
3832 .attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO },
3833 .show = queue_max_hw_sectors_show,
3836 static struct queue_sysfs_entry queue_iosched_entry = {
3837 .attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR },
3838 .show = elv_iosched_show,
3839 .store = elv_iosched_store,
3842 static struct attribute *default_attrs[] = {
3843 &queue_requests_entry.attr,
3844 &queue_ra_entry.attr,
3845 &queue_max_hw_sectors_entry.attr,
3846 &queue_max_sectors_entry.attr,
3847 &queue_iosched_entry.attr,
3848 NULL,
3851 #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
3853 static ssize_t
3854 queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3856 struct queue_sysfs_entry *entry = to_queue(attr);
3857 request_queue_t *q = container_of(kobj, struct request_queue, kobj);
3858 ssize_t res;
3860 if (!entry->show)
3861 return -EIO;
3862 mutex_lock(&q->sysfs_lock);
3863 if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) {
3864 mutex_unlock(&q->sysfs_lock);
3865 return -ENOENT;
3867 res = entry->show(q, page);
3868 mutex_unlock(&q->sysfs_lock);
3869 return res;
3872 static ssize_t
3873 queue_attr_store(struct kobject *kobj, struct attribute *attr,
3874 const char *page, size_t length)
3876 struct queue_sysfs_entry *entry = to_queue(attr);
3877 request_queue_t *q = container_of(kobj, struct request_queue, kobj);
3879 ssize_t res;
3881 if (!entry->store)
3882 return -EIO;
3883 mutex_lock(&q->sysfs_lock);
3884 if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) {
3885 mutex_unlock(&q->sysfs_lock);
3886 return -ENOENT;
3888 res = entry->store(q, page, length);
3889 mutex_unlock(&q->sysfs_lock);
3890 return res;
3893 static struct sysfs_ops queue_sysfs_ops = {
3894 .show = queue_attr_show,
3895 .store = queue_attr_store,
3898 static struct kobj_type queue_ktype = {
3899 .sysfs_ops = &queue_sysfs_ops,
3900 .default_attrs = default_attrs,
3901 .release = blk_release_queue,
3904 int blk_register_queue(struct gendisk *disk)
3906 int ret;
3908 request_queue_t *q = disk->queue;
3910 if (!q || !q->request_fn)
3911 return -ENXIO;
3913 q->kobj.parent = kobject_get(&disk->kobj);
3915 ret = kobject_add(&q->kobj);
3916 if (ret < 0)
3917 return ret;
3919 kobject_uevent(&q->kobj, KOBJ_ADD);
3921 ret = elv_register_queue(q);
3922 if (ret) {
3923 kobject_uevent(&q->kobj, KOBJ_REMOVE);
3924 kobject_del(&q->kobj);
3925 return ret;
3928 return 0;
3931 void blk_unregister_queue(struct gendisk *disk)
3933 request_queue_t *q = disk->queue;
3935 if (q && q->request_fn) {
3936 elv_unregister_queue(q);
3938 kobject_uevent(&q->kobj, KOBJ_REMOVE);
3939 kobject_del(&q->kobj);
3940 kobject_put(&disk->kobj);