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