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 <linux/vs_base.h>
25 #include <linux/vserver/namespace.h>
27 #include <asm/uaccess.h>
28 #include <asm/unistd.h>
30 extern int __init init_rootfs(void);
33 extern int __init sysfs_init(void);
35 static inline int sysfs_init(void)
41 /* spinlock for vfsmount related operations, inplace of dcache_lock */
42 spinlock_t vfsmount_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
44 static struct list_head *mount_hashtable;
45 static int hash_mask, hash_bits;
46 static kmem_cache_t *mnt_cache;
48 static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
50 unsigned long tmp = ((unsigned long) mnt / L1_CACHE_BYTES);
51 tmp += ((unsigned long) dentry / L1_CACHE_BYTES);
52 tmp = tmp + (tmp >> hash_bits);
53 return tmp & hash_mask;
56 struct vfsmount *alloc_vfsmnt(const char *name)
58 struct vfsmount *mnt = kmem_cache_alloc(mnt_cache, GFP_KERNEL);
60 memset(mnt, 0, sizeof(struct vfsmount));
61 atomic_set(&mnt->mnt_count,1);
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);
66 INIT_LIST_HEAD(&mnt->mnt_fslink);
68 int size = strlen(name)+1;
69 char *newname = kmalloc(size, GFP_KERNEL);
71 memcpy(newname, name, size);
72 mnt->mnt_devname = newname;
79 void free_vfsmnt(struct vfsmount *mnt)
81 kfree(mnt->mnt_devname);
82 kmem_cache_free(mnt_cache, mnt);
86 * Now, lookup_mnt increments the ref count before returning
87 * the vfsmount struct.
89 struct vfsmount *lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
91 struct list_head * head = mount_hashtable + hash(mnt, dentry);
92 struct list_head * tmp = head;
93 struct vfsmount *p, *found = NULL;
95 spin_lock(&vfsmount_lock);
101 p = list_entry(tmp, struct vfsmount, mnt_hash);
102 if (p->mnt_parent == mnt && p->mnt_mountpoint == dentry) {
107 spin_unlock(&vfsmount_lock);
111 EXPORT_SYMBOL(lookup_mnt);
113 static inline int check_mnt(struct vfsmount *mnt)
115 return mnt->mnt_namespace == current->namespace;
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;
167 mnt->mnt_namespace = old->mnt_namespace;
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 {
230 { MS_SYNCHRONOUS, ",sync" },
231 { MS_DIRSYNC, ",dirsync" },
232 { MS_MANDLOCK, ",mand" },
233 { MS_NOATIME, ",noatime" },
234 { MS_NODIRATIME, ",nodiratime" },
235 { MS_TAGXID, ",tagxid" },
238 static struct proc_fs_info mnt_info[] = {
239 { MNT_NOSUID, ",nosuid" },
240 { MNT_NODEV, ",nodev" },
241 { MNT_NOEXEC, ",noexec" },
244 struct proc_fs_info *fs_infop;
246 if (vx_flags(VXF_HIDE_MOUNT, 0))
248 if (!vx_check_vfsmount(current->vx_info, mnt))
251 mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
253 seq_path(m, mnt, mnt->mnt_root, " \t\n\\");
255 mangle(m, mnt->mnt_sb->s_type->name);
256 seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? " ro" : " rw");
257 for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
258 if (mnt->mnt_sb->s_flags & fs_infop->flag)
259 seq_puts(m, fs_infop->str);
261 for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
262 if (mnt->mnt_flags & fs_infop->flag)
263 seq_puts(m, fs_infop->str);
265 if (mnt->mnt_sb->s_op->show_options)
266 err = mnt->mnt_sb->s_op->show_options(m, mnt);
267 seq_puts(m, " 0 0\n");
271 struct seq_operations mounts_op = {
279 * may_umount_tree - check if a mount tree is busy
280 * @mnt: root of mount tree
282 * This is called to check if a tree of mounts has any
283 * open files, pwds, chroots or sub mounts that are
286 int may_umount_tree(struct vfsmount *mnt)
288 struct list_head *next;
289 struct vfsmount *this_parent = mnt;
293 spin_lock(&vfsmount_lock);
294 actual_refs = atomic_read(&mnt->mnt_count);
297 next = this_parent->mnt_mounts.next;
299 while (next != &this_parent->mnt_mounts) {
300 struct vfsmount *p = list_entry(next, struct vfsmount, mnt_child);
304 actual_refs += atomic_read(&p->mnt_count);
307 if (!list_empty(&p->mnt_mounts)) {
313 if (this_parent != mnt) {
314 next = this_parent->mnt_child.next;
315 this_parent = this_parent->mnt_parent;
318 spin_unlock(&vfsmount_lock);
320 if (actual_refs > minimum_refs)
326 EXPORT_SYMBOL(may_umount_tree);
329 * may_umount - check if a mount point is busy
330 * @mnt: root of mount
332 * This is called to check if a mount point has any
333 * open files, pwds, chroots or sub mounts. If the
334 * mount has sub mounts this will return busy
335 * regardless of whether the sub mounts are busy.
337 * Doesn't take quota and stuff into account. IOW, in some cases it will
338 * give false negatives. The main reason why it's here is that we need
339 * a non-destructive way to look for easily umountable filesystems.
341 int may_umount(struct vfsmount *mnt)
343 if (atomic_read(&mnt->mnt_count) > 2)
348 EXPORT_SYMBOL(may_umount);
350 static inline void __umount_tree(struct vfsmount *mnt, struct list_head *kill)
352 while (!list_empty(kill)) {
353 mnt = list_entry(kill->next, struct vfsmount, mnt_list);
354 list_del_init(&mnt->mnt_list);
355 list_del_init(&mnt->mnt_fslink);
356 if (mnt->mnt_parent == mnt) {
357 spin_unlock(&vfsmount_lock);
359 struct nameidata old_nd;
360 detach_mnt(mnt, &old_nd);
361 spin_unlock(&vfsmount_lock);
362 path_release(&old_nd);
365 spin_lock(&vfsmount_lock);
369 void umount_tree(struct vfsmount *mnt)
374 for (p = mnt; p; p = next_mnt(p, mnt)) {
375 list_del(&p->mnt_list);
376 list_add(&p->mnt_list, &kill);
378 __umount_tree(mnt, &kill);
381 void umount_unused(struct vfsmount *mnt, struct fs_struct *fs)
386 for (p = mnt; p; p = next_mnt(p, mnt)) {
387 if (p == fs->rootmnt || p == fs->pwdmnt)
389 list_del(&p->mnt_list);
390 list_add(&p->mnt_list, &kill);
392 __umount_tree(mnt, &kill);
395 static int do_umount(struct vfsmount *mnt, int flags)
397 struct super_block * sb = mnt->mnt_sb;
400 retval = security_sb_umount(mnt, flags);
405 * Allow userspace to request a mountpoint be expired rather than
406 * unmounting unconditionally. Unmount only happens if:
407 * (1) the mark is already set (the mark is cleared by mntput())
408 * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
410 if (flags & MNT_EXPIRE) {
411 if (mnt == current->fs->rootmnt ||
412 flags & (MNT_FORCE | MNT_DETACH))
415 if (atomic_read(&mnt->mnt_count) != 2)
418 if (!xchg(&mnt->mnt_expiry_mark, 1))
423 * If we may have to abort operations to get out of this
424 * mount, and they will themselves hold resources we must
425 * allow the fs to do things. In the Unix tradition of
426 * 'Gee thats tricky lets do it in userspace' the umount_begin
427 * might fail to complete on the first run through as other tasks
428 * must return, and the like. Thats for the mount program to worry
429 * about for the moment.
433 if( (flags&MNT_FORCE) && sb->s_op->umount_begin)
434 sb->s_op->umount_begin(sb);
438 * No sense to grab the lock for this test, but test itself looks
439 * somewhat bogus. Suggestions for better replacement?
440 * Ho-hum... In principle, we might treat that as umount + switch
441 * to rootfs. GC would eventually take care of the old vfsmount.
442 * Actually it makes sense, especially if rootfs would contain a
443 * /reboot - static binary that would close all descriptors and
444 * call reboot(9). Then init(8) could umount root and exec /reboot.
446 if (mnt == current->fs->rootmnt && !(flags & MNT_DETACH)) {
448 * Special case for "unmounting" root ...
449 * we just try to remount it readonly.
451 down_write(&sb->s_umount);
452 if (!(sb->s_flags & MS_RDONLY)) {
454 retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
457 up_write(&sb->s_umount);
461 down_write(¤t->namespace->sem);
462 spin_lock(&vfsmount_lock);
464 if (atomic_read(&sb->s_active) == 1) {
465 /* last instance - try to be smart */
466 spin_unlock(&vfsmount_lock);
471 security_sb_umount_close(mnt);
472 spin_lock(&vfsmount_lock);
475 if (atomic_read(&mnt->mnt_count) == 2 || flags & MNT_DETACH) {
476 if (!list_empty(&mnt->mnt_list))
480 spin_unlock(&vfsmount_lock);
482 security_sb_umount_busy(mnt);
483 up_write(¤t->namespace->sem);
488 * Now umount can handle mount points as well as block devices.
489 * This is important for filesystems which use unnamed block devices.
491 * We now support a flag for forced unmount like the other 'big iron'
492 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
495 asmlinkage long sys_umount(char __user * name, int flags)
500 retval = __user_walk(name, LOOKUP_FOLLOW, &nd);
504 if (nd.dentry != nd.mnt->mnt_root)
506 if (!check_mnt(nd.mnt))
510 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SECURE_MOUNT))
513 retval = do_umount(nd.mnt, flags);
515 path_release_on_umount(&nd);
520 #ifdef __ARCH_WANT_SYS_OLDUMOUNT
523 * The 2.0 compatible umount. No flags.
526 asmlinkage long sys_oldumount(char __user * name)
528 return sys_umount(name,0);
533 static int mount_is_safe(struct nameidata *nd)
535 if (capable(CAP_SYS_ADMIN))
537 if (vx_ccaps(VXC_SECURE_MOUNT))
541 if (S_ISLNK(nd->dentry->d_inode->i_mode))
543 if (nd->dentry->d_inode->i_mode & S_ISVTX) {
544 if (current->uid != nd->dentry->d_inode->i_uid)
547 if (permission(nd->dentry->d_inode, MAY_WRITE, nd))
554 lives_below_in_same_fs(struct dentry *d, struct dentry *dentry)
559 if (d == NULL || d == d->d_parent)
565 static struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry)
567 struct vfsmount *res, *p, *q, *r, *s;
571 res = q = clone_mnt(mnt, dentry);
574 q->mnt_mountpoint = mnt->mnt_mountpoint;
577 for (h = mnt->mnt_mounts.next; h != &mnt->mnt_mounts; h = h->next) {
578 r = list_entry(h, struct vfsmount, mnt_child);
579 if (!lives_below_in_same_fs(r->mnt_mountpoint, dentry))
582 for (s = r; s; s = next_mnt(s, r)) {
583 while (p != s->mnt_parent) {
589 nd.dentry = p->mnt_mountpoint;
590 q = clone_mnt(p, p->mnt_root);
593 spin_lock(&vfsmount_lock);
594 list_add_tail(&q->mnt_list, &res->mnt_list);
596 spin_unlock(&vfsmount_lock);
602 spin_lock(&vfsmount_lock);
604 spin_unlock(&vfsmount_lock);
609 static int graft_tree(struct vfsmount *mnt, struct nameidata *nd)
612 if (mnt->mnt_sb->s_flags & MS_NOUSER)
615 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
616 S_ISDIR(mnt->mnt_root->d_inode->i_mode))
620 down(&nd->dentry->d_inode->i_sem);
621 if (IS_DEADDIR(nd->dentry->d_inode))
624 err = security_sb_check_sb(mnt, nd);
629 spin_lock(&vfsmount_lock);
630 if (IS_ROOT(nd->dentry) || !d_unhashed(nd->dentry)) {
631 struct list_head head;
634 list_add_tail(&head, &mnt->mnt_list);
635 list_splice(&head, current->namespace->list.prev);
639 spin_unlock(&vfsmount_lock);
641 up(&nd->dentry->d_inode->i_sem);
643 security_sb_post_addmount(mnt, nd);
650 static int do_loopback(struct nameidata *nd, char *old_name, int recurse)
652 struct nameidata old_nd;
653 struct vfsmount *mnt = NULL;
654 int err = mount_is_safe(nd);
657 if (!old_name || !*old_name)
659 err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
663 down_write(¤t->namespace->sem);
665 if (check_mnt(nd->mnt) && (!recurse || check_mnt(old_nd.mnt))) {
668 mnt = copy_tree(old_nd.mnt, old_nd.dentry);
670 mnt = clone_mnt(old_nd.mnt, old_nd.dentry);
674 /* stop bind mounts from expiring */
675 spin_lock(&vfsmount_lock);
676 list_del_init(&mnt->mnt_fslink);
677 spin_unlock(&vfsmount_lock);
679 err = graft_tree(mnt, nd);
681 spin_lock(&vfsmount_lock);
683 spin_unlock(&vfsmount_lock);
688 up_write(¤t->namespace->sem);
689 path_release(&old_nd);
694 * change filesystem flags. dir should be a physical root of filesystem.
695 * If you've mounted a non-root directory somewhere and want to do remount
696 * on it - tough luck.
699 static int do_remount(struct nameidata *nd, int flags, int mnt_flags,
703 struct super_block * sb = nd->mnt->mnt_sb;
705 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SECURE_REMOUNT))
708 if (!check_mnt(nd->mnt))
711 if (nd->dentry != nd->mnt->mnt_root)
714 if (vx_ccaps(VXC_SECURE_REMOUNT))
715 mnt_flags |= MNT_NODEV;
716 down_write(&sb->s_umount);
717 err = do_remount_sb(sb, flags, data, 0);
719 nd->mnt->mnt_flags=mnt_flags;
720 up_write(&sb->s_umount);
722 security_sb_post_remount(nd->mnt, flags, data);
726 static int do_move_mount(struct nameidata *nd, char *old_name)
728 struct nameidata old_nd, parent_nd;
731 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SECURE_MOUNT))
733 if (!old_name || !*old_name)
735 err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
739 down_write(¤t->namespace->sem);
740 while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
743 if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt))
747 down(&nd->dentry->d_inode->i_sem);
748 if (IS_DEADDIR(nd->dentry->d_inode))
751 spin_lock(&vfsmount_lock);
752 if (!IS_ROOT(nd->dentry) && d_unhashed(nd->dentry))
756 if (old_nd.dentry != old_nd.mnt->mnt_root)
759 if (old_nd.mnt == old_nd.mnt->mnt_parent)
762 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
763 S_ISDIR(old_nd.dentry->d_inode->i_mode))
767 for (p = nd->mnt; p->mnt_parent!=p; p = p->mnt_parent)
772 detach_mnt(old_nd.mnt, &parent_nd);
773 attach_mnt(old_nd.mnt, nd);
775 /* if the mount is moved, it should no longer be expire
777 list_del_init(&old_nd.mnt->mnt_fslink);
779 spin_unlock(&vfsmount_lock);
781 up(&nd->dentry->d_inode->i_sem);
783 up_write(¤t->namespace->sem);
785 path_release(&parent_nd);
786 path_release(&old_nd);
791 * create a new mount for userspace and request it to be added into the
794 static int do_new_mount(struct nameidata *nd, char *type, int flags,
795 int mnt_flags, char *name, void *data)
797 struct vfsmount *mnt;
799 if (!type || !memchr(type, 0, PAGE_SIZE))
802 /* we need capabilities... */
803 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SECURE_MOUNT))
806 mnt = do_kern_mount(type, flags, name, data);
810 return do_add_mount(mnt, nd, mnt_flags, NULL);
814 * add a mount into a namespace's mount tree
815 * - provide the option of adding the new mount to an expiration list
817 int do_add_mount(struct vfsmount *newmnt, struct nameidata *nd,
818 int mnt_flags, struct list_head *fslist)
822 down_write(¤t->namespace->sem);
823 /* Something was mounted here while we slept */
824 while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
827 if (!check_mnt(nd->mnt))
830 /* Refuse the same filesystem on the same mount point */
832 if (nd->mnt->mnt_sb == newmnt->mnt_sb &&
833 nd->mnt->mnt_root == nd->dentry)
837 if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
840 newmnt->mnt_flags = mnt_flags;
841 err = graft_tree(newmnt, nd);
843 if (err == 0 && fslist) {
844 /* add to the specified expiration list */
845 spin_lock(&vfsmount_lock);
846 list_add_tail(&newmnt->mnt_fslink, fslist);
847 spin_unlock(&vfsmount_lock);
851 up_write(¤t->namespace->sem);
856 EXPORT_SYMBOL_GPL(do_add_mount);
859 * process a list of expirable mountpoints with the intent of discarding any
860 * mountpoints that aren't in use and haven't been touched since last we came
863 void mark_mounts_for_expiry(struct list_head *mounts)
865 struct namespace *namespace;
866 struct vfsmount *mnt, *next;
867 LIST_HEAD(graveyard);
869 if (list_empty(mounts))
872 spin_lock(&vfsmount_lock);
874 /* extract from the expiration list every vfsmount that matches the
875 * following criteria:
876 * - only referenced by its parent vfsmount
877 * - still marked for expiry (marked on the last call here; marks are
878 * cleared by mntput())
880 list_for_each_entry_safe(mnt, next, mounts, mnt_fslink) {
881 if (!xchg(&mnt->mnt_expiry_mark, 1) ||
882 atomic_read(&mnt->mnt_count) != 1)
886 list_move(&mnt->mnt_fslink, &graveyard);
890 * go through the vfsmounts we've just consigned to the graveyard to
891 * - check that they're still dead
892 * - delete the vfsmount from the appropriate namespace under lock
893 * - dispose of the corpse
895 while (!list_empty(&graveyard)) {
896 mnt = list_entry(graveyard.next, struct vfsmount, mnt_fslink);
897 list_del_init(&mnt->mnt_fslink);
899 /* don't do anything if the namespace is dead - all the
900 * vfsmounts from it are going away anyway */
901 namespace = mnt->mnt_namespace;
902 if (!namespace || atomic_read(&namespace->count) <= 0)
904 get_namespace(namespace);
906 spin_unlock(&vfsmount_lock);
907 down_write(&namespace->sem);
908 spin_lock(&vfsmount_lock);
910 /* check that it is still dead: the count should now be 2 - as
911 * contributed by the vfsmount parent and the mntget above */
912 if (atomic_read(&mnt->mnt_count) == 2) {
913 struct vfsmount *xdmnt;
914 struct dentry *xdentry;
916 /* delete from the namespace */
917 list_del_init(&mnt->mnt_list);
918 list_del_init(&mnt->mnt_child);
919 list_del_init(&mnt->mnt_hash);
920 mnt->mnt_mountpoint->d_mounted--;
922 xdentry = mnt->mnt_mountpoint;
923 mnt->mnt_mountpoint = mnt->mnt_root;
924 xdmnt = mnt->mnt_parent;
925 mnt->mnt_parent = mnt;
927 spin_unlock(&vfsmount_lock);
932 /* now lay it to rest if this was the last ref on the
934 if (atomic_read(&mnt->mnt_sb->s_active) == 1) {
935 /* last instance - try to be smart */
937 DQUOT_OFF(mnt->mnt_sb);
938 acct_auto_close(mnt->mnt_sb);
944 /* someone brought it back to life whilst we didn't
945 * have any locks held so return it to the expiration
947 list_add_tail(&mnt->mnt_fslink, mounts);
948 spin_unlock(&vfsmount_lock);
951 up_write(&namespace->sem);
954 put_namespace(namespace);
956 spin_lock(&vfsmount_lock);
959 spin_unlock(&vfsmount_lock);
962 EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
965 * Some copy_from_user() implementations do not return the exact number of
966 * bytes remaining to copy on a fault. But copy_mount_options() requires that.
967 * Note that this function differs from copy_from_user() in that it will oops
968 * on bad values of `to', rather than returning a short copy.
971 exact_copy_from_user(void *to, const void __user *from, unsigned long n)
974 const char __user *f = from;
977 if (!access_ok(VERIFY_READ, from, n))
981 if (__get_user(c, f)) {
992 int copy_mount_options(const void __user *data, unsigned long *where)
1002 if (!(page = __get_free_page(GFP_KERNEL)))
1005 /* We only care that *some* data at the address the user
1006 * gave us is valid. Just in case, we'll zero
1007 * the remainder of the page.
1009 /* copy_from_user cannot cross TASK_SIZE ! */
1010 size = TASK_SIZE - (unsigned long)data;
1011 if (size > PAGE_SIZE)
1014 i = size - exact_copy_from_user((void *)page, data, size);
1020 memset((char *)page + i, 0, PAGE_SIZE - i);
1026 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
1027 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
1029 * data is a (void *) that can point to any structure up to
1030 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
1031 * information (or be NULL).
1033 * Pre-0.97 versions of mount() didn't have a flags word.
1034 * When the flags word was introduced its top half was required
1035 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
1036 * Therefore, if this magic number is present, it carries no information
1037 * and must be discarded.
1039 long do_mount(char * dev_name, char * dir_name, char *type_page,
1040 unsigned long flags, void *data_page)
1042 struct nameidata nd;
1047 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
1048 flags &= ~MS_MGC_MSK;
1050 /* Basic sanity checks */
1052 if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
1054 if (dev_name && !memchr(dev_name, 0, PAGE_SIZE))
1058 ((char *)data_page)[PAGE_SIZE - 1] = 0;
1060 /* Separate the per-mountpoint flags */
1061 if (flags & MS_NOSUID)
1062 mnt_flags |= MNT_NOSUID;
1063 if (flags & MS_NODEV)
1064 mnt_flags |= MNT_NODEV;
1065 if (flags & MS_NOEXEC)
1066 mnt_flags |= MNT_NOEXEC;
1067 flags &= ~(MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_ACTIVE);
1069 if (vx_ccaps(VXC_SECURE_MOUNT))
1070 mnt_flags |= MNT_NODEV;
1072 /* ... and get the mountpoint */
1073 retval = path_lookup(dir_name, LOOKUP_FOLLOW, &nd);
1077 retval = security_sb_mount(dev_name, &nd, type_page, flags, data_page);
1081 if (flags & MS_REMOUNT)
1082 retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags,
1084 else if (flags & MS_BIND)
1085 retval = do_loopback(&nd, dev_name, flags & MS_REC);
1086 else if (flags & MS_MOVE)
1087 retval = do_move_mount(&nd, dev_name);
1089 retval = do_new_mount(&nd, type_page, flags, mnt_flags,
1090 dev_name, data_page);
1096 int copy_namespace(int flags, struct task_struct *tsk)
1098 struct namespace *namespace = tsk->namespace;
1099 struct namespace *new_ns;
1100 struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL;
1101 struct fs_struct *fs = tsk->fs;
1102 struct vfsmount *p, *q;
1107 get_namespace(namespace);
1109 if (!(flags & CLONE_NEWNS))
1112 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_SECURE_MOUNT)) {
1113 put_namespace(namespace);
1117 new_ns = kmalloc(sizeof(struct namespace), GFP_KERNEL);
1121 atomic_set(&new_ns->count, 1);
1122 init_rwsem(&new_ns->sem);
1123 INIT_LIST_HEAD(&new_ns->list);
1125 down_write(&tsk->namespace->sem);
1126 /* First pass: copy the tree topology */
1127 new_ns->root = copy_tree(namespace->root, namespace->root->mnt_root);
1128 if (!new_ns->root) {
1129 up_write(&tsk->namespace->sem);
1133 spin_lock(&vfsmount_lock);
1134 list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
1135 spin_unlock(&vfsmount_lock);
1138 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
1139 * as belonging to new namespace. We have already acquired a private
1140 * fs_struct, so tsk->fs->lock is not needed.
1142 p = namespace->root;
1145 q->mnt_namespace = new_ns;
1147 if (p == fs->rootmnt) {
1149 fs->rootmnt = mntget(q);
1151 if (p == fs->pwdmnt) {
1153 fs->pwdmnt = mntget(q);
1155 if (p == fs->altrootmnt) {
1157 fs->altrootmnt = mntget(q);
1160 p = next_mnt(p, namespace->root);
1161 q = next_mnt(q, new_ns->root);
1163 up_write(&tsk->namespace->sem);
1165 tsk->namespace = new_ns;
1174 put_namespace(namespace);
1178 put_namespace(namespace);
1182 asmlinkage long sys_mount(char __user * dev_name, char __user * dir_name,
1183 char __user * type, unsigned long flags,
1187 unsigned long data_page;
1188 unsigned long type_page;
1189 unsigned long dev_page;
1192 retval = copy_mount_options (type, &type_page);
1196 dir_page = getname(dir_name);
1197 retval = PTR_ERR(dir_page);
1198 if (IS_ERR(dir_page))
1201 retval = copy_mount_options (dev_name, &dev_page);
1205 retval = copy_mount_options (data, &data_page);
1210 retval = do_mount((char*)dev_page, dir_page, (char*)type_page,
1211 flags, (void*)data_page);
1213 free_page(data_page);
1216 free_page(dev_page);
1220 free_page(type_page);
1225 * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
1226 * It can block. Requires the big lock held.
1228 void set_fs_root(struct fs_struct *fs, struct vfsmount *mnt,
1229 struct dentry *dentry)
1231 struct dentry *old_root;
1232 struct vfsmount *old_rootmnt;
1233 write_lock(&fs->lock);
1234 old_root = fs->root;
1235 old_rootmnt = fs->rootmnt;
1236 fs->rootmnt = mntget(mnt);
1237 fs->root = dget(dentry);
1238 write_unlock(&fs->lock);
1241 mntput(old_rootmnt);
1245 EXPORT_SYMBOL(set_fs_root);
1248 * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
1249 * It can block. Requires the big lock held.
1251 void set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
1252 struct dentry *dentry)
1254 struct dentry *old_pwd;
1255 struct vfsmount *old_pwdmnt;
1257 write_lock(&fs->lock);
1259 old_pwdmnt = fs->pwdmnt;
1260 fs->pwdmnt = mntget(mnt);
1261 fs->pwd = dget(dentry);
1262 write_unlock(&fs->lock);
1270 EXPORT_SYMBOL(set_fs_pwd);
1272 static void chroot_fs_refs(struct nameidata *old_nd, struct nameidata *new_nd)
1274 struct task_struct *g, *p;
1275 struct fs_struct *fs;
1277 read_lock(&tasklist_lock);
1278 do_each_thread(g, p) {
1282 atomic_inc(&fs->count);
1284 if (fs->root==old_nd->dentry&&fs->rootmnt==old_nd->mnt)
1285 set_fs_root(fs, new_nd->mnt, new_nd->dentry);
1286 if (fs->pwd==old_nd->dentry&&fs->pwdmnt==old_nd->mnt)
1287 set_fs_pwd(fs, new_nd->mnt, new_nd->dentry);
1291 } while_each_thread(g, p);
1292 read_unlock(&tasklist_lock);
1296 * Moves the current root to put_root, and sets root/cwd of all processes
1297 * which had them on the old root to new_root.
1300 * - we don't move root/cwd if they are not at the root (reason: if something
1301 * cared enough to change them, it's probably wrong to force them elsewhere)
1302 * - it's okay to pick a root that isn't the root of a file system, e.g.
1303 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
1304 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
1308 asmlinkage long sys_pivot_root(const char __user *new_root, const char __user *put_old)
1310 struct vfsmount *tmp;
1311 struct nameidata new_nd, old_nd, parent_nd, root_parent, user_nd;
1314 if (!capable(CAP_SYS_ADMIN))
1319 error = __user_walk(new_root, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &new_nd);
1323 if (!check_mnt(new_nd.mnt))
1326 error = __user_walk(put_old, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &old_nd);
1330 error = security_sb_pivotroot(&old_nd, &new_nd);
1332 path_release(&old_nd);
1336 read_lock(¤t->fs->lock);
1337 user_nd.mnt = mntget(current->fs->rootmnt);
1338 user_nd.dentry = dget(current->fs->root);
1339 read_unlock(¤t->fs->lock);
1340 down_write(¤t->namespace->sem);
1341 down(&old_nd.dentry->d_inode->i_sem);
1343 if (!check_mnt(user_nd.mnt))
1346 if (IS_DEADDIR(new_nd.dentry->d_inode))
1348 if (d_unhashed(new_nd.dentry) && !IS_ROOT(new_nd.dentry))
1350 if (d_unhashed(old_nd.dentry) && !IS_ROOT(old_nd.dentry))
1353 if (new_nd.mnt == user_nd.mnt || old_nd.mnt == user_nd.mnt)
1354 goto out2; /* loop */
1356 if (user_nd.mnt->mnt_root != user_nd.dentry)
1358 if (new_nd.mnt->mnt_root != new_nd.dentry)
1359 goto out2; /* not a mountpoint */
1360 tmp = old_nd.mnt; /* make sure we can reach put_old from new_root */
1361 spin_lock(&vfsmount_lock);
1362 if (tmp != new_nd.mnt) {
1364 if (tmp->mnt_parent == tmp)
1366 if (tmp->mnt_parent == new_nd.mnt)
1368 tmp = tmp->mnt_parent;
1370 if (!is_subdir(tmp->mnt_mountpoint, new_nd.dentry))
1372 } else if (!is_subdir(old_nd.dentry, new_nd.dentry))
1374 detach_mnt(new_nd.mnt, &parent_nd);
1375 detach_mnt(user_nd.mnt, &root_parent);
1376 attach_mnt(user_nd.mnt, &old_nd);
1377 attach_mnt(new_nd.mnt, &root_parent);
1378 spin_unlock(&vfsmount_lock);
1379 chroot_fs_refs(&user_nd, &new_nd);
1380 security_sb_post_pivotroot(&user_nd, &new_nd);
1382 path_release(&root_parent);
1383 path_release(&parent_nd);
1385 up(&old_nd.dentry->d_inode->i_sem);
1386 up_write(¤t->namespace->sem);
1387 path_release(&user_nd);
1388 path_release(&old_nd);
1390 path_release(&new_nd);
1395 spin_unlock(&vfsmount_lock);
1399 static void __init init_mount_tree(void)
1401 struct vfsmount *mnt;
1402 struct namespace *namespace;
1403 struct task_struct *g, *p;
1405 mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
1407 panic("Can't create rootfs");
1408 namespace = kmalloc(sizeof(*namespace), GFP_KERNEL);
1410 panic("Can't allocate initial namespace");
1411 atomic_set(&namespace->count, 1);
1412 INIT_LIST_HEAD(&namespace->list);
1413 init_rwsem(&namespace->sem);
1414 list_add(&mnt->mnt_list, &namespace->list);
1415 namespace->root = mnt;
1416 mnt->mnt_namespace = namespace;
1418 init_task.namespace = namespace;
1419 read_lock(&tasklist_lock);
1420 do_each_thread(g, p) {
1421 get_namespace(namespace);
1422 p->namespace = namespace;
1423 } while_each_thread(g, p);
1424 read_unlock(&tasklist_lock);
1426 set_fs_pwd(current->fs, namespace->root, namespace->root->mnt_root);
1427 set_fs_root(current->fs, namespace->root, namespace->root->mnt_root);
1430 void __init mnt_init(unsigned long mempages)
1432 struct list_head *d;
1433 unsigned long order;
1434 unsigned int nr_hash;
1437 mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
1438 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1441 mount_hashtable = (struct list_head *)
1442 __get_free_pages(GFP_ATOMIC, order);
1444 if (!mount_hashtable)
1445 panic("Failed to allocate mount hash table\n");
1448 * Find the power-of-two list-heads that can fit into the allocation..
1449 * We don't guarantee that "sizeof(struct list_head)" is necessarily
1452 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct list_head);
1456 } while ((nr_hash >> hash_bits) != 0);
1460 * Re-calculate the actual number of entries and the mask
1461 * from the number of bits we can fit.
1463 nr_hash = 1UL << hash_bits;
1464 hash_mask = nr_hash-1;
1466 printk("Mount-cache hash table entries: %d (order: %ld, %ld bytes)\n",
1467 nr_hash, order, (PAGE_SIZE << order));
1469 /* And initialize the newly allocated array */
1470 d = mount_hashtable;
1482 void __put_namespace(struct namespace *namespace)
1484 struct vfsmount *mnt;
1486 down_write(&namespace->sem);
1487 spin_lock(&vfsmount_lock);
1489 list_for_each_entry(mnt, &namespace->list, mnt_list) {
1490 mnt->mnt_namespace = NULL;
1493 umount_tree(namespace->root);
1494 spin_unlock(&vfsmount_lock);
1495 up_write(&namespace->sem);