From f5c3779d18409b8b731aee82d29012afca71fb40 Mon Sep 17 00:00:00 2001 From: zhaoyanbai Date: Fri, 4 Dec 2026 15:29:31 +0800 Subject: [PATCH] =?utf8?q?fs=5Ftype=E7=9A=84read=5Fsuper=E6=94=B9=E4=B8=BA?= =?utf8?q?mount,=E4=BC=A0=E5=87=BA=E5=8F=82=E6=95=B0=E4=BB=8Emount=5Ft?= =?utf8?q?=E6=94=B9=E4=B8=BAsuperblock=E7=9A=84root=20dentry?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- fs/ext2.c | 11 +++++++++-- fs/mount.c | 34 +++++++++++++++++++++++++++++++++- fs/ramfs.c | 10 +++++++--- fs/vfs.c | 20 +++++++++----------- fs/vfs.h | 6 +++--- include/fs.h | 3 +++ 6 files changed, 64 insertions(+), 20 deletions(-) diff --git a/fs/ext2.c b/fs/ext2.c index d66c03d..783b64e 100644 --- a/fs/ext2.c +++ b/fs/ext2.c @@ -14,11 +14,18 @@ #include "system.h" #include "vfs.h" -// superblock_t *ext2_read_super(superblock_t *sb, void *data) { return sb; } + +int ext2_mount(fs_type_t* fs_type, int flags, const char *name, void *data, dentry_t** dentry) { + int ret = 0; + + // TODO + + return ret; +} fs_type_t ext2_fs_type = { .name = "ext2", - // .read_super = ext2_read_super, + .mount = ext2_mount, .next = 0, }; diff --git a/fs/mount.c b/fs/mount.c index 66edb72..ee1b89f 100644 --- a/fs/mount.c +++ b/fs/mount.c @@ -48,12 +48,14 @@ mount_t* vfs_kernel_mount(fs_type_t* type, int flags, const char* name, void* da mnt = alloc_mount(name); - ret = type->read_super(type, flags, name, data, mnt); + ret = type->mount(type, flags, name, data, &mnt->mnt_root); assert(ret == 0); assert(mnt->mnt_sb != 0); assert(mnt->mnt_root != 0); + mnt->mnt_sb = mnt->mnt_root->d_sb; + mnt->mnt_point = mnt->mnt_root; mnt->mnt_parent = mnt; @@ -123,3 +125,33 @@ void mount_root() { current->pwd.mnt = mnt; current->pwd.dentry = mnt->mnt_root; } + + + +// 1. 分配或找到一个superblock +// 2. 调用具体文件系统的 init_sb 回调函数初始化 superblock +// 3. 返回 superblock 的 root dentry + +int mount_nodev(fs_type_t *fs_type, int flags, void *data, init_sb_cb_t init_sb, dentry_t** dentry) { + int ret = 0; + + assert(fs_type != NULL); + assert(init_sb != NULL); + + superblock_t *sb = 0; + + // TODO 查找 or 分配 sb + + init_sb(sb, data); + + + *dentry = sb->sb_root; + + return ret; +} + +int mount_bdev(fs_type_t *type, int flags, block_device_t *bdev, dentry_t** dentry) { + int ret = 0; + + return ret; +} \ No newline at end of file diff --git a/fs/ramfs.c b/fs/ramfs.c index c3144fd..372f67b 100644 --- a/fs/ramfs.c +++ b/fs/ramfs.c @@ -158,17 +158,21 @@ int ramfs_fill_super_cb(superblock_t* sb, void* data) { return err; } -int ramfs_read_super(fs_type_t* type, int flags, const char* name, void* data, mount_t* mnt) { +int ramfs_mount(fs_type_t* fs_type, int flags, const char *name, void *data, dentry_t** dentry) { int ret = 0; - ret = read_super_for_nodev(type, flags, data, ramfs_fill_super_cb, mnt); + + // TODO + mount_t mnt; + ret = read_super_for_nodev(fs_type, flags, data, ramfs_fill_super_cb, &mnt); + *dentry = mnt.mnt_root; return ret; } fs_type_t ramfs_type = { .name = "ramfs", - .read_super = ramfs_read_super, + .mount = ramfs_mount, .next = 0, }; diff --git a/fs/vfs.c b/fs/vfs.c index 0d53b13..76fda60 100644 --- a/fs/vfs.c +++ b/fs/vfs.c @@ -66,17 +66,15 @@ fs_type_t* vfs_find_filesystem(const char* name) { return NULL; } -// void ramfs_init(); -// void vfs_init() { -// ramfs_init(); -// fs_type_t *fs = vfs_find_filesystem("ramfs"); -// if (NULL == fs) { -// panic("no ramfs"); -// } - -// // superblock_t *sb = fs->read_super(NULL, NULL); -// } - +///////// +mount_t* vfsmnt_get(mount_t* m) { + panic("todo"); + return NULL; +} +void vfsmnt_put(mount_t* m) { + // + panic("todo"); +} int vfs_create(inode_t* dir, dentry_t* dentry, int mode, namei_t* ni) { int ret = 0; diff --git a/fs/vfs.h b/fs/vfs.h index 97a2beb..399b6aa 100644 --- a/fs/vfs.h +++ b/fs/vfs.h @@ -270,9 +270,9 @@ struct mount { typedef struct fs_type fs_type_t; struct fs_type { const char* name; - // superblock_t *(*read_super)(superblock_t *, void *); - // TODO REMOVE read_super - int (*read_super)(fs_type_t* type, int flags, const char* name, void* data, mount_t* mnt); + + // dentry 是传出参数,可以根据dentry初始化mount + int (*mount)(fs_type_t* fs_type, int flags, const char *name, void *data, dentry_t** dentry); int flags; // FS_REQUIRES_DEV or NODEV struct fs_type* next; diff --git a/include/fs.h b/include/fs.h index d0bae2d..5977b30 100644 --- a/include/fs.h +++ b/include/fs.h @@ -56,6 +56,9 @@ enum { CHRDEV_CNSL, CHRDEV_SIZE }; extern chrdev_t* chrdev[]; + +typedef int (*init_sb_cb_t)(superblock_t* sb, void* data); + typedef int (*fill_super_cb_t)(superblock_t* sb, void* data); int read_super_for_nodev(fs_type_t* type, int flags, void* data, fill_super_cb_t fill_super, mount_t* mnt); -- 2.56.1