How filesystem atomically writes a block to disk

filesystemskernel

Block is an abstraction provided by filesystem, block size is integer multiples of disk sector size. Suppose a filesystem uses 4K as its block size, and the disk sector size is 512B, when the filesystem issues a write request to the disk driver, how to atomically write the entire 4K block to disk(avoid partial write)?
I want to know how modern kernel addresses this problem, but I don't want to dive into Linux codebase to find the answer. Any help will be appreciate.

Best Answer

A disk should grant that a sector is written atomically. The sector size was 512 bytes and today is typically 4096 bytes for larger disks.

In order to get no problem from partially written "blocks", it is important to write everything in a special order.

Note that the only reason why there could be a partially written part in the filesystem is a power outage or something similar.

The method is:

  • First write all file content and verify that this worked

  • Then write the meta data and make sure that all data structures in the meta data fit in a single disk sector and do not span a sector boundary. This is e.g. important for variable length file names as directory content.

Related Question