blob: 68ca487bedb18e4d39b756659e6be33e6e633451 [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>
Al Viro5ad4e532009-03-29 19:50:06 -04006#include <linux/fs_struct.h>
Al Viro3e93cd62009-03-29 19:00:13 -04007
8/*
9 * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
10 * It can block.
11 */
12void set_fs_root(struct fs_struct *fs, struct path *path)
13{
14 struct path old_root;
15
Nick Piggin2a4419b2010-08-18 04:37:33 +100016 spin_lock(&fs->lock);
Nick Pigginc28cc362011-01-07 17:49:53 +110017 write_seqcount_begin(&fs->seq);
Al Viro3e93cd62009-03-29 19:00:13 -040018 old_root = fs->root;
19 fs->root = *path;
Nick Pigginb3e19d92011-01-07 17:50:11 +110020 path_get_long(path);
Nick Pigginc28cc362011-01-07 17:49:53 +110021 write_seqcount_end(&fs->seq);
Nick Piggin2a4419b2010-08-18 04:37:33 +100022 spin_unlock(&fs->lock);
Al Viro3e93cd62009-03-29 19:00:13 -040023 if (old_root.dentry)
Nick Pigginb3e19d92011-01-07 17:50:11 +110024 path_put_long(&old_root);
Al Viro3e93cd62009-03-29 19:00:13 -040025}
26
27/*
28 * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
29 * It can block.
30 */
31void set_fs_pwd(struct fs_struct *fs, struct path *path)
32{
33 struct path old_pwd;
34
Nick Piggin2a4419b2010-08-18 04:37:33 +100035 spin_lock(&fs->lock);
Nick Pigginc28cc362011-01-07 17:49:53 +110036 write_seqcount_begin(&fs->seq);
Al Viro3e93cd62009-03-29 19:00:13 -040037 old_pwd = fs->pwd;
38 fs->pwd = *path;
Nick Pigginb3e19d92011-01-07 17:50:11 +110039 path_get_long(path);
Nick Pigginc28cc362011-01-07 17:49:53 +110040 write_seqcount_end(&fs->seq);
Nick Piggin2a4419b2010-08-18 04:37:33 +100041 spin_unlock(&fs->lock);
Al Viro3e93cd62009-03-29 19:00:13 -040042
43 if (old_pwd.dentry)
Nick Pigginb3e19d92011-01-07 17:50:11 +110044 path_put_long(&old_pwd);
Al Viro3e93cd62009-03-29 19:00:13 -040045}
46
47void chroot_fs_refs(struct path *old_root, struct path *new_root)
48{
49 struct task_struct *g, *p;
50 struct fs_struct *fs;
51 int count = 0;
52
53 read_lock(&tasklist_lock);
54 do_each_thread(g, p) {
55 task_lock(p);
56 fs = p->fs;
57 if (fs) {
Nick Piggin2a4419b2010-08-18 04:37:33 +100058 spin_lock(&fs->lock);
Nick Pigginc28cc362011-01-07 17:49:53 +110059 write_seqcount_begin(&fs->seq);
Al Viro3e93cd62009-03-29 19:00:13 -040060 if (fs->root.dentry == old_root->dentry
61 && fs->root.mnt == old_root->mnt) {
Nick Pigginb3e19d92011-01-07 17:50:11 +110062 path_get_long(new_root);
Al Viro3e93cd62009-03-29 19:00:13 -040063 fs->root = *new_root;
64 count++;
65 }
66 if (fs->pwd.dentry == old_root->dentry
67 && fs->pwd.mnt == old_root->mnt) {
Nick Pigginb3e19d92011-01-07 17:50:11 +110068 path_get_long(new_root);
Al Viro3e93cd62009-03-29 19:00:13 -040069 fs->pwd = *new_root;
70 count++;
71 }
Nick Pigginc28cc362011-01-07 17:49:53 +110072 write_seqcount_end(&fs->seq);
Nick Piggin2a4419b2010-08-18 04:37:33 +100073 spin_unlock(&fs->lock);
Al Viro3e93cd62009-03-29 19:00:13 -040074 }
75 task_unlock(p);
76 } while_each_thread(g, p);
77 read_unlock(&tasklist_lock);
78 while (count--)
Nick Pigginb3e19d92011-01-07 17:50:11 +110079 path_put_long(old_root);
Al Viro3e93cd62009-03-29 19:00:13 -040080}
81
Al Viro498052b2009-03-30 07:20:30 -040082void free_fs_struct(struct fs_struct *fs)
Al Viro3e93cd62009-03-29 19:00:13 -040083{
Nick Pigginb3e19d92011-01-07 17:50:11 +110084 path_put_long(&fs->root);
85 path_put_long(&fs->pwd);
Al Viro498052b2009-03-30 07:20:30 -040086 kmem_cache_free(fs_cachep, fs);
Al Viro3e93cd62009-03-29 19:00:13 -040087}
88
89void exit_fs(struct task_struct *tsk)
90{
Al Viro498052b2009-03-30 07:20:30 -040091 struct fs_struct *fs = tsk->fs;
Al Viro3e93cd62009-03-29 19:00:13 -040092
93 if (fs) {
Al Viro498052b2009-03-30 07:20:30 -040094 int kill;
Al Viro3e93cd62009-03-29 19:00:13 -040095 task_lock(tsk);
Nick Piggin2a4419b2010-08-18 04:37:33 +100096 spin_lock(&fs->lock);
Nick Pigginc28cc362011-01-07 17:49:53 +110097 write_seqcount_begin(&fs->seq);
Al Viro3e93cd62009-03-29 19:00:13 -040098 tsk->fs = NULL;
Al Viro498052b2009-03-30 07:20:30 -040099 kill = !--fs->users;
Nick Pigginc28cc362011-01-07 17:49:53 +1100100 write_seqcount_end(&fs->seq);
Nick Piggin2a4419b2010-08-18 04:37:33 +1000101 spin_unlock(&fs->lock);
Al Viro3e93cd62009-03-29 19:00:13 -0400102 task_unlock(tsk);
Al Viro498052b2009-03-30 07:20:30 -0400103 if (kill)
104 free_fs_struct(fs);
Al Viro3e93cd62009-03-29 19:00:13 -0400105 }
106}
107
108struct fs_struct *copy_fs_struct(struct fs_struct *old)
109{
110 struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
111 /* We don't need to lock fs - think why ;-) */
112 if (fs) {
Al Viro498052b2009-03-30 07:20:30 -0400113 fs->users = 1;
114 fs->in_exec = 0;
Nick Piggin2a4419b2010-08-18 04:37:33 +1000115 spin_lock_init(&fs->lock);
Nick Pigginc28cc362011-01-07 17:49:53 +1100116 seqcount_init(&fs->seq);
Al Viro3e93cd62009-03-29 19:00:13 -0400117 fs->umask = old->umask;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100118
119 spin_lock(&old->lock);
120 fs->root = old->root;
121 path_get_long(&fs->root);
122 fs->pwd = old->pwd;
123 path_get_long(&fs->pwd);
124 spin_unlock(&old->lock);
Al Viro3e93cd62009-03-29 19:00:13 -0400125 }
126 return fs;
127}
128
129int unshare_fs_struct(void)
130{
Al Viro498052b2009-03-30 07:20:30 -0400131 struct fs_struct *fs = current->fs;
132 struct fs_struct *new_fs = copy_fs_struct(fs);
133 int kill;
134
135 if (!new_fs)
Al Viro3e93cd62009-03-29 19:00:13 -0400136 return -ENOMEM;
Al Viro498052b2009-03-30 07:20:30 -0400137
138 task_lock(current);
Nick Piggin2a4419b2010-08-18 04:37:33 +1000139 spin_lock(&fs->lock);
Al Viro498052b2009-03-30 07:20:30 -0400140 kill = !--fs->users;
141 current->fs = new_fs;
Nick Piggin2a4419b2010-08-18 04:37:33 +1000142 spin_unlock(&fs->lock);
Al Viro498052b2009-03-30 07:20:30 -0400143 task_unlock(current);
144
145 if (kill)
146 free_fs_struct(fs);
147
Al Viro3e93cd62009-03-29 19:00:13 -0400148 return 0;
149}
150EXPORT_SYMBOL_GPL(unshare_fs_struct);
151
Al Viroce3b0f82009-03-29 19:08:22 -0400152int current_umask(void)
153{
154 return current->fs->umask;
155}
156EXPORT_SYMBOL(current_umask);
157
Al Viro3e93cd62009-03-29 19:00:13 -0400158/* to be mentioned only in INIT_TASK */
159struct fs_struct init_fs = {
Al Viro498052b2009-03-30 07:20:30 -0400160 .users = 1,
Nick Piggin2a4419b2010-08-18 04:37:33 +1000161 .lock = __SPIN_LOCK_UNLOCKED(init_fs.lock),
Nick Pigginc28cc362011-01-07 17:49:53 +1100162 .seq = SEQCNT_ZERO,
Al Viro3e93cd62009-03-29 19:00:13 -0400163 .umask = 0022,
164};
165
166void daemonize_fs_struct(void)
167{
Al Viro498052b2009-03-30 07:20:30 -0400168 struct fs_struct *fs = current->fs;
Al Viro3e93cd62009-03-29 19:00:13 -0400169
Al Viro498052b2009-03-30 07:20:30 -0400170 if (fs) {
171 int kill;
172
173 task_lock(current);
174
Nick Piggin2a4419b2010-08-18 04:37:33 +1000175 spin_lock(&init_fs.lock);
Al Viro498052b2009-03-30 07:20:30 -0400176 init_fs.users++;
Nick Piggin2a4419b2010-08-18 04:37:33 +1000177 spin_unlock(&init_fs.lock);
Al Viro498052b2009-03-30 07:20:30 -0400178
Nick Piggin2a4419b2010-08-18 04:37:33 +1000179 spin_lock(&fs->lock);
Al Viro498052b2009-03-30 07:20:30 -0400180 current->fs = &init_fs;
181 kill = !--fs->users;
Nick Piggin2a4419b2010-08-18 04:37:33 +1000182 spin_unlock(&fs->lock);
Al Viro498052b2009-03-30 07:20:30 -0400183
184 task_unlock(current);
185 if (kill)
186 free_fs_struct(fs);
187 }
Al Viro3e93cd62009-03-29 19:00:13 -0400188}