forked from ish-app/ish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mount.c
194 lines (172 loc) · 5.33 KB
/
mount.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <string.h>
#include <sys/stat.h>
#include "kernel/calls.h"
#include "kernel/fs.h"
#include "fs/path.h"
#include "fs/real.h"
#define MAX_FILESYSTEMS 10
static const struct fs_ops *filesystems[MAX_FILESYSTEMS] = {
&realfs,
&procfs,
&devptsfs,
&tmpfs,
};
void fs_register(const struct fs_ops *fs) {
for (unsigned i = 0; i < MAX_FILESYSTEMS; i++) {
if (filesystems[i] == NULL) {
filesystems[i] = fs;
return;
}
}
assert(!"reached filesystem limit");
}
struct mount *mount_find(char *path) {
assert(path_is_normalized(path));
lock(&mounts_lock);
struct mount *mount = NULL;
assert(!list_empty(&mounts)); // this would mean there's no root FS mounted
list_for_each_entry(&mounts, mount, mounts) {
size_t n = strlen(mount->point);
if (strncmp(path, mount->point, n) == 0 && (path[n] == '/' || path[n] == '\0'))
break;
}
mount->refcount++;
unlock(&mounts_lock);
return mount;
}
void mount_retain(struct mount *mount) {
lock(&mounts_lock);
mount->refcount++;
unlock(&mounts_lock);
}
void mount_release(struct mount *mount) {
lock(&mounts_lock);
mount->refcount--;
unlock(&mounts_lock);
}
int do_mount(const struct fs_ops *fs, const char *source, const char *point, const char *info, int flags) {
struct mount *new_mount = malloc(sizeof(struct mount));
if (new_mount == NULL)
return _ENOMEM;
new_mount->point = strdup(point);
new_mount->source = strdup(source);
new_mount->info = strdup(info);
new_mount->flags = flags;
new_mount->fs = fs;
new_mount->data = NULL;
new_mount->refcount = 0;
if (fs->mount) {
int err = fs->mount(new_mount);
if (err < 0) {
free((void *) new_mount->point);
free((void *) new_mount->source);
free(new_mount);
return err;
}
}
// the list must stay in descending order of mount point length
struct mount *mount;
list_for_each_entry(&mounts, mount, mounts) {
if (strlen(mount->point) <= strlen(new_mount->point))
break;
}
list_add_before(&mount->mounts, &new_mount->mounts);
return 0;
}
int mount_remove(struct mount *mount) {
if (mount->refcount != 0)
return _EBUSY;
if (mount->fs->umount)
mount->fs->umount(mount);
list_remove(&mount->mounts);
free((void *) mount->info);
free((void *) mount->source);
free((void *) mount->point);
free(mount);
return 0;
}
int do_umount(const char *point) {
struct mount *mount;
bool found = false;
list_for_each_entry(&mounts, mount, mounts) {
if (strcmp(point, mount->point) == 0) {
found = true;
break;
}
}
if (!found)
return _EINVAL;
return mount_remove(mount);
}
// FIXME: this is shit
bool mount_param_flag(const char *info, const char *flag) {
while (*info != '\0') {
if (strncmp(info, flag, strlen(flag)) == 0)
return true;
info += strcspn(info, ",");
}
return false;
}
#define MS_SUPPORTED (MS_READONLY_|MS_NOSUID_|MS_NODEV_|MS_NOEXEC_|MS_SILENT_)
#define MS_FLAGS (MS_READONLY_|MS_NOSUID_|MS_NODEV_|MS_NOEXEC_)
dword_t sys_mount(addr_t source_addr, addr_t point_addr, addr_t type_addr, dword_t flags, addr_t data_addr) {
char source[MAX_PATH];
if (user_read_string(source_addr, source, sizeof(source)))
return _EFAULT;
char point_raw[MAX_PATH];
if (user_read_string(point_addr, point_raw, sizeof(point_raw)))
return _EFAULT;
char data[MAX_PATH];
if (data_addr != 0) {
if (user_read_string(data_addr, data, sizeof(data)))
return _EFAULT;
}
char type[100];
if (user_read_string(type_addr, type, sizeof(type)))
return _EFAULT;
STRACE("mount(\"%s\", \"%s\", \"%s\", %#x, \"%s\")", source, point_raw, type, flags, data_addr != 0 ? data : NULL);
if (flags & ~MS_SUPPORTED) {
FIXME("missing mount flags %#x", flags & ~MS_SUPPORTED);
return _EINVAL;
}
const struct fs_ops *fs = NULL;
for (size_t i = 0; i < sizeof(filesystems)/sizeof(filesystems[0]); i++) {
if (filesystems[i] && (strcmp(filesystems[i]->name, type) == 0)) {
fs = filesystems[i];
break;
}
}
if (fs == NULL)
return _EINVAL;
struct statbuf stat;
int err = generic_statat(AT_PWD, point_raw, &stat, true);
if (err < 0)
return err;
if (!S_ISDIR(stat.mode))
return _ENOTDIR;
char point[MAX_PATH];
err = path_normalize(AT_PWD, point_raw, point, N_SYMLINK_FOLLOW);
if (err < 0)
return err;
lock(&mounts_lock);
err = do_mount(fs, source, point, data, flags & MS_FLAGS);
unlock(&mounts_lock);
return err;
}
#define UMOUNT_NOFOLLOW_ 8
dword_t sys_umount2(addr_t target_addr, dword_t flags) {
char target_raw[MAX_PATH];
if (user_read_string(target_addr, target_raw, sizeof(target_raw)))
return _EFAULT;
char target[MAX_PATH];
int err = path_normalize(AT_PWD, target_raw, target,
flags & UMOUNT_NOFOLLOW_ ? N_SYMLINK_NOFOLLOW : N_SYMLINK_FOLLOW);
if (err < 0)
return err;
lock(&mounts_lock);
err = do_umount(target);
unlock(&mounts_lock);
return err;
}
struct list mounts = {&mounts, &mounts};
lock_t mounts_lock = LOCK_INITIALIZER;