1 #include <lunaix/foptions.h>
3 #include <lunaix/mm/valloc.h>
4 #include <lunaix/process.h>
5 #include <lunaix/types.h>
7 struct llist_header all_mnts = { .next = &all_mnts, .prev = &all_mnts };
10 vfs_create_mount(struct v_mount* parent, struct v_dnode* mnt_point)
12 struct v_mount* mnt = vzalloc(sizeof(struct v_mount));
17 llist_init_head(&mnt->submnts);
18 llist_append(&all_mnts, &mnt->list);
19 mutex_init(&mnt->lock);
23 mnt->mnt_point = mnt_point;
24 mnt->super_block = mnt_point->super_block;
27 mutex_lock(&mnt->parent->lock);
28 llist_append(&parent->submnts, &mnt->sibmnts);
29 mutex_unlock(&mnt->parent->lock);
32 atomic_fetch_add(&mnt_point->ref_count, 1);
38 __vfs_do_unmount(struct v_mount* mnt)
41 struct v_superblock* sb = mnt->super_block;
43 if ((errno = sb->fs->unmount(sb))) {
47 llist_delete(&mnt->list);
48 llist_delete(&mnt->sibmnts);
50 // detached the inodes from cache, and let lru policy to recycle them
51 for (size_t i = 0; i < VFS_HASHTABLE_SIZE; i++) {
52 struct hbucket* bucket = &sb->i_cache[i];
56 bucket->head->pprev = 0;
59 mnt_chillax(mnt->parent);
62 vfs_d_free(mnt->mnt_point);
69 mnt_mkbusy(struct v_mount* mnt)
71 mutex_lock(&mnt->lock);
73 mutex_unlock(&mnt->lock);
77 mnt_chillax(struct v_mount* mnt)
79 mutex_lock(&mnt->lock);
81 mutex_unlock(&mnt->lock);
85 vfs_mount_root(const char* fs_name, struct device* device)
88 if (vfs_sysroot->mnt && (errno = vfs_unmount_at(vfs_sysroot))) {
91 return vfs_mount_at(fs_name, device, vfs_sysroot, 0);
95 vfs_mount(const char* target,
97 struct device* device,
104 vfs_walk(__current->cwd, target, &mnt, NULL, VFS_WALK_MKPARENT))) {
105 errno = vfs_mount_at(fs_name, device, mnt, options);
112 vfs_unmount(const char* target)
117 if (!(errno = vfs_walk(__current->cwd, target, &mnt, NULL, 0))) {
118 errno = vfs_unmount_at(mnt);
125 vfs_mount_at(const char* fs_name,
126 struct device* device,
127 struct v_dnode* mnt_point,
130 if (mnt_point->inode && !(mnt_point->inode->itype & VFS_IFDIR)) {
134 struct filesystem* fs = fsm_get(fs_name);
139 struct v_mount* parent_mnt = mnt_point->mnt;
140 struct v_superblock *sb = vfs_sb_alloc(), *old_sb = mnt_point->super_block;
142 mnt_point->super_block = sb;
145 if (!(errno = fs->mount(sb, mnt_point))) {
147 sb->root = mnt_point;
149 if (!(mnt_point->mnt = vfs_create_mount(parent_mnt, mnt_point))) {
154 mnt_point->mnt->flags = options;
162 mnt_point->super_block = old_sb;
168 vfs_unmount_at(struct v_dnode* mnt_point)
171 struct v_superblock* sb = mnt_point->super_block;
176 if (sb->root != mnt_point) {
180 if (mnt_point->mnt->busy_counter) {
184 if (!(errno = __vfs_do_unmount(mnt_point->mnt))) {
185 atomic_fetch_sub(&mnt_point->ref_count, 1);
192 vfs_check_writable(struct v_dnode* dnode)
194 if ((dnode->mnt->flags & MNT_RO)) {
200 __DEFINE_LXSYSCALL4(int,
211 struct v_dnode *dev, *mnt;
214 if ((errno = vfs_walk(__current->cwd, source, &dev, NULL, 0))) {
218 if ((errno = vfs_walk(__current->cwd, target, &mnt, NULL, 0))) {
222 if (mnt->ref_count > 1) {
227 // By our convention.
228 // XXX could we do better?
229 struct device* device = (struct device*)dev->inode->data;
231 if (!(dev->inode->itype & VFS_IFVOLDEV) || !device) {
236 errno = vfs_mount_at(fstype, device, mnt, options);
239 return DO_STATUS(errno);
242 __DEFINE_LXSYSCALL1(int, unmount, const char*, target)
244 return vfs_unmount(target);