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)
|
||||
|
|
|
|||
|
|
@ -44,6 +44,7 @@ int ramfs_create(u64 flags, fs_t **out)
|
|||
fs->root_dirent = root;
|
||||
root->type = S_IFDIR;
|
||||
root->fs = fs;
|
||||
inode_inc_ref(root_inode);
|
||||
root->inode = root_inode;
|
||||
|
||||
*out = fs;
|
||||
|
|
@ -76,7 +77,7 @@ static int alloc_inode(fs_t *fs, u64 mode, inode_t **out)
|
|||
static int free_inode(fs_t *fs, inode_t *inode)
|
||||
{
|
||||
kfree(inode->data);
|
||||
inode_free(inode);
|
||||
inode_dec_ref_lock(inode);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -123,12 +124,16 @@ static ssize_t write(inode_t *inode, char *buf, size_t size, size_t offset)
|
|||
}
|
||||
|
||||
if (inode->file_size < size + offset) {
|
||||
inode->data = krealloc(inode->data, size + offset, ALLOC_KERN);
|
||||
if (!inode->data) {
|
||||
size_t new_size = size + offset;
|
||||
u8 *new_data = krealloc(inode->data, new_size, ALLOC_KERN);
|
||||
if (!new_data) {
|
||||
ret = -ENOMEM;
|
||||
goto out;
|
||||
}
|
||||
inode->file_size = size + offset;
|
||||
if (offset > inode->file_size)
|
||||
memset(new_data + inode->file_size, 0, offset - inode->file_size);
|
||||
inode->data = new_data;
|
||||
inode->file_size = new_size;
|
||||
}
|
||||
|
||||
memcpy(inode->data + offset, buf, size);
|
||||
|
|
@ -152,7 +157,11 @@ static int link(dirent_t *in, inode_t *to, const char *name, dirent_t **out)
|
|||
new->inode = to;
|
||||
new->fs = in->fs;
|
||||
|
||||
list_insert(&in->inode->dirents, new);
|
||||
if (!list_insert(&in->inode->dirents, new)) {
|
||||
inode_dec_ref_lock(to);
|
||||
dirent_free(new);
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
*out = new;
|
||||
|
||||
|
|
@ -176,6 +185,9 @@ static ssize_t get_dents(inode_t *parent, dirent_t **dents)
|
|||
|
||||
/* now copy */
|
||||
dirent_t *arr = kalloc(n * sizeof(dirent_t), ALLOC_KERN);
|
||||
if (!arr)
|
||||
return -ENOMEM;
|
||||
|
||||
item = parent->dirents.head;
|
||||
n = 0;
|
||||
while (item) {
|
||||
|
|
|
|||
45
fs/vfs/vfs.c
45
fs/vfs/vfs.c
|
|
@ -14,12 +14,21 @@ static char *path_trim(const char *name)
|
|||
if (!*name)
|
||||
return NULL;
|
||||
|
||||
/* skip leading slashes so absolute paths resolve correctly */
|
||||
while (*name == '/')
|
||||
name++;
|
||||
|
||||
size_t len = strlen(name);
|
||||
char *ret = kzalloc(len + 1, ALLOC_KERN);
|
||||
const char *zeroptr = name + len;
|
||||
if (!ret)
|
||||
return NULL;
|
||||
|
||||
/* empty after stripping slashes (e.g. "/") */
|
||||
if (len == 0)
|
||||
return ret;
|
||||
|
||||
const char *zeroptr = name + len;
|
||||
|
||||
/* trim trailing slashes */
|
||||
for (size_t i = len - 1; i >= 1; i--) {
|
||||
if (name[i] == '/')
|
||||
|
|
@ -28,7 +37,7 @@ static char *path_trim(const char *name)
|
|||
break;
|
||||
}
|
||||
|
||||
/* trim double slashes */
|
||||
/* copy, collapsing doubled slashes */
|
||||
char c;
|
||||
char last = 0;
|
||||
size_t i = 0;
|
||||
|
|
@ -69,11 +78,11 @@ static int next_level(const char *path, dirent_t *dent, dirent_t **out_dirent)
|
|||
res = fs->fs_ops.open_inode(fs, inum, dirent, &dirent->inode);
|
||||
if (res < 0) {
|
||||
spinlock_release(&dirent->lock);
|
||||
dirent_dec_ref_lock(dirent);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
dirent_inc_ref(dirent);
|
||||
spinlock_release(&dirent->lock);
|
||||
|
||||
*out_dirent = dirent;
|
||||
|
|
@ -98,7 +107,7 @@ int vfs_open(const char *abs_path, dirent_t **out)
|
|||
|
||||
char *path_optr = path_trim(abs_path);
|
||||
if (!path_optr)
|
||||
return -ENOENT;
|
||||
return -ENOMEM;
|
||||
|
||||
char *cur_path = path_optr;
|
||||
dirent_t *current_dir = root_fs->root_dirent;
|
||||
|
|
@ -124,7 +133,6 @@ int vfs_open(const char *abs_path, dirent_t **out)
|
|||
goto out;
|
||||
}
|
||||
|
||||
dirent_inc_ref_lock(next_dir);
|
||||
dirent_dec_ref_lock(current_dir);
|
||||
|
||||
current_dir = next_dir;
|
||||
|
|
@ -154,6 +162,9 @@ ssize_t vfs_write(inode_t *ino, char *buf, size_t size, size_t offset)
|
|||
{
|
||||
ssize_t ret;
|
||||
|
||||
if (!ino)
|
||||
return -EINVAL;
|
||||
|
||||
write_lock(&ino->rwlock);
|
||||
ret = ino->i_ops.write(ino, buf, size, offset);
|
||||
write_unlock(&ino->rwlock);
|
||||
|
|
@ -165,6 +176,9 @@ ssize_t vfs_read(inode_t *ino, char *buf, size_t size, size_t offset)
|
|||
{
|
||||
ssize_t ret;
|
||||
|
||||
if (!ino)
|
||||
return -EINVAL;
|
||||
|
||||
read_lock(&ino->rwlock);
|
||||
ret = ino->i_ops.read(ino, buf, size, offset);
|
||||
read_unlock(&ino->rwlock);
|
||||
|
|
@ -184,6 +198,9 @@ int vfs_creat(dirent_t *parent, const char *name, u64 mode, int flags, dirent_t
|
|||
if (len > 255)
|
||||
return -ENAMETOOLONG;
|
||||
|
||||
if (ino->mode != INODE_DIRECTORY)
|
||||
return -ENOTDIR;
|
||||
|
||||
dirent_t *test = NULL;
|
||||
res = inode_find_dirent(ino, name, &test);
|
||||
|
||||
|
|
@ -195,9 +212,14 @@ int vfs_creat(dirent_t *parent, const char *name, u64 mode, int flags, dirent_t
|
|||
|
||||
write_lock(&ino->rwlock);
|
||||
|
||||
/* re-check under lock */
|
||||
if (ino->mode != INODE_DIRECTORY) {
|
||||
ret = -ENOTDIR;
|
||||
goto out_err;
|
||||
}
|
||||
|
||||
/* re-check under lock */
|
||||
test = NULL;
|
||||
res = inode_find_dirent(ino, name, &test);
|
||||
res = inode_find_dirent_locked(ino, name, &test);
|
||||
if (res == 0) {
|
||||
/* exists */
|
||||
ret = -EEXIST;
|
||||
|
|
@ -245,18 +267,21 @@ int vfs_mount(const char *mount_point, fs_t *fs_to_mount)
|
|||
{
|
||||
if (!mount_point || !strcmp(mount_point, "/")) {
|
||||
root_fs = fs_to_mount;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return -ENOSYS;
|
||||
}
|
||||
|
||||
void vfs_register(struct fs_type *fs)
|
||||
{
|
||||
struct fs_type *ins = kalloc(sizeof(*fs), ALLOC_KERN);
|
||||
if (!ins)
|
||||
return;
|
||||
|
||||
memcpy(ins, fs, sizeof(*fs));
|
||||
list_insert(&fs_type_list, ins);
|
||||
if (!list_insert(&fs_type_list, ins))
|
||||
kfree(ins);
|
||||
}
|
||||
|
||||
void vfs_init()
|
||||
|
|
|
|||
|
|
@ -88,6 +88,7 @@ void dirent_free(dirent_t *dirent);
|
|||
|
||||
/* inode ops */
|
||||
int inode_find_dirent(inode_t *inode, const char *name, dirent_t **out);
|
||||
int inode_find_dirent_locked(inode_t *inode, const char *name, dirent_t **out);
|
||||
inode_t *inode_create();
|
||||
void inode_free(inode_t *inode);
|
||||
void inode_inc_ref(inode_t *inode);
|
||||
|
|
|
|||
|
|
@ -64,6 +64,8 @@ void vfs_register(struct fs_type *fs);
|
|||
|
||||
int vfs_open(const char *abs_path, dirent_t **out);
|
||||
void vfs_close(dirent_t *dent);
|
||||
int vfs_creat(dirent_t *parent, const char *name, u64 mode, int flags, dirent_t **out);
|
||||
int vfs_mount(const char *mount_point, fs_t *fs_to_mount);
|
||||
|
||||
ssize_t vfs_write(inode_t *ino, char *buf, size_t size, size_t offset);
|
||||
ssize_t vfs_read(inode_t *ino, char *buf, size_t size, size_t offset);
|
||||
|
|
|
|||
|
|
@ -10,10 +10,12 @@ void list_create(list_t *list)
|
|||
{
|
||||
list->head = NULL;
|
||||
list->tail = NULL;
|
||||
list->lock = 0;
|
||||
}
|
||||
|
||||
void list_destroy(list_t *list)
|
||||
{
|
||||
spinlock_acquire(&list->lock);
|
||||
list_item_t *item = list->head;
|
||||
|
||||
while (item) {
|
||||
|
|
@ -21,12 +23,19 @@ void list_destroy(list_t *list)
|
|||
slab_free(list_cache, item);
|
||||
item = next;
|
||||
}
|
||||
list->head = NULL;
|
||||
list->tail = NULL;
|
||||
spinlock_release(&list->lock);
|
||||
}
|
||||
|
||||
list_item_t *list_insert(list_t *list, void *item)
|
||||
{
|
||||
spinlock_acquire(&list->lock);
|
||||
list_item_t *ret = slab_alloc(list_cache);
|
||||
if (!ret) {
|
||||
spinlock_release(&list->lock);
|
||||
return NULL;
|
||||
}
|
||||
ret->ptr = item;
|
||||
ret->list = list;
|
||||
ret->next = NULL;
|
||||
|
|
|
|||
Loading…
Reference in a new issue