Post · 3 min read· pp. 499–500

TMPFS

TMPFS

NuttX TMPFS file system is a tiny dynamic RAM based file system.

It can be enabled by adding CONFIG_FS_TMPFS=y to the configuration at build time.

At runtime, simply use mount -t tmpfs /tmp to have a /tmp folder backed by TMPFS, then files and folders can be created under that folder.

TMPFS uses kernel memory by default. CONFIG_FS_HEAPSIZE can reserve an independent heap for filesystem allocations. With CONFIG_FS_HEAPLARGE_ONLY=y, TMPFS file data and directory entry arrays use that heap, while object metadata and names use the kernel heap. Capacity is limited by the selected heaps. See Filesystem Heap.

The df -h command shows TMPFS usage. Its Size column changes as files are added or removed. These allocations consume space in the selected heaps.

Permissions

When CONFIG_FS_PERMISSION is enabled, tmpfs stores per-object owner, group, and mode and enforces them on path operations inside the volume. It also implements the optional mountpt_operations.permission hook. The root directory of a new volume has the mode 01777 (drwxrwxrwt).

Access into the mount from the pseudoFS is gated by the VFS (parent and mountpoint X_OK). See Filesystem Permission Interface.

Timestamps

Every tmpfs file and directory keeps st_atime, st_mtime and st_ctime. They are kept in RAM only and are lost when the volume is unmounted.

  • read() updates st_atime unless the file is opened with O_NOATIME. mmap() also updates it, because tmpfs cannot trap the first access to the mapping.
  • write(), ftruncate(), open() with O_TRUNC and msync() of a shared mapping update st_mtime and st_ctime.
  • Creating, removing or renaming an entry updates st_mtime and st_ctime of the directories involved, and st_ctime of a renamed object.
  • utimensat() and futimens() set st_atime and st_mtime. Any change of the times, mode or owner updates st_ctime.

With CONFIG_FS_LINKS enabled, TMPFS supports link(), symlink(), readlink() and lstat().

Hard links share the same file object, contents and st_ino. The st_nlink field reports the number of directory entries referring to that object. A regular file or symbolic link may have up to LINK_MAX names within one mounted volume. Directories cannot be hard linked. Removing a name does not release the data while another name, open file or mapping still refers to it. statfs() counts a shared file object once, while each name still consumes memory. FIOC_FILEPATH can return any surviving alias; it returns ENOENT after the last name is removed.

A symbolic link stores its target text without changing it. Relative targets are interpreted from the directory containing the name used to access the link, including . and .. within the volume. Absolute targets are resolved through the VFS and must refer to the same TMPFS mount. Following a target on another mount, or a relative .. outside the volume, is not supported and returns EXDEV. Target text is limited to SYMLINK_MAX bytes, and a lookup follows at most SYMLOOP_MAX symbolic links before returning ELOOP.

Absolute targets use the existing VFS path normalization, including its handling of .. before TMPFS resolves symbolic links. This does not provide full POSIX resolution for absolute paths combining links and ...

stat() and normal opens follow symbolic links. lstat() describes the link itself, and readlink() copies its stored target without adding a NUL terminator. O_NOFOLLOW rejects a final symbolic link with ELOOP; O_CREAT | O_EXCL treats an existing link, including a dangling one, as EEXIST. An ordinary O_CREAT can create a missing target when its parent directory exists and permits creation. Unlinking or renaming a link acts on the link itself. lchmod(), lchown() and lutimens() likewise change the link’s metadata.

With permissions enabled, resolution checks search access on every traversed directory, including those in the target path. Creation checks write and search access on the resolved parent directory. Reading the stored target or examining the link’s metadata does not require access to the target.