]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
xfs_io: add FALLOC_FL_WRITE_ZEROES support
authorZhang Yi <yi.zhang@huawei.com>
Wed, 13 Aug 2025 02:42:50 +0000 (10:42 +0800)
committerAndrey Albershteyn <aalbersh@kernel.org>
Mon, 8 Sep 2025 11:31:55 +0000 (13:31 +0200)
The Linux kernel (since version 6.17) supports FALLOC_FL_WRITE_ZEROES in
fallocate(2). Add support for FALLOC_FL_WRITE_ZEROES support to the
fallocate utility by introducing a new 'fwzero' command in the xfs_io
tool.

Link: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=278c7d9b5e0c
Signed-off-by: Zhang Yi <yi.zhang@huawei.com>
Reviewed-by: Darrick J. Wong <djwong@kernel.org>
io/prealloc.c
man/man8/xfs_io.8

index 8e968c9f2455d542500da7117aca514297df13c7..9a64bf53a08c881b85feefc64eede1311e024029 100644 (file)
 #define FALLOC_FL_UNSHARE_RANGE 0x40
 #endif
 
+#ifndef FALLOC_FL_WRITE_ZEROES
+#define FALLOC_FL_WRITE_ZEROES 0x80
+#endif
+
 static cmdinfo_t allocsp_cmd;
 static cmdinfo_t freesp_cmd;
 static cmdinfo_t resvsp_cmd;
@@ -41,6 +45,7 @@ static cmdinfo_t fcollapse_cmd;
 static cmdinfo_t finsert_cmd;
 static cmdinfo_t fzero_cmd;
 static cmdinfo_t funshare_cmd;
+static cmdinfo_t fwzero_cmd;
 
 static int
 offset_length(
@@ -377,6 +382,27 @@ funshare_f(
        return 0;
 }
 
+static int
+fwzero_f(
+       int             argc,
+       char            **argv)
+{
+       xfs_flock64_t   segment;
+       int             mode = FALLOC_FL_WRITE_ZEROES;
+
+       if (!offset_length(argv[1], argv[2], &segment)) {
+               exitcode = 1;
+               return 0;
+       }
+
+       if (fallocate(file->fd, mode, segment.l_start, segment.l_len)) {
+               perror("fallocate");
+               exitcode = 1;
+               return 0;
+       }
+       return 0;
+}
+
 void
 prealloc_init(void)
 {
@@ -489,4 +515,14 @@ prealloc_init(void)
        funshare_cmd.oneline =
        _("unshares shared blocks within the range");
        add_command(&funshare_cmd);
+
+       fwzero_cmd.name = "fwzero";
+       fwzero_cmd.cfunc = fwzero_f;
+       fwzero_cmd.argmin = 2;
+       fwzero_cmd.argmax = 2;
+       fwzero_cmd.flags = CMD_NOMAP_OK | CMD_FOREIGN_OK;
+       fwzero_cmd.args = _("off len");
+       fwzero_cmd.oneline =
+       _("zeroes space and eliminates holes by allocating and submitting write zeroes");
+       add_command(&fwzero_cmd);
 }
index b0dcfdb72c3ebc3eeb65a9d2a79115226189b85e..0a673322fde3a1b1440b0c645e2fe3277bfaf1a2 100644 (file)
@@ -550,6 +550,12 @@ With the
 .B -k
 option, use the FALLOC_FL_KEEP_SIZE flag as well.
 .TP
+.BI fwzero " offset length"
+Call fallocate with FALLOC_FL_WRITE_ZEROES flag as described in the
+.BR fallocate (2)
+manual page to allocate and zero blocks within the range by submitting write
+zeroes.
+.TP
 .BI zero " offset length"
 Call xfsctl with
 .B XFS_IOC_ZERO_RANGE