1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright 2022 Red Hat, Inc.
7 #include <linux/blk-crypto.h>
8 #include <linux/blk-integrity.h>
12 static inline bool dm_bvec_iter_rewind(const struct bio_vec *bv,
13 struct bvec_iter *iter,
18 iter->bi_size += bytes;
19 if (bytes <= iter->bi_bvec_done) {
20 iter->bi_bvec_done -= bytes;
24 bytes -= iter->bi_bvec_done;
25 idx = iter->bi_idx - 1;
27 while (idx >= 0 && bytes && bytes > bv[idx].bv_len) {
28 bytes -= bv[idx].bv_len;
32 if (WARN_ONCE(idx < 0 && bytes,
33 "Attempted to rewind iter beyond bvec's boundaries\n")) {
34 iter->bi_size -= bytes;
35 iter->bi_bvec_done = 0;
41 iter->bi_bvec_done = bv[idx].bv_len - bytes;
45 #if defined(CONFIG_BLK_DEV_INTEGRITY)
48 * dm_bio_integrity_rewind - Rewind integrity vector
49 * @bio: bio whose integrity vector to update
50 * @bytes_done: number of data bytes to rewind
52 * Description: This function calculates how many integrity bytes the
53 * number of completed data bytes correspond to and rewind the
54 * integrity vector accordingly.
56 static void dm_bio_integrity_rewind(struct bio *bio, unsigned int bytes_done)
58 struct bio_integrity_payload *bip = bio_integrity(bio);
59 struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
60 unsigned bytes = bio_integrity_bytes(bi, bytes_done >> 9);
62 bip->bip_iter.bi_sector -= bio_integrity_intervals(bi, bytes_done >> 9);
63 dm_bvec_iter_rewind(bip->bip_vec, &bip->bip_iter, bytes);
66 #else /* CONFIG_BLK_DEV_INTEGRITY */
68 static inline void dm_bio_integrity_rewind(struct bio *bio,
69 unsigned int bytes_done)
76 #if defined(CONFIG_BLK_INLINE_ENCRYPTION)
78 /* Decrements @dun by @dec, treating @dun as a multi-limb integer. */
79 static void dm_bio_crypt_dun_decrement(u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE],
84 for (i = 0; dec && i < BLK_CRYPTO_DUN_ARRAY_SIZE; i++) {
95 static void dm_bio_crypt_rewind(struct bio *bio, unsigned int bytes)
97 struct bio_crypt_ctx *bc = bio->bi_crypt_context;
99 dm_bio_crypt_dun_decrement(bc->bc_dun,
100 bytes >> bc->bc_key->data_unit_size_bits);
103 #else /* CONFIG_BLK_INLINE_ENCRYPTION */
105 static inline void dm_bio_crypt_rewind(struct bio *bio, unsigned int bytes)
112 static inline void dm_bio_rewind_iter(const struct bio *bio,
113 struct bvec_iter *iter, unsigned int bytes)
115 iter->bi_sector -= bytes >> 9;
117 /* No advance means no rewind */
118 if (bio_no_advance_iter(bio))
119 iter->bi_size += bytes;
121 dm_bvec_iter_rewind(bio->bi_io_vec, iter, bytes);
125 * dm_bio_rewind - update ->bi_iter of @bio by rewinding @bytes.
126 * @bio: bio to rewind
127 * @bytes: how many bytes to rewind
130 * Caller must ensure that @bio has a fixed end sector, to allow
131 * rewinding from end of bio and restoring its original position.
132 * Caller is also responsibile for restoring bio's size.
134 static void dm_bio_rewind(struct bio *bio, unsigned bytes)
136 if (bio_integrity(bio))
137 dm_bio_integrity_rewind(bio, bytes);
139 if (bio_has_crypt_ctx(bio))
140 dm_bio_crypt_rewind(bio, bytes);
142 dm_bio_rewind_iter(bio, &bio->bi_iter, bytes);
145 void dm_io_rewind(struct dm_io *io, struct bio_set *bs)
147 struct bio *orig = io->orig_bio;
148 struct bio *new_orig = bio_alloc_clone(orig->bi_bdev, orig,
151 * dm_bio_rewind can restore to previous position since the
152 * end sector is fixed for original bio, but we still need
153 * to restore bio's size manually (using io->sectors).
155 dm_bio_rewind(new_orig, ((io->sector_offset << 9) -
156 orig->bi_iter.bi_size));
157 bio_trim(new_orig, 0, io->sectors);
159 bio_chain(new_orig, orig);
161 * __bi_remaining was increased (by dm_split_and_process_bio),
162 * so must drop the one added in bio_chain.
164 atomic_dec(&orig->__bi_remaining);
165 io->orig_bio = new_orig;