Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * fs/f2fs/verity.c: fs-verity support for f2fs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright 2019 Google LLC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * Implementation of fsverity_operations for f2fs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * Like ext4, f2fs stores the verity metadata (Merkle tree and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * fsverity_descriptor) past the end of the file, starting at the first 64K
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * boundary beyond i_size.  This approach works because (a) verity files are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * readonly, and (b) pages fully beyond i_size aren't visible to userspace but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * can be read/written internally by f2fs with only some relatively small
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * changes to f2fs.  Extended attributes cannot be used because (a) f2fs limits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * the total size of an inode's xattr entries to 4096 bytes, which wouldn't be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * enough for even a single Merkle tree block, and (b) f2fs encryption doesn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * encrypt xattrs, yet the verity metadata *must* be encrypted when the file is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * because it contains hashes of the plaintext data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * Using a 64K boundary rather than a 4K one keeps things ready for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * architectures with 64K pages, and it doesn't necessarily waste space on-disk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * since there can be a hole between i_size and the start of the Merkle tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <linux/f2fs_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include "f2fs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include "xattr.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define F2FS_VERIFY_VER	(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) static inline loff_t f2fs_verity_metadata_pos(const struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	return round_up(inode->i_size, 65536);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  * Read some verity metadata from the inode.  __vfs_read() can't be used because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * we need to read beyond i_size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) static int pagecache_read(struct inode *inode, void *buf, size_t count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 			  loff_t pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	while (count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		size_t n = min_t(size_t, count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 				 PAGE_SIZE - offset_in_page(pos));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		void *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		page = read_mapping_page(inode->i_mapping, pos >> PAGE_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 					 NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		if (IS_ERR(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 			return PTR_ERR(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		addr = kmap_atomic(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		memcpy(buf, addr + offset_in_page(pos), n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		kunmap_atomic(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		buf += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		pos += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		count -= n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71)  * Write some verity metadata to the inode for FS_IOC_ENABLE_VERITY.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72)  * kernel_write() can't be used because the file descriptor is readonly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) static int pagecache_write(struct inode *inode, const void *buf, size_t count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 			   loff_t pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	if (pos + count > inode->i_sb->s_maxbytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		return -EFBIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	while (count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		size_t n = min_t(size_t, count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 				 PAGE_SIZE - offset_in_page(pos));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		void *fsdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		void *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		res = pagecache_write_begin(NULL, inode->i_mapping, pos, n, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 					    &page, &fsdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 			return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		addr = kmap_atomic(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		memcpy(addr + offset_in_page(pos), buf, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		kunmap_atomic(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		res = pagecache_write_end(NULL, inode->i_mapping, pos, n, n,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 					  page, fsdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		if (res < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 			return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		if (res != n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 			return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		buf += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		pos += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		count -= n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)  * Format of f2fs verity xattr.  This points to the location of the verity
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)  * descriptor within the file data rather than containing it directly because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)  * the verity descriptor *must* be encrypted when f2fs encryption is used.  But,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)  * f2fs encryption does not encrypt xattrs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) struct fsverity_descriptor_location {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	__le32 version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	__le32 size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	__le64 pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static int f2fs_begin_enable_verity(struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	struct inode *inode = file_inode(filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	if (f2fs_verity_in_progress(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	if (f2fs_is_atomic_file(inode) || f2fs_is_volatile_file(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	 * Since the file was opened readonly, we have to initialize the quotas
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	 * here and not rely on ->open() doing it.  This must be done before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	 * evicting the inline data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	err = dquot_initialize(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	err = f2fs_convert_inline_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	set_inode_flag(inode, FI_VERITY_IN_PROGRESS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static int f2fs_end_enable_verity(struct file *filp, const void *desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 				  size_t desc_size, u64 merkle_tree_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	struct inode *inode = file_inode(filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	u64 desc_pos = f2fs_verity_metadata_pos(inode) + merkle_tree_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	struct fsverity_descriptor_location dloc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		.version = cpu_to_le32(F2FS_VERIFY_VER),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		.size = cpu_to_le32(desc_size),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		.pos = cpu_to_le64(desc_pos),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	int err = 0, err2 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	 * If an error already occurred (which fs/verity/ signals by passing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	 * desc == NULL), then only clean-up is needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	if (desc == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	/* Append the verity descriptor. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	err = pagecache_write(inode, desc, desc_size, desc_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	 * Write all pages (both data and verity metadata).  Note that this must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	 * happen before clearing FI_VERITY_IN_PROGRESS; otherwise pages beyond
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	 * i_size won't be written properly.  For crash consistency, this also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	 * must happen before the verity inode flag gets persisted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	err = filemap_write_and_wait(inode->i_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	/* Set the verity xattr. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_VERITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 			    F2FS_XATTR_NAME_VERITY, &dloc, sizeof(dloc),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 			    NULL, XATTR_CREATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	/* Finally, set the verity inode flag. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	file_set_verity(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	f2fs_set_inode_flags(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	f2fs_mark_inode_dirty_sync(inode, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	clear_inode_flag(inode, FI_VERITY_IN_PROGRESS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	 * Verity failed to be enabled, so clean up by truncating any verity
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	 * metadata that was written beyond i_size (both from cache and from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	 * disk) and clearing FI_VERITY_IN_PROGRESS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	 * Taking i_gc_rwsem[WRITE] is needed to stop f2fs garbage collection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	 * from re-instantiating cached pages we are truncating (since unlike
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	 * normal file accesses, garbage collection isn't limited by i_size).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	truncate_inode_pages(inode->i_mapping, inode->i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	err2 = f2fs_truncate(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	if (err2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		f2fs_err(sbi, "Truncating verity metadata failed (errno=%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 			 err2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		set_sbi_flag(sbi, SBI_NEED_FSCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	clear_inode_flag(inode, FI_VERITY_IN_PROGRESS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	return err ?: err2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) static int f2fs_get_verity_descriptor(struct inode *inode, void *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 				      size_t buf_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	struct fsverity_descriptor_location dloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	u32 size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	u64 pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	/* Get the descriptor location */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	res = f2fs_getxattr(inode, F2FS_XATTR_INDEX_VERITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 			    F2FS_XATTR_NAME_VERITY, &dloc, sizeof(dloc), NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	if (res < 0 && res != -ERANGE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	if (res != sizeof(dloc) || dloc.version != cpu_to_le32(F2FS_VERIFY_VER)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		f2fs_warn(F2FS_I_SB(inode), "unknown verity xattr format");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	size = le32_to_cpu(dloc.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	pos = le64_to_cpu(dloc.pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	/* Get the descriptor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	if (pos + size < pos || pos + size > inode->i_sb->s_maxbytes ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	    pos < f2fs_verity_metadata_pos(inode) || size > INT_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		f2fs_warn(F2FS_I_SB(inode), "invalid verity xattr");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	if (buf_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		if (size > buf_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 			return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		res = pagecache_read(inode, buf, size, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 			return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) static struct page *f2fs_read_merkle_tree_page(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 					       pgoff_t index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 					       unsigned long num_ra_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	DEFINE_READAHEAD(ractl, NULL, inode->i_mapping, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	index += f2fs_verity_metadata_pos(inode) >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	page = find_get_page_flags(inode->i_mapping, index, FGP_ACCESSED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	if (!page || !PageUptodate(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		if (page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 			put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		else if (num_ra_pages > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 			page_cache_ra_unbounded(&ractl, num_ra_pages, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		page = read_mapping_page(inode->i_mapping, index, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	return page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) static int f2fs_write_merkle_tree_block(struct inode *inode, const void *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 					u64 index, int log_blocksize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	loff_t pos = f2fs_verity_metadata_pos(inode) + (index << log_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	return pagecache_write(inode, buf, 1 << log_blocksize, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) const struct fsverity_operations f2fs_verityops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	.begin_enable_verity	= f2fs_begin_enable_verity,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	.end_enable_verity	= f2fs_end_enable_verity,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	.get_verity_descriptor	= f2fs_get_verity_descriptor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	.read_merkle_tree_page	= f2fs_read_merkle_tree_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	.write_merkle_tree_block = f2fs_write_merkle_tree_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) };