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