[Yaffs] mmap, power-off, and data is lost

Top Page
Attachments:
Message as email
+ (text/plain)
+ mmap_test.c (text/x-csrc)
Delete this message
Reply to this message
Author: Louis JANG
Date:  
To: yaffs
Subject: [Yaffs] mmap, power-off, and data is lost
Hi all,

I'm using yaffs in several mobile platform, and using mmap interface to
read/write contents of file in yaffs. it works well in normal situation,
but it losts data when I turn off its power without unmount.

Please look at the attached sample code, and the following is its result.

# ./a.out
# ls -l
-rwxr-xr-x 1 0 0 1024 Apr 7 14:37 mmap_test
...
# sync

... power off without unmount, and power-on

# ls -l
-rwxr-xr-x 1 0 0 0 Apr 7 14:37 mmap_test

I also waited for about 10 seconds before power off, but data was lost also.

The above problem is not happened if I read mmap_test file before
power-off(I used cat). and it's not happened if I unmount before. I also
have tested the above problem with write api instead of mmap, it was not
happened in this case.

Regards,
Louis JANG

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>

int
main()
{
    const char *dbname = "/root/mmap_test";
    int fd = open(dbname, O_RDWR|O_TRUNC|O_CREAT, 0777);
    if (fd < 0) {
    perror("open");
    return -1;
    }


    int len = 1024;
    if (ftruncate(fd, len) < 0) {
    perror("ftruncate");
    return -1;
    }


    void *mmap_ptr = mmap(0, len, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
    if (mmap_ptr == MAP_FAILED) {
    perror("mmap");
    close(fd);
    return -1;
    }


    memset(mmap_ptr, '1', len);
    munmap(mmap_ptr, len);
    close(fd);
    return 0;
}