MOS Source Code
Loading...
Searching...
No Matches
mount.c
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2
4
7#include "mos/misc/setup.h"
8
10#include <mos_stdlib.h>
11
12#define VFS_MOUNTPOINT_MAP_SIZE 256
13static hashmap_t vfs_mountpoint_map = { 0 }; // dentry_t -> mount_t
14
21
29{
30 MOS_ASSERT(dentry);
31 MOS_ASSERT_X(dentry->name == NULL, "mounted root should not have a name");
32
33 if (dentry == root_dentry)
34 return dentry; // the root dentry is its own mountpoint
35
36 dentry_t *parent = dentry_parent(dentry);
37 if (parent == NULL)
38 {
39 // root for some other fs trees
40 return NULL;
41 }
42
43 tree_foreach_child(dentry_t, child, parent)
44 {
45 if (child->is_mountpoint)
46 {
47 mount_t *mount = dentry_get_mount(child);
48 if (mount->root == dentry)
49 return child;
50 }
51 }
52
53 return NULL; // not found, possibly just have been unmounted
54}
55
57{
58 if (!dentry->is_mountpoint)
59 {
60 mos_warn("dentry is not a mountpoint");
61 return NULL;
62 }
63
64 mount_t *mount = hashmap_get(&vfs_mountpoint_map, (ptr_t) dentry);
65 if (mount == NULL)
66 {
67 mos_warn("mountpoint not found");
68 return NULL;
69 }
70
71 // otherwise the mountpoint must match the dentry
72 MOS_ASSERT(mount->mountpoint == dentry);
73 return mount;
74}
75
76bool dentry_mount(dentry_t *mountpoint, dentry_t *root, filesystem_t *fs)
77{
78 MOS_ASSERT_X(root->name == NULL, "mountpoint already has a name");
79 MOS_ASSERT_X(dentry_parent(root) == NULL, "mountpoint already has a parent");
80
81 dentry_ref(root);
82
83 tree_node(root)->parent = NULL;
84 if (dentry_parent(mountpoint))
85 tree_node(root)->parent = tree_node(dentry_parent(mountpoint));
86
87 mountpoint->is_mountpoint = true;
88
89 mount_t *mount = kmalloc(mount_cache);
92 mount->root = root;
93 mount->superblock = root->inode->superblock;
94 mount->mountpoint = mountpoint;
95 mount->fs = fs;
96
97 if (hashmap_put(&vfs_mountpoint_map, (ptr_t) mountpoint, mount) != NULL)
98 {
99 mos_warn("failed to insert mountpoint into hashmap");
100 return false;
101 }
102
103 return true;
104}
105
106// remove root from the mount tree
108{
110 if (mount == NULL)
111 return NULL;
112
113 dentry_t *mountpoint = mount->mountpoint;
114
117 kfree(mount);
118 mountpoint->is_mountpoint = false;
119 return mountpoint;
120}
#define MOS_ASSERT_X(cond, msg,...)
Definition assert.h:15
#define MOS_ASSERT(cond)
Definition assert.h:14
#define mos_warn(fmt,...)
Definition assert.h:23
dentry_t * dentry_unmount(dentry_t *root)
Unmount a filesystem at the mountpoint.
Definition mount.c:107
bool dentry_mount(dentry_t *mountpoint, dentry_t *root, filesystem_t *fs)
Mount a filesystem at a mountpoint.
Definition mount.c:76
dentry_t * dentry_ref(dentry_t *dentry)
Increment the reference count of a dentry.
should_inline dentry_t * dentry_parent(const dentry_t *dentry)
Definition dentry.h:65
MOSAPI int __pure hashmap_simple_key_compare(uintn key1, uintn key2)
MOSAPI hash_t __pure hashmap_identity_hash(uintn key)
MOSAPI void * hashmap_get(hashmap_t *map, uintn key)
Definition hashmap.c:96
MOSAPI void * hashmap_put(hashmap_t *map, uintn key, void *value)
Definition hashmap.c:68
MOSAPI void hashmap_init(hashmap_t *map, size_t capacity, hashmap_hash_t hash_func, hashmap_key_compare_t compare_func)
Definition hashmap.c:24
MOSAPI void * hashmap_remove(hashmap_t *map, uintn key)
Definition hashmap.c:117
MOSAPI void linked_list_init(list_node_t *head_node)
Initialise a circular double linked list.
Definition list.c:15
#define LIST_HEAD_INIT(container)
Definition list.h:38
MOSAPI void list_node_append(list_node_t *head, list_node_t *item)
Definition list.c:68
#define list_node(element)
Get the ‘list_node’ of a list element. This is exactly the reverse of ‘list_entry’ above.
Definition list.h:68
list_node_t list_head
A linked list head.
Definition list.h:23
MOSAPI void list_node_remove(list_node_t *link)
Definition list.c:26
dentry_t * root_dentry
Definition vfs.c:36
#define VFS_MOUNTPOINT_MAP_SIZE
Definition mount.c:12
dentry_t * dentry_root_get_mountpoint(dentry_t *dentry)
Given a mounted root dentry, return the mountpoint dentry that points to it.
Definition mount.c:28
mount_t * dentry_get_mount(const dentry_t *dentry)
Definition mount.c:56
static hashmap_t vfs_mountpoint_map
Definition mount.c:13
static void mountpoint_map_init(void)
Definition mount.c:15
list_head vfs_mountpoint_list
Definition mount.c:20
#define NULL
Definition pb_syshdr.h:46
#define MOS_INIT(_comp, _fn)
Definition setup.h:40
bool is_mountpoint
Definition vfs_types.h:120
const char * name
Definition vfs_types.h:118
inode_t * inode
Definition vfs_types.h:117
superblock_t * superblock
Definition vfs_types.h:171
superblock_t * superblock
Definition vfs_types.h:193
dentry_t * root
Definition vfs_types.h:191
dentry_t * mountpoint
Definition vfs_types.h:192
filesystem_t * fs
Definition vfs_types.h:194
#define tree_node(element)
Definition tree.h:29
#define tree_foreach_child(t, v, h)
Definition tree.h:36
unsigned long ptr_t
Definition types.h:25
slab_t * mount_cache
Definition vfs.c:38