diff --git a/amd64/include/kernel/spinlock.h b/amd64/include/kernel/spinlock.h index 06ad9c2..718d10e 100644 --- a/amd64/include/kernel/spinlock.h +++ b/amd64/include/kernel/spinlock.h @@ -3,14 +3,10 @@ #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 809380b..560c290 100644 --- a/amd64/kernel/spinlock.c +++ b/amd64/kernel/spinlock.c @@ -18,9 +18,3 @@ 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 deleted file mode 100644 index cb091ca..0000000 --- a/fs/vfs/dcache.c +++ /dev/null @@ -1,90 +0,0 @@ -/* 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 b3e64fe..dc99c22 100644 --- a/fs/vfs/mount.c +++ b/fs/vfs/mount.c @@ -40,6 +40,15 @@ 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) @@ -56,7 +65,88 @@ static int mount_traverse(const char *path, struct mount **out_mount, dirent_t * return 0; } - return vfs_openat(root_mount, root_mount->fs->root_dirent, path, out_dirent, out_mount, false); + 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; } int vfs_mount(const char *path, fs_t *fs, u64 flags) @@ -64,9 +154,9 @@ int vfs_mount(const char *path, fs_t *fs, u64 flags) if (!fs) return -EINVAL; - if (!path) { - spinlock_acquire(&mount_lock); + spinlock_acquire(&mount_lock); + if (!path) { if (root_mount) { spinlock_release(&mount_lock); return -EBUSY; @@ -93,16 +183,17 @@ 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) + if (res < 0) { + spinlock_release(&mount_lock); 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; @@ -145,13 +236,15 @@ 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) + if (res < 0) { + spinlock_release(&mount_lock); 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 de6e00e..43762f9 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, struct mount **out_mnt, bool follow) +int vfs_openat(struct mount *mnt, dirent_t *in, const char *name, dirent_t **out) { if (!in || !name || !out) return -EINVAL; @@ -99,8 +99,6 @@ 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; } @@ -121,15 +119,8 @@ 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 && (!at_end || follow)) { + if (child) { dirent_dec_ref(current_dir); cur_mount = child; current_dir = child->fs->root_dirent; @@ -140,10 +131,13 @@ int vfs_openat(struct mount *mnt, dirent_t *in, const char *name, dirent_t **out dirent_inc_ref(current_dir); } - if (at_end) { + cur_path += res; + + while (*cur_path == '/') + cur_path++; + + if (*cur_path == '\0') { *out = current_dir; - if (out_mnt) - *out_mnt = cur_mount; ret = 0; goto out; } @@ -163,7 +157,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, NULL, true); + return vfs_openat(root, root->fs->root_dirent, abs_path, out); } void vfs_close(dirent_t *dent) diff --git a/include/fs/dcache.h b/include/fs/dcache.h deleted file mode 100644 index d9e0125..0000000 --- a/include/fs/dcache.h +++ /dev/null @@ -1,32 +0,0 @@ -/* 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 aa94fb0..5672042 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, struct mount **out_mnt, bool follow); +int vfs_openat(struct mount *mnt, dirent_t *in, const char *name, dirent_t **out); 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 7015e57..d6fd405 100644 --- a/include/kernel/common.h +++ b/include/kernel/common.h @@ -31,8 +31,6 @@ #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 7392629..6ab93dc 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 CLEANUP(cleanup_kfree) +#define CLEANUP_KFREE __attribute__((cleanup(_cleanup_kfree))) typedef struct _mem_cache_t { char *name; @@ -31,7 +31,6 @@ 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 df7b3c1..82343df 100644 --- a/include/lib/errno.h +++ b/include/lib/errno.h @@ -2,23 +2,24 @@ #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 14 -#define ESYSCALLBLK 15 -#define ENAMETOOLONG 16 -#define EROFS 17 +#define ENOSYS 13 +#define ESYSCALLBLK 14 +#define ENAMETOOLONG 15 +#define EROFS 16 #ifndef __ASM__ diff --git a/mem/slab.c b/mem/slab.c index d18a60c..0c301c0 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 9dafe0c..ebb0a40 100644 --- a/scripts/Makefile.limine +++ b/scripts/Makefile.limine @@ -35,7 +35,7 @@ limine/limine: $(INITRAMFS): initramfs/ $(Q)echo " CPIO $@" - $(Q)mkdir -p initramfs/dev + $(Q)mkdir -p initramfs/ $(Q)cd initramfs/ && find . | cpio -oH newc > ../$(INITRAMFS) .PHONY += run @@ -46,6 +46,7 @@ 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) @@ -57,6 +58,7 @@ 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 b1b7161..599b612 100644 --- a/usr.bin/init/.gitignore +++ b/usr.bin/init/.gitignore @@ -1 +1 @@ -init +utest diff --git a/usr.bin/init/init b/usr.bin/init/init new file mode 100755 index 0000000..32b5c06 Binary files /dev/null and b/usr.bin/init/init differ