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

genext2fs: new patches for Hurd support



user jk@jk.fr.eu.org
usertags 562999 gsoc2010
thanks

Hello,

The attached patches, based on the earlier one from Samuel Thibault,
add support for all the ext2 block sizes (1024, 2048 and 4096) to
genext2fs, and set the creator-os field of the superblock to an
appropriate value.

I have fixed a few problems with the block bitmaps which appear on
2048 and 4096 bytes blocks, related to the fact that the bootloader
area and superblock share the first block in these cases. Also, in
order to allow the block size to be chosen at runtime, I have has to
change the way the group descriptors are accessed (they cannot be kept
inside the 'filesystem' structure since their offset depends on the
block size).

The -B <block size> and -o <creator> options can be used to specify
the respective value. The last patch updates the test suite to reflect
these changes.

-- 
Jérémie Koenig <jk@jk.fr.eu.org>
http://jk.fr.eu.org/
From 61859664620308e43168b91cc6ec6ed6ecae0ea7 Mon Sep 17 00:00:00 2001
From: Jeremie Koenig <jk@jk.fr.eu.org>
Date: Tue, 25 May 2010 16:32:53 +0200
Subject: [PATCH 1/5] Support block sizes > 1024

The 'filesystem' structure is adapted to support arbitrary block sizes,
and the allocation code is fixed to handle first_data_block being 0.
---
 genext2fs.c |   27 +++++++++++++++------------
 1 files changed, 15 insertions(+), 12 deletions(-)

diff --git a/genext2fs.c b/genext2fs.c
index f0d797d..73c4eb1 100644
--- a/genext2fs.c
+++ b/genext2fs.c
@@ -571,16 +571,15 @@ typedef struct
 
 
 /* Filesystem structure that support groups */
-#if BLOCKSIZE == 1024
 typedef struct
 {
-	block zero;            // The famous block 0
-	superblock sb;         // The superblock
+	uint8 zero[1024];      // Room for bootloader stuff
+	superblock sb;         // The superblock, always at 1024
+#if BLOCKSIZE > 2048
+	uint8 zero2[BLOCKSIZE - 2048]; // align group descriptor on block size
+#endif
 	groupdescriptor gd[0]; // The group descriptors
 } filesystem;
-#else
-#error UNHANDLED BLOCKSIZE
-#endif
 
 // now the endianness swap
 
@@ -886,7 +885,7 @@ alloc_blk(filesystem *fs, uint32 nod)
 		error_msg_and_die("group descr %d. free blocks count == 0 (corrupted fs?)",grp);
 	if(!(fs->sb.s_free_blocks_count--))
 		error_msg_and_die("superblock free blocks count == 0 (corrupted fs?)");
-	return fs->sb.s_blocks_per_group*grp + bk;
+	return fs->sb.s_first_data_block + fs->sb.s_blocks_per_group*grp + (bk-1);
 }
 
 // free a block
@@ -1946,10 +1945,14 @@ init_fs(int nbblocks, int nbinodes, int nbresrvd, int holes, uint32 fs_timestamp
 	 */
 	min_nbgroups = (nbinodes + INODES_PER_GROUP - 1) / INODES_PER_GROUP;
 
+	/* On filesystems with 1k block size, the bootloader area uses a full
+	 * block. For 2048 and up, the superblock can be fitted into block 0.
+	 */
+	first_block = (BLOCKSIZE == 1024);
+
 	/* nbblocks is the total number of blocks in the filesystem.
 	 * a block group can have no more than 8192 blocks.
 	 */
-	first_block = (BLOCKSIZE == 1024);
 	nbgroups = (nbblocks - first_block + BLOCKS_PER_GROUP - 1) / BLOCKS_PER_GROUP;
 	if(nbgroups < min_nbgroups) nbgroups = min_nbgroups;
 	nbblocks_per_group = rndup((nbblocks - first_block + nbgroups - 1)/nbgroups, 8);
@@ -1961,10 +1964,10 @@ init_fs(int nbblocks, int nbinodes, int nbresrvd, int holes, uint32 fs_timestamp
 	gdsz = rndup(nbgroups*sizeof(groupdescriptor),BLOCKSIZE)/BLOCKSIZE;
 	itblsz = nbinodes_per_group * sizeof(inode)/BLOCKSIZE;
 	overhead_per_group = 3 /*sb,bbm,ibm*/ + gdsz + itblsz;
-	if((uint32)nbblocks - 1 < overhead_per_group * nbgroups)
-		error_msg_and_die("too much overhead, try fewer inodes or more blocks. Note: options have changed, see --help or the man page.");
-	free_blocks = nbblocks - overhead_per_group*nbgroups - 1 /*boot block*/;
+	free_blocks = nbblocks - overhead_per_group*nbgroups - first_block;
 	free_blocks_per_group = nbblocks_per_group - overhead_per_group;
+	if(free_blocks < 0)
+		error_msg_and_die("too much overhead, try fewer inodes or more blocks. Note: options have changed, see --help or the man page.");
 
 	if(!(fs = (filesystem*)calloc(nbblocks, BLOCKSIZE)))
 		error_msg_and_die("not enough memory for filesystem");
@@ -1986,7 +1989,7 @@ init_fs(int nbblocks, int nbinodes, int nbresrvd, int holes, uint32 fs_timestamp
 	fs->sb.s_lastcheck = fs_timestamp;
 
 	// set up groupdescriptors
-	for(i=0, bbmpos=gdsz+2, ibmpos=bbmpos+1, itblpos=ibmpos+1;
+	for(i=0, bbmpos=first_block+1+gdsz, ibmpos=bbmpos+1, itblpos=ibmpos+1;
 		i<nbgroups;
 		i++, bbmpos+=nbblocks_per_group, ibmpos+=nbblocks_per_group, itblpos+=nbblocks_per_group)
 	{
-- 
1.7.1

From 2e8251cfe728f2294da4a9257fab122ea1c40ec9 Mon Sep 17 00:00:00 2001
From: Jeremie Koenig <jk@jk.fr.eu.org>
Date: Tue, 25 May 2010 16:33:12 +0200
Subject: [PATCH 2/5] Make BLOCKSIZE a global variable

Since BLOCKSIZE is no longer a constant, the 'block' type cannot be a
table anymore and has been redefined as a pointer instead.

Also, group descriptors are aligned to the (now variable) block size,
so they have been removed from the 'filesystem' structure and are
accessed through the (new) function get_gd(fs, no).
---
 genext2fs.c |   86 +++++++++++++++++++++++++++++++++--------------------------
 1 files changed, 48 insertions(+), 38 deletions(-)

diff --git a/genext2fs.c b/genext2fs.c
index 73c4eb1..e722c94 100644
--- a/genext2fs.c
+++ b/genext2fs.c
@@ -151,7 +151,9 @@ struct stats {
 
 // block size
 
-#define BLOCKSIZE         1024
+static int blocksize = 1024;
+
+#define BLOCKSIZE         blocksize
 #define BLOCKS_PER_GROUP  8192
 #define INODES_PER_GROUP  8192
 /* Percentage of blocks that are reserved.*/
@@ -239,10 +241,10 @@ struct stats {
 	  (fs)->sb.s_blocks_per_group - 1) / (fs)->sb.s_blocks_per_group)
 
 // Get group block bitmap (bbm) given the group number
-#define GRP_GET_GROUP_BBM(fs,grp) ( get_blk((fs),(fs)->gd[(grp)].bg_block_bitmap) )
+#define GRP_GET_GROUP_BBM(fs,grp) ( get_blk((fs), get_gd((fs),(grp))->bg_block_bitmap) )
 
 // Get group inode bitmap (ibm) given the group number
-#define GRP_GET_GROUP_IBM(fs,grp) ( get_blk((fs),(fs)->gd[(grp)].bg_inode_bitmap) )
+#define GRP_GET_GROUP_IBM(fs,grp) ( get_blk((fs), get_gd((fs),(grp))->bg_inode_bitmap) )
 		
 // Given an inode number find the group it belongs to
 #define GRP_GROUP_OF_INODE(fs,nod) ( ((nod)-1) / (fs)->sb.s_inodes_per_group)
@@ -532,7 +534,7 @@ typedef struct
 	char d_name[0];
 } directory;
 
-typedef uint8 block[BLOCKSIZE];
+typedef uint8 *block;
 
 /* blockwalker fields:
    The blockwalker is used to access all the blocks of a file (including
@@ -575,10 +577,7 @@ typedef struct
 {
 	uint8 zero[1024];      // Room for bootloader stuff
 	superblock sb;         // The superblock, always at 1024
-#if BLOCKSIZE > 2048
-	uint8 zero2[BLOCKSIZE - 2048]; // align group descriptor on block size
-#endif
-	groupdescriptor gd[0]; // The group descriptors
+	// group descriptors come next, see get_gd() below
 } filesystem;
 
 // now the endianness swap
@@ -819,6 +818,14 @@ get_blk(filesystem *fs, uint32 blk)
 	return (uint8*)fs + blk*BLOCKSIZE;
 }
 
+// the group descriptors are aligned on the block size
+static inline groupdescriptor *
+get_gd(filesystem *fs, int no)
+{
+	int gdblk = (sizeof (filesystem) + BLOCKSIZE - 1) / BLOCKSIZE;
+	return ((groupdescriptor *) get_blk(fs, gdblk)) + no;
+}
+
 // return a given inode from a filesystem
 static inline inode *
 get_nod(filesystem *fs, uint32 nod)
@@ -828,7 +835,7 @@ get_nod(filesystem *fs, uint32 nod)
 
 	offset = GRP_IBM_OFFSET(fs,nod);
 	grp = GRP_GROUP_OF_INODE(fs,nod);
-	itab = (inode *)get_blk(fs, fs->gd[grp].bg_inode_table);
+	itab = (inode *)get_blk(fs, get_gd(fs,grp)->bg_inode_table);
 	return itab+offset-1;
 }
 
@@ -874,14 +881,14 @@ alloc_blk(filesystem *fs, uint32 nod)
 
 	grp = GRP_GROUP_OF_INODE(fs,nod);
 	nbgroups = GRP_NBGROUPS(fs);
-	if(!(bk = allocate(get_blk(fs,fs->gd[grp].bg_block_bitmap), 0))) {
+	if(!(bk = allocate(GRP_GET_GROUP_BBM(fs, grp), 0))) {
 		for(grp=0;grp<nbgroups && !bk;grp++)
-			bk=allocate(get_blk(fs,fs->gd[grp].bg_block_bitmap),0);
+			bk = allocate(GRP_GET_GROUP_BBM(fs, grp), 0);
 		grp--;
 	}
 	if (!bk)
 		error_msg_and_die("couldn't allocate a block (no free space)");
-	if(!(fs->gd[grp].bg_free_blocks_count--))
+	if(!(get_gd(fs, grp)->bg_free_blocks_count--))
 		error_msg_and_die("group descr %d. free blocks count == 0 (corrupted fs?)",grp);
 	if(!(fs->sb.s_free_blocks_count--))
 		error_msg_and_die("superblock free blocks count == 0 (corrupted fs?)");
@@ -896,8 +903,8 @@ free_blk(filesystem *fs, uint32 bk)
 
 	grp = bk / fs->sb.s_blocks_per_group;
 	bk %= fs->sb.s_blocks_per_group;
-	deallocate(get_blk(fs,fs->gd[grp].bg_block_bitmap), bk);
-	fs->gd[grp].bg_free_blocks_count++;
+	deallocate(GRP_GET_GROUP_BBM(fs, grp), bk);
+	get_gd(fs, grp)->bg_free_blocks_count++;
 	fs->sb.s_free_blocks_count++;
 }
 
@@ -917,16 +924,16 @@ alloc_nod(filesystem *fs)
 	/* We do it for all inodes.                                           */
 	avefreei  =  fs->sb.s_free_inodes_count / nbgroups;
 	for(grp=0; grp<nbgroups; grp++) {
-		if (fs->gd[grp].bg_free_inodes_count < avefreei ||
-		    fs->gd[grp].bg_free_inodes_count == 0)
+		if (get_gd(fs, grp)->bg_free_inodes_count < avefreei ||
+		    get_gd(fs, grp)->bg_free_inodes_count == 0)
 			continue;
 		if (!best_group || 
-			fs->gd[grp].bg_free_blocks_count > fs->gd[best_group].bg_free_blocks_count)
+			get_gd(fs, grp)->bg_free_blocks_count > get_gd(fs, best_group)->bg_free_blocks_count)
 			best_group = grp;
 	}
-	if (!(nod = allocate(get_blk(fs,fs->gd[best_group].bg_inode_bitmap),0)))
+	if (!(nod = allocate(GRP_GET_GROUP_IBM(fs, best_group), 0)))
 		error_msg_and_die("couldn't allocate an inode (no free inode)");
-	if(!(fs->gd[best_group].bg_free_inodes_count--))
+	if(!(get_gd(fs, best_group)->bg_free_inodes_count--))
 		error_msg_and_die("group descr. free blocks count == 0 (corrupted fs?)");
 	if(!(fs->sb.s_free_inodes_count--))
 		error_msg_and_die("superblock free blocks count == 0 (corrupted fs?)");
@@ -1392,7 +1399,7 @@ mknod_fs(filesystem *fs, uint32 parent_nod, const char *name, uint16 mode, uint1
 			case FM_IFDIR:
 				add2dir(fs, nod, nod, ".");
 				add2dir(fs, nod, parent_nod, "..");
-				fs->gd[GRP_GROUP_OF_INODE(fs,nod)].bg_used_dirs_count++;
+				get_gd(fs, GRP_GROUP_OF_INODE(fs,nod))->bg_used_dirs_count++;
 				break;
 		}
 	}
@@ -1884,7 +1891,7 @@ swap_goodfs(filesystem *fs)
 		swap_nod(nod);
 	}
 	for(i=0;i<GRP_NBGROUPS(fs);i++)
-		swap_gd(&(fs->gd[i]));
+		swap_gd(get_gd(fs, i));
 	swap_sb(&fs->sb);
 }
 
@@ -1894,7 +1901,7 @@ swap_badfs(filesystem *fs)
 	uint32 i;
 	swap_sb(&fs->sb);
 	for(i=0;i<GRP_NBGROUPS(fs);i++)
-		swap_gd(&(fs->gd[i]));
+		swap_gd(get_gd(fs, i));
 	for(i = 1; i < fs->sb.s_inodes_count; i++)
 	{
 		inode *nod = get_nod(fs, i);
@@ -1993,22 +2000,24 @@ init_fs(int nbblocks, int nbinodes, int nbresrvd, int holes, uint32 fs_timestamp
 		i<nbgroups;
 		i++, bbmpos+=nbblocks_per_group, ibmpos+=nbblocks_per_group, itblpos+=nbblocks_per_group)
 	{
+		groupdescriptor *gd = get_gd(fs, i);
+
 		if(free_blocks > free_blocks_per_group) {
-			fs->gd[i].bg_free_blocks_count = free_blocks_per_group;
+			gd->bg_free_blocks_count = free_blocks_per_group;
 			free_blocks -= free_blocks_per_group;
 		} else {
-			fs->gd[i].bg_free_blocks_count = free_blocks;
+			gd->bg_free_blocks_count = free_blocks;
 			free_blocks = 0; // this is the last block group
 		}
 		if(i)
-			fs->gd[i].bg_free_inodes_count = nbinodes_per_group;
+			gd->bg_free_inodes_count = nbinodes_per_group;
 		else
-			fs->gd[i].bg_free_inodes_count = nbinodes_per_group -
+			gd->bg_free_inodes_count = nbinodes_per_group -
 							EXT2_FIRST_INO + 2;
-		fs->gd[i].bg_used_dirs_count = 0;
-		fs->gd[i].bg_block_bitmap = bbmpos;
-		fs->gd[i].bg_inode_bitmap = ibmpos;
-		fs->gd[i].bg_inode_table = itblpos;
+		gd->bg_used_dirs_count = 0;
+		gd->bg_block_bitmap = bbmpos;
+		gd->bg_inode_bitmap = ibmpos;
+		gd->bg_inode_table = itblpos;
 	}
 
 	/* Mark non-filesystem blocks and inodes as allocated */
@@ -2016,9 +2025,9 @@ init_fs(int nbblocks, int nbinodes, int nbresrvd, int holes, uint32 fs_timestamp
 	for(i = 0; i<nbgroups;i++) {
 
 		/* Block bitmap */
-		bbm = get_blk(fs,fs->gd[i].bg_block_bitmap);	
+		bbm = GRP_GET_GROUP_BBM(fs, i);	
 		//non-filesystem blocks
-		for(j = fs->gd[i].bg_free_blocks_count
+		for(j = get_gd(fs, i)->bg_free_blocks_count
 		        + overhead_per_group + 1; j <= BLOCKSIZE * 8; j++)
 			allocate(bbm, j); 
 		//system blocks
@@ -2026,7 +2035,7 @@ init_fs(int nbblocks, int nbinodes, int nbresrvd, int holes, uint32 fs_timestamp
 			allocate(bbm, j); 
 
 		/* Inode bitmap */
-		ibm = get_blk(fs,fs->gd[i].bg_inode_bitmap);	
+		ibm = GRP_GET_GROUP_IBM(fs, i);
 		//non-filesystem inodes
 		for(j = fs->sb.s_inodes_per_group+1; j <= BLOCKSIZE * 8; j++)
 			allocate(ibm, j);
@@ -2040,9 +2049,9 @@ init_fs(int nbblocks, int nbinodes, int nbresrvd, int holes, uint32 fs_timestamp
 	// make root inode and directory
 	/* We have groups now. Add the root filesystem in group 0 */
 	/* Also increment the directory count for group 0 */
-	fs->gd[0].bg_free_inodes_count--;
-	fs->gd[0].bg_used_dirs_count = 1;
-	itab0 = (inode *)get_blk(fs,fs->gd[0].bg_inode_table);
+	get_gd(fs, 0)->bg_free_inodes_count--;
+	get_gd(fs, 0)->bg_used_dirs_count = 1;
+	itab0 = (inode *)get_blk(fs, get_gd(fs,0)->bg_inode_table);
 	itab0[EXT2_ROOT_INO-1].i_mode = FM_IFDIR | FM_IRWXU | FM_IRGRP | FM_IROTH | FM_IXGRP | FM_IXOTH; 
 	itab0[EXT2_ROOT_INO-1].i_ctime = fs_timestamp;
 	itab0[EXT2_ROOT_INO-1].i_mtime = fs_timestamp;
@@ -2366,8 +2375,9 @@ print_fs(filesystem *fs)
 	for (i = 0; i < GRP_NBGROUPS(fs); i++) {
 		printf("Group No: %d\n", i+1);
 		printf("block bitmap: block %d,inode bitmap: block %d, inode table: block %d\n",
-		     fs->gd[i].bg_block_bitmap, fs->gd[i].bg_inode_bitmap,
-		     fs->gd[i].bg_inode_table);
+		     get_gd(fs, i)->bg_block_bitmap,
+		     get_gd(fs, i)->bg_inode_bitmap,
+		     get_gd(fs, i)->bg_inode_table);
 		printf("block bitmap allocation:\n");
 		print_bm(GRP_GET_GROUP_BBM(fs, i),fs->sb.s_blocks_per_group);
 		printf("inode bitmap allocation:\n");
-- 
1.7.1

From 8f64fc491a9ef477839f10d1f1b522206b4c9285 Mon Sep 17 00:00:00 2001
From: Jeremie <jk@jk.fr.eu.org>
Date: Mon, 24 May 2010 17:31:58 +0200
Subject: [PATCH 3/5] Add the -B <size> option to control the block size

---
 genext2fs.c |   12 ++++++++++--
 1 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/genext2fs.c b/genext2fs.c
index e722c94..b164c20 100644
--- a/genext2fs.c
+++ b/genext2fs.c
@@ -2459,6 +2459,7 @@ showhelp(void)
 	"  -x, --starting-image <image>\n"
 	"  -d, --root <directory>\n"
 	"  -D, --devtable <file>\n"
+	"  -B, --block-size <bytes>\n"
 	"  -b, --size-in-blocks <blocks>\n"
 	"  -i, --bytes-per-inode <bytes per inode>\n"
 	"  -N, --number-of-inodes <number of inodes>\n"
@@ -2516,6 +2517,7 @@ main(int argc, char **argv)
 	  { "starting-image",	required_argument,	NULL, 'x' },
 	  { "root",		required_argument,	NULL, 'd' },
 	  { "devtable",		required_argument,	NULL, 'D' },
+	  { "block-size",	required_argument,	NULL, 'B' },
 	  { "size-in-blocks",	required_argument,	NULL, 'b' },
 	  { "bytes-per-inode",	required_argument,	NULL, 'i' },
 	  { "number-of-inodes",	required_argument,	NULL, 'N' },
@@ -2535,11 +2537,11 @@ main(int argc, char **argv)
 
 	app_name = argv[0];
 
-	while((c = getopt_long(argc, argv, "x:d:D:b:i:N:m:g:e:zfqUPhVv", longopts, NULL)) != EOF) {
+	while((c = getopt_long(argc, argv, "x:d:D:B:b:i:N:m:g:e:zfqUPhVv", longopts, NULL)) != EOF) {
 #else
 	app_name = argv[0];
 
-	while((c = getopt(argc, argv,      "x:d:D:b:i:N:m:g:e:zfqUPhVv")) != EOF) {
+	while((c = getopt(argc, argv,      "x:d:D:B:b:i:N:m:g:e:zfqUPhVv")) != EOF) {
 #endif /* HAVE_GETOPT_LONG */
 		switch(c)
 		{
@@ -2550,6 +2552,9 @@ main(int argc, char **argv)
 			case 'D':
 				dopt[didx++] = optarg;
 				break;
+			case 'B':
+				blocksize = SI_atof(optarg);
+				break;
 			case 'b':
 				nbblocks = SI_atof(optarg);
 				break;
@@ -2605,6 +2610,9 @@ main(int argc, char **argv)
 		error_msg_and_die("Not enough arguments. Try --help or else see the man page.");
 	fsout = argv[optind];
 
+	if(blocksize != 1024 && blocksize != 2048 && blocksize != 4096)
+		error_msg_and_die("Valid block sizes: 1024, 2048 or 4096.");
+
 	hdlinks.hdl = (struct hdlink_s *)malloc(hdlink_cnt * sizeof(struct hdlink_s));
 	if (!hdlinks.hdl)
 		error_msg_and_die("Not enough memory");
-- 
1.7.1

From 34d003df3ce7cc7aafbfbb6a17f41dfdc6aeccee Mon Sep 17 00:00:00 2001
From: Jeremie Koenig <jk@jk.fr.eu.org>
Date: Tue, 25 May 2010 16:12:37 +0200
Subject: [PATCH 4/5] Add the -o <os> option to set the creator_os field

---
 genext2fs.c |   51 +++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 47 insertions(+), 4 deletions(-)

diff --git a/genext2fs.c b/genext2fs.c
index b164c20..7ae4363 100644
--- a/genext2fs.c
+++ b/genext2fs.c
@@ -160,6 +160,15 @@ static int blocksize = 1024;
 #define RESERVED_BLOCKS       5/100
 #define MAX_RESERVED_BLOCKS  25/100
 
+/* The default value for s_creator_os. */
+#if defined(__GNU__)
+# define CREATOR_OS  1 /* Hurd */
+#elif defined(__FreeBSD__)
+# define CREATOR_OS  3 /* FreeBSD */
+#else
+# define CREATOR_OS  0 /* Linux */
+#endif
+
 
 // inode block size (why is it != BLOCKSIZE ?!?)
 /* The field i_blocks in the ext2 inode stores the number of data blocks
@@ -1926,7 +1935,8 @@ swap_badfs(filesystem *fs)
 
 // initialize an empty filesystem
 static filesystem *
-init_fs(int nbblocks, int nbinodes, int nbresrvd, int holes, uint32 fs_timestamp)
+init_fs(int nbblocks, int nbinodes, int nbresrvd, int holes,
+		uint32 fs_timestamp, uint32 creator_os)
 {
 	uint32 i;
 	filesystem *fs;
@@ -1994,6 +2004,7 @@ init_fs(int nbblocks, int nbinodes, int nbresrvd, int holes, uint32 fs_timestamp
 	fs->sb.s_wtime = fs_timestamp;
 	fs->sb.s_magic = EXT2_MAGIC_NUMBER;
 	fs->sb.s_lastcheck = fs_timestamp;
+	fs->sb.s_creator_os = creator_os;
 
 	// set up groupdescriptors
 	for(i=0, bbmpos=first_block+1+gdsz, ibmpos=bbmpos+1, itblpos=ibmpos+1;
@@ -2464,6 +2475,7 @@ showhelp(void)
 	"  -i, --bytes-per-inode <bytes per inode>\n"
 	"  -N, --number-of-inodes <number of inodes>\n"
 	"  -m, --reserved-percentage <percentage of blocks to reserve>\n"
+	"  -o, --creator-os <os>      'linux', 'hurd', 'freebsd' or a numerical value.\n"
 	"  -g, --block-map <path>     Generate a block map file for this path.\n"
 	"  -e, --fill-value <value>   Fill unallocated blocks with value.\n"
 	"  -z, --allow-holes          Allow files with holes.\n"
@@ -2485,6 +2497,29 @@ showhelp(void)
 extern char* optarg;
 extern int optind, opterr, optopt;
 
+// parse the value for -o <os>
+int
+lookup_creator_os(const char *name)
+{
+	static const char *const creators[] =
+		{"linux", "hurd", "2", "freebsd", NULL};
+	char *endptr;
+	int i;
+
+	// numerical value ?
+	i = strtol(name, &endptr, 0);
+	if(name[0] && *endptr == '\0')
+		return i;
+
+	// symbolic name ?
+	for(i=0; creators[i]; i++)
+	       if(strcasecmp(creators[i], name) == 0)
+		       return i;
+
+	// whatever ?
+	return -1;
+}
+
 int
 main(int argc, char **argv)
 {
@@ -2494,6 +2529,7 @@ main(int argc, char **argv)
 	float bytes_per_inode = -1;
 	float reserved_frac = -1;
 	int fs_timestamp = -1;
+	int creator_os = CREATOR_OS;
 	char * fsout = "-";
 	char * fsin = 0;
 	char * dopt[MAX_DOPT];
@@ -2522,6 +2558,7 @@ main(int argc, char **argv)
 	  { "bytes-per-inode",	required_argument,	NULL, 'i' },
 	  { "number-of-inodes",	required_argument,	NULL, 'N' },
 	  { "reserved-percentage", required_argument,	NULL, 'm' },
+	  { "creator-os",	required_argument,	NULL, 'o' },
 	  { "block-map",	required_argument,	NULL, 'g' },
 	  { "fill-value",	required_argument,	NULL, 'e' },
 	  { "allow-holes",	no_argument, 		NULL, 'z' },
@@ -2537,11 +2574,11 @@ main(int argc, char **argv)
 
 	app_name = argv[0];
 
-	while((c = getopt_long(argc, argv, "x:d:D:B:b:i:N:m:g:e:zfqUPhVv", longopts, NULL)) != EOF) {
+	while((c = getopt_long(argc, argv, "x:d:D:B:b:i:N:m:o:g:e:zfqUPhVv", longopts, NULL)) != EOF) {
 #else
 	app_name = argv[0];
 
-	while((c = getopt(argc, argv,      "x:d:D:B:b:i:N:m:g:e:zfqUPhVv")) != EOF) {
+	while((c = getopt(argc, argv,      "x:d:D:B:b:i:N:m:o:g:e:zfqUPhVv")) != EOF) {
 #endif /* HAVE_GETOPT_LONG */
 		switch(c)
 		{
@@ -2567,6 +2604,9 @@ main(int argc, char **argv)
 			case 'm':
 				reserved_frac = SI_atof(optarg) / 100;
 				break;
+			case 'o':
+				creator_os = lookup_creator_os(optarg);
+				break;
 			case 'g':
 				gopt[gidx++] = optarg;
 				break;
@@ -2612,6 +2652,8 @@ main(int argc, char **argv)
 
 	if(blocksize != 1024 && blocksize != 2048 && blocksize != 4096)
 		error_msg_and_die("Valid block sizes: 1024, 2048 or 4096.");
+	if(creator_os < 0)
+		error_msg_and_die("Creator OS unknown.");
 
 	hdlinks.hdl = (struct hdlink_s *)malloc(hdlink_cnt * sizeof(struct hdlink_s));
 	if (!hdlinks.hdl)
@@ -2657,7 +2699,8 @@ main(int argc, char **argv)
 		}
 		if(fs_timestamp == -1)
 			fs_timestamp = time(NULL);
-		fs = init_fs(nbblocks, nbinodes, nbresrvd, holes, fs_timestamp);
+		fs = init_fs(nbblocks, nbinodes, nbresrvd, holes,
+				fs_timestamp, creator_os);
 	}
 	
 	populate_fs(fs, dopt, didx, squash_uids, squash_perms, fs_timestamp, NULL);
-- 
1.7.1

From 9abef31a0b996e08f2775e5da6a1ec072882295d Mon Sep 17 00:00:00 2001
From: Jeremie Koenig <jk@jk.fr.eu.org>
Date: Thu, 27 May 2010 12:23:21 +0200
Subject: [PATCH 5/5] Update the test suite to handle -B <sz> and -o <os>

---
 test-gen.lib  |    6 +++---
 test-mount.sh |   32 ++++++++++++++++++++------------
 test.sh       |   28 ++++++++++++++++++----------
 3 files changed, 41 insertions(+), 25 deletions(-)

diff --git a/test-gen.lib b/test-gen.lib
index c6e73b9..1981216 100644
--- a/test-gen.lib
+++ b/test-gen.lib
@@ -8,7 +8,7 @@
 # Creates an image with a file of given size
 # Usage: dgen file-size number-of-blocks 
 dgen () {
-	size=$1; blocks=$2
+	size=$1; blocks=$2; blocksz=$3;
 	rm -rf test
 	mkdir -p test
 	cd test
@@ -20,7 +20,7 @@ dgen () {
         chmod 777 file.$1
 	TZ=UTC-11 touch -t 200502070321.43 file.$1 .
 	cd ..
-	./genext2fs -N 17 -b $blocks -d test -f -q ext2.img 
+	./genext2fs -B $blocksz -N 17 -b $blocks -d test -f -o Linux -q ext2.img
 }
 
 # fgen - Exercises the -f spec-file option of genext2fs
@@ -31,7 +31,7 @@ fgen () {
 	mkdir -p test
 	cp $fname test
 	TZ=UTC-11 touch -t 200502070321.43 test/$fname
-	./genext2fs -N 92 -b $blocks -D test/$fname -f ext2.img
+	./genext2fs -N 92 -b $blocks -D test/$fname -f -o Linux ext2.img
 }
 
 # gen_cleanup - Remove the files generated by the above functions
diff --git a/test-mount.sh b/test-mount.sh
index cd07edc..c5ddc1a 100755
--- a/test-mount.sh
+++ b/test-mount.sh
@@ -33,9 +33,9 @@ pass () {
 # and returns the command line with which to invoke dtest()
 # Usage: dtest-mount file-size number-of-blocks 
 dtest_mount () {
-	size=$1; blocks=$2
-	echo Testing with file of size $size
-	dgen $size $blocks
+	size=$1; blocks=$2; blocksz=$3;
+	echo Testing $blocks blocks of $blocksz bytes with file of size $size
+	dgen $size $blocks $blocksz
 	/sbin/e2fsck -fn ext2.img || fail
 	mkdir -p mnt
 	mount -t ext2 -o ro,loop ext2.img mnt || fail
@@ -44,7 +44,7 @@ dtest_mount () {
 	                                awk '{print $5}'`" ] ; then
 		fail
 	fi
-	pass dtest $size $blocks
+	pass dtest $size $blocks $blocksz
 }
 
 # ftest-mount - Exercise the -f spec-file option of genext2fs
@@ -75,13 +75,21 @@ ftest_mount () {
 	pass ftest $fname $blocks
 }
 
-dtest_mount 0 4096 
-dtest_mount 0 8193
-dtest_mount 0 8194
-dtest_mount 1 4096 
-dtest_mount 12288 4096 
-dtest_mount 274432 4096 
-dtest_mount 8388608 9000 
-dtest_mount 16777216 20000
+dtest_mount 0 4096 1024
+dtest_mount 0 2048 2048
+dtest_mount 0 1024 4096
+dtest_mount 0 8193 1024
+dtest_mount 0 8194 1024
+dtest_mount 0 8193 4096
+dtest_mount 0 8194 2048
+dtest_mount 1 4096 1024
+dtest_mount 1 1024 4096
+dtest_mount 12288 4096 1024
+dtest_mount 274432 4096 1024
+dtest_mount 8388608 9000 1024
+dtest_mount 8388608 4500 2048
+dtest_mount 8388608 2250 4096
+dtest_mount 16777216 20000 1024
+dtest_mount 16777216 10000 2048
 
 ftest_mount device_table.txt 4096 
diff --git a/test.sh b/test.sh
index dbe71ef..2b7a1dd 100755
--- a/test.sh
+++ b/test.sh
@@ -30,9 +30,9 @@ md5cmp () {
 # Creates an image with a file of given size and verifies it
 # Usage: dtest file-size number-of-blocks correct-checksum
 dtest () {
-	size=$1; blocks=$2; checksum=$3
+	size=$1; blocks=$2; blocksz=$3; checksum=$4
 	echo Testing with file of size $size
-	dgen $size $blocks
+	dgen $size $blocks $blocksz
 	md5cmp $checksum
 	gen_cleanup
 }
@@ -53,12 +53,20 @@ ftest () {
 # replace the following lines with the output of
 # sudo sh test-mount.sh|grep test
 
-dtest 0 4096 3bc6424b8fcd51a0de34ee59d91d5f16
-dtest 0 8193 f174804f6b433b552706cbbfc60c416d
-dtest 0 8194 4855a55d0cbdc44584634df49ebd5711
-dtest 1 4096 09c569b6bfb45222c729c42d04d5451f
-dtest 12288 4096 61febcbfbf32024ef99103fcdc282c39
-dtest 274432 4096 0c517803552c55c1806e4220b0a0164f
-dtest 8388608 9000 e0e5ea15bced10ab486d8135584b5d8e
-dtest 16777216 20000 fdf636eb905ab4dc1bf76dce5ac5d209
+dtest 0 4096 1024 3bc6424b8fcd51a0de34ee59d91d5f16
+dtest 0 2048 2048 230afa16496df019878cc2370c661cdc
+dtest 0 1024 4096 ebff5eeb38b70f3f1cd081e60eb44561
+dtest 0 8193 1024 f174804f6b433b552706cbbfc60c416d
+dtest 0 8194 1024 4855a55d0cbdc44584634df49ebd5711
+dtest 0 8193 4096 c493679698418ec7e6552005e2d2a6d8
+dtest 0 8194 2048 ec13f328fa7543563f35f494bddc059c
+dtest 1 4096 1024 09c569b6bfb45222c729c42d04d5451f
+dtest 1 1024 4096 d318a326fdc907810ae9e6b0a20e9b06
+dtest 12288 4096 1024 61febcbfbf32024ef99103fcdc282c39
+dtest 274432 4096 1024 0c517803552c55c1806e4220b0a0164f
+dtest 8388608 9000 1024 e0e5ea15bced10ab486d8135584b5d8e
+dtest 8388608 4500 2048 39f4d537a72f5053fd6891721c59680d
+dtest 8388608 2250 4096 1d697fa4bc2cfffe02ac91edfadc40bf
+dtest 16777216 20000 1024 fdf636eb905ab4dc1bf76dce5ac5d209
+dtest 16777216 10000 2048 f9824a81ea5e74fdf469c097927c292b
 ftest device_table.txt 4096 a0af06d944b11d2902dfd705484c64cc
-- 
1.7.1


Reply to: