Back to home page

LXR

 
 

    


0001 /*
0002  * Functions related to setting various queue properties from drivers
0003  */
0004 #include <linux/kernel.h>
0005 #include <linux/module.h>
0006 #include <linux/init.h>
0007 #include <linux/bio.h>
0008 #include <linux/blkdev.h>
0009 #include <linux/bootmem.h>  /* for max_pfn/max_low_pfn */
0010 #include <linux/gcd.h>
0011 #include <linux/lcm.h>
0012 #include <linux/jiffies.h>
0013 #include <linux/gfp.h>
0014 
0015 #include "blk.h"
0016 #include "blk-wbt.h"
0017 
0018 unsigned long blk_max_low_pfn;
0019 EXPORT_SYMBOL(blk_max_low_pfn);
0020 
0021 unsigned long blk_max_pfn;
0022 
0023 /**
0024  * blk_queue_prep_rq - set a prepare_request function for queue
0025  * @q:      queue
0026  * @pfn:    prepare_request function
0027  *
0028  * It's possible for a queue to register a prepare_request callback which
0029  * is invoked before the request is handed to the request_fn. The goal of
0030  * the function is to prepare a request for I/O, it can be used to build a
0031  * cdb from the request data for instance.
0032  *
0033  */
0034 void blk_queue_prep_rq(struct request_queue *q, prep_rq_fn *pfn)
0035 {
0036     q->prep_rq_fn = pfn;
0037 }
0038 EXPORT_SYMBOL(blk_queue_prep_rq);
0039 
0040 /**
0041  * blk_queue_unprep_rq - set an unprepare_request function for queue
0042  * @q:      queue
0043  * @ufn:    unprepare_request function
0044  *
0045  * It's possible for a queue to register an unprepare_request callback
0046  * which is invoked before the request is finally completed. The goal
0047  * of the function is to deallocate any data that was allocated in the
0048  * prepare_request callback.
0049  *
0050  */
0051 void blk_queue_unprep_rq(struct request_queue *q, unprep_rq_fn *ufn)
0052 {
0053     q->unprep_rq_fn = ufn;
0054 }
0055 EXPORT_SYMBOL(blk_queue_unprep_rq);
0056 
0057 void blk_queue_softirq_done(struct request_queue *q, softirq_done_fn *fn)
0058 {
0059     q->softirq_done_fn = fn;
0060 }
0061 EXPORT_SYMBOL(blk_queue_softirq_done);
0062 
0063 void blk_queue_rq_timeout(struct request_queue *q, unsigned int timeout)
0064 {
0065     q->rq_timeout = timeout;
0066 }
0067 EXPORT_SYMBOL_GPL(blk_queue_rq_timeout);
0068 
0069 void blk_queue_rq_timed_out(struct request_queue *q, rq_timed_out_fn *fn)
0070 {
0071     q->rq_timed_out_fn = fn;
0072 }
0073 EXPORT_SYMBOL_GPL(blk_queue_rq_timed_out);
0074 
0075 void blk_queue_lld_busy(struct request_queue *q, lld_busy_fn *fn)
0076 {
0077     q->lld_busy_fn = fn;
0078 }
0079 EXPORT_SYMBOL_GPL(blk_queue_lld_busy);
0080 
0081 /**
0082  * blk_set_default_limits - reset limits to default values
0083  * @lim:  the queue_limits structure to reset
0084  *
0085  * Description:
0086  *   Returns a queue_limit struct to its default state.
0087  */
0088 void blk_set_default_limits(struct queue_limits *lim)
0089 {
0090     lim->max_segments = BLK_MAX_SEGMENTS;
0091     lim->max_integrity_segments = 0;
0092     lim->seg_boundary_mask = BLK_SEG_BOUNDARY_MASK;
0093     lim->virt_boundary_mask = 0;
0094     lim->max_segment_size = BLK_MAX_SEGMENT_SIZE;
0095     lim->max_sectors = lim->max_hw_sectors = BLK_SAFE_MAX_SECTORS;
0096     lim->max_dev_sectors = 0;
0097     lim->chunk_sectors = 0;
0098     lim->max_write_same_sectors = 0;
0099     lim->max_write_zeroes_sectors = 0;
0100     lim->max_discard_sectors = 0;
0101     lim->max_hw_discard_sectors = 0;
0102     lim->discard_granularity = 0;
0103     lim->discard_alignment = 0;
0104     lim->discard_misaligned = 0;
0105     lim->discard_zeroes_data = 0;
0106     lim->logical_block_size = lim->physical_block_size = lim->io_min = 512;
0107     lim->bounce_pfn = (unsigned long)(BLK_BOUNCE_ANY >> PAGE_SHIFT);
0108     lim->alignment_offset = 0;
0109     lim->io_opt = 0;
0110     lim->misaligned = 0;
0111     lim->cluster = 1;
0112     lim->zoned = BLK_ZONED_NONE;
0113 }
0114 EXPORT_SYMBOL(blk_set_default_limits);
0115 
0116 /**
0117  * blk_set_stacking_limits - set default limits for stacking devices
0118  * @lim:  the queue_limits structure to reset
0119  *
0120  * Description:
0121  *   Returns a queue_limit struct to its default state. Should be used
0122  *   by stacking drivers like DM that have no internal limits.
0123  */
0124 void blk_set_stacking_limits(struct queue_limits *lim)
0125 {
0126     blk_set_default_limits(lim);
0127 
0128     /* Inherit limits from component devices */
0129     lim->discard_zeroes_data = 1;
0130     lim->max_segments = USHRT_MAX;
0131     lim->max_hw_sectors = UINT_MAX;
0132     lim->max_segment_size = UINT_MAX;
0133     lim->max_sectors = UINT_MAX;
0134     lim->max_dev_sectors = UINT_MAX;
0135     lim->max_write_same_sectors = UINT_MAX;
0136     lim->max_write_zeroes_sectors = UINT_MAX;
0137 }
0138 EXPORT_SYMBOL(blk_set_stacking_limits);
0139 
0140 /**
0141  * blk_queue_make_request - define an alternate make_request function for a device
0142  * @q:  the request queue for the device to be affected
0143  * @mfn: the alternate make_request function
0144  *
0145  * Description:
0146  *    The normal way for &struct bios to be passed to a device
0147  *    driver is for them to be collected into requests on a request
0148  *    queue, and then to allow the device driver to select requests
0149  *    off that queue when it is ready.  This works well for many block
0150  *    devices. However some block devices (typically virtual devices
0151  *    such as md or lvm) do not benefit from the processing on the
0152  *    request queue, and are served best by having the requests passed
0153  *    directly to them.  This can be achieved by providing a function
0154  *    to blk_queue_make_request().
0155  *
0156  * Caveat:
0157  *    The driver that does this *must* be able to deal appropriately
0158  *    with buffers in "highmemory". This can be accomplished by either calling
0159  *    __bio_kmap_atomic() to get a temporary kernel mapping, or by calling
0160  *    blk_queue_bounce() to create a buffer in normal memory.
0161  **/
0162 void blk_queue_make_request(struct request_queue *q, make_request_fn *mfn)
0163 {
0164     /*
0165      * set defaults
0166      */
0167     q->nr_requests = BLKDEV_MAX_RQ;
0168 
0169     q->make_request_fn = mfn;
0170     blk_queue_dma_alignment(q, 511);
0171     blk_queue_congestion_threshold(q);
0172     q->nr_batching = BLK_BATCH_REQ;
0173 
0174     blk_set_default_limits(&q->limits);
0175 
0176     /*
0177      * by default assume old behaviour and bounce for any highmem page
0178      */
0179     blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
0180 }
0181 EXPORT_SYMBOL(blk_queue_make_request);
0182 
0183 /**
0184  * blk_queue_bounce_limit - set bounce buffer limit for queue
0185  * @q: the request queue for the device
0186  * @max_addr: the maximum address the device can handle
0187  *
0188  * Description:
0189  *    Different hardware can have different requirements as to what pages
0190  *    it can do I/O directly to. A low level driver can call
0191  *    blk_queue_bounce_limit to have lower memory pages allocated as bounce
0192  *    buffers for doing I/O to pages residing above @max_addr.
0193  **/
0194 void blk_queue_bounce_limit(struct request_queue *q, u64 max_addr)
0195 {
0196     unsigned long b_pfn = max_addr >> PAGE_SHIFT;
0197     int dma = 0;
0198 
0199     q->bounce_gfp = GFP_NOIO;
0200 #if BITS_PER_LONG == 64
0201     /*
0202      * Assume anything <= 4GB can be handled by IOMMU.  Actually
0203      * some IOMMUs can handle everything, but I don't know of a
0204      * way to test this here.
0205      */
0206     if (b_pfn < (min_t(u64, 0xffffffffUL, BLK_BOUNCE_HIGH) >> PAGE_SHIFT))
0207         dma = 1;
0208     q->limits.bounce_pfn = max(max_low_pfn, b_pfn);
0209 #else
0210     if (b_pfn < blk_max_low_pfn)
0211         dma = 1;
0212     q->limits.bounce_pfn = b_pfn;
0213 #endif
0214     if (dma) {
0215         init_emergency_isa_pool();
0216         q->bounce_gfp = GFP_NOIO | GFP_DMA;
0217         q->limits.bounce_pfn = b_pfn;
0218     }
0219 }
0220 EXPORT_SYMBOL(blk_queue_bounce_limit);
0221 
0222 /**
0223  * blk_queue_max_hw_sectors - set max sectors for a request for this queue
0224  * @q:  the request queue for the device
0225  * @max_hw_sectors:  max hardware sectors in the usual 512b unit
0226  *
0227  * Description:
0228  *    Enables a low level driver to set a hard upper limit,
0229  *    max_hw_sectors, on the size of requests.  max_hw_sectors is set by
0230  *    the device driver based upon the capabilities of the I/O
0231  *    controller.
0232  *
0233  *    max_dev_sectors is a hard limit imposed by the storage device for
0234  *    READ/WRITE requests. It is set by the disk driver.
0235  *
0236  *    max_sectors is a soft limit imposed by the block layer for
0237  *    filesystem type requests.  This value can be overridden on a
0238  *    per-device basis in /sys/block/<device>/queue/max_sectors_kb.
0239  *    The soft limit can not exceed max_hw_sectors.
0240  **/
0241 void blk_queue_max_hw_sectors(struct request_queue *q, unsigned int max_hw_sectors)
0242 {
0243     struct queue_limits *limits = &q->limits;
0244     unsigned int max_sectors;
0245 
0246     if ((max_hw_sectors << 9) < PAGE_SIZE) {
0247         max_hw_sectors = 1 << (PAGE_SHIFT - 9);
0248         printk(KERN_INFO "%s: set to minimum %d\n",
0249                __func__, max_hw_sectors);
0250     }
0251 
0252     limits->max_hw_sectors = max_hw_sectors;
0253     max_sectors = min_not_zero(max_hw_sectors, limits->max_dev_sectors);
0254     max_sectors = min_t(unsigned int, max_sectors, BLK_DEF_MAX_SECTORS);
0255     limits->max_sectors = max_sectors;
0256     q->backing_dev_info.io_pages = max_sectors >> (PAGE_SHIFT - 9);
0257 }
0258 EXPORT_SYMBOL(blk_queue_max_hw_sectors);
0259 
0260 /**
0261  * blk_queue_chunk_sectors - set size of the chunk for this queue
0262  * @q:  the request queue for the device
0263  * @chunk_sectors:  chunk sectors in the usual 512b unit
0264  *
0265  * Description:
0266  *    If a driver doesn't want IOs to cross a given chunk size, it can set
0267  *    this limit and prevent merging across chunks. Note that the chunk size
0268  *    must currently be a power-of-2 in sectors. Also note that the block
0269  *    layer must accept a page worth of data at any offset. So if the
0270  *    crossing of chunks is a hard limitation in the driver, it must still be
0271  *    prepared to split single page bios.
0272  **/
0273 void blk_queue_chunk_sectors(struct request_queue *q, unsigned int chunk_sectors)
0274 {
0275     BUG_ON(!is_power_of_2(chunk_sectors));
0276     q->limits.chunk_sectors = chunk_sectors;
0277 }
0278 EXPORT_SYMBOL(blk_queue_chunk_sectors);
0279 
0280 /**
0281  * blk_queue_max_discard_sectors - set max sectors for a single discard
0282  * @q:  the request queue for the device
0283  * @max_discard_sectors: maximum number of sectors to discard
0284  **/
0285 void blk_queue_max_discard_sectors(struct request_queue *q,
0286         unsigned int max_discard_sectors)
0287 {
0288     q->limits.max_hw_discard_sectors = max_discard_sectors;
0289     q->limits.max_discard_sectors = max_discard_sectors;
0290 }
0291 EXPORT_SYMBOL(blk_queue_max_discard_sectors);
0292 
0293 /**
0294  * blk_queue_max_write_same_sectors - set max sectors for a single write same
0295  * @q:  the request queue for the device
0296  * @max_write_same_sectors: maximum number of sectors to write per command
0297  **/
0298 void blk_queue_max_write_same_sectors(struct request_queue *q,
0299                       unsigned int max_write_same_sectors)
0300 {
0301     q->limits.max_write_same_sectors = max_write_same_sectors;
0302 }
0303 EXPORT_SYMBOL(blk_queue_max_write_same_sectors);
0304 
0305 /**
0306  * blk_queue_max_write_zeroes_sectors - set max sectors for a single
0307  *                                      write zeroes
0308  * @q:  the request queue for the device
0309  * @max_write_zeroes_sectors: maximum number of sectors to write per command
0310  **/
0311 void blk_queue_max_write_zeroes_sectors(struct request_queue *q,
0312         unsigned int max_write_zeroes_sectors)
0313 {
0314     q->limits.max_write_zeroes_sectors = max_write_zeroes_sectors;
0315 }
0316 EXPORT_SYMBOL(blk_queue_max_write_zeroes_sectors);
0317 
0318 /**
0319  * blk_queue_max_segments - set max hw segments for a request for this queue
0320  * @q:  the request queue for the device
0321  * @max_segments:  max number of segments
0322  *
0323  * Description:
0324  *    Enables a low level driver to set an upper limit on the number of
0325  *    hw data segments in a request.
0326  **/
0327 void blk_queue_max_segments(struct request_queue *q, unsigned short max_segments)
0328 {
0329     if (!max_segments) {
0330         max_segments = 1;
0331         printk(KERN_INFO "%s: set to minimum %d\n",
0332                __func__, max_segments);
0333     }
0334 
0335     q->limits.max_segments = max_segments;
0336 }
0337 EXPORT_SYMBOL(blk_queue_max_segments);
0338 
0339 /**
0340  * blk_queue_max_segment_size - set max segment size for blk_rq_map_sg
0341  * @q:  the request queue for the device
0342  * @max_size:  max size of segment in bytes
0343  *
0344  * Description:
0345  *    Enables a low level driver to set an upper limit on the size of a
0346  *    coalesced segment
0347  **/
0348 void blk_queue_max_segment_size(struct request_queue *q, unsigned int max_size)
0349 {
0350     if (max_size < PAGE_SIZE) {
0351         max_size = PAGE_SIZE;
0352         printk(KERN_INFO "%s: set to minimum %d\n",
0353                __func__, max_size);
0354     }
0355 
0356     q->limits.max_segment_size = max_size;
0357 }
0358 EXPORT_SYMBOL(blk_queue_max_segment_size);
0359 
0360 /**
0361  * blk_queue_logical_block_size - set logical block size for the queue
0362  * @q:  the request queue for the device
0363  * @size:  the logical block size, in bytes
0364  *
0365  * Description:
0366  *   This should be set to the lowest possible block size that the
0367  *   storage device can address.  The default of 512 covers most
0368  *   hardware.
0369  **/
0370 void blk_queue_logical_block_size(struct request_queue *q, unsigned short size)
0371 {
0372     q->limits.logical_block_size = size;
0373 
0374     if (q->limits.physical_block_size < size)
0375         q->limits.physical_block_size = size;
0376 
0377     if (q->limits.io_min < q->limits.physical_block_size)
0378         q->limits.io_min = q->limits.physical_block_size;
0379 }
0380 EXPORT_SYMBOL(blk_queue_logical_block_size);
0381 
0382 /**
0383  * blk_queue_physical_block_size - set physical block size for the queue
0384  * @q:  the request queue for the device
0385  * @size:  the physical block size, in bytes
0386  *
0387  * Description:
0388  *   This should be set to the lowest possible sector size that the
0389  *   hardware can operate on without reverting to read-modify-write
0390  *   operations.
0391  */
0392 void blk_queue_physical_block_size(struct request_queue *q, unsigned int size)
0393 {
0394     q->limits.physical_block_size = size;
0395 
0396     if (q->limits.physical_block_size < q->limits.logical_block_size)
0397         q->limits.physical_block_size = q->limits.logical_block_size;
0398 
0399     if (q->limits.io_min < q->limits.physical_block_size)
0400         q->limits.io_min = q->limits.physical_block_size;
0401 }
0402 EXPORT_SYMBOL(blk_queue_physical_block_size);
0403 
0404 /**
0405  * blk_queue_alignment_offset - set physical block alignment offset
0406  * @q:  the request queue for the device
0407  * @offset: alignment offset in bytes
0408  *
0409  * Description:
0410  *   Some devices are naturally misaligned to compensate for things like
0411  *   the legacy DOS partition table 63-sector offset.  Low-level drivers
0412  *   should call this function for devices whose first sector is not
0413  *   naturally aligned.
0414  */
0415 void blk_queue_alignment_offset(struct request_queue *q, unsigned int offset)
0416 {
0417     q->limits.alignment_offset =
0418         offset & (q->limits.physical_block_size - 1);
0419     q->limits.misaligned = 0;
0420 }
0421 EXPORT_SYMBOL(blk_queue_alignment_offset);
0422 
0423 /**
0424  * blk_limits_io_min - set minimum request size for a device
0425  * @limits: the queue limits
0426  * @min:  smallest I/O size in bytes
0427  *
0428  * Description:
0429  *   Some devices have an internal block size bigger than the reported
0430  *   hardware sector size.  This function can be used to signal the
0431  *   smallest I/O the device can perform without incurring a performance
0432  *   penalty.
0433  */
0434 void blk_limits_io_min(struct queue_limits *limits, unsigned int min)
0435 {
0436     limits->io_min = min;
0437 
0438     if (limits->io_min < limits->logical_block_size)
0439         limits->io_min = limits->logical_block_size;
0440 
0441     if (limits->io_min < limits->physical_block_size)
0442         limits->io_min = limits->physical_block_size;
0443 }
0444 EXPORT_SYMBOL(blk_limits_io_min);
0445 
0446 /**
0447  * blk_queue_io_min - set minimum request size for the queue
0448  * @q:  the request queue for the device
0449  * @min:  smallest I/O size in bytes
0450  *
0451  * Description:
0452  *   Storage devices may report a granularity or preferred minimum I/O
0453  *   size which is the smallest request the device can perform without
0454  *   incurring a performance penalty.  For disk drives this is often the
0455  *   physical block size.  For RAID arrays it is often the stripe chunk
0456  *   size.  A properly aligned multiple of minimum_io_size is the
0457  *   preferred request size for workloads where a high number of I/O
0458  *   operations is desired.
0459  */
0460 void blk_queue_io_min(struct request_queue *q, unsigned int min)
0461 {
0462     blk_limits_io_min(&q->limits, min);
0463 }
0464 EXPORT_SYMBOL(blk_queue_io_min);
0465 
0466 /**
0467  * blk_limits_io_opt - set optimal request size for a device
0468  * @limits: the queue limits
0469  * @opt:  smallest I/O size in bytes
0470  *
0471  * Description:
0472  *   Storage devices may report an optimal I/O size, which is the
0473  *   device's preferred unit for sustained I/O.  This is rarely reported
0474  *   for disk drives.  For RAID arrays it is usually the stripe width or
0475  *   the internal track size.  A properly aligned multiple of
0476  *   optimal_io_size is the preferred request size for workloads where
0477  *   sustained throughput is desired.
0478  */
0479 void blk_limits_io_opt(struct queue_limits *limits, unsigned int opt)
0480 {
0481     limits->io_opt = opt;
0482 }
0483 EXPORT_SYMBOL(blk_limits_io_opt);
0484 
0485 /**
0486  * blk_queue_io_opt - set optimal request size for the queue
0487  * @q:  the request queue for the device
0488  * @opt:  optimal request size in bytes
0489  *
0490  * Description:
0491  *   Storage devices may report an optimal I/O size, which is the
0492  *   device's preferred unit for sustained I/O.  This is rarely reported
0493  *   for disk drives.  For RAID arrays it is usually the stripe width or
0494  *   the internal track size.  A properly aligned multiple of
0495  *   optimal_io_size is the preferred request size for workloads where
0496  *   sustained throughput is desired.
0497  */
0498 void blk_queue_io_opt(struct request_queue *q, unsigned int opt)
0499 {
0500     blk_limits_io_opt(&q->limits, opt);
0501 }
0502 EXPORT_SYMBOL(blk_queue_io_opt);
0503 
0504 /**
0505  * blk_queue_stack_limits - inherit underlying queue limits for stacked drivers
0506  * @t:  the stacking driver (top)
0507  * @b:  the underlying device (bottom)
0508  **/
0509 void blk_queue_stack_limits(struct request_queue *t, struct request_queue *b)
0510 {
0511     blk_stack_limits(&t->limits, &b->limits, 0);
0512 }
0513 EXPORT_SYMBOL(blk_queue_stack_limits);
0514 
0515 /**
0516  * blk_stack_limits - adjust queue_limits for stacked devices
0517  * @t:  the stacking driver limits (top device)
0518  * @b:  the underlying queue limits (bottom, component device)
0519  * @start:  first data sector within component device
0520  *
0521  * Description:
0522  *    This function is used by stacking drivers like MD and DM to ensure
0523  *    that all component devices have compatible block sizes and
0524  *    alignments.  The stacking driver must provide a queue_limits
0525  *    struct (top) and then iteratively call the stacking function for
0526  *    all component (bottom) devices.  The stacking function will
0527  *    attempt to combine the values and ensure proper alignment.
0528  *
0529  *    Returns 0 if the top and bottom queue_limits are compatible.  The
0530  *    top device's block sizes and alignment offsets may be adjusted to
0531  *    ensure alignment with the bottom device. If no compatible sizes
0532  *    and alignments exist, -1 is returned and the resulting top
0533  *    queue_limits will have the misaligned flag set to indicate that
0534  *    the alignment_offset is undefined.
0535  */
0536 int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
0537              sector_t start)
0538 {
0539     unsigned int top, bottom, alignment, ret = 0;
0540 
0541     t->max_sectors = min_not_zero(t->max_sectors, b->max_sectors);
0542     t->max_hw_sectors = min_not_zero(t->max_hw_sectors, b->max_hw_sectors);
0543     t->max_dev_sectors = min_not_zero(t->max_dev_sectors, b->max_dev_sectors);
0544     t->max_write_same_sectors = min(t->max_write_same_sectors,
0545                     b->max_write_same_sectors);
0546     t->max_write_zeroes_sectors = min(t->max_write_zeroes_sectors,
0547                     b->max_write_zeroes_sectors);
0548     t->bounce_pfn = min_not_zero(t->bounce_pfn, b->bounce_pfn);
0549 
0550     t->seg_boundary_mask = min_not_zero(t->seg_boundary_mask,
0551                         b->seg_boundary_mask);
0552     t->virt_boundary_mask = min_not_zero(t->virt_boundary_mask,
0553                         b->virt_boundary_mask);
0554 
0555     t->max_segments = min_not_zero(t->max_segments, b->max_segments);
0556     t->max_integrity_segments = min_not_zero(t->max_integrity_segments,
0557                          b->max_integrity_segments);
0558 
0559     t->max_segment_size = min_not_zero(t->max_segment_size,
0560                        b->max_segment_size);
0561 
0562     t->misaligned |= b->misaligned;
0563 
0564     alignment = queue_limit_alignment_offset(b, start);
0565 
0566     /* Bottom device has different alignment.  Check that it is
0567      * compatible with the current top alignment.
0568      */
0569     if (t->alignment_offset != alignment) {
0570 
0571         top = max(t->physical_block_size, t->io_min)
0572             + t->alignment_offset;
0573         bottom = max(b->physical_block_size, b->io_min) + alignment;
0574 
0575         /* Verify that top and bottom intervals line up */
0576         if (max(top, bottom) % min(top, bottom)) {
0577             t->misaligned = 1;
0578             ret = -1;
0579         }
0580     }
0581 
0582     t->logical_block_size = max(t->logical_block_size,
0583                     b->logical_block_size);
0584 
0585     t->physical_block_size = max(t->physical_block_size,
0586                      b->physical_block_size);
0587 
0588     t->io_min = max(t->io_min, b->io_min);
0589     t->io_opt = lcm_not_zero(t->io_opt, b->io_opt);
0590 
0591     t->cluster &= b->cluster;
0592     t->discard_zeroes_data &= b->discard_zeroes_data;
0593 
0594     /* Physical block size a multiple of the logical block size? */
0595     if (t->physical_block_size & (t->logical_block_size - 1)) {
0596         t->physical_block_size = t->logical_block_size;
0597         t->misaligned = 1;
0598         ret = -1;
0599     }
0600 
0601     /* Minimum I/O a multiple of the physical block size? */
0602     if (t->io_min & (t->physical_block_size - 1)) {
0603         t->io_min = t->physical_block_size;
0604         t->misaligned = 1;
0605         ret = -1;
0606     }
0607 
0608     /* Optimal I/O a multiple of the physical block size? */
0609     if (t->io_opt & (t->physical_block_size - 1)) {
0610         t->io_opt = 0;
0611         t->misaligned = 1;
0612         ret = -1;
0613     }
0614 
0615     t->raid_partial_stripes_expensive =
0616         max(t->raid_partial_stripes_expensive,
0617             b->raid_partial_stripes_expensive);
0618 
0619     /* Find lowest common alignment_offset */
0620     t->alignment_offset = lcm_not_zero(t->alignment_offset, alignment)
0621         % max(t->physical_block_size, t->io_min);
0622 
0623     /* Verify that new alignment_offset is on a logical block boundary */
0624     if (t->alignment_offset & (t->logical_block_size - 1)) {
0625         t->misaligned = 1;
0626         ret = -1;
0627     }
0628 
0629     /* Discard alignment and granularity */
0630     if (b->discard_granularity) {
0631         alignment = queue_limit_discard_alignment(b, start);
0632 
0633         if (t->discard_granularity != 0 &&
0634             t->discard_alignment != alignment) {
0635             top = t->discard_granularity + t->discard_alignment;
0636             bottom = b->discard_granularity + alignment;
0637 
0638             /* Verify that top and bottom intervals line up */
0639             if ((max(top, bottom) % min(top, bottom)) != 0)
0640                 t->discard_misaligned = 1;
0641         }
0642 
0643         t->max_discard_sectors = min_not_zero(t->max_discard_sectors,
0644                               b->max_discard_sectors);
0645         t->max_hw_discard_sectors = min_not_zero(t->max_hw_discard_sectors,
0646                              b->max_hw_discard_sectors);
0647         t->discard_granularity = max(t->discard_granularity,
0648                          b->discard_granularity);
0649         t->discard_alignment = lcm_not_zero(t->discard_alignment, alignment) %
0650             t->discard_granularity;
0651     }
0652 
0653     if (b->chunk_sectors)
0654         t->chunk_sectors = min_not_zero(t->chunk_sectors,
0655                         b->chunk_sectors);
0656 
0657     return ret;
0658 }
0659 EXPORT_SYMBOL(blk_stack_limits);
0660 
0661 /**
0662  * bdev_stack_limits - adjust queue limits for stacked drivers
0663  * @t:  the stacking driver limits (top device)
0664  * @bdev:  the component block_device (bottom)
0665  * @start:  first data sector within component device
0666  *
0667  * Description:
0668  *    Merges queue limits for a top device and a block_device.  Returns
0669  *    0 if alignment didn't change.  Returns -1 if adding the bottom
0670  *    device caused misalignment.
0671  */
0672 int bdev_stack_limits(struct queue_limits *t, struct block_device *bdev,
0673               sector_t start)
0674 {
0675     struct request_queue *bq = bdev_get_queue(bdev);
0676 
0677     start += get_start_sect(bdev);
0678 
0679     return blk_stack_limits(t, &bq->limits, start);
0680 }
0681 EXPORT_SYMBOL(bdev_stack_limits);
0682 
0683 /**
0684  * disk_stack_limits - adjust queue limits for stacked drivers
0685  * @disk:  MD/DM gendisk (top)
0686  * @bdev:  the underlying block device (bottom)
0687  * @offset:  offset to beginning of data within component device
0688  *
0689  * Description:
0690  *    Merges the limits for a top level gendisk and a bottom level
0691  *    block_device.
0692  */
0693 void disk_stack_limits(struct gendisk *disk, struct block_device *bdev,
0694                sector_t offset)
0695 {
0696     struct request_queue *t = disk->queue;
0697 
0698     if (bdev_stack_limits(&t->limits, bdev, offset >> 9) < 0) {
0699         char top[BDEVNAME_SIZE], bottom[BDEVNAME_SIZE];
0700 
0701         disk_name(disk, 0, top);
0702         bdevname(bdev, bottom);
0703 
0704         printk(KERN_NOTICE "%s: Warning: Device %s is misaligned\n",
0705                top, bottom);
0706     }
0707 }
0708 EXPORT_SYMBOL(disk_stack_limits);
0709 
0710 /**
0711  * blk_queue_dma_pad - set pad mask
0712  * @q:     the request queue for the device
0713  * @mask:  pad mask
0714  *
0715  * Set 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_dma_pad(struct request_queue *q, unsigned int mask)
0721 {
0722     q->dma_pad_mask = mask;
0723 }
0724 EXPORT_SYMBOL(blk_queue_dma_pad);
0725 
0726 /**
0727  * blk_queue_update_dma_pad - update pad mask
0728  * @q:     the request queue for the device
0729  * @mask:  pad mask
0730  *
0731  * Update dma pad mask.
0732  *
0733  * Appending pad buffer to a request modifies the last entry of a
0734  * scatter list such that it includes the pad buffer.
0735  **/
0736 void blk_queue_update_dma_pad(struct request_queue *q, unsigned int mask)
0737 {
0738     if (mask > q->dma_pad_mask)
0739         q->dma_pad_mask = mask;
0740 }
0741 EXPORT_SYMBOL(blk_queue_update_dma_pad);
0742 
0743 /**
0744  * blk_queue_dma_drain - Set up a drain buffer for excess dma.
0745  * @q:  the request queue for the device
0746  * @dma_drain_needed: fn which returns non-zero if drain is necessary
0747  * @buf:    physically contiguous buffer
0748  * @size:   size of the buffer in bytes
0749  *
0750  * Some devices have excess DMA problems and can't simply discard (or
0751  * zero fill) the unwanted piece of the transfer.  They have to have a
0752  * real area of memory to transfer it into.  The use case for this is
0753  * ATAPI devices in DMA mode.  If the packet command causes a transfer
0754  * bigger than the transfer size some HBAs will lock up if there
0755  * aren't DMA elements to contain the excess transfer.  What this API
0756  * does is adjust the queue so that the buf is always appended
0757  * silently to the scatterlist.
0758  *
0759  * Note: This routine adjusts max_hw_segments to make room for appending
0760  * the drain buffer.  If you call blk_queue_max_segments() after calling
0761  * this routine, you must set the limit to one fewer than your device
0762  * can support otherwise there won't be room for the drain buffer.
0763  */
0764 int blk_queue_dma_drain(struct request_queue *q,
0765                    dma_drain_needed_fn *dma_drain_needed,
0766                    void *buf, unsigned int size)
0767 {
0768     if (queue_max_segments(q) < 2)
0769         return -EINVAL;
0770     /* make room for appending the drain */
0771     blk_queue_max_segments(q, queue_max_segments(q) - 1);
0772     q->dma_drain_needed = dma_drain_needed;
0773     q->dma_drain_buffer = buf;
0774     q->dma_drain_size = size;
0775 
0776     return 0;
0777 }
0778 EXPORT_SYMBOL_GPL(blk_queue_dma_drain);
0779 
0780 /**
0781  * blk_queue_segment_boundary - set boundary rules for segment merging
0782  * @q:  the request queue for the device
0783  * @mask:  the memory boundary mask
0784  **/
0785 void blk_queue_segment_boundary(struct request_queue *q, unsigned long mask)
0786 {
0787     if (mask < PAGE_SIZE - 1) {
0788         mask = PAGE_SIZE - 1;
0789         printk(KERN_INFO "%s: set to minimum %lx\n",
0790                __func__, mask);
0791     }
0792 
0793     q->limits.seg_boundary_mask = mask;
0794 }
0795 EXPORT_SYMBOL(blk_queue_segment_boundary);
0796 
0797 /**
0798  * blk_queue_virt_boundary - set boundary rules for bio merging
0799  * @q:  the request queue for the device
0800  * @mask:  the memory boundary mask
0801  **/
0802 void blk_queue_virt_boundary(struct request_queue *q, unsigned long mask)
0803 {
0804     q->limits.virt_boundary_mask = mask;
0805 }
0806 EXPORT_SYMBOL(blk_queue_virt_boundary);
0807 
0808 /**
0809  * blk_queue_dma_alignment - set dma length and memory alignment
0810  * @q:     the request queue for the device
0811  * @mask:  alignment mask
0812  *
0813  * description:
0814  *    set required memory and length alignment for direct dma transactions.
0815  *    this is used when building direct io requests for the queue.
0816  *
0817  **/
0818 void blk_queue_dma_alignment(struct request_queue *q, int mask)
0819 {
0820     q->dma_alignment = mask;
0821 }
0822 EXPORT_SYMBOL(blk_queue_dma_alignment);
0823 
0824 /**
0825  * blk_queue_update_dma_alignment - update dma length and memory alignment
0826  * @q:     the request queue for the device
0827  * @mask:  alignment mask
0828  *
0829  * description:
0830  *    update required memory and length alignment for direct dma transactions.
0831  *    If the requested alignment is larger than the current alignment, then
0832  *    the current queue alignment is updated to the new value, otherwise it
0833  *    is left alone.  The design of this is to allow multiple objects
0834  *    (driver, device, transport etc) to set their respective
0835  *    alignments without having them interfere.
0836  *
0837  **/
0838 void blk_queue_update_dma_alignment(struct request_queue *q, int mask)
0839 {
0840     BUG_ON(mask > PAGE_SIZE);
0841 
0842     if (mask > q->dma_alignment)
0843         q->dma_alignment = mask;
0844 }
0845 EXPORT_SYMBOL(blk_queue_update_dma_alignment);
0846 
0847 void blk_queue_flush_queueable(struct request_queue *q, bool queueable)
0848 {
0849     spin_lock_irq(q->queue_lock);
0850     if (queueable)
0851         clear_bit(QUEUE_FLAG_FLUSH_NQ, &q->queue_flags);
0852     else
0853         set_bit(QUEUE_FLAG_FLUSH_NQ, &q->queue_flags);
0854     spin_unlock_irq(q->queue_lock);
0855 }
0856 EXPORT_SYMBOL_GPL(blk_queue_flush_queueable);
0857 
0858 /**
0859  * blk_set_queue_depth - tell the block layer about the device queue depth
0860  * @q:      the request queue for the device
0861  * @depth:      queue depth
0862  *
0863  */
0864 void blk_set_queue_depth(struct request_queue *q, unsigned int depth)
0865 {
0866     q->queue_depth = depth;
0867     wbt_set_queue_depth(q->rq_wb, depth);
0868 }
0869 EXPORT_SYMBOL(blk_set_queue_depth);
0870 
0871 /**
0872  * blk_queue_write_cache - configure queue's write cache
0873  * @q:      the request queue for the device
0874  * @wc:     write back cache on or off
0875  * @fua:    device supports FUA writes, if true
0876  *
0877  * Tell the block layer about the write cache of @q.
0878  */
0879 void blk_queue_write_cache(struct request_queue *q, bool wc, bool fua)
0880 {
0881     spin_lock_irq(q->queue_lock);
0882     if (wc)
0883         queue_flag_set(QUEUE_FLAG_WC, q);
0884     else
0885         queue_flag_clear(QUEUE_FLAG_WC, q);
0886     if (fua)
0887         queue_flag_set(QUEUE_FLAG_FUA, q);
0888     else
0889         queue_flag_clear(QUEUE_FLAG_FUA, q);
0890     spin_unlock_irq(q->queue_lock);
0891 
0892     wbt_set_write_cache(q->rq_wb, test_bit(QUEUE_FLAG_WC, &q->queue_flags));
0893 }
0894 EXPORT_SYMBOL_GPL(blk_queue_write_cache);
0895 
0896 static int __init blk_settings_init(void)
0897 {
0898     blk_max_low_pfn = max_low_pfn - 1;
0899     blk_max_pfn = max_pfn - 1;
0900     return 0;
0901 }
0902 subsys_initcall(blk_settings_init);