vfs: fix various logical errors in the vfs
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
parent
ab15b88cd6
commit
eb41b17fea
6 changed files with 146 additions and 45 deletions
112
fs/vfs/inode.c
112
fs/vfs/inode.c
|
|
@ -1,21 +1,22 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/mem.h>
|
||||
#include <kernel/slab.h>
|
||||
#include <kernel/atomic.h>
|
||||
|
||||
#include <fs/vfs.h>
|
||||
|
||||
#include <lib/string.h>
|
||||
#include <lib/errno.h>
|
||||
|
||||
static mem_cache_t *inode_slab = NULL;
|
||||
static mem_cache_t *dirent_slab = NULL;
|
||||
|
||||
int inode_populate_dirents(inode_t *inode)
|
||||
{
|
||||
/* write_lock(&inode-rwlock) is held by caller */
|
||||
/* write_lock(&inode->rwlock) is held by caller */
|
||||
|
||||
if (inode->dirents.head != NULL) {
|
||||
return -EINVAL;
|
||||
}
|
||||
if (inode->dirents.head != NULL)
|
||||
return 0;
|
||||
|
||||
dirent_t *dents;
|
||||
ssize_t n = inode->i_ops.get_dents(inode, &dents);
|
||||
|
|
@ -25,19 +26,63 @@ int inode_populate_dirents(inode_t *inode)
|
|||
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
dirent_t *dent = dents + i;
|
||||
dirent_t *insert = kalloc(sizeof(dirent_t), ALLOC_KERN);
|
||||
if (!insert) {
|
||||
kerr("failed to allocate dirent! memory leak likely\n");
|
||||
return -ENOMEM;
|
||||
dirent_t *insert = dirent_create(dent->name);
|
||||
if (!insert)
|
||||
goto cleanup;
|
||||
|
||||
insert->ino_num = dent->ino_num;
|
||||
insert->rec_len = dent->rec_len;
|
||||
insert->name_len = dent->name_len;
|
||||
insert->flags = dent->flags;
|
||||
insert->type = dent->type;
|
||||
insert->fs = dent->fs;
|
||||
|
||||
if (dent->inode) {
|
||||
inode_inc_ref_lock(dent->inode);
|
||||
insert->inode = dent->inode;
|
||||
}
|
||||
|
||||
memcpy(insert, dent, sizeof(dirent_t));
|
||||
list_insert(&inode->dirents, insert);
|
||||
if (!list_insert(&inode->dirents, insert)) {
|
||||
if (insert->inode)
|
||||
inode_dec_ref_lock(insert->inode);
|
||||
dirent_free(insert);
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
|
||||
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_lock(dent->inode);
|
||||
dirent_free(dent);
|
||||
item = next;
|
||||
}
|
||||
}
|
||||
kfree(dents);
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
int inode_find_dirent_locked(inode_t *inode, const char *name, dirent_t **out)
|
||||
{
|
||||
list_item_t *item = inode->dirents.head;
|
||||
while (item) {
|
||||
dirent_t *dent = item->ptr;
|
||||
if (!strcmp(dent->name, name)) {
|
||||
dirent_inc_ref_lock(dent);
|
||||
*out = dent;
|
||||
return 0;
|
||||
}
|
||||
item = item->next;
|
||||
}
|
||||
return -ENOENT;
|
||||
}
|
||||
|
||||
int inode_find_dirent(inode_t *inode, const char *name, dirent_t **out)
|
||||
|
|
@ -45,7 +90,10 @@ int inode_find_dirent(inode_t *inode, const char *name, dirent_t **out)
|
|||
if (inode->mode != INODE_DIRECTORY)
|
||||
return -ENOTDIR;
|
||||
|
||||
read_lock(&inode->rwlock);
|
||||
|
||||
if (inode->dirents.head == NULL) {
|
||||
read_unlock(&inode->rwlock);
|
||||
write_lock(&inode->rwlock);
|
||||
if (inode->dirents.head == NULL) {
|
||||
ssize_t res = inode_populate_dirents(inode);
|
||||
|
|
@ -55,22 +103,12 @@ int inode_find_dirent(inode_t *inode, const char *name, dirent_t **out)
|
|||
} else {
|
||||
write_unlock(&inode->rwlock);
|
||||
}
|
||||
read_lock(&inode->rwlock);
|
||||
}
|
||||
|
||||
list_item_t *item = inode->dirents.head;
|
||||
while (item) {
|
||||
dirent_t *dent = item->ptr;
|
||||
if (!strcmp(dent->name, name)) {
|
||||
/* found */
|
||||
dirent_inc_ref_lock(dent);
|
||||
*out = dent;
|
||||
|
||||
return 0;
|
||||
}
|
||||
item = item->next;
|
||||
}
|
||||
|
||||
return -ENOENT;
|
||||
int ret = inode_find_dirent_locked(inode, name, out);
|
||||
read_unlock(&inode->rwlock);
|
||||
return ret;
|
||||
}
|
||||
|
||||
inode_t *inode_create()
|
||||
|
|
@ -90,6 +128,8 @@ inode_t *inode_create()
|
|||
|
||||
void inode_free(inode_t *inode)
|
||||
{
|
||||
/* caller must hold write_lock(&inode->rwlock) */
|
||||
|
||||
if (inode->dirents.head) {
|
||||
list_item_t *item = inode->dirents.head;
|
||||
while (item) {
|
||||
|
|
@ -105,16 +145,17 @@ void inode_free(inode_t *inode)
|
|||
|
||||
void inode_inc_ref(inode_t *inode)
|
||||
{
|
||||
inode->refcount += 1;
|
||||
if (!inode)
|
||||
return;
|
||||
__sync_add_and_fetch(&inode->refcount, 1);
|
||||
}
|
||||
|
||||
void inode_dec_ref(inode_t *inode)
|
||||
{
|
||||
if (!inode)
|
||||
return;
|
||||
inode->refcount -= 1;
|
||||
|
||||
if (inode->refcount == 0) {
|
||||
if (__sync_sub_and_fetch(&inode->refcount, 1) == 0) {
|
||||
inode_free(inode);
|
||||
return;
|
||||
}
|
||||
|
|
@ -127,11 +168,12 @@ void dirent_inc_ref(dirent_t *dent)
|
|||
|
||||
void dirent_dec_ref(dirent_t *dent)
|
||||
{
|
||||
if (dent->refcount == 0)
|
||||
return;
|
||||
dent->refcount -= 1;
|
||||
if (dent->refcount == 0) {
|
||||
inode_dec_ref_lock(dent->inode);
|
||||
dirent_free(dent);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -145,8 +187,18 @@ void dirent_inc_ref_lock(dirent_t *dent)
|
|||
void dirent_dec_ref_lock(dirent_t *dent)
|
||||
{
|
||||
spinlock_acquire(&dent->lock);
|
||||
dirent_dec_ref(dent);
|
||||
if (dent->refcount == 0) {
|
||||
spinlock_release(&dent->lock);
|
||||
return;
|
||||
}
|
||||
dent->refcount -= 1;
|
||||
bool freed = (dent->refcount == 0);
|
||||
spinlock_release(&dent->lock);
|
||||
|
||||
if (freed) {
|
||||
inode_dec_ref_lock(dent->inode);
|
||||
dirent_free(dent);
|
||||
}
|
||||
}
|
||||
|
||||
void inode_inc_ref_lock(inode_t *inode)
|
||||
|
|
|
|||
Loading…
Reference in a new issue