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.
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>
25 #include <asm/unistd.h>
27 extern int __init init_rootfs(void);
30 extern int __init sysfs_init(void);
32 static inline int sysfs_init(void)
38 /* spinlock for vfsmount related operations, inplace of dcache_lock */
39 spinlock_t vfsmount_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
41 static struct list_head *mount_hashtable;
42 static int hash_mask, hash_bits;
43 static kmem_cache_t *mnt_cache;
45 static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
47 unsigned long tmp = ((unsigned long) mnt / L1_CACHE_BYTES);
48 tmp += ((unsigned long) dentry / L1_CACHE_BYTES);
49 tmp = tmp + (tmp >> hash_bits);
50 return tmp & hash_mask;
53 struct vfsmount *alloc_vfsmnt(const char *name)
55 struct vfsmount *mnt = kmem_cache_alloc(mnt_cache, GFP_KERNEL);
57 memset(mnt, 0, sizeof(struct vfsmount));
58 atomic_set(&mnt->mnt_count,1);
59 INIT_LIST_HEAD(&mnt->mnt_hash);
60 INIT_LIST_HEAD(&mnt->mnt_child);
61 INIT_LIST_HEAD(&mnt->mnt_mounts);
62 INIT_LIST_HEAD(&mnt->mnt_list);
64 int size = strlen(name)+1;
65 char *newname = kmalloc(size, GFP_KERNEL);
67 memcpy(newname, name, size);
68 mnt->mnt_devname = newname;
75 void free_vfsmnt(struct vfsmount *mnt)
77 kfree(mnt->mnt_devname);
78 kmem_cache_free(mnt_cache, mnt);
82 * Now, lookup_mnt increments the ref count before returning
83 * the vfsmount struct.
85 struct vfsmount *lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
87 struct list_head * head = mount_hashtable + hash(mnt, dentry);
88 struct list_head * tmp = head;
89 struct vfsmount *p, *found = NULL;
91 spin_lock(&vfsmount_lock);
97 p = list_entry(tmp, struct vfsmount, mnt_hash);
98 if (p->mnt_parent == mnt && p->mnt_mountpoint == dentry) {
103 spin_unlock(&vfsmount_lock);
107 EXPORT_SYMBOL(lookup_mnt);
109 static int check_mnt(struct vfsmount *mnt)
111 spin_lock(&vfsmount_lock);
112 while (mnt->mnt_parent != mnt)
113 mnt = mnt->mnt_parent;
114 spin_unlock(&vfsmount_lock);
115 return mnt == current->namespace->root;
118 static void detach_mnt(struct vfsmount *mnt, struct nameidata *old_nd)
120 old_nd->dentry = mnt->mnt_mountpoint;
121 old_nd->mnt = mnt->mnt_parent;
122 mnt->mnt_parent = mnt;
123 mnt->mnt_mountpoint = mnt->mnt_root;
124 list_del_init(&mnt->mnt_child);
125 list_del_init(&mnt->mnt_hash);
126 old_nd->dentry->d_mounted--;
129 static void attach_mnt(struct vfsmount *mnt, struct nameidata *nd)
131 mnt->mnt_parent = mntget(nd->mnt);
132 mnt->mnt_mountpoint = dget(nd->dentry);
133 list_add(&mnt->mnt_hash, mount_hashtable+hash(nd->mnt, nd->dentry));
134 list_add_tail(&mnt->mnt_child, &nd->mnt->mnt_mounts);
135 nd->dentry->d_mounted++;
138 static struct vfsmount *next_mnt(struct vfsmount *p, struct vfsmount *root)
140 struct list_head *next = p->mnt_mounts.next;
141 if (next == &p->mnt_mounts) {
145 next = p->mnt_child.next;
146 if (next != &p->mnt_parent->mnt_mounts)
151 return list_entry(next, struct vfsmount, mnt_child);
154 static struct vfsmount *
155 clone_mnt(struct vfsmount *old, struct dentry *root)
157 struct super_block *sb = old->mnt_sb;
158 struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname);
161 mnt->mnt_flags = old->mnt_flags;
162 atomic_inc(&sb->s_active);
164 mnt->mnt_root = dget(root);
165 mnt->mnt_mountpoint = mnt->mnt_root;
166 mnt->mnt_parent = mnt;
171 void __mntput(struct vfsmount *mnt)
173 struct super_block *sb = mnt->mnt_sb;
176 deactivate_super(sb);
179 EXPORT_SYMBOL(__mntput);
182 static void *m_start(struct seq_file *m, loff_t *pos)
184 struct namespace *n = m->private;
189 list_for_each(p, &n->list)
191 return list_entry(p, struct vfsmount, mnt_list);
195 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
197 struct namespace *n = m->private;
198 struct list_head *p = ((struct vfsmount *)v)->mnt_list.next;
200 return p==&n->list ? NULL : list_entry(p, struct vfsmount, mnt_list);
203 static void m_stop(struct seq_file *m, void *v)
205 struct namespace *n = m->private;
209 static inline void mangle(struct seq_file *m, const char *s)
211 seq_escape(m, s, " \t\n\\");
214 static int show_vfsmnt(struct seq_file *m, void *v)
216 struct vfsmount *mnt = v;
218 static struct proc_fs_info {
222 { MS_SYNCHRONOUS, ",sync" },
223 { MS_DIRSYNC, ",dirsync" },
224 { MS_MANDLOCK, ",mand" },
225 { MS_NOATIME, ",noatime" },
226 { MS_NODIRATIME, ",nodiratime" },
229 static struct proc_fs_info mnt_info[] = {
230 { MNT_NOSUID, ",nosuid" },
231 { MNT_NODEV, ",nodev" },
232 { MNT_NOEXEC, ",noexec" },
235 struct proc_fs_info *fs_infop;
237 if (vx_flags(VXF_HIDE_MOUNT, 0))
240 mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
242 seq_path(m, mnt, mnt->mnt_root, " \t\n\\");
244 mangle(m, mnt->mnt_sb->s_type->name);
245 seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? " ro" : " rw");
246 for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
247 if (mnt->mnt_sb->s_flags & fs_infop->flag)
248 seq_puts(m, fs_infop->str);
250 for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
251 if (mnt->mnt_flags & fs_infop->flag)
252 seq_puts(m, fs_infop->str);
254 if (mnt->mnt_sb->s_op->show_options)
255 err = mnt->mnt_sb->s_op->show_options(m, mnt);
256 seq_puts(m, " 0 0\n");
260 struct seq_operations mounts_op = {
268 * may_umount_tree - check if a mount tree is busy
269 * @mnt: root of mount tree
271 * This is called to check if a tree of mounts has any
272 * open files, pwds, chroots or sub mounts that are
275 int may_umount_tree(struct vfsmount *mnt)
277 struct list_head *next;
278 struct vfsmount *this_parent = mnt;
282 spin_lock(&vfsmount_lock);
283 actual_refs = atomic_read(&mnt->mnt_count);
286 next = this_parent->mnt_mounts.next;
288 while (next != &this_parent->mnt_mounts) {
289 struct vfsmount *p = list_entry(next, struct vfsmount, mnt_child);
293 actual_refs += atomic_read(&p->mnt_count);
296 if (!list_empty(&p->mnt_mounts)) {
302 if (this_parent != mnt) {
303 next = this_parent->mnt_child.next;
304 this_parent = this_parent->mnt_parent;
307 spin_unlock(&vfsmount_lock);
309 if (actual_refs > minimum_refs)
315 EXPORT_SYMBOL(may_umount_tree);
318 * may_umount - check if a mount point is busy
319 * @mnt: root of mount
321 * This is called to check if a mount point has any
322 * open files, pwds, chroots or sub mounts. If the
323 * mount has sub mounts this will return busy
324 * regardless of whether the sub mounts are busy.
326 * Doesn't take quota and stuff into account. IOW, in some cases it will
327 * give false negatives. The main reason why it's here is that we need
328 * a non-destructive way to look for easily umountable filesystems.
330 int may_umount(struct vfsmount *mnt)
332 if (atomic_read(&mnt->mnt_count) > 2)
337 EXPORT_SYMBOL(may_umount);
339 static inline void __umount_tree(struct vfsmount *mnt, struct list_head *kill)
341 while (!list_empty(kill)) {
342 mnt = list_entry(kill->next, struct vfsmount, mnt_list);
343 list_del_init(&mnt->mnt_list);
344 if (mnt->mnt_parent == mnt) {
345 spin_unlock(&vfsmount_lock);
347 struct nameidata old_nd;
348 detach_mnt(mnt, &old_nd);
349 spin_unlock(&vfsmount_lock);
350 path_release(&old_nd);
353 spin_lock(&vfsmount_lock);
357 void umount_tree(struct vfsmount *mnt)
362 for (p = mnt; p; p = next_mnt(p, mnt)) {
363 list_del(&p->mnt_list);
364 list_add(&p->mnt_list, &kill);
366 __umount_tree(mnt, &kill);
369 void umount_unused(struct vfsmount *mnt, struct fs_struct *fs)
374 for (p = mnt; p; p = next_mnt(p, mnt)) {
375 if (p == fs->rootmnt || p == fs->pwdmnt)
377 list_del(&p->mnt_list);
378 list_add(&p->mnt_list, &kill);
380 __umount_tree(mnt, &kill);
383 static int do_umount(struct vfsmount *mnt, int flags)
385 struct super_block * sb = mnt->mnt_sb;
388 retval = security_sb_umount(mnt, flags);
393 * If we may have to abort operations to get out of this
394 * mount, and they will themselves hold resources we must
395 * allow the fs to do things. In the Unix tradition of
396 * 'Gee thats tricky lets do it in userspace' the umount_begin
397 * might fail to complete on the first run through as other tasks
398 * must return, and the like. Thats for the mount program to worry
399 * about for the moment.
403 if( (flags&MNT_FORCE) && sb->s_op->umount_begin)
404 sb->s_op->umount_begin(sb);
408 * No sense to grab the lock for this test, but test itself looks
409 * somewhat bogus. Suggestions for better replacement?
410 * Ho-hum... In principle, we might treat that as umount + switch
411 * to rootfs. GC would eventually take care of the old vfsmount.
412 * Actually it makes sense, especially if rootfs would contain a
413 * /reboot - static binary that would close all descriptors and
414 * call reboot(9). Then init(8) could umount root and exec /reboot.
416 if (mnt == current->fs->rootmnt && !(flags & MNT_DETACH)) {
418 * Special case for "unmounting" root ...
419 * we just try to remount it readonly.
421 down_write(&sb->s_umount);
422 if (!(sb->s_flags & MS_RDONLY)) {
424 retval = do_remount_sb(sb, MS_RDONLY, 0, 0);
427 up_write(&sb->s_umount);
431 down_write(¤t->namespace->sem);
432 spin_lock(&vfsmount_lock);
434 if (atomic_read(&sb->s_active) == 1) {
435 /* last instance - try to be smart */
436 spin_unlock(&vfsmount_lock);
441 security_sb_umount_close(mnt);
442 spin_lock(&vfsmount_lock);
445 if (atomic_read(&mnt->mnt_count) == 2 || flags & MNT_DETACH) {
446 if (!list_empty(&mnt->mnt_list))
450 spin_unlock(&vfsmount_lock);
452 security_sb_umount_busy(mnt);
453 up_write(¤t->namespace->sem);
458 * Now umount can handle mount points as well as block devices.
459 * This is important for filesystems which use unnamed block devices.
461 * We now support a flag for forced unmount like the other 'big iron'
462 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
465 asmlinkage long sys_umount(char __user * name, int flags)
470 retval = __user_walk(name, LOOKUP_FOLLOW, &nd);
474 if (nd.dentry != nd.mnt->mnt_root)
476 if (!check_mnt(nd.mnt))
480 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SECURE_MOUNT))
483 retval = do_umount(nd.mnt, flags);
490 #ifdef __ARCH_WANT_SYS_OLDUMOUNT
493 * The 2.0 compatible umount. No flags.
496 asmlinkage long sys_oldumount(char __user * name)
498 return sys_umount(name,0);
503 static int mount_is_safe(struct nameidata *nd)
505 if (capable(CAP_SYS_ADMIN))
507 if (vx_ccaps(VXC_SECURE_MOUNT))
511 if (S_ISLNK(nd->dentry->d_inode->i_mode))
513 if (nd->dentry->d_inode->i_mode & S_ISVTX) {
514 if (current->uid != nd->dentry->d_inode->i_uid)
517 if (permission(nd->dentry->d_inode, MAY_WRITE, nd))
524 lives_below_in_same_fs(struct dentry *d, struct dentry *dentry)
529 if (d == NULL || d == d->d_parent)
535 static struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry)
537 struct vfsmount *res, *p, *q, *r, *s;
541 res = q = clone_mnt(mnt, dentry);
544 q->mnt_mountpoint = mnt->mnt_mountpoint;
547 for (h = mnt->mnt_mounts.next; h != &mnt->mnt_mounts; h = h->next) {
548 r = list_entry(h, struct vfsmount, mnt_child);
549 if (!lives_below_in_same_fs(r->mnt_mountpoint, dentry))
552 for (s = r; s; s = next_mnt(s, r)) {
553 while (p != s->mnt_parent) {
559 nd.dentry = p->mnt_mountpoint;
560 q = clone_mnt(p, p->mnt_root);
563 spin_lock(&vfsmount_lock);
564 list_add_tail(&q->mnt_list, &res->mnt_list);
566 spin_unlock(&vfsmount_lock);
572 spin_lock(&vfsmount_lock);
574 spin_unlock(&vfsmount_lock);
579 static int graft_tree(struct vfsmount *mnt, struct nameidata *nd)
582 if (mnt->mnt_sb->s_flags & MS_NOUSER)
585 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
586 S_ISDIR(mnt->mnt_root->d_inode->i_mode))
590 down(&nd->dentry->d_inode->i_sem);
591 if (IS_DEADDIR(nd->dentry->d_inode))
594 err = security_sb_check_sb(mnt, nd);
599 spin_lock(&vfsmount_lock);
600 if (IS_ROOT(nd->dentry) || !d_unhashed(nd->dentry)) {
601 struct list_head head;
604 list_add_tail(&head, &mnt->mnt_list);
605 list_splice(&head, current->namespace->list.prev);
609 spin_unlock(&vfsmount_lock);
611 up(&nd->dentry->d_inode->i_sem);
613 security_sb_post_addmount(mnt, nd);
620 static int do_loopback(struct nameidata *nd, char *old_name, int recurse)
622 struct nameidata old_nd;
623 struct vfsmount *mnt = NULL;
624 int err = mount_is_safe(nd);
627 if (!old_name || !*old_name)
629 err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
633 down_write(¤t->namespace->sem);
635 if (check_mnt(nd->mnt) && (!recurse || check_mnt(old_nd.mnt))) {
638 mnt = copy_tree(old_nd.mnt, old_nd.dentry);
640 mnt = clone_mnt(old_nd.mnt, old_nd.dentry);
644 err = graft_tree(mnt, nd);
646 spin_lock(&vfsmount_lock);
648 spin_unlock(&vfsmount_lock);
653 up_write(¤t->namespace->sem);
654 path_release(&old_nd);
659 * change filesystem flags. dir should be a physical root of filesystem.
660 * If you've mounted a non-root directory somewhere and want to do remount
661 * on it - tough luck.
664 static int do_remount(struct nameidata *nd,int flags,int mnt_flags,void *data)
667 struct super_block * sb = nd->mnt->mnt_sb;
669 if (!capable(CAP_SYS_ADMIN))
672 if (!check_mnt(nd->mnt))
675 if (nd->dentry != nd->mnt->mnt_root)
678 down_write(&sb->s_umount);
679 err = do_remount_sb(sb, flags, data, 0);
681 nd->mnt->mnt_flags=mnt_flags;
682 up_write(&sb->s_umount);
684 security_sb_post_remount(nd->mnt, flags, data);
688 static int do_move_mount(struct nameidata *nd, char *old_name)
690 struct nameidata old_nd, parent_nd;
693 if (!capable(CAP_SYS_ADMIN))
695 if (!old_name || !*old_name)
697 err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
701 down_write(¤t->namespace->sem);
702 while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
705 if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt))
709 down(&nd->dentry->d_inode->i_sem);
710 if (IS_DEADDIR(nd->dentry->d_inode))
713 spin_lock(&vfsmount_lock);
714 if (!IS_ROOT(nd->dentry) && d_unhashed(nd->dentry))
718 if (old_nd.dentry != old_nd.mnt->mnt_root)
721 if (old_nd.mnt == old_nd.mnt->mnt_parent)
724 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
725 S_ISDIR(old_nd.dentry->d_inode->i_mode))
729 for (p = nd->mnt; p->mnt_parent!=p; p = p->mnt_parent)
734 detach_mnt(old_nd.mnt, &parent_nd);
735 attach_mnt(old_nd.mnt, nd);
737 spin_unlock(&vfsmount_lock);
739 up(&nd->dentry->d_inode->i_sem);
741 up_write(¤t->namespace->sem);
743 path_release(&parent_nd);
744 path_release(&old_nd);
748 static int do_add_mount(struct nameidata *nd, char *type, int flags,
749 int mnt_flags, char *name, void *data)
751 struct vfsmount *mnt;
754 if (!type || !memchr(type, 0, PAGE_SIZE))
757 /* we need capabilities... */
758 if (!capable(CAP_SYS_ADMIN))
761 mnt = do_kern_mount(type, flags, name, data);
766 down_write(¤t->namespace->sem);
767 /* Something was mounted here while we slept */
768 while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
771 if (!check_mnt(nd->mnt))
774 /* Refuse the same filesystem on the same mount point */
776 if (nd->mnt->mnt_sb == mnt->mnt_sb && nd->mnt->mnt_root == nd->dentry)
780 if (S_ISLNK(mnt->mnt_root->d_inode->i_mode))
783 mnt->mnt_flags = mnt_flags;
784 err = graft_tree(mnt, nd);
786 up_write(¤t->namespace->sem);
792 int copy_mount_options (const void __user *data, unsigned long *where)
802 if (!(page = __get_free_page(GFP_KERNEL)))
805 /* We only care that *some* data at the address the user
806 * gave us is valid. Just in case, we'll zero
807 * the remainder of the page.
809 /* copy_from_user cannot cross TASK_SIZE ! */
810 size = TASK_SIZE - (unsigned long)data;
811 if (size > PAGE_SIZE)
814 i = size - copy_from_user((void *)page, data, size);
820 memset((char *)page + i, 0, PAGE_SIZE - i);
826 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
827 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
829 * data is a (void *) that can point to any structure up to
830 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
831 * information (or be NULL).
833 * Pre-0.97 versions of mount() didn't have a flags word.
834 * When the flags word was introduced its top half was required
835 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
836 * Therefore, if this magic number is present, it carries no information
837 * and must be discarded.
839 long do_mount(char * dev_name, char * dir_name, char *type_page,
840 unsigned long flags, void *data_page)
847 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
848 flags &= ~MS_MGC_MSK;
850 /* Basic sanity checks */
852 if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
854 if (dev_name && !memchr(dev_name, 0, PAGE_SIZE))
858 ((char *)data_page)[PAGE_SIZE - 1] = 0;
860 /* Separate the per-mountpoint flags */
861 if (flags & MS_NOSUID)
862 mnt_flags |= MNT_NOSUID;
863 if (flags & MS_NODEV)
864 mnt_flags |= MNT_NODEV;
865 if (flags & MS_NOEXEC)
866 mnt_flags |= MNT_NOEXEC;
867 flags &= ~(MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_ACTIVE);
869 if (vx_ccaps(VXC_SECURE_MOUNT))
870 mnt_flags |= MNT_NODEV;
872 /* ... and get the mountpoint */
873 retval = path_lookup(dir_name, LOOKUP_FOLLOW, &nd);
877 retval = security_sb_mount(dev_name, &nd, type_page, flags, data_page);
881 if (flags & MS_REMOUNT)
882 retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags,
884 else if (flags & MS_BIND)
885 retval = do_loopback(&nd, dev_name, flags & MS_REC);
886 else if (flags & MS_MOVE)
887 retval = do_move_mount(&nd, dev_name);
889 retval = do_add_mount(&nd, type_page, flags, mnt_flags,
890 dev_name, data_page);
896 int copy_namespace(int flags, struct task_struct *tsk)
898 struct namespace *namespace = tsk->namespace;
899 struct namespace *new_ns;
900 struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL;
901 struct fs_struct *fs = tsk->fs;
906 get_namespace(namespace);
908 if (!(flags & CLONE_NEWNS))
911 if (!capable(CAP_SYS_ADMIN)) {
912 put_namespace(namespace);
916 new_ns = kmalloc(sizeof(struct namespace), GFP_KERNEL);
920 atomic_set(&new_ns->count, 1);
921 init_rwsem(&new_ns->sem);
922 INIT_LIST_HEAD(&new_ns->list);
924 down_write(&tsk->namespace->sem);
925 /* First pass: copy the tree topology */
926 new_ns->root = copy_tree(namespace->root, namespace->root->mnt_root);
928 up_write(&tsk->namespace->sem);
932 spin_lock(&vfsmount_lock);
933 list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
934 spin_unlock(&vfsmount_lock);
936 /* Second pass: switch the tsk->fs->* elements */
938 struct vfsmount *p, *q;
939 write_lock(&fs->lock);
944 if (p == fs->rootmnt) {
946 fs->rootmnt = mntget(q);
948 if (p == fs->pwdmnt) {
950 fs->pwdmnt = mntget(q);
952 if (p == fs->altrootmnt) {
954 fs->altrootmnt = mntget(q);
956 p = next_mnt(p, namespace->root);
957 q = next_mnt(q, new_ns->root);
959 write_unlock(&fs->lock);
961 up_write(&tsk->namespace->sem);
963 tsk->namespace = new_ns;
972 put_namespace(namespace);
976 put_namespace(namespace);
980 asmlinkage long sys_mount(char __user * dev_name, char __user * dir_name,
981 char __user * type, unsigned long flags,
985 unsigned long data_page;
986 unsigned long type_page;
987 unsigned long dev_page;
990 retval = copy_mount_options (type, &type_page);
994 dir_page = getname(dir_name);
995 retval = PTR_ERR(dir_page);
996 if (IS_ERR(dir_page))
999 retval = copy_mount_options (dev_name, &dev_page);
1003 retval = copy_mount_options (data, &data_page);
1008 retval = do_mount((char*)dev_page, dir_page, (char*)type_page,
1009 flags, (void*)data_page);
1011 free_page(data_page);
1014 free_page(dev_page);
1018 free_page(type_page);
1023 * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
1024 * It can block. Requires the big lock held.
1026 void set_fs_root(struct fs_struct *fs, struct vfsmount *mnt,
1027 struct dentry *dentry)
1029 struct dentry *old_root;
1030 struct vfsmount *old_rootmnt;
1031 write_lock(&fs->lock);
1032 old_root = fs->root;
1033 old_rootmnt = fs->rootmnt;
1034 fs->rootmnt = mntget(mnt);
1035 fs->root = dget(dentry);
1036 write_unlock(&fs->lock);
1039 mntput(old_rootmnt);
1043 EXPORT_SYMBOL(set_fs_root);
1046 * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
1047 * It can block. Requires the big lock held.
1049 void set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
1050 struct dentry *dentry)
1052 struct dentry *old_pwd;
1053 struct vfsmount *old_pwdmnt;
1055 write_lock(&fs->lock);
1057 old_pwdmnt = fs->pwdmnt;
1058 fs->pwdmnt = mntget(mnt);
1059 fs->pwd = dget(dentry);
1060 write_unlock(&fs->lock);
1068 EXPORT_SYMBOL(set_fs_pwd);
1070 static void chroot_fs_refs(struct nameidata *old_nd, struct nameidata *new_nd)
1072 struct task_struct *g, *p;
1073 struct fs_struct *fs;
1075 read_lock(&tasklist_lock);
1076 do_each_thread(g, p) {
1080 atomic_inc(&fs->count);
1082 if (fs->root==old_nd->dentry&&fs->rootmnt==old_nd->mnt)
1083 set_fs_root(fs, new_nd->mnt, new_nd->dentry);
1084 if (fs->pwd==old_nd->dentry&&fs->pwdmnt==old_nd->mnt)
1085 set_fs_pwd(fs, new_nd->mnt, new_nd->dentry);
1089 } while_each_thread(g, p);
1090 read_unlock(&tasklist_lock);
1094 * Moves the current root to put_root, and sets root/cwd of all processes
1095 * which had them on the old root to new_root.
1098 * - we don't move root/cwd if they are not at the root (reason: if something
1099 * cared enough to change them, it's probably wrong to force them elsewhere)
1100 * - it's okay to pick a root that isn't the root of a file system, e.g.
1101 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
1102 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
1106 asmlinkage long sys_pivot_root(const char __user *new_root, const char __user *put_old)
1108 struct vfsmount *tmp;
1109 struct nameidata new_nd, old_nd, parent_nd, root_parent, user_nd;
1112 if (!capable(CAP_SYS_ADMIN))
1117 error = __user_walk(new_root, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &new_nd);
1121 if (!check_mnt(new_nd.mnt))
1124 error = __user_walk(put_old, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &old_nd);
1128 error = security_sb_pivotroot(&old_nd, &new_nd);
1130 path_release(&old_nd);
1134 read_lock(¤t->fs->lock);
1135 user_nd.mnt = mntget(current->fs->rootmnt);
1136 user_nd.dentry = dget(current->fs->root);
1137 read_unlock(¤t->fs->lock);
1138 down_write(¤t->namespace->sem);
1139 down(&old_nd.dentry->d_inode->i_sem);
1141 if (!check_mnt(user_nd.mnt))
1144 if (IS_DEADDIR(new_nd.dentry->d_inode))
1146 if (d_unhashed(new_nd.dentry) && !IS_ROOT(new_nd.dentry))
1148 if (d_unhashed(old_nd.dentry) && !IS_ROOT(old_nd.dentry))
1151 if (new_nd.mnt == user_nd.mnt || old_nd.mnt == user_nd.mnt)
1152 goto out2; /* loop */
1154 if (user_nd.mnt->mnt_root != user_nd.dentry)
1156 if (new_nd.mnt->mnt_root != new_nd.dentry)
1157 goto out2; /* not a mountpoint */
1158 tmp = old_nd.mnt; /* make sure we can reach put_old from new_root */
1159 spin_lock(&vfsmount_lock);
1160 if (tmp != new_nd.mnt) {
1162 if (tmp->mnt_parent == tmp)
1164 if (tmp->mnt_parent == new_nd.mnt)
1166 tmp = tmp->mnt_parent;
1168 if (!is_subdir(tmp->mnt_mountpoint, new_nd.dentry))
1170 } else if (!is_subdir(old_nd.dentry, new_nd.dentry))
1172 detach_mnt(new_nd.mnt, &parent_nd);
1173 detach_mnt(user_nd.mnt, &root_parent);
1174 attach_mnt(user_nd.mnt, &old_nd);
1175 attach_mnt(new_nd.mnt, &root_parent);
1176 spin_unlock(&vfsmount_lock);
1177 chroot_fs_refs(&user_nd, &new_nd);
1178 security_sb_post_pivotroot(&user_nd, &new_nd);
1180 path_release(&root_parent);
1181 path_release(&parent_nd);
1183 up(&old_nd.dentry->d_inode->i_sem);
1184 up_write(¤t->namespace->sem);
1185 path_release(&user_nd);
1186 path_release(&old_nd);
1188 path_release(&new_nd);
1193 spin_unlock(&vfsmount_lock);
1197 static void __init init_mount_tree(void)
1199 struct vfsmount *mnt;
1200 struct namespace *namespace;
1201 struct task_struct *g, *p;
1203 mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
1205 panic("Can't create rootfs");
1206 namespace = kmalloc(sizeof(*namespace), GFP_KERNEL);
1208 panic("Can't allocate initial namespace");
1209 atomic_set(&namespace->count, 1);
1210 INIT_LIST_HEAD(&namespace->list);
1211 init_rwsem(&namespace->sem);
1212 list_add(&mnt->mnt_list, &namespace->list);
1213 namespace->root = mnt;
1215 init_task.namespace = namespace;
1216 read_lock(&tasklist_lock);
1217 do_each_thread(g, p) {
1218 get_namespace(namespace);
1219 p->namespace = namespace;
1220 } while_each_thread(g, p);
1221 read_unlock(&tasklist_lock);
1223 set_fs_pwd(current->fs, namespace->root, namespace->root->mnt_root);
1224 set_fs_root(current->fs, namespace->root, namespace->root->mnt_root);
1227 void __init mnt_init(unsigned long mempages)
1229 struct list_head *d;
1230 unsigned long order;
1231 unsigned int nr_hash;
1234 mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
1235 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1238 mount_hashtable = (struct list_head *)
1239 __get_free_pages(GFP_ATOMIC, order);
1241 if (!mount_hashtable)
1242 panic("Failed to allocate mount hash table\n");
1245 * Find the power-of-two list-heads that can fit into the allocation..
1246 * We don't guarantee that "sizeof(struct list_head)" is necessarily
1249 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct list_head);
1253 } while ((nr_hash >> hash_bits) != 0);
1257 * Re-calculate the actual number of entries and the mask
1258 * from the number of bits we can fit.
1260 nr_hash = 1UL << hash_bits;
1261 hash_mask = nr_hash-1;
1263 printk("Mount-cache hash table entries: %d (order: %ld, %ld bytes)\n",
1264 nr_hash, order, (PAGE_SIZE << order));
1266 /* And initialize the newly allocated array */
1267 d = mount_hashtable;
1279 void __put_namespace(struct namespace *namespace)
1281 down_write(&namespace->sem);
1282 spin_lock(&vfsmount_lock);
1283 umount_tree(namespace->root);
1284 spin_unlock(&vfsmount_lock);
1285 up_write(&namespace->sem);