From 2f20777a47781e6c8d6f38d72e28c858b8a56e34 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Tue, 30 Jan 2024 18:54:59 -0500 Subject: [PATCH] fs: move directory parsing to vfs Previously, the vfs heavily relied on the physical filesystem for directory parsing, which limited capabilities to a single filesystem, save letter-based indexing which will not be used. Now, vnodes are parsed in the VFS, and only requested on-demand, greatly reducing complexity and reliance on physical drivers for handling. This temporarily removes the ability to create files with the current drivers. --- fs/ext2.c | 286 +---------------------------------------------- fs/vfs.c | 81 ++++++++++++-- include/fs/vfs.h | 10 +- init/cmd.c | 22 +--- 4 files changed, 81 insertions(+), 318 deletions(-) diff --git a/fs/ext2.c b/fs/ext2.c index 5bf9839..d5a4ffb 100644 --- a/fs/ext2.c +++ b/fs/ext2.c @@ -635,46 +635,6 @@ found: return inode_no; } -static int ext2_open_file(struct fs *vfs, struct vnode *vnode, const char *path) -{ - /* find the inode of the file */ - struct ext2fs *fs = (struct ext2fs *)vfs->fs; - struct ext2_inode *inode = kmalloc(sizeof(struct ext2_inode), ALLOC_KERN); - if (!inode) - return -ENOMEM; - - long ino_ret = ext2_inode_find_by_name(fs, path); - if (ino_ret < 0) { - kfree(inode); - return ino_ret; - } - ino_t inode_no = ino_ret; - - int ret = ext2_read_inode(fs, inode, inode_no); - if (ret < 0) { - kfree(inode); - return ret; - } - - vnode->fs = vfs; - vnode->size = inode->size; - vnode->uid = inode->uid; - vnode->gid = inode->gid; - vnode->flags = inode->mode; - vnode->ino_num = inode_no; - - char *bn = basename((char *)path); - memcpy(vnode->name, bn, MIN(strlen(bn), 255)); - - /* check for long filesize */ - if (fs->sb.feature_ro_compat & EXT2_FLAG_LONG_FILESIZE) - vnode->size |= ((uint64_t)inode->dir_acl << 32); - - kfree(inode); - - return 0; -} - static int ext2_readdir(struct vnode *vnode, struct dirent **out) { struct fs *vfs = vnode->fs; @@ -843,244 +803,6 @@ static int ino_type_to_dent(uint32_t ino_flags) return -1; } -static long ext2_creat(struct fs *vfs, const char *path, uint32_t type) -{ - struct ext2fs *fs = (struct ext2fs *)vfs->fs; - - /* make sure the file doesn't already exist */ - struct vnode vnode; - int ret = ext2_open_file(vfs, &vnode, path); - - if (ret == 0) - return -EEXIST; - - /* find the parent directory */ - char *path_copy = kzalloc(strlen(path) + 1, ALLOC_KERN); - if (!path_copy) - return -ENOMEM; - - strcpy(path_copy, path); - - char *base = basename(path_copy); - char *dir = dirname(path_copy); - - /* ensure the dir is a directory */ - struct ext2_inode *dir_inode = kmalloc(sizeof(struct ext2_inode), ALLOC_DMA); - - long parent_ino_num; - if (dir == NULL) - parent_ino_num = EXT2_ROOT_INO; - else - parent_ino_num = ext2_inode_find_by_name(fs, dir); - - if (parent_ino_num < 0) { - kfree(path_copy); - return parent_ino_num; - } - - ext2_read_inode(fs, dir_inode, parent_ino_num); - - /* allocate an inode */ - ino_t inode_no = ext2_alloc_inode(vfs, type); - - /* allocate a block for the inode */ - long block_no = ext2_alloc_block(fs); - if (block_no < 0) { - kfree(path_copy); - return block_no; - } - - /* write the inode to disk */ - struct ext2_inode *inode = kmalloc(sizeof(struct ext2_inode), ALLOC_KERN); - if (!inode) { - kfree(path_copy); - return -ENOMEM; - } - - memset(inode, 0, sizeof(struct ext2_inode)); - inode->mode = type; - inode->links_count = 1; - inode->size = 0; - inode->blocks = sizeof(struct ext2_inode) / fs->bdev->block_size; - inode->block[0] = block_no; - ext2_write_inode(fs, inode, inode_no); - kfree(inode); - - /* write the directory entry to the parent directory */ - struct ext2_dir_entry *entry = kmalloc(fs->block_size, ALLOC_DMA); - if (!entry) { - kfree(path_copy); - return -ENOMEM; - } - - size_t insert_size = sizeof(struct ext2_dir_entry) + strlen(base); - size_t num_blocks = (dir_inode->blocks * fs->bdev->block_size) / fs->block_size; - for (size_t parent_block = 0; parent_block < num_blocks; parent_block++) { - ext2_inode_read_block(fs, dir_inode, entry, parent_block); - - uint32_t rec_offset = 0; - uint32_t rem_len = 0; - while (rec_offset < fs->block_size) { - struct ext2_dir_entry *d_ent = (void *)entry + rec_offset; - - size_t t_len = d_ent->name_len + sizeof(struct ext2_dir_entry); - if (d_ent->rec_len > sizeof(struct ext2_dir_entry) + 255) { - if (t_len + rec_offset + insert_size < fs->block_size) { - rem_len = d_ent->rec_len - t_len; - d_ent->rec_len = t_len; - rec_offset += t_len; - - d_ent = (void *)entry + rec_offset; - } - } - - /* find a free entry of the right size */ - if (!d_ent->inode || !d_ent->rec_len || !d_ent->name_len) { - if (rec_offset + insert_size >= fs->block_size) - continue; - - /* write the entry */ - d_ent->inode = inode_no; - d_ent->rec_len = sizeof(struct ext2_dir_entry) + strlen(base); - d_ent->name_len = strlen(base); - d_ent->file_type = ino_type_to_dent(type); - memcpy(d_ent->name, base, strlen(base)); - - /* write the block back to disk */ - ext2_inode_write_block(fs, dir_inode, entry, parent_block); - - /* set the next rec_len to remaining space in the block */ - - if (rem_len) - d_ent->rec_len = rem_len; - - kfree(path_copy); - kfree(entry); - kfree(dir_inode); - - return inode_no; - } else { - rec_offset += d_ent->rec_len; - } - } - } - - /* if we get here, we need to allocate a new block for the directory */ - long dir_block = ext2_inode_alloc_block(fs, dir_inode, parent_ino_num); - if (dir_block < 0) { - kfree(path_copy); - kfree(entry); - kfree(dir_inode); - return dir_block; - } - - /* write the entry */ - entry->inode = inode_no; - entry->rec_len = fs->block_size; - entry->name_len = strlen(base); - entry->file_type = ino_type_to_dent(type); - memcpy(entry->name, base, strlen(base)); - - /* write the block back to disk */ - ext2_write_block(fs, entry, dir_block); - - /* write the parent inode back to disk */ - ext2_write_inode(fs, dir_inode, parent_ino_num); - - kfree(path_copy); - kfree(entry); - kfree(dir_inode); - - return inode_no; -} - -static int ext2_mkdir(struct fs *vfs, const char *path) -{ - long inode_no = ext2_creat(vfs, path, EXT2_INO_DIR); - - if (inode_no < 0) - return inode_no; - - /* allocate a block for the . and .. entries */ - struct ext2fs *fs = (struct ext2fs *)vfs->fs; - struct ext2_inode *inode = kmalloc(sizeof(struct ext2_inode), ALLOC_KERN); - if (!inode) - return -ENOMEM; - - long ret = ext2_read_inode(fs, inode, inode_no); - if (ret < 0) { - kfree(inode); - return ret; - } - - long block_no = ext2_inode_alloc_block(fs, inode, inode_no); - if (block_no < 0) { - kfree(inode); - return block_no; - } - - /* write the . and .. entries */ - - /* open the parent directory */ - char *path_copy = kzalloc(strlen(path) + 1, ALLOC_KERN); - if (!path_copy) { - kfree(inode); - return -ENOMEM; - } - - strcpy(path_copy, path); - - char *dir = dirname(path_copy); - long parent_ino_num; - if (dir == NULL) - parent_ino_num = EXT2_ROOT_INO; - else - parent_ino_num = ext2_inode_find_by_name(vfs->fs, dir); - - kfree(path_copy); - - if (parent_ino_num < 0) { - kfree(inode); - return parent_ino_num; - } - - /* create the entries */ - struct ext2_dir_entry *entry = kmalloc(fs->block_size, ALLOC_DMA); - if (!entry) { - kfree(inode); - return -ENOMEM; - } - - entry->inode = inode_no; - entry->rec_len = sizeof(struct ext2_dir_entry) + 1; - entry->name_len = 1; - entry->file_type = EXT2_DE_DIR; - entry->name[0] = '.'; - - struct ext2_dir_entry *entry2 = (void *)entry + entry->rec_len; - entry2->inode = parent_ino_num; - entry2->rec_len = fs->block_size - entry->rec_len; - entry2->name_len = 2; - entry2->file_type = EXT2_DE_DIR; - entry2->name[0] = '.'; - entry2->name[1] = '.'; - - /* write the block back to disk */ - ext2_write_block(fs, entry, block_no); - - kfree(inode); - kfree(entry); - - /* update block group descriptor used directories count */ - uint32_t group = (inode_no - 1) / fs->sb.inodes_per_group; - struct ext2_group_desc *bg = &fs->bgdt[group]; - bg->used_dirs_count++; - - ext2_write_bgdt(fs); - - return 0; -} - static int ext2_unlink(struct fs *vfs, const char *path) { struct ext2fs *fs = (struct ext2fs *)vfs->fs; @@ -1260,6 +982,7 @@ static int ext2_open_dir(struct fs *vfs, struct vnode *vnode) return num_entries; vnode->dirents = dirents; + vnode->num_dirents = num_entries; return 0; } @@ -1279,11 +1002,6 @@ static int ext2_read_vno(struct fs *vfs, struct vnode *vnode, void *buf, size_t return ext2_inode_read_block(fs, (struct ext2_inode *)&inode, buf, blockno); } -static int ext2_read_dirents(struct fs *vfs, struct vnode *vnode, struct dirent **out) -{ - return ext2_readdir(vnode, out); -} - struct fs *ext2_init_fs(struct block_device *bdev) { struct ext2fs *extfs = kmalloc(sizeof(*extfs), ALLOC_KERN); @@ -1330,11 +1048,9 @@ struct fs *ext2_init_fs(struct block_device *bdev) if (!ext2_ops) goto out; - ext2_ops->open = ext2_open_file; ext2_ops->read = ext2_read_file; ext2_ops->write = ext2_write_file; ext2_ops->readdir = ext2_readdir; - ext2_ops->mkdir = ext2_mkdir; ext2_ops->unlink = ext2_unlink; ext2_ops->open_vno = ext2_open_vno; ext2_ops->open_dir = ext2_open_dir; diff --git a/fs/vfs.c b/fs/vfs.c index 5590422..dceccd6 100644 --- a/fs/vfs.c +++ b/fs/vfs.c @@ -36,14 +36,79 @@ struct file *vfs_open(const char *pathname, int *err) if (pathname == NULL && (uintptr_t)err == 0x4) return file; - /* find the file */ - int result = rootfs->ops->open(rootfs, vnode, pathname); + /* start at root */ + struct fs *fs = rootfs; + struct vnode *cur_vnode = fs->root; - if (result < 0) { - *err = result; - goto out_2; + char *path_copy = kmalloc(strlen(pathname) + 1, ALLOC_KERN); + strcpy(path_copy, pathname); + + char *tok_last = NULL; + char *tok = strtok(path_copy, "/", &tok_last); + + struct dirent *entry = NULL; + + while (!strempty(tok)) { + bool found = false; + for (int i = 0; i < cur_vnode->num_dirents; i++) { + entry = &cur_vnode->dirents[i]; + if (strcmp(entry->name, tok) == 0) { + if (entry->vnode) { + /* go to next vnode */ + cur_vnode = entry->vnode; + spinlock_acquire(&cur_vnode->lock); + cur_vnode->refcount++; + spinlock_release(&cur_vnode->lock); + } else { + /* try to open the vnode */ + struct vnode *new_vnode = slab_alloc(vnode_slab); + if (!new_vnode) { + *err = -ENOMEM; + goto out_2; + } + memset(new_vnode, 0, sizeof(struct vnode)); + new_vnode->refcount = 1; + new_vnode->parent = cur_vnode; + + int res = fs->ops->open_vno(fs, new_vnode, entry->inode); + if (res < 0) { + *err = res; + slab_free(vnode_slab, new_vnode); + goto out_2; + } + + if (new_vnode->flags & VFS_VNO_DIR) { + int res = fs->ops->open_dir(vnode->fs, new_vnode); + if (res < 0) { + *err = res; + slab_free(vnode_slab, new_vnode); + goto out_2; + } + } + + cur_vnode = new_vnode; + entry->vnode = cur_vnode; + } + + found = true; + break; + } + } + + if (!found) { + *err = -ENOENT; + goto out_2; + } + + /* get next token */ + tok = strtok(NULL, "/", &tok_last); } + file->vnode = cur_vnode; + file->type = cur_vnode->flags & VFS_VTYPE_MASK; + file->size = vnode->size; + file->inode_num = vnode->ino_num; + return file; out_2: @@ -108,11 +173,6 @@ int unlink(const char *pathname) return rootfs->ops->unlink(rootfs, pathname); } -int mkdir(const char *pathname) -{ - return rootfs->ops->mkdir(rootfs, pathname); -} - DIR *opendir(const char *name) { int err; @@ -242,6 +302,7 @@ struct fs *vfs_mount(const char *dev, const char *mount_point) } root->dirents = dirents; + root->num_dirents = num_dirents; root->no_free = true; out: diff --git a/include/fs/vfs.h b/include/fs/vfs.h index 268224c..ac73d27 100644 --- a/include/fs/vfs.h +++ b/include/fs/vfs.h @@ -64,11 +64,16 @@ struct vnode { struct vnode *ptr; struct vnode *next; + struct vnode *parent; + struct dirent *dirents; /* for directories */ size_t num_dirents; bool mount_ptr; /* does this node point to a root of a mounted fs? */ bool no_free; /* permanently cached vnodes */ + + size_t refcount; + spinlock_t lock; }; struct dirent { @@ -76,6 +81,8 @@ struct dirent { uint16_t reclen; /* length of this record */ ftype_t type; /* type: file, dir, chrdev, blkdev, etc. */ char name[256]; + + struct vnode *vnode; /* we don't want duplicate vnodes */ }; typedef struct _DIR { @@ -112,9 +119,7 @@ struct file_descriptor { struct fs_ops { int (*read)(struct vnode *vnode, void *buf, size_t offset, size_t size); int (*write)(struct vnode *vnode, void *buf, size_t offset, size_t size); - int (*open)(struct fs *fs, struct vnode *vnode, const char *path); int (*readdir)(struct vnode *vnode, struct dirent **dir); - int (*mkdir)(struct fs *fs, const char *path); /* to be removed */ int (*unlink)(struct fs *fs, const char *path); /* change to vno, not path) */ int (*open_vno)(struct fs *vfs, struct vnode *out, ino_t ino_num); int (*read_vno)(struct fs *vfs, struct vnode *vnode, void *buf, size_t blockno); @@ -142,7 +147,6 @@ struct file *vfs_open(const char *pathname, int *err); int vfs_close(struct file *file); int unlink(const char *pathname); int statfd(struct file_descriptor *fdesc, struct statbuf *statbuf); -int mkdir(const char *pathname); DIR *opendir(const char *name); struct dirent *readdir(DIR *dir); diff --git a/init/cmd.c b/init/cmd.c index a43c65e..4a4add8 100644 --- a/init/cmd.c +++ b/init/cmd.c @@ -16,7 +16,6 @@ int kcmd_cat(int argc, char **argv); int kcmd_stat(int argc, char **argv); int kcmd_basename(int argc, char **argv); int kcmd_dirname(int argc, char **argv); -int kcmd_mkdir(int argc, char **argv); int kcmd_unlink(int argc, char **argv); int kcmd_elf(int argc, char **argv); int kcmd_exec(int argc, char **argv); @@ -33,9 +32,8 @@ struct kcmd { int (*func)(int argc, char **argv); }; -struct kcmd cmd_list[] = { KCMD_DECL(basename), KCMD_DECL(cat), KCMD_DECL(dirname), KCMD_DECL(clear), - KCMD_DECL(help), KCMD_DECL(ls), KCMD_DECL(mkdir), KCMD_DECL(stat), - KCMD_DECL(unlink), KCMD_DECL(elf), KCMD_DECL(exec) }; +struct kcmd cmd_list[] = { KCMD_DECL(basename), KCMD_DECL(cat), KCMD_DECL(dirname), KCMD_DECL(clear), KCMD_DECL(help), + KCMD_DECL(ls), KCMD_DECL(stat), KCMD_DECL(unlink), KCMD_DECL(elf), KCMD_DECL(exec) }; int kcmd_basename(int argc, char **argv) { @@ -150,22 +148,6 @@ int kcmd_ls(int argc, char **argv) return 0; } -int kcmd_mkdir(int argc, char **argv) -{ - if (!argv[1] || strempty(argv[1])) { - kprintf("Usage: mkdir \n"); - return 1; - } - - int ret = mkdir(argv[1]); - if (ret < 0) { - kprintf("Failed to mkdir %s: %s\n", argv[1], strerror(-ret)); - return 1; - } - - return 0; -} - int kcmd_stat(int argc, char **argv) { if (!argv[1] || strempty(argv[1])) {