blk-lib.c 7.1 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. 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. sector_t max_discard_sectors;
  42. sector_t granularity, 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;
  54. alignment = sector_div(alignment, granularity);
  55. /*
  56. * Ensure that max_discard_sectors is of the proper
  57. * granularity, so that requests stay aligned after a split.
  58. */
  59. max_discard_sectors = min(q->limits.max_discard_sectors, UINT_MAX >> 9);
  60. sector_div(max_discard_sectors, granularity);
  61. max_discard_sectors *= granularity;
  62. if (unlikely(!max_discard_sectors)) {
  63. /* Avoid infinite loop below. Being cautious never hurts. */
  64. return -EOPNOTSUPP;
  65. }
  66. if (flags & BLKDEV_DISCARD_SECURE) {
  67. if (!blk_queue_secdiscard(q))
  68. return -EOPNOTSUPP;
  69. type |= REQ_SECURE;
  70. }
  71. atomic_set(&bb.done, 1);
  72. bb.flags = 1 << BIO_UPTODATE;
  73. bb.wait = &wait;
  74. blk_start_plug(&plug);
  75. while (nr_sects) {
  76. unsigned int req_sects;
  77. sector_t end_sect, tmp;
  78. bio = bio_alloc(gfp_mask, 1);
  79. if (!bio) {
  80. ret = -ENOMEM;
  81. break;
  82. }
  83. req_sects = min_t(sector_t, nr_sects, max_discard_sectors);
  84. /*
  85. * If splitting a request, and the next starting sector would be
  86. * misaligned, stop the discard at the previous aligned sector.
  87. */
  88. end_sect = sector + req_sects;
  89. tmp = end_sect;
  90. if (req_sects < nr_sects &&
  91. sector_div(tmp, granularity) != alignment) {
  92. end_sect = end_sect - alignment;
  93. sector_div(end_sect, granularity);
  94. end_sect = end_sect * granularity + alignment;
  95. req_sects = end_sect - sector;
  96. }
  97. bio->bi_sector = sector;
  98. bio->bi_end_io = bio_batch_end_io;
  99. bio->bi_bdev = bdev;
  100. bio->bi_private = &bb;
  101. bio->bi_size = req_sects << 9;
  102. nr_sects -= req_sects;
  103. sector = end_sect;
  104. atomic_inc(&bb.done);
  105. submit_bio(type, bio);
  106. }
  107. blk_finish_plug(&plug);
  108. /* Wait for bios in-flight */
  109. if (!atomic_dec_and_test(&bb.done))
  110. wait_for_completion_io(&wait);
  111. if (!test_bit(BIO_UPTODATE, &bb.flags))
  112. ret = -EIO;
  113. return ret;
  114. }
  115. EXPORT_SYMBOL(blkdev_issue_discard);
  116. /**
  117. * blkdev_issue_write_same - queue a write same operation
  118. * @bdev: target blockdev
  119. * @sector: start sector
  120. * @nr_sects: number of sectors to write
  121. * @gfp_mask: memory allocation flags (for bio_alloc)
  122. * @page: page containing data to write
  123. *
  124. * Description:
  125. * Issue a write same request for the sectors in question.
  126. */
  127. int blkdev_issue_write_same(struct block_device *bdev, sector_t sector,
  128. sector_t nr_sects, gfp_t gfp_mask,
  129. struct page *page)
  130. {
  131. DECLARE_COMPLETION_ONSTACK(wait);
  132. struct request_queue *q = bdev_get_queue(bdev);
  133. unsigned int max_write_same_sectors;
  134. struct bio_batch bb;
  135. struct bio *bio;
  136. int ret = 0;
  137. if (!q)
  138. return -ENXIO;
  139. max_write_same_sectors = q->limits.max_write_same_sectors;
  140. if (max_write_same_sectors == 0)
  141. return -EOPNOTSUPP;
  142. atomic_set(&bb.done, 1);
  143. bb.flags = 1 << BIO_UPTODATE;
  144. bb.wait = &wait;
  145. while (nr_sects) {
  146. bio = bio_alloc(gfp_mask, 1);
  147. if (!bio) {
  148. ret = -ENOMEM;
  149. break;
  150. }
  151. bio->bi_sector = sector;
  152. bio->bi_end_io = bio_batch_end_io;
  153. bio->bi_bdev = bdev;
  154. bio->bi_private = &bb;
  155. bio->bi_vcnt = 1;
  156. bio->bi_io_vec->bv_page = page;
  157. bio->bi_io_vec->bv_offset = 0;
  158. bio->bi_io_vec->bv_len = bdev_logical_block_size(bdev);
  159. if (nr_sects > max_write_same_sectors) {
  160. bio->bi_size = max_write_same_sectors << 9;
  161. nr_sects -= max_write_same_sectors;
  162. sector += max_write_same_sectors;
  163. } else {
  164. bio->bi_size = nr_sects << 9;
  165. nr_sects = 0;
  166. }
  167. atomic_inc(&bb.done);
  168. submit_bio(REQ_WRITE | REQ_WRITE_SAME, bio);
  169. }
  170. /* Wait for bios in-flight */
  171. if (!atomic_dec_and_test(&bb.done))
  172. wait_for_completion_io(&wait);
  173. if (!test_bit(BIO_UPTODATE, &bb.flags))
  174. ret = -ENOTSUPP;
  175. return ret;
  176. }
  177. EXPORT_SYMBOL(blkdev_issue_write_same);
  178. /**
  179. * blkdev_issue_zeroout - generate number of zero filed write bios
  180. * @bdev: blockdev to issue
  181. * @sector: start sector
  182. * @nr_sects: number of sectors to write
  183. * @gfp_mask: memory allocation flags (for bio_alloc)
  184. *
  185. * Description:
  186. * Generate and issue number of bios with zerofiled pages.
  187. */
  188. int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
  189. sector_t nr_sects, gfp_t gfp_mask)
  190. {
  191. int ret;
  192. struct bio *bio;
  193. struct bio_batch bb;
  194. unsigned int sz;
  195. DECLARE_COMPLETION_ONSTACK(wait);
  196. atomic_set(&bb.done, 1);
  197. bb.flags = 1 << BIO_UPTODATE;
  198. bb.wait = &wait;
  199. ret = 0;
  200. while (nr_sects != 0) {
  201. bio = bio_alloc(gfp_mask,
  202. min(nr_sects, (sector_t)BIO_MAX_PAGES));
  203. if (!bio) {
  204. ret = -ENOMEM;
  205. break;
  206. }
  207. bio->bi_sector = sector;
  208. bio->bi_bdev = bdev;
  209. bio->bi_end_io = bio_batch_end_io;
  210. bio->bi_private = &bb;
  211. while (nr_sects != 0) {
  212. sz = min((sector_t) PAGE_SIZE >> 9 , nr_sects);
  213. ret = bio_add_page(bio, ZERO_PAGE(0), sz << 9, 0);
  214. nr_sects -= ret >> 9;
  215. sector += ret >> 9;
  216. if (ret < (sz << 9))
  217. break;
  218. }
  219. ret = 0;
  220. atomic_inc(&bb.done);
  221. submit_bio(WRITE, bio);
  222. }
  223. /* Wait for bios in-flight */
  224. if (!atomic_dec_and_test(&bb.done))
  225. wait_for_completion_io(&wait);
  226. if (!test_bit(BIO_UPTODATE, &bb.flags))
  227. /* One of bios in the batch was completed with error.*/
  228. ret = -EIO;
  229. return ret;
  230. }
  231. /**
  232. * blkdev_issue_zeroout - zero-fill a block range
  233. * @bdev: blockdev to write
  234. * @sector: start sector
  235. * @nr_sects: number of sectors to write
  236. * @gfp_mask: memory allocation flags (for bio_alloc)
  237. *
  238. * Description:
  239. * Generate and issue number of bios with zerofiled pages.
  240. */
  241. int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
  242. sector_t nr_sects, gfp_t gfp_mask)
  243. {
  244. if (bdev_write_same(bdev)) {
  245. unsigned char bdn[BDEVNAME_SIZE];
  246. if (!blkdev_issue_write_same(bdev, sector, nr_sects, gfp_mask,
  247. ZERO_PAGE(0)))
  248. return 0;
  249. bdevname(bdev, bdn);
  250. pr_err("%s: WRITE SAME failed. Manually zeroing.\n", bdn);
  251. }
  252. return __blkdev_issue_zeroout(bdev, sector, nr_sects, gfp_mask);
  253. }
  254. EXPORT_SYMBOL(blkdev_issue_zeroout);