Remix.run Logo
▲ londons_explore 2 days ago

Computers have had journalling filesystems for 25 years now. They should survive a power failure at any point...

So, use that ability! Don't shut anything down gradually - just pull the plug.

In a similar vein, don't worry about properly destructing and unloading everything when shutting down a program... Just call exit(0) and quit.

▲wavemode 2 days ago | parent | next [-]

Journalling prevents corruption of the filesystem itself, but it does nothing to prevent individual programs from having their files corrupted. (Maybe all programs ought to use sqlite as their data format, but alas.)

▲cozzyd a day ago | parent [-]

can't most applications ... write to a temp file and rename? Maybe that's not atomic on Widows?

▲dezgeg a day ago | parent [-]

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.

▲aaronmdjones a day ago | parent | next [-]

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

▲aaronmdjones an hour ago | parent [-]

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

▲cowlevel 11 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.

▲account42 14 hours ago | parent | prev [-]

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

▲kelnos 2 days ago | parent | prev [-]

Journaling saves you from catastrophic filesystem corruption, but it doesn't make sure data that's still in caches has been fully saved to disk. Applications may not have saved their work, and even if they have, the OS may not have written everything out yet.

▲SoftTalker a day ago | parent [-]

Any application that depends on cached state must be able to start up in a sane way if that state is corrupted. If it can't, it's not worth using.

▲lxgr a day ago | parent | next [-]

Consistency isn’t everything. Sometimes people also just don’t want to lose their changes, i.e. durability.

▲Ulkopuolinen a day ago | parent | prev [-]

You edit a file in a text editor. You save the file. You close the text editor. You cut power.

The file is lost because it was not written to the disk.

How is this a sign that the text editor is not worth using?

▲cachvico a day ago | parent | next [-]

That's got nothing to do with cached data or corruption.

▲Ulkopuolinen 15 hours ago | parent [-]

File not being stored to disk after the application saves it has everything to do with cached data. It's exactly what this thread is about.

▲SoftTalker 11 hours ago | parent [-]

Application should call sync() or equivalent and get a positive result before reporting to the user that the file was saved. If the OS hasn't actually written the bits to disk yet, or the disk controller has cached them but not written them, not much the application can do.

But all of that is beside the point. What I said was that the application should do something sane if the cached data is missing or corrupt. Not that it should never lose data; that's impossible for any app to guarantee.

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

They did say cached data