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

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