Blame SOURCES/bz1942434-1-gfs2_jadd_Use_fallocate_to_preallocate_journals.patch

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