struct file_system_type rootfs_fs_type = {
.name = "rootfs",
.init_fs_context = rootfs_init_fs_context,
.kill_sb = kill_litter_super,
};
start_kernel
vfs_caches_init
mnt_init
shmem_init
init_rootfs
根据各个全局变量初始化 is_tmpfs
一般 is_tmpfs = false
init_mount_tree
vfs_kern_mount(&rootfs_fs_type, 0, "rootfs", NULL);
fs_context_for_mount
fc_mount
vfs_get_tree
fc->ops->get_tree
get_tree_nodev(fc, ramfs_fill_super);
vfs_get_super
sb = sget_fc
if (!s) s = alloc_super
fc->root = dget(sb->s_root);
ramfs_fill_super
vfs_create_mount
alloc_mnt_ns(&init_user_ns, false);
...
set_fs_pwd(current->fs, &root);
set_fs_root(current->fs, &root);
第一个问题:挂载
我们关注以下两个内容
1. super_block 的填充 : ramfs_fill_super
2. mnt结构体链接到内核的过程 : vfs_create_mount
$3 = (struct mount *) 0x80cb80c0
$3 = {
mnt_hash = {
next = 0x0,
pprev = 0x0
},
mnt_parent = 0x80cb80c0,
mnt_mountpoint = 0x81002088,
mnt = {
mnt_root = 0x81002088,
mnt_sb = 0x80cac800,
mnt_flags = 0
},
{
mnt_rcu = {
next = 0x0,
func = 0x0
},
mnt_llist = {
next = 0x0
}
},
mnt_pcp = 0x80a78e74,
mnt_mounts = {
next = 0x80cb80e8,
prev = 0x80cb80e8
},
mnt_child = {
next = 0x80cb80f0,
prev = 0x80cb80f0
},
mnt_instance = {
next = 0x80cac86c,
prev = 0x80cac86c
},
mnt_devname = 0x808eb808 "none",
mnt_list = {
next = 0x80cb8104,
prev = 0x80cb8104
},
mnt_expire = {
next = 0x80cb810c,
prev = 0x80cb810c
},
mnt_share = {
next = 0x80cb8114,
prev = 0x80cb8114
},
mnt_slave_list = {
next = 0x80cb811c,
prev = 0x80cb811c
},
mnt_slave = {
next = 0x80cb8124,
prev = 0x80cb8124
},
mnt_master = 0x0,
mnt_ns = 0x0,
mnt_mp = 0x0,
{
mnt_mp_list = {
next = 0x0,
pprev = 0x0
},
mnt_umount = {
next = 0x0,
pprev = 0x0
}
},
mnt_umounting = {
next = 0x80cb8140,
prev = 0x80cb8140
},
mnt_fsnotify_marks = 0x0,
mnt_fsnotify_mask = 0,
mnt_id = 1,
mnt_group_id = 0,
mnt_expiry_mark = 0,
mnt_pins = {
first = 0x0
},
mnt_stuck_children = {
first = 0x0
}
}
第二个问题:文件操作
ramfs_get_inode
switch (mode & S_IFMT) {
case S_IFDIR:
inode->i_op = &ramfs_dir_inode_operations;
}
static const struct inode_operations ramfs_dir_inode_operations = {
.create = ramfs_create,
.lookup = simple_lookup,
.link = simple_link,
.unlink = simple_unlink,
.symlink = ramfs_symlink,
.mkdir = ramfs_mkdir,
.rmdir = simple_rmdir,
.mknod = ramfs_mknod,
.rename = simple_rename,
第三个问题 search及真实文件系统中层级目录的维护
我们知道 ext4 是 文件系统层级目录的维护是通过解析 "磁盘文件系统中的 inode节点的 数据信息" 来 获取的
那么 现在的 呢?
第四个问题 文件IO
ramfs_get_inode
switch (mode & S_IFMT) {
case S_IFREG:
inode->i_fop = &ramfs_file_operations;
}
const struct file_operations ramfs_file_operations = {
.read_iter = generic_file_read_iter,
.write_iter = generic_file_write_iter,
.mmap = generic_file_mmap,
.fsync = noop_fsync,
.splice_read = generic_file_splice_read,
.splice_write = iter_file_splice_write,
.llseek = generic_file_llseek,
.get_unmapped_area = ramfs_mmu_get_unmapped_area,
};