blob: d8ac61d0c9320ab194c9df90a1eaf3c858a55c0f [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 */
Al Virodcf787f2013-03-01 23:51:07 -050013void set_fs_root(struct fs_struct *fs, const struct path *path)
Al Viro3e93cd62009-03-29 19:00:13 -040014{
15 struct path old_root;
16
Al Virof7a99c52012-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 Virof7a99c52012-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 */
Al Virodcf787f2013-03-01 23:51:07 -050032void set_fs_pwd(struct fs_struct *fs, const struct path *path)
Al Viro3e93cd62009-03-29 19:00:13 -040033{
34 struct path old_pwd;
35
Al Virof7a99c52012-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 Virof7a99c52012-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 Virodcf787f2013-03-01 23:51:07 -050056void chroot_fs_refs(const struct path *old_root, const struct path *new_root)
Al Viro3e93cd62009-03-29 19:00:13 -040057{
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 Virof7a99c52012-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 Virof7a99c52012-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 Virof7a99c52012-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 Virof7a99c52012-06-09 00:59:08 -0400123 path_get(&fs->root);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100124 fs->pwd = old->pwd;
Al Virof7a99c52012-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}
130
131int unshare_fs_struct(void)
132{
Al Viro498052b2009-03-30 07:20:30 -0400133 struct fs_struct *fs = current->fs;
134 struct fs_struct *new_fs = copy_fs_struct(fs);
135 int kill;
136
137 if (!new_fs)
Al Viro3e93cd62009-03-29 19:00:13 -0400138 return -ENOMEM;
Al Viro498052b2009-03-30 07:20:30 -0400139
140 task_lock(current);
Nick Piggin2a4419b2010-08-18 04:37:33 +1000141 spin_lock(&fs->lock);
Al Viro498052b2009-03-30 07:20:30 -0400142 kill = !--fs->users;
143 current->fs = new_fs;
Nick Piggin2a4419b2010-08-18 04:37:33 +1000144 spin_unlock(&fs->lock);
Al Viro498052b2009-03-30 07:20:30 -0400145 task_unlock(current);
146
147 if (kill)
148 free_fs_struct(fs);
149
Al Viro3e93cd62009-03-29 19:00:13 -0400150 return 0;
151}
152EXPORT_SYMBOL_GPL(unshare_fs_struct);
153
Al Viroce3b0f82009-03-29 19:08:22 -0400154int current_umask(void)
155{
156 return current->fs->umask;
157}
158EXPORT_SYMBOL(current_umask);
159
Al Viro3e93cd62009-03-29 19:00:13 -0400160/* to be mentioned only in INIT_TASK */
161struct fs_struct init_fs = {
Al Viro498052b2009-03-30 07:20:30 -0400162 .users = 1,
Nick Piggin2a4419b2010-08-18 04:37:33 +1000163 .lock = __SPIN_LOCK_UNLOCKED(init_fs.lock),
Nick Pigginc28cc362011-01-07 17:49:53 +1100164 .seq = SEQCNT_ZERO,
Al Viro3e93cd62009-03-29 19:00:13 -0400165 .umask = 0022,
166};