blk-lib.c 6.8 KB

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