Unofficial Hacker News client - receive Push Notifications on replies.

"This message has been on the screen for almost two hours now." (opens external site)

335 points by firloop 2 days ago | unsung.aresluna.org | 222 comments on HN | Vote on HN (opens Hacker News)

View full thread · View on Hacker News

Comment thread (7)

  1. can't most applications ... write to a temp file and rename? Maybe that's not atomic on Widows?
    Replies (1)
    1. Using fsync() is still required to be correct in power-loss situation, as the filesystem can otherwise reorder the rename to happen before the temp file contents are persisted. It's harder problem than one'd think.
      Replies (2)
      1. 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.

        Replies (2)
        1. 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.
          Replies (1)
          1. The SYNCHRONIZE CACHE command (0x35 / 0x91) instructs the drive to write the volatile cached data to non-volatile cache and/or the recording medium itself. Which one the drive chooses is anyone's guess, but can be influenced by the host beforehand (if the drive supports FUA and the host instructs the drive to WRITE data directly to the medium). This is the mechanism invoked by fsync() after the WRITEs from the host page cache have been issued and is where the drive can lie to the host.

            The START STOP UNIT command (0x1B) is used to instruct the drive to (among other things) unload or eject the medium and/or power down. This is what the Eject / Safely Remove Drive functions invoke. If the drive isn't ready to do so, for example because there is still data sitting in the volatile cache for it to write or a drive self-test is still in progress, it will tell you so.

            These are SCSI commands, which are also used in drives that aren't electrically or physically SCSI. These include SATA, SAS, and USB Mass Storage (both MSC and UAS). That's why they all show up as sdx devices in Linux; sd stands for SCSI Disk (see drivers/scsi/sd.c), and you can't use any of these types of drives without SCSI support enabled in the kernel configuration (CONFIG_SCSI=y).

        2. > 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.

      2. It was a lot simpler before filesystems decided that reordering writes and renames should be allowed.