Hello,
I came across an interesting problem where a write immediately followed by
a fchmod results in file corruption. We are using Yaffs2, on a board with
2KB Nand flash. I reported a very similiar problem about two years ago
when using Yaffs1, the fix was to change nShortOpCache = 0.
http://lists.aleph1.co.uk/pipermail/yaffs/2005q1/000941.html
Below is a small program to reproduce the problem. I found after running
the program, a file named "file" is written to disk. After unmounting
and remounting the yaffs2 filesystem the file "file" has been corrupted.
You can use the md5sum utility to verify this as well as hexedit. In the
example provided the first 0x757FF bytes are correct, all data
after this byte is now 0. Also I found by syncing the file or sleeping
prior to calling fchmod avoids the problem...
#include <fcntl.h>
#include <assert.h>
#include <sys/stat.h>
#include <unistd.h>
#define BLOCK_SIZE 16384
#define FILE_SIZE 483279
int main(void)
{
unsigned char data[BLOCK_SIZE];
int file_size = FILE_SIZE, bytes, i, j = 0;
int fd = open("file", O_WRONLY|O_CREAT|O_TRUNC, 0600);
assert(fd != -1);
do
{
for(i=0;i<BLOCK_SIZE;i++)
data[i] = j;
j++;
if(file_size < BLOCK_SIZE)
bytes = write(fd, data, file_size);
else
bytes = write(fd, data, BLOCK_SIZE);
file_size = file_size - bytes;
}while(file_size);
//sleep(30);
//fsync(fd);
fchmod(fd, 0644);
close(fd);
return 0;
}
//Eddie