blk-lib.c 7.3 KB

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