Skip to content

Commit a18b1ab

Browse files
Christoph HellwigEric Biggers
authored andcommitted
fscrypt: move fscrypt_set_bio_crypt_ctx_bh to buffer.c
fscrypt_set_bio_crypt_ctx_bh is only used by submit_bh_wbc now. Move it there and merge bh_get_inode_and_lblk_num into it. Note that this does not add ifdefs for fscrypt as the compiler will optimize away the dead code if it is not built in. Signed-off-by: Christoph Hellwig <hch@lst.de> Link: https://lore.kernel.org/r/20260302141922.370070-6-hch@lst.de Signed-off-by: Eric Biggers <ebiggers@kernel.org>
1 parent 588e7c0 commit a18b1ab

3 files changed

Lines changed: 20 additions & 55 deletions

File tree

fs/buffer.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2774,6 +2774,24 @@ static void end_bio_bh_io_sync(struct bio *bio)
27742774
bio_put(bio);
27752775
}
27762776

2777+
static void buffer_set_crypto_ctx(struct bio *bio, const struct buffer_head *bh,
2778+
gfp_t gfp_mask)
2779+
{
2780+
const struct address_space *mapping = folio_mapping(bh->b_folio);
2781+
const struct inode *inode;
2782+
u64 lblk;
2783+
2784+
/*
2785+
* The ext4 journal (jbd2) can submit a buffer_head it directly created
2786+
* for a non-pagecache page. fscrypt doesn't care about these.
2787+
*/
2788+
if (!mapping)
2789+
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);
2793+
}
2794+
27772795
static void submit_bh_wbc(blk_opf_t opf, struct buffer_head *bh,
27782796
enum rw_hint write_hint,
27792797
struct writeback_control *wbc)
@@ -2800,7 +2818,8 @@ static void submit_bh_wbc(blk_opf_t opf, struct buffer_head *bh,
28002818

28012819
bio = bio_alloc(bh->b_bdev, 1, opf, GFP_NOIO);
28022820

2803-
fscrypt_set_bio_crypt_ctx_bh(bio, bh, GFP_NOIO);
2821+
if (IS_ENABLED(CONFIG_FS_ENCRYPTION))
2822+
buffer_set_crypto_ctx(bio, bh, GFP_NOIO);
28042823

28052824
bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9);
28062825
bio->bi_write_hint = write_hint;

fs/crypto/inline_crypt.c

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -314,51 +314,6 @@ void fscrypt_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode,
314314
}
315315
EXPORT_SYMBOL_GPL(fscrypt_set_bio_crypt_ctx);
316316

317-
/* Extract the inode and logical block number from a buffer_head. */
318-
static bool bh_get_inode_and_lblk_num(const struct buffer_head *bh,
319-
const struct inode **inode_ret,
320-
u64 *lblk_num_ret)
321-
{
322-
struct folio *folio = bh->b_folio;
323-
const struct address_space *mapping;
324-
const struct inode *inode;
325-
326-
/*
327-
* The ext4 journal (jbd2) can submit a buffer_head it directly created
328-
* for a non-pagecache page. fscrypt doesn't care about these.
329-
*/
330-
mapping = folio_mapping(folio);
331-
if (!mapping)
332-
return false;
333-
inode = mapping->host;
334-
335-
*inode_ret = inode;
336-
*lblk_num_ret = (folio_pos(folio) + bh_offset(bh)) >> inode->i_blkbits;
337-
return true;
338-
}
339-
340-
/**
341-
* fscrypt_set_bio_crypt_ctx_bh() - prepare a file contents bio for inline
342-
* crypto
343-
* @bio: a bio which will eventually be submitted to the file
344-
* @first_bh: the first buffer_head for which I/O will be submitted
345-
* @gfp_mask: memory allocation flags
346-
*
347-
* Same as fscrypt_set_bio_crypt_ctx(), except this takes a buffer_head instead
348-
* of an inode and block number directly.
349-
*/
350-
void fscrypt_set_bio_crypt_ctx_bh(struct bio *bio,
351-
const struct buffer_head *first_bh,
352-
gfp_t gfp_mask)
353-
{
354-
const struct inode *inode;
355-
u64 first_lblk;
356-
357-
if (bh_get_inode_and_lblk_num(first_bh, &inode, &first_lblk))
358-
fscrypt_set_bio_crypt_ctx(bio, inode, first_lblk, gfp_mask);
359-
}
360-
EXPORT_SYMBOL_GPL(fscrypt_set_bio_crypt_ctx_bh);
361-
362317
/**
363318
* fscrypt_mergeable_bio() - test whether data can be added to a bio
364319
* @bio: the bio being built up

include/linux/fscrypt.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -869,10 +869,6 @@ void fscrypt_set_bio_crypt_ctx(struct bio *bio,
869869
const struct inode *inode, u64 first_lblk,
870870
gfp_t gfp_mask);
871871

872-
void fscrypt_set_bio_crypt_ctx_bh(struct bio *bio,
873-
const struct buffer_head *first_bh,
874-
gfp_t gfp_mask);
875-
876872
bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode,
877873
u64 next_lblk);
878874

@@ -891,11 +887,6 @@ static inline void fscrypt_set_bio_crypt_ctx(struct bio *bio,
891887
const struct inode *inode,
892888
u64 first_lblk, gfp_t gfp_mask) { }
893889

894-
static inline void fscrypt_set_bio_crypt_ctx_bh(
895-
struct bio *bio,
896-
const struct buffer_head *first_bh,
897-
gfp_t gfp_mask) { }
898-
899890
static inline bool fscrypt_mergeable_bio(struct bio *bio,
900891
const struct inode *inode,
901892
u64 next_lblk)

0 commit comments

Comments
 (0)