1 // SPDX-License-Identifier: GPL-2.0-only
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 int 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)
75 #if defined(CONFIG_BLK_INLINE_ENCRYPTION)
77 /* Decrements @dun by @dec, treating @dun as a multi-limb integer. */
78 static void dm_bio_crypt_dun_decrement(u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE],
83 for (i = 0; dec && i < BLK_CRYPTO_DUN_ARRAY_SIZE; i++) {
94 static void dm_bio_crypt_rewind(struct bio *bio, unsigned int bytes)
96 struct bio_crypt_ctx *bc = bio->bi_crypt_context;
98 dm_bio_crypt_dun_decrement(bc->bc_dun,
99 bytes >> bc->bc_key->data_unit_size_bits);
102 #else /* CONFIG_BLK_INLINE_ENCRYPTION */
104 static inline void dm_bio_crypt_rewind(struct bio *bio, unsigned int bytes)
110 static inline void dm_bio_rewind_iter(const struct bio *bio,
111 struct bvec_iter *iter, unsigned int bytes)
113 iter->bi_sector -= bytes >> 9;
115 /* No advance means no rewind */
116 if (bio_no_advance_iter(bio))
117 iter->bi_size += bytes;
119 dm_bvec_iter_rewind(bio->bi_io_vec, iter, bytes);
123 * dm_bio_rewind - update ->bi_iter of @bio by rewinding @bytes.
124 * @bio: bio to rewind
125 * @bytes: how many bytes to rewind
128 * Caller must ensure that @bio has a fixed end sector, to allow
129 * rewinding from end of bio and restoring its original position.
130 * Caller is also responsibile for restoring bio's size.
132 static void dm_bio_rewind(struct bio *bio, unsigned int bytes)
134 if (bio_integrity(bio))
135 dm_bio_integrity_rewind(bio, bytes);
137 if (bio_has_crypt_ctx(bio))
138 dm_bio_crypt_rewind(bio, bytes);
140 dm_bio_rewind_iter(bio, &bio->bi_iter, bytes);
143 void dm_io_rewind(struct dm_io *io, struct bio_set *bs)
145 struct bio *orig = io->orig_bio;
146 struct bio *new_orig = bio_alloc_clone(orig->bi_bdev, orig,
149 * dm_bio_rewind can restore to previous position since the
150 * end sector is fixed for original bio, but we still need
151 * to restore bio's size manually (using io->sectors).
153 dm_bio_rewind(new_orig, ((io->sector_offset << 9) -
154 orig->bi_iter.bi_size));
155 bio_trim(new_orig, 0, io->sectors);
157 bio_chain(new_orig, orig);
159 * __bi_remaining was increased (by dm_split_and_process_bio),
160 * so must drop the one added in bio_chain.
162 atomic_dec(&orig->__bi_remaining);
163 io->orig_bio = new_orig;