Skip to content

Commit 3c7eaa7

Browse files
Christoph HellwigEric Biggers
authored andcommitted
fscrypt: pass a byte offset to fscrypt_set_bio_crypt_ctx
Logical offsets into an inode are usually expressed as bytes in the VFS. Switch fscrypt_set_bio_crypt_ctx to that convention. Signed-off-by: Christoph Hellwig <hch@lst.de> Link: https://lore.kernel.org/r/20260302141922.370070-9-hch@lst.de Signed-off-by: Eric Biggers <ebiggers@kernel.org>
1 parent 22be86a commit 3c7eaa7

8 files changed

Lines changed: 21 additions & 26 deletions

File tree

fs/buffer.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2778,18 +2778,15 @@ static void buffer_set_crypto_ctx(struct bio *bio, const struct buffer_head *bh,
27782778
gfp_t gfp_mask)
27792779
{
27802780
const struct address_space *mapping = folio_mapping(bh->b_folio);
2781-
const struct inode *inode;
2782-
u64 lblk;
27832781

27842782
/*
27852783
* The ext4 journal (jbd2) can submit a buffer_head it directly created
27862784
* for a non-pagecache page. fscrypt doesn't care about these.
27872785
*/
27882786
if (!mapping)
27892787
return;
2790-
inode = mapping->host;
2791-
lblk = (folio_pos(bh->b_folio) + bh_offset(bh)) >> inode->i_blkbits;
2792-
fscrypt_set_bio_crypt_ctx(bio, inode, lblk, gfp_mask);
2788+
fscrypt_set_bio_crypt_ctx(bio, mapping->host,
2789+
folio_pos(bh->b_folio) + bh_offset(bh), gfp_mask);
27932790
}
27942791

27952792
static void submit_bh_wbc(blk_opf_t opf, struct buffer_head *bh,

fs/crypto/bio.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ static int fscrypt_zeroout_range_inline_crypt(const struct inode *inode,
7575
{
7676
const unsigned int blockbits = inode->i_blkbits;
7777
const unsigned int blocks_per_page = 1 << (PAGE_SHIFT - blockbits);
78+
loff_t pos = (loff_t)lblk << blockbits;
7879
struct fscrypt_zero_done done = {
7980
.pending = ATOMIC_INIT(1),
8081
.done = COMPLETION_INITIALIZER_ONSTACK(done.done),
@@ -89,7 +90,7 @@ static int fscrypt_zeroout_range_inline_crypt(const struct inode *inode,
8990
bio->bi_iter.bi_sector = sector;
9091
bio->bi_private = &done;
9192
bio->bi_end_io = fscrypt_zeroout_range_end_io;
92-
fscrypt_set_bio_crypt_ctx(bio, inode, lblk, GFP_NOFS);
93+
fscrypt_set_bio_crypt_ctx(bio, inode, pos, GFP_NOFS);
9394

9495
for (n = 0; n < BIO_MAX_VECS; n++) {
9596
unsigned int blocks_this_page =
@@ -98,10 +99,9 @@ static int fscrypt_zeroout_range_inline_crypt(const struct inode *inode,
9899

99100
__bio_add_page(bio, ZERO_PAGE(0), bytes_this_page, 0);
100101
len -= blocks_this_page;
101-
lblk += blocks_this_page;
102+
pos += bytes_this_page;
102103
sector += (bytes_this_page >> SECTOR_SHIFT);
103-
if (!len || !fscrypt_mergeable_bio(bio, inode,
104-
(loff_t)lblk << blockbits))
104+
if (!len || !fscrypt_mergeable_bio(bio, inode, pos))
105105
break;
106106
}
107107

fs/crypto/inline_crypt.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ static void fscrypt_generate_dun(const struct fscrypt_inode_info *ci,
285285
* fscrypt_set_bio_crypt_ctx() - prepare a file contents bio for inline crypto
286286
* @bio: a bio which will eventually be submitted to the file
287287
* @inode: the file's inode
288-
* @first_lblk: the first file logical block number in the I/O
288+
* @pos: the first file position (in bytes) in the I/O
289289
* @gfp_mask: memory allocation flags - these must be a waiting mask so that
290290
* bio_crypt_set_ctx can't fail.
291291
*
@@ -298,7 +298,7 @@ static void fscrypt_generate_dun(const struct fscrypt_inode_info *ci,
298298
* The encryption context will be freed automatically when the bio is freed.
299299
*/
300300
void fscrypt_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode,
301-
u64 first_lblk, gfp_t gfp_mask)
301+
loff_t pos, gfp_t gfp_mask)
302302
{
303303
const struct fscrypt_inode_info *ci;
304304
u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE];
@@ -307,7 +307,7 @@ void fscrypt_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode,
307307
return;
308308
ci = fscrypt_get_inode_info_raw(inode);
309309

310-
fscrypt_generate_dun(ci, first_lblk << inode->i_blkbits, dun);
310+
fscrypt_generate_dun(ci, pos, dun);
311311
bio_crypt_set_ctx(bio, ci->ci_enc_key.blk_key, dun, gfp_mask);
312312
}
313313
EXPORT_SYMBOL_GPL(fscrypt_set_bio_crypt_ctx);

fs/ext4/page-io.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -427,9 +427,8 @@ static void io_submit_init_bio(struct ext4_io_submit *io,
427427
* __GFP_DIRECT_RECLAIM is set, see comments for bio_alloc_bioset().
428428
*/
429429
bio = bio_alloc(bh->b_bdev, BIO_MAX_VECS, REQ_OP_WRITE, GFP_NOIO);
430-
fscrypt_set_bio_crypt_ctx(bio, inode,
431-
(folio_pos(folio) + bh_offset(bh)) >> inode->i_blkbits,
432-
GFP_NOIO);
430+
fscrypt_set_bio_crypt_ctx(bio, inode, folio_pos(folio) + bh_offset(bh),
431+
GFP_NOIO);
433432
bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9);
434433
bio->bi_end_io = ext4_end_bio;
435434
bio->bi_private = ext4_get_io_end(io->io_end);

fs/ext4/readpage.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,8 @@ static int ext4_mpage_readpages(struct inode *inode, struct fsverity_info *vi,
355355
*/
356356
bio = bio_alloc(bdev, bio_max_segs(nr_pages),
357357
REQ_OP_READ, GFP_KERNEL);
358-
fscrypt_set_bio_crypt_ctx(bio, inode, next_block,
359-
GFP_KERNEL);
358+
fscrypt_set_bio_crypt_ctx(bio, inode,
359+
(loff_t)next_block << blkbits, GFP_KERNEL);
360360
ext4_set_bio_post_read_ctx(bio, inode, vi);
361361
bio->bi_iter.bi_sector = first_block << (blkbits - 9);
362362
bio->bi_end_io = mpage_end_io;

fs/f2fs/data.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,9 @@ static void f2fs_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode,
527527
* read/write raw data without encryption.
528528
*/
529529
if (!fio || !fio->encrypted_page)
530-
fscrypt_set_bio_crypt_ctx(bio, inode, first_idx, gfp_mask);
530+
fscrypt_set_bio_crypt_ctx(bio, inode,
531+
(loff_t)first_idx << inode->i_blkbits,
532+
gfp_mask);
531533
}
532534

533535
static bool f2fs_crypt_mergeable_bio(struct bio *bio, const struct inode *inode,

fs/iomap/direct-io.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,7 @@ static int iomap_dio_zero(const struct iomap_iter *iter, struct iomap_dio *dio,
311311

312312
bio = iomap_dio_alloc_bio(iter, dio, nr_vecs,
313313
REQ_OP_WRITE | REQ_SYNC | REQ_IDLE);
314-
fscrypt_set_bio_crypt_ctx(bio, inode, pos >> inode->i_blkbits,
315-
GFP_KERNEL);
314+
fscrypt_set_bio_crypt_ctx(bio, inode, pos, GFP_KERNEL);
316315
bio->bi_iter.bi_sector = iomap_sector(&iter->iomap, pos);
317316
bio->bi_private = dio;
318317
bio->bi_end_io = iomap_dio_bio_end_io;
@@ -342,8 +341,7 @@ static ssize_t iomap_dio_bio_iter_one(struct iomap_iter *iter,
342341
nr_vecs = bio_iov_vecs_to_alloc(dio->submit.iter, BIO_MAX_VECS);
343342

344343
bio = iomap_dio_alloc_bio(iter, dio, nr_vecs, op);
345-
fscrypt_set_bio_crypt_ctx(bio, iter->inode,
346-
pos >> iter->inode->i_blkbits, GFP_KERNEL);
344+
fscrypt_set_bio_crypt_ctx(bio, iter->inode, pos, GFP_KERNEL);
347345
bio->bi_iter.bi_sector = iomap_sector(&iter->iomap, pos);
348346
bio->bi_write_hint = iter->inode->i_write_hint;
349347
bio->bi_ioprio = dio->iocb->ki_ioprio;

include/linux/fscrypt.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -865,9 +865,8 @@ static inline void fscrypt_set_ops(struct super_block *sb,
865865

866866
bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode);
867867

868-
void fscrypt_set_bio_crypt_ctx(struct bio *bio,
869-
const struct inode *inode, u64 first_lblk,
870-
gfp_t gfp_mask);
868+
void fscrypt_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode,
869+
loff_t pos, gfp_t gfp_mask);
871870

872871
bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode,
873872
loff_t pos);
@@ -885,7 +884,7 @@ static inline bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode)
885884

886885
static inline void fscrypt_set_bio_crypt_ctx(struct bio *bio,
887886
const struct inode *inode,
888-
u64 first_lblk, gfp_t gfp_mask) { }
887+
loff_t pos, gfp_t gfp_mask) { }
889888

890889
static inline bool fscrypt_mergeable_bio(struct bio *bio,
891890
const struct inode *inode,

0 commit comments

Comments
 (0)