283 lines
5 KiB
C
283 lines
5 KiB
C
/* SPDX-License-Identifier: GPL-2.0-only */
|
|
#include <kernel/common.h>
|
|
#include <kernel/slab.h>
|
|
|
|
#include <fs/vfs.h>
|
|
|
|
#include <lib/errno.h>
|
|
#include <lib/string.h>
|
|
|
|
static struct mount *root_mount = NULL;
|
|
static spinlock_t mount_lock = 0;
|
|
|
|
fs_t *vfs_get_root(void)
|
|
{
|
|
if (!root_mount)
|
|
return NULL;
|
|
return root_mount->fs;
|
|
}
|
|
|
|
struct mount *vfs_get_root_mount(void)
|
|
{
|
|
return root_mount;
|
|
}
|
|
|
|
struct mount *mount_find_child(struct mount *parent, dirent_t *dent)
|
|
{
|
|
spinlock_acquire(&mount_lock);
|
|
|
|
list_item_t *item = parent->children.head;
|
|
while (item) {
|
|
struct mount *m = item->ptr;
|
|
if (m->mountpoint == dent) {
|
|
spinlock_release(&mount_lock);
|
|
return m;
|
|
}
|
|
item = item->next;
|
|
}
|
|
|
|
spinlock_release(&mount_lock);
|
|
return NULL;
|
|
}
|
|
|
|
static int load_inode(dirent_t *dent)
|
|
{
|
|
if (dent->inode)
|
|
return 0;
|
|
|
|
int res = dent->fs->fs_ops.open_inode(dent->fs, dent->ino_num, dent, &dent->inode);
|
|
return res;
|
|
}
|
|
|
|
static int mount_traverse(const char *path, struct mount **out_mount, dirent_t **out_dirent)
|
|
{
|
|
if (!root_mount)
|
|
return -ENOSYS;
|
|
|
|
while (*path == '/')
|
|
path++;
|
|
|
|
if (*path == '\0') {
|
|
*out_mount = root_mount;
|
|
*out_dirent = root_mount->fs->root_dirent;
|
|
if (*out_dirent)
|
|
dirent_inc_ref(*out_dirent);
|
|
return 0;
|
|
}
|
|
|
|
struct mount *cur = root_mount;
|
|
dirent_t *cur_dent = root_mount->fs->root_dirent;
|
|
|
|
if (!cur_dent)
|
|
return -ENOENT;
|
|
|
|
dirent_inc_ref(cur_dent);
|
|
|
|
const char *p = path;
|
|
|
|
while (*p != '\0') {
|
|
const char *comp_start = p;
|
|
while (*p != '/' && *p != '\0')
|
|
p++;
|
|
|
|
size_t comp_len = p - comp_start;
|
|
if (comp_len > FILE_NAME_MAX_LEN) {
|
|
dirent_dec_ref(cur_dent);
|
|
return -ENAMETOOLONG;
|
|
}
|
|
|
|
char name[FILE_NAME_MAX_LEN + 1];
|
|
memcpy(name, comp_start, comp_len);
|
|
name[comp_len] = '\0';
|
|
|
|
while (*p == '/')
|
|
p++;
|
|
|
|
bool is_last = (*p == '\0');
|
|
|
|
dirent_t *found = NULL;
|
|
int res = dirent_lookup(cur_dent, name, &found);
|
|
if (res < 0) {
|
|
dirent_dec_ref(cur_dent);
|
|
return res;
|
|
}
|
|
|
|
res = load_inode(found);
|
|
if (res < 0) {
|
|
dirent_dec_ref(found);
|
|
dirent_dec_ref(cur_dent);
|
|
return res;
|
|
}
|
|
|
|
if (is_last) {
|
|
dirent_dec_ref(cur_dent);
|
|
*out_mount = cur;
|
|
*out_dirent = found;
|
|
return 0;
|
|
}
|
|
|
|
struct mount *child = NULL;
|
|
list_item_t *item = cur->children.head;
|
|
while (item) {
|
|
struct mount *m = item->ptr;
|
|
if (m->mountpoint == found) {
|
|
child = m;
|
|
break;
|
|
}
|
|
item = item->next;
|
|
}
|
|
|
|
dirent_dec_ref(cur_dent);
|
|
|
|
if (child) {
|
|
dirent_dec_ref(found);
|
|
cur = child;
|
|
cur_dent = cur->fs->root_dirent;
|
|
if (!cur_dent)
|
|
return -ENOENT;
|
|
dirent_inc_ref(cur_dent);
|
|
} else {
|
|
cur_dent = found;
|
|
}
|
|
}
|
|
|
|
dirent_dec_ref(cur_dent);
|
|
*out_mount = cur;
|
|
*out_dirent = cur->fs->root_dirent;
|
|
if (*out_dirent)
|
|
dirent_inc_ref(*out_dirent);
|
|
return 0;
|
|
}
|
|
|
|
int vfs_mount(const char *path, fs_t *fs, u64 flags)
|
|
{
|
|
if (!fs)
|
|
return -EINVAL;
|
|
|
|
spinlock_acquire(&mount_lock);
|
|
|
|
if (!path) {
|
|
if (root_mount) {
|
|
spinlock_release(&mount_lock);
|
|
return -EBUSY;
|
|
}
|
|
|
|
struct mount *m = kzalloc(sizeof(*m), ALLOC_KERN);
|
|
if (!m) {
|
|
spinlock_release(&mount_lock);
|
|
return -ENOMEM;
|
|
}
|
|
|
|
m->parent = NULL;
|
|
m->fs = fs;
|
|
m->mountpoint = NULL;
|
|
m->flags = flags;
|
|
m->child_link = NULL;
|
|
list_create(&m->children);
|
|
|
|
root_mount = m;
|
|
spinlock_release(&mount_lock);
|
|
return 0;
|
|
}
|
|
|
|
struct mount *parent = NULL;
|
|
dirent_t *mp = NULL;
|
|
int res = mount_traverse(path, &parent, &mp);
|
|
if (res < 0) {
|
|
spinlock_release(&mount_lock);
|
|
return res;
|
|
}
|
|
|
|
if ((mp->inode->mode & S_IFMT) != S_IFDIR) {
|
|
dirent_dec_ref(mp);
|
|
spinlock_release(&mount_lock);
|
|
return -ENOTDIR;
|
|
}
|
|
|
|
list_item_t *item = parent->children.head;
|
|
while (item) {
|
|
struct mount *m = item->ptr;
|
|
if (m->mountpoint == mp) {
|
|
dirent_dec_ref(mp);
|
|
spinlock_release(&mount_lock);
|
|
return -EBUSY;
|
|
}
|
|
item = item->next;
|
|
}
|
|
|
|
struct mount *m = kzalloc(sizeof(*m), ALLOC_KERN);
|
|
if (!m) {
|
|
dirent_dec_ref(mp);
|
|
spinlock_release(&mount_lock);
|
|
return -ENOMEM;
|
|
}
|
|
|
|
m->parent = parent;
|
|
m->fs = fs;
|
|
m->mountpoint = mp;
|
|
m->flags = flags;
|
|
|
|
list_create(&m->children);
|
|
|
|
m->child_link = list_insert(&parent->children, m);
|
|
if (!m->child_link) {
|
|
dirent_dec_ref(mp);
|
|
kfree(m);
|
|
spinlock_release(&mount_lock);
|
|
return -ENOMEM;
|
|
}
|
|
|
|
spinlock_release(&mount_lock);
|
|
return 0;
|
|
}
|
|
|
|
int vfs_umount(const char *path)
|
|
{
|
|
if (!path)
|
|
return -EINVAL;
|
|
|
|
spinlock_acquire(&mount_lock);
|
|
|
|
struct mount *parent = NULL;
|
|
dirent_t *mp = NULL;
|
|
int res = mount_traverse(path, &parent, &mp);
|
|
if (res < 0) {
|
|
spinlock_release(&mount_lock);
|
|
return res;
|
|
}
|
|
|
|
struct mount *target = NULL;
|
|
list_item_t *item = parent->children.head;
|
|
while (item) {
|
|
struct mount *m = item->ptr;
|
|
if (m->mountpoint == mp) {
|
|
target = m;
|
|
break;
|
|
}
|
|
item = item->next;
|
|
}
|
|
|
|
dirent_dec_ref(mp);
|
|
|
|
if (!target) {
|
|
spinlock_release(&mount_lock);
|
|
return -EINVAL;
|
|
}
|
|
|
|
if (target == root_mount) {
|
|
spinlock_release(&mount_lock);
|
|
return -EINVAL;
|
|
}
|
|
|
|
if (target->children.head) {
|
|
spinlock_release(&mount_lock);
|
|
return -EBUSY;
|
|
}
|
|
|
|
list_remove(target->child_link);
|
|
dirent_dec_ref(target->mountpoint);
|
|
kfree(target);
|
|
|
|
spinlock_release(&mount_lock);
|
|
return 0;
|
|
}
|