Linux – How to guarantee writes are physically committed before reported completed

cachelinuxusb-drive

When my system boots, as it probes the external, USB-attached hard drive, I get the message Assuming drive cache: write through in the dmesg output, tagged with the device name of the external drive.

Since I use this drive to store backups, I'd really prefer the system to not make any assumptions about the drive's characteristics. I'm willing to trade write performance for secure knowledge that when something has been written to a file system on the drive in question, it really has been committed to permanent storage and isn't sitting in a cache somewhere (preferably not even on the drive itself, since a power failure will cause both the computer and the drive to lose power immediately).

A possibly complicating factor is that the drive holds a single partition, which is LUKS encrypted. The file system only exists within that LUKS container, like so:

+- physical drive ------------------------+
|                                         |
|  +- partition -----------------------+  |
|  |                                   |  |
|  |  +- LUKS encryption -----------+  |  |
|  |  |                             |  |  |
|  |  |  +- file system ---------+  |  |  |
|  |  |  |                       |  |  |  |
|  |  |  |      actual data      |  |  |  |
|  |  |  |                       |  |  |  |
|  |  |  +-----------------------+  |  |  |
|  |  |                             |  |  |
|  |  +-----------------------------+  |  |
|  |                                   |  |
|  +-----------------------------------+  |
|                                         |
+-----------------------------------------+

Hence my question: How can I make sure that all writes are committed to physical storage before being reported back to the userspace application as completed?

Best Answer

There are two constituent parts here: the disk write cache, and the filesystem cache.

The disk write cache can be disabled using hdparm -W 0 [device]. You can disable filesystem write caching by mounting/remounting it with the sync option. Note that these changes will greatly degrade performance.