194 lines
3.5 KiB
C
194 lines
3.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 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;
|
|
}
|
|
|
|
return vfs_openat(root_mount, root_mount->fs->root_dirent, path, out_dirent, out_mount, false);
|
|
}
|
|
|
|
int vfs_mount(const char *path, fs_t *fs, u64 flags)
|
|
{
|
|
if (!fs)
|
|
return -EINVAL;
|
|
|
|
if (!path) {
|
|
spinlock_acquire(&mount_lock);
|
|
|
|
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)
|
|
return res;
|
|
|
|
if ((mp->inode->mode & S_IFMT) != S_IFDIR) {
|
|
dirent_dec_ref(mp);
|
|
return -ENOTDIR;
|
|
}
|
|
|
|
spinlock_acquire(&mount_lock);
|
|
|
|
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);
|
|
|
|
list_lock(&parent->children);
|
|
m->child_link = list_insert(&parent->children, m);
|
|
list_unlock(&parent->children);
|
|
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;
|
|
|
|
struct mount *parent = NULL;
|
|
dirent_t *mp = NULL;
|
|
int res = mount_traverse(path, &parent, &mp);
|
|
if (res < 0)
|
|
return res;
|
|
|
|
spinlock_acquire(&mount_lock);
|
|
|
|
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_lock(target->child_link->list);
|
|
list_remove(target->child_link);
|
|
list_unlock(target->child_link->list);
|
|
dirent_dec_ref(target->mountpoint);
|
|
kfree(target);
|
|
|
|
spinlock_release(&mount_lock);
|
|
return 0;
|
|
}
|