Blame SOURCES/bz1942434-1-gfs2_jadd_Use_fallocate_to_preallocate_journals.patch

957bb1
commit 13e09a3519cc7cbf9417acc86a6d046bdba71a9f
957bb1
Author: Andrew Price <anprice@redhat.com>
957bb1
Date:   Thu Mar 18 17:30:53 2021 +0000
957bb1
957bb1
    gfs2_jadd: Use fallocate to preallocate journals
957bb1
    
957bb1
    Fall back to writes for ancient kernels and use larger writes in that
957bb1
    case to reduce the chance of fragmentation.
957bb1
    
957bb1
    Resolves: rhbz#1942434
957bb1
    
957bb1
    Signed-off-by: Andrew Price <anprice@redhat.com>
957bb1
957bb1
diff --git a/gfs2/mkfs/main_jadd.c b/gfs2/mkfs/main_jadd.c
957bb1
index ea89c96b..b0cffbac 100644
957bb1
--- a/gfs2/mkfs/main_jadd.c
957bb1
+++ b/gfs2/mkfs/main_jadd.c
957bb1
@@ -474,6 +474,43 @@ static uint64_t find_block_address(int fd, off_t offset, unsigned bsize)
957bb1
 }
957bb1
 #endif
957bb1
 
957bb1
+static int alloc_new_journal(int fd, unsigned bytes)
957bb1
+{
957bb1
+#define ALLOC_BUF_SIZE (4 << 20)
957bb1
+	unsigned left = bytes;
957bb1
+	int error;
957bb1
+	char *buf;
957bb1
+
957bb1
+	error = fallocate(fd, 0, 0, bytes);
957bb1
+	if (error == 0)
957bb1
+	       return 0;
957bb1
+	if (errno != EOPNOTSUPP)
957bb1
+		goto out_errno;
957bb1
+
957bb1
+	/* No fallocate support, fall back to writes */
957bb1
+	buf = calloc(1, ALLOC_BUF_SIZE);
957bb1
+	if (buf == NULL)
957bb1
+		goto out_errno;
957bb1
+
957bb1
+	while (left > 0) {
957bb1
+		unsigned sz = ALLOC_BUF_SIZE;
957bb1
+
957bb1
+		if (left < ALLOC_BUF_SIZE)
957bb1
+			sz = left;
957bb1
+
957bb1
+		if (pwrite(fd, buf, sz, bytes - left) != sz) {
957bb1
+			free(buf);
957bb1
+			goto out_errno;
957bb1
+		}
957bb1
+		left -= sz;
957bb1
+	}
957bb1
+	free(buf);
957bb1
+	return 0;
957bb1
+out_errno:
957bb1
+	perror("Failed to allocate space for new journal");
957bb1
+	return -1;
957bb1
+}
957bb1
+
957bb1
 static int add_j(struct gfs2_sbd *sdp, struct jadd_opts *opts)
957bb1
 {
957bb1
 	int fd, error = 0;
957bb1
@@ -490,14 +527,9 @@ static int add_j(struct gfs2_sbd *sdp, struct jadd_opts *opts)
957bb1
 	if ((error = set_flags(fd, JA_FL_CLEAR, FS_JOURNAL_DATA_FL)))
957bb1
 		goto close_fd;
957bb1
 
957bb1
-	memset(buf, 0, sdp->bsize);
957bb1
-	for (x=0; x
957bb1
-		if (write(fd, buf, sdp->bsize) != sdp->bsize) {
957bb1
-			perror("add_j write");
957bb1
-			error = -1;
957bb1
-			goto close_fd;
957bb1
-		}
957bb1
-	}
957bb1
+	error = alloc_new_journal(fd, sdp->jsize << 20);
957bb1
+	if (error != 0)
957bb1
+		goto close_fd;
957bb1
 
957bb1
 	if ((error = lseek(fd, 0, SEEK_SET)) < 0) {
957bb1
 		perror("add_j lseek");