清华老师带你彻底了解 Linux 文件系统初探(1),程序员进阶

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Linux运维全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上运维知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip1024b (备注运维)
img

正文

struct list_head s_list; //把所有超级块双向链接起来

dev_t s_dev; //文件系统所在设备标识符

unsigned long s_blocksize; //以字节为单位的盘块大小

unsigned char s_blocksize_bits; //以2的幂次表示的盘块大小,如4KB,则为12

unsigned char s_dirt; //修改脏标记

loff_t s_maxbytes; //文件大小上限

struct file_system_type *s_type; //指向注册表file_system_type结构的指针

const struct super_operations *s_op; //指向超级块操作函数集的指针

unsigned long s_flags; //标记位

unsigned long s_magic; //魔幻数

struct dentry *s_root; //安装目录的目录项对象

struct rw_semaphore s_umount; //卸载信号量

struct mutex s_lock; //超级块信号量

int s_count; //超级块引用计数

struct list_head s_inodes; //所有I节点数

struct list_head s_files;

struct list_head s_dentry_lru; /* unused dentry lru */

int s_nr_dentry_unused; /* # of dentry on lru */

struct list_head s_instances; //该类型文件系统

char s_id[32]; /* 文本名字 */

void *s_fs_info; //各个具体文件系统私有数据结构

struct super_operations {

struct inode *(*alloc_inode)(struct super_block *sb); //分配一个inode

void (*destroy_inode)(struct inode *); //销毁一个inode

void (*dirty_inode) (struct inode *); //将inode标记为脏

int (*write_inode) (struct inode *, int); //将给定的inode写回磁盘

void (*drop_inode) (struct inode *); //逻辑上释放inode

void (*delete_inode) (struct inode *); //物理上释放inode

void (*put_super) (struct super_block *); //释放超级块对象

void (*write_super) (struct super_block *);

int (*sync_fs)(struct super_block *sb, int wait);

int (*freeze_fs) (struct super_block *);

int (*unfreeze_fs) (struct super_block *);

int (*statfs) (struct dentry *, struct kstatfs *);

int (*remount_fs) (struct super_block *, int *, char *);

void (*clear_inode) (struct inode *); //清除一个inode

void (*umount_begin) (struct super_block *);

int (*show_options)(struct seq_file *, struct vfsmount *);

int (*show_stats)(struct seq_file *, struct vfsmount *);

int (bdev_try_to_free_page)(struct super_block, struct page*, gfp_t);

};

索引节点对象

  • inode 对象内包含了内核在操作文件或目录时需要的全部信息,文件名可以更改,但 inode 对文件是唯一的,且随文件的存在而存在。

  • 一个 inode 代表文件系统中的一个文件,它可以是设备或管道这类特殊文件,故 inode 中会包含特殊的项。

  • 当一个文件首次被访问时,内核会在内存中组装相应的索引节点对象,以便向内核提供对一个文件进行操 作时所必需的全部信息;这些信息一部分存储在磁盘特定位置,另外一部分是在加载时动态填充的。

struct inode {

struct hlist_node i_hash;

struct list_head i_list; /* backing dev IO list */

struct list_head i_sb_list;

struct list_head i_dentry;

unsigned long i_ino; //索引节点号

atomic_t i_count; //引用计数

umode_t i_mode; //文件类型与访问权限

……

const struct inode_operations *i_op; //索引操作指针

const struct file_operations *i_fop; //文件操作指针

struct super_block *i_sb; //指向超级块指针

struct file_lock *i_flock; //指向文件锁链表的指针

struct address_space *i_mapping; //共享内存中使用的address_space指针

struct address_space i_data; //设备的address_space对象

struct list_head i_devices; //设备链表

union {

struct pipe_inode_info *i_pipe; //管道设备I节点信息

struct block_device *i_bdev; //指向块设备驱动程序

struct cdev *i_cdev; //指向字符设备驱动程序

};

}

struct inode_operations {

int (*create) (struct inode *,struct dentry *,int, struct nameidata *); //创建一个新的inode

struct dentry * (*lookup) (struct inode *,struct dentry *, struct nameidata *); //查找一个inode所在的目录

int (*link) (struct dentry *,struct inode *,struct dentry *); //创建一个硬链接

int (*unlink) (struct inode *,struct dentry *); //删除一个硬链接

int (*symlink) (struct inode *,struct dentry *,const char *); //为符号链接创建一个inode

int (*mkdir) (struct inode *,struct dentry *,int); //为目录项创建一个inode

int (*rmdir) (struct inode *,struct dentry *); //为目录项删除一个inode

int (*mknod) (struct inode *,struct dentry *,int,dev_t); //建立一个目录项和一个特殊文件的对应索引节点

}

目录项对象

  • VFS 把每个目录看作一个文件,如在路径/tmp/test中, tmp 和 test 都是文件, tmp 是目录文件,而 test 是普通文件, tmp 和 test都有一个 inode 对象表示。

  • 每一个文件除了有一个 inode 数据结构外,还有一个 dentry 数据结构与之关联,该结构中的 d_inode 指针指向相应的 inode 结构。

  • dentry 数据结构可以加快对文件的快速定位,改进文件系统的效率。

  • dentry 描述文件的逻辑属性,它在磁盘上没有对应的映像; inode 结构记录文件的物理属性,在磁盘上有对应的映像。

struct dentry {

atomic_t d_count; //目录引用计数

unsigned int d_flags; //目录项状态标志

spinlock_t d_lock; //目录自旋锁

int d_mounted; //是否为安装点目录项

struct inode *d_inode; //目录项所在的Indoe节点

struct hlist_node d_hash; //目录项形成的hash表

struct dentry *d_parent; //父目录的目录项对象

struct qstr d_name; //目录项名字,用于快速查

struct list_head d_lru; //未使用的LRU双向链表

union {

struct list_head d_child; //父目录的子目录形成的双向链表

struct rcu_head d_rcu;

} d_u;

struct list_head d_subdirs; //该目录项的子目录的双向链表

struct list_head d_alias; //inode别名的链表

unsigned long d_time; //重新生效时间

const struct dentry_operations *d_op; //操作目录项的函数集

struct super_block *d_sb; //指向文件的超级快

void *d_fsdata; //文件系统特殊数据

unsigned char d_iname[DNAME_INLINE_LEN_MIN]; //文件名前15个字符

};

struct dentry_operations {

int (*d_revalidate)(struct dentry *, struct nameidata *); //判断目录是否有效

int (*d_hash) (struct dentry *, struct qstr *); //生成散列值

int (*d_compare) (struct dentry *, struct qstr *, struct qstr *); //比较两个文件名

int (*d_delete)(struct dentry *); //删除d_couny为0的目录项对象

void (*d_release)(struct dentry *); //释放一个目录项对象

void (*d_iput)(struct dentry *, struct inode *); //丢弃目录项对应的Indoe

};

文件对象

  • 文件对象在磁盘上没有映像,在文件被打开时创建由一个 file 结构组成。

  • 文件对象中的信息主要是文件指针,即文件中当前的位置,下一个操作将在该位置发生。

  • file 结构除保存文件当前位置外,还把指向该文件 inode 的指针放在其中,并形成一个双项链表,称系统打开文件表

struct file {

union {

struct list_head fu_list; //文件对象链表

struct rcu_head fu_rcuhead;

} f_u;

struct path f_path; //

const struct file_operations *f_op; //文件操作函数集

spinlock_t f _lock; /* f_ep_links, f_flags, no IRQ */

atomic_long_t f_count; //文件对象引用计数

unsigned int f_flags;

fmode_t f_mode; //操作文件模式

loff_t f_pos; //当前文件位置

struct fown_struct f_owner;

const struct cred *f_cred;

struct file_ra_state f_ra;

u64 f_version;

/* needed for tty driver, and maybe others */

void *private_data;

struct address_space *f_mapping;

}

struct file_operations {

struct module *owner;

loff_t (*llseek) (struct file *, loff_t, int); //修改文件指针

ssize_t (*read) (struct file *, char __user *, size_t, loff_t *); //从文件的偏移处写入若干字节

ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *); //向文件指定偏移处处写入若干字节

ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);//以异步方式从文件的偏移处 读出若干字节

ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);//以异步方式向文件指定偏移 处写入若干字节

int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);//向硬设备发送命令

int (*flush) (struct file *, fl_owner_t id); //关闭文件时刷新文件

I int (*release) (struct inode *, struct file *); //释放文件对象

int (*fsync) (struct file *, struct dentry *, int datasync); //将文件在缓存的数据写回磁盘

与文件相关


根据文件系统所在的物理介质和数据在物理介质上的组织方式来区分不同的文件系统类型的。 file_system_type 结构用于描述具体的文件系统的类型信息。被 Linux 支持的文件系统,都有且仅有一 个 file_system_type 结构而不管它有零个或多个实例被安装到系统中。

struct file_system_type {

const char *name; /文件系统的名字/

struct subsystem subsys; /sysfs子系统对象/

int fs_flags; /文件系统类型标志/

/在文件系统被安装时,从磁盘中读取超级块,在内存中组装超级块对象/

struct super_block *(get_sb) (struct file_system_type,

int, const char*, void *);

void (*kill_sb) (struct super_block *); /终止访问超级块/

struct module *owner; /文件系统模块/

struct file_system_type * next; /链表中的下一个文件系统类型/

struct list_head fs_supers; /具有同一种文件系统类型的超级块对象链表/

};

每当一个文件系统被实际安装,就有一个 vfsmount 结构体被创建,这个结构体对应一个安装点。

struct vfsmount

{

struct list_head mnt_hash; /散列表/

struct vfsmount *mnt_parent; /父文件系统/

struct dentry *mnt_mountpoint; /安装点的目录项对象/

struct dentry *mnt_root; /该文件系统的根目录项对象/

struct super_block *mnt_sb; /该文件系统的超级块/

struct list_head mnt_mounts; /子文件系统链表/

struct list_head mnt_child; /子文件系统链表/

atomic_t mnt_count; /使用计数/

int mnt_flags; /安装标志/

char *mnt_devname; /设备文件名/

struct list_head mnt_list; /描述符链表/

struct list_head mnt_fslink; /具体文件系统的到期列表/

struct namespace *mnt_namespace; /相关的名字空间/

};

与进程相关


  • 用户打开文件相关信息 files_struct

  • 进程的当前的工作目录和根目录的相关信息 fs_struct

struct task_struct {

struct fs_struct *fs; /文件系统信息/

struct files_struct *files; /当前打开的文件信息/

};

文件描述符 fd 用来描述打开的文件,每个进程用一个 files_struct 结构来记录文件描述符的使用情况,这个结构称为用户打开文件表。指向该结构的指针被用来保存在进程的 task_struct 结构的成员 files 中。

struct files_struct {

atomic_t count; //共享该表的进程数

struct fdtable *fdt; //文件描述符指针,指向fdtab

struct fdtable fdtab; //文件描述符表

spinlock_t file_lock ____cacheline_aligned_in_smp; //保护该结构的锁

int next_fd; //下一个空闲fd

struct embedded_fd_set close_on_exec_init; //执行exec函数时需关闭的文件描述符初始集

struct embedded_fd_set open_fds_init; //文件描述符初始集

struct file * fd_array[NR_OPEN_DEFAULT]; //指向文件对象的初始化指针数组

};

fd是指向文件对象的指针数组的指针,数组的长度存放在 max_fdset 域中。通常,fd 域指向 files_struct 结构的 fd_array 域,该域包含 NR_OPEN_DEFAULT (默认值为32)个文件对象指针。如果进程打开的文件数目大于 NR_OPEN_DEFAULT, n 内核分配一个新的文件指针数组,并将其地址存放在 fd 域中,同时更新 max_fds 域的值。

struct fdtable {

unsigned int max_fds; //进程当前具有的最大文件数

struct file ** fd; //指向文件对象(系统打开文件表项)的指针数组

fd_set *close_on_exec; //指向执行exec()函数时需关闭的文件描述符

fd_set *open_fds; //指向打开文件的描述符的指针

struct fdtable *next; //指向下一个描述表

};

对于在fd数组中有入口地址的每个文件来说,数组的下标就是文件描述符。通常,数组的第一个元素(索引为0)、第二个元素(索引为1)、第三个元素(索引为2)分别表示标准输入文件、标准输出文件、和标准错误文件,且这三个文件通常从父进程处继承而来。通过适当的系统调用,两个文件描述符可以指向同一个打开的文件,亦即数组的两个元素可以指向同一个文件对象。

为了进行文件查找,每个进程都有一个当前工作目录和当前工作目录所在文件系统根目录。

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注运维)
img

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
};

对于在fd数组中有入口地址的每个文件来说,数组的下标就是文件描述符。通常,数组的第一个元素(索引为0)、第二个元素(索引为1)、第三个元素(索引为2)分别表示标准输入文件、标准输出文件、和标准错误文件,且这三个文件通常从父进程处继承而来。通过适当的系统调用,两个文件描述符可以指向同一个打开的文件,亦即数组的两个元素可以指向同一个文件对象。

为了进行文件查找,每个进程都有一个当前工作目录和当前工作目录所在文件系统根目录。

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注运维)
[外链图片转存中…(img-0WfXI5w8-1713620305481)]

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值