/* SPDX-License-Identifier: GPL-2.0-only */ #include #include #include #include #include static struct mount *root_mount = NULL; static lock_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) { spin_lock(&mount_lock); list_item_t *item = parent->children.head; while (item) { struct mount *m = item->ptr; if (m->mountpoint == dent) { spin_unlock(&mount_lock); return m; } item = item->next; } spin_unlock(&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) { spin_lock(&mount_lock); if (root_mount) { spin_unlock(&mount_lock); return -EBUSY; } struct mount *m = kzalloc(sizeof(*m), ALLOC_KERN); if (!m) { spin_unlock(&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; spin_unlock(&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; } spin_lock(&mount_lock); list_item_t *item = parent->children.head; while (item) { struct mount *m = item->ptr; if (m->mountpoint == mp) { dirent_dec_ref(mp); spin_unlock(&mount_lock); return -EBUSY; } item = item->next; } struct mount *m = kzalloc(sizeof(*m), ALLOC_KERN); if (!m) { dirent_dec_ref(mp); spin_unlock(&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); spin_unlock(&mount_lock); return -ENOMEM; } spin_unlock(&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; spin_lock(&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) { spin_unlock(&mount_lock); return -EINVAL; } if (target == root_mount) { spin_unlock(&mount_lock); return -EINVAL; } if (target->children.head) { spin_unlock(&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); spin_unlock(&mount_lock); return 0; }