blob: f0976f1525bc3b8e35744be77efbc0467e75ca87 [file] [log] [blame]
Paul Gortmaker630d9c42011-11-16 23:57:37 -05001#include <linux/export.h>
Al Viro3e93cd62009-03-29 19:00:13 -04002#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 Virof03c6592011-01-14 22:30:21 -05007#include "internal.h"
8
Al Viro3e93cd62009-03-29 19:00:13 -04009/*
10 * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
11 * It can block.
12 */
13void set_fs_root(struct fs_struct *fs, struct path *path)
14{
15 struct path old_root;
16
Al Virod9eb5042012-06-09 00:59:08 -040017 path_get(path);
Nick Piggin2a4419b2010-08-18 04:37:33 +100018 spin_lock(&fs->lock);
Nick Pigginc28cc362011-01-07 17:49:53 +110019 write_seqcount_begin(&fs->seq);
Al Viro3e93cd62009-03-29 19:00:13 -040020 old_root = fs->root;
21 fs->root = *path;
Nick Pigginc28cc362011-01-07 17:49:53 +110022 write_seqcount_end(&fs->seq);
Nick Piggin2a4419b2010-08-18 04:37:33 +100023 spin_unlock(&fs->lock);
Al Viro3e93cd62009-03-29 19:00:13 -040024 if (old_root.dentry)
Al Virod9eb5042012-06-09 00:59:08 -040025 path_put(&old_root);
Al Viro3e93cd62009-03-29 19:00:13 -040026}
27
28/*
29 * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
30 * It can block.
31 */
32void set_fs_pwd(struct fs_struct *fs, struct path *path)
33{
34 struct path old_pwd;
35
Al Virod9eb5042012-06-09 00:59:08 -040036 path_get(path);
Nick Piggin2a4419b2010-08-18 04:37:33 +100037 spin_lock(&fs->lock);
Nick Pigginc28cc362011-01-07 17:49:53 +110038 write_seqcount_begin(&fs->seq);
Al Viro3e93cd62009-03-29 19:00:13 -040039 old_pwd = fs->pwd;
40 fs->pwd = *path;
Nick Pigginc28cc362011-01-07 17:49:53 +110041 write_seqcount_end(&fs->seq);
Nick Piggin2a4419b2010-08-18 04:37:33 +100042 spin_unlock(&fs->lock);
Al Viro3e93cd62009-03-29 19:00:13 -040043
44 if (old_pwd.dentry)
Al Virod9eb5042012-06-09 00:59:08 -040045 path_put(&old_pwd);
Al Viro3e93cd62009-03-29 19:00:13 -040046}
47
Al Viro82234e62012-03-15 14:48:55 -040048static inline int replace_path(struct path *p, const struct path *old, const struct path *new)
49{
50 if (likely(p->dentry != old->dentry || p->mnt != old->mnt))
51 return 0;
52 *p = *new;
53 return 1;
54}
55
Al Viro3e93cd62009-03-29 19:00:13 -040056void chroot_fs_refs(struct path *old_root, struct path *new_root)
57{
58 struct task_struct *g, *p;
59 struct fs_struct *fs;
60 int count = 0;
61
62 read_lock(&tasklist_lock);
63 do_each_thread(g, p) {
64 task_lock(p);
65 fs = p->fs;
66 if (fs) {
Al Viro82234e62012-03-15 14:48:55 -040067 int hits = 0;
Nick Piggin2a4419b2010-08-18 04:37:33 +100068 spin_lock(&fs->lock);
Nick Pigginc28cc362011-01-07 17:49:53 +110069 write_seqcount_begin(&fs->seq);
Al Viro82234e62012-03-15 14:48:55 -040070 hits += replace_path(&fs->root, old_root, new_root);
71 hits += replace_path(&fs->pwd, old_root, new_root);
Nick Pigginc28cc362011-01-07 17:49:53 +110072 write_seqcount_end(&fs->seq);
Al Viro82234e62012-03-15 14:48:55 -040073 while (hits--) {
74 count++;
Al Virod9eb5042012-06-09 00:59:08 -040075 path_get(new_root);
Al Viro82234e62012-03-15 14:48:55 -040076 }
Nick Piggin2a4419b2010-08-18 04:37:33 +100077 spin_unlock(&fs->lock);
Al Viro3e93cd62009-03-29 19:00:13 -040078 }
79 task_unlock(p);
80 } while_each_thread(g, p);
81 read_unlock(&tasklist_lock);
82 while (count--)
Al Virod9eb5042012-06-09 00:59:08 -040083 path_put(old_root);
Al Viro3e93cd62009-03-29 19:00:13 -040084}
85
Al Viro498052b2009-03-30 07:20:30 -040086void free_fs_struct(struct fs_struct *fs)
Al Viro3e93cd62009-03-29 19:00:13 -040087{
Al Virod9eb5042012-06-09 00:59:08 -040088 path_put(&fs->root);
89 path_put(&fs->pwd);
Al Viro498052b2009-03-30 07:20:30 -040090 kmem_cache_free(fs_cachep, fs);
Al Viro3e93cd62009-03-29 19:00:13 -040091}
92
93void exit_fs(struct task_struct *tsk)
94{
Al Viro498052b2009-03-30 07:20:30 -040095 struct fs_struct *fs = tsk->fs;
Al Viro3e93cd62009-03-29 19:00:13 -040096
97 if (fs) {
Al Viro498052b2009-03-30 07:20:30 -040098 int kill;
Al Viro3e93cd62009-03-29 19:00:13 -040099 task_lock(tsk);
Nick Piggin2a4419b2010-08-18 04:37:33 +1000100 spin_lock(&fs->lock);
Al Viro3e93cd62009-03-29 19:00:13 -0400101 tsk->fs = NULL;
Al Viro498052b2009-03-30 07:20:30 -0400102 kill = !--fs->users;
Nick Piggin2a4419b2010-08-18 04:37:33 +1000103 spin_unlock(&fs->lock);
Al Viro3e93cd62009-03-29 19:00:13 -0400104 task_unlock(tsk);
Al Viro498052b2009-03-30 07:20:30 -0400105 if (kill)
106 free_fs_struct(fs);
Al Viro3e93cd62009-03-29 19:00:13 -0400107 }
108}
109
110struct fs_struct *copy_fs_struct(struct fs_struct *old)
111{
112 struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
113 /* We don't need to lock fs - think why ;-) */
114 if (fs) {
Al Viro498052b2009-03-30 07:20:30 -0400115 fs->users = 1;
116 fs->in_exec = 0;
Nick Piggin2a4419b2010-08-18 04:37:33 +1000117 spin_lock_init(&fs->lock);
Nick Pigginc28cc362011-01-07 17:49:53 +1100118 seqcount_init(&fs->seq);
Al Viro3e93cd62009-03-29 19:00:13 -0400119 fs->umask = old->umask;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100120
121 spin_lock(&old->lock);
122 fs->root = old->root;
Al Virod9eb5042012-06-09 00:59:08 -0400123 path_get(&fs->root);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100124 fs->pwd = old->pwd;
Al Virod9eb5042012-06-09 00:59:08 -0400125 path_get(&fs->pwd);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100126 spin_unlock(&old->lock);
Al Viro3e93cd62009-03-29 19:00:13 -0400127 }
128 return fs;
129}
Daniel Rosenbergeb0f4392016-04-13 16:38:34 -0700130EXPORT_SYMBOL_GPL(copy_fs_struct);
Al Viro3e93cd62009-03-29 19:00:13 -0400131
132int unshare_fs_struct(void)
133{
Al Viro498052b2009-03-30 07:20:30 -0400134 struct fs_struct *fs = current->fs;
135 struct fs_struct *new_fs = copy_fs_struct(fs);
136 int kill;
137
138 if (!new_fs)
Al Viro3e93cd62009-03-29 19:00:13 -0400139 return -ENOMEM;
Al Viro498052b2009-03-30 07:20:30 -0400140
141 task_lock(current);
Nick Piggin2a4419b2010-08-18 04:37:33 +1000142 spin_lock(&fs->lock);
Al Viro498052b2009-03-30 07:20:30 -0400143 kill = !--fs->users;
144 current->fs = new_fs;
Nick Piggin2a4419b2010-08-18 04:37:33 +1000145 spin_unlock(&fs->lock);
Al Viro498052b2009-03-30 07:20:30 -0400146 task_unlock(current);
147
148 if (kill)
149 free_fs_struct(fs);
150
Al Viro3e93cd62009-03-29 19:00:13 -0400151 return 0;
152}
153EXPORT_SYMBOL_GPL(unshare_fs_struct);
154
Al Viroce3b0f82009-03-29 19:08:22 -0400155int current_umask(void)
156{
157 return current->fs->umask;
158}
159EXPORT_SYMBOL(current_umask);
160
Al Viro3e93cd62009-03-29 19:00:13 -0400161/* to be mentioned only in INIT_TASK */
162struct fs_struct init_fs = {
Al Viro498052b2009-03-30 07:20:30 -0400163 .users = 1,
Nick Piggin2a4419b2010-08-18 04:37:33 +1000164 .lock = __SPIN_LOCK_UNLOCKED(init_fs.lock),
Nick Pigginc28cc362011-01-07 17:49:53 +1100165 .seq = SEQCNT_ZERO,
Al Viro3e93cd62009-03-29 19:00:13 -0400166 .umask = 0022,
167};
168
169void daemonize_fs_struct(void)
170{
Al Viro498052b2009-03-30 07:20:30 -0400171 struct fs_struct *fs = current->fs;
Al Viro3e93cd62009-03-29 19:00:13 -0400172
Al Viro498052b2009-03-30 07:20:30 -0400173 if (fs) {
174 int kill;
175
176 task_lock(current);
177
Nick Piggin2a4419b2010-08-18 04:37:33 +1000178 spin_lock(&init_fs.lock);
Al Viro498052b2009-03-30 07:20:30 -0400179 init_fs.users++;
Nick Piggin2a4419b2010-08-18 04:37:33 +1000180 spin_unlock(&init_fs.lock);
Al Viro498052b2009-03-30 07:20:30 -0400181
Nick Piggin2a4419b2010-08-18 04:37:33 +1000182 spin_lock(&fs->lock);
Al Viro498052b2009-03-30 07:20:30 -0400183 current->fs = &init_fs;
184 kill = !--fs->users;
Nick Piggin2a4419b2010-08-18 04:37:33 +1000185 spin_unlock(&fs->lock);
Al Viro498052b2009-03-30 07:20:30 -0400186
187 task_unlock(current);
188 if (kill)
189 free_fs_struct(fs);
190 }
Al Viro3e93cd62009-03-29 19:00:13 -0400191}