Commit | Line | Data |
---|---|---|
630d9c47 | 1 | #include <linux/export.h> |
3e93cd67 AV |
2 | #include <linux/sched.h> |
3 | #include <linux/fs.h> | |
4 | #include <linux/path.h> | |
5 | #include <linux/slab.h> | |
5ad4e53b | 6 | #include <linux/fs_struct.h> |
f03c6599 AV |
7 | #include "internal.h" |
8 | ||
3e93cd67 AV |
9 | /* |
10 | * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values. | |
11 | * It can block. | |
12 | */ | |
dcf787f3 | 13 | void set_fs_root(struct fs_struct *fs, const struct path *path) |
3e93cd67 AV |
14 | { |
15 | struct path old_root; | |
16 | ||
f7a99c5b | 17 | path_get(path); |
2a4419b5 | 18 | spin_lock(&fs->lock); |
c28cc364 | 19 | write_seqcount_begin(&fs->seq); |
3e93cd67 AV |
20 | old_root = fs->root; |
21 | fs->root = *path; | |
c28cc364 | 22 | write_seqcount_end(&fs->seq); |
2a4419b5 | 23 | spin_unlock(&fs->lock); |
3e93cd67 | 24 | if (old_root.dentry) |
f7a99c5b | 25 | path_put(&old_root); |
3e93cd67 AV |
26 | } |
27 | ||
28 | /* | |
29 | * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values. | |
30 | * It can block. | |
31 | */ | |
dcf787f3 | 32 | void set_fs_pwd(struct fs_struct *fs, const struct path *path) |
3e93cd67 AV |
33 | { |
34 | struct path old_pwd; | |
35 | ||
f7a99c5b | 36 | path_get(path); |
2a4419b5 | 37 | spin_lock(&fs->lock); |
c28cc364 | 38 | write_seqcount_begin(&fs->seq); |
3e93cd67 AV |
39 | old_pwd = fs->pwd; |
40 | fs->pwd = *path; | |
c28cc364 | 41 | write_seqcount_end(&fs->seq); |
2a4419b5 | 42 | spin_unlock(&fs->lock); |
3e93cd67 AV |
43 | |
44 | if (old_pwd.dentry) | |
f7a99c5b | 45 | path_put(&old_pwd); |
3e93cd67 | 46 | } |
1cd3d347 | 47 | EXPORT_SYMBOL(set_fs_pwd); |
3e93cd67 | 48 | |
82234e61 AV |
49 | static inline int replace_path(struct path *p, const struct path *old, const struct path *new) |
50 | { | |
51 | if (likely(p->dentry != old->dentry || p->mnt != old->mnt)) | |
52 | return 0; | |
53 | *p = *new; | |
54 | return 1; | |
55 | } | |
56 | ||
dcf787f3 | 57 | void chroot_fs_refs(const struct path *old_root, const struct path *new_root) |
3e93cd67 AV |
58 | { |
59 | struct task_struct *g, *p; | |
60 | struct fs_struct *fs; | |
61 | int count = 0; | |
62 | ||
63 | read_lock(&tasklist_lock); | |
64 | do_each_thread(g, p) { | |
65 | task_lock(p); | |
66 | fs = p->fs; | |
67 | if (fs) { | |
82234e61 | 68 | int hits = 0; |
2a4419b5 | 69 | spin_lock(&fs->lock); |
c28cc364 | 70 | write_seqcount_begin(&fs->seq); |
82234e61 AV |
71 | hits += replace_path(&fs->root, old_root, new_root); |
72 | hits += replace_path(&fs->pwd, old_root, new_root); | |
73 | write_seqcount_end(&fs->seq); | |
74 | while (hits--) { | |
3e93cd67 | 75 | count++; |
f7a99c5b | 76 | path_get(new_root); |
3e93cd67 | 77 | } |
2a4419b5 | 78 | spin_unlock(&fs->lock); |
3e93cd67 AV |
79 | } |
80 | task_unlock(p); | |
81 | } while_each_thread(g, p); | |
82 | read_unlock(&tasklist_lock); | |
83 | while (count--) | |
f7a99c5b | 84 | path_put(old_root); |
3e93cd67 AV |
85 | } |
86 | ||
498052bb | 87 | void free_fs_struct(struct fs_struct *fs) |
3e93cd67 | 88 | { |
f7a99c5b AV |
89 | path_put(&fs->root); |
90 | path_put(&fs->pwd); | |
498052bb | 91 | kmem_cache_free(fs_cachep, fs); |
3e93cd67 | 92 | } |
1cd3d347 | 93 | EXPORT_SYMBOL(free_fs_struct); |
3e93cd67 AV |
94 | |
95 | void exit_fs(struct task_struct *tsk) | |
96 | { | |
498052bb | 97 | struct fs_struct *fs = tsk->fs; |
3e93cd67 AV |
98 | |
99 | if (fs) { | |
498052bb | 100 | int kill; |
3e93cd67 | 101 | task_lock(tsk); |
2a4419b5 | 102 | spin_lock(&fs->lock); |
3e93cd67 | 103 | tsk->fs = NULL; |
498052bb | 104 | kill = !--fs->users; |
2a4419b5 | 105 | spin_unlock(&fs->lock); |
3e93cd67 | 106 | task_unlock(tsk); |
498052bb AV |
107 | if (kill) |
108 | free_fs_struct(fs); | |
3e93cd67 AV |
109 | } |
110 | } | |
111 | ||
112 | struct fs_struct *copy_fs_struct(struct fs_struct *old) | |
113 | { | |
114 | struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL); | |
115 | /* We don't need to lock fs - think why ;-) */ | |
116 | if (fs) { | |
498052bb AV |
117 | fs->users = 1; |
118 | fs->in_exec = 0; | |
2a4419b5 | 119 | spin_lock_init(&fs->lock); |
c28cc364 | 120 | seqcount_init(&fs->seq); |
3e93cd67 | 121 | fs->umask = old->umask; |
b3e19d92 NP |
122 | |
123 | spin_lock(&old->lock); | |
124 | fs->root = old->root; | |
f7a99c5b | 125 | path_get(&fs->root); |
b3e19d92 | 126 | fs->pwd = old->pwd; |
f7a99c5b | 127 | path_get(&fs->pwd); |
b3e19d92 | 128 | spin_unlock(&old->lock); |
3e93cd67 AV |
129 | } |
130 | return fs; | |
131 | } | |
132 | ||
133 | int unshare_fs_struct(void) | |
134 | { | |
498052bb AV |
135 | struct fs_struct *fs = current->fs; |
136 | struct fs_struct *new_fs = copy_fs_struct(fs); | |
137 | int kill; | |
138 | ||
139 | if (!new_fs) | |
3e93cd67 | 140 | return -ENOMEM; |
498052bb AV |
141 | |
142 | task_lock(current); | |
2a4419b5 | 143 | spin_lock(&fs->lock); |
498052bb AV |
144 | kill = !--fs->users; |
145 | current->fs = new_fs; | |
2a4419b5 | 146 | spin_unlock(&fs->lock); |
498052bb AV |
147 | task_unlock(current); |
148 | ||
149 | if (kill) | |
150 | free_fs_struct(fs); | |
151 | ||
3e93cd67 AV |
152 | return 0; |
153 | } | |
154 | EXPORT_SYMBOL_GPL(unshare_fs_struct); | |
155 | ||
ce3b0f8d AV |
156 | int current_umask(void) |
157 | { | |
158 | return current->fs->umask; | |
159 | } | |
160 | EXPORT_SYMBOL(current_umask); | |
161 | ||
3e93cd67 AV |
162 | /* to be mentioned only in INIT_TASK */ |
163 | struct fs_struct init_fs = { | |
498052bb | 164 | .users = 1, |
2a4419b5 | 165 | .lock = __SPIN_LOCK_UNLOCKED(init_fs.lock), |
1ca7d67c | 166 | .seq = SEQCNT_ZERO(init_fs.seq), |
3e93cd67 AV |
167 | .umask = 0022, |
168 | }; |