Skip to contentSkip to conversation

e2fsprogs: Handle rec_len correctly for 64KB blocksize

PATCH Jan Kara 3 participants 6 messages Nov 7, 2007 – Nov 13, 2007 1 message not in the archive

lore
Share & tools
lore

Jan Kara

author
#1 lore raw
In reply to a message not in the archive

Hello,

sorry for replying to myself but I've just found out that the patch I've
sent was and old version of the patch which had some problems. Attached is a new version.

Quoted 3 lines · Jan Kara

it seems attached patch still did not get your attention. It makes
e2fsprogs properly handle filesystems with 64KB block size. Could you put it into e2fsprogs git? Thanks.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR

Subject: Support for 64KB blocksize in ext2-4 directories.

When block size is 64KB, we have to take care that rec_len does not overflow. Kernel stores 0xffff in case 0x10000 should be stored - perform appropriate conversion when reading from / writing to disk.

lib/ext2fs/dirblock.c +10 -8
index fb20fa0..db73edd 100644
@@ -38,9 +38,9 @@ errcode_t ext2fs_read_dir_block2(ext2_fi
 		dirent = (struct ext2_dir_entry *) p;
 #ifdef WORDS_BIGENDIAN
 		dirent->inode = ext2fs_swab32(dirent->inode);
-		dirent->rec_len = ext2fs_swab16(dirent->rec_len);
 		dirent->name_len = ext2fs_swab16(dirent->name_len);
 #endif
+		dirent->rec_len = ext2fs_rec_len_from_disk(dirent->rec_len);
 		name_len = dirent->name_len;
 #ifdef WORDS_BIGENDIAN
 		if (flags & EXT2_DIRBLOCK_V2_STRUCT)
@@ -68,12 +68,15 @@ errcode_t ext2fs_read_dir_block(ext2_fil
 errcode_t ext2fs_write_dir_block2(ext2_filsys fs, blk_t block,
 				  void *inbuf, int flags EXT2FS_ATTR((unused)))
 {
-#ifdef WORDS_BIGENDIAN
 	errcode_t	retval;
 	char		*p, *end;
 	char		*buf = 0;
 	struct ext2_dir_entry *dirent;
 
+#ifndef WORDS_BIGENDIAN
+	if (fs->blocksize < EXT2_MAX_REC_LEN)
+		goto just_write;
+#endif
 	retval = ext2fs_get_mem(fs->blocksize, &buf);
 	if (retval)
 		return retval;
@@ -88,19 +91,18 @@ errcode_t ext2fs_write_dir_block2(ext2_f
 			return (EXT2_ET_DIR_CORRUPTED);
 		}
 		p += dirent->rec_len;
+		dirent->rec_len = ext2fs_rec_len_to_disk(dirent->rec_len);
+#ifdef WORDS_BIGENDIAN
 		dirent->inode = ext2fs_swab32(dirent->inode);
-		dirent->rec_len = ext2fs_swab16(dirent->rec_len);
-		dirent->name_len = ext2fs_swab16(dirent->name_len);
-
-		if (flags & EXT2_DIRBLOCK_V2_STRUCT)
+		if (!(flags & EXT2_DIRBLOCK_V2_STRUCT))
 			dirent->name_len = ext2fs_swab16(dirent->name_len);
+#endif
 	}
  	retval = io_channel_write_blk(fs->io, block, 1, buf);
 	ext2fs_free_mem(&buf);
 	return retval;
-#else
+just_write:
  	return io_channel_write_blk(fs->io, block, 1, (char *) inbuf);
-#endif
 }
 
 
lib/ext2fs/ext2_fs.h +26 -0
index a316665..21747c2 100644
@@ -717,6 +718,32 @@ struct ext2_dir_entry_2 {
 #define EXT2_DIR_ROUND			(EXT2_DIR_PAD - 1)
 #define EXT2_DIR_REC_LEN(name_len)	(((name_len) + 8 + EXT2_DIR_ROUND) & \
 					 ~EXT2_DIR_ROUND)
+#define EXT2_MAX_REC_LEN		((1<<16)-1)
+
+static inline unsigned ext2fs_rec_len_from_disk(unsigned len)
+{
+#ifdef WORDS_BIGENDIAN
+	len = ext2fs_swab16(dlen);
+#endif
+	if (len == EXT2_MAX_REC_LEN)
+		return 1 << 16;
+	return len;
+}
+
+static inline unsigned ext2fs_rec_len_to_disk(unsigned len)
+{
+	if (len == (1 << 16))
+#ifdef WORDS_BIGENDIAN
+		return ext2fs_swab16(EXT2_MAX_REC_LEN);
+#else
+		return EXT2_MAX_REC_LEN;
+#endif
+#ifdef WORDS_BIGENDIAN
+	return ext2fs_swab_16(len);
+#else
+	return len;
+#endif
+}
 
 /*
  * This structure will be used for multiple mount protection. It will be
misc/e2image.c +2 -7
index 1fbb267..4e2c9fb 100644
@@ -345,10 +345,7 @@ static void scramble_dir_block(ext2_fils
 	end = buf + fs->blocksize;
 	for (p = buf; p < end-8; p += rec_len) {
 		dirent = (struct ext2_dir_entry_2 *) p;
-		rec_len = dirent->rec_len;
-#ifdef WORDS_BIGENDIAN
-		rec_len = ext2fs_swab16(rec_len);
-#endif
+		rec_len = ext2fs_rec_len_from_disk(dirent->rec_len);
 #if 0
 		printf("rec_len = %d, name_len = %d\n", rec_len, dirent->name_len);
 #endif
@@ -358,9 +355,7 @@ static void scramble_dir_block(ext2_fils
 			       "bad rec_len (%d)\n", (unsigned long) blk, 
 			       rec_len);
 			rec_len = end - p;
-#ifdef WORDS_BIGENDIAN
-				dirent->rec_len = ext2fs_swab16(rec_len);
-#endif
+			dirent->rec_len = ext2fs_rec_len_to_disk(rec_len);
 			continue;
 		}
 		if (dirent->name_len + 8 > rec_len) {
#2 lore raw
Quoted 5 lines · Jan Kara

Subject: Support for 64KB blocksize in ext2-4 directories.

When block size is 64KB, we have to take care that rec_len does not overflow. Kernel stores 0xffff in case 0x10000 should be stored - perform appropriate conversion when reading from / writing to disk.

NACK. You can't do the conversion in the reader/writer routines because the fundamentally rec_len is only a 16 bit field. So when you read a directory block where the rec_len field is encoded as 0xFFFF, and you translate it to 0x10000, when you assign it to dirent->rec_len, the 0x10000 gets chopped off and rec_len gets a value of zero. Did you test this patch before submitting it?

The only way to do this is to find all of the places that reference rec_len, and do the check there.

- Ted

author
#3 lore raw
Quoted 8 lines · Theodore Tso
Quoted 5 lines · Jan Kara

Subject: Support for 64KB blocksize in ext2-4 directories.

When block size is 64KB, we have to take care that rec_len does not overflow. Kernel stores 0xffff in case 0x10000 should be stored - perform appropriate conversion when reading from / writing to disk.

Quoted 6 lines

NACK. You can't do the conversion in the reader/writer routines because the fundamentally rec_len is only a 16 bit field. So when you read a directory block where the rec_len field is encoded as 0xFFFF, and you translate it to 0x10000, when you assign it to dirent->rec_len, the 0x10000 gets chopped off and rec_len gets a value of zero. Did you test this patch before submitting it?

Argh, stupid me. I've just tested that I didn't break anything for normal
block size and thought that I cannot make mistake in such a simple thing ;).

Quoted 2 lines

The only way to do this is to find all of the places that reference rec_len, and do the check there.

Yes.. Thanks for having look.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#4 lore raw
Quoted 5 lines · Jan Kara
Quoted 1 line

Did you test this patch before submitting it?

Argh, stupid me. I've just tested that I didn't break anything for normal
block size and thought that I cannot make mistake in such a simple thing ;).

Could I ask you to perhaps include some 64k blocksize test cases that would exercise the new codepaths?

Quoted 3 lines
Quoted 2 lines

The only way to do this is to find all of the places that reference rec_len, and do the check there.

Yes.. Thanks for having look.

One suggestion is that instead of just creating an conversion function, and then doing a global search and replace, in some places it might be better to declare an integer variable, and then assign "rec_len = ext2fs_rec_len_from_disk(dirent->rec_len)". For example, that would make ext2fs_process_dir_block() more readable, where dirent->rec_len is used no less than eight times.

Thanks, and my apologies for not having time to review the patch until now. At the moment things are a bit crazy since I am effectively doing two jobs, since I am in transition between two assignments, and me doing most of both of them at the moment. I should have substantially more time after the new year begins.

- Ted

author
#5 lore raw
Quoted 9 lines · Theodore Tso
Quoted 5 lines · Jan Kara
Quoted 1 line

Did you test this patch before submitting it?

Argh, stupid me. I've just tested that I didn't break anything for normal
block size and thought that I cannot make mistake in such a simple thing ;).

Could I ask you to perhaps include some 64k blocksize test cases that would exercise the new codepaths?

Fair enough, will do.

Quoted 10 lines
Quoted 3 lines
Quoted 2 lines

The only way to do this is to find all of the places that reference rec_len, and do the check there.

Yes.. Thanks for having look.

One suggestion is that instead of just creating an conversion function, and then doing a global search and replace, in some places it might be better to declare an integer variable, and then assign "rec_len = ext2fs_rec_len_from_disk(dirent->rec_len)". For example, that would make ext2fs_process_dir_block() more readable, where dirent->rec_len is used no less than eight times.

OK, thanks for suggestion.

Quoted 5 lines

Thanks, and my apologies for not having time to review the patch until now. At the moment things are a bit crazy since I am effectively doing two jobs, since I am in transition between two assignments, and me doing most of both of them at the moment. I should have substantially more time after the new year begins.

I see. I'll be patient then :)

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#6 lore raw
In reply to Theodore Tso
Quoted 9 lines · Theodore Tso
Quoted 5 lines · Jan Kara
Quoted 1 line

Did you test this patch before submitting it?

Argh, stupid me. I've just tested that I didn't break anything for normal
block size and thought that I cannot make mistake in such a simple thing ;).

Could I ask you to perhaps include some 64k blocksize test cases that would exercise the new codepaths?

One thing I had done to verify that 64kB rec_len was broken was to always create lost+found with at least 2 disk blocks, since the second block in lost+found will not have any dirents in it and will tickle this bug.

Patch attached.

Cheers, Andreas
--
Andreas Dilger
Sr. Software Engineer, Lustre Group
Sun Microsystems of Canada, Inc.

misc/mke2fs.c +4 -1
Index: e2fsprogs-cfs/misc/mke2fs.c
===================================================================
@@ -551,7 +551,10 @@ static void create_lost_and_found(ext2_f
 	}
 	
 	for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
-		if ((lpf_size += fs->blocksize) >= 16*1024)
+		/* Ensure that lost+found is at least 2 blocks, so we always
+		 * test large empty blocks for big-block filesystems.  */
+		if ((lpf_size += fs->blocksize) >= 16*1024 &&
+		    lpf_size >= 2 * fs->blocksize)
 			break;
 		retval = ext2fs_expand_dir(fs, ino);
 		if (retval) {