RENAME_EXCHANGE
RENAME_EXCHANGE is a Linux renameat2 flag that atomically swaps two existing names, so each pathname then reaches the object the other one reached before.
The flag arrived with the renameat2 system call in Linux 3.15 and glibc 2.28. Both pathnames must exist, and they may be of different types, for example a non-empty directory and a symbolic link. A missing newpath fails with ENOENT, combining the flag with RENAME_NOREPLACE or RENAME_WHITEOUT fails with EINVAL, and names on different mounts fail with EXDEV. [1] [3]
Because neither name is removed, the kernel checks deletion permission on each entry against that entry's own type, and a directory that moves to a different parent also needs write permission so that its .. entry can change. Across two parents, the parent that gains a subdirectory can hit its hard-link limit and fail the call with EMLINK. In Linux v6.12, a normal exchange emits move notifications in both directions. If both names refer to the same inode, vfs_rename returns success before changing the dentries or emitting those notifications. [3]
A sequential test of the flag observed swapped inode identities at the two names while descriptors opened earlier still read their original contents. That sequence does not show atomicity against concurrent lookups or durability across a crash. [2]
Separate opens across an exchange can capture the same original object twice while both names and copied-byte hashes remain correct. Exchanging back restores endpoint identities, not the read set. Intended hard links show why inode counts alone do not establish coverage. A retained-identity guard detects this fixture's mismatch but leaves capture incomplete; it is not a general snapshot protocol. This is not a backup failure-rate estimate or crash test. [4]
Relations
Advantages
- A prepared directory tree can replace a live one while the old tree stays reachable under the other name, so it can be inspected or swapped back. [1]
Limitations
- Exchanging distinct inodes requires file-system support; an unsupported flag can produce EINVAL. A successful same-inode no-op does not test that support because Linux v6.12 returns before calling the file system's rename operation. [1, 3]
- For an exchange between different inodes, the Linux v6.12 VFS rejects swapfiles with EPERM and local mount points with EBUSY. [3]
- An open descriptor retains its object, not frozen contents or atomic acquisition of a multi-object view. [5, 4]
Citations
- Linux man-pages 6.19: rename(2), renameat2 flags, errors and history
- Hesela: exchange observation and limitations
- Linux v6.12, fs/namei.c: do_renameat2 flag checks and vfs_rename exchange handling.
- Hesela: atomic exchange, captured-object identity and hard-link controls
- Linux man-pages: open(2), open file description and pathname rebinding
Sources & caveats
- rename is the Linux man-pages 6.19 rename(2) page, read in the renameat2 flag descriptions, ERRORS and HISTORY sections.
- linux612-namei is the Linux v6.12 fs/namei.c source, read in do_renameat2 and vfs_rename.
- observation is an earlier sequential experiment summarised from its published write-up and was not repeated for this revision.
- Per-file-system support for the flag was not checked because the man page lists support versions only for RENAME_NOREPLACE and RENAME_WHITEOUT.
- readset is a separate native ordered experiment with retained object identities, raw event traces, source hashes and offline replay at https://hesela.dev/research/exchange-readset/exchange-readset.zip; its 56 executions are not a population of devices or backup failures.
- open-description was reviewed in DESCRIPTION; descriptor lifetime is not content immutability or atomic acquisition of multiple objects.