1 #include <lunaix/foptions.h>
3 #include <lunaix/mm/valloc.h>
4 #include <lunaix/process.h>
5 #include <lunaix/spike.h>
6 #include <lunaix/types.h>
8 struct llist_header all_mnts = { .next = &all_mnts, .prev = &all_mnts };
11 vfs_create_mount(struct v_mount* parent, struct v_dnode* mnt_point)
13 struct v_mount* mnt = vzalloc(sizeof(struct v_mount));
18 llist_init_head(&mnt->submnts);
19 llist_append(&all_mnts, &mnt->list);
20 mutex_init(&mnt->lock);
23 mnt->mnt_point = mnt_point;
24 mnt->super_block = mnt_point->super_block;
28 mutex_lock(&mnt->parent->lock);
29 llist_append(&parent->submnts, &mnt->sibmnts);
30 mutex_unlock(&mnt->parent->lock);
33 atomic_fetch_add(&mnt_point->ref_count, 1);
39 __vfs_do_unmount(struct v_mount* mnt)
42 struct v_superblock* sb = mnt->super_block;
44 if ((errno = sb->fs->unmount(sb))) {
48 llist_delete(&mnt->list);
49 llist_delete(&mnt->sibmnts);
51 // detached the inodes from cache, and let lru policy to recycle them
52 for (size_t i = 0; i < VFS_HASHTABLE_SIZE; i++) {
53 struct hbucket* bucket = &sb->i_cache[i];
57 bucket->head->pprev = 0;
60 mnt_chillax(mnt->parent);
63 vfs_d_free(mnt->mnt_point);
70 mnt_mkbusy(struct v_mount* mnt)
72 mutex_lock(&mnt->lock);
74 mutex_unlock(&mnt->lock);
78 mnt_chillax(struct v_mount* mnt)
80 mutex_lock(&mnt->lock);
82 mutex_unlock(&mnt->lock);
86 vfs_mount_root(const char* fs_name, struct device* device)
88 extern struct v_dnode* vfs_sysroot;
90 if (vfs_sysroot->mnt && (errno = vfs_unmount_at(vfs_sysroot))) {
93 return vfs_mount_at(fs_name, device, vfs_sysroot, 0);
97 vfs_mount(const char* target,
99 struct device* device,
106 vfs_walk(__current->cwd, target, &mnt, NULL, VFS_WALK_MKPARENT))) {
107 errno = vfs_mount_at(fs_name, device, mnt, options);
114 vfs_unmount(const char* target)
119 if (!(errno = vfs_walk(__current->cwd, target, &mnt, NULL, 0))) {
120 errno = vfs_unmount_at(mnt);
127 vfs_mount_at(const char* fs_name,
128 struct device* device,
129 struct v_dnode* mnt_point,
132 if (mnt_point->inode && !(mnt_point->inode->itype & VFS_IFDIR)) {
136 struct filesystem* fs = fsm_get(fs_name);
141 struct v_mount* parent_mnt = mnt_point->mnt;
142 struct v_superblock *sb = vfs_sb_alloc(), *old_sb = mnt_point->super_block;
144 mnt_point->super_block = sb;
147 if (!(errno = fs->mount(sb, mnt_point))) {
149 sb->root = mnt_point;
151 if (!(mnt_point->mnt = vfs_create_mount(parent_mnt, mnt_point))) {
156 mnt_point->mnt->flags = options;
164 mnt_point->super_block = old_sb;
170 vfs_unmount_at(struct v_dnode* mnt_point)
173 struct v_superblock* sb = mnt_point->super_block;
178 if (sb->root != mnt_point) {
182 if (mnt_point->mnt->busy_counter) {
186 if (!(errno = __vfs_do_unmount(mnt_point->mnt))) {
187 atomic_fetch_sub(&mnt_point->ref_count, 1);
194 vfs_check_writable(struct v_dnode* dnode)
196 if ((dnode->mnt->flags & MNT_RO)) {
202 __DEFINE_LXSYSCALL4(int,
213 struct v_dnode *dev, *mnt;
216 if ((errno = vfs_walk(__current->cwd, source, &dev, NULL, 0))) {
220 if ((errno = vfs_walk(__current->cwd, target, &mnt, NULL, 0))) {
224 if (mnt->ref_count > 1) {
229 // By our convention.
230 // XXX could we do better?
231 struct device* device = (struct device*)dev->inode->data;
233 if (!(dev->inode->itype & VFS_IFVOLDEV) || !device) {
238 errno = vfs_mount_at(fstype, device, mnt, options);
241 return DO_STATUS(errno);
244 __DEFINE_LXSYSCALL1(int, unmount, const char*, target)
246 return vfs_unmount(target);