Linux 文件系统架构与源码总览

Linux 文件系统架构与源码总览

1. 范围

fs/ 同时包含:

  1. VFS:所有文件系统共享的对象模型和系统调用;
  2. 文件 I/O 基础设施:页缓存、写回、direct I/O、iomap、pipe、poll;
  3. 元数据基础设施:ACL、xattr、quota、locks、notify;
  4. 挂载和 namespace;
  5. 具体本地、内存、叠加、用户态及网络文件系统。

2. 分层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
userspace
open/read/write/stat/mmap/fsync/mount/ioctl/poll
|
syscall entry: fs/*.c |
v
VFS: file_system_type / super_block / inode / dentry / file
|
operation tables:
super_operations / inode_operations / dentry_operations
file_operations / address_space_operations / export_operations
|
ext4/f2fs/xfs/btrfs/erofs/overlay/fuse/nfs/smb/ceph/...
|
page cache / block layer / MTD / network / userspace daemon

VFS 不把所有文件系统变成相同实现,而是统一对象生命周期、权限、路径和调用约定。

3. 核心对象

对象 含义
file_system_type 文件系统类型注册项
fs_context 一次挂载配置过程
super_block 已挂载文件系统实例
vfsmount/mount namespace 中挂载关系
inode 文件对象元数据和操作
dentry 名称到 inode 的缓存关系
path dentry + mount
file 一次打开实例,绑定 flags/position/cred
address_space inode 页缓存与 I/O 操作
files_struct 进程 fdtable
fs_struct 进程 root/pwd

4. 打开文件

fdtablefilesystemdcachenamei.copen.cuserspacefdtablefilesystemdcachenamei.copen.cuserspaceopenat2()get_unused_fd_flags()do_filp_open()RCU path walk / lookup_fasthit or missinode_operations.lookup/createfile_operations.openfd_install()fd

路径解析优先走 dcache 的 RCU-walk,遇到需要阻塞、重验证或复杂符号链接时退化为 ref-walk。

5. 读写

1
2
3
4
5
6
7
8
read/write syscall
-> fdget_pos()
-> vfs_read/vfs_write
-> rw_verify_area + security hooks
-> file->f_op->read_iter/write_iter
-> buffered: page cache/filemap
-> direct: filesystem DIO/iomap
-> block/network/userspace backend

VFS 负责通用检查和统计;数据一致性、extent、日志和真实 I/O 由具体文件系统实现。

6. 挂载

mount(2) 与新 API 并存。新流程:

1
2
3
4
fsopen(type) -> fsconfig(parameters) -> fsmount()
-> vfs_get_tree()
-> filesystem get_tree/fill_super
-> move_mount() attach into namespace

namespace.c 维护 mount tree、传播、bind/move/remount;super.c 管理 superblock 创建和销毁。

7. 缓存

  • dcache:路径组件和 negative dentry;
  • inode cache:内存 inode;
  • page cache:文件数据 folio;
  • buffer_head:传统块映射和 metadata buffer;
  • filesystem-specific metadata cache;
  • fscache/cachefiles:网络文件系统本地缓存。

缓存不是持久性保证。fsync()、日志提交、barrier/FUA 和设备缓存共同决定落盘语义。

8. 并发

关键同步包括:

  • rename_lockmount_lock seqlock;
  • dentry d_lock
  • inode i_rwsemi_lock
  • superblock s_umount
  • fdtable RCU;
  • pathwalk RCU;
  • page/folio lock、mapping i_pages xarray;
  • filesystem transaction/journal locks。

锁顺序必须结合 Documentation/filesystems/ 和 lockdep,不可只按对象层级猜测。

9. RK3588

典型组合:

  • eMMC/SD:ext4 或 f2fs;
  • NVMe:ext4/xfs/btrfs(按产品需求);
  • 只读根文件系统:EROFS/SquashFS + OverlayFS;
  • raw NAND:UBI + UBIFS;
  • Android/容器:tmpfs、proc、sysfs、overlay;
  • NAS/集群:NFS/SMB/Ceph。

性能同时受文件系统、块层、I/O scheduler、控制器、介质、内存回收和写回影响。

文章互动

阅读 --

留言

0 条留言

正在加载留言…