blob: 940c683561dd377e33ff791ce6b406a45518dd54 [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}
Guenter Roeck0de4c4c2017-01-30 12:26:08 -080047EXPORT_SYMBOL(set_fs_pwd);
Al Viro3e93cd62009-03-29 19:00:13 -040048
Al Viro82234e62012-03-15 14:48:55 -040049static inline int replace_path(struct path *p, const struct path *old, const struct path *new)
50{
51 if (likely(p->dentry != old->dentry || p->mnt != old->mnt))
52 return 0;
53 *p = *new;
54 return 1;
55}
56
Al Virodcf787f2013-03-01 23:51:07 -050057void chroot_fs_refs(const struct path *old_root, const struct path *new_root)
Al Viro3e93cd62009-03-29 19:00:13 -040058{
59 struct task_struct *g, *p;
60 struct fs_struct *fs;
61 int count = 0;
62
63 read_lock(&tasklist_lock);
64 do_each_thread(g, p) {
65 task_lock(p);
66 fs = p->fs;
67 if (fs) {
Al Viro82234e62012-03-15 14:48:55 -040068 int hits = 0;
Nick Piggin2a4419b2010-08-18 04:37:33 +100069 spin_lock(&fs->lock);
Nick Pigginc28cc362011-01-07 17:49:53 +110070 write_seqcount_begin(&fs->seq);
Al Viro82234e62012-03-15 14:48:55 -040071 hits += replace_path(&fs->root, old_root, new_root);
72 hits += replace_path(&fs->pwd, old_root, new_root);
Nick Pigginc28cc362011-01-07 17:49:53 +110073 write_seqcount_end(&fs->seq);
Al Viro82234e62012-03-15 14:48:55 -040074 while (hits--) {
75 count++;
Al Virof7a99c52012-06-09 00:59:08 -040076 path_get(new_root);
Al Viro82234e62012-03-15 14:48:55 -040077 }
Nick Piggin2a4419b2010-08-18 04:37:33 +100078 spin_unlock(&fs->lock);
Al Viro3e93cd62009-03-29 19:00:13 -040079 }
80 task_unlock(p);
81 } while_each_thread(g, p);
82 read_unlock(&tasklist_lock);
83 while (count--)
Al Virof7a99c52012-06-09 00:59:08 -040084 path_put(old_root);
Al Viro3e93cd62009-03-29 19:00:13 -040085}
86
Al Viro498052b2009-03-30 07:20:30 -040087void free_fs_struct(struct fs_struct *fs)
Al Viro3e93cd62009-03-29 19:00:13 -040088{
Al Virof7a99c52012-06-09 00:59:08 -040089 path_put(&fs->root);
90 path_put(&fs->pwd);
Al Viro498052b2009-03-30 07:20:30 -040091 kmem_cache_free(fs_cachep, fs);
Al Viro3e93cd62009-03-29 19:00:13 -040092}
Guenter Roeck0de4c4c2017-01-30 12:26:08 -080093EXPORT_SYMBOL(free_fs_struct);
Al Viro3e93cd62009-03-29 19:00:13 -040094
95void exit_fs(struct task_struct *tsk)
96{
Al Viro498052b2009-03-30 07:20:30 -040097 struct fs_struct *fs = tsk->fs;
Al Viro3e93cd62009-03-29 19:00:13 -040098
99 if (fs) {
Al Viro498052b2009-03-30 07:20:30 -0400100 int kill;
Al Viro3e93cd62009-03-29 19:00:13 -0400101 task_lock(tsk);
Nick Piggin2a4419b2010-08-18 04:37:33 +1000102 spin_lock(&fs->lock);
Al Viro3e93cd62009-03-29 19:00:13 -0400103 tsk->fs = NULL;
Al Viro498052b2009-03-30 07:20:30 -0400104 kill = !--fs->users;
Nick Piggin2a4419b2010-08-18 04:37:33 +1000105 spin_unlock(&fs->lock);
Al Viro3e93cd62009-03-29 19:00:13 -0400106 task_unlock(tsk);
Al Viro498052b2009-03-30 07:20:30 -0400107 if (kill)
108 free_fs_struct(fs);
Al Viro3e93cd62009-03-29 19:00:13 -0400109 }
110}
111
112struct fs_struct *copy_fs_struct(struct fs_struct *old)
113{
114 struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
115 /* We don't need to lock fs - think why ;-) */
116 if (fs) {
Al Viro498052b2009-03-30 07:20:30 -0400117 fs->users = 1;
118 fs->in_exec = 0;
Nick Piggin2a4419b2010-08-18 04:37:33 +1000119 spin_lock_init(&fs->lock);
Nick Pigginc28cc362011-01-07 17:49:53 +1100120 seqcount_init(&fs->seq);
Al Viro3e93cd62009-03-29 19:00:13 -0400121 fs->umask = old->umask;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100122
123 spin_lock(&old->lock);
124 fs->root = old->root;
Al Virof7a99c52012-06-09 00:59:08 -0400125 path_get(&fs->root);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100126 fs->pwd = old->pwd;
Al Virof7a99c52012-06-09 00:59:08 -0400127 path_get(&fs->pwd);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100128 spin_unlock(&old->lock);
Al Viro3e93cd62009-03-29 19:00:13 -0400129 }
130 return fs;
131}
Daniel Rosenberg2b314622016-04-13 16:38:34 -0700132EXPORT_SYMBOL_GPL(copy_fs_struct);
Al Viro3e93cd62009-03-29 19:00:13 -0400133
134int unshare_fs_struct(void)
135{
Al Viro498052b2009-03-30 07:20:30 -0400136 struct fs_struct *fs = current->fs;
137 struct fs_struct *new_fs = copy_fs_struct(fs);
138 int kill;
139
140 if (!new_fs)
Al Viro3e93cd62009-03-29 19:00:13 -0400141 return -ENOMEM;
Al Viro498052b2009-03-30 07:20:30 -0400142
143 task_lock(current);
Nick Piggin2a4419b2010-08-18 04:37:33 +1000144 spin_lock(&fs->lock);
Al Viro498052b2009-03-30 07:20:30 -0400145 kill = !--fs->users;
146 current->fs = new_fs;
Nick Piggin2a4419b2010-08-18 04:37:33 +1000147 spin_unlock(&fs->lock);
Al Viro498052b2009-03-30 07:20:30 -0400148 task_unlock(current);
149
150 if (kill)
151 free_fs_struct(fs);
152
Al Viro3e93cd62009-03-29 19:00:13 -0400153 return 0;
154}
155EXPORT_SYMBOL_GPL(unshare_fs_struct);
156
Al Viroce3b0f82009-03-29 19:08:22 -0400157int current_umask(void)
158{
159 return current->fs->umask;
160}
161EXPORT_SYMBOL(current_umask);
162
Al Viro3e93cd62009-03-29 19:00:13 -0400163/* to be mentioned only in INIT_TASK */
164struct fs_struct init_fs = {
Al Viro498052b2009-03-30 07:20:30 -0400165 .users = 1,
Nick Piggin2a4419b2010-08-18 04:37:33 +1000166 .lock = __SPIN_LOCK_UNLOCKED(init_fs.lock),
John Stultz1ca7d672013-10-07 15:51:59 -0700167 .seq = SEQCNT_ZERO(init_fs.seq),
Al Viro3e93cd62009-03-29 19:00:13 -0400168 .umask = 0022,
169};