Skip to contentSkip to conversation

JFFS2 corruption

DISCUSSION Florian Schirmer 2 participants 7 messages Feb 5, 2004 – Feb 8, 2004 Memory technology devices (MTD)

lore
Share & tools
lore
#0 lore raw

Hi,

i'm having a strange problem with a JFFS2 filesystem: I always get messages about incorrect CRC values while mounting the filesystem. What i did:

<create a file which contains nothing but lots of 0x42 called dummy.bin>

eraseall -j /dev/mtdblock/3
mount -t jffs2 /dev/mtdblock /mnt
cp /dummy.bin /mnt/dummy1.bin
cp /dummy.bin /mnt/dummy2.bin
cp /dummy.bin /mnt/dummy3.bin
cp /dummy.bin /mnt/dummy4.bin
sync
cat /dev/mtdblock/3 > /dump1.raw
umount /mnt
cat /dev/mtdblock/3 > /dump2.raw
mount -t jffs2 /dev/mtdblock /mnt

<Multiple messages about invalid JFFS2 CRC values>

cat /dev/mtdblock/3 > /dump3.raw

cmp -l /dump1.raw /dump2.raw => files are the same cmp -l /dump2.raw /dump3.raw => Multiple differences.

Did some addition tests:

umount /mnt
cat /dev/mtdblock/3 > /dump2.raw
mount -t jffs2 /dev/mtdblock /mnt

Mounts just fine. But there are huge chunks of 0x00 in my dummy files on the jffs2 partition.

Any ideas on how to track down this problem? Basic MTD/flash stuff seems to work just fine. I can work with a cramfs image on this partition without any errors. So i assume something is going wrong in the JFFS2 code.

Specs:

Build Host: x86
Target: MIPS (LE)
Flash: NOR (AMD)
MTD/JFFS2 Codebase: current CVS

Thanks,
Florian

#1 lore raw
Quoted 4 lines · Florian Schirmer

Hi,

i'm having a strange problem with a JFFS2 filesystem: I always get messages about incorrect CRC values while mounting the filesystem. What i did:

Please set CONFIG_JFFS2_FS_DEBUG=1, then reproduce this with full logging -- you'll need to do it over a serial console to make sure you catch everything.

Signature · 1 line
dwmw2
#2 lore raw

Hi,

Quoted 7 lines
Quoted 3 lines

i'm having a strange problem with a JFFS2 filesystem: I always get messages about incorrect CRC values while mounting the filesystem. What i did:

Please set CONFIG_JFFS2_FS_DEBUG=1, then reproduce this with full logging -- you'll need to do it over a serial console to make sure you catch everything.

Thanks for your suggestion. I'm a little bit further finding this problem. It turns out to be a cmdset2 issue and not a jffs2 problem. I'm still digging what is going on.

What i've got so far is: jffs2_read_flash returns bogous data. In the case of a bad crc the last odd byte is wrong. Looks like a byte access instead of a word access. (Buswidth of the flash: 2) If you look at this:

<7>Node at 000bfbd4 (0) is a data node
<7>version 269, highest_version now 274
<4>crc'ing unpointed
<4>Node data dump (66 bytes)
<4>0x0000: 0x78 (0x78)
<4>0x0001: 0x5E (0x5E)
<4>0x0002: 0x72 (0x72)
<4>0x0003: 0x72 (0x72)
<4>0x0004: 0x1A (0x1A)
<4>0x0005: 0x05 (0x05)
<4>0x0006: 0xA3 (0xA3)
<4>0x0007: 0x21 (0x21)
<4>0x0008: 0x30 (0x30)
<4>0x0009: 0x1A (0x1A)
<4>0x000A: 0x02 (0x02)
<4>0x000B: 0xC3 (0xC3)
<4>0x000C: 0x31 (0x31)
<4>0x000D: 0x04 (0x04)
<4>0x000E: 0x00 (0x00)
<4>0x000F: 0x02 (0x02)
<4>0x0010: 0x68 (0x68)
<4>0x0011: 0xD4 (0xD4)
<4>0x0012: 0x4F (0x4F)
<4>0x0013: 0xA3 (0xA3)
<4>0x0014: 0x21 (0x21)
<4>0x0015: 0x30 (0x30)
<4>0x0016: 0x1A (0x1A)
<4>0x0017: 0x02 (0x02)
<4>0x0018: 0xC3 (0xC3)
<4>0x0019: 0x23 (0x23)
<4>0x001A: 0x04 (0x04)
<4>0x001B: 0x00 (0x00)
<4>0x001C: 0x02 (0x02)
<4>0x001D: 0x68 (0x68)
<4>0x001E: 0xD4 (0xD4)
<4>0x001F: 0x17 (0x17)
<4>0x0020: 0xA3 (0xA3)
<4>0x0021: 0x21 (0x21)
<4>0x0022: 0x30 (0x30)
<4>0x0023: 0x1A (0x1A)
<4>0x0024: 0x02 (0x02)
<4>0x0025: 0x43 (0x43)
<4>0x0026: 0x36 (0x36)
<4>0x0027: 0x04 (0x04)
<4>0x0028: 0x00 (0x00)
<4>0x0029: 0x02 (0x02)
<4>0x002A: 0x68 (0x68)
<4>0x002B: 0xD4 (0xD4)
<4>0x002C: 0xE1 (0xE1)
<4>0x002D: 0xA3 (0xA3)
<4>0x002E: 0x21 (0x21)
<4>0x002F: 0x30 (0x30)
<4>0x0030: 0x1A (0x1A)
<4>0x0031: 0x02 (0x02)
<4>0x0032: 0x43 (0x43)
<4>0x0033: 0x29 (0x29)
<4>0x0034: 0x04 (0x04)
<4>0x0035: 0x00 (0x00)
<4>0x0036: 0x02 (0x02)
<4>0x0037: 0x68 (0x68)
<4>0x0038: 0x84 (0x84)
<4>0x0039: 0xB8 (0xB8)
<4>0x003A: 0x15 (0x15)
<4>0x003B: 0x20 (0x20)
<4>0x003C: 0xC0 (0xC0)
<4>0x003D: 0x00 (0x00)
<4>0x003E: 0x0F (0x0F)
<4>0x003F: 0x3C (0x3C)
<4>0x0040: 0x20 (0x20)
<4>0x0041: 0x20 (0x3D)
<5>jffs2_get_inode_nodes(): Data CRC failed on node at 0x000bfbd4: Read 0xc073b413, calculated 0xa375d8ca
<7>Obsoleting previously unchecked node at 0x000bfbd4 of len 88: Dirtying

you will see that the last byte (offset 0x41) mirrors byte at offset 0x40. So i suspect a bug in the cmdset2 driver here. The third value is the value received by directly accessing the flash using the memory address and is always correct. If the node data size is odd then the 2nd byte from behind is affected:

<4>0x002F: 0x0F (0x0F)
<4>0x0030: 0x3C (0x3C)
<4>0x0031: 0x3C (0x20)
<4>0x0032: 0x3D (0x3D)

Am still collecting patterns to determine wether it is really a bug in the mtd core or just a cache issue or whatever.

BTW: Is %zd a leagal printk formatting tag? Doesn't seem to work for me. I can commit a fix (removing the z) for that if you want.

Regards,
Florian

#3 lore raw
Quoted 10 lines · Florian Schirmer

Hi,

Quoted 7 lines
Quoted 3 lines

i'm having a strange problem with a JFFS2 filesystem: I always get messages about incorrect CRC values while mounting the filesystem. What i did:

Please set CONFIG_JFFS2_FS_DEBUG=1, then reproduce this with full logging -- you'll need to do it over a serial console to make sure you catch everything.

Quoted 7 lines

Thanks for your suggestion. I'm a little bit further finding this problem. It turns out to be a cmdset2 issue and not a jffs2 problem. I'm still digging what is going on.

What i've got so far is: jffs2_read_flash returns bogous data. In the case of a bad crc the last odd byte is wrong. Looks like a byte access instead of a word access. (Buswidth of the flash: 2) If you look at this:

Interesting. That would be a problem in map_copy_from(), which is either just memcpy_fromio() or a function provided by your map driver, depending on whether you have CONFIG_MTD_COMPLEX_MAPPINGS enabled.

Can you check what memcpy_fromio() does directly? Try changing the definition of map_copy_from() in include/linux/mtd/map.h or drivers/mtd/maps/map_funcs.c or your map driver (depending on your configuration) to copy a byte at a time, and see if that fixes it.

The '%zd' modifier is the correct way to print a size_t; the old %Z was a gcc-ism. It's supported by the 2.6 and I thought also the current 2.4 kernels; what kernel are you using, precisely? It's a trivial patch to add it.

Signature · 1 line
dwmw2
#4 lore raw

Hi,

Quoted 8 lines

Interesting. That would be a problem in map_copy_from(), which is either just memcpy_fromio() or a function provided by your map driver, depending on whether you have CONFIG_MTD_COMPLEX_MAPPINGS enabled.

Can you check what memcpy_fromio() does directly? Try changing the definition of map_copy_from() in include/linux/mtd/map.h or drivers/mtd/maps/map_funcs.c or your map driver (depending on your configuration) to copy a byte at a time, and see if that fixes it.

Yeah. You're absolutely right. I should have checked cmdset2 before claiming it is broken.

memcopy_fromio is memcpy on MIPS. I've checked the memcpy implementation in arch/mips/lib/memcpy.S and it is using byte transfers for remaining byte transfers. So i expect at least the MIPS and SH plattform to be broken for non buswidth==1 setups. Haven't checked other archs yet.

I'm wondering what assumptions can be made about memcpy's access patterns? Looks like nobody even guarantees a specific access type. But we need to make sure every access to the flash matches the bus width, don't we?

My proposed solution would be to handle remaining transfers in maps.h/map_funcs.c already. So that the arch code only has to handle full transfers. See attached patch. I've to admit that its very hard/impossible to detect the remaining byte size. BITS_PER_LONG / 2 seems at least the best guess we can make. This will handle SH, MIPS32 and MIPS64 just fine.

Quoted 4 lines

The '%zd' modifier is the correct way to print a size_t; the old %Z was a gcc-ism. It's supported by the 2.6 and I thought also the current 2.4 kernels; what kernel are you using, precisely? It's a trivial patch to add it.

I'm still using what was provided by Broadcom/Linksys (2.4.20). So dont worry about that. I've ported the Broadcom code over to current 2.4.x but went back to 2.4.20 due to the jffs2 corruption.

Regards,
    Florian

include/linux/mtd/map.h renamed from include/linux/mtd/map.h-old +24 -1
@@ -156,7 +156,30 @@ static inline void map_write64(struct ma
 
 static inline void map_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
 {
-	memcpy_fromio(to, map->virt + from, len);
+#define MTD_ALIGN	(4 - 1)
+//#define MTD_ALIGN	((BITS_PER_LONG / 2) - 1)
+	u64 buf;
+	ssize_t transfer;
+	ssize_t done = (map->buswidth == 1) ? len : (len & ~MTD_ALIGN);
+	memcpy_fromio(to, map->virt + from, done);
+	while (done < len) {
+		switch(map->buswidth) {
+			case 2:
+				buf = map_read16(map, from + done);
+				break;
+			case 4:
+				buf = map_read32(map, from + done);
+				break;
+			case 8:
+				buf = map_read64(map, from + done);
+				break;
+		}
+		transfer = len - done;
+		if (transfer > map->buswidth)
+			transfer = map->buswidth;
+		memcpy((void *)((unsigned long)to + done), &buf, transfer);
+		done += transfer;
+	}
 }
 
 static inline void map_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
#5 lore raw
Quoted 4 lines · Florian Schirmer

memcopy_fromio is memcpy on MIPS. I've checked the memcpy implementation in arch/mips/lib/memcpy.S and it is using byte transfers for remaining byte transfers. So i expect at least the MIPS and SH plattform to be broken for non buswidth==1 setups. Haven't checked other archs yet.

I don't understand. Why should it be broken? If your bus controller is set up right for that region, byte loads ought to work fine. It'll _issue_ a wider load, then pick the right byte from it.

What's happening is that it's picking the _wrong_ byte from it. Is the bus controller set up to byte-swap transparently when we do 16-bit access?

Can you show the result of a single 32-bit load, two consecutive 16-bit loads, and four consecutive 8-bit loads all starting at the same address?

Quoted 3 lines

I'm wondering what assumptions can be made about memcpy's access patterns? Looks like nobody even guarantees a specific access type. But we need to make sure every access to the flash matches the bus width, don't we?

I don't think so. The bus controller really ought to handle this for us, for reading.

Signature · 1 line
dwmw2
#6 lore raw
In reply to Florian Schirmer
Quoted 2 lines · Florian Schirmer

i'm having a strange problem with a JFFS2 filesystem: I always get messages about incorrect CRC values while mounting the filesystem. What i did:

Also apply this.

Index: fs/jffs2/write.c
===================================================================
RCS file: /home/cvs/mtd/fs/jffs2/write.c,v
retrieving revision 1.80

jffs2/write.c +6 -0
diff -u -p -r1.80 write.c
@@ -97,6 +97,12 @@ struct jffs2_full_dnode *jffs2_write_dno
 	int retried = 0;
 	unsigned long cnt = 2;
 
+	if (data && datalen && 
+	    (je32_to_cpu(ri->data_crc) != crc32(0, data, datalen))) {
+		    printk(KERN_CRIT "Eep. Data CRC not correct in jffs2_write_dnode()\n");
+		    BUG();
+	}
+
 	D1(if(je32_to_cpu(ri->hdr_crc) != crc32(0, ri, sizeof(struct jffs2_unknown_node)-4)) {
 		printk(KERN_CRIT "Eep. CRC not correct in jffs2_write_dnode()\n");
 		BUG();
Signature · 1 line
dwmw2