/* SPDX-License-Identifier: GPL-2.0-only */ #include #include #include #include #include #include static fs_t *root_fs = NULL; static list_t fs_type_list = {0}; static char *path_trim(const char *name) { while (*name == '/') name++; size_t len = strlen(name); if (len == 0) return kzalloc(1, ALLOC_KERN); const char *end = name + len; while (end > name && *(end - 1) == '/') end--; char *ret = kzalloc((end - name) + 1, ALLOC_KERN); if (!ret) return NULL; char last = 0; size_t j = 0; for (const char *c = name; c < end; c++) { if (last == '/' && *c == '/') continue; ret[j++] = *c; last = *c; } return ret; } static int next_level(const char *path, dirent_t *dent, dirent_t **out_dirent) { if (!dent || !out_dirent) return -EINVAL; int res; char *build = kzalloc(FILE_NAME_MAX_LEN + 1, ALLOC_KERN); if (!build) return -ENOMEM; for (int i = 0; i < FILE_NAME_MAX_LEN; i++) { char c = path[i]; if (c == '/' || c == '\0') { dirent_t *dirent; res = inode_find_dirent(dent->inode, build, &dirent); kfree(build); if (res < 0) return -ENOENT; spinlock_acquire(&dirent->lock); if (!dirent->inode) { fs_t *fs = dent->fs; u64 inum = dirent->ino_num; res = fs->fs_ops.open_inode(fs, inum, dirent, &dirent->inode); if (res < 0) { spinlock_release(&dirent->lock); dirent_dec_ref(dirent); return res; } } spinlock_release(&dirent->lock); *out_dirent = dirent; return i; } else { build[i] = c; } } kfree(build); return -ENOENT; } int vfs_openat(dirent_t *in, const char *name, dirent_t **out) { if (!in || !name || !out) return -EINVAL; char *path_optr = path_trim(name); if (!path_optr) return -ENOMEM; dirent_t *current_dir = in; dirent_inc_ref(current_dir); if (*path_optr == '\0') { *out = current_dir; kfree(path_optr); return 0; } char *cur_path = path_optr; int ret = -ENOENT; while (1) { dirent_t *next_dir; int res = next_level(cur_path, current_dir, &next_dir); if (res < 0) { dirent_dec_ref(current_dir); ret = res; goto out; } dirent_dec_ref(current_dir); current_dir = next_dir; cur_path += res; while (*cur_path == '/') cur_path++; if (*cur_path == '\0') { *out = current_dir; ret = 0; goto out; } } out: kfree(path_optr); return ret; } int vfs_open(const char *abs_path, dirent_t **out) { if (!abs_path || !out) return -EINVAL; if (!root_fs->root_dirent) return -ENOENT; return vfs_openat(root_fs->root_dirent, abs_path, out); } void vfs_close(dirent_t *dent) { dirent_dec_ref(dent); } ssize_t vfs_write(inode_t *ino, char *buf, size_t size, size_t offset) { if (!ino) return -EINVAL; write_lock(&ino->rwlock); ssize_t ret = ino->i_ops.write(ino, buf, size, offset); write_unlock(&ino->rwlock); return ret; } ssize_t vfs_read(inode_t *ino, char *buf, size_t size, size_t offset) { if (!ino) return -EINVAL; read_lock(&ino->rwlock); ssize_t ret = ino->i_ops.read(ino, buf, size, offset); read_unlock(&ino->rwlock); return ret; } int vfs_creat(dirent_t *parent, const char *name, u64 mode, int flags, dirent_t **out) { if (!parent || !name || !out) return -EINVAL; inode_t *ino = parent->inode; size_t len = strlen(name); if (len > FILE_NAME_MAX_LEN) return -ENAMETOOLONG; if ((ino->mode & S_IFMT) != S_IFDIR) return -ENOTDIR; write_lock(&ino->rwlock); if ((ino->mode & S_IFMT) != S_IFDIR) { write_unlock(&ino->rwlock); return -ENOTDIR; } dirent_t *test = NULL; int res = inode_find_dirent_locked(ino, name, &test); if (res == 0) { dirent_dec_ref(test); write_unlock(&ino->rwlock); return -EEXIST; } else if (res != -ENOENT) { write_unlock(&ino->rwlock); return res; } fs_t *fs = parent->fs; inode_t *new_ino; dirent_t *new_dirent; res = fs->fs_ops.alloc_inode(fs, mode, &new_ino); if (res < 0) { write_unlock(&ino->rwlock); return res; } res = ino->i_ops.link(ino, new_ino, name, &new_dirent); if (res < 0) { fs->fs_ops.free_inode(fs, new_ino); write_unlock(&ino->rwlock); return res; } new_dirent->fs = fs; *out = new_dirent; write_unlock(&ino->rwlock); return 0; } int vfs_unlink(dirent_t *parent, const char *name) { if (!parent || !name) return -EINVAL; inode_t *ino = parent->inode; write_lock(&ino->rwlock); if ((ino->mode & S_IFMT) != S_IFDIR) { write_unlock(&ino->rwlock); return -ENOTDIR; } int res = ino->i_ops.unlink(ino, name); write_unlock(&ino->rwlock); return res; } int vfs_mount(const char *mount_point, fs_t *fs_to_mount) { if (!mount_point || !strcmp(mount_point, "/")) { root_fs = fs_to_mount; return 0; } return -ENOSYS; } void vfs_register(struct fs_driver *fs) { struct fs_driver *ins = kalloc(sizeof(*fs), ALLOC_KERN); if (!ins) return; memcpy(ins, fs, sizeof(*fs)); if (!list_insert(&fs_type_list, ins)) kfree(ins); } void vfs_init(void) { inode_init(); root_fs = kalloc(sizeof(fs_t), ALLOC_KERN); memset(root_fs, 0, sizeof(*root_fs)); list_create(&fs_type_list); }