Blame SOURCES/bz1833141-1-gfs2_jadd_Handle_out_of_space_issues.patch

9757e7
commit d033351753e1c45e6be74342d05de0a2501d5211
9757e7
Author: Abhi Das <adas@redhat.com>
9757e7
Date:   Mon May 11 09:22:31 2020 -0500
9757e7
9757e7
    gfs2_jadd: Handle out-of-space issues
9757e7
    
9757e7
    If gfs2_jadd runs out of disk space while adding journals, it does
9757e7
    not exit gracefully. It partially does its job and bails out when
9757e7
    it hits -ENOSPC. This leaves the metafs mounted and most likely a
9757e7
    corrupted filesystem that even fsck.gfs2 can't fix.
9757e7
    
9757e7
    This patch adds a pre-check that ensures that the journals requested
9757e7
    will fit in the available space before proceeding. Note that this is
9757e7
    not foolproof because gfs2_jadd operates on a mounted filesystem.
9757e7
    While it is required that the filesystem be idle (and mounted on only
9757e7
    one node) while gfs2_jadd is being run, there is nothing stopping a
9757e7
    user from having some I/O process competing with gfs2_jadd for disk
9757e7
    blocks and consequently crashing it.
9757e7
    
9757e7
    Resolves: rhbz#1833141
9757e7
    
9757e7
    Signed-off-by: Abhi Das <adas@redhat.com>
9757e7
9757e7
diff --git a/gfs2/mkfs/main_jadd.c b/gfs2/mkfs/main_jadd.c
9757e7
index efe91e30..c5424803 100644
9757e7
--- a/gfs2/mkfs/main_jadd.c
9757e7
+++ b/gfs2/mkfs/main_jadd.c
9757e7
@@ -396,6 +396,8 @@ static void gather_info(struct gfs2_sbd *sdp, struct jadd_opts *opts)
9757e7
 		exit(EXIT_FAILURE);
9757e7
 	}
9757e7
 	sdp->bsize = statbuf.f_bsize;
9757e7
+	sdp->blks_total = statbuf.f_blocks;
9757e7
+	sdp->blks_alloced = sdp->blks_total - statbuf.f_bfree;
9757e7
 }
9757e7
 
9757e7
 static void find_current_journals(struct jadd_opts *opts)
9757e7
@@ -527,13 +529,43 @@ static void add_j(struct gfs2_sbd *sdp, struct jadd_opts *opts)
9757e7
 	}
9757e7
 }
9757e7
 
9757e7
+static int check_fit(struct gfs2_sbd *sdp, struct jadd_opts *opts)
9757e7
+{
9757e7
+	/* Compute how much space we'll need for the new journals
9757e7
+	 * Number of blocks needed per added journal:
9757e7
+	 * 1 block for the ir inode
9757e7
+	 * 1 block for the sc inode
9757e7
+	 * for sizes of the qc and journal inodes, use lgfs2_space_for_data()
9757e7
+	 * to calculate.
9757e7
+	 */
9757e7
+	uint64_t blks_per_j, total_blks;
9757e7
+
9757e7
+	blks_per_j = 1 + 1 +
9757e7
+		lgfs2_space_for_data(sdp, sdp->bsize, sdp->qcsize << 20) +
9757e7
+		lgfs2_space_for_data(sdp, sdp->bsize, sdp->jsize << 20);
9757e7
+	total_blks = opts->journals * blks_per_j;
9757e7
+
9757e7
+	if (total_blks > (sdp->blks_total - sdp->blks_alloced)) {
9757e7
+		printf( _("\nInsufficient space on the device to add %u %uMB "
9757e7
+			  "journals (%uMB QC size)\n\n"),
9757e7
+			opts->journals, sdp->jsize, sdp->qcsize);
9757e7
+		printf( _("Required space  : %*lu blks (%lu blks per "
9757e7
+			  "journal)\n"), 10, total_blks, blks_per_j);
9757e7
+		printf( _("Available space : %*lu blks\n\n"), 10,
9757e7
+			sdp->blks_total - sdp->blks_alloced);
9757e7
+		errno = ENOSPC;
9757e7
+		return 1;
9757e7
+	}
9757e7
+	return 0;
9757e7
+}
9757e7
+
9757e7
 int main(int argc, char *argv[])
9757e7
 {
9757e7
 	struct jadd_opts opts = {0};
9757e7
 	struct gfs2_sbd sbd, *sdp = &sbd;
9757e7
 	struct metafs mfs = {0};
9757e7
 	struct mntent *mnt;
9757e7
-	unsigned int total;
9757e7
+	unsigned int total, ret = 0;
9757e7
 
9757e7
 	setlocale(LC_ALL, "");
9757e7
 	textdomain("gfs2-utils");
9757e7
@@ -574,6 +606,12 @@ int main(int argc, char *argv[])
9757e7
 	}
9757e7
 	find_current_journals(&opts);
9757e7
 
9757e7
+	ret = check_fit(sdp, &opts);
9757e7
+	if (ret) {
9757e7
+		perror(_("Failed to add journals"));
9757e7
+		goto out;
9757e7
+	}
9757e7
+
9757e7
 	total = opts.orig_journals + opts.journals;
9757e7
 	for (opts.journals = opts.orig_journals;
9757e7
 	     opts.journals < total;
9757e7
@@ -588,13 +626,16 @@ int main(int argc, char *argv[])
9757e7
 		add_j(sdp, &opts);
9757e7
 	}
9757e7
 
9757e7
+out:
9757e7
 	free(opts.new_inode);
9757e7
 	free(opts.per_node);
9757e7
 	free(opts.jindex);
9757e7
 	close(sdp->path_fd);
9757e7
 	cleanup_metafs(&mfs;;
9757e7
 	sync();
9757e7
-	print_results(&opts);
9757e7
 
9757e7
-	return 0;
9757e7
+	if (!ret)
9757e7
+		print_results(&opts);
9757e7
+
9757e7
+	return ret;
9757e7
 }