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