[prev in list] [next in list] [prev in thread] [next in thread] 

List:       linux-nilfs
Subject:    [PATCH] nilfs-utils: add missing error check on nilfs_sb_write
From:       Ryusuke Konishi <konishi.ryusuke () lab ! ntt ! co ! jp>
Date:       2010-09-26 13:48:01
Message-ID: 1285508881-6138-1-git-send-email-konishi.ryusuke () lab ! ntt ! co ! jp
[Download RAW message or body]

nilfs_sb_write() function does not see the return value of write()
system call, thus it may incur silent failure problem.

This fixes the issue and kills the following warning.

sb.c: In function ¡Ænilfs_sb_write¡Ç:
sb.c:206: warning: ignoring return value of ¡Æwrite¡Ç, declared with attribute warn_unused_result

Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
---
 lib/sb.c                     |   15 +++++++++++----
 sbin/nilfs-tune/nilfs-tune.c |   10 +++++++---
 2 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/lib/sb.c b/lib/sb.c
index 293cd6f..d93504c 100644
--- a/lib/sb.c
+++ b/lib/sb.c
@@ -171,7 +171,8 @@ int nilfs_sb_write(int devfd, struct nilfs_super_block *sbp, int mask)
 {
 	__u64 offsets[2];
 	struct nilfs_super_block *sbps[2];
-	int i;
+	ssize_t count;
+	int i, ret;
 	__u32 crc;
 
 	assert(devfd >= 0);
@@ -182,6 +183,7 @@ int nilfs_sb_write(int devfd, struct nilfs_super_block *sbp, int mask)
 	if (__nilfs_sb_read(devfd, sbps, offsets))
 		return -1;
 
+	ret = 0;
 	for (i = 0; i < 2; i++) {
 		if (!sbps[i])
 			continue;
@@ -202,12 +204,17 @@ int nilfs_sb_write(int devfd, struct nilfs_super_block *sbp, int mask)
 
 		crc = nilfs_sb_check_sum(sbps[i]);
 		sbps[i]->s_sum = cpu_to_le32(crc);
-		if (lseek(devfd, offsets[i], SEEK_SET) > 0)
-			write(devfd, sbps[i], NILFS_MAX_SB_SIZE);
+		if (lseek(devfd, offsets[i], SEEK_SET) > 0) {
+			count = write(devfd, sbps[i], NILFS_MAX_SB_SIZE);
+			if (count < NILFS_MAX_SB_SIZE) {
+				ret = -1;
+				break;
+			}
+		}
 	}
 
 	free(sbps[0]);
 	free(sbps[1]);
 
-	return 0;
+	return ret;
 }
diff --git a/sbin/nilfs-tune/nilfs-tune.c b/sbin/nilfs-tune/nilfs-tune.c
index f9e46a1..f5d5d83 100644
--- a/sbin/nilfs-tune/nilfs-tune.c
+++ b/sbin/nilfs-tune/nilfs-tune.c
@@ -434,9 +434,13 @@ int modify_nilfs(char *device, struct nilfs_tune_options *opts)
 	if (opts->mask & NILFS_SB_BLOCK_MAX)
 		sbp->s_c_block_max = cpu_to_le32(opts->c_block_max);
 
-	if (opts->mask)
-		nilfs_sb_write(devfd, sbp, opts->mask);
-
+	if (opts->mask) {
+		if (nilfs_sb_write(devfd, sbp, opts->mask) < 0) {
+			fprintf(stderr, "%s: cannot write super blocks\n",
+				device);
+			ret = EXIT_FAILURE;
+		}
+	}
 	if (opts->display)
 		show_nilfs_sb(sbp);
 
-- 
1.6.6.2

--
To unsubscribe from this list: send the line "unsubscribe linux-nilfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic