/* 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) { if (!*name) return NULL; size_t len = strlen(name); char *ret = kzalloc(len + 1, ALLOC_KERN); const char *zeroptr = name + len; if (!ret) return NULL; /* trim trailing slashes */ for (size_t i = len - 1; i >= 1; i--) { if (name[i] == '/') zeroptr = name + i; else break; } /* trim double slashes */ char c; char last = 0; size_t i = 0; const char *cursor = name; while (cursor < zeroptr && (c = *cursor++)) { if (last == c && c == '/') continue; last = c; ret[i++] = last; } 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[256] = {0}; for (int i = 0; i < sizeof(build) - 1; i++) { char c = path[i]; if (c == '/' || c == '\0') { dirent_t *dirent; res = inode_find_dirent(dent->inode, build, &dirent); 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); return res; } } dirent_inc_ref(dirent); spinlock_release(&dirent->lock); *out_dirent = dirent; return i; } else { build[i] = c; } } return -ENOENT; } /* opens a dirent by absolute path only * * IMPORTANT: ownership of the out dirent is passed to the caller, and managing * its refcount is up to the caller (e.g. dirent_dec_ref) */ int vfs_open(const char *abs_path, dirent_t **out) { if (!abs_path || !out) return -EINVAL; char *path_optr = path_trim(abs_path); if (!path_optr) return -ENOENT; char *cur_path = path_optr; dirent_t *current_dir = root_fs->root_dirent; int ret = -ENOENT; dirent_inc_ref_lock(current_dir); /* Handle root path "/" */ if (strlen(abs_path) == 1 && *abs_path == '/') { *out = current_dir; kfree(path_optr); return 0; } while (1) { dirent_t *next_dir; int res = next_level(cur_path, current_dir, &next_dir); if (res < 0) { dirent_dec_ref_lock(current_dir); ret = res; goto out; } dirent_inc_ref_lock(next_dir); dirent_dec_ref_lock(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; } void vfs_close(dirent_t *dent) { dirent_dec_ref_lock(dent); } ssize_t vfs_write(inode_t *ino, char *buf, size_t size, size_t offset) { ssize_t ret; write_lock(&ino->rwlock); 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) { ssize_t ret; read_lock(&ino->rwlock); 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); int res; int ret = 0; if (len > 255) return -ENAMETOOLONG; write_lock(&ino->rwlock); dirent_t *test = NULL; res = inode_find_dirent(ino, name, &test); if (res == 0) { /* exists */ ret = -EEXIST; goto out_err; } else if (res != -ENOENT) { /* various other error codes */ ret = res; goto out_err; } /* create inode */ 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) { ret = res; goto out_err; } res = ino->i_ops.link(parent, new_ino, name, &new_dirent); if (res < 0) { ret = res; res = fs->fs_ops.free_inode(fs, new_ino); if (res < 0) kerr("failed to link inode, then failed to free it!\n"); goto out_err; } *out = new_dirent; out_err: if (test) dirent_dec_ref_lock(test); write_unlock(&ino->rwlock); return ret; } int vfs_mount(const char *mount_point, fs_t *fs_to_mount) { if (!mount_point || !strcmp(mount_point, "/")) { root_fs = fs_to_mount; } else { } return 0; } void vfs_register(struct fs_type *fs) { struct fs_type *ins = kalloc(sizeof(*fs), ALLOC_KERN); memcpy(ins, fs, sizeof(*fs)); list_insert(&fs_type_list, ins); } void vfs_init() { void inode_init(); inode_init(); root_fs = kalloc(sizeof(fs_t), ALLOC_KERN); memset(root_fs, 0, sizeof(*root_fs)); list_create(&fs_type_list); }