Merge branch 'cleanup' into for-linus
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / fs_struct.c
CommitLineData
630d9c47 1#include <linux/export.h>
3e93cd67
AV
2#include <linux/sched.h>
3#include <linux/fs.h>
4#include <linux/path.h>
5#include <linux/slab.h>
5ad4e53b 6#include <linux/fs_struct.h>
f03c6599
AV
7#include "internal.h"
8
3e93cd67
AV
9/*
10 * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
11 * It can block.
12 */
dcf787f3 13void set_fs_root(struct fs_struct *fs, const struct path *path)
3e93cd67
AV
14{
15 struct path old_root;
16
f7a99c5b 17 path_get(path);
2a4419b5 18 spin_lock(&fs->lock);
c28cc364 19 write_seqcount_begin(&fs->seq);
3e93cd67
AV
20 old_root = fs->root;
21 fs->root = *path;
c28cc364 22 write_seqcount_end(&fs->seq);
2a4419b5 23 spin_unlock(&fs->lock);
3e93cd67 24 if (old_root.dentry)
f7a99c5b 25 path_put(&old_root);
3e93cd67
AV
26}
27
28/*
29 * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
30 * It can block.
31 */
dcf787f3 32void set_fs_pwd(struct fs_struct *fs, const struct path *path)
3e93cd67
AV
33{
34 struct path old_pwd;
35
f7a99c5b 36 path_get(path);
2a4419b5 37 spin_lock(&fs->lock);
c28cc364 38 write_seqcount_begin(&fs->seq);
3e93cd67
AV
39 old_pwd = fs->pwd;
40 fs->pwd = *path;
c28cc364 41 write_seqcount_end(&fs->seq);
2a4419b5 42 spin_unlock(&fs->lock);
3e93cd67
AV
43
44 if (old_pwd.dentry)
f7a99c5b 45 path_put(&old_pwd);
3e93cd67
AV
46}
47
82234e61
AV
48static 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
dcf787f3 56void chroot_fs_refs(const struct path *old_root, const struct path *new_root)
3e93cd67
AV
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) {
82234e61 67 int hits = 0;
2a4419b5 68 spin_lock(&fs->lock);
c28cc364 69 write_seqcount_begin(&fs->seq);
82234e61
AV
70 hits += replace_path(&fs->root, old_root, new_root);
71 hits += replace_path(&fs->pwd, old_root, new_root);
72 write_seqcount_end(&fs->seq);
73 while (hits--) {
3e93cd67 74 count++;
f7a99c5b 75 path_get(new_root);
3e93cd67 76 }
2a4419b5 77 spin_unlock(&fs->lock);
3e93cd67
AV
78 }
79 task_unlock(p);
80 } while_each_thread(g, p);
81 read_unlock(&tasklist_lock);
82 while (count--)
f7a99c5b 83 path_put(old_root);
3e93cd67
AV
84}
85
498052bb 86void free_fs_struct(struct fs_struct *fs)
3e93cd67 87{
f7a99c5b
AV
88 path_put(&fs->root);
89 path_put(&fs->pwd);
498052bb 90 kmem_cache_free(fs_cachep, fs);
3e93cd67
AV
91}
92
93void exit_fs(struct task_struct *tsk)
94{
498052bb 95 struct fs_struct *fs = tsk->fs;
3e93cd67
AV
96
97 if (fs) {
498052bb 98 int kill;
3e93cd67 99 task_lock(tsk);
2a4419b5 100 spin_lock(&fs->lock);
3e93cd67 101 tsk->fs = NULL;
498052bb 102 kill = !--fs->users;
2a4419b5 103 spin_unlock(&fs->lock);
3e93cd67 104 task_unlock(tsk);
498052bb
AV
105 if (kill)
106 free_fs_struct(fs);
3e93cd67
AV
107 }
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) {
498052bb
AV
115 fs->users = 1;
116 fs->in_exec = 0;
2a4419b5 117 spin_lock_init(&fs->lock);
c28cc364 118 seqcount_init(&fs->seq);
3e93cd67 119 fs->umask = old->umask;
b3e19d92
NP
120
121 spin_lock(&old->lock);
122 fs->root = old->root;
f7a99c5b 123 path_get(&fs->root);
b3e19d92 124 fs->pwd = old->pwd;
f7a99c5b 125 path_get(&fs->pwd);
b3e19d92 126 spin_unlock(&old->lock);
3e93cd67
AV
127 }
128 return fs;
129}
130
131int unshare_fs_struct(void)
132{
498052bb
AV
133 struct fs_struct *fs = current->fs;
134 struct fs_struct *new_fs = copy_fs_struct(fs);
135 int kill;
136
137 if (!new_fs)
3e93cd67 138 return -ENOMEM;
498052bb
AV
139
140 task_lock(current);
2a4419b5 141 spin_lock(&fs->lock);
498052bb
AV
142 kill = !--fs->users;
143 current->fs = new_fs;
2a4419b5 144 spin_unlock(&fs->lock);
498052bb
AV
145 task_unlock(current);
146
147 if (kill)
148 free_fs_struct(fs);
149
3e93cd67
AV
150 return 0;
151}
152EXPORT_SYMBOL_GPL(unshare_fs_struct);
153
ce3b0f8d
AV
154int current_umask(void)
155{
156 return current->fs->umask;
157}
158EXPORT_SYMBOL(current_umask);
159
3e93cd67
AV
160/* to be mentioned only in INIT_TASK */
161struct fs_struct init_fs = {
498052bb 162 .users = 1,
2a4419b5 163 .lock = __SPIN_LOCK_UNLOCKED(init_fs.lock),
c28cc364 164 .seq = SEQCNT_ZERO,
3e93cd67
AV
165 .umask = 0022,
166};