write() has returned, but data is not completely write into flash? - Embedded

This is a discussion on write() has returned, but data is not completely write into flash? - Embedded ; Hi, I met a problem that, I'm trying to write a big file into my flash. Actually I write the file in many rounds, which means via many write() calls with each time a partial piece. After all the write() ...

+ Reply to Thread
Results 1 to 7 of 7

Thread: write() has returned, but data is not completely write into flash?

  1. write() has returned, but data is not completely write into flash?

    Hi,

    I met a problem that, I'm trying to write a big
    file into my flash. Actually I write the file
    in many rounds, which means via many write() calls
    with each time a partial piece.
    After all the write() returned and the returned length
    is valid, I reboot my device immediately. But after rebooting, I
    found the file written in flash is not complete, its size
    is smaller than that I wrote.
    I was wondering whether the write() has returned but does not
    mean the real buffer is written done. So the reboot would cause
    the file imcomplete. Is this correct?
    Actually I tried with system call "sync" before reboot, but fail.
    Can anyone help me?

    Thanks
    sarick


  2. Re: write() has returned, but data is not completely write into flash?

    Sarick wrote:
    > Hi,
    >
    > I met a problem that, I'm trying to write a big
    > file into my flash. Actually I write the file
    > in many rounds, which means via many write() calls
    > with each time a partial piece.
    > After all the write() returned and the returned length
    > is valid, I reboot my device immediately. But after rebooting, I
    > found the file written in flash is not complete, its size
    > is smaller than that I wrote.
    > I was wondering whether the write() has returned but does not
    > mean the real buffer is written done. So the reboot would cause
    > the file imcomplete. Is this correct?
    > Actually I tried with system call "sync" before reboot, but fail.
    > Can anyone help me?
    >
    > Thanks
    > sarick
    >


    You need to sync() the file systems before reboot.
    There are an utility and a system call, both of
    the same name: sync.

    --

    Tauno Voipio
    tauno voipio (at) iki fi


  3. Re: write() has returned, but data is not completely write into flash?

    if u check the reboot scripts for many systems the sync() system call
    is called multiple times before reboot since the sync system call is
    just a hint to synchronize the the disc and does not initiate the
    process immediately


  4. Re: write() has returned, but data is not completely write into flash?


    Amit Limaye wrote:
    > if u check the reboot scripts for many systems the sync() system call
    > is called multiple times before reboot since the sync system call is
    > just a hint to synchronize the the disc and does not initiate the
    > process immediately



    Do you mean that I should try issue multiple sync(), or sync() does
    not help my case??
    Actually I tried both sync() and fsync(), but fail.
    Also I add an O_SYNC flag when opening the file descriptor with open(),
    but still doesn't fix the problem.
    Any suggestions?

    Thanks
    Sarick


  5. Re: write() has returned, but data is not completely write into flash?

    Sarick wrote:

    > I was wondering whether the write() has returned but does not
    > mean the real buffer is written done. So the reboot would cause
    > the file imcomplete. Is this correct?
    > Actually I tried with system call "sync" before reboot, but fail.
    > Can anyone help me?
    >


    'man 2 sync' gives (among other things):

    "
    BUGS
    According to the standard specification (e.g., SVID), sync() schedules
    the writes, but may return before the actual writing is done. However,
    since version 1.3.20 Linux does actually wait. (This still does not
    guarantee data integrity: modern disks have large caches.)
    "


    It's probably not the best practice to assume sync() will sync everything
    down. Try using multiple syncs with a 1sec or more delay before you actualy
    reboot the machine.

    Also, did you try it on another flash disk?
    Does data loss happen always or sometimes it goes ok?

    Regards,
    Mario


  6. Re: write() has returned, but data is not completely write into flash?

    > I reboot my device immediately. But after rebooting, I
    > found the file written in flash is not complete


    In addition to file system cacheing problems that can be solved via
    "sync", the flash system itself (e.g. a Compact Flash card) might have a
    reset feature (pin or command).

    The rebooting might trigger this reset and thus the Flash system might
    abort the write cycle. This might result in bad data, file system error
    or even in destruction of the internal bad block lists of the Flash
    system and same would become completely unusable.

    -Michael

  7. Re: write() has returned, but data is not completely write into flash?

    Sarick i dont know whcih kernel you are usung but the O_SYNC flag is
    not implemented in the older kernels also as someone suggested before
    multiple sync would help with a delay before trying to reboot.


+ Reply to Thread