blk-lib.c 4.7 KB

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