[PATCH 08/13] fsverity: expose merkle tree geometry to callers

Darrick J. Wong
2024-03-30T00:34:46Z

From: Darrick J. Wong <djwong@kernel.org>

Create a function that will return selected information about the
geometry of the merkle tree. Online fsck for XFS will need this piece
to perform basic checks of the merkle tree.

Signed-off-by: Darrick J. Wong <djwong@kernel.org>
---
fs/verity/open.c | 26 ++++++++++++++++++++++++++
include/linux/fsverity.h | 3 +++
2 files changed, 29 insertions(+)

diff --git a/fs/verity/open.c b/fs/verity/open.c
index 9603b3a404f74..7a86407732c41 100644
--- a/fs/verity/open.c
+++ b/fs/verity/open.c
@@ -412,6 +412,32 @@ void __fsverity_cleanup_inode(struct inode *inode)
}
EXPORT_SYMBOL_GPL(__fsverity_cleanup_inode);

+/**
+ * fsverity_merkle_tree_geometry() - return Merkle tree geometry
+ * @inode: the inode for which the Merkle tree is being built
+ * @block_size: size of a merkle tree block, in bytes
+ * @tree_size: size of the merkle tree, in bytes
+ */
+int fsverity_merkle_tree_geometry(struct inode *inode, unsigned int *block_size,
+ u64 *tree_size)
+{
+ struct fsverity_info *vi;
+ int error;
+
+ if (!IS_VERITY(inode))
+ return -EOPNOTSUPP;
+
+ error = ensure_verity_info(inode);
+ if (error)
+ return error;
+
+ vi = fsverity_get_info(inode);
+ *block_size = vi->tree_params.block_size;
+ *tree_size = vi->tree_params.tree_size;
+ return 0;
+}
+EXPORT_SYMBOL_GPL(fsverity_merkle_tree_geometry);
+
void __init fsverity_init_info_cache(void)
{
fsverity_info_cachep = KMEM_CACHE_USERCOPY(
diff --git a/include/linux/fsverity.h b/include/linux/fsverity.h
index 030d7094d80fc..5b1485a842983 100644
--- a/include/linux/fsverity.h
+++ b/include/linux/fsverity.h
@@ -245,6 +245,9 @@ int __fsverity_file_open(struct inode *inode, struct file *filp);
int __fsverity_prepare_setattr(struct dentry *dentry, struct iattr *attr);
void __fsverity_cleanup_inode(struct inode *inode);

+int fsverity_merkle_tree_geometry(struct inode *inode, unsigned int *block_size,
+ u64 *tree_size);
+
/**
* fsverity_cleanup_inode() - free the inode's verity info, if present
* @inode: an inode being evicted