77 lines
1.4 KiB
C
77 lines
1.4 KiB
C
/* SPDX-License-Identifier: GPL-2.0-only */
|
|
#include <kernel/mem.h>
|
|
#include <kernel/slab.h>
|
|
|
|
#include <fs/vfs.h>
|
|
|
|
#include <lib/string.h>
|
|
|
|
static mem_cache_t *inode_slab = NULL;
|
|
static mem_cache_t *dirent_slab = NULL;
|
|
|
|
dirent_t *inode_find_dirent(inode_t *inode, const char *name)
|
|
{
|
|
list_item_t *p = inode->dentries.head;
|
|
|
|
while (p) {
|
|
dirent_t *dirent = (void *)p->ptr;
|
|
if (!strncmp(name, dirent->name, dirent->name_len))
|
|
return dirent;
|
|
p = p->next;
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
dirent_t *dirent_create(const char *name)
|
|
{
|
|
dirent_t *ret = slab_alloc(dirent_slab);
|
|
|
|
if (!ret)
|
|
return NULL;
|
|
|
|
ret->rec_len = sizeof(dirent_t);
|
|
|
|
ret->name_len = strlen(name);
|
|
ret->name = strdup(name);
|
|
|
|
if (!ret->name) {
|
|
slab_free(dirent_slab, ret);
|
|
return NULL;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
void dirent_free(dirent_t *dirent)
|
|
{
|
|
kfree(dirent->name);
|
|
slab_free(dirent_slab, dirent);
|
|
}
|
|
|
|
inode_t *inode_create()
|
|
{
|
|
inode_t *ret = slab_alloc(inode_slab);
|
|
|
|
if (ret)
|
|
memset(ret, 0, sizeof(*ret));
|
|
|
|
rwlock_init(&ret->rwlock);
|
|
|
|
return ret;
|
|
}
|
|
|
|
void inode_free(inode_t *inode)
|
|
{
|
|
slab_free(inode_slab, inode);
|
|
}
|
|
|
|
void inode_init()
|
|
{
|
|
inode_slab = slab_create("inode_slab", sizeof(inode_t), 64 * KB, 0x8);
|
|
if (!inode_slab)
|
|
panic("failed to create inode_slab\n");
|
|
dirent_slab = slab_create("dentry_slab", sizeof(dirent_t), 64 * KB, 0x8);
|
|
if (!dirent_slab)
|
|
panic("failed to create dirent_slab\n");
|
|
}
|