aboutsummaryrefslogtreecommitdiffstats
path: root/block/blk-settings.c
diff options
context:
space:
mode:
Diffstat (limited to 'block/blk-settings.c')
-rw-r--r--block/blk-settings.c261
1 files changed, 154 insertions, 107 deletions
diff --git a/block/blk-settings.c b/block/blk-settings.c
index 66d4aa8799b7..f5ed5a1187ba 100644
--- a/block/blk-settings.c
+++ b/block/blk-settings.c
@@ -8,6 +8,9 @@
8#include <linux/blkdev.h> 8#include <linux/blkdev.h>
9#include <linux/bootmem.h> /* for max_pfn/max_low_pfn */ 9#include <linux/bootmem.h> /* for max_pfn/max_low_pfn */
10#include <linux/gcd.h> 10#include <linux/gcd.h>
11#include <linux/lcm.h>
12#include <linux/jiffies.h>
13#include <linux/gfp.h>
11 14
12#include "blk.h" 15#include "blk.h"
13 16
@@ -90,13 +93,16 @@ EXPORT_SYMBOL_GPL(blk_queue_lld_busy);
90 */ 93 */
91void blk_set_default_limits(struct queue_limits *lim) 94void blk_set_default_limits(struct queue_limits *lim)
92{ 95{
93 lim->max_phys_segments = MAX_PHYS_SEGMENTS; 96 lim->max_segments = BLK_MAX_SEGMENTS;
94 lim->max_hw_segments = MAX_HW_SEGMENTS;
95 lim->seg_boundary_mask = BLK_SEG_BOUNDARY_MASK; 97 lim->seg_boundary_mask = BLK_SEG_BOUNDARY_MASK;
96 lim->max_segment_size = MAX_SEGMENT_SIZE; 98 lim->max_segment_size = BLK_MAX_SEGMENT_SIZE;
97 lim->max_sectors = BLK_DEF_MAX_SECTORS; 99 lim->max_sectors = BLK_DEF_MAX_SECTORS;
98 lim->max_hw_sectors = INT_MAX; 100 lim->max_hw_sectors = INT_MAX;
99 lim->max_discard_sectors = SAFE_MAX_SECTORS; 101 lim->max_discard_sectors = 0;
102 lim->discard_granularity = 0;
103 lim->discard_alignment = 0;
104 lim->discard_misaligned = 0;
105 lim->discard_zeroes_data = -1;
100 lim->logical_block_size = lim->physical_block_size = lim->io_min = 512; 106 lim->logical_block_size = lim->physical_block_size = lim->io_min = 512;
101 lim->bounce_pfn = (unsigned long)(BLK_BOUNCE_ANY >> PAGE_SHIFT); 107 lim->bounce_pfn = (unsigned long)(BLK_BOUNCE_ANY >> PAGE_SHIFT);
102 lim->alignment_offset = 0; 108 lim->alignment_offset = 0;
@@ -141,7 +147,7 @@ void blk_queue_make_request(struct request_queue *q, make_request_fn *mfn)
141 q->nr_batching = BLK_BATCH_REQ; 147 q->nr_batching = BLK_BATCH_REQ;
142 148
143 q->unplug_thresh = 4; /* hmm */ 149 q->unplug_thresh = 4; /* hmm */
144 q->unplug_delay = (3 * HZ) / 1000; /* 3 milliseconds */ 150 q->unplug_delay = msecs_to_jiffies(3); /* 3 milliseconds */
145 if (q->unplug_delay == 0) 151 if (q->unplug_delay == 0)
146 q->unplug_delay = 1; 152 q->unplug_delay = 1;
147 153
@@ -149,7 +155,7 @@ void blk_queue_make_request(struct request_queue *q, make_request_fn *mfn)
149 q->unplug_timer.data = (unsigned long)q; 155 q->unplug_timer.data = (unsigned long)q;
150 156
151 blk_set_default_limits(&q->limits); 157 blk_set_default_limits(&q->limits);
152 blk_queue_max_sectors(q, SAFE_MAX_SECTORS); 158 blk_queue_max_hw_sectors(q, BLK_SAFE_MAX_SECTORS);
153 159
154 /* 160 /*
155 * If the caller didn't supply a lock, fall back to our embedded 161 * If the caller didn't supply a lock, fall back to our embedded
@@ -205,37 +211,32 @@ void blk_queue_bounce_limit(struct request_queue *q, u64 dma_mask)
205EXPORT_SYMBOL(blk_queue_bounce_limit); 211EXPORT_SYMBOL(blk_queue_bounce_limit);
206 212
207/** 213/**
208 * blk_queue_max_sectors - set max sectors for a request for this queue 214 * blk_queue_max_hw_sectors - set max sectors for a request for this queue
209 * @q: the request queue for the device 215 * @q: the request queue for the device
210 * @max_sectors: max sectors in the usual 512b unit 216 * @max_hw_sectors: max hardware sectors in the usual 512b unit
211 * 217 *
212 * Description: 218 * Description:
213 * Enables a low level driver to set an upper limit on the size of 219 * Enables a low level driver to set a hard upper limit,
214 * received requests. 220 * max_hw_sectors, on the size of requests. max_hw_sectors is set by
221 * the device driver based upon the combined capabilities of I/O
222 * controller and storage device.
223 *
224 * max_sectors is a soft limit imposed by the block layer for
225 * filesystem type requests. This value can be overridden on a
226 * per-device basis in /sys/block/<device>/queue/max_sectors_kb.
227 * The soft limit can not exceed max_hw_sectors.
215 **/ 228 **/
216void blk_queue_max_sectors(struct request_queue *q, unsigned int max_sectors) 229void blk_queue_max_hw_sectors(struct request_queue *q, unsigned int max_hw_sectors)
217{ 230{
218 if ((max_sectors << 9) < PAGE_CACHE_SIZE) { 231 if ((max_hw_sectors << 9) < PAGE_CACHE_SIZE) {
219 max_sectors = 1 << (PAGE_CACHE_SHIFT - 9); 232 max_hw_sectors = 1 << (PAGE_CACHE_SHIFT - 9);
220 printk(KERN_INFO "%s: set to minimum %d\n", 233 printk(KERN_INFO "%s: set to minimum %d\n",
221 __func__, max_sectors); 234 __func__, max_hw_sectors);
222 } 235 }
223 236
224 if (BLK_DEF_MAX_SECTORS > max_sectors) 237 q->limits.max_hw_sectors = max_hw_sectors;
225 q->limits.max_hw_sectors = q->limits.max_sectors = max_sectors; 238 q->limits.max_sectors = min_t(unsigned int, max_hw_sectors,
226 else { 239 BLK_DEF_MAX_SECTORS);
227 q->limits.max_sectors = BLK_DEF_MAX_SECTORS;
228 q->limits.max_hw_sectors = max_sectors;
229 }
230}
231EXPORT_SYMBOL(blk_queue_max_sectors);
232
233void blk_queue_max_hw_sectors(struct request_queue *q, unsigned int max_sectors)
234{
235 if (BLK_DEF_MAX_SECTORS > max_sectors)
236 q->limits.max_hw_sectors = BLK_DEF_MAX_SECTORS;
237 else
238 q->limits.max_hw_sectors = max_sectors;
239} 240}
240EXPORT_SYMBOL(blk_queue_max_hw_sectors); 241EXPORT_SYMBOL(blk_queue_max_hw_sectors);
241 242
@@ -252,41 +253,15 @@ void blk_queue_max_discard_sectors(struct request_queue *q,
252EXPORT_SYMBOL(blk_queue_max_discard_sectors); 253EXPORT_SYMBOL(blk_queue_max_discard_sectors);
253 254
254/** 255/**
255 * blk_queue_max_phys_segments - set max phys segments for a request for this queue 256 * blk_queue_max_segments - set max hw segments for a request for this queue
256 * @q: the request queue for the device
257 * @max_segments: max number of segments
258 *
259 * Description:
260 * Enables a low level driver to set an upper limit on the number of
261 * physical data segments in a request. This would be the largest sized
262 * scatter list the driver could handle.
263 **/
264void blk_queue_max_phys_segments(struct request_queue *q,
265 unsigned short max_segments)
266{
267 if (!max_segments) {
268 max_segments = 1;
269 printk(KERN_INFO "%s: set to minimum %d\n",
270 __func__, max_segments);
271 }
272
273 q->limits.max_phys_segments = max_segments;
274}
275EXPORT_SYMBOL(blk_queue_max_phys_segments);
276
277/**
278 * blk_queue_max_hw_segments - set max hw segments for a request for this queue
279 * @q: the request queue for the device 257 * @q: the request queue for the device
280 * @max_segments: max number of segments 258 * @max_segments: max number of segments
281 * 259 *
282 * Description: 260 * Description:
283 * Enables a low level driver to set an upper limit on the number of 261 * Enables a low level driver to set an upper limit on the number of
284 * hw data segments in a request. This would be the largest number of 262 * hw data segments in a request.
285 * address/length pairs the host adapter can actually give at once
286 * to the device.
287 **/ 263 **/
288void blk_queue_max_hw_segments(struct request_queue *q, 264void blk_queue_max_segments(struct request_queue *q, unsigned short max_segments)
289 unsigned short max_segments)
290{ 265{
291 if (!max_segments) { 266 if (!max_segments) {
292 max_segments = 1; 267 max_segments = 1;
@@ -294,9 +269,9 @@ void blk_queue_max_hw_segments(struct request_queue *q,
294 __func__, max_segments); 269 __func__, max_segments);
295 } 270 }
296 271
297 q->limits.max_hw_segments = max_segments; 272 q->limits.max_segments = max_segments;
298} 273}
299EXPORT_SYMBOL(blk_queue_max_hw_segments); 274EXPORT_SYMBOL(blk_queue_max_segments);
300 275
301/** 276/**
302 * blk_queue_max_segment_size - set max segment size for blk_rq_map_sg 277 * blk_queue_max_segment_size - set max segment size for blk_rq_map_sg
@@ -490,18 +465,30 @@ EXPORT_SYMBOL(blk_queue_stack_limits);
490 465
491/** 466/**
492 * blk_stack_limits - adjust queue_limits for stacked devices 467 * blk_stack_limits - adjust queue_limits for stacked devices
493 * @t: the stacking driver limits (top) 468 * @t: the stacking driver limits (top device)
494 * @b: the underlying queue limits (bottom) 469 * @b: the underlying queue limits (bottom, component device)
495 * @offset: offset to beginning of data within component device 470 * @start: first data sector within component device
496 * 471 *
497 * Description: 472 * Description:
498 * Merges two queue_limit structs. Returns 0 if alignment didn't 473 * This function is used by stacking drivers like MD and DM to ensure
499 * change. Returns -1 if adding the bottom device caused 474 * that all component devices have compatible block sizes and
500 * misalignment. 475 * alignments. The stacking driver must provide a queue_limits
476 * struct (top) and then iteratively call the stacking function for
477 * all component (bottom) devices. The stacking function will
478 * attempt to combine the values and ensure proper alignment.
479 *
480 * Returns 0 if the top and bottom queue_limits are compatible. The
481 * top device's block sizes and alignment offsets may be adjusted to
482 * ensure alignment with the bottom device. If no compatible sizes
483 * and alignments exist, -1 is returned and the resulting top
484 * queue_limits will have the misaligned flag set to indicate that
485 * the alignment_offset is undefined.
501 */ 486 */
502int blk_stack_limits(struct queue_limits *t, struct queue_limits *b, 487int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
503 sector_t offset) 488 sector_t start)
504{ 489{
490 unsigned int top, bottom, alignment, ret = 0;
491
505 t->max_sectors = min_not_zero(t->max_sectors, b->max_sectors); 492 t->max_sectors = min_not_zero(t->max_sectors, b->max_sectors);
506 t->max_hw_sectors = min_not_zero(t->max_hw_sectors, b->max_hw_sectors); 493 t->max_hw_sectors = min_not_zero(t->max_hw_sectors, b->max_hw_sectors);
507 t->bounce_pfn = min_not_zero(t->bounce_pfn, b->bounce_pfn); 494 t->bounce_pfn = min_not_zero(t->bounce_pfn, b->bounce_pfn);
@@ -509,15 +496,31 @@ int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
509 t->seg_boundary_mask = min_not_zero(t->seg_boundary_mask, 496 t->seg_boundary_mask = min_not_zero(t->seg_boundary_mask,
510 b->seg_boundary_mask); 497 b->seg_boundary_mask);
511 498
512 t->max_phys_segments = min_not_zero(t->max_phys_segments, 499 t->max_segments = min_not_zero(t->max_segments, b->max_segments);
513 b->max_phys_segments);
514
515 t->max_hw_segments = min_not_zero(t->max_hw_segments,
516 b->max_hw_segments);
517 500
518 t->max_segment_size = min_not_zero(t->max_segment_size, 501 t->max_segment_size = min_not_zero(t->max_segment_size,
519 b->max_segment_size); 502 b->max_segment_size);
520 503
504 t->misaligned |= b->misaligned;
505
506 alignment = queue_limit_alignment_offset(b, start);
507
508 /* Bottom device has different alignment. Check that it is
509 * compatible with the current top alignment.
510 */
511 if (t->alignment_offset != alignment) {
512
513 top = max(t->physical_block_size, t->io_min)
514 + t->alignment_offset;
515 bottom = max(b->physical_block_size, b->io_min) + alignment;
516
517 /* Verify that top and bottom intervals line up */
518 if (max(top, bottom) & (min(top, bottom) - 1)) {
519 t->misaligned = 1;
520 ret = -1;
521 }
522 }
523
521 t->logical_block_size = max(t->logical_block_size, 524 t->logical_block_size = max(t->logical_block_size,
522 b->logical_block_size); 525 b->logical_block_size);
523 526
@@ -525,50 +528,99 @@ int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
525 b->physical_block_size); 528 b->physical_block_size);
526 529
527 t->io_min = max(t->io_min, b->io_min); 530 t->io_min = max(t->io_min, b->io_min);
531 t->io_opt = lcm(t->io_opt, b->io_opt);
532
528 t->no_cluster |= b->no_cluster; 533 t->no_cluster |= b->no_cluster;
534 t->discard_zeroes_data &= b->discard_zeroes_data;
529 535
530 /* Bottom device offset aligned? */ 536 /* Physical block size a multiple of the logical block size? */
531 if (offset && 537 if (t->physical_block_size & (t->logical_block_size - 1)) {
532 (offset & (b->physical_block_size - 1)) != b->alignment_offset) { 538 t->physical_block_size = t->logical_block_size;
533 t->misaligned = 1; 539 t->misaligned = 1;
534 return -1; 540 ret = -1;
535 } 541 }
536 542
537 /* If top has no alignment offset, inherit from bottom */ 543 /* Minimum I/O a multiple of the physical block size? */
538 if (!t->alignment_offset) 544 if (t->io_min & (t->physical_block_size - 1)) {
539 t->alignment_offset = 545 t->io_min = t->physical_block_size;
540 b->alignment_offset & (b->physical_block_size - 1); 546 t->misaligned = 1;
547 ret = -1;
548 }
541 549
542 /* Top device aligned on logical block boundary? */ 550 /* Optimal I/O a multiple of the physical block size? */
543 if (t->alignment_offset & (t->logical_block_size - 1)) { 551 if (t->io_opt & (t->physical_block_size - 1)) {
552 t->io_opt = 0;
544 t->misaligned = 1; 553 t->misaligned = 1;
545 return -1; 554 ret = -1;
546 } 555 }
547 556
548 /* Find lcm() of optimal I/O size */ 557 /* Find lowest common alignment_offset */
549 if (t->io_opt && b->io_opt) 558 t->alignment_offset = lcm(t->alignment_offset, alignment)
550 t->io_opt = (t->io_opt * b->io_opt) / gcd(t->io_opt, b->io_opt); 559 & (max(t->physical_block_size, t->io_min) - 1);
551 else if (b->io_opt) 560
552 t->io_opt = b->io_opt; 561 /* Verify that new alignment_offset is on a logical block boundary */
562 if (t->alignment_offset & (t->logical_block_size - 1)) {
563 t->misaligned = 1;
564 ret = -1;
565 }
553 566
554 /* Verify that optimal I/O size is a multiple of io_min */ 567 /* Discard alignment and granularity */
555 if (t->io_min && t->io_opt % t->io_min) 568 if (b->discard_granularity) {
556 return -1; 569 alignment = queue_limit_discard_alignment(b, start);
570
571 if (t->discard_granularity != 0 &&
572 t->discard_alignment != alignment) {
573 top = t->discard_granularity + t->discard_alignment;
574 bottom = b->discard_granularity + alignment;
575
576 /* Verify that top and bottom intervals line up */
577 if (max(top, bottom) & (min(top, bottom) - 1))
578 t->discard_misaligned = 1;
579 }
580
581 t->max_discard_sectors = min_not_zero(t->max_discard_sectors,
582 b->max_discard_sectors);
583 t->discard_granularity = max(t->discard_granularity,
584 b->discard_granularity);
585 t->discard_alignment = lcm(t->discard_alignment, alignment) &
586 (t->discard_granularity - 1);
587 }
557 588
558 return 0; 589 return ret;
559} 590}
560EXPORT_SYMBOL(blk_stack_limits); 591EXPORT_SYMBOL(blk_stack_limits);
561 592
562/** 593/**
594 * bdev_stack_limits - adjust queue limits for stacked drivers
595 * @t: the stacking driver limits (top device)
596 * @bdev: the component block_device (bottom)
597 * @start: first data sector within component device
598 *
599 * Description:
600 * Merges queue limits for a top device and a block_device. Returns
601 * 0 if alignment didn't change. Returns -1 if adding the bottom
602 * device caused misalignment.
603 */
604int bdev_stack_limits(struct queue_limits *t, struct block_device *bdev,
605 sector_t start)
606{
607 struct request_queue *bq = bdev_get_queue(bdev);
608
609 start += get_start_sect(bdev);
610
611 return blk_stack_limits(t, &bq->limits, start);
612}
613EXPORT_SYMBOL(bdev_stack_limits);
614
615/**
563 * disk_stack_limits - adjust queue limits for stacked drivers 616 * disk_stack_limits - adjust queue limits for stacked drivers
564 * @disk: MD/DM gendisk (top) 617 * @disk: MD/DM gendisk (top)
565 * @bdev: the underlying block device (bottom) 618 * @bdev: the underlying block device (bottom)
566 * @offset: offset to beginning of data within component device 619 * @offset: offset to beginning of data within component device
567 * 620 *
568 * Description: 621 * Description:
569 * Merges the limits for two queues. Returns 0 if alignment 622 * Merges the limits for a top level gendisk and a bottom level
570 * didn't change. Returns -1 if adding the bottom device caused 623 * block_device.
571 * misalignment.
572 */ 624 */
573void disk_stack_limits(struct gendisk *disk, struct block_device *bdev, 625void disk_stack_limits(struct gendisk *disk, struct block_device *bdev,
574 sector_t offset) 626 sector_t offset)
@@ -576,9 +628,7 @@ void disk_stack_limits(struct gendisk *disk, struct block_device *bdev,
576 struct request_queue *t = disk->queue; 628 struct request_queue *t = disk->queue;
577 struct request_queue *b = bdev_get_queue(bdev); 629 struct request_queue *b = bdev_get_queue(bdev);
578 630
579 offset += get_start_sect(bdev) << 9; 631 if (bdev_stack_limits(&t->limits, bdev, offset >> 9) < 0) {
580
581 if (blk_stack_limits(&t->limits, &b->limits, offset) < 0) {
582 char top[BDEVNAME_SIZE], bottom[BDEVNAME_SIZE]; 632 char top[BDEVNAME_SIZE], bottom[BDEVNAME_SIZE];
583 633
584 disk_name(disk, 0, top); 634 disk_name(disk, 0, top);
@@ -650,22 +700,19 @@ EXPORT_SYMBOL(blk_queue_update_dma_pad);
650 * does is adjust the queue so that the buf is always appended 700 * does is adjust the queue so that the buf is always appended
651 * silently to the scatterlist. 701 * silently to the scatterlist.
652 * 702 *
653 * Note: This routine adjusts max_hw_segments to make room for 703 * Note: This routine adjusts max_hw_segments to make room for appending
654 * appending the drain buffer. If you call 704 * the drain buffer. If you call blk_queue_max_segments() after calling
655 * blk_queue_max_hw_segments() or blk_queue_max_phys_segments() after 705 * this routine, you must set the limit to one fewer than your device
656 * calling this routine, you must set the limit to one fewer than your 706 * can support otherwise there won't be room for the drain buffer.
657 * device can support otherwise there won't be room for the drain
658 * buffer.
659 */ 707 */
660int blk_queue_dma_drain(struct request_queue *q, 708int blk_queue_dma_drain(struct request_queue *q,
661 dma_drain_needed_fn *dma_drain_needed, 709 dma_drain_needed_fn *dma_drain_needed,
662 void *buf, unsigned int size) 710 void *buf, unsigned int size)
663{ 711{
664 if (queue_max_hw_segments(q) < 2 || queue_max_phys_segments(q) < 2) 712 if (queue_max_segments(q) < 2)
665 return -EINVAL; 713 return -EINVAL;
666 /* make room for appending the drain */ 714 /* make room for appending the drain */
667 blk_queue_max_hw_segments(q, queue_max_hw_segments(q) - 1); 715 blk_queue_max_segments(q, queue_max_segments(q) - 1);
668 blk_queue_max_phys_segments(q, queue_max_phys_segments(q) - 1);
669 q->dma_drain_needed = dma_drain_needed; 716 q->dma_drain_needed = dma_drain_needed;
670 q->dma_drain_buffer = buf; 717 q->dma_drain_buffer = buf;
671 q->dma_drain_size = size; 718 q->dma_drain_size = size;