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