Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 1 | #include <linux/module.h> |
| 2 | #include <linux/sched.h> |
| 3 | #include <linux/fs.h> |
| 4 | #include <linux/path.h> |
| 5 | #include <linux/slab.h> |
Al Viro | 5ad4e53 | 2009-03-29 19:50:06 -0400 | [diff] [blame] | 6 | #include <linux/fs_struct.h> |
Al Viro | f03c659 | 2011-01-14 22:30:21 -0500 | [diff] [blame] | 7 | #include "internal.h" |
| 8 | |
| 9 | static inline void path_get_longterm(struct path *path) |
| 10 | { |
| 11 | path_get(path); |
| 12 | mnt_make_longterm(path->mnt); |
| 13 | } |
| 14 | |
| 15 | static inline void path_put_longterm(struct path *path) |
| 16 | { |
| 17 | mnt_make_shortterm(path->mnt); |
| 18 | path_put(path); |
| 19 | } |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 20 | |
| 21 | /* |
| 22 | * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values. |
| 23 | * It can block. |
| 24 | */ |
| 25 | void set_fs_root(struct fs_struct *fs, struct path *path) |
| 26 | { |
| 27 | struct path old_root; |
| 28 | |
Nick Piggin | 2a4419b | 2010-08-18 04:37:33 +1000 | [diff] [blame] | 29 | spin_lock(&fs->lock); |
Nick Piggin | c28cc36 | 2011-01-07 17:49:53 +1100 | [diff] [blame] | 30 | write_seqcount_begin(&fs->seq); |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 31 | old_root = fs->root; |
| 32 | fs->root = *path; |
Al Viro | f03c659 | 2011-01-14 22:30:21 -0500 | [diff] [blame] | 33 | path_get_longterm(path); |
Nick Piggin | c28cc36 | 2011-01-07 17:49:53 +1100 | [diff] [blame] | 34 | write_seqcount_end(&fs->seq); |
Nick Piggin | 2a4419b | 2010-08-18 04:37:33 +1000 | [diff] [blame] | 35 | spin_unlock(&fs->lock); |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 36 | if (old_root.dentry) |
Al Viro | f03c659 | 2011-01-14 22:30:21 -0500 | [diff] [blame] | 37 | path_put_longterm(&old_root); |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | /* |
| 41 | * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values. |
| 42 | * It can block. |
| 43 | */ |
| 44 | void set_fs_pwd(struct fs_struct *fs, struct path *path) |
| 45 | { |
| 46 | struct path old_pwd; |
| 47 | |
Nick Piggin | 2a4419b | 2010-08-18 04:37:33 +1000 | [diff] [blame] | 48 | spin_lock(&fs->lock); |
Nick Piggin | c28cc36 | 2011-01-07 17:49:53 +1100 | [diff] [blame] | 49 | write_seqcount_begin(&fs->seq); |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 50 | old_pwd = fs->pwd; |
| 51 | fs->pwd = *path; |
Al Viro | f03c659 | 2011-01-14 22:30:21 -0500 | [diff] [blame] | 52 | path_get_longterm(path); |
Nick Piggin | c28cc36 | 2011-01-07 17:49:53 +1100 | [diff] [blame] | 53 | write_seqcount_end(&fs->seq); |
Nick Piggin | 2a4419b | 2010-08-18 04:37:33 +1000 | [diff] [blame] | 54 | spin_unlock(&fs->lock); |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 55 | |
| 56 | if (old_pwd.dentry) |
Al Viro | f03c659 | 2011-01-14 22:30:21 -0500 | [diff] [blame] | 57 | path_put_longterm(&old_pwd); |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void chroot_fs_refs(struct path *old_root, struct path *new_root) |
| 61 | { |
| 62 | struct task_struct *g, *p; |
| 63 | struct fs_struct *fs; |
| 64 | int count = 0; |
| 65 | |
| 66 | read_lock(&tasklist_lock); |
| 67 | do_each_thread(g, p) { |
| 68 | task_lock(p); |
| 69 | fs = p->fs; |
| 70 | if (fs) { |
Nick Piggin | 2a4419b | 2010-08-18 04:37:33 +1000 | [diff] [blame] | 71 | spin_lock(&fs->lock); |
Nick Piggin | c28cc36 | 2011-01-07 17:49:53 +1100 | [diff] [blame] | 72 | write_seqcount_begin(&fs->seq); |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 73 | if (fs->root.dentry == old_root->dentry |
| 74 | && fs->root.mnt == old_root->mnt) { |
Al Viro | f03c659 | 2011-01-14 22:30:21 -0500 | [diff] [blame] | 75 | path_get_longterm(new_root); |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 76 | fs->root = *new_root; |
| 77 | count++; |
| 78 | } |
| 79 | if (fs->pwd.dentry == old_root->dentry |
| 80 | && fs->pwd.mnt == old_root->mnt) { |
Al Viro | f03c659 | 2011-01-14 22:30:21 -0500 | [diff] [blame] | 81 | path_get_longterm(new_root); |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 82 | fs->pwd = *new_root; |
| 83 | count++; |
| 84 | } |
Nick Piggin | c28cc36 | 2011-01-07 17:49:53 +1100 | [diff] [blame] | 85 | write_seqcount_end(&fs->seq); |
Nick Piggin | 2a4419b | 2010-08-18 04:37:33 +1000 | [diff] [blame] | 86 | spin_unlock(&fs->lock); |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 87 | } |
| 88 | task_unlock(p); |
| 89 | } while_each_thread(g, p); |
| 90 | read_unlock(&tasklist_lock); |
| 91 | while (count--) |
Al Viro | f03c659 | 2011-01-14 22:30:21 -0500 | [diff] [blame] | 92 | path_put_longterm(old_root); |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 93 | } |
| 94 | |
Al Viro | 498052b | 2009-03-30 07:20:30 -0400 | [diff] [blame] | 95 | void free_fs_struct(struct fs_struct *fs) |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 96 | { |
Al Viro | f03c659 | 2011-01-14 22:30:21 -0500 | [diff] [blame] | 97 | path_put_longterm(&fs->root); |
| 98 | path_put_longterm(&fs->pwd); |
Al Viro | 498052b | 2009-03-30 07:20:30 -0400 | [diff] [blame] | 99 | kmem_cache_free(fs_cachep, fs); |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void exit_fs(struct task_struct *tsk) |
| 103 | { |
Al Viro | 498052b | 2009-03-30 07:20:30 -0400 | [diff] [blame] | 104 | struct fs_struct *fs = tsk->fs; |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 105 | |
| 106 | if (fs) { |
Al Viro | 498052b | 2009-03-30 07:20:30 -0400 | [diff] [blame] | 107 | int kill; |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 108 | task_lock(tsk); |
Nick Piggin | 2a4419b | 2010-08-18 04:37:33 +1000 | [diff] [blame] | 109 | spin_lock(&fs->lock); |
Nick Piggin | c28cc36 | 2011-01-07 17:49:53 +1100 | [diff] [blame] | 110 | write_seqcount_begin(&fs->seq); |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 111 | tsk->fs = NULL; |
Al Viro | 498052b | 2009-03-30 07:20:30 -0400 | [diff] [blame] | 112 | kill = !--fs->users; |
Nick Piggin | c28cc36 | 2011-01-07 17:49:53 +1100 | [diff] [blame] | 113 | write_seqcount_end(&fs->seq); |
Nick Piggin | 2a4419b | 2010-08-18 04:37:33 +1000 | [diff] [blame] | 114 | spin_unlock(&fs->lock); |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 115 | task_unlock(tsk); |
Al Viro | 498052b | 2009-03-30 07:20:30 -0400 | [diff] [blame] | 116 | if (kill) |
| 117 | free_fs_struct(fs); |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
| 121 | struct fs_struct *copy_fs_struct(struct fs_struct *old) |
| 122 | { |
| 123 | struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL); |
| 124 | /* We don't need to lock fs - think why ;-) */ |
| 125 | if (fs) { |
Al Viro | 498052b | 2009-03-30 07:20:30 -0400 | [diff] [blame] | 126 | fs->users = 1; |
| 127 | fs->in_exec = 0; |
Nick Piggin | 2a4419b | 2010-08-18 04:37:33 +1000 | [diff] [blame] | 128 | spin_lock_init(&fs->lock); |
Nick Piggin | c28cc36 | 2011-01-07 17:49:53 +1100 | [diff] [blame] | 129 | seqcount_init(&fs->seq); |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 130 | fs->umask = old->umask; |
Nick Piggin | b3e19d9 | 2011-01-07 17:50:11 +1100 | [diff] [blame] | 131 | |
| 132 | spin_lock(&old->lock); |
| 133 | fs->root = old->root; |
Al Viro | f03c659 | 2011-01-14 22:30:21 -0500 | [diff] [blame] | 134 | path_get_longterm(&fs->root); |
Nick Piggin | b3e19d9 | 2011-01-07 17:50:11 +1100 | [diff] [blame] | 135 | fs->pwd = old->pwd; |
Al Viro | f03c659 | 2011-01-14 22:30:21 -0500 | [diff] [blame] | 136 | path_get_longterm(&fs->pwd); |
Nick Piggin | b3e19d9 | 2011-01-07 17:50:11 +1100 | [diff] [blame] | 137 | spin_unlock(&old->lock); |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 138 | } |
| 139 | return fs; |
| 140 | } |
| 141 | |
| 142 | int unshare_fs_struct(void) |
| 143 | { |
Al Viro | 498052b | 2009-03-30 07:20:30 -0400 | [diff] [blame] | 144 | struct fs_struct *fs = current->fs; |
| 145 | struct fs_struct *new_fs = copy_fs_struct(fs); |
| 146 | int kill; |
| 147 | |
| 148 | if (!new_fs) |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 149 | return -ENOMEM; |
Al Viro | 498052b | 2009-03-30 07:20:30 -0400 | [diff] [blame] | 150 | |
| 151 | task_lock(current); |
Nick Piggin | 2a4419b | 2010-08-18 04:37:33 +1000 | [diff] [blame] | 152 | spin_lock(&fs->lock); |
Al Viro | 498052b | 2009-03-30 07:20:30 -0400 | [diff] [blame] | 153 | kill = !--fs->users; |
| 154 | current->fs = new_fs; |
Nick Piggin | 2a4419b | 2010-08-18 04:37:33 +1000 | [diff] [blame] | 155 | spin_unlock(&fs->lock); |
Al Viro | 498052b | 2009-03-30 07:20:30 -0400 | [diff] [blame] | 156 | task_unlock(current); |
| 157 | |
| 158 | if (kill) |
| 159 | free_fs_struct(fs); |
| 160 | |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 161 | return 0; |
| 162 | } |
| 163 | EXPORT_SYMBOL_GPL(unshare_fs_struct); |
| 164 | |
Al Viro | ce3b0f8 | 2009-03-29 19:08:22 -0400 | [diff] [blame] | 165 | int current_umask(void) |
| 166 | { |
| 167 | return current->fs->umask; |
| 168 | } |
| 169 | EXPORT_SYMBOL(current_umask); |
| 170 | |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 171 | /* to be mentioned only in INIT_TASK */ |
| 172 | struct fs_struct init_fs = { |
Al Viro | 498052b | 2009-03-30 07:20:30 -0400 | [diff] [blame] | 173 | .users = 1, |
Nick Piggin | 2a4419b | 2010-08-18 04:37:33 +1000 | [diff] [blame] | 174 | .lock = __SPIN_LOCK_UNLOCKED(init_fs.lock), |
Nick Piggin | c28cc36 | 2011-01-07 17:49:53 +1100 | [diff] [blame] | 175 | .seq = SEQCNT_ZERO, |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 176 | .umask = 0022, |
| 177 | }; |
| 178 | |
| 179 | void daemonize_fs_struct(void) |
| 180 | { |
Al Viro | 498052b | 2009-03-30 07:20:30 -0400 | [diff] [blame] | 181 | struct fs_struct *fs = current->fs; |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 182 | |
Al Viro | 498052b | 2009-03-30 07:20:30 -0400 | [diff] [blame] | 183 | if (fs) { |
| 184 | int kill; |
| 185 | |
| 186 | task_lock(current); |
| 187 | |
Nick Piggin | 2a4419b | 2010-08-18 04:37:33 +1000 | [diff] [blame] | 188 | spin_lock(&init_fs.lock); |
Al Viro | 498052b | 2009-03-30 07:20:30 -0400 | [diff] [blame] | 189 | init_fs.users++; |
Nick Piggin | 2a4419b | 2010-08-18 04:37:33 +1000 | [diff] [blame] | 190 | spin_unlock(&init_fs.lock); |
Al Viro | 498052b | 2009-03-30 07:20:30 -0400 | [diff] [blame] | 191 | |
Nick Piggin | 2a4419b | 2010-08-18 04:37:33 +1000 | [diff] [blame] | 192 | spin_lock(&fs->lock); |
Al Viro | 498052b | 2009-03-30 07:20:30 -0400 | [diff] [blame] | 193 | current->fs = &init_fs; |
| 194 | kill = !--fs->users; |
Nick Piggin | 2a4419b | 2010-08-18 04:37:33 +1000 | [diff] [blame] | 195 | spin_unlock(&fs->lock); |
Al Viro | 498052b | 2009-03-30 07:20:30 -0400 | [diff] [blame] | 196 | |
| 197 | task_unlock(current); |
| 198 | if (kill) |
| 199 | free_fs_struct(fs); |
| 200 | } |
Al Viro | 3e93cd6 | 2009-03-29 19:00:13 -0400 | [diff] [blame] | 201 | } |