lock: merge spinlock and mtx locks into one

Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
Michael Shaw 2026-08-13 14:19:10 -04:00
commit 2076f1dae1
Signed by: michaelsshaw
GPG key ID: 5A5E0FB20708A9E7
32 changed files with 158 additions and 169 deletions

View file

@ -4,7 +4,7 @@
#include <kernel/common.h>
#include <kernel/block.h>
#include <kernel/spinlock.h>
#include <kernel/lock.h>
#include <fs/vfs.h>

View file

@ -7,7 +7,7 @@
#define DC_TOMBSTONE ((struct dc_entry *)1)
struct dc_entry dcache[DCACHE_SIZE];
static spinlock_t dcache_locks[DCACHE_NLOCKS];
static lock_t dcache_locks[DCACHE_NLOCKS];
static u64 dcache_hash(struct dc_entry *parent, const char *name)
{
@ -17,7 +17,7 @@ static u64 dcache_hash(struct dc_entry *parent, const char *name)
return h;
}
static spinlock_t *dcache_lock_for(struct dc_entry *parent, const char *name)
static lock_t *dcache_lock_for(struct dc_entry *parent, const char *name)
{
u64 h = dcache_hash(parent, name);
return &dcache_locks[h & (DCACHE_NLOCKS - 1)];
@ -33,10 +33,10 @@ void dcache_init(void)
void dcache_insert(struct dc_entry *parent, dirent_t *dirent, const char *name)
{
spinlock_t *lock = dcache_lock_for(parent, name);
lock_t *lock = dcache_lock_for(parent, name);
u64 h = dcache_hash(parent, name);
spinlock_acquire(lock);
spin_lock(lock);
for (int i = 0; i < DCACHE_SIZE; i++) {
u32 b = (h + i) & (DCACHE_SIZE - 1);
struct dc_entry *s = &dcache[b];
@ -47,27 +47,27 @@ void dcache_insert(struct dc_entry *parent, dirent_t *dirent, const char *name)
s->parent = parent;
s->dirent = dirent;
memcpy(s->d_name, name, FILE_NAME_MAX_LEN + 1);
spinlock_release(lock);
spin_unlock(lock);
return;
}
}
spinlock_release(lock);
spin_unlock(lock);
}
void dcache_remove(struct dc_entry *d)
{
spinlock_t *lock = dcache_lock_for(d->parent, d->d_name);
lock_t *lock = dcache_lock_for(d->parent, d->d_name);
spinlock_acquire(lock);
spin_lock(lock);
dirent_free(d->dirent);
d->dirent = NULL;
d->parent = DC_TOMBSTONE;
spinlock_release(lock);
spin_unlock(lock);
}
struct dc_entry *dcache_lookup(struct dc_entry *parent, const char *name)
{
CLEANUP_SLOCK spinlock_t *lock = dcache_lock_for(parent, name);
CLEANUP_SLOCK lock_t *lock = dcache_lock_for(parent, name);
u64 h = dcache_hash(parent, name);
struct dc_entry *result = NULL;

View file

@ -52,19 +52,18 @@ int dirent_populate_children(dirent_t *dir)
kfree(dents);
return 0;
cleanup:
{
list_item_t *item = inode->dirents.head;
while (item) {
dirent_t *dent = item->ptr;
list_item_t *next = item->next;
list_remove(item);
if (dent->inode)
inode_dec_ref(dent->inode);
dirent_free(dent);
item = next;
}
cleanup: {
list_item_t *item = inode->dirents.head;
while (item) {
dirent_t *dent = item->ptr;
list_item_t *next = item->next;
list_remove(item);
if (dent->inode)
inode_dec_ref(dent->inode);
dirent_free(dent);
item = next;
}
}
list_unlock(&inode->dirents);
write_unlock(&inode->rwlock);
kfree(dents);
@ -162,21 +161,21 @@ void inode_dec_ref(inode_t *inode)
void dirent_inc_ref(dirent_t *dent)
{
spinlock_acquire(&dent->lock);
spin_lock(&dent->lock);
dent->refcount += 1;
spinlock_release(&dent->lock);
spin_unlock(&dent->lock);
}
void dirent_dec_ref(dirent_t *dent)
{
spinlock_acquire(&dent->lock);
spin_lock(&dent->lock);
if (dent->refcount == 0) {
spinlock_release(&dent->lock);
spin_unlock(&dent->lock);
return;
}
dent->refcount -= 1;
bool freed = (dent->refcount == 0);
spinlock_release(&dent->lock);
spin_unlock(&dent->lock);
if (freed) {
inode_dec_ref(dent->inode);

View file

@ -8,7 +8,7 @@
#include <lib/string.h>
static struct mount *root_mount = NULL;
static spinlock_t mount_lock = 0;
static lock_t mount_lock = 0;
fs_t *vfs_get_root(void)
{
@ -24,19 +24,19 @@ struct mount *vfs_get_root_mount(void)
struct mount *mount_find_child(struct mount *parent, dirent_t *dent)
{
spinlock_acquire(&mount_lock);
spin_lock(&mount_lock);
list_item_t *item = parent->children.head;
while (item) {
struct mount *m = item->ptr;
if (m->mountpoint == dent) {
spinlock_release(&mount_lock);
spin_unlock(&mount_lock);
return m;
}
item = item->next;
}
spinlock_release(&mount_lock);
spin_unlock(&mount_lock);
return NULL;
}
@ -65,16 +65,16 @@ int vfs_mount(const char *path, fs_t *fs, u64 flags)
return -EINVAL;
if (!path) {
spinlock_acquire(&mount_lock);
spin_lock(&mount_lock);
if (root_mount) {
spinlock_release(&mount_lock);
spin_unlock(&mount_lock);
return -EBUSY;
}
struct mount *m = kzalloc(sizeof(*m), ALLOC_KERN);
if (!m) {
spinlock_release(&mount_lock);
spin_unlock(&mount_lock);
return -ENOMEM;
}
@ -86,7 +86,7 @@ int vfs_mount(const char *path, fs_t *fs, u64 flags)
list_create(&m->children);
root_mount = m;
spinlock_release(&mount_lock);
spin_unlock(&mount_lock);
return 0;
}
@ -101,14 +101,14 @@ int vfs_mount(const char *path, fs_t *fs, u64 flags)
return -ENOTDIR;
}
spinlock_acquire(&mount_lock);
spin_lock(&mount_lock);
list_item_t *item = parent->children.head;
while (item) {
struct mount *m = item->ptr;
if (m->mountpoint == mp) {
dirent_dec_ref(mp);
spinlock_release(&mount_lock);
spin_unlock(&mount_lock);
return -EBUSY;
}
item = item->next;
@ -117,7 +117,7 @@ int vfs_mount(const char *path, fs_t *fs, u64 flags)
struct mount *m = kzalloc(sizeof(*m), ALLOC_KERN);
if (!m) {
dirent_dec_ref(mp);
spinlock_release(&mount_lock);
spin_unlock(&mount_lock);
return -ENOMEM;
}
@ -134,11 +134,11 @@ int vfs_mount(const char *path, fs_t *fs, u64 flags)
if (!m->child_link) {
dirent_dec_ref(mp);
kfree(m);
spinlock_release(&mount_lock);
spin_unlock(&mount_lock);
return -ENOMEM;
}
spinlock_release(&mount_lock);
spin_unlock(&mount_lock);
return 0;
}
@ -153,7 +153,7 @@ int vfs_umount(const char *path)
if (res < 0)
return res;
spinlock_acquire(&mount_lock);
spin_lock(&mount_lock);
struct mount *target = NULL;
list_item_t *item = parent->children.head;
@ -169,17 +169,17 @@ int vfs_umount(const char *path)
dirent_dec_ref(mp);
if (!target) {
spinlock_release(&mount_lock);
spin_unlock(&mount_lock);
return -EINVAL;
}
if (target == root_mount) {
spinlock_release(&mount_lock);
spin_unlock(&mount_lock);
return -EINVAL;
}
if (target->children.head) {
spinlock_release(&mount_lock);
spin_unlock(&mount_lock);
return -EBUSY;
}
@ -189,6 +189,6 @@ int vfs_umount(const char *path)
dirent_dec_ref(target->mountpoint);
kfree(target);
spinlock_release(&mount_lock);
spin_unlock(&mount_lock);
return 0;
}

View file

@ -76,7 +76,7 @@ static int sys_open(const char *path, int flags)
if (res < 0)
return res;
spinlock_acquire(&thread->fd_lock);
spin_lock(&thread->fd_lock);
int fdno = -1;
@ -109,7 +109,7 @@ static int sys_open(const char *path, int flags)
fdno = osize;
}
spinlock_release(&thread->fd_lock);
spin_unlock(&thread->fd_lock);
return fdno;
}
@ -125,12 +125,12 @@ static int sys_close(int fd)
if (!f->dirent)
return -EBADF;
spinlock_acquire(&thread->fd_lock);
spin_lock(&thread->fd_lock);
vfs_close(f->dirent);
memset(f, 0, sizeof(struct file));
spinlock_acquire(&thread->fd_lock);
spin_lock(&thread->fd_lock);
return 0;
}

View file

@ -58,20 +58,20 @@ static int next_level(const char *path, dirent_t *dent, dirent_t **out_dirent)
if (res < 0)
return -ENOENT;
spinlock_acquire(&dirent->lock);
spin_lock(&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);
spin_unlock(&dirent->lock);
dirent_dec_ref(dirent);
return res;
}
}
spinlock_release(&dirent->lock);
spin_unlock(&dirent->lock);
*out_dirent = dirent;
return i;