[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[Cluster-devel] [PATCH 8/8] libgfs2: Set errno in dirent_alloc and use dir_add consistently



Initially spotted by coverity: "error" is passed to a parameter that
cannot be negative.

It turned out that dirent_alloc was returning -ENOSPC and some calling
code (via dir_add) was assuming that errno was set. Other code was
assuming that dir_add returned -errno. This patch changes dirent_alloc
to set errno and return -1 and also updates the code which calls dir_add
to use errno.

Signed-off-by: Andrew Price <anprice redhat com>
---
 gfs2/fsck/initialize.c |   12 ++++++------
 gfs2/fsck/pass2.c      |    8 ++++----
 gfs2/fsck/pass3.c      |    2 +-
 gfs2/libgfs2/fs_ops.c  |   12 ++++++++----
 4 files changed, 19 insertions(+), 15 deletions(-)

diff --git a/gfs2/fsck/initialize.c b/gfs2/fsck/initialize.c
index bab61c1..3daf12d 100644
--- a/gfs2/fsck/initialize.c
+++ b/gfs2/fsck/initialize.c
@@ -375,7 +375,7 @@ static int rebuild_master(struct gfs2_sbd *sdp)
 		err = dir_add(sdp->master_dir, "jindex", 6, &inum,
 		              IF2DT(S_IFDIR | 0700));
 		if (err) {
-			log_crit(_("Error %d adding jindex directory\n"), err);
+			log_crit(_("Error %d adding jindex directory\n"), errno);
 			exit(FSCK_ERROR);
 		}
 		sdp->master_dir->i_di.di_nlink++;
@@ -394,7 +394,7 @@ static int rebuild_master(struct gfs2_sbd *sdp)
 			IF2DT(S_IFDIR | 0700));
 		if (err) {
 			log_crit(_("Error %d adding per_node directory\n"),
-				 err);
+				 errno);
 			exit(FSCK_ERROR);
 		}
 		sdp->master_dir->i_di.di_nlink++;
@@ -413,7 +413,7 @@ static int rebuild_master(struct gfs2_sbd *sdp)
 		err = dir_add(sdp->master_dir, "inum", 4, &inum,
 			IF2DT(S_IFREG | 0600));
 		if (err) {
-			log_crit(_("Error %d adding inum inode\n"), err);
+			log_crit(_("Error %d adding inum inode\n"), errno);
 			exit(FSCK_ERROR);
 		}
 	} else {
@@ -431,7 +431,7 @@ static int rebuild_master(struct gfs2_sbd *sdp)
 		err = dir_add(sdp->master_dir, "statfs", 6, &inum,
 			      IF2DT(S_IFREG | 0600));
 		if (err) {
-			log_crit(_("Error %d adding statfs inode\n"), err);
+			log_crit(_("Error %d adding statfs inode\n"), errno);
 			exit(FSCK_ERROR);
 		}
 	} else {
@@ -449,7 +449,7 @@ static int rebuild_master(struct gfs2_sbd *sdp)
 		err = dir_add(sdp->master_dir, "rindex", 6, &inum,
 			IF2DT(S_IFREG | 0600));
 		if (err) {
-			log_crit(_("Error %d adding rindex inode\n"), err);
+			log_crit(_("Error %d adding rindex inode\n"), errno);
 			exit(FSCK_ERROR);
 		}
 	} else {
@@ -466,7 +466,7 @@ static int rebuild_master(struct gfs2_sbd *sdp)
 		err = dir_add(sdp->master_dir, "quota", 5, &inum,
 			IF2DT(S_IFREG | 0600));
 		if (err) {
-			log_crit(_("Error %d adding quota inode\n"), err);
+			log_crit(_("Error %d adding quota inode\n"), errno);
 			exit(FSCK_ERROR);
 		}
 	} else {
diff --git a/gfs2/fsck/pass2.c b/gfs2/fsck/pass2.c
index d89dd4f..4201bb2 100644
--- a/gfs2/fsck/pass2.c
+++ b/gfs2/fsck/pass2.c
@@ -677,8 +677,8 @@ static int check_system_dir(struct gfs2_inode *sysinode, const char *dirname,
 					 GFS_FILE_DIR : DT_DIR));
 			if (error) {
 				log_err(_("Error adding directory %s: %s\n"),
-				        filename, strerror(error));
-				return -error;
+				        filename, strerror(errno));
+				return -errno;
 			}
 			if (cur_blks != sysinode->i_di.di_blocks)
 				reprocess_inode(sysinode, dirname);
@@ -897,8 +897,8 @@ int pass2(struct gfs2_sbd *sdp)
 						 DT_DIR));
 				if (error) {
 					log_err(_("Error adding directory %s: %s\n"),
-					        filename, strerror(error));
-					return -error;
+					        filename, strerror(errno));
+					return -errno;
 				}
 				if (cur_blks != ip->i_di.di_blocks) {
 					char dirname[80];
diff --git a/gfs2/fsck/pass3.c b/gfs2/fsck/pass3.c
index f0b35e2..ef4340e 100644
--- a/gfs2/fsck/pass3.c
+++ b/gfs2/fsck/pass3.c
@@ -58,7 +58,7 @@ static int attach_dotdot_to(struct gfs2_sbd *sdp, uint64_t newdotdot,
 		      (sdp->gfs1 ? GFS_FILE_DIR : DT_DIR));
 	if (err) {
 		log_err(_("Error adding directory %s: %s\n"),
-		        filename, strerror(err));
+		        filename, strerror(errno));
 		exit(FSCK_ERROR);
 	}
 	if (cur_blks != ip->i_di.di_blocks) {
diff --git a/gfs2/libgfs2/fs_ops.c b/gfs2/libgfs2/fs_ops.c
index befa25a..def6f80 100644
--- a/gfs2/libgfs2/fs_ops.c
+++ b/gfs2/libgfs2/fs_ops.c
@@ -737,6 +737,11 @@ int gfs2_dirent_next(struct gfs2_inode *dip, struct gfs2_buffer_head *bh,
 	return 0;
 }
 
+/**
+ * Allocate a gfs2 dirent
+ * Returns 0 on success, with *dent_out pointing to the new dirent,
+ * or -1 on failure, with errno set
+ */
 static int dirent_alloc(struct gfs2_inode *dip, struct gfs2_buffer_head *bh,
 			int name_len, struct gfs2_dirent **dent_out)
 {
@@ -807,7 +812,8 @@ static int dirent_alloc(struct gfs2_inode *dip, struct gfs2_buffer_head *bh,
 		}
 	} while (gfs2_dirent_next(dip, bh, &dent) == 0);
 
-	return -ENOSPC;
+	errno = ENOSPC;
+	return -1;
 }
 
 void dirent2_del(struct gfs2_inode *dip, struct gfs2_buffer_head *bh,
@@ -1395,10 +1401,8 @@ static struct gfs2_inode *__createi(struct gfs2_inode *dip,
 		inum.no_addr = bn;
 
 		err = dir_add(dip, filename, strlen(filename), &inum, IF2DT(mode));
-		if (err) {
-			errno = -err;
+		if (err)
 			return NULL;
-		}
 
 		if (if_gfs1)
 			is_dir = (IF2DT(mode) == GFS_FILE_DIR);
-- 
1.7.6.5


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]