blk-lib.c 7.5 KB

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