Blame SOURCES/bz1942434-1-gfs2_jadd_Use_fallocate_to_preallocate_journals.patch

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