exfat: add limited FALLOC_FL_ZERO_RANGE support
Subsystems (2)
cover exfat: add limited FALLOC_FL_ZERO_RANGE support
Fixes
- returning to userspace w/ inode lock being held in sync mode
- Errors in commit message
Davo
David Timber (1):
exfat: add limited FALLOC_FL_ZERO_RANGE support
fs/exfat/file.c | 75 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 64 insertions(+), 11 deletions(-)
Signature · 1 line
2.53.0.1.ga224b40d3f.dirty
PATCH 1/1 exfat: add limited FALLOC_FL_ZERO_RANGE support
As means of truncating the VDL of a regular file while maintaining the layout of the allocated clusters, allow the use fallocate mode FALLOC_FL_ZERO_RANGE with the range that covers EOF, with the support of optional FALLOC_FL_KEEP_SIZE flag.
To reset the VDL to 0, userspace may use fallocate() like so:
fallocate(fd, FALLOC_FL_ZERO_RANGE|FALLOC_FL_KEEP_SIZE, 0,
lseek(fd, 0, SEEK_END));FALLOC_FL_KEEP_SIZE flag is for multiple users to guard the file from TOCTOU conditions. Without the flag, the behaviour is the same as FALLOC_FL_ALLOCATE_RANGE.
--- fs/exfat/file.c | 75 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 64 insertions(+), 11 deletions(-)
fs/exfat/file.c +64 -11
@@ -36,7 +36,8 @@ static int exfat_cont_expand(struct inode *inode, loff_t size) num_clusters =EXFAT_B_TO_CLU (exfat_ondisk_size (inode), sbi); new_num_clusters =EXFAT_B_TO_CLU_ROUND_UP (size, sbi); -if (new_num_clusters == num_clusters) +WARN_ON (new_num_clusters < num_clusters); +if (new_num_clusters <= num_clusters) goto out;if (num_clusters) {
@@ -94,35 +95,87 @@ static int exfat_cont_expand(struct inode *inode, loff_t size)/* * Preallocate space for a file. This implements exfat's fallocate file * operation, which gets called from sys_fallocate system call. User space -* requests len bytes at offset. In contrary to fat, we only support -* FALLOC_FL_ALLOCATE_RANGE because by leaving the valid data length(VDL) -* field, it is unnecessary to zero out the newly allocated clusters. +* requests len bytes at offset. +* +* In contrary to fat, FALLOC_FL_ALLOCATE_RANGE can be done without zeroing out +* the newly allocated clusters by leaving the valid data length(VDL) field +* unchanged. +* +* Due to the inherent limitation of the VDL scheme, FALLOC_FL_ZERO_RANGE is +* only possible when the requested range covers EOF. */ static long exfat_fallocate (struct file *file,int mode,loff_t offset,loff_t len) {struct inode *inode = file->f_mapping->host; -loff_t newsize = offset + len; +loff_t newsize, isize;int err =0 ;/* No support for other modes */ -if (mode !=FALLOC_FL_ALLOCATE_RANGE ) +switch (mode) { +case FALLOC_FL_ALLOCATE_RANGE : +case FALLOC_FL_ZERO_RANGE : +case FALLOC_FL_ZERO_RANGE |FALLOC_FL_KEEP_SIZE : +break ; +default :return -EOPNOTSUPP ; + }/* No support for dir */ if (!S_ISREG (inode->i_mode)) -return -EOPNOTSUPP ; +return mode &FALLOC_FL_ZERO_RANGE ? -EINVAL : -EOPNOTSUPP ;if (unlikely (exfat_forced_shutdown (inode->i_sb)))return -EIO ;inode_lock (inode); -if (newsize <=i_size_read (inode)) - goto error; + newsize = offset + len; + isize =i_size_read (inode); + +if (mode &FALLOC_FL_ZERO_RANGE ) { +struct exfat_inode_info *ei =EXFAT_I (inode); +loff_t saved_validsize = ei->valid_size; + +/* The requested range must span to or past EOF */ +if (newsize < isize) { + err = -EOPNOTSUPP ; + goto error; + } + +/* valid_size can only be truncated */ +if (offset < ei->valid_size) + ei->valid_size = offset; +/* If offset >= ei->valid_size, the range is already zeroed so that'd be no-op */ + +if (!(mode &FALLOC_FL_KEEP_SIZE ) && isize < newsize) + err =exfat_cont_expand (inode, newsize); +/* inode invalidated in exfat_cont_expand() */ +else { +/* update inode */ +inode_set_mtime_to_ts (inode,inode_set_ctime_current (inode)); +mark_inode_dirty (inode); + +if (IS_SYNC (inode)) + err =write_inode_now (inode,1 ); + } + +if (err) { +/* inode unchanged - revert valid_size */ + ei->valid_size = saved_validsize; + goto error; + } + +/* drop cache after the new valid_size */ +if (ei->valid_size != saved_validsize) +truncate_pagecache (inode, ei->valid_size); + }else {/* mode == FALLOC_FL_ALLOCATE_RANGE */ +if (newsize <= isize) + goto error; -/* This is just an expanding truncate */ - err =exfat_cont_expand (inode, newsize); +/* This is just an expanding truncate */ + err =exfat_cont_expand (inode, newsize); + } error:inode_unlock (inode);
Signature · 1 line
2.53.0.1.ga224b40d3f.dirty
Namjae Jeon
maintainerQuoted 6 lines · David Timber
As means of truncating the VDL of a regular file while maintaining the layout of the allocated clusters, allow the use fallocate mode
FALLOC_FL_ZERO_RANGEwith the range that covers EOF, with the support of optionalFALLOC_FL_KEEP_SIZEflag.
Quoted 8 lines
To reset the VDL to 0, userspace may use
fallocate()like so:fallocate(fd, FALLOC_FL_ZERO_RANGE|FALLOC_FL_KEEP_SIZE, 0,
lseek(fd, 0, SEEK_END));
FALLOC_FL_KEEP_SIZEflag is for multiple users to guard the file from TOCTOU conditions. Without the flag, the behaviour is the same asFALLOC_FL_ALLOCATE_RANGE.
Why is this limited FALLOC_FL_ZERO_RANGE support actually needed in applications?
What are the practical use cases or benefits for applications?
fs/exfat/file.c static int exfat_cont_expand(struct inode *inode, loff_t size) · line 126
Quoted 5 lines
/* No support for dir */ if (!S_ISREG(inode->i_mode))fs/exfat/file.c +1
-1-return -EOPNOTSUPP ; +return mode &FALLOC_FL_ZERO_RANGE ? -EINVAL : -EOPNOTSUPP ;
Why should we return -EINVAL specifically when FALLOC_FL_ZERO_RANGE is used ? There are already the checks for non-regular files in vfs_fallocate().
Thanks.
Quoted 3 lines · Namjae Jeon
Why is this limited
FALLOC_FL_ZERO_RANGEsupport actually needed in applications?
What are the practical use cases or benefits for applications?
Please see the original cover letter at
https://lore.kernel.org/linux-fsdevel/[email protected]/
To answer the question: zeroing out entire file leaving the cluster chain intact to avoid I/O and flash wear
Quoted 5 lines
fs/exfat/file.c static int exfat_cont_expand(struct inode *inode, loff_t size) · line 126
Quoted 4 lines
/* No support for dir */ if (!S_ISREG(inode->i_mode))fs/exfat/file.c +1
-1-return -EOPNOTSUPP ; +return mode &FALLOC_FL_ZERO_RANGE ? -EINVAL : -EOPNOTSUPP ;Why should we return
-EINVALspecifically whenFALLOC_FL_ZERO_RANGEis used ?
The reason why a different errno SHOULD be returned could be found in fallocate(2):
EINVAL mode is FALLOC_FL_COLLAPSE_RANGE, *FALLOC_FL_ZERO_RANGE*, or FALLOC_FL_INSERT_RANGE, but the file referred to by fd is not a regular file.
Again, (circling back to the xfstests EFBIG issue at hand) this is more of correctness in behaviour.
Quoted 1 line
There are already the checks for non-regular files in
vfs_fallocate().
Yes and no. I understand the check might seem meaningless in exFAT because the types of files it support are just directories and regular files. It might seem as a leftover from the vfat codebase, but only the cases of ENODEV, ESPIPE and EISDIR are covered in vfs_fallocate(). fallocate() on "exotic" files like unix socket is implementation-defined. Such edge cases could slip through, so the check is not enirely meaningless.
As per the EINVAL, the case is not covered in vfs_fallocate() so it has to be returned by the fs that supports FALLOC_FL_ZERO_RANGE.
I'd like to add that relying on the checks in the abstraction layer is not a good practice especially when implementing a non-UNIX filesystem like exFAT and NTFS. The POSIX spec will evolve over time and things will get added to the spec(SEEK_HOLE being the prime example).
Davo
Namjae Jeon
maintainerQuoted 10 lines · David Timber
Quoted 3 lines · Namjae Jeon
Why is this limited
FALLOC_FL_ZERO_RANGEsupport actually needed in applications?
What are the practical use cases or benefits for applications?Please see the original cover letter at
https://lore.kernel.org/linux-fsdevel/[email protected]/To answer the question: zeroing out entire file leaving the cluster chain intact to avoid I/O and flash wear
I have some concerns about adding this limited FALLOC_FL_ZERO_RANGE support to exfat. It is already possible to achieve a very similar result using ftruncate() and fallocate(FALLOC_FL_ALLOCATE_RANGE). First, call ftruncate(fd, 0) to adjust the file size and Valid Data Length (VDL). Then, call ftruncate(fd, new_size) or use fallocate(FALLOC_FL_ALLOCATE_RANGE, newsize) to preallocate clusters. Additionally, The proposed implementation is quite restricted. It only works when the range covers EOF unlike the standard FALLOC_FL_ZERO_RANGE behavior documented in the man page and implemented in other filesystems. This may confuse userspace tools.
Quoted 6 lines · Namjae Jeon
I have some concerns about adding this limited
FALLOC_FL_ZERO_RANGEsupport to exfat. It is already possible to achieve a very similar result usingftruncate()andfallocate(FALLOC_FL_ALLOCATE_RANGE). First, callftruncate(fd, 0)to adjust the file size and Valid Data Length (VDL). Then, callftruncate(fd, new_size)or usefallocate(FALLOC_FL_ALLOCATE_RANGE, newsize)to preallocate clusters.
The method you've described involves destruction and reconstruction of the FAT chain. With the current cluster allocation scheme(just return the next one available doing O(N) search), the chance of the newly constructed file end up having NO_FAT_CHAIN is quite low. FAT chain construction is not particularly good on flash.
Quoted 4 lines
Additionally, The proposed implementation is quite restricted. It only works when the range covers EOF unlike the standard
FALLOC_FL_ZERO_RANGEbehavior documented in the man page and implemented in other filesystems. This may confuse userspace tools.
Yes. Fair point.
I spent some time decompiling the exfat.sys driver, basically an implementation copied-and-pasted from FASTFAT, in Windows and realised that the problem being addressed here will simply go away if the Linux in-kernel exFAT would employ an allocation algorithm somewhat on par with that of Windows(which prioritises the use of contiguous runs of clusters).
It was just an idea but rather a cheap one. Ultimately, the fragmentation issue has to be solved by applying a salient cluster allocation algorithm.
Davo
Namjae Jeon
maintainerQuoted 8 lines · David Timber
Quoted 6 lines · Namjae Jeon
I have some concerns about adding this limited
FALLOC_FL_ZERO_RANGEsupport to exfat. It is already possible to achieve a very similar result usingftruncate()andfallocate(FALLOC_FL_ALLOCATE_RANGE). First, callftruncate(fd, 0)to adjust the file size and Valid Data Length (VDL). Then, callftruncate(fd, new_size)or usefallocate(FALLOC_FL_ALLOCATE_RANGE, newsize)to preallocate clusters.
Quoted 5 lines
The method you've described involves destruction and reconstruction of the FAT chain. With the current cluster allocation scheme(just return the next one available doing O(N) search), the chance of the newly constructed file end up having
NO_FAT_CHAINis quite low. FAT chain construction is not particularly good on flash.
This is probably why the EXFAT_IOC_SET_VALID_DATA ioctl you suggested earlier would be useful for exfat. However, to prevent the exposure of stale security-sensitive data, we should restrict the ioctl so that it only allows changing the VDL to a smaller value than the current one.
Quoted 4 lines
Quoted 4 lines
Additionally, The proposed implementation is quite restricted. It only works when the range covers EOF unlike the standard
FALLOC_FL_ZERO_RANGEbehavior documented in the man page and implemented in other filesystems. This may confuse userspace tools.
Quoted 8 lines
Yes. Fair point.
I spent some time decompiling the exfat.sys driver, basically an implementation copied-and-pasted from FASTFAT, in Windows and realised that the problem being addressed here will simply go away if the Linux in-kernel exFAT would employ an allocation algorithm somewhat on par with that of Windows(which prioritises the use of contiguous runs of clusters).
Sounds great! I'm looking forward to seeing your patch
Quoted 4 lines
It was just an idea but rather a cheap one. Ultimately, the fragmentation issue has to be solved by applying a salient cluster allocation algorithm.
Okay. I appreciate your efforts on this.
Quoted 2 lines
Davo