[PATCH 05/24] fsverity: expose merkle tree geometry to callers

Andrey Albershteyn
2024-12-29T13:39:58Z

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 | 37 +++++++++++++++++++++++++++++++++++++
include/linux/fsverity.h | 11 +++++++++++
2 files changed, 48 insertions(+)

diff --git a/fs/verity/open.c b/fs/verity/open.c
index fdeb95eca3af..de1d0bd6e703 100644
--- a/fs/verity/open.c
+++ b/fs/verity/open.c
@@ -407,6 +407,43 @@ void __fsverity_cleanup_inode(struct inode *inode)
}
EXPORT_SYMBOL_GPL(__fsverity_cleanup_inode);

+/**
+ * fsverity_merkle_tree_geometry() - return Merkle tree geometry
+ * @inode: the inode to query
+ * @block_size: will be set to the log2 of the size of a merkle tree block
+ * @block_size: will be set to the size of a merkle tree block, in bytes
+ * @tree_size: will be set to the size of the merkle tree, in bytes
+ *
+ * Callers are not required to have opened the file.
+ *
+ * Return: 0 for success, -ENODATA if verity is not enabled, or any of the
+ * error codes that can result from loading verity information while opening a
+ * file.
+ */
+int fsverity_merkle_tree_geometry(struct inode *inode, u8 *log_blocksize,
+ unsigned int *block_size, u64 *tree_size)
+{
+ struct fsverity_info *vi;
+ int error;
+
+ if (!IS_VERITY(inode))
+ return -ENODATA;
+
+ error = ensure_verity_info(inode);
+ if (error)
+ return error;
+
+ vi = inode->i_verity_info;
+ if (log_blocksize)
+ *log_blocksize = vi->tree_params.log_blocksize;
+ if (block_size)
+ *block_size = vi->tree_params.block_size;
+ if (tree_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 81b07909d783..8627b11082b0 100644
--- a/include/linux/fsverity.h
+++ b/include/linux/fsverity.h
@@ -157,6 +157,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, u8 *log_blocksize,
+ unsigned int *block_size, u64 *tree_size);
+
/**
* fsverity_cleanup_inode() - free the inode's verity info, if present
* @inode: an inode being evicted
@@ -229,6 +232,14 @@ static inline void fsverity_cleanup_inode(struct inode *inode)
{
}

+static inline int fsverity_merkle_tree_geometry(struct inode *inode,
+ u8 *log_blocksize,
+ unsigned int *block_size,
+ u64 *tree_size)
+{
+ return -EOPNOTSUPP;
+}
+
/* read_metadata.c */

static inline int fsverity_ioctl_read_metadata(struct file *filp,
--
2.47.0