dcavalca / rpms / util-linux

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