linux中的文件系统与inode

📅 2026/8/14 1:57:17
linux中的文件系统与inode
linux文件系统超级块super block文件系统的全局信息文件系统的元数据是文件系统第一个块所包含的重要信息文件系统大小块大小inode表大小文件系统布局作用管理文件系统识别文件系统的类型、结构、状态提供文件系统的操作接口文件系统操作的函数指针维护状态一致挂载状态读写状态structsuper_block{structlist_heads_list;/* 保持此字段在首位用于超级块链表 */dev_ts_dev;/* 设备号用于查找索引 */unsignedchars_blocksize_bits;/* 块大小的位数以2的对数表示 */unsignedlongs_blocksize;/* 块大小字节 */loff_ts_maxbytes;/* 文件最大大小 */structfile_system_type*s_type;/* 文件系统类型指针 */conststructsuper_operations*s_op;/* 超级块操作函数集 */conststructdquot_operations*dq_op;/* 磁盘配额操作函数集 */conststructquotactl_ops*s_qcop;/* 配额控制操作函数集 */conststructexport_operations*s_export_op;/* 导出操作函数集用于NFS等 */unsignedlongs_flags;/* 超级块标志位 */unsignedlongs_iflags;/* 内部标志位 SB_I_* */unsignedlongs_magic;/* 文件系统魔数用于标识文件系统类型 */structdentry*s_root;/* 根目录的dentry目录项 */structrw_semaphores_umount;/* 卸载读写信号量控制卸载操作 */ints_count;/* 超级块引用计数 */atomic_ts_active;/* 活跃引用计数原子操作 */#ifdefCONFIG_SECURITYvoid*s_security;/* 安全模块私有数据 */#endifconststructxattr_handler**s_xattr;/* 扩展属性处理函数数组 */#ifdefCONFIG_FS_ENCRYPTIONconststructfscrypt_operations*s_cop;/* 文件系统加密操作函数集 */structfscrypt_keyring*s_master_keys;/* 正在使用的加密主密钥 */#endif#ifdefCONFIG_FS_VERITYconststructfsverity_operations*s_vop;/* 文系统完整性校验操作函数集 */#endif#ifIS_ENABLED(CONFIG_UNICODE)structunicode_map*s_encoding;/* Unicode编码映射表用于大小写不敏感 */__u16 s_encoding_flags;/* Unicode编码标志 */#endifstructhlist_bl_heads_roots;/* 备用根dentry链表用于NFS */structlist_heads_mounts;/* 挂载点链表仅供VFS使用 */structblock_device*s_bdev;/* 对应的块设备指针 */structbacking_dev_info*s_bdi;/* 后备设备信息用于回写 */structmtd_info*s_mtd;/* MTD设备信息用于闪存文件系统 */structhlist_nodes_instances;/* 文件系统实例链表节点 */unsignedints_quota_types;/* 支持的配额类型位掩码 */structquota_infos_dquot;/* 磁盘配额相关选项 */structsb_writerss_writers;/* 超级块写入者计数冻结控制 *//* * 将 s_fs_info、s_time_gran、s_fsnotify_mask 和 * s_fsnotify_marks 放在一起以提高缓存效率。 * 它们被频繁访问但很少修改。 */void*s_fs_info;/* 文件系统私有信息 *//* 创建/修改/访问时间戳的粒度纳秒最粗为一秒 */u32 s_time_gran;/* 创建/修改/访问时间戳的上下限秒 */time64_ts_time_min;time64_ts_time_max;#ifdefCONFIG_FSNOTIFY__u32 s_fsnotify_mask;/* 文件系统通知掩码 */structfsnotify_mark_connector__rcu*s_fsnotify_marks;/* 文件系统通知标记 */#endifchars_id[32];/* 文件系统标识名称如sda1 */uuid_ts_uuid;/* 文件系统的UUID */unsignedints_max_links;/* 最大硬链接数 */fmode_ts_mode;/* 文件系统挂载模式 *//* * 以下字段仅供VFS使用文件系统实现不应直接访问。 */structmutexs_vfs_rename_mutex;/* VFS重命名互斥锁防止跨目录重命名冲突 *//* * 文件系统子类型。如果非空则 /proc/mounts 中的 * 文件系统类型字段将显示为 type.subtype */constchar*s_subtype;/* 文件系统子类型名称 */conststructdentry_operations*s_d_op;/* 默认的dentry操作函数集 */structshrinkers_shrink;/* 每个超级块的shrinker句柄用于内存回收 *//* 链接数为0但仍被引用的inode数量 */atomic_long_ts_remove_count;/* * 正在被监视的inode/mount/sb对象数量 * 注意inode对象目前被重复计数。 */atomic_long_ts_fsnotify_connectors;/* 正在被重新挂载为只读 */ints_readonly_remount;/* 每个超级块的errseq_t用于通过syncfs报告回写错误 */errseq_ts_wb_err;/* 从中断上下文延迟的AIO完成工作队列 */structworkqueue_struct*s_dio_done_wq;structhlist_heads_pins;/* 引脚链表用于fs核心 *//* * 所属的用户命名空间以及解释文件系统 * uid、gid、配额、设备节点、扩展属性和安全标签的默认上下文。 */structuser_namespace*s_user_ns;/* * list_lru结构本质上只是一个指向每节点LRU链表的指针 * 每个链表有自己的自旋锁无需放入单独的缓存行。 */structlist_lrus_dentry_lru;/* dentry的LRU链表 */structlist_lrus_inode_lru;/* inode的LRU链表 */structrcu_headrcu;/* RCU回调头用于延迟释放 */structwork_structdestroy_work;/* 销毁工作项 */structmutexs_sync_lock;/* 同步序列化锁 *//* * 表示此超级块在文件系统堆栈中的深度 */ints_stack_depth;/* s_inode_list_lock 保护 s_inodes */spinlock_ts_inode_list_lock ____cacheline_aligned_in_smp;/* inode列表自旋锁 */structlist_heads_inodes;/* 所有inode的链表 */spinlock_ts_inode_wblist_lock;/* inode回写列表自旋锁 */structlist_heads_inodes_wb;/* 需要回写的inode链表 */}__randomize_layout;inode表inode table存储文件/目录的inode信息数据块data blocks文件系统存取文件的最小单元由多个连续的扇区组成存储文件/目录的实际数据块位图多个比特位组成的数据结构1表示占据0表示空闲inode文件的元数据数据结构以数组的形式存储作用跟踪文件系统中所有的文件与目录每个文件和目录在文件系统都有唯一的inode对应文件的原文件信息硬链接指向文件的数据块指针链接数属性记录有多少个文件名指向同一个inode多个硬链接共享inode号和数据块删除一个不会影响只能指向相同的文件系统软链接独立文件指向另一个文件/目录的路径快捷方式可以指向不同的文件系统structinode{umode_ti_mode;/* 文件类型和访问权限如0755 */unsignedshorti_opflags;/* inode操作标志 */kuid_ti_uid;/* 文件所有者的用户ID */kgid_ti_gid;/* 文件所有者的组ID */unsignedinti_flags;/* inode标志位如同步、不可变等 */#ifdefCONFIG_FS_POSIX_ACLstructposix_acl*i_acl;/* 访问控制列表ACL */structposix_acl*i_default_acl;/* 默认ACL用于目录继承 */#endifconststructinode_operations*i_op;/* inode操作函数集 */structsuper_block*i_sb;/* 所属的超级块指针 */structaddress_space*i_mapping;/* 地址空间映射用于页面缓存 */#ifdefCONFIG_SECURITYvoid*i_security;/* 安全模块私有数据 */#endif/* 状态数据路径遍历时不访问 */unsignedlongi_ino;/* inode编号文件系统中唯一标识 *//* * 文件系统只能直接读取 i_nlink修改时必须使用以下函数 * * (set|clear|inc|drop)_nlink * inode_(inc|dec)_link_count */union{constunsignedinti_nlink;/* 硬链接计数只读视图 */unsignedint__i_nlink;/* 硬链接计数可写视图 */};dev_ti_rdev;/* 设备号仅对设备文件有效 */loff_ti_size;/* 文件大小字节 */structtimespec64i_atime;/* 最后访问时间 */structtimespec64i_mtime;/* 最后修改时间 */structtimespec64i_ctime;/* 最后状态改变时间 */spinlock_ti_lock;/* 保护 i_blocks、i_bytes可能还有 i_size 的自旋锁 */unsignedshorti_bytes;/* 文件末尾不足一个块的字节数 */u8 i_blkbits;/* 块大小的位数以2的对数表示 */u8 i_write_hint;/* 写入提示用于优化写入路径 */blkcnt_ti_blocks;/* 文件占用的块数 */#ifdef__NEED_I_SIZE_ORDEREDseqcount_ti_size_seqcount;/* i_size的顺序计数器保证读取顺序 */#endif/* 杂项 */unsignedlongi_state;/* inode状态标志位 */structrw_semaphorei_rwsem;/* inode读写信号量 */unsignedlongdirtied_when;/* 首次被标记为脏的时间jiffies */unsignedlongdirtied_time_when;/* 脏时间戳被修改的时间jiffies */structhlist_nodei_hash;/* inode哈希表节点 */structlist_headi_io_list;/* 后备设备IO链表 */#ifdefCONFIG_CGROUP_WRITEBACKstructbdi_writeback*i_wb;/* 关联的cgroup回写设备 *//* 外部inode检测参见 wbc_detach_inode() */inti_wb_frn_winner;/* 当前获胜的外部回写设备ID */u16 i_wb_frn_avg_time;/* 外部回写平均时间 */u16 i_wb_frn_history;/* 外部回写历史记录 */#endifstructlist_headi_lru;/* inode LRU链表用于内存回收 */structlist_headi_sb_list;/* 所属超级块的inode链表 */structlist_headi_wb_list;/* 后备设备回写链表 */union{structhlist_headi_dentry;/* 关联的dentry链表头 */structrcu_headi_rcu;/* RCU回调头用于延迟释放 */};atomic64_ti_version;/* inode版本号用于检测变更 */atomic64_ti_sequence;/* 序列号用于futex */atomic_ti_count;/* inode引用计数 */atomic_ti_dio_count;/* 直接IO引用计数 */atomic_ti_writecount;/* 写者计数用于写者互斥 */#ifdefined(CONFIG_IMA)||defined(CONFIG_FILE_LOCKING)atomic_ti_readcount;/* 以只读方式打开的struct files计数 */#endifunion{conststructfile_operations*i_fop;/* 文件操作函数集默认文件操作 */void(*free_inode)(structinode*);/* 自定义inode释放函数 */};structfile_lock_context*i_flctx;/* 文件锁上下文 */structaddress_spacei_data;/* inode的地址空间页面缓存 */structlist_headi_devices;/* 设备链表用于块设备/字符设备 */union{structpipe_inode_info*i_pipe;/* 管道信息仅对管道文件有效 */structcdev*i_cdev;/* 字符设备信息仅对字符设备有效 */char*i_link;/* 符号链接目标路径 */unsignedi_dir_seq;/* 目录序列号用于目录并发控制 */};__u32 i_generation;/* inode世代号用于NFS文件句柄 */#ifdefCONFIG_FSNOTIFY__u32 i_fsnotify_mask;/* 此inode关注的所有事件掩码 */structfsnotify_mark_connector__rcu*i_fsnotify_marks;/* 文件系统通知标记 */#endif#ifdefCONFIG_FS_ENCRYPTIONstructfscrypt_info*i_crypt_info;/* 加密信息 */#endif#ifdefCONFIG_FS_VERITYstructfsverity_info*i_verity_info;/* 文件完整性校验信息 */#endifvoid*i_private;/* 文件系统或设备的私有指针 */}__randomize_layout;