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