blob: 6ac2193386704621e5acb0d08d0f9f1fa4b7138a [file] [log] [blame]
Al Viro3e93cd62009-03-29 19:00:13 -04001#include <linux/module.h>
2#include <linux/sched.h>
3#include <linux/fs.h>
4#include <linux/path.h>
5#include <linux/slab.h>
6
7/*
8 * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
9 * It can block.
10 */
11void set_fs_root(struct fs_struct *fs, struct path *path)
12{
13 struct path old_root;
14
15 write_lock(&fs->lock);
16 old_root = fs->root;
17 fs->root = *path;
18 path_get(path);
19 write_unlock(&fs->lock);
20 if (old_root.dentry)
21 path_put(&old_root);
22}
23
24/*
25 * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
26 * It can block.
27 */
28void set_fs_pwd(struct fs_struct *fs, struct path *path)
29{
30 struct path old_pwd;
31
32 write_lock(&fs->lock);
33 old_pwd = fs->pwd;
34 fs->pwd = *path;
35 path_get(path);
36 write_unlock(&fs->lock);
37
38 if (old_pwd.dentry)
39 path_put(&old_pwd);
40}
41
42void chroot_fs_refs(struct path *old_root, struct path *new_root)
43{
44 struct task_struct *g, *p;
45 struct fs_struct *fs;
46 int count = 0;
47
48 read_lock(&tasklist_lock);
49 do_each_thread(g, p) {
50 task_lock(p);
51 fs = p->fs;
52 if (fs) {
53 write_lock(&fs->lock);
54 if (fs->root.dentry == old_root->dentry
55 && fs->root.mnt == old_root->mnt) {
56 path_get(new_root);
57 fs->root = *new_root;
58 count++;
59 }
60 if (fs->pwd.dentry == old_root->dentry
61 && fs->pwd.mnt == old_root->mnt) {
62 path_get(new_root);
63 fs->pwd = *new_root;
64 count++;
65 }
66 write_unlock(&fs->lock);
67 }
68 task_unlock(p);
69 } while_each_thread(g, p);
70 read_unlock(&tasklist_lock);
71 while (count--)
72 path_put(old_root);
73}
74
Al Viro498052b2009-03-30 07:20:30 -040075void free_fs_struct(struct fs_struct *fs)
Al Viro3e93cd62009-03-29 19:00:13 -040076{
Al Viro498052b2009-03-30 07:20:30 -040077 path_put(&fs->root);
78 path_put(&fs->pwd);
79 kmem_cache_free(fs_cachep, fs);
Al Viro3e93cd62009-03-29 19:00:13 -040080}
81
82void exit_fs(struct task_struct *tsk)
83{
Al Viro498052b2009-03-30 07:20:30 -040084 struct fs_struct *fs = tsk->fs;
Al Viro3e93cd62009-03-29 19:00:13 -040085
86 if (fs) {
Al Viro498052b2009-03-30 07:20:30 -040087 int kill;
Al Viro3e93cd62009-03-29 19:00:13 -040088 task_lock(tsk);
Al Viro498052b2009-03-30 07:20:30 -040089 write_lock(&fs->lock);
Al Viro3e93cd62009-03-29 19:00:13 -040090 tsk->fs = NULL;
Al Viro498052b2009-03-30 07:20:30 -040091 kill = !--fs->users;
92 write_unlock(&fs->lock);
Al Viro3e93cd62009-03-29 19:00:13 -040093 task_unlock(tsk);
Al Viro498052b2009-03-30 07:20:30 -040094 if (kill)
95 free_fs_struct(fs);
Al Viro3e93cd62009-03-29 19:00:13 -040096 }
97}
98
99struct fs_struct *copy_fs_struct(struct fs_struct *old)
100{
101 struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
102 /* We don't need to lock fs - think why ;-) */
103 if (fs) {
Al Viro498052b2009-03-30 07:20:30 -0400104 fs->users = 1;
105 fs->in_exec = 0;
Al Viro3e93cd62009-03-29 19:00:13 -0400106 rwlock_init(&fs->lock);
107 fs->umask = old->umask;
108 read_lock(&old->lock);
109 fs->root = old->root;
110 path_get(&old->root);
111 fs->pwd = old->pwd;
112 path_get(&old->pwd);
113 read_unlock(&old->lock);
114 }
115 return fs;
116}
117
118int unshare_fs_struct(void)
119{
Al Viro498052b2009-03-30 07:20:30 -0400120 struct fs_struct *fs = current->fs;
121 struct fs_struct *new_fs = copy_fs_struct(fs);
122 int kill;
123
124 if (!new_fs)
Al Viro3e93cd62009-03-29 19:00:13 -0400125 return -ENOMEM;
Al Viro498052b2009-03-30 07:20:30 -0400126
127 task_lock(current);
128 write_lock(&fs->lock);
129 kill = !--fs->users;
130 current->fs = new_fs;
131 write_unlock(&fs->lock);
132 task_unlock(current);
133
134 if (kill)
135 free_fs_struct(fs);
136
Al Viro3e93cd62009-03-29 19:00:13 -0400137 return 0;
138}
139EXPORT_SYMBOL_GPL(unshare_fs_struct);
140
Al Viroce3b0f82009-03-29 19:08:22 -0400141int current_umask(void)
142{
143 return current->fs->umask;
144}
145EXPORT_SYMBOL(current_umask);
146
Al Viro3e93cd62009-03-29 19:00:13 -0400147/* to be mentioned only in INIT_TASK */
148struct fs_struct init_fs = {
Al Viro498052b2009-03-30 07:20:30 -0400149 .users = 1,
Al Viro3e93cd62009-03-29 19:00:13 -0400150 .lock = __RW_LOCK_UNLOCKED(init_fs.lock),
151 .umask = 0022,
152};
153
154void daemonize_fs_struct(void)
155{
Al Viro498052b2009-03-30 07:20:30 -0400156 struct fs_struct *fs = current->fs;
Al Viro3e93cd62009-03-29 19:00:13 -0400157
Al Viro498052b2009-03-30 07:20:30 -0400158 if (fs) {
159 int kill;
160
161 task_lock(current);
162
163 write_lock(&init_fs.lock);
164 init_fs.users++;
165 write_unlock(&init_fs.lock);
166
167 write_lock(&fs->lock);
168 current->fs = &init_fs;
169 kill = !--fs->users;
170 write_unlock(&fs->lock);
171
172 task_unlock(current);
173 if (kill)
174 free_fs_struct(fs);
175 }
Al Viro3e93cd62009-03-29 19:00:13 -0400176}