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 0bed914..b3e64fe 100644 --- a/fs/vfs/mount.c +++ b/fs/vfs/mount.c @@ -56,8 +56,7 @@ 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); + 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) 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_ */