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) { 280 lim->max_sectors = 281 min(max_hw_sectors, lim->io_opt >> SECTOR_SHIFT); 282 } else if (lim->io_min && 283 lim->io_min > (BLK_DEF_MAX_SECTORS_CAP << SECTOR_SHIFT)) { 284 lim->max_sectors = 285 min(max_hw_sectors, lim->io_min >> SECTOR_SHIFT); 286 } else { 287 lim->max_sectors = min(max_hw_sectors, BLK_DEF_MAX_SECTORS_CAP); 288 } 289 lim->max_sectors = round_down(lim->max_sectors, 290 logical_block_sectors); 291 292 /* 293 * Random default for the maximum number of segments. Driver should not 294 * rely on this and set their own. 295 */ 296 if (!lim->max_segments) 297 lim->max_segments = BLK_MAX_SEGMENTS; 298 299 lim->max_discard_sectors = 300 min(lim->max_hw_discard_sectors, lim->max_user_discard_sectors); 301 302 if (!lim->max_discard_segments) 303 lim->max_discard_segments = 1; 304 305 if (lim->discard_granularity < lim->physical_block_size) 306 lim->discard_granularity = lim->physical_block_size; 307 308 /* 309 * By default there is no limit on the segment boundary alignment, 310 * but if there is one it can't be smaller than the page size as 311 * that would break all the normal I/O patterns. 312 */ 313 if (!lim->seg_boundary_mask) 314 lim->seg_boundary_mask = BLK_SEG_BOUNDARY_MASK; 315 if (WARN_ON_ONCE(lim->seg_boundary_mask < PAGE_SIZE - 1)) 316 return -EINVAL; 317 318 /* 319 * Stacking device may have both virtual boundary and max segment 320 * size limit, so allow this setting now, and long-term the two 321 * might need to move out of stacking limits since we have immutable 322 * bvec and lower layer bio splitting is supposed to handle the two 323 * correctly. 324 */ 325 if (lim->virt_boundary_mask) { 326 if (!lim->max_segment_size) 327 lim->max_segment_size = UINT_MAX; 328 } else { 329 /* 330 * The maximum segment size has an odd historic 64k default that 331 * drivers probably should override. Just like the I/O size we 332 * require drivers to at least handle a full page per segment. 333 */ 334 if (!lim->max_segment_size) 335 lim->max_segment_size = BLK_MAX_SEGMENT_SIZE; 336 if (WARN_ON_ONCE(lim->max_segment_size < PAGE_SIZE)) 337 return -EINVAL; 338 } 339 340 /* 341 * We require drivers to at least do logical block aligned I/O, but 342 * historically could not check for that due to the separate calls 343 * to set the limits. Once the transition is finished the check 344 * below should be narrowed down to check the logical block size. 345 */ 346 if (!lim->dma_alignment) 347 lim->dma_alignment = SECTOR_SIZE - 1; 348 if (WARN_ON_ONCE(lim->dma_alignment > PAGE_SIZE)) 349 return -EINVAL; 350 351 if (lim->alignment_offset) { 352 lim->alignment_offset &= (lim->physical_block_size - 1); 353 lim->flags &= ~BLK_FLAG_MISALIGNED; 354 } 355 356 if (!(lim->features & BLK_FEAT_WRITE_CACHE)) 357 lim->features &= ~BLK_FEAT_FUA; 358 359 blk_validate_atomic_write_limits(lim); 360 361 err = blk_validate_integrity_limits(lim); 362 if (err) 363 return err; 364 return blk_validate_zoned_limits(lim); 365 } 366 367 /* 368 * Set the default limits for a newly allocated queue. @lim contains the 369 * initial limits set by the driver, which could be no limit in which case 370 * all fields are cleared to zero. 371 */ 372 int blk_set_default_limits(struct queue_limits *lim) 373 { 374 /* 375 * Most defaults are set by capping the bounds in blk_validate_limits, 376 * but max_user_discard_sectors is special and needs an explicit 377 * initialization to the max value here. 378 */ 379 lim->max_user_discard_sectors = UINT_MAX; 380 return blk_validate_limits(lim); 381 } 382 383 /** 384 * queue_limits_commit_update - commit an atomic update of queue limits 385 * @q: queue to update 386 * @lim: limits to apply 387 * 388 * Apply the limits in @lim that were obtained from queue_limits_start_update() 389 * and updated by the caller to @q. 390 * 391 * Returns 0 if successful, else a negative error code. 392 */ 393 int queue_limits_commit_update(struct request_queue *q, 394 struct queue_limits *lim) 395 { 396 int error; 397 398 error = blk_validate_limits(lim); 399 if (error) 400 goto out_unlock; 401 402 #ifdef CONFIG_BLK_INLINE_ENCRYPTION 403 if (q->crypto_profile && lim->integrity.tag_size) { 404 pr_warn("blk-integrity: Integrity and hardware inline encryption are not supported together.\n"); 405 error = -EINVAL; 406 goto out_unlock; 407 } 408 #endif 409 410 q->limits = *lim; 411 if (q->disk) 412 blk_apply_bdi_limits(q->disk->bdi, lim); 413 out_unlock: 414 mutex_unlock(&q->limits_lock); 415 return error; 416 } 417 EXPORT_SYMBOL_GPL(queue_limits_commit_update); 418 419 /** 420 * queue_limits_set - apply queue limits to queue 421 * @q: queue to update 422 * @lim: limits to apply 423 * 424 * Apply the limits in @lim that were freshly initialized to @q. 425 * To update existing limits use queue_limits_start_update() and 426 * queue_limits_commit_update() instead. 427 * 428 * Returns 0 if successful, else a negative error code. 429 */ 430 int queue_limits_set(struct request_queue *q, struct queue_limits *lim) 431 { 432 mutex_lock(&q->limits_lock); 433 return queue_limits_commit_update(q, lim); 434 } 435 EXPORT_SYMBOL_GPL(queue_limits_set); 436 437 /** 438 * blk_limits_io_min - set minimum request size for a device 439 * @limits: the queue limits 440 * @min: smallest I/O size in bytes 441 * 442 * Description: 443 * Some devices have an internal block size bigger than the reported 444 * hardware sector size. This function can be used to signal the 445 * smallest I/O the device can perform without incurring a performance 446 * penalty. 447 */ 448 void blk_limits_io_min(struct queue_limits *limits, unsigned int min) 449 { 450 limits->io_min = min; 451 452 if (limits->io_min < limits->logical_block_size) 453 limits->io_min = limits->logical_block_size; 454 455 if (limits->io_min < limits->physical_block_size) 456 limits->io_min = limits->physical_block_size; 457 } 458 EXPORT_SYMBOL(blk_limits_io_min); 459 460 /** 461 * blk_limits_io_opt - set optimal request size for a device 462 * @limits: the queue limits 463 * @opt: smallest I/O size in bytes 464 * 465 * Description: 466 * Storage devices may report an optimal I/O size, which is the 467 * device's preferred unit for sustained I/O. This is rarely reported 468 * for disk drives. For RAID arrays it is usually the stripe width or 469 * the internal track size. A properly aligned multiple of 470 * optimal_io_size is the preferred request size for workloads where 471 * sustained throughput is desired. 472 */ 473 void blk_limits_io_opt(struct queue_limits *limits, unsigned int opt) 474 { 475 limits->io_opt = opt; 476 } 477 EXPORT_SYMBOL(blk_limits_io_opt); 478 479 static int queue_limit_alignment_offset(const struct queue_limits *lim, 480 sector_t sector) 481 { 482 unsigned int granularity = max(lim->physical_block_size, lim->io_min); 483 unsigned int alignment = sector_div(sector, granularity >> SECTOR_SHIFT) 484 << SECTOR_SHIFT; 485 486 return (granularity + lim->alignment_offset - alignment) % granularity; 487 } 488 489 static unsigned int queue_limit_discard_alignment( 490 const struct queue_limits *lim, sector_t sector) 491 { 492 unsigned int alignment, granularity, offset; 493 494 if (!lim->max_discard_sectors) 495 return 0; 496 497 /* Why are these in bytes, not sectors? */ 498 alignment = lim->discard_alignment >> SECTOR_SHIFT; 499 granularity = lim->discard_granularity >> SECTOR_SHIFT; 500 if (!granularity) 501 return 0; 502 503 /* Offset of the partition start in 'granularity' sectors */ 504 offset = sector_div(sector, granularity); 505 506 /* And why do we do this modulus *again* in blkdev_issue_discard()? */ 507 offset = (granularity + alignment - offset) % granularity; 508 509 /* Turn it back into bytes, gaah */ 510 return offset << SECTOR_SHIFT; 511 } 512 513 static unsigned int blk_round_down_sectors(unsigned int sectors, unsigned int lbs) 514 { 515 sectors = round_down(sectors, lbs >> SECTOR_SHIFT); 516 if (sectors < PAGE_SIZE >> SECTOR_SHIFT) 517 sectors = PAGE_SIZE >> SECTOR_SHIFT; 518 return sectors; 519 } 520 521 /** 522 * blk_stack_limits - adjust queue_limits for stacked devices 523 * @t: the stacking driver limits (top device) 524 * @b: the underlying queue limits (bottom, component device) 525 * @start: first data sector within component device 526 * 527 * Description: 528 * This function is used by stacking drivers like MD and DM to ensure 529 * that all component devices have compatible block sizes and 530 * alignments. The stacking driver must provide a queue_limits 531 * struct (top) and then iteratively call the stacking function for 532 * all component (bottom) devices. The stacking function will 533 * attempt to combine the values and ensure proper alignment. 534 * 535 * Returns 0 if the top and bottom queue_limits are compatible. The 536 * top device's block sizes and alignment offsets may be adjusted to 537 * ensure alignment with the bottom device. If no compatible sizes 538 * and alignments exist, -1 is returned and the resulting top 539 * queue_limits will have the misaligned flag set to indicate that 540 * the alignment_offset is undefined. 541 */ 542 int blk_stack_limits(struct queue_limits *t, struct queue_limits *b, 543 sector_t start) 544 { 545 unsigned int top, bottom, alignment, ret = 0; 546 547 t->features |= (b->features & BLK_FEAT_INHERIT_MASK); 548 549 /* 550 * BLK_FEAT_NOWAIT and BLK_FEAT_POLL need to be supported both by the 551 * stacking driver and all underlying devices. The stacking driver sets 552 * the flags before stacking the limits, and this will clear the flags 553 * if any of the underlying devices does not support it. 554 */ 555 if (!(b->features & BLK_FEAT_NOWAIT)) 556 t->features &= ~BLK_FEAT_NOWAIT; 557 if (!(b->features & BLK_FEAT_POLL)) 558 t->features &= ~BLK_FEAT_POLL; 559 560 t->flags |= (b->flags & BLK_FLAG_MISALIGNED); 561 562 t->max_sectors = min_not_zero(t->max_sectors, b->max_sectors); 563 t->max_user_sectors = min_not_zero(t->max_user_sectors, 564 b->max_user_sectors); 565 t->max_hw_sectors = min_not_zero(t->max_hw_sectors, b->max_hw_sectors); 566 t->max_dev_sectors = min_not_zero(t->max_dev_sectors, b->max_dev_sectors); 567 t->max_write_zeroes_sectors = min(t->max_write_zeroes_sectors, 568 b->max_write_zeroes_sectors); 569 t->max_zone_append_sectors = min(queue_limits_max_zone_append_sectors(t), 570 queue_limits_max_zone_append_sectors(b)); 571 572 t->seg_boundary_mask = min_not_zero(t->seg_boundary_mask, 573 b->seg_boundary_mask); 574 t->virt_boundary_mask = min_not_zero(t->virt_boundary_mask, 575 b->virt_boundary_mask); 576 577 t->max_segments = min_not_zero(t->max_segments, b->max_segments); 578 t->max_discard_segments = min_not_zero(t->max_discard_segments, 579 b->max_discard_segments); 580 t->max_integrity_segments = min_not_zero(t->max_integrity_segments, 581 b->max_integrity_segments); 582 583 t->max_segment_size = min_not_zero(t->max_segment_size, 584 b->max_segment_size); 585 586 alignment = queue_limit_alignment_offset(b, start); 587 588 /* Bottom device has different alignment. Check that it is 589 * compatible with the current top alignment. 590 */ 591 if (t->alignment_offset != alignment) { 592 593 top = max(t->physical_block_size, t->io_min) 594 + t->alignment_offset; 595 bottom = max(b->physical_block_size, b->io_min) + alignment; 596 597 /* Verify that top and bottom intervals line up */ 598 if (max(top, bottom) % min(top, bottom)) { 599 t->flags |= BLK_FLAG_MISALIGNED; 600 ret = -1; 601 } 602 } 603 604 t->logical_block_size = max(t->logical_block_size, 605 b->logical_block_size); 606 607 t->physical_block_size = max(t->physical_block_size, 608 b->physical_block_size); 609 610 t->io_min = max(t->io_min, b->io_min); 611 t->io_opt = lcm_not_zero(t->io_opt, b->io_opt); 612 t->dma_alignment = max(t->dma_alignment, b->dma_alignment); 613 614 /* Set non-power-of-2 compatible chunk_sectors boundary */ 615 if (b->chunk_sectors) 616 t->chunk_sectors = gcd(t->chunk_sectors, b->chunk_sectors); 617 618 /* Physical block size a multiple of the logical block size? */ 619 if (t->physical_block_size & (t->logical_block_size - 1)) { 620 t->physical_block_size = t->logical_block_size; 621 t->flags |= BLK_FLAG_MISALIGNED; 622 ret = -1; 623 } 624 625 /* Minimum I/O a multiple of the physical block size? */ 626 if (t->io_min & (t->physical_block_size - 1)) { 627 t->io_min = t->physical_block_size; 628 t->flags |= BLK_FLAG_MISALIGNED; 629 ret = -1; 630 } 631 632 /* Optimal I/O a multiple of the physical block size? */ 633 if (t->io_opt & (t->physical_block_size - 1)) { 634 t->io_opt = 0; 635 t->flags |= BLK_FLAG_MISALIGNED; 636 ret = -1; 637 } 638 639 /* chunk_sectors a multiple of the physical block size? */ 640 if ((t->chunk_sectors << 9) & (t->physical_block_size - 1)) { 641 t->chunk_sectors = 0; 642 t->flags |= BLK_FLAG_MISALIGNED; 643 ret = -1; 644 } 645 646 /* Find lowest common alignment_offset */ 647 t->alignment_offset = lcm_not_zero(t->alignment_offset, alignment) 648 % max(t->physical_block_size, t->io_min); 649 650 /* Verify that new alignment_offset is on a logical block boundary */ 651 if (t->alignment_offset & (t->logical_block_size - 1)) { 652 t->flags |= BLK_FLAG_MISALIGNED; 653 ret = -1; 654 } 655 656 t->max_sectors = blk_round_down_sectors(t->max_sectors, t->logical_block_size); 657 t->max_hw_sectors = blk_round_down_sectors(t->max_hw_sectors, t->logical_block_size); 658 t->max_dev_sectors = blk_round_down_sectors(t->max_dev_sectors, t->logical_block_size); 659 660 /* Discard alignment and granularity */ 661 if (b->discard_granularity) { 662 alignment = queue_limit_discard_alignment(b, start); 663 664 t->max_discard_sectors = min_not_zero(t->max_discard_sectors, 665 b->max_discard_sectors); 666 t->max_hw_discard_sectors = min_not_zero(t->max_hw_discard_sectors, 667 b->max_hw_discard_sectors); 668 t->discard_granularity = max(t->discard_granularity, 669 b->discard_granularity); 670 t->discard_alignment = lcm_not_zero(t->discard_alignment, alignment) % 671 t->discard_granularity; 672 } 673 t->max_secure_erase_sectors = min_not_zero(t->max_secure_erase_sectors, 674 b->max_secure_erase_sectors); 675 t->zone_write_granularity = max(t->zone_write_granularity, 676 b->zone_write_granularity); 677 if (!(t->features & BLK_FEAT_ZONED)) { 678 t->zone_write_granularity = 0; 679 t->max_zone_append_sectors = 0; 680 } 681 return ret; 682 } 683 EXPORT_SYMBOL(blk_stack_limits); 684 685 /** 686 * queue_limits_stack_bdev - adjust queue_limits for stacked devices 687 * @t: the stacking driver limits (top device) 688 * @bdev: the underlying block device (bottom) 689 * @offset: offset to beginning of data within component device 690 * @pfx: prefix to use for warnings logged 691 * 692 * Description: 693 * This function is used by stacking drivers like MD and DM to ensure 694 * that all component devices have compatible block sizes and 695 * alignments. The stacking driver must provide a queue_limits 696 * struct (top) and then iteratively call the stacking function for 697 * all component (bottom) devices. The stacking function will 698 * attempt to combine the values and ensure proper alignment. 699 */ 700 void queue_limits_stack_bdev(struct queue_limits *t, struct block_device *bdev, 701 sector_t offset, const char *pfx) 702 { 703 if (blk_stack_limits(t, &bdev_get_queue(bdev)->limits, 704 get_start_sect(bdev) + offset)) 705 pr_notice("%s: Warning: Device %pg is misaligned\n", 706 pfx, bdev); 707 } 708 EXPORT_SYMBOL_GPL(queue_limits_stack_bdev); 709 710 /** 711 * queue_limits_stack_integrity - stack integrity profile 712 * @t: target queue limits 713 * @b: base queue limits 714 * 715 * Check if the integrity profile in the @b can be stacked into the 716 * target @t. Stacking is possible if either: 717 * 718 * a) does not have any integrity information stacked into it yet 719 * b) the integrity profile in @b is identical to the one in @t 720 * 721 * If @b can be stacked into @t, return %true. Else return %false and clear the 722 * integrity information in @t. 723 */ 724 bool queue_limits_stack_integrity(struct queue_limits *t, 725 struct queue_limits *b) 726 { 727 struct blk_integrity *ti = &t->integrity; 728 struct blk_integrity *bi = &b->integrity; 729 730 if (!IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY)) 731 return true; 732 733 if (!ti->tuple_size) { 734 /* inherit the settings from the first underlying device */ 735 if (!(ti->flags & BLK_INTEGRITY_STACKED)) { 736 ti->flags = BLK_INTEGRITY_DEVICE_CAPABLE | 737 (bi->flags & BLK_INTEGRITY_REF_TAG); 738 ti->csum_type = bi->csum_type; 739 ti->tuple_size = bi->tuple_size; 740 ti->pi_offset = bi->pi_offset; 741 ti->interval_exp = bi->interval_exp; 742 ti->tag_size = bi->tag_size; 743 goto done; 744 } 745 if (!bi->tuple_size) 746 goto done; 747 } 748 749 if (ti->tuple_size != bi->tuple_size) 750 goto incompatible; 751 if (ti->interval_exp != bi->interval_exp) 752 goto incompatible; 753 if (ti->tag_size != bi->tag_size) 754 goto incompatible; 755 if (ti->csum_type != bi->csum_type) 756 goto incompatible; 757 if ((ti->flags & BLK_INTEGRITY_REF_TAG) != 758 (bi->flags & BLK_INTEGRITY_REF_TAG)) 759 goto incompatible; 760 761 done: 762 ti->flags |= BLK_INTEGRITY_STACKED; 763 return true; 764 765 incompatible: 766 memset(ti, 0, sizeof(*ti)); 767 return false; 768 } 769 EXPORT_SYMBOL_GPL(queue_limits_stack_integrity); 770 771 /** 772 * blk_set_queue_depth - tell the block layer about the device queue depth 773 * @q: the request queue for the device 774 * @depth: queue depth 775 * 776 */ 777 void blk_set_queue_depth(struct request_queue *q, unsigned int depth) 778 { 779 q->queue_depth = depth; 780 rq_qos_queue_depth_changed(q); 781 } 782 EXPORT_SYMBOL(blk_set_queue_depth); 783 784 int bdev_alignment_offset(struct block_device *bdev) 785 { 786 struct request_queue *q = bdev_get_queue(bdev); 787 788 if (q->limits.flags & BLK_FLAG_MISALIGNED) 789 return -1; 790 if (bdev_is_partition(bdev)) 791 return queue_limit_alignment_offset(&q->limits, 792 bdev->bd_start_sect); 793 return q->limits.alignment_offset; 794 } 795 EXPORT_SYMBOL_GPL(bdev_alignment_offset); 796 797 unsigned int bdev_discard_alignment(struct block_device *bdev) 798 { 799 struct request_queue *q = bdev_get_queue(bdev); 800 801 if (bdev_is_partition(bdev)) 802 return queue_limit_discard_alignment(&q->limits, 803 bdev->bd_start_sect); 804 return q->limits.discard_alignment; 805 } 806 EXPORT_SYMBOL_GPL(bdev_discard_alignment); 807