blob: 90f76a379bbd884e32149e27742f4b6f343a2775 [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
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}
Al Viro3e93cd62009-03-29 19:00:13 -040020
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
Al Viro82234e62012-03-15 14:48:55 -040029 path_get_longterm(path);
Nick Piggin2a4419b2010-08-18 04:37:33 +100030 spin_lock(&fs->lock);
Nick Pigginc28cc362011-01-07 17:49:53 +110031 write_seqcount_begin(&fs->seq);
Al Viro3e93cd62009-03-29 19:00:13 -040032 old_root = fs->root;
33 fs->root = *path;
Nick Pigginc28cc362011-01-07 17:49:53 +110034 write_seqcount_end(&fs->seq);
Nick Piggin2a4419b2010-08-18 04:37:33 +100035 spin_unlock(&fs->lock);
Al Viro3e93cd62009-03-29 19:00:13 -040036 if (old_root.dentry)
Al Virof03c6592011-01-14 22:30:21 -050037 path_put_longterm(&old_root);
Al Viro3e93cd62009-03-29 19:00:13 -040038}
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
Al Viro82234e62012-03-15 14:48:55 -040048 path_get_longterm(path);
Nick Piggin2a4419b2010-08-18 04:37:33 +100049 spin_lock(&fs->lock);
Nick Pigginc28cc362011-01-07 17:49:53 +110050 write_seqcount_begin(&fs->seq);
Al Viro3e93cd62009-03-29 19:00:13 -040051 old_pwd = fs->pwd;
52 fs->pwd = *path;
Nick Pigginc28cc362011-01-07 17:49:53 +110053 write_seqcount_end(&fs->seq);
Nick Piggin2a4419b2010-08-18 04:37:33 +100054 spin_unlock(&fs->lock);
Al Viro3e93cd62009-03-29 19:00:13 -040055
56 if (old_pwd.dentry)
Al Virof03c6592011-01-14 22:30:21 -050057 path_put_longterm(&old_pwd);
Al Viro3e93cd62009-03-29 19:00:13 -040058}
59
Al Viro82234e62012-03-15 14:48:55 -040060static inline int replace_path(struct path *p, const struct path *old, const struct path *new)
61{
62 if (likely(p->dentry != old->dentry || p->mnt != old->mnt))
63 return 0;
64 *p = *new;
65 return 1;
66}
67
Al Viro3e93cd62009-03-29 19:00:13 -040068void chroot_fs_refs(struct path *old_root, struct path *new_root)
69{
70 struct task_struct *g, *p;
71 struct fs_struct *fs;
72 int count = 0;
73
74 read_lock(&tasklist_lock);
75 do_each_thread(g, p) {
76 task_lock(p);
77 fs = p->fs;
78 if (fs) {
Al Viro82234e62012-03-15 14:48:55 -040079 int hits = 0;
Nick Piggin2a4419b2010-08-18 04:37:33 +100080 spin_lock(&fs->lock);
Nick Pigginc28cc362011-01-07 17:49:53 +110081 write_seqcount_begin(&fs->seq);
Al Viro82234e62012-03-15 14:48:55 -040082 hits += replace_path(&fs->root, old_root, new_root);
83 hits += replace_path(&fs->pwd, old_root, new_root);
Nick Pigginc28cc362011-01-07 17:49:53 +110084 write_seqcount_end(&fs->seq);
Al Viro82234e62012-03-15 14:48:55 -040085 while (hits--) {
86 count++;
87 path_get_longterm(new_root);
88 }
Nick Piggin2a4419b2010-08-18 04:37:33 +100089 spin_unlock(&fs->lock);
Al Viro3e93cd62009-03-29 19:00:13 -040090 }
91 task_unlock(p);
92 } while_each_thread(g, p);
93 read_unlock(&tasklist_lock);
94 while (count--)
Al Virof03c6592011-01-14 22:30:21 -050095 path_put_longterm(old_root);
Al Viro3e93cd62009-03-29 19:00:13 -040096}
97
Al Viro498052b2009-03-30 07:20:30 -040098void free_fs_struct(struct fs_struct *fs)
Al Viro3e93cd62009-03-29 19:00:13 -040099{
Al Virof03c6592011-01-14 22:30:21 -0500100 path_put_longterm(&fs->root);
101 path_put_longterm(&fs->pwd);
Al Viro498052b2009-03-30 07:20:30 -0400102 kmem_cache_free(fs_cachep, fs);
Al Viro3e93cd62009-03-29 19:00:13 -0400103}
104
105void exit_fs(struct task_struct *tsk)
106{
Al Viro498052b2009-03-30 07:20:30 -0400107 struct fs_struct *fs = tsk->fs;
Al Viro3e93cd62009-03-29 19:00:13 -0400108
109 if (fs) {
Al Viro498052b2009-03-30 07:20:30 -0400110 int kill;
Al Viro3e93cd62009-03-29 19:00:13 -0400111 task_lock(tsk);
Nick Piggin2a4419b2010-08-18 04:37:33 +1000112 spin_lock(&fs->lock);
Al Viro3e93cd62009-03-29 19:00:13 -0400113 tsk->fs = NULL;
Al Viro498052b2009-03-30 07:20:30 -0400114 kill = !--fs->users;
Nick Piggin2a4419b2010-08-18 04:37:33 +1000115 spin_unlock(&fs->lock);
Al Viro3e93cd62009-03-29 19:00:13 -0400116 task_unlock(tsk);
Al Viro498052b2009-03-30 07:20:30 -0400117 if (kill)
118 free_fs_struct(fs);
Al Viro3e93cd62009-03-29 19:00:13 -0400119 }
120}
121
122struct fs_struct *copy_fs_struct(struct fs_struct *old)
123{
124 struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
125 /* We don't need to lock fs - think why ;-) */
126 if (fs) {
Al Viro498052b2009-03-30 07:20:30 -0400127 fs->users = 1;
128 fs->in_exec = 0;
Nick Piggin2a4419b2010-08-18 04:37:33 +1000129 spin_lock_init(&fs->lock);
Nick Pigginc28cc362011-01-07 17:49:53 +1100130 seqcount_init(&fs->seq);
Al Viro3e93cd62009-03-29 19:00:13 -0400131 fs->umask = old->umask;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100132
133 spin_lock(&old->lock);
134 fs->root = old->root;
Al Virof03c6592011-01-14 22:30:21 -0500135 path_get_longterm(&fs->root);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100136 fs->pwd = old->pwd;
Al Virof03c6592011-01-14 22:30:21 -0500137 path_get_longterm(&fs->pwd);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100138 spin_unlock(&old->lock);
Al Viro3e93cd62009-03-29 19:00:13 -0400139 }
140 return fs;
141}
142
Daniel Rosenberg8843b432016-04-13 16:38:34 -0700143EXPORT_SYMBOL_GPL(copy_fs_struct);
Al Viro3e93cd62009-03-29 19:00:13 -0400144int unshare_fs_struct(void)
145{
Al Viro498052b2009-03-30 07:20:30 -0400146 struct fs_struct *fs = current->fs;
147 struct fs_struct *new_fs = copy_fs_struct(fs);
148 int kill;
149
150 if (!new_fs)
Al Viro3e93cd62009-03-29 19:00:13 -0400151 return -ENOMEM;
Al Viro498052b2009-03-30 07:20:30 -0400152
153 task_lock(current);
Nick Piggin2a4419b2010-08-18 04:37:33 +1000154 spin_lock(&fs->lock);
Al Viro498052b2009-03-30 07:20:30 -0400155 kill = !--fs->users;
156 current->fs = new_fs;
Nick Piggin2a4419b2010-08-18 04:37:33 +1000157 spin_unlock(&fs->lock);
Al Viro498052b2009-03-30 07:20:30 -0400158 task_unlock(current);
159
160 if (kill)
161 free_fs_struct(fs);
162
Al Viro3e93cd62009-03-29 19:00:13 -0400163 return 0;
164}
165EXPORT_SYMBOL_GPL(unshare_fs_struct);
166
Al Viroce3b0f82009-03-29 19:08:22 -0400167int current_umask(void)
168{
169 return current->fs->umask;
170}
171EXPORT_SYMBOL(current_umask);
172
Al Viro3e93cd62009-03-29 19:00:13 -0400173/* to be mentioned only in INIT_TASK */
174struct fs_struct init_fs = {
Al Viro498052b2009-03-30 07:20:30 -0400175 .users = 1,
Nick Piggin2a4419b2010-08-18 04:37:33 +1000176 .lock = __SPIN_LOCK_UNLOCKED(init_fs.lock),
Nick Pigginc28cc362011-01-07 17:49:53 +1100177 .seq = SEQCNT_ZERO,
Al Viro3e93cd62009-03-29 19:00:13 -0400178 .umask = 0022,
179};
180
181void daemonize_fs_struct(void)
182{
Al Viro498052b2009-03-30 07:20:30 -0400183 struct fs_struct *fs = current->fs;
Al Viro3e93cd62009-03-29 19:00:13 -0400184
Al Viro498052b2009-03-30 07:20:30 -0400185 if (fs) {
186 int kill;
187
188 task_lock(current);
189
Nick Piggin2a4419b2010-08-18 04:37:33 +1000190 spin_lock(&init_fs.lock);
Al Viro498052b2009-03-30 07:20:30 -0400191 init_fs.users++;
Nick Piggin2a4419b2010-08-18 04:37:33 +1000192 spin_unlock(&init_fs.lock);
Al Viro498052b2009-03-30 07:20:30 -0400193
Nick Piggin2a4419b2010-08-18 04:37:33 +1000194 spin_lock(&fs->lock);
Al Viro498052b2009-03-30 07:20:30 -0400195 current->fs = &init_fs;
196 kill = !--fs->users;
Nick Piggin2a4419b2010-08-18 04:37:33 +1000197 spin_unlock(&fs->lock);
Al Viro498052b2009-03-30 07:20:30 -0400198
199 task_unlock(current);
200 if (kill)
201 free_fs_struct(fs);
202 }
Al Viro3e93cd62009-03-29 19:00:13 -0400203}