blk-lib.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. struct bio_batch {
  11. atomic_t done;
  12. int error;
  13. struct completion *wait;
  14. };
  15. static void bio_batch_end_io(struct bio *bio)
  16. {
  17. struct bio_batch *bb = bio->bi_private;
  18. if (bio->bi_error && bio->bi_error != -EOPNOTSUPP)
  19. bb->error = bio->bi_error;
  20. if (atomic_dec_and_test(&bb->done))
  21. complete(bb->wait);
  22. bio_put(bio);
  23. }
  24. /*
  25. * Ensure that max discard sectors doesn't overflow bi_size and hopefully
  26. * it is of the proper granularity as long as the granularity is a power
  27. * of two.
  28. */
  29. #define MAX_BIO_SECTORS ((1U << 31) >> 9)
  30. /**
  31. * blkdev_issue_discard - queue a discard
  32. * @bdev: blockdev to issue discard for
  33. * @sector: start sector
  34. * @nr_sects: number of sectors to discard
  35. * @gfp_mask: memory allocation flags (for bio_alloc)
  36. * @flags: BLKDEV_IFL_* flags to control behaviour
  37. *
  38. * Description:
  39. * Issue a discard request for the sectors in question.
  40. */
  41. int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
  42. sector_t nr_sects, gfp_t gfp_mask, unsigned long flags)
  43. {
  44. DECLARE_COMPLETION_ONSTACK(wait);
  45. struct request_queue *q = bdev_get_queue(bdev);
  46. int type = REQ_WRITE | REQ_DISCARD;
  47. struct bio_batch bb;
  48. struct bio *bio;
  49. int ret = 0;
  50. struct blk_plug plug;
  51. if (!q)
  52. return -ENXIO;
  53. if (!blk_queue_discard(q))
  54. return -EOPNOTSUPP;
  55. if (flags & BLKDEV_DISCARD_SECURE) {
  56. if (!blk_queue_secdiscard(q))
  57. return -EOPNOTSUPP;
  58. type |= REQ_SECURE;
  59. }
  60. atomic_set(&bb.done, 1);
  61. bb.error = 0;
  62. bb.wait = &wait;
  63. blk_start_plug(&plug);
  64. while (nr_sects) {
  65. unsigned int req_sects;
  66. sector_t end_sect;
  67. bio = bio_alloc(gfp_mask, 1);
  68. if (!bio) {
  69. ret = -ENOMEM;
  70. break;
  71. }
  72. req_sects = min_t(sector_t, nr_sects, MAX_BIO_SECTORS);
  73. end_sect = sector + req_sects;
  74. bio->bi_iter.bi_sector = sector;
  75. bio->bi_end_io = bio_batch_end_io;
  76. bio->bi_bdev = bdev;
  77. bio->bi_private = &bb;
  78. bio->bi_iter.bi_size = req_sects << 9;
  79. nr_sects -= req_sects;
  80. sector = end_sect;
  81. atomic_inc(&bb.done);
  82. submit_bio(type, bio);
  83. /*
  84. * We can loop for a long time in here, if someone does
  85. * full device discards (like mkfs). Be nice and allow
  86. * us to schedule out to avoid softlocking if preempt
  87. * is disabled.
  88. */
  89. cond_resched();
  90. }
  91. blk_finish_plug(&plug);
  92. /* Wait for bios in-flight */
  93. if (!atomic_dec_and_test(&bb.done))
  94. wait_for_completion_io(&wait);
  95. if (bb.error)
  96. return bb.error;
  97. return ret;
  98. }
  99. EXPORT_SYMBOL(blkdev_issue_discard);
  100. /**
  101. * blkdev_issue_write_same - queue a write same operation
  102. * @bdev: target blockdev
  103. * @sector: start sector
  104. * @nr_sects: number of sectors to write
  105. * @gfp_mask: memory allocation flags (for bio_alloc)
  106. * @page: page containing data to write
  107. *
  108. * Description:
  109. * Issue a write same request for the sectors in question.
  110. */
  111. int blkdev_issue_write_same(struct block_device *bdev, sector_t sector,
  112. sector_t nr_sects, gfp_t gfp_mask,
  113. struct page *page)
  114. {
  115. DECLARE_COMPLETION_ONSTACK(wait);
  116. struct request_queue *q = bdev_get_queue(bdev);
  117. unsigned int max_write_same_sectors;
  118. struct bio_batch bb;
  119. struct bio *bio;
  120. int ret = 0;
  121. if (!q)
  122. return -ENXIO;
  123. /* Ensure that max_write_same_sectors doesn't overflow bi_size */
  124. max_write_same_sectors = UINT_MAX >> 9;
  125. atomic_set(&bb.done, 1);
  126. bb.error = 0;
  127. bb.wait = &wait;
  128. while (nr_sects) {
  129. bio = bio_alloc(gfp_mask, 1);
  130. if (!bio) {
  131. ret = -ENOMEM;
  132. break;
  133. }
  134. bio->bi_iter.bi_sector = sector;
  135. bio->bi_end_io = bio_batch_end_io;
  136. bio->bi_bdev = bdev;
  137. bio->bi_private = &bb;
  138. bio->bi_vcnt = 1;
  139. bio->bi_io_vec->bv_page = page;
  140. bio->bi_io_vec->bv_offset = 0;
  141. bio->bi_io_vec->bv_len = bdev_logical_block_size(bdev);
  142. if (nr_sects > max_write_same_sectors) {
  143. bio->bi_iter.bi_size = max_write_same_sectors << 9;
  144. nr_sects -= max_write_same_sectors;
  145. sector += max_write_same_sectors;
  146. } else {
  147. bio->bi_iter.bi_size = nr_sects << 9;
  148. nr_sects = 0;
  149. }
  150. atomic_inc(&bb.done);
  151. submit_bio(REQ_WRITE | REQ_WRITE_SAME, bio);
  152. }
  153. /* Wait for bios in-flight */
  154. if (!atomic_dec_and_test(&bb.done))
  155. wait_for_completion_io(&wait);
  156. if (bb.error)
  157. return bb.error;
  158. return ret;
  159. }
  160. EXPORT_SYMBOL(blkdev_issue_write_same);
  161. /**
  162. * blkdev_issue_zeroout - generate number of zero filed write bios
  163. * @bdev: blockdev to issue
  164. * @sector: start sector
  165. * @nr_sects: number of sectors to write
  166. * @gfp_mask: memory allocation flags (for bio_alloc)
  167. *
  168. * Description:
  169. * Generate and issue number of bios with zerofiled pages.
  170. */
  171. static int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
  172. sector_t nr_sects, gfp_t gfp_mask)
  173. {
  174. int ret;
  175. struct bio *bio;
  176. struct bio_batch bb;
  177. unsigned int sz;
  178. DECLARE_COMPLETION_ONSTACK(wait);
  179. atomic_set(&bb.done, 1);
  180. bb.error = 0;
  181. bb.wait = &wait;
  182. ret = 0;
  183. while (nr_sects != 0) {
  184. bio = bio_alloc(gfp_mask,
  185. min(nr_sects, (sector_t)BIO_MAX_PAGES));
  186. if (!bio) {
  187. ret = -ENOMEM;
  188. break;
  189. }
  190. bio->bi_iter.bi_sector = sector;
  191. bio->bi_bdev = bdev;
  192. bio->bi_end_io = bio_batch_end_io;
  193. bio->bi_private = &bb;
  194. while (nr_sects != 0) {
  195. sz = min((sector_t) PAGE_SIZE >> 9 , nr_sects);
  196. ret = bio_add_page(bio, ZERO_PAGE(0), sz << 9, 0);
  197. nr_sects -= ret >> 9;
  198. sector += ret >> 9;
  199. if (ret < (sz << 9))
  200. break;
  201. }
  202. ret = 0;
  203. atomic_inc(&bb.done);
  204. submit_bio(WRITE, bio);
  205. }
  206. /* Wait for bios in-flight */
  207. if (!atomic_dec_and_test(&bb.done))
  208. wait_for_completion_io(&wait);
  209. if (bb.error)
  210. return bb.error;
  211. return ret;
  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. struct request_queue *q = bdev_get_queue(bdev);
  237. if (discard && blk_queue_discard(q) && q->limits.discard_zeroes_data &&
  238. blkdev_issue_discard(bdev, sector, nr_sects, gfp_mask, 0) == 0)
  239. return 0;
  240. if (bdev_write_same(bdev) &&
  241. blkdev_issue_write_same(bdev, sector, nr_sects, gfp_mask,
  242. ZERO_PAGE(0)) == 0)
  243. return 0;
  244. return __blkdev_issue_zeroout(bdev, sector, nr_sects, gfp_mask);
  245. }
  246. EXPORT_SYMBOL(blkdev_issue_zeroout);