VFS 核心对象与生命周期

VFS 核心对象与生命周期

1. file_system_type

具体文件系统以 register_filesystem() 注册到 filesystems.c 的类型链表。核心回调是 init_fs_context 或旧 mount,模块所有者保证代码生命周期。

2. super_block

super.c 管理挂载实例:

  • sget_fc() 查找或分配 super;
  • get_tree_bdev() 绑定块设备;
  • get_tree_nodev() 建立无设备实例;
  • deactivate_super() / generic_shutdown_super() 卸载;
  • s_umount 串行化 mount/unmount 和 freeze。

一个磁盘文件系统 super 通常包含块大小、root dentry、dirty inode、writeback、quota 和私有 s_fs_info

3. inode

inode.c 管理 inode cache:

1
2
3
4
5
iget_locked()/iget5_locked()
-> lookup inode hash
-> miss: alloc_inode() + insert
-> filesystem reads metadata
-> unlock_new_inode()

i_count 归零经 iput();无链接或 super teardown 时进入 evict(),调用 super_operations->evict_inode() 清理页缓存和磁盘资源。

inode 不是路径名。同一 inode 可被多个 hard link dentry 引用。

4. dentry

dcache.c 的 dentry 表示父目录 + 名称的 lookup 结果,可为:

  • positive:d_inode != NULL
  • negative:记录不存在,缓存失败 lookup;
  • disconnected/anonymous:exportfs 等特殊场景。

d_lookup() 查 hash;d_alloc() 创建;d_add() 绑定 inode;dput() 降引用;shrinker 回收未使用 dentry。

5. file

file_table.cfilp_cachep 分配 file,alloc_file() 绑定 path 和 operations。关键字段:

  • f_path
  • f_inode
  • f_op
  • f_flags / f_mode
  • f_pos
  • f_cred
  • f_count

fput() 最终异步或 task-work 执行 __fput(),调用 flush/release、释放 path 和安全上下文。

6. 操作表

典型职责
super_operations inode alloc/evict、sync、stat、freeze
inode_operations lookup/create/link/unlink/rename/permission/xattr
file_operations open/read_iter/write_iter/mmap/fsync/ioctl/poll
dentry_operations revalidate/hash/compare/release
address_space_operations read/write folio、dirty、writepages、invalidate

操作表通常为只读静态对象,VFS 在持有正确对象引用和锁时调用。

7. 引用关系

1
files_struct -> file -> path -> mount + dentry -> inode -> super_block

这些引用形成复杂图,不能按单一树理解。mount namespace、open fd、cwd、mmap、page cache、export handle 都可能延长对象寿命。

8. 回收

  • file:fput/__fput
  • dentry:LRU + shrink_dcache
  • inode:unused LRU + prune_icache
  • super:unmount + deactivate
  • page cache:memory reclaim/truncate/invalidate

泄漏排查应区分 fd 引用、mount 引用、cwd/root、mmap 和内核 pin。

文章互动

阅读 --

留言

0 条留言

正在加载留言…