Skip to contentSkip to conversation

fix inode->i_blocks 32-bit wrap in e2fsck/pass1.c.

PATCH In review Nick Dokos 2 participants 2 messages Apr 8, 2009 – Apr 15, 2009

lore
Share & tools
lore

Nick Dokos

author
#0 lore raw

PATCH 5/5 fix inode->i_blocks 32-bit wrap in e2fsck/pass1.c.

Running e2fsck on a 32TiB fs with a 16TiB file in it produced this error:

sudo ./e2fsck -n -f /dev/mapper/bigvg-bigvol e2fsck 1.41.4 (27-Jan-2009)
Pass 1: Checking inodes, blocks, and sizes
Inode 13, i_blocks is 3216, should be 34359741584. Fix? no

This patch adds a 64-bit aware access function to lib/ext2fs/blknum.c and uses that, instead of accessing the i_blocks field directly.

With this patch, the above error goes away.

In addition, there was a stutter in the code, where inode->i_blocks was checked twice in an if statement: I got rid of one instance. This was harmless, but unnecessary.

---
 e2fsck/pass1.c      |    6 +++---
 lib/ext2fs/blknum.c |   11 +++++++++++
 lib/ext2fs/ext2fs.h |    2 ++
 3 files changed, 16 insertions(+), 3 deletions(-)
e2fsck/pass1.c +3 -3
index 8c4d170..d69dd5c 100644
@@ -828,7 +828,7 @@ void e2fsck_pass1(e2fsck_t ctx)
 				check_blocks(ctx, &pctx, block_buf);
 				continue;
 			}
-			if ((inode->i_links_count || inode->i_blocks ||
+			if ((inode->i_links_count ||
 			     inode->i_blocks || inode->i_block[0]) &&
 			    fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR,
 					&pctx)) {
@@ -1925,7 +1925,7 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
 		pb.num_blocks *= (fs->blocksize / 512);
 #if 0
 	printf("inode %u, i_size = %lu, last_block = %lld, i_blocks=%lu, num_blocks = %lu\n",
-	       ino, inode->i_size, pb.last_block, inode->i_blocks,
+	       ino, inode->i_size, pb.last_block, ext2fs_inode_i_blocks(fs, inode),
 	       pb.num_blocks);
 #endif
 	if (pb.is_dir) {
@@ -1974,7 +1974,7 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
 	if (LINUX_S_ISREG(inode->i_mode) &&
 	    (inode->i_size_high || inode->i_size & 0x80000000UL))
 		ctx->large_files++;
-	if ((pb.num_blocks != inode->i_blocks) ||
+	if ((pb.num_blocks != ext2fs_inode_i_blocks(fs, inode)) ||
 	    ((fs->super->s_feature_ro_compat &
 	      EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
 	     (inode->i_flags & EXT4_HUGE_FILE_FL) &&
lib/ext2fs/blknum.c +11 -0
index 5959ded..b9666fb 100644
@@ -55,6 +55,17 @@ blk64_t ext2fs_inode_data_blocks2(ext2_filsys fs,
 }
 
 /*
+ * Return the inode i_blocks count
+ */
+blk64_t ext2fs_inode_i_blocks(ext2_filsys fs,
+					struct ext2_inode *inode)
+{
+	return (inode->i_blocks |
+		(fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT ?
+		 (__u64)inode->osd2.linux2.l_i_blocks_hi << 32 : 0));
+}
+
+/*
  * Return the fs block count
  */
 blk64_t ext2fs_blocks_count(struct ext2_super_block *super)
lib/ext2fs/ext2fs.h +2 -0
index ea8209b..b97d39e 100644
@@ -735,6 +735,8 @@ extern blk64_t ext2fs_group_first_block2(ext2_filsys fs, dgrp_t group);
 extern blk64_t ext2fs_group_last_block2(ext2_filsys fs, dgrp_t group);
 extern blk64_t ext2fs_inode_data_blocks2(ext2_filsys fs,
 					 struct ext2_inode *inode);
+extern blk64_t ext2fs_inode_i_blocks(ext2_filsys fs,
+					 struct ext2_inode *inode);
 extern blk64_t ext2fs_blocks_count(struct ext2_super_block *super);
 extern void ext2fs_blocks_count_set(struct ext2_super_block *super,
 				    blk64_t blk);
Signature · 1 line
1.6.0.6
#1 lore raw
Quoted 11 lines · Nick Dokos

Running e2fsck on a 32TiB fs with a 16TiB file in it produced this error:

sudo ./e2fsck -n -f /dev/mapper/bigvg-bigvol e2fsck 1.41.4 (27-Jan-2009)
Pass 1: Checking inodes, blocks, and sizes
Inode 13, i_blocks is 3216, should be 34359741584. Fix? no

This patch adds a 64-bit aware access function to lib/ext2fs/blknum.c and uses that, instead of accessing the i_blocks field directly.

Quoted 7 lines

With this patch, the above error goes away.

In addition, there was a stutter in the code, where inode->i_blocks was checked twice in an if statement: I got rid of one instance. This was harmless, but unnecessary.

Quoted 75 lines
---
 e2fsck/pass1.c      |    6 +++---
 lib/ext2fs/blknum.c |   11 +++++++++++
 lib/ext2fs/ext2fs.h |    2 ++
 3 files changed, 16 insertions(+), 3 deletions(-)
e2fsck/pass1.c +3 -3
index 8c4d170..d69dd5c 100644
@@ -828,7 +828,7 @@ void e2fsck_pass1(e2fsck_t ctx)
 				check_blocks(ctx, &pctx, block_buf);
 				continue;
 			}
-			if ((inode->i_links_count || inode->i_blocks ||
+			if ((inode->i_links_count ||
 			     inode->i_blocks || inode->i_block[0]) &&
 			    fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR,
 					&pctx)) {
@@ -1925,7 +1925,7 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
 		pb.num_blocks *= (fs->blocksize / 512);
 #if 0
 	printf("inode %u, i_size = %lu, last_block = %lld, i_blocks=%lu, num_blocks = %lu\n",
-	       ino, inode->i_size, pb.last_block, inode->i_blocks,
+	       ino, inode->i_size, pb.last_block, ext2fs_inode_i_blocks(fs, inode),
 	       pb.num_blocks);
 #endif
 	if (pb.is_dir) {
@@ -1974,7 +1974,7 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
 	if (LINUX_S_ISREG(inode->i_mode) &&
 	    (inode->i_size_high || inode->i_size & 0x80000000UL))
 		ctx->large_files++;
-	if ((pb.num_blocks != inode->i_blocks) ||
+	if ((pb.num_blocks != ext2fs_inode_i_blocks(fs, inode)) ||
 	    ((fs->super->s_feature_ro_compat &
 	      EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
 	     (inode->i_flags & EXT4_HUGE_FILE_FL) &&
lib/ext2fs/blknum.c +11 -0
index 5959ded..b9666fb 100644
@@ -55,6 +55,17 @@ blk64_t ext2fs_inode_data_blocks2(ext2_filsys fs,
 }
 
 /*
+ * Return the inode i_blocks count
+ */
+blk64_t ext2fs_inode_i_blocks(ext2_filsys fs,
+					struct ext2_inode *inode)
+{
+	return (inode->i_blocks |
+		(fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT ?
+		 (__u64)inode->osd2.linux2.l_i_blocks_hi << 32 : 0));
+}
+
+/*
  * Return the fs block count
  */
 blk64_t ext2fs_blocks_count(struct ext2_super_block *super)
lib/ext2fs/ext2fs.h +2 -0
index ea8209b..b97d39e 100644
@@ -735,6 +735,8 @@ extern blk64_t ext2fs_group_first_block2(ext2_filsys fs, dgrp_t group);
 extern blk64_t ext2fs_group_last_block2(ext2_filsys fs, dgrp_t group);
 extern blk64_t ext2fs_inode_data_blocks2(ext2_filsys fs,
 					 struct ext2_inode *inode);
+extern blk64_t ext2fs_inode_i_blocks(ext2_filsys fs,
+					 struct ext2_inode *inode);
 extern blk64_t ext2fs_blocks_count(struct ext2_super_block *super);
 extern void ext2fs_blocks_count_set(struct ext2_super_block *super,
 				    blk64_t blk);
Signature · 1 line
1.6.0.6