Blame SOURCES/bz1833141-2-gfs2_jadd_error_handling_overhaul.patch

06adb5
commit 206b040657b5125c2f2efe35dddcb7463fb49788
06adb5
Author: Abhi Das <adas@redhat.com>
06adb5
Date:   Mon May 11 14:41:06 2020 -0500
06adb5
06adb5
    gfs2_jadd: error handling overhaul
06adb5
    
06adb5
    Handle error conditions better and fail gracefully.
06adb5
    
06adb5
    Resolves: rhbz#1833141
06adb5
    
06adb5
    Signed-off-by: Abhi Das <adas@redhat.com>
06adb5
06adb5
diff --git a/gfs2/mkfs/main_jadd.c b/gfs2/mkfs/main_jadd.c
06adb5
index c5424803..ea89c96b 100644
06adb5
--- a/gfs2/mkfs/main_jadd.c
06adb5
+++ b/gfs2/mkfs/main_jadd.c
06adb5
@@ -42,15 +42,13 @@ struct jadd_opts {
06adb5
 
06adb5
 #define JA_FL_SET   0
06adb5
 #define JA_FL_CLEAR 1
06adb5
-static void set_flags(int fd, int op, uint32_t flags)
06adb5
+static int set_flags(int fd, int op, uint32_t flags)
06adb5
 {
06adb5
-        int err;
06adb5
 	uint32_t val;
06adb5
 
06adb5
-        err = ioctl(fd, FS_IOC_GETFLAGS, &val;;
06adb5
-        if (err) {
06adb5
+        if (ioctl(fd, FS_IOC_GETFLAGS, &val)) {
06adb5
 		perror("GETFLAGS");
06adb5
-		exit(EXIT_FAILURE);
06adb5
+		return -1;
06adb5
 	}
06adb5
 
06adb5
         if (op == JA_FL_SET)
06adb5
@@ -58,11 +56,11 @@ static void set_flags(int fd, int op, uint32_t flags)
06adb5
         else if (op == JA_FL_CLEAR)
06adb5
                 val &= ~flags;
06adb5
 
06adb5
-        err = ioctl(fd, FS_IOC_SETFLAGS, &val;;
06adb5
-        if (err) {
06adb5
+        if (ioctl(fd, FS_IOC_SETFLAGS, &val)) {
06adb5
 		perror("SETFLAGS");
06adb5
-		exit(EXIT_FAILURE);
06adb5
+		return -1;
06adb5
 	}
06adb5
+	return 0;
06adb5
 }
06adb5
 
06adb5
 static int rename2system(struct jadd_opts *opts, const char *new_dir, const char *new_name)
06adb5
@@ -243,188 +241,214 @@ static void print_results(struct jadd_opts *opts)
06adb5
 static int create_new_inode(struct jadd_opts *opts, uint64_t *addr)
06adb5
 {
06adb5
 	char *name = opts->new_inode;
06adb5
-	int fd;
06adb5
-	int error;
06adb5
+	int fd, error = 0;
06adb5
 
06adb5
 	for (;;) {
06adb5
 		fd = open(name, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, 0600);
06adb5
 		if (fd >= 0)
06adb5
 			break;
06adb5
 		if (errno == EEXIST) {
06adb5
-			error = unlink(name);
06adb5
-			if (error){
06adb5
+			if (unlink(name)) {
06adb5
 				perror("unlink");
06adb5
-				exit(EXIT_FAILURE);
06adb5
+				return -1;
06adb5
 			}
06adb5
-		} else{
06adb5
-			perror("create");
06adb5
-			exit(EXIT_FAILURE);
06adb5
+			continue;
06adb5
 		}
06adb5
+		perror("create");
06adb5
+		return -1;
06adb5
 	}
06adb5
+
06adb5
 	if (addr != NULL) {
06adb5
 		struct stat st;
06adb5
 
06adb5
-		fstat(fd, &st);
06adb5
+		if ((error = fstat(fd, &st))) {
06adb5
+			perror("fstat");
06adb5
+			return close(fd);
06adb5
+		}
06adb5
 		*addr = st.st_ino;
06adb5
 	}
06adb5
 
06adb5
 	return fd;
06adb5
 }
06adb5
 
06adb5
-static void add_ir(struct jadd_opts *opts)
06adb5
+static int add_ir(struct jadd_opts *opts)
06adb5
 {
06adb5
-	int fd;
06adb5
+	int fd, error = 0;
06adb5
 	char new_name[256];
06adb5
-	int error;
06adb5
+	struct gfs2_inum_range ir;
06adb5
 
06adb5
-	fd = create_new_inode(opts, NULL);
06adb5
+	if ((fd = create_new_inode(opts, NULL)) < 0)
06adb5
+		return fd;
06adb5
 
06adb5
-	{
06adb5
-		struct gfs2_inum_range ir;
06adb5
+	if ((error = set_flags(fd, JA_FL_SET, FS_JOURNAL_DATA_FL)))
06adb5
+		goto close_fd;
06adb5
 
06adb5
-		set_flags(fd, JA_FL_SET, FS_JOURNAL_DATA_FL);
06adb5
-		memset(&ir, 0, sizeof(struct gfs2_inum_range));
06adb5
-		if (write(fd, (void*)&ir, sizeof(struct gfs2_inum_range)) !=
06adb5
-		    sizeof(struct gfs2_inum_range)) {
06adb5
-			perror("add_ir");
06adb5
-			exit(EXIT_FAILURE);
06adb5
-		}
06adb5
+	memset(&ir, 0, sizeof(struct gfs2_inum_range));
06adb5
+	if (write(fd, (void*)&ir, sizeof(struct gfs2_inum_range)) !=
06adb5
+	    sizeof(struct gfs2_inum_range)) {
06adb5
+		perror("add_ir write");
06adb5
+		error = -1;
06adb5
+		goto close_fd;
06adb5
+	}
06adb5
+
06adb5
+	if ((error = fsync(fd))) {
06adb5
+		perror("add_ir fsync");
06adb5
+		goto close_fd;
06adb5
 	}
06adb5
 
06adb5
-	close(fd);
06adb5
 
06adb5
 	sprintf(new_name, "inum_range%u", opts->journals);
06adb5
 	error = rename2system(opts, opts->per_node, new_name);
06adb5
-	if (error < 0 && errno != EEXIST){
06adb5
+	if (error < 0 && errno != EEXIST) {
06adb5
 		perror("add_ir rename2system");
06adb5
-		exit(EXIT_FAILURE);
06adb5
+		goto close_fd;
06adb5
 	}
06adb5
+close_fd:
06adb5
+	return close(fd) || error;
06adb5
 }
06adb5
 
06adb5
-static void add_sc(struct jadd_opts *opts)
06adb5
+static int add_sc(struct jadd_opts *opts)
06adb5
 {
06adb5
-	int fd;
06adb5
+	int fd, error = 0;
06adb5
 	char new_name[256];
06adb5
-	int error;
06adb5
+	struct gfs2_statfs_change sc;
06adb5
 
06adb5
-	fd = create_new_inode(opts, NULL);
06adb5
+	if ((fd = create_new_inode(opts, NULL)) < 0)
06adb5
+		return fd;
06adb5
 
06adb5
-	{
06adb5
-		struct gfs2_statfs_change sc;
06adb5
-		set_flags(fd, JA_FL_SET, FS_JOURNAL_DATA_FL);
06adb5
+	if ((error = set_flags(fd, JA_FL_SET, FS_JOURNAL_DATA_FL)))
06adb5
+		goto close_fd;
06adb5
 
06adb5
-		memset(&sc, 0, sizeof(struct gfs2_statfs_change));
06adb5
-		if (write(fd, (void*)&sc, sizeof(struct gfs2_statfs_change)) !=
06adb5
-		    sizeof(struct gfs2_statfs_change)) {
06adb5
-			perror("add_sc");
06adb5
-			exit(EXIT_FAILURE);
06adb5
-		}
06adb5
+	memset(&sc, 0, sizeof(struct gfs2_statfs_change));
06adb5
+	if (write(fd, (void*)&sc, sizeof(struct gfs2_statfs_change)) !=
06adb5
+	    sizeof(struct gfs2_statfs_change)) {
06adb5
+		perror("add_sc write");
06adb5
+		error = -1;
06adb5
+		goto close_fd;
06adb5
 	}
06adb5
 
06adb5
-	close(fd);
06adb5
+	if ((error = fsync(fd))) {
06adb5
+		perror("add_sc fsync");
06adb5
+		goto close_fd;
06adb5
+	}
06adb5
 
06adb5
 	sprintf(new_name, "statfs_change%u", opts->journals);
06adb5
 	error = rename2system(opts, opts->per_node, new_name);
06adb5
 	if (error < 0 && errno != EEXIST){
06adb5
 		perror("add_sc rename2system");
06adb5
-		exit(EXIT_FAILURE);
06adb5
+		goto close_fd;
06adb5
 	}
06adb5
+close_fd:
06adb5
+	return close(fd) || error;
06adb5
 }
06adb5
 
06adb5
-static void add_qc(struct gfs2_sbd *sdp, struct jadd_opts *opts)
06adb5
+static int add_qc(struct gfs2_sbd *sdp, struct jadd_opts *opts)
06adb5
 {
06adb5
-	int fd;
06adb5
-	char new_name[256];
06adb5
-	int error;
06adb5
-
06adb5
-	fd = create_new_inode(opts, NULL);
06adb5
-
06adb5
-	{
06adb5
-		char buf[sdp->bsize];
06adb5
-		unsigned int blocks =
06adb5
-			sdp->qcsize << (20 - sdp->sd_sb.sb_bsize_shift);
06adb5
-		unsigned int x;
06adb5
-		struct gfs2_meta_header mh;
06adb5
-
06adb5
-		set_flags(fd, JA_FL_CLEAR, FS_JOURNAL_DATA_FL);
06adb5
-		memset(buf, 0, sdp->bsize);
06adb5
-
06adb5
-		for (x=0; x
06adb5
-			if (write(fd, buf, sdp->bsize) != sdp->bsize) {
06adb5
-				perror("add_qc");
06adb5
-				exit(EXIT_FAILURE);
06adb5
-			}
06adb5
+	int fd, error = 0;
06adb5
+	char new_name[256], buf[sdp->bsize];
06adb5
+	unsigned int blocks =
06adb5
+		sdp->qcsize << (20 - sdp->sd_sb.sb_bsize_shift);
06adb5
+	unsigned int x;
06adb5
+	struct gfs2_meta_header mh;
06adb5
+
06adb5
+	if ((fd = create_new_inode(opts, NULL)) < 0)
06adb5
+		return fd;
06adb5
+
06adb5
+	if ((error = set_flags(fd, JA_FL_CLEAR, FS_JOURNAL_DATA_FL)))
06adb5
+		goto close_fd;
06adb5
+
06adb5
+	memset(buf, 0, sdp->bsize);
06adb5
+	for (x=0; x
06adb5
+		if (write(fd, buf, sdp->bsize) != sdp->bsize) {
06adb5
+			perror("add_qc write");
06adb5
+			error = -1;
06adb5
+			goto close_fd;
06adb5
 		}
06adb5
+	}
06adb5
 
06adb5
-		lseek(fd, 0, SEEK_SET);
06adb5
-
06adb5
-		memset(&mh, 0, sizeof(struct gfs2_meta_header));
06adb5
-		mh.mh_magic = GFS2_MAGIC;
06adb5
-		mh.mh_type = GFS2_METATYPE_QC;
06adb5
-		mh.mh_format = GFS2_FORMAT_QC;
06adb5
-		gfs2_meta_header_out(&mh, buf);
06adb5
+	if ((error = lseek(fd, 0, SEEK_SET)) < 0) {
06adb5
+		perror("add_qc lseek");
06adb5
+		goto close_fd;
06adb5
+	}
06adb5
 
06adb5
-		for (x=0; x
06adb5
-			if (write(fd, buf, sdp->bsize) != sdp->bsize) {
06adb5
-				perror("add_qc");
06adb5
-				exit(EXIT_FAILURE);
06adb5
-			}
06adb5
+	memset(&mh, 0, sizeof(struct gfs2_meta_header));
06adb5
+	mh.mh_magic = GFS2_MAGIC;
06adb5
+	mh.mh_type = GFS2_METATYPE_QC;
06adb5
+	mh.mh_format = GFS2_FORMAT_QC;
06adb5
+	gfs2_meta_header_out(&mh, buf);
06adb5
+
06adb5
+	for (x=0; x
06adb5
+		if (write(fd, buf, sdp->bsize) != sdp->bsize) {
06adb5
+			perror("add_qc write");
06adb5
+			error = 1;
06adb5
+			goto close_fd;
06adb5
 		}
06adb5
-
06adb5
-		error = fsync(fd);
06adb5
-		if (error){
06adb5
+		if ((error = fsync(fd))) {
06adb5
 			perror("add_qc fsync");
06adb5
-			exit(EXIT_FAILURE);
06adb5
+			goto close_fd;
06adb5
 		}
06adb5
 	}
06adb5
 
06adb5
-	close(fd);
06adb5
-
06adb5
 	sprintf(new_name, "quota_change%u", opts->journals);
06adb5
 	error = rename2system(opts, opts->per_node, new_name);
06adb5
 	if (error < 0 && errno != EEXIST){
06adb5
 		perror("add_qc rename2system");
06adb5
-		exit(EXIT_FAILURE);
06adb5
+		goto close_fd;
06adb5
 	}
06adb5
+close_fd:
06adb5
+	return close(fd) || error;
06adb5
 }
06adb5
 
06adb5
-static void gather_info(struct gfs2_sbd *sdp, struct jadd_opts *opts)
06adb5
+static int gather_info(struct gfs2_sbd *sdp, struct jadd_opts *opts)
06adb5
 {
06adb5
 	struct statfs statbuf;
06adb5
+
06adb5
 	if (statfs(opts->path, &statbuf) < 0) {
06adb5
 		perror(opts->path);
06adb5
-		exit(EXIT_FAILURE);
06adb5
+		return -1;
06adb5
 	}
06adb5
+
06adb5
 	sdp->bsize = statbuf.f_bsize;
06adb5
 	sdp->blks_total = statbuf.f_blocks;
06adb5
 	sdp->blks_alloced = sdp->blks_total - statbuf.f_bfree;
06adb5
+
06adb5
+	return 0;
06adb5
 }
06adb5
 
06adb5
-static void find_current_journals(struct jadd_opts *opts)
06adb5
+static int find_current_journals(struct jadd_opts *opts)
06adb5
 {
06adb5
 	struct dirent *dp;
06adb5
 	DIR *dirp;
06adb5
 	unsigned existing_journals = 0;
06adb5
+	int ret = 0;
06adb5
 
06adb5
 	dirp = opendir(opts->jindex);
06adb5
 	if (!dirp) {
06adb5
 		perror("jindex");
06adb5
-		exit(EXIT_FAILURE);
06adb5
+		ret = -1;
06adb5
+		goto out;
06adb5
 	}
06adb5
 	while (dirp) {
06adb5
 		if ((dp = readdir(dirp)) != NULL) {
06adb5
 			if (strncmp(dp->d_name, "journal", 7) == 0)
06adb5
 				existing_journals++;
06adb5
 		} else
06adb5
-			goto close;
06adb5
+			goto close_fd;
06adb5
 	}
06adb5
-close:
06adb5
-	closedir(dirp);
06adb5
+close_fd:
06adb5
+	if ((ret = closedir(dirp)))
06adb5
+		goto out;
06adb5
+
06adb5
 	if (existing_journals == 0) {
06adb5
-		die( _("No journals found. Did you run mkfs.gfs2 correctly?\n"));
06adb5
+		errno = EINVAL;
06adb5
+		perror("No journals found. Did you run mkfs.gfs2 correctly?\n");
06adb5
+		ret = -1;
06adb5
+		goto out;
06adb5
 	}
06adb5
 
06adb5
 	opts->orig_journals = existing_journals;
06adb5
+out:
06adb5
+	return ret;
06adb5
 }
06adb5
 
06adb5
 #ifdef GFS2_HAS_LH_V2
06adb5
@@ -450,83 +474,88 @@ static uint64_t find_block_address(int fd, off_t offset, unsigned bsize)
06adb5
 }
06adb5
 #endif
06adb5
 
06adb5
-static void add_j(struct gfs2_sbd *sdp, struct jadd_opts *opts)
06adb5
+static int add_j(struct gfs2_sbd *sdp, struct jadd_opts *opts)
06adb5
 {
06adb5
-	int fd;
06adb5
-	char new_name[256];
06adb5
-	int error;
06adb5
-	uint64_t addr;
06adb5
-
06adb5
-	fd = create_new_inode(opts, &addr);
06adb5
-
06adb5
-	{
06adb5
-		char buf[sdp->bsize];
06adb5
-		unsigned int blocks =
06adb5
-			sdp->jsize << (20 - sdp->sd_sb.sb_bsize_shift);
06adb5
-		unsigned int x;
06adb5
-		struct gfs2_log_header lh;
06adb5
-		uint64_t seq = RANDOM(blocks);
06adb5
-		off_t off = 0;
06adb5
-
06adb5
-		set_flags(fd, JA_FL_CLEAR, FS_JOURNAL_DATA_FL);
06adb5
-		memset(buf, 0, sdp->bsize);
06adb5
-		for (x=0; x
06adb5
-			if (write(fd, buf, sdp->bsize) != sdp->bsize) {
06adb5
-				perror("add_j");
06adb5
-				exit(EXIT_FAILURE);
06adb5
-			}
06adb5
+	int fd, error = 0;
06adb5
+	char new_name[256], buf[sdp->bsize];
06adb5
+	unsigned int x, blocks =
06adb5
+		sdp->jsize << (20 - sdp->sd_sb.sb_bsize_shift);
06adb5
+	struct gfs2_log_header lh;
06adb5
+	uint64_t seq = RANDOM(blocks), addr;
06adb5
+	off_t off = 0;
06adb5
+
06adb5
+	if ((fd = create_new_inode(opts, &addr)) < 0)
06adb5
+		return fd;
06adb5
+
06adb5
+	if ((error = set_flags(fd, JA_FL_CLEAR, FS_JOURNAL_DATA_FL)))
06adb5
+		goto close_fd;
06adb5
+
06adb5
+	memset(buf, 0, sdp->bsize);
06adb5
+	for (x=0; x
06adb5
+		if (write(fd, buf, sdp->bsize) != sdp->bsize) {
06adb5
+			perror("add_j write");
06adb5
+			error = -1;
06adb5
+			goto close_fd;
06adb5
 		}
06adb5
+	}
06adb5
 
06adb5
-		lseek(fd, 0, SEEK_SET);
06adb5
+	if ((error = lseek(fd, 0, SEEK_SET)) < 0) {
06adb5
+		perror("add_j lseek");
06adb5
+		goto close_fd;
06adb5
+	}
06adb5
 
06adb5
-		memset(&lh, 0, sizeof(struct gfs2_log_header));
06adb5
-		lh.lh_header.mh_magic = GFS2_MAGIC;
06adb5
-		lh.lh_header.mh_type = GFS2_METATYPE_LH;
06adb5
-		lh.lh_header.mh_format = GFS2_FORMAT_LH;
06adb5
-		lh.lh_flags = GFS2_LOG_HEAD_UNMOUNT;
06adb5
+	memset(&lh, 0, sizeof(struct gfs2_log_header));
06adb5
+	lh.lh_header.mh_magic = GFS2_MAGIC;
06adb5
+	lh.lh_header.mh_type = GFS2_METATYPE_LH;
06adb5
+	lh.lh_header.mh_format = GFS2_FORMAT_LH;
06adb5
+	lh.lh_flags = GFS2_LOG_HEAD_UNMOUNT;
06adb5
 #ifdef GFS2_HAS_LH_V2
06adb5
-		lh.lh_flags |= GFS2_LOG_HEAD_USERSPACE;
06adb5
-		lh.lh_jinode = addr;
06adb5
+	lh.lh_flags |= GFS2_LOG_HEAD_USERSPACE;
06adb5
+	lh.lh_jinode = addr;
06adb5
 #endif
06adb5
-		for (x=0; x
06adb5
-			uint32_t hash;
06adb5
-
06adb5
-			lh.lh_sequence = seq;
06adb5
-			lh.lh_blkno = x;
06adb5
-			gfs2_log_header_out(&lh, buf);
06adb5
-			hash = lgfs2_log_header_hash(buf);
06adb5
-			((struct gfs2_log_header *)buf)->lh_hash = cpu_to_be32(hash);
06adb5
+	for (x=0; x
06adb5
+		uint32_t hash;
06adb5
 #ifdef GFS2_HAS_LH_V2
06adb5
-			((struct gfs2_log_header *)buf)->lh_addr = cpu_to_be64(
06adb5
-			                           find_block_address(fd, off, sdp->bsize));
06adb5
-			hash = lgfs2_log_header_crc(buf, sdp->bsize);
06adb5
-			((struct gfs2_log_header *)buf)->lh_crc = cpu_to_be32(hash);
06adb5
+		uint64_t blk_addr = 0;
06adb5
 #endif
06adb5
-			if (write(fd, buf, sdp->bsize) != sdp->bsize) {
06adb5
-				perror("add_j");
06adb5
-				exit(EXIT_FAILURE);
06adb5
-			}
06adb5
-
06adb5
-			if (++seq == blocks)
06adb5
-				seq = 0;
06adb5
-			off += sdp->bsize;
06adb5
+		lh.lh_sequence = seq;
06adb5
+		lh.lh_blkno = x;
06adb5
+		gfs2_log_header_out(&lh, buf);
06adb5
+		hash = lgfs2_log_header_hash(buf);
06adb5
+		((struct gfs2_log_header *)buf)->lh_hash = cpu_to_be32(hash);
06adb5
+#ifdef GFS2_HAS_LH_V2
06adb5
+		if (!(blk_addr = find_block_address(fd, off, sdp->bsize))) {
06adb5
+			error = -1;
06adb5
+			goto close_fd;
06adb5
+		}
06adb5
+		((struct gfs2_log_header *)buf)->lh_addr = cpu_to_be64(blk_addr);
06adb5
+		hash = lgfs2_log_header_crc(buf, sdp->bsize);
06adb5
+		((struct gfs2_log_header *)buf)->lh_crc = cpu_to_be32(hash);
06adb5
+#endif
06adb5
+		if (write(fd, buf, sdp->bsize) != sdp->bsize) {
06adb5
+			perror("add_j write");
06adb5
+			error = -1;
06adb5
+			goto close_fd;
06adb5
 		}
06adb5
 
06adb5
-		error = fsync(fd);
06adb5
-		if (error){
06adb5
+		if (++seq == blocks)
06adb5
+			seq = 0;
06adb5
+		off += sdp->bsize;
06adb5
+
06adb5
+		if ((error = fsync(fd))) {
06adb5
 			perror("add_j fsync");
06adb5
-			exit(EXIT_FAILURE);
06adb5
+			goto close_fd;
06adb5
 		}
06adb5
 	}
06adb5
 
06adb5
-	close(fd);
06adb5
-
06adb5
 	sprintf(new_name, "journal%u", opts->journals);
06adb5
 	error = rename2system(opts, opts->jindex, new_name);
06adb5
 	if (error < 0 && errno != EEXIST){
06adb5
 		perror("add_j rename2system");
06adb5
-		exit(EXIT_FAILURE);
06adb5
+		goto close_fd;
06adb5
 	}
06adb5
+close_fd:
06adb5
+	return close(fd) || error;
06adb5
 }
06adb5
 
06adb5
 static int check_fit(struct gfs2_sbd *sdp, struct jadd_opts *opts)
06adb5
@@ -554,7 +583,7 @@ static int check_fit(struct gfs2_sbd *sdp, struct jadd_opts *opts)
06adb5
 		printf( _("Available space : %*lu blks\n\n"), 10,
06adb5
 			sdp->blks_total - sdp->blks_alloced);
06adb5
 		errno = ENOSPC;
06adb5
-		return 1;
06adb5
+		return -1;
06adb5
 	}
06adb5
 	return 0;
06adb5
 }
06adb5
@@ -581,35 +610,42 @@ int main(int argc, char *argv[])
06adb5
 
06adb5
 	sbd.path_fd = lgfs2_open_mnt_dir(opts.path, O_RDONLY|O_CLOEXEC, &mnt;;
06adb5
 	if (sbd.path_fd < 0) {
06adb5
-		fprintf(stderr, _("Error looking up mount '%s': %s\n"), opts.path, strerror(errno));
06adb5
-		exit(EXIT_FAILURE);
06adb5
+		fprintf(stderr, "Error looking up mount '%s': %s\n",
06adb5
+			opts.path, strerror(errno));
06adb5
+		ret = -1;
06adb5
+		goto out;
06adb5
 	}
06adb5
 	if (mnt == NULL) {
06adb5
-		fprintf(stderr, _("%s: not a mounted gfs2 file system\n"), opts.path);
06adb5
-		exit(EXIT_FAILURE);
06adb5
+		fprintf(stderr, "%s: not a mounted gfs2 file system\n", opts.path);
06adb5
+		ret = -1;
06adb5
+		goto close_sb;
06adb5
 	}
06adb5
-	gather_info(sdp, &opts);
06adb5
+
06adb5
+	if ((ret = gather_info(sdp, &opts)))
06adb5
+		goto close_sb;
06adb5
+
06adb5
 	mfs.context = copy_context_opt(mnt);
06adb5
-	if (mount_gfs2_meta(&mfs, mnt->mnt_dir, opts.debug)) {
06adb5
+	if ((ret = mount_gfs2_meta(&mfs, mnt->mnt_dir, opts.debug))) {
06adb5
 		perror("GFS2 metafs");
06adb5
-		exit(EXIT_FAILURE);
06adb5
+		goto close_sb;
06adb5
 	}
06adb5
 
06adb5
-	if (build_paths(mfs.path, &opts)) {
06adb5
+	if ((ret = build_paths(mfs.path, &opts))) {
06adb5
 		perror(_("Failed to build paths"));
06adb5
-		exit(EXIT_FAILURE);
06adb5
+		goto umount_meta;
06adb5
 	}
06adb5
 
06adb5
-	if (compute_constants(sdp)) {
06adb5
+	if ((ret = compute_constants(sdp))) {
06adb5
 		perror(_("Failed to compute file system constants"));
06adb5
-		exit(EXIT_FAILURE);
06adb5
+		goto free_paths;
06adb5
 	}
06adb5
-	find_current_journals(&opts);
06adb5
 
06adb5
-	ret = check_fit(sdp, &opts);
06adb5
-	if (ret) {
06adb5
+	if ((ret = find_current_journals(&opts)))
06adb5
+		goto free_paths;
06adb5
+
06adb5
+	if ((ret = check_fit(sdp, &opts))) {
06adb5
 		perror(_("Failed to add journals"));
06adb5
-		goto out;
06adb5
+		goto free_paths;
06adb5
 	}
06adb5
 
06adb5
 	total = opts.orig_journals + opts.journals;
06adb5
@@ -617,23 +653,29 @@ int main(int argc, char *argv[])
06adb5
 	     opts.journals < total;
06adb5
 	     opts.journals++) {
06adb5
 		if (metafs_interrupted) {
06adb5
-			cleanup_metafs(&mfs;;
06adb5
-			exit(130);
06adb5
+			errno = 130;
06adb5
+			goto free_paths;
06adb5
 		}
06adb5
-		add_ir(&opts);
06adb5
-		add_sc(&opts);
06adb5
-		add_qc(sdp, &opts);
06adb5
-		add_j(sdp, &opts);
06adb5
+		if ((ret = add_ir(&opts)))
06adb5
+			goto free_paths;
06adb5
+		if ((ret = add_sc(&opts)))
06adb5
+			goto free_paths;
06adb5
+		if ((ret = add_qc(sdp, &opts)))
06adb5
+			goto free_paths;
06adb5
+		if ((ret = add_j(sdp, &opts)))
06adb5
+			goto free_paths;
06adb5
 	}
06adb5
 
06adb5
-out:
06adb5
+free_paths:
06adb5
 	free(opts.new_inode);
06adb5
 	free(opts.per_node);
06adb5
 	free(opts.jindex);
06adb5
-	close(sdp->path_fd);
06adb5
-	cleanup_metafs(&mfs;;
06adb5
+umount_meta:
06adb5
 	sync();
06adb5
-
06adb5
+	cleanup_metafs(&mfs;;
06adb5
+close_sb:
06adb5
+	close(sdp->path_fd);
06adb5
+out:
06adb5
 	if (!ret)
06adb5
 		print_results(&opts);
06adb5