Linux内核文件系统-虚拟文件系统-索引节点对象

建议点击这里查看个人主页上的最新原文

点击这里在哔哩哔哩bilibili在线观看配套的教学视频

点击这里在哔哩哔哩bilibili在线观看配套的加餐视频(就是一些补充)

点击跳转到内核课程所有目录

一般的Linux书籍都是先讲解进程和内存相关的知识,但我想先讲解文件系统。第一,因为我就是做文件系统的,更擅长这一块,其他模块的内容我还要再去好好看看书,毕竟不能误人子弟嘛;第二,是因为文件系统模块更接近于用户态,是相对比较好理解的内容(当然想深入还是要下大功夫的),由文件系统入手比较适合初学者。

虚拟文件系统英文全称Virtual file system,缩写为VFS,又称为虚拟文件切换系统(virtual filesystem switch)。所有的文件系统都要先经过虚拟文件系统层,虚拟文件系统相当于制定了一套规则,如果你想写一个新的文件系统,只需要遵守这套规则就可以了。

VFS虽然是用C语言写的,但使用了面向对象的设计思路。

索引节点包含了操作文件和目录时的全部信息,也定义在include/linux/fs.h。也不需要背,用到时查一下就可以。

/*
 * 将“struct inode”中的大多数已读字段和经常访问的字段(特别是用于RCU路径查找和“stat”数据的字段)放在前面。
 */
struct inode {
        umode_t                 i_mode; // 访问权限
        unsigned short          i_opflags;
        kuid_t                  i_uid; // 使用者的id
        kgid_t                  i_gid; // 使用组的id
        unsigned int            i_flags; // 文件系统标志

#ifdef CONFIG_FS_POSIX_ACL
        struct posix_acl        *i_acl;
        struct posix_acl        *i_default_acl;
#endif

        const struct inode_operations   *i_op; // 索引节点操作表
        struct super_block      *i_sb; // 相关的超级块
        struct address_space    *i_mapping; // 相关的地址映射

#ifdef CONFIG_SECURITY
        void                    *i_security; // 安全模块
#endif

        /* 统计数据,不在路径遍历中访问 */
        unsigned long           i_ino; // 索引节点号
        /*
         * 文件系统只能直接读取 i_nlink。它们应该使用以下函数进行修改:
         *
         *    (set|clear|inc|drop)_nlink
         *    inode_(inc|dec)_link_count
         */
        union {
                const unsigned int i_nlink; // 硬链接数
                unsigned int __i_nlink;
        };
        dev_t                   i_rdev; // 实际设备标识符
        loff_t                  i_size; // 大小,单位: 字节
        struct timespec64       i_atime; // 最后访问时间
        struct timespec64       i_mtime; // 最后修改时间
        struct timespec64       __i_ctime; /* 使用 inode_*_ctime accessors ! 最后改变时间 */
        spinlock_t              i_lock; /* 保护 i_blocks, i_bytes, 还有 i_size,自旋锁 */
        unsigned short          i_bytes; // 使用的字节数
        u8                      i_blkbits; // 以位为单位的块大小
        u8                      i_write_hint;
        blkcnt_t                i_blocks; // 文件的块数

#ifdef __NEED_I_SIZE_ORDERED
        seqcount_t              i_size_seqcount; // 对 i_size 进行串行计数
#endif

        /* Miscellaneous 杂项 */
        unsigned long           i_state; // 状态标志
        struct rw_semaphore     i_rwsem;

        unsigned long           dirtied_when;   /* 第一次弄脏时的 jiffies 值,第一次弄脏数据的时间 */
        unsigned long           dirtied_time_when;

        struct hlist_node       i_hash; // 散列表
        struct list_head        i_io_list;      /* 后备设备 IO 列表 */
#ifdef CONFIG_CGROUP_WRITEBACK
        struct bdi_writeback    *i_wb;          /* 关联的 cgroup wb */

        /* 外来 inode 检测,参见 wbc_detach_inode() */
        int                     i_wb_frn_winner;
        u16                     i_wb_frn_avg_time;
        u16                     i_wb_frn_history;
#endif
        struct list_head        i_lru;          /* inode LRU list,Least Recently Used 最近最少使用链表 */
        struct list_head        i_sb_list; // 超级块链表
        struct list_head        i_wb_list;      /* 后备设备回写列表 */
        union {
                struct hlist_head       i_dentry; // 目录项链表
                struct rcu_head         i_rcu;
        };
        atomic64_t              i_version; // 版本号
        atomic64_t              i_sequence; /* see futex */
        atomic_t                i_count; // 引用计数
        atomic_t                i_dio_count;
        atomic_t                i_writecount; // 写者计数
#if defined(CONFIG_IMA) || defined(CONFIG_FILE_LOCKING)
        atomic_t                i_readcount; /* struct files open RO */
#endif
        union {
                const struct file_operations    *i_fop; /* former ->i_op->default_file_ops,默认的索引节点操作 */
                void (*free_inode)(struct inode *);
        };
        struct file_lock_context        *i_flctx;
        struct address_space    i_data; // 设备地址映射
        struct list_head        i_devices; // 块设备链表
        union {
                struct pipe_inode_info  *i_pipe; // 管道信息
                struct cdev             *i_cdev; // 字符设备驱动
                char                    *i_link;
                unsigned                i_dir_seq;
        };

        __u32                   i_generation;

#ifdef CONFIG_FSNOTIFY
        __u32                   i_fsnotify_mask; /* 该 inode 关心的所有事件 */
        struct fsnotify_mark_connector __rcu    *i_fsnotify_marks;
#endif

#ifdef CONFIG_FS_ENCRYPTION
        struct fscrypt_info     *i_crypt_info;
#endif

#ifdef CONFIG_FS_VERITY
        struct fsverity_info    *i_verity_info;
#endif

        void                    *i_private; /* 文件系统或设备的私有指针 */
} __randomize_layout;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值