Remix.run Logo
▲ aaronmdjones 21 hours ago

Drives also sometimes lie about the data having been written (they will return success after completely reading the data into their DRAM, before they've finished writing it to their NAND flash for example). This means fsync() will return before the data is actually stored.

USB flash drives in particular are notorious for this, if you've ever wondered why the file copy dialog closed minutes ago but the drive indicator LED is still flashing and the eject (Linux) / safely remove drive (Windows) dialog tells you that the drive isn't ready to be removed yet.

If this is the case, shutting off the power will cause that data to be lost. If you're thinking SSDs have enough power loss protection capacitors for this to not be true, you are sorely mistaken; this protection only guarantees that the SSD's idea of which pages are still erased and what LBAs map where isn't corrupted during wear-levelling for example. It protects the data structures that the drive firmware needs to operate correctly, not your data.

The paranoid might want to journal all of their filesystem writes, metadata and data (ordinarily, on most journalling filesystems, only metadata writes are journalled). Using fsync() then guarantees that either the entire contents of the new temporary file and the file rename operation are in the journal and will be replayed if they don't complete (after power loss or kernel panic for example), or that the file rename operation will not be replayed and the old file will still exist. This reduces write throughput considerably.

A lot of database software is written with the assumption that the underlying storage will lie to it, and does this by itself by default but in a more performant specialised manner, so you don't need to journal all writes in cases like this.

Some filesystems are immune to this problem. For example, ZFS won't process the rename if the filesystem transaction that wrote the temporary file to-be-renamed didn't complete (assuming you used fsync()). It is able to determine this automatically with its data and transaction checksums.

▲moring 9 hours ago | parent | next [-]

Aren't you mixing up two different scenarios here? If the "eject" / "safe remove" action tells you that the drive isn't ready then it DOES signal that back to the OS, otherwise the OS would tell you "it's safe to remove now" while being unsafe to remove. The same information that blocks within the UI might be used to block fsync() as well -- if it doesn't then that is a choice made by the kernel/driver, not the hardware.

▲cowlevel 3 hours ago | parent | prev [-]

> and the eject (Linux) / safely remove drive (Windows) dialog tells you that the drive isn't ready to be removed yet.

If this is happening it's an OS-level problem. If the drive was doing it, the OS would think it had finished.