o-tmpfile · shipping · also Unnamed temporary file

O_TMPFILE

O_TMPFILE is a Linux open flag that creates an unnamed regular file in a directory's filesystem, discarded on last close unless it is linked.

Related Observatory article: Read on hesela.com

Available since Linux 3.11, O_TMPFILE takes a directory path and must be combined with O_RDWR or O_WRONLY, and the mode argument sets permissions as with O_CREAT. Data written is lost when the last descriptor closes unless the file is given a name with linkat(2). Adding O_EXCL prevents later linking, rather than selecting no-overwrite publication. [1]

Support depends on the filesystem. The initial implementation covered ext2, ext3, ext4, UDF, Minix and tmpfs, followed by XFS in Linux 3.15, Btrfs and F2FS in 3.16 and ubifs in 4.9. A kernel without the feature reports EISDIR or ENOENT, and a filesystem without it reports EOPNOTSUPP, so a support probe has to distinguish all three. [1]

An ordinary file unlinked while open is not equivalent. Its descriptor can retain readable data, but linking the /proc/self/fd entry of a deleted file fails with ENOENT, as does linking one created with O_TMPFILE | O_EXCL. O_TMPFILE without O_EXCL is a specific exception to the zero-link restriction, not a general undelete mechanism. After publication the inode is subject to ordinary link limits, for example 65,000 links on ext4 without dir_index. [2]

Relations

related
Hard link, Inode, Directory fsync, RENAME_NOREPLACE, RENAME_EXCHANGE

Advantages

  • Creation is race-free and not subject to symlink attacks, and no unique temporary name has to be devised. [1]
  • A file can be populated and given ownership, mode and extended attributes before any pathname can reach it. [1]

Limitations

  • Linking through AT_EMPTY_PATH requires CAP_DAC_READ_SEARCH, and the unprivileged alternative depends on a mounted proc filesystem. [1, 2]
  • Publication with linkat(2) fails with EEXIST when the target name already exists, so replacing an existing file needs another step. [2]

Citations

  1. Linux man-pages 6.19: open(2), O_TMPFILE, filesystem support and errors (accessed 2026-10-23)
  2. Hesela: unnamed-file publication observation (accessed 2026-10-17)
Sources & caveats
  • open is the Linux man-pages 6.19 open(2) page, re-read for O_TMPFILE flags, linking, filesystem support history and error codes.
  • link is the Linux man-pages 6.19 link(2) page, re-read for the zero-link exception, ENOENT and EEXIST cases and link limits.
  • The local ext4 probe measured zero links before O_TMPFILE publication, preserved inode identity after publication, and rejected relinking an ordinary unlinked file; no power-loss recovery was tested. [3]
  • The rename-exchange entry links here because exchanging names and linking an unnamed file are alternative ways to publish a fully prepared file.

JSON: /data/terms/o-tmpfile.json

Published by Hesela team · Updated

Report a correction · Editorial standards · Reuse guidance

Cite this page
Hesela team. O_TMPFILE. Hesela. 2026-10-23. https://hesela.dev/terms/o-tmpfile/