fs-verity support for XFS with post EOF merkle tree
Subsystems (6)
v12 cover fs-verity support for XFS with post EOF merkle tree
Hi all,
This is next revision of fsverity for XFS. This revision includes fixes for issues found by sashiko.dev (and claude-code scan).
Below are my responses to the sashiko points.
Range-diff with v11 also below.
Patches 5, 6, 7, and 11 are new.
Patches without review:
[PATCH v12 05/21] fsverity: improve flushing performance of [PATCH v12 06/21] fsverity: don't allow setting DAX file attribute on [PATCH v12 07/21] fs,fsverity: remove check for fsverity being [PATCH v12 10/21] xfs: disable direct read path for fs-verity files [PATCH v12 11/21] xfs: don't report dio_mem_align and [PATCH v12 12/21] xfs: handle fsverity I/O in write/read path [PATCH v12 14/21] xfs: add fs-verity support
[PATCH v12 15/21] xfs: remove unwritten extents after preallocations
This series based on v7.2-rc4
kernel:
https://git.kernel.org/pub/scm/linux/kernel/git/aalbersh/xfs-linux.git/log/?h=fsverity
xfsprogs:
https://git.kernel.org/pub/scm/linux/kernel/git/aalbersh/xfsprogs-dev.git/log/?h=fsverity
xfstests:
https://git.kernel.org/pub/scm/linux/kernel/git/aalbersh/xfstests-dev.git/log/?h=fsverity
sashiko review:
https://sashiko.dev/#/patchset/20260710085256.3464201-1-aalbersh%40kernel.org
v11:
https://lore.kernel.org/all/[email protected]/
v10:
https://lore.kernel.org/fsverity/[email protected]/#r
v9:
https://lore.kernel.org/fsverity/[email protected]/#r
---
Changes in v12:
- Refactored xfs_fsverity_cancel_unwritten()
- Switched to using inode_set_flags()
- Add a lock for COW fork reading
- Add pagecache truncation in cleanup path
- Added ERANGE and EBADMSG to fsverity scrub handling
- Add missing XFS_FSVERITY_CONSTRUCTION in various xfs_iomap
- Rebase to -rc3
Changes in v11:
- Drop wrong overlayfs patch
- Drop already merged iomap and fsverity patches
- Update to I_INO() use instead of ip->i_ino
- Sashiko.dev fixes. See list of issues below.
Changes in v10:
- Rebase to v7.1-rc3 with relevant adjustments
- Initialize ioend->io_vi to NULL to not get write work onto verity wq
- Range diff below
Changes in v9:
- Fix fsverity_fill_zerohash() parameter names
- A few fixes found by sashiko.dev:
- Replace ip->i_mount->m_attr_geo->blksize with m_sb.sb_blocksize
- Don't call xfs_trans_cancel() after xfs_trans_commit() in xfs_fsverity_end_enable()
- Call xfs_fsverity_delete_metadata() if verity enable failed
- Change start/end type from xfs_fileoff_t to loff_t
- Return xfs_trans_commit() error from
xfs_fsverity_cancel_unwritten()
Changes in v8:
- Return fsverity_ensure_verity_info() errors from
ovl_ensure_verity_loaded()
Changes in v7:
- Move kerneldoc to fsverity_ensure_verity_info() definition
- Drop patch adding XFS traces
- Fix overly long line in the comment
- Make order of fserror and fsverity_error consistent
- Add overlay patch converting to fsverity_ensure_verity_info()
Changes in v6:
- Removed stub for fsverity_ensure_verity_info() as it's optimized out
- Rename fsverity_folio_zero_hash() to fsverify_fill_zerohash()
- Merge patches 8 to 10 into one
- Merge patch gerating zero_hash and fsverity_fill_zerohash() into one
- Add kerneldoc to fsverity_ensure_verity_info()
- Add comments to iomap_block_needs_zeroing()
Changes in v5:
- Add fserror_report_data_lost() for data blocks in page spanning EOF
- Issue fsverity metadata readahead in data readahead
- iomap_fsverity_write() return type fix
- Use of S_ISREG(mode)
- Make 65536 #define instead of open-coded
- Use transaction per unwritten extent removal
- Fetch fsverity_info for all fsverity metadata
- Revert fsverity_folio_zero_hash() stub as used in iomap
- Extend cancel_unwritten to whole file range to remove cow leftovers
- Drop delayed allocation on the COW fork on fsverity completion
Changes in v4:
- Use fserror interface in fsverity instead of fs callback
- Hoist pagecache_read from f2fs/ext4 to fsverity
- Refactor iomap code
- Fetch fsverity_info only for file data and merkle tree holes
- Do not disable preallocation, remove unwritten extents instead
- Offload fsverity hash I/O to fsverity workqueue in read path
- Store merkle tree at round_up(i_size, 64k)
- Add a spacing between merkle tree and fsverity descriptor as next 64k
aligned block
- Squash helpers into first user commits
- Squash on-disk format changes into single commit
- Drop different offset for pagecache/on-disk
- Don't zero out pages in higher order folios in write path
- Link to v3: https://lore.kernel.org/fsverity/[email protected]/T/#t
Changes in v3:
- Different on-disk and pagecache offset
- Use read path ioends
- Switch to hashtable fsverity info
- Synthesize merkle tree blocks full of zeroes
- Other minor refactors
- Link to v2: https://lore.kernel.org/fsverity/20260114164210.GO15583@frogsfrogsfrogs/T/#t
Changes in v2:
- Move to VFS interface for merkle tree block reading
- Drop patchset for per filesystem workqueues
- Change how offsets of the descriptor and tree metadata is calculated
- Store fs-verity descriptor in data fork side by side with merkle tree
- Simplify iomap changes, remove interface for post eof read/write
- Get rid of extended attribute implementation
- Link to v1: https://lore.kernel.org/r/[email protected]
-- >8 --
https://sashiko.dev/#/patchset/20260710085256.3464201-1-aalbersh%40kernel.org?part=7
Quoted 6 lines
Should the online scrub and repair utilities also be updated to handle
XFS_DIFLAG2_VERITY?
Whilexfs_dinode_verify()correctly catches invalid verity flags, it appears the scrub and repair paths infs/xfs/scrub/inode_repair.candfs/xfs/scrub/inode.cwere not updated.
If an inode on disk is corrupted with theXFS_DIFLAG2_VERITYbit set when it
Quoted 8 lines
should not be,
xfs_dinode_verify()will correctly fail. When online scrub attempts to repair this viaxrep_dinode_core()callingxrep_dinode_flags(), the invalid flag is never cleared becausexrep_dinode_flags()lacks the clearing logic forXFS_DIFLAG2_VERITY(unlike REFLINK or DAX). When the repair finishes andxfs_trans_log_buf()attempts to write the repaired inode,xfs_dinode_verify()will trigger again, see the still-present invalid verity flag, and reject the write. Can this cause XFS to forcefully shut down the filesystem during periodic automated online scrub jobs?
The scrub and repair are updated latter in the series and removes this flag if fsverity verification of the file fails.
--------------------------------------------------------------------------------
https://sashiko.dev/#/patchset/20260710085256.3464201-1-aalbersh%40kernel.org?part=8
Quoted 16 lines
Does calling
fsverity_file_open()here trigger a NULL pointer dereference oninode->i_sb->s_vop?
If a crafted XFS image is mounted containing an inode with theXFS_DIFLAG2_VERITYflag set,IS_VERITY(inode)will evaluate to true. This triggers the following path:xfs_file_open()
fsverity_file_open()__fsverity_file_open() fsverity_ensure_verity_info() fsverity_get_descriptor() In fsverity_get_descriptor(), there is an unconditional dereference: fs/verity/open.c:fsverity_get_descriptor() { ... res = inode->i_sb->s_vop->get_verity_descriptor(inode, NULL, 0); ... }
Quoted 3 lines
Since the XFS superblock's
s_voppointer does not appear to be initialized in this patch, could this result in a panic when attempting to open the file?
This will not show up during bisect, but this is fine to move latter in the series when s_vop is initialized.
--------------------------------------------------------------------------------
https://sashiko.dev/#/patchset/20260710085256.3464201-1-aalbersh%40kernel.org?part=10
Quoted 9 lines
Does XFS actually fall back to buffered I/O for fsverity files? Looking at
xfs_file_read_iter()infs/xfs/xfs_file.c, it doesn't appear to checkfsverity_active()before routing the read to the direct I/O path:fs/xfs/xfs_file.c:xfs_file_read_iter() { ... else if (iocb->ki_flags & IOCB_DIRECT) ret = xfs_file_dio_read(iocb, to); ... }
Quoted 5 lines
If an attacker opens an fs-verity protected file with
O_DIRECT, won't this bypass the page cache andfsverity_verify_folio()entirely, allowing them to read unauthenticated data?
While this patch correctly zeroes out the alignment fields in statx, it doesn't seem to stop an application from issuing anO_DIRECTread anyway.
Again miss-ordered patches, swapped them so fallback patch is first one.
--------------------------------------------------------------------------------
https://sashiko.dev/#/patchset/20260710085256.3464201-1-aalbersh%40kernel.org?part=12
Quoted 8 lines
Will this check fail if the newly allocated metadata extent is merged with a preceding data extent?
Since XFS can aggressively merge contiguous extents, the mergedbr_startoffmight become the start of the data extent, making it less than the metadata offset.
If the check fails andIOMAP_F_FSVERITYis omitted, won't the generic iomap writeback code see a folio past EOF without the verity flag and discard it, causing silent loss of the Merkle tree?
I don't think it's the case. The extent merge could only happen when file size is exactly 64k and merkle tree extent got allocated in the next physical block. The delayed extent or unwritten extent merge is not the case as data is written back before building the tree.
Then, if merge happens the read path doesn't care about this as iomap/xfs read metadata based on file offset.
The merge could probably happen for merkle tree/descriptor but this is also fine as XFS gets descriptor based on the last block of the extent.
Quoted 3 lines
Does this face the same extent merging issue as the check above? If the new metadata extent merges with adjacent data blocks, the offset check will fail and drop the flag.
The same as above.
Quoted 12 lines
Should the source mapping in
xfs_zoned_buffered_write_iomap_begin()include theIOMAP_F_FSVERITYflag instead of hardcoding 0 for theiomap_flagsargument?
Looking at the unaligned write fallback later in this function:fs/xfs/xfs_iomap.c:xfs_zoned_buffered_write_iomap_begin() { ... if (!IS_ALIGNED(offset, mp->m_sb.sb_blocksize) || ... ) { ... error = xfs_bmbt_to_iomap(ip, srcmap, &smap, flags, 0, xfs_iomap_inode_sequence(ip, 0)); ... }
Quoted 5 lines
During an unaligned write, read-modify-write requires the source mapping. If the
IOMAP_F_FSVERITYflag is missing on the srcmap, the iomap layer might assume blocks past EOF should be zeroed.
Could this unintentionally zero out previously written Merkle tree blocks and corrupt the fs-verity metadata?
This one looks valid, pass iomap_flags to xfs_bmbt_to_iomap().
Quoted 8 lines
Does this fallback to
xfs_direct_write_iomap_begin()drop the newly assignedIOMAP_F_FSVERITYflag?
If a file has an extent size hint configured, the delegation skips passing theiomap_flagsvariable entirely.
Without this flag, could the generic iomap write code updatei_sizewhen appending the Merkle tree past EOF, unintentionally exposing the internal fs-verity metadata to userspace?
[ ... ]
This is actually highlighted the problem that inodes with extent hints won't correctly build merkle tree. Enabling this will require to implement fsverity in direct i/o path. So, added a check that fsverity can not be enabled on inode with extent hints.
Quoted 8 lines
What happens if a read operation (like a large readahead) starts in the data region but spans across the boundary into the fs-verity metadata region?
If the starting offset is less than the metadata offset,IOMAP_F_FSVERITYwon't be set. Because the returned iomap isn't truncated at the metadata boundary, it will cover the metadata region without the required flag. Will the generic iomap read code then incorrectly zero the folios pasti_sizein the page cache instead of reading the Merkle tree from disk?
Normal readahead will stop at EOF.
--------------------------------------------------------------------------------
https://sashiko.dev/#/patchset/20260710085256.3464201-1-aalbersh%40kernel.org?part=13
Quoted 9 lines
Does this completely bypass verification for merged ioends, causing a regression where corrupted data is read as valid?
When large contiguous reads are split into multiple bios by the block layer,xfs_end_io()merges the tail ioends into the head ioend'sio_listviaiomap_ioend_try_merge().
Sincexfs_end_ioend_read()only passes the head'sio_biotofsverity_verify_bio(), does it leave the merged tail bios completely unverified beforeiomap_finish_ioends()loops through the entireio_listand marks all of their folios as uptodate?
iomap_ioend_can_merge() returns false for read ioends. So, no merge occurs for these.
Quoted 9 lines
Does this strictly serialize fs-verity verification per-inode, causing a severe performance regression?
Unlike ext4 and f2fs which allocate awork_structper bio to allow parallel hashing across CPUs, this reuses the singleip->i_ioend_workper inode. When multiple concurrent reads to the same file complete, they are appended toip->i_ioend_list. A single worker thread runningxfs_end_io()iterates over them and sequentially calls the CPU-intensivefsverity_verify_bio(). Could this limit fs-verity throughput for a single file to the hashing speed of a single CPU core?
Yes this is true. This could probably be improved for all read path I/O. As far as I understood, the history is that there was high lock and transaction reservation usage on random writes. With fsverity ioends we don't need a lock so that could be improved.
Quoted 4 lines
Can this result in an unresolvable self-deadlock regression when the block device has integrity checksums enabled?
Ifbdev_has_integrity_csum()is true,xfs_get_iomap_read_ops()forces metadata reads to usexfs_iomap_read_ops.
Quoted 8 lines
When a data read completes, it queues the per-inode
ip->i_ioend_work. The worker callsxfs_end_io()->xfs_end_ioend_read()->fsverity_verify_bio(). Iffsverity_verify_bio()synchronously submits a read for a Merkle tree page, that metadata read completion will eventually callxfs_end_bio()and attempt to queue the exact sameip->i_ioend_workstruct while the worker is blocked waiting for the folio lock.
Since the work is already executing, the workqueue subsystem merely marks it pending, leaving the worker indefinitely blocked.
If we already got into BIO verification, work item is already executing (the pending flag cleared). So, the work item can be scheduled again. In pending case, the item will be added to ioend list.
So, no deadlock.
--------------------------------------------------------------------------------
https://sashiko.dev/#/patchset/20260710085256.3464201-1-aalbersh%40kernel.org?part=14
Quoted 7 lines
Does this leave stale dirty pages in the page cache?
If fsverity enablement fails after the Merkle tree blocks have been written to the page cache,xfs_fsverity_delete_metadata()removes the physical extents on disk viaxfs_itruncate_extents(). However, it doesn't seem to invalidate the corresponding dirty pages in the page cache.
When writeback later attempts to flush these past-EOF dirty pages, could it try to write to extents that no longer exist, causing XFS writeback to fail?
Hmm I added this in previous version but looks like I lost it during squashing. Added again.
Quoted 7 lines
Does initializing
ip->i_cowfpwithout holdingXFS_ILOCK_EXCLintroduce a data race?
Inxfs_fsverity_end_enable(),xfs_ifork_init_cow()is called without holdingXFS_ILOCK_EXCL. Background processes like XFS online scrub takeXFS_ILOCK_SHAREDto safely inspect inode forks.
If scrub runs concurrently, could it seeip->i_cowfp!= NULL but read an uninitializedif_formatdue to a lack of memory barriers, resulting in a crash?
This is valid, add a lock.
Quoted 5 lines
Does direct bitwise modification of
inode->i_flagscreate a data race? AlthoughXFS_IOLOCK_EXCLis held, other VFS paths might modify or readi_flagsunderinode->i_lock. Modifying the flags using a raw bitwise OR can lead to lost flag updates or torn reads. Should this useinode_set_flags()to ensure atomicity under the inode lock?
Yes, the inode_set_flags() need to be used to match the fsverity_active().
Quoted 13 lines
Does the error path cause a double-unlock of
XFS_ILOCK_EXCL?fs/xfs/xfs_fsverity.c:xfs_fsverity_cancel_unwritten() { ... xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); error = xfs_bunmapi(tp, ip, imap.br_startoff, imap.br_blockcount, 0, 1, &done); if (error) goto out_cancel; ... out_cancel: xfs_trans_cancel(tp); xfs_iunlock(ip, XFS_ILOCK_EXCL); ... }
Quoted 3 lines
Calling
xfs_trans_cancel()automatically unlocks the joined inode, but the code then explicitly callsxfs_iunlock(ip, XFS_ILOCK_EXCL). Could this cause a kernel panic due to a double-unlock?
Refactored to handle transaction/locking correctly
Quoted 7 lines
Does this unconditionally advance past incomplete unmap operations?
fs/xfs/xfs_fsverity.c:xfs_fsverity_cancel_unwritten() { ... error = xfs_bunmapi(tp, ip, imap.br_startoff, imap.br_blockcount, 0, 1, &done); ... offset_fsb = imap.br_startoff + imap.br_blockcount; }
Quoted 5 lines
xfs_bunmapi()limits the number of blocks it unmaps in a single transaction. If it unmaps a partial extent, it sets the done parameter to 0. However, this loop seems to ignore the done variable and unconditionally advancesoffset_fsb. If fsverity encounters a large unwritten extent, could this leave garbage unwritten metadata blocks on disk?
Added while(!done) loop
--------------------------------------------------------------------------------
https://sashiko.dev/#/patchset/20260710085256.3464201-1-aalbersh%40kernel.org?part=15
Quoted 11 lines
Does this error path cause a double unlock of the inode if
xfs_bunmapi()fails?
Ifxfs_bmapi_read()fails earlier in the loop, jumping toout_cancelworks correctly because the transaction doesn't own the lock yet, so the explicitxfs_iunlock()correctly drops it.
However, ifxfs_bunmapi()fails,xfs_trans_ijoin()has already been called withXFS_ILOCK_EXCL, transferring ownership of the inode lock to the transaction.
Whenxfs_trans_cancel()runs in this scenario, it aborts the transaction and automatically drops any joined locks. Wouldn't the subsequentxfs_iunlock()then try to drop the same lock a second time?
Yes, fixed above.
--------------------------------------------------------------------------------
https://sashiko.dev/#/patchset/20260710085256.3464201-1-aalbersh%40kernel.org?part=16
Quoted 17 lines
Does exposing this ioctl introduce a path to a double unlock regression? When
fsverity_ioctl_enable()is called, it can eventually reach the error path inxfs_fsverity_cancel_unwritten()ifxfs_bunmapi()fails:fs/xfs/xfs_fsverity.c:xfs_fsverity_cancel_unwritten() { ... if (imap.br_state == XFS_EXT_UNWRITTEN) { xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); error = xfs_bunmapi(tp, ip, imap.br_startoff, imap.br_blockcount, 0, 1, &done); if (error) goto out_cancel; ... out_cancel: xfs_trans_cancel(tp); xfs_iunlock(ip, XFS_ILOCK_EXCL); return error; }
Quoted 5 lines
Because the inode was already joined to the transaction with
XFS_ILOCK_EXCL, wouldn't the call toxfs_trans_cancel()automatically release the ILOCK? If so, the subsequent unconditional call toxfs_iunlock()would result in a double unlock of the rwsem. This could cause an atomic counter underflow, leading to lock state corruption or deadlocks.
Yes, the same as above.
--------------------------------------------------------------------------------
https://sashiko.dev/#/patchset/20260710085256.3464201-1-aalbersh%40kernel.org?part=18
Quoted 9 lines
Does this code handle all metadata corruption errors? If a file has a verity descriptor with an invalid size,
xfs_fsverity_get_descriptor()can return-ERANGE:fs/xfs/xfs_fsverity.c:xfs_fsverity_get_descriptor() { ... if (XFS_IS_CORRUPT(mp, desc_size > FS_VERITY_MAX_DESCRIPTOR_SIZE)) return -ERANGE; ... }
Quoted 5 lines
Similarly, if the PKCS#7 signature is invalid, fsverity can return
-EBADMSG. Without-ERANGEand-EBADMSGin this switch statement, they fall through to the default case. This causes the scrub session to abort and propagate the error toxchk_prepare_iscrub()instead of flagging the inode for repair. Should these codes be added to the list of validation errors?
These two make sense, the other returned by pkcs#7 doesn't seem suitable for repair (missing key, key rejected).
--------------------------------------------------------------------------------
Quoted 7 lines
Can clearing
S_VERITYon a live inode race with VFS open?
When opening a verity file,fsverity_file_open()evaluatesIS_VERITY(inode)without holding the inode's IOLOCK. If it evaluates to true, it callsfsverity_ensure_verity_info(), which eventually entersxfs_fsverity_get_descriptor().
Ifxrep_inode_verity()clears theS_VERITYflag concurrently,xfs_fsverity_get_descriptor()will trigger an assertion failure:
Quoted 8 lines
fs/xfs/xfs_fsverity.c:xfs_fsverity_get_descriptor() { ... ASSERT(inode->i_flags & S_VERITY); xfs_ilock(ip, XFS_ILOCK_SHARED); ... }Could this lead to a kernel panic (on debug builds) or use of a partially-cleared verity inode?
This is valid, let's use inode_set_flags() to match the fsverity_active().
--------------------------------------------------------------------------------
https://sashiko.dev/#/patchset/20260710085256.3464201-1-aalbersh%40kernel.org?part=20
Quoted 11 lines
Does this unconditional inclusion allow modifying verity files when
CONFIG_FS_VERITYis disabled?
When a kernel is built withoutCONFIG_FS_VERITY, XFS will mount the filesystem read-write instead of falling back to read-only mode, becauseXFS_SB_FEAT_RO_COMPAT_VERITYis unconditionally included here.
If a user with write permissions callstruncate()on a verity file,open(O_WRONLY)is blocked by a fallback returning-EOPNOTSUPP, buttruncate()bypasses this and reachessetattr_prepare().
BecauseCONFIG_FS_VERITYis disabled,setattr_prepare()skips theIS_VERITY()check. XFS would then proceed with the truncate, freeing all blocks past EOF, which could silently destroy the verity Merkle tree and descriptor.
This is valid, addressed with a fix.
--------------------------------------------------------------------------------
-: ------------ > 1: f950c862b409 fs-verity support for XFS with post EOF merkle tree
1: c6ec0d36e610 = 2: c4f1b8cf00c9 fsverity: report validation errors through fserror to fsnotify
2: c1bc4e09e99d = 3: 7c2c5311c9f6 fsverity: expose ensure_fsverity_info()
3: 46fa567c5396 = 4: 8055613f1a4d fsverity: pass digest size and hash of the all-zeroes block to ->write
4: bf674d6447c6 = 5: 9545ca3c3399 fsverity: hoist pagecache_read from f2fs/ext4 to fsverity
5: 881ca13124f5 = 6: cd46c76fe3b4 fsverity: improve flushing performance of fsverity_fill_zerohash
6: 70a3a18d0535 = 7: e8f9dddb89b9 fsverity: don't allow setting DAX file attribute on fsverity files
-: ------------ > 8: 3ca252cd6be2 fs,fsverity: remove check for fsverity being enabled in setattr_prepare()
7: 8985c8159373 = 9: a0dc2713a247 xfs: introduce fsverity on-disk changes
9: f3b835598dfd = 10: f58409354927 xfs: don't allow to enable DAX on fs-verity sealed inode
11: a75ae14156a5 = 11: de88296b352d xfs: disable direct read path for fs-verity files
10: bfe9d1a76b5d = 12: d0d20df8ad7c xfs: don't report dio_mem_align and dio_offset_align for fsverity files
12: 873908b75477 ! 13: a125d7c824f9 xfs: handle fsverity I/O in write/read path
@@ fs/xfs/xfs_iomap.c
#define XFS_ALLOC_ALIGN(mp, off) \
(((off) >> mp->m_allocsize_log) << mp->m_allocsize_log)
+@@ fs/xfs/xfs_iomap.c: xfs_direct_write_iomap_begin(
+ if (flags & IOMAP_ATOMIC)
+ iomap_flags |= IOMAP_F_ATOMIC_BIO;
+
++ if (xfs_iflags_test(ip, XFS_VERITY_CONSTRUCTION))
++ iomap_flags |= IOMAP_F_FSVERITY;
++
+ /*
+ * COW writes may allocate delalloc space or convert unwritten COW
+ * extents, so we need to make sure to take the lock exclusively here.
@@ fs/xfs/xfs_iomap.c: xfs_zoned_buffered_write_iomap_begin(
loff_t count,
unsigned flags,
@@ fs/xfs/xfs_iomap.c: xfs_zoned_buffered_write_iomap_begin(
xfs_filblks_t count_fsb;
xfs_extlen_t indlen;
@@ fs/xfs/xfs_iomap.c: xfs_zoned_buffered_write_iomap_begin(
- struct xfs_iext_cursor icur;
- int error = 0;
+ smap.br_startoff + smap.br_blockcount);
+ xfs_trim_extent(&smap, offset_fsb,
+ end_fsb - offset_fsb);
+- error = xfs_bmbt_to_iomap(ip, srcmap, &smap, flags, 0,
++ error = xfs_bmbt_to_iomap(ip, srcmap, &smap, flags,
++ iomap_flags,
+ xfs_iomap_inode_sequence(ip, 0));
+ if (error)
+ goto out_unlock;
+@@ fs/xfs/xfs_iomap.c: xfs_zoned_buffered_write_iomap_begin(
+ got.br_state = XFS_EXT_NORM;
+ xfs_bmap_add_extent_hole_delay(ip, XFS_COW_FORK, &icur, &got);
+ ac->reserved_blocks -= count_fsb;
+- iomap_flags |= IOMAP_F_NEW;
++ iomap_flags |= (IOMAP_F_NEW | IOMAP_F_SHARED);
-+ iomap_flags = iomap_flags | IOMAP_F_SHARED;
-+
- ASSERT(!xfs_get_extsz_hint(ip));
- ASSERT(!(flags & IOMAP_UNSHARE));
- ASSERT(ac);
+ trace_xfs_iomap_alloc(ip, offset, XFS_FSB_TO_B(mp, count_fsb),
+ XFS_COW_FORK, &got);
@@ fs/xfs/xfs_iomap.c: xfs_buffered_write_iomap_begin(
if (xfs_is_shutdown(mp))
return -EIO;
13: 9ca0788bde68 = 14: a65403d233e2 xfs: use read ioend for fsverity data verification
14: fd0353ac09d5 ! 15: 6ecf7b9bf300 xfs: add fs-verity support
@@ fs/xfs/xfs_fsverity.c
+#include "xfs_da_btree.h"
+#include "xfs_inode.h"
+#include "xfs_log_format.h"
-+#include "xfs_bmap_util.h"
+#include "xfs_trans.h"
+#include "xfs_trace.h"
+#include "xfs_quota.h"
@@ fs/xfs/xfs_fsverity.c: xfs_fsverity_is_file_data(
+ xfs_ilock(ip, XFS_ILOCK_EXCL);
+ xfs_trans_ijoin(tp, ip, 0);
+
++ truncate_inode_pages(VFS_I(ip)->i_mapping, XFS_ISIZE(ip));
++
+ /*
-+ * We removing post EOF data, no need to update i_size as fsverity
++ * We remove post EOF data, no need to update i_size as fsverity
+ * didn't move i_size in the first place
+ */
+ error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, XFS_ISIZE(ip));
@@ fs/xfs/xfs_fsverity.c: xfs_fsverity_is_file_data(
+ * files are read-only
+ */
+ if (xfs_is_cow_inode(ip)) {
-+ if (!ip->i_cowfp)
-+ xfs_ifork_init_cow(ip);
++ xfs_ilock(ip, XFS_ILOCK_EXCL);
++ xfs_ifork_init_cow(ip);
++ xfs_iunlock(ip, XFS_ILOCK_EXCL);
+ xfs_bmap_punch_delalloc_range(ip, XFS_COW_FORK, 0, LLONG_MAX,
+ NULL);
+ }
@@ fs/xfs/xfs_fsverity.c: xfs_fsverity_is_file_data(
+ xfs_iunlock(ip, XFS_ILOCK_EXCL);
+
+ if (!error)
-+ inode->i_flags |= S_VERITY;
++ inode_set_flags(inode, S_VERITY, S_VERITY);
+
+out:
+ if (error) {
15: 48eb71d0d46f ! 16: 9a5ec524c5db xfs: remove unwritten extents after preallocations in fsverity metadata
@@ Commit message
## fs/xfs/xfs_fsverity.c ##
@@
+ #include "xfs_inode.h"
+ #include "xfs_log_format.h"
+ #include "xfs_trans.h"
++#include "xfs_defer.h"
+ #include "xfs_trace.h"
+ #include "xfs_quota.h"
+ #include "xfs_fsverity.h"
#include "xfs_iomap.h"
#include "xfs_error.h"
#include "xfs_health.h"
@@ fs/xfs/xfs_fsverity.c: xfs_fsverity_delete_metadata(
+{
+ struct xfs_mount *mp = ip->i_mount;
+ struct xfs_trans *tp;
-+ xfs_fileoff_t offset_fsb = XFS_B_TO_FSB(mp, start);
++ xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, start);
+ xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, end);
+ struct xfs_bmbt_irec imap;
+ int nimaps;
+ int error = 0;
+ int done;
+
-+
+ while (offset_fsb < end_fsb) {
+ nimaps = 1;
+
-+ error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0,
-+ 0, &tp);
++ error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
+ if (error)
+ return error;
+
+ xfs_ilock(ip, XFS_ILOCK_EXCL);
-+ error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
-+ &imap, &nimaps, 0);
++ error = xfs_bmapi_read(ip, offset_fsb,
++ end_fsb - offset_fsb, &imap, &nimaps,
++ 0);
+ if (error)
+ goto out_cancel;
+
@@ fs/xfs/xfs_fsverity.c: xfs_fsverity_delete_metadata(
+ goto out_cancel;
+
+ if (imap.br_state == XFS_EXT_UNWRITTEN) {
-+ xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
++ done = 0;
+
-+ error = xfs_bunmapi(tp, ip, imap.br_startoff,
-+ imap.br_blockcount, 0, 1, &done);
-+ if (error)
-+ goto out_cancel;
++ xfs_trans_ijoin(tp, ip, 0);
++
++ while (!done) {
++ error = xfs_bunmapi(tp, ip, imap.br_startoff,
++ imap.br_blockcount, 0, 1,
++ &done);
++ if (error)
++ goto out_cancel;
++
++ error = xfs_defer_finish(&tp);
++ if (error)
++ goto out_cancel;
++ }
+
+ error = xfs_trans_commit(tp);
+ if (error)
-+ return error;
++ goto out_unlock;
+ } else {
+ xfs_trans_cancel(tp);
-+ xfs_iunlock(ip, XFS_ILOCK_EXCL);
+ }
++ xfs_iunlock(ip, XFS_ILOCK_EXCL);
+
+ offset_fsb = imap.br_startoff + imap.br_blockcount;
+ }
@@ fs/xfs/xfs_fsverity.c: xfs_fsverity_delete_metadata(
+ return error;
+out_cancel:
+ xfs_trans_cancel(tp);
++out_unlock:
+ xfs_iunlock(ip, XFS_ILOCK_EXCL);
+ return error;
+}
8: d782cf032d1d = 17: 9ac4bd88f99a xfs: initialize fs-verity on file open
16: 312d09ecc4e2 = 18: eff55c20dd50 xfs: add fs-verity ioctls
17: 57d88dcd36dd = 19: f78ba16752fe xfs: advertise fs-verity being available on filesystem
18: 856c3ff6b1db ! 20: a283ffc94e16 xfs: check and repair the verity inode flag state
@@ fs/xfs/scrub/common.c: xchk_inode_count_blocks(
+ case -EINVAL:
+ case -EFSCORRUPTED:
+ case -EFBIG:
++ case -ERANGE:
++ case -EBADMSG:
+ /*
+ * The nonzero errno codes above are the error codes that can
+ * be returned from fsverity on metadata validation errors.
@@ fs/xfs/scrub/inode_repair.c: xrep_inode_unlinked(
+
+ if (xchk_inode_verity_broken(sc->ip)) {
+ sc->ip->i_diflags2 &= ~XFS_DIFLAG2_VERITY;
-+ inode->i_flags &= ~S_VERITY;
++ inode_set_flags(inode, 0, S_VERITY);
+
+ xfs_trans_log_inode(sc->tp, sc->ip, XFS_ILOG_CORE);
+ }
19: 52de4f439ac7 = 21: 01d4b6059dc8 xfs: introduce health state for corrupted fsverity metadata
20: ee884674c76c = 22: cb4acb76a9b3 xfs: enable ro-compat fs-verity flagAndrey Albershteyn (19):
fsverity: report validation errors through fserror to fsnotify
fsverity: expose ensure_fsverity_info()
fsverity: pass digest size and hash of the all-zeroes block to ->write
fsverity: hoist pagecache_read from f2fs/ext4 to fsverity
fsverity: improve flushing performance of fsverity_fill_zerohash
fsverity: don't allow setting DAX file attribute on fsverity files fs,fsverity: remove check for fsverity being enabled in
setattr_prepare()
xfs: introduce fsverity on-disk changes
xfs: don't allow to enable DAX on fs-verity sealed inode
xfs: disable direct read path for fs-verity files
xfs: don't report dio_mem_align and dio_offset_align for fsverity
files
xfs: handle fsverity I/O in write/read path
xfs: use read ioend for fsverity data verification
xfs: add fs-verity support
xfs: remove unwritten extents after preallocations in fsverity
metadata
xfs: initialize fs-verity on file open
xfs: add fs-verity ioctls
xfs: introduce health state for corrupted fsverity metadata
xfs: enable ro-compat fs-verity flag
Darrick J. Wong (2):
xfs: advertise fs-verity being available on filesystem
xfs: check and repair the verity inode flag state
fs/attr.c | 2 +- fs/btrfs/verity.c | 6 +- fs/ext4/verity.c | 36 +-- fs/f2fs/verity.c | 34 +-- fs/file_attr.c | 5 + fs/verity/enable.c | 4 +- fs/verity/open.c | 22 +- fs/verity/pagecache.c | 57 +++- fs/verity/verify.c | 4 + fs/xfs/Makefile | 1 + fs/xfs/libxfs/xfs_bmap.c | 11 + fs/xfs/libxfs/xfs_format.h | 35 ++- fs/xfs/libxfs/xfs_fs.h | 2 + fs/xfs/libxfs/xfs_health.h | 4 +- fs/xfs/libxfs/xfs_inode_buf.c | 8 + fs/xfs/libxfs/xfs_inode_util.c | 5 +- fs/xfs/libxfs/xfs_sb.c | 4 + fs/xfs/scrub/common.c | 55 ++++ fs/xfs/scrub/common.h | 2 + fs/xfs/scrub/inode.c | 7 + fs/xfs/scrub/inode_repair.c | 36 +++ fs/xfs/xfs_aops.c | 81 +++++- fs/xfs/xfs_bmap_util.c | 8 + fs/xfs/xfs_file.c | 67 +++-- fs/xfs/xfs_fsverity.c | 485 +++++++++++++++++++++++++++++++++ fs/xfs/xfs_fsverity.h | 28 ++ fs/xfs/xfs_health.c | 1 + fs/xfs/xfs_inode.h | 6 + fs/xfs/xfs_ioctl.c | 14 + fs/xfs/xfs_iomap.c | 29 +- fs/xfs/xfs_iops.c | 12 +- fs/xfs/xfs_message.c | 4 + fs/xfs/xfs_message.h | 1 + fs/xfs/xfs_mount.h | 4 + fs/xfs/xfs_super.c | 7 + include/linux/fsverity.h | 10 +- 36 files changed, 981 insertions(+), 116 deletions(-) create mode 100644 fs/xfs/xfs_fsverity.c create mode 100644 fs/xfs/xfs_fsverity.h
Signature · 1 line
2.54.0
PATCH v12 1/21 fsverity: report validation errors through fserror to fsnotify
Reported verification errors to fsnotify through recently added fserror interface.
--- fs/verity/verify.c | 4 ++++ 1 file changed, 4 insertions(+)
fs/verity/verify.c +4 -0
@@ -9,6 +9,7 @@#include <linux/bio.h> #include <linux/export.h> +#include <linux/fserror.h> #define FS_VERITY_MAX_PENDING_BLOCKS 2
@@ -205,6 +206,8 @@ static bool verify_data_block(struct fsverity_info *vi,if (memchr_inv (dblock->data,0 , params->block_size)) {fsverity_err (inode,"FILE CORRUPTED! Data past EOF is not zeroed" ); +fserror_report_data_lost (inode, data_pos, + params->block_size,GFP_NOFS );return false; }return true;
@@ -312,6 +315,7 @@ static bool verify_data_block(struct fsverity_info *vi, data_pos, level -1 , params->hash_alg->name, hsize, want_hash, params->hash_alg->name, hsize, level == 0 ? dblock->real_hash : real_hash); +fserror_report_data_lost (inode, data_pos, params->block_size,GFP_NOFS ); error:for (; level >0 ; level--) {kunmap_local (hblocks[level -1 ].addr);
Signature · 1 line
2.54.0
PATCH v12 2/21 fsverity: expose ensure_fsverity_info()
In reply to Andrey AlbershteynThis function will be used by XFS's scrub to force fsverity activation, therefore, to read fsverity context.
--- fs/verity/open.c | 22 ++++++++++++++++++++-- include/linux/fsverity.h | 2 ++ 2 files changed, 22 insertions(+), 2 deletions(-)
fs/verity/open.c +20 -2
@@ -347,7 +347,24 @@ int fsverity_get_descriptor(struct inode *inode,return 0 ; } -static int ensure_verity_info (struct inode *inode) +/** +* fsverity_ensure_verity_info() - cache verity info if it's not already cached +* @inode: the inode for which verity info should be cached +* +* Ensure this inode has verity info attached to it, it's assumed the inode +* already has fsverity enabled. Read fsverity descriptor and creates verity +* based on that. +* +* This needs to be called at least once before any of the inode's data +* can be verified (and thus read at all) or the inode's fsverity digest +* retrieved. fsverity_file_open() calls this already, which handles +* normal file accesses. If a filesystem does any internal (i.e. not +* associated with a file descriptor) reads of the file's data or +* fsverity digest, it must call this explicitly before doing so. +* +* Return: 0 on success, -errno on failure +*/ +int fsverity_ensure_verity_info (struct inode *inode) {struct fsverity_info *vi =fsverity_get_info (inode), *found;struct fsverity_descriptor *desc;
@@ -383,12 +400,13 @@ static int ensure_verity_info(struct inode *inode)kfree (desc);return err; } +EXPORT_SYMBOL_GPL (fsverity_ensure_verity_info);int __fsverity_file_open (struct inode *inode,struct file *filp) {if (filp->f_mode &FMODE_WRITE )return -EPERM ; -return ensure_verity_info (inode); +return fsverity_ensure_verity_info (inode); }EXPORT_SYMBOL_GPL (__fsverity_file_open);
include/linux/fsverity.h +2 -0
@@ -317,6 +317,8 @@ static inline int fsverity_file_open(struct inode *inode, struct file *filp)return 0 ; } +int fsverity_ensure_verity_info (struct inode *inode); +void fsverity_cleanup_inode (struct inode *inode);struct page *generic_read_merkle_tree_page (struct inode *inode,pgoff_t index);
Signature · 1 line
2.54.0
PATCH v12 3/21 fsverity: pass digest size and hash of the all-zeroes block to ->write
In reply to Andrey AlbershteynLet filesystem iterate over hashes in the block and check if these are hashes of zeroed data blocks. XFS will use this to decide if it want to store tree block full of these hashes.
--- fs/btrfs/verity.c | 6 +++++- fs/ext4/verity.c | 4 +++- fs/f2fs/verity.c | 4 +++- fs/verity/enable.c | 4 +++- include/linux/fsverity.h | 6 +++++- 5 files changed, 19 insertions(+), 5 deletions(-)
fs/btrfs/verity.c +5 -1
@@ -773,11 +773,15 @@ static struct page *btrfs_read_merkle_tree_page(struct inode *inode,* @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) +* @zero_digest: the hash of the all-zeroes block +* @digest_size: size of zero_digest, in bytes * * Returns 0 on success or negative error code on failure */ static int btrfs_write_merkle_tree_block (struct file *file,const void *buf, -u64 pos,unsigned int size) +u64 pos,unsigned int size, +const u8 *zero_digest, +unsigned int digest_size) {struct inode *inode =file_inode (file);loff_t merkle_pos =merkle_file_pos (inode);
fs/ext4/verity.c +3 -1
@@ -374,7 +374,9 @@ static void ext4_readahead_merkle_tree(struct inode *inode, pgoff_t index, }static int ext4_write_merkle_tree_block (struct file *file,const void *buf, -u64 pos,unsigned int size) +u64 pos,unsigned int size, +const u8 *zero_digest, +unsigned int digest_size) { pos +=ext4_verity_metadata_pos (file_inode (file));
fs/f2fs/verity.c +3 -1
@@ -272,7 +272,9 @@ static void f2fs_readahead_merkle_tree(struct inode *inode, pgoff_t index, }static int f2fs_write_merkle_tree_block (struct file *file,const void *buf, -u64 pos,unsigned int size) +u64 pos,unsigned int size, +const u8 *zero_digest, +unsigned int digest_size) { pos +=f2fs_verity_metadata_pos (file_inode (file));
fs/verity/enable.c +3 -1
@@ -50,7 +50,9 @@ static int write_merkle_tree_block(struct file *file, const u8 *buf,int err; err = inode->i_sb->s_vop->write_merkle_tree_block (file, buf, pos, - params->block_size); + params->block_size, + params->zero_digest, + params->digest_size);if (err)fsverity_err (inode,"Error %d writing Merkle tree block %lu" , err, index);
include/linux/fsverity.h +5 -1
@@ -124,6 +124,8 @@ struct fsverity_operations {* @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) +* @zero_digest: the hash of the all-zeroes block +* @digest_size: size of zero_digest, in bytes * * This is only called between ->begin_enable_verity() and * ->end_enable_verity().
@@ -131,7 +133,9 @@ struct fsverity_operations {* Return: 0 on success, -errno on failure */ int (*write_merkle_tree_block)(struct file *file,const void *buf, -u64 pos,unsigned int size); +u64 pos,unsigned int size, +const u8 *zero_digest, +unsigned int digest_size); };#ifdef CONFIG_FS_VERITY
Signature · 1 line
2.54.0
Re: [PATCH v12 03/21] fsverity: pass digest size and hash of the all-zeroes block to ->write
Quoted 10 lines · Andrey Albershteyn
Let filesystem iterate over hashes in the block and check if these are hashes of zeroed data blocks. XFS will use this to decide if it want to store tree block full of these hashes.
Signed-off-by Andrey Albershteyn <[email protected]>Reviewed-by "Darrick J. Wong" <[email protected]>Acked-by Eric Biggers <[email protected]>---
fs/btrfs/verity.c | 6 +++++-
PATCH v12 4/21 fsverity: hoist pagecache_read from f2fs/ext4 to fsverity
In reply to Andrey AlbershteynThis is the same function to read from pageache. XFS will also need this, so move this to core fsverity.
Note that f2fs and ext4 functions diverged a bit, as ext4 operated over folios and f2fs operated over pages. The common one will operate over folios.
--- fs/ext4/verity.c | 32 +++----------------------------- fs/f2fs/verity.c | 30 +----------------------------- fs/verity/pagecache.c | 33 +++++++++++++++++++++++++++++++++ include/linux/fsverity.h | 2 ++ 4 files changed, 39 insertions(+), 58 deletions(-)
fs/ext4/verity.c +3 -29
@@ -34,32 +34,6 @@ static inline loff_t ext4_verity_metadata_pos(const struct inode *inode)return round_up (inode->i_size,65536 ); } -/* -* Read some verity metadata from the inode. __vfs_read() can't be used because -* we need to read beyond i_size. -*/ -static int pagecache_read (struct inode *inode,void *buf,size_t count, -loff_t pos) -{ -while (count) { -struct folio *folio; -size_t n; - - folio =read_mapping_folio (inode->i_mapping, pos >>PAGE_SHIFT , -NULL ); -if (IS_ERR (folio)) -return PTR_ERR (folio); - - n =memcpy_from_file_folio (buf, folio, pos, count); -folio_put (folio); - - buf += n; - pos += n; - count -= n; - } -return 0 ; -} -/* * Write some verity metadata to the inode for FS_IOC_ENABLE_VERITY. * kernel_write() can't be used because the file descriptor is readonly.
@@ -311,8 +285,8 @@ static int ext4_get_verity_descriptor_location(struct inode *inode, goto bad; desc_size_pos -=sizeof (desc_size_disk); - err =pagecache_read (inode, &desc_size_disk,sizeof (desc_size_disk), - desc_size_pos); + err =fsverity_pagecache_read (inode, &desc_size_disk, +sizeof (desc_size_disk), desc_size_pos);if (err)return err; desc_size =le32_to_cpu (desc_size_disk);
@@ -352,7 +326,7 @@ static int ext4_get_verity_descriptor(struct inode *inode, void *buf,if (buf_size) {if (desc_size > buf_size)return -ERANGE ; - err =pagecache_read (inode, buf, desc_size, desc_pos); + err =fsverity_pagecache_read (inode, buf, desc_size, desc_pos);if (err)return err; }
fs/f2fs/verity.c +1 -29
@@ -37,34 +37,6 @@ static inline loff_t f2fs_verity_metadata_pos(const struct inode *inode)return round_up (inode->i_size,65536 ); } -/* -* Read some verity metadata from the inode. __vfs_read() can't be used because -* we need to read beyond i_size. -*/ -static int pagecache_read (struct inode *inode,void *buf,size_t count, -loff_t pos) -{ -while (count) { -size_t n =min_t (size_t, count, -PAGE_SIZE -offset_in_page (pos)); -struct page *page; - - page =read_mapping_page (inode->i_mapping, pos >>PAGE_SHIFT , -NULL ); -if (IS_ERR (page)) -return PTR_ERR (page); - -memcpy_from_page (buf, page,offset_in_page (pos), n); - -put_page (page); - - buf += n; - pos += n; - count -= n; - } -return 0 ; -} -/* * Write some verity metadata to the inode for FS_IOC_ENABLE_VERITY. * kernel_write() can't be used because the file descriptor is readonly.
@@ -250,7 +222,7 @@ static int f2fs_get_verity_descriptor(struct inode *inode, void *buf,if (buf_size) {if (size > buf_size)return -ERANGE ; - res =pagecache_read (inode, buf, size, pos); + res =fsverity_pagecache_read (inode, buf, size, pos);if (res)return res; }
fs/verity/pagecache.c +33 -0
@@ -78,3 +78,36 @@ void fsverity_fill_zerohash(struct folio *folio, size_t offset, size_t len, vi->tree_params.digest_size); }EXPORT_SYMBOL_GPL (fsverity_fill_zerohash); + +/** +* fsverity_pagecache_read() - read page and copy data to buffer +* @inode: copy from this inode's address space +* @buf: buffer to copy to +* @count: number of bytes to copy +* @pos: position of the folio to copy from +* +* Read some verity metadata from the inode. __vfs_read() can't be used because +* we need to read beyond i_size. +*/ +int fsverity_pagecache_read (struct inode *inode,void *buf,size_t count, +loff_t pos) +{ +while (count) { +struct folio *folio; +size_t n; + + folio =read_mapping_folio (inode->i_mapping, pos >>PAGE_SHIFT , +NULL ); +if (IS_ERR (folio)) +return PTR_ERR (folio); + + n =memcpy_from_file_folio (buf, folio, pos, count); +folio_put (folio); + + buf += n; + pos += n; + count -= n; + } +return 0 ; +} +EXPORT_SYMBOL_GPL (fsverity_pagecache_read);
include/linux/fsverity.h +2 -0
@@ -328,5 +328,7 @@ void fsverity_cleanup_inode(struct inode *inode);struct page *generic_read_merkle_tree_page (struct inode *inode,pgoff_t index);void generic_readahead_merkle_tree (struct inode *inode,pgoff_t index,unsigned long nr_pages); +int fsverity_pagecache_read (struct inode *inode,void *buf,size_t count, +loff_t pos);#endif /* _LINUX_FSVERITY_H */
Signature · 1 line
2.54.0
PATCH v12 5/21 fsverity: improve flushing performance of fsverity_fill_zerohash
In reply to Andrey AlbershteynThe current version calls flush_dcache_folio(), in memcpy_to_folio(), to flush whole folio on every digest (which is 128 for 4k). Open code folio mapping and flushing to copy all digests at once.
--- fs/verity/pagecache.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-)
fs/verity/pagecache.c +21 -3
@@ -68,14 +68,32 @@ EXPORT_SYMBOL_GPL(generic_readahead_merkle_tree);void fsverity_fill_zerohash (struct folio *folio,size_t offset,size_t len,struct fsverity_info *vi) { -size_t off = offset; +void *vaddr; +void *to; +size_t chunk;WARN_ON_ONCE (!IS_ALIGNED (offset, vi->tree_params.digest_size));WARN_ON_ONCE (!IS_ALIGNED (len, vi->tree_params.digest_size)); +WARN_ON_ONCE (offset + len >folio_size (folio)); -for (; off < (offset + len); off += vi->tree_params.digest_size) -memcpy_to_folio (folio, off, vi->tree_params.zero_digest, +do { + vaddr =kmap_local_folio (folio, offset); + chunk = len; + to = vaddr; + +if (folio_test_partial_kmap (folio) && + chunk >PAGE_SIZE -offset_in_page (offset)) + chunk =PAGE_SIZE -offset_in_page (offset); +for (; to < (vaddr + chunk); to += vi->tree_params.digest_size) +memcpy (to, vi->tree_params.zero_digest, vi->tree_params.digest_size); +kunmap_local (vaddr); + + offset += chunk; + len -= chunk; + }while (len >0 ); + +flush_dcache_folio (folio); }EXPORT_SYMBOL_GPL (fsverity_fill_zerohash);
Signature · 1 line
2.54.0
PATCH v12 6/21 fsverity: don't allow setting DAX file attribute on fsverity files
In reply to Andrey AlbershteynWhen fsverity is enabled on the file, with FS_IOC_ENABLE_VERITY ioctl(), it checks if file has DAX enabled and fails if that's true. However, the opposite case is not checked.
--- fs/file_attr.c | 5 +++++ 1 file changed, 5 insertions(+)
fs/file_attr.c +5 -0
@@ -246,6 +246,11 @@ static int fileattr_set_prepare(struct inode *inode,if (fa->fsx_cowextsize ==0 ) fa->fsx_xflags &= ~FS_XFLAG_COWEXTSIZE ; +/* Can not enable DAX on fsverity file */ +if ((old_ma->fsx_xflags &FS_XFLAG_VERITY ) && + fa->fsx_xflags &FS_XFLAG_DAX ) +return -EINVAL ; +return 0 ; }
Signature · 1 line
2.54.0
PATCH v12 7/21 fs,fsverity: remove check for fsverity being enabled in setattr_prepare()
In reply to Andrey AlbershteynThe check that fs-verity is available in the kernel is not necessary here. Filesystems could have fsverity files even without fs-verity module, then truncate will succeed. Previously, this would return EOPNOTSUPP if verity is missing, but if it's not EPERM was returned anyway.
--- fs/attr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/attr.c +1 -1
@@ -176,7 +176,7 @@ int setattr_prepare(struct mnt_idmap *idmap, struct dentry *dentry,* covered by the open-time check because sys_truncate() takes a * path, not an open file. */ -if (IS_ENABLED (CONFIG_FS_VERITY ) &&IS_VERITY (inode)) +if (IS_VERITY (inode))return -EPERM ; error =inode_newsize_ok (inode, attr->ia_size);
Signature · 1 line
2.54.0
PATCH v12 8/21 xfs: introduce fsverity on-disk changes
In reply to Andrey AlbershteynIntroduce XFS_DIFLAG2_VERITY for inodes with fsverity. This flag indicates that inode has fs-verity enabled (i.e. descriptor exist, tree is built and file is read-only).
Introduce XFS_SB_FEAT_RO_COMPAT_VERITY for filesystems having fsverity inodes. As on-disk changes applies to fsverity inodes only, let older kernels read-only access. This will be enabled in the further patch after full fsverity support.
--- fs/xfs/libxfs/xfs_format.h | 30 +++++++++++++++++++++++++++++- fs/xfs/libxfs/xfs_inode_buf.c | 8 ++++++++ fs/xfs/libxfs/xfs_inode_util.c | 5 ++++- fs/xfs/libxfs/xfs_sb.c | 2 ++ fs/xfs/xfs_iops.c | 5 ++++- fs/xfs/xfs_mount.h | 2 ++ 6 files changed, 49 insertions(+), 3 deletions(-)
fs/xfs/libxfs/xfs_format.h +29 -1
@@ -374,6 +374,7 @@ xfs_sb_has_compat_feature(#define XFS_SB_FEAT_RO_COMPAT_RMAPBT (1 << 1)/* reverse map btree */ #define XFS_SB_FEAT_RO_COMPAT_REFLINK (1 << 2)/* reflinked files */ #define XFS_SB_FEAT_RO_COMPAT_INOBTCNT (1 << 3)/* inobt block counts */ +#define XFS_SB_FEAT_RO_COMPAT_VERITY (1 << 4)/* fs-verity */ #define XFS_SB_FEAT_RO_COMPAT_ALL \ (XFS_SB_FEAT_RO_COMPAT_FINOBT | \ XFS_SB_FEAT_RO_COMPAT_RMAPBT | \
@@ -1230,16 +1231,21 @@ static inline void xfs_dinode_put_rdev(struct xfs_dinode *dip, xfs_dev_t rdev)*/ #define XFS_DIFLAG2_METADATA_BIT 5 +/* inodes sealed with fs-verity */ +#define XFS_DIFLAG2_VERITY_BIT 6 +#define XFS_DIFLAG2_DAX (1ULL << XFS_DIFLAG2_DAX_BIT)#define XFS_DIFLAG2_REFLINK (1ULL << XFS_DIFLAG2_REFLINK_BIT)#define XFS_DIFLAG2_COWEXTSIZE (1ULL << XFS_DIFLAG2_COWEXTSIZE_BIT)#define XFS_DIFLAG2_BIGTIME (1ULL << XFS_DIFLAG2_BIGTIME_BIT)#define XFS_DIFLAG2_NREXT64 (1ULL << XFS_DIFLAG2_NREXT64_BIT)#define XFS_DIFLAG2_METADATA (1ULL << XFS_DIFLAG2_METADATA_BIT) +#define XFS_DIFLAG2_VERITY (1ULL << XFS_DIFLAG2_VERITY_BIT)#define XFS_DIFLAG2_ANY \ (XFS_DIFLAG2_DAX | XFS_DIFLAG2_REFLINK | XFS_DIFLAG2_COWEXTSIZE | \ - XFS_DIFLAG2_BIGTIME | XFS_DIFLAG2_NREXT64 | XFS_DIFLAG2_METADATA) + XFS_DIFLAG2_BIGTIME | XFS_DIFLAG2_NREXT64 | XFS_DIFLAG2_METADATA | \ + XFS_DIFLAG2_VERITY)static inline bool xfs_dinode_has_bigtime (const struct xfs_dinode *dip) {
@@ -2027,4 +2033,26 @@ struct xfs_acl {#define SGI_ACL_FILE_SIZE (sizeof(SGI_ACL_FILE)-1)#define SGI_ACL_DEFAULT_SIZE (sizeof(SGI_ACL_DEFAULT)-1) +/* +* At maximum of 8 levels with 128 hashes per block (32 bytes SHA-256) maximum +* tree size is ((128^8 − 1)/(128 − 1)) = 567*10^12 blocks. This should fit in +* 53 bits address space. +* +* At this Merkle tree size we can cover 295EB large file. This is much larger +* than the currently supported file size. +* +* For sha512 the largest file we can cover ends at 1 << 50 offset, this is also +* good. +*/ +#define XFS_FSVERITY_LARGEST_FILE ((loff_t)1ULL << 53) + +/* +* Alignment of the fsverity metadata placement. This is largest supported PAGE +* SIZE for fsverity. This is used to space out data and metadata in page cache. +* The spacing is necessary for non-exposure of metadata to userspace and +* correct merkle tree synethesis in the iomap. +*/ +#define XFS_FSVERITY_START_ALIGN (65536) + +#endif /* __XFS_FORMAT_H__ */
fs/xfs/libxfs/xfs_inode_buf.c +8 -0
@@ -760,6 +760,14 @@ xfs_dinode_verify( !xfs_has_rtreflink(mp))return __this_address; +/* only regular files can have fsverity */ +if (flags2 &XFS_DIFLAG2_VERITY ) { +if (!xfs_has_verity (mp)) +return __this_address; +if (!S_ISREG (mode)) +return __this_address; + } +if (xfs_has_zoned (mp) && dip->di_metatype ==cpu_to_be16 (XFS_METAFILE_RTRMAP )) {if (be32_to_cpu (dip->di_used_blocks) > mp->m_sb.sb_rgextents)
fs/xfs/libxfs/xfs_inode_util.c +4 -1
@@ -74,7 +74,8 @@ xfs_flags2diflags2(uint64_t di_flags2 = (ip->i_diflags2 & (XFS_DIFLAG2_REFLINK |XFS_DIFLAG2_BIGTIME | -XFS_DIFLAG2_NREXT64 )); +XFS_DIFLAG2_NREXT64 | +XFS_DIFLAG2_VERITY ));if (xflags &FS_XFLAG_DAX ) di_flags2 |=XFS_DIFLAG2_DAX ;
@@ -126,6 +127,8 @@ xfs_ip2xflags( flags |=FS_XFLAG_DAX ;if (ip->i_diflags2 &XFS_DIFLAG2_COWEXTSIZE ) flags |=FS_XFLAG_COWEXTSIZE ; +if (ip->i_diflags2 &XFS_DIFLAG2_VERITY ) + flags |=FS_XFLAG_VERITY ; }if (xfs_inode_has_attr_fork (ip))
fs/xfs/libxfs/xfs_sb.c +2 -0
@@ -165,6 +165,8 @@ xfs_sb_version_to_features( features |=XFS_FEAT_REFLINK ;if (sbp->sb_features_ro_compat &XFS_SB_FEAT_RO_COMPAT_INOBTCNT ) features |=XFS_FEAT_INOBTCNT ; +if (sbp->sb_features_ro_compat &XFS_SB_FEAT_RO_COMPAT_VERITY ) + features |=XFS_FEAT_VERITY ;if (sbp->sb_features_incompat &XFS_SB_FEAT_INCOMPAT_FTYPE ) features |=XFS_FEAT_FTYPE ;if (sbp->sb_features_incompat &XFS_SB_FEAT_INCOMPAT_SPINODES )
fs/xfs/xfs_iops.c +4 -1
@@ -1398,12 +1398,15 @@ xfs_diflags_to_iflags( flags |=S_NOATIME ;if (init &&xfs_inode_should_enable_dax (ip)) flags |=S_DAX ; +if (xflags &FS_XFLAG_VERITY ) + flags |=S_VERITY ;/* * S_DAX can only be set during inode initialization and is never set by * the VFS, so we cannot mask off S_DAX in i_flags. */ - inode->i_flags &= ~(S_IMMUTABLE |S_APPEND |S_SYNC |S_NOATIME ); + inode->i_flags &= + ~(S_IMMUTABLE |S_APPEND |S_SYNC |S_NOATIME |S_VERITY ); inode->i_flags |= flags; }
fs/xfs/xfs_mount.h +2 -0
@@ -388,6 +388,7 @@ typedef struct xfs_mount {#define XFS_FEAT_EXCHANGE_RANGE (1ULL << 27)/* exchange range */ #define XFS_FEAT_METADIR (1ULL << 28)/* metadata directory tree */ #define XFS_FEAT_ZONED (1ULL << 29)/* zoned RT device */ +#define XFS_FEAT_VERITY (1ULL << 30)/* fs-verity */ /* Mount features */ #define XFS_FEAT_NOLIFETIME (1ULL << 47)/* disable lifetime hints */
@@ -445,6 +446,7 @@ __XFS_HAS_FEAT(exchange_range, EXCHANGE_RANGE) __XFS_HAS_FEAT(metadir, METADIR) __XFS_HAS_FEAT(zoned, ZONED) __XFS_HAS_FEAT(nolifetime, NOLIFETIME) +__XFS_HAS_FEAT(verity, VERITY)static inline bool xfs_has_rtgroups (const struct xfs_mount *mp) {
Signature · 1 line
2.54.0
PATCH v12 9/21 xfs: don't allow to enable DAX on fs-verity sealed inode
In reply to Andrey Albershteynfs-verity doesn't support DAX. Forbid filesystem to enable DAX on inodes which already have fs-verity enabled. The opposite is checked when fs-verity is enabled, it won't be enabled if DAX is.
--- fs/xfs/xfs_iops.c | 2 ++ 1 file changed, 2 insertions(+)
fs/xfs/xfs_iops.c +2 -0
@@ -1370,6 +1370,8 @@ xfs_inode_should_enable_dax(return false;if (!xfs_inode_supports_dax (ip))return false; +if (ip->i_diflags2 &XFS_DIFLAG2_VERITY ) +return false;if (xfs_has_dax_always (ip->i_mount))return true;if (ip->i_diflags2 &XFS_DIFLAG2_DAX )
Signature · 1 line
2.54.0
PATCH v12 10/21 xfs: disable direct read path for fs-verity files
In reply to Andrey AlbershteynThe direct path is not supported on verity files. Attempts to use direct I/O path on such files should fall back to buffered I/O path.
Add a fall back to buffered I/O at two place, in a common fast path and latter when lock is acquired. The second check prevents TOCTOU issue with reading fsverity_active() status and resetting IOCB_DIRECT flag.
If one thread saw fsverity_active() to be false, and then second thread acquired XFS_IOLOCK_EXCL and enabled fsverity. The first thread will go through the DIO path.
--- fs/xfs/xfs_file.c | 59 +++++++++++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 20 deletions(-)
fs/xfs/xfs_file.c +39 -20
@@ -245,6 +245,25 @@ static const struct iomap_dio_ops xfs_dio_read_bounce_ops = { .bio_set = &iomap_ioend_bioset, }; +STATIC ssize_t +xfs_file_buffered_read ( +struct kiocb *iocb, +struct iov_iter *to) +{ +struct xfs_inode *ip =XFS_I (file_inode (iocb->ki_filp)); +ssize_t ret; + +trace_xfs_file_buffered_read (iocb, to); + + ret =xfs_ilock_iocb (iocb,XFS_IOLOCK_SHARED ); +if (ret) +return ret; + ret =generic_file_read_iter (iocb, to); +xfs_iunlock (ip,XFS_IOLOCK_SHARED ); + +return ret; +} + STATIC ssize_t xfs_file_dio_read( struct kiocb *iocb,
@@ -265,6 +284,16 @@ xfs_file_dio_read( ret =xfs_ilock_iocb (iocb,XFS_IOLOCK_SHARED );if (ret)return ret; +/* +* Re-check verity status after acquiring lock. This prevents TOCTOU in +* xfs_file_read_iter() while falling back from DIO to buffered I/O as +* now we are holding a lock +*/ +if (fsverity_active (VFS_I (ip))) { +xfs_iunlock (ip,XFS_IOLOCK_SHARED ); + iocb->ki_flags &= ~IOCB_DIRECT ; +return xfs_file_buffered_read (iocb, to); + }if (mapping_stable_writes (iocb->ki_filp->f_mapping)) { dio_ops = &xfs_dio_read_bounce_ops; dio_flags |=IOMAP_DIO_BOUNCE ;
@@ -281,7 +310,8 @@ xfs_file_dax_read(struct kiocb *iocb, struct iov_iter *to) { - struct xfs_inode *ip = XFS_I(iocb->ki_filp->f_mapping->host); + struct inode *inode = iocb->ki_filp->f_mapping->host; +struct xfs_inode *ip =XFS_I (inode);ssize_t ret =0 ;trace_xfs_file_dax_read (iocb, to);
@@ -299,25 +329,6 @@ xfs_file_dax_read(return ret; } -STATIC ssize_t -xfs_file_buffered_read ( -struct kiocb *iocb, -struct iov_iter *to) -{ -struct xfs_inode *ip =XFS_I (file_inode (iocb->ki_filp)); -ssize_t ret; - -trace_xfs_file_buffered_read (iocb, to); - - ret =xfs_ilock_iocb (iocb,XFS_IOLOCK_SHARED ); -if (ret) -return ret; - ret =generic_file_read_iter (iocb, to); -xfs_iunlock (ip,XFS_IOLOCK_SHARED ); - -return ret; -} - STATIC ssize_t xfs_file_read_iter( struct kiocb *iocb,
@@ -332,6 +343,14 @@ xfs_file_read_iter(if (xfs_is_shutdown (mp))return -EIO ; +/* +* In case fs-verity is enabled, we also fallback to the buffered read +* from the direct read path. Therefore, IOCB_DIRECT is set and need to +* be cleared (see generic_file_read_iter()) +*/ +if (fsverity_active (inode)) + iocb->ki_flags &= ~IOCB_DIRECT ; +if (IS_DAX (inode)) ret =xfs_file_dax_read (iocb, to);else if (iocb->ki_flags &IOCB_DIRECT )
Signature · 1 line
2.54.0
PATCH v12 11/21 xfs: don't report dio_mem_align and dio_offset_align for fsverity files
In reply to Andrey AlbershteynSashiko.dev reported that while fsverity files falls back to the buffered IO for Direct I/O, they should not report non-zero values in dio_mem_align and dio_offset_align, meaning it's not supported.
--- fs/xfs/xfs_iops.c | 5 +++++ 1 file changed, 5 insertions(+)
fs/xfs/xfs_iops.c +5 -0
@@ -35,6 +35,7 @@#include <linux/security.h> #include <linux/iversion.h> #include <linux/fiemap.h> +#include <linux/fsverity.h> /* * Directories have different lock order w.r.t. mmap_lock compared to regular
@@ -580,6 +581,10 @@ xfs_report_dioalign(struct block_device *bdev = target->bt_bdev; stat->result_mask |=STATX_DIOALIGN |STATX_DIO_READ_ALIGN ; + +if (fsverity_active (VFS_I (ip))) +return ; + stat->dio_mem_align =bdev_dma_alignment (bdev) +1 ;/*
Signature · 1 line
2.54.0
PATCH v12 12/21 xfs: handle fsverity I/O in write/read path
In reply to Andrey AlbershteynFor write/writeback set IOMAP_F_FSVERITY flag telling iomap to not update inode size and to not skip folios beyond EOF.
Initiate fsverity writeback with IOMAP_F_FSVERITY set to tell iomap should not skip folio that is dirty beyond EOF.
In read path let iomap know that we are reading fsverity metadata. So, treat holes in the tree as request to synthesize tree blocks and hole after descriptor as end of the fsverity region.
Introduce a new inode flag meaning that merkle tree is being build on the inode.
--- fs/xfs/Makefile | 1 + fs/xfs/libxfs/xfs_bmap.c | 11 +++++++++++ fs/xfs/xfs_aops.c | 36 +++++++++++++++++++++++++++++++----- fs/xfs/xfs_fsverity.c | 20 ++++++++++++++++++++ fs/xfs/xfs_fsverity.h | 20 ++++++++++++++++++++ fs/xfs/xfs_inode.h | 6 ++++++ fs/xfs/xfs_iomap.c | 29 ++++++++++++++++++++++------- 7 files changed, 111 insertions(+), 12 deletions(-) create mode 100644 fs/xfs/xfs_fsverity.c create mode 100644 fs/xfs/xfs_fsverity.h
fs/xfs/Makefile +1 -0
@@ -149,6 +149,7 @@ xfs-$(CONFIG_XFS_POSIX_ACL) += xfs_acl.o xfs-$(CONFIG_SYSCTL) += xfs_sysctl.o xfs-$(CONFIG_COMPAT) += xfs_ioctl32.o xfs-$(CONFIG_EXPORTFS_BLOCK_OPS) += xfs_pnfs.o +xfs-$(CONFIG_FS_VERITY) += xfs_fsverity.o # notify failure ifeq ($(CONFIG_MEMORY_FAILURE),y)
fs/xfs/libxfs/xfs_bmap.c +11 -0
@@ -41,6 +41,8 @@#include "xfs_inode_util.h" #include "xfs_rtgroup.h" #include "xfs_zone_alloc.h" +#include "xfs_fsverity.h" +#include <linux/fsverity.h> struct kmem_cache *xfs_bmap_intent_cache;
@@ -4402,6 +4404,10 @@ xfs_bmapi_convert_one_delalloc(* the extent. Just return the real extent at this offset. */ if (!isnullstartblock (bma.got.br_startblock)) { +if (xfs_iflags_test (ip,XFS_VERITY_CONSTRUCTION ) && +XFS_FSB_TO_B (mp, bma.got.br_startoff) >= +xfs_fsverity_metadata_offset (ip)) + flags |=IOMAP_F_FSVERITY ;xfs_bmbt_to_iomap (ip, iomap, &bma.got,0 , flags,xfs_iomap_inode_sequence (ip, flags));if (seq)
@@ -4449,6 +4455,11 @@ xfs_bmapi_convert_one_delalloc(XFS_STATS_ADD (mp, xs_xstrat_bytes,XFS_FSB_TO_B (mp, bma.length));XFS_STATS_INC (mp, xs_xstrat_quick); +if (xfs_iflags_test (ip,XFS_VERITY_CONSTRUCTION ) && +XFS_FSB_TO_B (mp, bma.got.br_startoff) >= +xfs_fsverity_metadata_offset (ip)) + flags |=IOMAP_F_FSVERITY ; +ASSERT (!isnullstartblock (bma.got.br_startblock));xfs_bmbt_to_iomap (ip, iomap, &bma.got,0 , flags,xfs_iomap_inode_sequence (ip, flags));
fs/xfs/xfs_aops.c +31 -5
@@ -22,6 +22,7 @@#include "xfs_icache.h" #include "xfs_zone_alloc.h" #include "xfs_rtgroup.h" +#include "xfs_fsverity.h" #include <linux/bio-integrity.h> struct xfs_writepage_ctx {
@@ -350,12 +351,16 @@ xfs_map_blocks(int retries =0 ;int error =0 ;unsigned int *seq; +unsigned int iomap_flags =0 ;if (xfs_is_shutdown (mp))return -EIO ;XFS_ERRORTAG_DELAY (mp,XFS_ERRTAG_WB_DELAY_MS ); +if (xfs_iflags_test (ip,XFS_VERITY_CONSTRUCTION )) + iomap_flags |=IOMAP_F_FSVERITY ; +/* * COW fork blocks can overlap data fork blocks even if the blocks * aren't shared. COW I/O always takes precedent, so we must always
@@ -443,7 +448,8 @@ xfs_map_blocks( isnullstartblock(imap.br_startblock)) goto allocate_blocks; -xfs_bmbt_to_iomap (ip, &wpc->iomap, &imap,0 ,0 ,XFS_WPC (wpc)->data_seq); +xfs_bmbt_to_iomap (ip, &wpc->iomap, &imap,0 , iomap_flags, +XFS_WPC (wpc)->data_seq);trace_xfs_map_blocks_found (ip, offset, count, whichfork, &imap);return 0 ; allocate_blocks:
@@ -590,12 +596,16 @@ xfs_zoned_map_blocks(xfs_filblks_t count_fsb;struct xfs_bmbt_irec imap, del;struct xfs_iext_cursor icur; +u16 iomap_flags =0 ;if (xfs_is_shutdown (mp))return -EIO ;XFS_ERRORTAG_DELAY (mp,XFS_ERRTAG_WB_DELAY_MS ); +if (xfs_iflags_test (ip,XFS_VERITY_CONSTRUCTION )) + iomap_flags |=IOMAP_F_FSVERITY ; +/* * All dirty data must be covered by delalloc extents. But truncate can * remove delalloc extents underneath us or reduce their size.
@@ -619,7 +629,7 @@ xfs_zoned_map_blocks( imap.br_startblock =HOLESTARTBLOCK ; imap.br_state =XFS_EXT_NORM ;xfs_iunlock (ip,XFS_ILOCK_EXCL ); -xfs_bmbt_to_iomap (ip, &wpc->iomap, &imap,0 ,0 ,0 ); +xfs_bmbt_to_iomap (ip, &wpc->iomap, &imap,0 , iomap_flags,0 );return 0 ; } end_fsb =min (end_fsb, imap.br_startoff + imap.br_blockcount);
@@ -632,7 +642,7 @@ xfs_zoned_map_blocks(xfs_iunlock (ip,XFS_ILOCK_EXCL ); wpc->iomap.type =IOMAP_MAPPED ; - wpc->iomap.flags =IOMAP_F_DIRTY ; + wpc->iomap.flags = iomap_flags |IOMAP_F_DIRTY ; wpc->iomap.bdev = mp->m_rtdev_targp->bt_bdev; wpc->iomap.offset = offset; wpc->iomap.length =XFS_FSB_TO_B (mp, count_fsb);
@@ -684,6 +694,22 @@ static const struct iomap_writeback_ops xfs_zoned_writeback_ops = { .writeback_submit = xfs_zoned_writeback_submit, }; +static int +xfs_iomap_writepages ( +struct xfs_inode *ip, +struct iomap_writepage_ctx *ctx) +{ +/* +* Writeback does not work for folios past EOF, let it know that +* I/O happens for fsverity metadata and this restriction need +* to be skipped +*/ +if (xfs_iflags_test (ip,XFS_VERITY_CONSTRUCTION )) + ctx->iomap.flags |=IOMAP_F_FSVERITY ; + +return iomap_writepages (ctx); +} + STATIC int xfs_vm_writepages( struct address_space *mapping,
@@ -703,7 +729,7 @@ xfs_vm_writepages( };int error; - error =iomap_writepages (&xc.ctx); + error =xfs_iomap_writepages (ip, &xc.ctx);if (xc.open_zone)xfs_open_zone_put (xc.open_zone);return error;
@@ -716,7 +742,7 @@ xfs_vm_writepages( }, }; -return iomap_writepages (&wpc.ctx); +return xfs_iomap_writepages (ip, &wpc.ctx); } }
fs/xfs/xfs_fsverity.c new file +20 -0
@@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2026 Red Hat, Inc. +*/ +#include "xfs_platform.h" +#include "xfs_format.h" +#include "xfs_inode.h" +#include "xfs_shared.h" +#include "xfs_trans_resv.h" +#include "xfs_mount.h" +#include "xfs_fsverity.h" +#include <linux/fsverity.h> +#include <linux/iomap.h> + +loff_t +xfs_fsverity_metadata_offset ( +const struct xfs_inode *ip) +{ +return round_up (i_size_read (VFS_IC (ip)),XFS_FSVERITY_START_ALIGN ); +}
fs/xfs/xfs_fsverity.h new file +20 -0
@@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2026 Red Hat, Inc. +*/ +#ifndef __XFS_FSVERITY_H__ +#define __XFS_FSVERITY_H__ + +#include "xfs_platform.h" + +#ifdef CONFIG_FS_VERITY +loff_t xfs_fsverity_metadata_offset (const struct xfs_inode *ip); +#else +static inline loff_t xfs_fsverity_metadata_offset (const struct xfs_inode *ip) +{ +WARN_ON_ONCE (1 ); +return ULLONG_MAX ; +} +#endif /* CONFIG_FS_VERITY */ + +#endif /* __XFS_FSVERITY_H__ */
fs/xfs/xfs_inode.h +6 -0
@@ -419,6 +419,12 @@ static inline bool xfs_inode_can_sw_atomic_write(const struct xfs_inode *ip)*/ #define XFS_IREMAPPING (1U << 15) +/* +* fs-verity's Merkle tree is under construction. The file is read-only, the +* only writes happening are for the fsverity metadata. +*/ +#define XFS_VERITY_CONSTRUCTION (1U << 16) +/* All inode state flags related to inode reclaim. */ #define XFS_ALL_IRECLAIM_FLAGS (XFS_IRECLAIMABLE | \ XFS_IRECLAIM | \
fs/xfs/xfs_iomap.c +22 -7
@@ -32,6 +32,8 @@#include "xfs_rtbitmap.h" #include "xfs_icache.h" #include "xfs_zone_alloc.h" +#include "xfs_fsverity.h" +#include <linux/fsverity.h> #define XFS_ALLOC_ALIGN (mp, off) \ (((off) >> mp->m_allocsize_log) << mp->m_allocsize_log)
@@ -883,6 +885,9 @@ xfs_direct_write_iomap_begin(if (flags &IOMAP_ATOMIC ) iomap_flags |=IOMAP_F_ATOMIC_BIO ; +if (xfs_iflags_test (ip,XFS_VERITY_CONSTRUCTION )) + iomap_flags |=IOMAP_F_FSVERITY ; +/* * COW writes may allocate delalloc space or convert unwritten COW * extents, so we need to make sure to take the lock exclusively here.
@@ -1589,7 +1594,8 @@ xfs_zoned_buffered_write_iomap_begin(loff_t count, unsigned flags, struct iomap *iomap, - struct iomap *srcmap) + struct iomap *srcmap, + u16 iomap_flags) { struct iomap_iter *iter = container_of(iomap, struct iomap_iter, iomap);
@@ -1599,7 +1605,6 @@ xfs_zoned_buffered_write_iomap_begin(struct xfs_mount *mp = ip->i_mount;xfs_fileoff_t offset_fsb =XFS_B_TO_FSBT (mp, offset);xfs_fileoff_t end_fsb =xfs_iomap_end_fsb (mp, offset, count); -u16 iomap_flags =IOMAP_F_SHARED ;unsigned int lockmode =XFS_ILOCK_EXCL ;xfs_filblks_t count_fsb;xfs_extlen_t indlen;
@@ -1662,7 +1667,8 @@ xfs_zoned_buffered_write_iomap_begin( smap.br_startoff + smap.br_blockcount);xfs_trim_extent (&smap, offset_fsb, end_fsb - offset_fsb); - error =xfs_bmbt_to_iomap (ip, srcmap, &smap, flags,0 , + error =xfs_bmbt_to_iomap (ip, srcmap, &smap, flags, + iomap_flags,xfs_iomap_inode_sequence (ip,0 ));if (error) goto out_unlock;
@@ -1761,7 +1767,7 @@ xfs_zoned_buffered_write_iomap_begin( got.br_state =XFS_EXT_NORM ;xfs_bmap_add_extent_hole_delay (ip,XFS_COW_FORK , &icur, &got); ac->reserved_blocks -= count_fsb; - iomap_flags |=IOMAP_F_NEW ; + iomap_flags |= (IOMAP_F_NEW |IOMAP_F_SHARED );trace_xfs_iomap_alloc (ip, offset,XFS_FSB_TO_B (mp, count_fsb),XFS_COW_FORK , &got);
@@ -1803,9 +1809,12 @@ xfs_buffered_write_iomap_begin(if (xfs_is_shutdown (mp))return -EIO ; +if (xfs_iflags_test (ip,XFS_VERITY_CONSTRUCTION )) + iomap_flags |=IOMAP_F_FSVERITY ; +if (xfs_is_zoned_inode (ip))return xfs_zoned_buffered_write_iomap_begin (inode, offset, - count, flags, iomap, srcmap); + count, flags, iomap, srcmap, iomap_flags);/* we can't use delayed allocations when using extent size hints */ if (xfs_get_extsz_hint (ip))
@@ -2191,12 +2200,17 @@ xfs_read_iomap_begin(bool shared = false;unsigned int lockmode =XFS_ILOCK_SHARED ;u64 seq; +unsigned int iomap_flags =0 ;ASSERT (!(flags & (IOMAP_WRITE |IOMAP_ZERO )));if (xfs_is_shutdown (mp))return -EIO ; +if (fsverity_active (inode) && + (offset >=xfs_fsverity_metadata_offset (ip))) + iomap_flags |=IOMAP_F_FSVERITY ; + error =xfs_ilock_for_iomap (ip, flags, &lockmode);if (error)return error;
@@ -2210,8 +2224,9 @@ xfs_read_iomap_begin(if (error)return error;trace_xfs_iomap_found (ip, offset, length,XFS_DATA_FORK , &imap); -return xfs_bmbt_to_iomap (ip, iomap, &imap, flags, - shared ?IOMAP_F_SHARED :0 , seq); + iomap_flags |= shared ?IOMAP_F_SHARED :0 ; + +return xfs_bmbt_to_iomap (ip, iomap, &imap, flags, iomap_flags, seq); }const struct iomap_ops xfs_read_iomap_ops = {
Signature · 1 line
2.54.0
PATCH v12 13/21 xfs: use read ioend for fsverity data verification
In reply to Andrey AlbershteynUse read ioends for fsverity verification. Do not issues fsverity metadata I/O through the same workqueue due to risk of a deadlock by a filled workqueue.
Pass fsverity_info from iomap context down to the ioend as hashtable lookups are expensive.
Add a simple helper to check that this is not fsverity metadata but file data that needs verification.
--- fs/xfs/xfs_aops.c | 45 ++++++++++++++++++++++++++++++++++--------- fs/xfs/xfs_fsverity.c | 9 +++++++++ fs/xfs/xfs_fsverity.h | 6 ++++++ 3 files changed, 51 insertions(+), 9 deletions(-)
fs/xfs/xfs_aops.c +36 -9
@@ -24,6 +24,7 @@#include "xfs_rtgroup.h" #include "xfs_fsverity.h" #include <linux/bio-integrity.h> +#include <linux/fsverity.h> struct xfs_writepage_ctx {struct iomap_writepage_ctx ctx;
@@ -182,6 +183,23 @@ xfs_end_ioend_write(memalloc_nofs_restore (nofs_flag); } +/* +* IO read completion. +*/ +static void +xfs_end_ioend_read ( +struct iomap_ioend *ioend) +{ +struct xfs_inode *ip =XFS_I (ioend->io_inode); + +if (!ioend->io_bio.bi_status && +xfs_fsverity_is_file_data (ip, ioend->io_offset)) +fsverity_verify_bio (ioend->io_vi, + &ioend->io_bio); +iomap_finish_ioends (ioend, +blk_status_to_errno (ioend->io_bio.bi_status)); +} +/* * Finish all pending IO completions that require transactional modifications. *
@@ -216,8 +234,7 @@ xfs_end_io(list_del_init (&ioend->io_list);iomap_ioend_try_merge (ioend, &tmp);if (bio_op (&ioend->io_bio) ==REQ_OP_READ ) -iomap_finish_ioends (ioend, -blk_status_to_errno (ioend->io_bio.bi_status)); +xfs_end_ioend_read (ioend);else xfs_end_ioend_write (ioend);cond_resched ();
@@ -243,9 +260,14 @@ xfs_end_bio( }spin_lock_irqsave (&ip->i_ioend_lock, flags); -if (list_empty (&ip->i_ioend_list)) -WARN_ON_ONCE (!queue_work (mp->m_unwritten_workqueue, +if (list_empty (&ip->i_ioend_list)) { +if (IS_ENABLED (CONFIG_FS_VERITY ) && ioend->io_vi && + ioend->io_offset <xfs_fsverity_metadata_offset (ip)) +fsverity_enqueue_verify_work (&ip->i_ioend_work); +else +WARN_ON_ONCE (!queue_work (mp->m_unwritten_workqueue, &ip->i_ioend_work)); + }list_add_tail (&ioend->io_list, &ip->i_ioend_list);spin_unlock_irqrestore (&ip->i_ioend_lock, flags); }
@@ -787,9 +809,12 @@ xfs_bio_submit_read( struct iomap_read_folio_ctx *ctx) {struct bio *bio = ctx->read_ctx; +struct iomap_ioend *ioend;/* defer read completions to the ioend workqueue */ -iomap_init_ioend (iter->inode, bio, ctx->read_ctx_file_offset,0 ); + ioend =iomap_init_ioend (iter->inode, bio, ctx->read_ctx_file_offset, +0 ); + ioend->io_vi = ctx->vi;iomap_bio_submit_read_endio (iter, ctx, xfs_end_bio); }
@@ -801,11 +826,13 @@ static const struct iomap_read_ops xfs_iomap_read_ops = {static inline const struct iomap_read_ops *xfs_get_iomap_read_ops ( -const struct address_space *mapping) +const struct address_space *mapping, +loff_t position) {struct xfs_inode *ip =XFS_I (mapping->host); -if (bdev_has_integrity_csum (xfs_inode_buftarg (ip)->bt_bdev)) +if (bdev_has_integrity_csum (xfs_inode_buftarg (ip)->bt_bdev) || +xfs_fsverity_is_file_data (ip, position))return &xfs_iomap_read_ops;return &iomap_bio_read_ops; }
@@ -817,7 +844,7 @@ xfs_vm_read_folio( {struct iomap_read_folio_ctx ctx = { .cur_folio = folio }; - ctx.ops =xfs_get_iomap_read_ops (folio->mapping); + ctx.ops =xfs_get_iomap_read_ops (folio->mapping,folio_pos (folio));iomap_read_folio (&xfs_read_iomap_ops, &ctx,NULL );return 0 ; }
@@ -828,7 +855,7 @@ xfs_vm_readahead( {struct iomap_read_folio_ctx ctx = { .rac = rac }; - ctx.ops =xfs_get_iomap_read_ops (rac->mapping), + ctx.ops =xfs_get_iomap_read_ops (rac->mapping,readahead_pos (rac));iomap_readahead (&xfs_read_iomap_ops, &ctx,NULL ); }
fs/xfs/xfs_fsverity.c +9 -0
@@ -18,3 +18,12 @@ xfs_fsverity_metadata_offset( {return round_up (i_size_read (VFS_IC (ip)),XFS_FSVERITY_START_ALIGN ); } + +bool +xfs_fsverity_is_file_data ( +const struct xfs_inode *ip, +loff_t offset) +{ +return fsverity_active (VFS_IC (ip)) && + offset <xfs_fsverity_metadata_offset (ip); +}
fs/xfs/xfs_fsverity.h +6 -0
@@ -9,12 +9,18 @@#ifdef CONFIG_FS_VERITY loff_t xfs_fsverity_metadata_offset (const struct xfs_inode *ip); +bool xfs_fsverity_is_file_data (const struct xfs_inode *ip,loff_t offset);#else static inline loff_t xfs_fsverity_metadata_offset (const struct xfs_inode *ip) {WARN_ON_ONCE (1 );return ULLONG_MAX ; } +static inline bool xfs_fsverity_is_file_data (const struct xfs_inode *ip, +loff_t offset) +{ +return false; +}#endif /* CONFIG_FS_VERITY */ #endif /* __XFS_FSVERITY_H__ */
Signature · 1 line
2.54.0
PATCH v12 14/21 xfs: add fs-verity support
In reply to Andrey AlbershteynAdd integration with fs-verity. XFS stores fs-verity descriptor and Merkle tree in the inode data fork at first block aligned to 64k past EOF.
The Merkle tree reading/writing is done through iomap interface. The data itself is read to the inode's page cache. When XFS reads from this region iomap doesn't call into fsverity to verify it against Merkle tree. For data, verification is done at ioend completion in a workqueue.
When fs-verity is enabled on an inode, the XFS_IVERITY_CONSTRUCTION flag is set meaning that the Merkle tree is being build. The initialization ends with storing of verity descriptor and setting inode on-disk flag (XFS_DIFLAG2_VERITY). Lastly, the XFS_IVERITY_CONSTRUCTION is dropped and I_VERITY is set on inode.
The descriptor is stored in a new block aligned to 64k after the last Merkle tree block. The size of the descriptor is stored at the end of the last descriptor block (descriptor can be multiple blocks).
--- fs/xfs/xfs_bmap_util.c | 8 + fs/xfs/xfs_fsverity.c | 372 ++++++++++++++++++++++++++++++++++++++++- fs/xfs/xfs_fsverity.h | 2 + fs/xfs/xfs_message.c | 4 + fs/xfs/xfs_message.h | 1 + fs/xfs/xfs_mount.h | 2 + fs/xfs/xfs_super.c | 7 + 7 files changed, 395 insertions(+), 1 deletion(-)
fs/xfs/xfs_bmap_util.c +8 -0
@@ -31,6 +31,7 @@#include "xfs_rtbitmap.h" #include "xfs_rtgroup.h" #include "xfs_zone_alloc.h" +#include <linux/fsverity.h> /* Kernel only BMAP related definitions and functions */
@@ -553,6 +554,13 @@ xfs_can_free_eofblocks(if (last_fsb <= end_fsb)return false; +/* +* Nothing to clean on fsverity inodes as they don't use prealloc and +* there no delalloc as only written data is fsverity metadata +*/ +if (IS_VERITY (VFS_I (ip))) +return false; +/* * Check if there is an post-EOF extent to free. If there are any * delalloc blocks attached to the inode (data fork delalloc
fs/xfs/xfs_fsverity.c +371 -1
387 lines not on this page · raw message
fs/xfs/xfs_fsverity.h +2 -0
@@ -6,8 +6,10 @@#define __XFS_FSVERITY_H__#include "xfs_platform.h" +#include <linux/fsverity.h> #ifdef CONFIG_FS_VERITY +extern const struct fsverity_operations xfs_fsverity_ops;loff_t xfs_fsverity_metadata_offset (const struct xfs_inode *ip);bool xfs_fsverity_is_file_data (const struct xfs_inode *ip,loff_t offset);#else
fs/xfs/xfs_message.c +4 -0
@@ -149,6 +149,10 @@ xfs_warn_experimental( .opstate = XFS_OPSTATE_WARNED_LARP, .name = "logged extended attributes", }, + [XFS_EXPERIMENTAL_FSVERITY] = { + .opstate = XFS_OPSTATE_WARNED_FSVERITY, + .name = "fsverity", + }, };ASSERT (feat >=0 && feat <XFS_EXPERIMENTAL_MAX );BUILD_BUG_ON (ARRAY_SIZE (features) !=XFS_EXPERIMENTAL_MAX );
fs/xfs/xfs_message.h +1 -0
@@ -93,6 +93,7 @@ void xfs_buf_alert_ratelimited(struct xfs_buf *bp, const char *rlmsg,enum xfs_experimental_feat {XFS_EXPERIMENTAL_SHRINK ,XFS_EXPERIMENTAL_LARP , +XFS_EXPERIMENTAL_FSVERITY ,XFS_EXPERIMENTAL_MAX , };
fs/xfs/xfs_mount.h +2 -0
@@ -584,6 +584,8 @@ __XFS_HAS_FEAT(nouuid, NOUUID)#define XFS_OPSTATE_RESUMING_QUOTAON 18/* (Zoned) GC is in progress */ #define XFS_OPSTATE_ZONEGC_RUNNING 20 +/* Kernel has logged a warning about fsverity support */ +#define XFS_OPSTATE_WARNED_FSVERITY 21#define __XFS_IS_OPSTATE (name,NAME ) \ static inline bool xfs_is_ ## name (struct xfs_mount *mp) \
fs/xfs/xfs_super.c +7 -0
@@ -30,6 +30,7 @@#include "xfs_filestream.h" #include "xfs_quota.h" #include "xfs_sysfs.h" +#include "xfs_fsverity.h" #include "xfs_ondisk.h" #include "xfs_rmap_item.h" #include "xfs_refcount_item.h"
@@ -1689,6 +1690,9 @@ xfs_fs_fill_super( sb->s_quota_types =QTYPE_MASK_USR |QTYPE_MASK_GRP |QTYPE_MASK_PRJ ;#endif sb->s_op = &xfs_super_operations; +#ifdef CONFIG_FS_VERITY + sb->s_vop = &xfs_fsverity_ops; +#endif /* * Delay mount work if the debug hook is set. This is debug
@@ -1941,6 +1945,9 @@ xfs_fs_fill_super(if (error) goto out_filestream_unmount; +if (xfs_has_verity (mp)) +xfs_warn_experimental (mp,XFS_EXPERIMENTAL_FSVERITY ); + root =igrab (VFS_I (mp->m_rootip));if (!root) { error = -ENOENT ;
Signature · 1 line
2.54.0
PATCH v12 15/21 xfs: remove unwritten extents after preallocations in fsverity metadata
In reply to Andrey AlbershteynXFS preallocates spaces during writes. In normal I/O this space, if unused, is removed by truncate. For files with fsverity XFS does not use truncate as fsverity metadata is stored past EOF.
After we're done with writing fsverity metadata iterate over extents in that region and remove any unwritten ones. These would be left overs in the holes in the merkle tree and past fsverity descriptor.
--- fs/xfs/xfs_fsverity.c | 79 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+)
fs/xfs/xfs_fsverity.c +79 -0
@@ -12,12 +12,15 @@#include "xfs_inode.h" #include "xfs_log_format.h" #include "xfs_trans.h" +#include "xfs_defer.h" #include "xfs_trace.h" #include "xfs_quota.h" #include "xfs_fsverity.h" #include "xfs_iomap.h" #include "xfs_error.h" #include "xfs_health.h" +#include "xfs_bmap.h" +#include "xfs_bmap_util.h" #include <linux/fsverity.h> #include <linux/iomap.h> #include <linux/pagemap.h>
@@ -179,6 +182,74 @@ xfs_fsverity_delete_metadata(return error; } +static int +xfs_fsverity_cancel_unwritten ( +struct xfs_inode *ip, +loff_t start, +loff_t end) +{ +struct xfs_mount *mp = ip->i_mount; +struct xfs_trans *tp; +xfs_fileoff_t offset_fsb =XFS_B_TO_FSBT (mp, start); +xfs_fileoff_t end_fsb =XFS_B_TO_FSB (mp, end); +struct xfs_bmbt_irec imap; +int nimaps; +int error =0 ; +int done; + +while (offset_fsb < end_fsb) { + nimaps =1 ; + + error =xfs_trans_alloc (mp, &M_RES (mp)->tr_write,0 ,0 ,0 , &tp); +if (error) +return error; + +xfs_ilock (ip,XFS_ILOCK_EXCL ); + error =xfs_bmapi_read (ip, offset_fsb, + end_fsb - offset_fsb, &imap, &nimaps, +0 ); +if (error) + goto out_cancel; + +if (nimaps ==0 ) + goto out_cancel; + +if (imap.br_state ==XFS_EXT_UNWRITTEN ) { + done =0 ; + +xfs_trans_ijoin (tp, ip,0 ); + +while (!done) { + error =xfs_bunmapi (tp, ip, imap.br_startoff, + imap.br_blockcount,0 ,1 , + &done); +if (error) + goto out_cancel; + + error =xfs_defer_finish (&tp); +if (error) + goto out_cancel; + } + + error =xfs_trans_commit (tp); +if (error) + goto out_unlock; + }else { +xfs_trans_cancel (tp); + } +xfs_iunlock (ip,XFS_ILOCK_EXCL ); + + offset_fsb = imap.br_startoff + imap.br_blockcount; + } + +return error; +out_cancel: +xfs_trans_cancel (tp); +out_unlock: +xfs_iunlock (ip,XFS_ILOCK_EXCL ); +return error; +} +/* * Prepare to enable fsverity by clearing old metadata.
@@ -262,6 +333,14 @@ xfs_fsverity_end_enable(if (error) goto out; +/* +* Remove unwritten extents left by COW preallocations and write +* preallocation in the merkle tree holes and past descriptor +*/ + error =xfs_fsverity_cancel_unwritten (ip, range_start,LLONG_MAX ); +if (error) + goto out; +/* * Proactively drop any delayed allocations in COW fork, the fsverity * files are read-only
Signature · 1 line
2.54.0
PATCH v12 16/21 xfs: initialize fs-verity on file open
In reply to Andrey Albershteynfs-verity will read and attach metadata (not the tree itself) from a disk for those inodes which already have fs-verity enabled.
--- fs/xfs/xfs_file.c | 8 ++++++++ 1 file changed, 8 insertions(+)
fs/xfs/xfs_file.c +8 -0
@@ -37,6 +37,7 @@#include <linux/fadvise.h> #include <linux/mount.h> #include <linux/filelock.h> +#include <linux/fsverity.h> static const struct vm_operations_struct xfs_file_vm_ops;
@@ -1719,11 +1720,18 @@ xfs_file_open(struct inode *inode, struct file *file) { + int error; +if (xfs_is_shutdown (XFS_M (inode->i_sb)))return -EIO ; file->f_mode |=FMODE_NOWAIT |FMODE_CAN_ODIRECT ;if (xfs_get_atomic_write_min (XFS_I (inode)) >0 ) file->f_mode |=FMODE_CAN_ATOMIC_WRITE ; + + error =fsverity_file_open (inode, file); +if (error) +return error; +return generic_file_open (inode, file); }
Signature · 1 line
2.54.0
PATCH v12 17/21 xfs: add fs-verity ioctls
In reply to Andrey AlbershteynAdd fs-verity ioctls to enable, dump metadata (descriptor and Merkle tree pages) and obtain file's digest.
[djwong: remove unnecessary casting]
--- fs/xfs/xfs_ioctl.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
fs/xfs/xfs_ioctl.c +14 -0
@@ -49,6 +49,7 @@#include <linux/mount.h> #include <linux/fileattr.h> +#include <linux/fsverity.h> /* Return 0 on success or positive error */ int
@@ -1466,6 +1467,19 @@ xfs_file_ioctl(case XFS_IOC_VERIFY_MEDIA :return xfs_ioc_verify_media (filp, arg); +case FS_IOC_ENABLE_VERITY : +if (!xfs_has_verity (mp)) +return -EOPNOTSUPP ; +return fsverity_ioctl_enable (filp, arg); +case FS_IOC_MEASURE_VERITY : +if (!xfs_has_verity (mp)) +return -EOPNOTSUPP ; +return fsverity_ioctl_measure (filp, arg); +case FS_IOC_READ_VERITY_METADATA : +if (!xfs_has_verity (mp)) +return -EOPNOTSUPP ; +return fsverity_ioctl_read_metadata (filp, arg); +default :return -ENOTTY ; }
Signature · 1 line
2.54.0
PATCH v12 18/21 xfs: advertise fs-verity being available on filesystem
In reply to Andrey AlbershteynFrom: "Darrick J. Wong" <[email protected]>
Advertise that this filesystem supports fsverity.
--- fs/xfs/libxfs/xfs_fs.h | 1 + fs/xfs/libxfs/xfs_sb.c | 2 ++ 2 files changed, 3 insertions(+)
fs/xfs/libxfs/xfs_fs.h +1 -0
@@ -250,6 +250,7 @@ typedef struct xfs_fsop_resblks {#define XFS_FSOP_GEOM_FLAGS_PARENT (1 << 25)/* linux parent pointers */ #define XFS_FSOP_GEOM_FLAGS_METADIR (1 << 26)/* metadata directories */ #define XFS_FSOP_GEOM_FLAGS_ZONED (1 << 27)/* zoned rt device */ +#define XFS_FSOP_GEOM_FLAGS_VERITY (1 << 28)/* fs-verity */ /* * Minimum and maximum sizes need for growth checks.
fs/xfs/libxfs/xfs_sb.c +2 -0
@@ -1590,6 +1590,8 @@ xfs_fs_geometry( geo->flags |=XFS_FSOP_GEOM_FLAGS_METADIR ;if (xfs_has_zoned (mp)) geo->flags |=XFS_FSOP_GEOM_FLAGS_ZONED ; +if (xfs_has_verity (mp)) + geo->flags |=XFS_FSOP_GEOM_FLAGS_VERITY ; geo->rtsectsize = sbp->sb_blocksize; geo->dirblocksize =xfs_dir2_dirblock_bytes (sbp);
Signature · 1 line
2.54.0
PATCH v12 19/21 xfs: check and repair the verity inode flag state
In reply to Andrey AlbershteynFrom: "Darrick J. Wong" <[email protected]>
If an inode has the incore verity iflag set, make sure that we can actually activate fsverity on that inode. If activation fails due to a fsverity metadata validation error, clear the flag. The usage model for fsverity requires that any program that cares about verity state is required to call statx/getflags to check that the flag is set after opening the file, so clearing the flag will not compromise that model.
--- fs/xfs/scrub/common.c | 55 +++++++++++++++++++++++++++++++++++++ fs/xfs/scrub/common.h | 2 ++ fs/xfs/scrub/inode.c | 7 +++++ fs/xfs/scrub/inode_repair.c | 36 ++++++++++++++++++++++++ 4 files changed, 100 insertions(+)
fs/xfs/scrub/common.c +55 -0
@@ -45,6 +45,8 @@#include "scrub/health.h" #include "scrub/tempfile.h" +#include <linux/fsverity.h> +/* Common code for the metadata scrubbers. */ /*
@@ -1754,3 +1756,56 @@ xchk_inode_count_blocks(return xfs_bmap_count_blocks (sc->tp, sc->ip, whichfork, nextents, count); } + +/* +* If this inode has S_VERITY set on it, read the verity info. If the reading +* fails with anything other than ENOMEM, the file is corrupt, which we can +* detect later with fsverity_active. +* +* Callers must hold the IOLOCK and must not hold the ILOCK of sc->ip because +* activation reads inode data. +*/ +int +xchk_inode_setup_verity ( +struct xfs_scrub *sc) +{ +int error; + +if (!fsverity_active (VFS_I (sc->ip))) +return 0 ; + + error =fsverity_ensure_verity_info (VFS_I (sc->ip)); +switch (error) { +case 0 : +/* fsverity is active */ +break ; +case -ENODATA : +case -EMSGSIZE : +case -EINVAL : +case -EFSCORRUPTED : +case -EFBIG : +case -ERANGE : +case -EBADMSG : +/* +* The nonzero errno codes above are the error codes that can +* be returned from fsverity on metadata validation errors. +*/ +return 0 ; +default : +/* runtime errors */ +return error; + } + +return 0 ; +} + +/* +* Is this a verity file that failed to activate? Callers must have tried to +* activate fsverity via xchk_inode_setup_verity. +*/ +bool +xchk_inode_verity_broken ( +struct xfs_inode *ip) +{ +return fsverity_active (VFS_I (ip)) && !fsverity_get_info (VFS_I (ip)); +}
fs/xfs/scrub/common.h +2 -0
@@ -268,6 +268,8 @@ int xchk_inode_is_allocated(struct xfs_scrub *sc, xfs_agino_t agino, bool *inuse);int xchk_inode_count_blocks (struct xfs_scrub *sc,int whichfork,xfs_extnum_t *nextents,xfs_filblks_t *count); +int xchk_inode_setup_verity (struct xfs_scrub *sc); +bool xchk_inode_verity_broken (struct xfs_inode *ip);bool xchk_inode_is_dirtree_root (const struct xfs_inode *ip);bool xchk_inode_is_sb_rooted (const struct xfs_inode *ip);
fs/xfs/scrub/inode.c +7 -0
@@ -36,6 +36,10 @@ xchk_prepare_iscrub(xchk_ilock (sc,XFS_IOLOCK_EXCL ); + error =xchk_inode_setup_verity (sc); +if (error) +return error; + error =xchk_trans_alloc (sc,0 );if (error)return error;
@@ -833,6 +837,9 @@ xchk_inode(if (S_ISREG (VFS_I (sc->ip)->i_mode))xchk_inode_check_reflink_iflag (sc,I_INO (sc->ip)); +if (xchk_inode_verity_broken (sc->ip)) +xchk_ino_set_corrupt (sc, sc->sm->sm_ino); +xchk_inode_check_unlinked (sc);xchk_inode_xref (sc,I_INO (sc->ip), &di);
fs/xfs/scrub/inode_repair.c +36 -0
@@ -573,6 +573,8 @@ xrep_dinode_flags( dip->di_nrext64_pad =0 ; else if (dip->di_version >= 3) dip->di_v3_pad = 0; +if (!xfs_has_verity (mp) || !S_ISREG (mode)) + flags2 &= ~XFS_DIFLAG2_VERITY ;if (flags2 &XFS_DIFLAG2_METADATA ) {xfs_failaddr_t fa;
@@ -1617,6 +1619,10 @@ xrep_dinode_core(if (iget_error)return iget_error; + error =xchk_inode_setup_verity (sc); +if (error) +return error; + error =xchk_trans_alloc (sc,0 );if (error)return error;
@@ -2035,6 +2041,27 @@ xrep_inode_unlinked(return 0 ; } +/* +* If this file is a fsverity file, xchk_prepare_iscrub or xrep_dinode_core +* should have activated it. If it's still not active, then there's something +* wrong with the verity descriptor and we should turn it off. +*/ +STATIC int +xrep_inode_verity ( +struct xfs_scrub *sc) +{ +struct inode *inode =VFS_I (sc->ip); + +if (xchk_inode_verity_broken (sc->ip)) { + sc->ip->i_diflags2 &= ~XFS_DIFLAG2_VERITY ; +inode_set_flags (inode,0 ,S_VERITY ); + +xfs_trans_log_inode (sc->tp, sc->ip,XFS_ILOG_CORE ); + } + +return 0 ; +} +/* Repair an inode's fields. */ int xrep_inode(
@@ -2084,6 +2111,15 @@ xrep_inode(return error; } +/* +* Disable fsverity if it cannot be activated. Activation failure +* prohibits the file from being opened, so there cannot be another +* program with an open fd to what it thinks is a verity file. +*/ + error =xrep_inode_verity (sc); +if (error) +return error; +/* Reconnect incore unlinked list */ error =xrep_inode_unlinked (sc);if (error)
Signature · 1 line
2.54.0
PATCH v12 20/21 xfs: introduce health state for corrupted fsverity metadata
In reply to Andrey AlbershteynReport corrupted fsverity descriptor through health system.
--- fs/xfs/libxfs/xfs_fs.h | 1 + fs/xfs/libxfs/xfs_health.h | 4 +++- fs/xfs/xfs_fsverity.c | 13 ++++++++++--- fs/xfs/xfs_health.c | 1 + 4 files changed, 15 insertions(+), 4 deletions(-)
fs/xfs/libxfs/xfs_fs.h +1 -0
@@ -422,6 +422,7 @@ struct xfs_bulkstat {#define XFS_BS_SICK_SYMLINK (1 << 6)/* symbolic link remote target */ #define XFS_BS_SICK_PARENT (1 << 7)/* parent pointers */ #define XFS_BS_SICK_DIRTREE (1 << 8)/* directory tree structure */ +#define XFS_BS_SICK_FSVERITY (1 << 9)/* fsverity metadata */ /* * Project quota id helpers (previously projid was 16bit only
fs/xfs/libxfs/xfs_health.h +3 -1
@@ -104,6 +104,7 @@ struct xfs_rtgroup;/* Don't propagate sick status to ag health summary during inactivation */ #define XFS_SICK_INO_FORGET (1 << 12)#define XFS_SICK_INO_DIRTREE (1 << 13)/* directory tree structure */ +#define XFS_SICK_INO_FSVERITY (1 << 14)/* fsverity metadata */ /* Primary evidence of health problems in a given group. */ #define XFS_SICK_FS_PRIMARY (XFS_SICK_FS_COUNTERS | \
@@ -140,7 +141,8 @@ struct xfs_rtgroup; XFS_SICK_INO_XATTR | \ XFS_SICK_INO_SYMLINK | \ XFS_SICK_INO_PARENT | \ - XFS_SICK_INO_DIRTREE) + XFS_SICK_INO_DIRTREE | \ + XFS_SICK_INO_FSVERITY)#define XFS_SICK_INO_ZAPPED (XFS_SICK_INO_BMBTD_ZAPPED | \ XFS_SICK_INO_BMBTA_ZAPPED | \
fs/xfs/xfs_fsverity.c +10 -3
@@ -84,16 +84,23 @@ xfs_fsverity_get_descriptor(return error; desc_size =be32_to_cpu (d_desc_size); -if (XFS_IS_CORRUPT (mp, desc_size >FS_VERITY_MAX_DESCRIPTOR_SIZE )) +if (XFS_IS_CORRUPT (mp, desc_size >FS_VERITY_MAX_DESCRIPTOR_SIZE )) { +xfs_inode_mark_sick (XFS_I (inode),XFS_SICK_INO_FSVERITY );return -ERANGE ; -if (XFS_IS_CORRUPT (mp, desc_size > desc_size_pos)) + } + +if (XFS_IS_CORRUPT (mp, desc_size > desc_size_pos)) { +xfs_inode_mark_sick (XFS_I (inode),XFS_SICK_INO_FSVERITY );return -ERANGE ; + }if (!buf_size)return desc_size; -if (XFS_IS_CORRUPT (mp, desc_size > buf_size)) +if (XFS_IS_CORRUPT (mp, desc_size > buf_size)) { +xfs_inode_mark_sick (XFS_I (inode),XFS_SICK_INO_FSVERITY );return -ERANGE ; + } desc_pos =round_down (desc_size_pos - desc_size, blocksize);if (desc_pos <xfs_fsverity_metadata_offset (ip)) {
fs/xfs/xfs_health.c +1 -0
@@ -625,6 +625,7 @@ static const struct ioctl_sick_map ino_map[] = { { XFS_SICK_INO_DIR_ZAPPED, XFS_BS_SICK_DIR }, { XFS_SICK_INO_SYMLINK_ZAPPED, XFS_BS_SICK_SYMLINK }, { XFS_SICK_INO_DIRTREE, XFS_BS_SICK_DIRTREE }, + { XFS_SICK_INO_FSVERITY, XFS_BS_SICK_FSVERITY }, };/* Fill out bulkstat health info. */
Signature · 1 line
2.54.0
PATCH v12 21/21 xfs: enable ro-compat fs-verity flag
In reply to Andrey AlbershteynFinalize fs-verity integration in XFS by making kernel fs-verity aware with ro-compat flag.
--- fs/xfs/libxfs/xfs_format.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
fs/xfs/libxfs/xfs_format.h +3 -2
@@ -378,8 +378,9 @@ xfs_sb_has_compat_feature(#define XFS_SB_FEAT_RO_COMPAT_ALL \ (XFS_SB_FEAT_RO_COMPAT_FINOBT | \ XFS_SB_FEAT_RO_COMPAT_RMAPBT | \ - XFS_SB_FEAT_RO_COMPAT_REFLINK| \ - XFS_SB_FEAT_RO_COMPAT_INOBTCNT) + XFS_SB_FEAT_RO_COMPAT_REFLINK | \ + XFS_SB_FEAT_RO_COMPAT_INOBTCNT | \ + XFS_SB_FEAT_RO_COMPAT_VERITY)#define XFS_SB_FEAT_RO_COMPAT_UNKNOWN ~XFS_SB_FEAT_RO_COMPAT_ALLstatic inline bool xfs_sb_has_ro_compat_feature(
Signature · 1 line
2.54.0
I will send v13 shortly with more sashiko.dev fixes. I run these AI reviews locally but model is not that good.
Signature · 1 line
- Andrey