blk-lib.c 10 KB

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