forked from davidsaOpenu/xv6-ns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kmount.c
336 lines (283 loc) · 7.69 KB
/
kmount.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include "types.h"
#include "defs.h"
#include "spinlock.h"
#include "fs.h"
#include "sleeplock.h"
#include "file.h"
#include "mount.h"
#include "param.h"
#include "stat.h"
#include "mmu.h"
#include "proc.h"
#include "namespace.h"
#include "mount_ns.h"
struct mount_list {
struct mount mnt;
struct mount_list *next;
};
struct {
struct spinlock mnt_list_lock; // protects mnt_list
struct mount_list mnt_list[NMOUNT];
} mount_holder;
struct mount_list*
getactivemounts()
{
return myproc()->nsproxy->mount_ns->active_mounts;
}
// Parent mount (if it exists) must already be ref-incremented.
static void
addmountinternal(struct mount_list *mnt_list, uint dev, struct inode *mountpoint, struct mount *parent)
{
mnt_list->mnt.mountpoint = mountpoint;
mnt_list->mnt.dev = dev;
mnt_list->mnt.parent = parent;
// add to linked list
mnt_list->next = getactivemounts();
myproc()->nsproxy->mount_ns->active_mounts = mnt_list;
}
struct mount *
getinitialrootmount(void)
{
return &mount_holder.mnt_list[0].mnt;
}
struct mount *
getrootmount(void)
{
return myproc()->nsproxy->mount_ns->root;
}
void
mntinit(void)
{
initlock(&mount_holder.mnt_list_lock, "mount_list");
addmountinternal(&mount_holder.mnt_list[0], ROOTDEV, 0, 0);
mount_holder.mnt_list[0].mnt.ref = 1;
myproc()->nsproxy->mount_ns->root = getinitialrootmount();
}
struct mount*
mntdup(struct mount *mnt)
{
acquire(&mount_holder.mnt_list_lock);
mnt->ref++;
release(&mount_holder.mnt_list_lock);
return mnt;
}
void
mntput(struct mount *mnt)
{
acquire(&mount_holder.mnt_list_lock);
mnt->ref--;
release(&mount_holder.mnt_list_lock);
}
static struct mount_list*
allocmntlist(void)
{
acquire(&mount_holder.mnt_list_lock);
int i;
// Find empty mount struct
for (i = 0; i < NMOUNT && mount_holder.mnt_list[i].mnt.ref != 0; i++);
if (i == NMOUNT) {
// error - no available mount memory.
panic("out of mount_list objects");
}
struct mount_list *newmountentry = &mount_holder.mnt_list[i];
newmountentry->mnt.ref = 1;
release(&mount_holder.mnt_list_lock);
return newmountentry;
}
// mountpoint and device must be locked.
int
mount(struct inode *mountpoint, struct inode *device, struct mount *parent)
{
struct mount_list *newmountentry = allocmntlist();
struct mount *newmount = &newmountentry->mnt;
int dev = getorcreatedevice(device);
if (dev < 0) {
newmount->ref = 0;
cprintf("failed to create device.\n");
return -1;
}
acquire(&myproc()->nsproxy->mount_ns->lock);
struct mount_list *current = getactivemounts();
while (current != 0) {
if (current->mnt.parent == parent && current->mnt.mountpoint == mountpoint) {
// error - mount already exists.
release(&myproc()->nsproxy->mount_ns->lock);
deviceput(dev);
newmount->ref = 0;
cprintf("mount already exists at that point.\n");
return -1;
}
current = current->next;
}
mntdup(parent);
addmountinternal(newmountentry, dev, mountpoint, parent);
release(&myproc()->nsproxy->mount_ns->lock);
return 0;
}
int
umount(struct mount *mnt)
{
acquire(&myproc()->nsproxy->mount_ns->lock);
struct mount_list *current = getactivemounts();
struct mount_list **previous = &(myproc()->nsproxy->mount_ns->active_mounts);
while (current != 0) {
if (¤t->mnt == mnt) {
break;
}
previous = ¤t->next;
current = current->next;
}
if (current == 0) {
// error - not actually mounted.
release(&myproc()->nsproxy->mount_ns->lock);
cprintf("current=0\n");
return -1;
}
if (current->mnt.parent == 0) {
// error - can't unmount root filesystem
release(&myproc()->nsproxy->mount_ns->lock);
cprintf("current->mnt.parent == 0\n");
return -1;
}
acquire(&mount_holder.mnt_list_lock);
// Base ref is 1, +1 for the mount being acquired before entering this method.
if (current->mnt.ref > 2) {
// error - can't unmount as there are references.
release(&mount_holder.mnt_list_lock);
release(&myproc()->nsproxy->mount_ns->lock);
return -1;
}
// remove from linked list
*previous = current->next;
release(&myproc()->nsproxy->mount_ns->lock);
struct inode *oldmountpoint = current->mnt.mountpoint;
int olddev = current->mnt.dev;
current->mnt.mountpoint = 0;
current->mnt.parent->ref--;
current->mnt.ref = 0;
current->mnt.dev = 0;
current->next = 0;
release(&mount_holder.mnt_list_lock);
iput(oldmountpoint);
deviceput(olddev);
return 0;
}
struct mount*
mntlookup(struct inode *mountpoint, struct mount *parent)
{
acquire(&myproc()->nsproxy->mount_ns->lock);
struct mount_list *entry = getactivemounts();
while (entry != 0) {
if (entry->mnt.mountpoint == mountpoint && entry->mnt.parent == parent) {
release(&myproc()->nsproxy->mount_ns->lock);
return mntdup(&entry->mnt);
}
entry = entry->next;
}
release(&myproc()->nsproxy->mount_ns->lock);
return 0;
}
void
printmounts(void)
{
acquire(&myproc()->nsproxy->mount_ns->lock);
struct mount_list *entry = getactivemounts();
int i = 0;
cprintf("Printing mounts:\n");
while (entry != 0) {
i++;
cprintf("%d: Mount %x attached to %x, child of %x, with ref %d\n", i, &entry->mnt, entry->mnt.mountpoint, entry->mnt.parent, entry->mnt.ref);
entry = entry->next;
}
release(&myproc()->nsproxy->mount_ns->lock);
}
void
umountall(struct mount_list* mounts)
{
while (mounts != 0) {
struct mount_list* next = mounts->next;
if (mounts->mnt.parent == 0) {
// No need to unmount root -
mounts->mnt.ref = 0;
} else if (umount(&mounts->mnt) != 0) {
panic("failed to umount upon namespace close");
}
mounts = next;
}
}
static struct mount_list*
shallowcopyactivemounts(struct mount **newcwdmount)
{
struct mount_list *head = 0;
struct mount_list *entry = myproc()->nsproxy->mount_ns->active_mounts;
struct mount_list *prev = 0;
while (entry != 0) {
struct mount_list* newentry = allocmntlist();
if (head == 0) {
head = newentry;
}
newentry->mnt.ref = 1;
newentry->mnt.mountpoint = idup(entry->mnt.mountpoint);
newentry->mnt.parent = 0;
newentry->mnt.dev=entry->mnt.dev;
deviceget(newentry->mnt.dev);
if (prev != 0) {
prev->next = newentry;
}
if (myproc()->cwdmount == &entry->mnt) {
*newcwdmount = &newentry->mnt;
}
prev = newentry;
entry = entry->next;
}
return head;
}
static void
fixparents(struct mount_list* newentry)
{
struct mount_list *entry = myproc()->nsproxy->mount_ns->active_mounts;
while (entry != 0) {
if (entry->mnt.parent != 0) {
struct mount_list *finder = myproc()->nsproxy->mount_ns->active_mounts;
struct mount_list *newfinder = newentry;
while (finder != 0 && entry->mnt.parent != &finder->mnt) {
finder = finder->next;
newfinder = newfinder->next;
}
if (finder == 0) {
panic("invalid mount tree structure");
}
newentry->mnt.parent = mntdup(&newfinder->mnt);
}
newentry = newentry->next;
entry = entry->next;
}
}
struct mount_list*
copyactivemounts(void)
{
acquire(&myproc()->nsproxy->mount_ns->lock);
struct mount *oldcwdmount = myproc()->cwdmount;
struct mount *newcwdmount = 0;
struct mount_list *newentry = shallowcopyactivemounts(&newcwdmount);
fixparents(newentry);
release(&myproc()->nsproxy->mount_ns->lock);
myproc()->cwdmount = mntdup(newcwdmount);
mntput(oldcwdmount);
return newentry;
}
struct mount*
getroot(struct mount_list* newentry)
{
if (newentry != 0) {
struct mount* current = &newentry->mnt;
while (current != 0 && current->parent != 0) {
current = current->parent;
}
if (current == 0) {
panic("malformed mount structure - no root");
}
return current;
}
return 0;
}