18c2ecf20Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci=====
48c2ecf20Sopenharmony_ciTmpfs
58c2ecf20Sopenharmony_ci=====
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ciTmpfs is a file system which keeps all files in virtual memory.
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ciEverything in tmpfs is temporary in the sense that no files will be
118c2ecf20Sopenharmony_cicreated on your hard drive. If you unmount a tmpfs instance,
128c2ecf20Sopenharmony_cieverything stored therein is lost.
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_citmpfs puts everything into the kernel internal caches and grows and
158c2ecf20Sopenharmony_cishrinks to accommodate the files it contains and is able to swap
168c2ecf20Sopenharmony_ciunneeded pages out to swap space. It has maximum size limits which can
178c2ecf20Sopenharmony_cibe adjusted on the fly via 'mount -o remount ...'
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ciIf you compare it to ramfs (which was the template to create tmpfs)
208c2ecf20Sopenharmony_ciyou gain swapping and limit checking. Another similar thing is the RAM
218c2ecf20Sopenharmony_cidisk (/dev/ram*), which simulates a fixed size hard disk in physical
228c2ecf20Sopenharmony_ciRAM, where you have to create an ordinary filesystem on top. Ramdisks
238c2ecf20Sopenharmony_cicannot swap and you do not have the possibility to resize them.
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ciSince tmpfs lives completely in the page cache and on swap, all tmpfs
268c2ecf20Sopenharmony_cipages will be shown as "Shmem" in /proc/meminfo and "Shared" in
278c2ecf20Sopenharmony_cifree(1). Notice that these counters also include shared memory
288c2ecf20Sopenharmony_ci(shmem, see ipcs(1)). The most reliable way to get the count is
298c2ecf20Sopenharmony_ciusing df(1) and du(1).
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_citmpfs has the following uses:
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci1) There is always a kernel internal mount which you will not see at
348c2ecf20Sopenharmony_ci   all. This is used for shared anonymous mappings and SYSV shared
358c2ecf20Sopenharmony_ci   memory.
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci   This mount does not depend on CONFIG_TMPFS. If CONFIG_TMPFS is not
388c2ecf20Sopenharmony_ci   set, the user visible part of tmpfs is not build. But the internal
398c2ecf20Sopenharmony_ci   mechanisms are always present.
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci2) glibc 2.2 and above expects tmpfs to be mounted at /dev/shm for
428c2ecf20Sopenharmony_ci   POSIX shared memory (shm_open, shm_unlink). Adding the following
438c2ecf20Sopenharmony_ci   line to /etc/fstab should take care of this::
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	tmpfs	/dev/shm	tmpfs	defaults	0 0
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci   Remember to create the directory that you intend to mount tmpfs on
488c2ecf20Sopenharmony_ci   if necessary.
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci   This mount is _not_ needed for SYSV shared memory. The internal
518c2ecf20Sopenharmony_ci   mount is used for that. (In the 2.3 kernel versions it was
528c2ecf20Sopenharmony_ci   necessary to mount the predecessor of tmpfs (shm fs) to use SYSV
538c2ecf20Sopenharmony_ci   shared memory)
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci3) Some people (including me) find it very convenient to mount it
568c2ecf20Sopenharmony_ci   e.g. on /tmp and /var/tmp and have a big swap partition. And now
578c2ecf20Sopenharmony_ci   loop mounts of tmpfs files do work, so mkinitrd shipped by most
588c2ecf20Sopenharmony_ci   distributions should succeed with a tmpfs /tmp.
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci4) And probably a lot more I do not know about :-)
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_citmpfs has three mount options for sizing:
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci=========  ============================================================
668c2ecf20Sopenharmony_cisize       The limit of allocated bytes for this tmpfs instance. The
678c2ecf20Sopenharmony_ci           default is half of your physical RAM without swap. If you
688c2ecf20Sopenharmony_ci           oversize your tmpfs instances the machine will deadlock
698c2ecf20Sopenharmony_ci           since the OOM handler will not be able to free that memory.
708c2ecf20Sopenharmony_cinr_blocks  The same as size, but in blocks of PAGE_SIZE.
718c2ecf20Sopenharmony_cinr_inodes  The maximum number of inodes for this instance. The default
728c2ecf20Sopenharmony_ci           is half of the number of your physical RAM pages, or (on a
738c2ecf20Sopenharmony_ci           machine with highmem) the number of lowmem RAM pages,
748c2ecf20Sopenharmony_ci           whichever is the lower.
758c2ecf20Sopenharmony_ci=========  ============================================================
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ciThese parameters accept a suffix k, m or g for kilo, mega and giga and
788c2ecf20Sopenharmony_cican be changed on remount.  The size parameter also accepts a suffix %
798c2ecf20Sopenharmony_cito limit this tmpfs instance to that percentage of your physical RAM:
808c2ecf20Sopenharmony_cithe default, when neither size nor nr_blocks is specified, is size=50%
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ciIf nr_blocks=0 (or size=0), blocks will not be limited in that instance;
838c2ecf20Sopenharmony_ciif nr_inodes=0, inodes will not be limited.  It is generally unwise to
848c2ecf20Sopenharmony_cimount with such options, since it allows any user with write access to
858c2ecf20Sopenharmony_ciuse up all the memory on the machine; but enhances the scalability of
868c2ecf20Sopenharmony_cithat instance in a system with many cpus making intensive use of it.
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_citmpfs has a mount option to set the NUMA memory allocation policy for
908c2ecf20Sopenharmony_ciall files in that instance (if CONFIG_NUMA is enabled) - which can be
918c2ecf20Sopenharmony_ciadjusted on the fly via 'mount -o remount ...'
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci======================== ==============================================
948c2ecf20Sopenharmony_cimpol=default             use the process allocation policy
958c2ecf20Sopenharmony_ci                         (see set_mempolicy(2))
968c2ecf20Sopenharmony_cimpol=prefer:Node         prefers to allocate memory from the given Node
978c2ecf20Sopenharmony_cimpol=bind:NodeList       allocates memory only from nodes in NodeList
988c2ecf20Sopenharmony_cimpol=interleave          prefers to allocate from each node in turn
998c2ecf20Sopenharmony_cimpol=interleave:NodeList allocates from each node of NodeList in turn
1008c2ecf20Sopenharmony_cimpol=local		 prefers to allocate memory from the local node
1018c2ecf20Sopenharmony_ci======================== ==============================================
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ciNodeList format is a comma-separated list of decimal numbers and ranges,
1048c2ecf20Sopenharmony_cia range being two hyphen-separated decimal numbers, the smallest and
1058c2ecf20Sopenharmony_cilargest node numbers in the range.  For example, mpol=bind:0-3,5,7,9-15
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ciA memory policy with a valid NodeList will be saved, as specified, for
1088c2ecf20Sopenharmony_ciuse at file creation time.  When a task allocates a file in the file
1098c2ecf20Sopenharmony_cisystem, the mount option memory policy will be applied with a NodeList,
1108c2ecf20Sopenharmony_ciif any, modified by the calling task's cpuset constraints
1118c2ecf20Sopenharmony_ci[See Documentation/admin-guide/cgroup-v1/cpusets.rst] and any optional flags,
1128c2ecf20Sopenharmony_cilisted below.  If the resulting NodeLists is the empty set, the effective
1138c2ecf20Sopenharmony_cimemory policy for the file will revert to "default" policy.
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ciNUMA memory allocation policies have optional flags that can be used in
1168c2ecf20Sopenharmony_ciconjunction with their modes.  These optional flags can be specified
1178c2ecf20Sopenharmony_ciwhen tmpfs is mounted by appending them to the mode before the NodeList.
1188c2ecf20Sopenharmony_ciSee Documentation/admin-guide/mm/numa_memory_policy.rst for a list of
1198c2ecf20Sopenharmony_ciall available memory allocation policy mode flags and their effect on
1208c2ecf20Sopenharmony_cimemory policy.
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci::
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	=static		is equivalent to	MPOL_F_STATIC_NODES
1258c2ecf20Sopenharmony_ci	=relative	is equivalent to	MPOL_F_RELATIVE_NODES
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ciFor example, mpol=bind=static:NodeList, is the equivalent of an
1288c2ecf20Sopenharmony_ciallocation policy of MPOL_BIND | MPOL_F_STATIC_NODES.
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ciNote that trying to mount a tmpfs with an mpol option will fail if the
1318c2ecf20Sopenharmony_cirunning kernel does not support NUMA; and will fail if its nodelist
1328c2ecf20Sopenharmony_cispecifies a node which is not online.  If your system relies on that
1338c2ecf20Sopenharmony_citmpfs being mounted, but from time to time runs a kernel built without
1348c2ecf20Sopenharmony_ciNUMA capability (perhaps a safe recovery kernel), or with fewer nodes
1358c2ecf20Sopenharmony_cionline, then it is advisable to omit the mpol option from automatic
1368c2ecf20Sopenharmony_cimount options.  It can be added later, when the tmpfs is already mounted
1378c2ecf20Sopenharmony_cion MountPoint, by 'mount -o remount,mpol=Policy:NodeList MountPoint'.
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ciTo specify the initial root directory you can use the following mount
1418c2ecf20Sopenharmony_cioptions:
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci====	==================================
1448c2ecf20Sopenharmony_cimode	The permissions as an octal number
1458c2ecf20Sopenharmony_ciuid	The user id
1468c2ecf20Sopenharmony_cigid	The group id
1478c2ecf20Sopenharmony_ci====	==================================
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ciThese options do not have any effect on remount. You can change these
1508c2ecf20Sopenharmony_ciparameters with chmod(1), chown(1) and chgrp(1) on a mounted filesystem.
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_citmpfs has a mount option to select whether it will wrap at 32- or 64-bit inode
1548c2ecf20Sopenharmony_cinumbers:
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci=======   ========================
1578c2ecf20Sopenharmony_ciinode64   Use 64-bit inode numbers
1588c2ecf20Sopenharmony_ciinode32   Use 32-bit inode numbers
1598c2ecf20Sopenharmony_ci=======   ========================
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ciOn a 32-bit kernel, inode32 is implicit, and inode64 is refused at mount time.
1628c2ecf20Sopenharmony_ciOn a 64-bit kernel, CONFIG_TMPFS_INODE64 sets the default.  inode64 avoids the
1638c2ecf20Sopenharmony_cipossibility of multiple files with the same inode number on a single device;
1648c2ecf20Sopenharmony_cibut risks glibc failing with EOVERFLOW once 33-bit inode numbers are reached -
1658c2ecf20Sopenharmony_ciif a long-lived tmpfs is accessed by 32-bit applications so ancient that
1668c2ecf20Sopenharmony_ciopening a file larger than 2GiB fails with EINVAL.
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ciSo 'mount -t tmpfs -o size=10G,nr_inodes=10k,mode=700 tmpfs /mytmpfs'
1708c2ecf20Sopenharmony_ciwill give you tmpfs instance on /mytmpfs which can allocate 10GB
1718c2ecf20Sopenharmony_ciRAM/SWAP in 10240 inodes and it is only accessible by root.
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci:Author:
1758c2ecf20Sopenharmony_ci   Christoph Rohland <cr@sap.com>, 1.12.01
1768c2ecf20Sopenharmony_ci:Updated:
1778c2ecf20Sopenharmony_ci   Hugh Dickins, 4 June 2007
1788c2ecf20Sopenharmony_ci:Updated:
1798c2ecf20Sopenharmony_ci   KOSAKI Motohiro, 16 Mar 2010
1808c2ecf20Sopenharmony_ci:Updated:
1818c2ecf20Sopenharmony_ci   Chris Down, 13 July 2020
182