Repos / kernel.git / commitdiff
summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: f5c3779)
raw | patch | inline | side by side (parent: f5c3779)
| author | zhaoyanbai <zhaoyanbai@126.com> | |
| Fri, 4 Dec 2026 10:03:27 +0000 (18:03 +0800) | ||
| committer | zhaoyanbai <zhaoyanbai@126.com> | |
| Fri, 4 Dec 2026 10:03:27 +0000 (18:03 +0800) |
| fs/dentry.c | patch | blob | history | |
| fs/ext2.c | patch | blob | history | |
| fs/file.c | patch | blob | history | |
| fs/mount.c | patch | blob | history | |
| fs/ramfs.c | patch | blob | history | |
| fs/super.c | patch | blob | history | |
| fs/vfs.h | patch | blob | history | |
| include/fs.h | patch | blob | history | |
| include/system.h | patch | blob | history |
diff --git a/fs/dentry.c b/fs/dentry.c
--- a/fs/dentry.c
+++ b/fs/dentry.c
#include <errno.h>
#include <mm.h>
-#include <sync.h>
#include <string.h>
+#include <sync.h>
#include <system.h>
#include <vfs.h>
#define DENTRY_HASH_TABLE_SIZE 233
continue;
}
-
mutex_unlock(&dhe->mutex);
return 0;
}
// *dentry = new_dentry;
// } else {
//
- // }
+ // }˝
}
semaphore_up(&dir->i_sem);
diff --git a/fs/ext2.c b/fs/ext2.c
--- a/fs/ext2.c
+++ b/fs/ext2.c
#include "system.h"
#include "vfs.h"
-
-int ext2_mount(fs_type_t* fs_type, int flags, const char *name, void *data, dentry_t** dentry) {
+int ext2_mount(fs_type_t* fs_type, int flags, const char* name, void* data, dentry_t** dentry) {
int ret = 0;
- // TODO
+ assert(fs_type != NULL);
+
+ assert(0 && "ext2_mount not implemented");
return ret;
}
blk_rw(system.root_dev, 1ULL * (blkid) * EXT2_BLOCK_SIZE, (blkcnt) * EXT2_BLOCK_SIZE, buf); \
} while (0)
#else
-#define BLKRW(blkid, blkcnt, buf) \
- do { \
- assert(0); \
+#define BLKRW(blkid, blkcnt, buf) \
+ do { \
+ assert(0); \
} while (0)
#endif
kmem_cache_t* ext2_block_cache;
diff --git a/fs/file.c b/fs/file.c
--- a/fs/file.c
+++ b/fs/file.c
uint32_t hash = page_hash_func(mapping, index);
assert(hash < PAGE_HASH_SIZE);
- ENTER_CRITICAL_ZONE(EFLAGS);
+ ENTER_CRITICAL_ZONE();
page_t* p = page_hash_table[hash];
p = p->hash_next;
}
- LEAVE_CRITICAL_ZONE(EFLAGS);
+ LEAVE_CRITICAL_ZONE();
return page;
}
assert(page->mapping == mapping);
- ENTER_CRITICAL_ZONE(EFLAGS);
+ ENTER_CRITICAL_ZONE();
page_t* p = 0;
p = page_hash_table[hash];
page->hash_next = p;
page_hash_table[hash] = page;
- LEAVE_CRITICAL_ZONE(EFLAGS);
+ LEAVE_CRITICAL_ZONE();
}
void add_page_to_inode(address_space_t* mapping, page_t* page) {
assert(page != NULL);
assert(page->mapping == mapping);
- ENTER_CRITICAL_ZONE(EFLAGS);
+ ENTER_CRITICAL_ZONE();
list_add(&page->list, &mapping->pages);
- LEAVE_CRITICAL_ZONE(EFLAGS);
+ LEAVE_CRITICAL_ZONE();
}
page_t* get_cached_page(address_space_t* mapping, uint32_t index) {
add_page_to_hash(page, mapping, index);
add_page_to_inode(mapping, page);
- ENTER_CRITICAL_ZONE(EFLAGS);
+ ENTER_CRITICAL_ZONE();
list_add(&page->list, &mapping->pages);
- LEAVE_CRITICAL_ZONE(EFLAGS);
+ LEAVE_CRITICAL_ZONE();
}
return page;
for (int i = 0; i < MAX_FILES; i++) {
file_t* p = g_files + i;
if (p->f_state == 0) {
- ENTER_CRITICAL_ZONE(EFLAGS);
+ ENTER_CRITICAL_ZONE();
if (p->f_state == 0) {
p->f_state = 1;
fp = p;
- LEAVE_CRITICAL_ZONE(EFLAGS);
+ LEAVE_CRITICAL_ZONE();
break;
}
- LEAVE_CRITICAL_ZONE(EFLAGS);
+ LEAVE_CRITICAL_ZONE();
}
}
diff --git a/fs/mount.c b/fs/mount.c
--- a/fs/mount.c
+++ b/fs/mount.c
@@ -49,13 +49,14 @@ mount_t* vfs_kernel_mount(fs_type_t* type, int flags, const char* name, void* da
mnt = alloc_mount(name);
ret = type->mount(type, flags, name, data, &mnt->mnt_root);
+
assert(ret == 0);
+ mnt->mnt_sb = mnt->mnt_root->d_sb;
+
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;
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 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;
+ superblock_t* sb = 0;
- // TODO 查找 or 分配 sb
+ ret = find_or_create_sb(fs_type, NULL, set_sb_nodev, NULL, &sb);
+ if (0 != ret) {
+ return ret;
+ }
- init_sb(sb, data);
+ assert(sb != NULL);
+ ret = init_sb(sb, data);
+ if (0 != ret) {
+ return ret;
+ }
*dentry = sb->sb_root;
return ret;
}
-int mount_bdev(fs_type_t *type, int flags, block_device_t *bdev, dentry_t** dentry) {
+int mount_bdev(fs_type_t* type, int flags, block_device_t* bdev, dentry_t** dentry) {
int ret = 0;
return ret;
diff --git a/fs/ramfs.c b/fs/ramfs.c
--- a/fs/ramfs.c
+++ b/fs/ramfs.c
// // .alloc_inode = ramfs_alloc_inode,
// };
-int ramfs_fill_super_cb(superblock_t* sb, void* data) {
- int err = 0;
+int ramfs_init_sb_cb(superblock_t* sb, void* data) {
+ int ret = 0;
- // assert(sb->sb_ops != NULL);
- inode_t* fs_root_inode = ramfs_get_inode(sb, S_IFDIR, 0);
+ inode_t* fs_root_inode = NULL;
+ fs_root_inode = ramfs_get_inode(sb, S_IFDIR, 0);
assert(fs_root_inode != NULL);
- dentry_t* fs_root_dentry = 0;
+ dentry_t* fs_root_dentry = NULL;
fs_root_dentry = dentry_alloc_root(fs_root_inode);
assert(fs_root_dentry != NULL);
sb->sb_root = fs_root_dentry;
- return err;
+ return ret;
}
-int ramfs_mount(fs_type_t* fs_type, int flags, const char *name, void *data, dentry_t** dentry) {
+int ramfs_mount(fs_type_t* fs_type, int flags, const char* name, void* data, dentry_t** dentry) {
int ret = 0;
-
- // TODO
- mount_t mnt;
- ret = read_super_for_nodev(fs_type, flags, data, ramfs_fill_super_cb, &mnt);
- *dentry = mnt.mnt_root;
+ ret = mount_nodev(fs_type, flags, data, ramfs_init_sb_cb, dentry);
return ret;
}
diff --git a/fs/super.c b/fs/super.c
--- a/fs/super.c
+++ b/fs/super.c
return s;
}
-
-
-int set_anonymous_super(superblock_t* s, void* data) {
+int set_sb_nodev(superblock_t* s, void* data) {
s->sb_bdev = NULL;
return 0;
}
-int read_super_for_nodev(fs_type_t* type, int flags, void* data, fill_super_cb_t fill_super, mount_t* mnt) {
- int err = 0;
- superblock_t* s = 0;
-
- // 分配superblock
- err = sget(type, NULL, set_anonymous_super, NULL, &s);
-
- assert(s != NULL);
-
- //
- s->sb_flags = flags;
-
- err = fill_super(s, data);
- if (0 != err) {
- panic("err: %d", err);
- }
-
- mnt->mnt_sb = s;
- mnt->mnt_root = s->sb_root;
-
- return err;
-}
-
-int sget(fs_type_t* type, //
- int (*test)(superblock_t*, void*), //
- int (*set)(superblock_t*, void*), //
- void* data, //
- superblock_t** s //
+int find_or_create_sb(fs_type_t* fs_type, //
+ int (*match_sb)(superblock_t*, void*), //
+ int (*setup_sb)(superblock_t*, void*), //
+ void* data, //
+ superblock_t** sb // OUT
) {
- int err = 0;
-
- *s = 0;
-
- if (0 != test) {
- panic("not implemented");
+ int ret = 0;
+
+ superblock_t* s = NULL;
+
+ ENTER_CRITICAL_ZONE();
+ list_head_t* pos = NULL;
+ list_for_each(pos, &g_superblocks) {
+ s = list_entry(pos, superblock_t, sb_list);
+
+ if (match_sb != NULL) {
+ if (match_sb(s, data) == 0) {
+ // match
+ *sb = s;
+ return 0;
+ } else {
+ // not match
+ continue;
+ }
+ }
}
+ LEAVE_CRITICAL_ZONE();
- // TOOD REMOVE
- assert(0 == *s);
+ assert(s == NULL);
- *s = alloc_super(type);
- if (0 == *s) {
+ s = alloc_super(fs_type);
+ if (s == NULL) {
return ENOMEM;
}
- assert(0 != set);
+ if (setup_sb != NULL) {
+ ret = setup_sb(s, data);
+ }
- err = set(*s, data);
- assert(0 == err);
+ if (0 == ret) {
+ *sb = s;
- uint32_t eflags;
- irq_save(eflags);
- list_add_tail(&(*s)->sb_list, &g_superblocks);
- irq_restore(eflags);
+ ENTER_CRITICAL_ZONE();
+ list_add_tail(&s->sb_list, &g_superblocks);
+ LEAVE_CRITICAL_ZONE();
+ }
- return err;
+ return ret;
}
diff --git a/fs/vfs.h b/fs/vfs.h
--- a/fs/vfs.h
+++ b/fs/vfs.h
//
sb_operations_t* sb_ops;
- list_head_t sb_list;
- list_head_t sb_instance;
+ list_head_t sb_list; // 所有superblock通过这个链成一个链表
+ list_head_t sb_instance; // 所有属于同一个文件系统的superblock通过这个链成一个链表
// dev_t sb_dev;
block_device_t* sb_bdev;
// dev_t i_dev; // inode存储在该设备上
// dev_t i_rdev; // inode所代表的设备号
-
loff_t i_size;
// 将mount与被安装设备的根目录dentry关联
#define MAX_VFSMNT_NAME_LEN 32
struct mount {
- dentry_t* mnt_point; // 挂载点 dentry
- dentry_t* mnt_root; // 设备根目录 dentry
- superblock_t* mnt_sb; // 被安装的设备的superblock
+ dentry_t* mnt_point; // 挂载点 dentry
+ dentry_t* mnt_root; // 设备根目录 dentry
+ superblock_t* mnt_sb; // 被安装的设备的superblock
struct mount* mnt_parent; // 如果多个设备挂载到同一个目录,则每个mount的parent都指向同一个mount
// sb->sb_ops->read_inode得到被安装设备根目录的inode
// 先简单实现:不支持一个设备挂载多次,或一个目录被挂载多次
};
-#define FS_TYPE_NODEV 1
-#define FS_TYPE_BLKDEV 2
-
typedef struct fs_type fs_type_t;
struct fs_type {
const char* name;
// dentry 是传出参数,可以根据dentry初始化mount
- int (*mount)(fs_type_t* fs_type, int flags, const char *name, void *data, dentry_t** dentry);
+ 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;
+
// 同属于这个文件系统的所有超级块链表
// 因为同名文件系统可能有多个实例,所有的该文件系统的实例的superblock,都通过sbs这个链到一起
list_head_t sbs;
diff --git a/include/fs.h b/include/fs.h
--- a/include/fs.h
+++ b/include/fs.h
extern chrdev_t* chrdev[];
+int set_sb_nodev(superblock_t* s, void* data);
+int find_or_create_sb(fs_type_t* fs_type, //
+ int (*match_sb)(superblock_t*, void*), //
+ int (*setup_sb)(superblock_t*, void*), //
+ void* data, //
+ superblock_t** sb // OUT
+);
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);
-
-int sget(fs_type_t* type, //
- int (*test)(superblock_t*, void*), //
- int (*set)(superblock_t*, void*), //
- void* data, //
- superblock_t** s // OUT
-);
+int mount_nodev(fs_type_t* fs_type, int flags, void* data, init_sb_cb_t init_sb, dentry_t** dentry);
file_t* get_file(int fd);
diff --git a/include/system.h b/include/system.h
--- a/include/system.h
+++ b/include/system.h
void leave_critical_zone();
bool in_critical_zone();
-#define ENTER_CRITICAL_ZONE(x) enter_critical_zone(__FILE__ ":" __STRING(x))
-#define LEAVE_CRITICAL_ZONE(x) leave_critical_zone()
+#define ENTER_CRITICAL_ZONE() enter_critical_zone(__FILE__ ":" __STRING(__LINE__))
+#define LEAVE_CRITICAL_ZONE() leave_critical_zone()
#define IN_CRITICAL_ZONE() in_critical_zone()
uint64_t read_uint64_consistent(volatile uint64_t* p);