blk-lib.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. static void blkdev_discard_end_io(struct bio *bio, int err)
  11. {
  12. if (err) {
  13. if (err == -EOPNOTSUPP)
  14. set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
  15. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  16. }
  17. if (bio->bi_private)
  18. complete(bio->bi_private);
  19. bio_put(bio);
  20. }
  21. /**
  22. * blkdev_issue_discard - queue a discard
  23. * @bdev: blockdev to issue discard for
  24. * @sector: start sector
  25. * @nr_sects: number of sectors to discard
  26. * @gfp_mask: memory allocation flags (for bio_alloc)
  27. * @flags: BLKDEV_IFL_* flags to control behaviour
  28. *
  29. * Description:
  30. * Issue a discard request for the sectors in question.
  31. */
  32. int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
  33. sector_t nr_sects, gfp_t gfp_mask, unsigned long flags)
  34. {
  35. DECLARE_COMPLETION_ONSTACK(wait);
  36. struct request_queue *q = bdev_get_queue(bdev);
  37. int type = flags & BLKDEV_IFL_BARRIER ?
  38. DISCARD_BARRIER : DISCARD_NOBARRIER;
  39. struct bio *bio;
  40. int ret = 0;
  41. if (!q)
  42. return -ENXIO;
  43. if (!blk_queue_discard(q))
  44. return -EOPNOTSUPP;
  45. while (nr_sects && !ret) {
  46. unsigned int max_discard_sectors =
  47. min(q->limits.max_discard_sectors, UINT_MAX >> 9);
  48. bio = bio_alloc(gfp_mask, 1);
  49. if (!bio) {
  50. ret = -ENOMEM;
  51. break;
  52. }
  53. bio->bi_sector = sector;
  54. bio->bi_end_io = blkdev_discard_end_io;
  55. bio->bi_bdev = bdev;
  56. if (flags & BLKDEV_IFL_WAIT)
  57. bio->bi_private = &wait;
  58. if (nr_sects > max_discard_sectors) {
  59. bio->bi_size = max_discard_sectors << 9;
  60. nr_sects -= max_discard_sectors;
  61. sector += max_discard_sectors;
  62. } else {
  63. bio->bi_size = nr_sects << 9;
  64. nr_sects = 0;
  65. }
  66. bio_get(bio);
  67. submit_bio(type, bio);
  68. if (flags & BLKDEV_IFL_WAIT)
  69. wait_for_completion(&wait);
  70. if (bio_flagged(bio, BIO_EOPNOTSUPP))
  71. ret = -EOPNOTSUPP;
  72. else if (!bio_flagged(bio, BIO_UPTODATE))
  73. ret = -EIO;
  74. bio_put(bio);
  75. }
  76. return ret;
  77. }
  78. EXPORT_SYMBOL(blkdev_issue_discard);
  79. struct bio_batch
  80. {
  81. atomic_t done;
  82. unsigned long flags;
  83. struct completion *wait;
  84. bio_end_io_t *end_io;
  85. };
  86. static void bio_batch_end_io(struct bio *bio, int err)
  87. {
  88. struct bio_batch *bb = bio->bi_private;
  89. if (err) {
  90. if (err == -EOPNOTSUPP)
  91. set_bit(BIO_EOPNOTSUPP, &bb->flags);
  92. else
  93. clear_bit(BIO_UPTODATE, &bb->flags);
  94. }
  95. if (bb) {
  96. if (bb->end_io)
  97. bb->end_io(bio, err);
  98. atomic_inc(&bb->done);
  99. complete(bb->wait);
  100. }
  101. bio_put(bio);
  102. }
  103. /**
  104. * blkdev_issue_zeroout generate number of zero filed write bios
  105. * @bdev: blockdev to issue
  106. * @sector: start sector
  107. * @nr_sects: number of sectors to write
  108. * @gfp_mask: memory allocation flags (for bio_alloc)
  109. * @flags: BLKDEV_IFL_* flags to control behaviour
  110. *
  111. * Description:
  112. * Generate and issue number of bios with zerofiled pages.
  113. * Send barrier at the beginning and at the end if requested. This guarantie
  114. * correct request ordering. Empty barrier allow us to avoid post queue flush.
  115. */
  116. int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
  117. sector_t nr_sects, gfp_t gfp_mask, unsigned long flags)
  118. {
  119. int ret = 0;
  120. struct bio *bio;
  121. struct bio_batch bb;
  122. unsigned int sz, issued = 0;
  123. DECLARE_COMPLETION_ONSTACK(wait);
  124. atomic_set(&bb.done, 0);
  125. bb.flags = 1 << BIO_UPTODATE;
  126. bb.wait = &wait;
  127. bb.end_io = NULL;
  128. if (flags & BLKDEV_IFL_BARRIER) {
  129. /* issue async barrier before the data */
  130. ret = blkdev_issue_flush(bdev, gfp_mask, NULL, 0);
  131. if (ret)
  132. return ret;
  133. }
  134. submit:
  135. while (nr_sects != 0) {
  136. bio = bio_alloc(gfp_mask,
  137. min(nr_sects, (sector_t)BIO_MAX_PAGES));
  138. if (!bio)
  139. break;
  140. bio->bi_sector = sector;
  141. bio->bi_bdev = bdev;
  142. bio->bi_end_io = bio_batch_end_io;
  143. if (flags & BLKDEV_IFL_WAIT)
  144. bio->bi_private = &bb;
  145. while (nr_sects != 0) {
  146. sz = min((sector_t) PAGE_SIZE >> 9 , nr_sects);
  147. if (sz == 0)
  148. /* bio has maximum size possible */
  149. break;
  150. ret = bio_add_page(bio, ZERO_PAGE(0), sz << 9, 0);
  151. nr_sects -= ret >> 9;
  152. sector += ret >> 9;
  153. if (ret < (sz << 9))
  154. break;
  155. }
  156. issued++;
  157. submit_bio(WRITE, bio);
  158. }
  159. /*
  160. * When all data bios are in flight. Send final barrier if requeted.
  161. */
  162. if (nr_sects == 0 && flags & BLKDEV_IFL_BARRIER)
  163. ret = blkdev_issue_flush(bdev, gfp_mask, NULL,
  164. flags & BLKDEV_IFL_WAIT);
  165. if (flags & BLKDEV_IFL_WAIT)
  166. /* Wait for bios in-flight */
  167. while ( issued != atomic_read(&bb.done))
  168. wait_for_completion(&wait);
  169. if (!test_bit(BIO_UPTODATE, &bb.flags))
  170. /* One of bios in the batch was completed with error.*/
  171. ret = -EIO;
  172. if (ret)
  173. goto out;
  174. if (test_bit(BIO_EOPNOTSUPP, &bb.flags)) {
  175. ret = -EOPNOTSUPP;
  176. goto out;
  177. }
  178. if (nr_sects != 0)
  179. goto submit;
  180. out:
  181. return ret;
  182. }
  183. EXPORT_SYMBOL(blkdev_issue_zeroout);