xfs: add FALLOC_FL_WRITE_ZEROES to XFS code base
RFC PATCH 1/1 xfs: add FALLOC_FL_WRITE_ZEROES to XFS code base
Add support for FALLOC_FL_WRITE_ZEROES if the underlying device enable the unmap write zeroes operation.
Inspired by the Ext4 implementation of the FALLOC_FL_WRITE_ZEROES. It can speed up some patterns on specific hardware.
time ( ./fallocate -l 360M /mnt/test.file; dd if=/dev/zero of=/mnt/test.file \
bs=1M count=360 conv=notrunc,nocreat oflag=direct,dsync)
360+0 records in
360+0 records out
377487360 bytes (377 MB, 360 MiB) copied, 22.0027 s, 17.2 MB/s
real 0m22.114s
user 0m0.006s
sys 0m3.085s
time (./fallocate -wl 360M /mnt/test.file; dd if=/dev/zero of=/mnt/test.file \
bs=1M count=360 conv=notrunc,nocreat oflag=direct,dsync );
360+0 records in
360+0 records out
377487360 bytes (377 MB, 360 MiB) copied, 2.02512 s, 186 MB/s
real 0m6.384s
user 0m0.002s
sys 0m5.823s
--- fs/xfs/xfs_bmap_util.c | 5 +++-- fs/xfs/xfs_bmap_util.h | 4 ++-- fs/xfs/xfs_file.c | 23 ++++++++++++++++++----- 3 files changed, 23 insertions(+), 9 deletions(-)
fs/xfs/xfs_bmap_util.c +3 -2
@@ -645,6 +645,7 @@ xfs_free_eofblocks(int xfs_alloc_file_space (struct xfs_inode *ip, +uint32_t flags,/* XFS_BMAPI_... */ xfs_off_t offset,xfs_off_t len) {
@@ -748,8 +749,8 @@ xfs_alloc_file_space(* will eventually reach the requested range. */ error =xfs_bmapi_write (tp, ip, startoffset_fsb, - allocatesize_fsb,XFS_BMAPI_PREALLOC ,0 , imapp, - &nimaps); + allocatesize_fsb, flags,0 , imapp, &nimaps); +if (error) {if (error != -ENOSR ) goto error;
fs/xfs/xfs_bmap_util.h +2 -2
@@ -55,8 +55,8 @@ int xfs_bmap_last_extent(struct xfs_trans *tp, struct xfs_inode *ip, int *is_empty);/* preallocation and hole punch interface */ -int xfs_alloc_file_space (struct xfs_inode *ip,xfs_off_t offset, -xfs_off_t len); +int xfs_alloc_file_space (struct xfs_inode *ip,uint32_t flags, +xfs_off_t offset,xfs_off_t len);int xfs_free_file_space (struct xfs_inode *ip,xfs_off_t offset,xfs_off_t len,struct xfs_zone_alloc_ctx *ac); int xfs_collapse_file_space(struct xfs_inode *, xfs_off_t offset,
fs/xfs/xfs_file.c +18 -5
@@ -1255,7 +1255,7 @@ xfs_falloc_insert_range(static int xfs_falloc_zero_range (struct file *file, -int mode, +int mode,loff_t offset,loff_t len,struct xfs_zone_alloc_ctx *ac)
@@ -1277,7 +1277,16 @@ xfs_falloc_zero_range( len =round_up (offset + len, blksize) -round_down (offset, blksize); offset =round_down (offset, blksize); - error =xfs_alloc_file_space (XFS_I (inode), offset, len); +if (mode &FALLOC_FL_WRITE_ZEROES ) { +if (!bdev_write_zeroes_unmap_sectors (inode->i_sb->s_bdev)) +return -EOPNOTSUPP ; + error =xfs_alloc_file_space (XFS_I (inode),XFS_BMAPI_ZERO , + offset, len); + } +else + error =xfs_alloc_file_space (XFS_I (inode),XFS_BMAPI_PREALLOC , + offset, len); +if (error)return error;return xfs_falloc_setsize (file, new_size);
@@ -1302,7 +1311,8 @@ xfs_falloc_unshare_range(if (error)return error; - error =xfs_alloc_file_space (XFS_I (inode), offset, len); + error =xfs_alloc_file_space (XFS_I (inode),XFS_BMAPI_PREALLOC , + offset, len);if (error)return error;return xfs_falloc_setsize (file, new_size);
@@ -1330,7 +1340,9 @@ xfs_falloc_allocate_range(if (error)return error; - error =xfs_alloc_file_space (XFS_I (inode), offset, len); + error =xfs_alloc_file_space (XFS_I (inode),XFS_BMAPI_PREALLOC , + offset, len); +if (error)return error;return xfs_falloc_setsize (file, new_size);
@@ -1340,7 +1352,7 @@ xfs_falloc_allocate_range( (FALLOC_FL_ALLOCATE_RANGE |FALLOC_FL_KEEP_SIZE | \FALLOC_FL_PUNCH_HOLE |FALLOC_FL_COLLAPSE_RANGE | \FALLOC_FL_ZERO_RANGE |FALLOC_FL_INSERT_RANGE | \ - FALLOC_FL_UNSHARE_RANGE) + FALLOC_FL_UNSHARE_RANGE | FALLOC_FL_WRITE_ZEROES )STATIC long __xfs_file_fallocate(
@@ -1383,6 +1395,7 @@ __xfs_file_fallocate(case FALLOC_FL_INSERT_RANGE : error =xfs_falloc_insert_range (file, offset, len);break ; +case FALLOC_FL_WRITE_ZEROES :case FALLOC_FL_ZERO_RANGE : error =xfs_falloc_zero_range (file, mode, offset, len, ac);break ;
Signature · 1 line
2.51.0
fs/xfs/xfs_bmap_util.c xfs_free_eofblocks( · line 648
Quoted 4 lines · Lukas Herbolt
int
xfs_alloc_file_space(fs/xfs/xfs_bmap_util.c +1
-0struct xfs_inode *ip, + uint32_t flags, /* XFS_BMAPI_... */
This seems to mix tabs and spaces.
fs/xfs/xfs_file.c xfs_falloc_insert_range( · line 1258
Quoted 5 lines
static int
xfs_falloc_zero_range(fs/xfs/xfs_file.c +1
-1struct file *file, - int mode, + int mode,
More whitespace damage here.
Quoted 5 lines
fs/xfs/xfs_file.c +0
-0loff_t offset, loff_t len, struct xfs_zone_alloc_ctx *ac)@@ -1277,7 +1277,16 @@ xfs_falloc_zero_range(
fs/xfs/xfs_file.c xfs_falloc_zero_range( · line 1286
Quoted 10 lines
fs/xfs/xfs_file.c +7
-1len =round_up (offset + len, blksize) -round_down (offset, blksize); offset =round_down (offset, blksize); - error =xfs_alloc_file_space (XFS_I (inode), offset, len); +if (mode &FALLOC_FL_WRITE_ZEROES ) { +if (!bdev_write_zeroes_unmap_sectors (inode->i_sb->s_bdev)) +return -EOPNOTSUPP ; + error =xfs_alloc_file_space (XFS_I (inode),XFS_BMAPI_ZERO , + offset, len); + } +else
The closing brace goes onto the same line as the else, and we usually add the brace in all branches.
Otherwise this looks fine.
Quoted 21 lines · Christoph Hellwig
fs/xfs/xfs_bmap_util.c xfs_free_eofblocks( · line 648
Quoted 4 lines · Lukas Herbolt
int
xfs_alloc_file_space(fs/xfs/xfs_bmap_util.c +1
-0struct xfs_inode *ip, + uint32_t flags, /* XFS_BMAPI_... */This seems to mix tabs and spaces.
fs/xfs/xfs_file.c xfs_falloc_insert_range( · line 1258
Quoted 5 lines
static int
xfs_falloc_zero_range(fs/xfs/xfs_file.c +1
-1struct file *file, - int mode, + int mode,More whitespace damage here.
Quoted 5 lines
fs/xfs/xfs_file.c +0
-0loff_t offset, loff_t len, struct xfs_zone_alloc_ctx *ac)@@ -1277,7 +1277,16 @@ xfs_falloc_zero_range(
Quoted 5 lines
fs/xfs/xfs_file.c xfs_falloc_zero_range( · line 1281
Quoted 5 lines
fs/xfs/xfs_file.c +2
-1len =round_up (offset + len, blksize) -round_down (offset, blksize); offset =round_down (offset, blksize); - error =xfs_alloc_file_space (XFS_I (inode), offset, len); +if (mode &FALLOC_FL_WRITE_ZEROES ) { +if (!bdev_write_zeroes_unmap_sectors (inode->i_sb->s_bdev))
not correct ^^^^^^^^^^^^^^^^^^^
You need to find the block device associated with the file because XFS is a multi-device filesystem.
--D
Quoted 11 lines
fs/xfs/xfs_file.c xfs_falloc_zero_range( · line 1286
Quoted 5 lines
fs/xfs/xfs_file.c +5
-0+return -EOPNOTSUPP ; + error =xfs_alloc_file_space (XFS_I (inode),XFS_BMAPI_ZERO , + offset, len); + } +else The closing brace goes onto the same line as the else, and we usually add the brace in all branches.
Otherwise this looks fine.
Quoted 8 lines · Darrick J. Wong
Quoted 3 lines
fs/xfs/xfs_file.c xfs_falloc_zero_range( · line 1281
Quoted 3 lines
fs/xfs/xfs_file.c +2
-1- error =xfs_alloc_file_space (XFS_I (inode), offset, len); +if (mode &FALLOC_FL_WRITE_ZEROES ) { +if (!bdev_write_zeroes_unmap_sectors (inode->i_sb->s_bdev))not correct ^^^^^^^^^^^^^^^^^^^You need to find the block device associated with the file because XFS is a multi-device filesystem.
Indeed. xfs_inode_buftarg will do the work, but we'll need to ensure the RT bit doesn't get flipped, i.e. it needs to hold a lock between that check and allocation the blocks if there were none yet.
Quoted 11 lines · Christoph Hellwig
Quoted 8 lines · Darrick J. Wong
Quoted 3 lines
fs/xfs/xfs_file.c xfs_falloc_zero_range( · line 1281
Quoted 3 lines
fs/xfs/xfs_file.c +2
-1- error =xfs_alloc_file_space (XFS_I (inode), offset, len); +if (mode &FALLOC_FL_WRITE_ZEROES ) { +if (!bdev_write_zeroes_unmap_sectors (inode->i_sb->s_bdev))not correct ^^^^^^^^^^^^^^^^^^^You need to find the block device associated with the file because XFS is a multi-device filesystem.
Indeed.
xfs_inode_buftargwill do the work, but we'll need to ensure
Thanks for xfs_inode_buftarg pointer.
Quoted 2 lines
the RT bit doesn't get flipped, i.e. it needs to hold a lock between that check and allocation the blocks if there were none yet.
I am having bit of trouble with that. If I get it right we should hold the XFS_ILOCK_EXCL but this lock is then grabbed in the xfs_trans_alloc_inode.
So I would need to release before and there would be again a small window where the RT flag can be flipped.
Looking at the xfs_alloc_file_space, there is also check for the RT bit without lock, so this also need an attention.
rt = XFS_IS_REALTIME_INODE(ip);
extsz = xfs_get_extsz_hint(ip);Or the xfs_trans_alloc_inode would need to check if we are a;ready holding the lock Is there a way how to check the current thread is the owner of the xfs_ilock rw_sem?
Something like?
---
static inline bool is_current_writer(struct rw_semaphore *sem)
{
/* The rwsem_owner() helper can be used to get the owner */
return sem->owner == current;
}---
Quoted 28 lines · On Tue, Oct 14, 2025 at 10:10:51AM +0200, [email protected] wrote:
Quoted 11 lines · Christoph Hellwig
Quoted 8 lines · Darrick J. Wong
Quoted 3 lines
fs/xfs/xfs_file.c xfs_falloc_zero_range( · line 1281
Quoted 3 lines
fs/xfs/xfs_file.c +2
-1- error =xfs_alloc_file_space (XFS_I (inode), offset, len); +if (mode &FALLOC_FL_WRITE_ZEROES ) { +if (!bdev_write_zeroes_unmap_sectors (inode->i_sb->s_bdev))not correct ^^^^^^^^^^^^^^^^^^^You need to find the block device associated with the file because XFS is a multi-device filesystem.
Indeed.
xfs_inode_buftargwill do the work, but we'll need to ensureThanks for
xfs_inode_buftargpointer.Quoted 2 lines
the RT bit doesn't get flipped, i.e. it needs to hold a lock between that check and allocation the blocks if there were none yet.
I am having bit of trouble with that. If I get it right we should hold the
XFS_ILOCK_EXCLbut this lock is then grabbed in thexfs_trans_alloc_inode.So I would need to release before and there would be again a small window where the RT flag can be flipped.
Looking at the
xfs_alloc_file_space, there is also check for the RT bit without
lock, so this also need an attention.
Quoted 7 lines
rt = XFS_IS_REALTIME_INODE(ip); extsz = xfs_get_extsz_hint(ip);Or the
xfs_trans_alloc_inodewould need to check if we are a;ready holding the
lock Is there a way how to check the current thread is the owner of thexfs_ilockrw_sem?
Hm? The VFS takes inode_lock in vfs_fileattr_set prior to changing the rt status bit, and and __xfs_file_fallocate takes the same lock (aka the IOLOCK) before calling xfs_falloc_*.
IOWs, a call to xfs_inode_buftarg would see a stable realtime flag and return the correct results.
--D
Quoted 10 lines
Something like?
---
static inline boolis_current_writer(struct rw_semaphore *sem){ /* The rwsem_owner() helper can be used to get the owner */ return sem->owner == current; }---
Quoted 41 lines · Lukas Herbolt
Add support for
FALLOC_FL_WRITE_ZEROESif the underlying device enable the unmap write zeroes operation.Inspired by the Ext4 implementation of the
FALLOC_FL_WRITE_ZEROES. It can speed up some patterns on specific hardware.time ( ./fallocate -l 360M
/mnt/test.file; dd if=/dev/zero of=/mnt/test.file \
bs=1M count=360 conv=notrunc,nocreat oflag=direct,dsync)360+0 records in
360+0 records out
377487360 bytes (377 MB, 360 MiB) copied, 22.0027 s, 17.2 MB/sreal 0m22.114s
user 0m0.006s
sys 0m3.085stime (./fallocate -wl 360M
/mnt/test.file; dd if=/dev/zero of=/mnt/test.file \
bs=1M count=360 conv=notrunc,nocreat oflag=direct,dsync );
360+0 records in
360+0 records out
377487360 bytes (377 MB, 360 MiB) copied, 2.02512 s, 186 MB/sreal 0m6.384s
user 0m0.002s
sys 0m5.823sSigned-off-by Lukas Herbolt <[email protected]>--- fs/xfs/xfs_bmap_util.c | 5 +++-- fs/xfs/xfs_bmap_util.h | 4 ++-- fs/xfs/xfs_file.c | 23 ++++++++++++++++++----- 3 files changed, 23 insertions(+), 9 deletions(-)fs/xfs/xfs_bmap_util.c +0
-0
fs/xfs/xfs_bmap_util.c xfs_free_eofblocks( · line 648
Quoted 4 lines
fs/xfs/xfs_bmap_util.c +1
-0@@ -645,6 +645,7 @@ xfs_free_eofblocks( int xfs_alloc_file_space( struct xfs_inode *ip, + uint32_t flags, /* XFS_BMAPI_... */
Spacing issue
Quoted 43 lines
fs/xfs/xfs_bmap_util.c +2
-2xfs_off_t offset, xfs_off_t len) {@@ -748,8 +749,8 @@ xfs_alloc_file_space(* will eventually reach the requested range. */ error =xfs_bmapi_write (tp, ip, startoffset_fsb, - allocatesize_fsb,XFS_BMAPI_PREALLOC ,0 , imapp, - &nimaps); + allocatesize_fsb, flags,0 , imapp, &nimaps); +if (error) {if (error != -ENOSR ) goto error;fs/xfs/xfs_bmap_util.h +2
-2@@ -55,8 +55,8 @@ int xfs_bmap_last_extent(struct xfs_trans *tp, struct xfs_inode *ip, int *is_empty);/* preallocation and hole punch interface */ -int xfs_alloc_file_space (struct xfs_inode *ip,xfs_off_t offset, -xfs_off_t len); +int xfs_alloc_file_space (struct xfs_inode *ip,uint32_t flags, +xfs_off_t offset,xfs_off_t len);int xfs_free_file_space (struct xfs_inode *ip,xfs_off_t offset,xfs_off_t len,struct xfs_zone_alloc_ctx *ac); int xfs_collapse_file_space(struct xfs_inode *, xfs_off_t offset,fs/xfs/xfs_file.c +1
-1@@ -1255,7 +1255,7 @@ xfs_falloc_insert_range(static int xfs_falloc_zero_range (struct file *file, -int mode, +int mode,loff_t offset,loff_t len,struct xfs_zone_alloc_ctx *ac)
fs/xfs/xfs_file.c xfs_falloc_zero_range( · line 1282
Quoted 7 lines
fs/xfs/xfs_file.c +3
-1@@ -1277,7 +1277,16 @@ xfs_falloc_zero_range( len =round_up (offset + len, blksize) -round_down (offset, blksize); offset =round_down (offset, blksize); - error =xfs_alloc_file_space (XFS_I (inode), offset, len); +if (mode &FALLOC_FL_WRITE_ZEROES ) { +if (!bdev_write_zeroes_unmap_sectors (inode->i_sb->s_bdev)) +return -EOPNOTSUPP ;
indentation issue above.
You can use ./scripts/checkpatch.pl --max-line-length=80 <path to patch files>. This should catch most of the common styling issues. Yes, there are XFS specific styles which the above script will not be able to catch.
--NR
Quoted 48 lines
fs/xfs/xfs_file.c +14
-3+ error =xfs_alloc_file_space (XFS_I (inode),XFS_BMAPI_ZERO , + offset, len); + } +else + error =xfs_alloc_file_space (XFS_I (inode),XFS_BMAPI_PREALLOC , + offset, len); +if (error)return error;return xfs_falloc_setsize (file, new_size);@@ -1302,7 +1311,8 @@ xfs_falloc_unshare_range(if (error)return error; - error =xfs_alloc_file_space (XFS_I (inode), offset, len); + error =xfs_alloc_file_space (XFS_I (inode),XFS_BMAPI_PREALLOC , + offset, len);if (error)return error;return xfs_falloc_setsize (file, new_size);@@ -1330,7 +1340,9 @@ xfs_falloc_allocate_range(if (error)return error; - error =xfs_alloc_file_space (XFS_I (inode), offset, len); + error =xfs_alloc_file_space (XFS_I (inode),XFS_BMAPI_PREALLOC , + offset, len); +if (error)return error;return xfs_falloc_setsize (file, new_size);@@ -1340,7 +1352,7 @@ xfs_falloc_allocate_range( (FALLOC_FL_ALLOCATE_RANGE |FALLOC_FL_KEEP_SIZE | \FALLOC_FL_PUNCH_HOLE |FALLOC_FL_COLLAPSE_RANGE | \FALLOC_FL_ZERO_RANGE |FALLOC_FL_INSERT_RANGE | \ - FALLOC_FL_UNSHARE_RANGE) + FALLOC_FL_UNSHARE_RANGE | FALLOC_FL_WRITE_ZEROES )STATIC long __xfs_file_fallocate(@@ -1383,6 +1395,7 @@ __xfs_file_fallocate(case FALLOC_FL_INSERT_RANGE : error =xfs_falloc_insert_range (file, offset, len);break ; +case FALLOC_FL_WRITE_ZEROES :case FALLOC_FL_ZERO_RANGE : error =xfs_falloc_zero_range (file, mode, offset, len, ac);break ;