我找到了许多链接,但几乎所有链接都指向修复不是原因。
我在SD卡上创建了一个7GB的ext4分区,通过USB读卡器连接到PC。我有一个应用程序,它将10488576字节写入上述分区(/ dev / sdc2)。应用程序运行后,文件系统看起来很糟糕:
#fsck.ext4 -v /dev/sdc2
e2fsck 1.42.8 (20-Jun-2013)
ext2fs_open2: Bad magic number in super-block
fsck.ext4: Superblock invalid, trying backup blocks...
Superblock has an invalid journal (inode 8).
Clear<y>? no
fsck.ext4: Illegal inode number while checking ext3 journal for /dev/sdc2
/dev/sdc2: ***** FILE SYSTEM WAS MODIFIED *****
/dev/sdc2: ********** WARNING: Filesystem still has errors **********
#dumpe2fs /dev/sdc2
dumpe2fs 1.42.8 (20-Jun-2013)
dumpe2fs: Bad magic number in super-block while trying to open /dev/sdc2
Couldn't find valid filesystem superblock.
应用程序只是使用类似下面的内容(我无法发布确切的代码):
char *write_buf; //declared in header
write_buf = (char *) malloc(size) // where size = 10488576. This allocation is happening in function a() called from main
char *buf; // declared locally in function b()
buf = write_buf; // in function b()
write(fd,buf,size); // in function b()
文件系统块大小为4K。 备份超级块32768,98304,163840,229376,294912,819200,884736,1605632 如果需要更多信息,请告诉我。我需要了解可能导致这种损坏的原因,因为我非常肯定应用程序代码可能有问题。
EDIT:
我可以看到主超级块从0开始,write()
之前的lseek()调用也在SEEK_SET
为0,这将覆盖超级块信息。我将在write()
之前尝试远离超级块的lseek。
EDIT
正如我上面提到的那样,我已经解决了这个问题。根据dumpe2fs o / p,我在0组以下:
Group 0: (Blocks 0-32767)
Checksum 0x8bba, unused inodes 8069
Primary superblock at 0, Group descriptors at 1-1
Reserved GDT blocks at 2-474
Block bitmap at 475 (+475), Inode bitmap at 491 (+491)
Inode table at 507-1011 (+507)
24175 free blocks, 8069 free inodes, 2 directories, 8069 unused inodes
Free blocks: 8593-32767
Free inodes: 12-8080
所以在写作之前我做了lseek到8593 * 4096.Now文件系统没有被破坏。
答案 0 :(得分:0)
我按照上面提到的那样修好了。根据dumpe2fs o / p,我在0组以下:
Group 0: (Blocks 0-32767)
Checksum 0x8bba, unused inodes 8069
Primary superblock at 0, Group descriptors at 1-1
Reserved GDT blocks at 2-474
Block bitmap at 475 (+475), Inode bitmap at 491 (+491)
Inode table at 507-1011 (+507)
24175 free blocks, 8069 free inodes, 2 directories, 8069 unused inodes
Free blocks: 8593-32767
Free inodes: 12-8080
所以在写之前我做了lseek
到8593 * 4096.Now文件系统没有被破坏。