[PATCH 3/4] - e2fsprogs - accomodate full 32-bit inode numbers

Eric Sandeen <[email protected]> Fri, 25 Aug 2006 17:04:33 -0500
Newsgroups gmane.comp.file-systems.ext2.devel
Message-ID <[email protected]>
handle types & overflows related to full unsigned 32-bit inode numbers.

Signed-off-by: Eric Sandeen <[email protected]>

Index: e2fsprogs-1.39-16T-2/e2fsck/unix.c
===================================================================
--- e2fsprogs-1.39-16T-2.orig/e2fsck/unix.c
+++ e2fsprogs-1.39-16T-2/e2fsck/unix.c
@@ -98,7 +98,7 @@ static void usage(e2fsck_t ctx)
 static void show_stats(e2fsck_t	ctx)
 {
 	ext2_filsys fs = ctx->fs;
-	__u32 inodes, inodes_used;
+	ext2_ino_t inodes, inodes_used;
 	blk_t blocks, blocks_used;
 	int dir_links;
 	int num_files, num_links;
Index: e2fsprogs-1.39-16T-2/lib/ext2fs/initialize.c
===================================================================
--- e2fsprogs-1.39-16T-2.orig/lib/ext2fs/initialize.c
+++ e2fsprogs-1.39-16T-2/lib/ext2fs/initialize.c
@@ -231,8 +231,7 @@ retry:
 	 * should be.  But make sure that we don't allocate more than
 	 * one bitmap's worth of inodes each group.
 	 */
-	ipg = (super->s_inodes_count + fs->group_desc_count - 1) /
-		fs->group_desc_count;
+	ipg = ((super->s_inodes_count - 1)/fs->group_desc_count) + 1;
 	if (ipg > fs->blocksize * 8) {
 		if (super->s_blocks_per_group >= 256) {
 			/* Try again with slightly different parameters */
@@ -248,9 +247,8 @@ retry:
 	if (ipg > (unsigned) EXT2_MAX_INODES_PER_GROUP(super))
 		ipg = EXT2_MAX_INODES_PER_GROUP(super);
 
+ipg_retry:
 	super->s_inodes_per_group = ipg;
-	if (super->s_inodes_count > ipg * fs->group_desc_count)
-		super->s_inodes_count = ipg * fs->group_desc_count;
 
 	/*
 	 * Make sure the number of inodes per group completely fills
@@ -278,6 +276,10 @@ retry:
 	/*
 	 * adjust inode count to reflect the adjusted inodes_per_group
 	 */
+	if ((__u64)super->s_inodes_per_group * fs->group_desc_count > ~0U) {
+		ipg--;
+		goto ipg_retry;
+	}
 	super->s_inodes_count = super->s_inodes_per_group *
 		fs->group_desc_count;
 	super->s_free_inodes_count = super->s_inodes_count;
Index: e2fsprogs-1.39-16T-2/misc/mke2fs.c
===================================================================
--- e2fsprogs-1.39-16T-2.orig/misc/mke2fs.c
+++ e2fsprogs-1.39-16T-2/misc/mke2fs.c
@@ -895,7 +895,7 @@ static void PRS(int argc, char *argv[])
 	double		reserved_ratio = 5.0;
 	int		sector_size = 0;
 	int		show_version_only = 0;
-	ext2_ino_t	num_inodes = 0;
+	__u64		num_inodes = 0;	/* u64 to catch too-large input */
 	errcode_t	retval;
 	char *		oldpath = getenv("PATH");
 	char *		extended_opts = 0;
@@ -1430,6 +1430,21 @@ static void PRS(int argc, char *argv[])
 		fs_param.s_inode_size = inode_size;
 	}
 
+	/* Make sure number of inodes specified will fit in 32 bits */
+	if (num_inodes == 0) {
+		__u64 n;
+		n = (__u64) fs_param.s_blocks_count * blocksize / inode_ratio;
+		if (n > ~0U) {
+			com_err(program_name, 0,
+			    _("too many inodes (%llu), raise inode ratio?"), n);
+			exit(1);
+		}
+	} else if (num_inodes > ~0U) {
+		com_err(program_name, 0,
+			_("too many inodes (%llu), specify < 2^32 inodes"),
+			  (__u64)num_inodes);
+		exit(1);
+	}
 	/*
 	 * Calculate number of inodes based on the inode ratio
 	 */
Index: e2fsprogs-1.39-16T-2/resize/resize2fs.c
===================================================================
--- e2fsprogs-1.39-16T-2.orig/resize/resize2fs.c
+++ e2fsprogs-1.39-16T-2/resize/resize2fs.c
@@ -186,6 +186,7 @@ errcode_t adjust_fs_info(ext2_filsys fs,
 	unsigned long	i, j, old_desc_blocks, max_group;
 	unsigned int	meta_bg, meta_bg_size;
 	int		has_super;
+	__u64		new_inodes;	/* u64 to check for overflow */
 
 	fs->super->s_blocks_count = new_size;
 
@@ -227,6 +228,12 @@ retry:
 	/*
 	 * Adjust the number of inodes
 	 */
+	new_inodes =(__u64)fs->super->s_inodes_per_group * fs->group_desc_count;
+	if (new_inodes > ~0U) {
+		fprintf(stderr, _("inodes (%llu) must be less than %u"),
+				   new_inodes, ~0U);
+		return EXT2_ET_TOO_MANY_INODES;
+	}
 	fs->super->s_inodes_count = fs->super->s_inodes_per_group *
 		fs->group_desc_count;
 



-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642