1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Functions related to setting various queue properties from drivers 4 */ 5 #include <linux/kernel.h> 6 #include <linux/module.h> 7 #include <linux/init.h> 8 #include <linux/bio.h> 9 #include <linux/blk-integrity.h> 10 #include <linux/pagemap.h> 11 #include <linux/backing-dev-defs.h> 12 #include <linux/gcd.h> 13 #include <linux/lcm.h> 14 #include <linux/jiffies.h> 15 #include <linux/gfp.h> 16 #include <linux/dma-mapping.h> 17 18 #include "blk.h" 19 #include "blk-rq-qos.h" 20 #include "blk-wbt.h" 21 22 void blk_queue_rq_timeout(struct request_queue *q, unsigned int timeout) 23 { 24 q->rq_timeout = timeout; 25 } 26 EXPORT_SYMBOL_GPL(blk_queue_rq_timeout); 27 28 /** 29 * blk_set_stacking_limits - set default limits for stacking devices 30 * @lim: the queue_limits structure to reset 31 * 32 * Prepare queue limits for applying limits from underlying devices using 33 * blk_stack_limits(). 34 */ 35 void blk_set_stacking_limits(struct queue_limits *lim) 36 { 37 memset(lim, 0, sizeof(*lim)); 38 lim->logical_block_size = SECTOR_SIZE; 39 lim->physical_block_size = SECTOR_SIZE; 40 lim->io_min = SECTOR_SIZE; 41 lim->discard_granularity = SECTOR_SIZE; 42 lim->dma_alignment = SECTOR_SIZE - 1; 43 lim->seg_boundary_mask = BLK_SEG_BOUNDARY_MASK; 44 45 /* Inherit limits from component devices */ 46 lim->max_segments = USHRT_MAX; 47 lim->max_discard_segments = USHRT_MAX; 48 lim->max_hw_sectors = UINT_MAX; 49 lim->max_segment_size = UINT_MAX; 50 lim->max_sectors = UINT_MAX; 51 lim->max_dev_sectors = UINT_MAX; 52 lim->max_write_zeroes_sectors = UINT_MAX; 53 lim->max_zone_append_sectors = UINT_MAX; 54 lim->max_user_discard_sectors = UINT_MAX; 55 } 56 EXPORT_SYMBOL(blk_set_stacking_limits); 57 58 void blk_apply_bdi_limits(struct backing_dev_info *bdi, 59 struct queue_limits *lim) 60 { 61 /* 62 * For read-ahead of large files to be effective, we need to read ahead 63 * at least twice the optimal I/O size. 64 */ 65 bdi->ra_pages = max(lim->io_opt * 2 / PAGE_SIZE, VM_READAHEAD_PAGES); 66 bdi->io_pages = lim->max_sectors >> PAGE_SECTORS_SHIFT; 67 } 68 69 static int blk_validate_zoned_limits(struct queue_limits *lim) 70 { 71 if (!(lim->features & BLK_FEAT_ZONED)) { 72 if (WARN_ON_ONCE(lim->max_open_zones) || 73 WARN_ON_ONCE(lim->max_active_zones) || 74 WARN_ON_ONCE(lim->zone_write_granularity) || 75 WARN_ON_ONCE(lim->max_zone_append_sectors)) 76 return -EINVAL; 77 return 0; 78 } 79 80 if (WARN_ON_ONCE(!IS_ENABLED(CONFIG_BLK_DEV_ZONED))) 81 return -EINVAL; 82 83 /* 84 * Given that active zones include open zones, the maximum number of 85 * open zones cannot be larger than the maximum number of active zones. 86 */ 87 if (lim->max_active_zones && 88 lim->max_open_zones > lim->max_active_zones) 89 return -EINVAL; 90 91 if (lim->zone_write_granularity < lim->logical_block_size) 92 lim->zone_write_granularity = lim->logical_block_size; 93 94 if (lim->max_zone_append_sectors) { 95 /* 96 * The Zone Append size is limited by the maximum I/O size 97 * and the zone size given that it can't span zones. 98 */ 99 lim->max_zone_append_sectors = 100 min3(lim->max_hw_sectors, 101 lim->max_zone_append_sectors, 102 lim->chunk_sectors); 103 } 104 105 return 0; 106 } 107 108 static int blk_validate_integrity_limits(struct queue_limits *lim) 109 { 110 struct blk_integrity *bi = &lim->integrity; 111 112 if (!bi->tuple_size) { 113 if (bi->csum_type != BLK_INTEGRITY_CSUM_NONE || 114 bi->tag_size || ((bi->flags & BLK_INTEGRITY_REF_TAG))) { 115 pr_warn("invalid PI settings.\n"); 116 return -EINVAL; 117 } 118 return 0; 119 } 120 121 if (!IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY)) { 122 pr_warn("integrity support disabled.\n"); 123 return -EINVAL; 124 } 125 126 if (bi->csum_type == BLK_INTEGRITY_CSUM_NONE && 127 (bi->flags & BLK_INTEGRITY_REF_TAG)) { 128 pr_warn("ref tag not support without checksum.\n"); 129 return -EINVAL; 130 } 131 132 if (!bi->interval_exp) 133 bi->interval_exp = ilog2(lim->logical_block_size); 134 135 return 0; 136 } 137 138 /* 139 * Returns max guaranteed bytes which we can fit in a bio. 140 * 141 * We request that an atomic_write is ITER_UBUF iov_iter (so a single vector), 142 * so we assume that we can fit in at least PAGE_SIZE in a segment, apart from 143 * the first and last segments. 144 */ 145 static unsigned int blk_queue_max_guaranteed_bio(struct queue_limits *lim) 146 { 147 unsigned int max_segments = min(BIO_MAX_VECS, lim->max_segments); 148 unsigned int length; 149 150 length = min(max_segments, 2) * lim->logical_block_size; 151 if (max_segments > 2) 152 length += (max_segments - 2) * PAGE_SIZE; 153 154 return length; 155 } 156 157 static void blk_atomic_writes_update_limits(struct queue_limits *lim) 158 { 159 unsigned int unit_limit = min(lim->max_hw_sectors << SECTOR_SHIFT, 160 blk_queue_max_guaranteed_bio(lim)); 161 162 unit_limit = rounddown_pow_of_two(unit_limit); 163 164 lim->atomic_write_max_sectors = 165 min(lim->atomic_write_hw_max >> SECTOR_SHIFT, 166 lim->max_hw_sectors); 167 lim->atomic_write_unit_min = 168 min(lim->atomic_write_hw_unit_min, unit_limit); 169 lim->atomic_write_unit_max = 170 min(lim->atomic_write_hw_unit_max, unit_limit); 171 lim->atomic_write_boundary_sectors = 172 lim->atomic_write_hw_boundary >> SECTOR_SHIFT; 173 } 174 175 static void blk_validate_atomic_write_limits(struct queue_limits *lim) 176 { 177 unsigned int boundary_sectors; 178 179 if (!lim->atomic_write_hw_max) 180 goto unsupported; 181 182 boundary_sectors = lim->atomic_write_hw_boundary >> SECTOR_SHIFT; 183 184 if (boundary_sectors) { 185 /* 186 * A feature of boundary support is that it disallows bios to 187 * be merged which would result in a merged request which 188 * crosses either a chunk sector or atomic write HW boundary, 189 * even though chunk sectors may be just set for performance. 190 * For simplicity, disallow atomic writes for a chunk sector 191 * which is non-zero and smaller than atomic write HW boundary. 192 * Furthermore, chunk sectors must be a multiple of atomic 193 * write HW boundary. Otherwise boundary support becomes 194 * complicated. 195 * Devices which do not conform to these rules can be dealt 196 * with if and when they show up. 197 */ 198 if (WARN_ON_ONCE(lim->chunk_sectors % boundary_sectors)) 199 goto unsupported; 200 201 /* 202 * The boundary size just needs to be a multiple of unit_max 203 * (and not necessarily a power-of-2), so this following check 204 * could be relaxed in future. 205 * Furthermore, if needed, unit_max could even be reduced so 206 * that it is compliant with a !power-of-2 boundary. 207 */ 208 if (!is_power_of_2(boundary_sectors)) 209 goto unsupported; 210 } 211 212 blk_atomic_writes_update_limits(lim); 213 return; 214 215 unsupported: 216 lim->atomic_write_max_sectors = 0; 217 lim->atomic_write_boundary_sectors = 0; 218 lim->atomic_write_unit_min = 0; 219 lim->atomic_write_unit_max = 0; 220 } 221 222 /* 223 * Check that the limits in lim are valid, initialize defaults for unset 224 * values, and cap values based on others where needed. 225 */ 226 static int blk_validate_limits(struct queue_limits *lim) 227 { 228 unsigned int max_hw_sectors; 229 unsigned int logical_block_sectors; 230 int err; 231 232 /* 233 * Unless otherwise specified, default to 512 byte logical blocks and a 234 * physical block size equal to the logical block size. 235 */ 236 if (!lim->logical_block_size) 237 lim->logical_block_size = SECTOR_SIZE; 238 if (lim->physical_block_size < lim->logical_block_size) 239 lim->physical_block_size = lim->logical_block_size; 240 241 /* 242 * The minimum I/O size defaults to the physical block size unless 243 * explicitly overridden. 244 */ 245 if (lim->io_min < lim->physical_block_size) 246 lim->io_min = lim->physical_block_size; 247 248 /* 249 * max_hw_sectors has a somewhat weird default for historical reason, 250 * but driver really should set their own instead of relying on this 251 * value. 252 * 253 * The block layer relies on the fact that every driver can 254 * handle at lest a page worth of data per I/O, and needs the value 255 * aligned to the logical block size. 256 */ 257 if (!lim->max_hw_sectors) 258 lim->max_hw_sectors = BLK_SAFE_MAX_SECTORS; 259 if (WARN_ON_ONCE(lim->max_hw_sectors < PAGE_SECTORS)) 260 return -EINVAL; 261 logical_block_sectors = lim->logical_block_size >> SECTOR_SHIFT; 262 if (WARN_ON_ONCE(logical_block_sectors > lim->max_hw_sectors)) 263 return -EINVAL; 264 lim->max_hw_sectors = round_down(lim->max_hw_sectors, 265 logical_block_sectors); 266 267 /* 268 * The actual max_sectors value is a complex beast and also takes the 269 * max_dev_sectors value (set by SCSI ULPs) and a user configurable 270 * value into account. The ->max_sectors value is always calculated 271 * from these, so directly setting it won't have any effect. 272 */ 273 max_hw_sectors = min_not_zero(lim->max_hw_sectors, 274 lim->max_dev_sectors); 275 if (lim->max_user_sectors) { 276 if (lim->max_user_sectors < PAGE_SIZE / SECTOR_SIZE) 277 return -EINVAL; 278 lim->max_sectors = min(max_hw_sectors, lim->max_user_sectors); 279 } else if (lim->io_opt > (BLK_DEF_MAX_SECTORS_CAP << SECTOR_SHIFT)) { 280 lim->max_sectors = 281 min(max_hw_sectors, lim->io_opt >> SECTOR_SHIFT); 282 } else if (lim->io_min > (BLK_DEF_MAX_SECTORS_CAP << SECTOR_SHIFT)) { 283 lim->max_sectors = 284 min(max_hw_sectors, lim->io_min >> SECTOR_SHIFT); 285 } else { 286 lim->max_sectors = min(max_hw_sectors, BLK_DEF_MAX_SECTORS_CAP); 287 } 288 lim->max_sectors = round_down(lim->max_sectors, 289 logical_block_sectors); 290 291 /* 292 * Random default for the maximum number of segments. Driver should not 293 * rely on this and set their own. 294 */ 295 if (!lim->max_segments) 296 lim->max_segments = BLK_MAX_SEGMENTS; 297 298 lim->max_discard_sectors = 299 min(lim->max_hw_discard_sectors, lim->max_user_discard_sectors); 300 301 if (!lim->max_discard_segments) 302 lim->max_discard_segments = 1; 303 304 if (lim->discard_granularity < lim->physical_block_size) 305 lim->discard_granularity = lim->physical_block_size; 306 307 /* 308 * By default there is no limit on the segment boundary alignment, 309 * but if there is one it can't be smaller than the page size as 310 * that would break all the normal I/O patterns. 311 */ 312 if (!lim->seg_boundary_mask) 313 lim->seg_boundary_mask = BLK_SEG_BOUNDARY_MASK; 314 if (WARN_ON_ONCE(lim->seg_boundary_mask < PAGE_SIZE - 1)) 315 return -EINVAL; 316 317 /* 318 * Stacking device may have both virtual boundary and max segment 319 * size limit, so allow this setting now, and long-term the two 320 * might need to move out of stacking limits since we have immutable 321 * bvec and lower layer bio splitting is supposed to handle the two 322 * correctly. 323 */ 324 if (lim->virt_boundary_mask) { 325 if (!lim->max_segment_size) 326 lim->max_segment_size = UINT_MAX; 327 } else { 328 /* 329 * The maximum segment size has an odd historic 64k default that 330 * drivers probably should override. Just like the I/O size we 331 * require drivers to at least handle a full page per segment. 332 */ 333 if (!lim->max_segment_size) 334 lim->max_segment_size = BLK_MAX_SEGMENT_SIZE; 335 if (WARN_ON_ONCE(lim->max_segment_size < PAGE_SIZE)) 336 return -EINVAL; 337 } 338 339 /* 340 * We require drivers to at least do logical block aligned I/O, but 341 * historically could not check for that due to the separate calls 342 * to set the limits. Once the transition is finished the check 343 * below should be narrowed down to check the logical block size. 344 */ 345 if (!lim->dma_alignment) 346 lim->dma_alignment = SECTOR_SIZE - 1; 347 if (WARN_ON_ONCE(lim->dma_alignment > PAGE_SIZE)) 348 return -EINVAL; 349 350 if (lim->alignment_offset) { 351 lim->alignment_offset &= (lim->physical_block_size - 1); 352 lim->flags &= ~BLK_FLAG_MISALIGNED; 353 } 354 355 if (!(lim->features & BLK_FEAT_WRITE_CACHE)) 356 lim->features &= ~BLK_FEAT_FUA; 357 358 blk_validate_atomic_write_limits(lim); 359 360 err = blk_validate_integrity_limits(lim); 361 if (err) 362 return err; 363 return blk_validate_zoned_limits(lim); 364 } 365 366 /* 367 * Set the default limits for a newly allocated queue. @lim contains the 368 * initial limits set by the driver, which could be no limit in which case 369 * all fields are cleared to zero. 370 */ 371 int blk_set_default_limits(struct queue_limits *lim) 372 { 373 /* 374 * Most defaults are set by capping the bounds in blk_validate_limits, 375 * but max_user_discard_sectors is special and needs an explicit 376 * initialization to the max value here. 377 */ 378 lim->max_user_discard_sectors = UINT_MAX; 379 return blk_validate_limits(lim); 380 } 381 382 /** 383 * queue_limits_commit_update - commit an atomic update of queue limits 384 * @q: queue to update 385 * @lim: limits to apply 386 * 387 * Apply the limits in @lim that were obtained from queue_limits_start_update() 388 * and updated by the caller to @q. 389 * 390 * Returns 0 if successful, else a negative error code. 391 */ 392 int queue_limits_commit_update(struct request_queue *q, 393 struct queue_limits *lim) 394 { 395 int error; 396 397 error = blk_validate_limits(lim); 398 if (error) 399 goto out_unlock; 400 401 #ifdef CONFIG_BLK_INLINE_ENCRYPTION 402 if (q->crypto_profile && lim->integrity.tag_size) { 403 pr_warn("blk-integrity: Integrity and hardware inline encryption are not supported together.\n"); 404 error = -EINVAL; 405 goto out_unlock; 406 } 407 #endif 408 409 q->limits = *lim; 410 if (q->disk) 411 blk_apply_bdi_limits(q->disk->bdi, lim); 412 out_unlock: 413 mutex_unlock(&q->limits_lock); 414 return error; 415 } 416 EXPORT_SYMBOL_GPL(queue_limits_commit_update); 417 418 /** 419 * queue_limits_set - apply queue limits to queue 420 * @q: queue to update 421 * @lim: limits to apply 422 * 423 * Apply the limits in @lim that were freshly initialized to @q. 424 * To update existing limits use queue_limits_start_update() and 425 * queue_limits_commit_update() instead. 426 * 427 * Returns 0 if successful, else a negative error code. 428 */ 429 int queue_limits_set(struct request_queue *q, struct queue_limits *lim) 430 { 431 mutex_lock(&q->limits_lock); 432 return queue_limits_commit_update(q, lim); 433 } 434 EXPORT_SYMBOL_GPL(queue_limits_set); 435 436 /** 437 * blk_limits_io_min - set minimum request size for a device 438 * @limits: the queue limits 439 * @min: smallest I/O size in bytes 440 * 441 * Description: 442 * Some devices have an internal block size bigger than the reported 443 * hardware sector size. This function can be used to signal the 444 * smallest I/O the device can perform without incurring a performance 445 * penalty. 446 */ 447 void blk_limits_io_min(struct queue_limits *limits, unsigned int min) 448 { 449 limits->io_min = min; 450 451 if (limits->io_min < limits->logical_block_size) 452 limits->io_min = limits->logical_block_size; 453 454 if (limits->io_min < limits->physical_block_size) 455 limits->io_min = limits->physical_block_size; 456 } 457 EXPORT_SYMBOL(blk_limits_io_min); 458 459 /** 460 * blk_limits_io_opt - set optimal request size for a device 461 * @limits: the queue limits 462 * @opt: smallest I/O size in bytes 463 * 464 * Description: 465 * Storage devices may report an optimal I/O size, which is the 466 * device's preferred unit for sustained I/O. This is rarely reported 467 * for disk drives. For RAID arrays it is usually the stripe width or 468 * the internal track size. A properly aligned multiple of 469 * optimal_io_size is the preferred request size for workloads where 470 * sustained throughput is desired. 471 */ 472 void blk_limits_io_opt(struct queue_limits *limits, unsigned int opt) 473 { 474 limits->io_opt = opt; 475 } 476 EXPORT_SYMBOL(blk_limits_io_opt); 477 478 static int queue_limit_alignment_offset(const struct queue_limits *lim, 479 sector_t sector) 480 { 481 unsigned int granularity = max(lim->physical_block_size, lim->io_min); 482 unsigned int alignment = sector_div(sector, granularity >> SECTOR_SHIFT) 483 << SECTOR_SHIFT; 484 485 return (granularity + lim->alignment_offset - alignment) % granularity; 486 } 487 488 static unsigned int queue_limit_discard_alignment( 489 const struct queue_limits *lim, sector_t sector) 490 { 491 unsigned int alignment, granularity, offset; 492 493 if (!lim->max_discard_sectors) 494 return 0; 495 496 /* Why are these in bytes, not sectors? */ 497 alignment = lim->discard_alignment >> SECTOR_SHIFT; 498 granularity = lim->discard_granularity >> SECTOR_SHIFT; 499 if (!granularity) 500 return 0; 501 502 /* Offset of the partition start in 'granularity' sectors */ 503 offset = sector_div(sector, granularity); 504 505 /* And why do we do this modulus *again* in blkdev_issue_discard()? */ 506 offset = (granularity + alignment - offset) % granularity; 507 508 /* Turn it back into bytes, gaah */ 509 return offset << SECTOR_SHIFT; 510 } 511 512 static unsigned int blk_round_down_sectors(unsigned int sectors, unsigned int lbs) 513 { 514 sectors = round_down(sectors, lbs >> SECTOR_SHIFT); 515 if (sectors < PAGE_SIZE >> SECTOR_SHIFT) 516 sectors = PAGE_SIZE >> SECTOR_SHIFT; 517 return sectors; 518 } 519 520 /** 521 * blk_stack_limits - adjust queue_limits for stacked devices 522 * @t: the stacking driver limits (top device) 523 * @b: the underlying queue limits (bottom, component device) 524 * @start: first data sector within component device 525 * 526 * Description: 527 * This function is used by stacking drivers like MD and DM to ensure 528 * that all component devices have compatible block sizes and 529 * alignments. The stacking driver must provide a queue_limits 530 * struct (top) and then iteratively call the stacking function for 531 * all component (bottom) devices. The stacking function will 532 * attempt to combine the values and ensure proper alignment. 533 * 534 * Returns 0 if the top and bottom queue_limits are compatible. The 535 * top device's block sizes and alignment offsets may be adjusted to 536 * ensure alignment with the bottom device. If no compatible sizes 537 * and alignments exist, -1 is returned and the resulting top 538 * queue_limits will have the misaligned flag set to indicate that 539 * the alignment_offset is undefined. 540 */ 541 int blk_stack_limits(struct queue_limits *t, struct queue_limits *b, 542 sector_t start) 543 { 544 unsigned int top, bottom, alignment, ret = 0; 545 546 t->features |= (b->features & BLK_FEAT_INHERIT_MASK); 547 548 /* 549 * BLK_FEAT_NOWAIT and BLK_FEAT_POLL need to be supported both by the 550 * stacking driver and all underlying devices. The stacking driver sets 551 * the flags before stacking the limits, and this will clear the flags 552 * if any of the underlying devices does not support it. 553 */ 554 if (!(b->features & BLK_FEAT_NOWAIT)) 555 t->features &= ~BLK_FEAT_NOWAIT; 556 if (!(b->features & BLK_FEAT_POLL)) 557 t->features &= ~BLK_FEAT_POLL; 558 559 t->flags |= (b->flags & BLK_FLAG_MISALIGNED); 560 561 t->max_sectors = min_not_zero(t->max_sectors, b->max_sectors); 562 t->max_user_sectors = min_not_zero(t->max_user_sectors, 563 b->max_user_sectors); 564 t->max_hw_sectors = min_not_zero(t->max_hw_sectors, b->max_hw_sectors); 565 t->max_dev_sectors = min_not_zero(t->max_dev_sectors, b->max_dev_sectors); 566 t->max_write_zeroes_sectors = min(t->max_write_zeroes_sectors, 567 b->max_write_zeroes_sectors); 568 t->max_zone_append_sectors = min(queue_limits_max_zone_append_sectors(t), 569 queue_limits_max_zone_append_sectors(b)); 570 571 t->seg_boundary_mask = min_not_zero(t->seg_boundary_mask, 572 b->seg_boundary_mask); 573 t->virt_boundary_mask = min_not_zero(t->virt_boundary_mask, 574 b->virt_boundary_mask); 575 576 t->max_segments = min_not_zero(t->max_segments, b->max_segments); 577 t->max_discard_segments = min_not_zero(t->max_discard_segments, 578 b->max_discard_segments); 579 t->max_integrity_segments = min_not_zero(t->max_integrity_segments, 580 b->max_integrity_segments); 581 582 t->max_segment_size = min_not_zero(t->max_segment_size, 583 b->max_segment_size); 584 585 alignment = queue_limit_alignment_offset(b, start); 586 587 /* Bottom device has different alignment. Check that it is 588 * compatible with the current top alignment. 589 */ 590 if (t->alignment_offset != alignment) { 591 592 top = max(t->physical_block_size, t->io_min) 593 + t->alignment_offset; 594 bottom = max(b->physical_block_size, b->io_min) + alignment; 595 596 /* Verify that top and bottom intervals line up */ 597 if (max(top, bottom) % min(top, bottom)) { 598 t->flags |= BLK_FLAG_MISALIGNED; 599 ret = -1; 600 } 601 } 602 603 t->logical_block_size = max(t->logical_block_size, 604 b->logical_block_size); 605 606 t->physical_block_size = max(t->physical_block_size, 607 b->physical_block_size); 608 609 t->io_min = max(t->io_min, b->io_min); 610 t->io_opt = lcm_not_zero(t->io_opt, b->io_opt); 611 t->dma_alignment = max(t->dma_alignment, b->dma_alignment); 612 613 /* Set non-power-of-2 compatible chunk_sectors boundary */ 614 if (b->chunk_sectors) 615 t->chunk_sectors = gcd(t->chunk_sectors, b->chunk_sectors); 616 617 /* Physical block size a multiple of the logical block size? */ 618 if (t->physical_block_size & (t->logical_block_size - 1)) { 619 t->physical_block_size = t->logical_block_size; 620 t->flags |= BLK_FLAG_MISALIGNED; 621 ret = -1; 622 } 623 624 /* Minimum I/O a multiple of the physical block size? */ 625 if (t->io_min & (t->physical_block_size - 1)) { 626 t->io_min = t->physical_block_size; 627 t->flags |= BLK_FLAG_MISALIGNED; 628 ret = -1; 629 } 630 631 /* Optimal I/O a multiple of the physical block size? */ 632 if (t->io_opt & (t->physical_block_size - 1)) { 633 t->io_opt = 0; 634 t->flags |= BLK_FLAG_MISALIGNED; 635 ret = -1; 636 } 637 638 /* chunk_sectors a multiple of the physical block size? */ 639 if ((t->chunk_sectors << 9) & (t->physical_block_size - 1)) { 640 t->chunk_sectors = 0; 641 t->flags |= BLK_FLAG_MISALIGNED; 642 ret = -1; 643 } 644 645 /* Find lowest common alignment_offset */ 646 t->alignment_offset = lcm_not_zero(t->alignment_offset, alignment) 647 % max(t->physical_block_size, t->io_min); 648 649 /* Verify that new alignment_offset is on a logical block boundary */ 650 if (t->alignment_offset & (t->logical_block_size - 1)) { 651 t->flags |= BLK_FLAG_MISALIGNED; 652 ret = -1; 653 } 654 655 t->max_sectors = blk_round_down_sectors(t->max_sectors, t->logical_block_size); 656 t->max_hw_sectors = blk_round_down_sectors(t->max_hw_sectors, t->logical_block_size); 657 t->max_dev_sectors = blk_round_down_sectors(t->max_dev_sectors, t->logical_block_size); 658 659 /* Discard alignment and granularity */ 660 if (b->discard_granularity) { 661 alignment = queue_limit_discard_alignment(b, start); 662 663 t->max_discard_sectors = min_not_zero(t->max_discard_sectors, 664 b->max_discard_sectors); 665 t->max_hw_discard_sectors = min_not_zero(t->max_hw_discard_sectors, 666 b->max_hw_discard_sectors); 667 t->discard_granularity = max(t->discard_granularity, 668 b->discard_granularity); 669 t->discard_alignment = lcm_not_zero(t->discard_alignment, alignment) % 670 t->discard_granularity; 671 } 672 t->max_secure_erase_sectors = min_not_zero(t->max_secure_erase_sectors, 673 b->max_secure_erase_sectors); 674 t->zone_write_granularity = max(t->zone_write_granularity, 675 b->zone_write_granularity); 676 if (!(t->features & BLK_FEAT_ZONED)) { 677 t->zone_write_granularity = 0; 678 t->max_zone_append_sectors = 0; 679 } 680 return ret; 681 } 682 EXPORT_SYMBOL(blk_stack_limits); 683 684 /** 685 * queue_limits_stack_bdev - adjust queue_limits for stacked devices 686 * @t: the stacking driver limits (top device) 687 * @bdev: the underlying block device (bottom) 688 * @offset: offset to beginning of data within component device 689 * @pfx: prefix to use for warnings logged 690 * 691 * Description: 692 * This function is used by stacking drivers like MD and DM to ensure 693 * that all component devices have compatible block sizes and 694 * alignments. The stacking driver must provide a queue_limits 695 * struct (top) and then iteratively call the stacking function for 696 * all component (bottom) devices. The stacking function will 697 * attempt to combine the values and ensure proper alignment. 698 */ 699 void queue_limits_stack_bdev(struct queue_limits *t, struct block_device *bdev, 700 sector_t offset, const char *pfx) 701 { 702 if (blk_stack_limits(t, &bdev_get_queue(bdev)->limits, 703 get_start_sect(bdev) + offset)) 704 pr_notice("%s: Warning: Device %pg is misaligned\n", 705 pfx, bdev); 706 } 707 EXPORT_SYMBOL_GPL(queue_limits_stack_bdev); 708 709 /** 710 * queue_limits_stack_integrity - stack integrity profile 711 * @t: target queue limits 712 * @b: base queue limits 713 * 714 * Check if the integrity profile in the @b can be stacked into the 715 * target @t. Stacking is possible if either: 716 * 717 * a) does not have any integrity information stacked into it yet 718 * b) the integrity profile in @b is identical to the one in @t 719 * 720 * If @b can be stacked into @t, return %true. Else return %false and clear the 721 * integrity information in @t. 722 */ 723 bool queue_limits_stack_integrity(struct queue_limits *t, 724 struct queue_limits *b) 725 { 726 struct blk_integrity *ti = &t->integrity; 727 struct blk_integrity *bi = &b->integrity; 728 729 if (!IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY)) 730 return true; 731 732 if (!ti->tuple_size) { 733 /* inherit the settings from the first underlying device */ 734 if (!(ti->flags & BLK_INTEGRITY_STACKED)) { 735 ti->flags = BLK_INTEGRITY_DEVICE_CAPABLE | 736 (bi->flags & BLK_INTEGRITY_REF_TAG); 737 ti->csum_type = bi->csum_type; 738 ti->tuple_size = bi->tuple_size; 739 ti->pi_offset = bi->pi_offset; 740 ti->interval_exp = bi->interval_exp; 741 ti->tag_size = bi->tag_size; 742 goto done; 743 } 744 if (!bi->tuple_size) 745 goto done; 746 } 747 748 if (ti->tuple_size != bi->tuple_size) 749 goto incompatible; 750 if (ti->interval_exp != bi->interval_exp) 751 goto incompatible; 752 if (ti->tag_size != bi->tag_size) 753 goto incompatible; 754 if (ti->csum_type != bi->csum_type) 755 goto incompatible; 756 if ((ti->flags & BLK_INTEGRITY_REF_TAG) != 757 (bi->flags & BLK_INTEGRITY_REF_TAG)) 758 goto incompatible; 759 760 done: 761 ti->flags |= BLK_INTEGRITY_STACKED; 762 return true; 763 764 incompatible: 765 memset(ti, 0, sizeof(*ti)); 766 return false; 767 } 768 EXPORT_SYMBOL_GPL(queue_limits_stack_integrity); 769 770 /** 771 * blk_set_queue_depth - tell the block layer about the device queue depth 772 * @q: the request queue for the device 773 * @depth: queue depth 774 * 775 */ 776 void blk_set_queue_depth(struct request_queue *q, unsigned int depth) 777 { 778 q->queue_depth = depth; 779 rq_qos_queue_depth_changed(q); 780 } 781 EXPORT_SYMBOL(blk_set_queue_depth); 782 783 int bdev_alignment_offset(struct block_device *bdev) 784 { 785 struct request_queue *q = bdev_get_queue(bdev); 786 787 if (q->limits.flags & BLK_FLAG_MISALIGNED) 788 return -1; 789 if (bdev_is_partition(bdev)) 790 return queue_limit_alignment_offset(&q->limits, 791 bdev->bd_start_sect); 792 return q->limits.alignment_offset; 793 } 794 EXPORT_SYMBOL_GPL(bdev_alignment_offset); 795 796 unsigned int bdev_discard_alignment(struct block_device *bdev) 797 { 798 struct request_queue *q = bdev_get_queue(bdev); 799 800 if (bdev_is_partition(bdev)) 801 return queue_limit_discard_alignment(&q->limits, 802 bdev->bd_start_sect); 803 return q->limits.discard_alignment; 804 } 805 EXPORT_SYMBOL_GPL(bdev_discard_alignment); 806