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

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