1 // SPDX-License-Identifier: GPL-2.0
3 * Functions related to setting various queue properties from drivers
5 #include <linux/kernel.h>
6 #include <linux/module.h>
7 #include <linux/init.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>
19 #include "blk-rq-qos.h"
22 void blk_queue_rq_timeout(struct request_queue
*q
, unsigned int timeout
)
24 q
->rq_timeout
= timeout
;
26 EXPORT_SYMBOL_GPL(blk_queue_rq_timeout
);
29 * blk_set_stacking_limits - set default limits for stacking devices
30 * @lim: the queue_limits structure to reset
32 * Prepare queue limits for applying limits from underlying devices using
35 void blk_set_stacking_limits(struct queue_limits
*lim
)
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
;
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_hw_zone_append_sectors
= UINT_MAX
;
54 lim
->max_user_discard_sectors
= UINT_MAX
;
56 EXPORT_SYMBOL(blk_set_stacking_limits
);
58 void blk_apply_bdi_limits(struct backing_dev_info
*bdi
,
59 struct queue_limits
*lim
)
62 * For read-ahead of large files to be effective, we need to read ahead
63 * at least twice the optimal I/O size.
65 bdi
->ra_pages
= max(lim
->io_opt
* 2 / PAGE_SIZE
, VM_READAHEAD_PAGES
);
66 bdi
->io_pages
= lim
->max_sectors
>> PAGE_SECTORS_SHIFT
;
69 static int blk_validate_zoned_limits(struct queue_limits
*lim
)
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
))
80 if (WARN_ON_ONCE(!IS_ENABLED(CONFIG_BLK_DEV_ZONED
)))
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.
87 if (lim
->max_active_zones
&&
88 lim
->max_open_zones
> lim
->max_active_zones
)
91 if (lim
->zone_write_granularity
< lim
->logical_block_size
)
92 lim
->zone_write_granularity
= lim
->logical_block_size
;
95 * The Zone Append size is limited by the maximum I/O size and the zone
96 * size given that it can't span zones.
98 * If no max_hw_zone_append_sectors limit is provided, the block layer
99 * will emulated it, else we're also bound by the hardware limit.
101 lim
->max_zone_append_sectors
=
102 min_not_zero(lim
->max_hw_zone_append_sectors
,
103 min(lim
->chunk_sectors
, lim
->max_hw_sectors
));
107 static int blk_validate_integrity_limits(struct queue_limits
*lim
)
109 struct blk_integrity
*bi
= &lim
->integrity
;
111 if (!bi
->tuple_size
) {
112 if (bi
->csum_type
!= BLK_INTEGRITY_CSUM_NONE
||
113 bi
->tag_size
|| ((bi
->flags
& BLK_INTEGRITY_REF_TAG
))) {
114 pr_warn("invalid PI settings.\n");
120 if (!IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY
)) {
121 pr_warn("integrity support disabled.\n");
125 if (bi
->csum_type
== BLK_INTEGRITY_CSUM_NONE
&&
126 (bi
->flags
& BLK_INTEGRITY_REF_TAG
)) {
127 pr_warn("ref tag not support without checksum.\n");
131 if (!bi
->interval_exp
)
132 bi
->interval_exp
= ilog2(lim
->logical_block_size
);
138 * Returns max guaranteed bytes which we can fit in a bio.
140 * We request that an atomic_write is ITER_UBUF iov_iter (so a single vector),
141 * so we assume that we can fit in at least PAGE_SIZE in a segment, apart from
142 * the first and last segments.
144 static unsigned int blk_queue_max_guaranteed_bio(struct queue_limits
*lim
)
146 unsigned int max_segments
= min(BIO_MAX_VECS
, lim
->max_segments
);
149 length
= min(max_segments
, 2) * lim
->logical_block_size
;
150 if (max_segments
> 2)
151 length
+= (max_segments
- 2) * PAGE_SIZE
;
156 static void blk_atomic_writes_update_limits(struct queue_limits
*lim
)
158 unsigned int unit_limit
= min(lim
->max_hw_sectors
<< SECTOR_SHIFT
,
159 blk_queue_max_guaranteed_bio(lim
));
161 unit_limit
= rounddown_pow_of_two(unit_limit
);
163 lim
->atomic_write_max_sectors
=
164 min(lim
->atomic_write_hw_max
>> SECTOR_SHIFT
,
165 lim
->max_hw_sectors
);
166 lim
->atomic_write_unit_min
=
167 min(lim
->atomic_write_hw_unit_min
, unit_limit
);
168 lim
->atomic_write_unit_max
=
169 min(lim
->atomic_write_hw_unit_max
, unit_limit
);
170 lim
->atomic_write_boundary_sectors
=
171 lim
->atomic_write_hw_boundary
>> SECTOR_SHIFT
;
174 static void blk_validate_atomic_write_limits(struct queue_limits
*lim
)
176 unsigned int boundary_sectors
;
178 if (!lim
->atomic_write_hw_max
)
181 if (WARN_ON_ONCE(!is_power_of_2(lim
->atomic_write_hw_unit_min
)))
184 if (WARN_ON_ONCE(!is_power_of_2(lim
->atomic_write_hw_unit_max
)))
187 if (WARN_ON_ONCE(lim
->atomic_write_hw_unit_min
>
188 lim
->atomic_write_hw_unit_max
))
191 if (WARN_ON_ONCE(lim
->atomic_write_hw_unit_max
>
192 lim
->atomic_write_hw_max
))
195 boundary_sectors
= lim
->atomic_write_hw_boundary
>> SECTOR_SHIFT
;
197 if (boundary_sectors
) {
198 if (WARN_ON_ONCE(lim
->atomic_write_hw_max
>
199 lim
->atomic_write_hw_boundary
))
202 * A feature of boundary support is that it disallows bios to
203 * be merged which would result in a merged request which
204 * crosses either a chunk sector or atomic write HW boundary,
205 * even though chunk sectors may be just set for performance.
206 * For simplicity, disallow atomic writes for a chunk sector
207 * which is non-zero and smaller than atomic write HW boundary.
208 * Furthermore, chunk sectors must be a multiple of atomic
209 * write HW boundary. Otherwise boundary support becomes
211 * Devices which do not conform to these rules can be dealt
212 * with if and when they show up.
214 if (WARN_ON_ONCE(lim
->chunk_sectors
% boundary_sectors
))
218 * The boundary size just needs to be a multiple of unit_max
219 * (and not necessarily a power-of-2), so this following check
220 * could be relaxed in future.
221 * Furthermore, if needed, unit_max could even be reduced so
222 * that it is compliant with a !power-of-2 boundary.
224 if (!is_power_of_2(boundary_sectors
))
228 blk_atomic_writes_update_limits(lim
);
232 lim
->atomic_write_max_sectors
= 0;
233 lim
->atomic_write_boundary_sectors
= 0;
234 lim
->atomic_write_unit_min
= 0;
235 lim
->atomic_write_unit_max
= 0;
239 * Check that the limits in lim are valid, initialize defaults for unset
240 * values, and cap values based on others where needed.
242 int blk_validate_limits(struct queue_limits
*lim
)
244 unsigned int max_hw_sectors
;
245 unsigned int logical_block_sectors
;
249 * Unless otherwise specified, default to 512 byte logical blocks and a
250 * physical block size equal to the logical block size.
252 if (!lim
->logical_block_size
)
253 lim
->logical_block_size
= SECTOR_SIZE
;
254 else if (blk_validate_block_size(lim
->logical_block_size
)) {
255 pr_warn("Invalid logical block size (%d)\n", lim
->logical_block_size
);
258 if (lim
->physical_block_size
< lim
->logical_block_size
)
259 lim
->physical_block_size
= lim
->logical_block_size
;
262 * The minimum I/O size defaults to the physical block size unless
263 * explicitly overridden.
265 if (lim
->io_min
< lim
->physical_block_size
)
266 lim
->io_min
= lim
->physical_block_size
;
269 * The optimal I/O size may not be aligned to physical block size
270 * (because it may be limited by dma engines which have no clue about
271 * block size of the disks attached to them), so we round it down here.
273 lim
->io_opt
= round_down(lim
->io_opt
, lim
->physical_block_size
);
276 * max_hw_sectors has a somewhat weird default for historical reason,
277 * but driver really should set their own instead of relying on this
280 * The block layer relies on the fact that every driver can
281 * handle at lest a page worth of data per I/O, and needs the value
282 * aligned to the logical block size.
284 if (!lim
->max_hw_sectors
)
285 lim
->max_hw_sectors
= BLK_SAFE_MAX_SECTORS
;
286 if (WARN_ON_ONCE(lim
->max_hw_sectors
< PAGE_SECTORS
))
288 logical_block_sectors
= lim
->logical_block_size
>> SECTOR_SHIFT
;
289 if (WARN_ON_ONCE(logical_block_sectors
> lim
->max_hw_sectors
))
291 lim
->max_hw_sectors
= round_down(lim
->max_hw_sectors
,
292 logical_block_sectors
);
295 * The actual max_sectors value is a complex beast and also takes the
296 * max_dev_sectors value (set by SCSI ULPs) and a user configurable
297 * value into account. The ->max_sectors value is always calculated
298 * from these, so directly setting it won't have any effect.
300 max_hw_sectors
= min_not_zero(lim
->max_hw_sectors
,
301 lim
->max_dev_sectors
);
302 if (lim
->max_user_sectors
) {
303 if (lim
->max_user_sectors
< PAGE_SIZE
/ SECTOR_SIZE
)
305 lim
->max_sectors
= min(max_hw_sectors
, lim
->max_user_sectors
);
306 } else if (lim
->io_opt
> (BLK_DEF_MAX_SECTORS_CAP
<< SECTOR_SHIFT
)) {
308 min(max_hw_sectors
, lim
->io_opt
>> SECTOR_SHIFT
);
309 } else if (lim
->io_min
> (BLK_DEF_MAX_SECTORS_CAP
<< SECTOR_SHIFT
)) {
311 min(max_hw_sectors
, lim
->io_min
>> SECTOR_SHIFT
);
313 lim
->max_sectors
= min(max_hw_sectors
, BLK_DEF_MAX_SECTORS_CAP
);
315 lim
->max_sectors
= round_down(lim
->max_sectors
,
316 logical_block_sectors
);
319 * Random default for the maximum number of segments. Driver should not
320 * rely on this and set their own.
322 if (!lim
->max_segments
)
323 lim
->max_segments
= BLK_MAX_SEGMENTS
;
325 lim
->max_discard_sectors
=
326 min(lim
->max_hw_discard_sectors
, lim
->max_user_discard_sectors
);
328 if (!lim
->max_discard_segments
)
329 lim
->max_discard_segments
= 1;
331 if (lim
->discard_granularity
< lim
->physical_block_size
)
332 lim
->discard_granularity
= lim
->physical_block_size
;
335 * By default there is no limit on the segment boundary alignment,
336 * but if there is one it can't be smaller than the page size as
337 * that would break all the normal I/O patterns.
339 if (!lim
->seg_boundary_mask
)
340 lim
->seg_boundary_mask
= BLK_SEG_BOUNDARY_MASK
;
341 if (WARN_ON_ONCE(lim
->seg_boundary_mask
< PAGE_SIZE
- 1))
345 * Stacking device may have both virtual boundary and max segment
346 * size limit, so allow this setting now, and long-term the two
347 * might need to move out of stacking limits since we have immutable
348 * bvec and lower layer bio splitting is supposed to handle the two
351 if (lim
->virt_boundary_mask
) {
352 if (!lim
->max_segment_size
)
353 lim
->max_segment_size
= UINT_MAX
;
356 * The maximum segment size has an odd historic 64k default that
357 * drivers probably should override. Just like the I/O size we
358 * require drivers to at least handle a full page per segment.
360 if (!lim
->max_segment_size
)
361 lim
->max_segment_size
= BLK_MAX_SEGMENT_SIZE
;
362 if (WARN_ON_ONCE(lim
->max_segment_size
< PAGE_SIZE
))
367 * We require drivers to at least do logical block aligned I/O, but
368 * historically could not check for that due to the separate calls
369 * to set the limits. Once the transition is finished the check
370 * below should be narrowed down to check the logical block size.
372 if (!lim
->dma_alignment
)
373 lim
->dma_alignment
= SECTOR_SIZE
- 1;
374 if (WARN_ON_ONCE(lim
->dma_alignment
> PAGE_SIZE
))
377 if (lim
->alignment_offset
) {
378 lim
->alignment_offset
&= (lim
->physical_block_size
- 1);
379 lim
->flags
&= ~BLK_FLAG_MISALIGNED
;
382 if (!(lim
->features
& BLK_FEAT_WRITE_CACHE
))
383 lim
->features
&= ~BLK_FEAT_FUA
;
385 blk_validate_atomic_write_limits(lim
);
387 err
= blk_validate_integrity_limits(lim
);
390 return blk_validate_zoned_limits(lim
);
392 EXPORT_SYMBOL_GPL(blk_validate_limits
);
395 * Set the default limits for a newly allocated queue. @lim contains the
396 * initial limits set by the driver, which could be no limit in which case
397 * all fields are cleared to zero.
399 int blk_set_default_limits(struct queue_limits
*lim
)
402 * Most defaults are set by capping the bounds in blk_validate_limits,
403 * but max_user_discard_sectors is special and needs an explicit
404 * initialization to the max value here.
406 lim
->max_user_discard_sectors
= UINT_MAX
;
407 return blk_validate_limits(lim
);
411 * queue_limits_commit_update - commit an atomic update of queue limits
412 * @q: queue to update
413 * @lim: limits to apply
415 * Apply the limits in @lim that were obtained from queue_limits_start_update()
416 * and updated by the caller to @q.
418 * Returns 0 if successful, else a negative error code.
420 int queue_limits_commit_update(struct request_queue
*q
,
421 struct queue_limits
*lim
)
425 error
= blk_validate_limits(lim
);
429 #ifdef CONFIG_BLK_INLINE_ENCRYPTION
430 if (q
->crypto_profile
&& lim
->integrity
.tag_size
) {
431 pr_warn("blk-integrity: Integrity and hardware inline encryption are not supported together.\n");
439 blk_apply_bdi_limits(q
->disk
->bdi
, lim
);
441 mutex_unlock(&q
->limits_lock
);
444 EXPORT_SYMBOL_GPL(queue_limits_commit_update
);
447 * queue_limits_set - apply queue limits to queue
448 * @q: queue to update
449 * @lim: limits to apply
451 * Apply the limits in @lim that were freshly initialized to @q.
452 * To update existing limits use queue_limits_start_update() and
453 * queue_limits_commit_update() instead.
455 * Returns 0 if successful, else a negative error code.
457 int queue_limits_set(struct request_queue
*q
, struct queue_limits
*lim
)
459 mutex_lock(&q
->limits_lock
);
460 return queue_limits_commit_update(q
, lim
);
462 EXPORT_SYMBOL_GPL(queue_limits_set
);
464 static int queue_limit_alignment_offset(const struct queue_limits
*lim
,
467 unsigned int granularity
= max(lim
->physical_block_size
, lim
->io_min
);
468 unsigned int alignment
= sector_div(sector
, granularity
>> SECTOR_SHIFT
)
471 return (granularity
+ lim
->alignment_offset
- alignment
) % granularity
;
474 static unsigned int queue_limit_discard_alignment(
475 const struct queue_limits
*lim
, sector_t sector
)
477 unsigned int alignment
, granularity
, offset
;
479 if (!lim
->max_discard_sectors
)
482 /* Why are these in bytes, not sectors? */
483 alignment
= lim
->discard_alignment
>> SECTOR_SHIFT
;
484 granularity
= lim
->discard_granularity
>> SECTOR_SHIFT
;
486 /* Offset of the partition start in 'granularity' sectors */
487 offset
= sector_div(sector
, granularity
);
489 /* And why do we do this modulus *again* in blkdev_issue_discard()? */
490 offset
= (granularity
+ alignment
- offset
) % granularity
;
492 /* Turn it back into bytes, gaah */
493 return offset
<< SECTOR_SHIFT
;
496 static unsigned int blk_round_down_sectors(unsigned int sectors
, unsigned int lbs
)
498 sectors
= round_down(sectors
, lbs
>> SECTOR_SHIFT
);
499 if (sectors
< PAGE_SIZE
>> SECTOR_SHIFT
)
500 sectors
= PAGE_SIZE
>> SECTOR_SHIFT
;
504 /* Check if second and later bottom devices are compliant */
505 static bool blk_stack_atomic_writes_tail(struct queue_limits
*t
,
506 struct queue_limits
*b
)
508 /* We're not going to support different boundary sizes.. yet */
509 if (t
->atomic_write_hw_boundary
!= b
->atomic_write_hw_boundary
)
512 /* Can't support this */
513 if (t
->atomic_write_hw_unit_min
> b
->atomic_write_hw_unit_max
)
517 if (t
->atomic_write_hw_unit_max
< b
->atomic_write_hw_unit_min
)
520 t
->atomic_write_hw_max
= min(t
->atomic_write_hw_max
,
521 b
->atomic_write_hw_max
);
522 t
->atomic_write_hw_unit_min
= max(t
->atomic_write_hw_unit_min
,
523 b
->atomic_write_hw_unit_min
);
524 t
->atomic_write_hw_unit_max
= min(t
->atomic_write_hw_unit_max
,
525 b
->atomic_write_hw_unit_max
);
529 /* Check for valid boundary of first bottom device */
530 static bool blk_stack_atomic_writes_boundary_head(struct queue_limits
*t
,
531 struct queue_limits
*b
)
534 * Ensure atomic write boundary is aligned with chunk sectors. Stacked
535 * devices store chunk sectors in t->io_min.
537 if (b
->atomic_write_hw_boundary
> t
->io_min
&&
538 b
->atomic_write_hw_boundary
% t
->io_min
)
540 if (t
->io_min
> b
->atomic_write_hw_boundary
&&
541 t
->io_min
% b
->atomic_write_hw_boundary
)
544 t
->atomic_write_hw_boundary
= b
->atomic_write_hw_boundary
;
549 /* Check stacking of first bottom device */
550 static bool blk_stack_atomic_writes_head(struct queue_limits
*t
,
551 struct queue_limits
*b
)
553 if (b
->atomic_write_hw_boundary
&&
554 !blk_stack_atomic_writes_boundary_head(t
, b
))
557 if (t
->io_min
<= SECTOR_SIZE
) {
558 /* No chunk sectors, so use bottom device values directly */
559 t
->atomic_write_hw_unit_max
= b
->atomic_write_hw_unit_max
;
560 t
->atomic_write_hw_unit_min
= b
->atomic_write_hw_unit_min
;
561 t
->atomic_write_hw_max
= b
->atomic_write_hw_max
;
566 * Find values for limits which work for chunk size.
567 * b->atomic_write_hw_unit_{min, max} may not be aligned with chunk
568 * size (t->io_min), as chunk size is not restricted to a power-of-2.
569 * So we need to find highest power-of-2 which works for the chunk
571 * As an example scenario, we could have b->unit_max = 16K and
572 * t->io_min = 24K. For this case, reduce t->unit_max to a value
573 * aligned with both limits, i.e. 8K in this example.
575 t
->atomic_write_hw_unit_max
= b
->atomic_write_hw_unit_max
;
576 while (t
->io_min
% t
->atomic_write_hw_unit_max
)
577 t
->atomic_write_hw_unit_max
/= 2;
579 t
->atomic_write_hw_unit_min
= min(b
->atomic_write_hw_unit_min
,
580 t
->atomic_write_hw_unit_max
);
581 t
->atomic_write_hw_max
= min(b
->atomic_write_hw_max
, t
->io_min
);
586 static void blk_stack_atomic_writes_limits(struct queue_limits
*t
,
587 struct queue_limits
*b
)
589 if (!(t
->features
& BLK_FEAT_ATOMIC_WRITES_STACKED
))
592 if (!b
->atomic_write_unit_min
)
596 * If atomic_write_hw_max is set, we have already stacked 1x bottom
597 * device, so check for compliance.
599 if (t
->atomic_write_hw_max
) {
600 if (!blk_stack_atomic_writes_tail(t
, b
))
605 if (!blk_stack_atomic_writes_head(t
, b
))
610 t
->atomic_write_hw_max
= 0;
611 t
->atomic_write_hw_unit_max
= 0;
612 t
->atomic_write_hw_unit_min
= 0;
613 t
->atomic_write_hw_boundary
= 0;
614 t
->features
&= ~BLK_FEAT_ATOMIC_WRITES_STACKED
;
618 * blk_stack_limits - adjust queue_limits for stacked devices
619 * @t: the stacking driver limits (top device)
620 * @b: the underlying queue limits (bottom, component device)
621 * @start: first data sector within component device
624 * This function is used by stacking drivers like MD and DM to ensure
625 * that all component devices have compatible block sizes and
626 * alignments. The stacking driver must provide a queue_limits
627 * struct (top) and then iteratively call the stacking function for
628 * all component (bottom) devices. The stacking function will
629 * attempt to combine the values and ensure proper alignment.
631 * Returns 0 if the top and bottom queue_limits are compatible. The
632 * top device's block sizes and alignment offsets may be adjusted to
633 * ensure alignment with the bottom device. If no compatible sizes
634 * and alignments exist, -1 is returned and the resulting top
635 * queue_limits will have the misaligned flag set to indicate that
636 * the alignment_offset is undefined.
638 int blk_stack_limits(struct queue_limits
*t
, struct queue_limits
*b
,
641 unsigned int top
, bottom
, alignment
, ret
= 0;
643 t
->features
|= (b
->features
& BLK_FEAT_INHERIT_MASK
);
646 * Some feaures need to be supported both by the stacking driver and all
647 * underlying devices. The stacking driver sets these flags before
648 * stacking the limits, and this will clear the flags if any of the
649 * underlying devices does not support it.
651 if (!(b
->features
& BLK_FEAT_NOWAIT
))
652 t
->features
&= ~BLK_FEAT_NOWAIT
;
653 if (!(b
->features
& BLK_FEAT_POLL
))
654 t
->features
&= ~BLK_FEAT_POLL
;
656 t
->flags
|= (b
->flags
& BLK_FLAG_MISALIGNED
);
658 t
->max_sectors
= min_not_zero(t
->max_sectors
, b
->max_sectors
);
659 t
->max_user_sectors
= min_not_zero(t
->max_user_sectors
,
660 b
->max_user_sectors
);
661 t
->max_hw_sectors
= min_not_zero(t
->max_hw_sectors
, b
->max_hw_sectors
);
662 t
->max_dev_sectors
= min_not_zero(t
->max_dev_sectors
, b
->max_dev_sectors
);
663 t
->max_write_zeroes_sectors
= min(t
->max_write_zeroes_sectors
,
664 b
->max_write_zeroes_sectors
);
665 t
->max_hw_zone_append_sectors
= min(t
->max_hw_zone_append_sectors
,
666 b
->max_hw_zone_append_sectors
);
668 t
->seg_boundary_mask
= min_not_zero(t
->seg_boundary_mask
,
669 b
->seg_boundary_mask
);
670 t
->virt_boundary_mask
= min_not_zero(t
->virt_boundary_mask
,
671 b
->virt_boundary_mask
);
673 t
->max_segments
= min_not_zero(t
->max_segments
, b
->max_segments
);
674 t
->max_discard_segments
= min_not_zero(t
->max_discard_segments
,
675 b
->max_discard_segments
);
676 t
->max_integrity_segments
= min_not_zero(t
->max_integrity_segments
,
677 b
->max_integrity_segments
);
679 t
->max_segment_size
= min_not_zero(t
->max_segment_size
,
680 b
->max_segment_size
);
682 alignment
= queue_limit_alignment_offset(b
, start
);
684 /* Bottom device has different alignment. Check that it is
685 * compatible with the current top alignment.
687 if (t
->alignment_offset
!= alignment
) {
689 top
= max(t
->physical_block_size
, t
->io_min
)
690 + t
->alignment_offset
;
691 bottom
= max(b
->physical_block_size
, b
->io_min
) + alignment
;
693 /* Verify that top and bottom intervals line up */
694 if (max(top
, bottom
) % min(top
, bottom
)) {
695 t
->flags
|= BLK_FLAG_MISALIGNED
;
700 t
->logical_block_size
= max(t
->logical_block_size
,
701 b
->logical_block_size
);
703 t
->physical_block_size
= max(t
->physical_block_size
,
704 b
->physical_block_size
);
706 t
->io_min
= max(t
->io_min
, b
->io_min
);
707 t
->io_opt
= lcm_not_zero(t
->io_opt
, b
->io_opt
);
708 t
->dma_alignment
= max(t
->dma_alignment
, b
->dma_alignment
);
710 /* Set non-power-of-2 compatible chunk_sectors boundary */
711 if (b
->chunk_sectors
)
712 t
->chunk_sectors
= gcd(t
->chunk_sectors
, b
->chunk_sectors
);
714 /* Physical block size a multiple of the logical block size? */
715 if (t
->physical_block_size
& (t
->logical_block_size
- 1)) {
716 t
->physical_block_size
= t
->logical_block_size
;
717 t
->flags
|= BLK_FLAG_MISALIGNED
;
721 /* Minimum I/O a multiple of the physical block size? */
722 if (t
->io_min
& (t
->physical_block_size
- 1)) {
723 t
->io_min
= t
->physical_block_size
;
724 t
->flags
|= BLK_FLAG_MISALIGNED
;
728 /* Optimal I/O a multiple of the physical block size? */
729 if (t
->io_opt
& (t
->physical_block_size
- 1)) {
731 t
->flags
|= BLK_FLAG_MISALIGNED
;
735 /* chunk_sectors a multiple of the physical block size? */
736 if ((t
->chunk_sectors
<< 9) & (t
->physical_block_size
- 1)) {
737 t
->chunk_sectors
= 0;
738 t
->flags
|= BLK_FLAG_MISALIGNED
;
742 /* Find lowest common alignment_offset */
743 t
->alignment_offset
= lcm_not_zero(t
->alignment_offset
, alignment
)
744 % max(t
->physical_block_size
, t
->io_min
);
746 /* Verify that new alignment_offset is on a logical block boundary */
747 if (t
->alignment_offset
& (t
->logical_block_size
- 1)) {
748 t
->flags
|= BLK_FLAG_MISALIGNED
;
752 t
->max_sectors
= blk_round_down_sectors(t
->max_sectors
, t
->logical_block_size
);
753 t
->max_hw_sectors
= blk_round_down_sectors(t
->max_hw_sectors
, t
->logical_block_size
);
754 t
->max_dev_sectors
= blk_round_down_sectors(t
->max_dev_sectors
, t
->logical_block_size
);
756 /* Discard alignment and granularity */
757 if (b
->discard_granularity
) {
758 alignment
= queue_limit_discard_alignment(b
, start
);
760 t
->max_discard_sectors
= min_not_zero(t
->max_discard_sectors
,
761 b
->max_discard_sectors
);
762 t
->max_hw_discard_sectors
= min_not_zero(t
->max_hw_discard_sectors
,
763 b
->max_hw_discard_sectors
);
764 t
->discard_granularity
= max(t
->discard_granularity
,
765 b
->discard_granularity
);
766 t
->discard_alignment
= lcm_not_zero(t
->discard_alignment
, alignment
) %
767 t
->discard_granularity
;
769 t
->max_secure_erase_sectors
= min_not_zero(t
->max_secure_erase_sectors
,
770 b
->max_secure_erase_sectors
);
771 t
->zone_write_granularity
= max(t
->zone_write_granularity
,
772 b
->zone_write_granularity
);
773 if (!(t
->features
& BLK_FEAT_ZONED
)) {
774 t
->zone_write_granularity
= 0;
775 t
->max_zone_append_sectors
= 0;
777 blk_stack_atomic_writes_limits(t
, b
);
781 EXPORT_SYMBOL(blk_stack_limits
);
784 * queue_limits_stack_bdev - adjust queue_limits for stacked devices
785 * @t: the stacking driver limits (top device)
786 * @bdev: the underlying block device (bottom)
787 * @offset: offset to beginning of data within component device
788 * @pfx: prefix to use for warnings logged
791 * This function is used by stacking drivers like MD and DM to ensure
792 * that all component devices have compatible block sizes and
793 * alignments. The stacking driver must provide a queue_limits
794 * struct (top) and then iteratively call the stacking function for
795 * all component (bottom) devices. The stacking function will
796 * attempt to combine the values and ensure proper alignment.
798 void queue_limits_stack_bdev(struct queue_limits
*t
, struct block_device
*bdev
,
799 sector_t offset
, const char *pfx
)
801 if (blk_stack_limits(t
, bdev_limits(bdev
),
802 get_start_sect(bdev
) + offset
))
803 pr_notice("%s: Warning: Device %pg is misaligned\n",
806 EXPORT_SYMBOL_GPL(queue_limits_stack_bdev
);
809 * queue_limits_stack_integrity - stack integrity profile
810 * @t: target queue limits
811 * @b: base queue limits
813 * Check if the integrity profile in the @b can be stacked into the
814 * target @t. Stacking is possible if either:
816 * a) does not have any integrity information stacked into it yet
817 * b) the integrity profile in @b is identical to the one in @t
819 * If @b can be stacked into @t, return %true. Else return %false and clear the
820 * integrity information in @t.
822 bool queue_limits_stack_integrity(struct queue_limits
*t
,
823 struct queue_limits
*b
)
825 struct blk_integrity
*ti
= &t
->integrity
;
826 struct blk_integrity
*bi
= &b
->integrity
;
828 if (!IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY
))
831 if (!ti
->tuple_size
) {
832 /* inherit the settings from the first underlying device */
833 if (!(ti
->flags
& BLK_INTEGRITY_STACKED
)) {
834 ti
->flags
= BLK_INTEGRITY_DEVICE_CAPABLE
|
835 (bi
->flags
& BLK_INTEGRITY_REF_TAG
);
836 ti
->csum_type
= bi
->csum_type
;
837 ti
->tuple_size
= bi
->tuple_size
;
838 ti
->pi_offset
= bi
->pi_offset
;
839 ti
->interval_exp
= bi
->interval_exp
;
840 ti
->tag_size
= bi
->tag_size
;
847 if (ti
->tuple_size
!= bi
->tuple_size
)
849 if (ti
->interval_exp
!= bi
->interval_exp
)
851 if (ti
->tag_size
!= bi
->tag_size
)
853 if (ti
->csum_type
!= bi
->csum_type
)
855 if ((ti
->flags
& BLK_INTEGRITY_REF_TAG
) !=
856 (bi
->flags
& BLK_INTEGRITY_REF_TAG
))
860 ti
->flags
|= BLK_INTEGRITY_STACKED
;
864 memset(ti
, 0, sizeof(*ti
));
867 EXPORT_SYMBOL_GPL(queue_limits_stack_integrity
);
870 * blk_set_queue_depth - tell the block layer about the device queue depth
871 * @q: the request queue for the device
872 * @depth: queue depth
875 void blk_set_queue_depth(struct request_queue
*q
, unsigned int depth
)
877 q
->queue_depth
= depth
;
878 rq_qos_queue_depth_changed(q
);
880 EXPORT_SYMBOL(blk_set_queue_depth
);
882 int bdev_alignment_offset(struct block_device
*bdev
)
884 struct request_queue
*q
= bdev_get_queue(bdev
);
886 if (q
->limits
.flags
& BLK_FLAG_MISALIGNED
)
888 if (bdev_is_partition(bdev
))
889 return queue_limit_alignment_offset(&q
->limits
,
890 bdev
->bd_start_sect
);
891 return q
->limits
.alignment_offset
;
893 EXPORT_SYMBOL_GPL(bdev_alignment_offset
);
895 unsigned int bdev_discard_alignment(struct block_device
*bdev
)
897 struct request_queue
*q
= bdev_get_queue(bdev
);
899 if (bdev_is_partition(bdev
))
900 return queue_limit_discard_alignment(&q
->limits
,
901 bdev
->bd_start_sect
);
902 return q
->limits
.discard_alignment
;
904 EXPORT_SYMBOL_GPL(bdev_discard_alignment
);