blk-lib.c 5.0 KB

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