Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Functions related to setting various queue properties from drivers
0004  */
0005 #include <linux/kernel.h>
0006 #include <linux/module.h>
0007 #include <linux/init.h>
0008 #include <linux/bio.h>
0009 #include <linux/blkdev.h>
0010 #include <linux/pagemap.h>
0011 #include <linux/backing-dev-defs.h>
0012 #include <linux/gcd.h>
0013 #include <linux/lcm.h>
0014 #include <linux/jiffies.h>
0015 #include <linux/gfp.h>
0016 #include <linux/dma-mapping.h>
0017 
0018 #include "blk.h"
0019 #include "blk-wbt.h"
0020 
0021 void blk_queue_rq_timeout(struct request_queue *q, unsigned int timeout)
0022 {
0023     q->rq_timeout = timeout;
0024 }
0025 EXPORT_SYMBOL_GPL(blk_queue_rq_timeout);
0026 
0027 /**
0028  * blk_set_default_limits - reset limits to default values
0029  * @lim:  the queue_limits structure to reset
0030  *
0031  * Description:
0032  *   Returns a queue_limit struct to its default state.
0033  */
0034 void blk_set_default_limits(struct queue_limits *lim)
0035 {
0036     lim->max_segments = BLK_MAX_SEGMENTS;
0037     lim->max_discard_segments = 1;
0038     lim->max_integrity_segments = 0;
0039     lim->seg_boundary_mask = BLK_SEG_BOUNDARY_MASK;
0040     lim->virt_boundary_mask = 0;
0041     lim->max_segment_size = BLK_MAX_SEGMENT_SIZE;
0042     lim->max_sectors = lim->max_hw_sectors = BLK_SAFE_MAX_SECTORS;
0043     lim->max_dev_sectors = 0;
0044     lim->chunk_sectors = 0;
0045     lim->max_write_zeroes_sectors = 0;
0046     lim->max_zone_append_sectors = 0;
0047     lim->max_discard_sectors = 0;
0048     lim->max_hw_discard_sectors = 0;
0049     lim->max_secure_erase_sectors = 0;
0050     lim->discard_granularity = 0;
0051     lim->discard_alignment = 0;
0052     lim->discard_misaligned = 0;
0053     lim->logical_block_size = lim->physical_block_size = lim->io_min = 512;
0054     lim->bounce = BLK_BOUNCE_NONE;
0055     lim->alignment_offset = 0;
0056     lim->io_opt = 0;
0057     lim->misaligned = 0;
0058     lim->zoned = BLK_ZONED_NONE;
0059     lim->zone_write_granularity = 0;
0060 }
0061 EXPORT_SYMBOL(blk_set_default_limits);
0062 
0063 /**
0064  * blk_set_stacking_limits - set default limits for stacking devices
0065  * @lim:  the queue_limits structure to reset
0066  *
0067  * Description:
0068  *   Returns a queue_limit struct to its default state. Should be used
0069  *   by stacking drivers like DM that have no internal limits.
0070  */
0071 void blk_set_stacking_limits(struct queue_limits *lim)
0072 {
0073     blk_set_default_limits(lim);
0074 
0075     /* Inherit limits from component devices */
0076     lim->max_segments = USHRT_MAX;
0077     lim->max_discard_segments = USHRT_MAX;
0078     lim->max_hw_sectors = UINT_MAX;
0079     lim->max_segment_size = UINT_MAX;
0080     lim->max_sectors = UINT_MAX;
0081     lim->max_dev_sectors = UINT_MAX;
0082     lim->max_write_zeroes_sectors = UINT_MAX;
0083     lim->max_zone_append_sectors = UINT_MAX;
0084 }
0085 EXPORT_SYMBOL(blk_set_stacking_limits);
0086 
0087 /**
0088  * blk_queue_bounce_limit - set bounce buffer limit for queue
0089  * @q: the request queue for the device
0090  * @bounce: bounce limit to enforce
0091  *
0092  * Description:
0093  *    Force bouncing for ISA DMA ranges or highmem.
0094  *
0095  *    DEPRECATED, don't use in new code.
0096  **/
0097 void blk_queue_bounce_limit(struct request_queue *q, enum blk_bounce bounce)
0098 {
0099     q->limits.bounce = bounce;
0100 }
0101 EXPORT_SYMBOL(blk_queue_bounce_limit);
0102 
0103 /**
0104  * blk_queue_max_hw_sectors - set max sectors for a request for this queue
0105  * @q:  the request queue for the device
0106  * @max_hw_sectors:  max hardware sectors in the usual 512b unit
0107  *
0108  * Description:
0109  *    Enables a low level driver to set a hard upper limit,
0110  *    max_hw_sectors, on the size of requests.  max_hw_sectors is set by
0111  *    the device driver based upon the capabilities of the I/O
0112  *    controller.
0113  *
0114  *    max_dev_sectors is a hard limit imposed by the storage device for
0115  *    READ/WRITE requests. It is set by the disk driver.
0116  *
0117  *    max_sectors is a soft limit imposed by the block layer for
0118  *    filesystem type requests.  This value can be overridden on a
0119  *    per-device basis in /sys/block/<device>/queue/max_sectors_kb.
0120  *    The soft limit can not exceed max_hw_sectors.
0121  **/
0122 void blk_queue_max_hw_sectors(struct request_queue *q, unsigned int max_hw_sectors)
0123 {
0124     struct queue_limits *limits = &q->limits;
0125     unsigned int max_sectors;
0126 
0127     if ((max_hw_sectors << 9) < PAGE_SIZE) {
0128         max_hw_sectors = 1 << (PAGE_SHIFT - 9);
0129         printk(KERN_INFO "%s: set to minimum %d\n",
0130                __func__, max_hw_sectors);
0131     }
0132 
0133     max_hw_sectors = round_down(max_hw_sectors,
0134                     limits->logical_block_size >> SECTOR_SHIFT);
0135     limits->max_hw_sectors = max_hw_sectors;
0136 
0137     max_sectors = min_not_zero(max_hw_sectors, limits->max_dev_sectors);
0138     max_sectors = min_t(unsigned int, max_sectors, BLK_DEF_MAX_SECTORS);
0139     max_sectors = round_down(max_sectors,
0140                  limits->logical_block_size >> SECTOR_SHIFT);
0141     limits->max_sectors = max_sectors;
0142 
0143     if (!q->disk)
0144         return;
0145     q->disk->bdi->io_pages = max_sectors >> (PAGE_SHIFT - 9);
0146 }
0147 EXPORT_SYMBOL(blk_queue_max_hw_sectors);
0148 
0149 /**
0150  * blk_queue_chunk_sectors - set size of the chunk for this queue
0151  * @q:  the request queue for the device
0152  * @chunk_sectors:  chunk sectors in the usual 512b unit
0153  *
0154  * Description:
0155  *    If a driver doesn't want IOs to cross a given chunk size, it can set
0156  *    this limit and prevent merging across chunks. Note that the block layer
0157  *    must accept a page worth of data at any offset. So if the crossing of
0158  *    chunks is a hard limitation in the driver, it must still be prepared
0159  *    to split single page bios.
0160  **/
0161 void blk_queue_chunk_sectors(struct request_queue *q, unsigned int chunk_sectors)
0162 {
0163     q->limits.chunk_sectors = chunk_sectors;
0164 }
0165 EXPORT_SYMBOL(blk_queue_chunk_sectors);
0166 
0167 /**
0168  * blk_queue_max_discard_sectors - set max sectors for a single discard
0169  * @q:  the request queue for the device
0170  * @max_discard_sectors: maximum number of sectors to discard
0171  **/
0172 void blk_queue_max_discard_sectors(struct request_queue *q,
0173         unsigned int max_discard_sectors)
0174 {
0175     q->limits.max_hw_discard_sectors = max_discard_sectors;
0176     q->limits.max_discard_sectors = max_discard_sectors;
0177 }
0178 EXPORT_SYMBOL(blk_queue_max_discard_sectors);
0179 
0180 /**
0181  * blk_queue_max_secure_erase_sectors - set max sectors for a secure erase
0182  * @q:  the request queue for the device
0183  * @max_sectors: maximum number of sectors to secure_erase
0184  **/
0185 void blk_queue_max_secure_erase_sectors(struct request_queue *q,
0186         unsigned int max_sectors)
0187 {
0188     q->limits.max_secure_erase_sectors = max_sectors;
0189 }
0190 EXPORT_SYMBOL(blk_queue_max_secure_erase_sectors);
0191 
0192 /**
0193  * blk_queue_max_write_zeroes_sectors - set max sectors for a single
0194  *                                      write zeroes
0195  * @q:  the request queue for the device
0196  * @max_write_zeroes_sectors: maximum number of sectors to write per command
0197  **/
0198 void blk_queue_max_write_zeroes_sectors(struct request_queue *q,
0199         unsigned int max_write_zeroes_sectors)
0200 {
0201     q->limits.max_write_zeroes_sectors = max_write_zeroes_sectors;
0202 }
0203 EXPORT_SYMBOL(blk_queue_max_write_zeroes_sectors);
0204 
0205 /**
0206  * blk_queue_max_zone_append_sectors - set max sectors for a single zone append
0207  * @q:  the request queue for the device
0208  * @max_zone_append_sectors: maximum number of sectors to write per command
0209  **/
0210 void blk_queue_max_zone_append_sectors(struct request_queue *q,
0211         unsigned int max_zone_append_sectors)
0212 {
0213     unsigned int max_sectors;
0214 
0215     if (WARN_ON(!blk_queue_is_zoned(q)))
0216         return;
0217 
0218     max_sectors = min(q->limits.max_hw_sectors, max_zone_append_sectors);
0219     max_sectors = min(q->limits.chunk_sectors, max_sectors);
0220 
0221     /*
0222      * Signal eventual driver bugs resulting in the max_zone_append sectors limit
0223      * being 0 due to a 0 argument, the chunk_sectors limit (zone size) not set,
0224      * or the max_hw_sectors limit not set.
0225      */
0226     WARN_ON(!max_sectors);
0227 
0228     q->limits.max_zone_append_sectors = max_sectors;
0229 }
0230 EXPORT_SYMBOL_GPL(blk_queue_max_zone_append_sectors);
0231 
0232 /**
0233  * blk_queue_max_segments - set max hw segments for a request for this queue
0234  * @q:  the request queue for the device
0235  * @max_segments:  max number of segments
0236  *
0237  * Description:
0238  *    Enables a low level driver to set an upper limit on the number of
0239  *    hw data segments in a request.
0240  **/
0241 void blk_queue_max_segments(struct request_queue *q, unsigned short max_segments)
0242 {
0243     if (!max_segments) {
0244         max_segments = 1;
0245         printk(KERN_INFO "%s: set to minimum %d\n",
0246                __func__, max_segments);
0247     }
0248 
0249     q->limits.max_segments = max_segments;
0250 }
0251 EXPORT_SYMBOL(blk_queue_max_segments);
0252 
0253 /**
0254  * blk_queue_max_discard_segments - set max segments for discard requests
0255  * @q:  the request queue for the device
0256  * @max_segments:  max number of segments
0257  *
0258  * Description:
0259  *    Enables a low level driver to set an upper limit on the number of
0260  *    segments in a discard request.
0261  **/
0262 void blk_queue_max_discard_segments(struct request_queue *q,
0263         unsigned short max_segments)
0264 {
0265     q->limits.max_discard_segments = max_segments;
0266 }
0267 EXPORT_SYMBOL_GPL(blk_queue_max_discard_segments);
0268 
0269 /**
0270  * blk_queue_max_segment_size - set max segment size for blk_rq_map_sg
0271  * @q:  the request queue for the device
0272  * @max_size:  max size of segment in bytes
0273  *
0274  * Description:
0275  *    Enables a low level driver to set an upper limit on the size of a
0276  *    coalesced segment
0277  **/
0278 void blk_queue_max_segment_size(struct request_queue *q, unsigned int max_size)
0279 {
0280     if (max_size < PAGE_SIZE) {
0281         max_size = PAGE_SIZE;
0282         printk(KERN_INFO "%s: set to minimum %d\n",
0283                __func__, max_size);
0284     }
0285 
0286     /* see blk_queue_virt_boundary() for the explanation */
0287     WARN_ON_ONCE(q->limits.virt_boundary_mask);
0288 
0289     q->limits.max_segment_size = max_size;
0290 }
0291 EXPORT_SYMBOL(blk_queue_max_segment_size);
0292 
0293 /**
0294  * blk_queue_logical_block_size - set logical block size for the queue
0295  * @q:  the request queue for the device
0296  * @size:  the logical block size, in bytes
0297  *
0298  * Description:
0299  *   This should be set to the lowest possible block size that the
0300  *   storage device can address.  The default of 512 covers most
0301  *   hardware.
0302  **/
0303 void blk_queue_logical_block_size(struct request_queue *q, unsigned int size)
0304 {
0305     struct queue_limits *limits = &q->limits;
0306 
0307     limits->logical_block_size = size;
0308 
0309     if (limits->physical_block_size < size)
0310         limits->physical_block_size = size;
0311 
0312     if (limits->io_min < limits->physical_block_size)
0313         limits->io_min = limits->physical_block_size;
0314 
0315     limits->max_hw_sectors =
0316         round_down(limits->max_hw_sectors, size >> SECTOR_SHIFT);
0317     limits->max_sectors =
0318         round_down(limits->max_sectors, size >> SECTOR_SHIFT);
0319 }
0320 EXPORT_SYMBOL(blk_queue_logical_block_size);
0321 
0322 /**
0323  * blk_queue_physical_block_size - set physical block size for the queue
0324  * @q:  the request queue for the device
0325  * @size:  the physical block size, in bytes
0326  *
0327  * Description:
0328  *   This should be set to the lowest possible sector size that the
0329  *   hardware can operate on without reverting to read-modify-write
0330  *   operations.
0331  */
0332 void blk_queue_physical_block_size(struct request_queue *q, unsigned int size)
0333 {
0334     q->limits.physical_block_size = size;
0335 
0336     if (q->limits.physical_block_size < q->limits.logical_block_size)
0337         q->limits.physical_block_size = q->limits.logical_block_size;
0338 
0339     if (q->limits.io_min < q->limits.physical_block_size)
0340         q->limits.io_min = q->limits.physical_block_size;
0341 }
0342 EXPORT_SYMBOL(blk_queue_physical_block_size);
0343 
0344 /**
0345  * blk_queue_zone_write_granularity - set zone write granularity for the queue
0346  * @q:  the request queue for the zoned device
0347  * @size:  the zone write granularity size, in bytes
0348  *
0349  * Description:
0350  *   This should be set to the lowest possible size allowing to write in
0351  *   sequential zones of a zoned block device.
0352  */
0353 void blk_queue_zone_write_granularity(struct request_queue *q,
0354                       unsigned int size)
0355 {
0356     if (WARN_ON_ONCE(!blk_queue_is_zoned(q)))
0357         return;
0358 
0359     q->limits.zone_write_granularity = size;
0360 
0361     if (q->limits.zone_write_granularity < q->limits.logical_block_size)
0362         q->limits.zone_write_granularity = q->limits.logical_block_size;
0363 }
0364 EXPORT_SYMBOL_GPL(blk_queue_zone_write_granularity);
0365 
0366 /**
0367  * blk_queue_alignment_offset - set physical block alignment offset
0368  * @q:  the request queue for the device
0369  * @offset: alignment offset in bytes
0370  *
0371  * Description:
0372  *   Some devices are naturally misaligned to compensate for things like
0373  *   the legacy DOS partition table 63-sector offset.  Low-level drivers
0374  *   should call this function for devices whose first sector is not
0375  *   naturally aligned.
0376  */
0377 void blk_queue_alignment_offset(struct request_queue *q, unsigned int offset)
0378 {
0379     q->limits.alignment_offset =
0380         offset & (q->limits.physical_block_size - 1);
0381     q->limits.misaligned = 0;
0382 }
0383 EXPORT_SYMBOL(blk_queue_alignment_offset);
0384 
0385 void disk_update_readahead(struct gendisk *disk)
0386 {
0387     struct request_queue *q = disk->queue;
0388 
0389     /*
0390      * For read-ahead of large files to be effective, we need to read ahead
0391      * at least twice the optimal I/O size.
0392      */
0393     disk->bdi->ra_pages =
0394         max(queue_io_opt(q) * 2 / PAGE_SIZE, VM_READAHEAD_PAGES);
0395     disk->bdi->io_pages = queue_max_sectors(q) >> (PAGE_SHIFT - 9);
0396 }
0397 EXPORT_SYMBOL_GPL(disk_update_readahead);
0398 
0399 /**
0400  * blk_limits_io_min - set minimum request size for a device
0401  * @limits: the queue limits
0402  * @min:  smallest I/O size in bytes
0403  *
0404  * Description:
0405  *   Some devices have an internal block size bigger than the reported
0406  *   hardware sector size.  This function can be used to signal the
0407  *   smallest I/O the device can perform without incurring a performance
0408  *   penalty.
0409  */
0410 void blk_limits_io_min(struct queue_limits *limits, unsigned int min)
0411 {
0412     limits->io_min = min;
0413 
0414     if (limits->io_min < limits->logical_block_size)
0415         limits->io_min = limits->logical_block_size;
0416 
0417     if (limits->io_min < limits->physical_block_size)
0418         limits->io_min = limits->physical_block_size;
0419 }
0420 EXPORT_SYMBOL(blk_limits_io_min);
0421 
0422 /**
0423  * blk_queue_io_min - set minimum request size for the queue
0424  * @q:  the request queue for the device
0425  * @min:  smallest I/O size in bytes
0426  *
0427  * Description:
0428  *   Storage devices may report a granularity or preferred minimum I/O
0429  *   size which is the smallest request the device can perform without
0430  *   incurring a performance penalty.  For disk drives this is often the
0431  *   physical block size.  For RAID arrays it is often the stripe chunk
0432  *   size.  A properly aligned multiple of minimum_io_size is the
0433  *   preferred request size for workloads where a high number of I/O
0434  *   operations is desired.
0435  */
0436 void blk_queue_io_min(struct request_queue *q, unsigned int min)
0437 {
0438     blk_limits_io_min(&q->limits, min);
0439 }
0440 EXPORT_SYMBOL(blk_queue_io_min);
0441 
0442 /**
0443  * blk_limits_io_opt - set optimal request size for a device
0444  * @limits: the queue limits
0445  * @opt:  smallest I/O size in bytes
0446  *
0447  * Description:
0448  *   Storage devices may report an optimal I/O size, which is the
0449  *   device's preferred unit for sustained I/O.  This is rarely reported
0450  *   for disk drives.  For RAID arrays it is usually the stripe width or
0451  *   the internal track size.  A properly aligned multiple of
0452  *   optimal_io_size is the preferred request size for workloads where
0453  *   sustained throughput is desired.
0454  */
0455 void blk_limits_io_opt(struct queue_limits *limits, unsigned int opt)
0456 {
0457     limits->io_opt = opt;
0458 }
0459 EXPORT_SYMBOL(blk_limits_io_opt);
0460 
0461 /**
0462  * blk_queue_io_opt - set optimal request size for the queue
0463  * @q:  the request queue for the device
0464  * @opt:  optimal request size in bytes
0465  *
0466  * Description:
0467  *   Storage devices may report an optimal I/O size, which is the
0468  *   device's preferred unit for sustained I/O.  This is rarely reported
0469  *   for disk drives.  For RAID arrays it is usually the stripe width or
0470  *   the internal track size.  A properly aligned multiple of
0471  *   optimal_io_size is the preferred request size for workloads where
0472  *   sustained throughput is desired.
0473  */
0474 void blk_queue_io_opt(struct request_queue *q, unsigned int opt)
0475 {
0476     blk_limits_io_opt(&q->limits, opt);
0477     if (!q->disk)
0478         return;
0479     q->disk->bdi->ra_pages =
0480         max(queue_io_opt(q) * 2 / PAGE_SIZE, VM_READAHEAD_PAGES);
0481 }
0482 EXPORT_SYMBOL(blk_queue_io_opt);
0483 
0484 static int queue_limit_alignment_offset(struct queue_limits *lim,
0485         sector_t sector)
0486 {
0487     unsigned int granularity = max(lim->physical_block_size, lim->io_min);
0488     unsigned int alignment = sector_div(sector, granularity >> SECTOR_SHIFT)
0489         << SECTOR_SHIFT;
0490 
0491     return (granularity + lim->alignment_offset - alignment) % granularity;
0492 }
0493 
0494 static unsigned int queue_limit_discard_alignment(struct queue_limits *lim,
0495         sector_t sector)
0496 {
0497     unsigned int alignment, granularity, offset;
0498 
0499     if (!lim->max_discard_sectors)
0500         return 0;
0501 
0502     /* Why are these in bytes, not sectors? */
0503     alignment = lim->discard_alignment >> SECTOR_SHIFT;
0504     granularity = lim->discard_granularity >> SECTOR_SHIFT;
0505     if (!granularity)
0506         return 0;
0507 
0508     /* Offset of the partition start in 'granularity' sectors */
0509     offset = sector_div(sector, granularity);
0510 
0511     /* And why do we do this modulus *again* in blkdev_issue_discard()? */
0512     offset = (granularity + alignment - offset) % granularity;
0513 
0514     /* Turn it back into bytes, gaah */
0515     return offset << SECTOR_SHIFT;
0516 }
0517 
0518 static unsigned int blk_round_down_sectors(unsigned int sectors, unsigned int lbs)
0519 {
0520     sectors = round_down(sectors, lbs >> SECTOR_SHIFT);
0521     if (sectors < PAGE_SIZE >> SECTOR_SHIFT)
0522         sectors = PAGE_SIZE >> SECTOR_SHIFT;
0523     return sectors;
0524 }
0525 
0526 /**
0527  * blk_stack_limits - adjust queue_limits for stacked devices
0528  * @t:  the stacking driver limits (top device)
0529  * @b:  the underlying queue limits (bottom, component device)
0530  * @start:  first data sector within component device
0531  *
0532  * Description:
0533  *    This function is used by stacking drivers like MD and DM to ensure
0534  *    that all component devices have compatible block sizes and
0535  *    alignments.  The stacking driver must provide a queue_limits
0536  *    struct (top) and then iteratively call the stacking function for
0537  *    all component (bottom) devices.  The stacking function will
0538  *    attempt to combine the values and ensure proper alignment.
0539  *
0540  *    Returns 0 if the top and bottom queue_limits are compatible.  The
0541  *    top device's block sizes and alignment offsets may be adjusted to
0542  *    ensure alignment with the bottom device. If no compatible sizes
0543  *    and alignments exist, -1 is returned and the resulting top
0544  *    queue_limits will have the misaligned flag set to indicate that
0545  *    the alignment_offset is undefined.
0546  */
0547 int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
0548              sector_t start)
0549 {
0550     unsigned int top, bottom, alignment, ret = 0;
0551 
0552     t->max_sectors = min_not_zero(t->max_sectors, b->max_sectors);
0553     t->max_hw_sectors = min_not_zero(t->max_hw_sectors, b->max_hw_sectors);
0554     t->max_dev_sectors = min_not_zero(t->max_dev_sectors, b->max_dev_sectors);
0555     t->max_write_zeroes_sectors = min(t->max_write_zeroes_sectors,
0556                     b->max_write_zeroes_sectors);
0557     t->max_zone_append_sectors = min(t->max_zone_append_sectors,
0558                     b->max_zone_append_sectors);
0559     t->bounce = max(t->bounce, b->bounce);
0560 
0561     t->seg_boundary_mask = min_not_zero(t->seg_boundary_mask,
0562                         b->seg_boundary_mask);
0563     t->virt_boundary_mask = min_not_zero(t->virt_boundary_mask,
0564                         b->virt_boundary_mask);
0565 
0566     t->max_segments = min_not_zero(t->max_segments, b->max_segments);
0567     t->max_discard_segments = min_not_zero(t->max_discard_segments,
0568                            b->max_discard_segments);
0569     t->max_integrity_segments = min_not_zero(t->max_integrity_segments,
0570                          b->max_integrity_segments);
0571 
0572     t->max_segment_size = min_not_zero(t->max_segment_size,
0573                        b->max_segment_size);
0574 
0575     t->misaligned |= b->misaligned;
0576 
0577     alignment = queue_limit_alignment_offset(b, start);
0578 
0579     /* Bottom device has different alignment.  Check that it is
0580      * compatible with the current top alignment.
0581      */
0582     if (t->alignment_offset != alignment) {
0583 
0584         top = max(t->physical_block_size, t->io_min)
0585             + t->alignment_offset;
0586         bottom = max(b->physical_block_size, b->io_min) + alignment;
0587 
0588         /* Verify that top and bottom intervals line up */
0589         if (max(top, bottom) % min(top, bottom)) {
0590             t->misaligned = 1;
0591             ret = -1;
0592         }
0593     }
0594 
0595     t->logical_block_size = max(t->logical_block_size,
0596                     b->logical_block_size);
0597 
0598     t->physical_block_size = max(t->physical_block_size,
0599                      b->physical_block_size);
0600 
0601     t->io_min = max(t->io_min, b->io_min);
0602     t->io_opt = lcm_not_zero(t->io_opt, b->io_opt);
0603 
0604     /* Set non-power-of-2 compatible chunk_sectors boundary */
0605     if (b->chunk_sectors)
0606         t->chunk_sectors = gcd(t->chunk_sectors, b->chunk_sectors);
0607 
0608     /* Physical block size a multiple of the logical block size? */
0609     if (t->physical_block_size & (t->logical_block_size - 1)) {
0610         t->physical_block_size = t->logical_block_size;
0611         t->misaligned = 1;
0612         ret = -1;
0613     }
0614 
0615     /* Minimum I/O a multiple of the physical block size? */
0616     if (t->io_min & (t->physical_block_size - 1)) {
0617         t->io_min = t->physical_block_size;
0618         t->misaligned = 1;
0619         ret = -1;
0620     }
0621 
0622     /* Optimal I/O a multiple of the physical block size? */
0623     if (t->io_opt & (t->physical_block_size - 1)) {
0624         t->io_opt = 0;
0625         t->misaligned = 1;
0626         ret = -1;
0627     }
0628 
0629     /* chunk_sectors a multiple of the physical block size? */
0630     if ((t->chunk_sectors << 9) & (t->physical_block_size - 1)) {
0631         t->chunk_sectors = 0;
0632         t->misaligned = 1;
0633         ret = -1;
0634     }
0635 
0636     t->raid_partial_stripes_expensive =
0637         max(t->raid_partial_stripes_expensive,
0638             b->raid_partial_stripes_expensive);
0639 
0640     /* Find lowest common alignment_offset */
0641     t->alignment_offset = lcm_not_zero(t->alignment_offset, alignment)
0642         % max(t->physical_block_size, t->io_min);
0643 
0644     /* Verify that new alignment_offset is on a logical block boundary */
0645     if (t->alignment_offset & (t->logical_block_size - 1)) {
0646         t->misaligned = 1;
0647         ret = -1;
0648     }
0649 
0650     t->max_sectors = blk_round_down_sectors(t->max_sectors, t->logical_block_size);
0651     t->max_hw_sectors = blk_round_down_sectors(t->max_hw_sectors, t->logical_block_size);
0652     t->max_dev_sectors = blk_round_down_sectors(t->max_dev_sectors, t->logical_block_size);
0653 
0654     /* Discard alignment and granularity */
0655     if (b->discard_granularity) {
0656         alignment = queue_limit_discard_alignment(b, start);
0657 
0658         if (t->discard_granularity != 0 &&
0659             t->discard_alignment != alignment) {
0660             top = t->discard_granularity + t->discard_alignment;
0661             bottom = b->discard_granularity + alignment;
0662 
0663             /* Verify that top and bottom intervals line up */
0664             if ((max(top, bottom) % min(top, bottom)) != 0)
0665                 t->discard_misaligned = 1;
0666         }
0667 
0668         t->max_discard_sectors = min_not_zero(t->max_discard_sectors,
0669                               b->max_discard_sectors);
0670         t->max_hw_discard_sectors = min_not_zero(t->max_hw_discard_sectors,
0671                              b->max_hw_discard_sectors);
0672         t->discard_granularity = max(t->discard_granularity,
0673                          b->discard_granularity);
0674         t->discard_alignment = lcm_not_zero(t->discard_alignment, alignment) %
0675             t->discard_granularity;
0676     }
0677     t->max_secure_erase_sectors = min_not_zero(t->max_secure_erase_sectors,
0678                            b->max_secure_erase_sectors);
0679     t->zone_write_granularity = max(t->zone_write_granularity,
0680                     b->zone_write_granularity);
0681     t->zoned = max(t->zoned, b->zoned);
0682     return ret;
0683 }
0684 EXPORT_SYMBOL(blk_stack_limits);
0685 
0686 /**
0687  * disk_stack_limits - adjust queue limits for stacked drivers
0688  * @disk:  MD/DM gendisk (top)
0689  * @bdev:  the underlying block device (bottom)
0690  * @offset:  offset to beginning of data within component device
0691  *
0692  * Description:
0693  *    Merges the limits for a top level gendisk and a bottom level
0694  *    block_device.
0695  */
0696 void disk_stack_limits(struct gendisk *disk, struct block_device *bdev,
0697                sector_t offset)
0698 {
0699     struct request_queue *t = disk->queue;
0700 
0701     if (blk_stack_limits(&t->limits, &bdev_get_queue(bdev)->limits,
0702             get_start_sect(bdev) + (offset >> 9)) < 0)
0703         pr_notice("%s: Warning: Device %pg is misaligned\n",
0704             disk->disk_name, bdev);
0705 
0706     disk_update_readahead(disk);
0707 }
0708 EXPORT_SYMBOL(disk_stack_limits);
0709 
0710 /**
0711  * blk_queue_update_dma_pad - update pad mask
0712  * @q:     the request queue for the device
0713  * @mask:  pad mask
0714  *
0715  * Update dma pad mask.
0716  *
0717  * Appending pad buffer to a request modifies the last entry of a
0718  * scatter list such that it includes the pad buffer.
0719  **/
0720 void blk_queue_update_dma_pad(struct request_queue *q, unsigned int mask)
0721 {
0722     if (mask > q->dma_pad_mask)
0723         q->dma_pad_mask = mask;
0724 }
0725 EXPORT_SYMBOL(blk_queue_update_dma_pad);
0726 
0727 /**
0728  * blk_queue_segment_boundary - set boundary rules for segment merging
0729  * @q:  the request queue for the device
0730  * @mask:  the memory boundary mask
0731  **/
0732 void blk_queue_segment_boundary(struct request_queue *q, unsigned long mask)
0733 {
0734     if (mask < PAGE_SIZE - 1) {
0735         mask = PAGE_SIZE - 1;
0736         printk(KERN_INFO "%s: set to minimum %lx\n",
0737                __func__, mask);
0738     }
0739 
0740     q->limits.seg_boundary_mask = mask;
0741 }
0742 EXPORT_SYMBOL(blk_queue_segment_boundary);
0743 
0744 /**
0745  * blk_queue_virt_boundary - set boundary rules for bio merging
0746  * @q:  the request queue for the device
0747  * @mask:  the memory boundary mask
0748  **/
0749 void blk_queue_virt_boundary(struct request_queue *q, unsigned long mask)
0750 {
0751     q->limits.virt_boundary_mask = mask;
0752 
0753     /*
0754      * Devices that require a virtual boundary do not support scatter/gather
0755      * I/O natively, but instead require a descriptor list entry for each
0756      * page (which might not be idential to the Linux PAGE_SIZE).  Because
0757      * of that they are not limited by our notion of "segment size".
0758      */
0759     if (mask)
0760         q->limits.max_segment_size = UINT_MAX;
0761 }
0762 EXPORT_SYMBOL(blk_queue_virt_boundary);
0763 
0764 /**
0765  * blk_queue_dma_alignment - set dma length and memory alignment
0766  * @q:     the request queue for the device
0767  * @mask:  alignment mask
0768  *
0769  * description:
0770  *    set required memory and length alignment for direct dma transactions.
0771  *    this is used when building direct io requests for the queue.
0772  *
0773  **/
0774 void blk_queue_dma_alignment(struct request_queue *q, int mask)
0775 {
0776     q->dma_alignment = mask;
0777 }
0778 EXPORT_SYMBOL(blk_queue_dma_alignment);
0779 
0780 /**
0781  * blk_queue_update_dma_alignment - update dma length and memory alignment
0782  * @q:     the request queue for the device
0783  * @mask:  alignment mask
0784  *
0785  * description:
0786  *    update required memory and length alignment for direct dma transactions.
0787  *    If the requested alignment is larger than the current alignment, then
0788  *    the current queue alignment is updated to the new value, otherwise it
0789  *    is left alone.  The design of this is to allow multiple objects
0790  *    (driver, device, transport etc) to set their respective
0791  *    alignments without having them interfere.
0792  *
0793  **/
0794 void blk_queue_update_dma_alignment(struct request_queue *q, int mask)
0795 {
0796     BUG_ON(mask > PAGE_SIZE);
0797 
0798     if (mask > q->dma_alignment)
0799         q->dma_alignment = mask;
0800 }
0801 EXPORT_SYMBOL(blk_queue_update_dma_alignment);
0802 
0803 /**
0804  * blk_set_queue_depth - tell the block layer about the device queue depth
0805  * @q:      the request queue for the device
0806  * @depth:      queue depth
0807  *
0808  */
0809 void blk_set_queue_depth(struct request_queue *q, unsigned int depth)
0810 {
0811     q->queue_depth = depth;
0812     rq_qos_queue_depth_changed(q);
0813 }
0814 EXPORT_SYMBOL(blk_set_queue_depth);
0815 
0816 /**
0817  * blk_queue_write_cache - configure queue's write cache
0818  * @q:      the request queue for the device
0819  * @wc:     write back cache on or off
0820  * @fua:    device supports FUA writes, if true
0821  *
0822  * Tell the block layer about the write cache of @q.
0823  */
0824 void blk_queue_write_cache(struct request_queue *q, bool wc, bool fua)
0825 {
0826     if (wc)
0827         blk_queue_flag_set(QUEUE_FLAG_WC, q);
0828     else
0829         blk_queue_flag_clear(QUEUE_FLAG_WC, q);
0830     if (fua)
0831         blk_queue_flag_set(QUEUE_FLAG_FUA, q);
0832     else
0833         blk_queue_flag_clear(QUEUE_FLAG_FUA, q);
0834 
0835     wbt_set_write_cache(q, test_bit(QUEUE_FLAG_WC, &q->queue_flags));
0836 }
0837 EXPORT_SYMBOL_GPL(blk_queue_write_cache);
0838 
0839 /**
0840  * blk_queue_required_elevator_features - Set a queue required elevator features
0841  * @q:      the request queue for the target device
0842  * @features:   Required elevator features OR'ed together
0843  *
0844  * Tell the block layer that for the device controlled through @q, only the
0845  * only elevators that can be used are those that implement at least the set of
0846  * features specified by @features.
0847  */
0848 void blk_queue_required_elevator_features(struct request_queue *q,
0849                       unsigned int features)
0850 {
0851     q->required_elevator_features = features;
0852 }
0853 EXPORT_SYMBOL_GPL(blk_queue_required_elevator_features);
0854 
0855 /**
0856  * blk_queue_can_use_dma_map_merging - configure queue for merging segments.
0857  * @q:      the request queue for the device
0858  * @dev:    the device pointer for dma
0859  *
0860  * Tell the block layer about merging the segments by dma map of @q.
0861  */
0862 bool blk_queue_can_use_dma_map_merging(struct request_queue *q,
0863                        struct device *dev)
0864 {
0865     unsigned long boundary = dma_get_merge_boundary(dev);
0866 
0867     if (!boundary)
0868         return false;
0869 
0870     /* No need to update max_segment_size. see blk_queue_virt_boundary() */
0871     blk_queue_virt_boundary(q, boundary);
0872 
0873     return true;
0874 }
0875 EXPORT_SYMBOL_GPL(blk_queue_can_use_dma_map_merging);
0876 
0877 static bool disk_has_partitions(struct gendisk *disk)
0878 {
0879     unsigned long idx;
0880     struct block_device *part;
0881     bool ret = false;
0882 
0883     rcu_read_lock();
0884     xa_for_each(&disk->part_tbl, idx, part) {
0885         if (bdev_is_partition(part)) {
0886             ret = true;
0887             break;
0888         }
0889     }
0890     rcu_read_unlock();
0891 
0892     return ret;
0893 }
0894 
0895 /**
0896  * disk_set_zoned - configure the zoned model for a disk
0897  * @disk:   the gendisk of the queue to configure
0898  * @model:  the zoned model to set
0899  *
0900  * Set the zoned model of @disk to @model.
0901  *
0902  * When @model is BLK_ZONED_HM (host managed), this should be called only
0903  * if zoned block device support is enabled (CONFIG_BLK_DEV_ZONED option).
0904  * If @model specifies BLK_ZONED_HA (host aware), the effective model used
0905  * depends on CONFIG_BLK_DEV_ZONED settings and on the existence of partitions
0906  * on the disk.
0907  */
0908 void disk_set_zoned(struct gendisk *disk, enum blk_zoned_model model)
0909 {
0910     struct request_queue *q = disk->queue;
0911 
0912     switch (model) {
0913     case BLK_ZONED_HM:
0914         /*
0915          * Host managed devices are supported only if
0916          * CONFIG_BLK_DEV_ZONED is enabled.
0917          */
0918         WARN_ON_ONCE(!IS_ENABLED(CONFIG_BLK_DEV_ZONED));
0919         break;
0920     case BLK_ZONED_HA:
0921         /*
0922          * Host aware devices can be treated either as regular block
0923          * devices (similar to drive managed devices) or as zoned block
0924          * devices to take advantage of the zone command set, similarly
0925          * to host managed devices. We try the latter if there are no
0926          * partitions and zoned block device support is enabled, else
0927          * we do nothing special as far as the block layer is concerned.
0928          */
0929         if (!IS_ENABLED(CONFIG_BLK_DEV_ZONED) ||
0930             disk_has_partitions(disk))
0931             model = BLK_ZONED_NONE;
0932         break;
0933     case BLK_ZONED_NONE:
0934     default:
0935         if (WARN_ON_ONCE(model != BLK_ZONED_NONE))
0936             model = BLK_ZONED_NONE;
0937         break;
0938     }
0939 
0940     q->limits.zoned = model;
0941     if (model != BLK_ZONED_NONE) {
0942         /*
0943          * Set the zone write granularity to the device logical block
0944          * size by default. The driver can change this value if needed.
0945          */
0946         blk_queue_zone_write_granularity(q,
0947                         queue_logical_block_size(q));
0948     } else {
0949         disk_clear_zone_settings(disk);
0950     }
0951 }
0952 EXPORT_SYMBOL_GPL(disk_set_zoned);
0953 
0954 int bdev_alignment_offset(struct block_device *bdev)
0955 {
0956     struct request_queue *q = bdev_get_queue(bdev);
0957 
0958     if (q->limits.misaligned)
0959         return -1;
0960     if (bdev_is_partition(bdev))
0961         return queue_limit_alignment_offset(&q->limits,
0962                 bdev->bd_start_sect);
0963     return q->limits.alignment_offset;
0964 }
0965 EXPORT_SYMBOL_GPL(bdev_alignment_offset);
0966 
0967 unsigned int bdev_discard_alignment(struct block_device *bdev)
0968 {
0969     struct request_queue *q = bdev_get_queue(bdev);
0970 
0971     if (bdev_is_partition(bdev))
0972         return queue_limit_discard_alignment(&q->limits,
0973                 bdev->bd_start_sect);
0974     return q->limits.discard_alignment;
0975 }
0976 EXPORT_SYMBOL_GPL(bdev_discard_alignment);