3

My company produces Android devices running Linux 3.4 and we have seen that sometimes but rarely a file on the ext4 emmc file system will suddenly become zero-length. Normally I would suspect an improper shutdown while the file was being written but in this situation I know that this file is written once at first boot and never written again; although it is read at every boot.

Is anyone aware of a bug in ext4 code that could cause this? It seems that ext4 is still under quite a bit of active development. Maybe some sys-admins out there have encountered this and are aware of patch or version of the kernel that has a fix?

4
  • 1
    How large are the files? There were some bugs with inline_data, 60 byte or less files, but I don't think 3.4 ext4 supported that option. Commented Dec 26, 2017 at 22:13
  • This happened on a file of 84 bytes size.
    – satur9nine
    Commented Dec 28, 2017 at 2:47
  • Are there many simultaneous writes happening at the same time?
    – Tigger
    Commented Dec 28, 2017 at 6:06
  • For the particular file in question that suffers from the problem there are definitely no simultaneous writes. In general the device is quite busy running various processes simultaneously that may each be writing files of their own. In general it is extremely rare that two processes or threads are writing the same file at the same time.
    – satur9nine
    Commented Dec 28, 2017 at 6:15

1 Answer 1

0

If you write a file, then there might be a time between the opening of the file, and the writing.

When the file open happens with O_CREAT|O_WRONLY|O_TRUNC options then you will have a 0 byte file immediately on calling open. If you lose power between that time and the write flush you will get a 0 byte file. This has nothing to do with the file system. Effectively opening and writing are not a single atomic operations so there will be a gap where you get bad result.

A mitigation is to do the open/write operations in a temp file(in the same file system) and then make a rename which is guaranteed to be atomic. This way you will not have undefined state.

There is also the possibility that your MMC flash is degrading and writes at the hardware level are not correctly done, but this is a bit more unlikely and requires case specific tools.

In general it is extremely rare that two processes or threads are writing the same file at the same time." Never write to the same file concurrently. I cannot think of a good reason to do so, and can think of multiple ways to avoid it.

The details on Android are unknown to me, and i doubt that the different libc would lead to different outcomes than what I described.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .