From: Christoph Hellwig Date: Wed, 28 Jan 2026 15:26:18 +0000 (+0100) Subject: fsverity: start consolidating pagecache code X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=821ddd25fbe88ea60e9c35cfb76c2ddeb1ffae26;p=thirdparty%2Fkernel%2Flinux.git fsverity: start consolidating pagecache code ext4 and f2fs are largely using the same code to read a page full of Merkle tree blocks from the page cache, and the upcoming xfs fsverity support would add another copy. Move the ext4 code to fs/verity/ and use it in f2fs as well. For f2fs this removes the previous f2fs-specific error injection, but otherwise the behavior remains unchanged. Signed-off-by: Christoph Hellwig Reviewed-by: Andrey Albershteyn Reviewed-by: Jan Kara Reviewed-by: "Darrick J. Wong" Link: https://lore.kernel.org/r/20260128152630.627409-7-hch@lst.de Signed-off-by: Eric Biggers --- diff --git a/fs/ext4/verity.c b/fs/ext4/verity.c index 2ce4cf8a1e314..a071860ad36ae 100644 --- a/fs/ext4/verity.c +++ b/fs/ext4/verity.c @@ -361,23 +361,8 @@ static struct page *ext4_read_merkle_tree_page(struct inode *inode, pgoff_t index, unsigned long num_ra_pages) { - struct folio *folio; - index += ext4_verity_metadata_pos(inode) >> PAGE_SHIFT; - - folio = __filemap_get_folio(inode->i_mapping, index, FGP_ACCESSED, 0); - if (IS_ERR(folio) || !folio_test_uptodate(folio)) { - DEFINE_READAHEAD(ractl, NULL, NULL, inode->i_mapping, index); - - if (!IS_ERR(folio)) - folio_put(folio); - else if (num_ra_pages > 1) - page_cache_ra_unbounded(&ractl, num_ra_pages, 0); - folio = read_mapping_folio(inode->i_mapping, index, NULL); - if (IS_ERR(folio)) - return ERR_CAST(folio); - } - return folio_file_page(folio, index); + return generic_read_merkle_tree_page(inode, index, num_ra_pages); } static int ext4_write_merkle_tree_block(struct file *file, const void *buf, diff --git a/fs/f2fs/verity.c b/fs/f2fs/verity.c index c1c4d80446818..d37e584423afe 100644 --- a/fs/f2fs/verity.c +++ b/fs/f2fs/verity.c @@ -259,23 +259,8 @@ static struct page *f2fs_read_merkle_tree_page(struct inode *inode, pgoff_t index, unsigned long num_ra_pages) { - struct folio *folio; - index += f2fs_verity_metadata_pos(inode) >> PAGE_SHIFT; - - folio = f2fs_filemap_get_folio(inode->i_mapping, index, FGP_ACCESSED, 0); - if (IS_ERR(folio) || !folio_test_uptodate(folio)) { - DEFINE_READAHEAD(ractl, NULL, NULL, inode->i_mapping, index); - - if (!IS_ERR(folio)) - folio_put(folio); - else if (num_ra_pages > 1) - page_cache_ra_unbounded(&ractl, num_ra_pages, 0); - folio = read_mapping_folio(inode->i_mapping, index, NULL); - if (IS_ERR(folio)) - return ERR_CAST(folio); - } - return folio_file_page(folio, index); + return generic_read_merkle_tree_page(inode, index, num_ra_pages); } static int f2fs_write_merkle_tree_block(struct file *file, const void *buf, diff --git a/fs/verity/Makefile b/fs/verity/Makefile index 435559a4fa9ea..ddb4a88a0d608 100644 --- a/fs/verity/Makefile +++ b/fs/verity/Makefile @@ -5,6 +5,7 @@ obj-$(CONFIG_FS_VERITY) += enable.o \ init.o \ measure.o \ open.o \ + pagecache.o \ read_metadata.o \ verify.o diff --git a/fs/verity/pagecache.c b/fs/verity/pagecache.c new file mode 100644 index 0000000000000..01c652bc802f3 --- /dev/null +++ b/fs/verity/pagecache.c @@ -0,0 +1,38 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright 2019 Google LLC + */ + +#include +#include + +/** + * generic_read_merkle_tree_page - generic ->read_merkle_tree_page helper + * @inode: inode containing the Merkle tree + * @index: 0-based index of the Merkle tree page in the inode + * @num_ra_pages: The number of Merkle tree pages that should be prefetched. + * + * The caller needs to adjust @index from the Merkle-tree relative index passed + * to ->read_merkle_tree_page to the actual index where the Merkle tree is + * stored in the page cache for @inode. + */ +struct page *generic_read_merkle_tree_page(struct inode *inode, pgoff_t index, + unsigned long num_ra_pages) +{ + struct folio *folio; + + folio = __filemap_get_folio(inode->i_mapping, index, FGP_ACCESSED, 0); + if (IS_ERR(folio) || !folio_test_uptodate(folio)) { + DEFINE_READAHEAD(ractl, NULL, NULL, inode->i_mapping, index); + + if (!IS_ERR(folio)) + folio_put(folio); + else if (num_ra_pages > 1) + page_cache_ra_unbounded(&ractl, num_ra_pages, 0); + folio = read_mapping_folio(inode->i_mapping, index, NULL); + if (IS_ERR(folio)) + return ERR_CAST(folio); + } + return folio_file_page(folio, index); +} +EXPORT_SYMBOL_GPL(generic_read_merkle_tree_page); diff --git a/include/linux/fsverity.h b/include/linux/fsverity.h index ac5c98fd4602f..8ddaa87fece3a 100644 --- a/include/linux/fsverity.h +++ b/include/linux/fsverity.h @@ -309,4 +309,7 @@ static inline int fsverity_file_open(struct inode *inode, struct file *filp) void fsverity_cleanup_inode(struct inode *inode); +struct page *generic_read_merkle_tree_page(struct inode *inode, pgoff_t index, + unsigned long num_ra_pages); + #endif /* _LINUX_FSVERITY_H */