dcache: add initial dcache
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
parent
27c3ffa8b1
commit
5ae890b3b0
3 changed files with 123 additions and 2 deletions
90
fs/vfs/dcache.c
Normal file
90
fs/vfs/dcache.c
Normal file
|
|
@ -0,0 +1,90 @@
|
||||||
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||||
|
#include <kernel/common.h>
|
||||||
|
#include <fs/dcache.h>
|
||||||
|
#include <fs/inode.h>
|
||||||
|
#include <lib/string.h>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
}
|
||||||
|
|
@ -56,8 +56,7 @@ static int mount_traverse(const char *path, struct mount **out_mount, dirent_t *
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return vfs_openat(root_mount, root_mount->fs->root_dirent, path,
|
return vfs_openat(root_mount, root_mount->fs->root_dirent, path, out_dirent, out_mount, false);
|
||||||
out_dirent, out_mount, false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int vfs_mount(const char *path, fs_t *fs, u64 flags)
|
int vfs_mount(const char *path, fs_t *fs, u64 flags)
|
||||||
|
|
|
||||||
32
include/fs/dcache.h
Normal file
32
include/fs/dcache.h
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||||
|
#ifndef _DCACHE_H_
|
||||||
|
#define _DCACHE_H_
|
||||||
|
|
||||||
|
#include <kernel/spinlock.h>
|
||||||
|
#include <fs/vfs.h>
|
||||||
|
|
||||||
|
#include <lib/list.h>
|
||||||
|
|
||||||
|
#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_ */
|
||||||
Loading…
Reference in a new issue