blk-lib.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. 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. /* Wait for bios in-flight */
  91. if (!atomic_dec_and_test(&bb.done))
  92. wait_for_completion(&wait);
  93. if (!test_bit(BIO_UPTODATE, &bb.flags))
  94. ret = -EIO;
  95. return ret;
  96. }
  97. EXPORT_SYMBOL(blkdev_issue_discard);
  98. /**
  99. * blkdev_issue_zeroout - generate number of zero filed write bios
  100. * @bdev: blockdev to issue
  101. * @sector: start sector
  102. * @nr_sects: number of sectors to write
  103. * @gfp_mask: memory allocation flags (for bio_alloc)
  104. *
  105. * Description:
  106. * Generate and issue number of bios with zerofiled pages.
  107. */
  108. int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
  109. sector_t nr_sects, gfp_t gfp_mask)
  110. {
  111. int ret;
  112. struct bio *bio;
  113. struct bio_batch bb;
  114. unsigned int sz;
  115. DECLARE_COMPLETION_ONSTACK(wait);
  116. atomic_set(&bb.done, 1);
  117. bb.flags = 1 << BIO_UPTODATE;
  118. bb.wait = &wait;
  119. ret = 0;
  120. while (nr_sects != 0) {
  121. bio = bio_alloc(gfp_mask,
  122. min(nr_sects, (sector_t)BIO_MAX_PAGES));
  123. if (!bio) {
  124. ret = -ENOMEM;
  125. break;
  126. }
  127. bio->bi_sector = sector;
  128. bio->bi_bdev = bdev;
  129. bio->bi_end_io = bio_batch_end_io;
  130. bio->bi_private = &bb;
  131. while (nr_sects != 0) {
  132. sz = min((sector_t) PAGE_SIZE >> 9 , nr_sects);
  133. ret = bio_add_page(bio, ZERO_PAGE(0), sz << 9, 0);
  134. nr_sects -= ret >> 9;
  135. sector += ret >> 9;
  136. if (ret < (sz << 9))
  137. break;
  138. }
  139. ret = 0;
  140. atomic_inc(&bb.done);
  141. submit_bio(WRITE, bio);
  142. }
  143. /* Wait for bios in-flight */
  144. if (!atomic_dec_and_test(&bb.done))
  145. wait_for_completion(&wait);
  146. if (!test_bit(BIO_UPTODATE, &bb.flags))
  147. /* One of bios in the batch was completed with error.*/
  148. ret = -EIO;
  149. return ret;
  150. }
  151. EXPORT_SYMBOL(blkdev_issue_zeroout);