blk-lib.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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_sector = sector;
  96. bio->bi_end_io = bio_batch_end_io;
  97. bio->bi_bdev = bdev;
  98. bio->bi_private = &bb;
  99. bio->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. blk_finish_plug(&plug);
  106. /* Wait for bios in-flight */
  107. if (!atomic_dec_and_test(&bb.done))
  108. wait_for_completion_io(&wait);
  109. if (!test_bit(BIO_UPTODATE, &bb.flags))
  110. ret = -EIO;
  111. return ret;
  112. }
  113. EXPORT_SYMBOL(blkdev_issue_discard);
  114. /**
  115. * blkdev_issue_write_same - queue a write same operation
  116. * @bdev: target blockdev
  117. * @sector: start sector
  118. * @nr_sects: number of sectors to write
  119. * @gfp_mask: memory allocation flags (for bio_alloc)
  120. * @page: page containing data to write
  121. *
  122. * Description:
  123. * Issue a write same request for the sectors in question.
  124. */
  125. int blkdev_issue_write_same(struct block_device *bdev, sector_t sector,
  126. sector_t nr_sects, gfp_t gfp_mask,
  127. struct page *page)
  128. {
  129. DECLARE_COMPLETION_ONSTACK(wait);
  130. struct request_queue *q = bdev_get_queue(bdev);
  131. unsigned int max_write_same_sectors;
  132. struct bio_batch bb;
  133. struct bio *bio;
  134. int ret = 0;
  135. if (!q)
  136. return -ENXIO;
  137. max_write_same_sectors = q->limits.max_write_same_sectors;
  138. if (max_write_same_sectors == 0)
  139. return -EOPNOTSUPP;
  140. atomic_set(&bb.done, 1);
  141. bb.flags = 1 << BIO_UPTODATE;
  142. bb.wait = &wait;
  143. while (nr_sects) {
  144. bio = bio_alloc(gfp_mask, 1);
  145. if (!bio) {
  146. ret = -ENOMEM;
  147. break;
  148. }
  149. bio->bi_sector = sector;
  150. bio->bi_end_io = bio_batch_end_io;
  151. bio->bi_bdev = bdev;
  152. bio->bi_private = &bb;
  153. bio->bi_vcnt = 1;
  154. bio->bi_io_vec->bv_page = page;
  155. bio->bi_io_vec->bv_offset = 0;
  156. bio->bi_io_vec->bv_len = bdev_logical_block_size(bdev);
  157. if (nr_sects > max_write_same_sectors) {
  158. bio->bi_size = max_write_same_sectors << 9;
  159. nr_sects -= max_write_same_sectors;
  160. sector += max_write_same_sectors;
  161. } else {
  162. bio->bi_size = nr_sects << 9;
  163. nr_sects = 0;
  164. }
  165. atomic_inc(&bb.done);
  166. submit_bio(REQ_WRITE | REQ_WRITE_SAME, bio);
  167. }
  168. /* Wait for bios in-flight */
  169. if (!atomic_dec_and_test(&bb.done))
  170. wait_for_completion_io(&wait);
  171. if (!test_bit(BIO_UPTODATE, &bb.flags))
  172. ret = -ENOTSUPP;
  173. return ret;
  174. }
  175. EXPORT_SYMBOL(blkdev_issue_write_same);
  176. /**
  177. * blkdev_issue_zeroout - generate number of zero filed write bios
  178. * @bdev: blockdev to issue
  179. * @sector: start sector
  180. * @nr_sects: number of sectors to write
  181. * @gfp_mask: memory allocation flags (for bio_alloc)
  182. *
  183. * Description:
  184. * Generate and issue number of bios with zerofiled pages.
  185. */
  186. int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
  187. sector_t nr_sects, gfp_t gfp_mask)
  188. {
  189. int ret;
  190. struct bio *bio;
  191. struct bio_batch bb;
  192. unsigned int sz;
  193. DECLARE_COMPLETION_ONSTACK(wait);
  194. atomic_set(&bb.done, 1);
  195. bb.flags = 1 << BIO_UPTODATE;
  196. bb.wait = &wait;
  197. ret = 0;
  198. while (nr_sects != 0) {
  199. bio = bio_alloc(gfp_mask,
  200. min(nr_sects, (sector_t)BIO_MAX_PAGES));
  201. if (!bio) {
  202. ret = -ENOMEM;
  203. break;
  204. }
  205. bio->bi_sector = sector;
  206. bio->bi_bdev = bdev;
  207. bio->bi_end_io = bio_batch_end_io;
  208. bio->bi_private = &bb;
  209. while (nr_sects != 0) {
  210. sz = min((sector_t) PAGE_SIZE >> 9 , nr_sects);
  211. ret = bio_add_page(bio, ZERO_PAGE(0), sz << 9, 0);
  212. nr_sects -= ret >> 9;
  213. sector += ret >> 9;
  214. if (ret < (sz << 9))
  215. break;
  216. }
  217. ret = 0;
  218. atomic_inc(&bb.done);
  219. submit_bio(WRITE, bio);
  220. }
  221. /* Wait for bios in-flight */
  222. if (!atomic_dec_and_test(&bb.done))
  223. wait_for_completion_io(&wait);
  224. if (!test_bit(BIO_UPTODATE, &bb.flags))
  225. /* One of bios in the batch was completed with error.*/
  226. ret = -EIO;
  227. return ret;
  228. }
  229. /**
  230. * blkdev_issue_zeroout - zero-fill a block range
  231. * @bdev: blockdev to write
  232. * @sector: start sector
  233. * @nr_sects: number of sectors to write
  234. * @gfp_mask: memory allocation flags (for bio_alloc)
  235. *
  236. * Description:
  237. * Generate and issue number of bios with zerofiled pages.
  238. */
  239. int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
  240. sector_t nr_sects, gfp_t gfp_mask)
  241. {
  242. if (bdev_write_same(bdev)) {
  243. unsigned char bdn[BDEVNAME_SIZE];
  244. if (!blkdev_issue_write_same(bdev, sector, nr_sects, gfp_mask,
  245. ZERO_PAGE(0)))
  246. return 0;
  247. bdevname(bdev, bdn);
  248. pr_err("%s: WRITE SAME failed. Manually zeroing.\n", bdn);
  249. }
  250. return __blkdev_issue_zeroout(bdev, sector, nr_sects, gfp_mask);
  251. }
  252. EXPORT_SYMBOL(blkdev_issue_zeroout);