Documentation/translations/zh_CN/filesystems/tmpfs.rst
Source file repositories/reference/linux-study-clean/Documentation/translations/zh_CN/filesystems/tmpfs.rst
File Facts
- System
- Linux kernel
- Corpus path
Documentation/translations/zh_CN/filesystems/tmpfs.rst- Extension
.rst- Size
- 6677 bytes
- Lines
- 147
- Domain
- Support Tooling And Documentation
- Bucket
- Documentation
- Inferred role
- Support Tooling And Documentation: documentation
- Status
- atlas-only
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
Dependency Surface
- No C-style include directives detected by the generator.
Detected Declarations
- No top-level syscall, struct, function, initcall, or export declaration detected by the generator.
Annotated Snippet
.. SPDX-License-Identifier: GPL-2.0
.. include:: ../disclaimer-zh_CN.rst
:Original: Documentation/filesystems/tmpfs.rst
translated by Wang Qing<wangqing@vivo.com>
=====
Tmpfs
=====
Tmpfs是一个将所有文件都保存在虚拟内存中的文件系统。
tmpfs中的所有内容都是临时的,也就是说没有任何文件会在硬盘上创建。
如果卸载tmpfs实例,所有保存在其中的文件都会丢失。
tmpfs将所有文件保存在内核缓存中,随着文件内容增长或缩小可以将不需要的
页面swap出去。它具有最大限制,可以通过“mount -o remount ...”调整。
和ramfs(创建tmpfs的模板)相比,tmpfs包含交换和限制检查。和tmpfs相似的另
一个东西是RAM磁盘(/dev/ram*),可以在物理RAM中模拟固定大小的硬盘,并在
此之上创建一个普通的文件系统。Ramdisks无法swap,因此无法调整它们的大小。
由于tmpfs完全保存于页面缓存和swap中,因此所有tmpfs页面将在/proc/meminfo
中显示为“Shmem”,而在free(1)中显示为“Shared”。请注意,这些计数还包括
共享内存(shmem,请参阅ipcs(1))。获得计数的最可靠方法是使用df(1)和du(1)。
tmpfs具有以下用途:
1) 内核总有一个无法看到的内部挂载,用于共享匿名映射和SYSV共享内存。
挂载不依赖于CONFIG_TMPFS。如果CONFIG_TMPFS未设置,tmpfs对用户不可见。
但是内部机制始终存在。
2) glibc 2.2及更高版本期望将tmpfs挂载在/dev/shm上以用于POSIX共享内存
(shm_open,shm_unlink)。添加内容到/etc/fstab应注意如下:
tmpfs /dev/shm tmpfs defaults 0 0
使用时需要记住创建挂载tmpfs的目录。
SYSV共享内存无需挂载,内部已默认支持。(在2.3内核版本中,必须挂载
tmpfs的前身(shm fs)才能使用SYSV共享内存)
3) 很多人(包括我)都觉的在/tmp和/var/tmp上挂载非常方便,并具有较大的
swap分区。目前循环挂载tmpfs可以正常工作,所以大多数发布都应当可以
使用mkinitrd通过/tmp访问/tmp。
4) 也许还有更多我不知道的地方:-)
tmpfs有三个用于调整大小的挂载选项:
========= ===========================================================
size tmpfs实例分配的字节数限制。默认值是不swap时物理RAM的一半。
如果tmpfs实例过大,机器将死锁,因为OOM处理将无法释放该内存。
nr_blocks 与size相同,但以PAGE_SIZE为单位。
nr_inodes tmpfs实例的最大inode个数。默认值是物理内存页数的一半,或者
(有高端内存的机器)低端内存RAM的页数,二者以较低者为准。
========= ===========================================================
这些参数接受后缀k,m或g表示千,兆和千兆字节,可以在remount时更改。
size参数也接受后缀%用来限制tmpfs实例占用物理RAM的百分比:
未指定size或nr_blocks时,默认值为size=50%
如果nr_blocks=0(或size=0),block个数将不受限制;如果nr_inodes=0,
inode个数将不受限制。这样挂载通常是不明智的,因为它允许任何具有写权限的
用户通过访问tmpfs耗尽机器上的所有内存;但同时这样做也会增强在多个CPU的
场景下的访问。
Annotation
- Atlas domain: Support Tooling And Documentation / Documentation.
- Implementation status: atlas-only.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.