一、mount
mount的文件系统在内核面会组织成一颗mount树,根节点是root filesystem的struct mount。
(注:图来自《趣谈Linux操作系统》)
struct mount {
struct hlist_node mnt_hash;
struct mount *mnt_parent;
struct dentry *mnt_mountpoint;
struct vfsmount mnt;
union {
struct rcu_head mnt_rcu;
struct llist_node mnt_llist;
};
#ifdef CONFIG_SMP
struct mnt_pcp __percpu *mnt_pcp;
#else
int mnt_count;
int mnt_writers;
#endif
struct list_head mnt_mounts; /* list of children, anchored here */
struct list_head mnt_child; /* and going through their mnt_child */
struct list_head mnt_instance; /* mount instance on sb->s_mounts */
const char *mnt_devname; /* Name of device e.g. /dev/dsk/hda1 */
struct list_head mnt_list;
struct list_head mnt_expire; /* link in fs-specific expiry list */
struct list_head mnt_share; /* circular list of shared mounts */
struct list_head mnt_slave_list;/* list of slave mounts */
struct list_head mnt_slave; /* slave list entry */
struct mount *mnt_master; /* slave is on master->mnt_slave_list */
struct mnt_namespace *mnt_ns; /* containing namespace */
struct mountpoint *mnt_mp; /* where is it mounted */
union {
struct hlist_node mnt_mp_list; /* list mounts with the same mountpoint */
struct hlist_node mnt_umount;
};
struct list_head mnt_umounting; /* list entry for umount propagation */
#ifdef CONFIG_FSNOTIFY
struct fsnotify_mark_connector __rcu *mnt_fsnotify_marks;
__u32 mnt_fsnotify_mask;
#endif
int mnt_id; /* mount identifier */
int mnt_group_id; /* peer group identifier */
int mnt_expiry_mark; /* true if marked for expiry */
struct hlist_head mnt_pins;
struct hlist_head mnt_stuck_children;
} __randomize_layout;
struct vfsmount {
struct dentry *mnt_root; /* root of the mounted tree */
struct super_block *mnt_sb; /* pointer to superblock */
int mnt_flags;
} __randomize_layout;
二、open
方法调用栈:
- do_sys_open
- get_unused_fd_flags:拿fd
- do_filp_open:查找文件
- set_nameidata:创建nameidata
- path_openat:开始路径查找
- get_empty_filp:创建一个空的file
- path_init:初始化nameidata
- link_path_walk:一个大循环对路径名逐层查找
- do_last:拿到inode并初始化file
- lookup_fast:先找dentry cache
- lookup_open:到文件系统中去找
- vfs_open:真正的打开文件,然后将打开的文件信息填写到file
- fd_install:查找成功,将fd放入文件,然后系统调用返回
文字版描述:
- 会先去拿fd,拿不到就报too many open filse的错;
- 查找文件;
- 创建st