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/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 <linux/vserver/namespace.h>
26 #include <linux/vserver/xid.h>
28 #include <asm/uaccess.h>
29 #include <asm/unistd.h>
31 extern int __init init_rootfs(void);
34 extern int __init sysfs_init(void);
36 static inline int sysfs_init(void)
42 /* spinlock for vfsmount related operations, inplace of dcache_lock */
43 spinlock_t vfsmount_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
45 static struct list_head *mount_hashtable;
46 static int hash_mask, hash_bits;
47 static kmem_cache_t *mnt_cache;
49 static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
51 unsigned long tmp = ((unsigned long) mnt / L1_CACHE_BYTES);
52 tmp += ((unsigned long) dentry / L1_CACHE_BYTES);
53 tmp = tmp + (tmp >> hash_bits);
54 return tmp & hash_mask;
57 struct vfsmount *alloc_vfsmnt(const char *name)
59 struct vfsmount *mnt = kmem_cache_alloc(mnt_cache, GFP_KERNEL);
61 memset(mnt, 0, sizeof(struct vfsmount));
62 atomic_set(&mnt->mnt_count,1);
63 INIT_LIST_HEAD(&mnt->mnt_hash);
64 INIT_LIST_HEAD(&mnt->mnt_child);
65 INIT_LIST_HEAD(&mnt->mnt_mounts);
66 INIT_LIST_HEAD(&mnt->mnt_list);
67 INIT_LIST_HEAD(&mnt->mnt_fslink);
69 int size = strlen(name)+1;
70 char *newname = kmalloc(size, GFP_KERNEL);
72 memcpy(newname, name, size);
73 mnt->mnt_devname = newname;
80 void free_vfsmnt(struct vfsmount *mnt)
82 kfree(mnt->mnt_devname);
83 kmem_cache_free(mnt_cache, mnt);
87 * Now, lookup_mnt increments the ref count before returning
88 * the vfsmount struct.
90 struct vfsmount *lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
92 struct list_head * head = mount_hashtable + hash(mnt, dentry);
93 struct list_head * tmp = head;
94 struct vfsmount *p, *found = NULL;
96 spin_lock(&vfsmount_lock);
102 p = list_entry(tmp, struct vfsmount, mnt_hash);
103 if (p->mnt_parent == mnt && p->mnt_mountpoint == dentry) {
108 spin_unlock(&vfsmount_lock);
112 static inline int check_mnt(struct vfsmount *mnt)
114 return mnt->mnt_namespace == current->namespace;
117 static void detach_mnt(struct vfsmount *mnt, struct nameidata *old_nd)
119 old_nd->dentry = mnt->mnt_mountpoint;
120 old_nd->mnt = mnt->mnt_parent;
121 mnt->mnt_parent = mnt;
122 mnt->mnt_mountpoint = mnt->mnt_root;
123 list_del_init(&mnt->mnt_child);
124 list_del_init(&mnt->mnt_hash);
125 old_nd->dentry->d_mounted--;
128 static void attach_mnt(struct vfsmount *mnt, struct nameidata *nd)
130 mnt->mnt_parent = mntget(nd->mnt);
131 mnt->mnt_mountpoint = dget(nd->dentry);
132 list_add(&mnt->mnt_hash, mount_hashtable+hash(nd->mnt, nd->dentry));
133 list_add_tail(&mnt->mnt_child, &nd->mnt->mnt_mounts);
134 nd->dentry->d_mounted++;
137 static struct vfsmount *next_mnt(struct vfsmount *p, struct vfsmount *root)
139 struct list_head *next = p->mnt_mounts.next;
140 if (next == &p->mnt_mounts) {
144 next = p->mnt_child.next;
145 if (next != &p->mnt_parent->mnt_mounts)
150 return list_entry(next, struct vfsmount, mnt_child);
153 static struct vfsmount *
154 clone_mnt(struct vfsmount *old, struct dentry *root)
156 struct super_block *sb = old->mnt_sb;
157 struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname);
160 mnt->mnt_flags = old->mnt_flags;
161 atomic_inc(&sb->s_active);
163 mnt->mnt_root = dget(root);
164 mnt->mnt_mountpoint = mnt->mnt_root;
165 mnt->mnt_parent = mnt;
166 mnt->mnt_namespace = old->mnt_namespace;
167 mnt->mnt_xid = old->mnt_xid;
169 /* stick the duplicate mount on the same expiry list
170 * as the original if that was on one */
171 spin_lock(&vfsmount_lock);
172 if (!list_empty(&old->mnt_fslink))
173 list_add(&mnt->mnt_fslink, &old->mnt_fslink);
174 spin_unlock(&vfsmount_lock);
179 void __mntput(struct vfsmount *mnt)
181 struct super_block *sb = mnt->mnt_sb;
184 deactivate_super(sb);
187 EXPORT_SYMBOL(__mntput);
190 static void *m_start(struct seq_file *m, loff_t *pos)
192 struct namespace *n = m->private;
197 list_for_each(p, &n->list)
199 return list_entry(p, struct vfsmount, mnt_list);
203 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
205 struct namespace *n = m->private;
206 struct list_head *p = ((struct vfsmount *)v)->mnt_list.next;
208 return p==&n->list ? NULL : list_entry(p, struct vfsmount, mnt_list);
211 static void m_stop(struct seq_file *m, void *v)
213 struct namespace *n = m->private;
217 static inline void mangle(struct seq_file *m, const char *s)
219 seq_escape(m, s, " \t\n\\");
222 static int show_vfsmnt(struct seq_file *m, void *v)
224 struct vfsmount *mnt = v;
226 static struct proc_fs_info {
232 { MS_RDONLY, MNT_RDONLY, "ro", "rw" },
233 { MS_SYNCHRONOUS, 0, ",sync", NULL },
234 { MS_DIRSYNC, 0, ",dirsync", NULL },
235 { MS_MANDLOCK, 0, ",mand", NULL },
236 { MS_NOATIME, MNT_NOATIME, ",noatime", NULL },
237 { MS_NODIRATIME, MNT_NODIRATIME, ",nodiratime", NULL },
238 { MS_TAGXID, MS_TAGXID, ",tagxid", NULL },
239 { 0, MNT_NOSUID, ",nosuid", NULL },
240 { 0, MNT_NODEV, ",nodev", NULL },
241 { 0, MNT_NOEXEC, ",noexec", NULL },
244 struct proc_fs_info *p;
245 unsigned long s_flags = mnt->mnt_sb->s_flags;
246 int mnt_flags = mnt->mnt_flags;
248 if (vx_flags(VXF_HIDE_MOUNT, 0))
250 if (!vx_check_vfsmount(current->vx_info, mnt))
253 if (vx_flags(VXF_HIDE_MOUNT, 0))
255 if (!vx_check_vfsmount(current->vx_info, mnt))
258 mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
260 seq_path(m, mnt, mnt->mnt_root, " \t\n\\");
262 mangle(m, mnt->mnt_sb->s_type->name);
264 for (p = fs_info; (p->s_flag | p->mnt_flag) ; p++) {
265 if ((s_flags & p->s_flag) || (mnt_flags & p->mnt_flag)) {
267 seq_puts(m, p->set_str);
270 seq_puts(m, p->unset_str);
273 if (mnt->mnt_flags & MNT_XID)
274 seq_printf(m, ",xid=%d", mnt->mnt_xid);
275 if (mnt->mnt_sb->s_op->show_options)
276 err = mnt->mnt_sb->s_op->show_options(m, mnt);
277 seq_puts(m, " 0 0\n");
281 struct seq_operations mounts_op = {
289 * may_umount_tree - check if a mount tree is busy
290 * @mnt: root of mount tree
292 * This is called to check if a tree of mounts has any
293 * open files, pwds, chroots or sub mounts that are
296 int may_umount_tree(struct vfsmount *mnt)
298 struct list_head *next;
299 struct vfsmount *this_parent = mnt;
303 spin_lock(&vfsmount_lock);
304 actual_refs = atomic_read(&mnt->mnt_count);
307 next = this_parent->mnt_mounts.next;
309 while (next != &this_parent->mnt_mounts) {
310 struct vfsmount *p = list_entry(next, struct vfsmount, mnt_child);
314 actual_refs += atomic_read(&p->mnt_count);
317 if (!list_empty(&p->mnt_mounts)) {
323 if (this_parent != mnt) {
324 next = this_parent->mnt_child.next;
325 this_parent = this_parent->mnt_parent;
328 spin_unlock(&vfsmount_lock);
330 if (actual_refs > minimum_refs)
336 EXPORT_SYMBOL(may_umount_tree);
339 * may_umount - check if a mount point is busy
340 * @mnt: root of mount
342 * This is called to check if a mount point has any
343 * open files, pwds, chroots or sub mounts. If the
344 * mount has sub mounts this will return busy
345 * regardless of whether the sub mounts are busy.
347 * Doesn't take quota and stuff into account. IOW, in some cases it will
348 * give false negatives. The main reason why it's here is that we need
349 * a non-destructive way to look for easily umountable filesystems.
351 int may_umount(struct vfsmount *mnt)
353 if (atomic_read(&mnt->mnt_count) > 2)
358 EXPORT_SYMBOL(may_umount);
360 static inline void __umount_list(struct list_head *kill)
362 struct vfsmount *mnt;
364 while (!list_empty(kill)) {
365 mnt = list_entry(kill->next, struct vfsmount, mnt_list);
366 list_del_init(&mnt->mnt_list);
367 list_del_init(&mnt->mnt_fslink);
368 if (mnt->mnt_parent == mnt) {
369 spin_unlock(&vfsmount_lock);
371 struct nameidata old_nd;
372 detach_mnt(mnt, &old_nd);
373 spin_unlock(&vfsmount_lock);
374 path_release(&old_nd);
377 spin_lock(&vfsmount_lock);
381 void umount_tree(struct vfsmount *mnt)
386 for (p = mnt; p; p = next_mnt(p, mnt)) {
387 list_del(&p->mnt_list);
388 list_add(&p->mnt_list, &kill);
390 __umount_list(&kill);
393 void umount_unused(struct vfsmount *mnt, struct fs_struct *fs)
398 for (p = mnt; p; p = next_mnt(p, mnt)) {
399 if (p == fs->rootmnt || p == fs->pwdmnt)
401 list_del(&p->mnt_list);
402 list_add(&p->mnt_list, &kill);
404 __umount_list(&kill);
407 static int do_umount(struct vfsmount *mnt, int flags)
409 struct super_block * sb = mnt->mnt_sb;
412 retval = security_sb_umount(mnt, flags);
417 * Allow userspace to request a mountpoint be expired rather than
418 * unmounting unconditionally. Unmount only happens if:
419 * (1) the mark is already set (the mark is cleared by mntput())
420 * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
422 if (flags & MNT_EXPIRE) {
423 if (mnt == current->fs->rootmnt ||
424 flags & (MNT_FORCE | MNT_DETACH))
427 if (atomic_read(&mnt->mnt_count) != 2)
430 if (!xchg(&mnt->mnt_expiry_mark, 1))
435 * If we may have to abort operations to get out of this
436 * mount, and they will themselves hold resources we must
437 * allow the fs to do things. In the Unix tradition of
438 * 'Gee thats tricky lets do it in userspace' the umount_begin
439 * might fail to complete on the first run through as other tasks
440 * must return, and the like. Thats for the mount program to worry
441 * about for the moment.
445 if( (flags&MNT_FORCE) && sb->s_op->umount_begin)
446 sb->s_op->umount_begin(sb);
450 * No sense to grab the lock for this test, but test itself looks
451 * somewhat bogus. Suggestions for better replacement?
452 * Ho-hum... In principle, we might treat that as umount + switch
453 * to rootfs. GC would eventually take care of the old vfsmount.
454 * Actually it makes sense, especially if rootfs would contain a
455 * /reboot - static binary that would close all descriptors and
456 * call reboot(9). Then init(8) could umount root and exec /reboot.
458 if (mnt == current->fs->rootmnt && !(flags & MNT_DETACH)) {
460 * Special case for "unmounting" root ...
461 * we just try to remount it readonly.
463 down_write(&sb->s_umount);
464 if (!(sb->s_flags & MS_RDONLY)) {
467 retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
470 up_write(&sb->s_umount);
474 down_write(¤t->namespace->sem);
475 spin_lock(&vfsmount_lock);
477 if (atomic_read(&sb->s_active) == 1) {
478 /* last instance - try to be smart */
479 spin_unlock(&vfsmount_lock);
484 security_sb_umount_close(mnt);
485 spin_lock(&vfsmount_lock);
488 if (atomic_read(&mnt->mnt_count) == 2 || flags & MNT_DETACH) {
489 if (!list_empty(&mnt->mnt_list))
493 spin_unlock(&vfsmount_lock);
495 security_sb_umount_busy(mnt);
496 up_write(¤t->namespace->sem);
501 * Now umount can handle mount points as well as block devices.
502 * This is important for filesystems which use unnamed block devices.
504 * We now support a flag for forced unmount like the other 'big iron'
505 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
508 asmlinkage long sys_umount(char __user * name, int flags)
513 retval = __user_walk(name, LOOKUP_FOLLOW, &nd);
517 if (nd.dentry != nd.mnt->mnt_root)
519 if (!check_mnt(nd.mnt))
523 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SECURE_MOUNT))
526 retval = do_umount(nd.mnt, flags);
528 path_release_on_umount(&nd);
533 #ifdef __ARCH_WANT_SYS_OLDUMOUNT
536 * The 2.0 compatible umount. No flags.
539 asmlinkage long sys_oldumount(char __user * name)
541 return sys_umount(name,0);
546 static int mount_is_safe(struct nameidata *nd)
548 if (capable(CAP_SYS_ADMIN))
550 if (vx_ccaps(VXC_SECURE_MOUNT))
554 if (S_ISLNK(nd->dentry->d_inode->i_mode))
556 if (nd->dentry->d_inode->i_mode & S_ISVTX) {
557 if (current->uid != nd->dentry->d_inode->i_uid)
560 if (permission(nd->dentry->d_inode, MAY_WRITE, nd))
567 lives_below_in_same_fs(struct dentry *d, struct dentry *dentry)
572 if (d == NULL || d == d->d_parent)
578 static struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry)
580 struct vfsmount *res, *p, *q, *r, *s;
584 res = q = clone_mnt(mnt, dentry);
587 q->mnt_mountpoint = mnt->mnt_mountpoint;
590 for (h = mnt->mnt_mounts.next; h != &mnt->mnt_mounts; h = h->next) {
591 r = list_entry(h, struct vfsmount, mnt_child);
592 if (!lives_below_in_same_fs(r->mnt_mountpoint, dentry))
595 for (s = r; s; s = next_mnt(s, r)) {
596 while (p != s->mnt_parent) {
602 nd.dentry = p->mnt_mountpoint;
603 q = clone_mnt(p, p->mnt_root);
606 spin_lock(&vfsmount_lock);
607 list_add_tail(&q->mnt_list, &res->mnt_list);
609 spin_unlock(&vfsmount_lock);
615 spin_lock(&vfsmount_lock);
617 spin_unlock(&vfsmount_lock);
622 static int graft_tree(struct vfsmount *mnt, struct nameidata *nd)
625 if (mnt->mnt_sb->s_flags & MS_NOUSER)
628 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
629 S_ISDIR(mnt->mnt_root->d_inode->i_mode))
633 down(&nd->dentry->d_inode->i_sem);
634 if (IS_DEADDIR(nd->dentry->d_inode))
637 err = security_sb_check_sb(mnt, nd);
642 spin_lock(&vfsmount_lock);
643 if (IS_ROOT(nd->dentry) || !d_unhashed(nd->dentry)) {
644 struct list_head head;
647 list_add_tail(&head, &mnt->mnt_list);
648 list_splice(&head, current->namespace->list.prev);
652 spin_unlock(&vfsmount_lock);
654 up(&nd->dentry->d_inode->i_sem);
656 security_sb_post_addmount(mnt, nd);
663 static int do_loopback(struct nameidata *nd, char *old_name, xid_t xid, unsigned long flags, int mnt_flags)
665 struct nameidata old_nd;
666 struct vfsmount *mnt = NULL;
667 int recurse = flags & MS_REC;
668 int err = mount_is_safe(nd);
672 if (!old_name || !*old_name)
674 err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
678 down_write(¤t->namespace->sem);
680 if (check_mnt(nd->mnt) && (!recurse || check_mnt(old_nd.mnt))) {
683 mnt = copy_tree(old_nd.mnt, old_nd.dentry);
685 mnt = clone_mnt(old_nd.mnt, old_nd.dentry);
689 /* stop bind mounts from expiring */
690 spin_lock(&vfsmount_lock);
691 list_del_init(&mnt->mnt_fslink);
692 spin_unlock(&vfsmount_lock);
694 if (flags & MS_XID) {
696 mnt->mnt_flags |= MNT_XID;
698 err = graft_tree(mnt, nd);
700 spin_lock(&vfsmount_lock);
702 spin_unlock(&vfsmount_lock);
705 mnt->mnt_flags = mnt_flags;
708 up_write(¤t->namespace->sem);
709 path_release(&old_nd);
714 * change filesystem flags. dir should be a physical root of filesystem.
715 * If you've mounted a non-root directory somewhere and want to do remount
716 * on it - tough luck.
719 static int do_remount(struct nameidata *nd, int flags, int mnt_flags,
720 void *data, xid_t xid)
723 struct super_block * sb = nd->mnt->mnt_sb;
725 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SECURE_REMOUNT))
728 if (!check_mnt(nd->mnt))
731 if (nd->dentry != nd->mnt->mnt_root)
734 if (vx_ccaps(VXC_SECURE_REMOUNT))
735 mnt_flags |= MNT_NODEV;
736 down_write(&sb->s_umount);
737 err = do_remount_sb(sb, flags, data, 0);
739 nd->mnt->mnt_flags=mnt_flags;
741 nd->mnt->mnt_xid = xid;
743 up_write(&sb->s_umount);
745 security_sb_post_remount(nd->mnt, flags, data);
749 static int do_move_mount(struct nameidata *nd, char *old_name)
751 struct nameidata old_nd, parent_nd;
754 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SECURE_MOUNT))
756 if (!old_name || !*old_name)
758 err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
762 down_write(¤t->namespace->sem);
763 while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
766 if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt))
770 down(&nd->dentry->d_inode->i_sem);
771 if (IS_DEADDIR(nd->dentry->d_inode))
774 spin_lock(&vfsmount_lock);
775 if (!IS_ROOT(nd->dentry) && d_unhashed(nd->dentry))
779 if (old_nd.dentry != old_nd.mnt->mnt_root)
782 if (old_nd.mnt == old_nd.mnt->mnt_parent)
785 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
786 S_ISDIR(old_nd.dentry->d_inode->i_mode))
790 for (p = nd->mnt; p->mnt_parent!=p; p = p->mnt_parent)
795 detach_mnt(old_nd.mnt, &parent_nd);
796 attach_mnt(old_nd.mnt, nd);
798 /* if the mount is moved, it should no longer be expire
800 list_del_init(&old_nd.mnt->mnt_fslink);
802 spin_unlock(&vfsmount_lock);
804 up(&nd->dentry->d_inode->i_sem);
806 up_write(¤t->namespace->sem);
808 path_release(&parent_nd);
809 path_release(&old_nd);
814 * create a new mount for userspace and request it to be added into the
817 static int do_new_mount(struct nameidata *nd, char *type, int flags,
818 int mnt_flags, char *name, void *data)
820 struct vfsmount *mnt;
822 if (!type || !memchr(type, 0, PAGE_SIZE))
825 /* we need capabilities... */
826 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SECURE_MOUNT))
829 mnt = do_kern_mount(type, flags, name, data);
833 return do_add_mount(mnt, nd, mnt_flags, NULL);
837 * add a mount into a namespace's mount tree
838 * - provide the option of adding the new mount to an expiration list
840 int do_add_mount(struct vfsmount *newmnt, struct nameidata *nd,
841 int mnt_flags, struct list_head *fslist)
845 down_write(¤t->namespace->sem);
846 /* Something was mounted here while we slept */
847 while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
850 if (!check_mnt(nd->mnt))
853 /* Refuse the same filesystem on the same mount point */
855 if (nd->mnt->mnt_sb == newmnt->mnt_sb &&
856 nd->mnt->mnt_root == nd->dentry)
860 if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
863 newmnt->mnt_flags = mnt_flags;
864 err = graft_tree(newmnt, nd);
866 if (err == 0 && fslist) {
867 /* add to the specified expiration list */
868 spin_lock(&vfsmount_lock);
869 list_add_tail(&newmnt->mnt_fslink, fslist);
870 spin_unlock(&vfsmount_lock);
874 up_write(¤t->namespace->sem);
879 EXPORT_SYMBOL_GPL(do_add_mount);
882 * process a list of expirable mountpoints with the intent of discarding any
883 * mountpoints that aren't in use and haven't been touched since last we came
886 void mark_mounts_for_expiry(struct list_head *mounts)
888 struct namespace *namespace;
889 struct vfsmount *mnt, *next;
890 LIST_HEAD(graveyard);
892 if (list_empty(mounts))
895 spin_lock(&vfsmount_lock);
897 /* extract from the expiration list every vfsmount that matches the
898 * following criteria:
899 * - only referenced by its parent vfsmount
900 * - still marked for expiry (marked on the last call here; marks are
901 * cleared by mntput())
903 list_for_each_entry_safe(mnt, next, mounts, mnt_fslink) {
904 if (!xchg(&mnt->mnt_expiry_mark, 1) ||
905 atomic_read(&mnt->mnt_count) != 1)
909 list_move(&mnt->mnt_fslink, &graveyard);
913 * go through the vfsmounts we've just consigned to the graveyard to
914 * - check that they're still dead
915 * - delete the vfsmount from the appropriate namespace under lock
916 * - dispose of the corpse
918 while (!list_empty(&graveyard)) {
919 mnt = list_entry(graveyard.next, struct vfsmount, mnt_fslink);
920 list_del_init(&mnt->mnt_fslink);
922 /* don't do anything if the namespace is dead - all the
923 * vfsmounts from it are going away anyway */
924 namespace = mnt->mnt_namespace;
925 if (!namespace || atomic_read(&namespace->count) <= 0)
927 get_namespace(namespace);
929 spin_unlock(&vfsmount_lock);
930 down_write(&namespace->sem);
931 spin_lock(&vfsmount_lock);
933 /* check that it is still dead: the count should now be 2 - as
934 * contributed by the vfsmount parent and the mntget above */
935 if (atomic_read(&mnt->mnt_count) == 2) {
936 struct vfsmount *xdmnt;
937 struct dentry *xdentry;
939 /* delete from the namespace */
940 list_del_init(&mnt->mnt_list);
941 list_del_init(&mnt->mnt_child);
942 list_del_init(&mnt->mnt_hash);
943 mnt->mnt_mountpoint->d_mounted--;
945 xdentry = mnt->mnt_mountpoint;
946 mnt->mnt_mountpoint = mnt->mnt_root;
947 xdmnt = mnt->mnt_parent;
948 mnt->mnt_parent = mnt;
950 spin_unlock(&vfsmount_lock);
955 /* now lay it to rest if this was the last ref on the
957 if (atomic_read(&mnt->mnt_sb->s_active) == 1) {
958 /* last instance - try to be smart */
960 DQUOT_OFF(mnt->mnt_sb);
961 acct_auto_close(mnt->mnt_sb);
967 /* someone brought it back to life whilst we didn't
968 * have any locks held so return it to the expiration
970 list_add_tail(&mnt->mnt_fslink, mounts);
971 spin_unlock(&vfsmount_lock);
974 up_write(&namespace->sem);
977 put_namespace(namespace);
979 spin_lock(&vfsmount_lock);
982 spin_unlock(&vfsmount_lock);
985 EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
988 * Some copy_from_user() implementations do not return the exact number of
989 * bytes remaining to copy on a fault. But copy_mount_options() requires that.
990 * Note that this function differs from copy_from_user() in that it will oops
991 * on bad values of `to', rather than returning a short copy.
994 exact_copy_from_user(void *to, const void __user *from, unsigned long n)
997 const char __user *f = from;
1000 if (!access_ok(VERIFY_READ, from, n))
1004 if (__get_user(c, f)) {
1015 int copy_mount_options(const void __user *data, unsigned long *where)
1025 if (!(page = __get_free_page(GFP_KERNEL)))
1028 /* We only care that *some* data at the address the user
1029 * gave us is valid. Just in case, we'll zero
1030 * the remainder of the page.
1032 /* copy_from_user cannot cross TASK_SIZE ! */
1033 size = TASK_SIZE - (unsigned long)data;
1034 if (size > PAGE_SIZE)
1037 i = size - exact_copy_from_user((void *)page, data, size);
1043 memset((char *)page + i, 0, PAGE_SIZE - i);
1049 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
1050 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
1052 * data is a (void *) that can point to any structure up to
1053 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
1054 * information (or be NULL).
1056 * Pre-0.97 versions of mount() didn't have a flags word.
1057 * When the flags word was introduced its top half was required
1058 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
1059 * Therefore, if this magic number is present, it carries no information
1060 * and must be discarded.
1062 long do_mount(char * dev_name, char * dir_name, char *type_page,
1063 unsigned long flags, void *data_page)
1065 struct nameidata nd;
1071 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
1072 flags &= ~MS_MGC_MSK;
1074 /* Basic sanity checks */
1076 if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
1078 if (dev_name && !memchr(dev_name, 0, PAGE_SIZE))
1082 ((char *)data_page)[PAGE_SIZE - 1] = 0;
1084 retval = vx_parse_xid(data_page, &xid, 1);
1086 mnt_flags |= MNT_XID;
1087 /* bind and re-mounts get xid flag */
1088 if (flags & (MS_BIND|MS_REMOUNT))
1092 /* Separate the per-mountpoint flags */
1093 if (flags & MS_RDONLY)
1094 mnt_flags |= MNT_RDONLY;
1095 if (flags & MS_NOSUID)
1096 mnt_flags |= MNT_NOSUID;
1097 if (flags & MS_NODEV)
1098 mnt_flags |= MNT_NODEV;
1099 if (flags & MS_NOEXEC)
1100 mnt_flags |= MNT_NOEXEC;
1101 if (flags & MS_NOATIME)
1102 mnt_flags |= MNT_NOATIME;
1103 if (flags & MS_NODIRATIME)
1104 mnt_flags |= MNT_NODIRATIME;
1105 flags &= ~(MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_ACTIVE);
1107 if (vx_ccaps(VXC_SECURE_MOUNT))
1108 mnt_flags |= MNT_NODEV;
1110 /* ... and get the mountpoint */
1111 retval = path_lookup(dir_name, LOOKUP_FOLLOW, &nd);
1115 retval = security_sb_mount(dev_name, &nd, type_page, flags, data_page);
1119 if (flags & MS_REMOUNT)
1120 retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags,
1122 else if (flags & MS_BIND)
1123 retval = do_loopback(&nd, dev_name, xid, flags, mnt_flags);
1125 else if (flags & MS_MOVE)
1126 retval = do_move_mount(&nd, dev_name);
1128 retval = do_new_mount(&nd, type_page, flags, mnt_flags,
1129 dev_name, data_page);
1135 int copy_namespace(int flags, struct task_struct *tsk)
1137 struct namespace *namespace = tsk->namespace;
1138 struct namespace *new_ns;
1139 struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL;
1140 struct fs_struct *fs = tsk->fs;
1141 struct vfsmount *p, *q;
1146 get_namespace(namespace);
1148 if (!(flags & CLONE_NEWNS))
1151 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SECURE_MOUNT)) {
1152 put_namespace(namespace);
1156 new_ns = kmalloc(sizeof(struct namespace), GFP_KERNEL);
1160 atomic_set(&new_ns->count, 1);
1161 init_rwsem(&new_ns->sem);
1162 INIT_LIST_HEAD(&new_ns->list);
1164 down_write(&tsk->namespace->sem);
1165 /* First pass: copy the tree topology */
1166 new_ns->root = copy_tree(namespace->root, namespace->root->mnt_root);
1167 if (!new_ns->root) {
1168 up_write(&tsk->namespace->sem);
1172 spin_lock(&vfsmount_lock);
1173 list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
1174 spin_unlock(&vfsmount_lock);
1177 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
1178 * as belonging to new namespace. We have already acquired a private
1179 * fs_struct, so tsk->fs->lock is not needed.
1181 p = namespace->root;
1184 q->mnt_namespace = new_ns;
1186 if (p == fs->rootmnt) {
1188 fs->rootmnt = mntget(q);
1190 if (p == fs->pwdmnt) {
1192 fs->pwdmnt = mntget(q);
1194 if (p == fs->altrootmnt) {
1196 fs->altrootmnt = mntget(q);
1199 p = next_mnt(p, namespace->root);
1200 q = next_mnt(q, new_ns->root);
1202 up_write(&tsk->namespace->sem);
1204 tsk->namespace = new_ns;
1213 put_namespace(namespace);
1217 put_namespace(namespace);
1221 asmlinkage long sys_mount(char __user * dev_name, char __user * dir_name,
1222 char __user * type, unsigned long flags,
1226 unsigned long data_page;
1227 unsigned long type_page;
1228 unsigned long dev_page;
1231 retval = copy_mount_options (type, &type_page);
1235 dir_page = getname(dir_name);
1236 retval = PTR_ERR(dir_page);
1237 if (IS_ERR(dir_page))
1240 retval = copy_mount_options (dev_name, &dev_page);
1244 retval = copy_mount_options (data, &data_page);
1249 retval = do_mount((char*)dev_page, dir_page, (char*)type_page,
1250 flags, (void*)data_page);
1252 free_page(data_page);
1255 free_page(dev_page);
1259 free_page(type_page);
1264 * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
1265 * It can block. Requires the big lock held.
1267 void set_fs_root(struct fs_struct *fs, struct vfsmount *mnt,
1268 struct dentry *dentry)
1270 struct dentry *old_root;
1271 struct vfsmount *old_rootmnt;
1272 write_lock(&fs->lock);
1273 old_root = fs->root;
1274 old_rootmnt = fs->rootmnt;
1275 fs->rootmnt = mntget(mnt);
1276 fs->root = dget(dentry);
1277 write_unlock(&fs->lock);
1280 mntput(old_rootmnt);
1284 EXPORT_SYMBOL_GPL(set_fs_root);
1287 * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
1288 * It can block. Requires the big lock held.
1290 void set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
1291 struct dentry *dentry)
1293 struct dentry *old_pwd;
1294 struct vfsmount *old_pwdmnt;
1296 write_lock(&fs->lock);
1298 old_pwdmnt = fs->pwdmnt;
1299 fs->pwdmnt = mntget(mnt);
1300 fs->pwd = dget(dentry);
1301 write_unlock(&fs->lock);
1309 static void chroot_fs_refs(struct nameidata *old_nd, struct nameidata *new_nd)
1311 struct task_struct *g, *p;
1312 struct fs_struct *fs;
1314 read_lock(&tasklist_lock);
1315 do_each_thread(g, p) {
1319 atomic_inc(&fs->count);
1321 if (fs->root==old_nd->dentry&&fs->rootmnt==old_nd->mnt)
1322 set_fs_root(fs, new_nd->mnt, new_nd->dentry);
1323 if (fs->pwd==old_nd->dentry&&fs->pwdmnt==old_nd->mnt)
1324 set_fs_pwd(fs, new_nd->mnt, new_nd->dentry);
1328 } while_each_thread(g, p);
1329 read_unlock(&tasklist_lock);
1333 * Moves the current root to put_root, and sets root/cwd of all processes
1334 * which had them on the old root to new_root.
1337 * - we don't move root/cwd if they are not at the root (reason: if something
1338 * cared enough to change them, it's probably wrong to force them elsewhere)
1339 * - it's okay to pick a root that isn't the root of a file system, e.g.
1340 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
1341 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
1345 asmlinkage long sys_pivot_root(const char __user *new_root, const char __user *put_old)
1347 struct vfsmount *tmp;
1348 struct nameidata new_nd, old_nd, parent_nd, root_parent, user_nd;
1351 if (!capable(CAP_SYS_ADMIN))
1356 error = __user_walk(new_root, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &new_nd);
1360 if (!check_mnt(new_nd.mnt))
1363 error = __user_walk(put_old, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &old_nd);
1367 error = security_sb_pivotroot(&old_nd, &new_nd);
1369 path_release(&old_nd);
1373 read_lock(¤t->fs->lock);
1374 user_nd.mnt = mntget(current->fs->rootmnt);
1375 user_nd.dentry = dget(current->fs->root);
1376 read_unlock(¤t->fs->lock);
1377 down_write(¤t->namespace->sem);
1378 down(&old_nd.dentry->d_inode->i_sem);
1380 if (!check_mnt(user_nd.mnt))
1383 if (IS_DEADDIR(new_nd.dentry->d_inode))
1385 if (d_unhashed(new_nd.dentry) && !IS_ROOT(new_nd.dentry))
1387 if (d_unhashed(old_nd.dentry) && !IS_ROOT(old_nd.dentry))
1390 if (new_nd.mnt == user_nd.mnt || old_nd.mnt == user_nd.mnt)
1391 goto out2; /* loop */
1393 if (user_nd.mnt->mnt_root != user_nd.dentry)
1395 if (new_nd.mnt->mnt_root != new_nd.dentry)
1396 goto out2; /* not a mountpoint */
1397 tmp = old_nd.mnt; /* make sure we can reach put_old from new_root */
1398 spin_lock(&vfsmount_lock);
1399 if (tmp != new_nd.mnt) {
1401 if (tmp->mnt_parent == tmp)
1403 if (tmp->mnt_parent == new_nd.mnt)
1405 tmp = tmp->mnt_parent;
1407 if (!is_subdir(tmp->mnt_mountpoint, new_nd.dentry))
1409 } else if (!is_subdir(old_nd.dentry, new_nd.dentry))
1411 detach_mnt(new_nd.mnt, &parent_nd);
1412 detach_mnt(user_nd.mnt, &root_parent);
1413 attach_mnt(user_nd.mnt, &old_nd);
1414 attach_mnt(new_nd.mnt, &root_parent);
1415 spin_unlock(&vfsmount_lock);
1416 chroot_fs_refs(&user_nd, &new_nd);
1417 security_sb_post_pivotroot(&user_nd, &new_nd);
1419 path_release(&root_parent);
1420 path_release(&parent_nd);
1422 up(&old_nd.dentry->d_inode->i_sem);
1423 up_write(¤t->namespace->sem);
1424 path_release(&user_nd);
1425 path_release(&old_nd);
1427 path_release(&new_nd);
1432 spin_unlock(&vfsmount_lock);
1436 static void __init init_mount_tree(void)
1438 struct vfsmount *mnt;
1439 struct namespace *namespace;
1440 struct task_struct *g, *p;
1442 mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
1444 panic("Can't create rootfs");
1445 namespace = kmalloc(sizeof(*namespace), GFP_KERNEL);
1447 panic("Can't allocate initial namespace");
1448 atomic_set(&namespace->count, 1);
1449 INIT_LIST_HEAD(&namespace->list);
1450 init_rwsem(&namespace->sem);
1451 list_add(&mnt->mnt_list, &namespace->list);
1452 namespace->root = mnt;
1453 mnt->mnt_namespace = namespace;
1455 init_task.namespace = namespace;
1456 read_lock(&tasklist_lock);
1457 do_each_thread(g, p) {
1458 get_namespace(namespace);
1459 p->namespace = namespace;
1460 } while_each_thread(g, p);
1461 read_unlock(&tasklist_lock);
1463 set_fs_pwd(current->fs, namespace->root, namespace->root->mnt_root);
1464 set_fs_root(current->fs, namespace->root, namespace->root->mnt_root);
1467 void __init mnt_init(unsigned long mempages)
1469 struct list_head *d;
1470 unsigned long order;
1471 unsigned int nr_hash;
1474 mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
1475 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1478 mount_hashtable = (struct list_head *)
1479 __get_free_pages(GFP_ATOMIC, order);
1481 if (!mount_hashtable)
1482 panic("Failed to allocate mount hash table\n");
1485 * Find the power-of-two list-heads that can fit into the allocation..
1486 * We don't guarantee that "sizeof(struct list_head)" is necessarily
1489 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct list_head);
1493 } while ((nr_hash >> hash_bits) != 0);
1497 * Re-calculate the actual number of entries and the mask
1498 * from the number of bits we can fit.
1500 nr_hash = 1UL << hash_bits;
1501 hash_mask = nr_hash-1;
1503 printk("Mount-cache hash table entries: %d (order: %ld, %ld bytes)\n",
1504 nr_hash, order, (PAGE_SIZE << order));
1506 /* And initialize the newly allocated array */
1507 d = mount_hashtable;
1519 void __put_namespace(struct namespace *namespace)
1521 struct vfsmount *mnt;
1523 down_write(&namespace->sem);
1524 spin_lock(&vfsmount_lock);
1526 list_for_each_entry(mnt, &namespace->list, mnt_list) {
1527 mnt->mnt_namespace = NULL;
1530 umount_tree(namespace->root);
1531 spin_unlock(&vfsmount_lock);
1532 up_write(&namespace->sem);