diff --git a/amd64/include/kernel/spinlock.h b/amd64/include/kernel/spinlock.h index 718d10e..06ad9c2 100644 --- a/amd64/include/kernel/spinlock.h +++ b/amd64/include/kernel/spinlock.h @@ -3,10 +3,14 @@ #define _SPINLOCK_H_ #include +#include + +#define CLEANUP_SLOCK CLEANUP(cleanup_spinlock) typedef u32 spinlock_t; void spinlock_acquire(spinlock_t *lock); void spinlock_release(spinlock_t *lock); +void cleanup_spinlock(spinlock_t **lock_ptr); #endif /* _SPINLOCK_H_ */ diff --git a/amd64/kernel/spinlock.c b/amd64/kernel/spinlock.c index 560c290..809380b 100644 --- a/amd64/kernel/spinlock.c +++ b/amd64/kernel/spinlock.c @@ -18,3 +18,9 @@ void spinlock_release(spinlock_t *lock) { atomic_cmpxchg(lock, 1, 0); } + +void cleanup_spinlock(spinlock_t **lock_ptr) +{ + if (lock_ptr && *lock_ptr) + spinlock_release(*lock_ptr); +} diff --git a/fs/vfs/dcache.c b/fs/vfs/dcache.c new file mode 100644 index 0000000..cb091ca --- /dev/null +++ b/fs/vfs/dcache.c @@ -0,0 +1,90 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include +#include +#include +#include + +#define DC_TOMBSTONE ((struct dc_entry *)1) + +struct dc_entry dcache[DCACHE_SIZE]; +static spinlock_t dcache_locks[DCACHE_NLOCKS]; + +static unsigned long dcache_hash(struct dc_entry *parent, const char *name) +{ + unsigned long h = (unsigned long)parent; + while (*name) + h = ((h << 5) - h) + (unsigned char)*name++; + return h; +} + +static spinlock_t *dcache_lock_for(struct dc_entry *parent, const char *name) +{ + unsigned long h = dcache_hash(parent, name); + return &dcache_locks[h & (DCACHE_NLOCKS - 1)]; +} + +void dcache_init(void) +{ + for (int i = 0; i < DCACHE_SIZE; i++) + dcache[i].parent = NULL; + for (int i = 0; i < DCACHE_NLOCKS; i++) + dcache_locks[i] = 0; +} + +void dcache_insert(struct dc_entry *parent, dirent_t *dirent, const char *name) +{ + spinlock_t *lock = dcache_lock_for(parent, name); + unsigned long h = dcache_hash(parent, name); + + spinlock_acquire(lock); + for (int i = 0; i < DCACHE_SIZE; i++) { + unsigned int b = (h + i) & (DCACHE_SIZE - 1); + struct dc_entry *s = &dcache[b]; + + if (!s->parent || s->parent == DC_TOMBSTONE || s->dirent->refcount == 0) { + if (s->dirent) + dirent_free(s->dirent); + s->parent = parent; + s->dirent = dirent; + memcpy(s->d_name, name, FILE_NAME_MAX_LEN + 1); + spinlock_release(lock); + return; + } + } + spinlock_release(lock); +} + +void dcache_remove(struct dc_entry *d) +{ + spinlock_t *lock = dcache_lock_for(d->parent, d->d_name); + + spinlock_acquire(lock); + dirent_free(d->dirent); + d->dirent = NULL; + d->parent = DC_TOMBSTONE; + spinlock_release(lock); +} + +struct dc_entry *dcache_lookup(struct dc_entry *parent, const char *name) +{ + spinlock_t *lock = dcache_lock_for(parent, name); + unsigned long h = dcache_hash(parent, name); + struct dc_entry *result = NULL; + + spinlock_acquire(lock); + for (int i = 0; i < DCACHE_SIZE; i++) { + unsigned int b = (h + i) & (DCACHE_SIZE - 1); + struct dc_entry *s = &dcache[b]; + + if (!s->parent) + goto out; + + if (s->parent != DC_TOMBSTONE && s->parent == parent && !strcmp(s->d_name, name)) { + result = s; + goto out; + } + } +out: + spinlock_release(lock); + return result; +} diff --git a/fs/vfs/mount.c b/fs/vfs/mount.c index dc99c22..b3e64fe 100644 --- a/fs/vfs/mount.c +++ b/fs/vfs/mount.c @@ -40,15 +40,6 @@ struct mount *mount_find_child(struct mount *parent, dirent_t *dent) return NULL; } -static int load_inode(dirent_t *dent) -{ - if (dent->inode) - return 0; - - int res = dent->fs->fs_ops.open_inode(dent->fs, dent->ino_num, dent, &dent->inode); - return res; -} - static int mount_traverse(const char *path, struct mount **out_mount, dirent_t **out_dirent) { if (!root_mount) @@ -65,88 +56,7 @@ static int mount_traverse(const char *path, struct mount **out_mount, dirent_t * return 0; } - struct mount *cur = root_mount; - dirent_t *cur_dent = root_mount->fs->root_dirent; - - if (!cur_dent) - return -ENOENT; - - dirent_inc_ref(cur_dent); - - const char *p = path; - - while (*p != '\0') { - const char *comp_start = p; - while (*p != '/' && *p != '\0') - p++; - - size_t comp_len = p - comp_start; - if (comp_len > FILE_NAME_MAX_LEN) { - dirent_dec_ref(cur_dent); - return -ENAMETOOLONG; - } - - char name[FILE_NAME_MAX_LEN + 1]; - memcpy(name, comp_start, comp_len); - name[comp_len] = '\0'; - - while (*p == '/') - p++; - - bool is_last = (*p == '\0'); - - dirent_t *found = NULL; - int res = dirent_lookup(cur_dent, name, &found); - if (res < 0) { - dirent_dec_ref(cur_dent); - return res; - } - - res = load_inode(found); - if (res < 0) { - dirent_dec_ref(found); - dirent_dec_ref(cur_dent); - return res; - } - - if (is_last) { - dirent_dec_ref(cur_dent); - *out_mount = cur; - *out_dirent = found; - return 0; - } - - struct mount *child = NULL; - list_item_t *item = cur->children.head; - while (item) { - struct mount *m = item->ptr; - if (m->mountpoint == found) { - child = m; - break; - } - item = item->next; - } - - dirent_dec_ref(cur_dent); - - if (child) { - dirent_dec_ref(found); - cur = child; - cur_dent = cur->fs->root_dirent; - if (!cur_dent) - return -ENOENT; - dirent_inc_ref(cur_dent); - } else { - cur_dent = found; - } - } - - dirent_dec_ref(cur_dent); - *out_mount = cur; - *out_dirent = cur->fs->root_dirent; - if (*out_dirent) - dirent_inc_ref(*out_dirent); - return 0; + return vfs_openat(root_mount, root_mount->fs->root_dirent, path, out_dirent, out_mount, false); } int vfs_mount(const char *path, fs_t *fs, u64 flags) @@ -154,9 +64,9 @@ int vfs_mount(const char *path, fs_t *fs, u64 flags) if (!fs) return -EINVAL; - spinlock_acquire(&mount_lock); - if (!path) { + spinlock_acquire(&mount_lock); + if (root_mount) { spinlock_release(&mount_lock); return -EBUSY; @@ -183,17 +93,16 @@ int vfs_mount(const char *path, fs_t *fs, u64 flags) struct mount *parent = NULL; dirent_t *mp = NULL; int res = mount_traverse(path, &parent, &mp); - if (res < 0) { - spinlock_release(&mount_lock); + if (res < 0) return res; - } if ((mp->inode->mode & S_IFMT) != S_IFDIR) { dirent_dec_ref(mp); - spinlock_release(&mount_lock); return -ENOTDIR; } + spinlock_acquire(&mount_lock); + list_item_t *item = parent->children.head; while (item) { struct mount *m = item->ptr; @@ -236,15 +145,13 @@ int vfs_umount(const char *path) if (!path) return -EINVAL; - spinlock_acquire(&mount_lock); - struct mount *parent = NULL; dirent_t *mp = NULL; int res = mount_traverse(path, &parent, &mp); - if (res < 0) { - spinlock_release(&mount_lock); + if (res < 0) return res; - } + + spinlock_acquire(&mount_lock); struct mount *target = NULL; list_item_t *item = parent->children.head; diff --git a/fs/vfs/vfs.c b/fs/vfs/vfs.c index 43762f9..de6e00e 100644 --- a/fs/vfs/vfs.c +++ b/fs/vfs/vfs.c @@ -84,7 +84,7 @@ static int next_level(const char *path, dirent_t *dent, dirent_t **out_dirent) return -ENOENT; } -int vfs_openat(struct mount *mnt, dirent_t *in, const char *name, dirent_t **out) +int vfs_openat(struct mount *mnt, dirent_t *in, const char *name, dirent_t **out, struct mount **out_mnt, bool follow) { if (!in || !name || !out) return -EINVAL; @@ -99,6 +99,8 @@ int vfs_openat(struct mount *mnt, dirent_t *in, const char *name, dirent_t **out if (*path_optr == '\0') { *out = current_dir; + if (out_mnt) + *out_mnt = mnt; kfree(path_optr); return 0; } @@ -119,8 +121,15 @@ int vfs_openat(struct mount *mnt, dirent_t *in, const char *name, dirent_t **out dirent_dec_ref(current_dir); current_dir = next_dir; + cur_path += res; + + while (*cur_path == '/') + cur_path++; + + bool at_end = (*cur_path == '\0'); + struct mount *child = mount_find_child(cur_mount, current_dir); - if (child) { + if (child && (!at_end || follow)) { dirent_dec_ref(current_dir); cur_mount = child; current_dir = child->fs->root_dirent; @@ -131,13 +140,10 @@ int vfs_openat(struct mount *mnt, dirent_t *in, const char *name, dirent_t **out dirent_inc_ref(current_dir); } - cur_path += res; - - while (*cur_path == '/') - cur_path++; - - if (*cur_path == '\0') { + if (at_end) { *out = current_dir; + if (out_mnt) + *out_mnt = cur_mount; ret = 0; goto out; } @@ -157,7 +163,7 @@ int vfs_open(const char *abs_path, dirent_t **out) if (!root || !root->fs || !root->fs->root_dirent) return -ENOENT; - return vfs_openat(root, root->fs->root_dirent, abs_path, out); + return vfs_openat(root, root->fs->root_dirent, abs_path, out, NULL, true); } void vfs_close(dirent_t *dent) diff --git a/include/fs/dcache.h b/include/fs/dcache.h new file mode 100644 index 0000000..d9e0125 --- /dev/null +++ b/include/fs/dcache.h @@ -0,0 +1,32 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _DCACHE_H_ +#define _DCACHE_H_ + +#include +#include + +#include + +#define DCACHE_SHIFT 10 +#define DCACHE_SIZE (1 << DCACHE_SHIFT) + +#define DCACHE_LOCK_SHIFT 4 +#define DCACHE_NLOCKS (1 << DCACHE_LOCK_SHIFT) + +struct dc_entry { + struct dc_entry *parent; + struct mount *mount; + spinlock_t lock; + list_t children; + dirent_t *dirent; + char d_name[FILE_NAME_MAX_LEN + 1]; +}; + +extern struct dc_entry dcache[DCACHE_SIZE]; + +void dcache_init(void); +void dcache_insert(struct dc_entry *parent, dirent_t *dirent, const char *name); +void dcache_remove(struct dc_entry *d); +struct dc_entry *dcache_lookup(struct dc_entry *parent, const char *name); + +#endif /* _DCACHE_H_ */ diff --git a/include/fs/vfs.h b/include/fs/vfs.h index 5672042..aa94fb0 100644 --- a/include/fs/vfs.h +++ b/include/fs/vfs.h @@ -107,7 +107,7 @@ void vfs_init(void); void vfs_register(struct fs_driver *fs); int vfs_open(const char *abs_path, dirent_t **out); -int vfs_openat(struct mount *mnt, dirent_t *in, const char *name, dirent_t **out); +int vfs_openat(struct mount *mnt, dirent_t *in, const char *name, dirent_t **out, struct mount **out_mnt, bool follow); void vfs_close(dirent_t *dent); int vfs_readdir(dirent_t *dir, struct dir_context *ctx, dirent_t **out); int vfs_creat(dirent_t *parent, const char *name, u64 mode, int flags, dirent_t **out); diff --git a/include/kernel/common.h b/include/kernel/common.h index d6fd405..7015e57 100644 --- a/include/kernel/common.h +++ b/include/kernel/common.h @@ -31,6 +31,8 @@ #define DRIVER_ENTRY(fn) static void (*__initcall_##fn)(void) __attribute((used, section(".initcall"))) = fn #define DRIVER_ENTRY_PRIORITY(fn, pr) static void (*__initcall_##fn)(void) __attribute((used, section(".initcall." #pr))) = fn +#define CLEANUP(fn) __attribute__((__cleanup__(fn))) + void yield(); void _panic(); diff --git a/include/kernel/slab.h b/include/kernel/slab.h index 6ab93dc..7392629 100644 --- a/include/kernel/slab.h +++ b/include/kernel/slab.h @@ -8,7 +8,7 @@ #define ALLOC_KERN 0x1 #define ALLOC_USER 0x2 -#define CLEANUP_KFREE __attribute__((cleanup(_cleanup_kfree))) +#define CLEANUP_KFREE CLEANUP(cleanup_kfree) typedef struct _mem_cache_t { char *name; @@ -31,6 +31,7 @@ void *kalloc(size_t size, u64 flags); void *kzalloc(size_t size, u64 flags); void *krealloc(void *old, size_t size, u64 flags); void kfree(void *ptr); +void cleanup_kfree(void **ptr); void kalloc_init(); diff --git a/include/lib/errno.h b/include/lib/errno.h index 82343df..df7b3c1 100644 --- a/include/lib/errno.h +++ b/include/lib/errno.h @@ -2,24 +2,23 @@ #ifndef _ERRNO_H_ #define _ERRNO_H_ - #define ENOMEM 1 #define ENOENT 2 #define EINVAL 3 #define EBADF 4 #define ENOTDIR 5 #define EISDIR 6 -#define ENOSPC 7 +#define ENOSPC 7 #define EEXIST 8 #define ENOEXEC 9 #define EFAULT 10 #define EAGAIN 11 #define EIO 12 #define EBUSY 13 -#define ENOSYS 13 -#define ESYSCALLBLK 14 -#define ENAMETOOLONG 15 -#define EROFS 16 +#define ENOSYS 14 +#define ESYSCALLBLK 15 +#define ENAMETOOLONG 16 +#define EROFS 17 #ifndef __ASM__ diff --git a/mem/slab.c b/mem/slab.c index 0c301c0..d18a60c 100644 --- a/mem/slab.c +++ b/mem/slab.c @@ -106,7 +106,7 @@ void slab_free(mem_cache_t *cache, void *ptr) spinlock_release(&cache->lock); } -void _cleanup_kfree(void **ptr) +void cleanup_kfree(void **ptr) { if (ptr && *ptr) kfree(ptr); diff --git a/scripts/Makefile.limine b/scripts/Makefile.limine index ebb0a40..9dafe0c 100644 --- a/scripts/Makefile.limine +++ b/scripts/Makefile.limine @@ -35,7 +35,7 @@ limine/limine: $(INITRAMFS): initramfs/ $(Q)echo " CPIO $@" - $(Q)mkdir -p initramfs/ + $(Q)mkdir -p initramfs/dev $(Q)cd initramfs/ && find . | cpio -oH newc > ../$(INITRAMFS) .PHONY += run @@ -46,7 +46,6 @@ run: edk2-ovmf $(IMG) -drive if=pflash,unit=0,format=raw,file=edk2-ovmf/ovmf-code-$(ARCH).fd,readonly=on \ -accel kvm \ -hda $(IMG) \ - -hdb test_drive.hdd \ -nographic \ -serial mon:stdio \ $(QEMUFLAGS) @@ -58,7 +57,6 @@ debug: edk2-ovmf $(IMG) -M q35 \ -drive if=pflash,unit=0,format=raw,file=edk2-ovmf/ovmf-code-$(ARCH).fd,readonly=on \ -hda $(IMG) \ - -hdb test_drive.hdd \ -nographic \ -serial mon:stdio \ $(QEMUFLAGS) diff --git a/usr.bin/init/.gitignore b/usr.bin/init/.gitignore index 599b612..b1b7161 100644 --- a/usr.bin/init/.gitignore +++ b/usr.bin/init/.gitignore @@ -1 +1 @@ -utest +init diff --git a/usr.bin/init/init b/usr.bin/init/init deleted file mode 100755 index 32b5c06..0000000 Binary files a/usr.bin/init/init and /dev/null differ