Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

fs: introduce FALLOC_FL_WRITE_ZEROES to fallocate

With the development of flash-based storage devices, we can quickly
write zeros to SSDs using the WRITE_ZERO command if the devices do not
actually write physical zeroes to the media. Therefore, we can use this
command to quickly preallocate a real all-zero file with written
extents. This approach should be beneficial for subsequent pure
overwriting within this file, as it can save on block allocation and,
consequently, significant metadata changes, which should greatly improve
overwrite performance on certain filesystems.

Therefore, introduce a new operation FALLOC_FL_WRITE_ZEROES to
fallocate. This flag is used to convert a specified range of a file to
zeros by issuing a zeroing operation. Blocks should be allocated for the
regions that span holes in the file, and the entire range is converted
to written extents. If the underlying device supports the actual offload
write zeroes command, the process of zeroing out operation can be
accelerated. If it does not, we currently don't prevent the file system
from writing actual zeros to the device. This provides users with a new
method to quickly generate a zeroed file, users no longer need to write
zero data to create a file with written extents.

Users can determine whether a disk supports the unmap write zeroes
feature through querying this sysfs interface:

/sys/block/<disk>/queue/write_zeroes_unmap_max_hw_bytes

Users can also enable or disable the unmap write zeroes operation
through this sysfs interface:

/sys/block/<disk>/queue/write_zeroes_unmap_max_bytes

Finally, this flag cannot be specified in conjunction with the
FALLOC_FL_KEEP_SIZE since allocating written extents beyond file EOF is
not permitted. In addition, filesystems that always require out-of-place
writes should not support this flag since they still need to allocated
new blocks during subsequent overwrites.

Signed-off-by: Zhang Yi <yi.zhang@huawei.com>
Link: https://lore.kernel.org/20250619111806.3546162-7-yi.zhang@huaweicloud.com
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: "Martin K. Petersen" <martin.petersen@oracle.com>
Signed-off-by: Christian Brauner <brauner@kernel.org>

authored by

Zhang Yi and committed by
Christian Brauner
7bd43cc7 2c46eab8

+20 -1
+1
fs/open.c
··· 281 281 break; 282 282 case FALLOC_FL_COLLAPSE_RANGE: 283 283 case FALLOC_FL_INSERT_RANGE: 284 + case FALLOC_FL_WRITE_ZEROES: 284 285 if (mode & FALLOC_FL_KEEP_SIZE) 285 286 return -EOPNOTSUPP; 286 287 break;
+2 -1
include/linux/falloc.h
··· 36 36 FALLOC_FL_COLLAPSE_RANGE | \ 37 37 FALLOC_FL_ZERO_RANGE | \ 38 38 FALLOC_FL_INSERT_RANGE | \ 39 - FALLOC_FL_UNSHARE_RANGE) 39 + FALLOC_FL_UNSHARE_RANGE | \ 40 + FALLOC_FL_WRITE_ZEROES) 40 41 41 42 /* on ia32 l_start is on a 32-bit boundary */ 42 43 #if defined(CONFIG_X86_64)
+17
include/uapi/linux/falloc.h
··· 78 78 */ 79 79 #define FALLOC_FL_UNSHARE_RANGE 0x40 80 80 81 + /* 82 + * FALLOC_FL_WRITE_ZEROES zeroes a specified file range in such a way that 83 + * subsequent writes to that range do not require further changes to the file 84 + * mapping metadata. This flag is beneficial for subsequent pure overwriting 85 + * within this range, as it can save on block allocation and, consequently, 86 + * significant metadata changes. Therefore, filesystems that always require 87 + * out-of-place writes should not support this flag. 88 + * 89 + * Different filesystems may implement different limitations on the 90 + * granularity of the zeroing operation. Most will preferably be accelerated 91 + * by submitting write zeroes command if the backing storage supports, which 92 + * may not physically write zeros to the media. 93 + * 94 + * This flag cannot be specified in conjunction with the FALLOC_FL_KEEP_SIZE. 95 + */ 96 + #define FALLOC_FL_WRITE_ZEROES 0x80 97 + 81 98 #endif /* _UAPI_FALLOC_H_ */