blk-lib.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 (q->limits.discard_granularity) {
  55. unsigned int disc_sects = q->limits.discard_granularity >> 9;
  56. max_discard_sectors &= ~(disc_sects - 1);
  57. }
  58. if (flags & BLKDEV_DISCARD_SECURE) {
  59. if (!blk_queue_secdiscard(q))
  60. return -EOPNOTSUPP;
  61. type |= REQ_SECURE;
  62. }
  63. atomic_set(&bb.done, 1);
  64. bb.flags = 1 << BIO_UPTODATE;
  65. bb.wait = &wait;
  66. while (nr_sects) {
  67. bio = bio_alloc(gfp_mask, 1);
  68. if (!bio) {
  69. ret = -ENOMEM;
  70. break;
  71. }
  72. bio->bi_sector = sector;
  73. bio->bi_end_io = bio_batch_end_io;
  74. bio->bi_bdev = bdev;
  75. bio->bi_private = &bb;
  76. if (nr_sects > max_discard_sectors) {
  77. bio->bi_size = max_discard_sectors << 9;
  78. nr_sects -= max_discard_sectors;
  79. sector += max_discard_sectors;
  80. } else {
  81. bio->bi_size = nr_sects << 9;
  82. nr_sects = 0;
  83. }
  84. atomic_inc(&bb.done);
  85. submit_bio(type, bio);
  86. }
  87. /* Wait for bios in-flight */
  88. if (!atomic_dec_and_test(&bb.done))
  89. wait_for_completion(&wait);
  90. if (!test_bit(BIO_UPTODATE, &bb.flags))
  91. ret = -EIO;
  92. return ret;
  93. }
  94. EXPORT_SYMBOL(blkdev_issue_discard);
  95. /**
  96. * blkdev_issue_zeroout - generate number of zero filed write bios
  97. * @bdev: blockdev to issue
  98. * @sector: start sector
  99. * @nr_sects: number of sectors to write
  100. * @gfp_mask: memory allocation flags (for bio_alloc)
  101. *
  102. * Description:
  103. * Generate and issue number of bios with zerofiled pages.
  104. */
  105. int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
  106. sector_t nr_sects, gfp_t gfp_mask)
  107. {
  108. int ret;
  109. struct bio *bio;
  110. struct bio_batch bb;
  111. unsigned int sz;
  112. DECLARE_COMPLETION_ONSTACK(wait);
  113. atomic_set(&bb.done, 1);
  114. bb.flags = 1 << BIO_UPTODATE;
  115. bb.wait = &wait;
  116. ret = 0;
  117. while (nr_sects != 0) {
  118. bio = bio_alloc(gfp_mask,
  119. min(nr_sects, (sector_t)BIO_MAX_PAGES));
  120. if (!bio) {
  121. ret = -ENOMEM;
  122. break;
  123. }
  124. bio->bi_sector = sector;
  125. bio->bi_bdev = bdev;
  126. bio->bi_end_io = bio_batch_end_io;
  127. bio->bi_private = &bb;
  128. while (nr_sects != 0) {
  129. sz = min((sector_t) PAGE_SIZE >> 9 , nr_sects);
  130. ret = bio_add_page(bio, ZERO_PAGE(0), sz << 9, 0);
  131. nr_sects -= ret >> 9;
  132. sector += ret >> 9;
  133. if (ret < (sz << 9))
  134. break;
  135. }
  136. ret = 0;
  137. atomic_inc(&bb.done);
  138. submit_bio(WRITE, bio);
  139. }
  140. /* Wait for bios in-flight */
  141. if (!atomic_dec_and_test(&bb.done))
  142. wait_for_completion(&wait);
  143. if (!test_bit(BIO_UPTODATE, &bb.flags))
  144. /* One of bios in the batch was completed with error.*/
  145. ret = -EIO;
  146. return ret;
  147. }
  148. EXPORT_SYMBOL(blkdev_issue_zeroout);