blk-settings.c 24 KB

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