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