1 #include <lunaix/foptions.h>
2 #include <lunaix/fs/api.h>
3 #include <lunaix/mm/valloc.h>
4 #include <lunaix/process.h>
5 #include <lunaix/spike.h>
6 #include <lunaix/syscall_utils.h>
7 #include <lunaix/syslog.h>
8 #include <lunaix/types.h>
12 struct llist_header all_mnts = { .next = &all_mnts, .prev = &all_mnts };
15 vfs_create_mount(struct v_mount* parent, struct v_dnode* mnt_point)
17 struct v_mount* mnt = vzalloc(sizeof(struct v_mount));
22 llist_init_head(&mnt->submnts);
23 llist_append(&all_mnts, &mnt->list);
24 mutex_init(&mnt->lock);
27 mnt->mnt_point = mnt_point;
28 vfs_vmnt_assign_sb(mnt, mnt_point->super_block);
32 mutex_lock(&mnt->parent->lock);
33 llist_append(&parent->submnts, &mnt->sibmnts);
34 mutex_unlock(&mnt->parent->lock);
37 atomic_fetch_add(&mnt_point->ref_count, 1);
43 __vfs_release_vmnt(struct v_mount* mnt)
45 assert(llist_empty(&mnt->submnts));
48 mnt_chillax(mnt->parent);
51 llist_delete(&mnt->sibmnts);
52 llist_delete(&mnt->list);
53 atomic_fetch_sub(&mnt->mnt_point->ref_count, 1);
58 __vfs_do_unmount(struct v_mount* mnt)
61 struct v_superblock* sb = mnt->super_block;
63 if ((errno = sb->fs->unmount(sb))) {
67 // detached the inodes from cache, and let lru policy to recycle them
68 for (size_t i = 0; i < VFS_HASHTABLE_SIZE; i++) {
69 struct hbucket* bucket = &sb->i_cache[i];
73 bucket->head->pprev = 0;
76 mnt->mnt_point->mnt = mnt->parent;
79 __vfs_release_vmnt(mnt);
85 mnt_mkbusy(struct v_mount* mnt)
87 mutex_lock(&mnt->lock);
89 mutex_unlock(&mnt->lock);
93 mnt_chillax(struct v_mount* mnt)
95 mutex_lock(&mnt->lock);
97 mutex_unlock(&mnt->lock);
101 vfs_mount_root(const char* fs_name, struct device* device)
103 extern struct v_dnode* vfs_sysroot;
105 if (vfs_sysroot->mnt && (errno = vfs_unmount_at(vfs_sysroot))) {
108 return vfs_mount_at(fs_name, device, vfs_sysroot, 0);
112 vfs_mount(const char* target,
114 struct device* device,
121 vfs_walk(__current->cwd, target, &mnt, NULL, VFS_WALK_MKPARENT))) {
122 errno = vfs_mount_at(fs_name, device, mnt, options);
129 vfs_unmount(const char* target)
134 if (!(errno = vfs_walk(__current->cwd, target, &mnt, NULL, 0))) {
135 errno = vfs_unmount_at(mnt);
142 vfs_mount_at(const char* fs_name,
143 struct device* device,
144 struct v_dnode* mnt_point,
147 if (device && device->dev_type != DEV_IFVOL) {
151 if (mnt_point->inode && !check_directory_node(mnt_point->inode)) {
155 struct filesystem* fs = fsm_get(fs_name);
160 if ((fs->types & FSTYPE_ROFS)) {
164 if (!(fs->types & FSTYPE_PSEUDO) && !device) {
169 char* dev_name = "sys";
170 struct v_mount* parent_mnt = mnt_point->mnt;
171 struct v_superblock *sb = vfs_sb_alloc(),
172 *old_sb = mnt_point->super_block;
175 dev_name = device->name_val;
178 // prepare v_superblock for fs::mount invoke
181 sb->root = mnt_point;
182 vfs_d_assign_sb(mnt_point, sb);
184 if (!(mnt_point->mnt = vfs_create_mount(parent_mnt, mnt_point))) {
189 mnt_point->mnt->flags = options;
190 if (!(errno = fs->mount(sb, mnt_point))) {
191 kprintf("mount: dev=%s, fs=%s, mode=%d", dev_name, fs_name, options);
200 ERROR("failed mount: dev=%s, fs=%s, mode=%d, err=%d",
205 vfs_d_assign_sb(mnt_point, old_sb);
207 __vfs_release_vmnt(mnt_point->mnt);
213 vfs_unmount_at(struct v_dnode* mnt_point)
216 struct v_superblock* sb = mnt_point->super_block;
221 if (sb->root != mnt_point) {
225 if (mnt_point->mnt->busy_counter) {
229 if (!(errno = __vfs_do_unmount(mnt_point->mnt))) {
230 atomic_fetch_sub(&mnt_point->ref_count, 1);
237 vfs_check_writable(struct v_dnode* dnode)
239 if ((dnode->mnt->flags & MNT_RO)) {
245 __DEFINE_LXSYSCALL4(int,
256 struct v_dnode *dev = NULL, *mnt = NULL;
259 // It is fine if source is not exist, as some mounting don't require it
260 vfs_walk(__current->cwd, source, &dev, NULL, 0);
262 if ((errno = vfs_walk(__current->cwd, target, &mnt, NULL, 0))) {
266 if (mnt->ref_count > 1) {
271 if (mnt->mnt->mnt_point == mnt) {
276 // By our convention.
277 // XXX could we do better?
278 struct device* device = NULL;
281 if (!check_voldev_node(dev->inode)) {
285 device = (struct device*)dev->inode->data;
288 errno = vfs_mount_at(fstype, device, mnt, options);
291 return DO_STATUS(errno);
294 __DEFINE_LXSYSCALL1(int, unmount, const char*, target)
296 return vfs_unmount(target);