Is `mount -o remount,ro` guaranteed to fail if there are files open for writing?

I am working on a safe and race-condition-free alternative to umount -l with removable devices:

I’m planning on:

  1. umount --move under a 000 permissions directory so no more files can be opened by absolute path
  2. Interactively kill (or gracefully shutdown) processes with files open for writing
  3. Atomically remount read-only only if step (2) is complete
  4. Interactively kill / close read-only processes that may cause cause issues
  5. Finally have umount succeed

There is a race condition in step (3) where a file with relative path could be opened rw after the last interactive kill and before the mount -o remount,ro.

Is mount -o remount,ro guaranteed to fail if there is any file on the filesystem opened for writing?

The manual page is silent on this, and I’m a bit paranoid after finding out that devices are writable even after blockdev --setro.

Asked By: Tom Hale


Yes. The relevant code is in sb_prepare_remount_readonly (as of Linux 4.0, the code may be organized differently on other versions). The logic is:

  • For each instance of the mount:
    • If that instance is not read-only:
      • Prevent any new writer from registering (MNT_WRITE_HOLD).
      • If there are registered writers, set the error flag (return EBUSY).
  • If there are any files that are removed (inode count = 0) but not yet deleted (still present because the file is open), set the error flag.
  • If the error flag is not set, mark the partition as read-only.
  • For each instance of the mount:
    • Stop preventing writers from registering.

Registered writers are files open for writing as well as ongoing operations that write metadata (mkdir, chmod, etc.). Check out calls to mnt_want_write which is where the count of registered writers is incremented.

The design of the system ensures that a read-only remount is a write registration barrier: if it succeeds then there is no registered writer, in particular there can’t be any file open for writing at the time of the remount operation. After the remount, no file can be opened for writing, so there is still no file that’s open for writing.

Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.