blk-lib.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Functions related to generic helpers functions
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/module.h>
  7. #include <linux/bio.h>
  8. #include <linux/blkdev.h>
  9. #include <linux/scatterlist.h>
  10. #include "blk.h"
  11. static struct bio *next_bio(struct bio *bio, unsigned int nr_pages,
  12. gfp_t gfp)
  13. {
  14. struct bio *new = bio_alloc(gfp, nr_pages);
  15. if (bio) {
  16. bio_chain(bio, new);
  17. submit_bio(bio);
  18. }
  19. return new;
  20. }
  21. int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
  22. sector_t nr_sects, gfp_t gfp_mask, int flags,
  23. struct bio **biop)
  24. {
  25. struct request_queue *q = bdev_get_queue(bdev);
  26. struct bio *bio = *biop;
  27. unsigned int granularity;
  28. unsigned int op;
  29. int alignment;
  30. sector_t bs_mask;
  31. if (!q)
  32. return -ENXIO;
  33. if (flags & BLKDEV_DISCARD_SECURE) {
  34. if (!blk_queue_secure_erase(q))
  35. return -EOPNOTSUPP;
  36. op = REQ_OP_SECURE_ERASE;
  37. } else {
  38. if (!blk_queue_discard(q))
  39. return -EOPNOTSUPP;
  40. op = REQ_OP_DISCARD;
  41. }
  42. bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1;
  43. if ((sector | nr_sects) & bs_mask)
  44. return -EINVAL;
  45. /* Zero-sector (unknown) and one-sector granularities are the same. */
  46. granularity = max(q->limits.discard_granularity >> 9, 1U);
  47. alignment = (bdev_discard_alignment(bdev) >> 9) % granularity;
  48. while (nr_sects) {
  49. unsigned int req_sects;
  50. sector_t end_sect, tmp;
  51. /*
  52. * Issue in chunks of the user defined max discard setting,
  53. * ensuring that bi_size doesn't overflow
  54. */
  55. req_sects = min_t(sector_t, nr_sects,
  56. q->limits.max_discard_sectors);
  57. if (!req_sects)
  58. goto fail;
  59. if (req_sects > UINT_MAX >> 9)
  60. req_sects = UINT_MAX >> 9;
  61. /*
  62. * If splitting a request, and the next starting sector would be
  63. * misaligned, stop the discard at the previous aligned sector.
  64. */
  65. end_sect = sector + req_sects;
  66. tmp = end_sect;
  67. if (req_sects < nr_sects &&
  68. sector_div(tmp, granularity) != alignment) {
  69. end_sect = end_sect - alignment;
  70. sector_div(end_sect, granularity);
  71. end_sect = end_sect * granularity + alignment;
  72. req_sects = end_sect - sector;
  73. }
  74. bio = next_bio(bio, 0, gfp_mask);
  75. bio->bi_iter.bi_sector = sector;
  76. bio_set_dev(bio, bdev);
  77. bio_set_op_attrs(bio, op, 0);
  78. bio->bi_iter.bi_size = req_sects << 9;
  79. nr_sects -= req_sects;
  80. sector = end_sect;
  81. /*
  82. * We can loop for a long time in here, if someone does
  83. * full device discards (like mkfs). Be nice and allow
  84. * us to schedule out to avoid softlocking if preempt
  85. * is disabled.
  86. */
  87. cond_resched();
  88. }
  89. *biop = bio;
  90. return 0;
  91. fail:
  92. if (bio) {
  93. submit_bio_wait(bio);
  94. bio_put(bio);
  95. }
  96. *biop = NULL;
  97. return -EOPNOTSUPP;
  98. }
  99. EXPORT_SYMBOL(__blkdev_issue_discard);
  100. /**
  101. * blkdev_issue_discard - queue a discard
  102. * @bdev: blockdev to issue discard for
  103. * @sector: start sector
  104. * @nr_sects: number of sectors to discard
  105. * @gfp_mask: memory allocation flags (for bio_alloc)
  106. * @flags: BLKDEV_DISCARD_* flags to control behaviour
  107. *
  108. * Description:
  109. * Issue a discard request for the sectors in question.
  110. */
  111. int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
  112. sector_t nr_sects, gfp_t gfp_mask, unsigned long flags)
  113. {
  114. struct bio *bio = NULL;
  115. struct blk_plug plug;
  116. int ret;
  117. blk_start_plug(&plug);
  118. ret = __blkdev_issue_discard(bdev, sector, nr_sects, gfp_mask, flags,
  119. &bio);
  120. if (!ret && bio) {
  121. ret = submit_bio_wait(bio);
  122. if (ret == -EOPNOTSUPP)
  123. ret = 0;
  124. bio_put(bio);
  125. }
  126. blk_finish_plug(&plug);
  127. return ret;
  128. }
  129. EXPORT_SYMBOL(blkdev_issue_discard);
  130. /**
  131. * __blkdev_issue_write_same - generate number of bios with same page
  132. * @bdev: target blockdev
  133. * @sector: start sector
  134. * @nr_sects: number of sectors to write
  135. * @gfp_mask: memory allocation flags (for bio_alloc)
  136. * @page: page containing data to write
  137. * @biop: pointer to anchor bio
  138. *
  139. * Description:
  140. * Generate and issue number of bios(REQ_OP_WRITE_SAME) with same page.
  141. */
  142. static int __blkdev_issue_write_same(struct block_device *bdev, sector_t sector,
  143. sector_t nr_sects, gfp_t gfp_mask, struct page *page,
  144. struct bio **biop)
  145. {
  146. struct request_queue *q = bdev_get_queue(bdev);
  147. unsigned int max_write_same_sectors;
  148. struct bio *bio = *biop;
  149. sector_t bs_mask;
  150. if (!q)
  151. return -ENXIO;
  152. bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1;
  153. if ((sector | nr_sects) & bs_mask)
  154. return -EINVAL;
  155. if (!bdev_write_same(bdev))
  156. return -EOPNOTSUPP;
  157. /* Ensure that max_write_same_sectors doesn't overflow bi_size */
  158. max_write_same_sectors = UINT_MAX >> 9;
  159. while (nr_sects) {
  160. bio = next_bio(bio, 1, gfp_mask);
  161. bio->bi_iter.bi_sector = sector;
  162. bio_set_dev(bio, bdev);
  163. bio->bi_vcnt = 1;
  164. bio->bi_io_vec->bv_page = page;
  165. bio->bi_io_vec->bv_offset = 0;
  166. bio->bi_io_vec->bv_len = bdev_logical_block_size(bdev);
  167. bio_set_op_attrs(bio, REQ_OP_WRITE_SAME, 0);
  168. if (nr_sects > max_write_same_sectors) {
  169. bio->bi_iter.bi_size = max_write_same_sectors << 9;
  170. nr_sects -= max_write_same_sectors;
  171. sector += max_write_same_sectors;
  172. } else {
  173. bio->bi_iter.bi_size = nr_sects << 9;
  174. nr_sects = 0;
  175. }
  176. cond_resched();
  177. }
  178. *biop = bio;
  179. return 0;
  180. }
  181. /**
  182. * blkdev_issue_write_same - queue a write same operation
  183. * @bdev: target blockdev
  184. * @sector: start sector
  185. * @nr_sects: number of sectors to write
  186. * @gfp_mask: memory allocation flags (for bio_alloc)
  187. * @page: page containing data
  188. *
  189. * Description:
  190. * Issue a write same request for the sectors in question.
  191. */
  192. int blkdev_issue_write_same(struct block_device *bdev, sector_t sector,
  193. sector_t nr_sects, gfp_t gfp_mask,
  194. struct page *page)
  195. {
  196. struct bio *bio = NULL;
  197. struct blk_plug plug;
  198. int ret;
  199. blk_start_plug(&plug);
  200. ret = __blkdev_issue_write_same(bdev, sector, nr_sects, gfp_mask, page,
  201. &bio);
  202. if (ret == 0 && bio) {
  203. ret = submit_bio_wait(bio);
  204. bio_put(bio);
  205. }
  206. blk_finish_plug(&plug);
  207. return ret;
  208. }
  209. EXPORT_SYMBOL(blkdev_issue_write_same);
  210. static int __blkdev_issue_write_zeroes(struct block_device *bdev,
  211. sector_t sector, sector_t nr_sects, gfp_t gfp_mask,
  212. struct bio **biop, unsigned flags)
  213. {
  214. struct bio *bio = *biop;
  215. unsigned int max_write_zeroes_sectors;
  216. struct request_queue *q = bdev_get_queue(bdev);
  217. if (!q)
  218. return -ENXIO;
  219. /* Ensure that max_write_zeroes_sectors doesn't overflow bi_size */
  220. max_write_zeroes_sectors = bdev_write_zeroes_sectors(bdev);
  221. if (max_write_zeroes_sectors == 0)
  222. return -EOPNOTSUPP;
  223. while (nr_sects) {
  224. bio = next_bio(bio, 0, gfp_mask);
  225. bio->bi_iter.bi_sector = sector;
  226. bio_set_dev(bio, bdev);
  227. bio->bi_opf = REQ_OP_WRITE_ZEROES;
  228. if (flags & BLKDEV_ZERO_NOUNMAP)
  229. bio->bi_opf |= REQ_NOUNMAP;
  230. if (nr_sects > max_write_zeroes_sectors) {
  231. bio->bi_iter.bi_size = max_write_zeroes_sectors << 9;
  232. nr_sects -= max_write_zeroes_sectors;
  233. sector += max_write_zeroes_sectors;
  234. } else {
  235. bio->bi_iter.bi_size = nr_sects << 9;
  236. nr_sects = 0;
  237. }
  238. cond_resched();
  239. }
  240. *biop = bio;
  241. return 0;
  242. }
  243. /*
  244. * Convert a number of 512B sectors to a number of pages.
  245. * The result is limited to a number of pages that can fit into a BIO.
  246. * Also make sure that the result is always at least 1 (page) for the cases
  247. * where nr_sects is lower than the number of sectors in a page.
  248. */
  249. static unsigned int __blkdev_sectors_to_bio_pages(sector_t nr_sects)
  250. {
  251. sector_t pages = DIV_ROUND_UP_SECTOR_T(nr_sects, PAGE_SIZE / 512);
  252. return min(pages, (sector_t)BIO_MAX_PAGES);
  253. }
  254. static int __blkdev_issue_zero_pages(struct block_device *bdev,
  255. sector_t sector, sector_t nr_sects, gfp_t gfp_mask,
  256. struct bio **biop)
  257. {
  258. struct request_queue *q = bdev_get_queue(bdev);
  259. struct bio *bio = *biop;
  260. int bi_size = 0;
  261. unsigned int sz;
  262. if (!q)
  263. return -ENXIO;
  264. while (nr_sects != 0) {
  265. bio = next_bio(bio, __blkdev_sectors_to_bio_pages(nr_sects),
  266. gfp_mask);
  267. bio->bi_iter.bi_sector = sector;
  268. bio_set_dev(bio, bdev);
  269. bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
  270. while (nr_sects != 0) {
  271. sz = min((sector_t) PAGE_SIZE, nr_sects << 9);
  272. bi_size = bio_add_page(bio, ZERO_PAGE(0), sz, 0);
  273. nr_sects -= bi_size >> 9;
  274. sector += bi_size >> 9;
  275. if (bi_size < sz)
  276. break;
  277. }
  278. cond_resched();
  279. }
  280. *biop = bio;
  281. return 0;
  282. }
  283. /**
  284. * __blkdev_issue_zeroout - generate number of zero filed write bios
  285. * @bdev: blockdev to issue
  286. * @sector: start sector
  287. * @nr_sects: number of sectors to write
  288. * @gfp_mask: memory allocation flags (for bio_alloc)
  289. * @biop: pointer to anchor bio
  290. * @flags: controls detailed behavior
  291. *
  292. * Description:
  293. * Zero-fill a block range, either using hardware offload or by explicitly
  294. * writing zeroes to the device.
  295. *
  296. * If a device is using logical block provisioning, the underlying space will
  297. * not be released if %flags contains BLKDEV_ZERO_NOUNMAP.
  298. *
  299. * If %flags contains BLKDEV_ZERO_NOFALLBACK, the function will return
  300. * -EOPNOTSUPP if no explicit hardware offload for zeroing is provided.
  301. */
  302. int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
  303. sector_t nr_sects, gfp_t gfp_mask, struct bio **biop,
  304. unsigned flags)
  305. {
  306. int ret;
  307. sector_t bs_mask;
  308. bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1;
  309. if ((sector | nr_sects) & bs_mask)
  310. return -EINVAL;
  311. ret = __blkdev_issue_write_zeroes(bdev, sector, nr_sects, gfp_mask,
  312. biop, flags);
  313. if (ret != -EOPNOTSUPP || (flags & BLKDEV_ZERO_NOFALLBACK))
  314. return ret;
  315. return __blkdev_issue_zero_pages(bdev, sector, nr_sects, gfp_mask,
  316. biop);
  317. }
  318. EXPORT_SYMBOL(__blkdev_issue_zeroout);
  319. /**
  320. * blkdev_issue_zeroout - zero-fill a block range
  321. * @bdev: blockdev to write
  322. * @sector: start sector
  323. * @nr_sects: number of sectors to write
  324. * @gfp_mask: memory allocation flags (for bio_alloc)
  325. * @flags: controls detailed behavior
  326. *
  327. * Description:
  328. * Zero-fill a block range, either using hardware offload or by explicitly
  329. * writing zeroes to the device. See __blkdev_issue_zeroout() for the
  330. * valid values for %flags.
  331. */
  332. int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
  333. sector_t nr_sects, gfp_t gfp_mask, unsigned flags)
  334. {
  335. int ret = 0;
  336. sector_t bs_mask;
  337. struct bio *bio;
  338. struct blk_plug plug;
  339. bool try_write_zeroes = !!bdev_write_zeroes_sectors(bdev);
  340. bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1;
  341. if ((sector | nr_sects) & bs_mask)
  342. return -EINVAL;
  343. retry:
  344. bio = NULL;
  345. blk_start_plug(&plug);
  346. if (try_write_zeroes) {
  347. ret = __blkdev_issue_write_zeroes(bdev, sector, nr_sects,
  348. gfp_mask, &bio, flags);
  349. } else if (!(flags & BLKDEV_ZERO_NOFALLBACK)) {
  350. ret = __blkdev_issue_zero_pages(bdev, sector, nr_sects,
  351. gfp_mask, &bio);
  352. } else {
  353. /* No zeroing offload support */
  354. ret = -EOPNOTSUPP;
  355. }
  356. if (ret == 0 && bio) {
  357. ret = submit_bio_wait(bio);
  358. bio_put(bio);
  359. }
  360. blk_finish_plug(&plug);
  361. if (ret && try_write_zeroes) {
  362. if (!(flags & BLKDEV_ZERO_NOFALLBACK)) {
  363. try_write_zeroes = false;
  364. goto retry;
  365. }
  366. if (!bdev_write_zeroes_sectors(bdev)) {
  367. /*
  368. * Zeroing offload support was indicated, but the
  369. * device reported ILLEGAL REQUEST (for some devices
  370. * there is no non-destructive way to verify whether
  371. * WRITE ZEROES is actually supported).
  372. */
  373. ret = -EOPNOTSUPP;
  374. }
  375. }
  376. return ret;
  377. }
  378. EXPORT_SYMBOL(blkdev_issue_zeroout);