Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/namespace.c |
| 3 | * |
| 4 | * (C) Copyright Al Viro 2000, 2001 |
| 5 | * Released under GPL v2. |
| 6 | * |
| 7 | * Based on code from fs/super.c, copyright Linus Torvalds and others. |
| 8 | * Heavily rewritten. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/config.h> |
| 12 | #include <linux/syscalls.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/sched.h> |
| 15 | #include <linux/smp_lock.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/quotaops.h> |
| 18 | #include <linux/acct.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/seq_file.h> |
| 21 | #include <linux/namespace.h> |
| 22 | #include <linux/namei.h> |
| 23 | #include <linux/security.h> |
| 24 | #include <linux/mount.h> |
| 25 | #include <asm/uaccess.h> |
| 26 | #include <asm/unistd.h> |
| 27 | |
| 28 | extern int __init init_rootfs(void); |
| 29 | |
| 30 | #ifdef CONFIG_SYSFS |
| 31 | extern int __init sysfs_init(void); |
| 32 | #else |
| 33 | static inline int sysfs_init(void) |
| 34 | { |
| 35 | return 0; |
| 36 | } |
| 37 | #endif |
| 38 | |
| 39 | /* spinlock for vfsmount related operations, inplace of dcache_lock */ |
Al Viro | 5addc5d | 2005-11-07 17:15:49 -0500 | [diff] [blame] | 40 | __cacheline_aligned_in_smp DEFINE_SPINLOCK(vfsmount_lock); |
| 41 | |
| 42 | static int event; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | static struct list_head *mount_hashtable; |
Ravikiran G Thirumalai | 6c231b7 | 2005-09-06 15:17:45 -0700 | [diff] [blame] | 45 | static int hash_mask __read_mostly, hash_bits __read_mostly; |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 46 | static kmem_cache_t *mnt_cache; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
| 48 | static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry) |
| 49 | { |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 50 | unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES); |
| 51 | tmp += ((unsigned long)dentry / L1_CACHE_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | tmp = tmp + (tmp >> hash_bits); |
| 53 | return tmp & hash_mask; |
| 54 | } |
| 55 | |
| 56 | struct vfsmount *alloc_vfsmnt(const char *name) |
| 57 | { |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 58 | struct vfsmount *mnt = kmem_cache_alloc(mnt_cache, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | if (mnt) { |
| 60 | memset(mnt, 0, sizeof(struct vfsmount)); |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 61 | atomic_set(&mnt->mnt_count, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | INIT_LIST_HEAD(&mnt->mnt_hash); |
| 63 | INIT_LIST_HEAD(&mnt->mnt_child); |
| 64 | INIT_LIST_HEAD(&mnt->mnt_mounts); |
| 65 | INIT_LIST_HEAD(&mnt->mnt_list); |
Miklos Szeredi | 55e700b | 2005-07-07 17:57:30 -0700 | [diff] [blame] | 66 | INIT_LIST_HEAD(&mnt->mnt_expire); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | if (name) { |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 68 | int size = strlen(name) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | char *newname = kmalloc(size, GFP_KERNEL); |
| 70 | if (newname) { |
| 71 | memcpy(newname, name, size); |
| 72 | mnt->mnt_devname = newname; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | return mnt; |
| 77 | } |
| 78 | |
| 79 | void free_vfsmnt(struct vfsmount *mnt) |
| 80 | { |
| 81 | kfree(mnt->mnt_devname); |
| 82 | kmem_cache_free(mnt_cache, mnt); |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * Now, lookup_mnt increments the ref count before returning |
| 87 | * the vfsmount struct. |
| 88 | */ |
| 89 | struct vfsmount *lookup_mnt(struct vfsmount *mnt, struct dentry *dentry) |
| 90 | { |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 91 | struct list_head *head = mount_hashtable + hash(mnt, dentry); |
| 92 | struct list_head *tmp = head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | struct vfsmount *p, *found = NULL; |
| 94 | |
| 95 | spin_lock(&vfsmount_lock); |
| 96 | for (;;) { |
| 97 | tmp = tmp->next; |
| 98 | p = NULL; |
| 99 | if (tmp == head) |
| 100 | break; |
| 101 | p = list_entry(tmp, struct vfsmount, mnt_hash); |
| 102 | if (p->mnt_parent == mnt && p->mnt_mountpoint == dentry) { |
| 103 | found = mntget(p); |
| 104 | break; |
| 105 | } |
| 106 | } |
| 107 | spin_unlock(&vfsmount_lock); |
| 108 | return found; |
| 109 | } |
| 110 | |
| 111 | static inline int check_mnt(struct vfsmount *mnt) |
| 112 | { |
| 113 | return mnt->mnt_namespace == current->namespace; |
| 114 | } |
| 115 | |
Al Viro | 5addc5d | 2005-11-07 17:15:49 -0500 | [diff] [blame] | 116 | static void touch_namespace(struct namespace *ns) |
| 117 | { |
| 118 | if (ns) { |
| 119 | ns->event = ++event; |
| 120 | wake_up_interruptible(&ns->poll); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | static void __touch_namespace(struct namespace *ns) |
| 125 | { |
| 126 | if (ns && ns->event != event) { |
| 127 | ns->event = event; |
| 128 | wake_up_interruptible(&ns->poll); |
| 129 | } |
| 130 | } |
| 131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | static void detach_mnt(struct vfsmount *mnt, struct nameidata *old_nd) |
| 133 | { |
| 134 | old_nd->dentry = mnt->mnt_mountpoint; |
| 135 | old_nd->mnt = mnt->mnt_parent; |
| 136 | mnt->mnt_parent = mnt; |
| 137 | mnt->mnt_mountpoint = mnt->mnt_root; |
| 138 | list_del_init(&mnt->mnt_child); |
| 139 | list_del_init(&mnt->mnt_hash); |
| 140 | old_nd->dentry->d_mounted--; |
| 141 | } |
| 142 | |
| 143 | static void attach_mnt(struct vfsmount *mnt, struct nameidata *nd) |
| 144 | { |
| 145 | mnt->mnt_parent = mntget(nd->mnt); |
| 146 | mnt->mnt_mountpoint = dget(nd->dentry); |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 147 | list_add(&mnt->mnt_hash, mount_hashtable + hash(nd->mnt, nd->dentry)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | list_add_tail(&mnt->mnt_child, &nd->mnt->mnt_mounts); |
| 149 | nd->dentry->d_mounted++; |
| 150 | } |
| 151 | |
| 152 | static struct vfsmount *next_mnt(struct vfsmount *p, struct vfsmount *root) |
| 153 | { |
| 154 | struct list_head *next = p->mnt_mounts.next; |
| 155 | if (next == &p->mnt_mounts) { |
| 156 | while (1) { |
| 157 | if (p == root) |
| 158 | return NULL; |
| 159 | next = p->mnt_child.next; |
| 160 | if (next != &p->mnt_parent->mnt_mounts) |
| 161 | break; |
| 162 | p = p->mnt_parent; |
| 163 | } |
| 164 | } |
| 165 | return list_entry(next, struct vfsmount, mnt_child); |
| 166 | } |
| 167 | |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 168 | static struct vfsmount *clone_mnt(struct vfsmount *old, struct dentry *root) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | { |
| 170 | struct super_block *sb = old->mnt_sb; |
| 171 | struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname); |
| 172 | |
| 173 | if (mnt) { |
| 174 | mnt->mnt_flags = old->mnt_flags; |
| 175 | atomic_inc(&sb->s_active); |
| 176 | mnt->mnt_sb = sb; |
| 177 | mnt->mnt_root = dget(root); |
| 178 | mnt->mnt_mountpoint = mnt->mnt_root; |
| 179 | mnt->mnt_parent = mnt; |
Miklos Szeredi | 68b4713 | 2005-08-07 09:42:25 -0700 | [diff] [blame] | 180 | mnt->mnt_namespace = current->namespace; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
| 182 | /* stick the duplicate mount on the same expiry list |
| 183 | * as the original if that was on one */ |
| 184 | spin_lock(&vfsmount_lock); |
Miklos Szeredi | 55e700b | 2005-07-07 17:57:30 -0700 | [diff] [blame] | 185 | if (!list_empty(&old->mnt_expire)) |
| 186 | list_add(&mnt->mnt_expire, &old->mnt_expire); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | spin_unlock(&vfsmount_lock); |
| 188 | } |
| 189 | return mnt; |
| 190 | } |
| 191 | |
Al Viro | 7b7b1ac | 2005-11-07 17:13:39 -0500 | [diff] [blame] | 192 | static inline void __mntput(struct vfsmount *mnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | { |
| 194 | struct super_block *sb = mnt->mnt_sb; |
| 195 | dput(mnt->mnt_root); |
| 196 | free_vfsmnt(mnt); |
| 197 | deactivate_super(sb); |
| 198 | } |
| 199 | |
Al Viro | 7b7b1ac | 2005-11-07 17:13:39 -0500 | [diff] [blame] | 200 | void mntput_no_expire(struct vfsmount *mnt) |
| 201 | { |
| 202 | repeat: |
| 203 | if (atomic_dec_and_lock(&mnt->mnt_count, &vfsmount_lock)) { |
| 204 | if (likely(!mnt->mnt_pinned)) { |
| 205 | spin_unlock(&vfsmount_lock); |
| 206 | __mntput(mnt); |
| 207 | return; |
| 208 | } |
| 209 | atomic_add(mnt->mnt_pinned + 1, &mnt->mnt_count); |
| 210 | mnt->mnt_pinned = 0; |
| 211 | spin_unlock(&vfsmount_lock); |
| 212 | acct_auto_close_mnt(mnt); |
| 213 | security_sb_umount_close(mnt); |
| 214 | goto repeat; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | EXPORT_SYMBOL(mntput_no_expire); |
| 219 | |
| 220 | void mnt_pin(struct vfsmount *mnt) |
| 221 | { |
| 222 | spin_lock(&vfsmount_lock); |
| 223 | mnt->mnt_pinned++; |
| 224 | spin_unlock(&vfsmount_lock); |
| 225 | } |
| 226 | |
| 227 | EXPORT_SYMBOL(mnt_pin); |
| 228 | |
| 229 | void mnt_unpin(struct vfsmount *mnt) |
| 230 | { |
| 231 | spin_lock(&vfsmount_lock); |
| 232 | if (mnt->mnt_pinned) { |
| 233 | atomic_inc(&mnt->mnt_count); |
| 234 | mnt->mnt_pinned--; |
| 235 | } |
| 236 | spin_unlock(&vfsmount_lock); |
| 237 | } |
| 238 | |
| 239 | EXPORT_SYMBOL(mnt_unpin); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
| 241 | /* iterator */ |
| 242 | static void *m_start(struct seq_file *m, loff_t *pos) |
| 243 | { |
| 244 | struct namespace *n = m->private; |
| 245 | struct list_head *p; |
| 246 | loff_t l = *pos; |
| 247 | |
| 248 | down_read(&n->sem); |
| 249 | list_for_each(p, &n->list) |
| 250 | if (!l--) |
| 251 | return list_entry(p, struct vfsmount, mnt_list); |
| 252 | return NULL; |
| 253 | } |
| 254 | |
| 255 | static void *m_next(struct seq_file *m, void *v, loff_t *pos) |
| 256 | { |
| 257 | struct namespace *n = m->private; |
| 258 | struct list_head *p = ((struct vfsmount *)v)->mnt_list.next; |
| 259 | (*pos)++; |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 260 | return p == &n->list ? NULL : list_entry(p, struct vfsmount, mnt_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | static void m_stop(struct seq_file *m, void *v) |
| 264 | { |
| 265 | struct namespace *n = m->private; |
| 266 | up_read(&n->sem); |
| 267 | } |
| 268 | |
| 269 | static inline void mangle(struct seq_file *m, const char *s) |
| 270 | { |
| 271 | seq_escape(m, s, " \t\n\\"); |
| 272 | } |
| 273 | |
| 274 | static int show_vfsmnt(struct seq_file *m, void *v) |
| 275 | { |
| 276 | struct vfsmount *mnt = v; |
| 277 | int err = 0; |
| 278 | static struct proc_fs_info { |
| 279 | int flag; |
| 280 | char *str; |
| 281 | } fs_info[] = { |
| 282 | { MS_SYNCHRONOUS, ",sync" }, |
| 283 | { MS_DIRSYNC, ",dirsync" }, |
| 284 | { MS_MANDLOCK, ",mand" }, |
| 285 | { MS_NOATIME, ",noatime" }, |
| 286 | { MS_NODIRATIME, ",nodiratime" }, |
| 287 | { 0, NULL } |
| 288 | }; |
| 289 | static struct proc_fs_info mnt_info[] = { |
| 290 | { MNT_NOSUID, ",nosuid" }, |
| 291 | { MNT_NODEV, ",nodev" }, |
| 292 | { MNT_NOEXEC, ",noexec" }, |
| 293 | { 0, NULL } |
| 294 | }; |
| 295 | struct proc_fs_info *fs_infop; |
| 296 | |
| 297 | mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none"); |
| 298 | seq_putc(m, ' '); |
| 299 | seq_path(m, mnt, mnt->mnt_root, " \t\n\\"); |
| 300 | seq_putc(m, ' '); |
| 301 | mangle(m, mnt->mnt_sb->s_type->name); |
| 302 | seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? " ro" : " rw"); |
| 303 | for (fs_infop = fs_info; fs_infop->flag; fs_infop++) { |
| 304 | if (mnt->mnt_sb->s_flags & fs_infop->flag) |
| 305 | seq_puts(m, fs_infop->str); |
| 306 | } |
| 307 | for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) { |
| 308 | if (mnt->mnt_flags & fs_infop->flag) |
| 309 | seq_puts(m, fs_infop->str); |
| 310 | } |
| 311 | if (mnt->mnt_sb->s_op->show_options) |
| 312 | err = mnt->mnt_sb->s_op->show_options(m, mnt); |
| 313 | seq_puts(m, " 0 0\n"); |
| 314 | return err; |
| 315 | } |
| 316 | |
| 317 | struct seq_operations mounts_op = { |
| 318 | .start = m_start, |
| 319 | .next = m_next, |
| 320 | .stop = m_stop, |
| 321 | .show = show_vfsmnt |
| 322 | }; |
| 323 | |
| 324 | /** |
| 325 | * may_umount_tree - check if a mount tree is busy |
| 326 | * @mnt: root of mount tree |
| 327 | * |
| 328 | * This is called to check if a tree of mounts has any |
| 329 | * open files, pwds, chroots or sub mounts that are |
| 330 | * busy. |
| 331 | */ |
| 332 | int may_umount_tree(struct vfsmount *mnt) |
| 333 | { |
| 334 | struct list_head *next; |
| 335 | struct vfsmount *this_parent = mnt; |
| 336 | int actual_refs; |
| 337 | int minimum_refs; |
| 338 | |
| 339 | spin_lock(&vfsmount_lock); |
| 340 | actual_refs = atomic_read(&mnt->mnt_count); |
| 341 | minimum_refs = 2; |
| 342 | repeat: |
| 343 | next = this_parent->mnt_mounts.next; |
| 344 | resume: |
| 345 | while (next != &this_parent->mnt_mounts) { |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 346 | struct vfsmount *p = |
| 347 | list_entry(next, struct vfsmount, mnt_child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | |
| 349 | next = next->next; |
| 350 | |
| 351 | actual_refs += atomic_read(&p->mnt_count); |
| 352 | minimum_refs += 2; |
| 353 | |
| 354 | if (!list_empty(&p->mnt_mounts)) { |
| 355 | this_parent = p; |
| 356 | goto repeat; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | if (this_parent != mnt) { |
| 361 | next = this_parent->mnt_child.next; |
| 362 | this_parent = this_parent->mnt_parent; |
| 363 | goto resume; |
| 364 | } |
| 365 | spin_unlock(&vfsmount_lock); |
| 366 | |
| 367 | if (actual_refs > minimum_refs) |
| 368 | return -EBUSY; |
| 369 | |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | EXPORT_SYMBOL(may_umount_tree); |
| 374 | |
| 375 | /** |
| 376 | * may_umount - check if a mount point is busy |
| 377 | * @mnt: root of mount |
| 378 | * |
| 379 | * This is called to check if a mount point has any |
| 380 | * open files, pwds, chroots or sub mounts. If the |
| 381 | * mount has sub mounts this will return busy |
| 382 | * regardless of whether the sub mounts are busy. |
| 383 | * |
| 384 | * Doesn't take quota and stuff into account. IOW, in some cases it will |
| 385 | * give false negatives. The main reason why it's here is that we need |
| 386 | * a non-destructive way to look for easily umountable filesystems. |
| 387 | */ |
| 388 | int may_umount(struct vfsmount *mnt) |
| 389 | { |
| 390 | if (atomic_read(&mnt->mnt_count) > 2) |
| 391 | return -EBUSY; |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | EXPORT_SYMBOL(may_umount); |
| 396 | |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame^] | 397 | static void release_mounts(struct list_head *head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | { |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame^] | 399 | struct vfsmount *mnt; |
| 400 | while(!list_empty(head)) { |
| 401 | mnt = list_entry(head->next, struct vfsmount, mnt_hash); |
| 402 | list_del_init(&mnt->mnt_hash); |
| 403 | if (mnt->mnt_parent != mnt) { |
| 404 | struct dentry *dentry; |
| 405 | struct vfsmount *m; |
| 406 | spin_lock(&vfsmount_lock); |
| 407 | dentry = mnt->mnt_mountpoint; |
| 408 | m = mnt->mnt_parent; |
| 409 | mnt->mnt_mountpoint = mnt->mnt_root; |
| 410 | mnt->mnt_parent = mnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | spin_unlock(&vfsmount_lock); |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame^] | 412 | dput(dentry); |
| 413 | mntput(m); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | } |
| 415 | mntput(mnt); |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame^] | 416 | } |
| 417 | } |
| 418 | |
| 419 | static void umount_tree(struct vfsmount *mnt, struct list_head *kill) |
| 420 | { |
| 421 | struct vfsmount *p; |
| 422 | |
| 423 | for (p = mnt; p; p = next_mnt(p, mnt)) { |
| 424 | list_del(&p->mnt_hash); |
| 425 | list_add(&p->mnt_hash, kill); |
| 426 | } |
| 427 | |
| 428 | list_for_each_entry(p, kill, mnt_hash) { |
| 429 | list_del_init(&p->mnt_expire); |
| 430 | list_del_init(&p->mnt_list); |
| 431 | __touch_namespace(p->mnt_namespace); |
| 432 | p->mnt_namespace = NULL; |
| 433 | list_del_init(&p->mnt_child); |
| 434 | if (p->mnt_parent != p) |
| 435 | mnt->mnt_mountpoint->d_mounted--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | |
| 439 | static int do_umount(struct vfsmount *mnt, int flags) |
| 440 | { |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 441 | struct super_block *sb = mnt->mnt_sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | int retval; |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame^] | 443 | LIST_HEAD(umount_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | |
| 445 | retval = security_sb_umount(mnt, flags); |
| 446 | if (retval) |
| 447 | return retval; |
| 448 | |
| 449 | /* |
| 450 | * Allow userspace to request a mountpoint be expired rather than |
| 451 | * unmounting unconditionally. Unmount only happens if: |
| 452 | * (1) the mark is already set (the mark is cleared by mntput()) |
| 453 | * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount] |
| 454 | */ |
| 455 | if (flags & MNT_EXPIRE) { |
| 456 | if (mnt == current->fs->rootmnt || |
| 457 | flags & (MNT_FORCE | MNT_DETACH)) |
| 458 | return -EINVAL; |
| 459 | |
| 460 | if (atomic_read(&mnt->mnt_count) != 2) |
| 461 | return -EBUSY; |
| 462 | |
| 463 | if (!xchg(&mnt->mnt_expiry_mark, 1)) |
| 464 | return -EAGAIN; |
| 465 | } |
| 466 | |
| 467 | /* |
| 468 | * If we may have to abort operations to get out of this |
| 469 | * mount, and they will themselves hold resources we must |
| 470 | * allow the fs to do things. In the Unix tradition of |
| 471 | * 'Gee thats tricky lets do it in userspace' the umount_begin |
| 472 | * might fail to complete on the first run through as other tasks |
| 473 | * must return, and the like. Thats for the mount program to worry |
| 474 | * about for the moment. |
| 475 | */ |
| 476 | |
| 477 | lock_kernel(); |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 478 | if ((flags & MNT_FORCE) && sb->s_op->umount_begin) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | sb->s_op->umount_begin(sb); |
| 480 | unlock_kernel(); |
| 481 | |
| 482 | /* |
| 483 | * No sense to grab the lock for this test, but test itself looks |
| 484 | * somewhat bogus. Suggestions for better replacement? |
| 485 | * Ho-hum... In principle, we might treat that as umount + switch |
| 486 | * to rootfs. GC would eventually take care of the old vfsmount. |
| 487 | * Actually it makes sense, especially if rootfs would contain a |
| 488 | * /reboot - static binary that would close all descriptors and |
| 489 | * call reboot(9). Then init(8) could umount root and exec /reboot. |
| 490 | */ |
| 491 | if (mnt == current->fs->rootmnt && !(flags & MNT_DETACH)) { |
| 492 | /* |
| 493 | * Special case for "unmounting" root ... |
| 494 | * we just try to remount it readonly. |
| 495 | */ |
| 496 | down_write(&sb->s_umount); |
| 497 | if (!(sb->s_flags & MS_RDONLY)) { |
| 498 | lock_kernel(); |
| 499 | DQUOT_OFF(sb); |
| 500 | retval = do_remount_sb(sb, MS_RDONLY, NULL, 0); |
| 501 | unlock_kernel(); |
| 502 | } |
| 503 | up_write(&sb->s_umount); |
| 504 | return retval; |
| 505 | } |
| 506 | |
| 507 | down_write(¤t->namespace->sem); |
| 508 | spin_lock(&vfsmount_lock); |
Al Viro | 5addc5d | 2005-11-07 17:15:49 -0500 | [diff] [blame] | 509 | event++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | retval = -EBUSY; |
| 512 | if (atomic_read(&mnt->mnt_count) == 2 || flags & MNT_DETACH) { |
| 513 | if (!list_empty(&mnt->mnt_list)) |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame^] | 514 | umount_tree(mnt, &umount_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | retval = 0; |
| 516 | } |
| 517 | spin_unlock(&vfsmount_lock); |
| 518 | if (retval) |
| 519 | security_sb_umount_busy(mnt); |
| 520 | up_write(¤t->namespace->sem); |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame^] | 521 | release_mounts(&umount_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | return retval; |
| 523 | } |
| 524 | |
| 525 | /* |
| 526 | * Now umount can handle mount points as well as block devices. |
| 527 | * This is important for filesystems which use unnamed block devices. |
| 528 | * |
| 529 | * We now support a flag for forced unmount like the other 'big iron' |
| 530 | * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD |
| 531 | */ |
| 532 | |
| 533 | asmlinkage long sys_umount(char __user * name, int flags) |
| 534 | { |
| 535 | struct nameidata nd; |
| 536 | int retval; |
| 537 | |
| 538 | retval = __user_walk(name, LOOKUP_FOLLOW, &nd); |
| 539 | if (retval) |
| 540 | goto out; |
| 541 | retval = -EINVAL; |
| 542 | if (nd.dentry != nd.mnt->mnt_root) |
| 543 | goto dput_and_out; |
| 544 | if (!check_mnt(nd.mnt)) |
| 545 | goto dput_and_out; |
| 546 | |
| 547 | retval = -EPERM; |
| 548 | if (!capable(CAP_SYS_ADMIN)) |
| 549 | goto dput_and_out; |
| 550 | |
| 551 | retval = do_umount(nd.mnt, flags); |
| 552 | dput_and_out: |
| 553 | path_release_on_umount(&nd); |
| 554 | out: |
| 555 | return retval; |
| 556 | } |
| 557 | |
| 558 | #ifdef __ARCH_WANT_SYS_OLDUMOUNT |
| 559 | |
| 560 | /* |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 561 | * The 2.0 compatible umount. No flags. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | asmlinkage long sys_oldumount(char __user * name) |
| 564 | { |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 565 | return sys_umount(name, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | #endif |
| 569 | |
| 570 | static int mount_is_safe(struct nameidata *nd) |
| 571 | { |
| 572 | if (capable(CAP_SYS_ADMIN)) |
| 573 | return 0; |
| 574 | return -EPERM; |
| 575 | #ifdef notyet |
| 576 | if (S_ISLNK(nd->dentry->d_inode->i_mode)) |
| 577 | return -EPERM; |
| 578 | if (nd->dentry->d_inode->i_mode & S_ISVTX) { |
| 579 | if (current->uid != nd->dentry->d_inode->i_uid) |
| 580 | return -EPERM; |
| 581 | } |
| 582 | if (permission(nd->dentry->d_inode, MAY_WRITE, nd)) |
| 583 | return -EPERM; |
| 584 | return 0; |
| 585 | #endif |
| 586 | } |
| 587 | |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 588 | static int lives_below_in_same_fs(struct dentry *d, struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | { |
| 590 | while (1) { |
| 591 | if (d == dentry) |
| 592 | return 1; |
| 593 | if (d == NULL || d == d->d_parent) |
| 594 | return 0; |
| 595 | d = d->d_parent; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | static struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry) |
| 600 | { |
| 601 | struct vfsmount *res, *p, *q, *r, *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | struct nameidata nd; |
| 603 | |
| 604 | res = q = clone_mnt(mnt, dentry); |
| 605 | if (!q) |
| 606 | goto Enomem; |
| 607 | q->mnt_mountpoint = mnt->mnt_mountpoint; |
| 608 | |
| 609 | p = mnt; |
Domen Puncer | fdadd65 | 2005-09-10 00:27:07 -0700 | [diff] [blame] | 610 | list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | if (!lives_below_in_same_fs(r->mnt_mountpoint, dentry)) |
| 612 | continue; |
| 613 | |
| 614 | for (s = r; s; s = next_mnt(s, r)) { |
| 615 | while (p != s->mnt_parent) { |
| 616 | p = p->mnt_parent; |
| 617 | q = q->mnt_parent; |
| 618 | } |
| 619 | p = s; |
| 620 | nd.mnt = q; |
| 621 | nd.dentry = p->mnt_mountpoint; |
| 622 | q = clone_mnt(p, p->mnt_root); |
| 623 | if (!q) |
| 624 | goto Enomem; |
| 625 | spin_lock(&vfsmount_lock); |
| 626 | list_add_tail(&q->mnt_list, &res->mnt_list); |
| 627 | attach_mnt(q, &nd); |
| 628 | spin_unlock(&vfsmount_lock); |
| 629 | } |
| 630 | } |
| 631 | return res; |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 632 | Enomem: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | if (res) { |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame^] | 634 | LIST_HEAD(umount_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | spin_lock(&vfsmount_lock); |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame^] | 636 | umount_tree(res, &umount_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | spin_unlock(&vfsmount_lock); |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame^] | 638 | release_mounts(&umount_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | } |
| 640 | return NULL; |
| 641 | } |
| 642 | |
| 643 | static int graft_tree(struct vfsmount *mnt, struct nameidata *nd) |
| 644 | { |
| 645 | int err; |
| 646 | if (mnt->mnt_sb->s_flags & MS_NOUSER) |
| 647 | return -EINVAL; |
| 648 | |
| 649 | if (S_ISDIR(nd->dentry->d_inode->i_mode) != |
| 650 | S_ISDIR(mnt->mnt_root->d_inode->i_mode)) |
| 651 | return -ENOTDIR; |
| 652 | |
| 653 | err = -ENOENT; |
| 654 | down(&nd->dentry->d_inode->i_sem); |
| 655 | if (IS_DEADDIR(nd->dentry->d_inode)) |
| 656 | goto out_unlock; |
| 657 | |
| 658 | err = security_sb_check_sb(mnt, nd); |
| 659 | if (err) |
| 660 | goto out_unlock; |
| 661 | |
| 662 | err = -ENOENT; |
| 663 | spin_lock(&vfsmount_lock); |
| 664 | if (IS_ROOT(nd->dentry) || !d_unhashed(nd->dentry)) { |
| 665 | struct list_head head; |
| 666 | |
| 667 | attach_mnt(mnt, nd); |
| 668 | list_add_tail(&head, &mnt->mnt_list); |
| 669 | list_splice(&head, current->namespace->list.prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | err = 0; |
Al Viro | 5addc5d | 2005-11-07 17:15:49 -0500 | [diff] [blame] | 671 | touch_namespace(current->namespace); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | } |
| 673 | spin_unlock(&vfsmount_lock); |
| 674 | out_unlock: |
| 675 | up(&nd->dentry->d_inode->i_sem); |
| 676 | if (!err) |
| 677 | security_sb_post_addmount(mnt, nd); |
| 678 | return err; |
| 679 | } |
| 680 | |
| 681 | /* |
| 682 | * do loopback mount. |
| 683 | */ |
| 684 | static int do_loopback(struct nameidata *nd, char *old_name, int recurse) |
| 685 | { |
| 686 | struct nameidata old_nd; |
| 687 | struct vfsmount *mnt = NULL; |
| 688 | int err = mount_is_safe(nd); |
| 689 | if (err) |
| 690 | return err; |
| 691 | if (!old_name || !*old_name) |
| 692 | return -EINVAL; |
| 693 | err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd); |
| 694 | if (err) |
| 695 | return err; |
| 696 | |
| 697 | down_write(¤t->namespace->sem); |
| 698 | err = -EINVAL; |
Al Viro | ccd48bc | 2005-11-07 17:15:04 -0500 | [diff] [blame] | 699 | if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt)) |
| 700 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | |
Al Viro | ccd48bc | 2005-11-07 17:15:04 -0500 | [diff] [blame] | 702 | err = -ENOMEM; |
| 703 | if (recurse) |
| 704 | mnt = copy_tree(old_nd.mnt, old_nd.dentry); |
| 705 | else |
| 706 | mnt = clone_mnt(old_nd.mnt, old_nd.dentry); |
| 707 | |
| 708 | if (!mnt) |
| 709 | goto out; |
| 710 | |
| 711 | /* stop bind mounts from expiring */ |
| 712 | spin_lock(&vfsmount_lock); |
| 713 | list_del_init(&mnt->mnt_expire); |
| 714 | spin_unlock(&vfsmount_lock); |
| 715 | |
| 716 | err = graft_tree(mnt, nd); |
| 717 | if (err) { |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame^] | 718 | LIST_HEAD(umount_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | spin_lock(&vfsmount_lock); |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame^] | 720 | umount_tree(mnt, &umount_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | spin_unlock(&vfsmount_lock); |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame^] | 722 | release_mounts(&umount_list); |
Ram Pai | 5b83d2c | 2005-11-07 17:16:29 -0500 | [diff] [blame] | 723 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | |
Al Viro | ccd48bc | 2005-11-07 17:15:04 -0500 | [diff] [blame] | 725 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | up_write(¤t->namespace->sem); |
| 727 | path_release(&old_nd); |
| 728 | return err; |
| 729 | } |
| 730 | |
| 731 | /* |
| 732 | * change filesystem flags. dir should be a physical root of filesystem. |
| 733 | * If you've mounted a non-root directory somewhere and want to do remount |
| 734 | * on it - tough luck. |
| 735 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | static int do_remount(struct nameidata *nd, int flags, int mnt_flags, |
| 737 | void *data) |
| 738 | { |
| 739 | int err; |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 740 | struct super_block *sb = nd->mnt->mnt_sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | |
| 742 | if (!capable(CAP_SYS_ADMIN)) |
| 743 | return -EPERM; |
| 744 | |
| 745 | if (!check_mnt(nd->mnt)) |
| 746 | return -EINVAL; |
| 747 | |
| 748 | if (nd->dentry != nd->mnt->mnt_root) |
| 749 | return -EINVAL; |
| 750 | |
| 751 | down_write(&sb->s_umount); |
| 752 | err = do_remount_sb(sb, flags, data, 0); |
| 753 | if (!err) |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 754 | nd->mnt->mnt_flags = mnt_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | up_write(&sb->s_umount); |
| 756 | if (!err) |
| 757 | security_sb_post_remount(nd->mnt, flags, data); |
| 758 | return err; |
| 759 | } |
| 760 | |
| 761 | static int do_move_mount(struct nameidata *nd, char *old_name) |
| 762 | { |
| 763 | struct nameidata old_nd, parent_nd; |
| 764 | struct vfsmount *p; |
| 765 | int err = 0; |
| 766 | if (!capable(CAP_SYS_ADMIN)) |
| 767 | return -EPERM; |
| 768 | if (!old_name || !*old_name) |
| 769 | return -EINVAL; |
| 770 | err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd); |
| 771 | if (err) |
| 772 | return err; |
| 773 | |
| 774 | down_write(¤t->namespace->sem); |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 775 | while (d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | ; |
| 777 | err = -EINVAL; |
| 778 | if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt)) |
| 779 | goto out; |
| 780 | |
| 781 | err = -ENOENT; |
| 782 | down(&nd->dentry->d_inode->i_sem); |
| 783 | if (IS_DEADDIR(nd->dentry->d_inode)) |
| 784 | goto out1; |
| 785 | |
| 786 | spin_lock(&vfsmount_lock); |
| 787 | if (!IS_ROOT(nd->dentry) && d_unhashed(nd->dentry)) |
| 788 | goto out2; |
| 789 | |
| 790 | err = -EINVAL; |
| 791 | if (old_nd.dentry != old_nd.mnt->mnt_root) |
| 792 | goto out2; |
| 793 | |
| 794 | if (old_nd.mnt == old_nd.mnt->mnt_parent) |
| 795 | goto out2; |
| 796 | |
| 797 | if (S_ISDIR(nd->dentry->d_inode->i_mode) != |
| 798 | S_ISDIR(old_nd.dentry->d_inode->i_mode)) |
| 799 | goto out2; |
| 800 | |
| 801 | err = -ELOOP; |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 802 | for (p = nd->mnt; p->mnt_parent != p; p = p->mnt_parent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | if (p == old_nd.mnt) |
| 804 | goto out2; |
| 805 | err = 0; |
| 806 | |
| 807 | detach_mnt(old_nd.mnt, &parent_nd); |
| 808 | attach_mnt(old_nd.mnt, nd); |
Al Viro | 5addc5d | 2005-11-07 17:15:49 -0500 | [diff] [blame] | 809 | touch_namespace(current->namespace); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | |
| 811 | /* if the mount is moved, it should no longer be expire |
| 812 | * automatically */ |
Miklos Szeredi | 55e700b | 2005-07-07 17:57:30 -0700 | [diff] [blame] | 813 | list_del_init(&old_nd.mnt->mnt_expire); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | out2: |
| 815 | spin_unlock(&vfsmount_lock); |
| 816 | out1: |
| 817 | up(&nd->dentry->d_inode->i_sem); |
| 818 | out: |
| 819 | up_write(¤t->namespace->sem); |
| 820 | if (!err) |
| 821 | path_release(&parent_nd); |
| 822 | path_release(&old_nd); |
| 823 | return err; |
| 824 | } |
| 825 | |
| 826 | /* |
| 827 | * create a new mount for userspace and request it to be added into the |
| 828 | * namespace's tree |
| 829 | */ |
| 830 | static int do_new_mount(struct nameidata *nd, char *type, int flags, |
| 831 | int mnt_flags, char *name, void *data) |
| 832 | { |
| 833 | struct vfsmount *mnt; |
| 834 | |
| 835 | if (!type || !memchr(type, 0, PAGE_SIZE)) |
| 836 | return -EINVAL; |
| 837 | |
| 838 | /* we need capabilities... */ |
| 839 | if (!capable(CAP_SYS_ADMIN)) |
| 840 | return -EPERM; |
| 841 | |
| 842 | mnt = do_kern_mount(type, flags, name, data); |
| 843 | if (IS_ERR(mnt)) |
| 844 | return PTR_ERR(mnt); |
| 845 | |
| 846 | return do_add_mount(mnt, nd, mnt_flags, NULL); |
| 847 | } |
| 848 | |
| 849 | /* |
| 850 | * add a mount into a namespace's mount tree |
| 851 | * - provide the option of adding the new mount to an expiration list |
| 852 | */ |
| 853 | int do_add_mount(struct vfsmount *newmnt, struct nameidata *nd, |
| 854 | int mnt_flags, struct list_head *fslist) |
| 855 | { |
| 856 | int err; |
| 857 | |
| 858 | down_write(¤t->namespace->sem); |
| 859 | /* Something was mounted here while we slept */ |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 860 | while (d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | ; |
| 862 | err = -EINVAL; |
| 863 | if (!check_mnt(nd->mnt)) |
| 864 | goto unlock; |
| 865 | |
| 866 | /* Refuse the same filesystem on the same mount point */ |
| 867 | err = -EBUSY; |
| 868 | if (nd->mnt->mnt_sb == newmnt->mnt_sb && |
| 869 | nd->mnt->mnt_root == nd->dentry) |
| 870 | goto unlock; |
| 871 | |
| 872 | err = -EINVAL; |
| 873 | if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode)) |
| 874 | goto unlock; |
| 875 | |
| 876 | newmnt->mnt_flags = mnt_flags; |
Ram Pai | 5b83d2c | 2005-11-07 17:16:29 -0500 | [diff] [blame] | 877 | if ((err = graft_tree(newmnt, nd))) |
| 878 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | |
Ram Pai | 5b83d2c | 2005-11-07 17:16:29 -0500 | [diff] [blame] | 880 | if (fslist) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | /* add to the specified expiration list */ |
| 882 | spin_lock(&vfsmount_lock); |
Miklos Szeredi | 55e700b | 2005-07-07 17:57:30 -0700 | [diff] [blame] | 883 | list_add_tail(&newmnt->mnt_expire, fslist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | spin_unlock(&vfsmount_lock); |
| 885 | } |
Ram Pai | 5b83d2c | 2005-11-07 17:16:29 -0500 | [diff] [blame] | 886 | up_write(¤t->namespace->sem); |
| 887 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | |
| 889 | unlock: |
| 890 | up_write(¤t->namespace->sem); |
| 891 | mntput(newmnt); |
| 892 | return err; |
| 893 | } |
| 894 | |
| 895 | EXPORT_SYMBOL_GPL(do_add_mount); |
| 896 | |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame^] | 897 | static void expire_mount(struct vfsmount *mnt, struct list_head *mounts, |
| 898 | struct list_head *umounts) |
Miklos Szeredi | 24ca2af | 2005-07-07 17:57:25 -0700 | [diff] [blame] | 899 | { |
| 900 | spin_lock(&vfsmount_lock); |
| 901 | |
| 902 | /* |
Miklos Szeredi | ed42c87 | 2005-07-07 17:57:26 -0700 | [diff] [blame] | 903 | * Check if mount is still attached, if not, let whoever holds it deal |
| 904 | * with the sucker |
| 905 | */ |
| 906 | if (mnt->mnt_parent == mnt) { |
| 907 | spin_unlock(&vfsmount_lock); |
| 908 | return; |
| 909 | } |
| 910 | |
| 911 | /* |
Miklos Szeredi | 24ca2af | 2005-07-07 17:57:25 -0700 | [diff] [blame] | 912 | * Check that it is still dead: the count should now be 2 - as |
| 913 | * contributed by the vfsmount parent and the mntget above |
| 914 | */ |
| 915 | if (atomic_read(&mnt->mnt_count) == 2) { |
Miklos Szeredi | 24ca2af | 2005-07-07 17:57:25 -0700 | [diff] [blame] | 916 | /* delete from the namespace */ |
Al Viro | 5addc5d | 2005-11-07 17:15:49 -0500 | [diff] [blame] | 917 | touch_namespace(mnt->mnt_namespace); |
Miklos Szeredi | 24ca2af | 2005-07-07 17:57:25 -0700 | [diff] [blame] | 918 | list_del_init(&mnt->mnt_list); |
Miklos Szeredi | ac08115 | 2005-07-07 17:57:27 -0700 | [diff] [blame] | 919 | mnt->mnt_namespace = NULL; |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame^] | 920 | umount_tree(mnt, umounts); |
Miklos Szeredi | 24ca2af | 2005-07-07 17:57:25 -0700 | [diff] [blame] | 921 | spin_unlock(&vfsmount_lock); |
Miklos Szeredi | 24ca2af | 2005-07-07 17:57:25 -0700 | [diff] [blame] | 922 | } else { |
| 923 | /* |
| 924 | * Someone brought it back to life whilst we didn't have any |
| 925 | * locks held so return it to the expiration list |
| 926 | */ |
Miklos Szeredi | 55e700b | 2005-07-07 17:57:30 -0700 | [diff] [blame] | 927 | list_add_tail(&mnt->mnt_expire, mounts); |
Miklos Szeredi | 24ca2af | 2005-07-07 17:57:25 -0700 | [diff] [blame] | 928 | spin_unlock(&vfsmount_lock); |
| 929 | } |
| 930 | } |
| 931 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | /* |
| 933 | * process a list of expirable mountpoints with the intent of discarding any |
| 934 | * mountpoints that aren't in use and haven't been touched since last we came |
| 935 | * here |
| 936 | */ |
| 937 | void mark_mounts_for_expiry(struct list_head *mounts) |
| 938 | { |
| 939 | struct namespace *namespace; |
| 940 | struct vfsmount *mnt, *next; |
| 941 | LIST_HEAD(graveyard); |
| 942 | |
| 943 | if (list_empty(mounts)) |
| 944 | return; |
| 945 | |
| 946 | spin_lock(&vfsmount_lock); |
| 947 | |
| 948 | /* extract from the expiration list every vfsmount that matches the |
| 949 | * following criteria: |
| 950 | * - only referenced by its parent vfsmount |
| 951 | * - still marked for expiry (marked on the last call here; marks are |
| 952 | * cleared by mntput()) |
| 953 | */ |
Miklos Szeredi | 55e700b | 2005-07-07 17:57:30 -0700 | [diff] [blame] | 954 | list_for_each_entry_safe(mnt, next, mounts, mnt_expire) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | if (!xchg(&mnt->mnt_expiry_mark, 1) || |
| 956 | atomic_read(&mnt->mnt_count) != 1) |
| 957 | continue; |
| 958 | |
| 959 | mntget(mnt); |
Miklos Szeredi | 55e700b | 2005-07-07 17:57:30 -0700 | [diff] [blame] | 960 | list_move(&mnt->mnt_expire, &graveyard); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | /* |
| 964 | * go through the vfsmounts we've just consigned to the graveyard to |
| 965 | * - check that they're still dead |
| 966 | * - delete the vfsmount from the appropriate namespace under lock |
| 967 | * - dispose of the corpse |
| 968 | */ |
| 969 | while (!list_empty(&graveyard)) { |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame^] | 970 | LIST_HEAD(umounts); |
Miklos Szeredi | 55e700b | 2005-07-07 17:57:30 -0700 | [diff] [blame] | 971 | mnt = list_entry(graveyard.next, struct vfsmount, mnt_expire); |
| 972 | list_del_init(&mnt->mnt_expire); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | |
| 974 | /* don't do anything if the namespace is dead - all the |
| 975 | * vfsmounts from it are going away anyway */ |
| 976 | namespace = mnt->mnt_namespace; |
Miklos Szeredi | 1ce88cf | 2005-07-07 17:57:24 -0700 | [diff] [blame] | 977 | if (!namespace || !namespace->root) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | continue; |
| 979 | get_namespace(namespace); |
| 980 | |
| 981 | spin_unlock(&vfsmount_lock); |
| 982 | down_write(&namespace->sem); |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame^] | 983 | expire_mount(mnt, mounts, &umounts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | up_write(&namespace->sem); |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame^] | 985 | release_mounts(&umounts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | mntput(mnt); |
| 987 | put_namespace(namespace); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | spin_lock(&vfsmount_lock); |
| 989 | } |
| 990 | |
| 991 | spin_unlock(&vfsmount_lock); |
| 992 | } |
| 993 | |
| 994 | EXPORT_SYMBOL_GPL(mark_mounts_for_expiry); |
| 995 | |
| 996 | /* |
| 997 | * Some copy_from_user() implementations do not return the exact number of |
| 998 | * bytes remaining to copy on a fault. But copy_mount_options() requires that. |
| 999 | * Note that this function differs from copy_from_user() in that it will oops |
| 1000 | * on bad values of `to', rather than returning a short copy. |
| 1001 | */ |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 1002 | static long exact_copy_from_user(void *to, const void __user * from, |
| 1003 | unsigned long n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | { |
| 1005 | char *t = to; |
| 1006 | const char __user *f = from; |
| 1007 | char c; |
| 1008 | |
| 1009 | if (!access_ok(VERIFY_READ, from, n)) |
| 1010 | return n; |
| 1011 | |
| 1012 | while (n) { |
| 1013 | if (__get_user(c, f)) { |
| 1014 | memset(t, 0, n); |
| 1015 | break; |
| 1016 | } |
| 1017 | *t++ = c; |
| 1018 | f++; |
| 1019 | n--; |
| 1020 | } |
| 1021 | return n; |
| 1022 | } |
| 1023 | |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 1024 | int copy_mount_options(const void __user * data, unsigned long *where) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | { |
| 1026 | int i; |
| 1027 | unsigned long page; |
| 1028 | unsigned long size; |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 1029 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | *where = 0; |
| 1031 | if (!data) |
| 1032 | return 0; |
| 1033 | |
| 1034 | if (!(page = __get_free_page(GFP_KERNEL))) |
| 1035 | return -ENOMEM; |
| 1036 | |
| 1037 | /* We only care that *some* data at the address the user |
| 1038 | * gave us is valid. Just in case, we'll zero |
| 1039 | * the remainder of the page. |
| 1040 | */ |
| 1041 | /* copy_from_user cannot cross TASK_SIZE ! */ |
| 1042 | size = TASK_SIZE - (unsigned long)data; |
| 1043 | if (size > PAGE_SIZE) |
| 1044 | size = PAGE_SIZE; |
| 1045 | |
| 1046 | i = size - exact_copy_from_user((void *)page, data, size); |
| 1047 | if (!i) { |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 1048 | free_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | return -EFAULT; |
| 1050 | } |
| 1051 | if (i != PAGE_SIZE) |
| 1052 | memset((char *)page + i, 0, PAGE_SIZE - i); |
| 1053 | *where = page; |
| 1054 | return 0; |
| 1055 | } |
| 1056 | |
| 1057 | /* |
| 1058 | * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to |
| 1059 | * be given to the mount() call (ie: read-only, no-dev, no-suid etc). |
| 1060 | * |
| 1061 | * data is a (void *) that can point to any structure up to |
| 1062 | * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent |
| 1063 | * information (or be NULL). |
| 1064 | * |
| 1065 | * Pre-0.97 versions of mount() didn't have a flags word. |
| 1066 | * When the flags word was introduced its top half was required |
| 1067 | * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9. |
| 1068 | * Therefore, if this magic number is present, it carries no information |
| 1069 | * and must be discarded. |
| 1070 | */ |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 1071 | long do_mount(char *dev_name, char *dir_name, char *type_page, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | unsigned long flags, void *data_page) |
| 1073 | { |
| 1074 | struct nameidata nd; |
| 1075 | int retval = 0; |
| 1076 | int mnt_flags = 0; |
| 1077 | |
| 1078 | /* Discard magic */ |
| 1079 | if ((flags & MS_MGC_MSK) == MS_MGC_VAL) |
| 1080 | flags &= ~MS_MGC_MSK; |
| 1081 | |
| 1082 | /* Basic sanity checks */ |
| 1083 | |
| 1084 | if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE)) |
| 1085 | return -EINVAL; |
| 1086 | if (dev_name && !memchr(dev_name, 0, PAGE_SIZE)) |
| 1087 | return -EINVAL; |
| 1088 | |
| 1089 | if (data_page) |
| 1090 | ((char *)data_page)[PAGE_SIZE - 1] = 0; |
| 1091 | |
| 1092 | /* Separate the per-mountpoint flags */ |
| 1093 | if (flags & MS_NOSUID) |
| 1094 | mnt_flags |= MNT_NOSUID; |
| 1095 | if (flags & MS_NODEV) |
| 1096 | mnt_flags |= MNT_NODEV; |
| 1097 | if (flags & MS_NOEXEC) |
| 1098 | mnt_flags |= MNT_NOEXEC; |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 1099 | flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | |
| 1101 | /* ... and get the mountpoint */ |
| 1102 | retval = path_lookup(dir_name, LOOKUP_FOLLOW, &nd); |
| 1103 | if (retval) |
| 1104 | return retval; |
| 1105 | |
| 1106 | retval = security_sb_mount(dev_name, &nd, type_page, flags, data_page); |
| 1107 | if (retval) |
| 1108 | goto dput_out; |
| 1109 | |
| 1110 | if (flags & MS_REMOUNT) |
| 1111 | retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags, |
| 1112 | data_page); |
| 1113 | else if (flags & MS_BIND) |
| 1114 | retval = do_loopback(&nd, dev_name, flags & MS_REC); |
| 1115 | else if (flags & MS_MOVE) |
| 1116 | retval = do_move_mount(&nd, dev_name); |
| 1117 | else |
| 1118 | retval = do_new_mount(&nd, type_page, flags, mnt_flags, |
| 1119 | dev_name, data_page); |
| 1120 | dput_out: |
| 1121 | path_release(&nd); |
| 1122 | return retval; |
| 1123 | } |
| 1124 | |
| 1125 | int copy_namespace(int flags, struct task_struct *tsk) |
| 1126 | { |
| 1127 | struct namespace *namespace = tsk->namespace; |
| 1128 | struct namespace *new_ns; |
| 1129 | struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL; |
| 1130 | struct fs_struct *fs = tsk->fs; |
| 1131 | struct vfsmount *p, *q; |
| 1132 | |
| 1133 | if (!namespace) |
| 1134 | return 0; |
| 1135 | |
| 1136 | get_namespace(namespace); |
| 1137 | |
| 1138 | if (!(flags & CLONE_NEWNS)) |
| 1139 | return 0; |
| 1140 | |
| 1141 | if (!capable(CAP_SYS_ADMIN)) { |
| 1142 | put_namespace(namespace); |
| 1143 | return -EPERM; |
| 1144 | } |
| 1145 | |
| 1146 | new_ns = kmalloc(sizeof(struct namespace), GFP_KERNEL); |
| 1147 | if (!new_ns) |
| 1148 | goto out; |
| 1149 | |
| 1150 | atomic_set(&new_ns->count, 1); |
| 1151 | init_rwsem(&new_ns->sem); |
| 1152 | INIT_LIST_HEAD(&new_ns->list); |
Al Viro | 5addc5d | 2005-11-07 17:15:49 -0500 | [diff] [blame] | 1153 | init_waitqueue_head(&new_ns->poll); |
| 1154 | new_ns->event = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | |
| 1156 | down_write(&tsk->namespace->sem); |
| 1157 | /* First pass: copy the tree topology */ |
| 1158 | new_ns->root = copy_tree(namespace->root, namespace->root->mnt_root); |
| 1159 | if (!new_ns->root) { |
| 1160 | up_write(&tsk->namespace->sem); |
| 1161 | kfree(new_ns); |
| 1162 | goto out; |
| 1163 | } |
| 1164 | spin_lock(&vfsmount_lock); |
| 1165 | list_add_tail(&new_ns->list, &new_ns->root->mnt_list); |
| 1166 | spin_unlock(&vfsmount_lock); |
| 1167 | |
| 1168 | /* |
| 1169 | * Second pass: switch the tsk->fs->* elements and mark new vfsmounts |
| 1170 | * as belonging to new namespace. We have already acquired a private |
| 1171 | * fs_struct, so tsk->fs->lock is not needed. |
| 1172 | */ |
| 1173 | p = namespace->root; |
| 1174 | q = new_ns->root; |
| 1175 | while (p) { |
| 1176 | q->mnt_namespace = new_ns; |
| 1177 | if (fs) { |
| 1178 | if (p == fs->rootmnt) { |
| 1179 | rootmnt = p; |
| 1180 | fs->rootmnt = mntget(q); |
| 1181 | } |
| 1182 | if (p == fs->pwdmnt) { |
| 1183 | pwdmnt = p; |
| 1184 | fs->pwdmnt = mntget(q); |
| 1185 | } |
| 1186 | if (p == fs->altrootmnt) { |
| 1187 | altrootmnt = p; |
| 1188 | fs->altrootmnt = mntget(q); |
| 1189 | } |
| 1190 | } |
| 1191 | p = next_mnt(p, namespace->root); |
| 1192 | q = next_mnt(q, new_ns->root); |
| 1193 | } |
| 1194 | up_write(&tsk->namespace->sem); |
| 1195 | |
| 1196 | tsk->namespace = new_ns; |
| 1197 | |
| 1198 | if (rootmnt) |
| 1199 | mntput(rootmnt); |
| 1200 | if (pwdmnt) |
| 1201 | mntput(pwdmnt); |
| 1202 | if (altrootmnt) |
| 1203 | mntput(altrootmnt); |
| 1204 | |
| 1205 | put_namespace(namespace); |
| 1206 | return 0; |
| 1207 | |
| 1208 | out: |
| 1209 | put_namespace(namespace); |
| 1210 | return -ENOMEM; |
| 1211 | } |
| 1212 | |
| 1213 | asmlinkage long sys_mount(char __user * dev_name, char __user * dir_name, |
| 1214 | char __user * type, unsigned long flags, |
| 1215 | void __user * data) |
| 1216 | { |
| 1217 | int retval; |
| 1218 | unsigned long data_page; |
| 1219 | unsigned long type_page; |
| 1220 | unsigned long dev_page; |
| 1221 | char *dir_page; |
| 1222 | |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 1223 | retval = copy_mount_options(type, &type_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | if (retval < 0) |
| 1225 | return retval; |
| 1226 | |
| 1227 | dir_page = getname(dir_name); |
| 1228 | retval = PTR_ERR(dir_page); |
| 1229 | if (IS_ERR(dir_page)) |
| 1230 | goto out1; |
| 1231 | |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 1232 | retval = copy_mount_options(dev_name, &dev_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | if (retval < 0) |
| 1234 | goto out2; |
| 1235 | |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 1236 | retval = copy_mount_options(data, &data_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | if (retval < 0) |
| 1238 | goto out3; |
| 1239 | |
| 1240 | lock_kernel(); |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 1241 | retval = do_mount((char *)dev_page, dir_page, (char *)type_page, |
| 1242 | flags, (void *)data_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | unlock_kernel(); |
| 1244 | free_page(data_page); |
| 1245 | |
| 1246 | out3: |
| 1247 | free_page(dev_page); |
| 1248 | out2: |
| 1249 | putname(dir_page); |
| 1250 | out1: |
| 1251 | free_page(type_page); |
| 1252 | return retval; |
| 1253 | } |
| 1254 | |
| 1255 | /* |
| 1256 | * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values. |
| 1257 | * It can block. Requires the big lock held. |
| 1258 | */ |
| 1259 | void set_fs_root(struct fs_struct *fs, struct vfsmount *mnt, |
| 1260 | struct dentry *dentry) |
| 1261 | { |
| 1262 | struct dentry *old_root; |
| 1263 | struct vfsmount *old_rootmnt; |
| 1264 | write_lock(&fs->lock); |
| 1265 | old_root = fs->root; |
| 1266 | old_rootmnt = fs->rootmnt; |
| 1267 | fs->rootmnt = mntget(mnt); |
| 1268 | fs->root = dget(dentry); |
| 1269 | write_unlock(&fs->lock); |
| 1270 | if (old_root) { |
| 1271 | dput(old_root); |
| 1272 | mntput(old_rootmnt); |
| 1273 | } |
| 1274 | } |
| 1275 | |
| 1276 | /* |
| 1277 | * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values. |
| 1278 | * It can block. Requires the big lock held. |
| 1279 | */ |
| 1280 | void set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt, |
| 1281 | struct dentry *dentry) |
| 1282 | { |
| 1283 | struct dentry *old_pwd; |
| 1284 | struct vfsmount *old_pwdmnt; |
| 1285 | |
| 1286 | write_lock(&fs->lock); |
| 1287 | old_pwd = fs->pwd; |
| 1288 | old_pwdmnt = fs->pwdmnt; |
| 1289 | fs->pwdmnt = mntget(mnt); |
| 1290 | fs->pwd = dget(dentry); |
| 1291 | write_unlock(&fs->lock); |
| 1292 | |
| 1293 | if (old_pwd) { |
| 1294 | dput(old_pwd); |
| 1295 | mntput(old_pwdmnt); |
| 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | static void chroot_fs_refs(struct nameidata *old_nd, struct nameidata *new_nd) |
| 1300 | { |
| 1301 | struct task_struct *g, *p; |
| 1302 | struct fs_struct *fs; |
| 1303 | |
| 1304 | read_lock(&tasklist_lock); |
| 1305 | do_each_thread(g, p) { |
| 1306 | task_lock(p); |
| 1307 | fs = p->fs; |
| 1308 | if (fs) { |
| 1309 | atomic_inc(&fs->count); |
| 1310 | task_unlock(p); |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 1311 | if (fs->root == old_nd->dentry |
| 1312 | && fs->rootmnt == old_nd->mnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | set_fs_root(fs, new_nd->mnt, new_nd->dentry); |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 1314 | if (fs->pwd == old_nd->dentry |
| 1315 | && fs->pwdmnt == old_nd->mnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | set_fs_pwd(fs, new_nd->mnt, new_nd->dentry); |
| 1317 | put_fs_struct(fs); |
| 1318 | } else |
| 1319 | task_unlock(p); |
| 1320 | } while_each_thread(g, p); |
| 1321 | read_unlock(&tasklist_lock); |
| 1322 | } |
| 1323 | |
| 1324 | /* |
| 1325 | * pivot_root Semantics: |
| 1326 | * Moves the root file system of the current process to the directory put_old, |
| 1327 | * makes new_root as the new root file system of the current process, and sets |
| 1328 | * root/cwd of all processes which had them on the current root to new_root. |
| 1329 | * |
| 1330 | * Restrictions: |
| 1331 | * The new_root and put_old must be directories, and must not be on the |
| 1332 | * same file system as the current process root. The put_old must be |
| 1333 | * underneath new_root, i.e. adding a non-zero number of /.. to the string |
| 1334 | * pointed to by put_old must yield the same directory as new_root. No other |
| 1335 | * file system may be mounted on put_old. After all, new_root is a mountpoint. |
| 1336 | * |
| 1337 | * Notes: |
| 1338 | * - we don't move root/cwd if they are not at the root (reason: if something |
| 1339 | * cared enough to change them, it's probably wrong to force them elsewhere) |
| 1340 | * - it's okay to pick a root that isn't the root of a file system, e.g. |
| 1341 | * /nfs/my_root where /nfs is the mount point. It must be a mountpoint, |
| 1342 | * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root |
| 1343 | * first. |
| 1344 | */ |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 1345 | asmlinkage long sys_pivot_root(const char __user * new_root, |
| 1346 | const char __user * put_old) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | { |
| 1348 | struct vfsmount *tmp; |
| 1349 | struct nameidata new_nd, old_nd, parent_nd, root_parent, user_nd; |
| 1350 | int error; |
| 1351 | |
| 1352 | if (!capable(CAP_SYS_ADMIN)) |
| 1353 | return -EPERM; |
| 1354 | |
| 1355 | lock_kernel(); |
| 1356 | |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 1357 | error = __user_walk(new_root, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, |
| 1358 | &new_nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | if (error) |
| 1360 | goto out0; |
| 1361 | error = -EINVAL; |
| 1362 | if (!check_mnt(new_nd.mnt)) |
| 1363 | goto out1; |
| 1364 | |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 1365 | error = __user_walk(put_old, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &old_nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | if (error) |
| 1367 | goto out1; |
| 1368 | |
| 1369 | error = security_sb_pivotroot(&old_nd, &new_nd); |
| 1370 | if (error) { |
| 1371 | path_release(&old_nd); |
| 1372 | goto out1; |
| 1373 | } |
| 1374 | |
| 1375 | read_lock(¤t->fs->lock); |
| 1376 | user_nd.mnt = mntget(current->fs->rootmnt); |
| 1377 | user_nd.dentry = dget(current->fs->root); |
| 1378 | read_unlock(¤t->fs->lock); |
| 1379 | down_write(¤t->namespace->sem); |
| 1380 | down(&old_nd.dentry->d_inode->i_sem); |
| 1381 | error = -EINVAL; |
| 1382 | if (!check_mnt(user_nd.mnt)) |
| 1383 | goto out2; |
| 1384 | error = -ENOENT; |
| 1385 | if (IS_DEADDIR(new_nd.dentry->d_inode)) |
| 1386 | goto out2; |
| 1387 | if (d_unhashed(new_nd.dentry) && !IS_ROOT(new_nd.dentry)) |
| 1388 | goto out2; |
| 1389 | if (d_unhashed(old_nd.dentry) && !IS_ROOT(old_nd.dentry)) |
| 1390 | goto out2; |
| 1391 | error = -EBUSY; |
| 1392 | if (new_nd.mnt == user_nd.mnt || old_nd.mnt == user_nd.mnt) |
| 1393 | goto out2; /* loop, on the same file system */ |
| 1394 | error = -EINVAL; |
| 1395 | if (user_nd.mnt->mnt_root != user_nd.dentry) |
| 1396 | goto out2; /* not a mountpoint */ |
Miklos Szeredi | 0bb6fcc | 2005-09-06 15:19:36 -0700 | [diff] [blame] | 1397 | if (user_nd.mnt->mnt_parent == user_nd.mnt) |
| 1398 | goto out2; /* not attached */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | if (new_nd.mnt->mnt_root != new_nd.dentry) |
| 1400 | goto out2; /* not a mountpoint */ |
Miklos Szeredi | 0bb6fcc | 2005-09-06 15:19:36 -0700 | [diff] [blame] | 1401 | if (new_nd.mnt->mnt_parent == new_nd.mnt) |
| 1402 | goto out2; /* not attached */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | tmp = old_nd.mnt; /* make sure we can reach put_old from new_root */ |
| 1404 | spin_lock(&vfsmount_lock); |
| 1405 | if (tmp != new_nd.mnt) { |
| 1406 | for (;;) { |
| 1407 | if (tmp->mnt_parent == tmp) |
| 1408 | goto out3; /* already mounted on put_old */ |
| 1409 | if (tmp->mnt_parent == new_nd.mnt) |
| 1410 | break; |
| 1411 | tmp = tmp->mnt_parent; |
| 1412 | } |
| 1413 | if (!is_subdir(tmp->mnt_mountpoint, new_nd.dentry)) |
| 1414 | goto out3; |
| 1415 | } else if (!is_subdir(old_nd.dentry, new_nd.dentry)) |
| 1416 | goto out3; |
| 1417 | detach_mnt(new_nd.mnt, &parent_nd); |
| 1418 | detach_mnt(user_nd.mnt, &root_parent); |
| 1419 | attach_mnt(user_nd.mnt, &old_nd); /* mount old root on put_old */ |
| 1420 | attach_mnt(new_nd.mnt, &root_parent); /* mount new_root on / */ |
Al Viro | 5addc5d | 2005-11-07 17:15:49 -0500 | [diff] [blame] | 1421 | touch_namespace(current->namespace); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | spin_unlock(&vfsmount_lock); |
| 1423 | chroot_fs_refs(&user_nd, &new_nd); |
| 1424 | security_sb_post_pivotroot(&user_nd, &new_nd); |
| 1425 | error = 0; |
| 1426 | path_release(&root_parent); |
| 1427 | path_release(&parent_nd); |
| 1428 | out2: |
| 1429 | up(&old_nd.dentry->d_inode->i_sem); |
| 1430 | up_write(¤t->namespace->sem); |
| 1431 | path_release(&user_nd); |
| 1432 | path_release(&old_nd); |
| 1433 | out1: |
| 1434 | path_release(&new_nd); |
| 1435 | out0: |
| 1436 | unlock_kernel(); |
| 1437 | return error; |
| 1438 | out3: |
| 1439 | spin_unlock(&vfsmount_lock); |
| 1440 | goto out2; |
| 1441 | } |
| 1442 | |
| 1443 | static void __init init_mount_tree(void) |
| 1444 | { |
| 1445 | struct vfsmount *mnt; |
| 1446 | struct namespace *namespace; |
| 1447 | struct task_struct *g, *p; |
| 1448 | |
| 1449 | mnt = do_kern_mount("rootfs", 0, "rootfs", NULL); |
| 1450 | if (IS_ERR(mnt)) |
| 1451 | panic("Can't create rootfs"); |
| 1452 | namespace = kmalloc(sizeof(*namespace), GFP_KERNEL); |
| 1453 | if (!namespace) |
| 1454 | panic("Can't allocate initial namespace"); |
| 1455 | atomic_set(&namespace->count, 1); |
| 1456 | INIT_LIST_HEAD(&namespace->list); |
| 1457 | init_rwsem(&namespace->sem); |
Al Viro | 5addc5d | 2005-11-07 17:15:49 -0500 | [diff] [blame] | 1458 | init_waitqueue_head(&namespace->poll); |
| 1459 | namespace->event = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | list_add(&mnt->mnt_list, &namespace->list); |
| 1461 | namespace->root = mnt; |
| 1462 | mnt->mnt_namespace = namespace; |
| 1463 | |
| 1464 | init_task.namespace = namespace; |
| 1465 | read_lock(&tasklist_lock); |
| 1466 | do_each_thread(g, p) { |
| 1467 | get_namespace(namespace); |
| 1468 | p->namespace = namespace; |
| 1469 | } while_each_thread(g, p); |
| 1470 | read_unlock(&tasklist_lock); |
| 1471 | |
| 1472 | set_fs_pwd(current->fs, namespace->root, namespace->root->mnt_root); |
| 1473 | set_fs_root(current->fs, namespace->root, namespace->root->mnt_root); |
| 1474 | } |
| 1475 | |
| 1476 | void __init mnt_init(unsigned long mempages) |
| 1477 | { |
| 1478 | struct list_head *d; |
| 1479 | unsigned int nr_hash; |
| 1480 | int i; |
| 1481 | |
| 1482 | mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount), |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 1483 | 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 1485 | mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | |
| 1487 | if (!mount_hashtable) |
| 1488 | panic("Failed to allocate mount hash table\n"); |
| 1489 | |
| 1490 | /* |
| 1491 | * Find the power-of-two list-heads that can fit into the allocation.. |
| 1492 | * We don't guarantee that "sizeof(struct list_head)" is necessarily |
| 1493 | * a power-of-two. |
| 1494 | */ |
| 1495 | nr_hash = PAGE_SIZE / sizeof(struct list_head); |
| 1496 | hash_bits = 0; |
| 1497 | do { |
| 1498 | hash_bits++; |
| 1499 | } while ((nr_hash >> hash_bits) != 0); |
| 1500 | hash_bits--; |
| 1501 | |
| 1502 | /* |
| 1503 | * Re-calculate the actual number of entries and the mask |
| 1504 | * from the number of bits we can fit. |
| 1505 | */ |
| 1506 | nr_hash = 1UL << hash_bits; |
Ram Pai | b58fed8 | 2005-11-07 17:16:09 -0500 | [diff] [blame] | 1507 | hash_mask = nr_hash - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | |
| 1509 | printk("Mount-cache hash table entries: %d\n", nr_hash); |
| 1510 | |
| 1511 | /* And initialize the newly allocated array */ |
| 1512 | d = mount_hashtable; |
| 1513 | i = nr_hash; |
| 1514 | do { |
| 1515 | INIT_LIST_HEAD(d); |
| 1516 | d++; |
| 1517 | i--; |
| 1518 | } while (i); |
| 1519 | sysfs_init(); |
| 1520 | init_rootfs(); |
| 1521 | init_mount_tree(); |
| 1522 | } |
| 1523 | |
| 1524 | void __put_namespace(struct namespace *namespace) |
| 1525 | { |
Miklos Szeredi | 1ce88cf | 2005-07-07 17:57:24 -0700 | [diff] [blame] | 1526 | struct vfsmount *root = namespace->root; |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame^] | 1527 | LIST_HEAD(umount_list); |
Miklos Szeredi | 1ce88cf | 2005-07-07 17:57:24 -0700 | [diff] [blame] | 1528 | namespace->root = NULL; |
| 1529 | spin_unlock(&vfsmount_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | down_write(&namespace->sem); |
| 1531 | spin_lock(&vfsmount_lock); |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame^] | 1532 | umount_tree(root, &umount_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | spin_unlock(&vfsmount_lock); |
| 1534 | up_write(&namespace->sem); |
Ram Pai | 70fbcdf | 2005-11-07 17:17:04 -0500 | [diff] [blame^] | 1535 | release_mounts(&umount_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | kfree(namespace); |
| 1537 | } |