Blame SOURCES/xfsprogs-5.5.0-libxfs-use-FALLOC_FL_ZERO_RANGE-in-libxfs_device_zer.patch

212ea0
From 9d6023a856a1c4f84415dff59b0d5459cc8768db Mon Sep 17 00:00:00 2001
212ea0
From: Eric Sandeen <sandeen@redhat.com>
212ea0
Date: Thu, 27 Feb 2020 15:05:48 -0500
212ea0
Subject: [PATCH] libxfs: use FALLOC_FL_ZERO_RANGE in libxfs_device_zero
212ea0
212ea0
I had a request from someone who cared about mkfs speed over
212ea0
a slower network block device to look into using faster zeroing
212ea0
methods, particularly for the log, during mkfs.
212ea0
212ea0
Using FALLOC_FL_ZERO_RANGE is faster in this case than writing
212ea0
a bunch of zeros across a wire.
212ea0
212ea0
Signed-off-by: Eric Sandeen <sandeen@redhat.com>
212ea0
Reviewed-by: Christoph Hellwig <hch@lst.de>
212ea0
Signed-off-by: Eric Sandeen <sandeen@sandeen.net>
212ea0
---
212ea0
 include/builddefs.in |  3 +++
212ea0
 include/linux.h      | 22 ++++++++++++++++++++++
212ea0
 libxfs/rdwr.c        | 15 +++++++++++----
212ea0
 3 files changed, 36 insertions(+), 4 deletions(-)
212ea0
212ea0
diff --git a/include/builddefs.in b/include/builddefs.in
212ea0
index 4700b52..1dd27f7 100644
212ea0
--- a/include/builddefs.in
212ea0
+++ b/include/builddefs.in
212ea0
@@ -144,6 +144,9 @@ endif
212ea0
 ifeq ($(HAVE_GETFSMAP),yes)
212ea0
 PCFLAGS+= -DHAVE_GETFSMAP
212ea0
 endif
212ea0
+ifeq ($(HAVE_FALLOCATE),yes)
212ea0
+PCFLAGS += -DHAVE_FALLOCATE
212ea0
+endif
212ea0
 
212ea0
 LIBICU_LIBS = @libicu_LIBS@
212ea0
 LIBICU_CFLAGS = @libicu_CFLAGS@
212ea0
diff --git a/include/linux.h b/include/linux.h
212ea0
index 8f3c32b..57726bb 100644
212ea0
--- a/include/linux.h
212ea0
+++ b/include/linux.h
212ea0
@@ -20,6 +20,10 @@
212ea0
 #include <stdio.h>
212ea0
 #include <asm/types.h>
212ea0
 #include <mntent.h>
212ea0
+#include <fcntl.h>
212ea0
+#if defined(HAVE_FALLOCATE)
212ea0
+#include <linux/falloc.h>
212ea0
+#endif
212ea0
 #ifdef OVERRIDE_SYSTEM_FSXATTR
212ea0
 # define fsxattr sys_fsxattr
212ea0
 #endif
212ea0
@@ -164,6 +168,24 @@ static inline void platform_mntent_close(struct mntent_cursor * cursor)
212ea0
 	endmntent(cursor->mtabp);
212ea0
 }
212ea0
 
212ea0
+#if defined(FALLOC_FL_ZERO_RANGE)
212ea0
+static inline int
212ea0
+platform_zero_range(
212ea0
+	int		fd,
212ea0
+	xfs_off_t	start,
212ea0
+	size_t		len)
212ea0
+{
212ea0
+	int ret;
212ea0
+
212ea0
+	ret = fallocate(fd, FALLOC_FL_ZERO_RANGE, start, len);
212ea0
+	if (!ret)
212ea0
+		return 0;
212ea0
+	return -errno;
212ea0
+}
212ea0
+#else
212ea0
+#define platform_zero_range(fd, s, l)	(-EOPNOTSUPP)
212ea0
+#endif
212ea0
+
212ea0
 /*
212ea0
  * Check whether we have to define FS_IOC_FS[GS]ETXATTR ourselves. These
212ea0
  * are a copy of the definitions moved to linux/uapi/fs.h in the 4.5 kernel,
212ea0
diff --git a/libxfs/rdwr.c b/libxfs/rdwr.c
212ea0
index 0d9d720..e2d9d79 100644
212ea0
--- a/libxfs/rdwr.c
212ea0
+++ b/libxfs/rdwr.c
212ea0
@@ -61,8 +61,18 @@ libxfs_device_zero(struct xfs_buftarg *btp, xfs_daddr_t start, uint len)
212ea0
 {
212ea0
 	xfs_off_t	start_offset, end_offset, offset;
212ea0
 	ssize_t		zsize, bytes;
212ea0
+	size_t		len_bytes;
212ea0
 	char		*z;
212ea0
-	int		fd;
212ea0
+	int		error, fd;
212ea0
+
212ea0
+	fd = libxfs_device_to_fd(btp->dev);
212ea0
+	start_offset = LIBXFS_BBTOOFF64(start);
212ea0
+
212ea0
+	/* try to use special zeroing methods, fall back to writes if needed */
212ea0
+	len_bytes = LIBXFS_BBTOOFF64(len);
212ea0
+	error = platform_zero_range(fd, start_offset, len_bytes);
212ea0
+	if (!error)
212ea0
+		return 0;
212ea0
 
212ea0
 	zsize = min(BDSTRAT_SIZE, BBTOB(len));
212ea0
 	if ((z = memalign(libxfs_device_alignment(), zsize)) == NULL) {
212ea0
@@ -73,9 +83,6 @@ libxfs_device_zero(struct xfs_buftarg *btp, xfs_daddr_t start, uint len)
212ea0
 	}
212ea0
 	memset(z, 0, zsize);
212ea0
 
212ea0
-	fd = libxfs_device_to_fd(btp->dev);
212ea0
-	start_offset = LIBXFS_BBTOOFF64(start);
212ea0
-
212ea0
 	if ((lseek(fd, start_offset, SEEK_SET)) < 0) {
212ea0
 		fprintf(stderr, _("%s: %s seek to offset %llu failed: %s\n"),
212ea0
 			progname, __FUNCTION__,
212ea0
-- 
212ea0
2.9.5
212ea0