Skip to content

Commit

Permalink
fsverity: pass pos and size to ->write_merkle_tree_block
Browse files Browse the repository at this point in the history
fsverity_operations::write_merkle_tree_block is passed the index of the
block to write and the log base 2 of the block size.  However, all
implementations of it use these parameters only to calculate the
position and the size of the block, in bytes.

Therefore, make ->write_merkle_tree_block take 'pos' and 'size'
parameters instead of 'index' and 'log_blocksize'.

Suggested-by: Dave Chinner <[email protected]>
Signed-off-by: Eric Biggers <[email protected]>
Acked-by: Dave Chinner <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
  • Loading branch information
ebiggers committed Jan 1, 2023
1 parent 9642946 commit 72ea15f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 24 deletions.
19 changes: 7 additions & 12 deletions fs/btrfs/verity.c
Original file line number Diff line number Diff line change
Expand Up @@ -783,30 +783,25 @@ static struct page *btrfs_read_merkle_tree_page(struct inode *inode,
/*
* fsverity op that writes a Merkle tree block into the btree.
*
* @inode: inode to write a Merkle tree block for
* @buf: Merkle tree data block to write
* @index: index of the block in the Merkle tree
* @log_blocksize: log base 2 of the Merkle tree block size
*
* Note that the block size could be different from the page size, so it is not
* safe to assume that index is a page index.
* @inode: inode to write a Merkle tree block for
* @buf: Merkle tree block to write
* @pos: the position of the block in the Merkle tree (in bytes)
* @size: the Merkle tree block size (in bytes)
*
* Returns 0 on success or negative error code on failure
*/
static int btrfs_write_merkle_tree_block(struct inode *inode, const void *buf,
u64 index, int log_blocksize)
u64 pos, unsigned int size)
{
u64 off = index << log_blocksize;
u64 len = 1ULL << log_blocksize;
loff_t merkle_pos = merkle_file_pos(inode);

if (merkle_pos < 0)
return merkle_pos;
if (merkle_pos > inode->i_sb->s_maxbytes - off - len)
if (merkle_pos > inode->i_sb->s_maxbytes - pos - size)
return -EFBIG;

return write_key_bytes(BTRFS_I(inode), BTRFS_VERITY_MERKLE_ITEM_KEY,
off, buf, len);
pos, buf, size);
}

const struct fsverity_operations btrfs_verityops = {
Expand Down
6 changes: 3 additions & 3 deletions fs/ext4/verity.c
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,11 @@ static struct page *ext4_read_merkle_tree_page(struct inode *inode,
}

static int ext4_write_merkle_tree_block(struct inode *inode, const void *buf,
u64 index, int log_blocksize)
u64 pos, unsigned int size)
{
loff_t pos = ext4_verity_metadata_pos(inode) + (index << log_blocksize);
pos += ext4_verity_metadata_pos(inode);

return pagecache_write(inode, buf, 1 << log_blocksize, pos);
return pagecache_write(inode, buf, size, pos);
}

const struct fsverity_operations ext4_verityops = {
Expand Down
6 changes: 3 additions & 3 deletions fs/f2fs/verity.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,11 @@ static struct page *f2fs_read_merkle_tree_page(struct inode *inode,
}

static int f2fs_write_merkle_tree_block(struct inode *inode, const void *buf,
u64 index, int log_blocksize)
u64 pos, unsigned int size)
{
loff_t pos = f2fs_verity_metadata_pos(inode) + (index << log_blocksize);
pos += f2fs_verity_metadata_pos(inode);

return pagecache_write(inode, buf, 1 << log_blocksize, pos);
return pagecache_write(inode, buf, size, pos);
}

const struct fsverity_operations f2fs_verityops = {
Expand Down
4 changes: 2 additions & 2 deletions fs/verity/enable.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ static int build_merkle_tree_level(struct file *filp, unsigned int level,
params->block_size - pending_size);
err = vops->write_merkle_tree_block(inode,
pending_hashes,
dst_block_num,
params->log_blocksize);
dst_block_num << params->log_blocksize,
params->block_size);
if (err) {
fsverity_err(inode,
"Error %d writing Merkle tree block %llu",
Expand Down
8 changes: 4 additions & 4 deletions include/linux/fsverity.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,17 @@ struct fsverity_operations {
* Write a Merkle tree block to the given inode.
*
* @inode: the inode for which the Merkle tree is being built
* @buf: block to write
* @index: 0-based index of the block within the Merkle tree
* @log_blocksize: log base 2 of the Merkle tree block size
* @buf: the Merkle tree block to write
* @pos: the position of the block in the Merkle tree (in bytes)
* @size: the Merkle tree block size (in bytes)
*
* This is only called between ->begin_enable_verity() and
* ->end_enable_verity().
*
* Return: 0 on success, -errno on failure
*/
int (*write_merkle_tree_block)(struct inode *inode, const void *buf,
u64 index, int log_blocksize);
u64 pos, unsigned int size);
};

#ifdef CONFIG_FS_VERITY
Expand Down

0 comments on commit 72ea15f

Please sign in to comment.