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

Reply to
Sarick
Loading thread data ...

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
 Click to see the full signature
Reply to
Tauno Voipio

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

Reply to
Amit Limaye

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

Reply to
Sarick

'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

Reply to
Mile Blenton

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

Reply to
Michael Schnell

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
Amit Limaye

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.