refactor: send the command with retry and error detection
[lunaix-os.git] / lunaix-os / kernel / fs / mount.c
1 #include <lunaix/fs.h>
2 #include <lunaix/mm/valloc.h>
3 #include <lunaix/process.h>
4 #include <lunaix/types.h>
5
6 static struct llist_header all_mnts = { .next = &all_mnts, .prev = &all_mnts };
7
8 struct v_mount*
9 vfs_create_mount(struct v_mount* parent, struct v_dnode* mnt_point)
10 {
11     struct v_mount* mnt = vzalloc(sizeof(struct v_mount));
12     if (!mnt) {
13         return NULL;
14     }
15
16     llist_init_head(&mnt->submnts);
17     llist_append(&all_mnts, &mnt->list);
18     mutex_init(&mnt->lock);
19
20     mnt_mkbusy(parent);
21     mnt->parent = parent;
22     mnt->mnt_point = mnt_point;
23     mnt->super_block = mnt_point->super_block;
24
25     if (parent) {
26         mutex_lock(&mnt->parent->lock);
27         llist_append(&parent->submnts, &mnt->sibmnts);
28         mutex_unlock(&mnt->parent->lock);
29     }
30
31     atomic_fetch_add(&mnt_point->ref_count, 1);
32
33     return mnt;
34 }
35
36 int
37 __vfs_do_unmount(struct v_mount* mnt)
38 {
39     int errno = 0;
40     struct v_superblock* sb = mnt->super_block;
41
42     if ((errno = sb->fs->unmount(sb))) {
43         return errno;
44     }
45
46     llist_delete(&mnt->list);
47     llist_delete(&mnt->sibmnts);
48
49     // detached the inodes from cache, and let lru policy to recycle them
50     for (size_t i = 0; i < VFS_HASHTABLE_SIZE; i++) {
51         struct hbucket* bucket = &sb->i_cache[i];
52         if (!bucket) {
53             continue;
54         }
55         bucket->head->pprev = 0;
56     }
57
58     mnt_chillax(mnt->parent);
59
60     vfs_sb_free(sb);
61     vfs_d_free(mnt->mnt_point);
62     vfree(mnt);
63
64     return errno;
65 }
66
67 void
68 mnt_mkbusy(struct v_mount* mnt)
69 {
70     while (mnt) {
71         mutex_lock(&mnt->lock);
72         mnt->busy_counter++;
73         mutex_unlock(&mnt->lock);
74
75         mnt = mnt->parent;
76     }
77 }
78
79 void
80 mnt_chillax(struct v_mount* mnt)
81 {
82     while (mnt) {
83         mutex_lock(&mnt->lock);
84         mnt->busy_counter--;
85         mutex_unlock(&mnt->lock);
86
87         mnt = mnt->parent;
88     }
89 }
90
91 int
92 vfs_mount(const char* target, const char* fs_name, struct device* device)
93 {
94     int errno;
95     struct v_dnode* mnt;
96
97     if (!(errno = vfs_walk(__current->cwd, target, &mnt, NULL, 0))) {
98         errno = vfs_mount_at(fs_name, device, mnt);
99     }
100
101     return errno;
102 }
103
104 int
105 vfs_unmount(const char* target)
106 {
107     int errno;
108     struct v_dnode* mnt;
109
110     if (!(errno = vfs_walk(__current->cwd, target, &mnt, NULL, 0))) {
111         errno = vfs_unmount_at(mnt);
112     }
113
114     return errno;
115 }
116
117 int
118 vfs_mount_at(const char* fs_name,
119              struct device* device,
120              struct v_dnode* mnt_point)
121 {
122     if (mnt_point->inode && !(mnt_point->inode->itype & VFS_IFDIR)) {
123         return ENOTDIR;
124     }
125
126     struct filesystem* fs = fsm_get(fs_name);
127     if (!fs) {
128         return ENODEV;
129     }
130
131     struct v_mount* parent_mnt = mnt_point->mnt;
132     struct v_superblock* sb = vfs_sb_alloc();
133     sb->dev = device;
134
135     int errno = 0;
136     if (!(errno = fs->mount(sb, mnt_point))) {
137         sb->fs = fs;
138         sb->root = mnt_point;
139         mnt_point->super_block = sb;
140
141         if (!(mnt_point->mnt = vfs_create_mount(parent_mnt, mnt_point))) {
142             errno = ENOMEM;
143             vfs_sb_free(sb);
144         }
145     }
146
147     return errno;
148 }
149
150 int
151 vfs_unmount_at(struct v_dnode* mnt_point)
152 {
153     int errno = 0;
154     struct v_superblock* sb = mnt_point->super_block;
155     if (!sb) {
156         return EINVAL;
157     }
158
159     if (sb->root != mnt_point) {
160         return EINVAL;
161     }
162
163     if (mnt_point->mnt->busy_counter) {
164         return EBUSY;
165     }
166
167     if (!(errno = __vfs_do_unmount(mnt_point->mnt))) {
168         atomic_fetch_sub(&mnt_point->ref_count, 1);
169     }
170
171     return errno;
172 }