淺析 Linux sockfs 文件系統
本文主要對 Linux 網絡文件系統的註冊與掛載過程進行分析
一、簡介
Linux 中 "萬物皆文件",socket 在 Linux 中對應的文件系統叫 Sockfs, 每創建一個 socket, 就在 sockfs 中創建了一個特殊的文件,同時創建了 sockfs 文件系統中的 inode,該 inode 唯一標識當前 socket 的通信。
本文的重點放在 sockfs 文件系統的註冊和掛載流程上,以後會對 socket 的底層來龍去脈進行詳細地分析與記錄。
二、三個核心結構體
1、struct file_system_type
file_system_type 結構體代表 Linux 內核的各種文件系統,每一種文件系統必須要有自己的 file_system_type 結構,用於描述具體的文件系統的類型,如 ext4 對應的 ext4_fs_type,struct file_system_type 結構體如所示:
struct file_system_type {
const char *name; //文件系統的名字
int fs_flags;
#define FS_REQUIRES_DEV 1
#define FS_BINARY_MOUNTDATA 2
#define FS_HAS_SUBTYPE 4
#define FS_USERNS_MOUNT 8 /* Can be mounted by userns root */
#define FS_RENAME_DOES_D_MOVE 32768 /* FS will handle d_move() during rename() internally. */
struct dentry *(*mount) (struct file_system_type *, int,
const char *, void *); //掛載此文件系統時使用的回調函數
void (*kill_sb) (struct super_block *); //釋放超級塊函數指針
struct module *owner;//指向實現這個文件系統的模塊,通常爲THIS_MODULE宏
struct file_system_type * next;//指向文件系統類型鏈表的下一個文件系統類型
struct hlist_head fs_supers;
struct lock_class_key s_lock_key;
struct lock_class_key s_umount_key;
struct lock_class_key s_vfs_rename_key;
struct lock_class_key s_writers_key[SB_FREEZE_LEVELS];
struct lock_class_key i_lock_key;
struct lock_class_key i_mutex_key;
struct lock_class_key i_mutex_dir_key;
};
如 struct file_system_type * next 結構體成員,所有文件系統的 file_system_type 結構形成一個鏈表,在 fs/filesystem.c 中有一個全局的文件系統變量
/* fs/filesystem.c*/
static struct file_system_type *file_systems;
在 Linux 內核中 sock_fs_type 結構定義代表了 sockfs 的網絡文件系統,如下所示:
static struct file_system_type sock_fs_type = {
.name = "sockfs",
.mount = sockfs_mount,
.kill_sb = kill_anon_super,
};
2、struct vfsmount 與 struct mount
每當一個文件系統被安裝時,就會有一個 vfsmount 結構和 mount 被創建,mount 代表該文件系統的一個安裝實例, 比較舊的內核版本中 mount 和 vfsmount 的成員都在 vfsmount 裏,現在 Linux 內核將 vfsmount 改作 mount 結構體,並將 mount 中 mnt_root, mnt_sb, mnt_flags 成員移到 vfsmount 結構體中了。這樣使得 vfsmount 的內容更加精簡,在很多情況下只需要傳遞 vfsmount 而已。struct vfsmount 如下:
struct vfsmount {
struct dentry *mnt_root; //指向這個文件系統的根的dentry
struct super_block *mnt_sb; // 指向這個文件系統的超級塊對象
int mnt_flags; // 此文件系統的掛載標誌
}
對於每一個 mount 的文件系統都有一個 vfsmount 結構來表示,sockfs 安裝時的 vfsmount 定義如下所示:
static struct vfsmount *sock_mnt __read_mostly;
struct mount 如下:
struct mount {
struct hlist_node mnt_hash; /* 用於鏈接到全局已掛載文件系統的鏈表 */
struct mount *mnt_parent; /* 指向此文件系統的掛載點所屬的文件系統,即父文件系統 */
struct dentry *mnt_mountpoint; /* 指向此文件系統的掛載點的dentry */
struct vfsmount mnt; /* 指向此文件系統的vfsmount實例 */
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; /* 掛載在此文件系統下的所有子文件系統的鏈表的表頭,下面的節點都是mnt_child */
struct list_head mnt_child; /* 鏈接到被此文件系統所掛的父文件系統的mnt_mounts上 */
struct list_head mnt_instance; /* 鏈接到sb->s_mounts上的一個mount實例 */
const char *mnt_devname; /* 設備名,如/dev/sdb1 */
struct list_head mnt_list; /* 鏈接到進程namespace中已掛載文件系統中,表頭爲mnt_namespace的list域 */
struct list_head mnt_expire; /* 鏈接到一些文件系統專有的過期鏈表,如NFS, CIFS等 */
struct list_head mnt_share; /* 鏈接到共享掛載的循環鏈表中 */
struct list_head mnt_slave_list;/* 此文件系統的slave mount鏈表的表頭 */
struct list_head mnt_slave; /* 連接到master文件系統的mnt_slave_list */
struct mount *mnt_master; /* 指向此文件系統的master文件系統,slave is on master->mnt_slave_list */
struct mnt_namespace *mnt_ns; /* 指向包含這個文件系統的進程的name space */
struct mountpoint *mnt_mp; /* where is it mounted */
struct hlist_node mnt_mp_list; /* list mounts with the same mountpoint */
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 fs_pin mnt_umount;
struct dentry *mnt_ex_mountpoint;
}
三、sockfs 文件系統的註冊
Linux 內核初始化時,執行 sock_init() 函數登記 sockfs,sock_init() 函數如下:
static int __init sock_init(void)
{
......
err = register_filesystem(&sock_fs_type);//註冊網絡文件系統
......
sock_mnt = kern_mount(&sock_fs_type);//安裝網絡文件系統
......
}
註冊函數:
int register_filesystem(struct file_system_type * fs)
{
int res = 0;
struct file_system_type ** p;
BUG_ON(strchr(fs->name, '.'));
if (fs->next)
return -EBUSY;
write_lock(&file_systems_lock);
p = find_filesystem(fs->name, strlen(fs->name)); //查找是否存在
if (*p)
res = -EBUSY;
else
*p = fs; //將filesystem靜態變量指向fs
write_unlock(&file_systems_lock);
return res;
}
註冊函數中的 find 函數如下, for 循環一開始的 file_systems 變量就是上面說的註冊文件系統使用到的全局變量指針,strncmp 去比較 file_system_type 的第一項 name(文件系統名) 是否和將要註冊的文件系統名字相同,如果相同返回的 P 就是指向同名 file_system_type 結構的指針,如果沒找到則指向 NULL。
static struct file_system_type **find_filesystem(const char *name, unsigned len)
{
struct file_system_type **p;
for (p = &file_systems; *p; p = &(*p)->next)
if (strncmp((*p)->name, name, len) == 0 &&
!(*p)->name[len])
break;
return p;
}
在返回 register_filesystem 函數後,判斷返回值,如果找到重複的則返回 EBUSY 錯誤,如果沒找到重複的,就把當前要註冊的文件系統掛到尾端 file_system_type 的 next 指針上,串聯進鏈表,至此一個文件系統模塊就註冊好了。
四、sockfs 文件系統的安裝
在上面的 sock_init() 函數中的 sock_mnt = kern_mount(&sock_fs_type) 開始進行安裝。kern_mount 函數主要用於那些沒有實體介質的文件系統,該函數主要是獲取文件系統的 super_block 對象與根目錄的 inode 與 dentry 對象,並將這些對象加入到系統鏈表。kern_mount 宏如下所示:
#define kern_mount(type) kern_mount_data(type, NULL)
kern_mount_data 如下:
struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
{
struct vfsmount *mnt;
mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, data);
if (!IS_ERR(mnt)) {
/*
* it is a longterm mount, don't release mnt until
* we unmount before file sys is unregistered
*/
real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL;
}
return mnt;
}
調用:vfs_kern_mount
struct vfsmount *
vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
{
struct mount *mnt;
struct dentry *root;
if (!type)
return ERR_PTR(-ENODEV);
mnt = alloc_vfsmnt(name);//分配一個mount對象,並對其進行部分初始化
if (!mnt)
return ERR_PTR(-ENOMEM);
if (flags & SB_KERNMOUNT)
mnt->mnt.mnt_flags = MNT_INTERNAL;
root = mount_fs(type, flags, name, data);//獲取該文件系統的根目錄的dentry,同時也獲取super_block
if (IS_ERR(root)) {
mnt_free_id(mnt);
free_vfsmnt(mnt);
return ERR_CAST(root);
}
//對mnt對象與root進行綁定
mnt->mnt.mnt_root = root;
mnt->mnt.mnt_sb = root->d_sb;
mnt->mnt_mountpoint = mnt->mnt.mnt_root;
mnt->mnt_parent = mnt;
lock_mount_hash();
list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts);//將mnt添加到root->d_sb->s_mounts鏈表中
unlock_mount_hash();
return &mnt->mnt;
}
vfs_kern_mount 函數調用 mount_fs 獲取該文件系統的根目錄的 dentry, 同時也獲取 super_block,具體實現如下:
struct dentry *
mount_fs(struct file_system_type *type, int flags, const char *name, void *data)
{
struct dentry *root;
struct super_block *sb;
char *secdata = NULL;
int error = -ENOMEM;
if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {//在kern_mount調用中data爲NULL,所以該if判斷爲假
secdata = alloc_secdata();
if (!secdata)
goto out;
error = security_sb_copy_data(data, secdata);
if (error)
goto out_free_secdata;
}
root = type->mount(type, flags, name, data);//調用file_system_type中的 mount方法
if (IS_ERR(root)) {
error = PTR_ERR(root);
goto out_free_secdata;
}
sb = root->d_sb;
BUG_ON(!sb);
WARN_ON(!sb->s_bdi);
sb->s_flags |= SB_BORN;
error = security_sb_kern_mount(sb, flags, secdata);
......
}
其中 type->mount() 繼續調用了 sockfs 的回調函數 sockfs_mount
static struct dentry *sockfs_mount(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data)
{
return mount_pseudo_xattr(fs_type, "socket:", &sockfs_ops,
sockfs_xattr_handlers,
&sockfs_dentry_operations, SOCKFS_MAGIC);
}
struct dentry *mount_pseudo_xattr(struct file_system_type *fs_type, char *name,
const struct super_operations *ops, const struct xattr_handler **xattr,
const struct dentry_operations *dops, unsigned long magic)
{
struct super_block *s;
struct dentry *dentry;
struct inode *root;
struct qstr d_name = QSTR_INIT(name, strlen(name));
s = sget_userns(fs_type, NULL, set_anon_super, SB_KERNMOUNT|SB_NOUSER,
&init_user_ns, NULL);
if (IS_ERR(s))
return ERR_CAST(s);
s->s_maxbytes = MAX_LFS_FILESIZE;
s->s_blocksize = PAGE_SIZE;
s->s_blocksize_bits = PAGE_SHIFT;
s->s_magic = magic;
s->s_op = ops ? ops : &simple_super_operations;
s->s_xattr = xattr;
s->s_time_gran = 1;
root = new_inode(s);
if (!root)
goto Enomem;
/*
* since this is the first inode, make it number 1. New inodes created
* after this must take care not to collide with it (by passing
* max_reserved of 1 to iunique).
*/
root->i_ino = 1;
root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
root->i_atime = root->i_mtime = root->i_ctime = current_time(root);
dentry = __d_alloc(s, &d_name);
if (!dentry) {
iput(root);
goto Enomem;
}
d_instantiate(dentry, root);
s->s_root = dentry;
s->s_d_op = dops;
s->s_flags |= SB_ACTIVE;
return dget(s->s_root);
Enomem:
deactivate_locked_super(s);
return ERR_PTR(-ENOMEM);
}
以上函數進行超級塊、根 root、根 dentry 相關的創建及初始化操作,其中上面的 s->s_d_op =dops 就說指向了 sockfs_ops 結構體,也就是該 sockfs 文件系統的 struct super_block 的函數操作集指向了 sockfs_ops。
static const struct super_operations sockfs_ops = {
.alloc_inode = sock_alloc_inode,
.destroy_inode = sock_destroy_inode,
.statfs = simple_statfs,
};
該函數表對 sockfs 文件系統的節點和目錄提供了具體的操作函數,後面涉及到的 sockfs 文件系統的重要操作均會到該函數表中查找到對應的操作函數,例如 Linux 內核在創建 socket 節點時會查找 sockfs_ops 的 alloc_inode 函數, 從而調用 sock_alloc_indode 函數完成 socket 以及 inode 節點的創建。
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源:https://mp.weixin.qq.com/s/u5GgIUNFEKbbaUEz92li2Q