blob: 60b8531f41c52c73be80f26931eb62feb6bc812f [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;
20 path_get(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)
24 path_put(&old_root);
25}
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;
39 path_get(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)
44 path_put(&old_pwd);
45}
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) {
62 path_get(new_root);
63 fs->root = *new_root;
64 count++;
65 }
66 if (fs->pwd.dentry == old_root->dentry
67 && fs->pwd.mnt == old_root->mnt) {
68 path_get(new_root);
69 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--)
79 path_put(old_root);
80}
81
Al Viro498052b2009-03-30 07:20:30 -040082void free_fs_struct(struct fs_struct *fs)
Al Viro3e93cd62009-03-29 19:00:13 -040083{
Al Viro498052b2009-03-30 07:20:30 -040084 path_put(&fs->root);
85 path_put(&fs->pwd);
86 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;
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200118 get_fs_root_and_pwd(old, &fs->root, &fs->pwd);
Al Viro3e93cd62009-03-29 19:00:13 -0400119 }
120 return fs;
121}
122
123int unshare_fs_struct(void)
124{
Al Viro498052b2009-03-30 07:20:30 -0400125 struct fs_struct *fs = current->fs;
126 struct fs_struct *new_fs = copy_fs_struct(fs);
127 int kill;
128
129 if (!new_fs)
Al Viro3e93cd62009-03-29 19:00:13 -0400130 return -ENOMEM;
Al Viro498052b2009-03-30 07:20:30 -0400131
132 task_lock(current);
Nick Piggin2a4419b2010-08-18 04:37:33 +1000133 spin_lock(&fs->lock);
Al Viro498052b2009-03-30 07:20:30 -0400134 kill = !--fs->users;
135 current->fs = new_fs;
Nick Piggin2a4419b2010-08-18 04:37:33 +1000136 spin_unlock(&fs->lock);
Al Viro498052b2009-03-30 07:20:30 -0400137 task_unlock(current);
138
139 if (kill)
140 free_fs_struct(fs);
141
Al Viro3e93cd62009-03-29 19:00:13 -0400142 return 0;
143}
144EXPORT_SYMBOL_GPL(unshare_fs_struct);
145
Al Viroce3b0f82009-03-29 19:08:22 -0400146int current_umask(void)
147{
148 return current->fs->umask;
149}
150EXPORT_SYMBOL(current_umask);
151
Al Viro3e93cd62009-03-29 19:00:13 -0400152/* to be mentioned only in INIT_TASK */
153struct fs_struct init_fs = {
Al Viro498052b2009-03-30 07:20:30 -0400154 .users = 1,
Nick Piggin2a4419b2010-08-18 04:37:33 +1000155 .lock = __SPIN_LOCK_UNLOCKED(init_fs.lock),
Nick Pigginc28cc362011-01-07 17:49:53 +1100156 .seq = SEQCNT_ZERO,
Al Viro3e93cd62009-03-29 19:00:13 -0400157 .umask = 0022,
158};
159
160void daemonize_fs_struct(void)
161{
Al Viro498052b2009-03-30 07:20:30 -0400162 struct fs_struct *fs = current->fs;
Al Viro3e93cd62009-03-29 19:00:13 -0400163
Al Viro498052b2009-03-30 07:20:30 -0400164 if (fs) {
165 int kill;
166
167 task_lock(current);
168
Nick Piggin2a4419b2010-08-18 04:37:33 +1000169 spin_lock(&init_fs.lock);
Al Viro498052b2009-03-30 07:20:30 -0400170 init_fs.users++;
Nick Piggin2a4419b2010-08-18 04:37:33 +1000171 spin_unlock(&init_fs.lock);
Al Viro498052b2009-03-30 07:20:30 -0400172
Nick Piggin2a4419b2010-08-18 04:37:33 +1000173 spin_lock(&fs->lock);
Al Viro498052b2009-03-30 07:20:30 -0400174 current->fs = &init_fs;
175 kill = !--fs->users;
Nick Piggin2a4419b2010-08-18 04:37:33 +1000176 spin_unlock(&fs->lock);
Al Viro498052b2009-03-30 07:20:30 -0400177
178 task_unlock(current);
179 if (kill)
180 free_fs_struct(fs);
181 }
Al Viro3e93cd62009-03-29 19:00:13 -0400182}