64664a
From e3cfe198ebff9721efe6dd063da4b7b2dfe1d8b9 Mon Sep 17 00:00:00 2001
64664a
From: Denis Chaplygin <dchaplyg@redhat.com>
64664a
Date: Tue, 31 Jan 2017 12:51:28 +0100
64664a
Subject: [PATCH 105/116] fallocate: Added posix_fallocate() support.
64664a
64664a
No all filesystems support Linux fallocate. The new option allow use
64664a
posix implementation if necessary.
64664a
64664a
Upstream: https://github.com/karelzak/util-linux/commit/833f9a7aae713278eec5f85266597482f18c7370
64664a
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1416467
64664a
Signed-off-by: Karel Zak <kzak@redhat.com>
64664a
---
64664a
 AUTHORS               |  1 +
64664a
 configure.ac          | 28 ++++++++++++++++++++++++++++
64664a
 sys-utils/fallocate.1 | 10 ++++++++++
64664a
 sys-utils/fallocate.c | 44 ++++++++++++++++++++++++++++++++++++++++++--
64664a
 4 files changed, 81 insertions(+), 2 deletions(-)
64664a
64664a
diff --git a/AUTHORS b/AUTHORS
64664a
index cd40985..3e02181 100644
64664a
--- a/AUTHORS
64664a
+++ b/AUTHORS
64664a
@@ -148,6 +148,7 @@ CONTRIBUTORS:
64664a
       David Miller <davem@davemloft.net>
64664a
       David Prévot <david@tilapin.org>
64664a
       David Woodhouse <dwmw2@infradead.org>
64664a
+      Denis Chaplygin <dchaplyg@redhat.com>
64664a
       Denis ChengRq <crquan@gmail.com>
64664a
       Dennis Gilmore <dennis@ausil.us>
64664a
       Dennis Jensen <dennis.h.jensen@siemens.com>
64664a
diff --git a/configure.ac b/configure.ac
64664a
index 266ef08..f87a885 100644
64664a
--- a/configure.ac
64664a
+++ b/configure.ac
64664a
@@ -917,6 +917,34 @@ if test "x$build_fallocate" = xyes; then
64664a
   AC_MSG_RESULT([no])])
64664a
 fi
64664a
 
64664a
+AS_IF([test "x$build_fallocate" = xyes], [
64664a
+  dnl check for valid posix_fallocate() function
64664a
+  AC_MSG_CHECKING([for valid posix_fallocate() function])
64664a
+  AC_LINK_IFELSE([AC_LANG_PROGRAM([[
64664a
+#ifdef HAVE_UNISTD_H
64664a
+# include <unistd.h>
64664a
+#endif
64664a
+#ifdef HAVE_SYS_TYPES_H
64664a
+# include <sys/types.h>
64664a
+#endif
64664a
+#ifdef HAVE_LINUX_FALLOC_H
64664a
+# include <linux/falloc.h>
64664a
+#endif
64664a
+#ifdef HAVE_FCNTL_H
64664a
+# include <fcntl.h>
64664a
+#endif
64664a
+]],[[
64664a
+   long ret;
64664a
+   ret = posix_fallocate(0, 0xfffffffful, 0xfffffffful);
64664a
+   if (ret != 0) {
64664a
+      return 1;
64664a
+   }
64664a
+   ]])],[
64664a
+  AC_MSG_RESULT([yes])
64664a
+  AC_DEFINE([HAVE_POSIX_FALLOCATE], [1], [Have valid posix_fallocate() function])],[
64664a
+  AC_MSG_RESULT([no])])
64664a
+])
64664a
+
64664a
 
64664a
 AC_ARG_ENABLE([unshare],
64664a
   AS_HELP_STRING([--disable-unshare], [do not build unshare]),
64664a
diff --git a/sys-utils/fallocate.1 b/sys-utils/fallocate.1
64664a
index 49d26e4..3763530 100644
64664a
--- a/sys-utils/fallocate.1
64664a
+++ b/sys-utils/fallocate.1
64664a
@@ -11,6 +11,13 @@ fallocate \- preallocate space to a file
64664a
 .B \-l
64664a
 .IR length
64664a
 .I filename
64664a
+.PP
64664a
+.B fallocate \-x
64664a
+.RB [ \-o
64664a
+.IR offset ]
64664a
+.RB \-l
64664a
+.IR length
64664a
+.I filename
64664a
 .SH DESCRIPTION
64664a
 .B fallocate
64664a
 is used to preallocate blocks to a file.  For filesystems which support the
64664a
@@ -39,6 +46,9 @@ Punch holes in the file, the range should not exceed the length of the file.
64664a
 Specifies the beginning offset of the allocation, in bytes.
64664a
 .IP "\fB\-l, \-\-length\fP \fIlength\fP
64664a
 Specifies the length of the allocation, in bytes.
64664a
+.IP "\fB\-x , \-\-posix\fP
64664a
+Enable POSIX operation mode. In that mode allocation operation always completes,
64664a
+but it may take longer time when fast allocation is not supported by the underlying filesystem.
64664a
 .IP "\fB\-h, \-\-help\fP"
64664a
 Print help and exit.
64664a
 .IP "\fB-V, \-\-version"
64664a
diff --git a/sys-utils/fallocate.c b/sys-utils/fallocate.c
64664a
index ff0f9e6..17ae5fe 100644
64664a
--- a/sys-utils/fallocate.c
64664a
+++ b/sys-utils/fallocate.c
64664a
@@ -52,6 +52,7 @@
64664a
 #include "strutils.h"
64664a
 #include "c.h"
64664a
 #include "closestream.h"
64664a
+#include "optutils.h"
64664a
 
64664a
 static void __attribute__((__noreturn__)) usage(FILE *out)
64664a
 {
64664a
@@ -63,6 +64,9 @@ static void __attribute__((__noreturn__)) usage(FILE *out)
64664a
 		" -p, --punch-hole    punch holes in the file\n"
64664a
 		" -o, --offset <num>  offset of the allocation, in bytes\n"
64664a
 		" -l, --length <num>  length of the allocation, in bytes\n"), out);
64664a
+#ifdef HAVE_POSIX_FALLOCATE
64664a
+	fputs(_(" -x, --posix         use posix_fallocate(3) instead of fallocate(2)\n"), out);
64664a
+#endif
64664a
 	fputs(USAGE_SEPARATOR, out);
64664a
 	fputs(USAGE_HELP, out);
64664a
 	fputs(USAGE_VERSION, out);
64664a
@@ -71,6 +75,18 @@ static void __attribute__((__noreturn__)) usage(FILE *out)
64664a
 	exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
64664a
 }
64664a
 
64664a
+
64664a
+#ifdef HAVE_POSIX_FALLOCATE
64664a
+static void xposix_fallocate(int fd, off_t offset, off_t length)
64664a
+{
64664a
+	int error = posix_fallocate(fd, offset, length);
64664a
+	if (error < 0) {
64664a
+		err(EXIT_FAILURE, _("fallocate failed"));
64664a
+	}
64664a
+}
64664a
+#endif
64664a
+
64664a
+
64664a
 static loff_t cvtnum(char *s)
64664a
 {
64664a
 	uintmax_t x;
64664a
@@ -85,9 +101,10 @@ int main(int argc, char **argv)
64664a
 {
64664a
 	char	*fname;
64664a
 	int	c;
64664a
-	int	error;
64664a
+	int	error = 0;
64664a
 	int	fd;
64664a
 	int	mode = 0;
64664a
+	int	posix = 0;
64664a
 	loff_t	length = -2LL;
64664a
 	loff_t	offset = 0;
64664a
 
64664a
@@ -98,15 +115,25 @@ int main(int argc, char **argv)
64664a
 	    { "punch-hole", 0, 0, 'p' },
64664a
 	    { "offset",    1, 0, 'o' },
64664a
 	    { "length",    1, 0, 'l' },
64664a
+	    { "posix",     0, 0, 'x' },
64664a
 	    { NULL,        0, 0, 0 }
64664a
 	};
64664a
 
64664a
+	static const ul_excl_t excl[] = {       /* rows and cols in ASCII order */
64664a
+		{ 'x', 'n', 'p' },
64664a
+		{ 0 }
64664a
+	};
64664a
+	int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
64664a
+
64664a
 	setlocale(LC_ALL, "");
64664a
 	bindtextdomain(PACKAGE, LOCALEDIR);
64664a
 	textdomain(PACKAGE);
64664a
 	atexit(close_stdout);
64664a
 
64664a
-	while ((c = getopt_long(argc, argv, "hVnpl:o:", longopts, NULL)) != -1) {
64664a
+	while ((c = getopt_long(argc, argv, "hVnpl:o:x", longopts, NULL)) != -1) {
64664a
+
64664a
+		err_exclusive_options(c, longopts, excl, excl_st);
64664a
+
64664a
 		switch(c) {
64664a
 		case 'h':
64664a
 			usage(stdout);
64664a
@@ -126,6 +153,13 @@ int main(int argc, char **argv)
64664a
 		case 'o':
64664a
 			offset = cvtnum(optarg);
64664a
 			break;
64664a
+		case 'x':
64664a
+#ifdef HAVE_POSIX_FALLOCATE
64664a
+			posix = 1;
64664a
+			break;
64664a
+#else
64664a
+			errx(EXIT_FAILURE, _("posix_fallocate support is not compiled"))
64664a
+#endif
64664a
 		default:
64664a
 			usage(stderr);
64664a
 			break;
64664a
@@ -152,6 +186,12 @@ int main(int argc, char **argv)
64664a
 	if (fd < 0)
64664a
 		err(EXIT_FAILURE, _("cannot open %s"), fname);
64664a
 
64664a
+#ifdef HAVE_POSIX_FALLOCATE
64664a
+	if (posix)
64664a
+		xposix_fallocate(fd, offset, length);
64664a
+	else
64664a
+#endif
64664a
+
64664a
 #ifdef HAVE_FALLOCATE
64664a
 	error = fallocate(fd, mode, offset, length);
64664a
 #else
64664a
-- 
64664a
2.9.3
64664a