]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
xfs_io: add sync_file_range support
authorDave Chinner <david@fromorbit.com>
Wed, 25 Jul 2012 22:30:48 +0000 (22:30 +0000)
committerMark Tinguely <tinguely@eagdhcp-232-125.americas.sgi.com>
Mon, 24 Sep 2012 21:38:35 +0000 (16:38 -0500)
Add sync_file_range support to xfs_io to allow fine grained control
of data writeback and syncing on a given file.

Signed-off-by: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Mark Tinguely <tinguely@sgi.com>
Signed-off-by: Mark Tinguely <tinguely@sgi.com>
io/Makefile
io/init.c
io/io.h
io/sync_file_range.c [new file with mode: 0644]
man/man8/xfs_io.8

index 9d79dca5a264d25ef27c8b96ff31407e3aa15120..bf46d56f251d353ca2d150a0ec9999ee7738a6b6 100644 (file)
@@ -58,6 +58,11 @@ CFILES += inject.c resblks.c
 LCFLAGS += -DHAVE_INJECT -DHAVE_RESBLKS
 endif
 
+ifeq ($(PKG_PLATFORM),linux)
+CFILES += sync_file_range.c
+LCFLAGS += -DHAVE_SYNC_FILE_RANGE
+endif
+
 ifeq ($(ENABLE_READLINE),yes)
 LLDLIBS += $(LIBREADLINE) $(LIBTERMCAP)
 endif
index f416acf9bc77172114f082bcb5efb3d43657c368..fb930820631897f79462101ed824aaaae4272386 100644 (file)
--- a/io/init.c
+++ b/io/init.c
@@ -78,6 +78,7 @@ init_commands(void)
        sendfile_init();
        shutdown_init();
        truncate_init();
+       sync_range_init();
 }
 
 static int
diff --git a/io/io.h b/io/io.h
index 2923362cfcb4dfad5c39f2273adf48aca190f7e0..8151b7b249118692e576988dcf5e3c820be5a2a3 100644 (file)
--- a/io/io.h
+++ b/io/io.h
@@ -141,3 +141,9 @@ extern void         fiemap_init(void);
 #else
 #define fiemap_init()  do { } while (0)
 #endif
+
+#ifdef HAVE_SYNC_FILE_RANGE
+extern void            sync_range_init(void);
+#else
+#define sync_range_init()      do { } while (0)
+#endif
diff --git a/io/sync_file_range.c b/io/sync_file_range.c
new file mode 100644 (file)
index 0000000..35d8cc5
--- /dev/null
@@ -0,0 +1,107 @@
+/*
+ * Copyright (c) 2012 Red Hat, Inc.
+ * All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it would be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write the Free Software Foundation,
+ * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include <xfs/xfs.h>
+#include <xfs/command.h>
+#include <xfs/input.h>
+#include "init.h"
+#include "io.h"
+
+static cmdinfo_t sync_range_cmd;
+
+static void
+sync_range_help(void)
+{
+       printf(_(
+"\n"
+" Trigger specific writeback commands on a range of the current file\n"
+"\n"
+" With no options, the SYNC_FILE_RANGE_WRITE is implied.\n"
+" -a -- wait for IO to finish after writing (SYNC_FILE_RANGE_WAIT_AFTER).\n"
+" -b -- wait for IO to finish before writing (SYNC_FILE_RANGE_WAIT_BEFORE).\n"
+" -w -- write dirty data in range (SYNC_FILE_RANGE_WRITE).\n"
+"\n"));
+}
+
+static int
+sync_range_f(
+       int             argc,
+       char            **argv)
+{
+       off64_t         offset = 0, length = 0;
+       int             c, sync_mode = 0;
+       size_t          blocksize, sectsize;
+
+       while ((c = getopt(argc, argv, "abw")) != EOF) {
+               switch (c) {
+               case 'a':
+                       sync_mode = SYNC_FILE_RANGE_WAIT_AFTER;
+                       break;
+               case 'b':
+                       sync_mode = SYNC_FILE_RANGE_WAIT_BEFORE;
+                       break;
+               case 'w':
+                       sync_mode = SYNC_FILE_RANGE_WRITE;
+                       break;
+               default:
+                       return command_usage(&sync_range_cmd);
+               }
+       }
+
+       /* default to just starting writeback on the range */
+       if (!sync_mode)
+               sync_mode = SYNC_FILE_RANGE_WRITE;
+
+       if (optind != argc - 2)
+               return command_usage(&sync_range_cmd);
+       init_cvtnum(&blocksize, &sectsize);
+       offset = cvtnum(blocksize, sectsize, argv[optind]);
+       if (offset < 0) {
+               printf(_("non-numeric offset argument -- %s\n"),
+                       argv[optind]);
+               return 0;
+       }
+       optind++;
+       length = cvtnum(blocksize, sectsize, argv[optind]);
+       if (length < 0) {
+               printf(_("non-numeric length argument -- %s\n"),
+                       argv[optind]);
+               return 0;
+       }
+
+       if (sync_file_range(file->fd, offset, length, sync_mode) < 0) {
+               perror("sync_file_range");
+               return 0;
+       }
+       return 0;
+}
+
+void
+sync_range_init(void)
+{
+       sync_range_cmd.name = "sync_range";
+       sync_range_cmd.cfunc = sync_range_f;
+       sync_range_cmd.argmin = 2;
+       sync_range_cmd.argmax = -1;
+       sync_range_cmd.flags = CMD_NOMAP_OK | CMD_FOREIGN_OK;
+       sync_range_cmd.args = _("[-abw] off len");
+       sync_range_cmd.oneline = _("Control writeback on a range of a file");
+       sync_range_cmd.help = sync_range_help;
+
+       add_command(&sync_range_cmd);
+}
index ebbfdec838c2bd59a440af13b8d48a88060967e4..a185798e5b601eb5d2f6d5d54aadb872d7544111 100644 (file)
@@ -273,6 +273,25 @@ See the
 .B fsync
 command.
 .TP
+.BI "sync_range [ \-a | \-b | \-w ] offset length "
+On platforms which support it, allows control of syncing a range of the file to
+disk. With no options, SYNC_FILE_RANGE_WRITE is implied on the range supplied.
+.RS 1.0i
+.PD 0
+.TP 0.4i
+.B \-a
+wait for IO in the given range to finish after writing
+(SYNC_FILE_RANGE_WAIT_AFTER).
+.TP
+.B \-b
+wait for IO in the given range to finish before writing
+(SYNC_FILE_RANGE_WAIT_BEFORE).
+.TP
+.B \-w
+start writeback of dirty data in the given range (SYNC_FILE_RANGE_WRITE).
+.RE
+.PD
+.TP
 .BI resvsp " offset length"
 Allocates reserved, unwritten space for part of a file using the
 XFS_IOC_RESVSP system call described in the