3

I have a single board appliance that runs Debian 10 on a chunk of flash. UBIFS is used, and is split into two volumes: an ro roots, and an rw /var. I have found that under power cycling/reset conditions, that I can end up with 0 byte files. I keep my "settings" in /var/opt/myApp. Changing the mount option of /var to include sync seems to make those incidents go away.

I know the usual advice is that async is preferred over sync, but it is usually caveated with "usually, but not always" with little explanation what the exceptions might be.

The alternate solution, would be to modify any and all call sites where I write data to disk, to not only flush on file close, but sync as well (I do a lot of it with python). From a coding/completeness sake, mounting as sync seemed both less work, and avoided me missing adding the sync guards at places, iow it's universal.

Additionally, I allow the appliance to save data to usb thumb drives. I think I should mount those sync too, to reduce loss when they are yanked out right after data is written to them.

Is this a suitably exceptional configuration to justify using sync? Or should I use the alternate solution?

4
  • At this point, no. I've toyed with bending the hardware guys to make the "reset" switch be soft and detectable, but right now it's just a power shunt. Which at times, is what you really want. Even if I did at the board level, the question remains germaine for thumb drives which can be unceremonious yanked out. Aug 29 '19 at 18:58
  • These are great comments @drewbenn, but they deviate from the real question. Tons of appliances have the same problem. To reset my AppleTV, I yank the power cord. Same with wifi routers. And countless other things. I can strive more what you describe (and I do), but the question of "to sync or not to sync" remains. Aug 29 '19 at 20:17
  • Most of the info I find on yaffs2 vs UBIFS is pretty dated. Yaffs2 appears to be dual licensed? Aug 29 '19 at 21:46
  • Even with sync option or with syncing everywhere in code, It's just more unlikely for empty files to disappear. I don't think you can avoid it that way. You can either teach your application to deal with those files or write to temporary files and rename them after sync -- this way you can guarantee the file is complete or inexistent, no matter when the system was shot.
    – Philippos
    Sep 5 '19 at 10:58
1

Here is my opinion: Sorry for verbosity in advance.

When we are specifically talking about ubifs we should always either sync / similar options.

ubifs supports write-back caching

That means changes written on the files do not go to flash directly. They are stored on the page cache first and later written to flash. (Read more on write buffers for NAND flashes in UBIFS)

This improves file system performance by reducing number of writes.

Note that this is asynchronous behavior of fs.

As you said in the question, when you mount UBIFS with -sync option, it will make the file system synchronous (changes are written to flash every time) however at the cost of performance drop.

If you are working with asynchronous file systems like ubifs, then the onus of making sure that writes are written to flash is on application developers. Here is the what man page of write(2) says:

$ man 2 write
NOTES
   A  successful return from write() does not make any guarantee that data
   has been committed to disk.  In fact, on some buggy implementations, it
   does  not  even guarantee that space has successfully been reserved for
   the data.  The only way to be sure is to call fsync(2)  after  you  are
   done writing all your data.

Using

sync - synchronizes whole fs. Might not be optimal

fsync - Mostly does the job

fdatasync - Only data changes are flushed and not the metadata (permissions). More optimal than fsync maybe (not sure)

Also read Good Read about fsync

So at the end, your options:

  1. mount with 'sync' - with performance hit
  2. improve the application using above sync options.
  3. handle 0 byte files in applications
  4. creating temp files and renaming them later

Last thought, might wanna switch to synchronous fs like jffs2 (not fully synchronous if using NAND flash though). I know this is not the answer to your question but eh, wrote so much might as well write this....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.