dm-io-rewind.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright 2022 Red Hat, Inc.
  4. */
  5. #include <linux/bio.h>
  6. #include <linux/blk-crypto.h>
  7. #include <linux/blk-integrity.h>
  8. #include "dm-core.h"
  9. static inline bool dm_bvec_iter_rewind(const struct bio_vec *bv,
  10. struct bvec_iter *iter,
  11. unsigned int bytes)
  12. {
  13. int idx;
  14. iter->bi_size += bytes;
  15. if (bytes <= iter->bi_bvec_done) {
  16. iter->bi_bvec_done -= bytes;
  17. return true;
  18. }
  19. bytes -= iter->bi_bvec_done;
  20. idx = iter->bi_idx - 1;
  21. while (idx >= 0 && bytes && bytes > bv[idx].bv_len) {
  22. bytes -= bv[idx].bv_len;
  23. idx--;
  24. }
  25. if (WARN_ONCE(idx < 0 && bytes,
  26. "Attempted to rewind iter beyond bvec's boundaries\n")) {
  27. iter->bi_size -= bytes;
  28. iter->bi_bvec_done = 0;
  29. iter->bi_idx = 0;
  30. return false;
  31. }
  32. iter->bi_idx = idx;
  33. iter->bi_bvec_done = bv[idx].bv_len - bytes;
  34. return true;
  35. }
  36. #if defined(CONFIG_BLK_DEV_INTEGRITY)
  37. /**
  38. * dm_bio_integrity_rewind - Rewind integrity vector
  39. * @bio: bio whose integrity vector to update
  40. * @bytes_done: number of data bytes to rewind
  41. *
  42. * Description: This function calculates how many integrity bytes the
  43. * number of completed data bytes correspond to and rewind the
  44. * integrity vector accordingly.
  45. */
  46. static void dm_bio_integrity_rewind(struct bio *bio, unsigned int bytes_done)
  47. {
  48. struct bio_integrity_payload *bip = bio_integrity(bio);
  49. struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
  50. unsigned int bytes = bio_integrity_bytes(bi, bytes_done >> 9);
  51. bip->bip_iter.bi_sector -= bio_integrity_intervals(bi, bytes_done >> 9);
  52. dm_bvec_iter_rewind(bip->bip_vec, &bip->bip_iter, bytes);
  53. }
  54. #else /* CONFIG_BLK_DEV_INTEGRITY */
  55. static inline void dm_bio_integrity_rewind(struct bio *bio,
  56. unsigned int bytes_done)
  57. {
  58. return;
  59. }
  60. #endif
  61. #if defined(CONFIG_BLK_INLINE_ENCRYPTION)
  62. /* Decrements @dun by @dec, treating @dun as a multi-limb integer. */
  63. static void dm_bio_crypt_dun_decrement(u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE],
  64. unsigned int dec)
  65. {
  66. int i;
  67. for (i = 0; dec && i < BLK_CRYPTO_DUN_ARRAY_SIZE; i++) {
  68. u64 prev = dun[i];
  69. dun[i] -= dec;
  70. if (dun[i] > prev)
  71. dec = 1;
  72. else
  73. dec = 0;
  74. }
  75. }
  76. static void dm_bio_crypt_rewind(struct bio *bio, unsigned int bytes)
  77. {
  78. struct bio_crypt_ctx *bc = bio->bi_crypt_context;
  79. dm_bio_crypt_dun_decrement(bc->bc_dun,
  80. bytes >> bc->bc_key->data_unit_size_bits);
  81. }
  82. #else /* CONFIG_BLK_INLINE_ENCRYPTION */
  83. static inline void dm_bio_crypt_rewind(struct bio *bio, unsigned int bytes)
  84. {
  85. return;
  86. }
  87. #endif
  88. static inline void dm_bio_rewind_iter(const struct bio *bio,
  89. struct bvec_iter *iter, unsigned int bytes)
  90. {
  91. iter->bi_sector -= bytes >> 9;
  92. /* No advance means no rewind */
  93. if (bio_no_advance_iter(bio))
  94. iter->bi_size += bytes;
  95. else
  96. dm_bvec_iter_rewind(bio->bi_io_vec, iter, bytes);
  97. }
  98. /**
  99. * dm_bio_rewind - update ->bi_iter of @bio by rewinding @bytes.
  100. * @bio: bio to rewind
  101. * @bytes: how many bytes to rewind
  102. *
  103. * WARNING:
  104. * Caller must ensure that @bio has a fixed end sector, to allow
  105. * rewinding from end of bio and restoring its original position.
  106. * Caller is also responsibile for restoring bio's size.
  107. */
  108. static void dm_bio_rewind(struct bio *bio, unsigned int bytes)
  109. {
  110. if (bio_integrity(bio))
  111. dm_bio_integrity_rewind(bio, bytes);
  112. if (bio_has_crypt_ctx(bio))
  113. dm_bio_crypt_rewind(bio, bytes);
  114. dm_bio_rewind_iter(bio, &bio->bi_iter, bytes);
  115. }
  116. void dm_io_rewind(struct dm_io *io, struct bio_set *bs)
  117. {
  118. struct bio *orig = io->orig_bio;
  119. struct bio *new_orig = bio_alloc_clone(orig->bi_bdev, orig,
  120. GFP_NOIO, bs);
  121. /*
  122. * dm_bio_rewind can restore to previous position since the
  123. * end sector is fixed for original bio, but we still need
  124. * to restore bio's size manually (using io->sectors).
  125. */
  126. dm_bio_rewind(new_orig, ((io->sector_offset << 9) -
  127. orig->bi_iter.bi_size));
  128. bio_trim(new_orig, 0, io->sectors);
  129. bio_chain(new_orig, orig);
  130. /*
  131. * __bi_remaining was increased (by dm_split_and_process_bio),
  132. * so must drop the one added in bio_chain.
  133. */
  134. atomic_dec(&orig->__bi_remaining);
  135. io->orig_bio = new_orig;
  136. }