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