forked from davidsaOpenu/xv6-ns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mount_ns.c
80 lines (69 loc) · 1.63 KB
/
mount_ns.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
#include "types.h"
#include "defs.h"
#include "param.h"
#include "stat.h"
#include "mmu.h"
#include "proc.h"
#include "spinlock.h"
#include "fs.h"
#include "sleeplock.h"
#include "file.h"
#include "mount.h"
#include "namespace.h"
#include "mount_ns.h"
struct {
struct spinlock lock;
struct mount_ns mount_ns[NNAMESPACE];
} mountnstable;
void mount_nsinit()
{
initlock(&mountnstable.lock, "mountns");
for (int i = 0; i < NNAMESPACE; i++) {
initlock(&mountnstable.mount_ns[i].lock, "mount_ns");
}
}
struct mount_ns* mount_nsdup(struct mount_ns* mount_ns)
{
acquire(&mountnstable.lock);
mount_ns->ref++;
release(&mountnstable.lock);
return mount_ns;
}
void mount_nsput(struct mount_ns* mount_ns)
{
acquire(&mountnstable.lock);
if (mount_ns->ref == 1) {
release(&mountnstable.lock);
umountall(mount_ns->active_mounts);
mount_ns->active_mounts = 0;
acquire(&mountnstable.lock);
}
mount_ns->ref--;
release(&mountnstable.lock);
}
static struct mount_ns* allocmount_ns()
{
acquire(&mountnstable.lock);
for (int i = 0; i < NNAMESPACE; i++) {
if (mountnstable.mount_ns[i].ref == 0) {
struct mount_ns* mount_ns = &mountnstable.mount_ns[i];
mount_ns->ref = 1;
release(&mountnstable.lock);
return mount_ns;
}
}
release(&mountnstable.lock);
panic("out of mount_ns objects");
}
struct mount_ns* copymount_ns()
{
struct mount_ns* mount_ns = allocmount_ns();
mount_ns->active_mounts = copyactivemounts();
mount_ns->root = getroot(mount_ns->active_mounts);
return mount_ns;
}
struct mount_ns* newmount_ns()
{
struct mount_ns* mount_ns = allocmount_ns();
return mount_ns;
}