How to Solve Zero Length File Problem in Linux's Ext4 File System?

The ext4 (fourth extended) is a journaled file system of Linux that has many benefits compared to its predecessors, such as support for backward compatibility, large file system, delayed allocation, etc. However, the delayed allocation feature sometimes poses problems for the Linux system. The Linux system based on ext4 file system may encounter inconsistencies in the files in case of a power surge. To overcome this behavior, you should take appropriate corrective measures. However, if no method employed by you is successful then you should use a third-party Linux recovery software to do the needful.

Consider a scenario wherein you are working on a Linux system. Suddenly, there is a power loss and the system shuts down abruptly. When the power is restored, you switch on the system and find that the files that you created just before the crash are showing zero bytes.

Cause:

The most possible cause for such behavior is the delayed allocation feature. What happens is when a file is closed after creation, the data blocks are allocated for the file after a minute or so. Because of this, the metadata of the modified file will show zero bytes as its size. The data allocation is done after the delayed allocation is over. Hence, in the case of a power surge the new file is considered as an empty file.

Resolution:

To resolve the issue, you can use the following methods:

  • Mounting the file system using "data write-back"

  • Using three patches designed for ext4 file system: The three patches (bf1b69c0, f32b730a, and 8411e347) enforce any file with delayed allocation will be allocated immediately when a file is created.

  • Mount the ext4 file system with the nodealloc mount option

  • Mount the system with the data=alloc-on-commit option: This would ensure that all blocks that had delayed allocation would be allocated and moved to disk before a commit takes place.

However, if you still are not able to recover the lost files then you should use a third-party Linux recovery software to perform data recovery for Linux system. Such read-only tools are able to recover lost data without overwriting the original data.