[PATCH] DVB: Documentation and Kconfig updazes
[linux-2.6/history.git] / fs / namespace.c
blob4584a684c685f4b2af928d17a4d81d47cbf248cf
1 /*
2 * linux/fs/namespace.c
4 * (C) Copyright Al Viro 2000, 2001
5 * Released under GPL v2.
7 * Based on code from fs/super.c, copyright Linus Torvalds and others.
8 * Heavily rewritten.
9 */
11 #include <linux/config.h>
12 #include <linux/slab.h>
13 #include <linux/sched.h>
14 #include <linux/smp_lock.h>
15 #include <linux/init.h>
16 #include <linux/quotaops.h>
17 #include <linux/acct.h>
18 #include <linux/module.h>
19 #include <linux/seq_file.h>
20 #include <linux/namespace.h>
21 #include <linux/namei.h>
22 #include <linux/security.h>
23 #include <linux/mount.h>
24 #include <asm/uaccess.h>
26 extern int __init init_rootfs(void);
27 extern int __init sysfs_init(void);
29 /* spinlock for vfsmount related operations, inplace of dcache_lock */
30 spinlock_t vfsmount_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
31 static struct list_head *mount_hashtable;
32 static int hash_mask, hash_bits;
33 static kmem_cache_t *mnt_cache;
35 static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
37 unsigned long tmp = ((unsigned long) mnt / L1_CACHE_BYTES);
38 tmp += ((unsigned long) dentry / L1_CACHE_BYTES);
39 tmp = tmp + (tmp >> hash_bits);
40 return tmp & hash_mask;
43 struct vfsmount *alloc_vfsmnt(const char *name)
45 struct vfsmount *mnt = kmem_cache_alloc(mnt_cache, GFP_KERNEL);
46 if (mnt) {
47 memset(mnt, 0, sizeof(struct vfsmount));
48 atomic_set(&mnt->mnt_count,1);
49 INIT_LIST_HEAD(&mnt->mnt_hash);
50 INIT_LIST_HEAD(&mnt->mnt_child);
51 INIT_LIST_HEAD(&mnt->mnt_mounts);
52 INIT_LIST_HEAD(&mnt->mnt_list);
53 if (name) {
54 int size = strlen(name)+1;
55 char *newname = kmalloc(size, GFP_KERNEL);
56 if (newname) {
57 memcpy(newname, name, size);
58 mnt->mnt_devname = newname;
62 return mnt;
65 void free_vfsmnt(struct vfsmount *mnt)
67 kfree(mnt->mnt_devname);
68 kmem_cache_free(mnt_cache, mnt);
72 * Now, lookup_mnt increments the ref count before returning
73 * the vfsmount struct.
75 struct vfsmount *lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
77 struct list_head * head = mount_hashtable + hash(mnt, dentry);
78 struct list_head * tmp = head;
79 struct vfsmount *p, *found = NULL;
81 spin_lock(&vfsmount_lock);
82 for (;;) {
83 tmp = tmp->next;
84 p = NULL;
85 if (tmp == head)
86 break;
87 p = list_entry(tmp, struct vfsmount, mnt_hash);
88 if (p->mnt_parent == mnt && p->mnt_mountpoint == dentry) {
89 found = mntget(p);
90 break;
93 spin_unlock(&vfsmount_lock);
94 return found;
97 EXPORT_SYMBOL(lookup_mnt);
99 static int check_mnt(struct vfsmount *mnt)
101 spin_lock(&vfsmount_lock);
102 while (mnt->mnt_parent != mnt)
103 mnt = mnt->mnt_parent;
104 spin_unlock(&vfsmount_lock);
105 return mnt == current->namespace->root;
108 static void detach_mnt(struct vfsmount *mnt, struct nameidata *old_nd)
110 old_nd->dentry = mnt->mnt_mountpoint;
111 old_nd->mnt = mnt->mnt_parent;
112 mnt->mnt_parent = mnt;
113 mnt->mnt_mountpoint = mnt->mnt_root;
114 list_del_init(&mnt->mnt_child);
115 list_del_init(&mnt->mnt_hash);
116 old_nd->dentry->d_mounted--;
119 static void attach_mnt(struct vfsmount *mnt, struct nameidata *nd)
121 mnt->mnt_parent = mntget(nd->mnt);
122 mnt->mnt_mountpoint = dget(nd->dentry);
123 list_add(&mnt->mnt_hash, mount_hashtable+hash(nd->mnt, nd->dentry));
124 list_add_tail(&mnt->mnt_child, &nd->mnt->mnt_mounts);
125 nd->dentry->d_mounted++;
128 static struct vfsmount *next_mnt(struct vfsmount *p, struct vfsmount *root)
130 struct list_head *next = p->mnt_mounts.next;
131 if (next == &p->mnt_mounts) {
132 while (1) {
133 if (p == root)
134 return NULL;
135 next = p->mnt_child.next;
136 if (next != &p->mnt_parent->mnt_mounts)
137 break;
138 p = p->mnt_parent;
141 return list_entry(next, struct vfsmount, mnt_child);
144 static struct vfsmount *
145 clone_mnt(struct vfsmount *old, struct dentry *root)
147 struct super_block *sb = old->mnt_sb;
148 struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname);
150 if (mnt) {
151 mnt->mnt_flags = old->mnt_flags;
152 atomic_inc(&sb->s_active);
153 mnt->mnt_sb = sb;
154 mnt->mnt_root = dget(root);
155 mnt->mnt_mountpoint = mnt->mnt_root;
156 mnt->mnt_parent = mnt;
158 return mnt;
161 void __mntput(struct vfsmount *mnt)
163 struct super_block *sb = mnt->mnt_sb;
164 dput(mnt->mnt_root);
165 free_vfsmnt(mnt);
166 deactivate_super(sb);
169 EXPORT_SYMBOL(__mntput);
171 /* iterator */
172 static void *m_start(struct seq_file *m, loff_t *pos)
174 struct namespace *n = m->private;
175 struct list_head *p;
176 loff_t l = *pos;
178 down_read(&n->sem);
179 list_for_each(p, &n->list)
180 if (!l--)
181 return list_entry(p, struct vfsmount, mnt_list);
182 return NULL;
185 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
187 struct namespace *n = m->private;
188 struct list_head *p = ((struct vfsmount *)v)->mnt_list.next;
189 (*pos)++;
190 return p==&n->list ? NULL : list_entry(p, struct vfsmount, mnt_list);
193 static void m_stop(struct seq_file *m, void *v)
195 struct namespace *n = m->private;
196 up_read(&n->sem);
199 static inline void mangle(struct seq_file *m, const char *s)
201 seq_escape(m, s, " \t\n\\");
204 static int show_vfsmnt(struct seq_file *m, void *v)
206 struct vfsmount *mnt = v;
207 int err = 0;
208 static struct proc_fs_info {
209 int flag;
210 char *str;
211 } fs_info[] = {
212 { MS_SYNCHRONOUS, ",sync" },
213 { MS_DIRSYNC, ",dirsync" },
214 { MS_MANDLOCK, ",mand" },
215 { MS_NOATIME, ",noatime" },
216 { MS_NODIRATIME, ",nodiratime" },
217 { 0, NULL }
219 static struct proc_fs_info mnt_info[] = {
220 { MNT_NOSUID, ",nosuid" },
221 { MNT_NODEV, ",nodev" },
222 { MNT_NOEXEC, ",noexec" },
223 { 0, NULL }
225 struct proc_fs_info *fs_infop;
227 mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
228 seq_putc(m, ' ');
229 seq_path(m, mnt, mnt->mnt_root, " \t\n\\");
230 seq_putc(m, ' ');
231 mangle(m, mnt->mnt_sb->s_type->name);
232 seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? " ro" : " rw");
233 for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
234 if (mnt->mnt_sb->s_flags & fs_infop->flag)
235 seq_puts(m, fs_infop->str);
237 for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
238 if (mnt->mnt_flags & fs_infop->flag)
239 seq_puts(m, fs_infop->str);
241 if (mnt->mnt_sb->s_op->show_options)
242 err = mnt->mnt_sb->s_op->show_options(m, mnt);
243 seq_puts(m, " 0 0\n");
244 return err;
247 struct seq_operations mounts_op = {
248 .start = m_start,
249 .next = m_next,
250 .stop = m_stop,
251 .show = show_vfsmnt
255 * Doesn't take quota and stuff into account. IOW, in some cases it will
256 * give false negatives. The main reason why it's here is that we need
257 * a non-destructive way to look for easily umountable filesystems.
259 int may_umount(struct vfsmount *mnt)
261 if (atomic_read(&mnt->mnt_count) > 2)
262 return -EBUSY;
263 return 0;
266 EXPORT_SYMBOL(may_umount);
268 void umount_tree(struct vfsmount *mnt)
270 struct vfsmount *p;
271 LIST_HEAD(kill);
273 for (p = mnt; p; p = next_mnt(p, mnt)) {
274 list_del(&p->mnt_list);
275 list_add(&p->mnt_list, &kill);
278 while (!list_empty(&kill)) {
279 mnt = list_entry(kill.next, struct vfsmount, mnt_list);
280 list_del_init(&mnt->mnt_list);
281 if (mnt->mnt_parent == mnt) {
282 spin_unlock(&vfsmount_lock);
283 } else {
284 struct nameidata old_nd;
285 detach_mnt(mnt, &old_nd);
286 spin_unlock(&vfsmount_lock);
287 path_release(&old_nd);
289 mntput(mnt);
290 spin_lock(&vfsmount_lock);
294 static int do_umount(struct vfsmount *mnt, int flags)
296 struct super_block * sb = mnt->mnt_sb;
297 int retval;
299 retval = security_sb_umount(mnt, flags);
300 if (retval)
301 return retval;
304 * If we may have to abort operations to get out of this
305 * mount, and they will themselves hold resources we must
306 * allow the fs to do things. In the Unix tradition of
307 * 'Gee thats tricky lets do it in userspace' the umount_begin
308 * might fail to complete on the first run through as other tasks
309 * must return, and the like. Thats for the mount program to worry
310 * about for the moment.
313 lock_kernel();
314 if( (flags&MNT_FORCE) && sb->s_op->umount_begin)
315 sb->s_op->umount_begin(sb);
316 unlock_kernel();
319 * No sense to grab the lock for this test, but test itself looks
320 * somewhat bogus. Suggestions for better replacement?
321 * Ho-hum... In principle, we might treat that as umount + switch
322 * to rootfs. GC would eventually take care of the old vfsmount.
323 * Actually it makes sense, especially if rootfs would contain a
324 * /reboot - static binary that would close all descriptors and
325 * call reboot(9). Then init(8) could umount root and exec /reboot.
327 if (mnt == current->fs->rootmnt && !(flags & MNT_DETACH)) {
329 * Special case for "unmounting" root ...
330 * we just try to remount it readonly.
332 down_write(&sb->s_umount);
333 if (!(sb->s_flags & MS_RDONLY)) {
334 lock_kernel();
335 retval = do_remount_sb(sb, MS_RDONLY, 0, 0);
336 unlock_kernel();
338 up_write(&sb->s_umount);
339 return retval;
342 down_write(&current->namespace->sem);
343 spin_lock(&vfsmount_lock);
345 if (atomic_read(&sb->s_active) == 1) {
346 /* last instance - try to be smart */
347 spin_unlock(&vfsmount_lock);
348 lock_kernel();
349 DQUOT_OFF(sb);
350 acct_auto_close(sb);
351 unlock_kernel();
352 security_sb_umount_close(mnt);
353 spin_lock(&vfsmount_lock);
355 retval = -EBUSY;
356 if (atomic_read(&mnt->mnt_count) == 2 || flags & MNT_DETACH) {
357 if (!list_empty(&mnt->mnt_list))
358 umount_tree(mnt);
359 retval = 0;
361 spin_unlock(&vfsmount_lock);
362 if (retval)
363 security_sb_umount_busy(mnt);
364 up_write(&current->namespace->sem);
365 return retval;
369 * Now umount can handle mount points as well as block devices.
370 * This is important for filesystems which use unnamed block devices.
372 * We now support a flag for forced unmount like the other 'big iron'
373 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
376 asmlinkage long sys_umount(char __user * name, int flags)
378 struct nameidata nd;
379 int retval;
381 retval = __user_walk(name, LOOKUP_FOLLOW, &nd);
382 if (retval)
383 goto out;
384 retval = -EINVAL;
385 if (nd.dentry != nd.mnt->mnt_root)
386 goto dput_and_out;
387 if (!check_mnt(nd.mnt))
388 goto dput_and_out;
390 retval = -EPERM;
391 if (!capable(CAP_SYS_ADMIN))
392 goto dput_and_out;
394 retval = do_umount(nd.mnt, flags);
395 dput_and_out:
396 path_release(&nd);
397 out:
398 return retval;
402 * The 2.0 compatible umount. No flags.
405 asmlinkage long sys_oldumount(char __user * name)
407 return sys_umount(name,0);
410 static int mount_is_safe(struct nameidata *nd)
412 if (capable(CAP_SYS_ADMIN))
413 return 0;
414 return -EPERM;
415 #ifdef notyet
416 if (S_ISLNK(nd->dentry->d_inode->i_mode))
417 return -EPERM;
418 if (nd->dentry->d_inode->i_mode & S_ISVTX) {
419 if (current->uid != nd->dentry->d_inode->i_uid)
420 return -EPERM;
422 if (permission(nd->dentry->d_inode, MAY_WRITE, nd))
423 return -EPERM;
424 return 0;
425 #endif
428 static int
429 lives_below_in_same_fs(struct dentry *d, struct dentry *dentry)
431 while (1) {
432 if (d == dentry)
433 return 1;
434 if (d == NULL || d == d->d_parent)
435 return 0;
436 d = d->d_parent;
440 static struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry)
442 struct vfsmount *res, *p, *q, *r, *s;
443 struct list_head *h;
444 struct nameidata nd;
446 res = q = clone_mnt(mnt, dentry);
447 if (!q)
448 goto Enomem;
449 q->mnt_mountpoint = mnt->mnt_mountpoint;
451 p = mnt;
452 for (h = mnt->mnt_mounts.next; h != &mnt->mnt_mounts; h = h->next) {
453 r = list_entry(h, struct vfsmount, mnt_child);
454 if (!lives_below_in_same_fs(r->mnt_mountpoint, dentry))
455 continue;
457 for (s = r; s; s = next_mnt(s, r)) {
458 while (p != s->mnt_parent) {
459 p = p->mnt_parent;
460 q = q->mnt_parent;
462 p = s;
463 nd.mnt = q;
464 nd.dentry = p->mnt_mountpoint;
465 q = clone_mnt(p, p->mnt_root);
466 if (!q)
467 goto Enomem;
468 spin_lock(&vfsmount_lock);
469 list_add_tail(&q->mnt_list, &res->mnt_list);
470 attach_mnt(q, &nd);
471 spin_unlock(&vfsmount_lock);
474 return res;
475 Enomem:
476 if (res) {
477 spin_lock(&vfsmount_lock);
478 umount_tree(res);
479 spin_unlock(&vfsmount_lock);
481 return NULL;
484 static int graft_tree(struct vfsmount *mnt, struct nameidata *nd)
486 int err;
487 if (mnt->mnt_sb->s_flags & MS_NOUSER)
488 return -EINVAL;
490 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
491 S_ISDIR(mnt->mnt_root->d_inode->i_mode))
492 return -ENOTDIR;
494 err = -ENOENT;
495 down(&nd->dentry->d_inode->i_sem);
496 if (IS_DEADDIR(nd->dentry->d_inode))
497 goto out_unlock;
499 err = security_sb_check_sb(mnt, nd);
500 if (err)
501 goto out_unlock;
503 err = -ENOENT;
504 spin_lock(&vfsmount_lock);
505 if (IS_ROOT(nd->dentry) || !d_unhashed(nd->dentry)) {
506 struct list_head head;
508 attach_mnt(mnt, nd);
509 list_add_tail(&head, &mnt->mnt_list);
510 list_splice(&head, current->namespace->list.prev);
511 mntget(mnt);
512 err = 0;
514 spin_unlock(&vfsmount_lock);
515 out_unlock:
516 up(&nd->dentry->d_inode->i_sem);
517 if (!err)
518 security_sb_post_addmount(mnt, nd);
519 return err;
523 * do loopback mount.
525 static int do_loopback(struct nameidata *nd, char *old_name, int recurse)
527 struct nameidata old_nd;
528 struct vfsmount *mnt = NULL;
529 int err = mount_is_safe(nd);
530 if (err)
531 return err;
532 if (!old_name || !*old_name)
533 return -EINVAL;
534 err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
535 if (err)
536 return err;
538 down_write(&current->namespace->sem);
539 err = -EINVAL;
540 if (check_mnt(nd->mnt) && (!recurse || check_mnt(old_nd.mnt))) {
541 err = -ENOMEM;
542 if (recurse)
543 mnt = copy_tree(old_nd.mnt, old_nd.dentry);
544 else
545 mnt = clone_mnt(old_nd.mnt, old_nd.dentry);
548 if (mnt) {
549 err = graft_tree(mnt, nd);
550 if (err) {
551 spin_lock(&vfsmount_lock);
552 umount_tree(mnt);
553 spin_unlock(&vfsmount_lock);
554 } else
555 mntput(mnt);
558 up_write(&current->namespace->sem);
559 path_release(&old_nd);
560 return err;
564 * change filesystem flags. dir should be a physical root of filesystem.
565 * If you've mounted a non-root directory somewhere and want to do remount
566 * on it - tough luck.
569 static int do_remount(struct nameidata *nd,int flags,int mnt_flags,void *data)
571 int err;
572 struct super_block * sb = nd->mnt->mnt_sb;
574 if (!capable(CAP_SYS_ADMIN))
575 return -EPERM;
577 if (!check_mnt(nd->mnt))
578 return -EINVAL;
580 if (nd->dentry != nd->mnt->mnt_root)
581 return -EINVAL;
583 down_write(&sb->s_umount);
584 err = do_remount_sb(sb, flags, data, 0);
585 if (!err)
586 nd->mnt->mnt_flags=mnt_flags;
587 up_write(&sb->s_umount);
588 if (!err)
589 security_sb_post_remount(nd->mnt, flags, data);
590 return err;
593 static int do_move_mount(struct nameidata *nd, char *old_name)
595 struct nameidata old_nd, parent_nd;
596 struct vfsmount *p;
597 int err = 0;
598 if (!capable(CAP_SYS_ADMIN))
599 return -EPERM;
600 if (!old_name || !*old_name)
601 return -EINVAL;
602 err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
603 if (err)
604 return err;
606 down_write(&current->namespace->sem);
607 while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
609 err = -EINVAL;
610 if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt))
611 goto out;
613 err = -ENOENT;
614 down(&nd->dentry->d_inode->i_sem);
615 if (IS_DEADDIR(nd->dentry->d_inode))
616 goto out1;
618 spin_lock(&vfsmount_lock);
619 if (!IS_ROOT(nd->dentry) && d_unhashed(nd->dentry))
620 goto out2;
622 err = -EINVAL;
623 if (old_nd.dentry != old_nd.mnt->mnt_root)
624 goto out2;
626 if (old_nd.mnt == old_nd.mnt->mnt_parent)
627 goto out2;
629 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
630 S_ISDIR(old_nd.dentry->d_inode->i_mode))
631 goto out2;
633 err = -ELOOP;
634 for (p = nd->mnt; p->mnt_parent!=p; p = p->mnt_parent)
635 if (p == old_nd.mnt)
636 goto out2;
637 err = 0;
639 detach_mnt(old_nd.mnt, &parent_nd);
640 attach_mnt(old_nd.mnt, nd);
641 out2:
642 spin_unlock(&vfsmount_lock);
643 out1:
644 up(&nd->dentry->d_inode->i_sem);
645 out:
646 up_write(&current->namespace->sem);
647 if (!err)
648 path_release(&parent_nd);
649 path_release(&old_nd);
650 return err;
653 static int do_add_mount(struct nameidata *nd, char *type, int flags,
654 int mnt_flags, char *name, void *data)
656 struct vfsmount *mnt;
657 int err;
659 if (!type || !memchr(type, 0, PAGE_SIZE))
660 return -EINVAL;
662 /* we need capabilities... */
663 if (!capable(CAP_SYS_ADMIN))
664 return -EPERM;
666 mnt = do_kern_mount(type, flags, name, data);
667 err = PTR_ERR(mnt);
668 if (IS_ERR(mnt))
669 goto out;
671 down_write(&current->namespace->sem);
672 /* Something was mounted here while we slept */
673 while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
675 err = -EINVAL;
676 if (!check_mnt(nd->mnt))
677 goto unlock;
679 /* Refuse the same filesystem on the same mount point */
680 err = -EBUSY;
681 if (nd->mnt->mnt_sb == mnt->mnt_sb && nd->mnt->mnt_root == nd->dentry)
682 goto unlock;
684 err = -EINVAL;
685 if (S_ISLNK(mnt->mnt_root->d_inode->i_mode))
686 goto unlock;
688 mnt->mnt_flags = mnt_flags;
689 err = graft_tree(mnt, nd);
690 unlock:
691 up_write(&current->namespace->sem);
692 mntput(mnt);
693 out:
694 return err;
697 int copy_mount_options (const void __user *data, unsigned long *where)
699 int i;
700 unsigned long page;
701 unsigned long size;
703 *where = 0;
704 if (!data)
705 return 0;
707 if (!(page = __get_free_page(GFP_KERNEL)))
708 return -ENOMEM;
710 /* We only care that *some* data at the address the user
711 * gave us is valid. Just in case, we'll zero
712 * the remainder of the page.
714 /* copy_from_user cannot cross TASK_SIZE ! */
715 size = TASK_SIZE - (unsigned long)data;
716 if (size > PAGE_SIZE)
717 size = PAGE_SIZE;
719 i = size - copy_from_user((void *)page, data, size);
720 if (!i) {
721 free_page(page);
722 return -EFAULT;
724 if (i != PAGE_SIZE)
725 memset((char *)page + i, 0, PAGE_SIZE - i);
726 *where = page;
727 return 0;
731 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
732 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
734 * data is a (void *) that can point to any structure up to
735 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
736 * information (or be NULL).
738 * Pre-0.97 versions of mount() didn't have a flags word.
739 * When the flags word was introduced its top half was required
740 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
741 * Therefore, if this magic number is present, it carries no information
742 * and must be discarded.
744 long do_mount(char * dev_name, char * dir_name, char *type_page,
745 unsigned long flags, void *data_page)
747 struct nameidata nd;
748 int retval = 0;
749 int mnt_flags = 0;
751 /* Discard magic */
752 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
753 flags &= ~MS_MGC_MSK;
755 /* Basic sanity checks */
757 if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
758 return -EINVAL;
759 if (dev_name && !memchr(dev_name, 0, PAGE_SIZE))
760 return -EINVAL;
762 if (data_page)
763 ((char *)data_page)[PAGE_SIZE - 1] = 0;
765 /* Separate the per-mountpoint flags */
766 if (flags & MS_NOSUID)
767 mnt_flags |= MNT_NOSUID;
768 if (flags & MS_NODEV)
769 mnt_flags |= MNT_NODEV;
770 if (flags & MS_NOEXEC)
771 mnt_flags |= MNT_NOEXEC;
772 flags &= ~(MS_NOSUID|MS_NOEXEC|MS_NODEV);
774 /* ... and get the mountpoint */
775 retval = path_lookup(dir_name, LOOKUP_FOLLOW, &nd);
776 if (retval)
777 return retval;
779 retval = security_sb_mount(dev_name, &nd, type_page, flags, data_page);
780 if (retval)
781 goto dput_out;
783 if (flags & MS_REMOUNT)
784 retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags,
785 data_page);
786 else if (flags & MS_BIND)
787 retval = do_loopback(&nd, dev_name, flags & MS_REC);
788 else if (flags & MS_MOVE)
789 retval = do_move_mount(&nd, dev_name);
790 else
791 retval = do_add_mount(&nd, type_page, flags, mnt_flags,
792 dev_name, data_page);
793 dput_out:
794 path_release(&nd);
795 return retval;
798 int copy_namespace(int flags, struct task_struct *tsk)
800 struct namespace *namespace = tsk->namespace;
801 struct namespace *new_ns;
802 struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL;
803 struct fs_struct *fs = tsk->fs;
805 if (!namespace)
806 return 0;
808 get_namespace(namespace);
810 if (!(flags & CLONE_NEWNS))
811 return 0;
813 if (!capable(CAP_SYS_ADMIN)) {
814 put_namespace(namespace);
815 return -EPERM;
818 new_ns = kmalloc(sizeof(struct namespace), GFP_KERNEL);
819 if (!new_ns)
820 goto out;
822 atomic_set(&new_ns->count, 1);
823 init_rwsem(&new_ns->sem);
824 INIT_LIST_HEAD(&new_ns->list);
826 down_write(&tsk->namespace->sem);
827 /* First pass: copy the tree topology */
828 new_ns->root = copy_tree(namespace->root, namespace->root->mnt_root);
829 if (!new_ns->root) {
830 up_write(&tsk->namespace->sem);
831 kfree(new_ns);
832 goto out;
834 spin_lock(&vfsmount_lock);
835 list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
836 spin_unlock(&vfsmount_lock);
838 /* Second pass: switch the tsk->fs->* elements */
839 if (fs) {
840 struct vfsmount *p, *q;
841 write_lock(&fs->lock);
843 p = namespace->root;
844 q = new_ns->root;
845 while (p) {
846 if (p == fs->rootmnt) {
847 rootmnt = p;
848 fs->rootmnt = mntget(q);
850 if (p == fs->pwdmnt) {
851 pwdmnt = p;
852 fs->pwdmnt = mntget(q);
854 if (p == fs->altrootmnt) {
855 altrootmnt = p;
856 fs->altrootmnt = mntget(q);
858 p = next_mnt(p, namespace->root);
859 q = next_mnt(q, new_ns->root);
861 write_unlock(&fs->lock);
863 up_write(&tsk->namespace->sem);
865 tsk->namespace = new_ns;
867 if (rootmnt)
868 mntput(rootmnt);
869 if (pwdmnt)
870 mntput(pwdmnt);
871 if (altrootmnt)
872 mntput(altrootmnt);
874 put_namespace(namespace);
875 return 0;
877 out:
878 put_namespace(namespace);
879 return -ENOMEM;
882 asmlinkage long sys_mount(char __user * dev_name, char __user * dir_name,
883 char __user * type, unsigned long flags,
884 void __user * data)
886 int retval;
887 unsigned long data_page;
888 unsigned long type_page;
889 unsigned long dev_page;
890 char *dir_page;
892 retval = copy_mount_options (type, &type_page);
893 if (retval < 0)
894 return retval;
896 dir_page = getname(dir_name);
897 retval = PTR_ERR(dir_page);
898 if (IS_ERR(dir_page))
899 goto out1;
901 retval = copy_mount_options (dev_name, &dev_page);
902 if (retval < 0)
903 goto out2;
905 retval = copy_mount_options (data, &data_page);
906 if (retval < 0)
907 goto out3;
909 lock_kernel();
910 retval = do_mount((char*)dev_page, dir_page, (char*)type_page,
911 flags, (void*)data_page);
912 unlock_kernel();
913 free_page(data_page);
915 out3:
916 free_page(dev_page);
917 out2:
918 putname(dir_page);
919 out1:
920 free_page(type_page);
921 return retval;
925 * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
926 * It can block. Requires the big lock held.
928 void set_fs_root(struct fs_struct *fs, struct vfsmount *mnt,
929 struct dentry *dentry)
931 struct dentry *old_root;
932 struct vfsmount *old_rootmnt;
933 write_lock(&fs->lock);
934 old_root = fs->root;
935 old_rootmnt = fs->rootmnt;
936 fs->rootmnt = mntget(mnt);
937 fs->root = dget(dentry);
938 write_unlock(&fs->lock);
939 if (old_root) {
940 dput(old_root);
941 mntput(old_rootmnt);
945 EXPORT_SYMBOL(set_fs_root);
948 * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
949 * It can block. Requires the big lock held.
951 void set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
952 struct dentry *dentry)
954 struct dentry *old_pwd;
955 struct vfsmount *old_pwdmnt;
957 write_lock(&fs->lock);
958 old_pwd = fs->pwd;
959 old_pwdmnt = fs->pwdmnt;
960 fs->pwdmnt = mntget(mnt);
961 fs->pwd = dget(dentry);
962 write_unlock(&fs->lock);
964 if (old_pwd) {
965 dput(old_pwd);
966 mntput(old_pwdmnt);
970 EXPORT_SYMBOL(set_fs_pwd);
972 static void chroot_fs_refs(struct nameidata *old_nd, struct nameidata *new_nd)
974 struct task_struct *g, *p;
975 struct fs_struct *fs;
977 read_lock(&tasklist_lock);
978 do_each_thread(g, p) {
979 task_lock(p);
980 fs = p->fs;
981 if (fs) {
982 atomic_inc(&fs->count);
983 task_unlock(p);
984 if (fs->root==old_nd->dentry&&fs->rootmnt==old_nd->mnt)
985 set_fs_root(fs, new_nd->mnt, new_nd->dentry);
986 if (fs->pwd==old_nd->dentry&&fs->pwdmnt==old_nd->mnt)
987 set_fs_pwd(fs, new_nd->mnt, new_nd->dentry);
988 put_fs_struct(fs);
989 } else
990 task_unlock(p);
991 } while_each_thread(g, p);
992 read_unlock(&tasklist_lock);
996 * Moves the current root to put_root, and sets root/cwd of all processes
997 * which had them on the old root to new_root.
999 * Note:
1000 * - we don't move root/cwd if they are not at the root (reason: if something
1001 * cared enough to change them, it's probably wrong to force them elsewhere)
1002 * - it's okay to pick a root that isn't the root of a file system, e.g.
1003 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
1004 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
1005 * first.
1008 asmlinkage long sys_pivot_root(const char __user *new_root, const char __user *put_old)
1010 struct vfsmount *tmp;
1011 struct nameidata new_nd, old_nd, parent_nd, root_parent, user_nd;
1012 int error;
1014 if (!capable(CAP_SYS_ADMIN))
1015 return -EPERM;
1017 lock_kernel();
1019 error = __user_walk(new_root, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &new_nd);
1020 if (error)
1021 goto out0;
1022 error = -EINVAL;
1023 if (!check_mnt(new_nd.mnt))
1024 goto out1;
1026 error = __user_walk(put_old, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &old_nd);
1027 if (error)
1028 goto out1;
1030 error = security_sb_pivotroot(&old_nd, &new_nd);
1031 if (error) {
1032 path_release(&old_nd);
1033 goto out1;
1036 read_lock(&current->fs->lock);
1037 user_nd.mnt = mntget(current->fs->rootmnt);
1038 user_nd.dentry = dget(current->fs->root);
1039 read_unlock(&current->fs->lock);
1040 down_write(&current->namespace->sem);
1041 down(&old_nd.dentry->d_inode->i_sem);
1042 error = -EINVAL;
1043 if (!check_mnt(user_nd.mnt))
1044 goto out2;
1045 error = -ENOENT;
1046 if (IS_DEADDIR(new_nd.dentry->d_inode))
1047 goto out2;
1048 if (d_unhashed(new_nd.dentry) && !IS_ROOT(new_nd.dentry))
1049 goto out2;
1050 if (d_unhashed(old_nd.dentry) && !IS_ROOT(old_nd.dentry))
1051 goto out2;
1052 error = -EBUSY;
1053 if (new_nd.mnt == user_nd.mnt || old_nd.mnt == user_nd.mnt)
1054 goto out2; /* loop */
1055 error = -EINVAL;
1056 if (user_nd.mnt->mnt_root != user_nd.dentry)
1057 goto out2;
1058 if (new_nd.mnt->mnt_root != new_nd.dentry)
1059 goto out2; /* not a mountpoint */
1060 tmp = old_nd.mnt; /* make sure we can reach put_old from new_root */
1061 spin_lock(&vfsmount_lock);
1062 if (tmp != new_nd.mnt) {
1063 for (;;) {
1064 if (tmp->mnt_parent == tmp)
1065 goto out3;
1066 if (tmp->mnt_parent == new_nd.mnt)
1067 break;
1068 tmp = tmp->mnt_parent;
1070 if (!is_subdir(tmp->mnt_mountpoint, new_nd.dentry))
1071 goto out3;
1072 } else if (!is_subdir(old_nd.dentry, new_nd.dentry))
1073 goto out3;
1074 detach_mnt(new_nd.mnt, &parent_nd);
1075 detach_mnt(user_nd.mnt, &root_parent);
1076 attach_mnt(user_nd.mnt, &old_nd);
1077 attach_mnt(new_nd.mnt, &root_parent);
1078 spin_unlock(&vfsmount_lock);
1079 chroot_fs_refs(&user_nd, &new_nd);
1080 security_sb_post_pivotroot(&user_nd, &new_nd);
1081 error = 0;
1082 path_release(&root_parent);
1083 path_release(&parent_nd);
1084 out2:
1085 up(&old_nd.dentry->d_inode->i_sem);
1086 up_write(&current->namespace->sem);
1087 path_release(&user_nd);
1088 path_release(&old_nd);
1089 out1:
1090 path_release(&new_nd);
1091 out0:
1092 unlock_kernel();
1093 return error;
1094 out3:
1095 spin_unlock(&vfsmount_lock);
1096 goto out2;
1099 static void __init init_mount_tree(void)
1101 struct vfsmount *mnt;
1102 struct namespace *namespace;
1103 struct task_struct *g, *p;
1105 mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
1106 if (IS_ERR(mnt))
1107 panic("Can't create rootfs");
1108 namespace = kmalloc(sizeof(*namespace), GFP_KERNEL);
1109 if (!namespace)
1110 panic("Can't allocate initial namespace");
1111 atomic_set(&namespace->count, 1);
1112 INIT_LIST_HEAD(&namespace->list);
1113 init_rwsem(&namespace->sem);
1114 list_add(&mnt->mnt_list, &namespace->list);
1115 namespace->root = mnt;
1117 init_task.namespace = namespace;
1118 read_lock(&tasklist_lock);
1119 do_each_thread(g, p) {
1120 get_namespace(namespace);
1121 p->namespace = namespace;
1122 } while_each_thread(g, p);
1123 read_unlock(&tasklist_lock);
1125 set_fs_pwd(current->fs, namespace->root, namespace->root->mnt_root);
1126 set_fs_root(current->fs, namespace->root, namespace->root->mnt_root);
1129 void __init mnt_init(unsigned long mempages)
1131 struct list_head *d;
1132 unsigned long order;
1133 unsigned int nr_hash;
1134 int i;
1136 mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
1137 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
1138 if (!mnt_cache)
1139 panic("Cannot create vfsmount cache");
1141 order = 0;
1142 mount_hashtable = (struct list_head *)
1143 __get_free_pages(GFP_ATOMIC, order);
1145 if (!mount_hashtable)
1146 panic("Failed to allocate mount hash table\n");
1149 * Find the power-of-two list-heads that can fit into the allocation..
1150 * We don't guarantee that "sizeof(struct list_head)" is necessarily
1151 * a power-of-two.
1153 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct list_head);
1154 hash_bits = 0;
1155 do {
1156 hash_bits++;
1157 } while ((nr_hash >> hash_bits) != 0);
1158 hash_bits--;
1161 * Re-calculate the actual number of entries and the mask
1162 * from the number of bits we can fit.
1164 nr_hash = 1UL << hash_bits;
1165 hash_mask = nr_hash-1;
1167 printk("Mount-cache hash table entries: %d (order: %ld, %ld bytes)\n",
1168 nr_hash, order, (PAGE_SIZE << order));
1170 /* And initialize the newly allocated array */
1171 d = mount_hashtable;
1172 i = nr_hash;
1173 do {
1174 INIT_LIST_HEAD(d);
1175 d++;
1176 i--;
1177 } while (i);
1178 sysfs_init();
1179 init_rootfs();
1180 init_mount_tree();
1183 void __put_namespace(struct namespace *namespace)
1185 down_write(&namespace->sem);
1186 spin_lock(&vfsmount_lock);
1187 umount_tree(namespace->root);
1188 spin_unlock(&vfsmount_lock);
1189 up_write(&namespace->sem);
1190 kfree(namespace);