RENAME_NOREPLACE
RENAME_NOREPLACE is a Linux renameat2 flag that makes a rename fail with EEXIST instead of replacing a destination name that already exists.
The flag came with the renameat2 system call in Linux 3.15, and the C library wrapper followed in glibc 2.28. A zero flags argument behaves like renameat, while RENAME_NOREPLACE turns the replace-if-present rule of rename into create-only. Combining it with RENAME_EXCHANGE fails with EINVAL. [1]
In the Linux 6.12 virtual file-system code, the destination is looked up after both parent directories are locked for the rename, and an existing destination entry makes the call return EEXIST before the file system is asked to move anything. Even a destination hard link to the source inode is rejected: this check precedes vfs_rename's same-inode no-op. A destination whose last component is . or .. also yields EEXIST under the flag. A separate existence check followed by a plain rename leaves a window in which another process can create the destination name. [3] [1]
Support is per file system: ext4 gained it in Linux 3.15, btrfs, tmpfs and cifs in 3.17, XFS in 4.0, and ext2, vfat, jfs and several others in 4.9. If execution reaches a file system without flag support, it can fail with EINVAL. An EEXIST collision alone does not establish support for moving to an absent name, because the VFS can reject it first. Falling back to a plain rename gives up the no-replace guarantee. [1] [3]
Relations
- related
- Atomic rename, O_TMPFILE, RENAME_EXCHANGE, Directory fsync
Advantages
- The existence check and the rename happen in one system call under the directory locks, so of several processes racing to publish the same name only one succeeds and the others get EEXIST. [3]
Limitations
- An existing destination directory is refused as well, including an empty one that a plain rename of a directory would replace. [3, 1]
- The rename still works only within one mounted file system, and a source and destination on different mounts fail with EXDEV. [1]
Citations
- Linux man-pages: rename(2), renameat2 flags
- Linux v6.12, fs/namei.c: do_renameat2 flag checks and destination lookup.
Further reading
Sources & caveats
- rename is the Linux man-pages rename(2) page, re-read in the renameat2, ERRORS and HISTORY sections.
- linux612-namei is the Linux v6.12 fs/namei.c source, read in do_renameat2.
- The per-file-system version list comes from the manual and was not checked against each file system's code.