no need to play with fs->seq in exit_fs()
[linux-block.git] / fs / fs_struct.c
CommitLineData
3e93cd67
AV
1#include <linux/module.h>
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
9static inline void path_get_longterm(struct path *path)
10{
11 path_get(path);
12 mnt_make_longterm(path->mnt);
13}
14
15static inline void path_put_longterm(struct path *path)
16{
17 mnt_make_shortterm(path->mnt);
18 path_put(path);
19}
3e93cd67
AV
20
21/*
22 * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
23 * It can block.
24 */
25void set_fs_root(struct fs_struct *fs, struct path *path)
26{
27 struct path old_root;
28
2a4419b5 29 spin_lock(&fs->lock);
c28cc364 30 write_seqcount_begin(&fs->seq);
3e93cd67
AV
31 old_root = fs->root;
32 fs->root = *path;
f03c6599 33 path_get_longterm(path);
c28cc364 34 write_seqcount_end(&fs->seq);
2a4419b5 35 spin_unlock(&fs->lock);
3e93cd67 36 if (old_root.dentry)
f03c6599 37 path_put_longterm(&old_root);
3e93cd67
AV
38}
39
40/*
41 * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
42 * It can block.
43 */
44void set_fs_pwd(struct fs_struct *fs, struct path *path)
45{
46 struct path old_pwd;
47
2a4419b5 48 spin_lock(&fs->lock);
c28cc364 49 write_seqcount_begin(&fs->seq);
3e93cd67
AV
50 old_pwd = fs->pwd;
51 fs->pwd = *path;
f03c6599 52 path_get_longterm(path);
c28cc364 53 write_seqcount_end(&fs->seq);
2a4419b5 54 spin_unlock(&fs->lock);
3e93cd67
AV
55
56 if (old_pwd.dentry)
f03c6599 57 path_put_longterm(&old_pwd);
3e93cd67
AV
58}
59
60void chroot_fs_refs(struct path *old_root, struct path *new_root)
61{
62 struct task_struct *g, *p;
63 struct fs_struct *fs;
64 int count = 0;
65
66 read_lock(&tasklist_lock);
67 do_each_thread(g, p) {
68 task_lock(p);
69 fs = p->fs;
70 if (fs) {
2a4419b5 71 spin_lock(&fs->lock);
c28cc364 72 write_seqcount_begin(&fs->seq);
3e93cd67
AV
73 if (fs->root.dentry == old_root->dentry
74 && fs->root.mnt == old_root->mnt) {
f03c6599 75 path_get_longterm(new_root);
3e93cd67
AV
76 fs->root = *new_root;
77 count++;
78 }
79 if (fs->pwd.dentry == old_root->dentry
80 && fs->pwd.mnt == old_root->mnt) {
f03c6599 81 path_get_longterm(new_root);
3e93cd67
AV
82 fs->pwd = *new_root;
83 count++;
84 }
c28cc364 85 write_seqcount_end(&fs->seq);
2a4419b5 86 spin_unlock(&fs->lock);
3e93cd67
AV
87 }
88 task_unlock(p);
89 } while_each_thread(g, p);
90 read_unlock(&tasklist_lock);
91 while (count--)
f03c6599 92 path_put_longterm(old_root);
3e93cd67
AV
93}
94
498052bb 95void free_fs_struct(struct fs_struct *fs)
3e93cd67 96{
f03c6599
AV
97 path_put_longterm(&fs->root);
98 path_put_longterm(&fs->pwd);
498052bb 99 kmem_cache_free(fs_cachep, fs);
3e93cd67
AV
100}
101
102void exit_fs(struct task_struct *tsk)
103{
498052bb 104 struct fs_struct *fs = tsk->fs;
3e93cd67
AV
105
106 if (fs) {
498052bb 107 int kill;
3e93cd67 108 task_lock(tsk);
2a4419b5 109 spin_lock(&fs->lock);
3e93cd67 110 tsk->fs = NULL;
498052bb 111 kill = !--fs->users;
2a4419b5 112 spin_unlock(&fs->lock);
3e93cd67 113 task_unlock(tsk);
498052bb
AV
114 if (kill)
115 free_fs_struct(fs);
3e93cd67
AV
116 }
117}
118
119struct fs_struct *copy_fs_struct(struct fs_struct *old)
120{
121 struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
122 /* We don't need to lock fs - think why ;-) */
123 if (fs) {
498052bb
AV
124 fs->users = 1;
125 fs->in_exec = 0;
2a4419b5 126 spin_lock_init(&fs->lock);
c28cc364 127 seqcount_init(&fs->seq);
3e93cd67 128 fs->umask = old->umask;
b3e19d92
NP
129
130 spin_lock(&old->lock);
131 fs->root = old->root;
f03c6599 132 path_get_longterm(&fs->root);
b3e19d92 133 fs->pwd = old->pwd;
f03c6599 134 path_get_longterm(&fs->pwd);
b3e19d92 135 spin_unlock(&old->lock);
3e93cd67
AV
136 }
137 return fs;
138}
139
140int unshare_fs_struct(void)
141{
498052bb
AV
142 struct fs_struct *fs = current->fs;
143 struct fs_struct *new_fs = copy_fs_struct(fs);
144 int kill;
145
146 if (!new_fs)
3e93cd67 147 return -ENOMEM;
498052bb
AV
148
149 task_lock(current);
2a4419b5 150 spin_lock(&fs->lock);
498052bb
AV
151 kill = !--fs->users;
152 current->fs = new_fs;
2a4419b5 153 spin_unlock(&fs->lock);
498052bb
AV
154 task_unlock(current);
155
156 if (kill)
157 free_fs_struct(fs);
158
3e93cd67
AV
159 return 0;
160}
161EXPORT_SYMBOL_GPL(unshare_fs_struct);
162
ce3b0f8d
AV
163int current_umask(void)
164{
165 return current->fs->umask;
166}
167EXPORT_SYMBOL(current_umask);
168
3e93cd67
AV
169/* to be mentioned only in INIT_TASK */
170struct fs_struct init_fs = {
498052bb 171 .users = 1,
2a4419b5 172 .lock = __SPIN_LOCK_UNLOCKED(init_fs.lock),
c28cc364 173 .seq = SEQCNT_ZERO,
3e93cd67
AV
174 .umask = 0022,
175};
176
177void daemonize_fs_struct(void)
178{
498052bb
AV
179 struct fs_struct *fs = current->fs;
180
181 if (fs) {
182 int kill;
183
184 task_lock(current);
3e93cd67 185
2a4419b5 186 spin_lock(&init_fs.lock);
498052bb 187 init_fs.users++;
2a4419b5 188 spin_unlock(&init_fs.lock);
498052bb 189
2a4419b5 190 spin_lock(&fs->lock);
498052bb
AV
191 current->fs = &init_fs;
192 kill = !--fs->users;
2a4419b5 193 spin_unlock(&fs->lock);
498052bb
AV
194
195 task_unlock(current);
196 if (kill)
197 free_fs_struct(fs);
198 }
3e93cd67 199}