vfs: switch to dirent tracking over inode tracking
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
parent
c7915d8e49
commit
f9d760993d
4 changed files with 77 additions and 62 deletions
|
|
@ -98,22 +98,37 @@ void inode_inc_ref(inode_t *inode)
|
|||
|
||||
void inode_dec_ref(inode_t *inode)
|
||||
{
|
||||
if (!inode)
|
||||
return;
|
||||
write_lock(&inode->rwlock);
|
||||
inode->refcount -= 1;
|
||||
|
||||
if (inode->refcount == 0) {
|
||||
if (inode->parent_inode) {
|
||||
write_lock(&inode->parent_inode->rwlock);
|
||||
inode_dec_ref(inode->parent_inode);
|
||||
write_unlock(&inode->parent_inode->rwlock);
|
||||
}
|
||||
inode->fs->fs_ops.free_inode(inode->fs, inode);
|
||||
|
||||
inode_free(inode);
|
||||
return;
|
||||
}
|
||||
write_unlock(&inode->rwlock);
|
||||
}
|
||||
|
||||
void dirent_inc_ref(dirent_t *dent)
|
||||
{
|
||||
spinlock_acquire(&dent->lock);
|
||||
dent->refcount += 1;
|
||||
spinlock_release(&dent->lock);
|
||||
}
|
||||
|
||||
void dirent_dec_ref(dirent_t *dent)
|
||||
{
|
||||
spinlock_acquire(&dent->lock);
|
||||
dent->refcount -= 1;
|
||||
if (dent->refcount == 0) {
|
||||
inode_dec_ref(dent->inode);
|
||||
dirent_free(dent);
|
||||
return;
|
||||
}
|
||||
spinlock_release(&dent->lock);
|
||||
}
|
||||
|
||||
dirent_t *dirent_create(const char *name)
|
||||
{
|
||||
dirent_t *ret = slab_alloc(dirent_slab);
|
||||
|
|
|
|||
84
fs/vfs/vfs.c
84
fs/vfs/vfs.c
|
|
@ -1,15 +1,12 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/slab.h>
|
||||
|
||||
#include <fs/vfs.h>
|
||||
|
||||
#include <lib/list.h>
|
||||
#include <lib/string.h>
|
||||
#include <lib/errno.h>
|
||||
|
||||
static fs_t *root_fs = NULL;
|
||||
|
||||
static list_t fs_type_list = {0};
|
||||
|
||||
static char *path_trim(const char *name)
|
||||
|
|
@ -47,9 +44,9 @@ static char *path_trim(const char *name)
|
|||
return ret;
|
||||
}
|
||||
|
||||
static int next_level(const char *path, inode_t **cur)
|
||||
static int next_level(const char *path, inode_t *dir, dirent_t **out_dirent)
|
||||
{
|
||||
if (!cur || !*cur)
|
||||
if (!dir || !out_dirent)
|
||||
return -EINVAL;
|
||||
|
||||
int res;
|
||||
|
|
@ -60,22 +57,25 @@ static int next_level(const char *path, inode_t **cur)
|
|||
|
||||
if (c == '/' || c == '\0') {
|
||||
dirent_t *dirent;
|
||||
res = inode_find_dirent(*cur, build, &dirent);
|
||||
res = inode_find_dirent(dir, build, &dirent);
|
||||
if (res < 0)
|
||||
return -ENOENT;
|
||||
|
||||
spinlock_acquire(&dirent->lock);
|
||||
if (dirent->inode) {
|
||||
*cur = dirent->inode;
|
||||
inode_inc_ref(*cur);
|
||||
} else {
|
||||
fs_t *fs = (*cur)->fs;
|
||||
res = fs->fs_ops.open_inode(fs, dirent->ino_num, cur);
|
||||
|
||||
if (!dirent->inode) {
|
||||
fs_t *fs = dir->fs;
|
||||
res = fs->fs_ops.open_inode(fs, dirent->ino_num, &dirent->inode);
|
||||
if (res < 0) {
|
||||
spinlock_release(&dirent->lock);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
dirent_inc_ref(dirent);
|
||||
spinlock_release(&dirent->lock);
|
||||
|
||||
*out_dirent = dirent;
|
||||
return i;
|
||||
} else {
|
||||
build[i] = c;
|
||||
|
|
@ -85,72 +85,68 @@ static int next_level(const char *path, inode_t **cur)
|
|||
return -ENOENT;
|
||||
}
|
||||
|
||||
int vfs_open(const char *abs_path, inode_t **out)
|
||||
/* opens a dirent by absolute path only
|
||||
*
|
||||
* IMPORTANT: ownership of the out dirent is passed to the caller, and managing
|
||||
* its refcount is up to the caller (e.g. dirent_dec_ref)
|
||||
*/
|
||||
int vfs_open(const char *abs_path, dirent_t **out)
|
||||
{
|
||||
if (!abs_path || !out)
|
||||
return -EINVAL;
|
||||
|
||||
inode_t *c_ino = root_fs->root_inode;
|
||||
char *path_optr = path_trim(abs_path);
|
||||
char *cur_path = path_optr;
|
||||
list_item_t *item;
|
||||
size_t ret = -ENOENT;
|
||||
|
||||
if (!cur_path)
|
||||
if (!path_optr)
|
||||
return -ENOENT;
|
||||
|
||||
list_t visited;
|
||||
list_create(&visited);
|
||||
inode_inc_ref(c_ino);
|
||||
list_insert(&visited, c_ino);
|
||||
char *cur_path = path_optr;
|
||||
dirent_t *current_dir = root_fs->root_dirent;
|
||||
int ret = -ENOENT;
|
||||
|
||||
dirent_inc_ref(current_dir);
|
||||
|
||||
/* Handle root path "/" */
|
||||
if (strlen(abs_path) == 1 && *abs_path == '/') {
|
||||
inode_inc_ref(c_ino);
|
||||
*out = c_ino;
|
||||
*out = current_dir;
|
||||
kfree(path_optr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
int res = next_level(cur_path, &c_ino);
|
||||
dirent_t *next_dir;
|
||||
|
||||
if (res < 0)
|
||||
int res = next_level(cur_path, current_dir->inode, &next_dir);
|
||||
|
||||
if (res < 0) {
|
||||
dirent_dec_ref(current_dir);
|
||||
ret = res;
|
||||
goto out;
|
||||
}
|
||||
|
||||
dirent_inc_ref(next_dir);
|
||||
dirent_dec_ref(current_dir);
|
||||
|
||||
current_dir = next_dir;
|
||||
cur_path += res;
|
||||
|
||||
while (*cur_path == '/')
|
||||
cur_path++;
|
||||
|
||||
if (!c_ino)
|
||||
goto out;
|
||||
|
||||
/* found */
|
||||
if (*cur_path == '\0') {
|
||||
*out = c_ino;
|
||||
*out = current_dir;
|
||||
ret = 0;
|
||||
goto out;
|
||||
}
|
||||
|
||||
list_insert(&visited, c_ino);
|
||||
}
|
||||
|
||||
out:
|
||||
item = visited.head;
|
||||
while (item) {
|
||||
inode_t *inode = item->ptr;
|
||||
inode_dec_ref(inode);
|
||||
item = item->next;
|
||||
}
|
||||
list_destroy(&visited);
|
||||
kfree(path_optr);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void vfs_close(inode_t *inode)
|
||||
void vfs_close(dirent_t *dent)
|
||||
{
|
||||
inode_dec_ref(inode);
|
||||
dirent_dec_ref(dent);
|
||||
}
|
||||
|
||||
ssize_t vfs_write(inode_t *ino, char *buf, size_t size, size_t offset)
|
||||
|
|
|
|||
|
|
@ -78,6 +78,8 @@ struct _dirent {
|
|||
u64 type;
|
||||
spinlock_t lock;
|
||||
|
||||
u64 refcount;
|
||||
|
||||
char *name;
|
||||
};
|
||||
|
||||
|
|
@ -91,5 +93,7 @@ inode_t *inode_create();
|
|||
void inode_free(inode_t *inode);
|
||||
void inode_inc_ref(inode_t *inode);
|
||||
void inode_dec_ref(inode_t *inode);
|
||||
void dirent_inc_ref(dirent_t *dent);
|
||||
void dirent_dec_ref(dirent_t *dent);
|
||||
|
||||
#endif /* _INODE_H_ */
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ struct _fs {
|
|||
size_t block_size;
|
||||
size_t n_blocks;
|
||||
|
||||
inode_t *root_inode;
|
||||
dirent_t *root_dirent;
|
||||
|
||||
bool mounted;
|
||||
dirent_t *mount_point;
|
||||
|
|
@ -37,22 +37,22 @@ struct _fs {
|
|||
};
|
||||
|
||||
struct ram_file {
|
||||
u64 type;
|
||||
u64 extra;
|
||||
char *name;
|
||||
size_t name_len;
|
||||
u64 type;
|
||||
u64 extra;
|
||||
char *name;
|
||||
size_t name_len;
|
||||
|
||||
void *data;
|
||||
size_t data_size;
|
||||
size_t file_size;
|
||||
void *data;
|
||||
size_t data_size;
|
||||
size_t file_size;
|
||||
};
|
||||
|
||||
/* VFS ops */
|
||||
void vfs_init();
|
||||
void vfs_register(struct fs_type *fs);
|
||||
|
||||
int vfs_open(const char *abs_path, inode_t **out);
|
||||
void vfs_close(inode_t *inode);
|
||||
int vfs_open(const char *abs_path, dirent_t **out);
|
||||
void vfs_close(dirent_t *dent);
|
||||
|
||||
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);
|
||||
|
|
|
|||
Loading…
Reference in a new issue