Blame SOURCES/bz1833141-2-gfs2_jadd_error_handling_overhaul.patch

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