From 0e624b1fcfee58626072929100b593ffb27354a3 Mon Sep 17 00:00:00 2001 From: fslongjin Date: Sat, 17 Sep 2022 14:11:17 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=EF=BC=9Adevfs=E6=B3=A8?= =?UTF-8?q?=E5=86=8C=E8=AE=BE=E5=A4=87=E5=90=8E=EF=BC=8C=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E6=8C=87=E5=90=91inode=E7=A7=81=E6=9C=89=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E7=9A=84=E6=8C=87=E9=92=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/driver/keyboard/ps2_keyboard.c | 2 +- kernel/driver/tty/tty.c | 5 +-- kernel/filesystem/devfs/devfs-types.h | 1 + kernel/filesystem/devfs/devfs.c | 50 ++++++++++++++++++--------- kernel/filesystem/devfs/devfs.h | 3 +- kernel/process/process.c | 1 + 6 files changed, 41 insertions(+), 21 deletions(-) diff --git a/kernel/driver/keyboard/ps2_keyboard.c b/kernel/driver/keyboard/ps2_keyboard.c index 0d64ef9c..04e9c1f9 100644 --- a/kernel/driver/keyboard/ps2_keyboard.c +++ b/kernel/driver/keyboard/ps2_keyboard.c @@ -205,7 +205,7 @@ void ps2_keyboard_init() // 先读一下键盘的数据,防止由于在键盘初始化之前,由于按键被按下从而导致接收不到中断。 io_in8(PORT_PS2_KEYBOARD_DATA); // 将设备挂载到devfs - devfs_register_device(DEV_TYPE_CHAR, CHAR_DEV_STYPE_PS2_KEYBOARD, &ps2_keyboard_fops); + devfs_register_device(DEV_TYPE_CHAR, CHAR_DEV_STYPE_PS2_KEYBOARD, &ps2_keyboard_fops, NULL); kinfo("ps/2 keyboard registered."); } diff --git a/kernel/driver/tty/tty.c b/kernel/driver/tty/tty.c index 5e92c22c..dfee71b2 100644 --- a/kernel/driver/tty/tty.c +++ b/kernel/driver/tty/tty.c @@ -2,6 +2,7 @@ #include #include "tty.h" +static struct devfs_private_inode_info_t * tty_inode_private_data_ptr; // 由devfs创建的inode私有信息指针 static int tty_private_data; /** @@ -87,6 +88,6 @@ struct vfs_file_operations_t tty_fops={ void tty_init(){ //注册devfs - devfs_register_device(DEV_TYPE_CHAR, CHAR_DEV_STYPE_TTY, &tty_fops); - kinfo("tty driver registered."); + devfs_register_device(DEV_TYPE_CHAR, CHAR_DEV_STYPE_TTY, &tty_fops, &tty_inode_private_data_ptr); + kinfo("tty driver registered. uuid=%d", tty_inode_private_data_ptr->uuid); } \ No newline at end of file diff --git a/kernel/filesystem/devfs/devfs-types.h b/kernel/filesystem/devfs/devfs-types.h index 4acf8aa7..d28c6493 100644 --- a/kernel/filesystem/devfs/devfs-types.h +++ b/kernel/filesystem/devfs/devfs-types.h @@ -42,4 +42,5 @@ struct devfs_private_inode_info_t uint16_t type; // 设备主类型 uint16_t sub_type; // 设备子类型 struct vfs_file_operations_t *f_ops; + uint64_t uuid; }; diff --git a/kernel/filesystem/devfs/devfs.c b/kernel/filesystem/devfs/devfs.c index 748761f6..dbbd8cf7 100644 --- a/kernel/filesystem/devfs/devfs.c +++ b/kernel/filesystem/devfs/devfs.c @@ -4,6 +4,7 @@ #include #include #include +#include struct vfs_super_block_operations_t devfs_sb_ops; struct vfs_dir_entry_operations_t devfs_dentry_ops; @@ -14,6 +15,15 @@ struct vfs_dir_entry_t *devfs_root_dentry; // 根结点的dentry struct vfs_superblock_t devfs_sb = {0}; const char __devfs_mount_path[] = "/dev"; +static spinlock_t devfs_global_lock; // devfs的全局锁 +static uint64_t __tmp_uuid = 0; // devfs的临时uuid变量(todo:在引入uuid lib之后删除这里) + +static inline uint64_t __devfs_get_uuid() +{ + // todo : 更改为使用uuid库来生成uuid + return ++__tmp_uuid; +} + /** * @brief 创建devfs的super block * @@ -32,11 +42,11 @@ struct vfs_superblock_t *devfs_read_superblock(struct block_device *blk) return &devfs_sb; } -static void devfs_write_superblock(struct vfs_superblock_t *sb) {return ; } +static void devfs_write_superblock(struct vfs_superblock_t *sb) { return; } -static void devfs_put_superblock(struct vfs_superblock_t *sb) {return ; } +static void devfs_put_superblock(struct vfs_superblock_t *sb) { return; } -static void devfs_write_inode(struct vfs_index_node_t *inode) {return ; } +static void devfs_write_inode(struct vfs_index_node_t *inode) { return; } struct vfs_super_block_operations_t devfs_sb_ops = { .write_superblock = &devfs_write_superblock, @@ -44,13 +54,13 @@ struct vfs_super_block_operations_t devfs_sb_ops = .write_inode = &devfs_write_inode, }; -static long devfs_compare(struct vfs_dir_entry_t *parent_dEntry, char *source_filename, char *dest_filename) {return 0; } +static long devfs_compare(struct vfs_dir_entry_t *parent_dEntry, char *source_filename, char *dest_filename) { return 0; } -static long devfs_hash(struct vfs_dir_entry_t *dEntry, char *filename) {return 0; } +static long devfs_hash(struct vfs_dir_entry_t *dEntry, char *filename) { return 0; } static long devfs_release(struct vfs_dir_entry_t *dEntry) { return 0; } -static long devfs_iput(struct vfs_dir_entry_t *dEntry, struct vfs_index_node_t *inode) {return 0; } +static long devfs_iput(struct vfs_dir_entry_t *dEntry, struct vfs_index_node_t *inode) { return 0; } struct vfs_dir_entry_operations_t devfs_dentry_ops = { @@ -64,10 +74,10 @@ static long devfs_open(struct vfs_index_node_t *inode, struct vfs_file_t *file_p { return 0; } -static long devfs_close(struct vfs_index_node_t *inode, struct vfs_file_t *file_ptr) {return 0; } -static long devfs_read(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *position) {return 0; } -static long devfs_write(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *position) {return 0; } -static long devfs_lseek(struct vfs_file_t *file_ptr, long offset, long origin) {return 0; } +static long devfs_close(struct vfs_index_node_t *inode, struct vfs_file_t *file_ptr) { return 0; } +static long devfs_read(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *position) { return 0; } +static long devfs_write(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *position) { return 0; } +static long devfs_lseek(struct vfs_file_t *file_ptr, long offset, long origin) { return 0; } static long devfs_ioctl(struct vfs_index_node_t *inode, struct vfs_file_t *file_ptr, uint64_t cmd, uint64_t arg) { return 0; } static long devfs_readdir(struct vfs_file_t *file_ptr, void *dirent, vfs_filldir_t filler) @@ -122,7 +132,7 @@ struct vfs_file_operations_t devfs_file_ops = */ static long devfs_create(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_t *dest_dEntry, int mode) { - return 0; + return 0; } static struct vfs_dir_entry_t *devfs_lookup(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_t *dest_dEntry) @@ -157,9 +167,9 @@ static long devfs_mkdir(struct vfs_index_node_t *inode, struct vfs_dir_entry_t * } static long devfs_rmdir(struct vfs_index_node_t *inode, struct vfs_dir_entry_t *dEntry) { return 0; } -static long devfs_rename(struct vfs_index_node_t *old_inode, struct vfs_dir_entry_t *old_dEntry, struct vfs_index_node_t *new_inode, struct vfs_dir_entry_t *new_dEntry) {return 0; } -static long devfs_getAttr(struct vfs_dir_entry_t *dEntry, uint64_t *attr) {return 0; } -static long devfs_setAttr(struct vfs_dir_entry_t *dEntry, uint64_t *attr) {return 0; } +static long devfs_rename(struct vfs_index_node_t *old_inode, struct vfs_dir_entry_t *old_dEntry, struct vfs_index_node_t *new_inode, struct vfs_dir_entry_t *new_dEntry) { return 0; } +static long devfs_getAttr(struct vfs_dir_entry_t *dEntry, uint64_t *attr) { return 0; } +static long devfs_setAttr(struct vfs_dir_entry_t *dEntry, uint64_t *attr) { return 0; } struct vfs_inode_operations_t devfs_inode_ops = { .create = &devfs_create, .lookup = &devfs_lookup, @@ -208,16 +218,19 @@ static __always_inline void __devfs_init_root_dentry() * @param device_type 设备主类型 * @param sub_type 设备子类型 * @param file_ops 设备的文件操作接口 + * @param ret_private_inode_info_ptr 返回的指向inode私有信息结构体的指针 * @return int 错误码 */ -int devfs_register_device(uint16_t device_type, uint16_t sub_type, struct vfs_file_operations_t *file_ops) +int devfs_register_device(uint16_t device_type, uint16_t sub_type, struct vfs_file_operations_t *file_ops, struct devfs_private_inode_info_t **ret_private_inode_info_ptr) { + spin_lock(&devfs_global_lock); int retval = 0; // 申请private info结构体 struct devfs_private_inode_info_t *private_info = (struct devfs_private_inode_info_t *)kzalloc(sizeof(struct devfs_private_inode_info_t), 0); private_info->f_ops = file_ops; private_info->type = device_type; private_info->sub_type = sub_type; + private_info->uuid = __devfs_get_uuid(); struct vfs_dir_entry_t *dentry = NULL; // 该指针由对应类型设备的注册函数设置 @@ -233,10 +246,14 @@ int devfs_register_device(uint16_t device_type, uint16_t sub_type, struct vfs_fi goto failed; break; } + if (ret_private_inode_info_ptr != NULL) + *ret_private_inode_info_ptr = private_info; + spin_unlock(&devfs_global_lock); return retval; failed:; kfree(private_info); + spin_unlock(&devfs_global_lock); return retval; } @@ -248,9 +265,8 @@ void devfs_init() { __devfs_init_root_dentry(); vfs_register_filesystem(&devfs_fs_type); + spin_init(&devfs_global_lock); vfs_mount_fs(__devfs_mount_path, "DEVFS", NULL); __devfs_chardev_init(); - - } \ No newline at end of file diff --git a/kernel/filesystem/devfs/devfs.h b/kernel/filesystem/devfs/devfs.h index 9d66ead1..b68f30a0 100644 --- a/kernel/filesystem/devfs/devfs.h +++ b/kernel/filesystem/devfs/devfs.h @@ -14,6 +14,7 @@ void devfs_init(); * @param device_type 设备主类型 * @param sub_type 设备子类型 * @param file_ops 设备的文件操作接口 + * @param ret_private_inode_info_ptr 返回的指向inode私有信息结构体的指针 * @return int 错误码 */ -int devfs_register_device(uint16_t device_type, uint16_t sub_type, struct vfs_file_operations_t *file_ops); \ No newline at end of file +int devfs_register_device(uint16_t device_type, uint16_t sub_type, struct vfs_file_operations_t *file_ops, struct devfs_private_inode_info_t **ret_private_inode_info_ptr); \ No newline at end of file diff --git a/kernel/process/process.c b/kernel/process/process.c index d5aa7b85..5359c30e 100644 --- a/kernel/process/process.c +++ b/kernel/process/process.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include