Skip to content

Commit 63391a3

Browse files
zhangyi089kawasaki
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 operation through querying this sysfs interface: /sys/block/<disk>/queue/write_zeroes_unmap 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]> Reviewed-by: Christoph Hellwig <[email protected]>
1 parent 9bb38bc commit 63391a3

3 files changed

Lines changed: 21 additions & 1 deletion

File tree

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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,22 @@
7878
*/
7979
#define FALLOC_FL_UNSHARE_RANGE 0x40
8080

81+
/*
82+
* FALLOC_FL_WRITE_ZEROES is used to convert a specified range of a file to
83+
* zeros by issuing a zeroing operation. Blocks should be allocated for the
84+
* regions that span holes in the file, and the entire range is converted to
85+
* written extents. This flag is beneficial for subsequent pure overwriting
86+
* within this range, as it can save on block allocation and, consequently,
87+
* significant metadata changes. Therefore, filesystems that always require
88+
* out-of-place writes should not support this flag.
89+
*
90+
* Different filesystems may implement different limitations on the
91+
* granularity of the zeroing operation. Most will preferably be accelerated
92+
* by submitting write zeroes command if the backing storage supports, which
93+
* may not physically write zeros to the media.
94+
*
95+
* This flag cannot be specified in conjunction with the FALLOC_FL_KEEP_SIZE.
96+
*/
97+
#define FALLOC_FL_WRITE_ZEROES 0x80
98+
8199
#endif /* _UAPI_FALLOC_H_ */

0 commit comments

Comments
 (0)