blk-lib.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. enum req_op op;
  28. int alignment;
  29. sector_t bs_mask;
  30. if (!q)
  31. return -ENXIO;
  32. if (flags & BLKDEV_DISCARD_SECURE) {
  33. if (flags & BLKDEV_DISCARD_ZERO)
  34. return -EOPNOTSUPP;
  35. if (!blk_queue_secure_erase(q))
  36. return -EOPNOTSUPP;
  37. op = REQ_OP_SECURE_ERASE;
  38. } else {
  39. if (!blk_queue_discard(q))
  40. return -EOPNOTSUPP;
  41. if ((flags & BLKDEV_DISCARD_ZERO) &&
  42. !q->limits.discard_zeroes_data)
  43. return -EOPNOTSUPP;
  44. op = REQ_OP_DISCARD;
  45. }
  46. bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1;
  47. if ((sector | nr_sects) & bs_mask)
  48. return -EINVAL;
  49. /* Zero-sector (unknown) and one-sector granularities are the same. */
  50. granularity = max(q->limits.discard_granularity >> 9, 1U);
  51. alignment = (bdev_discard_alignment(bdev) >> 9) % granularity;
  52. while (nr_sects) {
  53. unsigned int req_sects;
  54. sector_t end_sect, tmp;
  55. /*
  56. * Issue in chunks of the user defined max discard setting,
  57. * ensuring that bi_size doesn't overflow
  58. */
  59. req_sects = min_t(sector_t, nr_sects,
  60. q->limits.max_discard_sectors);
  61. if (!req_sects)
  62. goto fail;
  63. if (req_sects > UINT_MAX >> 9)
  64. req_sects = UINT_MAX >> 9;
  65. /*
  66. * If splitting a request, and the next starting sector would be
  67. * misaligned, stop the discard at the previous aligned sector.
  68. */
  69. end_sect = sector + req_sects;
  70. tmp = end_sect;
  71. if (req_sects < nr_sects &&
  72. sector_div(tmp, granularity) != alignment) {
  73. end_sect = end_sect - alignment;
  74. sector_div(end_sect, granularity);
  75. end_sect = end_sect * granularity + alignment;
  76. req_sects = end_sect - sector;
  77. }
  78. bio = next_bio(bio, 1, gfp_mask);
  79. bio->bi_iter.bi_sector = sector;
  80. bio->bi_bdev = bdev;
  81. bio_set_op_attrs(bio, op, 0);
  82. bio->bi_iter.bi_size = req_sects << 9;
  83. nr_sects -= req_sects;
  84. sector = end_sect;
  85. /*
  86. * We can loop for a long time in here, if someone does
  87. * full device discards (like mkfs). Be nice and allow
  88. * us to schedule out to avoid softlocking if preempt
  89. * is disabled.
  90. */
  91. cond_resched();
  92. }
  93. *biop = bio;
  94. return 0;
  95. fail:
  96. if (bio) {
  97. submit_bio_wait(bio);
  98. bio_put(bio);
  99. }
  100. *biop = NULL;
  101. return -EOPNOTSUPP;
  102. }
  103. EXPORT_SYMBOL(__blkdev_issue_discard);
  104. /**
  105. * blkdev_issue_discard - queue a discard
  106. * @bdev: blockdev to issue discard for
  107. * @sector: start sector
  108. * @nr_sects: number of sectors to discard
  109. * @gfp_mask: memory allocation flags (for bio_alloc)
  110. * @flags: BLKDEV_IFL_* flags to control behaviour
  111. *
  112. * Description:
  113. * Issue a discard request for the sectors in question.
  114. */
  115. int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
  116. sector_t nr_sects, gfp_t gfp_mask, unsigned long flags)
  117. {
  118. struct bio *bio = NULL;
  119. struct blk_plug plug;
  120. int ret;
  121. blk_start_plug(&plug);
  122. ret = __blkdev_issue_discard(bdev, sector, nr_sects, gfp_mask, flags,
  123. &bio);
  124. if (!ret && bio) {
  125. ret = submit_bio_wait(bio);
  126. if (ret == -EOPNOTSUPP && !(flags & BLKDEV_DISCARD_ZERO))
  127. ret = 0;
  128. bio_put(bio);
  129. }
  130. blk_finish_plug(&plug);
  131. return ret;
  132. }
  133. EXPORT_SYMBOL(blkdev_issue_discard);
  134. /**
  135. * blkdev_issue_write_same - queue a write same operation
  136. * @bdev: target blockdev
  137. * @sector: start sector
  138. * @nr_sects: number of sectors to write
  139. * @gfp_mask: memory allocation flags (for bio_alloc)
  140. * @page: page containing data to write
  141. *
  142. * Description:
  143. * Issue a write same request for the sectors in question.
  144. */
  145. int blkdev_issue_write_same(struct block_device *bdev, sector_t sector,
  146. sector_t nr_sects, gfp_t gfp_mask,
  147. struct page *page)
  148. {
  149. struct request_queue *q = bdev_get_queue(bdev);
  150. unsigned int max_write_same_sectors;
  151. struct bio *bio = NULL;
  152. int ret = 0;
  153. sector_t bs_mask;
  154. if (!q)
  155. return -ENXIO;
  156. bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1;
  157. if ((sector | nr_sects) & bs_mask)
  158. return -EINVAL;
  159. /* Ensure that max_write_same_sectors doesn't overflow bi_size */
  160. max_write_same_sectors = UINT_MAX >> 9;
  161. while (nr_sects) {
  162. bio = next_bio(bio, 1, gfp_mask);
  163. bio->bi_iter.bi_sector = sector;
  164. bio->bi_bdev = bdev;
  165. bio->bi_vcnt = 1;
  166. bio->bi_io_vec->bv_page = page;
  167. bio->bi_io_vec->bv_offset = 0;
  168. bio->bi_io_vec->bv_len = bdev_logical_block_size(bdev);
  169. bio_set_op_attrs(bio, REQ_OP_WRITE_SAME, 0);
  170. if (nr_sects > max_write_same_sectors) {
  171. bio->bi_iter.bi_size = max_write_same_sectors << 9;
  172. nr_sects -= max_write_same_sectors;
  173. sector += max_write_same_sectors;
  174. } else {
  175. bio->bi_iter.bi_size = nr_sects << 9;
  176. nr_sects = 0;
  177. }
  178. }
  179. if (bio) {
  180. ret = submit_bio_wait(bio);
  181. bio_put(bio);
  182. }
  183. return ret;
  184. }
  185. EXPORT_SYMBOL(blkdev_issue_write_same);
  186. /**
  187. * blkdev_issue_zeroout - generate number of zero filed write bios
  188. * @bdev: blockdev to issue
  189. * @sector: start sector
  190. * @nr_sects: number of sectors to write
  191. * @gfp_mask: memory allocation flags (for bio_alloc)
  192. *
  193. * Description:
  194. * Generate and issue number of bios with zerofiled pages.
  195. */
  196. static int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
  197. sector_t nr_sects, gfp_t gfp_mask)
  198. {
  199. int ret;
  200. struct bio *bio = NULL;
  201. unsigned int sz;
  202. sector_t bs_mask;
  203. bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1;
  204. if ((sector | nr_sects) & bs_mask)
  205. return -EINVAL;
  206. while (nr_sects != 0) {
  207. bio = next_bio(bio, min(nr_sects, (sector_t)BIO_MAX_PAGES),
  208. gfp_mask);
  209. bio->bi_iter.bi_sector = sector;
  210. bio->bi_bdev = bdev;
  211. bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
  212. while (nr_sects != 0) {
  213. sz = min((sector_t) PAGE_SIZE >> 9 , nr_sects);
  214. ret = bio_add_page(bio, ZERO_PAGE(0), sz << 9, 0);
  215. nr_sects -= ret >> 9;
  216. sector += ret >> 9;
  217. if (ret < (sz << 9))
  218. break;
  219. }
  220. }
  221. if (bio) {
  222. ret = submit_bio_wait(bio);
  223. bio_put(bio);
  224. return ret;
  225. }
  226. return 0;
  227. }
  228. /**
  229. * blkdev_issue_zeroout - zero-fill a block range
  230. * @bdev: blockdev to write
  231. * @sector: start sector
  232. * @nr_sects: number of sectors to write
  233. * @gfp_mask: memory allocation flags (for bio_alloc)
  234. * @discard: whether to discard the block range
  235. *
  236. * Description:
  237. * Zero-fill a block range. If the discard flag is set and the block
  238. * device guarantees that subsequent READ operations to the block range
  239. * in question will return zeroes, the blocks will be discarded. Should
  240. * the discard request fail, if the discard flag is not set, or if
  241. * discard_zeroes_data is not supported, this function will resort to
  242. * zeroing the blocks manually, thus provisioning (allocating,
  243. * anchoring) them. If the block device supports the WRITE SAME command
  244. * blkdev_issue_zeroout() will use it to optimize the process of
  245. * clearing the block range. Otherwise the zeroing will be performed
  246. * using regular WRITE calls.
  247. */
  248. int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
  249. sector_t nr_sects, gfp_t gfp_mask, bool discard)
  250. {
  251. if (discard) {
  252. if (!blkdev_issue_discard(bdev, sector, nr_sects, gfp_mask,
  253. BLKDEV_DISCARD_ZERO))
  254. return 0;
  255. }
  256. if (bdev_write_same(bdev) &&
  257. blkdev_issue_write_same(bdev, sector, nr_sects, gfp_mask,
  258. ZERO_PAGE(0)) == 0)
  259. return 0;
  260. return __blkdev_issue_zeroout(bdev, sector, nr_sects, gfp_mask);
  261. }
  262. EXPORT_SYMBOL(blkdev_issue_zeroout);