blk-lib.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 | REQ_PRIO;
  41. unsigned int max_discard_sectors;
  42. struct bio_batch bb;
  43. struct bio *bio;
  44. int ret = 0;
  45. if (!q)
  46. return -ENXIO;
  47. if (!blk_queue_discard(q))
  48. return -EOPNOTSUPP;
  49. /*
  50. * Ensure that max_discard_sectors is of the proper
  51. * granularity
  52. */
  53. max_discard_sectors = min(q->limits.max_discard_sectors, UINT_MAX >> 9);
  54. if (unlikely(!max_discard_sectors)) {
  55. /* Avoid infinite loop below. Being cautious never hurts. */
  56. return -EOPNOTSUPP;
  57. } else if (q->limits.discard_granularity) {
  58. unsigned int disc_sects = q->limits.discard_granularity >> 9;
  59. max_discard_sectors &= ~(disc_sects - 1);
  60. }
  61. if (flags & BLKDEV_DISCARD_SECURE) {
  62. if (!blk_queue_secdiscard(q))
  63. return -EOPNOTSUPP;
  64. type |= REQ_SECURE;
  65. }
  66. atomic_set(&bb.done, 1);
  67. bb.flags = 1 << BIO_UPTODATE;
  68. bb.wait = &wait;
  69. while (nr_sects) {
  70. bio = bio_alloc(gfp_mask, 1);
  71. if (!bio) {
  72. ret = -ENOMEM;
  73. break;
  74. }
  75. bio->bi_sector = sector;
  76. bio->bi_end_io = bio_batch_end_io;
  77. bio->bi_bdev = bdev;
  78. bio->bi_private = &bb;
  79. if (nr_sects > max_discard_sectors) {
  80. bio->bi_size = max_discard_sectors << 9;
  81. nr_sects -= max_discard_sectors;
  82. sector += max_discard_sectors;
  83. } else {
  84. bio->bi_size = nr_sects << 9;
  85. nr_sects = 0;
  86. }
  87. atomic_inc(&bb.done);
  88. submit_bio(type, bio);
  89. /*
  90. * We can loop for a long time in here, if someone does
  91. * full device discards (like mkfs). Be nice and allow
  92. * us to schedule out to avoid softlocking if preempt
  93. * is disabled.
  94. */
  95. cond_resched();
  96. }
  97. /* Wait for bios in-flight */
  98. if (!atomic_dec_and_test(&bb.done))
  99. wait_for_completion(&wait);
  100. if (!test_bit(BIO_UPTODATE, &bb.flags))
  101. ret = -EIO;
  102. return ret;
  103. }
  104. EXPORT_SYMBOL(blkdev_issue_discard);
  105. /**
  106. * blkdev_issue_sanitize - queue a sanitize request
  107. * @bdev: blockdev to issue sanitize for
  108. * @gfp_mask: memory allocation flags (for bio_alloc)
  109. *
  110. * Description:
  111. * Issue a sanitize request for the specified block device
  112. */
  113. int blkdev_issue_sanitize(struct block_device *bdev, gfp_t gfp_mask)
  114. {
  115. DECLARE_COMPLETION_ONSTACK(wait);
  116. struct request_queue *q = bdev_get_queue(bdev);
  117. int type = REQ_WRITE | REQ_SANITIZE;
  118. struct bio_batch bb;
  119. struct bio *bio;
  120. int ret = 0;
  121. if (!q)
  122. return -ENXIO;
  123. if (!blk_queue_sanitize(q)) {
  124. pr_err("%s - card doesn't support sanitize", __func__);
  125. return -EOPNOTSUPP;
  126. }
  127. bio = bio_alloc(gfp_mask, 1);
  128. if (!bio)
  129. return -ENOMEM;
  130. atomic_set(&bb.done, 1);
  131. bb.flags = 1 << BIO_UPTODATE;
  132. bb.wait = &wait;
  133. bio->bi_end_io = bio_batch_end_io;
  134. bio->bi_bdev = bdev;
  135. bio->bi_private = &bb;
  136. atomic_inc(&bb.done);
  137. submit_bio(type, bio);
  138. /* Wait for bios in-flight */
  139. if (!atomic_dec_and_test(&bb.done))
  140. wait_for_completion(&wait);
  141. if (!test_bit(BIO_UPTODATE, &bb.flags))
  142. ret = -EIO;
  143. return ret;
  144. }
  145. EXPORT_SYMBOL(blkdev_issue_sanitize);
  146. /**
  147. * blkdev_issue_zeroout - generate number of zero filed write bios
  148. * @bdev: blockdev to issue
  149. * @sector: start sector
  150. * @nr_sects: number of sectors to write
  151. * @gfp_mask: memory allocation flags (for bio_alloc)
  152. *
  153. * Description:
  154. * Generate and issue number of bios with zerofiled pages.
  155. */
  156. int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
  157. sector_t nr_sects, gfp_t gfp_mask)
  158. {
  159. int ret;
  160. struct bio *bio;
  161. struct bio_batch bb;
  162. unsigned int sz;
  163. DECLARE_COMPLETION_ONSTACK(wait);
  164. atomic_set(&bb.done, 1);
  165. bb.flags = 1 << BIO_UPTODATE;
  166. bb.wait = &wait;
  167. ret = 0;
  168. while (nr_sects != 0) {
  169. bio = bio_alloc(gfp_mask,
  170. min(nr_sects, (sector_t)BIO_MAX_PAGES));
  171. if (!bio) {
  172. ret = -ENOMEM;
  173. break;
  174. }
  175. bio->bi_sector = sector;
  176. bio->bi_bdev = bdev;
  177. bio->bi_end_io = bio_batch_end_io;
  178. bio->bi_private = &bb;
  179. while (nr_sects != 0) {
  180. sz = min((sector_t) PAGE_SIZE >> 9 , nr_sects);
  181. ret = bio_add_page(bio, ZERO_PAGE(0), sz << 9, 0);
  182. nr_sects -= ret >> 9;
  183. sector += ret >> 9;
  184. if (ret < (sz << 9))
  185. break;
  186. }
  187. ret = 0;
  188. atomic_inc(&bb.done);
  189. submit_bio(WRITE, bio);
  190. }
  191. /* Wait for bios in-flight */
  192. if (!atomic_dec_and_test(&bb.done))
  193. wait_for_completion(&wait);
  194. if (!test_bit(BIO_UPTODATE, &bb.flags))
  195. /* One of bios in the batch was completed with error.*/
  196. ret = -EIO;
  197. return ret;
  198. }
  199. EXPORT_SYMBOL(blkdev_issue_zeroout);