blk-lib.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. unsigned long flags;
  13. struct completion *wait;
  14. };
  15. static void bio_batch_end_io(struct bio *bio, int err)
  16. {
  17. struct bio_batch *bb = bio->bi_private;
  18. if (err && (err != -EOPNOTSUPP))
  19. clear_bit(BIO_UPTODATE, &bb->flags);
  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 max_discard_sectors, 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. /*
  55. * Ensure that max_discard_sectors is of the proper
  56. * granularity, so that requests stay aligned after a split.
  57. */
  58. max_discard_sectors = min(q->limits.max_discard_sectors, UINT_MAX >> 9);
  59. max_discard_sectors -= max_discard_sectors % granularity;
  60. if (unlikely(!max_discard_sectors)) {
  61. /* Avoid infinite loop below. Being cautious never hurts. */
  62. return -EOPNOTSUPP;
  63. }
  64. if (flags & BLKDEV_DISCARD_SECURE) {
  65. if (!blk_queue_secdiscard(q))
  66. return -EOPNOTSUPP;
  67. type |= REQ_SECURE;
  68. }
  69. atomic_set(&bb.done, 1);
  70. bb.flags = 1 << BIO_UPTODATE;
  71. bb.wait = &wait;
  72. blk_start_plug(&plug);
  73. while (nr_sects) {
  74. unsigned int req_sects;
  75. sector_t end_sect, tmp;
  76. bio = bio_alloc(gfp_mask, 1);
  77. if (!bio) {
  78. ret = -ENOMEM;
  79. break;
  80. }
  81. req_sects = min_t(sector_t, nr_sects, max_discard_sectors);
  82. /*
  83. * If splitting a request, and the next starting sector would be
  84. * misaligned, stop the discard at the previous aligned sector.
  85. */
  86. end_sect = sector + req_sects;
  87. tmp = end_sect;
  88. if (req_sects < nr_sects &&
  89. sector_div(tmp, granularity) != alignment) {
  90. end_sect = end_sect - alignment;
  91. sector_div(end_sect, granularity);
  92. end_sect = end_sect * granularity + alignment;
  93. req_sects = end_sect - sector;
  94. }
  95. bio->bi_iter.bi_sector = sector;
  96. bio->bi_end_io = bio_batch_end_io;
  97. bio->bi_bdev = bdev;
  98. bio->bi_private = &bb;
  99. bio->bi_iter.bi_size = req_sects << 9;
  100. nr_sects -= req_sects;
  101. sector = end_sect;
  102. atomic_inc(&bb.done);
  103. submit_bio(type, bio);
  104. /*
  105. * We can loop for a long time in here, if someone does
  106. * full device discards (like mkfs). Be nice and allow
  107. * us to schedule out to avoid softlocking if preempt
  108. * is disabled.
  109. */
  110. cond_resched();
  111. }
  112. blk_finish_plug(&plug);
  113. /* Wait for bios in-flight */
  114. if (!atomic_dec_and_test(&bb.done))
  115. wait_for_completion_io(&wait);
  116. if (!test_bit(BIO_UPTODATE, &bb.flags))
  117. ret = -EIO;
  118. return ret;
  119. }
  120. EXPORT_SYMBOL(blkdev_issue_discard);
  121. /**
  122. * blkdev_issue_write_same - queue a write same operation
  123. * @bdev: target blockdev
  124. * @sector: start sector
  125. * @nr_sects: number of sectors to write
  126. * @gfp_mask: memory allocation flags (for bio_alloc)
  127. * @page: page containing data to write
  128. *
  129. * Description:
  130. * Issue a write same request for the sectors in question.
  131. */
  132. int blkdev_issue_write_same(struct block_device *bdev, sector_t sector,
  133. sector_t nr_sects, gfp_t gfp_mask,
  134. struct page *page)
  135. {
  136. DECLARE_COMPLETION_ONSTACK(wait);
  137. struct request_queue *q = bdev_get_queue(bdev);
  138. unsigned int max_write_same_sectors;
  139. struct bio_batch bb;
  140. struct bio *bio;
  141. int ret = 0;
  142. if (!q)
  143. return -ENXIO;
  144. max_write_same_sectors = q->limits.max_write_same_sectors;
  145. if (max_write_same_sectors == 0)
  146. return -EOPNOTSUPP;
  147. atomic_set(&bb.done, 1);
  148. bb.flags = 1 << BIO_UPTODATE;
  149. bb.wait = &wait;
  150. while (nr_sects) {
  151. bio = bio_alloc(gfp_mask, 1);
  152. if (!bio) {
  153. ret = -ENOMEM;
  154. break;
  155. }
  156. bio->bi_iter.bi_sector = sector;
  157. bio->bi_end_io = bio_batch_end_io;
  158. bio->bi_bdev = bdev;
  159. bio->bi_private = &bb;
  160. bio->bi_vcnt = 1;
  161. bio->bi_io_vec->bv_page = page;
  162. bio->bi_io_vec->bv_offset = 0;
  163. bio->bi_io_vec->bv_len = bdev_logical_block_size(bdev);
  164. if (nr_sects > max_write_same_sectors) {
  165. bio->bi_iter.bi_size = max_write_same_sectors << 9;
  166. nr_sects -= max_write_same_sectors;
  167. sector += max_write_same_sectors;
  168. } else {
  169. bio->bi_iter.bi_size = nr_sects << 9;
  170. nr_sects = 0;
  171. }
  172. atomic_inc(&bb.done);
  173. submit_bio(REQ_WRITE | REQ_WRITE_SAME, bio);
  174. }
  175. /* Wait for bios in-flight */
  176. if (!atomic_dec_and_test(&bb.done))
  177. wait_for_completion_io(&wait);
  178. if (!test_bit(BIO_UPTODATE, &bb.flags))
  179. ret = -ENOTSUPP;
  180. return ret;
  181. }
  182. EXPORT_SYMBOL(blkdev_issue_write_same);
  183. /**
  184. * blkdev_issue_zeroout - generate number of zero filed write bios
  185. * @bdev: blockdev to issue
  186. * @sector: start sector
  187. * @nr_sects: number of sectors to write
  188. * @gfp_mask: memory allocation flags (for bio_alloc)
  189. *
  190. * Description:
  191. * Generate and issue number of bios with zerofiled pages.
  192. */
  193. static int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
  194. sector_t nr_sects, gfp_t gfp_mask)
  195. {
  196. int ret;
  197. struct bio *bio;
  198. struct bio_batch bb;
  199. unsigned int sz;
  200. DECLARE_COMPLETION_ONSTACK(wait);
  201. atomic_set(&bb.done, 1);
  202. bb.flags = 1 << BIO_UPTODATE;
  203. bb.wait = &wait;
  204. ret = 0;
  205. while (nr_sects != 0) {
  206. bio = bio_alloc(gfp_mask,
  207. min(nr_sects, (sector_t)BIO_MAX_PAGES));
  208. if (!bio) {
  209. ret = -ENOMEM;
  210. break;
  211. }
  212. bio->bi_iter.bi_sector = sector;
  213. bio->bi_bdev = bdev;
  214. bio->bi_end_io = bio_batch_end_io;
  215. bio->bi_private = &bb;
  216. while (nr_sects != 0) {
  217. sz = min((sector_t) PAGE_SIZE >> 9 , nr_sects);
  218. ret = bio_add_page(bio, ZERO_PAGE(0), sz << 9, 0);
  219. nr_sects -= ret >> 9;
  220. sector += ret >> 9;
  221. if (ret < (sz << 9))
  222. break;
  223. }
  224. ret = 0;
  225. atomic_inc(&bb.done);
  226. submit_bio(WRITE, bio);
  227. }
  228. /* Wait for bios in-flight */
  229. if (!atomic_dec_and_test(&bb.done))
  230. wait_for_completion_io(&wait);
  231. if (!test_bit(BIO_UPTODATE, &bb.flags))
  232. /* One of bios in the batch was completed with error.*/
  233. ret = -EIO;
  234. return ret;
  235. }
  236. /**
  237. * blkdev_issue_zeroout - zero-fill a block range
  238. * @bdev: blockdev to write
  239. * @sector: start sector
  240. * @nr_sects: number of sectors to write
  241. * @gfp_mask: memory allocation flags (for bio_alloc)
  242. * @discard: whether to discard the block range
  243. *
  244. * Description:
  245. * Zero-fill a block range. If the discard flag is set and the block
  246. * device guarantees that subsequent READ operations to the block range
  247. * in question will return zeroes, the blocks will be discarded. Should
  248. * the discard request fail, if the discard flag is not set, or if
  249. * discard_zeroes_data is not supported, this function will resort to
  250. * zeroing the blocks manually, thus provisioning (allocating,
  251. * anchoring) them. If the block device supports the WRITE SAME command
  252. * blkdev_issue_zeroout() will use it to optimize the process of
  253. * clearing the block range. Otherwise the zeroing will be performed
  254. * using regular WRITE calls.
  255. */
  256. int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
  257. sector_t nr_sects, gfp_t gfp_mask, bool discard)
  258. {
  259. struct request_queue *q = bdev_get_queue(bdev);
  260. if (discard && blk_queue_discard(q) && q->limits.discard_zeroes_data &&
  261. blkdev_issue_discard(bdev, sector, nr_sects, gfp_mask, 0) == 0)
  262. return 0;
  263. if (bdev_write_same(bdev) &&
  264. blkdev_issue_write_same(bdev, sector, nr_sects, gfp_mask,
  265. ZERO_PAGE(0)) == 0)
  266. return 0;
  267. return __blkdev_issue_zeroout(bdev, sector, nr_sects, gfp_mask);
  268. }
  269. EXPORT_SYMBOL(blkdev_issue_zeroout);