blk-lib.c 9.8 KB

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