blk-lib.c 6.9 KB

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