cdown / rpms / util-linux

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