Skip to content

Commit 7bd43cc

Browse files
zhangyi089brauner
authored andcommitted
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 <[email protected]> Link: https://lore.kernel.org/[email protected] Reviewed-by: Christoph Hellwig <[email protected]> Reviewed-by: "Martin K. Petersen" <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent 2c46eab commit 7bd43cc

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

fs/open.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ int vfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
281281
break;
282282
case FALLOC_FL_COLLAPSE_RANGE:
283283
case FALLOC_FL_INSERT_RANGE:
284+
case FALLOC_FL_WRITE_ZEROES:
284285
if (mode & FALLOC_FL_KEEP_SIZE)
285286
return -EOPNOTSUPP;
286287
break;

include/linux/falloc.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ struct space_resv {
3636
FALLOC_FL_COLLAPSE_RANGE | \
3737
FALLOC_FL_ZERO_RANGE | \
3838
FALLOC_FL_INSERT_RANGE | \
39-
FALLOC_FL_UNSHARE_RANGE)
39+
FALLOC_FL_UNSHARE_RANGE | \
40+
FALLOC_FL_WRITE_ZEROES)
4041

4142
/* on ia32 l_start is on a 32-bit boundary */
4243
#if defined(CONFIG_X86_64)

include/uapi/linux/falloc.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,21 @@
7878
*/
7979
#define FALLOC_FL_UNSHARE_RANGE 0x40
8080

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+
8198
#endif /* _UAPI_FALLOC_H_ */

0 commit comments

Comments
 (0)